i915_gem.c 101 KB

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