i915_gem.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292
  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 (alignment == 0)
  1389. alignment = PAGE_SIZE;
  1390. if (alignment & (PAGE_SIZE - 1)) {
  1391. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  1392. return -EINVAL;
  1393. }
  1394. search_free:
  1395. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  1396. obj->size, alignment, 0);
  1397. if (free_space != NULL) {
  1398. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  1399. alignment);
  1400. if (obj_priv->gtt_space != NULL) {
  1401. obj_priv->gtt_space->private = obj;
  1402. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  1403. }
  1404. }
  1405. if (obj_priv->gtt_space == NULL) {
  1406. /* If the gtt is empty and we're still having trouble
  1407. * fitting our object in, we're out of memory.
  1408. */
  1409. #if WATCH_LRU
  1410. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  1411. #endif
  1412. if (list_empty(&dev_priv->mm.inactive_list) &&
  1413. list_empty(&dev_priv->mm.flushing_list) &&
  1414. list_empty(&dev_priv->mm.active_list)) {
  1415. DRM_ERROR("GTT full, but LRU list empty\n");
  1416. return -ENOMEM;
  1417. }
  1418. ret = i915_gem_evict_something(dev);
  1419. if (ret != 0) {
  1420. if (ret != -ERESTARTSYS)
  1421. DRM_ERROR("Failed to evict a buffer %d\n", ret);
  1422. return ret;
  1423. }
  1424. goto search_free;
  1425. }
  1426. #if WATCH_BUF
  1427. DRM_INFO("Binding object of size %d at 0x%08x\n",
  1428. obj->size, obj_priv->gtt_offset);
  1429. #endif
  1430. ret = i915_gem_object_get_page_list(obj);
  1431. if (ret) {
  1432. drm_mm_put_block(obj_priv->gtt_space);
  1433. obj_priv->gtt_space = NULL;
  1434. return ret;
  1435. }
  1436. page_count = obj->size / PAGE_SIZE;
  1437. /* Create an AGP memory structure pointing at our pages, and bind it
  1438. * into the GTT.
  1439. */
  1440. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  1441. obj_priv->page_list,
  1442. page_count,
  1443. obj_priv->gtt_offset,
  1444. obj_priv->agp_type);
  1445. if (obj_priv->agp_mem == NULL) {
  1446. i915_gem_object_free_page_list(obj);
  1447. drm_mm_put_block(obj_priv->gtt_space);
  1448. obj_priv->gtt_space = NULL;
  1449. return -ENOMEM;
  1450. }
  1451. atomic_inc(&dev->gtt_count);
  1452. atomic_add(obj->size, &dev->gtt_memory);
  1453. /* Assert that the object is not currently in any GPU domain. As it
  1454. * wasn't in the GTT, there shouldn't be any way it could have been in
  1455. * a GPU cache
  1456. */
  1457. BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1458. BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1459. return 0;
  1460. }
  1461. void
  1462. i915_gem_clflush_object(struct drm_gem_object *obj)
  1463. {
  1464. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1465. /* If we don't have a page list set up, then we're not pinned
  1466. * to GPU, and we can ignore the cache flush because it'll happen
  1467. * again at bind time.
  1468. */
  1469. if (obj_priv->page_list == NULL)
  1470. return;
  1471. drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
  1472. }
  1473. /** Flushes any GPU write domain for the object if it's dirty. */
  1474. static void
  1475. i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
  1476. {
  1477. struct drm_device *dev = obj->dev;
  1478. uint32_t seqno;
  1479. if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  1480. return;
  1481. /* Queue the GPU write cache flushing we need. */
  1482. i915_gem_flush(dev, 0, obj->write_domain);
  1483. seqno = i915_add_request(dev, obj->write_domain);
  1484. obj->write_domain = 0;
  1485. i915_gem_object_move_to_active(obj, seqno);
  1486. }
  1487. /** Flushes the GTT write domain for the object if it's dirty. */
  1488. static void
  1489. i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
  1490. {
  1491. if (obj->write_domain != I915_GEM_DOMAIN_GTT)
  1492. return;
  1493. /* No actual flushing is required for the GTT write domain. Writes
  1494. * to it immediately go to main memory as far as we know, so there's
  1495. * no chipset flush. It also doesn't land in render cache.
  1496. */
  1497. obj->write_domain = 0;
  1498. }
  1499. /** Flushes the CPU write domain for the object if it's dirty. */
  1500. static void
  1501. i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
  1502. {
  1503. struct drm_device *dev = obj->dev;
  1504. if (obj->write_domain != I915_GEM_DOMAIN_CPU)
  1505. return;
  1506. i915_gem_clflush_object(obj);
  1507. drm_agp_chipset_flush(dev);
  1508. obj->write_domain = 0;
  1509. }
  1510. /**
  1511. * Moves a single object to the GTT read, and possibly write domain.
  1512. *
  1513. * This function returns when the move is complete, including waiting on
  1514. * flushes to occur.
  1515. */
  1516. int
  1517. i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
  1518. {
  1519. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1520. int ret;
  1521. /* Not valid to be called on unbound objects. */
  1522. if (obj_priv->gtt_space == NULL)
  1523. return -EINVAL;
  1524. i915_gem_object_flush_gpu_write_domain(obj);
  1525. /* Wait on any GPU rendering and flushing to occur. */
  1526. ret = i915_gem_object_wait_rendering(obj);
  1527. if (ret != 0)
  1528. return ret;
  1529. /* If we're writing through the GTT domain, then CPU and GPU caches
  1530. * will need to be invalidated at next use.
  1531. */
  1532. if (write)
  1533. obj->read_domains &= I915_GEM_DOMAIN_GTT;
  1534. i915_gem_object_flush_cpu_write_domain(obj);
  1535. /* It should now be out of any other write domains, and we can update
  1536. * the domain values for our changes.
  1537. */
  1538. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  1539. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  1540. if (write) {
  1541. obj->write_domain = I915_GEM_DOMAIN_GTT;
  1542. obj_priv->dirty = 1;
  1543. }
  1544. return 0;
  1545. }
  1546. /**
  1547. * Moves a single object to the CPU read, and possibly write domain.
  1548. *
  1549. * This function returns when the move is complete, including waiting on
  1550. * flushes to occur.
  1551. */
  1552. static int
  1553. i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
  1554. {
  1555. struct drm_device *dev = obj->dev;
  1556. int ret;
  1557. i915_gem_object_flush_gpu_write_domain(obj);
  1558. /* Wait on any GPU rendering and flushing to occur. */
  1559. ret = i915_gem_object_wait_rendering(obj);
  1560. if (ret != 0)
  1561. return ret;
  1562. i915_gem_object_flush_gtt_write_domain(obj);
  1563. /* If we have a partially-valid cache of the object in the CPU,
  1564. * finish invalidating it and free the per-page flags.
  1565. */
  1566. i915_gem_object_set_to_full_cpu_read_domain(obj);
  1567. /* Flush the CPU cache if it's still invalid. */
  1568. if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  1569. i915_gem_clflush_object(obj);
  1570. drm_agp_chipset_flush(dev);
  1571. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  1572. }
  1573. /* It should now be out of any other write domains, and we can update
  1574. * the domain values for our changes.
  1575. */
  1576. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  1577. /* If we're writing through the CPU, then the GPU read domains will
  1578. * need to be invalidated at next use.
  1579. */
  1580. if (write) {
  1581. obj->read_domains &= I915_GEM_DOMAIN_CPU;
  1582. obj->write_domain = I915_GEM_DOMAIN_CPU;
  1583. }
  1584. return 0;
  1585. }
  1586. /*
  1587. * Set the next domain for the specified object. This
  1588. * may not actually perform the necessary flushing/invaliding though,
  1589. * as that may want to be batched with other set_domain operations
  1590. *
  1591. * This is (we hope) the only really tricky part of gem. The goal
  1592. * is fairly simple -- track which caches hold bits of the object
  1593. * and make sure they remain coherent. A few concrete examples may
  1594. * help to explain how it works. For shorthand, we use the notation
  1595. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  1596. * a pair of read and write domain masks.
  1597. *
  1598. * Case 1: the batch buffer
  1599. *
  1600. * 1. Allocated
  1601. * 2. Written by CPU
  1602. * 3. Mapped to GTT
  1603. * 4. Read by GPU
  1604. * 5. Unmapped from GTT
  1605. * 6. Freed
  1606. *
  1607. * Let's take these a step at a time
  1608. *
  1609. * 1. Allocated
  1610. * Pages allocated from the kernel may still have
  1611. * cache contents, so we set them to (CPU, CPU) always.
  1612. * 2. Written by CPU (using pwrite)
  1613. * The pwrite function calls set_domain (CPU, CPU) and
  1614. * this function does nothing (as nothing changes)
  1615. * 3. Mapped by GTT
  1616. * This function asserts that the object is not
  1617. * currently in any GPU-based read or write domains
  1618. * 4. Read by GPU
  1619. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  1620. * As write_domain is zero, this function adds in the
  1621. * current read domains (CPU+COMMAND, 0).
  1622. * flush_domains is set to CPU.
  1623. * invalidate_domains is set to COMMAND
  1624. * clflush is run to get data out of the CPU caches
  1625. * then i915_dev_set_domain calls i915_gem_flush to
  1626. * emit an MI_FLUSH and drm_agp_chipset_flush
  1627. * 5. Unmapped from GTT
  1628. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  1629. * flush_domains and invalidate_domains end up both zero
  1630. * so no flushing/invalidating happens
  1631. * 6. Freed
  1632. * yay, done
  1633. *
  1634. * Case 2: The shared render buffer
  1635. *
  1636. * 1. Allocated
  1637. * 2. Mapped to GTT
  1638. * 3. Read/written by GPU
  1639. * 4. set_domain to (CPU,CPU)
  1640. * 5. Read/written by CPU
  1641. * 6. Read/written by GPU
  1642. *
  1643. * 1. Allocated
  1644. * Same as last example, (CPU, CPU)
  1645. * 2. Mapped to GTT
  1646. * Nothing changes (assertions find that it is not in the GPU)
  1647. * 3. Read/written by GPU
  1648. * execbuffer calls set_domain (RENDER, RENDER)
  1649. * flush_domains gets CPU
  1650. * invalidate_domains gets GPU
  1651. * clflush (obj)
  1652. * MI_FLUSH and drm_agp_chipset_flush
  1653. * 4. set_domain (CPU, CPU)
  1654. * flush_domains gets GPU
  1655. * invalidate_domains gets CPU
  1656. * wait_rendering (obj) to make sure all drawing is complete.
  1657. * This will include an MI_FLUSH to get the data from GPU
  1658. * to memory
  1659. * clflush (obj) to invalidate the CPU cache
  1660. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  1661. * 5. Read/written by CPU
  1662. * cache lines are loaded and dirtied
  1663. * 6. Read written by GPU
  1664. * Same as last GPU access
  1665. *
  1666. * Case 3: The constant buffer
  1667. *
  1668. * 1. Allocated
  1669. * 2. Written by CPU
  1670. * 3. Read by GPU
  1671. * 4. Updated (written) by CPU again
  1672. * 5. Read by GPU
  1673. *
  1674. * 1. Allocated
  1675. * (CPU, CPU)
  1676. * 2. Written by CPU
  1677. * (CPU, CPU)
  1678. * 3. Read by GPU
  1679. * (CPU+RENDER, 0)
  1680. * flush_domains = CPU
  1681. * invalidate_domains = RENDER
  1682. * clflush (obj)
  1683. * MI_FLUSH
  1684. * drm_agp_chipset_flush
  1685. * 4. Updated (written) by CPU again
  1686. * (CPU, CPU)
  1687. * flush_domains = 0 (no previous write domain)
  1688. * invalidate_domains = 0 (no new read domains)
  1689. * 5. Read by GPU
  1690. * (CPU+RENDER, 0)
  1691. * flush_domains = CPU
  1692. * invalidate_domains = RENDER
  1693. * clflush (obj)
  1694. * MI_FLUSH
  1695. * drm_agp_chipset_flush
  1696. */
  1697. static void
  1698. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
  1699. uint32_t read_domains,
  1700. uint32_t write_domain)
  1701. {
  1702. struct drm_device *dev = obj->dev;
  1703. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1704. uint32_t invalidate_domains = 0;
  1705. uint32_t flush_domains = 0;
  1706. BUG_ON(read_domains & I915_GEM_DOMAIN_CPU);
  1707. BUG_ON(write_domain == I915_GEM_DOMAIN_CPU);
  1708. #if WATCH_BUF
  1709. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  1710. __func__, obj,
  1711. obj->read_domains, read_domains,
  1712. obj->write_domain, write_domain);
  1713. #endif
  1714. /*
  1715. * If the object isn't moving to a new write domain,
  1716. * let the object stay in multiple read domains
  1717. */
  1718. if (write_domain == 0)
  1719. read_domains |= obj->read_domains;
  1720. else
  1721. obj_priv->dirty = 1;
  1722. /*
  1723. * Flush the current write domain if
  1724. * the new read domains don't match. Invalidate
  1725. * any read domains which differ from the old
  1726. * write domain
  1727. */
  1728. if (obj->write_domain && obj->write_domain != read_domains) {
  1729. flush_domains |= obj->write_domain;
  1730. invalidate_domains |= read_domains & ~obj->write_domain;
  1731. }
  1732. /*
  1733. * Invalidate any read caches which may have
  1734. * stale data. That is, any new read domains.
  1735. */
  1736. invalidate_domains |= read_domains & ~obj->read_domains;
  1737. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  1738. #if WATCH_BUF
  1739. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  1740. __func__, flush_domains, invalidate_domains);
  1741. #endif
  1742. i915_gem_clflush_object(obj);
  1743. }
  1744. if ((write_domain | flush_domains) != 0)
  1745. obj->write_domain = write_domain;
  1746. obj->read_domains = read_domains;
  1747. dev->invalidate_domains |= invalidate_domains;
  1748. dev->flush_domains |= flush_domains;
  1749. #if WATCH_BUF
  1750. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  1751. __func__,
  1752. obj->read_domains, obj->write_domain,
  1753. dev->invalidate_domains, dev->flush_domains);
  1754. #endif
  1755. }
  1756. /**
  1757. * Moves the object from a partially CPU read to a full one.
  1758. *
  1759. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  1760. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  1761. */
  1762. static void
  1763. i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
  1764. {
  1765. struct drm_device *dev = obj->dev;
  1766. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1767. if (!obj_priv->page_cpu_valid)
  1768. return;
  1769. /* If we're partially in the CPU read domain, finish moving it in.
  1770. */
  1771. if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
  1772. int i;
  1773. for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
  1774. if (obj_priv->page_cpu_valid[i])
  1775. continue;
  1776. drm_clflush_pages(obj_priv->page_list + i, 1);
  1777. }
  1778. drm_agp_chipset_flush(dev);
  1779. }
  1780. /* Free the page_cpu_valid mappings which are now stale, whether
  1781. * or not we've got I915_GEM_DOMAIN_CPU.
  1782. */
  1783. drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
  1784. DRM_MEM_DRIVER);
  1785. obj_priv->page_cpu_valid = NULL;
  1786. }
  1787. /**
  1788. * Set the CPU read domain on a range of the object.
  1789. *
  1790. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  1791. * not entirely valid. The page_cpu_valid member of the object flags which
  1792. * pages have been flushed, and will be respected by
  1793. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  1794. * of the whole object.
  1795. *
  1796. * This function returns when the move is complete, including waiting on
  1797. * flushes to occur.
  1798. */
  1799. static int
  1800. i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  1801. uint64_t offset, uint64_t size)
  1802. {
  1803. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1804. int i, ret;
  1805. if (offset == 0 && size == obj->size)
  1806. return i915_gem_object_set_to_cpu_domain(obj, 0);
  1807. i915_gem_object_flush_gpu_write_domain(obj);
  1808. /* Wait on any GPU rendering and flushing to occur. */
  1809. ret = i915_gem_object_wait_rendering(obj);
  1810. if (ret != 0)
  1811. return ret;
  1812. i915_gem_object_flush_gtt_write_domain(obj);
  1813. /* If we're already fully in the CPU read domain, we're done. */
  1814. if (obj_priv->page_cpu_valid == NULL &&
  1815. (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
  1816. return 0;
  1817. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  1818. * newly adding I915_GEM_DOMAIN_CPU
  1819. */
  1820. if (obj_priv->page_cpu_valid == NULL) {
  1821. obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
  1822. DRM_MEM_DRIVER);
  1823. if (obj_priv->page_cpu_valid == NULL)
  1824. return -ENOMEM;
  1825. } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
  1826. memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
  1827. /* Flush the cache on any pages that are still invalid from the CPU's
  1828. * perspective.
  1829. */
  1830. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  1831. i++) {
  1832. if (obj_priv->page_cpu_valid[i])
  1833. continue;
  1834. drm_clflush_pages(obj_priv->page_list + i, 1);
  1835. obj_priv->page_cpu_valid[i] = 1;
  1836. }
  1837. /* It should now be out of any other write domains, and we can update
  1838. * the domain values for our changes.
  1839. */
  1840. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  1841. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  1842. return 0;
  1843. }
  1844. /**
  1845. * Pin an object to the GTT and evaluate the relocations landing in it.
  1846. */
  1847. static int
  1848. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  1849. struct drm_file *file_priv,
  1850. struct drm_i915_gem_exec_object *entry)
  1851. {
  1852. struct drm_device *dev = obj->dev;
  1853. drm_i915_private_t *dev_priv = dev->dev_private;
  1854. struct drm_i915_gem_relocation_entry reloc;
  1855. struct drm_i915_gem_relocation_entry __user *relocs;
  1856. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1857. int i, ret;
  1858. void __iomem *reloc_page;
  1859. /* Choose the GTT offset for our buffer and put it there. */
  1860. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  1861. if (ret)
  1862. return ret;
  1863. entry->offset = obj_priv->gtt_offset;
  1864. relocs = (struct drm_i915_gem_relocation_entry __user *)
  1865. (uintptr_t) entry->relocs_ptr;
  1866. /* Apply the relocations, using the GTT aperture to avoid cache
  1867. * flushing requirements.
  1868. */
  1869. for (i = 0; i < entry->relocation_count; i++) {
  1870. struct drm_gem_object *target_obj;
  1871. struct drm_i915_gem_object *target_obj_priv;
  1872. uint32_t reloc_val, reloc_offset;
  1873. uint32_t __iomem *reloc_entry;
  1874. ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
  1875. if (ret != 0) {
  1876. i915_gem_object_unpin(obj);
  1877. return ret;
  1878. }
  1879. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  1880. reloc.target_handle);
  1881. if (target_obj == NULL) {
  1882. i915_gem_object_unpin(obj);
  1883. return -EBADF;
  1884. }
  1885. target_obj_priv = target_obj->driver_private;
  1886. /* The target buffer should have appeared before us in the
  1887. * exec_object list, so it should have a GTT space bound by now.
  1888. */
  1889. if (target_obj_priv->gtt_space == NULL) {
  1890. DRM_ERROR("No GTT space found for object %d\n",
  1891. reloc.target_handle);
  1892. drm_gem_object_unreference(target_obj);
  1893. i915_gem_object_unpin(obj);
  1894. return -EINVAL;
  1895. }
  1896. if (reloc.offset > obj->size - 4) {
  1897. DRM_ERROR("Relocation beyond object bounds: "
  1898. "obj %p target %d offset %d size %d.\n",
  1899. obj, reloc.target_handle,
  1900. (int) reloc.offset, (int) obj->size);
  1901. drm_gem_object_unreference(target_obj);
  1902. i915_gem_object_unpin(obj);
  1903. return -EINVAL;
  1904. }
  1905. if (reloc.offset & 3) {
  1906. DRM_ERROR("Relocation not 4-byte aligned: "
  1907. "obj %p target %d offset %d.\n",
  1908. obj, reloc.target_handle,
  1909. (int) reloc.offset);
  1910. drm_gem_object_unreference(target_obj);
  1911. i915_gem_object_unpin(obj);
  1912. return -EINVAL;
  1913. }
  1914. if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
  1915. reloc.read_domains & I915_GEM_DOMAIN_CPU) {
  1916. DRM_ERROR("reloc with read/write CPU domains: "
  1917. "obj %p target %d offset %d "
  1918. "read %08x write %08x",
  1919. obj, reloc.target_handle,
  1920. (int) reloc.offset,
  1921. reloc.read_domains,
  1922. reloc.write_domain);
  1923. return -EINVAL;
  1924. }
  1925. if (reloc.write_domain && target_obj->pending_write_domain &&
  1926. reloc.write_domain != target_obj->pending_write_domain) {
  1927. DRM_ERROR("Write domain conflict: "
  1928. "obj %p target %d offset %d "
  1929. "new %08x old %08x\n",
  1930. obj, reloc.target_handle,
  1931. (int) reloc.offset,
  1932. reloc.write_domain,
  1933. target_obj->pending_write_domain);
  1934. drm_gem_object_unreference(target_obj);
  1935. i915_gem_object_unpin(obj);
  1936. return -EINVAL;
  1937. }
  1938. #if WATCH_RELOC
  1939. DRM_INFO("%s: obj %p offset %08x target %d "
  1940. "read %08x write %08x gtt %08x "
  1941. "presumed %08x delta %08x\n",
  1942. __func__,
  1943. obj,
  1944. (int) reloc.offset,
  1945. (int) reloc.target_handle,
  1946. (int) reloc.read_domains,
  1947. (int) reloc.write_domain,
  1948. (int) target_obj_priv->gtt_offset,
  1949. (int) reloc.presumed_offset,
  1950. reloc.delta);
  1951. #endif
  1952. target_obj->pending_read_domains |= reloc.read_domains;
  1953. target_obj->pending_write_domain |= reloc.write_domain;
  1954. /* If the relocation already has the right value in it, no
  1955. * more work needs to be done.
  1956. */
  1957. if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
  1958. drm_gem_object_unreference(target_obj);
  1959. continue;
  1960. }
  1961. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  1962. if (ret != 0) {
  1963. drm_gem_object_unreference(target_obj);
  1964. i915_gem_object_unpin(obj);
  1965. return -EINVAL;
  1966. }
  1967. /* Map the page containing the relocation we're going to
  1968. * perform.
  1969. */
  1970. reloc_offset = obj_priv->gtt_offset + reloc.offset;
  1971. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  1972. (reloc_offset &
  1973. ~(PAGE_SIZE - 1)));
  1974. reloc_entry = (uint32_t __iomem *)(reloc_page +
  1975. (reloc_offset & (PAGE_SIZE - 1)));
  1976. reloc_val = target_obj_priv->gtt_offset + reloc.delta;
  1977. #if WATCH_BUF
  1978. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  1979. obj, (unsigned int) reloc.offset,
  1980. readl(reloc_entry), reloc_val);
  1981. #endif
  1982. writel(reloc_val, reloc_entry);
  1983. io_mapping_unmap_atomic(reloc_page);
  1984. /* Write the updated presumed offset for this entry back out
  1985. * to the user.
  1986. */
  1987. reloc.presumed_offset = target_obj_priv->gtt_offset;
  1988. ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
  1989. if (ret != 0) {
  1990. drm_gem_object_unreference(target_obj);
  1991. i915_gem_object_unpin(obj);
  1992. return ret;
  1993. }
  1994. drm_gem_object_unreference(target_obj);
  1995. }
  1996. #if WATCH_BUF
  1997. if (0)
  1998. i915_gem_dump_object(obj, 128, __func__, ~0);
  1999. #endif
  2000. return 0;
  2001. }
  2002. /** Dispatch a batchbuffer to the ring
  2003. */
  2004. static int
  2005. i915_dispatch_gem_execbuffer(struct drm_device *dev,
  2006. struct drm_i915_gem_execbuffer *exec,
  2007. uint64_t exec_offset)
  2008. {
  2009. drm_i915_private_t *dev_priv = dev->dev_private;
  2010. struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
  2011. (uintptr_t) exec->cliprects_ptr;
  2012. int nbox = exec->num_cliprects;
  2013. int i = 0, count;
  2014. uint32_t exec_start, exec_len;
  2015. RING_LOCALS;
  2016. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  2017. exec_len = (uint32_t) exec->batch_len;
  2018. if ((exec_start | exec_len) & 0x7) {
  2019. DRM_ERROR("alignment\n");
  2020. return -EINVAL;
  2021. }
  2022. if (!exec_start)
  2023. return -EINVAL;
  2024. count = nbox ? nbox : 1;
  2025. for (i = 0; i < count; i++) {
  2026. if (i < nbox) {
  2027. int ret = i915_emit_box(dev, boxes, i,
  2028. exec->DR1, exec->DR4);
  2029. if (ret)
  2030. return ret;
  2031. }
  2032. if (IS_I830(dev) || IS_845G(dev)) {
  2033. BEGIN_LP_RING(4);
  2034. OUT_RING(MI_BATCH_BUFFER);
  2035. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  2036. OUT_RING(exec_start + exec_len - 4);
  2037. OUT_RING(0);
  2038. ADVANCE_LP_RING();
  2039. } else {
  2040. BEGIN_LP_RING(2);
  2041. if (IS_I965G(dev)) {
  2042. OUT_RING(MI_BATCH_BUFFER_START |
  2043. (2 << 6) |
  2044. MI_BATCH_NON_SECURE_I965);
  2045. OUT_RING(exec_start);
  2046. } else {
  2047. OUT_RING(MI_BATCH_BUFFER_START |
  2048. (2 << 6));
  2049. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  2050. }
  2051. ADVANCE_LP_RING();
  2052. }
  2053. }
  2054. /* XXX breadcrumb */
  2055. return 0;
  2056. }
  2057. /* Throttle our rendering by waiting until the ring has completed our requests
  2058. * emitted over 20 msec ago.
  2059. *
  2060. * This should get us reasonable parallelism between CPU and GPU but also
  2061. * relatively low latency when blocking on a particular request to finish.
  2062. */
  2063. static int
  2064. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  2065. {
  2066. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  2067. int ret = 0;
  2068. uint32_t seqno;
  2069. mutex_lock(&dev->struct_mutex);
  2070. seqno = i915_file_priv->mm.last_gem_throttle_seqno;
  2071. i915_file_priv->mm.last_gem_throttle_seqno =
  2072. i915_file_priv->mm.last_gem_seqno;
  2073. if (seqno)
  2074. ret = i915_wait_request(dev, seqno);
  2075. mutex_unlock(&dev->struct_mutex);
  2076. return ret;
  2077. }
  2078. int
  2079. i915_gem_execbuffer(struct drm_device *dev, void *data,
  2080. struct drm_file *file_priv)
  2081. {
  2082. drm_i915_private_t *dev_priv = dev->dev_private;
  2083. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  2084. struct drm_i915_gem_execbuffer *args = data;
  2085. struct drm_i915_gem_exec_object *exec_list = NULL;
  2086. struct drm_gem_object **object_list = NULL;
  2087. struct drm_gem_object *batch_obj;
  2088. int ret, i, pinned = 0;
  2089. uint64_t exec_offset;
  2090. uint32_t seqno, flush_domains;
  2091. int pin_tries;
  2092. #if WATCH_EXEC
  2093. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  2094. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  2095. #endif
  2096. if (args->buffer_count < 1) {
  2097. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  2098. return -EINVAL;
  2099. }
  2100. /* Copy in the exec list from userland */
  2101. exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
  2102. DRM_MEM_DRIVER);
  2103. object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
  2104. DRM_MEM_DRIVER);
  2105. if (exec_list == NULL || object_list == NULL) {
  2106. DRM_ERROR("Failed to allocate exec or object list "
  2107. "for %d buffers\n",
  2108. args->buffer_count);
  2109. ret = -ENOMEM;
  2110. goto pre_mutex_err;
  2111. }
  2112. ret = copy_from_user(exec_list,
  2113. (struct drm_i915_relocation_entry __user *)
  2114. (uintptr_t) args->buffers_ptr,
  2115. sizeof(*exec_list) * args->buffer_count);
  2116. if (ret != 0) {
  2117. DRM_ERROR("copy %d exec entries failed %d\n",
  2118. args->buffer_count, ret);
  2119. goto pre_mutex_err;
  2120. }
  2121. mutex_lock(&dev->struct_mutex);
  2122. i915_verify_inactive(dev, __FILE__, __LINE__);
  2123. if (dev_priv->mm.wedged) {
  2124. DRM_ERROR("Execbuf while wedged\n");
  2125. mutex_unlock(&dev->struct_mutex);
  2126. return -EIO;
  2127. }
  2128. if (dev_priv->mm.suspended) {
  2129. DRM_ERROR("Execbuf while VT-switched.\n");
  2130. mutex_unlock(&dev->struct_mutex);
  2131. return -EBUSY;
  2132. }
  2133. /* Look up object handles */
  2134. for (i = 0; i < args->buffer_count; i++) {
  2135. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  2136. exec_list[i].handle);
  2137. if (object_list[i] == NULL) {
  2138. DRM_ERROR("Invalid object handle %d at index %d\n",
  2139. exec_list[i].handle, i);
  2140. ret = -EBADF;
  2141. goto err;
  2142. }
  2143. }
  2144. /* Pin and relocate */
  2145. for (pin_tries = 0; ; pin_tries++) {
  2146. ret = 0;
  2147. for (i = 0; i < args->buffer_count; i++) {
  2148. object_list[i]->pending_read_domains = 0;
  2149. object_list[i]->pending_write_domain = 0;
  2150. ret = i915_gem_object_pin_and_relocate(object_list[i],
  2151. file_priv,
  2152. &exec_list[i]);
  2153. if (ret)
  2154. break;
  2155. pinned = i + 1;
  2156. }
  2157. /* success */
  2158. if (ret == 0)
  2159. break;
  2160. /* error other than GTT full, or we've already tried again */
  2161. if (ret != -ENOMEM || pin_tries >= 1) {
  2162. if (ret != -ERESTARTSYS)
  2163. DRM_ERROR("Failed to pin buffers %d\n", ret);
  2164. goto err;
  2165. }
  2166. /* unpin all of our buffers */
  2167. for (i = 0; i < pinned; i++)
  2168. i915_gem_object_unpin(object_list[i]);
  2169. pinned = 0;
  2170. /* evict everyone we can from the aperture */
  2171. ret = i915_gem_evict_everything(dev);
  2172. if (ret)
  2173. goto err;
  2174. }
  2175. /* Set the pending read domains for the batch buffer to COMMAND */
  2176. batch_obj = object_list[args->buffer_count-1];
  2177. batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  2178. batch_obj->pending_write_domain = 0;
  2179. i915_verify_inactive(dev, __FILE__, __LINE__);
  2180. /* Zero the global flush/invalidate flags. These
  2181. * will be modified as new domains are computed
  2182. * for each object
  2183. */
  2184. dev->invalidate_domains = 0;
  2185. dev->flush_domains = 0;
  2186. for (i = 0; i < args->buffer_count; i++) {
  2187. struct drm_gem_object *obj = object_list[i];
  2188. /* Compute new gpu domains and update invalidate/flush */
  2189. i915_gem_object_set_to_gpu_domain(obj,
  2190. obj->pending_read_domains,
  2191. obj->pending_write_domain);
  2192. }
  2193. i915_verify_inactive(dev, __FILE__, __LINE__);
  2194. if (dev->invalidate_domains | dev->flush_domains) {
  2195. #if WATCH_EXEC
  2196. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  2197. __func__,
  2198. dev->invalidate_domains,
  2199. dev->flush_domains);
  2200. #endif
  2201. i915_gem_flush(dev,
  2202. dev->invalidate_domains,
  2203. dev->flush_domains);
  2204. if (dev->flush_domains)
  2205. (void)i915_add_request(dev, dev->flush_domains);
  2206. }
  2207. i915_verify_inactive(dev, __FILE__, __LINE__);
  2208. #if WATCH_COHERENCY
  2209. for (i = 0; i < args->buffer_count; i++) {
  2210. i915_gem_object_check_coherency(object_list[i],
  2211. exec_list[i].handle);
  2212. }
  2213. #endif
  2214. exec_offset = exec_list[args->buffer_count - 1].offset;
  2215. #if WATCH_EXEC
  2216. i915_gem_dump_object(object_list[args->buffer_count - 1],
  2217. args->batch_len,
  2218. __func__,
  2219. ~0);
  2220. #endif
  2221. /* Exec the batchbuffer */
  2222. ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
  2223. if (ret) {
  2224. DRM_ERROR("dispatch failed %d\n", ret);
  2225. goto err;
  2226. }
  2227. /*
  2228. * Ensure that the commands in the batch buffer are
  2229. * finished before the interrupt fires
  2230. */
  2231. flush_domains = i915_retire_commands(dev);
  2232. i915_verify_inactive(dev, __FILE__, __LINE__);
  2233. /*
  2234. * Get a seqno representing the execution of the current buffer,
  2235. * which we can wait on. We would like to mitigate these interrupts,
  2236. * likely by only creating seqnos occasionally (so that we have
  2237. * *some* interrupts representing completion of buffers that we can
  2238. * wait on when trying to clear up gtt space).
  2239. */
  2240. seqno = i915_add_request(dev, flush_domains);
  2241. BUG_ON(seqno == 0);
  2242. i915_file_priv->mm.last_gem_seqno = seqno;
  2243. for (i = 0; i < args->buffer_count; i++) {
  2244. struct drm_gem_object *obj = object_list[i];
  2245. i915_gem_object_move_to_active(obj, seqno);
  2246. #if WATCH_LRU
  2247. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  2248. #endif
  2249. }
  2250. #if WATCH_LRU
  2251. i915_dump_lru(dev, __func__);
  2252. #endif
  2253. i915_verify_inactive(dev, __FILE__, __LINE__);
  2254. /* Copy the new buffer offsets back to the user's exec list. */
  2255. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  2256. (uintptr_t) args->buffers_ptr,
  2257. exec_list,
  2258. sizeof(*exec_list) * args->buffer_count);
  2259. if (ret)
  2260. DRM_ERROR("failed to copy %d exec entries "
  2261. "back to user (%d)\n",
  2262. args->buffer_count, ret);
  2263. err:
  2264. for (i = 0; i < pinned; i++)
  2265. i915_gem_object_unpin(object_list[i]);
  2266. for (i = 0; i < args->buffer_count; i++)
  2267. drm_gem_object_unreference(object_list[i]);
  2268. mutex_unlock(&dev->struct_mutex);
  2269. pre_mutex_err:
  2270. drm_free(object_list, sizeof(*object_list) * args->buffer_count,
  2271. DRM_MEM_DRIVER);
  2272. drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
  2273. DRM_MEM_DRIVER);
  2274. return ret;
  2275. }
  2276. int
  2277. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  2278. {
  2279. struct drm_device *dev = obj->dev;
  2280. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2281. int ret;
  2282. i915_verify_inactive(dev, __FILE__, __LINE__);
  2283. if (obj_priv->gtt_space == NULL) {
  2284. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  2285. if (ret != 0) {
  2286. if (ret != -ERESTARTSYS)
  2287. DRM_ERROR("Failure to bind: %d", ret);
  2288. return ret;
  2289. }
  2290. }
  2291. obj_priv->pin_count++;
  2292. /* If the object is not active and not pending a flush,
  2293. * remove it from the inactive list
  2294. */
  2295. if (obj_priv->pin_count == 1) {
  2296. atomic_inc(&dev->pin_count);
  2297. atomic_add(obj->size, &dev->pin_memory);
  2298. if (!obj_priv->active &&
  2299. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  2300. I915_GEM_DOMAIN_GTT)) == 0 &&
  2301. !list_empty(&obj_priv->list))
  2302. list_del_init(&obj_priv->list);
  2303. }
  2304. i915_verify_inactive(dev, __FILE__, __LINE__);
  2305. return 0;
  2306. }
  2307. void
  2308. i915_gem_object_unpin(struct drm_gem_object *obj)
  2309. {
  2310. struct drm_device *dev = obj->dev;
  2311. drm_i915_private_t *dev_priv = dev->dev_private;
  2312. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2313. i915_verify_inactive(dev, __FILE__, __LINE__);
  2314. obj_priv->pin_count--;
  2315. BUG_ON(obj_priv->pin_count < 0);
  2316. BUG_ON(obj_priv->gtt_space == NULL);
  2317. /* If the object is no longer pinned, and is
  2318. * neither active nor being flushed, then stick it on
  2319. * the inactive list
  2320. */
  2321. if (obj_priv->pin_count == 0) {
  2322. if (!obj_priv->active &&
  2323. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  2324. I915_GEM_DOMAIN_GTT)) == 0)
  2325. list_move_tail(&obj_priv->list,
  2326. &dev_priv->mm.inactive_list);
  2327. atomic_dec(&dev->pin_count);
  2328. atomic_sub(obj->size, &dev->pin_memory);
  2329. }
  2330. i915_verify_inactive(dev, __FILE__, __LINE__);
  2331. }
  2332. int
  2333. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  2334. struct drm_file *file_priv)
  2335. {
  2336. struct drm_i915_gem_pin *args = data;
  2337. struct drm_gem_object *obj;
  2338. struct drm_i915_gem_object *obj_priv;
  2339. int ret;
  2340. mutex_lock(&dev->struct_mutex);
  2341. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  2342. if (obj == NULL) {
  2343. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  2344. args->handle);
  2345. mutex_unlock(&dev->struct_mutex);
  2346. return -EBADF;
  2347. }
  2348. obj_priv = obj->driver_private;
  2349. if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
  2350. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  2351. args->handle);
  2352. mutex_unlock(&dev->struct_mutex);
  2353. return -EINVAL;
  2354. }
  2355. obj_priv->user_pin_count++;
  2356. obj_priv->pin_filp = file_priv;
  2357. if (obj_priv->user_pin_count == 1) {
  2358. ret = i915_gem_object_pin(obj, args->alignment);
  2359. if (ret != 0) {
  2360. drm_gem_object_unreference(obj);
  2361. mutex_unlock(&dev->struct_mutex);
  2362. return ret;
  2363. }
  2364. }
  2365. /* XXX - flush the CPU caches for pinned objects
  2366. * as the X server doesn't manage domains yet
  2367. */
  2368. i915_gem_object_flush_cpu_write_domain(obj);
  2369. args->offset = obj_priv->gtt_offset;
  2370. drm_gem_object_unreference(obj);
  2371. mutex_unlock(&dev->struct_mutex);
  2372. return 0;
  2373. }
  2374. int
  2375. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  2376. struct drm_file *file_priv)
  2377. {
  2378. struct drm_i915_gem_pin *args = data;
  2379. struct drm_gem_object *obj;
  2380. struct drm_i915_gem_object *obj_priv;
  2381. mutex_lock(&dev->struct_mutex);
  2382. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  2383. if (obj == NULL) {
  2384. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  2385. args->handle);
  2386. mutex_unlock(&dev->struct_mutex);
  2387. return -EBADF;
  2388. }
  2389. obj_priv = obj->driver_private;
  2390. if (obj_priv->pin_filp != file_priv) {
  2391. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  2392. args->handle);
  2393. drm_gem_object_unreference(obj);
  2394. mutex_unlock(&dev->struct_mutex);
  2395. return -EINVAL;
  2396. }
  2397. obj_priv->user_pin_count--;
  2398. if (obj_priv->user_pin_count == 0) {
  2399. obj_priv->pin_filp = NULL;
  2400. i915_gem_object_unpin(obj);
  2401. }
  2402. drm_gem_object_unreference(obj);
  2403. mutex_unlock(&dev->struct_mutex);
  2404. return 0;
  2405. }
  2406. int
  2407. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  2408. struct drm_file *file_priv)
  2409. {
  2410. struct drm_i915_gem_busy *args = data;
  2411. struct drm_gem_object *obj;
  2412. struct drm_i915_gem_object *obj_priv;
  2413. mutex_lock(&dev->struct_mutex);
  2414. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  2415. if (obj == NULL) {
  2416. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  2417. args->handle);
  2418. mutex_unlock(&dev->struct_mutex);
  2419. return -EBADF;
  2420. }
  2421. obj_priv = obj->driver_private;
  2422. /* Don't count being on the flushing list against the object being
  2423. * done. Otherwise, a buffer left on the flushing list but not getting
  2424. * flushed (because nobody's flushing that domain) won't ever return
  2425. * unbusy and get reused by libdrm's bo cache. The other expected
  2426. * consumer of this interface, OpenGL's occlusion queries, also specs
  2427. * that the objects get unbusy "eventually" without any interference.
  2428. */
  2429. args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
  2430. drm_gem_object_unreference(obj);
  2431. mutex_unlock(&dev->struct_mutex);
  2432. return 0;
  2433. }
  2434. int
  2435. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  2436. struct drm_file *file_priv)
  2437. {
  2438. return i915_gem_ring_throttle(dev, file_priv);
  2439. }
  2440. int i915_gem_init_object(struct drm_gem_object *obj)
  2441. {
  2442. struct drm_i915_gem_object *obj_priv;
  2443. obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
  2444. if (obj_priv == NULL)
  2445. return -ENOMEM;
  2446. /*
  2447. * We've just allocated pages from the kernel,
  2448. * so they've just been written by the CPU with
  2449. * zeros. They'll need to be clflushed before we
  2450. * use them with the GPU.
  2451. */
  2452. obj->write_domain = I915_GEM_DOMAIN_CPU;
  2453. obj->read_domains = I915_GEM_DOMAIN_CPU;
  2454. obj_priv->agp_type = AGP_USER_MEMORY;
  2455. obj->driver_private = obj_priv;
  2456. obj_priv->obj = obj;
  2457. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  2458. INIT_LIST_HEAD(&obj_priv->list);
  2459. return 0;
  2460. }
  2461. void i915_gem_free_object(struct drm_gem_object *obj)
  2462. {
  2463. struct drm_device *dev = obj->dev;
  2464. struct drm_gem_mm *mm = dev->mm_private;
  2465. struct drm_map_list *list;
  2466. struct drm_map *map;
  2467. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2468. while (obj_priv->pin_count > 0)
  2469. i915_gem_object_unpin(obj);
  2470. i915_gem_object_unbind(obj);
  2471. list = &obj->map_list;
  2472. drm_ht_remove_item(&mm->offset_hash, &list->hash);
  2473. if (list->file_offset_node) {
  2474. drm_mm_put_block(list->file_offset_node);
  2475. list->file_offset_node = NULL;
  2476. }
  2477. map = list->map;
  2478. if (map) {
  2479. drm_free(map, sizeof(*map), DRM_MEM_DRIVER);
  2480. list->map = NULL;
  2481. }
  2482. drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
  2483. drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
  2484. }
  2485. /** Unbinds all objects that are on the given buffer list. */
  2486. static int
  2487. i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
  2488. {
  2489. struct drm_gem_object *obj;
  2490. struct drm_i915_gem_object *obj_priv;
  2491. int ret;
  2492. while (!list_empty(head)) {
  2493. obj_priv = list_first_entry(head,
  2494. struct drm_i915_gem_object,
  2495. list);
  2496. obj = obj_priv->obj;
  2497. if (obj_priv->pin_count != 0) {
  2498. DRM_ERROR("Pinned object in unbind list\n");
  2499. mutex_unlock(&dev->struct_mutex);
  2500. return -EINVAL;
  2501. }
  2502. ret = i915_gem_object_unbind(obj);
  2503. if (ret != 0) {
  2504. DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
  2505. ret);
  2506. mutex_unlock(&dev->struct_mutex);
  2507. return ret;
  2508. }
  2509. }
  2510. return 0;
  2511. }
  2512. static int
  2513. i915_gem_idle(struct drm_device *dev)
  2514. {
  2515. drm_i915_private_t *dev_priv = dev->dev_private;
  2516. uint32_t seqno, cur_seqno, last_seqno;
  2517. int stuck, ret;
  2518. mutex_lock(&dev->struct_mutex);
  2519. if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
  2520. mutex_unlock(&dev->struct_mutex);
  2521. return 0;
  2522. }
  2523. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  2524. * We need to replace this with a semaphore, or something.
  2525. */
  2526. dev_priv->mm.suspended = 1;
  2527. /* Cancel the retire work handler, wait for it to finish if running
  2528. */
  2529. mutex_unlock(&dev->struct_mutex);
  2530. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  2531. mutex_lock(&dev->struct_mutex);
  2532. i915_kernel_lost_context(dev);
  2533. /* Flush the GPU along with all non-CPU write domains
  2534. */
  2535. i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
  2536. ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  2537. seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU);
  2538. if (seqno == 0) {
  2539. mutex_unlock(&dev->struct_mutex);
  2540. return -ENOMEM;
  2541. }
  2542. dev_priv->mm.waiting_gem_seqno = seqno;
  2543. last_seqno = 0;
  2544. stuck = 0;
  2545. for (;;) {
  2546. cur_seqno = i915_get_gem_seqno(dev);
  2547. if (i915_seqno_passed(cur_seqno, seqno))
  2548. break;
  2549. if (last_seqno == cur_seqno) {
  2550. if (stuck++ > 100) {
  2551. DRM_ERROR("hardware wedged\n");
  2552. dev_priv->mm.wedged = 1;
  2553. DRM_WAKEUP(&dev_priv->irq_queue);
  2554. break;
  2555. }
  2556. }
  2557. msleep(10);
  2558. last_seqno = cur_seqno;
  2559. }
  2560. dev_priv->mm.waiting_gem_seqno = 0;
  2561. i915_gem_retire_requests(dev);
  2562. if (!dev_priv->mm.wedged) {
  2563. /* Active and flushing should now be empty as we've
  2564. * waited for a sequence higher than any pending execbuffer
  2565. */
  2566. WARN_ON(!list_empty(&dev_priv->mm.active_list));
  2567. WARN_ON(!list_empty(&dev_priv->mm.flushing_list));
  2568. /* Request should now be empty as we've also waited
  2569. * for the last request in the list
  2570. */
  2571. WARN_ON(!list_empty(&dev_priv->mm.request_list));
  2572. }
  2573. /* Empty the active and flushing lists to inactive. If there's
  2574. * anything left at this point, it means that we're wedged and
  2575. * nothing good's going to happen by leaving them there. So strip
  2576. * the GPU domains and just stuff them onto inactive.
  2577. */
  2578. while (!list_empty(&dev_priv->mm.active_list)) {
  2579. struct drm_i915_gem_object *obj_priv;
  2580. obj_priv = list_first_entry(&dev_priv->mm.active_list,
  2581. struct drm_i915_gem_object,
  2582. list);
  2583. obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
  2584. i915_gem_object_move_to_inactive(obj_priv->obj);
  2585. }
  2586. while (!list_empty(&dev_priv->mm.flushing_list)) {
  2587. struct drm_i915_gem_object *obj_priv;
  2588. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  2589. struct drm_i915_gem_object,
  2590. list);
  2591. obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS;
  2592. i915_gem_object_move_to_inactive(obj_priv->obj);
  2593. }
  2594. /* Move all inactive buffers out of the GTT. */
  2595. ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
  2596. WARN_ON(!list_empty(&dev_priv->mm.inactive_list));
  2597. if (ret) {
  2598. mutex_unlock(&dev->struct_mutex);
  2599. return ret;
  2600. }
  2601. i915_gem_cleanup_ringbuffer(dev);
  2602. mutex_unlock(&dev->struct_mutex);
  2603. return 0;
  2604. }
  2605. static int
  2606. i915_gem_init_hws(struct drm_device *dev)
  2607. {
  2608. drm_i915_private_t *dev_priv = dev->dev_private;
  2609. struct drm_gem_object *obj;
  2610. struct drm_i915_gem_object *obj_priv;
  2611. int ret;
  2612. /* If we need a physical address for the status page, it's already
  2613. * initialized at driver load time.
  2614. */
  2615. if (!I915_NEED_GFX_HWS(dev))
  2616. return 0;
  2617. obj = drm_gem_object_alloc(dev, 4096);
  2618. if (obj == NULL) {
  2619. DRM_ERROR("Failed to allocate status page\n");
  2620. return -ENOMEM;
  2621. }
  2622. obj_priv = obj->driver_private;
  2623. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  2624. ret = i915_gem_object_pin(obj, 4096);
  2625. if (ret != 0) {
  2626. drm_gem_object_unreference(obj);
  2627. return ret;
  2628. }
  2629. dev_priv->status_gfx_addr = obj_priv->gtt_offset;
  2630. dev_priv->hw_status_page = kmap(obj_priv->page_list[0]);
  2631. if (dev_priv->hw_status_page == NULL) {
  2632. DRM_ERROR("Failed to map status page.\n");
  2633. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2634. drm_gem_object_unreference(obj);
  2635. return -EINVAL;
  2636. }
  2637. dev_priv->hws_obj = obj;
  2638. memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
  2639. I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
  2640. I915_READ(HWS_PGA); /* posting read */
  2641. DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
  2642. return 0;
  2643. }
  2644. int
  2645. i915_gem_init_ringbuffer(struct drm_device *dev)
  2646. {
  2647. drm_i915_private_t *dev_priv = dev->dev_private;
  2648. struct drm_gem_object *obj;
  2649. struct drm_i915_gem_object *obj_priv;
  2650. drm_i915_ring_buffer_t *ring = &dev_priv->ring;
  2651. int ret;
  2652. u32 head;
  2653. ret = i915_gem_init_hws(dev);
  2654. if (ret != 0)
  2655. return ret;
  2656. obj = drm_gem_object_alloc(dev, 128 * 1024);
  2657. if (obj == NULL) {
  2658. DRM_ERROR("Failed to allocate ringbuffer\n");
  2659. return -ENOMEM;
  2660. }
  2661. obj_priv = obj->driver_private;
  2662. ret = i915_gem_object_pin(obj, 4096);
  2663. if (ret != 0) {
  2664. drm_gem_object_unreference(obj);
  2665. return ret;
  2666. }
  2667. /* Set up the kernel mapping for the ring. */
  2668. ring->Size = obj->size;
  2669. ring->tail_mask = obj->size - 1;
  2670. ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
  2671. ring->map.size = obj->size;
  2672. ring->map.type = 0;
  2673. ring->map.flags = 0;
  2674. ring->map.mtrr = 0;
  2675. drm_core_ioremap_wc(&ring->map, dev);
  2676. if (ring->map.handle == NULL) {
  2677. DRM_ERROR("Failed to map ringbuffer.\n");
  2678. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2679. drm_gem_object_unreference(obj);
  2680. return -EINVAL;
  2681. }
  2682. ring->ring_obj = obj;
  2683. ring->virtual_start = ring->map.handle;
  2684. /* Stop the ring if it's running. */
  2685. I915_WRITE(PRB0_CTL, 0);
  2686. I915_WRITE(PRB0_TAIL, 0);
  2687. I915_WRITE(PRB0_HEAD, 0);
  2688. /* Initialize the ring. */
  2689. I915_WRITE(PRB0_START, obj_priv->gtt_offset);
  2690. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2691. /* G45 ring initialization fails to reset head to zero */
  2692. if (head != 0) {
  2693. DRM_ERROR("Ring head not reset to zero "
  2694. "ctl %08x head %08x tail %08x start %08x\n",
  2695. I915_READ(PRB0_CTL),
  2696. I915_READ(PRB0_HEAD),
  2697. I915_READ(PRB0_TAIL),
  2698. I915_READ(PRB0_START));
  2699. I915_WRITE(PRB0_HEAD, 0);
  2700. DRM_ERROR("Ring head forced to zero "
  2701. "ctl %08x head %08x tail %08x start %08x\n",
  2702. I915_READ(PRB0_CTL),
  2703. I915_READ(PRB0_HEAD),
  2704. I915_READ(PRB0_TAIL),
  2705. I915_READ(PRB0_START));
  2706. }
  2707. I915_WRITE(PRB0_CTL,
  2708. ((obj->size - 4096) & RING_NR_PAGES) |
  2709. RING_NO_REPORT |
  2710. RING_VALID);
  2711. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2712. /* If the head is still not zero, the ring is dead */
  2713. if (head != 0) {
  2714. DRM_ERROR("Ring initialization failed "
  2715. "ctl %08x head %08x tail %08x start %08x\n",
  2716. I915_READ(PRB0_CTL),
  2717. I915_READ(PRB0_HEAD),
  2718. I915_READ(PRB0_TAIL),
  2719. I915_READ(PRB0_START));
  2720. return -EIO;
  2721. }
  2722. /* Update our cache of the ring state */
  2723. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2724. i915_kernel_lost_context(dev);
  2725. else {
  2726. ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  2727. ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
  2728. ring->space = ring->head - (ring->tail + 8);
  2729. if (ring->space < 0)
  2730. ring->space += ring->Size;
  2731. }
  2732. return 0;
  2733. }
  2734. void
  2735. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  2736. {
  2737. drm_i915_private_t *dev_priv = dev->dev_private;
  2738. if (dev_priv->ring.ring_obj == NULL)
  2739. return;
  2740. drm_core_ioremapfree(&dev_priv->ring.map, dev);
  2741. i915_gem_object_unpin(dev_priv->ring.ring_obj);
  2742. drm_gem_object_unreference(dev_priv->ring.ring_obj);
  2743. dev_priv->ring.ring_obj = NULL;
  2744. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2745. if (dev_priv->hws_obj != NULL) {
  2746. struct drm_gem_object *obj = dev_priv->hws_obj;
  2747. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2748. kunmap(obj_priv->page_list[0]);
  2749. i915_gem_object_unpin(obj);
  2750. drm_gem_object_unreference(obj);
  2751. dev_priv->hws_obj = NULL;
  2752. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2753. dev_priv->hw_status_page = NULL;
  2754. /* Write high address into HWS_PGA when disabling. */
  2755. I915_WRITE(HWS_PGA, 0x1ffff000);
  2756. }
  2757. }
  2758. int
  2759. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  2760. struct drm_file *file_priv)
  2761. {
  2762. drm_i915_private_t *dev_priv = dev->dev_private;
  2763. int ret;
  2764. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2765. return 0;
  2766. if (dev_priv->mm.wedged) {
  2767. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  2768. dev_priv->mm.wedged = 0;
  2769. }
  2770. ret = i915_gem_init_ringbuffer(dev);
  2771. if (ret != 0)
  2772. return ret;
  2773. dev_priv->mm.gtt_mapping = io_mapping_create_wc(dev->agp->base,
  2774. dev->agp->agp_info.aper_size
  2775. * 1024 * 1024);
  2776. mutex_lock(&dev->struct_mutex);
  2777. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  2778. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  2779. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  2780. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  2781. dev_priv->mm.suspended = 0;
  2782. mutex_unlock(&dev->struct_mutex);
  2783. drm_irq_install(dev);
  2784. return 0;
  2785. }
  2786. int
  2787. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  2788. struct drm_file *file_priv)
  2789. {
  2790. drm_i915_private_t *dev_priv = dev->dev_private;
  2791. int ret;
  2792. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2793. return 0;
  2794. ret = i915_gem_idle(dev);
  2795. drm_irq_uninstall(dev);
  2796. io_mapping_free(dev_priv->mm.gtt_mapping);
  2797. return ret;
  2798. }
  2799. void
  2800. i915_gem_lastclose(struct drm_device *dev)
  2801. {
  2802. int ret;
  2803. ret = i915_gem_idle(dev);
  2804. if (ret)
  2805. DRM_ERROR("failed to idle hardware: %d\n", ret);
  2806. }
  2807. void
  2808. i915_gem_load(struct drm_device *dev)
  2809. {
  2810. drm_i915_private_t *dev_priv = dev->dev_private;
  2811. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  2812. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  2813. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  2814. INIT_LIST_HEAD(&dev_priv->mm.request_list);
  2815. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  2816. i915_gem_retire_work_handler);
  2817. dev_priv->mm.next_gem_seqno = 1;
  2818. /* Old X drivers will take 0-2 for front, back, depth buffers */
  2819. dev_priv->fence_reg_start = 3;
  2820. if (IS_I965G(dev))
  2821. dev_priv->num_fence_regs = 16;
  2822. else
  2823. dev_priv->num_fence_regs = 8;
  2824. i915_gem_detect_bit_6_swizzle(dev);
  2825. }