i915_gem.c 117 KB

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