* [PATCH v3 0/2] media: nxp: imx8-isi: Fix scaler rounding and downscaling limits
@ 2026-03-23 8:33 Guoniu Zhou
2026-03-23 8:33 ` [PATCH v3 1/2] media: nxp: imx8-isi: Fix potential out-of-bounds issues Guoniu Zhou
2026-03-23 8:33 ` [PATCH v3 2/2] media: nxp: imx8-isi: Fix scale factor calculation for hardware rounding Guoniu Zhou
0 siblings, 2 replies; 3+ messages in thread
From: Guoniu Zhou @ 2026-03-23 8:33 UTC (permalink / raw)
To: Laurent Pinchart, Mauro Carvalho Chehab, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, Stefan Riedmueller,
Jacopo Mondi, Christian Hemp, Frank Li
Cc: Dong Aisheng, linux-media, imx, linux-arm-kernel, linux-kernel,
Guoniu Zhou, stable, Frank Li
Patch 1/2 enforces the 16x downscaling limit to prevent out-of-bounds access.
Patch 2/2 ensures the scale factor accounts for ISI's rounding-up behavior
to avoid producing one extra output line.
Signed-off-by: Guoniu Zhou <guoniu.zhou@nxp.com>
---
Changes in v3:
- New patch 2/2 to fix scale factor rounding
- Replace CLAMP_DOWNSCALE_16 macro with inline function
- Adjust downscale threshold from 0x4000 to 0x2000
- Clarify downscaling limit in comment
- Link to v2: https://lore.kernel.org/all/20260228-isi-v2-1-f6f9208c6844@nxp.com/
Changes in v2:
- Fix possible side-effects caused by 'max_val' in CLAMP_DOWNSCALE_16 macro.
- Add space line between two paragraph in comment.
- Link to v1: https://lore.kernel.org/r/20260122-isi-v1-1-c3ec6e264f13@nxp.com
---
Guoniu Zhou (2):
media: nxp: imx8-isi: Fix potential out-of-bounds issues
media: nxp: imx8-isi: Fix scale factor calculation for hardware rounding
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h | 16 ++++++++++++++++
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c | 13 +++++++++++--
drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c | 11 ++++++++---
drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c | 13 ++++++++-----
4 files changed, 43 insertions(+), 10 deletions(-)
---
base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
change-id: 20260122-isi-74f87fbb9a6f
Best regards,
--
Guoniu Zhou <guoniu.zhou@oss.nxp.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v3 1/2] media: nxp: imx8-isi: Fix potential out-of-bounds issues
2026-03-23 8:33 [PATCH v3 0/2] media: nxp: imx8-isi: Fix scaler rounding and downscaling limits Guoniu Zhou
@ 2026-03-23 8:33 ` Guoniu Zhou
2026-03-23 8:33 ` [PATCH v3 2/2] media: nxp: imx8-isi: Fix scale factor calculation for hardware rounding Guoniu Zhou
1 sibling, 0 replies; 3+ messages in thread
From: Guoniu Zhou @ 2026-03-23 8:33 UTC (permalink / raw)
To: Laurent Pinchart, Mauro Carvalho Chehab, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, Stefan Riedmueller,
Jacopo Mondi, Christian Hemp, Frank Li
Cc: Dong Aisheng, linux-media, imx, linux-arm-kernel, linux-kernel,
Guoniu Zhou, stable, Frank Li
From: Guoniu Zhou <guoniu.zhou@nxp.com>
The maximum downscaling factor supported by ISI can be up to 16. Add
minimum value constraint before applying the setting to hardware.
Otherwise, the process will not respond even when Ctrl+C is executed.
Fixes: cf21f328fcaf ("media: nxp: Add i.MX8 ISI driver")
Cc: stable@vger.kernel.org
Reviewed-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Guoniu Zhou <guoniu.zhou@nxp.com>
---
Changes in v3:
- Replace CLAMP_DOWNSCALE_16 macro with inline function
- Adjust downscale threshold from 0x4000 to 0x2000
- Clarify downscaling limit in comment
---
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h | 16 ++++++++++++++++
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c | 2 +-
drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c | 11 ++++++++---
drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c | 13 ++++++++-----
4 files changed, 33 insertions(+), 9 deletions(-)
diff --git a/drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h b/drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h
index 3cbd35305af0..822466445b72 100644
--- a/drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h
+++ b/drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h
@@ -11,6 +11,7 @@
#define __MXC_ISI_CORE_H__
#include <linux/list.h>
+#include <linux/math.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/types.h>
@@ -413,4 +414,19 @@ static inline void mxc_isi_debug_cleanup(struct mxc_isi_dev *isi)
}
#endif
+/*
+ * ISI scaling engine works in two parts: it performs pre-decimation of
+ * the image followed by bilinear filtering to achieve the desired
+ * downscaling factor.
+ *
+ * The decimation filter provides a maximum downscaling factor of 8, and
+ * the subsequent bilinear filter provides a maximum downscaling factor
+ * of 2. Combined, the maximum scaling factor can be up to 16.
+ */
+static inline unsigned int
+mxc_isi_clamp_downscale_16(unsigned int val, unsigned int max_val)
+{
+ return clamp(val, max(1U, DIV_ROUND_UP(max_val, 16)), max_val);
+}
+
#endif /* __MXC_ISI_CORE_H__ */
diff --git a/drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c b/drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c
index 9225a7ac1c3e..37e59d687ed7 100644
--- a/drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c
+++ b/drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c
@@ -11,7 +11,7 @@
#include "imx8-isi-core.h"
#include "imx8-isi-regs.h"
-#define ISI_DOWNSCALE_THRESHOLD 0x4000
+#define ISI_DOWNSCALE_THRESHOLD 0x2000
static inline u32 mxc_isi_read(struct mxc_isi_pipe *pipe, u32 reg)
{
diff --git a/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c b/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
index a39ad7a1ab18..a0e2061f4344 100644
--- a/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
+++ b/drivers/media/platform/nxp/imx8-isi/imx8-isi-m2m.c
@@ -508,10 +508,15 @@ __mxc_isi_m2m_try_fmt_vid(struct mxc_isi_m2m_ctx *ctx,
struct v4l2_pix_format_mplane *pix,
const enum mxc_isi_video_type type)
{
+ const struct v4l2_pix_format_mplane *format =
+ &ctx->queues.out.format;
+
if (type == MXC_ISI_VIDEO_M2M_CAP) {
- /* Downscaling only */
- pix->width = min(pix->width, ctx->queues.out.format.width);
- pix->height = min(pix->height, ctx->queues.out.format.height);
+ /* Downscaling only, by up to 16. */
+ pix->width = mxc_isi_clamp_downscale_16(pix->width,
+ format->width);
+ pix->height = mxc_isi_clamp_downscale_16(pix->height,
+ format->height);
}
return mxc_isi_format_try(ctx->m2m->pipe, pix, type);
diff --git a/drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c b/drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c
index a41c51dd9ce0..b290821d03d2 100644
--- a/drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c
+++ b/drivers/media/platform/nxp/imx8-isi/imx8-isi-pipe.c
@@ -641,16 +641,19 @@ static int mxc_isi_pipe_set_selection(struct v4l2_subdev *sd,
/* Composing is supported on the sink only. */
return -EINVAL;
- /* The sink crop is bound by the sink format downscaling only). */
+ /*
+ * The ISI supports downscaling only, with a factor up to 16.
+ * Clamp the compose rectangle size accordingly.
+ */
format = mxc_isi_pipe_get_pad_format(pipe, state,
MXC_ISI_PIPE_PAD_SINK);
sel->r.left = 0;
sel->r.top = 0;
- sel->r.width = clamp(sel->r.width, MXC_ISI_MIN_WIDTH,
- format->width);
- sel->r.height = clamp(sel->r.height, MXC_ISI_MIN_HEIGHT,
- format->height);
+ sel->r.width = mxc_isi_clamp_downscale_16(sel->r.width,
+ format->width);
+ sel->r.height = mxc_isi_clamp_downscale_16(sel->r.height,
+ format->height);
rect = mxc_isi_pipe_get_pad_compose(pipe, state,
MXC_ISI_PIPE_PAD_SINK);
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v3 2/2] media: nxp: imx8-isi: Fix scale factor calculation for hardware rounding
2026-03-23 8:33 [PATCH v3 0/2] media: nxp: imx8-isi: Fix scaler rounding and downscaling limits Guoniu Zhou
2026-03-23 8:33 ` [PATCH v3 1/2] media: nxp: imx8-isi: Fix potential out-of-bounds issues Guoniu Zhou
@ 2026-03-23 8:33 ` Guoniu Zhou
1 sibling, 0 replies; 3+ messages in thread
From: Guoniu Zhou @ 2026-03-23 8:33 UTC (permalink / raw)
To: Laurent Pinchart, Mauro Carvalho Chehab, Shawn Guo, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, Stefan Riedmueller,
Jacopo Mondi, Christian Hemp, Frank Li
Cc: Dong Aisheng, linux-media, imx, linux-arm-kernel, linux-kernel,
Guoniu Zhou, stable
From: Guoniu Zhou <guoniu.zhou@nxp.com>
The ISI hardware rounds the actual output size up to an integer, as
described in i.MX93 Reference Manual section 57.7.8 (Channel 0 Scale
Factor). The scale factor must be calculated to ensure the theoretical
output value rounds up to exactly the desired size.
Fixes: cf21f328fcaf ("media: nxp: Add i.MX8 ISI driver")
Cc: stable@vger.kernel.org
Signed-off-by: Guoniu Zhou <guoniu.zhou@nxp.com>
---
drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c b/drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c
index 37e59d687ed7..a2edac8292a7 100644
--- a/drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c
+++ b/drivers/media/platform/nxp/imx8-isi/imx8-isi-hw.c
@@ -112,7 +112,16 @@ static u32 mxc_isi_channel_scaling_ratio(unsigned int from, unsigned int to,
else
*dec = 8;
- return min_t(u32, from * 0x1000 / (to * *dec), ISI_DOWNSCALE_THRESHOLD);
+ /*
+ * The ISI rounds output dimensions up to the next integer (i.MX93 RM
+ * section 57.7.8). Calculate the scale factor such that the theoretical
+ * output (input / scale_factor) rounds up to exactly the desired output.
+ *
+ * Example from the reference manual: Scaling 800 to 720 lines
+ * - scale = 0x11C8: 800/0x1C8 = 719.859375 -> 720 (correct)
+ * - scale = 0x11C7: 800/0x1C7 = 720.017578 -> 721 (one extra line)
+ */
+ return min_t(u32, DIV_ROUND_UP(from * 0x1000, to * *dec), ISI_DOWNSCALE_THRESHOLD);
}
static void mxc_isi_channel_set_scaling(struct mxc_isi_pipe *pipe,
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-03-23 8:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-23 8:33 [PATCH v3 0/2] media: nxp: imx8-isi: Fix scaler rounding and downscaling limits Guoniu Zhou
2026-03-23 8:33 ` [PATCH v3 1/2] media: nxp: imx8-isi: Fix potential out-of-bounds issues Guoniu Zhou
2026-03-23 8:33 ` [PATCH v3 2/2] media: nxp: imx8-isi: Fix scale factor calculation for hardware rounding Guoniu Zhou
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox