i915_gem.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588
  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 <linux/swap.h>
  32. #include <linux/pci.h>
  33. #define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
  34. static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
  35. static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
  36. static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
  37. static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
  38. int write);
  39. static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  40. uint64_t offset,
  41. uint64_t size);
  42. static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
  43. static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
  44. static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
  45. static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
  46. static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
  47. unsigned alignment);
  48. static int i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write);
  49. static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
  50. static int i915_gem_evict_something(struct drm_device *dev);
  51. static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  52. struct drm_i915_gem_pwrite *args,
  53. struct drm_file *file_priv);
  54. int i915_gem_do_init(struct drm_device *dev, unsigned long start,
  55. unsigned long end)
  56. {
  57. drm_i915_private_t *dev_priv = dev->dev_private;
  58. if (start >= end ||
  59. (start & (PAGE_SIZE - 1)) != 0 ||
  60. (end & (PAGE_SIZE - 1)) != 0) {
  61. return -EINVAL;
  62. }
  63. drm_mm_init(&dev_priv->mm.gtt_space, start,
  64. end - start);
  65. dev->gtt_total = (uint32_t) (end - start);
  66. return 0;
  67. }
  68. int
  69. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  70. struct drm_file *file_priv)
  71. {
  72. struct drm_i915_gem_init *args = data;
  73. int ret;
  74. mutex_lock(&dev->struct_mutex);
  75. ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
  76. mutex_unlock(&dev->struct_mutex);
  77. return ret;
  78. }
  79. int
  80. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  81. struct drm_file *file_priv)
  82. {
  83. struct drm_i915_gem_get_aperture *args = data;
  84. if (!(dev->driver->driver_features & DRIVER_GEM))
  85. return -ENODEV;
  86. args->aper_size = dev->gtt_total;
  87. args->aper_available_size = (args->aper_size -
  88. atomic_read(&dev->pin_memory));
  89. return 0;
  90. }
  91. /**
  92. * Creates a new mm object and returns a handle to it.
  93. */
  94. int
  95. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  96. struct drm_file *file_priv)
  97. {
  98. struct drm_i915_gem_create *args = data;
  99. struct drm_gem_object *obj;
  100. int handle, ret;
  101. args->size = roundup(args->size, PAGE_SIZE);
  102. /* Allocate the new object */
  103. obj = drm_gem_object_alloc(dev, args->size);
  104. if (obj == NULL)
  105. return -ENOMEM;
  106. ret = drm_gem_handle_create(file_priv, obj, &handle);
  107. mutex_lock(&dev->struct_mutex);
  108. drm_gem_object_handle_unreference(obj);
  109. mutex_unlock(&dev->struct_mutex);
  110. if (ret)
  111. return ret;
  112. args->handle = handle;
  113. return 0;
  114. }
  115. /**
  116. * Reads data from the object referenced by handle.
  117. *
  118. * On error, the contents of *data are undefined.
  119. */
  120. int
  121. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  122. struct drm_file *file_priv)
  123. {
  124. struct drm_i915_gem_pread *args = data;
  125. struct drm_gem_object *obj;
  126. struct drm_i915_gem_object *obj_priv;
  127. ssize_t read;
  128. loff_t offset;
  129. int ret;
  130. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  131. if (obj == NULL)
  132. return -EBADF;
  133. obj_priv = obj->driver_private;
  134. /* Bounds check source.
  135. *
  136. * XXX: This could use review for overflow issues...
  137. */
  138. if (args->offset > obj->size || args->size > obj->size ||
  139. args->offset + args->size > obj->size) {
  140. drm_gem_object_unreference(obj);
  141. return -EINVAL;
  142. }
  143. mutex_lock(&dev->struct_mutex);
  144. ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
  145. args->size);
  146. if (ret != 0) {
  147. drm_gem_object_unreference(obj);
  148. mutex_unlock(&dev->struct_mutex);
  149. return ret;
  150. }
  151. offset = args->offset;
  152. read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
  153. args->size, &offset);
  154. if (read != args->size) {
  155. drm_gem_object_unreference(obj);
  156. mutex_unlock(&dev->struct_mutex);
  157. if (read < 0)
  158. return read;
  159. else
  160. return -EINVAL;
  161. }
  162. drm_gem_object_unreference(obj);
  163. mutex_unlock(&dev->struct_mutex);
  164. return 0;
  165. }
  166. /* This is the fast write path which cannot handle
  167. * page faults in the source data
  168. */
  169. static inline int
  170. fast_user_write(struct io_mapping *mapping,
  171. loff_t page_base, int page_offset,
  172. char __user *user_data,
  173. int length)
  174. {
  175. char *vaddr_atomic;
  176. unsigned long unwritten;
  177. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  178. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
  179. user_data, length);
  180. io_mapping_unmap_atomic(vaddr_atomic);
  181. if (unwritten)
  182. return -EFAULT;
  183. return 0;
  184. }
  185. /* Here's the write path which can sleep for
  186. * page faults
  187. */
  188. static inline int
  189. slow_user_write(struct io_mapping *mapping,
  190. loff_t page_base, int page_offset,
  191. char __user *user_data,
  192. int length)
  193. {
  194. char __iomem *vaddr;
  195. unsigned long unwritten;
  196. vaddr = io_mapping_map_wc(mapping, page_base);
  197. if (vaddr == NULL)
  198. return -EFAULT;
  199. unwritten = __copy_from_user(vaddr + page_offset,
  200. user_data, length);
  201. io_mapping_unmap(vaddr);
  202. if (unwritten)
  203. return -EFAULT;
  204. return 0;
  205. }
  206. static int
  207. i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  208. struct drm_i915_gem_pwrite *args,
  209. struct drm_file *file_priv)
  210. {
  211. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  212. drm_i915_private_t *dev_priv = dev->dev_private;
  213. ssize_t remain;
  214. loff_t offset, page_base;
  215. char __user *user_data;
  216. int page_offset, page_length;
  217. int ret;
  218. user_data = (char __user *) (uintptr_t) args->data_ptr;
  219. remain = args->size;
  220. if (!access_ok(VERIFY_READ, user_data, remain))
  221. return -EFAULT;
  222. mutex_lock(&dev->struct_mutex);
  223. ret = i915_gem_object_pin(obj, 0);
  224. if (ret) {
  225. mutex_unlock(&dev->struct_mutex);
  226. return ret;
  227. }
  228. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  229. if (ret)
  230. goto fail;
  231. obj_priv = obj->driver_private;
  232. offset = obj_priv->gtt_offset + args->offset;
  233. obj_priv->dirty = 1;
  234. while (remain > 0) {
  235. /* Operation in this page
  236. *
  237. * page_base = page offset within aperture
  238. * page_offset = offset within page
  239. * page_length = bytes to copy for this page
  240. */
  241. page_base = (offset & ~(PAGE_SIZE-1));
  242. page_offset = offset & (PAGE_SIZE-1);
  243. page_length = remain;
  244. if ((page_offset + remain) > PAGE_SIZE)
  245. page_length = PAGE_SIZE - page_offset;
  246. ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
  247. page_offset, user_data, page_length);
  248. /* If we get a fault while copying data, then (presumably) our
  249. * source page isn't available. In this case, use the
  250. * non-atomic function
  251. */
  252. if (ret) {
  253. ret = slow_user_write (dev_priv->mm.gtt_mapping,
  254. page_base, page_offset,
  255. user_data, page_length);
  256. if (ret)
  257. goto fail;
  258. }
  259. remain -= page_length;
  260. user_data += page_length;
  261. offset += page_length;
  262. }
  263. fail:
  264. i915_gem_object_unpin(obj);
  265. mutex_unlock(&dev->struct_mutex);
  266. return ret;
  267. }
  268. static int
  269. i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  270. struct drm_i915_gem_pwrite *args,
  271. struct drm_file *file_priv)
  272. {
  273. int ret;
  274. loff_t offset;
  275. ssize_t written;
  276. mutex_lock(&dev->struct_mutex);
  277. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  278. if (ret) {
  279. mutex_unlock(&dev->struct_mutex);
  280. return ret;
  281. }
  282. offset = args->offset;
  283. written = vfs_write(obj->filp,
  284. (char __user *)(uintptr_t) args->data_ptr,
  285. args->size, &offset);
  286. if (written != args->size) {
  287. mutex_unlock(&dev->struct_mutex);
  288. if (written < 0)
  289. return written;
  290. else
  291. return -EINVAL;
  292. }
  293. mutex_unlock(&dev->struct_mutex);
  294. return 0;
  295. }
  296. /**
  297. * Writes data to the object referenced by handle.
  298. *
  299. * On error, the contents of the buffer that were to be modified are undefined.
  300. */
  301. int
  302. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  303. struct drm_file *file_priv)
  304. {
  305. struct drm_i915_gem_pwrite *args = data;
  306. struct drm_gem_object *obj;
  307. struct drm_i915_gem_object *obj_priv;
  308. int ret = 0;
  309. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  310. if (obj == NULL)
  311. return -EBADF;
  312. obj_priv = obj->driver_private;
  313. /* Bounds check destination.
  314. *
  315. * XXX: This could use review for overflow issues...
  316. */
  317. if (args->offset > obj->size || args->size > obj->size ||
  318. args->offset + args->size > obj->size) {
  319. drm_gem_object_unreference(obj);
  320. return -EINVAL;
  321. }
  322. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  323. * it would end up going through the fenced access, and we'll get
  324. * different detiling behavior between reading and writing.
  325. * pread/pwrite currently are reading and writing from the CPU
  326. * perspective, requiring manual detiling by the client.
  327. */
  328. if (obj_priv->phys_obj)
  329. ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
  330. else if (obj_priv->tiling_mode == I915_TILING_NONE &&
  331. dev->gtt_total != 0)
  332. ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
  333. else
  334. ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
  335. #if WATCH_PWRITE
  336. if (ret)
  337. DRM_INFO("pwrite failed %d\n", ret);
  338. #endif
  339. drm_gem_object_unreference(obj);
  340. return ret;
  341. }
  342. /**
  343. * Called when user space prepares to use an object with the CPU, either
  344. * through the mmap ioctl's mapping or a GTT mapping.
  345. */
  346. int
  347. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  348. struct drm_file *file_priv)
  349. {
  350. struct drm_i915_gem_set_domain *args = data;
  351. struct drm_gem_object *obj;
  352. uint32_t read_domains = args->read_domains;
  353. uint32_t write_domain = args->write_domain;
  354. int ret;
  355. if (!(dev->driver->driver_features & DRIVER_GEM))
  356. return -ENODEV;
  357. /* Only handle setting domains to types used by the CPU. */
  358. if (write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
  359. return -EINVAL;
  360. if (read_domains & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
  361. return -EINVAL;
  362. /* Having something in the write domain implies it's in the read
  363. * domain, and only that read domain. Enforce that in the request.
  364. */
  365. if (write_domain != 0 && read_domains != write_domain)
  366. return -EINVAL;
  367. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  368. if (obj == NULL)
  369. return -EBADF;
  370. mutex_lock(&dev->struct_mutex);
  371. #if WATCH_BUF
  372. DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
  373. obj, obj->size, read_domains, write_domain);
  374. #endif
  375. if (read_domains & I915_GEM_DOMAIN_GTT) {
  376. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  377. /* Silently promote "you're not bound, there was nothing to do"
  378. * to success, since the client was just asking us to
  379. * make sure everything was done.
  380. */
  381. if (ret == -EINVAL)
  382. ret = 0;
  383. } else {
  384. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  385. }
  386. drm_gem_object_unreference(obj);
  387. mutex_unlock(&dev->struct_mutex);
  388. return ret;
  389. }
  390. /**
  391. * Called when user space has done writes to this buffer
  392. */
  393. int
  394. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  395. struct drm_file *file_priv)
  396. {
  397. struct drm_i915_gem_sw_finish *args = data;
  398. struct drm_gem_object *obj;
  399. struct drm_i915_gem_object *obj_priv;
  400. int ret = 0;
  401. if (!(dev->driver->driver_features & DRIVER_GEM))
  402. return -ENODEV;
  403. mutex_lock(&dev->struct_mutex);
  404. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  405. if (obj == NULL) {
  406. mutex_unlock(&dev->struct_mutex);
  407. return -EBADF;
  408. }
  409. #if WATCH_BUF
  410. DRM_INFO("%s: sw_finish %d (%p %d)\n",
  411. __func__, args->handle, obj, obj->size);
  412. #endif
  413. obj_priv = obj->driver_private;
  414. /* Pinned buffers may be scanout, so flush the cache */
  415. if (obj_priv->pin_count)
  416. i915_gem_object_flush_cpu_write_domain(obj);
  417. drm_gem_object_unreference(obj);
  418. mutex_unlock(&dev->struct_mutex);
  419. return ret;
  420. }
  421. /**
  422. * Maps the contents of an object, returning the address it is mapped
  423. * into.
  424. *
  425. * While the mapping holds a reference on the contents of the object, it doesn't
  426. * imply a ref on the object itself.
  427. */
  428. int
  429. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  430. struct drm_file *file_priv)
  431. {
  432. struct drm_i915_gem_mmap *args = data;
  433. struct drm_gem_object *obj;
  434. loff_t offset;
  435. unsigned long addr;
  436. if (!(dev->driver->driver_features & DRIVER_GEM))
  437. return -ENODEV;
  438. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  439. if (obj == NULL)
  440. return -EBADF;
  441. offset = args->offset;
  442. down_write(&current->mm->mmap_sem);
  443. addr = do_mmap(obj->filp, 0, args->size,
  444. PROT_READ | PROT_WRITE, MAP_SHARED,
  445. args->offset);
  446. up_write(&current->mm->mmap_sem);
  447. mutex_lock(&dev->struct_mutex);
  448. drm_gem_object_unreference(obj);
  449. mutex_unlock(&dev->struct_mutex);
  450. if (IS_ERR((void *)addr))
  451. return addr;
  452. args->addr_ptr = (uint64_t) addr;
  453. return 0;
  454. }
  455. /**
  456. * i915_gem_fault - fault a page into the GTT
  457. * vma: VMA in question
  458. * vmf: fault info
  459. *
  460. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  461. * from userspace. The fault handler takes care of binding the object to
  462. * the GTT (if needed), allocating and programming a fence register (again,
  463. * only if needed based on whether the old reg is still valid or the object
  464. * is tiled) and inserting a new PTE into the faulting process.
  465. *
  466. * Note that the faulting process may involve evicting existing objects
  467. * from the GTT and/or fence registers to make room. So performance may
  468. * suffer if the GTT working set is large or there are few fence registers
  469. * left.
  470. */
  471. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  472. {
  473. struct drm_gem_object *obj = vma->vm_private_data;
  474. struct drm_device *dev = obj->dev;
  475. struct drm_i915_private *dev_priv = dev->dev_private;
  476. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  477. pgoff_t page_offset;
  478. unsigned long pfn;
  479. int ret = 0;
  480. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  481. /* We don't use vmf->pgoff since that has the fake offset */
  482. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  483. PAGE_SHIFT;
  484. /* Now bind it into the GTT if needed */
  485. mutex_lock(&dev->struct_mutex);
  486. if (!obj_priv->gtt_space) {
  487. ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
  488. if (ret) {
  489. mutex_unlock(&dev->struct_mutex);
  490. return VM_FAULT_SIGBUS;
  491. }
  492. list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
  493. }
  494. /* Need a new fence register? */
  495. if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
  496. obj_priv->tiling_mode != I915_TILING_NONE) {
  497. ret = i915_gem_object_get_fence_reg(obj, write);
  498. if (ret) {
  499. mutex_unlock(&dev->struct_mutex);
  500. return VM_FAULT_SIGBUS;
  501. }
  502. }
  503. pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
  504. page_offset;
  505. /* Finally, remap it using the new GTT offset */
  506. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  507. mutex_unlock(&dev->struct_mutex);
  508. switch (ret) {
  509. case -ENOMEM:
  510. case -EAGAIN:
  511. return VM_FAULT_OOM;
  512. case -EFAULT:
  513. return VM_FAULT_SIGBUS;
  514. default:
  515. return VM_FAULT_NOPAGE;
  516. }
  517. }
  518. /**
  519. * i915_gem_create_mmap_offset - create a fake mmap offset for an object
  520. * @obj: obj in question
  521. *
  522. * GEM memory mapping works by handing back to userspace a fake mmap offset
  523. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  524. * up the object based on the offset and sets up the various memory mapping
  525. * structures.
  526. *
  527. * This routine allocates and attaches a fake offset for @obj.
  528. */
  529. static int
  530. i915_gem_create_mmap_offset(struct drm_gem_object *obj)
  531. {
  532. struct drm_device *dev = obj->dev;
  533. struct drm_gem_mm *mm = dev->mm_private;
  534. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  535. struct drm_map_list *list;
  536. struct drm_map *map;
  537. int ret = 0;
  538. /* Set the object up for mmap'ing */
  539. list = &obj->map_list;
  540. list->map = drm_calloc(1, sizeof(struct drm_map_list),
  541. DRM_MEM_DRIVER);
  542. if (!list->map)
  543. return -ENOMEM;
  544. map = list->map;
  545. map->type = _DRM_GEM;
  546. map->size = obj->size;
  547. map->handle = obj;
  548. /* Get a DRM GEM mmap offset allocated... */
  549. list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
  550. obj->size / PAGE_SIZE, 0, 0);
  551. if (!list->file_offset_node) {
  552. DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
  553. ret = -ENOMEM;
  554. goto out_free_list;
  555. }
  556. list->file_offset_node = drm_mm_get_block(list->file_offset_node,
  557. obj->size / PAGE_SIZE, 0);
  558. if (!list->file_offset_node) {
  559. ret = -ENOMEM;
  560. goto out_free_list;
  561. }
  562. list->hash.key = list->file_offset_node->start;
  563. if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
  564. DRM_ERROR("failed to add to map hash\n");
  565. goto out_free_mm;
  566. }
  567. /* By now we should be all set, any drm_mmap request on the offset
  568. * below will get to our mmap & fault handler */
  569. obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
  570. return 0;
  571. out_free_mm:
  572. drm_mm_put_block(list->file_offset_node);
  573. out_free_list:
  574. drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER);
  575. return ret;
  576. }
  577. static void
  578. i915_gem_free_mmap_offset(struct drm_gem_object *obj)
  579. {
  580. struct drm_device *dev = obj->dev;
  581. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  582. struct drm_gem_mm *mm = dev->mm_private;
  583. struct drm_map_list *list;
  584. list = &obj->map_list;
  585. drm_ht_remove_item(&mm->offset_hash, &list->hash);
  586. if (list->file_offset_node) {
  587. drm_mm_put_block(list->file_offset_node);
  588. list->file_offset_node = NULL;
  589. }
  590. if (list->map) {
  591. drm_free(list->map, sizeof(struct drm_map), DRM_MEM_DRIVER);
  592. list->map = NULL;
  593. }
  594. obj_priv->mmap_offset = 0;
  595. }
  596. /**
  597. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  598. * @obj: object to check
  599. *
  600. * Return the required GTT alignment for an object, taking into account
  601. * potential fence register mapping if needed.
  602. */
  603. static uint32_t
  604. i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
  605. {
  606. struct drm_device *dev = obj->dev;
  607. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  608. int start, i;
  609. /*
  610. * Minimum alignment is 4k (GTT page size), but might be greater
  611. * if a fence register is needed for the object.
  612. */
  613. if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
  614. return 4096;
  615. /*
  616. * Previous chips need to be aligned to the size of the smallest
  617. * fence register that can contain the object.
  618. */
  619. if (IS_I9XX(dev))
  620. start = 1024*1024;
  621. else
  622. start = 512*1024;
  623. for (i = start; i < obj->size; i <<= 1)
  624. ;
  625. return i;
  626. }
  627. /**
  628. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  629. * @dev: DRM device
  630. * @data: GTT mapping ioctl data
  631. * @file_priv: GEM object info
  632. *
  633. * Simply returns the fake offset to userspace so it can mmap it.
  634. * The mmap call will end up in drm_gem_mmap(), which will set things
  635. * up so we can get faults in the handler above.
  636. *
  637. * The fault handler will take care of binding the object into the GTT
  638. * (since it may have been evicted to make room for something), allocating
  639. * a fence register, and mapping the appropriate aperture address into
  640. * userspace.
  641. */
  642. int
  643. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  644. struct drm_file *file_priv)
  645. {
  646. struct drm_i915_gem_mmap_gtt *args = data;
  647. struct drm_i915_private *dev_priv = dev->dev_private;
  648. struct drm_gem_object *obj;
  649. struct drm_i915_gem_object *obj_priv;
  650. int ret;
  651. if (!(dev->driver->driver_features & DRIVER_GEM))
  652. return -ENODEV;
  653. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  654. if (obj == NULL)
  655. return -EBADF;
  656. mutex_lock(&dev->struct_mutex);
  657. obj_priv = obj->driver_private;
  658. if (!obj_priv->mmap_offset) {
  659. ret = i915_gem_create_mmap_offset(obj);
  660. if (ret) {
  661. drm_gem_object_unreference(obj);
  662. mutex_unlock(&dev->struct_mutex);
  663. return ret;
  664. }
  665. }
  666. args->offset = obj_priv->mmap_offset;
  667. obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj);
  668. /* Make sure the alignment is correct for fence regs etc */
  669. if (obj_priv->agp_mem &&
  670. (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) {
  671. drm_gem_object_unreference(obj);
  672. mutex_unlock(&dev->struct_mutex);
  673. return -EINVAL;
  674. }
  675. /*
  676. * Pull it into the GTT so that we have a page list (makes the
  677. * initial fault faster and any subsequent flushing possible).
  678. */
  679. if (!obj_priv->agp_mem) {
  680. ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment);
  681. if (ret) {
  682. drm_gem_object_unreference(obj);
  683. mutex_unlock(&dev->struct_mutex);
  684. return ret;
  685. }
  686. list_add(&obj_priv->list, &dev_priv->mm.inactive_list);
  687. }
  688. drm_gem_object_unreference(obj);
  689. mutex_unlock(&dev->struct_mutex);
  690. return 0;
  691. }
  692. static void
  693. i915_gem_object_free_page_list(struct drm_gem_object *obj)
  694. {
  695. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  696. int page_count = obj->size / PAGE_SIZE;
  697. int i;
  698. if (obj_priv->page_list == NULL)
  699. return;
  700. for (i = 0; i < page_count; i++)
  701. if (obj_priv->page_list[i] != NULL) {
  702. if (obj_priv->dirty)
  703. set_page_dirty(obj_priv->page_list[i]);
  704. mark_page_accessed(obj_priv->page_list[i]);
  705. page_cache_release(obj_priv->page_list[i]);
  706. }
  707. obj_priv->dirty = 0;
  708. drm_free(obj_priv->page_list,
  709. page_count * sizeof(struct page *),
  710. DRM_MEM_DRIVER);
  711. obj_priv->page_list = NULL;
  712. }
  713. static void
  714. i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
  715. {
  716. struct drm_device *dev = obj->dev;
  717. drm_i915_private_t *dev_priv = dev->dev_private;
  718. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  719. /* Add a reference if we're newly entering the active list. */
  720. if (!obj_priv->active) {
  721. drm_gem_object_reference(obj);
  722. obj_priv->active = 1;
  723. }
  724. /* Move from whatever list we were on to the tail of execution. */
  725. list_move_tail(&obj_priv->list,
  726. &dev_priv->mm.active_list);
  727. obj_priv->last_rendering_seqno = seqno;
  728. }
  729. static void
  730. i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
  731. {
  732. struct drm_device *dev = obj->dev;
  733. drm_i915_private_t *dev_priv = dev->dev_private;
  734. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  735. BUG_ON(!obj_priv->active);
  736. list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
  737. obj_priv->last_rendering_seqno = 0;
  738. }
  739. static void
  740. i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
  741. {
  742. struct drm_device *dev = obj->dev;
  743. drm_i915_private_t *dev_priv = dev->dev_private;
  744. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  745. i915_verify_inactive(dev, __FILE__, __LINE__);
  746. if (obj_priv->pin_count != 0)
  747. list_del_init(&obj_priv->list);
  748. else
  749. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  750. obj_priv->last_rendering_seqno = 0;
  751. if (obj_priv->active) {
  752. obj_priv->active = 0;
  753. drm_gem_object_unreference(obj);
  754. }
  755. i915_verify_inactive(dev, __FILE__, __LINE__);
  756. }
  757. /**
  758. * Creates a new sequence number, emitting a write of it to the status page
  759. * plus an interrupt, which will trigger i915_user_interrupt_handler.
  760. *
  761. * Must be called with struct_lock held.
  762. *
  763. * Returned sequence numbers are nonzero on success.
  764. */
  765. static uint32_t
  766. i915_add_request(struct drm_device *dev, uint32_t flush_domains)
  767. {
  768. drm_i915_private_t *dev_priv = dev->dev_private;
  769. struct drm_i915_gem_request *request;
  770. uint32_t seqno;
  771. int was_empty;
  772. RING_LOCALS;
  773. request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
  774. if (request == NULL)
  775. return 0;
  776. /* Grab the seqno we're going to make this request be, and bump the
  777. * next (skipping 0 so it can be the reserved no-seqno value).
  778. */
  779. seqno = dev_priv->mm.next_gem_seqno;
  780. dev_priv->mm.next_gem_seqno++;
  781. if (dev_priv->mm.next_gem_seqno == 0)
  782. dev_priv->mm.next_gem_seqno++;
  783. BEGIN_LP_RING(4);
  784. OUT_RING(MI_STORE_DWORD_INDEX);
  785. OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  786. OUT_RING(seqno);
  787. OUT_RING(MI_USER_INTERRUPT);
  788. ADVANCE_LP_RING();
  789. DRM_DEBUG("%d\n", seqno);
  790. request->seqno = seqno;
  791. request->emitted_jiffies = jiffies;
  792. was_empty = list_empty(&dev_priv->mm.request_list);
  793. list_add_tail(&request->list, &dev_priv->mm.request_list);
  794. /* Associate any objects on the flushing list matching the write
  795. * domain we're flushing with our flush.
  796. */
  797. if (flush_domains != 0) {
  798. struct drm_i915_gem_object *obj_priv, *next;
  799. list_for_each_entry_safe(obj_priv, next,
  800. &dev_priv->mm.flushing_list, list) {
  801. struct drm_gem_object *obj = obj_priv->obj;
  802. if ((obj->write_domain & flush_domains) ==
  803. obj->write_domain) {
  804. obj->write_domain = 0;
  805. i915_gem_object_move_to_active(obj, seqno);
  806. }
  807. }
  808. }
  809. if (was_empty && !dev_priv->mm.suspended)
  810. schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
  811. return seqno;
  812. }
  813. /**
  814. * Command execution barrier
  815. *
  816. * Ensures that all commands in the ring are finished
  817. * before signalling the CPU
  818. */
  819. static uint32_t
  820. i915_retire_commands(struct drm_device *dev)
  821. {
  822. drm_i915_private_t *dev_priv = dev->dev_private;
  823. uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  824. uint32_t flush_domains = 0;
  825. RING_LOCALS;
  826. /* The sampler always gets flushed on i965 (sigh) */
  827. if (IS_I965G(dev))
  828. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  829. BEGIN_LP_RING(2);
  830. OUT_RING(cmd);
  831. OUT_RING(0); /* noop */
  832. ADVANCE_LP_RING();
  833. return flush_domains;
  834. }
  835. /**
  836. * Moves buffers associated only with the given active seqno from the active
  837. * to inactive list, potentially freeing them.
  838. */
  839. static void
  840. i915_gem_retire_request(struct drm_device *dev,
  841. struct drm_i915_gem_request *request)
  842. {
  843. drm_i915_private_t *dev_priv = dev->dev_private;
  844. /* Move any buffers on the active list that are no longer referenced
  845. * by the ringbuffer to the flushing/inactive lists as appropriate.
  846. */
  847. while (!list_empty(&dev_priv->mm.active_list)) {
  848. struct drm_gem_object *obj;
  849. struct drm_i915_gem_object *obj_priv;
  850. obj_priv = list_first_entry(&dev_priv->mm.active_list,
  851. struct drm_i915_gem_object,
  852. list);
  853. obj = obj_priv->obj;
  854. /* If the seqno being retired doesn't match the oldest in the
  855. * list, then the oldest in the list must still be newer than
  856. * this seqno.
  857. */
  858. if (obj_priv->last_rendering_seqno != request->seqno)
  859. return;
  860. #if WATCH_LRU
  861. DRM_INFO("%s: retire %d moves to inactive list %p\n",
  862. __func__, request->seqno, obj);
  863. #endif
  864. if (obj->write_domain != 0)
  865. i915_gem_object_move_to_flushing(obj);
  866. else
  867. i915_gem_object_move_to_inactive(obj);
  868. }
  869. }
  870. /**
  871. * Returns true if seq1 is later than seq2.
  872. */
  873. static int
  874. i915_seqno_passed(uint32_t seq1, uint32_t seq2)
  875. {
  876. return (int32_t)(seq1 - seq2) >= 0;
  877. }
  878. uint32_t
  879. i915_get_gem_seqno(struct drm_device *dev)
  880. {
  881. drm_i915_private_t *dev_priv = dev->dev_private;
  882. return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
  883. }
  884. /**
  885. * This function clears the request list as sequence numbers are passed.
  886. */
  887. void
  888. i915_gem_retire_requests(struct drm_device *dev)
  889. {
  890. drm_i915_private_t *dev_priv = dev->dev_private;
  891. uint32_t seqno;
  892. if (!dev_priv->hw_status_page)
  893. return;
  894. seqno = i915_get_gem_seqno(dev);
  895. while (!list_empty(&dev_priv->mm.request_list)) {
  896. struct drm_i915_gem_request *request;
  897. uint32_t retiring_seqno;
  898. request = list_first_entry(&dev_priv->mm.request_list,
  899. struct drm_i915_gem_request,
  900. list);
  901. retiring_seqno = request->seqno;
  902. if (i915_seqno_passed(seqno, retiring_seqno) ||
  903. dev_priv->mm.wedged) {
  904. i915_gem_retire_request(dev, request);
  905. list_del(&request->list);
  906. drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
  907. } else
  908. break;
  909. }
  910. }
  911. void
  912. i915_gem_retire_work_handler(struct work_struct *work)
  913. {
  914. drm_i915_private_t *dev_priv;
  915. struct drm_device *dev;
  916. dev_priv = container_of(work, drm_i915_private_t,
  917. mm.retire_work.work);
  918. dev = dev_priv->dev;
  919. mutex_lock(&dev->struct_mutex);
  920. i915_gem_retire_requests(dev);
  921. if (!dev_priv->mm.suspended &&
  922. !list_empty(&dev_priv->mm.request_list))
  923. schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
  924. mutex_unlock(&dev->struct_mutex);
  925. }
  926. /**
  927. * Waits for a sequence number to be signaled, and cleans up the
  928. * request and object lists appropriately for that event.
  929. */
  930. static int
  931. i915_wait_request(struct drm_device *dev, uint32_t seqno)
  932. {
  933. drm_i915_private_t *dev_priv = dev->dev_private;
  934. int ret = 0;
  935. BUG_ON(seqno == 0);
  936. if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
  937. dev_priv->mm.waiting_gem_seqno = seqno;
  938. i915_user_irq_get(dev);
  939. ret = wait_event_interruptible(dev_priv->irq_queue,
  940. i915_seqno_passed(i915_get_gem_seqno(dev),
  941. seqno) ||
  942. dev_priv->mm.wedged);
  943. i915_user_irq_put(dev);
  944. dev_priv->mm.waiting_gem_seqno = 0;
  945. }
  946. if (dev_priv->mm.wedged)
  947. ret = -EIO;
  948. if (ret && ret != -ERESTARTSYS)
  949. DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
  950. __func__, ret, seqno, i915_get_gem_seqno(dev));
  951. /* Directly dispatch request retiring. While we have the work queue
  952. * to handle this, the waiter on a request often wants an associated
  953. * buffer to have made it to the inactive list, and we would need
  954. * a separate wait queue to handle that.
  955. */
  956. if (ret == 0)
  957. i915_gem_retire_requests(dev);
  958. return ret;
  959. }
  960. static void
  961. i915_gem_flush(struct drm_device *dev,
  962. uint32_t invalidate_domains,
  963. uint32_t flush_domains)
  964. {
  965. drm_i915_private_t *dev_priv = dev->dev_private;
  966. uint32_t cmd;
  967. RING_LOCALS;
  968. #if WATCH_EXEC
  969. DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
  970. invalidate_domains, flush_domains);
  971. #endif
  972. if (flush_domains & I915_GEM_DOMAIN_CPU)
  973. drm_agp_chipset_flush(dev);
  974. if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
  975. I915_GEM_DOMAIN_GTT)) {
  976. /*
  977. * read/write caches:
  978. *
  979. * I915_GEM_DOMAIN_RENDER is always invalidated, but is
  980. * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
  981. * also flushed at 2d versus 3d pipeline switches.
  982. *
  983. * read-only caches:
  984. *
  985. * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
  986. * MI_READ_FLUSH is set, and is always flushed on 965.
  987. *
  988. * I915_GEM_DOMAIN_COMMAND may not exist?
  989. *
  990. * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
  991. * invalidated when MI_EXE_FLUSH is set.
  992. *
  993. * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
  994. * invalidated with every MI_FLUSH.
  995. *
  996. * TLBs:
  997. *
  998. * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
  999. * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
  1000. * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
  1001. * are flushed at any MI_FLUSH.
  1002. */
  1003. cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  1004. if ((invalidate_domains|flush_domains) &
  1005. I915_GEM_DOMAIN_RENDER)
  1006. cmd &= ~MI_NO_WRITE_FLUSH;
  1007. if (!IS_I965G(dev)) {
  1008. /*
  1009. * On the 965, the sampler cache always gets flushed
  1010. * and this bit is reserved.
  1011. */
  1012. if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
  1013. cmd |= MI_READ_FLUSH;
  1014. }
  1015. if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
  1016. cmd |= MI_EXE_FLUSH;
  1017. #if WATCH_EXEC
  1018. DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
  1019. #endif
  1020. BEGIN_LP_RING(2);
  1021. OUT_RING(cmd);
  1022. OUT_RING(0); /* noop */
  1023. ADVANCE_LP_RING();
  1024. }
  1025. }
  1026. /**
  1027. * Ensures that all rendering to the object has completed and the object is
  1028. * safe to unbind from the GTT or access from the CPU.
  1029. */
  1030. static int
  1031. i915_gem_object_wait_rendering(struct drm_gem_object *obj)
  1032. {
  1033. struct drm_device *dev = obj->dev;
  1034. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1035. int ret;
  1036. /* This function only exists to support waiting for existing rendering,
  1037. * not for emitting required flushes.
  1038. */
  1039. BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
  1040. /* If there is rendering queued on the buffer being evicted, wait for
  1041. * it.
  1042. */
  1043. if (obj_priv->active) {
  1044. #if WATCH_BUF
  1045. DRM_INFO("%s: object %p wait for seqno %08x\n",
  1046. __func__, obj, obj_priv->last_rendering_seqno);
  1047. #endif
  1048. ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
  1049. if (ret != 0)
  1050. return ret;
  1051. }
  1052. return 0;
  1053. }
  1054. /**
  1055. * Unbinds an object from the GTT aperture.
  1056. */
  1057. int
  1058. i915_gem_object_unbind(struct drm_gem_object *obj)
  1059. {
  1060. struct drm_device *dev = obj->dev;
  1061. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1062. loff_t offset;
  1063. int ret = 0;
  1064. #if WATCH_BUF
  1065. DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
  1066. DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
  1067. #endif
  1068. if (obj_priv->gtt_space == NULL)
  1069. return 0;
  1070. if (obj_priv->pin_count != 0) {
  1071. DRM_ERROR("Attempting to unbind pinned buffer\n");
  1072. return -EINVAL;
  1073. }
  1074. /* Move the object to the CPU domain to ensure that
  1075. * any possible CPU writes while it's not in the GTT
  1076. * are flushed when we go to remap it. This will
  1077. * also ensure that all pending GPU writes are finished
  1078. * before we unbind.
  1079. */
  1080. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  1081. if (ret) {
  1082. if (ret != -ERESTARTSYS)
  1083. DRM_ERROR("set_domain failed: %d\n", ret);
  1084. return ret;
  1085. }
  1086. if (obj_priv->agp_mem != NULL) {
  1087. drm_unbind_agp(obj_priv->agp_mem);
  1088. drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
  1089. obj_priv->agp_mem = NULL;
  1090. }
  1091. BUG_ON(obj_priv->active);
  1092. /* blow away mappings if mapped through GTT */
  1093. offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT;
  1094. if (dev->dev_mapping)
  1095. unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1);
  1096. if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
  1097. i915_gem_clear_fence_reg(obj);
  1098. i915_gem_object_free_page_list(obj);
  1099. if (obj_priv->gtt_space) {
  1100. atomic_dec(&dev->gtt_count);
  1101. atomic_sub(obj->size, &dev->gtt_memory);
  1102. drm_mm_put_block(obj_priv->gtt_space);
  1103. obj_priv->gtt_space = NULL;
  1104. }
  1105. /* Remove ourselves from the LRU list if present. */
  1106. if (!list_empty(&obj_priv->list))
  1107. list_del_init(&obj_priv->list);
  1108. return 0;
  1109. }
  1110. static int
  1111. i915_gem_evict_something(struct drm_device *dev)
  1112. {
  1113. drm_i915_private_t *dev_priv = dev->dev_private;
  1114. struct drm_gem_object *obj;
  1115. struct drm_i915_gem_object *obj_priv;
  1116. int ret = 0;
  1117. for (;;) {
  1118. /* If there's an inactive buffer available now, grab it
  1119. * and be done.
  1120. */
  1121. if (!list_empty(&dev_priv->mm.inactive_list)) {
  1122. obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
  1123. struct drm_i915_gem_object,
  1124. list);
  1125. obj = obj_priv->obj;
  1126. BUG_ON(obj_priv->pin_count != 0);
  1127. #if WATCH_LRU
  1128. DRM_INFO("%s: evicting %p\n", __func__, obj);
  1129. #endif
  1130. BUG_ON(obj_priv->active);
  1131. /* Wait on the rendering and unbind the buffer. */
  1132. ret = i915_gem_object_unbind(obj);
  1133. break;
  1134. }
  1135. /* If we didn't get anything, but the ring is still processing
  1136. * things, wait for one of those things to finish and hopefully
  1137. * leave us a buffer to evict.
  1138. */
  1139. if (!list_empty(&dev_priv->mm.request_list)) {
  1140. struct drm_i915_gem_request *request;
  1141. request = list_first_entry(&dev_priv->mm.request_list,
  1142. struct drm_i915_gem_request,
  1143. list);
  1144. ret = i915_wait_request(dev, request->seqno);
  1145. if (ret)
  1146. break;
  1147. /* if waiting caused an object to become inactive,
  1148. * then loop around and wait for it. Otherwise, we
  1149. * assume that waiting freed and unbound something,
  1150. * so there should now be some space in the GTT
  1151. */
  1152. if (!list_empty(&dev_priv->mm.inactive_list))
  1153. continue;
  1154. break;
  1155. }
  1156. /* If we didn't have anything on the request list but there
  1157. * are buffers awaiting a flush, emit one and try again.
  1158. * When we wait on it, those buffers waiting for that flush
  1159. * will get moved to inactive.
  1160. */
  1161. if (!list_empty(&dev_priv->mm.flushing_list)) {
  1162. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  1163. struct drm_i915_gem_object,
  1164. list);
  1165. obj = obj_priv->obj;
  1166. i915_gem_flush(dev,
  1167. obj->write_domain,
  1168. obj->write_domain);
  1169. i915_add_request(dev, obj->write_domain);
  1170. obj = NULL;
  1171. continue;
  1172. }
  1173. DRM_ERROR("inactive empty %d request empty %d "
  1174. "flushing empty %d\n",
  1175. list_empty(&dev_priv->mm.inactive_list),
  1176. list_empty(&dev_priv->mm.request_list),
  1177. list_empty(&dev_priv->mm.flushing_list));
  1178. /* If we didn't do any of the above, there's nothing to be done
  1179. * and we just can't fit it in.
  1180. */
  1181. return -ENOMEM;
  1182. }
  1183. return ret;
  1184. }
  1185. static int
  1186. i915_gem_evict_everything(struct drm_device *dev)
  1187. {
  1188. int ret;
  1189. for (;;) {
  1190. ret = i915_gem_evict_something(dev);
  1191. if (ret != 0)
  1192. break;
  1193. }
  1194. if (ret == -ENOMEM)
  1195. return 0;
  1196. return ret;
  1197. }
  1198. static int
  1199. i915_gem_object_get_page_list(struct drm_gem_object *obj)
  1200. {
  1201. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1202. int page_count, i;
  1203. struct address_space *mapping;
  1204. struct inode *inode;
  1205. struct page *page;
  1206. int ret;
  1207. if (obj_priv->page_list)
  1208. return 0;
  1209. /* Get the list of pages out of our struct file. They'll be pinned
  1210. * at this point until we release them.
  1211. */
  1212. page_count = obj->size / PAGE_SIZE;
  1213. BUG_ON(obj_priv->page_list != NULL);
  1214. obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
  1215. DRM_MEM_DRIVER);
  1216. if (obj_priv->page_list == NULL) {
  1217. DRM_ERROR("Faled to allocate page list\n");
  1218. return -ENOMEM;
  1219. }
  1220. inode = obj->filp->f_path.dentry->d_inode;
  1221. mapping = inode->i_mapping;
  1222. for (i = 0; i < page_count; i++) {
  1223. page = read_mapping_page(mapping, i, NULL);
  1224. if (IS_ERR(page)) {
  1225. ret = PTR_ERR(page);
  1226. DRM_ERROR("read_mapping_page failed: %d\n", ret);
  1227. i915_gem_object_free_page_list(obj);
  1228. return ret;
  1229. }
  1230. obj_priv->page_list[i] = page;
  1231. }
  1232. return 0;
  1233. }
  1234. static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
  1235. {
  1236. struct drm_gem_object *obj = reg->obj;
  1237. struct drm_device *dev = obj->dev;
  1238. drm_i915_private_t *dev_priv = dev->dev_private;
  1239. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1240. int regnum = obj_priv->fence_reg;
  1241. uint64_t val;
  1242. val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
  1243. 0xfffff000) << 32;
  1244. val |= obj_priv->gtt_offset & 0xfffff000;
  1245. val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  1246. if (obj_priv->tiling_mode == I915_TILING_Y)
  1247. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1248. val |= I965_FENCE_REG_VALID;
  1249. I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
  1250. }
  1251. static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
  1252. {
  1253. struct drm_gem_object *obj = reg->obj;
  1254. struct drm_device *dev = obj->dev;
  1255. drm_i915_private_t *dev_priv = dev->dev_private;
  1256. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1257. int regnum = obj_priv->fence_reg;
  1258. int tile_width;
  1259. uint32_t val;
  1260. uint32_t pitch_val;
  1261. if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
  1262. (obj_priv->gtt_offset & (obj->size - 1))) {
  1263. WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
  1264. __func__, obj_priv->gtt_offset, obj->size);
  1265. return;
  1266. }
  1267. if (obj_priv->tiling_mode == I915_TILING_Y &&
  1268. HAS_128_BYTE_Y_TILING(dev))
  1269. tile_width = 128;
  1270. else
  1271. tile_width = 512;
  1272. /* Note: pitch better be a power of two tile widths */
  1273. pitch_val = obj_priv->stride / tile_width;
  1274. pitch_val = ffs(pitch_val) - 1;
  1275. val = obj_priv->gtt_offset;
  1276. if (obj_priv->tiling_mode == I915_TILING_Y)
  1277. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1278. val |= I915_FENCE_SIZE_BITS(obj->size);
  1279. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1280. val |= I830_FENCE_REG_VALID;
  1281. I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
  1282. }
  1283. static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
  1284. {
  1285. struct drm_gem_object *obj = reg->obj;
  1286. struct drm_device *dev = obj->dev;
  1287. drm_i915_private_t *dev_priv = dev->dev_private;
  1288. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1289. int regnum = obj_priv->fence_reg;
  1290. uint32_t val;
  1291. uint32_t pitch_val;
  1292. if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
  1293. (obj_priv->gtt_offset & (obj->size - 1))) {
  1294. WARN(1, "%s: object 0x%08x not 1M or size aligned\n",
  1295. __func__, obj_priv->gtt_offset);
  1296. return;
  1297. }
  1298. pitch_val = (obj_priv->stride / 128) - 1;
  1299. val = obj_priv->gtt_offset;
  1300. if (obj_priv->tiling_mode == I915_TILING_Y)
  1301. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1302. val |= I830_FENCE_SIZE_BITS(obj->size);
  1303. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1304. val |= I830_FENCE_REG_VALID;
  1305. I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
  1306. }
  1307. /**
  1308. * i915_gem_object_get_fence_reg - set up a fence reg for an object
  1309. * @obj: object to map through a fence reg
  1310. * @write: object is about to be written
  1311. *
  1312. * When mapping objects through the GTT, userspace wants to be able to write
  1313. * to them without having to worry about swizzling if the object is tiled.
  1314. *
  1315. * This function walks the fence regs looking for a free one for @obj,
  1316. * stealing one if it can't find any.
  1317. *
  1318. * It then sets up the reg based on the object's properties: address, pitch
  1319. * and tiling format.
  1320. */
  1321. static int
  1322. i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write)
  1323. {
  1324. struct drm_device *dev = obj->dev;
  1325. struct drm_i915_private *dev_priv = dev->dev_private;
  1326. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1327. struct drm_i915_fence_reg *reg = NULL;
  1328. struct drm_i915_gem_object *old_obj_priv = NULL;
  1329. int i, ret, avail;
  1330. switch (obj_priv->tiling_mode) {
  1331. case I915_TILING_NONE:
  1332. WARN(1, "allocating a fence for non-tiled object?\n");
  1333. break;
  1334. case I915_TILING_X:
  1335. if (!obj_priv->stride)
  1336. return -EINVAL;
  1337. WARN((obj_priv->stride & (512 - 1)),
  1338. "object 0x%08x is X tiled but has non-512B pitch\n",
  1339. obj_priv->gtt_offset);
  1340. break;
  1341. case I915_TILING_Y:
  1342. if (!obj_priv->stride)
  1343. return -EINVAL;
  1344. WARN((obj_priv->stride & (128 - 1)),
  1345. "object 0x%08x is Y tiled but has non-128B pitch\n",
  1346. obj_priv->gtt_offset);
  1347. break;
  1348. }
  1349. /* First try to find a free reg */
  1350. try_again:
  1351. avail = 0;
  1352. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  1353. reg = &dev_priv->fence_regs[i];
  1354. if (!reg->obj)
  1355. break;
  1356. old_obj_priv = reg->obj->driver_private;
  1357. if (!old_obj_priv->pin_count)
  1358. avail++;
  1359. }
  1360. /* None available, try to steal one or wait for a user to finish */
  1361. if (i == dev_priv->num_fence_regs) {
  1362. loff_t offset;
  1363. if (avail == 0)
  1364. return -ENOMEM;
  1365. /* Could try to use LRU here instead... */
  1366. for (i = dev_priv->fence_reg_start;
  1367. i < dev_priv->num_fence_regs; i++) {
  1368. reg = &dev_priv->fence_regs[i];
  1369. old_obj_priv = reg->obj->driver_private;
  1370. if (!old_obj_priv->pin_count)
  1371. break;
  1372. }
  1373. /*
  1374. * Now things get ugly... we have to wait for one of the
  1375. * objects to finish before trying again.
  1376. */
  1377. if (i == dev_priv->num_fence_regs) {
  1378. ret = i915_gem_object_set_to_gtt_domain(reg->obj, 0);
  1379. if (ret) {
  1380. WARN(ret != -ERESTARTSYS,
  1381. "switch to GTT domain failed: %d\n", ret);
  1382. return ret;
  1383. }
  1384. goto try_again;
  1385. }
  1386. /*
  1387. * Zap this virtual mapping so we can set up a fence again
  1388. * for this object next time we need it.
  1389. */
  1390. offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
  1391. if (dev->dev_mapping)
  1392. unmap_mapping_range(dev->dev_mapping, offset,
  1393. reg->obj->size, 1);
  1394. old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
  1395. }
  1396. obj_priv->fence_reg = i;
  1397. reg->obj = obj;
  1398. if (IS_I965G(dev))
  1399. i965_write_fence_reg(reg);
  1400. else if (IS_I9XX(dev))
  1401. i915_write_fence_reg(reg);
  1402. else
  1403. i830_write_fence_reg(reg);
  1404. return 0;
  1405. }
  1406. /**
  1407. * i915_gem_clear_fence_reg - clear out fence register info
  1408. * @obj: object to clear
  1409. *
  1410. * Zeroes out the fence register itself and clears out the associated
  1411. * data structures in dev_priv and obj_priv.
  1412. */
  1413. static void
  1414. i915_gem_clear_fence_reg(struct drm_gem_object *obj)
  1415. {
  1416. struct drm_device *dev = obj->dev;
  1417. drm_i915_private_t *dev_priv = dev->dev_private;
  1418. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1419. if (IS_I965G(dev))
  1420. I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
  1421. else
  1422. I915_WRITE(FENCE_REG_830_0 + (obj_priv->fence_reg * 4), 0);
  1423. dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
  1424. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  1425. }
  1426. /**
  1427. * Finds free space in the GTT aperture and binds the object there.
  1428. */
  1429. static int
  1430. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  1431. {
  1432. struct drm_device *dev = obj->dev;
  1433. drm_i915_private_t *dev_priv = dev->dev_private;
  1434. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1435. struct drm_mm_node *free_space;
  1436. int page_count, ret;
  1437. if (dev_priv->mm.suspended)
  1438. return -EBUSY;
  1439. if (alignment == 0)
  1440. alignment = i915_gem_get_gtt_alignment(obj);
  1441. if (alignment & (PAGE_SIZE - 1)) {
  1442. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  1443. return -EINVAL;
  1444. }
  1445. search_free:
  1446. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  1447. obj->size, alignment, 0);
  1448. if (free_space != NULL) {
  1449. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  1450. alignment);
  1451. if (obj_priv->gtt_space != NULL) {
  1452. obj_priv->gtt_space->private = obj;
  1453. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  1454. }
  1455. }
  1456. if (obj_priv->gtt_space == NULL) {
  1457. /* If the gtt is empty and we're still having trouble
  1458. * fitting our object in, we're out of memory.
  1459. */
  1460. #if WATCH_LRU
  1461. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  1462. #endif
  1463. if (list_empty(&dev_priv->mm.inactive_list) &&
  1464. list_empty(&dev_priv->mm.flushing_list) &&
  1465. list_empty(&dev_priv->mm.active_list)) {
  1466. DRM_ERROR("GTT full, but LRU list empty\n");
  1467. return -ENOMEM;
  1468. }
  1469. ret = i915_gem_evict_something(dev);
  1470. if (ret != 0) {
  1471. if (ret != -ERESTARTSYS)
  1472. DRM_ERROR("Failed to evict a buffer %d\n", ret);
  1473. return ret;
  1474. }
  1475. goto search_free;
  1476. }
  1477. #if WATCH_BUF
  1478. DRM_INFO("Binding object of size %d at 0x%08x\n",
  1479. obj->size, obj_priv->gtt_offset);
  1480. #endif
  1481. ret = i915_gem_object_get_page_list(obj);
  1482. if (ret) {
  1483. drm_mm_put_block(obj_priv->gtt_space);
  1484. obj_priv->gtt_space = NULL;
  1485. return ret;
  1486. }
  1487. page_count = obj->size / PAGE_SIZE;
  1488. /* Create an AGP memory structure pointing at our pages, and bind it
  1489. * into the GTT.
  1490. */
  1491. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  1492. obj_priv->page_list,
  1493. page_count,
  1494. obj_priv->gtt_offset,
  1495. obj_priv->agp_type);
  1496. if (obj_priv->agp_mem == NULL) {
  1497. i915_gem_object_free_page_list(obj);
  1498. drm_mm_put_block(obj_priv->gtt_space);
  1499. obj_priv->gtt_space = NULL;
  1500. return -ENOMEM;
  1501. }
  1502. atomic_inc(&dev->gtt_count);
  1503. atomic_add(obj->size, &dev->gtt_memory);
  1504. /* Assert that the object is not currently in any GPU domain. As it
  1505. * wasn't in the GTT, there shouldn't be any way it could have been in
  1506. * a GPU cache
  1507. */
  1508. BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1509. BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1510. return 0;
  1511. }
  1512. void
  1513. i915_gem_clflush_object(struct drm_gem_object *obj)
  1514. {
  1515. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1516. /* If we don't have a page list set up, then we're not pinned
  1517. * to GPU, and we can ignore the cache flush because it'll happen
  1518. * again at bind time.
  1519. */
  1520. if (obj_priv->page_list == NULL)
  1521. return;
  1522. drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
  1523. }
  1524. /** Flushes any GPU write domain for the object if it's dirty. */
  1525. static void
  1526. i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
  1527. {
  1528. struct drm_device *dev = obj->dev;
  1529. uint32_t seqno;
  1530. if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  1531. return;
  1532. /* Queue the GPU write cache flushing we need. */
  1533. i915_gem_flush(dev, 0, obj->write_domain);
  1534. seqno = i915_add_request(dev, obj->write_domain);
  1535. obj->write_domain = 0;
  1536. i915_gem_object_move_to_active(obj, seqno);
  1537. }
  1538. /** Flushes the GTT write domain for the object if it's dirty. */
  1539. static void
  1540. i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
  1541. {
  1542. if (obj->write_domain != I915_GEM_DOMAIN_GTT)
  1543. return;
  1544. /* No actual flushing is required for the GTT write domain. Writes
  1545. * to it immediately go to main memory as far as we know, so there's
  1546. * no chipset flush. It also doesn't land in render cache.
  1547. */
  1548. obj->write_domain = 0;
  1549. }
  1550. /** Flushes the CPU write domain for the object if it's dirty. */
  1551. static void
  1552. i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
  1553. {
  1554. struct drm_device *dev = obj->dev;
  1555. if (obj->write_domain != I915_GEM_DOMAIN_CPU)
  1556. return;
  1557. i915_gem_clflush_object(obj);
  1558. drm_agp_chipset_flush(dev);
  1559. obj->write_domain = 0;
  1560. }
  1561. /**
  1562. * Moves a single object to the GTT read, and possibly write domain.
  1563. *
  1564. * This function returns when the move is complete, including waiting on
  1565. * flushes to occur.
  1566. */
  1567. int
  1568. i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
  1569. {
  1570. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1571. int ret;
  1572. /* Not valid to be called on unbound objects. */
  1573. if (obj_priv->gtt_space == NULL)
  1574. return -EINVAL;
  1575. i915_gem_object_flush_gpu_write_domain(obj);
  1576. /* Wait on any GPU rendering and flushing to occur. */
  1577. ret = i915_gem_object_wait_rendering(obj);
  1578. if (ret != 0)
  1579. return ret;
  1580. /* If we're writing through the GTT domain, then CPU and GPU caches
  1581. * will need to be invalidated at next use.
  1582. */
  1583. if (write)
  1584. obj->read_domains &= I915_GEM_DOMAIN_GTT;
  1585. i915_gem_object_flush_cpu_write_domain(obj);
  1586. /* It should now be out of any other write domains, and we can update
  1587. * the domain values for our changes.
  1588. */
  1589. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  1590. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  1591. if (write) {
  1592. obj->write_domain = I915_GEM_DOMAIN_GTT;
  1593. obj_priv->dirty = 1;
  1594. }
  1595. return 0;
  1596. }
  1597. /**
  1598. * Moves a single object to the CPU read, and possibly write domain.
  1599. *
  1600. * This function returns when the move is complete, including waiting on
  1601. * flushes to occur.
  1602. */
  1603. static int
  1604. i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
  1605. {
  1606. struct drm_device *dev = obj->dev;
  1607. int ret;
  1608. i915_gem_object_flush_gpu_write_domain(obj);
  1609. /* Wait on any GPU rendering and flushing to occur. */
  1610. ret = i915_gem_object_wait_rendering(obj);
  1611. if (ret != 0)
  1612. return ret;
  1613. i915_gem_object_flush_gtt_write_domain(obj);
  1614. /* If we have a partially-valid cache of the object in the CPU,
  1615. * finish invalidating it and free the per-page flags.
  1616. */
  1617. i915_gem_object_set_to_full_cpu_read_domain(obj);
  1618. /* Flush the CPU cache if it's still invalid. */
  1619. if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  1620. i915_gem_clflush_object(obj);
  1621. drm_agp_chipset_flush(dev);
  1622. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  1623. }
  1624. /* It should now be out of any other write domains, and we can update
  1625. * the domain values for our changes.
  1626. */
  1627. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  1628. /* If we're writing through the CPU, then the GPU read domains will
  1629. * need to be invalidated at next use.
  1630. */
  1631. if (write) {
  1632. obj->read_domains &= I915_GEM_DOMAIN_CPU;
  1633. obj->write_domain = I915_GEM_DOMAIN_CPU;
  1634. }
  1635. return 0;
  1636. }
  1637. /*
  1638. * Set the next domain for the specified object. This
  1639. * may not actually perform the necessary flushing/invaliding though,
  1640. * as that may want to be batched with other set_domain operations
  1641. *
  1642. * This is (we hope) the only really tricky part of gem. The goal
  1643. * is fairly simple -- track which caches hold bits of the object
  1644. * and make sure they remain coherent. A few concrete examples may
  1645. * help to explain how it works. For shorthand, we use the notation
  1646. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  1647. * a pair of read and write domain masks.
  1648. *
  1649. * Case 1: the batch buffer
  1650. *
  1651. * 1. Allocated
  1652. * 2. Written by CPU
  1653. * 3. Mapped to GTT
  1654. * 4. Read by GPU
  1655. * 5. Unmapped from GTT
  1656. * 6. Freed
  1657. *
  1658. * Let's take these a step at a time
  1659. *
  1660. * 1. Allocated
  1661. * Pages allocated from the kernel may still have
  1662. * cache contents, so we set them to (CPU, CPU) always.
  1663. * 2. Written by CPU (using pwrite)
  1664. * The pwrite function calls set_domain (CPU, CPU) and
  1665. * this function does nothing (as nothing changes)
  1666. * 3. Mapped by GTT
  1667. * This function asserts that the object is not
  1668. * currently in any GPU-based read or write domains
  1669. * 4. Read by GPU
  1670. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  1671. * As write_domain is zero, this function adds in the
  1672. * current read domains (CPU+COMMAND, 0).
  1673. * flush_domains is set to CPU.
  1674. * invalidate_domains is set to COMMAND
  1675. * clflush is run to get data out of the CPU caches
  1676. * then i915_dev_set_domain calls i915_gem_flush to
  1677. * emit an MI_FLUSH and drm_agp_chipset_flush
  1678. * 5. Unmapped from GTT
  1679. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  1680. * flush_domains and invalidate_domains end up both zero
  1681. * so no flushing/invalidating happens
  1682. * 6. Freed
  1683. * yay, done
  1684. *
  1685. * Case 2: The shared render buffer
  1686. *
  1687. * 1. Allocated
  1688. * 2. Mapped to GTT
  1689. * 3. Read/written by GPU
  1690. * 4. set_domain to (CPU,CPU)
  1691. * 5. Read/written by CPU
  1692. * 6. Read/written by GPU
  1693. *
  1694. * 1. Allocated
  1695. * Same as last example, (CPU, CPU)
  1696. * 2. Mapped to GTT
  1697. * Nothing changes (assertions find that it is not in the GPU)
  1698. * 3. Read/written by GPU
  1699. * execbuffer calls set_domain (RENDER, RENDER)
  1700. * flush_domains gets CPU
  1701. * invalidate_domains gets GPU
  1702. * clflush (obj)
  1703. * MI_FLUSH and drm_agp_chipset_flush
  1704. * 4. set_domain (CPU, CPU)
  1705. * flush_domains gets GPU
  1706. * invalidate_domains gets CPU
  1707. * wait_rendering (obj) to make sure all drawing is complete.
  1708. * This will include an MI_FLUSH to get the data from GPU
  1709. * to memory
  1710. * clflush (obj) to invalidate the CPU cache
  1711. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  1712. * 5. Read/written by CPU
  1713. * cache lines are loaded and dirtied
  1714. * 6. Read written by GPU
  1715. * Same as last GPU access
  1716. *
  1717. * Case 3: The constant buffer
  1718. *
  1719. * 1. Allocated
  1720. * 2. Written by CPU
  1721. * 3. Read by GPU
  1722. * 4. Updated (written) by CPU again
  1723. * 5. Read by GPU
  1724. *
  1725. * 1. Allocated
  1726. * (CPU, CPU)
  1727. * 2. Written by CPU
  1728. * (CPU, CPU)
  1729. * 3. Read by GPU
  1730. * (CPU+RENDER, 0)
  1731. * flush_domains = CPU
  1732. * invalidate_domains = RENDER
  1733. * clflush (obj)
  1734. * MI_FLUSH
  1735. * drm_agp_chipset_flush
  1736. * 4. Updated (written) by CPU again
  1737. * (CPU, CPU)
  1738. * flush_domains = 0 (no previous write domain)
  1739. * invalidate_domains = 0 (no new read domains)
  1740. * 5. Read by GPU
  1741. * (CPU+RENDER, 0)
  1742. * flush_domains = CPU
  1743. * invalidate_domains = RENDER
  1744. * clflush (obj)
  1745. * MI_FLUSH
  1746. * drm_agp_chipset_flush
  1747. */
  1748. static void
  1749. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
  1750. {
  1751. struct drm_device *dev = obj->dev;
  1752. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1753. uint32_t invalidate_domains = 0;
  1754. uint32_t flush_domains = 0;
  1755. BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
  1756. BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
  1757. #if WATCH_BUF
  1758. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  1759. __func__, obj,
  1760. obj->read_domains, obj->pending_read_domains,
  1761. obj->write_domain, obj->pending_write_domain);
  1762. #endif
  1763. /*
  1764. * If the object isn't moving to a new write domain,
  1765. * let the object stay in multiple read domains
  1766. */
  1767. if (obj->pending_write_domain == 0)
  1768. obj->pending_read_domains |= obj->read_domains;
  1769. else
  1770. obj_priv->dirty = 1;
  1771. /*
  1772. * Flush the current write domain if
  1773. * the new read domains don't match. Invalidate
  1774. * any read domains which differ from the old
  1775. * write domain
  1776. */
  1777. if (obj->write_domain &&
  1778. obj->write_domain != obj->pending_read_domains) {
  1779. flush_domains |= obj->write_domain;
  1780. invalidate_domains |=
  1781. obj->pending_read_domains & ~obj->write_domain;
  1782. }
  1783. /*
  1784. * Invalidate any read caches which may have
  1785. * stale data. That is, any new read domains.
  1786. */
  1787. invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
  1788. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  1789. #if WATCH_BUF
  1790. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  1791. __func__, flush_domains, invalidate_domains);
  1792. #endif
  1793. i915_gem_clflush_object(obj);
  1794. }
  1795. /* The actual obj->write_domain will be updated with
  1796. * pending_write_domain after we emit the accumulated flush for all
  1797. * of our domain changes in execbuffers (which clears objects'
  1798. * write_domains). So if we have a current write domain that we
  1799. * aren't changing, set pending_write_domain to that.
  1800. */
  1801. if (flush_domains == 0 && obj->pending_write_domain == 0)
  1802. obj->pending_write_domain = obj->write_domain;
  1803. obj->read_domains = obj->pending_read_domains;
  1804. dev->invalidate_domains |= invalidate_domains;
  1805. dev->flush_domains |= flush_domains;
  1806. #if WATCH_BUF
  1807. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  1808. __func__,
  1809. obj->read_domains, obj->write_domain,
  1810. dev->invalidate_domains, dev->flush_domains);
  1811. #endif
  1812. }
  1813. /**
  1814. * Moves the object from a partially CPU read to a full one.
  1815. *
  1816. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  1817. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  1818. */
  1819. static void
  1820. i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
  1821. {
  1822. struct drm_device *dev = obj->dev;
  1823. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1824. if (!obj_priv->page_cpu_valid)
  1825. return;
  1826. /* If we're partially in the CPU read domain, finish moving it in.
  1827. */
  1828. if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
  1829. int i;
  1830. for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
  1831. if (obj_priv->page_cpu_valid[i])
  1832. continue;
  1833. drm_clflush_pages(obj_priv->page_list + i, 1);
  1834. }
  1835. drm_agp_chipset_flush(dev);
  1836. }
  1837. /* Free the page_cpu_valid mappings which are now stale, whether
  1838. * or not we've got I915_GEM_DOMAIN_CPU.
  1839. */
  1840. drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
  1841. DRM_MEM_DRIVER);
  1842. obj_priv->page_cpu_valid = NULL;
  1843. }
  1844. /**
  1845. * Set the CPU read domain on a range of the object.
  1846. *
  1847. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  1848. * not entirely valid. The page_cpu_valid member of the object flags which
  1849. * pages have been flushed, and will be respected by
  1850. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  1851. * of the whole object.
  1852. *
  1853. * This function returns when the move is complete, including waiting on
  1854. * flushes to occur.
  1855. */
  1856. static int
  1857. i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  1858. uint64_t offset, uint64_t size)
  1859. {
  1860. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1861. int i, ret;
  1862. if (offset == 0 && size == obj->size)
  1863. return i915_gem_object_set_to_cpu_domain(obj, 0);
  1864. i915_gem_object_flush_gpu_write_domain(obj);
  1865. /* Wait on any GPU rendering and flushing to occur. */
  1866. ret = i915_gem_object_wait_rendering(obj);
  1867. if (ret != 0)
  1868. return ret;
  1869. i915_gem_object_flush_gtt_write_domain(obj);
  1870. /* If we're already fully in the CPU read domain, we're done. */
  1871. if (obj_priv->page_cpu_valid == NULL &&
  1872. (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
  1873. return 0;
  1874. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  1875. * newly adding I915_GEM_DOMAIN_CPU
  1876. */
  1877. if (obj_priv->page_cpu_valid == NULL) {
  1878. obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
  1879. DRM_MEM_DRIVER);
  1880. if (obj_priv->page_cpu_valid == NULL)
  1881. return -ENOMEM;
  1882. } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
  1883. memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
  1884. /* Flush the cache on any pages that are still invalid from the CPU's
  1885. * perspective.
  1886. */
  1887. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  1888. i++) {
  1889. if (obj_priv->page_cpu_valid[i])
  1890. continue;
  1891. drm_clflush_pages(obj_priv->page_list + i, 1);
  1892. obj_priv->page_cpu_valid[i] = 1;
  1893. }
  1894. /* It should now be out of any other write domains, and we can update
  1895. * the domain values for our changes.
  1896. */
  1897. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  1898. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  1899. return 0;
  1900. }
  1901. /**
  1902. * Pin an object to the GTT and evaluate the relocations landing in it.
  1903. */
  1904. static int
  1905. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  1906. struct drm_file *file_priv,
  1907. struct drm_i915_gem_exec_object *entry)
  1908. {
  1909. struct drm_device *dev = obj->dev;
  1910. drm_i915_private_t *dev_priv = dev->dev_private;
  1911. struct drm_i915_gem_relocation_entry reloc;
  1912. struct drm_i915_gem_relocation_entry __user *relocs;
  1913. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1914. int i, ret;
  1915. void __iomem *reloc_page;
  1916. /* Choose the GTT offset for our buffer and put it there. */
  1917. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  1918. if (ret)
  1919. return ret;
  1920. entry->offset = obj_priv->gtt_offset;
  1921. relocs = (struct drm_i915_gem_relocation_entry __user *)
  1922. (uintptr_t) entry->relocs_ptr;
  1923. /* Apply the relocations, using the GTT aperture to avoid cache
  1924. * flushing requirements.
  1925. */
  1926. for (i = 0; i < entry->relocation_count; i++) {
  1927. struct drm_gem_object *target_obj;
  1928. struct drm_i915_gem_object *target_obj_priv;
  1929. uint32_t reloc_val, reloc_offset;
  1930. uint32_t __iomem *reloc_entry;
  1931. ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
  1932. if (ret != 0) {
  1933. i915_gem_object_unpin(obj);
  1934. return ret;
  1935. }
  1936. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  1937. reloc.target_handle);
  1938. if (target_obj == NULL) {
  1939. i915_gem_object_unpin(obj);
  1940. return -EBADF;
  1941. }
  1942. target_obj_priv = target_obj->driver_private;
  1943. /* The target buffer should have appeared before us in the
  1944. * exec_object list, so it should have a GTT space bound by now.
  1945. */
  1946. if (target_obj_priv->gtt_space == NULL) {
  1947. DRM_ERROR("No GTT space found for object %d\n",
  1948. reloc.target_handle);
  1949. drm_gem_object_unreference(target_obj);
  1950. i915_gem_object_unpin(obj);
  1951. return -EINVAL;
  1952. }
  1953. if (reloc.offset > obj->size - 4) {
  1954. DRM_ERROR("Relocation beyond object bounds: "
  1955. "obj %p target %d offset %d size %d.\n",
  1956. obj, reloc.target_handle,
  1957. (int) reloc.offset, (int) obj->size);
  1958. drm_gem_object_unreference(target_obj);
  1959. i915_gem_object_unpin(obj);
  1960. return -EINVAL;
  1961. }
  1962. if (reloc.offset & 3) {
  1963. DRM_ERROR("Relocation not 4-byte aligned: "
  1964. "obj %p target %d offset %d.\n",
  1965. obj, reloc.target_handle,
  1966. (int) reloc.offset);
  1967. drm_gem_object_unreference(target_obj);
  1968. i915_gem_object_unpin(obj);
  1969. return -EINVAL;
  1970. }
  1971. if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
  1972. reloc.read_domains & I915_GEM_DOMAIN_CPU) {
  1973. DRM_ERROR("reloc with read/write CPU domains: "
  1974. "obj %p target %d offset %d "
  1975. "read %08x write %08x",
  1976. obj, reloc.target_handle,
  1977. (int) reloc.offset,
  1978. reloc.read_domains,
  1979. reloc.write_domain);
  1980. drm_gem_object_unreference(target_obj);
  1981. i915_gem_object_unpin(obj);
  1982. return -EINVAL;
  1983. }
  1984. if (reloc.write_domain && target_obj->pending_write_domain &&
  1985. reloc.write_domain != target_obj->pending_write_domain) {
  1986. DRM_ERROR("Write domain conflict: "
  1987. "obj %p target %d offset %d "
  1988. "new %08x old %08x\n",
  1989. obj, reloc.target_handle,
  1990. (int) reloc.offset,
  1991. reloc.write_domain,
  1992. target_obj->pending_write_domain);
  1993. drm_gem_object_unreference(target_obj);
  1994. i915_gem_object_unpin(obj);
  1995. return -EINVAL;
  1996. }
  1997. #if WATCH_RELOC
  1998. DRM_INFO("%s: obj %p offset %08x target %d "
  1999. "read %08x write %08x gtt %08x "
  2000. "presumed %08x delta %08x\n",
  2001. __func__,
  2002. obj,
  2003. (int) reloc.offset,
  2004. (int) reloc.target_handle,
  2005. (int) reloc.read_domains,
  2006. (int) reloc.write_domain,
  2007. (int) target_obj_priv->gtt_offset,
  2008. (int) reloc.presumed_offset,
  2009. reloc.delta);
  2010. #endif
  2011. target_obj->pending_read_domains |= reloc.read_domains;
  2012. target_obj->pending_write_domain |= reloc.write_domain;
  2013. /* If the relocation already has the right value in it, no
  2014. * more work needs to be done.
  2015. */
  2016. if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
  2017. drm_gem_object_unreference(target_obj);
  2018. continue;
  2019. }
  2020. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  2021. if (ret != 0) {
  2022. drm_gem_object_unreference(target_obj);
  2023. i915_gem_object_unpin(obj);
  2024. return -EINVAL;
  2025. }
  2026. /* Map the page containing the relocation we're going to
  2027. * perform.
  2028. */
  2029. reloc_offset = obj_priv->gtt_offset + reloc.offset;
  2030. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  2031. (reloc_offset &
  2032. ~(PAGE_SIZE - 1)));
  2033. reloc_entry = (uint32_t __iomem *)(reloc_page +
  2034. (reloc_offset & (PAGE_SIZE - 1)));
  2035. reloc_val = target_obj_priv->gtt_offset + reloc.delta;
  2036. #if WATCH_BUF
  2037. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  2038. obj, (unsigned int) reloc.offset,
  2039. readl(reloc_entry), reloc_val);
  2040. #endif
  2041. writel(reloc_val, reloc_entry);
  2042. io_mapping_unmap_atomic(reloc_page);
  2043. /* Write the updated presumed offset for this entry back out
  2044. * to the user.
  2045. */
  2046. reloc.presumed_offset = target_obj_priv->gtt_offset;
  2047. ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
  2048. if (ret != 0) {
  2049. drm_gem_object_unreference(target_obj);
  2050. i915_gem_object_unpin(obj);
  2051. return ret;
  2052. }
  2053. drm_gem_object_unreference(target_obj);
  2054. }
  2055. #if WATCH_BUF
  2056. if (0)
  2057. i915_gem_dump_object(obj, 128, __func__, ~0);
  2058. #endif
  2059. return 0;
  2060. }
  2061. /** Dispatch a batchbuffer to the ring
  2062. */
  2063. static int
  2064. i915_dispatch_gem_execbuffer(struct drm_device *dev,
  2065. struct drm_i915_gem_execbuffer *exec,
  2066. uint64_t exec_offset)
  2067. {
  2068. drm_i915_private_t *dev_priv = dev->dev_private;
  2069. struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
  2070. (uintptr_t) exec->cliprects_ptr;
  2071. int nbox = exec->num_cliprects;
  2072. int i = 0, count;
  2073. uint32_t exec_start, exec_len;
  2074. RING_LOCALS;
  2075. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  2076. exec_len = (uint32_t) exec->batch_len;
  2077. if ((exec_start | exec_len) & 0x7) {
  2078. DRM_ERROR("alignment\n");
  2079. return -EINVAL;
  2080. }
  2081. if (!exec_start)
  2082. return -EINVAL;
  2083. count = nbox ? nbox : 1;
  2084. for (i = 0; i < count; i++) {
  2085. if (i < nbox) {
  2086. int ret = i915_emit_box(dev, boxes, i,
  2087. exec->DR1, exec->DR4);
  2088. if (ret)
  2089. return ret;
  2090. }
  2091. if (IS_I830(dev) || IS_845G(dev)) {
  2092. BEGIN_LP_RING(4);
  2093. OUT_RING(MI_BATCH_BUFFER);
  2094. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  2095. OUT_RING(exec_start + exec_len - 4);
  2096. OUT_RING(0);
  2097. ADVANCE_LP_RING();
  2098. } else {
  2099. BEGIN_LP_RING(2);
  2100. if (IS_I965G(dev)) {
  2101. OUT_RING(MI_BATCH_BUFFER_START |
  2102. (2 << 6) |
  2103. MI_BATCH_NON_SECURE_I965);
  2104. OUT_RING(exec_start);
  2105. } else {
  2106. OUT_RING(MI_BATCH_BUFFER_START |
  2107. (2 << 6));
  2108. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  2109. }
  2110. ADVANCE_LP_RING();
  2111. }
  2112. }
  2113. /* XXX breadcrumb */
  2114. return 0;
  2115. }
  2116. /* Throttle our rendering by waiting until the ring has completed our requests
  2117. * emitted over 20 msec ago.
  2118. *
  2119. * This should get us reasonable parallelism between CPU and GPU but also
  2120. * relatively low latency when blocking on a particular request to finish.
  2121. */
  2122. static int
  2123. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  2124. {
  2125. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  2126. int ret = 0;
  2127. uint32_t seqno;
  2128. mutex_lock(&dev->struct_mutex);
  2129. seqno = i915_file_priv->mm.last_gem_throttle_seqno;
  2130. i915_file_priv->mm.last_gem_throttle_seqno =
  2131. i915_file_priv->mm.last_gem_seqno;
  2132. if (seqno)
  2133. ret = i915_wait_request(dev, seqno);
  2134. mutex_unlock(&dev->struct_mutex);
  2135. return ret;
  2136. }
  2137. int
  2138. i915_gem_execbuffer(struct drm_device *dev, void *data,
  2139. struct drm_file *file_priv)
  2140. {
  2141. drm_i915_private_t *dev_priv = dev->dev_private;
  2142. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  2143. struct drm_i915_gem_execbuffer *args = data;
  2144. struct drm_i915_gem_exec_object *exec_list = NULL;
  2145. struct drm_gem_object **object_list = NULL;
  2146. struct drm_gem_object *batch_obj;
  2147. struct drm_i915_gem_object *obj_priv;
  2148. int ret, i, pinned = 0;
  2149. uint64_t exec_offset;
  2150. uint32_t seqno, flush_domains;
  2151. int pin_tries;
  2152. #if WATCH_EXEC
  2153. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  2154. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  2155. #endif
  2156. if (args->buffer_count < 1) {
  2157. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  2158. return -EINVAL;
  2159. }
  2160. /* Copy in the exec list from userland */
  2161. exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
  2162. DRM_MEM_DRIVER);
  2163. object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
  2164. DRM_MEM_DRIVER);
  2165. if (exec_list == NULL || object_list == NULL) {
  2166. DRM_ERROR("Failed to allocate exec or object list "
  2167. "for %d buffers\n",
  2168. args->buffer_count);
  2169. ret = -ENOMEM;
  2170. goto pre_mutex_err;
  2171. }
  2172. ret = copy_from_user(exec_list,
  2173. (struct drm_i915_relocation_entry __user *)
  2174. (uintptr_t) args->buffers_ptr,
  2175. sizeof(*exec_list) * args->buffer_count);
  2176. if (ret != 0) {
  2177. DRM_ERROR("copy %d exec entries failed %d\n",
  2178. args->buffer_count, ret);
  2179. goto pre_mutex_err;
  2180. }
  2181. mutex_lock(&dev->struct_mutex);
  2182. i915_verify_inactive(dev, __FILE__, __LINE__);
  2183. if (dev_priv->mm.wedged) {
  2184. DRM_ERROR("Execbuf while wedged\n");
  2185. mutex_unlock(&dev->struct_mutex);
  2186. ret = -EIO;
  2187. goto pre_mutex_err;
  2188. }
  2189. if (dev_priv->mm.suspended) {
  2190. DRM_ERROR("Execbuf while VT-switched.\n");
  2191. mutex_unlock(&dev->struct_mutex);
  2192. ret = -EBUSY;
  2193. goto pre_mutex_err;
  2194. }
  2195. /* Look up object handles */
  2196. for (i = 0; i < args->buffer_count; i++) {
  2197. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  2198. exec_list[i].handle);
  2199. if (object_list[i] == NULL) {
  2200. DRM_ERROR("Invalid object handle %d at index %d\n",
  2201. exec_list[i].handle, i);
  2202. ret = -EBADF;
  2203. goto err;
  2204. }
  2205. obj_priv = object_list[i]->driver_private;
  2206. if (obj_priv->in_execbuffer) {
  2207. DRM_ERROR("Object %p appears more than once in object list\n",
  2208. object_list[i]);
  2209. ret = -EBADF;
  2210. goto err;
  2211. }
  2212. obj_priv->in_execbuffer = true;
  2213. }
  2214. /* Pin and relocate */
  2215. for (pin_tries = 0; ; pin_tries++) {
  2216. ret = 0;
  2217. for (i = 0; i < args->buffer_count; i++) {
  2218. object_list[i]->pending_read_domains = 0;
  2219. object_list[i]->pending_write_domain = 0;
  2220. ret = i915_gem_object_pin_and_relocate(object_list[i],
  2221. file_priv,
  2222. &exec_list[i]);
  2223. if (ret)
  2224. break;
  2225. pinned = i + 1;
  2226. }
  2227. /* success */
  2228. if (ret == 0)
  2229. break;
  2230. /* error other than GTT full, or we've already tried again */
  2231. if (ret != -ENOMEM || pin_tries >= 1) {
  2232. if (ret != -ERESTARTSYS)
  2233. DRM_ERROR("Failed to pin buffers %d\n", ret);
  2234. goto err;
  2235. }
  2236. /* unpin all of our buffers */
  2237. for (i = 0; i < pinned; i++)
  2238. i915_gem_object_unpin(object_list[i]);
  2239. pinned = 0;
  2240. /* evict everyone we can from the aperture */
  2241. ret = i915_gem_evict_everything(dev);
  2242. if (ret)
  2243. goto err;
  2244. }
  2245. /* Set the pending read domains for the batch buffer to COMMAND */
  2246. batch_obj = object_list[args->buffer_count-1];
  2247. batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  2248. batch_obj->pending_write_domain = 0;
  2249. i915_verify_inactive(dev, __FILE__, __LINE__);
  2250. /* Zero the global flush/invalidate flags. These
  2251. * will be modified as new domains are computed
  2252. * for each object
  2253. */
  2254. dev->invalidate_domains = 0;
  2255. dev->flush_domains = 0;
  2256. for (i = 0; i < args->buffer_count; i++) {
  2257. struct drm_gem_object *obj = object_list[i];
  2258. /* Compute new gpu domains and update invalidate/flush */
  2259. i915_gem_object_set_to_gpu_domain(obj);
  2260. }
  2261. i915_verify_inactive(dev, __FILE__, __LINE__);
  2262. if (dev->invalidate_domains | dev->flush_domains) {
  2263. #if WATCH_EXEC
  2264. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  2265. __func__,
  2266. dev->invalidate_domains,
  2267. dev->flush_domains);
  2268. #endif
  2269. i915_gem_flush(dev,
  2270. dev->invalidate_domains,
  2271. dev->flush_domains);
  2272. if (dev->flush_domains)
  2273. (void)i915_add_request(dev, dev->flush_domains);
  2274. }
  2275. for (i = 0; i < args->buffer_count; i++) {
  2276. struct drm_gem_object *obj = object_list[i];
  2277. obj->write_domain = obj->pending_write_domain;
  2278. }
  2279. i915_verify_inactive(dev, __FILE__, __LINE__);
  2280. #if WATCH_COHERENCY
  2281. for (i = 0; i < args->buffer_count; i++) {
  2282. i915_gem_object_check_coherency(object_list[i],
  2283. exec_list[i].handle);
  2284. }
  2285. #endif
  2286. exec_offset = exec_list[args->buffer_count - 1].offset;
  2287. #if WATCH_EXEC
  2288. i915_gem_dump_object(object_list[args->buffer_count - 1],
  2289. args->batch_len,
  2290. __func__,
  2291. ~0);
  2292. #endif
  2293. /* Exec the batchbuffer */
  2294. ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
  2295. if (ret) {
  2296. DRM_ERROR("dispatch failed %d\n", ret);
  2297. goto err;
  2298. }
  2299. /*
  2300. * Ensure that the commands in the batch buffer are
  2301. * finished before the interrupt fires
  2302. */
  2303. flush_domains = i915_retire_commands(dev);
  2304. i915_verify_inactive(dev, __FILE__, __LINE__);
  2305. /*
  2306. * Get a seqno representing the execution of the current buffer,
  2307. * which we can wait on. We would like to mitigate these interrupts,
  2308. * likely by only creating seqnos occasionally (so that we have
  2309. * *some* interrupts representing completion of buffers that we can
  2310. * wait on when trying to clear up gtt space).
  2311. */
  2312. seqno = i915_add_request(dev, flush_domains);
  2313. BUG_ON(seqno == 0);
  2314. i915_file_priv->mm.last_gem_seqno = seqno;
  2315. for (i = 0; i < args->buffer_count; i++) {
  2316. struct drm_gem_object *obj = object_list[i];
  2317. i915_gem_object_move_to_active(obj, seqno);
  2318. #if WATCH_LRU
  2319. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  2320. #endif
  2321. }
  2322. #if WATCH_LRU
  2323. i915_dump_lru(dev, __func__);
  2324. #endif
  2325. i915_verify_inactive(dev, __FILE__, __LINE__);
  2326. err:
  2327. for (i = 0; i < pinned; i++)
  2328. i915_gem_object_unpin(object_list[i]);
  2329. for (i = 0; i < args->buffer_count; i++) {
  2330. if (object_list[i]) {
  2331. obj_priv = object_list[i]->driver_private;
  2332. obj_priv->in_execbuffer = false;
  2333. }
  2334. drm_gem_object_unreference(object_list[i]);
  2335. }
  2336. mutex_unlock(&dev->struct_mutex);
  2337. if (!ret) {
  2338. /* Copy the new buffer offsets back to the user's exec list. */
  2339. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  2340. (uintptr_t) args->buffers_ptr,
  2341. exec_list,
  2342. sizeof(*exec_list) * args->buffer_count);
  2343. if (ret)
  2344. DRM_ERROR("failed to copy %d exec entries "
  2345. "back to user (%d)\n",
  2346. args->buffer_count, ret);
  2347. }
  2348. pre_mutex_err:
  2349. drm_free(object_list, sizeof(*object_list) * args->buffer_count,
  2350. DRM_MEM_DRIVER);
  2351. drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
  2352. DRM_MEM_DRIVER);
  2353. return ret;
  2354. }
  2355. int
  2356. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  2357. {
  2358. struct drm_device *dev = obj->dev;
  2359. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2360. int ret;
  2361. i915_verify_inactive(dev, __FILE__, __LINE__);
  2362. if (obj_priv->gtt_space == NULL) {
  2363. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  2364. if (ret != 0) {
  2365. if (ret != -EBUSY && ret != -ERESTARTSYS)
  2366. DRM_ERROR("Failure to bind: %d\n", ret);
  2367. return ret;
  2368. }
  2369. }
  2370. /*
  2371. * Pre-965 chips need a fence register set up in order to
  2372. * properly handle tiled surfaces.
  2373. */
  2374. if (!IS_I965G(dev) &&
  2375. obj_priv->fence_reg == I915_FENCE_REG_NONE &&
  2376. obj_priv->tiling_mode != I915_TILING_NONE) {
  2377. ret = i915_gem_object_get_fence_reg(obj, true);
  2378. if (ret != 0) {
  2379. if (ret != -EBUSY && ret != -ERESTARTSYS)
  2380. DRM_ERROR("Failure to install fence: %d\n",
  2381. ret);
  2382. return ret;
  2383. }
  2384. }
  2385. obj_priv->pin_count++;
  2386. /* If the object is not active and not pending a flush,
  2387. * remove it from the inactive list
  2388. */
  2389. if (obj_priv->pin_count == 1) {
  2390. atomic_inc(&dev->pin_count);
  2391. atomic_add(obj->size, &dev->pin_memory);
  2392. if (!obj_priv->active &&
  2393. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  2394. I915_GEM_DOMAIN_GTT)) == 0 &&
  2395. !list_empty(&obj_priv->list))
  2396. list_del_init(&obj_priv->list);
  2397. }
  2398. i915_verify_inactive(dev, __FILE__, __LINE__);
  2399. return 0;
  2400. }
  2401. void
  2402. i915_gem_object_unpin(struct drm_gem_object *obj)
  2403. {
  2404. struct drm_device *dev = obj->dev;
  2405. drm_i915_private_t *dev_priv = dev->dev_private;
  2406. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2407. i915_verify_inactive(dev, __FILE__, __LINE__);
  2408. obj_priv->pin_count--;
  2409. BUG_ON(obj_priv->pin_count < 0);
  2410. BUG_ON(obj_priv->gtt_space == NULL);
  2411. /* If the object is no longer pinned, and is
  2412. * neither active nor being flushed, then stick it on
  2413. * the inactive list
  2414. */
  2415. if (obj_priv->pin_count == 0) {
  2416. if (!obj_priv->active &&
  2417. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  2418. I915_GEM_DOMAIN_GTT)) == 0)
  2419. list_move_tail(&obj_priv->list,
  2420. &dev_priv->mm.inactive_list);
  2421. atomic_dec(&dev->pin_count);
  2422. atomic_sub(obj->size, &dev->pin_memory);
  2423. }
  2424. i915_verify_inactive(dev, __FILE__, __LINE__);
  2425. }
  2426. int
  2427. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  2428. struct drm_file *file_priv)
  2429. {
  2430. struct drm_i915_gem_pin *args = data;
  2431. struct drm_gem_object *obj;
  2432. struct drm_i915_gem_object *obj_priv;
  2433. int ret;
  2434. mutex_lock(&dev->struct_mutex);
  2435. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  2436. if (obj == NULL) {
  2437. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  2438. args->handle);
  2439. mutex_unlock(&dev->struct_mutex);
  2440. return -EBADF;
  2441. }
  2442. obj_priv = obj->driver_private;
  2443. if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
  2444. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  2445. args->handle);
  2446. drm_gem_object_unreference(obj);
  2447. mutex_unlock(&dev->struct_mutex);
  2448. return -EINVAL;
  2449. }
  2450. obj_priv->user_pin_count++;
  2451. obj_priv->pin_filp = file_priv;
  2452. if (obj_priv->user_pin_count == 1) {
  2453. ret = i915_gem_object_pin(obj, args->alignment);
  2454. if (ret != 0) {
  2455. drm_gem_object_unreference(obj);
  2456. mutex_unlock(&dev->struct_mutex);
  2457. return ret;
  2458. }
  2459. }
  2460. /* XXX - flush the CPU caches for pinned objects
  2461. * as the X server doesn't manage domains yet
  2462. */
  2463. i915_gem_object_flush_cpu_write_domain(obj);
  2464. args->offset = obj_priv->gtt_offset;
  2465. drm_gem_object_unreference(obj);
  2466. mutex_unlock(&dev->struct_mutex);
  2467. return 0;
  2468. }
  2469. int
  2470. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  2471. struct drm_file *file_priv)
  2472. {
  2473. struct drm_i915_gem_pin *args = data;
  2474. struct drm_gem_object *obj;
  2475. struct drm_i915_gem_object *obj_priv;
  2476. mutex_lock(&dev->struct_mutex);
  2477. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  2478. if (obj == NULL) {
  2479. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  2480. args->handle);
  2481. mutex_unlock(&dev->struct_mutex);
  2482. return -EBADF;
  2483. }
  2484. obj_priv = obj->driver_private;
  2485. if (obj_priv->pin_filp != file_priv) {
  2486. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  2487. args->handle);
  2488. drm_gem_object_unreference(obj);
  2489. mutex_unlock(&dev->struct_mutex);
  2490. return -EINVAL;
  2491. }
  2492. obj_priv->user_pin_count--;
  2493. if (obj_priv->user_pin_count == 0) {
  2494. obj_priv->pin_filp = NULL;
  2495. i915_gem_object_unpin(obj);
  2496. }
  2497. drm_gem_object_unreference(obj);
  2498. mutex_unlock(&dev->struct_mutex);
  2499. return 0;
  2500. }
  2501. int
  2502. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  2503. struct drm_file *file_priv)
  2504. {
  2505. struct drm_i915_gem_busy *args = data;
  2506. struct drm_gem_object *obj;
  2507. struct drm_i915_gem_object *obj_priv;
  2508. mutex_lock(&dev->struct_mutex);
  2509. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  2510. if (obj == NULL) {
  2511. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  2512. args->handle);
  2513. mutex_unlock(&dev->struct_mutex);
  2514. return -EBADF;
  2515. }
  2516. /* Update the active list for the hardware's current position.
  2517. * Otherwise this only updates on a delayed timer or when irqs are
  2518. * actually unmasked, and our working set ends up being larger than
  2519. * required.
  2520. */
  2521. i915_gem_retire_requests(dev);
  2522. obj_priv = obj->driver_private;
  2523. /* Don't count being on the flushing list against the object being
  2524. * done. Otherwise, a buffer left on the flushing list but not getting
  2525. * flushed (because nobody's flushing that domain) won't ever return
  2526. * unbusy and get reused by libdrm's bo cache. The other expected
  2527. * consumer of this interface, OpenGL's occlusion queries, also specs
  2528. * that the objects get unbusy "eventually" without any interference.
  2529. */
  2530. args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
  2531. drm_gem_object_unreference(obj);
  2532. mutex_unlock(&dev->struct_mutex);
  2533. return 0;
  2534. }
  2535. int
  2536. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  2537. struct drm_file *file_priv)
  2538. {
  2539. return i915_gem_ring_throttle(dev, file_priv);
  2540. }
  2541. int i915_gem_init_object(struct drm_gem_object *obj)
  2542. {
  2543. struct drm_i915_gem_object *obj_priv;
  2544. obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
  2545. if (obj_priv == NULL)
  2546. return -ENOMEM;
  2547. /*
  2548. * We've just allocated pages from the kernel,
  2549. * so they've just been written by the CPU with
  2550. * zeros. They'll need to be clflushed before we
  2551. * use them with the GPU.
  2552. */
  2553. obj->write_domain = I915_GEM_DOMAIN_CPU;
  2554. obj->read_domains = I915_GEM_DOMAIN_CPU;
  2555. obj_priv->agp_type = AGP_USER_MEMORY;
  2556. obj->driver_private = obj_priv;
  2557. obj_priv->obj = obj;
  2558. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  2559. INIT_LIST_HEAD(&obj_priv->list);
  2560. return 0;
  2561. }
  2562. void i915_gem_free_object(struct drm_gem_object *obj)
  2563. {
  2564. struct drm_device *dev = obj->dev;
  2565. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2566. while (obj_priv->pin_count > 0)
  2567. i915_gem_object_unpin(obj);
  2568. if (obj_priv->phys_obj)
  2569. i915_gem_detach_phys_object(dev, obj);
  2570. i915_gem_object_unbind(obj);
  2571. i915_gem_free_mmap_offset(obj);
  2572. drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
  2573. drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
  2574. }
  2575. /** Unbinds all objects that are on the given buffer list. */
  2576. static int
  2577. i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
  2578. {
  2579. struct drm_gem_object *obj;
  2580. struct drm_i915_gem_object *obj_priv;
  2581. int ret;
  2582. while (!list_empty(head)) {
  2583. obj_priv = list_first_entry(head,
  2584. struct drm_i915_gem_object,
  2585. list);
  2586. obj = obj_priv->obj;
  2587. if (obj_priv->pin_count != 0) {
  2588. DRM_ERROR("Pinned object in unbind list\n");
  2589. mutex_unlock(&dev->struct_mutex);
  2590. return -EINVAL;
  2591. }
  2592. ret = i915_gem_object_unbind(obj);
  2593. if (ret != 0) {
  2594. DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
  2595. ret);
  2596. mutex_unlock(&dev->struct_mutex);
  2597. return ret;
  2598. }
  2599. }
  2600. return 0;
  2601. }
  2602. int
  2603. i915_gem_idle(struct drm_device *dev)
  2604. {
  2605. drm_i915_private_t *dev_priv = dev->dev_private;
  2606. uint32_t seqno, cur_seqno, last_seqno;
  2607. int stuck, ret;
  2608. mutex_lock(&dev->struct_mutex);
  2609. if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
  2610. mutex_unlock(&dev->struct_mutex);
  2611. return 0;
  2612. }
  2613. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  2614. * We need to replace this with a semaphore, or something.
  2615. */
  2616. dev_priv->mm.suspended = 1;
  2617. /* Cancel the retire work handler, wait for it to finish if running
  2618. */
  2619. mutex_unlock(&dev->struct_mutex);
  2620. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  2621. mutex_lock(&dev->struct_mutex);
  2622. i915_kernel_lost_context(dev);
  2623. /* Flush the GPU along with all non-CPU write domains
  2624. */
  2625. i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
  2626. ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  2627. seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU);
  2628. if (seqno == 0) {
  2629. mutex_unlock(&dev->struct_mutex);
  2630. return -ENOMEM;
  2631. }
  2632. dev_priv->mm.waiting_gem_seqno = seqno;
  2633. last_seqno = 0;
  2634. stuck = 0;
  2635. for (;;) {
  2636. cur_seqno = i915_get_gem_seqno(dev);
  2637. if (i915_seqno_passed(cur_seqno, seqno))
  2638. break;
  2639. if (last_seqno == cur_seqno) {
  2640. if (stuck++ > 100) {
  2641. DRM_ERROR("hardware wedged\n");
  2642. dev_priv->mm.wedged = 1;
  2643. DRM_WAKEUP(&dev_priv->irq_queue);
  2644. break;
  2645. }
  2646. }
  2647. msleep(10);
  2648. last_seqno = cur_seqno;
  2649. }
  2650. dev_priv->mm.waiting_gem_seqno = 0;
  2651. i915_gem_retire_requests(dev);
  2652. if (!dev_priv->mm.wedged) {
  2653. /* Active and flushing should now be empty as we've
  2654. * waited for a sequence higher than any pending execbuffer
  2655. */
  2656. WARN_ON(!list_empty(&dev_priv->mm.active_list));
  2657. WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
  2658. /* Request should now be empty as we've also waited
  2659. * for the last request in the list
  2660. */
  2661. WARN_ON(!list_empty(&dev_priv->mm.request_list));
  2662. }
  2663. /* Empty the active and flushing lists to inactive. If there's
  2664. * anything left at this point, it means that we're wedged and
  2665. * nothing good's going to happen by leaving them there. So strip
  2666. * the GPU domains and just stuff them onto inactive.
  2667. */
  2668. while (!list_empty(&dev_priv->mm.active_list)) {
  2669. struct drm_i915_gem_object *obj_priv;
  2670. obj_priv = list_first_entry(&dev_priv->mm.active_list,
  2671. struct drm_i915_gem_object,
  2672. list);
  2673. obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
  2674. i915_gem_object_move_to_inactive(obj_priv->obj);
  2675. }
  2676. while (!list_empty(&dev_priv->mm.flushing_list)) {
  2677. struct drm_i915_gem_object *obj_priv;
  2678. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  2679. struct drm_i915_gem_object,
  2680. list);
  2681. obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
  2682. i915_gem_object_move_to_inactive(obj_priv->obj);
  2683. }
  2684. /* Move all inactive buffers out of the GTT. */
  2685. ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
  2686. WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
  2687. if (ret) {
  2688. mutex_unlock(&dev->struct_mutex);
  2689. return ret;
  2690. }
  2691. i915_gem_cleanup_ringbuffer(dev);
  2692. mutex_unlock(&dev->struct_mutex);
  2693. return 0;
  2694. }
  2695. static int
  2696. i915_gem_init_hws(struct drm_device *dev)
  2697. {
  2698. drm_i915_private_t *dev_priv = dev->dev_private;
  2699. struct drm_gem_object *obj;
  2700. struct drm_i915_gem_object *obj_priv;
  2701. int ret;
  2702. /* If we need a physical address for the status page, it's already
  2703. * initialized at driver load time.
  2704. */
  2705. if (!I915_NEED_GFX_HWS(dev))
  2706. return 0;
  2707. obj = drm_gem_object_alloc(dev, 4096);
  2708. if (obj == NULL) {
  2709. DRM_ERROR("Failed to allocate status page\n");
  2710. return -ENOMEM;
  2711. }
  2712. obj_priv = obj->driver_private;
  2713. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  2714. ret = i915_gem_object_pin(obj, 4096);
  2715. if (ret != 0) {
  2716. drm_gem_object_unreference(obj);
  2717. return ret;
  2718. }
  2719. dev_priv->status_gfx_addr = obj_priv->gtt_offset;
  2720. dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
  2721. if (dev_priv->hw_status_page == NULL) {
  2722. DRM_ERROR("Failed to map status page.\n");
  2723. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2724. i915_gem_object_unpin(obj);
  2725. drm_gem_object_unreference(obj);
  2726. return -EINVAL;
  2727. }
  2728. dev_priv->hws_obj = obj;
  2729. memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
  2730. I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
  2731. I915_READ(HWS_PGA); /* posting read */
  2732. DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
  2733. return 0;
  2734. }
  2735. static void
  2736. i915_gem_cleanup_hws(struct drm_device *dev)
  2737. {
  2738. drm_i915_private_t *dev_priv = dev->dev_private;
  2739. struct drm_gem_object *obj;
  2740. struct drm_i915_gem_object *obj_priv;
  2741. if (dev_priv->hws_obj == NULL)
  2742. return;
  2743. obj = dev_priv->hws_obj;
  2744. obj_priv = obj->driver_private;
  2745. kunmap(obj_priv->page_list[0]);
  2746. i915_gem_object_unpin(obj);
  2747. drm_gem_object_unreference(obj);
  2748. dev_priv->hws_obj = NULL;
  2749. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2750. dev_priv->hw_status_page = NULL;
  2751. /* Write high address into HWS_PGA when disabling. */
  2752. I915_WRITE(HWS_PGA, 0x1ffff000);
  2753. }
  2754. int
  2755. i915_gem_init_ringbuffer(struct drm_device *dev)
  2756. {
  2757. drm_i915_private_t *dev_priv = dev->dev_private;
  2758. struct drm_gem_object *obj;
  2759. struct drm_i915_gem_object *obj_priv;
  2760. drm_i915_ring_buffer_t *ring = &dev_priv->ring;
  2761. int ret;
  2762. u32 head;
  2763. ret = i915_gem_init_hws(dev);
  2764. if (ret != 0)
  2765. return ret;
  2766. obj = drm_gem_object_alloc(dev, 128 * 1024);
  2767. if (obj == NULL) {
  2768. DRM_ERROR("Failed to allocate ringbuffer\n");
  2769. i915_gem_cleanup_hws(dev);
  2770. return -ENOMEM;
  2771. }
  2772. obj_priv = obj->driver_private;
  2773. ret = i915_gem_object_pin(obj, 4096);
  2774. if (ret != 0) {
  2775. drm_gem_object_unreference(obj);
  2776. i915_gem_cleanup_hws(dev);
  2777. return ret;
  2778. }
  2779. /* Set up the kernel mapping for the ring. */
  2780. ring->Size = obj->size;
  2781. ring->tail_mask = obj->size - 1;
  2782. ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
  2783. ring->map.size = obj->size;
  2784. ring->map.type = 0;
  2785. ring->map.flags = 0;
  2786. ring->map.mtrr = 0;
  2787. drm_core_ioremap_wc(&ring->map, dev);
  2788. if (ring->map.handle == NULL) {
  2789. DRM_ERROR("Failed to map ringbuffer.\n");
  2790. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2791. i915_gem_object_unpin(obj);
  2792. drm_gem_object_unreference(obj);
  2793. i915_gem_cleanup_hws(dev);
  2794. return -EINVAL;
  2795. }
  2796. ring->ring_obj = obj;
  2797. ring->virtual_start = ring->map.handle;
  2798. /* Stop the ring if it's running. */
  2799. I915_WRITE(PRB0_CTL, 0);
  2800. I915_WRITE(PRB0_TAIL, 0);
  2801. I915_WRITE(PRB0_HEAD, 0);
  2802. /* Initialize the ring. */
  2803. I915_WRITE(PRB0_START, obj_priv->gtt_offset);
  2804. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2805. /* G45 ring initialization fails to reset head to zero */
  2806. if (head != 0) {
  2807. DRM_ERROR("Ring head not reset to zero "
  2808. "ctl %08x head %08x tail %08x start %08x\n",
  2809. I915_READ(PRB0_CTL),
  2810. I915_READ(PRB0_HEAD),
  2811. I915_READ(PRB0_TAIL),
  2812. I915_READ(PRB0_START));
  2813. I915_WRITE(PRB0_HEAD, 0);
  2814. DRM_ERROR("Ring head forced to zero "
  2815. "ctl %08x head %08x tail %08x start %08x\n",
  2816. I915_READ(PRB0_CTL),
  2817. I915_READ(PRB0_HEAD),
  2818. I915_READ(PRB0_TAIL),
  2819. I915_READ(PRB0_START));
  2820. }
  2821. I915_WRITE(PRB0_CTL,
  2822. ((obj->size - 4096) & RING_NR_PAGES) |
  2823. RING_NO_REPORT |
  2824. RING_VALID);
  2825. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2826. /* If the head is still not zero, the ring is dead */
  2827. if (head != 0) {
  2828. DRM_ERROR("Ring initialization failed "
  2829. "ctl %08x head %08x tail %08x start %08x\n",
  2830. I915_READ(PRB0_CTL),
  2831. I915_READ(PRB0_HEAD),
  2832. I915_READ(PRB0_TAIL),
  2833. I915_READ(PRB0_START));
  2834. return -EIO;
  2835. }
  2836. /* Update our cache of the ring state */
  2837. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2838. i915_kernel_lost_context(dev);
  2839. else {
  2840. ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2841. ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
  2842. ring->space = ring->head - (ring->tail + 8);
  2843. if (ring->space < 0)
  2844. ring->space += ring->Size;
  2845. }
  2846. return 0;
  2847. }
  2848. void
  2849. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  2850. {
  2851. drm_i915_private_t *dev_priv = dev->dev_private;
  2852. if (dev_priv->ring.ring_obj == NULL)
  2853. return;
  2854. drm_core_ioremapfree(&dev_priv->ring.map, dev);
  2855. i915_gem_object_unpin(dev_priv->ring.ring_obj);
  2856. drm_gem_object_unreference(dev_priv->ring.ring_obj);
  2857. dev_priv->ring.ring_obj = NULL;
  2858. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2859. i915_gem_cleanup_hws(dev);
  2860. }
  2861. int
  2862. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  2863. struct drm_file *file_priv)
  2864. {
  2865. drm_i915_private_t *dev_priv = dev->dev_private;
  2866. int ret;
  2867. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2868. return 0;
  2869. if (dev_priv->mm.wedged) {
  2870. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  2871. dev_priv->mm.wedged = 0;
  2872. }
  2873. mutex_lock(&dev->struct_mutex);
  2874. dev_priv->mm.suspended = 0;
  2875. ret = i915_gem_init_ringbuffer(dev);
  2876. if (ret != 0)
  2877. return ret;
  2878. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  2879. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  2880. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  2881. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  2882. mutex_unlock(&dev->struct_mutex);
  2883. drm_irq_install(dev);
  2884. return 0;
  2885. }
  2886. int
  2887. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  2888. struct drm_file *file_priv)
  2889. {
  2890. int ret;
  2891. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2892. return 0;
  2893. ret = i915_gem_idle(dev);
  2894. drm_irq_uninstall(dev);
  2895. return ret;
  2896. }
  2897. void
  2898. i915_gem_lastclose(struct drm_device *dev)
  2899. {
  2900. int ret;
  2901. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2902. return;
  2903. ret = i915_gem_idle(dev);
  2904. if (ret)
  2905. DRM_ERROR("failed to idle hardware: %d\n", ret);
  2906. }
  2907. void
  2908. i915_gem_load(struct drm_device *dev)
  2909. {
  2910. drm_i915_private_t *dev_priv = dev->dev_private;
  2911. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  2912. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  2913. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  2914. INIT_LIST_HEAD(&dev_priv->mm.request_list);
  2915. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  2916. i915_gem_retire_work_handler);
  2917. dev_priv->mm.next_gem_seqno = 1;
  2918. /* Old X drivers will take 0-2 for front, back, depth buffers */
  2919. dev_priv->fence_reg_start = 3;
  2920. if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  2921. dev_priv->num_fence_regs = 16;
  2922. else
  2923. dev_priv->num_fence_regs = 8;
  2924. i915_gem_detect_bit_6_swizzle(dev);
  2925. }
  2926. /*
  2927. * Create a physically contiguous memory object for this object
  2928. * e.g. for cursor + overlay regs
  2929. */
  2930. int i915_gem_init_phys_object(struct drm_device *dev,
  2931. int id, int size)
  2932. {
  2933. drm_i915_private_t *dev_priv = dev->dev_private;
  2934. struct drm_i915_gem_phys_object *phys_obj;
  2935. int ret;
  2936. if (dev_priv->mm.phys_objs[id - 1] || !size)
  2937. return 0;
  2938. phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
  2939. if (!phys_obj)
  2940. return -ENOMEM;
  2941. phys_obj->id = id;
  2942. phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
  2943. if (!phys_obj->handle) {
  2944. ret = -ENOMEM;
  2945. goto kfree_obj;
  2946. }
  2947. #ifdef CONFIG_X86
  2948. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  2949. #endif
  2950. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  2951. return 0;
  2952. kfree_obj:
  2953. drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
  2954. return ret;
  2955. }
  2956. void i915_gem_free_phys_object(struct drm_device *dev, int id)
  2957. {
  2958. drm_i915_private_t *dev_priv = dev->dev_private;
  2959. struct drm_i915_gem_phys_object *phys_obj;
  2960. if (!dev_priv->mm.phys_objs[id - 1])
  2961. return;
  2962. phys_obj = dev_priv->mm.phys_objs[id - 1];
  2963. if (phys_obj->cur_obj) {
  2964. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  2965. }
  2966. #ifdef CONFIG_X86
  2967. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  2968. #endif
  2969. drm_pci_free(dev, phys_obj->handle);
  2970. kfree(phys_obj);
  2971. dev_priv->mm.phys_objs[id - 1] = NULL;
  2972. }
  2973. void i915_gem_free_all_phys_object(struct drm_device *dev)
  2974. {
  2975. int i;
  2976. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  2977. i915_gem_free_phys_object(dev, i);
  2978. }
  2979. void i915_gem_detach_phys_object(struct drm_device *dev,
  2980. struct drm_gem_object *obj)
  2981. {
  2982. struct drm_i915_gem_object *obj_priv;
  2983. int i;
  2984. int ret;
  2985. int page_count;
  2986. obj_priv = obj->driver_private;
  2987. if (!obj_priv->phys_obj)
  2988. return;
  2989. ret = i915_gem_object_get_page_list(obj);
  2990. if (ret)
  2991. goto out;
  2992. page_count = obj->size / PAGE_SIZE;
  2993. for (i = 0; i < page_count; i++) {
  2994. char *dst = kmap_atomic(obj_priv->page_list[i], KM_USER0);
  2995. char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  2996. memcpy(dst, src, PAGE_SIZE);
  2997. kunmap_atomic(dst, KM_USER0);
  2998. }
  2999. drm_clflush_pages(obj_priv->page_list, page_count);
  3000. drm_agp_chipset_flush(dev);
  3001. out:
  3002. obj_priv->phys_obj->cur_obj = NULL;
  3003. obj_priv->phys_obj = NULL;
  3004. }
  3005. int
  3006. i915_gem_attach_phys_object(struct drm_device *dev,
  3007. struct drm_gem_object *obj, int id)
  3008. {
  3009. drm_i915_private_t *dev_priv = dev->dev_private;
  3010. struct drm_i915_gem_object *obj_priv;
  3011. int ret = 0;
  3012. int page_count;
  3013. int i;
  3014. if (id > I915_MAX_PHYS_OBJECT)
  3015. return -EINVAL;
  3016. obj_priv = obj->driver_private;
  3017. if (obj_priv->phys_obj) {
  3018. if (obj_priv->phys_obj->id == id)
  3019. return 0;
  3020. i915_gem_detach_phys_object(dev, obj);
  3021. }
  3022. /* create a new object */
  3023. if (!dev_priv->mm.phys_objs[id - 1]) {
  3024. ret = i915_gem_init_phys_object(dev, id,
  3025. obj->size);
  3026. if (ret) {
  3027. DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
  3028. goto out;
  3029. }
  3030. }
  3031. /* bind to the object */
  3032. obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3033. obj_priv->phys_obj->cur_obj = obj;
  3034. ret = i915_gem_object_get_page_list(obj);
  3035. if (ret) {
  3036. DRM_ERROR("failed to get page list\n");
  3037. goto out;
  3038. }
  3039. page_count = obj->size / PAGE_SIZE;
  3040. for (i = 0; i < page_count; i++) {
  3041. char *src = kmap_atomic(obj_priv->page_list[i], KM_USER0);
  3042. char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3043. memcpy(dst, src, PAGE_SIZE);
  3044. kunmap_atomic(src, KM_USER0);
  3045. }
  3046. return 0;
  3047. out:
  3048. return ret;
  3049. }
  3050. static int
  3051. i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  3052. struct drm_i915_gem_pwrite *args,
  3053. struct drm_file *file_priv)
  3054. {
  3055. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  3056. void *obj_addr;
  3057. int ret;
  3058. char __user *user_data;
  3059. user_data = (char __user *) (uintptr_t) args->data_ptr;
  3060. obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
  3061. DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
  3062. ret = copy_from_user(obj_addr, user_data, args->size);
  3063. if (ret)
  3064. return -EFAULT;
  3065. drm_agp_chipset_flush(dev);
  3066. return 0;
  3067. }