i915_gem.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612
  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. uint32_t seqno = dev_priv->mm.next_gem_seqno;
  1363. loff_t offset;
  1364. if (avail == 0)
  1365. return -ENOMEM;
  1366. for (i = dev_priv->fence_reg_start;
  1367. i < dev_priv->num_fence_regs; i++) {
  1368. uint32_t this_seqno;
  1369. reg = &dev_priv->fence_regs[i];
  1370. old_obj_priv = reg->obj->driver_private;
  1371. if (old_obj_priv->pin_count)
  1372. continue;
  1373. /* i915 uses fences for GPU access to tiled buffers */
  1374. if (IS_I965G(dev) || !old_obj_priv->active)
  1375. break;
  1376. /* find the seqno of the first available fence */
  1377. this_seqno = old_obj_priv->last_rendering_seqno;
  1378. if (this_seqno != 0 &&
  1379. reg->obj->write_domain == 0 &&
  1380. i915_seqno_passed(seqno, this_seqno))
  1381. seqno = this_seqno;
  1382. }
  1383. /*
  1384. * Now things get ugly... we have to wait for one of the
  1385. * objects to finish before trying again.
  1386. */
  1387. if (i == dev_priv->num_fence_regs) {
  1388. if (seqno == dev_priv->mm.next_gem_seqno) {
  1389. i915_gem_flush(dev,
  1390. I915_GEM_GPU_DOMAINS,
  1391. I915_GEM_GPU_DOMAINS);
  1392. seqno = i915_add_request(dev,
  1393. I915_GEM_GPU_DOMAINS);
  1394. if (seqno == 0)
  1395. return -ENOMEM;
  1396. }
  1397. ret = i915_wait_request(dev, seqno);
  1398. if (ret)
  1399. return ret;
  1400. goto try_again;
  1401. }
  1402. BUG_ON(old_obj_priv->active ||
  1403. (reg->obj->write_domain & I915_GEM_GPU_DOMAINS));
  1404. /*
  1405. * Zap this virtual mapping so we can set up a fence again
  1406. * for this object next time we need it.
  1407. */
  1408. offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT;
  1409. if (dev->dev_mapping)
  1410. unmap_mapping_range(dev->dev_mapping, offset,
  1411. reg->obj->size, 1);
  1412. old_obj_priv->fence_reg = I915_FENCE_REG_NONE;
  1413. }
  1414. obj_priv->fence_reg = i;
  1415. reg->obj = obj;
  1416. if (IS_I965G(dev))
  1417. i965_write_fence_reg(reg);
  1418. else if (IS_I9XX(dev))
  1419. i915_write_fence_reg(reg);
  1420. else
  1421. i830_write_fence_reg(reg);
  1422. return 0;
  1423. }
  1424. /**
  1425. * i915_gem_clear_fence_reg - clear out fence register info
  1426. * @obj: object to clear
  1427. *
  1428. * Zeroes out the fence register itself and clears out the associated
  1429. * data structures in dev_priv and obj_priv.
  1430. */
  1431. static void
  1432. i915_gem_clear_fence_reg(struct drm_gem_object *obj)
  1433. {
  1434. struct drm_device *dev = obj->dev;
  1435. drm_i915_private_t *dev_priv = dev->dev_private;
  1436. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1437. if (IS_I965G(dev))
  1438. I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
  1439. else
  1440. I915_WRITE(FENCE_REG_830_0 + (obj_priv->fence_reg * 4), 0);
  1441. dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
  1442. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  1443. }
  1444. /**
  1445. * Finds free space in the GTT aperture and binds the object there.
  1446. */
  1447. static int
  1448. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  1449. {
  1450. struct drm_device *dev = obj->dev;
  1451. drm_i915_private_t *dev_priv = dev->dev_private;
  1452. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1453. struct drm_mm_node *free_space;
  1454. int page_count, ret;
  1455. if (dev_priv->mm.suspended)
  1456. return -EBUSY;
  1457. if (alignment == 0)
  1458. alignment = i915_gem_get_gtt_alignment(obj);
  1459. if (alignment & (PAGE_SIZE - 1)) {
  1460. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  1461. return -EINVAL;
  1462. }
  1463. search_free:
  1464. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  1465. obj->size, alignment, 0);
  1466. if (free_space != NULL) {
  1467. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  1468. alignment);
  1469. if (obj_priv->gtt_space != NULL) {
  1470. obj_priv->gtt_space->private = obj;
  1471. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  1472. }
  1473. }
  1474. if (obj_priv->gtt_space == NULL) {
  1475. /* If the gtt is empty and we're still having trouble
  1476. * fitting our object in, we're out of memory.
  1477. */
  1478. #if WATCH_LRU
  1479. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  1480. #endif
  1481. if (list_empty(&dev_priv->mm.inactive_list) &&
  1482. list_empty(&dev_priv->mm.flushing_list) &&
  1483. list_empty(&dev_priv->mm.active_list)) {
  1484. DRM_ERROR("GTT full, but LRU list empty\n");
  1485. return -ENOMEM;
  1486. }
  1487. ret = i915_gem_evict_something(dev);
  1488. if (ret != 0) {
  1489. if (ret != -ERESTARTSYS)
  1490. DRM_ERROR("Failed to evict a buffer %d\n", ret);
  1491. return ret;
  1492. }
  1493. goto search_free;
  1494. }
  1495. #if WATCH_BUF
  1496. DRM_INFO("Binding object of size %d at 0x%08x\n",
  1497. obj->size, obj_priv->gtt_offset);
  1498. #endif
  1499. ret = i915_gem_object_get_page_list(obj);
  1500. if (ret) {
  1501. drm_mm_put_block(obj_priv->gtt_space);
  1502. obj_priv->gtt_space = NULL;
  1503. return ret;
  1504. }
  1505. page_count = obj->size / PAGE_SIZE;
  1506. /* Create an AGP memory structure pointing at our pages, and bind it
  1507. * into the GTT.
  1508. */
  1509. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  1510. obj_priv->page_list,
  1511. page_count,
  1512. obj_priv->gtt_offset,
  1513. obj_priv->agp_type);
  1514. if (obj_priv->agp_mem == NULL) {
  1515. i915_gem_object_free_page_list(obj);
  1516. drm_mm_put_block(obj_priv->gtt_space);
  1517. obj_priv->gtt_space = NULL;
  1518. return -ENOMEM;
  1519. }
  1520. atomic_inc(&dev->gtt_count);
  1521. atomic_add(obj->size, &dev->gtt_memory);
  1522. /* Assert that the object is not currently in any GPU domain. As it
  1523. * wasn't in the GTT, there shouldn't be any way it could have been in
  1524. * a GPU cache
  1525. */
  1526. BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1527. BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1528. return 0;
  1529. }
  1530. void
  1531. i915_gem_clflush_object(struct drm_gem_object *obj)
  1532. {
  1533. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1534. /* If we don't have a page list set up, then we're not pinned
  1535. * to GPU, and we can ignore the cache flush because it'll happen
  1536. * again at bind time.
  1537. */
  1538. if (obj_priv->page_list == NULL)
  1539. return;
  1540. drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
  1541. }
  1542. /** Flushes any GPU write domain for the object if it's dirty. */
  1543. static void
  1544. i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
  1545. {
  1546. struct drm_device *dev = obj->dev;
  1547. uint32_t seqno;
  1548. if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  1549. return;
  1550. /* Queue the GPU write cache flushing we need. */
  1551. i915_gem_flush(dev, 0, obj->write_domain);
  1552. seqno = i915_add_request(dev, obj->write_domain);
  1553. obj->write_domain = 0;
  1554. i915_gem_object_move_to_active(obj, seqno);
  1555. }
  1556. /** Flushes the GTT write domain for the object if it's dirty. */
  1557. static void
  1558. i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
  1559. {
  1560. if (obj->write_domain != I915_GEM_DOMAIN_GTT)
  1561. return;
  1562. /* No actual flushing is required for the GTT write domain. Writes
  1563. * to it immediately go to main memory as far as we know, so there's
  1564. * no chipset flush. It also doesn't land in render cache.
  1565. */
  1566. obj->write_domain = 0;
  1567. }
  1568. /** Flushes the CPU write domain for the object if it's dirty. */
  1569. static void
  1570. i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
  1571. {
  1572. struct drm_device *dev = obj->dev;
  1573. if (obj->write_domain != I915_GEM_DOMAIN_CPU)
  1574. return;
  1575. i915_gem_clflush_object(obj);
  1576. drm_agp_chipset_flush(dev);
  1577. obj->write_domain = 0;
  1578. }
  1579. /**
  1580. * Moves a single object to the GTT read, and possibly write domain.
  1581. *
  1582. * This function returns when the move is complete, including waiting on
  1583. * flushes to occur.
  1584. */
  1585. int
  1586. i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
  1587. {
  1588. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1589. int ret;
  1590. /* Not valid to be called on unbound objects. */
  1591. if (obj_priv->gtt_space == NULL)
  1592. return -EINVAL;
  1593. i915_gem_object_flush_gpu_write_domain(obj);
  1594. /* Wait on any GPU rendering and flushing to occur. */
  1595. ret = i915_gem_object_wait_rendering(obj);
  1596. if (ret != 0)
  1597. return ret;
  1598. /* If we're writing through the GTT domain, then CPU and GPU caches
  1599. * will need to be invalidated at next use.
  1600. */
  1601. if (write)
  1602. obj->read_domains &= I915_GEM_DOMAIN_GTT;
  1603. i915_gem_object_flush_cpu_write_domain(obj);
  1604. /* It should now be out of any other write domains, and we can update
  1605. * the domain values for our changes.
  1606. */
  1607. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  1608. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  1609. if (write) {
  1610. obj->write_domain = I915_GEM_DOMAIN_GTT;
  1611. obj_priv->dirty = 1;
  1612. }
  1613. return 0;
  1614. }
  1615. /**
  1616. * Moves a single object to the CPU read, and possibly write domain.
  1617. *
  1618. * This function returns when the move is complete, including waiting on
  1619. * flushes to occur.
  1620. */
  1621. static int
  1622. i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
  1623. {
  1624. struct drm_device *dev = obj->dev;
  1625. int ret;
  1626. i915_gem_object_flush_gpu_write_domain(obj);
  1627. /* Wait on any GPU rendering and flushing to occur. */
  1628. ret = i915_gem_object_wait_rendering(obj);
  1629. if (ret != 0)
  1630. return ret;
  1631. i915_gem_object_flush_gtt_write_domain(obj);
  1632. /* If we have a partially-valid cache of the object in the CPU,
  1633. * finish invalidating it and free the per-page flags.
  1634. */
  1635. i915_gem_object_set_to_full_cpu_read_domain(obj);
  1636. /* Flush the CPU cache if it's still invalid. */
  1637. if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  1638. i915_gem_clflush_object(obj);
  1639. drm_agp_chipset_flush(dev);
  1640. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  1641. }
  1642. /* It should now be out of any other write domains, and we can update
  1643. * the domain values for our changes.
  1644. */
  1645. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  1646. /* If we're writing through the CPU, then the GPU read domains will
  1647. * need to be invalidated at next use.
  1648. */
  1649. if (write) {
  1650. obj->read_domains &= I915_GEM_DOMAIN_CPU;
  1651. obj->write_domain = I915_GEM_DOMAIN_CPU;
  1652. }
  1653. return 0;
  1654. }
  1655. /*
  1656. * Set the next domain for the specified object. This
  1657. * may not actually perform the necessary flushing/invaliding though,
  1658. * as that may want to be batched with other set_domain operations
  1659. *
  1660. * This is (we hope) the only really tricky part of gem. The goal
  1661. * is fairly simple -- track which caches hold bits of the object
  1662. * and make sure they remain coherent. A few concrete examples may
  1663. * help to explain how it works. For shorthand, we use the notation
  1664. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  1665. * a pair of read and write domain masks.
  1666. *
  1667. * Case 1: the batch buffer
  1668. *
  1669. * 1. Allocated
  1670. * 2. Written by CPU
  1671. * 3. Mapped to GTT
  1672. * 4. Read by GPU
  1673. * 5. Unmapped from GTT
  1674. * 6. Freed
  1675. *
  1676. * Let's take these a step at a time
  1677. *
  1678. * 1. Allocated
  1679. * Pages allocated from the kernel may still have
  1680. * cache contents, so we set them to (CPU, CPU) always.
  1681. * 2. Written by CPU (using pwrite)
  1682. * The pwrite function calls set_domain (CPU, CPU) and
  1683. * this function does nothing (as nothing changes)
  1684. * 3. Mapped by GTT
  1685. * This function asserts that the object is not
  1686. * currently in any GPU-based read or write domains
  1687. * 4. Read by GPU
  1688. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  1689. * As write_domain is zero, this function adds in the
  1690. * current read domains (CPU+COMMAND, 0).
  1691. * flush_domains is set to CPU.
  1692. * invalidate_domains is set to COMMAND
  1693. * clflush is run to get data out of the CPU caches
  1694. * then i915_dev_set_domain calls i915_gem_flush to
  1695. * emit an MI_FLUSH and drm_agp_chipset_flush
  1696. * 5. Unmapped from GTT
  1697. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  1698. * flush_domains and invalidate_domains end up both zero
  1699. * so no flushing/invalidating happens
  1700. * 6. Freed
  1701. * yay, done
  1702. *
  1703. * Case 2: The shared render buffer
  1704. *
  1705. * 1. Allocated
  1706. * 2. Mapped to GTT
  1707. * 3. Read/written by GPU
  1708. * 4. set_domain to (CPU,CPU)
  1709. * 5. Read/written by CPU
  1710. * 6. Read/written by GPU
  1711. *
  1712. * 1. Allocated
  1713. * Same as last example, (CPU, CPU)
  1714. * 2. Mapped to GTT
  1715. * Nothing changes (assertions find that it is not in the GPU)
  1716. * 3. Read/written by GPU
  1717. * execbuffer calls set_domain (RENDER, RENDER)
  1718. * flush_domains gets CPU
  1719. * invalidate_domains gets GPU
  1720. * clflush (obj)
  1721. * MI_FLUSH and drm_agp_chipset_flush
  1722. * 4. set_domain (CPU, CPU)
  1723. * flush_domains gets GPU
  1724. * invalidate_domains gets CPU
  1725. * wait_rendering (obj) to make sure all drawing is complete.
  1726. * This will include an MI_FLUSH to get the data from GPU
  1727. * to memory
  1728. * clflush (obj) to invalidate the CPU cache
  1729. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  1730. * 5. Read/written by CPU
  1731. * cache lines are loaded and dirtied
  1732. * 6. Read written by GPU
  1733. * Same as last GPU access
  1734. *
  1735. * Case 3: The constant buffer
  1736. *
  1737. * 1. Allocated
  1738. * 2. Written by CPU
  1739. * 3. Read by GPU
  1740. * 4. Updated (written) by CPU again
  1741. * 5. Read by GPU
  1742. *
  1743. * 1. Allocated
  1744. * (CPU, CPU)
  1745. * 2. Written by CPU
  1746. * (CPU, CPU)
  1747. * 3. Read by GPU
  1748. * (CPU+RENDER, 0)
  1749. * flush_domains = CPU
  1750. * invalidate_domains = RENDER
  1751. * clflush (obj)
  1752. * MI_FLUSH
  1753. * drm_agp_chipset_flush
  1754. * 4. Updated (written) by CPU again
  1755. * (CPU, CPU)
  1756. * flush_domains = 0 (no previous write domain)
  1757. * invalidate_domains = 0 (no new read domains)
  1758. * 5. Read by GPU
  1759. * (CPU+RENDER, 0)
  1760. * flush_domains = CPU
  1761. * invalidate_domains = RENDER
  1762. * clflush (obj)
  1763. * MI_FLUSH
  1764. * drm_agp_chipset_flush
  1765. */
  1766. static void
  1767. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
  1768. {
  1769. struct drm_device *dev = obj->dev;
  1770. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1771. uint32_t invalidate_domains = 0;
  1772. uint32_t flush_domains = 0;
  1773. BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
  1774. BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
  1775. #if WATCH_BUF
  1776. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  1777. __func__, obj,
  1778. obj->read_domains, obj->pending_read_domains,
  1779. obj->write_domain, obj->pending_write_domain);
  1780. #endif
  1781. /*
  1782. * If the object isn't moving to a new write domain,
  1783. * let the object stay in multiple read domains
  1784. */
  1785. if (obj->pending_write_domain == 0)
  1786. obj->pending_read_domains |= obj->read_domains;
  1787. else
  1788. obj_priv->dirty = 1;
  1789. /*
  1790. * Flush the current write domain if
  1791. * the new read domains don't match. Invalidate
  1792. * any read domains which differ from the old
  1793. * write domain
  1794. */
  1795. if (obj->write_domain &&
  1796. obj->write_domain != obj->pending_read_domains) {
  1797. flush_domains |= obj->write_domain;
  1798. invalidate_domains |=
  1799. obj->pending_read_domains & ~obj->write_domain;
  1800. }
  1801. /*
  1802. * Invalidate any read caches which may have
  1803. * stale data. That is, any new read domains.
  1804. */
  1805. invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
  1806. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  1807. #if WATCH_BUF
  1808. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  1809. __func__, flush_domains, invalidate_domains);
  1810. #endif
  1811. i915_gem_clflush_object(obj);
  1812. }
  1813. /* The actual obj->write_domain will be updated with
  1814. * pending_write_domain after we emit the accumulated flush for all
  1815. * of our domain changes in execbuffers (which clears objects'
  1816. * write_domains). So if we have a current write domain that we
  1817. * aren't changing, set pending_write_domain to that.
  1818. */
  1819. if (flush_domains == 0 && obj->pending_write_domain == 0)
  1820. obj->pending_write_domain = obj->write_domain;
  1821. obj->read_domains = obj->pending_read_domains;
  1822. dev->invalidate_domains |= invalidate_domains;
  1823. dev->flush_domains |= flush_domains;
  1824. #if WATCH_BUF
  1825. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  1826. __func__,
  1827. obj->read_domains, obj->write_domain,
  1828. dev->invalidate_domains, dev->flush_domains);
  1829. #endif
  1830. }
  1831. /**
  1832. * Moves the object from a partially CPU read to a full one.
  1833. *
  1834. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  1835. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  1836. */
  1837. static void
  1838. i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
  1839. {
  1840. struct drm_device *dev = obj->dev;
  1841. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1842. if (!obj_priv->page_cpu_valid)
  1843. return;
  1844. /* If we're partially in the CPU read domain, finish moving it in.
  1845. */
  1846. if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
  1847. int i;
  1848. for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
  1849. if (obj_priv->page_cpu_valid[i])
  1850. continue;
  1851. drm_clflush_pages(obj_priv->page_list + i, 1);
  1852. }
  1853. drm_agp_chipset_flush(dev);
  1854. }
  1855. /* Free the page_cpu_valid mappings which are now stale, whether
  1856. * or not we've got I915_GEM_DOMAIN_CPU.
  1857. */
  1858. drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
  1859. DRM_MEM_DRIVER);
  1860. obj_priv->page_cpu_valid = NULL;
  1861. }
  1862. /**
  1863. * Set the CPU read domain on a range of the object.
  1864. *
  1865. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  1866. * not entirely valid. The page_cpu_valid member of the object flags which
  1867. * pages have been flushed, and will be respected by
  1868. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  1869. * of the whole object.
  1870. *
  1871. * This function returns when the move is complete, including waiting on
  1872. * flushes to occur.
  1873. */
  1874. static int
  1875. i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  1876. uint64_t offset, uint64_t size)
  1877. {
  1878. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1879. int i, ret;
  1880. if (offset == 0 && size == obj->size)
  1881. return i915_gem_object_set_to_cpu_domain(obj, 0);
  1882. i915_gem_object_flush_gpu_write_domain(obj);
  1883. /* Wait on any GPU rendering and flushing to occur. */
  1884. ret = i915_gem_object_wait_rendering(obj);
  1885. if (ret != 0)
  1886. return ret;
  1887. i915_gem_object_flush_gtt_write_domain(obj);
  1888. /* If we're already fully in the CPU read domain, we're done. */
  1889. if (obj_priv->page_cpu_valid == NULL &&
  1890. (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
  1891. return 0;
  1892. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  1893. * newly adding I915_GEM_DOMAIN_CPU
  1894. */
  1895. if (obj_priv->page_cpu_valid == NULL) {
  1896. obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
  1897. DRM_MEM_DRIVER);
  1898. if (obj_priv->page_cpu_valid == NULL)
  1899. return -ENOMEM;
  1900. } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
  1901. memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
  1902. /* Flush the cache on any pages that are still invalid from the CPU's
  1903. * perspective.
  1904. */
  1905. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  1906. i++) {
  1907. if (obj_priv->page_cpu_valid[i])
  1908. continue;
  1909. drm_clflush_pages(obj_priv->page_list + i, 1);
  1910. obj_priv->page_cpu_valid[i] = 1;
  1911. }
  1912. /* It should now be out of any other write domains, and we can update
  1913. * the domain values for our changes.
  1914. */
  1915. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  1916. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  1917. return 0;
  1918. }
  1919. /**
  1920. * Pin an object to the GTT and evaluate the relocations landing in it.
  1921. */
  1922. static int
  1923. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  1924. struct drm_file *file_priv,
  1925. struct drm_i915_gem_exec_object *entry)
  1926. {
  1927. struct drm_device *dev = obj->dev;
  1928. drm_i915_private_t *dev_priv = dev->dev_private;
  1929. struct drm_i915_gem_relocation_entry reloc;
  1930. struct drm_i915_gem_relocation_entry __user *relocs;
  1931. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1932. int i, ret;
  1933. void __iomem *reloc_page;
  1934. /* Choose the GTT offset for our buffer and put it there. */
  1935. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  1936. if (ret)
  1937. return ret;
  1938. entry->offset = obj_priv->gtt_offset;
  1939. relocs = (struct drm_i915_gem_relocation_entry __user *)
  1940. (uintptr_t) entry->relocs_ptr;
  1941. /* Apply the relocations, using the GTT aperture to avoid cache
  1942. * flushing requirements.
  1943. */
  1944. for (i = 0; i < entry->relocation_count; i++) {
  1945. struct drm_gem_object *target_obj;
  1946. struct drm_i915_gem_object *target_obj_priv;
  1947. uint32_t reloc_val, reloc_offset;
  1948. uint32_t __iomem *reloc_entry;
  1949. ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
  1950. if (ret != 0) {
  1951. i915_gem_object_unpin(obj);
  1952. return ret;
  1953. }
  1954. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  1955. reloc.target_handle);
  1956. if (target_obj == NULL) {
  1957. i915_gem_object_unpin(obj);
  1958. return -EBADF;
  1959. }
  1960. target_obj_priv = target_obj->driver_private;
  1961. /* The target buffer should have appeared before us in the
  1962. * exec_object list, so it should have a GTT space bound by now.
  1963. */
  1964. if (target_obj_priv->gtt_space == NULL) {
  1965. DRM_ERROR("No GTT space found for object %d\n",
  1966. reloc.target_handle);
  1967. drm_gem_object_unreference(target_obj);
  1968. i915_gem_object_unpin(obj);
  1969. return -EINVAL;
  1970. }
  1971. if (reloc.offset > obj->size - 4) {
  1972. DRM_ERROR("Relocation beyond object bounds: "
  1973. "obj %p target %d offset %d size %d.\n",
  1974. obj, reloc.target_handle,
  1975. (int) reloc.offset, (int) obj->size);
  1976. drm_gem_object_unreference(target_obj);
  1977. i915_gem_object_unpin(obj);
  1978. return -EINVAL;
  1979. }
  1980. if (reloc.offset & 3) {
  1981. DRM_ERROR("Relocation not 4-byte aligned: "
  1982. "obj %p target %d offset %d.\n",
  1983. obj, reloc.target_handle,
  1984. (int) reloc.offset);
  1985. drm_gem_object_unreference(target_obj);
  1986. i915_gem_object_unpin(obj);
  1987. return -EINVAL;
  1988. }
  1989. if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
  1990. reloc.read_domains & I915_GEM_DOMAIN_CPU) {
  1991. DRM_ERROR("reloc with read/write CPU domains: "
  1992. "obj %p target %d offset %d "
  1993. "read %08x write %08x",
  1994. obj, reloc.target_handle,
  1995. (int) reloc.offset,
  1996. reloc.read_domains,
  1997. reloc.write_domain);
  1998. drm_gem_object_unreference(target_obj);
  1999. i915_gem_object_unpin(obj);
  2000. return -EINVAL;
  2001. }
  2002. if (reloc.write_domain && target_obj->pending_write_domain &&
  2003. reloc.write_domain != target_obj->pending_write_domain) {
  2004. DRM_ERROR("Write domain conflict: "
  2005. "obj %p target %d offset %d "
  2006. "new %08x old %08x\n",
  2007. obj, reloc.target_handle,
  2008. (int) reloc.offset,
  2009. reloc.write_domain,
  2010. target_obj->pending_write_domain);
  2011. drm_gem_object_unreference(target_obj);
  2012. i915_gem_object_unpin(obj);
  2013. return -EINVAL;
  2014. }
  2015. #if WATCH_RELOC
  2016. DRM_INFO("%s: obj %p offset %08x target %d "
  2017. "read %08x write %08x gtt %08x "
  2018. "presumed %08x delta %08x\n",
  2019. __func__,
  2020. obj,
  2021. (int) reloc.offset,
  2022. (int) reloc.target_handle,
  2023. (int) reloc.read_domains,
  2024. (int) reloc.write_domain,
  2025. (int) target_obj_priv->gtt_offset,
  2026. (int) reloc.presumed_offset,
  2027. reloc.delta);
  2028. #endif
  2029. target_obj->pending_read_domains |= reloc.read_domains;
  2030. target_obj->pending_write_domain |= reloc.write_domain;
  2031. /* If the relocation already has the right value in it, no
  2032. * more work needs to be done.
  2033. */
  2034. if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
  2035. drm_gem_object_unreference(target_obj);
  2036. continue;
  2037. }
  2038. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  2039. if (ret != 0) {
  2040. drm_gem_object_unreference(target_obj);
  2041. i915_gem_object_unpin(obj);
  2042. return -EINVAL;
  2043. }
  2044. /* Map the page containing the relocation we're going to
  2045. * perform.
  2046. */
  2047. reloc_offset = obj_priv->gtt_offset + reloc.offset;
  2048. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  2049. (reloc_offset &
  2050. ~(PAGE_SIZE - 1)));
  2051. reloc_entry = (uint32_t __iomem *)(reloc_page +
  2052. (reloc_offset & (PAGE_SIZE - 1)));
  2053. reloc_val = target_obj_priv->gtt_offset + reloc.delta;
  2054. #if WATCH_BUF
  2055. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  2056. obj, (unsigned int) reloc.offset,
  2057. readl(reloc_entry), reloc_val);
  2058. #endif
  2059. writel(reloc_val, reloc_entry);
  2060. io_mapping_unmap_atomic(reloc_page);
  2061. /* Write the updated presumed offset for this entry back out
  2062. * to the user.
  2063. */
  2064. reloc.presumed_offset = target_obj_priv->gtt_offset;
  2065. ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
  2066. if (ret != 0) {
  2067. drm_gem_object_unreference(target_obj);
  2068. i915_gem_object_unpin(obj);
  2069. return ret;
  2070. }
  2071. drm_gem_object_unreference(target_obj);
  2072. }
  2073. #if WATCH_BUF
  2074. if (0)
  2075. i915_gem_dump_object(obj, 128, __func__, ~0);
  2076. #endif
  2077. return 0;
  2078. }
  2079. /** Dispatch a batchbuffer to the ring
  2080. */
  2081. static int
  2082. i915_dispatch_gem_execbuffer(struct drm_device *dev,
  2083. struct drm_i915_gem_execbuffer *exec,
  2084. uint64_t exec_offset)
  2085. {
  2086. drm_i915_private_t *dev_priv = dev->dev_private;
  2087. struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
  2088. (uintptr_t) exec->cliprects_ptr;
  2089. int nbox = exec->num_cliprects;
  2090. int i = 0, count;
  2091. uint32_t exec_start, exec_len;
  2092. RING_LOCALS;
  2093. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  2094. exec_len = (uint32_t) exec->batch_len;
  2095. if ((exec_start | exec_len) & 0x7) {
  2096. DRM_ERROR("alignment\n");
  2097. return -EINVAL;
  2098. }
  2099. if (!exec_start)
  2100. return -EINVAL;
  2101. count = nbox ? nbox : 1;
  2102. for (i = 0; i < count; i++) {
  2103. if (i < nbox) {
  2104. int ret = i915_emit_box(dev, boxes, i,
  2105. exec->DR1, exec->DR4);
  2106. if (ret)
  2107. return ret;
  2108. }
  2109. if (IS_I830(dev) || IS_845G(dev)) {
  2110. BEGIN_LP_RING(4);
  2111. OUT_RING(MI_BATCH_BUFFER);
  2112. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  2113. OUT_RING(exec_start + exec_len - 4);
  2114. OUT_RING(0);
  2115. ADVANCE_LP_RING();
  2116. } else {
  2117. BEGIN_LP_RING(2);
  2118. if (IS_I965G(dev)) {
  2119. OUT_RING(MI_BATCH_BUFFER_START |
  2120. (2 << 6) |
  2121. MI_BATCH_NON_SECURE_I965);
  2122. OUT_RING(exec_start);
  2123. } else {
  2124. OUT_RING(MI_BATCH_BUFFER_START |
  2125. (2 << 6));
  2126. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  2127. }
  2128. ADVANCE_LP_RING();
  2129. }
  2130. }
  2131. /* XXX breadcrumb */
  2132. return 0;
  2133. }
  2134. /* Throttle our rendering by waiting until the ring has completed our requests
  2135. * emitted over 20 msec ago.
  2136. *
  2137. * This should get us reasonable parallelism between CPU and GPU but also
  2138. * relatively low latency when blocking on a particular request to finish.
  2139. */
  2140. static int
  2141. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  2142. {
  2143. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  2144. int ret = 0;
  2145. uint32_t seqno;
  2146. mutex_lock(&dev->struct_mutex);
  2147. seqno = i915_file_priv->mm.last_gem_throttle_seqno;
  2148. i915_file_priv->mm.last_gem_throttle_seqno =
  2149. i915_file_priv->mm.last_gem_seqno;
  2150. if (seqno)
  2151. ret = i915_wait_request(dev, seqno);
  2152. mutex_unlock(&dev->struct_mutex);
  2153. return ret;
  2154. }
  2155. int
  2156. i915_gem_execbuffer(struct drm_device *dev, void *data,
  2157. struct drm_file *file_priv)
  2158. {
  2159. drm_i915_private_t *dev_priv = dev->dev_private;
  2160. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  2161. struct drm_i915_gem_execbuffer *args = data;
  2162. struct drm_i915_gem_exec_object *exec_list = NULL;
  2163. struct drm_gem_object **object_list = NULL;
  2164. struct drm_gem_object *batch_obj;
  2165. struct drm_i915_gem_object *obj_priv;
  2166. int ret, i, pinned = 0;
  2167. uint64_t exec_offset;
  2168. uint32_t seqno, flush_domains;
  2169. int pin_tries;
  2170. #if WATCH_EXEC
  2171. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  2172. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  2173. #endif
  2174. if (args->buffer_count < 1) {
  2175. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  2176. return -EINVAL;
  2177. }
  2178. /* Copy in the exec list from userland */
  2179. exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
  2180. DRM_MEM_DRIVER);
  2181. object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
  2182. DRM_MEM_DRIVER);
  2183. if (exec_list == NULL || object_list == NULL) {
  2184. DRM_ERROR("Failed to allocate exec or object list "
  2185. "for %d buffers\n",
  2186. args->buffer_count);
  2187. ret = -ENOMEM;
  2188. goto pre_mutex_err;
  2189. }
  2190. ret = copy_from_user(exec_list,
  2191. (struct drm_i915_relocation_entry __user *)
  2192. (uintptr_t) args->buffers_ptr,
  2193. sizeof(*exec_list) * args->buffer_count);
  2194. if (ret != 0) {
  2195. DRM_ERROR("copy %d exec entries failed %d\n",
  2196. args->buffer_count, ret);
  2197. goto pre_mutex_err;
  2198. }
  2199. mutex_lock(&dev->struct_mutex);
  2200. i915_verify_inactive(dev, __FILE__, __LINE__);
  2201. if (dev_priv->mm.wedged) {
  2202. DRM_ERROR("Execbuf while wedged\n");
  2203. mutex_unlock(&dev->struct_mutex);
  2204. ret = -EIO;
  2205. goto pre_mutex_err;
  2206. }
  2207. if (dev_priv->mm.suspended) {
  2208. DRM_ERROR("Execbuf while VT-switched.\n");
  2209. mutex_unlock(&dev->struct_mutex);
  2210. ret = -EBUSY;
  2211. goto pre_mutex_err;
  2212. }
  2213. /* Look up object handles */
  2214. for (i = 0; i < args->buffer_count; i++) {
  2215. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  2216. exec_list[i].handle);
  2217. if (object_list[i] == NULL) {
  2218. DRM_ERROR("Invalid object handle %d at index %d\n",
  2219. exec_list[i].handle, i);
  2220. ret = -EBADF;
  2221. goto err;
  2222. }
  2223. obj_priv = object_list[i]->driver_private;
  2224. if (obj_priv->in_execbuffer) {
  2225. DRM_ERROR("Object %p appears more than once in object list\n",
  2226. object_list[i]);
  2227. ret = -EBADF;
  2228. goto err;
  2229. }
  2230. obj_priv->in_execbuffer = true;
  2231. }
  2232. /* Pin and relocate */
  2233. for (pin_tries = 0; ; pin_tries++) {
  2234. ret = 0;
  2235. for (i = 0; i < args->buffer_count; i++) {
  2236. object_list[i]->pending_read_domains = 0;
  2237. object_list[i]->pending_write_domain = 0;
  2238. ret = i915_gem_object_pin_and_relocate(object_list[i],
  2239. file_priv,
  2240. &exec_list[i]);
  2241. if (ret)
  2242. break;
  2243. pinned = i + 1;
  2244. }
  2245. /* success */
  2246. if (ret == 0)
  2247. break;
  2248. /* error other than GTT full, or we've already tried again */
  2249. if (ret != -ENOMEM || pin_tries >= 1) {
  2250. if (ret != -ERESTARTSYS)
  2251. DRM_ERROR("Failed to pin buffers %d\n", ret);
  2252. goto err;
  2253. }
  2254. /* unpin all of our buffers */
  2255. for (i = 0; i < pinned; i++)
  2256. i915_gem_object_unpin(object_list[i]);
  2257. pinned = 0;
  2258. /* evict everyone we can from the aperture */
  2259. ret = i915_gem_evict_everything(dev);
  2260. if (ret)
  2261. goto err;
  2262. }
  2263. /* Set the pending read domains for the batch buffer to COMMAND */
  2264. batch_obj = object_list[args->buffer_count-1];
  2265. batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  2266. batch_obj->pending_write_domain = 0;
  2267. i915_verify_inactive(dev, __FILE__, __LINE__);
  2268. /* Zero the global flush/invalidate flags. These
  2269. * will be modified as new domains are computed
  2270. * for each object
  2271. */
  2272. dev->invalidate_domains = 0;
  2273. dev->flush_domains = 0;
  2274. for (i = 0; i < args->buffer_count; i++) {
  2275. struct drm_gem_object *obj = object_list[i];
  2276. /* Compute new gpu domains and update invalidate/flush */
  2277. i915_gem_object_set_to_gpu_domain(obj);
  2278. }
  2279. i915_verify_inactive(dev, __FILE__, __LINE__);
  2280. if (dev->invalidate_domains | dev->flush_domains) {
  2281. #if WATCH_EXEC
  2282. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  2283. __func__,
  2284. dev->invalidate_domains,
  2285. dev->flush_domains);
  2286. #endif
  2287. i915_gem_flush(dev,
  2288. dev->invalidate_domains,
  2289. dev->flush_domains);
  2290. if (dev->flush_domains)
  2291. (void)i915_add_request(dev, dev->flush_domains);
  2292. }
  2293. for (i = 0; i < args->buffer_count; i++) {
  2294. struct drm_gem_object *obj = object_list[i];
  2295. obj->write_domain = obj->pending_write_domain;
  2296. }
  2297. i915_verify_inactive(dev, __FILE__, __LINE__);
  2298. #if WATCH_COHERENCY
  2299. for (i = 0; i < args->buffer_count; i++) {
  2300. i915_gem_object_check_coherency(object_list[i],
  2301. exec_list[i].handle);
  2302. }
  2303. #endif
  2304. exec_offset = exec_list[args->buffer_count - 1].offset;
  2305. #if WATCH_EXEC
  2306. i915_gem_dump_object(object_list[args->buffer_count - 1],
  2307. args->batch_len,
  2308. __func__,
  2309. ~0);
  2310. #endif
  2311. /* Exec the batchbuffer */
  2312. ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
  2313. if (ret) {
  2314. DRM_ERROR("dispatch failed %d\n", ret);
  2315. goto err;
  2316. }
  2317. /*
  2318. * Ensure that the commands in the batch buffer are
  2319. * finished before the interrupt fires
  2320. */
  2321. flush_domains = i915_retire_commands(dev);
  2322. i915_verify_inactive(dev, __FILE__, __LINE__);
  2323. /*
  2324. * Get a seqno representing the execution of the current buffer,
  2325. * which we can wait on. We would like to mitigate these interrupts,
  2326. * likely by only creating seqnos occasionally (so that we have
  2327. * *some* interrupts representing completion of buffers that we can
  2328. * wait on when trying to clear up gtt space).
  2329. */
  2330. seqno = i915_add_request(dev, flush_domains);
  2331. BUG_ON(seqno == 0);
  2332. i915_file_priv->mm.last_gem_seqno = seqno;
  2333. for (i = 0; i < args->buffer_count; i++) {
  2334. struct drm_gem_object *obj = object_list[i];
  2335. i915_gem_object_move_to_active(obj, seqno);
  2336. #if WATCH_LRU
  2337. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  2338. #endif
  2339. }
  2340. #if WATCH_LRU
  2341. i915_dump_lru(dev, __func__);
  2342. #endif
  2343. i915_verify_inactive(dev, __FILE__, __LINE__);
  2344. err:
  2345. for (i = 0; i < pinned; i++)
  2346. i915_gem_object_unpin(object_list[i]);
  2347. for (i = 0; i < args->buffer_count; i++) {
  2348. if (object_list[i]) {
  2349. obj_priv = object_list[i]->driver_private;
  2350. obj_priv->in_execbuffer = false;
  2351. }
  2352. drm_gem_object_unreference(object_list[i]);
  2353. }
  2354. mutex_unlock(&dev->struct_mutex);
  2355. if (!ret) {
  2356. /* Copy the new buffer offsets back to the user's exec list. */
  2357. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  2358. (uintptr_t) args->buffers_ptr,
  2359. exec_list,
  2360. sizeof(*exec_list) * args->buffer_count);
  2361. if (ret)
  2362. DRM_ERROR("failed to copy %d exec entries "
  2363. "back to user (%d)\n",
  2364. args->buffer_count, ret);
  2365. }
  2366. pre_mutex_err:
  2367. drm_free(object_list, sizeof(*object_list) * args->buffer_count,
  2368. DRM_MEM_DRIVER);
  2369. drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
  2370. DRM_MEM_DRIVER);
  2371. return ret;
  2372. }
  2373. int
  2374. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  2375. {
  2376. struct drm_device *dev = obj->dev;
  2377. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2378. int ret;
  2379. i915_verify_inactive(dev, __FILE__, __LINE__);
  2380. if (obj_priv->gtt_space == NULL) {
  2381. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  2382. if (ret != 0) {
  2383. if (ret != -EBUSY && ret != -ERESTARTSYS)
  2384. DRM_ERROR("Failure to bind: %d\n", ret);
  2385. return ret;
  2386. }
  2387. }
  2388. /*
  2389. * Pre-965 chips need a fence register set up in order to
  2390. * properly handle tiled surfaces.
  2391. */
  2392. if (!IS_I965G(dev) &&
  2393. obj_priv->fence_reg == I915_FENCE_REG_NONE &&
  2394. obj_priv->tiling_mode != I915_TILING_NONE) {
  2395. ret = i915_gem_object_get_fence_reg(obj, true);
  2396. if (ret != 0) {
  2397. if (ret != -EBUSY && ret != -ERESTARTSYS)
  2398. DRM_ERROR("Failure to install fence: %d\n",
  2399. ret);
  2400. return ret;
  2401. }
  2402. }
  2403. obj_priv->pin_count++;
  2404. /* If the object is not active and not pending a flush,
  2405. * remove it from the inactive list
  2406. */
  2407. if (obj_priv->pin_count == 1) {
  2408. atomic_inc(&dev->pin_count);
  2409. atomic_add(obj->size, &dev->pin_memory);
  2410. if (!obj_priv->active &&
  2411. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  2412. I915_GEM_DOMAIN_GTT)) == 0 &&
  2413. !list_empty(&obj_priv->list))
  2414. list_del_init(&obj_priv->list);
  2415. }
  2416. i915_verify_inactive(dev, __FILE__, __LINE__);
  2417. return 0;
  2418. }
  2419. void
  2420. i915_gem_object_unpin(struct drm_gem_object *obj)
  2421. {
  2422. struct drm_device *dev = obj->dev;
  2423. drm_i915_private_t *dev_priv = dev->dev_private;
  2424. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2425. i915_verify_inactive(dev, __FILE__, __LINE__);
  2426. obj_priv->pin_count--;
  2427. BUG_ON(obj_priv->pin_count < 0);
  2428. BUG_ON(obj_priv->gtt_space == NULL);
  2429. /* If the object is no longer pinned, and is
  2430. * neither active nor being flushed, then stick it on
  2431. * the inactive list
  2432. */
  2433. if (obj_priv->pin_count == 0) {
  2434. if (!obj_priv->active &&
  2435. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  2436. I915_GEM_DOMAIN_GTT)) == 0)
  2437. list_move_tail(&obj_priv->list,
  2438. &dev_priv->mm.inactive_list);
  2439. atomic_dec(&dev->pin_count);
  2440. atomic_sub(obj->size, &dev->pin_memory);
  2441. }
  2442. i915_verify_inactive(dev, __FILE__, __LINE__);
  2443. }
  2444. int
  2445. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  2446. struct drm_file *file_priv)
  2447. {
  2448. struct drm_i915_gem_pin *args = data;
  2449. struct drm_gem_object *obj;
  2450. struct drm_i915_gem_object *obj_priv;
  2451. int ret;
  2452. mutex_lock(&dev->struct_mutex);
  2453. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  2454. if (obj == NULL) {
  2455. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  2456. args->handle);
  2457. mutex_unlock(&dev->struct_mutex);
  2458. return -EBADF;
  2459. }
  2460. obj_priv = obj->driver_private;
  2461. if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
  2462. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  2463. args->handle);
  2464. drm_gem_object_unreference(obj);
  2465. mutex_unlock(&dev->struct_mutex);
  2466. return -EINVAL;
  2467. }
  2468. obj_priv->user_pin_count++;
  2469. obj_priv->pin_filp = file_priv;
  2470. if (obj_priv->user_pin_count == 1) {
  2471. ret = i915_gem_object_pin(obj, args->alignment);
  2472. if (ret != 0) {
  2473. drm_gem_object_unreference(obj);
  2474. mutex_unlock(&dev->struct_mutex);
  2475. return ret;
  2476. }
  2477. }
  2478. /* XXX - flush the CPU caches for pinned objects
  2479. * as the X server doesn't manage domains yet
  2480. */
  2481. i915_gem_object_flush_cpu_write_domain(obj);
  2482. args->offset = obj_priv->gtt_offset;
  2483. drm_gem_object_unreference(obj);
  2484. mutex_unlock(&dev->struct_mutex);
  2485. return 0;
  2486. }
  2487. int
  2488. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  2489. struct drm_file *file_priv)
  2490. {
  2491. struct drm_i915_gem_pin *args = data;
  2492. struct drm_gem_object *obj;
  2493. struct drm_i915_gem_object *obj_priv;
  2494. mutex_lock(&dev->struct_mutex);
  2495. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  2496. if (obj == NULL) {
  2497. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  2498. args->handle);
  2499. mutex_unlock(&dev->struct_mutex);
  2500. return -EBADF;
  2501. }
  2502. obj_priv = obj->driver_private;
  2503. if (obj_priv->pin_filp != file_priv) {
  2504. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  2505. args->handle);
  2506. drm_gem_object_unreference(obj);
  2507. mutex_unlock(&dev->struct_mutex);
  2508. return -EINVAL;
  2509. }
  2510. obj_priv->user_pin_count--;
  2511. if (obj_priv->user_pin_count == 0) {
  2512. obj_priv->pin_filp = NULL;
  2513. i915_gem_object_unpin(obj);
  2514. }
  2515. drm_gem_object_unreference(obj);
  2516. mutex_unlock(&dev->struct_mutex);
  2517. return 0;
  2518. }
  2519. int
  2520. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  2521. struct drm_file *file_priv)
  2522. {
  2523. struct drm_i915_gem_busy *args = data;
  2524. struct drm_gem_object *obj;
  2525. struct drm_i915_gem_object *obj_priv;
  2526. mutex_lock(&dev->struct_mutex);
  2527. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  2528. if (obj == NULL) {
  2529. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  2530. args->handle);
  2531. mutex_unlock(&dev->struct_mutex);
  2532. return -EBADF;
  2533. }
  2534. /* Update the active list for the hardware's current position.
  2535. * Otherwise this only updates on a delayed timer or when irqs are
  2536. * actually unmasked, and our working set ends up being larger than
  2537. * required.
  2538. */
  2539. i915_gem_retire_requests(dev);
  2540. obj_priv = obj->driver_private;
  2541. /* Don't count being on the flushing list against the object being
  2542. * done. Otherwise, a buffer left on the flushing list but not getting
  2543. * flushed (because nobody's flushing that domain) won't ever return
  2544. * unbusy and get reused by libdrm's bo cache. The other expected
  2545. * consumer of this interface, OpenGL's occlusion queries, also specs
  2546. * that the objects get unbusy "eventually" without any interference.
  2547. */
  2548. args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
  2549. drm_gem_object_unreference(obj);
  2550. mutex_unlock(&dev->struct_mutex);
  2551. return 0;
  2552. }
  2553. int
  2554. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  2555. struct drm_file *file_priv)
  2556. {
  2557. return i915_gem_ring_throttle(dev, file_priv);
  2558. }
  2559. int i915_gem_init_object(struct drm_gem_object *obj)
  2560. {
  2561. struct drm_i915_gem_object *obj_priv;
  2562. obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
  2563. if (obj_priv == NULL)
  2564. return -ENOMEM;
  2565. /*
  2566. * We've just allocated pages from the kernel,
  2567. * so they've just been written by the CPU with
  2568. * zeros. They'll need to be clflushed before we
  2569. * use them with the GPU.
  2570. */
  2571. obj->write_domain = I915_GEM_DOMAIN_CPU;
  2572. obj->read_domains = I915_GEM_DOMAIN_CPU;
  2573. obj_priv->agp_type = AGP_USER_MEMORY;
  2574. obj->driver_private = obj_priv;
  2575. obj_priv->obj = obj;
  2576. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  2577. INIT_LIST_HEAD(&obj_priv->list);
  2578. return 0;
  2579. }
  2580. void i915_gem_free_object(struct drm_gem_object *obj)
  2581. {
  2582. struct drm_device *dev = obj->dev;
  2583. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2584. while (obj_priv->pin_count > 0)
  2585. i915_gem_object_unpin(obj);
  2586. if (obj_priv->phys_obj)
  2587. i915_gem_detach_phys_object(dev, obj);
  2588. i915_gem_object_unbind(obj);
  2589. i915_gem_free_mmap_offset(obj);
  2590. drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
  2591. drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
  2592. }
  2593. /** Unbinds all objects that are on the given buffer list. */
  2594. static int
  2595. i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
  2596. {
  2597. struct drm_gem_object *obj;
  2598. struct drm_i915_gem_object *obj_priv;
  2599. int ret;
  2600. while (!list_empty(head)) {
  2601. obj_priv = list_first_entry(head,
  2602. struct drm_i915_gem_object,
  2603. list);
  2604. obj = obj_priv->obj;
  2605. if (obj_priv->pin_count != 0) {
  2606. DRM_ERROR("Pinned object in unbind list\n");
  2607. mutex_unlock(&dev->struct_mutex);
  2608. return -EINVAL;
  2609. }
  2610. ret = i915_gem_object_unbind(obj);
  2611. if (ret != 0) {
  2612. DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
  2613. ret);
  2614. mutex_unlock(&dev->struct_mutex);
  2615. return ret;
  2616. }
  2617. }
  2618. return 0;
  2619. }
  2620. int
  2621. i915_gem_idle(struct drm_device *dev)
  2622. {
  2623. drm_i915_private_t *dev_priv = dev->dev_private;
  2624. uint32_t seqno, cur_seqno, last_seqno;
  2625. int stuck, ret;
  2626. mutex_lock(&dev->struct_mutex);
  2627. if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
  2628. mutex_unlock(&dev->struct_mutex);
  2629. return 0;
  2630. }
  2631. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  2632. * We need to replace this with a semaphore, or something.
  2633. */
  2634. dev_priv->mm.suspended = 1;
  2635. /* Cancel the retire work handler, wait for it to finish if running
  2636. */
  2637. mutex_unlock(&dev->struct_mutex);
  2638. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  2639. mutex_lock(&dev->struct_mutex);
  2640. i915_kernel_lost_context(dev);
  2641. /* Flush the GPU along with all non-CPU write domains
  2642. */
  2643. i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
  2644. ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  2645. seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU);
  2646. if (seqno == 0) {
  2647. mutex_unlock(&dev->struct_mutex);
  2648. return -ENOMEM;
  2649. }
  2650. dev_priv->mm.waiting_gem_seqno = seqno;
  2651. last_seqno = 0;
  2652. stuck = 0;
  2653. for (;;) {
  2654. cur_seqno = i915_get_gem_seqno(dev);
  2655. if (i915_seqno_passed(cur_seqno, seqno))
  2656. break;
  2657. if (last_seqno == cur_seqno) {
  2658. if (stuck++ > 100) {
  2659. DRM_ERROR("hardware wedged\n");
  2660. dev_priv->mm.wedged = 1;
  2661. DRM_WAKEUP(&dev_priv->irq_queue);
  2662. break;
  2663. }
  2664. }
  2665. msleep(10);
  2666. last_seqno = cur_seqno;
  2667. }
  2668. dev_priv->mm.waiting_gem_seqno = 0;
  2669. i915_gem_retire_requests(dev);
  2670. if (!dev_priv->mm.wedged) {
  2671. /* Active and flushing should now be empty as we've
  2672. * waited for a sequence higher than any pending execbuffer
  2673. */
  2674. WARN_ON(!list_empty(&dev_priv->mm.active_list));
  2675. WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
  2676. /* Request should now be empty as we've also waited
  2677. * for the last request in the list
  2678. */
  2679. WARN_ON(!list_empty(&dev_priv->mm.request_list));
  2680. }
  2681. /* Empty the active and flushing lists to inactive. If there's
  2682. * anything left at this point, it means that we're wedged and
  2683. * nothing good's going to happen by leaving them there. So strip
  2684. * the GPU domains and just stuff them onto inactive.
  2685. */
  2686. while (!list_empty(&dev_priv->mm.active_list)) {
  2687. struct drm_i915_gem_object *obj_priv;
  2688. obj_priv = list_first_entry(&dev_priv->mm.active_list,
  2689. struct drm_i915_gem_object,
  2690. list);
  2691. obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
  2692. i915_gem_object_move_to_inactive(obj_priv->obj);
  2693. }
  2694. while (!list_empty(&dev_priv->mm.flushing_list)) {
  2695. struct drm_i915_gem_object *obj_priv;
  2696. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  2697. struct drm_i915_gem_object,
  2698. list);
  2699. obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
  2700. i915_gem_object_move_to_inactive(obj_priv->obj);
  2701. }
  2702. /* Move all inactive buffers out of the GTT. */
  2703. ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
  2704. WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
  2705. if (ret) {
  2706. mutex_unlock(&dev->struct_mutex);
  2707. return ret;
  2708. }
  2709. i915_gem_cleanup_ringbuffer(dev);
  2710. mutex_unlock(&dev->struct_mutex);
  2711. return 0;
  2712. }
  2713. static int
  2714. i915_gem_init_hws(struct drm_device *dev)
  2715. {
  2716. drm_i915_private_t *dev_priv = dev->dev_private;
  2717. struct drm_gem_object *obj;
  2718. struct drm_i915_gem_object *obj_priv;
  2719. int ret;
  2720. /* If we need a physical address for the status page, it's already
  2721. * initialized at driver load time.
  2722. */
  2723. if (!I915_NEED_GFX_HWS(dev))
  2724. return 0;
  2725. obj = drm_gem_object_alloc(dev, 4096);
  2726. if (obj == NULL) {
  2727. DRM_ERROR("Failed to allocate status page\n");
  2728. return -ENOMEM;
  2729. }
  2730. obj_priv = obj->driver_private;
  2731. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  2732. ret = i915_gem_object_pin(obj, 4096);
  2733. if (ret != 0) {
  2734. drm_gem_object_unreference(obj);
  2735. return ret;
  2736. }
  2737. dev_priv->status_gfx_addr = obj_priv->gtt_offset;
  2738. dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
  2739. if (dev_priv->hw_status_page == NULL) {
  2740. DRM_ERROR("Failed to map status page.\n");
  2741. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2742. i915_gem_object_unpin(obj);
  2743. drm_gem_object_unreference(obj);
  2744. return -EINVAL;
  2745. }
  2746. dev_priv->hws_obj = obj;
  2747. memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
  2748. I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
  2749. I915_READ(HWS_PGA); /* posting read */
  2750. DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
  2751. return 0;
  2752. }
  2753. static void
  2754. i915_gem_cleanup_hws(struct drm_device *dev)
  2755. {
  2756. drm_i915_private_t *dev_priv = dev->dev_private;
  2757. struct drm_gem_object *obj;
  2758. struct drm_i915_gem_object *obj_priv;
  2759. if (dev_priv->hws_obj == NULL)
  2760. return;
  2761. obj = dev_priv->hws_obj;
  2762. obj_priv = obj->driver_private;
  2763. kunmap(obj_priv->page_list[0]);
  2764. i915_gem_object_unpin(obj);
  2765. drm_gem_object_unreference(obj);
  2766. dev_priv->hws_obj = NULL;
  2767. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2768. dev_priv->hw_status_page = NULL;
  2769. /* Write high address into HWS_PGA when disabling. */
  2770. I915_WRITE(HWS_PGA, 0x1ffff000);
  2771. }
  2772. int
  2773. i915_gem_init_ringbuffer(struct drm_device *dev)
  2774. {
  2775. drm_i915_private_t *dev_priv = dev->dev_private;
  2776. struct drm_gem_object *obj;
  2777. struct drm_i915_gem_object *obj_priv;
  2778. drm_i915_ring_buffer_t *ring = &dev_priv->ring;
  2779. int ret;
  2780. u32 head;
  2781. ret = i915_gem_init_hws(dev);
  2782. if (ret != 0)
  2783. return ret;
  2784. obj = drm_gem_object_alloc(dev, 128 * 1024);
  2785. if (obj == NULL) {
  2786. DRM_ERROR("Failed to allocate ringbuffer\n");
  2787. i915_gem_cleanup_hws(dev);
  2788. return -ENOMEM;
  2789. }
  2790. obj_priv = obj->driver_private;
  2791. ret = i915_gem_object_pin(obj, 4096);
  2792. if (ret != 0) {
  2793. drm_gem_object_unreference(obj);
  2794. i915_gem_cleanup_hws(dev);
  2795. return ret;
  2796. }
  2797. /* Set up the kernel mapping for the ring. */
  2798. ring->Size = obj->size;
  2799. ring->tail_mask = obj->size - 1;
  2800. ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
  2801. ring->map.size = obj->size;
  2802. ring->map.type = 0;
  2803. ring->map.flags = 0;
  2804. ring->map.mtrr = 0;
  2805. drm_core_ioremap_wc(&ring->map, dev);
  2806. if (ring->map.handle == NULL) {
  2807. DRM_ERROR("Failed to map ringbuffer.\n");
  2808. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2809. i915_gem_object_unpin(obj);
  2810. drm_gem_object_unreference(obj);
  2811. i915_gem_cleanup_hws(dev);
  2812. return -EINVAL;
  2813. }
  2814. ring->ring_obj = obj;
  2815. ring->virtual_start = ring->map.handle;
  2816. /* Stop the ring if it's running. */
  2817. I915_WRITE(PRB0_CTL, 0);
  2818. I915_WRITE(PRB0_TAIL, 0);
  2819. I915_WRITE(PRB0_HEAD, 0);
  2820. /* Initialize the ring. */
  2821. I915_WRITE(PRB0_START, obj_priv->gtt_offset);
  2822. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2823. /* G45 ring initialization fails to reset head to zero */
  2824. if (head != 0) {
  2825. DRM_ERROR("Ring head not reset to zero "
  2826. "ctl %08x head %08x tail %08x start %08x\n",
  2827. I915_READ(PRB0_CTL),
  2828. I915_READ(PRB0_HEAD),
  2829. I915_READ(PRB0_TAIL),
  2830. I915_READ(PRB0_START));
  2831. I915_WRITE(PRB0_HEAD, 0);
  2832. DRM_ERROR("Ring head forced to zero "
  2833. "ctl %08x head %08x tail %08x start %08x\n",
  2834. I915_READ(PRB0_CTL),
  2835. I915_READ(PRB0_HEAD),
  2836. I915_READ(PRB0_TAIL),
  2837. I915_READ(PRB0_START));
  2838. }
  2839. I915_WRITE(PRB0_CTL,
  2840. ((obj->size - 4096) & RING_NR_PAGES) |
  2841. RING_NO_REPORT |
  2842. RING_VALID);
  2843. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2844. /* If the head is still not zero, the ring is dead */
  2845. if (head != 0) {
  2846. DRM_ERROR("Ring initialization failed "
  2847. "ctl %08x head %08x tail %08x start %08x\n",
  2848. I915_READ(PRB0_CTL),
  2849. I915_READ(PRB0_HEAD),
  2850. I915_READ(PRB0_TAIL),
  2851. I915_READ(PRB0_START));
  2852. return -EIO;
  2853. }
  2854. /* Update our cache of the ring state */
  2855. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2856. i915_kernel_lost_context(dev);
  2857. else {
  2858. ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2859. ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
  2860. ring->space = ring->head - (ring->tail + 8);
  2861. if (ring->space < 0)
  2862. ring->space += ring->Size;
  2863. }
  2864. return 0;
  2865. }
  2866. void
  2867. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  2868. {
  2869. drm_i915_private_t *dev_priv = dev->dev_private;
  2870. if (dev_priv->ring.ring_obj == NULL)
  2871. return;
  2872. drm_core_ioremapfree(&dev_priv->ring.map, dev);
  2873. i915_gem_object_unpin(dev_priv->ring.ring_obj);
  2874. drm_gem_object_unreference(dev_priv->ring.ring_obj);
  2875. dev_priv->ring.ring_obj = NULL;
  2876. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2877. i915_gem_cleanup_hws(dev);
  2878. }
  2879. int
  2880. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  2881. struct drm_file *file_priv)
  2882. {
  2883. drm_i915_private_t *dev_priv = dev->dev_private;
  2884. int ret;
  2885. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2886. return 0;
  2887. if (dev_priv->mm.wedged) {
  2888. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  2889. dev_priv->mm.wedged = 0;
  2890. }
  2891. mutex_lock(&dev->struct_mutex);
  2892. dev_priv->mm.suspended = 0;
  2893. ret = i915_gem_init_ringbuffer(dev);
  2894. if (ret != 0)
  2895. return ret;
  2896. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  2897. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  2898. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  2899. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  2900. mutex_unlock(&dev->struct_mutex);
  2901. drm_irq_install(dev);
  2902. return 0;
  2903. }
  2904. int
  2905. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  2906. struct drm_file *file_priv)
  2907. {
  2908. int ret;
  2909. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2910. return 0;
  2911. ret = i915_gem_idle(dev);
  2912. drm_irq_uninstall(dev);
  2913. return ret;
  2914. }
  2915. void
  2916. i915_gem_lastclose(struct drm_device *dev)
  2917. {
  2918. int ret;
  2919. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2920. return;
  2921. ret = i915_gem_idle(dev);
  2922. if (ret)
  2923. DRM_ERROR("failed to idle hardware: %d\n", ret);
  2924. }
  2925. void
  2926. i915_gem_load(struct drm_device *dev)
  2927. {
  2928. drm_i915_private_t *dev_priv = dev->dev_private;
  2929. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  2930. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  2931. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  2932. INIT_LIST_HEAD(&dev_priv->mm.request_list);
  2933. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  2934. i915_gem_retire_work_handler);
  2935. dev_priv->mm.next_gem_seqno = 1;
  2936. /* Old X drivers will take 0-2 for front, back, depth buffers */
  2937. dev_priv->fence_reg_start = 3;
  2938. if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  2939. dev_priv->num_fence_regs = 16;
  2940. else
  2941. dev_priv->num_fence_regs = 8;
  2942. i915_gem_detect_bit_6_swizzle(dev);
  2943. }
  2944. /*
  2945. * Create a physically contiguous memory object for this object
  2946. * e.g. for cursor + overlay regs
  2947. */
  2948. int i915_gem_init_phys_object(struct drm_device *dev,
  2949. int id, int size)
  2950. {
  2951. drm_i915_private_t *dev_priv = dev->dev_private;
  2952. struct drm_i915_gem_phys_object *phys_obj;
  2953. int ret;
  2954. if (dev_priv->mm.phys_objs[id - 1] || !size)
  2955. return 0;
  2956. phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
  2957. if (!phys_obj)
  2958. return -ENOMEM;
  2959. phys_obj->id = id;
  2960. phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff);
  2961. if (!phys_obj->handle) {
  2962. ret = -ENOMEM;
  2963. goto kfree_obj;
  2964. }
  2965. #ifdef CONFIG_X86
  2966. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  2967. #endif
  2968. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  2969. return 0;
  2970. kfree_obj:
  2971. drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER);
  2972. return ret;
  2973. }
  2974. void i915_gem_free_phys_object(struct drm_device *dev, int id)
  2975. {
  2976. drm_i915_private_t *dev_priv = dev->dev_private;
  2977. struct drm_i915_gem_phys_object *phys_obj;
  2978. if (!dev_priv->mm.phys_objs[id - 1])
  2979. return;
  2980. phys_obj = dev_priv->mm.phys_objs[id - 1];
  2981. if (phys_obj->cur_obj) {
  2982. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  2983. }
  2984. #ifdef CONFIG_X86
  2985. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  2986. #endif
  2987. drm_pci_free(dev, phys_obj->handle);
  2988. kfree(phys_obj);
  2989. dev_priv->mm.phys_objs[id - 1] = NULL;
  2990. }
  2991. void i915_gem_free_all_phys_object(struct drm_device *dev)
  2992. {
  2993. int i;
  2994. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  2995. i915_gem_free_phys_object(dev, i);
  2996. }
  2997. void i915_gem_detach_phys_object(struct drm_device *dev,
  2998. struct drm_gem_object *obj)
  2999. {
  3000. struct drm_i915_gem_object *obj_priv;
  3001. int i;
  3002. int ret;
  3003. int page_count;
  3004. obj_priv = obj->driver_private;
  3005. if (!obj_priv->phys_obj)
  3006. return;
  3007. ret = i915_gem_object_get_page_list(obj);
  3008. if (ret)
  3009. goto out;
  3010. page_count = obj->size / PAGE_SIZE;
  3011. for (i = 0; i < page_count; i++) {
  3012. char *dst = kmap_atomic(obj_priv->page_list[i], KM_USER0);
  3013. char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3014. memcpy(dst, src, PAGE_SIZE);
  3015. kunmap_atomic(dst, KM_USER0);
  3016. }
  3017. drm_clflush_pages(obj_priv->page_list, page_count);
  3018. drm_agp_chipset_flush(dev);
  3019. out:
  3020. obj_priv->phys_obj->cur_obj = NULL;
  3021. obj_priv->phys_obj = NULL;
  3022. }
  3023. int
  3024. i915_gem_attach_phys_object(struct drm_device *dev,
  3025. struct drm_gem_object *obj, int id)
  3026. {
  3027. drm_i915_private_t *dev_priv = dev->dev_private;
  3028. struct drm_i915_gem_object *obj_priv;
  3029. int ret = 0;
  3030. int page_count;
  3031. int i;
  3032. if (id > I915_MAX_PHYS_OBJECT)
  3033. return -EINVAL;
  3034. obj_priv = obj->driver_private;
  3035. if (obj_priv->phys_obj) {
  3036. if (obj_priv->phys_obj->id == id)
  3037. return 0;
  3038. i915_gem_detach_phys_object(dev, obj);
  3039. }
  3040. /* create a new object */
  3041. if (!dev_priv->mm.phys_objs[id - 1]) {
  3042. ret = i915_gem_init_phys_object(dev, id,
  3043. obj->size);
  3044. if (ret) {
  3045. DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
  3046. goto out;
  3047. }
  3048. }
  3049. /* bind to the object */
  3050. obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3051. obj_priv->phys_obj->cur_obj = obj;
  3052. ret = i915_gem_object_get_page_list(obj);
  3053. if (ret) {
  3054. DRM_ERROR("failed to get page list\n");
  3055. goto out;
  3056. }
  3057. page_count = obj->size / PAGE_SIZE;
  3058. for (i = 0; i < page_count; i++) {
  3059. char *src = kmap_atomic(obj_priv->page_list[i], KM_USER0);
  3060. char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3061. memcpy(dst, src, PAGE_SIZE);
  3062. kunmap_atomic(src, KM_USER0);
  3063. }
  3064. return 0;
  3065. out:
  3066. return ret;
  3067. }
  3068. static int
  3069. i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  3070. struct drm_i915_gem_pwrite *args,
  3071. struct drm_file *file_priv)
  3072. {
  3073. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  3074. void *obj_addr;
  3075. int ret;
  3076. char __user *user_data;
  3077. user_data = (char __user *) (uintptr_t) args->data_ptr;
  3078. obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
  3079. DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size);
  3080. ret = copy_from_user(obj_addr, user_data, args->size);
  3081. if (ret)
  3082. return -EFAULT;
  3083. drm_agp_chipset_flush(dev);
  3084. return 0;
  3085. }