i915_gem.c 136 KB

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