i915_gem.c 101 KB

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