i915_gem.c 87 KB

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