i915_gem_stolen.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. /*
  32. * The BIOS typically reserves some of the system's memory for the exclusive
  33. * use of the integrated graphics. This memory is no longer available for
  34. * use by the OS and so the user finds that his system has less memory
  35. * available than he put in. We refer to this memory as stolen.
  36. *
  37. * The BIOS will allocate its framebuffer from the stolen memory. Our
  38. * goal is try to reuse that object for our own fbcon which must always
  39. * be available for panics. Anything else we can reuse the stolen memory
  40. * for is a boon.
  41. */
  42. int i915_gem_stolen_insert_node(struct drm_i915_private *dev_priv,
  43. struct drm_mm_node *node, u64 size,
  44. unsigned alignment)
  45. {
  46. int ret;
  47. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  48. return -ENODEV;
  49. mutex_lock(&dev_priv->mm.stolen_lock);
  50. ret = drm_mm_insert_node(&dev_priv->mm.stolen, node, size, alignment,
  51. DRM_MM_SEARCH_DEFAULT);
  52. mutex_unlock(&dev_priv->mm.stolen_lock);
  53. return ret;
  54. }
  55. void i915_gem_stolen_remove_node(struct drm_i915_private *dev_priv,
  56. struct drm_mm_node *node)
  57. {
  58. mutex_lock(&dev_priv->mm.stolen_lock);
  59. drm_mm_remove_node(node);
  60. mutex_unlock(&dev_priv->mm.stolen_lock);
  61. }
  62. static unsigned long i915_stolen_to_physical(struct drm_device *dev)
  63. {
  64. struct drm_i915_private *dev_priv = dev->dev_private;
  65. struct resource *r;
  66. u32 base;
  67. /* Almost universally we can find the Graphics Base of Stolen Memory
  68. * at offset 0x5c in the igfx configuration space. On a few (desktop)
  69. * machines this is also mirrored in the bridge device at different
  70. * locations, or in the MCHBAR. On gen2, the layout is again slightly
  71. * different with the Graphics Segment immediately following Top of
  72. * Memory (or Top of Usable DRAM). Note it appears that TOUD is only
  73. * reported by 865g, so we just use the top of memory as determined
  74. * by the e820 probe.
  75. *
  76. * XXX However gen2 requires an unavailable symbol.
  77. */
  78. base = 0;
  79. if (INTEL_INFO(dev)->gen >= 3) {
  80. /* Read Graphics Base of Stolen Memory directly */
  81. pci_read_config_dword(dev->pdev, 0x5c, &base);
  82. base &= ~((1<<20) - 1);
  83. } else { /* GEN2 */
  84. #if 0
  85. /* Stolen is immediately above Top of Memory */
  86. base = max_low_pfn_mapped << PAGE_SHIFT;
  87. #endif
  88. }
  89. if (base == 0)
  90. return 0;
  91. /* make sure we don't clobber the GTT if it's within stolen memory */
  92. if (INTEL_INFO(dev)->gen <= 4 && !IS_G33(dev) && !IS_G4X(dev)) {
  93. struct {
  94. u32 start, end;
  95. } stolen[2] = {
  96. { .start = base, .end = base + dev_priv->gtt.stolen_size, },
  97. { .start = base, .end = base + dev_priv->gtt.stolen_size, },
  98. };
  99. u64 gtt_start, gtt_end;
  100. gtt_start = I915_READ(PGTBL_CTL);
  101. if (IS_GEN4(dev))
  102. gtt_start = (gtt_start & PGTBL_ADDRESS_LO_MASK) |
  103. (gtt_start & PGTBL_ADDRESS_HI_MASK) << 28;
  104. else
  105. gtt_start &= PGTBL_ADDRESS_LO_MASK;
  106. gtt_end = gtt_start + gtt_total_entries(dev_priv->gtt) * 4;
  107. if (gtt_start >= stolen[0].start && gtt_start < stolen[0].end)
  108. stolen[0].end = gtt_start;
  109. if (gtt_end > stolen[1].start && gtt_end <= stolen[1].end)
  110. stolen[1].start = gtt_end;
  111. /* pick the larger of the two chunks */
  112. if (stolen[0].end - stolen[0].start >
  113. stolen[1].end - stolen[1].start) {
  114. base = stolen[0].start;
  115. dev_priv->gtt.stolen_size = stolen[0].end - stolen[0].start;
  116. } else {
  117. base = stolen[1].start;
  118. dev_priv->gtt.stolen_size = stolen[1].end - stolen[1].start;
  119. }
  120. if (stolen[0].start != stolen[1].start ||
  121. stolen[0].end != stolen[1].end) {
  122. DRM_DEBUG_KMS("GTT within stolen memory at 0x%llx-0x%llx\n",
  123. (unsigned long long) gtt_start,
  124. (unsigned long long) gtt_end - 1);
  125. DRM_DEBUG_KMS("Stolen memory adjusted to 0x%x-0x%x\n",
  126. base, base + (u32) dev_priv->gtt.stolen_size - 1);
  127. }
  128. }
  129. /* Verify that nothing else uses this physical address. Stolen
  130. * memory should be reserved by the BIOS and hidden from the
  131. * kernel. So if the region is already marked as busy, something
  132. * is seriously wrong.
  133. */
  134. r = devm_request_mem_region(dev->dev, base, dev_priv->gtt.stolen_size,
  135. "Graphics Stolen Memory");
  136. if (r == NULL) {
  137. /*
  138. * One more attempt but this time requesting region from
  139. * base + 1, as we have seen that this resolves the region
  140. * conflict with the PCI Bus.
  141. * This is a BIOS w/a: Some BIOS wrap stolen in the root
  142. * PCI bus, but have an off-by-one error. Hence retry the
  143. * reservation starting from 1 instead of 0.
  144. */
  145. r = devm_request_mem_region(dev->dev, base + 1,
  146. dev_priv->gtt.stolen_size - 1,
  147. "Graphics Stolen Memory");
  148. /*
  149. * GEN3 firmware likes to smash pci bridges into the stolen
  150. * range. Apparently this works.
  151. */
  152. if (r == NULL && !IS_GEN3(dev)) {
  153. DRM_ERROR("conflict detected with stolen region: [0x%08x - 0x%08x]\n",
  154. base, base + (uint32_t)dev_priv->gtt.stolen_size);
  155. base = 0;
  156. }
  157. }
  158. return base;
  159. }
  160. void i915_gem_cleanup_stolen(struct drm_device *dev)
  161. {
  162. struct drm_i915_private *dev_priv = dev->dev_private;
  163. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  164. return;
  165. drm_mm_takedown(&dev_priv->mm.stolen);
  166. }
  167. static void gen6_get_stolen_reserved(struct drm_i915_private *dev_priv,
  168. unsigned long *base, unsigned long *size)
  169. {
  170. uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
  171. *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK;
  172. switch (reg_val & GEN6_STOLEN_RESERVED_SIZE_MASK) {
  173. case GEN6_STOLEN_RESERVED_1M:
  174. *size = 1024 * 1024;
  175. break;
  176. case GEN6_STOLEN_RESERVED_512K:
  177. *size = 512 * 1024;
  178. break;
  179. case GEN6_STOLEN_RESERVED_256K:
  180. *size = 256 * 1024;
  181. break;
  182. case GEN6_STOLEN_RESERVED_128K:
  183. *size = 128 * 1024;
  184. break;
  185. default:
  186. *size = 1024 * 1024;
  187. MISSING_CASE(reg_val & GEN6_STOLEN_RESERVED_SIZE_MASK);
  188. }
  189. }
  190. static void gen7_get_stolen_reserved(struct drm_i915_private *dev_priv,
  191. unsigned long *base, unsigned long *size)
  192. {
  193. uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
  194. *base = reg_val & GEN7_STOLEN_RESERVED_ADDR_MASK;
  195. switch (reg_val & GEN7_STOLEN_RESERVED_SIZE_MASK) {
  196. case GEN7_STOLEN_RESERVED_1M:
  197. *size = 1024 * 1024;
  198. break;
  199. case GEN7_STOLEN_RESERVED_256K:
  200. *size = 256 * 1024;
  201. break;
  202. default:
  203. *size = 1024 * 1024;
  204. MISSING_CASE(reg_val & GEN7_STOLEN_RESERVED_SIZE_MASK);
  205. }
  206. }
  207. static void gen8_get_stolen_reserved(struct drm_i915_private *dev_priv,
  208. unsigned long *base, unsigned long *size)
  209. {
  210. uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
  211. *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK;
  212. switch (reg_val & GEN8_STOLEN_RESERVED_SIZE_MASK) {
  213. case GEN8_STOLEN_RESERVED_1M:
  214. *size = 1024 * 1024;
  215. break;
  216. case GEN8_STOLEN_RESERVED_2M:
  217. *size = 2 * 1024 * 1024;
  218. break;
  219. case GEN8_STOLEN_RESERVED_4M:
  220. *size = 4 * 1024 * 1024;
  221. break;
  222. case GEN8_STOLEN_RESERVED_8M:
  223. *size = 8 * 1024 * 1024;
  224. break;
  225. default:
  226. *size = 8 * 1024 * 1024;
  227. MISSING_CASE(reg_val & GEN8_STOLEN_RESERVED_SIZE_MASK);
  228. }
  229. }
  230. static void bdw_get_stolen_reserved(struct drm_i915_private *dev_priv,
  231. unsigned long *base, unsigned long *size)
  232. {
  233. uint32_t reg_val = I915_READ(GEN6_STOLEN_RESERVED);
  234. unsigned long stolen_top;
  235. stolen_top = dev_priv->mm.stolen_base + dev_priv->gtt.stolen_size;
  236. *base = reg_val & GEN6_STOLEN_RESERVED_ADDR_MASK;
  237. /* On these platforms, the register doesn't have a size field, so the
  238. * size is the distance between the base and the top of the stolen
  239. * memory. We also have the genuine case where base is zero and there's
  240. * nothing reserved. */
  241. if (*base == 0)
  242. *size = 0;
  243. else
  244. *size = stolen_top - *base;
  245. }
  246. int i915_gem_init_stolen(struct drm_device *dev)
  247. {
  248. struct drm_i915_private *dev_priv = dev->dev_private;
  249. unsigned long reserved_total, reserved_base, reserved_size;
  250. unsigned long stolen_top;
  251. mutex_init(&dev_priv->mm.stolen_lock);
  252. #ifdef CONFIG_INTEL_IOMMU
  253. if (intel_iommu_gfx_mapped && INTEL_INFO(dev)->gen < 8) {
  254. DRM_INFO("DMAR active, disabling use of stolen memory\n");
  255. return 0;
  256. }
  257. #endif
  258. if (dev_priv->gtt.stolen_size == 0)
  259. return 0;
  260. dev_priv->mm.stolen_base = i915_stolen_to_physical(dev);
  261. if (dev_priv->mm.stolen_base == 0)
  262. return 0;
  263. stolen_top = dev_priv->mm.stolen_base + dev_priv->gtt.stolen_size;
  264. switch (INTEL_INFO(dev_priv)->gen) {
  265. case 2:
  266. case 3:
  267. case 4:
  268. case 5:
  269. /* Assume the gen6 maximum for the older platforms. */
  270. reserved_size = 1024 * 1024;
  271. reserved_base = stolen_top - reserved_size;
  272. break;
  273. case 6:
  274. gen6_get_stolen_reserved(dev_priv, &reserved_base,
  275. &reserved_size);
  276. break;
  277. case 7:
  278. gen7_get_stolen_reserved(dev_priv, &reserved_base,
  279. &reserved_size);
  280. break;
  281. default:
  282. if (IS_BROADWELL(dev_priv) || IS_SKYLAKE(dev_priv))
  283. bdw_get_stolen_reserved(dev_priv, &reserved_base,
  284. &reserved_size);
  285. else
  286. gen8_get_stolen_reserved(dev_priv, &reserved_base,
  287. &reserved_size);
  288. break;
  289. }
  290. /* It is possible for the reserved base to be zero, but the register
  291. * field for size doesn't have a zero option. */
  292. if (reserved_base == 0) {
  293. reserved_size = 0;
  294. reserved_base = stolen_top;
  295. }
  296. if (reserved_base < dev_priv->mm.stolen_base ||
  297. reserved_base + reserved_size > stolen_top) {
  298. DRM_DEBUG_KMS("Stolen reserved area [0x%08lx - 0x%08lx] outside stolen memory [0x%08lx - 0x%08lx]\n",
  299. reserved_base, reserved_base + reserved_size,
  300. dev_priv->mm.stolen_base, stolen_top);
  301. return 0;
  302. }
  303. /* It is possible for the reserved area to end before the end of stolen
  304. * memory, so just consider the start. */
  305. reserved_total = stolen_top - reserved_base;
  306. DRM_DEBUG_KMS("Memory reserved for graphics device: %zuK, usable: %luK\n",
  307. dev_priv->gtt.stolen_size >> 10,
  308. (dev_priv->gtt.stolen_size - reserved_total) >> 10);
  309. /* Basic memrange allocator for stolen space */
  310. drm_mm_init(&dev_priv->mm.stolen, 0, dev_priv->gtt.stolen_size -
  311. reserved_total);
  312. return 0;
  313. }
  314. static struct sg_table *
  315. i915_pages_create_for_stolen(struct drm_device *dev,
  316. u32 offset, u32 size)
  317. {
  318. struct drm_i915_private *dev_priv = dev->dev_private;
  319. struct sg_table *st;
  320. struct scatterlist *sg;
  321. DRM_DEBUG_DRIVER("offset=0x%x, size=%d\n", offset, size);
  322. BUG_ON(offset > dev_priv->gtt.stolen_size - size);
  323. /* We hide that we have no struct page backing our stolen object
  324. * by wrapping the contiguous physical allocation with a fake
  325. * dma mapping in a single scatterlist.
  326. */
  327. st = kmalloc(sizeof(*st), GFP_KERNEL);
  328. if (st == NULL)
  329. return NULL;
  330. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  331. kfree(st);
  332. return NULL;
  333. }
  334. sg = st->sgl;
  335. sg->offset = 0;
  336. sg->length = size;
  337. sg_dma_address(sg) = (dma_addr_t)dev_priv->mm.stolen_base + offset;
  338. sg_dma_len(sg) = size;
  339. return st;
  340. }
  341. static int i915_gem_object_get_pages_stolen(struct drm_i915_gem_object *obj)
  342. {
  343. BUG();
  344. return -EINVAL;
  345. }
  346. static void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object *obj)
  347. {
  348. /* Should only be called during free */
  349. sg_free_table(obj->pages);
  350. kfree(obj->pages);
  351. }
  352. static void
  353. i915_gem_object_release_stolen(struct drm_i915_gem_object *obj)
  354. {
  355. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  356. if (obj->stolen) {
  357. i915_gem_stolen_remove_node(dev_priv, obj->stolen);
  358. kfree(obj->stolen);
  359. obj->stolen = NULL;
  360. }
  361. }
  362. static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = {
  363. .get_pages = i915_gem_object_get_pages_stolen,
  364. .put_pages = i915_gem_object_put_pages_stolen,
  365. .release = i915_gem_object_release_stolen,
  366. };
  367. static struct drm_i915_gem_object *
  368. _i915_gem_object_create_stolen(struct drm_device *dev,
  369. struct drm_mm_node *stolen)
  370. {
  371. struct drm_i915_gem_object *obj;
  372. obj = i915_gem_object_alloc(dev);
  373. if (obj == NULL)
  374. return NULL;
  375. drm_gem_private_object_init(dev, &obj->base, stolen->size);
  376. i915_gem_object_init(obj, &i915_gem_object_stolen_ops);
  377. obj->pages = i915_pages_create_for_stolen(dev,
  378. stolen->start, stolen->size);
  379. if (obj->pages == NULL)
  380. goto cleanup;
  381. i915_gem_object_pin_pages(obj);
  382. obj->stolen = stolen;
  383. obj->base.read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT;
  384. obj->cache_level = HAS_LLC(dev) ? I915_CACHE_LLC : I915_CACHE_NONE;
  385. return obj;
  386. cleanup:
  387. i915_gem_object_free(obj);
  388. return NULL;
  389. }
  390. struct drm_i915_gem_object *
  391. i915_gem_object_create_stolen(struct drm_device *dev, u32 size)
  392. {
  393. struct drm_i915_private *dev_priv = dev->dev_private;
  394. struct drm_i915_gem_object *obj;
  395. struct drm_mm_node *stolen;
  396. int ret;
  397. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  398. return NULL;
  399. DRM_DEBUG_KMS("creating stolen object: size=%x\n", size);
  400. if (size == 0)
  401. return NULL;
  402. stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
  403. if (!stolen)
  404. return NULL;
  405. ret = i915_gem_stolen_insert_node(dev_priv, stolen, size, 4096);
  406. if (ret) {
  407. kfree(stolen);
  408. return NULL;
  409. }
  410. obj = _i915_gem_object_create_stolen(dev, stolen);
  411. if (obj)
  412. return obj;
  413. i915_gem_stolen_remove_node(dev_priv, stolen);
  414. kfree(stolen);
  415. return NULL;
  416. }
  417. struct drm_i915_gem_object *
  418. i915_gem_object_create_stolen_for_preallocated(struct drm_device *dev,
  419. u32 stolen_offset,
  420. u32 gtt_offset,
  421. u32 size)
  422. {
  423. struct drm_i915_private *dev_priv = dev->dev_private;
  424. struct i915_address_space *ggtt = &dev_priv->gtt.base;
  425. struct drm_i915_gem_object *obj;
  426. struct drm_mm_node *stolen;
  427. struct i915_vma *vma;
  428. int ret;
  429. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  430. return NULL;
  431. DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%x, gtt_offset=%x, size=%x\n",
  432. stolen_offset, gtt_offset, size);
  433. /* KISS and expect everything to be page-aligned */
  434. if (WARN_ON(size == 0) || WARN_ON(size & 4095) ||
  435. WARN_ON(stolen_offset & 4095))
  436. return NULL;
  437. stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
  438. if (!stolen)
  439. return NULL;
  440. stolen->start = stolen_offset;
  441. stolen->size = size;
  442. mutex_lock(&dev_priv->mm.stolen_lock);
  443. ret = drm_mm_reserve_node(&dev_priv->mm.stolen, stolen);
  444. mutex_unlock(&dev_priv->mm.stolen_lock);
  445. if (ret) {
  446. DRM_DEBUG_KMS("failed to allocate stolen space\n");
  447. kfree(stolen);
  448. return NULL;
  449. }
  450. obj = _i915_gem_object_create_stolen(dev, stolen);
  451. if (obj == NULL) {
  452. DRM_DEBUG_KMS("failed to allocate stolen object\n");
  453. i915_gem_stolen_remove_node(dev_priv, stolen);
  454. kfree(stolen);
  455. return NULL;
  456. }
  457. /* Some objects just need physical mem from stolen space */
  458. if (gtt_offset == I915_GTT_OFFSET_NONE)
  459. return obj;
  460. vma = i915_gem_obj_lookup_or_create_vma(obj, ggtt);
  461. if (IS_ERR(vma)) {
  462. ret = PTR_ERR(vma);
  463. goto err_out;
  464. }
  465. /* To simplify the initialisation sequence between KMS and GTT,
  466. * we allow construction of the stolen object prior to
  467. * setting up the GTT space. The actual reservation will occur
  468. * later.
  469. */
  470. vma->node.start = gtt_offset;
  471. vma->node.size = size;
  472. if (drm_mm_initialized(&ggtt->mm)) {
  473. ret = drm_mm_reserve_node(&ggtt->mm, &vma->node);
  474. if (ret) {
  475. DRM_DEBUG_KMS("failed to allocate stolen GTT space\n");
  476. goto err_vma;
  477. }
  478. }
  479. vma->bound |= GLOBAL_BIND;
  480. list_add_tail(&obj->global_list, &dev_priv->mm.bound_list);
  481. list_add_tail(&vma->mm_list, &ggtt->inactive_list);
  482. i915_gem_object_pin_pages(obj);
  483. return obj;
  484. err_vma:
  485. i915_gem_vma_destroy(vma);
  486. err_out:
  487. i915_gem_stolen_remove_node(dev_priv, stolen);
  488. kfree(stolen);
  489. drm_gem_object_unreference(&obj->base);
  490. return NULL;
  491. }