123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148
#include "xe_vm.h"
#include <linux/dma-fence-array.h>
#include <linux/error-injection.h>
#include <linux/nospec.h>
#include <drm/drm_drv.h>
#include <drm/drm_exec.h>
#include <drm/drm_print.h>
#include <drm/ttm/ttm_tt.h>
#include <uapi/drm/xe_drm.h>
#include <linux/ascii85.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/mm.h>
#include <linux/swap.h>
#include <generated/xe_wa_oob.h>
#include "regs/xe_gtt_defs.h"
#include "xe_assert.h"
#include "xe_bo.h"
#include "xe_cpu_bind.h"
#include "xe_device.h"
#include "xe_drm_client.h"
#include "xe_exec_queue.h"
#include "xe_gt.h"
#include "xe_migrate.h"
#include "xe_pagefault.h"
#include "xe_pat.h"
#include "xe_pm.h"
#include "xe_preempt_fence.h"
#include "xe_pt.h"
#include "xe_pxp.h"
#include "xe_sriov_vf.h"
#include "xe_svm.h"
#include "xe_sync.h"
#include "xe_tile.h"
#include "xe_tlb_inval.h"
#include "xe_trace_bo.h"
#include "xe_vm_madvise.h"
#include "xe_wa.h"
static struct drm_gem_object *xe_vm_obj(struct xe_vm *vm)
{
return vm->gpuvm.r_obj;
}
int xe_vm_drm_exec_lock(struct xe_vm *vm, struct drm_exec *exec)
{
return drm_exec_lock_obj(exec, xe_vm_obj(vm));
}
static bool preempt_fences_waiting(struct xe_vm *vm)
{
struct xe_exec_queue *q;
lockdep_assert_held(&vm->lock);
xe_vm_assert_held(vm);
list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
if (!q->lr.pfence ||
test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
&q->lr.pfence->flags)) {
return true;
}
}
return false;
}
static void free_preempt_fences(struct list_head *list)
{
struct list_head *link, *next;
list_for_each_safe(link, next, list)
xe_preempt_fence_free(to_preempt_fence_from_link(link));
}
static int alloc_preempt_fences(struct xe_vm *vm, struct list_head *list,
unsigned int *count)
{
lockdep_assert_held(&vm->lock);
xe_vm_assert_held(vm);
if (*count >= vm->preempt.num_exec_queues)
return 0;
for (; *count < vm->preempt.num_exec_queues; ++(*count)) {
struct xe_preempt_fence *pfence = xe_preempt_fence_alloc();
if (IS_ERR(pfence))
return PTR_ERR(pfence);
list_move_tail(xe_preempt_fence_link(pfence), list);
}
return 0;
}
static int wait_for_existing_preempt_fences(struct xe_vm *vm)
{
struct xe_exec_queue *q;
bool vf_migration = IS_SRIOV_VF(vm->xe) &&
xe_sriov_vf_migration_supported(vm->xe);
signed long wait_time = vf_migration ? HZ / 5 : MAX_SCHEDULE_TIMEOUT;
xe_vm_assert_held(vm);
list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
if (q->lr.pfence) {
long timeout;
timeout = dma_fence_wait_timeout(q->lr.pfence, false,
wait_time);
if (!timeout) {
xe_assert(vm->xe, vf_migration);
return -EAGAIN;
}
if (timeout < 0 || q->lr.pfence->error == -ETIME)
return -ETIME;
dma_fence_put(q->lr.pfence);
q->lr.pfence = NULL;
}
}
return 0;
}
static bool xe_vm_is_idle(struct xe_vm *vm)
{
struct xe_exec_queue *q;
xe_vm_assert_held(vm);
list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
if (!xe_exec_queue_is_idle(q, 0))
return false;
}
return true;
}
static void arm_preempt_fences(struct xe_vm *vm, struct list_head *list)
{
struct list_head *link;
struct xe_exec_queue *q;
list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
struct dma_fence *fence;
link = list->next;
xe_assert(vm->xe, link != list);
fence = xe_preempt_fence_arm(to_preempt_fence_from_link(link),
q, q->lr.context,
++q->lr.seqno);
dma_fence_put(q->lr.pfence);
q->lr.pfence = fence;
}
}
static int add_preempt_fences(struct xe_vm *vm, struct xe_bo *bo)
{
struct xe_exec_queue *q;
int err;
xe_bo_assert_held(bo);
if (!vm->preempt.num_exec_queues)
return 0;
err = dma_resv_reserve_fences(bo->ttm.base.resv, vm->preempt.num_exec_queues);
if (err)
return err;
list_for_each_entry(q, &vm->preempt.exec_queues, lr.link)
if (q->lr.pfence) {
dma_resv_add_fence(bo->ttm.base.resv,
q->lr.pfence,
DMA_RESV_USAGE_BOOKKEEP);
}
return 0;
}
static void resume_and_reinstall_preempt_fences(struct xe_vm *vm,
struct drm_exec *exec)
{
struct xe_exec_queue *q;
lockdep_assert_held(&vm->lock);
xe_vm_assert_held(vm);
list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
if (READ_ONCE(q->lr.suspended) && !q->ops->reset_status(q)) {
WRITE_ONCE(q->lr.suspended, false);
q->ops->resume(q);
}
drm_gpuvm_resv_add_fence(&vm->gpuvm, exec, q->lr.pfence,
DMA_RESV_USAGE_BOOKKEEP, DMA_RESV_USAGE_BOOKKEEP);
}
}
int xe_vm_add_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
{
struct drm_gpuvm_exec vm_exec = {
.vm = &vm->gpuvm,
.flags = DRM_EXEC_INTERRUPTIBLE_WAIT,
.num_fences = 1,
};
struct drm_exec *exec = &vm_exec.exec;
struct xe_validation_ctx ctx;
struct dma_fence *pfence;
int err;
bool wait;
xe_assert(vm->xe, xe_vm_in_preempt_fence_mode(vm));
down_write(&vm->lock);
err = xe_validation_exec_lock(&ctx, &vm_exec, &vm->xe->val);
if (err)
goto out_up_write;
pfence = xe_preempt_fence_create(q, q->lr.context,
++q->lr.seqno);
if (IS_ERR(pfence)) {
err = PTR_ERR(pfence);
goto out_fini;
}
list_add(&q->lr.link, &vm->preempt.exec_queues);
++vm->preempt.num_exec_queues;
q->lr.pfence = pfence;
xe_svm_notifier_lock(vm);
drm_gpuvm_resv_add_fence(&vm->gpuvm, exec, pfence,
DMA_RESV_USAGE_BOOKKEEP, DMA_RESV_USAGE_BOOKKEEP);
wait = __xe_vm_userptr_needs_repin(vm) || preempt_fences_waiting(vm) ||
!list_empty(&vm->gpuvm.evict.list) ||
!list_empty(&vm->rebind_list);
if (wait)
dma_fence_enable_signaling(pfence);
xe_svm_notifier_unlock(vm);
out_fini:
xe_validation_ctx_fini(&ctx);
out_up_write:
up_write(&vm->lock);
return err;
}
ALLOW_ERROR_INJECTION(xe_vm_add_compute_exec_queue, ERRNO);
void xe_vm_remove_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
{
if (!xe_vm_in_preempt_fence_mode(vm))
return;
down_write(&vm->lock);
if (!list_empty(&q->lr.link)) {
list_del_init(&q->lr.link);
--vm->preempt.num_exec_queues;
}
if (q->lr.pfence) {
dma_fence_enable_signaling(q->lr.pfence);
dma_fence_put(q->lr.pfence);
q->lr.pfence = NULL;
}
up_write(&vm->lock);
}
#define XE_VM_REBIND_RETRY_TIMEOUT_MS 1000
void xe_vm_kill(struct xe_vm *vm, bool unlocked)
{
struct xe_exec_queue *q;
lockdep_assert_held(&vm->lock);
if (unlocked)
xe_vm_lock(vm, false);
vm->flags |= XE_VM_FLAG_BANNED;
trace_xe_vm_kill(vm);
list_for_each_entry(q, &vm->preempt.exec_queues, lr.link)
q->ops->kill(q);
if (unlocked)
xe_vm_unlock(vm);
}
static int xe_gpuvm_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec)
{
struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
struct xe_bo *bo = gem_to_xe_bo(vm_bo->obj);
struct drm_gpuva *gpuva;
int ret;
lockdep_assert_held(&vm->lock);
drm_gpuvm_bo_for_each_va(gpuva, vm_bo)
list_move_tail(&gpuva_to_vma(gpuva)->combined_links.rebind,
&vm->rebind_list);
if (xe_bo_is_purged(bo)) {
vm_bo->evicted = false;
return 0;
}
if (!try_wait_for_completion(&vm->xe->pm_block))
return -EAGAIN;
ret = xe_bo_validate(bo, vm, false, exec);
if (ret)
return ret;
vm_bo->evicted = false;
return 0;
}
int xe_vm_validate_rebind(struct xe_vm *vm, struct drm_exec *exec,
unsigned int num_fences)
{
struct drm_gem_object *obj;
int ret;
do {
ret = drm_gpuvm_validate(&vm->gpuvm, exec);
if (ret)
return ret;
ret = xe_vm_rebind(vm, false);
if (ret)
return ret;
} while (!list_empty(&vm->gpuvm.evict.list));
drm_exec_for_each_locked_object(exec, obj) {
ret = dma_resv_reserve_fences(obj->resv, num_fences);
if (ret)
return ret;
}
return 0;
}
void xe_vm_bump_lru(struct xe_vm *vm, struct drm_exec *exec)
{
spinlock_t *lru_lock = &vm->xe->ttm.lru_lock;
struct drm_gem_object *obj;
spin_lock(lru_lock);
ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
drm_exec_for_each_locked_object(exec, obj) {
if (!drm_gpuvm_is_extobj(&vm->gpuvm, obj))
continue;
ttm_bo_move_to_lru_tail(&gem_to_xe_bo(obj)->ttm);
cond_resched_lock(lru_lock);
}
spin_unlock(lru_lock);
}
static int xe_preempt_work_begin(struct drm_exec *exec, struct xe_vm *vm,
bool *done)
{
int err;
err = drm_gpuvm_prepare_vm(&vm->gpuvm, exec, 0);
if (err)
return err;
if (xe_vm_is_idle(vm)) {
vm->preempt.rebind_deactivated = true;
*done = true;
return 0;
}
if (!preempt_fences_waiting(vm)) {
*done = true;
return 0;
}
err = drm_gpuvm_prepare_objects(&vm->gpuvm, exec, 0);
if (err)
return err;
err = wait_for_existing_preempt_fences(vm);
if (err)
return err;
return xe_vm_validate_rebind(vm, exec, vm->preempt.num_exec_queues);
}
static bool vm_suspend_rebind_worker(struct xe_vm *vm)
{
struct xe_device *xe = vm->xe;
bool ret = false;
mutex_lock(&xe->rebind_resume_lock);
if (!try_wait_for_completion(&vm->xe->pm_block)) {
ret = true;
list_move_tail(&vm->preempt.pm_activate_link, &xe->rebind_resume_list);
}
mutex_unlock(&xe->rebind_resume_lock);
return ret;
}
void xe_vm_resume_rebind_worker(struct xe_vm *vm)
{
queue_work(vm->xe->ordered_wq, &vm->preempt.rebind_work);
}
static void preempt_rebind_work_func(struct work_struct *w)
{
struct xe_vm *vm = container_of(w, struct xe_vm, preempt.rebind_work);
struct xe_validation_ctx ctx;
struct drm_exec exec;
unsigned int fence_count = 0;
LIST_HEAD(preempt_fences);
int err = 0;
long wait;
int __maybe_unused tries = 0;
xe_assert(vm->xe, xe_vm_in_preempt_fence_mode(vm));
trace_xe_vm_rebind_worker_enter(vm);
down_write(&vm->lock);
if (xe_vm_is_closed_or_banned(vm)) {
up_write(&vm->lock);
trace_xe_vm_rebind_worker_exit(vm);
return;
}
retry:
if (!try_wait_for_completion(&vm->xe->pm_block) && vm_suspend_rebind_worker(vm)) {
up_write(&vm->lock);
xe_pm_might_block_on_suspend();
return;
}
if (xe_vm_userptr_check_repin(vm)) {
err = xe_vm_userptr_pin(vm);
if (err)
goto out_unlock_outer;
}
err = xe_validation_ctx_init(&ctx, &vm->xe->val, &exec,
(struct xe_val_flags) {.interruptible = true});
if (err)
goto out_unlock_outer;
drm_exec_until_all_locked(&exec) {
bool done = false;
err = xe_preempt_work_begin(&exec, vm, &done);
drm_exec_retry_on_contention(&exec);
xe_validation_retry_on_oom(&ctx, &err);
if (err || done) {
xe_validation_ctx_fini(&ctx);
goto out_unlock_outer;
}
}
err = alloc_preempt_fences(vm, &preempt_fences, &fence_count);
if (err)
goto out_unlock;
xe_vm_set_validation_exec(vm, &exec);
err = xe_vm_rebind(vm, true);
xe_vm_set_validation_exec(vm, NULL);
if (err)
goto out_unlock;
wait = dma_resv_wait_timeout(xe_vm_resv(vm),
DMA_RESV_USAGE_KERNEL,
false, MAX_SCHEDULE_TIMEOUT);
if (wait <= 0) {
err = -ETIME;
goto out_unlock;
}
#define retry_required(__tries, __vm) \
(IS_ENABLED(CONFIG_DRM_XE_USERPTR_INVAL_INJECT) ? \
(!(__tries)++ || __xe_vm_userptr_needs_repin(__vm)) : \
__xe_vm_userptr_needs_repin(__vm))
xe_svm_notifier_lock(vm);
if (retry_required(tries, vm)) {
xe_svm_notifier_unlock(vm);
err = -EAGAIN;
goto out_unlock;
}
#undef retry_required
xe_vm_bump_lru(vm, &exec);
arm_preempt_fences(vm, &preempt_fences);
resume_and_reinstall_preempt_fences(vm, &exec);
xe_svm_notifier_unlock(vm);
out_unlock:
xe_validation_ctx_fini(&ctx);
out_unlock_outer:
if (err == -EAGAIN) {
trace_xe_vm_rebind_worker_retry(vm);
if (IS_SRIOV_VF(vm->xe) &&
xe_sriov_vf_migration_supported(vm->xe)) {
up_write(&vm->lock);
xe_vm_queue_rebind_worker(vm);
return;
}
goto retry;
}
if (err) {
drm_warn(&vm->xe->drm, "VM worker error: %d\n", err);
xe_vm_kill(vm, true);
}
up_write(&vm->lock);
free_preempt_fences(&preempt_fences);
trace_xe_vm_rebind_worker_exit(vm);
}
void xe_vm_add_fault_entry_pf(struct xe_vm *vm, struct xe_pagefault *pf)
{
struct xe_vm_fault_entry *e;
struct xe_hw_engine *hwe;
u8 engine_class = FIELD_GET(XE_PAGEFAULT_ENGINE_CLASS_MASK,
pf->consumer.engine_class_instance);
u8 engine_instance = FIELD_GET(XE_PAGEFAULT_ENGINE_INSTANCE_MASK,
pf->consumer.engine_class_instance);
hwe = xe_gt_hw_engine(pf->gt, engine_class, engine_instance, false);
if (!hwe || xe_hw_engine_is_reserved(hwe))
return;
e = kzalloc_obj(*e);
if (!e) {
drm_warn(&vm->xe->drm,
"Could not allocate memory for fault!\n");
return;
}
guard(spinlock)(&vm->faults.lock);
if (vm->faults.len >= MAX_FAULTS_SAVED_PER_VM) {
kfree(e);
return;
}
e->address = xe_pagefault_addr(pf);
e->address_precision = SZ_4K;
e->access_type = pf->consumer.access_type;
e->fault_type = FIELD_GET(XE_PAGEFAULT_TYPE_MASK,
pf->consumer.fault_type_level);
e->fault_level = FIELD_GET(XE_PAGEFAULT_LEVEL_MASK,
pf->consumer.fault_type_level);
e->srcid = FIELD_GET(XE_PAGEFAULT_SRCID_MASK, pf->consumer.id);
list_add_tail(&e->list, &vm->faults.list);
vm->faults.len++;
}
static void xe_vm_clear_fault_entries(struct xe_vm *vm)
{
struct xe_vm_fault_entry *e, *tmp;
guard(spinlock)(&vm->faults.lock);
list_for_each_entry_safe(e, tmp, &vm->faults.list, list) {
list_del(&e->list);
kfree(e);
}
vm->faults.len = 0;
}
static noinline int xe_vma_ops_alloc(struct xe_vma_ops *vops, bool array_of_binds)
{
int i;
for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i) {
if (!vops->pt_update_ops[i].num_ops)
continue;
vops->pt_update_ops[i].pt_job_ops =
xe_pt_job_ops_alloc(vops->pt_update_ops[i].num_ops);
if (!vops->pt_update_ops[i].pt_job_ops)
return array_of_binds ? -ENOBUFS : -ENOMEM;
}
return 0;
}
ALLOW_ERROR_INJECTION(xe_vma_ops_alloc, ERRNO);
static void xe_vma_svm_prefetch_ranges_fini(struct xe_vma_op *op)
{
struct xe_svm_range *svm_range;
unsigned long i;
xa_for_each(&op->prefetch_range.range, i, svm_range)
xe_svm_range_put(svm_range);
xa_destroy(&op->prefetch_range.range);
}
static void xe_vma_svm_prefetch_op_fini(struct xe_vma_op *op)
{
struct xe_vma *vma;
vma = gpuva_to_vma(op->base.prefetch.va);
if (op->base.op == DRM_GPUVA_OP_PREFETCH && xe_vma_is_cpu_addr_mirror(vma))
xe_vma_svm_prefetch_ranges_fini(op);
}
static void xe_vma_svm_prefetch_ops_fini(struct xe_vma_ops *vops)
{
struct xe_vma_op *op;
if (!(vops->flags & XE_VMA_OPS_FLAG_HAS_SVM_PREFETCH))
return;
list_for_each_entry(op, &vops->list, link)
xe_vma_svm_prefetch_op_fini(op);
}
static void xe_vma_ops_fini(struct xe_vma_ops *vops)
{
int i;
xe_vma_svm_prefetch_ops_fini(vops);
for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
xe_pt_job_ops_put(vops->pt_update_ops[i].pt_job_ops);
}
static void xe_vma_ops_incr_pt_update_ops(struct xe_vma_ops *vops, u8 tile_mask, int inc_val)
{
int i;
if (!inc_val)
return;
for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
if (BIT(i) & tile_mask)
vops->pt_update_ops[i].num_ops += inc_val;
}
#define XE_VMA_CREATE_MASK ( \
XE_VMA_READ_ONLY | \
XE_VMA_DUMPABLE | \
XE_VMA_SYSTEM_ALLOCATOR | \
DRM_GPUVA_SPARSE | \
XE_VMA_MADV_AUTORESET)
static void xe_vm_populate_rebind(struct xe_vma_op *op, struct xe_vma *vma,
u8 tile_mask)
{
INIT_LIST_HEAD(&op->link);
op->tile_mask = tile_mask;
op->base.op = DRM_GPUVA_OP_MAP;
op->base.map.va.addr = vma->gpuva.va.addr;
op->base.map.va.range = vma->gpuva.va.range;
op->base.map.gem.obj = vma->gpuva.gem.obj;
op->base.map.gem.offset = vma->gpuva.gem.offset;
op->map.vma = vma;
op->map.immediate = true;
op->map.vma_flags = vma->gpuva.flags & XE_VMA_CREATE_MASK;
}
static int xe_vm_ops_add_rebind(struct xe_vma_ops *vops, struct xe_vma *vma,
u8 tile_mask)
{
struct xe_vma_op *op;
op = kzalloc_obj(*op);
if (!op)
return -ENOMEM;
xe_vm_populate_rebind(op, vma, tile_mask);
list_add_tail(&op->link, &vops->list);
xe_vma_ops_incr_pt_update_ops(vops, tile_mask, 1);
return 0;
}
static struct dma_fence *ops_execute(struct xe_vm *vm,
struct xe_vma_ops *vops);
static void xe_vma_ops_init(struct xe_vma_ops *vops, struct xe_vm *vm,
struct xe_exec_queue *q,
struct xe_sync_entry *syncs, u32 num_syncs);
int xe_vm_rebind(struct xe_vm *vm, bool rebind_worker)
{
struct dma_fence *fence;
struct xe_vma *vma, *next;
struct xe_vma_ops vops;
struct xe_vma_op *op, *next_op;
int err;
lockdep_assert_held(&vm->lock);
if ((xe_vm_in_lr_mode(vm) && !rebind_worker) ||
list_empty(&vm->rebind_list))
return 0;
xe_vma_ops_init(&vops, vm, xe_cpu_bind_queue(vm->xe->cpu_bind),
NULL, 0);
vops.flags |= XE_VMA_OPS_FLAG_WAIT_VM_BOOKKEEP;
xe_vm_assert_held(vm);
list_for_each_entry(vma, &vm->rebind_list, combined_links.rebind) {
xe_assert(vm->xe, vma->tile_present);
if (rebind_worker)
trace_xe_vma_rebind_worker(vma);
else
trace_xe_vma_rebind_exec(vma);
err = xe_vm_ops_add_rebind(&vops, vma,
vma->tile_present);
if (err)
goto free_ops;
}
err = xe_vma_ops_alloc(&vops, false);
if (err)
goto free_ops;
fence = ops_execute(vm, &vops);
if (IS_ERR(fence)) {
err = PTR_ERR(fence);
} else {
dma_fence_put(fence);
list_for_each_entry_safe(vma, next, &vm->rebind_list,
combined_links.rebind)
list_del_init(&vma->combined_links.rebind);
}
free_ops:
list_for_each_entry_safe(op, next_op, &vops.list, link) {
list_del(&op->link);
kfree(op);
}
xe_vma_ops_fini(&vops);
return err;
}
static u8 adjust_rebind_tile_mask(struct xe_vm *vm, u8 tile_mask)
{
if (vm->xe->info.has_pt_mirror)
return tile_mask;
return (0x1 << vm->xe->info.tile_count) - 1;
}
struct dma_fence *xe_vma_rebind(struct xe_vm *vm, struct xe_vma *vma, u8 tile_mask)
{
struct dma_fence *fence = NULL;
struct xe_vma_ops vops;
struct xe_vma_op *op, *next_op;
int err;
lockdep_assert_held(&vm->lock);
xe_vm_assert_held(vm);
xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
xe_vma_ops_init(&vops, vm, xe_cpu_bind_queue(vm->xe->cpu_bind),
NULL, 0);
vops.flags |= XE_VMA_OPS_FLAG_SKIP_TLB_WAIT |
XE_VMA_OPS_FLAG_WAIT_VM_BOOKKEEP;
err = xe_vm_ops_add_rebind(&vops, vma,
adjust_rebind_tile_mask(vm, tile_mask));
if (err)
return ERR_PTR(err);
err = xe_vma_ops_alloc(&vops, false);
if (err) {
fence = ERR_PTR(err);
goto free_ops;
}
fence = ops_execute(vm, &vops);
free_ops:
list_for_each_entry_safe(op, next_op, &vops.list, link) {
list_del(&op->link);
kfree(op);
}
xe_vma_ops_fini(&vops);
return fence;
}
static void xe_vm_populate_range_rebind(struct xe_vma_op *op,
struct xe_vma *vma,
struct xe_svm_range *range,
u8 tile_mask)
{
INIT_LIST_HEAD(&op->link);
op->tile_mask = tile_mask;
op->base.op = DRM_GPUVA_OP_DRIVER;
op->subop = XE_VMA_SUBOP_MAP_RANGE;
op->map_range.vma = vma;
op->map_range.range = range;
}
static int
xe_vm_ops_add_range_rebind(struct xe_vma_ops *vops,
struct xe_vma *vma,
struct xe_svm_range *range,
u8 tile_mask)
{
struct xe_vma_op *op;
op = kzalloc_obj(*op);
if (!op)
return -ENOMEM;
xe_vm_populate_range_rebind(op, vma, range, tile_mask);
list_add_tail(&op->link, &vops->list);
xe_vma_ops_incr_pt_update_ops(vops, tile_mask, 1);
return 0;
}
struct dma_fence *xe_vm_range_rebind(struct xe_vm *vm,
struct xe_vma *vma,
struct xe_svm_range *range,
u8 tile_mask)
{
struct dma_fence *fence = NULL;
struct xe_vma_ops vops;
struct xe_vma_op *op, *next_op;
int err;
lockdep_assert_held(&range->lock);
lockdep_assert_held(&vm->lock);
xe_vm_assert_held(vm);
xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
xe_assert(vm->xe, xe_vma_is_cpu_addr_mirror(vma));
xe_vma_ops_init(&vops, vm, xe_cpu_bind_queue(vm->xe->cpu_bind),
NULL, 0);
vops.flags |= XE_VMA_OPS_FLAG_SKIP_TLB_WAIT |
XE_VMA_OPS_FLAG_WAIT_VM_BOOKKEEP;
err = xe_vm_ops_add_range_rebind(&vops, vma, range,
adjust_rebind_tile_mask(vm, tile_mask));
if (err)
return ERR_PTR(err);
err = xe_vma_ops_alloc(&vops, false);
if (err) {
fence = ERR_PTR(err);
goto free_ops;
}
fence = ops_execute(vm, &vops);
free_ops:
list_for_each_entry_safe(op, next_op, &vops.list, link) {
list_del(&op->link);
kfree(op);
}
xe_vma_ops_fini(&vops);
return fence;
}
static void xe_vm_populate_range_unbind(struct xe_vma_op *op,
struct xe_svm_range *range)
{
INIT_LIST_HEAD(&op->link);
op->tile_mask = range->tile_present;
op->base.op = DRM_GPUVA_OP_DRIVER;
op->subop = XE_VMA_SUBOP_UNMAP_RANGE;
op->unmap_range.range = range;
}
static int
xe_vm_ops_add_range_unbind(struct xe_vma_ops *vops,
struct xe_svm_range *range)
{
struct xe_vma_op *op;
op = kzalloc_obj(*op);
if (!op)
return -ENOMEM;
xe_vm_populate_range_unbind(op, range);
list_add_tail(&op->link, &vops->list);
xe_vma_ops_incr_pt_update_ops(vops, range->tile_present, 1);
return 0;
}
struct dma_fence *xe_vm_range_unbind(struct xe_vm *vm,
struct xe_svm_range *range)
{
struct dma_fence *fence = NULL;
struct xe_vma_ops vops;
struct xe_vma_op *op, *next_op;
int err;
lockdep_assert_held(&range->lock);
lockdep_assert_held(&vm->lock);
xe_vm_assert_held(vm);
xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
if (!range->tile_present)
return dma_fence_get_stub();
xe_vma_ops_init(&vops, vm, xe_cpu_bind_queue(vm->xe->cpu_bind),
NULL, 0);
vops.flags |= XE_VMA_OPS_FLAG_WAIT_VM_BOOKKEEP;
err = xe_vm_ops_add_range_unbind(&vops, range);
if (err)
return ERR_PTR(err);
err = xe_vma_ops_alloc(&vops, false);
if (err) {
fence = ERR_PTR(err);
goto free_ops;
}
fence = ops_execute(vm, &vops);
free_ops:
list_for_each_entry_safe(op, next_op, &vops.list, link) {
list_del(&op->link);
kfree(op);
}
xe_vma_ops_fini(&vops);
return fence;
}
static void xe_vma_mem_attr_fini(struct xe_vma_mem_attr *attr)
{
drm_pagemap_put(attr->preferred_loc.dpagemap);
}
static void xe_vma_free(struct xe_vma *vma)
{
xe_vma_mem_attr_fini(&vma->attr);
if (xe_vma_is_userptr(vma))
kfree(to_userptr_vma(vma));
else
kfree(vma);
}
void xe_vma_mem_attr_copy(struct xe_vma_mem_attr *to, struct xe_vma_mem_attr *from)
{
xe_vma_mem_attr_fini(to);
*to = *from;
if (to->preferred_loc.dpagemap)
drm_pagemap_get(to->preferred_loc.dpagemap);
}
static struct xe_vma *xe_vma_create(struct xe_vm *vm,
struct xe_bo *bo,
u64 bo_offset_or_userptr,
u64 start, u64 end,
struct xe_vma_mem_attr *attr,
unsigned int flags)
{
struct xe_vma *vma;
struct xe_tile *tile;
u8 id;
bool is_null = (flags & DRM_GPUVA_SPARSE);
bool is_cpu_addr_mirror = (flags & XE_VMA_SYSTEM_ALLOCATOR);
xe_assert(vm->xe, start < end);
xe_assert(vm->xe, end < vm->size);
if (!bo && !is_null && !is_cpu_addr_mirror) {
struct xe_userptr_vma *uvma = kzalloc_obj(*uvma);
if (!uvma)
return ERR_PTR(-ENOMEM);
vma = &uvma->vma;
} else {
vma = kzalloc_obj(*vma);
if (!vma)
return ERR_PTR(-ENOMEM);
if (bo)
vma->gpuva.gem.obj = &bo->ttm.base;
}
INIT_LIST_HEAD(&vma->combined_links.rebind);
INIT_LIST_HEAD(&vma->gpuva.gem.entry);
vma->gpuva.vm = &vm->gpuvm;
vma->gpuva.va.addr = start;
vma->gpuva.va.range = end - start + 1;
vma->gpuva.flags = flags;
for_each_tile(tile, vm->xe, id)
vma->tile_mask |= 0x1 << id;
if (vm->xe->info.has_atomic_enable_pte_bit)
vma->gpuva.flags |= XE_VMA_ATOMIC_PTE_BIT;
xe_vma_mem_attr_copy(&vma->attr, attr);
if (bo) {
struct drm_gpuvm_bo *vm_bo;
xe_bo_assert_held(bo);
if (vma->attr.purgeable_state == XE_MADV_PURGEABLE_WILLNEED) {
if (xe_bo_madv_is_dontneed(bo)) {
xe_vma_free(vma);
return ERR_PTR(-EBUSY);
}
if (xe_bo_is_purged(bo)) {
xe_vma_free(vma);
return ERR_PTR(-EINVAL);
}
}
vm_bo = drm_gpuvm_bo_obtain_locked(vma->gpuva.vm, &bo->ttm.base);
if (IS_ERR(vm_bo)) {
xe_vma_free(vma);
return ERR_CAST(vm_bo);
}
drm_gpuvm_bo_extobj_add(vm_bo);
drm_gem_object_get(&bo->ttm.base);
vma->gpuva.gem.offset = bo_offset_or_userptr;
drm_gpuva_link(&vma->gpuva, vm_bo);
drm_gpuvm_bo_put(vm_bo);
xe_bo_vma_count_inc_locked(bo);
if (vma->attr.purgeable_state == XE_MADV_PURGEABLE_WILLNEED)
xe_bo_willneed_get_locked(bo);
} else {
if (!is_null && !is_cpu_addr_mirror) {
struct xe_userptr_vma *uvma = to_userptr_vma(vma);
u64 size = end - start + 1;
int err;
vma->gpuva.gem.offset = bo_offset_or_userptr;
err = xe_userptr_setup(uvma, xe_vma_userptr(vma), size);
if (err) {
xe_vma_free(vma);
return ERR_PTR(err);
}
}
xe_vm_get(vm);
}
mutex_init(&vma->fault_lock);
return vma;
}
static void xe_vma_destroy_late(struct xe_vma *vma)
{
struct xe_vm *vm = xe_vma_vm(vma);
struct xe_bo *bo = xe_vma_bo(vma);
if (vma->ufence) {
xe_sync_ufence_put(vma->ufence);
vma->ufence = NULL;
}
if (xe_vma_is_userptr(vma)) {
struct xe_userptr_vma *uvma = to_userptr_vma(vma);
xe_userptr_remove(uvma);
xe_vm_put(vm);
} else if (xe_vma_is_null(vma) || xe_vma_is_cpu_addr_mirror(vma)) {
xe_vm_put(vm);
} else {
xe_bo_put(bo);
}
mutex_destroy(&vma->fault_lock);
xe_vma_free(vma);
}
static void vma_destroy_work_func(struct work_struct *w)
{
struct xe_vma *vma =
container_of(w, struct xe_vma, destroy_work);
xe_vma_destroy_late(vma);
}
static void vma_destroy_cb(struct dma_fence *fence,
struct dma_fence_cb *cb)
{
struct xe_vma *vma = container_of(cb, struct xe_vma, destroy_cb);
INIT_WORK(&vma->destroy_work, vma_destroy_work_func);
queue_work(system_dfl_wq, &vma->destroy_work);
}
static void xe_vm_assert_write_mode_or_garbage_collector(struct xe_vm *vm)
{
lockdep_assert(lockdep_is_held_type(&vm->lock, 0) ||
(lockdep_is_held_type(&vm->lock, 1) &&
lockdep_is_held_type(&vm->svm.garbage_collector.lock, 0)));
}
static void xe_vma_destroy(struct xe_vma *vma, struct dma_fence *fence)
{
struct xe_vm *vm = xe_vma_vm(vma);
struct xe_bo *bo = xe_vma_bo(vma);
xe_vm_assert_write_mode_or_garbage_collector(vm);
xe_assert(vm->xe, list_empty(&vma->combined_links.destroy));
if (xe_vma_is_userptr(vma)) {
xe_assert(vm->xe, vma->gpuva.flags & XE_VMA_DESTROYED);
xe_userptr_destroy(to_userptr_vma(vma));
} else if (!xe_vma_is_null(vma) && !xe_vma_is_cpu_addr_mirror(vma)) {
xe_bo_assert_held(bo);
drm_gpuva_unlink(&vma->gpuva);
xe_bo_vma_count_dec_locked(bo);
if (vma->attr.purgeable_state == XE_MADV_PURGEABLE_WILLNEED)
xe_bo_willneed_put_locked(bo);
}
xe_vm_assert_held(vm);
if (fence) {
int ret = dma_fence_add_callback(fence, &vma->destroy_cb,
vma_destroy_cb);
if (ret) {
XE_WARN_ON(ret != -ENOENT);
xe_vma_destroy_late(vma);
}
} else {
xe_vma_destroy_late(vma);
}
}
int xe_vm_lock_vma(struct drm_exec *exec, struct xe_vma *vma)
{
struct xe_vm *vm = xe_vma_vm(vma);
struct xe_bo *bo = xe_vma_bo(vma);
int err;
XE_WARN_ON(!vm);
err = drm_exec_lock_obj(exec, xe_vm_obj(vm));
if (!err && bo && !bo->vm)
err = drm_exec_lock_obj(exec, &bo->ttm.base);
return err;
}
static void xe_vma_destroy_unlocked(struct xe_vma *vma)
{
struct xe_device *xe = xe_vma_vm(vma)->xe;
struct xe_validation_ctx ctx;
struct drm_exec exec;
int err = 0;
xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {}, err) {
err = xe_vm_lock_vma(&exec, vma);
drm_exec_retry_on_contention(&exec);
if (XE_WARN_ON(err))
break;
xe_vma_destroy(vma, NULL);
}
xe_assert(xe, !err);
}
struct xe_vma *
xe_vm_find_overlapping_vma(struct xe_vm *vm, u64 start, u64 range)
{
struct drm_gpuva *gpuva;
lockdep_assert_held(&vm->lock);
if (xe_vm_is_closed_or_banned(vm))
return NULL;
xe_assert(vm->xe, start + range <= vm->size);
mutex_lock(&vm->snap_mutex);
gpuva = drm_gpuva_find_first(&vm->gpuvm, start, range);
mutex_unlock(&vm->snap_mutex);
return gpuva ? gpuva_to_vma(gpuva) : NULL;
}
static int xe_vm_insert_vma(struct xe_vm *vm, struct xe_vma *vma)
{
int err;
xe_assert(vm->xe, xe_vma_vm(vma) == vm);
xe_vm_assert_write_mode_or_garbage_collector(vm);
mutex_lock(&vm->snap_mutex);
err = drm_gpuva_insert(&vm->gpuvm, &vma->gpuva);
mutex_unlock(&vm->snap_mutex);
XE_WARN_ON(err);
return err;
}
static void xe_vm_remove_vma(struct xe_vm *vm, struct xe_vma *vma)
{
xe_assert(vm->xe, xe_vma_vm(vma) == vm);
xe_vm_assert_write_mode_or_garbage_collector(vm);
mutex_lock(&vm->snap_mutex);
drm_gpuva_remove(&vma->gpuva);
mutex_unlock(&vm->snap_mutex);
}
static struct drm_gpuva_op *xe_vm_op_alloc(void)
{
struct xe_vma_op *op;
op = kzalloc_obj(*op);
if (unlikely(!op))
return NULL;
return &op->base;
}
static void xe_vm_free(struct drm_gpuvm *gpuvm);
static const struct drm_gpuvm_ops gpuvm_ops = {
.op_alloc = xe_vm_op_alloc,
.vm_bo_validate = xe_gpuvm_validate,
.vm_free = xe_vm_free,
};
static u64 pde_encode_pat_index(u16 pat_index)
{
u64 pte = 0;
if (pat_index & BIT(0))
pte |= XE_PPGTT_PTE_PAT0;
if (pat_index & BIT(1))
pte |= XE_PPGTT_PTE_PAT1;
return pte;
}
static u64 pte_encode_pat_index(u16 pat_index, u32 pt_level)
{
u64 pte = 0;
if (pat_index & BIT(0))
pte |= XE_PPGTT_PTE_PAT0;
if (pat_index & BIT(1))
pte |= XE_PPGTT_PTE_PAT1;
if (pat_index & BIT(2)) {
if (pt_level)
pte |= XE_PPGTT_PDE_PDPE_PAT2;
else
pte |= XE_PPGTT_PTE_PAT2;
}
if (pat_index & BIT(3))
pte |= XELPG_PPGTT_PTE_PAT3;
if (pat_index & (BIT(4)))
pte |= XE2_PPGTT_PTE_PAT4;
return pte;
}
static u64 pte_encode_ps(u32 pt_level)
{
XE_WARN_ON(pt_level > MAX_HUGEPTE_LEVEL);
if (pt_level == 1)
return XE_PDE_PS_2M;
else if (pt_level == 2)
return XE_PDPE_PS_1G;
return 0;
}
static u16 pde_pat_index(struct xe_bo *bo)
{
struct xe_device *xe = xe_bo_device(bo);
u16 pat_index;
if (!xe_bo_is_vram(bo) && bo->ttm.ttm->caching == ttm_cached)
pat_index = xe_cache_pat_idx(xe, XE_CACHE_WB);
else
pat_index = xe_cache_pat_idx(xe, XE_CACHE_NONE);
xe_assert(xe, pat_index <= 3);
return pat_index;
}
static u64 xelp_pde_encode_bo(struct xe_bo *bo, u64 bo_offset)
{
u64 pde;
pde = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
pde |= XE_PAGE_PRESENT | XE_PAGE_RW;
pde |= pde_encode_pat_index(pde_pat_index(bo));
return pde;
}
static u64 xelp_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
u16 pat_index, u32 pt_level)
{
u64 pte;
pte = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
pte |= pte_encode_pat_index(pat_index, pt_level);
pte |= pte_encode_ps(pt_level);
if (xe_bo_is_vram(bo) || xe_bo_is_stolen_devmem(bo))
pte |= XE_PPGTT_PTE_DM;
return pte;
}
static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma,
u16 pat_index, u32 pt_level)
{
struct xe_bo *bo = xe_vma_bo(vma);
struct xe_vm *vm = xe_vma_vm(vma);
pte |= XE_PAGE_PRESENT;
if (likely(!xe_vma_read_only(vma)))
pte |= XE_PAGE_RW;
pte |= pte_encode_pat_index(pat_index, pt_level);
pte |= pte_encode_ps(pt_level);
if (unlikely(xe_vma_is_null(vma) ||
(bo && xe_bo_is_purged(bo) && xe_vm_has_scratch(vm))))
pte |= XE_PTE_NULL;
return pte;
}
static u64 xelp_pte_encode_addr(struct xe_device *xe, u64 addr,
u16 pat_index,
u32 pt_level, bool devmem, u64 flags)
{
u64 pte;
xe_assert(xe, !(flags & ~XE_PTE_PS64));
pte = addr;
pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
pte |= pte_encode_pat_index(pat_index, pt_level);
pte |= pte_encode_ps(pt_level);
if (devmem)
pte |= XE_PPGTT_PTE_DM;
pte |= flags;
return pte;
}
static const struct xe_pt_ops xelp_pt_ops = {
.pte_encode_bo = xelp_pte_encode_bo,
.pte_encode_vma = xelp_pte_encode_vma,
.pte_encode_addr = xelp_pte_encode_addr,
.pde_encode_bo = xelp_pde_encode_bo,
};
static void vm_destroy_work_func(struct work_struct *w);
static int xe_vm_create_scratch(struct xe_device *xe, struct xe_tile *tile,
struct xe_vm *vm, struct drm_exec *exec)
{
u8 id = tile->id;
int i;
for (i = MAX_HUGEPTE_LEVEL; i < vm->pt_root[id]->level; i++) {
vm->scratch_pt[id][i] = xe_pt_create(vm, tile, i, exec);
if (IS_ERR(vm->scratch_pt[id][i])) {
int err = PTR_ERR(vm->scratch_pt[id][i]);
vm->scratch_pt[id][i] = NULL;
return err;
}
xe_pt_populate_empty(tile, vm, vm->scratch_pt[id][i]);
}
return 0;
}
ALLOW_ERROR_INJECTION(xe_vm_create_scratch, ERRNO);
static void xe_vm_free_scratch(struct xe_vm *vm)
{
struct xe_tile *tile;
u8 id;
if (!xe_vm_has_scratch(vm))
return;
for_each_tile(tile, vm->xe, id) {
u32 i;
if (!vm->pt_root[id])
continue;
for (i = MAX_HUGEPTE_LEVEL; i < vm->pt_root[id]->level; ++i)
if (vm->scratch_pt[id][i])
xe_pt_destroy(vm->scratch_pt[id][i], vm->flags, NULL);
}
}
static void xe_vm_pt_destroy(struct xe_vm *vm)
{
struct xe_tile *tile;
u8 id;
xe_vm_assert_held(vm);
for_each_tile(tile, vm->xe, id) {
if (vm->pt_root[id]) {
xe_pt_destroy(vm->pt_root[id], vm->flags, NULL);
vm->pt_root[id] = NULL;
}
}
}
static void xe_vm_init_prove_locking(struct xe_device *xe, struct xe_vm *vm)
{
if (!IS_ENABLED(CONFIG_PROVE_LOCKING))
return;
fs_reclaim_acquire(GFP_KERNEL);
might_lock(&vm->exec_queues.lock);
fs_reclaim_release(GFP_KERNEL);
down_read(&vm->exec_queues.lock);
might_lock(&xe_root_mmio_gt(xe)->uc.guc.ct.lock);
up_read(&vm->exec_queues.lock);
}
struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags, struct xe_file *xef)
{
struct drm_gem_object *vm_resv_obj;
struct xe_validation_ctx ctx;
struct drm_exec exec;
struct xe_vm *vm;
int err;
struct xe_tile *tile;
u8 id;
xe_assert(xe, !((flags & XE_VM_FLAG_GSC) && (flags & XE_VM_FLAG_FAULT_MODE)));
vm = kzalloc_obj(*vm);
if (!vm)
return ERR_PTR(-ENOMEM);
vm->xe = xe;
vm->size = 1ull << xe->info.va_bits;
vm->flags = flags;
if (xef)
vm->xef = xe_file_get(xef);
if (flags & XE_VM_FLAG_GSC) {
static struct lock_class_key gsc_vm_key;
__init_rwsem(&vm->lock, "gsc_vm", &gsc_vm_key);
} else {
init_rwsem(&vm->lock);
}
mutex_init(&vm->snap_mutex);
INIT_LIST_HEAD(&vm->rebind_list);
INIT_LIST_HEAD(&vm->userptr.repin_list);
INIT_LIST_HEAD(&vm->userptr.invalidated);
spin_lock_init(&vm->userptr.invalidated_lock);
INIT_LIST_HEAD(&vm->faults.list);
spin_lock_init(&vm->faults.lock);
ttm_lru_bulk_move_init(&vm->lru_bulk_move);
INIT_WORK(&vm->destroy_work, vm_destroy_work_func);
INIT_LIST_HEAD(&vm->preempt.exec_queues);
for (id = 0; id < XE_MAX_TILES_PER_DEVICE * XE_MAX_GT_PER_TILE; ++id)
INIT_LIST_HEAD(&vm->exec_queues.list[id]);
if (flags & XE_VM_FLAG_FAULT_MODE)
vm->preempt.min_run_period_ms = xe->min_run_period_pf_ms;
else
vm->preempt.min_run_period_ms = xe->min_run_period_lr_ms;
init_rwsem(&vm->exec_queues.lock);
xe_vm_init_prove_locking(xe, vm);
xe_range_fence_tree_init(&vm->rftree);
vm->pt_ops = &xelp_pt_ops;
if (flags & XE_VM_FLAG_LR_MODE) {
INIT_WORK(&vm->preempt.rebind_work, preempt_rebind_work_func);
xe_pm_runtime_get_noresume(xe);
INIT_LIST_HEAD(&vm->preempt.pm_activate_link);
}
err = xe_svm_init(vm);
if (err)
goto err_no_resv;
vm_resv_obj = drm_gpuvm_resv_object_alloc(&xe->drm);
if (!vm_resv_obj) {
err = -ENOMEM;
goto err_svm_fini;
}
drm_gpuvm_init(&vm->gpuvm, "Xe VM", DRM_GPUVM_RESV_PROTECTED, &xe->drm,
vm_resv_obj, 0, vm->size, 0, 0, &gpuvm_ops);
drm_gem_object_put(vm_resv_obj);
err = 0;
xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = true},
err) {
err = xe_vm_drm_exec_lock(vm, &exec);
drm_exec_retry_on_contention(&exec);
if (IS_DGFX(xe) && xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K)
vm->flags |= XE_VM_FLAG_64K;
for_each_tile(tile, xe, id) {
if (flags & XE_VM_FLAG_MIGRATION &&
tile->id != XE_VM_FLAG_TILE_ID(flags) &&
(vm->xe->info.has_pt_mirror || id))
continue;
vm->pt_root[id] = xe_pt_create(vm, tile, xe->info.vm_max_level,
&exec);
if (IS_ERR(vm->pt_root[id])) {
err = PTR_ERR(vm->pt_root[id]);
vm->pt_root[id] = NULL;
xe_vm_pt_destroy(vm);
drm_exec_retry_on_contention(&exec);
xe_validation_retry_on_oom(&ctx, &err);
break;
}
}
if (err)
break;
if (xe_vm_has_scratch(vm)) {
for_each_tile(tile, xe, id) {
if (!vm->pt_root[id])
continue;
err = xe_vm_create_scratch(xe, tile, vm, &exec);
if (err) {
xe_vm_free_scratch(vm);
xe_vm_pt_destroy(vm);
drm_exec_retry_on_contention(&exec);
xe_validation_retry_on_oom(&ctx, &err);
break;
}
}
if (err)
break;
vm->batch_invalidate_tlb = true;
}
if (vm->flags & XE_VM_FLAG_LR_MODE)
vm->batch_invalidate_tlb = false;
for_each_tile(tile, xe, id) {
if (!vm->pt_root[id])
continue;
xe_pt_populate_empty(tile, vm, vm->pt_root[id]);
}
}
if (err)
goto err_close;
if (!(flags & XE_VM_FLAG_MIGRATION)) {
for_each_tile(tile, xe, id) {
struct xe_exec_queue *q;
u32 create_flags = EXEC_QUEUE_FLAG_VM;
if (!vm->pt_root[id] || vm->q)
continue;
if (!xef)
create_flags |= EXEC_QUEUE_FLAG_KERNEL;
q = xe_exec_queue_create_bind(xe, tile, vm, create_flags, 0);
if (IS_ERR(q)) {
err = PTR_ERR(q);
goto err_close;
}
vm->q = q;
}
}
if (xef && xe->info.has_asid) {
u32 asid;
down_write(&xe->usm.lock);
err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, vm,
XA_LIMIT(1, XE_MAX_ASID - 1),
&xe->usm.next_asid, GFP_NOWAIT);
up_write(&xe->usm.lock);
if (err < 0)
goto err_close;
vm->usm.asid = asid;
}
trace_xe_vm_create(vm);
return vm;
err_close:
xe_vm_close_and_put(vm);
return ERR_PTR(err);
err_svm_fini:
vm->size = 0;
if (flags & XE_VM_FLAG_FAULT_MODE)
xe_svm_close(vm);
xe_svm_fini(vm);
err_no_resv:
mutex_destroy(&vm->snap_mutex);
xe_range_fence_tree_fini(&vm->rftree);
ttm_lru_bulk_move_fini(&xe->ttm, &vm->lru_bulk_move);
if (vm->xef)
xe_file_put(vm->xef);
kfree(vm);
if (flags & XE_VM_FLAG_LR_MODE)
xe_pm_runtime_put(xe);
return ERR_PTR(err);
}
static void xe_vm_close(struct xe_vm *vm)
{
struct xe_device *xe = vm->xe;
bool bound;
int idx;
bound = drm_dev_enter(&xe->drm, &idx);
down_write(&vm->lock);
xe_vm_lock(vm, false);
if (xe_vm_in_fault_mode(vm))
xe_svm_notifier_lock(vm);
vm->size = 0;
if (!((vm->flags & XE_VM_FLAG_MIGRATION))) {
struct xe_tile *tile;
struct xe_gt *gt;
u8 id;
dma_resv_wait_timeout(xe_vm_resv(vm),
DMA_RESV_USAGE_BOOKKEEP,
false, MAX_SCHEDULE_TIMEOUT);
if (bound) {
for_each_tile(tile, xe, id)
if (vm->pt_root[id])
xe_pt_clear(xe, vm->pt_root[id]);
for_each_gt(gt, xe, id)
xe_tlb_inval_vm(>->tlb_inval, vm);
}
}
if (xe_vm_in_fault_mode(vm))
xe_svm_notifier_unlock(vm);
xe_vm_unlock(vm);
up_write(&vm->lock);
if (bound)
drm_dev_exit(idx);
}
void xe_vm_close_and_put(struct xe_vm *vm)
{
LIST_HEAD(contested);
struct xe_device *xe = vm->xe;
struct xe_vma *vma, *next_vma;
struct drm_gpuva *gpuva, *next;
xe_assert(xe, !vm->preempt.num_exec_queues);
trace_xe_vm_close_and_put(vm);
xe_vm_close(vm);
if (xe_vm_in_preempt_fence_mode(vm)) {
mutex_lock(&xe->rebind_resume_lock);
list_del_init(&vm->preempt.pm_activate_link);
mutex_unlock(&xe->rebind_resume_lock);
flush_work(&vm->preempt.rebind_work);
}
if (xe_vm_in_fault_mode(vm))
xe_svm_close(vm);
if (vm->q) {
int i;
down_write(&vm->lock);
xe_exec_queue_last_fence_put(vm->q, vm);
for_each_tlb_inval(vm->q, i)
xe_exec_queue_tlb_inval_last_fence_put(vm->q, vm, i);
up_write(&vm->lock);
xe_exec_queue_kill(vm->q);
xe_exec_queue_put(vm->q);
vm->q = NULL;
}
down_write(&vm->lock);
xe_vm_lock(vm, false);
drm_gpuvm_for_each_va_safe(gpuva, next, &vm->gpuvm) {
vma = gpuva_to_vma(gpuva);
if (xe_vma_has_no_bo(vma)) {
xe_svm_notifier_lock(vm);
vma->gpuva.flags |= XE_VMA_DESTROYED;
xe_svm_notifier_unlock(vm);
}
xe_vm_remove_vma(vm, vma);
if (xe_vma_has_no_bo(vma) || xe_vma_bo(vma)->vm) {
list_del_init(&vma->combined_links.rebind);
xe_vma_destroy(vma, NULL);
continue;
}
list_move_tail(&vma->combined_links.destroy, &contested);
vma->gpuva.flags |= XE_VMA_DESTROYED;
}
xe_vm_unlock(vm);
list_for_each_entry_safe(vma, next_vma, &contested,
combined_links.destroy) {
list_del_init(&vma->combined_links.destroy);
xe_vma_destroy_unlocked(vma);
}
xe_vm_lock(vm, false);
xe_vm_free_scratch(vm);
xe_vm_pt_destroy(vm);
xe_vm_unlock(vm);
xe_svm_fini(vm);
up_write(&vm->lock);
down_write(&xe->usm.lock);
if (vm->usm.asid) {
void *lookup;
xe_assert(xe, xe->info.has_asid);
xe_assert(xe, !(vm->flags & XE_VM_FLAG_MIGRATION));
trace_xe_vm_asid_release(vm);
lookup = xa_erase(&xe->usm.asid_to_vm, vm->usm.asid);
xe_assert(xe, lookup == vm);
}
up_write(&xe->usm.lock);
xe_vm_clear_fault_entries(vm);
xe_range_fence_tree_fini(&vm->rftree);
xe_vm_put(vm);
}
static void vm_destroy_work_func(struct work_struct *w)
{
struct xe_vm *vm =
container_of(w, struct xe_vm, destroy_work);
struct xe_device *xe = vm->xe;
struct xe_tile *tile;
u8 id;
xe_assert(xe, !vm->size);
if (xe_vm_in_preempt_fence_mode(vm))
flush_work(&vm->preempt.rebind_work);
mutex_destroy(&vm->snap_mutex);
if (vm->flags & XE_VM_FLAG_LR_MODE)
xe_pm_runtime_put(xe);
for_each_tile(tile, xe, id)
XE_WARN_ON(vm->pt_root[id]);
trace_xe_vm_free(vm);
ttm_lru_bulk_move_fini(&xe->ttm, &vm->lru_bulk_move);
if (vm->xef)
xe_file_put(vm->xef);
kfree(vm);
}
static void xe_vm_free(struct drm_gpuvm *gpuvm)
{
struct xe_vm *vm = container_of(gpuvm, struct xe_vm, gpuvm);
queue_work(system_dfl_wq, &vm->destroy_work);
}
struct xe_vm *xe_vm_lookup(struct xe_file *xef, u32 id)
{
struct xe_vm *vm;
mutex_lock(&xef->vm.lock);
vm = xa_load(&xef->vm.xa, id);
if (vm)
xe_vm_get(vm);
mutex_unlock(&xef->vm.lock);
return vm;
}
u64 xe_vm_pdp4_descriptor(struct xe_vm *vm, struct xe_tile *tile)
{
return vm->pt_ops->pde_encode_bo(xe_vm_pt_root(vm, tile->id)->bo, 0);
}
static struct xe_exec_queue *
to_wait_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
{
return q ? q : vm->q;
}
static struct xe_user_fence *
find_ufence_get(struct xe_sync_entry *syncs, u32 num_syncs)
{
unsigned int i;
for (i = 0; i < num_syncs; i++) {
struct xe_sync_entry *e = &syncs[i];
if (xe_sync_is_ufence(e))
return xe_sync_ufence_get(e);
}
return NULL;
}
#define ALL_DRM_XE_VM_CREATE_FLAGS (DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE | \
DRM_XE_VM_CREATE_FLAG_LR_MODE | \
DRM_XE_VM_CREATE_FLAG_FAULT_MODE | \
DRM_XE_VM_CREATE_FLAG_NO_VM_OVERCOMMIT)
int xe_vm_create_ioctl(struct drm_device *dev, void *data,
struct drm_file *file)
{
struct xe_device *xe = to_xe_device(dev);
struct xe_file *xef = to_xe_file(file);
struct drm_xe_vm_create *args = data;
struct xe_gt *wa_gt = xe_root_mmio_gt(xe);
struct xe_vm *vm;
u32 id;
int err;
u32 flags = 0;
if (XE_IOCTL_DBG(xe, args->extensions))
return -EINVAL;
if (wa_gt && XE_GT_WA(wa_gt, 22014953428))
args->flags |= DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE;
if (XE_IOCTL_DBG(xe, args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE &&
!xe->info.has_usm))
return -EINVAL;
if (XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
return -EINVAL;
if (XE_IOCTL_DBG(xe, args->flags & ~ALL_DRM_XE_VM_CREATE_FLAGS))
return -EINVAL;
if (XE_IOCTL_DBG(xe, args->flags & DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE &&
args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE &&
!xe->info.needs_scratch))
return -EINVAL;
if (XE_IOCTL_DBG(xe, !(args->flags & DRM_XE_VM_CREATE_FLAG_LR_MODE) &&
args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE))
return -EINVAL;
if (XE_IOCTL_DBG(xe, !(args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE) &&
args->flags & DRM_XE_VM_CREATE_FLAG_NO_VM_OVERCOMMIT))
return -EINVAL;
if (args->flags & DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE)
flags |= XE_VM_FLAG_SCRATCH_PAGE;
if (args->flags & DRM_XE_VM_CREATE_FLAG_LR_MODE)
flags |= XE_VM_FLAG_LR_MODE;
if (args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE)
flags |= XE_VM_FLAG_FAULT_MODE;
if (args->flags & DRM_XE_VM_CREATE_FLAG_NO_VM_OVERCOMMIT)
flags |= XE_VM_FLAG_NO_VM_OVERCOMMIT;
vm = xe_vm_create(xe, flags, xef);
if (IS_ERR(vm))
return PTR_ERR(vm);
#if IS_ENABLED(CONFIG_DRM_XE_DEBUG_MEM)
args->reserved[0] = xe_bo_main_addr(vm->pt_root[0]->bo, XE_PAGE_SIZE);
#endif
err = xa_alloc(&xef->vm.xa, &id, vm, xa_limit_32b, GFP_KERNEL);
if (err)
goto err_close_and_put;
args->vm_id = id;
return 0;
err_close_and_put:
xe_vm_close_and_put(vm);
return err;
}
int xe_vm_destroy_ioctl(struct drm_device *dev, void *data,
struct drm_file *file)
{
struct xe_device *xe = to_xe_device(dev);
struct xe_file *xef = to_xe_file(file);
struct drm_xe_vm_destroy *args = data;
struct xe_vm *vm;
int err = 0;
if (XE_IOCTL_DBG(xe, args->pad) ||
XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
return -EINVAL;
mutex_lock(&xef->vm.lock);
vm = xa_load(&xef->vm.xa, args->vm_id);
if (XE_IOCTL_DBG(xe, !vm))
err = -ENOENT;
else if (XE_IOCTL_DBG(xe, vm->preempt.num_exec_queues))
err = -EBUSY;
else
xa_erase(&xef->vm.xa, args->vm_id);
mutex_unlock(&xef->vm.lock);
if (!err)
xe_vm_close_and_put(vm);
return err;
}
static int xe_vm_query_vmas(struct xe_vm *vm, u64 start, u64 end)
{
struct drm_gpuva *gpuva;
u32 num_vmas = 0;
xe_vm_assert_write_mode_or_garbage_collector(vm);
drm_gpuvm_for_each_va_range(gpuva, &vm->gpuvm, start, end)
num_vmas++;
return num_vmas;
}
static int get_mem_attrs(struct xe_vm *vm, u32 *num_vmas, u64 start,
u64 end, struct drm_xe_mem_range_attr *attrs)
{
struct drm_gpuva *gpuva;
int i = 0;
xe_vm_assert_write_mode_or_garbage_collector(vm);
drm_gpuvm_for_each_va_range(gpuva, &vm->gpuvm, start, end) {
struct xe_vma *vma = gpuva_to_vma(gpuva);
if (i == *num_vmas)
return -ENOSPC;
attrs[i].start = xe_vma_start(vma);
attrs[i].end = xe_vma_end(vma);
attrs[i].atomic.val = vma->attr.atomic_access;
attrs[i].pat_index.val = vma->attr.pat_index;
attrs[i].preferred_mem_loc.devmem_fd = vma->attr.preferred_loc.devmem_fd;
attrs[i].preferred_mem_loc.migration_policy =
vma->attr.preferred_loc.migration_policy;
i++;
}
*num_vmas = i;
return 0;
}
int xe_vm_query_vmas_attrs_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
{
struct xe_device *xe = to_xe_device(dev);
struct xe_file *xef = to_xe_file(file);
struct drm_xe_mem_range_attr *mem_attrs;
struct drm_xe_vm_query_mem_range_attr *args = data;
u64 __user *attrs_user = u64_to_user_ptr(args->vector_of_mem_attr);
struct xe_vm *vm;
int err = 0;
if (XE_IOCTL_DBG(xe,
((args->num_mem_ranges == 0 &&
(attrs_user || args->sizeof_mem_range_attr != 0)) ||
(args->num_mem_ranges > 0 &&
(!attrs_user ||
args->sizeof_mem_range_attr !=
sizeof(struct drm_xe_mem_range_attr))))))
return -EINVAL;
vm = xe_vm_lookup(xef, args->vm_id);
if (XE_IOCTL_DBG(xe, !vm))
return -EINVAL;
err = down_write_killable(&vm->lock);
if (err)
goto put_vm;
attrs_user = u64_to_user_ptr(args->vector_of_mem_attr);
if (args->num_mem_ranges == 0 && !attrs_user) {
args->num_mem_ranges = xe_vm_query_vmas(vm, args->start, args->start + args->range);
args->sizeof_mem_range_attr = sizeof(struct drm_xe_mem_range_attr);
goto unlock_vm;
}
mem_attrs = kvmalloc_array(args->num_mem_ranges, args->sizeof_mem_range_attr,
GFP_KERNEL | __GFP_ACCOUNT |
__GFP_RETRY_MAYFAIL | __GFP_NOWARN);
if (!mem_attrs) {
err = args->num_mem_ranges > 1 ? -ENOBUFS : -ENOMEM;
goto unlock_vm;
}
memset(mem_attrs, 0, args->num_mem_ranges * args->sizeof_mem_range_attr);
err = get_mem_attrs(vm, &args->num_mem_ranges, args->start,
args->start + args->range, mem_attrs);
if (err)
goto free_mem_attrs;
err = copy_to_user(attrs_user, mem_attrs,
args->sizeof_mem_range_attr * args->num_mem_ranges);
if (err)
err = -EFAULT;
free_mem_attrs:
kvfree(mem_attrs);
unlock_vm:
up_write(&vm->lock);
put_vm:
xe_vm_put(vm);
return err;
}
struct xe_vma *xe_vm_find_vma_by_addr(struct xe_vm *vm, u64 page_addr)
{
return xe_vm_find_overlapping_vma(vm, page_addr, SZ_4K);
}
static const u32 region_to_mem_type[] = {
XE_PL_TT,
XE_PL_VRAM0,
XE_PL_VRAM1,
};
static void prep_vma_destroy(struct xe_vm *vm, struct xe_vma *vma,
bool post_commit)
{
xe_svm_notifier_lock(vm);
vma->gpuva.flags |= XE_VMA_DESTROYED;
xe_svm_notifier_unlock(vm);
if (post_commit)
xe_vm_remove_vma(vm, vma);
}
#undef ULL
#define ULL unsigned long long
#if IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM)
static void print_op(struct xe_device *xe, struct drm_gpuva_op *op)
{
struct xe_vma *vma;
switch (op->op) {
case DRM_GPUVA_OP_MAP:
vm_dbg(&xe->drm, "MAP: addr=0x%016llx, range=0x%016llx",
(ULL)op->map.va.addr, (ULL)op->map.va.range);
break;
case DRM_GPUVA_OP_REMAP:
vma = gpuva_to_vma(op->remap.unmap->va);
vm_dbg(&xe->drm, "REMAP:UNMAP: addr=0x%016llx, range=0x%016llx, keep=%d",
(ULL)xe_vma_start(vma), (ULL)xe_vma_size(vma),
op->remap.unmap->keep ? 1 : 0);
if (op->remap.prev)
vm_dbg(&xe->drm,
"REMAP:PREV: addr=0x%016llx, range=0x%016llx",
(ULL)op->remap.prev->va.addr,
(ULL)op->remap.prev->va.range);
if (op->remap.next)
vm_dbg(&xe->drm,
"REMAP:NEXT: addr=0x%016llx, range=0x%016llx",
(ULL)op->remap.next->va.addr,
(ULL)op->remap.next->va.range);
break;
case DRM_GPUVA_OP_UNMAP:
vma = gpuva_to_vma(op->unmap.va);
vm_dbg(&xe->drm, "UNMAP: addr=0x%016llx, range=0x%016llx, keep=%d",
(ULL)xe_vma_start(vma), (ULL)xe_vma_size(vma),
op->unmap.keep ? 1 : 0);
break;
case DRM_GPUVA_OP_PREFETCH:
vma = gpuva_to_vma(op->prefetch.va);
vm_dbg(&xe->drm, "PREFETCH: addr=0x%016llx, range=0x%016llx",
(ULL)xe_vma_start(vma), (ULL)xe_vma_size(vma));
break;
default:
drm_warn(&xe->drm, "NOT POSSIBLE\n");
}
}
#else
static void print_op(struct xe_device *xe, struct drm_gpuva_op *op)
{
}
#endif
static bool __xe_vm_needs_clear_scratch_pages(struct xe_vm *vm, u32 bind_flags)
{
if (!xe_vm_in_fault_mode(vm))
return false;
if (!xe_vm_has_scratch(vm))
return false;
if (bind_flags & DRM_XE_VM_BIND_FLAG_IMMEDIATE)
return false;
return true;
}
static void xe_svm_prefetch_gpuva_ops_fini(struct drm_gpuva_ops *ops)
{
struct drm_gpuva_op *__op;
drm_gpuva_for_each_op(__op, ops) {
struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
xe_vma_svm_prefetch_op_fini(op);
}
}
static struct drm_gpuva_ops *
vm_bind_ioctl_ops_create(struct xe_vm *vm, struct xe_vma_ops *vops,
struct xe_bo *bo, u64 bo_offset_or_userptr,
u64 addr, u64 range,
u32 operation, u32 flags,
u32 prefetch_region, u16 pat_index)
{
struct drm_gem_object *obj = bo ? &bo->ttm.base : NULL;
struct drm_gpuva_ops *ops;
struct drm_gpuva_op *__op;
struct drm_gpuvm_bo *vm_bo;
u64 range_start = addr;
u64 range_end = addr + range;
int err;
xe_vm_assert_write_mode_or_garbage_collector(vm);
vm_dbg(&vm->xe->drm,
"op=%d, addr=0x%016llx, range=0x%016llx, bo_offset_or_userptr=0x%016llx",
operation, (ULL)addr, (ULL)range,
(ULL)bo_offset_or_userptr);
switch (operation) {
case DRM_XE_VM_BIND_OP_MAP:
if (flags & DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR) {
xe_vm_find_cpu_addr_mirror_vma_range(vm, &range_start, &range_end);
vops->flags |= XE_VMA_OPS_FLAG_ALLOW_SVM_UNMAP;
}
fallthrough;
case DRM_XE_VM_BIND_OP_MAP_USERPTR: {
struct drm_gpuvm_map_req map_req = {
.map.va.addr = range_start,
.map.va.range = range_end - range_start,
.map.gem.obj = obj,
.map.gem.offset = bo_offset_or_userptr,
};
vops->flags |= XE_VMA_OPS_FLAG_MODIFIES_GPUVA;
ops = drm_gpuvm_sm_map_ops_create(&vm->gpuvm, &map_req);
break;
}
case DRM_XE_VM_BIND_OP_UNMAP:
vops->flags |= XE_VMA_OPS_FLAG_MODIFIES_GPUVA;
ops = drm_gpuvm_sm_unmap_ops_create(&vm->gpuvm, addr, range);
break;
case DRM_XE_VM_BIND_OP_PREFETCH:
ops = drm_gpuvm_prefetch_ops_create(&vm->gpuvm, addr, range);
break;
case DRM_XE_VM_BIND_OP_UNMAP_ALL:
xe_assert(vm->xe, bo);
vops->flags |= XE_VMA_OPS_FLAG_MODIFIES_GPUVA;
err = xe_bo_lock(bo, true);
if (err)
return ERR_PTR(err);
vm_bo = drm_gpuvm_bo_obtain_locked(&vm->gpuvm, obj);
if (IS_ERR(vm_bo)) {
xe_bo_unlock(bo);
return ERR_CAST(vm_bo);
}
ops = drm_gpuvm_bo_unmap_ops_create(vm_bo);
drm_gpuvm_bo_put(vm_bo);
xe_bo_unlock(bo);
break;
default:
drm_warn(&vm->xe->drm, "NOT POSSIBLE\n");
ops = ERR_PTR(-EINVAL);
}
if (IS_ERR(ops))
return ops;
drm_gpuva_for_each_op(__op, ops) {
struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
if (__op->op == DRM_GPUVA_OP_PREFETCH) {
xa_init_flags(&op->prefetch_range.range, XA_FLAGS_ALLOC);
op->prefetch_range.ranges_count = 0;
}
}
drm_gpuva_for_each_op(__op, ops) {
struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
if (__op->op == DRM_GPUVA_OP_MAP) {
op->map.immediate =
flags & DRM_XE_VM_BIND_FLAG_IMMEDIATE;
if (flags & DRM_XE_VM_BIND_FLAG_READONLY)
op->map.vma_flags |= XE_VMA_READ_ONLY;
if (flags & DRM_XE_VM_BIND_FLAG_NULL)
op->map.vma_flags |= DRM_GPUVA_SPARSE;
if (flags & DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR)
op->map.vma_flags |= XE_VMA_SYSTEM_ALLOCATOR;
if (flags & DRM_XE_VM_BIND_FLAG_DUMPABLE)
op->map.vma_flags |= XE_VMA_DUMPABLE;
if (flags & DRM_XE_VM_BIND_FLAG_MADVISE_AUTORESET)
op->map.vma_flags |= XE_VMA_MADV_AUTORESET;
op->map.request_decompress = flags & DRM_XE_VM_BIND_FLAG_DECOMPRESS;
op->map.pat_index = pat_index;
op->map.invalidate_on_bind =
__xe_vm_needs_clear_scratch_pages(vm, flags);
} else if (__op->op == DRM_GPUVA_OP_PREFETCH) {
struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);
struct xe_tile *tile;
struct xe_svm_range *svm_range;
struct drm_gpusvm_ctx ctx = {};
struct drm_pagemap *dpagemap = NULL;
u8 id, tile_mask = 0;
u32 i;
bool need_put, valid_pages;
if (xe_vma_is_userptr(vma))
vops->flags |= XE_VMA_OPS_FLAG_MODIFIES_GPUVA;
if (!xe_vma_is_cpu_addr_mirror(vma)) {
op->prefetch.region = prefetch_region;
continue;
}
ctx.read_only = xe_vma_read_only(vma);
ctx.devmem_possible = IS_DGFX(vm->xe) &&
IS_ENABLED(CONFIG_DRM_XE_PAGEMAP);
for_each_tile(tile, vm->xe, id) {
xe_migrate_ulls_enter(tile->migrate);
tile_mask |= 0x1 << id;
}
if (prefetch_region == DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC) {
dpagemap = xe_vma_resolve_pagemap(vma,
xe_device_get_root_tile(vm->xe));
} else if (prefetch_region) {
tile = &vm->xe->tiles[region_to_mem_type[prefetch_region] -
XE_PL_VRAM0];
dpagemap = xe_tile_local_pagemap(tile);
}
op->prefetch_range.dpagemap = dpagemap;
alloc_next_range:
need_put = false;
svm_range = xe_svm_range_find_or_insert(vm, addr, vma, &ctx);
if (PTR_ERR(svm_range) == -ENOENT) {
u64 ret = xe_svm_find_vma_start(vm, addr, range_end, vma);
addr = ret == ULONG_MAX ? 0 : ret;
if (addr)
goto alloc_next_range;
else
goto print_op_label;
}
if (IS_ERR(svm_range)) {
err = PTR_ERR(svm_range);
goto unwind_prefetch_ops;
}
if (xe_svm_range_validate(vm, svm_range, tile_mask,
dpagemap, &valid_pages)) {
xe_svm_range_debug(svm_range, "PREFETCH - RANGE IS VALID");
xe_assert(vm->xe, valid_pages);
if (dpagemap)
xe_svm_range_prefetch_lru_bump(vm, vma, svm_range,
dpagemap);
need_put = true;
goto check_next_range;
}
err = xa_alloc(&op->prefetch_range.range,
&i, svm_range, xa_limit_32b,
GFP_KERNEL);
if (err) {
xe_svm_range_put(svm_range);
goto unwind_prefetch_ops;
}
op->prefetch_range.ranges_count++;
vops->flags |= XE_VMA_OPS_FLAG_HAS_SVM_PREFETCH;
if (valid_pages)
vops->flags |= XE_VMA_OPS_FLAG_HAS_SVM_VALID_RANGE;
xe_svm_range_debug(svm_range, "PREFETCH - RANGE CREATED");
check_next_range:
if (range_end > xe_svm_range_end(svm_range) &&
xe_svm_range_end(svm_range) < xe_vma_end(vma)) {
addr = xe_svm_range_end(svm_range);
if (need_put)
xe_svm_range_put(svm_range);
goto alloc_next_range;
}
if (need_put)
xe_svm_range_put(svm_range);
}
print_op_label:
print_op(vm->xe, __op);
}
return ops;
unwind_prefetch_ops:
xe_svm_prefetch_gpuva_ops_fini(ops);
drm_gpuva_ops_free(&vm->gpuvm, ops);
return ERR_PTR(err);
}
ALLOW_ERROR_INJECTION(vm_bind_ioctl_ops_create, ERRNO);
static struct xe_vma *new_vma(struct xe_vm *vm, struct drm_gpuva_op_map *op,
struct xe_vma_mem_attr *attr, unsigned int flags)
{
struct xe_bo *bo = op->gem.obj ? gem_to_xe_bo(op->gem.obj) : NULL;
struct xe_validation_ctx ctx;
struct drm_exec exec;
struct xe_vma *vma;
int err = 0;
xe_vm_assert_write_mode_or_garbage_collector(vm);
if (bo) {
err = 0;
xe_validation_guard(&ctx, &vm->xe->val, &exec,
(struct xe_val_flags) {.interruptible = true}, err) {
if (!bo->vm) {
err = drm_exec_lock_obj(&exec, xe_vm_obj(vm));
drm_exec_retry_on_contention(&exec);
}
if (!err) {
err = drm_exec_lock_obj(&exec, &bo->ttm.base);
drm_exec_retry_on_contention(&exec);
}
if (err)
return ERR_PTR(err);
vma = xe_vma_create(vm, bo, op->gem.offset,
op->va.addr, op->va.addr +
op->va.range - 1, attr, flags);
if (IS_ERR(vma))
return vma;
if (!bo->vm) {
err = add_preempt_fences(vm, bo);
if (err) {
prep_vma_destroy(vm, vma, false);
xe_vma_destroy(vma, NULL);
}
}
}
if (err)
return ERR_PTR(err);
} else {
vma = xe_vma_create(vm, NULL, op->gem.offset,
op->va.addr, op->va.addr +
op->va.range - 1, attr, flags);
if (IS_ERR(vma))
return vma;
if (xe_vma_is_userptr(vma)) {
err = xe_vma_userptr_pin_pages(to_userptr_vma(vma));
if (err == -EBUSY)
err = -ENOMEM;
}
}
if (err) {
prep_vma_destroy(vm, vma, false);
xe_vma_destroy_unlocked(vma);
vma = ERR_PTR(err);
}
return vma;
}
static u64 xe_vma_max_pte_size(struct xe_vma *vma)
{
if (vma->gpuva.flags & XE_VMA_PTE_1G)
return SZ_1G;
else if (vma->gpuva.flags & (XE_VMA_PTE_2M | XE_VMA_PTE_COMPACT))
return SZ_2M;
else if (vma->gpuva.flags & XE_VMA_PTE_64K)
return SZ_64K;
else if (vma->gpuva.flags & XE_VMA_PTE_4K)
return SZ_4K;
return SZ_1G;
}
static void xe_vma_set_pte_size(struct xe_vma *vma, u64 size)
{
switch (size) {
case SZ_1G:
vma->gpuva.flags |= XE_VMA_PTE_1G;
break;
case SZ_2M:
vma->gpuva.flags |= XE_VMA_PTE_2M;
break;
case SZ_64K:
vma->gpuva.flags |= XE_VMA_PTE_64K;
break;
case SZ_4K:
vma->gpuva.flags |= XE_VMA_PTE_4K;
break;
}
}
static int xe_vma_op_commit(struct xe_vm *vm, struct xe_vma_op *op)
{
int err = 0;
lockdep_assert_held(&vm->lock);
switch (op->base.op) {
case DRM_GPUVA_OP_MAP:
xe_vm_assert_write_mode_or_garbage_collector(vm);
err |= xe_vm_insert_vma(vm, op->map.vma);
if (!err)
op->flags |= XE_VMA_OP_COMMITTED;
break;
case DRM_GPUVA_OP_REMAP:
{
u8 tile_present =
gpuva_to_vma(op->base.remap.unmap->va)->tile_present;
xe_vm_assert_write_mode_or_garbage_collector(vm);
prep_vma_destroy(vm, gpuva_to_vma(op->base.remap.unmap->va),
true);
op->flags |= XE_VMA_OP_COMMITTED;
if (op->remap.prev) {
err |= xe_vm_insert_vma(vm, op->remap.prev);
if (!err)
op->flags |= XE_VMA_OP_PREV_COMMITTED;
if (!err && op->remap.skip_prev) {
op->remap.prev->tile_present =
tile_present;
}
}
if (op->remap.next) {
err |= xe_vm_insert_vma(vm, op->remap.next);
if (!err)
op->flags |= XE_VMA_OP_NEXT_COMMITTED;
if (!err && op->remap.skip_next) {
op->remap.next->tile_present =
tile_present;
}
}
if (!err) {
op->base.remap.unmap->va->va.addr = op->remap.start;
op->base.remap.unmap->va->va.range = op->remap.range;
}
break;
}
case DRM_GPUVA_OP_UNMAP:
xe_vm_assert_write_mode_or_garbage_collector(vm);
prep_vma_destroy(vm, gpuva_to_vma(op->base.unmap.va), true);
op->flags |= XE_VMA_OP_COMMITTED;
break;
case DRM_GPUVA_OP_PREFETCH:
op->flags |= XE_VMA_OP_COMMITTED;
break;
default:
drm_warn(&vm->xe->drm, "NOT POSSIBLE\n");
}
return err;
}
bool xe_vma_has_default_mem_attrs(struct xe_vma *vma)
{
return (vma->attr.atomic_access == DRM_XE_ATOMIC_UNDEFINED &&
vma->attr.pat_index == vma->attr.default_pat_index &&
vma->attr.preferred_loc.devmem_fd == DRM_XE_PREFERRED_LOC_DEFAULT_DEVICE &&
vma->attr.preferred_loc.migration_policy == DRM_XE_MIGRATE_ALL_PAGES);
}
static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct drm_gpuva_ops *ops,
struct xe_vma_ops *vops)
{
struct xe_device *xe = vm->xe;
struct drm_gpuva_op *__op;
struct xe_tile *tile;
u8 id, tile_mask = 0;
int err = 0;
xe_vm_assert_write_mode_or_garbage_collector(vm);
for_each_tile(tile, vm->xe, id)
tile_mask |= 0x1 << id;
drm_gpuva_for_each_op(__op, ops) {
struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
struct xe_vma *vma;
unsigned int flags = 0;
INIT_LIST_HEAD(&op->link);
list_add_tail(&op->link, &vops->list);
op->tile_mask = tile_mask;
switch (op->base.op) {
case DRM_GPUVA_OP_MAP:
{
struct xe_vma_mem_attr default_attr = {
.preferred_loc = {
.devmem_fd = DRM_XE_PREFERRED_LOC_DEFAULT_DEVICE,
.migration_policy = DRM_XE_MIGRATE_ALL_PAGES,
},
.atomic_access = DRM_XE_ATOMIC_UNDEFINED,
.default_pat_index = op->map.pat_index,
.pat_index = op->map.pat_index,
.purgeable_state = XE_MADV_PURGEABLE_WILLNEED,
};
flags |= op->map.vma_flags & XE_VMA_CREATE_MASK;
vma = new_vma(vm, &op->base.map, &default_attr,
flags);
if (IS_ERR(vma))
return PTR_ERR(vma);
op->map.vma = vma;
if (((op->map.immediate || !xe_vm_in_fault_mode(vm)) &&
!(op->map.vma_flags & XE_VMA_SYSTEM_ALLOCATOR)) ||
op->map.invalidate_on_bind)
xe_vma_ops_incr_pt_update_ops(vops,
op->tile_mask, 1);
break;
}
case DRM_GPUVA_OP_REMAP:
{
struct xe_vma *old =
gpuva_to_vma(op->base.remap.unmap->va);
bool skip = xe_vma_is_cpu_addr_mirror(old);
u64 start = xe_vma_start(old), end = xe_vma_end(old);
int num_remap_ops = 0;
if (op->base.remap.prev)
start = op->base.remap.prev->va.addr +
op->base.remap.prev->va.range;
if (op->base.remap.next)
end = op->base.remap.next->va.addr;
if (xe_vma_is_cpu_addr_mirror(old) &&
xe_svm_has_mapping(vm, start, end)) {
if (vops->flags & XE_VMA_OPS_FLAG_MADVISE)
xe_svm_unmap_address_range(vm, start, end);
else
return -EBUSY;
}
op->remap.start = xe_vma_start(old);
op->remap.range = xe_vma_size(old);
op->remap.old_start = op->remap.start;
op->remap.old_range = op->remap.range;
flags |= op->base.remap.unmap->va->flags & XE_VMA_CREATE_MASK;
if (op->base.remap.prev) {
vma = new_vma(vm, op->base.remap.prev,
&old->attr, flags);
if (IS_ERR(vma))
return PTR_ERR(vma);
op->remap.prev = vma;
op->remap.skip_prev = skip ||
(!xe_vma_is_userptr(old) &&
IS_ALIGNED(xe_vma_end(vma),
xe_vma_max_pte_size(old)));
if (op->remap.skip_prev) {
xe_vma_set_pte_size(vma, xe_vma_max_pte_size(old));
op->remap.range -=
xe_vma_end(vma) -
xe_vma_start(old);
op->remap.start = xe_vma_end(vma);
vm_dbg(&xe->drm, "REMAP:SKIP_PREV: addr=0x%016llx, range=0x%016llx",
(ULL)op->remap.start,
(ULL)op->remap.range);
} else {
num_remap_ops++;
}
}
if (op->base.remap.next) {
vma = new_vma(vm, op->base.remap.next,
&old->attr, flags);
if (IS_ERR(vma))
return PTR_ERR(vma);
op->remap.next = vma;
op->remap.skip_next = skip ||
(!xe_vma_is_userptr(old) &&
IS_ALIGNED(xe_vma_start(vma),
xe_vma_max_pte_size(old)));
if (op->remap.skip_next) {
xe_vma_set_pte_size(vma, xe_vma_max_pte_size(old));
op->remap.range -=
xe_vma_end(old) -
xe_vma_start(vma);
vm_dbg(&xe->drm, "REMAP:SKIP_NEXT: addr=0x%016llx, range=0x%016llx",
(ULL)op->remap.start,
(ULL)op->remap.range);
} else {
num_remap_ops++;
}
}
if (!skip)
num_remap_ops++;
xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask, num_remap_ops);
break;
}
case DRM_GPUVA_OP_UNMAP:
vma = gpuva_to_vma(op->base.unmap.va);
if (xe_vma_is_cpu_addr_mirror(vma) &&
xe_svm_has_mapping(vm, xe_vma_start(vma),
xe_vma_end(vma)) &&
!(vops->flags & XE_VMA_OPS_FLAG_ALLOW_SVM_UNMAP))
return -EBUSY;
if (!xe_vma_is_cpu_addr_mirror(vma))
xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask, 1);
break;
case DRM_GPUVA_OP_PREFETCH:
vma = gpuva_to_vma(op->base.prefetch.va);
if (xe_vma_is_userptr(vma)) {
err = xe_vma_userptr_pin_pages(to_userptr_vma(vma));
if (err)
return err;
}
if (xe_vma_is_cpu_addr_mirror(vma))
xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask,
op->prefetch_range.ranges_count);
else
xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask, 1);
break;
default:
drm_warn(&vm->xe->drm, "NOT POSSIBLE\n");
}
err = xe_vma_op_commit(vm, op);
if (err)
return err;
}
return 0;
}
static void xe_vma_op_unwind(struct xe_vm *vm, struct xe_vma_op *op,
bool post_commit, bool prev_post_commit,
bool next_post_commit)
{
lockdep_assert_held(&vm->lock);
switch (op->base.op) {
case DRM_GPUVA_OP_MAP:
xe_vm_assert_write_mode_or_garbage_collector(vm);
if (op->map.vma) {
prep_vma_destroy(vm, op->map.vma, post_commit);
xe_vma_destroy_unlocked(op->map.vma);
}
break;
case DRM_GPUVA_OP_UNMAP:
{
struct xe_vma *vma = gpuva_to_vma(op->base.unmap.va);
xe_vm_assert_write_mode_or_garbage_collector(vm);
if (vma) {
xe_svm_notifier_lock(vm);
vma->gpuva.flags &= ~XE_VMA_DESTROYED;
xe_svm_notifier_unlock(vm);
if (post_commit)
xe_vm_insert_vma(vm, vma);
}
break;
}
case DRM_GPUVA_OP_REMAP:
{
struct xe_vma *vma = gpuva_to_vma(op->base.remap.unmap->va);
xe_vm_assert_write_mode_or_garbage_collector(vm);
if (op->remap.prev) {
prep_vma_destroy(vm, op->remap.prev, prev_post_commit);
xe_vma_destroy_unlocked(op->remap.prev);
}
if (op->remap.next) {
prep_vma_destroy(vm, op->remap.next, next_post_commit);
xe_vma_destroy_unlocked(op->remap.next);
}
if (vma) {
xe_svm_notifier_lock(vm);
vma->gpuva.flags &= ~XE_VMA_DESTROYED;
xe_svm_notifier_unlock(vm);
if (post_commit) {
op->base.remap.unmap->va->va.addr =
op->remap.old_start;
op->base.remap.unmap->va->va.range =
op->remap.old_range;
xe_vm_insert_vma(vm, vma);
}
}
break;
}
case DRM_GPUVA_OP_PREFETCH:
break;
default:
drm_warn(&vm->xe->drm, "NOT POSSIBLE\n");
}
}
static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm,
struct drm_gpuva_ops **ops,
int num_ops_list)
{
int i;
for (i = num_ops_list - 1; i >= 0; --i) {
struct drm_gpuva_ops *__ops = ops[i];
struct drm_gpuva_op *__op;
if (!__ops)
continue;
drm_gpuva_for_each_op_reverse(__op, __ops) {
struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
xe_vma_op_unwind(vm, op,
op->flags & XE_VMA_OP_COMMITTED,
op->flags & XE_VMA_OP_PREV_COMMITTED,
op->flags & XE_VMA_OP_NEXT_COMMITTED);
}
}
}
struct xe_vma_lock_and_validate_flags {
u32 res_evict : 1;
u32 validate : 1;
u32 request_decompress : 1;
u32 check_purged : 1;
};
static int vma_lock_and_validate(struct drm_exec *exec, struct xe_vma *vma,
struct xe_vma_lock_and_validate_flags flags)
{
struct xe_bo *bo = xe_vma_bo(vma);
struct xe_vm *vm = xe_vma_vm(vma);
bool validate_bo = flags.validate;
int err = 0;
if (bo) {
if (!bo->vm)
err = drm_exec_lock_obj(exec, &bo->ttm.base);
if (!err && flags.check_purged) {
if (xe_bo_madv_is_dontneed(bo))
err = -EBUSY;
else if (xe_bo_is_purged(bo))
err = -EINVAL;
}
if (vma->attr.purgeable_state != XE_MADV_PURGEABLE_WILLNEED)
validate_bo = false;
if (!err && validate_bo)
err = xe_bo_validate(bo, vm,
xe_vm_allow_vm_eviction(vm) &&
flags.res_evict, exec);
if (err)
return err;
if (flags.request_decompress)
err = xe_bo_decompress(bo);
}
return err;
}
static int check_ufence(struct xe_vma *vma)
{
if (vma->ufence) {
struct xe_user_fence * const f = vma->ufence;
if (!xe_sync_ufence_get_status(f))
return -EBUSY;
vma->ufence = NULL;
xe_sync_ufence_put(f);
}
return 0;
}
struct prefetch_thread {
struct work_struct work;
struct drm_gpusvm_ctx *ctx;
struct xe_vma *vma;
struct xe_svm_range *svm_range;
struct drm_pagemap *dpagemap;
int err;
};
static void prefetch_thread_func(struct prefetch_thread *thread)
{
struct xe_vma *vma = thread->vma;
struct xe_vm *vm = xe_vma_vm(vma);
struct xe_svm_range *svm_range = thread->svm_range;
struct drm_pagemap *dpagemap = thread->dpagemap;
int err = 0;
guard(mutex)(&svm_range->lock);
if (xe_svm_range_is_removed(svm_range))
return;
if (!dpagemap)
xe_svm_range_migrate_to_smem(vm, svm_range);
if (IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM)) {
drm_dbg(&vm->xe->drm,
"Prefetch pagemap is %s start 0x%016lx end 0x%016lx\n",
dpagemap ? dpagemap->drm->unique : "system",
xe_svm_range_start(svm_range), xe_svm_range_end(svm_range));
}
if (xe_svm_range_needs_migrate_to_vram(svm_range, vma, dpagemap)) {
err = xe_svm_alloc_vram(svm_range, thread->ctx, dpagemap);
if (err) {
drm_dbg(&vm->xe->drm, "VRAM allocation failed, retry from userspace, asid=%u, gpusvm=%p, errno=%pe\n",
vm->usm.asid, &vm->svm.gpusvm, ERR_PTR(err));
thread->err = -ENODATA;
return;
}
xe_svm_range_debug(svm_range, "PREFETCH - RANGE MIGRATED TO VRAM");
}
err = xe_svm_range_get_pages(vm, svm_range, thread->ctx);
if (err) {
drm_dbg(&vm->xe->drm, "Get pages failed, asid=%u, gpusvm=%p, errno=%pe\n",
vm->usm.asid, &vm->svm.gpusvm, ERR_PTR(err));
if (err == -EOPNOTSUPP || err == -EFAULT || err == -EPERM)
err = -ENODATA;
thread->err = err;
return;
}
xe_svm_range_debug(svm_range, "PREFETCH - RANGE GET PAGES DONE");
}
static void prefetch_work_func(struct work_struct *w)
{
struct prefetch_thread *thread =
container_of(w, struct prefetch_thread, work);
prefetch_thread_func(thread);
}
static int prefetch_ranges(struct xe_vm *vm, struct xe_vma_ops *vops,
struct xe_vma_op *op)
{
bool devmem_possible = IS_DGFX(vm->xe) && IS_ENABLED(CONFIG_DRM_XE_PAGEMAP);
struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);
struct drm_pagemap *dpagemap = op->prefetch_range.dpagemap;
struct xe_svm_range *svm_range;
struct drm_gpusvm_ctx ctx = {};
struct prefetch_thread stack_thread, *thread, *prefetches;
unsigned long i;
int err = 0, idx = 0;
bool skip_threads;
if (!xe_vma_is_cpu_addr_mirror(vma))
return 0;
ctx.read_only = xe_vma_read_only(vma);
ctx.devmem_possible = devmem_possible;
ctx.check_pages_threshold = devmem_possible ? SZ_64K : 0;
ctx.device_private_page_owner = xe_svm_private_page_owner(vm, !dpagemap);
ctx.devmem_fn = xe_svm_devmem_lru_bump;
skip_threads = op->prefetch_range.ranges_count == 1 ||
(!dpagemap && !(vops->flags &
XE_VMA_OPS_FLAG_HAS_SVM_VALID_RANGE)) ||
!(vops->flags & XE_VMA_OPS_FLAG_DOWNGRADE_LOCK) ||
vm->xe->info.num_pf_work == 1;
thread = skip_threads ? &stack_thread : NULL;
if (!skip_threads) {
prefetches = kvmalloc_objs(*prefetches,
op->prefetch_range.ranges_count);
if (!prefetches)
return -ENOMEM;
}
xa_for_each(&op->prefetch_range.range, i, svm_range) {
if (!skip_threads) {
thread = prefetches + idx++;
INIT_WORK(&thread->work, prefetch_work_func);
}
thread->ctx = &ctx;
thread->vma = vma;
thread->svm_range = svm_range;
thread->dpagemap = dpagemap;
thread->err = 0;
if (skip_threads) {
prefetch_thread_func(thread);
if (thread->err)
return thread->err;
} else {
queue_work(vm->xe->usm.prefetch_wq, &thread->work);
}
}
if (!skip_threads) {
for (i = 0; i < idx; ++i) {
thread = prefetches + i;
flush_work(&thread->work);
if (thread->err && !err)
err = thread->err;
}
kvfree(prefetches);
}
return err;
}
static int op_lock_and_prep(struct drm_exec *exec, struct xe_vm *vm,
struct xe_vma_ops *vops, struct xe_vma_op *op)
{
int err = 0;
bool res_evict;
res_evict = !(vops->flags & XE_VMA_OPS_ARRAY_OF_BINDS);
switch (op->base.op) {
case DRM_GPUVA_OP_MAP:
if (!op->map.invalidate_on_bind)
err = vma_lock_and_validate(exec, op->map.vma,
(struct xe_vma_lock_and_validate_flags) {
.res_evict = res_evict,
.validate = !xe_vm_in_fault_mode(vm) ||
op->map.immediate,
.request_decompress =
op->map.request_decompress,
.check_purged = false,
});
break;
case DRM_GPUVA_OP_REMAP:
err = check_ufence(gpuva_to_vma(op->base.remap.unmap->va));
if (err)
break;
err = vma_lock_and_validate(exec,
gpuva_to_vma(op->base.remap.unmap->va),
(struct xe_vma_lock_and_validate_flags) {
.res_evict = res_evict,
.validate = false,
.request_decompress = false,
.check_purged = false,
});
if (!err && op->remap.prev)
err = vma_lock_and_validate(exec, op->remap.prev,
(struct xe_vma_lock_and_validate_flags) {
.res_evict = res_evict,
.validate = true,
.request_decompress = false,
.check_purged = false,
});
if (!err && op->remap.next)
err = vma_lock_and_validate(exec, op->remap.next,
(struct xe_vma_lock_and_validate_flags) {
.res_evict = res_evict,
.validate = true,
.request_decompress = false,
.check_purged = false,
});
break;
case DRM_GPUVA_OP_UNMAP:
err = check_ufence(gpuva_to_vma(op->base.unmap.va));
if (err)
break;
err = vma_lock_and_validate(exec,
gpuva_to_vma(op->base.unmap.va),
(struct xe_vma_lock_and_validate_flags) {
.res_evict = res_evict,
.validate = false,
.request_decompress = false,
.check_purged = false,
});
break;
case DRM_GPUVA_OP_PREFETCH:
{
struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);
u32 region;
if (!xe_vma_is_cpu_addr_mirror(vma)) {
region = op->prefetch.region;
xe_assert(vm->xe, region == DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC ||
region <= ARRAY_SIZE(region_to_mem_type));
}
err = vma_lock_and_validate(exec,
gpuva_to_vma(op->base.prefetch.va),
(struct xe_vma_lock_and_validate_flags) {
.res_evict = res_evict,
.validate = false,
.request_decompress = false,
.check_purged = true,
});
if (!err && !xe_vma_has_no_bo(vma)) {
struct xe_bo *bo = xe_vma_bo(vma);
u32 mem_type;
if (region == DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC) {
unsigned int i;
mem_type = XE_PL_TT;
for (i = 0; i < bo->placement.num_placement; i++) {
if (mem_type_is_vram(bo->placements[i].mem_type)) {
mem_type = bo->placements[i].mem_type;
break;
}
}
} else {
mem_type = region_to_mem_type[region];
}
err = xe_bo_migrate(bo, mem_type, NULL, exec);
}
break;
}
default:
drm_warn(&vm->xe->drm, "NOT POSSIBLE\n");
}
return err;
}
static int vm_bind_ioctl_ops_prefetch_ranges(struct xe_vm *vm,
struct xe_vma_ops *vops)
{
struct xe_vma_op *op;
int err;
if (!(vops->flags & XE_VMA_OPS_FLAG_HAS_SVM_PREFETCH))
return 0;
list_for_each_entry(op, &vops->list, link) {
if (op->base.op == DRM_GPUVA_OP_PREFETCH) {
err = prefetch_ranges(vm, vops, op);
if (err)
return err;
}
}
return 0;
}
static int vm_bind_ioctl_ops_lock_and_prep(struct drm_exec *exec,
struct xe_vm *vm,
struct xe_vma_ops *vops)
{
struct xe_vma_op *op;
int err;
err = drm_exec_lock_obj(exec, xe_vm_obj(vm));
if (err)
return err;
list_for_each_entry(op, &vops->list, link) {
err = op_lock_and_prep(exec, vm, vops, op);
if (err)
return err;
}
#ifdef TEST_VM_OPS_ERROR
if (vops->inject_error &&
vm->xe->vm_inject_error_position == FORCE_OP_ERROR_LOCK)
return -ENOSPC;
#endif
return 0;
}
static void op_trace(struct xe_vma_op *op)
{
switch (op->base.op) {
case DRM_GPUVA_OP_MAP:
trace_xe_vma_bind(op->map.vma);
break;
case DRM_GPUVA_OP_REMAP:
trace_xe_vma_unbind(gpuva_to_vma(op->base.remap.unmap->va));
if (op->remap.prev)
trace_xe_vma_bind(op->remap.prev);
if (op->remap.next)
trace_xe_vma_bind(op->remap.next);
break;
case DRM_GPUVA_OP_UNMAP:
trace_xe_vma_unbind(gpuva_to_vma(op->base.unmap.va));
break;
case DRM_GPUVA_OP_PREFETCH:
trace_xe_vma_bind(gpuva_to_vma(op->base.prefetch.va));
break;
case DRM_GPUVA_OP_DRIVER:
break;
default:
XE_WARN_ON("NOT POSSIBLE");
}
}
static void trace_xe_vm_ops_execute(struct xe_vma_ops *vops)
{
struct xe_vma_op *op;
list_for_each_entry(op, &vops->list, link)
op_trace(op);
}
static int vm_ops_setup(struct xe_vm *vm, struct xe_vma_ops *vops)
{
struct xe_tile *tile;
int number_tiles = 0;
u8 id;
for_each_tile(tile, vm->xe, id)
if (vops->pt_update_ops[id].num_ops)
++number_tiles;
if (!vops->q)
vops->q = vm->q;
return number_tiles;
}
static struct dma_fence *ops_execute(struct xe_vm *vm,
struct xe_vma_ops *vops)
{
struct xe_device *xe = vm->xe;
struct dma_fence *fence = NULL;
struct dma_fence **fences = NULL;
struct dma_fence_array *cf = NULL;
int current_fence = 0, n_fence = 1, err, i;
if (!vm_ops_setup(vm, vops))
return ERR_PTR(-ENODATA);
if (!(vops->flags & XE_VMA_OPS_FLAG_SKIP_TLB_WAIT)) {
for_each_tlb_inval(vops->q, i)
++n_fence;
}
fences = kzalloc_objs(*fences, n_fence);
if (!fences) {
fence = ERR_PTR(-ENOMEM);
goto err_trace;
}
cf = dma_fence_array_alloc(n_fence);
if (!cf) {
fence = ERR_PTR(-ENOMEM);
goto err_out;
}
err = xe_pt_update_ops_prepare(xe, vops);
if (err) {
fence = ERR_PTR(err);
goto err_out;
}
trace_xe_vm_ops_execute(vops);
fence = xe_pt_update_ops_run(xe, vops);
if (IS_ERR(fence))
goto err_out;
fences[current_fence++] = fence;
if (!(vops->flags & XE_VMA_OPS_FLAG_SKIP_TLB_WAIT)) {
xe_cpu_bind_job_lock(xe->cpu_bind, vops->q);
for_each_tlb_inval(vops->q, i)
fences[current_fence++] =
xe_exec_queue_tlb_inval_last_fence_get(vops->q,
vm, i);
xe_cpu_bind_job_unlock(xe->cpu_bind, vops->q);
}
xe_assert(xe, current_fence == n_fence);
dma_fence_array_init(cf, n_fence, fences, dma_fence_context_alloc(1),
1);
fence = &cf->base;
xe_pt_update_ops_fini(xe, vops);
return fence;
err_out:
xe_pt_update_ops_abort(xe, vops);
kfree(fences);
kfree(cf);
err_trace:
trace_xe_vm_ops_fail(vm);
return fence;
}
static void vma_add_ufence(struct xe_vma *vma, struct xe_user_fence *ufence)
{
if (vma->ufence)
xe_sync_ufence_put(vma->ufence);
vma->ufence = __xe_sync_ufence_get(ufence);
}
static void op_add_ufence(struct xe_vm *vm, struct xe_vma_op *op,
struct xe_user_fence *ufence)
{
switch (op->base.op) {
case DRM_GPUVA_OP_MAP:
if (!xe_vma_is_cpu_addr_mirror(op->map.vma))
vma_add_ufence(op->map.vma, ufence);
break;
case DRM_GPUVA_OP_REMAP:
if (op->remap.prev)
vma_add_ufence(op->remap.prev, ufence);
if (op->remap.next)
vma_add_ufence(op->remap.next, ufence);
break;
case DRM_GPUVA_OP_UNMAP:
break;
case DRM_GPUVA_OP_PREFETCH:
vma_add_ufence(gpuva_to_vma(op->base.prefetch.va), ufence);
break;
default:
drm_warn(&vm->xe->drm, "NOT POSSIBLE\n");
}
}
static void vm_bind_ioctl_ops_fini(struct xe_vm *vm, struct xe_vma_ops *vops,
struct dma_fence *fence)
{
struct xe_user_fence *ufence;
struct xe_vma_op *op;
int i;
ufence = find_ufence_get(vops->syncs, vops->num_syncs);
list_for_each_entry(op, &vops->list, link) {
if (ufence)
op_add_ufence(vm, op, ufence);
if (op->base.op == DRM_GPUVA_OP_UNMAP)
xe_vma_destroy(gpuva_to_vma(op->base.unmap.va), fence);
else if (op->base.op == DRM_GPUVA_OP_REMAP)
xe_vma_destroy(gpuva_to_vma(op->base.remap.unmap->va),
fence);
}
if (ufence)
xe_sync_ufence_put(ufence);
if (fence) {
for (i = 0; i < vops->num_syncs; i++)
xe_sync_entry_signal(vops->syncs + i, fence);
}
}
static struct dma_fence *vm_bind_ioctl_ops_execute(struct xe_vm *vm,
struct xe_vma_ops *vops)
{
struct xe_validation_ctx ctx;
struct drm_exec exec;
struct dma_fence *fence;
int err = 0;
lockdep_assert_held(&vm->lock);
xe_validation_guard(&ctx, &vm->xe->val, &exec,
((struct xe_val_flags) {
.interruptible = true,
.exec_ignore_duplicates = true,
}), err) {
err = vm_bind_ioctl_ops_lock_and_prep(&exec, vm, vops);
drm_exec_retry_on_contention(&exec);
xe_validation_retry_on_oom(&ctx, &err);
if (err)
return ERR_PTR(err);
xe_vm_set_validation_exec(vm, &exec);
fence = ops_execute(vm, vops);
xe_vm_set_validation_exec(vm, NULL);
if (IS_ERR(fence)) {
if (PTR_ERR(fence) == -ENODATA)
vm_bind_ioctl_ops_fini(vm, vops, NULL);
return fence;
}
vm_bind_ioctl_ops_fini(vm, vops, fence);
}
return err ? ERR_PTR(err) : fence;
}
ALLOW_ERROR_INJECTION(vm_bind_ioctl_ops_execute, ERRNO);
#define SUPPORTED_FLAGS_STUB \
(DRM_XE_VM_BIND_FLAG_READONLY | \
DRM_XE_VM_BIND_FLAG_IMMEDIATE | \
DRM_XE_VM_BIND_FLAG_NULL | \
DRM_XE_VM_BIND_FLAG_DUMPABLE | \
DRM_XE_VM_BIND_FLAG_CHECK_PXP | \
DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR | \
DRM_XE_VM_BIND_FLAG_MADVISE_AUTORESET | \
DRM_XE_VM_BIND_FLAG_DECOMPRESS)
#ifdef TEST_VM_OPS_ERROR
#define SUPPORTED_FLAGS (SUPPORTED_FLAGS_STUB | FORCE_OP_ERROR)
#else
#define SUPPORTED_FLAGS SUPPORTED_FLAGS_STUB
#endif
#define XE_64K_PAGE_MASK 0xffffull
#define ALL_DRM_XE_SYNCS_FLAGS (DRM_XE_SYNCS_FLAG_WAIT_FOR_OP)
static int vm_bind_ioctl_check_args(struct xe_device *xe, struct xe_vm *vm,
struct drm_xe_vm_bind *args,
struct drm_xe_vm_bind_op **bind_ops)
{
int err;
int i;
if (XE_IOCTL_DBG(xe, args->pad || args->pad2) ||
XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
return -EINVAL;
if (XE_IOCTL_DBG(xe, args->extensions))
return -EINVAL;
if (XE_IOCTL_DBG(xe, args->num_syncs > DRM_XE_MAX_SYNCS))
return -EINVAL;
if (args->num_binds > 1) {
u64 __user *bind_user =
u64_to_user_ptr(args->vector_of_binds);
*bind_ops = kvmalloc_objs(struct drm_xe_vm_bind_op,
args->num_binds,
GFP_KERNEL | __GFP_ACCOUNT | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
if (!*bind_ops)
return args->num_binds > 1 ? -ENOBUFS : -ENOMEM;
err = copy_from_user(*bind_ops, bind_user,
sizeof(struct drm_xe_vm_bind_op) *
args->num_binds);
if (XE_IOCTL_DBG(xe, err)) {
err = -EFAULT;
goto free_bind_ops;
}
} else {
*bind_ops = &args->bind;
}
for (i = 0; i < args->num_binds; ++i) {
u64 range = (*bind_ops)[i].range;
u64 addr = (*bind_ops)[i].addr;
u32 op = (*bind_ops)[i].op;
u32 flags = (*bind_ops)[i].flags;
u32 obj = (*bind_ops)[i].obj;
u64 obj_offset = (*bind_ops)[i].obj_offset;
u32 prefetch_region = (*bind_ops)[i].prefetch_mem_region_instance;
bool is_null = flags & DRM_XE_VM_BIND_FLAG_NULL;
bool is_cpu_addr_mirror = flags &
DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR;
bool is_decompress = flags & DRM_XE_VM_BIND_FLAG_DECOMPRESS;
u16 pat_index = (*bind_ops)[i].pat_index;
u16 coh_mode;
bool comp_en;
if (XE_IOCTL_DBG(xe, is_cpu_addr_mirror &&
(!xe_vm_in_fault_mode(vm) ||
!IS_ENABLED(CONFIG_DRM_XE_GPUSVM)))) {
err = -EINVAL;
goto free_bind_ops;
}
if (XE_IOCTL_DBG(xe, pat_index >= xe->pat.n_entries)) {
err = -EINVAL;
goto free_bind_ops;
}
pat_index = array_index_nospec(pat_index, xe->pat.n_entries);
(*bind_ops)[i].pat_index = pat_index;
coh_mode = xe_pat_index_get_coh_mode(xe, pat_index);
comp_en = xe_pat_index_get_comp_en(xe, pat_index);
if (XE_IOCTL_DBG(xe, !coh_mode)) {
err = -EINVAL;
goto free_bind_ops;
}
if (XE_WARN_ON(coh_mode > XE_COH_2WAY)) {
err = -EINVAL;
goto free_bind_ops;
}
if (XE_IOCTL_DBG(xe, op > DRM_XE_VM_BIND_OP_PREFETCH) ||
XE_IOCTL_DBG(xe, flags & ~SUPPORTED_FLAGS) ||
XE_IOCTL_DBG(xe, obj && (is_null || is_cpu_addr_mirror)) ||
XE_IOCTL_DBG(xe, obj_offset && (is_null ||
is_cpu_addr_mirror)) ||
XE_IOCTL_DBG(xe, op != DRM_XE_VM_BIND_OP_MAP &&
(is_decompress || is_null || is_cpu_addr_mirror)) ||
XE_IOCTL_DBG(xe, is_decompress &&
xe_pat_index_get_comp_en(xe, pat_index)) ||
XE_IOCTL_DBG(xe, !obj &&
op == DRM_XE_VM_BIND_OP_MAP &&
!is_null && !is_cpu_addr_mirror) ||
XE_IOCTL_DBG(xe, !obj &&
op == DRM_XE_VM_BIND_OP_UNMAP_ALL) ||
XE_IOCTL_DBG(xe, addr &&
op == DRM_XE_VM_BIND_OP_UNMAP_ALL) ||
XE_IOCTL_DBG(xe, range &&
op == DRM_XE_VM_BIND_OP_UNMAP_ALL) ||
XE_IOCTL_DBG(xe, obj &&
op == DRM_XE_VM_BIND_OP_MAP_USERPTR) ||
XE_IOCTL_DBG(xe, coh_mode == XE_COH_NONE &&
op == DRM_XE_VM_BIND_OP_MAP_USERPTR) ||
XE_IOCTL_DBG(xe, !IS_DGFX(xe) && coh_mode == XE_COH_NONE &&
is_cpu_addr_mirror) ||
XE_IOCTL_DBG(xe, xe_device_is_l2_flush_optimized(xe) &&
(op == DRM_XE_VM_BIND_OP_MAP_USERPTR ||
is_cpu_addr_mirror) &&
(pat_index != 19 && coh_mode != XE_COH_2WAY)) ||
XE_IOCTL_DBG(xe, comp_en &&
op == DRM_XE_VM_BIND_OP_MAP_USERPTR) ||
XE_IOCTL_DBG(xe, op == DRM_XE_VM_BIND_OP_MAP_USERPTR &&
!IS_ENABLED(CONFIG_DRM_GPUSVM)) ||
XE_IOCTL_DBG(xe, obj &&
op == DRM_XE_VM_BIND_OP_PREFETCH) ||
XE_IOCTL_DBG(xe, prefetch_region &&
op != DRM_XE_VM_BIND_OP_PREFETCH) ||
XE_IOCTL_DBG(xe, (prefetch_region != DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC &&
(prefetch_region >= (sizeof(xe->info.mem_region_mask) * 8) ||
!(BIT(prefetch_region) & xe->info.mem_region_mask)))) ||
XE_IOCTL_DBG(xe, obj &&
op == DRM_XE_VM_BIND_OP_UNMAP) ||
XE_IOCTL_DBG(xe, (flags & DRM_XE_VM_BIND_FLAG_MADVISE_AUTORESET) &&
(!is_cpu_addr_mirror || op != DRM_XE_VM_BIND_OP_MAP))) {
err = -EINVAL;
goto free_bind_ops;
}
if (XE_IOCTL_DBG(xe, obj_offset & ~PAGE_MASK) ||
XE_IOCTL_DBG(xe, addr & ~PAGE_MASK) ||
XE_IOCTL_DBG(xe, range & ~PAGE_MASK) ||
XE_IOCTL_DBG(xe, !range &&
op != DRM_XE_VM_BIND_OP_UNMAP_ALL)) {
err = -EINVAL;
goto free_bind_ops;
}
if (is_decompress && (XE_IOCTL_DBG(xe, !xe_device_has_flat_ccs(xe)) ||
XE_IOCTL_DBG(xe, GRAPHICS_VER(xe) < 20) ||
XE_IOCTL_DBG(xe, !IS_DGFX(xe)))) {
err = -EOPNOTSUPP;
goto free_bind_ops;
}
}
return 0;
free_bind_ops:
if (args->num_binds > 1)
kvfree(*bind_ops);
*bind_ops = NULL;
return err;
}
static int vm_bind_ioctl_signal_fences(struct xe_vm *vm,
struct xe_exec_queue *q,
struct xe_sync_entry *syncs,
int num_syncs)
{
struct dma_fence *fence = NULL;
int i, err = 0;
if (num_syncs) {
fence = xe_sync_in_fence_get(syncs, num_syncs,
to_wait_exec_queue(vm, q), vm);
if (IS_ERR(fence))
return PTR_ERR(fence);
for (i = 0; i < num_syncs; i++)
xe_sync_entry_signal(&syncs[i], fence);
}
dma_fence_put(fence);
return err;
}
static void xe_vma_ops_init(struct xe_vma_ops *vops, struct xe_vm *vm,
struct xe_exec_queue *q,
struct xe_sync_entry *syncs, u32 num_syncs)
{
memset(vops, 0, sizeof(*vops));
INIT_LIST_HEAD(&vops->list);
vops->vm = vm;
vops->q = q;
vops->syncs = syncs;
vops->num_syncs = num_syncs;
vops->flags = 0;
vops->start = ~0x0ull;
vops->last = 0x0ull;
}
static int xe_vm_bind_ioctl_validate_bo(struct xe_device *xe, struct xe_bo *bo,
u64 addr, u64 range, u64 obj_offset,
u16 pat_index, u32 op, u32 bind_flags)
{
u16 coh_mode;
bool comp_en;
if (XE_IOCTL_DBG(xe, (bo->flags & XE_BO_FLAG_NO_COMPRESSION) &&
xe_pat_index_get_comp_en(xe, pat_index)))
return -EINVAL;
if (XE_IOCTL_DBG(xe, range > xe_bo_size(bo)) ||
XE_IOCTL_DBG(xe, obj_offset >
xe_bo_size(bo) - range)) {
return -EINVAL;
}
if ((bo->flags & XE_BO_FLAG_INTERNAL_64K) &&
(xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K)) {
if (XE_IOCTL_DBG(xe, obj_offset &
XE_64K_PAGE_MASK) ||
XE_IOCTL_DBG(xe, addr & XE_64K_PAGE_MASK) ||
XE_IOCTL_DBG(xe, range & XE_64K_PAGE_MASK)) {
return -EINVAL;
}
}
coh_mode = xe_pat_index_get_coh_mode(xe, pat_index);
if (bo->cpu_caching) {
if (XE_IOCTL_DBG(xe, coh_mode == XE_COH_NONE &&
bo->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB)) {
return -EINVAL;
}
} else if (XE_IOCTL_DBG(xe, coh_mode == XE_COH_NONE)) {
return -EINVAL;
}
comp_en = xe_pat_index_get_comp_en(xe, pat_index);
if (XE_IOCTL_DBG(xe, bo->ttm.base.import_attach && comp_en))
return -EINVAL;
if (XE_IOCTL_DBG(xe, bo->ttm.base.import_attach && xe_device_is_l2_flush_optimized(xe) &&
(pat_index != 19 && coh_mode != XE_COH_2WAY)))
return -EINVAL;
if ((bind_flags & DRM_XE_VM_BIND_FLAG_CHECK_PXP) && xe_bo_is_protected(bo) &&
op != DRM_XE_VM_BIND_OP_UNMAP && op != DRM_XE_VM_BIND_OP_UNMAP_ALL)
if (XE_IOCTL_DBG(xe, xe_pxp_bo_key_check(xe->pxp, bo) != 0))
return -ENOEXEC;
return 0;
}
int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
{
struct xe_device *xe = to_xe_device(dev);
struct xe_file *xef = to_xe_file(file);
struct drm_xe_vm_bind *args = data;
struct drm_xe_sync __user *syncs_user;
struct xe_bo **bos = NULL;
struct drm_gpuva_ops **ops = NULL;
struct xe_vm *vm;
struct xe_exec_queue *q = NULL;
u32 num_syncs, num_ufence = 0;
struct xe_sync_entry *syncs = NULL;
struct drm_xe_vm_bind_op *bind_ops = NULL;
struct xe_vma_ops vops = { .flags = 0, };
struct dma_fence *fence;
int err;
int i;
vm = xe_vm_lookup(xef, args->vm_id);
if (XE_IOCTL_DBG(xe, !vm))
return -EINVAL;
err = vm_bind_ioctl_check_args(xe, vm, args, &bind_ops);
if (err)
goto put_vm;
if (args->exec_queue_id) {
q = xe_exec_queue_lookup(xef, args->exec_queue_id);
if (XE_IOCTL_DBG(xe, !q)) {
err = -ENOENT;
goto free_bind_ops;
}
if (XE_IOCTL_DBG(xe, !(q->flags & EXEC_QUEUE_FLAG_VM))) {
err = -EINVAL;
goto put_exec_queue;
}
}
if (XE_IOCTL_DBG(xe, q && vm != q->user_vm)) {
err = -EINVAL;
goto put_exec_queue;
}
xe_svm_flush(vm);
err = down_write_killable(&vm->lock);
if (err)
goto put_exec_queue;
if (XE_IOCTL_DBG(xe, xe_vm_is_closed_or_banned(vm))) {
err = -ENOENT;
goto release_vm_lock;
}
for (i = 0; i < args->num_binds; ++i) {
u64 range = bind_ops[i].range;
u64 addr = bind_ops[i].addr;
if (XE_IOCTL_DBG(xe, range > vm->size) ||
XE_IOCTL_DBG(xe, addr > vm->size - range)) {
err = -EINVAL;
goto release_vm_lock;
}
}
if (args->num_binds) {
bos = kvzalloc_objs(*bos, args->num_binds,
GFP_KERNEL | __GFP_ACCOUNT | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
if (!bos) {
err = -ENOMEM;
goto release_vm_lock;
}
ops = kvzalloc_objs(*ops, args->num_binds,
GFP_KERNEL | __GFP_ACCOUNT | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
if (!ops) {
err = -ENOMEM;
goto free_bos;
}
}
for (i = 0; i < args->num_binds; ++i) {
struct drm_gem_object *gem_obj;
u64 range = bind_ops[i].range;
u64 addr = bind_ops[i].addr;
u32 obj = bind_ops[i].obj;
u64 obj_offset = bind_ops[i].obj_offset;
u16 pat_index = bind_ops[i].pat_index;
u32 op = bind_ops[i].op;
u32 bind_flags = bind_ops[i].flags;
if (!obj)
continue;
gem_obj = drm_gem_object_lookup(file, obj);
if (XE_IOCTL_DBG(xe, !gem_obj)) {
err = -ENOENT;
goto put_obj;
}
bos[i] = gem_to_xe_bo(gem_obj);
err = xe_vm_bind_ioctl_validate_bo(xe, bos[i], addr, range,
obj_offset, pat_index, op,
bind_flags);
if (err)
goto put_obj;
}
if (args->num_syncs) {
syncs = kzalloc_objs(*syncs, args->num_syncs);
if (!syncs) {
err = -ENOMEM;
goto put_obj;
}
}
syncs_user = u64_to_user_ptr(args->syncs);
for (num_syncs = 0; num_syncs < args->num_syncs; num_syncs++) {
struct xe_exec_queue *__q = q ?: vm->q;
err = xe_sync_entry_parse(xe, xef, &syncs[num_syncs],
&syncs_user[num_syncs],
__q->ufence_syncobj,
++__q->ufence_timeline_value,
(xe_vm_in_lr_mode(vm) ?
SYNC_PARSE_FLAG_LR_MODE : 0) |
(!args->num_binds ?
SYNC_PARSE_FLAG_DISALLOW_USER_FENCE : 0));
if (err)
goto free_syncs;
if (xe_sync_is_ufence(&syncs[num_syncs]))
num_ufence++;
}
if (XE_IOCTL_DBG(xe, num_ufence > 1)) {
err = -EINVAL;
goto free_syncs;
}
if (!args->num_binds) {
err = -ENODATA;
goto free_syncs;
}
xe_vma_ops_init(&vops, vm, q, syncs, num_syncs);
if (args->num_binds > 1)
vops.flags |= XE_VMA_OPS_ARRAY_OF_BINDS;
for (i = 0; i < args->num_binds; ++i) {
u64 range = bind_ops[i].range;
u64 addr = bind_ops[i].addr;
u32 op = bind_ops[i].op;
u32 flags = bind_ops[i].flags;
u64 obj_offset = bind_ops[i].obj_offset;
u32 prefetch_region = bind_ops[i].prefetch_mem_region_instance;
u16 pat_index = bind_ops[i].pat_index;
ops[i] = vm_bind_ioctl_ops_create(vm, &vops, bos[i], obj_offset,
addr, range, op, flags,
prefetch_region, pat_index);
if (IS_ERR(ops[i])) {
err = PTR_ERR(ops[i]);
ops[i] = NULL;
goto unwind_ops;
}
err = vm_bind_ioctl_ops_parse(vm, ops[i], &vops);
if (err)
goto unwind_ops;
#ifdef TEST_VM_OPS_ERROR
if (flags & FORCE_OP_ERROR) {
vops.inject_error = true;
vm->xe->vm_inject_error_position =
(vm->xe->vm_inject_error_position + 1) %
FORCE_OP_ERROR_COUNT;
}
#endif
}
if (list_empty(&vops.list)) {
err = -ENODATA;
goto unwind_ops;
}
if (!(vops.flags & XE_VMA_OPS_FLAG_MODIFIES_GPUVA)) {
vops.flags |= XE_VMA_OPS_FLAG_DOWNGRADE_LOCK;
downgrade_write(&vm->lock);
}
err = xe_vma_ops_alloc(&vops, args->num_binds > 1);
if (err)
goto unwind_ops;
err = vm_bind_ioctl_ops_prefetch_ranges(vm, &vops);
if (err)
goto unwind_ops;
fence = vm_bind_ioctl_ops_execute(vm, &vops);
if (IS_ERR(fence))
err = PTR_ERR(fence);
else
dma_fence_put(fence);
unwind_ops:
if (err && err != -ENODATA)
vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
xe_vma_ops_fini(&vops);
for (i = args->num_binds - 1; i >= 0; --i)
if (ops[i])
drm_gpuva_ops_free(&vm->gpuvm, ops[i]);
free_syncs:
if (err == -ENODATA)
err = vm_bind_ioctl_signal_fences(vm, q, syncs, num_syncs);
while (num_syncs--)
xe_sync_entry_cleanup(&syncs[num_syncs]);
kfree(syncs);
put_obj:
for (i = 0; i < args->num_binds; ++i)
xe_bo_put(bos[i]);
kvfree(ops);
free_bos:
kvfree(bos);
release_vm_lock:
if (vops.flags & XE_VMA_OPS_FLAG_DOWNGRADE_LOCK)
up_read(&vm->lock);
else
up_write(&vm->lock);
put_exec_queue:
if (q)
xe_exec_queue_put(q);
free_bind_ops:
if (args->num_binds > 1)
kvfree(bind_ops);
put_vm:
xe_vm_put(vm);
return err;
}
static u8 xe_to_user_access_type(u8 access_type)
{
return access_type & XE_PAGEFAULT_ACCESS_TYPE_MASK;
}
static u8 xe_to_user_fault_type(u8 fault_type)
{
return fault_type;
}
static u8 xe_to_user_fault_level(u8 fault_level)
{
return fault_level;
}
static u8 xe_to_user_srcid(u8 srcid)
{
return srcid;
}
static int fill_faults(struct xe_vm *vm,
struct drm_xe_vm_get_property *args)
{
struct xe_vm_fault __user *usr_ptr = u64_to_user_ptr(args->data);
struct xe_vm_fault *fault_list, fault_entry = { 0 };
struct xe_vm_fault_entry *entry;
int ret = 0, i = 0, count, entry_size;
entry_size = sizeof(struct xe_vm_fault);
count = args->size / entry_size;
fault_list = kzalloc_objs(struct xe_vm_fault, count);
if (!fault_list)
return -ENOMEM;
spin_lock(&vm->faults.lock);
list_for_each_entry(entry, &vm->faults.list, list) {
if (i == count)
break;
fault_entry.address = xe_device_canonicalize_addr(vm->xe, entry->address);
fault_entry.address_precision = entry->address_precision;
fault_entry.access_type = xe_to_user_access_type(entry->access_type);
fault_entry.fault_type = xe_to_user_fault_type(entry->fault_type);
fault_entry.fault_level = xe_to_user_fault_level(entry->fault_level);
fault_entry.srcid = xe_to_user_srcid(entry->srcid);
memcpy(&fault_list[i], &fault_entry, entry_size);
i++;
}
spin_unlock(&vm->faults.lock);
ret = copy_to_user(usr_ptr, fault_list, args->size);
kfree(fault_list);
return ret ? -EFAULT : 0;
}
static int xe_vm_get_property_helper(struct xe_vm *vm,
struct drm_xe_vm_get_property *args)
{
size_t size;
switch (args->property) {
case DRM_XE_VM_GET_PROPERTY_FAULTS:
spin_lock(&vm->faults.lock);
size = size_mul(sizeof(struct xe_vm_fault), vm->faults.len);
spin_unlock(&vm->faults.lock);
if (!args->size) {
args->size = size;
return 0;
}
if (args->size > size || args->size % sizeof(struct xe_vm_fault))
return -EINVAL;
return fill_faults(vm, args);
}
return -EINVAL;
}
int xe_vm_get_property_ioctl(struct drm_device *drm, void *data,
struct drm_file *file)
{
struct xe_device *xe = to_xe_device(drm);
struct xe_file *xef = to_xe_file(file);
struct drm_xe_vm_get_property *args = data;
struct xe_vm *vm;
int ret = 0;
if (XE_IOCTL_DBG(xe, (args->reserved[0] || args->reserved[1] ||
args->reserved[2] || args->extensions ||
args->pad)))
return -EINVAL;
vm = xe_vm_lookup(xef, args->vm_id);
if (XE_IOCTL_DBG(xe, !vm))
return -ENOENT;
ret = xe_vm_get_property_helper(vm, args);
xe_vm_put(vm);
return ret;
}
struct dma_fence *xe_vm_bind_kernel_bo(struct xe_vm *vm, struct xe_bo *bo,
struct xe_exec_queue *q, u64 addr,
enum xe_cache_level cache_lvl)
{
struct xe_vma_ops vops;
struct drm_gpuva_ops *ops = NULL;
struct dma_fence *fence;
int err;
xe_bo_get(bo);
xe_vm_get(vm);
if (q)
xe_exec_queue_get(q);
down_write(&vm->lock);
xe_vma_ops_init(&vops, vm, q, NULL, 0);
ops = vm_bind_ioctl_ops_create(vm, &vops, bo, 0, addr, xe_bo_size(bo),
DRM_XE_VM_BIND_OP_MAP, 0, 0,
xe_cache_pat_idx(vm->xe, cache_lvl));
if (IS_ERR(ops)) {
err = PTR_ERR(ops);
goto release_vm_lock;
}
err = vm_bind_ioctl_ops_parse(vm, ops, &vops);
if (err)
goto release_vm_lock;
xe_assert(vm->xe, !list_empty(&vops.list));
err = xe_vma_ops_alloc(&vops, false);
if (err)
goto unwind_ops;
fence = vm_bind_ioctl_ops_execute(vm, &vops);
if (IS_ERR(fence))
err = PTR_ERR(fence);
unwind_ops:
if (err && err != -ENODATA)
vm_bind_ioctl_ops_unwind(vm, &ops, 1);
xe_vma_ops_fini(&vops);
drm_gpuva_ops_free(&vm->gpuvm, ops);
release_vm_lock:
up_write(&vm->lock);
if (q)
xe_exec_queue_put(q);
xe_vm_put(vm);
xe_bo_put(bo);
if (err)
fence = ERR_PTR(err);
return fence;
}
int xe_vm_lock(struct xe_vm *vm, bool intr)
{
int ret;
if (intr)
ret = dma_resv_lock_interruptible(xe_vm_resv(vm), NULL);
else
ret = dma_resv_lock(xe_vm_resv(vm), NULL);
return ret;
}
void xe_vm_unlock(struct xe_vm *vm)
{
dma_resv_unlock(xe_vm_resv(vm));
}
int xe_vm_invalidate_vma_submit(struct xe_vma *vma, struct xe_tlb_inval_batch *batch)
{
struct xe_device *xe = xe_vma_vm(vma)->xe;
struct xe_vm *vm = xe_vma_vm(vma);
struct xe_tile *tile;
u8 tile_mask = 0;
int ret = 0;
u8 id;
xe_assert(xe, !xe_vma_is_null(vma));
xe_assert(xe, !xe_vma_is_cpu_addr_mirror(vma));
trace_xe_vma_invalidate(vma);
vm_dbg(&vm->xe->drm,
"INVALIDATE: addr=0x%016llx, range=0x%016llx",
xe_vma_start(vma), xe_vma_size(vma));
if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
if (xe_vma_is_userptr(vma)) {
lockdep_assert(lockdep_is_held_type(&vm->svm.gpusvm.notifier_lock, 0) ||
(lockdep_is_held_type(&vm->svm.gpusvm.notifier_lock, 1) &&
lockdep_is_held(&xe_vm_resv(vm)->lock.base)));
WARN_ON_ONCE(!mmu_interval_check_retry
(&to_userptr_vma(vma)->userptr.notifier,
to_userptr_vma(vma)->userptr.pages.notifier_seq));
WARN_ON_ONCE(!dma_resv_test_signaled(xe_vm_resv(vm),
DMA_RESV_USAGE_BOOKKEEP));
} else {
xe_bo_assert_held(xe_vma_bo(vma));
}
}
for_each_tile(tile, xe, id)
if (xe_pt_zap_ptes(tile, vma))
tile_mask |= BIT(id);
xe_device_wmb(xe);
ret = xe_tlb_inval_range_tilemask_submit(xe, xe_vma_vm(vma)->usm.asid,
xe_vma_start(vma), xe_vma_end(vma),
tile_mask, batch);
WRITE_ONCE(vma->tile_invalidated, vma->tile_mask);
return ret;
}
int xe_vm_invalidate_vma(struct xe_vma *vma)
{
struct xe_tlb_inval_batch batch;
int ret;
ret = xe_vm_invalidate_vma_submit(vma, &batch);
if (ret)
return ret;
xe_tlb_inval_batch_wait(&batch);
return ret;
}
int xe_vm_validate_protected(struct xe_vm *vm)
{
struct drm_gpuva *gpuva;
int err = 0;
if (!vm)
return -ENODEV;
mutex_lock(&vm->snap_mutex);
drm_gpuvm_for_each_va(gpuva, &vm->gpuvm) {
struct xe_vma *vma = gpuva_to_vma(gpuva);
struct xe_bo *bo = vma->gpuva.gem.obj ?
gem_to_xe_bo(vma->gpuva.gem.obj) : NULL;
if (!bo)
continue;
if (xe_bo_is_protected(bo)) {
err = xe_pxp_bo_key_check(vm->xe->pxp, bo);
if (err)
break;
}
}
mutex_unlock(&vm->snap_mutex);
return err;
}
struct xe_vm_snapshot {
int uapi_flags;
unsigned long num_snaps;
struct {
u64 ofs, bo_ofs;
unsigned long len;
#define XE_VM_SNAP_FLAG_USERPTR BIT(0)
#define XE_VM_SNAP_FLAG_READ_ONLY BIT(1)
#define XE_VM_SNAP_FLAG_IS_NULL BIT(2)
unsigned long flags;
int uapi_mem_region;
u16 pat_index;
int cpu_caching;
struct xe_bo *bo;
void *data;
struct mm_struct *mm;
} snap[];
};
struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm)
{
unsigned long num_snaps = 0, i;
struct xe_vm_snapshot *snap = NULL;
struct drm_gpuva *gpuva;
if (!vm)
return NULL;
mutex_lock(&vm->snap_mutex);
drm_gpuvm_for_each_va(gpuva, &vm->gpuvm) {
if (gpuva->flags & XE_VMA_DUMPABLE)
num_snaps++;
}
if (num_snaps)
snap = kvzalloc(offsetof(struct xe_vm_snapshot, snap[num_snaps]), GFP_NOWAIT);
if (!snap) {
snap = num_snaps ? ERR_PTR(-ENOMEM) : ERR_PTR(-ENODEV);
goto out_unlock;
}
if (vm->flags & XE_VM_FLAG_FAULT_MODE)
snap->uapi_flags |= DRM_XE_VM_CREATE_FLAG_FAULT_MODE;
if (vm->flags & XE_VM_FLAG_LR_MODE)
snap->uapi_flags |= DRM_XE_VM_CREATE_FLAG_LR_MODE;
if (vm->flags & XE_VM_FLAG_SCRATCH_PAGE)
snap->uapi_flags |= DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE;
snap->num_snaps = num_snaps;
i = 0;
drm_gpuvm_for_each_va(gpuva, &vm->gpuvm) {
struct xe_vma *vma = gpuva_to_vma(gpuva);
struct xe_bo *bo = vma->gpuva.gem.obj ?
gem_to_xe_bo(vma->gpuva.gem.obj) : NULL;
if (!(gpuva->flags & XE_VMA_DUMPABLE))
continue;
snap->snap[i].ofs = xe_vma_start(vma);
snap->snap[i].len = xe_vma_size(vma);
snap->snap[i].flags = xe_vma_read_only(vma) ?
XE_VM_SNAP_FLAG_READ_ONLY : 0;
snap->snap[i].pat_index = vma->attr.pat_index;
if (bo) {
snap->snap[i].cpu_caching = bo->cpu_caching;
snap->snap[i].bo = xe_bo_get(bo);
snap->snap[i].bo_ofs = xe_vma_bo_offset(vma);
switch (bo->ttm.resource->mem_type) {
case XE_PL_SYSTEM:
case XE_PL_TT:
snap->snap[i].uapi_mem_region = 0;
break;
case XE_PL_VRAM0:
snap->snap[i].uapi_mem_region = 1;
break;
case XE_PL_VRAM1:
snap->snap[i].uapi_mem_region = 2;
break;
}
} else if (xe_vma_is_userptr(vma)) {
struct mm_struct *mm =
to_userptr_vma(vma)->userptr.notifier.mm;
if (mmget_not_zero(mm))
snap->snap[i].mm = mm;
else
snap->snap[i].data = ERR_PTR(-EFAULT);
snap->snap[i].bo_ofs = xe_vma_userptr(vma);
snap->snap[i].flags |= XE_VM_SNAP_FLAG_USERPTR;
snap->snap[i].uapi_mem_region = 0;
} else if (xe_vma_is_null(vma)) {
snap->snap[i].flags |= XE_VM_SNAP_FLAG_IS_NULL;
snap->snap[i].uapi_mem_region = -1;
} else {
snap->snap[i].data = ERR_PTR(-ENOENT);
snap->snap[i].uapi_mem_region = -1;
}
i++;
}
out_unlock:
mutex_unlock(&vm->snap_mutex);
return snap;
}
void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap)
{
if (IS_ERR_OR_NULL(snap))
return;
for (int i = 0; i < snap->num_snaps; i++) {
struct xe_bo *bo = snap->snap[i].bo;
int err;
if (IS_ERR(snap->snap[i].data) ||
snap->snap[i].flags & XE_VM_SNAP_FLAG_IS_NULL)
continue;
snap->snap[i].data = kvmalloc(snap->snap[i].len, GFP_USER);
if (!snap->snap[i].data) {
snap->snap[i].data = ERR_PTR(-ENOMEM);
goto cleanup_bo;
}
if (bo) {
err = xe_bo_read(bo, snap->snap[i].bo_ofs,
snap->snap[i].data, snap->snap[i].len);
} else {
void __user *userptr = (void __user *)(size_t)snap->snap[i].bo_ofs;
kthread_use_mm(snap->snap[i].mm);
if (!copy_from_user(snap->snap[i].data, userptr, snap->snap[i].len))
err = 0;
else
err = -EFAULT;
kthread_unuse_mm(snap->snap[i].mm);
mmput(snap->snap[i].mm);
snap->snap[i].mm = NULL;
}
if (err) {
kvfree(snap->snap[i].data);
snap->snap[i].data = ERR_PTR(err);
}
cleanup_bo:
xe_bo_put(bo);
snap->snap[i].bo = NULL;
}
}
void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p)
{
unsigned long i, j;
if (IS_ERR_OR_NULL(snap)) {
drm_printf(p, "[0].error: %li\n", PTR_ERR(snap));
return;
}
drm_printf(p, "VM.uapi_flags: 0x%x\n", snap->uapi_flags);
for (i = 0; i < snap->num_snaps; i++) {
drm_printf(p, "[%llx].length: 0x%lx\n", snap->snap[i].ofs, snap->snap[i].len);
drm_printf(p, "[%llx].properties: %s|%s|mem_region=0x%lx|pat_index=%d|cpu_caching=%d\n",
snap->snap[i].ofs,
snap->snap[i].flags & XE_VM_SNAP_FLAG_READ_ONLY ?
"read_only" : "read_write",
snap->snap[i].flags & XE_VM_SNAP_FLAG_IS_NULL ?
"null_sparse" :
snap->snap[i].flags & XE_VM_SNAP_FLAG_USERPTR ?
"userptr" : "bo",
snap->snap[i].uapi_mem_region == -1 ? 0 :
BIT(snap->snap[i].uapi_mem_region),
snap->snap[i].pat_index,
snap->snap[i].cpu_caching);
if (IS_ERR(snap->snap[i].data)) {
drm_printf(p, "[%llx].error: %li\n", snap->snap[i].ofs,
PTR_ERR(snap->snap[i].data));
continue;
}
if (snap->snap[i].flags & XE_VM_SNAP_FLAG_IS_NULL)
continue;
drm_printf(p, "[%llx].data: ", snap->snap[i].ofs);
for (j = 0; j < snap->snap[i].len; j += sizeof(u32)) {
u32 *val = snap->snap[i].data + j;
char dumped[ASCII85_BUFSZ];
drm_puts(p, ascii85_encode(*val, dumped));
}
drm_puts(p, "\n");
if (drm_coredump_printer_is_full(p))
return;
}
}
void xe_vm_snapshot_free(struct xe_vm_snapshot *snap)
{
unsigned long i;
if (IS_ERR_OR_NULL(snap))
return;
for (i = 0; i < snap->num_snaps; i++) {
if (!IS_ERR(snap->snap[i].data))
kvfree(snap->snap[i].data);
xe_bo_put(snap->snap[i].bo);
if (snap->snap[i].mm)
mmput(snap->snap[i].mm);
}
kvfree(snap);
}
int xe_vma_need_vram_for_atomic(struct xe_device *xe, struct xe_vma *vma, bool is_atomic)
{
u32 atomic_access = xe_vma_bo(vma) ? xe_vma_bo(vma)->attr.atomic_access :
vma->attr.atomic_access;
if (!IS_DGFX(xe) || !is_atomic)
return false;
switch (atomic_access) {
case DRM_XE_ATOMIC_DEVICE:
return !xe->info.has_device_atomics_on_smem;
case DRM_XE_ATOMIC_CPU:
return -EACCES;
case DRM_XE_ATOMIC_UNDEFINED:
case DRM_XE_ATOMIC_GLOBAL:
default:
return 1;
}
}
static int xe_vm_alloc_vma(struct xe_vm *vm,
struct drm_gpuvm_map_req *map_req,
bool is_madvise)
{
struct xe_vma_ops vops;
struct drm_gpuva_ops *ops = NULL;
struct drm_gpuva_op *__op;
unsigned int vma_flags = 0;
bool remap_op = false;
struct xe_vma_mem_attr tmp_attr = {};
u16 default_pat;
int err;
xe_vm_assert_write_mode_or_garbage_collector(vm);
if (is_madvise)
ops = drm_gpuvm_madvise_ops_create(&vm->gpuvm, map_req);
else
ops = drm_gpuvm_sm_map_ops_create(&vm->gpuvm, map_req);
if (IS_ERR(ops))
return PTR_ERR(ops);
if (list_empty(&ops->list)) {
err = 0;
goto free_ops;
}
drm_gpuva_for_each_op(__op, ops) {
struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
struct xe_vma *vma = NULL;
if (!is_madvise) {
if (__op->op == DRM_GPUVA_OP_UNMAP) {
vma = gpuva_to_vma(op->base.unmap.va);
XE_WARN_ON(!xe_vma_has_default_mem_attrs(vma));
default_pat = vma->attr.default_pat_index;
vma_flags = vma->gpuva.flags;
}
if (__op->op == DRM_GPUVA_OP_REMAP) {
vma = gpuva_to_vma(op->base.remap.unmap->va);
default_pat = vma->attr.default_pat_index;
vma_flags = vma->gpuva.flags;
}
if (__op->op == DRM_GPUVA_OP_MAP) {
op->map.vma_flags |= vma_flags & XE_VMA_CREATE_MASK;
op->map.pat_index = default_pat;
}
} else {
if (__op->op == DRM_GPUVA_OP_REMAP) {
vma = gpuva_to_vma(op->base.remap.unmap->va);
xe_assert(vm->xe, !remap_op);
xe_assert(vm->xe, xe_vma_has_no_bo(vma));
remap_op = true;
vma_flags = vma->gpuva.flags;
}
if (__op->op == DRM_GPUVA_OP_MAP) {
xe_assert(vm->xe, remap_op);
remap_op = false;
op->map.vma_flags |= vma_flags & XE_VMA_CREATE_MASK;
}
}
print_op(vm->xe, __op);
}
xe_vma_ops_init(&vops, vm, NULL, NULL, 0);
if (is_madvise)
vops.flags |= XE_VMA_OPS_FLAG_MADVISE;
else
vops.flags |= XE_VMA_OPS_FLAG_ALLOW_SVM_UNMAP;
err = vm_bind_ioctl_ops_parse(vm, ops, &vops);
if (err)
goto unwind_ops;
xe_vm_lock(vm, false);
drm_gpuva_for_each_op(__op, ops) {
struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
struct xe_vma *vma;
if (__op->op == DRM_GPUVA_OP_UNMAP) {
vma = gpuva_to_vma(op->base.unmap.va);
if (is_madvise)
XE_WARN_ON("UNEXPECTED UNMAP");
xe_vma_destroy(vma, NULL);
} else if (__op->op == DRM_GPUVA_OP_REMAP) {
vma = gpuva_to_vma(op->base.remap.unmap->va);
if (is_madvise)
xe_vma_mem_attr_copy(&tmp_attr, &vma->attr);
xe_vma_destroy(gpuva_to_vma(op->base.remap.unmap->va), NULL);
} else if (__op->op == DRM_GPUVA_OP_MAP) {
vma = op->map.vma;
if (is_madvise)
xe_vma_mem_attr_copy(&vma->attr, &tmp_attr);
}
}
xe_vm_unlock(vm);
drm_gpuva_ops_free(&vm->gpuvm, ops);
xe_vma_mem_attr_fini(&tmp_attr);
return 0;
unwind_ops:
vm_bind_ioctl_ops_unwind(vm, &ops, 1);
free_ops:
drm_gpuva_ops_free(&vm->gpuvm, ops);
return err;
}
int xe_vm_alloc_madvise_vma(struct xe_vm *vm, uint64_t start, uint64_t range)
{
struct drm_gpuvm_map_req map_req = {
.map.va.addr = start,
.map.va.range = range,
};
xe_vm_assert_write_mode_or_garbage_collector(vm);
vm_dbg(&vm->xe->drm, "MADVISE_OPS_CREATE: addr=0x%016llx, size=0x%016llx", start, range);
return xe_vm_alloc_vma(vm, &map_req, true);
}
static bool is_cpu_addr_vma_with_default_attr(struct xe_vma *vma)
{
return vma && xe_vma_is_cpu_addr_mirror(vma) &&
xe_vma_has_default_mem_attrs(vma);
}
void xe_vm_find_cpu_addr_mirror_vma_range(struct xe_vm *vm, u64 *start, u64 *end)
{
struct xe_vma *prev, *next;
xe_vm_assert_write_mode_or_garbage_collector(vm);
if (*start >= SZ_4K) {
prev = xe_vm_find_vma_by_addr(vm, *start - SZ_4K);
if (is_cpu_addr_vma_with_default_attr(prev))
*start = xe_vma_start(prev);
}
if (*end < vm->size) {
next = xe_vm_find_vma_by_addr(vm, *end + 1);
if (is_cpu_addr_vma_with_default_attr(next))
*end = xe_vma_end(next);
}
}
int xe_vm_alloc_cpu_addr_mirror_vma(struct xe_vm *vm, uint64_t start, uint64_t range)
{
struct drm_gpuvm_map_req map_req = {
.map.va.addr = start,
.map.va.range = range,
};
xe_vm_assert_write_mode_or_garbage_collector(vm);
vm_dbg(&vm->xe->drm, "CPU_ADDR_MIRROR_VMA_OPS_CREATE: addr=0x%016llx, size=0x%016llx",
start, range);
return xe_vm_alloc_vma(vm, &map_req, false);
}
void xe_vm_add_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
{
struct xe_device *xe = vm->xe;
xe_assert(xe, !(q->flags & EXEC_QUEUE_FLAG_KERNEL));
xe_assert(xe, !(q->flags & EXEC_QUEUE_FLAG_VM));
xe_assert(xe, !(q->flags & EXEC_QUEUE_FLAG_MIGRATE));
xe_assert(xe, vm->xef);
xe_assert(xe, vm == q->vm);
if (!xe->info.has_ctx_tlb_inval)
return;
down_write(&vm->exec_queues.lock);
list_add(&q->vm_exec_queue_link, &vm->exec_queues.list[q->gt->info.id]);
++vm->exec_queues.count[q->gt->info.id];
up_write(&vm->exec_queues.lock);
}
void xe_vm_remove_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
{
if (!vm->xe->info.has_ctx_tlb_inval)
return;
down_write(&vm->exec_queues.lock);
if (!list_empty(&q->vm_exec_queue_link)) {
list_del(&q->vm_exec_queue_link);
--vm->exec_queues.count[q->gt->info.id];
}
up_write(&vm->exec_queues.lock);
}
struct xe_pt *xe_vm_pt_root(struct xe_vm *vm, u8 tile_id)
{
if (vm->xe->info.has_pt_mirror)
return vm->pt_root[tile_id];
return vm->pt_root[0];
}