i915_gem.c 122 KB

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