i915_gem.c 93 KB

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