i915_gem.c 142 KB

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