Re: [PATCH 3/5] drm/xe/huc: load the DG2 HuC through the GSC via mei_pxp
From: Arisa Snowbell
Date: Wed Dec 02 2026 - 19:49:11 EST
Hi Daniele,
thanks, that answers most of it. The changes below are in my tree; I'll
hold v2 until Q3-Q5 are settled so it doesn't cross another round.
On 12/2/2026, Daniele Ceraolo Spurio wrote:
> I obviously haven't seen the code so I'm not sure exactly how you
> implemented this, but the first time we get a mei bind after boot or
> d3cold the HuC is not going to be loaded but the worker has already been
> queued, so make sure that case is covered.
It is: the bind queues the worker again, the running worker sees the
component on its next poll and loads the HuC, and the queued run then finds
HUC_LOAD_SUCCESSFUL set and only ends. Looking at that path I did find a
window where the first run could signal the fence before a load queued
behind it had happened. The queueing now happens under the lock, and a run
that ends while another one is queued leaves the fence to that one.
> Anyway I'm not against it if you want to avoid that, but to keep the
> abstraction we likely need a xe_huc_flr_prepare() function and call it
> next to where xe_guc_flr_prepare() is called.
Added. xe_huc_flr_prepare() is called from uc_flr_prepare(), next to
xe_guc_flr_prepare(), and xe_uc_reset_prepare() stays untouched.
> The overlap should be harmless, but I don't think we ever actually
> tested it, and the HuC HW is still part of GT. [...] my recommendation
> would be to either test the overlap or just keep the wait.
Keeping the wait.
> I'd suggest to actually move it to huc_upload and have something like:
> [...]
> and then skip the huc_auth call or return 0 from it if huc_uses_mei.
Done that way: xe_huc_upload() queues the load for a HuC the GSC loads,
xe_huc_auth() returns 0 for it, and xe_uc_load_hw() is back to unchanged.
> In Xe we don't have this flexibility in that interface, so I'm ok with
> doing a blocking wait. However, this requires input from the media
> driver, since they're the consumer for this ioctl. [...] I'll send them
> a ping and see what they say.
Thanks. In case it saves a round trip, here is what I found in the
userspace sources:
- The media driver doesn't issue this query on xe today:
mos_query_uc_version_xe() in media_softlet/linux/common/os/xe/
mos_bufmgr_xe.c is static and has no caller ("Need to add this func to
bufmgr api later").
- Mesa's anv is the consumer: anv_physical_device.c sets has_huc from it
once, when the physical device is created (xe_gem_supports_huc()), and
has_huc gates video encode rate control and H.265 decode for the life of
that device. On i915 a device created during the load reads
I915_PARAM_HUC_STATUS == 0 and gets has_huc = false for good; without
the wait xe would do the same through -ENODEV. With the wait, only a
Vulkan device enumerated while the load is pending is delayed. On the
A40 the load ends with the mei bind, under a second after probe and
about 0.3 s after S3 or hibernation resume; the worst case is the 10 s
component timeout when mei_pxp never binds, once per boot.
- Both libdrm's drmIoctl() and Mesa's intel_ioctl() retry on EINTR and
EAGAIN, so the interruptible wait is safe there, and returning -EAGAIN
for "load pending" would turn into a busy loop in userspace. I'd rather
not offer that as an alternative.
So the question is probably for the Mesa (anv video) side rather than the
media driver.
Thanks,
Arisa
On Thu, Dec 3, 2026 at 1:15 AM Ceraolo Spurio, Daniele
<daniele.ceraolospurio@xxxxxxxxx> wrote:
>
>
>
> On 12/2/2026 3:00 AM, Arisa Snowbell wrote:
> > Hi Daniele,
> >
> > thanks for the review. v2 follows your outline; details and a few
> > questions are inline. I'll post v2 once you've had a look at the
> > questions, so it doesn't cross another round on the list.
> >
> > On 12/1/2026 5:38 PM, Ceraolo Spurio, Daniele wrote:
> >> High level approach comment: the state tracking for mei seems overly
> >> complicated. What we need to do here for HuC load and auth is basically
> >> the same thing we do for the late binding firmwares (which are loaded
> >> via the mei_late_bind component), so IMO we should copy and/or piggyback
> >> on that. In short, something like:
> >>
> >> - during init, take a runtime pm ref, to be released once the huc is
> >> loaded or we timeout. [...]
> >> - queue a worker to handle the load. [...]
> >> - if we're entering full suspend (S3/S0ix), wait for the worker to
> >> complete. From the resume flow, re-queue the worker if the HuC is not
> >> loaded and we haven't previously timed out. [...]
> >> - For the fence, you can create it before queuing the worker and signal
> >> from inside it (either for huc successfully loaded or for the timeout).
> >>
> >> Have I missed something that would cause this approach to not work?
> > No, it works, and v2 is built that way. The delayed work, the deadline
> > work, the mutex and the state enum are gone. What's left is the
> > component, the message page, one worker, the fence and a timed-out flag.
> >
> > - The runtime PM reference is taken whenever the worker is queued (from
> > xe_uc_load_hw(), so at probe, GT reset and resume) and dropped by the
> > worker. That's the same pairing as xe_late_bind_fw_load() and
> > xe_late_bind_work().
> > - The worker polls for the component (100 ms steps, 10 s), sends
> > START_HUC_AUTH, waits for HUC_LOAD_SUCCESSFUL and signals the fence on
> > every exit path. The fence is created before the worker is queued.
> > - Full suspend flushes the worker from xe_uc_suspend_prepare().
> > - There is no separate resume hook: xe_uc_load_hw() already runs on every
> > resume that loses the HuC (S3, D3cold, and on drm-xe-next the FLR
> > re-init). It queues the load unless the HuC is still loaded or an
> > earlier load timed out.
> >
> > There are two differences from the late bind flow:
> >
> > - A timeout is not final. As in i915's bind callback, a mei bind wakes
> > the device and queues the load whenever the HuC is not loaded. That
> > way the HuC still loads when mei_pxp shows up late, or when a mei
> > reset fails a load in flight. After a timeout, uC loads don't wait for
> > mei on their own.
>
> I obviously haven't seen the code so I'm not sure exactly how you
> implemented this, but the first time we get a mei bind after boot or
> d3cold the HuC is not going to be loaded but the worker has already been
> queued, so make sure that case is covered.
>
> > - The fence lets video decode jobs and the HuC version query wait for a
> > pending load, which late bind has no need for.
> >
> >> I think this would be cleaner nested in huc->fw.has_gsc_headers.
> > Done as you wrote it. xe_huc_mei_init() now returns an error. On failure
> > xe_huc_init() marks the firmware INIT_FAIL and the probe goes on without
> > a HuC instead of failing.
> >
> >> This function takes an input regarding the type of authentication it
> >> should be doing. By diverting to the mei side and returning early in
> >> this scenario you're ignoring that input, which breaks the abstraction.
> > Fixed. xe_uc_load_hw() picks the type with xe_huc_is_loaded_by_gsc().
> > For a HuC that the GSC loads, xe_huc_auth() queues the load on
> > XE_HUC_AUTH_VIA_GSC and returns -ENODEV on XE_HUC_AUTH_VIA_GUC.
> > Question 2 below is about this.
> >
> >> This belongs in abi/gsc_pxp_commands_abi.h
> > Moved.
> >
> >> This should be abstracted as xe_huc_stop_prepare, which internally can
> >> call xe_huc_mei_stop. Note that the GSC is not part of GT on DG2 and
> >> that the HuC is not impacted by GT reset, so I'm not sure we actually
> >> care about the HuC here for DG2. It might still be worth waiting for the
> >> process to complete before triggering the reset.
> > xe_huc_stop_prepare() is there, and all it does is flush the worker. My
> > A40 agrees with you: the HuC survives force_reset_sync
> > (HUC_KERNEL_LOAD_INFO keeps HUC_LOAD_SUCCESSFUL, status 0x00164001). The
> > load queued after the reset finds the HuC loaded, marks the firmware
> > RUNNING again and returns. Question 1 below asks whether to keep the
> > flush on reset at all.
> >
> >> I'd move this inside xe_uc_reset_prepare, which would also cover the
> >> reset case and make the call in xe_uc_stop_prepare redundant. Might end
> >> up waiting a bit for the worker if we go with my suggestion, but if
> >> we're already resetting it shouldn't be a big negative.
> > I tried that and moved it back out. xe_uc_reset_prepare() is also the
> > "reset already in flight" check in xe_gt_reset_async(), so with the
> > flush there every reset request waits for the worker, up to the 10 s
> > component timeout at boot or resume. One of those requests comes from
> > xe_guc_exec_queue_reset_failure_handler(), which runs in G2H processing
> > with ct->lock held, so G2H handling would stall for as long.
> > xe_gt_declare_wedged() calls it too.
>
> I was thinking that it's very unlikely that we get a GT reset within 10s
> of boot and resume, and if we do then something is going very wrong, so
> a few seconds of extra wait aren't really that bad.
> Anyway I'm not against it if you want to avoid that, but to keep the
> abstraction we likely need a xe_huc_flr_prepare() function and call it
> next to where xe_guc_flr_prepare() is called.
>
> >
> > v2 calls xe_huc_stop_prepare() from:
> >
> > - xe_uc_stop_prepare(), in the reset worker, next to
> > xe_gsc_stop_prepare(), which waits for the GSC init for the same
> > reason;
> > - xe_uc_suspend_prepare(), next to xe_gsc_wait_for_worker_completion().
> > On drm-xe-next this covers the FLR too: xe_device_suspend() with
> > flr = true calls xe_gt_suspend_prepare() before xe_gt_flr_prepare().
> >
> > Does that work for you, or do you still want it in
> > xe_uc_reset_prepare()?
>
> As mentioned above I'm fine either way as long as the code is layered
> correctly.
>
> >
> >> We can use xe_huc_stop_prepare here as well since we already need it
> >> for the above use-case
> > Done.
> >
> >> This shouldn't be needed if we block runtime suspend while huc load is
> >> pending.
> > Dropped. The worker's runtime PM reference blocks runtime suspend now.
> >
> > Other changes in v2:
> >
> > - The status of a HuC that the GSC loads is read from
> > HUC_KERNEL_LOAD_INFO, as on i915, instead of the HECI1 FWSTS5 bit,
> > which only reports the authentication of an image the driver uploaded.
> > This changes xe_huc_is_authenticated(XE_HUC_AUTH_VIA_GSC) on DG2 only,
> > and xe_pxp_init() returns early there.
> > - Video decode jobs depend on the load fence through xe_huc_await_load(),
> > the same engine class i915_request_await_huc() covers.
> > - v2 is rebased on drm-xe-next and Cc's the mei maintainers for patch 2.
> >
> > Questions before I post v2:
> >
> > 1. Do you want the flush on GT reset at all? If neither the GSC nor the
> > HuC is touched by a GT reset, the flush only keeps a GSC command in
> > flight from overlapping the reset, and the reset worker waits for it
> > with XE_FORCEWAKE_ALL held (up to the 10 s component timeout when mei
> > has not bound yet). If that overlap is harmless, I'd drop the call from
> > xe_uc_stop_prepare() and keep only the suspend one.
>
> The overlap should be harmless, but I don't think we ever actually
> tested it, and the HuC HW is still part of GT. It is possible that
> something weird/undefined might happen if the GSC tries to access GT HW
> to load the HuC while a GT reset is in progress, so my recommendation
> would be to either test the overlap or just keep the wait.
>
> >
> > 2. Is queueing the load from xe_huc_auth(XE_HUC_AUTH_VIA_GSC) fine with
> > you? i915 returns -ENODEV from intel_huc_auth() for a GSC-loaded HuC
> > and loads from the PXP bind callback instead. I can move the kick
> > next to the xe_huc_auth() call in xe_uc_load_hw() if you prefer.
>
> I'd suggest to actually move it to huc_upload and have something like:
>
> int xe_huc_upload(struct xe_huc *huc)
> {
> if (!xe_uc_fw_is_loadable(&huc->fw))
> return 0;
>
> if (huc_uses_mei())
> return huc_load_and_auth_via_mei();
> else
> return xe_uc_fw_upload(&huc->fw, 0, HUC_UKERNEL);
> }
>
> and then skip the huc_auth call or return 0 from it if huc_uses_mei.
> However, if you think that doing it from xe_huc_auth ends up cleaner
> then I'm ok with that. in that case it might be helpful to add a new
> XE_HUC_AUTH_VIA_MEI_GSC case instead of re-using the existing
> XE_HUC_AUTH_VIA_GSC
>
> >
> > 3. The HuC version query (XE_QUERY_UC_TYPE_HUC) waits for a pending load,
> > interruptibly and for at most 15 s, and the uAPI doc says so. Without
> > the wait, a query between probe and the end of the load returns
> > -ENODEV, the same answer as for a HuC that will never load.
> > i915's I915_PARAM_HUC_STATUS doesn't wait; it reports 0 (not loaded)
> > in that window. Is the bounded wait acceptable, or would you rather
> > solve it another way?
>
> In i915 the 0 return actually means "load in progress", while HuC not
> loaded or load fail are represented by negative values. In Xe we don't
> have this flexibility in that interface, so I'm ok with doing a blocking
> wait. However, this requires input from the media driver, since they're
> the consumer for this ioctl. I have no idea if a prolonged wait could
> cause consequences in userspace. I'll send them a ping and see what they
> say.
>
> >
> > 4. xe_huc_mei.c is adapted in large part from i915's intel_huc.c and
> > intel_pxp_huc.c, so it keeps "Copyright © 2016-2022 Intel Corporation"
> > and adds my line; xe_huc_mei.h carries only mine. Should it be written
> > differently?
>
> Good question, and I have no idea on the answer :)
> Hopefully one of the maintainers can chime in?
>
> >
> > 5. Patch 2 is a five-line drivers/misc/mei change (the "xe.mei-gsc" id).
> > Should it go in with the series through drm-xe, with an Ack from the
> > mei side, or through char-misc first?
>
> That's a decision for the char-misc maintainer (Greg KH). If he acks
> merging it via drm-xe-next then we can do it, otherwise it needs to go
> via char-misc
>
> Daniele
>
> >
> > v1 ran on my Arc Pro A40 through boot, GT reset, S3 and runtime D3cold.
> > I'll repeat those runs with v2 before I post it.
> >
> > Thanks,
> > Arisa
> >
> >
> > On Wed, Dec 2, 2026 at 2:38 AM Ceraolo Spurio, Daniele
> > <daniele.ceraolospurio@xxxxxxxxx> wrote:
> >>
> >>
> >> On 11/26/2026 12:57 PM, Arisa Snowbell wrote:
> >>> On DG2 the GSC both loads and authenticates the HuC. Register as the
> >>> I915_COMPONENT_PXP component, as i915 does, so mei_pxp hands us its ops,
> >>> and send PXP43 START_HUC_AUTH with the DPA of the firmware in VRAM through
> >>> gsc_command(), whose request and reply live in a VRAM page. Leaving the
> >>> GSC to mei keeps two drivers from driving the same hardware.
> >>>
> >>> The load is asynchronous. It runs from a worker after every uC load
> >>> (probe, GT reset, resume) and when mei binds; a stop before a reset, a
> >>> suspend or an FLR waits for a running load and drops a pending one. Each
> >>> load gets a fence that signals when it succeeded, failed or stopped
> >>> waiting for mei. If mei_pxp does not bind within 10 s the HuC stays
> >>> unloaded and later loads stop waiting for it until it binds, as i915's
> >>> INTEL_HUC_DELAYED_LOAD_ERROR does.
> >>>
> >>> As on i915, the GSC must be the one loading the HuC (GSC_LOADS_HUC), and
> >>> a missing mei module or a failed setup only leaves the HuC unloaded.
> >>>
> >>> A GSC command that never returns still holds up a stop, as on i915; a
> >>> mei reset ends it. Video decode does not wait on it past the 10 s.
> >>>
> >>> Nothing selects a DG2 HuC blob yet, so this changes nothing on its own.
> >>>
> >>> Signed-off-by: Arisa Snowbell <arisa.snowbell@xxxxxxxxx>
> >> High level approach comment: the state tracking for mei seems overly
> >> complicated. What we need to do here for HuC load and auth is basically
> >> the same thing we do for the late binding firmwares (which are loaded
> >> via the mei_late_bind component), so IMO we should copy and/or piggyback
> >> on that. In short, something like:
> >>
> >> - during init, take a runtime pm ref, to be released once the huc is
> >> loaded or we timeout. This makes it so the load flow can't race with
> >> runtime suspend, making things easier. You can see this done in
> >> xe_late_bind_fw_load(), with the release in xe_late_bind_work().
> >> - queue a worker to handle the load. This can wait for the component to
> >> bind and then do the load, or handle the timeout if the component
> >> doesn't bind in time. Again you can take inspiration from
> >> xe_late_bind_work().
> >> - if we're entering full suspend (S3/S0ix), wait for the worker to
> >> complete. From the resume flow, re-queue the worker if the HuC is not
> >> loaded and we haven't previously timed out. You can check
> >> xe_late_bind_pm_suspend and xe_late_bind_pm_resume as a reference.
> >> - For the fence, you can create it before queuing the worker and signal
> >> from inside it (either for huc successfully loaded or for the timeout).
> >>
> >> Have I missed something that would cause this approach to not work?
> >>
> >> Some minor comments below, but I've not looked into details at the mei
> >> handling since I'm recommending a different approach
> >>
> >>> ---
> >>> drivers/gpu/drm/xe/Makefile | 1 +
> >>> drivers/gpu/drm/xe/regs/xe_guc_regs.h | 3 +
> >>> drivers/gpu/drm/xe/xe_huc.c | 16 +-
> >>> drivers/gpu/drm/xe/xe_huc_mei.c | 569 ++++++++++++++++++++++++++
> >>> drivers/gpu/drm/xe/xe_huc_mei.h | 21 +
> >>> drivers/gpu/drm/xe/xe_huc_types.h | 57 +++
> >>> drivers/gpu/drm/xe/xe_uc.c | 7 +
> >>> 7 files changed, 673 insertions(+), 1 deletion(-)
> >>> create mode 100644 drivers/gpu/drm/xe/xe_huc_mei.c
> >>> create mode 100644 drivers/gpu/drm/xe/xe_huc_mei.h
> >>>
> >>> diff --git a/drivers/gpu/drm/xe/Makefile b/drivers/gpu/drm/xe/Makefile
> >>> index ad3ab8097..a974055fe 100644
> >>> --- a/drivers/gpu/drm/xe/Makefile
> >>> +++ b/drivers/gpu/drm/xe/Makefile
> >>> @@ -81,6 +81,7 @@ xe-y += xe_bb.o \
> >>> xe_guc_tlb_inval.o \
> >>> xe_heci_gsc.o \
> >>> xe_huc.o \
> >>> + xe_huc_mei.o \
> >>> xe_hw_engine.o \
> >>> xe_hw_engine_class_sysfs.o \
> >>> xe_hw_engine_group.o \
> >>> diff --git a/drivers/gpu/drm/xe/regs/xe_guc_regs.h b/drivers/gpu/drm/xe/regs/xe_guc_regs.h
> >>> index 5a8a67eb3..520e2a15b 100644
> >>> --- a/drivers/gpu/drm/xe/regs/xe_guc_regs.h
> >>> +++ b/drivers/gpu/drm/xe/regs/xe_guc_regs.h
> >>> @@ -60,6 +60,9 @@
> >>> #define GUC_ENABLE_READ_CACHE_LOGIC REG_BIT(1)
> >>> #define GUC_DISABLE_SRAM_INIT_TO_ZEROES REG_BIT(0)
> >>>
> >>> +#define GUC_SHIM_CONTROL2 XE_REG(0xc068)
> >>> +#define GSC_LOADS_HUC REG_BIT(30)
> >>> +
> >>> #define SOFT_SCRATCH(n) XE_REG(0xc180 + (n) * 4)
> >>> #define SOFT_SCRATCH_COUNT 16
> >>>
> >>> diff --git a/drivers/gpu/drm/xe/xe_huc.c b/drivers/gpu/drm/xe/xe_huc.c
> >>> index c73e1acbd..0a2f0688e 100644
> >>> --- a/drivers/gpu/drm/xe/xe_huc.c
> >>> +++ b/drivers/gpu/drm/xe/xe_huc.c
> >>> @@ -19,6 +19,7 @@
> >>> #include "xe_gt.h"
> >>> #include "xe_gt_printk.h"
> >>> #include "xe_guc.h"
> >>> +#include "xe_huc_mei.h"
> >>> #include "xe_map.h"
> >>> #include "xe_mmio.h"
> >>> #include "xe_sriov.h"
> >>> @@ -91,7 +92,8 @@ int xe_huc_init(struct xe_huc *huc)
> >>> if (IS_SRIOV_VF(xe))
> >>> return 0;
> >>>
> >>> - if (huc->fw.has_gsc_headers) {
> >>> + /* on DG2 the GSC loads the HuC through mei, not via GSCCS packets */
> >>> + if (huc->fw.has_gsc_headers && xe->info.platform != XE_DG2) {
> >>> ret = huc_alloc_gsc_pkt(huc);
> >>> if (ret)
> >>> goto out;
> >>> @@ -122,6 +124,8 @@ int xe_huc_init_post_hwconfig(struct xe_huc *huc)
> >>> if (ret)
> >>> return ret;
> >>>
> >>> + xe_huc_mei_init(huc);
> >> I think this would be cleaner nested in huc->fw.has_gsc_headers.
> >> Something like:
> >>
> >> if (huc->fw.has_gsc_headers) {
> >> if (xe->info.platform == XE_DG2) {
> >> xe_huc_mei_init(huc);
> >> } else {
> >> ret = huc_alloc_gsc_pkt(huc);
> >> if (ret)
> >> goto out;
> >> }
> >> }
> >>
> >>> +
> >>> return 0;
> >>> }
> >>>
> >>> @@ -129,6 +133,11 @@ int xe_huc_upload(struct xe_huc *huc)
> >>> {
> >>> if (!xe_uc_fw_is_loadable(&huc->fw))
> >>> return 0;
> >>> +
> >>> + /* the GSC loads it from VRAM itself */
> >>> + if (xe_huc_mei_is_used(huc))
> >>> + return 0;
> >>> +
> >>> return xe_uc_fw_upload(&huc->fw, 0, HUC_UKERNEL);
> >>> }
> >>>
> >>> @@ -244,6 +253,11 @@ int xe_huc_auth(struct xe_huc *huc, enum xe_huc_auth_types type)
> >>> if (!xe_uc_fw_is_loadable(&huc->fw))
> >>> return 0;
> >>>
> >>> + if (xe_huc_mei_is_used(huc)) {
> >>> + xe_huc_mei_start(huc);
> >>> + return 0;
> >>> + }
> >> This function takes an input regarding the type of authentication it
> >> should be doing. By diverting to the mei side and returning early in
> >> this scenario you're ignoring that input, which breaks the abstraction.
> >>
> >>> +
> >>> /* On newer platforms the HuC survives reset, so no need to re-auth */
> >>> if (xe_huc_is_authenticated(huc, type)) {
> >>> xe_uc_fw_change_status(&huc->fw, XE_UC_FIRMWARE_RUNNING);
> >>> diff --git a/drivers/gpu/drm/xe/xe_huc_mei.c b/drivers/gpu/drm/xe/xe_huc_mei.c
> >>> new file mode 100644
> >>> index 000000000..595405c4a
> >>> --- /dev/null
> >>> +++ b/drivers/gpu/drm/xe/xe_huc_mei.c
> >>> @@ -0,0 +1,569 @@
> >>> +// SPDX-License-Identifier: MIT
> >>> +/*
> >>> + * Copyright © 2016-2022 Intel Corporation
> >>> + * Copyright © 2026 Arisa Snowbell
> >>> + */
> >>> +
> >>> +/*
> >>> + * HuC load and authentication by the GSC, through mei (DG2).
> >>> + *
> >>> + * On DG2 the GSC both loads and authenticates the HuC. It is reached through
> >>> + * the PXP client on HECI1, owned by mei; mei_pxp hands us its ops as an
> >>> + * I915_COMPONENT_PXP component, as for i915 (intel_pxp_tee.c). Messages go
> >>> + * through gsc_command(), which takes the request and the reply as a
> >>> + * scatterlist in device memory.
> >>> + *
> >>> + * The load is asynchronous: mei binds some time after probe and again after
> >>> + * every system resume, and the HuC is lost on every reset and suspend. While a
> >>> + * load is pending, user submissions to the video decode engines wait for it on
> >>> + * a fence, as on i915.
> >>> + */
> >>> +
> >>> +#include <linux/component.h>
> >>> +#include <linux/dma-fence.h>
> >>> +#include <linux/scatterlist.h>
> >>> +#include <linux/slab.h>
> >>> +#include <linux/workqueue.h>
> >>> +
> >>> +#include <drm/drm_managed.h>
> >>> +#include <drm/intel/i915_component.h>
> >>> +#include <drm/intel/i915_pxp_tee_interface.h>
> >>> +
> >>> +#include "abi/gsc_pxp_commands_abi.h"
> >>> +#include "regs/xe_guc_regs.h"
> >>> +#include "xe_bo.h"
> >>> +#include "xe_device.h"
> >>> +#include "xe_force_wake.h"
> >>> +#include "xe_gt_printk.h"
> >>> +#include "xe_huc.h"
> >>> +#include "xe_huc_mei.h"
> >>> +#include "xe_map.h"
> >>> +#include "xe_mmio.h"
> >>> +#include "xe_pm.h"
> >>> +#include "xe_uc_fw.h"
> >>> +
> >>> +#define PXP43_CMDID_START_HUC_AUTH 0x0000003A /* DG2 */
> >>> +
> >>> +/* PXP-Input-Packet: HuC load and authentication */
> >>> +struct pxp43_start_huc_auth_in {
> >>> + struct pxp_cmd_header header;
> >>> + __le64 huc_base_address;
> >>> +} __packed;
> >> This belongs in abi/gsc_pxp_commands_abi.h
> >>
> >>> +
> >>> +/*
> >>> + * mei binds a few seconds after probe and re-binds after a system resume; past
> >>> + * this, media runs without HuC and later loads stop waiting for mei.
> >>> + */
> >>> +#define HUC_MEI_COMPONENT_TIMEOUT_MS 10000
> >>> +
> >>> +/* The GSC loads the whole binary before it flags the authentication */
> >>> +#define HUC_MEI_AUTH_TIMEOUT_US 1000000
> >>> +
> >>> +/* A safety net: the deadline above always settles a pending load first */
> >>> +#define HUC_MEI_SETTLE_TIMEOUT_MS 15000
> >>> +
> >>> +static struct xe_huc *mei_to_huc(struct xe_huc_mei *mei)
> >>> +{
> >>> + return container_of(mei, struct xe_huc, mei);
> >>> +}
> >>> +
> >>> +static struct xe_gt *huc_to_gt(struct xe_huc *huc)
> >>> +{
> >>> + return container_of(huc, struct xe_gt, uc.huc);
> >>> +}
> >>> +
> >>> +static struct xe_gt *mei_to_gt(struct xe_huc_mei *mei)
> >>> +{
> >>> + return huc_to_gt(mei_to_huc(mei));
> >>> +}
> >>> +
> >>> +static const char *huc_mei_fence_driver_name(struct dma_fence *fence)
> >>> +{
> >>> + return "xe";
> >>> +}
> >>> +
> >>> +static const char *huc_mei_fence_timeline_name(struct dma_fence *fence)
> >>> +{
> >>> + return "huc_load";
> >>> +}
> >>> +
> >>> +static const struct dma_fence_ops huc_mei_fence_ops = {
> >>> + .get_driver_name = huc_mei_fence_driver_name,
> >>> + .get_timeline_name = huc_mei_fence_timeline_name,
> >>> +};
> >>> +
> >>> +/* Its own lock: users of the fence may outlive the device */
> >>> +struct huc_mei_fence {
> >>> + struct dma_fence base;
> >>> + spinlock_t lock; /* the lock of @base */
> >>> +};
> >>> +
> >>> +/* start() runs in the reset and resume paths, which must not wait on reclaim */
> >>> +static struct dma_fence *huc_mei_fence_create(struct xe_huc_mei *mei)
> >>> +{
> >>> + struct huc_mei_fence *fence = kzalloc_obj(*fence, GFP_NOWAIT | __GFP_NOWARN);
> >>> +
> >>> + if (!fence)
> >>> + return NULL;
> >>> +
> >>> + spin_lock_init(&fence->lock);
> >>> + dma_fence_init(&fence->base, &huc_mei_fence_ops, &fence->lock,
> >>> + mei->fence_context, atomic64_inc_return(&mei->seqno));
> >>> +
> >>> + return &fence->base;
> >>> +}
> >>> +
> >>> +/**
> >>> + * xe_huc_mei_is_used() - whether the GSC loads this HuC through mei
> >>> + * @huc: the HuC
> >>> + *
> >>> + * Return: true on DG2 once its HuC is set up to load through the GSC.
> >>> + */
> >>> +bool xe_huc_mei_is_used(struct xe_huc *huc)
> >>> +{
> >>> + return huc->mei.state != XE_HUC_MEI_UNUSED;
> >>> +}
> >>> +
> >>> +static u32 huc_mei_load_info(struct xe_gt *gt)
> >>> +{
> >>> + CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT);
> >>> + if (!fw_ref.domains)
> >>> + return 0;
> >>> +
> >>> + return xe_mmio_read32(>->mmio, HUC_KERNEL_LOAD_INFO);
> >>> +}
> >>> +
> >>> +/* Ends the wait of the current load */
> >>> +static void huc_mei_settle_locked(struct xe_huc_mei *mei)
> >>> +{
> >>> + lockdep_assert_held(&mei->state_lock);
> >>> +
> >>> + if (mei->state != XE_HUC_MEI_LOADING)
> >>> + return;
> >>> +
> >>> + mei->state = XE_HUC_MEI_SETTLED;
> >>> + if (mei->load)
> >>> + dma_fence_signal(mei->load);
> >>> + cancel_delayed_work(&mei->deadline);
> >>> +}
> >>> +
> >>> +static int huc_mei_send_auth(struct xe_huc *huc, struct i915_pxp_component *pxp)
> >>> +{
> >>> + struct xe_gt *gt = huc_to_gt(huc);
> >>> + struct xe_device *xe = gt_to_xe(gt);
> >>> + struct xe_bo *msg = huc->mei.msg;
> >>> + struct pxp43_start_huc_auth_in in = {};
> >>> + struct pxp43_huc_auth_out out = {};
> >>> + struct scatterlist sg;
> >>> + dma_addr_t huc_addr = xe_bo_main_addr(huc->fw.bo, PAGE_SIZE);
> >>> + ssize_t ret;
> >>> +
> >>> + in.header.api_version = PXP_APIVER(4, 3);
> >>> + in.header.command_id = PXP43_CMDID_START_HUC_AUTH;
> >>> + in.header.buffer_len = sizeof(in.huc_base_address);
> >>> + in.huc_base_address = cpu_to_le64(huc_addr);
> >>> +
> >>> + xe_map_memset(xe, &msg->vmap, 0, 0, PAGE_SIZE);
> >>> + xe_map_memcpy_to(xe, &msg->vmap, 0, &in, sizeof(in));
> >>> +
> >>> + /* the same page carries the request and the reply, as on i915 */
> >>> + sg_init_table(&sg, 1);
> >>> + sg.length = PAGE_SIZE;
> >>> + sg_dma_address(&sg) = xe_bo_main_addr(msg, PAGE_SIZE);
> >>> + sg_dma_len(&sg) = PAGE_SIZE;
> >>> +
> >>> + xe_gt_dbg(gt, "HuC: asking the GSC to load %zu bytes at DPA %pad\n",
> >>> + xe_bo_size(huc->fw.bo), &huc_addr);
> >>> +
> >>> + ret = pxp->ops->gsc_command(pxp->tee_dev, 0, 0, &sg, sizeof(in), &sg);
> >>> + if (ret < 0) {
> >>> + xe_gt_err(gt, "HuC: GSC command failed: %pe\n", ERR_PTR(ret));
> >>> + return ret;
> >>> + }
> >>> + if (ret < (ssize_t)sizeof(out.header)) {
> >>> + xe_gt_err(gt, "HuC: GSC reply too short: %zd bytes\n", ret);
> >>> + return -EPROTO;
> >>> + }
> >>> +
> >>> + xe_map_memcpy_from(xe, &out, &msg->vmap, 0, sizeof(out));
> >>> +
> >>> + /*
> >>> + * HuC can survive a shallow suspend, the GSC then refuses the load with
> >>> + * OP_NOT_PERMITTED; the status register check that follows still
> >>> + * catches a HuC that is not there.
> >>> + */
> >>> + if (out.header.status != PXP_STATUS_SUCCESS &&
> >>> + out.header.status != PXP_STATUS_OP_NOT_PERMITTED) {
> >>> + xe_gt_err(gt, "HuC: GSC refused the load: %#x\n", out.header.status);
> >>> + return -EPROTO;
> >>> + }
> >>> +
> >>> + return 0;
> >>> +}
> >>> +
> >>> +static int huc_mei_load_and_auth(struct xe_huc *huc)
> >>> +{
> >>> + struct xe_gt *gt = huc_to_gt(huc);
> >>> + struct xe_huc_mei *mei = &huc->mei;
> >>> + int ret;
> >>> +
> >>> + if (huc_mei_load_info(gt) & HUC_LOAD_SUCCESSFUL)
> >>> + return 0;
> >>> +
> >>> + scoped_guard(mutex, &mei->lock) {
> >>> + /* mei binds later; the bind queues this again */
> >>> + if (!mei->pxp)
> >>> + return -EAGAIN;
> >>> +
> >>> + ret = huc_mei_send_auth(huc, mei->pxp);
> >>> + if (ret)
> >>> + return ret;
> >>> + }
> >>> +
> >>> + xe_uc_fw_change_status(&huc->fw, XE_UC_FIRMWARE_TRANSFERRED);
> >>> +
> >>> + CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT);
> >>> + if (!fw_ref.domains)
> >>> + return -ETIMEDOUT;
> >>> +
> >>> + ret = xe_mmio_wait32(>->mmio, HUC_KERNEL_LOAD_INFO, HUC_LOAD_SUCCESSFUL,
> >>> + HUC_LOAD_SUCCESSFUL, HUC_MEI_AUTH_TIMEOUT_US, NULL, false);
> >>> + if (ret)
> >>> + xe_gt_err(gt, "HuC: not authenticated after the GSC load, status %#x\n",
> >>> + xe_mmio_read32(>->mmio, HUC_KERNEL_LOAD_INFO));
> >>> +
> >>> + return ret;
> >>> +}
> >>> +
> >>> +/*
> >>> + * The work runs on a runtime PM reference its queuer took: suspend flushes it,
> >>> + * so it must never resume the device itself.
> >>> + */
> >>> +static void huc_mei_queue(struct xe_huc_mei *mei)
> >>> +{
> >>> + struct xe_device *xe = gt_to_xe(mei_to_gt(mei));
> >>> +
> >>> + xe_pm_runtime_get_noresume(xe);
> >>> + if (!queue_work(system_dfl_wq, &mei->auth))
> >>> + xe_pm_runtime_put(xe);
> >>> +}
> >>> +
> >>> +/* Drops a queued but not yet started load along with its reference */
> >>> +static void huc_mei_cancel(struct xe_huc_mei *mei)
> >>> +{
> >>> + if (cancel_work_sync(&mei->auth))
> >>> + xe_pm_runtime_put(gt_to_xe(mei_to_gt(mei)));
> >>> + cancel_delayed_work_sync(&mei->deadline);
> >>> +}
> >>> +
> >>> +static bool huc_mei_gt_up(struct xe_huc_mei *mei)
> >>> +{
> >>> + guard(spinlock_irqsave)(&mei->state_lock);
> >>> +
> >>> + return mei->state == XE_HUC_MEI_LOADING || mei->state == XE_HUC_MEI_SETTLED;
> >>> +}
> >>> +
> >>> +static void huc_mei_auth_work(struct work_struct *work)
> >>> +{
> >>> + struct xe_huc_mei *mei = container_of(work, struct xe_huc_mei, auth);
> >>> + struct xe_huc *huc = mei_to_huc(mei);
> >>> + struct xe_gt *gt = huc_to_gt(huc);
> >>> + int ret;
> >>> +
> >>> + guard(xe_pm_runtime_release_only)(gt_to_xe(gt));
> >>> +
> >>> + /* stop() cancels this work before the GT goes down */
> >>> + if (!huc_mei_gt_up(mei))
> >>> + return;
> >>> +
> >>> + ret = huc_mei_load_and_auth(huc);
> >>> + if (ret == -EAGAIN)
> >>> + return;
> >>> +
> >>> + if (ret) {
> >>> + xe_gt_err(gt, "HuC: load and authentication via GSC failed: %pe\n", ERR_PTR(ret));
> >>> + xe_uc_fw_change_status(&huc->fw, XE_UC_FIRMWARE_LOAD_FAIL);
> >>> + } else {
> >>> + if (!mei->announced)
> >>> + xe_gt_info(gt, "HuC: authenticated via GSC (version %u.%u.%u)\n",
> >>> + huc->fw.versions.found[XE_UC_FW_VER_RELEASE].major,
> >>> + huc->fw.versions.found[XE_UC_FW_VER_RELEASE].minor,
> >>> + huc->fw.versions.found[XE_UC_FW_VER_RELEASE].patch);
> >>> + mei->announced = true;
> >>> + xe_uc_fw_change_status(&huc->fw, XE_UC_FIRMWARE_RUNNING);
> >>> + }
> >>> +
> >>> + scoped_guard(spinlock_irqsave, &mei->state_lock)
> >>> + huc_mei_settle_locked(mei);
> >>> +}
> >>> +
> >>> +static void huc_mei_deadline_work(struct work_struct *work)
> >>> +{
> >>> + struct xe_huc_mei *mei = container_of(work, struct xe_huc_mei, deadline.work);
> >>> + struct xe_gt *gt = mei_to_gt(mei);
> >>> +
> >>> + /* not @mei->lock: a GSC command that never returns holds it */
> >>> + guard(spinlock_irqsave)(&mei->state_lock);
> >>> + if (mei->state != XE_HUC_MEI_LOADING)
> >>> + return;
> >>> +
> >>> + if (mei->bound) {
> >>> + xe_gt_warn(gt, "HuC: the GSC load takes over %u ms, media no longer waits for it\n",
> >>> + HUC_MEI_COMPONENT_TIMEOUT_MS);
> >>> + } else {
> >>> + xe_gt_notice(gt, "HuC: no mei PXP component after %u ms (is mei_pxp loaded?), media runs without HuC until it binds\n",
> >>> + HUC_MEI_COMPONENT_TIMEOUT_MS);
> >>> + mei->absent = true;
> >>> + }
> >>> + huc_mei_settle_locked(mei);
> >>> +}
> >>> +
> >>> +static int huc_mei_component_bind(struct device *xe_kdev, struct device *tee_kdev, void *data)
> >>> +{
> >>> + struct xe_device *xe = kdev_to_xe_device(xe_kdev);
> >>> + struct xe_huc_mei *mei = &xe_root_mmio_gt(xe)->uc.huc.mei;
> >>> + struct i915_pxp_component *pxp = data;
> >>> +
> >>> + scoped_guard(mutex, &mei->lock) {
> >>> + mei->pxp = pxp;
> >>> + mei->pxp->tee_dev = tee_kdev;
> >>> + }
> >>> +
> >>> + scoped_guard(spinlock_irqsave, &mei->state_lock) {
> >>> + mei->bound = true;
> >>> + mei->absent = false;
> >>> + }
> >>> +
> >>> + xe_gt_dbg(mei_to_gt(mei), "HuC: mei PXP component bound (%s)\n", dev_name(tee_kdev));
> >>> +
> >>> + /* a suspended device loads the HuC when start() runs at resume */
> >>> + if (!xe_pm_runtime_get_if_active(xe))
> >>> + return 0;
> >>> +
> >>> + if (huc_mei_gt_up(mei))
> >>> + huc_mei_queue(mei);
> >>> + xe_pm_runtime_put(xe);
> >>> +
> >>> + return 0;
> >>> +}
> >>> +
> >>> +static void huc_mei_component_unbind(struct device *xe_kdev, struct device *tee_kdev, void *data)
> >>> +{
> >>> + struct xe_device *xe = kdev_to_xe_device(xe_kdev);
> >>> + struct xe_huc_mei *mei = &xe_root_mmio_gt(xe)->uc.huc.mei;
> >>> +
> >>> + scoped_guard(spinlock_irqsave, &mei->state_lock)
> >>> + mei->bound = false;
> >>> +
> >>> + scoped_guard(mutex, &mei->lock)
> >>> + mei->pxp = NULL;
> >>> +}
> >>> +
> >>> +static const struct component_ops huc_mei_component_ops = {
> >>> + .bind = huc_mei_component_bind,
> >>> + .unbind = huc_mei_component_unbind,
> >>> +};
> >>> +
> >>> +static void huc_mei_go_down(struct xe_huc_mei *mei)
> >>> +{
> >>> + struct dma_fence *load;
> >>> +
> >>> + scoped_guard(spinlock_irqsave, &mei->state_lock) {
> >>> + huc_mei_settle_locked(mei);
> >>> + mei->state = XE_HUC_MEI_DOWN;
> >>> + load = mei->load;
> >>> + mei->load = NULL;
> >>> + }
> >>> + dma_fence_put(load);
> >>> +}
> >>> +
> >>> +static void huc_mei_fini(void *arg)
> >>> +{
> >>> + struct xe_huc_mei *mei = arg;
> >>> +
> >>> + huc_mei_go_down(mei);
> >>> + component_del(gt_to_xe(mei_to_gt(mei))->drm.dev, &huc_mei_component_ops);
> >>> + huc_mei_cancel(mei);
> >>> +}
> >>> +
> >>> +/* The DG2 blob is parsed for the GSC, it has no image to DMA */
> >>> +static int huc_mei_check_mode(struct xe_huc *huc)
> >>> +{
> >>> + struct xe_gt *gt = huc_to_gt(huc);
> >>> +
> >>> + CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT);
> >>> + if (!fw_ref.domains)
> >>> + return -ETIMEDOUT;
> >>> +
> >>> + if (!(xe_mmio_read32(>->mmio, GUC_SHIM_CONTROL2) & GSC_LOADS_HUC)) {
> >>> + xe_gt_err(gt, "HuC: HW in DMA mode, but the DG2 blob is GSC-only\n");
> >>> + return -ENOEXEC;
> >>> + }
> >>> +
> >>> + return 0;
> >>> +}
> >>> +
> >>> +/**
> >>> + * xe_huc_mei_init() - set up HuC loading through the GSC on DG2
> >>> + * @huc: the HuC, its firmware already in VRAM
> >>> + *
> >>> + * A failure leaves the HuC unloaded and does not fail the probe.
> >>> + */
> >>> +void xe_huc_mei_init(struct xe_huc *huc)
> >>> +{
> >>> + struct xe_gt *gt = huc_to_gt(huc);
> >>> + struct xe_device *xe = gt_to_xe(gt);
> >>> + struct xe_tile *tile = gt_to_tile(gt);
> >>> + struct xe_huc_mei *mei = &huc->mei;
> >>> + struct xe_bo *bo;
> >>> + int ret;
> >>> +
> >>> + if (xe->info.platform != XE_DG2)
> >>> + return;
> >>> +
> >>> + if (!IS_ENABLED(CONFIG_INTEL_MEI_PXP) || !IS_ENABLED(CONFIG_INTEL_MEI_GSC)) {
> >>> + xe_gt_notice(gt, "HuC: can't load without CONFIG_INTEL_MEI_PXP and CONFIG_INTEL_MEI_GSC\n");
> >>> + xe_uc_fw_change_status(&huc->fw, XE_UC_FIRMWARE_INIT_FAIL);
> >>> + return;
> >>> + }
> >>> +
> >>> + ret = huc_mei_check_mode(huc);
> >>> + if (ret)
> >>> + goto fail;
> >>> +
> >>> + ret = drmm_mutex_init(&xe->drm, &mei->lock);
> >>> + if (ret)
> >>> + goto fail;
> >>> +
> >>> + spin_lock_init(&mei->state_lock);
> >>> + mei->fence_context = dma_fence_context_alloc(1);
> >>> + INIT_WORK(&mei->auth, huc_mei_auth_work);
> >>> + INIT_DELAYED_WORK(&mei->deadline, huc_mei_deadline_work);
> >>> +
> >>> + /* pinned VRAM BOs are contiguous; the GSC reads the page by its DPA */
> >>> + bo = xe_managed_bo_create_pin_map(xe, tile, PAGE_SIZE, XE_BO_FLAG_VRAM_IF_DGFX(tile));
> >>> + if (IS_ERR(bo)) {
> >>> + ret = PTR_ERR(bo);
> >>> + goto fail;
> >>> + }
> >>> + mei->msg = bo;
> >>> + mei->state = XE_HUC_MEI_DOWN;
> >>> +
> >>> + ret = component_add_typed(xe->drm.dev, &huc_mei_component_ops, I915_COMPONENT_PXP);
> >>> + if (ret) {
> >>> + mei->state = XE_HUC_MEI_UNUSED;
> >>> + goto fail;
> >>> + }
> >>> +
> >>> + ret = devm_add_action_or_reset(xe->drm.dev, huc_mei_fini, mei);
> >>> + if (ret) {
> >>> + mei->state = XE_HUC_MEI_UNUSED;
> >>> + goto fail;
> >>> + }
> >>> +
> >>> + return;
> >>> +
> >>> +fail:
> >>> + xe_gt_err(gt, "HuC: can't load through the GSC: %pe\n", ERR_PTR(ret));
> >>> + /* not loadable: no DMA upload or GuC auth of a GSC-only blob either */
> >>> + xe_uc_fw_change_status(&huc->fw, XE_UC_FIRMWARE_INIT_FAIL);
> >>> +}
> >>> +
> >>> +/**
> >>> + * xe_huc_mei_start() - ask the GSC for the HuC once the GT runs
> >>> + * @huc: the HuC
> >>> + *
> >>> + * Called at the end of every uC load (probe, GT reset, resume) with the device
> >>> + * awake. The HuC does not survive those, the GSC must load it again; video
> >>> + * decode submissions wait for it until it settles.
> >>> + */
> >>> +void xe_huc_mei_start(struct xe_huc *huc)
> >>> +{
> >>> + struct xe_huc_mei *mei = &huc->mei;
> >>> + struct dma_fence *load, *old;
> >>> + bool wait;
> >>> +
> >>> + if (!xe_huc_mei_is_used(huc) || !xe_uc_fw_is_loadable(&huc->fw))
> >>> + return;
> >>> +
> >>> + scoped_guard(spinlock_irqsave, &mei->state_lock)
> >>> + wait = !mei->absent;
> >>> +
> >>> + /* without a fence submissions just don't wait */
> >>> + load = wait ? huc_mei_fence_create(mei) : NULL;
> >>> +
> >>> + scoped_guard(spinlock_irqsave, &mei->state_lock) {
> >>> + huc_mei_settle_locked(mei);
> >>> + old = mei->load;
> >>> + mei->state = wait ? XE_HUC_MEI_LOADING : XE_HUC_MEI_SETTLED;
> >>> + mei->load = load;
> >>> + }
> >>> + dma_fence_put(old);
> >>> +
> >>> + if (wait)
> >>> + mod_delayed_work(system_dfl_wq, &mei->deadline,
> >>> + msecs_to_jiffies(HUC_MEI_COMPONENT_TIMEOUT_MS));
> >>> + huc_mei_queue(mei);
> >>> +}
> >>> +
> >>> +/**
> >>> + * xe_huc_mei_stop() - forget the HuC before a reset, a suspend or an FLR
> >>> + * @huc: the HuC
> >>> + *
> >>> + * Waits for a running load and drops a pending one; submissions stop waiting
> >>> + * until the next start().
> >>> + */
> >>> +void xe_huc_mei_stop(struct xe_huc *huc)
> >>> +{
> >>> + struct xe_huc_mei *mei = &huc->mei;
> >>> +
> >>> + if (!xe_huc_mei_is_used(huc))
> >>> + return;
> >>> +
> >>> + huc_mei_go_down(mei);
> >>> + huc_mei_cancel(mei);
> >>> +}
> >>> +
> >>> +/**
> >>> + * xe_huc_mei_load_fence() - the fence of a pending HuC load
> >>> + * @huc: the HuC
> >>> + *
> >>> + * Return: a reference to the fence that signals when the pending load settles,
> >>> + * or NULL when no load is pending.
> >>> + */
> >>> +struct dma_fence *xe_huc_mei_load_fence(struct xe_huc *huc)
> >>> +{
> >>> + struct xe_huc_mei *mei = &huc->mei;
> >>> +
> >>> + if (!xe_huc_mei_is_used(huc))
> >>> + return NULL;
> >>> +
> >>> + guard(spinlock_irqsave)(&mei->state_lock);
> >>> + if (mei->state != XE_HUC_MEI_LOADING || !mei->load)
> >>> + return NULL;
> >>> +
> >>> + return dma_fence_get(mei->load);
> >>> +}
> >>> +
> >>> +/**
> >>> + * xe_huc_mei_wait() - wait until a pending HuC load settled
> >>> + * @huc: the HuC
> >>> + *
> >>> + * A HuC query must not race the load that follows a probe or a resume, or
> >>> + * userspace would see a HuC-less device.
> >>> + *
> >>> + * Return: 0 once settled or timed out, -ERESTARTSYS when interrupted.
> >>> + */
> >>> +int xe_huc_mei_wait(struct xe_huc *huc)
> >>> +{
> >>> + struct dma_fence *load = xe_huc_mei_load_fence(huc);
> >>> + long ret;
> >>> +
> >>> + if (!load)
> >>> + return 0;
> >>> +
> >>> + ret = dma_fence_wait_timeout(load, true, msecs_to_jiffies(HUC_MEI_SETTLE_TIMEOUT_MS));
> >>> + dma_fence_put(load);
> >>> +
> >>> + return ret < 0 ? ret : 0;
> >>> +}
> >>> diff --git a/drivers/gpu/drm/xe/xe_huc_mei.h b/drivers/gpu/drm/xe/xe_huc_mei.h
> >>> new file mode 100644
> >>> index 000000000..bdc68dcca
> >>> --- /dev/null
> >>> +++ b/drivers/gpu/drm/xe/xe_huc_mei.h
> >>> @@ -0,0 +1,21 @@
> >>> +/* SPDX-License-Identifier: MIT */
> >>> +/*
> >>> + * Copyright © 2026 Arisa Snowbell
> >>> + */
> >>> +
> >>> +#ifndef _XE_HUC_MEI_H_
> >>> +#define _XE_HUC_MEI_H_
> >>> +
> >>> +#include <linux/types.h>
> >>> +
> >>> +struct dma_fence;
> >>> +struct xe_huc;
> >>> +
> >>> +bool xe_huc_mei_is_used(struct xe_huc *huc);
> >>> +void xe_huc_mei_init(struct xe_huc *huc);
> >>> +void xe_huc_mei_start(struct xe_huc *huc);
> >>> +void xe_huc_mei_stop(struct xe_huc *huc);
> >>> +struct dma_fence *xe_huc_mei_load_fence(struct xe_huc *huc);
> >>> +int xe_huc_mei_wait(struct xe_huc *huc);
> >>> +
> >>> +#endif
> >>> diff --git a/drivers/gpu/drm/xe/xe_huc_types.h b/drivers/gpu/drm/xe/xe_huc_types.h
> >>> index cfbaa5e0d..d3dd36f72 100644
> >>> --- a/drivers/gpu/drm/xe/xe_huc_types.h
> >>> +++ b/drivers/gpu/drm/xe/xe_huc_types.h
> >>> @@ -6,10 +6,64 @@
> >>> #ifndef _XE_HUC_TYPES_H_
> >>> #define _XE_HUC_TYPES_H_
> >>>
> >>> +#include <linux/atomic.h>
> >>> +#include <linux/mutex.h>
> >>> +#include <linux/spinlock.h>
> >>> +#include <linux/workqueue.h>
> >>> +
> >>> #include "xe_uc_fw_types.h"
> >>>
> >>> +struct dma_fence;
> >>> +struct i915_pxp_component;
> >>> struct xe_bo;
> >>>
> >>> +/**
> >>> + * enum xe_huc_mei_state - where a HuC that the GSC loads through mei is
> >>> + * @XE_HUC_MEI_UNUSED: not loaded through mei (not DG2, or its setup failed)
> >>> + * @XE_HUC_MEI_DOWN: the GT is not running (before the first load, in a reset or
> >>> + * a suspend), there is nothing to load into
> >>> + * @XE_HUC_MEI_LOADING: the GT runs and the GSC load is pending
> >>> + * @XE_HUC_MEI_SETTLED: the load succeeded, failed or stopped waiting for mei
> >>> + */
> >>> +enum xe_huc_mei_state {
> >>> + XE_HUC_MEI_UNUSED,
> >>> + XE_HUC_MEI_DOWN,
> >>> + XE_HUC_MEI_LOADING,
> >>> + XE_HUC_MEI_SETTLED,
> >>> +};
> >>> +
> >>> +/**
> >>> + * struct xe_huc_mei - HuC loaded by the GSC through mei_pxp (DG2)
> >>> + */
> >>> +struct xe_huc_mei {
> >>> + /** @lock: protects @pxp, held across a GSC command */
> >>> + struct mutex lock;
> >>> + /** @pxp: the bound mei PXP component, NULL while unbound */
> >>> + struct i915_pxp_component *pxp;
> >>> + /** @msg: VRAM page for the GSC request and reply */
> >>> + struct xe_bo *msg;
> >>> + /** @auth: asks the GSC to load and authenticate the HuC */
> >>> + struct work_struct auth;
> >>> + /** @deadline: stops waiting for a mei that does not bind */
> >>> + struct delayed_work deadline;
> >>> + /** @state_lock: protects @state, @bound, @absent and @load */
> >>> + spinlock_t state_lock;
> >>> + /** @state: where the load is */
> >>> + enum xe_huc_mei_state state;
> >>> + /** @bound: the mei PXP component is bound */
> >>> + bool bound;
> >>> + /** @absent: mei did not bind in time, later loads don't wait for it */
> >>> + bool absent;
> >>> + /** @announced: the first successful load was logged */
> >>> + bool announced;
> >>> + /** @load: signalled when the pending load settles, NULL when none */
> >>> + struct dma_fence *load;
> >>> + /** @fence_context: dma-fence context of @load */
> >>> + u64 fence_context;
> >>> + /** @seqno: sequence number of the last @load */
> >>> + atomic64_t seqno;
> >>> +};
> >>> +
> >>> /**
> >>> * struct xe_huc - HuC
> >>> */
> >>> @@ -19,6 +73,9 @@ struct xe_huc {
> >>>
> >>> /** @gsc_pkt: bo to store the packet for auth via GSC */
> >>> struct xe_bo *gsc_pkt;
> >>> +
> >>> + /** @mei: loading by the GSC through mei (DG2) */
> >>> + struct xe_huc_mei mei;
> >>> };
> >>>
> >>> #endif
> >>> diff --git a/drivers/gpu/drm/xe/xe_uc.c b/drivers/gpu/drm/xe/xe_uc.c
> >>> index ff13f6e6c..69b632926 100644
> >>> --- a/drivers/gpu/drm/xe/xe_uc.c
> >>> +++ b/drivers/gpu/drm/xe/xe_uc.c
> >>> @@ -20,6 +20,7 @@
> >>> #include "xe_guc_engine_activity.h"
> >>> #include "xe_guc_submit.h"
> >>> #include "xe_huc.h"
> >>> +#include "xe_huc_mei.h"
> >>> #include "xe_mmio.h"
> >>> #include "xe_sriov.h"
> >>> #include "xe_tile.h"
> >>> @@ -374,6 +375,7 @@ int xe_uc_reset_prepare(struct xe_uc *uc)
> >>>
> >>> void xe_uc_stop_prepare(struct xe_uc *uc)
> >>> {
> >>> + xe_huc_mei_stop(&uc->huc);
> >> This should be abstracted as xe_huc_stop_prepare, which internally can
> >> call xe_huc_mei_stop. Note that the GSC is not part of GT on DG2 and
> >> that the HuC is not impacted by GT reset, so I'm not sure we actually
> >> care about the HuC here for DG2. It might still be worth waiting for the
> >> process to complete before triggering the reset.
> >>
> >>> xe_gsc_stop_prepare(&uc->gsc);
> >>> xe_guc_stop_prepare(&uc->guc);
> >>> }
> >>> @@ -444,6 +446,7 @@ void xe_uc_flr_prepare(struct xe_uc *uc)
> >>> struct xe_uc_flr flr = { .uc = uc };
> >>> struct xe_gt *gt = uc_to_gt(uc);
> >>>
> >>> + xe_huc_mei_stop(&uc->huc);
> >> I'd move this inside xe_uc_reset_prepare, which would also cover the
> >> reset case and make the call in xe_uc_stop_prepare redundant. Might end
> >> up waiting a bit for the worker if we go with my suggestion, but if
> >> we're already resetting it shouldn't be a big negative.
> >>
> >>> xe_uc_reset_prepare(uc);
> >>>
> >>> /*
> >>> @@ -485,6 +488,7 @@ int xe_uc_reinit(struct xe_uc *uc)
> >>>
> >>> void xe_uc_suspend_prepare(struct xe_uc *uc)
> >>> {
> >>> + xe_huc_mei_stop(&uc->huc);
> >> We can use xe_huc_stop_prepare here as well since we already need it for
> >> the above use-case
> >>
> >>> xe_gsc_wait_for_worker_completion(&uc->gsc);
> >>> xe_guc_stop_prepare(&uc->guc);
> >>> }
> >>> @@ -495,6 +499,9 @@ int xe_uc_suspend(struct xe_uc *uc)
> >>> if (!xe_device_uc_enabled(uc_to_xe(uc)))
> >>> return 0;
> >>>
> >>> + /* runtime D3cold gets here without xe_uc_suspend_prepare() */
> >>> + xe_huc_mei_stop(&uc->huc);
> >>> +
> >> This shouldn't be needed if we block runtime suspend while huc load is
> >> pending.
> >>
> >> Daniele
> >>
> >>> uc_reset_wait(uc);
> >>>
> >>> xe_uc_stop(uc);
>