i915_gem_stolen.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. /* Verify that nothing else uses this physical address. Stolen
  72. * memory should be reserved by the BIOS and hidden from the
  73. * kernel. So if the region is already marked as busy, something
  74. * is seriously wrong.
  75. */
  76. r = devm_request_mem_region(dev->dev, base, dev_priv->gtt.stolen_size,
  77. "Graphics Stolen Memory");
  78. if (r == NULL) {
  79. DRM_ERROR("conflict detected with stolen region: [0x%08x - 0x%08x]\n",
  80. base, base + (uint32_t)dev_priv->gtt.stolen_size);
  81. base = 0;
  82. }
  83. return base;
  84. }
  85. static int i915_setup_compression(struct drm_device *dev, int size)
  86. {
  87. struct drm_i915_private *dev_priv = dev->dev_private;
  88. struct drm_mm_node *compressed_fb, *uninitialized_var(compressed_llb);
  89. /* Try to over-allocate to reduce reallocations and fragmentation */
  90. compressed_fb = drm_mm_search_free(&dev_priv->mm.stolen,
  91. size <<= 1, 4096, 0);
  92. if (!compressed_fb)
  93. compressed_fb = drm_mm_search_free(&dev_priv->mm.stolen,
  94. size >>= 1, 4096, 0);
  95. if (compressed_fb)
  96. compressed_fb = drm_mm_get_block(compressed_fb, size, 4096);
  97. if (!compressed_fb)
  98. goto err;
  99. if (HAS_PCH_SPLIT(dev))
  100. I915_WRITE(ILK_DPFC_CB_BASE, compressed_fb->start);
  101. else if (IS_GM45(dev)) {
  102. I915_WRITE(DPFC_CB_BASE, compressed_fb->start);
  103. } else {
  104. compressed_llb = drm_mm_search_free(&dev_priv->mm.stolen,
  105. 4096, 4096, 0);
  106. if (compressed_llb)
  107. compressed_llb = drm_mm_get_block(compressed_llb,
  108. 4096, 4096);
  109. if (!compressed_llb)
  110. goto err_fb;
  111. dev_priv->fbc.compressed_llb = compressed_llb;
  112. I915_WRITE(FBC_CFB_BASE,
  113. dev_priv->mm.stolen_base + compressed_fb->start);
  114. I915_WRITE(FBC_LL_BASE,
  115. dev_priv->mm.stolen_base + compressed_llb->start);
  116. }
  117. dev_priv->fbc.compressed_fb = compressed_fb;
  118. dev_priv->fbc.size = size;
  119. DRM_DEBUG_KMS("reserved %d bytes of contiguous stolen space for FBC\n",
  120. size);
  121. return 0;
  122. err_fb:
  123. drm_mm_put_block(compressed_fb);
  124. err:
  125. 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);
  126. return -ENOSPC;
  127. }
  128. int i915_gem_stolen_setup_compression(struct drm_device *dev, int size)
  129. {
  130. struct drm_i915_private *dev_priv = dev->dev_private;
  131. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  132. return -ENODEV;
  133. if (size < dev_priv->fbc.size)
  134. return 0;
  135. /* Release any current block */
  136. i915_gem_stolen_cleanup_compression(dev);
  137. return i915_setup_compression(dev, size);
  138. }
  139. void i915_gem_stolen_cleanup_compression(struct drm_device *dev)
  140. {
  141. struct drm_i915_private *dev_priv = dev->dev_private;
  142. if (dev_priv->fbc.size == 0)
  143. return;
  144. if (dev_priv->fbc.compressed_fb)
  145. drm_mm_put_block(dev_priv->fbc.compressed_fb);
  146. if (dev_priv->fbc.compressed_llb)
  147. drm_mm_put_block(dev_priv->fbc.compressed_llb);
  148. dev_priv->fbc.size = 0;
  149. }
  150. void i915_gem_cleanup_stolen(struct drm_device *dev)
  151. {
  152. struct drm_i915_private *dev_priv = dev->dev_private;
  153. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  154. return;
  155. i915_gem_stolen_cleanup_compression(dev);
  156. drm_mm_takedown(&dev_priv->mm.stolen);
  157. }
  158. int i915_gem_init_stolen(struct drm_device *dev)
  159. {
  160. struct drm_i915_private *dev_priv = dev->dev_private;
  161. int bios_reserved = 0;
  162. dev_priv->mm.stolen_base = i915_stolen_to_physical(dev);
  163. if (dev_priv->mm.stolen_base == 0)
  164. return 0;
  165. DRM_DEBUG_KMS("found %zd bytes of stolen memory at %08lx\n",
  166. dev_priv->gtt.stolen_size, dev_priv->mm.stolen_base);
  167. if (IS_VALLEYVIEW(dev))
  168. bios_reserved = 1024*1024; /* top 1M on VLV/BYT */
  169. if (WARN_ON(bios_reserved > dev_priv->gtt.stolen_size))
  170. return 0;
  171. /* Basic memrange allocator for stolen space */
  172. drm_mm_init(&dev_priv->mm.stolen, 0, dev_priv->gtt.stolen_size -
  173. bios_reserved);
  174. return 0;
  175. }
  176. static struct sg_table *
  177. i915_pages_create_for_stolen(struct drm_device *dev,
  178. u32 offset, u32 size)
  179. {
  180. struct drm_i915_private *dev_priv = dev->dev_private;
  181. struct sg_table *st;
  182. struct scatterlist *sg;
  183. DRM_DEBUG_DRIVER("offset=0x%x, size=%d\n", offset, size);
  184. BUG_ON(offset > dev_priv->gtt.stolen_size - size);
  185. /* We hide that we have no struct page backing our stolen object
  186. * by wrapping the contiguous physical allocation with a fake
  187. * dma mapping in a single scatterlist.
  188. */
  189. st = kmalloc(sizeof(*st), GFP_KERNEL);
  190. if (st == NULL)
  191. return NULL;
  192. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  193. kfree(st);
  194. return NULL;
  195. }
  196. sg = st->sgl;
  197. sg->offset = offset;
  198. sg->length = size;
  199. sg_dma_address(sg) = (dma_addr_t)dev_priv->mm.stolen_base + offset;
  200. sg_dma_len(sg) = size;
  201. return st;
  202. }
  203. static int i915_gem_object_get_pages_stolen(struct drm_i915_gem_object *obj)
  204. {
  205. BUG();
  206. return -EINVAL;
  207. }
  208. static void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object *obj)
  209. {
  210. /* Should only be called during free */
  211. sg_free_table(obj->pages);
  212. kfree(obj->pages);
  213. }
  214. static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = {
  215. .get_pages = i915_gem_object_get_pages_stolen,
  216. .put_pages = i915_gem_object_put_pages_stolen,
  217. };
  218. static struct drm_i915_gem_object *
  219. _i915_gem_object_create_stolen(struct drm_device *dev,
  220. struct drm_mm_node *stolen)
  221. {
  222. struct drm_i915_gem_object *obj;
  223. obj = i915_gem_object_alloc(dev);
  224. if (obj == NULL)
  225. return NULL;
  226. if (drm_gem_private_object_init(dev, &obj->base, stolen->size))
  227. goto cleanup;
  228. i915_gem_object_init(obj, &i915_gem_object_stolen_ops);
  229. obj->pages = i915_pages_create_for_stolen(dev,
  230. stolen->start, stolen->size);
  231. if (obj->pages == NULL)
  232. goto cleanup;
  233. obj->has_dma_mapping = true;
  234. i915_gem_object_pin_pages(obj);
  235. obj->stolen = stolen;
  236. obj->base.read_domains = I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT;
  237. obj->cache_level = HAS_LLC(dev) ? I915_CACHE_LLC : I915_CACHE_NONE;
  238. return obj;
  239. cleanup:
  240. i915_gem_object_free(obj);
  241. return NULL;
  242. }
  243. struct drm_i915_gem_object *
  244. i915_gem_object_create_stolen(struct drm_device *dev, u32 size)
  245. {
  246. struct drm_i915_private *dev_priv = dev->dev_private;
  247. struct drm_i915_gem_object *obj;
  248. struct drm_mm_node *stolen;
  249. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  250. return NULL;
  251. DRM_DEBUG_KMS("creating stolen object: size=%x\n", size);
  252. if (size == 0)
  253. return NULL;
  254. stolen = drm_mm_search_free(&dev_priv->mm.stolen, size, 4096, 0);
  255. if (stolen)
  256. stolen = drm_mm_get_block(stolen, size, 4096);
  257. if (stolen == NULL)
  258. return NULL;
  259. obj = _i915_gem_object_create_stolen(dev, stolen);
  260. if (obj)
  261. return obj;
  262. drm_mm_put_block(stolen);
  263. return NULL;
  264. }
  265. struct drm_i915_gem_object *
  266. i915_gem_object_create_stolen_for_preallocated(struct drm_device *dev,
  267. u32 stolen_offset,
  268. u32 gtt_offset,
  269. u32 size)
  270. {
  271. struct drm_i915_private *dev_priv = dev->dev_private;
  272. struct i915_address_space *ggtt = &dev_priv->gtt.base;
  273. struct drm_i915_gem_object *obj;
  274. struct drm_mm_node *stolen;
  275. struct i915_vma *vma;
  276. int ret;
  277. if (!drm_mm_initialized(&dev_priv->mm.stolen))
  278. return NULL;
  279. DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%x, gtt_offset=%x, size=%x\n",
  280. stolen_offset, gtt_offset, size);
  281. /* KISS and expect everything to be page-aligned */
  282. BUG_ON(stolen_offset & 4095);
  283. BUG_ON(size & 4095);
  284. if (WARN_ON(size == 0))
  285. return NULL;
  286. stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
  287. if (!stolen)
  288. return NULL;
  289. stolen->start = stolen_offset;
  290. stolen->size = size;
  291. ret = drm_mm_reserve_node(&dev_priv->mm.stolen, stolen);
  292. if (ret) {
  293. DRM_DEBUG_KMS("failed to allocate stolen space\n");
  294. kfree(stolen);
  295. return NULL;
  296. }
  297. obj = _i915_gem_object_create_stolen(dev, stolen);
  298. if (obj == NULL) {
  299. DRM_DEBUG_KMS("failed to allocate stolen object\n");
  300. drm_mm_put_block(stolen);
  301. return NULL;
  302. }
  303. /* Some objects just need physical mem from stolen space */
  304. if (gtt_offset == I915_GTT_OFFSET_NONE)
  305. return obj;
  306. vma = i915_gem_vma_create(obj, ggtt);
  307. if (IS_ERR(vma)) {
  308. ret = PTR_ERR(vma);
  309. goto err_out;
  310. }
  311. /* To simplify the initialisation sequence between KMS and GTT,
  312. * we allow construction of the stolen object prior to
  313. * setting up the GTT space. The actual reservation will occur
  314. * later.
  315. */
  316. vma->node.start = gtt_offset;
  317. vma->node.size = size;
  318. if (drm_mm_initialized(&ggtt->mm)) {
  319. ret = drm_mm_reserve_node(&ggtt->mm, &vma->node);
  320. if (ret) {
  321. DRM_DEBUG_KMS("failed to allocate stolen GTT space\n");
  322. i915_gem_vma_destroy(vma);
  323. goto err_out;
  324. }
  325. }
  326. obj->has_global_gtt_mapping = 1;
  327. list_add_tail(&obj->global_list, &dev_priv->mm.bound_list);
  328. list_add_tail(&vma->mm_list, &ggtt->inactive_list);
  329. return obj;
  330. err_out:
  331. drm_mm_put_block(stolen);
  332. drm_gem_object_unreference(&obj->base);
  333. return NULL;
  334. }
  335. void
  336. i915_gem_object_release_stolen(struct drm_i915_gem_object *obj)
  337. {
  338. if (obj->stolen) {
  339. drm_mm_put_block(obj->stolen);
  340. obj->stolen = NULL;
  341. }
  342. }