i915_gem.c 95 KB

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