i915_gem_gtt.c 106 KB

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