i915_gem.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_gem_dmabuf.h"
  32. #include "i915_vgpu.h"
  33. #include "i915_trace.h"
  34. #include "intel_drv.h"
  35. #include "intel_frontbuffer.h"
  36. #include "intel_mocs.h"
  37. #include <linux/reservation.h>
  38. #include <linux/shmem_fs.h>
  39. #include <linux/slab.h>
  40. #include <linux/swap.h>
  41. #include <linux/pci.h>
  42. #include <linux/dma-buf.h>
  43. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  44. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  45. static bool cpu_cache_is_coherent(struct drm_device *dev,
  46. enum i915_cache_level level)
  47. {
  48. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  49. }
  50. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  51. {
  52. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  53. return false;
  54. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  55. return true;
  56. return obj->pin_display;
  57. }
  58. static int
  59. insert_mappable_node(struct drm_i915_private *i915,
  60. struct drm_mm_node *node, u32 size)
  61. {
  62. memset(node, 0, sizeof(*node));
  63. return drm_mm_insert_node_in_range_generic(&i915->ggtt.base.mm, node,
  64. size, 0, 0, 0,
  65. i915->ggtt.mappable_end,
  66. DRM_MM_SEARCH_DEFAULT,
  67. DRM_MM_CREATE_DEFAULT);
  68. }
  69. static void
  70. remove_mappable_node(struct drm_mm_node *node)
  71. {
  72. drm_mm_remove_node(node);
  73. }
  74. /* some bookkeeping */
  75. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  76. size_t size)
  77. {
  78. spin_lock(&dev_priv->mm.object_stat_lock);
  79. dev_priv->mm.object_count++;
  80. dev_priv->mm.object_memory += size;
  81. spin_unlock(&dev_priv->mm.object_stat_lock);
  82. }
  83. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  84. size_t size)
  85. {
  86. spin_lock(&dev_priv->mm.object_stat_lock);
  87. dev_priv->mm.object_count--;
  88. dev_priv->mm.object_memory -= size;
  89. spin_unlock(&dev_priv->mm.object_stat_lock);
  90. }
  91. static int
  92. i915_gem_wait_for_error(struct i915_gpu_error *error)
  93. {
  94. int ret;
  95. if (!i915_reset_in_progress(error))
  96. return 0;
  97. /*
  98. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  99. * userspace. If it takes that long something really bad is going on and
  100. * we should simply try to bail out and fail as gracefully as possible.
  101. */
  102. ret = wait_event_interruptible_timeout(error->reset_queue,
  103. !i915_reset_in_progress(error),
  104. 10*HZ);
  105. if (ret == 0) {
  106. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  107. return -EIO;
  108. } else if (ret < 0) {
  109. return ret;
  110. } else {
  111. return 0;
  112. }
  113. }
  114. int i915_mutex_lock_interruptible(struct drm_device *dev)
  115. {
  116. struct drm_i915_private *dev_priv = to_i915(dev);
  117. int ret;
  118. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  119. if (ret)
  120. return ret;
  121. ret = mutex_lock_interruptible(&dev->struct_mutex);
  122. if (ret)
  123. return ret;
  124. return 0;
  125. }
  126. int
  127. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  128. struct drm_file *file)
  129. {
  130. struct drm_i915_private *dev_priv = to_i915(dev);
  131. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  132. struct drm_i915_gem_get_aperture *args = data;
  133. struct i915_vma *vma;
  134. size_t pinned;
  135. pinned = 0;
  136. mutex_lock(&dev->struct_mutex);
  137. list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
  138. if (i915_vma_is_pinned(vma))
  139. pinned += vma->node.size;
  140. list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
  141. if (i915_vma_is_pinned(vma))
  142. pinned += vma->node.size;
  143. mutex_unlock(&dev->struct_mutex);
  144. args->aper_size = ggtt->base.total;
  145. args->aper_available_size = args->aper_size - pinned;
  146. return 0;
  147. }
  148. static int
  149. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  150. {
  151. struct address_space *mapping = obj->base.filp->f_mapping;
  152. char *vaddr = obj->phys_handle->vaddr;
  153. struct sg_table *st;
  154. struct scatterlist *sg;
  155. int i;
  156. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  157. return -EINVAL;
  158. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  159. struct page *page;
  160. char *src;
  161. page = shmem_read_mapping_page(mapping, i);
  162. if (IS_ERR(page))
  163. return PTR_ERR(page);
  164. src = kmap_atomic(page);
  165. memcpy(vaddr, src, PAGE_SIZE);
  166. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  167. kunmap_atomic(src);
  168. put_page(page);
  169. vaddr += PAGE_SIZE;
  170. }
  171. i915_gem_chipset_flush(to_i915(obj->base.dev));
  172. st = kmalloc(sizeof(*st), GFP_KERNEL);
  173. if (st == NULL)
  174. return -ENOMEM;
  175. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  176. kfree(st);
  177. return -ENOMEM;
  178. }
  179. sg = st->sgl;
  180. sg->offset = 0;
  181. sg->length = obj->base.size;
  182. sg_dma_address(sg) = obj->phys_handle->busaddr;
  183. sg_dma_len(sg) = obj->base.size;
  184. obj->pages = st;
  185. return 0;
  186. }
  187. static void
  188. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
  189. {
  190. int ret;
  191. BUG_ON(obj->madv == __I915_MADV_PURGED);
  192. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  193. if (WARN_ON(ret)) {
  194. /* In the event of a disaster, abandon all caches and
  195. * hope for the best.
  196. */
  197. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  198. }
  199. if (obj->madv == I915_MADV_DONTNEED)
  200. obj->dirty = 0;
  201. if (obj->dirty) {
  202. struct address_space *mapping = obj->base.filp->f_mapping;
  203. char *vaddr = obj->phys_handle->vaddr;
  204. int i;
  205. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  206. struct page *page;
  207. char *dst;
  208. page = shmem_read_mapping_page(mapping, i);
  209. if (IS_ERR(page))
  210. continue;
  211. dst = kmap_atomic(page);
  212. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  213. memcpy(dst, vaddr, PAGE_SIZE);
  214. kunmap_atomic(dst);
  215. set_page_dirty(page);
  216. if (obj->madv == I915_MADV_WILLNEED)
  217. mark_page_accessed(page);
  218. put_page(page);
  219. vaddr += PAGE_SIZE;
  220. }
  221. obj->dirty = 0;
  222. }
  223. sg_free_table(obj->pages);
  224. kfree(obj->pages);
  225. }
  226. static void
  227. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  228. {
  229. drm_pci_free(obj->base.dev, obj->phys_handle);
  230. }
  231. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  232. .get_pages = i915_gem_object_get_pages_phys,
  233. .put_pages = i915_gem_object_put_pages_phys,
  234. .release = i915_gem_object_release_phys,
  235. };
  236. int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  237. {
  238. struct i915_vma *vma;
  239. LIST_HEAD(still_in_list);
  240. int ret;
  241. lockdep_assert_held(&obj->base.dev->struct_mutex);
  242. /* Closed vma are removed from the obj->vma_list - but they may
  243. * still have an active binding on the object. To remove those we
  244. * must wait for all rendering to complete to the object (as unbinding
  245. * must anyway), and retire the requests.
  246. */
  247. ret = i915_gem_object_wait_rendering(obj, false);
  248. if (ret)
  249. return ret;
  250. i915_gem_retire_requests(to_i915(obj->base.dev));
  251. while ((vma = list_first_entry_or_null(&obj->vma_list,
  252. struct i915_vma,
  253. obj_link))) {
  254. list_move_tail(&vma->obj_link, &still_in_list);
  255. ret = i915_vma_unbind(vma);
  256. if (ret)
  257. break;
  258. }
  259. list_splice(&still_in_list, &obj->vma_list);
  260. return ret;
  261. }
  262. /**
  263. * Ensures that all rendering to the object has completed and the object is
  264. * safe to unbind from the GTT or access from the CPU.
  265. * @obj: i915 gem object
  266. * @readonly: waiting for just read access or read-write access
  267. */
  268. int
  269. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  270. bool readonly)
  271. {
  272. struct reservation_object *resv;
  273. struct i915_gem_active *active;
  274. unsigned long active_mask;
  275. int idx;
  276. lockdep_assert_held(&obj->base.dev->struct_mutex);
  277. if (!readonly) {
  278. active = obj->last_read;
  279. active_mask = i915_gem_object_get_active(obj);
  280. } else {
  281. active_mask = 1;
  282. active = &obj->last_write;
  283. }
  284. for_each_active(active_mask, idx) {
  285. int ret;
  286. ret = i915_gem_active_wait(&active[idx],
  287. &obj->base.dev->struct_mutex);
  288. if (ret)
  289. return ret;
  290. }
  291. resv = i915_gem_object_get_dmabuf_resv(obj);
  292. if (resv) {
  293. long err;
  294. err = reservation_object_wait_timeout_rcu(resv, !readonly, true,
  295. MAX_SCHEDULE_TIMEOUT);
  296. if (err < 0)
  297. return err;
  298. }
  299. return 0;
  300. }
  301. /* A nonblocking variant of the above wait. Must be called prior to
  302. * acquiring the mutex for the object, as the object state may change
  303. * during this call. A reference must be held by the caller for the object.
  304. */
  305. static __must_check int
  306. __unsafe_wait_rendering(struct drm_i915_gem_object *obj,
  307. struct intel_rps_client *rps,
  308. bool readonly)
  309. {
  310. struct i915_gem_active *active;
  311. unsigned long active_mask;
  312. int idx;
  313. active_mask = __I915_BO_ACTIVE(obj);
  314. if (!active_mask)
  315. return 0;
  316. if (!readonly) {
  317. active = obj->last_read;
  318. } else {
  319. active_mask = 1;
  320. active = &obj->last_write;
  321. }
  322. for_each_active(active_mask, idx) {
  323. int ret;
  324. ret = i915_gem_active_wait_unlocked(&active[idx],
  325. I915_WAIT_INTERRUPTIBLE,
  326. NULL, rps);
  327. if (ret)
  328. return ret;
  329. }
  330. return 0;
  331. }
  332. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  333. {
  334. struct drm_i915_file_private *fpriv = file->driver_priv;
  335. return &fpriv->rps;
  336. }
  337. int
  338. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  339. int align)
  340. {
  341. drm_dma_handle_t *phys;
  342. int ret;
  343. if (obj->phys_handle) {
  344. if ((unsigned long)obj->phys_handle->vaddr & (align -1))
  345. return -EBUSY;
  346. return 0;
  347. }
  348. if (obj->madv != I915_MADV_WILLNEED)
  349. return -EFAULT;
  350. if (obj->base.filp == NULL)
  351. return -EINVAL;
  352. ret = i915_gem_object_unbind(obj);
  353. if (ret)
  354. return ret;
  355. ret = i915_gem_object_put_pages(obj);
  356. if (ret)
  357. return ret;
  358. /* create a new object */
  359. phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
  360. if (!phys)
  361. return -ENOMEM;
  362. obj->phys_handle = phys;
  363. obj->ops = &i915_gem_phys_ops;
  364. return i915_gem_object_get_pages(obj);
  365. }
  366. static int
  367. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  368. struct drm_i915_gem_pwrite *args,
  369. struct drm_file *file_priv)
  370. {
  371. struct drm_device *dev = obj->base.dev;
  372. void *vaddr = obj->phys_handle->vaddr + args->offset;
  373. char __user *user_data = u64_to_user_ptr(args->data_ptr);
  374. int ret = 0;
  375. /* We manually control the domain here and pretend that it
  376. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  377. */
  378. ret = i915_gem_object_wait_rendering(obj, false);
  379. if (ret)
  380. return ret;
  381. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  382. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  383. unsigned long unwritten;
  384. /* The physical object once assigned is fixed for the lifetime
  385. * of the obj, so we can safely drop the lock and continue
  386. * to access vaddr.
  387. */
  388. mutex_unlock(&dev->struct_mutex);
  389. unwritten = copy_from_user(vaddr, user_data, args->size);
  390. mutex_lock(&dev->struct_mutex);
  391. if (unwritten) {
  392. ret = -EFAULT;
  393. goto out;
  394. }
  395. }
  396. drm_clflush_virt_range(vaddr, args->size);
  397. i915_gem_chipset_flush(to_i915(dev));
  398. out:
  399. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  400. return ret;
  401. }
  402. void *i915_gem_object_alloc(struct drm_device *dev)
  403. {
  404. struct drm_i915_private *dev_priv = to_i915(dev);
  405. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  406. }
  407. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  408. {
  409. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  410. kmem_cache_free(dev_priv->objects, obj);
  411. }
  412. static int
  413. i915_gem_create(struct drm_file *file,
  414. struct drm_device *dev,
  415. uint64_t size,
  416. uint32_t *handle_p)
  417. {
  418. struct drm_i915_gem_object *obj;
  419. int ret;
  420. u32 handle;
  421. size = roundup(size, PAGE_SIZE);
  422. if (size == 0)
  423. return -EINVAL;
  424. /* Allocate the new object */
  425. obj = i915_gem_object_create(dev, size);
  426. if (IS_ERR(obj))
  427. return PTR_ERR(obj);
  428. ret = drm_gem_handle_create(file, &obj->base, &handle);
  429. /* drop reference from allocate - handle holds it now */
  430. i915_gem_object_put_unlocked(obj);
  431. if (ret)
  432. return ret;
  433. *handle_p = handle;
  434. return 0;
  435. }
  436. int
  437. i915_gem_dumb_create(struct drm_file *file,
  438. struct drm_device *dev,
  439. struct drm_mode_create_dumb *args)
  440. {
  441. /* have to work out size/pitch and return them */
  442. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  443. args->size = args->pitch * args->height;
  444. return i915_gem_create(file, dev,
  445. args->size, &args->handle);
  446. }
  447. /**
  448. * Creates a new mm object and returns a handle to it.
  449. * @dev: drm device pointer
  450. * @data: ioctl data blob
  451. * @file: drm file pointer
  452. */
  453. int
  454. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  455. struct drm_file *file)
  456. {
  457. struct drm_i915_gem_create *args = data;
  458. return i915_gem_create(file, dev,
  459. args->size, &args->handle);
  460. }
  461. static inline int
  462. __copy_to_user_swizzled(char __user *cpu_vaddr,
  463. const char *gpu_vaddr, int gpu_offset,
  464. int length)
  465. {
  466. int ret, cpu_offset = 0;
  467. while (length > 0) {
  468. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  469. int this_length = min(cacheline_end - gpu_offset, length);
  470. int swizzled_gpu_offset = gpu_offset ^ 64;
  471. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  472. gpu_vaddr + swizzled_gpu_offset,
  473. this_length);
  474. if (ret)
  475. return ret + length;
  476. cpu_offset += this_length;
  477. gpu_offset += this_length;
  478. length -= this_length;
  479. }
  480. return 0;
  481. }
  482. static inline int
  483. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  484. const char __user *cpu_vaddr,
  485. int length)
  486. {
  487. int ret, cpu_offset = 0;
  488. while (length > 0) {
  489. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  490. int this_length = min(cacheline_end - gpu_offset, length);
  491. int swizzled_gpu_offset = gpu_offset ^ 64;
  492. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  493. cpu_vaddr + cpu_offset,
  494. this_length);
  495. if (ret)
  496. return ret + length;
  497. cpu_offset += this_length;
  498. gpu_offset += this_length;
  499. length -= this_length;
  500. }
  501. return 0;
  502. }
  503. /*
  504. * Pins the specified object's pages and synchronizes the object with
  505. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  506. * flush the object from the CPU cache.
  507. */
  508. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  509. unsigned int *needs_clflush)
  510. {
  511. int ret;
  512. *needs_clflush = 0;
  513. if (!i915_gem_object_has_struct_page(obj))
  514. return -ENODEV;
  515. ret = i915_gem_object_wait_rendering(obj, true);
  516. if (ret)
  517. return ret;
  518. ret = i915_gem_object_get_pages(obj);
  519. if (ret)
  520. return ret;
  521. i915_gem_object_pin_pages(obj);
  522. i915_gem_object_flush_gtt_write_domain(obj);
  523. /* If we're not in the cpu read domain, set ourself into the gtt
  524. * read domain and manually flush cachelines (if required). This
  525. * optimizes for the case when the gpu will dirty the data
  526. * anyway again before the next pread happens.
  527. */
  528. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  529. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  530. obj->cache_level);
  531. if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  532. ret = i915_gem_object_set_to_cpu_domain(obj, false);
  533. if (ret)
  534. goto err_unpin;
  535. *needs_clflush = 0;
  536. }
  537. /* return with the pages pinned */
  538. return 0;
  539. err_unpin:
  540. i915_gem_object_unpin_pages(obj);
  541. return ret;
  542. }
  543. int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
  544. unsigned int *needs_clflush)
  545. {
  546. int ret;
  547. *needs_clflush = 0;
  548. if (!i915_gem_object_has_struct_page(obj))
  549. return -ENODEV;
  550. ret = i915_gem_object_wait_rendering(obj, false);
  551. if (ret)
  552. return ret;
  553. ret = i915_gem_object_get_pages(obj);
  554. if (ret)
  555. return ret;
  556. i915_gem_object_pin_pages(obj);
  557. i915_gem_object_flush_gtt_write_domain(obj);
  558. /* If we're not in the cpu write domain, set ourself into the
  559. * gtt write domain and manually flush cachelines (as required).
  560. * This optimizes for the case when the gpu will use the data
  561. * right away and we therefore have to clflush anyway.
  562. */
  563. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  564. *needs_clflush |= cpu_write_needs_clflush(obj) << 1;
  565. /* Same trick applies to invalidate partially written cachelines read
  566. * before writing.
  567. */
  568. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  569. *needs_clflush |= !cpu_cache_is_coherent(obj->base.dev,
  570. obj->cache_level);
  571. if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  572. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  573. if (ret)
  574. goto err_unpin;
  575. *needs_clflush = 0;
  576. }
  577. if ((*needs_clflush & CLFLUSH_AFTER) == 0)
  578. obj->cache_dirty = true;
  579. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  580. obj->dirty = 1;
  581. /* return with the pages pinned */
  582. return 0;
  583. err_unpin:
  584. i915_gem_object_unpin_pages(obj);
  585. return ret;
  586. }
  587. /* Per-page copy function for the shmem pread fastpath.
  588. * Flushes invalid cachelines before reading the target if
  589. * needs_clflush is set. */
  590. static int
  591. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  592. char __user *user_data,
  593. bool page_do_bit17_swizzling, bool needs_clflush)
  594. {
  595. char *vaddr;
  596. int ret;
  597. if (unlikely(page_do_bit17_swizzling))
  598. return -EINVAL;
  599. vaddr = kmap_atomic(page);
  600. if (needs_clflush)
  601. drm_clflush_virt_range(vaddr + shmem_page_offset,
  602. page_length);
  603. ret = __copy_to_user_inatomic(user_data,
  604. vaddr + shmem_page_offset,
  605. page_length);
  606. kunmap_atomic(vaddr);
  607. return ret ? -EFAULT : 0;
  608. }
  609. static void
  610. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  611. bool swizzled)
  612. {
  613. if (unlikely(swizzled)) {
  614. unsigned long start = (unsigned long) addr;
  615. unsigned long end = (unsigned long) addr + length;
  616. /* For swizzling simply ensure that we always flush both
  617. * channels. Lame, but simple and it works. Swizzled
  618. * pwrite/pread is far from a hotpath - current userspace
  619. * doesn't use it at all. */
  620. start = round_down(start, 128);
  621. end = round_up(end, 128);
  622. drm_clflush_virt_range((void *)start, end - start);
  623. } else {
  624. drm_clflush_virt_range(addr, length);
  625. }
  626. }
  627. /* Only difference to the fast-path function is that this can handle bit17
  628. * and uses non-atomic copy and kmap functions. */
  629. static int
  630. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  631. char __user *user_data,
  632. bool page_do_bit17_swizzling, bool needs_clflush)
  633. {
  634. char *vaddr;
  635. int ret;
  636. vaddr = kmap(page);
  637. if (needs_clflush)
  638. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  639. page_length,
  640. page_do_bit17_swizzling);
  641. if (page_do_bit17_swizzling)
  642. ret = __copy_to_user_swizzled(user_data,
  643. vaddr, shmem_page_offset,
  644. page_length);
  645. else
  646. ret = __copy_to_user(user_data,
  647. vaddr + shmem_page_offset,
  648. page_length);
  649. kunmap(page);
  650. return ret ? - EFAULT : 0;
  651. }
  652. static inline unsigned long
  653. slow_user_access(struct io_mapping *mapping,
  654. uint64_t page_base, int page_offset,
  655. char __user *user_data,
  656. unsigned long length, bool pwrite)
  657. {
  658. void __iomem *ioaddr;
  659. void *vaddr;
  660. uint64_t unwritten;
  661. ioaddr = io_mapping_map_wc(mapping, page_base, PAGE_SIZE);
  662. /* We can use the cpu mem copy function because this is X86. */
  663. vaddr = (void __force *)ioaddr + page_offset;
  664. if (pwrite)
  665. unwritten = __copy_from_user(vaddr, user_data, length);
  666. else
  667. unwritten = __copy_to_user(user_data, vaddr, length);
  668. io_mapping_unmap(ioaddr);
  669. return unwritten;
  670. }
  671. static int
  672. i915_gem_gtt_pread(struct drm_device *dev,
  673. struct drm_i915_gem_object *obj, uint64_t size,
  674. uint64_t data_offset, uint64_t data_ptr)
  675. {
  676. struct drm_i915_private *dev_priv = to_i915(dev);
  677. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  678. struct i915_vma *vma;
  679. struct drm_mm_node node;
  680. char __user *user_data;
  681. uint64_t remain;
  682. uint64_t offset;
  683. int ret;
  684. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
  685. if (!IS_ERR(vma)) {
  686. node.start = i915_ggtt_offset(vma);
  687. node.allocated = false;
  688. ret = i915_vma_put_fence(vma);
  689. if (ret) {
  690. i915_vma_unpin(vma);
  691. vma = ERR_PTR(ret);
  692. }
  693. }
  694. if (IS_ERR(vma)) {
  695. ret = insert_mappable_node(dev_priv, &node, PAGE_SIZE);
  696. if (ret)
  697. goto out;
  698. ret = i915_gem_object_get_pages(obj);
  699. if (ret) {
  700. remove_mappable_node(&node);
  701. goto out;
  702. }
  703. i915_gem_object_pin_pages(obj);
  704. }
  705. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  706. if (ret)
  707. goto out_unpin;
  708. user_data = u64_to_user_ptr(data_ptr);
  709. remain = size;
  710. offset = data_offset;
  711. mutex_unlock(&dev->struct_mutex);
  712. if (likely(!i915.prefault_disable)) {
  713. ret = fault_in_multipages_writeable(user_data, remain);
  714. if (ret) {
  715. mutex_lock(&dev->struct_mutex);
  716. goto out_unpin;
  717. }
  718. }
  719. while (remain > 0) {
  720. /* Operation in this page
  721. *
  722. * page_base = page offset within aperture
  723. * page_offset = offset within page
  724. * page_length = bytes to copy for this page
  725. */
  726. u32 page_base = node.start;
  727. unsigned page_offset = offset_in_page(offset);
  728. unsigned page_length = PAGE_SIZE - page_offset;
  729. page_length = remain < page_length ? remain : page_length;
  730. if (node.allocated) {
  731. wmb();
  732. ggtt->base.insert_page(&ggtt->base,
  733. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  734. node.start,
  735. I915_CACHE_NONE, 0);
  736. wmb();
  737. } else {
  738. page_base += offset & PAGE_MASK;
  739. }
  740. /* This is a slow read/write as it tries to read from
  741. * and write to user memory which may result into page
  742. * faults, and so we cannot perform this under struct_mutex.
  743. */
  744. if (slow_user_access(&ggtt->mappable, page_base,
  745. page_offset, user_data,
  746. page_length, false)) {
  747. ret = -EFAULT;
  748. break;
  749. }
  750. remain -= page_length;
  751. user_data += page_length;
  752. offset += page_length;
  753. }
  754. mutex_lock(&dev->struct_mutex);
  755. if (ret == 0 && (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) {
  756. /* The user has modified the object whilst we tried
  757. * reading from it, and we now have no idea what domain
  758. * the pages should be in. As we have just been touching
  759. * them directly, flush everything back to the GTT
  760. * domain.
  761. */
  762. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  763. }
  764. out_unpin:
  765. if (node.allocated) {
  766. wmb();
  767. ggtt->base.clear_range(&ggtt->base,
  768. node.start, node.size,
  769. true);
  770. i915_gem_object_unpin_pages(obj);
  771. remove_mappable_node(&node);
  772. } else {
  773. i915_vma_unpin(vma);
  774. }
  775. out:
  776. return ret;
  777. }
  778. static int
  779. i915_gem_shmem_pread(struct drm_device *dev,
  780. struct drm_i915_gem_object *obj,
  781. struct drm_i915_gem_pread *args,
  782. struct drm_file *file)
  783. {
  784. char __user *user_data;
  785. ssize_t remain;
  786. loff_t offset;
  787. int shmem_page_offset, page_length, ret = 0;
  788. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  789. int prefaulted = 0;
  790. int needs_clflush = 0;
  791. struct sg_page_iter sg_iter;
  792. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  793. if (ret)
  794. return ret;
  795. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  796. user_data = u64_to_user_ptr(args->data_ptr);
  797. offset = args->offset;
  798. remain = args->size;
  799. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  800. offset >> PAGE_SHIFT) {
  801. struct page *page = sg_page_iter_page(&sg_iter);
  802. if (remain <= 0)
  803. break;
  804. /* Operation in this page
  805. *
  806. * shmem_page_offset = offset within page in shmem file
  807. * page_length = bytes to copy for this page
  808. */
  809. shmem_page_offset = offset_in_page(offset);
  810. page_length = remain;
  811. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  812. page_length = PAGE_SIZE - shmem_page_offset;
  813. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  814. (page_to_phys(page) & (1 << 17)) != 0;
  815. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  816. user_data, page_do_bit17_swizzling,
  817. needs_clflush);
  818. if (ret == 0)
  819. goto next_page;
  820. mutex_unlock(&dev->struct_mutex);
  821. if (likely(!i915.prefault_disable) && !prefaulted) {
  822. ret = fault_in_multipages_writeable(user_data, remain);
  823. /* Userspace is tricking us, but we've already clobbered
  824. * its pages with the prefault and promised to write the
  825. * data up to the first fault. Hence ignore any errors
  826. * and just continue. */
  827. (void)ret;
  828. prefaulted = 1;
  829. }
  830. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  831. user_data, page_do_bit17_swizzling,
  832. needs_clflush);
  833. mutex_lock(&dev->struct_mutex);
  834. if (ret)
  835. goto out;
  836. next_page:
  837. remain -= page_length;
  838. user_data += page_length;
  839. offset += page_length;
  840. }
  841. out:
  842. i915_gem_obj_finish_shmem_access(obj);
  843. return ret;
  844. }
  845. /**
  846. * Reads data from the object referenced by handle.
  847. * @dev: drm device pointer
  848. * @data: ioctl data blob
  849. * @file: drm file pointer
  850. *
  851. * On error, the contents of *data are undefined.
  852. */
  853. int
  854. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  855. struct drm_file *file)
  856. {
  857. struct drm_i915_gem_pread *args = data;
  858. struct drm_i915_gem_object *obj;
  859. int ret = 0;
  860. if (args->size == 0)
  861. return 0;
  862. if (!access_ok(VERIFY_WRITE,
  863. u64_to_user_ptr(args->data_ptr),
  864. args->size))
  865. return -EFAULT;
  866. obj = i915_gem_object_lookup(file, args->handle);
  867. if (!obj)
  868. return -ENOENT;
  869. /* Bounds check source. */
  870. if (args->offset > obj->base.size ||
  871. args->size > obj->base.size - args->offset) {
  872. ret = -EINVAL;
  873. goto err;
  874. }
  875. trace_i915_gem_object_pread(obj, args->offset, args->size);
  876. ret = __unsafe_wait_rendering(obj, to_rps_client(file), true);
  877. if (ret)
  878. goto err;
  879. ret = i915_mutex_lock_interruptible(dev);
  880. if (ret)
  881. goto err;
  882. ret = i915_gem_shmem_pread(dev, obj, args, file);
  883. /* pread for non shmem backed objects */
  884. if (ret == -EFAULT || ret == -ENODEV) {
  885. intel_runtime_pm_get(to_i915(dev));
  886. ret = i915_gem_gtt_pread(dev, obj, args->size,
  887. args->offset, args->data_ptr);
  888. intel_runtime_pm_put(to_i915(dev));
  889. }
  890. i915_gem_object_put(obj);
  891. mutex_unlock(&dev->struct_mutex);
  892. return ret;
  893. err:
  894. i915_gem_object_put_unlocked(obj);
  895. return ret;
  896. }
  897. /* This is the fast write path which cannot handle
  898. * page faults in the source data
  899. */
  900. static inline int
  901. fast_user_write(struct io_mapping *mapping,
  902. loff_t page_base, int page_offset,
  903. char __user *user_data,
  904. int length)
  905. {
  906. void __iomem *vaddr_atomic;
  907. void *vaddr;
  908. unsigned long unwritten;
  909. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  910. /* We can use the cpu mem copy function because this is X86. */
  911. vaddr = (void __force*)vaddr_atomic + page_offset;
  912. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  913. user_data, length);
  914. io_mapping_unmap_atomic(vaddr_atomic);
  915. return unwritten;
  916. }
  917. /**
  918. * This is the fast pwrite path, where we copy the data directly from the
  919. * user into the GTT, uncached.
  920. * @i915: i915 device private data
  921. * @obj: i915 gem object
  922. * @args: pwrite arguments structure
  923. * @file: drm file pointer
  924. */
  925. static int
  926. i915_gem_gtt_pwrite_fast(struct drm_i915_private *i915,
  927. struct drm_i915_gem_object *obj,
  928. struct drm_i915_gem_pwrite *args,
  929. struct drm_file *file)
  930. {
  931. struct i915_ggtt *ggtt = &i915->ggtt;
  932. struct drm_device *dev = obj->base.dev;
  933. struct i915_vma *vma;
  934. struct drm_mm_node node;
  935. uint64_t remain, offset;
  936. char __user *user_data;
  937. int ret;
  938. bool hit_slow_path = false;
  939. if (i915_gem_object_is_tiled(obj))
  940. return -EFAULT;
  941. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  942. PIN_MAPPABLE | PIN_NONBLOCK);
  943. if (!IS_ERR(vma)) {
  944. node.start = i915_ggtt_offset(vma);
  945. node.allocated = false;
  946. ret = i915_vma_put_fence(vma);
  947. if (ret) {
  948. i915_vma_unpin(vma);
  949. vma = ERR_PTR(ret);
  950. }
  951. }
  952. if (IS_ERR(vma)) {
  953. ret = insert_mappable_node(i915, &node, PAGE_SIZE);
  954. if (ret)
  955. goto out;
  956. ret = i915_gem_object_get_pages(obj);
  957. if (ret) {
  958. remove_mappable_node(&node);
  959. goto out;
  960. }
  961. i915_gem_object_pin_pages(obj);
  962. }
  963. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  964. if (ret)
  965. goto out_unpin;
  966. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  967. obj->dirty = true;
  968. user_data = u64_to_user_ptr(args->data_ptr);
  969. offset = args->offset;
  970. remain = args->size;
  971. while (remain) {
  972. /* Operation in this page
  973. *
  974. * page_base = page offset within aperture
  975. * page_offset = offset within page
  976. * page_length = bytes to copy for this page
  977. */
  978. u32 page_base = node.start;
  979. unsigned page_offset = offset_in_page(offset);
  980. unsigned page_length = PAGE_SIZE - page_offset;
  981. page_length = remain < page_length ? remain : page_length;
  982. if (node.allocated) {
  983. wmb(); /* flush the write before we modify the GGTT */
  984. ggtt->base.insert_page(&ggtt->base,
  985. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  986. node.start, I915_CACHE_NONE, 0);
  987. wmb(); /* flush modifications to the GGTT (insert_page) */
  988. } else {
  989. page_base += offset & PAGE_MASK;
  990. }
  991. /* If we get a fault while copying data, then (presumably) our
  992. * source page isn't available. Return the error and we'll
  993. * retry in the slow path.
  994. * If the object is non-shmem backed, we retry again with the
  995. * path that handles page fault.
  996. */
  997. if (fast_user_write(&ggtt->mappable, page_base,
  998. page_offset, user_data, page_length)) {
  999. hit_slow_path = true;
  1000. mutex_unlock(&dev->struct_mutex);
  1001. if (slow_user_access(&ggtt->mappable,
  1002. page_base,
  1003. page_offset, user_data,
  1004. page_length, true)) {
  1005. ret = -EFAULT;
  1006. mutex_lock(&dev->struct_mutex);
  1007. goto out_flush;
  1008. }
  1009. mutex_lock(&dev->struct_mutex);
  1010. }
  1011. remain -= page_length;
  1012. user_data += page_length;
  1013. offset += page_length;
  1014. }
  1015. out_flush:
  1016. if (hit_slow_path) {
  1017. if (ret == 0 &&
  1018. (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) {
  1019. /* The user has modified the object whilst we tried
  1020. * reading from it, and we now have no idea what domain
  1021. * the pages should be in. As we have just been touching
  1022. * them directly, flush everything back to the GTT
  1023. * domain.
  1024. */
  1025. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  1026. }
  1027. }
  1028. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  1029. out_unpin:
  1030. if (node.allocated) {
  1031. wmb();
  1032. ggtt->base.clear_range(&ggtt->base,
  1033. node.start, node.size,
  1034. true);
  1035. i915_gem_object_unpin_pages(obj);
  1036. remove_mappable_node(&node);
  1037. } else {
  1038. i915_vma_unpin(vma);
  1039. }
  1040. out:
  1041. return ret;
  1042. }
  1043. /* Per-page copy function for the shmem pwrite fastpath.
  1044. * Flushes invalid cachelines before writing to the target if
  1045. * needs_clflush_before is set and flushes out any written cachelines after
  1046. * writing if needs_clflush is set. */
  1047. static int
  1048. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  1049. char __user *user_data,
  1050. bool page_do_bit17_swizzling,
  1051. bool needs_clflush_before,
  1052. bool needs_clflush_after)
  1053. {
  1054. char *vaddr;
  1055. int ret;
  1056. if (unlikely(page_do_bit17_swizzling))
  1057. return -EINVAL;
  1058. vaddr = kmap_atomic(page);
  1059. if (needs_clflush_before)
  1060. drm_clflush_virt_range(vaddr + shmem_page_offset,
  1061. page_length);
  1062. ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
  1063. user_data, page_length);
  1064. if (needs_clflush_after)
  1065. drm_clflush_virt_range(vaddr + shmem_page_offset,
  1066. page_length);
  1067. kunmap_atomic(vaddr);
  1068. return ret ? -EFAULT : 0;
  1069. }
  1070. /* Only difference to the fast-path function is that this can handle bit17
  1071. * and uses non-atomic copy and kmap functions. */
  1072. static int
  1073. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  1074. char __user *user_data,
  1075. bool page_do_bit17_swizzling,
  1076. bool needs_clflush_before,
  1077. bool needs_clflush_after)
  1078. {
  1079. char *vaddr;
  1080. int ret;
  1081. vaddr = kmap(page);
  1082. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  1083. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  1084. page_length,
  1085. page_do_bit17_swizzling);
  1086. if (page_do_bit17_swizzling)
  1087. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  1088. user_data,
  1089. page_length);
  1090. else
  1091. ret = __copy_from_user(vaddr + shmem_page_offset,
  1092. user_data,
  1093. page_length);
  1094. if (needs_clflush_after)
  1095. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  1096. page_length,
  1097. page_do_bit17_swizzling);
  1098. kunmap(page);
  1099. return ret ? -EFAULT : 0;
  1100. }
  1101. static int
  1102. i915_gem_shmem_pwrite(struct drm_device *dev,
  1103. struct drm_i915_gem_object *obj,
  1104. struct drm_i915_gem_pwrite *args,
  1105. struct drm_file *file)
  1106. {
  1107. ssize_t remain;
  1108. loff_t offset;
  1109. char __user *user_data;
  1110. int shmem_page_offset, page_length, ret = 0;
  1111. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  1112. int hit_slowpath = 0;
  1113. unsigned int needs_clflush;
  1114. struct sg_page_iter sg_iter;
  1115. ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
  1116. if (ret)
  1117. return ret;
  1118. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  1119. user_data = u64_to_user_ptr(args->data_ptr);
  1120. offset = args->offset;
  1121. remain = args->size;
  1122. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  1123. offset >> PAGE_SHIFT) {
  1124. struct page *page = sg_page_iter_page(&sg_iter);
  1125. int partial_cacheline_write;
  1126. if (remain <= 0)
  1127. break;
  1128. /* Operation in this page
  1129. *
  1130. * shmem_page_offset = offset within page in shmem file
  1131. * page_length = bytes to copy for this page
  1132. */
  1133. shmem_page_offset = offset_in_page(offset);
  1134. page_length = remain;
  1135. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  1136. page_length = PAGE_SIZE - shmem_page_offset;
  1137. /* If we don't overwrite a cacheline completely we need to be
  1138. * careful to have up-to-date data by first clflushing. Don't
  1139. * overcomplicate things and flush the entire patch. */
  1140. partial_cacheline_write = needs_clflush & CLFLUSH_BEFORE &&
  1141. ((shmem_page_offset | page_length)
  1142. & (boot_cpu_data.x86_clflush_size - 1));
  1143. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  1144. (page_to_phys(page) & (1 << 17)) != 0;
  1145. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  1146. user_data, page_do_bit17_swizzling,
  1147. partial_cacheline_write,
  1148. needs_clflush & CLFLUSH_AFTER);
  1149. if (ret == 0)
  1150. goto next_page;
  1151. hit_slowpath = 1;
  1152. mutex_unlock(&dev->struct_mutex);
  1153. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  1154. user_data, page_do_bit17_swizzling,
  1155. partial_cacheline_write,
  1156. needs_clflush & CLFLUSH_AFTER);
  1157. mutex_lock(&dev->struct_mutex);
  1158. if (ret)
  1159. goto out;
  1160. next_page:
  1161. remain -= page_length;
  1162. user_data += page_length;
  1163. offset += page_length;
  1164. }
  1165. out:
  1166. i915_gem_obj_finish_shmem_access(obj);
  1167. if (hit_slowpath) {
  1168. /*
  1169. * Fixup: Flush cpu caches in case we didn't flush the dirty
  1170. * cachelines in-line while writing and the object moved
  1171. * out of the cpu write domain while we've dropped the lock.
  1172. */
  1173. if (!(needs_clflush & CLFLUSH_AFTER) &&
  1174. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  1175. if (i915_gem_clflush_object(obj, obj->pin_display))
  1176. needs_clflush |= CLFLUSH_AFTER;
  1177. }
  1178. }
  1179. if (needs_clflush & CLFLUSH_AFTER)
  1180. i915_gem_chipset_flush(to_i915(dev));
  1181. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  1182. return ret;
  1183. }
  1184. /**
  1185. * Writes data to the object referenced by handle.
  1186. * @dev: drm device
  1187. * @data: ioctl data blob
  1188. * @file: drm file
  1189. *
  1190. * On error, the contents of the buffer that were to be modified are undefined.
  1191. */
  1192. int
  1193. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  1194. struct drm_file *file)
  1195. {
  1196. struct drm_i915_private *dev_priv = to_i915(dev);
  1197. struct drm_i915_gem_pwrite *args = data;
  1198. struct drm_i915_gem_object *obj;
  1199. int ret;
  1200. if (args->size == 0)
  1201. return 0;
  1202. if (!access_ok(VERIFY_READ,
  1203. u64_to_user_ptr(args->data_ptr),
  1204. args->size))
  1205. return -EFAULT;
  1206. if (likely(!i915.prefault_disable)) {
  1207. ret = fault_in_multipages_readable(u64_to_user_ptr(args->data_ptr),
  1208. args->size);
  1209. if (ret)
  1210. return -EFAULT;
  1211. }
  1212. obj = i915_gem_object_lookup(file, args->handle);
  1213. if (!obj)
  1214. return -ENOENT;
  1215. /* Bounds check destination. */
  1216. if (args->offset > obj->base.size ||
  1217. args->size > obj->base.size - args->offset) {
  1218. ret = -EINVAL;
  1219. goto err;
  1220. }
  1221. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  1222. ret = __unsafe_wait_rendering(obj, to_rps_client(file), false);
  1223. if (ret)
  1224. goto err;
  1225. intel_runtime_pm_get(dev_priv);
  1226. ret = i915_mutex_lock_interruptible(dev);
  1227. if (ret)
  1228. goto err_rpm;
  1229. ret = -EFAULT;
  1230. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  1231. * it would end up going through the fenced access, and we'll get
  1232. * different detiling behavior between reading and writing.
  1233. * pread/pwrite currently are reading and writing from the CPU
  1234. * perspective, requiring manual detiling by the client.
  1235. */
  1236. if (!i915_gem_object_has_struct_page(obj) ||
  1237. cpu_write_needs_clflush(obj)) {
  1238. ret = i915_gem_gtt_pwrite_fast(dev_priv, obj, args, file);
  1239. /* Note that the gtt paths might fail with non-page-backed user
  1240. * pointers (e.g. gtt mappings when moving data between
  1241. * textures). Fallback to the shmem path in that case. */
  1242. }
  1243. if (ret == -EFAULT || ret == -ENOSPC) {
  1244. if (obj->phys_handle)
  1245. ret = i915_gem_phys_pwrite(obj, args, file);
  1246. else
  1247. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  1248. }
  1249. i915_gem_object_put(obj);
  1250. mutex_unlock(&dev->struct_mutex);
  1251. intel_runtime_pm_put(dev_priv);
  1252. return ret;
  1253. err_rpm:
  1254. intel_runtime_pm_put(dev_priv);
  1255. err:
  1256. i915_gem_object_put_unlocked(obj);
  1257. return ret;
  1258. }
  1259. static inline enum fb_op_origin
  1260. write_origin(struct drm_i915_gem_object *obj, unsigned domain)
  1261. {
  1262. return (domain == I915_GEM_DOMAIN_GTT ?
  1263. obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
  1264. }
  1265. /**
  1266. * Called when user space prepares to use an object with the CPU, either
  1267. * through the mmap ioctl's mapping or a GTT mapping.
  1268. * @dev: drm device
  1269. * @data: ioctl data blob
  1270. * @file: drm file
  1271. */
  1272. int
  1273. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1274. struct drm_file *file)
  1275. {
  1276. struct drm_i915_gem_set_domain *args = data;
  1277. struct drm_i915_gem_object *obj;
  1278. uint32_t read_domains = args->read_domains;
  1279. uint32_t write_domain = args->write_domain;
  1280. int ret;
  1281. /* Only handle setting domains to types used by the CPU. */
  1282. if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
  1283. return -EINVAL;
  1284. /* Having something in the write domain implies it's in the read
  1285. * domain, and only that read domain. Enforce that in the request.
  1286. */
  1287. if (write_domain != 0 && read_domains != write_domain)
  1288. return -EINVAL;
  1289. obj = i915_gem_object_lookup(file, args->handle);
  1290. if (!obj)
  1291. return -ENOENT;
  1292. /* Try to flush the object off the GPU without holding the lock.
  1293. * We will repeat the flush holding the lock in the normal manner
  1294. * to catch cases where we are gazumped.
  1295. */
  1296. ret = __unsafe_wait_rendering(obj, to_rps_client(file), !write_domain);
  1297. if (ret)
  1298. goto err;
  1299. ret = i915_mutex_lock_interruptible(dev);
  1300. if (ret)
  1301. goto err;
  1302. if (read_domains & I915_GEM_DOMAIN_GTT)
  1303. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1304. else
  1305. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1306. if (write_domain != 0)
  1307. intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
  1308. i915_gem_object_put(obj);
  1309. mutex_unlock(&dev->struct_mutex);
  1310. return ret;
  1311. err:
  1312. i915_gem_object_put_unlocked(obj);
  1313. return ret;
  1314. }
  1315. /**
  1316. * Called when user space has done writes to this buffer
  1317. * @dev: drm device
  1318. * @data: ioctl data blob
  1319. * @file: drm file
  1320. */
  1321. int
  1322. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1323. struct drm_file *file)
  1324. {
  1325. struct drm_i915_gem_sw_finish *args = data;
  1326. struct drm_i915_gem_object *obj;
  1327. int err = 0;
  1328. obj = i915_gem_object_lookup(file, args->handle);
  1329. if (!obj)
  1330. return -ENOENT;
  1331. /* Pinned buffers may be scanout, so flush the cache */
  1332. if (READ_ONCE(obj->pin_display)) {
  1333. err = i915_mutex_lock_interruptible(dev);
  1334. if (!err) {
  1335. i915_gem_object_flush_cpu_write_domain(obj);
  1336. mutex_unlock(&dev->struct_mutex);
  1337. }
  1338. }
  1339. i915_gem_object_put_unlocked(obj);
  1340. return err;
  1341. }
  1342. /**
  1343. * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
  1344. * it is mapped to.
  1345. * @dev: drm device
  1346. * @data: ioctl data blob
  1347. * @file: drm file
  1348. *
  1349. * While the mapping holds a reference on the contents of the object, it doesn't
  1350. * imply a ref on the object itself.
  1351. *
  1352. * IMPORTANT:
  1353. *
  1354. * DRM driver writers who look a this function as an example for how to do GEM
  1355. * mmap support, please don't implement mmap support like here. The modern way
  1356. * to implement DRM mmap support is with an mmap offset ioctl (like
  1357. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1358. * That way debug tooling like valgrind will understand what's going on, hiding
  1359. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1360. * does cpu mmaps this way because we didn't know better.
  1361. */
  1362. int
  1363. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1364. struct drm_file *file)
  1365. {
  1366. struct drm_i915_gem_mmap *args = data;
  1367. struct drm_i915_gem_object *obj;
  1368. unsigned long addr;
  1369. if (args->flags & ~(I915_MMAP_WC))
  1370. return -EINVAL;
  1371. if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
  1372. return -ENODEV;
  1373. obj = i915_gem_object_lookup(file, args->handle);
  1374. if (!obj)
  1375. return -ENOENT;
  1376. /* prime objects have no backing filp to GEM mmap
  1377. * pages from.
  1378. */
  1379. if (!obj->base.filp) {
  1380. i915_gem_object_put_unlocked(obj);
  1381. return -EINVAL;
  1382. }
  1383. addr = vm_mmap(obj->base.filp, 0, args->size,
  1384. PROT_READ | PROT_WRITE, MAP_SHARED,
  1385. args->offset);
  1386. if (args->flags & I915_MMAP_WC) {
  1387. struct mm_struct *mm = current->mm;
  1388. struct vm_area_struct *vma;
  1389. if (down_write_killable(&mm->mmap_sem)) {
  1390. i915_gem_object_put_unlocked(obj);
  1391. return -EINTR;
  1392. }
  1393. vma = find_vma(mm, addr);
  1394. if (vma)
  1395. vma->vm_page_prot =
  1396. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1397. else
  1398. addr = -ENOMEM;
  1399. up_write(&mm->mmap_sem);
  1400. /* This may race, but that's ok, it only gets set */
  1401. WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
  1402. }
  1403. i915_gem_object_put_unlocked(obj);
  1404. if (IS_ERR((void *)addr))
  1405. return addr;
  1406. args->addr_ptr = (uint64_t) addr;
  1407. return 0;
  1408. }
  1409. static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
  1410. {
  1411. u64 size;
  1412. size = i915_gem_object_get_stride(obj);
  1413. size *= i915_gem_object_get_tiling(obj) == I915_TILING_Y ? 32 : 8;
  1414. return size >> PAGE_SHIFT;
  1415. }
  1416. /**
  1417. * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
  1418. *
  1419. * A history of the GTT mmap interface:
  1420. *
  1421. * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
  1422. * aligned and suitable for fencing, and still fit into the available
  1423. * mappable space left by the pinned display objects. A classic problem
  1424. * we called the page-fault-of-doom where we would ping-pong between
  1425. * two objects that could not fit inside the GTT and so the memcpy
  1426. * would page one object in at the expense of the other between every
  1427. * single byte.
  1428. *
  1429. * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
  1430. * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
  1431. * object is too large for the available space (or simply too large
  1432. * for the mappable aperture!), a view is created instead and faulted
  1433. * into userspace. (This view is aligned and sized appropriately for
  1434. * fenced access.)
  1435. *
  1436. * Restrictions:
  1437. *
  1438. * * snoopable objects cannot be accessed via the GTT. It can cause machine
  1439. * hangs on some architectures, corruption on others. An attempt to service
  1440. * a GTT page fault from a snoopable object will generate a SIGBUS.
  1441. *
  1442. * * the object must be able to fit into RAM (physical memory, though no
  1443. * limited to the mappable aperture).
  1444. *
  1445. *
  1446. * Caveats:
  1447. *
  1448. * * a new GTT page fault will synchronize rendering from the GPU and flush
  1449. * all data to system memory. Subsequent access will not be synchronized.
  1450. *
  1451. * * all mappings are revoked on runtime device suspend.
  1452. *
  1453. * * there are only 8, 16 or 32 fence registers to share between all users
  1454. * (older machines require fence register for display and blitter access
  1455. * as well). Contention of the fence registers will cause the previous users
  1456. * to be unmapped and any new access will generate new page faults.
  1457. *
  1458. * * running out of memory while servicing a fault may generate a SIGBUS,
  1459. * rather than the expected SIGSEGV.
  1460. */
  1461. int i915_gem_mmap_gtt_version(void)
  1462. {
  1463. return 1;
  1464. }
  1465. /**
  1466. * i915_gem_fault - fault a page into the GTT
  1467. * @area: CPU VMA in question
  1468. * @vmf: fault info
  1469. *
  1470. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1471. * from userspace. The fault handler takes care of binding the object to
  1472. * the GTT (if needed), allocating and programming a fence register (again,
  1473. * only if needed based on whether the old reg is still valid or the object
  1474. * is tiled) and inserting a new PTE into the faulting process.
  1475. *
  1476. * Note that the faulting process may involve evicting existing objects
  1477. * from the GTT and/or fence registers to make room. So performance may
  1478. * suffer if the GTT working set is large or there are few fence registers
  1479. * left.
  1480. *
  1481. * The current feature set supported by i915_gem_fault() and thus GTT mmaps
  1482. * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
  1483. */
  1484. int i915_gem_fault(struct vm_area_struct *area, struct vm_fault *vmf)
  1485. {
  1486. #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
  1487. struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
  1488. struct drm_device *dev = obj->base.dev;
  1489. struct drm_i915_private *dev_priv = to_i915(dev);
  1490. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1491. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1492. struct i915_vma *vma;
  1493. pgoff_t page_offset;
  1494. unsigned int flags;
  1495. int ret;
  1496. /* We don't use vmf->pgoff since that has the fake offset */
  1497. page_offset = ((unsigned long)vmf->virtual_address - area->vm_start) >>
  1498. PAGE_SHIFT;
  1499. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1500. /* Try to flush the object off the GPU first without holding the lock.
  1501. * Upon acquiring the lock, we will perform our sanity checks and then
  1502. * repeat the flush holding the lock in the normal manner to catch cases
  1503. * where we are gazumped.
  1504. */
  1505. ret = __unsafe_wait_rendering(obj, NULL, !write);
  1506. if (ret)
  1507. goto err;
  1508. intel_runtime_pm_get(dev_priv);
  1509. ret = i915_mutex_lock_interruptible(dev);
  1510. if (ret)
  1511. goto err_rpm;
  1512. /* Access to snoopable pages through the GTT is incoherent. */
  1513. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1514. ret = -EFAULT;
  1515. goto err_unlock;
  1516. }
  1517. /* If the object is smaller than a couple of partial vma, it is
  1518. * not worth only creating a single partial vma - we may as well
  1519. * clear enough space for the full object.
  1520. */
  1521. flags = PIN_MAPPABLE;
  1522. if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
  1523. flags |= PIN_NONBLOCK | PIN_NONFAULT;
  1524. /* Now pin it into the GTT as needed */
  1525. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
  1526. if (IS_ERR(vma)) {
  1527. struct i915_ggtt_view view;
  1528. unsigned int chunk_size;
  1529. /* Use a partial view if it is bigger than available space */
  1530. chunk_size = MIN_CHUNK_PAGES;
  1531. if (i915_gem_object_is_tiled(obj))
  1532. chunk_size = max(chunk_size, tile_row_pages(obj));
  1533. memset(&view, 0, sizeof(view));
  1534. view.type = I915_GGTT_VIEW_PARTIAL;
  1535. view.params.partial.offset = rounddown(page_offset, chunk_size);
  1536. view.params.partial.size =
  1537. min_t(unsigned int, chunk_size,
  1538. (area->vm_end - area->vm_start) / PAGE_SIZE -
  1539. view.params.partial.offset);
  1540. /* If the partial covers the entire object, just create a
  1541. * normal VMA.
  1542. */
  1543. if (chunk_size >= obj->base.size >> PAGE_SHIFT)
  1544. view.type = I915_GGTT_VIEW_NORMAL;
  1545. /* Userspace is now writing through an untracked VMA, abandon
  1546. * all hope that the hardware is able to track future writes.
  1547. */
  1548. obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
  1549. vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
  1550. }
  1551. if (IS_ERR(vma)) {
  1552. ret = PTR_ERR(vma);
  1553. goto err_unlock;
  1554. }
  1555. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1556. if (ret)
  1557. goto err_unpin;
  1558. ret = i915_vma_get_fence(vma);
  1559. if (ret)
  1560. goto err_unpin;
  1561. /* Finally, remap it using the new GTT offset */
  1562. ret = remap_io_mapping(area,
  1563. area->vm_start + (vma->ggtt_view.params.partial.offset << PAGE_SHIFT),
  1564. (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
  1565. min_t(u64, vma->size, area->vm_end - area->vm_start),
  1566. &ggtt->mappable);
  1567. if (ret)
  1568. goto err_unpin;
  1569. obj->fault_mappable = true;
  1570. err_unpin:
  1571. __i915_vma_unpin(vma);
  1572. err_unlock:
  1573. mutex_unlock(&dev->struct_mutex);
  1574. err_rpm:
  1575. intel_runtime_pm_put(dev_priv);
  1576. err:
  1577. switch (ret) {
  1578. case -EIO:
  1579. /*
  1580. * We eat errors when the gpu is terminally wedged to avoid
  1581. * userspace unduly crashing (gl has no provisions for mmaps to
  1582. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1583. * and so needs to be reported.
  1584. */
  1585. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1586. ret = VM_FAULT_SIGBUS;
  1587. break;
  1588. }
  1589. case -EAGAIN:
  1590. /*
  1591. * EAGAIN means the gpu is hung and we'll wait for the error
  1592. * handler to reset everything when re-faulting in
  1593. * i915_mutex_lock_interruptible.
  1594. */
  1595. case 0:
  1596. case -ERESTARTSYS:
  1597. case -EINTR:
  1598. case -EBUSY:
  1599. /*
  1600. * EBUSY is ok: this just means that another thread
  1601. * already did the job.
  1602. */
  1603. ret = VM_FAULT_NOPAGE;
  1604. break;
  1605. case -ENOMEM:
  1606. ret = VM_FAULT_OOM;
  1607. break;
  1608. case -ENOSPC:
  1609. case -EFAULT:
  1610. ret = VM_FAULT_SIGBUS;
  1611. break;
  1612. default:
  1613. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1614. ret = VM_FAULT_SIGBUS;
  1615. break;
  1616. }
  1617. return ret;
  1618. }
  1619. /**
  1620. * i915_gem_release_mmap - remove physical page mappings
  1621. * @obj: obj in question
  1622. *
  1623. * Preserve the reservation of the mmapping with the DRM core code, but
  1624. * relinquish ownership of the pages back to the system.
  1625. *
  1626. * It is vital that we remove the page mapping if we have mapped a tiled
  1627. * object through the GTT and then lose the fence register due to
  1628. * resource pressure. Similarly if the object has been moved out of the
  1629. * aperture, than pages mapped into userspace must be revoked. Removing the
  1630. * mapping will then trigger a page fault on the next user access, allowing
  1631. * fixup by i915_gem_fault().
  1632. */
  1633. void
  1634. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1635. {
  1636. /* Serialisation between user GTT access and our code depends upon
  1637. * revoking the CPU's PTE whilst the mutex is held. The next user
  1638. * pagefault then has to wait until we release the mutex.
  1639. */
  1640. lockdep_assert_held(&obj->base.dev->struct_mutex);
  1641. if (!obj->fault_mappable)
  1642. return;
  1643. drm_vma_node_unmap(&obj->base.vma_node,
  1644. obj->base.dev->anon_inode->i_mapping);
  1645. /* Ensure that the CPU's PTE are revoked and there are not outstanding
  1646. * memory transactions from userspace before we return. The TLB
  1647. * flushing implied above by changing the PTE above *should* be
  1648. * sufficient, an extra barrier here just provides us with a bit
  1649. * of paranoid documentation about our requirement to serialise
  1650. * memory writes before touching registers / GSM.
  1651. */
  1652. wmb();
  1653. obj->fault_mappable = false;
  1654. }
  1655. void
  1656. i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
  1657. {
  1658. struct drm_i915_gem_object *obj;
  1659. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  1660. i915_gem_release_mmap(obj);
  1661. }
  1662. /**
  1663. * i915_gem_get_ggtt_size - return required global GTT size for an object
  1664. * @dev_priv: i915 device
  1665. * @size: object size
  1666. * @tiling_mode: tiling mode
  1667. *
  1668. * Return the required global GTT size for an object, taking into account
  1669. * potential fence register mapping.
  1670. */
  1671. u64 i915_gem_get_ggtt_size(struct drm_i915_private *dev_priv,
  1672. u64 size, int tiling_mode)
  1673. {
  1674. u64 ggtt_size;
  1675. GEM_BUG_ON(size == 0);
  1676. if (INTEL_GEN(dev_priv) >= 4 ||
  1677. tiling_mode == I915_TILING_NONE)
  1678. return size;
  1679. /* Previous chips need a power-of-two fence region when tiling */
  1680. if (IS_GEN3(dev_priv))
  1681. ggtt_size = 1024*1024;
  1682. else
  1683. ggtt_size = 512*1024;
  1684. while (ggtt_size < size)
  1685. ggtt_size <<= 1;
  1686. return ggtt_size;
  1687. }
  1688. /**
  1689. * i915_gem_get_ggtt_alignment - return required global GTT alignment
  1690. * @dev_priv: i915 device
  1691. * @size: object size
  1692. * @tiling_mode: tiling mode
  1693. * @fenced: is fenced alignment required or not
  1694. *
  1695. * Return the required global GTT alignment for an object, taking into account
  1696. * potential fence register mapping.
  1697. */
  1698. u64 i915_gem_get_ggtt_alignment(struct drm_i915_private *dev_priv, u64 size,
  1699. int tiling_mode, bool fenced)
  1700. {
  1701. GEM_BUG_ON(size == 0);
  1702. /*
  1703. * Minimum alignment is 4k (GTT page size), but might be greater
  1704. * if a fence register is needed for the object.
  1705. */
  1706. if (INTEL_GEN(dev_priv) >= 4 || (!fenced && IS_G33(dev_priv)) ||
  1707. tiling_mode == I915_TILING_NONE)
  1708. return 4096;
  1709. /*
  1710. * Previous chips need to be aligned to the size of the smallest
  1711. * fence register that can contain the object.
  1712. */
  1713. return i915_gem_get_ggtt_size(dev_priv, size, tiling_mode);
  1714. }
  1715. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1716. {
  1717. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1718. int err;
  1719. err = drm_gem_create_mmap_offset(&obj->base);
  1720. if (!err)
  1721. return 0;
  1722. /* We can idle the GPU locklessly to flush stale objects, but in order
  1723. * to claim that space for ourselves, we need to take the big
  1724. * struct_mutex to free the requests+objects and allocate our slot.
  1725. */
  1726. err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
  1727. if (err)
  1728. return err;
  1729. err = i915_mutex_lock_interruptible(&dev_priv->drm);
  1730. if (!err) {
  1731. i915_gem_retire_requests(dev_priv);
  1732. err = drm_gem_create_mmap_offset(&obj->base);
  1733. mutex_unlock(&dev_priv->drm.struct_mutex);
  1734. }
  1735. return err;
  1736. }
  1737. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1738. {
  1739. drm_gem_free_mmap_offset(&obj->base);
  1740. }
  1741. int
  1742. i915_gem_mmap_gtt(struct drm_file *file,
  1743. struct drm_device *dev,
  1744. uint32_t handle,
  1745. uint64_t *offset)
  1746. {
  1747. struct drm_i915_gem_object *obj;
  1748. int ret;
  1749. obj = i915_gem_object_lookup(file, handle);
  1750. if (!obj)
  1751. return -ENOENT;
  1752. ret = i915_gem_object_create_mmap_offset(obj);
  1753. if (ret == 0)
  1754. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1755. i915_gem_object_put_unlocked(obj);
  1756. return ret;
  1757. }
  1758. /**
  1759. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1760. * @dev: DRM device
  1761. * @data: GTT mapping ioctl data
  1762. * @file: GEM object info
  1763. *
  1764. * Simply returns the fake offset to userspace so it can mmap it.
  1765. * The mmap call will end up in drm_gem_mmap(), which will set things
  1766. * up so we can get faults in the handler above.
  1767. *
  1768. * The fault handler will take care of binding the object into the GTT
  1769. * (since it may have been evicted to make room for something), allocating
  1770. * a fence register, and mapping the appropriate aperture address into
  1771. * userspace.
  1772. */
  1773. int
  1774. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1775. struct drm_file *file)
  1776. {
  1777. struct drm_i915_gem_mmap_gtt *args = data;
  1778. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1779. }
  1780. /* Immediately discard the backing storage */
  1781. static void
  1782. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1783. {
  1784. i915_gem_object_free_mmap_offset(obj);
  1785. if (obj->base.filp == NULL)
  1786. return;
  1787. /* Our goal here is to return as much of the memory as
  1788. * is possible back to the system as we are called from OOM.
  1789. * To do this we must instruct the shmfs to drop all of its
  1790. * backing pages, *now*.
  1791. */
  1792. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1793. obj->madv = __I915_MADV_PURGED;
  1794. }
  1795. /* Try to discard unwanted pages */
  1796. static void
  1797. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1798. {
  1799. struct address_space *mapping;
  1800. switch (obj->madv) {
  1801. case I915_MADV_DONTNEED:
  1802. i915_gem_object_truncate(obj);
  1803. case __I915_MADV_PURGED:
  1804. return;
  1805. }
  1806. if (obj->base.filp == NULL)
  1807. return;
  1808. mapping = obj->base.filp->f_mapping,
  1809. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1810. }
  1811. static void
  1812. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1813. {
  1814. struct sgt_iter sgt_iter;
  1815. struct page *page;
  1816. int ret;
  1817. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1818. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1819. if (WARN_ON(ret)) {
  1820. /* In the event of a disaster, abandon all caches and
  1821. * hope for the best.
  1822. */
  1823. i915_gem_clflush_object(obj, true);
  1824. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1825. }
  1826. i915_gem_gtt_finish_object(obj);
  1827. if (i915_gem_object_needs_bit17_swizzle(obj))
  1828. i915_gem_object_save_bit_17_swizzle(obj);
  1829. if (obj->madv == I915_MADV_DONTNEED)
  1830. obj->dirty = 0;
  1831. for_each_sgt_page(page, sgt_iter, obj->pages) {
  1832. if (obj->dirty)
  1833. set_page_dirty(page);
  1834. if (obj->madv == I915_MADV_WILLNEED)
  1835. mark_page_accessed(page);
  1836. put_page(page);
  1837. }
  1838. obj->dirty = 0;
  1839. sg_free_table(obj->pages);
  1840. kfree(obj->pages);
  1841. }
  1842. int
  1843. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1844. {
  1845. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1846. if (obj->pages == NULL)
  1847. return 0;
  1848. if (obj->pages_pin_count)
  1849. return -EBUSY;
  1850. GEM_BUG_ON(obj->bind_count);
  1851. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1852. * array, hence protect them from being reaped by removing them from gtt
  1853. * lists early. */
  1854. list_del(&obj->global_list);
  1855. if (obj->mapping) {
  1856. void *ptr;
  1857. ptr = ptr_mask_bits(obj->mapping);
  1858. if (is_vmalloc_addr(ptr))
  1859. vunmap(ptr);
  1860. else
  1861. kunmap(kmap_to_page(ptr));
  1862. obj->mapping = NULL;
  1863. }
  1864. ops->put_pages(obj);
  1865. obj->pages = NULL;
  1866. i915_gem_object_invalidate(obj);
  1867. return 0;
  1868. }
  1869. static int
  1870. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1871. {
  1872. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1873. int page_count, i;
  1874. struct address_space *mapping;
  1875. struct sg_table *st;
  1876. struct scatterlist *sg;
  1877. struct sgt_iter sgt_iter;
  1878. struct page *page;
  1879. unsigned long last_pfn = 0; /* suppress gcc warning */
  1880. int ret;
  1881. gfp_t gfp;
  1882. /* Assert that the object is not currently in any GPU domain. As it
  1883. * wasn't in the GTT, there shouldn't be any way it could have been in
  1884. * a GPU cache
  1885. */
  1886. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1887. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1888. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1889. if (st == NULL)
  1890. return -ENOMEM;
  1891. page_count = obj->base.size / PAGE_SIZE;
  1892. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1893. kfree(st);
  1894. return -ENOMEM;
  1895. }
  1896. /* Get the list of pages out of our struct file. They'll be pinned
  1897. * at this point until we release them.
  1898. *
  1899. * Fail silently without starting the shrinker
  1900. */
  1901. mapping = obj->base.filp->f_mapping;
  1902. gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
  1903. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  1904. sg = st->sgl;
  1905. st->nents = 0;
  1906. for (i = 0; i < page_count; i++) {
  1907. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1908. if (IS_ERR(page)) {
  1909. i915_gem_shrink(dev_priv,
  1910. page_count,
  1911. I915_SHRINK_BOUND |
  1912. I915_SHRINK_UNBOUND |
  1913. I915_SHRINK_PURGEABLE);
  1914. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1915. }
  1916. if (IS_ERR(page)) {
  1917. /* We've tried hard to allocate the memory by reaping
  1918. * our own buffer, now let the real VM do its job and
  1919. * go down in flames if truly OOM.
  1920. */
  1921. i915_gem_shrink_all(dev_priv);
  1922. page = shmem_read_mapping_page(mapping, i);
  1923. if (IS_ERR(page)) {
  1924. ret = PTR_ERR(page);
  1925. goto err_pages;
  1926. }
  1927. }
  1928. #ifdef CONFIG_SWIOTLB
  1929. if (swiotlb_nr_tbl()) {
  1930. st->nents++;
  1931. sg_set_page(sg, page, PAGE_SIZE, 0);
  1932. sg = sg_next(sg);
  1933. continue;
  1934. }
  1935. #endif
  1936. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1937. if (i)
  1938. sg = sg_next(sg);
  1939. st->nents++;
  1940. sg_set_page(sg, page, PAGE_SIZE, 0);
  1941. } else {
  1942. sg->length += PAGE_SIZE;
  1943. }
  1944. last_pfn = page_to_pfn(page);
  1945. /* Check that the i965g/gm workaround works. */
  1946. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  1947. }
  1948. #ifdef CONFIG_SWIOTLB
  1949. if (!swiotlb_nr_tbl())
  1950. #endif
  1951. sg_mark_end(sg);
  1952. obj->pages = st;
  1953. ret = i915_gem_gtt_prepare_object(obj);
  1954. if (ret)
  1955. goto err_pages;
  1956. if (i915_gem_object_needs_bit17_swizzle(obj))
  1957. i915_gem_object_do_bit_17_swizzle(obj);
  1958. if (i915_gem_object_is_tiled(obj) &&
  1959. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  1960. i915_gem_object_pin_pages(obj);
  1961. return 0;
  1962. err_pages:
  1963. sg_mark_end(sg);
  1964. for_each_sgt_page(page, sgt_iter, st)
  1965. put_page(page);
  1966. sg_free_table(st);
  1967. kfree(st);
  1968. /* shmemfs first checks if there is enough memory to allocate the page
  1969. * and reports ENOSPC should there be insufficient, along with the usual
  1970. * ENOMEM for a genuine allocation failure.
  1971. *
  1972. * We use ENOSPC in our driver to mean that we have run out of aperture
  1973. * space and so want to translate the error from shmemfs back to our
  1974. * usual understanding of ENOMEM.
  1975. */
  1976. if (ret == -ENOSPC)
  1977. ret = -ENOMEM;
  1978. return ret;
  1979. }
  1980. /* Ensure that the associated pages are gathered from the backing storage
  1981. * and pinned into our object. i915_gem_object_get_pages() may be called
  1982. * multiple times before they are released by a single call to
  1983. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1984. * either as a result of memory pressure (reaping pages under the shrinker)
  1985. * or as the object is itself released.
  1986. */
  1987. int
  1988. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1989. {
  1990. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1991. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1992. int ret;
  1993. if (obj->pages)
  1994. return 0;
  1995. if (obj->madv != I915_MADV_WILLNEED) {
  1996. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  1997. return -EFAULT;
  1998. }
  1999. BUG_ON(obj->pages_pin_count);
  2000. ret = ops->get_pages(obj);
  2001. if (ret)
  2002. return ret;
  2003. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2004. obj->get_page.sg = obj->pages->sgl;
  2005. obj->get_page.last = 0;
  2006. return 0;
  2007. }
  2008. /* The 'mapping' part of i915_gem_object_pin_map() below */
  2009. static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
  2010. enum i915_map_type type)
  2011. {
  2012. unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
  2013. struct sg_table *sgt = obj->pages;
  2014. struct sgt_iter sgt_iter;
  2015. struct page *page;
  2016. struct page *stack_pages[32];
  2017. struct page **pages = stack_pages;
  2018. unsigned long i = 0;
  2019. pgprot_t pgprot;
  2020. void *addr;
  2021. /* A single page can always be kmapped */
  2022. if (n_pages == 1 && type == I915_MAP_WB)
  2023. return kmap(sg_page(sgt->sgl));
  2024. if (n_pages > ARRAY_SIZE(stack_pages)) {
  2025. /* Too big for stack -- allocate temporary array instead */
  2026. pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
  2027. if (!pages)
  2028. return NULL;
  2029. }
  2030. for_each_sgt_page(page, sgt_iter, sgt)
  2031. pages[i++] = page;
  2032. /* Check that we have the expected number of pages */
  2033. GEM_BUG_ON(i != n_pages);
  2034. switch (type) {
  2035. case I915_MAP_WB:
  2036. pgprot = PAGE_KERNEL;
  2037. break;
  2038. case I915_MAP_WC:
  2039. pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
  2040. break;
  2041. }
  2042. addr = vmap(pages, n_pages, 0, pgprot);
  2043. if (pages != stack_pages)
  2044. drm_free_large(pages);
  2045. return addr;
  2046. }
  2047. /* get, pin, and map the pages of the object into kernel space */
  2048. void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
  2049. enum i915_map_type type)
  2050. {
  2051. enum i915_map_type has_type;
  2052. bool pinned;
  2053. void *ptr;
  2054. int ret;
  2055. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2056. GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  2057. ret = i915_gem_object_get_pages(obj);
  2058. if (ret)
  2059. return ERR_PTR(ret);
  2060. i915_gem_object_pin_pages(obj);
  2061. pinned = obj->pages_pin_count > 1;
  2062. ptr = ptr_unpack_bits(obj->mapping, has_type);
  2063. if (ptr && has_type != type) {
  2064. if (pinned) {
  2065. ret = -EBUSY;
  2066. goto err;
  2067. }
  2068. if (is_vmalloc_addr(ptr))
  2069. vunmap(ptr);
  2070. else
  2071. kunmap(kmap_to_page(ptr));
  2072. ptr = obj->mapping = NULL;
  2073. }
  2074. if (!ptr) {
  2075. ptr = i915_gem_object_map(obj, type);
  2076. if (!ptr) {
  2077. ret = -ENOMEM;
  2078. goto err;
  2079. }
  2080. obj->mapping = ptr_pack_bits(ptr, type);
  2081. }
  2082. return ptr;
  2083. err:
  2084. i915_gem_object_unpin_pages(obj);
  2085. return ERR_PTR(ret);
  2086. }
  2087. static void
  2088. i915_gem_object_retire__write(struct i915_gem_active *active,
  2089. struct drm_i915_gem_request *request)
  2090. {
  2091. struct drm_i915_gem_object *obj =
  2092. container_of(active, struct drm_i915_gem_object, last_write);
  2093. intel_fb_obj_flush(obj, true, ORIGIN_CS);
  2094. }
  2095. static void
  2096. i915_gem_object_retire__read(struct i915_gem_active *active,
  2097. struct drm_i915_gem_request *request)
  2098. {
  2099. int idx = request->engine->id;
  2100. struct drm_i915_gem_object *obj =
  2101. container_of(active, struct drm_i915_gem_object, last_read[idx]);
  2102. GEM_BUG_ON(!i915_gem_object_has_active_engine(obj, idx));
  2103. i915_gem_object_clear_active(obj, idx);
  2104. if (i915_gem_object_is_active(obj))
  2105. return;
  2106. /* Bump our place on the bound list to keep it roughly in LRU order
  2107. * so that we don't steal from recently used but inactive objects
  2108. * (unless we are forced to ofc!)
  2109. */
  2110. if (obj->bind_count)
  2111. list_move_tail(&obj->global_list,
  2112. &request->i915->mm.bound_list);
  2113. i915_gem_object_put(obj);
  2114. }
  2115. static bool i915_context_is_banned(const struct i915_gem_context *ctx)
  2116. {
  2117. unsigned long elapsed;
  2118. if (ctx->hang_stats.banned)
  2119. return true;
  2120. elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
  2121. if (ctx->hang_stats.ban_period_seconds &&
  2122. elapsed <= ctx->hang_stats.ban_period_seconds) {
  2123. DRM_DEBUG("context hanging too fast, banning!\n");
  2124. return true;
  2125. }
  2126. return false;
  2127. }
  2128. static void i915_set_reset_status(struct i915_gem_context *ctx,
  2129. const bool guilty)
  2130. {
  2131. struct i915_ctx_hang_stats *hs = &ctx->hang_stats;
  2132. if (guilty) {
  2133. hs->banned = i915_context_is_banned(ctx);
  2134. hs->batch_active++;
  2135. hs->guilty_ts = get_seconds();
  2136. } else {
  2137. hs->batch_pending++;
  2138. }
  2139. }
  2140. struct drm_i915_gem_request *
  2141. i915_gem_find_active_request(struct intel_engine_cs *engine)
  2142. {
  2143. struct drm_i915_gem_request *request;
  2144. /* We are called by the error capture and reset at a random
  2145. * point in time. In particular, note that neither is crucially
  2146. * ordered with an interrupt. After a hang, the GPU is dead and we
  2147. * assume that no more writes can happen (we waited long enough for
  2148. * all writes that were in transaction to be flushed) - adding an
  2149. * extra delay for a recent interrupt is pointless. Hence, we do
  2150. * not need an engine->irq_seqno_barrier() before the seqno reads.
  2151. */
  2152. list_for_each_entry(request, &engine->request_list, link) {
  2153. if (i915_gem_request_completed(request))
  2154. continue;
  2155. if (!i915_sw_fence_done(&request->submit))
  2156. break;
  2157. return request;
  2158. }
  2159. return NULL;
  2160. }
  2161. static void reset_request(struct drm_i915_gem_request *request)
  2162. {
  2163. void *vaddr = request->ring->vaddr;
  2164. u32 head;
  2165. /* As this request likely depends on state from the lost
  2166. * context, clear out all the user operations leaving the
  2167. * breadcrumb at the end (so we get the fence notifications).
  2168. */
  2169. head = request->head;
  2170. if (request->postfix < head) {
  2171. memset(vaddr + head, 0, request->ring->size - head);
  2172. head = 0;
  2173. }
  2174. memset(vaddr + head, 0, request->postfix - head);
  2175. }
  2176. static void i915_gem_reset_engine(struct intel_engine_cs *engine)
  2177. {
  2178. struct drm_i915_gem_request *request;
  2179. struct i915_gem_context *incomplete_ctx;
  2180. bool ring_hung;
  2181. /* Ensure irq handler finishes, and not run again. */
  2182. tasklet_kill(&engine->irq_tasklet);
  2183. if (engine->irq_seqno_barrier)
  2184. engine->irq_seqno_barrier(engine);
  2185. request = i915_gem_find_active_request(engine);
  2186. if (!request)
  2187. return;
  2188. ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2189. i915_set_reset_status(request->ctx, ring_hung);
  2190. if (!ring_hung)
  2191. return;
  2192. DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
  2193. engine->name, request->fence.seqno);
  2194. /* Setup the CS to resume from the breadcrumb of the hung request */
  2195. engine->reset_hw(engine, request);
  2196. /* Users of the default context do not rely on logical state
  2197. * preserved between batches. They have to emit full state on
  2198. * every batch and so it is safe to execute queued requests following
  2199. * the hang.
  2200. *
  2201. * Other contexts preserve state, now corrupt. We want to skip all
  2202. * queued requests that reference the corrupt context.
  2203. */
  2204. incomplete_ctx = request->ctx;
  2205. if (i915_gem_context_is_default(incomplete_ctx))
  2206. return;
  2207. list_for_each_entry_continue(request, &engine->request_list, link)
  2208. if (request->ctx == incomplete_ctx)
  2209. reset_request(request);
  2210. }
  2211. void i915_gem_reset(struct drm_i915_private *dev_priv)
  2212. {
  2213. struct intel_engine_cs *engine;
  2214. i915_gem_retire_requests(dev_priv);
  2215. for_each_engine(engine, dev_priv)
  2216. i915_gem_reset_engine(engine);
  2217. i915_gem_restore_fences(&dev_priv->drm);
  2218. if (dev_priv->gt.awake) {
  2219. intel_sanitize_gt_powersave(dev_priv);
  2220. intel_enable_gt_powersave(dev_priv);
  2221. if (INTEL_GEN(dev_priv) >= 6)
  2222. gen6_rps_busy(dev_priv);
  2223. }
  2224. }
  2225. static void nop_submit_request(struct drm_i915_gem_request *request)
  2226. {
  2227. }
  2228. static void i915_gem_cleanup_engine(struct intel_engine_cs *engine)
  2229. {
  2230. engine->submit_request = nop_submit_request;
  2231. /* Mark all pending requests as complete so that any concurrent
  2232. * (lockless) lookup doesn't try and wait upon the request as we
  2233. * reset it.
  2234. */
  2235. intel_engine_init_seqno(engine, engine->last_submitted_seqno);
  2236. /*
  2237. * Clear the execlists queue up before freeing the requests, as those
  2238. * are the ones that keep the context and ringbuffer backing objects
  2239. * pinned in place.
  2240. */
  2241. if (i915.enable_execlists) {
  2242. spin_lock(&engine->execlist_lock);
  2243. INIT_LIST_HEAD(&engine->execlist_queue);
  2244. i915_gem_request_put(engine->execlist_port[0].request);
  2245. i915_gem_request_put(engine->execlist_port[1].request);
  2246. memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
  2247. spin_unlock(&engine->execlist_lock);
  2248. }
  2249. engine->i915->gt.active_engines &= ~intel_engine_flag(engine);
  2250. }
  2251. void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
  2252. {
  2253. struct intel_engine_cs *engine;
  2254. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2255. set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
  2256. i915_gem_context_lost(dev_priv);
  2257. for_each_engine(engine, dev_priv)
  2258. i915_gem_cleanup_engine(engine);
  2259. mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
  2260. i915_gem_retire_requests(dev_priv);
  2261. }
  2262. static void
  2263. i915_gem_retire_work_handler(struct work_struct *work)
  2264. {
  2265. struct drm_i915_private *dev_priv =
  2266. container_of(work, typeof(*dev_priv), gt.retire_work.work);
  2267. struct drm_device *dev = &dev_priv->drm;
  2268. /* Come back later if the device is busy... */
  2269. if (mutex_trylock(&dev->struct_mutex)) {
  2270. i915_gem_retire_requests(dev_priv);
  2271. mutex_unlock(&dev->struct_mutex);
  2272. }
  2273. /* Keep the retire handler running until we are finally idle.
  2274. * We do not need to do this test under locking as in the worst-case
  2275. * we queue the retire worker once too often.
  2276. */
  2277. if (READ_ONCE(dev_priv->gt.awake)) {
  2278. i915_queue_hangcheck(dev_priv);
  2279. queue_delayed_work(dev_priv->wq,
  2280. &dev_priv->gt.retire_work,
  2281. round_jiffies_up_relative(HZ));
  2282. }
  2283. }
  2284. static void
  2285. i915_gem_idle_work_handler(struct work_struct *work)
  2286. {
  2287. struct drm_i915_private *dev_priv =
  2288. container_of(work, typeof(*dev_priv), gt.idle_work.work);
  2289. struct drm_device *dev = &dev_priv->drm;
  2290. struct intel_engine_cs *engine;
  2291. bool rearm_hangcheck;
  2292. if (!READ_ONCE(dev_priv->gt.awake))
  2293. return;
  2294. if (READ_ONCE(dev_priv->gt.active_engines))
  2295. return;
  2296. rearm_hangcheck =
  2297. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  2298. if (!mutex_trylock(&dev->struct_mutex)) {
  2299. /* Currently busy, come back later */
  2300. mod_delayed_work(dev_priv->wq,
  2301. &dev_priv->gt.idle_work,
  2302. msecs_to_jiffies(50));
  2303. goto out_rearm;
  2304. }
  2305. if (dev_priv->gt.active_engines)
  2306. goto out_unlock;
  2307. for_each_engine(engine, dev_priv)
  2308. i915_gem_batch_pool_fini(&engine->batch_pool);
  2309. GEM_BUG_ON(!dev_priv->gt.awake);
  2310. dev_priv->gt.awake = false;
  2311. rearm_hangcheck = false;
  2312. if (INTEL_GEN(dev_priv) >= 6)
  2313. gen6_rps_idle(dev_priv);
  2314. intel_runtime_pm_put(dev_priv);
  2315. out_unlock:
  2316. mutex_unlock(&dev->struct_mutex);
  2317. out_rearm:
  2318. if (rearm_hangcheck) {
  2319. GEM_BUG_ON(!dev_priv->gt.awake);
  2320. i915_queue_hangcheck(dev_priv);
  2321. }
  2322. }
  2323. void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
  2324. {
  2325. struct drm_i915_gem_object *obj = to_intel_bo(gem);
  2326. struct drm_i915_file_private *fpriv = file->driver_priv;
  2327. struct i915_vma *vma, *vn;
  2328. mutex_lock(&obj->base.dev->struct_mutex);
  2329. list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
  2330. if (vma->vm->file == fpriv)
  2331. i915_vma_close(vma);
  2332. mutex_unlock(&obj->base.dev->struct_mutex);
  2333. }
  2334. /**
  2335. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2336. * @dev: drm device pointer
  2337. * @data: ioctl data blob
  2338. * @file: drm file pointer
  2339. *
  2340. * Returns 0 if successful, else an error is returned with the remaining time in
  2341. * the timeout parameter.
  2342. * -ETIME: object is still busy after timeout
  2343. * -ERESTARTSYS: signal interrupted the wait
  2344. * -ENONENT: object doesn't exist
  2345. * Also possible, but rare:
  2346. * -EAGAIN: GPU wedged
  2347. * -ENOMEM: damn
  2348. * -ENODEV: Internal IRQ fail
  2349. * -E?: The add request failed
  2350. *
  2351. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2352. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2353. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2354. * without holding struct_mutex the object may become re-busied before this
  2355. * function completes. A similar but shorter * race condition exists in the busy
  2356. * ioctl
  2357. */
  2358. int
  2359. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2360. {
  2361. struct drm_i915_gem_wait *args = data;
  2362. struct intel_rps_client *rps = to_rps_client(file);
  2363. struct drm_i915_gem_object *obj;
  2364. unsigned long active;
  2365. int idx, ret = 0;
  2366. if (args->flags != 0)
  2367. return -EINVAL;
  2368. obj = i915_gem_object_lookup(file, args->bo_handle);
  2369. if (!obj)
  2370. return -ENOENT;
  2371. active = __I915_BO_ACTIVE(obj);
  2372. for_each_active(active, idx) {
  2373. s64 *timeout = args->timeout_ns >= 0 ? &args->timeout_ns : NULL;
  2374. ret = i915_gem_active_wait_unlocked(&obj->last_read[idx],
  2375. I915_WAIT_INTERRUPTIBLE,
  2376. timeout, rps);
  2377. if (ret)
  2378. break;
  2379. }
  2380. i915_gem_object_put_unlocked(obj);
  2381. return ret;
  2382. }
  2383. static void __i915_vma_iounmap(struct i915_vma *vma)
  2384. {
  2385. GEM_BUG_ON(i915_vma_is_pinned(vma));
  2386. if (vma->iomap == NULL)
  2387. return;
  2388. io_mapping_unmap(vma->iomap);
  2389. vma->iomap = NULL;
  2390. }
  2391. int i915_vma_unbind(struct i915_vma *vma)
  2392. {
  2393. struct drm_i915_gem_object *obj = vma->obj;
  2394. unsigned long active;
  2395. int ret;
  2396. /* First wait upon any activity as retiring the request may
  2397. * have side-effects such as unpinning or even unbinding this vma.
  2398. */
  2399. active = i915_vma_get_active(vma);
  2400. if (active) {
  2401. int idx;
  2402. /* When a closed VMA is retired, it is unbound - eek.
  2403. * In order to prevent it from being recursively closed,
  2404. * take a pin on the vma so that the second unbind is
  2405. * aborted.
  2406. */
  2407. __i915_vma_pin(vma);
  2408. for_each_active(active, idx) {
  2409. ret = i915_gem_active_retire(&vma->last_read[idx],
  2410. &vma->vm->dev->struct_mutex);
  2411. if (ret)
  2412. break;
  2413. }
  2414. __i915_vma_unpin(vma);
  2415. if (ret)
  2416. return ret;
  2417. GEM_BUG_ON(i915_vma_is_active(vma));
  2418. }
  2419. if (i915_vma_is_pinned(vma))
  2420. return -EBUSY;
  2421. if (!drm_mm_node_allocated(&vma->node))
  2422. goto destroy;
  2423. GEM_BUG_ON(obj->bind_count == 0);
  2424. GEM_BUG_ON(!obj->pages);
  2425. if (i915_vma_is_map_and_fenceable(vma)) {
  2426. /* release the fence reg _after_ flushing */
  2427. ret = i915_vma_put_fence(vma);
  2428. if (ret)
  2429. return ret;
  2430. /* Force a pagefault for domain tracking on next user access */
  2431. i915_gem_release_mmap(obj);
  2432. __i915_vma_iounmap(vma);
  2433. vma->flags &= ~I915_VMA_CAN_FENCE;
  2434. }
  2435. if (likely(!vma->vm->closed)) {
  2436. trace_i915_vma_unbind(vma);
  2437. vma->vm->unbind_vma(vma);
  2438. }
  2439. vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
  2440. drm_mm_remove_node(&vma->node);
  2441. list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
  2442. if (vma->pages != obj->pages) {
  2443. GEM_BUG_ON(!vma->pages);
  2444. sg_free_table(vma->pages);
  2445. kfree(vma->pages);
  2446. }
  2447. vma->pages = NULL;
  2448. /* Since the unbound list is global, only move to that list if
  2449. * no more VMAs exist. */
  2450. if (--obj->bind_count == 0)
  2451. list_move_tail(&obj->global_list,
  2452. &to_i915(obj->base.dev)->mm.unbound_list);
  2453. /* And finally now the object is completely decoupled from this vma,
  2454. * we can drop its hold on the backing storage and allow it to be
  2455. * reaped by the shrinker.
  2456. */
  2457. i915_gem_object_unpin_pages(obj);
  2458. destroy:
  2459. if (unlikely(i915_vma_is_closed(vma)))
  2460. i915_vma_destroy(vma);
  2461. return 0;
  2462. }
  2463. int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv,
  2464. unsigned int flags)
  2465. {
  2466. struct intel_engine_cs *engine;
  2467. int ret;
  2468. for_each_engine(engine, dev_priv) {
  2469. if (engine->last_context == NULL)
  2470. continue;
  2471. ret = intel_engine_idle(engine, flags);
  2472. if (ret)
  2473. return ret;
  2474. }
  2475. return 0;
  2476. }
  2477. static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
  2478. unsigned long cache_level)
  2479. {
  2480. struct drm_mm_node *gtt_space = &vma->node;
  2481. struct drm_mm_node *other;
  2482. /*
  2483. * On some machines we have to be careful when putting differing types
  2484. * of snoopable memory together to avoid the prefetcher crossing memory
  2485. * domains and dying. During vm initialisation, we decide whether or not
  2486. * these constraints apply and set the drm_mm.color_adjust
  2487. * appropriately.
  2488. */
  2489. if (vma->vm->mm.color_adjust == NULL)
  2490. return true;
  2491. if (!drm_mm_node_allocated(gtt_space))
  2492. return true;
  2493. if (list_empty(&gtt_space->node_list))
  2494. return true;
  2495. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2496. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2497. return false;
  2498. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2499. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2500. return false;
  2501. return true;
  2502. }
  2503. /**
  2504. * i915_vma_insert - finds a slot for the vma in its address space
  2505. * @vma: the vma
  2506. * @size: requested size in bytes (can be larger than the VMA)
  2507. * @alignment: required alignment
  2508. * @flags: mask of PIN_* flags to use
  2509. *
  2510. * First we try to allocate some free space that meets the requirements for
  2511. * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
  2512. * preferrably the oldest idle entry to make room for the new VMA.
  2513. *
  2514. * Returns:
  2515. * 0 on success, negative error code otherwise.
  2516. */
  2517. static int
  2518. i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
  2519. {
  2520. struct drm_i915_private *dev_priv = to_i915(vma->vm->dev);
  2521. struct drm_i915_gem_object *obj = vma->obj;
  2522. u64 start, end;
  2523. int ret;
  2524. GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
  2525. GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
  2526. size = max(size, vma->size);
  2527. if (flags & PIN_MAPPABLE)
  2528. size = i915_gem_get_ggtt_size(dev_priv, size,
  2529. i915_gem_object_get_tiling(obj));
  2530. alignment = max(max(alignment, vma->display_alignment),
  2531. i915_gem_get_ggtt_alignment(dev_priv, size,
  2532. i915_gem_object_get_tiling(obj),
  2533. flags & PIN_MAPPABLE));
  2534. start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  2535. end = vma->vm->total;
  2536. if (flags & PIN_MAPPABLE)
  2537. end = min_t(u64, end, dev_priv->ggtt.mappable_end);
  2538. if (flags & PIN_ZONE_4G)
  2539. end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
  2540. /* If binding the object/GGTT view requires more space than the entire
  2541. * aperture has, reject it early before evicting everything in a vain
  2542. * attempt to find space.
  2543. */
  2544. if (size > end) {
  2545. DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n",
  2546. size, obj->base.size,
  2547. flags & PIN_MAPPABLE ? "mappable" : "total",
  2548. end);
  2549. return -E2BIG;
  2550. }
  2551. ret = i915_gem_object_get_pages(obj);
  2552. if (ret)
  2553. return ret;
  2554. i915_gem_object_pin_pages(obj);
  2555. if (flags & PIN_OFFSET_FIXED) {
  2556. u64 offset = flags & PIN_OFFSET_MASK;
  2557. if (offset & (alignment - 1) || offset > end - size) {
  2558. ret = -EINVAL;
  2559. goto err_unpin;
  2560. }
  2561. vma->node.start = offset;
  2562. vma->node.size = size;
  2563. vma->node.color = obj->cache_level;
  2564. ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node);
  2565. if (ret) {
  2566. ret = i915_gem_evict_for_vma(vma);
  2567. if (ret == 0)
  2568. ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node);
  2569. if (ret)
  2570. goto err_unpin;
  2571. }
  2572. } else {
  2573. u32 search_flag, alloc_flag;
  2574. if (flags & PIN_HIGH) {
  2575. search_flag = DRM_MM_SEARCH_BELOW;
  2576. alloc_flag = DRM_MM_CREATE_TOP;
  2577. } else {
  2578. search_flag = DRM_MM_SEARCH_DEFAULT;
  2579. alloc_flag = DRM_MM_CREATE_DEFAULT;
  2580. }
  2581. /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
  2582. * so we know that we always have a minimum alignment of 4096.
  2583. * The drm_mm range manager is optimised to return results
  2584. * with zero alignment, so where possible use the optimal
  2585. * path.
  2586. */
  2587. if (alignment <= 4096)
  2588. alignment = 0;
  2589. search_free:
  2590. ret = drm_mm_insert_node_in_range_generic(&vma->vm->mm,
  2591. &vma->node,
  2592. size, alignment,
  2593. obj->cache_level,
  2594. start, end,
  2595. search_flag,
  2596. alloc_flag);
  2597. if (ret) {
  2598. ret = i915_gem_evict_something(vma->vm, size, alignment,
  2599. obj->cache_level,
  2600. start, end,
  2601. flags);
  2602. if (ret == 0)
  2603. goto search_free;
  2604. goto err_unpin;
  2605. }
  2606. }
  2607. GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level));
  2608. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  2609. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  2610. obj->bind_count++;
  2611. return 0;
  2612. err_unpin:
  2613. i915_gem_object_unpin_pages(obj);
  2614. return ret;
  2615. }
  2616. bool
  2617. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  2618. bool force)
  2619. {
  2620. /* If we don't have a page list set up, then we're not pinned
  2621. * to GPU, and we can ignore the cache flush because it'll happen
  2622. * again at bind time.
  2623. */
  2624. if (obj->pages == NULL)
  2625. return false;
  2626. /*
  2627. * Stolen memory is always coherent with the GPU as it is explicitly
  2628. * marked as wc by the system, or the system is cache-coherent.
  2629. */
  2630. if (obj->stolen || obj->phys_handle)
  2631. return false;
  2632. /* If the GPU is snooping the contents of the CPU cache,
  2633. * we do not need to manually clear the CPU cache lines. However,
  2634. * the caches are only snooped when the render cache is
  2635. * flushed/invalidated. As we always have to emit invalidations
  2636. * and flushes when moving into and out of the RENDER domain, correct
  2637. * snooping behaviour occurs naturally as the result of our domain
  2638. * tracking.
  2639. */
  2640. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
  2641. obj->cache_dirty = true;
  2642. return false;
  2643. }
  2644. trace_i915_gem_object_clflush(obj);
  2645. drm_clflush_sg(obj->pages);
  2646. obj->cache_dirty = false;
  2647. return true;
  2648. }
  2649. /** Flushes the GTT write domain for the object if it's dirty. */
  2650. static void
  2651. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2652. {
  2653. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2654. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2655. return;
  2656. /* No actual flushing is required for the GTT write domain. Writes
  2657. * to it "immediately" go to main memory as far as we know, so there's
  2658. * no chipset flush. It also doesn't land in render cache.
  2659. *
  2660. * However, we do have to enforce the order so that all writes through
  2661. * the GTT land before any writes to the device, such as updates to
  2662. * the GATT itself.
  2663. *
  2664. * We also have to wait a bit for the writes to land from the GTT.
  2665. * An uncached read (i.e. mmio) seems to be ideal for the round-trip
  2666. * timing. This issue has only been observed when switching quickly
  2667. * between GTT writes and CPU reads from inside the kernel on recent hw,
  2668. * and it appears to only affect discrete GTT blocks (i.e. on LLC
  2669. * system agents we cannot reproduce this behaviour).
  2670. */
  2671. wmb();
  2672. if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv))
  2673. POSTING_READ(RING_ACTHD(dev_priv->engine[RCS].mmio_base));
  2674. intel_fb_obj_flush(obj, false, write_origin(obj, I915_GEM_DOMAIN_GTT));
  2675. obj->base.write_domain = 0;
  2676. trace_i915_gem_object_change_domain(obj,
  2677. obj->base.read_domains,
  2678. I915_GEM_DOMAIN_GTT);
  2679. }
  2680. /** Flushes the CPU write domain for the object if it's dirty. */
  2681. static void
  2682. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2683. {
  2684. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2685. return;
  2686. if (i915_gem_clflush_object(obj, obj->pin_display))
  2687. i915_gem_chipset_flush(to_i915(obj->base.dev));
  2688. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  2689. obj->base.write_domain = 0;
  2690. trace_i915_gem_object_change_domain(obj,
  2691. obj->base.read_domains,
  2692. I915_GEM_DOMAIN_CPU);
  2693. }
  2694. static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
  2695. {
  2696. struct i915_vma *vma;
  2697. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2698. if (!i915_vma_is_ggtt(vma))
  2699. continue;
  2700. if (i915_vma_is_active(vma))
  2701. continue;
  2702. if (!drm_mm_node_allocated(&vma->node))
  2703. continue;
  2704. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  2705. }
  2706. }
  2707. /**
  2708. * Moves a single object to the GTT read, and possibly write domain.
  2709. * @obj: object to act on
  2710. * @write: ask for write access or read only
  2711. *
  2712. * This function returns when the move is complete, including waiting on
  2713. * flushes to occur.
  2714. */
  2715. int
  2716. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2717. {
  2718. uint32_t old_write_domain, old_read_domains;
  2719. int ret;
  2720. ret = i915_gem_object_wait_rendering(obj, !write);
  2721. if (ret)
  2722. return ret;
  2723. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2724. return 0;
  2725. /* Flush and acquire obj->pages so that we are coherent through
  2726. * direct access in memory with previous cached writes through
  2727. * shmemfs and that our cache domain tracking remains valid.
  2728. * For example, if the obj->filp was moved to swap without us
  2729. * being notified and releasing the pages, we would mistakenly
  2730. * continue to assume that the obj remained out of the CPU cached
  2731. * domain.
  2732. */
  2733. ret = i915_gem_object_get_pages(obj);
  2734. if (ret)
  2735. return ret;
  2736. i915_gem_object_flush_cpu_write_domain(obj);
  2737. /* Serialise direct access to this object with the barriers for
  2738. * coherent writes from the GPU, by effectively invalidating the
  2739. * GTT domain upon first access.
  2740. */
  2741. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2742. mb();
  2743. old_write_domain = obj->base.write_domain;
  2744. old_read_domains = obj->base.read_domains;
  2745. /* It should now be out of any other write domains, and we can update
  2746. * the domain values for our changes.
  2747. */
  2748. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2749. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2750. if (write) {
  2751. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2752. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2753. obj->dirty = 1;
  2754. }
  2755. trace_i915_gem_object_change_domain(obj,
  2756. old_read_domains,
  2757. old_write_domain);
  2758. /* And bump the LRU for this access */
  2759. i915_gem_object_bump_inactive_ggtt(obj);
  2760. return 0;
  2761. }
  2762. /**
  2763. * Changes the cache-level of an object across all VMA.
  2764. * @obj: object to act on
  2765. * @cache_level: new cache level to set for the object
  2766. *
  2767. * After this function returns, the object will be in the new cache-level
  2768. * across all GTT and the contents of the backing storage will be coherent,
  2769. * with respect to the new cache-level. In order to keep the backing storage
  2770. * coherent for all users, we only allow a single cache level to be set
  2771. * globally on the object and prevent it from being changed whilst the
  2772. * hardware is reading from the object. That is if the object is currently
  2773. * on the scanout it will be set to uncached (or equivalent display
  2774. * cache coherency) and all non-MOCS GPU access will also be uncached so
  2775. * that all direct access to the scanout remains coherent.
  2776. */
  2777. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2778. enum i915_cache_level cache_level)
  2779. {
  2780. struct i915_vma *vma;
  2781. int ret = 0;
  2782. if (obj->cache_level == cache_level)
  2783. goto out;
  2784. /* Inspect the list of currently bound VMA and unbind any that would
  2785. * be invalid given the new cache-level. This is principally to
  2786. * catch the issue of the CS prefetch crossing page boundaries and
  2787. * reading an invalid PTE on older architectures.
  2788. */
  2789. restart:
  2790. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2791. if (!drm_mm_node_allocated(&vma->node))
  2792. continue;
  2793. if (i915_vma_is_pinned(vma)) {
  2794. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2795. return -EBUSY;
  2796. }
  2797. if (i915_gem_valid_gtt_space(vma, cache_level))
  2798. continue;
  2799. ret = i915_vma_unbind(vma);
  2800. if (ret)
  2801. return ret;
  2802. /* As unbinding may affect other elements in the
  2803. * obj->vma_list (due to side-effects from retiring
  2804. * an active vma), play safe and restart the iterator.
  2805. */
  2806. goto restart;
  2807. }
  2808. /* We can reuse the existing drm_mm nodes but need to change the
  2809. * cache-level on the PTE. We could simply unbind them all and
  2810. * rebind with the correct cache-level on next use. However since
  2811. * we already have a valid slot, dma mapping, pages etc, we may as
  2812. * rewrite the PTE in the belief that doing so tramples upon less
  2813. * state and so involves less work.
  2814. */
  2815. if (obj->bind_count) {
  2816. /* Before we change the PTE, the GPU must not be accessing it.
  2817. * If we wait upon the object, we know that all the bound
  2818. * VMA are no longer active.
  2819. */
  2820. ret = i915_gem_object_wait_rendering(obj, false);
  2821. if (ret)
  2822. return ret;
  2823. if (!HAS_LLC(obj->base.dev) && cache_level != I915_CACHE_NONE) {
  2824. /* Access to snoopable pages through the GTT is
  2825. * incoherent and on some machines causes a hard
  2826. * lockup. Relinquish the CPU mmaping to force
  2827. * userspace to refault in the pages and we can
  2828. * then double check if the GTT mapping is still
  2829. * valid for that pointer access.
  2830. */
  2831. i915_gem_release_mmap(obj);
  2832. /* As we no longer need a fence for GTT access,
  2833. * we can relinquish it now (and so prevent having
  2834. * to steal a fence from someone else on the next
  2835. * fence request). Note GPU activity would have
  2836. * dropped the fence as all snoopable access is
  2837. * supposed to be linear.
  2838. */
  2839. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2840. ret = i915_vma_put_fence(vma);
  2841. if (ret)
  2842. return ret;
  2843. }
  2844. } else {
  2845. /* We either have incoherent backing store and
  2846. * so no GTT access or the architecture is fully
  2847. * coherent. In such cases, existing GTT mmaps
  2848. * ignore the cache bit in the PTE and we can
  2849. * rewrite it without confusing the GPU or having
  2850. * to force userspace to fault back in its mmaps.
  2851. */
  2852. }
  2853. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2854. if (!drm_mm_node_allocated(&vma->node))
  2855. continue;
  2856. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  2857. if (ret)
  2858. return ret;
  2859. }
  2860. }
  2861. list_for_each_entry(vma, &obj->vma_list, obj_link)
  2862. vma->node.color = cache_level;
  2863. obj->cache_level = cache_level;
  2864. out:
  2865. /* Flush the dirty CPU caches to the backing storage so that the
  2866. * object is now coherent at its new cache level (with respect
  2867. * to the access domain).
  2868. */
  2869. if (obj->cache_dirty && cpu_write_needs_clflush(obj)) {
  2870. if (i915_gem_clflush_object(obj, true))
  2871. i915_gem_chipset_flush(to_i915(obj->base.dev));
  2872. }
  2873. return 0;
  2874. }
  2875. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  2876. struct drm_file *file)
  2877. {
  2878. struct drm_i915_gem_caching *args = data;
  2879. struct drm_i915_gem_object *obj;
  2880. obj = i915_gem_object_lookup(file, args->handle);
  2881. if (!obj)
  2882. return -ENOENT;
  2883. switch (obj->cache_level) {
  2884. case I915_CACHE_LLC:
  2885. case I915_CACHE_L3_LLC:
  2886. args->caching = I915_CACHING_CACHED;
  2887. break;
  2888. case I915_CACHE_WT:
  2889. args->caching = I915_CACHING_DISPLAY;
  2890. break;
  2891. default:
  2892. args->caching = I915_CACHING_NONE;
  2893. break;
  2894. }
  2895. i915_gem_object_put_unlocked(obj);
  2896. return 0;
  2897. }
  2898. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  2899. struct drm_file *file)
  2900. {
  2901. struct drm_i915_private *dev_priv = to_i915(dev);
  2902. struct drm_i915_gem_caching *args = data;
  2903. struct drm_i915_gem_object *obj;
  2904. enum i915_cache_level level;
  2905. int ret;
  2906. switch (args->caching) {
  2907. case I915_CACHING_NONE:
  2908. level = I915_CACHE_NONE;
  2909. break;
  2910. case I915_CACHING_CACHED:
  2911. /*
  2912. * Due to a HW issue on BXT A stepping, GPU stores via a
  2913. * snooped mapping may leave stale data in a corresponding CPU
  2914. * cacheline, whereas normally such cachelines would get
  2915. * invalidated.
  2916. */
  2917. if (!HAS_LLC(dev) && !HAS_SNOOP(dev))
  2918. return -ENODEV;
  2919. level = I915_CACHE_LLC;
  2920. break;
  2921. case I915_CACHING_DISPLAY:
  2922. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  2923. break;
  2924. default:
  2925. return -EINVAL;
  2926. }
  2927. intel_runtime_pm_get(dev_priv);
  2928. ret = i915_mutex_lock_interruptible(dev);
  2929. if (ret)
  2930. goto rpm_put;
  2931. obj = i915_gem_object_lookup(file, args->handle);
  2932. if (!obj) {
  2933. ret = -ENOENT;
  2934. goto unlock;
  2935. }
  2936. ret = i915_gem_object_set_cache_level(obj, level);
  2937. i915_gem_object_put(obj);
  2938. unlock:
  2939. mutex_unlock(&dev->struct_mutex);
  2940. rpm_put:
  2941. intel_runtime_pm_put(dev_priv);
  2942. return ret;
  2943. }
  2944. /*
  2945. * Prepare buffer for display plane (scanout, cursors, etc).
  2946. * Can be called from an uninterruptible phase (modesetting) and allows
  2947. * any flushes to be pipelined (for pageflips).
  2948. */
  2949. struct i915_vma *
  2950. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2951. u32 alignment,
  2952. const struct i915_ggtt_view *view)
  2953. {
  2954. struct i915_vma *vma;
  2955. u32 old_read_domains, old_write_domain;
  2956. int ret;
  2957. /* Mark the pin_display early so that we account for the
  2958. * display coherency whilst setting up the cache domains.
  2959. */
  2960. obj->pin_display++;
  2961. /* The display engine is not coherent with the LLC cache on gen6. As
  2962. * a result, we make sure that the pinning that is about to occur is
  2963. * done with uncached PTEs. This is lowest common denominator for all
  2964. * chipsets.
  2965. *
  2966. * However for gen6+, we could do better by using the GFDT bit instead
  2967. * of uncaching, which would allow us to flush all the LLC-cached data
  2968. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2969. */
  2970. ret = i915_gem_object_set_cache_level(obj,
  2971. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  2972. if (ret) {
  2973. vma = ERR_PTR(ret);
  2974. goto err_unpin_display;
  2975. }
  2976. /* As the user may map the buffer once pinned in the display plane
  2977. * (e.g. libkms for the bootup splash), we have to ensure that we
  2978. * always use map_and_fenceable for all scanout buffers. However,
  2979. * it may simply be too big to fit into mappable, in which case
  2980. * put it anyway and hope that userspace can cope (but always first
  2981. * try to preserve the existing ABI).
  2982. */
  2983. vma = ERR_PTR(-ENOSPC);
  2984. if (view->type == I915_GGTT_VIEW_NORMAL)
  2985. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
  2986. PIN_MAPPABLE | PIN_NONBLOCK);
  2987. if (IS_ERR(vma))
  2988. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, 0);
  2989. if (IS_ERR(vma))
  2990. goto err_unpin_display;
  2991. vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
  2992. WARN_ON(obj->pin_display > i915_vma_pin_count(vma));
  2993. i915_gem_object_flush_cpu_write_domain(obj);
  2994. old_write_domain = obj->base.write_domain;
  2995. old_read_domains = obj->base.read_domains;
  2996. /* It should now be out of any other write domains, and we can update
  2997. * the domain values for our changes.
  2998. */
  2999. obj->base.write_domain = 0;
  3000. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3001. trace_i915_gem_object_change_domain(obj,
  3002. old_read_domains,
  3003. old_write_domain);
  3004. return vma;
  3005. err_unpin_display:
  3006. obj->pin_display--;
  3007. return vma;
  3008. }
  3009. void
  3010. i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
  3011. {
  3012. if (WARN_ON(vma->obj->pin_display == 0))
  3013. return;
  3014. if (--vma->obj->pin_display == 0)
  3015. vma->display_alignment = 0;
  3016. /* Bump the LRU to try and avoid premature eviction whilst flipping */
  3017. if (!i915_vma_is_active(vma))
  3018. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  3019. i915_vma_unpin(vma);
  3020. WARN_ON(vma->obj->pin_display > i915_vma_pin_count(vma));
  3021. }
  3022. /**
  3023. * Moves a single object to the CPU read, and possibly write domain.
  3024. * @obj: object to act on
  3025. * @write: requesting write or read-only access
  3026. *
  3027. * This function returns when the move is complete, including waiting on
  3028. * flushes to occur.
  3029. */
  3030. int
  3031. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3032. {
  3033. uint32_t old_write_domain, old_read_domains;
  3034. int ret;
  3035. ret = i915_gem_object_wait_rendering(obj, !write);
  3036. if (ret)
  3037. return ret;
  3038. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3039. return 0;
  3040. i915_gem_object_flush_gtt_write_domain(obj);
  3041. old_write_domain = obj->base.write_domain;
  3042. old_read_domains = obj->base.read_domains;
  3043. /* Flush the CPU cache if it's still invalid. */
  3044. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3045. i915_gem_clflush_object(obj, false);
  3046. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3047. }
  3048. /* It should now be out of any other write domains, and we can update
  3049. * the domain values for our changes.
  3050. */
  3051. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3052. /* If we're writing through the CPU, then the GPU read domains will
  3053. * need to be invalidated at next use.
  3054. */
  3055. if (write) {
  3056. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3057. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3058. }
  3059. trace_i915_gem_object_change_domain(obj,
  3060. old_read_domains,
  3061. old_write_domain);
  3062. return 0;
  3063. }
  3064. /* Throttle our rendering by waiting until the ring has completed our requests
  3065. * emitted over 20 msec ago.
  3066. *
  3067. * Note that if we were to use the current jiffies each time around the loop,
  3068. * we wouldn't escape the function with any frames outstanding if the time to
  3069. * render a frame was over 20ms.
  3070. *
  3071. * This should get us reasonable parallelism between CPU and GPU but also
  3072. * relatively low latency when blocking on a particular request to finish.
  3073. */
  3074. static int
  3075. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3076. {
  3077. struct drm_i915_private *dev_priv = to_i915(dev);
  3078. struct drm_i915_file_private *file_priv = file->driver_priv;
  3079. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3080. struct drm_i915_gem_request *request, *target = NULL;
  3081. int ret;
  3082. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3083. if (ret)
  3084. return ret;
  3085. /* ABI: return -EIO if already wedged */
  3086. if (i915_terminally_wedged(&dev_priv->gpu_error))
  3087. return -EIO;
  3088. spin_lock(&file_priv->mm.lock);
  3089. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3090. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3091. break;
  3092. /*
  3093. * Note that the request might not have been submitted yet.
  3094. * In which case emitted_jiffies will be zero.
  3095. */
  3096. if (!request->emitted_jiffies)
  3097. continue;
  3098. target = request;
  3099. }
  3100. if (target)
  3101. i915_gem_request_get(target);
  3102. spin_unlock(&file_priv->mm.lock);
  3103. if (target == NULL)
  3104. return 0;
  3105. ret = i915_wait_request(target, I915_WAIT_INTERRUPTIBLE, NULL, NULL);
  3106. i915_gem_request_put(target);
  3107. return ret;
  3108. }
  3109. static bool
  3110. i915_vma_misplaced(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
  3111. {
  3112. if (!drm_mm_node_allocated(&vma->node))
  3113. return false;
  3114. if (vma->node.size < size)
  3115. return true;
  3116. if (alignment && vma->node.start & (alignment - 1))
  3117. return true;
  3118. if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
  3119. return true;
  3120. if (flags & PIN_OFFSET_BIAS &&
  3121. vma->node.start < (flags & PIN_OFFSET_MASK))
  3122. return true;
  3123. if (flags & PIN_OFFSET_FIXED &&
  3124. vma->node.start != (flags & PIN_OFFSET_MASK))
  3125. return true;
  3126. return false;
  3127. }
  3128. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
  3129. {
  3130. struct drm_i915_gem_object *obj = vma->obj;
  3131. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  3132. bool mappable, fenceable;
  3133. u32 fence_size, fence_alignment;
  3134. fence_size = i915_gem_get_ggtt_size(dev_priv,
  3135. vma->size,
  3136. i915_gem_object_get_tiling(obj));
  3137. fence_alignment = i915_gem_get_ggtt_alignment(dev_priv,
  3138. vma->size,
  3139. i915_gem_object_get_tiling(obj),
  3140. true);
  3141. fenceable = (vma->node.size == fence_size &&
  3142. (vma->node.start & (fence_alignment - 1)) == 0);
  3143. mappable = (vma->node.start + fence_size <=
  3144. dev_priv->ggtt.mappable_end);
  3145. if (mappable && fenceable)
  3146. vma->flags |= I915_VMA_CAN_FENCE;
  3147. else
  3148. vma->flags &= ~I915_VMA_CAN_FENCE;
  3149. }
  3150. int __i915_vma_do_pin(struct i915_vma *vma,
  3151. u64 size, u64 alignment, u64 flags)
  3152. {
  3153. unsigned int bound = vma->flags;
  3154. int ret;
  3155. GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
  3156. GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
  3157. if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
  3158. ret = -EBUSY;
  3159. goto err;
  3160. }
  3161. if ((bound & I915_VMA_BIND_MASK) == 0) {
  3162. ret = i915_vma_insert(vma, size, alignment, flags);
  3163. if (ret)
  3164. goto err;
  3165. }
  3166. ret = i915_vma_bind(vma, vma->obj->cache_level, flags);
  3167. if (ret)
  3168. goto err;
  3169. if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
  3170. __i915_vma_set_map_and_fenceable(vma);
  3171. GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
  3172. return 0;
  3173. err:
  3174. __i915_vma_unpin(vma);
  3175. return ret;
  3176. }
  3177. struct i915_vma *
  3178. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3179. const struct i915_ggtt_view *view,
  3180. u64 size,
  3181. u64 alignment,
  3182. u64 flags)
  3183. {
  3184. struct i915_address_space *vm = &to_i915(obj->base.dev)->ggtt.base;
  3185. struct i915_vma *vma;
  3186. int ret;
  3187. vma = i915_gem_obj_lookup_or_create_vma(obj, vm, view);
  3188. if (IS_ERR(vma))
  3189. return vma;
  3190. if (i915_vma_misplaced(vma, size, alignment, flags)) {
  3191. if (flags & PIN_NONBLOCK &&
  3192. (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
  3193. return ERR_PTR(-ENOSPC);
  3194. WARN(i915_vma_is_pinned(vma),
  3195. "bo is already pinned in ggtt with incorrect alignment:"
  3196. " offset=%08x, req.alignment=%llx,"
  3197. " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
  3198. i915_ggtt_offset(vma), alignment,
  3199. !!(flags & PIN_MAPPABLE),
  3200. i915_vma_is_map_and_fenceable(vma));
  3201. ret = i915_vma_unbind(vma);
  3202. if (ret)
  3203. return ERR_PTR(ret);
  3204. }
  3205. ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
  3206. if (ret)
  3207. return ERR_PTR(ret);
  3208. return vma;
  3209. }
  3210. static __always_inline unsigned int __busy_read_flag(unsigned int id)
  3211. {
  3212. /* Note that we could alias engines in the execbuf API, but
  3213. * that would be very unwise as it prevents userspace from
  3214. * fine control over engine selection. Ahem.
  3215. *
  3216. * This should be something like EXEC_MAX_ENGINE instead of
  3217. * I915_NUM_ENGINES.
  3218. */
  3219. BUILD_BUG_ON(I915_NUM_ENGINES > 16);
  3220. return 0x10000 << id;
  3221. }
  3222. static __always_inline unsigned int __busy_write_id(unsigned int id)
  3223. {
  3224. /* The uABI guarantees an active writer is also amongst the read
  3225. * engines. This would be true if we accessed the activity tracking
  3226. * under the lock, but as we perform the lookup of the object and
  3227. * its activity locklessly we can not guarantee that the last_write
  3228. * being active implies that we have set the same engine flag from
  3229. * last_read - hence we always set both read and write busy for
  3230. * last_write.
  3231. */
  3232. return id | __busy_read_flag(id);
  3233. }
  3234. static __always_inline unsigned int
  3235. __busy_set_if_active(const struct i915_gem_active *active,
  3236. unsigned int (*flag)(unsigned int id))
  3237. {
  3238. struct drm_i915_gem_request *request;
  3239. request = rcu_dereference(active->request);
  3240. if (!request || i915_gem_request_completed(request))
  3241. return 0;
  3242. /* This is racy. See __i915_gem_active_get_rcu() for an in detail
  3243. * discussion of how to handle the race correctly, but for reporting
  3244. * the busy state we err on the side of potentially reporting the
  3245. * wrong engine as being busy (but we guarantee that the result
  3246. * is at least self-consistent).
  3247. *
  3248. * As we use SLAB_DESTROY_BY_RCU, the request may be reallocated
  3249. * whilst we are inspecting it, even under the RCU read lock as we are.
  3250. * This means that there is a small window for the engine and/or the
  3251. * seqno to have been overwritten. The seqno will always be in the
  3252. * future compared to the intended, and so we know that if that
  3253. * seqno is idle (on whatever engine) our request is idle and the
  3254. * return 0 above is correct.
  3255. *
  3256. * The issue is that if the engine is switched, it is just as likely
  3257. * to report that it is busy (but since the switch happened, we know
  3258. * the request should be idle). So there is a small chance that a busy
  3259. * result is actually the wrong engine.
  3260. *
  3261. * So why don't we care?
  3262. *
  3263. * For starters, the busy ioctl is a heuristic that is by definition
  3264. * racy. Even with perfect serialisation in the driver, the hardware
  3265. * state is constantly advancing - the state we report to the user
  3266. * is stale.
  3267. *
  3268. * The critical information for the busy-ioctl is whether the object
  3269. * is idle as userspace relies on that to detect whether its next
  3270. * access will stall, or if it has missed submitting commands to
  3271. * the hardware allowing the GPU to stall. We never generate a
  3272. * false-positive for idleness, thus busy-ioctl is reliable at the
  3273. * most fundamental level, and we maintain the guarantee that a
  3274. * busy object left to itself will eventually become idle (and stay
  3275. * idle!).
  3276. *
  3277. * We allow ourselves the leeway of potentially misreporting the busy
  3278. * state because that is an optimisation heuristic that is constantly
  3279. * in flux. Being quickly able to detect the busy/idle state is much
  3280. * more important than accurate logging of exactly which engines were
  3281. * busy.
  3282. *
  3283. * For accuracy in reporting the engine, we could use
  3284. *
  3285. * result = 0;
  3286. * request = __i915_gem_active_get_rcu(active);
  3287. * if (request) {
  3288. * if (!i915_gem_request_completed(request))
  3289. * result = flag(request->engine->exec_id);
  3290. * i915_gem_request_put(request);
  3291. * }
  3292. *
  3293. * but that still remains susceptible to both hardware and userspace
  3294. * races. So we accept making the result of that race slightly worse,
  3295. * given the rarity of the race and its low impact on the result.
  3296. */
  3297. return flag(READ_ONCE(request->engine->exec_id));
  3298. }
  3299. static __always_inline unsigned int
  3300. busy_check_reader(const struct i915_gem_active *active)
  3301. {
  3302. return __busy_set_if_active(active, __busy_read_flag);
  3303. }
  3304. static __always_inline unsigned int
  3305. busy_check_writer(const struct i915_gem_active *active)
  3306. {
  3307. return __busy_set_if_active(active, __busy_write_id);
  3308. }
  3309. int
  3310. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3311. struct drm_file *file)
  3312. {
  3313. struct drm_i915_gem_busy *args = data;
  3314. struct drm_i915_gem_object *obj;
  3315. unsigned long active;
  3316. obj = i915_gem_object_lookup(file, args->handle);
  3317. if (!obj)
  3318. return -ENOENT;
  3319. args->busy = 0;
  3320. active = __I915_BO_ACTIVE(obj);
  3321. if (active) {
  3322. int idx;
  3323. /* Yes, the lookups are intentionally racy.
  3324. *
  3325. * First, we cannot simply rely on __I915_BO_ACTIVE. We have
  3326. * to regard the value as stale and as our ABI guarantees
  3327. * forward progress, we confirm the status of each active
  3328. * request with the hardware.
  3329. *
  3330. * Even though we guard the pointer lookup by RCU, that only
  3331. * guarantees that the pointer and its contents remain
  3332. * dereferencable and does *not* mean that the request we
  3333. * have is the same as the one being tracked by the object.
  3334. *
  3335. * Consider that we lookup the request just as it is being
  3336. * retired and freed. We take a local copy of the pointer,
  3337. * but before we add its engine into the busy set, the other
  3338. * thread reallocates it and assigns it to a task on another
  3339. * engine with a fresh and incomplete seqno. Guarding against
  3340. * that requires careful serialisation and reference counting,
  3341. * i.e. using __i915_gem_active_get_request_rcu(). We don't,
  3342. * instead we expect that if the result is busy, which engines
  3343. * are busy is not completely reliable - we only guarantee
  3344. * that the object was busy.
  3345. */
  3346. rcu_read_lock();
  3347. for_each_active(active, idx)
  3348. args->busy |= busy_check_reader(&obj->last_read[idx]);
  3349. /* For ABI sanity, we only care that the write engine is in
  3350. * the set of read engines. This should be ensured by the
  3351. * ordering of setting last_read/last_write in
  3352. * i915_vma_move_to_active(), and then in reverse in retire.
  3353. * However, for good measure, we always report the last_write
  3354. * request as a busy read as well as being a busy write.
  3355. *
  3356. * We don't care that the set of active read/write engines
  3357. * may change during construction of the result, as it is
  3358. * equally liable to change before userspace can inspect
  3359. * the result.
  3360. */
  3361. args->busy |= busy_check_writer(&obj->last_write);
  3362. rcu_read_unlock();
  3363. }
  3364. i915_gem_object_put_unlocked(obj);
  3365. return 0;
  3366. }
  3367. int
  3368. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3369. struct drm_file *file_priv)
  3370. {
  3371. return i915_gem_ring_throttle(dev, file_priv);
  3372. }
  3373. int
  3374. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3375. struct drm_file *file_priv)
  3376. {
  3377. struct drm_i915_private *dev_priv = to_i915(dev);
  3378. struct drm_i915_gem_madvise *args = data;
  3379. struct drm_i915_gem_object *obj;
  3380. int ret;
  3381. switch (args->madv) {
  3382. case I915_MADV_DONTNEED:
  3383. case I915_MADV_WILLNEED:
  3384. break;
  3385. default:
  3386. return -EINVAL;
  3387. }
  3388. ret = i915_mutex_lock_interruptible(dev);
  3389. if (ret)
  3390. return ret;
  3391. obj = i915_gem_object_lookup(file_priv, args->handle);
  3392. if (!obj) {
  3393. ret = -ENOENT;
  3394. goto unlock;
  3395. }
  3396. if (obj->pages &&
  3397. i915_gem_object_is_tiled(obj) &&
  3398. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3399. if (obj->madv == I915_MADV_WILLNEED)
  3400. i915_gem_object_unpin_pages(obj);
  3401. if (args->madv == I915_MADV_WILLNEED)
  3402. i915_gem_object_pin_pages(obj);
  3403. }
  3404. if (obj->madv != __I915_MADV_PURGED)
  3405. obj->madv = args->madv;
  3406. /* if the object is no longer attached, discard its backing storage */
  3407. if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
  3408. i915_gem_object_truncate(obj);
  3409. args->retained = obj->madv != __I915_MADV_PURGED;
  3410. i915_gem_object_put(obj);
  3411. unlock:
  3412. mutex_unlock(&dev->struct_mutex);
  3413. return ret;
  3414. }
  3415. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3416. const struct drm_i915_gem_object_ops *ops)
  3417. {
  3418. int i;
  3419. INIT_LIST_HEAD(&obj->global_list);
  3420. for (i = 0; i < I915_NUM_ENGINES; i++)
  3421. init_request_active(&obj->last_read[i],
  3422. i915_gem_object_retire__read);
  3423. init_request_active(&obj->last_write,
  3424. i915_gem_object_retire__write);
  3425. INIT_LIST_HEAD(&obj->obj_exec_link);
  3426. INIT_LIST_HEAD(&obj->vma_list);
  3427. INIT_LIST_HEAD(&obj->batch_pool_link);
  3428. obj->ops = ops;
  3429. obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
  3430. obj->madv = I915_MADV_WILLNEED;
  3431. i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
  3432. }
  3433. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3434. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
  3435. .get_pages = i915_gem_object_get_pages_gtt,
  3436. .put_pages = i915_gem_object_put_pages_gtt,
  3437. };
  3438. struct drm_i915_gem_object *i915_gem_object_create(struct drm_device *dev,
  3439. size_t size)
  3440. {
  3441. struct drm_i915_gem_object *obj;
  3442. struct address_space *mapping;
  3443. gfp_t mask;
  3444. int ret;
  3445. obj = i915_gem_object_alloc(dev);
  3446. if (obj == NULL)
  3447. return ERR_PTR(-ENOMEM);
  3448. ret = drm_gem_object_init(dev, &obj->base, size);
  3449. if (ret)
  3450. goto fail;
  3451. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3452. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3453. /* 965gm cannot relocate objects above 4GiB. */
  3454. mask &= ~__GFP_HIGHMEM;
  3455. mask |= __GFP_DMA32;
  3456. }
  3457. mapping = obj->base.filp->f_mapping;
  3458. mapping_set_gfp_mask(mapping, mask);
  3459. i915_gem_object_init(obj, &i915_gem_object_ops);
  3460. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3461. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3462. if (HAS_LLC(dev)) {
  3463. /* On some devices, we can have the GPU use the LLC (the CPU
  3464. * cache) for about a 10% performance improvement
  3465. * compared to uncached. Graphics requests other than
  3466. * display scanout are coherent with the CPU in
  3467. * accessing this cache. This means in this mode we
  3468. * don't need to clflush on the CPU side, and on the
  3469. * GPU side we only need to flush internal caches to
  3470. * get data visible to the CPU.
  3471. *
  3472. * However, we maintain the display planes as UC, and so
  3473. * need to rebind when first used as such.
  3474. */
  3475. obj->cache_level = I915_CACHE_LLC;
  3476. } else
  3477. obj->cache_level = I915_CACHE_NONE;
  3478. trace_i915_gem_object_create(obj);
  3479. return obj;
  3480. fail:
  3481. i915_gem_object_free(obj);
  3482. return ERR_PTR(ret);
  3483. }
  3484. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3485. {
  3486. /* If we are the last user of the backing storage (be it shmemfs
  3487. * pages or stolen etc), we know that the pages are going to be
  3488. * immediately released. In this case, we can then skip copying
  3489. * back the contents from the GPU.
  3490. */
  3491. if (obj->madv != I915_MADV_WILLNEED)
  3492. return false;
  3493. if (obj->base.filp == NULL)
  3494. return true;
  3495. /* At first glance, this looks racy, but then again so would be
  3496. * userspace racing mmap against close. However, the first external
  3497. * reference to the filp can only be obtained through the
  3498. * i915_gem_mmap_ioctl() which safeguards us against the user
  3499. * acquiring such a reference whilst we are in the middle of
  3500. * freeing the object.
  3501. */
  3502. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3503. }
  3504. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3505. {
  3506. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3507. struct drm_device *dev = obj->base.dev;
  3508. struct drm_i915_private *dev_priv = to_i915(dev);
  3509. struct i915_vma *vma, *next;
  3510. intel_runtime_pm_get(dev_priv);
  3511. trace_i915_gem_object_destroy(obj);
  3512. /* All file-owned VMA should have been released by this point through
  3513. * i915_gem_close_object(), or earlier by i915_gem_context_close().
  3514. * However, the object may also be bound into the global GTT (e.g.
  3515. * older GPUs without per-process support, or for direct access through
  3516. * the GTT either for the user or for scanout). Those VMA still need to
  3517. * unbound now.
  3518. */
  3519. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
  3520. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  3521. GEM_BUG_ON(i915_vma_is_active(vma));
  3522. vma->flags &= ~I915_VMA_PIN_MASK;
  3523. i915_vma_close(vma);
  3524. }
  3525. GEM_BUG_ON(obj->bind_count);
  3526. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3527. * before progressing. */
  3528. if (obj->stolen)
  3529. i915_gem_object_unpin_pages(obj);
  3530. WARN_ON(atomic_read(&obj->frontbuffer_bits));
  3531. if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
  3532. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
  3533. i915_gem_object_is_tiled(obj))
  3534. i915_gem_object_unpin_pages(obj);
  3535. if (WARN_ON(obj->pages_pin_count))
  3536. obj->pages_pin_count = 0;
  3537. if (discard_backing_storage(obj))
  3538. obj->madv = I915_MADV_DONTNEED;
  3539. i915_gem_object_put_pages(obj);
  3540. BUG_ON(obj->pages);
  3541. if (obj->base.import_attach)
  3542. drm_prime_gem_destroy(&obj->base, NULL);
  3543. if (obj->ops->release)
  3544. obj->ops->release(obj);
  3545. drm_gem_object_release(&obj->base);
  3546. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3547. kfree(obj->bit_17);
  3548. i915_gem_object_free(obj);
  3549. intel_runtime_pm_put(dev_priv);
  3550. }
  3551. int i915_gem_suspend(struct drm_device *dev)
  3552. {
  3553. struct drm_i915_private *dev_priv = to_i915(dev);
  3554. int ret;
  3555. intel_suspend_gt_powersave(dev_priv);
  3556. mutex_lock(&dev->struct_mutex);
  3557. /* We have to flush all the executing contexts to main memory so
  3558. * that they can saved in the hibernation image. To ensure the last
  3559. * context image is coherent, we have to switch away from it. That
  3560. * leaves the dev_priv->kernel_context still active when
  3561. * we actually suspend, and its image in memory may not match the GPU
  3562. * state. Fortunately, the kernel_context is disposable and we do
  3563. * not rely on its state.
  3564. */
  3565. ret = i915_gem_switch_to_kernel_context(dev_priv);
  3566. if (ret)
  3567. goto err;
  3568. ret = i915_gem_wait_for_idle(dev_priv,
  3569. I915_WAIT_INTERRUPTIBLE |
  3570. I915_WAIT_LOCKED);
  3571. if (ret)
  3572. goto err;
  3573. i915_gem_retire_requests(dev_priv);
  3574. i915_gem_context_lost(dev_priv);
  3575. mutex_unlock(&dev->struct_mutex);
  3576. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  3577. cancel_delayed_work_sync(&dev_priv->gt.retire_work);
  3578. flush_delayed_work(&dev_priv->gt.idle_work);
  3579. /* Assert that we sucessfully flushed all the work and
  3580. * reset the GPU back to its idle, low power state.
  3581. */
  3582. WARN_ON(dev_priv->gt.awake);
  3583. return 0;
  3584. err:
  3585. mutex_unlock(&dev->struct_mutex);
  3586. return ret;
  3587. }
  3588. void i915_gem_resume(struct drm_device *dev)
  3589. {
  3590. struct drm_i915_private *dev_priv = to_i915(dev);
  3591. mutex_lock(&dev->struct_mutex);
  3592. i915_gem_restore_gtt_mappings(dev);
  3593. /* As we didn't flush the kernel context before suspend, we cannot
  3594. * guarantee that the context image is complete. So let's just reset
  3595. * it and start again.
  3596. */
  3597. dev_priv->gt.resume(dev_priv);
  3598. mutex_unlock(&dev->struct_mutex);
  3599. }
  3600. void i915_gem_init_swizzling(struct drm_device *dev)
  3601. {
  3602. struct drm_i915_private *dev_priv = to_i915(dev);
  3603. if (INTEL_INFO(dev)->gen < 5 ||
  3604. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3605. return;
  3606. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3607. DISP_TILE_SURFACE_SWIZZLING);
  3608. if (IS_GEN5(dev))
  3609. return;
  3610. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3611. if (IS_GEN6(dev))
  3612. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3613. else if (IS_GEN7(dev))
  3614. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3615. else if (IS_GEN8(dev))
  3616. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3617. else
  3618. BUG();
  3619. }
  3620. static void init_unused_ring(struct drm_device *dev, u32 base)
  3621. {
  3622. struct drm_i915_private *dev_priv = to_i915(dev);
  3623. I915_WRITE(RING_CTL(base), 0);
  3624. I915_WRITE(RING_HEAD(base), 0);
  3625. I915_WRITE(RING_TAIL(base), 0);
  3626. I915_WRITE(RING_START(base), 0);
  3627. }
  3628. static void init_unused_rings(struct drm_device *dev)
  3629. {
  3630. if (IS_I830(dev)) {
  3631. init_unused_ring(dev, PRB1_BASE);
  3632. init_unused_ring(dev, SRB0_BASE);
  3633. init_unused_ring(dev, SRB1_BASE);
  3634. init_unused_ring(dev, SRB2_BASE);
  3635. init_unused_ring(dev, SRB3_BASE);
  3636. } else if (IS_GEN2(dev)) {
  3637. init_unused_ring(dev, SRB0_BASE);
  3638. init_unused_ring(dev, SRB1_BASE);
  3639. } else if (IS_GEN3(dev)) {
  3640. init_unused_ring(dev, PRB1_BASE);
  3641. init_unused_ring(dev, PRB2_BASE);
  3642. }
  3643. }
  3644. int
  3645. i915_gem_init_hw(struct drm_device *dev)
  3646. {
  3647. struct drm_i915_private *dev_priv = to_i915(dev);
  3648. struct intel_engine_cs *engine;
  3649. int ret;
  3650. /* Double layer security blanket, see i915_gem_init() */
  3651. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3652. if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
  3653. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3654. if (IS_HASWELL(dev))
  3655. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  3656. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3657. if (HAS_PCH_NOP(dev)) {
  3658. if (IS_IVYBRIDGE(dev)) {
  3659. u32 temp = I915_READ(GEN7_MSG_CTL);
  3660. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3661. I915_WRITE(GEN7_MSG_CTL, temp);
  3662. } else if (INTEL_INFO(dev)->gen >= 7) {
  3663. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  3664. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  3665. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  3666. }
  3667. }
  3668. i915_gem_init_swizzling(dev);
  3669. /*
  3670. * At least 830 can leave some of the unused rings
  3671. * "active" (ie. head != tail) after resume which
  3672. * will prevent c3 entry. Makes sure all unused rings
  3673. * are totally idle.
  3674. */
  3675. init_unused_rings(dev);
  3676. BUG_ON(!dev_priv->kernel_context);
  3677. ret = i915_ppgtt_init_hw(dev);
  3678. if (ret) {
  3679. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  3680. goto out;
  3681. }
  3682. /* Need to do basic initialisation of all rings first: */
  3683. for_each_engine(engine, dev_priv) {
  3684. ret = engine->init_hw(engine);
  3685. if (ret)
  3686. goto out;
  3687. }
  3688. intel_mocs_init_l3cc_table(dev);
  3689. /* We can't enable contexts until all firmware is loaded */
  3690. ret = intel_guc_setup(dev);
  3691. if (ret)
  3692. goto out;
  3693. out:
  3694. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3695. return ret;
  3696. }
  3697. bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
  3698. {
  3699. if (INTEL_INFO(dev_priv)->gen < 6)
  3700. return false;
  3701. /* TODO: make semaphores and Execlists play nicely together */
  3702. if (i915.enable_execlists)
  3703. return false;
  3704. if (value >= 0)
  3705. return value;
  3706. #ifdef CONFIG_INTEL_IOMMU
  3707. /* Enable semaphores on SNB when IO remapping is off */
  3708. if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
  3709. return false;
  3710. #endif
  3711. return true;
  3712. }
  3713. int i915_gem_init(struct drm_device *dev)
  3714. {
  3715. struct drm_i915_private *dev_priv = to_i915(dev);
  3716. int ret;
  3717. mutex_lock(&dev->struct_mutex);
  3718. if (!i915.enable_execlists) {
  3719. dev_priv->gt.resume = intel_legacy_submission_resume;
  3720. dev_priv->gt.cleanup_engine = intel_engine_cleanup;
  3721. } else {
  3722. dev_priv->gt.resume = intel_lr_context_resume;
  3723. dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
  3724. }
  3725. /* This is just a security blanket to placate dragons.
  3726. * On some systems, we very sporadically observe that the first TLBs
  3727. * used by the CS may be stale, despite us poking the TLB reset. If
  3728. * we hold the forcewake during initialisation these problems
  3729. * just magically go away.
  3730. */
  3731. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3732. i915_gem_init_userptr(dev_priv);
  3733. ret = i915_gem_init_ggtt(dev_priv);
  3734. if (ret)
  3735. goto out_unlock;
  3736. ret = i915_gem_context_init(dev);
  3737. if (ret)
  3738. goto out_unlock;
  3739. ret = intel_engines_init(dev);
  3740. if (ret)
  3741. goto out_unlock;
  3742. ret = i915_gem_init_hw(dev);
  3743. if (ret == -EIO) {
  3744. /* Allow engine initialisation to fail by marking the GPU as
  3745. * wedged. But we only want to do this where the GPU is angry,
  3746. * for all other failure, such as an allocation failure, bail.
  3747. */
  3748. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  3749. i915_gem_set_wedged(dev_priv);
  3750. ret = 0;
  3751. }
  3752. out_unlock:
  3753. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3754. mutex_unlock(&dev->struct_mutex);
  3755. return ret;
  3756. }
  3757. void
  3758. i915_gem_cleanup_engines(struct drm_device *dev)
  3759. {
  3760. struct drm_i915_private *dev_priv = to_i915(dev);
  3761. struct intel_engine_cs *engine;
  3762. for_each_engine(engine, dev_priv)
  3763. dev_priv->gt.cleanup_engine(engine);
  3764. }
  3765. static void
  3766. init_engine_lists(struct intel_engine_cs *engine)
  3767. {
  3768. INIT_LIST_HEAD(&engine->request_list);
  3769. }
  3770. void
  3771. i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  3772. {
  3773. struct drm_device *dev = &dev_priv->drm;
  3774. int i;
  3775. if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
  3776. !IS_CHERRYVIEW(dev_priv))
  3777. dev_priv->num_fence_regs = 32;
  3778. else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
  3779. IS_I945GM(dev_priv) || IS_G33(dev_priv))
  3780. dev_priv->num_fence_regs = 16;
  3781. else
  3782. dev_priv->num_fence_regs = 8;
  3783. if (intel_vgpu_active(dev_priv))
  3784. dev_priv->num_fence_regs =
  3785. I915_READ(vgtif_reg(avail_rs.fence_num));
  3786. /* Initialize fence registers to zero */
  3787. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  3788. struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
  3789. fence->i915 = dev_priv;
  3790. fence->id = i;
  3791. list_add_tail(&fence->link, &dev_priv->mm.fence_list);
  3792. }
  3793. i915_gem_restore_fences(dev);
  3794. i915_gem_detect_bit_6_swizzle(dev);
  3795. }
  3796. void
  3797. i915_gem_load_init(struct drm_device *dev)
  3798. {
  3799. struct drm_i915_private *dev_priv = to_i915(dev);
  3800. int i;
  3801. dev_priv->objects =
  3802. kmem_cache_create("i915_gem_object",
  3803. sizeof(struct drm_i915_gem_object), 0,
  3804. SLAB_HWCACHE_ALIGN,
  3805. NULL);
  3806. dev_priv->vmas =
  3807. kmem_cache_create("i915_gem_vma",
  3808. sizeof(struct i915_vma), 0,
  3809. SLAB_HWCACHE_ALIGN,
  3810. NULL);
  3811. dev_priv->requests =
  3812. kmem_cache_create("i915_gem_request",
  3813. sizeof(struct drm_i915_gem_request), 0,
  3814. SLAB_HWCACHE_ALIGN |
  3815. SLAB_RECLAIM_ACCOUNT |
  3816. SLAB_DESTROY_BY_RCU,
  3817. NULL);
  3818. INIT_LIST_HEAD(&dev_priv->context_list);
  3819. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  3820. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  3821. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3822. for (i = 0; i < I915_NUM_ENGINES; i++)
  3823. init_engine_lists(&dev_priv->engine[i]);
  3824. INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
  3825. i915_gem_retire_work_handler);
  3826. INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
  3827. i915_gem_idle_work_handler);
  3828. init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
  3829. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  3830. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3831. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3832. dev_priv->mm.interruptible = true;
  3833. atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
  3834. spin_lock_init(&dev_priv->fb_tracking.lock);
  3835. }
  3836. void i915_gem_load_cleanup(struct drm_device *dev)
  3837. {
  3838. struct drm_i915_private *dev_priv = to_i915(dev);
  3839. kmem_cache_destroy(dev_priv->requests);
  3840. kmem_cache_destroy(dev_priv->vmas);
  3841. kmem_cache_destroy(dev_priv->objects);
  3842. /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
  3843. rcu_barrier();
  3844. }
  3845. int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  3846. {
  3847. struct drm_i915_gem_object *obj;
  3848. struct list_head *phases[] = {
  3849. &dev_priv->mm.unbound_list,
  3850. &dev_priv->mm.bound_list,
  3851. NULL
  3852. }, **p;
  3853. /* Called just before we write the hibernation image.
  3854. *
  3855. * We need to update the domain tracking to reflect that the CPU
  3856. * will be accessing all the pages to create and restore from the
  3857. * hibernation, and so upon restoration those pages will be in the
  3858. * CPU domain.
  3859. *
  3860. * To make sure the hibernation image contains the latest state,
  3861. * we update that state just before writing out the image.
  3862. *
  3863. * To try and reduce the hibernation image, we manually shrink
  3864. * the objects as well.
  3865. */
  3866. i915_gem_shrink_all(dev_priv);
  3867. for (p = phases; *p; p++) {
  3868. list_for_each_entry(obj, *p, global_list) {
  3869. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3870. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3871. }
  3872. }
  3873. return 0;
  3874. }
  3875. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3876. {
  3877. struct drm_i915_file_private *file_priv = file->driver_priv;
  3878. struct drm_i915_gem_request *request;
  3879. /* Clean up our request list when the client is going away, so that
  3880. * later retire_requests won't dereference our soon-to-be-gone
  3881. * file_priv.
  3882. */
  3883. spin_lock(&file_priv->mm.lock);
  3884. list_for_each_entry(request, &file_priv->mm.request_list, client_list)
  3885. request->file_priv = NULL;
  3886. spin_unlock(&file_priv->mm.lock);
  3887. if (!list_empty(&file_priv->rps.link)) {
  3888. spin_lock(&to_i915(dev)->rps.client_lock);
  3889. list_del(&file_priv->rps.link);
  3890. spin_unlock(&to_i915(dev)->rps.client_lock);
  3891. }
  3892. }
  3893. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  3894. {
  3895. struct drm_i915_file_private *file_priv;
  3896. int ret;
  3897. DRM_DEBUG_DRIVER("\n");
  3898. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  3899. if (!file_priv)
  3900. return -ENOMEM;
  3901. file->driver_priv = file_priv;
  3902. file_priv->dev_priv = to_i915(dev);
  3903. file_priv->file = file;
  3904. INIT_LIST_HEAD(&file_priv->rps.link);
  3905. spin_lock_init(&file_priv->mm.lock);
  3906. INIT_LIST_HEAD(&file_priv->mm.request_list);
  3907. file_priv->bsd_engine = -1;
  3908. ret = i915_gem_context_open(dev, file);
  3909. if (ret)
  3910. kfree(file_priv);
  3911. return ret;
  3912. }
  3913. /**
  3914. * i915_gem_track_fb - update frontbuffer tracking
  3915. * @old: current GEM buffer for the frontbuffer slots
  3916. * @new: new GEM buffer for the frontbuffer slots
  3917. * @frontbuffer_bits: bitmask of frontbuffer slots
  3918. *
  3919. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  3920. * from @old and setting them in @new. Both @old and @new can be NULL.
  3921. */
  3922. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  3923. struct drm_i915_gem_object *new,
  3924. unsigned frontbuffer_bits)
  3925. {
  3926. /* Control of individual bits within the mask are guarded by
  3927. * the owning plane->mutex, i.e. we can never see concurrent
  3928. * manipulation of individual bits. But since the bitfield as a whole
  3929. * is updated using RMW, we need to use atomics in order to update
  3930. * the bits.
  3931. */
  3932. BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
  3933. sizeof(atomic_t) * BITS_PER_BYTE);
  3934. if (old) {
  3935. WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
  3936. atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
  3937. }
  3938. if (new) {
  3939. WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
  3940. atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
  3941. }
  3942. }
  3943. /* Like i915_gem_object_get_page(), but mark the returned page dirty */
  3944. struct page *
  3945. i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
  3946. {
  3947. struct page *page;
  3948. /* Only default objects have per-page dirty tracking */
  3949. if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
  3950. return NULL;
  3951. page = i915_gem_object_get_page(obj, n);
  3952. set_page_dirty(page);
  3953. return page;
  3954. }
  3955. /* Allocate a new GEM object and fill it with the supplied data */
  3956. struct drm_i915_gem_object *
  3957. i915_gem_object_create_from_data(struct drm_device *dev,
  3958. const void *data, size_t size)
  3959. {
  3960. struct drm_i915_gem_object *obj;
  3961. struct sg_table *sg;
  3962. size_t bytes;
  3963. int ret;
  3964. obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE));
  3965. if (IS_ERR(obj))
  3966. return obj;
  3967. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  3968. if (ret)
  3969. goto fail;
  3970. ret = i915_gem_object_get_pages(obj);
  3971. if (ret)
  3972. goto fail;
  3973. i915_gem_object_pin_pages(obj);
  3974. sg = obj->pages;
  3975. bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
  3976. obj->dirty = 1; /* Backing store is now out of date */
  3977. i915_gem_object_unpin_pages(obj);
  3978. if (WARN_ON(bytes != size)) {
  3979. DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
  3980. ret = -EFAULT;
  3981. goto fail;
  3982. }
  3983. return obj;
  3984. fail:
  3985. i915_gem_object_put(obj);
  3986. return ERR_PTR(ret);
  3987. }