i915_gem_stolen.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. * Copyright © 2008-2012 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Chris Wilson <chris@chris-wilson.co.uk>
  26. *
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #define KB(x) ((x) * 1024)
  32. #define MB(x) (KB(x) * 1024)
  33. /*
  34. * The BIOS typically reserves some of the system's memory for the exclusive
  35. * use of the integrated graphics. This memory is no longer available for
  36. * use by the OS and so the user finds that his system has less memory
  37. * available than he put in. We refer to this memory as stolen.
  38. *
  39. * The BIOS will allocate its framebuffer from the stolen memory. Our
  40. * goal is try to reuse that object for our own fbcon which must always
  41. * be available for panics. Anything else we can reuse the stolen memory
  42. * for is a boon.
  43. */
  44. int i915_gem_stolen_insert_node_in_range(struct drm_i915_private *dev_priv,
  45. struct drm_mm_node *node, u64 size,
  46. unsigned alignment, u64 start, u64 end)
  47. {
  48. int ret;
  49. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  50. return -ENODEV;
  51. /* See the comment at the drm_mm_init() call for more about this check.
  52. * WaSkipStolenMemoryFirstPage:bdw,chv (incomplete) */
  53. if (INTEL_INFO(dev_priv)->gen == 8 && start < 4096)
  54. start = 4096;
  55. mutex_lock(&dev_priv->mm.stolen_lock);
  56. ret = drm_mm_insert_node_in_range(&dev_priv->mm.stolen, node, size,
  57. alignment, start, end,
  58. DRM_MM_SEARCH_DEFAULT);
  59. mutex_unlock(&dev_priv->mm.stolen_lock);
  60. return ret;
  61. }
  62. int i915_gem_stolen_insert_node(struct drm_i915_private *dev_priv,
  63. struct drm_mm_node *node, u64 size,
  64. unsigned alignment)
  65. {
  66. return i915_gem_stolen_insert_node_in_range(dev_priv, node, size,
  67. alignment, 0,
  68. dev_priv->gtt.stolen_usable_size);
  69. }
  70. void i915_gem_stolen_remove_node(struct drm_i915_private *dev_priv,
  71. struct drm_mm_node *node)
  72. {
  73. mutex_lock(&dev_priv->mm.stolen_lock);
  74. drm_mm_remove_node(node);
  75. mutex_unlock(&dev_priv->mm.stolen_lock);
  76. }
  77. static unsigned long i915_stolen_to_physical(struct drm_device *dev)
  78. {
  79. struct drm_i915_private *dev_priv = dev->dev_private;
  80. struct resource *r;
  81. u32 base;
  82. /* Almost universally we can find the Graphics Base of Stolen Memory
  83. * at offset 0x5c in the igfx configuration space. On a few (desktop)
  84. * machines this is also mirrored in the bridge device at different
  85. * locations, or in the MCHBAR.
  86. *
  87. * On 865 we just check the TOUD register.
  88. *
  89. * On 830/845/85x the stolen memory base isn't available in any
  90. * register. We need to calculate it as TOM-TSEG_SIZE-stolen_size.
  91. *
  92. */
  93. base = 0;
  94. if (INTEL_INFO(dev)->gen >= 3) {
  95. /* Read Graphics Base of Stolen Memory directly */
  96. pci_read_config_dword(dev->pdev, 0x5c, &base);
  97. base &= ~((1<<20) - 1);
  98. } else if (IS_I865G(dev)) {
  99. u32 tseg_size = 0;
  100. u16 toud = 0;
  101. u8 tmp;
  102. pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0),
  103. I845_ESMRAMC, &tmp);
  104. if (tmp & TSEG_ENABLE) {
  105. switch (tmp & I845_TSEG_SIZE_MASK) {
  106. case I845_TSEG_SIZE_512K:
  107. tseg_size = KB(512);
  108. break;
  109. case I845_TSEG_SIZE_1M:
  110. tseg_size = MB(1);
  111. break;
  112. }
  113. }
  114. pci_bus_read_config_word(dev->pdev->bus, PCI_DEVFN(0, 0),
  115. I865_TOUD, &toud);
  116. base = (toud << 16) + tseg_size;
  117. } else if (IS_I85X(dev)) {
  118. u32 tseg_size = 0;
  119. u32 tom;
  120. u8 tmp;
  121. pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0),
  122. I85X_ESMRAMC, &tmp);
  123. if (tmp & TSEG_ENABLE)
  124. tseg_size = MB(1);
  125. pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 1),
  126. I85X_DRB3, &tmp);
  127. tom = tmp * MB(32);
  128. base = tom - tseg_size - dev_priv->gtt.stolen_size;
  129. } else if (IS_845G(dev)) {
  130. u32 tseg_size = 0;
  131. u32 tom;
  132. u8 tmp;
  133. pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0),
  134. I845_ESMRAMC, &tmp);
  135. if (tmp & TSEG_ENABLE) {
  136. switch (tmp & I845_TSEG_SIZE_MASK) {
  137. case I845_TSEG_SIZE_512K:
  138. tseg_size = KB(512);
  139. break;
  140. case I845_TSEG_SIZE_1M:
  141. tseg_size = MB(1);
  142. break;
  143. }
  144. }
  145. pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0),
  146. I830_DRB3, &tmp);
  147. tom = tmp * MB(32);
  148. base = tom - tseg_size - dev_priv->gtt.stolen_size;
  149. } else if (IS_I830(dev)) {
  150. u32 tseg_size = 0;
  151. u32 tom;
  152. u8 tmp;
  153. pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0),
  154. I830_ESMRAMC, &tmp);
  155. if (tmp & TSEG_ENABLE) {
  156. if (tmp & I830_TSEG_SIZE_1M)
  157. tseg_size = MB(1);
  158. else
  159. tseg_size = KB(512);
  160. }
  161. pci_bus_read_config_byte(dev->pdev->bus, PCI_DEVFN(0, 0),
  162. I830_DRB3, &tmp);
  163. tom = tmp * MB(32);
  164. base = tom - tseg_size - dev_priv->gtt.stolen_size;
  165. }
  166. if (base == 0)
  167. return 0;
  168. /* make sure we don't clobber the GTT if it's within stolen memory */
  169. if (INTEL_INFO(dev)->gen <= 4 && !IS_G33(dev) && !IS_G4X(dev)) {
  170. struct {
  171. u32 start, end;
  172. } stolen[2] = {
  173. { .start = base, .end = base + dev_priv->gtt.stolen_size, },
  174. { .start = base, .end = base + dev_priv->gtt.stolen_size, },
  175. };
  176. u64 gtt_start, gtt_end;
  177. gtt_start = I915_READ(PGTBL_CTL);
  178. if (IS_GEN4(dev))
  179. gtt_start = (gtt_start & PGTBL_ADDRESS_LO_MASK) |
  180. (gtt_start & PGTBL_ADDRESS_HI_MASK) << 28;
  181. else
  182. gtt_start &= PGTBL_ADDRESS_LO_MASK;
  183. gtt_end = gtt_start + gtt_total_entries(dev_priv->gtt) * 4;
  184. if (gtt_start >= stolen[0].start && gtt_start < stolen[0].end)
  185. stolen[0].end = gtt_start;
  186. if (gtt_end > stolen[1].start && gtt_end <= stolen[1].end)
  187. stolen[1].start = gtt_end;
  188. /* pick the larger of the two chunks */
  189. if (stolen[0].end - stolen[0].start >
  190. stolen[1].end - stolen[1].start) {
  191. base = stolen[0].start;
  192. dev_priv->gtt.stolen_size = stolen[0].end - stolen[0].start;
  193. } else {
  194. base = stolen[1].start;
  195. dev_priv->gtt.stolen_size = stolen[1].end - stolen[1].start;
  196. }
  197. if (stolen[0].start != stolen[1].start ||
  198. stolen[0].end != stolen[1].end) {
  199. DRM_DEBUG_KMS("GTT within stolen memory at 0x%llx-0x%llx\n",
  200. (unsigned long long) gtt_start,
  201. (unsigned long long) gtt_end - 1);
  202. DRM_DEBUG_KMS("Stolen memory adjusted to 0x%x-0x%x\n",
  203. base, base + (u32) dev_priv->gtt.stolen_size - 1);
  204. }
  205. }
  206. /* Verify that nothing else uses this physical address. Stolen
  207. * memory should be reserved by the BIOS and hidden from the
  208. * kernel. So if the region is already marked as busy, something
  209. * is seriously wrong.
  210. */
  211. r = devm_request_mem_region(dev->dev, base, dev_priv->gtt.stolen_size,
  212. "Graphics Stolen Memory");
  213. if (r == NULL) {
  214. /*
  215. * One more attempt but this time requesting region from
  216. * base + 1, as we have seen that this resolves the region
  217. * conflict with the PCI Bus.
  218. * This is a BIOS w/a: Some BIOS wrap stolen in the root
  219. * PCI bus, but have an off-by-one error. Hence retry the
  220. * reservation starting from 1 instead of 0.
  221. */
  222. r = devm_request_mem_region(dev->dev, base + 1,
  223. dev_priv->gtt.stolen_size - 1,
  224. "Graphics Stolen Memory");
  225. /*
  226. * GEN3 firmware likes to smash pci bridges into the stolen
  227. * range. Apparently this works.
  228. */
  229. if (r == NULL && !IS_GEN3(dev)) {
  230. DRM_ERROR("conflict detected with stolen region: [0x%08x - 0x%08x]\n",
  231. base, base + (uint32_t)dev_priv->gtt.stolen_size);
  232. base = 0;
  233. }
  234. }
  235. return base;
  236. }
  237. void i915_gem_cleanup_stolen(struct drm_device *dev)
  238. {
  239. struct drm_i915_private *dev_priv = dev->dev_private;
  240. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  241. return;
  242. drm_mm_takedown(&dev_priv->mm.stolen);
  243. }
  244. static void g4x_get_stolen_reserved(struct drm_i915_private *dev_priv,
  245. unsigned long *base, unsigned long *size)
  246. {
  247. uint32_t reg_val = I915_READ(IS_GM45(dev_priv) ?
  248. CTG_STOLEN_RESERVED :
  249. ELK_STOLEN_RESERVED);
  250. unsigned long stolen_top = dev_priv->mm.stolen_base +
  251. dev_priv->gtt.stolen_size;
  252. *base = (reg_val & G4X_STOLEN_RESERVED_ADDR2_MASK) << 16;
  253. WARN_ON((reg_val & G4X_STOLEN_RESERVED_ADDR1_MASK) < *base);
  254. /* On these platforms, the register doesn't have a size field, so the
  255. * size is the distance between the base and the top of the stolen
  256. * memory. We also have the genuine case where base is zero and there's
  257. * nothing reserved. */
  258. if (*base == 0)
  259. *size = 0;
  260. else
  261. *size = stolen_top - *base;
  262. }
  263. static void gen6_get_stolen_reserved(struct drm_i915_private *dev_priv,
  264. unsigned long *base, unsigned long *size)
  265. {
  266. uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
  267. *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK;
  268. switch (reg_val & GEN6_STOLEN_RESERVED_SIZE_MASK) {
  269. case GEN6_STOLEN_RESERVED_1M:
  270. *size = 1024 * 1024;
  271. break;
  272. case GEN6_STOLEN_RESERVED_512K:
  273. *size = 512 * 1024;
  274. break;
  275. case GEN6_STOLEN_RESERVED_256K:
  276. *size = 256 * 1024;
  277. break;
  278. case GEN6_STOLEN_RESERVED_128K:
  279. *size = 128 * 1024;
  280. break;
  281. default:
  282. *size = 1024 * 1024;
  283. MISSING_CASE(reg_val & GEN6_STOLEN_RESERVED_SIZE_MASK);
  284. }
  285. }
  286. static void gen7_get_stolen_reserved(struct drm_i915_private *dev_priv,
  287. unsigned long *base, unsigned long *size)
  288. {
  289. uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
  290. *base = reg_val & GEN7_STOLEN_RESERVED_ADDR_MASK;
  291. switch (reg_val & GEN7_STOLEN_RESERVED_SIZE_MASK) {
  292. case GEN7_STOLEN_RESERVED_1M:
  293. *size = 1024 * 1024;
  294. break;
  295. case GEN7_STOLEN_RESERVED_256K:
  296. *size = 256 * 1024;
  297. break;
  298. default:
  299. *size = 1024 * 1024;
  300. MISSING_CASE(reg_val & GEN7_STOLEN_RESERVED_SIZE_MASK);
  301. }
  302. }
  303. static void gen8_get_stolen_reserved(struct drm_i915_private *dev_priv,
  304. unsigned long *base, unsigned long *size)
  305. {
  306. uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
  307. *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK;
  308. switch (reg_val & GEN8_STOLEN_RESERVED_SIZE_MASK) {
  309. case GEN8_STOLEN_RESERVED_1M:
  310. *size = 1024 * 1024;
  311. break;
  312. case GEN8_STOLEN_RESERVED_2M:
  313. *size = 2 * 1024 * 1024;
  314. break;
  315. case GEN8_STOLEN_RESERVED_4M:
  316. *size = 4 * 1024 * 1024;
  317. break;
  318. case GEN8_STOLEN_RESERVED_8M:
  319. *size = 8 * 1024 * 1024;
  320. break;
  321. default:
  322. *size = 8 * 1024 * 1024;
  323. MISSING_CASE(reg_val & GEN8_STOLEN_RESERVED_SIZE_MASK);
  324. }
  325. }
  326. static void bdw_get_stolen_reserved(struct drm_i915_private *dev_priv,
  327. unsigned long *base, unsigned long *size)
  328. {
  329. uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
  330. unsigned long stolen_top;
  331. stolen_top = dev_priv->mm.stolen_base + dev_priv->gtt.stolen_size;
  332. *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK;
  333. /* On these platforms, the register doesn't have a size field, so the
  334. * size is the distance between the base and the top of the stolen
  335. * memory. We also have the genuine case where base is zero and there's
  336. * nothing reserved. */
  337. if (*base == 0)
  338. *size = 0;
  339. else
  340. *size = stolen_top - *base;
  341. }
  342. int i915_gem_init_stolen(struct drm_device *dev)
  343. {
  344. struct drm_i915_private *dev_priv = dev->dev_private;
  345. unsigned long reserved_total, reserved_base = 0, reserved_size;
  346. unsigned long stolen_top;
  347. mutex_init(&dev_priv->mm.stolen_lock);
  348. #ifdef CONFIG_INTEL_IOMMU
  349. if (intel_iommu_gfx_mapped && INTEL_INFO(dev)->gen < 8) {
  350. DRM_INFO("DMAR active, disabling use of stolen memory\n");
  351. return 0;
  352. }
  353. #endif
  354. if (dev_priv->gtt.stolen_size == 0)
  355. return 0;
  356. dev_priv->mm.stolen_base = i915_stolen_to_physical(dev);
  357. if (dev_priv->mm.stolen_base == 0)
  358. return 0;
  359. stolen_top = dev_priv->mm.stolen_base + dev_priv->gtt.stolen_size;
  360. switch (INTEL_INFO(dev_priv)->gen) {
  361. case 2:
  362. case 3:
  363. break;
  364. case 4:
  365. if (IS_G4X(dev))
  366. g4x_get_stolen_reserved(dev_priv, &reserved_base,
  367. &reserved_size);
  368. break;
  369. case 5:
  370. /* Assume the gen6 maximum for the older platforms. */
  371. reserved_size = 1024 * 1024;
  372. reserved_base = stolen_top - reserved_size;
  373. break;
  374. case 6:
  375. gen6_get_stolen_reserved(dev_priv, &reserved_base,
  376. &reserved_size);
  377. break;
  378. case 7:
  379. gen7_get_stolen_reserved(dev_priv, &reserved_base,
  380. &reserved_size);
  381. break;
  382. default:
  383. if (IS_BROADWELL(dev_priv) || IS_SKYLAKE(dev_priv))
  384. bdw_get_stolen_reserved(dev_priv, &reserved_base,
  385. &reserved_size);
  386. else
  387. gen8_get_stolen_reserved(dev_priv, &reserved_base,
  388. &reserved_size);
  389. break;
  390. }
  391. /* It is possible for the reserved base to be zero, but the register
  392. * field for size doesn't have a zero option. */
  393. if (reserved_base == 0) {
  394. reserved_size = 0;
  395. reserved_base = stolen_top;
  396. }
  397. if (reserved_base < dev_priv->mm.stolen_base ||
  398. reserved_base + reserved_size > stolen_top) {
  399. DRM_DEBUG_KMS("Stolen reserved area [0x%08lx - 0x%08lx] outside stolen memory [0x%08lx - 0x%08lx]\n",
  400. reserved_base, reserved_base + reserved_size,
  401. dev_priv->mm.stolen_base, stolen_top);
  402. return 0;
  403. }
  404. /* It is possible for the reserved area to end before the end of stolen
  405. * memory, so just consider the start. */
  406. reserved_total = stolen_top - reserved_base;
  407. DRM_DEBUG_KMS("Memory reserved for graphics device: %zuK, usable: %luK\n",
  408. dev_priv->gtt.stolen_size >> 10,
  409. (dev_priv->gtt.stolen_size - reserved_total) >> 10);
  410. dev_priv->gtt.stolen_usable_size = dev_priv->gtt.stolen_size -
  411. reserved_total;
  412. /*
  413. * Basic memrange allocator for stolen space.
  414. *
  415. * TODO: Notice that some platforms require us to not use the first page
  416. * of the stolen memory but their BIOSes may still put the framebuffer
  417. * on the first page. So we don't reserve this page for now because of
  418. * that. Our current solution is to just prevent new nodes from being
  419. * inserted on the first page - see the check we have at
  420. * i915_gem_stolen_insert_node_in_range(). We may want to fix the fbcon
  421. * problem later.
  422. */
  423. drm_mm_init(&dev_priv->mm.stolen, 0, dev_priv->gtt.stolen_usable_size);
  424. return 0;
  425. }
  426. static struct sg_table *
  427. i915_pages_create_for_stolen(struct drm_device *dev,
  428. u32 offset, u32 size)
  429. {
  430. struct drm_i915_private *dev_priv = dev->dev_private;
  431. struct sg_table *st;
  432. struct scatterlist *sg;
  433. DRM_DEBUG_DRIVER("offset=0x%x, size=%d\n", offset, size);
  434. BUG_ON(offset > dev_priv->gtt.stolen_size - size);
  435. /* We hide that we have no struct page backing our stolen object
  436. * by wrapping the contiguous physical allocation with a fake
  437. * dma mapping in a single scatterlist.
  438. */
  439. st = kmalloc(sizeof(*st), GFP_KERNEL);
  440. if (st == NULL)
  441. return NULL;
  442. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  443. kfree(st);
  444. return NULL;
  445. }
  446. sg = st->sgl;
  447. sg->offset = 0;
  448. sg->length = size;
  449. sg_dma_address(sg) = (dma_addr_t)dev_priv->mm.stolen_base + offset;
  450. sg_dma_len(sg) = size;
  451. return st;
  452. }
  453. static int i915_gem_object_get_pages_stolen(struct drm_i915_gem_object *obj)
  454. {
  455. BUG();
  456. return -EINVAL;
  457. }
  458. static void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object *obj)
  459. {
  460. /* Should only be called during free */
  461. sg_free_table(obj->pages);
  462. kfree(obj->pages);
  463. }
  464. static void
  465. i915_gem_object_release_stolen(struct drm_i915_gem_object *obj)
  466. {
  467. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  468. if (obj->stolen) {
  469. i915_gem_stolen_remove_node(dev_priv, obj->stolen);
  470. kfree(obj->stolen);
  471. obj->stolen = NULL;
  472. }
  473. }
  474. static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = {
  475. .get_pages = i915_gem_object_get_pages_stolen,
  476. .put_pages = i915_gem_object_put_pages_stolen,
  477. .release = i915_gem_object_release_stolen,
  478. };
  479. static struct drm_i915_gem_object *
  480. _i915_gem_object_create_stolen(struct drm_device *dev,
  481. struct drm_mm_node *stolen)
  482. {
  483. struct drm_i915_gem_object *obj;
  484. obj = i915_gem_object_alloc(dev);
  485. if (obj == NULL)
  486. return NULL;
  487. drm_gem_private_object_init(dev, &obj->base, stolen->size);
  488. i915_gem_object_init(obj, &i915_gem_object_stolen_ops);
  489. obj->pages = i915_pages_create_for_stolen(dev,
  490. stolen->start, stolen->size);
  491. if (obj->pages == NULL)
  492. goto cleanup;
  493. i915_gem_object_pin_pages(obj);
  494. obj->stolen = stolen;
  495. obj->base.read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT;
  496. obj->cache_level = HAS_LLC(dev) ? I915_CACHE_LLC : I915_CACHE_NONE;
  497. return obj;
  498. cleanup:
  499. i915_gem_object_free(obj);
  500. return NULL;
  501. }
  502. struct drm_i915_gem_object *
  503. i915_gem_object_create_stolen(struct drm_device *dev, u32 size)
  504. {
  505. struct drm_i915_private *dev_priv = dev->dev_private;
  506. struct drm_i915_gem_object *obj;
  507. struct drm_mm_node *stolen;
  508. int ret;
  509. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  510. return NULL;
  511. DRM_DEBUG_KMS("creating stolen object: size=%x\n", size);
  512. if (size == 0)
  513. return NULL;
  514. stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
  515. if (!stolen)
  516. return NULL;
  517. ret = i915_gem_stolen_insert_node(dev_priv, stolen, size, 4096);
  518. if (ret) {
  519. kfree(stolen);
  520. return NULL;
  521. }
  522. obj = _i915_gem_object_create_stolen(dev, stolen);
  523. if (obj)
  524. return obj;
  525. i915_gem_stolen_remove_node(dev_priv, stolen);
  526. kfree(stolen);
  527. return NULL;
  528. }
  529. struct drm_i915_gem_object *
  530. i915_gem_object_create_stolen_for_preallocated(struct drm_device *dev,
  531. u32 stolen_offset,
  532. u32 gtt_offset,
  533. u32 size)
  534. {
  535. struct drm_i915_private *dev_priv = dev->dev_private;
  536. struct i915_address_space *ggtt = &dev_priv->gtt.base;
  537. struct drm_i915_gem_object *obj;
  538. struct drm_mm_node *stolen;
  539. struct i915_vma *vma;
  540. int ret;
  541. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  542. return NULL;
  543. DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%x, gtt_offset=%x, size=%x\n",
  544. stolen_offset, gtt_offset, size);
  545. /* KISS and expect everything to be page-aligned */
  546. if (WARN_ON(size == 0) || WARN_ON(size & 4095) ||
  547. WARN_ON(stolen_offset & 4095))
  548. return NULL;
  549. stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
  550. if (!stolen)
  551. return NULL;
  552. stolen->start = stolen_offset;
  553. stolen->size = size;
  554. mutex_lock(&dev_priv->mm.stolen_lock);
  555. ret = drm_mm_reserve_node(&dev_priv->mm.stolen, stolen);
  556. mutex_unlock(&dev_priv->mm.stolen_lock);
  557. if (ret) {
  558. DRM_DEBUG_KMS("failed to allocate stolen space\n");
  559. kfree(stolen);
  560. return NULL;
  561. }
  562. obj = _i915_gem_object_create_stolen(dev, stolen);
  563. if (obj == NULL) {
  564. DRM_DEBUG_KMS("failed to allocate stolen object\n");
  565. i915_gem_stolen_remove_node(dev_priv, stolen);
  566. kfree(stolen);
  567. return NULL;
  568. }
  569. /* Some objects just need physical mem from stolen space */
  570. if (gtt_offset == I915_GTT_OFFSET_NONE)
  571. return obj;
  572. vma = i915_gem_obj_lookup_or_create_vma(obj, ggtt);
  573. if (IS_ERR(vma)) {
  574. ret = PTR_ERR(vma);
  575. goto err;
  576. }
  577. /* To simplify the initialisation sequence between KMS and GTT,
  578. * we allow construction of the stolen object prior to
  579. * setting up the GTT space. The actual reservation will occur
  580. * later.
  581. */
  582. vma->node.start = gtt_offset;
  583. vma->node.size = size;
  584. if (drm_mm_initialized(&ggtt->mm)) {
  585. ret = drm_mm_reserve_node(&ggtt->mm, &vma->node);
  586. if (ret) {
  587. DRM_DEBUG_KMS("failed to allocate stolen GTT space\n");
  588. goto err;
  589. }
  590. vma->bound |= GLOBAL_BIND;
  591. __i915_vma_set_map_and_fenceable(vma);
  592. list_add_tail(&vma->mm_list, &ggtt->inactive_list);
  593. }
  594. list_add_tail(&obj->global_list, &dev_priv->mm.bound_list);
  595. i915_gem_object_pin_pages(obj);
  596. return obj;
  597. err:
  598. drm_gem_object_unreference(&obj->base);
  599. return NULL;
  600. }