i915_gem.c 130 KB

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