i915_gem_stolen.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. static unsigned long i915_stolen_to_physical(struct drm_device *dev)
  43. {
  44. struct drm_i915_private *dev_priv = dev->dev_private;
  45. struct resource *r;
  46. u32 base;
  47. /* Almost universally we can find the Graphics Base of Stolen Memory
  48. * at offset 0x5c in the igfx configuration space. On a few (desktop)
  49. * machines this is also mirrored in the bridge device at different
  50. * locations, or in the MCHBAR. On gen2, the layout is again slightly
  51. * different with the Graphics Segment immediately following Top of
  52. * Memory (or Top of Usable DRAM). Note it appears that TOUD is only
  53. * reported by 865g, so we just use the top of memory as determined
  54. * by the e820 probe.
  55. *
  56. * XXX However gen2 requires an unavailable symbol.
  57. */
  58. base = 0;
  59. if (INTEL_INFO(dev)->gen >= 3) {
  60. /* Read Graphics Base of Stolen Memory directly */
  61. pci_read_config_dword(dev->pdev, 0x5c, &base);
  62. base &= ~((1<<20) - 1);
  63. } else { /* GEN2 */
  64. #if 0
  65. /* Stolen is immediately above Top of Memory */
  66. base = max_low_pfn_mapped << PAGE_SHIFT;
  67. #endif
  68. }
  69. if (base == 0)
  70. return 0;
  71. /* make sure we don't clobber the GTT if it's within stolen memory */
  72. if (INTEL_INFO(dev)->gen <= 4 && !IS_G33(dev) && !IS_G4X(dev)) {
  73. struct {
  74. u32 start, end;
  75. } stolen[2] = {
  76. { .start = base, .end = base + dev_priv->gtt.stolen_size, },
  77. { .start = base, .end = base + dev_priv->gtt.stolen_size, },
  78. };
  79. u64 gtt_start, gtt_end;
  80. gtt_start = I915_READ(PGTBL_CTL);
  81. if (IS_GEN4(dev))
  82. gtt_start = (gtt_start & PGTBL_ADDRESS_LO_MASK) |
  83. (gtt_start & PGTBL_ADDRESS_HI_MASK) << 28;
  84. else
  85. gtt_start &= PGTBL_ADDRESS_LO_MASK;
  86. gtt_end = gtt_start + gtt_total_entries(dev_priv->gtt) * 4;
  87. if (gtt_start >= stolen[0].start && gtt_start < stolen[0].end)
  88. stolen[0].end = gtt_start;
  89. if (gtt_end > stolen[1].start && gtt_end <= stolen[1].end)
  90. stolen[1].start = gtt_end;
  91. /* pick the larger of the two chunks */
  92. if (stolen[0].end - stolen[0].start >
  93. stolen[1].end - stolen[1].start) {
  94. base = stolen[0].start;
  95. dev_priv->gtt.stolen_size = stolen[0].end - stolen[0].start;
  96. } else {
  97. base = stolen[1].start;
  98. dev_priv->gtt.stolen_size = stolen[1].end - stolen[1].start;
  99. }
  100. if (stolen[0].start != stolen[1].start ||
  101. stolen[0].end != stolen[1].end) {
  102. DRM_DEBUG_KMS("GTT within stolen memory at 0x%llx-0x%llx\n",
  103. (unsigned long long) gtt_start,
  104. (unsigned long long) gtt_end - 1);
  105. DRM_DEBUG_KMS("Stolen memory adjusted to 0x%x-0x%x\n",
  106. base, base + (u32) dev_priv->gtt.stolen_size - 1);
  107. }
  108. }
  109. /* Verify that nothing else uses this physical address. Stolen
  110. * memory should be reserved by the BIOS and hidden from the
  111. * kernel. So if the region is already marked as busy, something
  112. * is seriously wrong.
  113. */
  114. r = devm_request_mem_region(dev->dev, base, dev_priv->gtt.stolen_size,
  115. "Graphics Stolen Memory");
  116. if (r == NULL) {
  117. /*
  118. * One more attempt but this time requesting region from
  119. * base + 1, as we have seen that this resolves the region
  120. * conflict with the PCI Bus.
  121. * This is a BIOS w/a: Some BIOS wrap stolen in the root
  122. * PCI bus, but have an off-by-one error. Hence retry the
  123. * reservation starting from 1 instead of 0.
  124. */
  125. r = devm_request_mem_region(dev->dev, base + 1,
  126. dev_priv->gtt.stolen_size - 1,
  127. "Graphics Stolen Memory");
  128. if (r == NULL) {
  129. DRM_ERROR("conflict detected with stolen region: [0x%08x - 0x%08x]\n",
  130. base, base + (uint32_t)dev_priv->gtt.stolen_size);
  131. base = 0;
  132. }
  133. }
  134. return base;
  135. }
  136. static int i915_setup_compression(struct drm_device *dev, int size)
  137. {
  138. struct drm_i915_private *dev_priv = dev->dev_private;
  139. struct drm_mm_node *compressed_fb, *uninitialized_var(compressed_llb);
  140. int ret;
  141. compressed_fb = kzalloc(sizeof(*compressed_fb), GFP_KERNEL);
  142. if (!compressed_fb)
  143. goto err_llb;
  144. /* Try to over-allocate to reduce reallocations and fragmentation */
  145. ret = drm_mm_insert_node(&dev_priv->mm.stolen, compressed_fb,
  146. size <<= 1, 4096, DRM_MM_SEARCH_DEFAULT);
  147. if (ret)
  148. ret = drm_mm_insert_node(&dev_priv->mm.stolen, compressed_fb,
  149. size >>= 1, 4096,
  150. DRM_MM_SEARCH_DEFAULT);
  151. if (ret)
  152. goto err_llb;
  153. if (HAS_PCH_SPLIT(dev))
  154. I915_WRITE(ILK_DPFC_CB_BASE, compressed_fb->start);
  155. else if (IS_GM45(dev)) {
  156. I915_WRITE(DPFC_CB_BASE, compressed_fb->start);
  157. } else {
  158. compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
  159. if (!compressed_llb)
  160. goto err_fb;
  161. ret = drm_mm_insert_node(&dev_priv->mm.stolen, compressed_llb,
  162. 4096, 4096, DRM_MM_SEARCH_DEFAULT);
  163. if (ret)
  164. goto err_fb;
  165. dev_priv->fbc.compressed_llb = compressed_llb;
  166. I915_WRITE(FBC_CFB_BASE,
  167. dev_priv->mm.stolen_base + compressed_fb->start);
  168. I915_WRITE(FBC_LL_BASE,
  169. dev_priv->mm.stolen_base + compressed_llb->start);
  170. }
  171. dev_priv->fbc.compressed_fb = compressed_fb;
  172. dev_priv->fbc.size = size;
  173. DRM_DEBUG_KMS("reserved %d bytes of contiguous stolen space for FBC\n",
  174. size);
  175. return 0;
  176. err_fb:
  177. kfree(compressed_llb);
  178. drm_mm_remove_node(compressed_fb);
  179. err_llb:
  180. kfree(compressed_fb);
  181. pr_info_once("drm: not enough stolen space for compressed buffer (need %d more bytes), disabling. Hint: you may be able to increase stolen memory size in the BIOS to avoid this.\n", size);
  182. return -ENOSPC;
  183. }
  184. int i915_gem_stolen_setup_compression(struct drm_device *dev, int size)
  185. {
  186. struct drm_i915_private *dev_priv = dev->dev_private;
  187. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  188. return -ENODEV;
  189. if (size < dev_priv->fbc.size)
  190. return 0;
  191. /* Release any current block */
  192. i915_gem_stolen_cleanup_compression(dev);
  193. return i915_setup_compression(dev, size);
  194. }
  195. void i915_gem_stolen_cleanup_compression(struct drm_device *dev)
  196. {
  197. struct drm_i915_private *dev_priv = dev->dev_private;
  198. if (dev_priv->fbc.size == 0)
  199. return;
  200. if (dev_priv->fbc.compressed_fb) {
  201. drm_mm_remove_node(dev_priv->fbc.compressed_fb);
  202. kfree(dev_priv->fbc.compressed_fb);
  203. }
  204. if (dev_priv->fbc.compressed_llb) {
  205. drm_mm_remove_node(dev_priv->fbc.compressed_llb);
  206. kfree(dev_priv->fbc.compressed_llb);
  207. }
  208. dev_priv->fbc.size = 0;
  209. }
  210. void i915_gem_cleanup_stolen(struct drm_device *dev)
  211. {
  212. struct drm_i915_private *dev_priv = dev->dev_private;
  213. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  214. return;
  215. i915_gem_stolen_cleanup_compression(dev);
  216. drm_mm_takedown(&dev_priv->mm.stolen);
  217. }
  218. int i915_gem_init_stolen(struct drm_device *dev)
  219. {
  220. struct drm_i915_private *dev_priv = dev->dev_private;
  221. int bios_reserved = 0;
  222. #ifdef CONFIG_INTEL_IOMMU
  223. if (intel_iommu_gfx_mapped && INTEL_INFO(dev)->gen < 8) {
  224. DRM_INFO("DMAR active, disabling use of stolen memory\n");
  225. return 0;
  226. }
  227. #endif
  228. if (dev_priv->gtt.stolen_size == 0)
  229. return 0;
  230. dev_priv->mm.stolen_base = i915_stolen_to_physical(dev);
  231. if (dev_priv->mm.stolen_base == 0)
  232. return 0;
  233. DRM_DEBUG_KMS("found %zd bytes of stolen memory at %08lx\n",
  234. dev_priv->gtt.stolen_size, dev_priv->mm.stolen_base);
  235. if (IS_VALLEYVIEW(dev))
  236. bios_reserved = 1024*1024; /* top 1M on VLV/BYT */
  237. if (WARN_ON(bios_reserved > dev_priv->gtt.stolen_size))
  238. return 0;
  239. /* Basic memrange allocator for stolen space */
  240. drm_mm_init(&dev_priv->mm.stolen, 0, dev_priv->gtt.stolen_size -
  241. bios_reserved);
  242. return 0;
  243. }
  244. static struct sg_table *
  245. i915_pages_create_for_stolen(struct drm_device *dev,
  246. u32 offset, u32 size)
  247. {
  248. struct drm_i915_private *dev_priv = dev->dev_private;
  249. struct sg_table *st;
  250. struct scatterlist *sg;
  251. DRM_DEBUG_DRIVER("offset=0x%x, size=%d\n", offset, size);
  252. BUG_ON(offset > dev_priv->gtt.stolen_size - size);
  253. /* We hide that we have no struct page backing our stolen object
  254. * by wrapping the contiguous physical allocation with a fake
  255. * dma mapping in a single scatterlist.
  256. */
  257. st = kmalloc(sizeof(*st), GFP_KERNEL);
  258. if (st == NULL)
  259. return NULL;
  260. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  261. kfree(st);
  262. return NULL;
  263. }
  264. sg = st->sgl;
  265. sg->offset = 0;
  266. sg->length = size;
  267. sg_dma_address(sg) = (dma_addr_t)dev_priv->mm.stolen_base + offset;
  268. sg_dma_len(sg) = size;
  269. return st;
  270. }
  271. static int i915_gem_object_get_pages_stolen(struct drm_i915_gem_object *obj)
  272. {
  273. BUG();
  274. return -EINVAL;
  275. }
  276. static void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object *obj)
  277. {
  278. /* Should only be called during free */
  279. sg_free_table(obj->pages);
  280. kfree(obj->pages);
  281. }
  282. static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = {
  283. .get_pages = i915_gem_object_get_pages_stolen,
  284. .put_pages = i915_gem_object_put_pages_stolen,
  285. };
  286. static struct drm_i915_gem_object *
  287. _i915_gem_object_create_stolen(struct drm_device *dev,
  288. struct drm_mm_node *stolen)
  289. {
  290. struct drm_i915_gem_object *obj;
  291. obj = i915_gem_object_alloc(dev);
  292. if (obj == NULL)
  293. return NULL;
  294. drm_gem_private_object_init(dev, &obj->base, stolen->size);
  295. i915_gem_object_init(obj, &i915_gem_object_stolen_ops);
  296. obj->pages = i915_pages_create_for_stolen(dev,
  297. stolen->start, stolen->size);
  298. if (obj->pages == NULL)
  299. goto cleanup;
  300. obj->has_dma_mapping = true;
  301. i915_gem_object_pin_pages(obj);
  302. obj->stolen = stolen;
  303. obj->base.read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT;
  304. obj->cache_level = HAS_LLC(dev) ? I915_CACHE_LLC : I915_CACHE_NONE;
  305. return obj;
  306. cleanup:
  307. i915_gem_object_free(obj);
  308. return NULL;
  309. }
  310. struct drm_i915_gem_object *
  311. i915_gem_object_create_stolen(struct drm_device *dev, u32 size)
  312. {
  313. struct drm_i915_private *dev_priv = dev->dev_private;
  314. struct drm_i915_gem_object *obj;
  315. struct drm_mm_node *stolen;
  316. int ret;
  317. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  318. return NULL;
  319. DRM_DEBUG_KMS("creating stolen object: size=%x\n", size);
  320. if (size == 0)
  321. return NULL;
  322. stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
  323. if (!stolen)
  324. return NULL;
  325. ret = drm_mm_insert_node(&dev_priv->mm.stolen, stolen, size,
  326. 4096, DRM_MM_SEARCH_DEFAULT);
  327. if (ret) {
  328. kfree(stolen);
  329. return NULL;
  330. }
  331. obj = _i915_gem_object_create_stolen(dev, stolen);
  332. if (obj)
  333. return obj;
  334. drm_mm_remove_node(stolen);
  335. kfree(stolen);
  336. return NULL;
  337. }
  338. struct drm_i915_gem_object *
  339. i915_gem_object_create_stolen_for_preallocated(struct drm_device *dev,
  340. u32 stolen_offset,
  341. u32 gtt_offset,
  342. u32 size)
  343. {
  344. struct drm_i915_private *dev_priv = dev->dev_private;
  345. struct i915_address_space *ggtt = &dev_priv->gtt.base;
  346. struct drm_i915_gem_object *obj;
  347. struct drm_mm_node *stolen;
  348. struct i915_vma *vma;
  349. int ret;
  350. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  351. return NULL;
  352. DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%x, gtt_offset=%x, size=%x\n",
  353. stolen_offset, gtt_offset, size);
  354. /* KISS and expect everything to be page-aligned */
  355. BUG_ON(stolen_offset & 4095);
  356. BUG_ON(size & 4095);
  357. if (WARN_ON(size == 0))
  358. return NULL;
  359. stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
  360. if (!stolen)
  361. return NULL;
  362. stolen->start = stolen_offset;
  363. stolen->size = size;
  364. ret = drm_mm_reserve_node(&dev_priv->mm.stolen, stolen);
  365. if (ret) {
  366. DRM_DEBUG_KMS("failed to allocate stolen space\n");
  367. kfree(stolen);
  368. return NULL;
  369. }
  370. obj = _i915_gem_object_create_stolen(dev, stolen);
  371. if (obj == NULL) {
  372. DRM_DEBUG_KMS("failed to allocate stolen object\n");
  373. drm_mm_remove_node(stolen);
  374. kfree(stolen);
  375. return NULL;
  376. }
  377. /* Some objects just need physical mem from stolen space */
  378. if (gtt_offset == I915_GTT_OFFSET_NONE)
  379. return obj;
  380. vma = i915_gem_obj_lookup_or_create_vma(obj, ggtt);
  381. if (IS_ERR(vma)) {
  382. ret = PTR_ERR(vma);
  383. goto err_out;
  384. }
  385. /* To simplify the initialisation sequence between KMS and GTT,
  386. * we allow construction of the stolen object prior to
  387. * setting up the GTT space. The actual reservation will occur
  388. * later.
  389. */
  390. vma->node.start = gtt_offset;
  391. vma->node.size = size;
  392. if (drm_mm_initialized(&ggtt->mm)) {
  393. ret = drm_mm_reserve_node(&ggtt->mm, &vma->node);
  394. if (ret) {
  395. DRM_DEBUG_KMS("failed to allocate stolen GTT space\n");
  396. goto err_vma;
  397. }
  398. }
  399. obj->has_global_gtt_mapping = 1;
  400. list_add_tail(&obj->global_list, &dev_priv->mm.bound_list);
  401. list_add_tail(&vma->mm_list, &ggtt->inactive_list);
  402. i915_gem_object_pin_pages(obj);
  403. return obj;
  404. err_vma:
  405. i915_gem_vma_destroy(vma);
  406. err_out:
  407. drm_mm_remove_node(stolen);
  408. kfree(stolen);
  409. drm_gem_object_unreference(&obj->base);
  410. return NULL;
  411. }
  412. void
  413. i915_gem_object_release_stolen(struct drm_i915_gem_object *obj)
  414. {
  415. if (obj->stolen) {
  416. drm_mm_remove_node(obj->stolen);
  417. kfree(obj->stolen);
  418. obj->stolen = NULL;
  419. }
  420. }