i915_gem.c 101 KB

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