i915_gem.c 154 KB

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