i915_gem.c 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869
  1. /*
  2. * Copyright © 2008-2015 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_gem_clflush.h"
  32. #include "i915_vgpu.h"
  33. #include "i915_trace.h"
  34. #include "intel_drv.h"
  35. #include "intel_frontbuffer.h"
  36. #include "intel_mocs.h"
  37. #include "i915_gemfs.h"
  38. #include <linux/dma-fence-array.h>
  39. #include <linux/kthread.h>
  40. #include <linux/reservation.h>
  41. #include <linux/shmem_fs.h>
  42. #include <linux/slab.h>
  43. #include <linux/stop_machine.h>
  44. #include <linux/swap.h>
  45. #include <linux/pci.h>
  46. #include <linux/dma-buf.h>
  47. static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
  48. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  49. {
  50. if (obj->cache_dirty)
  51. return false;
  52. if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
  53. return true;
  54. return obj->pin_global; /* currently in use by HW, keep flushed */
  55. }
  56. static int
  57. insert_mappable_node(struct i915_ggtt *ggtt,
  58. struct drm_mm_node *node, u32 size)
  59. {
  60. memset(node, 0, sizeof(*node));
  61. return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
  62. size, 0, I915_COLOR_UNEVICTABLE,
  63. 0, ggtt->mappable_end,
  64. DRM_MM_INSERT_LOW);
  65. }
  66. static void
  67. remove_mappable_node(struct drm_mm_node *node)
  68. {
  69. drm_mm_remove_node(node);
  70. }
  71. /* some bookkeeping */
  72. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  73. u64 size)
  74. {
  75. spin_lock(&dev_priv->mm.object_stat_lock);
  76. dev_priv->mm.object_count++;
  77. dev_priv->mm.object_memory += size;
  78. spin_unlock(&dev_priv->mm.object_stat_lock);
  79. }
  80. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  81. u64 size)
  82. {
  83. spin_lock(&dev_priv->mm.object_stat_lock);
  84. dev_priv->mm.object_count--;
  85. dev_priv->mm.object_memory -= size;
  86. spin_unlock(&dev_priv->mm.object_stat_lock);
  87. }
  88. static int
  89. i915_gem_wait_for_error(struct i915_gpu_error *error)
  90. {
  91. int ret;
  92. might_sleep();
  93. /*
  94. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  95. * userspace. If it takes that long something really bad is going on and
  96. * we should simply try to bail out and fail as gracefully as possible.
  97. */
  98. ret = wait_event_interruptible_timeout(error->reset_queue,
  99. !i915_reset_backoff(error),
  100. I915_RESET_TIMEOUT);
  101. if (ret == 0) {
  102. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  103. return -EIO;
  104. } else if (ret < 0) {
  105. return ret;
  106. } else {
  107. return 0;
  108. }
  109. }
  110. int i915_mutex_lock_interruptible(struct drm_device *dev)
  111. {
  112. struct drm_i915_private *dev_priv = to_i915(dev);
  113. int ret;
  114. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  115. if (ret)
  116. return ret;
  117. ret = mutex_lock_interruptible(&dev->struct_mutex);
  118. if (ret)
  119. return ret;
  120. return 0;
  121. }
  122. int
  123. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  124. struct drm_file *file)
  125. {
  126. struct drm_i915_private *dev_priv = to_i915(dev);
  127. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  128. struct drm_i915_gem_get_aperture *args = data;
  129. struct i915_vma *vma;
  130. u64 pinned;
  131. pinned = ggtt->base.reserved;
  132. mutex_lock(&dev->struct_mutex);
  133. list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
  134. if (i915_vma_is_pinned(vma))
  135. pinned += vma->node.size;
  136. list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
  137. if (i915_vma_is_pinned(vma))
  138. pinned += vma->node.size;
  139. mutex_unlock(&dev->struct_mutex);
  140. args->aper_size = ggtt->base.total;
  141. args->aper_available_size = args->aper_size - pinned;
  142. return 0;
  143. }
  144. static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  145. {
  146. struct address_space *mapping = obj->base.filp->f_mapping;
  147. drm_dma_handle_t *phys;
  148. struct sg_table *st;
  149. struct scatterlist *sg;
  150. char *vaddr;
  151. int i;
  152. int err;
  153. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  154. return -EINVAL;
  155. /* Always aligning to the object size, allows a single allocation
  156. * to handle all possible callers, and given typical object sizes,
  157. * the alignment of the buddy allocation will naturally match.
  158. */
  159. phys = drm_pci_alloc(obj->base.dev,
  160. roundup_pow_of_two(obj->base.size),
  161. roundup_pow_of_two(obj->base.size));
  162. if (!phys)
  163. return -ENOMEM;
  164. vaddr = phys->vaddr;
  165. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  166. struct page *page;
  167. char *src;
  168. page = shmem_read_mapping_page(mapping, i);
  169. if (IS_ERR(page)) {
  170. err = PTR_ERR(page);
  171. goto err_phys;
  172. }
  173. src = kmap_atomic(page);
  174. memcpy(vaddr, src, PAGE_SIZE);
  175. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  176. kunmap_atomic(src);
  177. put_page(page);
  178. vaddr += PAGE_SIZE;
  179. }
  180. i915_gem_chipset_flush(to_i915(obj->base.dev));
  181. st = kmalloc(sizeof(*st), GFP_KERNEL);
  182. if (!st) {
  183. err = -ENOMEM;
  184. goto err_phys;
  185. }
  186. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  187. kfree(st);
  188. err = -ENOMEM;
  189. goto err_phys;
  190. }
  191. sg = st->sgl;
  192. sg->offset = 0;
  193. sg->length = obj->base.size;
  194. sg_dma_address(sg) = phys->busaddr;
  195. sg_dma_len(sg) = obj->base.size;
  196. obj->phys_handle = phys;
  197. __i915_gem_object_set_pages(obj, st, sg->length);
  198. return 0;
  199. err_phys:
  200. drm_pci_free(obj->base.dev, phys);
  201. return err;
  202. }
  203. static void __start_cpu_write(struct drm_i915_gem_object *obj)
  204. {
  205. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  206. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  207. if (cpu_write_needs_clflush(obj))
  208. obj->cache_dirty = true;
  209. }
  210. static void
  211. __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
  212. struct sg_table *pages,
  213. bool needs_clflush)
  214. {
  215. GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
  216. if (obj->mm.madv == I915_MADV_DONTNEED)
  217. obj->mm.dirty = false;
  218. if (needs_clflush &&
  219. (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
  220. !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
  221. drm_clflush_sg(pages);
  222. __start_cpu_write(obj);
  223. }
  224. static void
  225. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
  226. struct sg_table *pages)
  227. {
  228. __i915_gem_object_release_shmem(obj, pages, false);
  229. if (obj->mm.dirty) {
  230. struct address_space *mapping = obj->base.filp->f_mapping;
  231. char *vaddr = obj->phys_handle->vaddr;
  232. int i;
  233. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  234. struct page *page;
  235. char *dst;
  236. page = shmem_read_mapping_page(mapping, i);
  237. if (IS_ERR(page))
  238. continue;
  239. dst = kmap_atomic(page);
  240. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  241. memcpy(dst, vaddr, PAGE_SIZE);
  242. kunmap_atomic(dst);
  243. set_page_dirty(page);
  244. if (obj->mm.madv == I915_MADV_WILLNEED)
  245. mark_page_accessed(page);
  246. put_page(page);
  247. vaddr += PAGE_SIZE;
  248. }
  249. obj->mm.dirty = false;
  250. }
  251. sg_free_table(pages);
  252. kfree(pages);
  253. drm_pci_free(obj->base.dev, obj->phys_handle);
  254. }
  255. static void
  256. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  257. {
  258. i915_gem_object_unpin_pages(obj);
  259. }
  260. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  261. .get_pages = i915_gem_object_get_pages_phys,
  262. .put_pages = i915_gem_object_put_pages_phys,
  263. .release = i915_gem_object_release_phys,
  264. };
  265. static const struct drm_i915_gem_object_ops i915_gem_object_ops;
  266. int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  267. {
  268. struct i915_vma *vma;
  269. LIST_HEAD(still_in_list);
  270. int ret;
  271. lockdep_assert_held(&obj->base.dev->struct_mutex);
  272. /* Closed vma are removed from the obj->vma_list - but they may
  273. * still have an active binding on the object. To remove those we
  274. * must wait for all rendering to complete to the object (as unbinding
  275. * must anyway), and retire the requests.
  276. */
  277. ret = i915_gem_object_set_to_cpu_domain(obj, false);
  278. if (ret)
  279. return ret;
  280. while ((vma = list_first_entry_or_null(&obj->vma_list,
  281. struct i915_vma,
  282. obj_link))) {
  283. list_move_tail(&vma->obj_link, &still_in_list);
  284. ret = i915_vma_unbind(vma);
  285. if (ret)
  286. break;
  287. }
  288. list_splice(&still_in_list, &obj->vma_list);
  289. return ret;
  290. }
  291. static long
  292. i915_gem_object_wait_fence(struct dma_fence *fence,
  293. unsigned int flags,
  294. long timeout,
  295. struct intel_rps_client *rps_client)
  296. {
  297. struct drm_i915_gem_request *rq;
  298. BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
  299. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  300. return timeout;
  301. if (!dma_fence_is_i915(fence))
  302. return dma_fence_wait_timeout(fence,
  303. flags & I915_WAIT_INTERRUPTIBLE,
  304. timeout);
  305. rq = to_request(fence);
  306. if (i915_gem_request_completed(rq))
  307. goto out;
  308. /* This client is about to stall waiting for the GPU. In many cases
  309. * this is undesirable and limits the throughput of the system, as
  310. * many clients cannot continue processing user input/output whilst
  311. * blocked. RPS autotuning may take tens of milliseconds to respond
  312. * to the GPU load and thus incurs additional latency for the client.
  313. * We can circumvent that by promoting the GPU frequency to maximum
  314. * before we wait. This makes the GPU throttle up much more quickly
  315. * (good for benchmarks and user experience, e.g. window animations),
  316. * but at a cost of spending more power processing the workload
  317. * (bad for battery). Not all clients even want their results
  318. * immediately and for them we should just let the GPU select its own
  319. * frequency to maximise efficiency. To prevent a single client from
  320. * forcing the clocks too high for the whole system, we only allow
  321. * each client to waitboost once in a busy period.
  322. */
  323. if (rps_client) {
  324. if (INTEL_GEN(rq->i915) >= 6)
  325. gen6_rps_boost(rq, rps_client);
  326. else
  327. rps_client = NULL;
  328. }
  329. timeout = i915_wait_request(rq, flags, timeout);
  330. out:
  331. if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
  332. i915_gem_request_retire_upto(rq);
  333. return timeout;
  334. }
  335. static long
  336. i915_gem_object_wait_reservation(struct reservation_object *resv,
  337. unsigned int flags,
  338. long timeout,
  339. struct intel_rps_client *rps_client)
  340. {
  341. unsigned int seq = __read_seqcount_begin(&resv->seq);
  342. struct dma_fence *excl;
  343. bool prune_fences = false;
  344. if (flags & I915_WAIT_ALL) {
  345. struct dma_fence **shared;
  346. unsigned int count, i;
  347. int ret;
  348. ret = reservation_object_get_fences_rcu(resv,
  349. &excl, &count, &shared);
  350. if (ret)
  351. return ret;
  352. for (i = 0; i < count; i++) {
  353. timeout = i915_gem_object_wait_fence(shared[i],
  354. flags, timeout,
  355. rps_client);
  356. if (timeout < 0)
  357. break;
  358. dma_fence_put(shared[i]);
  359. }
  360. for (; i < count; i++)
  361. dma_fence_put(shared[i]);
  362. kfree(shared);
  363. prune_fences = count && timeout >= 0;
  364. } else {
  365. excl = reservation_object_get_excl_rcu(resv);
  366. }
  367. if (excl && timeout >= 0) {
  368. timeout = i915_gem_object_wait_fence(excl, flags, timeout,
  369. rps_client);
  370. prune_fences = timeout >= 0;
  371. }
  372. dma_fence_put(excl);
  373. /* Oportunistically prune the fences iff we know they have *all* been
  374. * signaled and that the reservation object has not been changed (i.e.
  375. * no new fences have been added).
  376. */
  377. if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
  378. if (reservation_object_trylock(resv)) {
  379. if (!__read_seqcount_retry(&resv->seq, seq))
  380. reservation_object_add_excl_fence(resv, NULL);
  381. reservation_object_unlock(resv);
  382. }
  383. }
  384. return timeout;
  385. }
  386. static void __fence_set_priority(struct dma_fence *fence, int prio)
  387. {
  388. struct drm_i915_gem_request *rq;
  389. struct intel_engine_cs *engine;
  390. if (!dma_fence_is_i915(fence))
  391. return;
  392. rq = to_request(fence);
  393. engine = rq->engine;
  394. if (!engine->schedule)
  395. return;
  396. engine->schedule(rq, prio);
  397. }
  398. static void fence_set_priority(struct dma_fence *fence, int prio)
  399. {
  400. /* Recurse once into a fence-array */
  401. if (dma_fence_is_array(fence)) {
  402. struct dma_fence_array *array = to_dma_fence_array(fence);
  403. int i;
  404. for (i = 0; i < array->num_fences; i++)
  405. __fence_set_priority(array->fences[i], prio);
  406. } else {
  407. __fence_set_priority(fence, prio);
  408. }
  409. }
  410. int
  411. i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
  412. unsigned int flags,
  413. int prio)
  414. {
  415. struct dma_fence *excl;
  416. if (flags & I915_WAIT_ALL) {
  417. struct dma_fence **shared;
  418. unsigned int count, i;
  419. int ret;
  420. ret = reservation_object_get_fences_rcu(obj->resv,
  421. &excl, &count, &shared);
  422. if (ret)
  423. return ret;
  424. for (i = 0; i < count; i++) {
  425. fence_set_priority(shared[i], prio);
  426. dma_fence_put(shared[i]);
  427. }
  428. kfree(shared);
  429. } else {
  430. excl = reservation_object_get_excl_rcu(obj->resv);
  431. }
  432. if (excl) {
  433. fence_set_priority(excl, prio);
  434. dma_fence_put(excl);
  435. }
  436. return 0;
  437. }
  438. /**
  439. * Waits for rendering to the object to be completed
  440. * @obj: i915 gem object
  441. * @flags: how to wait (under a lock, for all rendering or just for writes etc)
  442. * @timeout: how long to wait
  443. * @rps_client: client (user process) to charge for any waitboosting
  444. */
  445. int
  446. i915_gem_object_wait(struct drm_i915_gem_object *obj,
  447. unsigned int flags,
  448. long timeout,
  449. struct intel_rps_client *rps_client)
  450. {
  451. might_sleep();
  452. #if IS_ENABLED(CONFIG_LOCKDEP)
  453. GEM_BUG_ON(debug_locks &&
  454. !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
  455. !!(flags & I915_WAIT_LOCKED));
  456. #endif
  457. GEM_BUG_ON(timeout < 0);
  458. timeout = i915_gem_object_wait_reservation(obj->resv,
  459. flags, timeout,
  460. rps_client);
  461. return timeout < 0 ? timeout : 0;
  462. }
  463. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  464. {
  465. struct drm_i915_file_private *fpriv = file->driver_priv;
  466. return &fpriv->rps_client;
  467. }
  468. static int
  469. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  470. struct drm_i915_gem_pwrite *args,
  471. struct drm_file *file)
  472. {
  473. void *vaddr = obj->phys_handle->vaddr + args->offset;
  474. char __user *user_data = u64_to_user_ptr(args->data_ptr);
  475. /* We manually control the domain here and pretend that it
  476. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  477. */
  478. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  479. if (copy_from_user(vaddr, user_data, args->size))
  480. return -EFAULT;
  481. drm_clflush_virt_range(vaddr, args->size);
  482. i915_gem_chipset_flush(to_i915(obj->base.dev));
  483. intel_fb_obj_flush(obj, ORIGIN_CPU);
  484. return 0;
  485. }
  486. void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
  487. {
  488. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  489. }
  490. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  491. {
  492. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  493. kmem_cache_free(dev_priv->objects, obj);
  494. }
  495. static int
  496. i915_gem_create(struct drm_file *file,
  497. struct drm_i915_private *dev_priv,
  498. uint64_t size,
  499. uint32_t *handle_p)
  500. {
  501. struct drm_i915_gem_object *obj;
  502. int ret;
  503. u32 handle;
  504. size = roundup(size, PAGE_SIZE);
  505. if (size == 0)
  506. return -EINVAL;
  507. /* Allocate the new object */
  508. obj = i915_gem_object_create(dev_priv, size);
  509. if (IS_ERR(obj))
  510. return PTR_ERR(obj);
  511. ret = drm_gem_handle_create(file, &obj->base, &handle);
  512. /* drop reference from allocate - handle holds it now */
  513. i915_gem_object_put(obj);
  514. if (ret)
  515. return ret;
  516. *handle_p = handle;
  517. return 0;
  518. }
  519. int
  520. i915_gem_dumb_create(struct drm_file *file,
  521. struct drm_device *dev,
  522. struct drm_mode_create_dumb *args)
  523. {
  524. /* have to work out size/pitch and return them */
  525. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  526. args->size = args->pitch * args->height;
  527. return i915_gem_create(file, to_i915(dev),
  528. args->size, &args->handle);
  529. }
  530. static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  531. {
  532. return !(obj->cache_level == I915_CACHE_NONE ||
  533. obj->cache_level == I915_CACHE_WT);
  534. }
  535. /**
  536. * Creates a new mm object and returns a handle to it.
  537. * @dev: drm device pointer
  538. * @data: ioctl data blob
  539. * @file: drm file pointer
  540. */
  541. int
  542. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  543. struct drm_file *file)
  544. {
  545. struct drm_i915_private *dev_priv = to_i915(dev);
  546. struct drm_i915_gem_create *args = data;
  547. i915_gem_flush_free_objects(dev_priv);
  548. return i915_gem_create(file, dev_priv,
  549. args->size, &args->handle);
  550. }
  551. static inline enum fb_op_origin
  552. fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
  553. {
  554. return (domain == I915_GEM_DOMAIN_GTT ?
  555. obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
  556. }
  557. void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
  558. {
  559. /*
  560. * No actual flushing is required for the GTT write domain for reads
  561. * from the GTT domain. Writes to it "immediately" go to main memory
  562. * as far as we know, so there's no chipset flush. It also doesn't
  563. * land in the GPU render cache.
  564. *
  565. * However, we do have to enforce the order so that all writes through
  566. * the GTT land before any writes to the device, such as updates to
  567. * the GATT itself.
  568. *
  569. * We also have to wait a bit for the writes to land from the GTT.
  570. * An uncached read (i.e. mmio) seems to be ideal for the round-trip
  571. * timing. This issue has only been observed when switching quickly
  572. * between GTT writes and CPU reads from inside the kernel on recent hw,
  573. * and it appears to only affect discrete GTT blocks (i.e. on LLC
  574. * system agents we cannot reproduce this behaviour, until Cannonlake
  575. * that was!).
  576. */
  577. wmb();
  578. intel_runtime_pm_get(dev_priv);
  579. spin_lock_irq(&dev_priv->uncore.lock);
  580. POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
  581. spin_unlock_irq(&dev_priv->uncore.lock);
  582. intel_runtime_pm_put(dev_priv);
  583. }
  584. static void
  585. flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
  586. {
  587. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  588. struct i915_vma *vma;
  589. if (!(obj->base.write_domain & flush_domains))
  590. return;
  591. switch (obj->base.write_domain) {
  592. case I915_GEM_DOMAIN_GTT:
  593. i915_gem_flush_ggtt_writes(dev_priv);
  594. intel_fb_obj_flush(obj,
  595. fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
  596. for_each_ggtt_vma(vma, obj) {
  597. if (vma->iomap)
  598. continue;
  599. i915_vma_unset_ggtt_write(vma);
  600. }
  601. break;
  602. case I915_GEM_DOMAIN_CPU:
  603. i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
  604. break;
  605. case I915_GEM_DOMAIN_RENDER:
  606. if (gpu_write_needs_clflush(obj))
  607. obj->cache_dirty = true;
  608. break;
  609. }
  610. obj->base.write_domain = 0;
  611. }
  612. static inline int
  613. __copy_to_user_swizzled(char __user *cpu_vaddr,
  614. const char *gpu_vaddr, int gpu_offset,
  615. int length)
  616. {
  617. int ret, cpu_offset = 0;
  618. while (length > 0) {
  619. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  620. int this_length = min(cacheline_end - gpu_offset, length);
  621. int swizzled_gpu_offset = gpu_offset ^ 64;
  622. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  623. gpu_vaddr + swizzled_gpu_offset,
  624. this_length);
  625. if (ret)
  626. return ret + length;
  627. cpu_offset += this_length;
  628. gpu_offset += this_length;
  629. length -= this_length;
  630. }
  631. return 0;
  632. }
  633. static inline int
  634. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  635. const char __user *cpu_vaddr,
  636. int length)
  637. {
  638. int ret, cpu_offset = 0;
  639. while (length > 0) {
  640. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  641. int this_length = min(cacheline_end - gpu_offset, length);
  642. int swizzled_gpu_offset = gpu_offset ^ 64;
  643. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  644. cpu_vaddr + cpu_offset,
  645. this_length);
  646. if (ret)
  647. return ret + length;
  648. cpu_offset += this_length;
  649. gpu_offset += this_length;
  650. length -= this_length;
  651. }
  652. return 0;
  653. }
  654. /*
  655. * Pins the specified object's pages and synchronizes the object with
  656. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  657. * flush the object from the CPU cache.
  658. */
  659. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  660. unsigned int *needs_clflush)
  661. {
  662. int ret;
  663. lockdep_assert_held(&obj->base.dev->struct_mutex);
  664. *needs_clflush = 0;
  665. if (!i915_gem_object_has_struct_page(obj))
  666. return -ENODEV;
  667. ret = i915_gem_object_wait(obj,
  668. I915_WAIT_INTERRUPTIBLE |
  669. I915_WAIT_LOCKED,
  670. MAX_SCHEDULE_TIMEOUT,
  671. NULL);
  672. if (ret)
  673. return ret;
  674. ret = i915_gem_object_pin_pages(obj);
  675. if (ret)
  676. return ret;
  677. if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
  678. !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  679. ret = i915_gem_object_set_to_cpu_domain(obj, false);
  680. if (ret)
  681. goto err_unpin;
  682. else
  683. goto out;
  684. }
  685. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  686. /* If we're not in the cpu read domain, set ourself into the gtt
  687. * read domain and manually flush cachelines (if required). This
  688. * optimizes for the case when the gpu will dirty the data
  689. * anyway again before the next pread happens.
  690. */
  691. if (!obj->cache_dirty &&
  692. !(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  693. *needs_clflush = CLFLUSH_BEFORE;
  694. out:
  695. /* return with the pages pinned */
  696. return 0;
  697. err_unpin:
  698. i915_gem_object_unpin_pages(obj);
  699. return ret;
  700. }
  701. int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
  702. unsigned int *needs_clflush)
  703. {
  704. int ret;
  705. lockdep_assert_held(&obj->base.dev->struct_mutex);
  706. *needs_clflush = 0;
  707. if (!i915_gem_object_has_struct_page(obj))
  708. return -ENODEV;
  709. ret = i915_gem_object_wait(obj,
  710. I915_WAIT_INTERRUPTIBLE |
  711. I915_WAIT_LOCKED |
  712. I915_WAIT_ALL,
  713. MAX_SCHEDULE_TIMEOUT,
  714. NULL);
  715. if (ret)
  716. return ret;
  717. ret = i915_gem_object_pin_pages(obj);
  718. if (ret)
  719. return ret;
  720. if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
  721. !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  722. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  723. if (ret)
  724. goto err_unpin;
  725. else
  726. goto out;
  727. }
  728. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  729. /* If we're not in the cpu write domain, set ourself into the
  730. * gtt write domain and manually flush cachelines (as required).
  731. * This optimizes for the case when the gpu will use the data
  732. * right away and we therefore have to clflush anyway.
  733. */
  734. if (!obj->cache_dirty) {
  735. *needs_clflush |= CLFLUSH_AFTER;
  736. /*
  737. * Same trick applies to invalidate partially written
  738. * cachelines read before writing.
  739. */
  740. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  741. *needs_clflush |= CLFLUSH_BEFORE;
  742. }
  743. out:
  744. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  745. obj->mm.dirty = true;
  746. /* return with the pages pinned */
  747. return 0;
  748. err_unpin:
  749. i915_gem_object_unpin_pages(obj);
  750. return ret;
  751. }
  752. static void
  753. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  754. bool swizzled)
  755. {
  756. if (unlikely(swizzled)) {
  757. unsigned long start = (unsigned long) addr;
  758. unsigned long end = (unsigned long) addr + length;
  759. /* For swizzling simply ensure that we always flush both
  760. * channels. Lame, but simple and it works. Swizzled
  761. * pwrite/pread is far from a hotpath - current userspace
  762. * doesn't use it at all. */
  763. start = round_down(start, 128);
  764. end = round_up(end, 128);
  765. drm_clflush_virt_range((void *)start, end - start);
  766. } else {
  767. drm_clflush_virt_range(addr, length);
  768. }
  769. }
  770. /* Only difference to the fast-path function is that this can handle bit17
  771. * and uses non-atomic copy and kmap functions. */
  772. static int
  773. shmem_pread_slow(struct page *page, int offset, int length,
  774. char __user *user_data,
  775. bool page_do_bit17_swizzling, bool needs_clflush)
  776. {
  777. char *vaddr;
  778. int ret;
  779. vaddr = kmap(page);
  780. if (needs_clflush)
  781. shmem_clflush_swizzled_range(vaddr + offset, length,
  782. page_do_bit17_swizzling);
  783. if (page_do_bit17_swizzling)
  784. ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
  785. else
  786. ret = __copy_to_user(user_data, vaddr + offset, length);
  787. kunmap(page);
  788. return ret ? - EFAULT : 0;
  789. }
  790. static int
  791. shmem_pread(struct page *page, int offset, int length, char __user *user_data,
  792. bool page_do_bit17_swizzling, bool needs_clflush)
  793. {
  794. int ret;
  795. ret = -ENODEV;
  796. if (!page_do_bit17_swizzling) {
  797. char *vaddr = kmap_atomic(page);
  798. if (needs_clflush)
  799. drm_clflush_virt_range(vaddr + offset, length);
  800. ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
  801. kunmap_atomic(vaddr);
  802. }
  803. if (ret == 0)
  804. return 0;
  805. return shmem_pread_slow(page, offset, length, user_data,
  806. page_do_bit17_swizzling, needs_clflush);
  807. }
  808. static int
  809. i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
  810. struct drm_i915_gem_pread *args)
  811. {
  812. char __user *user_data;
  813. u64 remain;
  814. unsigned int obj_do_bit17_swizzling;
  815. unsigned int needs_clflush;
  816. unsigned int idx, offset;
  817. int ret;
  818. obj_do_bit17_swizzling = 0;
  819. if (i915_gem_object_needs_bit17_swizzle(obj))
  820. obj_do_bit17_swizzling = BIT(17);
  821. ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
  822. if (ret)
  823. return ret;
  824. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  825. mutex_unlock(&obj->base.dev->struct_mutex);
  826. if (ret)
  827. return ret;
  828. remain = args->size;
  829. user_data = u64_to_user_ptr(args->data_ptr);
  830. offset = offset_in_page(args->offset);
  831. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  832. struct page *page = i915_gem_object_get_page(obj, idx);
  833. int length;
  834. length = remain;
  835. if (offset + length > PAGE_SIZE)
  836. length = PAGE_SIZE - offset;
  837. ret = shmem_pread(page, offset, length, user_data,
  838. page_to_phys(page) & obj_do_bit17_swizzling,
  839. needs_clflush);
  840. if (ret)
  841. break;
  842. remain -= length;
  843. user_data += length;
  844. offset = 0;
  845. }
  846. i915_gem_obj_finish_shmem_access(obj);
  847. return ret;
  848. }
  849. static inline bool
  850. gtt_user_read(struct io_mapping *mapping,
  851. loff_t base, int offset,
  852. char __user *user_data, int length)
  853. {
  854. void __iomem *vaddr;
  855. unsigned long unwritten;
  856. /* We can use the cpu mem copy function because this is X86. */
  857. vaddr = io_mapping_map_atomic_wc(mapping, base);
  858. unwritten = __copy_to_user_inatomic(user_data,
  859. (void __force *)vaddr + offset,
  860. length);
  861. io_mapping_unmap_atomic(vaddr);
  862. if (unwritten) {
  863. vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
  864. unwritten = copy_to_user(user_data,
  865. (void __force *)vaddr + offset,
  866. length);
  867. io_mapping_unmap(vaddr);
  868. }
  869. return unwritten;
  870. }
  871. static int
  872. i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
  873. const struct drm_i915_gem_pread *args)
  874. {
  875. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  876. struct i915_ggtt *ggtt = &i915->ggtt;
  877. struct drm_mm_node node;
  878. struct i915_vma *vma;
  879. void __user *user_data;
  880. u64 remain, offset;
  881. int ret;
  882. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  883. if (ret)
  884. return ret;
  885. intel_runtime_pm_get(i915);
  886. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  887. PIN_MAPPABLE |
  888. PIN_NONFAULT |
  889. PIN_NONBLOCK);
  890. if (!IS_ERR(vma)) {
  891. node.start = i915_ggtt_offset(vma);
  892. node.allocated = false;
  893. ret = i915_vma_put_fence(vma);
  894. if (ret) {
  895. i915_vma_unpin(vma);
  896. vma = ERR_PTR(ret);
  897. }
  898. }
  899. if (IS_ERR(vma)) {
  900. ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
  901. if (ret)
  902. goto out_unlock;
  903. GEM_BUG_ON(!node.allocated);
  904. }
  905. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  906. if (ret)
  907. goto out_unpin;
  908. mutex_unlock(&i915->drm.struct_mutex);
  909. user_data = u64_to_user_ptr(args->data_ptr);
  910. remain = args->size;
  911. offset = args->offset;
  912. while (remain > 0) {
  913. /* Operation in this page
  914. *
  915. * page_base = page offset within aperture
  916. * page_offset = offset within page
  917. * page_length = bytes to copy for this page
  918. */
  919. u32 page_base = node.start;
  920. unsigned page_offset = offset_in_page(offset);
  921. unsigned page_length = PAGE_SIZE - page_offset;
  922. page_length = remain < page_length ? remain : page_length;
  923. if (node.allocated) {
  924. wmb();
  925. ggtt->base.insert_page(&ggtt->base,
  926. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  927. node.start, I915_CACHE_NONE, 0);
  928. wmb();
  929. } else {
  930. page_base += offset & PAGE_MASK;
  931. }
  932. if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
  933. user_data, page_length)) {
  934. ret = -EFAULT;
  935. break;
  936. }
  937. remain -= page_length;
  938. user_data += page_length;
  939. offset += page_length;
  940. }
  941. mutex_lock(&i915->drm.struct_mutex);
  942. out_unpin:
  943. if (node.allocated) {
  944. wmb();
  945. ggtt->base.clear_range(&ggtt->base,
  946. node.start, node.size);
  947. remove_mappable_node(&node);
  948. } else {
  949. i915_vma_unpin(vma);
  950. }
  951. out_unlock:
  952. intel_runtime_pm_put(i915);
  953. mutex_unlock(&i915->drm.struct_mutex);
  954. return ret;
  955. }
  956. /**
  957. * Reads data from the object referenced by handle.
  958. * @dev: drm device pointer
  959. * @data: ioctl data blob
  960. * @file: drm file pointer
  961. *
  962. * On error, the contents of *data are undefined.
  963. */
  964. int
  965. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  966. struct drm_file *file)
  967. {
  968. struct drm_i915_gem_pread *args = data;
  969. struct drm_i915_gem_object *obj;
  970. int ret;
  971. if (args->size == 0)
  972. return 0;
  973. if (!access_ok(VERIFY_WRITE,
  974. u64_to_user_ptr(args->data_ptr),
  975. args->size))
  976. return -EFAULT;
  977. obj = i915_gem_object_lookup(file, args->handle);
  978. if (!obj)
  979. return -ENOENT;
  980. /* Bounds check source. */
  981. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  982. ret = -EINVAL;
  983. goto out;
  984. }
  985. trace_i915_gem_object_pread(obj, args->offset, args->size);
  986. ret = i915_gem_object_wait(obj,
  987. I915_WAIT_INTERRUPTIBLE,
  988. MAX_SCHEDULE_TIMEOUT,
  989. to_rps_client(file));
  990. if (ret)
  991. goto out;
  992. ret = i915_gem_object_pin_pages(obj);
  993. if (ret)
  994. goto out;
  995. ret = i915_gem_shmem_pread(obj, args);
  996. if (ret == -EFAULT || ret == -ENODEV)
  997. ret = i915_gem_gtt_pread(obj, args);
  998. i915_gem_object_unpin_pages(obj);
  999. out:
  1000. i915_gem_object_put(obj);
  1001. return ret;
  1002. }
  1003. /* This is the fast write path which cannot handle
  1004. * page faults in the source data
  1005. */
  1006. static inline bool
  1007. ggtt_write(struct io_mapping *mapping,
  1008. loff_t base, int offset,
  1009. char __user *user_data, int length)
  1010. {
  1011. void __iomem *vaddr;
  1012. unsigned long unwritten;
  1013. /* We can use the cpu mem copy function because this is X86. */
  1014. vaddr = io_mapping_map_atomic_wc(mapping, base);
  1015. unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
  1016. user_data, length);
  1017. io_mapping_unmap_atomic(vaddr);
  1018. if (unwritten) {
  1019. vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
  1020. unwritten = copy_from_user((void __force *)vaddr + offset,
  1021. user_data, length);
  1022. io_mapping_unmap(vaddr);
  1023. }
  1024. return unwritten;
  1025. }
  1026. /**
  1027. * This is the fast pwrite path, where we copy the data directly from the
  1028. * user into the GTT, uncached.
  1029. * @obj: i915 GEM object
  1030. * @args: pwrite arguments structure
  1031. */
  1032. static int
  1033. i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
  1034. const struct drm_i915_gem_pwrite *args)
  1035. {
  1036. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1037. struct i915_ggtt *ggtt = &i915->ggtt;
  1038. struct drm_mm_node node;
  1039. struct i915_vma *vma;
  1040. u64 remain, offset;
  1041. void __user *user_data;
  1042. int ret;
  1043. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  1044. if (ret)
  1045. return ret;
  1046. if (i915_gem_object_has_struct_page(obj)) {
  1047. /*
  1048. * Avoid waking the device up if we can fallback, as
  1049. * waking/resuming is very slow (worst-case 10-100 ms
  1050. * depending on PCI sleeps and our own resume time).
  1051. * This easily dwarfs any performance advantage from
  1052. * using the cache bypass of indirect GGTT access.
  1053. */
  1054. if (!intel_runtime_pm_get_if_in_use(i915)) {
  1055. ret = -EFAULT;
  1056. goto out_unlock;
  1057. }
  1058. } else {
  1059. /* No backing pages, no fallback, we must force GGTT access */
  1060. intel_runtime_pm_get(i915);
  1061. }
  1062. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  1063. PIN_MAPPABLE |
  1064. PIN_NONFAULT |
  1065. PIN_NONBLOCK);
  1066. if (!IS_ERR(vma)) {
  1067. node.start = i915_ggtt_offset(vma);
  1068. node.allocated = false;
  1069. ret = i915_vma_put_fence(vma);
  1070. if (ret) {
  1071. i915_vma_unpin(vma);
  1072. vma = ERR_PTR(ret);
  1073. }
  1074. }
  1075. if (IS_ERR(vma)) {
  1076. ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
  1077. if (ret)
  1078. goto out_rpm;
  1079. GEM_BUG_ON(!node.allocated);
  1080. }
  1081. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  1082. if (ret)
  1083. goto out_unpin;
  1084. mutex_unlock(&i915->drm.struct_mutex);
  1085. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  1086. user_data = u64_to_user_ptr(args->data_ptr);
  1087. offset = args->offset;
  1088. remain = args->size;
  1089. while (remain) {
  1090. /* Operation in this page
  1091. *
  1092. * page_base = page offset within aperture
  1093. * page_offset = offset within page
  1094. * page_length = bytes to copy for this page
  1095. */
  1096. u32 page_base = node.start;
  1097. unsigned int page_offset = offset_in_page(offset);
  1098. unsigned int page_length = PAGE_SIZE - page_offset;
  1099. page_length = remain < page_length ? remain : page_length;
  1100. if (node.allocated) {
  1101. wmb(); /* flush the write before we modify the GGTT */
  1102. ggtt->base.insert_page(&ggtt->base,
  1103. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  1104. node.start, I915_CACHE_NONE, 0);
  1105. wmb(); /* flush modifications to the GGTT (insert_page) */
  1106. } else {
  1107. page_base += offset & PAGE_MASK;
  1108. }
  1109. /* If we get a fault while copying data, then (presumably) our
  1110. * source page isn't available. Return the error and we'll
  1111. * retry in the slow path.
  1112. * If the object is non-shmem backed, we retry again with the
  1113. * path that handles page fault.
  1114. */
  1115. if (ggtt_write(&ggtt->iomap, page_base, page_offset,
  1116. user_data, page_length)) {
  1117. ret = -EFAULT;
  1118. break;
  1119. }
  1120. remain -= page_length;
  1121. user_data += page_length;
  1122. offset += page_length;
  1123. }
  1124. intel_fb_obj_flush(obj, ORIGIN_CPU);
  1125. mutex_lock(&i915->drm.struct_mutex);
  1126. out_unpin:
  1127. if (node.allocated) {
  1128. wmb();
  1129. ggtt->base.clear_range(&ggtt->base,
  1130. node.start, node.size);
  1131. remove_mappable_node(&node);
  1132. } else {
  1133. i915_vma_unpin(vma);
  1134. }
  1135. out_rpm:
  1136. intel_runtime_pm_put(i915);
  1137. out_unlock:
  1138. mutex_unlock(&i915->drm.struct_mutex);
  1139. return ret;
  1140. }
  1141. static int
  1142. shmem_pwrite_slow(struct page *page, int offset, int length,
  1143. char __user *user_data,
  1144. bool page_do_bit17_swizzling,
  1145. bool needs_clflush_before,
  1146. bool needs_clflush_after)
  1147. {
  1148. char *vaddr;
  1149. int ret;
  1150. vaddr = kmap(page);
  1151. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  1152. shmem_clflush_swizzled_range(vaddr + offset, length,
  1153. page_do_bit17_swizzling);
  1154. if (page_do_bit17_swizzling)
  1155. ret = __copy_from_user_swizzled(vaddr, offset, user_data,
  1156. length);
  1157. else
  1158. ret = __copy_from_user(vaddr + offset, user_data, length);
  1159. if (needs_clflush_after)
  1160. shmem_clflush_swizzled_range(vaddr + offset, length,
  1161. page_do_bit17_swizzling);
  1162. kunmap(page);
  1163. return ret ? -EFAULT : 0;
  1164. }
  1165. /* Per-page copy function for the shmem pwrite fastpath.
  1166. * Flushes invalid cachelines before writing to the target if
  1167. * needs_clflush_before is set and flushes out any written cachelines after
  1168. * writing if needs_clflush is set.
  1169. */
  1170. static int
  1171. shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
  1172. bool page_do_bit17_swizzling,
  1173. bool needs_clflush_before,
  1174. bool needs_clflush_after)
  1175. {
  1176. int ret;
  1177. ret = -ENODEV;
  1178. if (!page_do_bit17_swizzling) {
  1179. char *vaddr = kmap_atomic(page);
  1180. if (needs_clflush_before)
  1181. drm_clflush_virt_range(vaddr + offset, len);
  1182. ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
  1183. if (needs_clflush_after)
  1184. drm_clflush_virt_range(vaddr + offset, len);
  1185. kunmap_atomic(vaddr);
  1186. }
  1187. if (ret == 0)
  1188. return ret;
  1189. return shmem_pwrite_slow(page, offset, len, user_data,
  1190. page_do_bit17_swizzling,
  1191. needs_clflush_before,
  1192. needs_clflush_after);
  1193. }
  1194. static int
  1195. i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
  1196. const struct drm_i915_gem_pwrite *args)
  1197. {
  1198. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1199. void __user *user_data;
  1200. u64 remain;
  1201. unsigned int obj_do_bit17_swizzling;
  1202. unsigned int partial_cacheline_write;
  1203. unsigned int needs_clflush;
  1204. unsigned int offset, idx;
  1205. int ret;
  1206. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  1207. if (ret)
  1208. return ret;
  1209. ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
  1210. mutex_unlock(&i915->drm.struct_mutex);
  1211. if (ret)
  1212. return ret;
  1213. obj_do_bit17_swizzling = 0;
  1214. if (i915_gem_object_needs_bit17_swizzle(obj))
  1215. obj_do_bit17_swizzling = BIT(17);
  1216. /* If we don't overwrite a cacheline completely we need to be
  1217. * careful to have up-to-date data by first clflushing. Don't
  1218. * overcomplicate things and flush the entire patch.
  1219. */
  1220. partial_cacheline_write = 0;
  1221. if (needs_clflush & CLFLUSH_BEFORE)
  1222. partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
  1223. user_data = u64_to_user_ptr(args->data_ptr);
  1224. remain = args->size;
  1225. offset = offset_in_page(args->offset);
  1226. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  1227. struct page *page = i915_gem_object_get_page(obj, idx);
  1228. int length;
  1229. length = remain;
  1230. if (offset + length > PAGE_SIZE)
  1231. length = PAGE_SIZE - offset;
  1232. ret = shmem_pwrite(page, offset, length, user_data,
  1233. page_to_phys(page) & obj_do_bit17_swizzling,
  1234. (offset | length) & partial_cacheline_write,
  1235. needs_clflush & CLFLUSH_AFTER);
  1236. if (ret)
  1237. break;
  1238. remain -= length;
  1239. user_data += length;
  1240. offset = 0;
  1241. }
  1242. intel_fb_obj_flush(obj, ORIGIN_CPU);
  1243. i915_gem_obj_finish_shmem_access(obj);
  1244. return ret;
  1245. }
  1246. /**
  1247. * Writes data to the object referenced by handle.
  1248. * @dev: drm device
  1249. * @data: ioctl data blob
  1250. * @file: drm file
  1251. *
  1252. * On error, the contents of the buffer that were to be modified are undefined.
  1253. */
  1254. int
  1255. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  1256. struct drm_file *file)
  1257. {
  1258. struct drm_i915_gem_pwrite *args = data;
  1259. struct drm_i915_gem_object *obj;
  1260. int ret;
  1261. if (args->size == 0)
  1262. return 0;
  1263. if (!access_ok(VERIFY_READ,
  1264. u64_to_user_ptr(args->data_ptr),
  1265. args->size))
  1266. return -EFAULT;
  1267. obj = i915_gem_object_lookup(file, args->handle);
  1268. if (!obj)
  1269. return -ENOENT;
  1270. /* Bounds check destination. */
  1271. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  1272. ret = -EINVAL;
  1273. goto err;
  1274. }
  1275. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  1276. ret = -ENODEV;
  1277. if (obj->ops->pwrite)
  1278. ret = obj->ops->pwrite(obj, args);
  1279. if (ret != -ENODEV)
  1280. goto err;
  1281. ret = i915_gem_object_wait(obj,
  1282. I915_WAIT_INTERRUPTIBLE |
  1283. I915_WAIT_ALL,
  1284. MAX_SCHEDULE_TIMEOUT,
  1285. to_rps_client(file));
  1286. if (ret)
  1287. goto err;
  1288. ret = i915_gem_object_pin_pages(obj);
  1289. if (ret)
  1290. goto err;
  1291. ret = -EFAULT;
  1292. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  1293. * it would end up going through the fenced access, and we'll get
  1294. * different detiling behavior between reading and writing.
  1295. * pread/pwrite currently are reading and writing from the CPU
  1296. * perspective, requiring manual detiling by the client.
  1297. */
  1298. if (!i915_gem_object_has_struct_page(obj) ||
  1299. cpu_write_needs_clflush(obj))
  1300. /* Note that the gtt paths might fail with non-page-backed user
  1301. * pointers (e.g. gtt mappings when moving data between
  1302. * textures). Fallback to the shmem path in that case.
  1303. */
  1304. ret = i915_gem_gtt_pwrite_fast(obj, args);
  1305. if (ret == -EFAULT || ret == -ENOSPC) {
  1306. if (obj->phys_handle)
  1307. ret = i915_gem_phys_pwrite(obj, args, file);
  1308. else
  1309. ret = i915_gem_shmem_pwrite(obj, args);
  1310. }
  1311. i915_gem_object_unpin_pages(obj);
  1312. err:
  1313. i915_gem_object_put(obj);
  1314. return ret;
  1315. }
  1316. static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
  1317. {
  1318. struct drm_i915_private *i915;
  1319. struct list_head *list;
  1320. struct i915_vma *vma;
  1321. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
  1322. for_each_ggtt_vma(vma, obj) {
  1323. if (i915_vma_is_active(vma))
  1324. continue;
  1325. if (!drm_mm_node_allocated(&vma->node))
  1326. continue;
  1327. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  1328. }
  1329. i915 = to_i915(obj->base.dev);
  1330. spin_lock(&i915->mm.obj_lock);
  1331. list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
  1332. list_move_tail(&obj->mm.link, list);
  1333. spin_unlock(&i915->mm.obj_lock);
  1334. }
  1335. /**
  1336. * Called when user space prepares to use an object with the CPU, either
  1337. * through the mmap ioctl's mapping or a GTT mapping.
  1338. * @dev: drm device
  1339. * @data: ioctl data blob
  1340. * @file: drm file
  1341. */
  1342. int
  1343. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1344. struct drm_file *file)
  1345. {
  1346. struct drm_i915_gem_set_domain *args = data;
  1347. struct drm_i915_gem_object *obj;
  1348. uint32_t read_domains = args->read_domains;
  1349. uint32_t write_domain = args->write_domain;
  1350. int err;
  1351. /* Only handle setting domains to types used by the CPU. */
  1352. if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
  1353. return -EINVAL;
  1354. /* Having something in the write domain implies it's in the read
  1355. * domain, and only that read domain. Enforce that in the request.
  1356. */
  1357. if (write_domain != 0 && read_domains != write_domain)
  1358. return -EINVAL;
  1359. obj = i915_gem_object_lookup(file, args->handle);
  1360. if (!obj)
  1361. return -ENOENT;
  1362. /* Try to flush the object off the GPU without holding the lock.
  1363. * We will repeat the flush holding the lock in the normal manner
  1364. * to catch cases where we are gazumped.
  1365. */
  1366. err = i915_gem_object_wait(obj,
  1367. I915_WAIT_INTERRUPTIBLE |
  1368. (write_domain ? I915_WAIT_ALL : 0),
  1369. MAX_SCHEDULE_TIMEOUT,
  1370. to_rps_client(file));
  1371. if (err)
  1372. goto out;
  1373. /*
  1374. * Proxy objects do not control access to the backing storage, ergo
  1375. * they cannot be used as a means to manipulate the cache domain
  1376. * tracking for that backing storage. The proxy object is always
  1377. * considered to be outside of any cache domain.
  1378. */
  1379. if (i915_gem_object_is_proxy(obj)) {
  1380. err = -ENXIO;
  1381. goto out;
  1382. }
  1383. /*
  1384. * Flush and acquire obj->pages so that we are coherent through
  1385. * direct access in memory with previous cached writes through
  1386. * shmemfs and that our cache domain tracking remains valid.
  1387. * For example, if the obj->filp was moved to swap without us
  1388. * being notified and releasing the pages, we would mistakenly
  1389. * continue to assume that the obj remained out of the CPU cached
  1390. * domain.
  1391. */
  1392. err = i915_gem_object_pin_pages(obj);
  1393. if (err)
  1394. goto out;
  1395. err = i915_mutex_lock_interruptible(dev);
  1396. if (err)
  1397. goto out_unpin;
  1398. if (read_domains & I915_GEM_DOMAIN_WC)
  1399. err = i915_gem_object_set_to_wc_domain(obj, write_domain);
  1400. else if (read_domains & I915_GEM_DOMAIN_GTT)
  1401. err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
  1402. else
  1403. err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
  1404. /* And bump the LRU for this access */
  1405. i915_gem_object_bump_inactive_ggtt(obj);
  1406. mutex_unlock(&dev->struct_mutex);
  1407. if (write_domain != 0)
  1408. intel_fb_obj_invalidate(obj,
  1409. fb_write_origin(obj, write_domain));
  1410. out_unpin:
  1411. i915_gem_object_unpin_pages(obj);
  1412. out:
  1413. i915_gem_object_put(obj);
  1414. return err;
  1415. }
  1416. /**
  1417. * Called when user space has done writes to this buffer
  1418. * @dev: drm device
  1419. * @data: ioctl data blob
  1420. * @file: drm file
  1421. */
  1422. int
  1423. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1424. struct drm_file *file)
  1425. {
  1426. struct drm_i915_gem_sw_finish *args = data;
  1427. struct drm_i915_gem_object *obj;
  1428. obj = i915_gem_object_lookup(file, args->handle);
  1429. if (!obj)
  1430. return -ENOENT;
  1431. /*
  1432. * Proxy objects are barred from CPU access, so there is no
  1433. * need to ban sw_finish as it is a nop.
  1434. */
  1435. /* Pinned buffers may be scanout, so flush the cache */
  1436. i915_gem_object_flush_if_display(obj);
  1437. i915_gem_object_put(obj);
  1438. return 0;
  1439. }
  1440. /**
  1441. * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
  1442. * it is mapped to.
  1443. * @dev: drm device
  1444. * @data: ioctl data blob
  1445. * @file: drm file
  1446. *
  1447. * While the mapping holds a reference on the contents of the object, it doesn't
  1448. * imply a ref on the object itself.
  1449. *
  1450. * IMPORTANT:
  1451. *
  1452. * DRM driver writers who look a this function as an example for how to do GEM
  1453. * mmap support, please don't implement mmap support like here. The modern way
  1454. * to implement DRM mmap support is with an mmap offset ioctl (like
  1455. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1456. * That way debug tooling like valgrind will understand what's going on, hiding
  1457. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1458. * does cpu mmaps this way because we didn't know better.
  1459. */
  1460. int
  1461. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1462. struct drm_file *file)
  1463. {
  1464. struct drm_i915_gem_mmap *args = data;
  1465. struct drm_i915_gem_object *obj;
  1466. unsigned long addr;
  1467. if (args->flags & ~(I915_MMAP_WC))
  1468. return -EINVAL;
  1469. if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
  1470. return -ENODEV;
  1471. obj = i915_gem_object_lookup(file, args->handle);
  1472. if (!obj)
  1473. return -ENOENT;
  1474. /* prime objects have no backing filp to GEM mmap
  1475. * pages from.
  1476. */
  1477. if (!obj->base.filp) {
  1478. i915_gem_object_put(obj);
  1479. return -ENXIO;
  1480. }
  1481. addr = vm_mmap(obj->base.filp, 0, args->size,
  1482. PROT_READ | PROT_WRITE, MAP_SHARED,
  1483. args->offset);
  1484. if (args->flags & I915_MMAP_WC) {
  1485. struct mm_struct *mm = current->mm;
  1486. struct vm_area_struct *vma;
  1487. if (down_write_killable(&mm->mmap_sem)) {
  1488. i915_gem_object_put(obj);
  1489. return -EINTR;
  1490. }
  1491. vma = find_vma(mm, addr);
  1492. if (vma)
  1493. vma->vm_page_prot =
  1494. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1495. else
  1496. addr = -ENOMEM;
  1497. up_write(&mm->mmap_sem);
  1498. /* This may race, but that's ok, it only gets set */
  1499. WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
  1500. }
  1501. i915_gem_object_put(obj);
  1502. if (IS_ERR((void *)addr))
  1503. return addr;
  1504. args->addr_ptr = (uint64_t) addr;
  1505. return 0;
  1506. }
  1507. static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
  1508. {
  1509. return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
  1510. }
  1511. /**
  1512. * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
  1513. *
  1514. * A history of the GTT mmap interface:
  1515. *
  1516. * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
  1517. * aligned and suitable for fencing, and still fit into the available
  1518. * mappable space left by the pinned display objects. A classic problem
  1519. * we called the page-fault-of-doom where we would ping-pong between
  1520. * two objects that could not fit inside the GTT and so the memcpy
  1521. * would page one object in at the expense of the other between every
  1522. * single byte.
  1523. *
  1524. * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
  1525. * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
  1526. * object is too large for the available space (or simply too large
  1527. * for the mappable aperture!), a view is created instead and faulted
  1528. * into userspace. (This view is aligned and sized appropriately for
  1529. * fenced access.)
  1530. *
  1531. * 2 - Recognise WC as a separate cache domain so that we can flush the
  1532. * delayed writes via GTT before performing direct access via WC.
  1533. *
  1534. * Restrictions:
  1535. *
  1536. * * snoopable objects cannot be accessed via the GTT. It can cause machine
  1537. * hangs on some architectures, corruption on others. An attempt to service
  1538. * a GTT page fault from a snoopable object will generate a SIGBUS.
  1539. *
  1540. * * the object must be able to fit into RAM (physical memory, though no
  1541. * limited to the mappable aperture).
  1542. *
  1543. *
  1544. * Caveats:
  1545. *
  1546. * * a new GTT page fault will synchronize rendering from the GPU and flush
  1547. * all data to system memory. Subsequent access will not be synchronized.
  1548. *
  1549. * * all mappings are revoked on runtime device suspend.
  1550. *
  1551. * * there are only 8, 16 or 32 fence registers to share between all users
  1552. * (older machines require fence register for display and blitter access
  1553. * as well). Contention of the fence registers will cause the previous users
  1554. * to be unmapped and any new access will generate new page faults.
  1555. *
  1556. * * running out of memory while servicing a fault may generate a SIGBUS,
  1557. * rather than the expected SIGSEGV.
  1558. */
  1559. int i915_gem_mmap_gtt_version(void)
  1560. {
  1561. return 2;
  1562. }
  1563. static inline struct i915_ggtt_view
  1564. compute_partial_view(struct drm_i915_gem_object *obj,
  1565. pgoff_t page_offset,
  1566. unsigned int chunk)
  1567. {
  1568. struct i915_ggtt_view view;
  1569. if (i915_gem_object_is_tiled(obj))
  1570. chunk = roundup(chunk, tile_row_pages(obj));
  1571. view.type = I915_GGTT_VIEW_PARTIAL;
  1572. view.partial.offset = rounddown(page_offset, chunk);
  1573. view.partial.size =
  1574. min_t(unsigned int, chunk,
  1575. (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
  1576. /* If the partial covers the entire object, just create a normal VMA. */
  1577. if (chunk >= obj->base.size >> PAGE_SHIFT)
  1578. view.type = I915_GGTT_VIEW_NORMAL;
  1579. return view;
  1580. }
  1581. /**
  1582. * i915_gem_fault - fault a page into the GTT
  1583. * @vmf: fault info
  1584. *
  1585. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1586. * from userspace. The fault handler takes care of binding the object to
  1587. * the GTT (if needed), allocating and programming a fence register (again,
  1588. * only if needed based on whether the old reg is still valid or the object
  1589. * is tiled) and inserting a new PTE into the faulting process.
  1590. *
  1591. * Note that the faulting process may involve evicting existing objects
  1592. * from the GTT and/or fence registers to make room. So performance may
  1593. * suffer if the GTT working set is large or there are few fence registers
  1594. * left.
  1595. *
  1596. * The current feature set supported by i915_gem_fault() and thus GTT mmaps
  1597. * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
  1598. */
  1599. int i915_gem_fault(struct vm_fault *vmf)
  1600. {
  1601. #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
  1602. struct vm_area_struct *area = vmf->vma;
  1603. struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
  1604. struct drm_device *dev = obj->base.dev;
  1605. struct drm_i915_private *dev_priv = to_i915(dev);
  1606. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1607. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1608. struct i915_vma *vma;
  1609. pgoff_t page_offset;
  1610. unsigned int flags;
  1611. int ret;
  1612. /* We don't use vmf->pgoff since that has the fake offset */
  1613. page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
  1614. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1615. /* Try to flush the object off the GPU first without holding the lock.
  1616. * Upon acquiring the lock, we will perform our sanity checks and then
  1617. * repeat the flush holding the lock in the normal manner to catch cases
  1618. * where we are gazumped.
  1619. */
  1620. ret = i915_gem_object_wait(obj,
  1621. I915_WAIT_INTERRUPTIBLE,
  1622. MAX_SCHEDULE_TIMEOUT,
  1623. NULL);
  1624. if (ret)
  1625. goto err;
  1626. ret = i915_gem_object_pin_pages(obj);
  1627. if (ret)
  1628. goto err;
  1629. intel_runtime_pm_get(dev_priv);
  1630. ret = i915_mutex_lock_interruptible(dev);
  1631. if (ret)
  1632. goto err_rpm;
  1633. /* Access to snoopable pages through the GTT is incoherent. */
  1634. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
  1635. ret = -EFAULT;
  1636. goto err_unlock;
  1637. }
  1638. /* If the object is smaller than a couple of partial vma, it is
  1639. * not worth only creating a single partial vma - we may as well
  1640. * clear enough space for the full object.
  1641. */
  1642. flags = PIN_MAPPABLE;
  1643. if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
  1644. flags |= PIN_NONBLOCK | PIN_NONFAULT;
  1645. /* Now pin it into the GTT as needed */
  1646. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
  1647. if (IS_ERR(vma)) {
  1648. /* Use a partial view if it is bigger than available space */
  1649. struct i915_ggtt_view view =
  1650. compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
  1651. /* Userspace is now writing through an untracked VMA, abandon
  1652. * all hope that the hardware is able to track future writes.
  1653. */
  1654. obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
  1655. vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
  1656. }
  1657. if (IS_ERR(vma)) {
  1658. ret = PTR_ERR(vma);
  1659. goto err_unlock;
  1660. }
  1661. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1662. if (ret)
  1663. goto err_unpin;
  1664. ret = i915_vma_pin_fence(vma);
  1665. if (ret)
  1666. goto err_unpin;
  1667. /* Finally, remap it using the new GTT offset */
  1668. ret = remap_io_mapping(area,
  1669. area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
  1670. (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
  1671. min_t(u64, vma->size, area->vm_end - area->vm_start),
  1672. &ggtt->iomap);
  1673. if (ret)
  1674. goto err_fence;
  1675. /* Mark as being mmapped into userspace for later revocation */
  1676. assert_rpm_wakelock_held(dev_priv);
  1677. if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
  1678. list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
  1679. GEM_BUG_ON(!obj->userfault_count);
  1680. i915_vma_set_ggtt_write(vma);
  1681. err_fence:
  1682. i915_vma_unpin_fence(vma);
  1683. err_unpin:
  1684. __i915_vma_unpin(vma);
  1685. err_unlock:
  1686. mutex_unlock(&dev->struct_mutex);
  1687. err_rpm:
  1688. intel_runtime_pm_put(dev_priv);
  1689. i915_gem_object_unpin_pages(obj);
  1690. err:
  1691. switch (ret) {
  1692. case -EIO:
  1693. /*
  1694. * We eat errors when the gpu is terminally wedged to avoid
  1695. * userspace unduly crashing (gl has no provisions for mmaps to
  1696. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1697. * and so needs to be reported.
  1698. */
  1699. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1700. ret = VM_FAULT_SIGBUS;
  1701. break;
  1702. }
  1703. case -EAGAIN:
  1704. /*
  1705. * EAGAIN means the gpu is hung and we'll wait for the error
  1706. * handler to reset everything when re-faulting in
  1707. * i915_mutex_lock_interruptible.
  1708. */
  1709. case 0:
  1710. case -ERESTARTSYS:
  1711. case -EINTR:
  1712. case -EBUSY:
  1713. /*
  1714. * EBUSY is ok: this just means that another thread
  1715. * already did the job.
  1716. */
  1717. ret = VM_FAULT_NOPAGE;
  1718. break;
  1719. case -ENOMEM:
  1720. ret = VM_FAULT_OOM;
  1721. break;
  1722. case -ENOSPC:
  1723. case -EFAULT:
  1724. ret = VM_FAULT_SIGBUS;
  1725. break;
  1726. default:
  1727. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1728. ret = VM_FAULT_SIGBUS;
  1729. break;
  1730. }
  1731. return ret;
  1732. }
  1733. static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
  1734. {
  1735. struct i915_vma *vma;
  1736. GEM_BUG_ON(!obj->userfault_count);
  1737. obj->userfault_count = 0;
  1738. list_del(&obj->userfault_link);
  1739. drm_vma_node_unmap(&obj->base.vma_node,
  1740. obj->base.dev->anon_inode->i_mapping);
  1741. for_each_ggtt_vma(vma, obj)
  1742. i915_vma_unset_userfault(vma);
  1743. }
  1744. /**
  1745. * i915_gem_release_mmap - remove physical page mappings
  1746. * @obj: obj in question
  1747. *
  1748. * Preserve the reservation of the mmapping with the DRM core code, but
  1749. * relinquish ownership of the pages back to the system.
  1750. *
  1751. * It is vital that we remove the page mapping if we have mapped a tiled
  1752. * object through the GTT and then lose the fence register due to
  1753. * resource pressure. Similarly if the object has been moved out of the
  1754. * aperture, than pages mapped into userspace must be revoked. Removing the
  1755. * mapping will then trigger a page fault on the next user access, allowing
  1756. * fixup by i915_gem_fault().
  1757. */
  1758. void
  1759. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1760. {
  1761. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1762. /* Serialisation between user GTT access and our code depends upon
  1763. * revoking the CPU's PTE whilst the mutex is held. The next user
  1764. * pagefault then has to wait until we release the mutex.
  1765. *
  1766. * Note that RPM complicates somewhat by adding an additional
  1767. * requirement that operations to the GGTT be made holding the RPM
  1768. * wakeref.
  1769. */
  1770. lockdep_assert_held(&i915->drm.struct_mutex);
  1771. intel_runtime_pm_get(i915);
  1772. if (!obj->userfault_count)
  1773. goto out;
  1774. __i915_gem_object_release_mmap(obj);
  1775. /* Ensure that the CPU's PTE are revoked and there are not outstanding
  1776. * memory transactions from userspace before we return. The TLB
  1777. * flushing implied above by changing the PTE above *should* be
  1778. * sufficient, an extra barrier here just provides us with a bit
  1779. * of paranoid documentation about our requirement to serialise
  1780. * memory writes before touching registers / GSM.
  1781. */
  1782. wmb();
  1783. out:
  1784. intel_runtime_pm_put(i915);
  1785. }
  1786. void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
  1787. {
  1788. struct drm_i915_gem_object *obj, *on;
  1789. int i;
  1790. /*
  1791. * Only called during RPM suspend. All users of the userfault_list
  1792. * must be holding an RPM wakeref to ensure that this can not
  1793. * run concurrently with themselves (and use the struct_mutex for
  1794. * protection between themselves).
  1795. */
  1796. list_for_each_entry_safe(obj, on,
  1797. &dev_priv->mm.userfault_list, userfault_link)
  1798. __i915_gem_object_release_mmap(obj);
  1799. /* The fence will be lost when the device powers down. If any were
  1800. * in use by hardware (i.e. they are pinned), we should not be powering
  1801. * down! All other fences will be reacquired by the user upon waking.
  1802. */
  1803. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1804. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1805. /* Ideally we want to assert that the fence register is not
  1806. * live at this point (i.e. that no piece of code will be
  1807. * trying to write through fence + GTT, as that both violates
  1808. * our tracking of activity and associated locking/barriers,
  1809. * but also is illegal given that the hw is powered down).
  1810. *
  1811. * Previously we used reg->pin_count as a "liveness" indicator.
  1812. * That is not sufficient, and we need a more fine-grained
  1813. * tool if we want to have a sanity check here.
  1814. */
  1815. if (!reg->vma)
  1816. continue;
  1817. GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
  1818. reg->dirty = true;
  1819. }
  1820. }
  1821. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1822. {
  1823. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1824. int err;
  1825. err = drm_gem_create_mmap_offset(&obj->base);
  1826. if (likely(!err))
  1827. return 0;
  1828. /* Attempt to reap some mmap space from dead objects */
  1829. do {
  1830. err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
  1831. if (err)
  1832. break;
  1833. i915_gem_drain_freed_objects(dev_priv);
  1834. err = drm_gem_create_mmap_offset(&obj->base);
  1835. if (!err)
  1836. break;
  1837. } while (flush_delayed_work(&dev_priv->gt.retire_work));
  1838. return err;
  1839. }
  1840. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1841. {
  1842. drm_gem_free_mmap_offset(&obj->base);
  1843. }
  1844. int
  1845. i915_gem_mmap_gtt(struct drm_file *file,
  1846. struct drm_device *dev,
  1847. uint32_t handle,
  1848. uint64_t *offset)
  1849. {
  1850. struct drm_i915_gem_object *obj;
  1851. int ret;
  1852. obj = i915_gem_object_lookup(file, handle);
  1853. if (!obj)
  1854. return -ENOENT;
  1855. ret = i915_gem_object_create_mmap_offset(obj);
  1856. if (ret == 0)
  1857. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1858. i915_gem_object_put(obj);
  1859. return ret;
  1860. }
  1861. /**
  1862. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1863. * @dev: DRM device
  1864. * @data: GTT mapping ioctl data
  1865. * @file: GEM object info
  1866. *
  1867. * Simply returns the fake offset to userspace so it can mmap it.
  1868. * The mmap call will end up in drm_gem_mmap(), which will set things
  1869. * up so we can get faults in the handler above.
  1870. *
  1871. * The fault handler will take care of binding the object into the GTT
  1872. * (since it may have been evicted to make room for something), allocating
  1873. * a fence register, and mapping the appropriate aperture address into
  1874. * userspace.
  1875. */
  1876. int
  1877. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1878. struct drm_file *file)
  1879. {
  1880. struct drm_i915_gem_mmap_gtt *args = data;
  1881. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1882. }
  1883. /* Immediately discard the backing storage */
  1884. static void
  1885. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1886. {
  1887. i915_gem_object_free_mmap_offset(obj);
  1888. if (obj->base.filp == NULL)
  1889. return;
  1890. /* Our goal here is to return as much of the memory as
  1891. * is possible back to the system as we are called from OOM.
  1892. * To do this we must instruct the shmfs to drop all of its
  1893. * backing pages, *now*.
  1894. */
  1895. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1896. obj->mm.madv = __I915_MADV_PURGED;
  1897. obj->mm.pages = ERR_PTR(-EFAULT);
  1898. }
  1899. /* Try to discard unwanted pages */
  1900. void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1901. {
  1902. struct address_space *mapping;
  1903. lockdep_assert_held(&obj->mm.lock);
  1904. GEM_BUG_ON(i915_gem_object_has_pages(obj));
  1905. switch (obj->mm.madv) {
  1906. case I915_MADV_DONTNEED:
  1907. i915_gem_object_truncate(obj);
  1908. case __I915_MADV_PURGED:
  1909. return;
  1910. }
  1911. if (obj->base.filp == NULL)
  1912. return;
  1913. mapping = obj->base.filp->f_mapping,
  1914. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1915. }
  1916. static void
  1917. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
  1918. struct sg_table *pages)
  1919. {
  1920. struct sgt_iter sgt_iter;
  1921. struct page *page;
  1922. __i915_gem_object_release_shmem(obj, pages, true);
  1923. i915_gem_gtt_finish_pages(obj, pages);
  1924. if (i915_gem_object_needs_bit17_swizzle(obj))
  1925. i915_gem_object_save_bit_17_swizzle(obj, pages);
  1926. for_each_sgt_page(page, sgt_iter, pages) {
  1927. if (obj->mm.dirty)
  1928. set_page_dirty(page);
  1929. if (obj->mm.madv == I915_MADV_WILLNEED)
  1930. mark_page_accessed(page);
  1931. put_page(page);
  1932. }
  1933. obj->mm.dirty = false;
  1934. sg_free_table(pages);
  1935. kfree(pages);
  1936. }
  1937. static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
  1938. {
  1939. struct radix_tree_iter iter;
  1940. void __rcu **slot;
  1941. rcu_read_lock();
  1942. radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
  1943. radix_tree_delete(&obj->mm.get_page.radix, iter.index);
  1944. rcu_read_unlock();
  1945. }
  1946. void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
  1947. enum i915_mm_subclass subclass)
  1948. {
  1949. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1950. struct sg_table *pages;
  1951. if (i915_gem_object_has_pinned_pages(obj))
  1952. return;
  1953. GEM_BUG_ON(obj->bind_count);
  1954. if (!i915_gem_object_has_pages(obj))
  1955. return;
  1956. /* May be called by shrinker from within get_pages() (on another bo) */
  1957. mutex_lock_nested(&obj->mm.lock, subclass);
  1958. if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
  1959. goto unlock;
  1960. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1961. * array, hence protect them from being reaped by removing them from gtt
  1962. * lists early. */
  1963. pages = fetch_and_zero(&obj->mm.pages);
  1964. GEM_BUG_ON(!pages);
  1965. spin_lock(&i915->mm.obj_lock);
  1966. list_del(&obj->mm.link);
  1967. spin_unlock(&i915->mm.obj_lock);
  1968. if (obj->mm.mapping) {
  1969. void *ptr;
  1970. ptr = page_mask_bits(obj->mm.mapping);
  1971. if (is_vmalloc_addr(ptr))
  1972. vunmap(ptr);
  1973. else
  1974. kunmap(kmap_to_page(ptr));
  1975. obj->mm.mapping = NULL;
  1976. }
  1977. __i915_gem_object_reset_page_iter(obj);
  1978. if (!IS_ERR(pages))
  1979. obj->ops->put_pages(obj, pages);
  1980. obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
  1981. unlock:
  1982. mutex_unlock(&obj->mm.lock);
  1983. }
  1984. static bool i915_sg_trim(struct sg_table *orig_st)
  1985. {
  1986. struct sg_table new_st;
  1987. struct scatterlist *sg, *new_sg;
  1988. unsigned int i;
  1989. if (orig_st->nents == orig_st->orig_nents)
  1990. return false;
  1991. if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
  1992. return false;
  1993. new_sg = new_st.sgl;
  1994. for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
  1995. sg_set_page(new_sg, sg_page(sg), sg->length, 0);
  1996. /* called before being DMA mapped, no need to copy sg->dma_* */
  1997. new_sg = sg_next(new_sg);
  1998. }
  1999. GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
  2000. sg_free_table(orig_st);
  2001. *orig_st = new_st;
  2002. return true;
  2003. }
  2004. static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  2005. {
  2006. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2007. const unsigned long page_count = obj->base.size / PAGE_SIZE;
  2008. unsigned long i;
  2009. struct address_space *mapping;
  2010. struct sg_table *st;
  2011. struct scatterlist *sg;
  2012. struct sgt_iter sgt_iter;
  2013. struct page *page;
  2014. unsigned long last_pfn = 0; /* suppress gcc warning */
  2015. unsigned int max_segment = i915_sg_segment_size();
  2016. unsigned int sg_page_sizes;
  2017. gfp_t noreclaim;
  2018. int ret;
  2019. /* Assert that the object is not currently in any GPU domain. As it
  2020. * wasn't in the GTT, there shouldn't be any way it could have been in
  2021. * a GPU cache
  2022. */
  2023. GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  2024. GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  2025. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2026. if (st == NULL)
  2027. return -ENOMEM;
  2028. rebuild_st:
  2029. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  2030. kfree(st);
  2031. return -ENOMEM;
  2032. }
  2033. /* Get the list of pages out of our struct file. They'll be pinned
  2034. * at this point until we release them.
  2035. *
  2036. * Fail silently without starting the shrinker
  2037. */
  2038. mapping = obj->base.filp->f_mapping;
  2039. noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
  2040. noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
  2041. sg = st->sgl;
  2042. st->nents = 0;
  2043. sg_page_sizes = 0;
  2044. for (i = 0; i < page_count; i++) {
  2045. const unsigned int shrink[] = {
  2046. I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
  2047. 0,
  2048. }, *s = shrink;
  2049. gfp_t gfp = noreclaim;
  2050. do {
  2051. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  2052. if (likely(!IS_ERR(page)))
  2053. break;
  2054. if (!*s) {
  2055. ret = PTR_ERR(page);
  2056. goto err_sg;
  2057. }
  2058. i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
  2059. cond_resched();
  2060. /* We've tried hard to allocate the memory by reaping
  2061. * our own buffer, now let the real VM do its job and
  2062. * go down in flames if truly OOM.
  2063. *
  2064. * However, since graphics tend to be disposable,
  2065. * defer the oom here by reporting the ENOMEM back
  2066. * to userspace.
  2067. */
  2068. if (!*s) {
  2069. /* reclaim and warn, but no oom */
  2070. gfp = mapping_gfp_mask(mapping);
  2071. /* Our bo are always dirty and so we require
  2072. * kswapd to reclaim our pages (direct reclaim
  2073. * does not effectively begin pageout of our
  2074. * buffers on its own). However, direct reclaim
  2075. * only waits for kswapd when under allocation
  2076. * congestion. So as a result __GFP_RECLAIM is
  2077. * unreliable and fails to actually reclaim our
  2078. * dirty pages -- unless you try over and over
  2079. * again with !__GFP_NORETRY. However, we still
  2080. * want to fail this allocation rather than
  2081. * trigger the out-of-memory killer and for
  2082. * this we want __GFP_RETRY_MAYFAIL.
  2083. */
  2084. gfp |= __GFP_RETRY_MAYFAIL;
  2085. }
  2086. } while (1);
  2087. if (!i ||
  2088. sg->length >= max_segment ||
  2089. page_to_pfn(page) != last_pfn + 1) {
  2090. if (i) {
  2091. sg_page_sizes |= sg->length;
  2092. sg = sg_next(sg);
  2093. }
  2094. st->nents++;
  2095. sg_set_page(sg, page, PAGE_SIZE, 0);
  2096. } else {
  2097. sg->length += PAGE_SIZE;
  2098. }
  2099. last_pfn = page_to_pfn(page);
  2100. /* Check that the i965g/gm workaround works. */
  2101. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  2102. }
  2103. if (sg) { /* loop terminated early; short sg table */
  2104. sg_page_sizes |= sg->length;
  2105. sg_mark_end(sg);
  2106. }
  2107. /* Trim unused sg entries to avoid wasting memory. */
  2108. i915_sg_trim(st);
  2109. ret = i915_gem_gtt_prepare_pages(obj, st);
  2110. if (ret) {
  2111. /* DMA remapping failed? One possible cause is that
  2112. * it could not reserve enough large entries, asking
  2113. * for PAGE_SIZE chunks instead may be helpful.
  2114. */
  2115. if (max_segment > PAGE_SIZE) {
  2116. for_each_sgt_page(page, sgt_iter, st)
  2117. put_page(page);
  2118. sg_free_table(st);
  2119. max_segment = PAGE_SIZE;
  2120. goto rebuild_st;
  2121. } else {
  2122. dev_warn(&dev_priv->drm.pdev->dev,
  2123. "Failed to DMA remap %lu pages\n",
  2124. page_count);
  2125. goto err_pages;
  2126. }
  2127. }
  2128. if (i915_gem_object_needs_bit17_swizzle(obj))
  2129. i915_gem_object_do_bit_17_swizzle(obj, st);
  2130. __i915_gem_object_set_pages(obj, st, sg_page_sizes);
  2131. return 0;
  2132. err_sg:
  2133. sg_mark_end(sg);
  2134. err_pages:
  2135. for_each_sgt_page(page, sgt_iter, st)
  2136. put_page(page);
  2137. sg_free_table(st);
  2138. kfree(st);
  2139. /* shmemfs first checks if there is enough memory to allocate the page
  2140. * and reports ENOSPC should there be insufficient, along with the usual
  2141. * ENOMEM for a genuine allocation failure.
  2142. *
  2143. * We use ENOSPC in our driver to mean that we have run out of aperture
  2144. * space and so want to translate the error from shmemfs back to our
  2145. * usual understanding of ENOMEM.
  2146. */
  2147. if (ret == -ENOSPC)
  2148. ret = -ENOMEM;
  2149. return ret;
  2150. }
  2151. void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
  2152. struct sg_table *pages,
  2153. unsigned int sg_page_sizes)
  2154. {
  2155. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  2156. unsigned long supported = INTEL_INFO(i915)->page_sizes;
  2157. int i;
  2158. lockdep_assert_held(&obj->mm.lock);
  2159. obj->mm.get_page.sg_pos = pages->sgl;
  2160. obj->mm.get_page.sg_idx = 0;
  2161. obj->mm.pages = pages;
  2162. if (i915_gem_object_is_tiled(obj) &&
  2163. i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  2164. GEM_BUG_ON(obj->mm.quirked);
  2165. __i915_gem_object_pin_pages(obj);
  2166. obj->mm.quirked = true;
  2167. }
  2168. GEM_BUG_ON(!sg_page_sizes);
  2169. obj->mm.page_sizes.phys = sg_page_sizes;
  2170. /*
  2171. * Calculate the supported page-sizes which fit into the given
  2172. * sg_page_sizes. This will give us the page-sizes which we may be able
  2173. * to use opportunistically when later inserting into the GTT. For
  2174. * example if phys=2G, then in theory we should be able to use 1G, 2M,
  2175. * 64K or 4K pages, although in practice this will depend on a number of
  2176. * other factors.
  2177. */
  2178. obj->mm.page_sizes.sg = 0;
  2179. for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
  2180. if (obj->mm.page_sizes.phys & ~0u << i)
  2181. obj->mm.page_sizes.sg |= BIT(i);
  2182. }
  2183. GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
  2184. spin_lock(&i915->mm.obj_lock);
  2185. list_add(&obj->mm.link, &i915->mm.unbound_list);
  2186. spin_unlock(&i915->mm.obj_lock);
  2187. }
  2188. static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2189. {
  2190. int err;
  2191. if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
  2192. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  2193. return -EFAULT;
  2194. }
  2195. err = obj->ops->get_pages(obj);
  2196. GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
  2197. return err;
  2198. }
  2199. /* Ensure that the associated pages are gathered from the backing storage
  2200. * and pinned into our object. i915_gem_object_pin_pages() may be called
  2201. * multiple times before they are released by a single call to
  2202. * i915_gem_object_unpin_pages() - once the pages are no longer referenced
  2203. * either as a result of memory pressure (reaping pages under the shrinker)
  2204. * or as the object is itself released.
  2205. */
  2206. int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2207. {
  2208. int err;
  2209. err = mutex_lock_interruptible(&obj->mm.lock);
  2210. if (err)
  2211. return err;
  2212. if (unlikely(!i915_gem_object_has_pages(obj))) {
  2213. GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
  2214. err = ____i915_gem_object_get_pages(obj);
  2215. if (err)
  2216. goto unlock;
  2217. smp_mb__before_atomic();
  2218. }
  2219. atomic_inc(&obj->mm.pages_pin_count);
  2220. unlock:
  2221. mutex_unlock(&obj->mm.lock);
  2222. return err;
  2223. }
  2224. /* The 'mapping' part of i915_gem_object_pin_map() below */
  2225. static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
  2226. enum i915_map_type type)
  2227. {
  2228. unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
  2229. struct sg_table *sgt = obj->mm.pages;
  2230. struct sgt_iter sgt_iter;
  2231. struct page *page;
  2232. struct page *stack_pages[32];
  2233. struct page **pages = stack_pages;
  2234. unsigned long i = 0;
  2235. pgprot_t pgprot;
  2236. void *addr;
  2237. /* A single page can always be kmapped */
  2238. if (n_pages == 1 && type == I915_MAP_WB)
  2239. return kmap(sg_page(sgt->sgl));
  2240. if (n_pages > ARRAY_SIZE(stack_pages)) {
  2241. /* Too big for stack -- allocate temporary array instead */
  2242. pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
  2243. if (!pages)
  2244. return NULL;
  2245. }
  2246. for_each_sgt_page(page, sgt_iter, sgt)
  2247. pages[i++] = page;
  2248. /* Check that we have the expected number of pages */
  2249. GEM_BUG_ON(i != n_pages);
  2250. switch (type) {
  2251. default:
  2252. MISSING_CASE(type);
  2253. /* fallthrough to use PAGE_KERNEL anyway */
  2254. case I915_MAP_WB:
  2255. pgprot = PAGE_KERNEL;
  2256. break;
  2257. case I915_MAP_WC:
  2258. pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
  2259. break;
  2260. }
  2261. addr = vmap(pages, n_pages, 0, pgprot);
  2262. if (pages != stack_pages)
  2263. kvfree(pages);
  2264. return addr;
  2265. }
  2266. /* get, pin, and map the pages of the object into kernel space */
  2267. void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
  2268. enum i915_map_type type)
  2269. {
  2270. enum i915_map_type has_type;
  2271. bool pinned;
  2272. void *ptr;
  2273. int ret;
  2274. if (unlikely(!i915_gem_object_has_struct_page(obj)))
  2275. return ERR_PTR(-ENXIO);
  2276. ret = mutex_lock_interruptible(&obj->mm.lock);
  2277. if (ret)
  2278. return ERR_PTR(ret);
  2279. pinned = !(type & I915_MAP_OVERRIDE);
  2280. type &= ~I915_MAP_OVERRIDE;
  2281. if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
  2282. if (unlikely(!i915_gem_object_has_pages(obj))) {
  2283. GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
  2284. ret = ____i915_gem_object_get_pages(obj);
  2285. if (ret)
  2286. goto err_unlock;
  2287. smp_mb__before_atomic();
  2288. }
  2289. atomic_inc(&obj->mm.pages_pin_count);
  2290. pinned = false;
  2291. }
  2292. GEM_BUG_ON(!i915_gem_object_has_pages(obj));
  2293. ptr = page_unpack_bits(obj->mm.mapping, &has_type);
  2294. if (ptr && has_type != type) {
  2295. if (pinned) {
  2296. ret = -EBUSY;
  2297. goto err_unpin;
  2298. }
  2299. if (is_vmalloc_addr(ptr))
  2300. vunmap(ptr);
  2301. else
  2302. kunmap(kmap_to_page(ptr));
  2303. ptr = obj->mm.mapping = NULL;
  2304. }
  2305. if (!ptr) {
  2306. ptr = i915_gem_object_map(obj, type);
  2307. if (!ptr) {
  2308. ret = -ENOMEM;
  2309. goto err_unpin;
  2310. }
  2311. obj->mm.mapping = page_pack_bits(ptr, type);
  2312. }
  2313. out_unlock:
  2314. mutex_unlock(&obj->mm.lock);
  2315. return ptr;
  2316. err_unpin:
  2317. atomic_dec(&obj->mm.pages_pin_count);
  2318. err_unlock:
  2319. ptr = ERR_PTR(ret);
  2320. goto out_unlock;
  2321. }
  2322. static int
  2323. i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
  2324. const struct drm_i915_gem_pwrite *arg)
  2325. {
  2326. struct address_space *mapping = obj->base.filp->f_mapping;
  2327. char __user *user_data = u64_to_user_ptr(arg->data_ptr);
  2328. u64 remain, offset;
  2329. unsigned int pg;
  2330. /* Before we instantiate/pin the backing store for our use, we
  2331. * can prepopulate the shmemfs filp efficiently using a write into
  2332. * the pagecache. We avoid the penalty of instantiating all the
  2333. * pages, important if the user is just writing to a few and never
  2334. * uses the object on the GPU, and using a direct write into shmemfs
  2335. * allows it to avoid the cost of retrieving a page (either swapin
  2336. * or clearing-before-use) before it is overwritten.
  2337. */
  2338. if (i915_gem_object_has_pages(obj))
  2339. return -ENODEV;
  2340. if (obj->mm.madv != I915_MADV_WILLNEED)
  2341. return -EFAULT;
  2342. /* Before the pages are instantiated the object is treated as being
  2343. * in the CPU domain. The pages will be clflushed as required before
  2344. * use, and we can freely write into the pages directly. If userspace
  2345. * races pwrite with any other operation; corruption will ensue -
  2346. * that is userspace's prerogative!
  2347. */
  2348. remain = arg->size;
  2349. offset = arg->offset;
  2350. pg = offset_in_page(offset);
  2351. do {
  2352. unsigned int len, unwritten;
  2353. struct page *page;
  2354. void *data, *vaddr;
  2355. int err;
  2356. len = PAGE_SIZE - pg;
  2357. if (len > remain)
  2358. len = remain;
  2359. err = pagecache_write_begin(obj->base.filp, mapping,
  2360. offset, len, 0,
  2361. &page, &data);
  2362. if (err < 0)
  2363. return err;
  2364. vaddr = kmap(page);
  2365. unwritten = copy_from_user(vaddr + pg, user_data, len);
  2366. kunmap(page);
  2367. err = pagecache_write_end(obj->base.filp, mapping,
  2368. offset, len, len - unwritten,
  2369. page, data);
  2370. if (err < 0)
  2371. return err;
  2372. if (unwritten)
  2373. return -EFAULT;
  2374. remain -= len;
  2375. user_data += len;
  2376. offset += len;
  2377. pg = 0;
  2378. } while (remain);
  2379. return 0;
  2380. }
  2381. static bool ban_context(const struct i915_gem_context *ctx,
  2382. unsigned int score)
  2383. {
  2384. return (i915_gem_context_is_bannable(ctx) &&
  2385. score >= CONTEXT_SCORE_BAN_THRESHOLD);
  2386. }
  2387. static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
  2388. {
  2389. unsigned int score;
  2390. bool banned;
  2391. atomic_inc(&ctx->guilty_count);
  2392. score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score);
  2393. banned = ban_context(ctx, score);
  2394. DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
  2395. ctx->name, score, yesno(banned));
  2396. if (!banned)
  2397. return;
  2398. i915_gem_context_set_banned(ctx);
  2399. if (!IS_ERR_OR_NULL(ctx->file_priv)) {
  2400. atomic_inc(&ctx->file_priv->context_bans);
  2401. DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
  2402. ctx->name, atomic_read(&ctx->file_priv->context_bans));
  2403. }
  2404. }
  2405. static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
  2406. {
  2407. atomic_inc(&ctx->active_count);
  2408. }
  2409. struct drm_i915_gem_request *
  2410. i915_gem_find_active_request(struct intel_engine_cs *engine)
  2411. {
  2412. struct drm_i915_gem_request *request, *active = NULL;
  2413. unsigned long flags;
  2414. /* We are called by the error capture and reset at a random
  2415. * point in time. In particular, note that neither is crucially
  2416. * ordered with an interrupt. After a hang, the GPU is dead and we
  2417. * assume that no more writes can happen (we waited long enough for
  2418. * all writes that were in transaction to be flushed) - adding an
  2419. * extra delay for a recent interrupt is pointless. Hence, we do
  2420. * not need an engine->irq_seqno_barrier() before the seqno reads.
  2421. */
  2422. spin_lock_irqsave(&engine->timeline->lock, flags);
  2423. list_for_each_entry(request, &engine->timeline->requests, link) {
  2424. if (__i915_gem_request_completed(request,
  2425. request->global_seqno))
  2426. continue;
  2427. GEM_BUG_ON(request->engine != engine);
  2428. GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  2429. &request->fence.flags));
  2430. active = request;
  2431. break;
  2432. }
  2433. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2434. return active;
  2435. }
  2436. static bool engine_stalled(struct intel_engine_cs *engine)
  2437. {
  2438. if (!engine->hangcheck.stalled)
  2439. return false;
  2440. /* Check for possible seqno movement after hang declaration */
  2441. if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
  2442. DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
  2443. return false;
  2444. }
  2445. return true;
  2446. }
  2447. /*
  2448. * Ensure irq handler finishes, and not run again.
  2449. * Also return the active request so that we only search for it once.
  2450. */
  2451. struct drm_i915_gem_request *
  2452. i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
  2453. {
  2454. struct drm_i915_gem_request *request = NULL;
  2455. /*
  2456. * During the reset sequence, we must prevent the engine from
  2457. * entering RC6. As the context state is undefined until we restart
  2458. * the engine, if it does enter RC6 during the reset, the state
  2459. * written to the powercontext is undefined and so we may lose
  2460. * GPU state upon resume, i.e. fail to restart after a reset.
  2461. */
  2462. intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
  2463. /*
  2464. * Prevent the signaler thread from updating the request
  2465. * state (by calling dma_fence_signal) as we are processing
  2466. * the reset. The write from the GPU of the seqno is
  2467. * asynchronous and the signaler thread may see a different
  2468. * value to us and declare the request complete, even though
  2469. * the reset routine have picked that request as the active
  2470. * (incomplete) request. This conflict is not handled
  2471. * gracefully!
  2472. */
  2473. kthread_park(engine->breadcrumbs.signaler);
  2474. /*
  2475. * Prevent request submission to the hardware until we have
  2476. * completed the reset in i915_gem_reset_finish(). If a request
  2477. * is completed by one engine, it may then queue a request
  2478. * to a second via its execlists->tasklet *just* as we are
  2479. * calling engine->init_hw() and also writing the ELSP.
  2480. * Turning off the execlists->tasklet until the reset is over
  2481. * prevents the race.
  2482. */
  2483. tasklet_kill(&engine->execlists.tasklet);
  2484. tasklet_disable(&engine->execlists.tasklet);
  2485. /*
  2486. * We're using worker to queue preemption requests from the tasklet in
  2487. * GuC submission mode.
  2488. * Even though tasklet was disabled, we may still have a worker queued.
  2489. * Let's make sure that all workers scheduled before disabling the
  2490. * tasklet are completed before continuing with the reset.
  2491. */
  2492. if (engine->i915->guc.preempt_wq)
  2493. flush_workqueue(engine->i915->guc.preempt_wq);
  2494. if (engine->irq_seqno_barrier)
  2495. engine->irq_seqno_barrier(engine);
  2496. request = i915_gem_find_active_request(engine);
  2497. if (request && request->fence.error == -EIO)
  2498. request = ERR_PTR(-EIO); /* Previous reset failed! */
  2499. return request;
  2500. }
  2501. int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
  2502. {
  2503. struct intel_engine_cs *engine;
  2504. struct drm_i915_gem_request *request;
  2505. enum intel_engine_id id;
  2506. int err = 0;
  2507. for_each_engine(engine, dev_priv, id) {
  2508. request = i915_gem_reset_prepare_engine(engine);
  2509. if (IS_ERR(request)) {
  2510. err = PTR_ERR(request);
  2511. continue;
  2512. }
  2513. engine->hangcheck.active_request = request;
  2514. }
  2515. i915_gem_revoke_fences(dev_priv);
  2516. return err;
  2517. }
  2518. static void skip_request(struct drm_i915_gem_request *request)
  2519. {
  2520. void *vaddr = request->ring->vaddr;
  2521. u32 head;
  2522. /* As this request likely depends on state from the lost
  2523. * context, clear out all the user operations leaving the
  2524. * breadcrumb at the end (so we get the fence notifications).
  2525. */
  2526. head = request->head;
  2527. if (request->postfix < head) {
  2528. memset(vaddr + head, 0, request->ring->size - head);
  2529. head = 0;
  2530. }
  2531. memset(vaddr + head, 0, request->postfix - head);
  2532. dma_fence_set_error(&request->fence, -EIO);
  2533. }
  2534. static void engine_skip_context(struct drm_i915_gem_request *request)
  2535. {
  2536. struct intel_engine_cs *engine = request->engine;
  2537. struct i915_gem_context *hung_ctx = request->ctx;
  2538. struct intel_timeline *timeline;
  2539. unsigned long flags;
  2540. timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
  2541. spin_lock_irqsave(&engine->timeline->lock, flags);
  2542. spin_lock(&timeline->lock);
  2543. list_for_each_entry_continue(request, &engine->timeline->requests, link)
  2544. if (request->ctx == hung_ctx)
  2545. skip_request(request);
  2546. list_for_each_entry(request, &timeline->requests, link)
  2547. skip_request(request);
  2548. spin_unlock(&timeline->lock);
  2549. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2550. }
  2551. /* Returns the request if it was guilty of the hang */
  2552. static struct drm_i915_gem_request *
  2553. i915_gem_reset_request(struct intel_engine_cs *engine,
  2554. struct drm_i915_gem_request *request)
  2555. {
  2556. /* The guilty request will get skipped on a hung engine.
  2557. *
  2558. * Users of client default contexts do not rely on logical
  2559. * state preserved between batches so it is safe to execute
  2560. * queued requests following the hang. Non default contexts
  2561. * rely on preserved state, so skipping a batch loses the
  2562. * evolution of the state and it needs to be considered corrupted.
  2563. * Executing more queued batches on top of corrupted state is
  2564. * risky. But we take the risk by trying to advance through
  2565. * the queued requests in order to make the client behaviour
  2566. * more predictable around resets, by not throwing away random
  2567. * amount of batches it has prepared for execution. Sophisticated
  2568. * clients can use gem_reset_stats_ioctl and dma fence status
  2569. * (exported via sync_file info ioctl on explicit fences) to observe
  2570. * when it loses the context state and should rebuild accordingly.
  2571. *
  2572. * The context ban, and ultimately the client ban, mechanism are safety
  2573. * valves if client submission ends up resulting in nothing more than
  2574. * subsequent hangs.
  2575. */
  2576. if (engine_stalled(engine)) {
  2577. i915_gem_context_mark_guilty(request->ctx);
  2578. skip_request(request);
  2579. /* If this context is now banned, skip all pending requests. */
  2580. if (i915_gem_context_is_banned(request->ctx))
  2581. engine_skip_context(request);
  2582. } else {
  2583. /*
  2584. * Since this is not the hung engine, it may have advanced
  2585. * since the hang declaration. Double check by refinding
  2586. * the active request at the time of the reset.
  2587. */
  2588. request = i915_gem_find_active_request(engine);
  2589. if (request) {
  2590. i915_gem_context_mark_innocent(request->ctx);
  2591. dma_fence_set_error(&request->fence, -EAGAIN);
  2592. /* Rewind the engine to replay the incomplete rq */
  2593. spin_lock_irq(&engine->timeline->lock);
  2594. request = list_prev_entry(request, link);
  2595. if (&request->link == &engine->timeline->requests)
  2596. request = NULL;
  2597. spin_unlock_irq(&engine->timeline->lock);
  2598. }
  2599. }
  2600. return request;
  2601. }
  2602. void i915_gem_reset_engine(struct intel_engine_cs *engine,
  2603. struct drm_i915_gem_request *request)
  2604. {
  2605. /*
  2606. * Make sure this write is visible before we re-enable the interrupt
  2607. * handlers on another CPU, as tasklet_enable() resolves to just
  2608. * a compiler barrier which is insufficient for our purpose here.
  2609. */
  2610. smp_store_mb(engine->irq_posted, 0);
  2611. if (request)
  2612. request = i915_gem_reset_request(engine, request);
  2613. if (request) {
  2614. DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
  2615. engine->name, request->global_seqno);
  2616. }
  2617. /* Setup the CS to resume from the breadcrumb of the hung request */
  2618. engine->reset_hw(engine, request);
  2619. }
  2620. void i915_gem_reset(struct drm_i915_private *dev_priv)
  2621. {
  2622. struct intel_engine_cs *engine;
  2623. enum intel_engine_id id;
  2624. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2625. i915_gem_retire_requests(dev_priv);
  2626. for_each_engine(engine, dev_priv, id) {
  2627. struct i915_gem_context *ctx;
  2628. i915_gem_reset_engine(engine, engine->hangcheck.active_request);
  2629. ctx = fetch_and_zero(&engine->last_retired_context);
  2630. if (ctx)
  2631. engine->context_unpin(engine, ctx);
  2632. /*
  2633. * Ostensibily, we always want a context loaded for powersaving,
  2634. * so if the engine is idle after the reset, send a request
  2635. * to load our scratch kernel_context.
  2636. *
  2637. * More mysteriously, if we leave the engine idle after a reset,
  2638. * the next userspace batch may hang, with what appears to be
  2639. * an incoherent read by the CS (presumably stale TLB). An
  2640. * empty request appears sufficient to paper over the glitch.
  2641. */
  2642. if (list_empty(&engine->timeline->requests)) {
  2643. struct drm_i915_gem_request *rq;
  2644. rq = i915_gem_request_alloc(engine,
  2645. dev_priv->kernel_context);
  2646. if (!IS_ERR(rq))
  2647. __i915_add_request(rq, false);
  2648. }
  2649. }
  2650. i915_gem_restore_fences(dev_priv);
  2651. if (dev_priv->gt.awake) {
  2652. intel_sanitize_gt_powersave(dev_priv);
  2653. intel_enable_gt_powersave(dev_priv);
  2654. if (INTEL_GEN(dev_priv) >= 6)
  2655. gen6_rps_busy(dev_priv);
  2656. }
  2657. }
  2658. void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
  2659. {
  2660. tasklet_enable(&engine->execlists.tasklet);
  2661. kthread_unpark(engine->breadcrumbs.signaler);
  2662. intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
  2663. }
  2664. void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
  2665. {
  2666. struct intel_engine_cs *engine;
  2667. enum intel_engine_id id;
  2668. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2669. for_each_engine(engine, dev_priv, id) {
  2670. engine->hangcheck.active_request = NULL;
  2671. i915_gem_reset_finish_engine(engine);
  2672. }
  2673. }
  2674. static void nop_submit_request(struct drm_i915_gem_request *request)
  2675. {
  2676. dma_fence_set_error(&request->fence, -EIO);
  2677. i915_gem_request_submit(request);
  2678. }
  2679. static void nop_complete_submit_request(struct drm_i915_gem_request *request)
  2680. {
  2681. unsigned long flags;
  2682. dma_fence_set_error(&request->fence, -EIO);
  2683. spin_lock_irqsave(&request->engine->timeline->lock, flags);
  2684. __i915_gem_request_submit(request);
  2685. intel_engine_init_global_seqno(request->engine, request->global_seqno);
  2686. spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
  2687. }
  2688. void i915_gem_set_wedged(struct drm_i915_private *i915)
  2689. {
  2690. struct intel_engine_cs *engine;
  2691. enum intel_engine_id id;
  2692. /*
  2693. * First, stop submission to hw, but do not yet complete requests by
  2694. * rolling the global seqno forward (since this would complete requests
  2695. * for which we haven't set the fence error to EIO yet).
  2696. */
  2697. for_each_engine(engine, i915, id)
  2698. engine->submit_request = nop_submit_request;
  2699. /*
  2700. * Make sure no one is running the old callback before we proceed with
  2701. * cancelling requests and resetting the completion tracking. Otherwise
  2702. * we might submit a request to the hardware which never completes.
  2703. */
  2704. synchronize_rcu();
  2705. for_each_engine(engine, i915, id) {
  2706. /* Mark all executing requests as skipped */
  2707. engine->cancel_requests(engine);
  2708. /*
  2709. * Only once we've force-cancelled all in-flight requests can we
  2710. * start to complete all requests.
  2711. */
  2712. engine->submit_request = nop_complete_submit_request;
  2713. }
  2714. /*
  2715. * Make sure no request can slip through without getting completed by
  2716. * either this call here to intel_engine_init_global_seqno, or the one
  2717. * in nop_complete_submit_request.
  2718. */
  2719. synchronize_rcu();
  2720. for_each_engine(engine, i915, id) {
  2721. unsigned long flags;
  2722. /* Mark all pending requests as complete so that any concurrent
  2723. * (lockless) lookup doesn't try and wait upon the request as we
  2724. * reset it.
  2725. */
  2726. spin_lock_irqsave(&engine->timeline->lock, flags);
  2727. intel_engine_init_global_seqno(engine,
  2728. intel_engine_last_submit(engine));
  2729. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2730. }
  2731. set_bit(I915_WEDGED, &i915->gpu_error.flags);
  2732. wake_up_all(&i915->gpu_error.reset_queue);
  2733. }
  2734. bool i915_gem_unset_wedged(struct drm_i915_private *i915)
  2735. {
  2736. struct i915_gem_timeline *tl;
  2737. int i;
  2738. lockdep_assert_held(&i915->drm.struct_mutex);
  2739. if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
  2740. return true;
  2741. /* Before unwedging, make sure that all pending operations
  2742. * are flushed and errored out - we may have requests waiting upon
  2743. * third party fences. We marked all inflight requests as EIO, and
  2744. * every execbuf since returned EIO, for consistency we want all
  2745. * the currently pending requests to also be marked as EIO, which
  2746. * is done inside our nop_submit_request - and so we must wait.
  2747. *
  2748. * No more can be submitted until we reset the wedged bit.
  2749. */
  2750. list_for_each_entry(tl, &i915->gt.timelines, link) {
  2751. for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
  2752. struct drm_i915_gem_request *rq;
  2753. rq = i915_gem_active_peek(&tl->engine[i].last_request,
  2754. &i915->drm.struct_mutex);
  2755. if (!rq)
  2756. continue;
  2757. /* We can't use our normal waiter as we want to
  2758. * avoid recursively trying to handle the current
  2759. * reset. The basic dma_fence_default_wait() installs
  2760. * a callback for dma_fence_signal(), which is
  2761. * triggered by our nop handler (indirectly, the
  2762. * callback enables the signaler thread which is
  2763. * woken by the nop_submit_request() advancing the seqno
  2764. * and when the seqno passes the fence, the signaler
  2765. * then signals the fence waking us up).
  2766. */
  2767. if (dma_fence_default_wait(&rq->fence, true,
  2768. MAX_SCHEDULE_TIMEOUT) < 0)
  2769. return false;
  2770. }
  2771. }
  2772. /* Undo nop_submit_request. We prevent all new i915 requests from
  2773. * being queued (by disallowing execbuf whilst wedged) so having
  2774. * waited for all active requests above, we know the system is idle
  2775. * and do not have to worry about a thread being inside
  2776. * engine->submit_request() as we swap over. So unlike installing
  2777. * the nop_submit_request on reset, we can do this from normal
  2778. * context and do not require stop_machine().
  2779. */
  2780. intel_engines_reset_default_submission(i915);
  2781. i915_gem_contexts_lost(i915);
  2782. smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
  2783. clear_bit(I915_WEDGED, &i915->gpu_error.flags);
  2784. return true;
  2785. }
  2786. static void
  2787. i915_gem_retire_work_handler(struct work_struct *work)
  2788. {
  2789. struct drm_i915_private *dev_priv =
  2790. container_of(work, typeof(*dev_priv), gt.retire_work.work);
  2791. struct drm_device *dev = &dev_priv->drm;
  2792. /* Come back later if the device is busy... */
  2793. if (mutex_trylock(&dev->struct_mutex)) {
  2794. i915_gem_retire_requests(dev_priv);
  2795. mutex_unlock(&dev->struct_mutex);
  2796. }
  2797. /* Keep the retire handler running until we are finally idle.
  2798. * We do not need to do this test under locking as in the worst-case
  2799. * we queue the retire worker once too often.
  2800. */
  2801. if (READ_ONCE(dev_priv->gt.awake)) {
  2802. i915_queue_hangcheck(dev_priv);
  2803. queue_delayed_work(dev_priv->wq,
  2804. &dev_priv->gt.retire_work,
  2805. round_jiffies_up_relative(HZ));
  2806. }
  2807. }
  2808. static inline bool
  2809. new_requests_since_last_retire(const struct drm_i915_private *i915)
  2810. {
  2811. return (READ_ONCE(i915->gt.active_requests) ||
  2812. work_pending(&i915->gt.idle_work.work));
  2813. }
  2814. static void
  2815. i915_gem_idle_work_handler(struct work_struct *work)
  2816. {
  2817. struct drm_i915_private *dev_priv =
  2818. container_of(work, typeof(*dev_priv), gt.idle_work.work);
  2819. bool rearm_hangcheck;
  2820. ktime_t end;
  2821. if (!READ_ONCE(dev_priv->gt.awake))
  2822. return;
  2823. /*
  2824. * Wait for last execlists context complete, but bail out in case a
  2825. * new request is submitted.
  2826. */
  2827. end = ktime_add_ms(ktime_get(), I915_IDLE_ENGINES_TIMEOUT);
  2828. do {
  2829. if (new_requests_since_last_retire(dev_priv))
  2830. return;
  2831. if (intel_engines_are_idle(dev_priv))
  2832. break;
  2833. usleep_range(100, 500);
  2834. } while (ktime_before(ktime_get(), end));
  2835. rearm_hangcheck =
  2836. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  2837. if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
  2838. /* Currently busy, come back later */
  2839. mod_delayed_work(dev_priv->wq,
  2840. &dev_priv->gt.idle_work,
  2841. msecs_to_jiffies(50));
  2842. goto out_rearm;
  2843. }
  2844. /*
  2845. * New request retired after this work handler started, extend active
  2846. * period until next instance of the work.
  2847. */
  2848. if (new_requests_since_last_retire(dev_priv))
  2849. goto out_unlock;
  2850. /*
  2851. * Be paranoid and flush a concurrent interrupt to make sure
  2852. * we don't reactivate any irq tasklets after parking.
  2853. *
  2854. * FIXME: Note that even though we have waited for execlists to be idle,
  2855. * there may still be an in-flight interrupt even though the CSB
  2856. * is now empty. synchronize_irq() makes sure that a residual interrupt
  2857. * is completed before we continue, but it doesn't prevent the HW from
  2858. * raising a spurious interrupt later. To complete the shield we should
  2859. * coordinate disabling the CS irq with flushing the interrupts.
  2860. */
  2861. synchronize_irq(dev_priv->drm.irq);
  2862. intel_engines_park(dev_priv);
  2863. i915_gem_timelines_park(dev_priv);
  2864. i915_pmu_gt_parked(dev_priv);
  2865. GEM_BUG_ON(!dev_priv->gt.awake);
  2866. dev_priv->gt.awake = false;
  2867. rearm_hangcheck = false;
  2868. if (INTEL_GEN(dev_priv) >= 6)
  2869. gen6_rps_idle(dev_priv);
  2870. intel_display_power_put(dev_priv, POWER_DOMAIN_GT_IRQ);
  2871. intel_runtime_pm_put(dev_priv);
  2872. out_unlock:
  2873. mutex_unlock(&dev_priv->drm.struct_mutex);
  2874. out_rearm:
  2875. if (rearm_hangcheck) {
  2876. GEM_BUG_ON(!dev_priv->gt.awake);
  2877. i915_queue_hangcheck(dev_priv);
  2878. }
  2879. }
  2880. void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
  2881. {
  2882. struct drm_i915_private *i915 = to_i915(gem->dev);
  2883. struct drm_i915_gem_object *obj = to_intel_bo(gem);
  2884. struct drm_i915_file_private *fpriv = file->driver_priv;
  2885. struct i915_lut_handle *lut, *ln;
  2886. mutex_lock(&i915->drm.struct_mutex);
  2887. list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
  2888. struct i915_gem_context *ctx = lut->ctx;
  2889. struct i915_vma *vma;
  2890. GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
  2891. if (ctx->file_priv != fpriv)
  2892. continue;
  2893. vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
  2894. GEM_BUG_ON(vma->obj != obj);
  2895. /* We allow the process to have multiple handles to the same
  2896. * vma, in the same fd namespace, by virtue of flink/open.
  2897. */
  2898. GEM_BUG_ON(!vma->open_count);
  2899. if (!--vma->open_count && !i915_vma_is_ggtt(vma))
  2900. i915_vma_close(vma);
  2901. list_del(&lut->obj_link);
  2902. list_del(&lut->ctx_link);
  2903. kmem_cache_free(i915->luts, lut);
  2904. __i915_gem_object_release_unless_active(obj);
  2905. }
  2906. mutex_unlock(&i915->drm.struct_mutex);
  2907. }
  2908. static unsigned long to_wait_timeout(s64 timeout_ns)
  2909. {
  2910. if (timeout_ns < 0)
  2911. return MAX_SCHEDULE_TIMEOUT;
  2912. if (timeout_ns == 0)
  2913. return 0;
  2914. return nsecs_to_jiffies_timeout(timeout_ns);
  2915. }
  2916. /**
  2917. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2918. * @dev: drm device pointer
  2919. * @data: ioctl data blob
  2920. * @file: drm file pointer
  2921. *
  2922. * Returns 0 if successful, else an error is returned with the remaining time in
  2923. * the timeout parameter.
  2924. * -ETIME: object is still busy after timeout
  2925. * -ERESTARTSYS: signal interrupted the wait
  2926. * -ENONENT: object doesn't exist
  2927. * Also possible, but rare:
  2928. * -EAGAIN: incomplete, restart syscall
  2929. * -ENOMEM: damn
  2930. * -ENODEV: Internal IRQ fail
  2931. * -E?: The add request failed
  2932. *
  2933. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2934. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2935. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2936. * without holding struct_mutex the object may become re-busied before this
  2937. * function completes. A similar but shorter * race condition exists in the busy
  2938. * ioctl
  2939. */
  2940. int
  2941. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2942. {
  2943. struct drm_i915_gem_wait *args = data;
  2944. struct drm_i915_gem_object *obj;
  2945. ktime_t start;
  2946. long ret;
  2947. if (args->flags != 0)
  2948. return -EINVAL;
  2949. obj = i915_gem_object_lookup(file, args->bo_handle);
  2950. if (!obj)
  2951. return -ENOENT;
  2952. start = ktime_get();
  2953. ret = i915_gem_object_wait(obj,
  2954. I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
  2955. to_wait_timeout(args->timeout_ns),
  2956. to_rps_client(file));
  2957. if (args->timeout_ns > 0) {
  2958. args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
  2959. if (args->timeout_ns < 0)
  2960. args->timeout_ns = 0;
  2961. /*
  2962. * Apparently ktime isn't accurate enough and occasionally has a
  2963. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  2964. * things up to make the test happy. We allow up to 1 jiffy.
  2965. *
  2966. * This is a regression from the timespec->ktime conversion.
  2967. */
  2968. if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
  2969. args->timeout_ns = 0;
  2970. /* Asked to wait beyond the jiffie/scheduler precision? */
  2971. if (ret == -ETIME && args->timeout_ns)
  2972. ret = -EAGAIN;
  2973. }
  2974. i915_gem_object_put(obj);
  2975. return ret;
  2976. }
  2977. static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
  2978. {
  2979. int ret, i;
  2980. for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
  2981. ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
  2982. if (ret)
  2983. return ret;
  2984. }
  2985. return 0;
  2986. }
  2987. static int wait_for_engines(struct drm_i915_private *i915)
  2988. {
  2989. if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
  2990. dev_err(i915->drm.dev,
  2991. "Failed to idle engines, declaring wedged!\n");
  2992. if (drm_debug & DRM_UT_DRIVER) {
  2993. struct drm_printer p = drm_debug_printer(__func__);
  2994. struct intel_engine_cs *engine;
  2995. enum intel_engine_id id;
  2996. for_each_engine(engine, i915, id)
  2997. intel_engine_dump(engine, &p,
  2998. "%s", engine->name);
  2999. }
  3000. i915_gem_set_wedged(i915);
  3001. return -EIO;
  3002. }
  3003. return 0;
  3004. }
  3005. int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
  3006. {
  3007. int ret;
  3008. /* If the device is asleep, we have no requests outstanding */
  3009. if (!READ_ONCE(i915->gt.awake))
  3010. return 0;
  3011. if (flags & I915_WAIT_LOCKED) {
  3012. struct i915_gem_timeline *tl;
  3013. lockdep_assert_held(&i915->drm.struct_mutex);
  3014. list_for_each_entry(tl, &i915->gt.timelines, link) {
  3015. ret = wait_for_timeline(tl, flags);
  3016. if (ret)
  3017. return ret;
  3018. }
  3019. i915_gem_retire_requests(i915);
  3020. ret = wait_for_engines(i915);
  3021. } else {
  3022. ret = wait_for_timeline(&i915->gt.global_timeline, flags);
  3023. }
  3024. return ret;
  3025. }
  3026. static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
  3027. {
  3028. /*
  3029. * We manually flush the CPU domain so that we can override and
  3030. * force the flush for the display, and perform it asyncrhonously.
  3031. */
  3032. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  3033. if (obj->cache_dirty)
  3034. i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
  3035. obj->base.write_domain = 0;
  3036. }
  3037. void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
  3038. {
  3039. if (!READ_ONCE(obj->pin_global))
  3040. return;
  3041. mutex_lock(&obj->base.dev->struct_mutex);
  3042. __i915_gem_object_flush_for_display(obj);
  3043. mutex_unlock(&obj->base.dev->struct_mutex);
  3044. }
  3045. /**
  3046. * Moves a single object to the WC read, and possibly write domain.
  3047. * @obj: object to act on
  3048. * @write: ask for write access or read only
  3049. *
  3050. * This function returns when the move is complete, including waiting on
  3051. * flushes to occur.
  3052. */
  3053. int
  3054. i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
  3055. {
  3056. int ret;
  3057. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3058. ret = i915_gem_object_wait(obj,
  3059. I915_WAIT_INTERRUPTIBLE |
  3060. I915_WAIT_LOCKED |
  3061. (write ? I915_WAIT_ALL : 0),
  3062. MAX_SCHEDULE_TIMEOUT,
  3063. NULL);
  3064. if (ret)
  3065. return ret;
  3066. if (obj->base.write_domain == I915_GEM_DOMAIN_WC)
  3067. return 0;
  3068. /* Flush and acquire obj->pages so that we are coherent through
  3069. * direct access in memory with previous cached writes through
  3070. * shmemfs and that our cache domain tracking remains valid.
  3071. * For example, if the obj->filp was moved to swap without us
  3072. * being notified and releasing the pages, we would mistakenly
  3073. * continue to assume that the obj remained out of the CPU cached
  3074. * domain.
  3075. */
  3076. ret = i915_gem_object_pin_pages(obj);
  3077. if (ret)
  3078. return ret;
  3079. flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
  3080. /* Serialise direct access to this object with the barriers for
  3081. * coherent writes from the GPU, by effectively invalidating the
  3082. * WC domain upon first access.
  3083. */
  3084. if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0)
  3085. mb();
  3086. /* It should now be out of any other write domains, and we can update
  3087. * the domain values for our changes.
  3088. */
  3089. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0);
  3090. obj->base.read_domains |= I915_GEM_DOMAIN_WC;
  3091. if (write) {
  3092. obj->base.read_domains = I915_GEM_DOMAIN_WC;
  3093. obj->base.write_domain = I915_GEM_DOMAIN_WC;
  3094. obj->mm.dirty = true;
  3095. }
  3096. i915_gem_object_unpin_pages(obj);
  3097. return 0;
  3098. }
  3099. /**
  3100. * Moves a single object to the GTT read, and possibly write domain.
  3101. * @obj: object to act on
  3102. * @write: ask for write access or read only
  3103. *
  3104. * This function returns when the move is complete, including waiting on
  3105. * flushes to occur.
  3106. */
  3107. int
  3108. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  3109. {
  3110. int ret;
  3111. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3112. ret = i915_gem_object_wait(obj,
  3113. I915_WAIT_INTERRUPTIBLE |
  3114. I915_WAIT_LOCKED |
  3115. (write ? I915_WAIT_ALL : 0),
  3116. MAX_SCHEDULE_TIMEOUT,
  3117. NULL);
  3118. if (ret)
  3119. return ret;
  3120. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  3121. return 0;
  3122. /* Flush and acquire obj->pages so that we are coherent through
  3123. * direct access in memory with previous cached writes through
  3124. * shmemfs and that our cache domain tracking remains valid.
  3125. * For example, if the obj->filp was moved to swap without us
  3126. * being notified and releasing the pages, we would mistakenly
  3127. * continue to assume that the obj remained out of the CPU cached
  3128. * domain.
  3129. */
  3130. ret = i915_gem_object_pin_pages(obj);
  3131. if (ret)
  3132. return ret;
  3133. flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
  3134. /* Serialise direct access to this object with the barriers for
  3135. * coherent writes from the GPU, by effectively invalidating the
  3136. * GTT domain upon first access.
  3137. */
  3138. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3139. mb();
  3140. /* It should now be out of any other write domains, and we can update
  3141. * the domain values for our changes.
  3142. */
  3143. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  3144. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3145. if (write) {
  3146. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  3147. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  3148. obj->mm.dirty = true;
  3149. }
  3150. i915_gem_object_unpin_pages(obj);
  3151. return 0;
  3152. }
  3153. /**
  3154. * Changes the cache-level of an object across all VMA.
  3155. * @obj: object to act on
  3156. * @cache_level: new cache level to set for the object
  3157. *
  3158. * After this function returns, the object will be in the new cache-level
  3159. * across all GTT and the contents of the backing storage will be coherent,
  3160. * with respect to the new cache-level. In order to keep the backing storage
  3161. * coherent for all users, we only allow a single cache level to be set
  3162. * globally on the object and prevent it from being changed whilst the
  3163. * hardware is reading from the object. That is if the object is currently
  3164. * on the scanout it will be set to uncached (or equivalent display
  3165. * cache coherency) and all non-MOCS GPU access will also be uncached so
  3166. * that all direct access to the scanout remains coherent.
  3167. */
  3168. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3169. enum i915_cache_level cache_level)
  3170. {
  3171. struct i915_vma *vma;
  3172. int ret;
  3173. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3174. if (obj->cache_level == cache_level)
  3175. return 0;
  3176. /* Inspect the list of currently bound VMA and unbind any that would
  3177. * be invalid given the new cache-level. This is principally to
  3178. * catch the issue of the CS prefetch crossing page boundaries and
  3179. * reading an invalid PTE on older architectures.
  3180. */
  3181. restart:
  3182. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3183. if (!drm_mm_node_allocated(&vma->node))
  3184. continue;
  3185. if (i915_vma_is_pinned(vma)) {
  3186. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3187. return -EBUSY;
  3188. }
  3189. if (!i915_vma_is_closed(vma) &&
  3190. i915_gem_valid_gtt_space(vma, cache_level))
  3191. continue;
  3192. ret = i915_vma_unbind(vma);
  3193. if (ret)
  3194. return ret;
  3195. /* As unbinding may affect other elements in the
  3196. * obj->vma_list (due to side-effects from retiring
  3197. * an active vma), play safe and restart the iterator.
  3198. */
  3199. goto restart;
  3200. }
  3201. /* We can reuse the existing drm_mm nodes but need to change the
  3202. * cache-level on the PTE. We could simply unbind them all and
  3203. * rebind with the correct cache-level on next use. However since
  3204. * we already have a valid slot, dma mapping, pages etc, we may as
  3205. * rewrite the PTE in the belief that doing so tramples upon less
  3206. * state and so involves less work.
  3207. */
  3208. if (obj->bind_count) {
  3209. /* Before we change the PTE, the GPU must not be accessing it.
  3210. * If we wait upon the object, we know that all the bound
  3211. * VMA are no longer active.
  3212. */
  3213. ret = i915_gem_object_wait(obj,
  3214. I915_WAIT_INTERRUPTIBLE |
  3215. I915_WAIT_LOCKED |
  3216. I915_WAIT_ALL,
  3217. MAX_SCHEDULE_TIMEOUT,
  3218. NULL);
  3219. if (ret)
  3220. return ret;
  3221. if (!HAS_LLC(to_i915(obj->base.dev)) &&
  3222. cache_level != I915_CACHE_NONE) {
  3223. /* Access to snoopable pages through the GTT is
  3224. * incoherent and on some machines causes a hard
  3225. * lockup. Relinquish the CPU mmaping to force
  3226. * userspace to refault in the pages and we can
  3227. * then double check if the GTT mapping is still
  3228. * valid for that pointer access.
  3229. */
  3230. i915_gem_release_mmap(obj);
  3231. /* As we no longer need a fence for GTT access,
  3232. * we can relinquish it now (and so prevent having
  3233. * to steal a fence from someone else on the next
  3234. * fence request). Note GPU activity would have
  3235. * dropped the fence as all snoopable access is
  3236. * supposed to be linear.
  3237. */
  3238. for_each_ggtt_vma(vma, obj) {
  3239. ret = i915_vma_put_fence(vma);
  3240. if (ret)
  3241. return ret;
  3242. }
  3243. } else {
  3244. /* We either have incoherent backing store and
  3245. * so no GTT access or the architecture is fully
  3246. * coherent. In such cases, existing GTT mmaps
  3247. * ignore the cache bit in the PTE and we can
  3248. * rewrite it without confusing the GPU or having
  3249. * to force userspace to fault back in its mmaps.
  3250. */
  3251. }
  3252. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3253. if (!drm_mm_node_allocated(&vma->node))
  3254. continue;
  3255. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  3256. if (ret)
  3257. return ret;
  3258. }
  3259. }
  3260. list_for_each_entry(vma, &obj->vma_list, obj_link)
  3261. vma->node.color = cache_level;
  3262. i915_gem_object_set_cache_coherency(obj, cache_level);
  3263. obj->cache_dirty = true; /* Always invalidate stale cachelines */
  3264. return 0;
  3265. }
  3266. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3267. struct drm_file *file)
  3268. {
  3269. struct drm_i915_gem_caching *args = data;
  3270. struct drm_i915_gem_object *obj;
  3271. int err = 0;
  3272. rcu_read_lock();
  3273. obj = i915_gem_object_lookup_rcu(file, args->handle);
  3274. if (!obj) {
  3275. err = -ENOENT;
  3276. goto out;
  3277. }
  3278. switch (obj->cache_level) {
  3279. case I915_CACHE_LLC:
  3280. case I915_CACHE_L3_LLC:
  3281. args->caching = I915_CACHING_CACHED;
  3282. break;
  3283. case I915_CACHE_WT:
  3284. args->caching = I915_CACHING_DISPLAY;
  3285. break;
  3286. default:
  3287. args->caching = I915_CACHING_NONE;
  3288. break;
  3289. }
  3290. out:
  3291. rcu_read_unlock();
  3292. return err;
  3293. }
  3294. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3295. struct drm_file *file)
  3296. {
  3297. struct drm_i915_private *i915 = to_i915(dev);
  3298. struct drm_i915_gem_caching *args = data;
  3299. struct drm_i915_gem_object *obj;
  3300. enum i915_cache_level level;
  3301. int ret = 0;
  3302. switch (args->caching) {
  3303. case I915_CACHING_NONE:
  3304. level = I915_CACHE_NONE;
  3305. break;
  3306. case I915_CACHING_CACHED:
  3307. /*
  3308. * Due to a HW issue on BXT A stepping, GPU stores via a
  3309. * snooped mapping may leave stale data in a corresponding CPU
  3310. * cacheline, whereas normally such cachelines would get
  3311. * invalidated.
  3312. */
  3313. if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
  3314. return -ENODEV;
  3315. level = I915_CACHE_LLC;
  3316. break;
  3317. case I915_CACHING_DISPLAY:
  3318. level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
  3319. break;
  3320. default:
  3321. return -EINVAL;
  3322. }
  3323. obj = i915_gem_object_lookup(file, args->handle);
  3324. if (!obj)
  3325. return -ENOENT;
  3326. /*
  3327. * The caching mode of proxy object is handled by its generator, and
  3328. * not allowed to be changed by userspace.
  3329. */
  3330. if (i915_gem_object_is_proxy(obj)) {
  3331. ret = -ENXIO;
  3332. goto out;
  3333. }
  3334. if (obj->cache_level == level)
  3335. goto out;
  3336. ret = i915_gem_object_wait(obj,
  3337. I915_WAIT_INTERRUPTIBLE,
  3338. MAX_SCHEDULE_TIMEOUT,
  3339. to_rps_client(file));
  3340. if (ret)
  3341. goto out;
  3342. ret = i915_mutex_lock_interruptible(dev);
  3343. if (ret)
  3344. goto out;
  3345. ret = i915_gem_object_set_cache_level(obj, level);
  3346. mutex_unlock(&dev->struct_mutex);
  3347. out:
  3348. i915_gem_object_put(obj);
  3349. return ret;
  3350. }
  3351. /*
  3352. * Prepare buffer for display plane (scanout, cursors, etc).
  3353. * Can be called from an uninterruptible phase (modesetting) and allows
  3354. * any flushes to be pipelined (for pageflips).
  3355. */
  3356. struct i915_vma *
  3357. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3358. u32 alignment,
  3359. const struct i915_ggtt_view *view)
  3360. {
  3361. struct i915_vma *vma;
  3362. int ret;
  3363. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3364. /* Mark the global pin early so that we account for the
  3365. * display coherency whilst setting up the cache domains.
  3366. */
  3367. obj->pin_global++;
  3368. /* The display engine is not coherent with the LLC cache on gen6. As
  3369. * a result, we make sure that the pinning that is about to occur is
  3370. * done with uncached PTEs. This is lowest common denominator for all
  3371. * chipsets.
  3372. *
  3373. * However for gen6+, we could do better by using the GFDT bit instead
  3374. * of uncaching, which would allow us to flush all the LLC-cached data
  3375. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3376. */
  3377. ret = i915_gem_object_set_cache_level(obj,
  3378. HAS_WT(to_i915(obj->base.dev)) ?
  3379. I915_CACHE_WT : I915_CACHE_NONE);
  3380. if (ret) {
  3381. vma = ERR_PTR(ret);
  3382. goto err_unpin_global;
  3383. }
  3384. /* As the user may map the buffer once pinned in the display plane
  3385. * (e.g. libkms for the bootup splash), we have to ensure that we
  3386. * always use map_and_fenceable for all scanout buffers. However,
  3387. * it may simply be too big to fit into mappable, in which case
  3388. * put it anyway and hope that userspace can cope (but always first
  3389. * try to preserve the existing ABI).
  3390. */
  3391. vma = ERR_PTR(-ENOSPC);
  3392. if (!view || view->type == I915_GGTT_VIEW_NORMAL)
  3393. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
  3394. PIN_MAPPABLE | PIN_NONBLOCK);
  3395. if (IS_ERR(vma)) {
  3396. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  3397. unsigned int flags;
  3398. /* Valleyview is definitely limited to scanning out the first
  3399. * 512MiB. Lets presume this behaviour was inherited from the
  3400. * g4x display engine and that all earlier gen are similarly
  3401. * limited. Testing suggests that it is a little more
  3402. * complicated than this. For example, Cherryview appears quite
  3403. * happy to scanout from anywhere within its global aperture.
  3404. */
  3405. flags = 0;
  3406. if (HAS_GMCH_DISPLAY(i915))
  3407. flags = PIN_MAPPABLE;
  3408. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
  3409. }
  3410. if (IS_ERR(vma))
  3411. goto err_unpin_global;
  3412. vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
  3413. /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
  3414. __i915_gem_object_flush_for_display(obj);
  3415. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  3416. /* It should now be out of any other write domains, and we can update
  3417. * the domain values for our changes.
  3418. */
  3419. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3420. return vma;
  3421. err_unpin_global:
  3422. obj->pin_global--;
  3423. return vma;
  3424. }
  3425. void
  3426. i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
  3427. {
  3428. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  3429. if (WARN_ON(vma->obj->pin_global == 0))
  3430. return;
  3431. if (--vma->obj->pin_global == 0)
  3432. vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
  3433. /* Bump the LRU to try and avoid premature eviction whilst flipping */
  3434. i915_gem_object_bump_inactive_ggtt(vma->obj);
  3435. i915_vma_unpin(vma);
  3436. }
  3437. /**
  3438. * Moves a single object to the CPU read, and possibly write domain.
  3439. * @obj: object to act on
  3440. * @write: requesting write or read-only access
  3441. *
  3442. * This function returns when the move is complete, including waiting on
  3443. * flushes to occur.
  3444. */
  3445. int
  3446. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3447. {
  3448. int ret;
  3449. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3450. ret = i915_gem_object_wait(obj,
  3451. I915_WAIT_INTERRUPTIBLE |
  3452. I915_WAIT_LOCKED |
  3453. (write ? I915_WAIT_ALL : 0),
  3454. MAX_SCHEDULE_TIMEOUT,
  3455. NULL);
  3456. if (ret)
  3457. return ret;
  3458. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  3459. /* Flush the CPU cache if it's still invalid. */
  3460. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3461. i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
  3462. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3463. }
  3464. /* It should now be out of any other write domains, and we can update
  3465. * the domain values for our changes.
  3466. */
  3467. GEM_BUG_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  3468. /* If we're writing through the CPU, then the GPU read domains will
  3469. * need to be invalidated at next use.
  3470. */
  3471. if (write)
  3472. __start_cpu_write(obj);
  3473. return 0;
  3474. }
  3475. /* Throttle our rendering by waiting until the ring has completed our requests
  3476. * emitted over 20 msec ago.
  3477. *
  3478. * Note that if we were to use the current jiffies each time around the loop,
  3479. * we wouldn't escape the function with any frames outstanding if the time to
  3480. * render a frame was over 20ms.
  3481. *
  3482. * This should get us reasonable parallelism between CPU and GPU but also
  3483. * relatively low latency when blocking on a particular request to finish.
  3484. */
  3485. static int
  3486. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3487. {
  3488. struct drm_i915_private *dev_priv = to_i915(dev);
  3489. struct drm_i915_file_private *file_priv = file->driver_priv;
  3490. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3491. struct drm_i915_gem_request *request, *target = NULL;
  3492. long ret;
  3493. /* ABI: return -EIO if already wedged */
  3494. if (i915_terminally_wedged(&dev_priv->gpu_error))
  3495. return -EIO;
  3496. spin_lock(&file_priv->mm.lock);
  3497. list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
  3498. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3499. break;
  3500. if (target) {
  3501. list_del(&target->client_link);
  3502. target->file_priv = NULL;
  3503. }
  3504. target = request;
  3505. }
  3506. if (target)
  3507. i915_gem_request_get(target);
  3508. spin_unlock(&file_priv->mm.lock);
  3509. if (target == NULL)
  3510. return 0;
  3511. ret = i915_wait_request(target,
  3512. I915_WAIT_INTERRUPTIBLE,
  3513. MAX_SCHEDULE_TIMEOUT);
  3514. i915_gem_request_put(target);
  3515. return ret < 0 ? ret : 0;
  3516. }
  3517. struct i915_vma *
  3518. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3519. const struct i915_ggtt_view *view,
  3520. u64 size,
  3521. u64 alignment,
  3522. u64 flags)
  3523. {
  3524. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  3525. struct i915_address_space *vm = &dev_priv->ggtt.base;
  3526. struct i915_vma *vma;
  3527. int ret;
  3528. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3529. if (!view && flags & PIN_MAPPABLE) {
  3530. /* If the required space is larger than the available
  3531. * aperture, we will not able to find a slot for the
  3532. * object and unbinding the object now will be in
  3533. * vain. Worse, doing so may cause us to ping-pong
  3534. * the object in and out of the Global GTT and
  3535. * waste a lot of cycles under the mutex.
  3536. */
  3537. if (obj->base.size > dev_priv->ggtt.mappable_end)
  3538. return ERR_PTR(-E2BIG);
  3539. /* If NONBLOCK is set the caller is optimistically
  3540. * trying to cache the full object within the mappable
  3541. * aperture, and *must* have a fallback in place for
  3542. * situations where we cannot bind the object. We
  3543. * can be a little more lax here and use the fallback
  3544. * more often to avoid costly migrations of ourselves
  3545. * and other objects within the aperture.
  3546. *
  3547. * Half-the-aperture is used as a simple heuristic.
  3548. * More interesting would to do search for a free
  3549. * block prior to making the commitment to unbind.
  3550. * That caters for the self-harm case, and with a
  3551. * little more heuristics (e.g. NOFAULT, NOEVICT)
  3552. * we could try to minimise harm to others.
  3553. */
  3554. if (flags & PIN_NONBLOCK &&
  3555. obj->base.size > dev_priv->ggtt.mappable_end / 2)
  3556. return ERR_PTR(-ENOSPC);
  3557. }
  3558. vma = i915_vma_instance(obj, vm, view);
  3559. if (unlikely(IS_ERR(vma)))
  3560. return vma;
  3561. if (i915_vma_misplaced(vma, size, alignment, flags)) {
  3562. if (flags & PIN_NONBLOCK) {
  3563. if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
  3564. return ERR_PTR(-ENOSPC);
  3565. if (flags & PIN_MAPPABLE &&
  3566. vma->fence_size > dev_priv->ggtt.mappable_end / 2)
  3567. return ERR_PTR(-ENOSPC);
  3568. }
  3569. WARN(i915_vma_is_pinned(vma),
  3570. "bo is already pinned in ggtt with incorrect alignment:"
  3571. " offset=%08x, req.alignment=%llx,"
  3572. " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
  3573. i915_ggtt_offset(vma), alignment,
  3574. !!(flags & PIN_MAPPABLE),
  3575. i915_vma_is_map_and_fenceable(vma));
  3576. ret = i915_vma_unbind(vma);
  3577. if (ret)
  3578. return ERR_PTR(ret);
  3579. }
  3580. ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
  3581. if (ret)
  3582. return ERR_PTR(ret);
  3583. return vma;
  3584. }
  3585. static __always_inline unsigned int __busy_read_flag(unsigned int id)
  3586. {
  3587. /* Note that we could alias engines in the execbuf API, but
  3588. * that would be very unwise as it prevents userspace from
  3589. * fine control over engine selection. Ahem.
  3590. *
  3591. * This should be something like EXEC_MAX_ENGINE instead of
  3592. * I915_NUM_ENGINES.
  3593. */
  3594. BUILD_BUG_ON(I915_NUM_ENGINES > 16);
  3595. return 0x10000 << id;
  3596. }
  3597. static __always_inline unsigned int __busy_write_id(unsigned int id)
  3598. {
  3599. /* The uABI guarantees an active writer is also amongst the read
  3600. * engines. This would be true if we accessed the activity tracking
  3601. * under the lock, but as we perform the lookup of the object and
  3602. * its activity locklessly we can not guarantee that the last_write
  3603. * being active implies that we have set the same engine flag from
  3604. * last_read - hence we always set both read and write busy for
  3605. * last_write.
  3606. */
  3607. return id | __busy_read_flag(id);
  3608. }
  3609. static __always_inline unsigned int
  3610. __busy_set_if_active(const struct dma_fence *fence,
  3611. unsigned int (*flag)(unsigned int id))
  3612. {
  3613. struct drm_i915_gem_request *rq;
  3614. /* We have to check the current hw status of the fence as the uABI
  3615. * guarantees forward progress. We could rely on the idle worker
  3616. * to eventually flush us, but to minimise latency just ask the
  3617. * hardware.
  3618. *
  3619. * Note we only report on the status of native fences.
  3620. */
  3621. if (!dma_fence_is_i915(fence))
  3622. return 0;
  3623. /* opencode to_request() in order to avoid const warnings */
  3624. rq = container_of(fence, struct drm_i915_gem_request, fence);
  3625. if (i915_gem_request_completed(rq))
  3626. return 0;
  3627. return flag(rq->engine->uabi_id);
  3628. }
  3629. static __always_inline unsigned int
  3630. busy_check_reader(const struct dma_fence *fence)
  3631. {
  3632. return __busy_set_if_active(fence, __busy_read_flag);
  3633. }
  3634. static __always_inline unsigned int
  3635. busy_check_writer(const struct dma_fence *fence)
  3636. {
  3637. if (!fence)
  3638. return 0;
  3639. return __busy_set_if_active(fence, __busy_write_id);
  3640. }
  3641. int
  3642. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3643. struct drm_file *file)
  3644. {
  3645. struct drm_i915_gem_busy *args = data;
  3646. struct drm_i915_gem_object *obj;
  3647. struct reservation_object_list *list;
  3648. unsigned int seq;
  3649. int err;
  3650. err = -ENOENT;
  3651. rcu_read_lock();
  3652. obj = i915_gem_object_lookup_rcu(file, args->handle);
  3653. if (!obj)
  3654. goto out;
  3655. /* A discrepancy here is that we do not report the status of
  3656. * non-i915 fences, i.e. even though we may report the object as idle,
  3657. * a call to set-domain may still stall waiting for foreign rendering.
  3658. * This also means that wait-ioctl may report an object as busy,
  3659. * where busy-ioctl considers it idle.
  3660. *
  3661. * We trade the ability to warn of foreign fences to report on which
  3662. * i915 engines are active for the object.
  3663. *
  3664. * Alternatively, we can trade that extra information on read/write
  3665. * activity with
  3666. * args->busy =
  3667. * !reservation_object_test_signaled_rcu(obj->resv, true);
  3668. * to report the overall busyness. This is what the wait-ioctl does.
  3669. *
  3670. */
  3671. retry:
  3672. seq = raw_read_seqcount(&obj->resv->seq);
  3673. /* Translate the exclusive fence to the READ *and* WRITE engine */
  3674. args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
  3675. /* Translate shared fences to READ set of engines */
  3676. list = rcu_dereference(obj->resv->fence);
  3677. if (list) {
  3678. unsigned int shared_count = list->shared_count, i;
  3679. for (i = 0; i < shared_count; ++i) {
  3680. struct dma_fence *fence =
  3681. rcu_dereference(list->shared[i]);
  3682. args->busy |= busy_check_reader(fence);
  3683. }
  3684. }
  3685. if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
  3686. goto retry;
  3687. err = 0;
  3688. out:
  3689. rcu_read_unlock();
  3690. return err;
  3691. }
  3692. int
  3693. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3694. struct drm_file *file_priv)
  3695. {
  3696. return i915_gem_ring_throttle(dev, file_priv);
  3697. }
  3698. int
  3699. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3700. struct drm_file *file_priv)
  3701. {
  3702. struct drm_i915_private *dev_priv = to_i915(dev);
  3703. struct drm_i915_gem_madvise *args = data;
  3704. struct drm_i915_gem_object *obj;
  3705. int err;
  3706. switch (args->madv) {
  3707. case I915_MADV_DONTNEED:
  3708. case I915_MADV_WILLNEED:
  3709. break;
  3710. default:
  3711. return -EINVAL;
  3712. }
  3713. obj = i915_gem_object_lookup(file_priv, args->handle);
  3714. if (!obj)
  3715. return -ENOENT;
  3716. err = mutex_lock_interruptible(&obj->mm.lock);
  3717. if (err)
  3718. goto out;
  3719. if (i915_gem_object_has_pages(obj) &&
  3720. i915_gem_object_is_tiled(obj) &&
  3721. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3722. if (obj->mm.madv == I915_MADV_WILLNEED) {
  3723. GEM_BUG_ON(!obj->mm.quirked);
  3724. __i915_gem_object_unpin_pages(obj);
  3725. obj->mm.quirked = false;
  3726. }
  3727. if (args->madv == I915_MADV_WILLNEED) {
  3728. GEM_BUG_ON(obj->mm.quirked);
  3729. __i915_gem_object_pin_pages(obj);
  3730. obj->mm.quirked = true;
  3731. }
  3732. }
  3733. if (obj->mm.madv != __I915_MADV_PURGED)
  3734. obj->mm.madv = args->madv;
  3735. /* if the object is no longer attached, discard its backing storage */
  3736. if (obj->mm.madv == I915_MADV_DONTNEED &&
  3737. !i915_gem_object_has_pages(obj))
  3738. i915_gem_object_truncate(obj);
  3739. args->retained = obj->mm.madv != __I915_MADV_PURGED;
  3740. mutex_unlock(&obj->mm.lock);
  3741. out:
  3742. i915_gem_object_put(obj);
  3743. return err;
  3744. }
  3745. static void
  3746. frontbuffer_retire(struct i915_gem_active *active,
  3747. struct drm_i915_gem_request *request)
  3748. {
  3749. struct drm_i915_gem_object *obj =
  3750. container_of(active, typeof(*obj), frontbuffer_write);
  3751. intel_fb_obj_flush(obj, ORIGIN_CS);
  3752. }
  3753. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3754. const struct drm_i915_gem_object_ops *ops)
  3755. {
  3756. mutex_init(&obj->mm.lock);
  3757. INIT_LIST_HEAD(&obj->vma_list);
  3758. INIT_LIST_HEAD(&obj->lut_list);
  3759. INIT_LIST_HEAD(&obj->batch_pool_link);
  3760. obj->ops = ops;
  3761. reservation_object_init(&obj->__builtin_resv);
  3762. obj->resv = &obj->__builtin_resv;
  3763. obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
  3764. init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
  3765. obj->mm.madv = I915_MADV_WILLNEED;
  3766. INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
  3767. mutex_init(&obj->mm.get_page.lock);
  3768. i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
  3769. }
  3770. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3771. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
  3772. I915_GEM_OBJECT_IS_SHRINKABLE,
  3773. .get_pages = i915_gem_object_get_pages_gtt,
  3774. .put_pages = i915_gem_object_put_pages_gtt,
  3775. .pwrite = i915_gem_object_pwrite_gtt,
  3776. };
  3777. static int i915_gem_object_create_shmem(struct drm_device *dev,
  3778. struct drm_gem_object *obj,
  3779. size_t size)
  3780. {
  3781. struct drm_i915_private *i915 = to_i915(dev);
  3782. unsigned long flags = VM_NORESERVE;
  3783. struct file *filp;
  3784. drm_gem_private_object_init(dev, obj, size);
  3785. if (i915->mm.gemfs)
  3786. filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
  3787. flags);
  3788. else
  3789. filp = shmem_file_setup("i915", size, flags);
  3790. if (IS_ERR(filp))
  3791. return PTR_ERR(filp);
  3792. obj->filp = filp;
  3793. return 0;
  3794. }
  3795. struct drm_i915_gem_object *
  3796. i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
  3797. {
  3798. struct drm_i915_gem_object *obj;
  3799. struct address_space *mapping;
  3800. unsigned int cache_level;
  3801. gfp_t mask;
  3802. int ret;
  3803. /* There is a prevalence of the assumption that we fit the object's
  3804. * page count inside a 32bit _signed_ variable. Let's document this and
  3805. * catch if we ever need to fix it. In the meantime, if you do spot
  3806. * such a local variable, please consider fixing!
  3807. */
  3808. if (size >> PAGE_SHIFT > INT_MAX)
  3809. return ERR_PTR(-E2BIG);
  3810. if (overflows_type(size, obj->base.size))
  3811. return ERR_PTR(-E2BIG);
  3812. obj = i915_gem_object_alloc(dev_priv);
  3813. if (obj == NULL)
  3814. return ERR_PTR(-ENOMEM);
  3815. ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
  3816. if (ret)
  3817. goto fail;
  3818. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3819. if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
  3820. /* 965gm cannot relocate objects above 4GiB. */
  3821. mask &= ~__GFP_HIGHMEM;
  3822. mask |= __GFP_DMA32;
  3823. }
  3824. mapping = obj->base.filp->f_mapping;
  3825. mapping_set_gfp_mask(mapping, mask);
  3826. GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
  3827. i915_gem_object_init(obj, &i915_gem_object_ops);
  3828. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3829. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3830. if (HAS_LLC(dev_priv))
  3831. /* On some devices, we can have the GPU use the LLC (the CPU
  3832. * cache) for about a 10% performance improvement
  3833. * compared to uncached. Graphics requests other than
  3834. * display scanout are coherent with the CPU in
  3835. * accessing this cache. This means in this mode we
  3836. * don't need to clflush on the CPU side, and on the
  3837. * GPU side we only need to flush internal caches to
  3838. * get data visible to the CPU.
  3839. *
  3840. * However, we maintain the display planes as UC, and so
  3841. * need to rebind when first used as such.
  3842. */
  3843. cache_level = I915_CACHE_LLC;
  3844. else
  3845. cache_level = I915_CACHE_NONE;
  3846. i915_gem_object_set_cache_coherency(obj, cache_level);
  3847. trace_i915_gem_object_create(obj);
  3848. return obj;
  3849. fail:
  3850. i915_gem_object_free(obj);
  3851. return ERR_PTR(ret);
  3852. }
  3853. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3854. {
  3855. /* If we are the last user of the backing storage (be it shmemfs
  3856. * pages or stolen etc), we know that the pages are going to be
  3857. * immediately released. In this case, we can then skip copying
  3858. * back the contents from the GPU.
  3859. */
  3860. if (obj->mm.madv != I915_MADV_WILLNEED)
  3861. return false;
  3862. if (obj->base.filp == NULL)
  3863. return true;
  3864. /* At first glance, this looks racy, but then again so would be
  3865. * userspace racing mmap against close. However, the first external
  3866. * reference to the filp can only be obtained through the
  3867. * i915_gem_mmap_ioctl() which safeguards us against the user
  3868. * acquiring such a reference whilst we are in the middle of
  3869. * freeing the object.
  3870. */
  3871. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3872. }
  3873. static void __i915_gem_free_objects(struct drm_i915_private *i915,
  3874. struct llist_node *freed)
  3875. {
  3876. struct drm_i915_gem_object *obj, *on;
  3877. intel_runtime_pm_get(i915);
  3878. llist_for_each_entry_safe(obj, on, freed, freed) {
  3879. struct i915_vma *vma, *vn;
  3880. trace_i915_gem_object_destroy(obj);
  3881. mutex_lock(&i915->drm.struct_mutex);
  3882. GEM_BUG_ON(i915_gem_object_is_active(obj));
  3883. list_for_each_entry_safe(vma, vn,
  3884. &obj->vma_list, obj_link) {
  3885. GEM_BUG_ON(i915_vma_is_active(vma));
  3886. vma->flags &= ~I915_VMA_PIN_MASK;
  3887. i915_vma_close(vma);
  3888. }
  3889. GEM_BUG_ON(!list_empty(&obj->vma_list));
  3890. GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
  3891. /* This serializes freeing with the shrinker. Since the free
  3892. * is delayed, first by RCU then by the workqueue, we want the
  3893. * shrinker to be able to free pages of unreferenced objects,
  3894. * or else we may oom whilst there are plenty of deferred
  3895. * freed objects.
  3896. */
  3897. if (i915_gem_object_has_pages(obj)) {
  3898. spin_lock(&i915->mm.obj_lock);
  3899. list_del_init(&obj->mm.link);
  3900. spin_unlock(&i915->mm.obj_lock);
  3901. }
  3902. mutex_unlock(&i915->drm.struct_mutex);
  3903. GEM_BUG_ON(obj->bind_count);
  3904. GEM_BUG_ON(obj->userfault_count);
  3905. GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
  3906. GEM_BUG_ON(!list_empty(&obj->lut_list));
  3907. if (obj->ops->release)
  3908. obj->ops->release(obj);
  3909. if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
  3910. atomic_set(&obj->mm.pages_pin_count, 0);
  3911. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  3912. GEM_BUG_ON(i915_gem_object_has_pages(obj));
  3913. if (obj->base.import_attach)
  3914. drm_prime_gem_destroy(&obj->base, NULL);
  3915. reservation_object_fini(&obj->__builtin_resv);
  3916. drm_gem_object_release(&obj->base);
  3917. i915_gem_info_remove_obj(i915, obj->base.size);
  3918. kfree(obj->bit_17);
  3919. i915_gem_object_free(obj);
  3920. if (on)
  3921. cond_resched();
  3922. }
  3923. intel_runtime_pm_put(i915);
  3924. }
  3925. static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
  3926. {
  3927. struct llist_node *freed;
  3928. /* Free the oldest, most stale object to keep the free_list short */
  3929. freed = NULL;
  3930. if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
  3931. /* Only one consumer of llist_del_first() allowed */
  3932. spin_lock(&i915->mm.free_lock);
  3933. freed = llist_del_first(&i915->mm.free_list);
  3934. spin_unlock(&i915->mm.free_lock);
  3935. }
  3936. if (unlikely(freed)) {
  3937. freed->next = NULL;
  3938. __i915_gem_free_objects(i915, freed);
  3939. }
  3940. }
  3941. static void __i915_gem_free_work(struct work_struct *work)
  3942. {
  3943. struct drm_i915_private *i915 =
  3944. container_of(work, struct drm_i915_private, mm.free_work);
  3945. struct llist_node *freed;
  3946. /* All file-owned VMA should have been released by this point through
  3947. * i915_gem_close_object(), or earlier by i915_gem_context_close().
  3948. * However, the object may also be bound into the global GTT (e.g.
  3949. * older GPUs without per-process support, or for direct access through
  3950. * the GTT either for the user or for scanout). Those VMA still need to
  3951. * unbound now.
  3952. */
  3953. spin_lock(&i915->mm.free_lock);
  3954. while ((freed = llist_del_all(&i915->mm.free_list))) {
  3955. spin_unlock(&i915->mm.free_lock);
  3956. __i915_gem_free_objects(i915, freed);
  3957. if (need_resched())
  3958. return;
  3959. spin_lock(&i915->mm.free_lock);
  3960. }
  3961. spin_unlock(&i915->mm.free_lock);
  3962. }
  3963. static void __i915_gem_free_object_rcu(struct rcu_head *head)
  3964. {
  3965. struct drm_i915_gem_object *obj =
  3966. container_of(head, typeof(*obj), rcu);
  3967. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  3968. /* We can't simply use call_rcu() from i915_gem_free_object()
  3969. * as we need to block whilst unbinding, and the call_rcu
  3970. * task may be called from softirq context. So we take a
  3971. * detour through a worker.
  3972. */
  3973. if (llist_add(&obj->freed, &i915->mm.free_list))
  3974. schedule_work(&i915->mm.free_work);
  3975. }
  3976. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3977. {
  3978. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3979. if (obj->mm.quirked)
  3980. __i915_gem_object_unpin_pages(obj);
  3981. if (discard_backing_storage(obj))
  3982. obj->mm.madv = I915_MADV_DONTNEED;
  3983. /* Before we free the object, make sure any pure RCU-only
  3984. * read-side critical sections are complete, e.g.
  3985. * i915_gem_busy_ioctl(). For the corresponding synchronized
  3986. * lookup see i915_gem_object_lookup_rcu().
  3987. */
  3988. call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
  3989. }
  3990. void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
  3991. {
  3992. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3993. if (!i915_gem_object_has_active_reference(obj) &&
  3994. i915_gem_object_is_active(obj))
  3995. i915_gem_object_set_active_reference(obj);
  3996. else
  3997. i915_gem_object_put(obj);
  3998. }
  3999. static void assert_kernel_context_is_current(struct drm_i915_private *i915)
  4000. {
  4001. struct i915_gem_context *kernel_context = i915->kernel_context;
  4002. struct intel_engine_cs *engine;
  4003. enum intel_engine_id id;
  4004. for_each_engine(engine, i915, id) {
  4005. GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
  4006. GEM_BUG_ON(engine->last_retired_context != kernel_context);
  4007. }
  4008. }
  4009. void i915_gem_sanitize(struct drm_i915_private *i915)
  4010. {
  4011. if (i915_terminally_wedged(&i915->gpu_error)) {
  4012. mutex_lock(&i915->drm.struct_mutex);
  4013. i915_gem_unset_wedged(i915);
  4014. mutex_unlock(&i915->drm.struct_mutex);
  4015. }
  4016. /*
  4017. * If we inherit context state from the BIOS or earlier occupants
  4018. * of the GPU, the GPU may be in an inconsistent state when we
  4019. * try to take over. The only way to remove the earlier state
  4020. * is by resetting. However, resetting on earlier gen is tricky as
  4021. * it may impact the display and we are uncertain about the stability
  4022. * of the reset, so this could be applied to even earlier gen.
  4023. */
  4024. if (INTEL_GEN(i915) >= 5) {
  4025. int reset = intel_gpu_reset(i915, ALL_ENGINES);
  4026. WARN_ON(reset && reset != -ENODEV);
  4027. }
  4028. }
  4029. int i915_gem_suspend(struct drm_i915_private *dev_priv)
  4030. {
  4031. struct drm_device *dev = &dev_priv->drm;
  4032. int ret;
  4033. intel_runtime_pm_get(dev_priv);
  4034. intel_suspend_gt_powersave(dev_priv);
  4035. mutex_lock(&dev->struct_mutex);
  4036. /* We have to flush all the executing contexts to main memory so
  4037. * that they can saved in the hibernation image. To ensure the last
  4038. * context image is coherent, we have to switch away from it. That
  4039. * leaves the dev_priv->kernel_context still active when
  4040. * we actually suspend, and its image in memory may not match the GPU
  4041. * state. Fortunately, the kernel_context is disposable and we do
  4042. * not rely on its state.
  4043. */
  4044. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  4045. ret = i915_gem_switch_to_kernel_context(dev_priv);
  4046. if (ret)
  4047. goto err_unlock;
  4048. ret = i915_gem_wait_for_idle(dev_priv,
  4049. I915_WAIT_INTERRUPTIBLE |
  4050. I915_WAIT_LOCKED);
  4051. if (ret && ret != -EIO)
  4052. goto err_unlock;
  4053. assert_kernel_context_is_current(dev_priv);
  4054. }
  4055. i915_gem_contexts_lost(dev_priv);
  4056. mutex_unlock(&dev->struct_mutex);
  4057. intel_guc_suspend(dev_priv);
  4058. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  4059. cancel_delayed_work_sync(&dev_priv->gt.retire_work);
  4060. /* As the idle_work is rearming if it detects a race, play safe and
  4061. * repeat the flush until it is definitely idle.
  4062. */
  4063. drain_delayed_work(&dev_priv->gt.idle_work);
  4064. /* Assert that we sucessfully flushed all the work and
  4065. * reset the GPU back to its idle, low power state.
  4066. */
  4067. WARN_ON(dev_priv->gt.awake);
  4068. if (WARN_ON(!intel_engines_are_idle(dev_priv)))
  4069. i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
  4070. /*
  4071. * Neither the BIOS, ourselves or any other kernel
  4072. * expects the system to be in execlists mode on startup,
  4073. * so we need to reset the GPU back to legacy mode. And the only
  4074. * known way to disable logical contexts is through a GPU reset.
  4075. *
  4076. * So in order to leave the system in a known default configuration,
  4077. * always reset the GPU upon unload and suspend. Afterwards we then
  4078. * clean up the GEM state tracking, flushing off the requests and
  4079. * leaving the system in a known idle state.
  4080. *
  4081. * Note that is of the upmost importance that the GPU is idle and
  4082. * all stray writes are flushed *before* we dismantle the backing
  4083. * storage for the pinned objects.
  4084. *
  4085. * However, since we are uncertain that resetting the GPU on older
  4086. * machines is a good idea, we don't - just in case it leaves the
  4087. * machine in an unusable condition.
  4088. */
  4089. i915_gem_sanitize(dev_priv);
  4090. intel_runtime_pm_put(dev_priv);
  4091. return 0;
  4092. err_unlock:
  4093. mutex_unlock(&dev->struct_mutex);
  4094. intel_runtime_pm_put(dev_priv);
  4095. return ret;
  4096. }
  4097. void i915_gem_resume(struct drm_i915_private *i915)
  4098. {
  4099. WARN_ON(i915->gt.awake);
  4100. mutex_lock(&i915->drm.struct_mutex);
  4101. intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
  4102. i915_gem_restore_gtt_mappings(i915);
  4103. i915_gem_restore_fences(i915);
  4104. /*
  4105. * As we didn't flush the kernel context before suspend, we cannot
  4106. * guarantee that the context image is complete. So let's just reset
  4107. * it and start again.
  4108. */
  4109. i915->gt.resume(i915);
  4110. if (i915_gem_init_hw(i915))
  4111. goto err_wedged;
  4112. intel_guc_resume(i915);
  4113. /* Always reload a context for powersaving. */
  4114. if (i915_gem_switch_to_kernel_context(i915))
  4115. goto err_wedged;
  4116. out_unlock:
  4117. intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
  4118. mutex_unlock(&i915->drm.struct_mutex);
  4119. return;
  4120. err_wedged:
  4121. if (!i915_terminally_wedged(&i915->gpu_error)) {
  4122. DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
  4123. i915_gem_set_wedged(i915);
  4124. }
  4125. goto out_unlock;
  4126. }
  4127. void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
  4128. {
  4129. if (INTEL_GEN(dev_priv) < 5 ||
  4130. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  4131. return;
  4132. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  4133. DISP_TILE_SURFACE_SWIZZLING);
  4134. if (IS_GEN5(dev_priv))
  4135. return;
  4136. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  4137. if (IS_GEN6(dev_priv))
  4138. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  4139. else if (IS_GEN7(dev_priv))
  4140. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  4141. else if (IS_GEN8(dev_priv))
  4142. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  4143. else
  4144. BUG();
  4145. }
  4146. static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
  4147. {
  4148. I915_WRITE(RING_CTL(base), 0);
  4149. I915_WRITE(RING_HEAD(base), 0);
  4150. I915_WRITE(RING_TAIL(base), 0);
  4151. I915_WRITE(RING_START(base), 0);
  4152. }
  4153. static void init_unused_rings(struct drm_i915_private *dev_priv)
  4154. {
  4155. if (IS_I830(dev_priv)) {
  4156. init_unused_ring(dev_priv, PRB1_BASE);
  4157. init_unused_ring(dev_priv, SRB0_BASE);
  4158. init_unused_ring(dev_priv, SRB1_BASE);
  4159. init_unused_ring(dev_priv, SRB2_BASE);
  4160. init_unused_ring(dev_priv, SRB3_BASE);
  4161. } else if (IS_GEN2(dev_priv)) {
  4162. init_unused_ring(dev_priv, SRB0_BASE);
  4163. init_unused_ring(dev_priv, SRB1_BASE);
  4164. } else if (IS_GEN3(dev_priv)) {
  4165. init_unused_ring(dev_priv, PRB1_BASE);
  4166. init_unused_ring(dev_priv, PRB2_BASE);
  4167. }
  4168. }
  4169. static int __i915_gem_restart_engines(void *data)
  4170. {
  4171. struct drm_i915_private *i915 = data;
  4172. struct intel_engine_cs *engine;
  4173. enum intel_engine_id id;
  4174. int err;
  4175. for_each_engine(engine, i915, id) {
  4176. err = engine->init_hw(engine);
  4177. if (err)
  4178. return err;
  4179. }
  4180. return 0;
  4181. }
  4182. int i915_gem_init_hw(struct drm_i915_private *dev_priv)
  4183. {
  4184. int ret;
  4185. dev_priv->gt.last_init_time = ktime_get();
  4186. /* Double layer security blanket, see i915_gem_init() */
  4187. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4188. if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
  4189. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  4190. if (IS_HASWELL(dev_priv))
  4191. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
  4192. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  4193. if (HAS_PCH_NOP(dev_priv)) {
  4194. if (IS_IVYBRIDGE(dev_priv)) {
  4195. u32 temp = I915_READ(GEN7_MSG_CTL);
  4196. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  4197. I915_WRITE(GEN7_MSG_CTL, temp);
  4198. } else if (INTEL_GEN(dev_priv) >= 7) {
  4199. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  4200. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4201. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  4202. }
  4203. }
  4204. i915_gem_init_swizzling(dev_priv);
  4205. /*
  4206. * At least 830 can leave some of the unused rings
  4207. * "active" (ie. head != tail) after resume which
  4208. * will prevent c3 entry. Makes sure all unused rings
  4209. * are totally idle.
  4210. */
  4211. init_unused_rings(dev_priv);
  4212. BUG_ON(!dev_priv->kernel_context);
  4213. if (i915_terminally_wedged(&dev_priv->gpu_error)) {
  4214. ret = -EIO;
  4215. goto out;
  4216. }
  4217. ret = i915_ppgtt_init_hw(dev_priv);
  4218. if (ret) {
  4219. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  4220. goto out;
  4221. }
  4222. /* We can't enable contexts until all firmware is loaded */
  4223. ret = intel_uc_init_hw(dev_priv);
  4224. if (ret)
  4225. goto out;
  4226. intel_mocs_init_l3cc_table(dev_priv);
  4227. /* Only when the HW is re-initialised, can we replay the requests */
  4228. ret = __i915_gem_restart_engines(dev_priv);
  4229. out:
  4230. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4231. return ret;
  4232. }
  4233. static int __intel_engines_record_defaults(struct drm_i915_private *i915)
  4234. {
  4235. struct i915_gem_context *ctx;
  4236. struct intel_engine_cs *engine;
  4237. enum intel_engine_id id;
  4238. int err;
  4239. /*
  4240. * As we reset the gpu during very early sanitisation, the current
  4241. * register state on the GPU should reflect its defaults values.
  4242. * We load a context onto the hw (with restore-inhibit), then switch
  4243. * over to a second context to save that default register state. We
  4244. * can then prime every new context with that state so they all start
  4245. * from the same default HW values.
  4246. */
  4247. ctx = i915_gem_context_create_kernel(i915, 0);
  4248. if (IS_ERR(ctx))
  4249. return PTR_ERR(ctx);
  4250. for_each_engine(engine, i915, id) {
  4251. struct drm_i915_gem_request *rq;
  4252. rq = i915_gem_request_alloc(engine, ctx);
  4253. if (IS_ERR(rq)) {
  4254. err = PTR_ERR(rq);
  4255. goto out_ctx;
  4256. }
  4257. err = 0;
  4258. if (engine->init_context)
  4259. err = engine->init_context(rq);
  4260. __i915_add_request(rq, true);
  4261. if (err)
  4262. goto err_active;
  4263. }
  4264. err = i915_gem_switch_to_kernel_context(i915);
  4265. if (err)
  4266. goto err_active;
  4267. err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
  4268. if (err)
  4269. goto err_active;
  4270. assert_kernel_context_is_current(i915);
  4271. for_each_engine(engine, i915, id) {
  4272. struct i915_vma *state;
  4273. state = ctx->engine[id].state;
  4274. if (!state)
  4275. continue;
  4276. /*
  4277. * As we will hold a reference to the logical state, it will
  4278. * not be torn down with the context, and importantly the
  4279. * object will hold onto its vma (making it possible for a
  4280. * stray GTT write to corrupt our defaults). Unmap the vma
  4281. * from the GTT to prevent such accidents and reclaim the
  4282. * space.
  4283. */
  4284. err = i915_vma_unbind(state);
  4285. if (err)
  4286. goto err_active;
  4287. err = i915_gem_object_set_to_cpu_domain(state->obj, false);
  4288. if (err)
  4289. goto err_active;
  4290. engine->default_state = i915_gem_object_get(state->obj);
  4291. }
  4292. if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
  4293. unsigned int found = intel_engines_has_context_isolation(i915);
  4294. /*
  4295. * Make sure that classes with multiple engine instances all
  4296. * share the same basic configuration.
  4297. */
  4298. for_each_engine(engine, i915, id) {
  4299. unsigned int bit = BIT(engine->uabi_class);
  4300. unsigned int expected = engine->default_state ? bit : 0;
  4301. if ((found & bit) != expected) {
  4302. DRM_ERROR("mismatching default context state for class %d on engine %s\n",
  4303. engine->uabi_class, engine->name);
  4304. }
  4305. }
  4306. }
  4307. out_ctx:
  4308. i915_gem_context_set_closed(ctx);
  4309. i915_gem_context_put(ctx);
  4310. return err;
  4311. err_active:
  4312. /*
  4313. * If we have to abandon now, we expect the engines to be idle
  4314. * and ready to be torn-down. First try to flush any remaining
  4315. * request, ensure we are pointing at the kernel context and
  4316. * then remove it.
  4317. */
  4318. if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
  4319. goto out_ctx;
  4320. if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
  4321. goto out_ctx;
  4322. i915_gem_contexts_lost(i915);
  4323. goto out_ctx;
  4324. }
  4325. int i915_gem_init(struct drm_i915_private *dev_priv)
  4326. {
  4327. int ret;
  4328. /*
  4329. * We need to fallback to 4K pages since gvt gtt handling doesn't
  4330. * support huge page entries - we will need to check either hypervisor
  4331. * mm can support huge guest page or just do emulation in gvt.
  4332. */
  4333. if (intel_vgpu_active(dev_priv))
  4334. mkwrite_device_info(dev_priv)->page_sizes =
  4335. I915_GTT_PAGE_SIZE_4K;
  4336. dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
  4337. if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
  4338. dev_priv->gt.resume = intel_lr_context_resume;
  4339. dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
  4340. } else {
  4341. dev_priv->gt.resume = intel_legacy_submission_resume;
  4342. dev_priv->gt.cleanup_engine = intel_engine_cleanup;
  4343. }
  4344. ret = i915_gem_init_userptr(dev_priv);
  4345. if (ret)
  4346. return ret;
  4347. ret = intel_uc_init_wq(dev_priv);
  4348. if (ret)
  4349. return ret;
  4350. /* This is just a security blanket to placate dragons.
  4351. * On some systems, we very sporadically observe that the first TLBs
  4352. * used by the CS may be stale, despite us poking the TLB reset. If
  4353. * we hold the forcewake during initialisation these problems
  4354. * just magically go away.
  4355. */
  4356. mutex_lock(&dev_priv->drm.struct_mutex);
  4357. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4358. ret = i915_gem_init_ggtt(dev_priv);
  4359. if (ret) {
  4360. GEM_BUG_ON(ret == -EIO);
  4361. goto err_unlock;
  4362. }
  4363. ret = i915_gem_contexts_init(dev_priv);
  4364. if (ret) {
  4365. GEM_BUG_ON(ret == -EIO);
  4366. goto err_ggtt;
  4367. }
  4368. ret = intel_engines_init(dev_priv);
  4369. if (ret) {
  4370. GEM_BUG_ON(ret == -EIO);
  4371. goto err_context;
  4372. }
  4373. intel_init_gt_powersave(dev_priv);
  4374. ret = intel_uc_init(dev_priv);
  4375. if (ret)
  4376. goto err_pm;
  4377. ret = i915_gem_init_hw(dev_priv);
  4378. if (ret)
  4379. goto err_uc_init;
  4380. /*
  4381. * Despite its name intel_init_clock_gating applies both display
  4382. * clock gating workarounds; GT mmio workarounds and the occasional
  4383. * GT power context workaround. Worse, sometimes it includes a context
  4384. * register workaround which we need to apply before we record the
  4385. * default HW state for all contexts.
  4386. *
  4387. * FIXME: break up the workarounds and apply them at the right time!
  4388. */
  4389. intel_init_clock_gating(dev_priv);
  4390. ret = __intel_engines_record_defaults(dev_priv);
  4391. if (ret)
  4392. goto err_init_hw;
  4393. if (i915_inject_load_failure()) {
  4394. ret = -ENODEV;
  4395. goto err_init_hw;
  4396. }
  4397. if (i915_inject_load_failure()) {
  4398. ret = -EIO;
  4399. goto err_init_hw;
  4400. }
  4401. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4402. mutex_unlock(&dev_priv->drm.struct_mutex);
  4403. return 0;
  4404. /*
  4405. * Unwinding is complicated by that we want to handle -EIO to mean
  4406. * disable GPU submission but keep KMS alive. We want to mark the
  4407. * HW as irrevisibly wedged, but keep enough state around that the
  4408. * driver doesn't explode during runtime.
  4409. */
  4410. err_init_hw:
  4411. i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
  4412. i915_gem_contexts_lost(dev_priv);
  4413. intel_uc_fini_hw(dev_priv);
  4414. err_uc_init:
  4415. intel_uc_fini(dev_priv);
  4416. err_pm:
  4417. if (ret != -EIO) {
  4418. intel_cleanup_gt_powersave(dev_priv);
  4419. i915_gem_cleanup_engines(dev_priv);
  4420. }
  4421. err_context:
  4422. if (ret != -EIO)
  4423. i915_gem_contexts_fini(dev_priv);
  4424. err_ggtt:
  4425. err_unlock:
  4426. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4427. mutex_unlock(&dev_priv->drm.struct_mutex);
  4428. if (ret != -EIO)
  4429. i915_gem_cleanup_userptr(dev_priv);
  4430. if (ret == -EIO) {
  4431. /*
  4432. * Allow engine initialisation to fail by marking the GPU as
  4433. * wedged. But we only want to do this where the GPU is angry,
  4434. * for all other failure, such as an allocation failure, bail.
  4435. */
  4436. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  4437. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4438. i915_gem_set_wedged(dev_priv);
  4439. }
  4440. ret = 0;
  4441. }
  4442. i915_gem_drain_freed_objects(dev_priv);
  4443. return ret;
  4444. }
  4445. void i915_gem_init_mmio(struct drm_i915_private *i915)
  4446. {
  4447. i915_gem_sanitize(i915);
  4448. }
  4449. void
  4450. i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
  4451. {
  4452. struct intel_engine_cs *engine;
  4453. enum intel_engine_id id;
  4454. for_each_engine(engine, dev_priv, id)
  4455. dev_priv->gt.cleanup_engine(engine);
  4456. }
  4457. void
  4458. i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  4459. {
  4460. int i;
  4461. if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
  4462. !IS_CHERRYVIEW(dev_priv))
  4463. dev_priv->num_fence_regs = 32;
  4464. else if (INTEL_INFO(dev_priv)->gen >= 4 ||
  4465. IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  4466. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
  4467. dev_priv->num_fence_regs = 16;
  4468. else
  4469. dev_priv->num_fence_regs = 8;
  4470. if (intel_vgpu_active(dev_priv))
  4471. dev_priv->num_fence_regs =
  4472. I915_READ(vgtif_reg(avail_rs.fence_num));
  4473. /* Initialize fence registers to zero */
  4474. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  4475. struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
  4476. fence->i915 = dev_priv;
  4477. fence->id = i;
  4478. list_add_tail(&fence->link, &dev_priv->mm.fence_list);
  4479. }
  4480. i915_gem_restore_fences(dev_priv);
  4481. i915_gem_detect_bit_6_swizzle(dev_priv);
  4482. }
  4483. static void i915_gem_init__mm(struct drm_i915_private *i915)
  4484. {
  4485. spin_lock_init(&i915->mm.object_stat_lock);
  4486. spin_lock_init(&i915->mm.obj_lock);
  4487. spin_lock_init(&i915->mm.free_lock);
  4488. init_llist_head(&i915->mm.free_list);
  4489. INIT_LIST_HEAD(&i915->mm.unbound_list);
  4490. INIT_LIST_HEAD(&i915->mm.bound_list);
  4491. INIT_LIST_HEAD(&i915->mm.fence_list);
  4492. INIT_LIST_HEAD(&i915->mm.userfault_list);
  4493. INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
  4494. }
  4495. int
  4496. i915_gem_load_init(struct drm_i915_private *dev_priv)
  4497. {
  4498. int err = -ENOMEM;
  4499. dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
  4500. if (!dev_priv->objects)
  4501. goto err_out;
  4502. dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
  4503. if (!dev_priv->vmas)
  4504. goto err_objects;
  4505. dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
  4506. if (!dev_priv->luts)
  4507. goto err_vmas;
  4508. dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
  4509. SLAB_HWCACHE_ALIGN |
  4510. SLAB_RECLAIM_ACCOUNT |
  4511. SLAB_TYPESAFE_BY_RCU);
  4512. if (!dev_priv->requests)
  4513. goto err_luts;
  4514. dev_priv->dependencies = KMEM_CACHE(i915_dependency,
  4515. SLAB_HWCACHE_ALIGN |
  4516. SLAB_RECLAIM_ACCOUNT);
  4517. if (!dev_priv->dependencies)
  4518. goto err_requests;
  4519. dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
  4520. if (!dev_priv->priorities)
  4521. goto err_dependencies;
  4522. mutex_lock(&dev_priv->drm.struct_mutex);
  4523. INIT_LIST_HEAD(&dev_priv->gt.timelines);
  4524. err = i915_gem_timeline_init__global(dev_priv);
  4525. mutex_unlock(&dev_priv->drm.struct_mutex);
  4526. if (err)
  4527. goto err_priorities;
  4528. i915_gem_init__mm(dev_priv);
  4529. INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
  4530. i915_gem_retire_work_handler);
  4531. INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
  4532. i915_gem_idle_work_handler);
  4533. init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
  4534. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4535. atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
  4536. spin_lock_init(&dev_priv->fb_tracking.lock);
  4537. err = i915_gemfs_init(dev_priv);
  4538. if (err)
  4539. DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
  4540. return 0;
  4541. err_priorities:
  4542. kmem_cache_destroy(dev_priv->priorities);
  4543. err_dependencies:
  4544. kmem_cache_destroy(dev_priv->dependencies);
  4545. err_requests:
  4546. kmem_cache_destroy(dev_priv->requests);
  4547. err_luts:
  4548. kmem_cache_destroy(dev_priv->luts);
  4549. err_vmas:
  4550. kmem_cache_destroy(dev_priv->vmas);
  4551. err_objects:
  4552. kmem_cache_destroy(dev_priv->objects);
  4553. err_out:
  4554. return err;
  4555. }
  4556. void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
  4557. {
  4558. i915_gem_drain_freed_objects(dev_priv);
  4559. WARN_ON(!llist_empty(&dev_priv->mm.free_list));
  4560. WARN_ON(dev_priv->mm.object_count);
  4561. mutex_lock(&dev_priv->drm.struct_mutex);
  4562. i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
  4563. WARN_ON(!list_empty(&dev_priv->gt.timelines));
  4564. mutex_unlock(&dev_priv->drm.struct_mutex);
  4565. kmem_cache_destroy(dev_priv->priorities);
  4566. kmem_cache_destroy(dev_priv->dependencies);
  4567. kmem_cache_destroy(dev_priv->requests);
  4568. kmem_cache_destroy(dev_priv->luts);
  4569. kmem_cache_destroy(dev_priv->vmas);
  4570. kmem_cache_destroy(dev_priv->objects);
  4571. /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
  4572. rcu_barrier();
  4573. i915_gemfs_fini(dev_priv);
  4574. }
  4575. int i915_gem_freeze(struct drm_i915_private *dev_priv)
  4576. {
  4577. /* Discard all purgeable objects, let userspace recover those as
  4578. * required after resuming.
  4579. */
  4580. i915_gem_shrink_all(dev_priv);
  4581. return 0;
  4582. }
  4583. int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  4584. {
  4585. struct drm_i915_gem_object *obj;
  4586. struct list_head *phases[] = {
  4587. &dev_priv->mm.unbound_list,
  4588. &dev_priv->mm.bound_list,
  4589. NULL
  4590. }, **p;
  4591. /* Called just before we write the hibernation image.
  4592. *
  4593. * We need to update the domain tracking to reflect that the CPU
  4594. * will be accessing all the pages to create and restore from the
  4595. * hibernation, and so upon restoration those pages will be in the
  4596. * CPU domain.
  4597. *
  4598. * To make sure the hibernation image contains the latest state,
  4599. * we update that state just before writing out the image.
  4600. *
  4601. * To try and reduce the hibernation image, we manually shrink
  4602. * the objects as well, see i915_gem_freeze()
  4603. */
  4604. i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
  4605. i915_gem_drain_freed_objects(dev_priv);
  4606. spin_lock(&dev_priv->mm.obj_lock);
  4607. for (p = phases; *p; p++) {
  4608. list_for_each_entry(obj, *p, mm.link)
  4609. __start_cpu_write(obj);
  4610. }
  4611. spin_unlock(&dev_priv->mm.obj_lock);
  4612. return 0;
  4613. }
  4614. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4615. {
  4616. struct drm_i915_file_private *file_priv = file->driver_priv;
  4617. struct drm_i915_gem_request *request;
  4618. /* Clean up our request list when the client is going away, so that
  4619. * later retire_requests won't dereference our soon-to-be-gone
  4620. * file_priv.
  4621. */
  4622. spin_lock(&file_priv->mm.lock);
  4623. list_for_each_entry(request, &file_priv->mm.request_list, client_link)
  4624. request->file_priv = NULL;
  4625. spin_unlock(&file_priv->mm.lock);
  4626. }
  4627. int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
  4628. {
  4629. struct drm_i915_file_private *file_priv;
  4630. int ret;
  4631. DRM_DEBUG("\n");
  4632. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4633. if (!file_priv)
  4634. return -ENOMEM;
  4635. file->driver_priv = file_priv;
  4636. file_priv->dev_priv = i915;
  4637. file_priv->file = file;
  4638. spin_lock_init(&file_priv->mm.lock);
  4639. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4640. file_priv->bsd_engine = -1;
  4641. ret = i915_gem_context_open(i915, file);
  4642. if (ret)
  4643. kfree(file_priv);
  4644. return ret;
  4645. }
  4646. /**
  4647. * i915_gem_track_fb - update frontbuffer tracking
  4648. * @old: current GEM buffer for the frontbuffer slots
  4649. * @new: new GEM buffer for the frontbuffer slots
  4650. * @frontbuffer_bits: bitmask of frontbuffer slots
  4651. *
  4652. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4653. * from @old and setting them in @new. Both @old and @new can be NULL.
  4654. */
  4655. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4656. struct drm_i915_gem_object *new,
  4657. unsigned frontbuffer_bits)
  4658. {
  4659. /* Control of individual bits within the mask are guarded by
  4660. * the owning plane->mutex, i.e. we can never see concurrent
  4661. * manipulation of individual bits. But since the bitfield as a whole
  4662. * is updated using RMW, we need to use atomics in order to update
  4663. * the bits.
  4664. */
  4665. BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
  4666. sizeof(atomic_t) * BITS_PER_BYTE);
  4667. if (old) {
  4668. WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
  4669. atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
  4670. }
  4671. if (new) {
  4672. WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
  4673. atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
  4674. }
  4675. }
  4676. /* Allocate a new GEM object and fill it with the supplied data */
  4677. struct drm_i915_gem_object *
  4678. i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
  4679. const void *data, size_t size)
  4680. {
  4681. struct drm_i915_gem_object *obj;
  4682. struct file *file;
  4683. size_t offset;
  4684. int err;
  4685. obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
  4686. if (IS_ERR(obj))
  4687. return obj;
  4688. GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
  4689. file = obj->base.filp;
  4690. offset = 0;
  4691. do {
  4692. unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
  4693. struct page *page;
  4694. void *pgdata, *vaddr;
  4695. err = pagecache_write_begin(file, file->f_mapping,
  4696. offset, len, 0,
  4697. &page, &pgdata);
  4698. if (err < 0)
  4699. goto fail;
  4700. vaddr = kmap(page);
  4701. memcpy(vaddr, data, len);
  4702. kunmap(page);
  4703. err = pagecache_write_end(file, file->f_mapping,
  4704. offset, len, len,
  4705. page, pgdata);
  4706. if (err < 0)
  4707. goto fail;
  4708. size -= len;
  4709. data += len;
  4710. offset += len;
  4711. } while (size);
  4712. return obj;
  4713. fail:
  4714. i915_gem_object_put(obj);
  4715. return ERR_PTR(err);
  4716. }
  4717. struct scatterlist *
  4718. i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
  4719. unsigned int n,
  4720. unsigned int *offset)
  4721. {
  4722. struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
  4723. struct scatterlist *sg;
  4724. unsigned int idx, count;
  4725. might_sleep();
  4726. GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
  4727. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
  4728. /* As we iterate forward through the sg, we record each entry in a
  4729. * radixtree for quick repeated (backwards) lookups. If we have seen
  4730. * this index previously, we will have an entry for it.
  4731. *
  4732. * Initial lookup is O(N), but this is amortized to O(1) for
  4733. * sequential page access (where each new request is consecutive
  4734. * to the previous one). Repeated lookups are O(lg(obj->base.size)),
  4735. * i.e. O(1) with a large constant!
  4736. */
  4737. if (n < READ_ONCE(iter->sg_idx))
  4738. goto lookup;
  4739. mutex_lock(&iter->lock);
  4740. /* We prefer to reuse the last sg so that repeated lookup of this
  4741. * (or the subsequent) sg are fast - comparing against the last
  4742. * sg is faster than going through the radixtree.
  4743. */
  4744. sg = iter->sg_pos;
  4745. idx = iter->sg_idx;
  4746. count = __sg_page_count(sg);
  4747. while (idx + count <= n) {
  4748. unsigned long exception, i;
  4749. int ret;
  4750. /* If we cannot allocate and insert this entry, or the
  4751. * individual pages from this range, cancel updating the
  4752. * sg_idx so that on this lookup we are forced to linearly
  4753. * scan onwards, but on future lookups we will try the
  4754. * insertion again (in which case we need to be careful of
  4755. * the error return reporting that we have already inserted
  4756. * this index).
  4757. */
  4758. ret = radix_tree_insert(&iter->radix, idx, sg);
  4759. if (ret && ret != -EEXIST)
  4760. goto scan;
  4761. exception =
  4762. RADIX_TREE_EXCEPTIONAL_ENTRY |
  4763. idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
  4764. for (i = 1; i < count; i++) {
  4765. ret = radix_tree_insert(&iter->radix, idx + i,
  4766. (void *)exception);
  4767. if (ret && ret != -EEXIST)
  4768. goto scan;
  4769. }
  4770. idx += count;
  4771. sg = ____sg_next(sg);
  4772. count = __sg_page_count(sg);
  4773. }
  4774. scan:
  4775. iter->sg_pos = sg;
  4776. iter->sg_idx = idx;
  4777. mutex_unlock(&iter->lock);
  4778. if (unlikely(n < idx)) /* insertion completed by another thread */
  4779. goto lookup;
  4780. /* In case we failed to insert the entry into the radixtree, we need
  4781. * to look beyond the current sg.
  4782. */
  4783. while (idx + count <= n) {
  4784. idx += count;
  4785. sg = ____sg_next(sg);
  4786. count = __sg_page_count(sg);
  4787. }
  4788. *offset = n - idx;
  4789. return sg;
  4790. lookup:
  4791. rcu_read_lock();
  4792. sg = radix_tree_lookup(&iter->radix, n);
  4793. GEM_BUG_ON(!sg);
  4794. /* If this index is in the middle of multi-page sg entry,
  4795. * the radixtree will contain an exceptional entry that points
  4796. * to the start of that range. We will return the pointer to
  4797. * the base page and the offset of this page within the
  4798. * sg entry's range.
  4799. */
  4800. *offset = 0;
  4801. if (unlikely(radix_tree_exception(sg))) {
  4802. unsigned long base =
  4803. (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
  4804. sg = radix_tree_lookup(&iter->radix, base);
  4805. GEM_BUG_ON(!sg);
  4806. *offset = n - base;
  4807. }
  4808. rcu_read_unlock();
  4809. return sg;
  4810. }
  4811. struct page *
  4812. i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
  4813. {
  4814. struct scatterlist *sg;
  4815. unsigned int offset;
  4816. GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  4817. sg = i915_gem_object_get_sg(obj, n, &offset);
  4818. return nth_page(sg_page(sg), offset);
  4819. }
  4820. /* Like i915_gem_object_get_page(), but mark the returned page dirty */
  4821. struct page *
  4822. i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
  4823. unsigned int n)
  4824. {
  4825. struct page *page;
  4826. page = i915_gem_object_get_page(obj, n);
  4827. if (!obj->mm.dirty)
  4828. set_page_dirty(page);
  4829. return page;
  4830. }
  4831. dma_addr_t
  4832. i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
  4833. unsigned long n)
  4834. {
  4835. struct scatterlist *sg;
  4836. unsigned int offset;
  4837. sg = i915_gem_object_get_sg(obj, n, &offset);
  4838. return sg_dma_address(sg) + (offset << PAGE_SHIFT);
  4839. }
  4840. int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
  4841. {
  4842. struct sg_table *pages;
  4843. int err;
  4844. if (align > obj->base.size)
  4845. return -EINVAL;
  4846. if (obj->ops == &i915_gem_phys_ops)
  4847. return 0;
  4848. if (obj->ops != &i915_gem_object_ops)
  4849. return -EINVAL;
  4850. err = i915_gem_object_unbind(obj);
  4851. if (err)
  4852. return err;
  4853. mutex_lock(&obj->mm.lock);
  4854. if (obj->mm.madv != I915_MADV_WILLNEED) {
  4855. err = -EFAULT;
  4856. goto err_unlock;
  4857. }
  4858. if (obj->mm.quirked) {
  4859. err = -EFAULT;
  4860. goto err_unlock;
  4861. }
  4862. if (obj->mm.mapping) {
  4863. err = -EBUSY;
  4864. goto err_unlock;
  4865. }
  4866. pages = fetch_and_zero(&obj->mm.pages);
  4867. if (pages) {
  4868. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  4869. __i915_gem_object_reset_page_iter(obj);
  4870. spin_lock(&i915->mm.obj_lock);
  4871. list_del(&obj->mm.link);
  4872. spin_unlock(&i915->mm.obj_lock);
  4873. }
  4874. obj->ops = &i915_gem_phys_ops;
  4875. err = ____i915_gem_object_get_pages(obj);
  4876. if (err)
  4877. goto err_xfer;
  4878. /* Perma-pin (until release) the physical set of pages */
  4879. __i915_gem_object_pin_pages(obj);
  4880. if (!IS_ERR_OR_NULL(pages))
  4881. i915_gem_object_ops.put_pages(obj, pages);
  4882. mutex_unlock(&obj->mm.lock);
  4883. return 0;
  4884. err_xfer:
  4885. obj->ops = &i915_gem_object_ops;
  4886. obj->mm.pages = pages;
  4887. err_unlock:
  4888. mutex_unlock(&obj->mm.lock);
  4889. return err;
  4890. }
  4891. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  4892. #include "selftests/scatterlist.c"
  4893. #include "selftests/mock_gem_device.c"
  4894. #include "selftests/huge_gem_object.c"
  4895. #include "selftests/huge_pages.c"
  4896. #include "selftests/i915_gem_object.c"
  4897. #include "selftests/i915_gem_coherency.c"
  4898. #endif