i915_gem.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728
  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_object_get_page_list(struct drm_gem_object *obj)
  926. {
  927. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  928. int page_count, i;
  929. struct address_space *mapping;
  930. struct inode *inode;
  931. struct page *page;
  932. int ret;
  933. if (obj_priv->page_list)
  934. return 0;
  935. /* Get the list of pages out of our struct file. They'll be pinned
  936. * at this point until we release them.
  937. */
  938. page_count = obj->size / PAGE_SIZE;
  939. BUG_ON(obj_priv->page_list != NULL);
  940. obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
  941. DRM_MEM_DRIVER);
  942. if (obj_priv->page_list == NULL) {
  943. DRM_ERROR("Faled to allocate page list\n");
  944. return -ENOMEM;
  945. }
  946. inode = obj->filp->f_path.dentry->d_inode;
  947. mapping = inode->i_mapping;
  948. for (i = 0; i < page_count; i++) {
  949. page = read_mapping_page(mapping, i, NULL);
  950. if (IS_ERR(page)) {
  951. ret = PTR_ERR(page);
  952. DRM_ERROR("read_mapping_page failed: %d\n", ret);
  953. i915_gem_object_free_page_list(obj);
  954. return ret;
  955. }
  956. obj_priv->page_list[i] = page;
  957. }
  958. return 0;
  959. }
  960. /**
  961. * Finds free space in the GTT aperture and binds the object there.
  962. */
  963. static int
  964. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  965. {
  966. struct drm_device *dev = obj->dev;
  967. drm_i915_private_t *dev_priv = dev->dev_private;
  968. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  969. struct drm_mm_node *free_space;
  970. int page_count, ret;
  971. if (alignment == 0)
  972. alignment = PAGE_SIZE;
  973. if (alignment & (PAGE_SIZE - 1)) {
  974. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  975. return -EINVAL;
  976. }
  977. search_free:
  978. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  979. obj->size, alignment, 0);
  980. if (free_space != NULL) {
  981. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  982. alignment);
  983. if (obj_priv->gtt_space != NULL) {
  984. obj_priv->gtt_space->private = obj;
  985. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  986. }
  987. }
  988. if (obj_priv->gtt_space == NULL) {
  989. /* If the gtt is empty and we're still having trouble
  990. * fitting our object in, we're out of memory.
  991. */
  992. #if WATCH_LRU
  993. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  994. #endif
  995. if (list_empty(&dev_priv->mm.inactive_list) &&
  996. list_empty(&dev_priv->mm.flushing_list) &&
  997. list_empty(&dev_priv->mm.active_list)) {
  998. DRM_ERROR("GTT full, but LRU list empty\n");
  999. return -ENOMEM;
  1000. }
  1001. ret = i915_gem_evict_something(dev);
  1002. if (ret != 0) {
  1003. DRM_ERROR("Failed to evict a buffer %d\n", ret);
  1004. return ret;
  1005. }
  1006. goto search_free;
  1007. }
  1008. #if WATCH_BUF
  1009. DRM_INFO("Binding object of size %d at 0x%08x\n",
  1010. obj->size, obj_priv->gtt_offset);
  1011. #endif
  1012. ret = i915_gem_object_get_page_list(obj);
  1013. if (ret) {
  1014. drm_mm_put_block(obj_priv->gtt_space);
  1015. obj_priv->gtt_space = NULL;
  1016. return ret;
  1017. }
  1018. page_count = obj->size / PAGE_SIZE;
  1019. /* Create an AGP memory structure pointing at our pages, and bind it
  1020. * into the GTT.
  1021. */
  1022. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  1023. obj_priv->page_list,
  1024. page_count,
  1025. obj_priv->gtt_offset,
  1026. obj_priv->agp_type);
  1027. if (obj_priv->agp_mem == NULL) {
  1028. i915_gem_object_free_page_list(obj);
  1029. drm_mm_put_block(obj_priv->gtt_space);
  1030. obj_priv->gtt_space = NULL;
  1031. return -ENOMEM;
  1032. }
  1033. atomic_inc(&dev->gtt_count);
  1034. atomic_add(obj->size, &dev->gtt_memory);
  1035. /* Assert that the object is not currently in any GPU domain. As it
  1036. * wasn't in the GTT, there shouldn't be any way it could have been in
  1037. * a GPU cache
  1038. */
  1039. BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1040. BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1041. return 0;
  1042. }
  1043. void
  1044. i915_gem_clflush_object(struct drm_gem_object *obj)
  1045. {
  1046. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1047. /* If we don't have a page list set up, then we're not pinned
  1048. * to GPU, and we can ignore the cache flush because it'll happen
  1049. * again at bind time.
  1050. */
  1051. if (obj_priv->page_list == NULL)
  1052. return;
  1053. drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
  1054. }
  1055. /** Flushes any GPU write domain for the object if it's dirty. */
  1056. static void
  1057. i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
  1058. {
  1059. struct drm_device *dev = obj->dev;
  1060. uint32_t seqno;
  1061. if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  1062. return;
  1063. /* Queue the GPU write cache flushing we need. */
  1064. i915_gem_flush(dev, 0, obj->write_domain);
  1065. seqno = i915_add_request(dev, obj->write_domain);
  1066. obj->write_domain = 0;
  1067. i915_gem_object_move_to_active(obj, seqno);
  1068. }
  1069. /** Flushes the GTT write domain for the object if it's dirty. */
  1070. static void
  1071. i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
  1072. {
  1073. if (obj->write_domain != I915_GEM_DOMAIN_GTT)
  1074. return;
  1075. /* No actual flushing is required for the GTT write domain. Writes
  1076. * to it immediately go to main memory as far as we know, so there's
  1077. * no chipset flush. It also doesn't land in render cache.
  1078. */
  1079. obj->write_domain = 0;
  1080. }
  1081. /** Flushes the CPU write domain for the object if it's dirty. */
  1082. static void
  1083. i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
  1084. {
  1085. struct drm_device *dev = obj->dev;
  1086. if (obj->write_domain != I915_GEM_DOMAIN_CPU)
  1087. return;
  1088. i915_gem_clflush_object(obj);
  1089. drm_agp_chipset_flush(dev);
  1090. obj->write_domain = 0;
  1091. }
  1092. /**
  1093. * Moves a single object to the GTT read, and possibly write domain.
  1094. *
  1095. * This function returns when the move is complete, including waiting on
  1096. * flushes to occur.
  1097. */
  1098. static int
  1099. i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
  1100. {
  1101. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1102. int ret;
  1103. i915_gem_object_flush_gpu_write_domain(obj);
  1104. /* Wait on any GPU rendering and flushing to occur. */
  1105. ret = i915_gem_object_wait_rendering(obj);
  1106. if (ret != 0)
  1107. return ret;
  1108. /* If we're writing through the GTT domain, then CPU and GPU caches
  1109. * will need to be invalidated at next use.
  1110. */
  1111. if (write)
  1112. obj->read_domains &= I915_GEM_DOMAIN_GTT;
  1113. i915_gem_object_flush_cpu_write_domain(obj);
  1114. /* It should now be out of any other write domains, and we can update
  1115. * the domain values for our changes.
  1116. */
  1117. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  1118. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  1119. if (write) {
  1120. obj->write_domain = I915_GEM_DOMAIN_GTT;
  1121. obj_priv->dirty = 1;
  1122. }
  1123. return 0;
  1124. }
  1125. /**
  1126. * Moves a single object to the CPU read, and possibly write domain.
  1127. *
  1128. * This function returns when the move is complete, including waiting on
  1129. * flushes to occur.
  1130. */
  1131. static int
  1132. i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
  1133. {
  1134. struct drm_device *dev = obj->dev;
  1135. int ret;
  1136. i915_gem_object_flush_gpu_write_domain(obj);
  1137. /* Wait on any GPU rendering and flushing to occur. */
  1138. ret = i915_gem_object_wait_rendering(obj);
  1139. if (ret != 0)
  1140. return ret;
  1141. i915_gem_object_flush_gtt_write_domain(obj);
  1142. /* If we have a partially-valid cache of the object in the CPU,
  1143. * finish invalidating it and free the per-page flags.
  1144. */
  1145. i915_gem_object_set_to_full_cpu_read_domain(obj);
  1146. /* Flush the CPU cache if it's still invalid. */
  1147. if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  1148. i915_gem_clflush_object(obj);
  1149. drm_agp_chipset_flush(dev);
  1150. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  1151. }
  1152. /* It should now be out of any other write domains, and we can update
  1153. * the domain values for our changes.
  1154. */
  1155. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  1156. /* If we're writing through the CPU, then the GPU read domains will
  1157. * need to be invalidated at next use.
  1158. */
  1159. if (write) {
  1160. obj->read_domains &= I915_GEM_DOMAIN_CPU;
  1161. obj->write_domain = I915_GEM_DOMAIN_CPU;
  1162. }
  1163. return 0;
  1164. }
  1165. /*
  1166. * Set the next domain for the specified object. This
  1167. * may not actually perform the necessary flushing/invaliding though,
  1168. * as that may want to be batched with other set_domain operations
  1169. *
  1170. * This is (we hope) the only really tricky part of gem. The goal
  1171. * is fairly simple -- track which caches hold bits of the object
  1172. * and make sure they remain coherent. A few concrete examples may
  1173. * help to explain how it works. For shorthand, we use the notation
  1174. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  1175. * a pair of read and write domain masks.
  1176. *
  1177. * Case 1: the batch buffer
  1178. *
  1179. * 1. Allocated
  1180. * 2. Written by CPU
  1181. * 3. Mapped to GTT
  1182. * 4. Read by GPU
  1183. * 5. Unmapped from GTT
  1184. * 6. Freed
  1185. *
  1186. * Let's take these a step at a time
  1187. *
  1188. * 1. Allocated
  1189. * Pages allocated from the kernel may still have
  1190. * cache contents, so we set them to (CPU, CPU) always.
  1191. * 2. Written by CPU (using pwrite)
  1192. * The pwrite function calls set_domain (CPU, CPU) and
  1193. * this function does nothing (as nothing changes)
  1194. * 3. Mapped by GTT
  1195. * This function asserts that the object is not
  1196. * currently in any GPU-based read or write domains
  1197. * 4. Read by GPU
  1198. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  1199. * As write_domain is zero, this function adds in the
  1200. * current read domains (CPU+COMMAND, 0).
  1201. * flush_domains is set to CPU.
  1202. * invalidate_domains is set to COMMAND
  1203. * clflush is run to get data out of the CPU caches
  1204. * then i915_dev_set_domain calls i915_gem_flush to
  1205. * emit an MI_FLUSH and drm_agp_chipset_flush
  1206. * 5. Unmapped from GTT
  1207. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  1208. * flush_domains and invalidate_domains end up both zero
  1209. * so no flushing/invalidating happens
  1210. * 6. Freed
  1211. * yay, done
  1212. *
  1213. * Case 2: The shared render buffer
  1214. *
  1215. * 1. Allocated
  1216. * 2. Mapped to GTT
  1217. * 3. Read/written by GPU
  1218. * 4. set_domain to (CPU,CPU)
  1219. * 5. Read/written by CPU
  1220. * 6. Read/written by GPU
  1221. *
  1222. * 1. Allocated
  1223. * Same as last example, (CPU, CPU)
  1224. * 2. Mapped to GTT
  1225. * Nothing changes (assertions find that it is not in the GPU)
  1226. * 3. Read/written by GPU
  1227. * execbuffer calls set_domain (RENDER, RENDER)
  1228. * flush_domains gets CPU
  1229. * invalidate_domains gets GPU
  1230. * clflush (obj)
  1231. * MI_FLUSH and drm_agp_chipset_flush
  1232. * 4. set_domain (CPU, CPU)
  1233. * flush_domains gets GPU
  1234. * invalidate_domains gets CPU
  1235. * wait_rendering (obj) to make sure all drawing is complete.
  1236. * This will include an MI_FLUSH to get the data from GPU
  1237. * to memory
  1238. * clflush (obj) to invalidate the CPU cache
  1239. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  1240. * 5. Read/written by CPU
  1241. * cache lines are loaded and dirtied
  1242. * 6. Read written by GPU
  1243. * Same as last GPU access
  1244. *
  1245. * Case 3: The constant buffer
  1246. *
  1247. * 1. Allocated
  1248. * 2. Written by CPU
  1249. * 3. Read by GPU
  1250. * 4. Updated (written) by CPU again
  1251. * 5. Read by GPU
  1252. *
  1253. * 1. Allocated
  1254. * (CPU, CPU)
  1255. * 2. Written by CPU
  1256. * (CPU, CPU)
  1257. * 3. Read by GPU
  1258. * (CPU+RENDER, 0)
  1259. * flush_domains = CPU
  1260. * invalidate_domains = RENDER
  1261. * clflush (obj)
  1262. * MI_FLUSH
  1263. * drm_agp_chipset_flush
  1264. * 4. Updated (written) by CPU again
  1265. * (CPU, CPU)
  1266. * flush_domains = 0 (no previous write domain)
  1267. * invalidate_domains = 0 (no new read domains)
  1268. * 5. Read by GPU
  1269. * (CPU+RENDER, 0)
  1270. * flush_domains = CPU
  1271. * invalidate_domains = RENDER
  1272. * clflush (obj)
  1273. * MI_FLUSH
  1274. * drm_agp_chipset_flush
  1275. */
  1276. static void
  1277. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
  1278. uint32_t read_domains,
  1279. uint32_t write_domain)
  1280. {
  1281. struct drm_device *dev = obj->dev;
  1282. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1283. uint32_t invalidate_domains = 0;
  1284. uint32_t flush_domains = 0;
  1285. BUG_ON(read_domains & I915_GEM_DOMAIN_CPU);
  1286. BUG_ON(write_domain == I915_GEM_DOMAIN_CPU);
  1287. #if WATCH_BUF
  1288. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  1289. __func__, obj,
  1290. obj->read_domains, read_domains,
  1291. obj->write_domain, write_domain);
  1292. #endif
  1293. /*
  1294. * If the object isn't moving to a new write domain,
  1295. * let the object stay in multiple read domains
  1296. */
  1297. if (write_domain == 0)
  1298. read_domains |= obj->read_domains;
  1299. else
  1300. obj_priv->dirty = 1;
  1301. /*
  1302. * Flush the current write domain if
  1303. * the new read domains don't match. Invalidate
  1304. * any read domains which differ from the old
  1305. * write domain
  1306. */
  1307. if (obj->write_domain && obj->write_domain != read_domains) {
  1308. flush_domains |= obj->write_domain;
  1309. invalidate_domains |= read_domains & ~obj->write_domain;
  1310. }
  1311. /*
  1312. * Invalidate any read caches which may have
  1313. * stale data. That is, any new read domains.
  1314. */
  1315. invalidate_domains |= read_domains & ~obj->read_domains;
  1316. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  1317. #if WATCH_BUF
  1318. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  1319. __func__, flush_domains, invalidate_domains);
  1320. #endif
  1321. i915_gem_clflush_object(obj);
  1322. }
  1323. if ((write_domain | flush_domains) != 0)
  1324. obj->write_domain = write_domain;
  1325. obj->read_domains = read_domains;
  1326. dev->invalidate_domains |= invalidate_domains;
  1327. dev->flush_domains |= flush_domains;
  1328. #if WATCH_BUF
  1329. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  1330. __func__,
  1331. obj->read_domains, obj->write_domain,
  1332. dev->invalidate_domains, dev->flush_domains);
  1333. #endif
  1334. }
  1335. /**
  1336. * Moves the object from a partially CPU read to a full one.
  1337. *
  1338. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  1339. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  1340. */
  1341. static void
  1342. i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
  1343. {
  1344. struct drm_device *dev = obj->dev;
  1345. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1346. if (!obj_priv->page_cpu_valid)
  1347. return;
  1348. /* If we're partially in the CPU read domain, finish moving it in.
  1349. */
  1350. if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
  1351. int i;
  1352. for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
  1353. if (obj_priv->page_cpu_valid[i])
  1354. continue;
  1355. drm_clflush_pages(obj_priv->page_list + i, 1);
  1356. }
  1357. drm_agp_chipset_flush(dev);
  1358. }
  1359. /* Free the page_cpu_valid mappings which are now stale, whether
  1360. * or not we've got I915_GEM_DOMAIN_CPU.
  1361. */
  1362. drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
  1363. DRM_MEM_DRIVER);
  1364. obj_priv->page_cpu_valid = NULL;
  1365. }
  1366. /**
  1367. * Set the CPU read domain on a range of the object.
  1368. *
  1369. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  1370. * not entirely valid. The page_cpu_valid member of the object flags which
  1371. * pages have been flushed, and will be respected by
  1372. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  1373. * of the whole object.
  1374. *
  1375. * This function returns when the move is complete, including waiting on
  1376. * flushes to occur.
  1377. */
  1378. static int
  1379. i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  1380. uint64_t offset, uint64_t size)
  1381. {
  1382. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1383. int i, ret;
  1384. if (offset == 0 && size == obj->size)
  1385. return i915_gem_object_set_to_cpu_domain(obj, 0);
  1386. i915_gem_object_flush_gpu_write_domain(obj);
  1387. /* Wait on any GPU rendering and flushing to occur. */
  1388. ret = i915_gem_object_wait_rendering(obj);
  1389. if (ret != 0)
  1390. return ret;
  1391. i915_gem_object_flush_gtt_write_domain(obj);
  1392. /* If we're already fully in the CPU read domain, we're done. */
  1393. if (obj_priv->page_cpu_valid == NULL &&
  1394. (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
  1395. return 0;
  1396. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  1397. * newly adding I915_GEM_DOMAIN_CPU
  1398. */
  1399. if (obj_priv->page_cpu_valid == NULL) {
  1400. obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
  1401. DRM_MEM_DRIVER);
  1402. if (obj_priv->page_cpu_valid == NULL)
  1403. return -ENOMEM;
  1404. } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
  1405. memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
  1406. /* Flush the cache on any pages that are still invalid from the CPU's
  1407. * perspective.
  1408. */
  1409. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  1410. i++) {
  1411. if (obj_priv->page_cpu_valid[i])
  1412. continue;
  1413. drm_clflush_pages(obj_priv->page_list + i, 1);
  1414. obj_priv->page_cpu_valid[i] = 1;
  1415. }
  1416. /* It should now be out of any other write domains, and we can update
  1417. * the domain values for our changes.
  1418. */
  1419. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  1420. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  1421. return 0;
  1422. }
  1423. /**
  1424. * Once all of the objects have been set in the proper domain,
  1425. * perform the necessary flush and invalidate operations.
  1426. *
  1427. * Returns the write domains flushed, for use in flush tracking.
  1428. */
  1429. static uint32_t
  1430. i915_gem_dev_set_domain(struct drm_device *dev)
  1431. {
  1432. uint32_t flush_domains = dev->flush_domains;
  1433. /*
  1434. * Now that all the buffers are synced to the proper domains,
  1435. * flush and invalidate the collected domains
  1436. */
  1437. if (dev->invalidate_domains | dev->flush_domains) {
  1438. #if WATCH_EXEC
  1439. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  1440. __func__,
  1441. dev->invalidate_domains,
  1442. dev->flush_domains);
  1443. #endif
  1444. i915_gem_flush(dev,
  1445. dev->invalidate_domains,
  1446. dev->flush_domains);
  1447. dev->invalidate_domains = 0;
  1448. dev->flush_domains = 0;
  1449. }
  1450. return flush_domains;
  1451. }
  1452. /**
  1453. * Pin an object to the GTT and evaluate the relocations landing in it.
  1454. */
  1455. static int
  1456. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  1457. struct drm_file *file_priv,
  1458. struct drm_i915_gem_exec_object *entry)
  1459. {
  1460. struct drm_device *dev = obj->dev;
  1461. drm_i915_private_t *dev_priv = dev->dev_private;
  1462. struct drm_i915_gem_relocation_entry reloc;
  1463. struct drm_i915_gem_relocation_entry __user *relocs;
  1464. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1465. int i, ret;
  1466. void __iomem *reloc_page;
  1467. /* Choose the GTT offset for our buffer and put it there. */
  1468. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  1469. if (ret)
  1470. return ret;
  1471. entry->offset = obj_priv->gtt_offset;
  1472. relocs = (struct drm_i915_gem_relocation_entry __user *)
  1473. (uintptr_t) entry->relocs_ptr;
  1474. /* Apply the relocations, using the GTT aperture to avoid cache
  1475. * flushing requirements.
  1476. */
  1477. for (i = 0; i < entry->relocation_count; i++) {
  1478. struct drm_gem_object *target_obj;
  1479. struct drm_i915_gem_object *target_obj_priv;
  1480. uint32_t reloc_val, reloc_offset;
  1481. uint32_t __iomem *reloc_entry;
  1482. ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
  1483. if (ret != 0) {
  1484. i915_gem_object_unpin(obj);
  1485. return ret;
  1486. }
  1487. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  1488. reloc.target_handle);
  1489. if (target_obj == NULL) {
  1490. i915_gem_object_unpin(obj);
  1491. return -EBADF;
  1492. }
  1493. target_obj_priv = target_obj->driver_private;
  1494. /* The target buffer should have appeared before us in the
  1495. * exec_object list, so it should have a GTT space bound by now.
  1496. */
  1497. if (target_obj_priv->gtt_space == NULL) {
  1498. DRM_ERROR("No GTT space found for object %d\n",
  1499. reloc.target_handle);
  1500. drm_gem_object_unreference(target_obj);
  1501. i915_gem_object_unpin(obj);
  1502. return -EINVAL;
  1503. }
  1504. if (reloc.offset > obj->size - 4) {
  1505. DRM_ERROR("Relocation beyond object bounds: "
  1506. "obj %p target %d offset %d size %d.\n",
  1507. obj, reloc.target_handle,
  1508. (int) reloc.offset, (int) obj->size);
  1509. drm_gem_object_unreference(target_obj);
  1510. i915_gem_object_unpin(obj);
  1511. return -EINVAL;
  1512. }
  1513. if (reloc.offset & 3) {
  1514. DRM_ERROR("Relocation not 4-byte aligned: "
  1515. "obj %p target %d offset %d.\n",
  1516. obj, reloc.target_handle,
  1517. (int) reloc.offset);
  1518. drm_gem_object_unreference(target_obj);
  1519. i915_gem_object_unpin(obj);
  1520. return -EINVAL;
  1521. }
  1522. if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
  1523. reloc.read_domains & I915_GEM_DOMAIN_CPU) {
  1524. DRM_ERROR("reloc with read/write CPU domains: "
  1525. "obj %p target %d offset %d "
  1526. "read %08x write %08x",
  1527. obj, reloc.target_handle,
  1528. (int) reloc.offset,
  1529. reloc.read_domains,
  1530. reloc.write_domain);
  1531. return -EINVAL;
  1532. }
  1533. if (reloc.write_domain && target_obj->pending_write_domain &&
  1534. reloc.write_domain != target_obj->pending_write_domain) {
  1535. DRM_ERROR("Write domain conflict: "
  1536. "obj %p target %d offset %d "
  1537. "new %08x old %08x\n",
  1538. obj, reloc.target_handle,
  1539. (int) reloc.offset,
  1540. reloc.write_domain,
  1541. target_obj->pending_write_domain);
  1542. drm_gem_object_unreference(target_obj);
  1543. i915_gem_object_unpin(obj);
  1544. return -EINVAL;
  1545. }
  1546. #if WATCH_RELOC
  1547. DRM_INFO("%s: obj %p offset %08x target %d "
  1548. "read %08x write %08x gtt %08x "
  1549. "presumed %08x delta %08x\n",
  1550. __func__,
  1551. obj,
  1552. (int) reloc.offset,
  1553. (int) reloc.target_handle,
  1554. (int) reloc.read_domains,
  1555. (int) reloc.write_domain,
  1556. (int) target_obj_priv->gtt_offset,
  1557. (int) reloc.presumed_offset,
  1558. reloc.delta);
  1559. #endif
  1560. target_obj->pending_read_domains |= reloc.read_domains;
  1561. target_obj->pending_write_domain |= reloc.write_domain;
  1562. /* If the relocation already has the right value in it, no
  1563. * more work needs to be done.
  1564. */
  1565. if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
  1566. drm_gem_object_unreference(target_obj);
  1567. continue;
  1568. }
  1569. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  1570. if (ret != 0) {
  1571. drm_gem_object_unreference(target_obj);
  1572. i915_gem_object_unpin(obj);
  1573. return -EINVAL;
  1574. }
  1575. /* Map the page containing the relocation we're going to
  1576. * perform.
  1577. */
  1578. reloc_offset = obj_priv->gtt_offset + reloc.offset;
  1579. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  1580. (reloc_offset &
  1581. ~(PAGE_SIZE - 1)));
  1582. reloc_entry = (uint32_t __iomem *)(reloc_page +
  1583. (reloc_offset & (PAGE_SIZE - 1)));
  1584. reloc_val = target_obj_priv->gtt_offset + reloc.delta;
  1585. #if WATCH_BUF
  1586. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  1587. obj, (unsigned int) reloc.offset,
  1588. readl(reloc_entry), reloc_val);
  1589. #endif
  1590. writel(reloc_val, reloc_entry);
  1591. io_mapping_unmap_atomic(reloc_page);
  1592. /* Write the updated presumed offset for this entry back out
  1593. * to the user.
  1594. */
  1595. reloc.presumed_offset = target_obj_priv->gtt_offset;
  1596. ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
  1597. if (ret != 0) {
  1598. drm_gem_object_unreference(target_obj);
  1599. i915_gem_object_unpin(obj);
  1600. return ret;
  1601. }
  1602. drm_gem_object_unreference(target_obj);
  1603. }
  1604. #if WATCH_BUF
  1605. if (0)
  1606. i915_gem_dump_object(obj, 128, __func__, ~0);
  1607. #endif
  1608. return 0;
  1609. }
  1610. /** Dispatch a batchbuffer to the ring
  1611. */
  1612. static int
  1613. i915_dispatch_gem_execbuffer(struct drm_device *dev,
  1614. struct drm_i915_gem_execbuffer *exec,
  1615. uint64_t exec_offset)
  1616. {
  1617. drm_i915_private_t *dev_priv = dev->dev_private;
  1618. struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
  1619. (uintptr_t) exec->cliprects_ptr;
  1620. int nbox = exec->num_cliprects;
  1621. int i = 0, count;
  1622. uint32_t exec_start, exec_len;
  1623. RING_LOCALS;
  1624. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  1625. exec_len = (uint32_t) exec->batch_len;
  1626. if ((exec_start | exec_len) & 0x7) {
  1627. DRM_ERROR("alignment\n");
  1628. return -EINVAL;
  1629. }
  1630. if (!exec_start)
  1631. return -EINVAL;
  1632. count = nbox ? nbox : 1;
  1633. for (i = 0; i < count; i++) {
  1634. if (i < nbox) {
  1635. int ret = i915_emit_box(dev, boxes, i,
  1636. exec->DR1, exec->DR4);
  1637. if (ret)
  1638. return ret;
  1639. }
  1640. if (IS_I830(dev) || IS_845G(dev)) {
  1641. BEGIN_LP_RING(4);
  1642. OUT_RING(MI_BATCH_BUFFER);
  1643. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  1644. OUT_RING(exec_start + exec_len - 4);
  1645. OUT_RING(0);
  1646. ADVANCE_LP_RING();
  1647. } else {
  1648. BEGIN_LP_RING(2);
  1649. if (IS_I965G(dev)) {
  1650. OUT_RING(MI_BATCH_BUFFER_START |
  1651. (2 << 6) |
  1652. MI_BATCH_NON_SECURE_I965);
  1653. OUT_RING(exec_start);
  1654. } else {
  1655. OUT_RING(MI_BATCH_BUFFER_START |
  1656. (2 << 6));
  1657. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  1658. }
  1659. ADVANCE_LP_RING();
  1660. }
  1661. }
  1662. /* XXX breadcrumb */
  1663. return 0;
  1664. }
  1665. /* Throttle our rendering by waiting until the ring has completed our requests
  1666. * emitted over 20 msec ago.
  1667. *
  1668. * This should get us reasonable parallelism between CPU and GPU but also
  1669. * relatively low latency when blocking on a particular request to finish.
  1670. */
  1671. static int
  1672. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  1673. {
  1674. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  1675. int ret = 0;
  1676. uint32_t seqno;
  1677. mutex_lock(&dev->struct_mutex);
  1678. seqno = i915_file_priv->mm.last_gem_throttle_seqno;
  1679. i915_file_priv->mm.last_gem_throttle_seqno =
  1680. i915_file_priv->mm.last_gem_seqno;
  1681. if (seqno)
  1682. ret = i915_wait_request(dev, seqno);
  1683. mutex_unlock(&dev->struct_mutex);
  1684. return ret;
  1685. }
  1686. int
  1687. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1688. struct drm_file *file_priv)
  1689. {
  1690. drm_i915_private_t *dev_priv = dev->dev_private;
  1691. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  1692. struct drm_i915_gem_execbuffer *args = data;
  1693. struct drm_i915_gem_exec_object *exec_list = NULL;
  1694. struct drm_gem_object **object_list = NULL;
  1695. struct drm_gem_object *batch_obj;
  1696. int ret, i, pinned = 0;
  1697. uint64_t exec_offset;
  1698. uint32_t seqno, flush_domains;
  1699. #if WATCH_EXEC
  1700. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  1701. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  1702. #endif
  1703. if (args->buffer_count < 1) {
  1704. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  1705. return -EINVAL;
  1706. }
  1707. /* Copy in the exec list from userland */
  1708. exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
  1709. DRM_MEM_DRIVER);
  1710. object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
  1711. DRM_MEM_DRIVER);
  1712. if (exec_list == NULL || object_list == NULL) {
  1713. DRM_ERROR("Failed to allocate exec or object list "
  1714. "for %d buffers\n",
  1715. args->buffer_count);
  1716. ret = -ENOMEM;
  1717. goto pre_mutex_err;
  1718. }
  1719. ret = copy_from_user(exec_list,
  1720. (struct drm_i915_relocation_entry __user *)
  1721. (uintptr_t) args->buffers_ptr,
  1722. sizeof(*exec_list) * args->buffer_count);
  1723. if (ret != 0) {
  1724. DRM_ERROR("copy %d exec entries failed %d\n",
  1725. args->buffer_count, ret);
  1726. goto pre_mutex_err;
  1727. }
  1728. mutex_lock(&dev->struct_mutex);
  1729. i915_verify_inactive(dev, __FILE__, __LINE__);
  1730. if (dev_priv->mm.wedged) {
  1731. DRM_ERROR("Execbuf while wedged\n");
  1732. mutex_unlock(&dev->struct_mutex);
  1733. return -EIO;
  1734. }
  1735. if (dev_priv->mm.suspended) {
  1736. DRM_ERROR("Execbuf while VT-switched.\n");
  1737. mutex_unlock(&dev->struct_mutex);
  1738. return -EBUSY;
  1739. }
  1740. /* Zero the gloabl flush/invalidate flags. These
  1741. * will be modified as each object is bound to the
  1742. * gtt
  1743. */
  1744. dev->invalidate_domains = 0;
  1745. dev->flush_domains = 0;
  1746. /* Look up object handles and perform the relocations */
  1747. for (i = 0; i < args->buffer_count; i++) {
  1748. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  1749. exec_list[i].handle);
  1750. if (object_list[i] == NULL) {
  1751. DRM_ERROR("Invalid object handle %d at index %d\n",
  1752. exec_list[i].handle, i);
  1753. ret = -EBADF;
  1754. goto err;
  1755. }
  1756. object_list[i]->pending_read_domains = 0;
  1757. object_list[i]->pending_write_domain = 0;
  1758. ret = i915_gem_object_pin_and_relocate(object_list[i],
  1759. file_priv,
  1760. &exec_list[i]);
  1761. if (ret) {
  1762. DRM_ERROR("object bind and relocate failed %d\n", ret);
  1763. goto err;
  1764. }
  1765. pinned = i + 1;
  1766. }
  1767. /* Set the pending read domains for the batch buffer to COMMAND */
  1768. batch_obj = object_list[args->buffer_count-1];
  1769. batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  1770. batch_obj->pending_write_domain = 0;
  1771. i915_verify_inactive(dev, __FILE__, __LINE__);
  1772. for (i = 0; i < args->buffer_count; i++) {
  1773. struct drm_gem_object *obj = object_list[i];
  1774. /* Compute new gpu domains and update invalidate/flushing */
  1775. i915_gem_object_set_to_gpu_domain(obj,
  1776. obj->pending_read_domains,
  1777. obj->pending_write_domain);
  1778. }
  1779. i915_verify_inactive(dev, __FILE__, __LINE__);
  1780. /* Flush/invalidate caches and chipset buffer */
  1781. flush_domains = i915_gem_dev_set_domain(dev);
  1782. i915_verify_inactive(dev, __FILE__, __LINE__);
  1783. #if WATCH_COHERENCY
  1784. for (i = 0; i < args->buffer_count; i++) {
  1785. i915_gem_object_check_coherency(object_list[i],
  1786. exec_list[i].handle);
  1787. }
  1788. #endif
  1789. exec_offset = exec_list[args->buffer_count - 1].offset;
  1790. #if WATCH_EXEC
  1791. i915_gem_dump_object(object_list[args->buffer_count - 1],
  1792. args->batch_len,
  1793. __func__,
  1794. ~0);
  1795. #endif
  1796. (void)i915_add_request(dev, flush_domains);
  1797. /* Exec the batchbuffer */
  1798. ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
  1799. if (ret) {
  1800. DRM_ERROR("dispatch failed %d\n", ret);
  1801. goto err;
  1802. }
  1803. /*
  1804. * Ensure that the commands in the batch buffer are
  1805. * finished before the interrupt fires
  1806. */
  1807. flush_domains = i915_retire_commands(dev);
  1808. i915_verify_inactive(dev, __FILE__, __LINE__);
  1809. /*
  1810. * Get a seqno representing the execution of the current buffer,
  1811. * which we can wait on. We would like to mitigate these interrupts,
  1812. * likely by only creating seqnos occasionally (so that we have
  1813. * *some* interrupts representing completion of buffers that we can
  1814. * wait on when trying to clear up gtt space).
  1815. */
  1816. seqno = i915_add_request(dev, flush_domains);
  1817. BUG_ON(seqno == 0);
  1818. i915_file_priv->mm.last_gem_seqno = seqno;
  1819. for (i = 0; i < args->buffer_count; i++) {
  1820. struct drm_gem_object *obj = object_list[i];
  1821. i915_gem_object_move_to_active(obj, seqno);
  1822. #if WATCH_LRU
  1823. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  1824. #endif
  1825. }
  1826. #if WATCH_LRU
  1827. i915_dump_lru(dev, __func__);
  1828. #endif
  1829. i915_verify_inactive(dev, __FILE__, __LINE__);
  1830. /* Copy the new buffer offsets back to the user's exec list. */
  1831. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1832. (uintptr_t) args->buffers_ptr,
  1833. exec_list,
  1834. sizeof(*exec_list) * args->buffer_count);
  1835. if (ret)
  1836. DRM_ERROR("failed to copy %d exec entries "
  1837. "back to user (%d)\n",
  1838. args->buffer_count, ret);
  1839. err:
  1840. if (object_list != NULL) {
  1841. for (i = 0; i < pinned; i++)
  1842. i915_gem_object_unpin(object_list[i]);
  1843. for (i = 0; i < args->buffer_count; i++)
  1844. drm_gem_object_unreference(object_list[i]);
  1845. }
  1846. mutex_unlock(&dev->struct_mutex);
  1847. pre_mutex_err:
  1848. drm_free(object_list, sizeof(*object_list) * args->buffer_count,
  1849. DRM_MEM_DRIVER);
  1850. drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
  1851. DRM_MEM_DRIVER);
  1852. return ret;
  1853. }
  1854. int
  1855. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  1856. {
  1857. struct drm_device *dev = obj->dev;
  1858. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1859. int ret;
  1860. i915_verify_inactive(dev, __FILE__, __LINE__);
  1861. if (obj_priv->gtt_space == NULL) {
  1862. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  1863. if (ret != 0) {
  1864. DRM_ERROR("Failure to bind: %d", ret);
  1865. return ret;
  1866. }
  1867. }
  1868. obj_priv->pin_count++;
  1869. /* If the object is not active and not pending a flush,
  1870. * remove it from the inactive list
  1871. */
  1872. if (obj_priv->pin_count == 1) {
  1873. atomic_inc(&dev->pin_count);
  1874. atomic_add(obj->size, &dev->pin_memory);
  1875. if (!obj_priv->active &&
  1876. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  1877. I915_GEM_DOMAIN_GTT)) == 0 &&
  1878. !list_empty(&obj_priv->list))
  1879. list_del_init(&obj_priv->list);
  1880. }
  1881. i915_verify_inactive(dev, __FILE__, __LINE__);
  1882. return 0;
  1883. }
  1884. void
  1885. i915_gem_object_unpin(struct drm_gem_object *obj)
  1886. {
  1887. struct drm_device *dev = obj->dev;
  1888. drm_i915_private_t *dev_priv = dev->dev_private;
  1889. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1890. i915_verify_inactive(dev, __FILE__, __LINE__);
  1891. obj_priv->pin_count--;
  1892. BUG_ON(obj_priv->pin_count < 0);
  1893. BUG_ON(obj_priv->gtt_space == NULL);
  1894. /* If the object is no longer pinned, and is
  1895. * neither active nor being flushed, then stick it on
  1896. * the inactive list
  1897. */
  1898. if (obj_priv->pin_count == 0) {
  1899. if (!obj_priv->active &&
  1900. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  1901. I915_GEM_DOMAIN_GTT)) == 0)
  1902. list_move_tail(&obj_priv->list,
  1903. &dev_priv->mm.inactive_list);
  1904. atomic_dec(&dev->pin_count);
  1905. atomic_sub(obj->size, &dev->pin_memory);
  1906. }
  1907. i915_verify_inactive(dev, __FILE__, __LINE__);
  1908. }
  1909. int
  1910. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  1911. struct drm_file *file_priv)
  1912. {
  1913. struct drm_i915_gem_pin *args = data;
  1914. struct drm_gem_object *obj;
  1915. struct drm_i915_gem_object *obj_priv;
  1916. int ret;
  1917. mutex_lock(&dev->struct_mutex);
  1918. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1919. if (obj == NULL) {
  1920. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  1921. args->handle);
  1922. mutex_unlock(&dev->struct_mutex);
  1923. return -EBADF;
  1924. }
  1925. obj_priv = obj->driver_private;
  1926. ret = i915_gem_object_pin(obj, args->alignment);
  1927. if (ret != 0) {
  1928. drm_gem_object_unreference(obj);
  1929. mutex_unlock(&dev->struct_mutex);
  1930. return ret;
  1931. }
  1932. /* XXX - flush the CPU caches for pinned objects
  1933. * as the X server doesn't manage domains yet
  1934. */
  1935. i915_gem_object_flush_cpu_write_domain(obj);
  1936. args->offset = obj_priv->gtt_offset;
  1937. drm_gem_object_unreference(obj);
  1938. mutex_unlock(&dev->struct_mutex);
  1939. return 0;
  1940. }
  1941. int
  1942. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  1943. struct drm_file *file_priv)
  1944. {
  1945. struct drm_i915_gem_pin *args = data;
  1946. struct drm_gem_object *obj;
  1947. mutex_lock(&dev->struct_mutex);
  1948. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1949. if (obj == NULL) {
  1950. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  1951. args->handle);
  1952. mutex_unlock(&dev->struct_mutex);
  1953. return -EBADF;
  1954. }
  1955. i915_gem_object_unpin(obj);
  1956. drm_gem_object_unreference(obj);
  1957. mutex_unlock(&dev->struct_mutex);
  1958. return 0;
  1959. }
  1960. int
  1961. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  1962. struct drm_file *file_priv)
  1963. {
  1964. struct drm_i915_gem_busy *args = data;
  1965. struct drm_gem_object *obj;
  1966. struct drm_i915_gem_object *obj_priv;
  1967. mutex_lock(&dev->struct_mutex);
  1968. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1969. if (obj == NULL) {
  1970. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  1971. args->handle);
  1972. mutex_unlock(&dev->struct_mutex);
  1973. return -EBADF;
  1974. }
  1975. obj_priv = obj->driver_private;
  1976. args->busy = obj_priv->active;
  1977. drm_gem_object_unreference(obj);
  1978. mutex_unlock(&dev->struct_mutex);
  1979. return 0;
  1980. }
  1981. int
  1982. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  1983. struct drm_file *file_priv)
  1984. {
  1985. return i915_gem_ring_throttle(dev, file_priv);
  1986. }
  1987. int i915_gem_init_object(struct drm_gem_object *obj)
  1988. {
  1989. struct drm_i915_gem_object *obj_priv;
  1990. obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
  1991. if (obj_priv == NULL)
  1992. return -ENOMEM;
  1993. /*
  1994. * We've just allocated pages from the kernel,
  1995. * so they've just been written by the CPU with
  1996. * zeros. They'll need to be clflushed before we
  1997. * use them with the GPU.
  1998. */
  1999. obj->write_domain = I915_GEM_DOMAIN_CPU;
  2000. obj->read_domains = I915_GEM_DOMAIN_CPU;
  2001. obj_priv->agp_type = AGP_USER_MEMORY;
  2002. obj->driver_private = obj_priv;
  2003. obj_priv->obj = obj;
  2004. INIT_LIST_HEAD(&obj_priv->list);
  2005. return 0;
  2006. }
  2007. void i915_gem_free_object(struct drm_gem_object *obj)
  2008. {
  2009. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2010. while (obj_priv->pin_count > 0)
  2011. i915_gem_object_unpin(obj);
  2012. i915_gem_object_unbind(obj);
  2013. drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
  2014. drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
  2015. }
  2016. /** Unbinds all objects that are on the given buffer list. */
  2017. static int
  2018. i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
  2019. {
  2020. struct drm_gem_object *obj;
  2021. struct drm_i915_gem_object *obj_priv;
  2022. int ret;
  2023. while (!list_empty(head)) {
  2024. obj_priv = list_first_entry(head,
  2025. struct drm_i915_gem_object,
  2026. list);
  2027. obj = obj_priv->obj;
  2028. if (obj_priv->pin_count != 0) {
  2029. DRM_ERROR("Pinned object in unbind list\n");
  2030. mutex_unlock(&dev->struct_mutex);
  2031. return -EINVAL;
  2032. }
  2033. ret = i915_gem_object_unbind(obj);
  2034. if (ret != 0) {
  2035. DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
  2036. ret);
  2037. mutex_unlock(&dev->struct_mutex);
  2038. return ret;
  2039. }
  2040. }
  2041. return 0;
  2042. }
  2043. static int
  2044. i915_gem_idle(struct drm_device *dev)
  2045. {
  2046. drm_i915_private_t *dev_priv = dev->dev_private;
  2047. uint32_t seqno, cur_seqno, last_seqno;
  2048. int stuck, ret;
  2049. mutex_lock(&dev->struct_mutex);
  2050. if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
  2051. mutex_unlock(&dev->struct_mutex);
  2052. return 0;
  2053. }
  2054. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  2055. * We need to replace this with a semaphore, or something.
  2056. */
  2057. dev_priv->mm.suspended = 1;
  2058. /* Cancel the retire work handler, wait for it to finish if running
  2059. */
  2060. mutex_unlock(&dev->struct_mutex);
  2061. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  2062. mutex_lock(&dev->struct_mutex);
  2063. i915_kernel_lost_context(dev);
  2064. /* Flush the GPU along with all non-CPU write domains
  2065. */
  2066. i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
  2067. ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  2068. seqno = i915_add_request(dev, ~(I915_GEM_DOMAIN_CPU |
  2069. I915_GEM_DOMAIN_GTT));
  2070. if (seqno == 0) {
  2071. mutex_unlock(&dev->struct_mutex);
  2072. return -ENOMEM;
  2073. }
  2074. dev_priv->mm.waiting_gem_seqno = seqno;
  2075. last_seqno = 0;
  2076. stuck = 0;
  2077. for (;;) {
  2078. cur_seqno = i915_get_gem_seqno(dev);
  2079. if (i915_seqno_passed(cur_seqno, seqno))
  2080. break;
  2081. if (last_seqno == cur_seqno) {
  2082. if (stuck++ > 100) {
  2083. DRM_ERROR("hardware wedged\n");
  2084. dev_priv->mm.wedged = 1;
  2085. DRM_WAKEUP(&dev_priv->irq_queue);
  2086. break;
  2087. }
  2088. }
  2089. msleep(10);
  2090. last_seqno = cur_seqno;
  2091. }
  2092. dev_priv->mm.waiting_gem_seqno = 0;
  2093. i915_gem_retire_requests(dev);
  2094. if (!dev_priv->mm.wedged) {
  2095. /* Active and flushing should now be empty as we've
  2096. * waited for a sequence higher than any pending execbuffer
  2097. */
  2098. WARN_ON(!list_empty(&dev_priv->mm.active_list));
  2099. WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
  2100. /* Request should now be empty as we've also waited
  2101. * for the last request in the list
  2102. */
  2103. WARN_ON(!list_empty(&dev_priv->mm.request_list));
  2104. }
  2105. /* Empty the active and flushing lists to inactive. If there's
  2106. * anything left at this point, it means that we're wedged and
  2107. * nothing good's going to happen by leaving them there. So strip
  2108. * the GPU domains and just stuff them onto inactive.
  2109. */
  2110. while (!list_empty(&dev_priv->mm.active_list)) {
  2111. struct drm_i915_gem_object *obj_priv;
  2112. obj_priv = list_first_entry(&dev_priv->mm.active_list,
  2113. struct drm_i915_gem_object,
  2114. list);
  2115. obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
  2116. i915_gem_object_move_to_inactive(obj_priv->obj);
  2117. }
  2118. while (!list_empty(&dev_priv->mm.flushing_list)) {
  2119. struct drm_i915_gem_object *obj_priv;
  2120. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  2121. struct drm_i915_gem_object,
  2122. list);
  2123. obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
  2124. i915_gem_object_move_to_inactive(obj_priv->obj);
  2125. }
  2126. /* Move all inactive buffers out of the GTT. */
  2127. ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
  2128. WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
  2129. if (ret) {
  2130. mutex_unlock(&dev->struct_mutex);
  2131. return ret;
  2132. }
  2133. i915_gem_cleanup_ringbuffer(dev);
  2134. mutex_unlock(&dev->struct_mutex);
  2135. return 0;
  2136. }
  2137. static int
  2138. i915_gem_init_hws(struct drm_device *dev)
  2139. {
  2140. drm_i915_private_t *dev_priv = dev->dev_private;
  2141. struct drm_gem_object *obj;
  2142. struct drm_i915_gem_object *obj_priv;
  2143. int ret;
  2144. /* If we need a physical address for the status page, it's already
  2145. * initialized at driver load time.
  2146. */
  2147. if (!I915_NEED_GFX_HWS(dev))
  2148. return 0;
  2149. obj = drm_gem_object_alloc(dev, 4096);
  2150. if (obj == NULL) {
  2151. DRM_ERROR("Failed to allocate status page\n");
  2152. return -ENOMEM;
  2153. }
  2154. obj_priv = obj->driver_private;
  2155. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  2156. ret = i915_gem_object_pin(obj, 4096);
  2157. if (ret != 0) {
  2158. drm_gem_object_unreference(obj);
  2159. return ret;
  2160. }
  2161. dev_priv->status_gfx_addr = obj_priv->gtt_offset;
  2162. dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
  2163. if (dev_priv->hw_status_page == NULL) {
  2164. DRM_ERROR("Failed to map status page.\n");
  2165. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2166. drm_gem_object_unreference(obj);
  2167. return -EINVAL;
  2168. }
  2169. dev_priv->hws_obj = obj;
  2170. memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
  2171. I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
  2172. I915_READ(HWS_PGA); /* posting read */
  2173. DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
  2174. return 0;
  2175. }
  2176. static int
  2177. i915_gem_init_ringbuffer(struct drm_device *dev)
  2178. {
  2179. drm_i915_private_t *dev_priv = dev->dev_private;
  2180. struct drm_gem_object *obj;
  2181. struct drm_i915_gem_object *obj_priv;
  2182. int ret;
  2183. u32 head;
  2184. ret = i915_gem_init_hws(dev);
  2185. if (ret != 0)
  2186. return ret;
  2187. obj = drm_gem_object_alloc(dev, 128 * 1024);
  2188. if (obj == NULL) {
  2189. DRM_ERROR("Failed to allocate ringbuffer\n");
  2190. return -ENOMEM;
  2191. }
  2192. obj_priv = obj->driver_private;
  2193. ret = i915_gem_object_pin(obj, 4096);
  2194. if (ret != 0) {
  2195. drm_gem_object_unreference(obj);
  2196. return ret;
  2197. }
  2198. /* Set up the kernel mapping for the ring. */
  2199. dev_priv->ring.Size = obj->size;
  2200. dev_priv->ring.tail_mask = obj->size - 1;
  2201. dev_priv->ring.map.offset = dev->agp->base + obj_priv->gtt_offset;
  2202. dev_priv->ring.map.size = obj->size;
  2203. dev_priv->ring.map.type = 0;
  2204. dev_priv->ring.map.flags = 0;
  2205. dev_priv->ring.map.mtrr = 0;
  2206. drm_core_ioremap_wc(&dev_priv->ring.map, dev);
  2207. if (dev_priv->ring.map.handle == NULL) {
  2208. DRM_ERROR("Failed to map ringbuffer.\n");
  2209. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2210. drm_gem_object_unreference(obj);
  2211. return -EINVAL;
  2212. }
  2213. dev_priv->ring.ring_obj = obj;
  2214. dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
  2215. /* Stop the ring if it's running. */
  2216. I915_WRITE(PRB0_CTL, 0);
  2217. I915_WRITE(PRB0_TAIL, 0);
  2218. I915_WRITE(PRB0_HEAD, 0);
  2219. /* Initialize the ring. */
  2220. I915_WRITE(PRB0_START, obj_priv->gtt_offset);
  2221. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2222. /* G45 ring initialization fails to reset head to zero */
  2223. if (head != 0) {
  2224. DRM_ERROR("Ring head not reset to zero "
  2225. "ctl %08x head %08x tail %08x start %08x\n",
  2226. I915_READ(PRB0_CTL),
  2227. I915_READ(PRB0_HEAD),
  2228. I915_READ(PRB0_TAIL),
  2229. I915_READ(PRB0_START));
  2230. I915_WRITE(PRB0_HEAD, 0);
  2231. DRM_ERROR("Ring head forced to zero "
  2232. "ctl %08x head %08x tail %08x start %08x\n",
  2233. I915_READ(PRB0_CTL),
  2234. I915_READ(PRB0_HEAD),
  2235. I915_READ(PRB0_TAIL),
  2236. I915_READ(PRB0_START));
  2237. }
  2238. I915_WRITE(PRB0_CTL,
  2239. ((obj->size - 4096) & RING_NR_PAGES) |
  2240. RING_NO_REPORT |
  2241. RING_VALID);
  2242. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2243. /* If the head is still not zero, the ring is dead */
  2244. if (head != 0) {
  2245. DRM_ERROR("Ring initialization failed "
  2246. "ctl %08x head %08x tail %08x start %08x\n",
  2247. I915_READ(PRB0_CTL),
  2248. I915_READ(PRB0_HEAD),
  2249. I915_READ(PRB0_TAIL),
  2250. I915_READ(PRB0_START));
  2251. return -EIO;
  2252. }
  2253. /* Update our cache of the ring state */
  2254. i915_kernel_lost_context(dev);
  2255. return 0;
  2256. }
  2257. static void
  2258. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  2259. {
  2260. drm_i915_private_t *dev_priv = dev->dev_private;
  2261. if (dev_priv->ring.ring_obj == NULL)
  2262. return;
  2263. drm_core_ioremapfree(&dev_priv->ring.map, dev);
  2264. i915_gem_object_unpin(dev_priv->ring.ring_obj);
  2265. drm_gem_object_unreference(dev_priv->ring.ring_obj);
  2266. dev_priv->ring.ring_obj = NULL;
  2267. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2268. if (dev_priv->hws_obj != NULL) {
  2269. struct drm_gem_object *obj = dev_priv->hws_obj;
  2270. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2271. kunmap(obj_priv->page_list[0]);
  2272. i915_gem_object_unpin(obj);
  2273. drm_gem_object_unreference(obj);
  2274. dev_priv->hws_obj = NULL;
  2275. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2276. dev_priv->hw_status_page = NULL;
  2277. /* Write high address into HWS_PGA when disabling. */
  2278. I915_WRITE(HWS_PGA, 0x1ffff000);
  2279. }
  2280. }
  2281. int
  2282. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  2283. struct drm_file *file_priv)
  2284. {
  2285. drm_i915_private_t *dev_priv = dev->dev_private;
  2286. int ret;
  2287. if (dev_priv->mm.wedged) {
  2288. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  2289. dev_priv->mm.wedged = 0;
  2290. }
  2291. ret = i915_gem_init_ringbuffer(dev);
  2292. if (ret != 0)
  2293. return ret;
  2294. dev_priv->mm.gtt_mapping = io_mapping_create_wc(dev->agp->base,
  2295. dev->agp->agp_info.aper_size
  2296. * 1024 * 1024);
  2297. mutex_lock(&dev->struct_mutex);
  2298. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  2299. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  2300. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  2301. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  2302. dev_priv->mm.suspended = 0;
  2303. mutex_unlock(&dev->struct_mutex);
  2304. drm_irq_install(dev);
  2305. return 0;
  2306. }
  2307. int
  2308. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  2309. struct drm_file *file_priv)
  2310. {
  2311. drm_i915_private_t *dev_priv = dev->dev_private;
  2312. int ret;
  2313. ret = i915_gem_idle(dev);
  2314. drm_irq_uninstall(dev);
  2315. io_mapping_free(dev_priv->mm.gtt_mapping);
  2316. return ret;
  2317. }
  2318. void
  2319. i915_gem_lastclose(struct drm_device *dev)
  2320. {
  2321. int ret;
  2322. ret = i915_gem_idle(dev);
  2323. if (ret)
  2324. DRM_ERROR("failed to idle hardware: %d\n", ret);
  2325. }
  2326. void
  2327. i915_gem_load(struct drm_device *dev)
  2328. {
  2329. drm_i915_private_t *dev_priv = dev->dev_private;
  2330. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  2331. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  2332. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  2333. INIT_LIST_HEAD(&dev_priv->mm.request_list);
  2334. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  2335. i915_gem_retire_work_handler);
  2336. dev_priv->mm.next_gem_seqno = 1;
  2337. i915_gem_detect_bit_6_swizzle(dev);
  2338. }