i915_gem_gtt.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968
  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_vgpu.h"
  30. #include "i915_trace.h"
  31. #include "intel_drv.h"
  32. /**
  33. * DOC: Global GTT views
  34. *
  35. * Background and previous state
  36. *
  37. * Historically objects could exists (be bound) in global GTT space only as
  38. * singular instances with a view representing all of the object's backing pages
  39. * in a linear fashion. This view will be called a normal view.
  40. *
  41. * To support multiple views of the same object, where the number of mapped
  42. * pages is not equal to the backing store, or where the layout of the pages
  43. * is not linear, concept of a GGTT view was added.
  44. *
  45. * One example of an alternative view is a stereo display driven by a single
  46. * image. In this case we would have a framebuffer looking like this
  47. * (2x2 pages):
  48. *
  49. * 12
  50. * 34
  51. *
  52. * Above would represent a normal GGTT view as normally mapped for GPU or CPU
  53. * rendering. In contrast, fed to the display engine would be an alternative
  54. * view which could look something like this:
  55. *
  56. * 1212
  57. * 3434
  58. *
  59. * In this example both the size and layout of pages in the alternative view is
  60. * different from the normal view.
  61. *
  62. * Implementation and usage
  63. *
  64. * GGTT views are implemented using VMAs and are distinguished via enum
  65. * i915_ggtt_view_type and struct i915_ggtt_view.
  66. *
  67. * A new flavour of core GEM functions which work with GGTT bound objects were
  68. * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
  69. * renaming in large amounts of code. They take the struct i915_ggtt_view
  70. * parameter encapsulating all metadata required to implement a view.
  71. *
  72. * As a helper for callers which are only interested in the normal view,
  73. * globally const i915_ggtt_view_normal singleton instance exists. All old core
  74. * GEM API functions, the ones not taking the view parameter, are operating on,
  75. * or with the normal GGTT view.
  76. *
  77. * Code wanting to add or use a new GGTT view needs to:
  78. *
  79. * 1. Add a new enum with a suitable name.
  80. * 2. Extend the metadata in the i915_ggtt_view structure if required.
  81. * 3. Add support to i915_get_vma_pages().
  82. *
  83. * New views are required to build a scatter-gather table from within the
  84. * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
  85. * exists for the lifetime of an VMA.
  86. *
  87. * Core API is designed to have copy semantics which means that passed in
  88. * struct i915_ggtt_view does not need to be persistent (left around after
  89. * calling the core API functions).
  90. *
  91. */
  92. static int
  93. i915_get_ggtt_vma_pages(struct i915_vma *vma);
  94. const struct i915_ggtt_view i915_ggtt_view_normal;
  95. const struct i915_ggtt_view i915_ggtt_view_rotated = {
  96. .type = I915_GGTT_VIEW_ROTATED
  97. };
  98. static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
  99. {
  100. bool has_aliasing_ppgtt;
  101. bool has_full_ppgtt;
  102. has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
  103. has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
  104. if (intel_vgpu_active(dev))
  105. has_full_ppgtt = false; /* emulation is too hard */
  106. /*
  107. * We don't allow disabling PPGTT for gen9+ as it's a requirement for
  108. * execlists, the sole mechanism available to submit work.
  109. */
  110. if (INTEL_INFO(dev)->gen < 9 &&
  111. (enable_ppgtt == 0 || !has_aliasing_ppgtt))
  112. return 0;
  113. if (enable_ppgtt == 1)
  114. return 1;
  115. if (enable_ppgtt == 2 && has_full_ppgtt)
  116. return 2;
  117. #ifdef CONFIG_INTEL_IOMMU
  118. /* Disable ppgtt on SNB if VT-d is on. */
  119. if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
  120. DRM_INFO("Disabling PPGTT because VT-d is on\n");
  121. return 0;
  122. }
  123. #endif
  124. /* Early VLV doesn't have this */
  125. if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  126. dev->pdev->revision < 0xb) {
  127. DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
  128. return 0;
  129. }
  130. if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists)
  131. return 2;
  132. else
  133. return has_aliasing_ppgtt ? 1 : 0;
  134. }
  135. static int ppgtt_bind_vma(struct i915_vma *vma,
  136. enum i915_cache_level cache_level,
  137. u32 unused)
  138. {
  139. u32 pte_flags = 0;
  140. /* Currently applicable only to VLV */
  141. if (vma->obj->gt_ro)
  142. pte_flags |= PTE_READ_ONLY;
  143. vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
  144. cache_level, pte_flags);
  145. return 0;
  146. }
  147. static void ppgtt_unbind_vma(struct i915_vma *vma)
  148. {
  149. vma->vm->clear_range(vma->vm,
  150. vma->node.start,
  151. vma->obj->base.size,
  152. true);
  153. }
  154. static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
  155. enum i915_cache_level level,
  156. bool valid)
  157. {
  158. gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
  159. pte |= addr;
  160. switch (level) {
  161. case I915_CACHE_NONE:
  162. pte |= PPAT_UNCACHED_INDEX;
  163. break;
  164. case I915_CACHE_WT:
  165. pte |= PPAT_DISPLAY_ELLC_INDEX;
  166. break;
  167. default:
  168. pte |= PPAT_CACHED_INDEX;
  169. break;
  170. }
  171. return pte;
  172. }
  173. static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
  174. const enum i915_cache_level level)
  175. {
  176. gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
  177. pde |= addr;
  178. if (level != I915_CACHE_NONE)
  179. pde |= PPAT_CACHED_PDE_INDEX;
  180. else
  181. pde |= PPAT_UNCACHED_INDEX;
  182. return pde;
  183. }
  184. static gen6_pte_t snb_pte_encode(dma_addr_t addr,
  185. enum i915_cache_level level,
  186. bool valid, u32 unused)
  187. {
  188. gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  189. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  190. switch (level) {
  191. case I915_CACHE_L3_LLC:
  192. case I915_CACHE_LLC:
  193. pte |= GEN6_PTE_CACHE_LLC;
  194. break;
  195. case I915_CACHE_NONE:
  196. pte |= GEN6_PTE_UNCACHED;
  197. break;
  198. default:
  199. MISSING_CASE(level);
  200. }
  201. return pte;
  202. }
  203. static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
  204. enum i915_cache_level level,
  205. bool valid, u32 unused)
  206. {
  207. gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  208. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  209. switch (level) {
  210. case I915_CACHE_L3_LLC:
  211. pte |= GEN7_PTE_CACHE_L3_LLC;
  212. break;
  213. case I915_CACHE_LLC:
  214. pte |= GEN6_PTE_CACHE_LLC;
  215. break;
  216. case I915_CACHE_NONE:
  217. pte |= GEN6_PTE_UNCACHED;
  218. break;
  219. default:
  220. MISSING_CASE(level);
  221. }
  222. return pte;
  223. }
  224. static gen6_pte_t byt_pte_encode(dma_addr_t addr,
  225. enum i915_cache_level level,
  226. bool valid, u32 flags)
  227. {
  228. gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  229. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  230. if (!(flags & PTE_READ_ONLY))
  231. pte |= BYT_PTE_WRITEABLE;
  232. if (level != I915_CACHE_NONE)
  233. pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
  234. return pte;
  235. }
  236. static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
  237. enum i915_cache_level level,
  238. bool valid, u32 unused)
  239. {
  240. gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  241. pte |= HSW_PTE_ADDR_ENCODE(addr);
  242. if (level != I915_CACHE_NONE)
  243. pte |= HSW_WB_LLC_AGE3;
  244. return pte;
  245. }
  246. static gen6_pte_t iris_pte_encode(dma_addr_t addr,
  247. enum i915_cache_level level,
  248. bool valid, u32 unused)
  249. {
  250. gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  251. pte |= HSW_PTE_ADDR_ENCODE(addr);
  252. switch (level) {
  253. case I915_CACHE_NONE:
  254. break;
  255. case I915_CACHE_WT:
  256. pte |= HSW_WT_ELLC_LLC_AGE3;
  257. break;
  258. default:
  259. pte |= HSW_WB_ELLC_LLC_AGE3;
  260. break;
  261. }
  262. return pte;
  263. }
  264. static int __setup_page_dma(struct drm_device *dev,
  265. struct i915_page_dma *p, gfp_t flags)
  266. {
  267. struct device *device = &dev->pdev->dev;
  268. p->page = alloc_page(flags);
  269. if (!p->page)
  270. return -ENOMEM;
  271. p->daddr = dma_map_page(device,
  272. p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
  273. if (dma_mapping_error(device, p->daddr)) {
  274. __free_page(p->page);
  275. return -EINVAL;
  276. }
  277. return 0;
  278. }
  279. static int setup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
  280. {
  281. return __setup_page_dma(dev, p, GFP_KERNEL);
  282. }
  283. static void cleanup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
  284. {
  285. if (WARN_ON(!p->page))
  286. return;
  287. dma_unmap_page(&dev->pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
  288. __free_page(p->page);
  289. memset(p, 0, sizeof(*p));
  290. }
  291. static void *kmap_page_dma(struct i915_page_dma *p)
  292. {
  293. return kmap_atomic(p->page);
  294. }
  295. /* We use the flushing unmap only with ppgtt structures:
  296. * page directories, page tables and scratch pages.
  297. */
  298. static void kunmap_page_dma(struct drm_device *dev, void *vaddr)
  299. {
  300. /* There are only few exceptions for gen >=6. chv and bxt.
  301. * And we are not sure about the latter so play safe for now.
  302. */
  303. if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
  304. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  305. kunmap_atomic(vaddr);
  306. }
  307. #define kmap_px(px) kmap_page_dma(px_base(px))
  308. #define kunmap_px(ppgtt, vaddr) kunmap_page_dma((ppgtt)->base.dev, (vaddr))
  309. #define setup_px(dev, px) setup_page_dma((dev), px_base(px))
  310. #define cleanup_px(dev, px) cleanup_page_dma((dev), px_base(px))
  311. #define fill_px(dev, px, v) fill_page_dma((dev), px_base(px), (v))
  312. #define fill32_px(dev, px, v) fill_page_dma_32((dev), px_base(px), (v))
  313. static void fill_page_dma(struct drm_device *dev, struct i915_page_dma *p,
  314. const uint64_t val)
  315. {
  316. int i;
  317. uint64_t * const vaddr = kmap_page_dma(p);
  318. for (i = 0; i < 512; i++)
  319. vaddr[i] = val;
  320. kunmap_page_dma(dev, vaddr);
  321. }
  322. static void fill_page_dma_32(struct drm_device *dev, struct i915_page_dma *p,
  323. const uint32_t val32)
  324. {
  325. uint64_t v = val32;
  326. v = v << 32 | val32;
  327. fill_page_dma(dev, p, v);
  328. }
  329. static struct i915_page_scratch *alloc_scratch_page(struct drm_device *dev)
  330. {
  331. struct i915_page_scratch *sp;
  332. int ret;
  333. sp = kzalloc(sizeof(*sp), GFP_KERNEL);
  334. if (sp == NULL)
  335. return ERR_PTR(-ENOMEM);
  336. ret = __setup_page_dma(dev, px_base(sp), GFP_DMA32 | __GFP_ZERO);
  337. if (ret) {
  338. kfree(sp);
  339. return ERR_PTR(ret);
  340. }
  341. set_pages_uc(px_page(sp), 1);
  342. return sp;
  343. }
  344. static void free_scratch_page(struct drm_device *dev,
  345. struct i915_page_scratch *sp)
  346. {
  347. set_pages_wb(px_page(sp), 1);
  348. cleanup_px(dev, sp);
  349. kfree(sp);
  350. }
  351. static struct i915_page_table *alloc_pt(struct drm_device *dev)
  352. {
  353. struct i915_page_table *pt;
  354. const size_t count = INTEL_INFO(dev)->gen >= 8 ?
  355. GEN8_PTES : GEN6_PTES;
  356. int ret = -ENOMEM;
  357. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  358. if (!pt)
  359. return ERR_PTR(-ENOMEM);
  360. pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
  361. GFP_KERNEL);
  362. if (!pt->used_ptes)
  363. goto fail_bitmap;
  364. ret = setup_px(dev, pt);
  365. if (ret)
  366. goto fail_page_m;
  367. return pt;
  368. fail_page_m:
  369. kfree(pt->used_ptes);
  370. fail_bitmap:
  371. kfree(pt);
  372. return ERR_PTR(ret);
  373. }
  374. static void free_pt(struct drm_device *dev, struct i915_page_table *pt)
  375. {
  376. cleanup_px(dev, pt);
  377. kfree(pt->used_ptes);
  378. kfree(pt);
  379. }
  380. static void gen8_initialize_pt(struct i915_address_space *vm,
  381. struct i915_page_table *pt)
  382. {
  383. gen8_pte_t scratch_pte;
  384. scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
  385. I915_CACHE_LLC, true);
  386. fill_px(vm->dev, pt, scratch_pte);
  387. }
  388. static void gen6_initialize_pt(struct i915_address_space *vm,
  389. struct i915_page_table *pt)
  390. {
  391. gen6_pte_t scratch_pte;
  392. WARN_ON(px_dma(vm->scratch_page) == 0);
  393. scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
  394. I915_CACHE_LLC, true, 0);
  395. fill32_px(vm->dev, pt, scratch_pte);
  396. }
  397. static struct i915_page_directory *alloc_pd(struct drm_device *dev)
  398. {
  399. struct i915_page_directory *pd;
  400. int ret = -ENOMEM;
  401. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  402. if (!pd)
  403. return ERR_PTR(-ENOMEM);
  404. pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
  405. sizeof(*pd->used_pdes), GFP_KERNEL);
  406. if (!pd->used_pdes)
  407. goto fail_bitmap;
  408. ret = setup_px(dev, pd);
  409. if (ret)
  410. goto fail_page_m;
  411. return pd;
  412. fail_page_m:
  413. kfree(pd->used_pdes);
  414. fail_bitmap:
  415. kfree(pd);
  416. return ERR_PTR(ret);
  417. }
  418. static void free_pd(struct drm_device *dev, struct i915_page_directory *pd)
  419. {
  420. if (px_page(pd)) {
  421. cleanup_px(dev, pd);
  422. kfree(pd->used_pdes);
  423. kfree(pd);
  424. }
  425. }
  426. static void gen8_initialize_pd(struct i915_address_space *vm,
  427. struct i915_page_directory *pd)
  428. {
  429. gen8_pde_t scratch_pde;
  430. scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
  431. fill_px(vm->dev, pd, scratch_pde);
  432. }
  433. /* Broadwell Page Directory Pointer Descriptors */
  434. static int gen8_write_pdp(struct drm_i915_gem_request *req,
  435. unsigned entry,
  436. dma_addr_t addr)
  437. {
  438. struct intel_engine_cs *ring = req->ring;
  439. int ret;
  440. BUG_ON(entry >= 4);
  441. ret = intel_ring_begin(req, 6);
  442. if (ret)
  443. return ret;
  444. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  445. intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
  446. intel_ring_emit(ring, upper_32_bits(addr));
  447. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  448. intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
  449. intel_ring_emit(ring, lower_32_bits(addr));
  450. intel_ring_advance(ring);
  451. return 0;
  452. }
  453. static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
  454. struct drm_i915_gem_request *req)
  455. {
  456. int i, ret;
  457. for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
  458. const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
  459. ret = gen8_write_pdp(req, i, pd_daddr);
  460. if (ret)
  461. return ret;
  462. }
  463. return 0;
  464. }
  465. static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
  466. uint64_t start,
  467. uint64_t length,
  468. bool use_scratch)
  469. {
  470. struct i915_hw_ppgtt *ppgtt =
  471. container_of(vm, struct i915_hw_ppgtt, base);
  472. gen8_pte_t *pt_vaddr, scratch_pte;
  473. unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
  474. unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
  475. unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
  476. unsigned num_entries = length >> PAGE_SHIFT;
  477. unsigned last_pte, i;
  478. scratch_pte = gen8_pte_encode(px_dma(ppgtt->base.scratch_page),
  479. I915_CACHE_LLC, use_scratch);
  480. while (num_entries) {
  481. struct i915_page_directory *pd;
  482. struct i915_page_table *pt;
  483. if (WARN_ON(!ppgtt->pdp.page_directory[pdpe]))
  484. break;
  485. pd = ppgtt->pdp.page_directory[pdpe];
  486. if (WARN_ON(!pd->page_table[pde]))
  487. break;
  488. pt = pd->page_table[pde];
  489. if (WARN_ON(!px_page(pt)))
  490. break;
  491. last_pte = pte + num_entries;
  492. if (last_pte > GEN8_PTES)
  493. last_pte = GEN8_PTES;
  494. pt_vaddr = kmap_px(pt);
  495. for (i = pte; i < last_pte; i++) {
  496. pt_vaddr[i] = scratch_pte;
  497. num_entries--;
  498. }
  499. kunmap_px(ppgtt, pt);
  500. pte = 0;
  501. if (++pde == I915_PDES) {
  502. pdpe++;
  503. pde = 0;
  504. }
  505. }
  506. }
  507. static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
  508. struct sg_table *pages,
  509. uint64_t start,
  510. enum i915_cache_level cache_level, u32 unused)
  511. {
  512. struct i915_hw_ppgtt *ppgtt =
  513. container_of(vm, struct i915_hw_ppgtt, base);
  514. gen8_pte_t *pt_vaddr;
  515. unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
  516. unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
  517. unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
  518. struct sg_page_iter sg_iter;
  519. pt_vaddr = NULL;
  520. for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
  521. if (WARN_ON(pdpe >= GEN8_LEGACY_PDPES))
  522. break;
  523. if (pt_vaddr == NULL) {
  524. struct i915_page_directory *pd = ppgtt->pdp.page_directory[pdpe];
  525. struct i915_page_table *pt = pd->page_table[pde];
  526. pt_vaddr = kmap_px(pt);
  527. }
  528. pt_vaddr[pte] =
  529. gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
  530. cache_level, true);
  531. if (++pte == GEN8_PTES) {
  532. kunmap_px(ppgtt, pt_vaddr);
  533. pt_vaddr = NULL;
  534. if (++pde == I915_PDES) {
  535. pdpe++;
  536. pde = 0;
  537. }
  538. pte = 0;
  539. }
  540. }
  541. if (pt_vaddr)
  542. kunmap_px(ppgtt, pt_vaddr);
  543. }
  544. static void gen8_free_page_tables(struct drm_device *dev,
  545. struct i915_page_directory *pd)
  546. {
  547. int i;
  548. if (!px_page(pd))
  549. return;
  550. for_each_set_bit(i, pd->used_pdes, I915_PDES) {
  551. if (WARN_ON(!pd->page_table[i]))
  552. continue;
  553. free_pt(dev, pd->page_table[i]);
  554. pd->page_table[i] = NULL;
  555. }
  556. }
  557. static int gen8_init_scratch(struct i915_address_space *vm)
  558. {
  559. struct drm_device *dev = vm->dev;
  560. vm->scratch_page = alloc_scratch_page(dev);
  561. if (IS_ERR(vm->scratch_page))
  562. return PTR_ERR(vm->scratch_page);
  563. vm->scratch_pt = alloc_pt(dev);
  564. if (IS_ERR(vm->scratch_pt)) {
  565. free_scratch_page(dev, vm->scratch_page);
  566. return PTR_ERR(vm->scratch_pt);
  567. }
  568. vm->scratch_pd = alloc_pd(dev);
  569. if (IS_ERR(vm->scratch_pd)) {
  570. free_pt(dev, vm->scratch_pt);
  571. free_scratch_page(dev, vm->scratch_page);
  572. return PTR_ERR(vm->scratch_pd);
  573. }
  574. gen8_initialize_pt(vm, vm->scratch_pt);
  575. gen8_initialize_pd(vm, vm->scratch_pd);
  576. return 0;
  577. }
  578. static void gen8_free_scratch(struct i915_address_space *vm)
  579. {
  580. struct drm_device *dev = vm->dev;
  581. free_pd(dev, vm->scratch_pd);
  582. free_pt(dev, vm->scratch_pt);
  583. free_scratch_page(dev, vm->scratch_page);
  584. }
  585. static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
  586. {
  587. struct i915_hw_ppgtt *ppgtt =
  588. container_of(vm, struct i915_hw_ppgtt, base);
  589. int i;
  590. for_each_set_bit(i, ppgtt->pdp.used_pdpes, GEN8_LEGACY_PDPES) {
  591. if (WARN_ON(!ppgtt->pdp.page_directory[i]))
  592. continue;
  593. gen8_free_page_tables(ppgtt->base.dev,
  594. ppgtt->pdp.page_directory[i]);
  595. free_pd(ppgtt->base.dev, ppgtt->pdp.page_directory[i]);
  596. }
  597. gen8_free_scratch(vm);
  598. }
  599. /**
  600. * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
  601. * @ppgtt: Master ppgtt structure.
  602. * @pd: Page directory for this address range.
  603. * @start: Starting virtual address to begin allocations.
  604. * @length Size of the allocations.
  605. * @new_pts: Bitmap set by function with new allocations. Likely used by the
  606. * caller to free on error.
  607. *
  608. * Allocate the required number of page tables. Extremely similar to
  609. * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
  610. * the page directory boundary (instead of the page directory pointer). That
  611. * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
  612. * possible, and likely that the caller will need to use multiple calls of this
  613. * function to achieve the appropriate allocation.
  614. *
  615. * Return: 0 if success; negative error code otherwise.
  616. */
  617. static int gen8_ppgtt_alloc_pagetabs(struct i915_hw_ppgtt *ppgtt,
  618. struct i915_page_directory *pd,
  619. uint64_t start,
  620. uint64_t length,
  621. unsigned long *new_pts)
  622. {
  623. struct drm_device *dev = ppgtt->base.dev;
  624. struct i915_page_table *pt;
  625. uint64_t temp;
  626. uint32_t pde;
  627. gen8_for_each_pde(pt, pd, start, length, temp, pde) {
  628. /* Don't reallocate page tables */
  629. if (pt) {
  630. /* Scratch is never allocated this way */
  631. WARN_ON(pt == ppgtt->base.scratch_pt);
  632. continue;
  633. }
  634. pt = alloc_pt(dev);
  635. if (IS_ERR(pt))
  636. goto unwind_out;
  637. gen8_initialize_pt(&ppgtt->base, pt);
  638. pd->page_table[pde] = pt;
  639. __set_bit(pde, new_pts);
  640. }
  641. return 0;
  642. unwind_out:
  643. for_each_set_bit(pde, new_pts, I915_PDES)
  644. free_pt(dev, pd->page_table[pde]);
  645. return -ENOMEM;
  646. }
  647. /**
  648. * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
  649. * @ppgtt: Master ppgtt structure.
  650. * @pdp: Page directory pointer for this address range.
  651. * @start: Starting virtual address to begin allocations.
  652. * @length Size of the allocations.
  653. * @new_pds Bitmap set by function with new allocations. Likely used by the
  654. * caller to free on error.
  655. *
  656. * Allocate the required number of page directories starting at the pde index of
  657. * @start, and ending at the pde index @start + @length. This function will skip
  658. * over already allocated page directories within the range, and only allocate
  659. * new ones, setting the appropriate pointer within the pdp as well as the
  660. * correct position in the bitmap @new_pds.
  661. *
  662. * The function will only allocate the pages within the range for a give page
  663. * directory pointer. In other words, if @start + @length straddles a virtually
  664. * addressed PDP boundary (512GB for 4k pages), there will be more allocations
  665. * required by the caller, This is not currently possible, and the BUG in the
  666. * code will prevent it.
  667. *
  668. * Return: 0 if success; negative error code otherwise.
  669. */
  670. static int gen8_ppgtt_alloc_page_directories(struct i915_hw_ppgtt *ppgtt,
  671. struct i915_page_directory_pointer *pdp,
  672. uint64_t start,
  673. uint64_t length,
  674. unsigned long *new_pds)
  675. {
  676. struct drm_device *dev = ppgtt->base.dev;
  677. struct i915_page_directory *pd;
  678. uint64_t temp;
  679. uint32_t pdpe;
  680. WARN_ON(!bitmap_empty(new_pds, GEN8_LEGACY_PDPES));
  681. gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
  682. if (pd)
  683. continue;
  684. pd = alloc_pd(dev);
  685. if (IS_ERR(pd))
  686. goto unwind_out;
  687. gen8_initialize_pd(&ppgtt->base, pd);
  688. pdp->page_directory[pdpe] = pd;
  689. __set_bit(pdpe, new_pds);
  690. }
  691. return 0;
  692. unwind_out:
  693. for_each_set_bit(pdpe, new_pds, GEN8_LEGACY_PDPES)
  694. free_pd(dev, pdp->page_directory[pdpe]);
  695. return -ENOMEM;
  696. }
  697. static void
  698. free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long **new_pts)
  699. {
  700. int i;
  701. for (i = 0; i < GEN8_LEGACY_PDPES; i++)
  702. kfree(new_pts[i]);
  703. kfree(new_pts);
  704. kfree(new_pds);
  705. }
  706. /* Fills in the page directory bitmap, and the array of page tables bitmap. Both
  707. * of these are based on the number of PDPEs in the system.
  708. */
  709. static
  710. int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
  711. unsigned long ***new_pts)
  712. {
  713. int i;
  714. unsigned long *pds;
  715. unsigned long **pts;
  716. pds = kcalloc(BITS_TO_LONGS(GEN8_LEGACY_PDPES), sizeof(unsigned long), GFP_KERNEL);
  717. if (!pds)
  718. return -ENOMEM;
  719. pts = kcalloc(GEN8_LEGACY_PDPES, sizeof(unsigned long *), GFP_KERNEL);
  720. if (!pts) {
  721. kfree(pds);
  722. return -ENOMEM;
  723. }
  724. for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
  725. pts[i] = kcalloc(BITS_TO_LONGS(I915_PDES),
  726. sizeof(unsigned long), GFP_KERNEL);
  727. if (!pts[i])
  728. goto err_out;
  729. }
  730. *new_pds = pds;
  731. *new_pts = pts;
  732. return 0;
  733. err_out:
  734. free_gen8_temp_bitmaps(pds, pts);
  735. return -ENOMEM;
  736. }
  737. /* PDE TLBs are a pain to invalidate on GEN8+. When we modify
  738. * the page table structures, we mark them dirty so that
  739. * context switching/execlist queuing code takes extra steps
  740. * to ensure that tlbs are flushed.
  741. */
  742. static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
  743. {
  744. ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
  745. }
  746. static int gen8_alloc_va_range(struct i915_address_space *vm,
  747. uint64_t start,
  748. uint64_t length)
  749. {
  750. struct i915_hw_ppgtt *ppgtt =
  751. container_of(vm, struct i915_hw_ppgtt, base);
  752. unsigned long *new_page_dirs, **new_page_tables;
  753. struct i915_page_directory *pd;
  754. const uint64_t orig_start = start;
  755. const uint64_t orig_length = length;
  756. uint64_t temp;
  757. uint32_t pdpe;
  758. int ret;
  759. /* Wrap is never okay since we can only represent 48b, and we don't
  760. * actually use the other side of the canonical address space.
  761. */
  762. if (WARN_ON(start + length < start))
  763. return -ENODEV;
  764. if (WARN_ON(start + length > ppgtt->base.total))
  765. return -ENODEV;
  766. ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables);
  767. if (ret)
  768. return ret;
  769. /* Do the allocations first so we can easily bail out */
  770. ret = gen8_ppgtt_alloc_page_directories(ppgtt, &ppgtt->pdp, start, length,
  771. new_page_dirs);
  772. if (ret) {
  773. free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
  774. return ret;
  775. }
  776. /* For every page directory referenced, allocate page tables */
  777. gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) {
  778. ret = gen8_ppgtt_alloc_pagetabs(ppgtt, pd, start, length,
  779. new_page_tables[pdpe]);
  780. if (ret)
  781. goto err_out;
  782. }
  783. start = orig_start;
  784. length = orig_length;
  785. /* Allocations have completed successfully, so set the bitmaps, and do
  786. * the mappings. */
  787. gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) {
  788. gen8_pde_t *const page_directory = kmap_px(pd);
  789. struct i915_page_table *pt;
  790. uint64_t pd_len = gen8_clamp_pd(start, length);
  791. uint64_t pd_start = start;
  792. uint32_t pde;
  793. /* Every pd should be allocated, we just did that above. */
  794. WARN_ON(!pd);
  795. gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) {
  796. /* Same reasoning as pd */
  797. WARN_ON(!pt);
  798. WARN_ON(!pd_len);
  799. WARN_ON(!gen8_pte_count(pd_start, pd_len));
  800. /* Set our used ptes within the page table */
  801. bitmap_set(pt->used_ptes,
  802. gen8_pte_index(pd_start),
  803. gen8_pte_count(pd_start, pd_len));
  804. /* Our pde is now pointing to the pagetable, pt */
  805. __set_bit(pde, pd->used_pdes);
  806. /* Map the PDE to the page table */
  807. page_directory[pde] = gen8_pde_encode(px_dma(pt),
  808. I915_CACHE_LLC);
  809. /* NB: We haven't yet mapped ptes to pages. At this
  810. * point we're still relying on insert_entries() */
  811. }
  812. kunmap_px(ppgtt, page_directory);
  813. __set_bit(pdpe, ppgtt->pdp.used_pdpes);
  814. }
  815. free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
  816. mark_tlbs_dirty(ppgtt);
  817. return 0;
  818. err_out:
  819. while (pdpe--) {
  820. for_each_set_bit(temp, new_page_tables[pdpe], I915_PDES)
  821. free_pt(vm->dev, ppgtt->pdp.page_directory[pdpe]->page_table[temp]);
  822. }
  823. for_each_set_bit(pdpe, new_page_dirs, GEN8_LEGACY_PDPES)
  824. free_pd(vm->dev, ppgtt->pdp.page_directory[pdpe]);
  825. free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
  826. mark_tlbs_dirty(ppgtt);
  827. return ret;
  828. }
  829. /*
  830. * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
  831. * with a net effect resembling a 2-level page table in normal x86 terms. Each
  832. * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
  833. * space.
  834. *
  835. */
  836. static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
  837. {
  838. int ret;
  839. ret = gen8_init_scratch(&ppgtt->base);
  840. if (ret)
  841. return ret;
  842. ppgtt->base.start = 0;
  843. ppgtt->base.total = 1ULL << 32;
  844. if (IS_ENABLED(CONFIG_X86_32))
  845. /* While we have a proliferation of size_t variables
  846. * we cannot represent the full ppgtt size on 32bit,
  847. * so limit it to the same size as the GGTT (currently
  848. * 2GiB).
  849. */
  850. ppgtt->base.total = to_i915(ppgtt->base.dev)->gtt.base.total;
  851. ppgtt->base.cleanup = gen8_ppgtt_cleanup;
  852. ppgtt->base.allocate_va_range = gen8_alloc_va_range;
  853. ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
  854. ppgtt->base.clear_range = gen8_ppgtt_clear_range;
  855. ppgtt->base.unbind_vma = ppgtt_unbind_vma;
  856. ppgtt->base.bind_vma = ppgtt_bind_vma;
  857. ppgtt->switch_mm = gen8_mm_switch;
  858. return 0;
  859. }
  860. static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
  861. {
  862. struct i915_address_space *vm = &ppgtt->base;
  863. struct i915_page_table *unused;
  864. gen6_pte_t scratch_pte;
  865. uint32_t pd_entry;
  866. uint32_t pte, pde, temp;
  867. uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
  868. scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
  869. I915_CACHE_LLC, true, 0);
  870. gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) {
  871. u32 expected;
  872. gen6_pte_t *pt_vaddr;
  873. const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
  874. pd_entry = readl(ppgtt->pd_addr + pde);
  875. expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
  876. if (pd_entry != expected)
  877. seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
  878. pde,
  879. pd_entry,
  880. expected);
  881. seq_printf(m, "\tPDE: %x\n", pd_entry);
  882. pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
  883. for (pte = 0; pte < GEN6_PTES; pte+=4) {
  884. unsigned long va =
  885. (pde * PAGE_SIZE * GEN6_PTES) +
  886. (pte * PAGE_SIZE);
  887. int i;
  888. bool found = false;
  889. for (i = 0; i < 4; i++)
  890. if (pt_vaddr[pte + i] != scratch_pte)
  891. found = true;
  892. if (!found)
  893. continue;
  894. seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
  895. for (i = 0; i < 4; i++) {
  896. if (pt_vaddr[pte + i] != scratch_pte)
  897. seq_printf(m, " %08x", pt_vaddr[pte + i]);
  898. else
  899. seq_puts(m, " SCRATCH ");
  900. }
  901. seq_puts(m, "\n");
  902. }
  903. kunmap_px(ppgtt, pt_vaddr);
  904. }
  905. }
  906. /* Write pde (index) from the page directory @pd to the page table @pt */
  907. static void gen6_write_pde(struct i915_page_directory *pd,
  908. const int pde, struct i915_page_table *pt)
  909. {
  910. /* Caller needs to make sure the write completes if necessary */
  911. struct i915_hw_ppgtt *ppgtt =
  912. container_of(pd, struct i915_hw_ppgtt, pd);
  913. u32 pd_entry;
  914. pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
  915. pd_entry |= GEN6_PDE_VALID;
  916. writel(pd_entry, ppgtt->pd_addr + pde);
  917. }
  918. /* Write all the page tables found in the ppgtt structure to incrementing page
  919. * directories. */
  920. static void gen6_write_page_range(struct drm_i915_private *dev_priv,
  921. struct i915_page_directory *pd,
  922. uint32_t start, uint32_t length)
  923. {
  924. struct i915_page_table *pt;
  925. uint32_t pde, temp;
  926. gen6_for_each_pde(pt, pd, start, length, temp, pde)
  927. gen6_write_pde(pd, pde, pt);
  928. /* Make sure write is complete before other code can use this page
  929. * table. Also require for WC mapped PTEs */
  930. readl(dev_priv->gtt.gsm);
  931. }
  932. static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
  933. {
  934. BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
  935. return (ppgtt->pd.base.ggtt_offset / 64) << 16;
  936. }
  937. static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
  938. struct drm_i915_gem_request *req)
  939. {
  940. struct intel_engine_cs *ring = req->ring;
  941. int ret;
  942. /* NB: TLBs must be flushed and invalidated before a switch */
  943. ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  944. if (ret)
  945. return ret;
  946. ret = intel_ring_begin(req, 6);
  947. if (ret)
  948. return ret;
  949. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
  950. intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
  951. intel_ring_emit(ring, PP_DIR_DCLV_2G);
  952. intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
  953. intel_ring_emit(ring, get_pd_offset(ppgtt));
  954. intel_ring_emit(ring, MI_NOOP);
  955. intel_ring_advance(ring);
  956. return 0;
  957. }
  958. static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
  959. struct drm_i915_gem_request *req)
  960. {
  961. struct intel_engine_cs *ring = req->ring;
  962. struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
  963. I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
  964. I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
  965. return 0;
  966. }
  967. static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
  968. struct drm_i915_gem_request *req)
  969. {
  970. struct intel_engine_cs *ring = req->ring;
  971. int ret;
  972. /* NB: TLBs must be flushed and invalidated before a switch */
  973. ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  974. if (ret)
  975. return ret;
  976. ret = intel_ring_begin(req, 6);
  977. if (ret)
  978. return ret;
  979. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
  980. intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
  981. intel_ring_emit(ring, PP_DIR_DCLV_2G);
  982. intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
  983. intel_ring_emit(ring, get_pd_offset(ppgtt));
  984. intel_ring_emit(ring, MI_NOOP);
  985. intel_ring_advance(ring);
  986. /* XXX: RCS is the only one to auto invalidate the TLBs? */
  987. if (ring->id != RCS) {
  988. ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  989. if (ret)
  990. return ret;
  991. }
  992. return 0;
  993. }
  994. static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
  995. struct drm_i915_gem_request *req)
  996. {
  997. struct intel_engine_cs *ring = req->ring;
  998. struct drm_device *dev = ppgtt->base.dev;
  999. struct drm_i915_private *dev_priv = dev->dev_private;
  1000. I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
  1001. I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
  1002. POSTING_READ(RING_PP_DIR_DCLV(ring));
  1003. return 0;
  1004. }
  1005. static void gen8_ppgtt_enable(struct drm_device *dev)
  1006. {
  1007. struct drm_i915_private *dev_priv = dev->dev_private;
  1008. struct intel_engine_cs *ring;
  1009. int j;
  1010. for_each_ring(ring, dev_priv, j) {
  1011. I915_WRITE(RING_MODE_GEN7(ring),
  1012. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  1013. }
  1014. }
  1015. static void gen7_ppgtt_enable(struct drm_device *dev)
  1016. {
  1017. struct drm_i915_private *dev_priv = dev->dev_private;
  1018. struct intel_engine_cs *ring;
  1019. uint32_t ecochk, ecobits;
  1020. int i;
  1021. ecobits = I915_READ(GAC_ECO_BITS);
  1022. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
  1023. ecochk = I915_READ(GAM_ECOCHK);
  1024. if (IS_HASWELL(dev)) {
  1025. ecochk |= ECOCHK_PPGTT_WB_HSW;
  1026. } else {
  1027. ecochk |= ECOCHK_PPGTT_LLC_IVB;
  1028. ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
  1029. }
  1030. I915_WRITE(GAM_ECOCHK, ecochk);
  1031. for_each_ring(ring, dev_priv, i) {
  1032. /* GFX_MODE is per-ring on gen7+ */
  1033. I915_WRITE(RING_MODE_GEN7(ring),
  1034. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  1035. }
  1036. }
  1037. static void gen6_ppgtt_enable(struct drm_device *dev)
  1038. {
  1039. struct drm_i915_private *dev_priv = dev->dev_private;
  1040. uint32_t ecochk, gab_ctl, ecobits;
  1041. ecobits = I915_READ(GAC_ECO_BITS);
  1042. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
  1043. ECOBITS_PPGTT_CACHE64B);
  1044. gab_ctl = I915_READ(GAB_CTL);
  1045. I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
  1046. ecochk = I915_READ(GAM_ECOCHK);
  1047. I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
  1048. I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  1049. }
  1050. /* PPGTT support for Sandybdrige/Gen6 and later */
  1051. static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
  1052. uint64_t start,
  1053. uint64_t length,
  1054. bool use_scratch)
  1055. {
  1056. struct i915_hw_ppgtt *ppgtt =
  1057. container_of(vm, struct i915_hw_ppgtt, base);
  1058. gen6_pte_t *pt_vaddr, scratch_pte;
  1059. unsigned first_entry = start >> PAGE_SHIFT;
  1060. unsigned num_entries = length >> PAGE_SHIFT;
  1061. unsigned act_pt = first_entry / GEN6_PTES;
  1062. unsigned first_pte = first_entry % GEN6_PTES;
  1063. unsigned last_pte, i;
  1064. scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
  1065. I915_CACHE_LLC, true, 0);
  1066. while (num_entries) {
  1067. last_pte = first_pte + num_entries;
  1068. if (last_pte > GEN6_PTES)
  1069. last_pte = GEN6_PTES;
  1070. pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
  1071. for (i = first_pte; i < last_pte; i++)
  1072. pt_vaddr[i] = scratch_pte;
  1073. kunmap_px(ppgtt, pt_vaddr);
  1074. num_entries -= last_pte - first_pte;
  1075. first_pte = 0;
  1076. act_pt++;
  1077. }
  1078. }
  1079. static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
  1080. struct sg_table *pages,
  1081. uint64_t start,
  1082. enum i915_cache_level cache_level, u32 flags)
  1083. {
  1084. struct i915_hw_ppgtt *ppgtt =
  1085. container_of(vm, struct i915_hw_ppgtt, base);
  1086. gen6_pte_t *pt_vaddr;
  1087. unsigned first_entry = start >> PAGE_SHIFT;
  1088. unsigned act_pt = first_entry / GEN6_PTES;
  1089. unsigned act_pte = first_entry % GEN6_PTES;
  1090. struct sg_page_iter sg_iter;
  1091. pt_vaddr = NULL;
  1092. for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
  1093. if (pt_vaddr == NULL)
  1094. pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
  1095. pt_vaddr[act_pte] =
  1096. vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
  1097. cache_level, true, flags);
  1098. if (++act_pte == GEN6_PTES) {
  1099. kunmap_px(ppgtt, pt_vaddr);
  1100. pt_vaddr = NULL;
  1101. act_pt++;
  1102. act_pte = 0;
  1103. }
  1104. }
  1105. if (pt_vaddr)
  1106. kunmap_px(ppgtt, pt_vaddr);
  1107. }
  1108. static int gen6_alloc_va_range(struct i915_address_space *vm,
  1109. uint64_t start_in, uint64_t length_in)
  1110. {
  1111. DECLARE_BITMAP(new_page_tables, I915_PDES);
  1112. struct drm_device *dev = vm->dev;
  1113. struct drm_i915_private *dev_priv = dev->dev_private;
  1114. struct i915_hw_ppgtt *ppgtt =
  1115. container_of(vm, struct i915_hw_ppgtt, base);
  1116. struct i915_page_table *pt;
  1117. uint32_t start, length, start_save, length_save;
  1118. uint32_t pde, temp;
  1119. int ret;
  1120. if (WARN_ON(start_in + length_in > ppgtt->base.total))
  1121. return -ENODEV;
  1122. start = start_save = start_in;
  1123. length = length_save = length_in;
  1124. bitmap_zero(new_page_tables, I915_PDES);
  1125. /* The allocation is done in two stages so that we can bail out with
  1126. * minimal amount of pain. The first stage finds new page tables that
  1127. * need allocation. The second stage marks use ptes within the page
  1128. * tables.
  1129. */
  1130. gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
  1131. if (pt != vm->scratch_pt) {
  1132. WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
  1133. continue;
  1134. }
  1135. /* We've already allocated a page table */
  1136. WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
  1137. pt = alloc_pt(dev);
  1138. if (IS_ERR(pt)) {
  1139. ret = PTR_ERR(pt);
  1140. goto unwind_out;
  1141. }
  1142. gen6_initialize_pt(vm, pt);
  1143. ppgtt->pd.page_table[pde] = pt;
  1144. __set_bit(pde, new_page_tables);
  1145. trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
  1146. }
  1147. start = start_save;
  1148. length = length_save;
  1149. gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
  1150. DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
  1151. bitmap_zero(tmp_bitmap, GEN6_PTES);
  1152. bitmap_set(tmp_bitmap, gen6_pte_index(start),
  1153. gen6_pte_count(start, length));
  1154. if (__test_and_clear_bit(pde, new_page_tables))
  1155. gen6_write_pde(&ppgtt->pd, pde, pt);
  1156. trace_i915_page_table_entry_map(vm, pde, pt,
  1157. gen6_pte_index(start),
  1158. gen6_pte_count(start, length),
  1159. GEN6_PTES);
  1160. bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
  1161. GEN6_PTES);
  1162. }
  1163. WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
  1164. /* Make sure write is complete before other code can use this page
  1165. * table. Also require for WC mapped PTEs */
  1166. readl(dev_priv->gtt.gsm);
  1167. mark_tlbs_dirty(ppgtt);
  1168. return 0;
  1169. unwind_out:
  1170. for_each_set_bit(pde, new_page_tables, I915_PDES) {
  1171. struct i915_page_table *pt = ppgtt->pd.page_table[pde];
  1172. ppgtt->pd.page_table[pde] = vm->scratch_pt;
  1173. free_pt(vm->dev, pt);
  1174. }
  1175. mark_tlbs_dirty(ppgtt);
  1176. return ret;
  1177. }
  1178. static int gen6_init_scratch(struct i915_address_space *vm)
  1179. {
  1180. struct drm_device *dev = vm->dev;
  1181. vm->scratch_page = alloc_scratch_page(dev);
  1182. if (IS_ERR(vm->scratch_page))
  1183. return PTR_ERR(vm->scratch_page);
  1184. vm->scratch_pt = alloc_pt(dev);
  1185. if (IS_ERR(vm->scratch_pt)) {
  1186. free_scratch_page(dev, vm->scratch_page);
  1187. return PTR_ERR(vm->scratch_pt);
  1188. }
  1189. gen6_initialize_pt(vm, vm->scratch_pt);
  1190. return 0;
  1191. }
  1192. static void gen6_free_scratch(struct i915_address_space *vm)
  1193. {
  1194. struct drm_device *dev = vm->dev;
  1195. free_pt(dev, vm->scratch_pt);
  1196. free_scratch_page(dev, vm->scratch_page);
  1197. }
  1198. static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
  1199. {
  1200. struct i915_hw_ppgtt *ppgtt =
  1201. container_of(vm, struct i915_hw_ppgtt, base);
  1202. struct i915_page_table *pt;
  1203. uint32_t pde;
  1204. drm_mm_remove_node(&ppgtt->node);
  1205. gen6_for_all_pdes(pt, ppgtt, pde) {
  1206. if (pt != vm->scratch_pt)
  1207. free_pt(ppgtt->base.dev, pt);
  1208. }
  1209. gen6_free_scratch(vm);
  1210. }
  1211. static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
  1212. {
  1213. struct i915_address_space *vm = &ppgtt->base;
  1214. struct drm_device *dev = ppgtt->base.dev;
  1215. struct drm_i915_private *dev_priv = dev->dev_private;
  1216. bool retried = false;
  1217. int ret;
  1218. /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
  1219. * allocator works in address space sizes, so it's multiplied by page
  1220. * size. We allocate at the top of the GTT to avoid fragmentation.
  1221. */
  1222. BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
  1223. ret = gen6_init_scratch(vm);
  1224. if (ret)
  1225. return ret;
  1226. alloc:
  1227. ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
  1228. &ppgtt->node, GEN6_PD_SIZE,
  1229. GEN6_PD_ALIGN, 0,
  1230. 0, dev_priv->gtt.base.total,
  1231. DRM_MM_TOPDOWN);
  1232. if (ret == -ENOSPC && !retried) {
  1233. ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
  1234. GEN6_PD_SIZE, GEN6_PD_ALIGN,
  1235. I915_CACHE_NONE,
  1236. 0, dev_priv->gtt.base.total,
  1237. 0);
  1238. if (ret)
  1239. goto err_out;
  1240. retried = true;
  1241. goto alloc;
  1242. }
  1243. if (ret)
  1244. goto err_out;
  1245. if (ppgtt->node.start < dev_priv->gtt.mappable_end)
  1246. DRM_DEBUG("Forced to use aperture for PDEs\n");
  1247. return 0;
  1248. err_out:
  1249. gen6_free_scratch(vm);
  1250. return ret;
  1251. }
  1252. static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
  1253. {
  1254. return gen6_ppgtt_allocate_page_directories(ppgtt);
  1255. }
  1256. static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
  1257. uint64_t start, uint64_t length)
  1258. {
  1259. struct i915_page_table *unused;
  1260. uint32_t pde, temp;
  1261. gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
  1262. ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
  1263. }
  1264. static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
  1265. {
  1266. struct drm_device *dev = ppgtt->base.dev;
  1267. struct drm_i915_private *dev_priv = dev->dev_private;
  1268. int ret;
  1269. ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
  1270. if (IS_GEN6(dev)) {
  1271. ppgtt->switch_mm = gen6_mm_switch;
  1272. } else if (IS_HASWELL(dev)) {
  1273. ppgtt->switch_mm = hsw_mm_switch;
  1274. } else if (IS_GEN7(dev)) {
  1275. ppgtt->switch_mm = gen7_mm_switch;
  1276. } else
  1277. BUG();
  1278. if (intel_vgpu_active(dev))
  1279. ppgtt->switch_mm = vgpu_mm_switch;
  1280. ret = gen6_ppgtt_alloc(ppgtt);
  1281. if (ret)
  1282. return ret;
  1283. ppgtt->base.allocate_va_range = gen6_alloc_va_range;
  1284. ppgtt->base.clear_range = gen6_ppgtt_clear_range;
  1285. ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
  1286. ppgtt->base.unbind_vma = ppgtt_unbind_vma;
  1287. ppgtt->base.bind_vma = ppgtt_bind_vma;
  1288. ppgtt->base.cleanup = gen6_ppgtt_cleanup;
  1289. ppgtt->base.start = 0;
  1290. ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
  1291. ppgtt->debug_dump = gen6_dump_ppgtt;
  1292. ppgtt->pd.base.ggtt_offset =
  1293. ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
  1294. ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
  1295. ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
  1296. gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
  1297. gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
  1298. DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
  1299. ppgtt->node.size >> 20,
  1300. ppgtt->node.start / PAGE_SIZE);
  1301. DRM_DEBUG("Adding PPGTT at offset %x\n",
  1302. ppgtt->pd.base.ggtt_offset << 10);
  1303. return 0;
  1304. }
  1305. static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
  1306. {
  1307. ppgtt->base.dev = dev;
  1308. if (INTEL_INFO(dev)->gen < 8)
  1309. return gen6_ppgtt_init(ppgtt);
  1310. else
  1311. return gen8_ppgtt_init(ppgtt);
  1312. }
  1313. int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
  1314. {
  1315. struct drm_i915_private *dev_priv = dev->dev_private;
  1316. int ret = 0;
  1317. ret = __hw_ppgtt_init(dev, ppgtt);
  1318. if (ret == 0) {
  1319. kref_init(&ppgtt->ref);
  1320. drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
  1321. ppgtt->base.total);
  1322. i915_init_vm(dev_priv, &ppgtt->base);
  1323. }
  1324. return ret;
  1325. }
  1326. int i915_ppgtt_init_hw(struct drm_device *dev)
  1327. {
  1328. /* In the case of execlists, PPGTT is enabled by the context descriptor
  1329. * and the PDPs are contained within the context itself. We don't
  1330. * need to do anything here. */
  1331. if (i915.enable_execlists)
  1332. return 0;
  1333. if (!USES_PPGTT(dev))
  1334. return 0;
  1335. if (IS_GEN6(dev))
  1336. gen6_ppgtt_enable(dev);
  1337. else if (IS_GEN7(dev))
  1338. gen7_ppgtt_enable(dev);
  1339. else if (INTEL_INFO(dev)->gen >= 8)
  1340. gen8_ppgtt_enable(dev);
  1341. else
  1342. MISSING_CASE(INTEL_INFO(dev)->gen);
  1343. return 0;
  1344. }
  1345. int i915_ppgtt_init_ring(struct drm_i915_gem_request *req)
  1346. {
  1347. struct drm_i915_private *dev_priv = req->ring->dev->dev_private;
  1348. struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
  1349. if (i915.enable_execlists)
  1350. return 0;
  1351. if (!ppgtt)
  1352. return 0;
  1353. return ppgtt->switch_mm(ppgtt, req);
  1354. }
  1355. struct i915_hw_ppgtt *
  1356. i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
  1357. {
  1358. struct i915_hw_ppgtt *ppgtt;
  1359. int ret;
  1360. ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
  1361. if (!ppgtt)
  1362. return ERR_PTR(-ENOMEM);
  1363. ret = i915_ppgtt_init(dev, ppgtt);
  1364. if (ret) {
  1365. kfree(ppgtt);
  1366. return ERR_PTR(ret);
  1367. }
  1368. ppgtt->file_priv = fpriv;
  1369. trace_i915_ppgtt_create(&ppgtt->base);
  1370. return ppgtt;
  1371. }
  1372. void i915_ppgtt_release(struct kref *kref)
  1373. {
  1374. struct i915_hw_ppgtt *ppgtt =
  1375. container_of(kref, struct i915_hw_ppgtt, ref);
  1376. trace_i915_ppgtt_release(&ppgtt->base);
  1377. /* vmas should already be unbound */
  1378. WARN_ON(!list_empty(&ppgtt->base.active_list));
  1379. WARN_ON(!list_empty(&ppgtt->base.inactive_list));
  1380. list_del(&ppgtt->base.global_link);
  1381. drm_mm_takedown(&ppgtt->base.mm);
  1382. ppgtt->base.cleanup(&ppgtt->base);
  1383. kfree(ppgtt);
  1384. }
  1385. extern int intel_iommu_gfx_mapped;
  1386. /* Certain Gen5 chipsets require require idling the GPU before
  1387. * unmapping anything from the GTT when VT-d is enabled.
  1388. */
  1389. static bool needs_idle_maps(struct drm_device *dev)
  1390. {
  1391. #ifdef CONFIG_INTEL_IOMMU
  1392. /* Query intel_iommu to see if we need the workaround. Presumably that
  1393. * was loaded first.
  1394. */
  1395. if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
  1396. return true;
  1397. #endif
  1398. return false;
  1399. }
  1400. static bool do_idling(struct drm_i915_private *dev_priv)
  1401. {
  1402. bool ret = dev_priv->mm.interruptible;
  1403. if (unlikely(dev_priv->gtt.do_idle_maps)) {
  1404. dev_priv->mm.interruptible = false;
  1405. if (i915_gpu_idle(dev_priv->dev)) {
  1406. DRM_ERROR("Couldn't idle GPU\n");
  1407. /* Wait a bit, in hopes it avoids the hang */
  1408. udelay(10);
  1409. }
  1410. }
  1411. return ret;
  1412. }
  1413. static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
  1414. {
  1415. if (unlikely(dev_priv->gtt.do_idle_maps))
  1416. dev_priv->mm.interruptible = interruptible;
  1417. }
  1418. void i915_check_and_clear_faults(struct drm_device *dev)
  1419. {
  1420. struct drm_i915_private *dev_priv = dev->dev_private;
  1421. struct intel_engine_cs *ring;
  1422. int i;
  1423. if (INTEL_INFO(dev)->gen < 6)
  1424. return;
  1425. for_each_ring(ring, dev_priv, i) {
  1426. u32 fault_reg;
  1427. fault_reg = I915_READ(RING_FAULT_REG(ring));
  1428. if (fault_reg & RING_FAULT_VALID) {
  1429. DRM_DEBUG_DRIVER("Unexpected fault\n"
  1430. "\tAddr: 0x%08lx\n"
  1431. "\tAddress space: %s\n"
  1432. "\tSource ID: %d\n"
  1433. "\tType: %d\n",
  1434. fault_reg & PAGE_MASK,
  1435. fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
  1436. RING_FAULT_SRCID(fault_reg),
  1437. RING_FAULT_FAULT_TYPE(fault_reg));
  1438. I915_WRITE(RING_FAULT_REG(ring),
  1439. fault_reg & ~RING_FAULT_VALID);
  1440. }
  1441. }
  1442. POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
  1443. }
  1444. static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
  1445. {
  1446. if (INTEL_INFO(dev_priv->dev)->gen < 6) {
  1447. intel_gtt_chipset_flush();
  1448. } else {
  1449. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  1450. POSTING_READ(GFX_FLSH_CNTL_GEN6);
  1451. }
  1452. }
  1453. void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
  1454. {
  1455. struct drm_i915_private *dev_priv = dev->dev_private;
  1456. /* Don't bother messing with faults pre GEN6 as we have little
  1457. * documentation supporting that it's a good idea.
  1458. */
  1459. if (INTEL_INFO(dev)->gen < 6)
  1460. return;
  1461. i915_check_and_clear_faults(dev);
  1462. dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
  1463. dev_priv->gtt.base.start,
  1464. dev_priv->gtt.base.total,
  1465. true);
  1466. i915_ggtt_flush(dev_priv);
  1467. }
  1468. int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
  1469. {
  1470. if (!dma_map_sg(&obj->base.dev->pdev->dev,
  1471. obj->pages->sgl, obj->pages->nents,
  1472. PCI_DMA_BIDIRECTIONAL))
  1473. return -ENOSPC;
  1474. return 0;
  1475. }
  1476. static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
  1477. {
  1478. #ifdef writeq
  1479. writeq(pte, addr);
  1480. #else
  1481. iowrite32((u32)pte, addr);
  1482. iowrite32(pte >> 32, addr + 4);
  1483. #endif
  1484. }
  1485. static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
  1486. struct sg_table *st,
  1487. uint64_t start,
  1488. enum i915_cache_level level, u32 unused)
  1489. {
  1490. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1491. unsigned first_entry = start >> PAGE_SHIFT;
  1492. gen8_pte_t __iomem *gtt_entries =
  1493. (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
  1494. int i = 0;
  1495. struct sg_page_iter sg_iter;
  1496. dma_addr_t addr = 0; /* shut up gcc */
  1497. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
  1498. addr = sg_dma_address(sg_iter.sg) +
  1499. (sg_iter.sg_pgoffset << PAGE_SHIFT);
  1500. gen8_set_pte(&gtt_entries[i],
  1501. gen8_pte_encode(addr, level, true));
  1502. i++;
  1503. }
  1504. /*
  1505. * XXX: This serves as a posting read to make sure that the PTE has
  1506. * actually been updated. There is some concern that even though
  1507. * registers and PTEs are within the same BAR that they are potentially
  1508. * of NUMA access patterns. Therefore, even with the way we assume
  1509. * hardware should work, we must keep this posting read for paranoia.
  1510. */
  1511. if (i != 0)
  1512. WARN_ON(readq(&gtt_entries[i-1])
  1513. != gen8_pte_encode(addr, level, true));
  1514. /* This next bit makes the above posting read even more important. We
  1515. * want to flush the TLBs only after we're certain all the PTE updates
  1516. * have finished.
  1517. */
  1518. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  1519. POSTING_READ(GFX_FLSH_CNTL_GEN6);
  1520. }
  1521. /*
  1522. * Binds an object into the global gtt with the specified cache level. The object
  1523. * will be accessible to the GPU via commands whose operands reference offsets
  1524. * within the global GTT as well as accessible by the GPU through the GMADR
  1525. * mapped BAR (dev_priv->mm.gtt->gtt).
  1526. */
  1527. static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
  1528. struct sg_table *st,
  1529. uint64_t start,
  1530. enum i915_cache_level level, u32 flags)
  1531. {
  1532. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1533. unsigned first_entry = start >> PAGE_SHIFT;
  1534. gen6_pte_t __iomem *gtt_entries =
  1535. (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
  1536. int i = 0;
  1537. struct sg_page_iter sg_iter;
  1538. dma_addr_t addr = 0;
  1539. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
  1540. addr = sg_page_iter_dma_address(&sg_iter);
  1541. iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
  1542. i++;
  1543. }
  1544. /* XXX: This serves as a posting read to make sure that the PTE has
  1545. * actually been updated. There is some concern that even though
  1546. * registers and PTEs are within the same BAR that they are potentially
  1547. * of NUMA access patterns. Therefore, even with the way we assume
  1548. * hardware should work, we must keep this posting read for paranoia.
  1549. */
  1550. if (i != 0) {
  1551. unsigned long gtt = readl(&gtt_entries[i-1]);
  1552. WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
  1553. }
  1554. /* This next bit makes the above posting read even more important. We
  1555. * want to flush the TLBs only after we're certain all the PTE updates
  1556. * have finished.
  1557. */
  1558. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  1559. POSTING_READ(GFX_FLSH_CNTL_GEN6);
  1560. }
  1561. static void gen8_ggtt_clear_range(struct i915_address_space *vm,
  1562. uint64_t start,
  1563. uint64_t length,
  1564. bool use_scratch)
  1565. {
  1566. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1567. unsigned first_entry = start >> PAGE_SHIFT;
  1568. unsigned num_entries = length >> PAGE_SHIFT;
  1569. gen8_pte_t scratch_pte, __iomem *gtt_base =
  1570. (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
  1571. const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
  1572. int i;
  1573. if (WARN(num_entries > max_entries,
  1574. "First entry = %d; Num entries = %d (max=%d)\n",
  1575. first_entry, num_entries, max_entries))
  1576. num_entries = max_entries;
  1577. scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
  1578. I915_CACHE_LLC,
  1579. use_scratch);
  1580. for (i = 0; i < num_entries; i++)
  1581. gen8_set_pte(&gtt_base[i], scratch_pte);
  1582. readl(gtt_base);
  1583. }
  1584. static void gen6_ggtt_clear_range(struct i915_address_space *vm,
  1585. uint64_t start,
  1586. uint64_t length,
  1587. bool use_scratch)
  1588. {
  1589. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1590. unsigned first_entry = start >> PAGE_SHIFT;
  1591. unsigned num_entries = length >> PAGE_SHIFT;
  1592. gen6_pte_t scratch_pte, __iomem *gtt_base =
  1593. (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
  1594. const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
  1595. int i;
  1596. if (WARN(num_entries > max_entries,
  1597. "First entry = %d; Num entries = %d (max=%d)\n",
  1598. first_entry, num_entries, max_entries))
  1599. num_entries = max_entries;
  1600. scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
  1601. I915_CACHE_LLC, use_scratch, 0);
  1602. for (i = 0; i < num_entries; i++)
  1603. iowrite32(scratch_pte, &gtt_base[i]);
  1604. readl(gtt_base);
  1605. }
  1606. static void i915_ggtt_insert_entries(struct i915_address_space *vm,
  1607. struct sg_table *pages,
  1608. uint64_t start,
  1609. enum i915_cache_level cache_level, u32 unused)
  1610. {
  1611. unsigned int flags = (cache_level == I915_CACHE_NONE) ?
  1612. AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
  1613. intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
  1614. }
  1615. static void i915_ggtt_clear_range(struct i915_address_space *vm,
  1616. uint64_t start,
  1617. uint64_t length,
  1618. bool unused)
  1619. {
  1620. unsigned first_entry = start >> PAGE_SHIFT;
  1621. unsigned num_entries = length >> PAGE_SHIFT;
  1622. intel_gtt_clear_range(first_entry, num_entries);
  1623. }
  1624. static int ggtt_bind_vma(struct i915_vma *vma,
  1625. enum i915_cache_level cache_level,
  1626. u32 flags)
  1627. {
  1628. struct drm_device *dev = vma->vm->dev;
  1629. struct drm_i915_private *dev_priv = dev->dev_private;
  1630. struct drm_i915_gem_object *obj = vma->obj;
  1631. struct sg_table *pages = obj->pages;
  1632. u32 pte_flags = 0;
  1633. int ret;
  1634. ret = i915_get_ggtt_vma_pages(vma);
  1635. if (ret)
  1636. return ret;
  1637. pages = vma->ggtt_view.pages;
  1638. /* Currently applicable only to VLV */
  1639. if (obj->gt_ro)
  1640. pte_flags |= PTE_READ_ONLY;
  1641. if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
  1642. vma->vm->insert_entries(vma->vm, pages,
  1643. vma->node.start,
  1644. cache_level, pte_flags);
  1645. /* Note the inconsistency here is due to absence of the
  1646. * aliasing ppgtt on gen4 and earlier. Though we always
  1647. * request PIN_USER for execbuffer (translated to LOCAL_BIND),
  1648. * without the appgtt, we cannot honour that request and so
  1649. * must substitute it with a global binding. Since we do this
  1650. * behind the upper layers back, we need to explicitly set
  1651. * the bound flag ourselves.
  1652. */
  1653. vma->bound |= GLOBAL_BIND;
  1654. }
  1655. if (dev_priv->mm.aliasing_ppgtt && flags & LOCAL_BIND) {
  1656. struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
  1657. appgtt->base.insert_entries(&appgtt->base, pages,
  1658. vma->node.start,
  1659. cache_level, pte_flags);
  1660. }
  1661. return 0;
  1662. }
  1663. static void ggtt_unbind_vma(struct i915_vma *vma)
  1664. {
  1665. struct drm_device *dev = vma->vm->dev;
  1666. struct drm_i915_private *dev_priv = dev->dev_private;
  1667. struct drm_i915_gem_object *obj = vma->obj;
  1668. const uint64_t size = min_t(uint64_t,
  1669. obj->base.size,
  1670. vma->node.size);
  1671. if (vma->bound & GLOBAL_BIND) {
  1672. vma->vm->clear_range(vma->vm,
  1673. vma->node.start,
  1674. size,
  1675. true);
  1676. }
  1677. if (dev_priv->mm.aliasing_ppgtt && vma->bound & LOCAL_BIND) {
  1678. struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
  1679. appgtt->base.clear_range(&appgtt->base,
  1680. vma->node.start,
  1681. size,
  1682. true);
  1683. }
  1684. }
  1685. void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
  1686. {
  1687. struct drm_device *dev = obj->base.dev;
  1688. struct drm_i915_private *dev_priv = dev->dev_private;
  1689. bool interruptible;
  1690. interruptible = do_idling(dev_priv);
  1691. dma_unmap_sg(&dev->pdev->dev, obj->pages->sgl, obj->pages->nents,
  1692. PCI_DMA_BIDIRECTIONAL);
  1693. undo_idling(dev_priv, interruptible);
  1694. }
  1695. static void i915_gtt_color_adjust(struct drm_mm_node *node,
  1696. unsigned long color,
  1697. u64 *start,
  1698. u64 *end)
  1699. {
  1700. if (node->color != color)
  1701. *start += 4096;
  1702. if (!list_empty(&node->node_list)) {
  1703. node = list_entry(node->node_list.next,
  1704. struct drm_mm_node,
  1705. node_list);
  1706. if (node->allocated && node->color != color)
  1707. *end -= 4096;
  1708. }
  1709. }
  1710. static int i915_gem_setup_global_gtt(struct drm_device *dev,
  1711. unsigned long start,
  1712. unsigned long mappable_end,
  1713. unsigned long end)
  1714. {
  1715. /* Let GEM Manage all of the aperture.
  1716. *
  1717. * However, leave one page at the end still bound to the scratch page.
  1718. * There are a number of places where the hardware apparently prefetches
  1719. * past the end of the object, and we've seen multiple hangs with the
  1720. * GPU head pointer stuck in a batchbuffer bound at the last page of the
  1721. * aperture. One page should be enough to keep any prefetching inside
  1722. * of the aperture.
  1723. */
  1724. struct drm_i915_private *dev_priv = dev->dev_private;
  1725. struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
  1726. struct drm_mm_node *entry;
  1727. struct drm_i915_gem_object *obj;
  1728. unsigned long hole_start, hole_end;
  1729. int ret;
  1730. BUG_ON(mappable_end > end);
  1731. /* Subtract the guard page ... */
  1732. drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
  1733. dev_priv->gtt.base.start = start;
  1734. dev_priv->gtt.base.total = end - start;
  1735. if (intel_vgpu_active(dev)) {
  1736. ret = intel_vgt_balloon(dev);
  1737. if (ret)
  1738. return ret;
  1739. }
  1740. if (!HAS_LLC(dev))
  1741. dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
  1742. /* Mark any preallocated objects as occupied */
  1743. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  1744. struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
  1745. DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
  1746. i915_gem_obj_ggtt_offset(obj), obj->base.size);
  1747. WARN_ON(i915_gem_obj_ggtt_bound(obj));
  1748. ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
  1749. if (ret) {
  1750. DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
  1751. return ret;
  1752. }
  1753. vma->bound |= GLOBAL_BIND;
  1754. }
  1755. /* Clear any non-preallocated blocks */
  1756. drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
  1757. DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
  1758. hole_start, hole_end);
  1759. ggtt_vm->clear_range(ggtt_vm, hole_start,
  1760. hole_end - hole_start, true);
  1761. }
  1762. /* And finally clear the reserved guard page */
  1763. ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
  1764. if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
  1765. struct i915_hw_ppgtt *ppgtt;
  1766. ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
  1767. if (!ppgtt)
  1768. return -ENOMEM;
  1769. ret = __hw_ppgtt_init(dev, ppgtt);
  1770. if (ret) {
  1771. ppgtt->base.cleanup(&ppgtt->base);
  1772. kfree(ppgtt);
  1773. return ret;
  1774. }
  1775. if (ppgtt->base.allocate_va_range)
  1776. ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
  1777. ppgtt->base.total);
  1778. if (ret) {
  1779. ppgtt->base.cleanup(&ppgtt->base);
  1780. kfree(ppgtt);
  1781. return ret;
  1782. }
  1783. ppgtt->base.clear_range(&ppgtt->base,
  1784. ppgtt->base.start,
  1785. ppgtt->base.total,
  1786. true);
  1787. dev_priv->mm.aliasing_ppgtt = ppgtt;
  1788. }
  1789. return 0;
  1790. }
  1791. void i915_gem_init_global_gtt(struct drm_device *dev)
  1792. {
  1793. struct drm_i915_private *dev_priv = dev->dev_private;
  1794. u64 gtt_size, mappable_size;
  1795. gtt_size = dev_priv->gtt.base.total;
  1796. mappable_size = dev_priv->gtt.mappable_end;
  1797. i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
  1798. }
  1799. void i915_global_gtt_cleanup(struct drm_device *dev)
  1800. {
  1801. struct drm_i915_private *dev_priv = dev->dev_private;
  1802. struct i915_address_space *vm = &dev_priv->gtt.base;
  1803. if (dev_priv->mm.aliasing_ppgtt) {
  1804. struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
  1805. ppgtt->base.cleanup(&ppgtt->base);
  1806. }
  1807. if (drm_mm_initialized(&vm->mm)) {
  1808. if (intel_vgpu_active(dev))
  1809. intel_vgt_deballoon();
  1810. drm_mm_takedown(&vm->mm);
  1811. list_del(&vm->global_link);
  1812. }
  1813. vm->cleanup(vm);
  1814. }
  1815. static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
  1816. {
  1817. snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
  1818. snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
  1819. return snb_gmch_ctl << 20;
  1820. }
  1821. static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
  1822. {
  1823. bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
  1824. bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
  1825. if (bdw_gmch_ctl)
  1826. bdw_gmch_ctl = 1 << bdw_gmch_ctl;
  1827. #ifdef CONFIG_X86_32
  1828. /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
  1829. if (bdw_gmch_ctl > 4)
  1830. bdw_gmch_ctl = 4;
  1831. #endif
  1832. return bdw_gmch_ctl << 20;
  1833. }
  1834. static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
  1835. {
  1836. gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
  1837. gmch_ctrl &= SNB_GMCH_GGMS_MASK;
  1838. if (gmch_ctrl)
  1839. return 1 << (20 + gmch_ctrl);
  1840. return 0;
  1841. }
  1842. static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
  1843. {
  1844. snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
  1845. snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
  1846. return snb_gmch_ctl << 25; /* 32 MB units */
  1847. }
  1848. static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
  1849. {
  1850. bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  1851. bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
  1852. return bdw_gmch_ctl << 25; /* 32 MB units */
  1853. }
  1854. static size_t chv_get_stolen_size(u16 gmch_ctrl)
  1855. {
  1856. gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
  1857. gmch_ctrl &= SNB_GMCH_GMS_MASK;
  1858. /*
  1859. * 0x0 to 0x10: 32MB increments starting at 0MB
  1860. * 0x11 to 0x16: 4MB increments starting at 8MB
  1861. * 0x17 to 0x1d: 4MB increments start at 36MB
  1862. */
  1863. if (gmch_ctrl < 0x11)
  1864. return gmch_ctrl << 25;
  1865. else if (gmch_ctrl < 0x17)
  1866. return (gmch_ctrl - 0x11 + 2) << 22;
  1867. else
  1868. return (gmch_ctrl - 0x17 + 9) << 22;
  1869. }
  1870. static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
  1871. {
  1872. gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  1873. gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
  1874. if (gen9_gmch_ctl < 0xf0)
  1875. return gen9_gmch_ctl << 25; /* 32 MB units */
  1876. else
  1877. /* 4MB increments starting at 0xf0 for 4MB */
  1878. return (gen9_gmch_ctl - 0xf0 + 1) << 22;
  1879. }
  1880. static int ggtt_probe_common(struct drm_device *dev,
  1881. size_t gtt_size)
  1882. {
  1883. struct drm_i915_private *dev_priv = dev->dev_private;
  1884. struct i915_page_scratch *scratch_page;
  1885. phys_addr_t gtt_phys_addr;
  1886. /* For Modern GENs the PTEs and register space are split in the BAR */
  1887. gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
  1888. (pci_resource_len(dev->pdev, 0) / 2);
  1889. /*
  1890. * On BXT writes larger than 64 bit to the GTT pagetable range will be
  1891. * dropped. For WC mappings in general we have 64 byte burst writes
  1892. * when the WC buffer is flushed, so we can't use it, but have to
  1893. * resort to an uncached mapping. The WC issue is easily caught by the
  1894. * readback check when writing GTT PTE entries.
  1895. */
  1896. if (IS_BROXTON(dev))
  1897. dev_priv->gtt.gsm = ioremap_nocache(gtt_phys_addr, gtt_size);
  1898. else
  1899. dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
  1900. if (!dev_priv->gtt.gsm) {
  1901. DRM_ERROR("Failed to map the gtt page table\n");
  1902. return -ENOMEM;
  1903. }
  1904. scratch_page = alloc_scratch_page(dev);
  1905. if (IS_ERR(scratch_page)) {
  1906. DRM_ERROR("Scratch setup failed\n");
  1907. /* iounmap will also get called at remove, but meh */
  1908. iounmap(dev_priv->gtt.gsm);
  1909. return PTR_ERR(scratch_page);
  1910. }
  1911. dev_priv->gtt.base.scratch_page = scratch_page;
  1912. return 0;
  1913. }
  1914. /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
  1915. * bits. When using advanced contexts each context stores its own PAT, but
  1916. * writing this data shouldn't be harmful even in those cases. */
  1917. static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
  1918. {
  1919. uint64_t pat;
  1920. pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
  1921. GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
  1922. GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
  1923. GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
  1924. GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
  1925. GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
  1926. GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
  1927. GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
  1928. if (!USES_PPGTT(dev_priv->dev))
  1929. /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
  1930. * so RTL will always use the value corresponding to
  1931. * pat_sel = 000".
  1932. * So let's disable cache for GGTT to avoid screen corruptions.
  1933. * MOCS still can be used though.
  1934. * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
  1935. * before this patch, i.e. the same uncached + snooping access
  1936. * like on gen6/7 seems to be in effect.
  1937. * - So this just fixes blitter/render access. Again it looks
  1938. * like it's not just uncached access, but uncached + snooping.
  1939. * So we can still hold onto all our assumptions wrt cpu
  1940. * clflushing on LLC machines.
  1941. */
  1942. pat = GEN8_PPAT(0, GEN8_PPAT_UC);
  1943. /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
  1944. * write would work. */
  1945. I915_WRITE(GEN8_PRIVATE_PAT, pat);
  1946. I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
  1947. }
  1948. static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
  1949. {
  1950. uint64_t pat;
  1951. /*
  1952. * Map WB on BDW to snooped on CHV.
  1953. *
  1954. * Only the snoop bit has meaning for CHV, the rest is
  1955. * ignored.
  1956. *
  1957. * The hardware will never snoop for certain types of accesses:
  1958. * - CPU GTT (GMADR->GGTT->no snoop->memory)
  1959. * - PPGTT page tables
  1960. * - some other special cycles
  1961. *
  1962. * As with BDW, we also need to consider the following for GT accesses:
  1963. * "For GGTT, there is NO pat_sel[2:0] from the entry,
  1964. * so RTL will always use the value corresponding to
  1965. * pat_sel = 000".
  1966. * Which means we must set the snoop bit in PAT entry 0
  1967. * in order to keep the global status page working.
  1968. */
  1969. pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
  1970. GEN8_PPAT(1, 0) |
  1971. GEN8_PPAT(2, 0) |
  1972. GEN8_PPAT(3, 0) |
  1973. GEN8_PPAT(4, CHV_PPAT_SNOOP) |
  1974. GEN8_PPAT(5, CHV_PPAT_SNOOP) |
  1975. GEN8_PPAT(6, CHV_PPAT_SNOOP) |
  1976. GEN8_PPAT(7, CHV_PPAT_SNOOP);
  1977. I915_WRITE(GEN8_PRIVATE_PAT, pat);
  1978. I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
  1979. }
  1980. static int gen8_gmch_probe(struct drm_device *dev,
  1981. u64 *gtt_total,
  1982. size_t *stolen,
  1983. phys_addr_t *mappable_base,
  1984. u64 *mappable_end)
  1985. {
  1986. struct drm_i915_private *dev_priv = dev->dev_private;
  1987. u64 gtt_size;
  1988. u16 snb_gmch_ctl;
  1989. int ret;
  1990. /* TODO: We're not aware of mappable constraints on gen8 yet */
  1991. *mappable_base = pci_resource_start(dev->pdev, 2);
  1992. *mappable_end = pci_resource_len(dev->pdev, 2);
  1993. if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
  1994. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
  1995. pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  1996. if (INTEL_INFO(dev)->gen >= 9) {
  1997. *stolen = gen9_get_stolen_size(snb_gmch_ctl);
  1998. gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
  1999. } else if (IS_CHERRYVIEW(dev)) {
  2000. *stolen = chv_get_stolen_size(snb_gmch_ctl);
  2001. gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
  2002. } else {
  2003. *stolen = gen8_get_stolen_size(snb_gmch_ctl);
  2004. gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
  2005. }
  2006. *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
  2007. if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
  2008. chv_setup_private_ppat(dev_priv);
  2009. else
  2010. bdw_setup_private_ppat(dev_priv);
  2011. ret = ggtt_probe_common(dev, gtt_size);
  2012. dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
  2013. dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
  2014. dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
  2015. dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
  2016. return ret;
  2017. }
  2018. static int gen6_gmch_probe(struct drm_device *dev,
  2019. u64 *gtt_total,
  2020. size_t *stolen,
  2021. phys_addr_t *mappable_base,
  2022. u64 *mappable_end)
  2023. {
  2024. struct drm_i915_private *dev_priv = dev->dev_private;
  2025. unsigned int gtt_size;
  2026. u16 snb_gmch_ctl;
  2027. int ret;
  2028. *mappable_base = pci_resource_start(dev->pdev, 2);
  2029. *mappable_end = pci_resource_len(dev->pdev, 2);
  2030. /* 64/512MB is the current min/max we actually know of, but this is just
  2031. * a coarse sanity check.
  2032. */
  2033. if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
  2034. DRM_ERROR("Unknown GMADR size (%llx)\n",
  2035. dev_priv->gtt.mappable_end);
  2036. return -ENXIO;
  2037. }
  2038. if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
  2039. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
  2040. pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  2041. *stolen = gen6_get_stolen_size(snb_gmch_ctl);
  2042. gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
  2043. *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
  2044. ret = ggtt_probe_common(dev, gtt_size);
  2045. dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
  2046. dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
  2047. dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
  2048. dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
  2049. return ret;
  2050. }
  2051. static void gen6_gmch_remove(struct i915_address_space *vm)
  2052. {
  2053. struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
  2054. iounmap(gtt->gsm);
  2055. free_scratch_page(vm->dev, vm->scratch_page);
  2056. }
  2057. static int i915_gmch_probe(struct drm_device *dev,
  2058. u64 *gtt_total,
  2059. size_t *stolen,
  2060. phys_addr_t *mappable_base,
  2061. u64 *mappable_end)
  2062. {
  2063. struct drm_i915_private *dev_priv = dev->dev_private;
  2064. int ret;
  2065. ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
  2066. if (!ret) {
  2067. DRM_ERROR("failed to set up gmch\n");
  2068. return -EIO;
  2069. }
  2070. intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
  2071. dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
  2072. dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries;
  2073. dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
  2074. dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
  2075. dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
  2076. if (unlikely(dev_priv->gtt.do_idle_maps))
  2077. DRM_INFO("applying Ironlake quirks for intel_iommu\n");
  2078. return 0;
  2079. }
  2080. static void i915_gmch_remove(struct i915_address_space *vm)
  2081. {
  2082. intel_gmch_remove();
  2083. }
  2084. int i915_gem_gtt_init(struct drm_device *dev)
  2085. {
  2086. struct drm_i915_private *dev_priv = dev->dev_private;
  2087. struct i915_gtt *gtt = &dev_priv->gtt;
  2088. int ret;
  2089. if (INTEL_INFO(dev)->gen <= 5) {
  2090. gtt->gtt_probe = i915_gmch_probe;
  2091. gtt->base.cleanup = i915_gmch_remove;
  2092. } else if (INTEL_INFO(dev)->gen < 8) {
  2093. gtt->gtt_probe = gen6_gmch_probe;
  2094. gtt->base.cleanup = gen6_gmch_remove;
  2095. if (IS_HASWELL(dev) && dev_priv->ellc_size)
  2096. gtt->base.pte_encode = iris_pte_encode;
  2097. else if (IS_HASWELL(dev))
  2098. gtt->base.pte_encode = hsw_pte_encode;
  2099. else if (IS_VALLEYVIEW(dev))
  2100. gtt->base.pte_encode = byt_pte_encode;
  2101. else if (INTEL_INFO(dev)->gen >= 7)
  2102. gtt->base.pte_encode = ivb_pte_encode;
  2103. else
  2104. gtt->base.pte_encode = snb_pte_encode;
  2105. } else {
  2106. dev_priv->gtt.gtt_probe = gen8_gmch_probe;
  2107. dev_priv->gtt.base.cleanup = gen6_gmch_remove;
  2108. }
  2109. gtt->base.dev = dev;
  2110. ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
  2111. &gtt->mappable_base, &gtt->mappable_end);
  2112. if (ret)
  2113. return ret;
  2114. /* GMADR is the PCI mmio aperture into the global GTT. */
  2115. DRM_INFO("Memory usable by graphics device = %lluM\n",
  2116. gtt->base.total >> 20);
  2117. DRM_DEBUG_DRIVER("GMADR size = %lldM\n", gtt->mappable_end >> 20);
  2118. DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
  2119. #ifdef CONFIG_INTEL_IOMMU
  2120. if (intel_iommu_gfx_mapped)
  2121. DRM_INFO("VT-d active for gfx access\n");
  2122. #endif
  2123. /*
  2124. * i915.enable_ppgtt is read-only, so do an early pass to validate the
  2125. * user's requested state against the hardware/driver capabilities. We
  2126. * do this now so that we can print out any log messages once rather
  2127. * than every time we check intel_enable_ppgtt().
  2128. */
  2129. i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
  2130. DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
  2131. return 0;
  2132. }
  2133. void i915_gem_restore_gtt_mappings(struct drm_device *dev)
  2134. {
  2135. struct drm_i915_private *dev_priv = dev->dev_private;
  2136. struct drm_i915_gem_object *obj;
  2137. struct i915_address_space *vm;
  2138. struct i915_vma *vma;
  2139. bool flush;
  2140. i915_check_and_clear_faults(dev);
  2141. /* First fill our portion of the GTT with scratch pages */
  2142. dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
  2143. dev_priv->gtt.base.start,
  2144. dev_priv->gtt.base.total,
  2145. true);
  2146. /* Cache flush objects bound into GGTT and rebind them. */
  2147. vm = &dev_priv->gtt.base;
  2148. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  2149. flush = false;
  2150. list_for_each_entry(vma, &obj->vma_list, vma_link) {
  2151. if (vma->vm != vm)
  2152. continue;
  2153. WARN_ON(i915_vma_bind(vma, obj->cache_level,
  2154. PIN_UPDATE));
  2155. flush = true;
  2156. }
  2157. if (flush)
  2158. i915_gem_clflush_object(obj, obj->pin_display);
  2159. }
  2160. if (INTEL_INFO(dev)->gen >= 8) {
  2161. if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
  2162. chv_setup_private_ppat(dev_priv);
  2163. else
  2164. bdw_setup_private_ppat(dev_priv);
  2165. return;
  2166. }
  2167. if (USES_PPGTT(dev)) {
  2168. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  2169. /* TODO: Perhaps it shouldn't be gen6 specific */
  2170. struct i915_hw_ppgtt *ppgtt =
  2171. container_of(vm, struct i915_hw_ppgtt,
  2172. base);
  2173. if (i915_is_ggtt(vm))
  2174. ppgtt = dev_priv->mm.aliasing_ppgtt;
  2175. gen6_write_page_range(dev_priv, &ppgtt->pd,
  2176. 0, ppgtt->base.total);
  2177. }
  2178. }
  2179. i915_ggtt_flush(dev_priv);
  2180. }
  2181. static struct i915_vma *
  2182. __i915_gem_vma_create(struct drm_i915_gem_object *obj,
  2183. struct i915_address_space *vm,
  2184. const struct i915_ggtt_view *ggtt_view)
  2185. {
  2186. struct i915_vma *vma;
  2187. if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
  2188. return ERR_PTR(-EINVAL);
  2189. vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
  2190. if (vma == NULL)
  2191. return ERR_PTR(-ENOMEM);
  2192. INIT_LIST_HEAD(&vma->vma_link);
  2193. INIT_LIST_HEAD(&vma->mm_list);
  2194. INIT_LIST_HEAD(&vma->exec_list);
  2195. vma->vm = vm;
  2196. vma->obj = obj;
  2197. if (i915_is_ggtt(vm))
  2198. vma->ggtt_view = *ggtt_view;
  2199. list_add_tail(&vma->vma_link, &obj->vma_list);
  2200. if (!i915_is_ggtt(vm))
  2201. i915_ppgtt_get(i915_vm_to_ppgtt(vm));
  2202. return vma;
  2203. }
  2204. struct i915_vma *
  2205. i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
  2206. struct i915_address_space *vm)
  2207. {
  2208. struct i915_vma *vma;
  2209. vma = i915_gem_obj_to_vma(obj, vm);
  2210. if (!vma)
  2211. vma = __i915_gem_vma_create(obj, vm,
  2212. i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
  2213. return vma;
  2214. }
  2215. struct i915_vma *
  2216. i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
  2217. const struct i915_ggtt_view *view)
  2218. {
  2219. struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
  2220. struct i915_vma *vma;
  2221. if (WARN_ON(!view))
  2222. return ERR_PTR(-EINVAL);
  2223. vma = i915_gem_obj_to_ggtt_view(obj, view);
  2224. if (IS_ERR(vma))
  2225. return vma;
  2226. if (!vma)
  2227. vma = __i915_gem_vma_create(obj, ggtt, view);
  2228. return vma;
  2229. }
  2230. static void
  2231. rotate_pages(dma_addr_t *in, unsigned int width, unsigned int height,
  2232. struct sg_table *st)
  2233. {
  2234. unsigned int column, row;
  2235. unsigned int src_idx;
  2236. struct scatterlist *sg = st->sgl;
  2237. st->nents = 0;
  2238. for (column = 0; column < width; column++) {
  2239. src_idx = width * (height - 1) + column;
  2240. for (row = 0; row < height; row++) {
  2241. st->nents++;
  2242. /* We don't need the pages, but need to initialize
  2243. * the entries so the sg list can be happily traversed.
  2244. * The only thing we need are DMA addresses.
  2245. */
  2246. sg_set_page(sg, NULL, PAGE_SIZE, 0);
  2247. sg_dma_address(sg) = in[src_idx];
  2248. sg_dma_len(sg) = PAGE_SIZE;
  2249. sg = sg_next(sg);
  2250. src_idx -= width;
  2251. }
  2252. }
  2253. }
  2254. static struct sg_table *
  2255. intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view,
  2256. struct drm_i915_gem_object *obj)
  2257. {
  2258. struct intel_rotation_info *rot_info = &ggtt_view->rotation_info;
  2259. unsigned int size_pages = rot_info->size >> PAGE_SHIFT;
  2260. struct sg_page_iter sg_iter;
  2261. unsigned long i;
  2262. dma_addr_t *page_addr_list;
  2263. struct sg_table *st;
  2264. int ret = -ENOMEM;
  2265. /* Allocate a temporary list of source pages for random access. */
  2266. page_addr_list = drm_malloc_ab(obj->base.size / PAGE_SIZE,
  2267. sizeof(dma_addr_t));
  2268. if (!page_addr_list)
  2269. return ERR_PTR(ret);
  2270. /* Allocate target SG list. */
  2271. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2272. if (!st)
  2273. goto err_st_alloc;
  2274. ret = sg_alloc_table(st, size_pages, GFP_KERNEL);
  2275. if (ret)
  2276. goto err_sg_alloc;
  2277. /* Populate source page list from the object. */
  2278. i = 0;
  2279. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  2280. page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
  2281. i++;
  2282. }
  2283. /* Rotate the pages. */
  2284. rotate_pages(page_addr_list,
  2285. rot_info->width_pages, rot_info->height_pages,
  2286. st);
  2287. DRM_DEBUG_KMS(
  2288. "Created rotated page mapping for object size %zu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %u pages).\n",
  2289. obj->base.size, rot_info->pitch, rot_info->height,
  2290. rot_info->pixel_format, rot_info->width_pages,
  2291. rot_info->height_pages, size_pages);
  2292. drm_free_large(page_addr_list);
  2293. return st;
  2294. err_sg_alloc:
  2295. kfree(st);
  2296. err_st_alloc:
  2297. drm_free_large(page_addr_list);
  2298. DRM_DEBUG_KMS(
  2299. "Failed to create rotated mapping for object size %zu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %u pages)\n",
  2300. obj->base.size, ret, rot_info->pitch, rot_info->height,
  2301. rot_info->pixel_format, rot_info->width_pages,
  2302. rot_info->height_pages, size_pages);
  2303. return ERR_PTR(ret);
  2304. }
  2305. static struct sg_table *
  2306. intel_partial_pages(const struct i915_ggtt_view *view,
  2307. struct drm_i915_gem_object *obj)
  2308. {
  2309. struct sg_table *st;
  2310. struct scatterlist *sg;
  2311. struct sg_page_iter obj_sg_iter;
  2312. int ret = -ENOMEM;
  2313. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2314. if (!st)
  2315. goto err_st_alloc;
  2316. ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL);
  2317. if (ret)
  2318. goto err_sg_alloc;
  2319. sg = st->sgl;
  2320. st->nents = 0;
  2321. for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents,
  2322. view->params.partial.offset)
  2323. {
  2324. if (st->nents >= view->params.partial.size)
  2325. break;
  2326. sg_set_page(sg, NULL, PAGE_SIZE, 0);
  2327. sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter);
  2328. sg_dma_len(sg) = PAGE_SIZE;
  2329. sg = sg_next(sg);
  2330. st->nents++;
  2331. }
  2332. return st;
  2333. err_sg_alloc:
  2334. kfree(st);
  2335. err_st_alloc:
  2336. return ERR_PTR(ret);
  2337. }
  2338. static int
  2339. i915_get_ggtt_vma_pages(struct i915_vma *vma)
  2340. {
  2341. int ret = 0;
  2342. if (vma->ggtt_view.pages)
  2343. return 0;
  2344. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
  2345. vma->ggtt_view.pages = vma->obj->pages;
  2346. else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
  2347. vma->ggtt_view.pages =
  2348. intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj);
  2349. else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
  2350. vma->ggtt_view.pages =
  2351. intel_partial_pages(&vma->ggtt_view, vma->obj);
  2352. else
  2353. WARN_ONCE(1, "GGTT view %u not implemented!\n",
  2354. vma->ggtt_view.type);
  2355. if (!vma->ggtt_view.pages) {
  2356. DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
  2357. vma->ggtt_view.type);
  2358. ret = -EINVAL;
  2359. } else if (IS_ERR(vma->ggtt_view.pages)) {
  2360. ret = PTR_ERR(vma->ggtt_view.pages);
  2361. vma->ggtt_view.pages = NULL;
  2362. DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
  2363. vma->ggtt_view.type, ret);
  2364. }
  2365. return ret;
  2366. }
  2367. /**
  2368. * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
  2369. * @vma: VMA to map
  2370. * @cache_level: mapping cache level
  2371. * @flags: flags like global or local mapping
  2372. *
  2373. * DMA addresses are taken from the scatter-gather table of this object (or of
  2374. * this VMA in case of non-default GGTT views) and PTE entries set up.
  2375. * Note that DMA addresses are also the only part of the SG table we care about.
  2376. */
  2377. int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
  2378. u32 flags)
  2379. {
  2380. int ret;
  2381. u32 bind_flags;
  2382. if (WARN_ON(flags == 0))
  2383. return -EINVAL;
  2384. bind_flags = 0;
  2385. if (flags & PIN_GLOBAL)
  2386. bind_flags |= GLOBAL_BIND;
  2387. if (flags & PIN_USER)
  2388. bind_flags |= LOCAL_BIND;
  2389. if (flags & PIN_UPDATE)
  2390. bind_flags |= vma->bound;
  2391. else
  2392. bind_flags &= ~vma->bound;
  2393. if (bind_flags == 0)
  2394. return 0;
  2395. if (vma->bound == 0 && vma->vm->allocate_va_range) {
  2396. trace_i915_va_alloc(vma->vm,
  2397. vma->node.start,
  2398. vma->node.size,
  2399. VM_TO_TRACE_NAME(vma->vm));
  2400. /* XXX: i915_vma_pin() will fix this +- hack */
  2401. vma->pin_count++;
  2402. ret = vma->vm->allocate_va_range(vma->vm,
  2403. vma->node.start,
  2404. vma->node.size);
  2405. vma->pin_count--;
  2406. if (ret)
  2407. return ret;
  2408. }
  2409. ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
  2410. if (ret)
  2411. return ret;
  2412. vma->bound |= bind_flags;
  2413. return 0;
  2414. }
  2415. /**
  2416. * i915_ggtt_view_size - Get the size of a GGTT view.
  2417. * @obj: Object the view is of.
  2418. * @view: The view in question.
  2419. *
  2420. * @return The size of the GGTT view in bytes.
  2421. */
  2422. size_t
  2423. i915_ggtt_view_size(struct drm_i915_gem_object *obj,
  2424. const struct i915_ggtt_view *view)
  2425. {
  2426. if (view->type == I915_GGTT_VIEW_NORMAL) {
  2427. return obj->base.size;
  2428. } else if (view->type == I915_GGTT_VIEW_ROTATED) {
  2429. return view->rotation_info.size;
  2430. } else if (view->type == I915_GGTT_VIEW_PARTIAL) {
  2431. return view->params.partial.size << PAGE_SHIFT;
  2432. } else {
  2433. WARN_ONCE(1, "GGTT view %u not implemented!\n", view->type);
  2434. return obj->base.size;
  2435. }
  2436. }