i915_gem.c 109 KB

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