i915_gem.c 127 KB

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