123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209
#include "xe_bo.h"
#include <linux/dma-buf.h>
#include <linux/nospec.h>
#include <drm/drm_drv.h>
#include <drm/drm_dumb_buffers.h>
#include <drm/drm_gem_ttm_helper.h>
#include <drm/drm_managed.h>
#include <drm/ttm/ttm_backup.h>
#include <drm/ttm/ttm_device.h>
#include <drm/ttm/ttm_placement.h>
#include <drm/ttm/ttm_tt.h>
#include <uapi/drm/xe_drm.h>
#include <kunit/static_stub.h>
#include <trace/events/gpu_mem.h>
#include "xe_device.h"
#include "xe_dma_buf.h"
#include "xe_drm_client.h"
#include "xe_ggtt.h"
#include "xe_map.h"
#include "xe_migrate.h"
#include "xe_mmio_gem.h"
#include "xe_pat.h"
#include "xe_pm.h"
#include "xe_preempt_fence.h"
#include "xe_pxp.h"
#include "xe_res_cursor.h"
#include "xe_shrinker.h"
#include "xe_sriov_vf_ccs.h"
#include "xe_tile.h"
#include "xe_trace_bo.h"
#include "xe_ttm_stolen_mgr.h"
#include "xe_vm.h"
#include "xe_vram_types.h"
const char *const xe_mem_type_to_name[TTM_NUM_MEM_TYPES] = {
[XE_PL_SYSTEM] = "system",
[XE_PL_TT] = "gtt",
[XE_PL_VRAM0] = "vram0",
[XE_PL_VRAM1] = "vram1",
[XE_PL_STOLEN] = "stolen"
};
static const struct ttm_place sys_placement_flags = {
.fpfn = 0,
.lpfn = 0,
.mem_type = XE_PL_SYSTEM,
.flags = 0,
};
static struct ttm_placement sys_placement = {
.num_placement = 1,
.placement = &sys_placement_flags,
};
static struct ttm_placement purge_placement;
static const struct ttm_place tt_placement_flags[] = {
{
.fpfn = 0,
.lpfn = 0,
.mem_type = XE_PL_TT,
.flags = TTM_PL_FLAG_DESIRED,
},
{
.fpfn = 0,
.lpfn = 0,
.mem_type = XE_PL_SYSTEM,
.flags = TTM_PL_FLAG_FALLBACK,
}
};
static struct ttm_placement tt_placement = {
.num_placement = 2,
.placement = tt_placement_flags,
};
#define for_each_set_bo_vram_flag(bit__, bo_flags__) \
for (unsigned int __bit_tmp = BIT(0); __bit_tmp <= XE_BO_FLAG_VRAM_MASK; __bit_tmp <<= 1) \
for_each_if(((bit__) = __bit_tmp) & (bo_flags__) & XE_BO_FLAG_VRAM_MASK)
bool mem_type_is_vram(u32 mem_type)
{
return mem_type >= XE_PL_VRAM0 && mem_type != XE_PL_STOLEN;
}
static bool resource_is_stolen_vram(struct xe_device *xe, struct ttm_resource *res)
{
return res->mem_type == XE_PL_STOLEN && IS_DGFX(xe);
}
static bool resource_is_vram(struct ttm_resource *res)
{
return mem_type_is_vram(res->mem_type);
}
bool xe_bo_is_vram(struct xe_bo *bo)
{
struct ttm_resource *res = bo->ttm.resource;
return res && (resource_is_vram(res) || resource_is_stolen_vram(xe_bo_device(bo), res));
}
bool xe_bo_is_stolen(struct xe_bo *bo)
{
struct ttm_resource *res = bo->ttm.resource;
return res && res->mem_type == XE_PL_STOLEN;
}
bool xe_bo_has_single_placement(struct xe_bo *bo)
{
return bo->placement.num_placement == 1;
}
bool xe_bo_is_stolen_devmem(struct xe_bo *bo)
{
return xe_bo_is_stolen(bo) &&
GRAPHICS_VERx100(xe_bo_device(bo)) >= 1270;
}
bool xe_bo_is_vm_bound(struct xe_bo *bo)
{
xe_bo_assert_held(bo);
return !list_empty(&bo->ttm.base.gpuva.list);
}
bool xe_bo_is_user(struct xe_bo *bo)
{
return bo->flags & XE_BO_FLAG_USER;
}
static struct xe_migrate *
mem_type_to_migrate(struct xe_device *xe, u32 mem_type)
{
struct xe_tile *tile;
xe_assert(xe, mem_type == XE_PL_STOLEN || mem_type_is_vram(mem_type));
tile = &xe->tiles[mem_type == XE_PL_STOLEN ? 0 : (mem_type - XE_PL_VRAM0)];
return tile->migrate;
}
static void try_add_system(struct xe_device *xe, struct xe_bo *bo,
u32 bo_flags, u32 *c)
{
if (bo_flags & XE_BO_FLAG_SYSTEM) {
xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
bo->placements[*c] = (struct ttm_place) {
.mem_type = XE_PL_TT,
.flags = (bo_flags & XE_BO_FLAG_VRAM_MASK) ?
TTM_PL_FLAG_FALLBACK : 0,
};
*c += 1;
}
}
static bool force_contiguous(u32 bo_flags)
{
if (bo_flags & XE_BO_FLAG_STOLEN)
return true;
else if (bo_flags & XE_BO_FLAG_PINNED &&
!(bo_flags & XE_BO_FLAG_PINNED_LATE_RESTORE))
return true;
else if (bo_flags & XE_BO_FLAG_CPU_ADDR_MIRROR)
return true;
return bo_flags & XE_BO_FLAG_NEEDS_CPU_ACCESS &&
bo_flags & XE_BO_FLAG_PINNED;
}
static u8 vram_bo_flag_to_tile_id(struct xe_device *xe, u32 vram_bo_flag)
{
xe_assert(xe, vram_bo_flag & XE_BO_FLAG_VRAM_MASK);
xe_assert(xe, (vram_bo_flag & (vram_bo_flag - 1)) == 0);
return __ffs(vram_bo_flag >> (__ffs(XE_BO_FLAG_VRAM0) - 1)) - 1;
}
static u32 bo_vram_flags_to_vram_placement(struct xe_device *xe, u32 bo_flags, u32 vram_flag,
enum ttm_bo_type type)
{
u8 tile_id = vram_bo_flag_to_tile_id(xe, vram_flag);
xe_assert(xe, tile_id < xe->info.tile_count);
if (type == ttm_bo_type_kernel && !(bo_flags & XE_BO_FLAG_FORCE_USER_VRAM))
return xe->tiles[tile_id].mem.kernel_vram->placement;
else
return xe->tiles[tile_id].mem.vram->placement;
}
static void add_vram(struct xe_device *xe, struct xe_bo *bo,
struct ttm_place *places, u32 bo_flags, u32 mem_type, u32 *c)
{
struct ttm_place place = { .mem_type = mem_type };
struct ttm_resource_manager *mgr = ttm_manager_type(&xe->ttm, mem_type);
struct xe_ttm_vram_mgr *vram_mgr = to_xe_ttm_vram_mgr(mgr);
struct xe_vram_region *vram;
u64 io_size;
xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
vram = container_of(vram_mgr, struct xe_vram_region, ttm);
xe_assert(xe, vram && vram->usable_size);
io_size = vram->io_size;
if (force_contiguous(bo_flags))
place.flags |= TTM_PL_FLAG_CONTIGUOUS;
if (io_size < vram->usable_size) {
if (bo_flags & XE_BO_FLAG_NEEDS_CPU_ACCESS) {
place.fpfn = 0;
place.lpfn = io_size >> PAGE_SHIFT;
} else {
place.flags |= TTM_PL_FLAG_TOPDOWN;
}
}
places[*c] = place;
*c += 1;
}
static void try_add_vram(struct xe_device *xe, struct xe_bo *bo,
u32 bo_flags, enum ttm_bo_type type, u32 *c)
{
u32 vram_flag;
for_each_set_bo_vram_flag(vram_flag, bo_flags) {
u32 pl = bo_vram_flags_to_vram_placement(xe, bo_flags, vram_flag, type);
add_vram(xe, bo, bo->placements, bo_flags, pl, c);
}
}
static void try_add_stolen(struct xe_device *xe, struct xe_bo *bo,
u32 bo_flags, u32 *c)
{
if (bo_flags & XE_BO_FLAG_STOLEN) {
xe_assert(xe, *c < ARRAY_SIZE(bo->placements));
bo->placements[*c] = (struct ttm_place) {
.mem_type = XE_PL_STOLEN,
.flags = force_contiguous(bo_flags) ?
TTM_PL_FLAG_CONTIGUOUS : 0,
};
*c += 1;
}
}
static int __xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo,
u32 bo_flags, enum ttm_bo_type type)
{
u32 c = 0;
try_add_vram(xe, bo, bo_flags, type, &c);
try_add_system(xe, bo, bo_flags, &c);
try_add_stolen(xe, bo, bo_flags, &c);
if (!c)
return -EINVAL;
bo->placement = (struct ttm_placement) {
.num_placement = c,
.placement = bo->placements,
};
return 0;
}
int xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo,
u32 bo_flags, enum ttm_bo_type type)
{
xe_bo_assert_held(bo);
return __xe_bo_placement_for_flags(xe, bo, bo_flags, type);
}
static void xe_evict_flags(struct ttm_buffer_object *tbo,
struct ttm_placement *placement)
{
struct xe_device *xe = container_of(tbo->bdev, typeof(*xe), ttm);
bool device_unplugged = drm_dev_is_unplugged(&xe->drm);
struct xe_bo *bo;
if (!xe_bo_is_xe_bo(tbo)) {
if (tbo->type == ttm_bo_type_sg) {
placement->num_placement = 0;
return;
}
*placement = device_unplugged ? purge_placement : sys_placement;
return;
}
if (!tbo->resource) {
placement->num_placement = 0;
return;
}
bo = ttm_to_xe_bo(tbo);
if (bo->flags & XE_BO_FLAG_CPU_ADDR_MIRROR) {
*placement = sys_placement;
return;
}
if (device_unplugged && !tbo->base.dma_buf) {
*placement = purge_placement;
return;
}
if (xe_bo_madv_is_dontneed(bo)) {
*placement = sys_placement;
return;
}
switch (tbo->resource->mem_type) {
case XE_PL_VRAM0:
case XE_PL_VRAM1:
case XE_PL_STOLEN:
*placement = tt_placement;
break;
case XE_PL_TT:
default:
*placement = sys_placement;
break;
}
}
struct xe_ttm_tt {
struct ttm_tt ttm;
struct sg_table sgt;
struct sg_table *sg;
bool purgeable;
};
static int xe_tt_map_sg(struct xe_device *xe, struct ttm_tt *tt)
{
struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
unsigned long num_pages = tt->num_pages;
int ret;
XE_WARN_ON((tt->page_flags & TTM_TT_FLAG_EXTERNAL) &&
!(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE));
if (xe_tt->sg)
return 0;
ret = sg_alloc_table_from_pages_segment(&xe_tt->sgt, tt->pages,
num_pages, 0,
(u64)num_pages << PAGE_SHIFT,
xe_sg_segment_size(xe->drm.dev),
GFP_KERNEL);
if (ret)
return ret;
xe_tt->sg = &xe_tt->sgt;
ret = dma_map_sgtable(xe->drm.dev, xe_tt->sg, DMA_BIDIRECTIONAL,
DMA_ATTR_SKIP_CPU_SYNC);
if (ret) {
sg_free_table(xe_tt->sg);
xe_tt->sg = NULL;
return ret;
}
return 0;
}
static void xe_tt_unmap_sg(struct xe_device *xe, struct ttm_tt *tt)
{
struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
if (xe_tt->sg) {
dma_unmap_sgtable(xe->drm.dev, xe_tt->sg,
DMA_BIDIRECTIONAL, 0);
sg_free_table(xe_tt->sg);
xe_tt->sg = NULL;
}
}
struct sg_table *xe_bo_sg(struct xe_bo *bo)
{
struct ttm_tt *tt = bo->ttm.ttm;
struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
return xe_tt->sg;
}
static void xe_ttm_tt_account_add(struct xe_device *xe, struct ttm_tt *tt)
{
struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
if (xe_tt->purgeable)
xe_shrinker_mod_pages(xe->mem.shrinker, 0, tt->num_pages);
else
xe_shrinker_mod_pages(xe->mem.shrinker, tt->num_pages, 0);
}
static void xe_ttm_tt_account_subtract(struct xe_device *xe, struct ttm_tt *tt)
{
struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
if (xe_tt->purgeable)
xe_shrinker_mod_pages(xe->mem.shrinker, 0, -(long)tt->num_pages);
else
xe_shrinker_mod_pages(xe->mem.shrinker, -(long)tt->num_pages, 0);
}
static bool xe_ttm_bo_is_imported(struct ttm_buffer_object *tbo)
{
dma_resv_assert_held(tbo->base.resv);
return tbo->ttm &&
(tbo->ttm->page_flags & (TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_EXTERNAL_MAPPABLE)) ==
TTM_TT_FLAG_EXTERNAL;
}
static void xe_bo_account_pin(struct xe_bo *bo)
{
struct xe_device *xe = xe_bo_device(bo);
if (bo->ttm.pin_count == 1 && bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm) &&
!xe_ttm_bo_is_imported(&bo->ttm))
xe_ttm_tt_account_subtract(xe, bo->ttm.ttm);
}
static void xe_bo_account_unpin(struct xe_bo *bo)
{
struct xe_device *xe = xe_bo_device(bo);
bool last_unpin = bo->ttm.pin_count == 1;
if (last_unpin && !list_empty(&bo->pinned_link)) {
spin_lock(&xe->pinned.lock);
list_del_init(&bo->pinned_link);
spin_unlock(&xe->pinned.lock);
}
if (last_unpin && bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm) &&
!xe_ttm_bo_is_imported(&bo->ttm))
xe_ttm_tt_account_add(xe, bo->ttm.ttm);
}
void xe_bo_pin_account(struct xe_bo *bo)
{
ttm_bo_pin(&bo->ttm);
xe_bo_account_pin(bo);
}
void xe_bo_unpin_account(struct xe_bo *bo)
{
xe_bo_account_unpin(bo);
ttm_bo_unpin(&bo->ttm);
}
static void update_global_total_pages(struct ttm_device *ttm_dev,
long num_pages)
{
#if IS_ENABLED(CONFIG_TRACE_GPU_MEM)
struct xe_device *xe = ttm_to_xe_device(ttm_dev);
u64 global_total_pages =
atomic64_add_return(num_pages, &xe->global_total_pages);
trace_gpu_mem_total(xe->drm.primary->index, 0,
global_total_pages << PAGE_SHIFT);
#endif
}
static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
u32 page_flags)
{
struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
struct xe_device *xe = xe_bo_device(bo);
struct xe_ttm_tt *xe_tt;
struct ttm_tt *tt;
unsigned long extra_pages;
enum ttm_caching caching = ttm_cached;
int err;
xe_tt = kzalloc_obj(*xe_tt);
if (!xe_tt)
return NULL;
tt = &xe_tt->ttm;
extra_pages = 0;
if (xe_bo_needs_ccs_pages(bo))
extra_pages = DIV_ROUND_UP(xe_device_ccs_bytes(xe, xe_bo_size(bo)),
PAGE_SIZE);
if (!IS_DGFX(xe)) {
switch (bo->cpu_caching) {
case DRM_XE_GEM_CPU_CACHING_WC:
caching = ttm_write_combined;
break;
default:
caching = ttm_cached;
break;
}
WARN_ON((bo->flags & XE_BO_FLAG_USER) && !bo->cpu_caching);
if ((!bo->cpu_caching && bo->flags & XE_BO_FLAG_FORCE_WC) ||
(!xe->info.has_cached_pt && bo->flags & XE_BO_FLAG_PAGETABLE))
caching = ttm_write_combined;
}
if (bo->flags & XE_BO_FLAG_NEEDS_UC) {
xe_assert(xe, bo->cpu_caching == 0);
caching = ttm_uncached;
}
if (ttm_bo->type != ttm_bo_type_sg)
page_flags |= TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_EXTERNAL_MAPPABLE;
err = ttm_tt_init(tt, &bo->ttm, page_flags, caching, extra_pages);
if (err) {
kfree(xe_tt);
return NULL;
}
if (ttm_bo->type != ttm_bo_type_sg) {
err = ttm_tt_setup_backup(tt);
if (err) {
ttm_tt_fini(tt);
kfree(xe_tt);
return NULL;
}
}
return tt;
}
static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt,
struct ttm_operation_ctx *ctx)
{
struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
int err;
if ((tt->page_flags & TTM_TT_FLAG_EXTERNAL) &&
!(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE))
return 0;
if (ttm_tt_is_backed_up(tt) && !xe_tt->purgeable) {
err = ttm_tt_restore(ttm_dev, tt, ctx);
} else {
ttm_tt_clear_backed_up(tt);
err = ttm_pool_alloc(&ttm_dev->pool, tt, ctx);
}
if (err)
return err;
xe_tt->purgeable = false;
xe_ttm_tt_account_add(ttm_to_xe_device(ttm_dev), tt);
update_global_total_pages(ttm_dev, tt->num_pages);
return 0;
}
static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
{
struct xe_device *xe = ttm_to_xe_device(ttm_dev);
if ((tt->page_flags & TTM_TT_FLAG_EXTERNAL) &&
!(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE))
return;
xe_tt_unmap_sg(xe, tt);
ttm_pool_free(&ttm_dev->pool, tt);
xe_ttm_tt_account_subtract(xe, tt);
update_global_total_pages(ttm_dev, -(long)tt->num_pages);
}
static void xe_ttm_tt_destroy(struct ttm_device *ttm_dev, struct ttm_tt *tt)
{
ttm_tt_fini(tt);
kfree(tt);
}
static bool xe_ttm_resource_visible(struct xe_device *xe, struct ttm_resource *mem)
{
struct xe_ttm_vram_mgr_resource *vres;
if (mem->mem_type == XE_PL_STOLEN) {
struct xe_ttm_stolen_mgr *mgr = xe->mem.stolen_mgr;
return mgr->io_base && !xe_ttm_stolen_cpu_access_needs_ggtt(xe);
}
vres = to_xe_ttm_vram_mgr_resource(mem);
return vres->used_visible_size == mem->size;
}
bool xe_bo_is_visible_vram(struct xe_bo *bo)
{
if (drm_WARN_ON(bo->ttm.base.dev, !xe_bo_is_vram(bo)))
return false;
return xe_ttm_resource_visible(xe_bo_device(bo), bo->ttm.resource);
}
static int xe_ttm_io_mem_reserve(struct ttm_device *bdev,
struct ttm_resource *mem)
{
struct xe_device *xe = ttm_to_xe_device(bdev);
switch (mem->mem_type) {
case XE_PL_SYSTEM:
case XE_PL_TT:
return 0;
case XE_PL_VRAM0:
case XE_PL_VRAM1: {
struct xe_vram_region *vram = xe_map_resource_to_region(mem);
if (!xe_ttm_resource_visible(xe, mem))
return -EINVAL;
mem->bus.offset = mem->start << PAGE_SHIFT;
if (vram->mapping &&
mem->placement & TTM_PL_FLAG_CONTIGUOUS)
mem->bus.addr = (u8 __force *)vram->mapping +
mem->bus.offset;
mem->bus.offset += vram->io_start;
mem->bus.is_iomem = true;
#if !IS_ENABLED(CONFIG_X86)
mem->bus.caching = ttm_write_combined;
#endif
return 0;
} case XE_PL_STOLEN:
return xe_ttm_stolen_io_mem_reserve(xe, mem);
default:
return -EINVAL;
}
}
static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
const struct ttm_operation_ctx *ctx)
{
struct dma_resv_iter cursor;
struct dma_fence *fence;
struct drm_gem_object *obj = &bo->ttm.base;
struct drm_gpuvm_bo *vm_bo;
bool idle = false;
int ret = 0;
dma_resv_assert_held(bo->ttm.base.resv);
if (!list_empty(&bo->ttm.base.gpuva.list)) {
dma_resv_iter_begin(&cursor, bo->ttm.base.resv,
DMA_RESV_USAGE_BOOKKEEP);
dma_resv_for_each_fence_unlocked(&cursor, fence)
dma_fence_enable_signaling(fence);
dma_resv_iter_end(&cursor);
}
drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
struct drm_gpuva *gpuva;
if (!xe_vm_in_fault_mode(vm)) {
drm_gpuvm_bo_evict(vm_bo, true);
if (!xe_device_is_l2_flush_optimized(xe))
continue;
}
if (!idle) {
long timeout;
if (ctx->no_wait_gpu &&
!dma_resv_test_signaled(bo->ttm.base.resv,
DMA_RESV_USAGE_BOOKKEEP))
return -EBUSY;
timeout = dma_resv_wait_timeout(bo->ttm.base.resv,
DMA_RESV_USAGE_BOOKKEEP,
ctx->interruptible,
MAX_SCHEDULE_TIMEOUT);
if (!timeout)
return -ETIME;
if (timeout < 0)
return timeout;
idle = true;
}
drm_gpuvm_bo_for_each_va(gpuva, vm_bo) {
struct xe_vma *vma = gpuva_to_vma(gpuva);
trace_xe_vma_evict(vma);
ret = xe_vm_invalidate_vma(vma);
if (XE_WARN_ON(ret))
return ret;
}
}
return ret;
}
static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo,
struct ttm_resource *new_res)
{
struct dma_buf_attachment *attach = ttm_bo->base.import_attach;
struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt,
ttm);
struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
bool device_unplugged = drm_dev_is_unplugged(&xe->drm);
struct sg_table *sg;
xe_assert(xe, attach);
xe_assert(xe, ttm_bo->ttm);
if (device_unplugged && new_res->mem_type == XE_PL_SYSTEM &&
ttm_bo->sg) {
dma_resv_wait_timeout(ttm_bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
false, MAX_SCHEDULE_TIMEOUT);
dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
ttm_bo->sg = NULL;
}
if (new_res->mem_type == XE_PL_SYSTEM)
goto out;
if (ttm_bo->sg) {
dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
ttm_bo->sg = NULL;
}
sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
if (IS_ERR(sg))
return PTR_ERR(sg);
ttm_bo->sg = sg;
xe_tt->sg = sg;
out:
ttm_bo_move_null(ttm_bo, new_res);
return 0;
}
static int xe_bo_move_notify(struct xe_bo *bo,
const struct ttm_operation_ctx *ctx)
{
struct ttm_buffer_object *ttm_bo = &bo->ttm;
struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
struct ttm_resource *old_mem = ttm_bo->resource;
u32 old_mem_type = old_mem ? old_mem->mem_type : XE_PL_SYSTEM;
int ret;
if (xe_bo_is_pinned(bo))
return -EINVAL;
xe_bo_vunmap(bo);
ret = xe_bo_trigger_rebind(xe, bo, ctx);
if (ret)
return ret;
if (ttm_bo->base.dma_buf && !ttm_bo->base.import_attach)
dma_buf_invalidate_mappings(ttm_bo->base.dma_buf);
if (mem_type_is_vram(old_mem_type)) {
mutex_lock(&xe->mem_access.vram_userfault.lock);
if (!list_empty(&bo->vram_userfault_link))
list_del_init(&bo->vram_userfault_link);
mutex_unlock(&xe->mem_access.vram_userfault.lock);
}
return 0;
}
static void xe_bo_set_purgeable_shrinker(struct xe_bo *bo,
enum xe_madv_purgeable_state new_state)
{
struct ttm_buffer_object *ttm_bo = &bo->ttm;
struct ttm_tt *tt = ttm_bo->ttm;
struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
struct xe_ttm_tt *xe_tt;
long tt_pages;
xe_bo_assert_held(bo);
if (!tt || !ttm_tt_is_populated(tt))
return;
xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
tt_pages = xe_bo_is_pinned(bo) ? 0 : tt->num_pages;
if (!xe_tt->purgeable && new_state == XE_MADV_PURGEABLE_DONTNEED) {
xe_tt->purgeable = true;
xe_shrinker_mod_pages(xe->mem.shrinker, -tt_pages, tt_pages);
} else if (xe_tt->purgeable && new_state == XE_MADV_PURGEABLE_WILLNEED) {
xe_tt->purgeable = false;
xe_shrinker_mod_pages(xe->mem.shrinker, tt_pages, -tt_pages);
}
}
void xe_bo_set_purgeable_state(struct xe_bo *bo,
enum xe_madv_purgeable_state new_state)
{
struct xe_device *xe = xe_bo_device(bo);
xe_bo_assert_held(bo);
xe_assert(xe, new_state == XE_MADV_PURGEABLE_WILLNEED ||
new_state == XE_MADV_PURGEABLE_DONTNEED ||
new_state == XE_MADV_PURGEABLE_PURGED);
xe_assert(xe, !(bo->purgeable.state == XE_MADV_PURGEABLE_PURGED &&
new_state != XE_MADV_PURGEABLE_PURGED));
bo->purgeable.state = new_state;
xe_bo_set_purgeable_shrinker(bo, new_state);
}
int xe_ttm_bo_purge(struct ttm_buffer_object *ttm_bo, struct ttm_operation_ctx *ctx)
{
struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
struct ttm_placement place = {};
int ret;
xe_bo_assert_held(bo);
if (!xe_bo_madv_is_dontneed(bo))
return 0;
ret = xe_bo_move_notify(bo, ctx);
if (ret)
return ret;
ret = ttm_bo_validate(ttm_bo, &place, ctx);
if (ret)
return ret;
xe_bo_set_purgeable_state(bo, XE_MADV_PURGEABLE_PURGED);
return 0;
}
static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
struct ttm_operation_ctx *ctx,
struct ttm_resource *new_mem,
struct ttm_place *hop)
{
struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
struct ttm_resource *old_mem = ttm_bo->resource;
u32 old_mem_type = old_mem ? old_mem->mem_type : XE_PL_SYSTEM;
struct ttm_tt *ttm = ttm_bo->ttm;
struct xe_migrate *migrate = NULL;
struct dma_fence *fence;
bool move_lacks_source;
bool tt_has_data;
bool needs_clear;
bool handle_system_ccs = (!IS_DGFX(xe) && xe_bo_needs_ccs_pages(bo) &&
ttm && ttm_tt_is_populated(ttm)) ? true : false;
int ret = 0;
if (evict && xe_bo_madv_is_dontneed(bo)) {
ret = xe_ttm_bo_purge(ttm_bo, ctx);
if (ret)
return ret;
ttm_resource_free(ttm_bo, &new_mem);
return 0;
}
if ((!old_mem && ttm) && !handle_system_ccs) {
if (new_mem->mem_type == XE_PL_TT)
ret = xe_tt_map_sg(xe, ttm);
if (!ret)
ttm_bo_move_null(ttm_bo, new_mem);
goto out;
}
if (ttm_bo->type == ttm_bo_type_sg) {
if (new_mem->mem_type == XE_PL_SYSTEM)
ret = xe_bo_move_notify(bo, ctx);
if (!ret)
ret = xe_bo_move_dmabuf(ttm_bo, new_mem);
return ret;
}
tt_has_data = ttm && (ttm_tt_is_populated(ttm) || ttm_tt_is_swapped(ttm));
move_lacks_source = !old_mem || (handle_system_ccs ? (!bo->ccs_cleared) :
(!mem_type_is_vram(old_mem_type) && !tt_has_data));
needs_clear = (ttm && ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC) ||
(!ttm && ttm_bo->type == ttm_bo_type_device);
if (new_mem->mem_type == XE_PL_TT) {
ret = xe_tt_map_sg(xe, ttm);
if (ret)
goto out;
}
if ((move_lacks_source && !needs_clear)) {
ttm_bo_move_null(ttm_bo, new_mem);
goto out;
}
if (!move_lacks_source && (bo->flags & XE_BO_FLAG_CPU_ADDR_MIRROR) &&
new_mem->mem_type == XE_PL_SYSTEM) {
ret = xe_svm_bo_evict(bo);
if (!ret) {
drm_dbg(&xe->drm, "Evict system allocator BO success\n");
ttm_bo_move_null(ttm_bo, new_mem);
} else {
drm_dbg(&xe->drm, "Evict system allocator BO failed=%pe\n",
ERR_PTR(ret));
if (ret == -EBUSY)
ret = -ENOSPC;
}
goto out;
}
if (old_mem_type == XE_PL_SYSTEM && new_mem->mem_type == XE_PL_TT && !handle_system_ccs) {
ttm_bo_move_null(ttm_bo, new_mem);
goto out;
}
if (old_mem_type == XE_PL_TT &&
new_mem->mem_type == XE_PL_TT) {
ttm_bo_move_null(ttm_bo, new_mem);
goto out;
}
if (!move_lacks_source && !xe_bo_is_pinned(bo)) {
ret = xe_bo_move_notify(bo, ctx);
if (ret)
goto out;
}
if (old_mem_type == XE_PL_TT &&
new_mem->mem_type == XE_PL_SYSTEM) {
long timeout = dma_resv_wait_timeout(ttm_bo->base.resv,
DMA_RESV_USAGE_BOOKKEEP,
false,
MAX_SCHEDULE_TIMEOUT);
if (timeout < 0) {
ret = timeout;
goto out;
}
if (!handle_system_ccs) {
ttm_bo_move_null(ttm_bo, new_mem);
goto out;
}
}
if (!move_lacks_source &&
((old_mem_type == XE_PL_SYSTEM && resource_is_vram(new_mem)) ||
(mem_type_is_vram(old_mem_type) &&
new_mem->mem_type == XE_PL_SYSTEM))) {
hop->fpfn = 0;
hop->lpfn = 0;
hop->mem_type = XE_PL_TT;
hop->flags = TTM_PL_FLAG_TEMPORARY;
ret = -EMULTIHOP;
goto out;
}
if (bo->tile)
migrate = bo->tile->migrate;
else if (resource_is_vram(new_mem))
migrate = mem_type_to_migrate(xe, new_mem->mem_type);
else if (mem_type_is_vram(old_mem_type))
migrate = mem_type_to_migrate(xe, old_mem_type);
else
migrate = xe->tiles[0].migrate;
xe_assert(xe, migrate);
trace_xe_bo_move(bo, new_mem->mem_type, old_mem_type, move_lacks_source);
if (xe_rpm_reclaim_safe(xe)) {
xe_pm_runtime_get(xe);
} else {
drm_WARN_ON(&xe->drm, handle_system_ccs);
xe_pm_runtime_get_noresume(xe);
}
if (IS_VF_CCS_READY(xe) &&
((move_lacks_source && new_mem->mem_type == XE_PL_TT) ||
(old_mem_type == XE_PL_SYSTEM && new_mem->mem_type == XE_PL_TT)) &&
handle_system_ccs) {
ret = xe_sriov_vf_ccs_attach_bo(bo, new_mem);
if (ret) {
xe_pm_runtime_put(xe);
goto out;
}
}
if (move_lacks_source) {
u32 flags = 0;
if (mem_type_is_vram(new_mem->mem_type))
flags |= XE_MIGRATE_CLEAR_FLAG_FULL;
else if (handle_system_ccs)
flags |= XE_MIGRATE_CLEAR_FLAG_CCS_DATA;
fence = xe_migrate_clear(migrate, bo, new_mem, flags);
} else {
fence = xe_migrate_copy(migrate, bo, bo, old_mem, new_mem,
handle_system_ccs);
}
if (IS_ERR(fence)) {
ret = PTR_ERR(fence);
xe_pm_runtime_put(xe);
goto out;
}
if (!move_lacks_source) {
ret = ttm_bo_move_accel_cleanup(ttm_bo, fence, evict, true,
new_mem);
if (ret) {
dma_fence_wait(fence, false);
ttm_bo_move_null(ttm_bo, new_mem);
ret = 0;
}
} else {
dma_resv_add_fence(ttm_bo->base.resv, fence,
DMA_RESV_USAGE_KERNEL);
ttm_bo_move_null(ttm_bo, new_mem);
}
if (IS_VF_CCS_READY(xe) && old_mem_type == XE_PL_TT &&
new_mem->mem_type == XE_PL_SYSTEM) {
dma_fence_wait(fence, false);
xe_sriov_vf_ccs_detach_bo(bo);
}
dma_fence_put(fence);
xe_pm_runtime_put(xe);
out:
if ((!ttm_bo->resource || ttm_bo->resource->mem_type == XE_PL_SYSTEM) &&
ttm_bo->ttm) {
long timeout = dma_resv_wait_timeout(ttm_bo->base.resv,
DMA_RESV_USAGE_KERNEL,
false,
MAX_SCHEDULE_TIMEOUT);
if (timeout < 0)
ret = timeout;
if (IS_VF_CCS_READY(xe))
xe_sriov_vf_ccs_detach_bo(bo);
xe_tt_unmap_sg(xe, ttm_bo->ttm);
}
return ret;
}
static long xe_bo_shrink_purge(struct ttm_operation_ctx *ctx,
struct ttm_buffer_object *bo,
unsigned long *scanned)
{
struct xe_device *xe = ttm_to_xe_device(bo->bdev);
struct ttm_tt *tt = bo->ttm;
long lret;
if (bo->resource->mem_type != XE_PL_SYSTEM) {
struct ttm_resource *new_resource;
lret = ttm_bo_wait_ctx(bo, ctx);
if (lret)
return lret;
lret = ttm_bo_mem_space(bo, &sys_placement, &new_resource, ctx);
if (lret)
return lret;
xe_tt_unmap_sg(xe, bo->ttm);
ttm_bo_move_null(bo, new_resource);
}
*scanned += bo->ttm->num_pages;
lret = ttm_bo_shrink(ctx, bo, (struct ttm_bo_shrink_flags)
{.purge = true,
.writeback = false,
.allow_move = false});
if (lret > 0) {
xe_ttm_tt_account_subtract(xe, bo->ttm);
update_global_total_pages(bo->bdev, -(long)tt->num_pages);
}
return lret;
}
static bool
xe_bo_eviction_valuable(struct ttm_buffer_object *bo, const struct ttm_place *place)
{
struct drm_gpuvm_bo *vm_bo;
if (!ttm_bo_eviction_valuable(bo, place))
return false;
if (!xe_bo_is_xe_bo(bo))
return true;
drm_gem_for_each_gpuvm_bo(vm_bo, &bo->base) {
if (xe_vm_is_validating(gpuvm_to_vm(vm_bo->vm)))
return false;
}
return true;
}
long xe_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo,
const struct xe_bo_shrink_flags flags,
unsigned long *scanned)
{
struct ttm_tt *tt = bo->ttm;
struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
struct ttm_place place = {.mem_type = bo->resource->mem_type};
struct xe_bo *xe_bo = ttm_to_xe_bo(bo);
struct xe_device *xe = ttm_to_xe_device(bo->bdev);
bool needs_rpm;
long lret = 0L;
if (!(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE) ||
(flags.purge && !xe_tt->purgeable))
return -EBUSY;
if (!xe_bo_eviction_valuable(bo, &place))
return -EBUSY;
if (!xe_bo_is_xe_bo(bo) || !xe_bo_get_unless_zero(xe_bo))
return xe_bo_shrink_purge(ctx, bo, scanned);
if (xe_tt->purgeable) {
if (bo->resource->mem_type != XE_PL_SYSTEM)
lret = xe_bo_move_notify(xe_bo, ctx);
if (!lret)
lret = xe_bo_shrink_purge(ctx, bo, scanned);
if (lret > 0 && xe_bo_madv_is_dontneed(xe_bo))
xe_bo_set_purgeable_state(xe_bo,
XE_MADV_PURGEABLE_PURGED);
goto out_unref;
}
needs_rpm = (!IS_DGFX(xe) && bo->resource->mem_type != XE_PL_SYSTEM &&
xe_bo_needs_ccs_pages(xe_bo));
if (needs_rpm && !xe_pm_runtime_get_if_active(xe))
goto out_unref;
*scanned += tt->num_pages;
lret = ttm_bo_shrink(ctx, bo, (struct ttm_bo_shrink_flags)
{.purge = false,
.writeback = flags.writeback,
.allow_move = true});
if (needs_rpm)
xe_pm_runtime_put(xe);
if (lret > 0) {
xe_ttm_tt_account_subtract(xe, tt);
update_global_total_pages(bo->bdev, -(long)tt->num_pages);
}
out_unref:
xe_bo_put(xe_bo);
return lret;
}
int xe_bo_notifier_prepare_pinned(struct xe_bo *bo)
{
struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
struct xe_validation_ctx ctx;
struct drm_exec exec;
struct xe_bo *backup;
int ret = 0;
xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.exclusive = true}, ret) {
ret = drm_exec_lock_obj(&exec, &bo->ttm.base);
drm_exec_retry_on_contention(&exec);
xe_assert(xe, !ret);
xe_assert(xe, !bo->backup_obj);
if (!xe_bo_is_pinned(bo))
break;
if (!xe_bo_is_vram(bo))
break;
if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE)
break;
backup = xe_bo_init_locked(xe, NULL, NULL, bo->ttm.base.resv, NULL, xe_bo_size(bo),
DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel,
XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS |
XE_BO_FLAG_PINNED, NULL, &exec);
if (IS_ERR(backup)) {
drm_exec_retry_on_contention(&exec);
ret = PTR_ERR(backup);
xe_validation_retry_on_oom(&ctx, &ret);
break;
}
backup->parent_obj = xe_bo_get(bo);
xe_bo_pin_account(backup);
bo->backup_obj = backup;
}
return ret;
}
int xe_bo_notifier_unprepare_pinned(struct xe_bo *bo)
{
xe_bo_lock(bo, false);
if (bo->backup_obj) {
xe_bo_unpin_account(bo->backup_obj);
xe_bo_put(bo->backup_obj);
bo->backup_obj = NULL;
}
xe_bo_unlock(bo);
return 0;
}
static int xe_bo_evict_pinned_copy(struct xe_bo *bo, struct xe_bo *backup)
{
struct xe_device *xe = xe_bo_device(bo);
bool unmap = false;
int ret = 0;
if (xe_bo_is_user(bo) || (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE)) {
struct xe_migrate *migrate;
struct dma_fence *fence;
if (bo->tile)
migrate = bo->tile->migrate;
else
migrate = mem_type_to_migrate(xe, bo->ttm.resource->mem_type);
xe_assert(xe, bo->ttm.base.resv == backup->ttm.base.resv);
ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
if (ret)
goto out_backup;
fence = xe_migrate_copy(migrate, bo, backup, bo->ttm.resource,
backup->ttm.resource, false);
if (IS_ERR(fence)) {
ret = PTR_ERR(fence);
goto out_backup;
}
dma_resv_add_fence(bo->ttm.base.resv, fence,
DMA_RESV_USAGE_KERNEL);
dma_fence_put(fence);
} else {
ret = xe_bo_vmap(backup);
if (ret)
goto out_backup;
if (iosys_map_is_null(&bo->vmap)) {
ret = xe_bo_vmap(bo);
if (ret)
goto out_vunmap;
unmap = true;
}
xe_map_memcpy_from(xe, backup->vmap.vaddr, &bo->vmap, 0,
xe_bo_size(bo));
}
if (!bo->backup_obj)
bo->backup_obj = backup;
out_vunmap:
xe_bo_vunmap(backup);
out_backup:
if (unmap)
xe_bo_vunmap(bo);
return ret;
}
int xe_bo_evict_pinned(struct xe_bo *bo)
{
struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
struct xe_validation_ctx ctx;
struct drm_exec exec;
struct xe_bo *backup = bo->backup_obj;
bool backup_created = false;
int ret = 0;
xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.exclusive = true}, ret) {
ret = drm_exec_lock_obj(&exec, &bo->ttm.base);
drm_exec_retry_on_contention(&exec);
xe_assert(xe, !ret);
if (WARN_ON(!bo->ttm.resource)) {
ret = -EINVAL;
break;
}
if (WARN_ON(!xe_bo_is_pinned(bo))) {
ret = -EINVAL;
break;
}
if (!xe_bo_is_vram(bo))
break;
if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE)
break;
if (!backup) {
backup = xe_bo_init_locked(xe, NULL, NULL, bo->ttm.base.resv, NULL,
xe_bo_size(bo),
DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel,
XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS |
XE_BO_FLAG_PINNED, NULL, &exec);
if (IS_ERR(backup)) {
drm_exec_retry_on_contention(&exec);
ret = PTR_ERR(backup);
xe_validation_retry_on_oom(&ctx, &ret);
break;
}
backup->parent_obj = xe_bo_get(bo);
backup_created = true;
}
ret = xe_bo_evict_pinned_copy(bo, backup);
}
if (ret && backup_created)
xe_bo_put(backup);
return ret;
}
int xe_bo_restore_pinned(struct xe_bo *bo)
{
struct ttm_operation_ctx ctx = {
.interruptible = false,
.gfp_retry_mayfail = false,
};
struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
struct xe_bo *backup = bo->backup_obj;
bool unmap = false;
int ret;
if (!backup)
return 0;
xe_bo_lock(bo, false);
if (!xe_bo_is_pinned(backup)) {
ret = ttm_bo_validate(&backup->ttm, &backup->placement, &ctx);
if (ret)
goto out_unlock_bo;
}
if (xe_bo_is_user(bo) || (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE)) {
struct xe_migrate *migrate;
struct dma_fence *fence;
if (bo->tile)
migrate = bo->tile->migrate;
else
migrate = mem_type_to_migrate(xe, bo->ttm.resource->mem_type);
ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
if (ret)
goto out_unlock_bo;
fence = xe_migrate_copy(migrate, backup, bo,
backup->ttm.resource, bo->ttm.resource,
false);
if (IS_ERR(fence)) {
ret = PTR_ERR(fence);
goto out_unlock_bo;
}
dma_resv_add_fence(bo->ttm.base.resv, fence,
DMA_RESV_USAGE_KERNEL);
dma_fence_put(fence);
} else {
ret = xe_bo_vmap(backup);
if (ret)
goto out_unlock_bo;
if (iosys_map_is_null(&bo->vmap)) {
ret = xe_bo_vmap(bo);
if (ret)
goto out_backup;
unmap = true;
}
xe_map_memcpy_to(xe, &bo->vmap, 0, backup->vmap.vaddr,
xe_bo_size(bo));
}
bo->backup_obj = NULL;
out_backup:
xe_bo_vunmap(backup);
if (!bo->backup_obj) {
if (xe_bo_is_pinned(backup))
xe_bo_unpin_account(backup);
xe_bo_put(backup);
}
out_unlock_bo:
if (unmap)
xe_bo_vunmap(bo);
xe_bo_unlock(bo);
return ret;
}
int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
{
struct ttm_buffer_object *ttm_bo = &bo->ttm;
struct ttm_tt *tt = ttm_bo->ttm;
if (tt) {
struct xe_ttm_tt *xe_tt = container_of(tt, typeof(*xe_tt), ttm);
if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
dma_buf_unmap_attachment(ttm_bo->base.import_attach,
ttm_bo->sg,
DMA_BIDIRECTIONAL);
ttm_bo->sg = NULL;
xe_tt->sg = NULL;
} else if (xe_tt->sg) {
dma_unmap_sgtable(ttm_to_xe_device(ttm_bo->bdev)->drm.dev,
xe_tt->sg,
DMA_BIDIRECTIONAL, 0);
sg_free_table(xe_tt->sg);
xe_tt->sg = NULL;
}
}
return 0;
}
static unsigned long xe_ttm_io_mem_pfn(struct ttm_buffer_object *ttm_bo,
unsigned long page_offset)
{
struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
struct xe_res_cursor cursor;
struct xe_vram_region *vram;
if (ttm_bo->resource->mem_type == XE_PL_STOLEN)
return xe_ttm_stolen_io_offset(bo, page_offset << PAGE_SHIFT) >> PAGE_SHIFT;
vram = xe_map_resource_to_region(ttm_bo->resource);
xe_res_first(ttm_bo->resource, (u64)page_offset << PAGE_SHIFT, 0, &cursor);
return (vram->io_start + cursor.start) >> PAGE_SHIFT;
}
static void __xe_bo_vunmap(struct xe_bo *bo);
static bool xe_ttm_bo_lock_in_destructor(struct ttm_buffer_object *ttm_bo)
{
struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
bool locked;
xe_assert(xe, !kref_read(&ttm_bo->kref));
spin_lock(&ttm_bo->bdev->lru_lock);
locked = dma_resv_trylock(&ttm_bo->base._resv);
spin_unlock(&ttm_bo->bdev->lru_lock);
xe_assert(xe, locked);
return locked;
}
static void xe_ttm_bo_release_notify(struct ttm_buffer_object *ttm_bo)
{
struct dma_resv_iter cursor;
struct dma_fence *fence;
struct dma_fence *replacement = NULL;
struct xe_bo *bo;
if (!xe_bo_is_xe_bo(ttm_bo))
return;
bo = ttm_to_xe_bo(ttm_bo);
xe_assert(xe_bo_device(bo), !(bo->created && kref_read(&ttm_bo->base.refcount)));
if (!xe_ttm_bo_lock_in_destructor(ttm_bo))
return;
dma_resv_for_each_fence(&cursor, &ttm_bo->base._resv,
DMA_RESV_USAGE_BOOKKEEP, fence) {
if (xe_fence_is_xe_preempt(fence) &&
!dma_fence_is_signaled(fence)) {
if (!replacement)
replacement = dma_fence_get_stub();
dma_resv_replace_fences(&ttm_bo->base._resv,
fence->context,
replacement,
DMA_RESV_USAGE_BOOKKEEP);
}
}
dma_fence_put(replacement);
dma_resv_unlock(&ttm_bo->base._resv);
}
static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo)
{
struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
if (!xe_bo_is_xe_bo(ttm_bo))
return;
if (IS_VF_CCS_READY(ttm_to_xe_device(ttm_bo->bdev)))
xe_sriov_vf_ccs_detach_bo(bo);
if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm,
struct xe_ttm_tt, ttm);
dma_buf_unmap_attachment(ttm_bo->base.import_attach, ttm_bo->sg,
DMA_BIDIRECTIONAL);
ttm_bo->sg = NULL;
xe_tt->sg = NULL;
}
}
static void xe_ttm_bo_swap_notify(struct ttm_buffer_object *ttm_bo)
{
struct ttm_operation_ctx ctx = {
.interruptible = false,
.gfp_retry_mayfail = false,
};
if (ttm_bo->ttm) {
struct xe_ttm_tt *xe_tt =
container_of(ttm_bo->ttm, struct xe_ttm_tt, ttm);
if (xe_tt->purgeable)
xe_ttm_bo_purge(ttm_bo, &ctx);
}
}
static int xe_ttm_access_memory(struct ttm_buffer_object *ttm_bo,
unsigned long offset, void *buf, int len,
int write)
{
struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
struct iosys_map vmap;
struct xe_res_cursor cursor;
struct xe_vram_region *vram;
int bytes_left = len;
int err = 0;
xe_bo_assert_held(bo);
xe_device_assert_mem_access(xe);
if (!mem_type_is_vram(ttm_bo->resource->mem_type))
return -EIO;
if (!xe_bo_is_visible_vram(bo) || len >= SZ_16K) {
struct xe_migrate *migrate =
mem_type_to_migrate(xe, ttm_bo->resource->mem_type);
err = xe_migrate_access_memory(migrate, bo, offset, buf, len,
write);
goto out;
}
vram = xe_map_resource_to_region(ttm_bo->resource);
xe_res_first(ttm_bo->resource, offset & PAGE_MASK,
xe_bo_size(bo) - (offset & PAGE_MASK), &cursor);
do {
unsigned long page_offset = (offset & ~PAGE_MASK);
int byte_count = min((int)(PAGE_SIZE - page_offset), bytes_left);
iosys_map_set_vaddr_iomem(&vmap, (u8 __iomem *)vram->mapping +
cursor.start);
if (write)
xe_map_memcpy_to(xe, &vmap, page_offset, buf, byte_count);
else
xe_map_memcpy_from(xe, buf, &vmap, page_offset, byte_count);
buf += byte_count;
offset += byte_count;
bytes_left -= byte_count;
if (bytes_left)
xe_res_next(&cursor, PAGE_SIZE);
} while (bytes_left);
out:
return err ?: len;
}
const struct ttm_device_funcs xe_ttm_funcs = {
.ttm_tt_create = xe_ttm_tt_create,
.ttm_tt_populate = xe_ttm_tt_populate,
.ttm_tt_unpopulate = xe_ttm_tt_unpopulate,
.ttm_tt_destroy = xe_ttm_tt_destroy,
.evict_flags = xe_evict_flags,
.move = xe_bo_move,
.io_mem_reserve = xe_ttm_io_mem_reserve,
.io_mem_pfn = xe_ttm_io_mem_pfn,
.access_memory = xe_ttm_access_memory,
.release_notify = xe_ttm_bo_release_notify,
.eviction_valuable = xe_bo_eviction_valuable,
.delete_mem_notify = xe_ttm_bo_delete_mem_notify,
.swap_notify = xe_ttm_bo_swap_notify,
};
static void xe_ttm_bo_destroy(struct ttm_buffer_object *ttm_bo)
{
struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
struct xe_tile *tile;
u8 id;
if (bo->ttm.base.import_attach)
drm_prime_gem_destroy(&bo->ttm.base, NULL);
if (bo->dma_buf)
dma_buf_put(bo->dma_buf);
drm_gem_object_release(&bo->ttm.base);
xe_assert(xe, list_empty(&ttm_bo->base.gpuva.list));
for_each_tile(tile, xe, id)
if (bo->ggtt_node[id])
xe_ggtt_remove_bo(tile->mem.ggtt, bo);
#ifdef CONFIG_PROC_FS
if (bo->client)
xe_drm_client_remove_bo(bo);
#endif
if (bo->vm && (xe_bo_is_user(bo) || bo->flags & XE_BO_FLAG_PAGETABLE))
xe_vm_put(bo->vm);
if (bo->parent_obj)
xe_bo_put(bo->parent_obj);
mutex_lock(&xe->mem_access.vram_userfault.lock);
if (!list_empty(&bo->vram_userfault_link))
list_del(&bo->vram_userfault_link);
mutex_unlock(&xe->mem_access.vram_userfault.lock);
kfree(bo);
}
static void xe_gem_object_free(struct drm_gem_object *obj)
{
__xe_bo_vunmap(gem_to_xe_bo(obj));
ttm_bo_fini(container_of(obj, struct ttm_buffer_object, base));
}
static void xe_gem_object_close(struct drm_gem_object *obj,
struct drm_file *file_priv)
{
struct xe_bo *bo = gem_to_xe_bo(obj);
if (bo->vm && !xe_vm_in_fault_mode(bo->vm)) {
xe_assert(xe_bo_device(bo), xe_bo_is_user(bo));
xe_bo_lock(bo, false);
ttm_bo_set_bulk_move(&bo->ttm, NULL);
xe_bo_unlock(bo);
}
}
static bool should_migrate_to_smem(struct xe_bo *bo)
{
return bo->attr.atomic_access == DRM_XE_ATOMIC_GLOBAL ||
bo->attr.atomic_access == DRM_XE_ATOMIC_CPU;
}
static int xe_bo_wait_usage_kernel(struct xe_bo *bo, struct ttm_operation_ctx *ctx)
{
long lerr;
if (ctx->no_wait_gpu)
return dma_resv_test_signaled(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL) ?
0 : -EBUSY;
lerr = dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
ctx->interruptible, MAX_SCHEDULE_TIMEOUT);
if (lerr < 0)
return lerr;
if (lerr == 0)
return -EBUSY;
return 0;
}
static int xe_bo_fault_migrate(struct xe_bo *bo, struct ttm_operation_ctx *ctx,
struct drm_exec *exec)
{
struct ttm_buffer_object *tbo = &bo->ttm;
int err = 0;
if (ttm_manager_type(tbo->bdev, tbo->resource->mem_type)->use_tt) {
err = xe_bo_wait_usage_kernel(bo, ctx);
if (!err)
err = ttm_bo_populate(&bo->ttm, ctx);
} else if (should_migrate_to_smem(bo)) {
xe_assert(xe_bo_device(bo), bo->flags & XE_BO_FLAG_SYSTEM);
err = xe_bo_migrate(bo, XE_PL_TT, ctx, exec);
}
return err;
}
static vm_fault_t __xe_bo_cpu_fault(struct vm_fault *vmf, struct xe_device *xe, struct xe_bo *bo)
{
vm_fault_t ret;
trace_xe_bo_cpu_fault(bo);
ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
TTM_BO_VM_NUM_PREFAULT);
xe_assert(xe, ret != VM_FAULT_RETRY);
if (ret == VM_FAULT_NOPAGE &&
mem_type_is_vram(bo->ttm.resource->mem_type)) {
mutex_lock(&xe->mem_access.vram_userfault.lock);
if (list_empty(&bo->vram_userfault_link))
list_add(&bo->vram_userfault_link,
&xe->mem_access.vram_userfault.list);
mutex_unlock(&xe->mem_access.vram_userfault.lock);
}
return ret;
}
static vm_fault_t xe_err_to_fault_t(int err)
{
switch (err) {
case 0:
case -EINTR:
case -ERESTARTSYS:
case -EAGAIN:
return VM_FAULT_NOPAGE;
case -ENOMEM:
case -ENOSPC:
return VM_FAULT_OOM;
default:
break;
}
return VM_FAULT_SIGBUS;
}
static vm_fault_t xe_bo_cpu_fault_fastpath(struct vm_fault *vmf, struct xe_device *xe,
struct xe_bo *bo, bool needs_rpm)
{
struct ttm_buffer_object *tbo = &bo->ttm;
vm_fault_t ret = VM_FAULT_RETRY;
struct xe_validation_ctx ctx;
struct ttm_operation_ctx tctx = {
.interruptible = true,
.no_wait_gpu = true,
.gfp_retry_mayfail = true,
};
int err;
if (needs_rpm && !xe_pm_runtime_get_if_active(xe))
return VM_FAULT_RETRY;
err = xe_validation_ctx_init(&ctx, &xe->val, NULL,
(struct xe_val_flags) {
.interruptible = true,
.no_block = true
});
if (err)
goto out_pm;
if (!dma_resv_trylock(tbo->base.resv))
goto out_validation;
if (xe_bo_madv_is_dontneed(bo) || xe_bo_is_purged(bo)) {
ret = VM_FAULT_SIGBUS;
goto out_unlock;
}
if (xe_ttm_bo_is_imported(tbo)) {
ret = VM_FAULT_SIGBUS;
drm_dbg(&xe->drm, "CPU trying to access an imported buffer object.\n");
goto out_unlock;
}
err = xe_bo_fault_migrate(bo, &tctx, NULL);
if (err) {
if (err != -ENOMEM && err != -ENOSPC && err != -EBUSY)
ret = xe_err_to_fault_t(err);
goto out_unlock;
}
if (dma_resv_test_signaled(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL))
ret = __xe_bo_cpu_fault(vmf, xe, bo);
out_unlock:
dma_resv_unlock(tbo->base.resv);
out_validation:
xe_validation_ctx_fini(&ctx);
out_pm:
if (needs_rpm)
xe_pm_runtime_put(xe);
return ret;
}
static vm_fault_t xe_bo_cpu_fault(struct vm_fault *vmf)
{
struct ttm_buffer_object *tbo = vmf->vma->vm_private_data;
struct drm_device *ddev = tbo->base.dev;
struct xe_device *xe = to_xe_device(ddev);
struct xe_bo *bo = ttm_to_xe_bo(tbo);
bool needs_rpm = bo->flags & XE_BO_FLAG_VRAM_MASK;
bool retry_after_wait = false;
struct xe_validation_ctx ctx;
struct drm_exec exec;
vm_fault_t ret;
int err = 0;
int idx;
if (xe_device_wedged(xe) || !drm_dev_enter(&xe->drm, &idx))
return ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
ret = xe_bo_cpu_fault_fastpath(vmf, xe, bo, needs_rpm);
if (ret != VM_FAULT_RETRY)
goto out;
if (fault_flag_allow_retry_first(vmf->flags)) {
if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
goto out;
retry_after_wait = true;
xe_bo_get(bo);
mmap_read_unlock(vmf->vma->vm_mm);
} else {
ret = VM_FAULT_NOPAGE;
}
if (needs_rpm)
xe_pm_runtime_get(xe);
xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = true},
err) {
struct ttm_operation_ctx tctx = {
.interruptible = true,
.no_wait_gpu = false,
.gfp_retry_mayfail = retry_after_wait,
};
err = drm_exec_lock_obj(&exec, &tbo->base);
drm_exec_retry_on_contention(&exec);
if (err)
break;
if (xe_bo_madv_is_dontneed(bo) || xe_bo_is_purged(bo)) {
err = -EFAULT;
break;
}
if (xe_ttm_bo_is_imported(tbo)) {
err = -EFAULT;
drm_dbg(&xe->drm, "CPU trying to access an imported buffer object.\n");
break;
}
err = xe_bo_fault_migrate(bo, &tctx, &exec);
if (err) {
drm_exec_retry_on_contention(&exec);
xe_validation_retry_on_oom(&ctx, &err);
break;
}
err = xe_bo_wait_usage_kernel(bo, &tctx);
if (err)
break;
if (!retry_after_wait)
ret = __xe_bo_cpu_fault(vmf, xe, bo);
}
if (err && !retry_after_wait)
ret = xe_err_to_fault_t(err);
if (needs_rpm)
xe_pm_runtime_put(xe);
if (retry_after_wait)
xe_bo_put(bo);
out:
drm_dev_exit(idx);
return ret;
}
static int xe_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
void *buf, int len, int write)
{
struct ttm_buffer_object *ttm_bo = vma->vm_private_data;
struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
struct xe_device *xe = xe_bo_device(bo);
guard(xe_pm_runtime)(xe);
return ttm_bo_vm_access(vma, addr, buf, len, write);
}
int xe_bo_read(struct xe_bo *bo, u64 offset, void *dst, int size)
{
int ret;
ret = ttm_bo_access(&bo->ttm, offset, dst, size, 0);
if (ret >= 0 && ret != size)
ret = -EIO;
else if (ret == size)
ret = 0;
return ret;
}
static const struct vm_operations_struct xe_gem_vm_ops = {
.fault = xe_bo_cpu_fault,
.open = ttm_bo_vm_open,
.close = ttm_bo_vm_close,
.access = xe_bo_vm_access,
};
static int xe_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
{
struct xe_bo *bo = gem_to_xe_bo(obj);
int err = 0;
err = xe_bo_lock(bo, true);
if (err)
return err;
if (xe_bo_madv_is_dontneed(bo))
err = -EBUSY;
else if (xe_bo_is_purged(bo))
err = -EINVAL;
xe_bo_unlock(bo);
if (err)
return err;
return drm_gem_ttm_mmap(obj, vma);
}
static const struct drm_gem_object_funcs xe_gem_object_funcs = {
.free = xe_gem_object_free,
.close = xe_gem_object_close,
.mmap = xe_gem_object_mmap,
.export = xe_gem_prime_export,
.vm_ops = &xe_gem_vm_ops,
};
struct xe_bo *xe_bo_alloc(void)
{
struct xe_bo *bo = kzalloc_obj(*bo);
if (!bo)
return ERR_PTR(-ENOMEM);
return bo;
}
void xe_bo_free(struct xe_bo *bo)
{
kfree(bo);
}
struct xe_bo *xe_bo_init_locked(struct xe_device *xe, struct xe_bo *bo,
struct xe_tile *tile, struct dma_resv *resv,
struct ttm_lru_bulk_move *bulk, size_t size,
u16 cpu_caching, enum ttm_bo_type type,
u32 flags, struct dma_buf *dma_buf,
struct drm_exec *exec)
{
struct ttm_operation_ctx ctx = {
.interruptible = true,
.no_wait_gpu = false,
.gfp_retry_mayfail = true,
};
struct ttm_placement *placement;
uint32_t alignment;
size_t aligned_size;
int err;
xe_assert(xe, !tile || type == ttm_bo_type_kernel);
if (XE_WARN_ON(!size)) {
xe_bo_free(bo);
return ERR_PTR(-EINVAL);
}
if ((flags & XE_BO_FLAG_GGTT_ALL) && !(flags & XE_BO_FLAG_GGTT)) {
xe_bo_free(bo);
return ERR_PTR(-EINVAL);
}
if (flags & (XE_BO_FLAG_VRAM_MASK | XE_BO_FLAG_STOLEN) &&
!(flags & XE_BO_FLAG_IGNORE_MIN_PAGE_SIZE) &&
((xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) ||
(flags & (XE_BO_FLAG_NEEDS_64K | XE_BO_FLAG_NEEDS_2M |
XE_BO_FLAG_NEEDS_1G)))) {
size_t align;
if (flags & XE_BO_FLAG_NEEDS_1G)
align = SZ_1G;
else if (flags & XE_BO_FLAG_NEEDS_2M)
align = SZ_2M;
else
align = SZ_64K;
aligned_size = ALIGN(size, align);
if (type != ttm_bo_type_device)
size = ALIGN(size, align);
flags |= XE_BO_FLAG_INTERNAL_64K;
alignment = align >> PAGE_SHIFT;
} else {
aligned_size = ALIGN(size, SZ_4K);
flags &= ~XE_BO_FLAG_INTERNAL_64K;
alignment = SZ_4K >> PAGE_SHIFT;
}
if (type == ttm_bo_type_device && aligned_size != size) {
xe_bo_free(bo);
return ERR_PTR(-EINVAL);
}
if (!bo) {
bo = xe_bo_alloc();
if (IS_ERR(bo))
return bo;
}
bo->ccs_cleared = false;
bo->tile = tile;
bo->flags = flags;
bo->cpu_caching = cpu_caching;
bo->ttm.base.funcs = &xe_gem_object_funcs;
bo->ttm.priority = XE_BO_PRIORITY_NORMAL;
INIT_LIST_HEAD(&bo->pinned_link);
#ifdef CONFIG_PROC_FS
INIT_LIST_HEAD(&bo->client_link);
#endif
INIT_LIST_HEAD(&bo->vram_userfault_link);
bo->purgeable.state = XE_MADV_PURGEABLE_WILLNEED;
drm_gem_private_object_init(&xe->drm, &bo->ttm.base, size);
if (resv) {
ctx.allow_res_evict = !(flags & XE_BO_FLAG_NO_RESV_EVICT);
ctx.resv = resv;
}
xe_validation_assert_exec(xe, exec, &bo->ttm.base);
if (!(flags & XE_BO_FLAG_FIXED_PLACEMENT)) {
err = __xe_bo_placement_for_flags(xe, bo, bo->flags, type);
if (WARN_ON(err)) {
xe_ttm_bo_destroy(&bo->ttm);
return ERR_PTR(err);
}
}
placement = (type == ttm_bo_type_sg ||
bo->flags & XE_BO_FLAG_DEFER_BACKING) ? &sys_placement :
&bo->placement;
if (dma_buf) {
get_dma_buf(dma_buf);
bo->dma_buf = dma_buf;
}
err = ttm_bo_init_reserved(&xe->ttm, &bo->ttm, type,
placement, alignment,
&ctx, NULL, resv, xe_ttm_bo_destroy);
if (err)
return ERR_PTR(err);
if (type == ttm_bo_type_kernel) {
long timeout = dma_resv_wait_timeout(bo->ttm.base.resv,
DMA_RESV_USAGE_KERNEL,
ctx.interruptible,
MAX_SCHEDULE_TIMEOUT);
if (timeout < 0) {
if (!resv)
dma_resv_unlock(bo->ttm.base.resv);
xe_bo_put(bo);
return ERR_PTR(timeout);
}
}
bo->created = true;
if (bulk)
ttm_bo_set_bulk_move(&bo->ttm, bulk);
else
ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
return bo;
}
static int __xe_bo_fixed_placement(struct xe_device *xe,
struct xe_bo *bo, enum ttm_bo_type type,
u32 flags,
u64 start, u64 end, u64 size)
{
struct ttm_place *place = bo->placements;
u32 vram_flag, vram_stolen_flags;
xe_assert(xe, !IS_SRIOV_VF(xe) || !(bo->flags & XE_BO_FLAG_GGTT));
if (flags & (XE_BO_FLAG_USER | XE_BO_FLAG_SYSTEM))
return -EINVAL;
vram_flag = flags & XE_BO_FLAG_VRAM_MASK;
vram_stolen_flags = (flags & (XE_BO_FLAG_STOLEN)) | vram_flag;
if (hweight32(vram_stolen_flags) > 1)
return -EINVAL;
place->flags = TTM_PL_FLAG_CONTIGUOUS;
place->fpfn = start >> PAGE_SHIFT;
place->lpfn = end >> PAGE_SHIFT;
if (flags & XE_BO_FLAG_STOLEN)
place->mem_type = XE_PL_STOLEN;
else
place->mem_type = bo_vram_flags_to_vram_placement(xe, flags, vram_flag, type);
bo->placement = (struct ttm_placement) {
.num_placement = 1,
.placement = place,
};
return 0;
}
static struct xe_bo *
__xe_bo_create_locked(struct xe_device *xe,
struct xe_tile *tile, struct xe_vm *vm,
size_t size, u64 start, u64 end,
u16 cpu_caching, enum ttm_bo_type type, u32 flags,
u64 alignment, struct drm_exec *exec)
{
struct xe_bo *bo = NULL;
int err;
if (vm)
xe_vm_assert_held(vm);
if (start || end != ~0ULL) {
bo = xe_bo_alloc();
if (IS_ERR(bo))
return bo;
flags |= XE_BO_FLAG_FIXED_PLACEMENT;
err = __xe_bo_fixed_placement(xe, bo, type, flags, start, end, size);
if (err) {
xe_bo_free(bo);
return ERR_PTR(err);
}
}
bo = xe_bo_init_locked(xe, bo, tile, vm ? xe_vm_resv(vm) : NULL,
vm && !xe_vm_in_fault_mode(vm) &&
flags & XE_BO_FLAG_USER ?
&vm->lru_bulk_move : NULL, size,
cpu_caching, type, flags, NULL, exec);
if (IS_ERR(bo))
return bo;
bo->min_align = alignment;
if (vm && (xe_bo_is_user(bo) || bo->flags & XE_BO_FLAG_PAGETABLE))
xe_vm_get(vm);
bo->vm = vm;
if (bo->flags & XE_BO_FLAG_GGTT) {
struct xe_tile *t;
u8 id;
if (!(bo->flags & XE_BO_FLAG_GGTT_ALL)) {
if (!tile && flags & XE_BO_FLAG_STOLEN)
tile = xe_device_get_root_tile(xe);
xe_assert(xe, tile);
}
for_each_tile(t, xe, id) {
if (t != tile && !(bo->flags & XE_BO_FLAG_GGTTx(t)))
continue;
if (flags & XE_BO_FLAG_FIXED_PLACEMENT) {
err = xe_ggtt_insert_bo_at(t->mem.ggtt, bo,
start + xe_bo_size(bo), U64_MAX,
exec);
} else {
err = xe_ggtt_insert_bo(t->mem.ggtt, bo, exec);
}
if (err)
goto err_unlock_put_bo;
}
}
trace_xe_bo_create(bo);
return bo;
err_unlock_put_bo:
__xe_bo_unset_bulk_move(bo);
xe_bo_unlock_vm_held(bo);
xe_bo_put(bo);
return ERR_PTR(err);
}
struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile,
struct xe_vm *vm, size_t size,
enum ttm_bo_type type, u32 flags,
struct drm_exec *exec)
{
return __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL, 0, type,
flags, 0, exec);
}
static struct xe_bo *xe_bo_create_novm(struct xe_device *xe, struct xe_tile *tile,
size_t size, u16 cpu_caching,
enum ttm_bo_type type, u32 flags,
u64 alignment, bool intr)
{
struct xe_validation_ctx ctx;
struct drm_exec exec;
struct xe_bo *bo;
int ret = 0;
xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = intr},
ret) {
bo = __xe_bo_create_locked(xe, tile, NULL, size, 0, ~0ULL,
cpu_caching, type, flags, alignment, &exec);
drm_exec_retry_on_contention(&exec);
if (IS_ERR(bo)) {
ret = PTR_ERR(bo);
xe_validation_retry_on_oom(&ctx, &ret);
} else {
xe_bo_unlock(bo);
}
}
return ret ? ERR_PTR(ret) : bo;
}
#ifdef CONFIG_DRM_XE_DEBUG_PAGE_SIZE
static void xe_bo_debug_mixed_mode_cur_index_advance(struct xe_device *xe, struct xe_bo *bo)
{
if (!xe_debug_page_size_mode_is_mixed(xe))
return;
if (!(bo->flags & XE_BO_FLAG_VRAM_MASK) ||
!(bo->flags & XE_BO_FLAG_USER))
return;
mutex_lock(&xe->page_size_alloc_ctrl.lock);
if (xe->page_size_alloc_ctrl.mode == XE_PAGE_SIZE_ALLOC_CTRL_MODE_MIXED)
xe->page_size_alloc_ctrl.cur_index++;
mutex_unlock(&xe->page_size_alloc_ctrl.lock);
}
static bool xe_size_align_overflows(size_t size, size_t align)
{
return size > SIZE_MAX - (align - 1);
}
static u32 get_flag_from_cur_index_in_mixed_mode(struct xe_device *xe, size_t *align_size,
int *err)
{
static const struct {
u32 flag;
size_t align;
} map[] = {
{ 0, SZ_4K },
{ XE_BO_FLAG_NEEDS_64K, SZ_64K },
{ XE_BO_FLAG_NEEDS_2M, SZ_2M },
{ XE_BO_FLAG_NEEDS_1G, SZ_1G },
};
u32 idx;
const typeof(*map) *entry;
lockdep_assert_held(&xe->page_size_alloc_ctrl.lock);
*err = 0;
idx = xe->page_size_alloc_ctrl.cur_index % ARRAY_SIZE(map);
entry = &map[idx];
if (!entry->flag)
return 0;
if (xe_size_align_overflows(*align_size, entry->align)) {
*err = -EINVAL;
return 0;
}
*align_size = ALIGN(*align_size, entry->align);
return entry->flag;
}
static int xe_bo_apply_debug_page_size_policy(struct xe_device *xe,
u32 *bo_flags,
size_t *size)
{
enum xe_page_size_alloc_ctrl_mode mode;
u32 want = 0;
size_t align_size = *size;
int err = 0;
if (!(*bo_flags & XE_BO_FLAG_VRAM_MASK))
return 0;
if (*bo_flags & (XE_BO_FLAG_NEEDS_64K |
XE_BO_FLAG_NEEDS_2M |
XE_BO_FLAG_NEEDS_1G))
return 0;
if (!READ_ONCE(xe->page_size_alloc_ctrl.mode))
return 0;
mutex_lock(&xe->page_size_alloc_ctrl.lock);
mode = xe->page_size_alloc_ctrl.mode;
if (mode == XE_PAGE_SIZE_ALLOC_CTRL_MODE_NONE) {
goto out_unlock;
} else if (mode == XE_PAGE_SIZE_ALLOC_CTRL_MODE_ONLY_2M) {
if (xe_size_align_overflows(align_size, SZ_2M)) {
err = -EINVAL;
goto out_unlock;
}
want = XE_BO_FLAG_NEEDS_2M;
align_size = ALIGN(align_size, SZ_2M);
} else if (mode == XE_PAGE_SIZE_ALLOC_CTRL_MODE_ONLY_1G) {
if (xe_size_align_overflows(align_size, SZ_1G)) {
err = -EINVAL;
goto out_unlock;
}
want = XE_BO_FLAG_NEEDS_1G;
align_size = ALIGN(align_size, SZ_1G);
} else if (mode == XE_PAGE_SIZE_ALLOC_CTRL_MODE_MIXED) {
want = get_flag_from_cur_index_in_mixed_mode(xe, &align_size, &err);
if (err)
goto out_unlock;
} else {
goto out_unlock;
}
mutex_unlock(&xe->page_size_alloc_ctrl.lock);
*bo_flags |= want;
*size = align_size;
return err;
out_unlock:
mutex_unlock(&xe->page_size_alloc_ctrl.lock);
return err;
}
#else
static int xe_bo_apply_debug_page_size_policy(struct xe_device *xe,
u32 *bo_flags,
size_t *size)
{
return 0;
}
static void xe_bo_debug_mixed_mode_cur_index_advance(struct xe_device *xe,
struct xe_bo *bo)
{
}
#endif
struct xe_bo *xe_bo_create_user(struct xe_device *xe,
struct xe_vm *vm, size_t size,
u16 cpu_caching,
u32 flags, struct drm_exec *exec)
{
struct xe_bo *bo;
int err = 0;
flags |= XE_BO_FLAG_USER;
if (xe_debug_page_size_mode_not_none(xe)) {
err = xe_bo_apply_debug_page_size_policy(xe, &flags, &size);
if (err)
return ERR_PTR(err);
}
if (vm || exec) {
xe_assert(xe, exec);
bo = __xe_bo_create_locked(xe, NULL, vm, size, 0, ~0ULL,
cpu_caching, ttm_bo_type_device,
flags, 0, exec);
if (!IS_ERR(bo))
xe_bo_unlock_vm_held(bo);
} else {
bo = xe_bo_create_novm(xe, NULL, size, cpu_caching,
ttm_bo_type_device, flags, 0, true);
}
return bo;
}
struct xe_bo *xe_bo_create_pin_range_novm(struct xe_device *xe, struct xe_tile *tile,
size_t size, u64 start, u64 end,
enum ttm_bo_type type, u32 flags)
{
struct xe_validation_ctx ctx;
struct drm_exec exec;
struct xe_bo *bo;
int err = 0;
xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {}, err) {
bo = __xe_bo_create_locked(xe, tile, NULL, size, start, end,
0, type, flags, 0, &exec);
if (IS_ERR(bo)) {
drm_exec_retry_on_contention(&exec);
err = PTR_ERR(bo);
xe_validation_retry_on_oom(&ctx, &err);
break;
}
err = xe_bo_pin(bo, &exec);
xe_bo_unlock(bo);
if (err) {
xe_bo_put(bo);
drm_exec_retry_on_contention(&exec);
xe_validation_retry_on_oom(&ctx, &err);
break;
}
}
return err ? ERR_PTR(err) : bo;
}
static struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
struct xe_tile *tile,
struct xe_vm *vm,
size_t size, u64 offset,
enum ttm_bo_type type, u32 flags,
u64 alignment, struct drm_exec *exec)
{
struct xe_bo *bo;
int err;
u64 start = offset == ~0ull ? 0 : offset;
u64 end = offset == ~0ull ? ~0ull : start + size;
if (flags & XE_BO_FLAG_STOLEN &&
xe_ttm_stolen_cpu_access_needs_ggtt(xe))
flags |= XE_BO_FLAG_GGTT;
bo = __xe_bo_create_locked(xe, tile, vm, size, start, end, 0, type,
flags | XE_BO_FLAG_NEEDS_CPU_ACCESS | XE_BO_FLAG_PINNED,
alignment, exec);
if (IS_ERR(bo))
return bo;
err = xe_bo_pin(bo, exec);
if (err)
goto err_put;
err = xe_bo_vmap(bo);
if (err)
goto err_unpin;
xe_bo_unlock_vm_held(bo);
return bo;
err_unpin:
xe_bo_unpin(bo);
err_put:
xe_bo_unlock_vm_held(bo);
xe_bo_put(bo);
return ERR_PTR(err);
}
struct xe_bo *
xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile *tile,
size_t size, u64 offset, enum ttm_bo_type type, u32 flags,
u64 alignment, bool intr)
{
struct xe_validation_ctx ctx;
struct drm_exec exec;
struct xe_bo *bo;
int ret = 0;
xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = intr},
ret) {
bo = xe_bo_create_pin_map_at_aligned(xe, tile, NULL, size, offset,
type, flags, alignment, &exec);
if (IS_ERR(bo)) {
drm_exec_retry_on_contention(&exec);
ret = PTR_ERR(bo);
xe_validation_retry_on_oom(&ctx, &ret);
}
}
return ret ? ERR_PTR(ret) : bo;
}
struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
struct xe_vm *vm, size_t size,
enum ttm_bo_type type, u32 flags,
struct drm_exec *exec)
{
return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size, ~0ull, type, flags,
0, exec);
}
struct xe_bo *xe_bo_create_pin_map_novm(struct xe_device *xe, struct xe_tile *tile,
size_t size, enum ttm_bo_type type, u32 flags,
bool intr)
{
return xe_bo_create_pin_map_at_novm(xe, tile, size, ~0ull, type, flags, 0, intr);
}
static void __xe_bo_unpin_map_no_vm(void *arg)
{
xe_bo_unpin_map_no_vm(arg);
}
struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
size_t size, u32 flags)
{
struct xe_bo *bo;
int ret;
KUNIT_STATIC_STUB_REDIRECT(xe_managed_bo_create_pin_map, xe, tile, size, flags);
bo = xe_bo_create_pin_map_novm(xe, tile, size, ttm_bo_type_kernel, flags, true);
if (IS_ERR(bo))
return bo;
ret = devm_add_action_or_reset(xe->drm.dev, __xe_bo_unpin_map_no_vm, bo);
if (ret)
return ERR_PTR(ret);
return bo;
}
void xe_managed_bo_unpin_map_no_vm(struct xe_bo *bo)
{
devm_release_action(xe_bo_device(bo)->drm.dev, __xe_bo_unpin_map_no_vm, bo);
}
struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
const void *data, size_t size, u32 flags)
{
struct xe_bo *bo = xe_managed_bo_create_pin_map(xe, tile, ALIGN(size, PAGE_SIZE), flags);
if (IS_ERR(bo))
return bo;
xe_map_memcpy_to(xe, &bo->vmap, 0, data, size);
return bo;
}
int xe_managed_bo_reinit_in_vram(struct xe_device *xe, struct xe_tile *tile, struct xe_bo **src)
{
struct xe_bo *bo;
u32 dst_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile) | XE_BO_FLAG_GGTT;
dst_flags |= (*src)->flags & (XE_BO_FLAG_GGTT_INVALIDATE |
XE_BO_FLAG_PINNED_NORESTORE);
xe_assert(xe, IS_DGFX(xe));
xe_assert(xe, !(*src)->vmap.is_iomem);
bo = xe_managed_bo_create_from_data(xe, tile, (*src)->vmap.vaddr,
xe_bo_size(*src), dst_flags);
if (IS_ERR(bo))
return PTR_ERR(bo);
devm_release_action(xe->drm.dev, __xe_bo_unpin_map_no_vm, *src);
*src = bo;
return 0;
}
uint64_t vram_region_gpu_offset(struct ttm_resource *res)
{
struct xe_device *xe = ttm_to_xe_device(res->bo->bdev);
switch (res->mem_type) {
case XE_PL_STOLEN:
return xe_ttm_stolen_gpu_offset(xe);
case XE_PL_TT:
case XE_PL_SYSTEM:
return 0;
default:
return xe_map_resource_to_region(res)->dpa_base;
}
return 0;
}
int xe_bo_pin_external(struct xe_bo *bo, bool in_place, struct drm_exec *exec)
{
struct xe_device *xe = xe_bo_device(bo);
bool first_pin = !xe_bo_is_pinned(bo);
int err;
xe_assert(xe, !bo->vm);
xe_assert(xe, xe_bo_is_user(bo));
if (first_pin) {
if (!in_place) {
err = xe_bo_validate(bo, NULL, false, exec);
if (err)
return err;
}
spin_lock(&xe->pinned.lock);
list_add_tail(&bo->pinned_link, &xe->pinned.late.external);
spin_unlock(&xe->pinned.lock);
}
xe_bo_pin_account(bo);
ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
return 0;
}
int xe_bo_pin(struct xe_bo *bo, struct drm_exec *exec)
{
struct ttm_place *place = &bo->placements[0];
struct xe_device *xe = xe_bo_device(bo);
int err;
xe_assert(xe, !xe_bo_is_user(bo));
xe_assert(xe, bo->flags & (XE_BO_FLAG_PINNED |
XE_BO_FLAG_GGTT));
xe_assert(xe, !bo->ttm.base.import_attach);
xe_assert(xe, !xe_bo_is_pinned(bo));
err = xe_bo_validate(bo, NULL, false, exec);
if (err)
return err;
if (mem_type_is_vram(place->mem_type) || bo->flags & XE_BO_FLAG_GGTT) {
spin_lock(&xe->pinned.lock);
if (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE)
list_add_tail(&bo->pinned_link, &xe->pinned.late.kernel_bo_present);
else
list_add_tail(&bo->pinned_link, &xe->pinned.early.kernel_bo_present);
spin_unlock(&xe->pinned.lock);
}
xe_bo_pin_account(bo);
ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
return 0;
}
void xe_bo_unpin_external(struct xe_bo *bo)
{
struct xe_device *xe = xe_bo_device(bo);
xe_assert(xe, !bo->vm);
xe_assert(xe, xe_bo_is_pinned(bo));
xe_assert(xe, xe_bo_is_user(bo));
xe_bo_unpin_account(bo);
ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
}
void xe_bo_unpin(struct xe_bo *bo)
{
struct ttm_place *place = &bo->placements[0];
struct xe_device *xe = xe_bo_device(bo);
if (xe_bo_is_purged(bo))
return;
xe_assert(xe, !bo->ttm.base.import_attach);
xe_assert(xe, xe_bo_is_pinned(bo));
if (mem_type_is_vram(place->mem_type) || bo->flags & XE_BO_FLAG_GGTT) {
xe_assert(xe, !list_empty(&bo->pinned_link));
if (bo->backup_obj) {
if (xe_bo_is_pinned(bo->backup_obj))
xe_bo_unpin_account(bo->backup_obj);
xe_bo_put(bo->backup_obj);
bo->backup_obj = NULL;
}
}
xe_bo_unpin_account(bo);
}
int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict,
struct drm_exec *exec)
{
struct ttm_operation_ctx ctx = {
.interruptible = true,
.no_wait_gpu = false,
.gfp_retry_mayfail = true,
};
int ret;
if (xe_bo_is_pinned(bo))
return 0;
if (vm) {
lockdep_assert_held(&vm->lock);
xe_vm_assert_held(vm);
ctx.allow_res_evict = allow_res_evict;
ctx.resv = xe_vm_resv(vm);
}
xe_vm_set_validating(vm, allow_res_evict);
trace_xe_bo_validate(bo);
xe_validation_assert_exec(xe_bo_device(bo), exec, &bo->ttm.base);
ret = ttm_bo_validate(&bo->ttm, &bo->placement, &ctx);
xe_vm_clear_validating(vm, allow_res_evict);
return ret;
}
bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo)
{
if (bo->destroy == &xe_ttm_bo_destroy)
return true;
return false;
}
dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
{
struct xe_device *xe = xe_bo_device(bo);
struct xe_res_cursor cur;
u64 page;
xe_assert(xe, page_size <= PAGE_SIZE);
page = offset >> PAGE_SHIFT;
offset &= (PAGE_SIZE - 1);
if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
xe_assert(xe, bo->ttm.ttm);
xe_res_first_sg(xe_bo_sg(bo), page << PAGE_SHIFT,
page_size, &cur);
return xe_res_dma(&cur) + offset;
} else {
struct xe_res_cursor cur;
xe_res_first(bo->ttm.resource, page << PAGE_SHIFT,
page_size, &cur);
return cur.start + offset + vram_region_gpu_offset(bo->ttm.resource);
}
}
dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
{
if (!READ_ONCE(bo->ttm.pin_count))
xe_bo_assert_held(bo);
return __xe_bo_addr(bo, offset, page_size);
}
int xe_bo_vmap(struct xe_bo *bo)
{
struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
void *virtual;
bool is_iomem;
int ret;
xe_bo_assert_held(bo);
if (drm_WARN_ON(&xe->drm, !(bo->flags & XE_BO_FLAG_NEEDS_CPU_ACCESS) ||
!force_contiguous(bo->flags)))
return -EINVAL;
if (!iosys_map_is_null(&bo->vmap))
return 0;
ret = ttm_bo_kmap(&bo->ttm, 0, xe_bo_size(bo) >> PAGE_SHIFT, &bo->kmap);
if (ret)
return ret;
virtual = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
if (is_iomem)
iosys_map_set_vaddr_iomem(&bo->vmap, (void __iomem *)virtual);
else
iosys_map_set_vaddr(&bo->vmap, virtual);
return 0;
}
static void __xe_bo_vunmap(struct xe_bo *bo)
{
if (!iosys_map_is_null(&bo->vmap)) {
iosys_map_clear(&bo->vmap);
ttm_bo_kunmap(&bo->kmap);
}
}
void xe_bo_vunmap(struct xe_bo *bo)
{
xe_bo_assert_held(bo);
__xe_bo_vunmap(bo);
}
static int gem_create_set_pxp_type(struct xe_device *xe, struct xe_bo *bo, u64 value)
{
if (value == DRM_XE_PXP_TYPE_NONE)
return 0;
if (XE_IOCTL_DBG(xe, value != DRM_XE_PXP_TYPE_HWDRM))
return -EINVAL;
return xe_pxp_key_assign(xe->pxp, bo);
}
typedef int (*xe_gem_create_set_property_fn)(struct xe_device *xe,
struct xe_bo *bo,
u64 value);
static const xe_gem_create_set_property_fn gem_create_set_property_funcs[] = {
[DRM_XE_GEM_CREATE_SET_PROPERTY_PXP_TYPE] = gem_create_set_pxp_type,
};
static int gem_create_user_ext_set_property(struct xe_device *xe,
struct xe_bo *bo,
u64 extension)
{
u64 __user *address = u64_to_user_ptr(extension);
struct drm_xe_ext_set_property ext;
int err;
u32 idx;
err = copy_from_user(&ext, address, sizeof(ext));
if (XE_IOCTL_DBG(xe, err))
return -EFAULT;
if (XE_IOCTL_DBG(xe, ext.property >=
ARRAY_SIZE(gem_create_set_property_funcs)) ||
XE_IOCTL_DBG(xe, ext.pad) ||
XE_IOCTL_DBG(xe, ext.property != DRM_XE_GEM_CREATE_EXTENSION_SET_PROPERTY))
return -EINVAL;
idx = array_index_nospec(ext.property, ARRAY_SIZE(gem_create_set_property_funcs));
if (!gem_create_set_property_funcs[idx])
return -EINVAL;
return gem_create_set_property_funcs[idx](xe, bo, ext.value);
}
typedef int (*xe_gem_create_user_extension_fn)(struct xe_device *xe,
struct xe_bo *bo,
u64 extension);
static const xe_gem_create_user_extension_fn gem_create_user_extension_funcs[] = {
[DRM_XE_GEM_CREATE_EXTENSION_SET_PROPERTY] = gem_create_user_ext_set_property,
};
#define MAX_USER_EXTENSIONS 16
static int gem_create_user_extensions(struct xe_device *xe, struct xe_bo *bo,
u64 extensions, int ext_number)
{
u64 __user *address = u64_to_user_ptr(extensions);
struct drm_xe_user_extension ext;
int err;
u32 idx;
if (XE_IOCTL_DBG(xe, ext_number >= MAX_USER_EXTENSIONS))
return -E2BIG;
err = copy_from_user(&ext, address, sizeof(ext));
if (XE_IOCTL_DBG(xe, err))
return -EFAULT;
if (XE_IOCTL_DBG(xe, ext.pad) ||
XE_IOCTL_DBG(xe, ext.name >= ARRAY_SIZE(gem_create_user_extension_funcs)))
return -EINVAL;
idx = array_index_nospec(ext.name,
ARRAY_SIZE(gem_create_user_extension_funcs));
err = gem_create_user_extension_funcs[idx](xe, bo, extensions);
if (XE_IOCTL_DBG(xe, err))
return err;
if (ext.next_extension)
return gem_create_user_extensions(xe, bo, ext.next_extension,
++ext_number);
return 0;
}
int xe_gem_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_gem_create *args = data;
struct xe_validation_ctx ctx;
struct drm_exec exec;
struct xe_vm *vm = NULL;
struct xe_bo *bo;
unsigned int bo_flags;
u32 handle;
int err;
if (XE_IOCTL_DBG(xe, args->pad[0] || args->pad[1] || args->pad[2]) ||
XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
return -EINVAL;
if (XE_IOCTL_DBG(xe, (args->placement & ~xe->info.mem_region_mask) ||
!args->placement))
return -EINVAL;
if (XE_IOCTL_DBG(xe, args->flags &
~(DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING |
DRM_XE_GEM_CREATE_FLAG_SCANOUT |
DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM |
DRM_XE_GEM_CREATE_FLAG_NO_COMPRESSION)))
return -EINVAL;
if (XE_IOCTL_DBG(xe, args->handle))
return -EINVAL;
if (XE_IOCTL_DBG(xe, !args->size))
return -EINVAL;
if (XE_IOCTL_DBG(xe, args->size > SIZE_MAX))
return -EINVAL;
if (XE_IOCTL_DBG(xe, args->size & ~PAGE_MASK))
return -EINVAL;
bo_flags = 0;
if (args->flags & DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING)
bo_flags |= XE_BO_FLAG_DEFER_BACKING;
if (args->flags & DRM_XE_GEM_CREATE_FLAG_SCANOUT)
bo_flags |= XE_BO_FLAG_FORCE_WC;
if (args->flags & DRM_XE_GEM_CREATE_FLAG_NO_COMPRESSION) {
if (XE_IOCTL_DBG(xe, GRAPHICS_VER(xe) < 20))
return -EOPNOTSUPP;
bo_flags |= XE_BO_FLAG_NO_COMPRESSION;
}
bo_flags |= args->placement << (ffs(XE_BO_FLAG_SYSTEM) - 1);
if ((bo_flags & XE_BO_FLAG_VRAM_MASK) &&
(args->flags & DRM_XE_GEM_CREATE_FLAG_SCANOUT) &&
!(xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) &&
IS_ALIGNED(args->size, SZ_64K))
bo_flags |= XE_BO_FLAG_NEEDS_64K;
if (args->flags & DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM) {
if (XE_IOCTL_DBG(xe, !(bo_flags & XE_BO_FLAG_VRAM_MASK)))
return -EINVAL;
bo_flags |= XE_BO_FLAG_NEEDS_CPU_ACCESS;
}
if (XE_IOCTL_DBG(xe, !args->cpu_caching ||
args->cpu_caching > DRM_XE_GEM_CPU_CACHING_WC))
return -EINVAL;
if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_FLAG_VRAM_MASK &&
args->cpu_caching != DRM_XE_GEM_CPU_CACHING_WC))
return -EINVAL;
if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_FLAG_FORCE_WC &&
args->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB))
return -EINVAL;
if (args->vm_id) {
vm = xe_vm_lookup(xef, args->vm_id);
if (XE_IOCTL_DBG(xe, !vm))
return -ENOENT;
}
err = 0;
xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = true},
err) {
if (vm) {
err = xe_vm_drm_exec_lock(vm, &exec);
drm_exec_retry_on_contention(&exec);
if (err)
break;
}
bo = xe_bo_create_user(xe, vm, args->size, args->cpu_caching,
bo_flags, &exec);
drm_exec_retry_on_contention(&exec);
if (IS_ERR(bo)) {
err = PTR_ERR(bo);
xe_validation_retry_on_oom(&ctx, &err);
break;
}
}
if (err)
goto out_vm;
if (args->extensions) {
err = gem_create_user_extensions(xe, bo, args->extensions, 0);
if (err)
goto out_bulk;
}
err = drm_gem_handle_create(file, &bo->ttm.base, &handle);
if (err)
goto out_bulk;
xe_bo_debug_mixed_mode_cur_index_advance(xe, bo);
args->handle = handle;
goto out_put;
out_bulk:
if (vm && !xe_vm_in_fault_mode(vm)) {
xe_vm_lock(vm, false);
__xe_bo_unset_bulk_move(bo);
xe_vm_unlock(vm);
}
out_put:
xe_bo_put(bo);
out_vm:
if (vm)
xe_vm_put(vm);
return err;
}
static int xe_gem_pci_barrier_mmap_offset(struct xe_device *xe, struct drm_file *file,
struct drm_xe_gem_mmap_offset *args)
{
struct xe_file *xef = file->driver_priv;
struct xe_mmio_gem **barrier = &xef->mmio_gem.pci_barrier;
if (XE_IOCTL_DBG(xe, !IS_DGFX(xe)))
return -EINVAL;
if (XE_IOCTL_DBG(xe, args->handle))
return -EINVAL;
scoped_guard(mutex, &xef->mmio_gem.lock) {
if (!*barrier) {
phys_addr_t phys_addr;
#define LAST_DB_PAGE_OFFSET 0x7ff000
phys_addr = pci_resource_start(to_pci_dev(xe->drm.dev), 0) +
LAST_DB_PAGE_OFFSET;
*barrier = xe_mmio_gem_create(xe, file, phys_addr, SZ_4K);
if (IS_ERR(*barrier)) {
int err = PTR_ERR(*barrier);
*barrier = NULL;
return err;
}
}
args->offset = xe_mmio_gem_mmap_offset(*barrier);
}
return 0;
}
int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
struct drm_file *file)
{
struct xe_device *xe = to_xe_device(dev);
struct drm_xe_gem_mmap_offset *args = data;
struct drm_gem_object *gem_obj;
if (XE_IOCTL_DBG(xe, args->extensions) ||
XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
return -EINVAL;
if (XE_IOCTL_DBG(xe, args->flags &
~DRM_XE_MMAP_OFFSET_FLAG_PCI_BARRIER))
return -EINVAL;
if (args->flags & DRM_XE_MMAP_OFFSET_FLAG_PCI_BARRIER)
return xe_gem_pci_barrier_mmap_offset(xe, file, args);
gem_obj = drm_gem_object_lookup(file, args->handle);
if (XE_IOCTL_DBG(xe, !gem_obj))
return -ENOENT;
args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
xe_bo_put(gem_to_xe_bo(gem_obj));
return 0;
}
int xe_bo_decompress(struct xe_bo *bo)
{
struct xe_device *xe = xe_bo_device(bo);
struct xe_tile *tile = xe_device_get_root_tile(xe);
struct dma_fence *decomp_fence = NULL;
struct ttm_operation_ctx op_ctx = {
.interruptible = true,
.no_wait_gpu = false,
.gfp_retry_mayfail = false,
};
int err = 0;
if (!bo->ttm.resource || !mem_type_is_vram(bo->ttm.resource->mem_type))
return 0;
err = xe_bo_move_notify(bo, &op_ctx);
if (err)
return err;
err = dma_resv_reserve_fences(bo->ttm.base.resv, 1);
if (err)
return err;
decomp_fence = xe_migrate_resolve(tile->migrate,
bo,
bo->ttm.resource);
if (IS_ERR(decomp_fence))
return PTR_ERR(decomp_fence);
dma_resv_add_fence(bo->ttm.base.resv, decomp_fence, DMA_RESV_USAGE_KERNEL);
dma_fence_put(decomp_fence);
return 0;
}
int xe_bo_lock(struct xe_bo *bo, bool intr)
{
if (intr)
return dma_resv_lock_interruptible(bo->ttm.base.resv, NULL);
dma_resv_lock(bo->ttm.base.resv, NULL);
return 0;
}
void xe_bo_unlock(struct xe_bo *bo)
{
dma_resv_unlock(bo->ttm.base.resv);
}
bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type)
{
unsigned int cur_place;
if (bo->ttm.type == ttm_bo_type_kernel)
return true;
if (bo->ttm.type == ttm_bo_type_sg)
return false;
for (cur_place = 0; cur_place < bo->placement.num_placement;
cur_place++) {
if (bo->placements[cur_place].mem_type == mem_type)
return true;
}
return false;
}
static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place)
{
memset(place, 0, sizeof(*place));
place->mem_type = mem_type;
}
int xe_bo_migrate(struct xe_bo *bo, u32 mem_type, struct ttm_operation_ctx *tctx,
struct drm_exec *exec)
{
struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev);
struct ttm_operation_ctx ctx = {
.interruptible = true,
.no_wait_gpu = false,
.gfp_retry_mayfail = true,
};
struct ttm_placement placement;
struct ttm_place requested;
xe_bo_assert_held(bo);
tctx = tctx ? tctx : &ctx;
if (bo->ttm.resource->mem_type == mem_type)
return 0;
if (xe_bo_is_pinned(bo))
return -EBUSY;
if (!xe_bo_can_migrate(bo, mem_type))
return -EINVAL;
xe_place_from_ttm_type(mem_type, &requested);
placement.num_placement = 1;
placement.placement = &requested;
drm_WARN_ON(&xe->drm, mem_type == XE_PL_STOLEN);
if (mem_type_is_vram(mem_type)) {
u32 c = 0;
add_vram(xe, bo, &requested, bo->flags, mem_type, &c);
}
if (!tctx->no_wait_gpu)
xe_validation_assert_exec(xe_bo_device(bo), exec, &bo->ttm.base);
return ttm_bo_validate(&bo->ttm, &placement, tctx);
}
int xe_bo_evict(struct xe_bo *bo, struct drm_exec *exec)
{
struct ttm_operation_ctx ctx = {
.interruptible = false,
.no_wait_gpu = false,
.gfp_retry_mayfail = true,
};
struct ttm_placement placement;
int ret;
xe_evict_flags(&bo->ttm, &placement);
ret = ttm_bo_validate(&bo->ttm, &placement, &ctx);
if (ret)
return ret;
dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL,
false, MAX_SCHEDULE_TIMEOUT);
return 0;
}
bool xe_bo_needs_ccs_pages(struct xe_bo *bo)
{
struct xe_device *xe = xe_bo_device(bo);
if (GRAPHICS_VER(xe) >= 20 && IS_DGFX(xe))
return false;
if (!xe_device_has_flat_ccs(xe) || bo->ttm.type != ttm_bo_type_device)
return false;
if (IS_DGFX(xe) && (bo->flags & XE_BO_FLAG_SYSTEM))
return false;
if (bo->flags & XE_BO_FLAG_NO_COMPRESSION)
return false;
if (bo->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB &&
xe->pat.idx[XE_CACHE_WB_COMPRESSION] == XE_PAT_INVALID_IDX)
return false;
return true;
}
void __xe_bo_release_dummy(struct kref *kref)
{
}
void xe_bo_put_commit(struct llist_head *deferred)
{
struct llist_node *freed;
struct xe_bo *bo, *next;
if (!deferred)
return;
freed = llist_del_all(deferred);
if (!freed)
return;
llist_for_each_entry_safe(bo, next, freed, freed)
drm_gem_object_free(&bo->ttm.base.refcount);
}
static void xe_bo_dev_work_func(struct work_struct *work)
{
struct xe_bo_dev *bo_dev = container_of(work, typeof(*bo_dev), async_free);
xe_bo_put_commit(&bo_dev->async_list);
}
void xe_bo_dev_init(struct xe_bo_dev *bo_dev)
{
INIT_WORK(&bo_dev->async_free, xe_bo_dev_work_func);
}
void xe_bo_dev_fini(struct xe_bo_dev *bo_dev)
{
flush_work(&bo_dev->async_free);
}
void xe_bo_put(struct xe_bo *bo)
{
struct xe_tile *tile;
u8 id;
might_sleep();
if (bo) {
#ifdef CONFIG_PROC_FS
if (bo->client)
might_lock(&bo->client->bos_lock);
#endif
for_each_tile(tile, xe_bo_device(bo), id)
if (bo->ggtt_node[id])
xe_ggtt_might_lock(tile->mem.ggtt);
drm_gem_object_put(&bo->ttm.base);
}
}
int xe_bo_dumb_create(struct drm_file *file_priv,
struct drm_device *dev,
struct drm_mode_create_dumb *args)
{
struct xe_device *xe = to_xe_device(dev);
struct xe_bo *bo;
uint32_t handle;
int err;
u32 page_size = max_t(u32, PAGE_SIZE,
xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K);
err = drm_mode_size_dumb(dev, args, SZ_64, page_size);
if (err)
return err;
bo = xe_bo_create_user(xe, NULL, args->size,
DRM_XE_GEM_CPU_CACHING_WC,
XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |
XE_BO_FLAG_FORCE_WC |
XE_BO_FLAG_NEEDS_CPU_ACCESS, NULL);
if (IS_ERR(bo))
return PTR_ERR(bo);
err = drm_gem_handle_create(file_priv, &bo->ttm.base, &handle);
drm_gem_object_put(&bo->ttm.base);
if (!err)
args->handle = handle;
return err;
}
void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo *bo)
{
struct ttm_buffer_object *tbo = &bo->ttm;
struct ttm_device *bdev = tbo->bdev;
drm_vma_node_unmap(&tbo->base.vma_node, bdev->dev_mapping);
list_del_init(&bo->vram_userfault_link);
}
#if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
#include "tests/xe_bo.c"
#endif