i915_gem.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742
  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. #define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
  33. static void
  34. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
  35. uint32_t read_domains,
  36. uint32_t write_domain);
  37. static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
  38. static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
  39. static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
  40. static int i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj,
  41. int write);
  42. static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
  43. int write);
  44. static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  45. uint64_t offset,
  46. uint64_t size);
  47. static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
  48. static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
  49. static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
  50. static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
  51. static void
  52. i915_gem_cleanup_ringbuffer(struct drm_device *dev);
  53. int
  54. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  55. struct drm_file *file_priv)
  56. {
  57. drm_i915_private_t *dev_priv = dev->dev_private;
  58. struct drm_i915_gem_init *args = data;
  59. mutex_lock(&dev->struct_mutex);
  60. if (args->gtt_start >= args->gtt_end ||
  61. (args->gtt_start & (PAGE_SIZE - 1)) != 0 ||
  62. (args->gtt_end & (PAGE_SIZE - 1)) != 0) {
  63. mutex_unlock(&dev->struct_mutex);
  64. return -EINVAL;
  65. }
  66. drm_mm_init(&dev_priv->mm.gtt_space, args->gtt_start,
  67. args->gtt_end - args->gtt_start);
  68. dev->gtt_total = (uint32_t) (args->gtt_end - args->gtt_start);
  69. mutex_unlock(&dev->struct_mutex);
  70. return 0;
  71. }
  72. int
  73. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  74. struct drm_file *file_priv)
  75. {
  76. struct drm_i915_gem_get_aperture *args = data;
  77. if (!(dev->driver->driver_features & DRIVER_GEM))
  78. return -ENODEV;
  79. args->aper_size = dev->gtt_total;
  80. args->aper_available_size = (args->aper_size -
  81. atomic_read(&dev->pin_memory));
  82. return 0;
  83. }
  84. /**
  85. * Creates a new mm object and returns a handle to it.
  86. */
  87. int
  88. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  89. struct drm_file *file_priv)
  90. {
  91. struct drm_i915_gem_create *args = data;
  92. struct drm_gem_object *obj;
  93. int handle, ret;
  94. args->size = roundup(args->size, PAGE_SIZE);
  95. /* Allocate the new object */
  96. obj = drm_gem_object_alloc(dev, args->size);
  97. if (obj == NULL)
  98. return -ENOMEM;
  99. ret = drm_gem_handle_create(file_priv, obj, &handle);
  100. mutex_lock(&dev->struct_mutex);
  101. drm_gem_object_handle_unreference(obj);
  102. mutex_unlock(&dev->struct_mutex);
  103. if (ret)
  104. return ret;
  105. args->handle = handle;
  106. return 0;
  107. }
  108. /**
  109. * Reads data from the object referenced by handle.
  110. *
  111. * On error, the contents of *data are undefined.
  112. */
  113. int
  114. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  115. struct drm_file *file_priv)
  116. {
  117. struct drm_i915_gem_pread *args = data;
  118. struct drm_gem_object *obj;
  119. struct drm_i915_gem_object *obj_priv;
  120. ssize_t read;
  121. loff_t offset;
  122. int ret;
  123. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  124. if (obj == NULL)
  125. return -EBADF;
  126. obj_priv = obj->driver_private;
  127. /* Bounds check source.
  128. *
  129. * XXX: This could use review for overflow issues...
  130. */
  131. if (args->offset > obj->size || args->size > obj->size ||
  132. args->offset + args->size > obj->size) {
  133. drm_gem_object_unreference(obj);
  134. return -EINVAL;
  135. }
  136. mutex_lock(&dev->struct_mutex);
  137. ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
  138. args->size);
  139. if (ret != 0) {
  140. drm_gem_object_unreference(obj);
  141. mutex_unlock(&dev->struct_mutex);
  142. return ret;
  143. }
  144. offset = args->offset;
  145. read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
  146. args->size, &offset);
  147. if (read != args->size) {
  148. drm_gem_object_unreference(obj);
  149. mutex_unlock(&dev->struct_mutex);
  150. if (read < 0)
  151. return read;
  152. else
  153. return -EINVAL;
  154. }
  155. drm_gem_object_unreference(obj);
  156. mutex_unlock(&dev->struct_mutex);
  157. return 0;
  158. }
  159. /* This is the fast write path which cannot handle
  160. * page faults in the source data
  161. */
  162. static inline int
  163. fast_user_write(struct io_mapping *mapping,
  164. loff_t page_base, int page_offset,
  165. char __user *user_data,
  166. int length)
  167. {
  168. char *vaddr_atomic;
  169. unsigned long unwritten;
  170. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  171. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
  172. user_data, length);
  173. io_mapping_unmap_atomic(vaddr_atomic);
  174. if (unwritten)
  175. return -EFAULT;
  176. return 0;
  177. }
  178. /* Here's the write path which can sleep for
  179. * page faults
  180. */
  181. static inline int
  182. slow_user_write(struct io_mapping *mapping,
  183. loff_t page_base, int page_offset,
  184. char __user *user_data,
  185. int length)
  186. {
  187. char __iomem *vaddr;
  188. unsigned long unwritten;
  189. vaddr = io_mapping_map_wc(mapping, page_base);
  190. if (vaddr == NULL)
  191. return -EFAULT;
  192. unwritten = __copy_from_user(vaddr + page_offset,
  193. user_data, length);
  194. io_mapping_unmap(vaddr);
  195. if (unwritten)
  196. return -EFAULT;
  197. return 0;
  198. }
  199. static int
  200. i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  201. struct drm_i915_gem_pwrite *args,
  202. struct drm_file *file_priv)
  203. {
  204. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  205. drm_i915_private_t *dev_priv = dev->dev_private;
  206. ssize_t remain;
  207. loff_t offset, page_base;
  208. char __user *user_data;
  209. int page_offset, page_length;
  210. int ret;
  211. user_data = (char __user *) (uintptr_t) args->data_ptr;
  212. remain = args->size;
  213. if (!access_ok(VERIFY_READ, user_data, remain))
  214. return -EFAULT;
  215. mutex_lock(&dev->struct_mutex);
  216. ret = i915_gem_object_pin(obj, 0);
  217. if (ret) {
  218. mutex_unlock(&dev->struct_mutex);
  219. return ret;
  220. }
  221. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  222. if (ret)
  223. goto fail;
  224. obj_priv = obj->driver_private;
  225. offset = obj_priv->gtt_offset + args->offset;
  226. obj_priv->dirty = 1;
  227. while (remain > 0) {
  228. /* Operation in this page
  229. *
  230. * page_base = page offset within aperture
  231. * page_offset = offset within page
  232. * page_length = bytes to copy for this page
  233. */
  234. page_base = (offset & ~(PAGE_SIZE-1));
  235. page_offset = offset & (PAGE_SIZE-1);
  236. page_length = remain;
  237. if ((page_offset + remain) > PAGE_SIZE)
  238. page_length = PAGE_SIZE - page_offset;
  239. ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
  240. page_offset, user_data, page_length);
  241. /* If we get a fault while copying data, then (presumably) our
  242. * source page isn't available. In this case, use the
  243. * non-atomic function
  244. */
  245. if (ret) {
  246. ret = slow_user_write (dev_priv->mm.gtt_mapping,
  247. page_base, page_offset,
  248. user_data, page_length);
  249. if (ret)
  250. goto fail;
  251. }
  252. remain -= page_length;
  253. user_data += page_length;
  254. offset += page_length;
  255. }
  256. fail:
  257. i915_gem_object_unpin(obj);
  258. mutex_unlock(&dev->struct_mutex);
  259. return ret;
  260. }
  261. static int
  262. i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  263. struct drm_i915_gem_pwrite *args,
  264. struct drm_file *file_priv)
  265. {
  266. int ret;
  267. loff_t offset;
  268. ssize_t written;
  269. mutex_lock(&dev->struct_mutex);
  270. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  271. if (ret) {
  272. mutex_unlock(&dev->struct_mutex);
  273. return ret;
  274. }
  275. offset = args->offset;
  276. written = vfs_write(obj->filp,
  277. (char __user *)(uintptr_t) args->data_ptr,
  278. args->size, &offset);
  279. if (written != args->size) {
  280. mutex_unlock(&dev->struct_mutex);
  281. if (written < 0)
  282. return written;
  283. else
  284. return -EINVAL;
  285. }
  286. mutex_unlock(&dev->struct_mutex);
  287. return 0;
  288. }
  289. /**
  290. * Writes data to the object referenced by handle.
  291. *
  292. * On error, the contents of the buffer that were to be modified are undefined.
  293. */
  294. int
  295. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  296. struct drm_file *file_priv)
  297. {
  298. struct drm_i915_gem_pwrite *args = data;
  299. struct drm_gem_object *obj;
  300. struct drm_i915_gem_object *obj_priv;
  301. int ret = 0;
  302. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  303. if (obj == NULL)
  304. return -EBADF;
  305. obj_priv = obj->driver_private;
  306. /* Bounds check destination.
  307. *
  308. * XXX: This could use review for overflow issues...
  309. */
  310. if (args->offset > obj->size || args->size > obj->size ||
  311. args->offset + args->size > obj->size) {
  312. drm_gem_object_unreference(obj);
  313. return -EINVAL;
  314. }
  315. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  316. * it would end up going through the fenced access, and we'll get
  317. * different detiling behavior between reading and writing.
  318. * pread/pwrite currently are reading and writing from the CPU
  319. * perspective, requiring manual detiling by the client.
  320. */
  321. if (obj_priv->tiling_mode == I915_TILING_NONE &&
  322. dev->gtt_total != 0)
  323. ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
  324. else
  325. ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
  326. #if WATCH_PWRITE
  327. if (ret)
  328. DRM_INFO("pwrite failed %d\n", ret);
  329. #endif
  330. drm_gem_object_unreference(obj);
  331. return ret;
  332. }
  333. /**
  334. * Called when user space prepares to use an object with the CPU, either
  335. * through the mmap ioctl's mapping or a GTT mapping.
  336. */
  337. int
  338. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  339. struct drm_file *file_priv)
  340. {
  341. struct drm_i915_gem_set_domain *args = data;
  342. struct drm_gem_object *obj;
  343. uint32_t read_domains = args->read_domains;
  344. uint32_t write_domain = args->write_domain;
  345. int ret;
  346. if (!(dev->driver->driver_features & DRIVER_GEM))
  347. return -ENODEV;
  348. /* Only handle setting domains to types used by the CPU. */
  349. if (write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
  350. return -EINVAL;
  351. if (read_domains & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT))
  352. return -EINVAL;
  353. /* Having something in the write domain implies it's in the read
  354. * domain, and only that read domain. Enforce that in the request.
  355. */
  356. if (write_domain != 0 && read_domains != write_domain)
  357. return -EINVAL;
  358. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  359. if (obj == NULL)
  360. return -EBADF;
  361. mutex_lock(&dev->struct_mutex);
  362. #if WATCH_BUF
  363. DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
  364. obj, obj->size, read_domains, write_domain);
  365. #endif
  366. if (read_domains & I915_GEM_DOMAIN_GTT) {
  367. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  368. } else {
  369. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  370. }
  371. drm_gem_object_unreference(obj);
  372. mutex_unlock(&dev->struct_mutex);
  373. return ret;
  374. }
  375. /**
  376. * Called when user space has done writes to this buffer
  377. */
  378. int
  379. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  380. struct drm_file *file_priv)
  381. {
  382. struct drm_i915_gem_sw_finish *args = data;
  383. struct drm_gem_object *obj;
  384. struct drm_i915_gem_object *obj_priv;
  385. int ret = 0;
  386. if (!(dev->driver->driver_features & DRIVER_GEM))
  387. return -ENODEV;
  388. mutex_lock(&dev->struct_mutex);
  389. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  390. if (obj == NULL) {
  391. mutex_unlock(&dev->struct_mutex);
  392. return -EBADF;
  393. }
  394. #if WATCH_BUF
  395. DRM_INFO("%s: sw_finish %d (%p %d)\n",
  396. __func__, args->handle, obj, obj->size);
  397. #endif
  398. obj_priv = obj->driver_private;
  399. /* Pinned buffers may be scanout, so flush the cache */
  400. if (obj_priv->pin_count)
  401. i915_gem_object_flush_cpu_write_domain(obj);
  402. drm_gem_object_unreference(obj);
  403. mutex_unlock(&dev->struct_mutex);
  404. return ret;
  405. }
  406. /**
  407. * Maps the contents of an object, returning the address it is mapped
  408. * into.
  409. *
  410. * While the mapping holds a reference on the contents of the object, it doesn't
  411. * imply a ref on the object itself.
  412. */
  413. int
  414. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  415. struct drm_file *file_priv)
  416. {
  417. struct drm_i915_gem_mmap *args = data;
  418. struct drm_gem_object *obj;
  419. loff_t offset;
  420. unsigned long addr;
  421. if (!(dev->driver->driver_features & DRIVER_GEM))
  422. return -ENODEV;
  423. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  424. if (obj == NULL)
  425. return -EBADF;
  426. offset = args->offset;
  427. down_write(&current->mm->mmap_sem);
  428. addr = do_mmap(obj->filp, 0, args->size,
  429. PROT_READ | PROT_WRITE, MAP_SHARED,
  430. args->offset);
  431. up_write(&current->mm->mmap_sem);
  432. mutex_lock(&dev->struct_mutex);
  433. drm_gem_object_unreference(obj);
  434. mutex_unlock(&dev->struct_mutex);
  435. if (IS_ERR((void *)addr))
  436. return addr;
  437. args->addr_ptr = (uint64_t) addr;
  438. return 0;
  439. }
  440. static void
  441. i915_gem_object_free_page_list(struct drm_gem_object *obj)
  442. {
  443. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  444. int page_count = obj->size / PAGE_SIZE;
  445. int i;
  446. if (obj_priv->page_list == NULL)
  447. return;
  448. for (i = 0; i < page_count; i++)
  449. if (obj_priv->page_list[i] != NULL) {
  450. if (obj_priv->dirty)
  451. set_page_dirty(obj_priv->page_list[i]);
  452. mark_page_accessed(obj_priv->page_list[i]);
  453. page_cache_release(obj_priv->page_list[i]);
  454. }
  455. obj_priv->dirty = 0;
  456. drm_free(obj_priv->page_list,
  457. page_count * sizeof(struct page *),
  458. DRM_MEM_DRIVER);
  459. obj_priv->page_list = NULL;
  460. }
  461. static void
  462. i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno)
  463. {
  464. struct drm_device *dev = obj->dev;
  465. drm_i915_private_t *dev_priv = dev->dev_private;
  466. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  467. /* Add a reference if we're newly entering the active list. */
  468. if (!obj_priv->active) {
  469. drm_gem_object_reference(obj);
  470. obj_priv->active = 1;
  471. }
  472. /* Move from whatever list we were on to the tail of execution. */
  473. list_move_tail(&obj_priv->list,
  474. &dev_priv->mm.active_list);
  475. obj_priv->last_rendering_seqno = seqno;
  476. }
  477. static void
  478. i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
  479. {
  480. struct drm_device *dev = obj->dev;
  481. drm_i915_private_t *dev_priv = dev->dev_private;
  482. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  483. BUG_ON(!obj_priv->active);
  484. list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
  485. obj_priv->last_rendering_seqno = 0;
  486. }
  487. static void
  488. i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
  489. {
  490. struct drm_device *dev = obj->dev;
  491. drm_i915_private_t *dev_priv = dev->dev_private;
  492. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  493. i915_verify_inactive(dev, __FILE__, __LINE__);
  494. if (obj_priv->pin_count != 0)
  495. list_del_init(&obj_priv->list);
  496. else
  497. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  498. obj_priv->last_rendering_seqno = 0;
  499. if (obj_priv->active) {
  500. obj_priv->active = 0;
  501. drm_gem_object_unreference(obj);
  502. }
  503. i915_verify_inactive(dev, __FILE__, __LINE__);
  504. }
  505. /**
  506. * Creates a new sequence number, emitting a write of it to the status page
  507. * plus an interrupt, which will trigger i915_user_interrupt_handler.
  508. *
  509. * Must be called with struct_lock held.
  510. *
  511. * Returned sequence numbers are nonzero on success.
  512. */
  513. static uint32_t
  514. i915_add_request(struct drm_device *dev, uint32_t flush_domains)
  515. {
  516. drm_i915_private_t *dev_priv = dev->dev_private;
  517. struct drm_i915_gem_request *request;
  518. uint32_t seqno;
  519. int was_empty;
  520. RING_LOCALS;
  521. request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
  522. if (request == NULL)
  523. return 0;
  524. /* Grab the seqno we're going to make this request be, and bump the
  525. * next (skipping 0 so it can be the reserved no-seqno value).
  526. */
  527. seqno = dev_priv->mm.next_gem_seqno;
  528. dev_priv->mm.next_gem_seqno++;
  529. if (dev_priv->mm.next_gem_seqno == 0)
  530. dev_priv->mm.next_gem_seqno++;
  531. BEGIN_LP_RING(4);
  532. OUT_RING(MI_STORE_DWORD_INDEX);
  533. OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  534. OUT_RING(seqno);
  535. OUT_RING(MI_USER_INTERRUPT);
  536. ADVANCE_LP_RING();
  537. DRM_DEBUG("%d\n", seqno);
  538. request->seqno = seqno;
  539. request->emitted_jiffies = jiffies;
  540. was_empty = list_empty(&dev_priv->mm.request_list);
  541. list_add_tail(&request->list, &dev_priv->mm.request_list);
  542. /* Associate any objects on the flushing list matching the write
  543. * domain we're flushing with our flush.
  544. */
  545. if (flush_domains != 0) {
  546. struct drm_i915_gem_object *obj_priv, *next;
  547. list_for_each_entry_safe(obj_priv, next,
  548. &dev_priv->mm.flushing_list, list) {
  549. struct drm_gem_object *obj = obj_priv->obj;
  550. if ((obj->write_domain & flush_domains) ==
  551. obj->write_domain) {
  552. obj->write_domain = 0;
  553. i915_gem_object_move_to_active(obj, seqno);
  554. }
  555. }
  556. }
  557. if (was_empty && !dev_priv->mm.suspended)
  558. schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
  559. return seqno;
  560. }
  561. /**
  562. * Command execution barrier
  563. *
  564. * Ensures that all commands in the ring are finished
  565. * before signalling the CPU
  566. */
  567. static uint32_t
  568. i915_retire_commands(struct drm_device *dev)
  569. {
  570. drm_i915_private_t *dev_priv = dev->dev_private;
  571. uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  572. uint32_t flush_domains = 0;
  573. RING_LOCALS;
  574. /* The sampler always gets flushed on i965 (sigh) */
  575. if (IS_I965G(dev))
  576. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  577. BEGIN_LP_RING(2);
  578. OUT_RING(cmd);
  579. OUT_RING(0); /* noop */
  580. ADVANCE_LP_RING();
  581. return flush_domains;
  582. }
  583. /**
  584. * Moves buffers associated only with the given active seqno from the active
  585. * to inactive list, potentially freeing them.
  586. */
  587. static void
  588. i915_gem_retire_request(struct drm_device *dev,
  589. struct drm_i915_gem_request *request)
  590. {
  591. drm_i915_private_t *dev_priv = dev->dev_private;
  592. /* Move any buffers on the active list that are no longer referenced
  593. * by the ringbuffer to the flushing/inactive lists as appropriate.
  594. */
  595. while (!list_empty(&dev_priv->mm.active_list)) {
  596. struct drm_gem_object *obj;
  597. struct drm_i915_gem_object *obj_priv;
  598. obj_priv = list_first_entry(&dev_priv->mm.active_list,
  599. struct drm_i915_gem_object,
  600. list);
  601. obj = obj_priv->obj;
  602. /* If the seqno being retired doesn't match the oldest in the
  603. * list, then the oldest in the list must still be newer than
  604. * this seqno.
  605. */
  606. if (obj_priv->last_rendering_seqno != request->seqno)
  607. return;
  608. #if WATCH_LRU
  609. DRM_INFO("%s: retire %d moves to inactive list %p\n",
  610. __func__, request->seqno, obj);
  611. #endif
  612. if (obj->write_domain != 0)
  613. i915_gem_object_move_to_flushing(obj);
  614. else
  615. i915_gem_object_move_to_inactive(obj);
  616. }
  617. }
  618. /**
  619. * Returns true if seq1 is later than seq2.
  620. */
  621. static int
  622. i915_seqno_passed(uint32_t seq1, uint32_t seq2)
  623. {
  624. return (int32_t)(seq1 - seq2) >= 0;
  625. }
  626. uint32_t
  627. i915_get_gem_seqno(struct drm_device *dev)
  628. {
  629. drm_i915_private_t *dev_priv = dev->dev_private;
  630. return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
  631. }
  632. /**
  633. * This function clears the request list as sequence numbers are passed.
  634. */
  635. void
  636. i915_gem_retire_requests(struct drm_device *dev)
  637. {
  638. drm_i915_private_t *dev_priv = dev->dev_private;
  639. uint32_t seqno;
  640. seqno = i915_get_gem_seqno(dev);
  641. while (!list_empty(&dev_priv->mm.request_list)) {
  642. struct drm_i915_gem_request *request;
  643. uint32_t retiring_seqno;
  644. request = list_first_entry(&dev_priv->mm.request_list,
  645. struct drm_i915_gem_request,
  646. list);
  647. retiring_seqno = request->seqno;
  648. if (i915_seqno_passed(seqno, retiring_seqno) ||
  649. dev_priv->mm.wedged) {
  650. i915_gem_retire_request(dev, request);
  651. list_del(&request->list);
  652. drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
  653. } else
  654. break;
  655. }
  656. }
  657. void
  658. i915_gem_retire_work_handler(struct work_struct *work)
  659. {
  660. drm_i915_private_t *dev_priv;
  661. struct drm_device *dev;
  662. dev_priv = container_of(work, drm_i915_private_t,
  663. mm.retire_work.work);
  664. dev = dev_priv->dev;
  665. mutex_lock(&dev->struct_mutex);
  666. i915_gem_retire_requests(dev);
  667. if (!dev_priv->mm.suspended &&
  668. !list_empty(&dev_priv->mm.request_list))
  669. schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
  670. mutex_unlock(&dev->struct_mutex);
  671. }
  672. /**
  673. * Waits for a sequence number to be signaled, and cleans up the
  674. * request and object lists appropriately for that event.
  675. */
  676. static int
  677. i915_wait_request(struct drm_device *dev, uint32_t seqno)
  678. {
  679. drm_i915_private_t *dev_priv = dev->dev_private;
  680. int ret = 0;
  681. BUG_ON(seqno == 0);
  682. if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
  683. dev_priv->mm.waiting_gem_seqno = seqno;
  684. i915_user_irq_get(dev);
  685. ret = wait_event_interruptible(dev_priv->irq_queue,
  686. i915_seqno_passed(i915_get_gem_seqno(dev),
  687. seqno) ||
  688. dev_priv->mm.wedged);
  689. i915_user_irq_put(dev);
  690. dev_priv->mm.waiting_gem_seqno = 0;
  691. }
  692. if (dev_priv->mm.wedged)
  693. ret = -EIO;
  694. if (ret && ret != -ERESTARTSYS)
  695. DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
  696. __func__, ret, seqno, i915_get_gem_seqno(dev));
  697. /* Directly dispatch request retiring. While we have the work queue
  698. * to handle this, the waiter on a request often wants an associated
  699. * buffer to have made it to the inactive list, and we would need
  700. * a separate wait queue to handle that.
  701. */
  702. if (ret == 0)
  703. i915_gem_retire_requests(dev);
  704. return ret;
  705. }
  706. static void
  707. i915_gem_flush(struct drm_device *dev,
  708. uint32_t invalidate_domains,
  709. uint32_t flush_domains)
  710. {
  711. drm_i915_private_t *dev_priv = dev->dev_private;
  712. uint32_t cmd;
  713. RING_LOCALS;
  714. #if WATCH_EXEC
  715. DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
  716. invalidate_domains, flush_domains);
  717. #endif
  718. if (flush_domains & I915_GEM_DOMAIN_CPU)
  719. drm_agp_chipset_flush(dev);
  720. if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
  721. I915_GEM_DOMAIN_GTT)) {
  722. /*
  723. * read/write caches:
  724. *
  725. * I915_GEM_DOMAIN_RENDER is always invalidated, but is
  726. * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
  727. * also flushed at 2d versus 3d pipeline switches.
  728. *
  729. * read-only caches:
  730. *
  731. * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
  732. * MI_READ_FLUSH is set, and is always flushed on 965.
  733. *
  734. * I915_GEM_DOMAIN_COMMAND may not exist?
  735. *
  736. * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
  737. * invalidated when MI_EXE_FLUSH is set.
  738. *
  739. * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
  740. * invalidated with every MI_FLUSH.
  741. *
  742. * TLBs:
  743. *
  744. * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
  745. * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
  746. * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
  747. * are flushed at any MI_FLUSH.
  748. */
  749. cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  750. if ((invalidate_domains|flush_domains) &
  751. I915_GEM_DOMAIN_RENDER)
  752. cmd &= ~MI_NO_WRITE_FLUSH;
  753. if (!IS_I965G(dev)) {
  754. /*
  755. * On the 965, the sampler cache always gets flushed
  756. * and this bit is reserved.
  757. */
  758. if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
  759. cmd |= MI_READ_FLUSH;
  760. }
  761. if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
  762. cmd |= MI_EXE_FLUSH;
  763. #if WATCH_EXEC
  764. DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
  765. #endif
  766. BEGIN_LP_RING(2);
  767. OUT_RING(cmd);
  768. OUT_RING(0); /* noop */
  769. ADVANCE_LP_RING();
  770. }
  771. }
  772. /**
  773. * Ensures that all rendering to the object has completed and the object is
  774. * safe to unbind from the GTT or access from the CPU.
  775. */
  776. static int
  777. i915_gem_object_wait_rendering(struct drm_gem_object *obj)
  778. {
  779. struct drm_device *dev = obj->dev;
  780. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  781. int ret;
  782. /* This function only exists to support waiting for existing rendering,
  783. * not for emitting required flushes.
  784. */
  785. BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
  786. /* If there is rendering queued on the buffer being evicted, wait for
  787. * it.
  788. */
  789. if (obj_priv->active) {
  790. #if WATCH_BUF
  791. DRM_INFO("%s: object %p wait for seqno %08x\n",
  792. __func__, obj, obj_priv->last_rendering_seqno);
  793. #endif
  794. ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
  795. if (ret != 0)
  796. return ret;
  797. }
  798. return 0;
  799. }
  800. /**
  801. * Unbinds an object from the GTT aperture.
  802. */
  803. static int
  804. i915_gem_object_unbind(struct drm_gem_object *obj)
  805. {
  806. struct drm_device *dev = obj->dev;
  807. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  808. int ret = 0;
  809. #if WATCH_BUF
  810. DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
  811. DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
  812. #endif
  813. if (obj_priv->gtt_space == NULL)
  814. return 0;
  815. if (obj_priv->pin_count != 0) {
  816. DRM_ERROR("Attempting to unbind pinned buffer\n");
  817. return -EINVAL;
  818. }
  819. /* Move the object to the CPU domain to ensure that
  820. * any possible CPU writes while it's not in the GTT
  821. * are flushed when we go to remap it. This will
  822. * also ensure that all pending GPU writes are finished
  823. * before we unbind.
  824. */
  825. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  826. if (ret) {
  827. if (ret != -ERESTARTSYS)
  828. DRM_ERROR("set_domain failed: %d\n", ret);
  829. return ret;
  830. }
  831. if (obj_priv->agp_mem != NULL) {
  832. drm_unbind_agp(obj_priv->agp_mem);
  833. drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
  834. obj_priv->agp_mem = NULL;
  835. }
  836. BUG_ON(obj_priv->active);
  837. i915_gem_object_free_page_list(obj);
  838. if (obj_priv->gtt_space) {
  839. atomic_dec(&dev->gtt_count);
  840. atomic_sub(obj->size, &dev->gtt_memory);
  841. drm_mm_put_block(obj_priv->gtt_space);
  842. obj_priv->gtt_space = NULL;
  843. }
  844. /* Remove ourselves from the LRU list if present. */
  845. if (!list_empty(&obj_priv->list))
  846. list_del_init(&obj_priv->list);
  847. return 0;
  848. }
  849. static int
  850. i915_gem_evict_something(struct drm_device *dev)
  851. {
  852. drm_i915_private_t *dev_priv = dev->dev_private;
  853. struct drm_gem_object *obj;
  854. struct drm_i915_gem_object *obj_priv;
  855. int ret = 0;
  856. for (;;) {
  857. /* If there's an inactive buffer available now, grab it
  858. * and be done.
  859. */
  860. if (!list_empty(&dev_priv->mm.inactive_list)) {
  861. obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
  862. struct drm_i915_gem_object,
  863. list);
  864. obj = obj_priv->obj;
  865. BUG_ON(obj_priv->pin_count != 0);
  866. #if WATCH_LRU
  867. DRM_INFO("%s: evicting %p\n", __func__, obj);
  868. #endif
  869. BUG_ON(obj_priv->active);
  870. /* Wait on the rendering and unbind the buffer. */
  871. ret = i915_gem_object_unbind(obj);
  872. break;
  873. }
  874. /* If we didn't get anything, but the ring is still processing
  875. * things, wait for one of those things to finish and hopefully
  876. * leave us a buffer to evict.
  877. */
  878. if (!list_empty(&dev_priv->mm.request_list)) {
  879. struct drm_i915_gem_request *request;
  880. request = list_first_entry(&dev_priv->mm.request_list,
  881. struct drm_i915_gem_request,
  882. list);
  883. ret = i915_wait_request(dev, request->seqno);
  884. if (ret)
  885. break;
  886. /* if waiting caused an object to become inactive,
  887. * then loop around and wait for it. Otherwise, we
  888. * assume that waiting freed and unbound something,
  889. * so there should now be some space in the GTT
  890. */
  891. if (!list_empty(&dev_priv->mm.inactive_list))
  892. continue;
  893. break;
  894. }
  895. /* If we didn't have anything on the request list but there
  896. * are buffers awaiting a flush, emit one and try again.
  897. * When we wait on it, those buffers waiting for that flush
  898. * will get moved to inactive.
  899. */
  900. if (!list_empty(&dev_priv->mm.flushing_list)) {
  901. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  902. struct drm_i915_gem_object,
  903. list);
  904. obj = obj_priv->obj;
  905. i915_gem_flush(dev,
  906. obj->write_domain,
  907. obj->write_domain);
  908. i915_add_request(dev, obj->write_domain);
  909. obj = NULL;
  910. continue;
  911. }
  912. DRM_ERROR("inactive empty %d request empty %d "
  913. "flushing empty %d\n",
  914. list_empty(&dev_priv->mm.inactive_list),
  915. list_empty(&dev_priv->mm.request_list),
  916. list_empty(&dev_priv->mm.flushing_list));
  917. /* If we didn't do any of the above, there's nothing to be done
  918. * and we just can't fit it in.
  919. */
  920. return -ENOMEM;
  921. }
  922. return ret;
  923. }
  924. static int
  925. i915_gem_evict_everything(struct drm_device *dev)
  926. {
  927. int ret;
  928. for (;;) {
  929. ret = i915_gem_evict_something(dev);
  930. if (ret != 0)
  931. break;
  932. }
  933. return ret;
  934. }
  935. static int
  936. i915_gem_object_get_page_list(struct drm_gem_object *obj)
  937. {
  938. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  939. int page_count, i;
  940. struct address_space *mapping;
  941. struct inode *inode;
  942. struct page *page;
  943. int ret;
  944. if (obj_priv->page_list)
  945. return 0;
  946. /* Get the list of pages out of our struct file. They'll be pinned
  947. * at this point until we release them.
  948. */
  949. page_count = obj->size / PAGE_SIZE;
  950. BUG_ON(obj_priv->page_list != NULL);
  951. obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
  952. DRM_MEM_DRIVER);
  953. if (obj_priv->page_list == NULL) {
  954. DRM_ERROR("Faled to allocate page list\n");
  955. return -ENOMEM;
  956. }
  957. inode = obj->filp->f_path.dentry->d_inode;
  958. mapping = inode->i_mapping;
  959. for (i = 0; i < page_count; i++) {
  960. page = read_mapping_page(mapping, i, NULL);
  961. if (IS_ERR(page)) {
  962. ret = PTR_ERR(page);
  963. DRM_ERROR("read_mapping_page failed: %d\n", ret);
  964. i915_gem_object_free_page_list(obj);
  965. return ret;
  966. }
  967. obj_priv->page_list[i] = page;
  968. }
  969. return 0;
  970. }
  971. /**
  972. * Finds free space in the GTT aperture and binds the object there.
  973. */
  974. static int
  975. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  976. {
  977. struct drm_device *dev = obj->dev;
  978. drm_i915_private_t *dev_priv = dev->dev_private;
  979. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  980. struct drm_mm_node *free_space;
  981. int page_count, ret;
  982. if (alignment == 0)
  983. alignment = PAGE_SIZE;
  984. if (alignment & (PAGE_SIZE - 1)) {
  985. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  986. return -EINVAL;
  987. }
  988. search_free:
  989. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  990. obj->size, alignment, 0);
  991. if (free_space != NULL) {
  992. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  993. alignment);
  994. if (obj_priv->gtt_space != NULL) {
  995. obj_priv->gtt_space->private = obj;
  996. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  997. }
  998. }
  999. if (obj_priv->gtt_space == NULL) {
  1000. /* If the gtt is empty and we're still having trouble
  1001. * fitting our object in, we're out of memory.
  1002. */
  1003. #if WATCH_LRU
  1004. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  1005. #endif
  1006. if (list_empty(&dev_priv->mm.inactive_list) &&
  1007. list_empty(&dev_priv->mm.flushing_list) &&
  1008. list_empty(&dev_priv->mm.active_list)) {
  1009. DRM_ERROR("GTT full, but LRU list empty\n");
  1010. return -ENOMEM;
  1011. }
  1012. ret = i915_gem_evict_something(dev);
  1013. if (ret != 0) {
  1014. if (ret != -ERESTARTSYS)
  1015. DRM_ERROR("Failed to evict a buffer %d\n", ret);
  1016. return ret;
  1017. }
  1018. goto search_free;
  1019. }
  1020. #if WATCH_BUF
  1021. DRM_INFO("Binding object of size %d at 0x%08x\n",
  1022. obj->size, obj_priv->gtt_offset);
  1023. #endif
  1024. ret = i915_gem_object_get_page_list(obj);
  1025. if (ret) {
  1026. drm_mm_put_block(obj_priv->gtt_space);
  1027. obj_priv->gtt_space = NULL;
  1028. return ret;
  1029. }
  1030. page_count = obj->size / PAGE_SIZE;
  1031. /* Create an AGP memory structure pointing at our pages, and bind it
  1032. * into the GTT.
  1033. */
  1034. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  1035. obj_priv->page_list,
  1036. page_count,
  1037. obj_priv->gtt_offset,
  1038. obj_priv->agp_type);
  1039. if (obj_priv->agp_mem == NULL) {
  1040. i915_gem_object_free_page_list(obj);
  1041. drm_mm_put_block(obj_priv->gtt_space);
  1042. obj_priv->gtt_space = NULL;
  1043. return -ENOMEM;
  1044. }
  1045. atomic_inc(&dev->gtt_count);
  1046. atomic_add(obj->size, &dev->gtt_memory);
  1047. /* Assert that the object is not currently in any GPU domain. As it
  1048. * wasn't in the GTT, there shouldn't be any way it could have been in
  1049. * a GPU cache
  1050. */
  1051. BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1052. BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1053. return 0;
  1054. }
  1055. void
  1056. i915_gem_clflush_object(struct drm_gem_object *obj)
  1057. {
  1058. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1059. /* If we don't have a page list set up, then we're not pinned
  1060. * to GPU, and we can ignore the cache flush because it'll happen
  1061. * again at bind time.
  1062. */
  1063. if (obj_priv->page_list == NULL)
  1064. return;
  1065. drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
  1066. }
  1067. /** Flushes any GPU write domain for the object if it's dirty. */
  1068. static void
  1069. i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
  1070. {
  1071. struct drm_device *dev = obj->dev;
  1072. uint32_t seqno;
  1073. if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  1074. return;
  1075. /* Queue the GPU write cache flushing we need. */
  1076. i915_gem_flush(dev, 0, obj->write_domain);
  1077. seqno = i915_add_request(dev, obj->write_domain);
  1078. obj->write_domain = 0;
  1079. i915_gem_object_move_to_active(obj, seqno);
  1080. }
  1081. /** Flushes the GTT write domain for the object if it's dirty. */
  1082. static void
  1083. i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
  1084. {
  1085. if (obj->write_domain != I915_GEM_DOMAIN_GTT)
  1086. return;
  1087. /* No actual flushing is required for the GTT write domain. Writes
  1088. * to it immediately go to main memory as far as we know, so there's
  1089. * no chipset flush. It also doesn't land in render cache.
  1090. */
  1091. obj->write_domain = 0;
  1092. }
  1093. /** Flushes the CPU write domain for the object if it's dirty. */
  1094. static void
  1095. i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
  1096. {
  1097. struct drm_device *dev = obj->dev;
  1098. if (obj->write_domain != I915_GEM_DOMAIN_CPU)
  1099. return;
  1100. i915_gem_clflush_object(obj);
  1101. drm_agp_chipset_flush(dev);
  1102. obj->write_domain = 0;
  1103. }
  1104. /**
  1105. * Moves a single object to the GTT read, and possibly write domain.
  1106. *
  1107. * This function returns when the move is complete, including waiting on
  1108. * flushes to occur.
  1109. */
  1110. static int
  1111. i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
  1112. {
  1113. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1114. int ret;
  1115. i915_gem_object_flush_gpu_write_domain(obj);
  1116. /* Wait on any GPU rendering and flushing to occur. */
  1117. ret = i915_gem_object_wait_rendering(obj);
  1118. if (ret != 0)
  1119. return ret;
  1120. /* If we're writing through the GTT domain, then CPU and GPU caches
  1121. * will need to be invalidated at next use.
  1122. */
  1123. if (write)
  1124. obj->read_domains &= I915_GEM_DOMAIN_GTT;
  1125. i915_gem_object_flush_cpu_write_domain(obj);
  1126. /* It should now be out of any other write domains, and we can update
  1127. * the domain values for our changes.
  1128. */
  1129. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  1130. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  1131. if (write) {
  1132. obj->write_domain = I915_GEM_DOMAIN_GTT;
  1133. obj_priv->dirty = 1;
  1134. }
  1135. return 0;
  1136. }
  1137. /**
  1138. * Moves a single object to the CPU read, and possibly write domain.
  1139. *
  1140. * This function returns when the move is complete, including waiting on
  1141. * flushes to occur.
  1142. */
  1143. static int
  1144. i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
  1145. {
  1146. struct drm_device *dev = obj->dev;
  1147. int ret;
  1148. i915_gem_object_flush_gpu_write_domain(obj);
  1149. /* Wait on any GPU rendering and flushing to occur. */
  1150. ret = i915_gem_object_wait_rendering(obj);
  1151. if (ret != 0)
  1152. return ret;
  1153. i915_gem_object_flush_gtt_write_domain(obj);
  1154. /* If we have a partially-valid cache of the object in the CPU,
  1155. * finish invalidating it and free the per-page flags.
  1156. */
  1157. i915_gem_object_set_to_full_cpu_read_domain(obj);
  1158. /* Flush the CPU cache if it's still invalid. */
  1159. if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  1160. i915_gem_clflush_object(obj);
  1161. drm_agp_chipset_flush(dev);
  1162. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  1163. }
  1164. /* It should now be out of any other write domains, and we can update
  1165. * the domain values for our changes.
  1166. */
  1167. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  1168. /* If we're writing through the CPU, then the GPU read domains will
  1169. * need to be invalidated at next use.
  1170. */
  1171. if (write) {
  1172. obj->read_domains &= I915_GEM_DOMAIN_CPU;
  1173. obj->write_domain = I915_GEM_DOMAIN_CPU;
  1174. }
  1175. return 0;
  1176. }
  1177. /*
  1178. * Set the next domain for the specified object. This
  1179. * may not actually perform the necessary flushing/invaliding though,
  1180. * as that may want to be batched with other set_domain operations
  1181. *
  1182. * This is (we hope) the only really tricky part of gem. The goal
  1183. * is fairly simple -- track which caches hold bits of the object
  1184. * and make sure they remain coherent. A few concrete examples may
  1185. * help to explain how it works. For shorthand, we use the notation
  1186. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  1187. * a pair of read and write domain masks.
  1188. *
  1189. * Case 1: the batch buffer
  1190. *
  1191. * 1. Allocated
  1192. * 2. Written by CPU
  1193. * 3. Mapped to GTT
  1194. * 4. Read by GPU
  1195. * 5. Unmapped from GTT
  1196. * 6. Freed
  1197. *
  1198. * Let's take these a step at a time
  1199. *
  1200. * 1. Allocated
  1201. * Pages allocated from the kernel may still have
  1202. * cache contents, so we set them to (CPU, CPU) always.
  1203. * 2. Written by CPU (using pwrite)
  1204. * The pwrite function calls set_domain (CPU, CPU) and
  1205. * this function does nothing (as nothing changes)
  1206. * 3. Mapped by GTT
  1207. * This function asserts that the object is not
  1208. * currently in any GPU-based read or write domains
  1209. * 4. Read by GPU
  1210. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  1211. * As write_domain is zero, this function adds in the
  1212. * current read domains (CPU+COMMAND, 0).
  1213. * flush_domains is set to CPU.
  1214. * invalidate_domains is set to COMMAND
  1215. * clflush is run to get data out of the CPU caches
  1216. * then i915_dev_set_domain calls i915_gem_flush to
  1217. * emit an MI_FLUSH and drm_agp_chipset_flush
  1218. * 5. Unmapped from GTT
  1219. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  1220. * flush_domains and invalidate_domains end up both zero
  1221. * so no flushing/invalidating happens
  1222. * 6. Freed
  1223. * yay, done
  1224. *
  1225. * Case 2: The shared render buffer
  1226. *
  1227. * 1. Allocated
  1228. * 2. Mapped to GTT
  1229. * 3. Read/written by GPU
  1230. * 4. set_domain to (CPU,CPU)
  1231. * 5. Read/written by CPU
  1232. * 6. Read/written by GPU
  1233. *
  1234. * 1. Allocated
  1235. * Same as last example, (CPU, CPU)
  1236. * 2. Mapped to GTT
  1237. * Nothing changes (assertions find that it is not in the GPU)
  1238. * 3. Read/written by GPU
  1239. * execbuffer calls set_domain (RENDER, RENDER)
  1240. * flush_domains gets CPU
  1241. * invalidate_domains gets GPU
  1242. * clflush (obj)
  1243. * MI_FLUSH and drm_agp_chipset_flush
  1244. * 4. set_domain (CPU, CPU)
  1245. * flush_domains gets GPU
  1246. * invalidate_domains gets CPU
  1247. * wait_rendering (obj) to make sure all drawing is complete.
  1248. * This will include an MI_FLUSH to get the data from GPU
  1249. * to memory
  1250. * clflush (obj) to invalidate the CPU cache
  1251. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  1252. * 5. Read/written by CPU
  1253. * cache lines are loaded and dirtied
  1254. * 6. Read written by GPU
  1255. * Same as last GPU access
  1256. *
  1257. * Case 3: The constant buffer
  1258. *
  1259. * 1. Allocated
  1260. * 2. Written by CPU
  1261. * 3. Read by GPU
  1262. * 4. Updated (written) by CPU again
  1263. * 5. Read by GPU
  1264. *
  1265. * 1. Allocated
  1266. * (CPU, CPU)
  1267. * 2. Written by CPU
  1268. * (CPU, CPU)
  1269. * 3. Read by GPU
  1270. * (CPU+RENDER, 0)
  1271. * flush_domains = CPU
  1272. * invalidate_domains = RENDER
  1273. * clflush (obj)
  1274. * MI_FLUSH
  1275. * drm_agp_chipset_flush
  1276. * 4. Updated (written) by CPU again
  1277. * (CPU, CPU)
  1278. * flush_domains = 0 (no previous write domain)
  1279. * invalidate_domains = 0 (no new read domains)
  1280. * 5. Read by GPU
  1281. * (CPU+RENDER, 0)
  1282. * flush_domains = CPU
  1283. * invalidate_domains = RENDER
  1284. * clflush (obj)
  1285. * MI_FLUSH
  1286. * drm_agp_chipset_flush
  1287. */
  1288. static void
  1289. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
  1290. uint32_t read_domains,
  1291. uint32_t write_domain)
  1292. {
  1293. struct drm_device *dev = obj->dev;
  1294. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1295. uint32_t invalidate_domains = 0;
  1296. uint32_t flush_domains = 0;
  1297. BUG_ON(read_domains & I915_GEM_DOMAIN_CPU);
  1298. BUG_ON(write_domain == I915_GEM_DOMAIN_CPU);
  1299. #if WATCH_BUF
  1300. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  1301. __func__, obj,
  1302. obj->read_domains, read_domains,
  1303. obj->write_domain, write_domain);
  1304. #endif
  1305. /*
  1306. * If the object isn't moving to a new write domain,
  1307. * let the object stay in multiple read domains
  1308. */
  1309. if (write_domain == 0)
  1310. read_domains |= obj->read_domains;
  1311. else
  1312. obj_priv->dirty = 1;
  1313. /*
  1314. * Flush the current write domain if
  1315. * the new read domains don't match. Invalidate
  1316. * any read domains which differ from the old
  1317. * write domain
  1318. */
  1319. if (obj->write_domain && obj->write_domain != read_domains) {
  1320. flush_domains |= obj->write_domain;
  1321. invalidate_domains |= read_domains & ~obj->write_domain;
  1322. }
  1323. /*
  1324. * Invalidate any read caches which may have
  1325. * stale data. That is, any new read domains.
  1326. */
  1327. invalidate_domains |= read_domains & ~obj->read_domains;
  1328. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  1329. #if WATCH_BUF
  1330. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  1331. __func__, flush_domains, invalidate_domains);
  1332. #endif
  1333. i915_gem_clflush_object(obj);
  1334. }
  1335. if ((write_domain | flush_domains) != 0)
  1336. obj->write_domain = write_domain;
  1337. obj->read_domains = read_domains;
  1338. dev->invalidate_domains |= invalidate_domains;
  1339. dev->flush_domains |= flush_domains;
  1340. #if WATCH_BUF
  1341. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  1342. __func__,
  1343. obj->read_domains, obj->write_domain,
  1344. dev->invalidate_domains, dev->flush_domains);
  1345. #endif
  1346. }
  1347. /**
  1348. * Moves the object from a partially CPU read to a full one.
  1349. *
  1350. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  1351. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  1352. */
  1353. static void
  1354. i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
  1355. {
  1356. struct drm_device *dev = obj->dev;
  1357. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1358. if (!obj_priv->page_cpu_valid)
  1359. return;
  1360. /* If we're partially in the CPU read domain, finish moving it in.
  1361. */
  1362. if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
  1363. int i;
  1364. for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
  1365. if (obj_priv->page_cpu_valid[i])
  1366. continue;
  1367. drm_clflush_pages(obj_priv->page_list + i, 1);
  1368. }
  1369. drm_agp_chipset_flush(dev);
  1370. }
  1371. /* Free the page_cpu_valid mappings which are now stale, whether
  1372. * or not we've got I915_GEM_DOMAIN_CPU.
  1373. */
  1374. drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
  1375. DRM_MEM_DRIVER);
  1376. obj_priv->page_cpu_valid = NULL;
  1377. }
  1378. /**
  1379. * Set the CPU read domain on a range of the object.
  1380. *
  1381. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  1382. * not entirely valid. The page_cpu_valid member of the object flags which
  1383. * pages have been flushed, and will be respected by
  1384. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  1385. * of the whole object.
  1386. *
  1387. * This function returns when the move is complete, including waiting on
  1388. * flushes to occur.
  1389. */
  1390. static int
  1391. i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  1392. uint64_t offset, uint64_t size)
  1393. {
  1394. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1395. int i, ret;
  1396. if (offset == 0 && size == obj->size)
  1397. return i915_gem_object_set_to_cpu_domain(obj, 0);
  1398. i915_gem_object_flush_gpu_write_domain(obj);
  1399. /* Wait on any GPU rendering and flushing to occur. */
  1400. ret = i915_gem_object_wait_rendering(obj);
  1401. if (ret != 0)
  1402. return ret;
  1403. i915_gem_object_flush_gtt_write_domain(obj);
  1404. /* If we're already fully in the CPU read domain, we're done. */
  1405. if (obj_priv->page_cpu_valid == NULL &&
  1406. (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
  1407. return 0;
  1408. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  1409. * newly adding I915_GEM_DOMAIN_CPU
  1410. */
  1411. if (obj_priv->page_cpu_valid == NULL) {
  1412. obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
  1413. DRM_MEM_DRIVER);
  1414. if (obj_priv->page_cpu_valid == NULL)
  1415. return -ENOMEM;
  1416. } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
  1417. memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
  1418. /* Flush the cache on any pages that are still invalid from the CPU's
  1419. * perspective.
  1420. */
  1421. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  1422. i++) {
  1423. if (obj_priv->page_cpu_valid[i])
  1424. continue;
  1425. drm_clflush_pages(obj_priv->page_list + i, 1);
  1426. obj_priv->page_cpu_valid[i] = 1;
  1427. }
  1428. /* It should now be out of any other write domains, and we can update
  1429. * the domain values for our changes.
  1430. */
  1431. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  1432. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  1433. return 0;
  1434. }
  1435. /**
  1436. * Pin an object to the GTT and evaluate the relocations landing in it.
  1437. */
  1438. static int
  1439. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  1440. struct drm_file *file_priv,
  1441. struct drm_i915_gem_exec_object *entry)
  1442. {
  1443. struct drm_device *dev = obj->dev;
  1444. drm_i915_private_t *dev_priv = dev->dev_private;
  1445. struct drm_i915_gem_relocation_entry reloc;
  1446. struct drm_i915_gem_relocation_entry __user *relocs;
  1447. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1448. int i, ret;
  1449. void __iomem *reloc_page;
  1450. /* Choose the GTT offset for our buffer and put it there. */
  1451. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  1452. if (ret)
  1453. return ret;
  1454. entry->offset = obj_priv->gtt_offset;
  1455. relocs = (struct drm_i915_gem_relocation_entry __user *)
  1456. (uintptr_t) entry->relocs_ptr;
  1457. /* Apply the relocations, using the GTT aperture to avoid cache
  1458. * flushing requirements.
  1459. */
  1460. for (i = 0; i < entry->relocation_count; i++) {
  1461. struct drm_gem_object *target_obj;
  1462. struct drm_i915_gem_object *target_obj_priv;
  1463. uint32_t reloc_val, reloc_offset;
  1464. uint32_t __iomem *reloc_entry;
  1465. ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
  1466. if (ret != 0) {
  1467. i915_gem_object_unpin(obj);
  1468. return ret;
  1469. }
  1470. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  1471. reloc.target_handle);
  1472. if (target_obj == NULL) {
  1473. i915_gem_object_unpin(obj);
  1474. return -EBADF;
  1475. }
  1476. target_obj_priv = target_obj->driver_private;
  1477. /* The target buffer should have appeared before us in the
  1478. * exec_object list, so it should have a GTT space bound by now.
  1479. */
  1480. if (target_obj_priv->gtt_space == NULL) {
  1481. DRM_ERROR("No GTT space found for object %d\n",
  1482. reloc.target_handle);
  1483. drm_gem_object_unreference(target_obj);
  1484. i915_gem_object_unpin(obj);
  1485. return -EINVAL;
  1486. }
  1487. if (reloc.offset > obj->size - 4) {
  1488. DRM_ERROR("Relocation beyond object bounds: "
  1489. "obj %p target %d offset %d size %d.\n",
  1490. obj, reloc.target_handle,
  1491. (int) reloc.offset, (int) obj->size);
  1492. drm_gem_object_unreference(target_obj);
  1493. i915_gem_object_unpin(obj);
  1494. return -EINVAL;
  1495. }
  1496. if (reloc.offset & 3) {
  1497. DRM_ERROR("Relocation not 4-byte aligned: "
  1498. "obj %p target %d offset %d.\n",
  1499. obj, reloc.target_handle,
  1500. (int) reloc.offset);
  1501. drm_gem_object_unreference(target_obj);
  1502. i915_gem_object_unpin(obj);
  1503. return -EINVAL;
  1504. }
  1505. if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
  1506. reloc.read_domains & I915_GEM_DOMAIN_CPU) {
  1507. DRM_ERROR("reloc with read/write CPU domains: "
  1508. "obj %p target %d offset %d "
  1509. "read %08x write %08x",
  1510. obj, reloc.target_handle,
  1511. (int) reloc.offset,
  1512. reloc.read_domains,
  1513. reloc.write_domain);
  1514. return -EINVAL;
  1515. }
  1516. if (reloc.write_domain && target_obj->pending_write_domain &&
  1517. reloc.write_domain != target_obj->pending_write_domain) {
  1518. DRM_ERROR("Write domain conflict: "
  1519. "obj %p target %d offset %d "
  1520. "new %08x old %08x\n",
  1521. obj, reloc.target_handle,
  1522. (int) reloc.offset,
  1523. reloc.write_domain,
  1524. target_obj->pending_write_domain);
  1525. drm_gem_object_unreference(target_obj);
  1526. i915_gem_object_unpin(obj);
  1527. return -EINVAL;
  1528. }
  1529. #if WATCH_RELOC
  1530. DRM_INFO("%s: obj %p offset %08x target %d "
  1531. "read %08x write %08x gtt %08x "
  1532. "presumed %08x delta %08x\n",
  1533. __func__,
  1534. obj,
  1535. (int) reloc.offset,
  1536. (int) reloc.target_handle,
  1537. (int) reloc.read_domains,
  1538. (int) reloc.write_domain,
  1539. (int) target_obj_priv->gtt_offset,
  1540. (int) reloc.presumed_offset,
  1541. reloc.delta);
  1542. #endif
  1543. target_obj->pending_read_domains |= reloc.read_domains;
  1544. target_obj->pending_write_domain |= reloc.write_domain;
  1545. /* If the relocation already has the right value in it, no
  1546. * more work needs to be done.
  1547. */
  1548. if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
  1549. drm_gem_object_unreference(target_obj);
  1550. continue;
  1551. }
  1552. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  1553. if (ret != 0) {
  1554. drm_gem_object_unreference(target_obj);
  1555. i915_gem_object_unpin(obj);
  1556. return -EINVAL;
  1557. }
  1558. /* Map the page containing the relocation we're going to
  1559. * perform.
  1560. */
  1561. reloc_offset = obj_priv->gtt_offset + reloc.offset;
  1562. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  1563. (reloc_offset &
  1564. ~(PAGE_SIZE - 1)));
  1565. reloc_entry = (uint32_t __iomem *)(reloc_page +
  1566. (reloc_offset & (PAGE_SIZE - 1)));
  1567. reloc_val = target_obj_priv->gtt_offset + reloc.delta;
  1568. #if WATCH_BUF
  1569. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  1570. obj, (unsigned int) reloc.offset,
  1571. readl(reloc_entry), reloc_val);
  1572. #endif
  1573. writel(reloc_val, reloc_entry);
  1574. io_mapping_unmap_atomic(reloc_page);
  1575. /* Write the updated presumed offset for this entry back out
  1576. * to the user.
  1577. */
  1578. reloc.presumed_offset = target_obj_priv->gtt_offset;
  1579. ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
  1580. if (ret != 0) {
  1581. drm_gem_object_unreference(target_obj);
  1582. i915_gem_object_unpin(obj);
  1583. return ret;
  1584. }
  1585. drm_gem_object_unreference(target_obj);
  1586. }
  1587. #if WATCH_BUF
  1588. if (0)
  1589. i915_gem_dump_object(obj, 128, __func__, ~0);
  1590. #endif
  1591. return 0;
  1592. }
  1593. /** Dispatch a batchbuffer to the ring
  1594. */
  1595. static int
  1596. i915_dispatch_gem_execbuffer(struct drm_device *dev,
  1597. struct drm_i915_gem_execbuffer *exec,
  1598. uint64_t exec_offset)
  1599. {
  1600. drm_i915_private_t *dev_priv = dev->dev_private;
  1601. struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
  1602. (uintptr_t) exec->cliprects_ptr;
  1603. int nbox = exec->num_cliprects;
  1604. int i = 0, count;
  1605. uint32_t exec_start, exec_len;
  1606. RING_LOCALS;
  1607. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  1608. exec_len = (uint32_t) exec->batch_len;
  1609. if ((exec_start | exec_len) & 0x7) {
  1610. DRM_ERROR("alignment\n");
  1611. return -EINVAL;
  1612. }
  1613. if (!exec_start)
  1614. return -EINVAL;
  1615. count = nbox ? nbox : 1;
  1616. for (i = 0; i < count; i++) {
  1617. if (i < nbox) {
  1618. int ret = i915_emit_box(dev, boxes, i,
  1619. exec->DR1, exec->DR4);
  1620. if (ret)
  1621. return ret;
  1622. }
  1623. if (IS_I830(dev) || IS_845G(dev)) {
  1624. BEGIN_LP_RING(4);
  1625. OUT_RING(MI_BATCH_BUFFER);
  1626. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  1627. OUT_RING(exec_start + exec_len - 4);
  1628. OUT_RING(0);
  1629. ADVANCE_LP_RING();
  1630. } else {
  1631. BEGIN_LP_RING(2);
  1632. if (IS_I965G(dev)) {
  1633. OUT_RING(MI_BATCH_BUFFER_START |
  1634. (2 << 6) |
  1635. MI_BATCH_NON_SECURE_I965);
  1636. OUT_RING(exec_start);
  1637. } else {
  1638. OUT_RING(MI_BATCH_BUFFER_START |
  1639. (2 << 6));
  1640. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  1641. }
  1642. ADVANCE_LP_RING();
  1643. }
  1644. }
  1645. /* XXX breadcrumb */
  1646. return 0;
  1647. }
  1648. /* Throttle our rendering by waiting until the ring has completed our requests
  1649. * emitted over 20 msec ago.
  1650. *
  1651. * This should get us reasonable parallelism between CPU and GPU but also
  1652. * relatively low latency when blocking on a particular request to finish.
  1653. */
  1654. static int
  1655. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  1656. {
  1657. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  1658. int ret = 0;
  1659. uint32_t seqno;
  1660. mutex_lock(&dev->struct_mutex);
  1661. seqno = i915_file_priv->mm.last_gem_throttle_seqno;
  1662. i915_file_priv->mm.last_gem_throttle_seqno =
  1663. i915_file_priv->mm.last_gem_seqno;
  1664. if (seqno)
  1665. ret = i915_wait_request(dev, seqno);
  1666. mutex_unlock(&dev->struct_mutex);
  1667. return ret;
  1668. }
  1669. int
  1670. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1671. struct drm_file *file_priv)
  1672. {
  1673. drm_i915_private_t *dev_priv = dev->dev_private;
  1674. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  1675. struct drm_i915_gem_execbuffer *args = data;
  1676. struct drm_i915_gem_exec_object *exec_list = NULL;
  1677. struct drm_gem_object **object_list = NULL;
  1678. struct drm_gem_object *batch_obj;
  1679. int ret, i, pinned = 0;
  1680. uint64_t exec_offset;
  1681. uint32_t seqno, flush_domains;
  1682. int pin_tries;
  1683. #if WATCH_EXEC
  1684. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  1685. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  1686. #endif
  1687. if (args->buffer_count < 1) {
  1688. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  1689. return -EINVAL;
  1690. }
  1691. /* Copy in the exec list from userland */
  1692. exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
  1693. DRM_MEM_DRIVER);
  1694. object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
  1695. DRM_MEM_DRIVER);
  1696. if (exec_list == NULL || object_list == NULL) {
  1697. DRM_ERROR("Failed to allocate exec or object list "
  1698. "for %d buffers\n",
  1699. args->buffer_count);
  1700. ret = -ENOMEM;
  1701. goto pre_mutex_err;
  1702. }
  1703. ret = copy_from_user(exec_list,
  1704. (struct drm_i915_relocation_entry __user *)
  1705. (uintptr_t) args->buffers_ptr,
  1706. sizeof(*exec_list) * args->buffer_count);
  1707. if (ret != 0) {
  1708. DRM_ERROR("copy %d exec entries failed %d\n",
  1709. args->buffer_count, ret);
  1710. goto pre_mutex_err;
  1711. }
  1712. mutex_lock(&dev->struct_mutex);
  1713. i915_verify_inactive(dev, __FILE__, __LINE__);
  1714. if (dev_priv->mm.wedged) {
  1715. DRM_ERROR("Execbuf while wedged\n");
  1716. mutex_unlock(&dev->struct_mutex);
  1717. return -EIO;
  1718. }
  1719. if (dev_priv->mm.suspended) {
  1720. DRM_ERROR("Execbuf while VT-switched.\n");
  1721. mutex_unlock(&dev->struct_mutex);
  1722. return -EBUSY;
  1723. }
  1724. /* Look up object handles */
  1725. for (i = 0; i < args->buffer_count; i++) {
  1726. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  1727. exec_list[i].handle);
  1728. if (object_list[i] == NULL) {
  1729. DRM_ERROR("Invalid object handle %d at index %d\n",
  1730. exec_list[i].handle, i);
  1731. ret = -EBADF;
  1732. goto err;
  1733. }
  1734. }
  1735. /* Pin and relocate */
  1736. for (pin_tries = 0; ; pin_tries++) {
  1737. ret = 0;
  1738. for (i = 0; i < args->buffer_count; i++) {
  1739. object_list[i]->pending_read_domains = 0;
  1740. object_list[i]->pending_write_domain = 0;
  1741. ret = i915_gem_object_pin_and_relocate(object_list[i],
  1742. file_priv,
  1743. &exec_list[i]);
  1744. if (ret)
  1745. break;
  1746. pinned = i + 1;
  1747. }
  1748. /* success */
  1749. if (ret == 0)
  1750. break;
  1751. /* error other than GTT full, or we've already tried again */
  1752. if (ret != -ENOMEM || pin_tries >= 1) {
  1753. DRM_ERROR("Failed to pin buffers %d\n", ret);
  1754. goto err;
  1755. }
  1756. /* unpin all of our buffers */
  1757. for (i = 0; i < pinned; i++)
  1758. i915_gem_object_unpin(object_list[i]);
  1759. /* evict everyone we can from the aperture */
  1760. ret = i915_gem_evict_everything(dev);
  1761. if (ret)
  1762. goto err;
  1763. }
  1764. /* Set the pending read domains for the batch buffer to COMMAND */
  1765. batch_obj = object_list[args->buffer_count-1];
  1766. batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  1767. batch_obj->pending_write_domain = 0;
  1768. i915_verify_inactive(dev, __FILE__, __LINE__);
  1769. /* Zero the global flush/invalidate flags. These
  1770. * will be modified as new domains are computed
  1771. * for each object
  1772. */
  1773. dev->invalidate_domains = 0;
  1774. dev->flush_domains = 0;
  1775. for (i = 0; i < args->buffer_count; i++) {
  1776. struct drm_gem_object *obj = object_list[i];
  1777. /* Compute new gpu domains and update invalidate/flush */
  1778. i915_gem_object_set_to_gpu_domain(obj,
  1779. obj->pending_read_domains,
  1780. obj->pending_write_domain);
  1781. }
  1782. i915_verify_inactive(dev, __FILE__, __LINE__);
  1783. if (dev->invalidate_domains | dev->flush_domains) {
  1784. #if WATCH_EXEC
  1785. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  1786. __func__,
  1787. dev->invalidate_domains,
  1788. dev->flush_domains);
  1789. #endif
  1790. i915_gem_flush(dev,
  1791. dev->invalidate_domains,
  1792. dev->flush_domains);
  1793. if (dev->flush_domains)
  1794. (void)i915_add_request(dev, dev->flush_domains);
  1795. }
  1796. i915_verify_inactive(dev, __FILE__, __LINE__);
  1797. #if WATCH_COHERENCY
  1798. for (i = 0; i < args->buffer_count; i++) {
  1799. i915_gem_object_check_coherency(object_list[i],
  1800. exec_list[i].handle);
  1801. }
  1802. #endif
  1803. exec_offset = exec_list[args->buffer_count - 1].offset;
  1804. #if WATCH_EXEC
  1805. i915_gem_dump_object(object_list[args->buffer_count - 1],
  1806. args->batch_len,
  1807. __func__,
  1808. ~0);
  1809. #endif
  1810. /* Exec the batchbuffer */
  1811. ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
  1812. if (ret) {
  1813. DRM_ERROR("dispatch failed %d\n", ret);
  1814. goto err;
  1815. }
  1816. /*
  1817. * Ensure that the commands in the batch buffer are
  1818. * finished before the interrupt fires
  1819. */
  1820. flush_domains = i915_retire_commands(dev);
  1821. i915_verify_inactive(dev, __FILE__, __LINE__);
  1822. /*
  1823. * Get a seqno representing the execution of the current buffer,
  1824. * which we can wait on. We would like to mitigate these interrupts,
  1825. * likely by only creating seqnos occasionally (so that we have
  1826. * *some* interrupts representing completion of buffers that we can
  1827. * wait on when trying to clear up gtt space).
  1828. */
  1829. seqno = i915_add_request(dev, flush_domains);
  1830. BUG_ON(seqno == 0);
  1831. i915_file_priv->mm.last_gem_seqno = seqno;
  1832. for (i = 0; i < args->buffer_count; i++) {
  1833. struct drm_gem_object *obj = object_list[i];
  1834. i915_gem_object_move_to_active(obj, seqno);
  1835. #if WATCH_LRU
  1836. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  1837. #endif
  1838. }
  1839. #if WATCH_LRU
  1840. i915_dump_lru(dev, __func__);
  1841. #endif
  1842. i915_verify_inactive(dev, __FILE__, __LINE__);
  1843. /* Copy the new buffer offsets back to the user's exec list. */
  1844. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1845. (uintptr_t) args->buffers_ptr,
  1846. exec_list,
  1847. sizeof(*exec_list) * args->buffer_count);
  1848. if (ret)
  1849. DRM_ERROR("failed to copy %d exec entries "
  1850. "back to user (%d)\n",
  1851. args->buffer_count, ret);
  1852. err:
  1853. if (object_list != NULL) {
  1854. for (i = 0; i < pinned; i++)
  1855. i915_gem_object_unpin(object_list[i]);
  1856. for (i = 0; i < args->buffer_count; i++)
  1857. drm_gem_object_unreference(object_list[i]);
  1858. }
  1859. mutex_unlock(&dev->struct_mutex);
  1860. pre_mutex_err:
  1861. drm_free(object_list, sizeof(*object_list) * args->buffer_count,
  1862. DRM_MEM_DRIVER);
  1863. drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
  1864. DRM_MEM_DRIVER);
  1865. return ret;
  1866. }
  1867. int
  1868. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  1869. {
  1870. struct drm_device *dev = obj->dev;
  1871. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1872. int ret;
  1873. i915_verify_inactive(dev, __FILE__, __LINE__);
  1874. if (obj_priv->gtt_space == NULL) {
  1875. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  1876. if (ret != 0) {
  1877. DRM_ERROR("Failure to bind: %d", ret);
  1878. return ret;
  1879. }
  1880. }
  1881. obj_priv->pin_count++;
  1882. /* If the object is not active and not pending a flush,
  1883. * remove it from the inactive list
  1884. */
  1885. if (obj_priv->pin_count == 1) {
  1886. atomic_inc(&dev->pin_count);
  1887. atomic_add(obj->size, &dev->pin_memory);
  1888. if (!obj_priv->active &&
  1889. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  1890. I915_GEM_DOMAIN_GTT)) == 0 &&
  1891. !list_empty(&obj_priv->list))
  1892. list_del_init(&obj_priv->list);
  1893. }
  1894. i915_verify_inactive(dev, __FILE__, __LINE__);
  1895. return 0;
  1896. }
  1897. void
  1898. i915_gem_object_unpin(struct drm_gem_object *obj)
  1899. {
  1900. struct drm_device *dev = obj->dev;
  1901. drm_i915_private_t *dev_priv = dev->dev_private;
  1902. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1903. i915_verify_inactive(dev, __FILE__, __LINE__);
  1904. obj_priv->pin_count--;
  1905. BUG_ON(obj_priv->pin_count < 0);
  1906. BUG_ON(obj_priv->gtt_space == NULL);
  1907. /* If the object is no longer pinned, and is
  1908. * neither active nor being flushed, then stick it on
  1909. * the inactive list
  1910. */
  1911. if (obj_priv->pin_count == 0) {
  1912. if (!obj_priv->active &&
  1913. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  1914. I915_GEM_DOMAIN_GTT)) == 0)
  1915. list_move_tail(&obj_priv->list,
  1916. &dev_priv->mm.inactive_list);
  1917. atomic_dec(&dev->pin_count);
  1918. atomic_sub(obj->size, &dev->pin_memory);
  1919. }
  1920. i915_verify_inactive(dev, __FILE__, __LINE__);
  1921. }
  1922. int
  1923. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  1924. struct drm_file *file_priv)
  1925. {
  1926. struct drm_i915_gem_pin *args = data;
  1927. struct drm_gem_object *obj;
  1928. struct drm_i915_gem_object *obj_priv;
  1929. int ret;
  1930. mutex_lock(&dev->struct_mutex);
  1931. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1932. if (obj == NULL) {
  1933. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  1934. args->handle);
  1935. mutex_unlock(&dev->struct_mutex);
  1936. return -EBADF;
  1937. }
  1938. obj_priv = obj->driver_private;
  1939. ret = i915_gem_object_pin(obj, args->alignment);
  1940. if (ret != 0) {
  1941. drm_gem_object_unreference(obj);
  1942. mutex_unlock(&dev->struct_mutex);
  1943. return ret;
  1944. }
  1945. /* XXX - flush the CPU caches for pinned objects
  1946. * as the X server doesn't manage domains yet
  1947. */
  1948. i915_gem_object_flush_cpu_write_domain(obj);
  1949. args->offset = obj_priv->gtt_offset;
  1950. drm_gem_object_unreference(obj);
  1951. mutex_unlock(&dev->struct_mutex);
  1952. return 0;
  1953. }
  1954. int
  1955. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  1956. struct drm_file *file_priv)
  1957. {
  1958. struct drm_i915_gem_pin *args = data;
  1959. struct drm_gem_object *obj;
  1960. mutex_lock(&dev->struct_mutex);
  1961. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1962. if (obj == NULL) {
  1963. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  1964. args->handle);
  1965. mutex_unlock(&dev->struct_mutex);
  1966. return -EBADF;
  1967. }
  1968. i915_gem_object_unpin(obj);
  1969. drm_gem_object_unreference(obj);
  1970. mutex_unlock(&dev->struct_mutex);
  1971. return 0;
  1972. }
  1973. int
  1974. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  1975. struct drm_file *file_priv)
  1976. {
  1977. struct drm_i915_gem_busy *args = data;
  1978. struct drm_gem_object *obj;
  1979. struct drm_i915_gem_object *obj_priv;
  1980. mutex_lock(&dev->struct_mutex);
  1981. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1982. if (obj == NULL) {
  1983. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  1984. args->handle);
  1985. mutex_unlock(&dev->struct_mutex);
  1986. return -EBADF;
  1987. }
  1988. obj_priv = obj->driver_private;
  1989. args->busy = obj_priv->active;
  1990. drm_gem_object_unreference(obj);
  1991. mutex_unlock(&dev->struct_mutex);
  1992. return 0;
  1993. }
  1994. int
  1995. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  1996. struct drm_file *file_priv)
  1997. {
  1998. return i915_gem_ring_throttle(dev, file_priv);
  1999. }
  2000. int i915_gem_init_object(struct drm_gem_object *obj)
  2001. {
  2002. struct drm_i915_gem_object *obj_priv;
  2003. obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
  2004. if (obj_priv == NULL)
  2005. return -ENOMEM;
  2006. /*
  2007. * We've just allocated pages from the kernel,
  2008. * so they've just been written by the CPU with
  2009. * zeros. They'll need to be clflushed before we
  2010. * use them with the GPU.
  2011. */
  2012. obj->write_domain = I915_GEM_DOMAIN_CPU;
  2013. obj->read_domains = I915_GEM_DOMAIN_CPU;
  2014. obj_priv->agp_type = AGP_USER_MEMORY;
  2015. obj->driver_private = obj_priv;
  2016. obj_priv->obj = obj;
  2017. INIT_LIST_HEAD(&obj_priv->list);
  2018. return 0;
  2019. }
  2020. void i915_gem_free_object(struct drm_gem_object *obj)
  2021. {
  2022. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2023. while (obj_priv->pin_count > 0)
  2024. i915_gem_object_unpin(obj);
  2025. i915_gem_object_unbind(obj);
  2026. drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
  2027. drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
  2028. }
  2029. /** Unbinds all objects that are on the given buffer list. */
  2030. static int
  2031. i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
  2032. {
  2033. struct drm_gem_object *obj;
  2034. struct drm_i915_gem_object *obj_priv;
  2035. int ret;
  2036. while (!list_empty(head)) {
  2037. obj_priv = list_first_entry(head,
  2038. struct drm_i915_gem_object,
  2039. list);
  2040. obj = obj_priv->obj;
  2041. if (obj_priv->pin_count != 0) {
  2042. DRM_ERROR("Pinned object in unbind list\n");
  2043. mutex_unlock(&dev->struct_mutex);
  2044. return -EINVAL;
  2045. }
  2046. ret = i915_gem_object_unbind(obj);
  2047. if (ret != 0) {
  2048. DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
  2049. ret);
  2050. mutex_unlock(&dev->struct_mutex);
  2051. return ret;
  2052. }
  2053. }
  2054. return 0;
  2055. }
  2056. static int
  2057. i915_gem_idle(struct drm_device *dev)
  2058. {
  2059. drm_i915_private_t *dev_priv = dev->dev_private;
  2060. uint32_t seqno, cur_seqno, last_seqno;
  2061. int stuck, ret;
  2062. mutex_lock(&dev->struct_mutex);
  2063. if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
  2064. mutex_unlock(&dev->struct_mutex);
  2065. return 0;
  2066. }
  2067. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  2068. * We need to replace this with a semaphore, or something.
  2069. */
  2070. dev_priv->mm.suspended = 1;
  2071. /* Cancel the retire work handler, wait for it to finish if running
  2072. */
  2073. mutex_unlock(&dev->struct_mutex);
  2074. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  2075. mutex_lock(&dev->struct_mutex);
  2076. i915_kernel_lost_context(dev);
  2077. /* Flush the GPU along with all non-CPU write domains
  2078. */
  2079. i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
  2080. ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  2081. seqno = i915_add_request(dev, ~(I915_GEM_DOMAIN_CPU |
  2082. I915_GEM_DOMAIN_GTT));
  2083. if (seqno == 0) {
  2084. mutex_unlock(&dev->struct_mutex);
  2085. return -ENOMEM;
  2086. }
  2087. dev_priv->mm.waiting_gem_seqno = seqno;
  2088. last_seqno = 0;
  2089. stuck = 0;
  2090. for (;;) {
  2091. cur_seqno = i915_get_gem_seqno(dev);
  2092. if (i915_seqno_passed(cur_seqno, seqno))
  2093. break;
  2094. if (last_seqno == cur_seqno) {
  2095. if (stuck++ > 100) {
  2096. DRM_ERROR("hardware wedged\n");
  2097. dev_priv->mm.wedged = 1;
  2098. DRM_WAKEUP(&dev_priv->irq_queue);
  2099. break;
  2100. }
  2101. }
  2102. msleep(10);
  2103. last_seqno = cur_seqno;
  2104. }
  2105. dev_priv->mm.waiting_gem_seqno = 0;
  2106. i915_gem_retire_requests(dev);
  2107. if (!dev_priv->mm.wedged) {
  2108. /* Active and flushing should now be empty as we've
  2109. * waited for a sequence higher than any pending execbuffer
  2110. */
  2111. WARN_ON(!list_empty(&dev_priv->mm.active_list));
  2112. WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
  2113. /* Request should now be empty as we've also waited
  2114. * for the last request in the list
  2115. */
  2116. WARN_ON(!list_empty(&dev_priv->mm.request_list));
  2117. }
  2118. /* Empty the active and flushing lists to inactive. If there's
  2119. * anything left at this point, it means that we're wedged and
  2120. * nothing good's going to happen by leaving them there. So strip
  2121. * the GPU domains and just stuff them onto inactive.
  2122. */
  2123. while (!list_empty(&dev_priv->mm.active_list)) {
  2124. struct drm_i915_gem_object *obj_priv;
  2125. obj_priv = list_first_entry(&dev_priv->mm.active_list,
  2126. struct drm_i915_gem_object,
  2127. list);
  2128. obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
  2129. i915_gem_object_move_to_inactive(obj_priv->obj);
  2130. }
  2131. while (!list_empty(&dev_priv->mm.flushing_list)) {
  2132. struct drm_i915_gem_object *obj_priv;
  2133. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  2134. struct drm_i915_gem_object,
  2135. list);
  2136. obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
  2137. i915_gem_object_move_to_inactive(obj_priv->obj);
  2138. }
  2139. /* Move all inactive buffers out of the GTT. */
  2140. ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
  2141. WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
  2142. if (ret) {
  2143. mutex_unlock(&dev->struct_mutex);
  2144. return ret;
  2145. }
  2146. i915_gem_cleanup_ringbuffer(dev);
  2147. mutex_unlock(&dev->struct_mutex);
  2148. return 0;
  2149. }
  2150. static int
  2151. i915_gem_init_hws(struct drm_device *dev)
  2152. {
  2153. drm_i915_private_t *dev_priv = dev->dev_private;
  2154. struct drm_gem_object *obj;
  2155. struct drm_i915_gem_object *obj_priv;
  2156. int ret;
  2157. /* If we need a physical address for the status page, it's already
  2158. * initialized at driver load time.
  2159. */
  2160. if (!I915_NEED_GFX_HWS(dev))
  2161. return 0;
  2162. obj = drm_gem_object_alloc(dev, 4096);
  2163. if (obj == NULL) {
  2164. DRM_ERROR("Failed to allocate status page\n");
  2165. return -ENOMEM;
  2166. }
  2167. obj_priv = obj->driver_private;
  2168. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  2169. ret = i915_gem_object_pin(obj, 4096);
  2170. if (ret != 0) {
  2171. drm_gem_object_unreference(obj);
  2172. return ret;
  2173. }
  2174. dev_priv->status_gfx_addr = obj_priv->gtt_offset;
  2175. dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
  2176. if (dev_priv->hw_status_page == NULL) {
  2177. DRM_ERROR("Failed to map status page.\n");
  2178. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2179. drm_gem_object_unreference(obj);
  2180. return -EINVAL;
  2181. }
  2182. dev_priv->hws_obj = obj;
  2183. memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
  2184. I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
  2185. I915_READ(HWS_PGA); /* posting read */
  2186. DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
  2187. return 0;
  2188. }
  2189. static int
  2190. i915_gem_init_ringbuffer(struct drm_device *dev)
  2191. {
  2192. drm_i915_private_t *dev_priv = dev->dev_private;
  2193. struct drm_gem_object *obj;
  2194. struct drm_i915_gem_object *obj_priv;
  2195. int ret;
  2196. u32 head;
  2197. ret = i915_gem_init_hws(dev);
  2198. if (ret != 0)
  2199. return ret;
  2200. obj = drm_gem_object_alloc(dev, 128 * 1024);
  2201. if (obj == NULL) {
  2202. DRM_ERROR("Failed to allocate ringbuffer\n");
  2203. return -ENOMEM;
  2204. }
  2205. obj_priv = obj->driver_private;
  2206. ret = i915_gem_object_pin(obj, 4096);
  2207. if (ret != 0) {
  2208. drm_gem_object_unreference(obj);
  2209. return ret;
  2210. }
  2211. /* Set up the kernel mapping for the ring. */
  2212. dev_priv->ring.Size = obj->size;
  2213. dev_priv->ring.tail_mask = obj->size - 1;
  2214. dev_priv->ring.map.offset = dev->agp->base + obj_priv->gtt_offset;
  2215. dev_priv->ring.map.size = obj->size;
  2216. dev_priv->ring.map.type = 0;
  2217. dev_priv->ring.map.flags = 0;
  2218. dev_priv->ring.map.mtrr = 0;
  2219. drm_core_ioremap_wc(&dev_priv->ring.map, dev);
  2220. if (dev_priv->ring.map.handle == NULL) {
  2221. DRM_ERROR("Failed to map ringbuffer.\n");
  2222. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2223. drm_gem_object_unreference(obj);
  2224. return -EINVAL;
  2225. }
  2226. dev_priv->ring.ring_obj = obj;
  2227. dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
  2228. /* Stop the ring if it's running. */
  2229. I915_WRITE(PRB0_CTL, 0);
  2230. I915_WRITE(PRB0_TAIL, 0);
  2231. I915_WRITE(PRB0_HEAD, 0);
  2232. /* Initialize the ring. */
  2233. I915_WRITE(PRB0_START, obj_priv->gtt_offset);
  2234. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2235. /* G45 ring initialization fails to reset head to zero */
  2236. if (head != 0) {
  2237. DRM_ERROR("Ring head not reset to zero "
  2238. "ctl %08x head %08x tail %08x start %08x\n",
  2239. I915_READ(PRB0_CTL),
  2240. I915_READ(PRB0_HEAD),
  2241. I915_READ(PRB0_TAIL),
  2242. I915_READ(PRB0_START));
  2243. I915_WRITE(PRB0_HEAD, 0);
  2244. DRM_ERROR("Ring head forced to zero "
  2245. "ctl %08x head %08x tail %08x start %08x\n",
  2246. I915_READ(PRB0_CTL),
  2247. I915_READ(PRB0_HEAD),
  2248. I915_READ(PRB0_TAIL),
  2249. I915_READ(PRB0_START));
  2250. }
  2251. I915_WRITE(PRB0_CTL,
  2252. ((obj->size - 4096) & RING_NR_PAGES) |
  2253. RING_NO_REPORT |
  2254. RING_VALID);
  2255. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2256. /* If the head is still not zero, the ring is dead */
  2257. if (head != 0) {
  2258. DRM_ERROR("Ring initialization failed "
  2259. "ctl %08x head %08x tail %08x start %08x\n",
  2260. I915_READ(PRB0_CTL),
  2261. I915_READ(PRB0_HEAD),
  2262. I915_READ(PRB0_TAIL),
  2263. I915_READ(PRB0_START));
  2264. return -EIO;
  2265. }
  2266. /* Update our cache of the ring state */
  2267. i915_kernel_lost_context(dev);
  2268. return 0;
  2269. }
  2270. static void
  2271. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  2272. {
  2273. drm_i915_private_t *dev_priv = dev->dev_private;
  2274. if (dev_priv->ring.ring_obj == NULL)
  2275. return;
  2276. drm_core_ioremapfree(&dev_priv->ring.map, dev);
  2277. i915_gem_object_unpin(dev_priv->ring.ring_obj);
  2278. drm_gem_object_unreference(dev_priv->ring.ring_obj);
  2279. dev_priv->ring.ring_obj = NULL;
  2280. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2281. if (dev_priv->hws_obj != NULL) {
  2282. struct drm_gem_object *obj = dev_priv->hws_obj;
  2283. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2284. kunmap(obj_priv->page_list[0]);
  2285. i915_gem_object_unpin(obj);
  2286. drm_gem_object_unreference(obj);
  2287. dev_priv->hws_obj = NULL;
  2288. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2289. dev_priv->hw_status_page = NULL;
  2290. /* Write high address into HWS_PGA when disabling. */
  2291. I915_WRITE(HWS_PGA, 0x1ffff000);
  2292. }
  2293. }
  2294. int
  2295. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  2296. struct drm_file *file_priv)
  2297. {
  2298. drm_i915_private_t *dev_priv = dev->dev_private;
  2299. int ret;
  2300. if (dev_priv->mm.wedged) {
  2301. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  2302. dev_priv->mm.wedged = 0;
  2303. }
  2304. ret = i915_gem_init_ringbuffer(dev);
  2305. if (ret != 0)
  2306. return ret;
  2307. dev_priv->mm.gtt_mapping = io_mapping_create_wc(dev->agp->base,
  2308. dev->agp->agp_info.aper_size
  2309. * 1024 * 1024);
  2310. mutex_lock(&dev->struct_mutex);
  2311. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  2312. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  2313. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  2314. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  2315. dev_priv->mm.suspended = 0;
  2316. mutex_unlock(&dev->struct_mutex);
  2317. drm_irq_install(dev);
  2318. return 0;
  2319. }
  2320. int
  2321. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  2322. struct drm_file *file_priv)
  2323. {
  2324. drm_i915_private_t *dev_priv = dev->dev_private;
  2325. int ret;
  2326. ret = i915_gem_idle(dev);
  2327. drm_irq_uninstall(dev);
  2328. io_mapping_free(dev_priv->mm.gtt_mapping);
  2329. return ret;
  2330. }
  2331. void
  2332. i915_gem_lastclose(struct drm_device *dev)
  2333. {
  2334. int ret;
  2335. ret = i915_gem_idle(dev);
  2336. if (ret)
  2337. DRM_ERROR("failed to idle hardware: %d\n", ret);
  2338. }
  2339. void
  2340. i915_gem_load(struct drm_device *dev)
  2341. {
  2342. drm_i915_private_t *dev_priv = dev->dev_private;
  2343. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  2344. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  2345. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  2346. INIT_LIST_HEAD(&dev_priv->mm.request_list);
  2347. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  2348. i915_gem_retire_work_handler);
  2349. dev_priv->mm.next_gem_seqno = 1;
  2350. i915_gem_detect_bit_6_swizzle(dev);
  2351. }