Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Ceraolo Spurio, Daniele" <daniele.ceraolospurio@intel.com>
To: <sashiko-reviews@lists.linux.dev>, <intel-xe@lists.freedesktop.org>
Subject: Re: [PATCH v5 2/2] drm/xe/wopcm: Simplify GuC WOPCM programming
Date: Thu, 19 Nov 2026 15:31:32 -0800	[thread overview]
Message-ID: <5745ab1f-ed23-4005-80fa-cf90a1749467@intel.com> (raw)
In-Reply-To: <sashiko-outbox-224176@kernel.org>



On 11/19/2026 3:15 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] drm/xe: u32 underflow of guc_wopcm_size in uc_program_wopcm()
> --
>
> commit 2876ad005940248a7d3945230ac1cf31aa3198e3
> Author: Daniele Ceraolo Spurio <daniele.ceraolospurio@intel.com>
>
> drm/xe/wopcm: Simplify GuC WOPCM programming
>
> This commit removes the separate WOPCM module and layout checks, moving
> GuC WOPCM register programming into xe_uc.c.
>
>> diff --git a/drivers/gpu/drm/xe/xe_uc.c b/drivers/gpu/drm/xe/xe_uc.c
>> index b32ba504041c5..ff13f6e6ccb66 100644
>> --- a/drivers/gpu/drm/xe/xe_uc.c
>> +++ b/drivers/gpu/drm/xe/xe_uc.c
> [ ... ]
>
>> @@ -52,6 +55,126 @@ int xe_uc_init_noalloc(struct xe_uc *uc)
>>   	return ret;
>>   }
>>   
>> +static bool __wopcm_regs_locked(struct xe_gt *gt,
>> +				u32 *guc_wopcm_base, u32 *guc_wopcm_size)
>> +{
>> +	u32 reg_base = xe_mmio_read32(&gt->mmio, DMA_GUC_WOPCM_OFFSET);
>> +	u32 reg_size = xe_mmio_read32(&gt->mmio, GUC_WOPCM_SIZE);
>> +
>> +	if (!(reg_size & GUC_WOPCM_SIZE_LOCKED) ||
>> +	    !(reg_base & GUC_WOPCM_OFFSET_VALID))
>> +		return false;
>> +
>> +	*guc_wopcm_base = reg_base & GUC_WOPCM_OFFSET_MASK;
>> +	*guc_wopcm_size = reg_size & GUC_WOPCM_SIZE_MASK;
>> +	return true;
>> +}
>> +
>> +static int __wopcm_init_regs(struct xe_gt *gt, u32 base, u32 size)
>> +{
>> +	u32 huc_agent = xe_uc_fw_is_available(&gt->uc.huc.fw) ? HUC_LOADING_AGENT_GUC : 0;
>> +	u32 mask;
>> +	int err;
>> +
>> +	xe_gt_assert(gt, !(base & ~GUC_WOPCM_OFFSET_MASK));
>> +	xe_gt_assert(gt, !(size & ~GUC_WOPCM_SIZE_MASK));
>> +
>> +	mask = GUC_WOPCM_SIZE_MASK | GUC_WOPCM_SIZE_LOCKED;
>> +	err = xe_mmio_write32_and_verify(&gt->mmio, GUC_WOPCM_SIZE, size, mask,
>> +					 size | GUC_WOPCM_SIZE_LOCKED);
>> +	if (err)
>> +		goto err_out;
>> +
>> +	mask = GUC_WOPCM_OFFSET_MASK | GUC_WOPCM_OFFSET_VALID | huc_agent;
>> +	err = xe_mmio_write32_and_verify(&gt->mmio, DMA_GUC_WOPCM_OFFSET,
>> +					 base | huc_agent, mask,
>> +					 base | huc_agent |
>> +					 GUC_WOPCM_OFFSET_VALID);
>> +	if (err)
>> +		goto err_out;
>> +
>> +	xe_gt_dbg(gt, "Programmed GuC WOPCM [b=%uK, s=%uK]\n", base / SZ_1K, size / SZ_1K);
>> +
>> +	return 0;
>> +
>> +err_out:
>> +	xe_gt_notice(gt, "Failed to init uC WOPCM registers to [b=%uK, s=%uK]!\n",
>> +		     base / SZ_1K, size / SZ_1K);
>> +	xe_gt_notice(gt, "%s(%#x)=%#x\n", "DMA_GUC_WOPCM_OFFSET",
>> +		     DMA_GUC_WOPCM_OFFSET.addr,
>> +		     xe_mmio_read32(&gt->mmio, DMA_GUC_WOPCM_OFFSET));
>> +	xe_gt_notice(gt, "%s(%#x)=%#x\n", "GUC_WOPCM_SIZE",
>> +		     GUC_WOPCM_SIZE.addr,
>> +		     xe_mmio_read32(&gt->mmio, GUC_WOPCM_SIZE));
>> +
>> +	return err;
>> +}
>> +
>> +static int uc_program_wopcm(struct xe_uc *uc)
>> +{
>> +	struct xe_gt *gt = uc_to_gt(uc);
>> +	struct xe_tile *tile = gt_to_tile(gt);
>> +	u32 huc_fw_size = xe_uc_fw_get_upload_size(&gt->uc.huc.fw);
>> +	u32 wopcm_size = xe_tile_wopcm_size(tile);
>> +	u32 guc_wopcm_base;
>> +	u32 guc_wopcm_size;
>> +	bool locked;
> [ ... ]
>
>> +	/*
>> +	 * On legacy HW (pre-PVC) where the driver is responsible for
>> +	 * programming the WOPCM registers, the WOPCM is divided into three
>> +	 * regions, in order from bottom to top:
>> +	 * 1 - The HuC region, which contains the HuC firmware + a 16KB reserved
>> +	 *     section.
>> +	 * 2 - The GuC region, which contains the GuC firmware + a reserved
>> +	 *     section for GuC's usage.
>> +	 * 3 - A reserved region for HW contexts, which is 36KB in size.
>> +	 *
>> +	 * To keep the math simple, we remove the HW context region from the
>> +	 * WOPCM size, calculate the size of region 1 and then leave the
>> +	 * remaining area for region 2. The official binaries are guaranteed to
>> +	 * fit in the WOPCM, so the math below can only go wrong if a user is
>> +	 * manipulating the blobs. In such a scenario, we'll catch the error at
>> +	 * FW load time, so no need to double verify it here.
>> +	 */
>> +	wopcm_size -= (SZ_32K + SZ_4K);
>> +
>> +	guc_wopcm_base = huc_fw_size + SZ_16K;
>> +	guc_wopcm_base = ALIGN(guc_wopcm_base, 1UL << GUC_WOPCM_OFFSET_SHIFT);
>> +
>> +	guc_wopcm_size = wopcm_size - guc_wopcm_base; /* already 4KB aligned */
>> +
>> +	return __wopcm_init_regs(gt, guc_wopcm_base, guc_wopcm_size);
>> +}
> [Severity: Medium]
> Can guc_wopcm_size underflow u32 here when a modified or oversized HuC
> firmware blob is supplied?

Not with real binaries. If the user is manipulating the binaries to 
purposely cause a value underflow, we'll fail gracefully later at load 
time. The comment above the calculation explains this.
Also to note that this path only applies to pre-MTL platforms, for which 
the official support comes from the i915 driver.

Daniele

>
> Firmware header parsing in parse_headers() only checks that ucode_size and
> rsa_size fit within the loaded file, not within the tile's WOPCM size.  If
> guc_wopcm_base exceeds wopcm_size (or equals it), wopcm_size -
> guc_wopcm_base wraps around (or becomes 0) while remaining 4KB-aligned, so
> it passes the GUC_WOPCM_SIZE_MASK check in __wopcm_init_regs() and locks
> the write-once GUC_WOPCM_SIZE and DMA_GUC_WOPCM_OFFSET hardware registers
> with corrupt values before firmware load runs.
>


  reply	other threads:[~2026-11-19 23:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 23:04 [PATCH v5 1/2] drm/xe/wopcm: Use the size from HW when excluding the wopcm range from GGTT Daniele Ceraolo Spurio
2026-11-19 23:04 ` [PATCH v5 2/2] drm/xe/wopcm: Simplify GuC WOPCM programming Daniele Ceraolo Spurio
2026-11-19 23:15   ` sashiko-bot
2026-11-19 23:31     ` Ceraolo Spurio, Daniele [this message]
2026-11-20  2:32 ` ✗ CI.checkpatch: warning for series starting with [v5,1/2] drm/xe/wopcm: Use the size from HW when excluding the wopcm range from GGTT Patchwork
2026-11-20  2:34 ` ✓ CI.KUnit: success " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5745ab1f-ed23-4005-80fa-cf90a1749467@intel.com \
    --to=daniele.ceraolospurio@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox