i915_gem.c 136 KB

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