i915_gem.c 136 KB

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