i915_gem.c 132 KB

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