i915_gem.c 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474
  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->emitted_jiffies = jiffies;
  1707. was_empty = list_empty(&ring->request_list);
  1708. list_add_tail(&request->list, &ring->request_list);
  1709. request->file_priv = NULL;
  1710. if (file) {
  1711. struct drm_i915_file_private *file_priv = file->driver_priv;
  1712. spin_lock(&file_priv->mm.lock);
  1713. request->file_priv = file_priv;
  1714. list_add_tail(&request->client_list,
  1715. &file_priv->mm.request_list);
  1716. spin_unlock(&file_priv->mm.lock);
  1717. }
  1718. trace_i915_gem_request_add(ring, request->seqno);
  1719. ring->outstanding_lazy_request = 0;
  1720. if (!dev_priv->mm.suspended) {
  1721. if (i915_enable_hangcheck) {
  1722. mod_timer(&dev_priv->gpu_error.hangcheck_timer,
  1723. round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
  1724. }
  1725. if (was_empty) {
  1726. queue_delayed_work(dev_priv->wq,
  1727. &dev_priv->mm.retire_work,
  1728. round_jiffies_up_relative(HZ));
  1729. intel_mark_busy(dev_priv->dev);
  1730. }
  1731. }
  1732. if (out_seqno)
  1733. *out_seqno = request->seqno;
  1734. return 0;
  1735. }
  1736. static inline void
  1737. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1738. {
  1739. struct drm_i915_file_private *file_priv = request->file_priv;
  1740. if (!file_priv)
  1741. return;
  1742. spin_lock(&file_priv->mm.lock);
  1743. if (request->file_priv) {
  1744. list_del(&request->client_list);
  1745. request->file_priv = NULL;
  1746. }
  1747. spin_unlock(&file_priv->mm.lock);
  1748. }
  1749. static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
  1750. struct intel_ring_buffer *ring)
  1751. {
  1752. while (!list_empty(&ring->request_list)) {
  1753. struct drm_i915_gem_request *request;
  1754. request = list_first_entry(&ring->request_list,
  1755. struct drm_i915_gem_request,
  1756. list);
  1757. list_del(&request->list);
  1758. i915_gem_request_remove_from_client(request);
  1759. kfree(request);
  1760. }
  1761. while (!list_empty(&ring->active_list)) {
  1762. struct drm_i915_gem_object *obj;
  1763. obj = list_first_entry(&ring->active_list,
  1764. struct drm_i915_gem_object,
  1765. ring_list);
  1766. i915_gem_object_move_to_inactive(obj);
  1767. }
  1768. }
  1769. static void i915_gem_reset_fences(struct drm_device *dev)
  1770. {
  1771. struct drm_i915_private *dev_priv = dev->dev_private;
  1772. int i;
  1773. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1774. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1775. if (reg->obj)
  1776. i915_gem_object_fence_lost(reg->obj);
  1777. i915_gem_write_fence(dev, i, NULL);
  1778. reg->pin_count = 0;
  1779. reg->obj = NULL;
  1780. INIT_LIST_HEAD(&reg->lru_list);
  1781. }
  1782. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  1783. }
  1784. void i915_gem_reset(struct drm_device *dev)
  1785. {
  1786. struct drm_i915_private *dev_priv = dev->dev_private;
  1787. struct drm_i915_gem_object *obj;
  1788. struct intel_ring_buffer *ring;
  1789. int i;
  1790. for_each_ring(ring, dev_priv, i)
  1791. i915_gem_reset_ring_lists(dev_priv, ring);
  1792. /* Move everything out of the GPU domains to ensure we do any
  1793. * necessary invalidation upon reuse.
  1794. */
  1795. list_for_each_entry(obj,
  1796. &dev_priv->mm.inactive_list,
  1797. mm_list)
  1798. {
  1799. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  1800. }
  1801. /* The fence registers are invalidated so clear them out */
  1802. i915_gem_reset_fences(dev);
  1803. }
  1804. /**
  1805. * This function clears the request list as sequence numbers are passed.
  1806. */
  1807. void
  1808. i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
  1809. {
  1810. uint32_t seqno;
  1811. if (list_empty(&ring->request_list))
  1812. return;
  1813. WARN_ON(i915_verify_lists(ring->dev));
  1814. seqno = ring->get_seqno(ring, true);
  1815. while (!list_empty(&ring->request_list)) {
  1816. struct drm_i915_gem_request *request;
  1817. request = list_first_entry(&ring->request_list,
  1818. struct drm_i915_gem_request,
  1819. list);
  1820. if (!i915_seqno_passed(seqno, request->seqno))
  1821. break;
  1822. trace_i915_gem_request_retire(ring, request->seqno);
  1823. /* We know the GPU must have read the request to have
  1824. * sent us the seqno + interrupt, so use the position
  1825. * of tail of the request to update the last known position
  1826. * of the GPU head.
  1827. */
  1828. ring->last_retired_head = request->tail;
  1829. list_del(&request->list);
  1830. i915_gem_request_remove_from_client(request);
  1831. kfree(request);
  1832. }
  1833. /* Move any buffers on the active list that are no longer referenced
  1834. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1835. */
  1836. while (!list_empty(&ring->active_list)) {
  1837. struct drm_i915_gem_object *obj;
  1838. obj = list_first_entry(&ring->active_list,
  1839. struct drm_i915_gem_object,
  1840. ring_list);
  1841. if (!i915_seqno_passed(seqno, obj->last_read_seqno))
  1842. break;
  1843. i915_gem_object_move_to_inactive(obj);
  1844. }
  1845. if (unlikely(ring->trace_irq_seqno &&
  1846. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  1847. ring->irq_put(ring);
  1848. ring->trace_irq_seqno = 0;
  1849. }
  1850. WARN_ON(i915_verify_lists(ring->dev));
  1851. }
  1852. void
  1853. i915_gem_retire_requests(struct drm_device *dev)
  1854. {
  1855. drm_i915_private_t *dev_priv = dev->dev_private;
  1856. struct intel_ring_buffer *ring;
  1857. int i;
  1858. for_each_ring(ring, dev_priv, i)
  1859. i915_gem_retire_requests_ring(ring);
  1860. }
  1861. static void
  1862. i915_gem_retire_work_handler(struct work_struct *work)
  1863. {
  1864. drm_i915_private_t *dev_priv;
  1865. struct drm_device *dev;
  1866. struct intel_ring_buffer *ring;
  1867. bool idle;
  1868. int i;
  1869. dev_priv = container_of(work, drm_i915_private_t,
  1870. mm.retire_work.work);
  1871. dev = dev_priv->dev;
  1872. /* Come back later if the device is busy... */
  1873. if (!mutex_trylock(&dev->struct_mutex)) {
  1874. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1875. round_jiffies_up_relative(HZ));
  1876. return;
  1877. }
  1878. i915_gem_retire_requests(dev);
  1879. /* Send a periodic flush down the ring so we don't hold onto GEM
  1880. * objects indefinitely.
  1881. */
  1882. idle = true;
  1883. for_each_ring(ring, dev_priv, i) {
  1884. if (ring->gpu_caches_dirty)
  1885. i915_add_request(ring, NULL, NULL);
  1886. idle &= list_empty(&ring->request_list);
  1887. }
  1888. if (!dev_priv->mm.suspended && !idle)
  1889. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1890. round_jiffies_up_relative(HZ));
  1891. if (idle)
  1892. intel_mark_idle(dev);
  1893. mutex_unlock(&dev->struct_mutex);
  1894. }
  1895. /**
  1896. * Ensures that an object will eventually get non-busy by flushing any required
  1897. * write domains, emitting any outstanding lazy request and retiring and
  1898. * completed requests.
  1899. */
  1900. static int
  1901. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  1902. {
  1903. int ret;
  1904. if (obj->active) {
  1905. ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
  1906. if (ret)
  1907. return ret;
  1908. i915_gem_retire_requests_ring(obj->ring);
  1909. }
  1910. return 0;
  1911. }
  1912. /**
  1913. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  1914. * @DRM_IOCTL_ARGS: standard ioctl arguments
  1915. *
  1916. * Returns 0 if successful, else an error is returned with the remaining time in
  1917. * the timeout parameter.
  1918. * -ETIME: object is still busy after timeout
  1919. * -ERESTARTSYS: signal interrupted the wait
  1920. * -ENONENT: object doesn't exist
  1921. * Also possible, but rare:
  1922. * -EAGAIN: GPU wedged
  1923. * -ENOMEM: damn
  1924. * -ENODEV: Internal IRQ fail
  1925. * -E?: The add request failed
  1926. *
  1927. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  1928. * non-zero timeout parameter the wait ioctl will wait for the given number of
  1929. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  1930. * without holding struct_mutex the object may become re-busied before this
  1931. * function completes. A similar but shorter * race condition exists in the busy
  1932. * ioctl
  1933. */
  1934. int
  1935. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  1936. {
  1937. drm_i915_private_t *dev_priv = dev->dev_private;
  1938. struct drm_i915_gem_wait *args = data;
  1939. struct drm_i915_gem_object *obj;
  1940. struct intel_ring_buffer *ring = NULL;
  1941. struct timespec timeout_stack, *timeout = NULL;
  1942. unsigned reset_counter;
  1943. u32 seqno = 0;
  1944. int ret = 0;
  1945. if (args->timeout_ns >= 0) {
  1946. timeout_stack = ns_to_timespec(args->timeout_ns);
  1947. timeout = &timeout_stack;
  1948. }
  1949. ret = i915_mutex_lock_interruptible(dev);
  1950. if (ret)
  1951. return ret;
  1952. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  1953. if (&obj->base == NULL) {
  1954. mutex_unlock(&dev->struct_mutex);
  1955. return -ENOENT;
  1956. }
  1957. /* Need to make sure the object gets inactive eventually. */
  1958. ret = i915_gem_object_flush_active(obj);
  1959. if (ret)
  1960. goto out;
  1961. if (obj->active) {
  1962. seqno = obj->last_read_seqno;
  1963. ring = obj->ring;
  1964. }
  1965. if (seqno == 0)
  1966. goto out;
  1967. /* Do this after OLR check to make sure we make forward progress polling
  1968. * on this IOCTL with a 0 timeout (like busy ioctl)
  1969. */
  1970. if (!args->timeout_ns) {
  1971. ret = -ETIME;
  1972. goto out;
  1973. }
  1974. drm_gem_object_unreference(&obj->base);
  1975. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  1976. mutex_unlock(&dev->struct_mutex);
  1977. ret = __wait_seqno(ring, seqno, reset_counter, true, timeout);
  1978. if (timeout) {
  1979. WARN_ON(!timespec_valid(timeout));
  1980. args->timeout_ns = timespec_to_ns(timeout);
  1981. }
  1982. return ret;
  1983. out:
  1984. drm_gem_object_unreference(&obj->base);
  1985. mutex_unlock(&dev->struct_mutex);
  1986. return ret;
  1987. }
  1988. /**
  1989. * i915_gem_object_sync - sync an object to a ring.
  1990. *
  1991. * @obj: object which may be in use on another ring.
  1992. * @to: ring we wish to use the object on. May be NULL.
  1993. *
  1994. * This code is meant to abstract object synchronization with the GPU.
  1995. * Calling with NULL implies synchronizing the object with the CPU
  1996. * rather than a particular GPU ring.
  1997. *
  1998. * Returns 0 if successful, else propagates up the lower layer error.
  1999. */
  2000. int
  2001. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2002. struct intel_ring_buffer *to)
  2003. {
  2004. struct intel_ring_buffer *from = obj->ring;
  2005. u32 seqno;
  2006. int ret, idx;
  2007. if (from == NULL || to == from)
  2008. return 0;
  2009. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  2010. return i915_gem_object_wait_rendering(obj, false);
  2011. idx = intel_ring_sync_index(from, to);
  2012. seqno = obj->last_read_seqno;
  2013. if (seqno <= from->sync_seqno[idx])
  2014. return 0;
  2015. ret = i915_gem_check_olr(obj->ring, seqno);
  2016. if (ret)
  2017. return ret;
  2018. ret = to->sync_to(to, from, seqno);
  2019. if (!ret)
  2020. /* We use last_read_seqno because sync_to()
  2021. * might have just caused seqno wrap under
  2022. * the radar.
  2023. */
  2024. from->sync_seqno[idx] = obj->last_read_seqno;
  2025. return ret;
  2026. }
  2027. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2028. {
  2029. u32 old_write_domain, old_read_domains;
  2030. /* Force a pagefault for domain tracking on next user access */
  2031. i915_gem_release_mmap(obj);
  2032. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2033. return;
  2034. /* Wait for any direct GTT access to complete */
  2035. mb();
  2036. old_read_domains = obj->base.read_domains;
  2037. old_write_domain = obj->base.write_domain;
  2038. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2039. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2040. trace_i915_gem_object_change_domain(obj,
  2041. old_read_domains,
  2042. old_write_domain);
  2043. }
  2044. /**
  2045. * Unbinds an object from the GTT aperture.
  2046. */
  2047. int
  2048. i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  2049. {
  2050. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2051. int ret;
  2052. if (obj->gtt_space == NULL)
  2053. return 0;
  2054. if (obj->pin_count)
  2055. return -EBUSY;
  2056. BUG_ON(obj->pages == NULL);
  2057. ret = i915_gem_object_finish_gpu(obj);
  2058. if (ret)
  2059. return ret;
  2060. /* Continue on if we fail due to EIO, the GPU is hung so we
  2061. * should be safe and we need to cleanup or else we might
  2062. * cause memory corruption through use-after-free.
  2063. */
  2064. i915_gem_object_finish_gtt(obj);
  2065. /* release the fence reg _after_ flushing */
  2066. ret = i915_gem_object_put_fence(obj);
  2067. if (ret)
  2068. return ret;
  2069. trace_i915_gem_object_unbind(obj);
  2070. if (obj->has_global_gtt_mapping)
  2071. i915_gem_gtt_unbind_object(obj);
  2072. if (obj->has_aliasing_ppgtt_mapping) {
  2073. i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
  2074. obj->has_aliasing_ppgtt_mapping = 0;
  2075. }
  2076. i915_gem_gtt_finish_object(obj);
  2077. list_del(&obj->mm_list);
  2078. list_move_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
  2079. /* Avoid an unnecessary call to unbind on rebind. */
  2080. obj->map_and_fenceable = true;
  2081. drm_mm_put_block(obj->gtt_space);
  2082. obj->gtt_space = NULL;
  2083. obj->gtt_offset = 0;
  2084. return 0;
  2085. }
  2086. int i915_gpu_idle(struct drm_device *dev)
  2087. {
  2088. drm_i915_private_t *dev_priv = dev->dev_private;
  2089. struct intel_ring_buffer *ring;
  2090. int ret, i;
  2091. /* Flush everything onto the inactive list. */
  2092. for_each_ring(ring, dev_priv, i) {
  2093. ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
  2094. if (ret)
  2095. return ret;
  2096. ret = intel_ring_idle(ring);
  2097. if (ret)
  2098. return ret;
  2099. }
  2100. return 0;
  2101. }
  2102. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2103. struct drm_i915_gem_object *obj)
  2104. {
  2105. drm_i915_private_t *dev_priv = dev->dev_private;
  2106. int fence_reg;
  2107. int fence_pitch_shift;
  2108. uint64_t val;
  2109. if (INTEL_INFO(dev)->gen >= 6) {
  2110. fence_reg = FENCE_REG_SANDYBRIDGE_0;
  2111. fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2112. } else {
  2113. fence_reg = FENCE_REG_965_0;
  2114. fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  2115. }
  2116. if (obj) {
  2117. u32 size = obj->gtt_space->size;
  2118. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  2119. 0xfffff000) << 32;
  2120. val |= obj->gtt_offset & 0xfffff000;
  2121. val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
  2122. if (obj->tiling_mode == I915_TILING_Y)
  2123. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2124. val |= I965_FENCE_REG_VALID;
  2125. } else
  2126. val = 0;
  2127. fence_reg += reg * 8;
  2128. I915_WRITE64(fence_reg, val);
  2129. POSTING_READ(fence_reg);
  2130. }
  2131. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2132. struct drm_i915_gem_object *obj)
  2133. {
  2134. drm_i915_private_t *dev_priv = dev->dev_private;
  2135. u32 val;
  2136. if (obj) {
  2137. u32 size = obj->gtt_space->size;
  2138. int pitch_val;
  2139. int tile_width;
  2140. WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
  2141. (size & -size) != size ||
  2142. (obj->gtt_offset & (size - 1)),
  2143. "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2144. obj->gtt_offset, obj->map_and_fenceable, size);
  2145. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2146. tile_width = 128;
  2147. else
  2148. tile_width = 512;
  2149. /* Note: pitch better be a power of two tile widths */
  2150. pitch_val = obj->stride / tile_width;
  2151. pitch_val = ffs(pitch_val) - 1;
  2152. val = obj->gtt_offset;
  2153. if (obj->tiling_mode == I915_TILING_Y)
  2154. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2155. val |= I915_FENCE_SIZE_BITS(size);
  2156. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2157. val |= I830_FENCE_REG_VALID;
  2158. } else
  2159. val = 0;
  2160. if (reg < 8)
  2161. reg = FENCE_REG_830_0 + reg * 4;
  2162. else
  2163. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2164. I915_WRITE(reg, val);
  2165. POSTING_READ(reg);
  2166. }
  2167. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2168. struct drm_i915_gem_object *obj)
  2169. {
  2170. drm_i915_private_t *dev_priv = dev->dev_private;
  2171. uint32_t val;
  2172. if (obj) {
  2173. u32 size = obj->gtt_space->size;
  2174. uint32_t pitch_val;
  2175. WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
  2176. (size & -size) != size ||
  2177. (obj->gtt_offset & (size - 1)),
  2178. "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
  2179. obj->gtt_offset, size);
  2180. pitch_val = obj->stride / 128;
  2181. pitch_val = ffs(pitch_val) - 1;
  2182. val = obj->gtt_offset;
  2183. if (obj->tiling_mode == I915_TILING_Y)
  2184. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2185. val |= I830_FENCE_SIZE_BITS(size);
  2186. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2187. val |= I830_FENCE_REG_VALID;
  2188. } else
  2189. val = 0;
  2190. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2191. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2192. }
  2193. inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
  2194. {
  2195. return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
  2196. }
  2197. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2198. struct drm_i915_gem_object *obj)
  2199. {
  2200. struct drm_i915_private *dev_priv = dev->dev_private;
  2201. /* Ensure that all CPU reads are completed before installing a fence
  2202. * and all writes before removing the fence.
  2203. */
  2204. if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
  2205. mb();
  2206. switch (INTEL_INFO(dev)->gen) {
  2207. case 7:
  2208. case 6:
  2209. case 5:
  2210. case 4: i965_write_fence_reg(dev, reg, obj); break;
  2211. case 3: i915_write_fence_reg(dev, reg, obj); break;
  2212. case 2: i830_write_fence_reg(dev, reg, obj); break;
  2213. default: BUG();
  2214. }
  2215. /* And similarly be paranoid that no direct access to this region
  2216. * is reordered to before the fence is installed.
  2217. */
  2218. if (i915_gem_object_needs_mb(obj))
  2219. mb();
  2220. }
  2221. static inline int fence_number(struct drm_i915_private *dev_priv,
  2222. struct drm_i915_fence_reg *fence)
  2223. {
  2224. return fence - dev_priv->fence_regs;
  2225. }
  2226. static void i915_gem_write_fence__ipi(void *data)
  2227. {
  2228. wbinvd();
  2229. }
  2230. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2231. struct drm_i915_fence_reg *fence,
  2232. bool enable)
  2233. {
  2234. struct drm_device *dev = obj->base.dev;
  2235. struct drm_i915_private *dev_priv = dev->dev_private;
  2236. int fence_reg = fence_number(dev_priv, fence);
  2237. /* In order to fully serialize access to the fenced region and
  2238. * the update to the fence register we need to take extreme
  2239. * measures on SNB+. In theory, the write to the fence register
  2240. * flushes all memory transactions before, and coupled with the
  2241. * mb() placed around the register write we serialise all memory
  2242. * operations with respect to the changes in the tiler. Yet, on
  2243. * SNB+ we need to take a step further and emit an explicit wbinvd()
  2244. * on each processor in order to manually flush all memory
  2245. * transactions before updating the fence register.
  2246. */
  2247. if (HAS_LLC(obj->base.dev))
  2248. on_each_cpu(i915_gem_write_fence__ipi, NULL, 1);
  2249. i915_gem_write_fence(dev, fence_reg, enable ? obj : NULL);
  2250. if (enable) {
  2251. obj->fence_reg = fence_reg;
  2252. fence->obj = obj;
  2253. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2254. } else {
  2255. obj->fence_reg = I915_FENCE_REG_NONE;
  2256. fence->obj = NULL;
  2257. list_del_init(&fence->lru_list);
  2258. }
  2259. }
  2260. static int
  2261. i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
  2262. {
  2263. if (obj->last_fenced_seqno) {
  2264. int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
  2265. if (ret)
  2266. return ret;
  2267. obj->last_fenced_seqno = 0;
  2268. }
  2269. obj->fenced_gpu_access = false;
  2270. return 0;
  2271. }
  2272. int
  2273. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2274. {
  2275. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2276. struct drm_i915_fence_reg *fence;
  2277. int ret;
  2278. ret = i915_gem_object_wait_fence(obj);
  2279. if (ret)
  2280. return ret;
  2281. if (obj->fence_reg == I915_FENCE_REG_NONE)
  2282. return 0;
  2283. fence = &dev_priv->fence_regs[obj->fence_reg];
  2284. i915_gem_object_fence_lost(obj);
  2285. i915_gem_object_update_fence(obj, fence, false);
  2286. return 0;
  2287. }
  2288. static struct drm_i915_fence_reg *
  2289. i915_find_fence_reg(struct drm_device *dev)
  2290. {
  2291. struct drm_i915_private *dev_priv = dev->dev_private;
  2292. struct drm_i915_fence_reg *reg, *avail;
  2293. int i;
  2294. /* First try to find a free reg */
  2295. avail = NULL;
  2296. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2297. reg = &dev_priv->fence_regs[i];
  2298. if (!reg->obj)
  2299. return reg;
  2300. if (!reg->pin_count)
  2301. avail = reg;
  2302. }
  2303. if (avail == NULL)
  2304. return NULL;
  2305. /* None available, try to steal one or wait for a user to finish */
  2306. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2307. if (reg->pin_count)
  2308. continue;
  2309. return reg;
  2310. }
  2311. return NULL;
  2312. }
  2313. /**
  2314. * i915_gem_object_get_fence - set up fencing for an object
  2315. * @obj: object to map through a fence reg
  2316. *
  2317. * When mapping objects through the GTT, userspace wants to be able to write
  2318. * to them without having to worry about swizzling if the object is tiled.
  2319. * This function walks the fence regs looking for a free one for @obj,
  2320. * stealing one if it can't find any.
  2321. *
  2322. * It then sets up the reg based on the object's properties: address, pitch
  2323. * and tiling format.
  2324. *
  2325. * For an untiled surface, this removes any existing fence.
  2326. */
  2327. int
  2328. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2329. {
  2330. struct drm_device *dev = obj->base.dev;
  2331. struct drm_i915_private *dev_priv = dev->dev_private;
  2332. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2333. struct drm_i915_fence_reg *reg;
  2334. int ret;
  2335. /* Have we updated the tiling parameters upon the object and so
  2336. * will need to serialise the write to the associated fence register?
  2337. */
  2338. if (obj->fence_dirty) {
  2339. ret = i915_gem_object_wait_fence(obj);
  2340. if (ret)
  2341. return ret;
  2342. }
  2343. /* Just update our place in the LRU if our fence is getting reused. */
  2344. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2345. reg = &dev_priv->fence_regs[obj->fence_reg];
  2346. if (!obj->fence_dirty) {
  2347. list_move_tail(&reg->lru_list,
  2348. &dev_priv->mm.fence_list);
  2349. return 0;
  2350. }
  2351. } else if (enable) {
  2352. reg = i915_find_fence_reg(dev);
  2353. if (reg == NULL)
  2354. return -EDEADLK;
  2355. if (reg->obj) {
  2356. struct drm_i915_gem_object *old = reg->obj;
  2357. ret = i915_gem_object_wait_fence(old);
  2358. if (ret)
  2359. return ret;
  2360. i915_gem_object_fence_lost(old);
  2361. }
  2362. } else
  2363. return 0;
  2364. i915_gem_object_update_fence(obj, reg, enable);
  2365. obj->fence_dirty = false;
  2366. return 0;
  2367. }
  2368. static bool i915_gem_valid_gtt_space(struct drm_device *dev,
  2369. struct drm_mm_node *gtt_space,
  2370. unsigned long cache_level)
  2371. {
  2372. struct drm_mm_node *other;
  2373. /* On non-LLC machines we have to be careful when putting differing
  2374. * types of snoopable memory together to avoid the prefetcher
  2375. * crossing memory domains and dying.
  2376. */
  2377. if (HAS_LLC(dev))
  2378. return true;
  2379. if (gtt_space == NULL)
  2380. return true;
  2381. if (list_empty(&gtt_space->node_list))
  2382. return true;
  2383. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2384. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2385. return false;
  2386. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2387. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2388. return false;
  2389. return true;
  2390. }
  2391. static void i915_gem_verify_gtt(struct drm_device *dev)
  2392. {
  2393. #if WATCH_GTT
  2394. struct drm_i915_private *dev_priv = dev->dev_private;
  2395. struct drm_i915_gem_object *obj;
  2396. int err = 0;
  2397. list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
  2398. if (obj->gtt_space == NULL) {
  2399. printk(KERN_ERR "object found on GTT list with no space reserved\n");
  2400. err++;
  2401. continue;
  2402. }
  2403. if (obj->cache_level != obj->gtt_space->color) {
  2404. printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
  2405. obj->gtt_space->start,
  2406. obj->gtt_space->start + obj->gtt_space->size,
  2407. obj->cache_level,
  2408. obj->gtt_space->color);
  2409. err++;
  2410. continue;
  2411. }
  2412. if (!i915_gem_valid_gtt_space(dev,
  2413. obj->gtt_space,
  2414. obj->cache_level)) {
  2415. printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
  2416. obj->gtt_space->start,
  2417. obj->gtt_space->start + obj->gtt_space->size,
  2418. obj->cache_level);
  2419. err++;
  2420. continue;
  2421. }
  2422. }
  2423. WARN_ON(err);
  2424. #endif
  2425. }
  2426. /**
  2427. * Finds free space in the GTT aperture and binds the object there.
  2428. */
  2429. static int
  2430. i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  2431. unsigned alignment,
  2432. bool map_and_fenceable,
  2433. bool nonblocking)
  2434. {
  2435. struct drm_device *dev = obj->base.dev;
  2436. drm_i915_private_t *dev_priv = dev->dev_private;
  2437. struct drm_mm_node *node;
  2438. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2439. bool mappable, fenceable;
  2440. int ret;
  2441. fence_size = i915_gem_get_gtt_size(dev,
  2442. obj->base.size,
  2443. obj->tiling_mode);
  2444. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2445. obj->base.size,
  2446. obj->tiling_mode, true);
  2447. unfenced_alignment =
  2448. i915_gem_get_gtt_alignment(dev,
  2449. obj->base.size,
  2450. obj->tiling_mode, false);
  2451. if (alignment == 0)
  2452. alignment = map_and_fenceable ? fence_alignment :
  2453. unfenced_alignment;
  2454. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2455. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2456. return -EINVAL;
  2457. }
  2458. size = map_and_fenceable ? fence_size : obj->base.size;
  2459. /* If the object is bigger than the entire aperture, reject it early
  2460. * before evicting everything in a vain attempt to find space.
  2461. */
  2462. if (obj->base.size >
  2463. (map_and_fenceable ? dev_priv->gtt.mappable_end : dev_priv->gtt.total)) {
  2464. DRM_ERROR("Attempting to bind an object larger than the aperture\n");
  2465. return -E2BIG;
  2466. }
  2467. ret = i915_gem_object_get_pages(obj);
  2468. if (ret)
  2469. return ret;
  2470. i915_gem_object_pin_pages(obj);
  2471. node = kzalloc(sizeof(*node), GFP_KERNEL);
  2472. if (node == NULL) {
  2473. i915_gem_object_unpin_pages(obj);
  2474. return -ENOMEM;
  2475. }
  2476. search_free:
  2477. if (map_and_fenceable)
  2478. ret = drm_mm_insert_node_in_range_generic(&dev_priv->mm.gtt_space, node,
  2479. size, alignment, obj->cache_level,
  2480. 0, dev_priv->gtt.mappable_end);
  2481. else
  2482. ret = drm_mm_insert_node_generic(&dev_priv->mm.gtt_space, node,
  2483. size, alignment, obj->cache_level);
  2484. if (ret) {
  2485. ret = i915_gem_evict_something(dev, size, alignment,
  2486. obj->cache_level,
  2487. map_and_fenceable,
  2488. nonblocking);
  2489. if (ret == 0)
  2490. goto search_free;
  2491. i915_gem_object_unpin_pages(obj);
  2492. kfree(node);
  2493. return ret;
  2494. }
  2495. if (WARN_ON(!i915_gem_valid_gtt_space(dev, node, obj->cache_level))) {
  2496. i915_gem_object_unpin_pages(obj);
  2497. drm_mm_put_block(node);
  2498. return -EINVAL;
  2499. }
  2500. ret = i915_gem_gtt_prepare_object(obj);
  2501. if (ret) {
  2502. i915_gem_object_unpin_pages(obj);
  2503. drm_mm_put_block(node);
  2504. return ret;
  2505. }
  2506. list_move_tail(&obj->gtt_list, &dev_priv->mm.bound_list);
  2507. list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2508. obj->gtt_space = node;
  2509. obj->gtt_offset = node->start;
  2510. fenceable =
  2511. node->size == fence_size &&
  2512. (node->start & (fence_alignment - 1)) == 0;
  2513. mappable =
  2514. obj->gtt_offset + obj->base.size <= dev_priv->gtt.mappable_end;
  2515. obj->map_and_fenceable = mappable && fenceable;
  2516. i915_gem_object_unpin_pages(obj);
  2517. trace_i915_gem_object_bind(obj, map_and_fenceable);
  2518. i915_gem_verify_gtt(dev);
  2519. return 0;
  2520. }
  2521. void
  2522. i915_gem_clflush_object(struct drm_i915_gem_object *obj)
  2523. {
  2524. /* If we don't have a page list set up, then we're not pinned
  2525. * to GPU, and we can ignore the cache flush because it'll happen
  2526. * again at bind time.
  2527. */
  2528. if (obj->pages == NULL)
  2529. return;
  2530. /*
  2531. * Stolen memory is always coherent with the GPU as it is explicitly
  2532. * marked as wc by the system, or the system is cache-coherent.
  2533. */
  2534. if (obj->stolen)
  2535. return;
  2536. /* If the GPU is snooping the contents of the CPU cache,
  2537. * we do not need to manually clear the CPU cache lines. However,
  2538. * the caches are only snooped when the render cache is
  2539. * flushed/invalidated. As we always have to emit invalidations
  2540. * and flushes when moving into and out of the RENDER domain, correct
  2541. * snooping behaviour occurs naturally as the result of our domain
  2542. * tracking.
  2543. */
  2544. if (obj->cache_level != I915_CACHE_NONE)
  2545. return;
  2546. trace_i915_gem_object_clflush(obj);
  2547. drm_clflush_sg(obj->pages);
  2548. }
  2549. /** Flushes the GTT write domain for the object if it's dirty. */
  2550. static void
  2551. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2552. {
  2553. uint32_t old_write_domain;
  2554. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2555. return;
  2556. /* No actual flushing is required for the GTT write domain. Writes
  2557. * to it immediately go to main memory as far as we know, so there's
  2558. * no chipset flush. It also doesn't land in render cache.
  2559. *
  2560. * However, we do have to enforce the order so that all writes through
  2561. * the GTT land before any writes to the device, such as updates to
  2562. * the GATT itself.
  2563. */
  2564. wmb();
  2565. old_write_domain = obj->base.write_domain;
  2566. obj->base.write_domain = 0;
  2567. trace_i915_gem_object_change_domain(obj,
  2568. obj->base.read_domains,
  2569. old_write_domain);
  2570. }
  2571. /** Flushes the CPU write domain for the object if it's dirty. */
  2572. static void
  2573. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2574. {
  2575. uint32_t old_write_domain;
  2576. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2577. return;
  2578. i915_gem_clflush_object(obj);
  2579. i915_gem_chipset_flush(obj->base.dev);
  2580. old_write_domain = obj->base.write_domain;
  2581. obj->base.write_domain = 0;
  2582. trace_i915_gem_object_change_domain(obj,
  2583. obj->base.read_domains,
  2584. old_write_domain);
  2585. }
  2586. /**
  2587. * Moves a single object to the GTT read, and possibly write domain.
  2588. *
  2589. * This function returns when the move is complete, including waiting on
  2590. * flushes to occur.
  2591. */
  2592. int
  2593. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2594. {
  2595. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2596. uint32_t old_write_domain, old_read_domains;
  2597. int ret;
  2598. /* Not valid to be called on unbound objects. */
  2599. if (obj->gtt_space == NULL)
  2600. return -EINVAL;
  2601. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2602. return 0;
  2603. ret = i915_gem_object_wait_rendering(obj, !write);
  2604. if (ret)
  2605. return ret;
  2606. i915_gem_object_flush_cpu_write_domain(obj);
  2607. /* Serialise direct access to this object with the barriers for
  2608. * coherent writes from the GPU, by effectively invalidating the
  2609. * GTT domain upon first access.
  2610. */
  2611. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2612. mb();
  2613. old_write_domain = obj->base.write_domain;
  2614. old_read_domains = obj->base.read_domains;
  2615. /* It should now be out of any other write domains, and we can update
  2616. * the domain values for our changes.
  2617. */
  2618. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2619. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2620. if (write) {
  2621. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2622. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2623. obj->dirty = 1;
  2624. }
  2625. trace_i915_gem_object_change_domain(obj,
  2626. old_read_domains,
  2627. old_write_domain);
  2628. /* And bump the LRU for this access */
  2629. if (i915_gem_object_is_inactive(obj))
  2630. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2631. return 0;
  2632. }
  2633. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2634. enum i915_cache_level cache_level)
  2635. {
  2636. struct drm_device *dev = obj->base.dev;
  2637. drm_i915_private_t *dev_priv = dev->dev_private;
  2638. int ret;
  2639. if (obj->cache_level == cache_level)
  2640. return 0;
  2641. if (obj->pin_count) {
  2642. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2643. return -EBUSY;
  2644. }
  2645. if (!i915_gem_valid_gtt_space(dev, obj->gtt_space, cache_level)) {
  2646. ret = i915_gem_object_unbind(obj);
  2647. if (ret)
  2648. return ret;
  2649. }
  2650. if (obj->gtt_space) {
  2651. ret = i915_gem_object_finish_gpu(obj);
  2652. if (ret)
  2653. return ret;
  2654. i915_gem_object_finish_gtt(obj);
  2655. /* Before SandyBridge, you could not use tiling or fence
  2656. * registers with snooped memory, so relinquish any fences
  2657. * currently pointing to our region in the aperture.
  2658. */
  2659. if (INTEL_INFO(dev)->gen < 6) {
  2660. ret = i915_gem_object_put_fence(obj);
  2661. if (ret)
  2662. return ret;
  2663. }
  2664. if (obj->has_global_gtt_mapping)
  2665. i915_gem_gtt_bind_object(obj, cache_level);
  2666. if (obj->has_aliasing_ppgtt_mapping)
  2667. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  2668. obj, cache_level);
  2669. obj->gtt_space->color = cache_level;
  2670. }
  2671. if (cache_level == I915_CACHE_NONE) {
  2672. u32 old_read_domains, old_write_domain;
  2673. /* If we're coming from LLC cached, then we haven't
  2674. * actually been tracking whether the data is in the
  2675. * CPU cache or not, since we only allow one bit set
  2676. * in obj->write_domain and have been skipping the clflushes.
  2677. * Just set it to the CPU cache for now.
  2678. */
  2679. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  2680. WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
  2681. old_read_domains = obj->base.read_domains;
  2682. old_write_domain = obj->base.write_domain;
  2683. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2684. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2685. trace_i915_gem_object_change_domain(obj,
  2686. old_read_domains,
  2687. old_write_domain);
  2688. }
  2689. obj->cache_level = cache_level;
  2690. i915_gem_verify_gtt(dev);
  2691. return 0;
  2692. }
  2693. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  2694. struct drm_file *file)
  2695. {
  2696. struct drm_i915_gem_caching *args = data;
  2697. struct drm_i915_gem_object *obj;
  2698. int ret;
  2699. ret = i915_mutex_lock_interruptible(dev);
  2700. if (ret)
  2701. return ret;
  2702. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2703. if (&obj->base == NULL) {
  2704. ret = -ENOENT;
  2705. goto unlock;
  2706. }
  2707. args->caching = obj->cache_level != I915_CACHE_NONE;
  2708. drm_gem_object_unreference(&obj->base);
  2709. unlock:
  2710. mutex_unlock(&dev->struct_mutex);
  2711. return ret;
  2712. }
  2713. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  2714. struct drm_file *file)
  2715. {
  2716. struct drm_i915_gem_caching *args = data;
  2717. struct drm_i915_gem_object *obj;
  2718. enum i915_cache_level level;
  2719. int ret;
  2720. switch (args->caching) {
  2721. case I915_CACHING_NONE:
  2722. level = I915_CACHE_NONE;
  2723. break;
  2724. case I915_CACHING_CACHED:
  2725. level = I915_CACHE_LLC;
  2726. break;
  2727. default:
  2728. return -EINVAL;
  2729. }
  2730. ret = i915_mutex_lock_interruptible(dev);
  2731. if (ret)
  2732. return ret;
  2733. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2734. if (&obj->base == NULL) {
  2735. ret = -ENOENT;
  2736. goto unlock;
  2737. }
  2738. ret = i915_gem_object_set_cache_level(obj, level);
  2739. drm_gem_object_unreference(&obj->base);
  2740. unlock:
  2741. mutex_unlock(&dev->struct_mutex);
  2742. return ret;
  2743. }
  2744. /*
  2745. * Prepare buffer for display plane (scanout, cursors, etc).
  2746. * Can be called from an uninterruptible phase (modesetting) and allows
  2747. * any flushes to be pipelined (for pageflips).
  2748. */
  2749. int
  2750. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2751. u32 alignment,
  2752. struct intel_ring_buffer *pipelined)
  2753. {
  2754. u32 old_read_domains, old_write_domain;
  2755. int ret;
  2756. if (pipelined != obj->ring) {
  2757. ret = i915_gem_object_sync(obj, pipelined);
  2758. if (ret)
  2759. return ret;
  2760. }
  2761. /* The display engine is not coherent with the LLC cache on gen6. As
  2762. * a result, we make sure that the pinning that is about to occur is
  2763. * done with uncached PTEs. This is lowest common denominator for all
  2764. * chipsets.
  2765. *
  2766. * However for gen6+, we could do better by using the GFDT bit instead
  2767. * of uncaching, which would allow us to flush all the LLC-cached data
  2768. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2769. */
  2770. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
  2771. if (ret)
  2772. return ret;
  2773. /* As the user may map the buffer once pinned in the display plane
  2774. * (e.g. libkms for the bootup splash), we have to ensure that we
  2775. * always use map_and_fenceable for all scanout buffers.
  2776. */
  2777. ret = i915_gem_object_pin(obj, alignment, true, false);
  2778. if (ret)
  2779. return ret;
  2780. i915_gem_object_flush_cpu_write_domain(obj);
  2781. old_write_domain = obj->base.write_domain;
  2782. old_read_domains = obj->base.read_domains;
  2783. /* It should now be out of any other write domains, and we can update
  2784. * the domain values for our changes.
  2785. */
  2786. obj->base.write_domain = 0;
  2787. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2788. trace_i915_gem_object_change_domain(obj,
  2789. old_read_domains,
  2790. old_write_domain);
  2791. return 0;
  2792. }
  2793. int
  2794. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  2795. {
  2796. int ret;
  2797. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  2798. return 0;
  2799. ret = i915_gem_object_wait_rendering(obj, false);
  2800. if (ret)
  2801. return ret;
  2802. /* Ensure that we invalidate the GPU's caches and TLBs. */
  2803. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  2804. return 0;
  2805. }
  2806. /**
  2807. * Moves a single object to the CPU read, and possibly write domain.
  2808. *
  2809. * This function returns when the move is complete, including waiting on
  2810. * flushes to occur.
  2811. */
  2812. int
  2813. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  2814. {
  2815. uint32_t old_write_domain, old_read_domains;
  2816. int ret;
  2817. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  2818. return 0;
  2819. ret = i915_gem_object_wait_rendering(obj, !write);
  2820. if (ret)
  2821. return ret;
  2822. i915_gem_object_flush_gtt_write_domain(obj);
  2823. old_write_domain = obj->base.write_domain;
  2824. old_read_domains = obj->base.read_domains;
  2825. /* Flush the CPU cache if it's still invalid. */
  2826. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2827. i915_gem_clflush_object(obj);
  2828. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2829. }
  2830. /* It should now be out of any other write domains, and we can update
  2831. * the domain values for our changes.
  2832. */
  2833. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2834. /* If we're writing through the CPU, then the GPU read domains will
  2835. * need to be invalidated at next use.
  2836. */
  2837. if (write) {
  2838. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2839. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2840. }
  2841. trace_i915_gem_object_change_domain(obj,
  2842. old_read_domains,
  2843. old_write_domain);
  2844. return 0;
  2845. }
  2846. /* Throttle our rendering by waiting until the ring has completed our requests
  2847. * emitted over 20 msec ago.
  2848. *
  2849. * Note that if we were to use the current jiffies each time around the loop,
  2850. * we wouldn't escape the function with any frames outstanding if the time to
  2851. * render a frame was over 20ms.
  2852. *
  2853. * This should get us reasonable parallelism between CPU and GPU but also
  2854. * relatively low latency when blocking on a particular request to finish.
  2855. */
  2856. static int
  2857. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  2858. {
  2859. struct drm_i915_private *dev_priv = dev->dev_private;
  2860. struct drm_i915_file_private *file_priv = file->driver_priv;
  2861. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2862. struct drm_i915_gem_request *request;
  2863. struct intel_ring_buffer *ring = NULL;
  2864. unsigned reset_counter;
  2865. u32 seqno = 0;
  2866. int ret;
  2867. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  2868. if (ret)
  2869. return ret;
  2870. ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  2871. if (ret)
  2872. return ret;
  2873. spin_lock(&file_priv->mm.lock);
  2874. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  2875. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2876. break;
  2877. ring = request->ring;
  2878. seqno = request->seqno;
  2879. }
  2880. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2881. spin_unlock(&file_priv->mm.lock);
  2882. if (seqno == 0)
  2883. return 0;
  2884. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
  2885. if (ret == 0)
  2886. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  2887. return ret;
  2888. }
  2889. int
  2890. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  2891. uint32_t alignment,
  2892. bool map_and_fenceable,
  2893. bool nonblocking)
  2894. {
  2895. int ret;
  2896. if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  2897. return -EBUSY;
  2898. if (obj->gtt_space != NULL) {
  2899. if ((alignment && obj->gtt_offset & (alignment - 1)) ||
  2900. (map_and_fenceable && !obj->map_and_fenceable)) {
  2901. WARN(obj->pin_count,
  2902. "bo is already pinned with incorrect alignment:"
  2903. " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
  2904. " obj->map_and_fenceable=%d\n",
  2905. obj->gtt_offset, alignment,
  2906. map_and_fenceable,
  2907. obj->map_and_fenceable);
  2908. ret = i915_gem_object_unbind(obj);
  2909. if (ret)
  2910. return ret;
  2911. }
  2912. }
  2913. if (obj->gtt_space == NULL) {
  2914. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2915. ret = i915_gem_object_bind_to_gtt(obj, alignment,
  2916. map_and_fenceable,
  2917. nonblocking);
  2918. if (ret)
  2919. return ret;
  2920. if (!dev_priv->mm.aliasing_ppgtt)
  2921. i915_gem_gtt_bind_object(obj, obj->cache_level);
  2922. }
  2923. if (!obj->has_global_gtt_mapping && map_and_fenceable)
  2924. i915_gem_gtt_bind_object(obj, obj->cache_level);
  2925. obj->pin_count++;
  2926. obj->pin_mappable |= map_and_fenceable;
  2927. return 0;
  2928. }
  2929. void
  2930. i915_gem_object_unpin(struct drm_i915_gem_object *obj)
  2931. {
  2932. BUG_ON(obj->pin_count == 0);
  2933. BUG_ON(obj->gtt_space == NULL);
  2934. if (--obj->pin_count == 0)
  2935. obj->pin_mappable = false;
  2936. }
  2937. int
  2938. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  2939. struct drm_file *file)
  2940. {
  2941. struct drm_i915_gem_pin *args = data;
  2942. struct drm_i915_gem_object *obj;
  2943. int ret;
  2944. ret = i915_mutex_lock_interruptible(dev);
  2945. if (ret)
  2946. return ret;
  2947. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2948. if (&obj->base == NULL) {
  2949. ret = -ENOENT;
  2950. goto unlock;
  2951. }
  2952. if (obj->madv != I915_MADV_WILLNEED) {
  2953. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  2954. ret = -EINVAL;
  2955. goto out;
  2956. }
  2957. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  2958. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  2959. args->handle);
  2960. ret = -EINVAL;
  2961. goto out;
  2962. }
  2963. if (obj->user_pin_count == 0) {
  2964. ret = i915_gem_object_pin(obj, args->alignment, true, false);
  2965. if (ret)
  2966. goto out;
  2967. }
  2968. obj->user_pin_count++;
  2969. obj->pin_filp = file;
  2970. /* XXX - flush the CPU caches for pinned objects
  2971. * as the X server doesn't manage domains yet
  2972. */
  2973. i915_gem_object_flush_cpu_write_domain(obj);
  2974. args->offset = obj->gtt_offset;
  2975. out:
  2976. drm_gem_object_unreference(&obj->base);
  2977. unlock:
  2978. mutex_unlock(&dev->struct_mutex);
  2979. return ret;
  2980. }
  2981. int
  2982. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  2983. struct drm_file *file)
  2984. {
  2985. struct drm_i915_gem_pin *args = data;
  2986. struct drm_i915_gem_object *obj;
  2987. int ret;
  2988. ret = i915_mutex_lock_interruptible(dev);
  2989. if (ret)
  2990. return ret;
  2991. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2992. if (&obj->base == NULL) {
  2993. ret = -ENOENT;
  2994. goto unlock;
  2995. }
  2996. if (obj->pin_filp != file) {
  2997. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  2998. args->handle);
  2999. ret = -EINVAL;
  3000. goto out;
  3001. }
  3002. obj->user_pin_count--;
  3003. if (obj->user_pin_count == 0) {
  3004. obj->pin_filp = NULL;
  3005. i915_gem_object_unpin(obj);
  3006. }
  3007. out:
  3008. drm_gem_object_unreference(&obj->base);
  3009. unlock:
  3010. mutex_unlock(&dev->struct_mutex);
  3011. return ret;
  3012. }
  3013. int
  3014. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3015. struct drm_file *file)
  3016. {
  3017. struct drm_i915_gem_busy *args = data;
  3018. struct drm_i915_gem_object *obj;
  3019. int ret;
  3020. ret = i915_mutex_lock_interruptible(dev);
  3021. if (ret)
  3022. return ret;
  3023. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3024. if (&obj->base == NULL) {
  3025. ret = -ENOENT;
  3026. goto unlock;
  3027. }
  3028. /* Count all active objects as busy, even if they are currently not used
  3029. * by the gpu. Users of this interface expect objects to eventually
  3030. * become non-busy without any further actions, therefore emit any
  3031. * necessary flushes here.
  3032. */
  3033. ret = i915_gem_object_flush_active(obj);
  3034. args->busy = obj->active;
  3035. if (obj->ring) {
  3036. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3037. args->busy |= intel_ring_flag(obj->ring) << 16;
  3038. }
  3039. drm_gem_object_unreference(&obj->base);
  3040. unlock:
  3041. mutex_unlock(&dev->struct_mutex);
  3042. return ret;
  3043. }
  3044. int
  3045. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3046. struct drm_file *file_priv)
  3047. {
  3048. return i915_gem_ring_throttle(dev, file_priv);
  3049. }
  3050. int
  3051. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3052. struct drm_file *file_priv)
  3053. {
  3054. struct drm_i915_gem_madvise *args = data;
  3055. struct drm_i915_gem_object *obj;
  3056. int ret;
  3057. switch (args->madv) {
  3058. case I915_MADV_DONTNEED:
  3059. case I915_MADV_WILLNEED:
  3060. break;
  3061. default:
  3062. return -EINVAL;
  3063. }
  3064. ret = i915_mutex_lock_interruptible(dev);
  3065. if (ret)
  3066. return ret;
  3067. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3068. if (&obj->base == NULL) {
  3069. ret = -ENOENT;
  3070. goto unlock;
  3071. }
  3072. if (obj->pin_count) {
  3073. ret = -EINVAL;
  3074. goto out;
  3075. }
  3076. if (obj->madv != __I915_MADV_PURGED)
  3077. obj->madv = args->madv;
  3078. /* if the object is no longer attached, discard its backing storage */
  3079. if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
  3080. i915_gem_object_truncate(obj);
  3081. args->retained = obj->madv != __I915_MADV_PURGED;
  3082. out:
  3083. drm_gem_object_unreference(&obj->base);
  3084. unlock:
  3085. mutex_unlock(&dev->struct_mutex);
  3086. return ret;
  3087. }
  3088. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3089. const struct drm_i915_gem_object_ops *ops)
  3090. {
  3091. INIT_LIST_HEAD(&obj->mm_list);
  3092. INIT_LIST_HEAD(&obj->gtt_list);
  3093. INIT_LIST_HEAD(&obj->ring_list);
  3094. INIT_LIST_HEAD(&obj->exec_list);
  3095. obj->ops = ops;
  3096. obj->fence_reg = I915_FENCE_REG_NONE;
  3097. obj->madv = I915_MADV_WILLNEED;
  3098. /* Avoid an unnecessary call to unbind on the first bind. */
  3099. obj->map_and_fenceable = true;
  3100. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3101. }
  3102. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3103. .get_pages = i915_gem_object_get_pages_gtt,
  3104. .put_pages = i915_gem_object_put_pages_gtt,
  3105. };
  3106. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3107. size_t size)
  3108. {
  3109. struct drm_i915_gem_object *obj;
  3110. struct address_space *mapping;
  3111. gfp_t mask;
  3112. obj = i915_gem_object_alloc(dev);
  3113. if (obj == NULL)
  3114. return NULL;
  3115. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3116. i915_gem_object_free(obj);
  3117. return NULL;
  3118. }
  3119. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3120. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3121. /* 965gm cannot relocate objects above 4GiB. */
  3122. mask &= ~__GFP_HIGHMEM;
  3123. mask |= __GFP_DMA32;
  3124. }
  3125. mapping = file_inode(obj->base.filp)->i_mapping;
  3126. mapping_set_gfp_mask(mapping, mask);
  3127. i915_gem_object_init(obj, &i915_gem_object_ops);
  3128. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3129. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3130. if (HAS_LLC(dev)) {
  3131. /* On some devices, we can have the GPU use the LLC (the CPU
  3132. * cache) for about a 10% performance improvement
  3133. * compared to uncached. Graphics requests other than
  3134. * display scanout are coherent with the CPU in
  3135. * accessing this cache. This means in this mode we
  3136. * don't need to clflush on the CPU side, and on the
  3137. * GPU side we only need to flush internal caches to
  3138. * get data visible to the CPU.
  3139. *
  3140. * However, we maintain the display planes as UC, and so
  3141. * need to rebind when first used as such.
  3142. */
  3143. obj->cache_level = I915_CACHE_LLC;
  3144. } else
  3145. obj->cache_level = I915_CACHE_NONE;
  3146. return obj;
  3147. }
  3148. int i915_gem_init_object(struct drm_gem_object *obj)
  3149. {
  3150. BUG();
  3151. return 0;
  3152. }
  3153. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3154. {
  3155. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3156. struct drm_device *dev = obj->base.dev;
  3157. drm_i915_private_t *dev_priv = dev->dev_private;
  3158. trace_i915_gem_object_destroy(obj);
  3159. if (obj->phys_obj)
  3160. i915_gem_detach_phys_object(dev, obj);
  3161. obj->pin_count = 0;
  3162. if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
  3163. bool was_interruptible;
  3164. was_interruptible = dev_priv->mm.interruptible;
  3165. dev_priv->mm.interruptible = false;
  3166. WARN_ON(i915_gem_object_unbind(obj));
  3167. dev_priv->mm.interruptible = was_interruptible;
  3168. }
  3169. obj->pages_pin_count = 0;
  3170. i915_gem_object_put_pages(obj);
  3171. i915_gem_object_free_mmap_offset(obj);
  3172. i915_gem_object_release_stolen(obj);
  3173. BUG_ON(obj->pages);
  3174. if (obj->base.import_attach)
  3175. drm_prime_gem_destroy(&obj->base, NULL);
  3176. drm_gem_object_release(&obj->base);
  3177. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3178. kfree(obj->bit_17);
  3179. i915_gem_object_free(obj);
  3180. }
  3181. int
  3182. i915_gem_idle(struct drm_device *dev)
  3183. {
  3184. drm_i915_private_t *dev_priv = dev->dev_private;
  3185. int ret;
  3186. mutex_lock(&dev->struct_mutex);
  3187. if (dev_priv->mm.suspended) {
  3188. mutex_unlock(&dev->struct_mutex);
  3189. return 0;
  3190. }
  3191. ret = i915_gpu_idle(dev);
  3192. if (ret) {
  3193. mutex_unlock(&dev->struct_mutex);
  3194. return ret;
  3195. }
  3196. i915_gem_retire_requests(dev);
  3197. /* Under UMS, be paranoid and evict. */
  3198. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3199. i915_gem_evict_everything(dev);
  3200. i915_gem_reset_fences(dev);
  3201. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3202. * We need to replace this with a semaphore, or something.
  3203. * And not confound mm.suspended!
  3204. */
  3205. dev_priv->mm.suspended = 1;
  3206. del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
  3207. i915_kernel_lost_context(dev);
  3208. i915_gem_cleanup_ringbuffer(dev);
  3209. mutex_unlock(&dev->struct_mutex);
  3210. /* Cancel the retire work handler, which should be idle now. */
  3211. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3212. return 0;
  3213. }
  3214. void i915_gem_l3_remap(struct drm_device *dev)
  3215. {
  3216. drm_i915_private_t *dev_priv = dev->dev_private;
  3217. u32 misccpctl;
  3218. int i;
  3219. if (!HAS_L3_GPU_CACHE(dev))
  3220. return;
  3221. if (!dev_priv->l3_parity.remap_info)
  3222. return;
  3223. misccpctl = I915_READ(GEN7_MISCCPCTL);
  3224. I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  3225. POSTING_READ(GEN7_MISCCPCTL);
  3226. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3227. u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
  3228. if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
  3229. DRM_DEBUG("0x%x was already programmed to %x\n",
  3230. GEN7_L3LOG_BASE + i, remap);
  3231. if (remap && !dev_priv->l3_parity.remap_info[i/4])
  3232. DRM_DEBUG_DRIVER("Clearing remapped register\n");
  3233. I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
  3234. }
  3235. /* Make sure all the writes land before disabling dop clock gating */
  3236. POSTING_READ(GEN7_L3LOG_BASE);
  3237. I915_WRITE(GEN7_MISCCPCTL, misccpctl);
  3238. }
  3239. void i915_gem_init_swizzling(struct drm_device *dev)
  3240. {
  3241. drm_i915_private_t *dev_priv = dev->dev_private;
  3242. if (INTEL_INFO(dev)->gen < 5 ||
  3243. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3244. return;
  3245. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3246. DISP_TILE_SURFACE_SWIZZLING);
  3247. if (IS_GEN5(dev))
  3248. return;
  3249. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3250. if (IS_GEN6(dev))
  3251. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3252. else if (IS_GEN7(dev))
  3253. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3254. else
  3255. BUG();
  3256. }
  3257. static bool
  3258. intel_enable_blt(struct drm_device *dev)
  3259. {
  3260. if (!HAS_BLT(dev))
  3261. return false;
  3262. /* The blitter was dysfunctional on early prototypes */
  3263. if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  3264. DRM_INFO("BLT not supported on this pre-production hardware;"
  3265. " graphics performance will be degraded.\n");
  3266. return false;
  3267. }
  3268. return true;
  3269. }
  3270. static int i915_gem_init_rings(struct drm_device *dev)
  3271. {
  3272. struct drm_i915_private *dev_priv = dev->dev_private;
  3273. int ret;
  3274. ret = intel_init_render_ring_buffer(dev);
  3275. if (ret)
  3276. return ret;
  3277. if (HAS_BSD(dev)) {
  3278. ret = intel_init_bsd_ring_buffer(dev);
  3279. if (ret)
  3280. goto cleanup_render_ring;
  3281. }
  3282. if (intel_enable_blt(dev)) {
  3283. ret = intel_init_blt_ring_buffer(dev);
  3284. if (ret)
  3285. goto cleanup_bsd_ring;
  3286. }
  3287. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  3288. if (ret)
  3289. goto cleanup_blt_ring;
  3290. return 0;
  3291. cleanup_blt_ring:
  3292. intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  3293. cleanup_bsd_ring:
  3294. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3295. cleanup_render_ring:
  3296. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3297. return ret;
  3298. }
  3299. int
  3300. i915_gem_init_hw(struct drm_device *dev)
  3301. {
  3302. drm_i915_private_t *dev_priv = dev->dev_private;
  3303. int ret;
  3304. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3305. return -EIO;
  3306. if (IS_HASWELL(dev) && (I915_READ(0x120010) == 1))
  3307. I915_WRITE(0x9008, I915_READ(0x9008) | 0xf0000);
  3308. if (HAS_PCH_NOP(dev)) {
  3309. u32 temp = I915_READ(GEN7_MSG_CTL);
  3310. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3311. I915_WRITE(GEN7_MSG_CTL, temp);
  3312. }
  3313. i915_gem_l3_remap(dev);
  3314. i915_gem_init_swizzling(dev);
  3315. ret = i915_gem_init_rings(dev);
  3316. if (ret)
  3317. return ret;
  3318. /*
  3319. * XXX: There was some w/a described somewhere suggesting loading
  3320. * contexts before PPGTT.
  3321. */
  3322. i915_gem_context_init(dev);
  3323. if (dev_priv->mm.aliasing_ppgtt) {
  3324. ret = dev_priv->mm.aliasing_ppgtt->enable(dev);
  3325. if (ret) {
  3326. i915_gem_cleanup_aliasing_ppgtt(dev);
  3327. DRM_INFO("PPGTT enable failed. This is not fatal, but unexpected\n");
  3328. }
  3329. }
  3330. return 0;
  3331. }
  3332. int i915_gem_init(struct drm_device *dev)
  3333. {
  3334. struct drm_i915_private *dev_priv = dev->dev_private;
  3335. int ret;
  3336. mutex_lock(&dev->struct_mutex);
  3337. if (IS_VALLEYVIEW(dev)) {
  3338. /* VLVA0 (potential hack), BIOS isn't actually waking us */
  3339. I915_WRITE(VLV_GTLC_WAKE_CTRL, 1);
  3340. if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10))
  3341. DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  3342. }
  3343. i915_gem_init_global_gtt(dev);
  3344. ret = i915_gem_init_hw(dev);
  3345. mutex_unlock(&dev->struct_mutex);
  3346. if (ret) {
  3347. i915_gem_cleanup_aliasing_ppgtt(dev);
  3348. return ret;
  3349. }
  3350. /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
  3351. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3352. dev_priv->dri1.allow_batchbuffer = 1;
  3353. return 0;
  3354. }
  3355. void
  3356. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3357. {
  3358. drm_i915_private_t *dev_priv = dev->dev_private;
  3359. struct intel_ring_buffer *ring;
  3360. int i;
  3361. for_each_ring(ring, dev_priv, i)
  3362. intel_cleanup_ring_buffer(ring);
  3363. }
  3364. int
  3365. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3366. struct drm_file *file_priv)
  3367. {
  3368. drm_i915_private_t *dev_priv = dev->dev_private;
  3369. int ret;
  3370. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3371. return 0;
  3372. if (i915_reset_in_progress(&dev_priv->gpu_error)) {
  3373. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3374. atomic_set(&dev_priv->gpu_error.reset_counter, 0);
  3375. }
  3376. mutex_lock(&dev->struct_mutex);
  3377. dev_priv->mm.suspended = 0;
  3378. ret = i915_gem_init_hw(dev);
  3379. if (ret != 0) {
  3380. mutex_unlock(&dev->struct_mutex);
  3381. return ret;
  3382. }
  3383. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  3384. mutex_unlock(&dev->struct_mutex);
  3385. ret = drm_irq_install(dev);
  3386. if (ret)
  3387. goto cleanup_ringbuffer;
  3388. return 0;
  3389. cleanup_ringbuffer:
  3390. mutex_lock(&dev->struct_mutex);
  3391. i915_gem_cleanup_ringbuffer(dev);
  3392. dev_priv->mm.suspended = 1;
  3393. mutex_unlock(&dev->struct_mutex);
  3394. return ret;
  3395. }
  3396. int
  3397. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3398. struct drm_file *file_priv)
  3399. {
  3400. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3401. return 0;
  3402. drm_irq_uninstall(dev);
  3403. return i915_gem_idle(dev);
  3404. }
  3405. void
  3406. i915_gem_lastclose(struct drm_device *dev)
  3407. {
  3408. int ret;
  3409. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3410. return;
  3411. ret = i915_gem_idle(dev);
  3412. if (ret)
  3413. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3414. }
  3415. static void
  3416. init_ring_lists(struct intel_ring_buffer *ring)
  3417. {
  3418. INIT_LIST_HEAD(&ring->active_list);
  3419. INIT_LIST_HEAD(&ring->request_list);
  3420. }
  3421. void
  3422. i915_gem_load(struct drm_device *dev)
  3423. {
  3424. drm_i915_private_t *dev_priv = dev->dev_private;
  3425. int i;
  3426. dev_priv->slab =
  3427. kmem_cache_create("i915_gem_object",
  3428. sizeof(struct drm_i915_gem_object), 0,
  3429. SLAB_HWCACHE_ALIGN,
  3430. NULL);
  3431. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  3432. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  3433. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  3434. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  3435. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3436. for (i = 0; i < I915_NUM_RINGS; i++)
  3437. init_ring_lists(&dev_priv->ring[i]);
  3438. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  3439. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3440. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3441. i915_gem_retire_work_handler);
  3442. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  3443. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3444. if (IS_GEN3(dev)) {
  3445. I915_WRITE(MI_ARB_STATE,
  3446. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  3447. }
  3448. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3449. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3450. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3451. dev_priv->fence_reg_start = 3;
  3452. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  3453. dev_priv->num_fence_regs = 32;
  3454. else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3455. dev_priv->num_fence_regs = 16;
  3456. else
  3457. dev_priv->num_fence_regs = 8;
  3458. /* Initialize fence registers to zero */
  3459. i915_gem_reset_fences(dev);
  3460. i915_gem_detect_bit_6_swizzle(dev);
  3461. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3462. dev_priv->mm.interruptible = true;
  3463. dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
  3464. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  3465. register_shrinker(&dev_priv->mm.inactive_shrinker);
  3466. }
  3467. /*
  3468. * Create a physically contiguous memory object for this object
  3469. * e.g. for cursor + overlay regs
  3470. */
  3471. static int i915_gem_init_phys_object(struct drm_device *dev,
  3472. int id, int size, int align)
  3473. {
  3474. drm_i915_private_t *dev_priv = dev->dev_private;
  3475. struct drm_i915_gem_phys_object *phys_obj;
  3476. int ret;
  3477. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3478. return 0;
  3479. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3480. if (!phys_obj)
  3481. return -ENOMEM;
  3482. phys_obj->id = id;
  3483. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3484. if (!phys_obj->handle) {
  3485. ret = -ENOMEM;
  3486. goto kfree_obj;
  3487. }
  3488. #ifdef CONFIG_X86
  3489. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3490. #endif
  3491. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3492. return 0;
  3493. kfree_obj:
  3494. kfree(phys_obj);
  3495. return ret;
  3496. }
  3497. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3498. {
  3499. drm_i915_private_t *dev_priv = dev->dev_private;
  3500. struct drm_i915_gem_phys_object *phys_obj;
  3501. if (!dev_priv->mm.phys_objs[id - 1])
  3502. return;
  3503. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3504. if (phys_obj->cur_obj) {
  3505. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3506. }
  3507. #ifdef CONFIG_X86
  3508. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3509. #endif
  3510. drm_pci_free(dev, phys_obj->handle);
  3511. kfree(phys_obj);
  3512. dev_priv->mm.phys_objs[id - 1] = NULL;
  3513. }
  3514. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3515. {
  3516. int i;
  3517. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3518. i915_gem_free_phys_object(dev, i);
  3519. }
  3520. void i915_gem_detach_phys_object(struct drm_device *dev,
  3521. struct drm_i915_gem_object *obj)
  3522. {
  3523. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  3524. char *vaddr;
  3525. int i;
  3526. int page_count;
  3527. if (!obj->phys_obj)
  3528. return;
  3529. vaddr = obj->phys_obj->handle->vaddr;
  3530. page_count = obj->base.size / PAGE_SIZE;
  3531. for (i = 0; i < page_count; i++) {
  3532. struct page *page = shmem_read_mapping_page(mapping, i);
  3533. if (!IS_ERR(page)) {
  3534. char *dst = kmap_atomic(page);
  3535. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  3536. kunmap_atomic(dst);
  3537. drm_clflush_pages(&page, 1);
  3538. set_page_dirty(page);
  3539. mark_page_accessed(page);
  3540. page_cache_release(page);
  3541. }
  3542. }
  3543. i915_gem_chipset_flush(dev);
  3544. obj->phys_obj->cur_obj = NULL;
  3545. obj->phys_obj = NULL;
  3546. }
  3547. int
  3548. i915_gem_attach_phys_object(struct drm_device *dev,
  3549. struct drm_i915_gem_object *obj,
  3550. int id,
  3551. int align)
  3552. {
  3553. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  3554. drm_i915_private_t *dev_priv = dev->dev_private;
  3555. int ret = 0;
  3556. int page_count;
  3557. int i;
  3558. if (id > I915_MAX_PHYS_OBJECT)
  3559. return -EINVAL;
  3560. if (obj->phys_obj) {
  3561. if (obj->phys_obj->id == id)
  3562. return 0;
  3563. i915_gem_detach_phys_object(dev, obj);
  3564. }
  3565. /* create a new object */
  3566. if (!dev_priv->mm.phys_objs[id - 1]) {
  3567. ret = i915_gem_init_phys_object(dev, id,
  3568. obj->base.size, align);
  3569. if (ret) {
  3570. DRM_ERROR("failed to init phys object %d size: %zu\n",
  3571. id, obj->base.size);
  3572. return ret;
  3573. }
  3574. }
  3575. /* bind to the object */
  3576. obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3577. obj->phys_obj->cur_obj = obj;
  3578. page_count = obj->base.size / PAGE_SIZE;
  3579. for (i = 0; i < page_count; i++) {
  3580. struct page *page;
  3581. char *dst, *src;
  3582. page = shmem_read_mapping_page(mapping, i);
  3583. if (IS_ERR(page))
  3584. return PTR_ERR(page);
  3585. src = kmap_atomic(page);
  3586. dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3587. memcpy(dst, src, PAGE_SIZE);
  3588. kunmap_atomic(src);
  3589. mark_page_accessed(page);
  3590. page_cache_release(page);
  3591. }
  3592. return 0;
  3593. }
  3594. static int
  3595. i915_gem_phys_pwrite(struct drm_device *dev,
  3596. struct drm_i915_gem_object *obj,
  3597. struct drm_i915_gem_pwrite *args,
  3598. struct drm_file *file_priv)
  3599. {
  3600. void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
  3601. char __user *user_data = to_user_ptr(args->data_ptr);
  3602. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  3603. unsigned long unwritten;
  3604. /* The physical object once assigned is fixed for the lifetime
  3605. * of the obj, so we can safely drop the lock and continue
  3606. * to access vaddr.
  3607. */
  3608. mutex_unlock(&dev->struct_mutex);
  3609. unwritten = copy_from_user(vaddr, user_data, args->size);
  3610. mutex_lock(&dev->struct_mutex);
  3611. if (unwritten)
  3612. return -EFAULT;
  3613. }
  3614. i915_gem_chipset_flush(dev);
  3615. return 0;
  3616. }
  3617. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3618. {
  3619. struct drm_i915_file_private *file_priv = file->driver_priv;
  3620. /* Clean up our request list when the client is going away, so that
  3621. * later retire_requests won't dereference our soon-to-be-gone
  3622. * file_priv.
  3623. */
  3624. spin_lock(&file_priv->mm.lock);
  3625. while (!list_empty(&file_priv->mm.request_list)) {
  3626. struct drm_i915_gem_request *request;
  3627. request = list_first_entry(&file_priv->mm.request_list,
  3628. struct drm_i915_gem_request,
  3629. client_list);
  3630. list_del(&request->client_list);
  3631. request->file_priv = NULL;
  3632. }
  3633. spin_unlock(&file_priv->mm.lock);
  3634. }
  3635. static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  3636. {
  3637. if (!mutex_is_locked(mutex))
  3638. return false;
  3639. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  3640. return mutex->owner == task;
  3641. #else
  3642. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  3643. return false;
  3644. #endif
  3645. }
  3646. static int
  3647. i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
  3648. {
  3649. struct drm_i915_private *dev_priv =
  3650. container_of(shrinker,
  3651. struct drm_i915_private,
  3652. mm.inactive_shrinker);
  3653. struct drm_device *dev = dev_priv->dev;
  3654. struct drm_i915_gem_object *obj;
  3655. int nr_to_scan = sc->nr_to_scan;
  3656. bool unlock = true;
  3657. int cnt;
  3658. if (!mutex_trylock(&dev->struct_mutex)) {
  3659. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  3660. return 0;
  3661. if (dev_priv->mm.shrinker_no_lock_stealing)
  3662. return 0;
  3663. unlock = false;
  3664. }
  3665. if (nr_to_scan) {
  3666. nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
  3667. if (nr_to_scan > 0)
  3668. nr_to_scan -= __i915_gem_shrink(dev_priv, nr_to_scan,
  3669. false);
  3670. if (nr_to_scan > 0)
  3671. i915_gem_shrink_all(dev_priv);
  3672. }
  3673. cnt = 0;
  3674. list_for_each_entry(obj, &dev_priv->mm.unbound_list, gtt_list)
  3675. if (obj->pages_pin_count == 0)
  3676. cnt += obj->base.size >> PAGE_SHIFT;
  3677. list_for_each_entry(obj, &dev_priv->mm.inactive_list, gtt_list)
  3678. if (obj->pin_count == 0 && obj->pages_pin_count == 0)
  3679. cnt += obj->base.size >> PAGE_SHIFT;
  3680. if (unlock)
  3681. mutex_unlock(&dev->struct_mutex);
  3682. return cnt;
  3683. }