i915_gem.c 131 KB

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