i915_gem_gtt.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751
  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/log2.h>
  26. #include <linux/random.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/stop_machine.h>
  29. #include <drm/drmP.h>
  30. #include <drm/i915_drm.h>
  31. #include "i915_drv.h"
  32. #include "i915_vgpu.h"
  33. #include "i915_trace.h"
  34. #include "intel_drv.h"
  35. #include "intel_frontbuffer.h"
  36. #define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
  37. /**
  38. * DOC: Global GTT views
  39. *
  40. * Background and previous state
  41. *
  42. * Historically objects could exists (be bound) in global GTT space only as
  43. * singular instances with a view representing all of the object's backing pages
  44. * in a linear fashion. This view will be called a normal view.
  45. *
  46. * To support multiple views of the same object, where the number of mapped
  47. * pages is not equal to the backing store, or where the layout of the pages
  48. * is not linear, concept of a GGTT view was added.
  49. *
  50. * One example of an alternative view is a stereo display driven by a single
  51. * image. In this case we would have a framebuffer looking like this
  52. * (2x2 pages):
  53. *
  54. * 12
  55. * 34
  56. *
  57. * Above would represent a normal GGTT view as normally mapped for GPU or CPU
  58. * rendering. In contrast, fed to the display engine would be an alternative
  59. * view which could look something like this:
  60. *
  61. * 1212
  62. * 3434
  63. *
  64. * In this example both the size and layout of pages in the alternative view is
  65. * different from the normal view.
  66. *
  67. * Implementation and usage
  68. *
  69. * GGTT views are implemented using VMAs and are distinguished via enum
  70. * i915_ggtt_view_type and struct i915_ggtt_view.
  71. *
  72. * A new flavour of core GEM functions which work with GGTT bound objects were
  73. * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
  74. * renaming in large amounts of code. They take the struct i915_ggtt_view
  75. * parameter encapsulating all metadata required to implement a view.
  76. *
  77. * As a helper for callers which are only interested in the normal view,
  78. * globally const i915_ggtt_view_normal singleton instance exists. All old core
  79. * GEM API functions, the ones not taking the view parameter, are operating on,
  80. * or with the normal GGTT view.
  81. *
  82. * Code wanting to add or use a new GGTT view needs to:
  83. *
  84. * 1. Add a new enum with a suitable name.
  85. * 2. Extend the metadata in the i915_ggtt_view structure if required.
  86. * 3. Add support to i915_get_vma_pages().
  87. *
  88. * New views are required to build a scatter-gather table from within the
  89. * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
  90. * exists for the lifetime of an VMA.
  91. *
  92. * Core API is designed to have copy semantics which means that passed in
  93. * struct i915_ggtt_view does not need to be persistent (left around after
  94. * calling the core API functions).
  95. *
  96. */
  97. static int
  98. i915_get_ggtt_vma_pages(struct i915_vma *vma);
  99. static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
  100. {
  101. /* Note that as an uncached mmio write, this should flush the
  102. * WCB of the writes into the GGTT before it triggers the invalidate.
  103. */
  104. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  105. }
  106. static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
  107. {
  108. gen6_ggtt_invalidate(dev_priv);
  109. I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
  110. }
  111. static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
  112. {
  113. intel_gtt_chipset_flush();
  114. }
  115. static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
  116. {
  117. i915->ggtt.invalidate(i915);
  118. }
  119. int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
  120. int enable_ppgtt)
  121. {
  122. bool has_aliasing_ppgtt;
  123. bool has_full_ppgtt;
  124. bool has_full_48bit_ppgtt;
  125. has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
  126. has_full_ppgtt = dev_priv->info.has_full_ppgtt;
  127. has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
  128. if (intel_vgpu_active(dev_priv)) {
  129. /* emulation is too hard */
  130. has_full_ppgtt = false;
  131. has_full_48bit_ppgtt = false;
  132. }
  133. if (!has_aliasing_ppgtt)
  134. return 0;
  135. /*
  136. * We don't allow disabling PPGTT for gen9+ as it's a requirement for
  137. * execlists, the sole mechanism available to submit work.
  138. */
  139. if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
  140. return 0;
  141. if (enable_ppgtt == 1)
  142. return 1;
  143. if (enable_ppgtt == 2 && has_full_ppgtt)
  144. return 2;
  145. if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
  146. return 3;
  147. #ifdef CONFIG_INTEL_IOMMU
  148. /* Disable ppgtt on SNB if VT-d is on. */
  149. if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
  150. DRM_INFO("Disabling PPGTT because VT-d is on\n");
  151. return 0;
  152. }
  153. #endif
  154. /* Early VLV doesn't have this */
  155. if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
  156. DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
  157. return 0;
  158. }
  159. if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
  160. return has_full_48bit_ppgtt ? 3 : 2;
  161. else
  162. return has_aliasing_ppgtt ? 1 : 0;
  163. }
  164. static int ppgtt_bind_vma(struct i915_vma *vma,
  165. enum i915_cache_level cache_level,
  166. u32 unused)
  167. {
  168. u32 pte_flags = 0;
  169. vma->pages = vma->obj->mm.pages;
  170. /* Currently applicable only to VLV */
  171. if (vma->obj->gt_ro)
  172. pte_flags |= PTE_READ_ONLY;
  173. vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
  174. cache_level, pte_flags);
  175. return 0;
  176. }
  177. static void ppgtt_unbind_vma(struct i915_vma *vma)
  178. {
  179. vma->vm->clear_range(vma->vm,
  180. vma->node.start,
  181. vma->size);
  182. }
  183. static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
  184. enum i915_cache_level level)
  185. {
  186. gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
  187. pte |= addr;
  188. switch (level) {
  189. case I915_CACHE_NONE:
  190. pte |= PPAT_UNCACHED_INDEX;
  191. break;
  192. case I915_CACHE_WT:
  193. pte |= PPAT_DISPLAY_ELLC_INDEX;
  194. break;
  195. default:
  196. pte |= PPAT_CACHED_INDEX;
  197. break;
  198. }
  199. return pte;
  200. }
  201. static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
  202. const enum i915_cache_level level)
  203. {
  204. gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
  205. pde |= addr;
  206. if (level != I915_CACHE_NONE)
  207. pde |= PPAT_CACHED_PDE_INDEX;
  208. else
  209. pde |= PPAT_UNCACHED_INDEX;
  210. return pde;
  211. }
  212. #define gen8_pdpe_encode gen8_pde_encode
  213. #define gen8_pml4e_encode gen8_pde_encode
  214. static gen6_pte_t snb_pte_encode(dma_addr_t addr,
  215. enum i915_cache_level level,
  216. u32 unused)
  217. {
  218. gen6_pte_t pte = GEN6_PTE_VALID;
  219. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  220. switch (level) {
  221. case I915_CACHE_L3_LLC:
  222. case I915_CACHE_LLC:
  223. pte |= GEN6_PTE_CACHE_LLC;
  224. break;
  225. case I915_CACHE_NONE:
  226. pte |= GEN6_PTE_UNCACHED;
  227. break;
  228. default:
  229. MISSING_CASE(level);
  230. }
  231. return pte;
  232. }
  233. static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
  234. enum i915_cache_level level,
  235. u32 unused)
  236. {
  237. gen6_pte_t pte = GEN6_PTE_VALID;
  238. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  239. switch (level) {
  240. case I915_CACHE_L3_LLC:
  241. pte |= GEN7_PTE_CACHE_L3_LLC;
  242. break;
  243. case I915_CACHE_LLC:
  244. pte |= GEN6_PTE_CACHE_LLC;
  245. break;
  246. case I915_CACHE_NONE:
  247. pte |= GEN6_PTE_UNCACHED;
  248. break;
  249. default:
  250. MISSING_CASE(level);
  251. }
  252. return pte;
  253. }
  254. static gen6_pte_t byt_pte_encode(dma_addr_t addr,
  255. enum i915_cache_level level,
  256. u32 flags)
  257. {
  258. gen6_pte_t pte = GEN6_PTE_VALID;
  259. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  260. if (!(flags & PTE_READ_ONLY))
  261. pte |= BYT_PTE_WRITEABLE;
  262. if (level != I915_CACHE_NONE)
  263. pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
  264. return pte;
  265. }
  266. static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
  267. enum i915_cache_level level,
  268. u32 unused)
  269. {
  270. gen6_pte_t pte = GEN6_PTE_VALID;
  271. pte |= HSW_PTE_ADDR_ENCODE(addr);
  272. if (level != I915_CACHE_NONE)
  273. pte |= HSW_WB_LLC_AGE3;
  274. return pte;
  275. }
  276. static gen6_pte_t iris_pte_encode(dma_addr_t addr,
  277. enum i915_cache_level level,
  278. u32 unused)
  279. {
  280. gen6_pte_t pte = GEN6_PTE_VALID;
  281. pte |= HSW_PTE_ADDR_ENCODE(addr);
  282. switch (level) {
  283. case I915_CACHE_NONE:
  284. break;
  285. case I915_CACHE_WT:
  286. pte |= HSW_WT_ELLC_LLC_AGE3;
  287. break;
  288. default:
  289. pte |= HSW_WB_ELLC_LLC_AGE3;
  290. break;
  291. }
  292. return pte;
  293. }
  294. static int __setup_page_dma(struct drm_i915_private *dev_priv,
  295. struct i915_page_dma *p, gfp_t flags)
  296. {
  297. struct device *kdev = &dev_priv->drm.pdev->dev;
  298. p->page = alloc_page(flags);
  299. if (!p->page)
  300. return -ENOMEM;
  301. p->daddr = dma_map_page(kdev,
  302. p->page, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  303. if (dma_mapping_error(kdev, p->daddr)) {
  304. __free_page(p->page);
  305. return -EINVAL;
  306. }
  307. return 0;
  308. }
  309. static int setup_page_dma(struct drm_i915_private *dev_priv,
  310. struct i915_page_dma *p)
  311. {
  312. return __setup_page_dma(dev_priv, p, I915_GFP_DMA);
  313. }
  314. static void cleanup_page_dma(struct drm_i915_private *dev_priv,
  315. struct i915_page_dma *p)
  316. {
  317. struct pci_dev *pdev = dev_priv->drm.pdev;
  318. if (WARN_ON(!p->page))
  319. return;
  320. dma_unmap_page(&pdev->dev, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  321. __free_page(p->page);
  322. memset(p, 0, sizeof(*p));
  323. }
  324. static void *kmap_page_dma(struct i915_page_dma *p)
  325. {
  326. return kmap_atomic(p->page);
  327. }
  328. /* We use the flushing unmap only with ppgtt structures:
  329. * page directories, page tables and scratch pages.
  330. */
  331. static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
  332. {
  333. /* There are only few exceptions for gen >=6. chv and bxt.
  334. * And we are not sure about the latter so play safe for now.
  335. */
  336. if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
  337. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  338. kunmap_atomic(vaddr);
  339. }
  340. #define kmap_px(px) kmap_page_dma(px_base(px))
  341. #define kunmap_px(ppgtt, vaddr) \
  342. kunmap_page_dma((ppgtt)->base.i915, (vaddr))
  343. #define setup_px(dev_priv, px) setup_page_dma((dev_priv), px_base(px))
  344. #define cleanup_px(dev_priv, px) cleanup_page_dma((dev_priv), px_base(px))
  345. #define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
  346. #define fill32_px(dev_priv, px, v) \
  347. fill_page_dma_32((dev_priv), px_base(px), (v))
  348. static void fill_page_dma(struct drm_i915_private *dev_priv,
  349. struct i915_page_dma *p, const uint64_t val)
  350. {
  351. int i;
  352. uint64_t * const vaddr = kmap_page_dma(p);
  353. for (i = 0; i < 512; i++)
  354. vaddr[i] = val;
  355. kunmap_page_dma(dev_priv, vaddr);
  356. }
  357. static void fill_page_dma_32(struct drm_i915_private *dev_priv,
  358. struct i915_page_dma *p, const uint32_t val32)
  359. {
  360. uint64_t v = val32;
  361. v = v << 32 | val32;
  362. fill_page_dma(dev_priv, p, v);
  363. }
  364. static int
  365. setup_scratch_page(struct drm_i915_private *dev_priv,
  366. struct i915_page_dma *scratch,
  367. gfp_t gfp)
  368. {
  369. return __setup_page_dma(dev_priv, scratch, gfp | __GFP_ZERO);
  370. }
  371. static void cleanup_scratch_page(struct drm_i915_private *dev_priv,
  372. struct i915_page_dma *scratch)
  373. {
  374. cleanup_page_dma(dev_priv, scratch);
  375. }
  376. static struct i915_page_table *alloc_pt(struct drm_i915_private *dev_priv)
  377. {
  378. struct i915_page_table *pt;
  379. const size_t count = INTEL_GEN(dev_priv) >= 8 ? GEN8_PTES : GEN6_PTES;
  380. int ret = -ENOMEM;
  381. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  382. if (!pt)
  383. return ERR_PTR(-ENOMEM);
  384. pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
  385. GFP_KERNEL);
  386. if (!pt->used_ptes)
  387. goto fail_bitmap;
  388. ret = setup_px(dev_priv, pt);
  389. if (ret)
  390. goto fail_page_m;
  391. return pt;
  392. fail_page_m:
  393. kfree(pt->used_ptes);
  394. fail_bitmap:
  395. kfree(pt);
  396. return ERR_PTR(ret);
  397. }
  398. static void free_pt(struct drm_i915_private *dev_priv,
  399. struct i915_page_table *pt)
  400. {
  401. cleanup_px(dev_priv, pt);
  402. kfree(pt->used_ptes);
  403. kfree(pt);
  404. }
  405. static void gen8_initialize_pt(struct i915_address_space *vm,
  406. struct i915_page_table *pt)
  407. {
  408. gen8_pte_t scratch_pte;
  409. scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
  410. I915_CACHE_LLC);
  411. fill_px(vm->i915, pt, scratch_pte);
  412. }
  413. static void gen6_initialize_pt(struct i915_address_space *vm,
  414. struct i915_page_table *pt)
  415. {
  416. gen6_pte_t scratch_pte;
  417. WARN_ON(vm->scratch_page.daddr == 0);
  418. scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
  419. I915_CACHE_LLC, 0);
  420. fill32_px(vm->i915, pt, scratch_pte);
  421. }
  422. static struct i915_page_directory *alloc_pd(struct drm_i915_private *dev_priv)
  423. {
  424. struct i915_page_directory *pd;
  425. int ret = -ENOMEM;
  426. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  427. if (!pd)
  428. return ERR_PTR(-ENOMEM);
  429. pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
  430. sizeof(*pd->used_pdes), GFP_KERNEL);
  431. if (!pd->used_pdes)
  432. goto fail_bitmap;
  433. ret = setup_px(dev_priv, pd);
  434. if (ret)
  435. goto fail_page_m;
  436. return pd;
  437. fail_page_m:
  438. kfree(pd->used_pdes);
  439. fail_bitmap:
  440. kfree(pd);
  441. return ERR_PTR(ret);
  442. }
  443. static void free_pd(struct drm_i915_private *dev_priv,
  444. struct i915_page_directory *pd)
  445. {
  446. if (px_page(pd)) {
  447. cleanup_px(dev_priv, pd);
  448. kfree(pd->used_pdes);
  449. kfree(pd);
  450. }
  451. }
  452. static void gen8_initialize_pd(struct i915_address_space *vm,
  453. struct i915_page_directory *pd)
  454. {
  455. gen8_pde_t scratch_pde;
  456. scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
  457. fill_px(vm->i915, pd, scratch_pde);
  458. }
  459. static int __pdp_init(struct drm_i915_private *dev_priv,
  460. struct i915_page_directory_pointer *pdp)
  461. {
  462. size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
  463. pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
  464. sizeof(unsigned long),
  465. GFP_KERNEL);
  466. if (!pdp->used_pdpes)
  467. return -ENOMEM;
  468. pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
  469. GFP_KERNEL);
  470. if (!pdp->page_directory) {
  471. kfree(pdp->used_pdpes);
  472. /* the PDP might be the statically allocated top level. Keep it
  473. * as clean as possible */
  474. pdp->used_pdpes = NULL;
  475. return -ENOMEM;
  476. }
  477. return 0;
  478. }
  479. static void __pdp_fini(struct i915_page_directory_pointer *pdp)
  480. {
  481. kfree(pdp->used_pdpes);
  482. kfree(pdp->page_directory);
  483. pdp->page_directory = NULL;
  484. }
  485. static struct
  486. i915_page_directory_pointer *alloc_pdp(struct drm_i915_private *dev_priv)
  487. {
  488. struct i915_page_directory_pointer *pdp;
  489. int ret = -ENOMEM;
  490. WARN_ON(!USES_FULL_48BIT_PPGTT(dev_priv));
  491. pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
  492. if (!pdp)
  493. return ERR_PTR(-ENOMEM);
  494. ret = __pdp_init(dev_priv, pdp);
  495. if (ret)
  496. goto fail_bitmap;
  497. ret = setup_px(dev_priv, pdp);
  498. if (ret)
  499. goto fail_page_m;
  500. return pdp;
  501. fail_page_m:
  502. __pdp_fini(pdp);
  503. fail_bitmap:
  504. kfree(pdp);
  505. return ERR_PTR(ret);
  506. }
  507. static void free_pdp(struct drm_i915_private *dev_priv,
  508. struct i915_page_directory_pointer *pdp)
  509. {
  510. __pdp_fini(pdp);
  511. if (USES_FULL_48BIT_PPGTT(dev_priv)) {
  512. cleanup_px(dev_priv, pdp);
  513. kfree(pdp);
  514. }
  515. }
  516. static void gen8_initialize_pdp(struct i915_address_space *vm,
  517. struct i915_page_directory_pointer *pdp)
  518. {
  519. gen8_ppgtt_pdpe_t scratch_pdpe;
  520. scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
  521. fill_px(vm->i915, pdp, scratch_pdpe);
  522. }
  523. static void gen8_initialize_pml4(struct i915_address_space *vm,
  524. struct i915_pml4 *pml4)
  525. {
  526. gen8_ppgtt_pml4e_t scratch_pml4e;
  527. scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
  528. I915_CACHE_LLC);
  529. fill_px(vm->i915, pml4, scratch_pml4e);
  530. }
  531. static void
  532. gen8_setup_pdpe(struct i915_hw_ppgtt *ppgtt,
  533. struct i915_page_directory_pointer *pdp,
  534. struct i915_page_directory *pd,
  535. int index)
  536. {
  537. gen8_ppgtt_pdpe_t *page_directorypo;
  538. if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
  539. return;
  540. page_directorypo = kmap_px(pdp);
  541. page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
  542. kunmap_px(ppgtt, page_directorypo);
  543. }
  544. static void
  545. gen8_setup_pml4e(struct i915_hw_ppgtt *ppgtt,
  546. struct i915_pml4 *pml4,
  547. struct i915_page_directory_pointer *pdp,
  548. int index)
  549. {
  550. gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
  551. WARN_ON(!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)));
  552. pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
  553. kunmap_px(ppgtt, pagemap);
  554. }
  555. /* Broadwell Page Directory Pointer Descriptors */
  556. static int gen8_write_pdp(struct drm_i915_gem_request *req,
  557. unsigned entry,
  558. dma_addr_t addr)
  559. {
  560. struct intel_ring *ring = req->ring;
  561. struct intel_engine_cs *engine = req->engine;
  562. int ret;
  563. BUG_ON(entry >= 4);
  564. ret = intel_ring_begin(req, 6);
  565. if (ret)
  566. return ret;
  567. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  568. intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
  569. intel_ring_emit(ring, upper_32_bits(addr));
  570. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  571. intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
  572. intel_ring_emit(ring, lower_32_bits(addr));
  573. intel_ring_advance(ring);
  574. return 0;
  575. }
  576. static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
  577. struct drm_i915_gem_request *req)
  578. {
  579. int i, ret;
  580. for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
  581. const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
  582. ret = gen8_write_pdp(req, i, pd_daddr);
  583. if (ret)
  584. return ret;
  585. }
  586. return 0;
  587. }
  588. static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
  589. struct drm_i915_gem_request *req)
  590. {
  591. return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
  592. }
  593. /* PDE TLBs are a pain to invalidate on GEN8+. When we modify
  594. * the page table structures, we mark them dirty so that
  595. * context switching/execlist queuing code takes extra steps
  596. * to ensure that tlbs are flushed.
  597. */
  598. static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
  599. {
  600. ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
  601. }
  602. /* Removes entries from a single page table, releasing it if it's empty.
  603. * Caller can use the return value to update higher-level entries.
  604. */
  605. static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
  606. struct i915_page_table *pt,
  607. uint64_t start,
  608. uint64_t length)
  609. {
  610. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  611. unsigned int num_entries = gen8_pte_count(start, length);
  612. unsigned int pte = gen8_pte_index(start);
  613. unsigned int pte_end = pte + num_entries;
  614. gen8_pte_t *pt_vaddr;
  615. gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
  616. I915_CACHE_LLC);
  617. if (WARN_ON(!px_page(pt)))
  618. return false;
  619. GEM_BUG_ON(pte_end > GEN8_PTES);
  620. bitmap_clear(pt->used_ptes, pte, num_entries);
  621. if (bitmap_empty(pt->used_ptes, GEN8_PTES))
  622. return true;
  623. pt_vaddr = kmap_px(pt);
  624. while (pte < pte_end)
  625. pt_vaddr[pte++] = scratch_pte;
  626. kunmap_px(ppgtt, pt_vaddr);
  627. return false;
  628. }
  629. /* Removes entries from a single page dir, releasing it if it's empty.
  630. * Caller can use the return value to update higher-level entries
  631. */
  632. static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
  633. struct i915_page_directory *pd,
  634. uint64_t start,
  635. uint64_t length)
  636. {
  637. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  638. struct i915_page_table *pt;
  639. uint64_t pde;
  640. gen8_pde_t *pde_vaddr;
  641. gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
  642. I915_CACHE_LLC);
  643. gen8_for_each_pde(pt, pd, start, length, pde) {
  644. if (WARN_ON(!pd->page_table[pde]))
  645. break;
  646. if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
  647. __clear_bit(pde, pd->used_pdes);
  648. pde_vaddr = kmap_px(pd);
  649. pde_vaddr[pde] = scratch_pde;
  650. kunmap_px(ppgtt, pde_vaddr);
  651. free_pt(vm->i915, pt);
  652. }
  653. }
  654. if (bitmap_empty(pd->used_pdes, I915_PDES))
  655. return true;
  656. return false;
  657. }
  658. /* Removes entries from a single page dir pointer, releasing it if it's empty.
  659. * Caller can use the return value to update higher-level entries
  660. */
  661. static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
  662. struct i915_page_directory_pointer *pdp,
  663. uint64_t start,
  664. uint64_t length)
  665. {
  666. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  667. struct i915_page_directory *pd;
  668. uint64_t pdpe;
  669. gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
  670. if (WARN_ON(!pdp->page_directory[pdpe]))
  671. break;
  672. if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
  673. __clear_bit(pdpe, pdp->used_pdpes);
  674. gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
  675. free_pd(vm->i915, pd);
  676. }
  677. }
  678. mark_tlbs_dirty(ppgtt);
  679. if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
  680. return true;
  681. return false;
  682. }
  683. /* Removes entries from a single pml4.
  684. * This is the top-level structure in 4-level page tables used on gen8+.
  685. * Empty entries are always scratch pml4e.
  686. */
  687. static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
  688. struct i915_pml4 *pml4,
  689. uint64_t start,
  690. uint64_t length)
  691. {
  692. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  693. struct i915_page_directory_pointer *pdp;
  694. uint64_t pml4e;
  695. GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
  696. gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
  697. if (WARN_ON(!pml4->pdps[pml4e]))
  698. break;
  699. if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
  700. __clear_bit(pml4e, pml4->used_pml4es);
  701. gen8_setup_pml4e(ppgtt, pml4, vm->scratch_pdp, pml4e);
  702. free_pdp(vm->i915, pdp);
  703. }
  704. }
  705. }
  706. static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
  707. uint64_t start, uint64_t length)
  708. {
  709. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  710. if (USES_FULL_48BIT_PPGTT(vm->i915))
  711. gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
  712. else
  713. gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
  714. }
  715. static void
  716. gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
  717. struct i915_page_directory_pointer *pdp,
  718. struct sg_page_iter *sg_iter,
  719. uint64_t start,
  720. enum i915_cache_level cache_level)
  721. {
  722. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  723. gen8_pte_t *pt_vaddr;
  724. unsigned pdpe = gen8_pdpe_index(start);
  725. unsigned pde = gen8_pde_index(start);
  726. unsigned pte = gen8_pte_index(start);
  727. pt_vaddr = NULL;
  728. while (__sg_page_iter_next(sg_iter)) {
  729. if (pt_vaddr == NULL) {
  730. struct i915_page_directory *pd = pdp->page_directory[pdpe];
  731. struct i915_page_table *pt = pd->page_table[pde];
  732. pt_vaddr = kmap_px(pt);
  733. }
  734. pt_vaddr[pte] =
  735. gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
  736. cache_level);
  737. if (++pte == GEN8_PTES) {
  738. kunmap_px(ppgtt, pt_vaddr);
  739. pt_vaddr = NULL;
  740. if (++pde == I915_PDES) {
  741. if (++pdpe == I915_PDPES_PER_PDP(vm->i915))
  742. break;
  743. pde = 0;
  744. }
  745. pte = 0;
  746. }
  747. }
  748. if (pt_vaddr)
  749. kunmap_px(ppgtt, pt_vaddr);
  750. }
  751. static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
  752. struct sg_table *pages,
  753. uint64_t start,
  754. enum i915_cache_level cache_level,
  755. u32 unused)
  756. {
  757. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  758. struct sg_page_iter sg_iter;
  759. __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
  760. if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
  761. gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
  762. cache_level);
  763. } else {
  764. struct i915_page_directory_pointer *pdp;
  765. uint64_t pml4e;
  766. uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
  767. gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
  768. gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
  769. start, cache_level);
  770. }
  771. }
  772. }
  773. static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
  774. struct i915_page_directory *pd)
  775. {
  776. int i;
  777. if (!px_page(pd))
  778. return;
  779. for_each_set_bit(i, pd->used_pdes, I915_PDES) {
  780. if (WARN_ON(!pd->page_table[i]))
  781. continue;
  782. free_pt(dev_priv, pd->page_table[i]);
  783. pd->page_table[i] = NULL;
  784. }
  785. }
  786. static int gen8_init_scratch(struct i915_address_space *vm)
  787. {
  788. struct drm_i915_private *dev_priv = vm->i915;
  789. int ret;
  790. ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
  791. if (ret)
  792. return ret;
  793. vm->scratch_pt = alloc_pt(dev_priv);
  794. if (IS_ERR(vm->scratch_pt)) {
  795. ret = PTR_ERR(vm->scratch_pt);
  796. goto free_scratch_page;
  797. }
  798. vm->scratch_pd = alloc_pd(dev_priv);
  799. if (IS_ERR(vm->scratch_pd)) {
  800. ret = PTR_ERR(vm->scratch_pd);
  801. goto free_pt;
  802. }
  803. if (USES_FULL_48BIT_PPGTT(dev_priv)) {
  804. vm->scratch_pdp = alloc_pdp(dev_priv);
  805. if (IS_ERR(vm->scratch_pdp)) {
  806. ret = PTR_ERR(vm->scratch_pdp);
  807. goto free_pd;
  808. }
  809. }
  810. gen8_initialize_pt(vm, vm->scratch_pt);
  811. gen8_initialize_pd(vm, vm->scratch_pd);
  812. if (USES_FULL_48BIT_PPGTT(dev_priv))
  813. gen8_initialize_pdp(vm, vm->scratch_pdp);
  814. return 0;
  815. free_pd:
  816. free_pd(dev_priv, vm->scratch_pd);
  817. free_pt:
  818. free_pt(dev_priv, vm->scratch_pt);
  819. free_scratch_page:
  820. cleanup_scratch_page(dev_priv, &vm->scratch_page);
  821. return ret;
  822. }
  823. static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
  824. {
  825. enum vgt_g2v_type msg;
  826. struct drm_i915_private *dev_priv = ppgtt->base.i915;
  827. int i;
  828. if (USES_FULL_48BIT_PPGTT(dev_priv)) {
  829. u64 daddr = px_dma(&ppgtt->pml4);
  830. I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
  831. I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
  832. msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
  833. VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
  834. } else {
  835. for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
  836. u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
  837. I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
  838. I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
  839. }
  840. msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
  841. VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
  842. }
  843. I915_WRITE(vgtif_reg(g2v_notify), msg);
  844. return 0;
  845. }
  846. static void gen8_free_scratch(struct i915_address_space *vm)
  847. {
  848. struct drm_i915_private *dev_priv = vm->i915;
  849. if (USES_FULL_48BIT_PPGTT(dev_priv))
  850. free_pdp(dev_priv, vm->scratch_pdp);
  851. free_pd(dev_priv, vm->scratch_pd);
  852. free_pt(dev_priv, vm->scratch_pt);
  853. cleanup_scratch_page(dev_priv, &vm->scratch_page);
  854. }
  855. static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
  856. struct i915_page_directory_pointer *pdp)
  857. {
  858. int i;
  859. for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
  860. if (WARN_ON(!pdp->page_directory[i]))
  861. continue;
  862. gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
  863. free_pd(dev_priv, pdp->page_directory[i]);
  864. }
  865. free_pdp(dev_priv, pdp);
  866. }
  867. static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
  868. {
  869. struct drm_i915_private *dev_priv = ppgtt->base.i915;
  870. int i;
  871. for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
  872. if (WARN_ON(!ppgtt->pml4.pdps[i]))
  873. continue;
  874. gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
  875. }
  876. cleanup_px(dev_priv, &ppgtt->pml4);
  877. }
  878. static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
  879. {
  880. struct drm_i915_private *dev_priv = vm->i915;
  881. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  882. if (intel_vgpu_active(dev_priv))
  883. gen8_ppgtt_notify_vgt(ppgtt, false);
  884. if (!USES_FULL_48BIT_PPGTT(dev_priv))
  885. gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
  886. else
  887. gen8_ppgtt_cleanup_4lvl(ppgtt);
  888. gen8_free_scratch(vm);
  889. }
  890. /**
  891. * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
  892. * @vm: Master vm structure.
  893. * @pd: Page directory for this address range.
  894. * @start: Starting virtual address to begin allocations.
  895. * @length: Size of the allocations.
  896. * @new_pts: Bitmap set by function with new allocations. Likely used by the
  897. * caller to free on error.
  898. *
  899. * Allocate the required number of page tables. Extremely similar to
  900. * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
  901. * the page directory boundary (instead of the page directory pointer). That
  902. * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
  903. * possible, and likely that the caller will need to use multiple calls of this
  904. * function to achieve the appropriate allocation.
  905. *
  906. * Return: 0 if success; negative error code otherwise.
  907. */
  908. static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
  909. struct i915_page_directory *pd,
  910. uint64_t start,
  911. uint64_t length,
  912. unsigned long *new_pts)
  913. {
  914. struct drm_i915_private *dev_priv = vm->i915;
  915. struct i915_page_table *pt;
  916. uint32_t pde;
  917. gen8_for_each_pde(pt, pd, start, length, pde) {
  918. /* Don't reallocate page tables */
  919. if (test_bit(pde, pd->used_pdes)) {
  920. /* Scratch is never allocated this way */
  921. WARN_ON(pt == vm->scratch_pt);
  922. continue;
  923. }
  924. pt = alloc_pt(dev_priv);
  925. if (IS_ERR(pt))
  926. goto unwind_out;
  927. gen8_initialize_pt(vm, pt);
  928. pd->page_table[pde] = pt;
  929. __set_bit(pde, new_pts);
  930. trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
  931. }
  932. return 0;
  933. unwind_out:
  934. for_each_set_bit(pde, new_pts, I915_PDES)
  935. free_pt(dev_priv, pd->page_table[pde]);
  936. return -ENOMEM;
  937. }
  938. /**
  939. * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
  940. * @vm: Master vm structure.
  941. * @pdp: Page directory pointer for this address range.
  942. * @start: Starting virtual address to begin allocations.
  943. * @length: Size of the allocations.
  944. * @new_pds: Bitmap set by function with new allocations. Likely used by the
  945. * caller to free on error.
  946. *
  947. * Allocate the required number of page directories starting at the pde index of
  948. * @start, and ending at the pde index @start + @length. This function will skip
  949. * over already allocated page directories within the range, and only allocate
  950. * new ones, setting the appropriate pointer within the pdp as well as the
  951. * correct position in the bitmap @new_pds.
  952. *
  953. * The function will only allocate the pages within the range for a give page
  954. * directory pointer. In other words, if @start + @length straddles a virtually
  955. * addressed PDP boundary (512GB for 4k pages), there will be more allocations
  956. * required by the caller, This is not currently possible, and the BUG in the
  957. * code will prevent it.
  958. *
  959. * Return: 0 if success; negative error code otherwise.
  960. */
  961. static int
  962. gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
  963. struct i915_page_directory_pointer *pdp,
  964. uint64_t start,
  965. uint64_t length,
  966. unsigned long *new_pds)
  967. {
  968. struct drm_i915_private *dev_priv = vm->i915;
  969. struct i915_page_directory *pd;
  970. uint32_t pdpe;
  971. uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
  972. WARN_ON(!bitmap_empty(new_pds, pdpes));
  973. gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
  974. if (test_bit(pdpe, pdp->used_pdpes))
  975. continue;
  976. pd = alloc_pd(dev_priv);
  977. if (IS_ERR(pd))
  978. goto unwind_out;
  979. gen8_initialize_pd(vm, pd);
  980. pdp->page_directory[pdpe] = pd;
  981. __set_bit(pdpe, new_pds);
  982. trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
  983. }
  984. return 0;
  985. unwind_out:
  986. for_each_set_bit(pdpe, new_pds, pdpes)
  987. free_pd(dev_priv, pdp->page_directory[pdpe]);
  988. return -ENOMEM;
  989. }
  990. /**
  991. * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
  992. * @vm: Master vm structure.
  993. * @pml4: Page map level 4 for this address range.
  994. * @start: Starting virtual address to begin allocations.
  995. * @length: Size of the allocations.
  996. * @new_pdps: Bitmap set by function with new allocations. Likely used by the
  997. * caller to free on error.
  998. *
  999. * Allocate the required number of page directory pointers. Extremely similar to
  1000. * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
  1001. * The main difference is here we are limited by the pml4 boundary (instead of
  1002. * the page directory pointer).
  1003. *
  1004. * Return: 0 if success; negative error code otherwise.
  1005. */
  1006. static int
  1007. gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
  1008. struct i915_pml4 *pml4,
  1009. uint64_t start,
  1010. uint64_t length,
  1011. unsigned long *new_pdps)
  1012. {
  1013. struct drm_i915_private *dev_priv = vm->i915;
  1014. struct i915_page_directory_pointer *pdp;
  1015. uint32_t pml4e;
  1016. WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
  1017. gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
  1018. if (!test_bit(pml4e, pml4->used_pml4es)) {
  1019. pdp = alloc_pdp(dev_priv);
  1020. if (IS_ERR(pdp))
  1021. goto unwind_out;
  1022. gen8_initialize_pdp(vm, pdp);
  1023. pml4->pdps[pml4e] = pdp;
  1024. __set_bit(pml4e, new_pdps);
  1025. trace_i915_page_directory_pointer_entry_alloc(vm,
  1026. pml4e,
  1027. start,
  1028. GEN8_PML4E_SHIFT);
  1029. }
  1030. }
  1031. return 0;
  1032. unwind_out:
  1033. for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
  1034. free_pdp(dev_priv, pml4->pdps[pml4e]);
  1035. return -ENOMEM;
  1036. }
  1037. static void
  1038. free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
  1039. {
  1040. kfree(new_pts);
  1041. kfree(new_pds);
  1042. }
  1043. /* Fills in the page directory bitmap, and the array of page tables bitmap. Both
  1044. * of these are based on the number of PDPEs in the system.
  1045. */
  1046. static
  1047. int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
  1048. unsigned long **new_pts,
  1049. uint32_t pdpes)
  1050. {
  1051. unsigned long *pds;
  1052. unsigned long *pts;
  1053. pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
  1054. if (!pds)
  1055. return -ENOMEM;
  1056. pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
  1057. GFP_TEMPORARY);
  1058. if (!pts)
  1059. goto err_out;
  1060. *new_pds = pds;
  1061. *new_pts = pts;
  1062. return 0;
  1063. err_out:
  1064. free_gen8_temp_bitmaps(pds, pts);
  1065. return -ENOMEM;
  1066. }
  1067. static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
  1068. struct i915_page_directory_pointer *pdp,
  1069. uint64_t start,
  1070. uint64_t length)
  1071. {
  1072. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1073. unsigned long *new_page_dirs, *new_page_tables;
  1074. struct drm_i915_private *dev_priv = vm->i915;
  1075. struct i915_page_directory *pd;
  1076. const uint64_t orig_start = start;
  1077. const uint64_t orig_length = length;
  1078. uint32_t pdpe;
  1079. uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
  1080. int ret;
  1081. ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
  1082. if (ret)
  1083. return ret;
  1084. /* Do the allocations first so we can easily bail out */
  1085. ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
  1086. new_page_dirs);
  1087. if (ret) {
  1088. free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
  1089. return ret;
  1090. }
  1091. /* For every page directory referenced, allocate page tables */
  1092. gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
  1093. ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
  1094. new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
  1095. if (ret)
  1096. goto err_out;
  1097. }
  1098. start = orig_start;
  1099. length = orig_length;
  1100. /* Allocations have completed successfully, so set the bitmaps, and do
  1101. * the mappings. */
  1102. gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
  1103. gen8_pde_t *const page_directory = kmap_px(pd);
  1104. struct i915_page_table *pt;
  1105. uint64_t pd_len = length;
  1106. uint64_t pd_start = start;
  1107. uint32_t pde;
  1108. /* Every pd should be allocated, we just did that above. */
  1109. WARN_ON(!pd);
  1110. gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
  1111. /* Same reasoning as pd */
  1112. WARN_ON(!pt);
  1113. WARN_ON(!pd_len);
  1114. WARN_ON(!gen8_pte_count(pd_start, pd_len));
  1115. /* Set our used ptes within the page table */
  1116. bitmap_set(pt->used_ptes,
  1117. gen8_pte_index(pd_start),
  1118. gen8_pte_count(pd_start, pd_len));
  1119. /* Our pde is now pointing to the pagetable, pt */
  1120. __set_bit(pde, pd->used_pdes);
  1121. /* Map the PDE to the page table */
  1122. page_directory[pde] = gen8_pde_encode(px_dma(pt),
  1123. I915_CACHE_LLC);
  1124. trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
  1125. gen8_pte_index(start),
  1126. gen8_pte_count(start, length),
  1127. GEN8_PTES);
  1128. /* NB: We haven't yet mapped ptes to pages. At this
  1129. * point we're still relying on insert_entries() */
  1130. }
  1131. kunmap_px(ppgtt, page_directory);
  1132. __set_bit(pdpe, pdp->used_pdpes);
  1133. gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
  1134. }
  1135. free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
  1136. mark_tlbs_dirty(ppgtt);
  1137. return 0;
  1138. err_out:
  1139. while (pdpe--) {
  1140. unsigned long temp;
  1141. for_each_set_bit(temp, new_page_tables + pdpe *
  1142. BITS_TO_LONGS(I915_PDES), I915_PDES)
  1143. free_pt(dev_priv,
  1144. pdp->page_directory[pdpe]->page_table[temp]);
  1145. }
  1146. for_each_set_bit(pdpe, new_page_dirs, pdpes)
  1147. free_pd(dev_priv, pdp->page_directory[pdpe]);
  1148. free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
  1149. mark_tlbs_dirty(ppgtt);
  1150. return ret;
  1151. }
  1152. static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
  1153. struct i915_pml4 *pml4,
  1154. uint64_t start,
  1155. uint64_t length)
  1156. {
  1157. DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
  1158. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1159. struct i915_page_directory_pointer *pdp;
  1160. uint64_t pml4e;
  1161. int ret = 0;
  1162. /* Do the pml4 allocations first, so we don't need to track the newly
  1163. * allocated tables below the pdp */
  1164. bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
  1165. /* The pagedirectory and pagetable allocations are done in the shared 3
  1166. * and 4 level code. Just allocate the pdps.
  1167. */
  1168. ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
  1169. new_pdps);
  1170. if (ret)
  1171. return ret;
  1172. WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
  1173. "The allocation has spanned more than 512GB. "
  1174. "It is highly likely this is incorrect.");
  1175. gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
  1176. WARN_ON(!pdp);
  1177. ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
  1178. if (ret)
  1179. goto err_out;
  1180. gen8_setup_pml4e(ppgtt, pml4, pdp, pml4e);
  1181. }
  1182. bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
  1183. GEN8_PML4ES_PER_PML4);
  1184. return 0;
  1185. err_out:
  1186. for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
  1187. gen8_ppgtt_cleanup_3lvl(vm->i915, pml4->pdps[pml4e]);
  1188. return ret;
  1189. }
  1190. static int gen8_alloc_va_range(struct i915_address_space *vm,
  1191. uint64_t start, uint64_t length)
  1192. {
  1193. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1194. if (USES_FULL_48BIT_PPGTT(vm->i915))
  1195. return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
  1196. else
  1197. return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
  1198. }
  1199. static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
  1200. uint64_t start, uint64_t length,
  1201. gen8_pte_t scratch_pte,
  1202. struct seq_file *m)
  1203. {
  1204. struct i915_page_directory *pd;
  1205. uint32_t pdpe;
  1206. gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
  1207. struct i915_page_table *pt;
  1208. uint64_t pd_len = length;
  1209. uint64_t pd_start = start;
  1210. uint32_t pde;
  1211. if (!test_bit(pdpe, pdp->used_pdpes))
  1212. continue;
  1213. seq_printf(m, "\tPDPE #%d\n", pdpe);
  1214. gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
  1215. uint32_t pte;
  1216. gen8_pte_t *pt_vaddr;
  1217. if (!test_bit(pde, pd->used_pdes))
  1218. continue;
  1219. pt_vaddr = kmap_px(pt);
  1220. for (pte = 0; pte < GEN8_PTES; pte += 4) {
  1221. uint64_t va =
  1222. (pdpe << GEN8_PDPE_SHIFT) |
  1223. (pde << GEN8_PDE_SHIFT) |
  1224. (pte << GEN8_PTE_SHIFT);
  1225. int i;
  1226. bool found = false;
  1227. for (i = 0; i < 4; i++)
  1228. if (pt_vaddr[pte + i] != scratch_pte)
  1229. found = true;
  1230. if (!found)
  1231. continue;
  1232. seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
  1233. for (i = 0; i < 4; i++) {
  1234. if (pt_vaddr[pte + i] != scratch_pte)
  1235. seq_printf(m, " %llx", pt_vaddr[pte + i]);
  1236. else
  1237. seq_puts(m, " SCRATCH ");
  1238. }
  1239. seq_puts(m, "\n");
  1240. }
  1241. /* don't use kunmap_px, it could trigger
  1242. * an unnecessary flush.
  1243. */
  1244. kunmap_atomic(pt_vaddr);
  1245. }
  1246. }
  1247. }
  1248. static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
  1249. {
  1250. struct i915_address_space *vm = &ppgtt->base;
  1251. uint64_t start = ppgtt->base.start;
  1252. uint64_t length = ppgtt->base.total;
  1253. gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
  1254. I915_CACHE_LLC);
  1255. if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
  1256. gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
  1257. } else {
  1258. uint64_t pml4e;
  1259. struct i915_pml4 *pml4 = &ppgtt->pml4;
  1260. struct i915_page_directory_pointer *pdp;
  1261. gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
  1262. if (!test_bit(pml4e, pml4->used_pml4es))
  1263. continue;
  1264. seq_printf(m, " PML4E #%llu\n", pml4e);
  1265. gen8_dump_pdp(pdp, start, length, scratch_pte, m);
  1266. }
  1267. }
  1268. }
  1269. static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
  1270. {
  1271. unsigned long *new_page_dirs, *new_page_tables;
  1272. uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
  1273. int ret;
  1274. /* We allocate temp bitmap for page tables for no gain
  1275. * but as this is for init only, lets keep the things simple
  1276. */
  1277. ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
  1278. if (ret)
  1279. return ret;
  1280. /* Allocate for all pdps regardless of how the ppgtt
  1281. * was defined.
  1282. */
  1283. ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
  1284. 0, 1ULL << 32,
  1285. new_page_dirs);
  1286. if (!ret)
  1287. *ppgtt->pdp.used_pdpes = *new_page_dirs;
  1288. free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
  1289. return ret;
  1290. }
  1291. /*
  1292. * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
  1293. * with a net effect resembling a 2-level page table in normal x86 terms. Each
  1294. * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
  1295. * space.
  1296. *
  1297. */
  1298. static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
  1299. {
  1300. struct drm_i915_private *dev_priv = ppgtt->base.i915;
  1301. int ret;
  1302. ret = gen8_init_scratch(&ppgtt->base);
  1303. if (ret)
  1304. return ret;
  1305. ppgtt->base.start = 0;
  1306. ppgtt->base.cleanup = gen8_ppgtt_cleanup;
  1307. ppgtt->base.allocate_va_range = gen8_alloc_va_range;
  1308. ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
  1309. ppgtt->base.clear_range = gen8_ppgtt_clear_range;
  1310. ppgtt->base.unbind_vma = ppgtt_unbind_vma;
  1311. ppgtt->base.bind_vma = ppgtt_bind_vma;
  1312. ppgtt->debug_dump = gen8_dump_ppgtt;
  1313. if (USES_FULL_48BIT_PPGTT(dev_priv)) {
  1314. ret = setup_px(dev_priv, &ppgtt->pml4);
  1315. if (ret)
  1316. goto free_scratch;
  1317. gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
  1318. ppgtt->base.total = 1ULL << 48;
  1319. ppgtt->switch_mm = gen8_48b_mm_switch;
  1320. } else {
  1321. ret = __pdp_init(dev_priv, &ppgtt->pdp);
  1322. if (ret)
  1323. goto free_scratch;
  1324. ppgtt->base.total = 1ULL << 32;
  1325. ppgtt->switch_mm = gen8_legacy_mm_switch;
  1326. trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
  1327. 0, 0,
  1328. GEN8_PML4E_SHIFT);
  1329. if (intel_vgpu_active(dev_priv)) {
  1330. ret = gen8_preallocate_top_level_pdps(ppgtt);
  1331. if (ret)
  1332. goto free_scratch;
  1333. }
  1334. }
  1335. if (intel_vgpu_active(dev_priv))
  1336. gen8_ppgtt_notify_vgt(ppgtt, true);
  1337. return 0;
  1338. free_scratch:
  1339. gen8_free_scratch(&ppgtt->base);
  1340. return ret;
  1341. }
  1342. static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
  1343. {
  1344. struct i915_address_space *vm = &ppgtt->base;
  1345. struct i915_page_table *unused;
  1346. gen6_pte_t scratch_pte;
  1347. uint32_t pd_entry;
  1348. uint32_t pte, pde;
  1349. uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
  1350. scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
  1351. I915_CACHE_LLC, 0);
  1352. gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
  1353. u32 expected;
  1354. gen6_pte_t *pt_vaddr;
  1355. const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
  1356. pd_entry = readl(ppgtt->pd_addr + pde);
  1357. expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
  1358. if (pd_entry != expected)
  1359. seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
  1360. pde,
  1361. pd_entry,
  1362. expected);
  1363. seq_printf(m, "\tPDE: %x\n", pd_entry);
  1364. pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
  1365. for (pte = 0; pte < GEN6_PTES; pte+=4) {
  1366. unsigned long va =
  1367. (pde * PAGE_SIZE * GEN6_PTES) +
  1368. (pte * PAGE_SIZE);
  1369. int i;
  1370. bool found = false;
  1371. for (i = 0; i < 4; i++)
  1372. if (pt_vaddr[pte + i] != scratch_pte)
  1373. found = true;
  1374. if (!found)
  1375. continue;
  1376. seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
  1377. for (i = 0; i < 4; i++) {
  1378. if (pt_vaddr[pte + i] != scratch_pte)
  1379. seq_printf(m, " %08x", pt_vaddr[pte + i]);
  1380. else
  1381. seq_puts(m, " SCRATCH ");
  1382. }
  1383. seq_puts(m, "\n");
  1384. }
  1385. kunmap_px(ppgtt, pt_vaddr);
  1386. }
  1387. }
  1388. /* Write pde (index) from the page directory @pd to the page table @pt */
  1389. static void gen6_write_pde(struct i915_page_directory *pd,
  1390. const int pde, struct i915_page_table *pt)
  1391. {
  1392. /* Caller needs to make sure the write completes if necessary */
  1393. struct i915_hw_ppgtt *ppgtt =
  1394. container_of(pd, struct i915_hw_ppgtt, pd);
  1395. u32 pd_entry;
  1396. pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
  1397. pd_entry |= GEN6_PDE_VALID;
  1398. writel(pd_entry, ppgtt->pd_addr + pde);
  1399. }
  1400. /* Write all the page tables found in the ppgtt structure to incrementing page
  1401. * directories. */
  1402. static void gen6_write_page_range(struct drm_i915_private *dev_priv,
  1403. struct i915_page_directory *pd,
  1404. uint32_t start, uint32_t length)
  1405. {
  1406. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1407. struct i915_page_table *pt;
  1408. uint32_t pde;
  1409. gen6_for_each_pde(pt, pd, start, length, pde)
  1410. gen6_write_pde(pd, pde, pt);
  1411. /* Make sure write is complete before other code can use this page
  1412. * table. Also require for WC mapped PTEs */
  1413. readl(ggtt->gsm);
  1414. }
  1415. static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
  1416. {
  1417. BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
  1418. return (ppgtt->pd.base.ggtt_offset / 64) << 16;
  1419. }
  1420. static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
  1421. struct drm_i915_gem_request *req)
  1422. {
  1423. struct intel_ring *ring = req->ring;
  1424. struct intel_engine_cs *engine = req->engine;
  1425. int ret;
  1426. /* NB: TLBs must be flushed and invalidated before a switch */
  1427. ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
  1428. if (ret)
  1429. return ret;
  1430. ret = intel_ring_begin(req, 6);
  1431. if (ret)
  1432. return ret;
  1433. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
  1434. intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
  1435. intel_ring_emit(ring, PP_DIR_DCLV_2G);
  1436. intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
  1437. intel_ring_emit(ring, get_pd_offset(ppgtt));
  1438. intel_ring_emit(ring, MI_NOOP);
  1439. intel_ring_advance(ring);
  1440. return 0;
  1441. }
  1442. static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
  1443. struct drm_i915_gem_request *req)
  1444. {
  1445. struct intel_ring *ring = req->ring;
  1446. struct intel_engine_cs *engine = req->engine;
  1447. int ret;
  1448. /* NB: TLBs must be flushed and invalidated before a switch */
  1449. ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
  1450. if (ret)
  1451. return ret;
  1452. ret = intel_ring_begin(req, 6);
  1453. if (ret)
  1454. return ret;
  1455. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
  1456. intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
  1457. intel_ring_emit(ring, PP_DIR_DCLV_2G);
  1458. intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
  1459. intel_ring_emit(ring, get_pd_offset(ppgtt));
  1460. intel_ring_emit(ring, MI_NOOP);
  1461. intel_ring_advance(ring);
  1462. /* XXX: RCS is the only one to auto invalidate the TLBs? */
  1463. if (engine->id != RCS) {
  1464. ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
  1465. if (ret)
  1466. return ret;
  1467. }
  1468. return 0;
  1469. }
  1470. static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
  1471. struct drm_i915_gem_request *req)
  1472. {
  1473. struct intel_engine_cs *engine = req->engine;
  1474. struct drm_i915_private *dev_priv = req->i915;
  1475. I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
  1476. I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
  1477. return 0;
  1478. }
  1479. static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
  1480. {
  1481. struct intel_engine_cs *engine;
  1482. enum intel_engine_id id;
  1483. for_each_engine(engine, dev_priv, id) {
  1484. u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
  1485. GEN8_GFX_PPGTT_48B : 0;
  1486. I915_WRITE(RING_MODE_GEN7(engine),
  1487. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
  1488. }
  1489. }
  1490. static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
  1491. {
  1492. struct intel_engine_cs *engine;
  1493. uint32_t ecochk, ecobits;
  1494. enum intel_engine_id id;
  1495. ecobits = I915_READ(GAC_ECO_BITS);
  1496. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
  1497. ecochk = I915_READ(GAM_ECOCHK);
  1498. if (IS_HASWELL(dev_priv)) {
  1499. ecochk |= ECOCHK_PPGTT_WB_HSW;
  1500. } else {
  1501. ecochk |= ECOCHK_PPGTT_LLC_IVB;
  1502. ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
  1503. }
  1504. I915_WRITE(GAM_ECOCHK, ecochk);
  1505. for_each_engine(engine, dev_priv, id) {
  1506. /* GFX_MODE is per-ring on gen7+ */
  1507. I915_WRITE(RING_MODE_GEN7(engine),
  1508. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  1509. }
  1510. }
  1511. static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
  1512. {
  1513. uint32_t ecochk, gab_ctl, ecobits;
  1514. ecobits = I915_READ(GAC_ECO_BITS);
  1515. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
  1516. ECOBITS_PPGTT_CACHE64B);
  1517. gab_ctl = I915_READ(GAB_CTL);
  1518. I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
  1519. ecochk = I915_READ(GAM_ECOCHK);
  1520. I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
  1521. I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  1522. }
  1523. /* PPGTT support for Sandybdrige/Gen6 and later */
  1524. static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
  1525. uint64_t start,
  1526. uint64_t length)
  1527. {
  1528. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1529. gen6_pte_t *pt_vaddr, scratch_pte;
  1530. unsigned first_entry = start >> PAGE_SHIFT;
  1531. unsigned num_entries = length >> PAGE_SHIFT;
  1532. unsigned act_pt = first_entry / GEN6_PTES;
  1533. unsigned first_pte = first_entry % GEN6_PTES;
  1534. unsigned last_pte, i;
  1535. scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
  1536. I915_CACHE_LLC, 0);
  1537. while (num_entries) {
  1538. last_pte = first_pte + num_entries;
  1539. if (last_pte > GEN6_PTES)
  1540. last_pte = GEN6_PTES;
  1541. pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
  1542. for (i = first_pte; i < last_pte; i++)
  1543. pt_vaddr[i] = scratch_pte;
  1544. kunmap_px(ppgtt, pt_vaddr);
  1545. num_entries -= last_pte - first_pte;
  1546. first_pte = 0;
  1547. act_pt++;
  1548. }
  1549. }
  1550. static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
  1551. struct sg_table *pages,
  1552. uint64_t start,
  1553. enum i915_cache_level cache_level, u32 flags)
  1554. {
  1555. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1556. unsigned first_entry = start >> PAGE_SHIFT;
  1557. unsigned act_pt = first_entry / GEN6_PTES;
  1558. unsigned act_pte = first_entry % GEN6_PTES;
  1559. gen6_pte_t *pt_vaddr = NULL;
  1560. struct sgt_iter sgt_iter;
  1561. dma_addr_t addr;
  1562. for_each_sgt_dma(addr, sgt_iter, pages) {
  1563. if (pt_vaddr == NULL)
  1564. pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
  1565. pt_vaddr[act_pte] =
  1566. vm->pte_encode(addr, cache_level, flags);
  1567. if (++act_pte == GEN6_PTES) {
  1568. kunmap_px(ppgtt, pt_vaddr);
  1569. pt_vaddr = NULL;
  1570. act_pt++;
  1571. act_pte = 0;
  1572. }
  1573. }
  1574. if (pt_vaddr)
  1575. kunmap_px(ppgtt, pt_vaddr);
  1576. }
  1577. static int gen6_alloc_va_range(struct i915_address_space *vm,
  1578. uint64_t start_in, uint64_t length_in)
  1579. {
  1580. DECLARE_BITMAP(new_page_tables, I915_PDES);
  1581. struct drm_i915_private *dev_priv = vm->i915;
  1582. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1583. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1584. struct i915_page_table *pt;
  1585. uint32_t start, length, start_save, length_save;
  1586. uint32_t pde;
  1587. int ret;
  1588. start = start_save = start_in;
  1589. length = length_save = length_in;
  1590. bitmap_zero(new_page_tables, I915_PDES);
  1591. /* The allocation is done in two stages so that we can bail out with
  1592. * minimal amount of pain. The first stage finds new page tables that
  1593. * need allocation. The second stage marks use ptes within the page
  1594. * tables.
  1595. */
  1596. gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
  1597. if (pt != vm->scratch_pt) {
  1598. WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
  1599. continue;
  1600. }
  1601. /* We've already allocated a page table */
  1602. WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
  1603. pt = alloc_pt(dev_priv);
  1604. if (IS_ERR(pt)) {
  1605. ret = PTR_ERR(pt);
  1606. goto unwind_out;
  1607. }
  1608. gen6_initialize_pt(vm, pt);
  1609. ppgtt->pd.page_table[pde] = pt;
  1610. __set_bit(pde, new_page_tables);
  1611. trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
  1612. }
  1613. start = start_save;
  1614. length = length_save;
  1615. gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
  1616. DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
  1617. bitmap_zero(tmp_bitmap, GEN6_PTES);
  1618. bitmap_set(tmp_bitmap, gen6_pte_index(start),
  1619. gen6_pte_count(start, length));
  1620. if (__test_and_clear_bit(pde, new_page_tables))
  1621. gen6_write_pde(&ppgtt->pd, pde, pt);
  1622. trace_i915_page_table_entry_map(vm, pde, pt,
  1623. gen6_pte_index(start),
  1624. gen6_pte_count(start, length),
  1625. GEN6_PTES);
  1626. bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
  1627. GEN6_PTES);
  1628. }
  1629. WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
  1630. /* Make sure write is complete before other code can use this page
  1631. * table. Also require for WC mapped PTEs */
  1632. readl(ggtt->gsm);
  1633. mark_tlbs_dirty(ppgtt);
  1634. return 0;
  1635. unwind_out:
  1636. for_each_set_bit(pde, new_page_tables, I915_PDES) {
  1637. struct i915_page_table *pt = ppgtt->pd.page_table[pde];
  1638. ppgtt->pd.page_table[pde] = vm->scratch_pt;
  1639. free_pt(dev_priv, pt);
  1640. }
  1641. mark_tlbs_dirty(ppgtt);
  1642. return ret;
  1643. }
  1644. static int gen6_init_scratch(struct i915_address_space *vm)
  1645. {
  1646. struct drm_i915_private *dev_priv = vm->i915;
  1647. int ret;
  1648. ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
  1649. if (ret)
  1650. return ret;
  1651. vm->scratch_pt = alloc_pt(dev_priv);
  1652. if (IS_ERR(vm->scratch_pt)) {
  1653. cleanup_scratch_page(dev_priv, &vm->scratch_page);
  1654. return PTR_ERR(vm->scratch_pt);
  1655. }
  1656. gen6_initialize_pt(vm, vm->scratch_pt);
  1657. return 0;
  1658. }
  1659. static void gen6_free_scratch(struct i915_address_space *vm)
  1660. {
  1661. struct drm_i915_private *dev_priv = vm->i915;
  1662. free_pt(dev_priv, vm->scratch_pt);
  1663. cleanup_scratch_page(dev_priv, &vm->scratch_page);
  1664. }
  1665. static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
  1666. {
  1667. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1668. struct i915_page_directory *pd = &ppgtt->pd;
  1669. struct drm_i915_private *dev_priv = vm->i915;
  1670. struct i915_page_table *pt;
  1671. uint32_t pde;
  1672. drm_mm_remove_node(&ppgtt->node);
  1673. gen6_for_all_pdes(pt, pd, pde)
  1674. if (pt != vm->scratch_pt)
  1675. free_pt(dev_priv, pt);
  1676. gen6_free_scratch(vm);
  1677. }
  1678. static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
  1679. {
  1680. struct i915_address_space *vm = &ppgtt->base;
  1681. struct drm_i915_private *dev_priv = ppgtt->base.i915;
  1682. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1683. int ret;
  1684. /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
  1685. * allocator works in address space sizes, so it's multiplied by page
  1686. * size. We allocate at the top of the GTT to avoid fragmentation.
  1687. */
  1688. BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
  1689. ret = gen6_init_scratch(vm);
  1690. if (ret)
  1691. return ret;
  1692. ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
  1693. GEN6_PD_SIZE, GEN6_PD_ALIGN,
  1694. I915_COLOR_UNEVICTABLE,
  1695. 0, ggtt->base.total,
  1696. PIN_HIGH);
  1697. if (ret)
  1698. goto err_out;
  1699. if (ppgtt->node.start < ggtt->mappable_end)
  1700. DRM_DEBUG("Forced to use aperture for PDEs\n");
  1701. return 0;
  1702. err_out:
  1703. gen6_free_scratch(vm);
  1704. return ret;
  1705. }
  1706. static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
  1707. {
  1708. return gen6_ppgtt_allocate_page_directories(ppgtt);
  1709. }
  1710. static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
  1711. uint64_t start, uint64_t length)
  1712. {
  1713. struct i915_page_table *unused;
  1714. uint32_t pde;
  1715. gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
  1716. ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
  1717. }
  1718. static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
  1719. {
  1720. struct drm_i915_private *dev_priv = ppgtt->base.i915;
  1721. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1722. int ret;
  1723. ppgtt->base.pte_encode = ggtt->base.pte_encode;
  1724. if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
  1725. ppgtt->switch_mm = gen6_mm_switch;
  1726. else if (IS_HASWELL(dev_priv))
  1727. ppgtt->switch_mm = hsw_mm_switch;
  1728. else if (IS_GEN7(dev_priv))
  1729. ppgtt->switch_mm = gen7_mm_switch;
  1730. else
  1731. BUG();
  1732. ret = gen6_ppgtt_alloc(ppgtt);
  1733. if (ret)
  1734. return ret;
  1735. ppgtt->base.allocate_va_range = gen6_alloc_va_range;
  1736. ppgtt->base.clear_range = gen6_ppgtt_clear_range;
  1737. ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
  1738. ppgtt->base.unbind_vma = ppgtt_unbind_vma;
  1739. ppgtt->base.bind_vma = ppgtt_bind_vma;
  1740. ppgtt->base.cleanup = gen6_ppgtt_cleanup;
  1741. ppgtt->base.start = 0;
  1742. ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
  1743. ppgtt->debug_dump = gen6_dump_ppgtt;
  1744. ppgtt->pd.base.ggtt_offset =
  1745. ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
  1746. ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
  1747. ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
  1748. gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
  1749. gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
  1750. DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
  1751. ppgtt->node.size >> 20,
  1752. ppgtt->node.start / PAGE_SIZE);
  1753. DRM_DEBUG("Adding PPGTT at offset %x\n",
  1754. ppgtt->pd.base.ggtt_offset << 10);
  1755. return 0;
  1756. }
  1757. static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
  1758. struct drm_i915_private *dev_priv)
  1759. {
  1760. ppgtt->base.i915 = dev_priv;
  1761. if (INTEL_INFO(dev_priv)->gen < 8)
  1762. return gen6_ppgtt_init(ppgtt);
  1763. else
  1764. return gen8_ppgtt_init(ppgtt);
  1765. }
  1766. static void i915_address_space_init(struct i915_address_space *vm,
  1767. struct drm_i915_private *dev_priv,
  1768. const char *name)
  1769. {
  1770. i915_gem_timeline_init(dev_priv, &vm->timeline, name);
  1771. drm_mm_init(&vm->mm, vm->start, vm->total);
  1772. INIT_LIST_HEAD(&vm->active_list);
  1773. INIT_LIST_HEAD(&vm->inactive_list);
  1774. INIT_LIST_HEAD(&vm->unbound_list);
  1775. list_add_tail(&vm->global_link, &dev_priv->vm_list);
  1776. }
  1777. static void i915_address_space_fini(struct i915_address_space *vm)
  1778. {
  1779. i915_gem_timeline_fini(&vm->timeline);
  1780. drm_mm_takedown(&vm->mm);
  1781. list_del(&vm->global_link);
  1782. }
  1783. static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
  1784. {
  1785. /* This function is for gtt related workarounds. This function is
  1786. * called on driver load and after a GPU reset, so you can place
  1787. * workarounds here even if they get overwritten by GPU reset.
  1788. */
  1789. /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
  1790. if (IS_BROADWELL(dev_priv))
  1791. I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
  1792. else if (IS_CHERRYVIEW(dev_priv))
  1793. I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
  1794. else if (IS_SKYLAKE(dev_priv))
  1795. I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
  1796. else if (IS_BROXTON(dev_priv))
  1797. I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
  1798. }
  1799. static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
  1800. struct drm_i915_private *dev_priv,
  1801. struct drm_i915_file_private *file_priv,
  1802. const char *name)
  1803. {
  1804. int ret;
  1805. ret = __hw_ppgtt_init(ppgtt, dev_priv);
  1806. if (ret == 0) {
  1807. kref_init(&ppgtt->ref);
  1808. i915_address_space_init(&ppgtt->base, dev_priv, name);
  1809. ppgtt->base.file = file_priv;
  1810. }
  1811. return ret;
  1812. }
  1813. int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
  1814. {
  1815. gtt_write_workarounds(dev_priv);
  1816. /* In the case of execlists, PPGTT is enabled by the context descriptor
  1817. * and the PDPs are contained within the context itself. We don't
  1818. * need to do anything here. */
  1819. if (i915.enable_execlists)
  1820. return 0;
  1821. if (!USES_PPGTT(dev_priv))
  1822. return 0;
  1823. if (IS_GEN6(dev_priv))
  1824. gen6_ppgtt_enable(dev_priv);
  1825. else if (IS_GEN7(dev_priv))
  1826. gen7_ppgtt_enable(dev_priv);
  1827. else if (INTEL_GEN(dev_priv) >= 8)
  1828. gen8_ppgtt_enable(dev_priv);
  1829. else
  1830. MISSING_CASE(INTEL_GEN(dev_priv));
  1831. return 0;
  1832. }
  1833. struct i915_hw_ppgtt *
  1834. i915_ppgtt_create(struct drm_i915_private *dev_priv,
  1835. struct drm_i915_file_private *fpriv,
  1836. const char *name)
  1837. {
  1838. struct i915_hw_ppgtt *ppgtt;
  1839. int ret;
  1840. ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
  1841. if (!ppgtt)
  1842. return ERR_PTR(-ENOMEM);
  1843. ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
  1844. if (ret) {
  1845. kfree(ppgtt);
  1846. return ERR_PTR(ret);
  1847. }
  1848. trace_i915_ppgtt_create(&ppgtt->base);
  1849. return ppgtt;
  1850. }
  1851. void i915_ppgtt_close(struct i915_address_space *vm)
  1852. {
  1853. struct list_head *phases[] = {
  1854. &vm->active_list,
  1855. &vm->inactive_list,
  1856. &vm->unbound_list,
  1857. NULL,
  1858. }, **phase;
  1859. GEM_BUG_ON(vm->closed);
  1860. vm->closed = true;
  1861. for (phase = phases; *phase; phase++) {
  1862. struct i915_vma *vma, *vn;
  1863. list_for_each_entry_safe(vma, vn, *phase, vm_link)
  1864. if (!i915_vma_is_closed(vma))
  1865. i915_vma_close(vma);
  1866. }
  1867. }
  1868. void i915_ppgtt_release(struct kref *kref)
  1869. {
  1870. struct i915_hw_ppgtt *ppgtt =
  1871. container_of(kref, struct i915_hw_ppgtt, ref);
  1872. trace_i915_ppgtt_release(&ppgtt->base);
  1873. /* vmas should already be unbound and destroyed */
  1874. WARN_ON(!list_empty(&ppgtt->base.active_list));
  1875. WARN_ON(!list_empty(&ppgtt->base.inactive_list));
  1876. WARN_ON(!list_empty(&ppgtt->base.unbound_list));
  1877. i915_address_space_fini(&ppgtt->base);
  1878. ppgtt->base.cleanup(&ppgtt->base);
  1879. kfree(ppgtt);
  1880. }
  1881. /* Certain Gen5 chipsets require require idling the GPU before
  1882. * unmapping anything from the GTT when VT-d is enabled.
  1883. */
  1884. static bool needs_idle_maps(struct drm_i915_private *dev_priv)
  1885. {
  1886. #ifdef CONFIG_INTEL_IOMMU
  1887. /* Query intel_iommu to see if we need the workaround. Presumably that
  1888. * was loaded first.
  1889. */
  1890. if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
  1891. return true;
  1892. #endif
  1893. return false;
  1894. }
  1895. void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
  1896. {
  1897. struct intel_engine_cs *engine;
  1898. enum intel_engine_id id;
  1899. if (INTEL_INFO(dev_priv)->gen < 6)
  1900. return;
  1901. for_each_engine(engine, dev_priv, id) {
  1902. u32 fault_reg;
  1903. fault_reg = I915_READ(RING_FAULT_REG(engine));
  1904. if (fault_reg & RING_FAULT_VALID) {
  1905. DRM_DEBUG_DRIVER("Unexpected fault\n"
  1906. "\tAddr: 0x%08lx\n"
  1907. "\tAddress space: %s\n"
  1908. "\tSource ID: %d\n"
  1909. "\tType: %d\n",
  1910. fault_reg & PAGE_MASK,
  1911. fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
  1912. RING_FAULT_SRCID(fault_reg),
  1913. RING_FAULT_FAULT_TYPE(fault_reg));
  1914. I915_WRITE(RING_FAULT_REG(engine),
  1915. fault_reg & ~RING_FAULT_VALID);
  1916. }
  1917. }
  1918. /* Engine specific init may not have been done till this point. */
  1919. if (dev_priv->engine[RCS])
  1920. POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
  1921. }
  1922. void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
  1923. {
  1924. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1925. /* Don't bother messing with faults pre GEN6 as we have little
  1926. * documentation supporting that it's a good idea.
  1927. */
  1928. if (INTEL_GEN(dev_priv) < 6)
  1929. return;
  1930. i915_check_and_clear_faults(dev_priv);
  1931. ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
  1932. i915_ggtt_invalidate(dev_priv);
  1933. }
  1934. int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
  1935. struct sg_table *pages)
  1936. {
  1937. do {
  1938. if (dma_map_sg(&obj->base.dev->pdev->dev,
  1939. pages->sgl, pages->nents,
  1940. PCI_DMA_BIDIRECTIONAL))
  1941. return 0;
  1942. /* If the DMA remap fails, one cause can be that we have
  1943. * too many objects pinned in a small remapping table,
  1944. * such as swiotlb. Incrementally purge all other objects and
  1945. * try again - if there are no more pages to remove from
  1946. * the DMA remapper, i915_gem_shrink will return 0.
  1947. */
  1948. GEM_BUG_ON(obj->mm.pages == pages);
  1949. } while (i915_gem_shrink(to_i915(obj->base.dev),
  1950. obj->base.size >> PAGE_SHIFT,
  1951. I915_SHRINK_BOUND |
  1952. I915_SHRINK_UNBOUND |
  1953. I915_SHRINK_ACTIVE));
  1954. return -ENOSPC;
  1955. }
  1956. static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
  1957. {
  1958. writeq(pte, addr);
  1959. }
  1960. static void gen8_ggtt_insert_page(struct i915_address_space *vm,
  1961. dma_addr_t addr,
  1962. uint64_t offset,
  1963. enum i915_cache_level level,
  1964. u32 unused)
  1965. {
  1966. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1967. gen8_pte_t __iomem *pte =
  1968. (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
  1969. gen8_set_pte(pte, gen8_pte_encode(addr, level));
  1970. ggtt->invalidate(vm->i915);
  1971. }
  1972. static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
  1973. struct sg_table *st,
  1974. uint64_t start,
  1975. enum i915_cache_level level, u32 unused)
  1976. {
  1977. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1978. struct sgt_iter sgt_iter;
  1979. gen8_pte_t __iomem *gtt_entries;
  1980. gen8_pte_t gtt_entry;
  1981. dma_addr_t addr;
  1982. int i = 0;
  1983. gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
  1984. for_each_sgt_dma(addr, sgt_iter, st) {
  1985. gtt_entry = gen8_pte_encode(addr, level);
  1986. gen8_set_pte(&gtt_entries[i++], gtt_entry);
  1987. }
  1988. /*
  1989. * XXX: This serves as a posting read to make sure that the PTE has
  1990. * actually been updated. There is some concern that even though
  1991. * registers and PTEs are within the same BAR that they are potentially
  1992. * of NUMA access patterns. Therefore, even with the way we assume
  1993. * hardware should work, we must keep this posting read for paranoia.
  1994. */
  1995. if (i != 0)
  1996. WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
  1997. /* This next bit makes the above posting read even more important. We
  1998. * want to flush the TLBs only after we're certain all the PTE updates
  1999. * have finished.
  2000. */
  2001. ggtt->invalidate(vm->i915);
  2002. }
  2003. struct insert_entries {
  2004. struct i915_address_space *vm;
  2005. struct sg_table *st;
  2006. uint64_t start;
  2007. enum i915_cache_level level;
  2008. u32 flags;
  2009. };
  2010. static int gen8_ggtt_insert_entries__cb(void *_arg)
  2011. {
  2012. struct insert_entries *arg = _arg;
  2013. gen8_ggtt_insert_entries(arg->vm, arg->st,
  2014. arg->start, arg->level, arg->flags);
  2015. return 0;
  2016. }
  2017. static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
  2018. struct sg_table *st,
  2019. uint64_t start,
  2020. enum i915_cache_level level,
  2021. u32 flags)
  2022. {
  2023. struct insert_entries arg = { vm, st, start, level, flags };
  2024. stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
  2025. }
  2026. static void gen6_ggtt_insert_page(struct i915_address_space *vm,
  2027. dma_addr_t addr,
  2028. uint64_t offset,
  2029. enum i915_cache_level level,
  2030. u32 flags)
  2031. {
  2032. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  2033. gen6_pte_t __iomem *pte =
  2034. (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
  2035. iowrite32(vm->pte_encode(addr, level, flags), pte);
  2036. ggtt->invalidate(vm->i915);
  2037. }
  2038. /*
  2039. * Binds an object into the global gtt with the specified cache level. The object
  2040. * will be accessible to the GPU via commands whose operands reference offsets
  2041. * within the global GTT as well as accessible by the GPU through the GMADR
  2042. * mapped BAR (dev_priv->mm.gtt->gtt).
  2043. */
  2044. static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
  2045. struct sg_table *st,
  2046. uint64_t start,
  2047. enum i915_cache_level level, u32 flags)
  2048. {
  2049. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  2050. struct sgt_iter sgt_iter;
  2051. gen6_pte_t __iomem *gtt_entries;
  2052. gen6_pte_t gtt_entry;
  2053. dma_addr_t addr;
  2054. int i = 0;
  2055. gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
  2056. for_each_sgt_dma(addr, sgt_iter, st) {
  2057. gtt_entry = vm->pte_encode(addr, level, flags);
  2058. iowrite32(gtt_entry, &gtt_entries[i++]);
  2059. }
  2060. /* XXX: This serves as a posting read to make sure that the PTE has
  2061. * actually been updated. There is some concern that even though
  2062. * registers and PTEs are within the same BAR that they are potentially
  2063. * of NUMA access patterns. Therefore, even with the way we assume
  2064. * hardware should work, we must keep this posting read for paranoia.
  2065. */
  2066. if (i != 0)
  2067. WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
  2068. /* This next bit makes the above posting read even more important. We
  2069. * want to flush the TLBs only after we're certain all the PTE updates
  2070. * have finished.
  2071. */
  2072. ggtt->invalidate(vm->i915);
  2073. }
  2074. static void nop_clear_range(struct i915_address_space *vm,
  2075. uint64_t start, uint64_t length)
  2076. {
  2077. }
  2078. static void gen8_ggtt_clear_range(struct i915_address_space *vm,
  2079. uint64_t start, uint64_t length)
  2080. {
  2081. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  2082. unsigned first_entry = start >> PAGE_SHIFT;
  2083. unsigned num_entries = length >> PAGE_SHIFT;
  2084. gen8_pte_t scratch_pte, __iomem *gtt_base =
  2085. (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
  2086. const int max_entries = ggtt_total_entries(ggtt) - first_entry;
  2087. int i;
  2088. if (WARN(num_entries > max_entries,
  2089. "First entry = %d; Num entries = %d (max=%d)\n",
  2090. first_entry, num_entries, max_entries))
  2091. num_entries = max_entries;
  2092. scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
  2093. I915_CACHE_LLC);
  2094. for (i = 0; i < num_entries; i++)
  2095. gen8_set_pte(&gtt_base[i], scratch_pte);
  2096. readl(gtt_base);
  2097. }
  2098. static void gen6_ggtt_clear_range(struct i915_address_space *vm,
  2099. uint64_t start,
  2100. uint64_t length)
  2101. {
  2102. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  2103. unsigned first_entry = start >> PAGE_SHIFT;
  2104. unsigned num_entries = length >> PAGE_SHIFT;
  2105. gen6_pte_t scratch_pte, __iomem *gtt_base =
  2106. (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
  2107. const int max_entries = ggtt_total_entries(ggtt) - first_entry;
  2108. int i;
  2109. if (WARN(num_entries > max_entries,
  2110. "First entry = %d; Num entries = %d (max=%d)\n",
  2111. first_entry, num_entries, max_entries))
  2112. num_entries = max_entries;
  2113. scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
  2114. I915_CACHE_LLC, 0);
  2115. for (i = 0; i < num_entries; i++)
  2116. iowrite32(scratch_pte, &gtt_base[i]);
  2117. readl(gtt_base);
  2118. }
  2119. static void i915_ggtt_insert_page(struct i915_address_space *vm,
  2120. dma_addr_t addr,
  2121. uint64_t offset,
  2122. enum i915_cache_level cache_level,
  2123. u32 unused)
  2124. {
  2125. unsigned int flags = (cache_level == I915_CACHE_NONE) ?
  2126. AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
  2127. intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
  2128. }
  2129. static void i915_ggtt_insert_entries(struct i915_address_space *vm,
  2130. struct sg_table *pages,
  2131. uint64_t start,
  2132. enum i915_cache_level cache_level, u32 unused)
  2133. {
  2134. unsigned int flags = (cache_level == I915_CACHE_NONE) ?
  2135. AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
  2136. intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
  2137. }
  2138. static void i915_ggtt_clear_range(struct i915_address_space *vm,
  2139. uint64_t start,
  2140. uint64_t length)
  2141. {
  2142. intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
  2143. }
  2144. static int ggtt_bind_vma(struct i915_vma *vma,
  2145. enum i915_cache_level cache_level,
  2146. u32 flags)
  2147. {
  2148. struct drm_i915_private *i915 = vma->vm->i915;
  2149. struct drm_i915_gem_object *obj = vma->obj;
  2150. u32 pte_flags = 0;
  2151. int ret;
  2152. ret = i915_get_ggtt_vma_pages(vma);
  2153. if (ret)
  2154. return ret;
  2155. /* Currently applicable only to VLV */
  2156. if (obj->gt_ro)
  2157. pte_flags |= PTE_READ_ONLY;
  2158. intel_runtime_pm_get(i915);
  2159. vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
  2160. cache_level, pte_flags);
  2161. intel_runtime_pm_put(i915);
  2162. /*
  2163. * Without aliasing PPGTT there's no difference between
  2164. * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
  2165. * upgrade to both bound if we bind either to avoid double-binding.
  2166. */
  2167. vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
  2168. return 0;
  2169. }
  2170. static int aliasing_gtt_bind_vma(struct i915_vma *vma,
  2171. enum i915_cache_level cache_level,
  2172. u32 flags)
  2173. {
  2174. struct drm_i915_private *i915 = vma->vm->i915;
  2175. u32 pte_flags;
  2176. int ret;
  2177. ret = i915_get_ggtt_vma_pages(vma);
  2178. if (ret)
  2179. return ret;
  2180. /* Currently applicable only to VLV */
  2181. pte_flags = 0;
  2182. if (vma->obj->gt_ro)
  2183. pte_flags |= PTE_READ_ONLY;
  2184. if (flags & I915_VMA_GLOBAL_BIND) {
  2185. intel_runtime_pm_get(i915);
  2186. vma->vm->insert_entries(vma->vm,
  2187. vma->pages, vma->node.start,
  2188. cache_level, pte_flags);
  2189. intel_runtime_pm_put(i915);
  2190. }
  2191. if (flags & I915_VMA_LOCAL_BIND) {
  2192. struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
  2193. appgtt->base.insert_entries(&appgtt->base,
  2194. vma->pages, vma->node.start,
  2195. cache_level, pte_flags);
  2196. }
  2197. return 0;
  2198. }
  2199. static void ggtt_unbind_vma(struct i915_vma *vma)
  2200. {
  2201. struct drm_i915_private *i915 = vma->vm->i915;
  2202. struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
  2203. const u64 size = min(vma->size, vma->node.size);
  2204. if (vma->flags & I915_VMA_GLOBAL_BIND) {
  2205. intel_runtime_pm_get(i915);
  2206. vma->vm->clear_range(vma->vm,
  2207. vma->node.start, size);
  2208. intel_runtime_pm_put(i915);
  2209. }
  2210. if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
  2211. appgtt->base.clear_range(&appgtt->base,
  2212. vma->node.start, size);
  2213. }
  2214. void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
  2215. struct sg_table *pages)
  2216. {
  2217. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2218. struct device *kdev = &dev_priv->drm.pdev->dev;
  2219. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2220. if (unlikely(ggtt->do_idle_maps)) {
  2221. if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
  2222. DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
  2223. /* Wait a bit, in hopes it avoids the hang */
  2224. udelay(10);
  2225. }
  2226. }
  2227. dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
  2228. }
  2229. static void i915_gtt_color_adjust(const struct drm_mm_node *node,
  2230. unsigned long color,
  2231. u64 *start,
  2232. u64 *end)
  2233. {
  2234. if (node->color != color)
  2235. *start += I915_GTT_PAGE_SIZE;
  2236. node = list_next_entry(node, node_list);
  2237. if (node->allocated && node->color != color)
  2238. *end -= I915_GTT_PAGE_SIZE;
  2239. }
  2240. int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
  2241. {
  2242. /* Let GEM Manage all of the aperture.
  2243. *
  2244. * However, leave one page at the end still bound to the scratch page.
  2245. * There are a number of places where the hardware apparently prefetches
  2246. * past the end of the object, and we've seen multiple hangs with the
  2247. * GPU head pointer stuck in a batchbuffer bound at the last page of the
  2248. * aperture. One page should be enough to keep any prefetching inside
  2249. * of the aperture.
  2250. */
  2251. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2252. unsigned long hole_start, hole_end;
  2253. struct i915_hw_ppgtt *ppgtt;
  2254. struct drm_mm_node *entry;
  2255. int ret;
  2256. ret = intel_vgt_balloon(dev_priv);
  2257. if (ret)
  2258. return ret;
  2259. /* Reserve a mappable slot for our lockless error capture */
  2260. ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
  2261. &ggtt->error_capture,
  2262. PAGE_SIZE, 0,
  2263. I915_COLOR_UNEVICTABLE,
  2264. 0, ggtt->mappable_end,
  2265. 0, 0);
  2266. if (ret)
  2267. return ret;
  2268. /* Clear any non-preallocated blocks */
  2269. drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
  2270. DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
  2271. hole_start, hole_end);
  2272. ggtt->base.clear_range(&ggtt->base, hole_start,
  2273. hole_end - hole_start);
  2274. }
  2275. /* And finally clear the reserved guard page */
  2276. ggtt->base.clear_range(&ggtt->base,
  2277. ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
  2278. if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
  2279. ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
  2280. if (!ppgtt) {
  2281. ret = -ENOMEM;
  2282. goto err;
  2283. }
  2284. ret = __hw_ppgtt_init(ppgtt, dev_priv);
  2285. if (ret)
  2286. goto err_ppgtt;
  2287. if (ppgtt->base.allocate_va_range) {
  2288. ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
  2289. ppgtt->base.total);
  2290. if (ret)
  2291. goto err_ppgtt_cleanup;
  2292. }
  2293. ppgtt->base.clear_range(&ppgtt->base,
  2294. ppgtt->base.start,
  2295. ppgtt->base.total);
  2296. dev_priv->mm.aliasing_ppgtt = ppgtt;
  2297. WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
  2298. ggtt->base.bind_vma = aliasing_gtt_bind_vma;
  2299. }
  2300. return 0;
  2301. err_ppgtt_cleanup:
  2302. ppgtt->base.cleanup(&ppgtt->base);
  2303. err_ppgtt:
  2304. kfree(ppgtt);
  2305. err:
  2306. drm_mm_remove_node(&ggtt->error_capture);
  2307. return ret;
  2308. }
  2309. /**
  2310. * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
  2311. * @dev_priv: i915 device
  2312. */
  2313. void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
  2314. {
  2315. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2316. if (dev_priv->mm.aliasing_ppgtt) {
  2317. struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
  2318. ppgtt->base.cleanup(&ppgtt->base);
  2319. kfree(ppgtt);
  2320. }
  2321. i915_gem_cleanup_stolen(&dev_priv->drm);
  2322. if (drm_mm_node_allocated(&ggtt->error_capture))
  2323. drm_mm_remove_node(&ggtt->error_capture);
  2324. if (drm_mm_initialized(&ggtt->base.mm)) {
  2325. intel_vgt_deballoon(dev_priv);
  2326. mutex_lock(&dev_priv->drm.struct_mutex);
  2327. i915_address_space_fini(&ggtt->base);
  2328. mutex_unlock(&dev_priv->drm.struct_mutex);
  2329. }
  2330. ggtt->base.cleanup(&ggtt->base);
  2331. arch_phys_wc_del(ggtt->mtrr);
  2332. io_mapping_fini(&ggtt->mappable);
  2333. }
  2334. static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
  2335. {
  2336. snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
  2337. snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
  2338. return snb_gmch_ctl << 20;
  2339. }
  2340. static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
  2341. {
  2342. bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
  2343. bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
  2344. if (bdw_gmch_ctl)
  2345. bdw_gmch_ctl = 1 << bdw_gmch_ctl;
  2346. #ifdef CONFIG_X86_32
  2347. /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
  2348. if (bdw_gmch_ctl > 4)
  2349. bdw_gmch_ctl = 4;
  2350. #endif
  2351. return bdw_gmch_ctl << 20;
  2352. }
  2353. static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
  2354. {
  2355. gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
  2356. gmch_ctrl &= SNB_GMCH_GGMS_MASK;
  2357. if (gmch_ctrl)
  2358. return 1 << (20 + gmch_ctrl);
  2359. return 0;
  2360. }
  2361. static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
  2362. {
  2363. snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
  2364. snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
  2365. return snb_gmch_ctl << 25; /* 32 MB units */
  2366. }
  2367. static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
  2368. {
  2369. bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  2370. bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
  2371. return bdw_gmch_ctl << 25; /* 32 MB units */
  2372. }
  2373. static size_t chv_get_stolen_size(u16 gmch_ctrl)
  2374. {
  2375. gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
  2376. gmch_ctrl &= SNB_GMCH_GMS_MASK;
  2377. /*
  2378. * 0x0 to 0x10: 32MB increments starting at 0MB
  2379. * 0x11 to 0x16: 4MB increments starting at 8MB
  2380. * 0x17 to 0x1d: 4MB increments start at 36MB
  2381. */
  2382. if (gmch_ctrl < 0x11)
  2383. return gmch_ctrl << 25;
  2384. else if (gmch_ctrl < 0x17)
  2385. return (gmch_ctrl - 0x11 + 2) << 22;
  2386. else
  2387. return (gmch_ctrl - 0x17 + 9) << 22;
  2388. }
  2389. static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
  2390. {
  2391. gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  2392. gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
  2393. if (gen9_gmch_ctl < 0xf0)
  2394. return gen9_gmch_ctl << 25; /* 32 MB units */
  2395. else
  2396. /* 4MB increments starting at 0xf0 for 4MB */
  2397. return (gen9_gmch_ctl - 0xf0 + 1) << 22;
  2398. }
  2399. static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
  2400. {
  2401. struct drm_i915_private *dev_priv = ggtt->base.i915;
  2402. struct pci_dev *pdev = dev_priv->drm.pdev;
  2403. phys_addr_t phys_addr;
  2404. int ret;
  2405. /* For Modern GENs the PTEs and register space are split in the BAR */
  2406. phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
  2407. /*
  2408. * On BXT writes larger than 64 bit to the GTT pagetable range will be
  2409. * dropped. For WC mappings in general we have 64 byte burst writes
  2410. * when the WC buffer is flushed, so we can't use it, but have to
  2411. * resort to an uncached mapping. The WC issue is easily caught by the
  2412. * readback check when writing GTT PTE entries.
  2413. */
  2414. if (IS_GEN9_LP(dev_priv))
  2415. ggtt->gsm = ioremap_nocache(phys_addr, size);
  2416. else
  2417. ggtt->gsm = ioremap_wc(phys_addr, size);
  2418. if (!ggtt->gsm) {
  2419. DRM_ERROR("Failed to map the ggtt page table\n");
  2420. return -ENOMEM;
  2421. }
  2422. ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
  2423. if (ret) {
  2424. DRM_ERROR("Scratch setup failed\n");
  2425. /* iounmap will also get called at remove, but meh */
  2426. iounmap(ggtt->gsm);
  2427. return ret;
  2428. }
  2429. return 0;
  2430. }
  2431. /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
  2432. * bits. When using advanced contexts each context stores its own PAT, but
  2433. * writing this data shouldn't be harmful even in those cases. */
  2434. static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
  2435. {
  2436. uint64_t pat;
  2437. pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
  2438. GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
  2439. GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
  2440. GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
  2441. GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
  2442. GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
  2443. GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
  2444. GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
  2445. if (!USES_PPGTT(dev_priv))
  2446. /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
  2447. * so RTL will always use the value corresponding to
  2448. * pat_sel = 000".
  2449. * So let's disable cache for GGTT to avoid screen corruptions.
  2450. * MOCS still can be used though.
  2451. * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
  2452. * before this patch, i.e. the same uncached + snooping access
  2453. * like on gen6/7 seems to be in effect.
  2454. * - So this just fixes blitter/render access. Again it looks
  2455. * like it's not just uncached access, but uncached + snooping.
  2456. * So we can still hold onto all our assumptions wrt cpu
  2457. * clflushing on LLC machines.
  2458. */
  2459. pat = GEN8_PPAT(0, GEN8_PPAT_UC);
  2460. /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
  2461. * write would work. */
  2462. I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
  2463. I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
  2464. }
  2465. static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
  2466. {
  2467. uint64_t pat;
  2468. /*
  2469. * Map WB on BDW to snooped on CHV.
  2470. *
  2471. * Only the snoop bit has meaning for CHV, the rest is
  2472. * ignored.
  2473. *
  2474. * The hardware will never snoop for certain types of accesses:
  2475. * - CPU GTT (GMADR->GGTT->no snoop->memory)
  2476. * - PPGTT page tables
  2477. * - some other special cycles
  2478. *
  2479. * As with BDW, we also need to consider the following for GT accesses:
  2480. * "For GGTT, there is NO pat_sel[2:0] from the entry,
  2481. * so RTL will always use the value corresponding to
  2482. * pat_sel = 000".
  2483. * Which means we must set the snoop bit in PAT entry 0
  2484. * in order to keep the global status page working.
  2485. */
  2486. pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
  2487. GEN8_PPAT(1, 0) |
  2488. GEN8_PPAT(2, 0) |
  2489. GEN8_PPAT(3, 0) |
  2490. GEN8_PPAT(4, CHV_PPAT_SNOOP) |
  2491. GEN8_PPAT(5, CHV_PPAT_SNOOP) |
  2492. GEN8_PPAT(6, CHV_PPAT_SNOOP) |
  2493. GEN8_PPAT(7, CHV_PPAT_SNOOP);
  2494. I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
  2495. I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
  2496. }
  2497. static void gen6_gmch_remove(struct i915_address_space *vm)
  2498. {
  2499. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  2500. iounmap(ggtt->gsm);
  2501. cleanup_scratch_page(vm->i915, &vm->scratch_page);
  2502. }
  2503. static int gen8_gmch_probe(struct i915_ggtt *ggtt)
  2504. {
  2505. struct drm_i915_private *dev_priv = ggtt->base.i915;
  2506. struct pci_dev *pdev = dev_priv->drm.pdev;
  2507. unsigned int size;
  2508. u16 snb_gmch_ctl;
  2509. /* TODO: We're not aware of mappable constraints on gen8 yet */
  2510. ggtt->mappable_base = pci_resource_start(pdev, 2);
  2511. ggtt->mappable_end = pci_resource_len(pdev, 2);
  2512. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
  2513. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
  2514. pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  2515. if (INTEL_GEN(dev_priv) >= 9) {
  2516. ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
  2517. size = gen8_get_total_gtt_size(snb_gmch_ctl);
  2518. } else if (IS_CHERRYVIEW(dev_priv)) {
  2519. ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
  2520. size = chv_get_total_gtt_size(snb_gmch_ctl);
  2521. } else {
  2522. ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
  2523. size = gen8_get_total_gtt_size(snb_gmch_ctl);
  2524. }
  2525. ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
  2526. if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
  2527. chv_setup_private_ppat(dev_priv);
  2528. else
  2529. bdw_setup_private_ppat(dev_priv);
  2530. ggtt->base.cleanup = gen6_gmch_remove;
  2531. ggtt->base.bind_vma = ggtt_bind_vma;
  2532. ggtt->base.unbind_vma = ggtt_unbind_vma;
  2533. ggtt->base.insert_page = gen8_ggtt_insert_page;
  2534. ggtt->base.clear_range = nop_clear_range;
  2535. if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
  2536. ggtt->base.clear_range = gen8_ggtt_clear_range;
  2537. ggtt->base.insert_entries = gen8_ggtt_insert_entries;
  2538. if (IS_CHERRYVIEW(dev_priv))
  2539. ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
  2540. ggtt->invalidate = gen6_ggtt_invalidate;
  2541. return ggtt_probe_common(ggtt, size);
  2542. }
  2543. static int gen6_gmch_probe(struct i915_ggtt *ggtt)
  2544. {
  2545. struct drm_i915_private *dev_priv = ggtt->base.i915;
  2546. struct pci_dev *pdev = dev_priv->drm.pdev;
  2547. unsigned int size;
  2548. u16 snb_gmch_ctl;
  2549. ggtt->mappable_base = pci_resource_start(pdev, 2);
  2550. ggtt->mappable_end = pci_resource_len(pdev, 2);
  2551. /* 64/512MB is the current min/max we actually know of, but this is just
  2552. * a coarse sanity check.
  2553. */
  2554. if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
  2555. DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
  2556. return -ENXIO;
  2557. }
  2558. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
  2559. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
  2560. pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  2561. ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
  2562. size = gen6_get_total_gtt_size(snb_gmch_ctl);
  2563. ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
  2564. ggtt->base.clear_range = gen6_ggtt_clear_range;
  2565. ggtt->base.insert_page = gen6_ggtt_insert_page;
  2566. ggtt->base.insert_entries = gen6_ggtt_insert_entries;
  2567. ggtt->base.bind_vma = ggtt_bind_vma;
  2568. ggtt->base.unbind_vma = ggtt_unbind_vma;
  2569. ggtt->base.cleanup = gen6_gmch_remove;
  2570. ggtt->invalidate = gen6_ggtt_invalidate;
  2571. if (HAS_EDRAM(dev_priv))
  2572. ggtt->base.pte_encode = iris_pte_encode;
  2573. else if (IS_HASWELL(dev_priv))
  2574. ggtt->base.pte_encode = hsw_pte_encode;
  2575. else if (IS_VALLEYVIEW(dev_priv))
  2576. ggtt->base.pte_encode = byt_pte_encode;
  2577. else if (INTEL_GEN(dev_priv) >= 7)
  2578. ggtt->base.pte_encode = ivb_pte_encode;
  2579. else
  2580. ggtt->base.pte_encode = snb_pte_encode;
  2581. return ggtt_probe_common(ggtt, size);
  2582. }
  2583. static void i915_gmch_remove(struct i915_address_space *vm)
  2584. {
  2585. intel_gmch_remove();
  2586. }
  2587. static int i915_gmch_probe(struct i915_ggtt *ggtt)
  2588. {
  2589. struct drm_i915_private *dev_priv = ggtt->base.i915;
  2590. int ret;
  2591. ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
  2592. if (!ret) {
  2593. DRM_ERROR("failed to set up gmch\n");
  2594. return -EIO;
  2595. }
  2596. intel_gtt_get(&ggtt->base.total,
  2597. &ggtt->stolen_size,
  2598. &ggtt->mappable_base,
  2599. &ggtt->mappable_end);
  2600. ggtt->do_idle_maps = needs_idle_maps(dev_priv);
  2601. ggtt->base.insert_page = i915_ggtt_insert_page;
  2602. ggtt->base.insert_entries = i915_ggtt_insert_entries;
  2603. ggtt->base.clear_range = i915_ggtt_clear_range;
  2604. ggtt->base.bind_vma = ggtt_bind_vma;
  2605. ggtt->base.unbind_vma = ggtt_unbind_vma;
  2606. ggtt->base.cleanup = i915_gmch_remove;
  2607. ggtt->invalidate = gmch_ggtt_invalidate;
  2608. if (unlikely(ggtt->do_idle_maps))
  2609. DRM_INFO("applying Ironlake quirks for intel_iommu\n");
  2610. return 0;
  2611. }
  2612. /**
  2613. * i915_ggtt_probe_hw - Probe GGTT hardware location
  2614. * @dev_priv: i915 device
  2615. */
  2616. int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
  2617. {
  2618. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2619. int ret;
  2620. ggtt->base.i915 = dev_priv;
  2621. if (INTEL_GEN(dev_priv) <= 5)
  2622. ret = i915_gmch_probe(ggtt);
  2623. else if (INTEL_GEN(dev_priv) < 8)
  2624. ret = gen6_gmch_probe(ggtt);
  2625. else
  2626. ret = gen8_gmch_probe(ggtt);
  2627. if (ret)
  2628. return ret;
  2629. /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
  2630. * This is easier than doing range restriction on the fly, as we
  2631. * currently don't have any bits spare to pass in this upper
  2632. * restriction!
  2633. */
  2634. if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
  2635. ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
  2636. ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
  2637. }
  2638. if ((ggtt->base.total - 1) >> 32) {
  2639. DRM_ERROR("We never expected a Global GTT with more than 32bits"
  2640. " of address space! Found %lldM!\n",
  2641. ggtt->base.total >> 20);
  2642. ggtt->base.total = 1ULL << 32;
  2643. ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
  2644. }
  2645. if (ggtt->mappable_end > ggtt->base.total) {
  2646. DRM_ERROR("mappable aperture extends past end of GGTT,"
  2647. " aperture=%llx, total=%llx\n",
  2648. ggtt->mappable_end, ggtt->base.total);
  2649. ggtt->mappable_end = ggtt->base.total;
  2650. }
  2651. /* GMADR is the PCI mmio aperture into the global GTT. */
  2652. DRM_INFO("Memory usable by graphics device = %lluM\n",
  2653. ggtt->base.total >> 20);
  2654. DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
  2655. DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
  2656. #ifdef CONFIG_INTEL_IOMMU
  2657. if (intel_iommu_gfx_mapped)
  2658. DRM_INFO("VT-d active for gfx access\n");
  2659. #endif
  2660. return 0;
  2661. }
  2662. /**
  2663. * i915_ggtt_init_hw - Initialize GGTT hardware
  2664. * @dev_priv: i915 device
  2665. */
  2666. int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
  2667. {
  2668. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2669. int ret;
  2670. INIT_LIST_HEAD(&dev_priv->vm_list);
  2671. /* Subtract the guard page before address space initialization to
  2672. * shrink the range used by drm_mm.
  2673. */
  2674. mutex_lock(&dev_priv->drm.struct_mutex);
  2675. ggtt->base.total -= PAGE_SIZE;
  2676. i915_address_space_init(&ggtt->base, dev_priv, "[global]");
  2677. ggtt->base.total += PAGE_SIZE;
  2678. if (!HAS_LLC(dev_priv))
  2679. ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
  2680. mutex_unlock(&dev_priv->drm.struct_mutex);
  2681. if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
  2682. dev_priv->ggtt.mappable_base,
  2683. dev_priv->ggtt.mappable_end)) {
  2684. ret = -EIO;
  2685. goto out_gtt_cleanup;
  2686. }
  2687. ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
  2688. /*
  2689. * Initialise stolen early so that we may reserve preallocated
  2690. * objects for the BIOS to KMS transition.
  2691. */
  2692. ret = i915_gem_init_stolen(dev_priv);
  2693. if (ret)
  2694. goto out_gtt_cleanup;
  2695. return 0;
  2696. out_gtt_cleanup:
  2697. ggtt->base.cleanup(&ggtt->base);
  2698. return ret;
  2699. }
  2700. int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
  2701. {
  2702. if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
  2703. return -EIO;
  2704. return 0;
  2705. }
  2706. void i915_ggtt_enable_guc(struct drm_i915_private *i915)
  2707. {
  2708. i915->ggtt.invalidate = guc_ggtt_invalidate;
  2709. }
  2710. void i915_ggtt_disable_guc(struct drm_i915_private *i915)
  2711. {
  2712. i915->ggtt.invalidate = gen6_ggtt_invalidate;
  2713. }
  2714. void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
  2715. {
  2716. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2717. struct drm_i915_gem_object *obj, *on;
  2718. i915_check_and_clear_faults(dev_priv);
  2719. /* First fill our portion of the GTT with scratch pages */
  2720. ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
  2721. ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
  2722. /* clflush objects bound into the GGTT and rebind them. */
  2723. list_for_each_entry_safe(obj, on,
  2724. &dev_priv->mm.bound_list, global_link) {
  2725. bool ggtt_bound = false;
  2726. struct i915_vma *vma;
  2727. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2728. if (vma->vm != &ggtt->base)
  2729. continue;
  2730. if (!i915_vma_unbind(vma))
  2731. continue;
  2732. WARN_ON(i915_vma_bind(vma, obj->cache_level,
  2733. PIN_UPDATE));
  2734. ggtt_bound = true;
  2735. }
  2736. if (ggtt_bound)
  2737. WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
  2738. }
  2739. ggtt->base.closed = false;
  2740. if (INTEL_GEN(dev_priv) >= 8) {
  2741. if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
  2742. chv_setup_private_ppat(dev_priv);
  2743. else
  2744. bdw_setup_private_ppat(dev_priv);
  2745. return;
  2746. }
  2747. if (USES_PPGTT(dev_priv)) {
  2748. struct i915_address_space *vm;
  2749. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  2750. /* TODO: Perhaps it shouldn't be gen6 specific */
  2751. struct i915_hw_ppgtt *ppgtt;
  2752. if (i915_is_ggtt(vm))
  2753. ppgtt = dev_priv->mm.aliasing_ppgtt;
  2754. else
  2755. ppgtt = i915_vm_to_ppgtt(vm);
  2756. gen6_write_page_range(dev_priv, &ppgtt->pd,
  2757. 0, ppgtt->base.total);
  2758. }
  2759. }
  2760. i915_ggtt_invalidate(dev_priv);
  2761. }
  2762. static struct scatterlist *
  2763. rotate_pages(const dma_addr_t *in, unsigned int offset,
  2764. unsigned int width, unsigned int height,
  2765. unsigned int stride,
  2766. struct sg_table *st, struct scatterlist *sg)
  2767. {
  2768. unsigned int column, row;
  2769. unsigned int src_idx;
  2770. for (column = 0; column < width; column++) {
  2771. src_idx = stride * (height - 1) + column;
  2772. for (row = 0; row < height; row++) {
  2773. st->nents++;
  2774. /* We don't need the pages, but need to initialize
  2775. * the entries so the sg list can be happily traversed.
  2776. * The only thing we need are DMA addresses.
  2777. */
  2778. sg_set_page(sg, NULL, PAGE_SIZE, 0);
  2779. sg_dma_address(sg) = in[offset + src_idx];
  2780. sg_dma_len(sg) = PAGE_SIZE;
  2781. sg = sg_next(sg);
  2782. src_idx -= stride;
  2783. }
  2784. }
  2785. return sg;
  2786. }
  2787. static struct sg_table *
  2788. intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
  2789. struct drm_i915_gem_object *obj)
  2790. {
  2791. const size_t n_pages = obj->base.size / PAGE_SIZE;
  2792. unsigned int size = intel_rotation_info_size(rot_info);
  2793. struct sgt_iter sgt_iter;
  2794. dma_addr_t dma_addr;
  2795. unsigned long i;
  2796. dma_addr_t *page_addr_list;
  2797. struct sg_table *st;
  2798. struct scatterlist *sg;
  2799. int ret = -ENOMEM;
  2800. /* Allocate a temporary list of source pages for random access. */
  2801. page_addr_list = drm_malloc_gfp(n_pages,
  2802. sizeof(dma_addr_t),
  2803. GFP_TEMPORARY);
  2804. if (!page_addr_list)
  2805. return ERR_PTR(ret);
  2806. /* Allocate target SG list. */
  2807. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2808. if (!st)
  2809. goto err_st_alloc;
  2810. ret = sg_alloc_table(st, size, GFP_KERNEL);
  2811. if (ret)
  2812. goto err_sg_alloc;
  2813. /* Populate source page list from the object. */
  2814. i = 0;
  2815. for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
  2816. page_addr_list[i++] = dma_addr;
  2817. GEM_BUG_ON(i != n_pages);
  2818. st->nents = 0;
  2819. sg = st->sgl;
  2820. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
  2821. sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
  2822. rot_info->plane[i].width, rot_info->plane[i].height,
  2823. rot_info->plane[i].stride, st, sg);
  2824. }
  2825. DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
  2826. obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
  2827. drm_free_large(page_addr_list);
  2828. return st;
  2829. err_sg_alloc:
  2830. kfree(st);
  2831. err_st_alloc:
  2832. drm_free_large(page_addr_list);
  2833. DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
  2834. obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
  2835. return ERR_PTR(ret);
  2836. }
  2837. static struct sg_table *
  2838. intel_partial_pages(const struct i915_ggtt_view *view,
  2839. struct drm_i915_gem_object *obj)
  2840. {
  2841. struct sg_table *st;
  2842. struct scatterlist *sg, *iter;
  2843. unsigned int count = view->partial.size;
  2844. unsigned int offset;
  2845. int ret = -ENOMEM;
  2846. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2847. if (!st)
  2848. goto err_st_alloc;
  2849. ret = sg_alloc_table(st, count, GFP_KERNEL);
  2850. if (ret)
  2851. goto err_sg_alloc;
  2852. iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
  2853. GEM_BUG_ON(!iter);
  2854. sg = st->sgl;
  2855. st->nents = 0;
  2856. do {
  2857. unsigned int len;
  2858. len = min(iter->length - (offset << PAGE_SHIFT),
  2859. count << PAGE_SHIFT);
  2860. sg_set_page(sg, NULL, len, 0);
  2861. sg_dma_address(sg) =
  2862. sg_dma_address(iter) + (offset << PAGE_SHIFT);
  2863. sg_dma_len(sg) = len;
  2864. st->nents++;
  2865. count -= len >> PAGE_SHIFT;
  2866. if (count == 0) {
  2867. sg_mark_end(sg);
  2868. return st;
  2869. }
  2870. sg = __sg_next(sg);
  2871. iter = __sg_next(iter);
  2872. offset = 0;
  2873. } while (1);
  2874. err_sg_alloc:
  2875. kfree(st);
  2876. err_st_alloc:
  2877. return ERR_PTR(ret);
  2878. }
  2879. static int
  2880. i915_get_ggtt_vma_pages(struct i915_vma *vma)
  2881. {
  2882. int ret = 0;
  2883. /* The vma->pages are only valid within the lifespan of the borrowed
  2884. * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
  2885. * must be the vma->pages. A simple rule is that vma->pages must only
  2886. * be accessed when the obj->mm.pages are pinned.
  2887. */
  2888. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
  2889. if (vma->pages)
  2890. return 0;
  2891. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
  2892. vma->pages = vma->obj->mm.pages;
  2893. else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
  2894. vma->pages =
  2895. intel_rotate_fb_obj_pages(&vma->ggtt_view.rotated,
  2896. vma->obj);
  2897. else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
  2898. vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
  2899. else
  2900. WARN_ONCE(1, "GGTT view %u not implemented!\n",
  2901. vma->ggtt_view.type);
  2902. if (!vma->pages) {
  2903. DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
  2904. vma->ggtt_view.type);
  2905. ret = -EINVAL;
  2906. } else if (IS_ERR(vma->pages)) {
  2907. ret = PTR_ERR(vma->pages);
  2908. vma->pages = NULL;
  2909. DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
  2910. vma->ggtt_view.type, ret);
  2911. }
  2912. return ret;
  2913. }
  2914. /**
  2915. * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
  2916. * @vm: the &struct i915_address_space
  2917. * @node: the &struct drm_mm_node (typically i915_vma.mode)
  2918. * @size: how much space to allocate inside the GTT,
  2919. * must be #I915_GTT_PAGE_SIZE aligned
  2920. * @offset: where to insert inside the GTT,
  2921. * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
  2922. * (@offset + @size) must fit within the address space
  2923. * @color: color to apply to node, if this node is not from a VMA,
  2924. * color must be #I915_COLOR_UNEVICTABLE
  2925. * @flags: control search and eviction behaviour
  2926. *
  2927. * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
  2928. * the address space (using @size and @color). If the @node does not fit, it
  2929. * tries to evict any overlapping nodes from the GTT, including any
  2930. * neighbouring nodes if the colors do not match (to ensure guard pages between
  2931. * differing domains). See i915_gem_evict_for_node() for the gory details
  2932. * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
  2933. * evicting active overlapping objects, and any overlapping node that is pinned
  2934. * or marked as unevictable will also result in failure.
  2935. *
  2936. * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
  2937. * asked to wait for eviction and interrupted.
  2938. */
  2939. int i915_gem_gtt_reserve(struct i915_address_space *vm,
  2940. struct drm_mm_node *node,
  2941. u64 size, u64 offset, unsigned long color,
  2942. unsigned int flags)
  2943. {
  2944. int err;
  2945. GEM_BUG_ON(!size);
  2946. GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
  2947. GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
  2948. GEM_BUG_ON(range_overflows(offset, size, vm->total));
  2949. GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
  2950. GEM_BUG_ON(drm_mm_node_allocated(node));
  2951. node->size = size;
  2952. node->start = offset;
  2953. node->color = color;
  2954. err = drm_mm_reserve_node(&vm->mm, node);
  2955. if (err != -ENOSPC)
  2956. return err;
  2957. err = i915_gem_evict_for_node(vm, node, flags);
  2958. if (err == 0)
  2959. err = drm_mm_reserve_node(&vm->mm, node);
  2960. return err;
  2961. }
  2962. static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
  2963. {
  2964. u64 range, addr;
  2965. GEM_BUG_ON(range_overflows(start, len, end));
  2966. GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
  2967. range = round_down(end - len, align) - round_up(start, align);
  2968. if (range) {
  2969. if (sizeof(unsigned long) == sizeof(u64)) {
  2970. addr = get_random_long();
  2971. } else {
  2972. addr = get_random_int();
  2973. if (range > U32_MAX) {
  2974. addr <<= 32;
  2975. addr |= get_random_int();
  2976. }
  2977. }
  2978. div64_u64_rem(addr, range, &addr);
  2979. start += addr;
  2980. }
  2981. return round_up(start, align);
  2982. }
  2983. /**
  2984. * i915_gem_gtt_insert - insert a node into an address_space (GTT)
  2985. * @vm: the &struct i915_address_space
  2986. * @node: the &struct drm_mm_node (typically i915_vma.node)
  2987. * @size: how much space to allocate inside the GTT,
  2988. * must be #I915_GTT_PAGE_SIZE aligned
  2989. * @alignment: required alignment of starting offset, may be 0 but
  2990. * if specified, this must be a power-of-two and at least
  2991. * #I915_GTT_MIN_ALIGNMENT
  2992. * @color: color to apply to node
  2993. * @start: start of any range restriction inside GTT (0 for all),
  2994. * must be #I915_GTT_PAGE_SIZE aligned
  2995. * @end: end of any range restriction inside GTT (U64_MAX for all),
  2996. * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
  2997. * @flags: control search and eviction behaviour
  2998. *
  2999. * i915_gem_gtt_insert() first searches for an available hole into which
  3000. * is can insert the node. The hole address is aligned to @alignment and
  3001. * its @size must then fit entirely within the [@start, @end] bounds. The
  3002. * nodes on either side of the hole must match @color, or else a guard page
  3003. * will be inserted between the two nodes (or the node evicted). If no
  3004. * suitable hole is found, first a victim is randomly selected and tested
  3005. * for eviction, otherwise then the LRU list of objects within the GTT
  3006. * is scanned to find the first set of replacement nodes to create the hole.
  3007. * Those old overlapping nodes are evicted from the GTT (and so must be
  3008. * rebound before any future use). Any node that is currently pinned cannot
  3009. * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
  3010. * active and #PIN_NONBLOCK is specified, that node is also skipped when
  3011. * searching for an eviction candidate. See i915_gem_evict_something() for
  3012. * the gory details on the eviction algorithm.
  3013. *
  3014. * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
  3015. * asked to wait for eviction and interrupted.
  3016. */
  3017. int i915_gem_gtt_insert(struct i915_address_space *vm,
  3018. struct drm_mm_node *node,
  3019. u64 size, u64 alignment, unsigned long color,
  3020. u64 start, u64 end, unsigned int flags)
  3021. {
  3022. u32 search_flag, alloc_flag;
  3023. u64 offset;
  3024. int err;
  3025. lockdep_assert_held(&vm->i915->drm.struct_mutex);
  3026. GEM_BUG_ON(!size);
  3027. GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
  3028. GEM_BUG_ON(alignment && !is_power_of_2(alignment));
  3029. GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
  3030. GEM_BUG_ON(start >= end);
  3031. GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
  3032. GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
  3033. GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
  3034. GEM_BUG_ON(drm_mm_node_allocated(node));
  3035. if (unlikely(range_overflows(start, size, end)))
  3036. return -ENOSPC;
  3037. if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
  3038. return -ENOSPC;
  3039. if (flags & PIN_HIGH) {
  3040. search_flag = DRM_MM_SEARCH_BELOW;
  3041. alloc_flag = DRM_MM_CREATE_TOP;
  3042. } else {
  3043. search_flag = DRM_MM_SEARCH_DEFAULT;
  3044. alloc_flag = DRM_MM_CREATE_DEFAULT;
  3045. }
  3046. /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
  3047. * so we know that we always have a minimum alignment of 4096.
  3048. * The drm_mm range manager is optimised to return results
  3049. * with zero alignment, so where possible use the optimal
  3050. * path.
  3051. */
  3052. BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
  3053. if (alignment <= I915_GTT_MIN_ALIGNMENT)
  3054. alignment = 0;
  3055. err = drm_mm_insert_node_in_range_generic(&vm->mm, node,
  3056. size, alignment, color,
  3057. start, end,
  3058. search_flag, alloc_flag);
  3059. if (err != -ENOSPC)
  3060. return err;
  3061. /* No free space, pick a slot at random.
  3062. *
  3063. * There is a pathological case here using a GTT shared between
  3064. * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
  3065. *
  3066. * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
  3067. * (64k objects) (448k objects)
  3068. *
  3069. * Now imagine that the eviction LRU is ordered top-down (just because
  3070. * pathology meets real life), and that we need to evict an object to
  3071. * make room inside the aperture. The eviction scan then has to walk
  3072. * the 448k list before it finds one within range. And now imagine that
  3073. * it has to search for a new hole between every byte inside the memcpy,
  3074. * for several simultaneous clients.
  3075. *
  3076. * On a full-ppgtt system, if we have run out of available space, there
  3077. * will be lots and lots of objects in the eviction list! Again,
  3078. * searching that LRU list may be slow if we are also applying any
  3079. * range restrictions (e.g. restriction to low 4GiB) and so, for
  3080. * simplicity and similarilty between different GTT, try the single
  3081. * random replacement first.
  3082. */
  3083. offset = random_offset(start, end,
  3084. size, alignment ?: I915_GTT_MIN_ALIGNMENT);
  3085. err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
  3086. if (err != -ENOSPC)
  3087. return err;
  3088. /* Randomly selected placement is pinned, do a search */
  3089. err = i915_gem_evict_something(vm, size, alignment, color,
  3090. start, end, flags);
  3091. if (err)
  3092. return err;
  3093. search_flag = DRM_MM_SEARCH_DEFAULT;
  3094. return drm_mm_insert_node_in_range_generic(&vm->mm, node,
  3095. size, alignment, color,
  3096. start, end,
  3097. search_flag, alloc_flag);
  3098. }