i915_gem.c 127 KB

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