i915_gem.c 101 KB

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