i915_gem_gtt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Copyright © 2010 Daniel Vetter
  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. */
  24. #include "drmP.h"
  25. #include "drm.h"
  26. #include "i915_drm.h"
  27. #include "i915_drv.h"
  28. #include "i915_trace.h"
  29. #include "intel_drv.h"
  30. /* PPGTT support for Sandybdrige/Gen6 and later */
  31. static void i915_ppgtt_clear_range(struct i915_hw_ppgtt *ppgtt,
  32. unsigned first_entry,
  33. unsigned num_entries)
  34. {
  35. uint32_t *pt_vaddr;
  36. uint32_t scratch_pte;
  37. unsigned act_pd = first_entry / I915_PPGTT_PT_ENTRIES;
  38. unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
  39. unsigned last_pte, i;
  40. scratch_pte = GEN6_PTE_ADDR_ENCODE(ppgtt->scratch_page_dma_addr);
  41. scratch_pte |= GEN6_PTE_VALID | GEN6_PTE_CACHE_LLC;
  42. while (num_entries) {
  43. last_pte = first_pte + num_entries;
  44. if (last_pte > I915_PPGTT_PT_ENTRIES)
  45. last_pte = I915_PPGTT_PT_ENTRIES;
  46. pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pd]);
  47. for (i = first_pte; i < last_pte; i++)
  48. pt_vaddr[i] = scratch_pte;
  49. kunmap_atomic(pt_vaddr);
  50. num_entries -= last_pte - first_pte;
  51. first_pte = 0;
  52. act_pd++;
  53. }
  54. }
  55. int i915_gem_init_aliasing_ppgtt(struct drm_device *dev)
  56. {
  57. struct drm_i915_private *dev_priv = dev->dev_private;
  58. struct i915_hw_ppgtt *ppgtt;
  59. unsigned first_pd_entry_in_global_pt;
  60. int i;
  61. int ret = -ENOMEM;
  62. /* ppgtt PDEs reside in the global gtt pagetable, which has 512*1024
  63. * entries. For aliasing ppgtt support we just steal them at the end for
  64. * now. */
  65. first_pd_entry_in_global_pt = dev_priv->mm.gtt->gtt_total_entries - I915_PPGTT_PD_ENTRIES;
  66. ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
  67. if (!ppgtt)
  68. return ret;
  69. ppgtt->num_pd_entries = I915_PPGTT_PD_ENTRIES;
  70. ppgtt->pt_pages = kzalloc(sizeof(struct page *)*ppgtt->num_pd_entries,
  71. GFP_KERNEL);
  72. if (!ppgtt->pt_pages)
  73. goto err_ppgtt;
  74. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  75. ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
  76. if (!ppgtt->pt_pages[i])
  77. goto err_pt_alloc;
  78. }
  79. if (dev_priv->mm.gtt->needs_dmar) {
  80. ppgtt->pt_dma_addr = kzalloc(sizeof(dma_addr_t)
  81. *ppgtt->num_pd_entries,
  82. GFP_KERNEL);
  83. if (!ppgtt->pt_dma_addr)
  84. goto err_pt_alloc;
  85. }
  86. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  87. dma_addr_t pt_addr;
  88. if (dev_priv->mm.gtt->needs_dmar) {
  89. pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i],
  90. 0, 4096,
  91. PCI_DMA_BIDIRECTIONAL);
  92. if (pci_dma_mapping_error(dev->pdev,
  93. pt_addr)) {
  94. ret = -EIO;
  95. goto err_pd_pin;
  96. }
  97. ppgtt->pt_dma_addr[i] = pt_addr;
  98. } else
  99. pt_addr = page_to_phys(ppgtt->pt_pages[i]);
  100. }
  101. ppgtt->scratch_page_dma_addr = dev_priv->mm.gtt->scratch_page_dma;
  102. i915_ppgtt_clear_range(ppgtt, 0,
  103. ppgtt->num_pd_entries*I915_PPGTT_PT_ENTRIES);
  104. ppgtt->pd_offset = (first_pd_entry_in_global_pt)*sizeof(uint32_t);
  105. dev_priv->mm.aliasing_ppgtt = ppgtt;
  106. return 0;
  107. err_pd_pin:
  108. if (ppgtt->pt_dma_addr) {
  109. for (i--; i >= 0; i--)
  110. pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
  111. 4096, PCI_DMA_BIDIRECTIONAL);
  112. }
  113. err_pt_alloc:
  114. kfree(ppgtt->pt_dma_addr);
  115. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  116. if (ppgtt->pt_pages[i])
  117. __free_page(ppgtt->pt_pages[i]);
  118. }
  119. kfree(ppgtt->pt_pages);
  120. err_ppgtt:
  121. kfree(ppgtt);
  122. return ret;
  123. }
  124. void i915_gem_cleanup_aliasing_ppgtt(struct drm_device *dev)
  125. {
  126. struct drm_i915_private *dev_priv = dev->dev_private;
  127. struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
  128. int i;
  129. if (!ppgtt)
  130. return;
  131. if (ppgtt->pt_dma_addr) {
  132. for (i = 0; i < ppgtt->num_pd_entries; i++)
  133. pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
  134. 4096, PCI_DMA_BIDIRECTIONAL);
  135. }
  136. kfree(ppgtt->pt_dma_addr);
  137. for (i = 0; i < ppgtt->num_pd_entries; i++)
  138. __free_page(ppgtt->pt_pages[i]);
  139. kfree(ppgtt->pt_pages);
  140. kfree(ppgtt);
  141. }
  142. static void i915_ppgtt_insert_sg_entries(struct i915_hw_ppgtt *ppgtt,
  143. struct scatterlist *sg_list,
  144. unsigned sg_len,
  145. unsigned first_entry,
  146. uint32_t pte_flags)
  147. {
  148. uint32_t *pt_vaddr, pte;
  149. unsigned act_pd = first_entry / I915_PPGTT_PT_ENTRIES;
  150. unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
  151. unsigned i, j, m, segment_len;
  152. dma_addr_t page_addr;
  153. struct scatterlist *sg;
  154. /* init sg walking */
  155. sg = sg_list;
  156. i = 0;
  157. segment_len = sg_dma_len(sg) >> PAGE_SHIFT;
  158. m = 0;
  159. while (i < sg_len) {
  160. pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pd]);
  161. for (j = first_pte; j < I915_PPGTT_PT_ENTRIES; j++) {
  162. page_addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
  163. pte = GEN6_PTE_ADDR_ENCODE(page_addr);
  164. pt_vaddr[j] = pte | pte_flags;
  165. /* grab the next page */
  166. m++;
  167. if (m == segment_len) {
  168. sg = sg_next(sg);
  169. i++;
  170. if (i == sg_len)
  171. break;
  172. segment_len = sg_dma_len(sg) >> PAGE_SHIFT;
  173. m = 0;
  174. }
  175. }
  176. kunmap_atomic(pt_vaddr);
  177. first_pte = 0;
  178. act_pd++;
  179. }
  180. }
  181. static void i915_ppgtt_insert_pages(struct i915_hw_ppgtt *ppgtt,
  182. unsigned first_entry, unsigned num_entries,
  183. struct page **pages, uint32_t pte_flags)
  184. {
  185. uint32_t *pt_vaddr, pte;
  186. unsigned act_pd = first_entry / I915_PPGTT_PT_ENTRIES;
  187. unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
  188. unsigned last_pte, i;
  189. dma_addr_t page_addr;
  190. while (num_entries) {
  191. last_pte = first_pte + num_entries;
  192. last_pte = min_t(unsigned, last_pte, I915_PPGTT_PT_ENTRIES);
  193. pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pd]);
  194. for (i = first_pte; i < last_pte; i++) {
  195. page_addr = page_to_phys(*pages);
  196. pte = GEN6_PTE_ADDR_ENCODE(page_addr);
  197. pt_vaddr[i] = pte | pte_flags;
  198. pages++;
  199. }
  200. kunmap_atomic(pt_vaddr);
  201. num_entries -= last_pte - first_pte;
  202. first_pte = 0;
  203. act_pd++;
  204. }
  205. }
  206. void i915_ppgtt_bind_object(struct i915_hw_ppgtt *ppgtt,
  207. struct drm_i915_gem_object *obj,
  208. enum i915_cache_level cache_level)
  209. {
  210. struct drm_device *dev = obj->base.dev;
  211. struct drm_i915_private *dev_priv = dev->dev_private;
  212. uint32_t pte_flags = GEN6_PTE_VALID;
  213. switch (cache_level) {
  214. case I915_CACHE_LLC_MLC:
  215. pte_flags |= GEN6_PTE_CACHE_LLC_MLC;
  216. break;
  217. case I915_CACHE_LLC:
  218. pte_flags |= GEN6_PTE_CACHE_LLC;
  219. break;
  220. case I915_CACHE_NONE:
  221. pte_flags |= GEN6_PTE_UNCACHED;
  222. break;
  223. default:
  224. BUG();
  225. }
  226. if (dev_priv->mm.gtt->needs_dmar) {
  227. BUG_ON(!obj->sg_list);
  228. i915_ppgtt_insert_sg_entries(ppgtt,
  229. obj->sg_list,
  230. obj->num_sg,
  231. obj->gtt_space->start >> PAGE_SHIFT,
  232. pte_flags);
  233. } else
  234. i915_ppgtt_insert_pages(ppgtt,
  235. obj->gtt_space->start >> PAGE_SHIFT,
  236. obj->base.size >> PAGE_SHIFT,
  237. obj->pages,
  238. pte_flags);
  239. }
  240. void i915_ppgtt_unbind_object(struct i915_hw_ppgtt *ppgtt,
  241. struct drm_i915_gem_object *obj)
  242. {
  243. i915_ppgtt_clear_range(ppgtt,
  244. obj->gtt_space->start >> PAGE_SHIFT,
  245. obj->base.size >> PAGE_SHIFT);
  246. }
  247. /* XXX kill agp_type! */
  248. static unsigned int cache_level_to_agp_type(struct drm_device *dev,
  249. enum i915_cache_level cache_level)
  250. {
  251. switch (cache_level) {
  252. case I915_CACHE_LLC_MLC:
  253. if (INTEL_INFO(dev)->gen >= 6)
  254. return AGP_USER_CACHED_MEMORY_LLC_MLC;
  255. /* Older chipsets do not have this extra level of CPU
  256. * cacheing, so fallthrough and request the PTE simply
  257. * as cached.
  258. */
  259. case I915_CACHE_LLC:
  260. return AGP_USER_CACHED_MEMORY;
  261. default:
  262. case I915_CACHE_NONE:
  263. return AGP_USER_MEMORY;
  264. }
  265. }
  266. static bool do_idling(struct drm_i915_private *dev_priv)
  267. {
  268. bool ret = dev_priv->mm.interruptible;
  269. if (unlikely(dev_priv->mm.gtt->do_idle_maps)) {
  270. dev_priv->mm.interruptible = false;
  271. if (i915_gpu_idle(dev_priv->dev, false)) {
  272. DRM_ERROR("Couldn't idle GPU\n");
  273. /* Wait a bit, in hopes it avoids the hang */
  274. udelay(10);
  275. }
  276. }
  277. return ret;
  278. }
  279. static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
  280. {
  281. if (unlikely(dev_priv->mm.gtt->do_idle_maps))
  282. dev_priv->mm.interruptible = interruptible;
  283. }
  284. void i915_gem_restore_gtt_mappings(struct drm_device *dev)
  285. {
  286. struct drm_i915_private *dev_priv = dev->dev_private;
  287. struct drm_i915_gem_object *obj;
  288. /* First fill our portion of the GTT with scratch pages */
  289. intel_gtt_clear_range(dev_priv->mm.gtt_start / PAGE_SIZE,
  290. (dev_priv->mm.gtt_end - dev_priv->mm.gtt_start) / PAGE_SIZE);
  291. list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
  292. i915_gem_clflush_object(obj);
  293. i915_gem_gtt_rebind_object(obj, obj->cache_level);
  294. }
  295. intel_gtt_chipset_flush();
  296. }
  297. int i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj)
  298. {
  299. struct drm_device *dev = obj->base.dev;
  300. struct drm_i915_private *dev_priv = dev->dev_private;
  301. unsigned int agp_type = cache_level_to_agp_type(dev, obj->cache_level);
  302. int ret;
  303. if (dev_priv->mm.gtt->needs_dmar) {
  304. ret = intel_gtt_map_memory(obj->pages,
  305. obj->base.size >> PAGE_SHIFT,
  306. &obj->sg_list,
  307. &obj->num_sg);
  308. if (ret != 0)
  309. return ret;
  310. intel_gtt_insert_sg_entries(obj->sg_list,
  311. obj->num_sg,
  312. obj->gtt_space->start >> PAGE_SHIFT,
  313. agp_type);
  314. } else
  315. intel_gtt_insert_pages(obj->gtt_space->start >> PAGE_SHIFT,
  316. obj->base.size >> PAGE_SHIFT,
  317. obj->pages,
  318. agp_type);
  319. return 0;
  320. }
  321. void i915_gem_gtt_rebind_object(struct drm_i915_gem_object *obj,
  322. enum i915_cache_level cache_level)
  323. {
  324. struct drm_device *dev = obj->base.dev;
  325. struct drm_i915_private *dev_priv = dev->dev_private;
  326. unsigned int agp_type = cache_level_to_agp_type(dev, cache_level);
  327. if (dev_priv->mm.gtt->needs_dmar) {
  328. BUG_ON(!obj->sg_list);
  329. intel_gtt_insert_sg_entries(obj->sg_list,
  330. obj->num_sg,
  331. obj->gtt_space->start >> PAGE_SHIFT,
  332. agp_type);
  333. } else
  334. intel_gtt_insert_pages(obj->gtt_space->start >> PAGE_SHIFT,
  335. obj->base.size >> PAGE_SHIFT,
  336. obj->pages,
  337. agp_type);
  338. }
  339. void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj)
  340. {
  341. struct drm_device *dev = obj->base.dev;
  342. struct drm_i915_private *dev_priv = dev->dev_private;
  343. bool interruptible;
  344. interruptible = do_idling(dev_priv);
  345. intel_gtt_clear_range(obj->gtt_space->start >> PAGE_SHIFT,
  346. obj->base.size >> PAGE_SHIFT);
  347. if (obj->sg_list) {
  348. intel_gtt_unmap_memory(obj->sg_list, obj->num_sg);
  349. obj->sg_list = NULL;
  350. }
  351. undo_idling(dev_priv, interruptible);
  352. }