12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115
#include "xe_guc_submit.h"
#include <linux/bitfield.h>
#include <uapi/drm/xe_drm.h>
#include <linux/bitmap.h>
#include <linux/circ_buf.h>
#include <linux/dma-fence-array.h>
#include <drm/drm_drv.h>
#include <drm/drm_managed.h>
#include "abi/guc_actions_abi.h"
#include "abi/guc_actions_slpc_abi.h"
#include "abi/guc_klvs_abi.h"
#include "xe_assert.h"
#include "xe_bo.h"
#include "xe_cpu_bind.h"
#include "xe_devcoredump.h"
#include "xe_device.h"
#include "xe_exec_queue.h"
#include "xe_force_wake.h"
#include "xe_gpu_scheduler.h"
#include "xe_gt.h"
#include "xe_gt_clock.h"
#include "xe_gt_printk.h"
#include "xe_guc.h"
#include "xe_guc_capture.h"
#include "xe_guc_ct.h"
#include "xe_guc_exec_queue_types.h"
#include "xe_guc_id_mgr.h"
#include "xe_guc_klv_helpers.h"
#include "xe_guc_submit_types.h"
#include "xe_hw_engine.h"
#include "xe_log.h"
#include "xe_lrc.h"
#include "xe_macros.h"
#include "xe_map.h"
#include "xe_mocs.h"
#include "xe_module.h"
#include "xe_pm.h"
#include "xe_pt.h"
#include "xe_ring_ops_types.h"
#include "xe_sched_job.h"
#include "xe_sleep.h"
#include "xe_tlb_inval.h"
#include "xe_trace.h"
#include "xe_uc_fw.h"
#include "xe_vm.h"
#define XE_GUC_EXEC_QUEUE_CGP_CONTEXT_ERROR_LEN 6
static int guc_submit_reset_prepare(struct xe_guc *guc);
static struct xe_guc *
exec_queue_to_guc(struct xe_exec_queue *q)
{
return &q->gt->uc.guc;
}
#define EXEC_QUEUE_STATE_REGISTERED (1 << 0)
#define EXEC_QUEUE_STATE_ENABLED (1 << 1)
#define EXEC_QUEUE_STATE_PENDING_ENABLE (1 << 2)
#define EXEC_QUEUE_STATE_PENDING_DISABLE (1 << 3)
#define EXEC_QUEUE_STATE_DESTROYED (1 << 4)
#define EXEC_QUEUE_STATE_SUSPENDED (1 << 5)
#define EXEC_QUEUE_STATE_RESET (1 << 6)
#define EXEC_QUEUE_STATE_KILLED (1 << 7)
#define EXEC_QUEUE_STATE_WEDGED (1 << 8)
#define EXEC_QUEUE_STATE_BANNED (1 << 9)
#define EXEC_QUEUE_STATE_PENDING_RESUME (1 << 10)
static bool exec_queue_registered(struct xe_exec_queue *q)
{
return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_REGISTERED;
}
static void set_exec_queue_registered(struct xe_exec_queue *q)
{
atomic_or(EXEC_QUEUE_STATE_REGISTERED, &q->guc->state);
}
static void clear_exec_queue_registered(struct xe_exec_queue *q)
{
atomic_and(~EXEC_QUEUE_STATE_REGISTERED, &q->guc->state);
}
static bool exec_queue_enabled(struct xe_exec_queue *q)
{
return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_ENABLED;
}
static void set_exec_queue_enabled(struct xe_exec_queue *q)
{
atomic_or(EXEC_QUEUE_STATE_ENABLED, &q->guc->state);
}
static void clear_exec_queue_enabled(struct xe_exec_queue *q)
{
atomic_and(~EXEC_QUEUE_STATE_ENABLED, &q->guc->state);
}
static bool exec_queue_pending_enable(struct xe_exec_queue *q)
{
return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_ENABLE;
}
static void set_exec_queue_pending_enable(struct xe_exec_queue *q)
{
atomic_or(EXEC_QUEUE_STATE_PENDING_ENABLE, &q->guc->state);
}
static void clear_exec_queue_pending_enable(struct xe_exec_queue *q)
{
atomic_and(~EXEC_QUEUE_STATE_PENDING_ENABLE, &q->guc->state);
}
static bool exec_queue_pending_disable(struct xe_exec_queue *q)
{
return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_DISABLE;
}
static void set_exec_queue_pending_disable(struct xe_exec_queue *q)
{
atomic_or(EXEC_QUEUE_STATE_PENDING_DISABLE, &q->guc->state);
}
static void clear_exec_queue_pending_disable(struct xe_exec_queue *q)
{
atomic_and(~EXEC_QUEUE_STATE_PENDING_DISABLE, &q->guc->state);
}
static bool exec_queue_destroyed(struct xe_exec_queue *q)
{
return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_DESTROYED;
}
static void set_exec_queue_destroyed(struct xe_exec_queue *q)
{
atomic_or(EXEC_QUEUE_STATE_DESTROYED, &q->guc->state);
}
static void clear_exec_queue_destroyed(struct xe_exec_queue *q)
{
atomic_and(~EXEC_QUEUE_STATE_DESTROYED, &q->guc->state);
}
static bool exec_queue_banned(struct xe_exec_queue *q)
{
return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_BANNED;
}
static void set_exec_queue_banned(struct xe_exec_queue *q)
{
atomic_or(EXEC_QUEUE_STATE_BANNED, &q->guc->state);
}
static void clear_exec_queue_banned(struct xe_exec_queue *q)
{
atomic_andnot(EXEC_QUEUE_STATE_BANNED, &q->guc->state);
}
static bool exec_queue_suspended(struct xe_exec_queue *q)
{
return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_SUSPENDED;
}
static void set_exec_queue_suspended(struct xe_exec_queue *q)
{
atomic_or(EXEC_QUEUE_STATE_SUSPENDED, &q->guc->state);
}
static void clear_exec_queue_suspended(struct xe_exec_queue *q)
{
atomic_and(~EXEC_QUEUE_STATE_SUSPENDED, &q->guc->state);
}
static bool exec_queue_reset(struct xe_exec_queue *q)
{
return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_RESET;
}
static void set_exec_queue_reset(struct xe_exec_queue *q)
{
atomic_or(EXEC_QUEUE_STATE_RESET, &q->guc->state);
}
static bool exec_queue_killed(struct xe_exec_queue *q)
{
return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_KILLED;
}
static void set_exec_queue_killed(struct xe_exec_queue *q)
{
atomic_or(EXEC_QUEUE_STATE_KILLED, &q->guc->state);
}
static bool exec_queue_wedged(struct xe_exec_queue *q)
{
return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_WEDGED;
}
static void set_exec_queue_wedged(struct xe_exec_queue *q)
{
atomic_or(EXEC_QUEUE_STATE_WEDGED, &q->guc->state);
}
static bool exec_queue_pending_resume(struct xe_exec_queue *q)
{
return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_RESUME;
}
static void set_exec_queue_pending_resume(struct xe_exec_queue *q)
{
atomic_or(EXEC_QUEUE_STATE_PENDING_RESUME, &q->guc->state);
}
static void clear_exec_queue_pending_resume(struct xe_exec_queue *q)
{
atomic_and(~EXEC_QUEUE_STATE_PENDING_RESUME, &q->guc->state);
}
static bool exec_queue_killed_or_banned_or_wedged(struct xe_exec_queue *q)
{
return (atomic_read(&q->guc->state) &
(EXEC_QUEUE_STATE_WEDGED | EXEC_QUEUE_STATE_KILLED |
EXEC_QUEUE_STATE_BANNED));
}
static void guc_submit_sw_fini(struct drm_device *drm, void *arg)
{
struct xe_guc *guc = arg;
struct xe_gt *gt = guc_to_gt(guc);
xe_gt_assert(gt, xa_empty(&guc->submission_state.exec_queue_lookup));
xa_destroy(&guc->submission_state.exec_queue_lookup);
}
static void guc_submit_fini(void *arg)
{
struct xe_guc *guc = arg;
struct xe_exec_queue *q;
unsigned long index;
mutex_lock(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
if (exec_queue_wedged(q)) {
mutex_unlock(&guc->submission_state.lock);
xe_exec_queue_put(q);
mutex_lock(&guc->submission_state.lock);
}
}
mutex_unlock(&guc->submission_state.lock);
xe_guc_ct_stop(&guc->ct);
xe_tlb_inval_reset(&guc_to_gt(guc)->tlb_inval);
guc_submit_reset_prepare(guc);
xe_guc_softreset(guc);
xe_guc_submit_stop(guc);
xe_uc_fw_sanitize(&guc->fw);
xe_guc_submit_pause_abort(guc);
}
static const struct xe_exec_queue_ops guc_exec_queue_ops;
static void primelockdep(struct xe_guc *guc)
{
if (!IS_ENABLED(CONFIG_LOCKDEP))
return;
fs_reclaim_acquire(GFP_KERNEL);
mutex_lock(&guc->submission_state.lock);
mutex_unlock(&guc->submission_state.lock);
fs_reclaim_release(GFP_KERNEL);
}
int xe_guc_submit_init(struct xe_guc *guc, unsigned int num_ids)
{
struct xe_device *xe = guc_to_xe(guc);
struct xe_gt *gt = guc_to_gt(guc);
int err;
err = drmm_mutex_init(&xe->drm, &guc->submission_state.lock);
if (err)
return err;
err = xe_guc_id_mgr_init(&guc->submission_state.idm, num_ids);
if (err)
return err;
gt->exec_queue_ops = &guc_exec_queue_ops;
xa_init(&guc->submission_state.exec_queue_lookup);
primelockdep(guc);
guc->submission_state.initialized = true;
err = drmm_add_action_or_reset(&xe->drm, guc_submit_sw_fini, guc);
if (err)
return err;
return devm_add_action_or_reset(xe->drm.dev, guc_submit_fini, guc);
}
#define RC_YIELD_DURATION 80 /* in ms */
#define RC_YIELD_RATIO 20 /* in percent */
static u32 *emit_render_compute_yield_klv(u32 *emit)
{
*emit++ = PREP_GUC_KLV_TAG(SCHEDULING_POLICIES_RENDER_COMPUTE_YIELD);
*emit++ = RC_YIELD_DURATION;
*emit++ = RC_YIELD_RATIO;
return emit;
}
#define SCHEDULING_POLICY_MAX_DWORDS 16
static int guc_init_global_schedule_policy(struct xe_guc *guc)
{
u32 data[SCHEDULING_POLICY_MAX_DWORDS];
u32 *emit = data;
u32 count = 0;
int ret;
if (GUC_SUBMIT_VER(guc) < MAKE_GUC_VER(1, 1, 0))
return 0;
*emit++ = XE_GUC_ACTION_UPDATE_SCHEDULING_POLICIES_KLV;
if (CCS_INSTANCES(guc_to_gt(guc)))
emit = emit_render_compute_yield_klv(emit);
count = emit - data;
if (count > 1) {
xe_assert(guc_to_xe(guc), count <= SCHEDULING_POLICY_MAX_DWORDS);
ret = xe_guc_ct_send_block(&guc->ct, data, count);
if (ret < 0) {
xe_gt_err(guc_to_gt(guc),
"failed to enable GuC scheduling policies: %pe\n",
ERR_PTR(ret));
return ret;
}
}
return 0;
}
int xe_guc_submit_enable(struct xe_guc *guc)
{
int ret;
ret = guc_init_global_schedule_policy(guc);
if (ret)
return ret;
guc->submission_state.enabled = true;
return 0;
}
void xe_guc_submit_disable(struct xe_guc *guc)
{
guc->submission_state.enabled = false;
}
static void __release_guc_id(struct xe_guc *guc, struct xe_exec_queue *q,
int count)
{
int i;
mutex_lock(&guc->submission_state.lock);
for (i = 0; i < count; ++i)
xa_erase(&guc->submission_state.exec_queue_lookup,
q->guc->id + i);
xe_guc_id_mgr_release_locked(&guc->submission_state.idm,
q->guc->id, q->width);
mutex_unlock(&guc->submission_state.lock);
}
static int alloc_guc_id(struct xe_guc *guc, struct xe_exec_queue *q)
{
int ret, i;
mutex_lock(&guc->submission_state.lock);
ret = xe_guc_id_mgr_reserve_locked(&guc->submission_state.idm,
q->width);
mutex_unlock(&guc->submission_state.lock);
if (ret < 0)
return ret;
q->guc->id = ret;
for (i = 0; i < q->width; ++i) {
ret = xa_insert(&guc->submission_state.exec_queue_lookup,
q->guc->id + i, NULL, GFP_KERNEL);
if (ret)
goto err_release;
}
return 0;
err_release:
__release_guc_id(guc, q, i);
return ret;
}
static void publish_guc_id(struct xe_guc *guc, struct xe_exec_queue *q)
{
int i;
lockdep_assert_held(&guc->submission_state.lock);
for (i = 0; i < q->width; ++i) {
void *old;
old = xa_store(&guc->submission_state.exec_queue_lookup,
q->guc->id + i, q, GFP_NOWAIT);
XE_WARN_ON(old || xa_is_err(old));
}
}
static void release_guc_id(struct xe_guc *guc, struct xe_exec_queue *q)
{
__release_guc_id(guc, q, q->width);
}
struct exec_queue_policy {
u32 count;
struct guc_update_exec_queue_policy h2g;
};
static u32 __guc_exec_queue_policy_action_size(struct exec_queue_policy *policy)
{
size_t bytes = sizeof(policy->h2g.header) +
(sizeof(policy->h2g.klv[0]) * policy->count);
return bytes / sizeof(u32);
}
static void __guc_exec_queue_policy_start_klv(struct exec_queue_policy *policy,
u16 guc_id)
{
policy->h2g.header.action =
XE_GUC_ACTION_HOST2GUC_UPDATE_CONTEXT_POLICIES;
policy->h2g.header.guc_id = guc_id;
policy->count = 0;
}
#define MAKE_EXEC_QUEUE_POLICY_ADD(func, id) \
static void __guc_exec_queue_policy_add_##func(struct exec_queue_policy *policy, \
u32 data) \
{ \
XE_WARN_ON(policy->count >= GUC_CONTEXT_POLICIES_KLV_NUM_IDS); \
\
policy->h2g.klv[policy->count].kl = \
FIELD_PREP(GUC_KLV_0_KEY, \
GUC_CONTEXT_POLICIES_KLV_ID_##id) | \
FIELD_PREP(GUC_KLV_0_LEN, 1); \
policy->h2g.klv[policy->count].value = data; \
policy->count++; \
}
MAKE_EXEC_QUEUE_POLICY_ADD(execution_quantum, EXECUTION_QUANTUM)
MAKE_EXEC_QUEUE_POLICY_ADD(preemption_timeout, PREEMPTION_TIMEOUT)
MAKE_EXEC_QUEUE_POLICY_ADD(priority, SCHEDULING_PRIORITY)
MAKE_EXEC_QUEUE_POLICY_ADD(slpc_exec_queue_freq_req, SLPM_GT_FREQUENCY)
#undef MAKE_EXEC_QUEUE_POLICY_ADD
static const int xe_exec_queue_prio_to_guc[] = {
[XE_EXEC_QUEUE_PRIORITY_LOW] = GUC_CLIENT_PRIORITY_NORMAL,
[XE_EXEC_QUEUE_PRIORITY_NORMAL] = GUC_CLIENT_PRIORITY_KMD_NORMAL,
[XE_EXEC_QUEUE_PRIORITY_HIGH] = GUC_CLIENT_PRIORITY_HIGH,
[XE_EXEC_QUEUE_PRIORITY_KERNEL] = GUC_CLIENT_PRIORITY_KMD_HIGH,
};
static void init_policies(struct xe_guc *guc, struct xe_exec_queue *q)
{
struct exec_queue_policy policy;
enum xe_exec_queue_priority prio = q->sched_props.priority;
u32 timeslice_us = q->sched_props.timeslice_us;
u32 slpc_exec_queue_freq_req = 0;
u32 preempt_timeout_us = q->sched_props.preempt_timeout_us;
xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q) &&
!xe_exec_queue_is_multi_queue_secondary(q));
if (q->flags & EXEC_QUEUE_FLAG_LOW_LATENCY)
slpc_exec_queue_freq_req |= SLPC_CTX_FREQ_REQ_IS_COMPUTE;
__guc_exec_queue_policy_start_klv(&policy, q->guc->id);
__guc_exec_queue_policy_add_priority(&policy, xe_exec_queue_prio_to_guc[prio]);
__guc_exec_queue_policy_add_execution_quantum(&policy, timeslice_us);
__guc_exec_queue_policy_add_preemption_timeout(&policy, preempt_timeout_us);
__guc_exec_queue_policy_add_slpc_exec_queue_freq_req(&policy,
slpc_exec_queue_freq_req);
xe_guc_ct_send(&guc->ct, (u32 *)&policy.h2g,
__guc_exec_queue_policy_action_size(&policy), 0, 0);
}
static void set_min_preemption_timeout(struct xe_guc *guc, struct xe_exec_queue *q)
{
struct exec_queue_policy policy;
xe_assert(guc_to_xe(guc), !xe_exec_queue_is_multi_queue_secondary(q));
__guc_exec_queue_policy_start_klv(&policy, q->guc->id);
__guc_exec_queue_policy_add_preemption_timeout(&policy, 1);
xe_guc_ct_send(&guc->ct, (u32 *)&policy.h2g,
__guc_exec_queue_policy_action_size(&policy), 0, 0);
}
static bool vf_recovery(struct xe_guc *guc)
{
return xe_gt_recovery_pending(guc_to_gt(guc));
}
static void xe_guc_exec_queue_trigger_cleanup(struct xe_exec_queue *q)
{
struct xe_guc *guc = exec_queue_to_guc(q);
struct xe_device *xe = guc_to_xe(guc);
wake_up_all(&xe->ufence_wq);
xe_sched_tdr_queue_imm(&q->guc->sched);
}
static void xe_guc_exec_queue_group_stop(struct xe_exec_queue *q)
{
struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
struct xe_exec_queue_group *group = q->multi_queue.group;
struct xe_exec_queue *eq, *next;
LIST_HEAD(tmp);
xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)),
xe_exec_queue_is_multi_queue(q));
mutex_lock(&group->list_lock);
group->stopped = true;
list_for_each_entry_safe(eq, next, &group->list, multi_queue.link)
if (xe_exec_queue_get_unless_zero(eq))
list_move_tail(&eq->multi_queue.link, &tmp);
mutex_unlock(&group->list_lock);
xe_sched_submission_stop(&primary->guc->sched);
list_for_each_entry(eq, &tmp, multi_queue.link)
xe_sched_submission_stop(&eq->guc->sched);
mutex_lock(&group->list_lock);
list_for_each_entry_safe(eq, next, &tmp, multi_queue.link) {
if (READ_ONCE(group->banned))
xe_guc_exec_queue_trigger_cleanup(eq);
list_move_tail(&eq->multi_queue.link, &group->list);
xe_exec_queue_put(eq);
}
mutex_unlock(&group->list_lock);
}
static void xe_guc_exec_queue_group_start(struct xe_exec_queue *q)
{
struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
struct xe_exec_queue_group *group = q->multi_queue.group;
struct xe_exec_queue *eq;
xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)),
xe_exec_queue_is_multi_queue(q));
xe_sched_submission_start(&primary->guc->sched);
mutex_lock(&group->list_lock);
group->stopped = false;
list_for_each_entry(eq, &group->list, multi_queue.link)
xe_sched_submission_start(&eq->guc->sched);
mutex_unlock(&group->list_lock);
}
static void xe_guc_exec_queue_group_trigger_cleanup(struct xe_exec_queue *q)
{
struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
struct xe_exec_queue_group *group = q->multi_queue.group;
struct xe_exec_queue *eq;
xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)),
xe_exec_queue_is_multi_queue(q));
WRITE_ONCE(group->banned, true);
xe_guc_exec_queue_trigger_cleanup(primary);
mutex_lock(&group->list_lock);
list_for_each_entry(eq, &group->list, multi_queue.link)
xe_guc_exec_queue_trigger_cleanup(eq);
mutex_unlock(&group->list_lock);
}
static void xe_guc_exec_queue_reset_trigger_cleanup(struct xe_exec_queue *q)
{
if (xe_exec_queue_is_multi_queue(q)) {
struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
struct xe_exec_queue_group *group = q->multi_queue.group;
struct xe_exec_queue *eq;
WRITE_ONCE(group->banned, true);
set_exec_queue_reset(primary);
if (!exec_queue_banned(primary))
xe_guc_exec_queue_trigger_cleanup(primary);
mutex_lock(&group->list_lock);
list_for_each_entry(eq, &group->list, multi_queue.link) {
set_exec_queue_reset(eq);
if (!exec_queue_banned(eq))
xe_guc_exec_queue_trigger_cleanup(eq);
}
mutex_unlock(&group->list_lock);
} else {
set_exec_queue_reset(q);
if (!exec_queue_banned(q))
xe_guc_exec_queue_trigger_cleanup(q);
}
}
static void set_exec_queue_group_banned(struct xe_exec_queue *q)
{
struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
struct xe_exec_queue_group *group = q->multi_queue.group;
struct xe_exec_queue *eq;
xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)),
xe_exec_queue_is_multi_queue(q));
set_exec_queue_banned(primary);
mutex_lock(&group->list_lock);
list_for_each_entry(eq, &group->list, multi_queue.link)
set_exec_queue_banned(eq);
mutex_unlock(&group->list_lock);
}
struct guc_ctxt_registration_info {
u32 flags;
u32 context_idx;
u32 engine_class;
u32 engine_submit_mask;
u32 wq_desc_lo;
u32 wq_desc_hi;
u32 wq_base_lo;
u32 wq_base_hi;
u32 wq_size;
u32 cgp_lo;
u32 cgp_hi;
u32 hwlrca_lo;
u32 hwlrca_hi;
};
#define parallel_read(xe_, map_, field_) \
xe_map_rd_field(xe_, &map_, 0, struct guc_submit_parallel_scratch, \
field_)
#define parallel_write(xe_, map_, field_, val_) \
xe_map_wr_field(xe_, &map_, 0, struct guc_submit_parallel_scratch, \
field_, val_)
#define CGP_VERSION_MAJOR_SHIFT 8
static void xe_guc_exec_queue_group_cgp_update(struct xe_device *xe,
struct xe_exec_queue *q)
{
struct xe_exec_queue_group *group = q->multi_queue.group;
u32 guc_id = group->primary->guc->id;
xe_map_wr(xe, &group->cgp_bo->vmap, 0, u32,
1 << CGP_VERSION_MAJOR_SHIFT);
xe_map_wr(xe, &group->cgp_bo->vmap,
(32 + q->multi_queue.pos * 2) * sizeof(u32),
u32, lower_32_bits(xe_lrc_descriptor(q->lrc[0])));
xe_map_wr(xe, &group->cgp_bo->vmap,
(33 + q->multi_queue.pos * 2) * sizeof(u32),
u32, guc_id);
if (q->multi_queue.pos / 32) {
xe_map_wr(xe, &group->cgp_bo->vmap, 17 * sizeof(u32),
u32, BIT(q->multi_queue.pos % 32));
xe_map_wr(xe, &group->cgp_bo->vmap, 16 * sizeof(u32), u32, 0);
} else {
xe_map_wr(xe, &group->cgp_bo->vmap, 16 * sizeof(u32),
u32, BIT(q->multi_queue.pos));
xe_map_wr(xe, &group->cgp_bo->vmap, 17 * sizeof(u32), u32, 0);
}
}
#define CGP_SYNC_REGISTRATION BIT(0)
static void xe_guc_exec_queue_group_cgp_sync(struct xe_guc *guc,
struct xe_exec_queue *q,
const u32 *action, u32 len,
unsigned int flags)
{
struct xe_exec_queue_group *group = q->multi_queue.group;
struct xe_device *xe = guc_to_xe(guc);
enum xe_multi_queue_priority priority;
long ret;
ret = wait_event_timeout(guc->ct.wq,
!READ_ONCE(group->sync_pending) ||
xe_guc_read_stopped(guc) || vf_recovery(guc),
HZ);
if ((!ret && !vf_recovery(guc)) || xe_guc_read_stopped(guc)) {
xe_gt_warn(guc_to_gt(guc), "Wait for CGP_SYNC_DONE response failed!\n");
set_exec_queue_group_banned(q);
xe_gt_reset_async(q->gt);
xe_guc_exec_queue_group_trigger_cleanup(q);
return;
}
if (vf_recovery(guc)) {
if (flags & CGP_SYNC_REGISTRATION)
q->guc->multi_queue.re_register = true;
else
q->guc->multi_queue.re_update = true;
return;
}
scoped_guard(spinlock, &q->multi_queue.lock)
priority = q->multi_queue.priority;
xe_lrc_set_multi_queue_priority(q->lrc[0], priority);
xe_guc_exec_queue_group_cgp_update(xe, q);
if (flags & CGP_SYNC_REGISTRATION) {
q->guc->multi_queue.registering_cgp = true;
q->guc->multi_queue.updating_cgp = false;
} else {
q->guc->multi_queue.updating_cgp = true;
q->guc->multi_queue.registering_cgp = false;
}
WRITE_ONCE(group->cgp_update_q, q);
WRITE_ONCE(group->sync_pending, true);
xe_guc_ct_send(&guc->ct, action, len, G2H_LEN_DW_MULTI_QUEUE_CONTEXT, 1);
}
static void guc_exec_queue_send_cgp_sync(struct xe_exec_queue *q, unsigned int flags)
{
#define MAX_MULTI_QUEUE_CGP_SYNC_SIZE (2)
struct xe_guc *guc = exec_queue_to_guc(q);
struct xe_exec_queue_group *group = q->multi_queue.group;
u32 action[MAX_MULTI_QUEUE_CGP_SYNC_SIZE];
int len = 0;
action[len++] = XE_GUC_ACTION_MULTI_QUEUE_CONTEXT_CGP_SYNC;
action[len++] = group->primary->guc->id;
xe_gt_assert(guc_to_gt(guc), len <= MAX_MULTI_QUEUE_CGP_SYNC_SIZE);
#undef MAX_MULTI_QUEUE_CGP_SYNC_SIZE
xe_guc_exec_queue_group_cgp_sync(guc, q, action, len, flags);
}
static void __register_exec_queue_group(struct xe_exec_queue *q,
struct guc_ctxt_registration_info *info)
{
struct xe_guc *guc = exec_queue_to_guc(q);
#define MAX_MULTI_QUEUE_REG_SIZE (8)
u32 action[MAX_MULTI_QUEUE_REG_SIZE];
int len = 0;
action[len++] = XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_QUEUE;
action[len++] = info->flags;
action[len++] = info->context_idx;
action[len++] = info->engine_class;
action[len++] = info->engine_submit_mask;
action[len++] = 0;
action[len++] = info->cgp_lo;
action[len++] = info->cgp_hi;
xe_gt_assert(guc_to_gt(guc), len <= MAX_MULTI_QUEUE_REG_SIZE);
#undef MAX_MULTI_QUEUE_REG_SIZE
xe_guc_exec_queue_group_cgp_sync(guc, q, action, len,
CGP_SYNC_REGISTRATION);
}
static void __register_mlrc_exec_queue(struct xe_guc *guc,
struct xe_exec_queue *q,
struct guc_ctxt_registration_info *info)
{
#define MAX_MLRC_REG_SIZE (13 + XE_HW_ENGINE_MAX_INSTANCE * 2)
u32 action[MAX_MLRC_REG_SIZE];
int len = 0;
int i;
xe_gt_assert(guc_to_gt(guc), xe_exec_queue_is_parallel(q));
action[len++] = XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC;
action[len++] = info->flags;
action[len++] = info->context_idx;
action[len++] = info->engine_class;
action[len++] = info->engine_submit_mask;
action[len++] = info->wq_desc_lo;
action[len++] = info->wq_desc_hi;
action[len++] = info->wq_base_lo;
action[len++] = info->wq_base_hi;
action[len++] = info->wq_size;
action[len++] = q->width;
action[len++] = info->hwlrca_lo;
action[len++] = info->hwlrca_hi;
for (i = 1; i < q->width; ++i) {
struct xe_lrc *lrc = q->lrc[i];
action[len++] = lower_32_bits(xe_lrc_descriptor(lrc));
action[len++] = upper_32_bits(xe_lrc_descriptor(lrc));
}
xe_gt_assert(guc_to_gt(guc), info->wq_desc_lo ==
action[XE_GUC_REGISTER_CONTEXT_MULTI_LRC_DATA_5_WQ_DESC_ADDR_LOWER]);
xe_gt_assert(guc_to_gt(guc), info->wq_base_lo ==
action[XE_GUC_REGISTER_CONTEXT_MULTI_LRC_DATA_7_WQ_BUF_BASE_LOWER]);
xe_gt_assert(guc_to_gt(guc), q->width ==
action[XE_GUC_REGISTER_CONTEXT_MULTI_LRC_DATA_10_NUM_CTXS]);
xe_gt_assert(guc_to_gt(guc), info->hwlrca_lo ==
action[XE_GUC_REGISTER_CONTEXT_MULTI_LRC_DATA_11_HW_LRC_ADDR]);
xe_gt_assert(guc_to_gt(guc), len <= MAX_MLRC_REG_SIZE);
#undef MAX_MLRC_REG_SIZE
xe_guc_ct_send(&guc->ct, action, len, 0, 0);
}
static void __register_exec_queue(struct xe_guc *guc,
struct guc_ctxt_registration_info *info)
{
u32 action[] = {
XE_GUC_ACTION_REGISTER_CONTEXT,
info->flags,
info->context_idx,
info->engine_class,
info->engine_submit_mask,
info->wq_desc_lo,
info->wq_desc_hi,
info->wq_base_lo,
info->wq_base_hi,
info->wq_size,
info->hwlrca_lo,
info->hwlrca_hi,
};
xe_gt_assert(guc_to_gt(guc), info->wq_desc_lo ==
action[XE_GUC_REGISTER_CONTEXT_DATA_5_WQ_DESC_ADDR_LOWER]);
xe_gt_assert(guc_to_gt(guc), info->wq_base_lo ==
action[XE_GUC_REGISTER_CONTEXT_DATA_7_WQ_BUF_BASE_LOWER]);
xe_gt_assert(guc_to_gt(guc), info->hwlrca_lo ==
action[XE_GUC_REGISTER_CONTEXT_DATA_10_HW_LRC_ADDR]);
xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0);
}
static u32 xe_hwe_guc_logical_to_submit_mask(struct xe_hw_engine *hwe, u32 logical_mask)
{
struct xe_gt *gt = hwe->gt;
if (xe_gt_is_usm_hwe(gt, hwe)) {
int shift = gt->usm.paging_hwe0->logical_instance;
u32 paging_logical_mask = gt->usm.paging_logical_mask;
xe_gt_assert(gt, (logical_mask & paging_logical_mask) == logical_mask);
if (xe_guc_has_paging_engine(&hwe->gt->uc.guc))
return logical_mask >> shift;
}
return logical_mask;
}
static void register_exec_queue(struct xe_exec_queue *q, int ctx_type)
{
struct xe_guc *guc = exec_queue_to_guc(q);
struct xe_device *xe = guc_to_xe(guc);
struct xe_lrc *lrc = q->lrc[0];
struct guc_ctxt_registration_info info;
xe_gt_assert(guc_to_gt(guc), !exec_queue_registered(q));
xe_gt_assert(guc_to_gt(guc), ctx_type < GUC_CONTEXT_COUNT);
memset(&info, 0, sizeof(info));
info.context_idx = q->guc->id;
info.engine_class = xe_hwe_to_guc_class(q->hwe);
info.engine_submit_mask =
xe_hwe_guc_logical_to_submit_mask(q->hwe, q->logical_mask);
info.hwlrca_lo = lower_32_bits(xe_lrc_descriptor(lrc));
info.hwlrca_hi = upper_32_bits(xe_lrc_descriptor(lrc));
info.flags = CONTEXT_REGISTRATION_FLAG_KMD |
FIELD_PREP(CONTEXT_REGISTRATION_FLAG_TYPE, ctx_type);
if (xe_exec_queue_is_multi_queue(q)) {
struct xe_exec_queue_group *group = q->multi_queue.group;
info.cgp_lo = xe_bo_ggtt_addr(group->cgp_bo);
info.cgp_hi = 0;
}
if (xe_exec_queue_is_parallel(q)) {
u64 ggtt_addr = xe_lrc_parallel_ggtt_addr(lrc);
struct iosys_map map = xe_lrc_parallel_map(lrc);
info.wq_desc_lo = lower_32_bits(ggtt_addr +
offsetof(struct guc_submit_parallel_scratch, wq_desc));
info.wq_desc_hi = upper_32_bits(ggtt_addr +
offsetof(struct guc_submit_parallel_scratch, wq_desc));
info.wq_base_lo = lower_32_bits(ggtt_addr +
offsetof(struct guc_submit_parallel_scratch, wq[0]));
info.wq_base_hi = upper_32_bits(ggtt_addr +
offsetof(struct guc_submit_parallel_scratch, wq[0]));
info.wq_size = WQ_SIZE;
q->guc->wqi_head = 0;
q->guc->wqi_tail = 0;
xe_map_memset(xe, &map, 0, 0, PARALLEL_SCRATCH_SIZE - WQ_SIZE);
parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE);
}
set_exec_queue_registered(q);
trace_xe_exec_queue_register(q);
if (xe_exec_queue_is_multi_queue_primary(q))
__register_exec_queue_group(q, &info);
else if (xe_exec_queue_is_parallel(q))
__register_mlrc_exec_queue(guc, q, &info);
else if (!xe_exec_queue_is_multi_queue_secondary(q))
__register_exec_queue(guc, &info);
if (!xe_exec_queue_is_multi_queue_secondary(q))
init_policies(guc, q);
if (xe_exec_queue_is_multi_queue_secondary(q))
guc_exec_queue_send_cgp_sync(q, CGP_SYNC_REGISTRATION);
}
static u32 wq_space_until_wrap(struct xe_exec_queue *q)
{
return (WQ_SIZE - q->guc->wqi_tail);
}
static int wq_wait_for_space(struct xe_exec_queue *q, u32 wqi_size)
{
struct xe_guc *guc = exec_queue_to_guc(q);
struct xe_device *xe = guc_to_xe(guc);
struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]);
unsigned int sleep_period_ms = 1, sleep_total_ms = 0;
#define AVAILABLE_SPACE \
CIRC_SPACE(q->guc->wqi_tail, q->guc->wqi_head, WQ_SIZE)
if (wqi_size > AVAILABLE_SPACE && !vf_recovery(guc)) {
try_again:
q->guc->wqi_head = parallel_read(xe, map, wq_desc.head);
if (wqi_size > AVAILABLE_SPACE && !vf_recovery(guc)) {
if (sleep_total_ms > 2000) {
xe_gt_reset_async(q->gt);
return -ENODEV;
}
sleep_total_ms += xe_sleep_exponential_ms(&sleep_period_ms, 64);
goto try_again;
}
}
#undef AVAILABLE_SPACE
return 0;
}
static int wq_noop_append(struct xe_exec_queue *q)
{
struct xe_guc *guc = exec_queue_to_guc(q);
struct xe_device *xe = guc_to_xe(guc);
struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]);
u32 len_dw = wq_space_until_wrap(q) / sizeof(u32) - 1;
if (wq_wait_for_space(q, wq_space_until_wrap(q)))
return -ENODEV;
xe_gt_assert(guc_to_gt(guc), FIELD_FIT(WQ_LEN_MASK, len_dw));
parallel_write(xe, map, wq[q->guc->wqi_tail / sizeof(u32)],
FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) |
FIELD_PREP(WQ_LEN_MASK, len_dw));
q->guc->wqi_tail = 0;
return 0;
}
static void wq_item_append(struct xe_exec_queue *q)
{
struct xe_guc *guc = exec_queue_to_guc(q);
struct xe_device *xe = guc_to_xe(guc);
struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]);
#define WQ_HEADER_SIZE 4 /* Includes 1 LRC address too */
u32 wqi[XE_HW_ENGINE_MAX_INSTANCE + (WQ_HEADER_SIZE - 1)];
u32 wqi_size = (q->width + (WQ_HEADER_SIZE - 1)) * sizeof(u32);
u32 len_dw = (wqi_size / sizeof(u32)) - 1;
int i = 0, j;
if (wqi_size > wq_space_until_wrap(q)) {
if (wq_noop_append(q))
return;
}
if (wq_wait_for_space(q, wqi_size))
return;
wqi[i++] = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_MULTI_LRC) |
FIELD_PREP(WQ_LEN_MASK, len_dw);
wqi[i++] = xe_lrc_descriptor(q->lrc[0]);
wqi[i++] = FIELD_PREP(WQ_GUC_ID_MASK, q->guc->id) |
FIELD_PREP(WQ_RING_TAIL_MASK, q->lrc[0]->ring.tail / sizeof(u64));
wqi[i++] = 0;
for (j = 1; j < q->width; ++j) {
struct xe_lrc *lrc = q->lrc[j];
wqi[i++] = lrc->ring.tail / sizeof(u64);
}
xe_gt_assert(guc_to_gt(guc), i == wqi_size / sizeof(u32));
iosys_map_incr(&map, offsetof(struct guc_submit_parallel_scratch,
wq[q->guc->wqi_tail / sizeof(u32)]));
xe_map_memcpy_to(xe, &map, 0, wqi, wqi_size);
q->guc->wqi_tail += wqi_size;
xe_gt_assert(guc_to_gt(guc), q->guc->wqi_tail <= WQ_SIZE);
xe_device_wmb(xe);
map = xe_lrc_parallel_map(q->lrc[0]);
parallel_write(xe, map, wq_desc.tail, q->guc->wqi_tail);
}
#define RESUME_PENDING ~0x0ull
static void submit_exec_queue(struct xe_exec_queue *q, struct xe_sched_job *job)
{
struct xe_guc *guc = exec_queue_to_guc(q);
struct xe_lrc *lrc = q->lrc[0];
u32 action[3];
u32 g2h_len = 0;
u32 num_g2h = 0;
int len = 0;
bool extra_submit = false;
xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q));
if (!job->restore_replay || job->last_replay) {
if (xe_exec_queue_is_parallel(q))
wq_item_append(q);
else if (!xe_sched_job_ulls_is_chained(job) ||
!exec_queue_enabled(q))
xe_lrc_set_ring_tail(lrc, lrc->ring.tail);
job->last_replay = false;
}
if (exec_queue_suspended(q) && !xe_exec_queue_is_parallel(q))
return;
q = xe_exec_queue_multi_queue_primary(q);
if (exec_queue_suspended(q))
return;
if (xe_sched_job_ulls_is_chained(job))
xe_lrc_set_ulls_semaphore(lrc, xe_sched_job_lrc_seqno(job));
if (!exec_queue_enabled(q)) {
action[len++] = XE_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
action[len++] = q->guc->id;
action[len++] = GUC_CONTEXT_ENABLE;
g2h_len = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET;
num_g2h = 1;
if (xe_exec_queue_is_parallel(q))
extra_submit = true;
q->guc->resume_time = RESUME_PENDING;
set_exec_queue_pending_enable(q);
set_exec_queue_enabled(q);
trace_xe_exec_queue_scheduling_enable(q);
} else if (!xe_sched_job_ulls_is_chained(job)) {
action[len++] = XE_GUC_ACTION_SCHED_CONTEXT;
action[len++] = q->guc->id;
trace_xe_exec_queue_submit(q);
}
if (!xe_sched_job_ulls_is_chained(job) || num_g2h)
xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
if (extra_submit) {
len = 0;
action[len++] = XE_GUC_ACTION_SCHED_CONTEXT;
action[len++] = q->guc->id;
trace_xe_exec_queue_submit(q);
xe_guc_ct_send(&guc->ct, action, len, 0, 0);
}
}
static bool is_pt_job(struct xe_sched_job *job)
{
return job->is_pt_job;
}
static void run_pt_job(struct xe_device *xe, struct xe_sched_job *job,
bool force_clear)
{
struct xe_tile *tile;
int id;
for_each_tile(tile, xe, id) {
struct xe_pt_job_ops *pt_job_ops =
job->pt_update[0].pt_job_ops[id];
if (!pt_job_ops || !pt_job_ops->current_op)
continue;
xe_cpu_bind_update_pgtables_execute(job->pt_update[0].vm, tile,
job->pt_update[0].ops,
pt_job_ops->ops,
pt_job_ops->current_op,
force_clear);
}
}
static void put_pt_job(struct xe_device *xe, struct xe_sched_job *job)
{
struct xe_tile *tile;
int id;
for_each_tile(tile, xe, id) {
struct xe_pt_job_ops *pt_job_ops =
job->pt_update[0].pt_job_ops[id];
xe_pt_job_ops_put(pt_job_ops);
}
}
static struct dma_fence *
guc_exec_queue_run_job(struct drm_sched_job *drm_job)
{
struct xe_sched_job *job = to_xe_sched_job(drm_job);
struct xe_exec_queue *q = job->q;
struct xe_guc *guc = exec_queue_to_guc(q);
bool killed_or_banned_or_wedged_or_error =
exec_queue_killed_or_banned_or_wedged(q) ||
xe_sched_job_is_error(job);
xe_gt_assert(guc_to_gt(guc), !(exec_queue_destroyed(q) || exec_queue_pending_disable(q)) ||
exec_queue_banned(q) || exec_queue_suspended(q));
trace_xe_sched_job_run(job);
if (is_pt_job(job)) {
xe_gt_assert(guc_to_gt(guc), !exec_queue_registered(q));
run_pt_job(guc_to_xe(guc), job,
killed_or_banned_or_wedged_or_error);
put_pt_job(guc_to_xe(guc), job);
dma_fence_put(job->fence);
return NULL;
} else if (!killed_or_banned_or_wedged_or_error) {
if (xe_exec_queue_is_multi_queue_secondary(q)) {
struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
if (exec_queue_killed_or_banned_or_wedged(primary))
goto run_job_out;
if (!exec_queue_registered(primary))
register_exec_queue(primary, GUC_CONTEXT_NORMAL);
}
if (!exec_queue_registered(q))
register_exec_queue(q, GUC_CONTEXT_NORMAL);
if (!job->restore_replay)
q->ring_ops->emit_job(job);
submit_exec_queue(q, job);
job->restore_replay = false;
}
run_job_out:
return job->fence;
}
static void guc_exec_queue_free_job(struct drm_sched_job *drm_job)
{
struct xe_sched_job *job = to_xe_sched_job(drm_job);
trace_xe_sched_job_free(job);
xe_sched_job_put(job);
}
int xe_guc_read_stopped(struct xe_guc *guc)
{
return atomic_read(&guc->submission_state.stopped);
}
static void handle_multi_queue_secondary_sched_done(struct xe_guc *guc,
struct xe_exec_queue *q,
u32 runnable_state);
static void handle_deregister_done(struct xe_guc *guc, struct xe_exec_queue *q);
#define MAKE_SCHED_CONTEXT_ACTION(q, enable_disable) \
u32 action[] = { \
XE_GUC_ACTION_SCHED_CONTEXT_MODE_SET, \
q->guc->id, \
GUC_CONTEXT_##enable_disable, \
}
static void disable_scheduling_deregister(struct xe_guc *guc,
struct xe_exec_queue *q)
{
MAKE_SCHED_CONTEXT_ACTION(q, DISABLE);
int ret;
if (!xe_exec_queue_is_multi_queue_secondary(q))
set_min_preemption_timeout(guc, q);
smp_rmb();
ret = wait_event_timeout(guc->ct.wq,
(!exec_queue_pending_enable(q) &&
!exec_queue_pending_disable(q)) ||
xe_guc_read_stopped(guc) ||
vf_recovery(guc),
HZ * 5);
if (!ret && !vf_recovery(guc)) {
struct xe_gpu_scheduler *sched = &q->guc->sched;
xe_gt_warn(q->gt, "Pending enable/disable failed to respond\n");
xe_sched_submission_start(sched);
xe_gt_reset_async(q->gt);
xe_sched_tdr_queue_imm(sched);
return;
}
clear_exec_queue_enabled(q);
set_exec_queue_pending_disable(q);
set_exec_queue_destroyed(q);
trace_xe_exec_queue_scheduling_disable(q);
if (xe_exec_queue_is_multi_queue_secondary(q))
handle_multi_queue_secondary_sched_done(guc, q, 0);
else
xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
G2H_LEN_DW_SCHED_CONTEXT_MODE_SET +
G2H_LEN_DW_DEREGISTER_CONTEXT, 2);
}
void xe_guc_submit_wedge(struct xe_guc *guc)
{
struct xe_device *xe = guc_to_xe(guc);
struct xe_exec_queue *q;
unsigned long index;
xe_gt_assert(guc_to_gt(guc), guc_to_xe(guc)->wedged.mode);
if (!guc->submission_state.initialized)
return;
if (xe->wedged.mode == XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) {
mutex_lock(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
if (xe_exec_queue_get_unless_zero(q))
set_exec_queue_wedged(q);
mutex_unlock(&guc->submission_state.lock);
} else {
guc_submit_reset_prepare(guc);
xe_guc_submit_stop(guc);
xe_guc_softreset(guc);
xe_uc_fw_sanitize(&guc->fw);
xe_guc_submit_pause_abort(guc);
}
}
static bool guc_submit_hint_wedged(struct xe_guc *guc)
{
struct xe_device *xe = guc_to_xe(guc);
if (xe->wedged.mode != XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET)
return false;
if (xe_device_wedged(xe))
return true;
xe_device_declare_wedged(xe, DRM_WEDGE_RECOVERY_REBIND | DRM_WEDGE_RECOVERY_BUS_RESET);
return true;
}
#define ADJUST_FIVE_PERCENT(__t) mul_u64_u32_div(__t, 105, 100)
static bool check_timeout(struct xe_exec_queue *q, struct xe_sched_job *job)
{
struct xe_gt *gt = guc_to_gt(exec_queue_to_guc(q));
u32 ctx_timestamp, ctx_job_timestamp;
u32 timeout_ms = q->sched_props.job_timeout_ms;
u32 diff;
u64 running_time_ms;
if (!xe_sched_job_started(job)) {
xe_gt_warn(gt, "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, not started",
xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job),
q->guc->id);
return true;
}
ctx_timestamp = lower_32_bits(xe_lrc_timestamp(q->lrc[0]));
if (ctx_timestamp == job->sample_timestamp) {
if (IS_SRIOV_VF(gt_to_xe(gt)))
xe_gt_notice(gt, "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, timestamp stuck",
xe_sched_job_seqno(job),
xe_sched_job_lrc_seqno(job), q->guc->id);
else
xe_gt_warn(gt, "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, timestamp stuck",
xe_sched_job_seqno(job),
xe_sched_job_lrc_seqno(job), q->guc->id);
return xe_sched_invalidate_job(job, 0);
}
job->sample_timestamp = ctx_timestamp;
ctx_job_timestamp = xe_lrc_ctx_job_timestamp(q->lrc[0]);
xe_gt_assert(gt, timeout_ms < 100 * MSEC_PER_SEC);
diff = ctx_timestamp - ctx_job_timestamp;
running_time_ms =
ADJUST_FIVE_PERCENT(xe_gt_clock_interval_to_ms(gt, diff));
xe_gt_dbg(gt,
"Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, running_time_ms=%llu, timeout_ms=%u, diff=0x%08x",
xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job),
q->guc->id, running_time_ms, timeout_ms, diff);
return running_time_ms >= timeout_ms;
}
static void enable_scheduling(struct xe_exec_queue *q)
{
MAKE_SCHED_CONTEXT_ACTION(q, ENABLE);
struct xe_guc *guc = exec_queue_to_guc(q);
int ret;
xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q));
xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q));
xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q));
xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_enable(q));
set_exec_queue_pending_enable(q);
set_exec_queue_enabled(q);
trace_xe_exec_queue_scheduling_enable(q);
if (xe_exec_queue_is_multi_queue_secondary(q))
handle_multi_queue_secondary_sched_done(guc, q, 1);
else
xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1);
ret = wait_event_timeout(guc->ct.wq,
!exec_queue_pending_enable(q) ||
xe_guc_read_stopped(guc) ||
vf_recovery(guc), HZ * 5);
if ((!ret && !vf_recovery(guc)) || xe_guc_read_stopped(guc)) {
xe_gt_warn(guc_to_gt(guc), "Schedule enable failed to respond");
set_exec_queue_banned(q);
xe_gt_reset_async(q->gt);
xe_sched_tdr_queue_imm(&q->guc->sched);
}
}
static void disable_scheduling(struct xe_exec_queue *q, bool immediate)
{
MAKE_SCHED_CONTEXT_ACTION(q, DISABLE);
struct xe_guc *guc = exec_queue_to_guc(q);
xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q));
xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q));
xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q));
if (immediate && !xe_exec_queue_is_multi_queue_secondary(q))
set_min_preemption_timeout(guc, q);
clear_exec_queue_enabled(q);
set_exec_queue_pending_disable(q);
trace_xe_exec_queue_scheduling_disable(q);
if (xe_exec_queue_is_multi_queue_secondary(q))
handle_multi_queue_secondary_sched_done(guc, q, 0);
else
xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1);
}
static bool timeout_needs_gt_reset(struct xe_exec_queue *q, struct xe_sched_job *job,
bool skip_timeout_check)
{
if (q->flags & EXEC_QUEUE_FLAG_KERNEL)
return true;
return !skip_timeout_check && !xe_sched_job_started(job);
}
static enum drm_gpu_sched_stat
guc_exec_queue_timedout_job(struct drm_sched_job *drm_job)
{
struct xe_sched_job *job = to_xe_sched_job(drm_job);
struct drm_sched_job *tmp_job;
struct xe_exec_queue *q = job->q, *primary;
struct xe_gpu_scheduler *sched = &q->guc->sched;
struct xe_guc *guc = exec_queue_to_guc(q);
const char *process_name = "no process";
struct xe_device *xe = guc_to_xe(guc);
int err = -ETIME;
pid_t pid = -1;
bool wedged = false, wedge_device = false, skip_timeout_check;
int idx;
xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q));
primary = xe_exec_queue_multi_queue_primary(q);
if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &job->fence->flags) ||
vf_recovery(guc))
return DRM_GPU_SCHED_STAT_NO_HANG;
if (!drm_dev_enter(&xe->drm, &idx)) {
xe_sched_job_set_error(job, -ENODEV);
return DRM_GPU_SCHED_STAT_NO_HANG;
}
if (xe_exec_queue_is_multi_queue(q))
xe_guc_exec_queue_group_stop(q);
else
xe_sched_submission_stop(sched);
skip_timeout_check = exec_queue_reset(q) ||
exec_queue_killed_or_banned_or_wedged(q);
if (xe_exec_queue_is_multi_queue(q) &&
READ_ONCE(q->multi_queue.group->banned))
skip_timeout_check = true;
if (xe_exec_queue_is_lr(q))
xe_gt_assert(guc_to_gt(guc), skip_timeout_check);
if (!xe_device_is_in_reset(xe) && !exec_queue_killed(q) && !xe->devcoredump.captured &&
!xe_guc_capture_get_matching_and_lock(q)) {
CLASS(xe_force_wake, fw_ref)(gt_to_fw(q->gt), XE_FORCEWAKE_ALL);
if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL))
xe_gt_info(q->gt, "failed to get forcewake for coredump capture\n");
xe_engine_snapshot_capture_for_queue(q);
}
if (!skip_timeout_check && !check_timeout(q, job))
goto rearm;
if (!exec_queue_killed(q))
wedged = guc_submit_hint_wedged(exec_queue_to_guc(q));
else
wedged = xe_device_wedged(xe);
if (!exec_queue_killed(q))
atomic_or(DRM_XE_EXEC_QUEUE_BAN_REASON_GPU_HANG, &q->ban_reason);
set_exec_queue_banned(q);
if (!xe_device_is_in_reset(xe) && !wedged &&
(exec_queue_enabled(primary) || exec_queue_pending_disable(primary))) {
int ret;
if (exec_queue_reset(primary))
err = -EIO;
if (xe_uc_fw_is_running(&guc->fw)) {
ret = wait_event_timeout(guc->ct.wq,
(!exec_queue_pending_enable(primary) &&
!exec_queue_pending_disable(primary)) ||
xe_guc_read_stopped(guc) ||
vf_recovery(guc), HZ * 5);
if (vf_recovery(guc))
goto handle_vf_resume;
if (!ret || xe_guc_read_stopped(guc))
goto trigger_reset;
disable_scheduling(primary, skip_timeout_check);
}
smp_rmb();
ret = wait_event_timeout(guc->ct.wq,
!xe_uc_fw_is_running(&guc->fw) ||
!exec_queue_pending_disable(primary) ||
xe_guc_read_stopped(guc) ||
vf_recovery(guc), HZ * 5);
if (vf_recovery(guc))
goto handle_vf_resume;
if (!ret || xe_guc_read_stopped(guc)) {
trigger_reset:
if (!ret)
xe_gt_warn(guc_to_gt(guc),
"Schedule disable failed to respond, guc_id=%d",
primary->guc->id);
xe_devcoredump(primary, job,
"Schedule disable failed to respond, guc_id=%d, ret=%d, guc_read=%d",
primary->guc->id, ret, xe_guc_read_stopped(guc));
xe_gt_reset_async(primary->gt);
xe_sched_tdr_queue_imm(sched);
goto rearm;
}
}
if (q->vm && q->vm->xef) {
process_name = q->vm->xef->process_name;
pid = q->vm->xef->pid;
}
if (!exec_queue_killed(q))
xe_gt_notice(guc_to_gt(guc),
"Timedout job: seqno=%u, lrc_seqno=%u, guc_id=%d, flags=0x%lx in %s [%d]",
xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job),
q->guc->id, q->flags, process_name, pid);
trace_xe_sched_job_timedout(job);
if (!xe_device_is_in_reset(xe) && !exec_queue_killed(q))
xe_devcoredump(q, job,
"Timedout job - seqno=%u, lrc_seqno=%u, guc_id=%d, flags=0x%lx",
xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job),
q->guc->id, q->flags);
if (!wedged) {
if (timeout_needs_gt_reset(q, job, skip_timeout_check)) {
if (!xe_sched_invalidate_job(job, 2)) {
clear_exec_queue_banned(q);
atomic_andnot(DRM_XE_EXEC_QUEUE_BAN_REASON_GPU_HANG,
&q->ban_reason);
xe_gt_reset_async(q->gt);
goto rearm;
}
if (q->flags & EXEC_QUEUE_FLAG_KERNEL) {
xe_gt_WARN(q->gt, true, "Kernel-submitted job timed out\n");
wedge_device = true;
}
} else if (q->flags & EXEC_QUEUE_FLAG_VM && !exec_queue_killed(q)) {
xe_gt_WARN(q->gt, true, "VM job timed out on non-killed execqueue\n");
}
}
xe_sched_job_set_error(job, err);
drm_sched_for_each_pending_job(tmp_job, &sched->base, NULL)
xe_sched_job_set_error(to_xe_sched_job(tmp_job), -ECANCELED);
if (xe_exec_queue_is_multi_queue(q)) {
xe_guc_exec_queue_group_start(q);
xe_guc_exec_queue_group_trigger_cleanup(q);
} else {
xe_sched_submission_start(sched);
xe_guc_exec_queue_trigger_cleanup(q);
}
if (wedge_device)
xe_device_declare_wedged(gt_to_xe(q->gt),
DRM_WEDGE_RECOVERY_REBIND | DRM_WEDGE_RECOVERY_BUS_RESET);
drm_dev_exit(idx);
return DRM_GPU_SCHED_STAT_NO_HANG;
rearm:
if (xe_exec_queue_is_multi_queue(q))
xe_guc_exec_queue_group_start(q);
else
xe_sched_submission_start(sched);
handle_vf_resume:
drm_dev_exit(idx);
return DRM_GPU_SCHED_STAT_NO_HANG;
}
static void guc_exec_queue_multi_queue_drop_suspend(struct xe_exec_queue *q);
static int guc_exec_queue_suspend_wait_blocking(struct xe_exec_queue *q);
static void guc_exec_queue_fini(struct xe_exec_queue *q)
{
struct xe_guc_exec_queue *ge = q->guc;
struct xe_guc *guc = exec_queue_to_guc(q);
struct drm_device *drm = &guc_to_xe(guc)->drm;
if (xe_exec_queue_is_multi_queue_secondary(q)) {
if (READ_ONCE(q->guc->suspend_count))
guc_exec_queue_suspend_wait_blocking(q);
guc_exec_queue_multi_queue_drop_suspend(q);
}
if (xe_exec_queue_is_multi_queue_secondary(q)) {
struct xe_exec_queue_group *group = q->multi_queue.group;
mutex_lock(&group->list_lock);
list_del(&q->multi_queue.link);
mutex_unlock(&group->list_lock);
}
release_guc_id(guc, q);
xe_sched_entity_fini(&ge->entity);
xe_sched_fini(&ge->sched);
kfree_rcu(ge, rcu);
drm_dev_put(drm);
}
static void guc_exec_queue_do_destroy(struct xe_exec_queue *q)
{
struct xe_guc_exec_queue *ge = q->guc;
struct xe_guc *guc = exec_queue_to_guc(q);
struct xe_device *xe = guc_to_xe(guc);
struct drm_device *drm = &xe->drm;
drm_dev_get(drm);
scoped_guard(xe_pm_runtime, xe) {
trace_xe_exec_queue_destroy(q);
cancel_delayed_work_sync(&ge->sched.base.work_tdr);
xe_exec_queue_fini(q);
}
drm_dev_put(drm);
}
static void __guc_exec_queue_destroy_async(struct work_struct *w)
{
struct xe_guc_exec_queue *ge =
container_of(w, struct xe_guc_exec_queue, destroy_async);
guc_exec_queue_do_destroy(ge->q);
}
static void guc_exec_queue_destroy_async(struct xe_exec_queue *q)
{
INIT_WORK(&q->guc->destroy_async, __guc_exec_queue_destroy_async);
xe_destroy_wq_queue(&q->guc->destroy_async);
}
static void __guc_exec_queue_process_msg_cleanup(struct xe_sched_msg *msg,
bool bound)
{
struct xe_exec_queue *q = msg->private_data;
struct xe_guc *guc = exec_queue_to_guc(q);
trace_xe_exec_queue_cleanup_entity(q);
if (bound && !exec_queue_wedged(q) && exec_queue_registered(q) &&
xe_uc_fw_is_running(&guc->fw))
disable_scheduling_deregister(guc, q);
else
guc_exec_queue_destroy_async(q);
}
static bool guc_exec_queue_allowed_to_change_state(struct xe_exec_queue *q)
{
return !exec_queue_killed_or_banned_or_wedged(q) && exec_queue_registered(q);
}
static void __guc_exec_queue_process_msg_set_sched_props(struct xe_sched_msg *msg,
bool bound)
{
struct xe_exec_queue *q = msg->private_data;
struct xe_guc *guc = exec_queue_to_guc(q);
if (guc_exec_queue_allowed_to_change_state(q) && bound)
init_policies(guc, q);
kfree(msg);
}
static void suspend_wait_wake(struct xe_exec_queue *q)
{
struct xe_guc *guc = exec_queue_to_guc(q);
struct xe_device *xe = guc_to_xe(guc);
if (IS_SRIOV_VF(xe))
wake_up_all(&guc->ct.wq);
else
wake_up(&q->guc->suspend_wait);
}
static void __suspend_fence_signal(struct xe_exec_queue *q)
{
if (!q->guc->suspend_pending)
return;
WRITE_ONCE(q->guc->suspend_pending, false);
suspend_wait_wake(q);
}
static void suspend_fence_signal(struct xe_exec_queue *q)
{
struct xe_guc *guc = exec_queue_to_guc(q);
xe_gt_assert(guc_to_gt(guc), exec_queue_suspended(q) || exec_queue_killed(q) ||
xe_guc_read_stopped(guc));
xe_gt_assert(guc_to_gt(guc), q->guc->suspend_pending);
__suspend_fence_signal(q);
}
static void __guc_exec_queue_process_msg_suspend(struct xe_sched_msg *msg,
bool bound)
{
struct xe_exec_queue *q = msg->private_data;
struct xe_guc *guc = exec_queue_to_guc(q);
if (guc_exec_queue_allowed_to_change_state(q) && !exec_queue_suspended(q) &&
exec_queue_enabled(q) && bound) {
wait_event(guc->ct.wq, vf_recovery(guc) ||
((q->guc->resume_time != RESUME_PENDING ||
xe_guc_read_stopped(guc)) && !exec_queue_pending_disable(q)));
if (!xe_guc_read_stopped(guc)) {
s64 since_resume_ms =
ktime_ms_delta(ktime_get(),
q->guc->resume_time);
s64 wait_ms = q->vm->preempt.min_run_period_ms -
since_resume_ms;
if (wait_ms > 0 && q->guc->resume_time)
xe_sleep_relaxed_ms(wait_ms);
set_exec_queue_suspended(q);
disable_scheduling(q, false);
}
} else if (q->guc->suspend_pending) {
set_exec_queue_suspended(q);
suspend_fence_signal(q);
}
}
static void __guc_exec_queue_process_msg_resume(struct xe_sched_msg *msg,
bool bound)
{
struct xe_exec_queue *q = msg->private_data;
if (guc_exec_queue_allowed_to_change_state(q) && bound) {
clear_exec_queue_suspended(q);
if (!exec_queue_enabled(q)) {
q->guc->resume_time = RESUME_PENDING;
set_exec_queue_pending_resume(q);
enable_scheduling(q);
}
} else {
clear_exec_queue_suspended(q);
}
}
static void __guc_exec_queue_process_msg_set_multi_queue_priority(struct xe_sched_msg *msg,
bool bound)
{
struct xe_exec_queue *q = msg->private_data;
if (guc_exec_queue_allowed_to_change_state(q) && bound)
guc_exec_queue_send_cgp_sync(q, 0);
kfree(msg);
}
static void __guc_exec_queue_process_msg_cgp_sync(struct xe_sched_msg *msg,
bool bound)
{
struct xe_exec_queue *q = msg->private_data;
if (guc_exec_queue_allowed_to_change_state(q) && bound)
guc_exec_queue_send_cgp_sync(q, 0);
}
#define CLEANUP 1 /* Non-zero values to catch uninitialized msg */
#define SET_SCHED_PROPS 2
#define SUSPEND 3
#define RESUME 4
#define SET_MULTI_QUEUE_PRIORITY 5
#define CGP_SYNC_MSG 6
#define OPCODE_MASK 0xf
#define MSG_LOCKED BIT(8)
#define MSG_HEAD BIT(9)
#define MSG_PM_REF BIT(10)
static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)
{
struct xe_device *xe = guc_to_xe(exec_queue_to_guc(msg->private_data));
int idx;
bool pm_ref = !!(msg->opcode & MSG_PM_REF);
bool bound = drm_dev_enter(&xe->drm, &idx);
trace_xe_sched_msg_recv(msg);
switch (msg->opcode & OPCODE_MASK) {
case CLEANUP:
__guc_exec_queue_process_msg_cleanup(msg, bound);
break;
case SET_SCHED_PROPS:
__guc_exec_queue_process_msg_set_sched_props(msg, bound);
break;
case SUSPEND:
__guc_exec_queue_process_msg_suspend(msg, bound);
break;
case RESUME:
__guc_exec_queue_process_msg_resume(msg, bound);
break;
case SET_MULTI_QUEUE_PRIORITY:
__guc_exec_queue_process_msg_set_multi_queue_priority(msg, bound);
break;
case CGP_SYNC_MSG:
__guc_exec_queue_process_msg_cgp_sync(msg, bound);
break;
default:
XE_WARN_ON("Unknown message type");
}
if (pm_ref)
xe_pm_runtime_put(xe);
if (bound)
drm_dev_exit(idx);
}
static const struct drm_sched_backend_ops drm_sched_ops = {
.run_job = guc_exec_queue_run_job,
.free_job = guc_exec_queue_free_job,
.timedout_job = guc_exec_queue_timedout_job,
};
static const struct xe_sched_backend_ops xe_sched_ops = {
.process_msg = guc_exec_queue_process_msg,
};
static int guc_exec_queue_init(struct xe_exec_queue *q)
{
struct xe_gpu_scheduler *sched;
struct xe_guc *guc = exec_queue_to_guc(q);
struct drm_device *drm = &guc_to_xe(guc)->drm;
struct workqueue_struct *submit_wq = NULL;
struct xe_guc_exec_queue *ge;
long timeout;
int err, i;
int max_jobs = (xe_lrc_ring_size() / MAX_JOB_SIZE_BYTES);
xe_gt_assert(guc_to_gt(guc), xe_device_uc_enabled(guc_to_xe(guc)));
ge = kzalloc_obj(*ge);
if (!ge)
return -ENOMEM;
drm_dev_get(drm);
q->guc = ge;
ge->q = q;
init_rcu_head(&ge->rcu);
init_waitqueue_head(&ge->suspend_wait);
for (i = 0; i < MAX_STATIC_MSG_TYPE; ++i)
INIT_LIST_HEAD(&ge->static_msgs[i].link);
timeout = (q->vm && xe_vm_in_lr_mode(q->vm)) ? MAX_SCHEDULE_TIMEOUT :
msecs_to_jiffies(q->sched_props.job_timeout_ms);
err = alloc_guc_id(guc, q);
if (err)
goto err_free;
xe_exec_queue_assign_name(q, q->guc->id);
strscpy(ge->name, q->name, sizeof(ge->name));
if (xe_exec_queue_is_multi_queue_secondary(q)) {
struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
submit_wq = primary->guc->sched.base.submit_wq;
}
if (q->vm && q->vm->flags & XE_VM_FLAG_MIGRATION)
max_jobs = min(max_jobs, LRC_MIGRATION_ULLS_SEMAPHORE_COUNT - 1);
err = xe_sched_init(&ge->sched, &drm_sched_ops, &xe_sched_ops,
submit_wq, max_jobs, 64,
timeout, guc_to_gt(guc)->ordered_wq, NULL,
ge->name, gt_to_xe(q->gt)->drm.dev);
if (err)
goto err_release_id;
sched = &ge->sched;
err = xe_sched_entity_init(&ge->entity, sched);
if (err)
goto err_sched;
q->entity = &ge->entity;
mutex_lock(&guc->submission_state.lock);
if (xe_guc_read_stopped(guc) || vf_recovery(guc))
xe_sched_stop(sched);
publish_guc_id(guc, q);
mutex_unlock(&guc->submission_state.lock);
if (xe_exec_queue_is_multi_queue_secondary(q)) {
struct xe_exec_queue_group *group = q->multi_queue.group;
INIT_LIST_HEAD(&q->multi_queue.link);
mutex_lock(&group->list_lock);
if (group->stopped)
WRITE_ONCE(q->guc->sched.base.pause_submit, true);
list_add_tail(&q->multi_queue.link, &group->list);
mutex_unlock(&group->list_lock);
}
if (xe_exec_queue_is_multi_queue(q))
trace_xe_exec_queue_create_multi_queue(q);
else
trace_xe_exec_queue_create(q);
return 0;
err_sched:
xe_sched_fini(&ge->sched);
err_release_id:
release_guc_id(guc, q);
err_free:
kfree(ge);
drm_dev_put(drm);
return err;
}
static void guc_exec_queue_kill(struct xe_exec_queue *q)
{
trace_xe_exec_queue_kill(q);
set_exec_queue_killed(q);
suspend_wait_wake(q);
xe_guc_exec_queue_trigger_cleanup(q);
}
static void guc_exec_queue_add_msg(struct xe_exec_queue *q, struct xe_sched_msg *msg,
u32 opcode)
{
struct xe_device *xe = guc_to_xe(exec_queue_to_guc(q));
int idx;
bool bound = drm_dev_enter(&xe->drm, &idx);
INIT_LIST_HEAD(&msg->link);
msg->opcode = opcode & OPCODE_MASK;
if (bound) {
xe_pm_runtime_get_noresume(xe);
msg->opcode |= MSG_PM_REF;
}
msg->private_data = q;
trace_xe_sched_msg_add(msg);
if (opcode & MSG_HEAD)
xe_sched_add_msg_head(&q->guc->sched, msg);
else if (opcode & MSG_LOCKED)
xe_sched_add_msg_locked(&q->guc->sched, msg);
else
xe_sched_add_msg(&q->guc->sched, msg);
if (bound)
drm_dev_exit(idx);
}
static void guc_exec_queue_try_add_msg_head(struct xe_exec_queue *q,
struct xe_sched_msg *msg,
u32 opcode)
{
if (!list_empty(&msg->link))
return;
guc_exec_queue_add_msg(q, msg, opcode | MSG_LOCKED | MSG_HEAD);
}
static bool guc_exec_queue_try_add_msg(struct xe_exec_queue *q,
struct xe_sched_msg *msg,
u32 opcode)
{
if (!list_empty(&msg->link))
return false;
guc_exec_queue_add_msg(q, msg, opcode | MSG_LOCKED);
return true;
}
#define STATIC_MSG_CLEANUP 0
#define STATIC_MSG_SUSPEND 1
#define STATIC_MSG_RESUME 2
#define STATIC_MSG_CGP_SYNC 3
static void guc_exec_queue_destroy(struct xe_exec_queue *q)
{
struct xe_sched_msg *msg = q->guc->static_msgs + STATIC_MSG_CLEANUP;
guc_exec_queue_add_msg(q, msg, CLEANUP);
}
static int guc_exec_queue_set_priority(struct xe_exec_queue *q,
enum xe_exec_queue_priority priority)
{
struct xe_sched_msg *msg;
if (q->sched_props.priority == priority ||
exec_queue_killed_or_banned_or_wedged(q))
return 0;
msg = kmalloc_obj(*msg);
if (!msg)
return -ENOMEM;
q->sched_props.priority = priority;
guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS);
return 0;
}
static int guc_exec_queue_set_timeslice(struct xe_exec_queue *q, u32 timeslice_us)
{
struct xe_sched_msg *msg;
if (q->sched_props.timeslice_us == timeslice_us ||
exec_queue_killed_or_banned_or_wedged(q))
return 0;
msg = kmalloc_obj(*msg);
if (!msg)
return -ENOMEM;
q->sched_props.timeslice_us = timeslice_us;
guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS);
return 0;
}
static int guc_exec_queue_set_preempt_timeout(struct xe_exec_queue *q,
u32 preempt_timeout_us)
{
struct xe_sched_msg *msg;
if (q->sched_props.preempt_timeout_us == preempt_timeout_us ||
exec_queue_killed_or_banned_or_wedged(q))
return 0;
msg = kmalloc_obj(*msg);
if (!msg)
return -ENOMEM;
q->sched_props.preempt_timeout_us = preempt_timeout_us;
guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS);
return 0;
}
static int guc_exec_queue_set_multi_queue_priority(struct xe_exec_queue *q,
enum xe_multi_queue_priority priority)
{
struct xe_sched_msg *msg;
xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)), xe_exec_queue_is_multi_queue(q));
if (exec_queue_killed_or_banned_or_wedged(q))
return 0;
msg = kmalloc_obj(*msg);
if (!msg)
return -ENOMEM;
scoped_guard(spinlock, &q->multi_queue.lock) {
if (q->multi_queue.priority == priority) {
kfree(msg);
return 0;
}
q->multi_queue.priority = priority;
}
guc_exec_queue_add_msg(q, msg, SET_MULTI_QUEUE_PRIORITY);
return 0;
}
static bool __guc_exec_queue_suspend(struct xe_exec_queue *q)
{
struct xe_guc_exec_queue *ge = q->guc;
struct xe_gpu_scheduler *sched = &ge->sched;
struct xe_sched_msg *msg = ge->static_msgs + STATIC_MSG_SUSPEND;
bool first;
xe_sched_msg_lock(sched);
first = (++ge->suspend_count == 1);
if (first) {
bool added = guc_exec_queue_try_add_msg(q, msg, SUSPEND);
xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)), added);
ge->suspend_pending = true;
}
xe_sched_msg_unlock(sched);
return first;
}
static bool __guc_exec_queue_resume(struct xe_exec_queue *q)
{
struct xe_guc_exec_queue *ge = q->guc;
struct xe_gpu_scheduler *sched = &ge->sched;
struct xe_sched_msg *msg = ge->static_msgs + STATIC_MSG_RESUME;
struct xe_guc *guc = exec_queue_to_guc(q);
bool last;
xe_sched_msg_lock(sched);
xe_gt_assert(guc_to_gt(guc), !ge->suspend_pending ||
exec_queue_killed_or_banned_or_wedged(q));
xe_gt_assert(guc_to_gt(guc), ge->suspend_count > 0);
last = (--ge->suspend_count == 0);
if (last) {
bool added = guc_exec_queue_try_add_msg(q, msg, RESUME);
xe_gt_assert(guc_to_gt(guc), added);
}
xe_sched_msg_unlock(sched);
return last;
}
static int guc_exec_queue_suspend(struct xe_exec_queue *q)
{
if (exec_queue_killed_or_banned_or_wedged(q))
return -EINVAL;
if (!xe_exec_queue_is_multi_queue_secondary(q)) {
__guc_exec_queue_suspend(q);
return 0;
}
if (exec_queue_killed_or_banned_or_wedged(xe_exec_queue_multi_queue_primary(q)))
return -EINVAL;
scoped_guard(spinlock, &q->multi_queue.group->suspend_lock) {
if (__guc_exec_queue_suspend(q))
__guc_exec_queue_suspend(xe_exec_queue_multi_queue_primary(q));
}
return 0;
}
static void guc_exec_queue_suspend_timeout_ban(struct xe_exec_queue *q)
{
struct xe_guc *guc = exec_queue_to_guc(q);
xe_gt_warn(guc_to_gt(guc),
"Suspend fence, guc_id=%d, failed to respond, banning queue",
q->guc->id);
if (xe_exec_queue_is_multi_queue(q)) {
set_exec_queue_group_banned(q);
xe_guc_exec_queue_group_trigger_cleanup(q);
} else {
set_exec_queue_banned(q);
xe_guc_exec_queue_trigger_cleanup(q);
}
}
static int guc_exec_queue_wait_suspend_done(struct xe_exec_queue *q, bool blocking)
{
struct xe_guc *guc = exec_queue_to_guc(q);
struct xe_device *xe = guc_to_xe(guc);
int ret;
#define WAIT_COND \
(!READ_ONCE(q->guc->suspend_pending) || exec_queue_killed(q) || \
xe_guc_read_stopped(guc))
retry:
if (blocking) {
if (IS_SRIOV_VF(xe))
ret = wait_event_timeout(guc->ct.wq, WAIT_COND, HZ * 5);
else
ret = wait_event_timeout(q->guc->suspend_wait, WAIT_COND,
HZ * 5);
} else if (IS_SRIOV_VF(xe)) {
ret = wait_event_interruptible_timeout(guc->ct.wq, WAIT_COND ||
vf_recovery(guc), HZ * 5);
} else {
ret = wait_event_interruptible_timeout(q->guc->suspend_wait,
WAIT_COND, HZ * 5);
}
if (!blocking && vf_recovery(guc) && !xe_device_wedged(xe))
return -EAGAIN;
if (!ret)
return -ETIME;
else if (!blocking && IS_SRIOV_VF(xe) && !WAIT_COND)
goto retry;
#undef WAIT_COND
return ret < 0 ? ret : 0;
}
static int guc_exec_queue_suspend_wait_common(struct xe_exec_queue *q, bool blocking)
{
int ret;
if (xe_exec_queue_is_multi_queue_secondary(q)) {
ret = guc_exec_queue_wait_suspend_done(q, blocking);
if (ret == -ETIME)
guc_exec_queue_suspend_timeout_ban(q);
if (ret)
return ret;
}
q = xe_exec_queue_multi_queue_primary(q);
ret = guc_exec_queue_wait_suspend_done(q, blocking);
if (ret == -ETIME)
guc_exec_queue_suspend_timeout_ban(q);
return ret;
}
static int guc_exec_queue_suspend_wait(struct xe_exec_queue *q)
{
return guc_exec_queue_suspend_wait_common(q, false);
}
static int guc_exec_queue_suspend_wait_blocking(struct xe_exec_queue *q)
{
return guc_exec_queue_suspend_wait_common(q, true);
}
static void guc_exec_queue_resume(struct xe_exec_queue *q)
{
if (!xe_exec_queue_is_multi_queue_secondary(q)) {
__guc_exec_queue_resume(q);
return;
}
scoped_guard(spinlock, &q->multi_queue.group->suspend_lock) {
if (__guc_exec_queue_resume(q))
__guc_exec_queue_resume(xe_exec_queue_multi_queue_primary(q));
}
}
static void guc_exec_queue_multi_queue_drop_suspend(struct xe_exec_queue *q)
{
scoped_guard(spinlock, &q->multi_queue.group->suspend_lock) {
struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
if (!READ_ONCE(q->guc->suspend_count))
break;
if (exec_queue_killed_or_banned_or_wedged(primary))
break;
__guc_exec_queue_resume(primary);
}
}
static u64 guc_exec_queue_reset_status(struct xe_exec_queue *q)
{
if (xe_exec_queue_is_multi_queue_secondary(q)) {
u64 status = guc_exec_queue_reset_status(xe_exec_queue_multi_queue_primary(q));
if (status)
return status;
}
if (exec_queue_reset(q) || exec_queue_killed_or_banned_or_wedged(q)) {
u64 reason = atomic_read_acquire(&q->ban_reason);
if (!reason)
reason = DRM_XE_EXEC_QUEUE_BAN_REASON_GPU_HANG;
return reason;
}
return 0;
}
static const struct xe_exec_queue_ops guc_exec_queue_ops = {
.init = guc_exec_queue_init,
.kill = guc_exec_queue_kill,
.fini = guc_exec_queue_fini,
.destroy = guc_exec_queue_destroy,
.set_priority = guc_exec_queue_set_priority,
.set_timeslice = guc_exec_queue_set_timeslice,
.set_preempt_timeout = guc_exec_queue_set_preempt_timeout,
.set_multi_queue_priority = guc_exec_queue_set_multi_queue_priority,
.suspend = guc_exec_queue_suspend,
.suspend_wait = guc_exec_queue_suspend_wait,
.suspend_wait_blocking = guc_exec_queue_suspend_wait_blocking,
.resume = guc_exec_queue_resume,
.reset_status = guc_exec_queue_reset_status,
};
static void guc_exec_queue_stop(struct xe_guc *guc, struct xe_exec_queue *q)
{
struct xe_gpu_scheduler *sched = &q->guc->sched;
bool do_destroy = false;
xe_sched_submission_stop(sched);
if (exec_queue_registered(q)) {
if (exec_queue_destroyed(q))
do_destroy = true;
}
xe_sched_msg_lock(sched);
if (q->guc->suspend_pending) {
set_exec_queue_suspended(q);
suspend_fence_signal(q);
}
xe_sched_msg_unlock(sched);
atomic_and(EXEC_QUEUE_STATE_WEDGED | EXEC_QUEUE_STATE_BANNED |
EXEC_QUEUE_STATE_KILLED | EXEC_QUEUE_STATE_DESTROYED |
EXEC_QUEUE_STATE_SUSPENDED,
&q->guc->state);
q->guc->resume_time = 0;
trace_xe_exec_queue_stop(q);
if (!(q->flags & (EXEC_QUEUE_FLAG_KERNEL | EXEC_QUEUE_FLAG_VM))) {
struct xe_sched_job *job = xe_sched_first_pending_job(sched);
bool ban = false;
if (job) {
if ((xe_sched_job_started(job) &&
!xe_sched_job_completed(job)) ||
xe_sched_invalidate_job(job, 2)) {
trace_xe_sched_job_ban(job);
ban = true;
}
}
if (ban) {
set_exec_queue_banned(q);
xe_guc_exec_queue_trigger_cleanup(q);
}
}
if (do_destroy)
guc_exec_queue_destroy_async(q);
}
static int guc_submit_reset_prepare(struct xe_guc *guc)
{
int ret;
ret = atomic_fetch_or(1, &guc->submission_state.stopped);
smp_wmb();
wake_up_all(&guc->ct.wq);
return ret;
}
int xe_guc_submit_reset_prepare(struct xe_guc *guc)
{
if (xe_gt_WARN_ON(guc_to_gt(guc), vf_recovery(guc)))
return 0;
if (!guc->submission_state.initialized)
return 0;
return guc_submit_reset_prepare(guc);
}
void xe_guc_submit_reset_wait(struct xe_guc *guc)
{
wait_event(guc->ct.wq, xe_device_wedged(guc_to_xe(guc)) ||
!xe_guc_read_stopped(guc));
}
void xe_guc_submit_stop(struct xe_guc *guc)
{
struct xe_exec_queue *q;
unsigned long index;
xe_gt_assert(guc_to_gt(guc), xe_guc_read_stopped(guc) == 1);
mutex_lock(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
if (q->guc->id != index)
continue;
guc_exec_queue_stop(guc, q);
}
mutex_unlock(&guc->submission_state.lock);
}
static void guc_exec_queue_revert_pending_state_change(struct xe_guc *guc,
struct xe_exec_queue *q)
{
bool pending_enable, pending_disable, pending_resume;
pending_enable = exec_queue_pending_enable(q);
pending_resume = exec_queue_pending_resume(q);
if (pending_enable && pending_resume) {
q->guc->needs_resume = true;
xe_gt_dbg(guc_to_gt(guc), "Replay RESUME - guc_id=%d",
q->guc->id);
}
if (pending_enable && !pending_resume) {
clear_exec_queue_registered(q);
xe_gt_dbg(guc_to_gt(guc), "Replay REGISTER - guc_id=%d",
q->guc->id);
}
if (pending_enable) {
clear_exec_queue_enabled(q);
clear_exec_queue_pending_resume(q);
clear_exec_queue_pending_enable(q);
xe_gt_dbg(guc_to_gt(guc), "Replay ENABLE - guc_id=%d",
q->guc->id);
}
if (exec_queue_destroyed(q) && exec_queue_registered(q)) {
clear_exec_queue_destroyed(q);
q->guc->needs_cleanup = true;
xe_gt_dbg(guc_to_gt(guc), "Replay CLEANUP - guc_id=%d",
q->guc->id);
}
pending_disable = exec_queue_pending_disable(q);
if (pending_disable && exec_queue_suspended(q)) {
clear_exec_queue_suspended(q);
q->guc->needs_suspend = true;
xe_gt_dbg(guc_to_gt(guc), "Replay SUSPEND - guc_id=%d",
q->guc->id);
}
if (pending_disable) {
if (!pending_enable)
set_exec_queue_enabled(q);
clear_exec_queue_pending_disable(q);
xe_gt_dbg(guc_to_gt(guc), "Replay DISABLE - guc_id=%d",
q->guc->id);
}
if (q->guc->multi_queue.re_register) {
clear_exec_queue_registered(q);
q->guc->multi_queue.re_register = false;
xe_gt_dbg(guc_to_gt(guc), "Replay REGISTER (cgp) - guc_id=%d",
q->guc->id);
}
if (xe_exec_queue_is_multi_queue(q)) {
struct xe_exec_queue_group *group = q->multi_queue.group;
if (q == READ_ONCE(group->cgp_update_q) &&
READ_ONCE(group->sync_pending)) {
if (q->guc->multi_queue.registering_cgp) {
clear_exec_queue_registered(q);
xe_gt_dbg(guc_to_gt(guc), "Replay REGISTER (cgp sync) - guc_id=%d",
q->guc->id);
} else if (q->guc->multi_queue.updating_cgp) {
q->guc->multi_queue.needs_cgp_sync = true;
xe_gt_dbg(guc_to_gt(guc), "Replay CGP_SYNC - guc_id=%d",
q->guc->id);
}
q->guc->multi_queue.registering_cgp = false;
q->guc->multi_queue.updating_cgp = false;
WRITE_ONCE(group->cgp_update_q, NULL);
WRITE_ONCE(group->sync_pending, false);
}
}
if (q->guc->multi_queue.re_update) {
q->guc->multi_queue.re_update = false;
if (exec_queue_registered(q)) {
q->guc->multi_queue.needs_cgp_sync = true;
xe_gt_dbg(guc_to_gt(guc), "Replay CGP_SYNC (re-update) - guc_id=%d",
q->guc->id);
}
}
q->guc->resume_time = 0;
}
static void lrc_parallel_clear(struct xe_lrc *lrc)
{
struct xe_device *xe = gt_to_xe(lrc->gt);
struct iosys_map map = xe_lrc_parallel_map(lrc);
int i;
for (i = 0; i < WQ_SIZE / sizeof(u32); ++i)
parallel_write(xe, map, wq[i],
FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) |
FIELD_PREP(WQ_LEN_MASK, 0));
}
static void guc_exec_queue_pause_prepare(struct xe_guc *guc, struct xe_exec_queue *q)
{
struct xe_gpu_scheduler *sched = &q->guc->sched;
lockdep_assert_held(&guc->submission_state.lock);
xe_sched_submission_stop(sched);
cancel_delayed_work_sync(&sched->base.work_tdr);
}
static void guc_exec_queue_pause(struct xe_guc *guc, struct xe_exec_queue *q)
{
struct xe_gpu_scheduler *sched = &q->guc->sched;
struct xe_sched_job *job;
int i;
lockdep_assert_held(&guc->submission_state.lock);
guc_exec_queue_revert_pending_state_change(guc, q);
if (xe_exec_queue_is_parallel(q)) {
struct xe_lrc *lrc = READ_ONCE(q->lrc[0]);
if (lrc)
lrc_parallel_clear(lrc);
}
job = xe_sched_first_pending_job(sched);
if (job) {
job->restore_replay = true;
for (i = 0; i < q->width; ++i)
q->lrc[i]->ring.tail = job->ptrs[i].head;
}
}
void xe_guc_submit_pause(struct xe_guc *guc)
{
struct xe_exec_queue *q;
unsigned long index;
mutex_lock(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
xe_sched_submission_stop(&q->guc->sched);
mutex_unlock(&guc->submission_state.lock);
}
void xe_guc_submit_pause_vf(struct xe_guc *guc)
{
struct xe_exec_queue *q;
unsigned long index;
xe_gt_assert(guc_to_gt(guc), IS_SRIOV_VF(guc_to_xe(guc)));
xe_gt_assert(guc_to_gt(guc), vf_recovery(guc));
mutex_lock(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
if (q->guc->id != index)
continue;
guc_exec_queue_pause_prepare(guc, q);
}
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
if (q->guc->id != index)
continue;
guc_exec_queue_pause(guc, q);
}
mutex_unlock(&guc->submission_state.lock);
}
static void guc_exec_queue_start(struct xe_exec_queue *q)
{
struct xe_gpu_scheduler *sched = &q->guc->sched;
if (!exec_queue_killed_or_banned_or_wedged(q)) {
struct xe_sched_job *job = xe_sched_first_pending_job(sched);
int i;
trace_xe_exec_queue_resubmit(q);
if (job) {
for (i = 0; i < q->width; ++i) {
q->lrc[i]->ring.tail = job->ptrs[i].head;
if (xe_sched_job_is_ulls(job))
xe_lrc_set_ring_head(q->lrc[i],
job->ptrs[i].head);
xe_lrc_set_ring_tail(q->lrc[i],
xe_lrc_ring_head(q->lrc[i]));
}
}
xe_sched_resubmit_jobs(sched);
}
xe_sched_submission_start(sched);
xe_sched_submission_resume_tdr(sched);
}
int xe_guc_submit_start(struct xe_guc *guc)
{
struct xe_exec_queue *q;
unsigned long index;
xe_gt_assert(guc_to_gt(guc), xe_guc_read_stopped(guc) == 1);
mutex_lock(&guc->submission_state.lock);
atomic_dec(&guc->submission_state.stopped);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
if (q->guc->id != index)
continue;
guc_exec_queue_start(q);
}
mutex_unlock(&guc->submission_state.lock);
wake_up_all(&guc->ct.wq);
return 0;
}
static void guc_exec_queue_unpause_prepare(struct xe_guc *guc,
struct xe_exec_queue *q)
{
struct xe_gpu_scheduler *sched = &q->guc->sched;
struct xe_sched_job *job = NULL;
struct drm_sched_job *s_job;
bool restore_replay = false;
drm_sched_for_each_pending_job(s_job, &sched->base, NULL) {
job = to_xe_sched_job(s_job);
restore_replay |= job->restore_replay;
if (restore_replay) {
xe_gt_dbg(guc_to_gt(guc), "Replay JOB - guc_id=%d, seqno=%d",
q->guc->id, xe_sched_job_seqno(job));
q->ring_ops->emit_job(job);
job->restore_replay = true;
}
}
if (job)
job->last_replay = true;
}
void xe_guc_submit_unpause_prepare_vf(struct xe_guc *guc)
{
struct xe_exec_queue *q;
unsigned long index;
xe_gt_assert(guc_to_gt(guc), IS_SRIOV_VF(guc_to_xe(guc)));
xe_gt_assert(guc_to_gt(guc), vf_recovery(guc));
mutex_lock(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
if (q->guc->id != index)
continue;
guc_exec_queue_unpause_prepare(guc, q);
}
mutex_unlock(&guc->submission_state.lock);
}
static void guc_exec_queue_replay_pending_state_change(struct xe_exec_queue *q)
{
struct xe_gpu_scheduler *sched = &q->guc->sched;
struct xe_sched_msg *msg;
if (q->guc->multi_queue.needs_cgp_sync) {
msg = q->guc->static_msgs + STATIC_MSG_CGP_SYNC;
xe_sched_msg_lock(sched);
guc_exec_queue_try_add_msg_head(q, msg, CGP_SYNC_MSG);
xe_sched_msg_unlock(sched);
q->guc->multi_queue.needs_cgp_sync = false;
}
if (q->guc->needs_cleanup) {
msg = q->guc->static_msgs + STATIC_MSG_CLEANUP;
guc_exec_queue_add_msg(q, msg, CLEANUP);
q->guc->needs_cleanup = false;
}
if (q->guc->needs_suspend) {
msg = q->guc->static_msgs + STATIC_MSG_SUSPEND;
xe_sched_msg_lock(sched);
guc_exec_queue_try_add_msg_head(q, msg, SUSPEND);
xe_sched_msg_unlock(sched);
q->guc->needs_suspend = false;
}
if (q->guc->needs_resume) {
msg = q->guc->static_msgs + STATIC_MSG_RESUME;
xe_sched_msg_lock(sched);
guc_exec_queue_try_add_msg_head(q, msg, RESUME);
xe_sched_msg_unlock(sched);
q->guc->needs_resume = false;
}
}
static void guc_exec_queue_unpause(struct xe_guc *guc, struct xe_exec_queue *q)
{
struct xe_gpu_scheduler *sched = &q->guc->sched;
bool needs_tdr = exec_queue_killed_or_banned_or_wedged(q);
lockdep_assert_held(&guc->submission_state.lock);
xe_sched_resubmit_jobs(sched);
guc_exec_queue_replay_pending_state_change(q);
xe_sched_submission_start(sched);
if (needs_tdr)
xe_guc_exec_queue_trigger_cleanup(q);
xe_sched_submission_resume_tdr(sched);
}
void xe_guc_submit_unpause(struct xe_guc *guc)
{
struct xe_exec_queue *q;
unsigned long index;
mutex_lock(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
xe_sched_submission_start(&q->guc->sched);
mutex_unlock(&guc->submission_state.lock);
}
void xe_guc_submit_unpause_vf(struct xe_guc *guc)
{
struct xe_exec_queue *q;
unsigned long index;
xe_gt_assert(guc_to_gt(guc), IS_SRIOV_VF(guc_to_xe(guc)));
mutex_lock(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
if (q->guc->id != index ||
!drm_sched_is_stopped(&q->guc->sched.base))
continue;
guc_exec_queue_unpause(guc, q);
}
mutex_unlock(&guc->submission_state.lock);
}
void xe_guc_submit_pause_abort(struct xe_guc *guc)
{
struct xe_exec_queue *q;
unsigned long index;
mutex_lock(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
struct xe_gpu_scheduler *sched = &q->guc->sched;
if (q->guc->id != index)
continue;
xe_sched_submission_start(sched);
guc_exec_queue_kill(q);
}
mutex_unlock(&guc->submission_state.lock);
}
static struct xe_exec_queue *
g2h_exec_queue_lookup(struct xe_guc *guc, u32 guc_id)
{
struct xe_gt *gt = guc_to_gt(guc);
struct xe_exec_queue *q;
if (unlikely(guc_id >= GUC_ID_MAX)) {
xe_gt_err(gt, "Invalid guc_id %u\n", guc_id);
return NULL;
}
q = xa_load(&guc->submission_state.exec_queue_lookup, guc_id);
if (unlikely(!q)) {
xe_gt_err(gt, "No exec queue found for guc_id %u\n", guc_id);
return NULL;
}
xe_gt_assert(guc_to_gt(guc), guc_id >= q->guc->id);
xe_gt_assert(guc_to_gt(guc), guc_id < (q->guc->id + q->width));
return q;
}
static void deregister_exec_queue(struct xe_guc *guc, struct xe_exec_queue *q)
{
u32 action[] = {
XE_GUC_ACTION_DEREGISTER_CONTEXT,
q->guc->id,
};
xe_gt_assert(guc_to_gt(guc), exec_queue_destroyed(q));
xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q));
xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q));
xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_enable(q));
trace_xe_exec_queue_deregister(q);
if (xe_exec_queue_is_multi_queue_secondary(q))
handle_deregister_done(guc, q);
else
xe_guc_ct_send_g2h_handler(&guc->ct, action,
ARRAY_SIZE(action));
}
static void handle_sched_done(struct xe_guc *guc, struct xe_exec_queue *q,
u32 runnable_state)
{
trace_xe_exec_queue_scheduling_done(q);
if (runnable_state == 1) {
xe_gt_assert(guc_to_gt(guc), exec_queue_pending_enable(q));
q->guc->resume_time = ktime_get();
clear_exec_queue_pending_resume(q);
clear_exec_queue_pending_enable(q);
smp_wmb();
wake_up_all(&guc->ct.wq);
} else {
bool was_pending;
xe_gt_assert(guc_to_gt(guc), runnable_state == 0);
xe_gt_assert(guc_to_gt(guc), exec_queue_pending_disable(q));
xe_sched_msg_lock(&q->guc->sched);
was_pending = q->guc->suspend_pending && exec_queue_suspended(q);
if (was_pending) {
clear_exec_queue_pending_disable(q);
suspend_fence_signal(q);
}
xe_sched_msg_unlock(&q->guc->sched);
if (!was_pending) {
bool destroyed = exec_queue_destroyed(q);
clear_exec_queue_pending_disable(q);
if (destroyed)
deregister_exec_queue(guc, q);
}
smp_wmb();
wake_up_all(&guc->ct.wq);
}
}
static void handle_multi_queue_secondary_sched_done(struct xe_guc *guc,
struct xe_exec_queue *q,
u32 runnable_state)
{
mutex_lock(&guc->ct.lock);
handle_sched_done(guc, q, runnable_state);
mutex_unlock(&guc->ct.lock);
}
int xe_guc_sched_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
{
struct xe_exec_queue *q;
u32 guc_id, runnable_state;
if (unlikely(len < 2))
return -EPROTO;
guc_id = msg[0];
runnable_state = msg[1];
q = g2h_exec_queue_lookup(guc, guc_id);
if (unlikely(!q))
return -EPROTO;
if (unlikely(!exec_queue_pending_enable(q) &&
!exec_queue_pending_disable(q))) {
xe_gt_err(guc_to_gt(guc),
"SCHED_DONE: Unexpected engine state 0x%04x, guc_id=%d, runnable_state=%u",
atomic_read(&q->guc->state), q->guc->id,
runnable_state);
return -EPROTO;
}
handle_sched_done(guc, q, runnable_state);
return 0;
}
static void handle_deregister_done(struct xe_guc *guc, struct xe_exec_queue *q)
{
trace_xe_exec_queue_deregister_done(q);
clear_exec_queue_registered(q);
guc_exec_queue_destroy_async(q);
}
int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
{
struct xe_exec_queue *q;
u32 guc_id;
if (unlikely(len < 1))
return -EPROTO;
guc_id = msg[0];
q = g2h_exec_queue_lookup(guc, guc_id);
if (unlikely(!q))
return -EPROTO;
if (!exec_queue_destroyed(q) || exec_queue_pending_disable(q) ||
exec_queue_pending_enable(q) || exec_queue_enabled(q)) {
xe_gt_err(guc_to_gt(guc),
"DEREGISTER_DONE: Unexpected engine state 0x%04x, guc_id=%d",
atomic_read(&q->guc->state), q->guc->id);
return -EPROTO;
}
handle_deregister_done(guc, q);
return 0;
}
int xe_guc_exec_queue_reset_handler(struct xe_guc *guc, u32 *msg, u32 len)
{
struct xe_gt *gt = guc_to_gt(guc);
struct xe_exec_queue *q;
u32 guc_id;
if (unlikely(len < 1))
return -EPROTO;
guc_id = msg[0];
q = g2h_exec_queue_lookup(guc, guc_id);
if (unlikely(!q))
return -EPROTO;
if (!exec_queue_killed(q))
xe_gt_info(gt, "Engine reset: engine_class=%s, logical_mask: 0x%x, guc_id=%d, state=0x%0x",
xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id,
atomic_read(&q->guc->state));
trace_xe_exec_queue_reset(q);
xe_guc_exec_queue_reset_trigger_cleanup(q);
return 0;
}
int xe_guc_error_capture_handler(struct xe_guc *guc, u32 *msg, u32 len)
{
u32 status;
if (unlikely(len != XE_GUC_ACTION_STATE_CAPTURE_NOTIFICATION_DATA_LEN))
return -EPROTO;
status = msg[0] & XE_GUC_STATE_CAPTURE_EVENT_STATUS_MASK;
if (status == XE_GUC_STATE_CAPTURE_EVENT_STATUS_NOSPACE)
xe_gt_warn(guc_to_gt(guc), "G2H-Error capture no space");
xe_guc_capture_process(guc);
return 0;
}
int xe_guc_exec_queue_memory_cat_error_handler(struct xe_guc *guc, u32 *msg,
u32 len)
{
struct xe_gt *gt = guc_to_gt(guc);
struct xe_exec_queue *q;
u32 guc_id;
u32 type = XE_GUC_CAT_ERR_TYPE_INVALID;
if (unlikely(!len || len > 2))
return -EPROTO;
guc_id = msg[0];
if (len == 2)
type = msg[1];
if (guc_id == GUC_ID_UNKNOWN) {
xe_gt_err_ratelimited(gt, "Memory CAT error reported by GuC!\n");
return 0;
}
q = g2h_exec_queue_lookup(guc, guc_id);
if (unlikely(!q))
return -EPROTO;
if (type != XE_GUC_CAT_ERR_TYPE_INVALID)
xe_gt_info(gt,
"Engine memory CAT error [%u]: class=%s, logical_mask: 0x%x, guc_id=%d",
type, xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id);
else
xe_gt_info(gt,
"Engine memory CAT error: class=%s, logical_mask: 0x%x, guc_id=%d",
xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id);
trace_xe_exec_queue_memory_cat_error(q);
xe_guc_exec_queue_reset_trigger_cleanup(q);
return 0;
}
int xe_guc_uncorrectable_error_handler(struct xe_guc *guc, u32 *msg, u32 len)
{
struct xe_gt *gt = guc_to_gt(guc);
struct xe_exec_queue *q;
u32 guc_id;
if (unlikely(!len || len > 1))
return -EPROTO;
guc_id = msg[0];
if (guc_id == GUC_ID_UNKNOWN) {
xe_gt_err_ratelimited(gt, "GuC: Uncorrectable local error with unknown GuC id\n");
return 0;
}
q = g2h_exec_queue_lookup(guc, guc_id);
if (unlikely(!q))
return -EPROTO;
if (gt_to_xe(gt)->info.platform == XE_CRESCENTISLAND)
xe_gt_dbg(gt,
"GuC: Uncorrectable local error! guc_id=%d class=%s, logical_mask=0x%x",
guc_id, xe_hw_engine_class_to_str(q->class), q->logical_mask);
else
xe_gt_err(gt,
"GuC: Uncorrectable local error! guc_id=%d class=%s, logical_mask=0x%x",
guc_id, xe_hw_engine_class_to_str(q->class), q->logical_mask);
trace_xe_guc_uncorrectable_error(q);
xe_guc_exec_queue_reset_trigger_cleanup(q);
return 0;
}
int xe_guc_exec_queue_reset_failure_handler(struct xe_guc *guc, u32 *msg, u32 len)
{
struct xe_gt *gt = guc_to_gt(guc);
u8 guc_class, instance;
u32 reason;
if (unlikely(len != 3))
return -EPROTO;
guc_class = msg[0];
instance = msg[1];
reason = msg[2];
xe_log_err(gt, GUCSUBMIT, -EIO,
"engine reset failed on %u:%u, reason=%#x\n",
guc_class, instance, reason);
xe_gt_reset_async(gt);
return 0;
}
int xe_guc_exec_queue_cgp_context_error_handler(struct xe_guc *guc, u32 *msg,
u32 len)
{
struct xe_gt *gt = guc_to_gt(guc);
struct xe_device *xe = guc_to_xe(guc);
struct xe_exec_queue *q;
u32 guc_id = msg[2];
if (unlikely(len != XE_GUC_EXEC_QUEUE_CGP_CONTEXT_ERROR_LEN)) {
drm_err(&xe->drm, "Invalid length %u", len);
return -EPROTO;
}
q = g2h_exec_queue_lookup(guc, guc_id);
if (unlikely(!q))
return -EPROTO;
xe_gt_dbg(gt,
"CGP context error: [%s] err=0x%x, q0_id=0x%x LRCA=0x%x guc_id=0x%x",
msg[0] & 1 ? "uc" : "kmd", msg[1], msg[2], msg[3], msg[4]);
trace_xe_exec_queue_cgp_context_error(q);
xe_guc_exec_queue_reset_trigger_cleanup(q);
return 0;
}
int xe_guc_exec_queue_cgp_sync_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
{
struct xe_device *xe = guc_to_xe(guc);
struct xe_exec_queue_group *group;
struct xe_exec_queue *q, *upd_q;
u32 guc_id = msg[0];
if (unlikely(len < 1)) {
drm_err(&xe->drm, "Invalid CGP_SYNC_DONE length %u", len);
return -EPROTO;
}
q = g2h_exec_queue_lookup(guc, guc_id);
if (unlikely(!q))
return -EPROTO;
if (!xe_exec_queue_is_multi_queue_primary(q)) {
drm_err(&xe->drm, "Unexpected CGP_SYNC_DONE response");
return -EPROTO;
}
group = q->multi_queue.group;
upd_q = READ_ONCE(group->cgp_update_q);
if (upd_q) {
upd_q->guc->multi_queue.registering_cgp = false;
upd_q->guc->multi_queue.updating_cgp = false;
WRITE_ONCE(group->cgp_update_q, NULL);
}
WRITE_ONCE(group->sync_pending, false);
xe_guc_ct_wake_waiters(&guc->ct);
return 0;
}
static void
guc_exec_queue_wq_snapshot_capture(struct xe_exec_queue *q,
struct xe_guc_submit_exec_queue_snapshot *snapshot)
{
struct xe_guc *guc = exec_queue_to_guc(q);
struct xe_device *xe = guc_to_xe(guc);
struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]);
int i;
snapshot->guc.wqi_head = q->guc->wqi_head;
snapshot->guc.wqi_tail = q->guc->wqi_tail;
snapshot->parallel.wq_desc.head = parallel_read(xe, map, wq_desc.head);
snapshot->parallel.wq_desc.tail = parallel_read(xe, map, wq_desc.tail);
snapshot->parallel.wq_desc.status = parallel_read(xe, map,
wq_desc.wq_status);
if (snapshot->parallel.wq_desc.head !=
snapshot->parallel.wq_desc.tail) {
for (i = snapshot->parallel.wq_desc.head;
i != snapshot->parallel.wq_desc.tail;
i = (i + sizeof(u32)) % WQ_SIZE)
snapshot->parallel.wq[i / sizeof(u32)] =
parallel_read(xe, map, wq[i / sizeof(u32)]);
}
}
static void
guc_exec_queue_wq_snapshot_print(struct xe_guc_submit_exec_queue_snapshot *snapshot,
struct drm_printer *p)
{
int i;
drm_printf(p, "\tWQ head: %u (internal), %d (memory)\n",
snapshot->guc.wqi_head, snapshot->parallel.wq_desc.head);
drm_printf(p, "\tWQ tail: %u (internal), %d (memory)\n",
snapshot->guc.wqi_tail, snapshot->parallel.wq_desc.tail);
drm_printf(p, "\tWQ status: %u\n", snapshot->parallel.wq_desc.status);
if (snapshot->parallel.wq_desc.head !=
snapshot->parallel.wq_desc.tail) {
for (i = snapshot->parallel.wq_desc.head;
i != snapshot->parallel.wq_desc.tail;
i = (i + sizeof(u32)) % WQ_SIZE)
drm_printf(p, "\tWQ[%zu]: 0x%08x\n", i / sizeof(u32),
snapshot->parallel.wq[i / sizeof(u32)]);
}
}
struct xe_guc_submit_exec_queue_snapshot *
xe_guc_exec_queue_snapshot_capture(struct xe_exec_queue *q)
{
struct xe_gpu_scheduler *sched = &q->guc->sched;
struct xe_guc_submit_exec_queue_snapshot *snapshot;
int i;
snapshot = kzalloc_obj(*snapshot, GFP_ATOMIC);
if (!snapshot)
return NULL;
snapshot->guc.id = q->guc->id;
memcpy(&snapshot->name, &q->name, sizeof(snapshot->name));
snapshot->class = q->class;
snapshot->logical_mask = q->logical_mask;
snapshot->width = q->width;
snapshot->refcount = kref_read(&q->refcount);
snapshot->jobcount = atomic_read(&q->job_cnt);
snapshot->sched_timeout = sched->base.timeout;
snapshot->sched_props.timeslice_us = q->sched_props.timeslice_us;
snapshot->sched_props.preempt_timeout_us =
q->sched_props.preempt_timeout_us;
snapshot->lrc = kmalloc_objs(struct xe_lrc_snapshot *, q->width,
GFP_ATOMIC);
if (snapshot->lrc) {
for (i = 0; i < q->width; ++i) {
struct xe_lrc *lrc = q->lrc[i];
snapshot->lrc[i] = xe_lrc_snapshot_capture(lrc);
}
}
snapshot->schedule_state = atomic_read(&q->guc->state);
snapshot->exec_queue_flags = q->flags;
snapshot->parallel_execution = xe_exec_queue_is_parallel(q);
if (snapshot->parallel_execution)
guc_exec_queue_wq_snapshot_capture(q, snapshot);
if (xe_exec_queue_is_multi_queue(q)) {
snapshot->multi_queue.valid = true;
snapshot->multi_queue.primary = xe_exec_queue_multi_queue_primary(q)->guc->id;
snapshot->multi_queue.pos = q->multi_queue.pos;
}
return snapshot;
}
void
xe_guc_exec_queue_snapshot_capture_delayed(struct xe_guc_submit_exec_queue_snapshot *snapshot)
{
int i;
if (!snapshot || !snapshot->lrc)
return;
for (i = 0; i < snapshot->width; ++i)
xe_lrc_snapshot_capture_delayed(snapshot->lrc[i]);
}
void
xe_guc_exec_queue_snapshot_print(struct xe_guc_submit_exec_queue_snapshot *snapshot,
struct drm_printer *p)
{
int i;
if (!snapshot)
return;
drm_printf(p, "GuC ID: %d\n", snapshot->guc.id);
drm_printf(p, "\tName: %s\n", snapshot->name);
drm_printf(p, "\tClass: %d\n", snapshot->class);
drm_printf(p, "\tLogical mask: 0x%x\n", snapshot->logical_mask);
drm_printf(p, "\tWidth: %d\n", snapshot->width);
drm_printf(p, "\tRef: %d\n", snapshot->refcount);
drm_printf(p, "\tJob count: %u\n", snapshot->jobcount);
drm_printf(p, "\tTimeout: %ld (ms)\n", snapshot->sched_timeout);
drm_printf(p, "\tTimeslice: %u (us)\n",
snapshot->sched_props.timeslice_us);
drm_printf(p, "\tPreempt timeout: %u (us)\n",
snapshot->sched_props.preempt_timeout_us);
for (i = 0; snapshot->lrc && i < snapshot->width; ++i)
xe_lrc_snapshot_print(snapshot->lrc[i], p);
drm_printf(p, "\tSchedule State: 0x%x\n", snapshot->schedule_state);
drm_printf(p, "\tFlags: 0x%lx\n", snapshot->exec_queue_flags);
if (snapshot->parallel_execution)
guc_exec_queue_wq_snapshot_print(snapshot, p);
if (snapshot->multi_queue.valid) {
drm_printf(p, "\tMulti queue primary GuC ID: %d\n", snapshot->multi_queue.primary);
drm_printf(p, "\tMulti queue position: %d\n", snapshot->multi_queue.pos);
}
}
void xe_guc_exec_queue_snapshot_free(struct xe_guc_submit_exec_queue_snapshot *snapshot)
{
int i;
if (!snapshot)
return;
if (snapshot->lrc) {
for (i = 0; i < snapshot->width; i++)
xe_lrc_snapshot_free(snapshot->lrc[i]);
kfree(snapshot->lrc);
}
kfree(snapshot);
}
static void guc_exec_queue_print(struct xe_exec_queue *q, struct drm_printer *p)
{
struct xe_guc_submit_exec_queue_snapshot *snapshot;
snapshot = xe_guc_exec_queue_snapshot_capture(q);
xe_guc_exec_queue_snapshot_print(snapshot, p);
xe_guc_exec_queue_snapshot_free(snapshot);
}
void xe_guc_register_vf_exec_queue(struct xe_exec_queue *q, int ctx_type)
{
struct xe_guc *guc = exec_queue_to_guc(q);
struct xe_device *xe = guc_to_xe(guc);
struct xe_gt *gt = guc_to_gt(guc);
xe_gt_assert(gt, IS_SRIOV_VF(xe));
xe_gt_assert(gt, !IS_DGFX(xe));
xe_gt_assert(gt, ctx_type == GUC_CONTEXT_COMPRESSION_SAVE ||
ctx_type == GUC_CONTEXT_COMPRESSION_RESTORE);
xe_gt_assert(gt, GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 23, 0));
register_exec_queue(q, ctx_type);
enable_scheduling(q);
}
void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p)
{
struct xe_exec_queue *q;
unsigned long index;
if (!xe_device_uc_enabled(guc_to_xe(guc)))
return;
mutex_lock(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
guc_exec_queue_print(q, p);
mutex_unlock(&guc->submission_state.lock);
}
bool xe_guc_has_registered_mlrc_queues(struct xe_guc *guc)
{
struct xe_exec_queue *q;
unsigned long index;
guard(mutex)(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
if (q->width > 1)
return true;
return false;
}
u32 xe_guc_submit_active_multi_queue_lrca(struct xe_guc *guc,
struct xe_hw_engine *hwe,
u32 cur_lrca, u32 active_id)
{
struct xe_exec_queue *q;
unsigned long index;
u32 lrca = 0;
guard(mutex)(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
struct xe_exec_queue_group *group = q->multi_queue.group;
struct xe_lrc *active_lrc;
struct xe_lrc *primary_lrc;
if (!q->multi_queue.valid || !group || !group->primary)
continue;
if (q->class != hwe->class)
continue;
if (q->multi_queue.pos != active_id)
continue;
primary_lrc = xe_exec_queue_get_lrc(group->primary, 0);
if (!primary_lrc)
continue;
if ((xe_lrc_ggtt_addr(primary_lrc) ^ cur_lrca) & GENMASK(31, 12)) {
xe_lrc_put(primary_lrc);
continue;
}
active_lrc = xe_exec_queue_get_lrc(q, 0);
xe_lrc_put(primary_lrc);
if (!active_lrc)
continue;
lrca = xe_lrc_ggtt_addr(active_lrc);
xe_lrc_put(active_lrc);
break;
}
return lrca;
}
int xe_guc_contexts_hwsp_rebase(struct xe_guc *guc, void *scratch)
{
struct xe_exec_queue *q;
unsigned long index;
int err = 0;
mutex_lock(&guc->submission_state.lock);
xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
if (q->guc->id != index)
continue;
err = xe_exec_queue_contexts_hwsp_rebase(q, scratch);
if (err)
break;
}
mutex_unlock(&guc->submission_state.lock);
return err;
}