i915_gem.c 136 KB

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