i915_gem_stolen.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 pci_dev *pdev = dev_priv->bridge_dev;
  46. struct resource *r;
  47. u32 base;
  48. /* On the machines I have tested the Graphics Base of Stolen Memory
  49. * is unreliable, so on those compute the base by subtracting the
  50. * stolen memory from the Top of Low Usable DRAM which is where the
  51. * BIOS places the graphics stolen memory.
  52. *
  53. * On gen2, the layout is slightly different with the Graphics Segment
  54. * immediately following Top of Memory (or Top of Usable DRAM). Note
  55. * it appears that TOUD is only reported by 865g, so we just use the
  56. * top of memory as determined by the e820 probe.
  57. *
  58. * XXX gen2 requires an unavailable symbol and 945gm fails with
  59. * its value of TOLUD.
  60. */
  61. base = 0;
  62. if (IS_VALLEYVIEW(dev)) {
  63. pci_read_config_dword(dev->pdev, 0x5c, &base);
  64. base &= ~((1<<20) - 1);
  65. } else if (INTEL_INFO(dev)->gen >= 6) {
  66. /* Read Base Data of Stolen Memory Register (BDSM) directly.
  67. * Note that there is also a MCHBAR miror at 0x1080c0 or
  68. * we could use device 2:0x5c instead.
  69. */
  70. pci_read_config_dword(pdev, 0xB0, &base);
  71. base &= ~4095; /* lower bits used for locking register */
  72. } else if (INTEL_INFO(dev)->gen > 3 || IS_G33(dev)) {
  73. /* Read Graphics Base of Stolen Memory directly */
  74. pci_read_config_dword(pdev, 0xA4, &base);
  75. #if 0
  76. } else if (IS_GEN3(dev)) {
  77. u8 val;
  78. /* Stolen is immediately below Top of Low Usable DRAM */
  79. pci_read_config_byte(pdev, 0x9c, &val);
  80. base = val >> 3 << 27;
  81. base -= dev_priv->mm.gtt->stolen_size;
  82. } else {
  83. /* Stolen is immediately above Top of Memory */
  84. base = max_low_pfn_mapped << PAGE_SHIFT;
  85. #endif
  86. }
  87. if (base == 0)
  88. return 0;
  89. /* Verify that nothing else uses this physical address. Stolen
  90. * memory should be reserved by the BIOS and hidden from the
  91. * kernel. So if the region is already marked as busy, something
  92. * is seriously wrong.
  93. */
  94. r = devm_request_mem_region(dev->dev, base, dev_priv->gtt.stolen_size,
  95. "Graphics Stolen Memory");
  96. if (r == NULL) {
  97. DRM_ERROR("conflict detected with stolen region: [0x%08x - 0x%08x]\n",
  98. base, base + (uint32_t)dev_priv->gtt.stolen_size);
  99. base = 0;
  100. }
  101. return base;
  102. }
  103. static int i915_setup_compression(struct drm_device *dev, int size)
  104. {
  105. struct drm_i915_private *dev_priv = dev->dev_private;
  106. struct drm_mm_node *compressed_fb, *uninitialized_var(compressed_llb);
  107. /* Try to over-allocate to reduce reallocations and fragmentation */
  108. compressed_fb = drm_mm_search_free(&dev_priv->mm.stolen,
  109. size <<= 1, 4096, 0);
  110. if (!compressed_fb)
  111. compressed_fb = drm_mm_search_free(&dev_priv->mm.stolen,
  112. size >>= 1, 4096, 0);
  113. if (compressed_fb)
  114. compressed_fb = drm_mm_get_block(compressed_fb, size, 4096);
  115. if (!compressed_fb)
  116. goto err;
  117. if (HAS_PCH_SPLIT(dev))
  118. I915_WRITE(ILK_DPFC_CB_BASE, compressed_fb->start);
  119. else if (IS_GM45(dev)) {
  120. I915_WRITE(DPFC_CB_BASE, compressed_fb->start);
  121. } else {
  122. compressed_llb = drm_mm_search_free(&dev_priv->mm.stolen,
  123. 4096, 4096, 0);
  124. if (compressed_llb)
  125. compressed_llb = drm_mm_get_block(compressed_llb,
  126. 4096, 4096);
  127. if (!compressed_llb)
  128. goto err_fb;
  129. dev_priv->fbc.compressed_llb = compressed_llb;
  130. I915_WRITE(FBC_CFB_BASE,
  131. dev_priv->mm.stolen_base + compressed_fb->start);
  132. I915_WRITE(FBC_LL_BASE,
  133. dev_priv->mm.stolen_base + compressed_llb->start);
  134. }
  135. dev_priv->fbc.compressed_fb = compressed_fb;
  136. dev_priv->fbc.size = size;
  137. DRM_DEBUG_KMS("reserved %d bytes of contiguous stolen space for FBC\n",
  138. size);
  139. return 0;
  140. err_fb:
  141. drm_mm_put_block(compressed_fb);
  142. err:
  143. 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);
  144. return -ENOSPC;
  145. }
  146. int i915_gem_stolen_setup_compression(struct drm_device *dev, int size)
  147. {
  148. struct drm_i915_private *dev_priv = dev->dev_private;
  149. if (dev_priv->mm.stolen_base == 0)
  150. return -ENODEV;
  151. if (size < dev_priv->fbc.size)
  152. return 0;
  153. /* Release any current block */
  154. i915_gem_stolen_cleanup_compression(dev);
  155. return i915_setup_compression(dev, size);
  156. }
  157. void i915_gem_stolen_cleanup_compression(struct drm_device *dev)
  158. {
  159. struct drm_i915_private *dev_priv = dev->dev_private;
  160. if (dev_priv->fbc.size == 0)
  161. return;
  162. if (dev_priv->fbc.compressed_fb)
  163. drm_mm_put_block(dev_priv->fbc.compressed_fb);
  164. if (dev_priv->fbc.compressed_llb)
  165. drm_mm_put_block(dev_priv->fbc.compressed_llb);
  166. dev_priv->fbc.size = 0;
  167. }
  168. void i915_gem_cleanup_stolen(struct drm_device *dev)
  169. {
  170. struct drm_i915_private *dev_priv = dev->dev_private;
  171. i915_gem_stolen_cleanup_compression(dev);
  172. drm_mm_takedown(&dev_priv->mm.stolen);
  173. }
  174. int i915_gem_init_stolen(struct drm_device *dev)
  175. {
  176. struct drm_i915_private *dev_priv = dev->dev_private;
  177. int bios_reserved = 0;
  178. dev_priv->mm.stolen_base = i915_stolen_to_physical(dev);
  179. if (dev_priv->mm.stolen_base == 0)
  180. return 0;
  181. DRM_DEBUG_KMS("found %zd bytes of stolen memory at %08lx\n",
  182. dev_priv->gtt.stolen_size, dev_priv->mm.stolen_base);
  183. if (IS_VALLEYVIEW(dev))
  184. bios_reserved = 1024*1024; /* top 1M on VLV/BYT */
  185. /* Basic memrange allocator for stolen space */
  186. drm_mm_init(&dev_priv->mm.stolen, 0, dev_priv->gtt.stolen_size -
  187. bios_reserved);
  188. return 0;
  189. }
  190. static struct sg_table *
  191. i915_pages_create_for_stolen(struct drm_device *dev,
  192. u32 offset, u32 size)
  193. {
  194. struct drm_i915_private *dev_priv = dev->dev_private;
  195. struct sg_table *st;
  196. struct scatterlist *sg;
  197. DRM_DEBUG_DRIVER("offset=0x%x, size=%d\n", offset, size);
  198. BUG_ON(offset > dev_priv->gtt.stolen_size - size);
  199. /* We hide that we have no struct page backing our stolen object
  200. * by wrapping the contiguous physical allocation with a fake
  201. * dma mapping in a single scatterlist.
  202. */
  203. st = kmalloc(sizeof(*st), GFP_KERNEL);
  204. if (st == NULL)
  205. return NULL;
  206. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  207. kfree(st);
  208. return NULL;
  209. }
  210. sg = st->sgl;
  211. sg->offset = offset;
  212. sg->length = size;
  213. sg_dma_address(sg) = (dma_addr_t)dev_priv->mm.stolen_base + offset;
  214. sg_dma_len(sg) = size;
  215. return st;
  216. }
  217. static int i915_gem_object_get_pages_stolen(struct drm_i915_gem_object *obj)
  218. {
  219. BUG();
  220. return -EINVAL;
  221. }
  222. static void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object *obj)
  223. {
  224. /* Should only be called during free */
  225. sg_free_table(obj->pages);
  226. kfree(obj->pages);
  227. }
  228. static const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = {
  229. .get_pages = i915_gem_object_get_pages_stolen,
  230. .put_pages = i915_gem_object_put_pages_stolen,
  231. };
  232. static struct drm_i915_gem_object *
  233. _i915_gem_object_create_stolen(struct drm_device *dev,
  234. struct drm_mm_node *stolen)
  235. {
  236. struct drm_i915_gem_object *obj;
  237. obj = i915_gem_object_alloc(dev);
  238. if (obj == NULL)
  239. return NULL;
  240. if (drm_gem_private_object_init(dev, &obj->base, stolen->size))
  241. goto cleanup;
  242. i915_gem_object_init(obj, &i915_gem_object_stolen_ops);
  243. obj->pages = i915_pages_create_for_stolen(dev,
  244. stolen->start, stolen->size);
  245. if (obj->pages == NULL)
  246. goto cleanup;
  247. obj->has_dma_mapping = true;
  248. i915_gem_object_pin_pages(obj);
  249. obj->stolen = stolen;
  250. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  251. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  252. obj->cache_level = I915_CACHE_NONE;
  253. return obj;
  254. cleanup:
  255. i915_gem_object_free(obj);
  256. return NULL;
  257. }
  258. struct drm_i915_gem_object *
  259. i915_gem_object_create_stolen(struct drm_device *dev, u32 size)
  260. {
  261. struct drm_i915_private *dev_priv = dev->dev_private;
  262. struct drm_i915_gem_object *obj;
  263. struct drm_mm_node *stolen;
  264. if (dev_priv->mm.stolen_base == 0)
  265. return NULL;
  266. DRM_DEBUG_KMS("creating stolen object: size=%x\n", size);
  267. if (size == 0)
  268. return NULL;
  269. stolen = drm_mm_search_free(&dev_priv->mm.stolen, size, 4096, 0);
  270. if (stolen)
  271. stolen = drm_mm_get_block(stolen, size, 4096);
  272. if (stolen == NULL)
  273. return NULL;
  274. obj = _i915_gem_object_create_stolen(dev, stolen);
  275. if (obj)
  276. return obj;
  277. drm_mm_put_block(stolen);
  278. return NULL;
  279. }
  280. struct drm_i915_gem_object *
  281. i915_gem_object_create_stolen_for_preallocated(struct drm_device *dev,
  282. u32 stolen_offset,
  283. u32 gtt_offset,
  284. u32 size)
  285. {
  286. struct drm_i915_private *dev_priv = dev->dev_private;
  287. struct drm_i915_gem_object *obj;
  288. struct drm_mm_node *stolen;
  289. int ret;
  290. if (dev_priv->mm.stolen_base == 0)
  291. return NULL;
  292. DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%x, gtt_offset=%x, size=%x\n",
  293. stolen_offset, gtt_offset, size);
  294. /* KISS and expect everything to be page-aligned */
  295. BUG_ON(stolen_offset & 4095);
  296. BUG_ON(size & 4095);
  297. if (WARN_ON(size == 0))
  298. return NULL;
  299. stolen = kzalloc(sizeof(*stolen), GFP_KERNEL);
  300. if (!stolen)
  301. return NULL;
  302. stolen->start = stolen_offset;
  303. stolen->size = size;
  304. ret = drm_mm_reserve_node(&dev_priv->mm.stolen, stolen);
  305. if (ret) {
  306. DRM_DEBUG_KMS("failed to allocate stolen space\n");
  307. kfree(stolen);
  308. return NULL;
  309. }
  310. obj = _i915_gem_object_create_stolen(dev, stolen);
  311. if (obj == NULL) {
  312. DRM_DEBUG_KMS("failed to allocate stolen object\n");
  313. drm_mm_put_block(stolen);
  314. return NULL;
  315. }
  316. /* Some objects just need physical mem from stolen space */
  317. if (gtt_offset == I915_GTT_OFFSET_NONE)
  318. return obj;
  319. /* To simplify the initialisation sequence between KMS and GTT,
  320. * we allow construction of the stolen object prior to
  321. * setting up the GTT space. The actual reservation will occur
  322. * later.
  323. */
  324. obj->gtt_space.start = gtt_offset;
  325. obj->gtt_space.size = size;
  326. if (drm_mm_initialized(&dev_priv->mm.gtt_space)) {
  327. ret = drm_mm_reserve_node(&dev_priv->mm.gtt_space,
  328. &obj->gtt_space);
  329. if (ret) {
  330. DRM_DEBUG_KMS("failed to allocate stolen GTT space\n");
  331. goto unref_out;
  332. }
  333. }
  334. obj->has_global_gtt_mapping = 1;
  335. list_add_tail(&obj->global_list, &dev_priv->mm.bound_list);
  336. list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  337. return obj;
  338. unref_out:
  339. drm_gem_object_unreference(&obj->base);
  340. return NULL;
  341. }
  342. void
  343. i915_gem_object_release_stolen(struct drm_i915_gem_object *obj)
  344. {
  345. if (obj->stolen) {
  346. drm_mm_put_block(obj->stolen);
  347. obj->stolen = NULL;
  348. }
  349. }