i915_gem.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377
  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. u32 seqno)
  1560. {
  1561. struct drm_device *dev = obj->base.dev;
  1562. struct drm_i915_private *dev_priv = dev->dev_private;
  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 u32
  1607. i915_gem_get_seqno(struct drm_device *dev)
  1608. {
  1609. drm_i915_private_t *dev_priv = dev->dev_private;
  1610. u32 seqno = dev_priv->next_seqno;
  1611. /* reserve 0 for non-seqno */
  1612. if (++dev_priv->next_seqno == 0)
  1613. dev_priv->next_seqno = 1;
  1614. return seqno;
  1615. }
  1616. u32
  1617. i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
  1618. {
  1619. if (ring->outstanding_lazy_request == 0)
  1620. ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
  1621. return ring->outstanding_lazy_request;
  1622. }
  1623. int
  1624. i915_add_request(struct intel_ring_buffer *ring,
  1625. struct drm_file *file,
  1626. u32 *out_seqno)
  1627. {
  1628. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1629. struct drm_i915_gem_request *request;
  1630. u32 request_ring_position;
  1631. u32 seqno;
  1632. int was_empty;
  1633. int ret;
  1634. /*
  1635. * Emit any outstanding flushes - execbuf can fail to emit the flush
  1636. * after having emitted the batchbuffer command. Hence we need to fix
  1637. * things up similar to emitting the lazy request. The difference here
  1638. * is that the flush _must_ happen before the next request, no matter
  1639. * what.
  1640. */
  1641. ret = intel_ring_flush_all_caches(ring);
  1642. if (ret)
  1643. return ret;
  1644. request = kmalloc(sizeof(*request), GFP_KERNEL);
  1645. if (request == NULL)
  1646. return -ENOMEM;
  1647. seqno = i915_gem_next_request_seqno(ring);
  1648. /* Record the position of the start of the request so that
  1649. * should we detect the updated seqno part-way through the
  1650. * GPU processing the request, we never over-estimate the
  1651. * position of the head.
  1652. */
  1653. request_ring_position = intel_ring_get_tail(ring);
  1654. ret = ring->add_request(ring, &seqno);
  1655. if (ret) {
  1656. kfree(request);
  1657. return ret;
  1658. }
  1659. trace_i915_gem_request_add(ring, seqno);
  1660. request->seqno = seqno;
  1661. request->ring = ring;
  1662. request->tail = request_ring_position;
  1663. request->emitted_jiffies = jiffies;
  1664. was_empty = list_empty(&ring->request_list);
  1665. list_add_tail(&request->list, &ring->request_list);
  1666. request->file_priv = NULL;
  1667. if (file) {
  1668. struct drm_i915_file_private *file_priv = file->driver_priv;
  1669. spin_lock(&file_priv->mm.lock);
  1670. request->file_priv = file_priv;
  1671. list_add_tail(&request->client_list,
  1672. &file_priv->mm.request_list);
  1673. spin_unlock(&file_priv->mm.lock);
  1674. }
  1675. ring->outstanding_lazy_request = 0;
  1676. if (!dev_priv->mm.suspended) {
  1677. if (i915_enable_hangcheck) {
  1678. mod_timer(&dev_priv->hangcheck_timer,
  1679. round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
  1680. }
  1681. if (was_empty) {
  1682. queue_delayed_work(dev_priv->wq,
  1683. &dev_priv->mm.retire_work,
  1684. round_jiffies_up_relative(HZ));
  1685. intel_mark_busy(dev_priv->dev);
  1686. }
  1687. }
  1688. if (out_seqno)
  1689. *out_seqno = seqno;
  1690. return 0;
  1691. }
  1692. static inline void
  1693. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1694. {
  1695. struct drm_i915_file_private *file_priv = request->file_priv;
  1696. if (!file_priv)
  1697. return;
  1698. spin_lock(&file_priv->mm.lock);
  1699. if (request->file_priv) {
  1700. list_del(&request->client_list);
  1701. request->file_priv = NULL;
  1702. }
  1703. spin_unlock(&file_priv->mm.lock);
  1704. }
  1705. static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
  1706. struct intel_ring_buffer *ring)
  1707. {
  1708. while (!list_empty(&ring->request_list)) {
  1709. struct drm_i915_gem_request *request;
  1710. request = list_first_entry(&ring->request_list,
  1711. struct drm_i915_gem_request,
  1712. list);
  1713. list_del(&request->list);
  1714. i915_gem_request_remove_from_client(request);
  1715. kfree(request);
  1716. }
  1717. while (!list_empty(&ring->active_list)) {
  1718. struct drm_i915_gem_object *obj;
  1719. obj = list_first_entry(&ring->active_list,
  1720. struct drm_i915_gem_object,
  1721. ring_list);
  1722. i915_gem_object_move_to_inactive(obj);
  1723. }
  1724. }
  1725. static void i915_gem_reset_fences(struct drm_device *dev)
  1726. {
  1727. struct drm_i915_private *dev_priv = dev->dev_private;
  1728. int i;
  1729. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1730. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1731. i915_gem_write_fence(dev, i, NULL);
  1732. if (reg->obj)
  1733. i915_gem_object_fence_lost(reg->obj);
  1734. reg->pin_count = 0;
  1735. reg->obj = NULL;
  1736. INIT_LIST_HEAD(&reg->lru_list);
  1737. }
  1738. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  1739. }
  1740. void i915_gem_reset(struct drm_device *dev)
  1741. {
  1742. struct drm_i915_private *dev_priv = dev->dev_private;
  1743. struct drm_i915_gem_object *obj;
  1744. struct intel_ring_buffer *ring;
  1745. int i;
  1746. for_each_ring(ring, dev_priv, i)
  1747. i915_gem_reset_ring_lists(dev_priv, ring);
  1748. /* Move everything out of the GPU domains to ensure we do any
  1749. * necessary invalidation upon reuse.
  1750. */
  1751. list_for_each_entry(obj,
  1752. &dev_priv->mm.inactive_list,
  1753. mm_list)
  1754. {
  1755. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  1756. }
  1757. /* The fence registers are invalidated so clear them out */
  1758. i915_gem_reset_fences(dev);
  1759. }
  1760. /**
  1761. * This function clears the request list as sequence numbers are passed.
  1762. */
  1763. void
  1764. i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
  1765. {
  1766. uint32_t seqno;
  1767. int i;
  1768. if (list_empty(&ring->request_list))
  1769. return;
  1770. WARN_ON(i915_verify_lists(ring->dev));
  1771. seqno = ring->get_seqno(ring, true);
  1772. for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
  1773. if (seqno >= ring->sync_seqno[i])
  1774. ring->sync_seqno[i] = 0;
  1775. while (!list_empty(&ring->request_list)) {
  1776. struct drm_i915_gem_request *request;
  1777. request = list_first_entry(&ring->request_list,
  1778. struct drm_i915_gem_request,
  1779. list);
  1780. if (!i915_seqno_passed(seqno, request->seqno))
  1781. break;
  1782. trace_i915_gem_request_retire(ring, request->seqno);
  1783. /* We know the GPU must have read the request to have
  1784. * sent us the seqno + interrupt, so use the position
  1785. * of tail of the request to update the last known position
  1786. * of the GPU head.
  1787. */
  1788. ring->last_retired_head = request->tail;
  1789. list_del(&request->list);
  1790. i915_gem_request_remove_from_client(request);
  1791. kfree(request);
  1792. }
  1793. /* Move any buffers on the active list that are no longer referenced
  1794. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1795. */
  1796. while (!list_empty(&ring->active_list)) {
  1797. struct drm_i915_gem_object *obj;
  1798. obj = list_first_entry(&ring->active_list,
  1799. struct drm_i915_gem_object,
  1800. ring_list);
  1801. if (!i915_seqno_passed(seqno, obj->last_read_seqno))
  1802. break;
  1803. i915_gem_object_move_to_inactive(obj);
  1804. }
  1805. if (unlikely(ring->trace_irq_seqno &&
  1806. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  1807. ring->irq_put(ring);
  1808. ring->trace_irq_seqno = 0;
  1809. }
  1810. WARN_ON(i915_verify_lists(ring->dev));
  1811. }
  1812. void
  1813. i915_gem_retire_requests(struct drm_device *dev)
  1814. {
  1815. drm_i915_private_t *dev_priv = dev->dev_private;
  1816. struct intel_ring_buffer *ring;
  1817. int i;
  1818. for_each_ring(ring, dev_priv, i)
  1819. i915_gem_retire_requests_ring(ring);
  1820. }
  1821. static void
  1822. i915_gem_retire_work_handler(struct work_struct *work)
  1823. {
  1824. drm_i915_private_t *dev_priv;
  1825. struct drm_device *dev;
  1826. struct intel_ring_buffer *ring;
  1827. bool idle;
  1828. int i;
  1829. dev_priv = container_of(work, drm_i915_private_t,
  1830. mm.retire_work.work);
  1831. dev = dev_priv->dev;
  1832. /* Come back later if the device is busy... */
  1833. if (!mutex_trylock(&dev->struct_mutex)) {
  1834. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1835. round_jiffies_up_relative(HZ));
  1836. return;
  1837. }
  1838. i915_gem_retire_requests(dev);
  1839. /* Send a periodic flush down the ring so we don't hold onto GEM
  1840. * objects indefinitely.
  1841. */
  1842. idle = true;
  1843. for_each_ring(ring, dev_priv, i) {
  1844. if (ring->gpu_caches_dirty)
  1845. i915_add_request(ring, NULL, NULL);
  1846. idle &= list_empty(&ring->request_list);
  1847. }
  1848. if (!dev_priv->mm.suspended && !idle)
  1849. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1850. round_jiffies_up_relative(HZ));
  1851. if (idle)
  1852. intel_mark_idle(dev);
  1853. mutex_unlock(&dev->struct_mutex);
  1854. }
  1855. /**
  1856. * Ensures that an object will eventually get non-busy by flushing any required
  1857. * write domains, emitting any outstanding lazy request and retiring and
  1858. * completed requests.
  1859. */
  1860. static int
  1861. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  1862. {
  1863. int ret;
  1864. if (obj->active) {
  1865. ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
  1866. if (ret)
  1867. return ret;
  1868. i915_gem_retire_requests_ring(obj->ring);
  1869. }
  1870. return 0;
  1871. }
  1872. /**
  1873. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  1874. * @DRM_IOCTL_ARGS: standard ioctl arguments
  1875. *
  1876. * Returns 0 if successful, else an error is returned with the remaining time in
  1877. * the timeout parameter.
  1878. * -ETIME: object is still busy after timeout
  1879. * -ERESTARTSYS: signal interrupted the wait
  1880. * -ENONENT: object doesn't exist
  1881. * Also possible, but rare:
  1882. * -EAGAIN: GPU wedged
  1883. * -ENOMEM: damn
  1884. * -ENODEV: Internal IRQ fail
  1885. * -E?: The add request failed
  1886. *
  1887. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  1888. * non-zero timeout parameter the wait ioctl will wait for the given number of
  1889. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  1890. * without holding struct_mutex the object may become re-busied before this
  1891. * function completes. A similar but shorter * race condition exists in the busy
  1892. * ioctl
  1893. */
  1894. int
  1895. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  1896. {
  1897. struct drm_i915_gem_wait *args = data;
  1898. struct drm_i915_gem_object *obj;
  1899. struct intel_ring_buffer *ring = NULL;
  1900. struct timespec timeout_stack, *timeout = NULL;
  1901. u32 seqno = 0;
  1902. int ret = 0;
  1903. if (args->timeout_ns >= 0) {
  1904. timeout_stack = ns_to_timespec(args->timeout_ns);
  1905. timeout = &timeout_stack;
  1906. }
  1907. ret = i915_mutex_lock_interruptible(dev);
  1908. if (ret)
  1909. return ret;
  1910. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  1911. if (&obj->base == NULL) {
  1912. mutex_unlock(&dev->struct_mutex);
  1913. return -ENOENT;
  1914. }
  1915. /* Need to make sure the object gets inactive eventually. */
  1916. ret = i915_gem_object_flush_active(obj);
  1917. if (ret)
  1918. goto out;
  1919. if (obj->active) {
  1920. seqno = obj->last_read_seqno;
  1921. ring = obj->ring;
  1922. }
  1923. if (seqno == 0)
  1924. goto out;
  1925. /* Do this after OLR check to make sure we make forward progress polling
  1926. * on this IOCTL with a 0 timeout (like busy ioctl)
  1927. */
  1928. if (!args->timeout_ns) {
  1929. ret = -ETIME;
  1930. goto out;
  1931. }
  1932. drm_gem_object_unreference(&obj->base);
  1933. mutex_unlock(&dev->struct_mutex);
  1934. ret = __wait_seqno(ring, seqno, true, timeout);
  1935. if (timeout) {
  1936. WARN_ON(!timespec_valid(timeout));
  1937. args->timeout_ns = timespec_to_ns(timeout);
  1938. }
  1939. return ret;
  1940. out:
  1941. drm_gem_object_unreference(&obj->base);
  1942. mutex_unlock(&dev->struct_mutex);
  1943. return ret;
  1944. }
  1945. /**
  1946. * i915_gem_object_sync - sync an object to a ring.
  1947. *
  1948. * @obj: object which may be in use on another ring.
  1949. * @to: ring we wish to use the object on. May be NULL.
  1950. *
  1951. * This code is meant to abstract object synchronization with the GPU.
  1952. * Calling with NULL implies synchronizing the object with the CPU
  1953. * rather than a particular GPU ring.
  1954. *
  1955. * Returns 0 if successful, else propagates up the lower layer error.
  1956. */
  1957. int
  1958. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  1959. struct intel_ring_buffer *to)
  1960. {
  1961. struct intel_ring_buffer *from = obj->ring;
  1962. u32 seqno;
  1963. int ret, idx;
  1964. if (from == NULL || to == from)
  1965. return 0;
  1966. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  1967. return i915_gem_object_wait_rendering(obj, false);
  1968. idx = intel_ring_sync_index(from, to);
  1969. seqno = obj->last_read_seqno;
  1970. if (seqno <= from->sync_seqno[idx])
  1971. return 0;
  1972. ret = i915_gem_check_olr(obj->ring, seqno);
  1973. if (ret)
  1974. return ret;
  1975. ret = to->sync_to(to, from, seqno);
  1976. if (!ret)
  1977. from->sync_seqno[idx] = seqno;
  1978. return ret;
  1979. }
  1980. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  1981. {
  1982. u32 old_write_domain, old_read_domains;
  1983. /* Act a barrier for all accesses through the GTT */
  1984. mb();
  1985. /* Force a pagefault for domain tracking on next user access */
  1986. i915_gem_release_mmap(obj);
  1987. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  1988. return;
  1989. old_read_domains = obj->base.read_domains;
  1990. old_write_domain = obj->base.write_domain;
  1991. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  1992. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  1993. trace_i915_gem_object_change_domain(obj,
  1994. old_read_domains,
  1995. old_write_domain);
  1996. }
  1997. /**
  1998. * Unbinds an object from the GTT aperture.
  1999. */
  2000. int
  2001. i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  2002. {
  2003. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2004. int ret = 0;
  2005. if (obj->gtt_space == NULL)
  2006. return 0;
  2007. if (obj->pin_count)
  2008. return -EBUSY;
  2009. BUG_ON(obj->pages == NULL);
  2010. ret = i915_gem_object_finish_gpu(obj);
  2011. if (ret)
  2012. return ret;
  2013. /* Continue on if we fail due to EIO, the GPU is hung so we
  2014. * should be safe and we need to cleanup or else we might
  2015. * cause memory corruption through use-after-free.
  2016. */
  2017. i915_gem_object_finish_gtt(obj);
  2018. /* release the fence reg _after_ flushing */
  2019. ret = i915_gem_object_put_fence(obj);
  2020. if (ret)
  2021. return ret;
  2022. trace_i915_gem_object_unbind(obj);
  2023. if (obj->has_global_gtt_mapping)
  2024. i915_gem_gtt_unbind_object(obj);
  2025. if (obj->has_aliasing_ppgtt_mapping) {
  2026. i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
  2027. obj->has_aliasing_ppgtt_mapping = 0;
  2028. }
  2029. i915_gem_gtt_finish_object(obj);
  2030. list_del(&obj->mm_list);
  2031. list_move_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
  2032. /* Avoid an unnecessary call to unbind on rebind. */
  2033. obj->map_and_fenceable = true;
  2034. drm_mm_put_block(obj->gtt_space);
  2035. obj->gtt_space = NULL;
  2036. obj->gtt_offset = 0;
  2037. return 0;
  2038. }
  2039. static int i915_ring_idle(struct intel_ring_buffer *ring)
  2040. {
  2041. if (list_empty(&ring->active_list))
  2042. return 0;
  2043. return i915_wait_seqno(ring, i915_gem_next_request_seqno(ring));
  2044. }
  2045. int i915_gpu_idle(struct drm_device *dev)
  2046. {
  2047. drm_i915_private_t *dev_priv = dev->dev_private;
  2048. struct intel_ring_buffer *ring;
  2049. int ret, i;
  2050. /* Flush everything onto the inactive list. */
  2051. for_each_ring(ring, dev_priv, i) {
  2052. ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
  2053. if (ret)
  2054. return ret;
  2055. ret = i915_ring_idle(ring);
  2056. if (ret)
  2057. return ret;
  2058. }
  2059. return 0;
  2060. }
  2061. static void sandybridge_write_fence_reg(struct drm_device *dev, int reg,
  2062. struct drm_i915_gem_object *obj)
  2063. {
  2064. drm_i915_private_t *dev_priv = dev->dev_private;
  2065. uint64_t val;
  2066. if (obj) {
  2067. u32 size = obj->gtt_space->size;
  2068. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  2069. 0xfffff000) << 32;
  2070. val |= obj->gtt_offset & 0xfffff000;
  2071. val |= (uint64_t)((obj->stride / 128) - 1) <<
  2072. SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2073. if (obj->tiling_mode == I915_TILING_Y)
  2074. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2075. val |= I965_FENCE_REG_VALID;
  2076. } else
  2077. val = 0;
  2078. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val);
  2079. POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8);
  2080. }
  2081. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2082. struct drm_i915_gem_object *obj)
  2083. {
  2084. drm_i915_private_t *dev_priv = dev->dev_private;
  2085. uint64_t val;
  2086. if (obj) {
  2087. u32 size = obj->gtt_space->size;
  2088. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  2089. 0xfffff000) << 32;
  2090. val |= obj->gtt_offset & 0xfffff000;
  2091. val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  2092. if (obj->tiling_mode == I915_TILING_Y)
  2093. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2094. val |= I965_FENCE_REG_VALID;
  2095. } else
  2096. val = 0;
  2097. I915_WRITE64(FENCE_REG_965_0 + reg * 8, val);
  2098. POSTING_READ(FENCE_REG_965_0 + reg * 8);
  2099. }
  2100. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2101. struct drm_i915_gem_object *obj)
  2102. {
  2103. drm_i915_private_t *dev_priv = dev->dev_private;
  2104. u32 val;
  2105. if (obj) {
  2106. u32 size = obj->gtt_space->size;
  2107. int pitch_val;
  2108. int tile_width;
  2109. WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
  2110. (size & -size) != size ||
  2111. (obj->gtt_offset & (size - 1)),
  2112. "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2113. obj->gtt_offset, obj->map_and_fenceable, size);
  2114. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2115. tile_width = 128;
  2116. else
  2117. tile_width = 512;
  2118. /* Note: pitch better be a power of two tile widths */
  2119. pitch_val = obj->stride / tile_width;
  2120. pitch_val = ffs(pitch_val) - 1;
  2121. val = obj->gtt_offset;
  2122. if (obj->tiling_mode == I915_TILING_Y)
  2123. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2124. val |= I915_FENCE_SIZE_BITS(size);
  2125. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2126. val |= I830_FENCE_REG_VALID;
  2127. } else
  2128. val = 0;
  2129. if (reg < 8)
  2130. reg = FENCE_REG_830_0 + reg * 4;
  2131. else
  2132. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2133. I915_WRITE(reg, val);
  2134. POSTING_READ(reg);
  2135. }
  2136. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2137. struct drm_i915_gem_object *obj)
  2138. {
  2139. drm_i915_private_t *dev_priv = dev->dev_private;
  2140. uint32_t val;
  2141. if (obj) {
  2142. u32 size = obj->gtt_space->size;
  2143. uint32_t pitch_val;
  2144. WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
  2145. (size & -size) != size ||
  2146. (obj->gtt_offset & (size - 1)),
  2147. "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
  2148. obj->gtt_offset, size);
  2149. pitch_val = obj->stride / 128;
  2150. pitch_val = ffs(pitch_val) - 1;
  2151. val = obj->gtt_offset;
  2152. if (obj->tiling_mode == I915_TILING_Y)
  2153. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2154. val |= I830_FENCE_SIZE_BITS(size);
  2155. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2156. val |= I830_FENCE_REG_VALID;
  2157. } else
  2158. val = 0;
  2159. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2160. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2161. }
  2162. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2163. struct drm_i915_gem_object *obj)
  2164. {
  2165. switch (INTEL_INFO(dev)->gen) {
  2166. case 7:
  2167. case 6: sandybridge_write_fence_reg(dev, reg, obj); break;
  2168. case 5:
  2169. case 4: i965_write_fence_reg(dev, reg, obj); break;
  2170. case 3: i915_write_fence_reg(dev, reg, obj); break;
  2171. case 2: i830_write_fence_reg(dev, reg, obj); break;
  2172. default: break;
  2173. }
  2174. }
  2175. static inline int fence_number(struct drm_i915_private *dev_priv,
  2176. struct drm_i915_fence_reg *fence)
  2177. {
  2178. return fence - dev_priv->fence_regs;
  2179. }
  2180. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2181. struct drm_i915_fence_reg *fence,
  2182. bool enable)
  2183. {
  2184. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2185. int reg = fence_number(dev_priv, fence);
  2186. i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  2187. if (enable) {
  2188. obj->fence_reg = reg;
  2189. fence->obj = obj;
  2190. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2191. } else {
  2192. obj->fence_reg = I915_FENCE_REG_NONE;
  2193. fence->obj = NULL;
  2194. list_del_init(&fence->lru_list);
  2195. }
  2196. }
  2197. static int
  2198. i915_gem_object_flush_fence(struct drm_i915_gem_object *obj)
  2199. {
  2200. if (obj->last_fenced_seqno) {
  2201. int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
  2202. if (ret)
  2203. return ret;
  2204. obj->last_fenced_seqno = 0;
  2205. }
  2206. /* Ensure that all CPU reads are completed before installing a fence
  2207. * and all writes before removing the fence.
  2208. */
  2209. if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
  2210. mb();
  2211. obj->fenced_gpu_access = false;
  2212. return 0;
  2213. }
  2214. int
  2215. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2216. {
  2217. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2218. int ret;
  2219. ret = i915_gem_object_flush_fence(obj);
  2220. if (ret)
  2221. return ret;
  2222. if (obj->fence_reg == I915_FENCE_REG_NONE)
  2223. return 0;
  2224. i915_gem_object_update_fence(obj,
  2225. &dev_priv->fence_regs[obj->fence_reg],
  2226. false);
  2227. i915_gem_object_fence_lost(obj);
  2228. return 0;
  2229. }
  2230. static struct drm_i915_fence_reg *
  2231. i915_find_fence_reg(struct drm_device *dev)
  2232. {
  2233. struct drm_i915_private *dev_priv = dev->dev_private;
  2234. struct drm_i915_fence_reg *reg, *avail;
  2235. int i;
  2236. /* First try to find a free reg */
  2237. avail = NULL;
  2238. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2239. reg = &dev_priv->fence_regs[i];
  2240. if (!reg->obj)
  2241. return reg;
  2242. if (!reg->pin_count)
  2243. avail = reg;
  2244. }
  2245. if (avail == NULL)
  2246. return NULL;
  2247. /* None available, try to steal one or wait for a user to finish */
  2248. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2249. if (reg->pin_count)
  2250. continue;
  2251. return reg;
  2252. }
  2253. return NULL;
  2254. }
  2255. /**
  2256. * i915_gem_object_get_fence - set up fencing for an object
  2257. * @obj: object to map through a fence reg
  2258. *
  2259. * When mapping objects through the GTT, userspace wants to be able to write
  2260. * to them without having to worry about swizzling if the object is tiled.
  2261. * This function walks the fence regs looking for a free one for @obj,
  2262. * stealing one if it can't find any.
  2263. *
  2264. * It then sets up the reg based on the object's properties: address, pitch
  2265. * and tiling format.
  2266. *
  2267. * For an untiled surface, this removes any existing fence.
  2268. */
  2269. int
  2270. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2271. {
  2272. struct drm_device *dev = obj->base.dev;
  2273. struct drm_i915_private *dev_priv = dev->dev_private;
  2274. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2275. struct drm_i915_fence_reg *reg;
  2276. int ret;
  2277. /* Have we updated the tiling parameters upon the object and so
  2278. * will need to serialise the write to the associated fence register?
  2279. */
  2280. if (obj->fence_dirty) {
  2281. ret = i915_gem_object_flush_fence(obj);
  2282. if (ret)
  2283. return ret;
  2284. }
  2285. /* Just update our place in the LRU if our fence is getting reused. */
  2286. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2287. reg = &dev_priv->fence_regs[obj->fence_reg];
  2288. if (!obj->fence_dirty) {
  2289. list_move_tail(&reg->lru_list,
  2290. &dev_priv->mm.fence_list);
  2291. return 0;
  2292. }
  2293. } else if (enable) {
  2294. reg = i915_find_fence_reg(dev);
  2295. if (reg == NULL)
  2296. return -EDEADLK;
  2297. if (reg->obj) {
  2298. struct drm_i915_gem_object *old = reg->obj;
  2299. ret = i915_gem_object_flush_fence(old);
  2300. if (ret)
  2301. return ret;
  2302. i915_gem_object_fence_lost(old);
  2303. }
  2304. } else
  2305. return 0;
  2306. i915_gem_object_update_fence(obj, reg, enable);
  2307. obj->fence_dirty = false;
  2308. return 0;
  2309. }
  2310. static bool i915_gem_valid_gtt_space(struct drm_device *dev,
  2311. struct drm_mm_node *gtt_space,
  2312. unsigned long cache_level)
  2313. {
  2314. struct drm_mm_node *other;
  2315. /* On non-LLC machines we have to be careful when putting differing
  2316. * types of snoopable memory together to avoid the prefetcher
  2317. * crossing memory domains and dieing.
  2318. */
  2319. if (HAS_LLC(dev))
  2320. return true;
  2321. if (gtt_space == NULL)
  2322. return true;
  2323. if (list_empty(&gtt_space->node_list))
  2324. return true;
  2325. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2326. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2327. return false;
  2328. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2329. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2330. return false;
  2331. return true;
  2332. }
  2333. static void i915_gem_verify_gtt(struct drm_device *dev)
  2334. {
  2335. #if WATCH_GTT
  2336. struct drm_i915_private *dev_priv = dev->dev_private;
  2337. struct drm_i915_gem_object *obj;
  2338. int err = 0;
  2339. list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
  2340. if (obj->gtt_space == NULL) {
  2341. printk(KERN_ERR "object found on GTT list with no space reserved\n");
  2342. err++;
  2343. continue;
  2344. }
  2345. if (obj->cache_level != obj->gtt_space->color) {
  2346. printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
  2347. obj->gtt_space->start,
  2348. obj->gtt_space->start + obj->gtt_space->size,
  2349. obj->cache_level,
  2350. obj->gtt_space->color);
  2351. err++;
  2352. continue;
  2353. }
  2354. if (!i915_gem_valid_gtt_space(dev,
  2355. obj->gtt_space,
  2356. obj->cache_level)) {
  2357. printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
  2358. obj->gtt_space->start,
  2359. obj->gtt_space->start + obj->gtt_space->size,
  2360. obj->cache_level);
  2361. err++;
  2362. continue;
  2363. }
  2364. }
  2365. WARN_ON(err);
  2366. #endif
  2367. }
  2368. /**
  2369. * Finds free space in the GTT aperture and binds the object there.
  2370. */
  2371. static int
  2372. i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  2373. unsigned alignment,
  2374. bool map_and_fenceable,
  2375. bool nonblocking)
  2376. {
  2377. struct drm_device *dev = obj->base.dev;
  2378. drm_i915_private_t *dev_priv = dev->dev_private;
  2379. struct drm_mm_node *free_space;
  2380. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2381. bool mappable, fenceable;
  2382. int ret;
  2383. if (obj->madv != I915_MADV_WILLNEED) {
  2384. DRM_ERROR("Attempting to bind a purgeable object\n");
  2385. return -EINVAL;
  2386. }
  2387. fence_size = i915_gem_get_gtt_size(dev,
  2388. obj->base.size,
  2389. obj->tiling_mode);
  2390. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2391. obj->base.size,
  2392. obj->tiling_mode);
  2393. unfenced_alignment =
  2394. i915_gem_get_unfenced_gtt_alignment(dev,
  2395. obj->base.size,
  2396. obj->tiling_mode);
  2397. if (alignment == 0)
  2398. alignment = map_and_fenceable ? fence_alignment :
  2399. unfenced_alignment;
  2400. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2401. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2402. return -EINVAL;
  2403. }
  2404. size = map_and_fenceable ? fence_size : obj->base.size;
  2405. /* If the object is bigger than the entire aperture, reject it early
  2406. * before evicting everything in a vain attempt to find space.
  2407. */
  2408. if (obj->base.size >
  2409. (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
  2410. DRM_ERROR("Attempting to bind an object larger than the aperture\n");
  2411. return -E2BIG;
  2412. }
  2413. ret = i915_gem_object_get_pages(obj);
  2414. if (ret)
  2415. return ret;
  2416. i915_gem_object_pin_pages(obj);
  2417. search_free:
  2418. if (map_and_fenceable)
  2419. free_space =
  2420. drm_mm_search_free_in_range_color(&dev_priv->mm.gtt_space,
  2421. size, alignment, obj->cache_level,
  2422. 0, dev_priv->mm.gtt_mappable_end,
  2423. false);
  2424. else
  2425. free_space = drm_mm_search_free_color(&dev_priv->mm.gtt_space,
  2426. size, alignment, obj->cache_level,
  2427. false);
  2428. if (free_space != NULL) {
  2429. if (map_and_fenceable)
  2430. obj->gtt_space =
  2431. drm_mm_get_block_range_generic(free_space,
  2432. size, alignment, obj->cache_level,
  2433. 0, dev_priv->mm.gtt_mappable_end,
  2434. false);
  2435. else
  2436. obj->gtt_space =
  2437. drm_mm_get_block_generic(free_space,
  2438. size, alignment, obj->cache_level,
  2439. false);
  2440. }
  2441. if (obj->gtt_space == NULL) {
  2442. ret = i915_gem_evict_something(dev, size, alignment,
  2443. obj->cache_level,
  2444. map_and_fenceable,
  2445. nonblocking);
  2446. if (ret) {
  2447. i915_gem_object_unpin_pages(obj);
  2448. return ret;
  2449. }
  2450. goto search_free;
  2451. }
  2452. if (WARN_ON(!i915_gem_valid_gtt_space(dev,
  2453. obj->gtt_space,
  2454. obj->cache_level))) {
  2455. i915_gem_object_unpin_pages(obj);
  2456. drm_mm_put_block(obj->gtt_space);
  2457. obj->gtt_space = NULL;
  2458. return -EINVAL;
  2459. }
  2460. ret = i915_gem_gtt_prepare_object(obj);
  2461. if (ret) {
  2462. i915_gem_object_unpin_pages(obj);
  2463. drm_mm_put_block(obj->gtt_space);
  2464. obj->gtt_space = NULL;
  2465. return ret;
  2466. }
  2467. if (!dev_priv->mm.aliasing_ppgtt)
  2468. i915_gem_gtt_bind_object(obj, obj->cache_level);
  2469. list_move_tail(&obj->gtt_list, &dev_priv->mm.bound_list);
  2470. list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2471. obj->gtt_offset = obj->gtt_space->start;
  2472. fenceable =
  2473. obj->gtt_space->size == fence_size &&
  2474. (obj->gtt_space->start & (fence_alignment - 1)) == 0;
  2475. mappable =
  2476. obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
  2477. obj->map_and_fenceable = mappable && fenceable;
  2478. i915_gem_object_unpin_pages(obj);
  2479. trace_i915_gem_object_bind(obj, map_and_fenceable);
  2480. i915_gem_verify_gtt(dev);
  2481. return 0;
  2482. }
  2483. void
  2484. i915_gem_clflush_object(struct drm_i915_gem_object *obj)
  2485. {
  2486. /* If we don't have a page list set up, then we're not pinned
  2487. * to GPU, and we can ignore the cache flush because it'll happen
  2488. * again at bind time.
  2489. */
  2490. if (obj->pages == NULL)
  2491. return;
  2492. /* If the GPU is snooping the contents of the CPU cache,
  2493. * we do not need to manually clear the CPU cache lines. However,
  2494. * the caches are only snooped when the render cache is
  2495. * flushed/invalidated. As we always have to emit invalidations
  2496. * and flushes when moving into and out of the RENDER domain, correct
  2497. * snooping behaviour occurs naturally as the result of our domain
  2498. * tracking.
  2499. */
  2500. if (obj->cache_level != I915_CACHE_NONE)
  2501. return;
  2502. trace_i915_gem_object_clflush(obj);
  2503. drm_clflush_sg(obj->pages);
  2504. }
  2505. /** Flushes the GTT write domain for the object if it's dirty. */
  2506. static void
  2507. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2508. {
  2509. uint32_t old_write_domain;
  2510. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2511. return;
  2512. /* No actual flushing is required for the GTT write domain. Writes
  2513. * to it immediately go to main memory as far as we know, so there's
  2514. * no chipset flush. It also doesn't land in render cache.
  2515. *
  2516. * However, we do have to enforce the order so that all writes through
  2517. * the GTT land before any writes to the device, such as updates to
  2518. * the GATT itself.
  2519. */
  2520. wmb();
  2521. old_write_domain = obj->base.write_domain;
  2522. obj->base.write_domain = 0;
  2523. trace_i915_gem_object_change_domain(obj,
  2524. obj->base.read_domains,
  2525. old_write_domain);
  2526. }
  2527. /** Flushes the CPU write domain for the object if it's dirty. */
  2528. static void
  2529. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2530. {
  2531. uint32_t old_write_domain;
  2532. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2533. return;
  2534. i915_gem_clflush_object(obj);
  2535. i915_gem_chipset_flush(obj->base.dev);
  2536. old_write_domain = obj->base.write_domain;
  2537. obj->base.write_domain = 0;
  2538. trace_i915_gem_object_change_domain(obj,
  2539. obj->base.read_domains,
  2540. old_write_domain);
  2541. }
  2542. /**
  2543. * Moves a single object to the GTT read, and possibly write domain.
  2544. *
  2545. * This function returns when the move is complete, including waiting on
  2546. * flushes to occur.
  2547. */
  2548. int
  2549. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2550. {
  2551. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2552. uint32_t old_write_domain, old_read_domains;
  2553. int ret;
  2554. /* Not valid to be called on unbound objects. */
  2555. if (obj->gtt_space == NULL)
  2556. return -EINVAL;
  2557. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2558. return 0;
  2559. ret = i915_gem_object_wait_rendering(obj, !write);
  2560. if (ret)
  2561. return ret;
  2562. i915_gem_object_flush_cpu_write_domain(obj);
  2563. old_write_domain = obj->base.write_domain;
  2564. old_read_domains = obj->base.read_domains;
  2565. /* It should now be out of any other write domains, and we can update
  2566. * the domain values for our changes.
  2567. */
  2568. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2569. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2570. if (write) {
  2571. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2572. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2573. obj->dirty = 1;
  2574. }
  2575. trace_i915_gem_object_change_domain(obj,
  2576. old_read_domains,
  2577. old_write_domain);
  2578. /* And bump the LRU for this access */
  2579. if (i915_gem_object_is_inactive(obj))
  2580. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2581. return 0;
  2582. }
  2583. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2584. enum i915_cache_level cache_level)
  2585. {
  2586. struct drm_device *dev = obj->base.dev;
  2587. drm_i915_private_t *dev_priv = dev->dev_private;
  2588. int ret;
  2589. if (obj->cache_level == cache_level)
  2590. return 0;
  2591. if (obj->pin_count) {
  2592. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2593. return -EBUSY;
  2594. }
  2595. if (!i915_gem_valid_gtt_space(dev, obj->gtt_space, cache_level)) {
  2596. ret = i915_gem_object_unbind(obj);
  2597. if (ret)
  2598. return ret;
  2599. }
  2600. if (obj->gtt_space) {
  2601. ret = i915_gem_object_finish_gpu(obj);
  2602. if (ret)
  2603. return ret;
  2604. i915_gem_object_finish_gtt(obj);
  2605. /* Before SandyBridge, you could not use tiling or fence
  2606. * registers with snooped memory, so relinquish any fences
  2607. * currently pointing to our region in the aperture.
  2608. */
  2609. if (INTEL_INFO(dev)->gen < 6) {
  2610. ret = i915_gem_object_put_fence(obj);
  2611. if (ret)
  2612. return ret;
  2613. }
  2614. if (obj->has_global_gtt_mapping)
  2615. i915_gem_gtt_bind_object(obj, cache_level);
  2616. if (obj->has_aliasing_ppgtt_mapping)
  2617. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  2618. obj, cache_level);
  2619. obj->gtt_space->color = cache_level;
  2620. }
  2621. if (cache_level == I915_CACHE_NONE) {
  2622. u32 old_read_domains, old_write_domain;
  2623. /* If we're coming from LLC cached, then we haven't
  2624. * actually been tracking whether the data is in the
  2625. * CPU cache or not, since we only allow one bit set
  2626. * in obj->write_domain and have been skipping the clflushes.
  2627. * Just set it to the CPU cache for now.
  2628. */
  2629. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  2630. WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
  2631. old_read_domains = obj->base.read_domains;
  2632. old_write_domain = obj->base.write_domain;
  2633. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2634. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2635. trace_i915_gem_object_change_domain(obj,
  2636. old_read_domains,
  2637. old_write_domain);
  2638. }
  2639. obj->cache_level = cache_level;
  2640. i915_gem_verify_gtt(dev);
  2641. return 0;
  2642. }
  2643. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  2644. struct drm_file *file)
  2645. {
  2646. struct drm_i915_gem_caching *args = data;
  2647. struct drm_i915_gem_object *obj;
  2648. int ret;
  2649. ret = i915_mutex_lock_interruptible(dev);
  2650. if (ret)
  2651. return ret;
  2652. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2653. if (&obj->base == NULL) {
  2654. ret = -ENOENT;
  2655. goto unlock;
  2656. }
  2657. args->caching = obj->cache_level != I915_CACHE_NONE;
  2658. drm_gem_object_unreference(&obj->base);
  2659. unlock:
  2660. mutex_unlock(&dev->struct_mutex);
  2661. return ret;
  2662. }
  2663. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  2664. struct drm_file *file)
  2665. {
  2666. struct drm_i915_gem_caching *args = data;
  2667. struct drm_i915_gem_object *obj;
  2668. enum i915_cache_level level;
  2669. int ret;
  2670. switch (args->caching) {
  2671. case I915_CACHING_NONE:
  2672. level = I915_CACHE_NONE;
  2673. break;
  2674. case I915_CACHING_CACHED:
  2675. level = I915_CACHE_LLC;
  2676. break;
  2677. default:
  2678. return -EINVAL;
  2679. }
  2680. ret = i915_mutex_lock_interruptible(dev);
  2681. if (ret)
  2682. return ret;
  2683. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2684. if (&obj->base == NULL) {
  2685. ret = -ENOENT;
  2686. goto unlock;
  2687. }
  2688. ret = i915_gem_object_set_cache_level(obj, level);
  2689. drm_gem_object_unreference(&obj->base);
  2690. unlock:
  2691. mutex_unlock(&dev->struct_mutex);
  2692. return ret;
  2693. }
  2694. /*
  2695. * Prepare buffer for display plane (scanout, cursors, etc).
  2696. * Can be called from an uninterruptible phase (modesetting) and allows
  2697. * any flushes to be pipelined (for pageflips).
  2698. */
  2699. int
  2700. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2701. u32 alignment,
  2702. struct intel_ring_buffer *pipelined)
  2703. {
  2704. u32 old_read_domains, old_write_domain;
  2705. int ret;
  2706. if (pipelined != obj->ring) {
  2707. ret = i915_gem_object_sync(obj, pipelined);
  2708. if (ret)
  2709. return ret;
  2710. }
  2711. /* The display engine is not coherent with the LLC cache on gen6. As
  2712. * a result, we make sure that the pinning that is about to occur is
  2713. * done with uncached PTEs. This is lowest common denominator for all
  2714. * chipsets.
  2715. *
  2716. * However for gen6+, we could do better by using the GFDT bit instead
  2717. * of uncaching, which would allow us to flush all the LLC-cached data
  2718. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2719. */
  2720. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
  2721. if (ret)
  2722. return ret;
  2723. /* As the user may map the buffer once pinned in the display plane
  2724. * (e.g. libkms for the bootup splash), we have to ensure that we
  2725. * always use map_and_fenceable for all scanout buffers.
  2726. */
  2727. ret = i915_gem_object_pin(obj, alignment, true, false);
  2728. if (ret)
  2729. return ret;
  2730. i915_gem_object_flush_cpu_write_domain(obj);
  2731. old_write_domain = obj->base.write_domain;
  2732. old_read_domains = obj->base.read_domains;
  2733. /* It should now be out of any other write domains, and we can update
  2734. * the domain values for our changes.
  2735. */
  2736. obj->base.write_domain = 0;
  2737. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2738. trace_i915_gem_object_change_domain(obj,
  2739. old_read_domains,
  2740. old_write_domain);
  2741. return 0;
  2742. }
  2743. int
  2744. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  2745. {
  2746. int ret;
  2747. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  2748. return 0;
  2749. ret = i915_gem_object_wait_rendering(obj, false);
  2750. if (ret)
  2751. return ret;
  2752. /* Ensure that we invalidate the GPU's caches and TLBs. */
  2753. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  2754. return 0;
  2755. }
  2756. /**
  2757. * Moves a single object to the CPU read, and possibly write domain.
  2758. *
  2759. * This function returns when the move is complete, including waiting on
  2760. * flushes to occur.
  2761. */
  2762. int
  2763. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  2764. {
  2765. uint32_t old_write_domain, old_read_domains;
  2766. int ret;
  2767. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  2768. return 0;
  2769. ret = i915_gem_object_wait_rendering(obj, !write);
  2770. if (ret)
  2771. return ret;
  2772. i915_gem_object_flush_gtt_write_domain(obj);
  2773. old_write_domain = obj->base.write_domain;
  2774. old_read_domains = obj->base.read_domains;
  2775. /* Flush the CPU cache if it's still invalid. */
  2776. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2777. i915_gem_clflush_object(obj);
  2778. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2779. }
  2780. /* It should now be out of any other write domains, and we can update
  2781. * the domain values for our changes.
  2782. */
  2783. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2784. /* If we're writing through the CPU, then the GPU read domains will
  2785. * need to be invalidated at next use.
  2786. */
  2787. if (write) {
  2788. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2789. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2790. }
  2791. trace_i915_gem_object_change_domain(obj,
  2792. old_read_domains,
  2793. old_write_domain);
  2794. return 0;
  2795. }
  2796. /* Throttle our rendering by waiting until the ring has completed our requests
  2797. * emitted over 20 msec ago.
  2798. *
  2799. * Note that if we were to use the current jiffies each time around the loop,
  2800. * we wouldn't escape the function with any frames outstanding if the time to
  2801. * render a frame was over 20ms.
  2802. *
  2803. * This should get us reasonable parallelism between CPU and GPU but also
  2804. * relatively low latency when blocking on a particular request to finish.
  2805. */
  2806. static int
  2807. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  2808. {
  2809. struct drm_i915_private *dev_priv = dev->dev_private;
  2810. struct drm_i915_file_private *file_priv = file->driver_priv;
  2811. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2812. struct drm_i915_gem_request *request;
  2813. struct intel_ring_buffer *ring = NULL;
  2814. u32 seqno = 0;
  2815. int ret;
  2816. if (atomic_read(&dev_priv->mm.wedged))
  2817. return -EIO;
  2818. spin_lock(&file_priv->mm.lock);
  2819. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  2820. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2821. break;
  2822. ring = request->ring;
  2823. seqno = request->seqno;
  2824. }
  2825. spin_unlock(&file_priv->mm.lock);
  2826. if (seqno == 0)
  2827. return 0;
  2828. ret = __wait_seqno(ring, seqno, true, NULL);
  2829. if (ret == 0)
  2830. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  2831. return ret;
  2832. }
  2833. int
  2834. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  2835. uint32_t alignment,
  2836. bool map_and_fenceable,
  2837. bool nonblocking)
  2838. {
  2839. int ret;
  2840. if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  2841. return -EBUSY;
  2842. if (obj->gtt_space != NULL) {
  2843. if ((alignment && obj->gtt_offset & (alignment - 1)) ||
  2844. (map_and_fenceable && !obj->map_and_fenceable)) {
  2845. WARN(obj->pin_count,
  2846. "bo is already pinned with incorrect alignment:"
  2847. " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
  2848. " obj->map_and_fenceable=%d\n",
  2849. obj->gtt_offset, alignment,
  2850. map_and_fenceable,
  2851. obj->map_and_fenceable);
  2852. ret = i915_gem_object_unbind(obj);
  2853. if (ret)
  2854. return ret;
  2855. }
  2856. }
  2857. if (obj->gtt_space == NULL) {
  2858. ret = i915_gem_object_bind_to_gtt(obj, alignment,
  2859. map_and_fenceable,
  2860. nonblocking);
  2861. if (ret)
  2862. return ret;
  2863. }
  2864. if (!obj->has_global_gtt_mapping && map_and_fenceable)
  2865. i915_gem_gtt_bind_object(obj, obj->cache_level);
  2866. obj->pin_count++;
  2867. obj->pin_mappable |= map_and_fenceable;
  2868. return 0;
  2869. }
  2870. void
  2871. i915_gem_object_unpin(struct drm_i915_gem_object *obj)
  2872. {
  2873. BUG_ON(obj->pin_count == 0);
  2874. BUG_ON(obj->gtt_space == NULL);
  2875. if (--obj->pin_count == 0)
  2876. obj->pin_mappable = false;
  2877. }
  2878. int
  2879. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  2880. struct drm_file *file)
  2881. {
  2882. struct drm_i915_gem_pin *args = data;
  2883. struct drm_i915_gem_object *obj;
  2884. int ret;
  2885. ret = i915_mutex_lock_interruptible(dev);
  2886. if (ret)
  2887. return ret;
  2888. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2889. if (&obj->base == NULL) {
  2890. ret = -ENOENT;
  2891. goto unlock;
  2892. }
  2893. if (obj->madv != I915_MADV_WILLNEED) {
  2894. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  2895. ret = -EINVAL;
  2896. goto out;
  2897. }
  2898. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  2899. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  2900. args->handle);
  2901. ret = -EINVAL;
  2902. goto out;
  2903. }
  2904. obj->user_pin_count++;
  2905. obj->pin_filp = file;
  2906. if (obj->user_pin_count == 1) {
  2907. ret = i915_gem_object_pin(obj, args->alignment, true, false);
  2908. if (ret)
  2909. goto out;
  2910. }
  2911. /* XXX - flush the CPU caches for pinned objects
  2912. * as the X server doesn't manage domains yet
  2913. */
  2914. i915_gem_object_flush_cpu_write_domain(obj);
  2915. args->offset = obj->gtt_offset;
  2916. out:
  2917. drm_gem_object_unreference(&obj->base);
  2918. unlock:
  2919. mutex_unlock(&dev->struct_mutex);
  2920. return ret;
  2921. }
  2922. int
  2923. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  2924. struct drm_file *file)
  2925. {
  2926. struct drm_i915_gem_pin *args = data;
  2927. struct drm_i915_gem_object *obj;
  2928. int ret;
  2929. ret = i915_mutex_lock_interruptible(dev);
  2930. if (ret)
  2931. return ret;
  2932. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2933. if (&obj->base == NULL) {
  2934. ret = -ENOENT;
  2935. goto unlock;
  2936. }
  2937. if (obj->pin_filp != file) {
  2938. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  2939. args->handle);
  2940. ret = -EINVAL;
  2941. goto out;
  2942. }
  2943. obj->user_pin_count--;
  2944. if (obj->user_pin_count == 0) {
  2945. obj->pin_filp = NULL;
  2946. i915_gem_object_unpin(obj);
  2947. }
  2948. out:
  2949. drm_gem_object_unreference(&obj->base);
  2950. unlock:
  2951. mutex_unlock(&dev->struct_mutex);
  2952. return ret;
  2953. }
  2954. int
  2955. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  2956. struct drm_file *file)
  2957. {
  2958. struct drm_i915_gem_busy *args = data;
  2959. struct drm_i915_gem_object *obj;
  2960. int ret;
  2961. ret = i915_mutex_lock_interruptible(dev);
  2962. if (ret)
  2963. return ret;
  2964. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2965. if (&obj->base == NULL) {
  2966. ret = -ENOENT;
  2967. goto unlock;
  2968. }
  2969. /* Count all active objects as busy, even if they are currently not used
  2970. * by the gpu. Users of this interface expect objects to eventually
  2971. * become non-busy without any further actions, therefore emit any
  2972. * necessary flushes here.
  2973. */
  2974. ret = i915_gem_object_flush_active(obj);
  2975. args->busy = obj->active;
  2976. if (obj->ring) {
  2977. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  2978. args->busy |= intel_ring_flag(obj->ring) << 16;
  2979. }
  2980. drm_gem_object_unreference(&obj->base);
  2981. unlock:
  2982. mutex_unlock(&dev->struct_mutex);
  2983. return ret;
  2984. }
  2985. int
  2986. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  2987. struct drm_file *file_priv)
  2988. {
  2989. return i915_gem_ring_throttle(dev, file_priv);
  2990. }
  2991. int
  2992. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  2993. struct drm_file *file_priv)
  2994. {
  2995. struct drm_i915_gem_madvise *args = data;
  2996. struct drm_i915_gem_object *obj;
  2997. int ret;
  2998. switch (args->madv) {
  2999. case I915_MADV_DONTNEED:
  3000. case I915_MADV_WILLNEED:
  3001. break;
  3002. default:
  3003. return -EINVAL;
  3004. }
  3005. ret = i915_mutex_lock_interruptible(dev);
  3006. if (ret)
  3007. return ret;
  3008. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3009. if (&obj->base == NULL) {
  3010. ret = -ENOENT;
  3011. goto unlock;
  3012. }
  3013. if (obj->pin_count) {
  3014. ret = -EINVAL;
  3015. goto out;
  3016. }
  3017. if (obj->madv != __I915_MADV_PURGED)
  3018. obj->madv = args->madv;
  3019. /* if the object is no longer attached, discard its backing storage */
  3020. if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
  3021. i915_gem_object_truncate(obj);
  3022. args->retained = obj->madv != __I915_MADV_PURGED;
  3023. out:
  3024. drm_gem_object_unreference(&obj->base);
  3025. unlock:
  3026. mutex_unlock(&dev->struct_mutex);
  3027. return ret;
  3028. }
  3029. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3030. const struct drm_i915_gem_object_ops *ops)
  3031. {
  3032. INIT_LIST_HEAD(&obj->mm_list);
  3033. INIT_LIST_HEAD(&obj->gtt_list);
  3034. INIT_LIST_HEAD(&obj->ring_list);
  3035. INIT_LIST_HEAD(&obj->exec_list);
  3036. obj->ops = ops;
  3037. obj->fence_reg = I915_FENCE_REG_NONE;
  3038. obj->madv = I915_MADV_WILLNEED;
  3039. /* Avoid an unnecessary call to unbind on the first bind. */
  3040. obj->map_and_fenceable = true;
  3041. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3042. }
  3043. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3044. .get_pages = i915_gem_object_get_pages_gtt,
  3045. .put_pages = i915_gem_object_put_pages_gtt,
  3046. };
  3047. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3048. size_t size)
  3049. {
  3050. struct drm_i915_gem_object *obj;
  3051. struct address_space *mapping;
  3052. u32 mask;
  3053. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  3054. if (obj == NULL)
  3055. return NULL;
  3056. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3057. kfree(obj);
  3058. return NULL;
  3059. }
  3060. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3061. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3062. /* 965gm cannot relocate objects above 4GiB. */
  3063. mask &= ~__GFP_HIGHMEM;
  3064. mask |= __GFP_DMA32;
  3065. }
  3066. mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3067. mapping_set_gfp_mask(mapping, mask);
  3068. i915_gem_object_init(obj, &i915_gem_object_ops);
  3069. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3070. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3071. if (HAS_LLC(dev)) {
  3072. /* On some devices, we can have the GPU use the LLC (the CPU
  3073. * cache) for about a 10% performance improvement
  3074. * compared to uncached. Graphics requests other than
  3075. * display scanout are coherent with the CPU in
  3076. * accessing this cache. This means in this mode we
  3077. * don't need to clflush on the CPU side, and on the
  3078. * GPU side we only need to flush internal caches to
  3079. * get data visible to the CPU.
  3080. *
  3081. * However, we maintain the display planes as UC, and so
  3082. * need to rebind when first used as such.
  3083. */
  3084. obj->cache_level = I915_CACHE_LLC;
  3085. } else
  3086. obj->cache_level = I915_CACHE_NONE;
  3087. return obj;
  3088. }
  3089. int i915_gem_init_object(struct drm_gem_object *obj)
  3090. {
  3091. BUG();
  3092. return 0;
  3093. }
  3094. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3095. {
  3096. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3097. struct drm_device *dev = obj->base.dev;
  3098. drm_i915_private_t *dev_priv = dev->dev_private;
  3099. trace_i915_gem_object_destroy(obj);
  3100. if (obj->phys_obj)
  3101. i915_gem_detach_phys_object(dev, obj);
  3102. obj->pin_count = 0;
  3103. if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
  3104. bool was_interruptible;
  3105. was_interruptible = dev_priv->mm.interruptible;
  3106. dev_priv->mm.interruptible = false;
  3107. WARN_ON(i915_gem_object_unbind(obj));
  3108. dev_priv->mm.interruptible = was_interruptible;
  3109. }
  3110. obj->pages_pin_count = 0;
  3111. i915_gem_object_put_pages(obj);
  3112. i915_gem_object_free_mmap_offset(obj);
  3113. BUG_ON(obj->pages);
  3114. if (obj->base.import_attach)
  3115. drm_prime_gem_destroy(&obj->base, NULL);
  3116. drm_gem_object_release(&obj->base);
  3117. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3118. kfree(obj->bit_17);
  3119. kfree(obj);
  3120. }
  3121. int
  3122. i915_gem_idle(struct drm_device *dev)
  3123. {
  3124. drm_i915_private_t *dev_priv = dev->dev_private;
  3125. int ret;
  3126. mutex_lock(&dev->struct_mutex);
  3127. if (dev_priv->mm.suspended) {
  3128. mutex_unlock(&dev->struct_mutex);
  3129. return 0;
  3130. }
  3131. ret = i915_gpu_idle(dev);
  3132. if (ret) {
  3133. mutex_unlock(&dev->struct_mutex);
  3134. return ret;
  3135. }
  3136. i915_gem_retire_requests(dev);
  3137. /* Under UMS, be paranoid and evict. */
  3138. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3139. i915_gem_evict_everything(dev);
  3140. i915_gem_reset_fences(dev);
  3141. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3142. * We need to replace this with a semaphore, or something.
  3143. * And not confound mm.suspended!
  3144. */
  3145. dev_priv->mm.suspended = 1;
  3146. del_timer_sync(&dev_priv->hangcheck_timer);
  3147. i915_kernel_lost_context(dev);
  3148. i915_gem_cleanup_ringbuffer(dev);
  3149. mutex_unlock(&dev->struct_mutex);
  3150. /* Cancel the retire work handler, which should be idle now. */
  3151. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3152. return 0;
  3153. }
  3154. void i915_gem_l3_remap(struct drm_device *dev)
  3155. {
  3156. drm_i915_private_t *dev_priv = dev->dev_private;
  3157. u32 misccpctl;
  3158. int i;
  3159. if (!IS_IVYBRIDGE(dev))
  3160. return;
  3161. if (!dev_priv->l3_parity.remap_info)
  3162. return;
  3163. misccpctl = I915_READ(GEN7_MISCCPCTL);
  3164. I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  3165. POSTING_READ(GEN7_MISCCPCTL);
  3166. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3167. u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
  3168. if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
  3169. DRM_DEBUG("0x%x was already programmed to %x\n",
  3170. GEN7_L3LOG_BASE + i, remap);
  3171. if (remap && !dev_priv->l3_parity.remap_info[i/4])
  3172. DRM_DEBUG_DRIVER("Clearing remapped register\n");
  3173. I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
  3174. }
  3175. /* Make sure all the writes land before disabling dop clock gating */
  3176. POSTING_READ(GEN7_L3LOG_BASE);
  3177. I915_WRITE(GEN7_MISCCPCTL, misccpctl);
  3178. }
  3179. void i915_gem_init_swizzling(struct drm_device *dev)
  3180. {
  3181. drm_i915_private_t *dev_priv = dev->dev_private;
  3182. if (INTEL_INFO(dev)->gen < 5 ||
  3183. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3184. return;
  3185. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3186. DISP_TILE_SURFACE_SWIZZLING);
  3187. if (IS_GEN5(dev))
  3188. return;
  3189. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3190. if (IS_GEN6(dev))
  3191. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3192. else
  3193. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3194. }
  3195. static bool
  3196. intel_enable_blt(struct drm_device *dev)
  3197. {
  3198. if (!HAS_BLT(dev))
  3199. return false;
  3200. /* The blitter was dysfunctional on early prototypes */
  3201. if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  3202. DRM_INFO("BLT not supported on this pre-production hardware;"
  3203. " graphics performance will be degraded.\n");
  3204. return false;
  3205. }
  3206. return true;
  3207. }
  3208. int
  3209. i915_gem_init_hw(struct drm_device *dev)
  3210. {
  3211. drm_i915_private_t *dev_priv = dev->dev_private;
  3212. int ret;
  3213. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3214. return -EIO;
  3215. if (IS_HASWELL(dev) && (I915_READ(0x120010) == 1))
  3216. I915_WRITE(0x9008, I915_READ(0x9008) | 0xf0000);
  3217. i915_gem_l3_remap(dev);
  3218. i915_gem_init_swizzling(dev);
  3219. ret = intel_init_render_ring_buffer(dev);
  3220. if (ret)
  3221. return ret;
  3222. if (HAS_BSD(dev)) {
  3223. ret = intel_init_bsd_ring_buffer(dev);
  3224. if (ret)
  3225. goto cleanup_render_ring;
  3226. }
  3227. if (intel_enable_blt(dev)) {
  3228. ret = intel_init_blt_ring_buffer(dev);
  3229. if (ret)
  3230. goto cleanup_bsd_ring;
  3231. }
  3232. dev_priv->next_seqno = 1;
  3233. /*
  3234. * XXX: There was some w/a described somewhere suggesting loading
  3235. * contexts before PPGTT.
  3236. */
  3237. i915_gem_context_init(dev);
  3238. i915_gem_init_ppgtt(dev);
  3239. return 0;
  3240. cleanup_bsd_ring:
  3241. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3242. cleanup_render_ring:
  3243. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3244. return ret;
  3245. }
  3246. static bool
  3247. intel_enable_ppgtt(struct drm_device *dev)
  3248. {
  3249. if (i915_enable_ppgtt >= 0)
  3250. return i915_enable_ppgtt;
  3251. #ifdef CONFIG_INTEL_IOMMU
  3252. /* Disable ppgtt on SNB if VT-d is on. */
  3253. if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
  3254. return false;
  3255. #endif
  3256. return true;
  3257. }
  3258. int i915_gem_init(struct drm_device *dev)
  3259. {
  3260. struct drm_i915_private *dev_priv = dev->dev_private;
  3261. unsigned long gtt_size, mappable_size;
  3262. int ret;
  3263. gtt_size = dev_priv->mm.gtt->gtt_total_entries << PAGE_SHIFT;
  3264. mappable_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
  3265. mutex_lock(&dev->struct_mutex);
  3266. if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
  3267. /* PPGTT pdes are stolen from global gtt ptes, so shrink the
  3268. * aperture accordingly when using aliasing ppgtt. */
  3269. gtt_size -= I915_PPGTT_PD_ENTRIES*PAGE_SIZE;
  3270. i915_gem_init_global_gtt(dev, 0, mappable_size, gtt_size);
  3271. ret = i915_gem_init_aliasing_ppgtt(dev);
  3272. if (ret) {
  3273. mutex_unlock(&dev->struct_mutex);
  3274. return ret;
  3275. }
  3276. } else {
  3277. /* Let GEM Manage all of the aperture.
  3278. *
  3279. * However, leave one page at the end still bound to the scratch
  3280. * page. There are a number of places where the hardware
  3281. * apparently prefetches past the end of the object, and we've
  3282. * seen multiple hangs with the GPU head pointer stuck in a
  3283. * batchbuffer bound at the last page of the aperture. One page
  3284. * should be enough to keep any prefetching inside of the
  3285. * aperture.
  3286. */
  3287. i915_gem_init_global_gtt(dev, 0, mappable_size,
  3288. gtt_size);
  3289. }
  3290. ret = i915_gem_init_hw(dev);
  3291. mutex_unlock(&dev->struct_mutex);
  3292. if (ret) {
  3293. i915_gem_cleanup_aliasing_ppgtt(dev);
  3294. return ret;
  3295. }
  3296. /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
  3297. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3298. dev_priv->dri1.allow_batchbuffer = 1;
  3299. return 0;
  3300. }
  3301. void
  3302. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3303. {
  3304. drm_i915_private_t *dev_priv = dev->dev_private;
  3305. struct intel_ring_buffer *ring;
  3306. int i;
  3307. for_each_ring(ring, dev_priv, i)
  3308. intel_cleanup_ring_buffer(ring);
  3309. }
  3310. int
  3311. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3312. struct drm_file *file_priv)
  3313. {
  3314. drm_i915_private_t *dev_priv = dev->dev_private;
  3315. int ret;
  3316. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3317. return 0;
  3318. if (atomic_read(&dev_priv->mm.wedged)) {
  3319. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3320. atomic_set(&dev_priv->mm.wedged, 0);
  3321. }
  3322. mutex_lock(&dev->struct_mutex);
  3323. dev_priv->mm.suspended = 0;
  3324. ret = i915_gem_init_hw(dev);
  3325. if (ret != 0) {
  3326. mutex_unlock(&dev->struct_mutex);
  3327. return ret;
  3328. }
  3329. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  3330. mutex_unlock(&dev->struct_mutex);
  3331. ret = drm_irq_install(dev);
  3332. if (ret)
  3333. goto cleanup_ringbuffer;
  3334. return 0;
  3335. cleanup_ringbuffer:
  3336. mutex_lock(&dev->struct_mutex);
  3337. i915_gem_cleanup_ringbuffer(dev);
  3338. dev_priv->mm.suspended = 1;
  3339. mutex_unlock(&dev->struct_mutex);
  3340. return ret;
  3341. }
  3342. int
  3343. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3344. struct drm_file *file_priv)
  3345. {
  3346. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3347. return 0;
  3348. drm_irq_uninstall(dev);
  3349. return i915_gem_idle(dev);
  3350. }
  3351. void
  3352. i915_gem_lastclose(struct drm_device *dev)
  3353. {
  3354. int ret;
  3355. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3356. return;
  3357. ret = i915_gem_idle(dev);
  3358. if (ret)
  3359. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3360. }
  3361. static void
  3362. init_ring_lists(struct intel_ring_buffer *ring)
  3363. {
  3364. INIT_LIST_HEAD(&ring->active_list);
  3365. INIT_LIST_HEAD(&ring->request_list);
  3366. }
  3367. void
  3368. i915_gem_load(struct drm_device *dev)
  3369. {
  3370. int i;
  3371. drm_i915_private_t *dev_priv = dev->dev_private;
  3372. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  3373. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  3374. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  3375. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  3376. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3377. for (i = 0; i < I915_NUM_RINGS; i++)
  3378. init_ring_lists(&dev_priv->ring[i]);
  3379. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  3380. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3381. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3382. i915_gem_retire_work_handler);
  3383. init_completion(&dev_priv->error_completion);
  3384. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3385. if (IS_GEN3(dev)) {
  3386. I915_WRITE(MI_ARB_STATE,
  3387. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  3388. }
  3389. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3390. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3391. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3392. dev_priv->fence_reg_start = 3;
  3393. if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3394. dev_priv->num_fence_regs = 16;
  3395. else
  3396. dev_priv->num_fence_regs = 8;
  3397. /* Initialize fence registers to zero */
  3398. i915_gem_reset_fences(dev);
  3399. i915_gem_detect_bit_6_swizzle(dev);
  3400. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3401. dev_priv->mm.interruptible = true;
  3402. dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
  3403. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  3404. register_shrinker(&dev_priv->mm.inactive_shrinker);
  3405. }
  3406. /*
  3407. * Create a physically contiguous memory object for this object
  3408. * e.g. for cursor + overlay regs
  3409. */
  3410. static int i915_gem_init_phys_object(struct drm_device *dev,
  3411. int id, int size, int align)
  3412. {
  3413. drm_i915_private_t *dev_priv = dev->dev_private;
  3414. struct drm_i915_gem_phys_object *phys_obj;
  3415. int ret;
  3416. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3417. return 0;
  3418. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3419. if (!phys_obj)
  3420. return -ENOMEM;
  3421. phys_obj->id = id;
  3422. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3423. if (!phys_obj->handle) {
  3424. ret = -ENOMEM;
  3425. goto kfree_obj;
  3426. }
  3427. #ifdef CONFIG_X86
  3428. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3429. #endif
  3430. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3431. return 0;
  3432. kfree_obj:
  3433. kfree(phys_obj);
  3434. return ret;
  3435. }
  3436. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3437. {
  3438. drm_i915_private_t *dev_priv = dev->dev_private;
  3439. struct drm_i915_gem_phys_object *phys_obj;
  3440. if (!dev_priv->mm.phys_objs[id - 1])
  3441. return;
  3442. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3443. if (phys_obj->cur_obj) {
  3444. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3445. }
  3446. #ifdef CONFIG_X86
  3447. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3448. #endif
  3449. drm_pci_free(dev, phys_obj->handle);
  3450. kfree(phys_obj);
  3451. dev_priv->mm.phys_objs[id - 1] = NULL;
  3452. }
  3453. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3454. {
  3455. int i;
  3456. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3457. i915_gem_free_phys_object(dev, i);
  3458. }
  3459. void i915_gem_detach_phys_object(struct drm_device *dev,
  3460. struct drm_i915_gem_object *obj)
  3461. {
  3462. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3463. char *vaddr;
  3464. int i;
  3465. int page_count;
  3466. if (!obj->phys_obj)
  3467. return;
  3468. vaddr = obj->phys_obj->handle->vaddr;
  3469. page_count = obj->base.size / PAGE_SIZE;
  3470. for (i = 0; i < page_count; i++) {
  3471. struct page *page = shmem_read_mapping_page(mapping, i);
  3472. if (!IS_ERR(page)) {
  3473. char *dst = kmap_atomic(page);
  3474. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  3475. kunmap_atomic(dst);
  3476. drm_clflush_pages(&page, 1);
  3477. set_page_dirty(page);
  3478. mark_page_accessed(page);
  3479. page_cache_release(page);
  3480. }
  3481. }
  3482. i915_gem_chipset_flush(dev);
  3483. obj->phys_obj->cur_obj = NULL;
  3484. obj->phys_obj = NULL;
  3485. }
  3486. int
  3487. i915_gem_attach_phys_object(struct drm_device *dev,
  3488. struct drm_i915_gem_object *obj,
  3489. int id,
  3490. int align)
  3491. {
  3492. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3493. drm_i915_private_t *dev_priv = dev->dev_private;
  3494. int ret = 0;
  3495. int page_count;
  3496. int i;
  3497. if (id > I915_MAX_PHYS_OBJECT)
  3498. return -EINVAL;
  3499. if (obj->phys_obj) {
  3500. if (obj->phys_obj->id == id)
  3501. return 0;
  3502. i915_gem_detach_phys_object(dev, obj);
  3503. }
  3504. /* create a new object */
  3505. if (!dev_priv->mm.phys_objs[id - 1]) {
  3506. ret = i915_gem_init_phys_object(dev, id,
  3507. obj->base.size, align);
  3508. if (ret) {
  3509. DRM_ERROR("failed to init phys object %d size: %zu\n",
  3510. id, obj->base.size);
  3511. return ret;
  3512. }
  3513. }
  3514. /* bind to the object */
  3515. obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3516. obj->phys_obj->cur_obj = obj;
  3517. page_count = obj->base.size / PAGE_SIZE;
  3518. for (i = 0; i < page_count; i++) {
  3519. struct page *page;
  3520. char *dst, *src;
  3521. page = shmem_read_mapping_page(mapping, i);
  3522. if (IS_ERR(page))
  3523. return PTR_ERR(page);
  3524. src = kmap_atomic(page);
  3525. dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3526. memcpy(dst, src, PAGE_SIZE);
  3527. kunmap_atomic(src);
  3528. mark_page_accessed(page);
  3529. page_cache_release(page);
  3530. }
  3531. return 0;
  3532. }
  3533. static int
  3534. i915_gem_phys_pwrite(struct drm_device *dev,
  3535. struct drm_i915_gem_object *obj,
  3536. struct drm_i915_gem_pwrite *args,
  3537. struct drm_file *file_priv)
  3538. {
  3539. void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
  3540. char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
  3541. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  3542. unsigned long unwritten;
  3543. /* The physical object once assigned is fixed for the lifetime
  3544. * of the obj, so we can safely drop the lock and continue
  3545. * to access vaddr.
  3546. */
  3547. mutex_unlock(&dev->struct_mutex);
  3548. unwritten = copy_from_user(vaddr, user_data, args->size);
  3549. mutex_lock(&dev->struct_mutex);
  3550. if (unwritten)
  3551. return -EFAULT;
  3552. }
  3553. i915_gem_chipset_flush(dev);
  3554. return 0;
  3555. }
  3556. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3557. {
  3558. struct drm_i915_file_private *file_priv = file->driver_priv;
  3559. /* Clean up our request list when the client is going away, so that
  3560. * later retire_requests won't dereference our soon-to-be-gone
  3561. * file_priv.
  3562. */
  3563. spin_lock(&file_priv->mm.lock);
  3564. while (!list_empty(&file_priv->mm.request_list)) {
  3565. struct drm_i915_gem_request *request;
  3566. request = list_first_entry(&file_priv->mm.request_list,
  3567. struct drm_i915_gem_request,
  3568. client_list);
  3569. list_del(&request->client_list);
  3570. request->file_priv = NULL;
  3571. }
  3572. spin_unlock(&file_priv->mm.lock);
  3573. }
  3574. static int
  3575. i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
  3576. {
  3577. struct drm_i915_private *dev_priv =
  3578. container_of(shrinker,
  3579. struct drm_i915_private,
  3580. mm.inactive_shrinker);
  3581. struct drm_device *dev = dev_priv->dev;
  3582. struct drm_i915_gem_object *obj;
  3583. int nr_to_scan = sc->nr_to_scan;
  3584. int cnt;
  3585. if (!mutex_trylock(&dev->struct_mutex))
  3586. return 0;
  3587. if (nr_to_scan) {
  3588. nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
  3589. if (nr_to_scan > 0)
  3590. i915_gem_shrink_all(dev_priv);
  3591. }
  3592. cnt = 0;
  3593. list_for_each_entry(obj, &dev_priv->mm.unbound_list, gtt_list)
  3594. if (obj->pages_pin_count == 0)
  3595. cnt += obj->base.size >> PAGE_SHIFT;
  3596. list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
  3597. if (obj->pin_count == 0 && obj->pages_pin_count == 0)
  3598. cnt += obj->base.size >> PAGE_SHIFT;
  3599. mutex_unlock(&dev->struct_mutex);
  3600. return cnt;
  3601. }