i915_gem.c 132 KB

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