* [PATCH v5 01/19] accel: ethosu: Prevent command stream export
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 02/19] accel: ethosu: Move DMA mode to src/dst struct Rob Herring (Arm)
` (17 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
Command stream BOs are validated at creation time, but the generic PRIME
export path can expose their backing memory to a DMA-BUF importer. The
importer can then modify the command stream before the NPU executes it.
Reject PRIME export for validated command stream BOs while retaining
generic export for data BOs.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- No changes.
v3:
- new patch
---
drivers/accel/ethosu/ethosu_gem.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index 9afe2549ec84..c046aee42687 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -6,6 +6,7 @@
#include <linux/slab.h>
#include <drm/ethosu_accel.h>
+#include <drm/drm_prime.h>
#include "ethosu_device.h"
#include "ethosu_gem.h"
@@ -30,9 +31,18 @@ static int ethosu_gem_mmap(struct drm_gem_object *obj, struct vm_area_struct *vm
return drm_gem_dma_object_mmap(obj, vma);
}
+static struct dma_buf *ethosu_gem_export(struct drm_gem_object *obj, int flags)
+{
+ if (to_ethosu_bo(obj)->info)
+ return ERR_PTR(-EPERM);
+
+ return drm_gem_prime_export(obj, flags);
+}
+
static const struct drm_gem_object_funcs ethosu_gem_funcs = {
.free = ethosu_gem_free_object,
.print_info = drm_gem_dma_object_print_info,
+ .export = ethosu_gem_export,
.get_sg_table = drm_gem_dma_object_get_sg_table,
.vmap = drm_gem_dma_object_vmap,
.mmap = ethosu_gem_mmap,
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 02/19] accel: ethosu: Move DMA mode to src/dst struct
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 01/19] accel: ethosu: Prevent command stream export Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 03/19] accel: ethosu: Track command stream register setup Rob Herring (Arm)
` (16 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
U65 DMA mode is programmed independently for source and destination.
Keep it in each endpoint rather than the global DMA state, in preparation
for separating U65 and U85 transfer-length calculations.
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- Clarify that independent source/destination mode is specific to U65;
no code changes.
v2:
- new patch
---
drivers/accel/ethosu/ethosu_gem.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index c046aee42687..2b9c98251c94 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -110,6 +110,7 @@ int ethosu_gem_create_with_handle(struct drm_file *file,
struct dma {
s8 region;
+ s8 mode;
u64 len;
u64 offset;
s64 stride[2];
@@ -118,7 +119,6 @@ struct dma {
struct dma_state {
u16 size0;
u16 size1;
- s8 mode;
struct dma src;
struct dma dst;
};
@@ -171,7 +171,7 @@ static u64 cmd_to_addr(u32 *cmd)
static u64 dma_length(struct ethosu_validated_cmdstream_info *info,
struct dma_state *dma_st, struct dma *dma)
{
- s8 mode = dma_st->mode;
+ s8 mode = dma->mode;
u64 len = dma->len;
if (len == U64_MAX)
@@ -664,13 +664,14 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
st.dma.src.region = -1;
else
st.dma.src.region = param & 0x7;
- st.dma.mode = (param >> 9) & 0x3;
+ st.dma.src.mode = (param >> 9) & 0x3;
break;
case NPU_SET_DMA0_DST_REGION:
if (param & 0x100)
st.dma.dst.region = -1;
else
st.dma.dst.region = param & 0x7;
+ st.dma.dst.mode = (param >> 9) & 0x3;
break;
case NPU_SET_DMA0_SIZE0:
st.dma.size0 = param;
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 03/19] accel: ethosu: Track command stream register setup
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 01/19] accel: ethosu: Prevent command stream export Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 02/19] accel: ethosu: Move DMA mode to src/dst struct Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 04/19] accel: ethosu: Factor buffer bounds checks Rob Herring (Arm)
` (15 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
The current method of tracking command stream state initializing state
tracking to illegal values and assuming unaccessed registers are 0 is
proving inadequate with additional validation. Instead, track all the
registers in a bitmap as the register address space is fairly small.
CMD1 opcodes overlap CMD0 after bit 14 is stripped, so maintain a
separate bitmap for each bank.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- Add the Fixes trailer for the original driver.
v4:
- Use NPU_CMD_CTRL_CMD1 define
v3:
- Fix return value in dma_params_valid()
v2:
- new patch
---
drivers/accel/ethosu/ethosu_device.h | 2 +
drivers/accel/ethosu/ethosu_gem.c | 271 ++++++++++++++++++++++++++++++-----
2 files changed, 237 insertions(+), 36 deletions(-)
diff --git a/drivers/accel/ethosu/ethosu_device.h b/drivers/accel/ethosu/ethosu_device.h
index 1eca8590e68d..7fd9c7439691 100644
--- a/drivers/accel/ethosu/ethosu_device.h
+++ b/drivers/accel/ethosu/ethosu_device.h
@@ -86,6 +86,8 @@ struct gen_pool;
#define PMU_EV_TYPE_CYCLES 0x11
#define PMU_EV_TYPE_IDLE 0x20
+#define NPU_CMD_CTRL_CMD1 BIT(14)
+
enum ethosu_cmds {
NPU_OP_STOP = 0x0,
NPU_OP_CONV = 0x2,
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index 2b9c98251c94..75f6901d19e5 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-only or MIT
/* Copyright 2025 Arm, Ltd. */
+#include <linux/bitmap.h>
#include <linux/err.h>
#include <linux/overflow.h>
#include <linux/slab.h>
@@ -148,7 +149,12 @@ struct feat_matrix {
u8 pad_right;
};
+#define NPU_CMD0_REGS 0x200
+#define NPU_CMD1_REGS 0x100
+
struct cmd_state {
+ DECLARE_BITMAP(cmd0, NPU_CMD0_REGS);
+ DECLARE_BITMAP(cmd1, NPU_CMD1_REGS);
struct dma_state dma;
struct buffer scale[2];
struct buffer weight[4];
@@ -159,8 +165,29 @@ struct cmd_state {
static void cmd_state_init(struct cmd_state *st)
{
- /* Initialize to all 1s to detect missing setup */
- memset(st, 0xff, sizeof(*st));
+ memset(st, 0, sizeof(*st));
+}
+
+static void cmd_state_set_reg(struct cmd_state *st, u16 cmd)
+{
+ u16 reg = cmd & ~NPU_CMD_CTRL_CMD1;
+
+ if (cmd & NPU_CMD_CTRL_CMD1) {
+ if (reg < NPU_CMD1_REGS)
+ __set_bit(reg, st->cmd1);
+ } else if (reg < NPU_CMD0_REGS) {
+ __set_bit(reg, st->cmd0);
+ }
+}
+
+static bool cmd_state_reg_is_set(struct cmd_state *st, u16 cmd)
+{
+ u16 reg = cmd & ~NPU_CMD_CTRL_CMD1;
+
+ if (cmd & NPU_CMD_CTRL_CMD1)
+ return reg < NPU_CMD1_REGS && test_bit(reg, st->cmd1);
+
+ return reg < NPU_CMD0_REGS && test_bit(reg, st->cmd0);
}
static u64 cmd_to_addr(u32 *cmd)
@@ -168,13 +195,54 @@ static u64 cmd_to_addr(u32 *cmd)
return (((u64)cmd[0] & 0xff0000) << 16) | cmd[1];
}
-static u64 dma_length(struct ethosu_validated_cmdstream_info *info,
- struct dma_state *dma_st, struct dma *dma)
+static bool dma_use_src_stride(struct ethosu_device *edev,
+ const struct dma_state *dma_st, const struct dma *dma)
+{
+ return ethosu_is_u65(edev) || dma == &dma_st->src;
+}
+
+static bool dma_params_valid(struct ethosu_device *edev, struct cmd_state *st,
+ const struct dma_state *dma_st,
+ const struct dma *dma,
+ u16 region_cmd, u16 addr_cmd)
+{
+ s8 mode = dma->mode;
+
+ if (!cmd_state_reg_is_set(st, region_cmd) ||
+ !cmd_state_reg_is_set(st, addr_cmd) ||
+ !cmd_state_reg_is_set(st, NPU_SET_DMA0_LEN) || mode < 0 || mode > 2)
+ return false;
+
+ if (mode >= 1 &&
+ !cmd_state_reg_is_set(st, dma_use_src_stride(edev, dma_st, dma) ?
+ NPU_SET_DMA0_SRC_STRIDE0 :
+ NPU_SET_DMA0_DST_STRIDE0))
+ return false;
+ if (mode == 2 &&
+ !cmd_state_reg_is_set(st, dma_use_src_stride(edev, dma_st, dma) ?
+ NPU_SET_DMA0_SRC_STRIDE1 :
+ NPU_SET_DMA0_DST_STRIDE1))
+ return false;
+
+ if (mode >= 1 &&
+ (!cmd_state_reg_is_set(st, NPU_SET_DMA0_SIZE0) || !dma_st->size0))
+ return false;
+ if (mode == 2 &&
+ (!cmd_state_reg_is_set(st, NPU_SET_DMA0_SIZE1) || !dma_st->size1))
+ return false;
+
+ return true;
+}
+
+static u64 dma_length(struct ethosu_device *edev,
+ struct ethosu_validated_cmdstream_info *info,
+ struct cmd_state *st, struct dma_state *dma_st,
+ struct dma *dma, u16 region_cmd, u16 addr_cmd)
{
s8 mode = dma->mode;
u64 len = dma->len;
- if (len == U64_MAX)
+ if (!dma_params_valid(edev, st, dma_st, dma, region_cmd, addr_cmd))
return U64_MAX;
if (mode >= 1) {
@@ -209,17 +277,98 @@ static bool feat_matrix_chained(struct ethosu_device *edev, struct feat_matrix *
return !ethosu_is_u65(edev) && storage == 2;
}
+enum feat_matrix_type {
+ FEAT_MATRIX_IFM,
+ FEAT_MATRIX_OFM,
+ FEAT_MATRIX_IFM2,
+};
+
+static u16 feat_matrix_base_cmd(enum feat_matrix_type type)
+{
+ switch (type) {
+ case FEAT_MATRIX_IFM:
+ return NPU_SET_IFM_BASE0;
+ case FEAT_MATRIX_OFM:
+ return NPU_SET_OFM_BASE0;
+ case FEAT_MATRIX_IFM2:
+ return NPU_SET_IFM2_BASE0;
+ }
+
+ return 0;
+}
+
+static int feat_matrix_validate(struct ethosu_device *edev,
+ struct cmd_state *st, struct feat_matrix *fm,
+ enum feat_matrix_type type)
+{
+ u32 format;
+ u16 stride_cmd;
+
+ switch (type) {
+ case FEAT_MATRIX_IFM:
+ if (!cmd_state_reg_is_set(st, NPU_SET_IFM_REGION) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM_PRECISION) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM_DEPTH_M1))
+ return -EINVAL;
+ if (feat_matrix_chained(edev, fm))
+ return 0;
+ if (!cmd_state_reg_is_set(st, NPU_SET_IFM_WIDTH0_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM_HEIGHT0_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM_HEIGHT1_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM_STRIDE_Y))
+ return -EINVAL;
+ break;
+ case FEAT_MATRIX_OFM:
+ if (!cmd_state_reg_is_set(st, NPU_SET_OFM_REGION) ||
+ !cmd_state_reg_is_set(st, NPU_SET_OFM_PRECISION) ||
+ !cmd_state_reg_is_set(st, NPU_SET_OFM_DEPTH_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_OFM_WIDTH_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_OFM_HEIGHT_M1))
+ return -EINVAL;
+ if (feat_matrix_chained(edev, fm))
+ return 0;
+ if (!cmd_state_reg_is_set(st, NPU_SET_OFM_WIDTH0_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_OFM_HEIGHT0_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_OFM_HEIGHT1_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_OFM_STRIDE_Y))
+ return -EINVAL;
+ break;
+ case FEAT_MATRIX_IFM2:
+ if (!cmd_state_reg_is_set(st, NPU_SET_IFM2_REGION) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM2_PRECISION))
+ return -EINVAL;
+ if (feat_matrix_chained(edev, fm))
+ return 0;
+ if (!cmd_state_reg_is_set(st, NPU_SET_IFM2_WIDTH0_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM2_HEIGHT0_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM2_HEIGHT1_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM2_STRIDE_Y))
+ return -EINVAL;
+ break;
+ }
+
+ format = (fm->precision >> 6) & 0x3;
+ stride_cmd = feat_matrix_base_cmd(type) + (format ? 6 : 4);
+ if (!cmd_state_reg_is_set(st, stride_cmd))
+ return -EINVAL;
+
+ return 0;
+}
static u64 feat_matrix_length(struct ethosu_device *edev,
struct ethosu_validated_cmdstream_info *info,
- struct feat_matrix *fm,
+ struct cmd_state *st, struct feat_matrix *fm,
+ enum feat_matrix_type type,
u32 x, u32 y, u32 c, bool ofm)
{
u32 element_size, storage = ethosu_is_u65(edev) ? 0 : fm->precision >> 14;
int tile = 0;
u64 addr;
+ u64 offset;
if (fm->region < 0)
return U64_MAX;
+ if (feat_matrix_validate(edev, st, fm, type))
+ return U64_MAX;
if (feat_matrix_chained(edev, fm))
return 0;
@@ -247,24 +396,39 @@ static u64 feat_matrix_length(struct ethosu_device *edev,
default:
return U64_MAX;
}
- if (fm->base[tile] == U64_MAX)
+ if (!cmd_state_reg_is_set(st, feat_matrix_base_cmd(type) + tile))
return U64_MAX;
- addr = fm->base[tile] + y * fm->stride_y;
+ if (check_mul_overflow(y, (u64)fm->stride_y, &offset) ||
+ check_add_overflow(fm->base[tile], offset, &addr))
+ return U64_MAX;
switch ((fm->precision >> 6) & 0x3) { // format
case 0: //nhwc:
element_size = BIT((fm->precision >> (ofm ? 1 : 2)) & 0x3);
- addr += x * fm->stride_x + c * element_size;
+ if (check_mul_overflow(x, (u64)fm->stride_x, &offset) ||
+ check_add_overflow(addr, offset, &addr) ||
+ check_mul_overflow(c, element_size, &offset) ||
+ check_add_overflow(addr, offset, &addr))
+ return U64_MAX;
break;
case 1: //nhcwb16:
element_size = BIT((fm->precision >> (ofm ? 1 : 2)) & 0x3);
- addr += (c / 16) * fm->stride_c + (16 * x + (c & 0xf)) * element_size;
+ if (check_mul_overflow(c / 16, (u64)fm->stride_c, &offset) ||
+ check_add_overflow(addr, offset, &addr) ||
+ check_mul_overflow(16 * x + (c & 0xf), element_size, &offset) ||
+ check_add_overflow(addr, offset, &addr))
+ return U64_MAX;
break;
+ default:
+ return U64_MAX;
}
- info->region_size[fm->region] = max(info->region_size[fm->region], addr + 1);
+ if (check_add_overflow(addr, 1ULL, &offset))
+ return U64_MAX;
+
+ info->region_size[fm->region] = max(info->region_size[fm->region], offset);
return addr;
}
@@ -278,7 +442,13 @@ static int calc_sizes(struct drm_device *ddev,
u64 len;
if (ifm) {
- if (st->ifm.stride_kernel == U16_MAX)
+ if (!cmd_state_reg_is_set(st, NPU_SET_KERNEL_WIDTH_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_KERNEL_HEIGHT_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_KERNEL_STRIDE) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM_PAD_TOP) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM_PAD_LEFT) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM_PAD_RIGHT) ||
+ !cmd_state_reg_is_set(st, NPU_SET_IFM_PAD_BOTTOM))
return -EINVAL;
u32 stride_y = ((st->ifm.stride_kernel >> 8) & 0x2) +
((st->ifm.stride_kernel >> 1) & 0x1) + 1;
@@ -292,8 +462,9 @@ static int calc_sizes(struct drm_device *ddev,
if (ifm_height < 0 || ifm_width < 0)
return -EINVAL;
- len = feat_matrix_length(edev, info, &st->ifm, ifm_width,
- ifm_height, st->ifm.depth, false);
+ len = feat_matrix_length(edev, info, st, &st->ifm,
+ FEAT_MATRIX_IFM, ifm_width, ifm_height,
+ st->ifm.depth, false);
dev_dbg(ddev->dev, "op %d: IFM:%d:0x%llx-0x%llx\n",
op, st->ifm.region, st->ifm.base[0], len);
if (len == U64_MAX)
@@ -301,8 +472,9 @@ static int calc_sizes(struct drm_device *ddev,
}
if (ifm2) {
- len = feat_matrix_length(edev, info, &st->ifm2, st->ifm.depth,
- 0, st->ofm.depth, false);
+ len = feat_matrix_length(edev, info, st, &st->ifm2,
+ FEAT_MATRIX_IFM2, st->ifm.depth, 0,
+ st->ofm.depth, false);
dev_dbg(ddev->dev, "op %d: IFM2:%d:0x%llx-0x%llx\n",
op, st->ifm2.region, st->ifm2.base[0], len);
if (len == U64_MAX)
@@ -313,8 +485,9 @@ static int calc_sizes(struct drm_device *ddev,
dev_dbg(ddev->dev, "op %d: W:%d:0x%llx-0x%llx\n",
op, st->weight[0].region, st->weight[0].base,
st->weight[0].base + st->weight[0].length - 1);
- if (st->weight[0].region < 0 || st->weight[0].base == U64_MAX ||
- st->weight[0].length == U32_MAX)
+ if (!cmd_state_reg_is_set(st, NPU_SET_WEIGHT_REGION) ||
+ !cmd_state_reg_is_set(st, NPU_SET_WEIGHT_BASE) ||
+ !cmd_state_reg_is_set(st, NPU_SET_WEIGHT_LENGTH))
return -EINVAL;
info->region_size[st->weight[0].region] =
max(info->region_size[st->weight[0].region],
@@ -325,16 +498,18 @@ static int calc_sizes(struct drm_device *ddev,
dev_dbg(ddev->dev, "op %d: S:%d:0x%llx-0x%llx\n",
op, st->scale[0].region, st->scale[0].base,
st->scale[0].base + st->scale[0].length - 1);
- if (st->scale[0].region < 0 || st->scale[0].base == U64_MAX ||
- st->scale[0].length == U32_MAX)
+ if (!cmd_state_reg_is_set(st, NPU_SET_SCALE_REGION) ||
+ !cmd_state_reg_is_set(st, NPU_SET_SCALE_BASE) ||
+ !cmd_state_reg_is_set(st, NPU_SET_SCALE_LENGTH))
return -EINVAL;
info->region_size[st->scale[0].region] =
max(info->region_size[st->scale[0].region],
st->scale[0].base + st->scale[0].length);
}
- len = feat_matrix_length(edev, info, &st->ofm, st->ofm.width,
- st->ofm.height[2], st->ofm.depth, true);
+ len = feat_matrix_length(edev, info, st, &st->ofm, FEAT_MATRIX_OFM,
+ st->ofm.width, st->ofm.height[2], st->ofm.depth,
+ true);
dev_dbg(ddev->dev, "op %d: OFM:%d:0x%llx-0x%llx\n",
op, st->ofm.region, st->ofm.base[0], len);
if (len == U64_MAX)
@@ -359,8 +534,8 @@ static int calc_sizes_elemwise(struct drm_device *ddev,
width = st->ifm.broadcast & 0x2 ? 0 : st->ofm.width;
depth = st->ifm.broadcast & 0x4 ? 0 : st->ofm.depth;
- len = feat_matrix_length(edev, info, &st->ifm, width,
- height, depth, false);
+ len = feat_matrix_length(edev, info, st, &st->ifm,
+ FEAT_MATRIX_IFM, width, height, depth, false);
dev_dbg(ddev->dev, "op %d: IFM:%d:0x%llx-0x%llx\n",
op, st->ifm.region, st->ifm.base[0], len);
if (len == U64_MAX)
@@ -372,16 +547,17 @@ static int calc_sizes_elemwise(struct drm_device *ddev,
width = st->ifm2.broadcast & 0x2 ? 0 : st->ofm.width;
depth = st->ifm2.broadcast & 0x4 ? 0 : st->ofm.depth;
- len = feat_matrix_length(edev, info, &st->ifm2, width,
- height, depth, false);
+ len = feat_matrix_length(edev, info, st, &st->ifm2,
+ FEAT_MATRIX_IFM2, width, height, depth, false);
dev_dbg(ddev->dev, "op %d: IFM2:%d:0x%llx-0x%llx\n",
op, st->ifm2.region, st->ifm2.base[0], len);
if (len == U64_MAX)
return -EINVAL;
}
- len = feat_matrix_length(edev, info, &st->ofm, st->ofm.width,
- st->ofm.height[2], st->ofm.depth, true);
+ len = feat_matrix_length(edev, info, st, &st->ofm, FEAT_MATRIX_OFM,
+ st->ofm.width, st->ofm.height[2], st->ofm.depth,
+ true);
dev_dbg(ddev->dev, "op %d: OFM:%d:0x%llx-0x%llx\n",
op, st->ofm.region, st->ofm.base[0], len);
if (len == U64_MAX)
@@ -425,7 +601,7 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
cmd = cmds[0];
param = cmds[0] >> 16;
- if (cmd & 0x4000) {
+ if (cmd & NPU_CMD_CTRL_CMD1) {
if (get_user(cmds[1], ucmds++))
return -EFAULT;
@@ -436,6 +612,8 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
addr = cmd_to_addr(cmds);
}
+ cmd_state_set_reg(&st, cmd);
+
switch (cmd) {
case NPU_OP_STOP:
if (i != size / 4 - 1)
@@ -443,8 +621,10 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
ends_with_stop = true;
break;
case NPU_OP_DMA_START:
- srclen = dma_length(info, &st.dma, &st.dma.src);
- dstlen = dma_length(info, &st.dma, &st.dma.dst);
+ srclen = dma_length(edev, info, &st, &st.dma, &st.dma.src,
+ NPU_SET_DMA0_SRC_REGION, NPU_SET_DMA0_SRC);
+ dstlen = dma_length(edev, info, &st, &st.dma, &st.dma.dst,
+ NPU_SET_DMA0_DST_REGION, NPU_SET_DMA0_DST);
if (srclen == U64_MAX || dstlen == U64_MAX)
return -EINVAL;
@@ -455,16 +635,28 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
st.dma.dst.region, st.dma.dst.offset, dstlen);
break;
case NPU_OP_CONV:
- case NPU_OP_DEPTHWISE:
use_ifm2 = param & 0x1; // weights_ifm2
+ if (!cmd_state_reg_is_set(&st, NPU_SET_OFM_PRECISION))
+ return -EINVAL;
use_scale = !(st.ofm.precision & 0x100);
ret = calc_sizes(ddev, info, cmd, &st, true, use_ifm2,
!use_ifm2, use_scale);
if (ret)
return ret;
break;
+ case NPU_OP_DEPTHWISE:
+ if (!cmd_state_reg_is_set(&st, NPU_SET_OFM_PRECISION))
+ return -EINVAL;
+ use_scale = !(st.ofm.precision & 0x100);
+ ret = calc_sizes(ddev, info, cmd, &st, true, false, true,
+ use_scale);
+ if (ret)
+ return ret;
+ break;
case NPU_OP_POOL:
use_ifm = param != 0x4; // pooling mode
+ if (!cmd_state_reg_is_set(&st, NPU_SET_OFM_PRECISION))
+ return -EINVAL;
use_scale = !(st.ofm.precision & 0x100);
ret = calc_sizes(ddev, info, cmd, &st, use_ifm, false,
false, use_scale);
@@ -472,11 +664,18 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
return ret;
break;
case NPU_OP_ELEMENTWISE:
- use_scale = ethosu_is_u65(edev) ?
+ if (!ethosu_is_u65(edev) &&
+ !cmd_state_reg_is_set(&st, NPU_SET_IFM_BROADCAST))
+ return -EINVAL;
+ use_ifm2 = (param != 5) && (param != 6) &&
+ (param != 7) && (param != 0x24);
+ if (use_ifm2 &&
+ !cmd_state_reg_is_set(&st, NPU_SET_IFM2_BROADCAST))
+ return -EINVAL;
+ use_scale = use_ifm2 && (ethosu_is_u65(edev) ?
(st.ifm2.broadcast & 0x80) :
- (st.ifm2.broadcast == 8);
- use_ifm2 = !(use_scale || (param == 5) ||
- (param == 6) || (param == 7) || (param == 0x24));
+ (st.ifm2.broadcast == 8));
+ use_ifm2 = use_ifm2 && !use_scale;
use_ifm = st.ifm.broadcast != 8;
ret = calc_sizes_elemwise(ddev, info, cmd, &st, use_ifm, use_ifm2);
if (ret)
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 04/19] accel: ethosu: Factor buffer bounds checks
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (2 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 03/19] accel: ethosu: Track command stream register setup Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 05/19] accel: ethosu: Fix NHCWB16 bounds calculation Rob Herring (Arm)
` (14 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
Move the repeated command-stream buffer range validation into a
helper in preparation for validating all weight and scale streams.
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- No changes.
v2:
- Adjust due to previous patch
---
drivers/accel/ethosu/ethosu_gem.c | 37 +++++++++++++++++++++++++------------
1 file changed, 25 insertions(+), 12 deletions(-)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index 75f6901d19e5..24f88696c18b 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -433,6 +433,25 @@ static u64 feat_matrix_length(struct ethosu_device *edev,
return addr;
}
+static int buffer_size(struct ethosu_validated_cmdstream_info *info,
+ struct cmd_state *st, struct buffer *buf, s8 region,
+ u16 region_cmd, u16 base_cmd, u16 length_cmd)
+{
+ u64 end;
+
+ if (region < 0 || !cmd_state_reg_is_set(st, region_cmd) ||
+ !cmd_state_reg_is_set(st, base_cmd) ||
+ !cmd_state_reg_is_set(st, length_cmd))
+ return -EINVAL;
+
+ if (check_add_overflow(buf->base, (u64)buf->length, &end))
+ return -EINVAL;
+
+ info->region_size[region] = max(info->region_size[region], end);
+
+ return 0;
+}
+
static int calc_sizes(struct drm_device *ddev,
struct ethosu_validated_cmdstream_info *info,
u16 op, struct cmd_state *st,
@@ -485,26 +504,20 @@ static int calc_sizes(struct drm_device *ddev,
dev_dbg(ddev->dev, "op %d: W:%d:0x%llx-0x%llx\n",
op, st->weight[0].region, st->weight[0].base,
st->weight[0].base + st->weight[0].length - 1);
- if (!cmd_state_reg_is_set(st, NPU_SET_WEIGHT_REGION) ||
- !cmd_state_reg_is_set(st, NPU_SET_WEIGHT_BASE) ||
- !cmd_state_reg_is_set(st, NPU_SET_WEIGHT_LENGTH))
+ if (buffer_size(info, st, &st->weight[0], st->weight[0].region,
+ NPU_SET_WEIGHT_REGION, NPU_SET_WEIGHT_BASE,
+ NPU_SET_WEIGHT_LENGTH))
return -EINVAL;
- info->region_size[st->weight[0].region] =
- max(info->region_size[st->weight[0].region],
- st->weight[0].base + st->weight[0].length);
}
if (scale) {
dev_dbg(ddev->dev, "op %d: S:%d:0x%llx-0x%llx\n",
op, st->scale[0].region, st->scale[0].base,
st->scale[0].base + st->scale[0].length - 1);
- if (!cmd_state_reg_is_set(st, NPU_SET_SCALE_REGION) ||
- !cmd_state_reg_is_set(st, NPU_SET_SCALE_BASE) ||
- !cmd_state_reg_is_set(st, NPU_SET_SCALE_LENGTH))
+ if (buffer_size(info, st, &st->scale[0], st->scale[0].region,
+ NPU_SET_SCALE_REGION, NPU_SET_SCALE_BASE,
+ NPU_SET_SCALE_LENGTH))
return -EINVAL;
- info->region_size[st->scale[0].region] =
- max(info->region_size[st->scale[0].region],
- st->scale[0].base + st->scale[0].length);
}
len = feat_matrix_length(edev, info, st, &st->ofm, FEAT_MATRIX_OFM,
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 05/19] accel: ethosu: Fix NHCWB16 bounds calculation
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (3 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 04/19] accel: ethosu: Factor buffer bounds checks Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 06/19] accel: ethosu: Validate secondary streams Rob Herring (Arm)
` (13 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel, sashiko-bot
The Ethos-U NPU inherently processes NHCWB16 data in 16-channel bricks.
The NHCWB16 address calculation uses the final channel of the last
accessed brick. If the channel depth modulo 16 is less than 15, the
hardware will still read or write the full 16-element brick.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Reported-by: sashiko-bot@kernel.org
Closes: https://lore.kernel.org/all/20260905010523.866411F00A3D@smtp.kernel.org/
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- No changes.
v3:
- new patch
---
drivers/accel/ethosu/ethosu_gem.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index 24f88696c18b..67c7cbbd39b8 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -415,9 +415,10 @@ static u64 feat_matrix_length(struct ethosu_device *edev,
case 1: //nhcwb16:
element_size = BIT((fm->precision >> (ofm ? 1 : 2)) & 0x3);
+ /* NHCWB16 accesses the complete final channel brick. */
if (check_mul_overflow(c / 16, (u64)fm->stride_c, &offset) ||
check_add_overflow(addr, offset, &addr) ||
- check_mul_overflow(16 * x + (c & 0xf), element_size, &offset) ||
+ check_mul_overflow(16 * x + 15, element_size, &offset) ||
check_add_overflow(addr, offset, &addr))
return U64_MAX;
break;
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 06/19] accel: ethosu: Validate secondary streams
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (4 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 05/19] accel: ethosu: Fix NHCWB16 bounds calculation Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 07/19] accel: ethosu: Reject unsupported commands and params Rob Herring (Arm)
` (12 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
The command-stream validator records the additional U65 scale and
weight stream addresses and the U85 weight decoder addresses, but only
checked stream 0 against its region buffer.
Check every configured secondary stream against the matching weight or
scale region before accepting a kernel operation.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- No changes.
v2:
- Adjust for register state tracking
---
drivers/accel/ethosu/ethosu_gem.c | 33 ++++++++++++++++++++++++++++-----
1 file changed, 28 insertions(+), 5 deletions(-)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index 67c7cbbd39b8..af8a0c45039b 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -436,13 +436,17 @@ static u64 feat_matrix_length(struct ethosu_device *edev,
static int buffer_size(struct ethosu_validated_cmdstream_info *info,
struct cmd_state *st, struct buffer *buf, s8 region,
- u16 region_cmd, u16 base_cmd, u16 length_cmd)
+ u16 region_cmd, u16 base_cmd, u16 length_cmd, bool optional)
{
u64 end;
+ bool base_set = cmd_state_reg_is_set(st, base_cmd);
+ bool length_set = cmd_state_reg_is_set(st, length_cmd);
+
+ if (optional && !base_set && !length_set)
+ return 0;
if (region < 0 || !cmd_state_reg_is_set(st, region_cmd) ||
- !cmd_state_reg_is_set(st, base_cmd) ||
- !cmd_state_reg_is_set(st, length_cmd))
+ !base_set || !length_set)
return -EINVAL;
if (check_add_overflow(buf->base, (u64)buf->length, &end))
@@ -507,7 +511,20 @@ static int calc_sizes(struct drm_device *ddev,
st->weight[0].base + st->weight[0].length - 1);
if (buffer_size(info, st, &st->weight[0], st->weight[0].region,
NPU_SET_WEIGHT_REGION, NPU_SET_WEIGHT_BASE,
- NPU_SET_WEIGHT_LENGTH))
+ NPU_SET_WEIGHT_LENGTH, false))
+ return -EINVAL;
+
+ if (buffer_size(info, st, &st->weight[1], st->weight[0].region,
+ NPU_SET_WEIGHT_REGION, NPU_SET_WEIGHT1_BASE,
+ NPU_SET_WEIGHT1_LENGTH, true) ||
+ buffer_size(info, st, &st->weight[3], st->weight[0].region,
+ NPU_SET_WEIGHT_REGION, NPU_SET_WEIGHT3_BASE,
+ NPU_SET_WEIGHT3_LENGTH, true))
+ return -EINVAL;
+ if (!ethosu_is_u65(edev) &&
+ buffer_size(info, st, &st->weight[2], st->weight[0].region,
+ NPU_SET_WEIGHT_REGION, NPU_SET_WEIGHT2_BASE,
+ NPU_SET_WEIGHT2_LENGTH, true))
return -EINVAL;
}
@@ -517,7 +534,13 @@ static int calc_sizes(struct drm_device *ddev,
st->scale[0].base + st->scale[0].length - 1);
if (buffer_size(info, st, &st->scale[0], st->scale[0].region,
NPU_SET_SCALE_REGION, NPU_SET_SCALE_BASE,
- NPU_SET_SCALE_LENGTH))
+ NPU_SET_SCALE_LENGTH, false))
+ return -EINVAL;
+
+ if (ethosu_is_u65(edev) &&
+ buffer_size(info, st, &st->scale[1], st->scale[0].region,
+ NPU_SET_SCALE_REGION, NPU_SET_SCALE1_BASE,
+ NPU_SET_SCALE1_LENGTH, true))
return -EINVAL;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 07/19] accel: ethosu: Reject unsupported commands and params
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (5 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 06/19] accel: ethosu: Validate secondary streams Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 08/19] accel: ethosu: Validate all feature map tiles Rob Herring (Arm)
` (11 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
The command-stream validator does not model U85 branches, indexed DMA,
or OFM transposes. A branch can bypass the linear validation state,
indexed DMA accesses an unchecked index buffer, and a transpose changes
the feature-map address calculation.
Reject any unhandled operation commands or out of range registers, as
well as the reserved DMA stride mode and feature-map formats. Reject
command-stream IRQs because they can signal job completion before later
commands finish.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- Handle undefined pooling and elementwise modes
v4:
- Combine "accel: ethosu: Reject unsupported commands" and "accel:
ethosu: Reject reserved command encodings"
- Move NPU_CMD_CTRL_CMD1 to "accel: ethosu: Track command stream register setup"
- Rework checks for unknown op cmds and reserved bits
v2:
- no changes
---
drivers/accel/ethosu/ethosu_device.h | 4 +++
drivers/accel/ethosu/ethosu_gem.c | 48 +++++++++++++++++++++++++++++++++++-
2 files changed, 51 insertions(+), 1 deletion(-)
diff --git a/drivers/accel/ethosu/ethosu_device.h b/drivers/accel/ethosu/ethosu_device.h
index 7fd9c7439691..1ea7638287f0 100644
--- a/drivers/accel/ethosu/ethosu_device.h
+++ b/drivers/accel/ethosu/ethosu_device.h
@@ -86,6 +86,8 @@ struct gen_pool;
#define PMU_EV_TYPE_CYCLES 0x11
#define PMU_EV_TYPE_IDLE 0x20
+#define NPU_DMA_REGION_INDEX_MODE BIT(11)
+
#define NPU_CMD_CTRL_CMD1 BIT(14)
enum ethosu_cmds {
@@ -96,6 +98,8 @@ enum ethosu_cmds {
NPU_OP_ELEMENTWISE = 0x6,
NPU_OP_RESIZE = 0x7, // U85 only
NPU_OP_DMA_START = 0x10,
+ NPU_OP_DMA_WAIT = 0x11,
+ NPU_OP_KERNEL_WAIT = 0x12,
NPU_SET_IFM_PAD_TOP = 0x100,
NPU_SET_IFM_PAD_LEFT = 0x101,
NPU_SET_IFM_PAD_RIGHT = 0x102,
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index af8a0c45039b..9a66d8439408 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -190,6 +190,16 @@ static bool cmd_state_reg_is_set(struct cmd_state *st, u16 cmd)
return reg < NPU_CMD0_REGS && test_bit(reg, st->cmd0);
}
+static bool cmd_is_register(u16 cmd)
+{
+ u16 reg = cmd & ~NPU_CMD_CTRL_CMD1;
+
+ if (cmd & NPU_CMD_CTRL_CMD1)
+ return reg < NPU_CMD1_REGS;
+
+ return cmd >= 0x100 && cmd < NPU_CMD0_REGS;
+}
+
static u64 cmd_to_addr(u32 *cmd)
{
return (((u64)cmd[0] & 0xff0000) << 16) | cmd[1];
@@ -652,6 +662,9 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
cmd_state_set_reg(&st, cmd);
switch (cmd) {
+ case NPU_OP_DMA_WAIT:
+ case NPU_OP_KERNEL_WAIT:
+ break;
case NPU_OP_STOP:
if (i != size / 4 - 1)
return -EINVAL;
@@ -691,6 +704,8 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
return ret;
break;
case NPU_OP_POOL:
+ if (param > (ethosu_is_u65(edev) ? 2 : 7))
+ return -EINVAL;
use_ifm = param != 0x4; // pooling mode
if (!cmd_state_reg_is_set(&st, NPU_SET_OFM_PRECISION))
return -EINVAL;
@@ -701,6 +716,19 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
return ret;
break;
case NPU_OP_ELEMENTWISE:
+ switch (param) {
+ case 0 ... 9:
+ break;
+ case 10 ... 11: /* lsr, div */
+ case 16 ... 19: /* comparisons */
+ case 33 ... 36: /* bitwise operations */
+ case 42: /* and_not */
+ if (ethosu_is_u65(edev))
+ return -EINVAL;
+ break;
+ default:
+ return -EINVAL;
+ }
if (!ethosu_is_u65(edev) &&
!cmd_state_reg_is_set(&st, NPU_SET_IFM_BROADCAST))
return -EINVAL;
@@ -745,6 +773,8 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
st.ifm.depth = param;
break;
case NPU_SET_IFM_PRECISION:
+ if (((param >> 6) & 0x3) > 1)
+ return -EINVAL;
st.ifm.precision = param;
break;
case NPU_SET_IFM_BROADCAST:
@@ -788,6 +818,10 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
st.ofm.depth = param;
break;
case NPU_SET_OFM_PRECISION:
+ if (((param >> 6) & 0x3) > 1)
+ return -EINVAL;
+ if (!ethosu_is_u65(edev) && (param & GENMASK(13, 11)))
+ return -EINVAL;
st.ofm.precision = param;
break;
case NPU_SET_OFM_REGION:
@@ -822,6 +856,8 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
st.ifm2.broadcast = param;
break;
case NPU_SET_IFM2_PRECISION:
+ if (((param >> 6) & 0x3) > 1)
+ return -EINVAL;
st.ifm2.precision = param;
break;
case NPU_SET_IFM2_REGION:
@@ -896,18 +932,26 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
break;
case NPU_SET_DMA0_SRC_REGION:
+ if (param & NPU_DMA_REGION_INDEX_MODE)
+ return -EINVAL;
if (param & 0x100)
st.dma.src.region = -1;
else
st.dma.src.region = param & 0x7;
st.dma.src.mode = (param >> 9) & 0x3;
+ if (st.dma.src.mode == 3)
+ return -EINVAL;
break;
case NPU_SET_DMA0_DST_REGION:
+ if (param & NPU_DMA_REGION_INDEX_MODE)
+ return -EINVAL;
if (param & 0x100)
st.dma.dst.region = -1;
else
st.dma.dst.region = param & 0x7;
st.dma.dst.mode = (param >> 9) & 0x3;
+ if (st.dma.dst.mode == 3)
+ return -EINVAL;
break;
case NPU_SET_DMA0_SIZE0:
st.dma.size0 = param;
@@ -937,7 +981,9 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
st.dma.src.len = st.dma.dst.len = addr;
break;
default:
- break;
+ if (cmd_is_register(cmd))
+ break;
+ return -EINVAL;
}
}
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 08/19] accel: ethosu: Validate all feature map tiles
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (6 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 07/19] accel: ethosu: Reject unsupported commands and params Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 09/19] accel: ethosu: Account for feature map element size Rob Herring (Arm)
` (10 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
The command-stream validator checked only the final feature-map
coordinate. For tiled tensors, this can leave an earlier tile base
address unchecked even though the operation accesses it.
Check the final coordinate of every tile touched by an operation. Also
treat U65 feature maps as 2x2 tiled: its precision rounding bits are not
the U85 storage encoding.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- No changes.
v4:
- NHCWB16 feature maps may be vertically tiled. Account for tile 0's
maximum Y coordinate when calculating the feature map bounds.
v3:
- Fix for storage mode 0 intermediate tile calculations
v2:
- no changes
---
drivers/accel/ethosu/ethosu_gem.c | 147 ++++++++++++++++++++++++++++++--------
1 file changed, 119 insertions(+), 28 deletions(-)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index 9a66d8439408..11f41382dc5d 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -444,6 +444,96 @@ static u64 feat_matrix_length(struct ethosu_device *edev,
return addr;
}
+static int feat_matrix_check_location(struct ethosu_device *edev,
+ struct ethosu_validated_cmdstream_info *info,
+ struct cmd_state *st, struct feat_matrix *fm,
+ enum feat_matrix_type type, u32 x, u32 y,
+ u32 c, bool ofm, u64 *max_len)
+{
+ u64 len;
+
+ len = feat_matrix_length(edev, info, st, fm, type, x, y, c, ofm);
+ if (len == U64_MAX)
+ return -EINVAL;
+
+ *max_len = max(*max_len, len);
+ return 0;
+}
+
+static int feat_matrix_size(struct ethosu_device *edev,
+ struct ethosu_validated_cmdstream_info *info,
+ struct cmd_state *st, struct feat_matrix *fm,
+ enum feat_matrix_type type,
+ u32 x, u32 y, u32 c, bool ofm, u64 *max_len)
+{
+ u32 storage = ethosu_is_u65(edev) ? 0 : fm->precision >> 14;
+ int ret;
+
+ *max_len = 0;
+
+ if (storage == 0) {
+ ret = feat_matrix_check_location(edev, info, st, fm, type, 0, 0,
+ c, ofm, max_len);
+ if (ret)
+ return ret;
+
+ ret = feat_matrix_check_location(edev, info, st, fm, type,
+ min(x, (u32)fm->width0),
+ min(y, (u32)fm->height[0]), c, ofm,
+ max_len);
+ if (ret)
+ return ret;
+
+ if (fm->width0 < x) {
+ ret = feat_matrix_check_location(edev, info, st, fm, type, x,
+ min(y, (u32)fm->height[1]), c,
+ ofm, max_len);
+ if (ret)
+ return ret;
+ }
+
+ if (fm->height[0] < y) {
+ ret = feat_matrix_check_location(edev, info, st, fm, type,
+ min(x, (u32)fm->width0), y, c,
+ ofm, max_len);
+ if (ret)
+ return ret;
+ }
+
+ if (fm->width0 < x && fm->height[1] < y)
+ return feat_matrix_check_location(edev, info, st, fm, type,
+ x, y, c, ofm, max_len);
+
+ return 0;
+ }
+
+ if (storage == 1) {
+ ret = feat_matrix_check_location(edev, info, st, fm, type, x,
+ min3(y, (u32)fm->height[0],
+ (u32)fm->height[1]), c, ofm,
+ max_len);
+ if (ret)
+ return ret;
+ if (fm->height[0] < fm->height[1] && fm->height[1] <= y) {
+ ret = feat_matrix_check_location(edev, info, st, fm, type, x,
+ fm->height[1], c, ofm,
+ max_len);
+ if (ret)
+ return ret;
+ }
+ if (fm->height[1] < y) {
+ ret = feat_matrix_check_location(edev, info, st, fm, type, x,
+ fm->height[1] + 1, c, ofm,
+ max_len);
+ if (ret)
+ return ret;
+ }
+ }
+
+ return feat_matrix_check_location(edev, info, st, fm, type, x, y, c, ofm,
+ max_len);
+}
+
static int buffer_size(struct ethosu_validated_cmdstream_info *info,
struct cmd_state *st, struct buffer *buf, s8 region,
u16 region_cmd, u16 base_cmd, u16 length_cmd, bool optional)
@@ -474,6 +564,7 @@ static int calc_sizes(struct drm_device *ddev,
{
struct ethosu_device *edev = to_ethosu_device(ddev);
u64 len;
+ int ret;
if (ifm) {
if (!cmd_state_reg_is_set(st, NPU_SET_KERNEL_WIDTH_M1) ||
@@ -496,23 +587,22 @@ static int calc_sizes(struct drm_device *ddev,
if (ifm_height < 0 || ifm_width < 0)
return -EINVAL;
- len = feat_matrix_length(edev, info, st, &st->ifm,
- FEAT_MATRIX_IFM, ifm_width, ifm_height,
- st->ifm.depth, false);
+ ret = feat_matrix_size(edev, info, st, &st->ifm, FEAT_MATRIX_IFM,
+ ifm_width, ifm_height, st->ifm.depth, false,
+ &len);
dev_dbg(ddev->dev, "op %d: IFM:%d:0x%llx-0x%llx\n",
op, st->ifm.region, st->ifm.base[0], len);
- if (len == U64_MAX)
- return -EINVAL;
+ if (ret)
+ return ret;
}
if (ifm2) {
- len = feat_matrix_length(edev, info, st, &st->ifm2,
- FEAT_MATRIX_IFM2, st->ifm.depth, 0,
- st->ofm.depth, false);
+ ret = feat_matrix_size(edev, info, st, &st->ifm2, FEAT_MATRIX_IFM2,
+ st->ifm.depth, 0, st->ofm.depth, false, &len);
dev_dbg(ddev->dev, "op %d: IFM2:%d:0x%llx-0x%llx\n",
op, st->ifm2.region, st->ifm2.base[0], len);
- if (len == U64_MAX)
- return -EINVAL;
+ if (ret)
+ return ret;
}
if (weight) {
@@ -554,13 +644,13 @@ static int calc_sizes(struct drm_device *ddev,
return -EINVAL;
}
- len = feat_matrix_length(edev, info, st, &st->ofm, FEAT_MATRIX_OFM,
- st->ofm.width, st->ofm.height[2], st->ofm.depth,
- true);
+ ret = feat_matrix_size(edev, info, st, &st->ofm, FEAT_MATRIX_OFM,
+ st->ofm.width, st->ofm.height[2], st->ofm.depth,
+ true, &len);
dev_dbg(ddev->dev, "op %d: OFM:%d:0x%llx-0x%llx\n",
op, st->ofm.region, st->ofm.base[0], len);
- if (len == U64_MAX)
- return -EINVAL;
+ if (ret)
+ return ret;
if (!feat_matrix_chained(edev, &st->ofm))
info->output_region[st->ofm.region] = true;
@@ -575,18 +665,19 @@ static int calc_sizes_elemwise(struct drm_device *ddev,
struct ethosu_device *edev = to_ethosu_device(ddev);
u32 height, width, depth;
u64 len;
+ int ret;
if (ifm) {
height = st->ifm.broadcast & 0x1 ? 0 : st->ofm.height[2];
width = st->ifm.broadcast & 0x2 ? 0 : st->ofm.width;
depth = st->ifm.broadcast & 0x4 ? 0 : st->ofm.depth;
- len = feat_matrix_length(edev, info, st, &st->ifm,
- FEAT_MATRIX_IFM, width, height, depth, false);
+ ret = feat_matrix_size(edev, info, st, &st->ifm, FEAT_MATRIX_IFM,
+ width, height, depth, false, &len);
dev_dbg(ddev->dev, "op %d: IFM:%d:0x%llx-0x%llx\n",
op, st->ifm.region, st->ifm.base[0], len);
- if (len == U64_MAX)
- return -EINVAL;
+ if (ret)
+ return ret;
}
if (ifm2) {
@@ -594,21 +685,21 @@ static int calc_sizes_elemwise(struct drm_device *ddev,
width = st->ifm2.broadcast & 0x2 ? 0 : st->ofm.width;
depth = st->ifm2.broadcast & 0x4 ? 0 : st->ofm.depth;
- len = feat_matrix_length(edev, info, st, &st->ifm2,
- FEAT_MATRIX_IFM2, width, height, depth, false);
+ ret = feat_matrix_size(edev, info, st, &st->ifm2, FEAT_MATRIX_IFM2,
+ width, height, depth, false, &len);
dev_dbg(ddev->dev, "op %d: IFM2:%d:0x%llx-0x%llx\n",
op, st->ifm2.region, st->ifm2.base[0], len);
- if (len == U64_MAX)
- return -EINVAL;
+ if (ret)
+ return ret;
}
- len = feat_matrix_length(edev, info, st, &st->ofm, FEAT_MATRIX_OFM,
- st->ofm.width, st->ofm.height[2], st->ofm.depth,
- true);
+ ret = feat_matrix_size(edev, info, st, &st->ofm, FEAT_MATRIX_OFM,
+ st->ofm.width, st->ofm.height[2], st->ofm.depth,
+ true, &len);
dev_dbg(ddev->dev, "op %d: OFM:%d:0x%llx-0x%llx\n",
op, st->ofm.region, st->ofm.base[0], len);
- if (len == U64_MAX)
- return -EINVAL;
+ if (ret)
+ return ret;
if (!feat_matrix_chained(edev, &st->ofm))
info->output_region[st->ofm.region] = true;
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 09/19] accel: ethosu: Account for feature map element size
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (7 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 08/19] accel: ethosu: Validate all feature map tiles Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 10/19] accel: ethosu: Validate convolution parameter Rob Herring (Arm)
` (9 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
The feature map bounds calculation currently accounts for only one byte
of the final element. This can leave the tail of B16, B32, and B64
elements outside the validated buffer.
Use the feature map element size when calculating the final byte.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- No changes.
v2:
- new patch
---
drivers/accel/ethosu/ethosu_gem.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index 11f41382dc5d..b5df8e61c1b1 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -436,7 +436,7 @@ static u64 feat_matrix_length(struct ethosu_device *edev,
return U64_MAX;
}
- if (check_add_overflow(addr, 1ULL, &offset))
+ if (check_add_overflow(addr, (u64)element_size, &offset))
return U64_MAX;
info->region_size[fm->region] = max(info->region_size[fm->region], offset);
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 10/19] accel: ethosu: Validate convolution parameter
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (8 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 09/19] accel: ethosu: Account for feature map element size Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 11/19] accel: ethosu: Validate accumulator input Rob Herring (Arm)
` (8 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
The U65 NPU_OP_CONV command has no parameter fields, but the
validator interpreted bit zero as the U85 weights_ifm2 field. A crafted
U65 stream could consequently make validation skip the weight buffer
that hardware accesses.
Require a zero parameter on U65 and reject the reserved U85 parameter
bits.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- No changes.
v2:
- new patch
---
drivers/accel/ethosu/ethosu_device.h | 1 +
drivers/accel/ethosu/ethosu_gem.c | 5 ++++-
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/drivers/accel/ethosu/ethosu_device.h b/drivers/accel/ethosu/ethosu_device.h
index 1ea7638287f0..c1cae47ab0e2 100644
--- a/drivers/accel/ethosu/ethosu_device.h
+++ b/drivers/accel/ethosu/ethosu_device.h
@@ -87,6 +87,7 @@ struct gen_pool;
#define PMU_EV_TYPE_IDLE 0x20
#define NPU_DMA_REGION_INDEX_MODE BIT(11)
+#define NPU_OP_CONV_WEIGHTS_IFM2 BIT(0)
#define NPU_CMD_CTRL_CMD1 BIT(14)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index b5df8e61c1b1..512a853fd942 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -776,7 +776,10 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
st.dma.dst.region, st.dma.dst.offset, dstlen);
break;
case NPU_OP_CONV:
- use_ifm2 = param & 0x1; // weights_ifm2
+ if ((ethosu_is_u65(edev) && param) || (param & ~NPU_OP_CONV_WEIGHTS_IFM2))
+ return -EINVAL;
+
+ use_ifm2 = param & NPU_OP_CONV_WEIGHTS_IFM2;
if (!cmd_state_reg_is_set(&st, NPU_SET_OFM_PRECISION))
return -EINVAL;
use_scale = !(st.ofm.precision & 0x100);
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 11/19] accel: ethosu: Validate accumulator input
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (9 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 10/19] accel: ethosu: Validate convolution parameter Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 12/19] accel: ethosu: Restrict dynamic IFM2 weights Rob Herring (Arm)
` (7 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
The U85 ACC_FORMAT command can select IFM2 as the accumulator
input. This is used by null-pool operations and can also be used
by convolution. Track this selection and validate the IFM2 feature
map against the OFM extent before submitting the operation.
U85 accumulator restore reads 32 or 64 bits according to ACC_FORMAT,
regardless of the programmed IFM2 precision. Require IFM2 precision to
match that width before using it to calculate the input bounds.
Otherwise a stream can advertise a smaller element size and pass
validation with a buffer smaller than the hardware reads. Also reject
reserved accumulator formats whose read width is not modeled.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- Ensure ACC_FORMAT has been set when accumulator input is used
- Handle accumulator size mismatch with IFM2 size
v4:
- Accumulator input should not be checked for elementwise ops
v2:
- new patch
---
drivers/accel/ethosu/ethosu_device.h | 4 ++++
drivers/accel/ethosu/ethosu_gem.c | 45 ++++++++++++++++++++++++++++++++++++
2 files changed, 49 insertions(+)
diff --git a/drivers/accel/ethosu/ethosu_device.h b/drivers/accel/ethosu/ethosu_device.h
index c1cae47ab0e2..716c648c2dfd 100644
--- a/drivers/accel/ethosu/ethosu_device.h
+++ b/drivers/accel/ethosu/ethosu_device.h
@@ -123,6 +123,7 @@ enum ethosu_cmds {
NPU_SET_KERNEL_WIDTH_M1 = 0x120,
NPU_SET_KERNEL_HEIGHT_M1 = 0x121,
NPU_SET_KERNEL_STRIDE = 0x122,
+ NPU_SET_ACC_FORMAT = 0x124,
NPU_SET_WEIGHT_REGION = 0x128,
NPU_SET_SCALE_REGION = 0x129,
NPU_SET_DMA0_SRC_REGION = 0x130,
@@ -177,6 +178,9 @@ enum ethosu_cmds {
NPU_SET_WEIGHT3_LENGTH = 0x4095,
};
+#define NPU_ACC_FORMAT_INPUT_MASK GENMASK(5, 4)
+#define NPU_ACC_INPUT_IFM2 2
+
#define ETHOSU_SRAM_REGION 2 /* Matching Vela compiler */
struct ethosu_perfmon;
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index 512a853fd942..e3cd28c02e81 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -155,6 +155,7 @@ struct feat_matrix {
struct cmd_state {
DECLARE_BITMAP(cmd0, NPU_CMD0_REGS);
DECLARE_BITMAP(cmd1, NPU_CMD1_REGS);
+ u8 acc_input_precision;
struct dma_state dma;
struct buffer scale[2];
struct buffer weight[4];
@@ -534,6 +535,36 @@ static int feat_matrix_size(struct ethosu_device *edev,
max_len);
}
+static int
+calc_acc_input_size(struct drm_device *ddev,
+ struct ethosu_validated_cmdstream_info *info,
+ struct cmd_state *st)
+{
+ struct ethosu_device *edev = to_ethosu_device(ddev);
+ u64 len;
+ int ret;
+
+ if (!ethosu_is_u65(edev) &&
+ !cmd_state_reg_is_set(st, NPU_SET_ACC_FORMAT))
+ return -EINVAL;
+
+ if (!st->acc_input_precision)
+ return 0;
+
+ /* ACC_FORMAT selects the read width, not IFM2_PRECISION. */
+ if (((st->ifm2.precision >> 2) & 0x3) != st->acc_input_precision)
+ return -EINVAL;
+
+ /* The accumulator has one input value for each OFM element. */
+ ret = feat_matrix_size(edev, info, st, &st->ifm2,
+ FEAT_MATRIX_IFM2, st->ofm.width,
+ st->ofm.height[2], st->ofm.depth, false, &len);
+ dev_dbg(ddev->dev, "ACC IFM2:%d:0x%llx-0x%llx\n",
+ st->ifm2.region, st->ifm2.base[0], len);
+
+ return ret;
+}
+
static int buffer_size(struct ethosu_validated_cmdstream_info *info,
struct cmd_state *st, struct buffer *buf, s8 region,
u16 region_cmd, u16 base_cmd, u16 length_cmd, bool optional)
@@ -649,6 +680,9 @@ static int calc_sizes(struct drm_device *ddev,
true, &len);
dev_dbg(ddev->dev, "op %d: OFM:%d:0x%llx-0x%llx\n",
op, st->ofm.region, st->ofm.base[0], len);
+ if (ret)
+ return ret;
+ ret = calc_acc_input_size(ddev, info, st);
if (ret)
return ret;
if (!feat_matrix_chained(edev, &st->ofm))
@@ -851,6 +885,17 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
case NPU_SET_KERNEL_STRIDE:
st.ifm.stride_kernel = param;
break;
+ case NPU_SET_ACC_FORMAT:
+ if (!ethosu_is_u65(edev)) {
+ u32 acc_input = FIELD_GET(NPU_ACC_FORMAT_INPUT_MASK, param);
+
+ if (acc_input > NPU_ACC_INPUT_IFM2 || (param & 0x3) > 1)
+ return -EINVAL;
+ /* i32 and i48 require b32 and b64 IFM2, respectively. */
+ st.acc_input_precision = acc_input == NPU_ACC_INPUT_IFM2 ?
+ (param & 0x3) + 2 : 0;
+ }
+ break;
case NPU_SET_IFM_PAD_TOP:
st.ifm.pad_top = param & 0x7f;
break;
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 12/19] accel: ethosu: Restrict dynamic IFM2 weights
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (10 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 11/19] accel: ethosu: Validate accumulator input Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 13/19] accel: ethosu: Split U65 and U85 DMA length validation Rob Herring (Arm)
` (6 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
The validator bounds dynamic IFM2 weights as a 1x1 weight matrix, but
did not enforce the corresponding kernel shape. A larger crafted kernel
could make the NPU access beyond the validated IFM2 feature map.
Reject dynamic-weight convolutions whose kernel is not 1x1.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- No changes.
v2:
- new patch
---
drivers/accel/ethosu/ethosu_gem.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index e3cd28c02e81..1d2bc5d4a37a 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -606,6 +606,9 @@ static int calc_sizes(struct drm_device *ddev,
!cmd_state_reg_is_set(st, NPU_SET_IFM_PAD_RIGHT) ||
!cmd_state_reg_is_set(st, NPU_SET_IFM_PAD_BOTTOM))
return -EINVAL;
+ /* Dynamic IFM2 weights are only supported for 1x1 convolutions. */
+ if (ifm2 && (st->ifm.width || st->ifm.height[2]))
+ return -EINVAL;
u32 stride_y = ((st->ifm.stride_kernel >> 8) & 0x2) +
((st->ifm.stride_kernel >> 1) & 0x1) + 1;
u32 stride_x = ((st->ifm.stride_kernel >> 5) & 0x2) +
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 13/19] accel: ethosu: Split U65 and U85 DMA length validation
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (11 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 12/19] accel: ethosu: Restrict dynamic IFM2 weights Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 14/19] accel: ethosu: Validate OFM transpose Rob Herring (Arm)
` (5 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
Ethos-U65 and Ethos-U85 have slightly different DMA programming models.
The U65 has skip values added on to the size whereas U85 has signed
stride values. The U65 shares the skip values for source and destination
whereas the U85 has independent settings for source and destination.
The current validation only correctly handles U65 constraints and only
for source 2D/3D mode. Split the shared DMA length calculation into U65
and U85 specific versions adding U65 destination 2D/3D mode and U85
constraints.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- Handle U85 shared stride mode
- Fix U65 case with mixed contiguous buffer and strided buffer
v4:
- Only account for a skip/stride between transfers and not at the end of
last transfer
v3:
- Fix U65 destination stride/skip handling
v2:
- new patch
---
drivers/accel/ethosu/ethosu_gem.c | 131 +++++++++++++++++++++++++++++++-------
1 file changed, 107 insertions(+), 24 deletions(-)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index 1d2bc5d4a37a..0ab73b99c458 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -212,12 +212,18 @@ static bool dma_use_src_stride(struct ethosu_device *edev,
return ethosu_is_u65(edev) || dma == &dma_st->src;
}
+static s8 dma_mode(struct ethosu_device *edev,
+ const struct dma_state *dma_st, const struct dma *dma)
+{
+ return ethosu_is_u65(edev) ? dma->mode : dma_st->src.mode;
+}
+
static bool dma_params_valid(struct ethosu_device *edev, struct cmd_state *st,
const struct dma_state *dma_st,
const struct dma *dma,
u16 region_cmd, u16 addr_cmd)
{
- s8 mode = dma->mode;
+ s8 mode = dma_mode(edev, dma_st, dma);
if (!cmd_state_reg_is_set(st, region_cmd) ||
!cmd_state_reg_is_set(st, addr_cmd) ||
@@ -245,40 +251,101 @@ static bool dma_params_valid(struct ethosu_device *edev, struct cmd_state *st,
return true;
}
-static u64 dma_length(struct ethosu_device *edev,
- struct ethosu_validated_cmdstream_info *info,
- struct cmd_state *st, struct dma_state *dma_st,
- struct dma *dma, u16 region_cmd, u16 addr_cmd)
+static u64 dma_length_finish(struct ethosu_validated_cmdstream_info *info,
+ const struct dma *dma, u64 len)
{
- s8 mode = dma->mode;
- u64 len = dma->len;
+ if (dma->region >= 0) {
+ u64 end;
- if (!dma_params_valid(edev, st, dma_st, dma, region_cmd, addr_cmd))
- return U64_MAX;
+ if (check_add_overflow(len, dma->offset, &end))
+ return U64_MAX;
+ info->region_size[dma->region] =
+ max(info->region_size[dma->region], end);
+ }
+
+ return len;
+}
+
+static u64 dma_length_u65(struct ethosu_validated_cmdstream_info *info,
+ struct dma_state *dma_st,
+ struct dma *dma)
+{
+ s8 mode = max(dma_st->src.mode, dma_st->dst.mode);
+ u64 len = dma->len;
+ u64 stride;
if (mode >= 1) {
- if (dma->stride[0] < 0 && (u64)(-dma->stride[0]) > len)
- return U64_MAX;
- len += dma->stride[0];
if (check_mul_overflow(len, (u64)dma_st->size0, &len))
return U64_MAX;
+ if (dma->mode >= 1 &&
+ (check_mul_overflow((u64)dma->stride[0],
+ (u64)dma_st->size0 - 1, &stride) ||
+ check_add_overflow(len, stride, &len)))
+ return U64_MAX;
}
if (mode == 2) {
- if (dma->stride[1] < 0 && (u64)(-dma->stride[1]) > len)
- return U64_MAX;
- len += dma->stride[1];
if (check_mul_overflow(len, (u64)dma_st->size1, &len))
return U64_MAX;
+ if (dma->mode == 2 &&
+ (check_mul_overflow((u64)dma->stride[1],
+ (u64)dma_st->size1 - 1, &stride) ||
+ check_add_overflow(len, stride, &len)))
+ return U64_MAX;
}
- if (dma->region >= 0) {
- u64 end;
- if (check_add_overflow(len, dma->offset, &end))
+ return dma_length_finish(info, dma, len);
+}
+
+static u64 dma_length_u85(struct ethosu_validated_cmdstream_info *info,
+ struct dma_state *dma_st,
+ struct dma *dma)
+{
+ s8 mode = dma_st->src.mode;
+ s64 min = 0;
+ u64 max = dma->len;
+ s64 stride;
+
+ if (mode >= 1) {
+ if (check_mul_overflow(dma->stride[0],
+ (s64)dma_st->size0 - 1, &stride))
+ return U64_MAX;
+ if (stride < 0) {
+ if (check_add_overflow(min, stride, &min))
+ return U64_MAX;
+ } else if (check_add_overflow(max, (u64)stride, &max)) {
return U64_MAX;
- info->region_size[dma->region] = max(info->region_size[dma->region], end);
+ }
+ }
+ if (mode == 2) {
+ if (check_mul_overflow(dma->stride[1],
+ (s64)dma_st->size1 - 1, &stride))
+ return U64_MAX;
+ if (stride < 0) {
+ if (check_add_overflow(min, stride, &min))
+ return U64_MAX;
+ } else if (check_add_overflow(max, (u64)stride, &max)) {
+ return U64_MAX;
+ }
}
- return len;
+ if (min < 0 && -(u64)min > dma->offset)
+ return U64_MAX;
+
+ return dma_length_finish(info, dma, max);
+}
+
+static u64 dma_length(struct ethosu_device *edev,
+ struct ethosu_validated_cmdstream_info *info,
+ struct cmd_state *st, struct dma_state *dma_st,
+ struct dma *dma, u16 region_cmd, u16 addr_cmd)
+{
+ if (!dma_params_valid(edev, st, dma_st, dma, region_cmd, addr_cmd))
+ return U64_MAX;
+
+ if (ethosu_is_u65(edev))
+ return dma_length_u65(info, dma_st, dma);
+
+ return dma_length_u85(info, dma_st, dma);
}
static bool feat_matrix_chained(struct ethosu_device *edev, struct feat_matrix *fm)
@@ -1102,16 +1169,32 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
st.dma.size1 = param;
break;
case NPU_SET_DMA0_SRC_STRIDE0:
- st.dma.src.stride[0] = ((s64)addr << 24) >> 24;
+ if (ethosu_is_u65(edev)) {
+ st.dma.src.stride[0] = addr;
+ st.dma.dst.stride[0] = addr;
+ } else {
+ st.dma.src.stride[0] = sign_extend64(addr, 39);
+ }
break;
case NPU_SET_DMA0_SRC_STRIDE1:
- st.dma.src.stride[1] = ((s64)addr << 24) >> 24;
+ if (ethosu_is_u65(edev)) {
+ st.dma.src.stride[1] = addr;
+ st.dma.dst.stride[1] = addr;
+ } else {
+ st.dma.src.stride[1] = sign_extend64(addr, 39);
+ }
break;
case NPU_SET_DMA0_DST_STRIDE0:
- st.dma.dst.stride[0] = ((s64)addr << 24) >> 24;
+ if (!ethosu_is_u65(edev))
+ st.dma.dst.stride[0] = sign_extend64(addr, 39);
+ else
+ return -EINVAL;
break;
case NPU_SET_DMA0_DST_STRIDE1:
- st.dma.dst.stride[1] = ((s64)addr << 24) >> 24;
+ if (!ethosu_is_u65(edev))
+ st.dma.dst.stride[1] = sign_extend64(addr, 39);
+ else
+ return -EINVAL;
break;
case NPU_SET_DMA0_SRC:
st.dma.src.offset = addr;
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 14/19] accel: ethosu: Validate OFM transpose
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (12 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 13/19] accel: ethosu: Split U65 and U85 DMA length validation Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 15:07 ` sashiko-bot
2026-11-20 14:57 ` [PATCH v5 15/19] accel: ethosu: Validate resize operations Rob Herring (Arm)
` (4 subsequent siblings)
18 siblings, 1 reply; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
U85 OFM dimensions are specified before transposition, while
tile bases and strides address the transposed feature map. Permute
the output endpoint before validating its tile and stride accesses.
Allow the defined U85 transpose encodings and reject the two
reserved encodings.
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- No changes.
v4:
- Allow only HWC, HCW, and CHW output layouts for elementwise ops
- Don't allow 64-bit output precision and chained output storage
v2:
- no change
---
drivers/accel/ethosu/ethosu_device.h | 1 +
drivers/accel/ethosu/ethosu_gem.c | 69 ++++++++++++++++++++++++++++++++++--
2 files changed, 68 insertions(+), 2 deletions(-)
diff --git a/drivers/accel/ethosu/ethosu_device.h b/drivers/accel/ethosu/ethosu_device.h
index 716c648c2dfd..368daac25acc 100644
--- a/drivers/accel/ethosu/ethosu_device.h
+++ b/drivers/accel/ethosu/ethosu_device.h
@@ -88,6 +88,7 @@ struct gen_pool;
#define NPU_DMA_REGION_INDEX_MODE BIT(11)
#define NPU_OP_CONV_WEIGHTS_IFM2 BIT(0)
+#define NPU_OFM_TRANSPOSE_MASK GENMASK(13, 11)
#define NPU_CMD_CTRL_CMD1 BIT(14)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index 0ab73b99c458..b8f840ab1cee 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -432,6 +432,52 @@ static int feat_matrix_validate(struct ethosu_device *edev,
return 0;
}
+
+static int feat_matrix_permute(struct ethosu_device *edev,
+ struct feat_matrix *fm, u32 *x, u32 *y,
+ u32 *c, bool ofm)
+{
+ u32 width = *x;
+ u32 height = *y;
+ u32 depth = *c;
+ u32 transpose;
+
+ if (ethosu_is_u65(edev) || !ofm)
+ return 0;
+
+ transpose = FIELD_GET(NPU_OFM_TRANSPOSE_MASK, fm->precision);
+
+ switch (transpose) {
+ case 0: /* HWC */
+ break;
+ case 1: /* WHC */
+ *x = height;
+ *y = width;
+ break;
+ case 2: /* HCW */
+ *x = depth;
+ *c = width;
+ break;
+ case 3: /* WCH */
+ *x = depth;
+ *y = width;
+ *c = height;
+ break;
+ case 6: /* CHW */
+ *x = height;
+ *y = depth;
+ *c = width;
+ break;
+ case 7: /* CWH */
+ *y = depth;
+ *c = height;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
static u64 feat_matrix_length(struct ethosu_device *edev,
struct ethosu_validated_cmdstream_info *info,
struct cmd_state *st, struct feat_matrix *fm,
@@ -538,6 +584,9 @@ static int feat_matrix_size(struct ethosu_device *edev,
int ret;
*max_len = 0;
+ ret = feat_matrix_permute(edev, fm, &x, &y, &c, ofm);
+ if (ret)
+ return ret;
if (storage == 0) {
ret = feat_matrix_check_location(edev, info, st, fm, type, 0, 0,
@@ -927,6 +976,11 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
default:
return -EINVAL;
}
+ if (!ethosu_is_u65(edev) &&
+ FIELD_GET(NPU_OFM_TRANSPOSE_MASK, st.ofm.precision) != 0 &&
+ FIELD_GET(NPU_OFM_TRANSPOSE_MASK, st.ofm.precision) != 2 &&
+ FIELD_GET(NPU_OFM_TRANSPOSE_MASK, st.ofm.precision) != 6)
+ return -EINVAL;
if (!ethosu_is_u65(edev) &&
!cmd_state_reg_is_set(&st, NPU_SET_IFM_BROADCAST))
return -EINVAL;
@@ -1029,8 +1083,19 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
case NPU_SET_OFM_PRECISION:
if (((param >> 6) & 0x3) > 1)
return -EINVAL;
- if (!ethosu_is_u65(edev) && (param & GENMASK(13, 11)))
- return -EINVAL;
+ if (!ethosu_is_u65(edev)) {
+ u32 transpose = FIELD_GET(NPU_OFM_TRANSPOSE_MASK, param);
+
+ switch (transpose) {
+ case 4:
+ case 5:
+ return -EINVAL;
+ }
+ if (transpose && ((param >> 1) & 0x3) == 3)
+ return -EINVAL;
+ if ((param >> 14) == 2 && transpose)
+ return -EINVAL;
+ }
st.ofm.precision = param;
break;
case NPU_SET_OFM_REGION:
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v5 14/19] accel: ethosu: Validate OFM transpose
2026-11-20 14:57 ` [PATCH v5 14/19] accel: ethosu: Validate OFM transpose Rob Herring (Arm)
@ 2026-11-20 15:07 ` sashiko-bot
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-20 15:07 UTC (permalink / raw)
To: Rob Herring (Arm); +Cc: dri-devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Critical] accel: ethosu: missing NHCWB16 format check when OFM
transpose is set
--
commit 819bad25b9dbcbe54db35bf0c5afd5f5f47c3c99
Author: Rob Herring (Arm) <robh@kernel.org>
accel: ethosu: Validate OFM transpose
This commit adds validation for Ethos-U85 output feature map (OFM)
transpose encodings and permutes the OFM dimensions before validating
tile and stride accesses.
> diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
> index 0ab73b99c458c..b8f840ab1ceed 100644
> --- a/drivers/accel/ethosu/ethosu_gem.c
> +++ b/drivers/accel/ethosu/ethosu_gem.c
[ ... ]
> @@ -1029,8 +1083,19 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
> case NPU_SET_OFM_PRECISION:
> if (((param >> 6) & 0x3) > 1)
> return -EINVAL;
> - if (!ethosu_is_u65(edev) && (param & GENMASK(13, 11)))
> - return -EINVAL;
> + if (!ethosu_is_u65(edev)) {
> + u32 transpose = FIELD_GET(NPU_OFM_TRANSPOSE_MASK, param);
> +
> + switch (transpose) {
> + case 4:
> + case 5:
> + return -EINVAL;
> + }
> + if (transpose && ((param >> 1) & 0x3) == 3)
> + return -EINVAL;
> + if ((param >> 14) == 2 && transpose)
> + return -EINVAL;
> + }
> st.ofm.precision = param;
> break;
> case NPU_SET_OFM_REGION:
[Severity: Critical]
Should NPU_SET_OFM_PRECISION also reject a non-zero transpose when the
activation format is NHCWB16 (((param >> 6) & 0x3) == 1)?
OFM transposition on U85 is only valid with the linear NHWC format. If
NHCWB16 is combined with a non-zero transpose, feat_matrix_validate() only
requires NPU_SET_OFM_STRIDE_C to be set, and feat_matrix_length() computes
the NHCWB16 brick offset on the coordinates already permuted by
feat_matrix_permute() while ignoring fm->stride_x. That can underestimate
info->region_size[fm->region] and allow out-of-bounds NPU DMA writes past
the end of the GEM buffer.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-ethosu-fixes-v5-0-52d459acdb84@kernel.org?part=14
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v5 15/19] accel: ethosu: Validate resize operations
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (13 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 14/19] accel: ethosu: Validate OFM transpose Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 16/19] accel: ethosu: Ensure cmd stream is multiple of 4 bytes Rob Herring (Arm)
` (3 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
Resize input coordinates are controlled by the scale, offset, and step
registers. Require those values to be explicitly programmed and validate
their scale and step relationships before calculating feature-map bounds.
KERNEL_WIDTH_M1 and KERNEL_HEIGHT_M1 specify the input dimensions for
resize, not a convolution footprint to add to each sampling coordinate.
Compute the final sampling position using the signed resize offset,
check it against the input dimensions, and include the upper sample
only for interpolation modes that use it.
Pass zero-based maximum coordinates to feature-map validation, as for
other operations. Keep the block-step relationship based on the stored
M1 block dimensions, as required by the hardware.
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- Use signed resize offsets and the programmed input dimensions.
- Account for interpolation mode when including an upper sample.
- Avoid adding input dimensions to the sampling footprint or converting
the maximum coordinate to an element count.
- Do not validate an accumulator input for resize as the accumulator is
never used for resize.
v3:
- Fix width and height calculations to account for kernel size reported
by sashiko
v2:
- new patch
---
drivers/accel/ethosu/ethosu_device.h | 8 ++
drivers/accel/ethosu/ethosu_gem.c | 161 ++++++++++++++++++++++++++++++++++-
2 files changed, 168 insertions(+), 1 deletion(-)
diff --git a/drivers/accel/ethosu/ethosu_device.h b/drivers/accel/ethosu/ethosu_device.h
index 368daac25acc..6aa7f035f32c 100644
--- a/drivers/accel/ethosu/ethosu_device.h
+++ b/drivers/accel/ethosu/ethosu_device.h
@@ -117,6 +117,8 @@ enum ethosu_cmds {
NPU_SET_OFM_HEIGHT_M1 = 0x112,
NPU_SET_OFM_DEPTH_M1 = 0x113,
NPU_SET_OFM_PRECISION = 0x114,
+ NPU_SET_OFM_BLK_WIDTH_M1 = 0x115,
+ NPU_SET_OFM_BLK_HEIGHT_M1 = 0x116,
NPU_SET_OFM_WIDTH0_M1 = 0x11a,
NPU_SET_OFM_HEIGHT0_M1 = 0x11b,
NPU_SET_OFM_HEIGHT1_M1 = 0x11c,
@@ -127,6 +129,10 @@ enum ethosu_cmds {
NPU_SET_ACC_FORMAT = 0x124,
NPU_SET_WEIGHT_REGION = 0x128,
NPU_SET_SCALE_REGION = 0x129,
+ NPU_SET_RESIZE_X_SCALE_N_M1 = 0x12a,
+ NPU_SET_RESIZE_Y_SCALE_N_M1 = 0x12b,
+ NPU_SET_RESIZE_X_OFFSET = 0x12c,
+ NPU_SET_RESIZE_Y_OFFSET = 0x12d,
NPU_SET_DMA0_SRC_REGION = 0x130,
NPU_SET_DMA0_DST_REGION = 0x131,
NPU_SET_DMA0_SIZE0 = 0x132,
@@ -177,6 +183,8 @@ enum ethosu_cmds {
NPU_SET_WEIGHT2_LENGTH = 0x4093,
NPU_SET_WEIGHT3_BASE = 0x4094,
NPU_SET_WEIGHT3_LENGTH = 0x4095,
+ NPU_SET_RESIZE_X = 0x4096,
+ NPU_SET_RESIZE_Y = 0x4097,
};
#define NPU_ACC_FORMAT_INPUT_MASK GENMASK(5, 4)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index b8f840ab1cee..ff89cfdb4772 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -149,6 +149,15 @@ struct feat_matrix {
u8 pad_right;
};
+struct resize_axis {
+ u16 scale_n;
+ s16 offset;
+ u16 one_step_int;
+ u16 one_step_mod;
+ u16 blk_step_int;
+ u16 blk_step_mod;
+};
+
#define NPU_CMD0_REGS 0x200
#define NPU_CMD1_REGS 0x100
@@ -162,6 +171,9 @@ struct cmd_state {
struct feat_matrix ofm;
struct feat_matrix ifm;
struct feat_matrix ifm2;
+ u16 ofm_blk_width;
+ u16 ofm_blk_height;
+ struct resize_axis resize[2];
};
static void cmd_state_init(struct cmd_state *st)
@@ -681,6 +693,98 @@ calc_acc_input_size(struct drm_device *ddev,
return ret;
}
+static int resize_axis_size(struct cmd_state *st, int axis, u16 ofm_size,
+ u16 ofm_blk_size, u16 ifm_size, u16 mode, u32 *size)
+{
+ struct resize_axis *resize = &st->resize[axis];
+ u64 one_step, blk_step, coord;
+ s64 position;
+ u32 remainder;
+
+ if (resize->offset < -(s16)resize->scale_n ||
+ resize->offset >= resize->scale_n ||
+ resize->one_step_mod >= resize->scale_n ||
+ resize->blk_step_mod >= resize->scale_n)
+ return -EINVAL;
+
+ one_step = resize->one_step_int * resize->scale_n +
+ resize->one_step_mod;
+ blk_step = resize->blk_step_int * resize->scale_n +
+ resize->blk_step_mod;
+ if (check_mul_overflow((u64)ofm_blk_size, one_step, &coord) ||
+ blk_step != coord)
+ return -EINVAL;
+
+ if (check_mul_overflow((s64)ofm_size, (s64)one_step, &position) ||
+ check_add_overflow(position, (s64)resize->offset, &position))
+ return -EINVAL;
+
+ /* Sampling positions are clamped to the programmed IFM dimensions. */
+ coord = div_u64_rem(max_t(s64, position, 0), resize->scale_n,
+ &remainder);
+ if (coord > ifm_size)
+ return -EINVAL;
+
+ if ((mode == 0 && position >= 0) ||
+ (mode == 2 && 2 * remainder >= resize->scale_n))
+ coord = min_t(u64, coord + 1, ifm_size);
+
+ *size = coord;
+ return 0;
+}
+
+
+static int calc_sizes_resize(struct drm_device *ddev,
+ struct ethosu_validated_cmdstream_info *info,
+ struct cmd_state *st, u16 mode)
+{
+ struct ethosu_device *edev = to_ethosu_device(ddev);
+ u32 ifm_width, ifm_height;
+ u64 len;
+ int ret;
+
+ if (!cmd_state_reg_is_set(st, NPU_SET_KERNEL_WIDTH_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_KERNEL_HEIGHT_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_OFM_BLK_WIDTH_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_OFM_BLK_HEIGHT_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_RESIZE_X_SCALE_N_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_RESIZE_Y_SCALE_N_M1) ||
+ !cmd_state_reg_is_set(st, NPU_SET_RESIZE_X_OFFSET) ||
+ !cmd_state_reg_is_set(st, NPU_SET_RESIZE_Y_OFFSET) ||
+ !cmd_state_reg_is_set(st, NPU_SET_RESIZE_X) ||
+ !cmd_state_reg_is_set(st, NPU_SET_RESIZE_Y))
+ return -EINVAL;
+
+ ret = resize_axis_size(st, 0, st->ofm.width, st->ofm_blk_width,
+ st->ifm.width, mode, &ifm_width);
+ if (ret)
+ return ret;
+ ret = resize_axis_size(st, 1, st->ofm.height[2], st->ofm_blk_height,
+ st->ifm.height[2], mode, &ifm_height);
+ if (ret)
+ return ret;
+
+ ret = feat_matrix_size(edev, info, st, &st->ifm, FEAT_MATRIX_IFM,
+ ifm_width, ifm_height, st->ifm.depth, false, &len);
+ dev_dbg(ddev->dev, "op %d: IFM:%d:0x%llx-0x%llx\n", NPU_OP_RESIZE,
+ st->ifm.region, st->ifm.base[0], len);
+ if (ret)
+ return ret;
+
+ ret = feat_matrix_size(edev, info, st, &st->ofm, FEAT_MATRIX_OFM,
+ st->ofm.width, st->ofm.height[2], st->ofm.depth,
+ true, &len);
+ dev_dbg(ddev->dev, "op %d: OFM:%d:0x%llx-0x%llx\n", NPU_OP_RESIZE,
+ st->ofm.region, st->ofm.base[0], len);
+ if (ret)
+ return ret;
+
+ if (!feat_matrix_chained(edev, &st->ofm))
+ info->output_region[st->ofm.region] = true;
+
+ return 0;
+}
+
static int buffer_size(struct ethosu_validated_cmdstream_info *info,
struct cmd_state *st, struct buffer *buf, s8 region,
u16 region_cmd, u16 base_cmd, u16 length_cmd, bool optional)
@@ -999,7 +1103,12 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
return ret;
break;
case NPU_OP_RESIZE: // U85 only
- return -EINVAL;
+ if (ethosu_is_u65(edev) || param > 2)
+ return -EINVAL;
+ ret = calc_sizes_resize(ddev, info, &st, param);
+ if (ret)
+ return ret;
+ break;
case NPU_SET_KERNEL_WIDTH_M1:
st.ifm.width = param;
break;
@@ -1098,6 +1207,12 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
}
st.ofm.precision = param;
break;
+ case NPU_SET_OFM_BLK_WIDTH_M1:
+ st.ofm_blk_width = param & 0x7f;
+ break;
+ case NPU_SET_OFM_BLK_HEIGHT_M1:
+ st.ofm_blk_height = param & 0x7f;
+ break;
case NPU_SET_OFM_REGION:
st.ofm.region = param & 0x7;
break;
@@ -1168,6 +1283,34 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
case NPU_SET_SCALE_REGION:
st.scale[0].region = param & 0x7;
break;
+ case NPU_SET_RESIZE_X_SCALE_N_M1:
+ if (ethosu_is_u65(edev))
+ break;
+ if (param & GENMASK(15, 11))
+ return -EINVAL;
+ st.resize[0].scale_n = param + 1;
+ break;
+ case NPU_SET_RESIZE_Y_SCALE_N_M1:
+ if (ethosu_is_u65(edev))
+ break;
+ if (param & GENMASK(15, 11))
+ return -EINVAL;
+ st.resize[1].scale_n = param + 1;
+ break;
+ case NPU_SET_RESIZE_X_OFFSET:
+ if (ethosu_is_u65(edev))
+ break;
+ if (param & GENMASK(15, 12))
+ return -EINVAL;
+ st.resize[0].offset = sign_extend32(param, 11);
+ break;
+ case NPU_SET_RESIZE_Y_OFFSET:
+ if (ethosu_is_u65(edev))
+ break;
+ if (param & GENMASK(15, 12))
+ return -EINVAL;
+ st.resize[1].offset = sign_extend32(param, 11);
+ break;
case NPU_SET_WEIGHT_BASE:
st.weight[0].base = addr;
break;
@@ -1204,6 +1347,22 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
case NPU_SET_WEIGHT3_LENGTH:
st.weight[3].length = cmds[1];
break;
+ case NPU_SET_RESIZE_X:
+ case NPU_SET_RESIZE_Y:
+ if (ethosu_is_u65(edev))
+ break;
+ if ((cmds[0] & BIT(31)) ||
+ (cmds[1] & (GENMASK(31, 27) | GENMASK(15, 11))))
+ return -EINVAL;
+ st.resize[cmd - NPU_SET_RESIZE_X].one_step_int =
+ FIELD_GET(GENMASK(19, 16), cmds[0]);
+ st.resize[cmd - NPU_SET_RESIZE_X].blk_step_int =
+ FIELD_GET(GENMASK(30, 20), cmds[0]);
+ st.resize[cmd - NPU_SET_RESIZE_X].one_step_mod =
+ FIELD_GET(GENMASK(10, 0), cmds[1]);
+ st.resize[cmd - NPU_SET_RESIZE_X].blk_step_mod =
+ FIELD_GET(GENMASK(26, 16), cmds[1]);
+ break;
case NPU_SET_DMA0_SRC_REGION:
if (param & NPU_DMA_REGION_INDEX_MODE)
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 16/19] accel: ethosu: Ensure cmd stream is multiple of 4 bytes
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (14 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 15/19] accel: ethosu: Validate resize operations Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 17/19] accel: ethosu: Require active weight streams setup Rob Herring (Arm)
` (2 subsequent siblings)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
The command-stream validator consumes 32-bit words, so reject empty or
unaligned streams at that boundary. This also prevents QSIZE from
receiving an unvalidated trailing byte.
Check that the second word of a CMD1 command is within the declared
stream before reading it from userspace. A word-aligned stream can still
end with the first word of a two-word command. The old ordering read
beyond the supplied size and could return -EFAULT instead of rejecting
the truncated command with -EINVAL.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- Also check length before get_user() access
v4:
- New patch
---
drivers/accel/ethosu/ethosu_gem.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index ff89cfdb4772..fc31372a2c08 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -977,6 +977,8 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
if (!info)
return -ENOMEM;
+ if (!size || size & 3)
+ return -EINVAL;
info->cmd_size = size;
cmd_state_init(&st);
@@ -997,12 +999,11 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
param = cmds[0] >> 16;
if (cmd & NPU_CMD_CTRL_CMD1) {
+ if (++i >= size / 4)
+ return -EINVAL;
if (get_user(cmds[1], ucmds++))
return -EFAULT;
- i++;
- if (i >= size / 4)
- return -EINVAL;
bocmds[i] = cmds[1];
addr = cmd_to_addr(cmds);
}
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 17/19] accel: ethosu: Require active weight streams setup
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (15 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 16/19] accel: ethosu: Ensure cmd stream is multiple of 4 bytes Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 18/19] accel: ethosu: Support IFM upscaling Rob Herring (Arm)
2026-11-20 14:57 ` [PATCH v5 19/19] accel: ethosu: Reject IFM broadcast on U65 Rob Herring (Arm)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
An active weight or scale stream must be explicitly disabled or configured.
Otherwise retained stream state can bypass buffer bounds validation.
Derive the active stream count from the NPU configuration on U85 and
from PARALLEL_MODE on U65. Require each active stream's length command,
while allowing a zero length to disable it.
Require U85 weight operations to set WEIGHT_FORMAT explicitly so stream
validation does not depend on format state retained from another job.
An inactive secondary stream can retain its length and base after a mode
change. Skip it regardless of that retained setup: checking stale bounds
against a newly selected region can reject a valid job even though the
operation never accesses that stream.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- Require explicit U85 weight format setup before weight operations.
- Skip inactive streams even when they retain old base/length setup.
- Handle U85 fast weights mode
v4:
- New patch
---
drivers/accel/ethosu/ethosu_device.h | 5 +++
drivers/accel/ethosu/ethosu_gem.c | 72 ++++++++++++++++++++++++++++++++----
2 files changed, 70 insertions(+), 7 deletions(-)
diff --git a/drivers/accel/ethosu/ethosu_device.h b/drivers/accel/ethosu/ethosu_device.h
index 6aa7f035f32c..f8066a9cf610 100644
--- a/drivers/accel/ethosu/ethosu_device.h
+++ b/drivers/accel/ethosu/ethosu_device.h
@@ -61,6 +61,7 @@ struct gen_pool;
#define CONFIG_MACS_PER_CC_MASK GENMASK(3, 0)
#define CONFIG_CMD_STREAM_VER_MASK GENMASK(7, 4)
+#define CONFIG_NUM_WD_MASK GENMASK(13, 12)
#define STATUS_STATE_RUNNING BIT(0)
#define STATUS_IRQ_RAISED BIT(1)
@@ -126,6 +127,7 @@ enum ethosu_cmds {
NPU_SET_KERNEL_WIDTH_M1 = 0x120,
NPU_SET_KERNEL_HEIGHT_M1 = 0x121,
NPU_SET_KERNEL_STRIDE = 0x122,
+ NPU_SET_PARALLEL_MODE = 0x123, // U65 only
NPU_SET_ACC_FORMAT = 0x124,
NPU_SET_WEIGHT_REGION = 0x128,
NPU_SET_SCALE_REGION = 0x129,
@@ -133,6 +135,7 @@ enum ethosu_cmds {
NPU_SET_RESIZE_Y_SCALE_N_M1 = 0x12b,
NPU_SET_RESIZE_X_OFFSET = 0x12c,
NPU_SET_RESIZE_Y_OFFSET = 0x12d,
+ NPU_SET_WEIGHT_FORMAT = 0x12e, // U85 only
NPU_SET_DMA0_SRC_REGION = 0x130,
NPU_SET_DMA0_DST_REGION = 0x131,
NPU_SET_DMA0_SIZE0 = 0x132,
@@ -189,6 +192,8 @@ enum ethosu_cmds {
#define NPU_ACC_FORMAT_INPUT_MASK GENMASK(5, 4)
#define NPU_ACC_INPUT_IFM2 2
+#define NPU_MACS_PER_CC_256 8
+#define NPU_PARALLEL_MODE_DUAL_CORE_DEPTH 1
#define ETHOSU_SRAM_REGION 2 /* Matching Vela compiler */
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index fc31372a2c08..d2884d9922ae 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -165,6 +165,8 @@ struct cmd_state {
DECLARE_BITMAP(cmd0, NPU_CMD0_REGS);
DECLARE_BITMAP(cmd1, NPU_CMD1_REGS);
u8 acc_input_precision;
+ bool fast_weights;
+ u8 parallel_mode;
struct dma_state dma;
struct buffer scale[2];
struct buffer weight[4];
@@ -785,19 +787,47 @@ static int calc_sizes_resize(struct drm_device *ddev,
return 0;
}
+static int weight_streams(struct ethosu_device *edev, struct cmd_state *st,
+ u8 *streams)
+{
+ if (ethosu_is_u65(edev)) {
+ if (FIELD_GET(CONFIG_MACS_PER_CC_MASK, edev->npu_info.config) ==
+ NPU_MACS_PER_CC_256) {
+ *streams = 1;
+ return 0;
+ }
+
+ if (!cmd_state_reg_is_set(st, NPU_SET_PARALLEL_MODE))
+ return -EINVAL;
+
+ *streams = st->parallel_mode + 1;
+ } else if (st->fast_weights) {
+ *streams = 1;
+ } else {
+ *streams = 1 << FIELD_GET(CONFIG_NUM_WD_MASK,
+ edev->npu_info.config);
+ }
+
+ return 0;
+}
+
static int buffer_size(struct ethosu_validated_cmdstream_info *info,
struct cmd_state *st, struct buffer *buf, s8 region,
- u16 region_cmd, u16 base_cmd, u16 length_cmd, bool optional)
+ u16 region_cmd, u16 base_cmd, u16 length_cmd, bool inactive)
{
u64 end;
bool base_set = cmd_state_reg_is_set(st, base_cmd);
bool length_set = cmd_state_reg_is_set(st, length_cmd);
- if (optional && !base_set && !length_set)
+ if (inactive)
return 0;
if (region < 0 || !cmd_state_reg_is_set(st, region_cmd) ||
- !base_set || !length_set)
+ !length_set)
+ return -EINVAL;
+ if (!buf->length)
+ return 0;
+ if (!base_set)
return -EINVAL;
if (check_add_overflow(buf->base, (u64)buf->length, &end))
@@ -815,6 +845,7 @@ static int calc_sizes(struct drm_device *ddev,
{
struct ethosu_device *edev = to_ethosu_device(ddev);
u64 len;
+ u8 streams;
int ret;
if (ifm) {
@@ -860,6 +891,15 @@ static int calc_sizes(struct drm_device *ddev,
}
if (weight) {
+ /* The weight format can persist from a previous job. */
+ if (!ethosu_is_u65(edev) &&
+ !cmd_state_reg_is_set(st, NPU_SET_WEIGHT_FORMAT))
+ return -EINVAL;
+
+ ret = weight_streams(edev, st, &streams);
+ if (ret)
+ return ret;
+
dev_dbg(ddev->dev, "op %d: W:%d:0x%llx-0x%llx\n",
op, st->weight[0].region, st->weight[0].base,
st->weight[0].base + st->weight[0].length - 1);
@@ -870,19 +910,23 @@ static int calc_sizes(struct drm_device *ddev,
if (buffer_size(info, st, &st->weight[1], st->weight[0].region,
NPU_SET_WEIGHT_REGION, NPU_SET_WEIGHT1_BASE,
- NPU_SET_WEIGHT1_LENGTH, true) ||
+ NPU_SET_WEIGHT1_LENGTH, streams < 2) ||
buffer_size(info, st, &st->weight[3], st->weight[0].region,
NPU_SET_WEIGHT_REGION, NPU_SET_WEIGHT3_BASE,
- NPU_SET_WEIGHT3_LENGTH, true))
+ NPU_SET_WEIGHT3_LENGTH, streams < 4))
return -EINVAL;
if (!ethosu_is_u65(edev) &&
buffer_size(info, st, &st->weight[2], st->weight[0].region,
NPU_SET_WEIGHT_REGION, NPU_SET_WEIGHT2_BASE,
- NPU_SET_WEIGHT2_LENGTH, true))
+ NPU_SET_WEIGHT2_LENGTH, streams < 3))
return -EINVAL;
}
if (scale) {
+ ret = weight_streams(edev, st, &streams);
+ if (ret)
+ return ret;
+
dev_dbg(ddev->dev, "op %d: S:%d:0x%llx-0x%llx\n",
op, st->scale[0].region, st->scale[0].base,
st->scale[0].base + st->scale[0].length - 1);
@@ -894,7 +938,7 @@ static int calc_sizes(struct drm_device *ddev,
if (ethosu_is_u65(edev) &&
buffer_size(info, st, &st->scale[1], st->scale[0].region,
NPU_SET_SCALE_REGION, NPU_SET_SCALE1_BASE,
- NPU_SET_SCALE1_LENGTH, true))
+ NPU_SET_SCALE1_LENGTH, streams < 2))
return -EINVAL;
}
@@ -1119,6 +1163,16 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
case NPU_SET_KERNEL_STRIDE:
st.ifm.stride_kernel = param;
break;
+ case NPU_SET_PARALLEL_MODE:
+ if (!ethosu_is_u65(edev) ||
+ param > NPU_PARALLEL_MODE_DUAL_CORE_DEPTH)
+ return -EINVAL;
+ if (FIELD_GET(CONFIG_MACS_PER_CC_MASK, edev->npu_info.config) ==
+ NPU_MACS_PER_CC_256 &&
+ param)
+ return -EINVAL;
+ st.parallel_mode = param;
+ break;
case NPU_SET_ACC_FORMAT:
if (!ethosu_is_u65(edev)) {
u32 acc_input = FIELD_GET(NPU_ACC_FORMAT_INPUT_MASK, param);
@@ -1130,6 +1184,10 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
(param & 0x3) + 2 : 0;
}
break;
+ case NPU_SET_WEIGHT_FORMAT:
+ if (!ethosu_is_u65(edev))
+ st.fast_weights = param & BIT(0);
+ break;
case NPU_SET_IFM_PAD_TOP:
st.ifm.pad_top = param & 0x7f;
break;
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [PATCH v5 18/19] accel: ethosu: Support IFM upscaling
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (16 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 17/19] accel: ethosu: Require active weight streams setup Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
2026-11-20 15:12 ` sashiko-bot
2026-11-20 14:57 ` [PATCH v5 19/19] accel: ethosu: Reject IFM broadcast on U65 Rob Herring (Arm)
18 siblings, 1 reply; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
Track the IFM upscaling mode while validating command streams and
adjust feature-map bounds for its 2x resampling modes.
Resize operations require no IFM upscaling, so reject a non-default
mode there. Ethos-U only defines upscaled IFM reads with unit kernel
strides and non-chained storage. Reject incompatible state when the
operation actually reads IFM, including elementwise operations, rather
than checking when registers are programmed.
Also enforce the pooling-mode restrictions: max/min cannot insert zeros,
and reduce_sum and argmax do not support IFM upscaling. The coordinate
reduction used for bounds checking must not be applied to these
undefined hardware configurations.
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- Reject various invalid modes
- IFM upscaling is not U85 specific, but supported on U65 too
v4:
- New patch
---
drivers/accel/ethosu/ethosu_device.h | 7 ++++++
drivers/accel/ethosu/ethosu_gem.c | 46 ++++++++++++++++++++++++++++++++++++
2 files changed, 53 insertions(+)
diff --git a/drivers/accel/ethosu/ethosu_device.h b/drivers/accel/ethosu/ethosu_device.h
index f8066a9cf610..40b7f28ae61d 100644
--- a/drivers/accel/ethosu/ethosu_device.h
+++ b/drivers/accel/ethosu/ethosu_device.h
@@ -93,6 +93,12 @@ struct gen_pool;
#define NPU_CMD_CTRL_CMD1 BIT(14)
+enum ethosu_ifm_upscale_mode {
+ NPU_IFM_UPSCALE_NONE,
+ NPU_IFM_UPSCALE_NEAREST,
+ NPU_IFM_UPSCALE_ZEROS,
+};
+
enum ethosu_cmds {
NPU_OP_STOP = 0x0,
NPU_OP_CONV = 0x2,
@@ -109,6 +115,7 @@ enum ethosu_cmds {
NPU_SET_IFM_PAD_BOTTOM = 0x103,
NPU_SET_IFM_DEPTH_M1 = 0x104,
NPU_SET_IFM_PRECISION = 0x105,
+ NPU_SET_IFM_UPSCALE = 0x107,
NPU_SET_IFM_BROADCAST = 0x108,
NPU_SET_IFM_WIDTH0_M1 = 0x10a,
NPU_SET_IFM_HEIGHT0_M1 = 0x10b,
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index d2884d9922ae..b4544298742f 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -167,6 +167,7 @@ struct cmd_state {
u8 acc_input_precision;
bool fast_weights;
u8 parallel_mode;
+ u8 ifm_upscale;
struct dma_state dma;
struct buffer scale[2];
struct buffer weight[4];
@@ -402,6 +403,11 @@ static int feat_matrix_validate(struct ethosu_device *edev,
!cmd_state_reg_is_set(st, NPU_SET_IFM_PRECISION) ||
!cmd_state_reg_is_set(st, NPU_SET_IFM_DEPTH_M1))
return -EINVAL;
+ if (st->ifm_upscale &&
+ (!cmd_state_reg_is_set(st, NPU_SET_KERNEL_STRIDE) ||
+ feat_matrix_chained(edev, fm) ||
+ (st->ifm.stride_kernel & (BIT(0) | BIT(1) | BIT(6) | BIT(9)))))
+ return -EINVAL;
if (feat_matrix_chained(edev, fm))
return 0;
if (!cmd_state_reg_is_set(st, NPU_SET_IFM_WIDTH0_M1) ||
@@ -745,6 +751,11 @@ static int calc_sizes_resize(struct drm_device *ddev,
u64 len;
int ret;
+ if (st->ifm_upscale != NPU_IFM_UPSCALE_NONE ||
+ feat_matrix_chained(edev, &st->ofm) ||
+ FIELD_GET(NPU_OFM_TRANSPOSE_MASK, st->ofm.precision))
+ return -EINVAL;
+
if (!cmd_state_reg_is_set(st, NPU_SET_KERNEL_WIDTH_M1) ||
!cmd_state_reg_is_set(st, NPU_SET_KERNEL_HEIGHT_M1) ||
!cmd_state_reg_is_set(st, NPU_SET_OFM_BLK_WIDTH_M1) ||
@@ -811,6 +822,19 @@ static int weight_streams(struct ethosu_device *edev, struct cmd_state *st,
return 0;
}
+static u32 ifm_upscale_coord(u32 coord, u8 mode)
+{
+ switch (mode) {
+ case NPU_IFM_UPSCALE_NONE:
+ return coord;
+ case NPU_IFM_UPSCALE_NEAREST:
+ case NPU_IFM_UPSCALE_ZEROS:
+ return coord / 2;
+ }
+
+ return 0;
+}
+
static int buffer_size(struct ethosu_validated_cmdstream_info *info,
struct cmd_state *st, struct buffer *buf, s8 region,
u16 region_cmd, u16 base_cmd, u16 length_cmd, bool inactive)
@@ -871,6 +895,8 @@ static int calc_sizes(struct drm_device *ddev,
if (ifm_height < 0 || ifm_width < 0)
return -EINVAL;
+ ifm_height = ifm_upscale_coord(ifm_height, st->ifm_upscale);
+ ifm_width = ifm_upscale_coord(ifm_width, st->ifm_upscale);
ret = feat_matrix_size(edev, info, st, &st->ifm, FEAT_MATRIX_IFM,
ifm_width, ifm_height, st->ifm.depth, false,
@@ -972,6 +998,8 @@ static int calc_sizes_elemwise(struct drm_device *ddev,
height = st->ifm.broadcast & 0x1 ? 0 : st->ofm.height[2];
width = st->ifm.broadcast & 0x2 ? 0 : st->ofm.width;
depth = st->ifm.broadcast & 0x4 ? 0 : st->ofm.depth;
+ height = ifm_upscale_coord(height, st->ifm_upscale);
+ width = ifm_upscale_coord(width, st->ifm_upscale);
ret = feat_matrix_size(edev, info, st, &st->ifm, FEAT_MATRIX_IFM,
width, height, depth, false, &len);
@@ -1102,6 +1130,19 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
case NPU_OP_POOL:
if (param > (ethosu_is_u65(edev) ? 2 : 7))
return -EINVAL;
+ if (st.ifm_upscale) {
+ switch (param) {
+ case 0: /* max */
+ case 5: /* min */
+ if (st.ifm_upscale == NPU_IFM_UPSCALE_ZEROS)
+ return -EINVAL;
+ break;
+ case 2: /* reduce_sum */
+ case 6: /* argmax_x */
+ case 7: /* argmax_y */
+ return -EINVAL;
+ }
+ }
use_ifm = param != 0x4; // pooling mode
if (!cmd_state_reg_is_set(&st, NPU_SET_OFM_PRECISION))
return -EINVAL;
@@ -1208,6 +1249,11 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
return -EINVAL;
st.ifm.precision = param;
break;
+ case NPU_SET_IFM_UPSCALE:
+ if (param > NPU_IFM_UPSCALE_ZEROS)
+ return -EINVAL;
+ st.ifm_upscale = param;
+ break;
case NPU_SET_IFM_BROADCAST:
st.ifm.broadcast = param;
break;
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [PATCH v5 18/19] accel: ethosu: Support IFM upscaling
2026-11-20 14:57 ` [PATCH v5 18/19] accel: ethosu: Support IFM upscaling Rob Herring (Arm)
@ 2026-11-20 15:12 ` sashiko-bot
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-20 15:12 UTC (permalink / raw)
To: Rob Herring (Arm); +Cc: dri-devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] accel: ethosu: missing NPU_SET_IFM_UPSCALE check in
feat_matrix_validate()
--
commit a2e33d72a9a5c1a79fd1f24546e3bdc167a7ece4
Author: Rob Herring (Arm) <robh@kernel.org>
accel: ethosu: Support IFM upscaling
This commit tracks NPU_SET_IFM_UPSCALE during command stream validation,
adjusts IFM bounds for 2x resampling modes, and rejects incompatible
stride, chained storage, resize, and pooling configurations.
> diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
> index d2884d9922aea..b4544298742f1 100644
> --- a/drivers/accel/ethosu/ethosu_gem.c
> +++ b/drivers/accel/ethosu/ethosu_gem.c
[ ... ]
> @@ -402,6 +403,11 @@ static int feat_matrix_validate(struct ethosu_device *edev,
> !cmd_state_reg_is_set(st, NPU_SET_IFM_PRECISION) ||
> !cmd_state_reg_is_set(st, NPU_SET_IFM_DEPTH_M1))
> return -EINVAL;
> + if (st->ifm_upscale &&
> + (!cmd_state_reg_is_set(st, NPU_SET_KERNEL_STRIDE) ||
> + feat_matrix_chained(edev, fm) ||
> + (st->ifm.stride_kernel & (BIT(0) | BIT(1) | BIT(6) | BIT(9)))))
> + return -EINVAL;
> if (feat_matrix_chained(edev, fm))
> return 0;
> if (!cmd_state_reg_is_set(st, NPU_SET_IFM_WIDTH0_M1) ||
[Severity: High]
Should feat_matrix_validate() require cmd_state_reg_is_set(st,
NPU_SET_IFM_UPSCALE) for FEAT_MATRIX_IFM?
The NPU is not reset between consecutive jobs, so NPU_SET_IFM_UPSCALE
persists in hardware from a previous job while cmd_state_init()
zero-initializes st->ifm_upscale to NPU_IFM_UPSCALE_NONE. If a second job
omits NPU_SET_IFM_UPSCALE, it runs with IFM upscaling enabled in hardware
while validation checks st->ifm_upscale == NPU_IFM_UPSCALE_NONE, bypassing
the chained-IFM and kernel-stride checks here as well as the upscaling
restrictions in calc_sizes_resize() and NPU_OP_POOL.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-ethosu-fixes-v5-0-52d459acdb84@kernel.org?part=18
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v5 19/19] accel: ethosu: Reject IFM broadcast on U65
2026-11-20 14:57 [PATCH v5 00/19] accel: ethosu: Cmd stream validation fixes Rob Herring (Arm)
` (17 preceding siblings ...)
2026-11-20 14:57 ` [PATCH v5 18/19] accel: ethosu: Support IFM upscaling Rob Herring (Arm)
@ 2026-11-20 14:57 ` Rob Herring (Arm)
18 siblings, 0 replies; 22+ messages in thread
From: Rob Herring (Arm) @ 2026-11-20 14:57 UTC (permalink / raw)
To: Tomeu Vizoso, Oded Gabbay, Frank Li, Thomas Zimmermann
Cc: dri-devel, linux-kernel
IFM broadcasting is only supported by U85. Recording IFM_BROADCAST on
U65 lets a command stream reduce the dimensions used for IFM bounds
validation, or skip IFM validation entirely with scalar broadcasting.
The hardware does not implement that broadcast configuration.
Reject NPU_SET_IFM_BROADCAST on U65 rather than using an unsupported
register command to determine the memory accessed by an operation.
Fixes: 5a5e9c0228e6 ("accel: Add Arm Ethos-U NPU driver")
Closes: https://sashiko.dev/#/bug/linux-c74c700c-c823-4b8d-8c90-14842935cdf0
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
v5:
- New patch.
---
drivers/accel/ethosu/ethosu_gem.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/accel/ethosu/ethosu_gem.c b/drivers/accel/ethosu/ethosu_gem.c
index b4544298742f..928764de2e2c 100644
--- a/drivers/accel/ethosu/ethosu_gem.c
+++ b/drivers/accel/ethosu/ethosu_gem.c
@@ -1255,6 +1255,8 @@ static int ethosu_gem_cmdstream_copy_and_validate(struct drm_device *ddev,
st.ifm_upscale = param;
break;
case NPU_SET_IFM_BROADCAST:
+ if (ethosu_is_u65(edev))
+ return -EINVAL;
st.ifm.broadcast = param;
break;
case NPU_SET_IFM_REGION:
--
2.53.0
^ permalink raw reply related [flat|nested] 22+ messages in thread