i915_gem.c 120 KB

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