i915_gem.c 72 KB

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