i915_gem.c 113 KB

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