i915_gem_gtt.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227
  1. /*
  2. * Copyright © 2010 Daniel Vetter
  3. * Copyright © 2011-2014 Intel Corporation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. *
  24. */
  25. #include <linux/seq_file.h>
  26. #include <drm/drmP.h>
  27. #include <drm/i915_drm.h>
  28. #include "i915_drv.h"
  29. #include "i915_trace.h"
  30. #include "intel_drv.h"
  31. static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv);
  32. static void chv_setup_private_ppat(struct drm_i915_private *dev_priv);
  33. static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
  34. {
  35. bool has_aliasing_ppgtt;
  36. bool has_full_ppgtt;
  37. has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
  38. has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
  39. if (IS_GEN8(dev))
  40. has_full_ppgtt = false; /* XXX why? */
  41. /*
  42. * We don't allow disabling PPGTT for gen9+ as it's a requirement for
  43. * execlists, the sole mechanism available to submit work.
  44. */
  45. if (INTEL_INFO(dev)->gen < 9 &&
  46. (enable_ppgtt == 0 || !has_aliasing_ppgtt))
  47. return 0;
  48. if (enable_ppgtt == 1)
  49. return 1;
  50. if (enable_ppgtt == 2 && has_full_ppgtt)
  51. return 2;
  52. #ifdef CONFIG_INTEL_IOMMU
  53. /* Disable ppgtt on SNB if VT-d is on. */
  54. if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
  55. DRM_INFO("Disabling PPGTT because VT-d is on\n");
  56. return 0;
  57. }
  58. #endif
  59. /* Early VLV doesn't have this */
  60. if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  61. dev->pdev->revision < 0xb) {
  62. DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
  63. return 0;
  64. }
  65. return has_aliasing_ppgtt ? 1 : 0;
  66. }
  67. static void ppgtt_bind_vma(struct i915_vma *vma,
  68. enum i915_cache_level cache_level,
  69. u32 flags);
  70. static void ppgtt_unbind_vma(struct i915_vma *vma);
  71. static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
  72. enum i915_cache_level level,
  73. bool valid)
  74. {
  75. gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
  76. pte |= addr;
  77. switch (level) {
  78. case I915_CACHE_NONE:
  79. pte |= PPAT_UNCACHED_INDEX;
  80. break;
  81. case I915_CACHE_WT:
  82. pte |= PPAT_DISPLAY_ELLC_INDEX;
  83. break;
  84. default:
  85. pte |= PPAT_CACHED_INDEX;
  86. break;
  87. }
  88. return pte;
  89. }
  90. static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev,
  91. dma_addr_t addr,
  92. enum i915_cache_level level)
  93. {
  94. gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
  95. pde |= addr;
  96. if (level != I915_CACHE_NONE)
  97. pde |= PPAT_CACHED_PDE_INDEX;
  98. else
  99. pde |= PPAT_UNCACHED_INDEX;
  100. return pde;
  101. }
  102. static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
  103. enum i915_cache_level level,
  104. bool valid, u32 unused)
  105. {
  106. gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  107. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  108. switch (level) {
  109. case I915_CACHE_L3_LLC:
  110. case I915_CACHE_LLC:
  111. pte |= GEN6_PTE_CACHE_LLC;
  112. break;
  113. case I915_CACHE_NONE:
  114. pte |= GEN6_PTE_UNCACHED;
  115. break;
  116. default:
  117. WARN_ON(1);
  118. }
  119. return pte;
  120. }
  121. static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
  122. enum i915_cache_level level,
  123. bool valid, u32 unused)
  124. {
  125. gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  126. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  127. switch (level) {
  128. case I915_CACHE_L3_LLC:
  129. pte |= GEN7_PTE_CACHE_L3_LLC;
  130. break;
  131. case I915_CACHE_LLC:
  132. pte |= GEN6_PTE_CACHE_LLC;
  133. break;
  134. case I915_CACHE_NONE:
  135. pte |= GEN6_PTE_UNCACHED;
  136. break;
  137. default:
  138. WARN_ON(1);
  139. }
  140. return pte;
  141. }
  142. static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
  143. enum i915_cache_level level,
  144. bool valid, u32 flags)
  145. {
  146. gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  147. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  148. if (!(flags & PTE_READ_ONLY))
  149. pte |= BYT_PTE_WRITEABLE;
  150. if (level != I915_CACHE_NONE)
  151. pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
  152. return pte;
  153. }
  154. static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
  155. enum i915_cache_level level,
  156. bool valid, u32 unused)
  157. {
  158. gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  159. pte |= HSW_PTE_ADDR_ENCODE(addr);
  160. if (level != I915_CACHE_NONE)
  161. pte |= HSW_WB_LLC_AGE3;
  162. return pte;
  163. }
  164. static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
  165. enum i915_cache_level level,
  166. bool valid, u32 unused)
  167. {
  168. gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  169. pte |= HSW_PTE_ADDR_ENCODE(addr);
  170. switch (level) {
  171. case I915_CACHE_NONE:
  172. break;
  173. case I915_CACHE_WT:
  174. pte |= HSW_WT_ELLC_LLC_AGE3;
  175. break;
  176. default:
  177. pte |= HSW_WB_ELLC_LLC_AGE3;
  178. break;
  179. }
  180. return pte;
  181. }
  182. /* Broadwell Page Directory Pointer Descriptors */
  183. static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry,
  184. uint64_t val)
  185. {
  186. int ret;
  187. BUG_ON(entry >= 4);
  188. ret = intel_ring_begin(ring, 6);
  189. if (ret)
  190. return ret;
  191. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  192. intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
  193. intel_ring_emit(ring, (u32)(val >> 32));
  194. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  195. intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
  196. intel_ring_emit(ring, (u32)(val));
  197. intel_ring_advance(ring);
  198. return 0;
  199. }
  200. static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
  201. struct intel_engine_cs *ring)
  202. {
  203. int i, ret;
  204. /* bit of a hack to find the actual last used pd */
  205. int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE;
  206. for (i = used_pd - 1; i >= 0; i--) {
  207. dma_addr_t addr = ppgtt->pd_dma_addr[i];
  208. ret = gen8_write_pdp(ring, i, addr);
  209. if (ret)
  210. return ret;
  211. }
  212. return 0;
  213. }
  214. static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
  215. uint64_t start,
  216. uint64_t length,
  217. bool use_scratch)
  218. {
  219. struct i915_hw_ppgtt *ppgtt =
  220. container_of(vm, struct i915_hw_ppgtt, base);
  221. gen8_gtt_pte_t *pt_vaddr, scratch_pte;
  222. unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
  223. unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
  224. unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
  225. unsigned num_entries = length >> PAGE_SHIFT;
  226. unsigned last_pte, i;
  227. scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
  228. I915_CACHE_LLC, use_scratch);
  229. while (num_entries) {
  230. struct page *page_table = ppgtt->gen8_pt_pages[pdpe][pde];
  231. last_pte = pte + num_entries;
  232. if (last_pte > GEN8_PTES_PER_PAGE)
  233. last_pte = GEN8_PTES_PER_PAGE;
  234. pt_vaddr = kmap_atomic(page_table);
  235. for (i = pte; i < last_pte; i++) {
  236. pt_vaddr[i] = scratch_pte;
  237. num_entries--;
  238. }
  239. if (!HAS_LLC(ppgtt->base.dev))
  240. drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
  241. kunmap_atomic(pt_vaddr);
  242. pte = 0;
  243. if (++pde == GEN8_PDES_PER_PAGE) {
  244. pdpe++;
  245. pde = 0;
  246. }
  247. }
  248. }
  249. static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
  250. struct sg_table *pages,
  251. uint64_t start,
  252. enum i915_cache_level cache_level, u32 unused)
  253. {
  254. struct i915_hw_ppgtt *ppgtt =
  255. container_of(vm, struct i915_hw_ppgtt, base);
  256. gen8_gtt_pte_t *pt_vaddr;
  257. unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
  258. unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
  259. unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
  260. struct sg_page_iter sg_iter;
  261. pt_vaddr = NULL;
  262. for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
  263. if (WARN_ON(pdpe >= GEN8_LEGACY_PDPS))
  264. break;
  265. if (pt_vaddr == NULL)
  266. pt_vaddr = kmap_atomic(ppgtt->gen8_pt_pages[pdpe][pde]);
  267. pt_vaddr[pte] =
  268. gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
  269. cache_level, true);
  270. if (++pte == GEN8_PTES_PER_PAGE) {
  271. if (!HAS_LLC(ppgtt->base.dev))
  272. drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
  273. kunmap_atomic(pt_vaddr);
  274. pt_vaddr = NULL;
  275. if (++pde == GEN8_PDES_PER_PAGE) {
  276. pdpe++;
  277. pde = 0;
  278. }
  279. pte = 0;
  280. }
  281. }
  282. if (pt_vaddr) {
  283. if (!HAS_LLC(ppgtt->base.dev))
  284. drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
  285. kunmap_atomic(pt_vaddr);
  286. }
  287. }
  288. static void gen8_free_page_tables(struct page **pt_pages)
  289. {
  290. int i;
  291. if (pt_pages == NULL)
  292. return;
  293. for (i = 0; i < GEN8_PDES_PER_PAGE; i++)
  294. if (pt_pages[i])
  295. __free_pages(pt_pages[i], 0);
  296. }
  297. static void gen8_ppgtt_free(const struct i915_hw_ppgtt *ppgtt)
  298. {
  299. int i;
  300. for (i = 0; i < ppgtt->num_pd_pages; i++) {
  301. gen8_free_page_tables(ppgtt->gen8_pt_pages[i]);
  302. kfree(ppgtt->gen8_pt_pages[i]);
  303. kfree(ppgtt->gen8_pt_dma_addr[i]);
  304. }
  305. __free_pages(ppgtt->pd_pages, get_order(ppgtt->num_pd_pages << PAGE_SHIFT));
  306. }
  307. static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
  308. {
  309. struct pci_dev *hwdev = ppgtt->base.dev->pdev;
  310. int i, j;
  311. for (i = 0; i < ppgtt->num_pd_pages; i++) {
  312. /* TODO: In the future we'll support sparse mappings, so this
  313. * will have to change. */
  314. if (!ppgtt->pd_dma_addr[i])
  315. continue;
  316. pci_unmap_page(hwdev, ppgtt->pd_dma_addr[i], PAGE_SIZE,
  317. PCI_DMA_BIDIRECTIONAL);
  318. for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
  319. dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
  320. if (addr)
  321. pci_unmap_page(hwdev, addr, PAGE_SIZE,
  322. PCI_DMA_BIDIRECTIONAL);
  323. }
  324. }
  325. }
  326. static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
  327. {
  328. struct i915_hw_ppgtt *ppgtt =
  329. container_of(vm, struct i915_hw_ppgtt, base);
  330. gen8_ppgtt_unmap_pages(ppgtt);
  331. gen8_ppgtt_free(ppgtt);
  332. }
  333. static struct page **__gen8_alloc_page_tables(void)
  334. {
  335. struct page **pt_pages;
  336. int i;
  337. pt_pages = kcalloc(GEN8_PDES_PER_PAGE, sizeof(struct page *), GFP_KERNEL);
  338. if (!pt_pages)
  339. return ERR_PTR(-ENOMEM);
  340. for (i = 0; i < GEN8_PDES_PER_PAGE; i++) {
  341. pt_pages[i] = alloc_page(GFP_KERNEL);
  342. if (!pt_pages[i])
  343. goto bail;
  344. }
  345. return pt_pages;
  346. bail:
  347. gen8_free_page_tables(pt_pages);
  348. kfree(pt_pages);
  349. return ERR_PTR(-ENOMEM);
  350. }
  351. static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt,
  352. const int max_pdp)
  353. {
  354. struct page **pt_pages[GEN8_LEGACY_PDPS];
  355. int i, ret;
  356. for (i = 0; i < max_pdp; i++) {
  357. pt_pages[i] = __gen8_alloc_page_tables();
  358. if (IS_ERR(pt_pages[i])) {
  359. ret = PTR_ERR(pt_pages[i]);
  360. goto unwind_out;
  361. }
  362. }
  363. /* NB: Avoid touching gen8_pt_pages until last to keep the allocation,
  364. * "atomic" - for cleanup purposes.
  365. */
  366. for (i = 0; i < max_pdp; i++)
  367. ppgtt->gen8_pt_pages[i] = pt_pages[i];
  368. return 0;
  369. unwind_out:
  370. while (i--) {
  371. gen8_free_page_tables(pt_pages[i]);
  372. kfree(pt_pages[i]);
  373. }
  374. return ret;
  375. }
  376. static int gen8_ppgtt_allocate_dma(struct i915_hw_ppgtt *ppgtt)
  377. {
  378. int i;
  379. for (i = 0; i < ppgtt->num_pd_pages; i++) {
  380. ppgtt->gen8_pt_dma_addr[i] = kcalloc(GEN8_PDES_PER_PAGE,
  381. sizeof(dma_addr_t),
  382. GFP_KERNEL);
  383. if (!ppgtt->gen8_pt_dma_addr[i])
  384. return -ENOMEM;
  385. }
  386. return 0;
  387. }
  388. static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
  389. const int max_pdp)
  390. {
  391. ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
  392. if (!ppgtt->pd_pages)
  393. return -ENOMEM;
  394. ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
  395. BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
  396. return 0;
  397. }
  398. static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
  399. const int max_pdp)
  400. {
  401. int ret;
  402. ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
  403. if (ret)
  404. return ret;
  405. ret = gen8_ppgtt_allocate_page_tables(ppgtt, max_pdp);
  406. if (ret) {
  407. __free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
  408. return ret;
  409. }
  410. ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
  411. ret = gen8_ppgtt_allocate_dma(ppgtt);
  412. if (ret)
  413. gen8_ppgtt_free(ppgtt);
  414. return ret;
  415. }
  416. static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
  417. const int pd)
  418. {
  419. dma_addr_t pd_addr;
  420. int ret;
  421. pd_addr = pci_map_page(ppgtt->base.dev->pdev,
  422. &ppgtt->pd_pages[pd], 0,
  423. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  424. ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
  425. if (ret)
  426. return ret;
  427. ppgtt->pd_dma_addr[pd] = pd_addr;
  428. return 0;
  429. }
  430. static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
  431. const int pd,
  432. const int pt)
  433. {
  434. dma_addr_t pt_addr;
  435. struct page *p;
  436. int ret;
  437. p = ppgtt->gen8_pt_pages[pd][pt];
  438. pt_addr = pci_map_page(ppgtt->base.dev->pdev,
  439. p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  440. ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
  441. if (ret)
  442. return ret;
  443. ppgtt->gen8_pt_dma_addr[pd][pt] = pt_addr;
  444. return 0;
  445. }
  446. /**
  447. * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
  448. * with a net effect resembling a 2-level page table in normal x86 terms. Each
  449. * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
  450. * space.
  451. *
  452. * FIXME: split allocation into smaller pieces. For now we only ever do this
  453. * once, but with full PPGTT, the multiple contiguous allocations will be bad.
  454. * TODO: Do something with the size parameter
  455. */
  456. static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
  457. {
  458. const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
  459. const int min_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
  460. int i, j, ret;
  461. if (size % (1<<30))
  462. DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
  463. /* 1. Do all our allocations for page directories and page tables. */
  464. ret = gen8_ppgtt_alloc(ppgtt, max_pdp);
  465. if (ret)
  466. return ret;
  467. /*
  468. * 2. Create DMA mappings for the page directories and page tables.
  469. */
  470. for (i = 0; i < max_pdp; i++) {
  471. ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
  472. if (ret)
  473. goto bail;
  474. for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
  475. ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
  476. if (ret)
  477. goto bail;
  478. }
  479. }
  480. /*
  481. * 3. Map all the page directory entires to point to the page tables
  482. * we've allocated.
  483. *
  484. * For now, the PPGTT helper functions all require that the PDEs are
  485. * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
  486. * will never need to touch the PDEs again.
  487. */
  488. for (i = 0; i < max_pdp; i++) {
  489. gen8_ppgtt_pde_t *pd_vaddr;
  490. pd_vaddr = kmap_atomic(&ppgtt->pd_pages[i]);
  491. for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
  492. dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
  493. pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
  494. I915_CACHE_LLC);
  495. }
  496. if (!HAS_LLC(ppgtt->base.dev))
  497. drm_clflush_virt_range(pd_vaddr, PAGE_SIZE);
  498. kunmap_atomic(pd_vaddr);
  499. }
  500. ppgtt->switch_mm = gen8_mm_switch;
  501. ppgtt->base.clear_range = gen8_ppgtt_clear_range;
  502. ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
  503. ppgtt->base.cleanup = gen8_ppgtt_cleanup;
  504. ppgtt->base.start = 0;
  505. ppgtt->base.total = ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE * PAGE_SIZE;
  506. ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
  507. DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
  508. ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
  509. DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
  510. ppgtt->num_pd_entries,
  511. (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30));
  512. return 0;
  513. bail:
  514. gen8_ppgtt_unmap_pages(ppgtt);
  515. gen8_ppgtt_free(ppgtt);
  516. return ret;
  517. }
  518. static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
  519. {
  520. struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
  521. struct i915_address_space *vm = &ppgtt->base;
  522. gen6_gtt_pte_t __iomem *pd_addr;
  523. gen6_gtt_pte_t scratch_pte;
  524. uint32_t pd_entry;
  525. int pte, pde;
  526. scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
  527. pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm +
  528. ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
  529. seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
  530. ppgtt->pd_offset, ppgtt->pd_offset + ppgtt->num_pd_entries);
  531. for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
  532. u32 expected;
  533. gen6_gtt_pte_t *pt_vaddr;
  534. dma_addr_t pt_addr = ppgtt->pt_dma_addr[pde];
  535. pd_entry = readl(pd_addr + pde);
  536. expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
  537. if (pd_entry != expected)
  538. seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
  539. pde,
  540. pd_entry,
  541. expected);
  542. seq_printf(m, "\tPDE: %x\n", pd_entry);
  543. pt_vaddr = kmap_atomic(ppgtt->pt_pages[pde]);
  544. for (pte = 0; pte < I915_PPGTT_PT_ENTRIES; pte+=4) {
  545. unsigned long va =
  546. (pde * PAGE_SIZE * I915_PPGTT_PT_ENTRIES) +
  547. (pte * PAGE_SIZE);
  548. int i;
  549. bool found = false;
  550. for (i = 0; i < 4; i++)
  551. if (pt_vaddr[pte + i] != scratch_pte)
  552. found = true;
  553. if (!found)
  554. continue;
  555. seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
  556. for (i = 0; i < 4; i++) {
  557. if (pt_vaddr[pte + i] != scratch_pte)
  558. seq_printf(m, " %08x", pt_vaddr[pte + i]);
  559. else
  560. seq_puts(m, " SCRATCH ");
  561. }
  562. seq_puts(m, "\n");
  563. }
  564. kunmap_atomic(pt_vaddr);
  565. }
  566. }
  567. static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
  568. {
  569. struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
  570. gen6_gtt_pte_t __iomem *pd_addr;
  571. uint32_t pd_entry;
  572. int i;
  573. WARN_ON(ppgtt->pd_offset & 0x3f);
  574. pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
  575. ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
  576. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  577. dma_addr_t pt_addr;
  578. pt_addr = ppgtt->pt_dma_addr[i];
  579. pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
  580. pd_entry |= GEN6_PDE_VALID;
  581. writel(pd_entry, pd_addr + i);
  582. }
  583. readl(pd_addr);
  584. }
  585. static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
  586. {
  587. BUG_ON(ppgtt->pd_offset & 0x3f);
  588. return (ppgtt->pd_offset / 64) << 16;
  589. }
  590. static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
  591. struct intel_engine_cs *ring)
  592. {
  593. int ret;
  594. /* NB: TLBs must be flushed and invalidated before a switch */
  595. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  596. if (ret)
  597. return ret;
  598. ret = intel_ring_begin(ring, 6);
  599. if (ret)
  600. return ret;
  601. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
  602. intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
  603. intel_ring_emit(ring, PP_DIR_DCLV_2G);
  604. intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
  605. intel_ring_emit(ring, get_pd_offset(ppgtt));
  606. intel_ring_emit(ring, MI_NOOP);
  607. intel_ring_advance(ring);
  608. return 0;
  609. }
  610. static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
  611. struct intel_engine_cs *ring)
  612. {
  613. int ret;
  614. /* NB: TLBs must be flushed and invalidated before a switch */
  615. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  616. if (ret)
  617. return ret;
  618. ret = intel_ring_begin(ring, 6);
  619. if (ret)
  620. return ret;
  621. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
  622. intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
  623. intel_ring_emit(ring, PP_DIR_DCLV_2G);
  624. intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
  625. intel_ring_emit(ring, get_pd_offset(ppgtt));
  626. intel_ring_emit(ring, MI_NOOP);
  627. intel_ring_advance(ring);
  628. /* XXX: RCS is the only one to auto invalidate the TLBs? */
  629. if (ring->id != RCS) {
  630. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  631. if (ret)
  632. return ret;
  633. }
  634. return 0;
  635. }
  636. static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
  637. struct intel_engine_cs *ring)
  638. {
  639. struct drm_device *dev = ppgtt->base.dev;
  640. struct drm_i915_private *dev_priv = dev->dev_private;
  641. I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
  642. I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
  643. POSTING_READ(RING_PP_DIR_DCLV(ring));
  644. return 0;
  645. }
  646. static void gen8_ppgtt_enable(struct drm_device *dev)
  647. {
  648. struct drm_i915_private *dev_priv = dev->dev_private;
  649. struct intel_engine_cs *ring;
  650. int j;
  651. for_each_ring(ring, dev_priv, j) {
  652. I915_WRITE(RING_MODE_GEN7(ring),
  653. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  654. }
  655. }
  656. static void gen7_ppgtt_enable(struct drm_device *dev)
  657. {
  658. struct drm_i915_private *dev_priv = dev->dev_private;
  659. struct intel_engine_cs *ring;
  660. uint32_t ecochk, ecobits;
  661. int i;
  662. ecobits = I915_READ(GAC_ECO_BITS);
  663. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
  664. ecochk = I915_READ(GAM_ECOCHK);
  665. if (IS_HASWELL(dev)) {
  666. ecochk |= ECOCHK_PPGTT_WB_HSW;
  667. } else {
  668. ecochk |= ECOCHK_PPGTT_LLC_IVB;
  669. ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
  670. }
  671. I915_WRITE(GAM_ECOCHK, ecochk);
  672. for_each_ring(ring, dev_priv, i) {
  673. /* GFX_MODE is per-ring on gen7+ */
  674. I915_WRITE(RING_MODE_GEN7(ring),
  675. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  676. }
  677. }
  678. static void gen6_ppgtt_enable(struct drm_device *dev)
  679. {
  680. struct drm_i915_private *dev_priv = dev->dev_private;
  681. uint32_t ecochk, gab_ctl, ecobits;
  682. ecobits = I915_READ(GAC_ECO_BITS);
  683. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
  684. ECOBITS_PPGTT_CACHE64B);
  685. gab_ctl = I915_READ(GAB_CTL);
  686. I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
  687. ecochk = I915_READ(GAM_ECOCHK);
  688. I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
  689. I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  690. }
  691. /* PPGTT support for Sandybdrige/Gen6 and later */
  692. static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
  693. uint64_t start,
  694. uint64_t length,
  695. bool use_scratch)
  696. {
  697. struct i915_hw_ppgtt *ppgtt =
  698. container_of(vm, struct i915_hw_ppgtt, base);
  699. gen6_gtt_pte_t *pt_vaddr, scratch_pte;
  700. unsigned first_entry = start >> PAGE_SHIFT;
  701. unsigned num_entries = length >> PAGE_SHIFT;
  702. unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
  703. unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
  704. unsigned last_pte, i;
  705. scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
  706. while (num_entries) {
  707. last_pte = first_pte + num_entries;
  708. if (last_pte > I915_PPGTT_PT_ENTRIES)
  709. last_pte = I915_PPGTT_PT_ENTRIES;
  710. pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
  711. for (i = first_pte; i < last_pte; i++)
  712. pt_vaddr[i] = scratch_pte;
  713. kunmap_atomic(pt_vaddr);
  714. num_entries -= last_pte - first_pte;
  715. first_pte = 0;
  716. act_pt++;
  717. }
  718. }
  719. static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
  720. struct sg_table *pages,
  721. uint64_t start,
  722. enum i915_cache_level cache_level, u32 flags)
  723. {
  724. struct i915_hw_ppgtt *ppgtt =
  725. container_of(vm, struct i915_hw_ppgtt, base);
  726. gen6_gtt_pte_t *pt_vaddr;
  727. unsigned first_entry = start >> PAGE_SHIFT;
  728. unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
  729. unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
  730. struct sg_page_iter sg_iter;
  731. pt_vaddr = NULL;
  732. for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
  733. if (pt_vaddr == NULL)
  734. pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
  735. pt_vaddr[act_pte] =
  736. vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
  737. cache_level, true, flags);
  738. if (++act_pte == I915_PPGTT_PT_ENTRIES) {
  739. kunmap_atomic(pt_vaddr);
  740. pt_vaddr = NULL;
  741. act_pt++;
  742. act_pte = 0;
  743. }
  744. }
  745. if (pt_vaddr)
  746. kunmap_atomic(pt_vaddr);
  747. }
  748. static void gen6_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
  749. {
  750. int i;
  751. if (ppgtt->pt_dma_addr) {
  752. for (i = 0; i < ppgtt->num_pd_entries; i++)
  753. pci_unmap_page(ppgtt->base.dev->pdev,
  754. ppgtt->pt_dma_addr[i],
  755. 4096, PCI_DMA_BIDIRECTIONAL);
  756. }
  757. }
  758. static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
  759. {
  760. int i;
  761. kfree(ppgtt->pt_dma_addr);
  762. for (i = 0; i < ppgtt->num_pd_entries; i++)
  763. __free_page(ppgtt->pt_pages[i]);
  764. kfree(ppgtt->pt_pages);
  765. }
  766. static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
  767. {
  768. struct i915_hw_ppgtt *ppgtt =
  769. container_of(vm, struct i915_hw_ppgtt, base);
  770. drm_mm_remove_node(&ppgtt->node);
  771. gen6_ppgtt_unmap_pages(ppgtt);
  772. gen6_ppgtt_free(ppgtt);
  773. }
  774. static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
  775. {
  776. struct drm_device *dev = ppgtt->base.dev;
  777. struct drm_i915_private *dev_priv = dev->dev_private;
  778. bool retried = false;
  779. int ret;
  780. /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
  781. * allocator works in address space sizes, so it's multiplied by page
  782. * size. We allocate at the top of the GTT to avoid fragmentation.
  783. */
  784. BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
  785. alloc:
  786. ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
  787. &ppgtt->node, GEN6_PD_SIZE,
  788. GEN6_PD_ALIGN, 0,
  789. 0, dev_priv->gtt.base.total,
  790. DRM_MM_TOPDOWN);
  791. if (ret == -ENOSPC && !retried) {
  792. ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
  793. GEN6_PD_SIZE, GEN6_PD_ALIGN,
  794. I915_CACHE_NONE,
  795. 0, dev_priv->gtt.base.total,
  796. 0);
  797. if (ret)
  798. return ret;
  799. retried = true;
  800. goto alloc;
  801. }
  802. if (ppgtt->node.start < dev_priv->gtt.mappable_end)
  803. DRM_DEBUG("Forced to use aperture for PDEs\n");
  804. ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
  805. return ret;
  806. }
  807. static int gen6_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt)
  808. {
  809. int i;
  810. ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
  811. GFP_KERNEL);
  812. if (!ppgtt->pt_pages)
  813. return -ENOMEM;
  814. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  815. ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
  816. if (!ppgtt->pt_pages[i]) {
  817. gen6_ppgtt_free(ppgtt);
  818. return -ENOMEM;
  819. }
  820. }
  821. return 0;
  822. }
  823. static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
  824. {
  825. int ret;
  826. ret = gen6_ppgtt_allocate_page_directories(ppgtt);
  827. if (ret)
  828. return ret;
  829. ret = gen6_ppgtt_allocate_page_tables(ppgtt);
  830. if (ret) {
  831. drm_mm_remove_node(&ppgtt->node);
  832. return ret;
  833. }
  834. ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
  835. GFP_KERNEL);
  836. if (!ppgtt->pt_dma_addr) {
  837. drm_mm_remove_node(&ppgtt->node);
  838. gen6_ppgtt_free(ppgtt);
  839. return -ENOMEM;
  840. }
  841. return 0;
  842. }
  843. static int gen6_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt)
  844. {
  845. struct drm_device *dev = ppgtt->base.dev;
  846. int i;
  847. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  848. dma_addr_t pt_addr;
  849. pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
  850. PCI_DMA_BIDIRECTIONAL);
  851. if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
  852. gen6_ppgtt_unmap_pages(ppgtt);
  853. return -EIO;
  854. }
  855. ppgtt->pt_dma_addr[i] = pt_addr;
  856. }
  857. return 0;
  858. }
  859. static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
  860. {
  861. struct drm_device *dev = ppgtt->base.dev;
  862. struct drm_i915_private *dev_priv = dev->dev_private;
  863. int ret;
  864. ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
  865. if (IS_GEN6(dev)) {
  866. ppgtt->switch_mm = gen6_mm_switch;
  867. } else if (IS_HASWELL(dev)) {
  868. ppgtt->switch_mm = hsw_mm_switch;
  869. } else if (IS_GEN7(dev)) {
  870. ppgtt->switch_mm = gen7_mm_switch;
  871. } else
  872. BUG();
  873. ret = gen6_ppgtt_alloc(ppgtt);
  874. if (ret)
  875. return ret;
  876. ret = gen6_ppgtt_setup_page_tables(ppgtt);
  877. if (ret) {
  878. gen6_ppgtt_free(ppgtt);
  879. return ret;
  880. }
  881. ppgtt->base.clear_range = gen6_ppgtt_clear_range;
  882. ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
  883. ppgtt->base.cleanup = gen6_ppgtt_cleanup;
  884. ppgtt->base.start = 0;
  885. ppgtt->base.total = ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
  886. ppgtt->debug_dump = gen6_dump_ppgtt;
  887. ppgtt->pd_offset =
  888. ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
  889. ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
  890. DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
  891. ppgtt->node.size >> 20,
  892. ppgtt->node.start / PAGE_SIZE);
  893. gen6_write_pdes(ppgtt);
  894. DRM_DEBUG("Adding PPGTT at offset %x\n",
  895. ppgtt->pd_offset << 10);
  896. return 0;
  897. }
  898. static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
  899. {
  900. struct drm_i915_private *dev_priv = dev->dev_private;
  901. ppgtt->base.dev = dev;
  902. ppgtt->base.scratch = dev_priv->gtt.base.scratch;
  903. if (INTEL_INFO(dev)->gen < 8)
  904. return gen6_ppgtt_init(ppgtt);
  905. else if (IS_GEN8(dev) || IS_GEN9(dev))
  906. return gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
  907. else
  908. BUG();
  909. }
  910. int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
  911. {
  912. struct drm_i915_private *dev_priv = dev->dev_private;
  913. int ret = 0;
  914. ret = __hw_ppgtt_init(dev, ppgtt);
  915. if (ret == 0) {
  916. kref_init(&ppgtt->ref);
  917. drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
  918. ppgtt->base.total);
  919. i915_init_vm(dev_priv, &ppgtt->base);
  920. }
  921. return ret;
  922. }
  923. int i915_ppgtt_init_hw(struct drm_device *dev)
  924. {
  925. struct drm_i915_private *dev_priv = dev->dev_private;
  926. struct intel_engine_cs *ring;
  927. struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
  928. int i, ret = 0;
  929. /* In the case of execlists, PPGTT is enabled by the context descriptor
  930. * and the PDPs are contained within the context itself. We don't
  931. * need to do anything here. */
  932. if (i915.enable_execlists)
  933. return 0;
  934. if (!USES_PPGTT(dev))
  935. return 0;
  936. if (IS_GEN6(dev))
  937. gen6_ppgtt_enable(dev);
  938. else if (IS_GEN7(dev))
  939. gen7_ppgtt_enable(dev);
  940. else if (INTEL_INFO(dev)->gen >= 8)
  941. gen8_ppgtt_enable(dev);
  942. else
  943. WARN_ON(1);
  944. if (ppgtt) {
  945. for_each_ring(ring, dev_priv, i) {
  946. ret = ppgtt->switch_mm(ppgtt, ring);
  947. if (ret != 0)
  948. return ret;
  949. }
  950. }
  951. return ret;
  952. }
  953. struct i915_hw_ppgtt *
  954. i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
  955. {
  956. struct i915_hw_ppgtt *ppgtt;
  957. int ret;
  958. ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
  959. if (!ppgtt)
  960. return ERR_PTR(-ENOMEM);
  961. ret = i915_ppgtt_init(dev, ppgtt);
  962. if (ret) {
  963. kfree(ppgtt);
  964. return ERR_PTR(ret);
  965. }
  966. ppgtt->file_priv = fpriv;
  967. trace_i915_ppgtt_create(&ppgtt->base);
  968. return ppgtt;
  969. }
  970. void i915_ppgtt_release(struct kref *kref)
  971. {
  972. struct i915_hw_ppgtt *ppgtt =
  973. container_of(kref, struct i915_hw_ppgtt, ref);
  974. trace_i915_ppgtt_release(&ppgtt->base);
  975. /* vmas should already be unbound */
  976. WARN_ON(!list_empty(&ppgtt->base.active_list));
  977. WARN_ON(!list_empty(&ppgtt->base.inactive_list));
  978. list_del(&ppgtt->base.global_link);
  979. drm_mm_takedown(&ppgtt->base.mm);
  980. ppgtt->base.cleanup(&ppgtt->base);
  981. kfree(ppgtt);
  982. }
  983. static void
  984. ppgtt_bind_vma(struct i915_vma *vma,
  985. enum i915_cache_level cache_level,
  986. u32 flags)
  987. {
  988. /* Currently applicable only to VLV */
  989. if (vma->obj->gt_ro)
  990. flags |= PTE_READ_ONLY;
  991. vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
  992. cache_level, flags);
  993. }
  994. static void ppgtt_unbind_vma(struct i915_vma *vma)
  995. {
  996. vma->vm->clear_range(vma->vm,
  997. vma->node.start,
  998. vma->obj->base.size,
  999. true);
  1000. }
  1001. extern int intel_iommu_gfx_mapped;
  1002. /* Certain Gen5 chipsets require require idling the GPU before
  1003. * unmapping anything from the GTT when VT-d is enabled.
  1004. */
  1005. static inline bool needs_idle_maps(struct drm_device *dev)
  1006. {
  1007. #ifdef CONFIG_INTEL_IOMMU
  1008. /* Query intel_iommu to see if we need the workaround. Presumably that
  1009. * was loaded first.
  1010. */
  1011. if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
  1012. return true;
  1013. #endif
  1014. return false;
  1015. }
  1016. static bool do_idling(struct drm_i915_private *dev_priv)
  1017. {
  1018. bool ret = dev_priv->mm.interruptible;
  1019. if (unlikely(dev_priv->gtt.do_idle_maps)) {
  1020. dev_priv->mm.interruptible = false;
  1021. if (i915_gpu_idle(dev_priv->dev)) {
  1022. DRM_ERROR("Couldn't idle GPU\n");
  1023. /* Wait a bit, in hopes it avoids the hang */
  1024. udelay(10);
  1025. }
  1026. }
  1027. return ret;
  1028. }
  1029. static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
  1030. {
  1031. if (unlikely(dev_priv->gtt.do_idle_maps))
  1032. dev_priv->mm.interruptible = interruptible;
  1033. }
  1034. void i915_check_and_clear_faults(struct drm_device *dev)
  1035. {
  1036. struct drm_i915_private *dev_priv = dev->dev_private;
  1037. struct intel_engine_cs *ring;
  1038. int i;
  1039. if (INTEL_INFO(dev)->gen < 6)
  1040. return;
  1041. for_each_ring(ring, dev_priv, i) {
  1042. u32 fault_reg;
  1043. fault_reg = I915_READ(RING_FAULT_REG(ring));
  1044. if (fault_reg & RING_FAULT_VALID) {
  1045. DRM_DEBUG_DRIVER("Unexpected fault\n"
  1046. "\tAddr: 0x%08lx\n"
  1047. "\tAddress space: %s\n"
  1048. "\tSource ID: %d\n"
  1049. "\tType: %d\n",
  1050. fault_reg & PAGE_MASK,
  1051. fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
  1052. RING_FAULT_SRCID(fault_reg),
  1053. RING_FAULT_FAULT_TYPE(fault_reg));
  1054. I915_WRITE(RING_FAULT_REG(ring),
  1055. fault_reg & ~RING_FAULT_VALID);
  1056. }
  1057. }
  1058. POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
  1059. }
  1060. static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
  1061. {
  1062. if (INTEL_INFO(dev_priv->dev)->gen < 6) {
  1063. intel_gtt_chipset_flush();
  1064. } else {
  1065. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  1066. POSTING_READ(GFX_FLSH_CNTL_GEN6);
  1067. }
  1068. }
  1069. void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
  1070. {
  1071. struct drm_i915_private *dev_priv = dev->dev_private;
  1072. /* Don't bother messing with faults pre GEN6 as we have little
  1073. * documentation supporting that it's a good idea.
  1074. */
  1075. if (INTEL_INFO(dev)->gen < 6)
  1076. return;
  1077. i915_check_and_clear_faults(dev);
  1078. dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
  1079. dev_priv->gtt.base.start,
  1080. dev_priv->gtt.base.total,
  1081. true);
  1082. i915_ggtt_flush(dev_priv);
  1083. }
  1084. void i915_gem_restore_gtt_mappings(struct drm_device *dev)
  1085. {
  1086. struct drm_i915_private *dev_priv = dev->dev_private;
  1087. struct drm_i915_gem_object *obj;
  1088. struct i915_address_space *vm;
  1089. i915_check_and_clear_faults(dev);
  1090. /* First fill our portion of the GTT with scratch pages */
  1091. dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
  1092. dev_priv->gtt.base.start,
  1093. dev_priv->gtt.base.total,
  1094. true);
  1095. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  1096. struct i915_vma *vma = i915_gem_obj_to_vma(obj,
  1097. &dev_priv->gtt.base);
  1098. if (!vma)
  1099. continue;
  1100. i915_gem_clflush_object(obj, obj->pin_display);
  1101. /* The bind_vma code tries to be smart about tracking mappings.
  1102. * Unfortunately above, we've just wiped out the mappings
  1103. * without telling our object about it. So we need to fake it.
  1104. */
  1105. vma->bound &= ~GLOBAL_BIND;
  1106. vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
  1107. }
  1108. if (INTEL_INFO(dev)->gen >= 8) {
  1109. if (IS_CHERRYVIEW(dev))
  1110. chv_setup_private_ppat(dev_priv);
  1111. else
  1112. bdw_setup_private_ppat(dev_priv);
  1113. return;
  1114. }
  1115. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  1116. /* TODO: Perhaps it shouldn't be gen6 specific */
  1117. if (i915_is_ggtt(vm)) {
  1118. if (dev_priv->mm.aliasing_ppgtt)
  1119. gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
  1120. continue;
  1121. }
  1122. gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
  1123. }
  1124. i915_ggtt_flush(dev_priv);
  1125. }
  1126. int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
  1127. {
  1128. if (obj->has_dma_mapping)
  1129. return 0;
  1130. if (!dma_map_sg(&obj->base.dev->pdev->dev,
  1131. obj->pages->sgl, obj->pages->nents,
  1132. PCI_DMA_BIDIRECTIONAL))
  1133. return -ENOSPC;
  1134. return 0;
  1135. }
  1136. static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
  1137. {
  1138. #ifdef writeq
  1139. writeq(pte, addr);
  1140. #else
  1141. iowrite32((u32)pte, addr);
  1142. iowrite32(pte >> 32, addr + 4);
  1143. #endif
  1144. }
  1145. static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
  1146. struct sg_table *st,
  1147. uint64_t start,
  1148. enum i915_cache_level level, u32 unused)
  1149. {
  1150. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1151. unsigned first_entry = start >> PAGE_SHIFT;
  1152. gen8_gtt_pte_t __iomem *gtt_entries =
  1153. (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
  1154. int i = 0;
  1155. struct sg_page_iter sg_iter;
  1156. dma_addr_t addr = 0; /* shut up gcc */
  1157. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
  1158. addr = sg_dma_address(sg_iter.sg) +
  1159. (sg_iter.sg_pgoffset << PAGE_SHIFT);
  1160. gen8_set_pte(&gtt_entries[i],
  1161. gen8_pte_encode(addr, level, true));
  1162. i++;
  1163. }
  1164. /*
  1165. * XXX: This serves as a posting read to make sure that the PTE has
  1166. * actually been updated. There is some concern that even though
  1167. * registers and PTEs are within the same BAR that they are potentially
  1168. * of NUMA access patterns. Therefore, even with the way we assume
  1169. * hardware should work, we must keep this posting read for paranoia.
  1170. */
  1171. if (i != 0)
  1172. WARN_ON(readq(&gtt_entries[i-1])
  1173. != gen8_pte_encode(addr, level, true));
  1174. /* This next bit makes the above posting read even more important. We
  1175. * want to flush the TLBs only after we're certain all the PTE updates
  1176. * have finished.
  1177. */
  1178. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  1179. POSTING_READ(GFX_FLSH_CNTL_GEN6);
  1180. }
  1181. /*
  1182. * Binds an object into the global gtt with the specified cache level. The object
  1183. * will be accessible to the GPU via commands whose operands reference offsets
  1184. * within the global GTT as well as accessible by the GPU through the GMADR
  1185. * mapped BAR (dev_priv->mm.gtt->gtt).
  1186. */
  1187. static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
  1188. struct sg_table *st,
  1189. uint64_t start,
  1190. enum i915_cache_level level, u32 flags)
  1191. {
  1192. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1193. unsigned first_entry = start >> PAGE_SHIFT;
  1194. gen6_gtt_pte_t __iomem *gtt_entries =
  1195. (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
  1196. int i = 0;
  1197. struct sg_page_iter sg_iter;
  1198. dma_addr_t addr = 0;
  1199. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
  1200. addr = sg_page_iter_dma_address(&sg_iter);
  1201. iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
  1202. i++;
  1203. }
  1204. /* XXX: This serves as a posting read to make sure that the PTE has
  1205. * actually been updated. There is some concern that even though
  1206. * registers and PTEs are within the same BAR that they are potentially
  1207. * of NUMA access patterns. Therefore, even with the way we assume
  1208. * hardware should work, we must keep this posting read for paranoia.
  1209. */
  1210. if (i != 0) {
  1211. unsigned long gtt = readl(&gtt_entries[i-1]);
  1212. WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
  1213. }
  1214. /* This next bit makes the above posting read even more important. We
  1215. * want to flush the TLBs only after we're certain all the PTE updates
  1216. * have finished.
  1217. */
  1218. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  1219. POSTING_READ(GFX_FLSH_CNTL_GEN6);
  1220. }
  1221. static void gen8_ggtt_clear_range(struct i915_address_space *vm,
  1222. uint64_t start,
  1223. uint64_t length,
  1224. bool use_scratch)
  1225. {
  1226. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1227. unsigned first_entry = start >> PAGE_SHIFT;
  1228. unsigned num_entries = length >> PAGE_SHIFT;
  1229. gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
  1230. (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
  1231. const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
  1232. int i;
  1233. if (WARN(num_entries > max_entries,
  1234. "First entry = %d; Num entries = %d (max=%d)\n",
  1235. first_entry, num_entries, max_entries))
  1236. num_entries = max_entries;
  1237. scratch_pte = gen8_pte_encode(vm->scratch.addr,
  1238. I915_CACHE_LLC,
  1239. use_scratch);
  1240. for (i = 0; i < num_entries; i++)
  1241. gen8_set_pte(&gtt_base[i], scratch_pte);
  1242. readl(gtt_base);
  1243. }
  1244. static void gen6_ggtt_clear_range(struct i915_address_space *vm,
  1245. uint64_t start,
  1246. uint64_t length,
  1247. bool use_scratch)
  1248. {
  1249. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1250. unsigned first_entry = start >> PAGE_SHIFT;
  1251. unsigned num_entries = length >> PAGE_SHIFT;
  1252. gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
  1253. (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
  1254. const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
  1255. int i;
  1256. if (WARN(num_entries > max_entries,
  1257. "First entry = %d; Num entries = %d (max=%d)\n",
  1258. first_entry, num_entries, max_entries))
  1259. num_entries = max_entries;
  1260. scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
  1261. for (i = 0; i < num_entries; i++)
  1262. iowrite32(scratch_pte, &gtt_base[i]);
  1263. readl(gtt_base);
  1264. }
  1265. static void i915_ggtt_bind_vma(struct i915_vma *vma,
  1266. enum i915_cache_level cache_level,
  1267. u32 unused)
  1268. {
  1269. const unsigned long entry = vma->node.start >> PAGE_SHIFT;
  1270. unsigned int flags = (cache_level == I915_CACHE_NONE) ?
  1271. AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
  1272. BUG_ON(!i915_is_ggtt(vma->vm));
  1273. intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
  1274. vma->bound = GLOBAL_BIND;
  1275. }
  1276. static void i915_ggtt_clear_range(struct i915_address_space *vm,
  1277. uint64_t start,
  1278. uint64_t length,
  1279. bool unused)
  1280. {
  1281. unsigned first_entry = start >> PAGE_SHIFT;
  1282. unsigned num_entries = length >> PAGE_SHIFT;
  1283. intel_gtt_clear_range(first_entry, num_entries);
  1284. }
  1285. static void i915_ggtt_unbind_vma(struct i915_vma *vma)
  1286. {
  1287. const unsigned int first = vma->node.start >> PAGE_SHIFT;
  1288. const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
  1289. BUG_ON(!i915_is_ggtt(vma->vm));
  1290. vma->bound = 0;
  1291. intel_gtt_clear_range(first, size);
  1292. }
  1293. static void ggtt_bind_vma(struct i915_vma *vma,
  1294. enum i915_cache_level cache_level,
  1295. u32 flags)
  1296. {
  1297. struct drm_device *dev = vma->vm->dev;
  1298. struct drm_i915_private *dev_priv = dev->dev_private;
  1299. struct drm_i915_gem_object *obj = vma->obj;
  1300. /* Currently applicable only to VLV */
  1301. if (obj->gt_ro)
  1302. flags |= PTE_READ_ONLY;
  1303. /* If there is no aliasing PPGTT, or the caller needs a global mapping,
  1304. * or we have a global mapping already but the cacheability flags have
  1305. * changed, set the global PTEs.
  1306. *
  1307. * If there is an aliasing PPGTT it is anecdotally faster, so use that
  1308. * instead if none of the above hold true.
  1309. *
  1310. * NB: A global mapping should only be needed for special regions like
  1311. * "gtt mappable", SNB errata, or if specified via special execbuf
  1312. * flags. At all other times, the GPU will use the aliasing PPGTT.
  1313. */
  1314. if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
  1315. if (!(vma->bound & GLOBAL_BIND) ||
  1316. (cache_level != obj->cache_level)) {
  1317. vma->vm->insert_entries(vma->vm, obj->pages,
  1318. vma->node.start,
  1319. cache_level, flags);
  1320. vma->bound |= GLOBAL_BIND;
  1321. }
  1322. }
  1323. if (dev_priv->mm.aliasing_ppgtt &&
  1324. (!(vma->bound & LOCAL_BIND) ||
  1325. (cache_level != obj->cache_level))) {
  1326. struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
  1327. appgtt->base.insert_entries(&appgtt->base,
  1328. vma->obj->pages,
  1329. vma->node.start,
  1330. cache_level, flags);
  1331. vma->bound |= LOCAL_BIND;
  1332. }
  1333. }
  1334. static void ggtt_unbind_vma(struct i915_vma *vma)
  1335. {
  1336. struct drm_device *dev = vma->vm->dev;
  1337. struct drm_i915_private *dev_priv = dev->dev_private;
  1338. struct drm_i915_gem_object *obj = vma->obj;
  1339. if (vma->bound & GLOBAL_BIND) {
  1340. vma->vm->clear_range(vma->vm,
  1341. vma->node.start,
  1342. obj->base.size,
  1343. true);
  1344. vma->bound &= ~GLOBAL_BIND;
  1345. }
  1346. if (vma->bound & LOCAL_BIND) {
  1347. struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
  1348. appgtt->base.clear_range(&appgtt->base,
  1349. vma->node.start,
  1350. obj->base.size,
  1351. true);
  1352. vma->bound &= ~LOCAL_BIND;
  1353. }
  1354. }
  1355. void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
  1356. {
  1357. struct drm_device *dev = obj->base.dev;
  1358. struct drm_i915_private *dev_priv = dev->dev_private;
  1359. bool interruptible;
  1360. interruptible = do_idling(dev_priv);
  1361. if (!obj->has_dma_mapping)
  1362. dma_unmap_sg(&dev->pdev->dev,
  1363. obj->pages->sgl, obj->pages->nents,
  1364. PCI_DMA_BIDIRECTIONAL);
  1365. undo_idling(dev_priv, interruptible);
  1366. }
  1367. static void i915_gtt_color_adjust(struct drm_mm_node *node,
  1368. unsigned long color,
  1369. unsigned long *start,
  1370. unsigned long *end)
  1371. {
  1372. if (node->color != color)
  1373. *start += 4096;
  1374. if (!list_empty(&node->node_list)) {
  1375. node = list_entry(node->node_list.next,
  1376. struct drm_mm_node,
  1377. node_list);
  1378. if (node->allocated && node->color != color)
  1379. *end -= 4096;
  1380. }
  1381. }
  1382. static int i915_gem_setup_global_gtt(struct drm_device *dev,
  1383. unsigned long start,
  1384. unsigned long mappable_end,
  1385. unsigned long end)
  1386. {
  1387. /* Let GEM Manage all of the aperture.
  1388. *
  1389. * However, leave one page at the end still bound to the scratch page.
  1390. * There are a number of places where the hardware apparently prefetches
  1391. * past the end of the object, and we've seen multiple hangs with the
  1392. * GPU head pointer stuck in a batchbuffer bound at the last page of the
  1393. * aperture. One page should be enough to keep any prefetching inside
  1394. * of the aperture.
  1395. */
  1396. struct drm_i915_private *dev_priv = dev->dev_private;
  1397. struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
  1398. struct drm_mm_node *entry;
  1399. struct drm_i915_gem_object *obj;
  1400. unsigned long hole_start, hole_end;
  1401. int ret;
  1402. BUG_ON(mappable_end > end);
  1403. /* Subtract the guard page ... */
  1404. drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
  1405. if (!HAS_LLC(dev))
  1406. dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
  1407. /* Mark any preallocated objects as occupied */
  1408. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  1409. struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
  1410. DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
  1411. i915_gem_obj_ggtt_offset(obj), obj->base.size);
  1412. WARN_ON(i915_gem_obj_ggtt_bound(obj));
  1413. ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
  1414. if (ret) {
  1415. DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
  1416. return ret;
  1417. }
  1418. vma->bound |= GLOBAL_BIND;
  1419. }
  1420. dev_priv->gtt.base.start = start;
  1421. dev_priv->gtt.base.total = end - start;
  1422. /* Clear any non-preallocated blocks */
  1423. drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
  1424. DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
  1425. hole_start, hole_end);
  1426. ggtt_vm->clear_range(ggtt_vm, hole_start,
  1427. hole_end - hole_start, true);
  1428. }
  1429. /* And finally clear the reserved guard page */
  1430. ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
  1431. if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
  1432. struct i915_hw_ppgtt *ppgtt;
  1433. ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
  1434. if (!ppgtt)
  1435. return -ENOMEM;
  1436. ret = __hw_ppgtt_init(dev, ppgtt);
  1437. if (ret != 0)
  1438. return ret;
  1439. dev_priv->mm.aliasing_ppgtt = ppgtt;
  1440. }
  1441. return 0;
  1442. }
  1443. void i915_gem_init_global_gtt(struct drm_device *dev)
  1444. {
  1445. struct drm_i915_private *dev_priv = dev->dev_private;
  1446. unsigned long gtt_size, mappable_size;
  1447. gtt_size = dev_priv->gtt.base.total;
  1448. mappable_size = dev_priv->gtt.mappable_end;
  1449. i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
  1450. }
  1451. void i915_global_gtt_cleanup(struct drm_device *dev)
  1452. {
  1453. struct drm_i915_private *dev_priv = dev->dev_private;
  1454. struct i915_address_space *vm = &dev_priv->gtt.base;
  1455. if (dev_priv->mm.aliasing_ppgtt) {
  1456. struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
  1457. ppgtt->base.cleanup(&ppgtt->base);
  1458. }
  1459. if (drm_mm_initialized(&vm->mm)) {
  1460. drm_mm_takedown(&vm->mm);
  1461. list_del(&vm->global_link);
  1462. }
  1463. vm->cleanup(vm);
  1464. }
  1465. static int setup_scratch_page(struct drm_device *dev)
  1466. {
  1467. struct drm_i915_private *dev_priv = dev->dev_private;
  1468. struct page *page;
  1469. dma_addr_t dma_addr;
  1470. page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
  1471. if (page == NULL)
  1472. return -ENOMEM;
  1473. set_pages_uc(page, 1);
  1474. #ifdef CONFIG_INTEL_IOMMU
  1475. dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
  1476. PCI_DMA_BIDIRECTIONAL);
  1477. if (pci_dma_mapping_error(dev->pdev, dma_addr))
  1478. return -EINVAL;
  1479. #else
  1480. dma_addr = page_to_phys(page);
  1481. #endif
  1482. dev_priv->gtt.base.scratch.page = page;
  1483. dev_priv->gtt.base.scratch.addr = dma_addr;
  1484. return 0;
  1485. }
  1486. static void teardown_scratch_page(struct drm_device *dev)
  1487. {
  1488. struct drm_i915_private *dev_priv = dev->dev_private;
  1489. struct page *page = dev_priv->gtt.base.scratch.page;
  1490. set_pages_wb(page, 1);
  1491. pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
  1492. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  1493. __free_page(page);
  1494. }
  1495. static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
  1496. {
  1497. snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
  1498. snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
  1499. return snb_gmch_ctl << 20;
  1500. }
  1501. static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
  1502. {
  1503. bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
  1504. bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
  1505. if (bdw_gmch_ctl)
  1506. bdw_gmch_ctl = 1 << bdw_gmch_ctl;
  1507. #ifdef CONFIG_X86_32
  1508. /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
  1509. if (bdw_gmch_ctl > 4)
  1510. bdw_gmch_ctl = 4;
  1511. #endif
  1512. return bdw_gmch_ctl << 20;
  1513. }
  1514. static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
  1515. {
  1516. gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
  1517. gmch_ctrl &= SNB_GMCH_GGMS_MASK;
  1518. if (gmch_ctrl)
  1519. return 1 << (20 + gmch_ctrl);
  1520. return 0;
  1521. }
  1522. static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
  1523. {
  1524. snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
  1525. snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
  1526. return snb_gmch_ctl << 25; /* 32 MB units */
  1527. }
  1528. static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
  1529. {
  1530. bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  1531. bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
  1532. return bdw_gmch_ctl << 25; /* 32 MB units */
  1533. }
  1534. static size_t chv_get_stolen_size(u16 gmch_ctrl)
  1535. {
  1536. gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
  1537. gmch_ctrl &= SNB_GMCH_GMS_MASK;
  1538. /*
  1539. * 0x0 to 0x10: 32MB increments starting at 0MB
  1540. * 0x11 to 0x16: 4MB increments starting at 8MB
  1541. * 0x17 to 0x1d: 4MB increments start at 36MB
  1542. */
  1543. if (gmch_ctrl < 0x11)
  1544. return gmch_ctrl << 25;
  1545. else if (gmch_ctrl < 0x17)
  1546. return (gmch_ctrl - 0x11 + 2) << 22;
  1547. else
  1548. return (gmch_ctrl - 0x17 + 9) << 22;
  1549. }
  1550. static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
  1551. {
  1552. gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  1553. gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
  1554. if (gen9_gmch_ctl < 0xf0)
  1555. return gen9_gmch_ctl << 25; /* 32 MB units */
  1556. else
  1557. /* 4MB increments starting at 0xf0 for 4MB */
  1558. return (gen9_gmch_ctl - 0xf0 + 1) << 22;
  1559. }
  1560. static int ggtt_probe_common(struct drm_device *dev,
  1561. size_t gtt_size)
  1562. {
  1563. struct drm_i915_private *dev_priv = dev->dev_private;
  1564. phys_addr_t gtt_phys_addr;
  1565. int ret;
  1566. /* For Modern GENs the PTEs and register space are split in the BAR */
  1567. gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
  1568. (pci_resource_len(dev->pdev, 0) / 2);
  1569. dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
  1570. if (!dev_priv->gtt.gsm) {
  1571. DRM_ERROR("Failed to map the gtt page table\n");
  1572. return -ENOMEM;
  1573. }
  1574. ret = setup_scratch_page(dev);
  1575. if (ret) {
  1576. DRM_ERROR("Scratch setup failed\n");
  1577. /* iounmap will also get called at remove, but meh */
  1578. iounmap(dev_priv->gtt.gsm);
  1579. }
  1580. return ret;
  1581. }
  1582. /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
  1583. * bits. When using advanced contexts each context stores its own PAT, but
  1584. * writing this data shouldn't be harmful even in those cases. */
  1585. static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
  1586. {
  1587. uint64_t pat;
  1588. pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
  1589. GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
  1590. GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
  1591. GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
  1592. GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
  1593. GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
  1594. GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
  1595. GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
  1596. if (!USES_PPGTT(dev_priv->dev))
  1597. /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
  1598. * so RTL will always use the value corresponding to
  1599. * pat_sel = 000".
  1600. * So let's disable cache for GGTT to avoid screen corruptions.
  1601. * MOCS still can be used though.
  1602. * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
  1603. * before this patch, i.e. the same uncached + snooping access
  1604. * like on gen6/7 seems to be in effect.
  1605. * - So this just fixes blitter/render access. Again it looks
  1606. * like it's not just uncached access, but uncached + snooping.
  1607. * So we can still hold onto all our assumptions wrt cpu
  1608. * clflushing on LLC machines.
  1609. */
  1610. pat = GEN8_PPAT(0, GEN8_PPAT_UC);
  1611. /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
  1612. * write would work. */
  1613. I915_WRITE(GEN8_PRIVATE_PAT, pat);
  1614. I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
  1615. }
  1616. static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
  1617. {
  1618. uint64_t pat;
  1619. /*
  1620. * Map WB on BDW to snooped on CHV.
  1621. *
  1622. * Only the snoop bit has meaning for CHV, the rest is
  1623. * ignored.
  1624. *
  1625. * The hardware will never snoop for certain types of accesses:
  1626. * - CPU GTT (GMADR->GGTT->no snoop->memory)
  1627. * - PPGTT page tables
  1628. * - some other special cycles
  1629. *
  1630. * As with BDW, we also need to consider the following for GT accesses:
  1631. * "For GGTT, there is NO pat_sel[2:0] from the entry,
  1632. * so RTL will always use the value corresponding to
  1633. * pat_sel = 000".
  1634. * Which means we must set the snoop bit in PAT entry 0
  1635. * in order to keep the global status page working.
  1636. */
  1637. pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
  1638. GEN8_PPAT(1, 0) |
  1639. GEN8_PPAT(2, 0) |
  1640. GEN8_PPAT(3, 0) |
  1641. GEN8_PPAT(4, CHV_PPAT_SNOOP) |
  1642. GEN8_PPAT(5, CHV_PPAT_SNOOP) |
  1643. GEN8_PPAT(6, CHV_PPAT_SNOOP) |
  1644. GEN8_PPAT(7, CHV_PPAT_SNOOP);
  1645. I915_WRITE(GEN8_PRIVATE_PAT, pat);
  1646. I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
  1647. }
  1648. static int gen8_gmch_probe(struct drm_device *dev,
  1649. size_t *gtt_total,
  1650. size_t *stolen,
  1651. phys_addr_t *mappable_base,
  1652. unsigned long *mappable_end)
  1653. {
  1654. struct drm_i915_private *dev_priv = dev->dev_private;
  1655. unsigned int gtt_size;
  1656. u16 snb_gmch_ctl;
  1657. int ret;
  1658. /* TODO: We're not aware of mappable constraints on gen8 yet */
  1659. *mappable_base = pci_resource_start(dev->pdev, 2);
  1660. *mappable_end = pci_resource_len(dev->pdev, 2);
  1661. if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
  1662. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
  1663. pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  1664. if (INTEL_INFO(dev)->gen >= 9) {
  1665. *stolen = gen9_get_stolen_size(snb_gmch_ctl);
  1666. gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
  1667. } else if (IS_CHERRYVIEW(dev)) {
  1668. *stolen = chv_get_stolen_size(snb_gmch_ctl);
  1669. gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
  1670. } else {
  1671. *stolen = gen8_get_stolen_size(snb_gmch_ctl);
  1672. gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
  1673. }
  1674. *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
  1675. if (IS_CHERRYVIEW(dev))
  1676. chv_setup_private_ppat(dev_priv);
  1677. else
  1678. bdw_setup_private_ppat(dev_priv);
  1679. ret = ggtt_probe_common(dev, gtt_size);
  1680. dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
  1681. dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
  1682. return ret;
  1683. }
  1684. static int gen6_gmch_probe(struct drm_device *dev,
  1685. size_t *gtt_total,
  1686. size_t *stolen,
  1687. phys_addr_t *mappable_base,
  1688. unsigned long *mappable_end)
  1689. {
  1690. struct drm_i915_private *dev_priv = dev->dev_private;
  1691. unsigned int gtt_size;
  1692. u16 snb_gmch_ctl;
  1693. int ret;
  1694. *mappable_base = pci_resource_start(dev->pdev, 2);
  1695. *mappable_end = pci_resource_len(dev->pdev, 2);
  1696. /* 64/512MB is the current min/max we actually know of, but this is just
  1697. * a coarse sanity check.
  1698. */
  1699. if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
  1700. DRM_ERROR("Unknown GMADR size (%lx)\n",
  1701. dev_priv->gtt.mappable_end);
  1702. return -ENXIO;
  1703. }
  1704. if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
  1705. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
  1706. pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  1707. *stolen = gen6_get_stolen_size(snb_gmch_ctl);
  1708. gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
  1709. *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
  1710. ret = ggtt_probe_common(dev, gtt_size);
  1711. dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
  1712. dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
  1713. return ret;
  1714. }
  1715. static void gen6_gmch_remove(struct i915_address_space *vm)
  1716. {
  1717. struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
  1718. iounmap(gtt->gsm);
  1719. teardown_scratch_page(vm->dev);
  1720. }
  1721. static int i915_gmch_probe(struct drm_device *dev,
  1722. size_t *gtt_total,
  1723. size_t *stolen,
  1724. phys_addr_t *mappable_base,
  1725. unsigned long *mappable_end)
  1726. {
  1727. struct drm_i915_private *dev_priv = dev->dev_private;
  1728. int ret;
  1729. ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
  1730. if (!ret) {
  1731. DRM_ERROR("failed to set up gmch\n");
  1732. return -EIO;
  1733. }
  1734. intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
  1735. dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
  1736. dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
  1737. if (unlikely(dev_priv->gtt.do_idle_maps))
  1738. DRM_INFO("applying Ironlake quirks for intel_iommu\n");
  1739. return 0;
  1740. }
  1741. static void i915_gmch_remove(struct i915_address_space *vm)
  1742. {
  1743. intel_gmch_remove();
  1744. }
  1745. int i915_gem_gtt_init(struct drm_device *dev)
  1746. {
  1747. struct drm_i915_private *dev_priv = dev->dev_private;
  1748. struct i915_gtt *gtt = &dev_priv->gtt;
  1749. int ret;
  1750. if (INTEL_INFO(dev)->gen <= 5) {
  1751. gtt->gtt_probe = i915_gmch_probe;
  1752. gtt->base.cleanup = i915_gmch_remove;
  1753. } else if (INTEL_INFO(dev)->gen < 8) {
  1754. gtt->gtt_probe = gen6_gmch_probe;
  1755. gtt->base.cleanup = gen6_gmch_remove;
  1756. if (IS_HASWELL(dev) && dev_priv->ellc_size)
  1757. gtt->base.pte_encode = iris_pte_encode;
  1758. else if (IS_HASWELL(dev))
  1759. gtt->base.pte_encode = hsw_pte_encode;
  1760. else if (IS_VALLEYVIEW(dev))
  1761. gtt->base.pte_encode = byt_pte_encode;
  1762. else if (INTEL_INFO(dev)->gen >= 7)
  1763. gtt->base.pte_encode = ivb_pte_encode;
  1764. else
  1765. gtt->base.pte_encode = snb_pte_encode;
  1766. } else {
  1767. dev_priv->gtt.gtt_probe = gen8_gmch_probe;
  1768. dev_priv->gtt.base.cleanup = gen6_gmch_remove;
  1769. }
  1770. ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
  1771. &gtt->mappable_base, &gtt->mappable_end);
  1772. if (ret)
  1773. return ret;
  1774. gtt->base.dev = dev;
  1775. /* GMADR is the PCI mmio aperture into the global GTT. */
  1776. DRM_INFO("Memory usable by graphics device = %zdM\n",
  1777. gtt->base.total >> 20);
  1778. DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
  1779. DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
  1780. #ifdef CONFIG_INTEL_IOMMU
  1781. if (intel_iommu_gfx_mapped)
  1782. DRM_INFO("VT-d active for gfx access\n");
  1783. #endif
  1784. /*
  1785. * i915.enable_ppgtt is read-only, so do an early pass to validate the
  1786. * user's requested state against the hardware/driver capabilities. We
  1787. * do this now so that we can print out any log messages once rather
  1788. * than every time we check intel_enable_ppgtt().
  1789. */
  1790. i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
  1791. DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
  1792. return 0;
  1793. }
  1794. static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
  1795. struct i915_address_space *vm)
  1796. {
  1797. struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
  1798. if (vma == NULL)
  1799. return ERR_PTR(-ENOMEM);
  1800. INIT_LIST_HEAD(&vma->vma_link);
  1801. INIT_LIST_HEAD(&vma->mm_list);
  1802. INIT_LIST_HEAD(&vma->exec_list);
  1803. vma->vm = vm;
  1804. vma->obj = obj;
  1805. switch (INTEL_INFO(vm->dev)->gen) {
  1806. case 9:
  1807. case 8:
  1808. case 7:
  1809. case 6:
  1810. if (i915_is_ggtt(vm)) {
  1811. vma->unbind_vma = ggtt_unbind_vma;
  1812. vma->bind_vma = ggtt_bind_vma;
  1813. } else {
  1814. vma->unbind_vma = ppgtt_unbind_vma;
  1815. vma->bind_vma = ppgtt_bind_vma;
  1816. }
  1817. break;
  1818. case 5:
  1819. case 4:
  1820. case 3:
  1821. case 2:
  1822. BUG_ON(!i915_is_ggtt(vm));
  1823. vma->unbind_vma = i915_ggtt_unbind_vma;
  1824. vma->bind_vma = i915_ggtt_bind_vma;
  1825. break;
  1826. default:
  1827. BUG();
  1828. }
  1829. /* Keep GGTT vmas first to make debug easier */
  1830. if (i915_is_ggtt(vm))
  1831. list_add(&vma->vma_link, &obj->vma_list);
  1832. else {
  1833. list_add_tail(&vma->vma_link, &obj->vma_list);
  1834. i915_ppgtt_get(i915_vm_to_ppgtt(vm));
  1835. }
  1836. return vma;
  1837. }
  1838. struct i915_vma *
  1839. i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
  1840. struct i915_address_space *vm)
  1841. {
  1842. struct i915_vma *vma;
  1843. vma = i915_gem_obj_to_vma(obj, vm);
  1844. if (!vma)
  1845. vma = __i915_gem_vma_create(obj, vm);
  1846. return vma;
  1847. }