i915_gem.c 93 KB

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