i915_gem.c 112 KB

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