i915_gem.c 134 KB

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