i915_gem.c 136 KB

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