i915_gem.c 145 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_vgpu.h"
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. #include "intel_mocs.h"
  35. #include <linux/shmem_fs.h>
  36. #include <linux/slab.h>
  37. #include <linux/swap.h>
  38. #include <linux/pci.h>
  39. #include <linux/dma-buf.h>
  40. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  41. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  42. static void
  43. i915_gem_object_retire__write(struct drm_i915_gem_object *obj);
  44. static void
  45. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring);
  46. static bool cpu_cache_is_coherent(struct drm_device *dev,
  47. enum i915_cache_level level)
  48. {
  49. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  50. }
  51. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  52. {
  53. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  54. return false;
  55. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  56. return true;
  57. return obj->pin_display;
  58. }
  59. static int
  60. insert_mappable_node(struct drm_i915_private *i915,
  61. struct drm_mm_node *node, u32 size)
  62. {
  63. memset(node, 0, sizeof(*node));
  64. return drm_mm_insert_node_in_range_generic(&i915->ggtt.base.mm, node,
  65. size, 0, 0, 0,
  66. i915->ggtt.mappable_end,
  67. DRM_MM_SEARCH_DEFAULT,
  68. DRM_MM_CREATE_DEFAULT);
  69. }
  70. static void
  71. remove_mappable_node(struct drm_mm_node *node)
  72. {
  73. drm_mm_remove_node(node);
  74. }
  75. /* some bookkeeping */
  76. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  77. size_t size)
  78. {
  79. spin_lock(&dev_priv->mm.object_stat_lock);
  80. dev_priv->mm.object_count++;
  81. dev_priv->mm.object_memory += size;
  82. spin_unlock(&dev_priv->mm.object_stat_lock);
  83. }
  84. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  85. size_t size)
  86. {
  87. spin_lock(&dev_priv->mm.object_stat_lock);
  88. dev_priv->mm.object_count--;
  89. dev_priv->mm.object_memory -= size;
  90. spin_unlock(&dev_priv->mm.object_stat_lock);
  91. }
  92. static int
  93. i915_gem_wait_for_error(struct i915_gpu_error *error)
  94. {
  95. int ret;
  96. if (!i915_reset_in_progress(error))
  97. return 0;
  98. /*
  99. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  100. * userspace. If it takes that long something really bad is going on and
  101. * we should simply try to bail out and fail as gracefully as possible.
  102. */
  103. ret = wait_event_interruptible_timeout(error->reset_queue,
  104. !i915_reset_in_progress(error),
  105. 10*HZ);
  106. if (ret == 0) {
  107. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  108. return -EIO;
  109. } else if (ret < 0) {
  110. return ret;
  111. } else {
  112. return 0;
  113. }
  114. }
  115. int i915_mutex_lock_interruptible(struct drm_device *dev)
  116. {
  117. struct drm_i915_private *dev_priv = to_i915(dev);
  118. int ret;
  119. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  120. if (ret)
  121. return ret;
  122. ret = mutex_lock_interruptible(&dev->struct_mutex);
  123. if (ret)
  124. return ret;
  125. WARN_ON(i915_verify_lists(dev));
  126. return 0;
  127. }
  128. int
  129. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  130. struct drm_file *file)
  131. {
  132. struct drm_i915_private *dev_priv = to_i915(dev);
  133. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  134. struct drm_i915_gem_get_aperture *args = data;
  135. struct i915_vma *vma;
  136. size_t pinned;
  137. pinned = 0;
  138. mutex_lock(&dev->struct_mutex);
  139. list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
  140. if (vma->pin_count)
  141. pinned += vma->node.size;
  142. list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
  143. if (vma->pin_count)
  144. pinned += vma->node.size;
  145. mutex_unlock(&dev->struct_mutex);
  146. args->aper_size = ggtt->base.total;
  147. args->aper_available_size = args->aper_size - pinned;
  148. return 0;
  149. }
  150. static int
  151. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  152. {
  153. struct address_space *mapping = obj->base.filp->f_mapping;
  154. char *vaddr = obj->phys_handle->vaddr;
  155. struct sg_table *st;
  156. struct scatterlist *sg;
  157. int i;
  158. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  159. return -EINVAL;
  160. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  161. struct page *page;
  162. char *src;
  163. page = shmem_read_mapping_page(mapping, i);
  164. if (IS_ERR(page))
  165. return PTR_ERR(page);
  166. src = kmap_atomic(page);
  167. memcpy(vaddr, src, PAGE_SIZE);
  168. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  169. kunmap_atomic(src);
  170. put_page(page);
  171. vaddr += PAGE_SIZE;
  172. }
  173. i915_gem_chipset_flush(to_i915(obj->base.dev));
  174. st = kmalloc(sizeof(*st), GFP_KERNEL);
  175. if (st == NULL)
  176. return -ENOMEM;
  177. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  178. kfree(st);
  179. return -ENOMEM;
  180. }
  181. sg = st->sgl;
  182. sg->offset = 0;
  183. sg->length = obj->base.size;
  184. sg_dma_address(sg) = obj->phys_handle->busaddr;
  185. sg_dma_len(sg) = obj->base.size;
  186. obj->pages = st;
  187. return 0;
  188. }
  189. static void
  190. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
  191. {
  192. int ret;
  193. BUG_ON(obj->madv == __I915_MADV_PURGED);
  194. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  195. if (WARN_ON(ret)) {
  196. /* In the event of a disaster, abandon all caches and
  197. * hope for the best.
  198. */
  199. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  200. }
  201. if (obj->madv == I915_MADV_DONTNEED)
  202. obj->dirty = 0;
  203. if (obj->dirty) {
  204. struct address_space *mapping = obj->base.filp->f_mapping;
  205. char *vaddr = obj->phys_handle->vaddr;
  206. int i;
  207. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  208. struct page *page;
  209. char *dst;
  210. page = shmem_read_mapping_page(mapping, i);
  211. if (IS_ERR(page))
  212. continue;
  213. dst = kmap_atomic(page);
  214. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  215. memcpy(dst, vaddr, PAGE_SIZE);
  216. kunmap_atomic(dst);
  217. set_page_dirty(page);
  218. if (obj->madv == I915_MADV_WILLNEED)
  219. mark_page_accessed(page);
  220. put_page(page);
  221. vaddr += PAGE_SIZE;
  222. }
  223. obj->dirty = 0;
  224. }
  225. sg_free_table(obj->pages);
  226. kfree(obj->pages);
  227. }
  228. static void
  229. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  230. {
  231. drm_pci_free(obj->base.dev, obj->phys_handle);
  232. }
  233. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  234. .get_pages = i915_gem_object_get_pages_phys,
  235. .put_pages = i915_gem_object_put_pages_phys,
  236. .release = i915_gem_object_release_phys,
  237. };
  238. static int
  239. drop_pages(struct drm_i915_gem_object *obj)
  240. {
  241. struct i915_vma *vma, *next;
  242. int ret;
  243. drm_gem_object_reference(&obj->base);
  244. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link)
  245. if (i915_vma_unbind(vma))
  246. break;
  247. ret = i915_gem_object_put_pages(obj);
  248. drm_gem_object_unreference(&obj->base);
  249. return ret;
  250. }
  251. int
  252. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  253. int align)
  254. {
  255. drm_dma_handle_t *phys;
  256. int ret;
  257. if (obj->phys_handle) {
  258. if ((unsigned long)obj->phys_handle->vaddr & (align -1))
  259. return -EBUSY;
  260. return 0;
  261. }
  262. if (obj->madv != I915_MADV_WILLNEED)
  263. return -EFAULT;
  264. if (obj->base.filp == NULL)
  265. return -EINVAL;
  266. ret = drop_pages(obj);
  267. if (ret)
  268. return ret;
  269. /* create a new object */
  270. phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
  271. if (!phys)
  272. return -ENOMEM;
  273. obj->phys_handle = phys;
  274. obj->ops = &i915_gem_phys_ops;
  275. return i915_gem_object_get_pages(obj);
  276. }
  277. static int
  278. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  279. struct drm_i915_gem_pwrite *args,
  280. struct drm_file *file_priv)
  281. {
  282. struct drm_device *dev = obj->base.dev;
  283. void *vaddr = obj->phys_handle->vaddr + args->offset;
  284. char __user *user_data = u64_to_user_ptr(args->data_ptr);
  285. int ret = 0;
  286. /* We manually control the domain here and pretend that it
  287. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  288. */
  289. ret = i915_gem_object_wait_rendering(obj, false);
  290. if (ret)
  291. return ret;
  292. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  293. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  294. unsigned long unwritten;
  295. /* The physical object once assigned is fixed for the lifetime
  296. * of the obj, so we can safely drop the lock and continue
  297. * to access vaddr.
  298. */
  299. mutex_unlock(&dev->struct_mutex);
  300. unwritten = copy_from_user(vaddr, user_data, args->size);
  301. mutex_lock(&dev->struct_mutex);
  302. if (unwritten) {
  303. ret = -EFAULT;
  304. goto out;
  305. }
  306. }
  307. drm_clflush_virt_range(vaddr, args->size);
  308. i915_gem_chipset_flush(to_i915(dev));
  309. out:
  310. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  311. return ret;
  312. }
  313. void *i915_gem_object_alloc(struct drm_device *dev)
  314. {
  315. struct drm_i915_private *dev_priv = to_i915(dev);
  316. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  317. }
  318. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  319. {
  320. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  321. kmem_cache_free(dev_priv->objects, obj);
  322. }
  323. static int
  324. i915_gem_create(struct drm_file *file,
  325. struct drm_device *dev,
  326. uint64_t size,
  327. uint32_t *handle_p)
  328. {
  329. struct drm_i915_gem_object *obj;
  330. int ret;
  331. u32 handle;
  332. size = roundup(size, PAGE_SIZE);
  333. if (size == 0)
  334. return -EINVAL;
  335. /* Allocate the new object */
  336. obj = i915_gem_object_create(dev, size);
  337. if (IS_ERR(obj))
  338. return PTR_ERR(obj);
  339. ret = drm_gem_handle_create(file, &obj->base, &handle);
  340. /* drop reference from allocate - handle holds it now */
  341. drm_gem_object_unreference_unlocked(&obj->base);
  342. if (ret)
  343. return ret;
  344. *handle_p = handle;
  345. return 0;
  346. }
  347. int
  348. i915_gem_dumb_create(struct drm_file *file,
  349. struct drm_device *dev,
  350. struct drm_mode_create_dumb *args)
  351. {
  352. /* have to work out size/pitch and return them */
  353. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  354. args->size = args->pitch * args->height;
  355. return i915_gem_create(file, dev,
  356. args->size, &args->handle);
  357. }
  358. /**
  359. * Creates a new mm object and returns a handle to it.
  360. * @dev: drm device pointer
  361. * @data: ioctl data blob
  362. * @file: drm file pointer
  363. */
  364. int
  365. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  366. struct drm_file *file)
  367. {
  368. struct drm_i915_gem_create *args = data;
  369. return i915_gem_create(file, dev,
  370. args->size, &args->handle);
  371. }
  372. static inline int
  373. __copy_to_user_swizzled(char __user *cpu_vaddr,
  374. const char *gpu_vaddr, int gpu_offset,
  375. int length)
  376. {
  377. int ret, cpu_offset = 0;
  378. while (length > 0) {
  379. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  380. int this_length = min(cacheline_end - gpu_offset, length);
  381. int swizzled_gpu_offset = gpu_offset ^ 64;
  382. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  383. gpu_vaddr + swizzled_gpu_offset,
  384. this_length);
  385. if (ret)
  386. return ret + length;
  387. cpu_offset += this_length;
  388. gpu_offset += this_length;
  389. length -= this_length;
  390. }
  391. return 0;
  392. }
  393. static inline int
  394. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  395. const char __user *cpu_vaddr,
  396. int length)
  397. {
  398. int ret, cpu_offset = 0;
  399. while (length > 0) {
  400. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  401. int this_length = min(cacheline_end - gpu_offset, length);
  402. int swizzled_gpu_offset = gpu_offset ^ 64;
  403. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  404. cpu_vaddr + cpu_offset,
  405. this_length);
  406. if (ret)
  407. return ret + length;
  408. cpu_offset += this_length;
  409. gpu_offset += this_length;
  410. length -= this_length;
  411. }
  412. return 0;
  413. }
  414. /*
  415. * Pins the specified object's pages and synchronizes the object with
  416. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  417. * flush the object from the CPU cache.
  418. */
  419. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  420. int *needs_clflush)
  421. {
  422. int ret;
  423. *needs_clflush = 0;
  424. if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
  425. return -EINVAL;
  426. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  427. /* If we're not in the cpu read domain, set ourself into the gtt
  428. * read domain and manually flush cachelines (if required). This
  429. * optimizes for the case when the gpu will dirty the data
  430. * anyway again before the next pread happens. */
  431. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  432. obj->cache_level);
  433. ret = i915_gem_object_wait_rendering(obj, true);
  434. if (ret)
  435. return ret;
  436. }
  437. ret = i915_gem_object_get_pages(obj);
  438. if (ret)
  439. return ret;
  440. i915_gem_object_pin_pages(obj);
  441. return ret;
  442. }
  443. /* Per-page copy function for the shmem pread fastpath.
  444. * Flushes invalid cachelines before reading the target if
  445. * needs_clflush is set. */
  446. static int
  447. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  448. char __user *user_data,
  449. bool page_do_bit17_swizzling, bool needs_clflush)
  450. {
  451. char *vaddr;
  452. int ret;
  453. if (unlikely(page_do_bit17_swizzling))
  454. return -EINVAL;
  455. vaddr = kmap_atomic(page);
  456. if (needs_clflush)
  457. drm_clflush_virt_range(vaddr + shmem_page_offset,
  458. page_length);
  459. ret = __copy_to_user_inatomic(user_data,
  460. vaddr + shmem_page_offset,
  461. page_length);
  462. kunmap_atomic(vaddr);
  463. return ret ? -EFAULT : 0;
  464. }
  465. static void
  466. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  467. bool swizzled)
  468. {
  469. if (unlikely(swizzled)) {
  470. unsigned long start = (unsigned long) addr;
  471. unsigned long end = (unsigned long) addr + length;
  472. /* For swizzling simply ensure that we always flush both
  473. * channels. Lame, but simple and it works. Swizzled
  474. * pwrite/pread is far from a hotpath - current userspace
  475. * doesn't use it at all. */
  476. start = round_down(start, 128);
  477. end = round_up(end, 128);
  478. drm_clflush_virt_range((void *)start, end - start);
  479. } else {
  480. drm_clflush_virt_range(addr, length);
  481. }
  482. }
  483. /* Only difference to the fast-path function is that this can handle bit17
  484. * and uses non-atomic copy and kmap functions. */
  485. static int
  486. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  487. char __user *user_data,
  488. bool page_do_bit17_swizzling, bool needs_clflush)
  489. {
  490. char *vaddr;
  491. int ret;
  492. vaddr = kmap(page);
  493. if (needs_clflush)
  494. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  495. page_length,
  496. page_do_bit17_swizzling);
  497. if (page_do_bit17_swizzling)
  498. ret = __copy_to_user_swizzled(user_data,
  499. vaddr, shmem_page_offset,
  500. page_length);
  501. else
  502. ret = __copy_to_user(user_data,
  503. vaddr + shmem_page_offset,
  504. page_length);
  505. kunmap(page);
  506. return ret ? - EFAULT : 0;
  507. }
  508. static inline unsigned long
  509. slow_user_access(struct io_mapping *mapping,
  510. uint64_t page_base, int page_offset,
  511. char __user *user_data,
  512. unsigned long length, bool pwrite)
  513. {
  514. void __iomem *ioaddr;
  515. void *vaddr;
  516. uint64_t unwritten;
  517. ioaddr = io_mapping_map_wc(mapping, page_base, PAGE_SIZE);
  518. /* We can use the cpu mem copy function because this is X86. */
  519. vaddr = (void __force *)ioaddr + page_offset;
  520. if (pwrite)
  521. unwritten = __copy_from_user(vaddr, user_data, length);
  522. else
  523. unwritten = __copy_to_user(user_data, vaddr, length);
  524. io_mapping_unmap(ioaddr);
  525. return unwritten;
  526. }
  527. static int
  528. i915_gem_gtt_pread(struct drm_device *dev,
  529. struct drm_i915_gem_object *obj, uint64_t size,
  530. uint64_t data_offset, uint64_t data_ptr)
  531. {
  532. struct drm_i915_private *dev_priv = to_i915(dev);
  533. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  534. struct drm_mm_node node;
  535. char __user *user_data;
  536. uint64_t remain;
  537. uint64_t offset;
  538. int ret;
  539. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE);
  540. if (ret) {
  541. ret = insert_mappable_node(dev_priv, &node, PAGE_SIZE);
  542. if (ret)
  543. goto out;
  544. ret = i915_gem_object_get_pages(obj);
  545. if (ret) {
  546. remove_mappable_node(&node);
  547. goto out;
  548. }
  549. i915_gem_object_pin_pages(obj);
  550. } else {
  551. node.start = i915_gem_obj_ggtt_offset(obj);
  552. node.allocated = false;
  553. ret = i915_gem_object_put_fence(obj);
  554. if (ret)
  555. goto out_unpin;
  556. }
  557. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  558. if (ret)
  559. goto out_unpin;
  560. user_data = u64_to_user_ptr(data_ptr);
  561. remain = size;
  562. offset = data_offset;
  563. mutex_unlock(&dev->struct_mutex);
  564. if (likely(!i915.prefault_disable)) {
  565. ret = fault_in_multipages_writeable(user_data, remain);
  566. if (ret) {
  567. mutex_lock(&dev->struct_mutex);
  568. goto out_unpin;
  569. }
  570. }
  571. while (remain > 0) {
  572. /* Operation in this page
  573. *
  574. * page_base = page offset within aperture
  575. * page_offset = offset within page
  576. * page_length = bytes to copy for this page
  577. */
  578. u32 page_base = node.start;
  579. unsigned page_offset = offset_in_page(offset);
  580. unsigned page_length = PAGE_SIZE - page_offset;
  581. page_length = remain < page_length ? remain : page_length;
  582. if (node.allocated) {
  583. wmb();
  584. ggtt->base.insert_page(&ggtt->base,
  585. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  586. node.start,
  587. I915_CACHE_NONE, 0);
  588. wmb();
  589. } else {
  590. page_base += offset & PAGE_MASK;
  591. }
  592. /* This is a slow read/write as it tries to read from
  593. * and write to user memory which may result into page
  594. * faults, and so we cannot perform this under struct_mutex.
  595. */
  596. if (slow_user_access(ggtt->mappable, page_base,
  597. page_offset, user_data,
  598. page_length, false)) {
  599. ret = -EFAULT;
  600. break;
  601. }
  602. remain -= page_length;
  603. user_data += page_length;
  604. offset += page_length;
  605. }
  606. mutex_lock(&dev->struct_mutex);
  607. if (ret == 0 && (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) {
  608. /* The user has modified the object whilst we tried
  609. * reading from it, and we now have no idea what domain
  610. * the pages should be in. As we have just been touching
  611. * them directly, flush everything back to the GTT
  612. * domain.
  613. */
  614. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  615. }
  616. out_unpin:
  617. if (node.allocated) {
  618. wmb();
  619. ggtt->base.clear_range(&ggtt->base,
  620. node.start, node.size,
  621. true);
  622. i915_gem_object_unpin_pages(obj);
  623. remove_mappable_node(&node);
  624. } else {
  625. i915_gem_object_ggtt_unpin(obj);
  626. }
  627. out:
  628. return ret;
  629. }
  630. static int
  631. i915_gem_shmem_pread(struct drm_device *dev,
  632. struct drm_i915_gem_object *obj,
  633. struct drm_i915_gem_pread *args,
  634. struct drm_file *file)
  635. {
  636. char __user *user_data;
  637. ssize_t remain;
  638. loff_t offset;
  639. int shmem_page_offset, page_length, ret = 0;
  640. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  641. int prefaulted = 0;
  642. int needs_clflush = 0;
  643. struct sg_page_iter sg_iter;
  644. if (!i915_gem_object_has_struct_page(obj))
  645. return -ENODEV;
  646. user_data = u64_to_user_ptr(args->data_ptr);
  647. remain = args->size;
  648. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  649. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  650. if (ret)
  651. return ret;
  652. offset = args->offset;
  653. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  654. offset >> PAGE_SHIFT) {
  655. struct page *page = sg_page_iter_page(&sg_iter);
  656. if (remain <= 0)
  657. break;
  658. /* Operation in this page
  659. *
  660. * shmem_page_offset = offset within page in shmem file
  661. * page_length = bytes to copy for this page
  662. */
  663. shmem_page_offset = offset_in_page(offset);
  664. page_length = remain;
  665. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  666. page_length = PAGE_SIZE - shmem_page_offset;
  667. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  668. (page_to_phys(page) & (1 << 17)) != 0;
  669. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  670. user_data, page_do_bit17_swizzling,
  671. needs_clflush);
  672. if (ret == 0)
  673. goto next_page;
  674. mutex_unlock(&dev->struct_mutex);
  675. if (likely(!i915.prefault_disable) && !prefaulted) {
  676. ret = fault_in_multipages_writeable(user_data, remain);
  677. /* Userspace is tricking us, but we've already clobbered
  678. * its pages with the prefault and promised to write the
  679. * data up to the first fault. Hence ignore any errors
  680. * and just continue. */
  681. (void)ret;
  682. prefaulted = 1;
  683. }
  684. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  685. user_data, page_do_bit17_swizzling,
  686. needs_clflush);
  687. mutex_lock(&dev->struct_mutex);
  688. if (ret)
  689. goto out;
  690. next_page:
  691. remain -= page_length;
  692. user_data += page_length;
  693. offset += page_length;
  694. }
  695. out:
  696. i915_gem_object_unpin_pages(obj);
  697. return ret;
  698. }
  699. /**
  700. * Reads data from the object referenced by handle.
  701. * @dev: drm device pointer
  702. * @data: ioctl data blob
  703. * @file: drm file pointer
  704. *
  705. * On error, the contents of *data are undefined.
  706. */
  707. int
  708. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  709. struct drm_file *file)
  710. {
  711. struct drm_i915_gem_pread *args = data;
  712. struct drm_i915_gem_object *obj;
  713. int ret = 0;
  714. if (args->size == 0)
  715. return 0;
  716. if (!access_ok(VERIFY_WRITE,
  717. u64_to_user_ptr(args->data_ptr),
  718. args->size))
  719. return -EFAULT;
  720. ret = i915_mutex_lock_interruptible(dev);
  721. if (ret)
  722. return ret;
  723. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  724. if (&obj->base == NULL) {
  725. ret = -ENOENT;
  726. goto unlock;
  727. }
  728. /* Bounds check source. */
  729. if (args->offset > obj->base.size ||
  730. args->size > obj->base.size - args->offset) {
  731. ret = -EINVAL;
  732. goto out;
  733. }
  734. trace_i915_gem_object_pread(obj, args->offset, args->size);
  735. ret = i915_gem_shmem_pread(dev, obj, args, file);
  736. /* pread for non shmem backed objects */
  737. if (ret == -EFAULT || ret == -ENODEV) {
  738. intel_runtime_pm_get(to_i915(dev));
  739. ret = i915_gem_gtt_pread(dev, obj, args->size,
  740. args->offset, args->data_ptr);
  741. intel_runtime_pm_put(to_i915(dev));
  742. }
  743. out:
  744. drm_gem_object_unreference(&obj->base);
  745. unlock:
  746. mutex_unlock(&dev->struct_mutex);
  747. return ret;
  748. }
  749. /* This is the fast write path which cannot handle
  750. * page faults in the source data
  751. */
  752. static inline int
  753. fast_user_write(struct io_mapping *mapping,
  754. loff_t page_base, int page_offset,
  755. char __user *user_data,
  756. int length)
  757. {
  758. void __iomem *vaddr_atomic;
  759. void *vaddr;
  760. unsigned long unwritten;
  761. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  762. /* We can use the cpu mem copy function because this is X86. */
  763. vaddr = (void __force*)vaddr_atomic + page_offset;
  764. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  765. user_data, length);
  766. io_mapping_unmap_atomic(vaddr_atomic);
  767. return unwritten;
  768. }
  769. /**
  770. * This is the fast pwrite path, where we copy the data directly from the
  771. * user into the GTT, uncached.
  772. * @dev: drm device pointer
  773. * @obj: i915 gem object
  774. * @args: pwrite arguments structure
  775. * @file: drm file pointer
  776. */
  777. static int
  778. i915_gem_gtt_pwrite_fast(struct drm_i915_private *i915,
  779. struct drm_i915_gem_object *obj,
  780. struct drm_i915_gem_pwrite *args,
  781. struct drm_file *file)
  782. {
  783. struct i915_ggtt *ggtt = &i915->ggtt;
  784. struct drm_device *dev = obj->base.dev;
  785. struct drm_mm_node node;
  786. uint64_t remain, offset;
  787. char __user *user_data;
  788. int ret;
  789. bool hit_slow_path = false;
  790. if (obj->tiling_mode != I915_TILING_NONE)
  791. return -EFAULT;
  792. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
  793. if (ret) {
  794. ret = insert_mappable_node(i915, &node, PAGE_SIZE);
  795. if (ret)
  796. goto out;
  797. ret = i915_gem_object_get_pages(obj);
  798. if (ret) {
  799. remove_mappable_node(&node);
  800. goto out;
  801. }
  802. i915_gem_object_pin_pages(obj);
  803. } else {
  804. node.start = i915_gem_obj_ggtt_offset(obj);
  805. node.allocated = false;
  806. ret = i915_gem_object_put_fence(obj);
  807. if (ret)
  808. goto out_unpin;
  809. }
  810. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  811. if (ret)
  812. goto out_unpin;
  813. intel_fb_obj_invalidate(obj, ORIGIN_GTT);
  814. obj->dirty = true;
  815. user_data = u64_to_user_ptr(args->data_ptr);
  816. offset = args->offset;
  817. remain = args->size;
  818. while (remain) {
  819. /* Operation in this page
  820. *
  821. * page_base = page offset within aperture
  822. * page_offset = offset within page
  823. * page_length = bytes to copy for this page
  824. */
  825. u32 page_base = node.start;
  826. unsigned page_offset = offset_in_page(offset);
  827. unsigned page_length = PAGE_SIZE - page_offset;
  828. page_length = remain < page_length ? remain : page_length;
  829. if (node.allocated) {
  830. wmb(); /* flush the write before we modify the GGTT */
  831. ggtt->base.insert_page(&ggtt->base,
  832. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  833. node.start, I915_CACHE_NONE, 0);
  834. wmb(); /* flush modifications to the GGTT (insert_page) */
  835. } else {
  836. page_base += offset & PAGE_MASK;
  837. }
  838. /* If we get a fault while copying data, then (presumably) our
  839. * source page isn't available. Return the error and we'll
  840. * retry in the slow path.
  841. * If the object is non-shmem backed, we retry again with the
  842. * path that handles page fault.
  843. */
  844. if (fast_user_write(ggtt->mappable, page_base,
  845. page_offset, user_data, page_length)) {
  846. hit_slow_path = true;
  847. mutex_unlock(&dev->struct_mutex);
  848. if (slow_user_access(ggtt->mappable,
  849. page_base,
  850. page_offset, user_data,
  851. page_length, true)) {
  852. ret = -EFAULT;
  853. mutex_lock(&dev->struct_mutex);
  854. goto out_flush;
  855. }
  856. mutex_lock(&dev->struct_mutex);
  857. }
  858. remain -= page_length;
  859. user_data += page_length;
  860. offset += page_length;
  861. }
  862. out_flush:
  863. if (hit_slow_path) {
  864. if (ret == 0 &&
  865. (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) {
  866. /* The user has modified the object whilst we tried
  867. * reading from it, and we now have no idea what domain
  868. * the pages should be in. As we have just been touching
  869. * them directly, flush everything back to the GTT
  870. * domain.
  871. */
  872. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  873. }
  874. }
  875. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  876. out_unpin:
  877. if (node.allocated) {
  878. wmb();
  879. ggtt->base.clear_range(&ggtt->base,
  880. node.start, node.size,
  881. true);
  882. i915_gem_object_unpin_pages(obj);
  883. remove_mappable_node(&node);
  884. } else {
  885. i915_gem_object_ggtt_unpin(obj);
  886. }
  887. out:
  888. return ret;
  889. }
  890. /* Per-page copy function for the shmem pwrite fastpath.
  891. * Flushes invalid cachelines before writing to the target if
  892. * needs_clflush_before is set and flushes out any written cachelines after
  893. * writing if needs_clflush is set. */
  894. static int
  895. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  896. char __user *user_data,
  897. bool page_do_bit17_swizzling,
  898. bool needs_clflush_before,
  899. bool needs_clflush_after)
  900. {
  901. char *vaddr;
  902. int ret;
  903. if (unlikely(page_do_bit17_swizzling))
  904. return -EINVAL;
  905. vaddr = kmap_atomic(page);
  906. if (needs_clflush_before)
  907. drm_clflush_virt_range(vaddr + shmem_page_offset,
  908. page_length);
  909. ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
  910. user_data, page_length);
  911. if (needs_clflush_after)
  912. drm_clflush_virt_range(vaddr + shmem_page_offset,
  913. page_length);
  914. kunmap_atomic(vaddr);
  915. return ret ? -EFAULT : 0;
  916. }
  917. /* Only difference to the fast-path function is that this can handle bit17
  918. * and uses non-atomic copy and kmap functions. */
  919. static int
  920. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  921. char __user *user_data,
  922. bool page_do_bit17_swizzling,
  923. bool needs_clflush_before,
  924. bool needs_clflush_after)
  925. {
  926. char *vaddr;
  927. int ret;
  928. vaddr = kmap(page);
  929. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  930. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  931. page_length,
  932. page_do_bit17_swizzling);
  933. if (page_do_bit17_swizzling)
  934. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  935. user_data,
  936. page_length);
  937. else
  938. ret = __copy_from_user(vaddr + shmem_page_offset,
  939. user_data,
  940. page_length);
  941. if (needs_clflush_after)
  942. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  943. page_length,
  944. page_do_bit17_swizzling);
  945. kunmap(page);
  946. return ret ? -EFAULT : 0;
  947. }
  948. static int
  949. i915_gem_shmem_pwrite(struct drm_device *dev,
  950. struct drm_i915_gem_object *obj,
  951. struct drm_i915_gem_pwrite *args,
  952. struct drm_file *file)
  953. {
  954. ssize_t remain;
  955. loff_t offset;
  956. char __user *user_data;
  957. int shmem_page_offset, page_length, ret = 0;
  958. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  959. int hit_slowpath = 0;
  960. int needs_clflush_after = 0;
  961. int needs_clflush_before = 0;
  962. struct sg_page_iter sg_iter;
  963. user_data = u64_to_user_ptr(args->data_ptr);
  964. remain = args->size;
  965. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  966. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  967. /* If we're not in the cpu write domain, set ourself into the gtt
  968. * write domain and manually flush cachelines (if required). This
  969. * optimizes for the case when the gpu will use the data
  970. * right away and we therefore have to clflush anyway. */
  971. needs_clflush_after = cpu_write_needs_clflush(obj);
  972. ret = i915_gem_object_wait_rendering(obj, false);
  973. if (ret)
  974. return ret;
  975. }
  976. /* Same trick applies to invalidate partially written cachelines read
  977. * before writing. */
  978. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  979. needs_clflush_before =
  980. !cpu_cache_is_coherent(dev, obj->cache_level);
  981. ret = i915_gem_object_get_pages(obj);
  982. if (ret)
  983. return ret;
  984. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  985. i915_gem_object_pin_pages(obj);
  986. offset = args->offset;
  987. obj->dirty = 1;
  988. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  989. offset >> PAGE_SHIFT) {
  990. struct page *page = sg_page_iter_page(&sg_iter);
  991. int partial_cacheline_write;
  992. if (remain <= 0)
  993. break;
  994. /* Operation in this page
  995. *
  996. * shmem_page_offset = offset within page in shmem file
  997. * page_length = bytes to copy for this page
  998. */
  999. shmem_page_offset = offset_in_page(offset);
  1000. page_length = remain;
  1001. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  1002. page_length = PAGE_SIZE - shmem_page_offset;
  1003. /* If we don't overwrite a cacheline completely we need to be
  1004. * careful to have up-to-date data by first clflushing. Don't
  1005. * overcomplicate things and flush the entire patch. */
  1006. partial_cacheline_write = needs_clflush_before &&
  1007. ((shmem_page_offset | page_length)
  1008. & (boot_cpu_data.x86_clflush_size - 1));
  1009. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  1010. (page_to_phys(page) & (1 << 17)) != 0;
  1011. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  1012. user_data, page_do_bit17_swizzling,
  1013. partial_cacheline_write,
  1014. needs_clflush_after);
  1015. if (ret == 0)
  1016. goto next_page;
  1017. hit_slowpath = 1;
  1018. mutex_unlock(&dev->struct_mutex);
  1019. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  1020. user_data, page_do_bit17_swizzling,
  1021. partial_cacheline_write,
  1022. needs_clflush_after);
  1023. mutex_lock(&dev->struct_mutex);
  1024. if (ret)
  1025. goto out;
  1026. next_page:
  1027. remain -= page_length;
  1028. user_data += page_length;
  1029. offset += page_length;
  1030. }
  1031. out:
  1032. i915_gem_object_unpin_pages(obj);
  1033. if (hit_slowpath) {
  1034. /*
  1035. * Fixup: Flush cpu caches in case we didn't flush the dirty
  1036. * cachelines in-line while writing and the object moved
  1037. * out of the cpu write domain while we've dropped the lock.
  1038. */
  1039. if (!needs_clflush_after &&
  1040. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  1041. if (i915_gem_clflush_object(obj, obj->pin_display))
  1042. needs_clflush_after = true;
  1043. }
  1044. }
  1045. if (needs_clflush_after)
  1046. i915_gem_chipset_flush(to_i915(dev));
  1047. else
  1048. obj->cache_dirty = true;
  1049. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  1050. return ret;
  1051. }
  1052. /**
  1053. * Writes data to the object referenced by handle.
  1054. * @dev: drm device
  1055. * @data: ioctl data blob
  1056. * @file: drm file
  1057. *
  1058. * On error, the contents of the buffer that were to be modified are undefined.
  1059. */
  1060. int
  1061. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  1062. struct drm_file *file)
  1063. {
  1064. struct drm_i915_private *dev_priv = to_i915(dev);
  1065. struct drm_i915_gem_pwrite *args = data;
  1066. struct drm_i915_gem_object *obj;
  1067. int ret;
  1068. if (args->size == 0)
  1069. return 0;
  1070. if (!access_ok(VERIFY_READ,
  1071. u64_to_user_ptr(args->data_ptr),
  1072. args->size))
  1073. return -EFAULT;
  1074. if (likely(!i915.prefault_disable)) {
  1075. ret = fault_in_multipages_readable(u64_to_user_ptr(args->data_ptr),
  1076. args->size);
  1077. if (ret)
  1078. return -EFAULT;
  1079. }
  1080. intel_runtime_pm_get(dev_priv);
  1081. ret = i915_mutex_lock_interruptible(dev);
  1082. if (ret)
  1083. goto put_rpm;
  1084. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  1085. if (&obj->base == NULL) {
  1086. ret = -ENOENT;
  1087. goto unlock;
  1088. }
  1089. /* Bounds check destination. */
  1090. if (args->offset > obj->base.size ||
  1091. args->size > obj->base.size - args->offset) {
  1092. ret = -EINVAL;
  1093. goto out;
  1094. }
  1095. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  1096. ret = -EFAULT;
  1097. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  1098. * it would end up going through the fenced access, and we'll get
  1099. * different detiling behavior between reading and writing.
  1100. * pread/pwrite currently are reading and writing from the CPU
  1101. * perspective, requiring manual detiling by the client.
  1102. */
  1103. if (!i915_gem_object_has_struct_page(obj) ||
  1104. cpu_write_needs_clflush(obj)) {
  1105. ret = i915_gem_gtt_pwrite_fast(dev_priv, obj, args, file);
  1106. /* Note that the gtt paths might fail with non-page-backed user
  1107. * pointers (e.g. gtt mappings when moving data between
  1108. * textures). Fallback to the shmem path in that case. */
  1109. }
  1110. if (ret == -EFAULT || ret == -ENOSPC) {
  1111. if (obj->phys_handle)
  1112. ret = i915_gem_phys_pwrite(obj, args, file);
  1113. else if (i915_gem_object_has_struct_page(obj))
  1114. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  1115. else
  1116. ret = -ENODEV;
  1117. }
  1118. out:
  1119. drm_gem_object_unreference(&obj->base);
  1120. unlock:
  1121. mutex_unlock(&dev->struct_mutex);
  1122. put_rpm:
  1123. intel_runtime_pm_put(dev_priv);
  1124. return ret;
  1125. }
  1126. static int
  1127. i915_gem_check_wedge(unsigned reset_counter, bool interruptible)
  1128. {
  1129. if (__i915_terminally_wedged(reset_counter))
  1130. return -EIO;
  1131. if (__i915_reset_in_progress(reset_counter)) {
  1132. /* Non-interruptible callers can't handle -EAGAIN, hence return
  1133. * -EIO unconditionally for these. */
  1134. if (!interruptible)
  1135. return -EIO;
  1136. return -EAGAIN;
  1137. }
  1138. return 0;
  1139. }
  1140. static unsigned long local_clock_us(unsigned *cpu)
  1141. {
  1142. unsigned long t;
  1143. /* Cheaply and approximately convert from nanoseconds to microseconds.
  1144. * The result and subsequent calculations are also defined in the same
  1145. * approximate microseconds units. The principal source of timing
  1146. * error here is from the simple truncation.
  1147. *
  1148. * Note that local_clock() is only defined wrt to the current CPU;
  1149. * the comparisons are no longer valid if we switch CPUs. Instead of
  1150. * blocking preemption for the entire busywait, we can detect the CPU
  1151. * switch and use that as indicator of system load and a reason to
  1152. * stop busywaiting, see busywait_stop().
  1153. */
  1154. *cpu = get_cpu();
  1155. t = local_clock() >> 10;
  1156. put_cpu();
  1157. return t;
  1158. }
  1159. static bool busywait_stop(unsigned long timeout, unsigned cpu)
  1160. {
  1161. unsigned this_cpu;
  1162. if (time_after(local_clock_us(&this_cpu), timeout))
  1163. return true;
  1164. return this_cpu != cpu;
  1165. }
  1166. bool __i915_spin_request(const struct drm_i915_gem_request *req,
  1167. int state, unsigned long timeout_us)
  1168. {
  1169. unsigned cpu;
  1170. /* When waiting for high frequency requests, e.g. during synchronous
  1171. * rendering split between the CPU and GPU, the finite amount of time
  1172. * required to set up the irq and wait upon it limits the response
  1173. * rate. By busywaiting on the request completion for a short while we
  1174. * can service the high frequency waits as quick as possible. However,
  1175. * if it is a slow request, we want to sleep as quickly as possible.
  1176. * The tradeoff between waiting and sleeping is roughly the time it
  1177. * takes to sleep on a request, on the order of a microsecond.
  1178. */
  1179. timeout_us += local_clock_us(&cpu);
  1180. do {
  1181. if (i915_gem_request_completed(req))
  1182. return true;
  1183. if (signal_pending_state(state, current))
  1184. break;
  1185. if (busywait_stop(timeout_us, cpu))
  1186. break;
  1187. cpu_relax_lowlatency();
  1188. } while (!need_resched());
  1189. return false;
  1190. }
  1191. /**
  1192. * __i915_wait_request - wait until execution of request has finished
  1193. * @req: duh!
  1194. * @interruptible: do an interruptible wait (normally yes)
  1195. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  1196. * @rps: RPS client
  1197. *
  1198. * Note: It is of utmost importance that the passed in seqno and reset_counter
  1199. * values have been read by the caller in an smp safe manner. Where read-side
  1200. * locks are involved, it is sufficient to read the reset_counter before
  1201. * unlocking the lock that protects the seqno. For lockless tricks, the
  1202. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  1203. * inserted.
  1204. *
  1205. * Returns 0 if the request was found within the alloted time. Else returns the
  1206. * errno with remaining time filled in timeout argument.
  1207. */
  1208. int __i915_wait_request(struct drm_i915_gem_request *req,
  1209. bool interruptible,
  1210. s64 *timeout,
  1211. struct intel_rps_client *rps)
  1212. {
  1213. int state = interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  1214. DEFINE_WAIT(reset);
  1215. struct intel_wait wait;
  1216. unsigned long timeout_remain;
  1217. s64 before = 0; /* Only to silence a compiler warning. */
  1218. int ret = 0;
  1219. might_sleep();
  1220. if (list_empty(&req->list))
  1221. return 0;
  1222. if (i915_gem_request_completed(req))
  1223. return 0;
  1224. timeout_remain = MAX_SCHEDULE_TIMEOUT;
  1225. if (timeout) {
  1226. if (WARN_ON(*timeout < 0))
  1227. return -EINVAL;
  1228. if (*timeout == 0)
  1229. return -ETIME;
  1230. timeout_remain = nsecs_to_jiffies_timeout(*timeout);
  1231. /*
  1232. * Record current time in case interrupted by signal, or wedged.
  1233. */
  1234. before = ktime_get_raw_ns();
  1235. }
  1236. trace_i915_gem_request_wait_begin(req);
  1237. /* This client is about to stall waiting for the GPU. In many cases
  1238. * this is undesirable and limits the throughput of the system, as
  1239. * many clients cannot continue processing user input/output whilst
  1240. * blocked. RPS autotuning may take tens of milliseconds to respond
  1241. * to the GPU load and thus incurs additional latency for the client.
  1242. * We can circumvent that by promoting the GPU frequency to maximum
  1243. * before we wait. This makes the GPU throttle up much more quickly
  1244. * (good for benchmarks and user experience, e.g. window animations),
  1245. * but at a cost of spending more power processing the workload
  1246. * (bad for battery). Not all clients even want their results
  1247. * immediately and for them we should just let the GPU select its own
  1248. * frequency to maximise efficiency. To prevent a single client from
  1249. * forcing the clocks too high for the whole system, we only allow
  1250. * each client to waitboost once in a busy period.
  1251. */
  1252. if (INTEL_INFO(req->i915)->gen >= 6)
  1253. gen6_rps_boost(req->i915, rps, req->emitted_jiffies);
  1254. /* Optimistic spin for the next ~jiffie before touching IRQs */
  1255. if (i915_spin_request(req, state, 5))
  1256. goto complete;
  1257. set_current_state(state);
  1258. add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
  1259. intel_wait_init(&wait, req->seqno);
  1260. if (intel_engine_add_wait(req->engine, &wait))
  1261. /* In order to check that we haven't missed the interrupt
  1262. * as we enabled it, we need to kick ourselves to do a
  1263. * coherent check on the seqno before we sleep.
  1264. */
  1265. goto wakeup;
  1266. for (;;) {
  1267. if (signal_pending_state(state, current)) {
  1268. ret = -ERESTARTSYS;
  1269. break;
  1270. }
  1271. timeout_remain = io_schedule_timeout(timeout_remain);
  1272. if (timeout_remain == 0) {
  1273. ret = -ETIME;
  1274. break;
  1275. }
  1276. if (intel_wait_complete(&wait))
  1277. break;
  1278. set_current_state(state);
  1279. wakeup:
  1280. /* Carefully check if the request is complete, giving time
  1281. * for the seqno to be visible following the interrupt.
  1282. * We also have to check in case we are kicked by the GPU
  1283. * reset in order to drop the struct_mutex.
  1284. */
  1285. if (__i915_request_irq_complete(req))
  1286. break;
  1287. /* Only spin if we know the GPU is processing this request */
  1288. if (i915_spin_request(req, state, 2))
  1289. break;
  1290. }
  1291. remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
  1292. intel_engine_remove_wait(req->engine, &wait);
  1293. __set_current_state(TASK_RUNNING);
  1294. complete:
  1295. trace_i915_gem_request_wait_end(req);
  1296. if (timeout) {
  1297. s64 tres = *timeout - (ktime_get_raw_ns() - before);
  1298. *timeout = tres < 0 ? 0 : tres;
  1299. /*
  1300. * Apparently ktime isn't accurate enough and occasionally has a
  1301. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  1302. * things up to make the test happy. We allow up to 1 jiffy.
  1303. *
  1304. * This is a regrssion from the timespec->ktime conversion.
  1305. */
  1306. if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
  1307. *timeout = 0;
  1308. }
  1309. if (rps && req->seqno == req->engine->last_submitted_seqno) {
  1310. /* The GPU is now idle and this client has stalled.
  1311. * Since no other client has submitted a request in the
  1312. * meantime, assume that this client is the only one
  1313. * supplying work to the GPU but is unable to keep that
  1314. * work supplied because it is waiting. Since the GPU is
  1315. * then never kept fully busy, RPS autoclocking will
  1316. * keep the clocks relatively low, causing further delays.
  1317. * Compensate by giving the synchronous client credit for
  1318. * a waitboost next time.
  1319. */
  1320. spin_lock(&req->i915->rps.client_lock);
  1321. list_del_init(&rps->link);
  1322. spin_unlock(&req->i915->rps.client_lock);
  1323. }
  1324. return ret;
  1325. }
  1326. int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
  1327. struct drm_file *file)
  1328. {
  1329. struct drm_i915_file_private *file_priv;
  1330. WARN_ON(!req || !file || req->file_priv);
  1331. if (!req || !file)
  1332. return -EINVAL;
  1333. if (req->file_priv)
  1334. return -EINVAL;
  1335. file_priv = file->driver_priv;
  1336. spin_lock(&file_priv->mm.lock);
  1337. req->file_priv = file_priv;
  1338. list_add_tail(&req->client_list, &file_priv->mm.request_list);
  1339. spin_unlock(&file_priv->mm.lock);
  1340. req->pid = get_pid(task_pid(current));
  1341. return 0;
  1342. }
  1343. static inline void
  1344. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1345. {
  1346. struct drm_i915_file_private *file_priv = request->file_priv;
  1347. if (!file_priv)
  1348. return;
  1349. spin_lock(&file_priv->mm.lock);
  1350. list_del(&request->client_list);
  1351. request->file_priv = NULL;
  1352. spin_unlock(&file_priv->mm.lock);
  1353. put_pid(request->pid);
  1354. request->pid = NULL;
  1355. }
  1356. static void i915_gem_request_retire(struct drm_i915_gem_request *request)
  1357. {
  1358. trace_i915_gem_request_retire(request);
  1359. /* We know the GPU must have read the request to have
  1360. * sent us the seqno + interrupt, so use the position
  1361. * of tail of the request to update the last known position
  1362. * of the GPU head.
  1363. *
  1364. * Note this requires that we are always called in request
  1365. * completion order.
  1366. */
  1367. request->ringbuf->last_retired_head = request->postfix;
  1368. list_del_init(&request->list);
  1369. i915_gem_request_remove_from_client(request);
  1370. if (request->previous_context) {
  1371. if (i915.enable_execlists)
  1372. intel_lr_context_unpin(request->previous_context,
  1373. request->engine);
  1374. }
  1375. i915_gem_context_unreference(request->ctx);
  1376. i915_gem_request_unreference(request);
  1377. }
  1378. static void
  1379. __i915_gem_request_retire__upto(struct drm_i915_gem_request *req)
  1380. {
  1381. struct intel_engine_cs *engine = req->engine;
  1382. struct drm_i915_gem_request *tmp;
  1383. lockdep_assert_held(&engine->i915->drm.struct_mutex);
  1384. if (list_empty(&req->list))
  1385. return;
  1386. do {
  1387. tmp = list_first_entry(&engine->request_list,
  1388. typeof(*tmp), list);
  1389. i915_gem_request_retire(tmp);
  1390. } while (tmp != req);
  1391. WARN_ON(i915_verify_lists(engine->dev));
  1392. }
  1393. /**
  1394. * Waits for a request to be signaled, and cleans up the
  1395. * request and object lists appropriately for that event.
  1396. * @req: request to wait on
  1397. */
  1398. int
  1399. i915_wait_request(struct drm_i915_gem_request *req)
  1400. {
  1401. struct drm_i915_private *dev_priv = req->i915;
  1402. bool interruptible;
  1403. int ret;
  1404. interruptible = dev_priv->mm.interruptible;
  1405. BUG_ON(!mutex_is_locked(&dev_priv->drm.struct_mutex));
  1406. ret = __i915_wait_request(req, interruptible, NULL, NULL);
  1407. if (ret)
  1408. return ret;
  1409. /* If the GPU hung, we want to keep the requests to find the guilty. */
  1410. if (!i915_reset_in_progress(&dev_priv->gpu_error))
  1411. __i915_gem_request_retire__upto(req);
  1412. return 0;
  1413. }
  1414. /**
  1415. * Ensures that all rendering to the object has completed and the object is
  1416. * safe to unbind from the GTT or access from the CPU.
  1417. * @obj: i915 gem object
  1418. * @readonly: waiting for read access or write
  1419. */
  1420. int
  1421. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  1422. bool readonly)
  1423. {
  1424. int ret, i;
  1425. if (!obj->active)
  1426. return 0;
  1427. if (readonly) {
  1428. if (obj->last_write_req != NULL) {
  1429. ret = i915_wait_request(obj->last_write_req);
  1430. if (ret)
  1431. return ret;
  1432. i = obj->last_write_req->engine->id;
  1433. if (obj->last_read_req[i] == obj->last_write_req)
  1434. i915_gem_object_retire__read(obj, i);
  1435. else
  1436. i915_gem_object_retire__write(obj);
  1437. }
  1438. } else {
  1439. for (i = 0; i < I915_NUM_ENGINES; i++) {
  1440. if (obj->last_read_req[i] == NULL)
  1441. continue;
  1442. ret = i915_wait_request(obj->last_read_req[i]);
  1443. if (ret)
  1444. return ret;
  1445. i915_gem_object_retire__read(obj, i);
  1446. }
  1447. GEM_BUG_ON(obj->active);
  1448. }
  1449. return 0;
  1450. }
  1451. static void
  1452. i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
  1453. struct drm_i915_gem_request *req)
  1454. {
  1455. int ring = req->engine->id;
  1456. if (obj->last_read_req[ring] == req)
  1457. i915_gem_object_retire__read(obj, ring);
  1458. else if (obj->last_write_req == req)
  1459. i915_gem_object_retire__write(obj);
  1460. if (!i915_reset_in_progress(&req->i915->gpu_error))
  1461. __i915_gem_request_retire__upto(req);
  1462. }
  1463. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  1464. * as the object state may change during this call.
  1465. */
  1466. static __must_check int
  1467. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  1468. struct intel_rps_client *rps,
  1469. bool readonly)
  1470. {
  1471. struct drm_device *dev = obj->base.dev;
  1472. struct drm_i915_private *dev_priv = to_i915(dev);
  1473. struct drm_i915_gem_request *requests[I915_NUM_ENGINES];
  1474. int ret, i, n = 0;
  1475. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1476. BUG_ON(!dev_priv->mm.interruptible);
  1477. if (!obj->active)
  1478. return 0;
  1479. if (readonly) {
  1480. struct drm_i915_gem_request *req;
  1481. req = obj->last_write_req;
  1482. if (req == NULL)
  1483. return 0;
  1484. requests[n++] = i915_gem_request_reference(req);
  1485. } else {
  1486. for (i = 0; i < I915_NUM_ENGINES; i++) {
  1487. struct drm_i915_gem_request *req;
  1488. req = obj->last_read_req[i];
  1489. if (req == NULL)
  1490. continue;
  1491. requests[n++] = i915_gem_request_reference(req);
  1492. }
  1493. }
  1494. mutex_unlock(&dev->struct_mutex);
  1495. ret = 0;
  1496. for (i = 0; ret == 0 && i < n; i++)
  1497. ret = __i915_wait_request(requests[i], true, NULL, rps);
  1498. mutex_lock(&dev->struct_mutex);
  1499. for (i = 0; i < n; i++) {
  1500. if (ret == 0)
  1501. i915_gem_object_retire_request(obj, requests[i]);
  1502. i915_gem_request_unreference(requests[i]);
  1503. }
  1504. return ret;
  1505. }
  1506. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  1507. {
  1508. struct drm_i915_file_private *fpriv = file->driver_priv;
  1509. return &fpriv->rps;
  1510. }
  1511. static enum fb_op_origin
  1512. write_origin(struct drm_i915_gem_object *obj, unsigned domain)
  1513. {
  1514. return domain == I915_GEM_DOMAIN_GTT && !obj->has_wc_mmap ?
  1515. ORIGIN_GTT : ORIGIN_CPU;
  1516. }
  1517. /**
  1518. * Called when user space prepares to use an object with the CPU, either
  1519. * through the mmap ioctl's mapping or a GTT mapping.
  1520. * @dev: drm device
  1521. * @data: ioctl data blob
  1522. * @file: drm file
  1523. */
  1524. int
  1525. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1526. struct drm_file *file)
  1527. {
  1528. struct drm_i915_gem_set_domain *args = data;
  1529. struct drm_i915_gem_object *obj;
  1530. uint32_t read_domains = args->read_domains;
  1531. uint32_t write_domain = args->write_domain;
  1532. int ret;
  1533. /* Only handle setting domains to types used by the CPU. */
  1534. if (write_domain & I915_GEM_GPU_DOMAINS)
  1535. return -EINVAL;
  1536. if (read_domains & I915_GEM_GPU_DOMAINS)
  1537. return -EINVAL;
  1538. /* Having something in the write domain implies it's in the read
  1539. * domain, and only that read domain. Enforce that in the request.
  1540. */
  1541. if (write_domain != 0 && read_domains != write_domain)
  1542. return -EINVAL;
  1543. ret = i915_mutex_lock_interruptible(dev);
  1544. if (ret)
  1545. return ret;
  1546. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  1547. if (&obj->base == NULL) {
  1548. ret = -ENOENT;
  1549. goto unlock;
  1550. }
  1551. /* Try to flush the object off the GPU without holding the lock.
  1552. * We will repeat the flush holding the lock in the normal manner
  1553. * to catch cases where we are gazumped.
  1554. */
  1555. ret = i915_gem_object_wait_rendering__nonblocking(obj,
  1556. to_rps_client(file),
  1557. !write_domain);
  1558. if (ret)
  1559. goto unref;
  1560. if (read_domains & I915_GEM_DOMAIN_GTT)
  1561. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1562. else
  1563. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1564. if (write_domain != 0)
  1565. intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
  1566. unref:
  1567. drm_gem_object_unreference(&obj->base);
  1568. unlock:
  1569. mutex_unlock(&dev->struct_mutex);
  1570. return ret;
  1571. }
  1572. /**
  1573. * Called when user space has done writes to this buffer
  1574. * @dev: drm device
  1575. * @data: ioctl data blob
  1576. * @file: drm file
  1577. */
  1578. int
  1579. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1580. struct drm_file *file)
  1581. {
  1582. struct drm_i915_gem_sw_finish *args = data;
  1583. struct drm_i915_gem_object *obj;
  1584. int ret = 0;
  1585. ret = i915_mutex_lock_interruptible(dev);
  1586. if (ret)
  1587. return ret;
  1588. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  1589. if (&obj->base == NULL) {
  1590. ret = -ENOENT;
  1591. goto unlock;
  1592. }
  1593. /* Pinned buffers may be scanout, so flush the cache */
  1594. if (obj->pin_display)
  1595. i915_gem_object_flush_cpu_write_domain(obj);
  1596. drm_gem_object_unreference(&obj->base);
  1597. unlock:
  1598. mutex_unlock(&dev->struct_mutex);
  1599. return ret;
  1600. }
  1601. /**
  1602. * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
  1603. * it is mapped to.
  1604. * @dev: drm device
  1605. * @data: ioctl data blob
  1606. * @file: drm file
  1607. *
  1608. * While the mapping holds a reference on the contents of the object, it doesn't
  1609. * imply a ref on the object itself.
  1610. *
  1611. * IMPORTANT:
  1612. *
  1613. * DRM driver writers who look a this function as an example for how to do GEM
  1614. * mmap support, please don't implement mmap support like here. The modern way
  1615. * to implement DRM mmap support is with an mmap offset ioctl (like
  1616. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1617. * That way debug tooling like valgrind will understand what's going on, hiding
  1618. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1619. * does cpu mmaps this way because we didn't know better.
  1620. */
  1621. int
  1622. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1623. struct drm_file *file)
  1624. {
  1625. struct drm_i915_gem_mmap *args = data;
  1626. struct drm_gem_object *obj;
  1627. unsigned long addr;
  1628. if (args->flags & ~(I915_MMAP_WC))
  1629. return -EINVAL;
  1630. if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
  1631. return -ENODEV;
  1632. obj = drm_gem_object_lookup(file, args->handle);
  1633. if (obj == NULL)
  1634. return -ENOENT;
  1635. /* prime objects have no backing filp to GEM mmap
  1636. * pages from.
  1637. */
  1638. if (!obj->filp) {
  1639. drm_gem_object_unreference_unlocked(obj);
  1640. return -EINVAL;
  1641. }
  1642. addr = vm_mmap(obj->filp, 0, args->size,
  1643. PROT_READ | PROT_WRITE, MAP_SHARED,
  1644. args->offset);
  1645. if (args->flags & I915_MMAP_WC) {
  1646. struct mm_struct *mm = current->mm;
  1647. struct vm_area_struct *vma;
  1648. if (down_write_killable(&mm->mmap_sem)) {
  1649. drm_gem_object_unreference_unlocked(obj);
  1650. return -EINTR;
  1651. }
  1652. vma = find_vma(mm, addr);
  1653. if (vma)
  1654. vma->vm_page_prot =
  1655. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1656. else
  1657. addr = -ENOMEM;
  1658. up_write(&mm->mmap_sem);
  1659. /* This may race, but that's ok, it only gets set */
  1660. WRITE_ONCE(to_intel_bo(obj)->has_wc_mmap, true);
  1661. }
  1662. drm_gem_object_unreference_unlocked(obj);
  1663. if (IS_ERR((void *)addr))
  1664. return addr;
  1665. args->addr_ptr = (uint64_t) addr;
  1666. return 0;
  1667. }
  1668. /**
  1669. * i915_gem_fault - fault a page into the GTT
  1670. * @vma: VMA in question
  1671. * @vmf: fault info
  1672. *
  1673. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1674. * from userspace. The fault handler takes care of binding the object to
  1675. * the GTT (if needed), allocating and programming a fence register (again,
  1676. * only if needed based on whether the old reg is still valid or the object
  1677. * is tiled) and inserting a new PTE into the faulting process.
  1678. *
  1679. * Note that the faulting process may involve evicting existing objects
  1680. * from the GTT and/or fence registers to make room. So performance may
  1681. * suffer if the GTT working set is large or there are few fence registers
  1682. * left.
  1683. */
  1684. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1685. {
  1686. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1687. struct drm_device *dev = obj->base.dev;
  1688. struct drm_i915_private *dev_priv = to_i915(dev);
  1689. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1690. struct i915_ggtt_view view = i915_ggtt_view_normal;
  1691. pgoff_t page_offset;
  1692. unsigned long pfn;
  1693. int ret = 0;
  1694. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1695. intel_runtime_pm_get(dev_priv);
  1696. /* We don't use vmf->pgoff since that has the fake offset */
  1697. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1698. PAGE_SHIFT;
  1699. ret = i915_mutex_lock_interruptible(dev);
  1700. if (ret)
  1701. goto out;
  1702. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1703. /* Try to flush the object off the GPU first without holding the lock.
  1704. * Upon reacquiring the lock, we will perform our sanity checks and then
  1705. * repeat the flush holding the lock in the normal manner to catch cases
  1706. * where we are gazumped.
  1707. */
  1708. ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
  1709. if (ret)
  1710. goto unlock;
  1711. /* Access to snoopable pages through the GTT is incoherent. */
  1712. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1713. ret = -EFAULT;
  1714. goto unlock;
  1715. }
  1716. /* Use a partial view if the object is bigger than the aperture. */
  1717. if (obj->base.size >= ggtt->mappable_end &&
  1718. obj->tiling_mode == I915_TILING_NONE) {
  1719. static const unsigned int chunk_size = 256; // 1 MiB
  1720. memset(&view, 0, sizeof(view));
  1721. view.type = I915_GGTT_VIEW_PARTIAL;
  1722. view.params.partial.offset = rounddown(page_offset, chunk_size);
  1723. view.params.partial.size =
  1724. min_t(unsigned int,
  1725. chunk_size,
  1726. (vma->vm_end - vma->vm_start)/PAGE_SIZE -
  1727. view.params.partial.offset);
  1728. }
  1729. /* Now pin it into the GTT if needed */
  1730. ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
  1731. if (ret)
  1732. goto unlock;
  1733. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1734. if (ret)
  1735. goto unpin;
  1736. ret = i915_gem_object_get_fence(obj);
  1737. if (ret)
  1738. goto unpin;
  1739. /* Finally, remap it using the new GTT offset */
  1740. pfn = ggtt->mappable_base +
  1741. i915_gem_obj_ggtt_offset_view(obj, &view);
  1742. pfn >>= PAGE_SHIFT;
  1743. if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
  1744. /* Overriding existing pages in partial view does not cause
  1745. * us any trouble as TLBs are still valid because the fault
  1746. * is due to userspace losing part of the mapping or never
  1747. * having accessed it before (at this partials' range).
  1748. */
  1749. unsigned long base = vma->vm_start +
  1750. (view.params.partial.offset << PAGE_SHIFT);
  1751. unsigned int i;
  1752. for (i = 0; i < view.params.partial.size; i++) {
  1753. ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
  1754. if (ret)
  1755. break;
  1756. }
  1757. obj->fault_mappable = true;
  1758. } else {
  1759. if (!obj->fault_mappable) {
  1760. unsigned long size = min_t(unsigned long,
  1761. vma->vm_end - vma->vm_start,
  1762. obj->base.size);
  1763. int i;
  1764. for (i = 0; i < size >> PAGE_SHIFT; i++) {
  1765. ret = vm_insert_pfn(vma,
  1766. (unsigned long)vma->vm_start + i * PAGE_SIZE,
  1767. pfn + i);
  1768. if (ret)
  1769. break;
  1770. }
  1771. obj->fault_mappable = true;
  1772. } else
  1773. ret = vm_insert_pfn(vma,
  1774. (unsigned long)vmf->virtual_address,
  1775. pfn + page_offset);
  1776. }
  1777. unpin:
  1778. i915_gem_object_ggtt_unpin_view(obj, &view);
  1779. unlock:
  1780. mutex_unlock(&dev->struct_mutex);
  1781. out:
  1782. switch (ret) {
  1783. case -EIO:
  1784. /*
  1785. * We eat errors when the gpu is terminally wedged to avoid
  1786. * userspace unduly crashing (gl has no provisions for mmaps to
  1787. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1788. * and so needs to be reported.
  1789. */
  1790. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1791. ret = VM_FAULT_SIGBUS;
  1792. break;
  1793. }
  1794. case -EAGAIN:
  1795. /*
  1796. * EAGAIN means the gpu is hung and we'll wait for the error
  1797. * handler to reset everything when re-faulting in
  1798. * i915_mutex_lock_interruptible.
  1799. */
  1800. case 0:
  1801. case -ERESTARTSYS:
  1802. case -EINTR:
  1803. case -EBUSY:
  1804. /*
  1805. * EBUSY is ok: this just means that another thread
  1806. * already did the job.
  1807. */
  1808. ret = VM_FAULT_NOPAGE;
  1809. break;
  1810. case -ENOMEM:
  1811. ret = VM_FAULT_OOM;
  1812. break;
  1813. case -ENOSPC:
  1814. case -EFAULT:
  1815. ret = VM_FAULT_SIGBUS;
  1816. break;
  1817. default:
  1818. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1819. ret = VM_FAULT_SIGBUS;
  1820. break;
  1821. }
  1822. intel_runtime_pm_put(dev_priv);
  1823. return ret;
  1824. }
  1825. /**
  1826. * i915_gem_release_mmap - remove physical page mappings
  1827. * @obj: obj in question
  1828. *
  1829. * Preserve the reservation of the mmapping with the DRM core code, but
  1830. * relinquish ownership of the pages back to the system.
  1831. *
  1832. * It is vital that we remove the page mapping if we have mapped a tiled
  1833. * object through the GTT and then lose the fence register due to
  1834. * resource pressure. Similarly if the object has been moved out of the
  1835. * aperture, than pages mapped into userspace must be revoked. Removing the
  1836. * mapping will then trigger a page fault on the next user access, allowing
  1837. * fixup by i915_gem_fault().
  1838. */
  1839. void
  1840. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1841. {
  1842. /* Serialisation between user GTT access and our code depends upon
  1843. * revoking the CPU's PTE whilst the mutex is held. The next user
  1844. * pagefault then has to wait until we release the mutex.
  1845. */
  1846. lockdep_assert_held(&obj->base.dev->struct_mutex);
  1847. if (!obj->fault_mappable)
  1848. return;
  1849. drm_vma_node_unmap(&obj->base.vma_node,
  1850. obj->base.dev->anon_inode->i_mapping);
  1851. /* Ensure that the CPU's PTE are revoked and there are not outstanding
  1852. * memory transactions from userspace before we return. The TLB
  1853. * flushing implied above by changing the PTE above *should* be
  1854. * sufficient, an extra barrier here just provides us with a bit
  1855. * of paranoid documentation about our requirement to serialise
  1856. * memory writes before touching registers / GSM.
  1857. */
  1858. wmb();
  1859. obj->fault_mappable = false;
  1860. }
  1861. void
  1862. i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
  1863. {
  1864. struct drm_i915_gem_object *obj;
  1865. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  1866. i915_gem_release_mmap(obj);
  1867. }
  1868. uint32_t
  1869. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1870. {
  1871. uint32_t gtt_size;
  1872. if (INTEL_INFO(dev)->gen >= 4 ||
  1873. tiling_mode == I915_TILING_NONE)
  1874. return size;
  1875. /* Previous chips need a power-of-two fence region when tiling */
  1876. if (IS_GEN3(dev))
  1877. gtt_size = 1024*1024;
  1878. else
  1879. gtt_size = 512*1024;
  1880. while (gtt_size < size)
  1881. gtt_size <<= 1;
  1882. return gtt_size;
  1883. }
  1884. /**
  1885. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1886. * @dev: drm device
  1887. * @size: object size
  1888. * @tiling_mode: tiling mode
  1889. * @fenced: is fenced alignemned required or not
  1890. *
  1891. * Return the required GTT alignment for an object, taking into account
  1892. * potential fence register mapping.
  1893. */
  1894. uint32_t
  1895. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1896. int tiling_mode, bool fenced)
  1897. {
  1898. /*
  1899. * Minimum alignment is 4k (GTT page size), but might be greater
  1900. * if a fence register is needed for the object.
  1901. */
  1902. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1903. tiling_mode == I915_TILING_NONE)
  1904. return 4096;
  1905. /*
  1906. * Previous chips need to be aligned to the size of the smallest
  1907. * fence register that can contain the object.
  1908. */
  1909. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1910. }
  1911. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1912. {
  1913. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1914. int ret;
  1915. dev_priv->mm.shrinker_no_lock_stealing = true;
  1916. ret = drm_gem_create_mmap_offset(&obj->base);
  1917. if (ret != -ENOSPC)
  1918. goto out;
  1919. /* Badly fragmented mmap space? The only way we can recover
  1920. * space is by destroying unwanted objects. We can't randomly release
  1921. * mmap_offsets as userspace expects them to be persistent for the
  1922. * lifetime of the objects. The closest we can is to release the
  1923. * offsets on purgeable objects by truncating it and marking it purged,
  1924. * which prevents userspace from ever using that object again.
  1925. */
  1926. i915_gem_shrink(dev_priv,
  1927. obj->base.size >> PAGE_SHIFT,
  1928. I915_SHRINK_BOUND |
  1929. I915_SHRINK_UNBOUND |
  1930. I915_SHRINK_PURGEABLE);
  1931. ret = drm_gem_create_mmap_offset(&obj->base);
  1932. if (ret != -ENOSPC)
  1933. goto out;
  1934. i915_gem_shrink_all(dev_priv);
  1935. ret = drm_gem_create_mmap_offset(&obj->base);
  1936. out:
  1937. dev_priv->mm.shrinker_no_lock_stealing = false;
  1938. return ret;
  1939. }
  1940. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1941. {
  1942. drm_gem_free_mmap_offset(&obj->base);
  1943. }
  1944. int
  1945. i915_gem_mmap_gtt(struct drm_file *file,
  1946. struct drm_device *dev,
  1947. uint32_t handle,
  1948. uint64_t *offset)
  1949. {
  1950. struct drm_i915_gem_object *obj;
  1951. int ret;
  1952. ret = i915_mutex_lock_interruptible(dev);
  1953. if (ret)
  1954. return ret;
  1955. obj = to_intel_bo(drm_gem_object_lookup(file, handle));
  1956. if (&obj->base == NULL) {
  1957. ret = -ENOENT;
  1958. goto unlock;
  1959. }
  1960. if (obj->madv != I915_MADV_WILLNEED) {
  1961. DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
  1962. ret = -EFAULT;
  1963. goto out;
  1964. }
  1965. ret = i915_gem_object_create_mmap_offset(obj);
  1966. if (ret)
  1967. goto out;
  1968. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1969. out:
  1970. drm_gem_object_unreference(&obj->base);
  1971. unlock:
  1972. mutex_unlock(&dev->struct_mutex);
  1973. return ret;
  1974. }
  1975. /**
  1976. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1977. * @dev: DRM device
  1978. * @data: GTT mapping ioctl data
  1979. * @file: GEM object info
  1980. *
  1981. * Simply returns the fake offset to userspace so it can mmap it.
  1982. * The mmap call will end up in drm_gem_mmap(), which will set things
  1983. * up so we can get faults in the handler above.
  1984. *
  1985. * The fault handler will take care of binding the object into the GTT
  1986. * (since it may have been evicted to make room for something), allocating
  1987. * a fence register, and mapping the appropriate aperture address into
  1988. * userspace.
  1989. */
  1990. int
  1991. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1992. struct drm_file *file)
  1993. {
  1994. struct drm_i915_gem_mmap_gtt *args = data;
  1995. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1996. }
  1997. /* Immediately discard the backing storage */
  1998. static void
  1999. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  2000. {
  2001. i915_gem_object_free_mmap_offset(obj);
  2002. if (obj->base.filp == NULL)
  2003. return;
  2004. /* Our goal here is to return as much of the memory as
  2005. * is possible back to the system as we are called from OOM.
  2006. * To do this we must instruct the shmfs to drop all of its
  2007. * backing pages, *now*.
  2008. */
  2009. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  2010. obj->madv = __I915_MADV_PURGED;
  2011. }
  2012. /* Try to discard unwanted pages */
  2013. static void
  2014. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  2015. {
  2016. struct address_space *mapping;
  2017. switch (obj->madv) {
  2018. case I915_MADV_DONTNEED:
  2019. i915_gem_object_truncate(obj);
  2020. case __I915_MADV_PURGED:
  2021. return;
  2022. }
  2023. if (obj->base.filp == NULL)
  2024. return;
  2025. mapping = obj->base.filp->f_mapping,
  2026. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  2027. }
  2028. static void
  2029. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  2030. {
  2031. struct sgt_iter sgt_iter;
  2032. struct page *page;
  2033. int ret;
  2034. BUG_ON(obj->madv == __I915_MADV_PURGED);
  2035. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  2036. if (WARN_ON(ret)) {
  2037. /* In the event of a disaster, abandon all caches and
  2038. * hope for the best.
  2039. */
  2040. i915_gem_clflush_object(obj, true);
  2041. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2042. }
  2043. i915_gem_gtt_finish_object(obj);
  2044. if (i915_gem_object_needs_bit17_swizzle(obj))
  2045. i915_gem_object_save_bit_17_swizzle(obj);
  2046. if (obj->madv == I915_MADV_DONTNEED)
  2047. obj->dirty = 0;
  2048. for_each_sgt_page(page, sgt_iter, obj->pages) {
  2049. if (obj->dirty)
  2050. set_page_dirty(page);
  2051. if (obj->madv == I915_MADV_WILLNEED)
  2052. mark_page_accessed(page);
  2053. put_page(page);
  2054. }
  2055. obj->dirty = 0;
  2056. sg_free_table(obj->pages);
  2057. kfree(obj->pages);
  2058. }
  2059. int
  2060. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  2061. {
  2062. const struct drm_i915_gem_object_ops *ops = obj->ops;
  2063. if (obj->pages == NULL)
  2064. return 0;
  2065. if (obj->pages_pin_count)
  2066. return -EBUSY;
  2067. BUG_ON(i915_gem_obj_bound_any(obj));
  2068. /* ->put_pages might need to allocate memory for the bit17 swizzle
  2069. * array, hence protect them from being reaped by removing them from gtt
  2070. * lists early. */
  2071. list_del(&obj->global_list);
  2072. if (obj->mapping) {
  2073. if (is_vmalloc_addr(obj->mapping))
  2074. vunmap(obj->mapping);
  2075. else
  2076. kunmap(kmap_to_page(obj->mapping));
  2077. obj->mapping = NULL;
  2078. }
  2079. ops->put_pages(obj);
  2080. obj->pages = NULL;
  2081. i915_gem_object_invalidate(obj);
  2082. return 0;
  2083. }
  2084. static int
  2085. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  2086. {
  2087. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2088. int page_count, i;
  2089. struct address_space *mapping;
  2090. struct sg_table *st;
  2091. struct scatterlist *sg;
  2092. struct sgt_iter sgt_iter;
  2093. struct page *page;
  2094. unsigned long last_pfn = 0; /* suppress gcc warning */
  2095. int ret;
  2096. gfp_t gfp;
  2097. /* Assert that the object is not currently in any GPU domain. As it
  2098. * wasn't in the GTT, there shouldn't be any way it could have been in
  2099. * a GPU cache
  2100. */
  2101. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  2102. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  2103. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2104. if (st == NULL)
  2105. return -ENOMEM;
  2106. page_count = obj->base.size / PAGE_SIZE;
  2107. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  2108. kfree(st);
  2109. return -ENOMEM;
  2110. }
  2111. /* Get the list of pages out of our struct file. They'll be pinned
  2112. * at this point until we release them.
  2113. *
  2114. * Fail silently without starting the shrinker
  2115. */
  2116. mapping = obj->base.filp->f_mapping;
  2117. gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
  2118. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  2119. sg = st->sgl;
  2120. st->nents = 0;
  2121. for (i = 0; i < page_count; i++) {
  2122. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  2123. if (IS_ERR(page)) {
  2124. i915_gem_shrink(dev_priv,
  2125. page_count,
  2126. I915_SHRINK_BOUND |
  2127. I915_SHRINK_UNBOUND |
  2128. I915_SHRINK_PURGEABLE);
  2129. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  2130. }
  2131. if (IS_ERR(page)) {
  2132. /* We've tried hard to allocate the memory by reaping
  2133. * our own buffer, now let the real VM do its job and
  2134. * go down in flames if truly OOM.
  2135. */
  2136. i915_gem_shrink_all(dev_priv);
  2137. page = shmem_read_mapping_page(mapping, i);
  2138. if (IS_ERR(page)) {
  2139. ret = PTR_ERR(page);
  2140. goto err_pages;
  2141. }
  2142. }
  2143. #ifdef CONFIG_SWIOTLB
  2144. if (swiotlb_nr_tbl()) {
  2145. st->nents++;
  2146. sg_set_page(sg, page, PAGE_SIZE, 0);
  2147. sg = sg_next(sg);
  2148. continue;
  2149. }
  2150. #endif
  2151. if (!i || page_to_pfn(page) != last_pfn + 1) {
  2152. if (i)
  2153. sg = sg_next(sg);
  2154. st->nents++;
  2155. sg_set_page(sg, page, PAGE_SIZE, 0);
  2156. } else {
  2157. sg->length += PAGE_SIZE;
  2158. }
  2159. last_pfn = page_to_pfn(page);
  2160. /* Check that the i965g/gm workaround works. */
  2161. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  2162. }
  2163. #ifdef CONFIG_SWIOTLB
  2164. if (!swiotlb_nr_tbl())
  2165. #endif
  2166. sg_mark_end(sg);
  2167. obj->pages = st;
  2168. ret = i915_gem_gtt_prepare_object(obj);
  2169. if (ret)
  2170. goto err_pages;
  2171. if (i915_gem_object_needs_bit17_swizzle(obj))
  2172. i915_gem_object_do_bit_17_swizzle(obj);
  2173. if (obj->tiling_mode != I915_TILING_NONE &&
  2174. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  2175. i915_gem_object_pin_pages(obj);
  2176. return 0;
  2177. err_pages:
  2178. sg_mark_end(sg);
  2179. for_each_sgt_page(page, sgt_iter, st)
  2180. put_page(page);
  2181. sg_free_table(st);
  2182. kfree(st);
  2183. /* shmemfs first checks if there is enough memory to allocate the page
  2184. * and reports ENOSPC should there be insufficient, along with the usual
  2185. * ENOMEM for a genuine allocation failure.
  2186. *
  2187. * We use ENOSPC in our driver to mean that we have run out of aperture
  2188. * space and so want to translate the error from shmemfs back to our
  2189. * usual understanding of ENOMEM.
  2190. */
  2191. if (ret == -ENOSPC)
  2192. ret = -ENOMEM;
  2193. return ret;
  2194. }
  2195. /* Ensure that the associated pages are gathered from the backing storage
  2196. * and pinned into our object. i915_gem_object_get_pages() may be called
  2197. * multiple times before they are released by a single call to
  2198. * i915_gem_object_put_pages() - once the pages are no longer referenced
  2199. * either as a result of memory pressure (reaping pages under the shrinker)
  2200. * or as the object is itself released.
  2201. */
  2202. int
  2203. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2204. {
  2205. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2206. const struct drm_i915_gem_object_ops *ops = obj->ops;
  2207. int ret;
  2208. if (obj->pages)
  2209. return 0;
  2210. if (obj->madv != I915_MADV_WILLNEED) {
  2211. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  2212. return -EFAULT;
  2213. }
  2214. BUG_ON(obj->pages_pin_count);
  2215. ret = ops->get_pages(obj);
  2216. if (ret)
  2217. return ret;
  2218. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2219. obj->get_page.sg = obj->pages->sgl;
  2220. obj->get_page.last = 0;
  2221. return 0;
  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. {
  2226. unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
  2227. struct sg_table *sgt = obj->pages;
  2228. struct sgt_iter sgt_iter;
  2229. struct page *page;
  2230. struct page *stack_pages[32];
  2231. struct page **pages = stack_pages;
  2232. unsigned long i = 0;
  2233. void *addr;
  2234. /* A single page can always be kmapped */
  2235. if (n_pages == 1)
  2236. return kmap(sg_page(sgt->sgl));
  2237. if (n_pages > ARRAY_SIZE(stack_pages)) {
  2238. /* Too big for stack -- allocate temporary array instead */
  2239. pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
  2240. if (!pages)
  2241. return NULL;
  2242. }
  2243. for_each_sgt_page(page, sgt_iter, sgt)
  2244. pages[i++] = page;
  2245. /* Check that we have the expected number of pages */
  2246. GEM_BUG_ON(i != n_pages);
  2247. addr = vmap(pages, n_pages, 0, PAGE_KERNEL);
  2248. if (pages != stack_pages)
  2249. drm_free_large(pages);
  2250. return addr;
  2251. }
  2252. /* get, pin, and map the pages of the object into kernel space */
  2253. void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj)
  2254. {
  2255. int ret;
  2256. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2257. ret = i915_gem_object_get_pages(obj);
  2258. if (ret)
  2259. return ERR_PTR(ret);
  2260. i915_gem_object_pin_pages(obj);
  2261. if (!obj->mapping) {
  2262. obj->mapping = i915_gem_object_map(obj);
  2263. if (!obj->mapping) {
  2264. i915_gem_object_unpin_pages(obj);
  2265. return ERR_PTR(-ENOMEM);
  2266. }
  2267. }
  2268. return obj->mapping;
  2269. }
  2270. void i915_vma_move_to_active(struct i915_vma *vma,
  2271. struct drm_i915_gem_request *req)
  2272. {
  2273. struct drm_i915_gem_object *obj = vma->obj;
  2274. struct intel_engine_cs *engine;
  2275. engine = i915_gem_request_get_engine(req);
  2276. /* Add a reference if we're newly entering the active list. */
  2277. if (obj->active == 0)
  2278. drm_gem_object_reference(&obj->base);
  2279. obj->active |= intel_engine_flag(engine);
  2280. list_move_tail(&obj->engine_list[engine->id], &engine->active_list);
  2281. i915_gem_request_assign(&obj->last_read_req[engine->id], req);
  2282. list_move_tail(&vma->vm_link, &vma->vm->active_list);
  2283. }
  2284. static void
  2285. i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
  2286. {
  2287. GEM_BUG_ON(obj->last_write_req == NULL);
  2288. GEM_BUG_ON(!(obj->active & intel_engine_flag(obj->last_write_req->engine)));
  2289. i915_gem_request_assign(&obj->last_write_req, NULL);
  2290. intel_fb_obj_flush(obj, true, ORIGIN_CS);
  2291. }
  2292. static void
  2293. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring)
  2294. {
  2295. struct i915_vma *vma;
  2296. GEM_BUG_ON(obj->last_read_req[ring] == NULL);
  2297. GEM_BUG_ON(!(obj->active & (1 << ring)));
  2298. list_del_init(&obj->engine_list[ring]);
  2299. i915_gem_request_assign(&obj->last_read_req[ring], NULL);
  2300. if (obj->last_write_req && obj->last_write_req->engine->id == ring)
  2301. i915_gem_object_retire__write(obj);
  2302. obj->active &= ~(1 << ring);
  2303. if (obj->active)
  2304. return;
  2305. /* Bump our place on the bound list to keep it roughly in LRU order
  2306. * so that we don't steal from recently used but inactive objects
  2307. * (unless we are forced to ofc!)
  2308. */
  2309. list_move_tail(&obj->global_list,
  2310. &to_i915(obj->base.dev)->mm.bound_list);
  2311. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2312. if (!list_empty(&vma->vm_link))
  2313. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  2314. }
  2315. i915_gem_request_assign(&obj->last_fenced_req, NULL);
  2316. drm_gem_object_unreference(&obj->base);
  2317. }
  2318. static int
  2319. i915_gem_init_seqno(struct drm_i915_private *dev_priv, u32 seqno)
  2320. {
  2321. struct intel_engine_cs *engine;
  2322. int ret;
  2323. /* Carefully retire all requests without writing to the rings */
  2324. for_each_engine(engine, dev_priv) {
  2325. ret = intel_engine_idle(engine);
  2326. if (ret)
  2327. return ret;
  2328. }
  2329. i915_gem_retire_requests(dev_priv);
  2330. /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
  2331. if (!i915_seqno_passed(seqno, dev_priv->next_seqno)) {
  2332. while (intel_kick_waiters(dev_priv) ||
  2333. intel_kick_signalers(dev_priv))
  2334. yield();
  2335. }
  2336. /* Finally reset hw state */
  2337. for_each_engine(engine, dev_priv)
  2338. intel_ring_init_seqno(engine, seqno);
  2339. return 0;
  2340. }
  2341. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  2342. {
  2343. struct drm_i915_private *dev_priv = to_i915(dev);
  2344. int ret;
  2345. if (seqno == 0)
  2346. return -EINVAL;
  2347. /* HWS page needs to be set less than what we
  2348. * will inject to ring
  2349. */
  2350. ret = i915_gem_init_seqno(dev_priv, seqno - 1);
  2351. if (ret)
  2352. return ret;
  2353. /* Carefully set the last_seqno value so that wrap
  2354. * detection still works
  2355. */
  2356. dev_priv->next_seqno = seqno;
  2357. dev_priv->last_seqno = seqno - 1;
  2358. if (dev_priv->last_seqno == 0)
  2359. dev_priv->last_seqno--;
  2360. return 0;
  2361. }
  2362. int
  2363. i915_gem_get_seqno(struct drm_i915_private *dev_priv, u32 *seqno)
  2364. {
  2365. /* reserve 0 for non-seqno */
  2366. if (dev_priv->next_seqno == 0) {
  2367. int ret = i915_gem_init_seqno(dev_priv, 0);
  2368. if (ret)
  2369. return ret;
  2370. dev_priv->next_seqno = 1;
  2371. }
  2372. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  2373. return 0;
  2374. }
  2375. static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
  2376. {
  2377. struct drm_i915_private *dev_priv = engine->i915;
  2378. dev_priv->gt.active_engines |= intel_engine_flag(engine);
  2379. if (dev_priv->gt.awake)
  2380. return;
  2381. intel_runtime_pm_get_noresume(dev_priv);
  2382. dev_priv->gt.awake = true;
  2383. i915_update_gfx_val(dev_priv);
  2384. if (INTEL_GEN(dev_priv) >= 6)
  2385. gen6_rps_busy(dev_priv);
  2386. queue_delayed_work(dev_priv->wq,
  2387. &dev_priv->gt.retire_work,
  2388. round_jiffies_up_relative(HZ));
  2389. }
  2390. /*
  2391. * NB: This function is not allowed to fail. Doing so would mean the the
  2392. * request is not being tracked for completion but the work itself is
  2393. * going to happen on the hardware. This would be a Bad Thing(tm).
  2394. */
  2395. void __i915_add_request(struct drm_i915_gem_request *request,
  2396. struct drm_i915_gem_object *obj,
  2397. bool flush_caches)
  2398. {
  2399. struct intel_engine_cs *engine;
  2400. struct intel_ringbuffer *ringbuf;
  2401. u32 request_start;
  2402. u32 reserved_tail;
  2403. int ret;
  2404. if (WARN_ON(request == NULL))
  2405. return;
  2406. engine = request->engine;
  2407. ringbuf = request->ringbuf;
  2408. /*
  2409. * To ensure that this call will not fail, space for its emissions
  2410. * should already have been reserved in the ring buffer. Let the ring
  2411. * know that it is time to use that space up.
  2412. */
  2413. request_start = intel_ring_get_tail(ringbuf);
  2414. reserved_tail = request->reserved_space;
  2415. request->reserved_space = 0;
  2416. /*
  2417. * Emit any outstanding flushes - execbuf can fail to emit the flush
  2418. * after having emitted the batchbuffer command. Hence we need to fix
  2419. * things up similar to emitting the lazy request. The difference here
  2420. * is that the flush _must_ happen before the next request, no matter
  2421. * what.
  2422. */
  2423. if (flush_caches) {
  2424. if (i915.enable_execlists)
  2425. ret = logical_ring_flush_all_caches(request);
  2426. else
  2427. ret = intel_ring_flush_all_caches(request);
  2428. /* Not allowed to fail! */
  2429. WARN(ret, "*_ring_flush_all_caches failed: %d!\n", ret);
  2430. }
  2431. trace_i915_gem_request_add(request);
  2432. request->head = request_start;
  2433. /* Whilst this request exists, batch_obj will be on the
  2434. * active_list, and so will hold the active reference. Only when this
  2435. * request is retired will the the batch_obj be moved onto the
  2436. * inactive_list and lose its active reference. Hence we do not need
  2437. * to explicitly hold another reference here.
  2438. */
  2439. request->batch_obj = obj;
  2440. /* Seal the request and mark it as pending execution. Note that
  2441. * we may inspect this state, without holding any locks, during
  2442. * hangcheck. Hence we apply the barrier to ensure that we do not
  2443. * see a more recent value in the hws than we are tracking.
  2444. */
  2445. request->emitted_jiffies = jiffies;
  2446. request->previous_seqno = engine->last_submitted_seqno;
  2447. smp_store_mb(engine->last_submitted_seqno, request->seqno);
  2448. list_add_tail(&request->list, &engine->request_list);
  2449. /* Record the position of the start of the request so that
  2450. * should we detect the updated seqno part-way through the
  2451. * GPU processing the request, we never over-estimate the
  2452. * position of the head.
  2453. */
  2454. request->postfix = intel_ring_get_tail(ringbuf);
  2455. if (i915.enable_execlists)
  2456. ret = engine->emit_request(request);
  2457. else {
  2458. ret = engine->add_request(request);
  2459. request->tail = intel_ring_get_tail(ringbuf);
  2460. }
  2461. /* Not allowed to fail! */
  2462. WARN(ret, "emit|add_request failed: %d!\n", ret);
  2463. /* Sanity check that the reserved size was large enough. */
  2464. ret = intel_ring_get_tail(ringbuf) - request_start;
  2465. if (ret < 0)
  2466. ret += ringbuf->size;
  2467. WARN_ONCE(ret > reserved_tail,
  2468. "Not enough space reserved (%d bytes) "
  2469. "for adding the request (%d bytes)\n",
  2470. reserved_tail, ret);
  2471. i915_gem_mark_busy(engine);
  2472. }
  2473. static bool i915_context_is_banned(const struct i915_gem_context *ctx)
  2474. {
  2475. unsigned long elapsed;
  2476. if (ctx->hang_stats.banned)
  2477. return true;
  2478. elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
  2479. if (ctx->hang_stats.ban_period_seconds &&
  2480. elapsed <= ctx->hang_stats.ban_period_seconds) {
  2481. DRM_DEBUG("context hanging too fast, banning!\n");
  2482. return true;
  2483. }
  2484. return false;
  2485. }
  2486. static void i915_set_reset_status(struct i915_gem_context *ctx,
  2487. const bool guilty)
  2488. {
  2489. struct i915_ctx_hang_stats *hs = &ctx->hang_stats;
  2490. if (guilty) {
  2491. hs->banned = i915_context_is_banned(ctx);
  2492. hs->batch_active++;
  2493. hs->guilty_ts = get_seconds();
  2494. } else {
  2495. hs->batch_pending++;
  2496. }
  2497. }
  2498. void i915_gem_request_free(struct kref *req_ref)
  2499. {
  2500. struct drm_i915_gem_request *req = container_of(req_ref,
  2501. typeof(*req), ref);
  2502. kmem_cache_free(req->i915->requests, req);
  2503. }
  2504. static inline int
  2505. __i915_gem_request_alloc(struct intel_engine_cs *engine,
  2506. struct i915_gem_context *ctx,
  2507. struct drm_i915_gem_request **req_out)
  2508. {
  2509. struct drm_i915_private *dev_priv = engine->i915;
  2510. unsigned reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  2511. struct drm_i915_gem_request *req;
  2512. int ret;
  2513. if (!req_out)
  2514. return -EINVAL;
  2515. *req_out = NULL;
  2516. /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  2517. * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
  2518. * and restart.
  2519. */
  2520. ret = i915_gem_check_wedge(reset_counter, dev_priv->mm.interruptible);
  2521. if (ret)
  2522. return ret;
  2523. req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
  2524. if (req == NULL)
  2525. return -ENOMEM;
  2526. ret = i915_gem_get_seqno(engine->i915, &req->seqno);
  2527. if (ret)
  2528. goto err;
  2529. kref_init(&req->ref);
  2530. req->i915 = dev_priv;
  2531. req->engine = engine;
  2532. req->ctx = ctx;
  2533. i915_gem_context_reference(req->ctx);
  2534. /*
  2535. * Reserve space in the ring buffer for all the commands required to
  2536. * eventually emit this request. This is to guarantee that the
  2537. * i915_add_request() call can't fail. Note that the reserve may need
  2538. * to be redone if the request is not actually submitted straight
  2539. * away, e.g. because a GPU scheduler has deferred it.
  2540. */
  2541. req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  2542. if (i915.enable_execlists)
  2543. ret = intel_logical_ring_alloc_request_extras(req);
  2544. else
  2545. ret = intel_ring_alloc_request_extras(req);
  2546. if (ret)
  2547. goto err_ctx;
  2548. *req_out = req;
  2549. return 0;
  2550. err_ctx:
  2551. i915_gem_context_unreference(ctx);
  2552. err:
  2553. kmem_cache_free(dev_priv->requests, req);
  2554. return ret;
  2555. }
  2556. /**
  2557. * i915_gem_request_alloc - allocate a request structure
  2558. *
  2559. * @engine: engine that we wish to issue the request on.
  2560. * @ctx: context that the request will be associated with.
  2561. * This can be NULL if the request is not directly related to
  2562. * any specific user context, in which case this function will
  2563. * choose an appropriate context to use.
  2564. *
  2565. * Returns a pointer to the allocated request if successful,
  2566. * or an error code if not.
  2567. */
  2568. struct drm_i915_gem_request *
  2569. i915_gem_request_alloc(struct intel_engine_cs *engine,
  2570. struct i915_gem_context *ctx)
  2571. {
  2572. struct drm_i915_gem_request *req;
  2573. int err;
  2574. if (ctx == NULL)
  2575. ctx = engine->i915->kernel_context;
  2576. err = __i915_gem_request_alloc(engine, ctx, &req);
  2577. return err ? ERR_PTR(err) : req;
  2578. }
  2579. struct drm_i915_gem_request *
  2580. i915_gem_find_active_request(struct intel_engine_cs *engine)
  2581. {
  2582. struct drm_i915_gem_request *request;
  2583. /* We are called by the error capture and reset at a random
  2584. * point in time. In particular, note that neither is crucially
  2585. * ordered with an interrupt. After a hang, the GPU is dead and we
  2586. * assume that no more writes can happen (we waited long enough for
  2587. * all writes that were in transaction to be flushed) - adding an
  2588. * extra delay for a recent interrupt is pointless. Hence, we do
  2589. * not need an engine->irq_seqno_barrier() before the seqno reads.
  2590. */
  2591. list_for_each_entry(request, &engine->request_list, list) {
  2592. if (i915_gem_request_completed(request))
  2593. continue;
  2594. return request;
  2595. }
  2596. return NULL;
  2597. }
  2598. static void i915_gem_reset_engine_status(struct intel_engine_cs *engine)
  2599. {
  2600. struct drm_i915_gem_request *request;
  2601. bool ring_hung;
  2602. request = i915_gem_find_active_request(engine);
  2603. if (request == NULL)
  2604. return;
  2605. ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2606. i915_set_reset_status(request->ctx, ring_hung);
  2607. list_for_each_entry_continue(request, &engine->request_list, list)
  2608. i915_set_reset_status(request->ctx, false);
  2609. }
  2610. static void i915_gem_reset_engine_cleanup(struct intel_engine_cs *engine)
  2611. {
  2612. struct intel_ringbuffer *buffer;
  2613. while (!list_empty(&engine->active_list)) {
  2614. struct drm_i915_gem_object *obj;
  2615. obj = list_first_entry(&engine->active_list,
  2616. struct drm_i915_gem_object,
  2617. engine_list[engine->id]);
  2618. i915_gem_object_retire__read(obj, engine->id);
  2619. }
  2620. /*
  2621. * Clear the execlists queue up before freeing the requests, as those
  2622. * are the ones that keep the context and ringbuffer backing objects
  2623. * pinned in place.
  2624. */
  2625. if (i915.enable_execlists) {
  2626. /* Ensure irq handler finishes or is cancelled. */
  2627. tasklet_kill(&engine->irq_tasklet);
  2628. intel_execlists_cancel_requests(engine);
  2629. }
  2630. /*
  2631. * We must free the requests after all the corresponding objects have
  2632. * been moved off active lists. Which is the same order as the normal
  2633. * retire_requests function does. This is important if object hold
  2634. * implicit references on things like e.g. ppgtt address spaces through
  2635. * the request.
  2636. */
  2637. while (!list_empty(&engine->request_list)) {
  2638. struct drm_i915_gem_request *request;
  2639. request = list_first_entry(&engine->request_list,
  2640. struct drm_i915_gem_request,
  2641. list);
  2642. i915_gem_request_retire(request);
  2643. }
  2644. /* Having flushed all requests from all queues, we know that all
  2645. * ringbuffers must now be empty. However, since we do not reclaim
  2646. * all space when retiring the request (to prevent HEADs colliding
  2647. * with rapid ringbuffer wraparound) the amount of available space
  2648. * upon reset is less than when we start. Do one more pass over
  2649. * all the ringbuffers to reset last_retired_head.
  2650. */
  2651. list_for_each_entry(buffer, &engine->buffers, link) {
  2652. buffer->last_retired_head = buffer->tail;
  2653. intel_ring_update_space(buffer);
  2654. }
  2655. intel_ring_init_seqno(engine, engine->last_submitted_seqno);
  2656. engine->i915->gt.active_engines &= ~intel_engine_flag(engine);
  2657. }
  2658. void i915_gem_reset(struct drm_device *dev)
  2659. {
  2660. struct drm_i915_private *dev_priv = to_i915(dev);
  2661. struct intel_engine_cs *engine;
  2662. /*
  2663. * Before we free the objects from the requests, we need to inspect
  2664. * them for finding the guilty party. As the requests only borrow
  2665. * their reference to the objects, the inspection must be done first.
  2666. */
  2667. for_each_engine(engine, dev_priv)
  2668. i915_gem_reset_engine_status(engine);
  2669. for_each_engine(engine, dev_priv)
  2670. i915_gem_reset_engine_cleanup(engine);
  2671. mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
  2672. i915_gem_context_reset(dev);
  2673. i915_gem_restore_fences(dev);
  2674. WARN_ON(i915_verify_lists(dev));
  2675. }
  2676. /**
  2677. * This function clears the request list as sequence numbers are passed.
  2678. * @engine: engine to retire requests on
  2679. */
  2680. void
  2681. i915_gem_retire_requests_ring(struct intel_engine_cs *engine)
  2682. {
  2683. WARN_ON(i915_verify_lists(engine->dev));
  2684. /* Retire requests first as we use it above for the early return.
  2685. * If we retire requests last, we may use a later seqno and so clear
  2686. * the requests lists without clearing the active list, leading to
  2687. * confusion.
  2688. */
  2689. while (!list_empty(&engine->request_list)) {
  2690. struct drm_i915_gem_request *request;
  2691. request = list_first_entry(&engine->request_list,
  2692. struct drm_i915_gem_request,
  2693. list);
  2694. if (!i915_gem_request_completed(request))
  2695. break;
  2696. i915_gem_request_retire(request);
  2697. }
  2698. /* Move any buffers on the active list that are no longer referenced
  2699. * by the ringbuffer to the flushing/inactive lists as appropriate,
  2700. * before we free the context associated with the requests.
  2701. */
  2702. while (!list_empty(&engine->active_list)) {
  2703. struct drm_i915_gem_object *obj;
  2704. obj = list_first_entry(&engine->active_list,
  2705. struct drm_i915_gem_object,
  2706. engine_list[engine->id]);
  2707. if (!list_empty(&obj->last_read_req[engine->id]->list))
  2708. break;
  2709. i915_gem_object_retire__read(obj, engine->id);
  2710. }
  2711. WARN_ON(i915_verify_lists(engine->dev));
  2712. }
  2713. void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
  2714. {
  2715. struct intel_engine_cs *engine;
  2716. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2717. if (dev_priv->gt.active_engines == 0)
  2718. return;
  2719. GEM_BUG_ON(!dev_priv->gt.awake);
  2720. for_each_engine(engine, dev_priv) {
  2721. i915_gem_retire_requests_ring(engine);
  2722. if (list_empty(&engine->request_list))
  2723. dev_priv->gt.active_engines &= ~intel_engine_flag(engine);
  2724. }
  2725. if (dev_priv->gt.active_engines == 0)
  2726. queue_delayed_work(dev_priv->wq,
  2727. &dev_priv->gt.idle_work,
  2728. msecs_to_jiffies(100));
  2729. }
  2730. static void
  2731. i915_gem_retire_work_handler(struct work_struct *work)
  2732. {
  2733. struct drm_i915_private *dev_priv =
  2734. container_of(work, typeof(*dev_priv), gt.retire_work.work);
  2735. struct drm_device *dev = &dev_priv->drm;
  2736. /* Come back later if the device is busy... */
  2737. if (mutex_trylock(&dev->struct_mutex)) {
  2738. i915_gem_retire_requests(dev_priv);
  2739. mutex_unlock(&dev->struct_mutex);
  2740. }
  2741. /* Keep the retire handler running until we are finally idle.
  2742. * We do not need to do this test under locking as in the worst-case
  2743. * we queue the retire worker once too often.
  2744. */
  2745. if (READ_ONCE(dev_priv->gt.awake))
  2746. queue_delayed_work(dev_priv->wq,
  2747. &dev_priv->gt.retire_work,
  2748. round_jiffies_up_relative(HZ));
  2749. }
  2750. static void
  2751. i915_gem_idle_work_handler(struct work_struct *work)
  2752. {
  2753. struct drm_i915_private *dev_priv =
  2754. container_of(work, typeof(*dev_priv), gt.idle_work.work);
  2755. struct drm_device *dev = &dev_priv->drm;
  2756. struct intel_engine_cs *engine;
  2757. unsigned int stuck_engines;
  2758. bool rearm_hangcheck;
  2759. if (!READ_ONCE(dev_priv->gt.awake))
  2760. return;
  2761. if (READ_ONCE(dev_priv->gt.active_engines))
  2762. return;
  2763. rearm_hangcheck =
  2764. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  2765. if (!mutex_trylock(&dev->struct_mutex)) {
  2766. /* Currently busy, come back later */
  2767. mod_delayed_work(dev_priv->wq,
  2768. &dev_priv->gt.idle_work,
  2769. msecs_to_jiffies(50));
  2770. goto out_rearm;
  2771. }
  2772. if (dev_priv->gt.active_engines)
  2773. goto out_unlock;
  2774. for_each_engine(engine, dev_priv)
  2775. i915_gem_batch_pool_fini(&engine->batch_pool);
  2776. GEM_BUG_ON(!dev_priv->gt.awake);
  2777. dev_priv->gt.awake = false;
  2778. rearm_hangcheck = false;
  2779. stuck_engines = intel_kick_waiters(dev_priv);
  2780. if (unlikely(stuck_engines)) {
  2781. DRM_DEBUG_DRIVER("kicked stuck waiters...missed irq\n");
  2782. dev_priv->gpu_error.missed_irq_rings |= stuck_engines;
  2783. }
  2784. if (INTEL_GEN(dev_priv) >= 6)
  2785. gen6_rps_idle(dev_priv);
  2786. intel_runtime_pm_put(dev_priv);
  2787. out_unlock:
  2788. mutex_unlock(&dev->struct_mutex);
  2789. out_rearm:
  2790. if (rearm_hangcheck) {
  2791. GEM_BUG_ON(!dev_priv->gt.awake);
  2792. i915_queue_hangcheck(dev_priv);
  2793. }
  2794. }
  2795. /**
  2796. * Ensures that an object will eventually get non-busy by flushing any required
  2797. * write domains, emitting any outstanding lazy request and retiring and
  2798. * completed requests.
  2799. * @obj: object to flush
  2800. */
  2801. static int
  2802. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2803. {
  2804. int i;
  2805. if (!obj->active)
  2806. return 0;
  2807. for (i = 0; i < I915_NUM_ENGINES; i++) {
  2808. struct drm_i915_gem_request *req;
  2809. req = obj->last_read_req[i];
  2810. if (req == NULL)
  2811. continue;
  2812. if (i915_gem_request_completed(req))
  2813. i915_gem_object_retire__read(obj, i);
  2814. }
  2815. return 0;
  2816. }
  2817. /**
  2818. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2819. * @dev: drm device pointer
  2820. * @data: ioctl data blob
  2821. * @file: drm file pointer
  2822. *
  2823. * Returns 0 if successful, else an error is returned with the remaining time in
  2824. * the timeout parameter.
  2825. * -ETIME: object is still busy after timeout
  2826. * -ERESTARTSYS: signal interrupted the wait
  2827. * -ENONENT: object doesn't exist
  2828. * Also possible, but rare:
  2829. * -EAGAIN: GPU wedged
  2830. * -ENOMEM: damn
  2831. * -ENODEV: Internal IRQ fail
  2832. * -E?: The add request failed
  2833. *
  2834. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2835. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2836. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2837. * without holding struct_mutex the object may become re-busied before this
  2838. * function completes. A similar but shorter * race condition exists in the busy
  2839. * ioctl
  2840. */
  2841. int
  2842. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2843. {
  2844. struct drm_i915_gem_wait *args = data;
  2845. struct drm_i915_gem_object *obj;
  2846. struct drm_i915_gem_request *req[I915_NUM_ENGINES];
  2847. int i, n = 0;
  2848. int ret;
  2849. if (args->flags != 0)
  2850. return -EINVAL;
  2851. ret = i915_mutex_lock_interruptible(dev);
  2852. if (ret)
  2853. return ret;
  2854. obj = to_intel_bo(drm_gem_object_lookup(file, args->bo_handle));
  2855. if (&obj->base == NULL) {
  2856. mutex_unlock(&dev->struct_mutex);
  2857. return -ENOENT;
  2858. }
  2859. /* Need to make sure the object gets inactive eventually. */
  2860. ret = i915_gem_object_flush_active(obj);
  2861. if (ret)
  2862. goto out;
  2863. if (!obj->active)
  2864. goto out;
  2865. /* Do this after OLR check to make sure we make forward progress polling
  2866. * on this IOCTL with a timeout == 0 (like busy ioctl)
  2867. */
  2868. if (args->timeout_ns == 0) {
  2869. ret = -ETIME;
  2870. goto out;
  2871. }
  2872. drm_gem_object_unreference(&obj->base);
  2873. for (i = 0; i < I915_NUM_ENGINES; i++) {
  2874. if (obj->last_read_req[i] == NULL)
  2875. continue;
  2876. req[n++] = i915_gem_request_reference(obj->last_read_req[i]);
  2877. }
  2878. mutex_unlock(&dev->struct_mutex);
  2879. for (i = 0; i < n; i++) {
  2880. if (ret == 0)
  2881. ret = __i915_wait_request(req[i], true,
  2882. args->timeout_ns > 0 ? &args->timeout_ns : NULL,
  2883. to_rps_client(file));
  2884. i915_gem_request_unreference(req[i]);
  2885. }
  2886. return ret;
  2887. out:
  2888. drm_gem_object_unreference(&obj->base);
  2889. mutex_unlock(&dev->struct_mutex);
  2890. return ret;
  2891. }
  2892. static int
  2893. __i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2894. struct intel_engine_cs *to,
  2895. struct drm_i915_gem_request *from_req,
  2896. struct drm_i915_gem_request **to_req)
  2897. {
  2898. struct intel_engine_cs *from;
  2899. int ret;
  2900. from = i915_gem_request_get_engine(from_req);
  2901. if (to == from)
  2902. return 0;
  2903. if (i915_gem_request_completed(from_req))
  2904. return 0;
  2905. if (!i915_semaphore_is_enabled(to_i915(obj->base.dev))) {
  2906. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  2907. ret = __i915_wait_request(from_req,
  2908. i915->mm.interruptible,
  2909. NULL,
  2910. &i915->rps.semaphores);
  2911. if (ret)
  2912. return ret;
  2913. i915_gem_object_retire_request(obj, from_req);
  2914. } else {
  2915. int idx = intel_ring_sync_index(from, to);
  2916. u32 seqno = i915_gem_request_get_seqno(from_req);
  2917. WARN_ON(!to_req);
  2918. if (seqno <= from->semaphore.sync_seqno[idx])
  2919. return 0;
  2920. if (*to_req == NULL) {
  2921. struct drm_i915_gem_request *req;
  2922. req = i915_gem_request_alloc(to, NULL);
  2923. if (IS_ERR(req))
  2924. return PTR_ERR(req);
  2925. *to_req = req;
  2926. }
  2927. trace_i915_gem_ring_sync_to(*to_req, from, from_req);
  2928. ret = to->semaphore.sync_to(*to_req, from, seqno);
  2929. if (ret)
  2930. return ret;
  2931. /* We use last_read_req because sync_to()
  2932. * might have just caused seqno wrap under
  2933. * the radar.
  2934. */
  2935. from->semaphore.sync_seqno[idx] =
  2936. i915_gem_request_get_seqno(obj->last_read_req[from->id]);
  2937. }
  2938. return 0;
  2939. }
  2940. /**
  2941. * i915_gem_object_sync - sync an object to a ring.
  2942. *
  2943. * @obj: object which may be in use on another ring.
  2944. * @to: ring we wish to use the object on. May be NULL.
  2945. * @to_req: request we wish to use the object for. See below.
  2946. * This will be allocated and returned if a request is
  2947. * required but not passed in.
  2948. *
  2949. * This code is meant to abstract object synchronization with the GPU.
  2950. * Calling with NULL implies synchronizing the object with the CPU
  2951. * rather than a particular GPU ring. Conceptually we serialise writes
  2952. * between engines inside the GPU. We only allow one engine to write
  2953. * into a buffer at any time, but multiple readers. To ensure each has
  2954. * a coherent view of memory, we must:
  2955. *
  2956. * - If there is an outstanding write request to the object, the new
  2957. * request must wait for it to complete (either CPU or in hw, requests
  2958. * on the same ring will be naturally ordered).
  2959. *
  2960. * - If we are a write request (pending_write_domain is set), the new
  2961. * request must wait for outstanding read requests to complete.
  2962. *
  2963. * For CPU synchronisation (NULL to) no request is required. For syncing with
  2964. * rings to_req must be non-NULL. However, a request does not have to be
  2965. * pre-allocated. If *to_req is NULL and sync commands will be emitted then a
  2966. * request will be allocated automatically and returned through *to_req. Note
  2967. * that it is not guaranteed that commands will be emitted (because the system
  2968. * might already be idle). Hence there is no need to create a request that
  2969. * might never have any work submitted. Note further that if a request is
  2970. * returned in *to_req, it is the responsibility of the caller to submit
  2971. * that request (after potentially adding more work to it).
  2972. *
  2973. * Returns 0 if successful, else propagates up the lower layer error.
  2974. */
  2975. int
  2976. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2977. struct intel_engine_cs *to,
  2978. struct drm_i915_gem_request **to_req)
  2979. {
  2980. const bool readonly = obj->base.pending_write_domain == 0;
  2981. struct drm_i915_gem_request *req[I915_NUM_ENGINES];
  2982. int ret, i, n;
  2983. if (!obj->active)
  2984. return 0;
  2985. if (to == NULL)
  2986. return i915_gem_object_wait_rendering(obj, readonly);
  2987. n = 0;
  2988. if (readonly) {
  2989. if (obj->last_write_req)
  2990. req[n++] = obj->last_write_req;
  2991. } else {
  2992. for (i = 0; i < I915_NUM_ENGINES; i++)
  2993. if (obj->last_read_req[i])
  2994. req[n++] = obj->last_read_req[i];
  2995. }
  2996. for (i = 0; i < n; i++) {
  2997. ret = __i915_gem_object_sync(obj, to, req[i], to_req);
  2998. if (ret)
  2999. return ret;
  3000. }
  3001. return 0;
  3002. }
  3003. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  3004. {
  3005. u32 old_write_domain, old_read_domains;
  3006. /* Force a pagefault for domain tracking on next user access */
  3007. i915_gem_release_mmap(obj);
  3008. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3009. return;
  3010. old_read_domains = obj->base.read_domains;
  3011. old_write_domain = obj->base.write_domain;
  3012. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  3013. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  3014. trace_i915_gem_object_change_domain(obj,
  3015. old_read_domains,
  3016. old_write_domain);
  3017. }
  3018. static void __i915_vma_iounmap(struct i915_vma *vma)
  3019. {
  3020. GEM_BUG_ON(vma->pin_count);
  3021. if (vma->iomap == NULL)
  3022. return;
  3023. io_mapping_unmap(vma->iomap);
  3024. vma->iomap = NULL;
  3025. }
  3026. static int __i915_vma_unbind(struct i915_vma *vma, bool wait)
  3027. {
  3028. struct drm_i915_gem_object *obj = vma->obj;
  3029. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  3030. int ret;
  3031. if (list_empty(&vma->obj_link))
  3032. return 0;
  3033. if (!drm_mm_node_allocated(&vma->node)) {
  3034. i915_gem_vma_destroy(vma);
  3035. return 0;
  3036. }
  3037. if (vma->pin_count)
  3038. return -EBUSY;
  3039. BUG_ON(obj->pages == NULL);
  3040. if (wait) {
  3041. ret = i915_gem_object_wait_rendering(obj, false);
  3042. if (ret)
  3043. return ret;
  3044. }
  3045. if (vma->is_ggtt && vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  3046. i915_gem_object_finish_gtt(obj);
  3047. /* release the fence reg _after_ flushing */
  3048. ret = i915_gem_object_put_fence(obj);
  3049. if (ret)
  3050. return ret;
  3051. __i915_vma_iounmap(vma);
  3052. }
  3053. trace_i915_vma_unbind(vma);
  3054. vma->vm->unbind_vma(vma);
  3055. vma->bound = 0;
  3056. list_del_init(&vma->vm_link);
  3057. if (vma->is_ggtt) {
  3058. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  3059. obj->map_and_fenceable = false;
  3060. } else if (vma->ggtt_view.pages) {
  3061. sg_free_table(vma->ggtt_view.pages);
  3062. kfree(vma->ggtt_view.pages);
  3063. }
  3064. vma->ggtt_view.pages = NULL;
  3065. }
  3066. drm_mm_remove_node(&vma->node);
  3067. i915_gem_vma_destroy(vma);
  3068. /* Since the unbound list is global, only move to that list if
  3069. * no more VMAs exist. */
  3070. if (list_empty(&obj->vma_list))
  3071. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  3072. /* And finally now the object is completely decoupled from this vma,
  3073. * we can drop its hold on the backing storage and allow it to be
  3074. * reaped by the shrinker.
  3075. */
  3076. i915_gem_object_unpin_pages(obj);
  3077. return 0;
  3078. }
  3079. int i915_vma_unbind(struct i915_vma *vma)
  3080. {
  3081. return __i915_vma_unbind(vma, true);
  3082. }
  3083. int __i915_vma_unbind_no_wait(struct i915_vma *vma)
  3084. {
  3085. return __i915_vma_unbind(vma, false);
  3086. }
  3087. int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv)
  3088. {
  3089. struct intel_engine_cs *engine;
  3090. int ret;
  3091. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  3092. for_each_engine(engine, dev_priv) {
  3093. if (engine->last_context == NULL)
  3094. continue;
  3095. ret = intel_engine_idle(engine);
  3096. if (ret)
  3097. return ret;
  3098. }
  3099. WARN_ON(i915_verify_lists(dev));
  3100. return 0;
  3101. }
  3102. static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
  3103. unsigned long cache_level)
  3104. {
  3105. struct drm_mm_node *gtt_space = &vma->node;
  3106. struct drm_mm_node *other;
  3107. /*
  3108. * On some machines we have to be careful when putting differing types
  3109. * of snoopable memory together to avoid the prefetcher crossing memory
  3110. * domains and dying. During vm initialisation, we decide whether or not
  3111. * these constraints apply and set the drm_mm.color_adjust
  3112. * appropriately.
  3113. */
  3114. if (vma->vm->mm.color_adjust == NULL)
  3115. return true;
  3116. if (!drm_mm_node_allocated(gtt_space))
  3117. return true;
  3118. if (list_empty(&gtt_space->node_list))
  3119. return true;
  3120. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  3121. if (other->allocated && !other->hole_follows && other->color != cache_level)
  3122. return false;
  3123. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  3124. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  3125. return false;
  3126. return true;
  3127. }
  3128. /**
  3129. * Finds free space in the GTT aperture and binds the object or a view of it
  3130. * there.
  3131. * @obj: object to bind
  3132. * @vm: address space to bind into
  3133. * @ggtt_view: global gtt view if applicable
  3134. * @alignment: requested alignment
  3135. * @flags: mask of PIN_* flags to use
  3136. */
  3137. static struct i915_vma *
  3138. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  3139. struct i915_address_space *vm,
  3140. const struct i915_ggtt_view *ggtt_view,
  3141. unsigned alignment,
  3142. uint64_t flags)
  3143. {
  3144. struct drm_device *dev = obj->base.dev;
  3145. struct drm_i915_private *dev_priv = to_i915(dev);
  3146. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  3147. u32 fence_alignment, unfenced_alignment;
  3148. u32 search_flag, alloc_flag;
  3149. u64 start, end;
  3150. u64 size, fence_size;
  3151. struct i915_vma *vma;
  3152. int ret;
  3153. if (i915_is_ggtt(vm)) {
  3154. u32 view_size;
  3155. if (WARN_ON(!ggtt_view))
  3156. return ERR_PTR(-EINVAL);
  3157. view_size = i915_ggtt_view_size(obj, ggtt_view);
  3158. fence_size = i915_gem_get_gtt_size(dev,
  3159. view_size,
  3160. obj->tiling_mode);
  3161. fence_alignment = i915_gem_get_gtt_alignment(dev,
  3162. view_size,
  3163. obj->tiling_mode,
  3164. true);
  3165. unfenced_alignment = i915_gem_get_gtt_alignment(dev,
  3166. view_size,
  3167. obj->tiling_mode,
  3168. false);
  3169. size = flags & PIN_MAPPABLE ? fence_size : view_size;
  3170. } else {
  3171. fence_size = i915_gem_get_gtt_size(dev,
  3172. obj->base.size,
  3173. obj->tiling_mode);
  3174. fence_alignment = i915_gem_get_gtt_alignment(dev,
  3175. obj->base.size,
  3176. obj->tiling_mode,
  3177. true);
  3178. unfenced_alignment =
  3179. i915_gem_get_gtt_alignment(dev,
  3180. obj->base.size,
  3181. obj->tiling_mode,
  3182. false);
  3183. size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
  3184. }
  3185. start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  3186. end = vm->total;
  3187. if (flags & PIN_MAPPABLE)
  3188. end = min_t(u64, end, ggtt->mappable_end);
  3189. if (flags & PIN_ZONE_4G)
  3190. end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
  3191. if (alignment == 0)
  3192. alignment = flags & PIN_MAPPABLE ? fence_alignment :
  3193. unfenced_alignment;
  3194. if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
  3195. DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
  3196. ggtt_view ? ggtt_view->type : 0,
  3197. alignment);
  3198. return ERR_PTR(-EINVAL);
  3199. }
  3200. /* If binding the object/GGTT view requires more space than the entire
  3201. * aperture has, reject it early before evicting everything in a vain
  3202. * attempt to find space.
  3203. */
  3204. if (size > end) {
  3205. DRM_DEBUG("Attempting to bind an object (view type=%u) larger than the aperture: size=%llu > %s aperture=%llu\n",
  3206. ggtt_view ? ggtt_view->type : 0,
  3207. size,
  3208. flags & PIN_MAPPABLE ? "mappable" : "total",
  3209. end);
  3210. return ERR_PTR(-E2BIG);
  3211. }
  3212. ret = i915_gem_object_get_pages(obj);
  3213. if (ret)
  3214. return ERR_PTR(ret);
  3215. i915_gem_object_pin_pages(obj);
  3216. vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
  3217. i915_gem_obj_lookup_or_create_vma(obj, vm);
  3218. if (IS_ERR(vma))
  3219. goto err_unpin;
  3220. if (flags & PIN_OFFSET_FIXED) {
  3221. uint64_t offset = flags & PIN_OFFSET_MASK;
  3222. if (offset & (alignment - 1) || offset + size > end) {
  3223. ret = -EINVAL;
  3224. goto err_free_vma;
  3225. }
  3226. vma->node.start = offset;
  3227. vma->node.size = size;
  3228. vma->node.color = obj->cache_level;
  3229. ret = drm_mm_reserve_node(&vm->mm, &vma->node);
  3230. if (ret) {
  3231. ret = i915_gem_evict_for_vma(vma);
  3232. if (ret == 0)
  3233. ret = drm_mm_reserve_node(&vm->mm, &vma->node);
  3234. }
  3235. if (ret)
  3236. goto err_free_vma;
  3237. } else {
  3238. if (flags & PIN_HIGH) {
  3239. search_flag = DRM_MM_SEARCH_BELOW;
  3240. alloc_flag = DRM_MM_CREATE_TOP;
  3241. } else {
  3242. search_flag = DRM_MM_SEARCH_DEFAULT;
  3243. alloc_flag = DRM_MM_CREATE_DEFAULT;
  3244. }
  3245. search_free:
  3246. ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  3247. size, alignment,
  3248. obj->cache_level,
  3249. start, end,
  3250. search_flag,
  3251. alloc_flag);
  3252. if (ret) {
  3253. ret = i915_gem_evict_something(dev, vm, size, alignment,
  3254. obj->cache_level,
  3255. start, end,
  3256. flags);
  3257. if (ret == 0)
  3258. goto search_free;
  3259. goto err_free_vma;
  3260. }
  3261. }
  3262. if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
  3263. ret = -EINVAL;
  3264. goto err_remove_node;
  3265. }
  3266. trace_i915_vma_bind(vma, flags);
  3267. ret = i915_vma_bind(vma, obj->cache_level, flags);
  3268. if (ret)
  3269. goto err_remove_node;
  3270. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  3271. list_add_tail(&vma->vm_link, &vm->inactive_list);
  3272. return vma;
  3273. err_remove_node:
  3274. drm_mm_remove_node(&vma->node);
  3275. err_free_vma:
  3276. i915_gem_vma_destroy(vma);
  3277. vma = ERR_PTR(ret);
  3278. err_unpin:
  3279. i915_gem_object_unpin_pages(obj);
  3280. return vma;
  3281. }
  3282. bool
  3283. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  3284. bool force)
  3285. {
  3286. /* If we don't have a page list set up, then we're not pinned
  3287. * to GPU, and we can ignore the cache flush because it'll happen
  3288. * again at bind time.
  3289. */
  3290. if (obj->pages == NULL)
  3291. return false;
  3292. /*
  3293. * Stolen memory is always coherent with the GPU as it is explicitly
  3294. * marked as wc by the system, or the system is cache-coherent.
  3295. */
  3296. if (obj->stolen || obj->phys_handle)
  3297. return false;
  3298. /* If the GPU is snooping the contents of the CPU cache,
  3299. * we do not need to manually clear the CPU cache lines. However,
  3300. * the caches are only snooped when the render cache is
  3301. * flushed/invalidated. As we always have to emit invalidations
  3302. * and flushes when moving into and out of the RENDER domain, correct
  3303. * snooping behaviour occurs naturally as the result of our domain
  3304. * tracking.
  3305. */
  3306. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
  3307. obj->cache_dirty = true;
  3308. return false;
  3309. }
  3310. trace_i915_gem_object_clflush(obj);
  3311. drm_clflush_sg(obj->pages);
  3312. obj->cache_dirty = false;
  3313. return true;
  3314. }
  3315. /** Flushes the GTT write domain for the object if it's dirty. */
  3316. static void
  3317. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  3318. {
  3319. uint32_t old_write_domain;
  3320. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  3321. return;
  3322. /* No actual flushing is required for the GTT write domain. Writes
  3323. * to it immediately go to main memory as far as we know, so there's
  3324. * no chipset flush. It also doesn't land in render cache.
  3325. *
  3326. * However, we do have to enforce the order so that all writes through
  3327. * the GTT land before any writes to the device, such as updates to
  3328. * the GATT itself.
  3329. */
  3330. wmb();
  3331. old_write_domain = obj->base.write_domain;
  3332. obj->base.write_domain = 0;
  3333. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  3334. trace_i915_gem_object_change_domain(obj,
  3335. obj->base.read_domains,
  3336. old_write_domain);
  3337. }
  3338. /** Flushes the CPU write domain for the object if it's dirty. */
  3339. static void
  3340. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  3341. {
  3342. uint32_t old_write_domain;
  3343. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  3344. return;
  3345. if (i915_gem_clflush_object(obj, obj->pin_display))
  3346. i915_gem_chipset_flush(to_i915(obj->base.dev));
  3347. old_write_domain = obj->base.write_domain;
  3348. obj->base.write_domain = 0;
  3349. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  3350. trace_i915_gem_object_change_domain(obj,
  3351. obj->base.read_domains,
  3352. old_write_domain);
  3353. }
  3354. /**
  3355. * Moves a single object to the GTT read, and possibly write domain.
  3356. * @obj: object to act on
  3357. * @write: ask for write access or read only
  3358. *
  3359. * This function returns when the move is complete, including waiting on
  3360. * flushes to occur.
  3361. */
  3362. int
  3363. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  3364. {
  3365. struct drm_device *dev = obj->base.dev;
  3366. struct drm_i915_private *dev_priv = to_i915(dev);
  3367. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  3368. uint32_t old_write_domain, old_read_domains;
  3369. struct i915_vma *vma;
  3370. int ret;
  3371. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  3372. return 0;
  3373. ret = i915_gem_object_wait_rendering(obj, !write);
  3374. if (ret)
  3375. return ret;
  3376. /* Flush and acquire obj->pages so that we are coherent through
  3377. * direct access in memory with previous cached writes through
  3378. * shmemfs and that our cache domain tracking remains valid.
  3379. * For example, if the obj->filp was moved to swap without us
  3380. * being notified and releasing the pages, we would mistakenly
  3381. * continue to assume that the obj remained out of the CPU cached
  3382. * domain.
  3383. */
  3384. ret = i915_gem_object_get_pages(obj);
  3385. if (ret)
  3386. return ret;
  3387. i915_gem_object_flush_cpu_write_domain(obj);
  3388. /* Serialise direct access to this object with the barriers for
  3389. * coherent writes from the GPU, by effectively invalidating the
  3390. * GTT domain upon first access.
  3391. */
  3392. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3393. mb();
  3394. old_write_domain = obj->base.write_domain;
  3395. old_read_domains = obj->base.read_domains;
  3396. /* It should now be out of any other write domains, and we can update
  3397. * the domain values for our changes.
  3398. */
  3399. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  3400. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3401. if (write) {
  3402. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  3403. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  3404. obj->dirty = 1;
  3405. }
  3406. trace_i915_gem_object_change_domain(obj,
  3407. old_read_domains,
  3408. old_write_domain);
  3409. /* And bump the LRU for this access */
  3410. vma = i915_gem_obj_to_ggtt(obj);
  3411. if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
  3412. list_move_tail(&vma->vm_link,
  3413. &ggtt->base.inactive_list);
  3414. return 0;
  3415. }
  3416. /**
  3417. * Changes the cache-level of an object across all VMA.
  3418. * @obj: object to act on
  3419. * @cache_level: new cache level to set for the object
  3420. *
  3421. * After this function returns, the object will be in the new cache-level
  3422. * across all GTT and the contents of the backing storage will be coherent,
  3423. * with respect to the new cache-level. In order to keep the backing storage
  3424. * coherent for all users, we only allow a single cache level to be set
  3425. * globally on the object and prevent it from being changed whilst the
  3426. * hardware is reading from the object. That is if the object is currently
  3427. * on the scanout it will be set to uncached (or equivalent display
  3428. * cache coherency) and all non-MOCS GPU access will also be uncached so
  3429. * that all direct access to the scanout remains coherent.
  3430. */
  3431. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3432. enum i915_cache_level cache_level)
  3433. {
  3434. struct drm_device *dev = obj->base.dev;
  3435. struct i915_vma *vma, *next;
  3436. bool bound = false;
  3437. int ret = 0;
  3438. if (obj->cache_level == cache_level)
  3439. goto out;
  3440. /* Inspect the list of currently bound VMA and unbind any that would
  3441. * be invalid given the new cache-level. This is principally to
  3442. * catch the issue of the CS prefetch crossing page boundaries and
  3443. * reading an invalid PTE on older architectures.
  3444. */
  3445. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
  3446. if (!drm_mm_node_allocated(&vma->node))
  3447. continue;
  3448. if (vma->pin_count) {
  3449. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3450. return -EBUSY;
  3451. }
  3452. if (!i915_gem_valid_gtt_space(vma, cache_level)) {
  3453. ret = i915_vma_unbind(vma);
  3454. if (ret)
  3455. return ret;
  3456. } else
  3457. bound = true;
  3458. }
  3459. /* We can reuse the existing drm_mm nodes but need to change the
  3460. * cache-level on the PTE. We could simply unbind them all and
  3461. * rebind with the correct cache-level on next use. However since
  3462. * we already have a valid slot, dma mapping, pages etc, we may as
  3463. * rewrite the PTE in the belief that doing so tramples upon less
  3464. * state and so involves less work.
  3465. */
  3466. if (bound) {
  3467. /* Before we change the PTE, the GPU must not be accessing it.
  3468. * If we wait upon the object, we know that all the bound
  3469. * VMA are no longer active.
  3470. */
  3471. ret = i915_gem_object_wait_rendering(obj, false);
  3472. if (ret)
  3473. return ret;
  3474. if (!HAS_LLC(dev) && cache_level != I915_CACHE_NONE) {
  3475. /* Access to snoopable pages through the GTT is
  3476. * incoherent and on some machines causes a hard
  3477. * lockup. Relinquish the CPU mmaping to force
  3478. * userspace to refault in the pages and we can
  3479. * then double check if the GTT mapping is still
  3480. * valid for that pointer access.
  3481. */
  3482. i915_gem_release_mmap(obj);
  3483. /* As we no longer need a fence for GTT access,
  3484. * we can relinquish it now (and so prevent having
  3485. * to steal a fence from someone else on the next
  3486. * fence request). Note GPU activity would have
  3487. * dropped the fence as all snoopable access is
  3488. * supposed to be linear.
  3489. */
  3490. ret = i915_gem_object_put_fence(obj);
  3491. if (ret)
  3492. return ret;
  3493. } else {
  3494. /* We either have incoherent backing store and
  3495. * so no GTT access or the architecture is fully
  3496. * coherent. In such cases, existing GTT mmaps
  3497. * ignore the cache bit in the PTE and we can
  3498. * rewrite it without confusing the GPU or having
  3499. * to force userspace to fault back in its mmaps.
  3500. */
  3501. }
  3502. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3503. if (!drm_mm_node_allocated(&vma->node))
  3504. continue;
  3505. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  3506. if (ret)
  3507. return ret;
  3508. }
  3509. }
  3510. list_for_each_entry(vma, &obj->vma_list, obj_link)
  3511. vma->node.color = cache_level;
  3512. obj->cache_level = cache_level;
  3513. out:
  3514. /* Flush the dirty CPU caches to the backing storage so that the
  3515. * object is now coherent at its new cache level (with respect
  3516. * to the access domain).
  3517. */
  3518. if (obj->cache_dirty && cpu_write_needs_clflush(obj)) {
  3519. if (i915_gem_clflush_object(obj, true))
  3520. i915_gem_chipset_flush(to_i915(obj->base.dev));
  3521. }
  3522. return 0;
  3523. }
  3524. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3525. struct drm_file *file)
  3526. {
  3527. struct drm_i915_gem_caching *args = data;
  3528. struct drm_i915_gem_object *obj;
  3529. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  3530. if (&obj->base == NULL)
  3531. return -ENOENT;
  3532. switch (obj->cache_level) {
  3533. case I915_CACHE_LLC:
  3534. case I915_CACHE_L3_LLC:
  3535. args->caching = I915_CACHING_CACHED;
  3536. break;
  3537. case I915_CACHE_WT:
  3538. args->caching = I915_CACHING_DISPLAY;
  3539. break;
  3540. default:
  3541. args->caching = I915_CACHING_NONE;
  3542. break;
  3543. }
  3544. drm_gem_object_unreference_unlocked(&obj->base);
  3545. return 0;
  3546. }
  3547. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3548. struct drm_file *file)
  3549. {
  3550. struct drm_i915_private *dev_priv = to_i915(dev);
  3551. struct drm_i915_gem_caching *args = data;
  3552. struct drm_i915_gem_object *obj;
  3553. enum i915_cache_level level;
  3554. int ret;
  3555. switch (args->caching) {
  3556. case I915_CACHING_NONE:
  3557. level = I915_CACHE_NONE;
  3558. break;
  3559. case I915_CACHING_CACHED:
  3560. /*
  3561. * Due to a HW issue on BXT A stepping, GPU stores via a
  3562. * snooped mapping may leave stale data in a corresponding CPU
  3563. * cacheline, whereas normally such cachelines would get
  3564. * invalidated.
  3565. */
  3566. if (!HAS_LLC(dev) && !HAS_SNOOP(dev))
  3567. return -ENODEV;
  3568. level = I915_CACHE_LLC;
  3569. break;
  3570. case I915_CACHING_DISPLAY:
  3571. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  3572. break;
  3573. default:
  3574. return -EINVAL;
  3575. }
  3576. intel_runtime_pm_get(dev_priv);
  3577. ret = i915_mutex_lock_interruptible(dev);
  3578. if (ret)
  3579. goto rpm_put;
  3580. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  3581. if (&obj->base == NULL) {
  3582. ret = -ENOENT;
  3583. goto unlock;
  3584. }
  3585. ret = i915_gem_object_set_cache_level(obj, level);
  3586. drm_gem_object_unreference(&obj->base);
  3587. unlock:
  3588. mutex_unlock(&dev->struct_mutex);
  3589. rpm_put:
  3590. intel_runtime_pm_put(dev_priv);
  3591. return ret;
  3592. }
  3593. /*
  3594. * Prepare buffer for display plane (scanout, cursors, etc).
  3595. * Can be called from an uninterruptible phase (modesetting) and allows
  3596. * any flushes to be pipelined (for pageflips).
  3597. */
  3598. int
  3599. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3600. u32 alignment,
  3601. const struct i915_ggtt_view *view)
  3602. {
  3603. u32 old_read_domains, old_write_domain;
  3604. int ret;
  3605. /* Mark the pin_display early so that we account for the
  3606. * display coherency whilst setting up the cache domains.
  3607. */
  3608. obj->pin_display++;
  3609. /* The display engine is not coherent with the LLC cache on gen6. As
  3610. * a result, we make sure that the pinning that is about to occur is
  3611. * done with uncached PTEs. This is lowest common denominator for all
  3612. * chipsets.
  3613. *
  3614. * However for gen6+, we could do better by using the GFDT bit instead
  3615. * of uncaching, which would allow us to flush all the LLC-cached data
  3616. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3617. */
  3618. ret = i915_gem_object_set_cache_level(obj,
  3619. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  3620. if (ret)
  3621. goto err_unpin_display;
  3622. /* As the user may map the buffer once pinned in the display plane
  3623. * (e.g. libkms for the bootup splash), we have to ensure that we
  3624. * always use map_and_fenceable for all scanout buffers.
  3625. */
  3626. ret = i915_gem_object_ggtt_pin(obj, view, alignment,
  3627. view->type == I915_GGTT_VIEW_NORMAL ?
  3628. PIN_MAPPABLE : 0);
  3629. if (ret)
  3630. goto err_unpin_display;
  3631. i915_gem_object_flush_cpu_write_domain(obj);
  3632. old_write_domain = obj->base.write_domain;
  3633. old_read_domains = obj->base.read_domains;
  3634. /* It should now be out of any other write domains, and we can update
  3635. * the domain values for our changes.
  3636. */
  3637. obj->base.write_domain = 0;
  3638. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3639. trace_i915_gem_object_change_domain(obj,
  3640. old_read_domains,
  3641. old_write_domain);
  3642. return 0;
  3643. err_unpin_display:
  3644. obj->pin_display--;
  3645. return ret;
  3646. }
  3647. void
  3648. i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
  3649. const struct i915_ggtt_view *view)
  3650. {
  3651. if (WARN_ON(obj->pin_display == 0))
  3652. return;
  3653. i915_gem_object_ggtt_unpin_view(obj, view);
  3654. obj->pin_display--;
  3655. }
  3656. /**
  3657. * Moves a single object to the CPU read, and possibly write domain.
  3658. * @obj: object to act on
  3659. * @write: requesting write or read-only access
  3660. *
  3661. * This function returns when the move is complete, including waiting on
  3662. * flushes to occur.
  3663. */
  3664. int
  3665. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3666. {
  3667. uint32_t old_write_domain, old_read_domains;
  3668. int ret;
  3669. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3670. return 0;
  3671. ret = i915_gem_object_wait_rendering(obj, !write);
  3672. if (ret)
  3673. return ret;
  3674. i915_gem_object_flush_gtt_write_domain(obj);
  3675. old_write_domain = obj->base.write_domain;
  3676. old_read_domains = obj->base.read_domains;
  3677. /* Flush the CPU cache if it's still invalid. */
  3678. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3679. i915_gem_clflush_object(obj, false);
  3680. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3681. }
  3682. /* It should now be out of any other write domains, and we can update
  3683. * the domain values for our changes.
  3684. */
  3685. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3686. /* If we're writing through the CPU, then the GPU read domains will
  3687. * need to be invalidated at next use.
  3688. */
  3689. if (write) {
  3690. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3691. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3692. }
  3693. trace_i915_gem_object_change_domain(obj,
  3694. old_read_domains,
  3695. old_write_domain);
  3696. return 0;
  3697. }
  3698. /* Throttle our rendering by waiting until the ring has completed our requests
  3699. * emitted over 20 msec ago.
  3700. *
  3701. * Note that if we were to use the current jiffies each time around the loop,
  3702. * we wouldn't escape the function with any frames outstanding if the time to
  3703. * render a frame was over 20ms.
  3704. *
  3705. * This should get us reasonable parallelism between CPU and GPU but also
  3706. * relatively low latency when blocking on a particular request to finish.
  3707. */
  3708. static int
  3709. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3710. {
  3711. struct drm_i915_private *dev_priv = to_i915(dev);
  3712. struct drm_i915_file_private *file_priv = file->driver_priv;
  3713. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3714. struct drm_i915_gem_request *request, *target = NULL;
  3715. int ret;
  3716. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3717. if (ret)
  3718. return ret;
  3719. /* ABI: return -EIO if already wedged */
  3720. if (i915_terminally_wedged(&dev_priv->gpu_error))
  3721. return -EIO;
  3722. spin_lock(&file_priv->mm.lock);
  3723. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3724. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3725. break;
  3726. /*
  3727. * Note that the request might not have been submitted yet.
  3728. * In which case emitted_jiffies will be zero.
  3729. */
  3730. if (!request->emitted_jiffies)
  3731. continue;
  3732. target = request;
  3733. }
  3734. if (target)
  3735. i915_gem_request_reference(target);
  3736. spin_unlock(&file_priv->mm.lock);
  3737. if (target == NULL)
  3738. return 0;
  3739. ret = __i915_wait_request(target, true, NULL, NULL);
  3740. i915_gem_request_unreference(target);
  3741. return ret;
  3742. }
  3743. static bool
  3744. i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
  3745. {
  3746. struct drm_i915_gem_object *obj = vma->obj;
  3747. if (alignment &&
  3748. vma->node.start & (alignment - 1))
  3749. return true;
  3750. if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
  3751. return true;
  3752. if (flags & PIN_OFFSET_BIAS &&
  3753. vma->node.start < (flags & PIN_OFFSET_MASK))
  3754. return true;
  3755. if (flags & PIN_OFFSET_FIXED &&
  3756. vma->node.start != (flags & PIN_OFFSET_MASK))
  3757. return true;
  3758. return false;
  3759. }
  3760. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
  3761. {
  3762. struct drm_i915_gem_object *obj = vma->obj;
  3763. bool mappable, fenceable;
  3764. u32 fence_size, fence_alignment;
  3765. fence_size = i915_gem_get_gtt_size(obj->base.dev,
  3766. obj->base.size,
  3767. obj->tiling_mode);
  3768. fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
  3769. obj->base.size,
  3770. obj->tiling_mode,
  3771. true);
  3772. fenceable = (vma->node.size == fence_size &&
  3773. (vma->node.start & (fence_alignment - 1)) == 0);
  3774. mappable = (vma->node.start + fence_size <=
  3775. to_i915(obj->base.dev)->ggtt.mappable_end);
  3776. obj->map_and_fenceable = mappable && fenceable;
  3777. }
  3778. static int
  3779. i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
  3780. struct i915_address_space *vm,
  3781. const struct i915_ggtt_view *ggtt_view,
  3782. uint32_t alignment,
  3783. uint64_t flags)
  3784. {
  3785. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  3786. struct i915_vma *vma;
  3787. unsigned bound;
  3788. int ret;
  3789. if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
  3790. return -ENODEV;
  3791. if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
  3792. return -EINVAL;
  3793. if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
  3794. return -EINVAL;
  3795. if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
  3796. return -EINVAL;
  3797. vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
  3798. i915_gem_obj_to_vma(obj, vm);
  3799. if (vma) {
  3800. if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3801. return -EBUSY;
  3802. if (i915_vma_misplaced(vma, alignment, flags)) {
  3803. WARN(vma->pin_count,
  3804. "bo is already pinned in %s with incorrect alignment:"
  3805. " offset=%08x %08x, req.alignment=%x, req.map_and_fenceable=%d,"
  3806. " obj->map_and_fenceable=%d\n",
  3807. ggtt_view ? "ggtt" : "ppgtt",
  3808. upper_32_bits(vma->node.start),
  3809. lower_32_bits(vma->node.start),
  3810. alignment,
  3811. !!(flags & PIN_MAPPABLE),
  3812. obj->map_and_fenceable);
  3813. ret = i915_vma_unbind(vma);
  3814. if (ret)
  3815. return ret;
  3816. vma = NULL;
  3817. }
  3818. }
  3819. bound = vma ? vma->bound : 0;
  3820. if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
  3821. vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
  3822. flags);
  3823. if (IS_ERR(vma))
  3824. return PTR_ERR(vma);
  3825. } else {
  3826. ret = i915_vma_bind(vma, obj->cache_level, flags);
  3827. if (ret)
  3828. return ret;
  3829. }
  3830. if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
  3831. (bound ^ vma->bound) & GLOBAL_BIND) {
  3832. __i915_vma_set_map_and_fenceable(vma);
  3833. WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
  3834. }
  3835. vma->pin_count++;
  3836. return 0;
  3837. }
  3838. int
  3839. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3840. struct i915_address_space *vm,
  3841. uint32_t alignment,
  3842. uint64_t flags)
  3843. {
  3844. return i915_gem_object_do_pin(obj, vm,
  3845. i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
  3846. alignment, flags);
  3847. }
  3848. int
  3849. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3850. const struct i915_ggtt_view *view,
  3851. uint32_t alignment,
  3852. uint64_t flags)
  3853. {
  3854. struct drm_device *dev = obj->base.dev;
  3855. struct drm_i915_private *dev_priv = to_i915(dev);
  3856. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  3857. BUG_ON(!view);
  3858. return i915_gem_object_do_pin(obj, &ggtt->base, view,
  3859. alignment, flags | PIN_GLOBAL);
  3860. }
  3861. void
  3862. i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
  3863. const struct i915_ggtt_view *view)
  3864. {
  3865. struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
  3866. WARN_ON(vma->pin_count == 0);
  3867. WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
  3868. --vma->pin_count;
  3869. }
  3870. int
  3871. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3872. struct drm_file *file)
  3873. {
  3874. struct drm_i915_gem_busy *args = data;
  3875. struct drm_i915_gem_object *obj;
  3876. int ret;
  3877. ret = i915_mutex_lock_interruptible(dev);
  3878. if (ret)
  3879. return ret;
  3880. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  3881. if (&obj->base == NULL) {
  3882. ret = -ENOENT;
  3883. goto unlock;
  3884. }
  3885. /* Count all active objects as busy, even if they are currently not used
  3886. * by the gpu. Users of this interface expect objects to eventually
  3887. * become non-busy without any further actions, therefore emit any
  3888. * necessary flushes here.
  3889. */
  3890. ret = i915_gem_object_flush_active(obj);
  3891. if (ret)
  3892. goto unref;
  3893. args->busy = 0;
  3894. if (obj->active) {
  3895. int i;
  3896. for (i = 0; i < I915_NUM_ENGINES; i++) {
  3897. struct drm_i915_gem_request *req;
  3898. req = obj->last_read_req[i];
  3899. if (req)
  3900. args->busy |= 1 << (16 + req->engine->exec_id);
  3901. }
  3902. if (obj->last_write_req)
  3903. args->busy |= obj->last_write_req->engine->exec_id;
  3904. }
  3905. unref:
  3906. drm_gem_object_unreference(&obj->base);
  3907. unlock:
  3908. mutex_unlock(&dev->struct_mutex);
  3909. return ret;
  3910. }
  3911. int
  3912. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3913. struct drm_file *file_priv)
  3914. {
  3915. return i915_gem_ring_throttle(dev, file_priv);
  3916. }
  3917. int
  3918. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3919. struct drm_file *file_priv)
  3920. {
  3921. struct drm_i915_private *dev_priv = to_i915(dev);
  3922. struct drm_i915_gem_madvise *args = data;
  3923. struct drm_i915_gem_object *obj;
  3924. int ret;
  3925. switch (args->madv) {
  3926. case I915_MADV_DONTNEED:
  3927. case I915_MADV_WILLNEED:
  3928. break;
  3929. default:
  3930. return -EINVAL;
  3931. }
  3932. ret = i915_mutex_lock_interruptible(dev);
  3933. if (ret)
  3934. return ret;
  3935. obj = to_intel_bo(drm_gem_object_lookup(file_priv, args->handle));
  3936. if (&obj->base == NULL) {
  3937. ret = -ENOENT;
  3938. goto unlock;
  3939. }
  3940. if (i915_gem_obj_is_pinned(obj)) {
  3941. ret = -EINVAL;
  3942. goto out;
  3943. }
  3944. if (obj->pages &&
  3945. obj->tiling_mode != I915_TILING_NONE &&
  3946. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3947. if (obj->madv == I915_MADV_WILLNEED)
  3948. i915_gem_object_unpin_pages(obj);
  3949. if (args->madv == I915_MADV_WILLNEED)
  3950. i915_gem_object_pin_pages(obj);
  3951. }
  3952. if (obj->madv != __I915_MADV_PURGED)
  3953. obj->madv = args->madv;
  3954. /* if the object is no longer attached, discard its backing storage */
  3955. if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
  3956. i915_gem_object_truncate(obj);
  3957. args->retained = obj->madv != __I915_MADV_PURGED;
  3958. out:
  3959. drm_gem_object_unreference(&obj->base);
  3960. unlock:
  3961. mutex_unlock(&dev->struct_mutex);
  3962. return ret;
  3963. }
  3964. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3965. const struct drm_i915_gem_object_ops *ops)
  3966. {
  3967. int i;
  3968. INIT_LIST_HEAD(&obj->global_list);
  3969. for (i = 0; i < I915_NUM_ENGINES; i++)
  3970. INIT_LIST_HEAD(&obj->engine_list[i]);
  3971. INIT_LIST_HEAD(&obj->obj_exec_link);
  3972. INIT_LIST_HEAD(&obj->vma_list);
  3973. INIT_LIST_HEAD(&obj->batch_pool_link);
  3974. obj->ops = ops;
  3975. obj->fence_reg = I915_FENCE_REG_NONE;
  3976. obj->madv = I915_MADV_WILLNEED;
  3977. i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
  3978. }
  3979. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3980. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
  3981. .get_pages = i915_gem_object_get_pages_gtt,
  3982. .put_pages = i915_gem_object_put_pages_gtt,
  3983. };
  3984. struct drm_i915_gem_object *i915_gem_object_create(struct drm_device *dev,
  3985. size_t size)
  3986. {
  3987. struct drm_i915_gem_object *obj;
  3988. struct address_space *mapping;
  3989. gfp_t mask;
  3990. int ret;
  3991. obj = i915_gem_object_alloc(dev);
  3992. if (obj == NULL)
  3993. return ERR_PTR(-ENOMEM);
  3994. ret = drm_gem_object_init(dev, &obj->base, size);
  3995. if (ret)
  3996. goto fail;
  3997. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3998. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3999. /* 965gm cannot relocate objects above 4GiB. */
  4000. mask &= ~__GFP_HIGHMEM;
  4001. mask |= __GFP_DMA32;
  4002. }
  4003. mapping = obj->base.filp->f_mapping;
  4004. mapping_set_gfp_mask(mapping, mask);
  4005. i915_gem_object_init(obj, &i915_gem_object_ops);
  4006. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  4007. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  4008. if (HAS_LLC(dev)) {
  4009. /* On some devices, we can have the GPU use the LLC (the CPU
  4010. * cache) for about a 10% performance improvement
  4011. * compared to uncached. Graphics requests other than
  4012. * display scanout are coherent with the CPU in
  4013. * accessing this cache. This means in this mode we
  4014. * don't need to clflush on the CPU side, and on the
  4015. * GPU side we only need to flush internal caches to
  4016. * get data visible to the CPU.
  4017. *
  4018. * However, we maintain the display planes as UC, and so
  4019. * need to rebind when first used as such.
  4020. */
  4021. obj->cache_level = I915_CACHE_LLC;
  4022. } else
  4023. obj->cache_level = I915_CACHE_NONE;
  4024. trace_i915_gem_object_create(obj);
  4025. return obj;
  4026. fail:
  4027. i915_gem_object_free(obj);
  4028. return ERR_PTR(ret);
  4029. }
  4030. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  4031. {
  4032. /* If we are the last user of the backing storage (be it shmemfs
  4033. * pages or stolen etc), we know that the pages are going to be
  4034. * immediately released. In this case, we can then skip copying
  4035. * back the contents from the GPU.
  4036. */
  4037. if (obj->madv != I915_MADV_WILLNEED)
  4038. return false;
  4039. if (obj->base.filp == NULL)
  4040. return true;
  4041. /* At first glance, this looks racy, but then again so would be
  4042. * userspace racing mmap against close. However, the first external
  4043. * reference to the filp can only be obtained through the
  4044. * i915_gem_mmap_ioctl() which safeguards us against the user
  4045. * acquiring such a reference whilst we are in the middle of
  4046. * freeing the object.
  4047. */
  4048. return atomic_long_read(&obj->base.filp->f_count) == 1;
  4049. }
  4050. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  4051. {
  4052. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  4053. struct drm_device *dev = obj->base.dev;
  4054. struct drm_i915_private *dev_priv = to_i915(dev);
  4055. struct i915_vma *vma, *next;
  4056. intel_runtime_pm_get(dev_priv);
  4057. trace_i915_gem_object_destroy(obj);
  4058. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
  4059. int ret;
  4060. vma->pin_count = 0;
  4061. ret = i915_vma_unbind(vma);
  4062. if (WARN_ON(ret == -ERESTARTSYS)) {
  4063. bool was_interruptible;
  4064. was_interruptible = dev_priv->mm.interruptible;
  4065. dev_priv->mm.interruptible = false;
  4066. WARN_ON(i915_vma_unbind(vma));
  4067. dev_priv->mm.interruptible = was_interruptible;
  4068. }
  4069. }
  4070. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  4071. * before progressing. */
  4072. if (obj->stolen)
  4073. i915_gem_object_unpin_pages(obj);
  4074. WARN_ON(obj->frontbuffer_bits);
  4075. if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
  4076. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
  4077. obj->tiling_mode != I915_TILING_NONE)
  4078. i915_gem_object_unpin_pages(obj);
  4079. if (WARN_ON(obj->pages_pin_count))
  4080. obj->pages_pin_count = 0;
  4081. if (discard_backing_storage(obj))
  4082. obj->madv = I915_MADV_DONTNEED;
  4083. i915_gem_object_put_pages(obj);
  4084. i915_gem_object_free_mmap_offset(obj);
  4085. BUG_ON(obj->pages);
  4086. if (obj->base.import_attach)
  4087. drm_prime_gem_destroy(&obj->base, NULL);
  4088. if (obj->ops->release)
  4089. obj->ops->release(obj);
  4090. drm_gem_object_release(&obj->base);
  4091. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  4092. kfree(obj->bit_17);
  4093. i915_gem_object_free(obj);
  4094. intel_runtime_pm_put(dev_priv);
  4095. }
  4096. struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
  4097. struct i915_address_space *vm)
  4098. {
  4099. struct i915_vma *vma;
  4100. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  4101. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL &&
  4102. vma->vm == vm)
  4103. return vma;
  4104. }
  4105. return NULL;
  4106. }
  4107. struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
  4108. const struct i915_ggtt_view *view)
  4109. {
  4110. struct i915_vma *vma;
  4111. GEM_BUG_ON(!view);
  4112. list_for_each_entry(vma, &obj->vma_list, obj_link)
  4113. if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
  4114. return vma;
  4115. return NULL;
  4116. }
  4117. void i915_gem_vma_destroy(struct i915_vma *vma)
  4118. {
  4119. WARN_ON(vma->node.allocated);
  4120. /* Keep the vma as a placeholder in the execbuffer reservation lists */
  4121. if (!list_empty(&vma->exec_list))
  4122. return;
  4123. if (!vma->is_ggtt)
  4124. i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
  4125. list_del(&vma->obj_link);
  4126. kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
  4127. }
  4128. static void
  4129. i915_gem_stop_engines(struct drm_device *dev)
  4130. {
  4131. struct drm_i915_private *dev_priv = to_i915(dev);
  4132. struct intel_engine_cs *engine;
  4133. for_each_engine(engine, dev_priv)
  4134. dev_priv->gt.stop_engine(engine);
  4135. }
  4136. int
  4137. i915_gem_suspend(struct drm_device *dev)
  4138. {
  4139. struct drm_i915_private *dev_priv = to_i915(dev);
  4140. int ret = 0;
  4141. mutex_lock(&dev->struct_mutex);
  4142. ret = i915_gem_wait_for_idle(dev_priv);
  4143. if (ret)
  4144. goto err;
  4145. i915_gem_retire_requests(dev_priv);
  4146. i915_gem_stop_engines(dev);
  4147. i915_gem_context_lost(dev_priv);
  4148. mutex_unlock(&dev->struct_mutex);
  4149. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  4150. cancel_delayed_work_sync(&dev_priv->gt.retire_work);
  4151. flush_delayed_work(&dev_priv->gt.idle_work);
  4152. /* Assert that we sucessfully flushed all the work and
  4153. * reset the GPU back to its idle, low power state.
  4154. */
  4155. WARN_ON(dev_priv->gt.awake);
  4156. return 0;
  4157. err:
  4158. mutex_unlock(&dev->struct_mutex);
  4159. return ret;
  4160. }
  4161. void i915_gem_init_swizzling(struct drm_device *dev)
  4162. {
  4163. struct drm_i915_private *dev_priv = to_i915(dev);
  4164. if (INTEL_INFO(dev)->gen < 5 ||
  4165. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  4166. return;
  4167. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  4168. DISP_TILE_SURFACE_SWIZZLING);
  4169. if (IS_GEN5(dev))
  4170. return;
  4171. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  4172. if (IS_GEN6(dev))
  4173. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  4174. else if (IS_GEN7(dev))
  4175. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  4176. else if (IS_GEN8(dev))
  4177. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  4178. else
  4179. BUG();
  4180. }
  4181. static void init_unused_ring(struct drm_device *dev, u32 base)
  4182. {
  4183. struct drm_i915_private *dev_priv = to_i915(dev);
  4184. I915_WRITE(RING_CTL(base), 0);
  4185. I915_WRITE(RING_HEAD(base), 0);
  4186. I915_WRITE(RING_TAIL(base), 0);
  4187. I915_WRITE(RING_START(base), 0);
  4188. }
  4189. static void init_unused_rings(struct drm_device *dev)
  4190. {
  4191. if (IS_I830(dev)) {
  4192. init_unused_ring(dev, PRB1_BASE);
  4193. init_unused_ring(dev, SRB0_BASE);
  4194. init_unused_ring(dev, SRB1_BASE);
  4195. init_unused_ring(dev, SRB2_BASE);
  4196. init_unused_ring(dev, SRB3_BASE);
  4197. } else if (IS_GEN2(dev)) {
  4198. init_unused_ring(dev, SRB0_BASE);
  4199. init_unused_ring(dev, SRB1_BASE);
  4200. } else if (IS_GEN3(dev)) {
  4201. init_unused_ring(dev, PRB1_BASE);
  4202. init_unused_ring(dev, PRB2_BASE);
  4203. }
  4204. }
  4205. int i915_gem_init_engines(struct drm_device *dev)
  4206. {
  4207. struct drm_i915_private *dev_priv = to_i915(dev);
  4208. int ret;
  4209. ret = intel_init_render_ring_buffer(dev);
  4210. if (ret)
  4211. return ret;
  4212. if (HAS_BSD(dev)) {
  4213. ret = intel_init_bsd_ring_buffer(dev);
  4214. if (ret)
  4215. goto cleanup_render_ring;
  4216. }
  4217. if (HAS_BLT(dev)) {
  4218. ret = intel_init_blt_ring_buffer(dev);
  4219. if (ret)
  4220. goto cleanup_bsd_ring;
  4221. }
  4222. if (HAS_VEBOX(dev)) {
  4223. ret = intel_init_vebox_ring_buffer(dev);
  4224. if (ret)
  4225. goto cleanup_blt_ring;
  4226. }
  4227. if (HAS_BSD2(dev)) {
  4228. ret = intel_init_bsd2_ring_buffer(dev);
  4229. if (ret)
  4230. goto cleanup_vebox_ring;
  4231. }
  4232. return 0;
  4233. cleanup_vebox_ring:
  4234. intel_cleanup_engine(&dev_priv->engine[VECS]);
  4235. cleanup_blt_ring:
  4236. intel_cleanup_engine(&dev_priv->engine[BCS]);
  4237. cleanup_bsd_ring:
  4238. intel_cleanup_engine(&dev_priv->engine[VCS]);
  4239. cleanup_render_ring:
  4240. intel_cleanup_engine(&dev_priv->engine[RCS]);
  4241. return ret;
  4242. }
  4243. int
  4244. i915_gem_init_hw(struct drm_device *dev)
  4245. {
  4246. struct drm_i915_private *dev_priv = to_i915(dev);
  4247. struct intel_engine_cs *engine;
  4248. int ret;
  4249. /* Double layer security blanket, see i915_gem_init() */
  4250. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4251. if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
  4252. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  4253. if (IS_HASWELL(dev))
  4254. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  4255. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  4256. if (HAS_PCH_NOP(dev)) {
  4257. if (IS_IVYBRIDGE(dev)) {
  4258. u32 temp = I915_READ(GEN7_MSG_CTL);
  4259. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  4260. I915_WRITE(GEN7_MSG_CTL, temp);
  4261. } else if (INTEL_INFO(dev)->gen >= 7) {
  4262. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  4263. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4264. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  4265. }
  4266. }
  4267. i915_gem_init_swizzling(dev);
  4268. /*
  4269. * At least 830 can leave some of the unused rings
  4270. * "active" (ie. head != tail) after resume which
  4271. * will prevent c3 entry. Makes sure all unused rings
  4272. * are totally idle.
  4273. */
  4274. init_unused_rings(dev);
  4275. BUG_ON(!dev_priv->kernel_context);
  4276. ret = i915_ppgtt_init_hw(dev);
  4277. if (ret) {
  4278. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  4279. goto out;
  4280. }
  4281. /* Need to do basic initialisation of all rings first: */
  4282. for_each_engine(engine, dev_priv) {
  4283. ret = engine->init_hw(engine);
  4284. if (ret)
  4285. goto out;
  4286. }
  4287. intel_mocs_init_l3cc_table(dev);
  4288. /* We can't enable contexts until all firmware is loaded */
  4289. ret = intel_guc_setup(dev);
  4290. if (ret)
  4291. goto out;
  4292. out:
  4293. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4294. return ret;
  4295. }
  4296. int i915_gem_init(struct drm_device *dev)
  4297. {
  4298. struct drm_i915_private *dev_priv = to_i915(dev);
  4299. int ret;
  4300. mutex_lock(&dev->struct_mutex);
  4301. if (!i915.enable_execlists) {
  4302. dev_priv->gt.execbuf_submit = i915_gem_ringbuffer_submission;
  4303. dev_priv->gt.init_engines = i915_gem_init_engines;
  4304. dev_priv->gt.cleanup_engine = intel_cleanup_engine;
  4305. dev_priv->gt.stop_engine = intel_stop_engine;
  4306. } else {
  4307. dev_priv->gt.execbuf_submit = intel_execlists_submission;
  4308. dev_priv->gt.init_engines = intel_logical_rings_init;
  4309. dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
  4310. dev_priv->gt.stop_engine = intel_logical_ring_stop;
  4311. }
  4312. /* This is just a security blanket to placate dragons.
  4313. * On some systems, we very sporadically observe that the first TLBs
  4314. * used by the CS may be stale, despite us poking the TLB reset. If
  4315. * we hold the forcewake during initialisation these problems
  4316. * just magically go away.
  4317. */
  4318. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4319. i915_gem_init_userptr(dev_priv);
  4320. i915_gem_init_ggtt(dev);
  4321. ret = i915_gem_context_init(dev);
  4322. if (ret)
  4323. goto out_unlock;
  4324. ret = dev_priv->gt.init_engines(dev);
  4325. if (ret)
  4326. goto out_unlock;
  4327. ret = i915_gem_init_hw(dev);
  4328. if (ret == -EIO) {
  4329. /* Allow ring initialisation to fail by marking the GPU as
  4330. * wedged. But we only want to do this where the GPU is angry,
  4331. * for all other failure, such as an allocation failure, bail.
  4332. */
  4333. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4334. atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
  4335. ret = 0;
  4336. }
  4337. out_unlock:
  4338. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4339. mutex_unlock(&dev->struct_mutex);
  4340. return ret;
  4341. }
  4342. void
  4343. i915_gem_cleanup_engines(struct drm_device *dev)
  4344. {
  4345. struct drm_i915_private *dev_priv = to_i915(dev);
  4346. struct intel_engine_cs *engine;
  4347. for_each_engine(engine, dev_priv)
  4348. dev_priv->gt.cleanup_engine(engine);
  4349. }
  4350. static void
  4351. init_engine_lists(struct intel_engine_cs *engine)
  4352. {
  4353. INIT_LIST_HEAD(&engine->active_list);
  4354. INIT_LIST_HEAD(&engine->request_list);
  4355. }
  4356. void
  4357. i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  4358. {
  4359. struct drm_device *dev = &dev_priv->drm;
  4360. if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
  4361. !IS_CHERRYVIEW(dev_priv))
  4362. dev_priv->num_fence_regs = 32;
  4363. else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
  4364. IS_I945GM(dev_priv) || IS_G33(dev_priv))
  4365. dev_priv->num_fence_regs = 16;
  4366. else
  4367. dev_priv->num_fence_regs = 8;
  4368. if (intel_vgpu_active(dev_priv))
  4369. dev_priv->num_fence_regs =
  4370. I915_READ(vgtif_reg(avail_rs.fence_num));
  4371. /* Initialize fence registers to zero */
  4372. i915_gem_restore_fences(dev);
  4373. i915_gem_detect_bit_6_swizzle(dev);
  4374. }
  4375. void
  4376. i915_gem_load_init(struct drm_device *dev)
  4377. {
  4378. struct drm_i915_private *dev_priv = to_i915(dev);
  4379. int i;
  4380. dev_priv->objects =
  4381. kmem_cache_create("i915_gem_object",
  4382. sizeof(struct drm_i915_gem_object), 0,
  4383. SLAB_HWCACHE_ALIGN,
  4384. NULL);
  4385. dev_priv->vmas =
  4386. kmem_cache_create("i915_gem_vma",
  4387. sizeof(struct i915_vma), 0,
  4388. SLAB_HWCACHE_ALIGN,
  4389. NULL);
  4390. dev_priv->requests =
  4391. kmem_cache_create("i915_gem_request",
  4392. sizeof(struct drm_i915_gem_request), 0,
  4393. SLAB_HWCACHE_ALIGN,
  4394. NULL);
  4395. INIT_LIST_HEAD(&dev_priv->vm_list);
  4396. INIT_LIST_HEAD(&dev_priv->context_list);
  4397. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4398. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4399. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4400. for (i = 0; i < I915_NUM_ENGINES; i++)
  4401. init_engine_lists(&dev_priv->engine[i]);
  4402. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  4403. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4404. INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
  4405. i915_gem_retire_work_handler);
  4406. INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
  4407. i915_gem_idle_work_handler);
  4408. init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
  4409. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4410. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  4411. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4412. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4413. dev_priv->mm.interruptible = true;
  4414. mutex_init(&dev_priv->fb_tracking.lock);
  4415. }
  4416. void i915_gem_load_cleanup(struct drm_device *dev)
  4417. {
  4418. struct drm_i915_private *dev_priv = to_i915(dev);
  4419. kmem_cache_destroy(dev_priv->requests);
  4420. kmem_cache_destroy(dev_priv->vmas);
  4421. kmem_cache_destroy(dev_priv->objects);
  4422. }
  4423. int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  4424. {
  4425. struct drm_i915_gem_object *obj;
  4426. /* Called just before we write the hibernation image.
  4427. *
  4428. * We need to update the domain tracking to reflect that the CPU
  4429. * will be accessing all the pages to create and restore from the
  4430. * hibernation, and so upon restoration those pages will be in the
  4431. * CPU domain.
  4432. *
  4433. * To make sure the hibernation image contains the latest state,
  4434. * we update that state just before writing out the image.
  4435. */
  4436. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
  4437. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  4438. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  4439. }
  4440. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  4441. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  4442. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  4443. }
  4444. return 0;
  4445. }
  4446. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4447. {
  4448. struct drm_i915_file_private *file_priv = file->driver_priv;
  4449. /* Clean up our request list when the client is going away, so that
  4450. * later retire_requests won't dereference our soon-to-be-gone
  4451. * file_priv.
  4452. */
  4453. spin_lock(&file_priv->mm.lock);
  4454. while (!list_empty(&file_priv->mm.request_list)) {
  4455. struct drm_i915_gem_request *request;
  4456. request = list_first_entry(&file_priv->mm.request_list,
  4457. struct drm_i915_gem_request,
  4458. client_list);
  4459. list_del(&request->client_list);
  4460. request->file_priv = NULL;
  4461. }
  4462. spin_unlock(&file_priv->mm.lock);
  4463. if (!list_empty(&file_priv->rps.link)) {
  4464. spin_lock(&to_i915(dev)->rps.client_lock);
  4465. list_del(&file_priv->rps.link);
  4466. spin_unlock(&to_i915(dev)->rps.client_lock);
  4467. }
  4468. }
  4469. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4470. {
  4471. struct drm_i915_file_private *file_priv;
  4472. int ret;
  4473. DRM_DEBUG_DRIVER("\n");
  4474. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4475. if (!file_priv)
  4476. return -ENOMEM;
  4477. file->driver_priv = file_priv;
  4478. file_priv->dev_priv = to_i915(dev);
  4479. file_priv->file = file;
  4480. INIT_LIST_HEAD(&file_priv->rps.link);
  4481. spin_lock_init(&file_priv->mm.lock);
  4482. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4483. file_priv->bsd_ring = -1;
  4484. ret = i915_gem_context_open(dev, file);
  4485. if (ret)
  4486. kfree(file_priv);
  4487. return ret;
  4488. }
  4489. /**
  4490. * i915_gem_track_fb - update frontbuffer tracking
  4491. * @old: current GEM buffer for the frontbuffer slots
  4492. * @new: new GEM buffer for the frontbuffer slots
  4493. * @frontbuffer_bits: bitmask of frontbuffer slots
  4494. *
  4495. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4496. * from @old and setting them in @new. Both @old and @new can be NULL.
  4497. */
  4498. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4499. struct drm_i915_gem_object *new,
  4500. unsigned frontbuffer_bits)
  4501. {
  4502. if (old) {
  4503. WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
  4504. WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
  4505. old->frontbuffer_bits &= ~frontbuffer_bits;
  4506. }
  4507. if (new) {
  4508. WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
  4509. WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
  4510. new->frontbuffer_bits |= frontbuffer_bits;
  4511. }
  4512. }
  4513. /* All the new VM stuff */
  4514. u64 i915_gem_obj_offset(struct drm_i915_gem_object *o,
  4515. struct i915_address_space *vm)
  4516. {
  4517. struct drm_i915_private *dev_priv = to_i915(o->base.dev);
  4518. struct i915_vma *vma;
  4519. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4520. list_for_each_entry(vma, &o->vma_list, obj_link) {
  4521. if (vma->is_ggtt &&
  4522. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4523. continue;
  4524. if (vma->vm == vm)
  4525. return vma->node.start;
  4526. }
  4527. WARN(1, "%s vma for this object not found.\n",
  4528. i915_is_ggtt(vm) ? "global" : "ppgtt");
  4529. return -1;
  4530. }
  4531. u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
  4532. const struct i915_ggtt_view *view)
  4533. {
  4534. struct i915_vma *vma;
  4535. list_for_each_entry(vma, &o->vma_list, obj_link)
  4536. if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
  4537. return vma->node.start;
  4538. WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
  4539. return -1;
  4540. }
  4541. bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
  4542. struct i915_address_space *vm)
  4543. {
  4544. struct i915_vma *vma;
  4545. list_for_each_entry(vma, &o->vma_list, obj_link) {
  4546. if (vma->is_ggtt &&
  4547. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4548. continue;
  4549. if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
  4550. return true;
  4551. }
  4552. return false;
  4553. }
  4554. bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
  4555. const struct i915_ggtt_view *view)
  4556. {
  4557. struct i915_vma *vma;
  4558. list_for_each_entry(vma, &o->vma_list, obj_link)
  4559. if (vma->is_ggtt &&
  4560. i915_ggtt_view_equal(&vma->ggtt_view, view) &&
  4561. drm_mm_node_allocated(&vma->node))
  4562. return true;
  4563. return false;
  4564. }
  4565. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  4566. {
  4567. struct i915_vma *vma;
  4568. list_for_each_entry(vma, &o->vma_list, obj_link)
  4569. if (drm_mm_node_allocated(&vma->node))
  4570. return true;
  4571. return false;
  4572. }
  4573. unsigned long i915_gem_obj_ggtt_size(struct drm_i915_gem_object *o)
  4574. {
  4575. struct i915_vma *vma;
  4576. GEM_BUG_ON(list_empty(&o->vma_list));
  4577. list_for_each_entry(vma, &o->vma_list, obj_link) {
  4578. if (vma->is_ggtt &&
  4579. vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
  4580. return vma->node.size;
  4581. }
  4582. return 0;
  4583. }
  4584. bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
  4585. {
  4586. struct i915_vma *vma;
  4587. list_for_each_entry(vma, &obj->vma_list, obj_link)
  4588. if (vma->pin_count > 0)
  4589. return true;
  4590. return false;
  4591. }
  4592. /* Like i915_gem_object_get_page(), but mark the returned page dirty */
  4593. struct page *
  4594. i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
  4595. {
  4596. struct page *page;
  4597. /* Only default objects have per-page dirty tracking */
  4598. if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
  4599. return NULL;
  4600. page = i915_gem_object_get_page(obj, n);
  4601. set_page_dirty(page);
  4602. return page;
  4603. }
  4604. /* Allocate a new GEM object and fill it with the supplied data */
  4605. struct drm_i915_gem_object *
  4606. i915_gem_object_create_from_data(struct drm_device *dev,
  4607. const void *data, size_t size)
  4608. {
  4609. struct drm_i915_gem_object *obj;
  4610. struct sg_table *sg;
  4611. size_t bytes;
  4612. int ret;
  4613. obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE));
  4614. if (IS_ERR(obj))
  4615. return obj;
  4616. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  4617. if (ret)
  4618. goto fail;
  4619. ret = i915_gem_object_get_pages(obj);
  4620. if (ret)
  4621. goto fail;
  4622. i915_gem_object_pin_pages(obj);
  4623. sg = obj->pages;
  4624. bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
  4625. obj->dirty = 1; /* Backing store is now out of date */
  4626. i915_gem_object_unpin_pages(obj);
  4627. if (WARN_ON(bytes != size)) {
  4628. DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
  4629. ret = -EFAULT;
  4630. goto fail;
  4631. }
  4632. return obj;
  4633. fail:
  4634. drm_gem_object_unreference(&obj->base);
  4635. return ERR_PTR(ret);
  4636. }