i915_gem.c 101 KB

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