i915_gem.c 110 KB

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