i915_gem.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057
  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 "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/slab.h>
  34. #include <linux/swap.h>
  35. #include <linux/pci.h>
  36. static void i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
  37. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  38. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  39. static int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
  40. bool write);
  41. static int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
  42. uint64_t offset,
  43. uint64_t size);
  44. static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
  45. static int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  46. unsigned alignment,
  47. bool map_and_fenceable);
  48. static void i915_gem_clear_fence_reg(struct drm_device *dev,
  49. struct drm_i915_fence_reg *reg);
  50. static int i915_gem_phys_pwrite(struct drm_device *dev,
  51. struct drm_i915_gem_object *obj,
  52. struct drm_i915_gem_pwrite *args,
  53. struct drm_file *file);
  54. static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
  55. static int i915_gem_inactive_shrink(struct shrinker *shrinker,
  56. int nr_to_scan,
  57. gfp_t gfp_mask);
  58. /* some bookkeeping */
  59. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  60. size_t size)
  61. {
  62. dev_priv->mm.object_count++;
  63. dev_priv->mm.object_memory += size;
  64. }
  65. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  66. size_t size)
  67. {
  68. dev_priv->mm.object_count--;
  69. dev_priv->mm.object_memory -= size;
  70. }
  71. int
  72. i915_gem_check_is_wedged(struct drm_device *dev)
  73. {
  74. struct drm_i915_private *dev_priv = dev->dev_private;
  75. struct completion *x = &dev_priv->error_completion;
  76. unsigned long flags;
  77. int ret;
  78. if (!atomic_read(&dev_priv->mm.wedged))
  79. return 0;
  80. ret = wait_for_completion_interruptible(x);
  81. if (ret)
  82. return ret;
  83. /* Success, we reset the GPU! */
  84. if (!atomic_read(&dev_priv->mm.wedged))
  85. return 0;
  86. /* GPU is hung, bump the completion count to account for
  87. * the token we just consumed so that we never hit zero and
  88. * end up waiting upon a subsequent completion event that
  89. * will never happen.
  90. */
  91. spin_lock_irqsave(&x->wait.lock, flags);
  92. x->done++;
  93. spin_unlock_irqrestore(&x->wait.lock, flags);
  94. return -EIO;
  95. }
  96. int i915_mutex_lock_interruptible(struct drm_device *dev)
  97. {
  98. struct drm_i915_private *dev_priv = dev->dev_private;
  99. int ret;
  100. ret = i915_gem_check_is_wedged(dev);
  101. if (ret)
  102. return ret;
  103. ret = mutex_lock_interruptible(&dev->struct_mutex);
  104. if (ret)
  105. return ret;
  106. if (atomic_read(&dev_priv->mm.wedged)) {
  107. mutex_unlock(&dev->struct_mutex);
  108. return -EAGAIN;
  109. }
  110. WARN_ON(i915_verify_lists(dev));
  111. return 0;
  112. }
  113. static inline bool
  114. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  115. {
  116. return obj->gtt_space && !obj->active && obj->pin_count == 0;
  117. }
  118. void i915_gem_do_init(struct drm_device *dev,
  119. unsigned long start,
  120. unsigned long mappable_end,
  121. unsigned long end)
  122. {
  123. drm_i915_private_t *dev_priv = dev->dev_private;
  124. drm_mm_init(&dev_priv->mm.gtt_space, start,
  125. end - start);
  126. dev_priv->mm.gtt_total = end - start;
  127. dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
  128. dev_priv->mm.gtt_mappable_end = mappable_end;
  129. }
  130. int
  131. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  132. struct drm_file *file)
  133. {
  134. struct drm_i915_gem_init *args = data;
  135. if (args->gtt_start >= args->gtt_end ||
  136. (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
  137. return -EINVAL;
  138. mutex_lock(&dev->struct_mutex);
  139. i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
  140. mutex_unlock(&dev->struct_mutex);
  141. return 0;
  142. }
  143. int
  144. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  145. struct drm_file *file)
  146. {
  147. struct drm_i915_private *dev_priv = dev->dev_private;
  148. struct drm_i915_gem_get_aperture *args = data;
  149. struct drm_i915_gem_object *obj;
  150. size_t pinned;
  151. if (!(dev->driver->driver_features & DRIVER_GEM))
  152. return -ENODEV;
  153. pinned = 0;
  154. mutex_lock(&dev->struct_mutex);
  155. list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
  156. pinned += obj->gtt_space->size;
  157. mutex_unlock(&dev->struct_mutex);
  158. args->aper_size = dev_priv->mm.gtt_total;
  159. args->aper_available_size = args->aper_size -pinned;
  160. return 0;
  161. }
  162. /**
  163. * Creates a new mm object and returns a handle to it.
  164. */
  165. int
  166. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  167. struct drm_file *file)
  168. {
  169. struct drm_i915_gem_create *args = data;
  170. struct drm_i915_gem_object *obj;
  171. int ret;
  172. u32 handle;
  173. args->size = roundup(args->size, PAGE_SIZE);
  174. /* Allocate the new object */
  175. obj = i915_gem_alloc_object(dev, args->size);
  176. if (obj == NULL)
  177. return -ENOMEM;
  178. ret = drm_gem_handle_create(file, &obj->base, &handle);
  179. if (ret) {
  180. drm_gem_object_release(&obj->base);
  181. i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
  182. kfree(obj);
  183. return ret;
  184. }
  185. /* drop reference from allocate - handle holds it now */
  186. drm_gem_object_unreference(&obj->base);
  187. trace_i915_gem_object_create(obj);
  188. args->handle = handle;
  189. return 0;
  190. }
  191. static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
  192. {
  193. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  194. return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
  195. obj->tiling_mode != I915_TILING_NONE;
  196. }
  197. static inline void
  198. slow_shmem_copy(struct page *dst_page,
  199. int dst_offset,
  200. struct page *src_page,
  201. int src_offset,
  202. int length)
  203. {
  204. char *dst_vaddr, *src_vaddr;
  205. dst_vaddr = kmap(dst_page);
  206. src_vaddr = kmap(src_page);
  207. memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
  208. kunmap(src_page);
  209. kunmap(dst_page);
  210. }
  211. static inline void
  212. slow_shmem_bit17_copy(struct page *gpu_page,
  213. int gpu_offset,
  214. struct page *cpu_page,
  215. int cpu_offset,
  216. int length,
  217. int is_read)
  218. {
  219. char *gpu_vaddr, *cpu_vaddr;
  220. /* Use the unswizzled path if this page isn't affected. */
  221. if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
  222. if (is_read)
  223. return slow_shmem_copy(cpu_page, cpu_offset,
  224. gpu_page, gpu_offset, length);
  225. else
  226. return slow_shmem_copy(gpu_page, gpu_offset,
  227. cpu_page, cpu_offset, length);
  228. }
  229. gpu_vaddr = kmap(gpu_page);
  230. cpu_vaddr = kmap(cpu_page);
  231. /* Copy the data, XORing A6 with A17 (1). The user already knows he's
  232. * XORing with the other bits (A9 for Y, A9 and A10 for X)
  233. */
  234. while (length > 0) {
  235. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  236. int this_length = min(cacheline_end - gpu_offset, length);
  237. int swizzled_gpu_offset = gpu_offset ^ 64;
  238. if (is_read) {
  239. memcpy(cpu_vaddr + cpu_offset,
  240. gpu_vaddr + swizzled_gpu_offset,
  241. this_length);
  242. } else {
  243. memcpy(gpu_vaddr + swizzled_gpu_offset,
  244. cpu_vaddr + cpu_offset,
  245. this_length);
  246. }
  247. cpu_offset += this_length;
  248. gpu_offset += this_length;
  249. length -= this_length;
  250. }
  251. kunmap(cpu_page);
  252. kunmap(gpu_page);
  253. }
  254. /**
  255. * This is the fast shmem pread path, which attempts to copy_from_user directly
  256. * from the backing pages of the object to the user's address space. On a
  257. * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
  258. */
  259. static int
  260. i915_gem_shmem_pread_fast(struct drm_device *dev,
  261. struct drm_i915_gem_object *obj,
  262. struct drm_i915_gem_pread *args,
  263. struct drm_file *file)
  264. {
  265. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  266. ssize_t remain;
  267. loff_t offset;
  268. char __user *user_data;
  269. int page_offset, page_length;
  270. user_data = (char __user *) (uintptr_t) args->data_ptr;
  271. remain = args->size;
  272. offset = args->offset;
  273. while (remain > 0) {
  274. struct page *page;
  275. char *vaddr;
  276. int ret;
  277. /* Operation in this page
  278. *
  279. * page_offset = offset within page
  280. * page_length = bytes to copy for this page
  281. */
  282. page_offset = offset & (PAGE_SIZE-1);
  283. page_length = remain;
  284. if ((page_offset + remain) > PAGE_SIZE)
  285. page_length = PAGE_SIZE - page_offset;
  286. page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
  287. GFP_HIGHUSER | __GFP_RECLAIMABLE);
  288. if (IS_ERR(page))
  289. return PTR_ERR(page);
  290. vaddr = kmap_atomic(page);
  291. ret = __copy_to_user_inatomic(user_data,
  292. vaddr + page_offset,
  293. page_length);
  294. kunmap_atomic(vaddr);
  295. mark_page_accessed(page);
  296. page_cache_release(page);
  297. if (ret)
  298. return -EFAULT;
  299. remain -= page_length;
  300. user_data += page_length;
  301. offset += page_length;
  302. }
  303. return 0;
  304. }
  305. /**
  306. * This is the fallback shmem pread path, which allocates temporary storage
  307. * in kernel space to copy_to_user into outside of the struct_mutex, so we
  308. * can copy out of the object's backing pages while holding the struct mutex
  309. * and not take page faults.
  310. */
  311. static int
  312. i915_gem_shmem_pread_slow(struct drm_device *dev,
  313. struct drm_i915_gem_object *obj,
  314. struct drm_i915_gem_pread *args,
  315. struct drm_file *file)
  316. {
  317. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  318. struct mm_struct *mm = current->mm;
  319. struct page **user_pages;
  320. ssize_t remain;
  321. loff_t offset, pinned_pages, i;
  322. loff_t first_data_page, last_data_page, num_pages;
  323. int shmem_page_offset;
  324. int data_page_index, data_page_offset;
  325. int page_length;
  326. int ret;
  327. uint64_t data_ptr = args->data_ptr;
  328. int do_bit17_swizzling;
  329. remain = args->size;
  330. /* Pin the user pages containing the data. We can't fault while
  331. * holding the struct mutex, yet we want to hold it while
  332. * dereferencing the user data.
  333. */
  334. first_data_page = data_ptr / PAGE_SIZE;
  335. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  336. num_pages = last_data_page - first_data_page + 1;
  337. user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
  338. if (user_pages == NULL)
  339. return -ENOMEM;
  340. mutex_unlock(&dev->struct_mutex);
  341. down_read(&mm->mmap_sem);
  342. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  343. num_pages, 1, 0, user_pages, NULL);
  344. up_read(&mm->mmap_sem);
  345. mutex_lock(&dev->struct_mutex);
  346. if (pinned_pages < num_pages) {
  347. ret = -EFAULT;
  348. goto out;
  349. }
  350. ret = i915_gem_object_set_cpu_read_domain_range(obj,
  351. args->offset,
  352. args->size);
  353. if (ret)
  354. goto out;
  355. do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  356. offset = args->offset;
  357. while (remain > 0) {
  358. struct page *page;
  359. /* Operation in this page
  360. *
  361. * shmem_page_offset = offset within page in shmem file
  362. * data_page_index = page number in get_user_pages return
  363. * data_page_offset = offset with data_page_index page.
  364. * page_length = bytes to copy for this page
  365. */
  366. shmem_page_offset = offset & ~PAGE_MASK;
  367. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  368. data_page_offset = data_ptr & ~PAGE_MASK;
  369. page_length = remain;
  370. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  371. page_length = PAGE_SIZE - shmem_page_offset;
  372. if ((data_page_offset + page_length) > PAGE_SIZE)
  373. page_length = PAGE_SIZE - data_page_offset;
  374. page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
  375. GFP_HIGHUSER | __GFP_RECLAIMABLE);
  376. if (IS_ERR(page))
  377. return PTR_ERR(page);
  378. if (do_bit17_swizzling) {
  379. slow_shmem_bit17_copy(page,
  380. shmem_page_offset,
  381. user_pages[data_page_index],
  382. data_page_offset,
  383. page_length,
  384. 1);
  385. } else {
  386. slow_shmem_copy(user_pages[data_page_index],
  387. data_page_offset,
  388. page,
  389. shmem_page_offset,
  390. page_length);
  391. }
  392. mark_page_accessed(page);
  393. page_cache_release(page);
  394. remain -= page_length;
  395. data_ptr += page_length;
  396. offset += page_length;
  397. }
  398. out:
  399. for (i = 0; i < pinned_pages; i++) {
  400. SetPageDirty(user_pages[i]);
  401. mark_page_accessed(user_pages[i]);
  402. page_cache_release(user_pages[i]);
  403. }
  404. drm_free_large(user_pages);
  405. return ret;
  406. }
  407. /**
  408. * Reads data from the object referenced by handle.
  409. *
  410. * On error, the contents of *data are undefined.
  411. */
  412. int
  413. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  414. struct drm_file *file)
  415. {
  416. struct drm_i915_gem_pread *args = data;
  417. struct drm_i915_gem_object *obj;
  418. int ret = 0;
  419. if (args->size == 0)
  420. return 0;
  421. if (!access_ok(VERIFY_WRITE,
  422. (char __user *)(uintptr_t)args->data_ptr,
  423. args->size))
  424. return -EFAULT;
  425. ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
  426. args->size);
  427. if (ret)
  428. return -EFAULT;
  429. ret = i915_mutex_lock_interruptible(dev);
  430. if (ret)
  431. return ret;
  432. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  433. if (obj == NULL) {
  434. ret = -ENOENT;
  435. goto unlock;
  436. }
  437. /* Bounds check source. */
  438. if (args->offset > obj->base.size ||
  439. args->size > obj->base.size - args->offset) {
  440. ret = -EINVAL;
  441. goto out;
  442. }
  443. ret = i915_gem_object_set_cpu_read_domain_range(obj,
  444. args->offset,
  445. args->size);
  446. if (ret)
  447. goto out;
  448. ret = -EFAULT;
  449. if (!i915_gem_object_needs_bit17_swizzle(obj))
  450. ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
  451. if (ret == -EFAULT)
  452. ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
  453. out:
  454. drm_gem_object_unreference(&obj->base);
  455. unlock:
  456. mutex_unlock(&dev->struct_mutex);
  457. return ret;
  458. }
  459. /* This is the fast write path which cannot handle
  460. * page faults in the source data
  461. */
  462. static inline int
  463. fast_user_write(struct io_mapping *mapping,
  464. loff_t page_base, int page_offset,
  465. char __user *user_data,
  466. int length)
  467. {
  468. char *vaddr_atomic;
  469. unsigned long unwritten;
  470. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  471. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
  472. user_data, length);
  473. io_mapping_unmap_atomic(vaddr_atomic);
  474. return unwritten;
  475. }
  476. /* Here's the write path which can sleep for
  477. * page faults
  478. */
  479. static inline void
  480. slow_kernel_write(struct io_mapping *mapping,
  481. loff_t gtt_base, int gtt_offset,
  482. struct page *user_page, int user_offset,
  483. int length)
  484. {
  485. char __iomem *dst_vaddr;
  486. char *src_vaddr;
  487. dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
  488. src_vaddr = kmap(user_page);
  489. memcpy_toio(dst_vaddr + gtt_offset,
  490. src_vaddr + user_offset,
  491. length);
  492. kunmap(user_page);
  493. io_mapping_unmap(dst_vaddr);
  494. }
  495. /**
  496. * This is the fast pwrite path, where we copy the data directly from the
  497. * user into the GTT, uncached.
  498. */
  499. static int
  500. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  501. struct drm_i915_gem_object *obj,
  502. struct drm_i915_gem_pwrite *args,
  503. struct drm_file *file)
  504. {
  505. drm_i915_private_t *dev_priv = dev->dev_private;
  506. ssize_t remain;
  507. loff_t offset, page_base;
  508. char __user *user_data;
  509. int page_offset, page_length;
  510. user_data = (char __user *) (uintptr_t) args->data_ptr;
  511. remain = args->size;
  512. offset = obj->gtt_offset + args->offset;
  513. while (remain > 0) {
  514. /* Operation in this page
  515. *
  516. * page_base = page offset within aperture
  517. * page_offset = offset within page
  518. * page_length = bytes to copy for this page
  519. */
  520. page_base = (offset & ~(PAGE_SIZE-1));
  521. page_offset = offset & (PAGE_SIZE-1);
  522. page_length = remain;
  523. if ((page_offset + remain) > PAGE_SIZE)
  524. page_length = PAGE_SIZE - page_offset;
  525. /* If we get a fault while copying data, then (presumably) our
  526. * source page isn't available. Return the error and we'll
  527. * retry in the slow path.
  528. */
  529. if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
  530. page_offset, user_data, page_length))
  531. return -EFAULT;
  532. remain -= page_length;
  533. user_data += page_length;
  534. offset += page_length;
  535. }
  536. return 0;
  537. }
  538. /**
  539. * This is the fallback GTT pwrite path, which uses get_user_pages to pin
  540. * the memory and maps it using kmap_atomic for copying.
  541. *
  542. * This code resulted in x11perf -rgb10text consuming about 10% more CPU
  543. * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
  544. */
  545. static int
  546. i915_gem_gtt_pwrite_slow(struct drm_device *dev,
  547. struct drm_i915_gem_object *obj,
  548. struct drm_i915_gem_pwrite *args,
  549. struct drm_file *file)
  550. {
  551. drm_i915_private_t *dev_priv = dev->dev_private;
  552. ssize_t remain;
  553. loff_t gtt_page_base, offset;
  554. loff_t first_data_page, last_data_page, num_pages;
  555. loff_t pinned_pages, i;
  556. struct page **user_pages;
  557. struct mm_struct *mm = current->mm;
  558. int gtt_page_offset, data_page_offset, data_page_index, page_length;
  559. int ret;
  560. uint64_t data_ptr = args->data_ptr;
  561. remain = args->size;
  562. /* Pin the user pages containing the data. We can't fault while
  563. * holding the struct mutex, and all of the pwrite implementations
  564. * want to hold it while dereferencing the user data.
  565. */
  566. first_data_page = data_ptr / PAGE_SIZE;
  567. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  568. num_pages = last_data_page - first_data_page + 1;
  569. user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
  570. if (user_pages == NULL)
  571. return -ENOMEM;
  572. mutex_unlock(&dev->struct_mutex);
  573. down_read(&mm->mmap_sem);
  574. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  575. num_pages, 0, 0, user_pages, NULL);
  576. up_read(&mm->mmap_sem);
  577. mutex_lock(&dev->struct_mutex);
  578. if (pinned_pages < num_pages) {
  579. ret = -EFAULT;
  580. goto out_unpin_pages;
  581. }
  582. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  583. if (ret)
  584. goto out_unpin_pages;
  585. ret = i915_gem_object_put_fence(obj);
  586. if (ret)
  587. goto out_unpin_pages;
  588. offset = obj->gtt_offset + args->offset;
  589. while (remain > 0) {
  590. /* Operation in this page
  591. *
  592. * gtt_page_base = page offset within aperture
  593. * gtt_page_offset = offset within page in aperture
  594. * data_page_index = page number in get_user_pages return
  595. * data_page_offset = offset with data_page_index page.
  596. * page_length = bytes to copy for this page
  597. */
  598. gtt_page_base = offset & PAGE_MASK;
  599. gtt_page_offset = offset & ~PAGE_MASK;
  600. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  601. data_page_offset = data_ptr & ~PAGE_MASK;
  602. page_length = remain;
  603. if ((gtt_page_offset + page_length) > PAGE_SIZE)
  604. page_length = PAGE_SIZE - gtt_page_offset;
  605. if ((data_page_offset + page_length) > PAGE_SIZE)
  606. page_length = PAGE_SIZE - data_page_offset;
  607. slow_kernel_write(dev_priv->mm.gtt_mapping,
  608. gtt_page_base, gtt_page_offset,
  609. user_pages[data_page_index],
  610. data_page_offset,
  611. page_length);
  612. remain -= page_length;
  613. offset += page_length;
  614. data_ptr += page_length;
  615. }
  616. out_unpin_pages:
  617. for (i = 0; i < pinned_pages; i++)
  618. page_cache_release(user_pages[i]);
  619. drm_free_large(user_pages);
  620. return ret;
  621. }
  622. /**
  623. * This is the fast shmem pwrite path, which attempts to directly
  624. * copy_from_user into the kmapped pages backing the object.
  625. */
  626. static int
  627. i915_gem_shmem_pwrite_fast(struct drm_device *dev,
  628. struct drm_i915_gem_object *obj,
  629. struct drm_i915_gem_pwrite *args,
  630. struct drm_file *file)
  631. {
  632. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  633. ssize_t remain;
  634. loff_t offset;
  635. char __user *user_data;
  636. int page_offset, page_length;
  637. user_data = (char __user *) (uintptr_t) args->data_ptr;
  638. remain = args->size;
  639. offset = args->offset;
  640. obj->dirty = 1;
  641. while (remain > 0) {
  642. struct page *page;
  643. char *vaddr;
  644. int ret;
  645. /* Operation in this page
  646. *
  647. * page_offset = offset within page
  648. * page_length = bytes to copy for this page
  649. */
  650. page_offset = offset & (PAGE_SIZE-1);
  651. page_length = remain;
  652. if ((page_offset + remain) > PAGE_SIZE)
  653. page_length = PAGE_SIZE - page_offset;
  654. page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
  655. GFP_HIGHUSER | __GFP_RECLAIMABLE);
  656. if (IS_ERR(page))
  657. return PTR_ERR(page);
  658. vaddr = kmap_atomic(page, KM_USER0);
  659. ret = __copy_from_user_inatomic(vaddr + page_offset,
  660. user_data,
  661. page_length);
  662. kunmap_atomic(vaddr, KM_USER0);
  663. set_page_dirty(page);
  664. mark_page_accessed(page);
  665. page_cache_release(page);
  666. /* If we get a fault while copying data, then (presumably) our
  667. * source page isn't available. Return the error and we'll
  668. * retry in the slow path.
  669. */
  670. if (ret)
  671. return -EFAULT;
  672. remain -= page_length;
  673. user_data += page_length;
  674. offset += page_length;
  675. }
  676. return 0;
  677. }
  678. /**
  679. * This is the fallback shmem pwrite path, which uses get_user_pages to pin
  680. * the memory and maps it using kmap_atomic for copying.
  681. *
  682. * This avoids taking mmap_sem for faulting on the user's address while the
  683. * struct_mutex is held.
  684. */
  685. static int
  686. i915_gem_shmem_pwrite_slow(struct drm_device *dev,
  687. struct drm_i915_gem_object *obj,
  688. struct drm_i915_gem_pwrite *args,
  689. struct drm_file *file)
  690. {
  691. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  692. struct mm_struct *mm = current->mm;
  693. struct page **user_pages;
  694. ssize_t remain;
  695. loff_t offset, pinned_pages, i;
  696. loff_t first_data_page, last_data_page, num_pages;
  697. int shmem_page_offset;
  698. int data_page_index, data_page_offset;
  699. int page_length;
  700. int ret;
  701. uint64_t data_ptr = args->data_ptr;
  702. int do_bit17_swizzling;
  703. remain = args->size;
  704. /* Pin the user pages containing the data. We can't fault while
  705. * holding the struct mutex, and all of the pwrite implementations
  706. * want to hold it while dereferencing the user data.
  707. */
  708. first_data_page = data_ptr / PAGE_SIZE;
  709. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  710. num_pages = last_data_page - first_data_page + 1;
  711. user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
  712. if (user_pages == NULL)
  713. return -ENOMEM;
  714. mutex_unlock(&dev->struct_mutex);
  715. down_read(&mm->mmap_sem);
  716. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  717. num_pages, 0, 0, user_pages, NULL);
  718. up_read(&mm->mmap_sem);
  719. mutex_lock(&dev->struct_mutex);
  720. if (pinned_pages < num_pages) {
  721. ret = -EFAULT;
  722. goto out;
  723. }
  724. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  725. if (ret)
  726. goto out;
  727. do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  728. offset = args->offset;
  729. obj->dirty = 1;
  730. while (remain > 0) {
  731. struct page *page;
  732. /* Operation in this page
  733. *
  734. * shmem_page_offset = offset within page in shmem file
  735. * data_page_index = page number in get_user_pages return
  736. * data_page_offset = offset with data_page_index page.
  737. * page_length = bytes to copy for this page
  738. */
  739. shmem_page_offset = offset & ~PAGE_MASK;
  740. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  741. data_page_offset = data_ptr & ~PAGE_MASK;
  742. page_length = remain;
  743. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  744. page_length = PAGE_SIZE - shmem_page_offset;
  745. if ((data_page_offset + page_length) > PAGE_SIZE)
  746. page_length = PAGE_SIZE - data_page_offset;
  747. page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
  748. GFP_HIGHUSER | __GFP_RECLAIMABLE);
  749. if (IS_ERR(page)) {
  750. ret = PTR_ERR(page);
  751. goto out;
  752. }
  753. if (do_bit17_swizzling) {
  754. slow_shmem_bit17_copy(page,
  755. shmem_page_offset,
  756. user_pages[data_page_index],
  757. data_page_offset,
  758. page_length,
  759. 0);
  760. } else {
  761. slow_shmem_copy(page,
  762. shmem_page_offset,
  763. user_pages[data_page_index],
  764. data_page_offset,
  765. page_length);
  766. }
  767. set_page_dirty(page);
  768. mark_page_accessed(page);
  769. page_cache_release(page);
  770. remain -= page_length;
  771. data_ptr += page_length;
  772. offset += page_length;
  773. }
  774. out:
  775. for (i = 0; i < pinned_pages; i++)
  776. page_cache_release(user_pages[i]);
  777. drm_free_large(user_pages);
  778. return ret;
  779. }
  780. /**
  781. * Writes data to the object referenced by handle.
  782. *
  783. * On error, the contents of the buffer that were to be modified are undefined.
  784. */
  785. int
  786. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  787. struct drm_file *file)
  788. {
  789. struct drm_i915_gem_pwrite *args = data;
  790. struct drm_i915_gem_object *obj;
  791. int ret;
  792. if (args->size == 0)
  793. return 0;
  794. if (!access_ok(VERIFY_READ,
  795. (char __user *)(uintptr_t)args->data_ptr,
  796. args->size))
  797. return -EFAULT;
  798. ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
  799. args->size);
  800. if (ret)
  801. return -EFAULT;
  802. ret = i915_mutex_lock_interruptible(dev);
  803. if (ret)
  804. return ret;
  805. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  806. if (obj == NULL) {
  807. ret = -ENOENT;
  808. goto unlock;
  809. }
  810. /* Bounds check destination. */
  811. if (args->offset > obj->base.size ||
  812. args->size > obj->base.size - args->offset) {
  813. ret = -EINVAL;
  814. goto out;
  815. }
  816. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  817. * it would end up going through the fenced access, and we'll get
  818. * different detiling behavior between reading and writing.
  819. * pread/pwrite currently are reading and writing from the CPU
  820. * perspective, requiring manual detiling by the client.
  821. */
  822. if (obj->phys_obj)
  823. ret = i915_gem_phys_pwrite(dev, obj, args, file);
  824. else if (obj->gtt_space &&
  825. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  826. ret = i915_gem_object_pin(obj, 0, true);
  827. if (ret)
  828. goto out;
  829. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  830. if (ret)
  831. goto out_unpin;
  832. ret = i915_gem_object_put_fence(obj);
  833. if (ret)
  834. goto out_unpin;
  835. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  836. if (ret == -EFAULT)
  837. ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
  838. out_unpin:
  839. i915_gem_object_unpin(obj);
  840. } else {
  841. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  842. if (ret)
  843. goto out;
  844. ret = -EFAULT;
  845. if (!i915_gem_object_needs_bit17_swizzle(obj))
  846. ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
  847. if (ret == -EFAULT)
  848. ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
  849. }
  850. out:
  851. drm_gem_object_unreference(&obj->base);
  852. unlock:
  853. mutex_unlock(&dev->struct_mutex);
  854. return ret;
  855. }
  856. /**
  857. * Called when user space prepares to use an object with the CPU, either
  858. * through the mmap ioctl's mapping or a GTT mapping.
  859. */
  860. int
  861. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  862. struct drm_file *file)
  863. {
  864. struct drm_i915_gem_set_domain *args = data;
  865. struct drm_i915_gem_object *obj;
  866. uint32_t read_domains = args->read_domains;
  867. uint32_t write_domain = args->write_domain;
  868. int ret;
  869. if (!(dev->driver->driver_features & DRIVER_GEM))
  870. return -ENODEV;
  871. /* Only handle setting domains to types used by the CPU. */
  872. if (write_domain & I915_GEM_GPU_DOMAINS)
  873. return -EINVAL;
  874. if (read_domains & I915_GEM_GPU_DOMAINS)
  875. return -EINVAL;
  876. /* Having something in the write domain implies it's in the read
  877. * domain, and only that read domain. Enforce that in the request.
  878. */
  879. if (write_domain != 0 && read_domains != write_domain)
  880. return -EINVAL;
  881. ret = i915_mutex_lock_interruptible(dev);
  882. if (ret)
  883. return ret;
  884. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  885. if (obj == NULL) {
  886. ret = -ENOENT;
  887. goto unlock;
  888. }
  889. if (read_domains & I915_GEM_DOMAIN_GTT) {
  890. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  891. /* Silently promote "you're not bound, there was nothing to do"
  892. * to success, since the client was just asking us to
  893. * make sure everything was done.
  894. */
  895. if (ret == -EINVAL)
  896. ret = 0;
  897. } else {
  898. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  899. }
  900. drm_gem_object_unreference(&obj->base);
  901. unlock:
  902. mutex_unlock(&dev->struct_mutex);
  903. return ret;
  904. }
  905. /**
  906. * Called when user space has done writes to this buffer
  907. */
  908. int
  909. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  910. struct drm_file *file)
  911. {
  912. struct drm_i915_gem_sw_finish *args = data;
  913. struct drm_i915_gem_object *obj;
  914. int ret = 0;
  915. if (!(dev->driver->driver_features & DRIVER_GEM))
  916. return -ENODEV;
  917. ret = i915_mutex_lock_interruptible(dev);
  918. if (ret)
  919. return ret;
  920. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  921. if (obj == NULL) {
  922. ret = -ENOENT;
  923. goto unlock;
  924. }
  925. /* Pinned buffers may be scanout, so flush the cache */
  926. if (obj->pin_count)
  927. i915_gem_object_flush_cpu_write_domain(obj);
  928. drm_gem_object_unreference(&obj->base);
  929. unlock:
  930. mutex_unlock(&dev->struct_mutex);
  931. return ret;
  932. }
  933. /**
  934. * Maps the contents of an object, returning the address it is mapped
  935. * into.
  936. *
  937. * While the mapping holds a reference on the contents of the object, it doesn't
  938. * imply a ref on the object itself.
  939. */
  940. int
  941. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  942. struct drm_file *file)
  943. {
  944. struct drm_i915_private *dev_priv = dev->dev_private;
  945. struct drm_i915_gem_mmap *args = data;
  946. struct drm_gem_object *obj;
  947. loff_t offset;
  948. unsigned long addr;
  949. if (!(dev->driver->driver_features & DRIVER_GEM))
  950. return -ENODEV;
  951. obj = drm_gem_object_lookup(dev, file, args->handle);
  952. if (obj == NULL)
  953. return -ENOENT;
  954. if (obj->size > dev_priv->mm.gtt_mappable_end) {
  955. drm_gem_object_unreference_unlocked(obj);
  956. return -E2BIG;
  957. }
  958. offset = args->offset;
  959. down_write(&current->mm->mmap_sem);
  960. addr = do_mmap(obj->filp, 0, args->size,
  961. PROT_READ | PROT_WRITE, MAP_SHARED,
  962. args->offset);
  963. up_write(&current->mm->mmap_sem);
  964. drm_gem_object_unreference_unlocked(obj);
  965. if (IS_ERR((void *)addr))
  966. return addr;
  967. args->addr_ptr = (uint64_t) addr;
  968. return 0;
  969. }
  970. /**
  971. * i915_gem_fault - fault a page into the GTT
  972. * vma: VMA in question
  973. * vmf: fault info
  974. *
  975. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  976. * from userspace. The fault handler takes care of binding the object to
  977. * the GTT (if needed), allocating and programming a fence register (again,
  978. * only if needed based on whether the old reg is still valid or the object
  979. * is tiled) and inserting a new PTE into the faulting process.
  980. *
  981. * Note that the faulting process may involve evicting existing objects
  982. * from the GTT and/or fence registers to make room. So performance may
  983. * suffer if the GTT working set is large or there are few fence registers
  984. * left.
  985. */
  986. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  987. {
  988. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  989. struct drm_device *dev = obj->base.dev;
  990. drm_i915_private_t *dev_priv = dev->dev_private;
  991. pgoff_t page_offset;
  992. unsigned long pfn;
  993. int ret = 0;
  994. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  995. /* We don't use vmf->pgoff since that has the fake offset */
  996. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  997. PAGE_SHIFT;
  998. /* Now bind it into the GTT if needed */
  999. mutex_lock(&dev->struct_mutex);
  1000. if (!obj->map_and_fenceable) {
  1001. ret = i915_gem_object_unbind(obj);
  1002. if (ret)
  1003. goto unlock;
  1004. }
  1005. if (!obj->gtt_space) {
  1006. ret = i915_gem_object_bind_to_gtt(obj, 0, true);
  1007. if (ret)
  1008. goto unlock;
  1009. }
  1010. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1011. if (ret)
  1012. goto unlock;
  1013. if (obj->tiling_mode == I915_TILING_NONE)
  1014. ret = i915_gem_object_put_fence(obj);
  1015. else
  1016. ret = i915_gem_object_get_fence(obj, NULL, true);
  1017. if (ret)
  1018. goto unlock;
  1019. if (i915_gem_object_is_inactive(obj))
  1020. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  1021. obj->fault_mappable = true;
  1022. pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
  1023. page_offset;
  1024. /* Finally, remap it using the new GTT offset */
  1025. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  1026. unlock:
  1027. mutex_unlock(&dev->struct_mutex);
  1028. switch (ret) {
  1029. case -EAGAIN:
  1030. set_need_resched();
  1031. case 0:
  1032. case -ERESTARTSYS:
  1033. return VM_FAULT_NOPAGE;
  1034. case -ENOMEM:
  1035. return VM_FAULT_OOM;
  1036. default:
  1037. return VM_FAULT_SIGBUS;
  1038. }
  1039. }
  1040. /**
  1041. * i915_gem_create_mmap_offset - create a fake mmap offset for an object
  1042. * @obj: obj in question
  1043. *
  1044. * GEM memory mapping works by handing back to userspace a fake mmap offset
  1045. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  1046. * up the object based on the offset and sets up the various memory mapping
  1047. * structures.
  1048. *
  1049. * This routine allocates and attaches a fake offset for @obj.
  1050. */
  1051. static int
  1052. i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
  1053. {
  1054. struct drm_device *dev = obj->base.dev;
  1055. struct drm_gem_mm *mm = dev->mm_private;
  1056. struct drm_map_list *list;
  1057. struct drm_local_map *map;
  1058. int ret = 0;
  1059. /* Set the object up for mmap'ing */
  1060. list = &obj->base.map_list;
  1061. list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
  1062. if (!list->map)
  1063. return -ENOMEM;
  1064. map = list->map;
  1065. map->type = _DRM_GEM;
  1066. map->size = obj->base.size;
  1067. map->handle = obj;
  1068. /* Get a DRM GEM mmap offset allocated... */
  1069. list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
  1070. obj->base.size / PAGE_SIZE,
  1071. 0, 0);
  1072. if (!list->file_offset_node) {
  1073. DRM_ERROR("failed to allocate offset for bo %d\n",
  1074. obj->base.name);
  1075. ret = -ENOSPC;
  1076. goto out_free_list;
  1077. }
  1078. list->file_offset_node = drm_mm_get_block(list->file_offset_node,
  1079. obj->base.size / PAGE_SIZE,
  1080. 0);
  1081. if (!list->file_offset_node) {
  1082. ret = -ENOMEM;
  1083. goto out_free_list;
  1084. }
  1085. list->hash.key = list->file_offset_node->start;
  1086. ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
  1087. if (ret) {
  1088. DRM_ERROR("failed to add to map hash\n");
  1089. goto out_free_mm;
  1090. }
  1091. return 0;
  1092. out_free_mm:
  1093. drm_mm_put_block(list->file_offset_node);
  1094. out_free_list:
  1095. kfree(list->map);
  1096. list->map = NULL;
  1097. return ret;
  1098. }
  1099. /**
  1100. * i915_gem_release_mmap - remove physical page mappings
  1101. * @obj: obj in question
  1102. *
  1103. * Preserve the reservation of the mmapping with the DRM core code, but
  1104. * relinquish ownership of the pages back to the system.
  1105. *
  1106. * It is vital that we remove the page mapping if we have mapped a tiled
  1107. * object through the GTT and then lose the fence register due to
  1108. * resource pressure. Similarly if the object has been moved out of the
  1109. * aperture, than pages mapped into userspace must be revoked. Removing the
  1110. * mapping will then trigger a page fault on the next user access, allowing
  1111. * fixup by i915_gem_fault().
  1112. */
  1113. void
  1114. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1115. {
  1116. if (!obj->fault_mappable)
  1117. return;
  1118. unmap_mapping_range(obj->base.dev->dev_mapping,
  1119. (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
  1120. obj->base.size, 1);
  1121. obj->fault_mappable = false;
  1122. }
  1123. static void
  1124. i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
  1125. {
  1126. struct drm_device *dev = obj->base.dev;
  1127. struct drm_gem_mm *mm = dev->mm_private;
  1128. struct drm_map_list *list = &obj->base.map_list;
  1129. drm_ht_remove_item(&mm->offset_hash, &list->hash);
  1130. drm_mm_put_block(list->file_offset_node);
  1131. kfree(list->map);
  1132. list->map = NULL;
  1133. }
  1134. static uint32_t
  1135. i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
  1136. {
  1137. struct drm_device *dev = obj->base.dev;
  1138. uint32_t size;
  1139. if (INTEL_INFO(dev)->gen >= 4 ||
  1140. obj->tiling_mode == I915_TILING_NONE)
  1141. return obj->base.size;
  1142. /* Previous chips need a power-of-two fence region when tiling */
  1143. if (INTEL_INFO(dev)->gen == 3)
  1144. size = 1024*1024;
  1145. else
  1146. size = 512*1024;
  1147. while (size < obj->base.size)
  1148. size <<= 1;
  1149. return size;
  1150. }
  1151. /**
  1152. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1153. * @obj: object to check
  1154. *
  1155. * Return the required GTT alignment for an object, taking into account
  1156. * potential fence register mapping.
  1157. */
  1158. static uint32_t
  1159. i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
  1160. {
  1161. struct drm_device *dev = obj->base.dev;
  1162. /*
  1163. * Minimum alignment is 4k (GTT page size), but might be greater
  1164. * if a fence register is needed for the object.
  1165. */
  1166. if (INTEL_INFO(dev)->gen >= 4 ||
  1167. obj->tiling_mode == I915_TILING_NONE)
  1168. return 4096;
  1169. /*
  1170. * Previous chips need to be aligned to the size of the smallest
  1171. * fence register that can contain the object.
  1172. */
  1173. return i915_gem_get_gtt_size(obj);
  1174. }
  1175. /**
  1176. * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
  1177. * unfenced object
  1178. * @obj: object to check
  1179. *
  1180. * Return the required GTT alignment for an object, only taking into account
  1181. * unfenced tiled surface requirements.
  1182. */
  1183. static uint32_t
  1184. i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
  1185. {
  1186. struct drm_device *dev = obj->base.dev;
  1187. int tile_height;
  1188. /*
  1189. * Minimum alignment is 4k (GTT page size) for sane hw.
  1190. */
  1191. if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
  1192. obj->tiling_mode == I915_TILING_NONE)
  1193. return 4096;
  1194. /*
  1195. * Older chips need unfenced tiled buffers to be aligned to the left
  1196. * edge of an even tile row (where tile rows are counted as if the bo is
  1197. * placed in a fenced gtt region).
  1198. */
  1199. if (IS_GEN2(dev) ||
  1200. (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
  1201. tile_height = 32;
  1202. else
  1203. tile_height = 8;
  1204. return tile_height * obj->stride * 2;
  1205. }
  1206. /**
  1207. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1208. * @dev: DRM device
  1209. * @data: GTT mapping ioctl data
  1210. * @file: GEM object info
  1211. *
  1212. * Simply returns the fake offset to userspace so it can mmap it.
  1213. * The mmap call will end up in drm_gem_mmap(), which will set things
  1214. * up so we can get faults in the handler above.
  1215. *
  1216. * The fault handler will take care of binding the object into the GTT
  1217. * (since it may have been evicted to make room for something), allocating
  1218. * a fence register, and mapping the appropriate aperture address into
  1219. * userspace.
  1220. */
  1221. int
  1222. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1223. struct drm_file *file)
  1224. {
  1225. struct drm_i915_private *dev_priv = dev->dev_private;
  1226. struct drm_i915_gem_mmap_gtt *args = data;
  1227. struct drm_i915_gem_object *obj;
  1228. int ret;
  1229. if (!(dev->driver->driver_features & DRIVER_GEM))
  1230. return -ENODEV;
  1231. ret = i915_mutex_lock_interruptible(dev);
  1232. if (ret)
  1233. return ret;
  1234. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1235. if (obj == NULL) {
  1236. ret = -ENOENT;
  1237. goto unlock;
  1238. }
  1239. if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
  1240. ret = -E2BIG;
  1241. goto unlock;
  1242. }
  1243. if (obj->madv != I915_MADV_WILLNEED) {
  1244. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1245. ret = -EINVAL;
  1246. goto out;
  1247. }
  1248. if (!obj->base.map_list.map) {
  1249. ret = i915_gem_create_mmap_offset(obj);
  1250. if (ret)
  1251. goto out;
  1252. }
  1253. args->offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
  1254. out:
  1255. drm_gem_object_unreference(&obj->base);
  1256. unlock:
  1257. mutex_unlock(&dev->struct_mutex);
  1258. return ret;
  1259. }
  1260. static int
  1261. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
  1262. gfp_t gfpmask)
  1263. {
  1264. int page_count, i;
  1265. struct address_space *mapping;
  1266. struct inode *inode;
  1267. struct page *page;
  1268. /* Get the list of pages out of our struct file. They'll be pinned
  1269. * at this point until we release them.
  1270. */
  1271. page_count = obj->base.size / PAGE_SIZE;
  1272. BUG_ON(obj->pages != NULL);
  1273. obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
  1274. if (obj->pages == NULL)
  1275. return -ENOMEM;
  1276. inode = obj->base.filp->f_path.dentry->d_inode;
  1277. mapping = inode->i_mapping;
  1278. for (i = 0; i < page_count; i++) {
  1279. page = read_cache_page_gfp(mapping, i,
  1280. GFP_HIGHUSER |
  1281. __GFP_COLD |
  1282. __GFP_RECLAIMABLE |
  1283. gfpmask);
  1284. if (IS_ERR(page))
  1285. goto err_pages;
  1286. obj->pages[i] = page;
  1287. }
  1288. if (obj->tiling_mode != I915_TILING_NONE)
  1289. i915_gem_object_do_bit_17_swizzle(obj);
  1290. return 0;
  1291. err_pages:
  1292. while (i--)
  1293. page_cache_release(obj->pages[i]);
  1294. drm_free_large(obj->pages);
  1295. obj->pages = NULL;
  1296. return PTR_ERR(page);
  1297. }
  1298. static void
  1299. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1300. {
  1301. int page_count = obj->base.size / PAGE_SIZE;
  1302. int i;
  1303. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1304. if (obj->tiling_mode != I915_TILING_NONE)
  1305. i915_gem_object_save_bit_17_swizzle(obj);
  1306. if (obj->madv == I915_MADV_DONTNEED)
  1307. obj->dirty = 0;
  1308. for (i = 0; i < page_count; i++) {
  1309. if (obj->dirty)
  1310. set_page_dirty(obj->pages[i]);
  1311. if (obj->madv == I915_MADV_WILLNEED)
  1312. mark_page_accessed(obj->pages[i]);
  1313. page_cache_release(obj->pages[i]);
  1314. }
  1315. obj->dirty = 0;
  1316. drm_free_large(obj->pages);
  1317. obj->pages = NULL;
  1318. }
  1319. void
  1320. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1321. struct intel_ring_buffer *ring,
  1322. u32 seqno)
  1323. {
  1324. struct drm_device *dev = obj->base.dev;
  1325. struct drm_i915_private *dev_priv = dev->dev_private;
  1326. BUG_ON(ring == NULL);
  1327. obj->ring = ring;
  1328. /* Add a reference if we're newly entering the active list. */
  1329. if (!obj->active) {
  1330. drm_gem_object_reference(&obj->base);
  1331. obj->active = 1;
  1332. }
  1333. /* Move from whatever list we were on to the tail of execution. */
  1334. list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
  1335. list_move_tail(&obj->ring_list, &ring->active_list);
  1336. obj->last_rendering_seqno = seqno;
  1337. if (obj->fenced_gpu_access) {
  1338. struct drm_i915_fence_reg *reg;
  1339. BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
  1340. obj->last_fenced_seqno = seqno;
  1341. obj->last_fenced_ring = ring;
  1342. reg = &dev_priv->fence_regs[obj->fence_reg];
  1343. list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  1344. }
  1345. }
  1346. static void
  1347. i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
  1348. {
  1349. list_del_init(&obj->ring_list);
  1350. obj->last_rendering_seqno = 0;
  1351. }
  1352. static void
  1353. i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
  1354. {
  1355. struct drm_device *dev = obj->base.dev;
  1356. drm_i915_private_t *dev_priv = dev->dev_private;
  1357. BUG_ON(!obj->active);
  1358. list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
  1359. i915_gem_object_move_off_active(obj);
  1360. }
  1361. static void
  1362. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1363. {
  1364. struct drm_device *dev = obj->base.dev;
  1365. struct drm_i915_private *dev_priv = dev->dev_private;
  1366. if (obj->pin_count != 0)
  1367. list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
  1368. else
  1369. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  1370. BUG_ON(!list_empty(&obj->gpu_write_list));
  1371. BUG_ON(!obj->active);
  1372. obj->ring = NULL;
  1373. i915_gem_object_move_off_active(obj);
  1374. obj->fenced_gpu_access = false;
  1375. obj->active = 0;
  1376. obj->pending_gpu_write = false;
  1377. drm_gem_object_unreference(&obj->base);
  1378. WARN_ON(i915_verify_lists(dev));
  1379. }
  1380. /* Immediately discard the backing storage */
  1381. static void
  1382. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1383. {
  1384. struct inode *inode;
  1385. /* Our goal here is to return as much of the memory as
  1386. * is possible back to the system as we are called from OOM.
  1387. * To do this we must instruct the shmfs to drop all of its
  1388. * backing pages, *now*. Here we mirror the actions taken
  1389. * when by shmem_delete_inode() to release the backing store.
  1390. */
  1391. inode = obj->base.filp->f_path.dentry->d_inode;
  1392. truncate_inode_pages(inode->i_mapping, 0);
  1393. if (inode->i_op->truncate_range)
  1394. inode->i_op->truncate_range(inode, 0, (loff_t)-1);
  1395. obj->madv = __I915_MADV_PURGED;
  1396. }
  1397. static inline int
  1398. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1399. {
  1400. return obj->madv == I915_MADV_DONTNEED;
  1401. }
  1402. static void
  1403. i915_gem_process_flushing_list(struct drm_device *dev,
  1404. uint32_t flush_domains,
  1405. struct intel_ring_buffer *ring)
  1406. {
  1407. struct drm_i915_gem_object *obj, *next;
  1408. list_for_each_entry_safe(obj, next,
  1409. &ring->gpu_write_list,
  1410. gpu_write_list) {
  1411. if (obj->base.write_domain & flush_domains) {
  1412. uint32_t old_write_domain = obj->base.write_domain;
  1413. obj->base.write_domain = 0;
  1414. list_del_init(&obj->gpu_write_list);
  1415. i915_gem_object_move_to_active(obj, ring,
  1416. i915_gem_next_request_seqno(dev, ring));
  1417. trace_i915_gem_object_change_domain(obj,
  1418. obj->base.read_domains,
  1419. old_write_domain);
  1420. }
  1421. }
  1422. }
  1423. int
  1424. i915_add_request(struct drm_device *dev,
  1425. struct drm_file *file,
  1426. struct drm_i915_gem_request *request,
  1427. struct intel_ring_buffer *ring)
  1428. {
  1429. drm_i915_private_t *dev_priv = dev->dev_private;
  1430. struct drm_i915_file_private *file_priv = NULL;
  1431. uint32_t seqno;
  1432. int was_empty;
  1433. int ret;
  1434. BUG_ON(request == NULL);
  1435. if (file != NULL)
  1436. file_priv = file->driver_priv;
  1437. ret = ring->add_request(ring, &seqno);
  1438. if (ret)
  1439. return ret;
  1440. ring->outstanding_lazy_request = false;
  1441. request->seqno = seqno;
  1442. request->ring = ring;
  1443. request->emitted_jiffies = jiffies;
  1444. was_empty = list_empty(&ring->request_list);
  1445. list_add_tail(&request->list, &ring->request_list);
  1446. if (file_priv) {
  1447. spin_lock(&file_priv->mm.lock);
  1448. request->file_priv = file_priv;
  1449. list_add_tail(&request->client_list,
  1450. &file_priv->mm.request_list);
  1451. spin_unlock(&file_priv->mm.lock);
  1452. }
  1453. if (!dev_priv->mm.suspended) {
  1454. mod_timer(&dev_priv->hangcheck_timer,
  1455. jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
  1456. if (was_empty)
  1457. queue_delayed_work(dev_priv->wq,
  1458. &dev_priv->mm.retire_work, HZ);
  1459. }
  1460. return 0;
  1461. }
  1462. static inline void
  1463. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1464. {
  1465. struct drm_i915_file_private *file_priv = request->file_priv;
  1466. if (!file_priv)
  1467. return;
  1468. spin_lock(&file_priv->mm.lock);
  1469. list_del(&request->client_list);
  1470. request->file_priv = NULL;
  1471. spin_unlock(&file_priv->mm.lock);
  1472. }
  1473. static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
  1474. struct intel_ring_buffer *ring)
  1475. {
  1476. while (!list_empty(&ring->request_list)) {
  1477. struct drm_i915_gem_request *request;
  1478. request = list_first_entry(&ring->request_list,
  1479. struct drm_i915_gem_request,
  1480. list);
  1481. list_del(&request->list);
  1482. i915_gem_request_remove_from_client(request);
  1483. kfree(request);
  1484. }
  1485. while (!list_empty(&ring->active_list)) {
  1486. struct drm_i915_gem_object *obj;
  1487. obj = list_first_entry(&ring->active_list,
  1488. struct drm_i915_gem_object,
  1489. ring_list);
  1490. obj->base.write_domain = 0;
  1491. list_del_init(&obj->gpu_write_list);
  1492. i915_gem_object_move_to_inactive(obj);
  1493. }
  1494. }
  1495. static void i915_gem_reset_fences(struct drm_device *dev)
  1496. {
  1497. struct drm_i915_private *dev_priv = dev->dev_private;
  1498. int i;
  1499. for (i = 0; i < 16; i++) {
  1500. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1501. struct drm_i915_gem_object *obj = reg->obj;
  1502. if (!obj)
  1503. continue;
  1504. if (obj->tiling_mode)
  1505. i915_gem_release_mmap(obj);
  1506. reg->obj->fence_reg = I915_FENCE_REG_NONE;
  1507. reg->obj->fenced_gpu_access = false;
  1508. reg->obj->last_fenced_seqno = 0;
  1509. reg->obj->last_fenced_ring = NULL;
  1510. i915_gem_clear_fence_reg(dev, reg);
  1511. }
  1512. }
  1513. void i915_gem_reset(struct drm_device *dev)
  1514. {
  1515. struct drm_i915_private *dev_priv = dev->dev_private;
  1516. struct drm_i915_gem_object *obj;
  1517. int i;
  1518. for (i = 0; i < I915_NUM_RINGS; i++)
  1519. i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
  1520. /* Remove anything from the flushing lists. The GPU cache is likely
  1521. * to be lost on reset along with the data, so simply move the
  1522. * lost bo to the inactive list.
  1523. */
  1524. while (!list_empty(&dev_priv->mm.flushing_list)) {
  1525. obj= list_first_entry(&dev_priv->mm.flushing_list,
  1526. struct drm_i915_gem_object,
  1527. mm_list);
  1528. obj->base.write_domain = 0;
  1529. list_del_init(&obj->gpu_write_list);
  1530. i915_gem_object_move_to_inactive(obj);
  1531. }
  1532. /* Move everything out of the GPU domains to ensure we do any
  1533. * necessary invalidation upon reuse.
  1534. */
  1535. list_for_each_entry(obj,
  1536. &dev_priv->mm.inactive_list,
  1537. mm_list)
  1538. {
  1539. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  1540. }
  1541. /* The fence registers are invalidated so clear them out */
  1542. i915_gem_reset_fences(dev);
  1543. }
  1544. /**
  1545. * This function clears the request list as sequence numbers are passed.
  1546. */
  1547. static void
  1548. i915_gem_retire_requests_ring(struct drm_device *dev,
  1549. struct intel_ring_buffer *ring)
  1550. {
  1551. drm_i915_private_t *dev_priv = dev->dev_private;
  1552. uint32_t seqno;
  1553. int i;
  1554. if (!ring->status_page.page_addr ||
  1555. list_empty(&ring->request_list))
  1556. return;
  1557. WARN_ON(i915_verify_lists(dev));
  1558. seqno = ring->get_seqno(ring);
  1559. for (i = 0; i < I915_NUM_RINGS; i++)
  1560. if (seqno >= ring->sync_seqno[i])
  1561. ring->sync_seqno[i] = 0;
  1562. while (!list_empty(&ring->request_list)) {
  1563. struct drm_i915_gem_request *request;
  1564. request = list_first_entry(&ring->request_list,
  1565. struct drm_i915_gem_request,
  1566. list);
  1567. if (!i915_seqno_passed(seqno, request->seqno))
  1568. break;
  1569. trace_i915_gem_request_retire(dev, request->seqno);
  1570. list_del(&request->list);
  1571. i915_gem_request_remove_from_client(request);
  1572. kfree(request);
  1573. }
  1574. /* Move any buffers on the active list that are no longer referenced
  1575. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1576. */
  1577. while (!list_empty(&ring->active_list)) {
  1578. struct drm_i915_gem_object *obj;
  1579. obj= list_first_entry(&ring->active_list,
  1580. struct drm_i915_gem_object,
  1581. ring_list);
  1582. if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
  1583. break;
  1584. if (obj->base.write_domain != 0)
  1585. i915_gem_object_move_to_flushing(obj);
  1586. else
  1587. i915_gem_object_move_to_inactive(obj);
  1588. }
  1589. if (unlikely (dev_priv->trace_irq_seqno &&
  1590. i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
  1591. ring->irq_put(ring);
  1592. dev_priv->trace_irq_seqno = 0;
  1593. }
  1594. WARN_ON(i915_verify_lists(dev));
  1595. }
  1596. void
  1597. i915_gem_retire_requests(struct drm_device *dev)
  1598. {
  1599. drm_i915_private_t *dev_priv = dev->dev_private;
  1600. int i;
  1601. if (!list_empty(&dev_priv->mm.deferred_free_list)) {
  1602. struct drm_i915_gem_object *obj, *next;
  1603. /* We must be careful that during unbind() we do not
  1604. * accidentally infinitely recurse into retire requests.
  1605. * Currently:
  1606. * retire -> free -> unbind -> wait -> retire_ring
  1607. */
  1608. list_for_each_entry_safe(obj, next,
  1609. &dev_priv->mm.deferred_free_list,
  1610. mm_list)
  1611. i915_gem_free_object_tail(obj);
  1612. }
  1613. for (i = 0; i < I915_NUM_RINGS; i++)
  1614. i915_gem_retire_requests_ring(dev, &dev_priv->ring[i]);
  1615. }
  1616. static void
  1617. i915_gem_retire_work_handler(struct work_struct *work)
  1618. {
  1619. drm_i915_private_t *dev_priv;
  1620. struct drm_device *dev;
  1621. dev_priv = container_of(work, drm_i915_private_t,
  1622. mm.retire_work.work);
  1623. dev = dev_priv->dev;
  1624. /* Come back later if the device is busy... */
  1625. if (!mutex_trylock(&dev->struct_mutex)) {
  1626. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1627. return;
  1628. }
  1629. i915_gem_retire_requests(dev);
  1630. if (!dev_priv->mm.suspended &&
  1631. (!list_empty(&dev_priv->ring[RCS].request_list) ||
  1632. !list_empty(&dev_priv->ring[VCS].request_list) ||
  1633. !list_empty(&dev_priv->ring[BCS].request_list)))
  1634. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1635. mutex_unlock(&dev->struct_mutex);
  1636. }
  1637. int
  1638. i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
  1639. bool interruptible, struct intel_ring_buffer *ring)
  1640. {
  1641. drm_i915_private_t *dev_priv = dev->dev_private;
  1642. u32 ier;
  1643. int ret = 0;
  1644. BUG_ON(seqno == 0);
  1645. if (atomic_read(&dev_priv->mm.wedged))
  1646. return -EAGAIN;
  1647. if (seqno == ring->outstanding_lazy_request) {
  1648. struct drm_i915_gem_request *request;
  1649. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1650. if (request == NULL)
  1651. return -ENOMEM;
  1652. ret = i915_add_request(dev, NULL, request, ring);
  1653. if (ret) {
  1654. kfree(request);
  1655. return ret;
  1656. }
  1657. seqno = request->seqno;
  1658. }
  1659. if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
  1660. if (HAS_PCH_SPLIT(dev))
  1661. ier = I915_READ(DEIER) | I915_READ(GTIER);
  1662. else
  1663. ier = I915_READ(IER);
  1664. if (!ier) {
  1665. DRM_ERROR("something (likely vbetool) disabled "
  1666. "interrupts, re-enabling\n");
  1667. i915_driver_irq_preinstall(dev);
  1668. i915_driver_irq_postinstall(dev);
  1669. }
  1670. trace_i915_gem_request_wait_begin(dev, seqno);
  1671. ring->waiting_seqno = seqno;
  1672. ret = -ENODEV;
  1673. if (ring->irq_get(ring)) {
  1674. if (interruptible)
  1675. ret = wait_event_interruptible(ring->irq_queue,
  1676. i915_seqno_passed(ring->get_seqno(ring), seqno)
  1677. || atomic_read(&dev_priv->mm.wedged));
  1678. else
  1679. wait_event(ring->irq_queue,
  1680. i915_seqno_passed(ring->get_seqno(ring), seqno)
  1681. || atomic_read(&dev_priv->mm.wedged));
  1682. ring->irq_put(ring);
  1683. }
  1684. ring->waiting_seqno = 0;
  1685. trace_i915_gem_request_wait_end(dev, seqno);
  1686. }
  1687. if (atomic_read(&dev_priv->mm.wedged))
  1688. ret = -EAGAIN;
  1689. if (ret && ret != -ERESTARTSYS)
  1690. DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
  1691. __func__, ret, seqno, ring->get_seqno(ring),
  1692. dev_priv->next_seqno);
  1693. /* Directly dispatch request retiring. While we have the work queue
  1694. * to handle this, the waiter on a request often wants an associated
  1695. * buffer to have made it to the inactive list, and we would need
  1696. * a separate wait queue to handle that.
  1697. */
  1698. if (ret == 0)
  1699. i915_gem_retire_requests_ring(dev, ring);
  1700. return ret;
  1701. }
  1702. /**
  1703. * Waits for a sequence number to be signaled, and cleans up the
  1704. * request and object lists appropriately for that event.
  1705. */
  1706. static int
  1707. i915_wait_request(struct drm_device *dev, uint32_t seqno,
  1708. struct intel_ring_buffer *ring)
  1709. {
  1710. return i915_do_wait_request(dev, seqno, 1, ring);
  1711. }
  1712. /**
  1713. * Ensures that all rendering to the object has completed and the object is
  1714. * safe to unbind from the GTT or access from the CPU.
  1715. */
  1716. int
  1717. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  1718. bool interruptible)
  1719. {
  1720. struct drm_device *dev = obj->base.dev;
  1721. int ret;
  1722. /* This function only exists to support waiting for existing rendering,
  1723. * not for emitting required flushes.
  1724. */
  1725. BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
  1726. /* If there is rendering queued on the buffer being evicted, wait for
  1727. * it.
  1728. */
  1729. if (obj->active) {
  1730. ret = i915_do_wait_request(dev,
  1731. obj->last_rendering_seqno,
  1732. interruptible,
  1733. obj->ring);
  1734. if (ret)
  1735. return ret;
  1736. }
  1737. return 0;
  1738. }
  1739. /**
  1740. * Unbinds an object from the GTT aperture.
  1741. */
  1742. int
  1743. i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  1744. {
  1745. int ret = 0;
  1746. if (obj->gtt_space == NULL)
  1747. return 0;
  1748. if (obj->pin_count != 0) {
  1749. DRM_ERROR("Attempting to unbind pinned buffer\n");
  1750. return -EINVAL;
  1751. }
  1752. /* blow away mappings if mapped through GTT */
  1753. i915_gem_release_mmap(obj);
  1754. /* Move the object to the CPU domain to ensure that
  1755. * any possible CPU writes while it's not in the GTT
  1756. * are flushed when we go to remap it. This will
  1757. * also ensure that all pending GPU writes are finished
  1758. * before we unbind.
  1759. */
  1760. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  1761. if (ret == -ERESTARTSYS)
  1762. return ret;
  1763. /* Continue on if we fail due to EIO, the GPU is hung so we
  1764. * should be safe and we need to cleanup or else we might
  1765. * cause memory corruption through use-after-free.
  1766. */
  1767. if (ret) {
  1768. i915_gem_clflush_object(obj);
  1769. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1770. }
  1771. /* release the fence reg _after_ flushing */
  1772. ret = i915_gem_object_put_fence(obj);
  1773. if (ret == -ERESTARTSYS)
  1774. return ret;
  1775. i915_gem_gtt_unbind_object(obj);
  1776. i915_gem_object_put_pages_gtt(obj);
  1777. list_del_init(&obj->gtt_list);
  1778. list_del_init(&obj->mm_list);
  1779. /* Avoid an unnecessary call to unbind on rebind. */
  1780. obj->map_and_fenceable = true;
  1781. drm_mm_put_block(obj->gtt_space);
  1782. obj->gtt_space = NULL;
  1783. obj->gtt_offset = 0;
  1784. if (i915_gem_object_is_purgeable(obj))
  1785. i915_gem_object_truncate(obj);
  1786. trace_i915_gem_object_unbind(obj);
  1787. return ret;
  1788. }
  1789. void
  1790. i915_gem_flush_ring(struct drm_device *dev,
  1791. struct intel_ring_buffer *ring,
  1792. uint32_t invalidate_domains,
  1793. uint32_t flush_domains)
  1794. {
  1795. ring->flush(ring, invalidate_domains, flush_domains);
  1796. i915_gem_process_flushing_list(dev, flush_domains, ring);
  1797. }
  1798. static int i915_ring_idle(struct drm_device *dev,
  1799. struct intel_ring_buffer *ring)
  1800. {
  1801. if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
  1802. return 0;
  1803. if (!list_empty(&ring->gpu_write_list))
  1804. i915_gem_flush_ring(dev, ring,
  1805. I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  1806. return i915_wait_request(dev,
  1807. i915_gem_next_request_seqno(dev, ring),
  1808. ring);
  1809. }
  1810. int
  1811. i915_gpu_idle(struct drm_device *dev)
  1812. {
  1813. drm_i915_private_t *dev_priv = dev->dev_private;
  1814. bool lists_empty;
  1815. int ret, i;
  1816. lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
  1817. list_empty(&dev_priv->mm.active_list));
  1818. if (lists_empty)
  1819. return 0;
  1820. /* Flush everything onto the inactive list. */
  1821. for (i = 0; i < I915_NUM_RINGS; i++) {
  1822. ret = i915_ring_idle(dev, &dev_priv->ring[i]);
  1823. if (ret)
  1824. return ret;
  1825. }
  1826. return 0;
  1827. }
  1828. static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
  1829. struct intel_ring_buffer *pipelined)
  1830. {
  1831. struct drm_device *dev = obj->base.dev;
  1832. drm_i915_private_t *dev_priv = dev->dev_private;
  1833. u32 size = obj->gtt_space->size;
  1834. int regnum = obj->fence_reg;
  1835. uint64_t val;
  1836. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  1837. 0xfffff000) << 32;
  1838. val |= obj->gtt_offset & 0xfffff000;
  1839. val |= (uint64_t)((obj->stride / 128) - 1) <<
  1840. SANDYBRIDGE_FENCE_PITCH_SHIFT;
  1841. if (obj->tiling_mode == I915_TILING_Y)
  1842. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1843. val |= I965_FENCE_REG_VALID;
  1844. if (pipelined) {
  1845. int ret = intel_ring_begin(pipelined, 6);
  1846. if (ret)
  1847. return ret;
  1848. intel_ring_emit(pipelined, MI_NOOP);
  1849. intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
  1850. intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
  1851. intel_ring_emit(pipelined, (u32)val);
  1852. intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
  1853. intel_ring_emit(pipelined, (u32)(val >> 32));
  1854. intel_ring_advance(pipelined);
  1855. } else
  1856. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
  1857. return 0;
  1858. }
  1859. static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
  1860. struct intel_ring_buffer *pipelined)
  1861. {
  1862. struct drm_device *dev = obj->base.dev;
  1863. drm_i915_private_t *dev_priv = dev->dev_private;
  1864. u32 size = obj->gtt_space->size;
  1865. int regnum = obj->fence_reg;
  1866. uint64_t val;
  1867. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  1868. 0xfffff000) << 32;
  1869. val |= obj->gtt_offset & 0xfffff000;
  1870. val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  1871. if (obj->tiling_mode == I915_TILING_Y)
  1872. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1873. val |= I965_FENCE_REG_VALID;
  1874. if (pipelined) {
  1875. int ret = intel_ring_begin(pipelined, 6);
  1876. if (ret)
  1877. return ret;
  1878. intel_ring_emit(pipelined, MI_NOOP);
  1879. intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
  1880. intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
  1881. intel_ring_emit(pipelined, (u32)val);
  1882. intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
  1883. intel_ring_emit(pipelined, (u32)(val >> 32));
  1884. intel_ring_advance(pipelined);
  1885. } else
  1886. I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
  1887. return 0;
  1888. }
  1889. static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
  1890. struct intel_ring_buffer *pipelined)
  1891. {
  1892. struct drm_device *dev = obj->base.dev;
  1893. drm_i915_private_t *dev_priv = dev->dev_private;
  1894. u32 size = obj->gtt_space->size;
  1895. u32 fence_reg, val, pitch_val;
  1896. int tile_width;
  1897. if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
  1898. (size & -size) != size ||
  1899. (obj->gtt_offset & (size - 1)),
  1900. "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  1901. obj->gtt_offset, obj->map_and_fenceable, size))
  1902. return -EINVAL;
  1903. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  1904. tile_width = 128;
  1905. else
  1906. tile_width = 512;
  1907. /* Note: pitch better be a power of two tile widths */
  1908. pitch_val = obj->stride / tile_width;
  1909. pitch_val = ffs(pitch_val) - 1;
  1910. val = obj->gtt_offset;
  1911. if (obj->tiling_mode == I915_TILING_Y)
  1912. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1913. val |= I915_FENCE_SIZE_BITS(size);
  1914. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1915. val |= I830_FENCE_REG_VALID;
  1916. fence_reg = obj->fence_reg;
  1917. if (fence_reg < 8)
  1918. fence_reg = FENCE_REG_830_0 + fence_reg * 4;
  1919. else
  1920. fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
  1921. if (pipelined) {
  1922. int ret = intel_ring_begin(pipelined, 4);
  1923. if (ret)
  1924. return ret;
  1925. intel_ring_emit(pipelined, MI_NOOP);
  1926. intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
  1927. intel_ring_emit(pipelined, fence_reg);
  1928. intel_ring_emit(pipelined, val);
  1929. intel_ring_advance(pipelined);
  1930. } else
  1931. I915_WRITE(fence_reg, val);
  1932. return 0;
  1933. }
  1934. static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
  1935. struct intel_ring_buffer *pipelined)
  1936. {
  1937. struct drm_device *dev = obj->base.dev;
  1938. drm_i915_private_t *dev_priv = dev->dev_private;
  1939. u32 size = obj->gtt_space->size;
  1940. int regnum = obj->fence_reg;
  1941. uint32_t val;
  1942. uint32_t pitch_val;
  1943. if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
  1944. (size & -size) != size ||
  1945. (obj->gtt_offset & (size - 1)),
  1946. "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
  1947. obj->gtt_offset, size))
  1948. return -EINVAL;
  1949. pitch_val = obj->stride / 128;
  1950. pitch_val = ffs(pitch_val) - 1;
  1951. val = obj->gtt_offset;
  1952. if (obj->tiling_mode == I915_TILING_Y)
  1953. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1954. val |= I830_FENCE_SIZE_BITS(size);
  1955. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1956. val |= I830_FENCE_REG_VALID;
  1957. if (pipelined) {
  1958. int ret = intel_ring_begin(pipelined, 4);
  1959. if (ret)
  1960. return ret;
  1961. intel_ring_emit(pipelined, MI_NOOP);
  1962. intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
  1963. intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
  1964. intel_ring_emit(pipelined, val);
  1965. intel_ring_advance(pipelined);
  1966. } else
  1967. I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
  1968. return 0;
  1969. }
  1970. static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
  1971. {
  1972. return i915_seqno_passed(ring->get_seqno(ring), seqno);
  1973. }
  1974. static int
  1975. i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
  1976. struct intel_ring_buffer *pipelined,
  1977. bool interruptible)
  1978. {
  1979. int ret;
  1980. if (obj->fenced_gpu_access) {
  1981. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
  1982. i915_gem_flush_ring(obj->base.dev,
  1983. obj->last_fenced_ring,
  1984. 0, obj->base.write_domain);
  1985. obj->fenced_gpu_access = false;
  1986. }
  1987. if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
  1988. if (!ring_passed_seqno(obj->last_fenced_ring,
  1989. obj->last_fenced_seqno)) {
  1990. ret = i915_do_wait_request(obj->base.dev,
  1991. obj->last_fenced_seqno,
  1992. interruptible,
  1993. obj->last_fenced_ring);
  1994. if (ret)
  1995. return ret;
  1996. }
  1997. obj->last_fenced_seqno = 0;
  1998. obj->last_fenced_ring = NULL;
  1999. }
  2000. return 0;
  2001. }
  2002. int
  2003. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2004. {
  2005. int ret;
  2006. if (obj->tiling_mode)
  2007. i915_gem_release_mmap(obj);
  2008. ret = i915_gem_object_flush_fence(obj, NULL, true);
  2009. if (ret)
  2010. return ret;
  2011. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2012. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2013. i915_gem_clear_fence_reg(obj->base.dev,
  2014. &dev_priv->fence_regs[obj->fence_reg]);
  2015. obj->fence_reg = I915_FENCE_REG_NONE;
  2016. }
  2017. return 0;
  2018. }
  2019. static struct drm_i915_fence_reg *
  2020. i915_find_fence_reg(struct drm_device *dev,
  2021. struct intel_ring_buffer *pipelined)
  2022. {
  2023. struct drm_i915_private *dev_priv = dev->dev_private;
  2024. struct drm_i915_fence_reg *reg, *first, *avail;
  2025. int i;
  2026. /* First try to find a free reg */
  2027. avail = NULL;
  2028. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2029. reg = &dev_priv->fence_regs[i];
  2030. if (!reg->obj)
  2031. return reg;
  2032. if (!reg->obj->pin_count)
  2033. avail = reg;
  2034. }
  2035. if (avail == NULL)
  2036. return NULL;
  2037. /* None available, try to steal one or wait for a user to finish */
  2038. avail = first = NULL;
  2039. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2040. if (reg->obj->pin_count)
  2041. continue;
  2042. if (first == NULL)
  2043. first = reg;
  2044. if (!pipelined ||
  2045. !reg->obj->last_fenced_ring ||
  2046. reg->obj->last_fenced_ring == pipelined) {
  2047. avail = reg;
  2048. break;
  2049. }
  2050. }
  2051. if (avail == NULL)
  2052. avail = first;
  2053. return avail;
  2054. }
  2055. /**
  2056. * i915_gem_object_get_fence - set up a fence reg for an object
  2057. * @obj: object to map through a fence reg
  2058. * @pipelined: ring on which to queue the change, or NULL for CPU access
  2059. * @interruptible: must we wait uninterruptibly for the register to retire?
  2060. *
  2061. * When mapping objects through the GTT, userspace wants to be able to write
  2062. * to them without having to worry about swizzling if the object is tiled.
  2063. *
  2064. * This function walks the fence regs looking for a free one for @obj,
  2065. * stealing one if it can't find any.
  2066. *
  2067. * It then sets up the reg based on the object's properties: address, pitch
  2068. * and tiling format.
  2069. */
  2070. int
  2071. i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
  2072. struct intel_ring_buffer *pipelined,
  2073. bool interruptible)
  2074. {
  2075. struct drm_device *dev = obj->base.dev;
  2076. struct drm_i915_private *dev_priv = dev->dev_private;
  2077. struct drm_i915_fence_reg *reg;
  2078. int ret;
  2079. /* XXX disable pipelining. There are bugs. Shocking. */
  2080. pipelined = NULL;
  2081. /* Just update our place in the LRU if our fence is getting reused. */
  2082. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2083. reg = &dev_priv->fence_regs[obj->fence_reg];
  2084. list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  2085. if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
  2086. pipelined = NULL;
  2087. if (!pipelined) {
  2088. if (reg->setup_seqno) {
  2089. if (!ring_passed_seqno(obj->last_fenced_ring,
  2090. reg->setup_seqno)) {
  2091. ret = i915_do_wait_request(obj->base.dev,
  2092. reg->setup_seqno,
  2093. interruptible,
  2094. obj->last_fenced_ring);
  2095. if (ret)
  2096. return ret;
  2097. }
  2098. reg->setup_seqno = 0;
  2099. }
  2100. } else if (obj->last_fenced_ring &&
  2101. obj->last_fenced_ring != pipelined) {
  2102. ret = i915_gem_object_flush_fence(obj,
  2103. pipelined,
  2104. interruptible);
  2105. if (ret)
  2106. return ret;
  2107. } else if (obj->tiling_changed) {
  2108. if (obj->fenced_gpu_access) {
  2109. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
  2110. i915_gem_flush_ring(obj->base.dev, obj->ring,
  2111. 0, obj->base.write_domain);
  2112. obj->fenced_gpu_access = false;
  2113. }
  2114. }
  2115. if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
  2116. pipelined = NULL;
  2117. BUG_ON(!pipelined && reg->setup_seqno);
  2118. if (obj->tiling_changed) {
  2119. if (pipelined) {
  2120. reg->setup_seqno =
  2121. i915_gem_next_request_seqno(dev, pipelined);
  2122. obj->last_fenced_seqno = reg->setup_seqno;
  2123. obj->last_fenced_ring = pipelined;
  2124. }
  2125. goto update;
  2126. }
  2127. return 0;
  2128. }
  2129. reg = i915_find_fence_reg(dev, pipelined);
  2130. if (reg == NULL)
  2131. return -ENOSPC;
  2132. ret = i915_gem_object_flush_fence(obj, pipelined, interruptible);
  2133. if (ret)
  2134. return ret;
  2135. if (reg->obj) {
  2136. struct drm_i915_gem_object *old = reg->obj;
  2137. drm_gem_object_reference(&old->base);
  2138. if (old->tiling_mode)
  2139. i915_gem_release_mmap(old);
  2140. ret = i915_gem_object_flush_fence(old,
  2141. pipelined,
  2142. interruptible);
  2143. if (ret) {
  2144. drm_gem_object_unreference(&old->base);
  2145. return ret;
  2146. }
  2147. if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
  2148. pipelined = NULL;
  2149. old->fence_reg = I915_FENCE_REG_NONE;
  2150. old->last_fenced_ring = pipelined;
  2151. old->last_fenced_seqno =
  2152. pipelined ? i915_gem_next_request_seqno(dev, pipelined) : 0;
  2153. drm_gem_object_unreference(&old->base);
  2154. } else if (obj->last_fenced_seqno == 0)
  2155. pipelined = NULL;
  2156. reg->obj = obj;
  2157. list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  2158. obj->fence_reg = reg - dev_priv->fence_regs;
  2159. obj->last_fenced_ring = pipelined;
  2160. reg->setup_seqno =
  2161. pipelined ? i915_gem_next_request_seqno(dev, pipelined) : 0;
  2162. obj->last_fenced_seqno = reg->setup_seqno;
  2163. update:
  2164. obj->tiling_changed = false;
  2165. switch (INTEL_INFO(dev)->gen) {
  2166. case 6:
  2167. ret = sandybridge_write_fence_reg(obj, pipelined);
  2168. break;
  2169. case 5:
  2170. case 4:
  2171. ret = i965_write_fence_reg(obj, pipelined);
  2172. break;
  2173. case 3:
  2174. ret = i915_write_fence_reg(obj, pipelined);
  2175. break;
  2176. case 2:
  2177. ret = i830_write_fence_reg(obj, pipelined);
  2178. break;
  2179. }
  2180. return ret;
  2181. }
  2182. /**
  2183. * i915_gem_clear_fence_reg - clear out fence register info
  2184. * @obj: object to clear
  2185. *
  2186. * Zeroes out the fence register itself and clears out the associated
  2187. * data structures in dev_priv and obj.
  2188. */
  2189. static void
  2190. i915_gem_clear_fence_reg(struct drm_device *dev,
  2191. struct drm_i915_fence_reg *reg)
  2192. {
  2193. drm_i915_private_t *dev_priv = dev->dev_private;
  2194. uint32_t fence_reg = reg - dev_priv->fence_regs;
  2195. switch (INTEL_INFO(dev)->gen) {
  2196. case 6:
  2197. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
  2198. break;
  2199. case 5:
  2200. case 4:
  2201. I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
  2202. break;
  2203. case 3:
  2204. if (fence_reg >= 8)
  2205. fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
  2206. else
  2207. case 2:
  2208. fence_reg = FENCE_REG_830_0 + fence_reg * 4;
  2209. I915_WRITE(fence_reg, 0);
  2210. break;
  2211. }
  2212. list_del_init(&reg->lru_list);
  2213. reg->obj = NULL;
  2214. reg->setup_seqno = 0;
  2215. }
  2216. /**
  2217. * Finds free space in the GTT aperture and binds the object there.
  2218. */
  2219. static int
  2220. i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  2221. unsigned alignment,
  2222. bool map_and_fenceable)
  2223. {
  2224. struct drm_device *dev = obj->base.dev;
  2225. drm_i915_private_t *dev_priv = dev->dev_private;
  2226. struct drm_mm_node *free_space;
  2227. gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
  2228. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2229. bool mappable, fenceable;
  2230. int ret;
  2231. if (obj->madv != I915_MADV_WILLNEED) {
  2232. DRM_ERROR("Attempting to bind a purgeable object\n");
  2233. return -EINVAL;
  2234. }
  2235. fence_size = i915_gem_get_gtt_size(obj);
  2236. fence_alignment = i915_gem_get_gtt_alignment(obj);
  2237. unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
  2238. if (alignment == 0)
  2239. alignment = map_and_fenceable ? fence_alignment :
  2240. unfenced_alignment;
  2241. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2242. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2243. return -EINVAL;
  2244. }
  2245. size = map_and_fenceable ? fence_size : obj->base.size;
  2246. /* If the object is bigger than the entire aperture, reject it early
  2247. * before evicting everything in a vain attempt to find space.
  2248. */
  2249. if (obj->base.size >
  2250. (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
  2251. DRM_ERROR("Attempting to bind an object larger than the aperture\n");
  2252. return -E2BIG;
  2253. }
  2254. search_free:
  2255. if (map_and_fenceable)
  2256. free_space =
  2257. drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
  2258. size, alignment, 0,
  2259. dev_priv->mm.gtt_mappable_end,
  2260. 0);
  2261. else
  2262. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  2263. size, alignment, 0);
  2264. if (free_space != NULL) {
  2265. if (map_and_fenceable)
  2266. obj->gtt_space =
  2267. drm_mm_get_block_range_generic(free_space,
  2268. size, alignment, 0,
  2269. dev_priv->mm.gtt_mappable_end,
  2270. 0);
  2271. else
  2272. obj->gtt_space =
  2273. drm_mm_get_block(free_space, size, alignment);
  2274. }
  2275. if (obj->gtt_space == NULL) {
  2276. /* If the gtt is empty and we're still having trouble
  2277. * fitting our object in, we're out of memory.
  2278. */
  2279. ret = i915_gem_evict_something(dev, size, alignment,
  2280. map_and_fenceable);
  2281. if (ret)
  2282. return ret;
  2283. goto search_free;
  2284. }
  2285. ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
  2286. if (ret) {
  2287. drm_mm_put_block(obj->gtt_space);
  2288. obj->gtt_space = NULL;
  2289. if (ret == -ENOMEM) {
  2290. /* first try to clear up some space from the GTT */
  2291. ret = i915_gem_evict_something(dev, size,
  2292. alignment,
  2293. map_and_fenceable);
  2294. if (ret) {
  2295. /* now try to shrink everyone else */
  2296. if (gfpmask) {
  2297. gfpmask = 0;
  2298. goto search_free;
  2299. }
  2300. return ret;
  2301. }
  2302. goto search_free;
  2303. }
  2304. return ret;
  2305. }
  2306. ret = i915_gem_gtt_bind_object(obj);
  2307. if (ret) {
  2308. i915_gem_object_put_pages_gtt(obj);
  2309. drm_mm_put_block(obj->gtt_space);
  2310. obj->gtt_space = NULL;
  2311. ret = i915_gem_evict_something(dev, size,
  2312. alignment, map_and_fenceable);
  2313. if (ret)
  2314. return ret;
  2315. goto search_free;
  2316. }
  2317. list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
  2318. list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2319. /* Assert that the object is not currently in any GPU domain. As it
  2320. * wasn't in the GTT, there shouldn't be any way it could have been in
  2321. * a GPU cache
  2322. */
  2323. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  2324. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  2325. obj->gtt_offset = obj->gtt_space->start;
  2326. fenceable =
  2327. obj->gtt_space->size == fence_size &&
  2328. (obj->gtt_space->start & (fence_alignment -1)) == 0;
  2329. mappable =
  2330. obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
  2331. obj->map_and_fenceable = mappable && fenceable;
  2332. trace_i915_gem_object_bind(obj, obj->gtt_offset, map_and_fenceable);
  2333. return 0;
  2334. }
  2335. void
  2336. i915_gem_clflush_object(struct drm_i915_gem_object *obj)
  2337. {
  2338. /* If we don't have a page list set up, then we're not pinned
  2339. * to GPU, and we can ignore the cache flush because it'll happen
  2340. * again at bind time.
  2341. */
  2342. if (obj->pages == NULL)
  2343. return;
  2344. trace_i915_gem_object_clflush(obj);
  2345. drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
  2346. }
  2347. /** Flushes any GPU write domain for the object if it's dirty. */
  2348. static void
  2349. i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
  2350. {
  2351. struct drm_device *dev = obj->base.dev;
  2352. if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
  2353. return;
  2354. /* Queue the GPU write cache flushing we need. */
  2355. i915_gem_flush_ring(dev, obj->ring, 0, obj->base.write_domain);
  2356. BUG_ON(obj->base.write_domain);
  2357. }
  2358. /** Flushes the GTT write domain for the object if it's dirty. */
  2359. static void
  2360. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2361. {
  2362. uint32_t old_write_domain;
  2363. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2364. return;
  2365. /* No actual flushing is required for the GTT write domain. Writes
  2366. * to it immediately go to main memory as far as we know, so there's
  2367. * no chipset flush. It also doesn't land in render cache.
  2368. */
  2369. i915_gem_release_mmap(obj);
  2370. old_write_domain = obj->base.write_domain;
  2371. obj->base.write_domain = 0;
  2372. trace_i915_gem_object_change_domain(obj,
  2373. obj->base.read_domains,
  2374. old_write_domain);
  2375. }
  2376. /** Flushes the CPU write domain for the object if it's dirty. */
  2377. static void
  2378. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2379. {
  2380. uint32_t old_write_domain;
  2381. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2382. return;
  2383. i915_gem_clflush_object(obj);
  2384. intel_gtt_chipset_flush();
  2385. old_write_domain = obj->base.write_domain;
  2386. obj->base.write_domain = 0;
  2387. trace_i915_gem_object_change_domain(obj,
  2388. obj->base.read_domains,
  2389. old_write_domain);
  2390. }
  2391. /**
  2392. * Moves a single object to the GTT read, and possibly write domain.
  2393. *
  2394. * This function returns when the move is complete, including waiting on
  2395. * flushes to occur.
  2396. */
  2397. int
  2398. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2399. {
  2400. uint32_t old_write_domain, old_read_domains;
  2401. int ret;
  2402. /* Not valid to be called on unbound objects. */
  2403. if (obj->gtt_space == NULL)
  2404. return -EINVAL;
  2405. i915_gem_object_flush_gpu_write_domain(obj);
  2406. if (obj->pending_gpu_write || write) {
  2407. ret = i915_gem_object_wait_rendering(obj, true);
  2408. if (ret)
  2409. return ret;
  2410. }
  2411. i915_gem_object_flush_cpu_write_domain(obj);
  2412. old_write_domain = obj->base.write_domain;
  2413. old_read_domains = obj->base.read_domains;
  2414. /* It should now be out of any other write domains, and we can update
  2415. * the domain values for our changes.
  2416. */
  2417. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2418. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2419. if (write) {
  2420. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2421. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2422. obj->dirty = 1;
  2423. }
  2424. trace_i915_gem_object_change_domain(obj,
  2425. old_read_domains,
  2426. old_write_domain);
  2427. return 0;
  2428. }
  2429. /*
  2430. * Prepare buffer for display plane. Use uninterruptible for possible flush
  2431. * wait, as in modesetting process we're not supposed to be interrupted.
  2432. */
  2433. int
  2434. i915_gem_object_set_to_display_plane(struct drm_i915_gem_object *obj,
  2435. struct intel_ring_buffer *pipelined)
  2436. {
  2437. uint32_t old_read_domains;
  2438. int ret;
  2439. /* Not valid to be called on unbound objects. */
  2440. if (obj->gtt_space == NULL)
  2441. return -EINVAL;
  2442. i915_gem_object_flush_gpu_write_domain(obj);
  2443. /* Currently, we are always called from an non-interruptible context. */
  2444. if (pipelined != obj->ring) {
  2445. ret = i915_gem_object_wait_rendering(obj, false);
  2446. if (ret)
  2447. return ret;
  2448. }
  2449. i915_gem_object_flush_cpu_write_domain(obj);
  2450. old_read_domains = obj->base.read_domains;
  2451. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2452. trace_i915_gem_object_change_domain(obj,
  2453. old_read_domains,
  2454. obj->base.write_domain);
  2455. return 0;
  2456. }
  2457. int
  2458. i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
  2459. bool interruptible)
  2460. {
  2461. if (!obj->active)
  2462. return 0;
  2463. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
  2464. i915_gem_flush_ring(obj->base.dev, obj->ring,
  2465. 0, obj->base.write_domain);
  2466. return i915_gem_object_wait_rendering(obj, interruptible);
  2467. }
  2468. /**
  2469. * Moves a single object to the CPU read, and possibly write domain.
  2470. *
  2471. * This function returns when the move is complete, including waiting on
  2472. * flushes to occur.
  2473. */
  2474. static int
  2475. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  2476. {
  2477. uint32_t old_write_domain, old_read_domains;
  2478. int ret;
  2479. i915_gem_object_flush_gpu_write_domain(obj);
  2480. ret = i915_gem_object_wait_rendering(obj, true);
  2481. if (ret)
  2482. return ret;
  2483. i915_gem_object_flush_gtt_write_domain(obj);
  2484. /* If we have a partially-valid cache of the object in the CPU,
  2485. * finish invalidating it and free the per-page flags.
  2486. */
  2487. i915_gem_object_set_to_full_cpu_read_domain(obj);
  2488. old_write_domain = obj->base.write_domain;
  2489. old_read_domains = obj->base.read_domains;
  2490. /* Flush the CPU cache if it's still invalid. */
  2491. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2492. i915_gem_clflush_object(obj);
  2493. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2494. }
  2495. /* It should now be out of any other write domains, and we can update
  2496. * the domain values for our changes.
  2497. */
  2498. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2499. /* If we're writing through the CPU, then the GPU read domains will
  2500. * need to be invalidated at next use.
  2501. */
  2502. if (write) {
  2503. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2504. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2505. }
  2506. trace_i915_gem_object_change_domain(obj,
  2507. old_read_domains,
  2508. old_write_domain);
  2509. return 0;
  2510. }
  2511. /**
  2512. * Moves the object from a partially CPU read to a full one.
  2513. *
  2514. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  2515. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  2516. */
  2517. static void
  2518. i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
  2519. {
  2520. if (!obj->page_cpu_valid)
  2521. return;
  2522. /* If we're partially in the CPU read domain, finish moving it in.
  2523. */
  2524. if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
  2525. int i;
  2526. for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
  2527. if (obj->page_cpu_valid[i])
  2528. continue;
  2529. drm_clflush_pages(obj->pages + i, 1);
  2530. }
  2531. }
  2532. /* Free the page_cpu_valid mappings which are now stale, whether
  2533. * or not we've got I915_GEM_DOMAIN_CPU.
  2534. */
  2535. kfree(obj->page_cpu_valid);
  2536. obj->page_cpu_valid = NULL;
  2537. }
  2538. /**
  2539. * Set the CPU read domain on a range of the object.
  2540. *
  2541. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  2542. * not entirely valid. The page_cpu_valid member of the object flags which
  2543. * pages have been flushed, and will be respected by
  2544. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  2545. * of the whole object.
  2546. *
  2547. * This function returns when the move is complete, including waiting on
  2548. * flushes to occur.
  2549. */
  2550. static int
  2551. i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
  2552. uint64_t offset, uint64_t size)
  2553. {
  2554. uint32_t old_read_domains;
  2555. int i, ret;
  2556. if (offset == 0 && size == obj->base.size)
  2557. return i915_gem_object_set_to_cpu_domain(obj, 0);
  2558. i915_gem_object_flush_gpu_write_domain(obj);
  2559. ret = i915_gem_object_wait_rendering(obj, true);
  2560. if (ret)
  2561. return ret;
  2562. i915_gem_object_flush_gtt_write_domain(obj);
  2563. /* If we're already fully in the CPU read domain, we're done. */
  2564. if (obj->page_cpu_valid == NULL &&
  2565. (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
  2566. return 0;
  2567. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  2568. * newly adding I915_GEM_DOMAIN_CPU
  2569. */
  2570. if (obj->page_cpu_valid == NULL) {
  2571. obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
  2572. GFP_KERNEL);
  2573. if (obj->page_cpu_valid == NULL)
  2574. return -ENOMEM;
  2575. } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  2576. memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
  2577. /* Flush the cache on any pages that are still invalid from the CPU's
  2578. * perspective.
  2579. */
  2580. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  2581. i++) {
  2582. if (obj->page_cpu_valid[i])
  2583. continue;
  2584. drm_clflush_pages(obj->pages + i, 1);
  2585. obj->page_cpu_valid[i] = 1;
  2586. }
  2587. /* It should now be out of any other write domains, and we can update
  2588. * the domain values for our changes.
  2589. */
  2590. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2591. old_read_domains = obj->base.read_domains;
  2592. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2593. trace_i915_gem_object_change_domain(obj,
  2594. old_read_domains,
  2595. obj->base.write_domain);
  2596. return 0;
  2597. }
  2598. /* Throttle our rendering by waiting until the ring has completed our requests
  2599. * emitted over 20 msec ago.
  2600. *
  2601. * Note that if we were to use the current jiffies each time around the loop,
  2602. * we wouldn't escape the function with any frames outstanding if the time to
  2603. * render a frame was over 20ms.
  2604. *
  2605. * This should get us reasonable parallelism between CPU and GPU but also
  2606. * relatively low latency when blocking on a particular request to finish.
  2607. */
  2608. static int
  2609. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  2610. {
  2611. struct drm_i915_private *dev_priv = dev->dev_private;
  2612. struct drm_i915_file_private *file_priv = file->driver_priv;
  2613. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2614. struct drm_i915_gem_request *request;
  2615. struct intel_ring_buffer *ring = NULL;
  2616. u32 seqno = 0;
  2617. int ret;
  2618. spin_lock(&file_priv->mm.lock);
  2619. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  2620. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2621. break;
  2622. ring = request->ring;
  2623. seqno = request->seqno;
  2624. }
  2625. spin_unlock(&file_priv->mm.lock);
  2626. if (seqno == 0)
  2627. return 0;
  2628. ret = 0;
  2629. if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
  2630. /* And wait for the seqno passing without holding any locks and
  2631. * causing extra latency for others. This is safe as the irq
  2632. * generation is designed to be run atomically and so is
  2633. * lockless.
  2634. */
  2635. if (ring->irq_get(ring)) {
  2636. ret = wait_event_interruptible(ring->irq_queue,
  2637. i915_seqno_passed(ring->get_seqno(ring), seqno)
  2638. || atomic_read(&dev_priv->mm.wedged));
  2639. ring->irq_put(ring);
  2640. if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
  2641. ret = -EIO;
  2642. }
  2643. }
  2644. if (ret == 0)
  2645. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  2646. return ret;
  2647. }
  2648. int
  2649. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  2650. uint32_t alignment,
  2651. bool map_and_fenceable)
  2652. {
  2653. struct drm_device *dev = obj->base.dev;
  2654. struct drm_i915_private *dev_priv = dev->dev_private;
  2655. int ret;
  2656. BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
  2657. WARN_ON(i915_verify_lists(dev));
  2658. if (obj->gtt_space != NULL) {
  2659. if ((alignment && obj->gtt_offset & (alignment - 1)) ||
  2660. (map_and_fenceable && !obj->map_and_fenceable)) {
  2661. WARN(obj->pin_count,
  2662. "bo is already pinned with incorrect alignment:"
  2663. " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
  2664. " obj->map_and_fenceable=%d\n",
  2665. obj->gtt_offset, alignment,
  2666. map_and_fenceable,
  2667. obj->map_and_fenceable);
  2668. ret = i915_gem_object_unbind(obj);
  2669. if (ret)
  2670. return ret;
  2671. }
  2672. }
  2673. if (obj->gtt_space == NULL) {
  2674. ret = i915_gem_object_bind_to_gtt(obj, alignment,
  2675. map_and_fenceable);
  2676. if (ret)
  2677. return ret;
  2678. }
  2679. if (obj->pin_count++ == 0) {
  2680. if (!obj->active)
  2681. list_move_tail(&obj->mm_list,
  2682. &dev_priv->mm.pinned_list);
  2683. }
  2684. obj->pin_mappable |= map_and_fenceable;
  2685. WARN_ON(i915_verify_lists(dev));
  2686. return 0;
  2687. }
  2688. void
  2689. i915_gem_object_unpin(struct drm_i915_gem_object *obj)
  2690. {
  2691. struct drm_device *dev = obj->base.dev;
  2692. drm_i915_private_t *dev_priv = dev->dev_private;
  2693. WARN_ON(i915_verify_lists(dev));
  2694. BUG_ON(obj->pin_count == 0);
  2695. BUG_ON(obj->gtt_space == NULL);
  2696. if (--obj->pin_count == 0) {
  2697. if (!obj->active)
  2698. list_move_tail(&obj->mm_list,
  2699. &dev_priv->mm.inactive_list);
  2700. obj->pin_mappable = false;
  2701. }
  2702. WARN_ON(i915_verify_lists(dev));
  2703. }
  2704. int
  2705. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  2706. struct drm_file *file)
  2707. {
  2708. struct drm_i915_gem_pin *args = data;
  2709. struct drm_i915_gem_object *obj;
  2710. int ret;
  2711. ret = i915_mutex_lock_interruptible(dev);
  2712. if (ret)
  2713. return ret;
  2714. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2715. if (obj == NULL) {
  2716. ret = -ENOENT;
  2717. goto unlock;
  2718. }
  2719. if (obj->madv != I915_MADV_WILLNEED) {
  2720. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  2721. ret = -EINVAL;
  2722. goto out;
  2723. }
  2724. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  2725. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  2726. args->handle);
  2727. ret = -EINVAL;
  2728. goto out;
  2729. }
  2730. obj->user_pin_count++;
  2731. obj->pin_filp = file;
  2732. if (obj->user_pin_count == 1) {
  2733. ret = i915_gem_object_pin(obj, args->alignment, true);
  2734. if (ret)
  2735. goto out;
  2736. }
  2737. /* XXX - flush the CPU caches for pinned objects
  2738. * as the X server doesn't manage domains yet
  2739. */
  2740. i915_gem_object_flush_cpu_write_domain(obj);
  2741. args->offset = obj->gtt_offset;
  2742. out:
  2743. drm_gem_object_unreference(&obj->base);
  2744. unlock:
  2745. mutex_unlock(&dev->struct_mutex);
  2746. return ret;
  2747. }
  2748. int
  2749. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  2750. struct drm_file *file)
  2751. {
  2752. struct drm_i915_gem_pin *args = data;
  2753. struct drm_i915_gem_object *obj;
  2754. int ret;
  2755. ret = i915_mutex_lock_interruptible(dev);
  2756. if (ret)
  2757. return ret;
  2758. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2759. if (obj == NULL) {
  2760. ret = -ENOENT;
  2761. goto unlock;
  2762. }
  2763. if (obj->pin_filp != file) {
  2764. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  2765. args->handle);
  2766. ret = -EINVAL;
  2767. goto out;
  2768. }
  2769. obj->user_pin_count--;
  2770. if (obj->user_pin_count == 0) {
  2771. obj->pin_filp = NULL;
  2772. i915_gem_object_unpin(obj);
  2773. }
  2774. out:
  2775. drm_gem_object_unreference(&obj->base);
  2776. unlock:
  2777. mutex_unlock(&dev->struct_mutex);
  2778. return ret;
  2779. }
  2780. int
  2781. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  2782. struct drm_file *file)
  2783. {
  2784. struct drm_i915_gem_busy *args = data;
  2785. struct drm_i915_gem_object *obj;
  2786. int ret;
  2787. ret = i915_mutex_lock_interruptible(dev);
  2788. if (ret)
  2789. return ret;
  2790. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2791. if (obj == NULL) {
  2792. ret = -ENOENT;
  2793. goto unlock;
  2794. }
  2795. /* Count all active objects as busy, even if they are currently not used
  2796. * by the gpu. Users of this interface expect objects to eventually
  2797. * become non-busy without any further actions, therefore emit any
  2798. * necessary flushes here.
  2799. */
  2800. args->busy = obj->active;
  2801. if (args->busy) {
  2802. /* Unconditionally flush objects, even when the gpu still uses this
  2803. * object. Userspace calling this function indicates that it wants to
  2804. * use this buffer rather sooner than later, so issuing the required
  2805. * flush earlier is beneficial.
  2806. */
  2807. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
  2808. i915_gem_flush_ring(dev, obj->ring,
  2809. 0, obj->base.write_domain);
  2810. } else if (obj->ring->outstanding_lazy_request ==
  2811. obj->last_rendering_seqno) {
  2812. struct drm_i915_gem_request *request;
  2813. /* This ring is not being cleared by active usage,
  2814. * so emit a request to do so.
  2815. */
  2816. request = kzalloc(sizeof(*request), GFP_KERNEL);
  2817. if (request)
  2818. ret = i915_add_request(dev,
  2819. NULL, request,
  2820. obj->ring);
  2821. else
  2822. ret = -ENOMEM;
  2823. }
  2824. /* Update the active list for the hardware's current position.
  2825. * Otherwise this only updates on a delayed timer or when irqs
  2826. * are actually unmasked, and our working set ends up being
  2827. * larger than required.
  2828. */
  2829. i915_gem_retire_requests_ring(dev, obj->ring);
  2830. args->busy = obj->active;
  2831. }
  2832. drm_gem_object_unreference(&obj->base);
  2833. unlock:
  2834. mutex_unlock(&dev->struct_mutex);
  2835. return ret;
  2836. }
  2837. int
  2838. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  2839. struct drm_file *file_priv)
  2840. {
  2841. return i915_gem_ring_throttle(dev, file_priv);
  2842. }
  2843. int
  2844. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  2845. struct drm_file *file_priv)
  2846. {
  2847. struct drm_i915_gem_madvise *args = data;
  2848. struct drm_i915_gem_object *obj;
  2849. int ret;
  2850. switch (args->madv) {
  2851. case I915_MADV_DONTNEED:
  2852. case I915_MADV_WILLNEED:
  2853. break;
  2854. default:
  2855. return -EINVAL;
  2856. }
  2857. ret = i915_mutex_lock_interruptible(dev);
  2858. if (ret)
  2859. return ret;
  2860. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  2861. if (obj == NULL) {
  2862. ret = -ENOENT;
  2863. goto unlock;
  2864. }
  2865. if (obj->pin_count) {
  2866. ret = -EINVAL;
  2867. goto out;
  2868. }
  2869. if (obj->madv != __I915_MADV_PURGED)
  2870. obj->madv = args->madv;
  2871. /* if the object is no longer bound, discard its backing storage */
  2872. if (i915_gem_object_is_purgeable(obj) &&
  2873. obj->gtt_space == NULL)
  2874. i915_gem_object_truncate(obj);
  2875. args->retained = obj->madv != __I915_MADV_PURGED;
  2876. out:
  2877. drm_gem_object_unreference(&obj->base);
  2878. unlock:
  2879. mutex_unlock(&dev->struct_mutex);
  2880. return ret;
  2881. }
  2882. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  2883. size_t size)
  2884. {
  2885. struct drm_i915_private *dev_priv = dev->dev_private;
  2886. struct drm_i915_gem_object *obj;
  2887. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  2888. if (obj == NULL)
  2889. return NULL;
  2890. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  2891. kfree(obj);
  2892. return NULL;
  2893. }
  2894. i915_gem_info_add_obj(dev_priv, size);
  2895. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2896. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2897. obj->agp_type = AGP_USER_MEMORY;
  2898. obj->base.driver_private = NULL;
  2899. obj->fence_reg = I915_FENCE_REG_NONE;
  2900. INIT_LIST_HEAD(&obj->mm_list);
  2901. INIT_LIST_HEAD(&obj->gtt_list);
  2902. INIT_LIST_HEAD(&obj->ring_list);
  2903. INIT_LIST_HEAD(&obj->exec_list);
  2904. INIT_LIST_HEAD(&obj->gpu_write_list);
  2905. obj->madv = I915_MADV_WILLNEED;
  2906. /* Avoid an unnecessary call to unbind on the first bind. */
  2907. obj->map_and_fenceable = true;
  2908. return obj;
  2909. }
  2910. int i915_gem_init_object(struct drm_gem_object *obj)
  2911. {
  2912. BUG();
  2913. return 0;
  2914. }
  2915. static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
  2916. {
  2917. struct drm_device *dev = obj->base.dev;
  2918. drm_i915_private_t *dev_priv = dev->dev_private;
  2919. int ret;
  2920. ret = i915_gem_object_unbind(obj);
  2921. if (ret == -ERESTARTSYS) {
  2922. list_move(&obj->mm_list,
  2923. &dev_priv->mm.deferred_free_list);
  2924. return;
  2925. }
  2926. if (obj->base.map_list.map)
  2927. i915_gem_free_mmap_offset(obj);
  2928. drm_gem_object_release(&obj->base);
  2929. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  2930. kfree(obj->page_cpu_valid);
  2931. kfree(obj->bit_17);
  2932. kfree(obj);
  2933. }
  2934. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  2935. {
  2936. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  2937. struct drm_device *dev = obj->base.dev;
  2938. trace_i915_gem_object_destroy(obj);
  2939. while (obj->pin_count > 0)
  2940. i915_gem_object_unpin(obj);
  2941. if (obj->phys_obj)
  2942. i915_gem_detach_phys_object(dev, obj);
  2943. i915_gem_free_object_tail(obj);
  2944. }
  2945. int
  2946. i915_gem_idle(struct drm_device *dev)
  2947. {
  2948. drm_i915_private_t *dev_priv = dev->dev_private;
  2949. int ret;
  2950. mutex_lock(&dev->struct_mutex);
  2951. if (dev_priv->mm.suspended) {
  2952. mutex_unlock(&dev->struct_mutex);
  2953. return 0;
  2954. }
  2955. ret = i915_gpu_idle(dev);
  2956. if (ret) {
  2957. mutex_unlock(&dev->struct_mutex);
  2958. return ret;
  2959. }
  2960. /* Under UMS, be paranoid and evict. */
  2961. if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
  2962. ret = i915_gem_evict_inactive(dev, false);
  2963. if (ret) {
  2964. mutex_unlock(&dev->struct_mutex);
  2965. return ret;
  2966. }
  2967. }
  2968. i915_gem_reset_fences(dev);
  2969. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  2970. * We need to replace this with a semaphore, or something.
  2971. * And not confound mm.suspended!
  2972. */
  2973. dev_priv->mm.suspended = 1;
  2974. del_timer_sync(&dev_priv->hangcheck_timer);
  2975. i915_kernel_lost_context(dev);
  2976. i915_gem_cleanup_ringbuffer(dev);
  2977. mutex_unlock(&dev->struct_mutex);
  2978. /* Cancel the retire work handler, which should be idle now. */
  2979. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  2980. return 0;
  2981. }
  2982. int
  2983. i915_gem_init_ringbuffer(struct drm_device *dev)
  2984. {
  2985. drm_i915_private_t *dev_priv = dev->dev_private;
  2986. int ret;
  2987. ret = intel_init_render_ring_buffer(dev);
  2988. if (ret)
  2989. return ret;
  2990. if (HAS_BSD(dev)) {
  2991. ret = intel_init_bsd_ring_buffer(dev);
  2992. if (ret)
  2993. goto cleanup_render_ring;
  2994. }
  2995. if (HAS_BLT(dev)) {
  2996. ret = intel_init_blt_ring_buffer(dev);
  2997. if (ret)
  2998. goto cleanup_bsd_ring;
  2999. }
  3000. dev_priv->next_seqno = 1;
  3001. return 0;
  3002. cleanup_bsd_ring:
  3003. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3004. cleanup_render_ring:
  3005. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3006. return ret;
  3007. }
  3008. void
  3009. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3010. {
  3011. drm_i915_private_t *dev_priv = dev->dev_private;
  3012. int i;
  3013. for (i = 0; i < I915_NUM_RINGS; i++)
  3014. intel_cleanup_ring_buffer(&dev_priv->ring[i]);
  3015. }
  3016. int
  3017. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3018. struct drm_file *file_priv)
  3019. {
  3020. drm_i915_private_t *dev_priv = dev->dev_private;
  3021. int ret, i;
  3022. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3023. return 0;
  3024. if (atomic_read(&dev_priv->mm.wedged)) {
  3025. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3026. atomic_set(&dev_priv->mm.wedged, 0);
  3027. }
  3028. mutex_lock(&dev->struct_mutex);
  3029. dev_priv->mm.suspended = 0;
  3030. ret = i915_gem_init_ringbuffer(dev);
  3031. if (ret != 0) {
  3032. mutex_unlock(&dev->struct_mutex);
  3033. return ret;
  3034. }
  3035. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  3036. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  3037. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  3038. for (i = 0; i < I915_NUM_RINGS; i++) {
  3039. BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
  3040. BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
  3041. }
  3042. mutex_unlock(&dev->struct_mutex);
  3043. ret = drm_irq_install(dev);
  3044. if (ret)
  3045. goto cleanup_ringbuffer;
  3046. return 0;
  3047. cleanup_ringbuffer:
  3048. mutex_lock(&dev->struct_mutex);
  3049. i915_gem_cleanup_ringbuffer(dev);
  3050. dev_priv->mm.suspended = 1;
  3051. mutex_unlock(&dev->struct_mutex);
  3052. return ret;
  3053. }
  3054. int
  3055. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3056. struct drm_file *file_priv)
  3057. {
  3058. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3059. return 0;
  3060. drm_irq_uninstall(dev);
  3061. return i915_gem_idle(dev);
  3062. }
  3063. void
  3064. i915_gem_lastclose(struct drm_device *dev)
  3065. {
  3066. int ret;
  3067. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3068. return;
  3069. ret = i915_gem_idle(dev);
  3070. if (ret)
  3071. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3072. }
  3073. static void
  3074. init_ring_lists(struct intel_ring_buffer *ring)
  3075. {
  3076. INIT_LIST_HEAD(&ring->active_list);
  3077. INIT_LIST_HEAD(&ring->request_list);
  3078. INIT_LIST_HEAD(&ring->gpu_write_list);
  3079. }
  3080. void
  3081. i915_gem_load(struct drm_device *dev)
  3082. {
  3083. int i;
  3084. drm_i915_private_t *dev_priv = dev->dev_private;
  3085. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  3086. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  3087. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  3088. INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
  3089. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3090. INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
  3091. INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
  3092. for (i = 0; i < I915_NUM_RINGS; i++)
  3093. init_ring_lists(&dev_priv->ring[i]);
  3094. for (i = 0; i < 16; i++)
  3095. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3096. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3097. i915_gem_retire_work_handler);
  3098. init_completion(&dev_priv->error_completion);
  3099. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3100. if (IS_GEN3(dev)) {
  3101. u32 tmp = I915_READ(MI_ARB_STATE);
  3102. if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
  3103. /* arb state is a masked write, so set bit + bit in mask */
  3104. tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
  3105. I915_WRITE(MI_ARB_STATE, tmp);
  3106. }
  3107. }
  3108. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3109. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3110. dev_priv->fence_reg_start = 3;
  3111. if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3112. dev_priv->num_fence_regs = 16;
  3113. else
  3114. dev_priv->num_fence_regs = 8;
  3115. /* Initialize fence registers to zero */
  3116. switch (INTEL_INFO(dev)->gen) {
  3117. case 6:
  3118. for (i = 0; i < 16; i++)
  3119. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
  3120. break;
  3121. case 5:
  3122. case 4:
  3123. for (i = 0; i < 16; i++)
  3124. I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
  3125. break;
  3126. case 3:
  3127. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3128. for (i = 0; i < 8; i++)
  3129. I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
  3130. case 2:
  3131. for (i = 0; i < 8; i++)
  3132. I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
  3133. break;
  3134. }
  3135. i915_gem_detect_bit_6_swizzle(dev);
  3136. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3137. dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
  3138. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  3139. register_shrinker(&dev_priv->mm.inactive_shrinker);
  3140. }
  3141. /*
  3142. * Create a physically contiguous memory object for this object
  3143. * e.g. for cursor + overlay regs
  3144. */
  3145. static int i915_gem_init_phys_object(struct drm_device *dev,
  3146. int id, int size, int align)
  3147. {
  3148. drm_i915_private_t *dev_priv = dev->dev_private;
  3149. struct drm_i915_gem_phys_object *phys_obj;
  3150. int ret;
  3151. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3152. return 0;
  3153. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3154. if (!phys_obj)
  3155. return -ENOMEM;
  3156. phys_obj->id = id;
  3157. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3158. if (!phys_obj->handle) {
  3159. ret = -ENOMEM;
  3160. goto kfree_obj;
  3161. }
  3162. #ifdef CONFIG_X86
  3163. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3164. #endif
  3165. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3166. return 0;
  3167. kfree_obj:
  3168. kfree(phys_obj);
  3169. return ret;
  3170. }
  3171. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3172. {
  3173. drm_i915_private_t *dev_priv = dev->dev_private;
  3174. struct drm_i915_gem_phys_object *phys_obj;
  3175. if (!dev_priv->mm.phys_objs[id - 1])
  3176. return;
  3177. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3178. if (phys_obj->cur_obj) {
  3179. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3180. }
  3181. #ifdef CONFIG_X86
  3182. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3183. #endif
  3184. drm_pci_free(dev, phys_obj->handle);
  3185. kfree(phys_obj);
  3186. dev_priv->mm.phys_objs[id - 1] = NULL;
  3187. }
  3188. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3189. {
  3190. int i;
  3191. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3192. i915_gem_free_phys_object(dev, i);
  3193. }
  3194. void i915_gem_detach_phys_object(struct drm_device *dev,
  3195. struct drm_i915_gem_object *obj)
  3196. {
  3197. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3198. char *vaddr;
  3199. int i;
  3200. int page_count;
  3201. if (!obj->phys_obj)
  3202. return;
  3203. vaddr = obj->phys_obj->handle->vaddr;
  3204. page_count = obj->base.size / PAGE_SIZE;
  3205. for (i = 0; i < page_count; i++) {
  3206. struct page *page = read_cache_page_gfp(mapping, i,
  3207. GFP_HIGHUSER | __GFP_RECLAIMABLE);
  3208. if (!IS_ERR(page)) {
  3209. char *dst = kmap_atomic(page);
  3210. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  3211. kunmap_atomic(dst);
  3212. drm_clflush_pages(&page, 1);
  3213. set_page_dirty(page);
  3214. mark_page_accessed(page);
  3215. page_cache_release(page);
  3216. }
  3217. }
  3218. intel_gtt_chipset_flush();
  3219. obj->phys_obj->cur_obj = NULL;
  3220. obj->phys_obj = NULL;
  3221. }
  3222. int
  3223. i915_gem_attach_phys_object(struct drm_device *dev,
  3224. struct drm_i915_gem_object *obj,
  3225. int id,
  3226. int align)
  3227. {
  3228. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3229. drm_i915_private_t *dev_priv = dev->dev_private;
  3230. int ret = 0;
  3231. int page_count;
  3232. int i;
  3233. if (id > I915_MAX_PHYS_OBJECT)
  3234. return -EINVAL;
  3235. if (obj->phys_obj) {
  3236. if (obj->phys_obj->id == id)
  3237. return 0;
  3238. i915_gem_detach_phys_object(dev, obj);
  3239. }
  3240. /* create a new object */
  3241. if (!dev_priv->mm.phys_objs[id - 1]) {
  3242. ret = i915_gem_init_phys_object(dev, id,
  3243. obj->base.size, align);
  3244. if (ret) {
  3245. DRM_ERROR("failed to init phys object %d size: %zu\n",
  3246. id, obj->base.size);
  3247. return ret;
  3248. }
  3249. }
  3250. /* bind to the object */
  3251. obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3252. obj->phys_obj->cur_obj = obj;
  3253. page_count = obj->base.size / PAGE_SIZE;
  3254. for (i = 0; i < page_count; i++) {
  3255. struct page *page;
  3256. char *dst, *src;
  3257. page = read_cache_page_gfp(mapping, i,
  3258. GFP_HIGHUSER | __GFP_RECLAIMABLE);
  3259. if (IS_ERR(page))
  3260. return PTR_ERR(page);
  3261. src = kmap_atomic(page);
  3262. dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3263. memcpy(dst, src, PAGE_SIZE);
  3264. kunmap_atomic(src);
  3265. mark_page_accessed(page);
  3266. page_cache_release(page);
  3267. }
  3268. return 0;
  3269. }
  3270. static int
  3271. i915_gem_phys_pwrite(struct drm_device *dev,
  3272. struct drm_i915_gem_object *obj,
  3273. struct drm_i915_gem_pwrite *args,
  3274. struct drm_file *file_priv)
  3275. {
  3276. void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
  3277. char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
  3278. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  3279. unsigned long unwritten;
  3280. /* The physical object once assigned is fixed for the lifetime
  3281. * of the obj, so we can safely drop the lock and continue
  3282. * to access vaddr.
  3283. */
  3284. mutex_unlock(&dev->struct_mutex);
  3285. unwritten = copy_from_user(vaddr, user_data, args->size);
  3286. mutex_lock(&dev->struct_mutex);
  3287. if (unwritten)
  3288. return -EFAULT;
  3289. }
  3290. intel_gtt_chipset_flush();
  3291. return 0;
  3292. }
  3293. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3294. {
  3295. struct drm_i915_file_private *file_priv = file->driver_priv;
  3296. /* Clean up our request list when the client is going away, so that
  3297. * later retire_requests won't dereference our soon-to-be-gone
  3298. * file_priv.
  3299. */
  3300. spin_lock(&file_priv->mm.lock);
  3301. while (!list_empty(&file_priv->mm.request_list)) {
  3302. struct drm_i915_gem_request *request;
  3303. request = list_first_entry(&file_priv->mm.request_list,
  3304. struct drm_i915_gem_request,
  3305. client_list);
  3306. list_del(&request->client_list);
  3307. request->file_priv = NULL;
  3308. }
  3309. spin_unlock(&file_priv->mm.lock);
  3310. }
  3311. static int
  3312. i915_gpu_is_active(struct drm_device *dev)
  3313. {
  3314. drm_i915_private_t *dev_priv = dev->dev_private;
  3315. int lists_empty;
  3316. lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
  3317. list_empty(&dev_priv->mm.active_list);
  3318. return !lists_empty;
  3319. }
  3320. static int
  3321. i915_gem_inactive_shrink(struct shrinker *shrinker,
  3322. int nr_to_scan,
  3323. gfp_t gfp_mask)
  3324. {
  3325. struct drm_i915_private *dev_priv =
  3326. container_of(shrinker,
  3327. struct drm_i915_private,
  3328. mm.inactive_shrinker);
  3329. struct drm_device *dev = dev_priv->dev;
  3330. struct drm_i915_gem_object *obj, *next;
  3331. int cnt;
  3332. if (!mutex_trylock(&dev->struct_mutex))
  3333. return 0;
  3334. /* "fast-path" to count number of available objects */
  3335. if (nr_to_scan == 0) {
  3336. cnt = 0;
  3337. list_for_each_entry(obj,
  3338. &dev_priv->mm.inactive_list,
  3339. mm_list)
  3340. cnt++;
  3341. mutex_unlock(&dev->struct_mutex);
  3342. return cnt / 100 * sysctl_vfs_cache_pressure;
  3343. }
  3344. rescan:
  3345. /* first scan for clean buffers */
  3346. i915_gem_retire_requests(dev);
  3347. list_for_each_entry_safe(obj, next,
  3348. &dev_priv->mm.inactive_list,
  3349. mm_list) {
  3350. if (i915_gem_object_is_purgeable(obj)) {
  3351. if (i915_gem_object_unbind(obj) == 0 &&
  3352. --nr_to_scan == 0)
  3353. break;
  3354. }
  3355. }
  3356. /* second pass, evict/count anything still on the inactive list */
  3357. cnt = 0;
  3358. list_for_each_entry_safe(obj, next,
  3359. &dev_priv->mm.inactive_list,
  3360. mm_list) {
  3361. if (nr_to_scan &&
  3362. i915_gem_object_unbind(obj) == 0)
  3363. nr_to_scan--;
  3364. else
  3365. cnt++;
  3366. }
  3367. if (nr_to_scan && i915_gpu_is_active(dev)) {
  3368. /*
  3369. * We are desperate for pages, so as a last resort, wait
  3370. * for the GPU to finish and discard whatever we can.
  3371. * This has a dramatic impact to reduce the number of
  3372. * OOM-killer events whilst running the GPU aggressively.
  3373. */
  3374. if (i915_gpu_idle(dev) == 0)
  3375. goto rescan;
  3376. }
  3377. mutex_unlock(&dev->struct_mutex);
  3378. return cnt / 100 * sysctl_vfs_cache_pressure;
  3379. }