i915_gem_stolen.c 15 KB

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