i915_gem.c 93 KB

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