i915_gem_stolen.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 find_compression_threshold(struct drm_device *dev,
  137. struct drm_mm_node *node,
  138. int size,
  139. int fb_cpp)
  140. {
  141. struct drm_i915_private *dev_priv = dev->dev_private;
  142. int compression_threshold = 1;
  143. int ret;
  144. /* HACK: This code depends on what we will do in *_enable_fbc. If that
  145. * code changes, this code needs to change as well.
  146. *
  147. * The enable_fbc code will attempt to use one of our 2 compression
  148. * thresholds, therefore, in that case, we only have 1 resort.
  149. */
  150. /* Try to over-allocate to reduce reallocations and fragmentation. */
  151. ret = drm_mm_insert_node(&dev_priv->mm.stolen, node,
  152. size <<= 1, 4096, DRM_MM_SEARCH_DEFAULT);
  153. if (ret == 0)
  154. return compression_threshold;
  155. again:
  156. /* HW's ability to limit the CFB is 1:4 */
  157. if (compression_threshold > 4 ||
  158. (fb_cpp == 2 && compression_threshold == 2))
  159. return 0;
  160. ret = drm_mm_insert_node(&dev_priv->mm.stolen, node,
  161. size >>= 1, 4096,
  162. DRM_MM_SEARCH_DEFAULT);
  163. if (ret && INTEL_INFO(dev)->gen <= 4) {
  164. return 0;
  165. } else if (ret) {
  166. compression_threshold <<= 1;
  167. goto again;
  168. } else {
  169. return compression_threshold;
  170. }
  171. }
  172. static int i915_setup_compression(struct drm_device *dev, int size, int fb_cpp)
  173. {
  174. struct drm_i915_private *dev_priv = dev->dev_private;
  175. struct drm_mm_node *uninitialized_var(compressed_llb);
  176. int ret;
  177. ret = find_compression_threshold(dev, &dev_priv->fbc.compressed_fb,
  178. size, fb_cpp);
  179. if (!ret)
  180. goto err_llb;
  181. else if (ret > 1) {
  182. DRM_INFO("Reducing the compressed framebuffer size. This may lead to less power savings than a non-reduced-size. Try to increase stolen memory size if available in BIOS.\n");
  183. }
  184. dev_priv->fbc.threshold = ret;
  185. if (HAS_PCH_SPLIT(dev))
  186. I915_WRITE(ILK_DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
  187. else if (IS_GM45(dev)) {
  188. I915_WRITE(DPFC_CB_BASE, dev_priv->fbc.compressed_fb.start);
  189. } else {
  190. compressed_llb = kzalloc(sizeof(*compressed_llb), GFP_KERNEL);
  191. if (!compressed_llb)
  192. goto err_fb;
  193. ret = drm_mm_insert_node(&dev_priv->mm.stolen, compressed_llb,
  194. 4096, 4096, DRM_MM_SEARCH_DEFAULT);
  195. if (ret)
  196. goto err_fb;
  197. dev_priv->fbc.compressed_llb = compressed_llb;
  198. I915_WRITE(FBC_CFB_BASE,
  199. dev_priv->mm.stolen_base + dev_priv->fbc.compressed_fb.start);
  200. I915_WRITE(FBC_LL_BASE,
  201. dev_priv->mm.stolen_base + compressed_llb->start);
  202. }
  203. dev_priv->fbc.size = size / dev_priv->fbc.threshold;
  204. DRM_DEBUG_KMS("reserved %d bytes of contiguous stolen space for FBC\n",
  205. size);
  206. return 0;
  207. err_fb:
  208. kfree(compressed_llb);
  209. drm_mm_remove_node(&dev_priv->fbc.compressed_fb);
  210. err_llb:
  211. 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);
  212. return -ENOSPC;
  213. }
  214. int i915_gem_stolen_setup_compression(struct drm_device *dev, int size, int fb_cpp)
  215. {
  216. struct drm_i915_private *dev_priv = dev->dev_private;
  217. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  218. return -ENODEV;
  219. if (size < dev_priv->fbc.size)
  220. return 0;
  221. /* Release any current block */
  222. i915_gem_stolen_cleanup_compression(dev);
  223. return i915_setup_compression(dev, size, fb_cpp);
  224. }
  225. void i915_gem_stolen_cleanup_compression(struct drm_device *dev)
  226. {
  227. struct drm_i915_private *dev_priv = dev->dev_private;
  228. if (dev_priv->fbc.size == 0)
  229. return;
  230. drm_mm_remove_node(&dev_priv->fbc.compressed_fb);
  231. if (dev_priv->fbc.compressed_llb) {
  232. drm_mm_remove_node(dev_priv->fbc.compressed_llb);
  233. kfree(dev_priv->fbc.compressed_llb);
  234. }
  235. dev_priv->fbc.size = 0;
  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. i915_gem_stolen_cleanup_compression(dev);
  243. drm_mm_takedown(&dev_priv->mm.stolen);
  244. }
  245. int i915_gem_init_stolen(struct drm_device *dev)
  246. {
  247. struct drm_i915_private *dev_priv = dev->dev_private;
  248. u32 tmp;
  249. int bios_reserved = 0;
  250. #ifdef CONFIG_INTEL_IOMMU
  251. if (intel_iommu_gfx_mapped && INTEL_INFO(dev)->gen < 8) {
  252. DRM_INFO("DMAR active, disabling use of stolen memory\n");
  253. return 0;
  254. }
  255. #endif
  256. if (dev_priv->gtt.stolen_size == 0)
  257. return 0;
  258. dev_priv->mm.stolen_base = i915_stolen_to_physical(dev);
  259. if (dev_priv->mm.stolen_base == 0)
  260. return 0;
  261. DRM_DEBUG_KMS("found %zd bytes of stolen memory at %08lx\n",
  262. dev_priv->gtt.stolen_size, dev_priv->mm.stolen_base);
  263. if (INTEL_INFO(dev)->gen >= 8) {
  264. tmp = I915_READ(GEN7_BIOS_RESERVED);
  265. tmp >>= GEN8_BIOS_RESERVED_SHIFT;
  266. tmp &= GEN8_BIOS_RESERVED_MASK;
  267. bios_reserved = (1024*1024) << tmp;
  268. } else if (IS_GEN7(dev)) {
  269. tmp = I915_READ(GEN7_BIOS_RESERVED);
  270. bios_reserved = tmp & GEN7_BIOS_RESERVED_256K ?
  271. 256*1024 : 1024*1024;
  272. }
  273. if (WARN_ON(bios_reserved > dev_priv->gtt.stolen_size))
  274. return 0;
  275. /* Basic memrange allocator for stolen space */
  276. drm_mm_init(&dev_priv->mm.stolen, 0, dev_priv->gtt.stolen_size -
  277. bios_reserved);
  278. return 0;
  279. }
  280. static struct sg_table *
  281. i915_pages_create_for_stolen(struct drm_device *dev,
  282. u32 offset, u32 size)
  283. {
  284. struct drm_i915_private *dev_priv = dev->dev_private;
  285. struct sg_table *st;
  286. struct scatterlist *sg;
  287. DRM_DEBUG_DRIVER("offset=0x%x, size=%d\n", offset, size);
  288. BUG_ON(offset > dev_priv->gtt.stolen_size - size);
  289. /* We hide that we have no struct page backing our stolen object
  290. * by wrapping the contiguous physical allocation with a fake
  291. * dma mapping in a single scatterlist.
  292. */
  293. st = kmalloc(sizeof(*st), GFP_KERNEL);
  294. if (st == NULL)
  295. return NULL;
  296. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  297. kfree(st);
  298. return NULL;
  299. }
  300. sg = st->sgl;
  301. sg->offset = 0;
  302. sg->length = size;
  303. sg_dma_address(sg) = (dma_addr_t)dev_priv->mm.stolen_base + offset;
  304. sg_dma_len(sg) = size;
  305. return st;
  306. }
  307. static int i915_gem_object_get_pages_stolen(struct drm_i915_gem_object *obj)
  308. {
  309. BUG();
  310. return -EINVAL;
  311. }
  312. static void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object *obj)
  313. {
  314. /* Should only be called during free */
  315. sg_free_table(obj->pages);
  316. kfree(obj->pages);
  317. }
  318. static void
  319. i915_gem_object_release_stolen(struct drm_i915_gem_object *obj)
  320. {
  321. if (obj->stolen) {
  322. drm_mm_remove_node(obj->stolen);
  323. kfree(obj->stolen);
  324. obj->stolen = NULL;
  325. }
  326. }
  327. static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = {
  328. .get_pages = i915_gem_object_get_pages_stolen,
  329. .put_pages = i915_gem_object_put_pages_stolen,
  330. .release = i915_gem_object_release_stolen,
  331. };
  332. static struct drm_i915_gem_object *
  333. _i915_gem_object_create_stolen(struct drm_device *dev,
  334. struct drm_mm_node *stolen)
  335. {
  336. struct drm_i915_gem_object *obj;
  337. obj = i915_gem_object_alloc(dev);
  338. if (obj == NULL)
  339. return NULL;
  340. drm_gem_private_object_init(dev, &obj->base, stolen->size);
  341. i915_gem_object_init(obj, &i915_gem_object_stolen_ops);
  342. obj->pages = i915_pages_create_for_stolen(dev,
  343. stolen->start, stolen->size);
  344. if (obj->pages == NULL)
  345. goto cleanup;
  346. obj->has_dma_mapping = true;
  347. i915_gem_object_pin_pages(obj);
  348. obj->stolen = stolen;
  349. obj->base.read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT;
  350. obj->cache_level = HAS_LLC(dev) ? I915_CACHE_LLC : I915_CACHE_NONE;
  351. return obj;
  352. cleanup:
  353. i915_gem_object_free(obj);
  354. return NULL;
  355. }
  356. struct drm_i915_gem_object *
  357. i915_gem_object_create_stolen(struct drm_device *dev, u32 size)
  358. {
  359. struct drm_i915_private *dev_priv = dev->dev_private;
  360. struct drm_i915_gem_object *obj;
  361. struct drm_mm_node *stolen;
  362. int ret;
  363. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  364. return NULL;
  365. DRM_DEBUG_KMS("creating stolen object: size=%x\n", size);
  366. if (size == 0)
  367. return NULL;
  368. stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
  369. if (!stolen)
  370. return NULL;
  371. ret = drm_mm_insert_node(&dev_priv->mm.stolen, stolen, size,
  372. 4096, DRM_MM_SEARCH_DEFAULT);
  373. if (ret) {
  374. kfree(stolen);
  375. return NULL;
  376. }
  377. obj = _i915_gem_object_create_stolen(dev, stolen);
  378. if (obj)
  379. return obj;
  380. drm_mm_remove_node(stolen);
  381. kfree(stolen);
  382. return NULL;
  383. }
  384. struct drm_i915_gem_object *
  385. i915_gem_object_create_stolen_for_preallocated(struct drm_device *dev,
  386. u32 stolen_offset,
  387. u32 gtt_offset,
  388. u32 size)
  389. {
  390. struct drm_i915_private *dev_priv = dev->dev_private;
  391. struct i915_address_space *ggtt = &dev_priv->gtt.base;
  392. struct drm_i915_gem_object *obj;
  393. struct drm_mm_node *stolen;
  394. struct i915_vma *vma;
  395. int ret;
  396. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  397. return NULL;
  398. DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%x, gtt_offset=%x, size=%x\n",
  399. stolen_offset, gtt_offset, size);
  400. /* KISS and expect everything to be page-aligned */
  401. BUG_ON(stolen_offset & 4095);
  402. BUG_ON(size & 4095);
  403. if (WARN_ON(size == 0))
  404. return NULL;
  405. stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
  406. if (!stolen)
  407. return NULL;
  408. stolen->start = stolen_offset;
  409. stolen->size = size;
  410. ret = drm_mm_reserve_node(&dev_priv->mm.stolen, stolen);
  411. if (ret) {
  412. DRM_DEBUG_KMS("failed to allocate stolen space\n");
  413. kfree(stolen);
  414. return NULL;
  415. }
  416. obj = _i915_gem_object_create_stolen(dev, stolen);
  417. if (obj == NULL) {
  418. DRM_DEBUG_KMS("failed to allocate stolen object\n");
  419. drm_mm_remove_node(stolen);
  420. kfree(stolen);
  421. return NULL;
  422. }
  423. /* Some objects just need physical mem from stolen space */
  424. if (gtt_offset == I915_GTT_OFFSET_NONE)
  425. return obj;
  426. vma = i915_gem_obj_lookup_or_create_vma(obj, ggtt);
  427. if (IS_ERR(vma)) {
  428. ret = PTR_ERR(vma);
  429. goto err_out;
  430. }
  431. /* To simplify the initialisation sequence between KMS and GTT,
  432. * we allow construction of the stolen object prior to
  433. * setting up the GTT space. The actual reservation will occur
  434. * later.
  435. */
  436. vma->node.start = gtt_offset;
  437. vma->node.size = size;
  438. if (drm_mm_initialized(&ggtt->mm)) {
  439. ret = drm_mm_reserve_node(&ggtt->mm, &vma->node);
  440. if (ret) {
  441. DRM_DEBUG_KMS("failed to allocate stolen GTT space\n");
  442. goto err_vma;
  443. }
  444. }
  445. obj->has_global_gtt_mapping = 1;
  446. list_add_tail(&obj->global_list, &dev_priv->mm.bound_list);
  447. list_add_tail(&vma->mm_list, &ggtt->inactive_list);
  448. i915_gem_object_pin_pages(obj);
  449. return obj;
  450. err_vma:
  451. i915_gem_vma_destroy(vma);
  452. err_out:
  453. drm_mm_remove_node(stolen);
  454. kfree(stolen);
  455. drm_gem_object_unreference(&obj->base);
  456. return NULL;
  457. }