i915_gem.c 129 KB

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