i915_gem.c 91 KB

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