i915_gem.c 122 KB

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