i915_gem.c 67 KB

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