i915_gem.c 104 KB

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