Skip to contentSkip to conversation

Add AMD ISP4 driver

PATCH Superseded Bin Du 5 participants 98 messages Jun 18, 2025 – Sep 22, 2025 Page 2 of 3

Subsystems (9)

v2 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
author maintainer
#28 lore raw

Re: [PATCH v2 4/8] media: platform: amd: Add isp4 fw and hw interface

In reply to Sakari Ailus

Many thanks Askari Ailus for your careful review

Quoted 16 lines · Sakari Ailus

Hi Bin,

Quoted 4 lines · Bin Du

ISP firmware controls ISP HW pipeline using dedicated embedded processor called ccpu.
The communication between ISP FW and driver is using commands and response messages sent through the ring buffer. Command buffers support

Quoted 8 lines

either global setting that is not specific to the steam and support stream specific parameters. Response buffers contains ISP FW notification information such as frame buffer done and command done. IRQ is used for receiving response buffer from ISP firmware, which is handled in the main isp4 media device. ISP ccpu is booted up through the firmware loading helper function prior to stream start.
Memory used for command buffer and response buffer needs to be allocated from amdgpu buffer manager because isp4 is a child device of amdgpu.

Quoted 2 lines

Please rewrap this, some lines above are quite short.

Thanks, the line after the short line is supposed to be a new paragraph? Should we put all the description in one paragraph?

Quoted 93 lines
Quoted 93 lines
---
  drivers/media/platform/amd/isp4/Makefile      |   12 +
  .../platform/amd/isp4/isp4_fw_cmd_resp.h      |  318 +++++
  .../media/platform/amd/isp4/isp4_interface.c  | 1052 +++++++++++++++++
  .../media/platform/amd/isp4/isp4_interface.h  |  164 +++
  4 files changed, 1546 insertions(+)

create mode 100644 drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h create mode 100644 drivers/media/platform/amd/isp4/isp4_interface.c create mode 100644 drivers/media/platform/amd/isp4/isp4_interface.h

drivers/media/platform/amd/isp4/Makefile +12 -0
index 0e36201fbb30..c0166f954516 100644
@@ -5,10 +5,22 @@
  obj-$(CONFIG_AMD_ISP4) += amd_capture.o
  amd_capture-objs := isp4.o	\
  			isp4_phy.o \
+			isp4_interface.o \
  			isp4_hw.o	\
 
  ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
+ccflags-y += -I$(srctree)/drivers/gpu/drm/amd/include
+ccflags-y += -I$(srctree)/drivers/gpu/drm/amd/amdgpu
+ccflags-y += -I$(srctree)/drivers/gpu/drm/amd/pm/inc
+ccflags-y += -I$(srctree)/include/drm
  ccflags-y += -I$(srctree)/include
+ccflags-y += -I$(srctree)/include/uapi/drm
+ccflags-y += -I$(srctree)/drivers/gpu/drm/amd/acp/include
+ccflags-y += -I$(srctree)/drivers/gpu/drm/amd/display
+ccflags-y += -I$(srctree)/drivers/gpu/drm/amd/display/include
+ccflags-y += -I$(srctree)/drivers/gpu/drm/amd/display/modules/inc
+ccflags-y += -I$(srctree)/drivers/gpu/drm/amd/display/dc
+ccflags-y += -I$(srctree)/drivers/gpu/drm/amd/display/amdgpu_dm
 
  ifneq ($(call cc-option, -mpreferred-stack-boundary=4),)
  	cc_stack_align := -mpreferred-stack-boundary=4
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +47 -0
new file mode 100644
index 000000000000..437d89469af2
@@ -0,0 +1,318 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_CMD_RESP_H_
+#define _ISP4_CMD_RESP_H_
+
+/*
+ * @brief Host and Firmware command & response channel.
+ *        Two types of command/response channel.
+ *          Type Global Command has one command/response channel.
+ *          Type Stream Command has one command/response channel.
+ *-----------                                        ------------
+ *|         |       ---------------------------      |          |
+ *|         |  ---->|  Global Command         |----> |          |
+ *|         |       ---------------------------      |          |
+ *|         |                                        |          |
+ *|         |                                        |          |
+ *|         |       ---------------------------      |          |
+ *|         |  ---->|   Stream Command        |----> |          |
+ *|         |       ---------------------------      |          |
+ *|         |                                        |          |
+ *|         |                                        |          |
+ *|         |                                        |          |
+ *|  HOST   |                                        | Firmware |
+ *|         |                                        |          |
+ *|         |                                        |          |
+ *|         |       --------------------------       |          |
+ *|         |  <----|  Global Response       |<----  |          |
+ *|         |       --------------------------       |          |
+ *|         |                                        |          |
+ *|         |                                        |          |
+ *|         |       --------------------------       |          |
+ *|         |  <----|  Stream Response       |<----  |          |
+ *|         |       --------------------------       |          |
+ *|         |                                        |          |
+ *|         |                                        |          |
+ *-----------                                        ------------
+ */
+
+/*
+ * @brief command ID format
+ *        cmd_id is in the format of following type:
+ *        type: indicate command type, global/stream commands.
+ *        group: indicate the command group.
+ *        id: A unique command identification in one type and group.
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h line 53

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +6 -0
+ *        |<-Bit31 ~ Bit24->|<-Bit23 ~ Bit16->|<-Bit15 ~ Bit0->|
+ *        |      type       |      group      |       id       |
+ */
+
+#define CMD_TYPE_SHIFT (24)
+#define CMD_GROUP_SHIFT (16)

Redundant parentheses. Please remove all of them.

Sure, will remove them in the next patch

Quoted 4 lines

drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h line 54

Quoted 1 line
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +1 -0
+#define CMD_TYPE_STREAM_CTRL ((u32)0x2 << CMD_TYPE_SHIFT)

Maybe 0x2U << CMD_TYPE_SHIFT instead?

Sure, will modify in the next patch

Quoted 44 lines
Quoted 44 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +44 -0
+
+#define CMD_GROUP_STREAM_CTRL ((u32)0x1 << CMD_GROUP_SHIFT)
+#define CMD_GROUP_STREAM_BUFFER ((u32)0x4 << CMD_GROUP_SHIFT)
+
+/* Stream  Command */
+#define CMD_ID_SET_STREAM_CONFIG                        \
+	(CMD_TYPE_STREAM_CTRL | CMD_GROUP_STREAM_CTRL | 0x1)
+#define CMD_ID_SET_OUT_CHAN_PROP                        \
+	(CMD_TYPE_STREAM_CTRL | CMD_GROUP_STREAM_CTRL | 0x3)
+#define CMD_ID_ENABLE_OUT_CHAN                          \
+	(CMD_TYPE_STREAM_CTRL | CMD_GROUP_STREAM_CTRL | 0x5)
+#define CMD_ID_START_STREAM                             \
+	(CMD_TYPE_STREAM_CTRL | CMD_GROUP_STREAM_CTRL | 0x7)
+#define CMD_ID_STOP_STREAM                              \
+	(CMD_TYPE_STREAM_CTRL | CMD_GROUP_STREAM_CTRL | 0x8)
+
+/* Stream Buffer Command */
+#define CMD_ID_SEND_BUFFER                                \
+	(CMD_TYPE_STREAM_CTRL | CMD_GROUP_STREAM_BUFFER | 0x1)
+
+/*
+ * @brief response ID format
+ *        resp_id is in the format of following type:
+ *        type: indicate command type, global/stream commands.
+ *        group: indicate the command group.
+ *        id: A unique command identification in one type and group.
+ *        |<-Bit31 ~ Bit24->|<-Bit23 ~ Bit16->|<-Bit15 ~ Bit0->|
+ *        |      type       |      group      |       id       |
+ */
+
+#define RESP_GROUP_SHIFT (16)
+#define RESP_GROUP_MASK  (0xff << RESP_GROUP_SHIFT)
+
+#define GET_RESP_GROUP_VALUE(resp_id)   \
+	(((resp_id) & RESP_GROUP_MASK) >> \
+	 RESP_GROUP_SHIFT)
+#define GET_RESP_ID_VALUE(resp_id) ((resp_id) & 0xffff)
+
+#define RESP_GROUP_GENERAL (0x1 << RESP_GROUP_SHIFT)
+#define RESP_GROUP_NOTIFICATION (0x3 << RESP_GROUP_SHIFT)
+
+/* General  Response */
+#define RESP_ID_CMD_DONE (RESP_GROUP_GENERAL | 0x1)
+
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h line 104

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +6 -0
+/* Notification */
+#define RESP_ID_NOTI_FRAME_DONE (RESP_GROUP_NOTIFICATION | 0x1)
+
+#define CMD_STATUS_SUCCESS (0)
+#define CMD_STATUS_FAIL (1)
+#define CMD_STATUS_SKIPPED (2)

Again, please align macro bodies.

Sure, will modify in the next patch

Quoted 349 lines
Quoted 349 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +214 -0
+
+#define ADDR_SPACE_TYPE_GPU_VA 4
+
+#define FW_MEMORY_POOL_SIZE (200 * 1024 * 1024)
+
+/*
+ * standard ISP mipicsi=>isp
+ */
+#define MIPI0_ISP_PIPELINE_ID 0x5f91
+
+enum isp4fw_sensor_id {
+	SENSOR_ID_ON_MIPI0  = 0,  /* Sensor id for ISP input from MIPI port 0 */
+};
+
+enum isp4fw_stream_id {
+	STREAM_ID_INVALID = -1, /* STREAM_ID_INVALID. */
+	STREAM_ID_1 = 0,        /* STREAM_ID_1. */
+	STREAM_ID_2 = 1,        /* STREAM_ID_2. */
+	STREAM_ID_3 = 2,        /* STREAM_ID_3. */
+	STREAM_ID_MAXIMUM       /* STREAM_ID_MAXIMUM. */
+};
+
+enum isp4fw_image_format {
+	IMAGE_FORMAT_NV12 = 1,              /* 4:2:0,semi-planar, 8-bit */
+	IMAGE_FORMAT_YUV422INTERLEAVED = 7, /* interleave, 4:2:2, 8-bit */
+};
+
+enum isp4fw_pipe_out_ch {
+	ISP_PIPE_OUT_CH_PREVIEW = 0,
+};
+
+enum isp4fw_yuv_range {
+	ISP_YUV_RANGE_FULL = 0,     /* YUV value range in 0~255 */
+	ISP_YUV_RANGE_NARROW = 1,   /* YUV value range in 16~235 */
+	ISP_YUV_RANGE_MAX
+};
+
+enum isp4fw_buffer_type {
+	BUFFER_TYPE_PREVIEW = 8,
+	BUFFER_TYPE_META_INFO = 10,
+	BUFFER_TYPE_MEM_POOL = 15,
+};
+
+enum isp4fw_buffer_status {
+	BUFFER_STATUS_INVALID,  /* The buffer is INVALID */
+	BUFFER_STATUS_SKIPPED,  /* The buffer is not filled with image data */
+	BUFFER_STATUS_EXIST,    /* The buffer is exist and waiting for filled */
+	BUFFER_STATUS_DONE,     /* The buffer is filled with image data */
+	BUFFER_STATUS_LACK,     /* The buffer is unavailable */
+	BUFFER_STATUS_DIRTY,    /* The buffer is dirty, probably caused by
+				 * LMI leakage
+				 */
+	BUFFER_STATUS_MAX       /* The buffer STATUS_MAX */
+};
+
+enum isp4fw_buffer_source {
+	/* The buffer is from the stream buffer queue */
+	BUFFER_SOURCE_STREAM,
+};
+
+struct isp4fw_error_code {
+	u32 code1;
+	u32 code2;
+	u32 code3;
+	u32 code4;
+	u32 code5;
+};
+
+/*
+ * Command Structure for FW
+ */
+
+struct isp4fw_cmd {
+	u32 cmd_seq_num;
+	u32 cmd_id;
+	u32 cmd_param[12];
+	u16 cmd_stream_id;
+	u8 cmd_silent_resp;
+	u8 reserved;
+	u32 cmd_check_sum;
+};
+
+struct isp4fw_resp_cmd_done {
+	/* The host2fw command seqNum.
+	 * To indicate which command this response refer to.
+	 */
+	u32 cmd_seq_num;
+	/* The host2fw command id for host double check. */
+	u32 cmd_id;
+	/* Indicate the command process status.
+	 * 0 means success. 1 means fail. 2 means skipped
+	 */
+	u16 cmd_status;
+	/* If the cmd_status is 1, that means the command is processed fail, */
+	/* host can check the isp4fw_error_code to get the detail
+	 * error information
+	 */
+	u16 isp4fw_error_code;
+	/* The response payload will be in different struct type */
+	/* according to different cmd done response. */
+	u8 payload[36];
+};
+
+struct isp4fw_resp_param_package {
+	u32 package_addr_lo;	/* The low 32 bit addr of the pkg address. */
+	u32 package_addr_hi;	/* The high 32 bit addr of the pkg address. */
+	u32 package_size;	/* The total pkg size in bytes. */
+	u32 package_check_sum;	/* The byte sum of the pkg. */
+};
+
+struct isp4fw_resp {
+	u32 resp_seq_num;
+	u32 resp_id;
+	union {
+		struct isp4fw_resp_cmd_done cmd_done;
+		struct isp4fw_resp_param_package frame_done;
+		u32 resp_param[12];
+	} param;
+	u8  reserved[4];
+	u32 resp_check_sum;
+};
+
+struct isp4fw_mipi_pipe_path_cfg {
+	u32 b_enable;
+	enum isp4fw_sensor_id isp4fw_sensor_id;
+};
+
+struct isp4fw_isp_pipe_path_cfg {
+	u32  isp_pipe_id;	/* pipe ids for pipeline construction */
+};
+
+struct isp4fw_isp_stream_cfg {
+	/* Isp mipi path */
+	struct isp4fw_mipi_pipe_path_cfg mipi_pipe_path_cfg;
+	/* Isp pipe path */
+	struct isp4fw_isp_pipe_path_cfg  isp_pipe_path_cfg;
+	/* enable TNR */
+	u32 b_enable_tnr;
+	/* number of frame rta per-processing,
+	 * set to 0 to use fw default value
+	 */
+	u32 rta_frames_per_proc;
+};
+
+struct isp4fw_image_prop {
+	enum isp4fw_image_format image_format;	/* Image format */
+	u32 width;				/* Width */
+	u32 height;				/* Height */
+	u32 luma_pitch;				/* Luma pitch */
+	u32 chroma_pitch;			/* Chrom pitch */
+	enum isp4fw_yuv_range yuv_range;		/* YUV value range */
+};
+
+struct isp4fw_buffer {
+	/* A check num for debug usage, host need to */
+	/* set the buf_tags to different number */
+	u32 buf_tags;
+	union {
+		u32 value;
+		struct {
+			u32 space : 16;
+			u32 vmid  : 16;
+		} bit;
+	} vmid_space;
+	u32 buf_base_a_lo;		/* Low address of buffer A */
+	u32 buf_base_a_hi;		/* High address of buffer A */
+	u32 buf_size_a;			/* Buffer size of buffer A */
+
+	u32 buf_base_b_lo;		/* Low address of buffer B */
+	u32 buf_base_b_hi;		/* High address of buffer B */
+	u32 buf_size_b;			/* Buffer size of buffer B */
+
+	u32 buf_base_c_lo;		/* Low address of buffer C */
+	u32 buf_base_c_hi;		/* High address of buffer C */
+	u32 buf_size_c;			/* Buffer size of buffer C */
+};
+
+struct isp4fw_buffer_meta_info {
+	u32 enabled;					/* enabled flag */
+	enum isp4fw_buffer_status status;		/* BufferStatus */
+	struct isp4fw_error_code err;			/* err code */
+	enum isp4fw_buffer_source source;		/* BufferSource */
+	struct isp4fw_image_prop image_prop;		/* image_prop */
+	struct isp4fw_buffer buffer;			/* buffer */
+};
+
+struct isp4fw_meta_info {
+	u32 poc;				/* frame id */
+	u32 fc_id;				/* frame ctl id */
+	u32 time_stamp_lo;			/* time_stamp_lo */
+	u32 time_stamp_hi;			/* time_stamp_hi */
+	struct isp4fw_buffer_meta_info preview;	/* preview BufferMetaInfo */
+};
+
+struct isp4fw_cmd_send_buffer {
+	enum isp4fw_buffer_type buffer_type;	/* buffer Type */
+	struct isp4fw_buffer buffer;		/* buffer info */
+};
+
+struct isp4fw_cmd_set_out_ch_prop {
+	enum isp4fw_pipe_out_ch ch;	/* ISP pipe out channel */
+	struct isp4fw_image_prop image_prop;	/* image property */
+};
+
+struct isp4fw_cmd_enable_out_ch {
+	enum isp4fw_pipe_out_ch ch;	/* ISP pipe out channel */
+	u32 is_enable;			/* If enable channel or not */
+};
+
+struct isp4fw_cmd_set_stream_cfg {
+	struct isp4fw_isp_stream_cfg stream_cfg; /* stream path config */
+};
+
+#endif
drivers/media/platform/amd/isp4/isp4_interface.c new file +129 -0
new file mode 100644
index 000000000000..0e1eb22a0de5
@@ -0,0 +1,1052 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/mutex.h>
+
+#include "amdgpu_object.h"
+
+#include "isp4_fw_cmd_resp.h"
+#include "isp4_hw.h"
+#include "isp4_hw_reg.h"
+#include "isp4_interface.h"
+
+#define ISP4IF_FW_RESP_RB_IRQ_EN_MASK \
+	(ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT9_EN_MASK |  \
+	 ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT10_EN_MASK | \
+	 ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT11_EN_MASK | \
+	 ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT12_EN_MASK)
+
+struct isp4if_rb_config {
+	const char *name;
+	u32 index;
+	u32 reg_rptr;
+	u32 reg_wptr;
+	u32 reg_base_lo;
+	u32 reg_base_hi;
+	u32 reg_size;
+	u32 val_size;
+	u64 base_mc_addr;
+	void *base_sys_addr;
+};
+
+/* FW cmd ring buffer configuration */
+static struct isp4if_rb_config
+	isp4if_cmd_rb_config[ISP4IF_STREAM_ID_MAX] = {
+	{
+		.name = "CMD_RB_GBL0",
+		.index = 3,
+		.reg_rptr = ISP_RB_RPTR4,
+		.reg_wptr = ISP_RB_WPTR4,
+		.reg_base_lo = ISP_RB_BASE_LO4,
+		.reg_base_hi = ISP_RB_BASE_HI4,
+		.reg_size = ISP_RB_SIZE4,
+	},
+	{
+		.name = "CMD_RB_STR1",
+		.index = 0,
+		.reg_rptr = ISP_RB_RPTR1,
+		.reg_wptr = ISP_RB_WPTR1,
+		.reg_base_lo = ISP_RB_BASE_LO1,
+		.reg_base_hi = ISP_RB_BASE_HI1,
+		.reg_size = ISP_RB_SIZE1,
+	},
+	{
+		.name = "CMD_RB_STR2",
+		.index = 1,
+		.reg_rptr = ISP_RB_RPTR2,
+		.reg_wptr = ISP_RB_WPTR2,
+		.reg_base_lo = ISP_RB_BASE_LO2,
+		.reg_base_hi = ISP_RB_BASE_HI2,
+		.reg_size = ISP_RB_SIZE2,
+	},
+	{
+		.name = "CMD_RB_STR3",
+		.index = 2,
+		.reg_rptr = ISP_RB_RPTR3,
+		.reg_wptr = ISP_RB_WPTR3,
+		.reg_base_lo = ISP_RB_BASE_LO3,
+		.reg_base_hi = ISP_RB_BASE_HI3,
+		.reg_size = ISP_RB_SIZE3,
+	},
+};
+
+/* FW resp ring buffer configuration */
+static struct isp4if_rb_config
+	isp4if_resp_rb_config[ISP4IF_STREAM_ID_MAX] = {
+	{
+		.name = "RES_RB_GBL0",
+		.index = 3,
+		.reg_rptr = ISP_RB_RPTR12,
+		.reg_wptr = ISP_RB_WPTR12,
+		.reg_base_lo = ISP_RB_BASE_LO12,
+		.reg_base_hi = ISP_RB_BASE_HI12,
+		.reg_size = ISP_RB_SIZE12,
+	},
+	{
+		.name = "RES_RB_STR1",
+		.index = 0,
+		.reg_rptr = ISP_RB_RPTR9,
+		.reg_wptr = ISP_RB_WPTR9,
+		.reg_base_lo = ISP_RB_BASE_LO9,
+		.reg_base_hi = ISP_RB_BASE_HI9,
+		.reg_size = ISP_RB_SIZE9,
+	},
+	{
+		.name = "RES_RB_STR2",
+		.index = 1,
+		.reg_rptr = ISP_RB_RPTR10,
+		.reg_wptr = ISP_RB_WPTR10,
+		.reg_base_lo = ISP_RB_BASE_LO10,
+		.reg_base_hi = ISP_RB_BASE_HI10,
+		.reg_size = ISP_RB_SIZE10,
+	},
+	{
+		.name = "RES_RB_STR3",
+		.index = 2,
+		.reg_rptr = ISP_RB_RPTR11,
+		.reg_wptr = ISP_RB_WPTR11,
+		.reg_base_lo = ISP_RB_BASE_LO11,
+		.reg_base_hi = ISP_RB_BASE_HI11,
+		.reg_size = ISP_RB_SIZE11,
+	},
+};
+
+/* FW log ring buffer configuration */
+static struct isp4if_rb_config isp4if_log_rb_config = {
+	.name = "LOG_RB",
+	.index = 0,
+	.reg_rptr = ISP_LOG_RB_RPTR0,
+	.reg_wptr = ISP_LOG_RB_WPTR0,
+	.reg_base_lo = ISP_LOG_RB_BASE_LO0,
+	.reg_base_hi = ISP_LOG_RB_BASE_HI0,
+	.reg_size = ISP_LOG_RB_SIZE0,
+};
+
+static struct isp4if_gpu_mem_info *isp4if_gpu_mem_alloc(struct isp4_interface
+							*ispif,
+							u32 mem_size)
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 135

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+{
+	struct isp4if_gpu_mem_info *mem_info;
+	struct amdgpu_bo *bo = NULL;
+	struct amdgpu_device *adev;
+	struct device *dev;
+

Extra newline.

Sure, will remove in the next patch

Quoted 17 lines
Quoted 4 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +4 -0
+	void *cpu_ptr;
+	u64 gpu_addr;
+	u32 ret;
+

drivers/media/platform/amd/isp4/isp4_interface.c line 149

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +10 -0
+	dev = ispif->dev;
+
+	if (!mem_size)
+		return NULL;
+
+	mem_info = kzalloc(sizeof(*mem_info), GFP_KERNEL);
+	if (!mem_info)
+		return NULL;
+
+	adev = (struct amdgpu_device *)ispif->adev;

Why the cast?

Quoted 2 lines

adev isn't a great name here as it's usually used for struct acpi_devices.

In the next patch, will use new helper function for this and will no longer use amdgpu_device

Quoted 5 lines
Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +5 -0
+	mem_info->mem_size = mem_size;
+	mem_info->mem_align = ISP4IF_ISP_MC_ADDR_ALIGN;
+	mem_info->mem_domain = AMDGPU_GEM_DOMAIN_GTT;
+
+	ret = amdgpu_bo_create_kernel(adev,
Quoted 11 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 160

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+				      mem_info->mem_size,
+				      mem_info->mem_align,
+				      mem_info->mem_domain,
+				      &bo,
+				      &gpu_addr,
+				      &cpu_ptr);

This fits on fewer lines.

Quoted 1 line
drivers/media/platform/amd/isp4/isp4_interface.c new file +1 -0
+

Sure, will modify in the next patch

Quoted 2 lines

Extra newline.

Sure, will modify in the next patch

Quoted 19 lines
Quoted 19 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +19 -0
+	if (!cpu_ptr || ret) {
+		dev_err(dev, "gpuvm buffer alloc fail, size %u\n", mem_size);
+		kfree(mem_info);
+		return NULL;
+	}
+
+	mem_info->sys_addr = cpu_ptr;
+	mem_info->gpu_mc_addr = gpu_addr;
+	mem_info->mem_handle = (void *)bo;
+
+	return mem_info;
+}
+
+static int isp4if_gpu_mem_free(struct isp4_interface *ispif,
+			       struct isp4if_gpu_mem_info *mem_info)
+{
+	struct device *dev = ispif->dev;
+	struct amdgpu_bo *bo;
+
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 186

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+	if (!mem_info) {
+		dev_err(dev, "invalid mem_info\n");
+		return -EINVAL;
+	}
+
+	bo = (struct amdgpu_bo *)mem_info->mem_handle;

Why do you need to cast here?

In the next patch, will use new helper function for this and will no longer use amdgpu_bo

Quoted 35 lines
Quoted 35 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +35 -0
+
+	amdgpu_bo_free_kernel(&bo, &mem_info->gpu_mc_addr, &mem_info->sys_addr);
+
+	kfree(mem_info);
+
+	return 0;
+}
+
+static int isp4if_dealloc_fw_gpumem(struct isp4_interface *ispif)
+{
+	int i;
+
+	if (ispif->fw_mem_pool) {
+		isp4if_gpu_mem_free(ispif, ispif->fw_mem_pool);
+		ispif->fw_mem_pool = NULL;
+	}
+
+	if (ispif->fw_cmd_resp_buf) {
+		isp4if_gpu_mem_free(ispif, ispif->fw_cmd_resp_buf);
+		ispif->fw_cmd_resp_buf = NULL;
+	}
+
+	if (ispif->fw_log_buf) {
+		isp4if_gpu_mem_free(ispif, ispif->fw_log_buf);
+		ispif->fw_log_buf = NULL;
+	}
+
+	for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++) {
+		if (ispif->metainfo_buf_pool[i]) {
+			isp4if_gpu_mem_free(ispif, ispif->metainfo_buf_pool[i]);
+			ispif->metainfo_buf_pool[i] = NULL;
+		}
+	}
+
+	return 0;
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 227

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+}
+
+static int isp4if_alloc_fw_gpumem(struct isp4_interface *ispif)
+{
+	struct device *dev = ispif->dev;
+	int i;

Unsigned int, please.

Sure, will modify in the next patch

Quoted 110 lines
Quoted 110 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +110 -0
+
+	ispif->fw_mem_pool = isp4if_gpu_mem_alloc(ispif, FW_MEMORY_POOL_SIZE);
+	if (!ispif->fw_mem_pool)
+		goto error_no_memory;
+
+	ispif->fw_cmd_resp_buf =
+		isp4if_gpu_mem_alloc(ispif, ISP4IF_RB_PMBMAP_MEM_SIZE);
+	if (!ispif->fw_cmd_resp_buf)
+		goto error_no_memory;
+
+	ispif->fw_log_buf =
+		isp4if_gpu_mem_alloc(ispif, ISP4IF_FW_LOG_RINGBUF_SIZE);
+	if (!ispif->fw_log_buf)
+		goto error_no_memory;
+
+	for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++) {
+		ispif->metainfo_buf_pool[i] =
+			isp4if_gpu_mem_alloc(ispif,
+					     ISP4IF_META_INFO_BUF_SIZE);
+		if (!ispif->metainfo_buf_pool[i])
+			goto error_no_memory;
+	}
+
+	return 0;
+
+error_no_memory:
+	dev_err(dev, "failed to allocate gpu memory");
+	return -ENOMEM;
+}
+
+static u32 isp4if_compute_check_sum(u8 *buf, u32 buf_size)
+{
+	u32 checksum = 0;
+	u8 *surplus_ptr;
+	u32 *buffer;
+	u32 i;
+
+	buffer = (u32 *)buf;
+	for (i = 0; i < buf_size / sizeof(u32); i++)
+		checksum += buffer[i];
+
+	surplus_ptr = (u8 *)&buffer[i];
+	/* add surplus data crc checksum */
+	for (i = 0; i < buf_size % sizeof(u32); i++)
+		checksum += surplus_ptr[i];
+
+	return checksum;
+}
+
+void isp4if_clear_cmdq(struct isp4_interface *ispif)
+{
+	struct isp4if_cmd_element *buf_node = NULL;
+	struct isp4if_cmd_element *tmp_node = NULL;
+
+	guard(mutex)(&ispif->cmdq_mutex);
+
+	list_for_each_entry_safe(buf_node, tmp_node, &ispif->cmdq, list) {
+		list_del(&buf_node->list);
+		kfree(buf_node);
+	}
+}
+
+static bool isp4if_is_cmdq_rb_full(struct isp4_interface *ispif,
+				   enum isp4if_stream_id cmd_buf_idx)
+{
+	struct isp4if_rb_config *rb_config;
+	u32 rd_ptr, wr_ptr;
+	u32 new_wr_ptr;
+	u32 rreg;
+	u32 wreg;
+	u32 len;
+
+	rb_config = &isp4if_cmd_rb_config[cmd_buf_idx];
+	rreg = rb_config->reg_rptr;
+	wreg = rb_config->reg_wptr;
+	len = rb_config->val_size;
+
+	rd_ptr = isp4hw_rreg(ispif->mmio, rreg);
+	wr_ptr = isp4hw_rreg(ispif->mmio, wreg);
+
+	new_wr_ptr = wr_ptr + sizeof(struct isp4fw_cmd);
+
+	if (wr_ptr >= rd_ptr) {
+		if (new_wr_ptr < len) {
+			return false;
+		} else if (new_wr_ptr == len) {
+			if (rd_ptr == 0)
+				return true;
+
+			return false;
+		}
+
+		new_wr_ptr -= len;
+		if (new_wr_ptr < rd_ptr)
+			return false;
+
+		return true;
+	}
+
+	if (new_wr_ptr < rd_ptr)
+		return false;
+
+	return true;
+}
+
+static struct isp4if_cmd_element *
+isp4if_append_cmd_2_cmdq(struct isp4_interface *ispif,
+			 struct isp4if_cmd_element *cmd_ele)
+{
+	struct isp4if_cmd_element *copy_command = NULL;
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 343

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+
+	copy_command = kmalloc(sizeof(*copy_command), GFP_KERNEL);
+	if (!copy_command)
+		return NULL;
+
+	memcpy(copy_command, cmd_ele, sizeof(*copy_command));

kmemdup()?

Kmemdup is to allocate memory and copy, can't be used here.

Quoted 157 lines
Quoted 157 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +157 -0
+
+	guard(mutex)(&ispif->cmdq_mutex);
+
+	list_add_tail(&copy_command->list, &ispif->cmdq);
+
+	return copy_command;
+}
+
+struct isp4if_cmd_element *
+isp4if_rm_cmd_from_cmdq(struct isp4_interface *ispif,
+			u32 seq_num,
+			u32 cmd_id)
+{
+	struct isp4if_cmd_element *buf_node = NULL;
+	struct isp4if_cmd_element *tmp_node = NULL;
+
+	guard(mutex)(&ispif->cmdq_mutex);
+
+	list_for_each_entry_safe(buf_node, tmp_node, &ispif->cmdq, list) {
+		if (buf_node->seq_num == seq_num &&
+		    buf_node->cmd_id == cmd_id) {
+			list_del(&buf_node->list);
+			return buf_node;
+		}
+	}
+
+	return NULL;
+}
+
+static int isp4if_insert_isp_fw_cmd(struct isp4_interface *ispif,
+				    enum isp4if_stream_id stream,
+				    struct isp4fw_cmd *cmd)
+{
+	struct isp4if_rb_config *rb_config;
+	struct device *dev = ispif->dev;
+	u64 mem_addr;
+	u64 mem_sys;
+	u32 wr_ptr;
+	u32 rd_ptr;
+	u32 rreg;
+	u32 wreg;
+	u32 len;
+
+	rb_config = &isp4if_cmd_rb_config[stream];
+	rreg = rb_config->reg_rptr;
+	wreg = rb_config->reg_wptr;
+	mem_sys = (u64)rb_config->base_sys_addr;
+	mem_addr = rb_config->base_mc_addr;
+	len = rb_config->val_size;
+
+	if (isp4if_is_cmdq_rb_full(ispif, stream)) {
+		dev_err(dev, "fail no cmdslot (%d)\n", stream);
+		return -EINVAL;
+	}
+
+	wr_ptr = isp4hw_rreg(ispif->mmio, wreg);
+	rd_ptr = isp4hw_rreg(ispif->mmio, rreg);
+
+	if (rd_ptr > len) {
+		dev_err(dev, "fail (%u),rd_ptr %u(should<=%u),wr_ptr %u\n",
+			stream, rd_ptr, len, wr_ptr);
+		return -EINVAL;
+	}
+
+	if (wr_ptr > len) {
+		dev_err(dev, "fail (%u),wr_ptr %u(should<=%u), rd_ptr %u\n",
+			stream, wr_ptr, len, rd_ptr);
+		return -EINVAL;
+	}
+
+	if (wr_ptr < rd_ptr) {
+		mem_addr += wr_ptr;
+
+		memcpy((u8 *)(mem_sys + wr_ptr),
+		       (u8 *)cmd, sizeof(struct isp4fw_cmd));
+	} else {
+		if ((len - wr_ptr) >= (sizeof(struct isp4fw_cmd))) {
+			mem_addr += wr_ptr;
+
+			memcpy((u8 *)(mem_sys + wr_ptr),
+			       (u8 *)cmd, sizeof(struct isp4fw_cmd));
+		} else {
+			u32 size;
+			u8 *src;
+
+			src = (u8 *)cmd;
+			size = len - wr_ptr;
+
+			memcpy((u8 *)(mem_sys + wr_ptr), src, size);
+
+			src += size;
+			size = sizeof(struct isp4fw_cmd) - size;
+			memcpy((u8 *)(mem_sys), src, size);
+		}
+	}
+
+	wr_ptr += sizeof(struct isp4fw_cmd);
+	if (wr_ptr >= len)
+		wr_ptr -= len;
+
+	isp4hw_wreg(ispif->mmio, wreg, wr_ptr);
+
+	return 0;
+}
+
+static inline enum isp4if_stream_id isp4if_get_fw_stream(u32 cmd_id)
+{
+	return ISP4IF_STREAM_ID_1;
+}
+
+static int isp4if_send_fw_cmd(struct isp4_interface *ispif,
+			      u32 cmd_id,
+			      void *package,
+			      u32 package_size,
+			      wait_queue_head_t *wq,
+			      u32 *wq_cond,
+			      u32 *seq)
+{
+	enum isp4if_stream_id stream = isp4if_get_fw_stream(cmd_id);
+	struct isp4if_cmd_element command_element = { 0 };
+	struct isp4if_gpu_mem_info *gpu_mem = NULL;
+	struct isp4if_cmd_element *cmd_ele = NULL;
+	struct isp4if_rb_config *rb_config;
+	struct device *dev = ispif->dev;
+	struct isp4fw_cmd cmd = {0};
+	u64 package_base = 0;
+	u32 sleep_count;
+	u32 seq_num;
+	u32 rreg;
+	u32 wreg;
+	int ret;
+
+	if (package_size > sizeof(cmd.cmd_param)) {
+		dev_err(dev, "fail pkgsize(%u)>%lu cmd:0x%x,stream %d\n",
+			package_size, sizeof(cmd.cmd_param), cmd_id, stream);
+		return -EINVAL;
+	}
+
+	sleep_count = 0;
+
+	rb_config = &isp4if_resp_rb_config[stream];
+	rreg = rb_config->reg_rptr;
+	wreg = rb_config->reg_wptr;
+
+	guard(mutex)(&ispif->isp4if_mutex);
+
+	while (1) {
+		if (isp4if_is_cmdq_rb_full(ispif, stream)) {
+			u32 rd_ptr, wr_ptr;
+
+			if (sleep_count < ISP4IF_MAX_SLEEP_COUNT) {
+				msleep(ISP4IF_MAX_SLEEP_TIME);
+				sleep_count++;
+				continue;
+			}
+			rd_ptr = isp4hw_rreg(ispif->mmio, rreg);
+			wr_ptr = isp4hw_rreg(ispif->mmio, wreg);
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 506

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+			dev_err(dev, "fail to get cmdq slot,stream (%d), rd %u, wr %u\n",
+				stream, rd_ptr, wr_ptr);
+			return -ETIMEDOUT;
+		}
+		break;
+	}

read_poll_timeout()?

Sure, will modify in the next patch

Quoted 8 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 511

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +5 -0
+
+	cmd.cmd_id = cmd_id;
+	switch (stream) {
+	case ISP4IF_STREAM_ID_GLOBAL:
+		cmd.cmd_stream_id = (u16)STREAM_ID_INVALID;

Is there a need to cast here?

Sure, will remove it in the next patch

Quoted 60 lines
Quoted 47 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +47 -0
+		break;
+	case ISP4IF_STREAM_ID_1:
+		cmd.cmd_stream_id = STREAM_ID_1;
+		break;
+	default:
+		dev_err(dev, "fail bad stream id %d\n", stream);
+		return -EINVAL;
+	}
+
+	if (package && package_size)
+		memcpy(cmd.cmd_param, package, package_size);
+
+	seq_num = ispif->host2fw_seq_num++;
+	cmd.cmd_seq_num = seq_num;
+	cmd.cmd_check_sum =
+		isp4if_compute_check_sum((u8 *)&cmd, sizeof(cmd) - 4);
+
+	if (seq)
+		*seq = seq_num;
+	command_element.seq_num = seq_num;
+	command_element.cmd_id = cmd_id;
+	command_element.mc_addr = package_base;
+	command_element.wq = wq;
+	command_element.wq_cond = wq_cond;
+	command_element.gpu_pkg = gpu_mem;
+	command_element.stream = stream;
+	/* only append the fw cmd to queue when its response needs to be
+	 * waited for, currently there are only two such commands,
+	 * disable channel and stop stream which are only sent after close
+	 * camera
+	 */
+	if (wq && wq_cond) {
+		cmd_ele = isp4if_append_cmd_2_cmdq(ispif, &command_element);
+		if (!cmd_ele) {
+			dev_err(dev, "fail for isp_append_cmd_2_cmdq\n");
+			return -ENOMEM;
+		}
+	}
+
+	ret = isp4if_insert_isp_fw_cmd(ispif, stream, &cmd);
+	if (ret) {
+		dev_err(dev, "fail for insert_isp_fw_cmd camId (0x%08x)\n",
+			cmd_id);
+		if (cmd_ele) {
+			isp4if_rm_cmd_from_cmdq(ispif, cmd_ele->seq_num,
+						cmd_ele->cmd_id);
+			kfree(cmd_ele);

drivers/media/platform/amd/isp4/isp4_interface.c line 568

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +10 -0
+		}
+	}
+
+	return ret;
+}
+
+static int isp4if_send_buffer(struct isp4_interface *ispif,
+			      struct isp4if_img_buf_info *buf_info)
+{
+	struct isp4fw_cmd_send_buffer cmd;

= { };

Quoted 2 lines

And you can remove the memset.

Sure, will modify in the next patch

Quoted 107 lines
Quoted 107 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +107 -0
+
+	memset(&cmd, 0, sizeof(cmd));
+	cmd.buffer_type = BUFFER_TYPE_PREVIEW;
+	cmd.buffer.vmid_space.bit.vmid = 0;
+	cmd.buffer.vmid_space.bit.space = ADDR_SPACE_TYPE_GPU_VA;
+	isp4if_split_addr64(buf_info->planes[0].mc_addr,
+			    &cmd.buffer.buf_base_a_lo,
+			    &cmd.buffer.buf_base_a_hi);
+	cmd.buffer.buf_size_a = buf_info->planes[0].len;
+
+	isp4if_split_addr64(buf_info->planes[1].mc_addr,
+			    &cmd.buffer.buf_base_b_lo,
+			    &cmd.buffer.buf_base_b_hi);
+	cmd.buffer.buf_size_b = buf_info->planes[1].len;
+
+	isp4if_split_addr64(buf_info->planes[2].mc_addr,
+			    &cmd.buffer.buf_base_c_lo,
+			    &cmd.buffer.buf_base_c_hi);
+	cmd.buffer.buf_size_c = buf_info->planes[2].len;
+
+	return isp4if_send_fw_cmd(ispif, CMD_ID_SEND_BUFFER, &cmd,
+				  sizeof(cmd), NULL, NULL, NULL);
+}
+
+static void isp4if_init_rb_config(struct isp4_interface *ispif,
+				  struct isp4if_rb_config *rb_config)
+{
+	u32 lo;
+	u32 hi;
+
+	isp4if_split_addr64(rb_config->base_mc_addr, &lo, &hi);
+
+	isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif),
+		    rb_config->reg_rptr, 0x0);
+	isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif),
+		    rb_config->reg_wptr, 0x0);
+	isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif),
+		    rb_config->reg_base_lo, lo);
+	isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif),
+		    rb_config->reg_base_hi, hi);
+	isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif),
+		    rb_config->reg_size, rb_config->val_size);
+}
+
+static int isp4if_fw_init(struct isp4_interface *ispif)
+{
+	struct isp4if_rb_config *rb_config;
+	u32 offset;
+	int i;
+
+	/* initialize CMD_RB streams */
+	for (i = 0; i < ISP4IF_STREAM_ID_MAX; i++) {
+		rb_config = (isp4if_cmd_rb_config + i);
+		offset = ispif->aligned_rb_chunk_size *
+			 (rb_config->index + ispif->cmd_rb_base_index);
+
+		rb_config->val_size = ISP4IF_FW_CMD_BUF_SIZE;
+		rb_config->base_sys_addr =
+			(u8 *)ispif->fw_cmd_resp_buf->sys_addr + offset;
+		rb_config->base_mc_addr =
+			ispif->fw_cmd_resp_buf->gpu_mc_addr + offset;
+
+		isp4if_init_rb_config(ispif, rb_config);
+	}
+
+	/* initialize RESP_RB streams */
+	for (i = 0; i < ISP4IF_STREAM_ID_MAX; i++) {
+		rb_config = (isp4if_resp_rb_config + i);
+		offset = ispif->aligned_rb_chunk_size *
+			 (rb_config->index + ispif->resp_rb_base_index);
+
+		rb_config->val_size = ISP4IF_FW_CMD_BUF_SIZE;
+		rb_config->base_sys_addr =
+			(u8 *)ispif->fw_cmd_resp_buf->sys_addr + offset;
+		rb_config->base_mc_addr =
+			ispif->fw_cmd_resp_buf->gpu_mc_addr + offset;
+
+		isp4if_init_rb_config(ispif, rb_config);
+	}
+
+	/* initialize LOG_RB stream */
+	rb_config = &isp4if_log_rb_config;
+	rb_config->val_size = ISP4IF_FW_LOG_RINGBUF_SIZE;
+	rb_config->base_mc_addr = ispif->fw_log_buf->gpu_mc_addr;
+	rb_config->base_sys_addr = ispif->fw_log_buf->sys_addr;
+
+	isp4if_init_rb_config(ispif, rb_config);
+
+	return 0;
+}
+
+static int isp4if_wait_fw_ready(struct isp4_interface *ispif,
+				u32 isp_status_addr)
+{
+	struct device *dev = ispif->dev;
+	u32 fw_ready_timeout;
+	u32 timeout_ms = 100;
+	u32 interval_ms = 1;
+	u32 timeout = 0;
+	u32 reg_val;
+
+	fw_ready_timeout = timeout_ms / interval_ms;
+
+	/* wait for FW initialize done! */
+	while (timeout < fw_ready_timeout) {
+		reg_val = isp4hw_rreg(GET_ISP4IF_REG_BASE(ispif),
+				      isp_status_addr);
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 681

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+		if (reg_val & ISP_STATUS__CCPU_REPORT_MASK)
+			return 0;
+
+		msleep(interval_ms);
+		timeout++;
+	}

read_poll_timeout()?

Sure, will modify in the next patch

Quoted 175 lines
Quoted 175 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +175 -0
+
+	dev_err(dev, "ISP CCPU FW boot failed\n");
+
+	return -ETIME;
+}
+
+static void isp4if_enable_ccpu(struct isp4_interface *ispif)
+{
+	u32 reg_val;
+
+	reg_val = isp4hw_rreg(GET_ISP4IF_REG_BASE(ispif), ISP_SOFT_RESET);
+	reg_val &= (~ISP_SOFT_RESET__CCPU_SOFT_RESET_MASK);
+	isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif), ISP_SOFT_RESET, reg_val);
+
+	usleep_range(100, 150);
+
+	reg_val = isp4hw_rreg(GET_ISP4IF_REG_BASE(ispif), ISP_CCPU_CNTL);
+	reg_val &= (~ISP_CCPU_CNTL__CCPU_HOST_SOFT_RST_MASK);
+	isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif), ISP_CCPU_CNTL, reg_val);
+}
+
+static void isp4if_disable_ccpu(struct isp4_interface *ispif)
+{
+	u32 reg_val;
+
+	reg_val = isp4hw_rreg(GET_ISP4IF_REG_BASE(ispif), ISP_CCPU_CNTL);
+	reg_val |= ISP_CCPU_CNTL__CCPU_HOST_SOFT_RST_MASK;
+	isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif), ISP_CCPU_CNTL, reg_val);
+
+	usleep_range(100, 150);
+
+	reg_val = isp4hw_rreg(GET_ISP4IF_REG_BASE(ispif), ISP_SOFT_RESET);
+	reg_val |= ISP_SOFT_RESET__CCPU_SOFT_RESET_MASK;
+	isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif), ISP_SOFT_RESET, reg_val);
+}
+
+static int isp4if_fw_boot(struct isp4_interface *ispif)
+{
+	struct device *dev = ispif->dev;
+
+	if (ispif->status != ISP4IF_STATUS_PWR_ON) {
+		dev_err(dev, "invalid isp power status %d\n", ispif->status);
+		return -EINVAL;
+	}
+
+	isp4if_disable_ccpu(ispif);
+
+	isp4if_fw_init(ispif);
+
+	/* clear ccpu status */
+	isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif), ISP_STATUS, 0x0);
+
+	isp4if_enable_ccpu(ispif);
+
+	if (isp4if_wait_fw_ready(ispif, ISP_STATUS)) {
+		isp4if_disable_ccpu(ispif);
+		return -EINVAL;
+	}
+
+	/* enable interrupts */
+	isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif), ISP_SYS_INT0_EN,
+		    ISP4IF_FW_RESP_RB_IRQ_EN_MASK);
+
+	ispif->status = ISP4IF_STATUS_FW_RUNNING;
+
+	dev_dbg(dev, "ISP CCPU FW boot success\n");
+
+	return 0;
+}
+
+int isp4if_f2h_resp(struct isp4_interface *ispif,
+		    enum isp4if_stream_id stream,
+		    void *resp)
+{
+	struct isp4fw_resp *response = resp;
+	struct isp4if_rb_config *rb_config;
+	struct device *dev = ispif->dev;
+	u32 rd_ptr_dbg;
+	u32 wr_ptr_dbg;
+	void *mem_sys;
+	u64 mem_addr;
+	u32 checksum;
+	u32 rd_ptr;
+	u32 wr_ptr;
+	u32 rreg;
+	u32 wreg;
+	u32 len;
+
+	rb_config = &isp4if_resp_rb_config[stream];
+	rreg = rb_config->reg_rptr;
+	wreg = rb_config->reg_wptr;
+	mem_sys = rb_config->base_sys_addr;
+	mem_addr = rb_config->base_mc_addr;
+	len = rb_config->val_size;
+
+	rd_ptr = isp4hw_rreg(GET_ISP4IF_REG_BASE(ispif), rreg);
+	wr_ptr = isp4hw_rreg(GET_ISP4IF_REG_BASE(ispif), wreg);
+	rd_ptr_dbg = rd_ptr;
+	wr_ptr_dbg = wr_ptr;
+
+	if (rd_ptr > len) {
+		dev_err(dev, "fail (%u),rd_ptr %u(should<=%u),wr_ptr %u\n",
+			stream, rd_ptr, len, wr_ptr);
+		return -EINVAL;
+	}
+
+	if (wr_ptr > len) {
+		dev_err(dev, "fail (%u),wr_ptr %u(should<=%u), rd_ptr %u\n",
+			stream, wr_ptr, len, rd_ptr);
+		return -EINVAL;
+	}
+
+	if (rd_ptr < wr_ptr) {
+		if ((wr_ptr - rd_ptr) >= (sizeof(struct isp4fw_resp))) {
+			memcpy((u8 *)response, (u8 *)mem_sys + rd_ptr,
+			       sizeof(struct isp4fw_resp));
+
+			rd_ptr += sizeof(struct isp4fw_resp);
+			if (rd_ptr < len) {
+				isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif),
+					    rreg, rd_ptr);
+			} else {
+				dev_err(dev, "(%u),rd %u(should<=%u),wr %u\n",
+					stream, rd_ptr, len, wr_ptr);
+				return -EINVAL;
+			}
+
+		} else {
+			dev_err(dev, "sth wrong with wptr and rptr\n");
+			return -EINVAL;
+		}
+	} else if (rd_ptr > wr_ptr) {
+		u32 size;
+		u8 *dst;
+
+		dst = (u8 *)response;
+
+		size = len - rd_ptr;
+		if (size > sizeof(struct isp4fw_resp)) {
+			mem_addr += rd_ptr;
+			memcpy((u8 *)response,
+			       (u8 *)(mem_sys) + rd_ptr,
+			       sizeof(struct isp4fw_resp));
+			rd_ptr += sizeof(struct isp4fw_resp);
+			if (rd_ptr < len) {
+				isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif),
+					    rreg, rd_ptr);
+			} else {
+				dev_err(dev, "(%u),rd %u(should<=%u),wr %u\n",
+					stream, rd_ptr, len, wr_ptr);
+				return -EINVAL;
+			}
+
+		} else {
+			if ((size + wr_ptr) < (sizeof(struct isp4fw_resp))) {
+				dev_err(dev, "sth wrong with wptr and rptr1\n");
+				return -EINVAL;
+			}
+
+			memcpy(dst, (u8 *)(mem_sys) + rd_ptr, size);
+
+			dst += size;
+			size = sizeof(struct isp4fw_resp) - size;
+			if (size)
+				memcpy(dst, (u8 *)(mem_sys), size);
+			rd_ptr = size;
+			if (rd_ptr < len) {
+				isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif),
+					    rreg, rd_ptr);
+			} else {
+				dev_err(dev, "(%u),rd %u(should<=%u),wr %u\n",
+					stream, rd_ptr, len, wr_ptr);
+				return -EINVAL;
+			}
+		}
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 862

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+	} else {
+		return -ETIME;
+	}
+
+	checksum = isp4if_compute_check_sum((u8 *)response,
+					    (sizeof(struct isp4fw_resp) - 4));

Redundant parentheses again.

Sure, will modify in the next patch

Quoted 4 lines
Quoted 4 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +4 -0
+
+	if (checksum != response->resp_check_sum) {
+		dev_err(dev, "resp checksum 0x%x,should 0x%x,rptr %u,wptr %u\n",
+			checksum, response->resp_check_sum,
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 872

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+			rd_ptr_dbg, wr_ptr_dbg);
+
+		dev_err(dev, "(%u), seqNo %u, resp_id (0x%x)\n",
+			stream,
+			response->resp_seq_num,
+			response->resp_id);

Fits on fewer lines.

Sure, will modify in the next patch

Quoted 5 lines
Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +5 -0
+
+		return -EINVAL;
+	}
+
+	return 0;
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 883

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+}
+
+int isp4if_send_command(struct isp4_interface *ispif,
+			u32 cmd_id,
+			void *package,
+			u32 package_size)

Ditto.

Ditto

Quoted 7 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 887

Quoted 4 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +4 -0
+{
+	return isp4if_send_fw_cmd(ispif,
+				  cmd_id, package,
+				  package_size, NULL, NULL, NULL);

Ditto.

Ditto

Quoted 10 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 894

Quoted 7 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +7 -0
+}
+
+int isp4if_send_command_sync(struct isp4_interface *ispif,
+			     u32 cmd_id,
+			     void *package,
+			     u32 package_size,
+			     u32 timeout)

Ditto.

Ditto

Quoted 12 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 903

Quoted 9 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +9 -0
+{
+	struct device *dev = ispif->dev;
+	DECLARE_WAIT_QUEUE_HEAD(cmd_wq);
+	u32 wq_cond = 0;
+	int ret;
+	u32 seq;
+
+	ret = isp4if_send_fw_cmd(ispif,
+				 cmd_id, package,

Ditto.

Ditto

Quoted 13 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 913

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +10 -0
+				 package_size, &cmd_wq, &wq_cond, &seq);
+
+	if (ret) {
+		dev_err(dev, "send fw cmd fail %d\n", ret);
+		return ret;
+	}
+
+	ret = wait_event_timeout(cmd_wq, wq_cond != 0,
+				 msecs_to_jiffies(timeout));
+

Extra newline.

Quoted 4 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 914

Quoted 1 line
drivers/media/platform/amd/isp4/isp4_interface.c new file +1 -0
+	/* timeout occurred */

Redundant comment.

Sure, will modify in the next patch

Quoted 14 lines
Quoted 14 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +14 -0
+	if (ret == 0) {
+		struct isp4if_cmd_element *ele;
+
+		ele = isp4if_rm_cmd_from_cmdq(ispif, seq, cmd_id);
+		kfree(ele);
+		return -ETIMEDOUT;
+	}
+
+	return 0;
+}
+
+void isp4if_clear_bufq(struct isp4_interface *ispif)
+{
+	struct isp4if_img_buf_node *buf_node = NULL;
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 934

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+	struct isp4if_img_buf_node *tmp_node = NULL;
+
+	guard(mutex)(&ispif->bufq_mutex);
+
+	list_for_each_entry_safe(buf_node, tmp_node, &ispif->bufq,
+				 node) {

Please rewrap.

Sure, will modify in the next patch

Quoted 26 lines
Quoted 26 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +26 -0
+		list_del(&buf_node->node);
+		kfree(buf_node);
+	}
+}
+
+void isp4if_dealloc_buffer_node(struct isp4if_img_buf_node *buf_node)
+{
+	kfree(buf_node);
+}
+
+struct isp4if_img_buf_node *
+isp4if_alloc_buffer_node(struct isp4if_img_buf_info *buf_info)
+{
+	struct isp4if_img_buf_node *node = NULL;
+
+	node = kmalloc(sizeof(*node), GFP_KERNEL);
+	if (node)
+		node->buf_info = *buf_info;
+
+	return node;
+};
+
+struct isp4if_img_buf_node *
+isp4if_dequeue_buffer(struct isp4_interface *ispif)
+{
+	struct isp4if_img_buf_node *buf_node = NULL;
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 966

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+
+	guard(mutex)(&ispif->bufq_mutex);
+
+	buf_node = list_first_entry_or_null(&ispif->bufq,
+					    typeof(*buf_node),
+					    node);

Ditto.

Sure, will modify in the next patch

Quoted 41 lines
Quoted 41 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +41 -0
+	if (buf_node)
+		list_del(&buf_node->node);
+
+	return buf_node;
+}
+
+int isp4if_queue_buffer(struct isp4_interface *ispif,
+			struct isp4if_img_buf_node *buf_node)
+{
+	int ret;
+
+	ret = isp4if_send_buffer(ispif, &buf_node->buf_info);
+	if (ret)
+		return ret;
+
+	guard(mutex)(&ispif->bufq_mutex);
+
+	list_add_tail(&buf_node->node, &ispif->bufq);
+
+	return 0;
+}
+
+int isp4if_stop(struct isp4_interface *ispif)
+{
+	isp4if_disable_ccpu(ispif);
+
+	isp4if_dealloc_fw_gpumem(ispif);
+
+	return 0;
+}
+
+int isp4if_start(struct isp4_interface *ispif)
+{
+	int ret;
+
+	ret = isp4if_alloc_fw_gpumem(ispif);
+	if (ret)
+		goto failed_gpumem_alloc;
+
+	ret = isp4if_fw_boot(ispif);
+	if (ret)
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 1013

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+		goto failed_fw_boot;
+
+	return 0;
+
+failed_gpumem_alloc:
+	return -ENOMEM;

No label needed for this.

Sure, will modify in the next patch

Quoted 159 lines
Quoted 146 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +39 -0
+
+failed_fw_boot:
+	isp4if_dealloc_fw_gpumem(ispif);
+	return ret;
+}
+
+int isp4if_deinit(struct isp4_interface *ispif)
+{
+	isp4if_clear_cmdq(ispif);
+
+	isp4if_clear_bufq(ispif);
+
+	mutex_destroy(&ispif->cmdq_mutex);
+	mutex_destroy(&ispif->bufq_mutex);
+	mutex_destroy(&ispif->isp4if_mutex);
+
+	return 0;
+}
+
+int isp4if_init(struct isp4_interface *ispif, struct device *dev,
+		void *amdgpu_dev, void __iomem *isp_mmip)
+{
+	ispif->dev = dev;
+	ispif->adev = amdgpu_dev;
+	ispif->mmio = isp_mmip;
+
+	ispif->cmd_rb_base_index = 0;
+	ispif->resp_rb_base_index = ISP4IF_RESP_CHAN_TO_RB_OFFSET - 1;
+	ispif->aligned_rb_chunk_size = ISP4IF_RB_PMBMAP_MEM_CHUNK & 0xffffffc0;
+
+	mutex_init(&ispif->cmdq_mutex); /* used for cmdq access */
+	mutex_init(&ispif->bufq_mutex); /* used for bufq access */
+	mutex_init(&ispif->isp4if_mutex); /* used for commands sent to ispfw */
+
+	INIT_LIST_HEAD(&ispif->cmdq);
+	INIT_LIST_HEAD(&ispif->bufq);
+
+	return 0;
+}
drivers/media/platform/amd/isp4/isp4_interface.h new file +101 -0
new file mode 100644
index 000000000000..b2ca147b78b6
@@ -0,0 +1,164 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_INTERFACE_
+#define _ISP4_INTERFACE_
+
+#define ISP4IF_RB_MAX (25)
+#define ISP4IF_RESP_CHAN_TO_RB_OFFSET (9)
+#define ISP4IF_RB_PMBMAP_MEM_SIZE (16 * 1024 * 1024 - 1)
+#define ISP4IF_RB_PMBMAP_MEM_CHUNK (ISP4IF_RB_PMBMAP_MEM_SIZE \
+	/ (ISP4IF_RB_MAX - 1))
+#define ISP4IF_ISP_MC_ADDR_ALIGN (1024 * 32)
+#define ISP4IF_HOST2FW_COMMAND_SIZE (sizeof(struct isp4fw_cmd))
+#define ISP4IF_FW_CMD_BUF_COUNT 4
+#define ISP4IF_FW_RESP_BUF_COUNT 4
+#define ISP4IF_MAX_NUM_HOST2FW_COMMAND (40)
+#define ISP4IF_FW_CMD_BUF_SIZE (ISP4IF_MAX_NUM_HOST2FW_COMMAND \
+	* ISP4IF_HOST2FW_COMMAND_SIZE)
+#define ISP4IF_MAX_SLEEP_COUNT (10)
+#define ISP4IF_MAX_SLEEP_TIME (33)
+
+#define ISP4IF_META_INFO_BUF_SIZE ALIGN(sizeof(struct isp4fw_meta_info), 0x8000)
+#define ISP4IF_MAX_STREAM_META_BUF_COUNT 6
+
+#define ISP4IF_FW_LOG_RINGBUF_SIZE (2 * 1024 * 1024)
+
+#define ISP4IF_MAX_CMD_RESPONSE_BUF_SIZE (4 * 1024)
+
+#define GET_ISP4IF_REG_BASE(ispif) (((ispif))->mmio)
+
+enum isp4if_stream_id {
+	ISP4IF_STREAM_ID_GLOBAL = 0,
+	ISP4IF_STREAM_ID_1 = 1,
+	ISP4IF_STREAM_ID_MAX = 4
+};
+
+enum isp4if_status {
+	ISP4IF_STATUS_PWR_OFF,
+	ISP4IF_STATUS_PWR_ON,
+	ISP4IF_STATUS_FW_RUNNING,
+	ISP4IF_FSM_STATUS_MAX
+};
+
+struct isp4if_gpu_mem_info {
+	u32	mem_domain;
+	u64	mem_size;
+	u32	mem_align;
+	u64	gpu_mc_addr;
+	void	*sys_addr;
+	void	*mem_handle;
+};
+
+struct isp4if_img_buf_info {
+	struct {
+		void *sys_addr;
+		u64 mc_addr;
+		u32 len;
+	} planes[3];
+};
+
+struct isp4if_img_buf_node {
+	struct list_head node;
+	struct isp4if_img_buf_info buf_info;
+};
+
+struct isp4if_cmd_element {
+	struct list_head list;
+	u32 seq_num;
+	u32 cmd_id;
+	enum isp4if_stream_id stream;
+	u64 mc_addr;
+	wait_queue_head_t *wq;
+	u32 *wq_cond;
+	struct isp4if_gpu_mem_info *gpu_pkg;
+};
+
+struct isp4_interface {
+	struct amdgpu_device *adev;
+
+	struct device *dev;
+	void __iomem *mmio;
+
+	struct mutex cmdq_mutex; /* used for cmdq access */
+	struct mutex bufq_mutex; /* used for bufq access */
+	struct mutex isp4if_mutex; /* used to send fw cmd and read fw log */
+
+	struct list_head cmdq; /* commands sent to fw */
+	struct list_head bufq; /* buffers sent to fw */
+
+	enum isp4if_status status;
+	u32 host2fw_seq_num;
+
+	/* FW ring buffer configs */
+	u32 cmd_rb_base_index;
+	u32 resp_rb_base_index;
+	u32 aligned_rb_chunk_size;
+
+	/* ISP fw buffers */
+	struct isp4if_gpu_mem_info *fw_log_buf;

drivers/media/platform/amd/isp4/isp4_interface.h line 111

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_interface.h new file +10 -0
+	struct isp4if_gpu_mem_info *fw_cmd_resp_buf;
+	struct isp4if_gpu_mem_info *fw_mem_pool;
+	struct isp4if_gpu_mem_info *
+		metainfo_buf_pool[ISP4IF_MAX_STREAM_META_BUF_COUNT];
+};
+
+static inline void isp4if_split_addr64(u64 addr, u32 *lo, u32 *hi)
+{
+	if (lo)
+		*lo = (u32)(addr & 0xffffffff);

Redundant and and cast, too.

Quoted 5 lines

drivers/media/platform/amd/isp4/isp4_interface.h line 113

Quoted 2 lines
drivers/media/platform/amd/isp4/isp4_interface.h new file +2 -0
+	if (hi)
+		*hi = (u32)(addr >> 32);

Redundant cast.

Sure, will modify in the next patch

Quoted 52 lines
Quoted 51 lines
drivers/media/platform/amd/isp4/isp4_interface.h new file +51 -0
+}
+
+static inline u64 isp4if_join_addr64(u32 lo, u32 hi)
+{
+	return (((u64)hi) << 32) | (u64)lo;
+}
+
+int isp4if_f2h_resp(struct isp4_interface *ispif,
+		    enum isp4if_stream_id stream,
+		    void *response);
+
+int isp4if_send_command(struct isp4_interface *ispif,
+			u32 cmd_id,
+			void *package,
+			u32 package_size);
+
+int isp4if_send_command_sync(struct isp4_interface *ispif,
+			     u32 cmd_id,
+			     void *package,
+			     u32 package_size,
+			     u32 timeout);
+
+struct isp4if_cmd_element *
+isp4if_rm_cmd_from_cmdq(struct isp4_interface *ispif,
+			u32 seq_num,
+			u32 cmd_id);
+
+void isp4if_clear_cmdq(struct isp4_interface *ispif);
+
+void isp4if_clear_bufq(struct isp4_interface *ispif);
+
+void isp4if_dealloc_buffer_node(struct isp4if_img_buf_node *buf_node);
+
+struct isp4if_img_buf_node *
+isp4if_alloc_buffer_node(struct isp4if_img_buf_info *buf_info);
+
+struct isp4if_img_buf_node *isp4if_dequeue_buffer(struct isp4_interface *ispif);
+
+int isp4if_queue_buffer(struct isp4_interface *ispif,
+			struct isp4if_img_buf_node *buf_node);
+
+int isp4if_stop(struct isp4_interface *ispif);
+
+int isp4if_start(struct isp4_interface *ispif);
+
+int isp4if_deinit(struct isp4_interface *ispif);
+
+int isp4if_init(struct isp4_interface *ispif, struct device *dev,
+		void *amdgpu_dev, void __iomem *isp_mmip);
+
+#endif
maintainer
#29 lore raw

Re: [PATCH v2 4/8] media: platform: amd: Add isp4 fw and hw interface

Hi Bin,

Quoted 19 lines · Bin

Many thanks Askari Ailus for your careful review

Quoted 16 lines · Sakari Ailus

Hi Bin,

Quoted 12 lines · Bin Du

ISP firmware controls ISP HW pipeline using dedicated embedded processor called ccpu.
The communication between ISP FW and driver is using commands and response messages sent through the ring buffer. Command buffers support either global setting that is not specific to the steam and support stream specific parameters. Response buffers contains ISP FW notification information such as frame buffer done and command done. IRQ is used for receiving response buffer from ISP firmware, which is handled in the main isp4 media device. ISP ccpu is booted up through the firmware loading helper function prior to stream start.
Memory used for command buffer and response buffer needs to be allocated from amdgpu buffer manager because isp4 is a child device of amdgpu.

Quoted 4 lines
Quoted 2 lines

Please rewrap this, some lines above are quite short.

Thanks, the line after the short line is supposed to be a new paragraph? Should we put all the description in one paragraph?

One or more paragraphs work fine, but a new paragraph is separated from the previous one by another newline.

...

Quoted 17 lines
Quoted 17 lines
Quoted 4 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +4 -0
+	void *cpu_ptr;
+	u64 gpu_addr;
+	u32 ret;
+

drivers/media/platform/amd/isp4/isp4_interface.c line 149

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +10 -0
+	dev = ispif->dev;
+
+	if (!mem_size)
+		return NULL;
+
+	mem_info = kzalloc(sizeof(*mem_info), GFP_KERNEL);
+	if (!mem_info)
+		return NULL;
+
+	adev = (struct amdgpu_device *)ispif->adev;

Why the cast?

Quoted 4 lines
Quoted 2 lines

adev isn't a great name here as it's usually used for struct acpi_devices.

In the next patch, will use new helper function for this and will no longer use amdgpu_device

Use correct types when you can; either way this doesn't seem to be changed by the further patches in the set.

...

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+static int isp4if_gpu_mem_free(struct isp4_interface *ispif,
+			       struct isp4if_gpu_mem_info *mem_info)
+{
+	struct device *dev = ispif->dev;
+	struct amdgpu_bo *bo;
+
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 186

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+	if (!mem_info) {
+		dev_err(dev, "invalid mem_info\n");
+		return -EINVAL;
+	}
+
+	bo = (struct amdgpu_bo *)mem_info->mem_handle;

Why do you need to cast here?

Quoted 2 lines

In the next patch, will use new helper function for this and will no longer use amdgpu_bo

Not quite, on top of this patch number 6 adds more of the same.

...

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +5 -0
+static struct isp4if_cmd_element *
+isp4if_append_cmd_2_cmdq(struct isp4_interface *ispif,
+			 struct isp4if_cmd_element *cmd_ele)
+{
+	struct isp4if_cmd_element *copy_command = NULL;
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 343

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+
+	copy_command = kmalloc(sizeof(*copy_command), GFP_KERNEL);
+	if (!copy_command)
+		return NULL;
+
+	memcpy(copy_command, cmd_ele, sizeof(*copy_command));

kmemdup()?

Quoted 1 line

Kmemdup is to allocate memory and copy, can't be used here.

Isn't that what you're doing above?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +7 -0
+
+	guard(mutex)(&ispif->cmdq_mutex);
+
+	list_add_tail(&copy_command->list, &ispif->cmdq);
+
+	return copy_command;
+}
Signature · 3 lines
Regards,

Sakari Ailus
#30 lore raw

Re: [PATCH v2 4/8] media: platform: amd: Add isp4 fw and hw interface

Quoted 18 lines · Sakari Ailus
Quoted 17 lines · Bin
Quoted 16 lines · Sakari Ailus
Quoted 4 lines · Bin Du

ISP firmware controls ISP HW pipeline using dedicated embedded processor called ccpu.
The communication between ISP FW and driver is using commands and response messages sent through the ring buffer. Command buffers support

Quoted 8 lines

either global setting that is not specific to the steam and support stream specific parameters. Response buffers contains ISP FW notification information such as frame buffer done and command done. IRQ is used for receiving response buffer from ISP firmware, which is handled in the main isp4 media device. ISP ccpu is booted up through the firmware loading helper function prior to stream start.
Memory used for command buffer and response buffer needs to be allocated from amdgpu buffer manager because isp4 is a child device of amdgpu.

Please rewrap this, some lines above are quite short.

Quoted 7 lines
Quoted 2 lines

Thanks, the line after the short line is supposed to be a new paragraph? Should we put all the description in one paragraph?

One or more paragraphs work fine, but a new paragraph is separated from the previous one by another newline.

...

Paragraphs are defined as a block of text that convey one idea. They should be visually separated by a space. As we can't have fractional line spacing in plain text, paragraphs need to be separated by a blank line. This is a typography rule that maximizes readability. There should be no line break between sentences in a single paragraph.

Whether you write commit messages, formal documentation or comments in code, typography is important to give the best experience to readers. After all, a block of text that wouldn't focus on the readers would have no reason to exist.

Now compare the above with

Paragraphs are defined as a block of text that convey one idea. They should be visually separated by a space.
As we can't have fractional line spacing in plain text, paragraphs need to be separated by a blank line.
This is a typography rule that maximizes readability. There should be no line break between sentences in a single paragraph. Whether you write commit messages, formal documentation or comments in code, typography is important to give the best experience to readers.
After all, a block of text that wouldn't focus on the readers would have no reason to exist.

Quoted 74 lines
Quoted 17 lines
Quoted 17 lines
Quoted 4 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +4 -0
+	void *cpu_ptr;
+	u64 gpu_addr;
+	u32 ret;
+

drivers/media/platform/amd/isp4/isp4_interface.c line 149

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +10 -0
+	dev = ispif->dev;
+
+	if (!mem_size)
+		return NULL;
+
+	mem_info = kzalloc(sizeof(*mem_info), GFP_KERNEL);
+	if (!mem_info)
+		return NULL;
+
+	adev = (struct amdgpu_device *)ispif->adev;

Why the cast?

Quoted 4 lines
Quoted 2 lines

adev isn't a great name here as it's usually used for struct acpi_devices.

In the next patch, will use new helper function for this and will no longer use amdgpu_device

Use correct types when you can; either way this doesn't seem to be changed by the further patches in the set.

...

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+static int isp4if_gpu_mem_free(struct isp4_interface *ispif,
+			       struct isp4if_gpu_mem_info *mem_info)
+{
+	struct device *dev = ispif->dev;
+	struct amdgpu_bo *bo;
+
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 186

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+	if (!mem_info) {
+		dev_err(dev, "invalid mem_info\n");
+		return -EINVAL;
+	}
+
+	bo = (struct amdgpu_bo *)mem_info->mem_handle;

Why do you need to cast here?

Quoted 2 lines

In the next patch, will use new helper function for this and will no longer use amdgpu_bo

Not quite, on top of this patch number 6 adds more of the same.

...

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +5 -0
+static struct isp4if_cmd_element *
+isp4if_append_cmd_2_cmdq(struct isp4_interface *ispif,
+			 struct isp4if_cmd_element *cmd_ele)
+{
+	struct isp4if_cmd_element *copy_command = NULL;
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 343

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+
+	copy_command = kmalloc(sizeof(*copy_command), GFP_KERNEL);
+	if (!copy_command)
+		return NULL;
+
+	memcpy(copy_command, cmd_ele, sizeof(*copy_command));

kmemdup()?

Quoted 1 line

Kmemdup is to allocate memory and copy, can't be used here.

Isn't that what you're doing above?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +7 -0
+
+	guard(mutex)(&ispif->cmdq_mutex);
+
+	list_add_tail(&copy_command->list, &ispif->cmdq);
+
+	return copy_command;
+}
Signature · 3 lines
Regards,

Laurent Pinchart
author maintainer
#31 lore raw

Re: [PATCH v2 4/8] media: platform: amd: Add isp4 fw and hw interface

Many thanks Laurent Pinchart for the comments.

Quoted 42 lines · Laurent Pinchart
Quoted 18 lines · Sakari Ailus
Quoted 17 lines · Bin
Quoted 16 lines · Sakari Ailus
Quoted 4 lines · Bin Du

ISP firmware controls ISP HW pipeline using dedicated embedded processor called ccpu.
The communication between ISP FW and driver is using commands and response messages sent through the ring buffer. Command buffers support

Quoted 8 lines

either global setting that is not specific to the steam and support stream specific parameters. Response buffers contains ISP FW notification information such as frame buffer done and command done. IRQ is used for receiving response buffer from ISP firmware, which is handled in the main isp4 media device. ISP ccpu is booted up through the firmware loading helper function prior to stream start.
Memory used for command buffer and response buffer needs to be allocated from amdgpu buffer manager because isp4 is a child device of amdgpu.

Please rewrap this, some lines above are quite short.

Quoted 7 lines
Quoted 2 lines

Thanks, the line after the short line is supposed to be a new paragraph? Should we put all the description in one paragraph?

One or more paragraphs work fine, but a new paragraph is separated from the previous one by another newline.

...

Paragraphs are defined as a block of text that convey one idea. They should be visually separated by a space. As we can't have fractional line spacing in plain text, paragraphs need to be separated by a blank line. This is a typography rule that maximizes readability. There should be no line break between sentences in a single paragraph.

Whether you write commit messages, formal documentation or comments in code, typography is important to give the best experience to readers. After all, a block of text that wouldn't focus on the readers would have no reason to exist.

Now compare the above with

Quoted 11 lines

Paragraphs are defined as a block of text that convey one idea. They should be visually separated by a space.
As we can't have fractional line spacing in plain text, paragraphs need to be separated by a blank line.
This is a typography rule that maximizes readability. There should be no line break between sentences in a single paragraph. Whether you write commit messages, formal documentation or comments in code, typography is important to give the best experience to readers.
After all, a block of text that wouldn't focus on the readers would have no reason to exist.

Really appreciate the detailed guide, will follow it. May I summarize like this? 1 Separate paragraphs by a blank line. 2 Don't add line break between sentences in a single paragraph, an exception to this is commit message, because of the 75-character patch check limit, line break can be added, but it should at the 75-character limit boundary

Quoted 75 lines
Quoted 74 lines
Quoted 17 lines
Quoted 17 lines
Quoted 4 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +4 -0
+	void *cpu_ptr;
+	u64 gpu_addr;
+	u32 ret;
+

drivers/media/platform/amd/isp4/isp4_interface.c line 149

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +10 -0
+	dev = ispif->dev;
+
+	if (!mem_size)
+		return NULL;
+
+	mem_info = kzalloc(sizeof(*mem_info), GFP_KERNEL);
+	if (!mem_info)
+		return NULL;
+
+	adev = (struct amdgpu_device *)ispif->adev;

Why the cast?

Quoted 4 lines
Quoted 2 lines

adev isn't a great name here as it's usually used for struct acpi_devices.

In the next patch, will use new helper function for this and will no longer use amdgpu_device

Use correct types when you can; either way this doesn't seem to be changed by the further patches in the set.

...

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+static int isp4if_gpu_mem_free(struct isp4_interface *ispif,
+			       struct isp4if_gpu_mem_info *mem_info)
+{
+	struct device *dev = ispif->dev;
+	struct amdgpu_bo *bo;
+
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 186

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+	if (!mem_info) {
+		dev_err(dev, "invalid mem_info\n");
+		return -EINVAL;
+	}
+
+	bo = (struct amdgpu_bo *)mem_info->mem_handle;

Why do you need to cast here?

Quoted 2 lines

In the next patch, will use new helper function for this and will no longer use amdgpu_bo

Not quite, on top of this patch number 6 adds more of the same.

...

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +5 -0
+static struct isp4if_cmd_element *
+isp4if_append_cmd_2_cmdq(struct isp4_interface *ispif,
+			 struct isp4if_cmd_element *cmd_ele)
+{
+	struct isp4if_cmd_element *copy_command = NULL;
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 343

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+
+	copy_command = kmalloc(sizeof(*copy_command), GFP_KERNEL);
+	if (!copy_command)
+		return NULL;
+
+	memcpy(copy_command, cmd_ele, sizeof(*copy_command));

kmemdup()?

Quoted 1 line

Kmemdup is to allocate memory and copy, can't be used here.

Isn't that what you're doing above?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +7 -0
+
+	guard(mutex)(&ispif->cmdq_mutex);
+
+	list_add_tail(&copy_command->list, &ispif->cmdq);
+
+	return copy_command;
+}
#32 lore raw

Re: [PATCH v2 4/8] media: platform: amd: Add isp4 fw and hw interface

Quoted 56 lines · Bin

Many thanks Laurent Pinchart for the comments.

Quoted 42 lines · Laurent Pinchart
Quoted 18 lines · Sakari Ailus
Quoted 17 lines · Bin
Quoted 16 lines · Sakari Ailus
Quoted 4 lines · Bin Du

ISP firmware controls ISP HW pipeline using dedicated embedded processor called ccpu.
The communication between ISP FW and driver is using commands and response messages sent through the ring buffer. Command buffers support

Quoted 8 lines

either global setting that is not specific to the steam and support stream specific parameters. Response buffers contains ISP FW notification information such as frame buffer done and command done. IRQ is used for receiving response buffer from ISP firmware, which is handled in the main isp4 media device. ISP ccpu is booted up through the firmware loading helper function prior to stream start.
Memory used for command buffer and response buffer needs to be allocated from amdgpu buffer manager because isp4 is a child device of amdgpu.

Please rewrap this, some lines above are quite short.

Quoted 7 lines
Quoted 2 lines

Thanks, the line after the short line is supposed to be a new paragraph? Should we put all the description in one paragraph?

One or more paragraphs work fine, but a new paragraph is separated from the previous one by another newline.

...

Paragraphs are defined as a block of text that convey one idea. They should be visually separated by a space. As we can't have fractional line spacing in plain text, paragraphs need to be separated by a blank line. This is a typography rule that maximizes readability. There should be no line break between sentences in a single paragraph.

Whether you write commit messages, formal documentation or comments in code, typography is important to give the best experience to readers. After all, a block of text that wouldn't focus on the readers would have no reason to exist.

Now compare the above with

Quoted 10 lines

Paragraphs are defined as a block of text that convey one idea. They should be visually separated by a space.
As we can't have fractional line spacing in plain text, paragraphs need to be separated by a blank line.
This is a typography rule that maximizes readability. There should be no line break between sentences in a single paragraph. Whether you write commit messages, formal documentation or comments in code, typography is important to give the best experience to readers.
After all, a block of text that wouldn't focus on the readers would have no reason to exist.

Quoted 5 lines

Really appreciate the detailed guide, will follow it. May I summarize like this? 1 Separate paragraphs by a blank line. 2 Don't add line break between sentences in a single paragraph, an exception to this is commit message, because of the 75-character patch check limit, line break can be added, but it should at the 75-character limit boundary

When I wrote "line break", I meant breaking the line after a sentence, before the 75 columns limits. Text blocks should always be wrapped (at 75 columns in commit messages, or 80 in kernel code). What you should avoid is line breaks not related to the columns limit.

This is fine:

This paragraph has a long sentence that does not hold on a single line of 72 characters and therefore needs to be wrapped. There is no line break otherwise, for instance between the first and second sentence, or within a sentence.

This is not right:

This paragraph has a long sentence that does not hold on a single line of 72 characters and therefore needs to be wrapped.
There is a line break between the first and second sentence, and also a line break in the second sentence, which are not fine.

Quoted 74 lines
Quoted 74 lines
Quoted 74 lines
Quoted 17 lines
Quoted 17 lines
Quoted 4 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +4 -0
+	void *cpu_ptr;
+	u64 gpu_addr;
+	u32 ret;
+

drivers/media/platform/amd/isp4/isp4_interface.c line 149

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +10 -0
+	dev = ispif->dev;
+
+	if (!mem_size)
+		return NULL;
+
+	mem_info = kzalloc(sizeof(*mem_info), GFP_KERNEL);
+	if (!mem_info)
+		return NULL;
+
+	adev = (struct amdgpu_device *)ispif->adev;

Why the cast?

Quoted 4 lines
Quoted 2 lines

adev isn't a great name here as it's usually used for struct acpi_devices.

In the next patch, will use new helper function for this and will no longer use amdgpu_device

Use correct types when you can; either way this doesn't seem to be changed by the further patches in the set.

...

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+static int isp4if_gpu_mem_free(struct isp4_interface *ispif,
+			       struct isp4if_gpu_mem_info *mem_info)
+{
+	struct device *dev = ispif->dev;
+	struct amdgpu_bo *bo;
+
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 186

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+	if (!mem_info) {
+		dev_err(dev, "invalid mem_info\n");
+		return -EINVAL;
+	}
+
+	bo = (struct amdgpu_bo *)mem_info->mem_handle;

Why do you need to cast here?

Quoted 2 lines

In the next patch, will use new helper function for this and will no longer use amdgpu_bo

Not quite, on top of this patch number 6 adds more of the same.

...

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +5 -0
+static struct isp4if_cmd_element *
+isp4if_append_cmd_2_cmdq(struct isp4_interface *ispif,
+			 struct isp4if_cmd_element *cmd_ele)
+{
+	struct isp4if_cmd_element *copy_command = NULL;
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 343

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+
+	copy_command = kmalloc(sizeof(*copy_command), GFP_KERNEL);
+	if (!copy_command)
+		return NULL;
+
+	memcpy(copy_command, cmd_ele, sizeof(*copy_command));

kmemdup()?

Quoted 1 line

Kmemdup is to allocate memory and copy, can't be used here.

Isn't that what you're doing above?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +7 -0
+
+	guard(mutex)(&ispif->cmdq_mutex);
+
+	list_add_tail(&copy_command->list, &ispif->cmdq);
+
+	return copy_command;
+}
Signature · 3 lines
Regards,

Laurent Pinchart
author maintainer
#33 lore raw

Re: [PATCH v2 4/8] media: platform: amd: Add isp4 fw and hw interface

Many thanks Laurent Pinchart

Quoted 75 lines · Laurent Pinchart
Quoted 56 lines · Bin

Many thanks Laurent Pinchart for the comments.

Quoted 42 lines · Laurent Pinchart
Quoted 18 lines · Sakari Ailus
Quoted 17 lines · Bin
Quoted 16 lines · Sakari Ailus
Quoted 4 lines · Bin Du

ISP firmware controls ISP HW pipeline using dedicated embedded processor called ccpu.
The communication between ISP FW and driver is using commands and response messages sent through the ring buffer. Command buffers support

Quoted 8 lines

either global setting that is not specific to the steam and support stream specific parameters. Response buffers contains ISP FW notification information such as frame buffer done and command done. IRQ is used for receiving response buffer from ISP firmware, which is handled in the main isp4 media device. ISP ccpu is booted up through the firmware loading helper function prior to stream start.
Memory used for command buffer and response buffer needs to be allocated from amdgpu buffer manager because isp4 is a child device of amdgpu.

Please rewrap this, some lines above are quite short.

Quoted 7 lines
Quoted 2 lines

Thanks, the line after the short line is supposed to be a new paragraph? Should we put all the description in one paragraph?

One or more paragraphs work fine, but a new paragraph is separated from the previous one by another newline.

...

Paragraphs are defined as a block of text that convey one idea. They should be visually separated by a space. As we can't have fractional line spacing in plain text, paragraphs need to be separated by a blank line. This is a typography rule that maximizes readability. There should be no line break between sentences in a single paragraph.

Whether you write commit messages, formal documentation or comments in code, typography is important to give the best experience to readers. After all, a block of text that wouldn't focus on the readers would have no reason to exist.

Now compare the above with

Quoted 10 lines

Paragraphs are defined as a block of text that convey one idea. They should be visually separated by a space.
As we can't have fractional line spacing in plain text, paragraphs need to be separated by a blank line.
This is a typography rule that maximizes readability. There should be no line break between sentences in a single paragraph. Whether you write commit messages, formal documentation or comments in code, typography is important to give the best experience to readers.
After all, a block of text that wouldn't focus on the readers would have no reason to exist.

Quoted 5 lines

Really appreciate the detailed guide, will follow it. May I summarize like this? 1 Separate paragraphs by a blank line. 2 Don't add line break between sentences in a single paragraph, an exception to this is commit message, because of the 75-character patch check limit, line break can be added, but it should at the 75-character limit boundary

When I wrote "line break", I meant breaking the line after a sentence, before the 75 columns limits. Text blocks should always be wrapped (at 75 columns in commit messages, or 80 in kernel code). What you should avoid is line breaks not related to the columns limit.

This is fine:

This paragraph has a long sentence that does not hold on a single line of 72 characters and therefore needs to be wrapped. There is no line break otherwise, for instance between the first and second sentence, or within a sentence.

Quoted 7 lines

This is not right:

This paragraph has a long sentence that does not hold on a single line of 72 characters and therefore needs to be wrapped.
There is a line break between the first and second sentence, and also a line break in the second sentence, which are not fine.

Really appreciate for your patient explanation and wonderful example, it's totally clear now.

Quoted 75 lines
Quoted 74 lines
Quoted 74 lines
Quoted 74 lines
Quoted 17 lines
Quoted 17 lines
Quoted 4 lines
(quoted diff) +4 -0
+	void *cpu_ptr;
+	u64 gpu_addr;
+	u32 ret;
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+	dev = ispif->dev;
+
+	if (!mem_size)
+		return NULL;
+
+	mem_info = kzalloc(sizeof(*mem_info), GFP_KERNEL);
+	if (!mem_info)
+		return NULL;
+
+	adev = (struct amdgpu_device *)ispif->adev;

Why the cast?

Quoted 4 lines
Quoted 2 lines

adev isn't a great name here as it's usually used for struct acpi_devices.

In the next patch, will use new helper function for this and will no longer use amdgpu_device

Use correct types when you can; either way this doesn't seem to be changed by the further patches in the set.

...

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
(quoted diff) +6 -0
+static int isp4if_gpu_mem_free(struct isp4_interface *ispif,
+			       struct isp4if_gpu_mem_info *mem_info)
+{
+	struct device *dev = ispif->dev;
+	struct amdgpu_bo *bo;
+
Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+	if (!mem_info) {
+		dev_err(dev, "invalid mem_info\n");
+		return -EINVAL;
+	}
+
+	bo = (struct amdgpu_bo *)mem_info->mem_handle;

Why do you need to cast here?

Quoted 2 lines

In the next patch, will use new helper function for this and will no longer use amdgpu_bo

Not quite, on top of this patch number 6 adds more of the same.

...

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+static struct isp4if_cmd_element *
+isp4if_append_cmd_2_cmdq(struct isp4_interface *ispif,
+			 struct isp4if_cmd_element *cmd_ele)
+{
+	struct isp4if_cmd_element *copy_command = NULL;
Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+
+	copy_command = kmalloc(sizeof(*copy_command), GFP_KERNEL);
+	if (!copy_command)
+		return NULL;
+
+	memcpy(copy_command, cmd_ele, sizeof(*copy_command));

kmemdup()?

Quoted 1 line

Kmemdup is to allocate memory and copy, can't be used here.

Isn't that what you're doing above?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
(quoted diff) +7 -0
+
+	guard(mutex)(&ispif->cmdq_mutex);
+
+	list_add_tail(&copy_command->list, &ispif->cmdq);
+
+	return copy_command;
+}
Signature · 2 lines
Regards,
Bin
maintainer
#34 lore raw

Re: [PATCH v2 4/8] media: platform: amd: Add isp4 fw and hw interface

In reply to Laurent Pinchart

Hi Laurent, Bin,

Quoted 75 lines · Laurent Pinchart
Quoted 56 lines · Bin

Many thanks Laurent Pinchart for the comments.

Quoted 42 lines · Laurent Pinchart
Quoted 18 lines · Sakari Ailus
Quoted 17 lines · Bin
Quoted 16 lines · Sakari Ailus
Quoted 4 lines · Bin Du

ISP firmware controls ISP HW pipeline using dedicated embedded processor called ccpu.
The communication between ISP FW and driver is using commands and response messages sent through the ring buffer. Command buffers support

Quoted 8 lines

either global setting that is not specific to the steam and support stream specific parameters. Response buffers contains ISP FW notification information such as frame buffer done and command done. IRQ is used for receiving response buffer from ISP firmware, which is handled in the main isp4 media device. ISP ccpu is booted up through the firmware loading helper function prior to stream start.
Memory used for command buffer and response buffer needs to be allocated from amdgpu buffer manager because isp4 is a child device of amdgpu.

Please rewrap this, some lines above are quite short.

Quoted 7 lines
Quoted 2 lines

Thanks, the line after the short line is supposed to be a new paragraph? Should we put all the description in one paragraph?

One or more paragraphs work fine, but a new paragraph is separated from the previous one by another newline.

...

Paragraphs are defined as a block of text that convey one idea. They should be visually separated by a space. As we can't have fractional line spacing in plain text, paragraphs need to be separated by a blank line. This is a typography rule that maximizes readability. There should be no line break between sentences in a single paragraph.

Whether you write commit messages, formal documentation or comments in code, typography is important to give the best experience to readers. After all, a block of text that wouldn't focus on the readers would have no reason to exist.

Now compare the above with

Quoted 10 lines

Paragraphs are defined as a block of text that convey one idea. They should be visually separated by a space.
As we can't have fractional line spacing in plain text, paragraphs need to be separated by a blank line.
This is a typography rule that maximizes readability. There should be no line break between sentences in a single paragraph. Whether you write commit messages, formal documentation or comments in code, typography is important to give the best experience to readers.
After all, a block of text that wouldn't focus on the readers would have no reason to exist.

Quoted 5 lines

Really appreciate the detailed guide, will follow it. May I summarize like this? 1 Separate paragraphs by a blank line. 2 Don't add line break between sentences in a single paragraph, an exception to this is commit message, because of the 75-character patch check limit, line break can be added, but it should at the 75-character limit boundary

When I wrote "line break", I meant breaking the line after a sentence, before the 75 columns limits. Text blocks should always be wrapped (at 75 columns in commit messages, or 80 in kernel code). What you should avoid is line breaks not related to the columns limit.

This is fine:

This paragraph has a long sentence that does not hold on a single line of 72 characters and therefore needs to be wrapped. There is no line break otherwise, for instance between the first and second sentence, or within a sentence.

Quoted 6 lines

This is not right:

This paragraph has a long sentence that does not hold on a single line of 72 characters and therefore needs to be wrapped.
There is a line break between the first and second sentence, and also a line break in the second sentence, which are not fine.

I wonder if this should make it to kernel documentation. I've come across many new developers recently who would definitely benefit from this.

Also, most editors can rewrap paragraphs of text (e.g. Emacs M-q or Joe C-k C-j).

Signature · 3 lines
Kind regards,

Sakari Ailus
author maintainer
#35 lore raw

Re: [PATCH v2 4/8] media: platform: amd: Add isp4 fw and hw interface

Hi Sakari Ailus,

Quoted 80 lines · Sakari Ailus

Hi Laurent, Bin,

Quoted 77 lines · Laurent Pinchart
Quoted 56 lines · Bin

Many thanks Laurent Pinchart for the comments.

Quoted 42 lines · Laurent Pinchart
Quoted 18 lines · Sakari Ailus
Quoted 17 lines · Bin
Quoted 16 lines · Sakari Ailus
Quoted 4 lines · Bin Du

ISP firmware controls ISP HW pipeline using dedicated embedded processor called ccpu.
The communication between ISP FW and driver is using commands and response messages sent through the ring buffer. Command buffers support

Quoted 8 lines

either global setting that is not specific to the steam and support stream specific parameters. Response buffers contains ISP FW notification information such as frame buffer done and command done. IRQ is used for receiving response buffer from ISP firmware, which is handled in the main isp4 media device. ISP ccpu is booted up through the firmware loading helper function prior to stream start.
Memory used for command buffer and response buffer needs to be allocated from amdgpu buffer manager because isp4 is a child device of amdgpu.

Please rewrap this, some lines above are quite short.

Quoted 7 lines
Quoted 2 lines

Thanks, the line after the short line is supposed to be a new paragraph? Should we put all the description in one paragraph?

One or more paragraphs work fine, but a new paragraph is separated from the previous one by another newline.

...

Paragraphs are defined as a block of text that convey one idea. They should be visually separated by a space. As we can't have fractional line spacing in plain text, paragraphs need to be separated by a blank line. This is a typography rule that maximizes readability. There should be no line break between sentences in a single paragraph.

Whether you write commit messages, formal documentation or comments in code, typography is important to give the best experience to readers. After all, a block of text that wouldn't focus on the readers would have no reason to exist.

Now compare the above with

Quoted 10 lines

Paragraphs are defined as a block of text that convey one idea. They should be visually separated by a space.
As we can't have fractional line spacing in plain text, paragraphs need to be separated by a blank line.
This is a typography rule that maximizes readability. There should be no line break between sentences in a single paragraph. Whether you write commit messages, formal documentation or comments in code, typography is important to give the best experience to readers.
After all, a block of text that wouldn't focus on the readers would have no reason to exist.

Quoted 5 lines

Really appreciate the detailed guide, will follow it. May I summarize like this? 1 Separate paragraphs by a blank line. 2 Don't add line break between sentences in a single paragraph, an exception to this is commit message, because of the 75-character patch check limit, line break can be added, but it should at the 75-character limit boundary

When I wrote "line break", I meant breaking the line after a sentence, before the 75 columns limits. Text blocks should always be wrapped (at 75 columns in commit messages, or 80 in kernel code). What you should avoid is line breaks not related to the columns limit.

This is fine:

This paragraph has a long sentence that does not hold on a single line of 72 characters and therefore needs to be wrapped. There is no line break otherwise, for instance between the first and second sentence, or within a sentence.

This is not right:

Quoted 8 lines
Quoted 4 lines

This paragraph has a long sentence that does not hold on a single line of 72 characters and therefore needs to be wrapped.
There is a line break between the first and second sentence, and also a line break in the second sentence, which are not fine.

I wonder if this should make it to kernel documentation. I've come across many new developers recently who would definitely benefit from this.

Yes, totally agree, furthermore, if checkpatch script can be strengthened to add this kind of check, developers can even find and fix them before submit the patches

Quoted 3 lines

Also, most editors can rewrap paragraphs of text (e.g. Emacs M-q or Joe C-k C-j).

Signature · 2 lines
Regards,
Bin
author maintainer
#36 lore raw

Re: [PATCH v2 4/8] media: platform: amd: Add isp4 fw and hw interface

In reply to Sakari Ailus

Thanks Sakari Ailus for the comments

Quoted 24 lines · Sakari Ailus

Hi Bin,

Quoted 21 lines · Bin

Many thanks Askari Ailus for your careful review

Quoted 18 lines · Sakari Ailus

Hi Bin,

Quoted 4 lines · Bin Du

ISP firmware controls ISP HW pipeline using dedicated embedded processor called ccpu.
The communication between ISP FW and driver is using commands and response messages sent through the ring buffer. Command buffers support

Quoted 8 lines

either global setting that is not specific to the steam and support stream specific parameters. Response buffers contains ISP FW notification information such as frame buffer done and command done. IRQ is used for receiving response buffer from ISP firmware, which is handled in the main isp4 media device. ISP ccpu is booted up through the firmware loading helper function prior to stream start.
Memory used for command buffer and response buffer needs to be allocated from amdgpu buffer manager because isp4 is a child device of amdgpu.

Please rewrap this, some lines above are quite short.

Quoted 8 lines
Quoted 2 lines

Thanks, the line after the short line is supposed to be a new paragraph? Should we put all the description in one paragraph?

One or more paragraphs work fine, but a new paragraph is separated from the previous one by another newline.

...

Got it, thanks.

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 4 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +4 -0
+	void *cpu_ptr;
+	u64 gpu_addr;
+	u32 ret;
+

drivers/media/platform/amd/isp4/isp4_interface.c line 149

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +10 -0
+	dev = ispif->dev;
+
+	if (!mem_size)
+		return NULL;
+
+	mem_info = kzalloc(sizeof(*mem_info), GFP_KERNEL);
+	if (!mem_info)
+		return NULL;
+
+	adev = (struct amdgpu_device *)ispif->adev;

Why the cast?

adev isn't a great name here as it's usually used for struct acpi_devices.

Quoted 8 lines
Quoted 2 lines

In the next patch, will use new helper function for this and will no longer use amdgpu_device

Use correct types when you can; either way this doesn't seem to be changed by the further patches in the set.

...

Yes, totally agree.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 186

Quoted 12 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +12 -0
+static int isp4if_gpu_mem_free(struct isp4_interface *ispif,
+			       struct isp4if_gpu_mem_info *mem_info)
+{
+	struct device *dev = ispif->dev;
+	struct amdgpu_bo *bo;
+
+	if (!mem_info) {
+		dev_err(dev, "invalid mem_info\n");
+		return -EINVAL;
+	}
+
+	bo = (struct amdgpu_bo *)mem_info->mem_handle;

Why do you need to cast here?

Quoted 7 lines
Quoted 2 lines

In the next patch, will use new helper function for this and will no longer use amdgpu_bo

Not quite, on top of this patch number 6 adds more of the same.

...

Thanks, will double check all new patches to avoid similar problem.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 343

Quoted 11 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +11 -0
+static struct isp4if_cmd_element *
+isp4if_append_cmd_2_cmdq(struct isp4_interface *ispif,
+			 struct isp4if_cmd_element *cmd_ele)
+{
+	struct isp4if_cmd_element *copy_command = NULL;
+
+	copy_command = kmalloc(sizeof(*copy_command), GFP_KERNEL);
+	if (!copy_command)
+		return NULL;
+
+	memcpy(copy_command, cmd_ele, sizeof(*copy_command));

kmemdup()?

Quoted 5 lines
Quoted 1 line

Kmemdup is to allocate memory and copy, can't be used here.

Isn't that what you're doing above?

Quoted 1 line

Yes, you are absolutely right. Sorry, missed the kmalloc before the memcpy, will fix in the next patch.

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

drivers/media/platform/amd/isp4/isp4_interface.c line 350

Quoted 7 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +7 -0
+
+	guard(mutex)(&ispif->cmdq_mutex);
+
+	list_add_tail(&copy_command->list, &ispif->cmdq);
+
+	return copy_command;
+}

Regards,
Bin

author maintainer
#37 lore raw

PATCH v2 5/8 media: platform: amd: isp4 subdev and firmware loading handling added

In reply to Bin Du

Isp4 sub-device is implementing v4l2 sub-device interface. It has one capture video node, and supports only preview stream. It manages firmware states, stream configuration and mipi phy configuration. This change also adds interrupt handling and notification for isp firmware to isp-subdevice.

---
 drivers/media/platform/amd/isp4/Makefile      |    3 +-
 drivers/media/platform/amd/isp4/isp4.c        |  267 +++-
 drivers/media/platform/amd/isp4/isp4.h        |    4 +-
 drivers/media/platform/amd/isp4/isp4_subdev.c | 1192 +++++++++++++++++
 drivers/media/platform/amd/isp4/isp4_subdev.h |  145 ++
 5 files changed, 1601 insertions(+), 10 deletions(-)
 create mode 100644 drivers/media/platform/amd/isp4/isp4_subdev.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4_subdev.h
drivers/media/platform/amd/isp4/Makefile +2 -1
index c0166f954516..52defc06189e 100644
@@ -3,9 +3,10 @@
 # Copyright (C) 2025 Advanced Micro Devices, Inc.
 
 obj-$(CONFIG_AMD_ISP4) += amd_capture.o
-amd_capture-objs := isp4.o	\
+amd_capture-objs := isp4_subdev.o \
 			isp4_phy.o \
 			isp4_interface.o \
+			isp4.o	\
 			isp4_hw.o	\
 
 ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
drivers/media/platform/amd/isp4/isp4.c +259 -8

328 lines not on this page · raw message

drivers/media/platform/amd/isp4/isp4.h +3 -1
index 27a7362ce6f9..596431b4a5c2 100644
@@ -7,9 +7,9 @@
 #define _ISP4_H_
 
 #include <linux/mutex.h>
-#include <media/v4l2-device.h>
 #include <media/videobuf2-memops.h>
 #include <media/videobuf2-vmalloc.h>
+#include "isp4_subdev.h"
 
 #define ISP4_GET_ISP_REG_BASE(isp4sd) (((isp4sd))->mmio)
 
@@ -25,11 +25,13 @@ struct isp4_platform_data {
 
 struct isp4_device {
 	struct v4l2_device v4l2_dev;
+	struct isp4_subdev isp_sdev;
 	struct media_device mdev;
 
 	struct isp4_platform_data *pltf_data;
 	struct platform_device *pdev;
 	struct notifier_block i2c_nb;
+	struct v4l2_async_notifier notifier;
 };
 
 #endif /* isp4.h */
drivers/media/platform/amd/isp4/isp4_subdev.c new file +1192 -0

1,192 lines not on this page · raw message

drivers/media/platform/amd/isp4/isp4_subdev.h new file +145 -0

145 lines not on this page · raw message

Signature · 1 line
2.34.1
#38 lore raw

Re: [PATCH v2 5/8] media: platform: amd: isp4 subdev and firmware loading handling added

Quoted 81 lines · Bin Du

Isp4 sub-device is implementing v4l2 sub-device interface. It has one capture video node, and supports only preview stream. It manages firmware states, stream configuration and mipi phy configuration. This change also adds interrupt handling and notification for isp firmware to isp-subdevice.

---
  drivers/media/platform/amd/isp4/Makefile      |    3 +-
  drivers/media/platform/amd/isp4/isp4.c        |  267 +++-
  drivers/media/platform/amd/isp4/isp4.h        |    4 +-
  drivers/media/platform/amd/isp4/isp4_subdev.c | 1192 +++++++++++++++++
  drivers/media/platform/amd/isp4/isp4_subdev.h |  145 ++
  5 files changed, 1601 insertions(+), 10 deletions(-)

create mode 100644 drivers/media/platform/amd/isp4/isp4_subdev.c create mode 100644 drivers/media/platform/amd/isp4/isp4_subdev.h

drivers/media/platform/amd/isp4/Makefile +2 -1
index c0166f954516..52defc06189e 100644
@@ -3,9 +3,10 @@
  # Copyright (C) 2025 Advanced Micro Devices, Inc.
 
  obj-$(CONFIG_AMD_ISP4) += amd_capture.o
-amd_capture-objs := isp4.o	\
+amd_capture-objs := isp4_subdev.o \
  			isp4_phy.o \
  			isp4_interface.o \
+			isp4.o	\
  			isp4_hw.o	\
 
  ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
drivers/media/platform/amd/isp4/isp4.c +25 -2
index d0be90c5ec3b..c1aca2bd35e3 100644
@@ -5,13 +5,20 @@
 
  #include <linux/pm_runtime.h>
  #include <linux/vmalloc.h>
+#include <media/v4l2-fwnode.h>
  #include <media/v4l2-ioctl.h>
 
-#include "isp4.h"
+#include "amdgpu_object.h"
 
-#define VIDEO_BUF_NUM 5
+#include "isp4.h"
+#include "isp4_hw.h"
 
  #define ISP4_DRV_NAME "amd_isp_capture"
+#define ISP4_FW_RESP_RB_IRQ_STATUS_MASK \
+	(ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT9_INT_MASK  | \
+	 ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT10_INT_MASK | \
+	 ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT11_INT_MASK | \
+	 ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT12_INT_MASK)
 
  /* interrupt num */
  static const u32 isp4_ringbuf_interrupt_num[] = {
@@ -24,23 +31,233 @@ static const u32 isp4_ringbuf_interrupt_num[] = {
  #define to_isp4_device(dev) \
  	((struct isp4_device *)container_of(dev, struct isp4_device, v4l2_dev))
 
+static int isp4_create_links(struct isp4_device *isp4_dev,
+			     struct v4l2_subdev *sensor_sdev)
+{
+	struct v4l2_subdev *isp4_sdev = &isp4_dev->isp_sdev.sdev;
+	struct device *dev = &isp4_dev->pdev->dev;
+	int ret;
+
+	ret = media_create_pad_link(&sensor_sdev->entity,
+				    0, &isp4_sdev->entity, 0,
+				    MEDIA_LNK_FL_ENABLED |
+				    MEDIA_LNK_FL_IMMUTABLE);
+	if (ret)
+		dev_err(dev, "create sensor to isp link fail:%d\n", ret);
+
+	return ret;
+}

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 59

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c +10 -0
+
+static int isp4_register_subdev_and_create_links(struct isp4_device *isp_dev,
+						 struct v4l2_subdev *sdev)
+{
+	struct device *dev = &isp_dev->pdev->dev;
+	int ret;
+
+	ret = isp4_create_links(isp_dev, sdev);
+	if (ret)
+		dev_err(dev, "fail create isp link:%d\n", ret);

You're discarding ret here. Shouldn't you return ret in the failure path?

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 64

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c +5 -0
+
+	ret = v4l2_device_register_subdev_nodes(&isp_dev->v4l2_dev);
+	if (ret != 0) {
+		dev_warn(dev, "register subdev as nodes fail:%d\n", ret);
+		ret = 0;

This isn't fatal?

Quoted 9 lines
drivers/media/platform/amd/isp4/isp4.c +9 -0
+	}
+
+	return ret;
+}
+
+static int isp4_camera_sensor_bound(struct v4l2_async_notifier *notifier,
+				    struct v4l2_subdev *sensor_sdev,
+				    struct v4l2_async_connection *asd)
+{

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 83

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c +10 -0
+	struct isp4_device *isp_dev = to_isp4_device(notifier->v4l2_dev);
+	struct device *dev = &isp_dev->pdev->dev;
+	int ret;
+
+	ret = isp4_register_subdev_and_create_links(isp_dev, sensor_sdev);
+	if (ret)
+		dev_err(dev, "register sensor subdev fail:%d\n",
+			ret);
+	else
+		dev_dbg(dev, "register sensor subdev suc\n");

Probably can drop this now

Quoted 72 lines
drivers/media/platform/amd/isp4/isp4.c +71 -0
+	return ret;
+}
+
+static void isp4_camera_sensor_unbind(struct v4l2_async_notifier *notifier,
+				      struct v4l2_subdev *sensor_sdev,
+				      struct v4l2_async_connection *asd)
+{
+}
+
+static const struct v4l2_async_notifier_operations isp4_camera_sensor_ops = {
+	.bound = isp4_camera_sensor_bound,
+	.unbind = isp4_camera_sensor_unbind,
+};
+
+static void isp4_wake_up_resp_thread(struct isp4_subdev *isp, u32 index)
+{
+	if (isp && index < ISP4SD_MAX_FW_RESP_STREAM_NUM) {
+		struct isp4sd_thread_handler *thread_ctx =
+				&isp->fw_resp_thread[index];
+
+		thread_ctx->wq_cond = 1;
+		wake_up_interruptible(&thread_ctx->waitq);
+	}
+}
+
+static void isp4_resp_interrupt_notify(struct isp4_subdev *isp, u32 intr_status)
+{
+	bool wake = (isp->ispif.status == ISP4IF_STATUS_FW_RUNNING);
+
+	u32 intr_ack = 0;
+
+	/* global response */
+	if (intr_status &
+	    ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT12_INT_MASK) {
+		if (wake)
+			isp4_wake_up_resp_thread(isp, 0);
+
+		intr_ack |= ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT12_ACK_MASK;
+	}
+
+	/* stream 1 response */
+	if (intr_status &
+	    ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT9_INT_MASK) {
+		if (wake)
+			isp4_wake_up_resp_thread(isp, 1);
+
+		intr_ack |= ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT9_ACK_MASK;
+	}
+
+	/* stream 2 response */
+	if (intr_status &
+	    ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT10_INT_MASK) {
+		if (wake)
+			isp4_wake_up_resp_thread(isp, 2);
+
+		intr_ack |= ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT10_ACK_MASK;
+	}
+
+	/* stream 3 response */
+	if (intr_status &
+	    ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT11_INT_MASK) {
+		if (wake)
+			isp4_wake_up_resp_thread(isp, 3);
+
+		intr_ack |= ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT11_ACK_MASK;
+	}
+
+	/* clear ISP_SYS interrupts */
+	isp4hw_wreg(ISP4_GET_ISP_REG_BASE(isp), ISP_SYS_INT0_ACK, intr_ack);
+}
+
  static irqreturn_t isp4_irq_handler(int irq, void *arg)

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 163

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c +5 -2
  {
-	struct isp4_device *isp_dev = dev_get_drvdata((struct device *)arg);
+	struct isp4_device *isp_dev = dev_get_drvdata(arg);
+	struct isp4_subdev *isp = NULL;
+	u32 isp_sys_irq_status = 0x0;
+	u32 r1;
 
  	if (!isp_dev)
-		goto error_drv_data;
+		return IRQ_HANDLED;

As mentioned in the first patch, avoid ping ponging the code.

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 34

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c +8 -1
+
+	isp = &isp_dev->isp_sdev;
+	/* check ISP_SYS interrupts status */
+	r1 = isp4hw_rreg(ISP4_GET_ISP_REG_BASE(isp), ISP_SYS_INT0_STATUS);
+
+	isp_sys_irq_status = r1 & ISP4_FW_RESP_RB_IRQ_STATUS_MASK;
+
+	isp4_resp_interrupt_notify(isp, isp_sys_irq_status);
 
-error_drv_data:

As mentioned in the first patch, avoid ping ponging the code.

Quoted 150 lines
drivers/media/platform/amd/isp4/isp4.c +108 -2
  	return IRQ_HANDLED;
  }
 
+static int isp4_parse_fwnode_init_async_nf(struct isp4_device *isp_dev)
+{
+	struct isp4_subdev *isp_sdev = &isp_dev->isp_sdev;
+	struct device *dev = &isp_dev->pdev->dev;
+	struct v4l2_fwnode_endpoint bus_cfg = {
+		.bus_type = V4L2_MBUS_CSI2_DPHY
+	};
+	struct fwnode_handle *remote_ep = NULL;
+	struct fwnode_handle *local_ep = NULL;
+	struct v4l2_async_connection *asd;
+	struct fwnode_handle *fwnode;
+	struct fwnode_endpoint fwep;
+	int ret;
+
+	fwnode = dev_fwnode(dev);
+
+	local_ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
+	if (!local_ep) {
+		ret = -ENXIO;
+		goto err_fwnode;
+	}
+
+	remote_ep = fwnode_graph_get_remote_endpoint(local_ep);
+	if (!remote_ep) {
+		ret = -ENXIO;
+		goto err_fwnode;
+	}
+
+	ret = fwnode_graph_parse_endpoint(remote_ep, &fwep);
+	if (ret)
+		goto err_fwnode;
+	isp_sdev->phy_id = fwep.port;
+
+	ret = v4l2_fwnode_endpoint_alloc_parse(remote_ep, &bus_cfg);
+	if (ret)
+		goto err_fwnode;
+
+	if (!bus_cfg.nr_of_link_frequencies) {
+		ret = -EINVAL;
+		dev_err(dev, "fail invalid link freq number %u\n", bus_cfg.nr_of_link_frequencies);
+		v4l2_fwnode_endpoint_free(&bus_cfg);
+		goto err_fwnode;
+	}
+	isp_sdev->phy_link_freq = *bus_cfg.link_frequencies;
+	v4l2_fwnode_endpoint_free(&bus_cfg);
+
+	isp_sdev->phy_num_data_lanes =
+		fwnode_property_count_u32(remote_ep, "data-lanes");
+
+	v4l2_async_nf_init(&isp_dev->notifier, &isp_dev->v4l2_dev);
+
+	asd = v4l2_async_nf_add_fwnode(&isp_dev->notifier, remote_ep,
+				       struct v4l2_async_connection);
+	if (IS_ERR(asd)) {
+		ret = PTR_ERR(asd);
+		goto err_async_nf_cleanup;
+	}
+
+	isp_dev->notifier.ops = &isp4_camera_sensor_ops;
+	ret = v4l2_async_nf_register(&isp_dev->notifier);
+	if (ret) {
+		dev_err(dev, "v4l2_async_nf_register fail:%d", ret);
+		goto err_async_nf_cleanup;
+	}
+
+	return 0;
+
+err_async_nf_cleanup:
+	v4l2_async_nf_cleanup(&isp_dev->notifier);
+err_fwnode:
+	if (remote_ep)
+		fwnode_handle_put(remote_ep);
+	if (local_ep)
+		fwnode_handle_put(remote_ep);
+
+	return ret;
+}
+
  /*
   * amd capture module
   */
  static int isp4_capture_probe(struct platform_device *pdev)
  {
  	struct device *dev = &pdev->dev;
+	struct isp4_subdev *isp_sdev;
  	struct isp4_device *isp_dev;
 
  	int i, irq, ret;
@@ -52,6 +269,17 @@ static int isp4_capture_probe(struct platform_device *pdev)
  	isp_dev->pdev = pdev;
  	dev->init_name = ISP4_DRV_NAME;
 
+	isp_sdev = &isp_dev->isp_sdev;
+	isp_sdev->mmio = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(isp_sdev->mmio))
+		return dev_err_probe(dev, PTR_ERR(isp_sdev->mmio),
+				     "isp ioremap fail\n");
+
+	isp_sdev->isp_phy_mmio = devm_platform_ioremap_resource(pdev, 1);
+	if (IS_ERR(isp_sdev->isp_phy_mmio))
+		return dev_err_probe(dev, PTR_ERR(isp_sdev->isp_phy_mmio),
+				     "isp phy mmio ioremap fail\n");
+
  	for (i = 0; i < ARRAY_SIZE(isp4_ringbuf_interrupt_num); i++) {
  		irq = platform_get_irq(pdev, isp4_ringbuf_interrupt_num[i]);
  		if (irq < 0)
@@ -90,10 +318,23 @@ static int isp4_capture_probe(struct platform_device *pdev)
 
  	dev_dbg(dev, "AMD ISP v4l2 device registered\n");
 
+	ret = isp4sd_init(&isp_dev->isp_sdev, &isp_dev->v4l2_dev,
+			  isp_dev->pltf_data->adev);
+	if (ret) {
+		dev_err(dev, "fail init isp4 sub dev %d\n", ret);
+		goto err_unreg_v4l2;
+	}
+
+	ret = isp4_parse_fwnode_init_async_nf(isp_dev);
+	if (ret) {
+		dev_err(dev, "fail to parse fwnode %d\n", ret);
+		goto err_unreg_v4l2;
+	}
+
  	ret = media_device_register(&isp_dev->mdev);
  	if (ret) {
  		dev_err(dev, "fail to register media device %d\n", ret);
-		goto err_unreg_v4l2;
+		goto err_isp4_deinit;
  	}
 
  	platform_set_drvdata(pdev, isp_dev);
@@ -103,6 +344,10 @@ static int isp4_capture_probe(struct platform_device *pdev)
 
  	return 0;
 
+err_isp4_deinit:
+	v4l2_async_nf_unregister(&isp_dev->notifier);
+	v4l2_async_nf_cleanup(&isp_dev->notifier);
+	isp4sd_deinit(&isp_dev->isp_sdev);
  err_unreg_v4l2:
  	v4l2_device_unregister(&isp_dev->v4l2_dev);
 
@@ -112,11 +357,17 @@ static int isp4_capture_probe(struct platform_device *pdev)
  static void isp4_capture_remove(struct platform_device *pdev)
  {
  	struct isp4_device *isp_dev = platform_get_drvdata(pdev);
-	struct device *dev = &pdev->dev;

drivers/media/platform/amd/isp4/isp4.c static int isp4_capture_probe(struct platform_device *pdev) · line 368

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c +6 -1
+
+	v4l2_async_nf_unregister(&isp_dev->notifier);
+	v4l2_async_nf_cleanup(&isp_dev->notifier);
+	v4l2_device_unregister_subdev(&isp_dev->isp_sdev.sdev);
 
  	media_device_unregister(&isp_dev->mdev);
+	media_entity_cleanup(&isp_dev->isp_sdev.sdev.entity);
  	v4l2_device_unregister(&isp_dev->v4l2_dev);
-	dev_dbg(dev, "AMD ISP v4l2 device unregistered\n");
+	dev_dbg(&pdev->dev, "AMD ISP v4l2 device unregistered\n");

Don't ping pong this

Quoted 1,383 lines
drivers/media/platform/amd/isp4/isp4.c +2 -0
+
+	isp4sd_deinit(&isp_dev->isp_sdev);
  }
 
  static struct platform_driver isp4_capture_drv = {
drivers/media/platform/amd/isp4/isp4.h +3 -1
index 27a7362ce6f9..596431b4a5c2 100644
@@ -7,9 +7,9 @@
  #define _ISP4_H_
 
  #include <linux/mutex.h>
-#include <media/v4l2-device.h>
  #include <media/videobuf2-memops.h>
  #include <media/videobuf2-vmalloc.h>
+#include "isp4_subdev.h"
 
  #define ISP4_GET_ISP_REG_BASE(isp4sd) (((isp4sd))->mmio)
 
@@ -25,11 +25,13 @@ struct isp4_platform_data {
 
  struct isp4_device {
  	struct v4l2_device v4l2_dev;
+	struct isp4_subdev isp_sdev;
  	struct media_device mdev;
 
  	struct isp4_platform_data *pltf_data;
  	struct platform_device *pdev;
  	struct notifier_block i2c_nb;
+	struct v4l2_async_notifier notifier;
  };
 
  #endif /* isp4.h */
drivers/media/platform/amd/isp4/isp4_subdev.c new file +1192 -0
new file mode 100644
index 000000000000..978164031067
@@ -0,0 +1,1192 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/mutex.h>
+#include <linux/pm_domain.h>
+#include <linux/pm_runtime.h>
+
+#include "isp4_fw_cmd_resp.h"
+#include "isp4_hw.h"
+#include "isp4_interface.h"
+#include "isp4_phy.h"
+#include "isp4_subdev.h"
+#include <linux/units.h>
+
+#define ISP4SD_MAX_CMD_RESP_BUF_SIZE (4 * 1024)
+#define ISP4SD_MIN_BUF_CNT_BEF_START_STREAM 4
+
+#define ISP4SD_PERFORMANCE_STATE_LOW 0
+#define ISP4SD_PERFORMANCE_STATE_HIGH 1
+
+#define ISP4SD_FW_CMD_TIMEOUT_IN_MS  500
+#define ISP4SD_WAIT_RESP_IRQ_TIMEOUT  5 /* ms */
+/* align 32KB */
+#define ISP4SD_META_BUF_SIZE ALIGN(sizeof(struct isp4fw_meta_info), 0x8000)
+
+#define to_isp4_subdev(v4l2_sdev)  \
+	container_of(v4l2_sdev, struct isp4_subdev, sdev)
+
+#define to_isp4_vdev(isp4_vid)  \
+	container_of(isp4_vid, struct isp4_subdev, isp_vdev)
+
+static const char *isp4sd_entity_name = "amd isp4";
+
+struct isp4sd_mbus_image_format_remap {
+	u32				mbus_code;
+	enum isp4fw_image_format	image_format;
+};
+
+static const struct isp4sd_mbus_image_format_remap
+	isp4sd_image_formats[] = {
+	{
+		.mbus_code	= MEDIA_BUS_FMT_YUYV8_1_5X8,
+		.image_format	= IMAGE_FORMAT_NV12,
+	},
+	{
+		.mbus_code	= MEDIA_BUS_FMT_YUYV8_1X16,
+		.image_format	= IMAGE_FORMAT_YUV422INTERLEAVED,
+	},
+};
+
+static void isp4sd_module_enable(struct isp4_subdev *isp_subdev, bool enable)
+{
+	if (isp_subdev->enable_gpio) {
+		gpiod_set_value(isp_subdev->enable_gpio, enable ? 1 : 0);
+		dev_dbg(isp_subdev->dev, "%s isp_subdev module\n",
+			enable ? "enable" : "disable");
+	}
+}
+
+static int isp4sd_setup_fw_mem_pool(struct isp4_subdev *isp_subdev)
+{
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct isp4fw_cmd_send_buffer buf_type;
+	struct device *dev = isp_subdev->dev;
+	int ret;
+
+	if (!ispif->fw_mem_pool) {
+		dev_err(dev, "fail to alloc mem pool\n");
+		return -ENOMEM;
+	}
+
+	memset(&buf_type, 0, sizeof(buf_type));
+	buf_type.buffer_type = BUFFER_TYPE_MEM_POOL;
+	buf_type.buffer.buf_tags = 0;
+	buf_type.buffer.vmid_space.bit.vmid = 0;
+	buf_type.buffer.vmid_space.bit.space = ADDR_SPACE_TYPE_GPU_VA;
+	isp4if_split_addr64(ispif->fw_mem_pool->gpu_mc_addr,
+			    &buf_type.buffer.buf_base_a_lo,
+			    &buf_type.buffer.buf_base_a_hi);
+	buf_type.buffer.buf_size_a = (u32)ispif->fw_mem_pool->mem_size;
+
+	ret = isp4if_send_command(ispif, CMD_ID_SEND_BUFFER,
+				  &buf_type, sizeof(buf_type));
+	if (ret) {
+		dev_err(dev, "send fw mem pool 0x%llx(%u) fail %d\n",
+			ispif->fw_mem_pool->gpu_mc_addr,
+			buf_type.buffer.buf_size_a,
+			ret);
+		return ret;
+	}
+
+	dev_dbg(dev, "send fw mem pool 0x%llx(%u) suc\n",
+		ispif->fw_mem_pool->gpu_mc_addr,
+		buf_type.buffer.buf_size_a);
+
+	return 0;
+};
+
+static int isp4sd_set_stream_path(struct isp4_subdev *isp_subdev)
+{
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct isp4fw_cmd_set_stream_cfg cmd = {0};
+	struct device *dev = isp_subdev->dev;
+
+	cmd.stream_cfg.mipi_pipe_path_cfg.isp4fw_sensor_id = SENSOR_ID_ON_MIPI0;
+	cmd.stream_cfg.mipi_pipe_path_cfg.b_enable = true;
+	cmd.stream_cfg.isp_pipe_path_cfg.isp_pipe_id = MIPI0_ISP_PIPELINE_ID;
+
+	cmd.stream_cfg.b_enable_tnr = true;
+	dev_dbg(dev, "isp4fw_sensor_id %d, pipeId 0x%x EnableTnr %u\n",
+		cmd.stream_cfg.mipi_pipe_path_cfg.isp4fw_sensor_id,
+		cmd.stream_cfg.isp_pipe_path_cfg.isp_pipe_id,
+		cmd.stream_cfg.b_enable_tnr);
+
+	return isp4if_send_command(ispif, CMD_ID_SET_STREAM_CONFIG,
+				   &cmd, sizeof(cmd));
+}
+
+static int isp4sd_send_meta_buf(struct isp4_subdev *isp_subdev)
+{
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct isp4fw_cmd_send_buffer buf_type = { 0 };
+	struct isp4sd_sensor_info *sensor_info;
+	struct device *dev = isp_subdev->dev;
+	u32 i;
+
+	sensor_info = &isp_subdev->sensor_info;
+	for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++) {
+		int ret;
+
+		if (!sensor_info->meta_info_buf[i]) {
+			dev_err(dev, "fail for no meta info buf(%u)\n", i);
+			return -ENOMEM;
+		}
+		buf_type.buffer_type = BUFFER_TYPE_META_INFO;
+		buf_type.buffer.buf_tags = 0;
+		buf_type.buffer.vmid_space.bit.vmid = 0;
+		buf_type.buffer.vmid_space.bit.space = ADDR_SPACE_TYPE_GPU_VA;
+		isp4if_split_addr64(sensor_info->meta_info_buf[i]->gpu_mc_addr,
+				    &buf_type.buffer.buf_base_a_lo,
+				    &buf_type.buffer.buf_base_a_hi);
+		buf_type.buffer.buf_size_a =
+			(u32)sensor_info->meta_info_buf[i]->mem_size;
+		ret = isp4if_send_command(ispif, CMD_ID_SEND_BUFFER,
+					  &buf_type,
+					  sizeof(buf_type));
+		if (ret) {
+			dev_err(dev, "send meta info(%u) fail\n", i);
+			return ret;
+		}
+	}
+
+	dev_dbg(dev, "send meta info suc\n");
+	return 0;
+}
+
+static int isp4sd_enum_mbus_code(struct v4l2_subdev *isp_subdev,
+				 struct v4l2_subdev_state *state,
+				 struct v4l2_subdev_mbus_code_enum *code_enum)
+{
+	if (code_enum->index >= ARRAY_SIZE(isp4sd_image_formats))
+		return -EINVAL;
+
+	code_enum->code = isp4sd_image_formats[code_enum->index].mbus_code;
+
+	return 0;
+}
+
+static bool isp4sd_get_str_out_prop(struct isp4_subdev *isp_subdev,
+				    struct isp4fw_image_prop *out_prop,
+				    struct v4l2_subdev_state *state, u32 pad)
+{
+	struct v4l2_mbus_framefmt *format = NULL;
+	struct device *dev = isp_subdev->dev;
+	bool ret;
+
+	format = v4l2_subdev_state_get_format(state, pad, 0);
+	if (!format) {
+		dev_err(dev, "fail get subdev state format\n");
+		return false;
+	}
+
+	switch (format->code) {
+	case MEDIA_BUS_FMT_YUYV8_1_5X8:
+		out_prop->image_format = IMAGE_FORMAT_NV12;
+		out_prop->width = format->width;
+		out_prop->height = format->height;
+		out_prop->luma_pitch = format->width;
+		out_prop->chroma_pitch = out_prop->width;
+		ret = true;
+		break;
+	case MEDIA_BUS_FMT_YUYV8_1X16:
+		out_prop->image_format = IMAGE_FORMAT_YUV422INTERLEAVED;
+		out_prop->width = format->width;
+		out_prop->height = format->height;
+		out_prop->luma_pitch = format->width * 2;
+		out_prop->chroma_pitch = 0;
+		ret = true;
+		break;
+	default:
+		dev_err(dev, "fail for bad image format:0x%x\n",
+			format->code);
+		ret = false;
+		break;
+	}
+
+	if (!out_prop->width || !out_prop->height)
+		ret = false;
+	return ret;
+}
+
+static int isp4sd_kickoff_stream(struct isp4_subdev *isp_subdev, u32 w, u32 h)
+{
+	struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct device *dev = isp_subdev->dev;
+
+	if (sensor_info->status == ISP4SD_START_STATUS_STARTED) {
+		return 0;
+	} else if (sensor_info->status == ISP4SD_START_STATUS_START_FAIL) {
+		dev_err(dev, "fail for previous start fail\n");
+		return -EINVAL;
+	}
+
+	dev_dbg(dev, "w:%u,h:%u\n", w, h);
+
+	sensor_info->status = ISP4SD_START_STATUS_START_FAIL;
+
+	if (isp4sd_send_meta_buf(isp_subdev)) {
+		dev_err(dev, "fail to send meta buf\n");
+		return -EINVAL;
+	};
+
+	sensor_info->status = ISP4SD_START_STATUS_NOT_START;
+
+	if (!sensor_info->start_stream_cmd_sent &&
+	    sensor_info->buf_sent_cnt >=
+	    ISP4SD_MIN_BUF_CNT_BEF_START_STREAM) {
+		int ret = isp4if_send_command(ispif, CMD_ID_START_STREAM,
+					      NULL, 0);
+		if (ret) {
+			dev_err(dev, "fail to start stream\n");
+			return ret;
+		}
+
+		sensor_info->start_stream_cmd_sent = true;
+	} else {
+		dev_dbg(dev,
+			"no send START_STREAM, start_sent %u, buf_sent %u\n",
+			sensor_info->start_stream_cmd_sent,
+			sensor_info->buf_sent_cnt);
+	}
+
+	return 0;
+}
+
+static int isp4sd_setup_output(struct isp4_subdev *isp_subdev,
+			       struct v4l2_subdev_state *state, u32 pad)
+{
+	struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+	struct isp4sd_output_info *output_info =
+			&isp_subdev->sensor_info.output_info;
+	struct isp4fw_cmd_set_out_ch_prop cmd_ch_prop = {0};
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct isp4fw_cmd_enable_out_ch cmd_ch_en = {0};
+	struct device *dev = isp_subdev->dev;
+	struct isp4fw_image_prop *out_prop;
+	int ret;
+
+	if (output_info->start_status == ISP4SD_START_STATUS_STARTED)
+		return 0;
+
+	if (output_info->start_status == ISP4SD_START_STATUS_START_FAIL) {
+		dev_err(dev, "fail for previous start fail\n");
+		return -EINVAL;
+	}
+
+	out_prop = &cmd_ch_prop.image_prop;
+	cmd_ch_prop.ch = ISP_PIPE_OUT_CH_PREVIEW;
+	cmd_ch_en.ch = ISP_PIPE_OUT_CH_PREVIEW;
+	cmd_ch_en.is_enable = true;
+
+	if (!isp4sd_get_str_out_prop(isp_subdev, out_prop, state, pad)) {
+		dev_err(dev, "fail to get out prop\n");
+		return -EINVAL;
+	}
+
+	dev_dbg(dev, "channel: w:h=%u:%u,lp:%u,cp%u\n",
+		cmd_ch_prop.image_prop.width, cmd_ch_prop.image_prop.height,
+		cmd_ch_prop.image_prop.luma_pitch,
+		cmd_ch_prop.image_prop.chroma_pitch);
+
+	ret = isp4if_send_command(ispif, CMD_ID_SET_OUT_CHAN_PROP,
+				  &cmd_ch_prop,
+				  sizeof(cmd_ch_prop));
+	if (ret) {
+		output_info->start_status = ISP4SD_START_STATUS_START_FAIL;
+		dev_err(dev, "fail to set out prop\n");
+		return ret;
+	};
+
+	ret = isp4if_send_command(ispif, CMD_ID_ENABLE_OUT_CHAN,
+				  &cmd_ch_en, sizeof(cmd_ch_en));
+
+	if (ret) {
+		output_info->start_status = ISP4SD_START_STATUS_START_FAIL;
+		dev_err(dev, "fail to enable channel\n");
+		return ret;
+	}
+
+	if (!sensor_info->start_stream_cmd_sent) {
+		ret = isp4sd_kickoff_stream(isp_subdev, out_prop->width,
+					    out_prop->height);
+		if (ret) {
+			dev_err(dev, "kickoff stream fail %d\n", ret);
+			return ret;
+		}
+		/* sensor_info->start_stream_cmd_sent will be set to true
+		 * 1. in isp4sd_kickoff_stream, if app first send buffer then
+		 * start stream
+		 * 2. in isp_set_stream_buf, if app first start stream, then
+		 * send buffer
+		 * because ISP FW has the requirement, host needs to send buffer
+		 * before send start stream cmd
+		 */
+		if (sensor_info->start_stream_cmd_sent) {
+			sensor_info->status = ISP4SD_START_STATUS_STARTED;
+			output_info->start_status = ISP4SD_START_STATUS_STARTED;
+			dev_dbg(dev, "kickoff stream suc,start cmd sent\n");
+		} else {
+			dev_dbg(dev, "kickoff stream suc,start cmd not sent\n");
+		}
+	} else {
+		dev_dbg(dev, "stream running, no need kickoff\n");
+		output_info->start_status = ISP4SD_START_STATUS_STARTED;
+	}
+
+	dev_dbg(dev, "setup output suc\n");
+	return 0;
+}
+
+static int isp4sd_alloc_meta_buf(struct isp4_subdev *isp_subdev)
+{
+	struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct device *dev = isp_subdev->dev;
+	u32 i;
+
+	/* TODO: check alloc method */
+	for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++) {
+		if (!sensor_info->meta_info_buf[i]) {
+			sensor_info->meta_info_buf[i] =
+				ispif->metainfo_buf_pool[i];
+			if (sensor_info->meta_info_buf[i]) {
+				dev_dbg(dev, "valid %u meta_info_buf ok\n", i);
+			} else {
+				dev_err(dev,
+					"invalid %u meta_info_buf fail\n", i);
+				return -ENOMEM;
+			}
+		}
+	}
+
+	return 0;
+}
+
+static int isp4sd_init_stream(struct isp4_subdev *isp_subdev)
+{
+	struct device *dev = isp_subdev->dev;
+	int ret;
+
+	ret  = isp4sd_setup_fw_mem_pool(isp_subdev);
+	if (ret) {
+		dev_err(dev, "fail to  setup fw mem pool\n");
+		return ret;
+	}
+
+	ret  = isp4sd_alloc_meta_buf(isp_subdev);
+	if (ret) {
+		dev_err(dev, "fail to alloc fw driver shared buf\n");
+		return ret;
+	}
+
+	ret = isp4sd_set_stream_path(isp_subdev);
+	if (ret) {
+		dev_err(dev, "fail to setup stream path\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static void isp4sd_reset_stream_info(struct isp4_subdev *isp_subdev,
+				     struct v4l2_subdev_state *state, u32 pad)
+{
+	struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+	struct v4l2_mbus_framefmt *format = NULL;
+	struct isp4sd_output_info *str_info;
+	int i;
+
+	format = v4l2_subdev_state_get_format(state,
+					      pad,
+					      0);
+
+	if (!format) {
+		pr_err("fail reset stream info for not get format\n");
+
+	} else {
+		memset(format, 0, sizeof(*format));
+		format->code = MEDIA_BUS_FMT_YUYV8_1_5X8;
+	}
+
+	for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++)
+		sensor_info->meta_info_buf[i] = NULL;
+
+	str_info = &sensor_info->output_info;
+	str_info->start_status = ISP4SD_START_STATUS_NOT_START;
+}
+
+static bool isp4sd_is_stream_running(struct isp4_subdev *isp_subdev)
+{
+	struct isp4sd_sensor_info *sif;
+	enum isp4sd_start_status stat;
+
+	sif = &isp_subdev->sensor_info;
+	stat = sif->output_info.start_status;
+	if (stat == ISP4SD_START_STATUS_STARTED)
+		return true;
+
+	return false;
+}
+
+static void isp4sd_reset_camera_info(struct isp4_subdev *isp_subdev,
+				     struct v4l2_subdev_state *state, u32 pad)
+{
+	struct isp4sd_sensor_info *info  = &isp_subdev->sensor_info;
+
+	info->status = ISP4SD_START_STATUS_NOT_START;
+	isp4sd_reset_stream_info(isp_subdev, state, pad);
+
+	info->start_stream_cmd_sent = false;
+}
+
+static int isp4sd_uninit_stream(struct isp4_subdev *isp_subdev,
+				struct v4l2_subdev_state *state, u32 pad)
+{
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct device *dev = isp_subdev->dev;
+	bool running;
+
+	running = isp4sd_is_stream_running(isp_subdev);
+
+	if (running) {
+		dev_dbg(dev, "fail for stream is still running\n");
+		return -EINVAL;
+	}
+
+	isp4sd_reset_camera_info(isp_subdev, state, pad);
+
+	isp4if_clear_cmdq(ispif);
+	return 0;
+}
+
+static void isp4sd_fw_resp_cmd_done(struct isp4_subdev *isp_subdev,
+				    enum isp4if_stream_id stream_id,
+				    struct isp4fw_resp_cmd_done *para)
+{
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct isp4if_cmd_element *ele =
+		isp4if_rm_cmd_from_cmdq(ispif, para->cmd_seq_num, para->cmd_id);
+	struct device *dev = isp_subdev->dev;
+
+	dev_dbg(dev, "stream %d,cmd (0x%08x)(%d),seq %u, ele %p\n",
+		stream_id,
+		para->cmd_id, para->cmd_status, para->cmd_seq_num,
+		ele);
+
+	if (!ele)
+		return;
+
+	if (ele->wq) {
+		dev_dbg(dev, "signal event %p\n", ele->wq);
+		if (ele->wq_cond)
+			*ele->wq_cond = 1;
+		wake_up(ele->wq);
+	}
+
+	kfree(ele);
+}
+
+static struct isp4fw_meta_info *
+isp4sd_get_meta_by_mc(struct isp4_subdev *isp_subdev,
+		      u64 mc)
+{
+	u32 i;
+
+	for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++) {
+		struct isp4if_gpu_mem_info *meta_info_buf =
+				isp_subdev->sensor_info.meta_info_buf[i];
+
+		if (meta_info_buf) {
+			if (mc == meta_info_buf->gpu_mc_addr)
+				return meta_info_buf->sys_addr;
+		}
+	}
+	return NULL;
+};
+
+static struct isp4if_img_buf_node *
+isp4sd_preview_done(struct isp4_subdev *isp_subdev,
+		    struct isp4fw_meta_info *meta)
+{
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct isp4if_img_buf_node *prev = NULL;
+	struct device *dev = isp_subdev->dev;
+
+	if (!meta) {
+		dev_err(dev, "fail bad param for preview done\n");
+		return prev;
+	}
+
+	if (meta->preview.enabled &&
+	    (meta->preview.status == BUFFER_STATUS_SKIPPED ||
+	     meta->preview.status == BUFFER_STATUS_DONE ||
+	     meta->preview.status == BUFFER_STATUS_DIRTY)) {
+		prev = isp4if_dequeue_buffer(ispif);
+		if (!prev)
+			dev_err(dev, "fail null prev buf\n");
+
+	} else if (meta->preview.enabled) {
+		dev_err(dev, "fail bad preview status %u\n",
+			meta->preview.status);
+	}
+
+	return prev;
+}
+
+static void isp4sd_send_meta_info(struct isp4_subdev *isp_subdev,
+				  u64 meta_info_mc)
+{
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct isp4fw_cmd_send_buffer buf_type;
+	struct device *dev = isp_subdev->dev;
+
+	if (isp_subdev->sensor_info.status != ISP4SD_START_STATUS_STARTED) {
+		dev_warn(dev, "not working status %i, meta_info 0x%llx\n",
+			 isp_subdev->sensor_info.status, meta_info_mc);
+		return;
+	}
+
+	if (meta_info_mc) {
+		memset(&buf_type, 0, sizeof(buf_type));
+		buf_type.buffer_type = BUFFER_TYPE_META_INFO;
+		buf_type.buffer.buf_tags = 0;
+		buf_type.buffer.vmid_space.bit.vmid = 0;
+		buf_type.buffer.vmid_space.bit.space = ADDR_SPACE_TYPE_GPU_VA;
+		isp4if_split_addr64(meta_info_mc,
+				    &buf_type.buffer.buf_base_a_lo,
+				    &buf_type.buffer.buf_base_a_hi);
+
+		buf_type.buffer.buf_size_a = ISP4SD_META_BUF_SIZE;
+		if (isp4if_send_command(ispif, CMD_ID_SEND_BUFFER,
+					&buf_type, sizeof(buf_type))) {
+			dev_err(dev, "fail send meta_info 0x%llx\n",
+				meta_info_mc);
+		} else {
+			dev_dbg(dev, "resend meta_info 0x%llx\n", meta_info_mc);
+		}
+	}
+}
+
+static void isp4sd_fw_resp_frame_done(struct isp4_subdev *isp_subdev,
+				      enum isp4if_stream_id stream_id,
+				      struct isp4fw_resp_param_package *para)
+{
+	struct isp4if_img_buf_node *prev = NULL;
+	struct device *dev = isp_subdev->dev;
+	struct isp4fw_meta_info *meta;
+	u64 mc = 0;
+
+	mc = isp4if_join_addr64(para->package_addr_lo, para->package_addr_hi);
+	meta = isp4sd_get_meta_by_mc(isp_subdev, mc);
+	if (mc == 0 || !meta) {
+		dev_err(dev, "fail to get meta from mc %llx\n", mc);
+		return;
+	}
+
+	dev_dbg(dev, "ts:%llu,streamId:%d,poc:%u,preview_en:%u,(%i)\n",
+		ktime_get_ns(), stream_id, meta->poc,
+		meta->preview.enabled,
+		meta->preview.status);
+
+	prev = isp4sd_preview_done(isp_subdev, meta);
+
+	isp4if_dealloc_buffer_node(prev);
+
+	if (isp_subdev->sensor_info.status == ISP4SD_START_STATUS_STARTED)
+		isp4sd_send_meta_info(isp_subdev, mc);
+
+	dev_dbg(dev, "stream_id:%d, status:%d\n", stream_id,
+		isp_subdev->sensor_info.status);
+}
+
+static void isp4sd_fw_resp_func(struct isp4_subdev *isp_subdev,
+				enum isp4if_stream_id stream_id)
+{
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct device *dev = isp_subdev->dev;
+	struct isp4fw_resp resp;
+
+	if (ispif->status < ISP4IF_STATUS_FW_RUNNING)
+		return;
+
+	while (true) {
+		s32 ret;
+
+		ret = isp4if_f2h_resp(ispif, stream_id, &resp);
+		if (ret)
+			break;
+
+		switch (resp.resp_id) {
+		case RESP_ID_CMD_DONE:
+			isp4sd_fw_resp_cmd_done(isp_subdev, stream_id,
+						&resp.param.cmd_done);
+			break;
+		case RESP_ID_NOTI_FRAME_DONE:
+			isp4sd_fw_resp_frame_done(isp_subdev, stream_id,
+						  &resp.param.frame_done);
+			break;
+		default:
+			dev_err(dev, "-><- fail respid (0x%x)\n",
+				resp.resp_id);
+			break;
+		}
+	}
+}
+
+static s32 isp4sd_fw_resp_thread_wrapper(void *context)
+{
+	struct isp4_subdev_thread_param *para = context;
+	struct isp4sd_thread_handler *thread_ctx;
+	enum isp4if_stream_id stream_id;
+
+	struct isp4_subdev *isp_subdev;
+	struct device *dev;
+	u64 timeout;
+
+	if (!para)
+		return -EINVAL;
+
+	isp_subdev = para->isp_subdev;
+	dev = isp_subdev->dev;
+
+	switch (para->idx) {
+	case 0:
+		stream_id = ISP4IF_STREAM_ID_GLOBAL;
+		break;
+	case 1:
+		stream_id = ISP4IF_STREAM_ID_1;
+		break;
+	default:
+		dev_err(dev, "fail invalid %d\n", para->idx);
+		return -EINVAL;
+	}
+
+	thread_ctx = &isp_subdev->fw_resp_thread[para->idx];
+
+	thread_ctx->wq_cond = 0;
+	mutex_init(&thread_ctx->mutex);
+	init_waitqueue_head(&thread_ctx->waitq);
+	timeout = msecs_to_jiffies(ISP4SD_WAIT_RESP_IRQ_TIMEOUT);
+
+	dev_dbg(dev, "[%u] started\n", para->idx);
+
+	while (true) {
+		wait_event_interruptible_timeout(thread_ctx->waitq,
+						 thread_ctx->wq_cond != 0,
+						 timeout);
+		thread_ctx->wq_cond = 0;
+
+		if (kthread_should_stop()) {
+			dev_dbg(dev, "[%u] quit\n", para->idx);
+			break;
+		}
+
+		mutex_lock(&thread_ctx->mutex);
+		isp4sd_fw_resp_func(isp_subdev, stream_id);
+		mutex_unlock(&thread_ctx->mutex);
+	}
+
+	mutex_destroy(&thread_ctx->mutex);
+
+	return 0;
+}
+
+static int isp4sd_start_resp_proc_threads(struct isp4_subdev *isp_subdev)
+{
+	struct device *dev = isp_subdev->dev;
+	int i;
+
+	for (i = 0; i < ISP4SD_MAX_FW_RESP_STREAM_NUM; i++) {
+		struct isp4sd_thread_handler *thread_ctx =
+				&isp_subdev->fw_resp_thread[i];
+
+		isp_subdev->isp_resp_para[i].idx = i;
+		isp_subdev->isp_resp_para[i].isp_subdev = isp_subdev;
+
+		thread_ctx->thread = kthread_run(isp4sd_fw_resp_thread_wrapper,
+						 &isp_subdev->isp_resp_para[i],
+						 "amd_isp4_thread");
+		if (IS_ERR(thread_ctx->thread)) {
+			dev_err(dev, "create thread [%d] fail\n", i);
+			return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+
+static int isp4sd_stop_resp_proc_threads(struct isp4_subdev *isp_subdev)
+{
+	int i;
+
+	for (i = 0; i < ISP4SD_MAX_FW_RESP_STREAM_NUM; i++) {
+		struct isp4sd_thread_handler *thread_ctx =
+				&isp_subdev->fw_resp_thread[i];
+
+		if (thread_ctx->thread) {
+			kthread_stop(thread_ctx->thread);
+			thread_ctx->thread = NULL;
+		}
+	}
+
+	return 0;
+}
+
+static u32 isp4sd_get_started_stream_count(struct isp4_subdev *isp_subdev)
+{
+	u32 cnt = 0;
+
+	if (isp_subdev->sensor_info.status == ISP4SD_START_STATUS_STARTED)
+		cnt++;
+	return cnt;
+}
+
+static int isp4sd_pwroff_and_deinit(struct isp4_subdev *isp_subdev)
+{
+	struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+	unsigned int perf_state = ISP4SD_PERFORMANCE_STATE_LOW;
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+
+	struct device *dev = isp_subdev->dev;
+	u32 cnt;
+	int ret;
+
+	mutex_lock(&isp_subdev->ops_mutex);
+
+	if (sensor_info->status == ISP4SD_START_STATUS_STARTED) {
+		dev_err(dev, "fail for stream still running\n");
+		mutex_unlock(&isp_subdev->ops_mutex);
+		return -EINVAL;
+	}
+
+	sensor_info->status = ISP4SD_START_STATUS_NOT_START;
+	cnt = isp4sd_get_started_stream_count(isp_subdev);
+	if (cnt > 0) {
+		dev_dbg(dev, "no need power off isp_subdev\n");
+		mutex_unlock(&isp_subdev->ops_mutex);
+		return 0;
+	}
+
+	isp4if_stop(ispif);
+
+	ret = dev_pm_genpd_set_performance_state(dev, perf_state);
+	if (ret)
+		dev_err(dev,
+			"fail to set isp_subdev performance state %u,ret %d\n",
+			perf_state, ret);
+	isp4sd_stop_resp_proc_threads(isp_subdev);
+	dev_dbg(dev, "isp_subdev stop resp proc streads suc");
+	/* hold ccpu reset */
+	isp4hw_wreg(isp_subdev->mmio, ISP_SOFT_RESET, 0x0);
+	isp4hw_wreg(isp_subdev->mmio, ISP_POWER_STATUS, 0);
+	ret = pm_runtime_put_sync(dev);
+	if (ret)
+		dev_err(dev, "power off isp_subdev fail %d\n", ret);
+	else
+		dev_dbg(dev, "power off isp_subdev suc\n");
+
+	ispif->status = ISP4IF_STATUS_PWR_OFF;
+	isp4if_clear_cmdq(ispif);
+	isp4sd_module_enable(isp_subdev, false);
+
+	msleep(20);
+
+	mutex_unlock(&isp_subdev->ops_mutex);
+
+	return 0;
+}
+
+static int isp4sd_pwron_and_init(struct isp4_subdev *isp_subdev)
+{
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct device *dev = isp_subdev->dev;
+	int ret;
+
+	if (ispif->status == ISP4IF_STATUS_FW_RUNNING) {
+		dev_dbg(dev, "camera already opened, do nothing\n");
+		return 0;
+	}
+
+	mutex_lock(&isp_subdev->ops_mutex);
+
+	isp4sd_module_enable(isp_subdev, true);
+
+	isp_subdev->sensor_info.start_stream_cmd_sent = false;
+	isp_subdev->sensor_info.buf_sent_cnt = 0;
+
+	if (ispif->status < ISP4IF_STATUS_PWR_ON) {
+		unsigned int perf_state = ISP4SD_PERFORMANCE_STATE_HIGH;
+
+		ret = pm_runtime_resume_and_get(dev);
+		if (ret) {
+			dev_err(dev, "fail to power on isp_subdev ret %d\n",
+				ret);
+			goto err_unlock_and_close;
+		}
+
+		/* ISPPG ISP Power Status */
+		isp4hw_wreg(isp_subdev->mmio, ISP_POWER_STATUS, 0x7FF);
+		ret = dev_pm_genpd_set_performance_state(dev, perf_state);
+		if (ret) {
+			dev_err(dev,
+				"fail to set performance state %u, ret %d\n",
+				perf_state, ret);
+			goto err_unlock_and_close;
+		}
+
+		ispif->status = ISP4IF_STATUS_PWR_ON;
+
+		if (isp4sd_start_resp_proc_threads(isp_subdev)) {
+			dev_err(dev, "isp_start_resp_proc_threads fail");
+			goto err_unlock_and_close;
+		} else {
+			dev_dbg(dev, "create resp threads ok");
+		}
+	}
+
+	isp_subdev->sensor_info.start_stream_cmd_sent = false;
+	isp_subdev->sensor_info.buf_sent_cnt = 0;
+
+	ret = isp4if_start(ispif);
+	if (ret) {
+		dev_err(dev, "fail to start isp_subdev interface\n");
+		goto err_unlock_and_close;
+	}
+
+	mutex_unlock(&isp_subdev->ops_mutex);
+	return 0;
+err_unlock_and_close:
+	mutex_unlock(&isp_subdev->ops_mutex);
+	isp4sd_pwroff_and_deinit(isp_subdev);
+	return -EINVAL;
+}
+
+static int isp4sd_stop_stream(struct isp4_subdev *isp_subdev,
+			      struct v4l2_subdev_state *state, u32 pad)
+{
+	struct isp4sd_output_info *output_info =
+			&isp_subdev->sensor_info.output_info;
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct device *dev = isp_subdev->dev;
+	int ret = 0;
+
+	dev_dbg(dev, "status %i\n", output_info->start_status);
+	mutex_lock(&isp_subdev->ops_mutex);
+
+	if (output_info->start_status == ISP4SD_START_STATUS_STARTED) {
+		struct isp4fw_cmd_enable_out_ch cmd_ch_disable;
+
+		cmd_ch_disable.ch = ISP_PIPE_OUT_CH_PREVIEW;
+		cmd_ch_disable.is_enable = false;
+		ret = isp4if_send_command_sync(ispif,
+					       CMD_ID_ENABLE_OUT_CHAN,
+					       &cmd_ch_disable,
+					       sizeof(cmd_ch_disable),
+					       ISP4SD_FW_CMD_TIMEOUT_IN_MS);
+		if (ret)
+			dev_err(dev, "fail to disable stream\n");
+		else
+			dev_dbg(dev, "wait disable stream suc\n");
+
+		ret = isp4if_send_command_sync(ispif, CMD_ID_STOP_STREAM,
+					       NULL,
+					       0,
+					       ISP4SD_FW_CMD_TIMEOUT_IN_MS);
+		if (ret)
+			dev_err(dev, "fail to stop steam\n");
+		else
+			dev_dbg(dev, "wait stop stream suc\n");
+	}
+
+	isp4if_clear_bufq(ispif);
+
+	output_info->start_status = ISP4SD_START_STATUS_NOT_START;
+	isp4sd_reset_stream_info(isp_subdev, state, pad);
+
+	mutex_unlock(&isp_subdev->ops_mutex);
+
+	isp4sd_uninit_stream(isp_subdev, state, pad);
+
+	return ret;
+}
+
+static int isp4sd_start_stream(struct isp4_subdev *isp_subdev,
+			       struct v4l2_subdev_state *state, u32 pad)
+{
+	struct isp4sd_output_info *output_info =
+			&isp_subdev->sensor_info.output_info;
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct device *dev = isp_subdev->dev;
+	int ret;
+
+	mutex_lock(&isp_subdev->ops_mutex);
+
+	if (ispif->status != ISP4IF_STATUS_FW_RUNNING) {
+		mutex_unlock(&isp_subdev->ops_mutex);
+		dev_err(dev, "fail, bad fsm %d", ispif->status);
+		return -EINVAL;
+	}
+
+	ret = isp4sd_init_stream(isp_subdev);
+
+	if (ret) {
+		dev_err(dev, "fail to init isp_subdev stream\n");
+		ret = -EINVAL;
+		goto unlock_and_check_ret;
+	}
+
+	if (output_info->start_status == ISP4SD_START_STATUS_STARTED) {
+		ret = 0;
+		dev_dbg(dev, "stream started, do nothing\n");
+		goto unlock_and_check_ret;
+	} else if (output_info->start_status ==
+		   ISP4SD_START_STATUS_START_FAIL) {
+		ret = -EINVAL;
+		dev_err(dev, "stream  fail to start before\n");
+		goto unlock_and_check_ret;
+	}
+
+	if (isp4sd_setup_output(isp_subdev, state, pad)) {
+		dev_err(dev, "fail to setup output\n");
+		ret = -EINVAL;
+	} else {
+		ret = 0;
+		dev_dbg(dev, "suc to setup out\n");
+	}
+unlock_and_check_ret:
+	mutex_unlock(&isp_subdev->ops_mutex);
+	if (ret) {
+		isp4sd_stop_stream(isp_subdev, state, pad);
+		dev_err(dev, "start stream fail\n");
+	} else {
+		dev_dbg(dev, "start stream suc\n");
+	}
+
+	return ret;
+}
+
+static int isp4sd_subdev_pre_streamon(struct v4l2_subdev *sd, u32 flags)
+{
+	struct isp4_subdev *isp_subdev = to_isp4_subdev(sd);
+	u64 phy_bit_rate = isp_subdev->phy_link_freq * isp_subdev->phy_num_data_lanes / HZ_PER_MHZ;
+	u32 num_data_lanes = isp_subdev->phy_num_data_lanes;
+	u32 phy_id = isp_subdev->phy_id;
+	int ret;
+
+	ret = isp4phy_start(isp_subdev->dev,
+			    isp_subdev->isp_phy_mmio, phy_id,
+			    phy_bit_rate, num_data_lanes);
+	if (ret) {
+		dev_err(isp_subdev->dev,
+			"fail start phy,lane %d id %u bitrate %llu, %d\n",
+			num_data_lanes, phy_id, phy_bit_rate, ret);
+		return ret;
+	}
+
+	dev_dbg(isp_subdev->dev, "start phy suc,lane %d id %u bit_rate %llu\n",
+		num_data_lanes, phy_id, phy_bit_rate);
+
+	return ret;
+}
+
+static int isp4sd_subdev_post_streamoff(struct v4l2_subdev *sd)
+{
+	struct isp4_subdev *isp_subdev = to_isp4_subdev(sd);
+	int ret;
+
+	dev_dbg(isp_subdev->dev, "stopping phy %u\n", isp_subdev->phy_id);
+	ret = isp4phy_stop(isp_subdev->isp_phy_mmio,
+			   isp_subdev->phy_id);
+	if (ret)
+		dev_err(isp_subdev->dev, "fail to stop the Phy:%d", ret);
+
+	return ret;
+}
+
+static int isp4sd_set_power(struct v4l2_subdev *sd, int on)
+{
+	struct isp4_subdev *ispsd = to_isp4_subdev(sd);
+
+	if (on)
+		return isp4sd_pwron_and_init(ispsd);
+	else
+		return isp4sd_pwroff_and_deinit(ispsd);
+};
+
+static const struct v4l2_subdev_core_ops isp4sd_core_ops = {
+	.s_power = isp4sd_set_power,
+};
+
+static const struct v4l2_subdev_video_ops isp4sd_video_ops = {
+	.s_stream = v4l2_subdev_s_stream_helper,
+	.pre_streamon = isp4sd_subdev_pre_streamon,
+	.post_streamoff = isp4sd_subdev_post_streamoff,
+};
+
+static int isp4sd_set_pad_format(struct v4l2_subdev *sd,
+				 struct v4l2_subdev_state *sd_state,
+				 struct v4l2_subdev_format *fmt)
+{
+	struct isp4sd_output_info *steam_info =
+		&(to_isp4_subdev(sd)->sensor_info.output_info);
+	struct v4l2_mbus_framefmt *format;
+
+	format = v4l2_subdev_state_get_format(sd_state, fmt->pad);
+
+	if (!format) {
+		dev_err(sd->dev, "fail to get state format\n");
+		return -EINVAL;
+	}
+
+	*format = fmt->format;
+	switch (format->code) {
+	case MEDIA_BUS_FMT_YUYV8_1_5X8:
+		steam_info->image_size = format->width * format->height * 3 / 2;
+		break;
+	case MEDIA_BUS_FMT_YUYV8_1X16:
+		steam_info->image_size = format->width * format->height * 2;
+		break;
+	default:
+		steam_info->image_size = 0;
+		break;
+	}
+	if (!steam_info->image_size) {
+		dev_err(sd->dev,
+			"fail set pad format,code 0x%x,width %u, height %u\n",
+			format->code, format->width, format->height);
+		return -EINVAL;
+	}
+	dev_dbg(sd->dev,
+		"set pad format suc, code:%x w:%u h:%u size:%u\n", format->code,
+		format->width, format->height, steam_info->image_size);
+
+	return 0;
+}
+
+static int isp4sd_enable_streams(struct v4l2_subdev *sd,
+				 struct v4l2_subdev_state *state, u32 pad,
+				 u64 streams_mask)
+{
+	struct isp4_subdev *ispsd = to_isp4_subdev(sd);
+
+	return isp4sd_start_stream(ispsd, state, pad);
+}
+
+static int isp4sd_disable_streams(struct v4l2_subdev *sd,
+				  struct v4l2_subdev_state *state, u32 pad,
+				  u64 streams_mask)
+{
+	struct isp4_subdev *ispsd = to_isp4_subdev(sd);
+
+	return isp4sd_stop_stream(ispsd, state, pad);
+}
+
+static const struct v4l2_subdev_pad_ops isp4sd_pad_ops = {
+	.enum_mbus_code	= isp4sd_enum_mbus_code,
+	.get_fmt = v4l2_subdev_get_fmt,
+	.set_fmt = isp4sd_set_pad_format,
+	.enable_streams = isp4sd_enable_streams,
+	.disable_streams = isp4sd_disable_streams,
+};
+
+static const struct v4l2_subdev_ops isp4sd_subdev_ops = {
+	.core = &isp4sd_core_ops,
+	.video = &isp4sd_video_ops,
+	.pad = &isp4sd_pad_ops,
+};
+
+static int isp4sd_sdev_link_validate(struct media_link *link)
+{
+	return 0;
+}
+
+static const struct media_entity_operations isp4sd_sdev_ent_ops = {
+	.link_validate = isp4sd_sdev_link_validate,
+};
+
+int isp4sd_init(struct isp4_subdev *isp_subdev,
+		struct v4l2_device *v4l2_dev,
+		void *amdgpu_dev)
+{
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+	struct isp4sd_sensor_info *sensor_info;
+	struct device *dev = v4l2_dev->dev;
+	int ret;
+
+	isp_subdev->dev = dev;
+	isp_subdev->amdgpu_dev = amdgpu_dev;
+	v4l2_subdev_init(&isp_subdev->sdev, &isp4sd_subdev_ops);
+	isp_subdev->sdev.owner = THIS_MODULE;
+	isp_subdev->sdev.dev = dev;
+	snprintf(isp_subdev->sdev.name, sizeof(isp_subdev->sdev.name), "%s",
+		 dev_name(dev));
+
+	isp_subdev->sdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
+	isp_subdev->sdev.entity.name = isp4sd_entity_name;
+	isp_subdev->sdev.entity.function = MEDIA_ENT_F_PROC_VIDEO_ISP;
+	isp_subdev->sdev.entity.ops = &isp4sd_sdev_ent_ops;
+	isp_subdev->sdev_pad[0].flags = MEDIA_PAD_FL_SINK;
+	isp_subdev->sdev_pad[1].flags = MEDIA_PAD_FL_SOURCE;
+	ret = media_entity_pads_init(&isp_subdev->sdev.entity, 2,
+				     isp_subdev->sdev_pad);
+	if (ret) {
+		dev_err(dev, "fail to init isp4 subdev entity pad %d\n", ret);
+		return ret;
+	}
+	ret = v4l2_subdev_init_finalize(&isp_subdev->sdev);
+	if (ret < 0) {
+		dev_err(dev, "fail to init finalize isp4 subdev %d\n",
+			ret);
+		return ret;
+	}
+	ret = v4l2_device_register_subdev(v4l2_dev, &isp_subdev->sdev);
+	if (ret) {
+		dev_err(dev, "fail to register isp4 subdev to V4L2 device %d\n",
+			ret);
+		goto err_media_clean_up;
+	}
+
+	sensor_info = &isp_subdev->sensor_info;
+
+	isp4if_init(ispif, dev, amdgpu_dev, isp_subdev->mmio);
+
+	mutex_init(&isp_subdev->ops_mutex);
+	sensor_info->start_stream_cmd_sent = false;
+	sensor_info->status = ISP4SD_START_STATUS_NOT_START;
+
+	/* create ISP enable gpio control */
+	isp_subdev->enable_gpio = devm_gpiod_get(isp_subdev->dev,
+						 "enable_isp",
+						 GPIOD_OUT_LOW);
+	if (IS_ERR(isp_subdev->enable_gpio)) {
+		dev_err(dev, "fail to get gpiod %d\n", ret);
+		media_entity_cleanup(&isp_subdev->sdev.entity);
+		return PTR_ERR(isp_subdev->enable_gpio);
+	}
+
+	isp_subdev->host2fw_seq_num = 1;
+	ispif->status = ISP4IF_STATUS_PWR_OFF;
+
+	if (ret)
+		goto err_media_clean_up;
+	return ret;
+
+err_media_clean_up:
+	media_entity_cleanup(&isp_subdev->sdev.entity);
+	return ret;
+}
+
+void isp4sd_deinit(struct isp4_subdev *isp_subdev)
+{
+	struct isp4_interface *ispif = &isp_subdev->ispif;
+
+	media_entity_cleanup(&isp_subdev->sdev.entity);
+	isp4if_deinit(ispif);
+	isp4sd_module_enable(isp_subdev, false);
+
+	ispif->status = ISP4IF_STATUS_PWR_OFF;
+}
drivers/media/platform/amd/isp4/isp4_subdev.h new file +145 -0
new file mode 100644
index 000000000000..bcbb93dce18f
@@ -0,0 +1,145 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_CONTEXT_H_
+#define _ISP4_CONTEXT_H_
+
+#include <linux/delay.h>
+#include <linux/firmware.h>
+#include <linux/platform_device.h>
+#include <linux/uaccess.h>
+#include <linux/types.h>
+#include <linux/debugfs.h>
+#include <media/v4l2-device.h>
+#include <media/videobuf2-vmalloc.h>
+
+#include "isp4_fw_cmd_resp.h"
+#include "isp4_hw_reg.h"
+#include "isp4_interface.h"
+
+/*
+ * one is for none sesnor specefic response which is not used now
+ * another is for sensor specific response
+ */
+#define ISP4SD_MAX_FW_RESP_STREAM_NUM 2
+
+/*
+ * cmd used to register frame done callback, parameter is
+ * struct isp4sd_register_framedone_cb_param *
+ * when a image buffer is filled by ISP, ISP will call the registered callback.
+ * callback func prototype is isp4sd_framedone_cb, cb_ctx can be anything
+ * provided by caller which will be provided back as the first parameter of the
+ * callback function.
+ * both cb_func and cb_ctx are provide by caller, set cb_func to NULL to
+ * unregister the callback
+ */
+
+/* used to indicate the ISP status*/
+enum isp4sd_status {
+	ISP4SD_STATUS_PWR_OFF,
+	ISP4SD_STATUS_PWR_ON,
+	ISP4SD_STATUS_FW_RUNNING,
+	ISP4SD_STATUS_MAX
+};
+
+/*used to indicate the status of sensor, output stream */
+enum isp4sd_start_status {
+	ISP4SD_START_STATUS_NOT_START,
+	ISP4SD_START_STATUS_STARTED,
+	ISP4SD_START_STATUS_START_FAIL,
+};
+
+struct isp4sd_img_buf_node {
+	struct list_head node;
+	struct isp4if_img_buf_info buf_info;
+};
+
+/* this is isp output after processing bayer raw input from sensor */
+struct isp4sd_output_info {
+	enum isp4sd_start_status start_status;
+	u32 image_size;
+};
+
+/* This struct represents the sensor info which is input or source of ISP,
+ * meta_info_buf is the buffer store the fw to driver metainfo response
+ * status is the sensor status
+ * output_info is the isp output info after ISP processing the sensor input,
+ * start_stream_cmd_sent mean if CMD_ID_START_STREAM has sent to fw.
+ * buf_sent_cnt is buffer count app has sent to receive the images
+ */
+struct isp4sd_sensor_info {
+	struct isp4if_gpu_mem_info *
+		meta_info_buf[ISP4IF_MAX_STREAM_META_BUF_COUNT];
+	struct isp4sd_output_info output_info;
+	enum isp4sd_start_status status;
+	bool start_stream_cmd_sent;
+	u32 buf_sent_cnt;
+};
+
+/*
+ * Thread created by driver to receive fw response
+ * thread will be wakeup by fw to driver response interrupt
+ */
+struct isp4sd_thread_handler {
+	struct task_struct *thread;
+	struct mutex mutex; /* mutex */
+	wait_queue_head_t waitq;
+	int wq_cond;
+};
+
+struct isp4_subdev_thread_param {
+	u32 idx;
+	struct isp4_subdev *isp_subdev;
+};
+
+struct isp4_subdev {
+	struct v4l2_subdev sdev;
+	struct isp4_interface ispif;
+
+	/*
+	 * sdev_pad[0] is sink pad connected to sensor
+	 * sdev_pad[0] is sink pad connected to sensor
+	 * sdev_pad[1] is sourc pad connected v4l2 video device
+	 */
+	struct media_pad sdev_pad[2];
+
+	enum isp4sd_status isp_status;
+	struct mutex ops_mutex; /* ops_mutex */
+
+	/* Used to store fw cmds sent to FW whose response driver needs
+	 * to wait for
+	 */
+	struct isp4sd_thread_handler
+		fw_resp_thread[ISP4SD_MAX_FW_RESP_STREAM_NUM];
+
+	u32 host2fw_seq_num;
+
+	struct isp4sd_sensor_info sensor_info;
+
+	/* gpio descriptor */
+	struct gpio_desc *enable_gpio;
+	struct device *dev;
+	void *amdgpu_dev;
+	void __iomem *mmio;
+	struct isp4_subdev_thread_param
+		isp_resp_para[ISP4SD_MAX_FW_RESP_STREAM_NUM];
+#ifdef CONFIG_DEBUG_FS
+	struct dentry *debugfs_dir;
+	bool enable_fw_log;
+	char *fw_log_output;
+#endif
+	u32 phy_num_data_lanes;
+	u32 phy_id;
+	u64 phy_link_freq;
+
+	void __iomem *isp_phy_mmio;
+};
+
+int isp4sd_init(struct isp4_subdev *isp_subdev,
+		struct v4l2_device *v4l2_dev,
+		void *amdgpu_dev);
+void isp4sd_deinit(struct isp4_subdev *isp_subdev);
+
+#endif
author maintainer
#39 lore raw

Re: [PATCH v2 5/8] media: platform: amd: isp4 subdev and firmware loading handling added

Thanks Mario, add some comments

Quoted 79 lines · Mario Limonciello
Quoted 78 lines · Bin Du

Isp4 sub-device is implementing v4l2 sub-device interface. It has one capture video node, and supports only preview stream. It manages firmware states, stream configuration and mipi phy configuration. This change also adds interrupt handling and notification for isp firmware to isp-subdevice.

---
  drivers/media/platform/amd/isp4/Makefile      |    3 +- drivers/media/platform/amd/isp4/isp4.c        |  267 +++- drivers/media/platform/amd/isp4/isp4.h        |    4 +- drivers/media/platform/amd/isp4/isp4_subdev.c | 1192 +++++++++++++++++ drivers/media/platform/amd/isp4/isp4_subdev.h |  145 ++

  5 files changed, 1601 insertions(+), 10 deletions(-)

  create mode 100644 drivers/media/platform/amd/isp4/isp4_subdev.c create mode 100644 drivers/media/platform/amd/isp4/isp4_subdev.h

drivers/media/platform/amd/isp4/Makefile +0 -0
index c0166f954516..52defc06189e 100644
@@ -3,9 +3,10 @@
   # Copyright (C) 2025 Advanced Micro Devices, Inc.

  obj-$(CONFIG_AMD_ISP4) += amd_capture.o

drivers/media/platform/amd/isp4/Makefile +1 -1
-amd_capture-objs := isp4.o    \
+amd_capture-objs := isp4_subdev.o \

              isp4_phy.o \
              isp4_interface.o \
+            isp4.o    \
              isp4_hw.o    \
  ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4

drivers/media/platform/amd/isp4/isp4.c +29 -2
index d0be90c5ec3b..c1aca2bd35e3 100644
@@ -5,13 +5,20 @@
   #include <linux/pm_runtime.h>
   #include <linux/vmalloc.h>
+#include <media/v4l2-fwnode.h>
   #include <media/v4l2-ioctl.h>
-#include "isp4.h"
+#include "amdgpu_object.h"
-#define VIDEO_BUF_NUM 5
+#include "isp4.h"
+#include "isp4_hw.h"
   #define ISP4_DRV_NAME "amd_isp_capture"
+#define ISP4_FW_RESP_RB_IRQ_STATUS_MASK \
+    (ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT9_INT_MASK  | \
+     ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT10_INT_MASK | \
+     ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT11_INT_MASK | \
+     ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT12_INT_MASK)
   /* interrupt num */
   static const u32 isp4_ringbuf_interrupt_num[] = {
@@ -24,23 +31,233 @@ static const u32 isp4_ringbuf_interrupt_num[] = {
   #define to_isp4_device(dev) \
       ((struct isp4_device *)container_of(dev, struct isp4_device, v4l2_dev))
+static int isp4_create_links(struct isp4_device *isp4_dev,
+                 struct v4l2_subdev *sensor_sdev)
+{
+    struct v4l2_subdev *isp4_sdev = &isp4_dev->isp_sdev.sdev;
+    struct device *dev = &isp4_dev->pdev->dev;
+    int ret;
+
+    ret = media_create_pad_link(&sensor_sdev->entity,
+                    0, &isp4_sdev->entity, 0,
+                    MEDIA_LNK_FL_ENABLED |
+                    MEDIA_LNK_FL_IMMUTABLE);
+    if (ret)
+        dev_err(dev, "create sensor to isp link fail:%d\n", ret);
+
+    return ret;
+}
+
+static int isp4_register_subdev_and_create_links(struct isp4_device 
 *isp_dev,
+                         struct v4l2_subdev *sdev)
+{
Quoted 8 lines

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 59

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.c +6 -0
+    struct device *dev = &isp_dev->pdev->dev;
+    int ret;
+
+    ret = isp4_create_links(isp_dev, sdev);
+    if (ret)
+        dev_err(dev, "fail create isp link:%d\n", ret);

You're discarding ret here.  Shouldn't you return ret in the failure path?

Even if it fails, some Apps which doesn't depend on the media link can still work, so just print error message and ignore it so at least these Apps can still work, do you think it acceptible?>

Quoted 8 lines

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 64

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c +5 -0
+
+    ret = v4l2_device_register_subdev_nodes(&isp_dev->v4l2_dev);
+    if (ret != 0) {
+        dev_warn(dev, "register subdev as nodes fail:%d\n", ret);
+        ret = 0;

This isn't fatal?

This call is just expose subdev node to user space, even if it fails, some Apps can still work, so just print error message and ignore it so at least these Apps can still work, do you think it acceptible?>> +    }

Quoted 13 lines
Quoted 13 lines
drivers/media/platform/amd/isp4/isp4.c +12 -0
+
+    return ret;
+}
+
+static int isp4_camera_sensor_bound(struct v4l2_async_notifier 
 *notifier,
+                    struct v4l2_subdev *sensor_sdev,
+                    struct v4l2_async_connection *asd)
+{
+    struct isp4_device *isp_dev = to_isp4_device(notifier->v4l2_dev);
+    struct device *dev = &isp_dev->pdev->dev;
+    int ret;
+
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 83

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.c +6 -0
+    ret = isp4_register_subdev_and_create_links(isp_dev, sensor_sdev);
+    if (ret)
+        dev_err(dev, "register sensor subdev fail:%d\n",
+            ret);
+    else
+        dev_dbg(dev, "register sensor subdev suc\n");

Probably can drop this now

I'd prefer to keeping this for quickly identifying possible camera open issue, do you think it acceptible?

Quoted 78 lines
Quoted 78 lines
drivers/media/platform/amd/isp4/isp4.c +72 -1
+    return ret;
+}
+
+static void isp4_camera_sensor_unbind(struct v4l2_async_notifier 
 *notifier,
+                      struct v4l2_subdev *sensor_sdev,
+                      struct v4l2_async_connection *asd)
+{
+}
+
+static const struct v4l2_async_notifier_operations 
 isp4_camera_sensor_ops = {
+    .bound = isp4_camera_sensor_bound,
+    .unbind = isp4_camera_sensor_unbind,
+};
+
+static void isp4_wake_up_resp_thread(struct isp4_subdev *isp, u32 index)
+{
+    if (isp && index < ISP4SD_MAX_FW_RESP_STREAM_NUM) {
+        struct isp4sd_thread_handler *thread_ctx =
+                &isp->fw_resp_thread[index];
+
+        thread_ctx->wq_cond = 1;
+        wake_up_interruptible(&thread_ctx->waitq);
+    }
+}
+
+static void isp4_resp_interrupt_notify(struct isp4_subdev *isp, u32 
 intr_status)
+{
+    bool wake = (isp->ispif.status == ISP4IF_STATUS_FW_RUNNING);
+
+    u32 intr_ack = 0;
+
+    /* global response */
+    if (intr_status &
+        ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT12_INT_MASK) {
+        if (wake)
+            isp4_wake_up_resp_thread(isp, 0);
+
+        intr_ack |= ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT12_ACK_MASK;
+    }
+
+    /* stream 1 response */
+    if (intr_status &
+        ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT9_INT_MASK) {
+        if (wake)
+            isp4_wake_up_resp_thread(isp, 1);
+
+        intr_ack |= ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT9_ACK_MASK;
+    }
+
+    /* stream 2 response */
+    if (intr_status &
+        ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT10_INT_MASK) {
+        if (wake)
+            isp4_wake_up_resp_thread(isp, 2);
+
+        intr_ack |= ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT10_ACK_MASK;
+    }
+
+    /* stream 3 response */
+    if (intr_status &
+        ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT11_INT_MASK) {
+        if (wake)
+            isp4_wake_up_resp_thread(isp, 3);
+
+        intr_ack |= ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT11_ACK_MASK;
+    }
+
+    /* clear ISP_SYS interrupts */
+    isp4hw_wreg(ISP4_GET_ISP_REG_BASE(isp), ISP_SYS_INT0_ACK, intr_ack);
+}
+
   static irqreturn_t isp4_irq_handler(int irq, void *arg)
   {
-    struct isp4_device *isp_dev = dev_get_drvdata((struct device *)arg);
+    struct isp4_device *isp_dev = dev_get_drvdata(arg);
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 163

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.c +4 -1
+    struct isp4_subdev *isp = NULL;
+    u32 isp_sys_irq_status = 0x0;
+    u32 r1;
       if (!isp_dev)
-        goto error_drv_data;
+        return IRQ_HANDLED;

As mentioned in the first patch, avoid ping ponging the code.

Thanks, will remove the code ping ponging in next patch>> +

Quoted 9 lines

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 34

Quoted 8 lines
drivers/media/platform/amd/isp4/isp4.c +7 -1
+    isp = &isp_dev->isp_sdev;
+    /* check ISP_SYS interrupts status */
+    r1 = isp4hw_rreg(ISP4_GET_ISP_REG_BASE(isp), ISP_SYS_INT0_STATUS);
+
+    isp_sys_irq_status = r1 & ISP4_FW_RESP_RB_IRQ_STATUS_MASK;
+
+    isp4_resp_interrupt_notify(isp, isp_sys_irq_status);
-error_drv_data:

As mentioned in the first patch, avoid ping ponging the code.

Thanks, will remove the code ping ponging in next patch>>       return IRQ_HANDLED;

Quoted 148 lines
Quoted 148 lines
drivers/media/platform/amd/isp4/isp4.c +108 -1
   }
+static int isp4_parse_fwnode_init_async_nf(struct isp4_device *isp_dev)
+{
+    struct isp4_subdev *isp_sdev = &isp_dev->isp_sdev;
+    struct device *dev = &isp_dev->pdev->dev;
+    struct v4l2_fwnode_endpoint bus_cfg = {
+        .bus_type = V4L2_MBUS_CSI2_DPHY
+    };
+    struct fwnode_handle *remote_ep = NULL;
+    struct fwnode_handle *local_ep = NULL;
+    struct v4l2_async_connection *asd;
+    struct fwnode_handle *fwnode;
+    struct fwnode_endpoint fwep;
+    int ret;
+
+    fwnode = dev_fwnode(dev);
+
+    local_ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
+    if (!local_ep) {
+        ret = -ENXIO;
+        goto err_fwnode;
+    }
+
+    remote_ep = fwnode_graph_get_remote_endpoint(local_ep);
+    if (!remote_ep) {
+        ret = -ENXIO;
+        goto err_fwnode;
+    }
+
+    ret = fwnode_graph_parse_endpoint(remote_ep, &fwep);
+    if (ret)
+        goto err_fwnode;
+    isp_sdev->phy_id = fwep.port;
+
+    ret = v4l2_fwnode_endpoint_alloc_parse(remote_ep, &bus_cfg);
+    if (ret)
+        goto err_fwnode;
+
+    if (!bus_cfg.nr_of_link_frequencies) {
+        ret = -EINVAL;
+        dev_err(dev, "fail invalid link freq number %u\n", 
 bus_cfg.nr_of_link_frequencies);
+        v4l2_fwnode_endpoint_free(&bus_cfg);
+        goto err_fwnode;
+    }
+    isp_sdev->phy_link_freq = *bus_cfg.link_frequencies;
+    v4l2_fwnode_endpoint_free(&bus_cfg);
+
+    isp_sdev->phy_num_data_lanes =
+        fwnode_property_count_u32(remote_ep, "data-lanes");
+
+    v4l2_async_nf_init(&isp_dev->notifier, &isp_dev->v4l2_dev);
+
+    asd = v4l2_async_nf_add_fwnode(&isp_dev->notifier, remote_ep,
+                       struct v4l2_async_connection);
+    if (IS_ERR(asd)) {
+        ret = PTR_ERR(asd);
+        goto err_async_nf_cleanup;
+    }
+
+    isp_dev->notifier.ops = &isp4_camera_sensor_ops;
+    ret = v4l2_async_nf_register(&isp_dev->notifier);
+    if (ret) {
+        dev_err(dev, "v4l2_async_nf_register fail:%d", ret);
+        goto err_async_nf_cleanup;
+    }
+
+    return 0;
+
+err_async_nf_cleanup:
+    v4l2_async_nf_cleanup(&isp_dev->notifier);
+err_fwnode:
+    if (remote_ep)
+        fwnode_handle_put(remote_ep);
+    if (local_ep)
+        fwnode_handle_put(remote_ep);
+
+    return ret;
+}
+
   /*
    * amd capture module
    */
   static int isp4_capture_probe(struct platform_device *pdev)
   {
       struct device *dev = &pdev->dev;
+    struct isp4_subdev *isp_sdev;
       struct isp4_device *isp_dev;
       int i, irq, ret;
@@ -52,6 +269,17 @@ static int isp4_capture_probe(struct 
 platform_device *pdev)
       isp_dev->pdev = pdev;
       dev->init_name = ISP4_DRV_NAME;
+    isp_sdev = &isp_dev->isp_sdev;
+    isp_sdev->mmio = devm_platform_ioremap_resource(pdev, 0);
+    if (IS_ERR(isp_sdev->mmio))
+        return dev_err_probe(dev, PTR_ERR(isp_sdev->mmio),
+                     "isp ioremap fail\n");
+
+    isp_sdev->isp_phy_mmio = devm_platform_ioremap_resource(pdev, 1);
+    if (IS_ERR(isp_sdev->isp_phy_mmio))
+        return dev_err_probe(dev, PTR_ERR(isp_sdev->isp_phy_mmio),
+                     "isp phy mmio ioremap fail\n");
+
       for (i = 0; i < ARRAY_SIZE(isp4_ringbuf_interrupt_num); i++) {
           irq = platform_get_irq(pdev, isp4_ringbuf_interrupt_num[i]);
           if (irq < 0)
@@ -90,10 +318,23 @@ static int isp4_capture_probe(struct 
 platform_device *pdev)
       dev_dbg(dev, "AMD ISP v4l2 device registered\n");
+    ret = isp4sd_init(&isp_dev->isp_sdev, &isp_dev->v4l2_dev,
+              isp_dev->pltf_data->adev);
+    if (ret) {
+        dev_err(dev, "fail init isp4 sub dev %d\n", ret);
+        goto err_unreg_v4l2;
+    }
+
+    ret = isp4_parse_fwnode_init_async_nf(isp_dev);
+    if (ret) {
+        dev_err(dev, "fail to parse fwnode %d\n", ret);
+        goto err_unreg_v4l2;
+    }
+
       ret = media_device_register(&isp_dev->mdev);
       if (ret) {
           dev_err(dev, "fail to register media device %d\n", ret);
-        goto err_unreg_v4l2;
+        goto err_isp4_deinit;
       }
       platform_set_drvdata(pdev, isp_dev);
@@ -103,6 +344,10 @@ static int isp4_capture_probe(struct 
 platform_device *pdev)
       return 0;
+err_isp4_deinit:
+    v4l2_async_nf_unregister(&isp_dev->notifier);
+    v4l2_async_nf_cleanup(&isp_dev->notifier);
+    isp4sd_deinit(&isp_dev->isp_sdev);

  err_unreg_v4l2:
      v4l2_device_unregister(&isp_dev->v4l2_dev);

drivers/media/platform/amd/isp4/isp4.c +3 -1
@@ -112,11 +357,17 @@ static int isp4_capture_probe(struct 
 platform_device *pdev)
   static void isp4_capture_remove(struct platform_device *pdev)
   {
       struct isp4_device *isp_dev = platform_get_drvdata(pdev);
-    struct device *dev = &pdev->dev;
+
+    v4l2_async_nf_unregister(&isp_dev->notifier);
+    v4l2_async_nf_cleanup(&isp_dev->notifier);
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4.c static int isp4_capture_probe(struct platform_device *pdev) · line 368

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.c +3 -1
+    v4l2_device_unregister_subdev(&isp_dev->isp_sdev.sdev);
       media_device_unregister(&isp_dev->mdev);
+    media_entity_cleanup(&isp_dev->isp_sdev.sdev.entity);
       v4l2_device_unregister(&isp_dev->v4l2_dev);
-    dev_dbg(dev, "AMD ISP v4l2 device unregistered\n");
+    dev_dbg(&pdev->dev, "AMD ISP v4l2 device unregistered\n");

Don't ping pong this

This log will be removed in next patch as you suggested in other comment

Quoted 1,392 lines
Quoted 1,391 lines
drivers/media/platform/amd/isp4/isp4.c +2 -0
+
+    isp4sd_deinit(&isp_dev->isp_sdev);
   }
   static struct platform_driver isp4_capture_drv = {
drivers/media/platform/amd/isp4/isp4.h +3 -1
index 27a7362ce6f9..596431b4a5c2 100644
@@ -7,9 +7,9 @@
   #define _ISP4_H_
   #include <linux/mutex.h>
-#include <media/v4l2-device.h>
   #include <media/videobuf2-memops.h>
   #include <media/videobuf2-vmalloc.h>
+#include "isp4_subdev.h"
   #define ISP4_GET_ISP_REG_BASE(isp4sd) (((isp4sd))->mmio)
@@ -25,11 +25,13 @@ struct isp4_platform_data {
   struct isp4_device {
       struct v4l2_device v4l2_dev;
+    struct isp4_subdev isp_sdev;
       struct media_device mdev;
       struct isp4_platform_data *pltf_data;
       struct platform_device *pdev;
       struct notifier_block i2c_nb;
+    struct v4l2_async_notifier notifier;
   };
   #endif /* isp4.h */
drivers/media/platform/amd/isp4/isp4_subdev.c new file +26 -0
new file mode 100644
index 000000000000..978164031067
@@ -0,0 +1,1192 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/mutex.h>
+#include <linux/pm_domain.h>
+#include <linux/pm_runtime.h>
+
+#include "isp4_fw_cmd_resp.h"
+#include "isp4_hw.h"
+#include "isp4_interface.h"
+#include "isp4_phy.h"
+#include "isp4_subdev.h"
+#include <linux/units.h>
+
+#define ISP4SD_MAX_CMD_RESP_BUF_SIZE (4 * 1024)
+#define ISP4SD_MIN_BUF_CNT_BEF_START_STREAM 4
+
+#define ISP4SD_PERFORMANCE_STATE_LOW 0
+#define ISP4SD_PERFORMANCE_STATE_HIGH 1
+
+#define ISP4SD_FW_CMD_TIMEOUT_IN_MS  500
+#define ISP4SD_WAIT_RESP_IRQ_TIMEOUT  5 /* ms */
+/* align 32KB */
+#define ISP4SD_META_BUF_SIZE ALIGN(sizeof(struct isp4fw_meta_info), 

0x8000)

drivers/media/platform/amd/isp4/isp4_subdev.c new file +27 -0
+
+#define to_isp4_subdev(v4l2_sdev)  \
+    container_of(v4l2_sdev, struct isp4_subdev, sdev)
+
+#define to_isp4_vdev(isp4_vid)  \
+    container_of(isp4_vid, struct isp4_subdev, isp_vdev)
+
+static const char *isp4sd_entity_name = "amd isp4";
+
+struct isp4sd_mbus_image_format_remap {
+    u32                mbus_code;
+    enum isp4fw_image_format    image_format;
+};
+
+static const struct isp4sd_mbus_image_format_remap
+    isp4sd_image_formats[] = {
+    {
+        .mbus_code    = MEDIA_BUS_FMT_YUYV8_1_5X8,
+        .image_format    = IMAGE_FORMAT_NV12,
+    },
+    {
+        .mbus_code    = MEDIA_BUS_FMT_YUYV8_1X16,
+        .image_format    = IMAGE_FORMAT_YUV422INTERLEAVED,
+    },
+};
+
+static void isp4sd_module_enable(struct isp4_subdev *isp_subdev, bool 

enable)

drivers/media/platform/amd/isp4/isp4_subdev.c new file +54 -0
+{
+    if (isp_subdev->enable_gpio) {
+        gpiod_set_value(isp_subdev->enable_gpio, enable ? 1 : 0);
+        dev_dbg(isp_subdev->dev, "%s isp_subdev module\n",
+            enable ? "enable" : "disable");
+    }
+}
+
+static int isp4sd_setup_fw_mem_pool(struct isp4_subdev *isp_subdev)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4fw_cmd_send_buffer buf_type;
+    struct device *dev = isp_subdev->dev;
+    int ret;
+
+    if (!ispif->fw_mem_pool) {
+        dev_err(dev, "fail to alloc mem pool\n");
+        return -ENOMEM;
+    }
+
+    memset(&buf_type, 0, sizeof(buf_type));
+    buf_type.buffer_type = BUFFER_TYPE_MEM_POOL;
+    buf_type.buffer.buf_tags = 0;
+    buf_type.buffer.vmid_space.bit.vmid = 0;
+    buf_type.buffer.vmid_space.bit.space = ADDR_SPACE_TYPE_GPU_VA;
+    isp4if_split_addr64(ispif->fw_mem_pool->gpu_mc_addr,
+                &buf_type.buffer.buf_base_a_lo,
+                &buf_type.buffer.buf_base_a_hi);
+    buf_type.buffer.buf_size_a = (u32)ispif->fw_mem_pool->mem_size;
+
+    ret = isp4if_send_command(ispif, CMD_ID_SEND_BUFFER,
+                  &buf_type, sizeof(buf_type));
+    if (ret) {
+        dev_err(dev, "send fw mem pool 0x%llx(%u) fail %d\n",
+            ispif->fw_mem_pool->gpu_mc_addr,
+            buf_type.buffer.buf_size_a,
+            ret);
+        return ret;
+    }
+
+    dev_dbg(dev, "send fw mem pool 0x%llx(%u) suc\n",
+        ispif->fw_mem_pool->gpu_mc_addr,
+        buf_type.buffer.buf_size_a);
+
+    return 0;
+};
+
+static int isp4sd_set_stream_path(struct isp4_subdev *isp_subdev)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4fw_cmd_set_stream_cfg cmd = {0};
+    struct device *dev = isp_subdev->dev;
+
+    cmd.stream_cfg.mipi_pipe_path_cfg.isp4fw_sensor_id = 

SENSOR_ID_ON_MIPI0;
+    cmd.stream_cfg.mipi_pipe_path_cfg.b_enable = true; +    cmd.stream_cfg.isp_pipe_path_cfg.isp_pipe_id = MIPI0_ISP_PIPELINE_ID;

drivers/media/platform/amd/isp4/isp4_subdev.c new file +105 -0
+
+    cmd.stream_cfg.b_enable_tnr = true;
+    dev_dbg(dev, "isp4fw_sensor_id %d, pipeId 0x%x EnableTnr %u\n",
+        cmd.stream_cfg.mipi_pipe_path_cfg.isp4fw_sensor_id,
+        cmd.stream_cfg.isp_pipe_path_cfg.isp_pipe_id,
+        cmd.stream_cfg.b_enable_tnr);
+
+    return isp4if_send_command(ispif, CMD_ID_SET_STREAM_CONFIG,
+                   &cmd, sizeof(cmd));
+}
+
+static int isp4sd_send_meta_buf(struct isp4_subdev *isp_subdev)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4fw_cmd_send_buffer buf_type = { 0 };
+    struct isp4sd_sensor_info *sensor_info;
+    struct device *dev = isp_subdev->dev;
+    u32 i;
+
+    sensor_info = &isp_subdev->sensor_info;
+    for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++) {
+        int ret;
+
+        if (!sensor_info->meta_info_buf[i]) {
+            dev_err(dev, "fail for no meta info buf(%u)\n", i);
+            return -ENOMEM;
+        }
+        buf_type.buffer_type = BUFFER_TYPE_META_INFO;
+        buf_type.buffer.buf_tags = 0;
+        buf_type.buffer.vmid_space.bit.vmid = 0;
+        buf_type.buffer.vmid_space.bit.space = ADDR_SPACE_TYPE_GPU_VA;
+        isp4if_split_addr64(sensor_info->meta_info_buf[i]->gpu_mc_addr,
+                    &buf_type.buffer.buf_base_a_lo,
+                    &buf_type.buffer.buf_base_a_hi);
+        buf_type.buffer.buf_size_a =
+            (u32)sensor_info->meta_info_buf[i]->mem_size;
+        ret = isp4if_send_command(ispif, CMD_ID_SEND_BUFFER,
+                      &buf_type,
+                      sizeof(buf_type));
+        if (ret) {
+            dev_err(dev, "send meta info(%u) fail\n", i);
+            return ret;
+        }
+    }
+
+    dev_dbg(dev, "send meta info suc\n");
+    return 0;
+}
+
+static int isp4sd_enum_mbus_code(struct v4l2_subdev *isp_subdev,
+                 struct v4l2_subdev_state *state,
+                 struct v4l2_subdev_mbus_code_enum *code_enum)
+{
+    if (code_enum->index >= ARRAY_SIZE(isp4sd_image_formats))
+        return -EINVAL;
+
+    code_enum->code = isp4sd_image_formats[code_enum->index].mbus_code;
+
+    return 0;
+}
+
+static bool isp4sd_get_str_out_prop(struct isp4_subdev *isp_subdev,
+                    struct isp4fw_image_prop *out_prop,
+                    struct v4l2_subdev_state *state, u32 pad)
+{
+    struct v4l2_mbus_framefmt *format = NULL;
+    struct device *dev = isp_subdev->dev;
+    bool ret;
+
+    format = v4l2_subdev_state_get_format(state, pad, 0);
+    if (!format) {
+        dev_err(dev, "fail get subdev state format\n");
+        return false;
+    }
+
+    switch (format->code) {
+    case MEDIA_BUS_FMT_YUYV8_1_5X8:
+        out_prop->image_format = IMAGE_FORMAT_NV12;
+        out_prop->width = format->width;
+        out_prop->height = format->height;
+        out_prop->luma_pitch = format->width;
+        out_prop->chroma_pitch = out_prop->width;
+        ret = true;
+        break;
+    case MEDIA_BUS_FMT_YUYV8_1X16:
+        out_prop->image_format = IMAGE_FORMAT_YUV422INTERLEAVED;
+        out_prop->width = format->width;
+        out_prop->height = format->height;
+        out_prop->luma_pitch = format->width * 2;
+        out_prop->chroma_pitch = 0;
+        ret = true;
+        break;
+    default:
+        dev_err(dev, "fail for bad image format:0x%x\n",
+            format->code);
+        ret = false;
+        break;
+    }
+
+    if (!out_prop->width || !out_prop->height)
+        ret = false;
+    return ret;
+}
+
+static int isp4sd_kickoff_stream(struct isp4_subdev *isp_subdev, u32 

w, u32 h)

drivers/media/platform/amd/isp4/isp4_subdev.c new file +369 -0
+{
+    struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+
+    if (sensor_info->status == ISP4SD_START_STATUS_STARTED) {
+        return 0;
+    } else if (sensor_info->status == ISP4SD_START_STATUS_START_FAIL) {
+        dev_err(dev, "fail for previous start fail\n");
+        return -EINVAL;
+    }
+
+    dev_dbg(dev, "w:%u,h:%u\n", w, h);
+
+    sensor_info->status = ISP4SD_START_STATUS_START_FAIL;
+
+    if (isp4sd_send_meta_buf(isp_subdev)) {
+        dev_err(dev, "fail to send meta buf\n");
+        return -EINVAL;
+    };
+
+    sensor_info->status = ISP4SD_START_STATUS_NOT_START;
+
+    if (!sensor_info->start_stream_cmd_sent &&
+        sensor_info->buf_sent_cnt >=
+        ISP4SD_MIN_BUF_CNT_BEF_START_STREAM) {
+        int ret = isp4if_send_command(ispif, CMD_ID_START_STREAM,
+                          NULL, 0);
+        if (ret) {
+            dev_err(dev, "fail to start stream\n");
+            return ret;
+        }
+
+        sensor_info->start_stream_cmd_sent = true;
+    } else {
+        dev_dbg(dev,
+            "no send START_STREAM, start_sent %u, buf_sent %u\n",
+            sensor_info->start_stream_cmd_sent,
+            sensor_info->buf_sent_cnt);
+    }
+
+    return 0;
+}
+
+static int isp4sd_setup_output(struct isp4_subdev *isp_subdev,
+                   struct v4l2_subdev_state *state, u32 pad)
+{
+    struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+    struct isp4sd_output_info *output_info =
+            &isp_subdev->sensor_info.output_info;
+    struct isp4fw_cmd_set_out_ch_prop cmd_ch_prop = {0};
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4fw_cmd_enable_out_ch cmd_ch_en = {0};
+    struct device *dev = isp_subdev->dev;
+    struct isp4fw_image_prop *out_prop;
+    int ret;
+
+    if (output_info->start_status == ISP4SD_START_STATUS_STARTED)
+        return 0;
+
+    if (output_info->start_status == ISP4SD_START_STATUS_START_FAIL) {
+        dev_err(dev, "fail for previous start fail\n");
+        return -EINVAL;
+    }
+
+    out_prop = &cmd_ch_prop.image_prop;
+    cmd_ch_prop.ch = ISP_PIPE_OUT_CH_PREVIEW;
+    cmd_ch_en.ch = ISP_PIPE_OUT_CH_PREVIEW;
+    cmd_ch_en.is_enable = true;
+
+    if (!isp4sd_get_str_out_prop(isp_subdev, out_prop, state, pad)) {
+        dev_err(dev, "fail to get out prop\n");
+        return -EINVAL;
+    }
+
+    dev_dbg(dev, "channel: w:h=%u:%u,lp:%u,cp%u\n",
+        cmd_ch_prop.image_prop.width, cmd_ch_prop.image_prop.height,
+        cmd_ch_prop.image_prop.luma_pitch,
+        cmd_ch_prop.image_prop.chroma_pitch);
+
+    ret = isp4if_send_command(ispif, CMD_ID_SET_OUT_CHAN_PROP,
+                  &cmd_ch_prop,
+                  sizeof(cmd_ch_prop));
+    if (ret) {
+        output_info->start_status = ISP4SD_START_STATUS_START_FAIL;
+        dev_err(dev, "fail to set out prop\n");
+        return ret;
+    };
+
+    ret = isp4if_send_command(ispif, CMD_ID_ENABLE_OUT_CHAN,
+                  &cmd_ch_en, sizeof(cmd_ch_en));
+
+    if (ret) {
+        output_info->start_status = ISP4SD_START_STATUS_START_FAIL;
+        dev_err(dev, "fail to enable channel\n");
+        return ret;
+    }
+
+    if (!sensor_info->start_stream_cmd_sent) {
+        ret = isp4sd_kickoff_stream(isp_subdev, out_prop->width,
+                        out_prop->height);
+        if (ret) {
+            dev_err(dev, "kickoff stream fail %d\n", ret);
+            return ret;
+        }
+        /* sensor_info->start_stream_cmd_sent will be set to true
+         * 1. in isp4sd_kickoff_stream, if app first send buffer then
+         * start stream
+         * 2. in isp_set_stream_buf, if app first start stream, then
+         * send buffer
+         * because ISP FW has the requirement, host needs to send buffer
+         * before send start stream cmd
+         */
+        if (sensor_info->start_stream_cmd_sent) {
+            sensor_info->status = ISP4SD_START_STATUS_STARTED;
+            output_info->start_status = ISP4SD_START_STATUS_STARTED;
+            dev_dbg(dev, "kickoff stream suc,start cmd sent\n");
+        } else {
+            dev_dbg(dev, "kickoff stream suc,start cmd not sent\n");
+        }
+    } else {
+        dev_dbg(dev, "stream running, no need kickoff\n");
+        output_info->start_status = ISP4SD_START_STATUS_STARTED;
+    }
+
+    dev_dbg(dev, "setup output suc\n");
+    return 0;
+}
+
+static int isp4sd_alloc_meta_buf(struct isp4_subdev *isp_subdev)
+{
+    struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+    u32 i;
+
+    /* TODO: check alloc method */
+    for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++) {
+        if (!sensor_info->meta_info_buf[i]) {
+            sensor_info->meta_info_buf[i] =
+                ispif->metainfo_buf_pool[i];
+            if (sensor_info->meta_info_buf[i]) {
+                dev_dbg(dev, "valid %u meta_info_buf ok\n", i);
+            } else {
+                dev_err(dev,
+                    "invalid %u meta_info_buf fail\n", i);
+                return -ENOMEM;
+            }
+        }
+    }
+
+    return 0;
+}
+
+static int isp4sd_init_stream(struct isp4_subdev *isp_subdev)
+{
+    struct device *dev = isp_subdev->dev;
+    int ret;
+
+    ret  = isp4sd_setup_fw_mem_pool(isp_subdev);
+    if (ret) {
+        dev_err(dev, "fail to  setup fw mem pool\n");
+        return ret;
+    }
+
+    ret  = isp4sd_alloc_meta_buf(isp_subdev);
+    if (ret) {
+        dev_err(dev, "fail to alloc fw driver shared buf\n");
+        return ret;
+    }
+
+    ret = isp4sd_set_stream_path(isp_subdev);
+    if (ret) {
+        dev_err(dev, "fail to setup stream path\n");
+        return ret;
+    }
+
+    return 0;
+}
+
+static void isp4sd_reset_stream_info(struct isp4_subdev *isp_subdev,
+                     struct v4l2_subdev_state *state, u32 pad)
+{
+    struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+    struct v4l2_mbus_framefmt *format = NULL;
+    struct isp4sd_output_info *str_info;
+    int i;
+
+    format = v4l2_subdev_state_get_format(state,
+                          pad,
+                          0);
+
+    if (!format) {
+        pr_err("fail reset stream info for not get format\n");
+
+    } else {
+        memset(format, 0, sizeof(*format));
+        format->code = MEDIA_BUS_FMT_YUYV8_1_5X8;
+    }
+
+    for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++)
+        sensor_info->meta_info_buf[i] = NULL;
+
+    str_info = &sensor_info->output_info;
+    str_info->start_status = ISP4SD_START_STATUS_NOT_START;
+}
+
+static bool isp4sd_is_stream_running(struct isp4_subdev *isp_subdev)
+{
+    struct isp4sd_sensor_info *sif;
+    enum isp4sd_start_status stat;
+
+    sif = &isp_subdev->sensor_info;
+    stat = sif->output_info.start_status;
+    if (stat == ISP4SD_START_STATUS_STARTED)
+        return true;
+
+    return false;
+}
+
+static void isp4sd_reset_camera_info(struct isp4_subdev *isp_subdev,
+                     struct v4l2_subdev_state *state, u32 pad)
+{
+    struct isp4sd_sensor_info *info  = &isp_subdev->sensor_info;
+
+    info->status = ISP4SD_START_STATUS_NOT_START;
+    isp4sd_reset_stream_info(isp_subdev, state, pad);
+
+    info->start_stream_cmd_sent = false;
+}
+
+static int isp4sd_uninit_stream(struct isp4_subdev *isp_subdev,
+                struct v4l2_subdev_state *state, u32 pad)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+    bool running;
+
+    running = isp4sd_is_stream_running(isp_subdev);
+
+    if (running) {
+        dev_dbg(dev, "fail for stream is still running\n");
+        return -EINVAL;
+    }
+
+    isp4sd_reset_camera_info(isp_subdev, state, pad);
+
+    isp4if_clear_cmdq(ispif);
+    return 0;
+}
+
+static void isp4sd_fw_resp_cmd_done(struct isp4_subdev *isp_subdev,
+                    enum isp4if_stream_id stream_id,
+                    struct isp4fw_resp_cmd_done *para)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4if_cmd_element *ele =
+        isp4if_rm_cmd_from_cmdq(ispif, para->cmd_seq_num, para->cmd_id);
+    struct device *dev = isp_subdev->dev;
+
+    dev_dbg(dev, "stream %d,cmd (0x%08x)(%d),seq %u, ele %p\n",
+        stream_id,
+        para->cmd_id, para->cmd_status, para->cmd_seq_num,
+        ele);
+
+    if (!ele)
+        return;
+
+    if (ele->wq) {
+        dev_dbg(dev, "signal event %p\n", ele->wq);
+        if (ele->wq_cond)
+            *ele->wq_cond = 1;
+        wake_up(ele->wq);
+    }
+
+    kfree(ele);
+}
+
+static struct isp4fw_meta_info *
+isp4sd_get_meta_by_mc(struct isp4_subdev *isp_subdev,
+              u64 mc)
+{
+    u32 i;
+
+    for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++) {
+        struct isp4if_gpu_mem_info *meta_info_buf =
+                isp_subdev->sensor_info.meta_info_buf[i];
+
+        if (meta_info_buf) {
+            if (mc == meta_info_buf->gpu_mc_addr)
+                return meta_info_buf->sys_addr;
+        }
+    }
+    return NULL;
+};
+
+static struct isp4if_img_buf_node *
+isp4sd_preview_done(struct isp4_subdev *isp_subdev,
+            struct isp4fw_meta_info *meta)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4if_img_buf_node *prev = NULL;
+    struct device *dev = isp_subdev->dev;
+
+    if (!meta) {
+        dev_err(dev, "fail bad param for preview done\n");
+        return prev;
+    }
+
+    if (meta->preview.enabled &&
+        (meta->preview.status == BUFFER_STATUS_SKIPPED ||
+         meta->preview.status == BUFFER_STATUS_DONE ||
+         meta->preview.status == BUFFER_STATUS_DIRTY)) {
+        prev = isp4if_dequeue_buffer(ispif);
+        if (!prev)
+            dev_err(dev, "fail null prev buf\n");
+
+    } else if (meta->preview.enabled) {
+        dev_err(dev, "fail bad preview status %u\n",
+            meta->preview.status);
+    }
+
+    return prev;
+}
+
+static void isp4sd_send_meta_info(struct isp4_subdev *isp_subdev,
+                  u64 meta_info_mc)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4fw_cmd_send_buffer buf_type;
+    struct device *dev = isp_subdev->dev;
+
+    if (isp_subdev->sensor_info.status != ISP4SD_START_STATUS_STARTED) {
+        dev_warn(dev, "not working status %i, meta_info 0x%llx\n",
+             isp_subdev->sensor_info.status, meta_info_mc);
+        return;
+    }
+
+    if (meta_info_mc) {
+        memset(&buf_type, 0, sizeof(buf_type));
+        buf_type.buffer_type = BUFFER_TYPE_META_INFO;
+        buf_type.buffer.buf_tags = 0;
+        buf_type.buffer.vmid_space.bit.vmid = 0;
+        buf_type.buffer.vmid_space.bit.space = ADDR_SPACE_TYPE_GPU_VA;
+        isp4if_split_addr64(meta_info_mc,
+                    &buf_type.buffer.buf_base_a_lo,
+                    &buf_type.buffer.buf_base_a_hi);
+
+        buf_type.buffer.buf_size_a = ISP4SD_META_BUF_SIZE;
+        if (isp4if_send_command(ispif, CMD_ID_SEND_BUFFER,
+                    &buf_type, sizeof(buf_type))) {
+            dev_err(dev, "fail send meta_info 0x%llx\n",
+                meta_info_mc);
+        } else {
+            dev_dbg(dev, "resend meta_info 0x%llx\n", meta_info_mc);
+        }
+    }
+}
+
+static void isp4sd_fw_resp_frame_done(struct isp4_subdev *isp_subdev,
+                      enum isp4if_stream_id stream_id,
+                      struct isp4fw_resp_param_package *para)
+{
+    struct isp4if_img_buf_node *prev = NULL;
+    struct device *dev = isp_subdev->dev;
+    struct isp4fw_meta_info *meta;
+    u64 mc = 0;
+
+    mc = isp4if_join_addr64(para->package_addr_lo, para- 
Quoted 1 line

package_addr_hi);

+    meta = isp4sd_get_meta_by_mc(isp_subdev, mc); +    if (mc == 0 || !meta) {
+        dev_err(dev, "fail to get meta from mc %llx\n", mc); +        return;
+    }

drivers/media/platform/amd/isp4/isp4_subdev.c new file +110 -0
+
+    dev_dbg(dev, "ts:%llu,streamId:%d,poc:%u,preview_en:%u,(%i)\n",
+        ktime_get_ns(), stream_id, meta->poc,
+        meta->preview.enabled,
+        meta->preview.status);
+
+    prev = isp4sd_preview_done(isp_subdev, meta);
+
+    isp4if_dealloc_buffer_node(prev);
+
+    if (isp_subdev->sensor_info.status == ISP4SD_START_STATUS_STARTED)
+        isp4sd_send_meta_info(isp_subdev, mc);
+
+    dev_dbg(dev, "stream_id:%d, status:%d\n", stream_id,
+        isp_subdev->sensor_info.status);
+}
+
+static void isp4sd_fw_resp_func(struct isp4_subdev *isp_subdev,
+                enum isp4if_stream_id stream_id)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+    struct isp4fw_resp resp;
+
+    if (ispif->status < ISP4IF_STATUS_FW_RUNNING)
+        return;
+
+    while (true) {
+        s32 ret;
+
+        ret = isp4if_f2h_resp(ispif, stream_id, &resp);
+        if (ret)
+            break;
+
+        switch (resp.resp_id) {
+        case RESP_ID_CMD_DONE:
+            isp4sd_fw_resp_cmd_done(isp_subdev, stream_id,
+                        &resp.param.cmd_done);
+            break;
+        case RESP_ID_NOTI_FRAME_DONE:
+            isp4sd_fw_resp_frame_done(isp_subdev, stream_id,
+                          &resp.param.frame_done);
+            break;
+        default:
+            dev_err(dev, "-><- fail respid (0x%x)\n",
+                resp.resp_id);
+            break;
+        }
+    }
+}
+
+static s32 isp4sd_fw_resp_thread_wrapper(void *context)
+{
+    struct isp4_subdev_thread_param *para = context;
+    struct isp4sd_thread_handler *thread_ctx;
+    enum isp4if_stream_id stream_id;
+
+    struct isp4_subdev *isp_subdev;
+    struct device *dev;
+    u64 timeout;
+
+    if (!para)
+        return -EINVAL;
+
+    isp_subdev = para->isp_subdev;
+    dev = isp_subdev->dev;
+
+    switch (para->idx) {
+    case 0:
+        stream_id = ISP4IF_STREAM_ID_GLOBAL;
+        break;
+    case 1:
+        stream_id = ISP4IF_STREAM_ID_1;
+        break;
+    default:
+        dev_err(dev, "fail invalid %d\n", para->idx);
+        return -EINVAL;
+    }
+
+    thread_ctx = &isp_subdev->fw_resp_thread[para->idx];
+
+    thread_ctx->wq_cond = 0;
+    mutex_init(&thread_ctx->mutex);
+    init_waitqueue_head(&thread_ctx->waitq);
+    timeout = msecs_to_jiffies(ISP4SD_WAIT_RESP_IRQ_TIMEOUT);
+
+    dev_dbg(dev, "[%u] started\n", para->idx);
+
+    while (true) {
+        wait_event_interruptible_timeout(thread_ctx->waitq,
+                         thread_ctx->wq_cond != 0,
+                         timeout);
+        thread_ctx->wq_cond = 0;
+
+        if (kthread_should_stop()) {
+            dev_dbg(dev, "[%u] quit\n", para->idx);
+            break;
+        }
+
+        mutex_lock(&thread_ctx->mutex);
+        isp4sd_fw_resp_func(isp_subdev, stream_id);
+        mutex_unlock(&thread_ctx->mutex);
+    }
+
+    mutex_destroy(&thread_ctx->mutex);
+
+    return 0;
+}
+
+static int isp4sd_start_resp_proc_threads(struct isp4_subdev 

*isp_subdev)

drivers/media/platform/amd/isp4/isp4_subdev.c new file +41 -0
+{
+    struct device *dev = isp_subdev->dev;
+    int i;
+
+    for (i = 0; i < ISP4SD_MAX_FW_RESP_STREAM_NUM; i++) {
+        struct isp4sd_thread_handler *thread_ctx =
+                &isp_subdev->fw_resp_thread[i];
+
+        isp_subdev->isp_resp_para[i].idx = i;
+        isp_subdev->isp_resp_para[i].isp_subdev = isp_subdev;
+
+        thread_ctx->thread = kthread_run(isp4sd_fw_resp_thread_wrapper,
+                         &isp_subdev->isp_resp_para[i],
+                         "amd_isp4_thread");
+        if (IS_ERR(thread_ctx->thread)) {
+            dev_err(dev, "create thread [%d] fail\n", i);
+            return -EINVAL;
+        }
+    }
+
+    return 0;
+}
+
+static int isp4sd_stop_resp_proc_threads(struct isp4_subdev *isp_subdev)
+{
+    int i;
+
+    for (i = 0; i < ISP4SD_MAX_FW_RESP_STREAM_NUM; i++) {
+        struct isp4sd_thread_handler *thread_ctx =
+                &isp_subdev->fw_resp_thread[i];
+
+        if (thread_ctx->thread) {
+            kthread_stop(thread_ctx->thread);
+            thread_ctx->thread = NULL;
+        }
+    }
+
+    return 0;
+}
+
+static u32 isp4sd_get_started_stream_count(struct isp4_subdev 

*isp_subdev)

drivers/media/platform/amd/isp4/isp4_subdev.c new file +236 -0
+{
+    u32 cnt = 0;
+
+    if (isp_subdev->sensor_info.status == ISP4SD_START_STATUS_STARTED)
+        cnt++;
+    return cnt;
+}
+
+static int isp4sd_pwroff_and_deinit(struct isp4_subdev *isp_subdev)
+{
+    struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+    unsigned int perf_state = ISP4SD_PERFORMANCE_STATE_LOW;
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+
+    struct device *dev = isp_subdev->dev;
+    u32 cnt;
+    int ret;
+
+    mutex_lock(&isp_subdev->ops_mutex);
+
+    if (sensor_info->status == ISP4SD_START_STATUS_STARTED) {
+        dev_err(dev, "fail for stream still running\n");
+        mutex_unlock(&isp_subdev->ops_mutex);
+        return -EINVAL;
+    }
+
+    sensor_info->status = ISP4SD_START_STATUS_NOT_START;
+    cnt = isp4sd_get_started_stream_count(isp_subdev);
+    if (cnt > 0) {
+        dev_dbg(dev, "no need power off isp_subdev\n");
+        mutex_unlock(&isp_subdev->ops_mutex);
+        return 0;
+    }
+
+    isp4if_stop(ispif);
+
+    ret = dev_pm_genpd_set_performance_state(dev, perf_state);
+    if (ret)
+        dev_err(dev,
+            "fail to set isp_subdev performance state %u,ret %d\n",
+            perf_state, ret);
+    isp4sd_stop_resp_proc_threads(isp_subdev);
+    dev_dbg(dev, "isp_subdev stop resp proc streads suc");
+    /* hold ccpu reset */
+    isp4hw_wreg(isp_subdev->mmio, ISP_SOFT_RESET, 0x0);
+    isp4hw_wreg(isp_subdev->mmio, ISP_POWER_STATUS, 0);
+    ret = pm_runtime_put_sync(dev);
+    if (ret)
+        dev_err(dev, "power off isp_subdev fail %d\n", ret);
+    else
+        dev_dbg(dev, "power off isp_subdev suc\n");
+
+    ispif->status = ISP4IF_STATUS_PWR_OFF;
+    isp4if_clear_cmdq(ispif);
+    isp4sd_module_enable(isp_subdev, false);
+
+    msleep(20);
+
+    mutex_unlock(&isp_subdev->ops_mutex);
+
+    return 0;
+}
+
+static int isp4sd_pwron_and_init(struct isp4_subdev *isp_subdev)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+    int ret;
+
+    if (ispif->status == ISP4IF_STATUS_FW_RUNNING) {
+        dev_dbg(dev, "camera already opened, do nothing\n");
+        return 0;
+    }
+
+    mutex_lock(&isp_subdev->ops_mutex);
+
+    isp4sd_module_enable(isp_subdev, true);
+
+    isp_subdev->sensor_info.start_stream_cmd_sent = false;
+    isp_subdev->sensor_info.buf_sent_cnt = 0;
+
+    if (ispif->status < ISP4IF_STATUS_PWR_ON) {
+        unsigned int perf_state = ISP4SD_PERFORMANCE_STATE_HIGH;
+
+        ret = pm_runtime_resume_and_get(dev);
+        if (ret) {
+            dev_err(dev, "fail to power on isp_subdev ret %d\n",
+                ret);
+            goto err_unlock_and_close;
+        }
+
+        /* ISPPG ISP Power Status */
+        isp4hw_wreg(isp_subdev->mmio, ISP_POWER_STATUS, 0x7FF);
+        ret = dev_pm_genpd_set_performance_state(dev, perf_state);
+        if (ret) {
+            dev_err(dev,
+                "fail to set performance state %u, ret %d\n",
+                perf_state, ret);
+            goto err_unlock_and_close;
+        }
+
+        ispif->status = ISP4IF_STATUS_PWR_ON;
+
+        if (isp4sd_start_resp_proc_threads(isp_subdev)) {
+            dev_err(dev, "isp_start_resp_proc_threads fail");
+            goto err_unlock_and_close;
+        } else {
+            dev_dbg(dev, "create resp threads ok");
+        }
+    }
+
+    isp_subdev->sensor_info.start_stream_cmd_sent = false;
+    isp_subdev->sensor_info.buf_sent_cnt = 0;
+
+    ret = isp4if_start(ispif);
+    if (ret) {
+        dev_err(dev, "fail to start isp_subdev interface\n");
+        goto err_unlock_and_close;
+    }
+
+    mutex_unlock(&isp_subdev->ops_mutex);
+    return 0;
+err_unlock_and_close:
+    mutex_unlock(&isp_subdev->ops_mutex);
+    isp4sd_pwroff_and_deinit(isp_subdev);
+    return -EINVAL;
+}
+
+static int isp4sd_stop_stream(struct isp4_subdev *isp_subdev,
+                  struct v4l2_subdev_state *state, u32 pad)
+{
+    struct isp4sd_output_info *output_info =
+            &isp_subdev->sensor_info.output_info;
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+    int ret = 0;
+
+    dev_dbg(dev, "status %i\n", output_info->start_status);
+    mutex_lock(&isp_subdev->ops_mutex);
+
+    if (output_info->start_status == ISP4SD_START_STATUS_STARTED) {
+        struct isp4fw_cmd_enable_out_ch cmd_ch_disable;
+
+        cmd_ch_disable.ch = ISP_PIPE_OUT_CH_PREVIEW;
+        cmd_ch_disable.is_enable = false;
+        ret = isp4if_send_command_sync(ispif,
+                           CMD_ID_ENABLE_OUT_CHAN,
+                           &cmd_ch_disable,
+                           sizeof(cmd_ch_disable),
+                           ISP4SD_FW_CMD_TIMEOUT_IN_MS);
+        if (ret)
+            dev_err(dev, "fail to disable stream\n");
+        else
+            dev_dbg(dev, "wait disable stream suc\n");
+
+        ret = isp4if_send_command_sync(ispif, CMD_ID_STOP_STREAM,
+                           NULL,
+                           0,
+                           ISP4SD_FW_CMD_TIMEOUT_IN_MS);
+        if (ret)
+            dev_err(dev, "fail to stop steam\n");
+        else
+            dev_dbg(dev, "wait stop stream suc\n");
+    }
+
+    isp4if_clear_bufq(ispif);
+
+    output_info->start_status = ISP4SD_START_STATUS_NOT_START;
+    isp4sd_reset_stream_info(isp_subdev, state, pad);
+
+    mutex_unlock(&isp_subdev->ops_mutex);
+
+    isp4sd_uninit_stream(isp_subdev, state, pad);
+
+    return ret;
+}
+
+static int isp4sd_start_stream(struct isp4_subdev *isp_subdev,
+                   struct v4l2_subdev_state *state, u32 pad)
+{
+    struct isp4sd_output_info *output_info =
+            &isp_subdev->sensor_info.output_info;
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+    int ret;
+
+    mutex_lock(&isp_subdev->ops_mutex);
+
+    if (ispif->status != ISP4IF_STATUS_FW_RUNNING) {
+        mutex_unlock(&isp_subdev->ops_mutex);
+        dev_err(dev, "fail, bad fsm %d", ispif->status);
+        return -EINVAL;
+    }
+
+    ret = isp4sd_init_stream(isp_subdev);
+
+    if (ret) {
+        dev_err(dev, "fail to init isp_subdev stream\n");
+        ret = -EINVAL;
+        goto unlock_and_check_ret;
+    }
+
+    if (output_info->start_status == ISP4SD_START_STATUS_STARTED) {
+        ret = 0;
+        dev_dbg(dev, "stream started, do nothing\n");
+        goto unlock_and_check_ret;
+    } else if (output_info->start_status ==
+           ISP4SD_START_STATUS_START_FAIL) {
+        ret = -EINVAL;
+        dev_err(dev, "stream  fail to start before\n");
+        goto unlock_and_check_ret;
+    }
+
+    if (isp4sd_setup_output(isp_subdev, state, pad)) {
+        dev_err(dev, "fail to setup output\n");
+        ret = -EINVAL;
+    } else {
+        ret = 0;
+        dev_dbg(dev, "suc to setup out\n");
+    }
+unlock_and_check_ret:
+    mutex_unlock(&isp_subdev->ops_mutex);
+    if (ret) {
+        isp4sd_stop_stream(isp_subdev, state, pad);
+        dev_err(dev, "start stream fail\n");
+    } else {
+        dev_dbg(dev, "start stream suc\n");
+    }
+
+    return ret;
+}
+
+static int isp4sd_subdev_pre_streamon(struct v4l2_subdev *sd, u32 flags)
+{
+    struct isp4_subdev *isp_subdev = to_isp4_subdev(sd);
+    u64 phy_bit_rate = isp_subdev->phy_link_freq * isp_subdev- 
Quoted 1 line

phy_num_data_lanes / HZ_PER_MHZ;

+    u32 num_data_lanes = isp_subdev->phy_num_data_lanes; +    u32 phy_id = isp_subdev->phy_id;
+    int ret;

drivers/media/platform/amd/isp4/isp4_subdev.c new file +12 -0
+
+    ret = isp4phy_start(isp_subdev->dev,
+                isp_subdev->isp_phy_mmio, phy_id,
+                phy_bit_rate, num_data_lanes);
+    if (ret) {
+        dev_err(isp_subdev->dev,
+            "fail start phy,lane %d id %u bitrate %llu, %d\n",
+            num_data_lanes, phy_id, phy_bit_rate, ret);
+        return ret;
+    }
+
+    dev_dbg(isp_subdev->dev, "start phy suc,lane %d id %u bit_rate 

%llu\n",
+        num_data_lanes, phy_id, phy_bit_rate);

drivers/media/platform/amd/isp4/isp4_subdev.c new file +201 -0
+
+    return ret;
+}
+
+static int isp4sd_subdev_post_streamoff(struct v4l2_subdev *sd)
+{
+    struct isp4_subdev *isp_subdev = to_isp4_subdev(sd);
+    int ret;
+
+    dev_dbg(isp_subdev->dev, "stopping phy %u\n", isp_subdev->phy_id);
+    ret = isp4phy_stop(isp_subdev->isp_phy_mmio,
+               isp_subdev->phy_id);
+    if (ret)
+        dev_err(isp_subdev->dev, "fail to stop the Phy:%d", ret);
+
+    return ret;
+}
+
+static int isp4sd_set_power(struct v4l2_subdev *sd, int on)
+{
+    struct isp4_subdev *ispsd = to_isp4_subdev(sd);
+
+    if (on)
+        return isp4sd_pwron_and_init(ispsd);
+    else
+        return isp4sd_pwroff_and_deinit(ispsd);
+};
+
+static const struct v4l2_subdev_core_ops isp4sd_core_ops = {
+    .s_power = isp4sd_set_power,
+};
+
+static const struct v4l2_subdev_video_ops isp4sd_video_ops = {
+    .s_stream = v4l2_subdev_s_stream_helper,
+    .pre_streamon = isp4sd_subdev_pre_streamon,
+    .post_streamoff = isp4sd_subdev_post_streamoff,
+};
+
+static int isp4sd_set_pad_format(struct v4l2_subdev *sd,
+                 struct v4l2_subdev_state *sd_state,
+                 struct v4l2_subdev_format *fmt)
+{
+    struct isp4sd_output_info *steam_info =
+        &(to_isp4_subdev(sd)->sensor_info.output_info);
+    struct v4l2_mbus_framefmt *format;
+
+    format = v4l2_subdev_state_get_format(sd_state, fmt->pad);
+
+    if (!format) {
+        dev_err(sd->dev, "fail to get state format\n");
+        return -EINVAL;
+    }
+
+    *format = fmt->format;
+    switch (format->code) {
+    case MEDIA_BUS_FMT_YUYV8_1_5X8:
+        steam_info->image_size = format->width * format->height * 3 / 2;
+        break;
+    case MEDIA_BUS_FMT_YUYV8_1X16:
+        steam_info->image_size = format->width * format->height * 2;
+        break;
+    default:
+        steam_info->image_size = 0;
+        break;
+    }
+    if (!steam_info->image_size) {
+        dev_err(sd->dev,
+            "fail set pad format,code 0x%x,width %u, height %u\n",
+            format->code, format->width, format->height);
+        return -EINVAL;
+    }
+    dev_dbg(sd->dev,
+        "set pad format suc, code:%x w:%u h:%u size:%u\n", format->code,
+        format->width, format->height, steam_info->image_size);
+
+    return 0;
+}
+
+static int isp4sd_enable_streams(struct v4l2_subdev *sd,
+                 struct v4l2_subdev_state *state, u32 pad,
+                 u64 streams_mask)
+{
+    struct isp4_subdev *ispsd = to_isp4_subdev(sd);
+
+    return isp4sd_start_stream(ispsd, state, pad);
+}
+
+static int isp4sd_disable_streams(struct v4l2_subdev *sd,
+                  struct v4l2_subdev_state *state, u32 pad,
+                  u64 streams_mask)
+{
+    struct isp4_subdev *ispsd = to_isp4_subdev(sd);
+
+    return isp4sd_stop_stream(ispsd, state, pad);
+}
+
+static const struct v4l2_subdev_pad_ops isp4sd_pad_ops = {
+    .enum_mbus_code    = isp4sd_enum_mbus_code,
+    .get_fmt = v4l2_subdev_get_fmt,
+    .set_fmt = isp4sd_set_pad_format,
+    .enable_streams = isp4sd_enable_streams,
+    .disable_streams = isp4sd_disable_streams,
+};
+
+static const struct v4l2_subdev_ops isp4sd_subdev_ops = {
+    .core = &isp4sd_core_ops,
+    .video = &isp4sd_video_ops,
+    .pad = &isp4sd_pad_ops,
+};
+
+static int isp4sd_sdev_link_validate(struct media_link *link)
+{
+    return 0;
+}
+
+static const struct media_entity_operations isp4sd_sdev_ent_ops = {
+    .link_validate = isp4sd_sdev_link_validate,
+};
+
+int isp4sd_init(struct isp4_subdev *isp_subdev,
+        struct v4l2_device *v4l2_dev,
+        void *amdgpu_dev)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4sd_sensor_info *sensor_info;
+    struct device *dev = v4l2_dev->dev;
+    int ret;
+
+    isp_subdev->dev = dev;
+    isp_subdev->amdgpu_dev = amdgpu_dev;
+    v4l2_subdev_init(&isp_subdev->sdev, &isp4sd_subdev_ops);
+    isp_subdev->sdev.owner = THIS_MODULE;
+    isp_subdev->sdev.dev = dev;
+    snprintf(isp_subdev->sdev.name, sizeof(isp_subdev->sdev.name), "%s",
+         dev_name(dev));
+
+    isp_subdev->sdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
+    isp_subdev->sdev.entity.name = isp4sd_entity_name;
+    isp_subdev->sdev.entity.function = MEDIA_ENT_F_PROC_VIDEO_ISP;
+    isp_subdev->sdev.entity.ops = &isp4sd_sdev_ent_ops;
+    isp_subdev->sdev_pad[0].flags = MEDIA_PAD_FL_SINK;
+    isp_subdev->sdev_pad[1].flags = MEDIA_PAD_FL_SOURCE;
+    ret = media_entity_pads_init(&isp_subdev->sdev.entity, 2,
+                     isp_subdev->sdev_pad);
+    if (ret) {
+        dev_err(dev, "fail to init isp4 subdev entity pad %d\n", ret);
+        return ret;
+    }
+    ret = v4l2_subdev_init_finalize(&isp_subdev->sdev);
+    if (ret < 0) {
+        dev_err(dev, "fail to init finalize isp4 subdev %d\n",
+            ret);
+        return ret;
+    }
+    ret = v4l2_device_register_subdev(v4l2_dev, &isp_subdev->sdev);
+    if (ret) {
+        dev_err(dev, "fail to register isp4 subdev to V4L2 device %d\n",
+            ret);
+        goto err_media_clean_up;
+    }
+
+    sensor_info = &isp_subdev->sensor_info;
+
+    isp4if_init(ispif, dev, amdgpu_dev, isp_subdev->mmio);
+
+    mutex_init(&isp_subdev->ops_mutex);
+    sensor_info->start_stream_cmd_sent = false;
+    sensor_info->status = ISP4SD_START_STATUS_NOT_START;
+
+    /* create ISP enable gpio control */
+    isp_subdev->enable_gpio = devm_gpiod_get(isp_subdev->dev,
+                         "enable_isp",
+                         GPIOD_OUT_LOW);
+    if (IS_ERR(isp_subdev->enable_gpio)) {
+        dev_err(dev, "fail to get gpiod %d\n", ret);
+        media_entity_cleanup(&isp_subdev->sdev.entity);
+        return PTR_ERR(isp_subdev->enable_gpio);
+    }
+
+    isp_subdev->host2fw_seq_num = 1;
+    ispif->status = ISP4IF_STATUS_PWR_OFF;
+
+    if (ret)
+        goto err_media_clean_up;
+    return ret;
+
+err_media_clean_up:
+    media_entity_cleanup(&isp_subdev->sdev.entity);
+    return ret;
+}
+
+void isp4sd_deinit(struct isp4_subdev *isp_subdev)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+
+    media_entity_cleanup(&isp_subdev->sdev.entity);
+    isp4if_deinit(ispif);
+    isp4sd_module_enable(isp_subdev, false);
+
+    ispif->status = ISP4IF_STATUS_PWR_OFF;
+}
drivers/media/platform/amd/isp4/isp4_subdev.h new file +31 -0
new file mode 100644
index 000000000000..bcbb93dce18f
@@ -0,0 +1,145 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_CONTEXT_H_
+#define _ISP4_CONTEXT_H_
+
+#include <linux/delay.h>
+#include <linux/firmware.h>
+#include <linux/platform_device.h>
+#include <linux/uaccess.h>
+#include <linux/types.h>
+#include <linux/debugfs.h>
+#include <media/v4l2-device.h>
+#include <media/videobuf2-vmalloc.h>
+
+#include "isp4_fw_cmd_resp.h"
+#include "isp4_hw_reg.h"
+#include "isp4_interface.h"
+
+/*
+ * one is for none sesnor specefic response which is not used now
+ * another is for sensor specific response
+ */
+#define ISP4SD_MAX_FW_RESP_STREAM_NUM 2
+
+/*
+ * cmd used to register frame done callback, parameter is
+ * struct isp4sd_register_framedone_cb_param *
+ * when a image buffer is filled by ISP, ISP will call the registered 

callback.
+ * callback func prototype is isp4sd_framedone_cb, cb_ctx can be anything
+ * provided by caller which will be provided back as the first parameter of the
+ * callback function.
+ * both cb_func and cb_ctx are provide by caller, set cb_func to NULL to
+ * unregister the callback
+ */

drivers/media/platform/amd/isp4/isp4_subdev.h new file +28 -0
+
+/* used to indicate the ISP status*/
+enum isp4sd_status {
+    ISP4SD_STATUS_PWR_OFF,
+    ISP4SD_STATUS_PWR_ON,
+    ISP4SD_STATUS_FW_RUNNING,
+    ISP4SD_STATUS_MAX
+};
+
+/*used to indicate the status of sensor, output stream */
+enum isp4sd_start_status {
+    ISP4SD_START_STATUS_NOT_START,
+    ISP4SD_START_STATUS_STARTED,
+    ISP4SD_START_STATUS_START_FAIL,
+};
+
+struct isp4sd_img_buf_node {
+    struct list_head node;
+    struct isp4if_img_buf_info buf_info;
+};
+
+/* this is isp output after processing bayer raw input from sensor */
+struct isp4sd_output_info {
+    enum isp4sd_start_status start_status;
+    u32 image_size;
+};
+
+/* This struct represents the sensor info which is input or source of 

ISP,
+ * meta_info_buf is the buffer store the fw to driver metainfo response
+ * status is the sensor status
+ * output_info is the isp output info after ISP processing the sensor input,
+ * start_stream_cmd_sent mean if CMD_ID_START_STREAM has sent to fw.
+ * buf_sent_cnt is buffer count app has sent to receive the images
+ */

drivers/media/platform/amd/isp4/isp4_subdev.h new file +74 -0
+struct isp4sd_sensor_info {
+    struct isp4if_gpu_mem_info *
+        meta_info_buf[ISP4IF_MAX_STREAM_META_BUF_COUNT];
+    struct isp4sd_output_info output_info;
+    enum isp4sd_start_status status;
+    bool start_stream_cmd_sent;
+    u32 buf_sent_cnt;
+};
+
+/*
+ * Thread created by driver to receive fw response
+ * thread will be wakeup by fw to driver response interrupt
+ */
+struct isp4sd_thread_handler {
+    struct task_struct *thread;
+    struct mutex mutex; /* mutex */
+    wait_queue_head_t waitq;
+    int wq_cond;
+};
+
+struct isp4_subdev_thread_param {
+    u32 idx;
+    struct isp4_subdev *isp_subdev;
+};
+
+struct isp4_subdev {
+    struct v4l2_subdev sdev;
+    struct isp4_interface ispif;
+
+    /*
+     * sdev_pad[0] is sink pad connected to sensor
+     * sdev_pad[0] is sink pad connected to sensor
+     * sdev_pad[1] is sourc pad connected v4l2 video device
+     */
+    struct media_pad sdev_pad[2];
+
+    enum isp4sd_status isp_status;
+    struct mutex ops_mutex; /* ops_mutex */
+
+    /* Used to store fw cmds sent to FW whose response driver needs
+     * to wait for
+     */
+    struct isp4sd_thread_handler
+        fw_resp_thread[ISP4SD_MAX_FW_RESP_STREAM_NUM];
+
+    u32 host2fw_seq_num;
+
+    struct isp4sd_sensor_info sensor_info;
+
+    /* gpio descriptor */
+    struct gpio_desc *enable_gpio;
+    struct device *dev;
+    void *amdgpu_dev;
+    void __iomem *mmio;
+    struct isp4_subdev_thread_param
+        isp_resp_para[ISP4SD_MAX_FW_RESP_STREAM_NUM];
+#ifdef CONFIG_DEBUG_FS
+    struct dentry *debugfs_dir;
+    bool enable_fw_log;
+    char *fw_log_output;
+#endif
+    u32 phy_num_data_lanes;
+    u32 phy_id;
+    u64 phy_link_freq;
+
+    void __iomem *isp_phy_mmio;
+};
+
+int isp4sd_init(struct isp4_subdev *isp_subdev,
+        struct v4l2_device *v4l2_dev,
+        void *amdgpu_dev);
+void isp4sd_deinit(struct isp4_subdev *isp_subdev);
+
+#endif
#40 lore raw

Re: [PATCH v2 5/8] media: platform: amd: isp4 subdev and firmware loading handling added

Quoted 90 lines · Bin

Thanks Mario, add some comments

Quoted 79 lines · Mario Limonciello
Quoted 78 lines · Bin Du

Isp4 sub-device is implementing v4l2 sub-device interface. It has one capture video node, and supports only preview stream. It manages firmware states, stream configuration and mipi phy configuration. This change also adds interrupt handling and notification for isp firmware to isp-subdevice.

---
  drivers/media/platform/amd/isp4/Makefile      |    3 +- drivers/media/platform/amd/isp4/isp4.c        |  267 +++- drivers/media/platform/amd/isp4/isp4.h        |    4 +- drivers/media/platform/amd/isp4/isp4_subdev.c | 1192 +++++++++++++++++ drivers/media/platform/amd/isp4/isp4_subdev.h |  145 ++

  5 files changed, 1601 insertions(+), 10 deletions(-)

  create mode 100644 drivers/media/platform/amd/isp4/isp4_subdev.c create mode 100644 drivers/media/platform/amd/isp4/isp4_subdev.h

drivers/media/platform/amd/isp4/Makefile +0 -0
index c0166f954516..52defc06189e 100644
@@ -3,9 +3,10 @@
   # Copyright (C) 2025 Advanced Micro Devices, Inc.

  obj-$(CONFIG_AMD_ISP4) += amd_capture.o

drivers/media/platform/amd/isp4/Makefile +1 -1
-amd_capture-objs := isp4.o    \
+amd_capture-objs := isp4_subdev.o \

              isp4_phy.o \
              isp4_interface.o \
+            isp4.o    \
              isp4_hw.o    \
  ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4

drivers/media/platform/amd/isp4/isp4.c +29 -2
index d0be90c5ec3b..c1aca2bd35e3 100644
@@ -5,13 +5,20 @@
   #include <linux/pm_runtime.h>
   #include <linux/vmalloc.h>
+#include <media/v4l2-fwnode.h>
   #include <media/v4l2-ioctl.h>
-#include "isp4.h"
+#include "amdgpu_object.h"
-#define VIDEO_BUF_NUM 5
+#include "isp4.h"
+#include "isp4_hw.h"
   #define ISP4_DRV_NAME "amd_isp_capture"
+#define ISP4_FW_RESP_RB_IRQ_STATUS_MASK \
+    (ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT9_INT_MASK  | \
+     ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT10_INT_MASK | \
+     ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT11_INT_MASK | \
+     ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT12_INT_MASK)
   /* interrupt num */
   static const u32 isp4_ringbuf_interrupt_num[] = {
@@ -24,23 +31,233 @@ static const u32 isp4_ringbuf_interrupt_num[] = {
   #define to_isp4_device(dev) \
       ((struct isp4_device *)container_of(dev, struct isp4_device, v4l2_dev))
+static int isp4_create_links(struct isp4_device *isp4_dev,
+                 struct v4l2_subdev *sensor_sdev)
+{
+    struct v4l2_subdev *isp4_sdev = &isp4_dev->isp_sdev.sdev;
+    struct device *dev = &isp4_dev->pdev->dev;
+    int ret;
+
+    ret = media_create_pad_link(&sensor_sdev->entity,
+                    0, &isp4_sdev->entity, 0,
+                    MEDIA_LNK_FL_ENABLED |
+                    MEDIA_LNK_FL_IMMUTABLE);
+    if (ret)
+        dev_err(dev, "create sensor to isp link fail:%d\n", ret);
+
+    return ret;
+}
+
+static int isp4_register_subdev_and_create_links(struct isp4_device 
 *isp_dev,
+                         struct v4l2_subdev *sdev)
+{
Quoted 8 lines

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 59

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.c +6 -0
+    struct device *dev = &isp_dev->pdev->dev;
+    int ret;
+
+    ret = isp4_create_links(isp_dev, sdev);
+    if (ret)
+        dev_err(dev, "fail create isp link:%d\n", ret);

You're discarding ret here.  Shouldn't you return ret in the failure path?

Quoted 1 line

Even if it fails, some Apps which doesn't depend on the media link can still work, so just print error message and ignore it so at least these Apps can still work, do you think it acceptible?>

I think it should be a warn or an info if it's an acceptable situation along with a comment to help us years later understand why the error handling is that way.

Quoted 9 lines
Quoted 8 lines

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 64

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c +5 -0
+
+    ret = v4l2_device_register_subdev_nodes(&isp_dev->v4l2_dev);
+    if (ret != 0) {
+        dev_warn(dev, "register subdev as nodes fail:%d\n", ret);
+        ret = 0;

This isn't fatal?

This call is just expose subdev node to user space, even if it fails, some Apps can still work, so just print error message and ignore it so at least these Apps can still work, do you think it acceptible?>> +    }

I guess same comment as above.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
drivers/media/platform/amd/isp4/isp4.c +12 -0
+
+    return ret;
+}
+
+static int isp4_camera_sensor_bound(struct v4l2_async_notifier 
 *notifier,
+                    struct v4l2_subdev *sensor_sdev,
+                    struct v4l2_async_connection *asd)
+{
+    struct isp4_device *isp_dev = to_isp4_device(notifier->v4l2_dev);
+    struct device *dev = &isp_dev->pdev->dev;
+    int ret;
+
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 83

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.c +6 -0
+    ret = isp4_register_subdev_and_create_links(isp_dev, sensor_sdev);
+    if (ret)
+        dev_err(dev, "register sensor subdev fail:%d\n",
+            ret);
+    else
+        dev_dbg(dev, "register sensor subdev suc\n");

Probably can drop this now

Quoted 1 line

I'd prefer to keeping this for quickly identifying possible camera open issue, do you think it acceptible?

Sure.

Quoted 1,666 lines
Quoted 85 lines
Quoted 85 lines
drivers/media/platform/amd/isp4/isp4.c +72 -1
+    return ret;
+}
+
+static void isp4_camera_sensor_unbind(struct v4l2_async_notifier 
 *notifier,
+                      struct v4l2_subdev *sensor_sdev,
+                      struct v4l2_async_connection *asd)
+{
+}
+
+static const struct v4l2_async_notifier_operations 
 isp4_camera_sensor_ops = {
+    .bound = isp4_camera_sensor_bound,
+    .unbind = isp4_camera_sensor_unbind,
+};
+
+static void isp4_wake_up_resp_thread(struct isp4_subdev *isp, u32 
 index)
+{
+    if (isp && index < ISP4SD_MAX_FW_RESP_STREAM_NUM) {
+        struct isp4sd_thread_handler *thread_ctx =
+                &isp->fw_resp_thread[index];
+
+        thread_ctx->wq_cond = 1;
+        wake_up_interruptible(&thread_ctx->waitq);
+    }
+}
+
+static void isp4_resp_interrupt_notify(struct isp4_subdev *isp, u32 
 intr_status)
+{
+    bool wake = (isp->ispif.status == ISP4IF_STATUS_FW_RUNNING);
+
+    u32 intr_ack = 0;
+
+    /* global response */
+    if (intr_status &
+        ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT12_INT_MASK) {
+        if (wake)
+            isp4_wake_up_resp_thread(isp, 0);
+
+        intr_ack |= 
 ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT12_ACK_MASK;
+    }
+
+    /* stream 1 response */
+    if (intr_status &
+        ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT9_INT_MASK) {
+        if (wake)
+            isp4_wake_up_resp_thread(isp, 1);
+
+        intr_ack |= ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT9_ACK_MASK;
+    }
+
+    /* stream 2 response */
+    if (intr_status &
+        ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT10_INT_MASK) {
+        if (wake)
+            isp4_wake_up_resp_thread(isp, 2);
+
+        intr_ack |= 
 ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT10_ACK_MASK;
+    }
+
+    /* stream 3 response */
+    if (intr_status &
+        ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT11_INT_MASK) {
+        if (wake)
+            isp4_wake_up_resp_thread(isp, 3);
+
+        intr_ack |= 
 ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT11_ACK_MASK;
+    }
+
+    /* clear ISP_SYS interrupts */
+    isp4hw_wreg(ISP4_GET_ISP_REG_BASE(isp), ISP_SYS_INT0_ACK, 
 intr_ack);
+}
+
   static irqreturn_t isp4_irq_handler(int irq, void *arg)
   {
-    struct isp4_device *isp_dev = dev_get_drvdata((struct device 
 *)arg);
+    struct isp4_device *isp_dev = dev_get_drvdata(arg);
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 163

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.c +4 -1
+    struct isp4_subdev *isp = NULL;
+    u32 isp_sys_irq_status = 0x0;
+    u32 r1;
       if (!isp_dev)
-        goto error_drv_data;
+        return IRQ_HANDLED;

As mentioned in the first patch, avoid ping ponging the code.

Thanks, will remove the code ping ponging in next patch>> +

Quoted 9 lines

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 34

Quoted 8 lines
drivers/media/platform/amd/isp4/isp4.c +7 -1
+    isp = &isp_dev->isp_sdev;
+    /* check ISP_SYS interrupts status */
+    r1 = isp4hw_rreg(ISP4_GET_ISP_REG_BASE(isp), ISP_SYS_INT0_STATUS);
+
+    isp_sys_irq_status = r1 & ISP4_FW_RESP_RB_IRQ_STATUS_MASK;
+
+    isp4_resp_interrupt_notify(isp, isp_sys_irq_status);
-error_drv_data:

As mentioned in the first patch, avoid ping ponging the code.

Thanks, will remove the code ping ponging in next patch>>       return IRQ_HANDLED;

Quoted 148 lines
Quoted 148 lines
drivers/media/platform/amd/isp4/isp4.c +78 -0
   }
+static int isp4_parse_fwnode_init_async_nf(struct isp4_device *isp_dev)
+{
+    struct isp4_subdev *isp_sdev = &isp_dev->isp_sdev;
+    struct device *dev = &isp_dev->pdev->dev;
+    struct v4l2_fwnode_endpoint bus_cfg = {
+        .bus_type = V4L2_MBUS_CSI2_DPHY
+    };
+    struct fwnode_handle *remote_ep = NULL;
+    struct fwnode_handle *local_ep = NULL;
+    struct v4l2_async_connection *asd;
+    struct fwnode_handle *fwnode;
+    struct fwnode_endpoint fwep;
+    int ret;
+
+    fwnode = dev_fwnode(dev);
+
+    local_ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
+    if (!local_ep) {
+        ret = -ENXIO;
+        goto err_fwnode;
+    }
+
+    remote_ep = fwnode_graph_get_remote_endpoint(local_ep);
+    if (!remote_ep) {
+        ret = -ENXIO;
+        goto err_fwnode;
+    }
+
+    ret = fwnode_graph_parse_endpoint(remote_ep, &fwep);
+    if (ret)
+        goto err_fwnode;
+    isp_sdev->phy_id = fwep.port;
+
+    ret = v4l2_fwnode_endpoint_alloc_parse(remote_ep, &bus_cfg);
+    if (ret)
+        goto err_fwnode;
+
+    if (!bus_cfg.nr_of_link_frequencies) {
+        ret = -EINVAL;
+        dev_err(dev, "fail invalid link freq number %u\n", 
 bus_cfg.nr_of_link_frequencies);
+        v4l2_fwnode_endpoint_free(&bus_cfg);
+        goto err_fwnode;
+    }
+    isp_sdev->phy_link_freq = *bus_cfg.link_frequencies;
+    v4l2_fwnode_endpoint_free(&bus_cfg);
+
+    isp_sdev->phy_num_data_lanes =
+        fwnode_property_count_u32(remote_ep, "data-lanes");
+
+    v4l2_async_nf_init(&isp_dev->notifier, &isp_dev->v4l2_dev);
+
+    asd = v4l2_async_nf_add_fwnode(&isp_dev->notifier, remote_ep,
+                       struct v4l2_async_connection);
+    if (IS_ERR(asd)) {
+        ret = PTR_ERR(asd);
+        goto err_async_nf_cleanup;
+    }
+
+    isp_dev->notifier.ops = &isp4_camera_sensor_ops;
+    ret = v4l2_async_nf_register(&isp_dev->notifier);
+    if (ret) {
+        dev_err(dev, "v4l2_async_nf_register fail:%d", ret);
+        goto err_async_nf_cleanup;
+    }
+
+    return 0;
+
+err_async_nf_cleanup:
+    v4l2_async_nf_cleanup(&isp_dev->notifier);
+err_fwnode:
+    if (remote_ep)
+        fwnode_handle_put(remote_ep);
+    if (local_ep)
+        fwnode_handle_put(remote_ep);
+
+    return ret;
+}
+
   /*
    * amd capture module

   */
  static int isp4_capture_probe(struct platform_device *pdev) {
      struct device *dev = &pdev->dev;
+    struct isp4_subdev *isp_sdev;
      struct isp4_device *isp_dev;
      int i, irq, ret;

drivers/media/platform/amd/isp4/isp4.c +29 -1
@@ -52,6 +269,17 @@ static int isp4_capture_probe(struct 
 platform_device *pdev)
       isp_dev->pdev = pdev;
       dev->init_name = ISP4_DRV_NAME;
+    isp_sdev = &isp_dev->isp_sdev;
+    isp_sdev->mmio = devm_platform_ioremap_resource(pdev, 0);
+    if (IS_ERR(isp_sdev->mmio))
+        return dev_err_probe(dev, PTR_ERR(isp_sdev->mmio),
+                     "isp ioremap fail\n");
+
+    isp_sdev->isp_phy_mmio = devm_platform_ioremap_resource(pdev, 1);
+    if (IS_ERR(isp_sdev->isp_phy_mmio))
+        return dev_err_probe(dev, PTR_ERR(isp_sdev->isp_phy_mmio),
+                     "isp phy mmio ioremap fail\n");
+
       for (i = 0; i < ARRAY_SIZE(isp4_ringbuf_interrupt_num); i++) {
           irq = platform_get_irq(pdev, isp4_ringbuf_interrupt_num[i]);
           if (irq < 0)
@@ -90,10 +318,23 @@ static int isp4_capture_probe(struct 
 platform_device *pdev)
       dev_dbg(dev, "AMD ISP v4l2 device registered\n");
+    ret = isp4sd_init(&isp_dev->isp_sdev, &isp_dev->v4l2_dev,
+              isp_dev->pltf_data->adev);
+    if (ret) {
+        dev_err(dev, "fail init isp4 sub dev %d\n", ret);
+        goto err_unreg_v4l2;
+    }
+
+    ret = isp4_parse_fwnode_init_async_nf(isp_dev);
+    if (ret) {
+        dev_err(dev, "fail to parse fwnode %d\n", ret);
+        goto err_unreg_v4l2;
+    }
+
       ret = media_device_register(&isp_dev->mdev);
       if (ret) {
           dev_err(dev, "fail to register media device %d\n", ret);
-        goto err_unreg_v4l2;
+        goto err_isp4_deinit;
       }
       platform_set_drvdata(pdev, isp_dev);
@@ -103,6 +344,10 @@ static int isp4_capture_probe(struct 
 platform_device *pdev)
       return 0;
+err_isp4_deinit:
+    v4l2_async_nf_unregister(&isp_dev->notifier);
+    v4l2_async_nf_cleanup(&isp_dev->notifier);
+    isp4sd_deinit(&isp_dev->isp_sdev);

  err_unreg_v4l2:
      v4l2_device_unregister(&isp_dev->v4l2_dev);

drivers/media/platform/amd/isp4/isp4.c +3 -1
@@ -112,11 +357,17 @@ static int isp4_capture_probe(struct 
 platform_device *pdev)
   static void isp4_capture_remove(struct platform_device *pdev)
   {
       struct isp4_device *isp_dev = platform_get_drvdata(pdev);
-    struct device *dev = &pdev->dev;
+
+    v4l2_async_nf_unregister(&isp_dev->notifier);
+    v4l2_async_nf_cleanup(&isp_dev->notifier);
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4.c static int isp4_capture_probe(struct platform_device *pdev) · line 368

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.c +3 -1
+    v4l2_device_unregister_subdev(&isp_dev->isp_sdev.sdev);
       media_device_unregister(&isp_dev->mdev);
+    media_entity_cleanup(&isp_dev->isp_sdev.sdev.entity);
       v4l2_device_unregister(&isp_dev->v4l2_dev);
-    dev_dbg(dev, "AMD ISP v4l2 device unregistered\n");
+    dev_dbg(&pdev->dev, "AMD ISP v4l2 device unregistered\n");

Don't ping pong this

This log will be removed in next patch as you suggested in other comment

Quoted 1,402 lines
Quoted 1,401 lines
drivers/media/platform/amd/isp4/isp4.c +2 -0
+
+    isp4sd_deinit(&isp_dev->isp_sdev);
   }
   static struct platform_driver isp4_capture_drv = {
drivers/media/platform/amd/isp4/isp4.h +3 -1
index 27a7362ce6f9..596431b4a5c2 100644
@@ -7,9 +7,9 @@
   #define _ISP4_H_
   #include <linux/mutex.h>
-#include <media/v4l2-device.h>
   #include <media/videobuf2-memops.h>
   #include <media/videobuf2-vmalloc.h>
+#include "isp4_subdev.h"
   #define ISP4_GET_ISP_REG_BASE(isp4sd) (((isp4sd))->mmio)
@@ -25,11 +25,13 @@ struct isp4_platform_data {
   struct isp4_device {
       struct v4l2_device v4l2_dev;
+    struct isp4_subdev isp_sdev;
       struct media_device mdev;
       struct isp4_platform_data *pltf_data;
       struct platform_device *pdev;
       struct notifier_block i2c_nb;
+    struct v4l2_async_notifier notifier;
   };
   #endif /* isp4.h */
drivers/media/platform/amd/isp4/isp4_subdev.c new file +26 -0
new file mode 100644
index 000000000000..978164031067
@@ -0,0 +1,1192 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/mutex.h>
+#include <linux/pm_domain.h>
+#include <linux/pm_runtime.h>
+
+#include "isp4_fw_cmd_resp.h"
+#include "isp4_hw.h"
+#include "isp4_interface.h"
+#include "isp4_phy.h"
+#include "isp4_subdev.h"
+#include <linux/units.h>
+
+#define ISP4SD_MAX_CMD_RESP_BUF_SIZE (4 * 1024)
+#define ISP4SD_MIN_BUF_CNT_BEF_START_STREAM 4
+
+#define ISP4SD_PERFORMANCE_STATE_LOW 0
+#define ISP4SD_PERFORMANCE_STATE_HIGH 1
+
+#define ISP4SD_FW_CMD_TIMEOUT_IN_MS  500
+#define ISP4SD_WAIT_RESP_IRQ_TIMEOUT  5 /* ms */
+/* align 32KB */
+#define ISP4SD_META_BUF_SIZE ALIGN(sizeof(struct isp4fw_meta_info), 

0x8000)

drivers/media/platform/amd/isp4/isp4_subdev.c new file +27 -0
+
+#define to_isp4_subdev(v4l2_sdev)  \
+    container_of(v4l2_sdev, struct isp4_subdev, sdev)
+
+#define to_isp4_vdev(isp4_vid)  \
+    container_of(isp4_vid, struct isp4_subdev, isp_vdev)
+
+static const char *isp4sd_entity_name = "amd isp4";
+
+struct isp4sd_mbus_image_format_remap {
+    u32                mbus_code;
+    enum isp4fw_image_format    image_format;
+};
+
+static const struct isp4sd_mbus_image_format_remap
+    isp4sd_image_formats[] = {
+    {
+        .mbus_code    = MEDIA_BUS_FMT_YUYV8_1_5X8,
+        .image_format    = IMAGE_FORMAT_NV12,
+    },
+    {
+        .mbus_code    = MEDIA_BUS_FMT_YUYV8_1X16,
+        .image_format    = IMAGE_FORMAT_YUV422INTERLEAVED,
+    },
+};
+
+static void isp4sd_module_enable(struct isp4_subdev *isp_subdev, 

bool enable)

drivers/media/platform/amd/isp4/isp4_subdev.c new file +54 -0
+{
+    if (isp_subdev->enable_gpio) {
+        gpiod_set_value(isp_subdev->enable_gpio, enable ? 1 : 0);
+        dev_dbg(isp_subdev->dev, "%s isp_subdev module\n",
+            enable ? "enable" : "disable");
+    }
+}
+
+static int isp4sd_setup_fw_mem_pool(struct isp4_subdev *isp_subdev)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4fw_cmd_send_buffer buf_type;
+    struct device *dev = isp_subdev->dev;
+    int ret;
+
+    if (!ispif->fw_mem_pool) {
+        dev_err(dev, "fail to alloc mem pool\n");
+        return -ENOMEM;
+    }
+
+    memset(&buf_type, 0, sizeof(buf_type));
+    buf_type.buffer_type = BUFFER_TYPE_MEM_POOL;
+    buf_type.buffer.buf_tags = 0;
+    buf_type.buffer.vmid_space.bit.vmid = 0;
+    buf_type.buffer.vmid_space.bit.space = ADDR_SPACE_TYPE_GPU_VA;
+    isp4if_split_addr64(ispif->fw_mem_pool->gpu_mc_addr,
+                &buf_type.buffer.buf_base_a_lo,
+                &buf_type.buffer.buf_base_a_hi);
+    buf_type.buffer.buf_size_a = (u32)ispif->fw_mem_pool->mem_size;
+
+    ret = isp4if_send_command(ispif, CMD_ID_SEND_BUFFER,
+                  &buf_type, sizeof(buf_type));
+    if (ret) {
+        dev_err(dev, "send fw mem pool 0x%llx(%u) fail %d\n",
+            ispif->fw_mem_pool->gpu_mc_addr,
+            buf_type.buffer.buf_size_a,
+            ret);
+        return ret;
+    }
+
+    dev_dbg(dev, "send fw mem pool 0x%llx(%u) suc\n",
+        ispif->fw_mem_pool->gpu_mc_addr,
+        buf_type.buffer.buf_size_a);
+
+    return 0;
+};
+
+static int isp4sd_set_stream_path(struct isp4_subdev *isp_subdev)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4fw_cmd_set_stream_cfg cmd = {0};
+    struct device *dev = isp_subdev->dev;
+
+    cmd.stream_cfg.mipi_pipe_path_cfg.isp4fw_sensor_id = 

SENSOR_ID_ON_MIPI0;
+    cmd.stream_cfg.mipi_pipe_path_cfg.b_enable = true; +    cmd.stream_cfg.isp_pipe_path_cfg.isp_pipe_id = MIPI0_ISP_PIPELINE_ID;

drivers/media/platform/amd/isp4/isp4_subdev.c new file +105 -0
+
+    cmd.stream_cfg.b_enable_tnr = true;
+    dev_dbg(dev, "isp4fw_sensor_id %d, pipeId 0x%x EnableTnr %u\n",
+        cmd.stream_cfg.mipi_pipe_path_cfg.isp4fw_sensor_id,
+        cmd.stream_cfg.isp_pipe_path_cfg.isp_pipe_id,
+        cmd.stream_cfg.b_enable_tnr);
+
+    return isp4if_send_command(ispif, CMD_ID_SET_STREAM_CONFIG,
+                   &cmd, sizeof(cmd));
+}
+
+static int isp4sd_send_meta_buf(struct isp4_subdev *isp_subdev)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4fw_cmd_send_buffer buf_type = { 0 };
+    struct isp4sd_sensor_info *sensor_info;
+    struct device *dev = isp_subdev->dev;
+    u32 i;
+
+    sensor_info = &isp_subdev->sensor_info;
+    for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++) {
+        int ret;
+
+        if (!sensor_info->meta_info_buf[i]) {
+            dev_err(dev, "fail for no meta info buf(%u)\n", i);
+            return -ENOMEM;
+        }
+        buf_type.buffer_type = BUFFER_TYPE_META_INFO;
+        buf_type.buffer.buf_tags = 0;
+        buf_type.buffer.vmid_space.bit.vmid = 0;
+        buf_type.buffer.vmid_space.bit.space = ADDR_SPACE_TYPE_GPU_VA;
+        isp4if_split_addr64(sensor_info->meta_info_buf[i]->gpu_mc_addr,
+                    &buf_type.buffer.buf_base_a_lo,
+                    &buf_type.buffer.buf_base_a_hi);
+        buf_type.buffer.buf_size_a =
+            (u32)sensor_info->meta_info_buf[i]->mem_size;
+        ret = isp4if_send_command(ispif, CMD_ID_SEND_BUFFER,
+                      &buf_type,
+                      sizeof(buf_type));
+        if (ret) {
+            dev_err(dev, "send meta info(%u) fail\n", i);
+            return ret;
+        }
+    }
+
+    dev_dbg(dev, "send meta info suc\n");
+    return 0;
+}
+
+static int isp4sd_enum_mbus_code(struct v4l2_subdev *isp_subdev,
+                 struct v4l2_subdev_state *state,
+                 struct v4l2_subdev_mbus_code_enum *code_enum)
+{
+    if (code_enum->index >= ARRAY_SIZE(isp4sd_image_formats))
+        return -EINVAL;
+
+    code_enum->code = isp4sd_image_formats[code_enum->index].mbus_code;
+
+    return 0;
+}
+
+static bool isp4sd_get_str_out_prop(struct isp4_subdev *isp_subdev,
+                    struct isp4fw_image_prop *out_prop,
+                    struct v4l2_subdev_state *state, u32 pad)
+{
+    struct v4l2_mbus_framefmt *format = NULL;
+    struct device *dev = isp_subdev->dev;
+    bool ret;
+
+    format = v4l2_subdev_state_get_format(state, pad, 0);
+    if (!format) {
+        dev_err(dev, "fail get subdev state format\n");
+        return false;
+    }
+
+    switch (format->code) {
+    case MEDIA_BUS_FMT_YUYV8_1_5X8:
+        out_prop->image_format = IMAGE_FORMAT_NV12;
+        out_prop->width = format->width;
+        out_prop->height = format->height;
+        out_prop->luma_pitch = format->width;
+        out_prop->chroma_pitch = out_prop->width;
+        ret = true;
+        break;
+    case MEDIA_BUS_FMT_YUYV8_1X16:
+        out_prop->image_format = IMAGE_FORMAT_YUV422INTERLEAVED;
+        out_prop->width = format->width;
+        out_prop->height = format->height;
+        out_prop->luma_pitch = format->width * 2;
+        out_prop->chroma_pitch = 0;
+        ret = true;
+        break;
+    default:
+        dev_err(dev, "fail for bad image format:0x%x\n",
+            format->code);
+        ret = false;
+        break;
+    }
+
+    if (!out_prop->width || !out_prop->height)
+        ret = false;
+    return ret;
+}
+
+static int isp4sd_kickoff_stream(struct isp4_subdev *isp_subdev, u32 

w, u32 h)

drivers/media/platform/amd/isp4/isp4_subdev.c new file +111 -0
+{
+    struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+
+    if (sensor_info->status == ISP4SD_START_STATUS_STARTED) {
+        return 0;
+    } else if (sensor_info->status == ISP4SD_START_STATUS_START_FAIL) {
+        dev_err(dev, "fail for previous start fail\n");
+        return -EINVAL;
+    }
+
+    dev_dbg(dev, "w:%u,h:%u\n", w, h);
+
+    sensor_info->status = ISP4SD_START_STATUS_START_FAIL;
+
+    if (isp4sd_send_meta_buf(isp_subdev)) {
+        dev_err(dev, "fail to send meta buf\n");
+        return -EINVAL;
+    };
+
+    sensor_info->status = ISP4SD_START_STATUS_NOT_START;
+
+    if (!sensor_info->start_stream_cmd_sent &&
+        sensor_info->buf_sent_cnt >=
+        ISP4SD_MIN_BUF_CNT_BEF_START_STREAM) {
+        int ret = isp4if_send_command(ispif, CMD_ID_START_STREAM,
+                          NULL, 0);
+        if (ret) {
+            dev_err(dev, "fail to start stream\n");
+            return ret;
+        }
+
+        sensor_info->start_stream_cmd_sent = true;
+    } else {
+        dev_dbg(dev,
+            "no send START_STREAM, start_sent %u, buf_sent %u\n",
+            sensor_info->start_stream_cmd_sent,
+            sensor_info->buf_sent_cnt);
+    }
+
+    return 0;
+}
+
+static int isp4sd_setup_output(struct isp4_subdev *isp_subdev,
+                   struct v4l2_subdev_state *state, u32 pad)
+{
+    struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+    struct isp4sd_output_info *output_info =
+            &isp_subdev->sensor_info.output_info;
+    struct isp4fw_cmd_set_out_ch_prop cmd_ch_prop = {0};
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4fw_cmd_enable_out_ch cmd_ch_en = {0};
+    struct device *dev = isp_subdev->dev;
+    struct isp4fw_image_prop *out_prop;
+    int ret;
+
+    if (output_info->start_status == ISP4SD_START_STATUS_STARTED)
+        return 0;
+
+    if (output_info->start_status == ISP4SD_START_STATUS_START_FAIL) {
+        dev_err(dev, "fail for previous start fail\n");
+        return -EINVAL;
+    }
+
+    out_prop = &cmd_ch_prop.image_prop;
+    cmd_ch_prop.ch = ISP_PIPE_OUT_CH_PREVIEW;
+    cmd_ch_en.ch = ISP_PIPE_OUT_CH_PREVIEW;
+    cmd_ch_en.is_enable = true;
+
+    if (!isp4sd_get_str_out_prop(isp_subdev, out_prop, state, pad)) {
+        dev_err(dev, "fail to get out prop\n");
+        return -EINVAL;
+    }
+
+    dev_dbg(dev, "channel: w:h=%u:%u,lp:%u,cp%u\n",
+        cmd_ch_prop.image_prop.width, cmd_ch_prop.image_prop.height,
+        cmd_ch_prop.image_prop.luma_pitch,
+        cmd_ch_prop.image_prop.chroma_pitch);
+
+    ret = isp4if_send_command(ispif, CMD_ID_SET_OUT_CHAN_PROP,
+                  &cmd_ch_prop,
+                  sizeof(cmd_ch_prop));
+    if (ret) {
+        output_info->start_status = ISP4SD_START_STATUS_START_FAIL;
+        dev_err(dev, "fail to set out prop\n");
+        return ret;
+    };
+
+    ret = isp4if_send_command(ispif, CMD_ID_ENABLE_OUT_CHAN,
+                  &cmd_ch_en, sizeof(cmd_ch_en));
+
+    if (ret) {
+        output_info->start_status = ISP4SD_START_STATUS_START_FAIL;
+        dev_err(dev, "fail to enable channel\n");
+        return ret;
+    }
+
+    if (!sensor_info->start_stream_cmd_sent) {
+        ret = isp4sd_kickoff_stream(isp_subdev, out_prop->width,
+                        out_prop->height);
+        if (ret) {
+            dev_err(dev, "kickoff stream fail %d\n", ret);
+            return ret;
+        }
+        /* sensor_info->start_stream_cmd_sent will be set to true
+         * 1. in isp4sd_kickoff_stream, if app first send buffer then
+         * start stream
+         * 2. in isp_set_stream_buf, if app first start stream, then
+         * send buffer
+         * because ISP FW has the requirement, host needs to send 

buffer
+         * before send start stream cmd
+         */
+        if (sensor_info->start_stream_cmd_sent) {
+            sensor_info->status = ISP4SD_START_STATUS_STARTED; +            output_info->start_status = ISP4SD_START_STATUS_STARTED; +            dev_dbg(dev, "kickoff stream suc,start cmd sent\n"); +        } else {
+            dev_dbg(dev, "kickoff stream suc,start cmd not sent\n"); +        }
+    } else {
+        dev_dbg(dev, "stream running, no need kickoff\n"); +        output_info->start_status = ISP4SD_START_STATUS_STARTED; +    }

drivers/media/platform/amd/isp4/isp4_subdev.c new file +134 -0
+
+    dev_dbg(dev, "setup output suc\n");
+    return 0;
+}
+
+static int isp4sd_alloc_meta_buf(struct isp4_subdev *isp_subdev)
+{
+    struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+    u32 i;
+
+    /* TODO: check alloc method */
+    for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++) {
+        if (!sensor_info->meta_info_buf[i]) {
+            sensor_info->meta_info_buf[i] =
+                ispif->metainfo_buf_pool[i];
+            if (sensor_info->meta_info_buf[i]) {
+                dev_dbg(dev, "valid %u meta_info_buf ok\n", i);
+            } else {
+                dev_err(dev,
+                    "invalid %u meta_info_buf fail\n", i);
+                return -ENOMEM;
+            }
+        }
+    }
+
+    return 0;
+}
+
+static int isp4sd_init_stream(struct isp4_subdev *isp_subdev)
+{
+    struct device *dev = isp_subdev->dev;
+    int ret;
+
+    ret  = isp4sd_setup_fw_mem_pool(isp_subdev);
+    if (ret) {
+        dev_err(dev, "fail to  setup fw mem pool\n");
+        return ret;
+    }
+
+    ret  = isp4sd_alloc_meta_buf(isp_subdev);
+    if (ret) {
+        dev_err(dev, "fail to alloc fw driver shared buf\n");
+        return ret;
+    }
+
+    ret = isp4sd_set_stream_path(isp_subdev);
+    if (ret) {
+        dev_err(dev, "fail to setup stream path\n");
+        return ret;
+    }
+
+    return 0;
+}
+
+static void isp4sd_reset_stream_info(struct isp4_subdev *isp_subdev,
+                     struct v4l2_subdev_state *state, u32 pad)
+{
+    struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+    struct v4l2_mbus_framefmt *format = NULL;
+    struct isp4sd_output_info *str_info;
+    int i;
+
+    format = v4l2_subdev_state_get_format(state,
+                          pad,
+                          0);
+
+    if (!format) {
+        pr_err("fail reset stream info for not get format\n");
+
+    } else {
+        memset(format, 0, sizeof(*format));
+        format->code = MEDIA_BUS_FMT_YUYV8_1_5X8;
+    }
+
+    for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++)
+        sensor_info->meta_info_buf[i] = NULL;
+
+    str_info = &sensor_info->output_info;
+    str_info->start_status = ISP4SD_START_STATUS_NOT_START;
+}
+
+static bool isp4sd_is_stream_running(struct isp4_subdev *isp_subdev)
+{
+    struct isp4sd_sensor_info *sif;
+    enum isp4sd_start_status stat;
+
+    sif = &isp_subdev->sensor_info;
+    stat = sif->output_info.start_status;
+    if (stat == ISP4SD_START_STATUS_STARTED)
+        return true;
+
+    return false;
+}
+
+static void isp4sd_reset_camera_info(struct isp4_subdev *isp_subdev,
+                     struct v4l2_subdev_state *state, u32 pad)
+{
+    struct isp4sd_sensor_info *info  = &isp_subdev->sensor_info;
+
+    info->status = ISP4SD_START_STATUS_NOT_START;
+    isp4sd_reset_stream_info(isp_subdev, state, pad);
+
+    info->start_stream_cmd_sent = false;
+}
+
+static int isp4sd_uninit_stream(struct isp4_subdev *isp_subdev,
+                struct v4l2_subdev_state *state, u32 pad)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+    bool running;
+
+    running = isp4sd_is_stream_running(isp_subdev);
+
+    if (running) {
+        dev_dbg(dev, "fail for stream is still running\n");
+        return -EINVAL;
+    }
+
+    isp4sd_reset_camera_info(isp_subdev, state, pad);
+
+    isp4if_clear_cmdq(ispif);
+    return 0;
+}
+
+static void isp4sd_fw_resp_cmd_done(struct isp4_subdev *isp_subdev,
+                    enum isp4if_stream_id stream_id,
+                    struct isp4fw_resp_cmd_done *para)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4if_cmd_element *ele =
+        isp4if_rm_cmd_from_cmdq(ispif, para->cmd_seq_num, para- 
Quoted 1 line

cmd_id);

+    struct device *dev = isp_subdev->dev;

drivers/media/platform/amd/isp4/isp4_subdev.c new file +74 -0
+
+    dev_dbg(dev, "stream %d,cmd (0x%08x)(%d),seq %u, ele %p\n",
+        stream_id,
+        para->cmd_id, para->cmd_status, para->cmd_seq_num,
+        ele);
+
+    if (!ele)
+        return;
+
+    if (ele->wq) {
+        dev_dbg(dev, "signal event %p\n", ele->wq);
+        if (ele->wq_cond)
+            *ele->wq_cond = 1;
+        wake_up(ele->wq);
+    }
+
+    kfree(ele);
+}
+
+static struct isp4fw_meta_info *
+isp4sd_get_meta_by_mc(struct isp4_subdev *isp_subdev,
+              u64 mc)
+{
+    u32 i;
+
+    for (i = 0; i < ISP4IF_MAX_STREAM_META_BUF_COUNT; i++) {
+        struct isp4if_gpu_mem_info *meta_info_buf =
+                isp_subdev->sensor_info.meta_info_buf[i];
+
+        if (meta_info_buf) {
+            if (mc == meta_info_buf->gpu_mc_addr)
+                return meta_info_buf->sys_addr;
+        }
+    }
+    return NULL;
+};
+
+static struct isp4if_img_buf_node *
+isp4sd_preview_done(struct isp4_subdev *isp_subdev,
+            struct isp4fw_meta_info *meta)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4if_img_buf_node *prev = NULL;
+    struct device *dev = isp_subdev->dev;
+
+    if (!meta) {
+        dev_err(dev, "fail bad param for preview done\n");
+        return prev;
+    }
+
+    if (meta->preview.enabled &&
+        (meta->preview.status == BUFFER_STATUS_SKIPPED ||
+         meta->preview.status == BUFFER_STATUS_DONE ||
+         meta->preview.status == BUFFER_STATUS_DIRTY)) {
+        prev = isp4if_dequeue_buffer(ispif);
+        if (!prev)
+            dev_err(dev, "fail null prev buf\n");
+
+    } else if (meta->preview.enabled) {
+        dev_err(dev, "fail bad preview status %u\n",
+            meta->preview.status);
+    }
+
+    return prev;
+}
+
+static void isp4sd_send_meta_info(struct isp4_subdev *isp_subdev,
+                  u64 meta_info_mc)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4fw_cmd_send_buffer buf_type;
+    struct device *dev = isp_subdev->dev;
+
+    if (isp_subdev->sensor_info.status != 

ISP4SD_START_STATUS_STARTED) {
+        dev_warn(dev, "not working status %i, meta_info 0x%llx\n", +             isp_subdev->sensor_info.status, meta_info_mc); +        return;
+    }

drivers/media/platform/amd/isp4/isp4_subdev.c new file +32 -0
+
+    if (meta_info_mc) {
+        memset(&buf_type, 0, sizeof(buf_type));
+        buf_type.buffer_type = BUFFER_TYPE_META_INFO;
+        buf_type.buffer.buf_tags = 0;
+        buf_type.buffer.vmid_space.bit.vmid = 0;
+        buf_type.buffer.vmid_space.bit.space = ADDR_SPACE_TYPE_GPU_VA;
+        isp4if_split_addr64(meta_info_mc,
+                    &buf_type.buffer.buf_base_a_lo,
+                    &buf_type.buffer.buf_base_a_hi);
+
+        buf_type.buffer.buf_size_a = ISP4SD_META_BUF_SIZE;
+        if (isp4if_send_command(ispif, CMD_ID_SEND_BUFFER,
+                    &buf_type, sizeof(buf_type))) {
+            dev_err(dev, "fail send meta_info 0x%llx\n",
+                meta_info_mc);
+        } else {
+            dev_dbg(dev, "resend meta_info 0x%llx\n", meta_info_mc);
+        }
+    }
+}
+
+static void isp4sd_fw_resp_frame_done(struct isp4_subdev *isp_subdev,
+                      enum isp4if_stream_id stream_id,
+                      struct isp4fw_resp_param_package *para)
+{
+    struct isp4if_img_buf_node *prev = NULL;
+    struct device *dev = isp_subdev->dev;
+    struct isp4fw_meta_info *meta;
+    u64 mc = 0;
+
+    mc = isp4if_join_addr64(para->package_addr_lo, para- 
Quoted 1 line

package_addr_hi);

+    meta = isp4sd_get_meta_by_mc(isp_subdev, mc); +    if (mc == 0 || !meta) {
+        dev_err(dev, "fail to get meta from mc %llx\n", mc); +        return;
+    }

drivers/media/platform/amd/isp4/isp4_subdev.c new file +110 -0
+
+    dev_dbg(dev, "ts:%llu,streamId:%d,poc:%u,preview_en:%u,(%i)\n",
+        ktime_get_ns(), stream_id, meta->poc,
+        meta->preview.enabled,
+        meta->preview.status);
+
+    prev = isp4sd_preview_done(isp_subdev, meta);
+
+    isp4if_dealloc_buffer_node(prev);
+
+    if (isp_subdev->sensor_info.status == ISP4SD_START_STATUS_STARTED)
+        isp4sd_send_meta_info(isp_subdev, mc);
+
+    dev_dbg(dev, "stream_id:%d, status:%d\n", stream_id,
+        isp_subdev->sensor_info.status);
+}
+
+static void isp4sd_fw_resp_func(struct isp4_subdev *isp_subdev,
+                enum isp4if_stream_id stream_id)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+    struct isp4fw_resp resp;
+
+    if (ispif->status < ISP4IF_STATUS_FW_RUNNING)
+        return;
+
+    while (true) {
+        s32 ret;
+
+        ret = isp4if_f2h_resp(ispif, stream_id, &resp);
+        if (ret)
+            break;
+
+        switch (resp.resp_id) {
+        case RESP_ID_CMD_DONE:
+            isp4sd_fw_resp_cmd_done(isp_subdev, stream_id,
+                        &resp.param.cmd_done);
+            break;
+        case RESP_ID_NOTI_FRAME_DONE:
+            isp4sd_fw_resp_frame_done(isp_subdev, stream_id,
+                          &resp.param.frame_done);
+            break;
+        default:
+            dev_err(dev, "-><- fail respid (0x%x)\n",
+                resp.resp_id);
+            break;
+        }
+    }
+}
+
+static s32 isp4sd_fw_resp_thread_wrapper(void *context)
+{
+    struct isp4_subdev_thread_param *para = context;
+    struct isp4sd_thread_handler *thread_ctx;
+    enum isp4if_stream_id stream_id;
+
+    struct isp4_subdev *isp_subdev;
+    struct device *dev;
+    u64 timeout;
+
+    if (!para)
+        return -EINVAL;
+
+    isp_subdev = para->isp_subdev;
+    dev = isp_subdev->dev;
+
+    switch (para->idx) {
+    case 0:
+        stream_id = ISP4IF_STREAM_ID_GLOBAL;
+        break;
+    case 1:
+        stream_id = ISP4IF_STREAM_ID_1;
+        break;
+    default:
+        dev_err(dev, "fail invalid %d\n", para->idx);
+        return -EINVAL;
+    }
+
+    thread_ctx = &isp_subdev->fw_resp_thread[para->idx];
+
+    thread_ctx->wq_cond = 0;
+    mutex_init(&thread_ctx->mutex);
+    init_waitqueue_head(&thread_ctx->waitq);
+    timeout = msecs_to_jiffies(ISP4SD_WAIT_RESP_IRQ_TIMEOUT);
+
+    dev_dbg(dev, "[%u] started\n", para->idx);
+
+    while (true) {
+        wait_event_interruptible_timeout(thread_ctx->waitq,
+                         thread_ctx->wq_cond != 0,
+                         timeout);
+        thread_ctx->wq_cond = 0;
+
+        if (kthread_should_stop()) {
+            dev_dbg(dev, "[%u] quit\n", para->idx);
+            break;
+        }
+
+        mutex_lock(&thread_ctx->mutex);
+        isp4sd_fw_resp_func(isp_subdev, stream_id);
+        mutex_unlock(&thread_ctx->mutex);
+    }
+
+    mutex_destroy(&thread_ctx->mutex);
+
+    return 0;
+}
+
+static int isp4sd_start_resp_proc_threads(struct isp4_subdev 

*isp_subdev)

drivers/media/platform/amd/isp4/isp4_subdev.c new file +24 -0
+{
+    struct device *dev = isp_subdev->dev;
+    int i;
+
+    for (i = 0; i < ISP4SD_MAX_FW_RESP_STREAM_NUM; i++) {
+        struct isp4sd_thread_handler *thread_ctx =
+                &isp_subdev->fw_resp_thread[i];
+
+        isp_subdev->isp_resp_para[i].idx = i;
+        isp_subdev->isp_resp_para[i].isp_subdev = isp_subdev;
+
+        thread_ctx->thread = kthread_run(isp4sd_fw_resp_thread_wrapper,
+                         &isp_subdev->isp_resp_para[i],
+                         "amd_isp4_thread");
+        if (IS_ERR(thread_ctx->thread)) {
+            dev_err(dev, "create thread [%d] fail\n", i);
+            return -EINVAL;
+        }
+    }
+
+    return 0;
+}
+
+static int isp4sd_stop_resp_proc_threads(struct isp4_subdev 

*isp_subdev)

drivers/media/platform/amd/isp4/isp4_subdev.c new file +17 -0
+{
+    int i;
+
+    for (i = 0; i < ISP4SD_MAX_FW_RESP_STREAM_NUM; i++) {
+        struct isp4sd_thread_handler *thread_ctx =
+                &isp_subdev->fw_resp_thread[i];
+
+        if (thread_ctx->thread) {
+            kthread_stop(thread_ctx->thread);
+            thread_ctx->thread = NULL;
+        }
+    }
+
+    return 0;
+}
+
+static u32 isp4sd_get_started_stream_count(struct isp4_subdev 

*isp_subdev)

drivers/media/platform/amd/isp4/isp4_subdev.c new file +233 -0
+{
+    u32 cnt = 0;
+
+    if (isp_subdev->sensor_info.status == ISP4SD_START_STATUS_STARTED)
+        cnt++;
+    return cnt;
+}
+
+static int isp4sd_pwroff_and_deinit(struct isp4_subdev *isp_subdev)
+{
+    struct isp4sd_sensor_info *sensor_info = &isp_subdev->sensor_info;
+    unsigned int perf_state = ISP4SD_PERFORMANCE_STATE_LOW;
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+
+    struct device *dev = isp_subdev->dev;
+    u32 cnt;
+    int ret;
+
+    mutex_lock(&isp_subdev->ops_mutex);
+
+    if (sensor_info->status == ISP4SD_START_STATUS_STARTED) {
+        dev_err(dev, "fail for stream still running\n");
+        mutex_unlock(&isp_subdev->ops_mutex);
+        return -EINVAL;
+    }
+
+    sensor_info->status = ISP4SD_START_STATUS_NOT_START;
+    cnt = isp4sd_get_started_stream_count(isp_subdev);
+    if (cnt > 0) {
+        dev_dbg(dev, "no need power off isp_subdev\n");
+        mutex_unlock(&isp_subdev->ops_mutex);
+        return 0;
+    }
+
+    isp4if_stop(ispif);
+
+    ret = dev_pm_genpd_set_performance_state(dev, perf_state);
+    if (ret)
+        dev_err(dev,
+            "fail to set isp_subdev performance state %u,ret %d\n",
+            perf_state, ret);
+    isp4sd_stop_resp_proc_threads(isp_subdev);
+    dev_dbg(dev, "isp_subdev stop resp proc streads suc");
+    /* hold ccpu reset */
+    isp4hw_wreg(isp_subdev->mmio, ISP_SOFT_RESET, 0x0);
+    isp4hw_wreg(isp_subdev->mmio, ISP_POWER_STATUS, 0);
+    ret = pm_runtime_put_sync(dev);
+    if (ret)
+        dev_err(dev, "power off isp_subdev fail %d\n", ret);
+    else
+        dev_dbg(dev, "power off isp_subdev suc\n");
+
+    ispif->status = ISP4IF_STATUS_PWR_OFF;
+    isp4if_clear_cmdq(ispif);
+    isp4sd_module_enable(isp_subdev, false);
+
+    msleep(20);
+
+    mutex_unlock(&isp_subdev->ops_mutex);
+
+    return 0;
+}
+
+static int isp4sd_pwron_and_init(struct isp4_subdev *isp_subdev)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+    int ret;
+
+    if (ispif->status == ISP4IF_STATUS_FW_RUNNING) {
+        dev_dbg(dev, "camera already opened, do nothing\n");
+        return 0;
+    }
+
+    mutex_lock(&isp_subdev->ops_mutex);
+
+    isp4sd_module_enable(isp_subdev, true);
+
+    isp_subdev->sensor_info.start_stream_cmd_sent = false;
+    isp_subdev->sensor_info.buf_sent_cnt = 0;
+
+    if (ispif->status < ISP4IF_STATUS_PWR_ON) {
+        unsigned int perf_state = ISP4SD_PERFORMANCE_STATE_HIGH;
+
+        ret = pm_runtime_resume_and_get(dev);
+        if (ret) {
+            dev_err(dev, "fail to power on isp_subdev ret %d\n",
+                ret);
+            goto err_unlock_and_close;
+        }
+
+        /* ISPPG ISP Power Status */
+        isp4hw_wreg(isp_subdev->mmio, ISP_POWER_STATUS, 0x7FF);
+        ret = dev_pm_genpd_set_performance_state(dev, perf_state);
+        if (ret) {
+            dev_err(dev,
+                "fail to set performance state %u, ret %d\n",
+                perf_state, ret);
+            goto err_unlock_and_close;
+        }
+
+        ispif->status = ISP4IF_STATUS_PWR_ON;
+
+        if (isp4sd_start_resp_proc_threads(isp_subdev)) {
+            dev_err(dev, "isp_start_resp_proc_threads fail");
+            goto err_unlock_and_close;
+        } else {
+            dev_dbg(dev, "create resp threads ok");
+        }
+    }
+
+    isp_subdev->sensor_info.start_stream_cmd_sent = false;
+    isp_subdev->sensor_info.buf_sent_cnt = 0;
+
+    ret = isp4if_start(ispif);
+    if (ret) {
+        dev_err(dev, "fail to start isp_subdev interface\n");
+        goto err_unlock_and_close;
+    }
+
+    mutex_unlock(&isp_subdev->ops_mutex);
+    return 0;
+err_unlock_and_close:
+    mutex_unlock(&isp_subdev->ops_mutex);
+    isp4sd_pwroff_and_deinit(isp_subdev);
+    return -EINVAL;
+}
+
+static int isp4sd_stop_stream(struct isp4_subdev *isp_subdev,
+                  struct v4l2_subdev_state *state, u32 pad)
+{
+    struct isp4sd_output_info *output_info =
+            &isp_subdev->sensor_info.output_info;
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+    int ret = 0;
+
+    dev_dbg(dev, "status %i\n", output_info->start_status);
+    mutex_lock(&isp_subdev->ops_mutex);
+
+    if (output_info->start_status == ISP4SD_START_STATUS_STARTED) {
+        struct isp4fw_cmd_enable_out_ch cmd_ch_disable;
+
+        cmd_ch_disable.ch = ISP_PIPE_OUT_CH_PREVIEW;
+        cmd_ch_disable.is_enable = false;
+        ret = isp4if_send_command_sync(ispif,
+                           CMD_ID_ENABLE_OUT_CHAN,
+                           &cmd_ch_disable,
+                           sizeof(cmd_ch_disable),
+                           ISP4SD_FW_CMD_TIMEOUT_IN_MS);
+        if (ret)
+            dev_err(dev, "fail to disable stream\n");
+        else
+            dev_dbg(dev, "wait disable stream suc\n");
+
+        ret = isp4if_send_command_sync(ispif, CMD_ID_STOP_STREAM,
+                           NULL,
+                           0,
+                           ISP4SD_FW_CMD_TIMEOUT_IN_MS);
+        if (ret)
+            dev_err(dev, "fail to stop steam\n");
+        else
+            dev_dbg(dev, "wait stop stream suc\n");
+    }
+
+    isp4if_clear_bufq(ispif);
+
+    output_info->start_status = ISP4SD_START_STATUS_NOT_START;
+    isp4sd_reset_stream_info(isp_subdev, state, pad);
+
+    mutex_unlock(&isp_subdev->ops_mutex);
+
+    isp4sd_uninit_stream(isp_subdev, state, pad);
+
+    return ret;
+}
+
+static int isp4sd_start_stream(struct isp4_subdev *isp_subdev,
+                   struct v4l2_subdev_state *state, u32 pad)
+{
+    struct isp4sd_output_info *output_info =
+            &isp_subdev->sensor_info.output_info;
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct device *dev = isp_subdev->dev;
+    int ret;
+
+    mutex_lock(&isp_subdev->ops_mutex);
+
+    if (ispif->status != ISP4IF_STATUS_FW_RUNNING) {
+        mutex_unlock(&isp_subdev->ops_mutex);
+        dev_err(dev, "fail, bad fsm %d", ispif->status);
+        return -EINVAL;
+    }
+
+    ret = isp4sd_init_stream(isp_subdev);
+
+    if (ret) {
+        dev_err(dev, "fail to init isp_subdev stream\n");
+        ret = -EINVAL;
+        goto unlock_and_check_ret;
+    }
+
+    if (output_info->start_status == ISP4SD_START_STATUS_STARTED) {
+        ret = 0;
+        dev_dbg(dev, "stream started, do nothing\n");
+        goto unlock_and_check_ret;
+    } else if (output_info->start_status ==
+           ISP4SD_START_STATUS_START_FAIL) {
+        ret = -EINVAL;
+        dev_err(dev, "stream  fail to start before\n");
+        goto unlock_and_check_ret;
+    }
+
+    if (isp4sd_setup_output(isp_subdev, state, pad)) {
+        dev_err(dev, "fail to setup output\n");
+        ret = -EINVAL;
+    } else {
+        ret = 0;
+        dev_dbg(dev, "suc to setup out\n");
+    }
+unlock_and_check_ret:
+    mutex_unlock(&isp_subdev->ops_mutex);
+    if (ret) {
+        isp4sd_stop_stream(isp_subdev, state, pad);
+        dev_err(dev, "start stream fail\n");
+    } else {
+        dev_dbg(dev, "start stream suc\n");
+    }
+
+    return ret;
+}
+
+static int isp4sd_subdev_pre_streamon(struct v4l2_subdev *sd, u32 

flags)
+{
+    struct isp4_subdev *isp_subdev = to_isp4_subdev(sd); +    u64 phy_bit_rate = isp_subdev->phy_link_freq * isp_subdev-

Quoted 1 line

phy_num_data_lanes / HZ_PER_MHZ;

+    u32 num_data_lanes = isp_subdev->phy_num_data_lanes; +    u32 phy_id = isp_subdev->phy_id;
+    int ret;

drivers/media/platform/amd/isp4/isp4_subdev.c new file +12 -0
+
+    ret = isp4phy_start(isp_subdev->dev,
+                isp_subdev->isp_phy_mmio, phy_id,
+                phy_bit_rate, num_data_lanes);
+    if (ret) {
+        dev_err(isp_subdev->dev,
+            "fail start phy,lane %d id %u bitrate %llu, %d\n",
+            num_data_lanes, phy_id, phy_bit_rate, ret);
+        return ret;
+    }
+
+    dev_dbg(isp_subdev->dev, "start phy suc,lane %d id %u bit_rate 

%llu\n",
+        num_data_lanes, phy_id, phy_bit_rate);

drivers/media/platform/amd/isp4/isp4_subdev.c new file +57 -0
+
+    return ret;
+}
+
+static int isp4sd_subdev_post_streamoff(struct v4l2_subdev *sd)
+{
+    struct isp4_subdev *isp_subdev = to_isp4_subdev(sd);
+    int ret;
+
+    dev_dbg(isp_subdev->dev, "stopping phy %u\n", isp_subdev->phy_id);
+    ret = isp4phy_stop(isp_subdev->isp_phy_mmio,
+               isp_subdev->phy_id);
+    if (ret)
+        dev_err(isp_subdev->dev, "fail to stop the Phy:%d", ret);
+
+    return ret;
+}
+
+static int isp4sd_set_power(struct v4l2_subdev *sd, int on)
+{
+    struct isp4_subdev *ispsd = to_isp4_subdev(sd);
+
+    if (on)
+        return isp4sd_pwron_and_init(ispsd);
+    else
+        return isp4sd_pwroff_and_deinit(ispsd);
+};
+
+static const struct v4l2_subdev_core_ops isp4sd_core_ops = {
+    .s_power = isp4sd_set_power,
+};
+
+static const struct v4l2_subdev_video_ops isp4sd_video_ops = {
+    .s_stream = v4l2_subdev_s_stream_helper,
+    .pre_streamon = isp4sd_subdev_pre_streamon,
+    .post_streamoff = isp4sd_subdev_post_streamoff,
+};
+
+static int isp4sd_set_pad_format(struct v4l2_subdev *sd,
+                 struct v4l2_subdev_state *sd_state,
+                 struct v4l2_subdev_format *fmt)
+{
+    struct isp4sd_output_info *steam_info =
+        &(to_isp4_subdev(sd)->sensor_info.output_info);
+    struct v4l2_mbus_framefmt *format;
+
+    format = v4l2_subdev_state_get_format(sd_state, fmt->pad);
+
+    if (!format) {
+        dev_err(sd->dev, "fail to get state format\n");
+        return -EINVAL;
+    }
+
+    *format = fmt->format;
+    switch (format->code) {
+    case MEDIA_BUS_FMT_YUYV8_1_5X8:
+        steam_info->image_size = format->width * format->height * 

3 / 2;
+        break;
+    case MEDIA_BUS_FMT_YUYV8_1X16:
+        steam_info->image_size = format->width * format->height * 2; +        break;
+    default:
+        steam_info->image_size = 0;
+        break;
+    }
+    if (!steam_info->image_size) {
+        dev_err(sd->dev,
+            "fail set pad format,code 0x%x,width %u, height %u\n", +            format->code, format->width, format->height); +        return -EINVAL;
+    }
+    dev_dbg(sd->dev,
+        "set pad format suc, code:%x w:%u h:%u size:%u\n", format-

Quoted 1 line

code,

+        format->width, format->height, steam_info->image_size);

drivers/media/platform/amd/isp4/isp4_subdev.c new file +60 -0
+
+    return 0;
+}
+
+static int isp4sd_enable_streams(struct v4l2_subdev *sd,
+                 struct v4l2_subdev_state *state, u32 pad,
+                 u64 streams_mask)
+{
+    struct isp4_subdev *ispsd = to_isp4_subdev(sd);
+
+    return isp4sd_start_stream(ispsd, state, pad);
+}
+
+static int isp4sd_disable_streams(struct v4l2_subdev *sd,
+                  struct v4l2_subdev_state *state, u32 pad,
+                  u64 streams_mask)
+{
+    struct isp4_subdev *ispsd = to_isp4_subdev(sd);
+
+    return isp4sd_stop_stream(ispsd, state, pad);
+}
+
+static const struct v4l2_subdev_pad_ops isp4sd_pad_ops = {
+    .enum_mbus_code    = isp4sd_enum_mbus_code,
+    .get_fmt = v4l2_subdev_get_fmt,
+    .set_fmt = isp4sd_set_pad_format,
+    .enable_streams = isp4sd_enable_streams,
+    .disable_streams = isp4sd_disable_streams,
+};
+
+static const struct v4l2_subdev_ops isp4sd_subdev_ops = {
+    .core = &isp4sd_core_ops,
+    .video = &isp4sd_video_ops,
+    .pad = &isp4sd_pad_ops,
+};
+
+static int isp4sd_sdev_link_validate(struct media_link *link)
+{
+    return 0;
+}
+
+static const struct media_entity_operations isp4sd_sdev_ent_ops = {
+    .link_validate = isp4sd_sdev_link_validate,
+};
+
+int isp4sd_init(struct isp4_subdev *isp_subdev,
+        struct v4l2_device *v4l2_dev,
+        void *amdgpu_dev)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+    struct isp4sd_sensor_info *sensor_info;
+    struct device *dev = v4l2_dev->dev;
+    int ret;
+
+    isp_subdev->dev = dev;
+    isp_subdev->amdgpu_dev = amdgpu_dev;
+    v4l2_subdev_init(&isp_subdev->sdev, &isp4sd_subdev_ops);
+    isp_subdev->sdev.owner = THIS_MODULE;
+    isp_subdev->sdev.dev = dev;
+    snprintf(isp_subdev->sdev.name, sizeof(isp_subdev->sdev.name), 

"%s",
+         dev_name(dev));
+
+    isp_subdev->sdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
+    isp_subdev->sdev.entity.name = isp4sd_entity_name;
+    isp_subdev->sdev.entity.function = MEDIA_ENT_F_PROC_VIDEO_ISP; +    isp_subdev->sdev.entity.ops = &isp4sd_sdev_ent_ops;
+    isp_subdev->sdev_pad[0].flags = MEDIA_PAD_FL_SINK;
+    isp_subdev->sdev_pad[1].flags = MEDIA_PAD_FL_SOURCE;
+    ret = media_entity_pads_init(&isp_subdev->sdev.entity, 2, +                     isp_subdev->sdev_pad);
+    if (ret) {
+        dev_err(dev, "fail to init isp4 subdev entity pad %d\n", ret); +        return ret;
+    }
+    ret = v4l2_subdev_init_finalize(&isp_subdev->sdev);
+    if (ret < 0) {
+        dev_err(dev, "fail to init finalize isp4 subdev %d\n", +            ret);
+        return ret;
+    }
+    ret = v4l2_device_register_subdev(v4l2_dev, &isp_subdev->sdev); +    if (ret) {
+        dev_err(dev, "fail to register isp4 subdev to V4L2 device %d\n",
+            ret);
+        goto err_media_clean_up;
+    }

drivers/media/platform/amd/isp4/isp4_subdev.c new file +41 -0
+
+    sensor_info = &isp_subdev->sensor_info;
+
+    isp4if_init(ispif, dev, amdgpu_dev, isp_subdev->mmio);
+
+    mutex_init(&isp_subdev->ops_mutex);
+    sensor_info->start_stream_cmd_sent = false;
+    sensor_info->status = ISP4SD_START_STATUS_NOT_START;
+
+    /* create ISP enable gpio control */
+    isp_subdev->enable_gpio = devm_gpiod_get(isp_subdev->dev,
+                         "enable_isp",
+                         GPIOD_OUT_LOW);
+    if (IS_ERR(isp_subdev->enable_gpio)) {
+        dev_err(dev, "fail to get gpiod %d\n", ret);
+        media_entity_cleanup(&isp_subdev->sdev.entity);
+        return PTR_ERR(isp_subdev->enable_gpio);
+    }
+
+    isp_subdev->host2fw_seq_num = 1;
+    ispif->status = ISP4IF_STATUS_PWR_OFF;
+
+    if (ret)
+        goto err_media_clean_up;
+    return ret;
+
+err_media_clean_up:
+    media_entity_cleanup(&isp_subdev->sdev.entity);
+    return ret;
+}
+
+void isp4sd_deinit(struct isp4_subdev *isp_subdev)
+{
+    struct isp4_interface *ispif = &isp_subdev->ispif;
+
+    media_entity_cleanup(&isp_subdev->sdev.entity);
+    isp4if_deinit(ispif);
+    isp4sd_module_enable(isp_subdev, false);
+
+    ispif->status = ISP4IF_STATUS_PWR_OFF;
+}
drivers/media/platform/amd/isp4/isp4_subdev.h new file +31 -0
new file mode 100644
index 000000000000..bcbb93dce18f
@@ -0,0 +1,145 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_CONTEXT_H_
+#define _ISP4_CONTEXT_H_
+
+#include <linux/delay.h>
+#include <linux/firmware.h>
+#include <linux/platform_device.h>
+#include <linux/uaccess.h>
+#include <linux/types.h>
+#include <linux/debugfs.h>
+#include <media/v4l2-device.h>
+#include <media/videobuf2-vmalloc.h>
+
+#include "isp4_fw_cmd_resp.h"
+#include "isp4_hw_reg.h"
+#include "isp4_interface.h"
+
+/*
+ * one is for none sesnor specefic response which is not used now
+ * another is for sensor specific response
+ */
+#define ISP4SD_MAX_FW_RESP_STREAM_NUM 2
+
+/*
+ * cmd used to register frame done callback, parameter is
+ * struct isp4sd_register_framedone_cb_param *
+ * when a image buffer is filled by ISP, ISP will call the 

registered callback.
+ * callback func prototype is isp4sd_framedone_cb, cb_ctx can be anything
+ * provided by caller which will be provided back as the first parameter of the
+ * callback function.
+ * both cb_func and cb_ctx are provide by caller, set cb_func to NULL to
+ * unregister the callback
+ */

drivers/media/platform/amd/isp4/isp4_subdev.h new file +28 -0
+
+/* used to indicate the ISP status*/
+enum isp4sd_status {
+    ISP4SD_STATUS_PWR_OFF,
+    ISP4SD_STATUS_PWR_ON,
+    ISP4SD_STATUS_FW_RUNNING,
+    ISP4SD_STATUS_MAX
+};
+
+/*used to indicate the status of sensor, output stream */
+enum isp4sd_start_status {
+    ISP4SD_START_STATUS_NOT_START,
+    ISP4SD_START_STATUS_STARTED,
+    ISP4SD_START_STATUS_START_FAIL,
+};
+
+struct isp4sd_img_buf_node {
+    struct list_head node;
+    struct isp4if_img_buf_info buf_info;
+};
+
+/* this is isp output after processing bayer raw input from sensor */
+struct isp4sd_output_info {
+    enum isp4sd_start_status start_status;
+    u32 image_size;
+};
+
+/* This struct represents the sensor info which is input or source 

of ISP,
+ * meta_info_buf is the buffer store the fw to driver metainfo response
+ * status is the sensor status
+ * output_info is the isp output info after ISP processing the sensor input,
+ * start_stream_cmd_sent mean if CMD_ID_START_STREAM has sent to fw.
+ * buf_sent_cnt is buffer count app has sent to receive the images
+ */

drivers/media/platform/amd/isp4/isp4_subdev.h new file +74 -0
+struct isp4sd_sensor_info {
+    struct isp4if_gpu_mem_info *
+        meta_info_buf[ISP4IF_MAX_STREAM_META_BUF_COUNT];
+    struct isp4sd_output_info output_info;
+    enum isp4sd_start_status status;
+    bool start_stream_cmd_sent;
+    u32 buf_sent_cnt;
+};
+
+/*
+ * Thread created by driver to receive fw response
+ * thread will be wakeup by fw to driver response interrupt
+ */
+struct isp4sd_thread_handler {
+    struct task_struct *thread;
+    struct mutex mutex; /* mutex */
+    wait_queue_head_t waitq;
+    int wq_cond;
+};
+
+struct isp4_subdev_thread_param {
+    u32 idx;
+    struct isp4_subdev *isp_subdev;
+};
+
+struct isp4_subdev {
+    struct v4l2_subdev sdev;
+    struct isp4_interface ispif;
+
+    /*
+     * sdev_pad[0] is sink pad connected to sensor
+     * sdev_pad[0] is sink pad connected to sensor
+     * sdev_pad[1] is sourc pad connected v4l2 video device
+     */
+    struct media_pad sdev_pad[2];
+
+    enum isp4sd_status isp_status;
+    struct mutex ops_mutex; /* ops_mutex */
+
+    /* Used to store fw cmds sent to FW whose response driver needs
+     * to wait for
+     */
+    struct isp4sd_thread_handler
+        fw_resp_thread[ISP4SD_MAX_FW_RESP_STREAM_NUM];
+
+    u32 host2fw_seq_num;
+
+    struct isp4sd_sensor_info sensor_info;
+
+    /* gpio descriptor */
+    struct gpio_desc *enable_gpio;
+    struct device *dev;
+    void *amdgpu_dev;
+    void __iomem *mmio;
+    struct isp4_subdev_thread_param
+        isp_resp_para[ISP4SD_MAX_FW_RESP_STREAM_NUM];
+#ifdef CONFIG_DEBUG_FS
+    struct dentry *debugfs_dir;
+    bool enable_fw_log;
+    char *fw_log_output;
+#endif
+    u32 phy_num_data_lanes;
+    u32 phy_id;
+    u64 phy_link_freq;
+
+    void __iomem *isp_phy_mmio;
+};
+
+int isp4sd_init(struct isp4_subdev *isp_subdev,
+        struct v4l2_device *v4l2_dev,
+        void *amdgpu_dev);
+void isp4sd_deinit(struct isp4_subdev *isp_subdev);
+
+#endif
author maintainer
#41 lore raw

Re: [PATCH v2 5/8] media: platform: amd: isp4 subdev and firmware loading handling added

Quoted 76 lines · Mario Limonciello
Quoted 75 lines · Bin

Thanks Mario, add some comments

Quoted 72 lines · Mario Limonciello
Quoted 62 lines · Bin Du

Isp4 sub-device is implementing v4l2 sub-device interface. It has one capture video node, and supports only preview stream. It manages firmware states, stream configuration and mipi phy configuration. This change also adds interrupt handling and notification for isp firmware to isp-subdevice.

---
  drivers/media/platform/amd/isp4/Makefile      |    3 +- drivers/media/platform/amd/isp4/isp4.c        |  267 +++- drivers/media/platform/amd/isp4/isp4.h        |    4 +- drivers/media/platform/amd/isp4/isp4_subdev.c | 1192 +++++++++++++
++++
  drivers/media/platform/amd/isp4/isp4_subdev.h |  145 ++

  5 files changed, 1601 insertions(+), 10 deletions(-)

  create mode 100644 drivers/media/platform/amd/isp4/isp4_subdev.c create mode 100644 drivers/media/platform/amd/isp4/isp4_subdev.h

drivers/media/platform/amd/isp4/Makefile +0 -0
index c0166f954516..52defc06189e 100644
@@ -3,9 +3,10 @@
   # Copyright (C) 2025 Advanced Micro Devices, Inc.

  obj-$(CONFIG_AMD_ISP4) += amd_capture.o

drivers/media/platform/amd/isp4/Makefile +1 -1
-amd_capture-objs := isp4.o    \
+amd_capture-objs := isp4_subdev.o \

              isp4_phy.o \
              isp4_interface.o \
+            isp4.o    \
              isp4_hw.o    \
  ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4

drivers/media/platform/amd/isp4/isp4.c +9 -2
index d0be90c5ec3b..c1aca2bd35e3 100644
@@ -5,13 +5,20 @@
   #include <linux/pm_runtime.h>
   #include <linux/vmalloc.h>
+#include <media/v4l2-fwnode.h>
   #include <media/v4l2-ioctl.h>
-#include "isp4.h"
+#include "amdgpu_object.h"
-#define VIDEO_BUF_NUM 5
+#include "isp4.h"
+#include "isp4_hw.h"
   #define ISP4_DRV_NAME "amd_isp_capture"
+#define ISP4_FW_RESP_RB_IRQ_STATUS_MASK \
+    (ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT9_INT_MASK  | \
+     ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT10_INT_MASK | \
+     ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT11_INT_MASK | \
+     ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT12_INT_MASK)
   /* interrupt num */
   static const u32 isp4_ringbuf_interrupt_num[] = {
@@ -24,23 +31,233 @@ static const u32 isp4_ringbuf_interrupt_num[] = {
   #define to_isp4_device(dev) \
       ((struct isp4_device *)container_of(dev, struct isp4_device, v4l2_dev))
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[snip]
+static int isp4_register_subdev_and_create_links(struct isp4_device

*isp_dev,
+                         struct v4l2_subdev *sdev)

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 59

Quoted 7 lines
drivers/media/platform/amd/isp4/isp4.c +7 -0
+{
+    struct device *dev = &isp_dev->pdev->dev;
+    int ret;
+
+    ret = isp4_create_links(isp_dev, sdev);
+    if (ret)
+        dev_err(dev, "fail create isp link:%d\n", ret);

You're discarding ret here.  Shouldn't you return ret in the failure path?

Quoted 4 lines
Quoted 1 line

Even if it fails, some Apps which doesn't depend on the media link can still work, so just print error message and ignore it so at least these Apps can still work, do you think it acceptible?>

I think it should be a warn or an info if it's an acceptable situation along with a comment to help us years later understand why the error handling is that way.

Sure, will make it warn and add comment>>>> +

Quoted 11 lines
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

+    ret = v4l2_device_register_subdev_nodes(&isp_dev->v4l2_dev); +    if (ret != 0) {
+        dev_warn(dev, "register subdev as nodes fail:%d\n", ret); +        ret = 0;

This isn't fatal?

This call is just expose subdev node to user space, even if it fails, some Apps can still work, so just print error message and ignore it so at least these Apps can still work, do you think it acceptible?>> +    }

I guess same comment as above.

Same as above>>>> +

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

+    return ret;
+}

[snip]>>>>

#42 lore raw

Re: [PATCH v2 5/8] media: platform: amd: isp4 subdev and firmware loading handling added

In reply to Bin Du
Quoted 9 lines · Bin Du
drivers/media/platform/amd/isp4/isp4.c +9 -0
+static int isp4_parse_fwnode_init_async_nf(struct isp4_device *isp_dev)
+{
+	struct isp4_subdev *isp_sdev = &isp_dev->isp_sdev;
+	struct device *dev = &isp_dev->pdev->dev;
+	struct v4l2_fwnode_endpoint bus_cfg = {
+		.bus_type = V4L2_MBUS_CSI2_DPHY
+	};
+	struct fwnode_handle *remote_ep = NULL;
+	struct fwnode_handle *local_ep = NULL;

[snip]

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 249

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c +5 -0
+err_fwnode:
+	if (remote_ep)
+		fwnode_handle_put(remote_ep);
+	if (local_ep)
+		fwnode_handle_put(remote_ep);

Copy/paste error: the second fwnode_handle_put() should put local_ep.

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 49

Quoted 3 lines
drivers/media/platform/amd/isp4/isp4.c +3 -0
+
+	return ret;
+}

Sultan

author maintainer
#43 lore raw

Re: [PATCH v2 5/8] media: platform: amd: isp4 subdev and firmware loading handling added

Thanks Sultan, will fix it in the next patch

Regards,
Bin

Quoted 26 lines · Sultan Alsawaf
Quoted 9 lines · Bin Du
drivers/media/platform/amd/isp4/isp4.c +9 -0
+static int isp4_parse_fwnode_init_async_nf(struct isp4_device *isp_dev)
+{
+	struct isp4_subdev *isp_sdev = &isp_dev->isp_sdev;
+	struct device *dev = &isp_dev->pdev->dev;
+	struct v4l2_fwnode_endpoint bus_cfg = {
+		.bus_type = V4L2_MBUS_CSI2_DPHY
+	};
+	struct fwnode_handle *remote_ep = NULL;
+	struct fwnode_handle *local_ep = NULL;

[snip]

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 249

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c +5 -0
+err_fwnode:
+	if (remote_ep)
+		fwnode_handle_put(remote_ep);
+	if (local_ep)
+		fwnode_handle_put(remote_ep);

Copy/paste error: the second fwnode_handle_put() should put local_ep.

drivers/media/platform/amd/isp4/isp4.c static const u32 isp4_ringbuf_interrupt_num[] = { · line 49

Quoted 3 lines
drivers/media/platform/amd/isp4/isp4.c +3 -0
+
+	return ret;
+}

Sultan

author maintainer
#44 lore raw

PATCH v2 6/8 media: platform: amd: isp4 video node and buffers handling added

In reply to Bin Du

Isp video implements v4l2 video interface and supports NV12 and YUYV. It manages buffers, pipeline power and state.

---
 drivers/media/platform/amd/isp4/Makefile      |    1 +
 drivers/media/platform/amd/isp4/isp4.c        |   13 +-
 drivers/media/platform/amd/isp4/isp4_subdev.c |  119 +-
 drivers/media/platform/amd/isp4/isp4_subdev.h |    2 +
 drivers/media/platform/amd/isp4/isp4_video.c  | 1443 +++++++++++++++++
 drivers/media/platform/amd/isp4/isp4_video.h  |   93 ++
 6 files changed, 1661 insertions(+), 10 deletions(-)
 create mode 100644 drivers/media/platform/amd/isp4/isp4_video.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4_video.h
drivers/media/platform/amd/isp4/Makefile +1 -0
index 52defc06189e..99285fe45b52 100644
@@ -7,6 +7,7 @@ amd_capture-objs := isp4_subdev.o \
 			isp4_phy.o \
 			isp4_interface.o \
 			isp4.o	\
+			isp4_video.o \
 			isp4_hw.o	\
 
 ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
drivers/media/platform/amd/isp4/isp4.c +12 -1
index c1aca2bd35e3..3d971fe2813d 100644
@@ -337,6 +337,16 @@ static int isp4_capture_probe(struct platform_device *pdev)
 		goto err_isp4_deinit;
 	}
 
+	ret = media_create_pad_link(&isp_dev->isp_sdev.sdev.entity,
+				    1, &isp_dev->isp_sdev.isp_vdev.vdev.entity,
+				    0,
+				    MEDIA_LNK_FL_ENABLED |
+				    MEDIA_LNK_FL_IMMUTABLE);
+	if (ret) {
+		dev_err(dev, "fail to create pad link %d\n", ret);
+		goto err_unreg_video_dev_notifier;
+	}
+
 	platform_set_drvdata(pdev, isp_dev);
 
 	pm_runtime_set_suspended(dev);
@@ -344,9 +354,10 @@ static int isp4_capture_probe(struct platform_device *pdev)
 
 	return 0;
 
-err_isp4_deinit:
+err_unreg_video_dev_notifier:
 	v4l2_async_nf_unregister(&isp_dev->notifier);
 	v4l2_async_nf_cleanup(&isp_dev->notifier);
+err_isp4_deinit:
 	isp4sd_deinit(&isp_dev->isp_sdev);
 err_unreg_v4l2:
 	v4l2_device_unregister(&isp_dev->v4l2_dev);
drivers/media/platform/amd/isp4/isp4_subdev.c +110 -9

189 lines not on this page · raw message

drivers/media/platform/amd/isp4/isp4_subdev.h +2 -0
index bcbb93dce18f..7959a119527a 100644
@@ -18,6 +18,7 @@
 #include "isp4_fw_cmd_resp.h"
 #include "isp4_hw_reg.h"
 #include "isp4_interface.h"
+#include "isp4_video.h"
 
 /*
  * one is for none sesnor specefic response which is not used now
@@ -97,6 +98,7 @@ struct isp4_subdev_thread_param {
 struct isp4_subdev {
 	struct v4l2_subdev sdev;
 	struct isp4_interface ispif;
+	struct isp4vid_dev isp_vdev;
 
 	/*
 	 * sdev_pad[0] is sink pad connected to sensor
drivers/media/platform/amd/isp4/isp4_video.c new file +1443 -0

1,443 lines not on this page · raw message

drivers/media/platform/amd/isp4/isp4_video.h new file +93 -0

93 lines not on this page · raw message

Signature · 1 line
2.34.1
#45 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

drivers/media/platform/amd/isp4/isp4_video.c line 214

Quoted 10 lines · Bin Du
drivers/media/platform/amd/isp4/isp4_video.c new file +10 -0
+static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
+{
+	struct isp4vid_vb2_buf *buf = mem_priv;
+
+	if (!buf) {
+		pr_err("fail invalid buf handle\n");
+		return;
+	}
+
+	struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);

Variable declaration mixed with code, move the variable declaration to the top.

drivers/media/platform/amd/isp4/isp4_video.c line 223

Quoted 9 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +9 -0
+
+	dev_dbg(buf->dev, "detach dmabuf of isp user bo 0x%llx size %ld",
+		buf->gpu_addr, buf->size);
+
+	if (buf->vaddr)
+		dma_buf_vunmap_unlocked(buf->dbuf, &map);
+
+	// put dmabuf for exported ones
+	dma_buf_put(buf->dbuf);

The dmabuf shouldn't be put from the detach_dmabuf memop. Remove this.

Quoted 3 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +3 -0
+
+	kfree(buf);
+}

[snip]

drivers/media/platform/amd/isp4/isp4_video.c line 423

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +5 -0
+static void isp4vid_vb2_dmabuf_ops_release(struct dma_buf *dbuf)
+{
+	struct isp4vid_vb2_buf *buf = dbuf->priv;
+
+	/* drop reference obtained in vb2_isp4vid_get_dmabuf */

s/vb2_isp4vid_get_dmabuf/isp4vid_vb2_get_dmabuf/

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +5 -0
+	if (buf->is_expbuf)
+		isp4vid_vb2_put(dbuf->priv);
+	else
+		dev_dbg(buf->dev, "ignore buf release for implicit case");
+}

[snip]

drivers/media/platform/amd/isp4/isp4_video.c line 484

Quoted 11 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +11 -0
+static struct dma_buf *isp4vid_vb2_get_dmabuf(struct vb2_buffer *vb,
+					      void *buf_priv,
+					      unsigned long flags)
+{
+	struct isp4vid_vb2_buf *buf = buf_priv;
+	struct dma_buf *dbuf;
+
+	if (buf->dbuf) {
+		dev_dbg(buf->dev,
+			"dbuf already created, reuse implicit dbuf\n");
+		dbuf = buf->dbuf;

The dmabuf is reused here without taking a reference to it. When the get_dmabuf memop is called by vb2_core_expbuf(), it assumes that a reference to the dmabuf is acquired. So you need to add get_dma_buf(dbuf) here.

Quoted 12 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +12 -0
+	} else {
+		dbuf = isp4vid_get_dmabuf(vb, buf_priv, flags);
+		dev_dbg(buf->dev, "created new dbuf\n");
+	}
+	buf->is_expbuf = true;
+	refcount_inc(&buf->refcount);
+
+	dev_dbg(buf->dev, "buf exported, refcount %d\n",
+		buf->refcount.refs.counter);
+
+	return dbuf;
+}

[snip]

Quoted 13 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +13 -0
+static void *isp4vid_vb2_get_userptr(struct vb2_buffer *vb, struct device *dev,
+				     unsigned long vaddr, unsigned long size)
+{
+	struct isp4vid_vb2_buf *buf;
+	struct frame_vector *vec;
+	int n_pages, offset, i;
+	int ret = -ENOMEM;
+
+	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
+	if (!buf)
+		return ERR_PTR(-ENOMEM);
+
+	buf->dev = dev;

drivers/media/platform/amd/isp4/isp4_video.c line 552

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +10 -0
+	buf->dma_dir = vb->vb2_queue->dma_dir;
+	offset = vaddr & ~PAGE_MASK;
+	buf->size = size;
+	vec = vb2_create_framevec(vaddr, size,
+				  buf->dma_dir == DMA_FROM_DEVICE ||
+				  buf->dma_dir == DMA_BIDIRECTIONAL);
+	if (IS_ERR(vec)) {
+		kfree(buf);
+		return vec;
+	}

You can combine the error handling here with the error path at the bottom of the function instead of duplicating the kfree(buf).

Quoted 34 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +34 -0
+	buf->vec = vec;
+	n_pages = frame_vector_count(vec);
+	if (frame_vector_to_pages(vec) < 0) {
+		unsigned long *nums = frame_vector_pfns(vec);
+
+		/*
+		 * We cannot get page pointers for these pfns. Check memory is
+		 * physically contiguous and use direct mapping.
+		 */
+		for (i = 1; i < n_pages; i++)
+			if (nums[i - 1] + 1 != nums[i])
+				goto err_destroy_free;
+		buf->vaddr = (__force void *)
+			     ioremap(__pfn_to_phys(nums[0]), size + offset);
+	} else {
+		buf->vaddr = vm_map_ram(frame_vector_pages(vec), n_pages, -1);
+	}
+
+	if (!buf->vaddr)
+		goto err_destroy_free;
+
+	buf->vaddr = ((char *)buf->vaddr) + offset;
+	return buf;
+
+err_destroy_free:
+	vb2_destroy_framevec(vec);
+	kfree(buf);
+	return ERR_PTR(ret);
+}
+
+static void isp4vid_vb2_put(void *buf_priv)
+{
+	struct isp4vid_vb2_buf *buf = (struct isp4vid_vb2_buf *)buf_priv;
+	struct amdgpu_bo *bo = (struct amdgpu_bo *)buf->bo;

drivers/media/platform/amd/isp4/isp4_video.c line 596

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +10 -0
+
+	dev_dbg(buf->dev,
+		"release isp user bo 0x%llx size %ld refcount %d is_expbuf %d",
+		buf->gpu_addr, buf->size,
+		buf->refcount.refs.counter, buf->is_expbuf);
+
+	if (refcount_dec_and_test(&buf->refcount)) {
+		amdgpu_bo_free_isp_user(bo);
+
+		// put implicit dmabuf here, detach_dmabuf will not be called

Comment style (use /**/ instead of //).

drivers/media/platform/amd/isp4/isp4_video.c line 602

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +6 -0
+		if (!buf->is_expbuf)
+			dma_buf_put(buf->dbuf);
+
+		vfree(buf->vaddr);
+		kfree(buf);
+		buf = NULL;

buf = NULL; here is superfluous; you can remove it.

drivers/media/platform/amd/isp4/isp4_video.c line 605

Quoted 3 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +3 -0
+	} else {
+		dev_warn(buf->dev, "ignore buffer free, refcount %u > 0",
+			 refcount_read(&buf->refcount));

This refcount_read() is a possible use-after-free because buf is accessed after isp4vid_vb2_put() puts its reference to buf. So something else could put the last reference to buf and free it after this refcount dec but before the refcount_read(). Maybe just remove this dev_warn() entirely?

Quoted 18 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +18 -0
+	}
+}
+
+static void *isp4vid_vb2_alloc(struct vb2_buffer *vb, struct device *dev,
+			       unsigned long size)
+{
+	struct isp4vid_dev *isp_vdev = vb2_get_drv_priv(vb->vb2_queue);
+	struct isp4vid_vb2_buf *buf = NULL;
+	struct amdgpu_bo *bo;
+	u64 gpu_addr;
+	u32 ret;
+
+	buf = kzalloc(sizeof(*buf), GFP_KERNEL | vb->vb2_queue->gfp_flags);
+	if (!buf)
+		return ERR_PTR(-ENOMEM);
+
+	buf->dev = dev;
+	buf->size = size;

drivers/media/platform/amd/isp4/isp4_video.c line 633

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +10 -0
+	buf->vaddr = vmalloc_user(buf->size);
+	if (!buf->vaddr) {
+		kfree(buf);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	buf->dma_dir = vb->vb2_queue->dma_dir;
+	buf->handler.refcount = &buf->refcount;
+	buf->handler.put = isp4vid_vb2_put;
+	buf->handler.arg = buf;

What is buf->handler for? I don't see it used anywhere in the entire patchset; I can delete handler from struct isp4vid_vb2_buf along with these lines and it compiles.

drivers/media/platform/amd/isp4/isp4_video.c line 635

Quoted 2 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +2 -0
+
+	// get implicit dmabuf

Comment style.

drivers/media/platform/amd/isp4/isp4_video.c line 640

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +5 -0
+	buf->dbuf = isp4vid_get_dmabuf(vb, buf, 0);
+	if (!buf->dbuf) {
+		dev_err(dev, "fail to get dmabuf\n");
+		return ERR_PTR(-EINVAL);
+	}

Doesn't free buf or buf->vaddr on error here. Also, comment style.

drivers/media/platform/amd/isp4/isp4_video.c line 642

Quoted 2 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +2 -0
+
+	// create isp user BO and obtain gpu_addr

Comment style.

drivers/media/platform/amd/isp4/isp4_video.c line 648

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +6 -0
+	ret = amdgpu_bo_create_isp_user(isp_vdev->amdgpu_dev, buf->dbuf,
+					AMDGPU_GEM_DOMAIN_GTT, &bo, &gpu_addr);
+	if (ret) {
+		dev_err(dev, "fail to create BO\n");
+		return ERR_PTR(-EINVAL);
+	}

Doesn't free buf or buf->vaddr or put buf->dbuf on error here.

drivers/media/platform/amd/isp4/isp4_video.c line 653

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +5 -0
+
+	buf->bo = (void *)bo;
+	buf->gpu_addr = gpu_addr;
+
+	refcount_set(&buf->refcount, 1);

This discards the refcount inc triggered from amdgpu_bo_create_isp_user() when it calls get_dma_buf(), leading to a use-after-free. Move this refcount_set() up, preferably right after vmalloc_user() or right after buf is allocated so there's no risk of this issue occurring again in the future.

drivers/media/platform/amd/isp4/isp4_video.c line 659

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +6 -0
+
+	dev_dbg(dev, "allocated isp user bo 0x%llx size %ld refcount %d",
+		buf->gpu_addr, buf->size, buf->refcount.refs.counter);
+
+	return buf;
+}

[snip]

Sultan

#46 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Quoted 11 lines · Sultan Alsawaf

drivers/media/platform/amd/isp4/isp4_video.c line 653

Quoted 5 lines · Bin Du
drivers/media/platform/amd/isp4/isp4_video.c new file +5 -0
+
+	buf->bo = (void *)bo;
+	buf->gpu_addr = gpu_addr;
+
+	refcount_set(&buf->refcount, 1);

This discards the refcount inc triggered from amdgpu_bo_create_isp_user() when it calls get_dma_buf(), leading to a use-after-free. Move this refcount_set() up, preferably right after vmalloc_user() or right after buf is allocated so there's no risk of this issue occurring again in the future.

Following up to correct myself: please disregard this one comment from my previous email since it's incorrect. Sorry for any confusion.

Sultan

author maintainer
#47 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Thanks Sultan, never mind.

Regards,
Bin

Quoted 17 lines · Sultan Alsawaf
Quoted 11 lines · Sultan Alsawaf

drivers/media/platform/amd/isp4/isp4_video.c line 653

Quoted 5 lines · Bin Du
drivers/media/platform/amd/isp4/isp4_video.c new file +5 -0
+
+	buf->bo = (void *)bo;
+	buf->gpu_addr = gpu_addr;
+
+	refcount_set(&buf->refcount, 1);

This discards the refcount inc triggered from amdgpu_bo_create_isp_user() when it calls get_dma_buf(), leading to a use-after-free. Move this refcount_set() up, preferably right after vmalloc_user() or right after buf is allocated so there's no risk of this issue occurring again in the future.

Following up to correct myself: please disregard this one comment from my previous email since it's incorrect. Sorry for any confusion.

Sultan

author maintainer
#48 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

In reply to Sultan Alsawaf

Many thanks Sultan for your so careful review.

Regards,
Bin

Quoted 13 lines · Sultan Alsawaf

drivers/media/platform/amd/isp4/isp4_video.c line 214

Quoted 10 lines · Bin Du
drivers/media/platform/amd/isp4/isp4_video.c new file +10 -0
+static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
+{
+	struct isp4vid_vb2_buf *buf = mem_priv;
+
+	if (!buf) {
+		pr_err("fail invalid buf handle\n");
+		return;
+	}
+
+	struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);

Variable declaration mixed with code, move the variable declaration to the top.

Will fix in the next patch>

Quoted 12 lines

drivers/media/platform/amd/isp4/isp4_video.c line 223

Quoted 9 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +9 -0
+
+	dev_dbg(buf->dev, "detach dmabuf of isp user bo 0x%llx size %ld",
+		buf->gpu_addr, buf->size);
+
+	if (buf->vaddr)
+		dma_buf_vunmap_unlocked(buf->dbuf, &map);
+
+	// put dmabuf for exported ones
+	dma_buf_put(buf->dbuf);

The dmabuf shouldn't be put from the detach_dmabuf memop. Remove this.

Will fix in the next patch>> +

Quoted 5 lines
Quoted 2 lines
(quoted diff) +2 -0
+	kfree(buf);
+}

[snip]

Quoted 8 lines

drivers/media/platform/amd/isp4/isp4_video.c line 423

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +5 -0
+static void isp4vid_vb2_dmabuf_ops_release(struct dma_buf *dbuf)
+{
+	struct isp4vid_vb2_buf *buf = dbuf->priv;
+
+	/* drop reference obtained in vb2_isp4vid_get_dmabuf */

s/vb2_isp4vid_get_dmabuf/isp4vid_vb2_get_dmabuf/

Will fix in the next patch>> + if (buf->is_expbuf)

Quoted 7 lines
Quoted 4 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +4 -0
+		isp4vid_vb2_put(dbuf->priv);
+	else
+		dev_dbg(buf->dev, "ignore buf release for implicit case");
+}

[snip]

Quoted 15 lines

drivers/media/platform/amd/isp4/isp4_video.c line 484

Quoted 11 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +11 -0
+static struct dma_buf *isp4vid_vb2_get_dmabuf(struct vb2_buffer *vb,
+					      void *buf_priv,
+					      unsigned long flags)
+{
+	struct isp4vid_vb2_buf *buf = buf_priv;
+	struct dma_buf *dbuf;
+
+	if (buf->dbuf) {
+		dev_dbg(buf->dev,
+			"dbuf already created, reuse implicit dbuf\n");
+		dbuf = buf->dbuf;

The dmabuf is reused here without taking a reference to it. When the get_dmabuf memop is called by vb2_core_expbuf(), it assumes that a reference to the dmabuf is acquired. So you need to add get_dma_buf(dbuf) here.

After test, we found we can't add get_dma_buf(dbuf) here because it will make cheese APP fail to open camera with following error:
amdgpu: [drm] *ERROR* failed to alloc gart kernel buffer (-28)>

Quoted 33 lines
Quoted 12 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +12 -0
+	} else {
+		dbuf = isp4vid_get_dmabuf(vb, buf_priv, flags);
+		dev_dbg(buf->dev, "created new dbuf\n");
+	}
+	buf->is_expbuf = true;
+	refcount_inc(&buf->refcount);
+
+	dev_dbg(buf->dev, "buf exported, refcount %d\n",
+		buf->refcount.refs.counter);
+
+	return dbuf;
+}

[snip]

Quoted 18 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +18 -0
+static void *isp4vid_vb2_get_userptr(struct vb2_buffer *vb, struct device *dev,
+				     unsigned long vaddr, unsigned long size)
+{
+	struct isp4vid_vb2_buf *buf;
+	struct frame_vector *vec;
+	int n_pages, offset, i;
+	int ret = -ENOMEM;
+
+	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
+	if (!buf)
+		return ERR_PTR(-ENOMEM);
+
+	buf->dev = dev;
+	buf->dma_dir = vb->vb2_queue->dma_dir;
+	offset = vaddr & ~PAGE_MASK;
+	buf->size = size;
+	vec = vb2_create_framevec(vaddr, size,
+				  buf->dma_dir == DMA_FROM_DEVICE ||
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_video.c line 552

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +5 -0
+				  buf->dma_dir == DMA_BIDIRECTIONAL);
+	if (IS_ERR(vec)) {
+		kfree(buf);
+		return vec;
+	}

You can combine the error handling here with the error path at the bottom of the function instead of duplicating the kfree(buf).

Will fix in the next patch>> + buf->vec = vec;

Quoted 37 lines
Quoted 37 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +37 -0
+	n_pages = frame_vector_count(vec);
+	if (frame_vector_to_pages(vec) < 0) {
+		unsigned long *nums = frame_vector_pfns(vec);
+
+		/*
+		 * We cannot get page pointers for these pfns. Check memory is
+		 * physically contiguous and use direct mapping.
+		 */
+		for (i = 1; i < n_pages; i++)
+			if (nums[i - 1] + 1 != nums[i])
+				goto err_destroy_free;
+		buf->vaddr = (__force void *)
+			     ioremap(__pfn_to_phys(nums[0]), size + offset);
+	} else {
+		buf->vaddr = vm_map_ram(frame_vector_pages(vec), n_pages, -1);
+	}
+
+	if (!buf->vaddr)
+		goto err_destroy_free;
+
+	buf->vaddr = ((char *)buf->vaddr) + offset;
+	return buf;
+
+err_destroy_free:
+	vb2_destroy_framevec(vec);
+	kfree(buf);
+	return ERR_PTR(ret);
+}
+
+static void isp4vid_vb2_put(void *buf_priv)
+{
+	struct isp4vid_vb2_buf *buf = (struct isp4vid_vb2_buf *)buf_priv;
+	struct amdgpu_bo *bo = (struct amdgpu_bo *)buf->bo;
+
+	dev_dbg(buf->dev,
+		"release isp user bo 0x%llx size %ld refcount %d is_expbuf %d",
+		buf->gpu_addr, buf->size,
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_video.c line 596

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +6 -0
+		buf->refcount.refs.counter, buf->is_expbuf);
+
+	if (refcount_dec_and_test(&buf->refcount)) {
+		amdgpu_bo_free_isp_user(bo);
+
+		// put implicit dmabuf here, detach_dmabuf will not be called

Comment style (use /**/ instead of //).

Will fix in the next patch>> + if (!buf->is_expbuf)

Quoted 8 lines

drivers/media/platform/amd/isp4/isp4_video.c line 602

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +5 -0
+			dma_buf_put(buf->dbuf);
+
+		vfree(buf->vaddr);
+		kfree(buf);
+		buf = NULL;

buf = NULL; here is superfluous; you can remove it.

Will fix in the next patch>> + } else {

Quoted 8 lines

drivers/media/platform/amd/isp4/isp4_video.c line 605

Quoted 2 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +2 -0
+		dev_warn(buf->dev, "ignore buffer free, refcount %u > 0",
+			 refcount_read(&buf->refcount));

This refcount_read() is a possible use-after-free because buf is accessed after isp4vid_vb2_put() puts its reference to buf. So something else could put the last reference to buf and free it after this refcount dec but before the refcount_read(). Maybe just remove this dev_warn() entirely?

The warning is important to debug mem related issue, plan to keep it but without accessing buf or buf->refcount here. Do you think it acceptible?>> + }

Quoted 23 lines
Quoted 23 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +23 -0
+}
+
+static void *isp4vid_vb2_alloc(struct vb2_buffer *vb, struct device *dev,
+			       unsigned long size)
+{
+	struct isp4vid_dev *isp_vdev = vb2_get_drv_priv(vb->vb2_queue);
+	struct isp4vid_vb2_buf *buf = NULL;
+	struct amdgpu_bo *bo;
+	u64 gpu_addr;
+	u32 ret;
+
+	buf = kzalloc(sizeof(*buf), GFP_KERNEL | vb->vb2_queue->gfp_flags);
+	if (!buf)
+		return ERR_PTR(-ENOMEM);
+
+	buf->dev = dev;
+	buf->size = size;
+	buf->vaddr = vmalloc_user(buf->size);
+	if (!buf->vaddr) {
+		kfree(buf);
+		return ERR_PTR(-ENOMEM);
+	}
+
Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_video.c line 633

Quoted 4 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +4 -0
+	buf->dma_dir = vb->vb2_queue->dma_dir;
+	buf->handler.refcount = &buf->refcount;
+	buf->handler.put = isp4vid_vb2_put;
+	buf->handler.arg = buf;

What is buf->handler for? I don't see it used anywhere in the entire patchset; I can delete handler from struct isp4vid_vb2_buf along with these lines and it compiles.

Yes, will remove it.>> +

Quoted 4 lines

drivers/media/platform/amd/isp4/isp4_video.c line 635

Quoted 1 line
drivers/media/platform/amd/isp4/isp4_video.c new file +1 -0
+	// get implicit dmabuf

Comment style.

Quoted 8 lines

drivers/media/platform/amd/isp4/isp4_video.c line 640

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +5 -0
+	buf->dbuf = isp4vid_get_dmabuf(vb, buf, 0);
+	if (!buf->dbuf) {
+		dev_err(dev, "fail to get dmabuf\n");
+		return ERR_PTR(-EINVAL);
+	}

Doesn't free buf or buf->vaddr on error here. Also, comment style.

Will fix in the next patch>> +

Quoted 4 lines

drivers/media/platform/amd/isp4/isp4_video.c line 642

Quoted 1 line
drivers/media/platform/amd/isp4/isp4_video.c new file +1 -0
+	// create isp user BO and obtain gpu_addr

Comment style.

Will fix in the next patch>> + ret = amdgpu_bo_create_isp_user(isp_vdev->amdgpu_dev, buf->dbuf,

Quoted 8 lines

drivers/media/platform/amd/isp4/isp4_video.c line 648

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +5 -0
+					AMDGPU_GEM_DOMAIN_GTT, &bo, &gpu_addr);
+	if (ret) {
+		dev_err(dev, "fail to create BO\n");
+		return ERR_PTR(-EINVAL);
+	}

Doesn't free buf or buf->vaddr or put buf->dbuf on error here.

Will fix in the next patch>> +

Quoted 10 lines

drivers/media/platform/amd/isp4/isp4_video.c line 653

Quoted 4 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +4 -0
+	buf->bo = (void *)bo;
+	buf->gpu_addr = gpu_addr;
+
+	refcount_set(&buf->refcount, 1);

This discards the refcount inc triggered from amdgpu_bo_create_isp_user() when it calls get_dma_buf(), leading to a use-after-free. Move this refcount_set() up, preferably right after vmalloc_user() or right after buf is allocated so there's no risk of this issue occurring again in the future.

Ignore it as you did correction in another mail.>> +

Quoted 9 lines

drivers/media/platform/amd/isp4/isp4_video.c line 659

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +5 -0
+	dev_dbg(dev, "allocated isp user bo 0x%llx size %ld refcount %d",
+		buf->gpu_addr, buf->size, buf->refcount.refs.counter);
+
+	return buf;
+}

[snip]

Sultan

#49 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Quoted 15 lines · Bin
Quoted 7 lines
Quoted 7 lines
(quoted diff) +7 -0
+static struct dma_buf *isp4vid_vb2_get_dmabuf(struct vb2_buffer *vb,
+					      void *buf_priv,
+					      unsigned long flags)
+{
+	struct isp4vid_vb2_buf *buf = buf_priv;
+	struct dma_buf *dbuf;
+
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+	if (buf->dbuf) {
+		dev_dbg(buf->dev,
+			"dbuf already created, reuse implicit dbuf\n");
+		dbuf = buf->dbuf;

The dmabuf is reused here without taking a reference to it. When the get_dmabuf memop is called by vb2_core_expbuf(), it assumes that a reference to the dmabuf is acquired. So you need to add get_dma_buf(dbuf) here.

Quoted 3 lines

After test, we found we can't add get_dma_buf(dbuf) here because it will make cheese APP fail to open camera with following error:
amdgpu: [drm] *ERROR* failed to alloc gart kernel buffer (-28)

I see, it's because buf->is_expbuf is set to true even for the implicit dbuf, so the initial reference on the implicit dbuf is never put, causing a leak.

Also, the refcount increment in isp4vid_vb2_get_dmabuf() is done every time even when reusing the existing dbuf, but releasing the dbuf will only do a single refcount decrement. This also causes a leak.

And, isp4vid_get_dmabuf() may fail but isp4vid_vb2_get_dmabuf() doesn't check the return value, so there may be another leak when isp4vid_get_dmabuf() fails because of the refcount increment. The refcount increment and setting of buf->is_expbuf to true should only be done on success.

I have fixed all of these isp4vid_vb2_get_dmabuf() issues in the following diff, please try it:

drivers/media/platform/amd/isp4/isp4_video.c +10 -6
@@ -476,18 +476,22 @@ static struct dma_buf *isp4vid_vb2_get_dmabuf(struct vb2_buffer *vb,
 					      unsigned long flags)
 {
 	struct isp4vid_vb2_buf *buf = buf_priv;
-	struct dma_buf *dbuf;
+	struct dma_buf *dbuf = buf->dbuf;
 
-	if (buf->dbuf) {
+	if (dbuf) {
 		dev_dbg(buf->dev,
-			"dbuf already created, reuse implicit dbuf\n");
-		dbuf = buf->dbuf;
+			"dbuf already created, reuse %s dbuf\n",
+			buf->is_expbuf ? "exported" : "implicit");
+		get_dma_buf(dbuf);
 	} else {
 		dbuf = isp4vid_get_dmabuf(vb, buf_priv, flags);
+		if (!dbuf)
+			return NULL;
+
 		dev_dbg(buf->dev, "created new dbuf\n");
+		buf->is_expbuf = true;
+		refcount_inc(&buf->refcount);
 	}
-	buf->is_expbuf = true;
-	refcount_inc(&buf->refcount);
 
 	dev_dbg(buf->dev, "buf exported, refcount %d\n",
 		buf->refcount.refs.counter);

--

Quoted 10 lines
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		dev_warn(buf->dev, "ignore buffer free, refcount %u > 0",
+			 refcount_read(&buf->refcount));

This refcount_read() is a possible use-after-free because buf is accessed after isp4vid_vb2_put() puts its reference to buf. So something else could put the last reference to buf and free it after this refcount dec but before the refcount_read(). Maybe just remove this dev_warn() entirely?

The warning is important to debug mem related issue, plan to keep it but without accessing buf or buf->refcount here. Do you think it acceptible?

Yes, that sounds good. So something like this:
dev_warn(buf->dev, "ignore buffer free, refcount > 0");

Sultan

#50 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Quoted 9 lines · Sultan Alsawaf
Quoted 8 lines · Bin
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		dev_warn(buf->dev, "ignore buffer free, refcount %u > 0",
+			 refcount_read(&buf->refcount));

This refcount_read() is a possible use-after-free because buf is accessed after isp4vid_vb2_put() puts its reference to buf. So something else could put the last reference to buf and free it after this refcount dec but before the refcount_read(). Maybe just remove this dev_warn() entirely?

Quoted 5 lines
Quoted 2 lines

The warning is important to debug mem related issue, plan to keep it but without accessing buf or buf->refcount here. Do you think it acceptible?

Yes, that sounds good. So something like this:
dev_warn(buf->dev, "ignore buffer free, refcount > 0");

Sorry, to fix the dev_warn() we need to make a copy of buf->dev first:

drivers/media/platform/amd/isp4/isp4_video.c +3 -3
@@ -584,8 +584,9 @@ static void isp4vid_vb2_put(void *buf_priv)
 {
 	struct isp4vid_vb2_buf *buf = (struct isp4vid_vb2_buf *)buf_priv;
 	struct amdgpu_bo *bo = (struct amdgpu_bo *)buf->bo;
+	struct device *dev = buf->dev;
 
-	dev_dbg(buf->dev,
+	dev_dbg(dev,
 		"release isp user bo 0x%llx size %ld refcount %d is_expbuf %d",
 		buf->gpu_addr, buf->size,
 		buf->refcount.refs.counter, buf->is_expbuf);
@@ -601,8 +602,7 @@ static void isp4vid_vb2_put(void *buf_priv)
 		kfree(buf);
 		buf = NULL;
 	} else {
-		dev_warn(buf->dev, "ignore buffer free, refcount %u > 0",
-			 refcount_read(&buf->refcount));
+		dev_warn(dev, "ignore buffer free, refcount > 0\n");
 	}
 }
 

--

Sultan

author maintainer
#51 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Many thanks Sultan, will try your changes in this and other mails together and let you know the result

Quoted 44 lines · Sultan Alsawaf
Quoted 9 lines · Sultan Alsawaf
Quoted 8 lines · Bin
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		dev_warn(buf->dev, "ignore buffer free, refcount %u > 0",
+			 refcount_read(&buf->refcount));

This refcount_read() is a possible use-after-free because buf is accessed after isp4vid_vb2_put() puts its reference to buf. So something else could put the last reference to buf and free it after this refcount dec but before the refcount_read(). Maybe just remove this dev_warn() entirely?

Quoted 5 lines
Quoted 2 lines

The warning is important to debug mem related issue, plan to keep it but without accessing buf or buf->refcount here. Do you think it acceptible?

Yes, that sounds good. So something like this:
dev_warn(buf->dev, "ignore buffer free, refcount > 0");

Sorry, to fix the dev_warn() we need to make a copy of buf->dev first:

drivers/media/platform/amd/isp4/isp4_video.c +3 -4
@@ -584,8 +584,9 @@ static void isp4vid_vb2_put(void *buf_priv)
  {
  	struct isp4vid_vb2_buf *buf = (struct isp4vid_vb2_buf *)buf_priv;
  	struct amdgpu_bo *bo = (struct amdgpu_bo *)buf->bo;
+	struct device *dev = buf->dev;
 
-	dev_dbg(buf->dev,
+	dev_dbg(dev,
  		"release isp user bo 0x%llx size %ld refcount %d is_expbuf %d",
  		buf->gpu_addr, buf->size,
  		buf->refcount.refs.counter, buf->is_expbuf);
@@ -601,8 +602,7 @@ static void isp4vid_vb2_put(void *buf_priv)
  		kfree(buf);
  		buf = NULL;
  	} else {
-		dev_warn(buf->dev, "ignore buffer free, refcount %u > 0",
-			 refcount_read(&buf->refcount));
+		dev_warn(dev, "ignore buffer free, refcount > 0\n");
  	}
  }
 
--

Sultan

author maintainer
#52 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

In reply to Sultan Alsawaf

Many thanks Sultan, will try your modifications in this and other mails together and let you know the result

Quoted 83 lines · Sultan Alsawaf
Quoted 15 lines · Bin
Quoted 7 lines
Quoted 7 lines
(quoted diff) +7 -0
+static struct dma_buf *isp4vid_vb2_get_dmabuf(struct vb2_buffer *vb,
+					      void *buf_priv,
+					      unsigned long flags)
+{
+	struct isp4vid_vb2_buf *buf = buf_priv;
+	struct dma_buf *dbuf;
+
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+	if (buf->dbuf) {
+		dev_dbg(buf->dev,
+			"dbuf already created, reuse implicit dbuf\n");
+		dbuf = buf->dbuf;

The dmabuf is reused here without taking a reference to it. When the get_dmabuf memop is called by vb2_core_expbuf(), it assumes that a reference to the dmabuf is acquired. So you need to add get_dma_buf(dbuf) here.

Quoted 3 lines

After test, we found we can't add get_dma_buf(dbuf) here because it will make cheese APP fail to open camera with following error:
amdgpu: [drm] *ERROR* failed to alloc gart kernel buffer (-28)

I see, it's because buf->is_expbuf is set to true even for the implicit dbuf, so the initial reference on the implicit dbuf is never put, causing a leak.

Also, the refcount increment in isp4vid_vb2_get_dmabuf() is done every time even when reusing the existing dbuf, but releasing the dbuf will only do a single refcount decrement. This also causes a leak.

And, isp4vid_get_dmabuf() may fail but isp4vid_vb2_get_dmabuf() doesn't check the return value, so there may be another leak when isp4vid_get_dmabuf() fails because of the refcount increment. The refcount increment and setting of buf->is_expbuf to true should only be done on success.

I have fixed all of these isp4vid_vb2_get_dmabuf() issues in the following diff, please try it:

drivers/media/platform/amd/isp4/isp4_video.c +10 -7
@@ -476,18 +476,22 @@ static struct dma_buf *isp4vid_vb2_get_dmabuf(struct vb2_buffer *vb,
  					      unsigned long flags)
  {
  	struct isp4vid_vb2_buf *buf = buf_priv;
-	struct dma_buf *dbuf;
+	struct dma_buf *dbuf = buf->dbuf;
 
-	if (buf->dbuf) {
+	if (dbuf) {
  		dev_dbg(buf->dev,
-			"dbuf already created, reuse implicit dbuf\n");
-		dbuf = buf->dbuf;
+			"dbuf already created, reuse %s dbuf\n",
+			buf->is_expbuf ? "exported" : "implicit");
+		get_dma_buf(dbuf);
  	} else {
  		dbuf = isp4vid_get_dmabuf(vb, buf_priv, flags);
+		if (!dbuf)
+			return NULL;
+
  		dev_dbg(buf->dev, "created new dbuf\n");
+		buf->is_expbuf = true;
+		refcount_inc(&buf->refcount);
  	}
-	buf->is_expbuf = true;
-	refcount_inc(&buf->refcount);
 
  	dev_dbg(buf->dev, "buf exported, refcount %d\n",
  		buf->refcount.refs.counter);
--
Quoted 11 lines
Quoted 9 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+		dev_warn(buf->dev, "ignore buffer free, refcount %u > 0",
+			 refcount_read(&buf->refcount));

This refcount_read() is a possible use-after-free because buf is accessed after isp4vid_vb2_put() puts its reference to buf. So something else could put the last reference to buf and free it after this refcount dec but before the refcount_read(). Maybe just remove this dev_warn() entirely?

The warning is important to debug mem related issue, plan to keep it but without accessing buf or buf->refcount here. Do you think it acceptible?

Yes, that sounds good. So something like this:
dev_warn(buf->dev, "ignore buffer free, refcount > 0");

Sultan

#53 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

In reply to Bin Du

I found more refcounting issues in addition to the ones from my other emails while trying to make my webcam work:

Quoted 13 lines · Bin Du
drivers/media/platform/amd/isp4/isp4_video.c new file +13 -0
+static int isp4vid_vb2_mmap(void *buf_priv, struct vm_area_struct *vma)
+{
+	struct isp4vid_vb2_buf *buf = buf_priv;
+	int ret;
+
+	if (!buf) {
+		pr_err("fail no memory to map\n");
+		return -EINVAL;
+	}
+
+	ret = remap_vmalloc_range(vma, buf->vaddr, 0);
+	if (ret) {
+		dev_err(buf->dev, "fail remap vmalloc mem, %d\n", ret);

drivers/media/platform/amd/isp4/isp4_video.c line 183

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +10 -0
+		return ret;
+	}
+
+	/*
+	 * Make sure that vm_areas for 2 buffers won't be merged together
+	 */
+	vm_flags_set(vma, VM_DONTEXPAND);
+
+	dev_dbg(buf->dev, "mmap isp user bo 0x%llx size %ld refcount %d\n",
+		buf->gpu_addr, buf->size, buf->refcount.refs.counter);

Use refcount_read() instead of reading the refcount's atomic_t counter directly. This is done in 3 other places; change those to refcount_read() as well.

This didn't cause any functional problems, but it should still be fixed.

Quoted 3 lines
drivers/media/platform/amd/isp4/isp4_subdev.c +3 -0
+
+	return 0;
+}

[snip]

Quoted 12 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +12 -0
+static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
+{
+	struct isp4vid_vb2_buf *buf = mem_priv;
+
+	if (!buf) {
+		pr_err("fail invalid buf handle\n");
+		return;
+	}
+
+	struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);
+
+	dev_dbg(buf->dev, "detach dmabuf of isp user bo 0x%llx size %ld",

drivers/media/platform/amd/isp4/isp4_video.c line 226

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +10 -0
+		buf->gpu_addr, buf->size);
+
+	if (buf->vaddr)
+		dma_buf_vunmap_unlocked(buf->dbuf, &map);
+
+	// put dmabuf for exported ones
+	dma_buf_put(buf->dbuf);
+
+	kfree(buf);
+}

As mentioned in the other email, the dma_buf_put() here needs to be removed. But that's not all: the dma_buf_vunmap_unlocked() needs to be removed too because vb2 will always unmap the buffer before detaching it. As a result, having the dma_buf_vunmap_unlocked() call here results in a use-after-free when vb2 calls the unmap_dmabuf memop.

Change this function to the following:

	static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
	{
		struct isp4vid_vb2_buf *buf = mem_priv;

		kfree(buf);
	}
Quoted 14 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +14 -0
+static void isp4vid_qops_buffer_cleanup(struct vb2_buffer *vb)
+{
+	struct isp4vid_dev *isp_vdev = vb2_get_drv_priv(vb->vb2_queue);
+	struct isp4vid_vb2_buf *buf = vb->planes[0].mem_priv;
+
+	dev_dbg(isp_vdev->dev, "%s|index=%u vb->memory %u",
+		isp_vdev->vdev.name, vb->index, vb->memory);
+
+	if (!buf) {
+		dev_err(isp_vdev->dev, "Invalid buf handle");
+		return;
+	}
+
+	// release implicit dmabuf reference here for vb2 buffer

drivers/media/platform/amd/isp4/isp4_video.c line 1170

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +10 -0
+	// of type MMAP and is exported
+	if (vb->memory == VB2_MEMORY_MMAP && buf->is_expbuf) {
+		dma_buf_put(buf->dbuf);
+		dev_dbg(isp_vdev->dev,
+			"put dmabuf for vb->memory %d expbuf %d",
+			vb->memory,
+			buf->is_expbuf);
+	}
+}
+

Remove the isp4vid_qops_buffer_cleanup() function. It causes a use-after-free by doing an extra dma_buf_put(). This function isn't needed now that the refcount issues are solved.

[snip]

drivers/media/platform/amd/isp4/isp4_video.c line 1338

Quoted 3 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +3 -0
+static const struct vb2_ops isp4vid_qops = {
+	.queue_setup = isp4vid_qops_queue_setup,
+	.buf_cleanup = isp4vid_qops_buffer_cleanup,

Remove the .buf_cleanup hook too.

drivers/media/platform/amd/isp4/isp4_video.c line 1344

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_video.c new file +6 -0
+	.buf_queue = isp4vid_qops_buffer_queue,
+	.start_streaming = isp4vid_qops_start_streaming,
+	.stop_streaming = isp4vid_qops_stop_streaming,
+	.wait_prepare = vb2_ops_wait_prepare,
+	.wait_finish = vb2_ops_wait_finish,
+};

[snip]

Along with the changes from my other emails, I believe this finally fixes all of the refcounting issues. No more UaF or leaks here. :-)

Sultan

author maintainer
#54 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Hi Sultan, really appreciate your time and effort

Quoted 31 lines · Sultan Alsawaf

I found more refcounting issues in addition to the ones from my other emails while trying to make my webcam work:

Quoted 13 lines · Bin Du
(quoted diff) +13 -0
+static int isp4vid_vb2_mmap(void *buf_priv, struct vm_area_struct *vma)
+{
+	struct isp4vid_vb2_buf *buf = buf_priv;
+	int ret;
+
+	if (!buf) {
+		pr_err("fail no memory to map\n");
+		return -EINVAL;
+	}
+
+	ret = remap_vmalloc_range(vma, buf->vaddr, 0);
+	if (ret) {
+		dev_err(buf->dev, "fail remap vmalloc mem, %d\n", ret);

Review

Quoted 10 lines
(quoted diff) +10 -0
+		return ret;
+	}
+
+	/*
+	 * Make sure that vm_areas for 2 buffers won't be merged together
+	 */
+	vm_flags_set(vma, VM_DONTEXPAND);
+
+	dev_dbg(buf->dev, "mmap isp user bo 0x%llx size %ld refcount %d\n",
+		buf->gpu_addr, buf->size, buf->refcount.refs.counter);

Use refcount_read() instead of reading the refcount's atomic_t counter directly. This is done in 3 other places; change those to refcount_read() as well.

Quoted 6 lines

This didn't cause any functional problems, but it should still be fixed.

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+	return 0;
+}

Sure, will fix it

Quoted 31 lines

[snip]

Quoted 12 lines
(quoted diff) +12 -0
+static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
+{
+	struct isp4vid_vb2_buf *buf = mem_priv;
+
+	if (!buf) {
+		pr_err("fail invalid buf handle\n");
+		return;
+	}
+
+	struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);
+
+	dev_dbg(buf->dev, "detach dmabuf of isp user bo 0x%llx size %ld",

Review

Quoted 10 lines
(quoted diff) +10 -0
+		buf->gpu_addr, buf->size);
+
+	if (buf->vaddr)
+		dma_buf_vunmap_unlocked(buf->dbuf, &map);
+
+	// put dmabuf for exported ones
+	dma_buf_put(buf->dbuf);
+
+	kfree(buf);
+}

As mentioned in the other email, the dma_buf_put() here needs to be removed. But that's not all: the dma_buf_vunmap_unlocked() needs to be removed too because vb2 will always unmap the buffer before detaching it. As a result, having the dma_buf_vunmap_unlocked() call here results in a use-after-free when vb2 calls the unmap_dmabuf memop.

Quoted 9 lines

Change this function to the following:

	static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
	{
		struct isp4vid_vb2_buf *buf = mem_priv;

		kfree(buf);
	}

Will have a try

Quoted 20 lines
Quoted 20 lines
(quoted diff) +20 -0
+static void isp4vid_qops_buffer_cleanup(struct vb2_buffer *vb)
+{
+	struct isp4vid_dev *isp_vdev = vb2_get_drv_priv(vb->vb2_queue);
+	struct isp4vid_vb2_buf *buf = vb->planes[0].mem_priv;
+
+	dev_dbg(isp_vdev->dev, "%s|index=%u vb->memory %u",
+		isp_vdev->vdev.name, vb->index, vb->memory);
+
+	if (!buf) {
+		dev_err(isp_vdev->dev, "Invalid buf handle");
+		return;
+	}
+
+	// release implicit dmabuf reference here for vb2 buffer
+	// of type MMAP and is exported
+	if (vb->memory == VB2_MEMORY_MMAP && buf->is_expbuf) {
+		dma_buf_put(buf->dbuf);
+		dev_dbg(isp_vdev->dev,
+			"put dmabuf for vb->memory %d expbuf %d",
+			vb->memory,
Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+			buf->is_expbuf);
+	}
+}
+

Remove the isp4vid_qops_buffer_cleanup() function. It causes a use-after-free by doing an extra dma_buf_put(). This function isn't needed now that the refcount issues are solved.

Will have a try

Quoted 8 lines

[snip]

Review

Quoted 3 lines
(quoted diff) +3 -0
+static const struct vb2_ops isp4vid_qops = {
+	.queue_setup = isp4vid_qops_queue_setup,
+	.buf_cleanup = isp4vid_qops_buffer_cleanup,

Remove the .buf_cleanup hook too.

Will have a try

Quoted 9 lines
Quoted 6 lines
(quoted diff) +6 -0
+	.buf_queue = isp4vid_qops_buffer_queue,
+	.start_streaming = isp4vid_qops_start_streaming,
+	.stop_streaming = isp4vid_qops_stop_streaming,
+	.wait_prepare = vb2_ops_wait_prepare,
+	.wait_finish = vb2_ops_wait_finish,
+};

[snip]

Quoted 4 lines

Along with the changes from my other emails, I believe this finally fixes all of the refcounting issues. No more UaF or leaks here. :-)

Sultan

Tried changes in this and other two mails together, found the kernel will crash when re-open cheese App (means open cheese, close it and then open it again), the crash stack dump is shown below

[   38.008686] amdgpu 0000:c3:00.0: amdgpu: power on isp suc
[   38.032693] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 suc, reg_val 800
[   38.060866] amdgpu 0000:c3:00.0: amdgpu: set iclk 788 suc, reg_val 792
[   38.060877] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 iclk 788 to performance state 1 suc
[   38.425284] amdgpu 0000:c3:00.0: amdgpu: isp set performance do none to state 0
[   38.448631] amdgpu 0000:c3:00.0: amdgpu: power off isp suc
[   38.508368] amdgpu 0000:c3:00.0: amdgpu: power on isp suc
[   38.538525] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 suc, reg_val 800
[   38.568839] amdgpu 0000:c3:00.0: amdgpu: set iclk 788 suc, reg_val 792
[   38.568851] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 iclk 788 to performance state 1 suc
[   38.997779] amdgpu 0000:c3:00.0: amdgpu: isp set performance do none to state 0
[   39.029297] amdgpu 0000:c3:00.0: amdgpu: power off isp suc
[   39.084010] BUG: kernel NULL pointer dereference, address: 0000000000000008
[   39.084024] #PF: supervisor read access in kernel mode
[   39.084028] #PF: error_code(0x0000) - not-present page
[   39.084031] PGD 0 P4D 0
[   39.084037] Oops: Oops: 0000 [#1] PREEMPT SMP NOPTI
[   39.084042] CPU: 3 UID: 1000 PID: 3119 Comm: viewfinderbin-q Not tainted 6.14.0-rc2+ #944
[   39.084046] Hardware name: HP HP ZBook Ultra G1a 14 inch Mobile Workstation PC/8D01, BIOS X89 Ver. 01.03.00 04/25/2025
[   39.084049] RIP: 0010:validate_page_before_insert+0x5/0xc0
[   39.084060] Code: 31 c9 31 ff 45 31 c0 c3 cc cc cc cc 66 0f 1f 84 00 00 00 00 00 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 <48> 8b 46 08 a8 01 75 7a 66 90 48 89 f0 8b 50 34 85 d2 0f 84 8c 00
[   39.084064] RSP: 0018:ffffb95e05e7b6a0 EFLAGS: 00010202
[   39.084067] RAX: 0000000000000000 RBX: 8000000000000025 RCX: 0000000000000000
[   39.084069] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffff9efa10e400b8
[   39.084071] RBP: ffffb95e05e7b6e0 R08: 0000000000000000 R09: 0000000000000000
[   39.084072] R10: 0000000000000000 R11: 0000000000000000 R12: ffff9efa10e400b8
[   39.084074] R13: ffffb95e0e055000 R14: 00007fa3d6119000 R15: 0000000000000000
[   39.084076] FS:  00007fa3f4bf96c0(0000) GS:ffff9f050d780000(0000) knlGS:0000000000000000
[   39.084078] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   39.084080] CR2: 0000000000000008 CR3: 0000000137bba000 CR4: 0000000000750ef0
[   39.084083] PKRU: 55555554
[   39.084084] Call Trace:
[   39.084087]  <TASK>
[   39.084093]  ? show_regs+0x71/0x90
[   39.084099]  ? __die+0x25/0x80
[   39.084102]  ? page_fault_oops+0x15c/0x570
[   39.084108]  ? do_user_addr_fault+0x4a9/0x810
[   39.084111]  ? __mod_memcg_lruvec_state+0xde/0x280
[   39.084117]  ? exc_page_fault+0x83/0x1d0
[   39.084123]  ? asm_exc_page_fault+0x27/0x30
[   39.084131]  ? validate_page_before_insert+0x5/0xc0
[   39.084134]  ? vm_insert_page+0xd4/0x1a0
[   39.084139]  remap_vmalloc_range_partial+0xe3/0x160
[   39.084145]  remap_vmalloc_range+0x21/0x40
[   39.084153]  isp4vid_vb2_mmap+0x2d/0x130 [amd_capture]
[   39.084164]  isp4vid_vb2_dmabuf_ops_mmap+0x12/0x20 [amd_capture]
[   39.084168]  dma_buf_mmap_internal+0x41/0x70
[   39.084173]  __mmap_region+0x62f/0xbc0
[   39.084179]  mmap_region+0x67/0xe0
[   39.084181]  do_mmap+0x52b/0x650
[   39.084187]  vm_mmap_pgoff+0xf4/0x1c0
[   39.084194]  ksys_mmap_pgoff+0x182/0x250
[   39.084198]  __x64_sys_mmap+0x33/0x70
[   39.084202]  x64_sys_call+0x2541/0x26f0
[   39.084206]  do_syscall_64+0x70/0x130
[   39.084212]  ? __rseq_handle_notify_resume+0xa4/0x520
[   39.084216]  ? do_futex+0x17f/0x220
[   39.084220]  ? restore_fpregs_from_fpstate+0x3d/0xd0
[   39.084225]  ? switch_fpu_return+0x50/0xe0
[   39.084229]  ? syscall_exit_to_user_mode+0x18c/0x1c0
[   39.084234]  ? do_syscall_64+0x7c/0x130
[   39.084237]  ? __pfx_futex_wake_mark+0x10/0x10
[   39.084241]  ? hrtimer_cancel+0x15/0x50
[   39.084246]  ? futex_wait+0x7c/0x120
[   39.084249]  ? __pfx_hrtimer_wakeup+0x10/0x10
[   39.084254]  ? __rseq_handle_notify_resume+0xa4/0x520
[   39.084256]  ? do_futex+0x17f/0x220
[   39.084258]  ? restore_fpregs_from_fpstate+0x3d/0xd0
[   39.084261]  ? switch_fpu_return+0x50/0xe0
[   39.084264]  ? syscall_exit_to_user_mode+0x18c/0x1c0
[   39.084268]  ? do_syscall_64+0x7c/0x130
[   39.084272]  ? __count_memcg_events+0xcd/0x190
[   39.084276]  ? count_memcg_events.constprop.0+0x2a/0x50
[   39.084279]  ? handle_mm_fault+0x1ef/0x2d0
[   39.084284]  ? do_user_addr_fault+0x58d/0x810
[   39.084286]  ? irqentry_exit_to_user_mode+0x33/0x180
[   39.084291]  ? irqentry_exit+0x43/0x50
[   39.084294]  ? exc_page_fault+0x94/0x1d0
[   39.084298]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[   39.084302] RIP: 0033:0x7fa463b2531c
[   39.084305] Code: 1e fa 41 f7 c1 ff 0f 00 00 75 33 55 48 89 e5 41 54 41 89 cc 53 48 89 fb 48 85 ff 74 41 45 89 e2 48 89 df b8 09 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 7c 5b 41 5c 5d c3 0f 1f 80 00 00 00 00 48 8b
[   39.084308] RSP: 002b:00007fa3f4bf79e0 EFLAGS: 00000246 ORIG_RAX: 0000000000000009
[   39.084311] RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007fa463b2531c
[   39.084312] RDX: 0000000000000001 RSI: 00000000007782c0 RDI: 0000000000000000
[   39.084314] RBP: 00007fa3f4bf79f0 R08: 0000000000000047 R09: 0000000000000000
[   39.084315] R10: 0000000000000001 R11: 0000000000000246 R12: 0000000000000001
[   39.084317] R13: 0000000000000000 R14: 00007fa3f8019b48 R15: 00007fa3f4bf7b60
[   39.084320]  </TASK>
[   39.084321] Modules linked in: snd_seq_dummy snd_hrtimer hid_sensor_prox hid_sensor_gyro_3d hid_sensor_trigger industrialio_triggered_buffer kfifo_buf hid_sensor_iio_common industrialio hid_sensor_hub rfcomm cmac algif_hash algif_skcipher af_alg hid_logitech_hidpp ov05c10 v4l2_cci amd_capture v4l2_fwnode videobuf2_memops videobuf2_v4l2 videobuf2_common v4l2_async videodev pinctrl_amdisp i2c_designware_amdisp qrtr bnep btusb btrtl btintel btbcm btmtk bluetooth hid_logitech_dj intel_rapl_msr amd_atl intel_rapl_common binfmt_misc nls_iso8859_1 snd_acp_legacy_mach snd_acp_mach snd_soc_nau8821 snd_acp3x_rn amdgpu edac_mce_amd snd_ctl_led snd_hda_codec_realtek snd_acp70 snd_hda_codec_generic snd_acp_i2s snd_acp_pdm snd_hda_scodec_component snd_soc_dmic snd_acp_pcm snd_sof_amd_rembrandt snd_sof_amd_renoir snd_sof_amd_acp snd_sof_pci snd_sof_xtensa_dsp snd_hda_codec_hdmi snd_sof snd_sof_utils snd_pci_ps snd_soc_acpi_amd_match snd_amd_sdw_acpi soundwire_amd soundwire_generic_allocation soundwire_bus snd_hda_intel kvm_amd
[   39.084385]  snd_soc_sdca snd_intel_dspcfg snd_intel_sdw_acpi snd_soc_core snd_hda_codec amdxcp kvm snd_compress drm_panel_backlight_quirks snd_hda_core snd_usb_audio gpu_sched ac97_bus polyval_clmulni snd_pcm_dmaengine polyval_generic ghash_clmulni_intel drm_buddy snd_usbmidi_lib snd_seq_midi snd_rpl_pci_acp6x sha256_ssse3 snd_ump drm_ttm_helper snd_seq_midi_event snd_hwdep sha1_ssse3 ttm aesni_intel drm_exec snd_rawmidi drm_suballoc_helper snd_acp_pci mc crypto_simd snd_acp_legacy_common drm_client_lib cryptd drm_display_helper snd_pci_acp6x snd_seq rapl hp_wmi cec wmi_bmof snd_pcm sparse_keymap snd_seq_device rc_core snd_timer snd_pci_acp5x drm_kms_helper snd_rn_pci_acp3x amd_pmf snd i2c_algo_bit snd_acp_config i2c_piix4 snd_soc_acpi amdtee soundcore snd_pci_acp3x i2c_smbus ccp joydev tee input_leds serial_multi_instantiate amd_pmc platform_profile wireless_hotkey mac_hid serio_raw sch_fq_codel msr parport_pc ppdev lp parport efi_pstore nfnetlink dmi_sysfs ip_tables x_tables autofs4 r8152 mii usbhid hid_multitouch
[   39.084451]  hid_generic nvme ucsi_acpi i2c_hid_acpi video typec_ucsi i2c_hid amd_sfh thunderbolt hid nvme_core typec wmi drm
[   39.084465] CR2: 0000000000000008
[   39.084468] ---[ end trace 0000000000000000 ]---
[   39.116114] amdgpu 0000:c3:00.0: amdgpu: power on isp suc
[   39.159519] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 suc, reg_val 800
[   39.201325] amdgpu 0000:c3:00.0: amdgpu: set iclk 788 suc, reg_val 792
[   39.201334] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 iclk 788 to performance state 1 suc
[   39.343894] RIP: 0010:validate_page_before_insert+0x5/0xc0
[   39.343907] Code: 31 c9 31 ff 45 31 c0 c3 cc cc cc cc 66 0f 1f 84 00 00 00 00 00 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 <48> 8b 46 08 a8 01 75 7a 66 90 48 89 f0 8b 50 34 85 d2 0f 84 8c 00
[   39.343909] RSP: 0018:ffffb95e05e7b6a0 EFLAGS: 00010202
[   39.343911] RAX: 0000000000000000 RBX: 8000000000000025 RCX: 0000000000000000
[   39.343913] RDX: 0000000000000000 RSI: 0000000000000000 RDI: ffff9efa10e400b8
[   39.343913] RBP: ffffb95e05e7b6e0 R08: 0000000000000000 R09: 0000000000000000
[   39.343914] R10: 0000000000000000 R11: 0000000000000000 R12: ffff9efa10e400b8
[   39.343915] R13: ffffb95e0e055000 R14: 00007fa3d6119000 R15: 0000000000000000
[   39.343916] FS:  00007fa3f4bf96c0(0000) GS:ffff9f050d780000(0000) knlGS:0000000000000000
[   39.343917] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   39.343918] CR2: 0000000000000008 CR3: 0000000137bba000 CR4: 0000000000750ef0
[   39.343919] PKRU: 55555554
[   39.343920] note: viewfinderbin-q[3119] exited with irqs disabled

This is the patch, it should have contained all your changes

From b0a61b6b7500635928166516d5563e6de50300bb Mon Sep 17 00:00:00 2001
From: Bin Du <[email protected]>
Date: Mon, 28 Jul 2025 14:18:04 +0800
Subject: [PATCH] add comment from community

Change-Id: I06fff629c0dc691eaeaa9d9b81e6af27cbb6adb2

---
 drivers/media/platform/amd/isp4/isp4_video.c | 59 ++++++--------------
 1 file changed, 17 insertions(+), 42 deletions(-)
drivers/media/platform/amd/isp4/isp4_video.c +17 -42
index a1376498c246..b210a0b1e49b 100644
@@ -183,7 +183,7 @@ static int isp4vid_vb2_mmap(void *buf_priv, struct vm_area_struct *vma)
 	vm_flags_set(vma, VM_DONTEXPAND);
 
 	dev_dbg(buf->dev, "mmap isp user bo 0x%llx size %ld refcount %d\n",
-		buf->gpu_addr, buf->size, buf->refcount.refs.counter);
+		buf->gpu_addr, buf->size, refcount_read(&buf->refcount));
 
 	return 0;
 }
@@ -214,14 +214,9 @@ static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
 		return;
 	}
 
-	struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);
-
 	dev_dbg(buf->dev, "detach dmabuf of isp user bo 0x%llx size %ld",
 		buf->gpu_addr, buf->size);
 
-	if (buf->vaddr)
-		dma_buf_vunmap_unlocked(buf->dbuf, &map);
-
 	kfree(buf);
 }
 
@@ -470,21 +465,25 @@ static struct dma_buf *isp4vid_vb2_get_dmabuf(struct vb2_buffer *vb,
 					      unsigned long flags)
 {
 	struct isp4vid_vb2_buf *buf = buf_priv;
-	struct dma_buf *dbuf;
+	struct dma_buf *dbuf = buf->dbuf;
+
+	if (dbuf) {
+		dev_dbg(buf->dev, "dbuf already created, reuse %s dbuf\n",
+			buf->is_expbuf ? "exported" : "implicit");
+		get_dma_buf(dbuf);
 
-	if (buf->dbuf) {
-		dev_dbg(buf->dev, "dbuf already created, reuse implicit dbuf\n");
-		dbuf = buf->dbuf;
 	} else {
 		dbuf = isp4vid_get_dmabuf(vb, buf_priv, flags);
+		if (!dbuf)
+			return NULL;
+
 		dev_dbg(buf->dev, "created new dbuf\n");
+		buf->is_expbuf = true;
+		refcount_inc(&buf->refcount);
 	}
 
-	buf->is_expbuf = true;
-	refcount_inc(&buf->refcount);
-
 	dev_dbg(buf->dev, "buf exported, refcount %d\n",
-		buf->refcount.refs.counter);
+		refcount_read(&buf->refcount));
 
 	return dbuf;
 }
@@ -579,8 +578,9 @@ static void isp4vid_vb2_put(void *buf_priv)
 {
 	struct isp4vid_vb2_buf *buf = (struct isp4vid_vb2_buf *)buf_priv;
 	unsigned int ref_cnt = refcount_read(&buf->refcount);
+	struct device *dev = buf->dev;
 
-	dev_dbg(buf->dev,
+	dev_dbg(dev,
 		"release isp user bo 0x%llx size %ld refcount %u is_expbuf %d\n",
 		buf->gpu_addr, buf->size,
 		ref_cnt, buf->is_expbuf);
@@ -598,7 +598,7 @@ static void isp4vid_vb2_put(void *buf_priv)
 		/* the ref_cnt - 1 is just a predicted value as reference, can't
 		 * guarantee it's the actual value now
 		 */
-		dev_warn(buf->dev, "ignore buffer free, refcount %u > 0\n", ref_cnt - 1);
+		dev_warn(dev, "ignore buffer free, refcount %u > 0\n", ref_cnt - 1);
 	}
 }
 
@@ -642,7 +642,7 @@ static void *isp4vid_vb2_alloc(struct vb2_buffer *vb, struct device *dev,
 	refcount_set(&buf->refcount, 1);
 
 	dev_dbg(dev, "allocated isp user bo 0x%llx size %ld refcount %d\n",
-		buf->gpu_addr, buf->size, buf->refcount.refs.counter);
+		buf->gpu_addr, buf->size, refcount_read(&buf->refcount));
 
 	return buf;
 err_free:
@@ -1140,30 +1140,6 @@ static void isp4vid_qops_buffer_queue(struct vb2_buffer *vb)
 	mutex_unlock(&isp_vdev->buf_list_lock);
 }
 
-static void isp4vid_qops_buffer_cleanup(struct vb2_buffer *vb)
-{
-	struct isp4vid_dev *isp_vdev = vb2_get_drv_priv(vb->vb2_queue);
-	struct isp4vid_vb2_buf *buf = vb->planes[0].mem_priv;
-
-	dev_dbg(isp_vdev->dev, "%s|index=%u vb->memory %u",
-		isp_vdev->vdev.name, vb->index, vb->memory);
-
-	if (!buf) {
-		dev_err(isp_vdev->dev, "Invalid buf handle");
-		return;
-	}
-
-	// release implicit dmabuf reference here for vb2 buffer
-	// of type MMAP and is exported
-	if (vb->memory == VB2_MEMORY_MMAP && buf->is_expbuf) {
-		dma_buf_put(buf->dbuf);
-		dev_dbg(isp_vdev->dev,
-			"put dmabuf for vb->memory %d expbuf %d",
-			vb->memory,
-			buf->is_expbuf);
-	}
-}
-
 static int isp4vid_qops_start_streaming(struct vb2_queue *vq,
 					unsigned int count)
 {
@@ -1343,7 +1319,6 @@ static int isp4vid_fill_buffer_size(struct isp4vid_dev *isp_vdev)
 
 static const struct vb2_ops isp4vid_qops = {
 	.queue_setup = isp4vid_qops_queue_setup,
-	.buf_cleanup = isp4vid_qops_buffer_cleanup,
 	.buf_queue = isp4vid_qops_buffer_queue,
 	.start_streaming = isp4vid_qops_start_streaming,
 	.stop_streaming = isp4vid_qops_stop_streaming,
Signature · 4 lines
2.34.1

Regards,
Bin
#55 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Quoted 464 lines · Bin

Hi Sultan, really appreciate your time and effort

Quoted 31 lines · Sultan Alsawaf

I found more refcounting issues in addition to the ones from my other emails while trying to make my webcam work:

Quoted 13 lines · Bin Du
(quoted diff) +13 -0
+static int isp4vid_vb2_mmap(void *buf_priv, struct vm_area_struct *vma)
+{
+	struct isp4vid_vb2_buf *buf = buf_priv;
+	int ret;
+
+	if (!buf) {
+		pr_err("fail no memory to map\n");
+		return -EINVAL;
+	}
+
+	ret = remap_vmalloc_range(vma, buf->vaddr, 0);
+	if (ret) {
+		dev_err(buf->dev, "fail remap vmalloc mem, %d\n", ret);

Review

Quoted 10 lines
(quoted diff) +10 -0
+		return ret;
+	}
+
+	/*
+	 * Make sure that vm_areas for 2 buffers won't be merged together
+	 */
+	vm_flags_set(vma, VM_DONTEXPAND);
+
+	dev_dbg(buf->dev, "mmap isp user bo 0x%llx size %ld refcount %d\n",
+		buf->gpu_addr, buf->size, buf->refcount.refs.counter);

Use refcount_read() instead of reading the refcount's atomic_t counter directly. This is done in 3 other places; change those to refcount_read() as well.

Quoted 6 lines

This didn't cause any functional problems, but it should still be fixed.

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+	return 0;
+}

Sure, will fix it

Quoted 31 lines

[snip]

Quoted 12 lines
(quoted diff) +12 -0
+static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
+{
+	struct isp4vid_vb2_buf *buf = mem_priv;
+
+	if (!buf) {
+		pr_err("fail invalid buf handle\n");
+		return;
+	}
+
+	struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);
+
+	dev_dbg(buf->dev, "detach dmabuf of isp user bo 0x%llx size %ld",

Review

Quoted 10 lines
(quoted diff) +10 -0
+		buf->gpu_addr, buf->size);
+
+	if (buf->vaddr)
+		dma_buf_vunmap_unlocked(buf->dbuf, &map);
+
+	// put dmabuf for exported ones
+	dma_buf_put(buf->dbuf);
+
+	kfree(buf);
+}

As mentioned in the other email, the dma_buf_put() here needs to be removed. But that's not all: the dma_buf_vunmap_unlocked() needs to be removed too because vb2 will always unmap the buffer before detaching it. As a result, having the dma_buf_vunmap_unlocked() call here results in a use-after-free when vb2 calls the unmap_dmabuf memop.

Quoted 9 lines

Change this function to the following:

	static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
	{
		struct isp4vid_vb2_buf *buf = mem_priv;

		kfree(buf);
	}

Will have a try

Quoted 20 lines
Quoted 20 lines
(quoted diff) +20 -0
+static void isp4vid_qops_buffer_cleanup(struct vb2_buffer *vb)
+{
+	struct isp4vid_dev *isp_vdev = vb2_get_drv_priv(vb->vb2_queue);
+	struct isp4vid_vb2_buf *buf = vb->planes[0].mem_priv;
+
+	dev_dbg(isp_vdev->dev, "%s|index=%u vb->memory %u",
+		isp_vdev->vdev.name, vb->index, vb->memory);
+
+	if (!buf) {
+		dev_err(isp_vdev->dev, "Invalid buf handle");
+		return;
+	}
+
+	// release implicit dmabuf reference here for vb2 buffer
+	// of type MMAP and is exported
+	if (vb->memory == VB2_MEMORY_MMAP && buf->is_expbuf) {
+		dma_buf_put(buf->dbuf);
+		dev_dbg(isp_vdev->dev,
+			"put dmabuf for vb->memory %d expbuf %d",
+			vb->memory,
Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+			buf->is_expbuf);
+	}
+}
+

Remove the isp4vid_qops_buffer_cleanup() function. It causes a use-after-free by doing an extra dma_buf_put(). This function isn't needed now that the refcount issues are solved.

Will have a try

Quoted 8 lines

[snip]

Review

Quoted 3 lines
(quoted diff) +3 -0
+static const struct vb2_ops isp4vid_qops = {
+	.queue_setup = isp4vid_qops_queue_setup,
+	.buf_cleanup = isp4vid_qops_buffer_cleanup,

Remove the .buf_cleanup hook too.

Will have a try

Quoted 9 lines
Quoted 6 lines
(quoted diff) +6 -0
+	.buf_queue = isp4vid_qops_buffer_queue,
+	.start_streaming = isp4vid_qops_start_streaming,
+	.stop_streaming = isp4vid_qops_stop_streaming,
+	.wait_prepare = vb2_ops_wait_prepare,
+	.wait_finish = vb2_ops_wait_finish,
+};

[snip]

Quoted 4 lines

Along with the changes from my other emails, I believe this finally fixes all of the refcounting issues. No more UaF or leaks here. :-)

Sultan

Tried changes in this and other two mails together, found the kernel will crash when re-open cheese App (means open cheese, close it and then open it again), the crash stack dump is shown below

[   38.008686] amdgpu 0000:c3:00.0: amdgpu: power on isp suc
[   38.032693] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 suc, reg_val 800
[   38.060866] amdgpu 0000:c3:00.0: amdgpu: set iclk 788 suc, reg_val 792
[   38.060877] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 iclk 788 to
performance state 1 suc
[   38.425284] amdgpu 0000:c3:00.0: amdgpu: isp set performance do none to
state 0
[   38.448631] amdgpu 0000:c3:00.0: amdgpu: power off isp suc
[   38.508368] amdgpu 0000:c3:00.0: amdgpu: power on isp suc
[   38.538525] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 suc, reg_val 800
[   38.568839] amdgpu 0000:c3:00.0: amdgpu: set iclk 788 suc, reg_val 792
[   38.568851] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 iclk 788 to
performance state 1 suc
[   38.997779] amdgpu 0000:c3:00.0: amdgpu: isp set performance do none to
state 0
[   39.029297] amdgpu 0000:c3:00.0: amdgpu: power off isp suc
[   39.084010] BUG: kernel NULL pointer dereference, address:
0000000000000008
[   39.084024] #PF: supervisor read access in kernel mode
[   39.084028] #PF: error_code(0x0000) - not-present page
[   39.084031] PGD 0 P4D 0
[   39.084037] Oops: Oops: 0000 [#1] PREEMPT SMP NOPTI
[   39.084042] CPU: 3 UID: 1000 PID: 3119 Comm: viewfinderbin-q Not tainted
6.14.0-rc2+ #944
[   39.084046] Hardware name: HP HP ZBook Ultra G1a 14 inch Mobile
Workstation PC/8D01, BIOS X89 Ver. 01.03.00 04/25/2025
[   39.084049] RIP: 0010:validate_page_before_insert+0x5/0xc0
[   39.084060] Code: 31 c9 31 ff 45 31 c0 c3 cc cc cc cc 66 0f 1f 84 00 00
00 00 00 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 <48>
8b 46 08 a8 01 75 7a 66 90 48 89 f0 8b 50 34 85 d2 0f 84 8c 00
[   39.084064] RSP: 0018:ffffb95e05e7b6a0 EFLAGS: 00010202
[   39.084067] RAX: 0000000000000000 RBX: 8000000000000025 RCX:
0000000000000000
[   39.084069] RDX: 0000000000000000 RSI: 0000000000000000 RDI:
ffff9efa10e400b8
[   39.084071] RBP: ffffb95e05e7b6e0 R08: 0000000000000000 R09:
0000000000000000
[   39.084072] R10: 0000000000000000 R11: 0000000000000000 R12:
ffff9efa10e400b8
[   39.084074] R13: ffffb95e0e055000 R14: 00007fa3d6119000 R15:
0000000000000000
[   39.084076] FS:  00007fa3f4bf96c0(0000) GS:ffff9f050d780000(0000)
knlGS:0000000000000000
[   39.084078] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   39.084080] CR2: 0000000000000008 CR3: 0000000137bba000 CR4:
0000000000750ef0
[   39.084083] PKRU: 55555554
[   39.084084] Call Trace:
[   39.084087]  <TASK>
[   39.084093]  ? show_regs+0x71/0x90
[   39.084099]  ? __die+0x25/0x80
[   39.084102]  ? page_fault_oops+0x15c/0x570
[   39.084108]  ? do_user_addr_fault+0x4a9/0x810
[   39.084111]  ? __mod_memcg_lruvec_state+0xde/0x280
[   39.084117]  ? exc_page_fault+0x83/0x1d0
[   39.084123]  ? asm_exc_page_fault+0x27/0x30
[   39.084131]  ? validate_page_before_insert+0x5/0xc0
[   39.084134]  ? vm_insert_page+0xd4/0x1a0
[   39.084139]  remap_vmalloc_range_partial+0xe3/0x160
[   39.084145]  remap_vmalloc_range+0x21/0x40
[   39.084153]  isp4vid_vb2_mmap+0x2d/0x130 [amd_capture]
[   39.084164]  isp4vid_vb2_dmabuf_ops_mmap+0x12/0x20 [amd_capture]
[   39.084168]  dma_buf_mmap_internal+0x41/0x70
[   39.084173]  __mmap_region+0x62f/0xbc0
[   39.084179]  mmap_region+0x67/0xe0
[   39.084181]  do_mmap+0x52b/0x650
[   39.084187]  vm_mmap_pgoff+0xf4/0x1c0
[   39.084194]  ksys_mmap_pgoff+0x182/0x250
[   39.084198]  __x64_sys_mmap+0x33/0x70
[   39.084202]  x64_sys_call+0x2541/0x26f0
[   39.084206]  do_syscall_64+0x70/0x130
[   39.084212]  ? __rseq_handle_notify_resume+0xa4/0x520
[   39.084216]  ? do_futex+0x17f/0x220
[   39.084220]  ? restore_fpregs_from_fpstate+0x3d/0xd0
[   39.084225]  ? switch_fpu_return+0x50/0xe0
[   39.084229]  ? syscall_exit_to_user_mode+0x18c/0x1c0
[   39.084234]  ? do_syscall_64+0x7c/0x130
[   39.084237]  ? __pfx_futex_wake_mark+0x10/0x10
[   39.084241]  ? hrtimer_cancel+0x15/0x50
[   39.084246]  ? futex_wait+0x7c/0x120
[   39.084249]  ? __pfx_hrtimer_wakeup+0x10/0x10
[   39.084254]  ? __rseq_handle_notify_resume+0xa4/0x520
[   39.084256]  ? do_futex+0x17f/0x220
[   39.084258]  ? restore_fpregs_from_fpstate+0x3d/0xd0
[   39.084261]  ? switch_fpu_return+0x50/0xe0
[   39.084264]  ? syscall_exit_to_user_mode+0x18c/0x1c0
[   39.084268]  ? do_syscall_64+0x7c/0x130
[   39.084272]  ? __count_memcg_events+0xcd/0x190
[   39.084276]  ? count_memcg_events.constprop.0+0x2a/0x50
[   39.084279]  ? handle_mm_fault+0x1ef/0x2d0
[   39.084284]  ? do_user_addr_fault+0x58d/0x810
[   39.084286]  ? irqentry_exit_to_user_mode+0x33/0x180
[   39.084291]  ? irqentry_exit+0x43/0x50
[   39.084294]  ? exc_page_fault+0x94/0x1d0
[   39.084298]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[   39.084302] RIP: 0033:0x7fa463b2531c
[   39.084305] Code: 1e fa 41 f7 c1 ff 0f 00 00 75 33 55 48 89 e5 41 54 41
89 cc 53 48 89 fb 48 85 ff 74 41 45 89 e2 48 89 df b8 09 00 00 00 0f 05 <48>
3d 00 f0 ff ff 77 7c 5b 41 5c 5d c3 0f 1f 80 00 00 00 00 48 8b
[   39.084308] RSP: 002b:00007fa3f4bf79e0 EFLAGS: 00000246 ORIG_RAX:
0000000000000009
[   39.084311] RAX: ffffffffffffffda RBX: 0000000000000000 RCX:
00007fa463b2531c
[   39.084312] RDX: 0000000000000001 RSI: 00000000007782c0 RDI:
0000000000000000
[   39.084314] RBP: 00007fa3f4bf79f0 R08: 0000000000000047 R09:
0000000000000000
[   39.084315] R10: 0000000000000001 R11: 0000000000000246 R12:
0000000000000001
[   39.084317] R13: 0000000000000000 R14: 00007fa3f8019b48 R15:
00007fa3f4bf7b60
[   39.084320]  </TASK>
[   39.084321] Modules linked in: snd_seq_dummy snd_hrtimer hid_sensor_prox

hid_sensor_gyro_3d hid_sensor_trigger industrialio_triggered_buffer kfifo_buf hid_sensor_iio_common industrialio hid_sensor_hub rfcomm cmac algif_hash algif_skcipher af_alg hid_logitech_hidpp ov05c10 v4l2_cci amd_capture v4l2_fwnode videobuf2_memops videobuf2_v4l2 videobuf2_common v4l2_async videodev pinctrl_amdisp i2c_designware_amdisp qrtr bnep btusb btrtl btintel btbcm btmtk bluetooth hid_logitech_dj intel_rapl_msr amd_atl intel_rapl_common binfmt_misc nls_iso8859_1 snd_acp_legacy_mach snd_acp_mach snd_soc_nau8821 snd_acp3x_rn amdgpu edac_mce_amd snd_ctl_led snd_hda_codec_realtek snd_acp70 snd_hda_codec_generic snd_acp_i2s snd_acp_pdm snd_hda_scodec_component snd_soc_dmic snd_acp_pcm snd_sof_amd_rembrandt snd_sof_amd_renoir snd_sof_amd_acp snd_sof_pci snd_sof_xtensa_dsp snd_hda_codec_hdmi snd_sof snd_sof_utils snd_pci_ps snd_soc_acpi_amd_match snd_amd_sdw_acpi soundwire_amd soundwire_generic_allocation soundwire_bus snd_hda_intel kvm_amd

[   39.084385]  snd_soc_sdca snd_intel_dspcfg snd_intel_sdw_acpi

snd_soc_core snd_hda_codec amdxcp kvm snd_compress drm_panel_backlight_quirks snd_hda_core snd_usb_audio gpu_sched ac97_bus polyval_clmulni snd_pcm_dmaengine polyval_generic ghash_clmulni_intel drm_buddy snd_usbmidi_lib snd_seq_midi snd_rpl_pci_acp6x sha256_ssse3 snd_ump drm_ttm_helper snd_seq_midi_event snd_hwdep sha1_ssse3 ttm aesni_intel drm_exec snd_rawmidi drm_suballoc_helper snd_acp_pci mc crypto_simd snd_acp_legacy_common drm_client_lib cryptd drm_display_helper snd_pci_acp6x snd_seq rapl hp_wmi cec wmi_bmof snd_pcm sparse_keymap snd_seq_device rc_core snd_timer snd_pci_acp5x drm_kms_helper snd_rn_pci_acp3x amd_pmf snd i2c_algo_bit snd_acp_config i2c_piix4 snd_soc_acpi amdtee soundcore snd_pci_acp3x i2c_smbus ccp joydev tee input_leds serial_multi_instantiate amd_pmc platform_profile wireless_hotkey mac_hid serio_raw sch_fq_codel msr parport_pc ppdev lp parport efi_pstore nfnetlink dmi_sysfs ip_tables x_tables autofs4 r8152 mii usbhid hid_multitouch

[   39.084451]  hid_generic nvme ucsi_acpi i2c_hid_acpi video typec_ucsi
i2c_hid amd_sfh thunderbolt hid nvme_core typec wmi drm
[   39.084465] CR2: 0000000000000008
[   39.084468] ---[ end trace 0000000000000000 ]---
[   39.116114] amdgpu 0000:c3:00.0: amdgpu: power on isp suc
[   39.159519] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 suc, reg_val 800
[   39.201325] amdgpu 0000:c3:00.0: amdgpu: set iclk 788 suc, reg_val 792
[   39.201334] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 iclk 788 to
performance state 1 suc
[   39.343894] RIP: 0010:validate_page_before_insert+0x5/0xc0
[   39.343907] Code: 31 c9 31 ff 45 31 c0 c3 cc cc cc cc 66 0f 1f 84 00 00
00 00 00 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 <48>
8b 46 08 a8 01 75 7a 66 90 48 89 f0 8b 50 34 85 d2 0f 84 8c 00
[   39.343909] RSP: 0018:ffffb95e05e7b6a0 EFLAGS: 00010202
[   39.343911] RAX: 0000000000000000 RBX: 8000000000000025 RCX:
0000000000000000
[   39.343913] RDX: 0000000000000000 RSI: 0000000000000000 RDI:
ffff9efa10e400b8
[   39.343913] RBP: ffffb95e05e7b6e0 R08: 0000000000000000 R09:
0000000000000000
[   39.343914] R10: 0000000000000000 R11: 0000000000000000 R12:
ffff9efa10e400b8
[   39.343915] R13: ffffb95e0e055000 R14: 00007fa3d6119000 R15:
0000000000000000
[   39.343916] FS:  00007fa3f4bf96c0(0000) GS:ffff9f050d780000(0000)
knlGS:0000000000000000
[   39.343917] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   39.343918] CR2: 0000000000000008 CR3: 0000000137bba000 CR4:
0000000000750ef0
[   39.343919] PKRU: 55555554
[   39.343920] note: viewfinderbin-q[3119] exited with irqs disabled

This is the patch, it should have contained all your changes

From b0a61b6b7500635928166516d5563e6de50300bb Mon Sep 17 00:00:00 2001
From: Bin Du <[email protected]>
Date: Mon, 28 Jul 2025 14:18:04 +0800
Subject: [PATCH] add comment from community

Change-Id: I06fff629c0dc691eaeaa9d9b81e6af27cbb6adb2

---
 drivers/media/platform/amd/isp4/isp4_video.c | 59 ++++++--------------
 1 file changed, 17 insertions(+), 42 deletions(-)
drivers/media/platform/amd/isp4/isp4_video.c +1 -1
index a1376498c246..b210a0b1e49b 100644
@@ -183,7 +183,7 @@ static int isp4vid_vb2_mmap(void *buf_priv, struct
 vm_area_struct *vma)
 	vm_flags_set(vma, VM_DONTEXPAND);
 
 	dev_dbg(buf->dev, "mmap isp user bo 0x%llx size %ld refcount %d\n",
-		buf->gpu_addr, buf->size, buf->refcount.refs.counter);
+		buf->gpu_addr, buf->size, refcount_read(&buf->refcount));
 
 	return 0;

}

drivers/media/platform/amd/isp4/isp4_video.c +12 -13
@@ -214,14 +214,9 @@ static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
 		return;
 	}
 
-	struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);
-
 	dev_dbg(buf->dev, "detach dmabuf of isp user bo 0x%llx size %ld",
 		buf->gpu_addr, buf->size);
 
-	if (buf->vaddr)
-		dma_buf_vunmap_unlocked(buf->dbuf, &map);
-
 	kfree(buf);
 }
 
@@ -470,21 +465,25 @@ static struct dma_buf *isp4vid_vb2_get_dmabuf(struct
 vb2_buffer *vb,
 					      unsigned long flags)
 {
 	struct isp4vid_vb2_buf *buf = buf_priv;
-	struct dma_buf *dbuf;
+	struct dma_buf *dbuf = buf->dbuf;
+
+	if (dbuf) {
+		dev_dbg(buf->dev, "dbuf already created, reuse %s dbuf\n",
+			buf->is_expbuf ? "exported" : "implicit");
+		get_dma_buf(dbuf);
 
-	if (buf->dbuf) {
-		dev_dbg(buf->dev, "dbuf already created, reuse implicit dbuf\n");
-		dbuf = buf->dbuf;
 	} else {
 		dbuf = isp4vid_get_dmabuf(vb, buf_priv, flags);
+		if (!dbuf)
+			return NULL;
+
 		dev_dbg(buf->dev, "created new dbuf\n");
+		buf->is_expbuf = true;
+		refcount_inc(&buf->refcount);
 	}
 
-	buf->is_expbuf = true;
-	refcount_inc(&buf->refcount);
-
 	dev_dbg(buf->dev, "buf exported, refcount %d\n",
-		buf->refcount.refs.counter);
+		refcount_read(&buf->refcount));
 
 	return dbuf;

}

drivers/media/platform/amd/isp4/isp4_video.c +4 -3
@@ -579,8 +578,9 @@ static void isp4vid_vb2_put(void *buf_priv)
 {
 	struct isp4vid_vb2_buf *buf = (struct isp4vid_vb2_buf *)buf_priv;
 	unsigned int ref_cnt = refcount_read(&buf->refcount);
+	struct device *dev = buf->dev;
 
-	dev_dbg(buf->dev,
+	dev_dbg(dev,
 		"release isp user bo 0x%llx size %ld refcount %u is_expbuf %d\n",
 		buf->gpu_addr, buf->size,
 		ref_cnt, buf->is_expbuf);
@@ -598,7 +598,7 @@ static void isp4vid_vb2_put(void *buf_priv)
 		/* the ref_cnt - 1 is just a predicted value as reference, can't
 		 * guarantee it's the actual value now
 		 */
-		dev_warn(buf->dev, "ignore buffer free, refcount %u > 0\n", ref_cnt - 1);
+		dev_warn(dev, "ignore buffer free, refcount %u > 0\n", ref_cnt - 1);
 	}
 }
 
@@ -642,7 +642,7 @@ static void *isp4vid_vb2_alloc(struct vb2_buffer *vb,
 struct device *dev,
 	refcount_set(&buf->refcount, 1);
 
 	dev_dbg(dev, "allocated isp user bo 0x%llx size %ld refcount %d\n",
-		buf->gpu_addr, buf->size, buf->refcount.refs.counter);
+		buf->gpu_addr, buf->size, refcount_read(&buf->refcount));
 
 	return buf;

err_free:

drivers/media/platform/amd/isp4/isp4_video.c +0 -24
@@ -1140,30 +1140,6 @@ static void isp4vid_qops_buffer_queue(struct
 vb2_buffer *vb)
 	mutex_unlock(&isp_vdev->buf_list_lock);
 }
 
-static void isp4vid_qops_buffer_cleanup(struct vb2_buffer *vb)
-{
-	struct isp4vid_dev *isp_vdev = vb2_get_drv_priv(vb->vb2_queue);
-	struct isp4vid_vb2_buf *buf = vb->planes[0].mem_priv;
-
-	dev_dbg(isp_vdev->dev, "%s|index=%u vb->memory %u",
-		isp_vdev->vdev.name, vb->index, vb->memory);
-
-	if (!buf) {
-		dev_err(isp_vdev->dev, "Invalid buf handle");
-		return;
-	}
-
-	// release implicit dmabuf reference here for vb2 buffer
-	// of type MMAP and is exported
-	if (vb->memory == VB2_MEMORY_MMAP && buf->is_expbuf) {
-		dma_buf_put(buf->dbuf);
-		dev_dbg(isp_vdev->dev,
-			"put dmabuf for vb->memory %d expbuf %d",
-			vb->memory,
-			buf->is_expbuf);
-	}
-}
-
 static int isp4vid_qops_start_streaming(struct vb2_queue *vq,
 					unsigned int count)

{

drivers/media/platform/amd/isp4/isp4_video.c +0 -1
@@ -1343,7 +1319,6 @@ static int isp4vid_fill_buffer_size(struct isp4vid_dev
 *isp_vdev)
 
 static const struct vb2_ops isp4vid_qops = {
 	.queue_setup = isp4vid_qops_queue_setup,
-	.buf_cleanup = isp4vid_qops_buffer_cleanup,
 	.buf_queue = isp4vid_qops_buffer_queue,
 	.start_streaming = isp4vid_qops_start_streaming,
Quoted 3 lines

.stop_streaming = isp4vid_qops_stop_streaming,

Signature · 1 line
2.34.1

The patch looks correct. I will get back to you on this after doing some testing myself.

FYI, I can only test these changes with the old isp4 driver right now, since the new isp4 driver doesn't work for me as mentioned in my other email. So far, something does seem wrong after these changes I suggested because they break the camera on the old isp4 driver too.

Sultan

author maintainer
#56 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Thanks Sultan.

Quoted 7 lines · Sultan Alsawaf
Quoted 6 lines · Bin

Hi Sultan, really appreciate your time and effort

Quoted 3 lines · Sultan Alsawaf

I found more refcounting issues in addition to the ones from my other emails while trying to make my webcam work:

[snip]

Quoted 12 lines
Quoted 8 lines
  static const struct vb2_ops isp4vid_qops = {
  	.queue_setup = isp4vid_qops_queue_setup,
drivers/media/platform/amd/isp4/isp4_video.c +0 -1
-	.buf_cleanup = isp4vid_qops_buffer_cleanup,
  	.buf_queue = isp4vid_qops_buffer_queue,
  	.start_streaming = isp4vid_qops_start_streaming,
  	.stop_streaming = isp4vid_qops_stop_streaming,
Signature · 1 line
2.34.1

The patch looks correct. I will get back to you on this after doing some testing myself.

Quoted 6 lines

FYI, I can only test these changes with the old isp4 driver right now, since the new isp4 driver doesn't work for me as mentioned in my other email. So far, something does seem wrong after these changes I suggested because they break the camera on the old isp4 driver too.

Sultan

Yes, seems so. At the same time, we are checking the new upstream isp4 driver

Regards,
Bin

#57 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

In reply to Sultan Alsawaf
Quoted 472 lines · Sultan Alsawaf
Quoted 464 lines · Bin

Hi Sultan, really appreciate your time and effort

Quoted 31 lines · Sultan Alsawaf

I found more refcounting issues in addition to the ones from my other emails while trying to make my webcam work:

Quoted 13 lines · Bin Du
(quoted diff) +13 -0
+static int isp4vid_vb2_mmap(void *buf_priv, struct vm_area_struct *vma)
+{
+	struct isp4vid_vb2_buf *buf = buf_priv;
+	int ret;
+
+	if (!buf) {
+		pr_err("fail no memory to map\n");
+		return -EINVAL;
+	}
+
+	ret = remap_vmalloc_range(vma, buf->vaddr, 0);
+	if (ret) {
+		dev_err(buf->dev, "fail remap vmalloc mem, %d\n", ret);

Review

Quoted 10 lines
(quoted diff) +10 -0
+		return ret;
+	}
+
+	/*
+	 * Make sure that vm_areas for 2 buffers won't be merged together
+	 */
+	vm_flags_set(vma, VM_DONTEXPAND);
+
+	dev_dbg(buf->dev, "mmap isp user bo 0x%llx size %ld refcount %d\n",
+		buf->gpu_addr, buf->size, buf->refcount.refs.counter);

Use refcount_read() instead of reading the refcount's atomic_t counter directly. This is done in 3 other places; change those to refcount_read() as well.

Quoted 6 lines

This didn't cause any functional problems, but it should still be fixed.

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+	return 0;
+}

Sure, will fix it

Quoted 31 lines

[snip]

Quoted 12 lines
(quoted diff) +12 -0
+static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
+{
+	struct isp4vid_vb2_buf *buf = mem_priv;
+
+	if (!buf) {
+		pr_err("fail invalid buf handle\n");
+		return;
+	}
+
+	struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);
+
+	dev_dbg(buf->dev, "detach dmabuf of isp user bo 0x%llx size %ld",

Review

Quoted 10 lines
(quoted diff) +10 -0
+		buf->gpu_addr, buf->size);
+
+	if (buf->vaddr)
+		dma_buf_vunmap_unlocked(buf->dbuf, &map);
+
+	// put dmabuf for exported ones
+	dma_buf_put(buf->dbuf);
+
+	kfree(buf);
+}

As mentioned in the other email, the dma_buf_put() here needs to be removed. But that's not all: the dma_buf_vunmap_unlocked() needs to be removed too because vb2 will always unmap the buffer before detaching it. As a result, having the dma_buf_vunmap_unlocked() call here results in a use-after-free when vb2 calls the unmap_dmabuf memop.

Quoted 9 lines

Change this function to the following:

	static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
	{
		struct isp4vid_vb2_buf *buf = mem_priv;

		kfree(buf);
	}

Will have a try

Quoted 20 lines
Quoted 20 lines
(quoted diff) +20 -0
+static void isp4vid_qops_buffer_cleanup(struct vb2_buffer *vb)
+{
+	struct isp4vid_dev *isp_vdev = vb2_get_drv_priv(vb->vb2_queue);
+	struct isp4vid_vb2_buf *buf = vb->planes[0].mem_priv;
+
+	dev_dbg(isp_vdev->dev, "%s|index=%u vb->memory %u",
+		isp_vdev->vdev.name, vb->index, vb->memory);
+
+	if (!buf) {
+		dev_err(isp_vdev->dev, "Invalid buf handle");
+		return;
+	}
+
+	// release implicit dmabuf reference here for vb2 buffer
+	// of type MMAP and is exported
+	if (vb->memory == VB2_MEMORY_MMAP && buf->is_expbuf) {
+		dma_buf_put(buf->dbuf);
+		dev_dbg(isp_vdev->dev,
+			"put dmabuf for vb->memory %d expbuf %d",
+			vb->memory,
Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+			buf->is_expbuf);
+	}
+}
+

Remove the isp4vid_qops_buffer_cleanup() function. It causes a use-after-free by doing an extra dma_buf_put(). This function isn't needed now that the refcount issues are solved.

Will have a try

Quoted 8 lines

[snip]

Review

Quoted 3 lines
(quoted diff) +3 -0
+static const struct vb2_ops isp4vid_qops = {
+	.queue_setup = isp4vid_qops_queue_setup,
+	.buf_cleanup = isp4vid_qops_buffer_cleanup,

Remove the .buf_cleanup hook too.

Will have a try

Quoted 9 lines
Quoted 6 lines
(quoted diff) +6 -0
+	.buf_queue = isp4vid_qops_buffer_queue,
+	.start_streaming = isp4vid_qops_start_streaming,
+	.stop_streaming = isp4vid_qops_stop_streaming,
+	.wait_prepare = vb2_ops_wait_prepare,
+	.wait_finish = vb2_ops_wait_finish,
+};

[snip]

Quoted 4 lines

Along with the changes from my other emails, I believe this finally fixes all of the refcounting issues. No more UaF or leaks here. :-)

Sultan

Tried changes in this and other two mails together, found the kernel will crash when re-open cheese App (means open cheese, close it and then open it again), the crash stack dump is shown below

[   38.008686] amdgpu 0000:c3:00.0: amdgpu: power on isp suc
[   38.032693] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 suc, reg_val 800
[   38.060866] amdgpu 0000:c3:00.0: amdgpu: set iclk 788 suc, reg_val 792
[   38.060877] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 iclk 788 to
performance state 1 suc
[   38.425284] amdgpu 0000:c3:00.0: amdgpu: isp set performance do none to
state 0
[   38.448631] amdgpu 0000:c3:00.0: amdgpu: power off isp suc
[   38.508368] amdgpu 0000:c3:00.0: amdgpu: power on isp suc
[   38.538525] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 suc, reg_val 800
[   38.568839] amdgpu 0000:c3:00.0: amdgpu: set iclk 788 suc, reg_val 792
[   38.568851] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 iclk 788 to
performance state 1 suc
[   38.997779] amdgpu 0000:c3:00.0: amdgpu: isp set performance do none to
state 0
[   39.029297] amdgpu 0000:c3:00.0: amdgpu: power off isp suc
[   39.084010] BUG: kernel NULL pointer dereference, address:
0000000000000008
[   39.084024] #PF: supervisor read access in kernel mode
[   39.084028] #PF: error_code(0x0000) - not-present page
[   39.084031] PGD 0 P4D 0
[   39.084037] Oops: Oops: 0000 [#1] PREEMPT SMP NOPTI
[   39.084042] CPU: 3 UID: 1000 PID: 3119 Comm: viewfinderbin-q Not tainted
6.14.0-rc2+ #944
[   39.084046] Hardware name: HP HP ZBook Ultra G1a 14 inch Mobile
Workstation PC/8D01, BIOS X89 Ver. 01.03.00 04/25/2025
[   39.084049] RIP: 0010:validate_page_before_insert+0x5/0xc0
[   39.084060] Code: 31 c9 31 ff 45 31 c0 c3 cc cc cc cc 66 0f 1f 84 00 00
00 00 00 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 <48>
8b 46 08 a8 01 75 7a 66 90 48 89 f0 8b 50 34 85 d2 0f 84 8c 00
[   39.084064] RSP: 0018:ffffb95e05e7b6a0 EFLAGS: 00010202
[   39.084067] RAX: 0000000000000000 RBX: 8000000000000025 RCX:
0000000000000000
[   39.084069] RDX: 0000000000000000 RSI: 0000000000000000 RDI:
ffff9efa10e400b8
[   39.084071] RBP: ffffb95e05e7b6e0 R08: 0000000000000000 R09:
0000000000000000
[   39.084072] R10: 0000000000000000 R11: 0000000000000000 R12:
ffff9efa10e400b8
[   39.084074] R13: ffffb95e0e055000 R14: 00007fa3d6119000 R15:
0000000000000000
[   39.084076] FS:  00007fa3f4bf96c0(0000) GS:ffff9f050d780000(0000)
knlGS:0000000000000000
[   39.084078] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   39.084080] CR2: 0000000000000008 CR3: 0000000137bba000 CR4:
0000000000750ef0
[   39.084083] PKRU: 55555554
[   39.084084] Call Trace:
[   39.084087]  <TASK>
[   39.084093]  ? show_regs+0x71/0x90
[   39.084099]  ? __die+0x25/0x80
[   39.084102]  ? page_fault_oops+0x15c/0x570
[   39.084108]  ? do_user_addr_fault+0x4a9/0x810
[   39.084111]  ? __mod_memcg_lruvec_state+0xde/0x280
[   39.084117]  ? exc_page_fault+0x83/0x1d0
[   39.084123]  ? asm_exc_page_fault+0x27/0x30
[   39.084131]  ? validate_page_before_insert+0x5/0xc0
[   39.084134]  ? vm_insert_page+0xd4/0x1a0
[   39.084139]  remap_vmalloc_range_partial+0xe3/0x160
[   39.084145]  remap_vmalloc_range+0x21/0x40
[   39.084153]  isp4vid_vb2_mmap+0x2d/0x130 [amd_capture]
[   39.084164]  isp4vid_vb2_dmabuf_ops_mmap+0x12/0x20 [amd_capture]
[   39.084168]  dma_buf_mmap_internal+0x41/0x70
[   39.084173]  __mmap_region+0x62f/0xbc0
[   39.084179]  mmap_region+0x67/0xe0
[   39.084181]  do_mmap+0x52b/0x650
[   39.084187]  vm_mmap_pgoff+0xf4/0x1c0
[   39.084194]  ksys_mmap_pgoff+0x182/0x250
[   39.084198]  __x64_sys_mmap+0x33/0x70
[   39.084202]  x64_sys_call+0x2541/0x26f0
[   39.084206]  do_syscall_64+0x70/0x130
[   39.084212]  ? __rseq_handle_notify_resume+0xa4/0x520
[   39.084216]  ? do_futex+0x17f/0x220
[   39.084220]  ? restore_fpregs_from_fpstate+0x3d/0xd0
[   39.084225]  ? switch_fpu_return+0x50/0xe0
[   39.084229]  ? syscall_exit_to_user_mode+0x18c/0x1c0
[   39.084234]  ? do_syscall_64+0x7c/0x130
[   39.084237]  ? __pfx_futex_wake_mark+0x10/0x10
[   39.084241]  ? hrtimer_cancel+0x15/0x50
[   39.084246]  ? futex_wait+0x7c/0x120
[   39.084249]  ? __pfx_hrtimer_wakeup+0x10/0x10
[   39.084254]  ? __rseq_handle_notify_resume+0xa4/0x520
[   39.084256]  ? do_futex+0x17f/0x220
[   39.084258]  ? restore_fpregs_from_fpstate+0x3d/0xd0
[   39.084261]  ? switch_fpu_return+0x50/0xe0
[   39.084264]  ? syscall_exit_to_user_mode+0x18c/0x1c0
[   39.084268]  ? do_syscall_64+0x7c/0x130
[   39.084272]  ? __count_memcg_events+0xcd/0x190
[   39.084276]  ? count_memcg_events.constprop.0+0x2a/0x50
[   39.084279]  ? handle_mm_fault+0x1ef/0x2d0
[   39.084284]  ? do_user_addr_fault+0x58d/0x810
[   39.084286]  ? irqentry_exit_to_user_mode+0x33/0x180
[   39.084291]  ? irqentry_exit+0x43/0x50
[   39.084294]  ? exc_page_fault+0x94/0x1d0
[   39.084298]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[   39.084302] RIP: 0033:0x7fa463b2531c
[   39.084305] Code: 1e fa 41 f7 c1 ff 0f 00 00 75 33 55 48 89 e5 41 54 41
89 cc 53 48 89 fb 48 85 ff 74 41 45 89 e2 48 89 df b8 09 00 00 00 0f 05 <48>
3d 00 f0 ff ff 77 7c 5b 41 5c 5d c3 0f 1f 80 00 00 00 00 48 8b
[   39.084308] RSP: 002b:00007fa3f4bf79e0 EFLAGS: 00000246 ORIG_RAX:
0000000000000009
[   39.084311] RAX: ffffffffffffffda RBX: 0000000000000000 RCX:
00007fa463b2531c
[   39.084312] RDX: 0000000000000001 RSI: 00000000007782c0 RDI:
0000000000000000
[   39.084314] RBP: 00007fa3f4bf79f0 R08: 0000000000000047 R09:
0000000000000000
[   39.084315] R10: 0000000000000001 R11: 0000000000000246 R12:
0000000000000001
[   39.084317] R13: 0000000000000000 R14: 00007fa3f8019b48 R15:
00007fa3f4bf7b60
[   39.084320]  </TASK>
[   39.084321] Modules linked in: snd_seq_dummy snd_hrtimer hid_sensor_prox

hid_sensor_gyro_3d hid_sensor_trigger industrialio_triggered_buffer kfifo_buf hid_sensor_iio_common industrialio hid_sensor_hub rfcomm cmac algif_hash algif_skcipher af_alg hid_logitech_hidpp ov05c10 v4l2_cci amd_capture v4l2_fwnode videobuf2_memops videobuf2_v4l2 videobuf2_common v4l2_async videodev pinctrl_amdisp i2c_designware_amdisp qrtr bnep btusb btrtl btintel btbcm btmtk bluetooth hid_logitech_dj intel_rapl_msr amd_atl intel_rapl_common binfmt_misc nls_iso8859_1 snd_acp_legacy_mach snd_acp_mach snd_soc_nau8821 snd_acp3x_rn amdgpu edac_mce_amd snd_ctl_led snd_hda_codec_realtek snd_acp70 snd_hda_codec_generic snd_acp_i2s snd_acp_pdm snd_hda_scodec_component snd_soc_dmic snd_acp_pcm snd_sof_amd_rembrandt snd_sof_amd_renoir snd_sof_amd_acp snd_sof_pci snd_sof_xtensa_dsp snd_hda_codec_hdmi snd_sof snd_sof_utils snd_pci_ps snd_soc_acpi_amd_match snd_amd_sdw_acpi soundwire_amd soundwire_generic_allocation soundwire_bus snd_hda_intel kvm_amd

[   39.084385]  snd_soc_sdca snd_intel_dspcfg snd_intel_sdw_acpi

snd_soc_core snd_hda_codec amdxcp kvm snd_compress drm_panel_backlight_quirks snd_hda_core snd_usb_audio gpu_sched ac97_bus polyval_clmulni snd_pcm_dmaengine polyval_generic ghash_clmulni_intel drm_buddy snd_usbmidi_lib snd_seq_midi snd_rpl_pci_acp6x sha256_ssse3 snd_ump drm_ttm_helper snd_seq_midi_event snd_hwdep sha1_ssse3 ttm aesni_intel drm_exec snd_rawmidi drm_suballoc_helper snd_acp_pci mc crypto_simd snd_acp_legacy_common drm_client_lib cryptd drm_display_helper snd_pci_acp6x snd_seq rapl hp_wmi cec wmi_bmof snd_pcm sparse_keymap snd_seq_device rc_core snd_timer snd_pci_acp5x drm_kms_helper snd_rn_pci_acp3x amd_pmf snd i2c_algo_bit snd_acp_config i2c_piix4 snd_soc_acpi amdtee soundcore snd_pci_acp3x i2c_smbus ccp joydev tee input_leds serial_multi_instantiate amd_pmc platform_profile wireless_hotkey mac_hid serio_raw sch_fq_codel msr parport_pc ppdev lp parport efi_pstore nfnetlink dmi_sysfs ip_tables x_tables autofs4 r8152 mii usbhid hid_multitouch

[   39.084451]  hid_generic nvme ucsi_acpi i2c_hid_acpi video typec_ucsi
i2c_hid amd_sfh thunderbolt hid nvme_core typec wmi drm
[   39.084465] CR2: 0000000000000008
[   39.084468] ---[ end trace 0000000000000000 ]---
[   39.116114] amdgpu 0000:c3:00.0: amdgpu: power on isp suc
[   39.159519] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 suc, reg_val 800
[   39.201325] amdgpu 0000:c3:00.0: amdgpu: set iclk 788 suc, reg_val 792
[   39.201334] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 iclk 788 to
performance state 1 suc
[   39.343894] RIP: 0010:validate_page_before_insert+0x5/0xc0
[   39.343907] Code: 31 c9 31 ff 45 31 c0 c3 cc cc cc cc 66 0f 1f 84 00 00
00 00 00 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 <48>
8b 46 08 a8 01 75 7a 66 90 48 89 f0 8b 50 34 85 d2 0f 84 8c 00
[   39.343909] RSP: 0018:ffffb95e05e7b6a0 EFLAGS: 00010202
[   39.343911] RAX: 0000000000000000 RBX: 8000000000000025 RCX:
0000000000000000
[   39.343913] RDX: 0000000000000000 RSI: 0000000000000000 RDI:
ffff9efa10e400b8
[   39.343913] RBP: ffffb95e05e7b6e0 R08: 0000000000000000 R09:
0000000000000000
[   39.343914] R10: 0000000000000000 R11: 0000000000000000 R12:
ffff9efa10e400b8
[   39.343915] R13: ffffb95e0e055000 R14: 00007fa3d6119000 R15:
0000000000000000
[   39.343916] FS:  00007fa3f4bf96c0(0000) GS:ffff9f050d780000(0000)
knlGS:0000000000000000
[   39.343917] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   39.343918] CR2: 0000000000000008 CR3: 0000000137bba000 CR4:
0000000000750ef0
[   39.343919] PKRU: 55555554
[   39.343920] note: viewfinderbin-q[3119] exited with irqs disabled

This is the patch, it should have contained all your changes

From b0a61b6b7500635928166516d5563e6de50300bb Mon Sep 17 00:00:00 2001
From: Bin Du <[email protected]>
Date: Mon, 28 Jul 2025 14:18:04 +0800
Subject: [PATCH] add comment from community

Change-Id: I06fff629c0dc691eaeaa9d9b81e6af27cbb6adb2

---
 drivers/media/platform/amd/isp4/isp4_video.c | 59 ++++++--------------
 1 file changed, 17 insertions(+), 42 deletions(-)
drivers/media/platform/amd/isp4/isp4_video.c +1 -1
index a1376498c246..b210a0b1e49b 100644
@@ -183,7 +183,7 @@ static int isp4vid_vb2_mmap(void *buf_priv, struct
 vm_area_struct *vma)
 	vm_flags_set(vma, VM_DONTEXPAND);
 
 	dev_dbg(buf->dev, "mmap isp user bo 0x%llx size %ld refcount %d\n",
-		buf->gpu_addr, buf->size, buf->refcount.refs.counter);
+		buf->gpu_addr, buf->size, refcount_read(&buf->refcount));
 
 	return 0;

}

drivers/media/platform/amd/isp4/isp4_video.c +12 -13
@@ -214,14 +214,9 @@ static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
 		return;
 	}
 
-	struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);
-
 	dev_dbg(buf->dev, "detach dmabuf of isp user bo 0x%llx size %ld",
 		buf->gpu_addr, buf->size);
 
-	if (buf->vaddr)
-		dma_buf_vunmap_unlocked(buf->dbuf, &map);
-
 	kfree(buf);
 }
 
@@ -470,21 +465,25 @@ static struct dma_buf *isp4vid_vb2_get_dmabuf(struct
 vb2_buffer *vb,
 					      unsigned long flags)
 {
 	struct isp4vid_vb2_buf *buf = buf_priv;
-	struct dma_buf *dbuf;
+	struct dma_buf *dbuf = buf->dbuf;
+
+	if (dbuf) {
+		dev_dbg(buf->dev, "dbuf already created, reuse %s dbuf\n",
+			buf->is_expbuf ? "exported" : "implicit");
+		get_dma_buf(dbuf);
 
-	if (buf->dbuf) {
-		dev_dbg(buf->dev, "dbuf already created, reuse implicit dbuf\n");
-		dbuf = buf->dbuf;
 	} else {
 		dbuf = isp4vid_get_dmabuf(vb, buf_priv, flags);
+		if (!dbuf)
+			return NULL;
+
 		dev_dbg(buf->dev, "created new dbuf\n");
+		buf->is_expbuf = true;
+		refcount_inc(&buf->refcount);
 	}
 
-	buf->is_expbuf = true;
-	refcount_inc(&buf->refcount);
-
 	dev_dbg(buf->dev, "buf exported, refcount %d\n",
-		buf->refcount.refs.counter);
+		refcount_read(&buf->refcount));
 
 	return dbuf;

}

drivers/media/platform/amd/isp4/isp4_video.c +4 -3
@@ -579,8 +578,9 @@ static void isp4vid_vb2_put(void *buf_priv)
 {
 	struct isp4vid_vb2_buf *buf = (struct isp4vid_vb2_buf *)buf_priv;
 	unsigned int ref_cnt = refcount_read(&buf->refcount);
+	struct device *dev = buf->dev;
 
-	dev_dbg(buf->dev,
+	dev_dbg(dev,
 		"release isp user bo 0x%llx size %ld refcount %u is_expbuf %d\n",
 		buf->gpu_addr, buf->size,
 		ref_cnt, buf->is_expbuf);
@@ -598,7 +598,7 @@ static void isp4vid_vb2_put(void *buf_priv)
 		/* the ref_cnt - 1 is just a predicted value as reference, can't
 		 * guarantee it's the actual value now
 		 */
-		dev_warn(buf->dev, "ignore buffer free, refcount %u > 0\n", ref_cnt - 1);
+		dev_warn(dev, "ignore buffer free, refcount %u > 0\n", ref_cnt - 1);
 	}
 }
 
@@ -642,7 +642,7 @@ static void *isp4vid_vb2_alloc(struct vb2_buffer *vb,
 struct device *dev,
 	refcount_set(&buf->refcount, 1);
 
 	dev_dbg(dev, "allocated isp user bo 0x%llx size %ld refcount %d\n",
-		buf->gpu_addr, buf->size, buf->refcount.refs.counter);
+		buf->gpu_addr, buf->size, refcount_read(&buf->refcount));
 
 	return buf;

err_free:

drivers/media/platform/amd/isp4/isp4_video.c +0 -24
@@ -1140,30 +1140,6 @@ static void isp4vid_qops_buffer_queue(struct
 vb2_buffer *vb)
 	mutex_unlock(&isp_vdev->buf_list_lock);
 }
 
-static void isp4vid_qops_buffer_cleanup(struct vb2_buffer *vb)
-{
-	struct isp4vid_dev *isp_vdev = vb2_get_drv_priv(vb->vb2_queue);
-	struct isp4vid_vb2_buf *buf = vb->planes[0].mem_priv;
-
-	dev_dbg(isp_vdev->dev, "%s|index=%u vb->memory %u",
-		isp_vdev->vdev.name, vb->index, vb->memory);
-
-	if (!buf) {
-		dev_err(isp_vdev->dev, "Invalid buf handle");
-		return;
-	}
-
-	// release implicit dmabuf reference here for vb2 buffer
-	// of type MMAP and is exported
-	if (vb->memory == VB2_MEMORY_MMAP && buf->is_expbuf) {
-		dma_buf_put(buf->dbuf);
-		dev_dbg(isp_vdev->dev,
-			"put dmabuf for vb->memory %d expbuf %d",
-			vb->memory,
-			buf->is_expbuf);
-	}
-}
-
 static int isp4vid_qops_start_streaming(struct vb2_queue *vq,
 					unsigned int count)

{

drivers/media/platform/amd/isp4/isp4_video.c +0 -1
@@ -1343,7 +1319,6 @@ static int isp4vid_fill_buffer_size(struct isp4vid_dev
 *isp_vdev)
 
 static const struct vb2_ops isp4vid_qops = {
 	.queue_setup = isp4vid_qops_queue_setup,
-	.buf_cleanup = isp4vid_qops_buffer_cleanup,
 	.buf_queue = isp4vid_qops_buffer_queue,
 	.start_streaming = isp4vid_qops_start_streaming,
Quoted 3 lines

.stop_streaming = isp4vid_qops_stop_streaming,

Signature · 1 line
2.34.1

The patch looks correct. I will get back to you on this after doing some testing myself.

Quoted 4 lines

FYI, I can only test these changes with the old isp4 driver right now, since the new isp4 driver doesn't work for me as mentioned in my other email. So far, something does seem wrong after these changes I suggested because they break the camera on the old isp4 driver too.

Following up on this: there were several more issues hiding in the driver that needed to be fixed unfortunately. I ended up making a lot of changes to fix all of the buffer lifetime issues, and I created individual commits for this work because it was getting hard to keep track of all the changes.

I think I'll have this completed tomorrow and will provide a link to my tree with my commits. Hopefully your internal branch hasn't diverged too much from this patchset submission, otherwise there may be some conflicts to resolve.

Sultan

author maintainer
#58 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Thanks Sultan.

Quoted 483 lines · Sultan Alsawaf
Quoted 472 lines · Sultan Alsawaf
Quoted 464 lines · Bin

Hi Sultan, really appreciate your time and effort

Quoted 31 lines · Sultan Alsawaf

I found more refcounting issues in addition to the ones from my other emails while trying to make my webcam work:

Quoted 13 lines · Bin Du
(quoted diff) +13 -0
+static int isp4vid_vb2_mmap(void *buf_priv, struct vm_area_struct *vma)
+{
+	struct isp4vid_vb2_buf *buf = buf_priv;
+	int ret;
+
+	if (!buf) {
+		pr_err("fail no memory to map\n");
+		return -EINVAL;
+	}
+
+	ret = remap_vmalloc_range(vma, buf->vaddr, 0);
+	if (ret) {
+		dev_err(buf->dev, "fail remap vmalloc mem, %d\n", ret);

Review

Quoted 10 lines
(quoted diff) +10 -0
+		return ret;
+	}
+
+	/*
+	 * Make sure that vm_areas for 2 buffers won't be merged together
+	 */
+	vm_flags_set(vma, VM_DONTEXPAND);
+
+	dev_dbg(buf->dev, "mmap isp user bo 0x%llx size %ld refcount %d\n",
+		buf->gpu_addr, buf->size, buf->refcount.refs.counter);

Use refcount_read() instead of reading the refcount's atomic_t counter directly. This is done in 3 other places; change those to refcount_read() as well.

Quoted 6 lines

This didn't cause any functional problems, but it should still be fixed.

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+	return 0;
+}

Sure, will fix it

Quoted 31 lines

[snip]

Quoted 12 lines
(quoted diff) +12 -0
+static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
+{
+	struct isp4vid_vb2_buf *buf = mem_priv;
+
+	if (!buf) {
+		pr_err("fail invalid buf handle\n");
+		return;
+	}
+
+	struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);
+
+	dev_dbg(buf->dev, "detach dmabuf of isp user bo 0x%llx size %ld",

Review

Quoted 10 lines
(quoted diff) +10 -0
+		buf->gpu_addr, buf->size);
+
+	if (buf->vaddr)
+		dma_buf_vunmap_unlocked(buf->dbuf, &map);
+
+	// put dmabuf for exported ones
+	dma_buf_put(buf->dbuf);
+
+	kfree(buf);
+}

As mentioned in the other email, the dma_buf_put() here needs to be removed. But that's not all: the dma_buf_vunmap_unlocked() needs to be removed too because vb2 will always unmap the buffer before detaching it. As a result, having the dma_buf_vunmap_unlocked() call here results in a use-after-free when vb2 calls the unmap_dmabuf memop.

Quoted 9 lines

Change this function to the following:

	static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
	{
		struct isp4vid_vb2_buf *buf = mem_priv;

		kfree(buf);
	}

Will have a try

Quoted 20 lines
Quoted 20 lines
(quoted diff) +20 -0
+static void isp4vid_qops_buffer_cleanup(struct vb2_buffer *vb)
+{
+	struct isp4vid_dev *isp_vdev = vb2_get_drv_priv(vb->vb2_queue);
+	struct isp4vid_vb2_buf *buf = vb->planes[0].mem_priv;
+
+	dev_dbg(isp_vdev->dev, "%s|index=%u vb->memory %u",
+		isp_vdev->vdev.name, vb->index, vb->memory);
+
+	if (!buf) {
+		dev_err(isp_vdev->dev, "Invalid buf handle");
+		return;
+	}
+
+	// release implicit dmabuf reference here for vb2 buffer
+	// of type MMAP and is exported
+	if (vb->memory == VB2_MEMORY_MMAP && buf->is_expbuf) {
+		dma_buf_put(buf->dbuf);
+		dev_dbg(isp_vdev->dev,
+			"put dmabuf for vb->memory %d expbuf %d",
+			vb->memory,
Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+			buf->is_expbuf);
+	}
+}
+

Remove the isp4vid_qops_buffer_cleanup() function. It causes a use-after-free by doing an extra dma_buf_put(). This function isn't needed now that the refcount issues are solved.

Will have a try

Quoted 8 lines

[snip]

Review

Quoted 3 lines
(quoted diff) +3 -0
+static const struct vb2_ops isp4vid_qops = {
+	.queue_setup = isp4vid_qops_queue_setup,
+	.buf_cleanup = isp4vid_qops_buffer_cleanup,

Remove the .buf_cleanup hook too.

Will have a try

Quoted 9 lines
Quoted 6 lines
(quoted diff) +6 -0
+	.buf_queue = isp4vid_qops_buffer_queue,
+	.start_streaming = isp4vid_qops_start_streaming,
+	.stop_streaming = isp4vid_qops_stop_streaming,
+	.wait_prepare = vb2_ops_wait_prepare,
+	.wait_finish = vb2_ops_wait_finish,
+};

[snip]

Quoted 4 lines

Along with the changes from my other emails, I believe this finally fixes all of the refcounting issues. No more UaF or leaks here. :-)

Sultan

Tried changes in this and other two mails together, found the kernel will crash when re-open cheese App (means open cheese, close it and then open it again), the crash stack dump is shown below

[   38.008686] amdgpu 0000:c3:00.0: amdgpu: power on isp suc
[   38.032693] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 suc, reg_val 800
[   38.060866] amdgpu 0000:c3:00.0: amdgpu: set iclk 788 suc, reg_val 792
[   38.060877] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 iclk 788 to
performance state 1 suc
[   38.425284] amdgpu 0000:c3:00.0: amdgpu: isp set performance do none to
state 0
[   38.448631] amdgpu 0000:c3:00.0: amdgpu: power off isp suc
[   38.508368] amdgpu 0000:c3:00.0: amdgpu: power on isp suc
[   38.538525] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 suc, reg_val 800
[   38.568839] amdgpu 0000:c3:00.0: amdgpu: set iclk 788 suc, reg_val 792
[   38.568851] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 iclk 788 to
performance state 1 suc
[   38.997779] amdgpu 0000:c3:00.0: amdgpu: isp set performance do none to
state 0
[   39.029297] amdgpu 0000:c3:00.0: amdgpu: power off isp suc
[   39.084010] BUG: kernel NULL pointer dereference, address:
0000000000000008
[   39.084024] #PF: supervisor read access in kernel mode
[   39.084028] #PF: error_code(0x0000) - not-present page
[   39.084031] PGD 0 P4D 0
[   39.084037] Oops: Oops: 0000 [#1] PREEMPT SMP NOPTI
[   39.084042] CPU: 3 UID: 1000 PID: 3119 Comm: viewfinderbin-q Not tainted
6.14.0-rc2+ #944
[   39.084046] Hardware name: HP HP ZBook Ultra G1a 14 inch Mobile
Workstation PC/8D01, BIOS X89 Ver. 01.03.00 04/25/2025
[   39.084049] RIP: 0010:validate_page_before_insert+0x5/0xc0
[   39.084060] Code: 31 c9 31 ff 45 31 c0 c3 cc cc cc cc 66 0f 1f 84 00 00
00 00 00 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 <48>
8b 46 08 a8 01 75 7a 66 90 48 89 f0 8b 50 34 85 d2 0f 84 8c 00
[   39.084064] RSP: 0018:ffffb95e05e7b6a0 EFLAGS: 00010202
[   39.084067] RAX: 0000000000000000 RBX: 8000000000000025 RCX:
0000000000000000
[   39.084069] RDX: 0000000000000000 RSI: 0000000000000000 RDI:
ffff9efa10e400b8
[   39.084071] RBP: ffffb95e05e7b6e0 R08: 0000000000000000 R09:
0000000000000000
[   39.084072] R10: 0000000000000000 R11: 0000000000000000 R12:
ffff9efa10e400b8
[   39.084074] R13: ffffb95e0e055000 R14: 00007fa3d6119000 R15:
0000000000000000
[   39.084076] FS:  00007fa3f4bf96c0(0000) GS:ffff9f050d780000(0000)
knlGS:0000000000000000
[   39.084078] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   39.084080] CR2: 0000000000000008 CR3: 0000000137bba000 CR4:
0000000000750ef0
[   39.084083] PKRU: 55555554
[   39.084084] Call Trace:
[   39.084087]  <TASK>
[   39.084093]  ? show_regs+0x71/0x90
[   39.084099]  ? __die+0x25/0x80
[   39.084102]  ? page_fault_oops+0x15c/0x570
[   39.084108]  ? do_user_addr_fault+0x4a9/0x810
[   39.084111]  ? __mod_memcg_lruvec_state+0xde/0x280
[   39.084117]  ? exc_page_fault+0x83/0x1d0
[   39.084123]  ? asm_exc_page_fault+0x27/0x30
[   39.084131]  ? validate_page_before_insert+0x5/0xc0
[   39.084134]  ? vm_insert_page+0xd4/0x1a0
[   39.084139]  remap_vmalloc_range_partial+0xe3/0x160
[   39.084145]  remap_vmalloc_range+0x21/0x40
[   39.084153]  isp4vid_vb2_mmap+0x2d/0x130 [amd_capture]
[   39.084164]  isp4vid_vb2_dmabuf_ops_mmap+0x12/0x20 [amd_capture]
[   39.084168]  dma_buf_mmap_internal+0x41/0x70
[   39.084173]  __mmap_region+0x62f/0xbc0
[   39.084179]  mmap_region+0x67/0xe0
[   39.084181]  do_mmap+0x52b/0x650
[   39.084187]  vm_mmap_pgoff+0xf4/0x1c0
[   39.084194]  ksys_mmap_pgoff+0x182/0x250
[   39.084198]  __x64_sys_mmap+0x33/0x70
[   39.084202]  x64_sys_call+0x2541/0x26f0
[   39.084206]  do_syscall_64+0x70/0x130
[   39.084212]  ? __rseq_handle_notify_resume+0xa4/0x520
[   39.084216]  ? do_futex+0x17f/0x220
[   39.084220]  ? restore_fpregs_from_fpstate+0x3d/0xd0
[   39.084225]  ? switch_fpu_return+0x50/0xe0
[   39.084229]  ? syscall_exit_to_user_mode+0x18c/0x1c0
[   39.084234]  ? do_syscall_64+0x7c/0x130
[   39.084237]  ? __pfx_futex_wake_mark+0x10/0x10
[   39.084241]  ? hrtimer_cancel+0x15/0x50
[   39.084246]  ? futex_wait+0x7c/0x120
[   39.084249]  ? __pfx_hrtimer_wakeup+0x10/0x10
[   39.084254]  ? __rseq_handle_notify_resume+0xa4/0x520
[   39.084256]  ? do_futex+0x17f/0x220
[   39.084258]  ? restore_fpregs_from_fpstate+0x3d/0xd0
[   39.084261]  ? switch_fpu_return+0x50/0xe0
[   39.084264]  ? syscall_exit_to_user_mode+0x18c/0x1c0
[   39.084268]  ? do_syscall_64+0x7c/0x130
[   39.084272]  ? __count_memcg_events+0xcd/0x190
[   39.084276]  ? count_memcg_events.constprop.0+0x2a/0x50
[   39.084279]  ? handle_mm_fault+0x1ef/0x2d0
[   39.084284]  ? do_user_addr_fault+0x58d/0x810
[   39.084286]  ? irqentry_exit_to_user_mode+0x33/0x180
[   39.084291]  ? irqentry_exit+0x43/0x50
[   39.084294]  ? exc_page_fault+0x94/0x1d0
[   39.084298]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[   39.084302] RIP: 0033:0x7fa463b2531c
[   39.084305] Code: 1e fa 41 f7 c1 ff 0f 00 00 75 33 55 48 89 e5 41 54 41
89 cc 53 48 89 fb 48 85 ff 74 41 45 89 e2 48 89 df b8 09 00 00 00 0f 05 <48>
3d 00 f0 ff ff 77 7c 5b 41 5c 5d c3 0f 1f 80 00 00 00 00 48 8b
[   39.084308] RSP: 002b:00007fa3f4bf79e0 EFLAGS: 00000246 ORIG_RAX:
0000000000000009
[   39.084311] RAX: ffffffffffffffda RBX: 0000000000000000 RCX:
00007fa463b2531c
[   39.084312] RDX: 0000000000000001 RSI: 00000000007782c0 RDI:
0000000000000000
[   39.084314] RBP: 00007fa3f4bf79f0 R08: 0000000000000047 R09:
0000000000000000
[   39.084315] R10: 0000000000000001 R11: 0000000000000246 R12:
0000000000000001
[   39.084317] R13: 0000000000000000 R14: 00007fa3f8019b48 R15:
00007fa3f4bf7b60
[   39.084320]  </TASK>
[   39.084321] Modules linked in: snd_seq_dummy snd_hrtimer hid_sensor_prox

hid_sensor_gyro_3d hid_sensor_trigger industrialio_triggered_buffer kfifo_buf hid_sensor_iio_common industrialio hid_sensor_hub rfcomm cmac algif_hash algif_skcipher af_alg hid_logitech_hidpp ov05c10 v4l2_cci amd_capture v4l2_fwnode videobuf2_memops videobuf2_v4l2 videobuf2_common v4l2_async videodev pinctrl_amdisp i2c_designware_amdisp qrtr bnep btusb btrtl btintel btbcm btmtk bluetooth hid_logitech_dj intel_rapl_msr amd_atl intel_rapl_common binfmt_misc nls_iso8859_1 snd_acp_legacy_mach snd_acp_mach snd_soc_nau8821 snd_acp3x_rn amdgpu edac_mce_amd snd_ctl_led snd_hda_codec_realtek snd_acp70 snd_hda_codec_generic snd_acp_i2s snd_acp_pdm snd_hda_scodec_component snd_soc_dmic snd_acp_pcm snd_sof_amd_rembrandt snd_sof_amd_renoir snd_sof_amd_acp snd_sof_pci snd_sof_xtensa_dsp snd_hda_codec_hdmi snd_sof snd_sof_utils snd_pci_ps snd_soc_acpi_amd_match snd_amd_sdw_acpi soundwire_amd soundwire_generic_allocation soundwire_bus snd_hda_intel kvm_amd

[   39.084385]  snd_soc_sdca snd_intel_dspcfg snd_intel_sdw_acpi

snd_soc_core snd_hda_codec amdxcp kvm snd_compress drm_panel_backlight_quirks snd_hda_core snd_usb_audio gpu_sched ac97_bus polyval_clmulni snd_pcm_dmaengine polyval_generic ghash_clmulni_intel drm_buddy snd_usbmidi_lib snd_seq_midi snd_rpl_pci_acp6x sha256_ssse3 snd_ump drm_ttm_helper snd_seq_midi_event snd_hwdep sha1_ssse3 ttm aesni_intel drm_exec snd_rawmidi drm_suballoc_helper snd_acp_pci mc crypto_simd snd_acp_legacy_common drm_client_lib cryptd drm_display_helper snd_pci_acp6x snd_seq rapl hp_wmi cec wmi_bmof snd_pcm sparse_keymap snd_seq_device rc_core snd_timer snd_pci_acp5x drm_kms_helper snd_rn_pci_acp3x amd_pmf snd i2c_algo_bit snd_acp_config i2c_piix4 snd_soc_acpi amdtee soundcore snd_pci_acp3x i2c_smbus ccp joydev tee input_leds serial_multi_instantiate amd_pmc platform_profile wireless_hotkey mac_hid serio_raw sch_fq_codel msr parport_pc ppdev lp parport efi_pstore nfnetlink dmi_sysfs ip_tables x_tables autofs4 r8152 mii usbhid hid_multitouch

[   39.084451]  hid_generic nvme ucsi_acpi i2c_hid_acpi video typec_ucsi
i2c_hid amd_sfh thunderbolt hid nvme_core typec wmi drm
[   39.084465] CR2: 0000000000000008
[   39.084468] ---[ end trace 0000000000000000 ]---
[   39.116114] amdgpu 0000:c3:00.0: amdgpu: power on isp suc
[   39.159519] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 suc, reg_val 800
[   39.201325] amdgpu 0000:c3:00.0: amdgpu: set iclk 788 suc, reg_val 792
[   39.201334] amdgpu 0000:c3:00.0: amdgpu: set xclk 788 iclk 788 to
performance state 1 suc
[   39.343894] RIP: 0010:validate_page_before_insert+0x5/0xc0
[   39.343907] Code: 31 c9 31 ff 45 31 c0 c3 cc cc cc cc 66 0f 1f 84 00 00
00 00 00 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 <48>
8b 46 08 a8 01 75 7a 66 90 48 89 f0 8b 50 34 85 d2 0f 84 8c 00
[   39.343909] RSP: 0018:ffffb95e05e7b6a0 EFLAGS: 00010202
[   39.343911] RAX: 0000000000000000 RBX: 8000000000000025 RCX:
0000000000000000
[   39.343913] RDX: 0000000000000000 RSI: 0000000000000000 RDI:
ffff9efa10e400b8
[   39.343913] RBP: ffffb95e05e7b6e0 R08: 0000000000000000 R09:
0000000000000000
[   39.343914] R10: 0000000000000000 R11: 0000000000000000 R12:
ffff9efa10e400b8
[   39.343915] R13: ffffb95e0e055000 R14: 00007fa3d6119000 R15:
0000000000000000
[   39.343916] FS:  00007fa3f4bf96c0(0000) GS:ffff9f050d780000(0000)
knlGS:0000000000000000
[   39.343917] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   39.343918] CR2: 0000000000000008 CR3: 0000000137bba000 CR4:
0000000000750ef0
[   39.343919] PKRU: 55555554
[   39.343920] note: viewfinderbin-q[3119] exited with irqs disabled

This is the patch, it should have contained all your changes

From b0a61b6b7500635928166516d5563e6de50300bb Mon Sep 17 00:00:00 2001
From: Bin Du <[email protected]>
Date: Mon, 28 Jul 2025 14:18:04 +0800
Subject: [PATCH] add comment from community

Change-Id: I06fff629c0dc691eaeaa9d9b81e6af27cbb6adb2

---
  drivers/media/platform/amd/isp4/isp4_video.c | 59 ++++++--------------
  1 file changed, 17 insertions(+), 42 deletions(-)
drivers/media/platform/amd/isp4/isp4_video.c +1 -1
index a1376498c246..b210a0b1e49b 100644
@@ -183,7 +183,7 @@ static int isp4vid_vb2_mmap(void *buf_priv, struct
 vm_area_struct *vma)
  	vm_flags_set(vma, VM_DONTEXPAND);
 
  	dev_dbg(buf->dev, "mmap isp user bo 0x%llx size %ld refcount %d\n",
-		buf->gpu_addr, buf->size, buf->refcount.refs.counter);
+		buf->gpu_addr, buf->size, refcount_read(&buf->refcount));
 
  	return 0;
  }
drivers/media/platform/amd/isp4/isp4_video.c +12 -13
@@ -214,14 +214,9 @@ static void isp4vid_vb2_detach_dmabuf(void *mem_priv)
  		return;
  	}
 
-	struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);
-
  	dev_dbg(buf->dev, "detach dmabuf of isp user bo 0x%llx size %ld",
  		buf->gpu_addr, buf->size);
 
-	if (buf->vaddr)
-		dma_buf_vunmap_unlocked(buf->dbuf, &map);
-
  	kfree(buf);
  }
 
@@ -470,21 +465,25 @@ static struct dma_buf *isp4vid_vb2_get_dmabuf(struct
 vb2_buffer *vb,
  					      unsigned long flags)
  {
  	struct isp4vid_vb2_buf *buf = buf_priv;
-	struct dma_buf *dbuf;
+	struct dma_buf *dbuf = buf->dbuf;
+
+	if (dbuf) {
+		dev_dbg(buf->dev, "dbuf already created, reuse %s dbuf\n",
+			buf->is_expbuf ? "exported" : "implicit");
+		get_dma_buf(dbuf);
 
-	if (buf->dbuf) {
-		dev_dbg(buf->dev, "dbuf already created, reuse implicit dbuf\n");
-		dbuf = buf->dbuf;
  	} else {
  		dbuf = isp4vid_get_dmabuf(vb, buf_priv, flags);
+		if (!dbuf)
+			return NULL;
+
  		dev_dbg(buf->dev, "created new dbuf\n");
+		buf->is_expbuf = true;
+		refcount_inc(&buf->refcount);
  	}
 
-	buf->is_expbuf = true;
-	refcount_inc(&buf->refcount);
-
  	dev_dbg(buf->dev, "buf exported, refcount %d\n",
-		buf->refcount.refs.counter);
+		refcount_read(&buf->refcount));
 
  	return dbuf;
  }
drivers/media/platform/amd/isp4/isp4_video.c +4 -3
@@ -579,8 +578,9 @@ static void isp4vid_vb2_put(void *buf_priv)
  {
  	struct isp4vid_vb2_buf *buf = (struct isp4vid_vb2_buf *)buf_priv;
  	unsigned int ref_cnt = refcount_read(&buf->refcount);
+	struct device *dev = buf->dev;
 
-	dev_dbg(buf->dev,
+	dev_dbg(dev,
  		"release isp user bo 0x%llx size %ld refcount %u is_expbuf %d\n",
  		buf->gpu_addr, buf->size,
  		ref_cnt, buf->is_expbuf);
@@ -598,7 +598,7 @@ static void isp4vid_vb2_put(void *buf_priv)
  		/* the ref_cnt - 1 is just a predicted value as reference, can't
  		 * guarantee it's the actual value now
  		 */
-		dev_warn(buf->dev, "ignore buffer free, refcount %u > 0\n", ref_cnt - 1);
+		dev_warn(dev, "ignore buffer free, refcount %u > 0\n", ref_cnt - 1);
  	}
  }
 
@@ -642,7 +642,7 @@ static void *isp4vid_vb2_alloc(struct vb2_buffer *vb,
 struct device *dev,
  	refcount_set(&buf->refcount, 1);
 
  	dev_dbg(dev, "allocated isp user bo 0x%llx size %ld refcount %d\n",
-		buf->gpu_addr, buf->size, buf->refcount.refs.counter);
+		buf->gpu_addr, buf->size, refcount_read(&buf->refcount));
 
  	return buf;

err_free:

drivers/media/platform/amd/isp4/isp4_video.c +0 -24
@@ -1140,30 +1140,6 @@ static void isp4vid_qops_buffer_queue(struct
 vb2_buffer *vb)
  	mutex_unlock(&isp_vdev->buf_list_lock);
  }
 
-static void isp4vid_qops_buffer_cleanup(struct vb2_buffer *vb)
-{
-	struct isp4vid_dev *isp_vdev = vb2_get_drv_priv(vb->vb2_queue);
-	struct isp4vid_vb2_buf *buf = vb->planes[0].mem_priv;
-
-	dev_dbg(isp_vdev->dev, "%s|index=%u vb->memory %u",
-		isp_vdev->vdev.name, vb->index, vb->memory);
-
-	if (!buf) {
-		dev_err(isp_vdev->dev, "Invalid buf handle");
-		return;
-	}
-
-	// release implicit dmabuf reference here for vb2 buffer
-	// of type MMAP and is exported
-	if (vb->memory == VB2_MEMORY_MMAP && buf->is_expbuf) {
-		dma_buf_put(buf->dbuf);
-		dev_dbg(isp_vdev->dev,
-			"put dmabuf for vb->memory %d expbuf %d",
-			vb->memory,
-			buf->is_expbuf);
-	}
-}
-
  static int isp4vid_qops_start_streaming(struct vb2_queue *vq,
  					unsigned int count)
  {
drivers/media/platform/amd/isp4/isp4_video.c +0 -1
@@ -1343,7 +1319,6 @@ static int isp4vid_fill_buffer_size(struct isp4vid_dev
 *isp_vdev)
 
  static const struct vb2_ops isp4vid_qops = {
  	.queue_setup = isp4vid_qops_queue_setup,
-	.buf_cleanup = isp4vid_qops_buffer_cleanup,
  	.buf_queue = isp4vid_qops_buffer_queue,
  	.start_streaming = isp4vid_qops_start_streaming,
Quoted 3 lines

.stop_streaming = isp4vid_qops_stop_streaming,

Signature · 1 line
2.34.1

The patch looks correct. I will get back to you on this after doing some testing myself.

Quoted 4 lines

FYI, I can only test these changes with the old isp4 driver right now, since the new isp4 driver doesn't work for me as mentioned in my other email. So far, something does seem wrong after these changes I suggested because they break the camera on the old isp4 driver too.

Following up on this: there were several more issues hiding in the driver that needed to be fixed unfortunately. I ended up making a lot of changes to fix all of the buffer lifetime issues, and I created individual commits for this work because it was getting hard to keep track of all the changes.

Quoted 5 lines

I think I'll have this completed tomorrow and will provide a link to my tree with my commits. Hopefully your internal branch hasn't diverged too much from this patchset submission, otherwise there may be some conflicts to resolve.

Sultan

Really appreciate your time and effort. Yes, big code changes happen in our internal branch to address upstream comments, but i believe your commits will be extremely valuable. We can check and test after fixing possbile conflict.

Regards,
Bin

#59 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Quoted 11 lines · Bin
Quoted 11 lines
Quoted 1 line

[ old quotes trimmed out ]

Following up on this: there were several more issues hiding in the driver that needed to be fixed unfortunately. I ended up making a lot of changes to fix all of the buffer lifetime issues, and I created individual commits for this work because it was getting hard to keep track of all the changes.

I think I'll have this completed tomorrow and will provide a link to my tree with my commits. Hopefully your internal branch hasn't diverged too much from this patchset submission, otherwise there may be some conflicts to resolve.

Quoted 6 lines
Quoted 1 line

Sultan

Really appreciate your time and effort. Yes, big code changes happen in our internal branch to address upstream comments, but i believe your commits will be extremely valuable. We can check and test after fixing possbile conflict.

I've pushed my work here:

https://github.com/kerneltoast/kernel_x86_laptop.git v6.16-drm-tip-isp4-for-amd

Please see my changes since commit 48d18b6e58c6dadbc79374773736924d2f532da5 on that branch, up to commit 5cdacf4ff108d83869414a00465e7a612bcd04b1.

There are 29 patches:

Sultan Alsawaf (29):
media: amd: isp4: Use amdgpu helper functions for ISP buffers
media: amd: isp4: Remove -mpreferred-stack-boundary=4 cflag
media: amd: isp4: Remove -DCONFIG_COMPAT from ccflags-y
media: amd: isp4: Remove the myriad of redundant ccflags includes
media: amd: isp4: Pass warning flags through cc-option
media: amd: isp4: Clean up amd_capture-objs formatting
media: amd: isp4: Don't set CONFIG_AMD_ISP4 to y by default
media: amd: isp4: Clean up AMD_ISP4 Kconfig dependencies
media: amd: Fix Kconfig/Makefile directory structure
media: amd: isp4: Remove superfluous NULL pointer checks
media: amd: isp4: Remove superfluous void pointer casts
media: amd: isp4: Remove superfluous memset in isp4vid_vb2_map_dmabuf()
media: amd: isp4: Don't read refcount counter directly
media: amd: isp4: Add missing refcount tracking to mmap memop
media: amd: isp4: Don't put or unmap the dmabuf when detaching
media: amd: isp4: Don't increment refcount when dmabuf export fails
media: amd: isp4: Fix possible use-after-free in isp4vid_vb2_put()
media: amd: isp4: Always export a new dmabuf from get_dmabuf memop
media: amd: isp4: Fix implicit dmabuf lifetime tracking
media: amd: isp4: Fix possible use-after-free when putting implicit dmabuf
media: amd: isp4: Remove 'refcount > 0' warning in isp4vid_vb2_put()
media: amd: isp4: Fix comment in isp4vid_vb2_dmabuf_ops_release()
media: amd: isp4: Simplify isp4vid_get_dmabuf() arguments
media: amd: isp4: Add comment to CONFIG_HAS_DMA #endif
media: amd: isp4: Move up buf->vaddr check in isp4vid_get_dmabuf()
media: amd: isp4: Remove unused userptr memops
media: amd: isp4: Make isp4vid_vb2_memops static
media: amd: isp4: Add missing cleanup on error in isp4vid_vb2_alloc()
media: amd: isp4: Release queued buffers on error in start_streaming

 drivers/media/platform/amd/Kconfig               |  18 +-
 drivers/media/platform/amd/Makefile              |   6 +-
 drivers/media/platform/amd/isp4/Kconfig          |  14 ++
 drivers/media/platform/amd/isp4/Makefile         |  42 +---
 drivers/media/platform/amd/isp4/isp4.c           |   9 +-
 drivers/media/platform/amd/isp4/isp4.h           |  11 -
 drivers/media/platform/amd/isp4/isp4_debug.c     |   4 +-
 drivers/media/platform/amd/isp4/isp4_interface.c |  46 +---
 drivers/media/platform/amd/isp4/isp4_interface.h |   6 +-
 drivers/media/platform/amd/isp4/isp4_subdev.c    |   8 +-
 drivers/media/platform/amd/isp4/isp4_subdev.h    |   4 +-
 drivers/media/platform/amd/isp4/isp4_video.c     | 297 ++++++-----------------
 drivers/media/platform/amd/isp4/isp4_video.h     |   4 +-
 13 files changed, 122 insertions(+), 347 deletions(-)

At least 1 of them should be redundant for your internal branch (9f394a7af2cf "media: amd: isp4: Use amdgpu helper functions for ISP buffers").

Cheers,
Sultan

author maintainer
#60 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Many thanks, Sultan.

Quoted 74 lines · Sultan Alsawaf
Quoted 11 lines · Bin
Quoted 11 lines
Quoted 1 line

[ old quotes trimmed out ]

Following up on this: there were several more issues hiding in the driver that needed to be fixed unfortunately. I ended up making a lot of changes to fix all of the buffer lifetime issues, and I created individual commits for this work because it was getting hard to keep track of all the changes.

I think I'll have this completed tomorrow and will provide a link to my tree with my commits. Hopefully your internal branch hasn't diverged too much from this patchset submission, otherwise there may be some conflicts to resolve.

Quoted 6 lines
Quoted 1 line

Sultan

Really appreciate your time and effort. Yes, big code changes happen in our internal branch to address upstream comments, but i believe your commits will be extremely valuable. We can check and test after fixing possbile conflict.

I've pushed my work here:

   https://github.com/kerneltoast/kernel_x86_laptop.git v6.16-drm-tip-isp4-for-amd

Please see my changes since commit 48d18b6e58c6dadbc79374773736924d2f532da5 on that branch, up to commit 5cdacf4ff108d83869414a00465e7a612bcd04b1.

There are 29 patches:

Sultan Alsawaf (29):
media: amd: isp4: Use amdgpu helper functions for ISP buffers
media: amd: isp4: Remove -mpreferred-stack-boundary=4 cflag
media: amd: isp4: Remove -DCONFIG_COMPAT from ccflags-y
media: amd: isp4: Remove the myriad of redundant ccflags includes
media: amd: isp4: Pass warning flags through cc-option
media: amd: isp4: Clean up amd_capture-objs formatting
media: amd: isp4: Don't set CONFIG_AMD_ISP4 to y by default
media: amd: isp4: Clean up AMD_ISP4 Kconfig dependencies
media: amd: Fix Kconfig/Makefile directory structure
media: amd: isp4: Remove superfluous NULL pointer checks
media: amd: isp4: Remove superfluous void pointer casts
media: amd: isp4: Remove superfluous memset in isp4vid_vb2_map_dmabuf()
media: amd: isp4: Don't read refcount counter directly
media: amd: isp4: Add missing refcount tracking to mmap memop
media: amd: isp4: Don't put or unmap the dmabuf when detaching
media: amd: isp4: Don't increment refcount when dmabuf export fails
media: amd: isp4: Fix possible use-after-free in isp4vid_vb2_put()
media: amd: isp4: Always export a new dmabuf from get_dmabuf memop
media: amd: isp4: Fix implicit dmabuf lifetime tracking
media: amd: isp4: Fix possible use-after-free when putting implicit dmabuf
media: amd: isp4: Remove 'refcount > 0' warning in isp4vid_vb2_put()
media: amd: isp4: Fix comment in isp4vid_vb2_dmabuf_ops_release()
media: amd: isp4: Simplify isp4vid_get_dmabuf() arguments
media: amd: isp4: Add comment to CONFIG_HAS_DMA #endif
media: amd: isp4: Move up buf->vaddr check in isp4vid_get_dmabuf()
media: amd: isp4: Remove unused userptr memops
media: amd: isp4: Make isp4vid_vb2_memops static
media: amd: isp4: Add missing cleanup on error in isp4vid_vb2_alloc()
media: amd: isp4: Release queued buffers on error in start_streaming

  drivers/media/platform/amd/Kconfig               |  18 +-
  drivers/media/platform/amd/Makefile              |   6 +-
  drivers/media/platform/amd/isp4/Kconfig          |  14 ++
  drivers/media/platform/amd/isp4/Makefile         |  42 +---
  drivers/media/platform/amd/isp4/isp4.c           |   9 +-
  drivers/media/platform/amd/isp4/isp4.h           |  11 -
  drivers/media/platform/amd/isp4/isp4_debug.c     |   4 +-
  drivers/media/platform/amd/isp4/isp4_interface.c |  46 +---
  drivers/media/platform/amd/isp4/isp4_interface.h |   6 +-
  drivers/media/platform/amd/isp4/isp4_subdev.c    |   8 +-
  drivers/media/platform/amd/isp4/isp4_subdev.h    |   4 +-
  drivers/media/platform/amd/isp4/isp4_video.c     | 297 ++++++-----------------
  drivers/media/platform/amd/isp4/isp4_video.h     |   4 +-
  13 files changed, 122 insertions(+), 347 deletions(-)
Quoted 5 lines

At least 1 of them should be redundant for your internal branch (9f394a7af2cf "media: amd: isp4: Use amdgpu helper functions for ISP buffers").

Cheers,
Sultan

Wow, amazing, fantastic work. Will compare to our latest internal branch to see what might be missing and try to merge, then have some test. Will let you know the result, it might cost some time. Thanks again.

Signature · 2 lines
Regards,
Bin
author maintainer
#61 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Hi Sultan,

Quoted 78 lines · Bin

Many thanks, Sultan.

Quoted 75 lines · Sultan Alsawaf
Quoted 11 lines · Bin
Quoted 11 lines
Quoted 1 line

[ old quotes trimmed out ]

Following up on this: there were several more issues hiding in the driver that needed to be fixed unfortunately. I ended up making a lot of changes to fix all of the buffer lifetime issues, and I created individual commits for this work because it was getting hard to keep track of all the changes.

I think I'll have this completed tomorrow and will provide a link to my tree with my commits. Hopefully your internal branch hasn't diverged too much from this patchset submission, otherwise there may be some conflicts to resolve.

Quoted 6 lines
Quoted 1 line

Sultan

Really appreciate your time and effort. Yes, big code changes happen in our internal branch to address upstream comments, but i believe your commits will be extremely valuable. We can check and test after fixing possbile conflict.

I've pushed my work here:

   https://github.com/kerneltoast/kernel_x86_laptop.git v6.16-drm-tip- isp4-for-amd

Please see my changes since commit 48d18b6e58c6dadbc79374773736924d2f532da5 on that branch, up to commit 5cdacf4ff108d83869414a00465e7a612bcd04b1.

There are 29 patches:

Sultan Alsawaf (29):
       media: amd: isp4: Use amdgpu helper functions for ISP buffers
       media: amd: isp4: Remove -mpreferred-stack-boundary=4 cflag
       media: amd: isp4: Remove -DCONFIG_COMPAT from ccflags-y
       media: amd: isp4: Remove the myriad of redundant ccflags includes
       media: amd: isp4: Pass warning flags through cc-option
       media: amd: isp4: Clean up amd_capture-objs formatting
       media: amd: isp4: Don't set CONFIG_AMD_ISP4 to y by default
       media: amd: isp4: Clean up AMD_ISP4 Kconfig dependencies
       media: amd: Fix Kconfig/Makefile directory structure
       media: amd: isp4: Remove superfluous NULL pointer checks
       media: amd: isp4: Remove superfluous void pointer casts
       media: amd: isp4: Remove superfluous memset in isp4vid_vb2_map_dmabuf()
       media: amd: isp4: Don't read refcount counter directly
       media: amd: isp4: Add missing refcount tracking to mmap memop
       media: amd: isp4: Don't put or unmap the dmabuf when detaching
       media: amd: isp4: Don't increment refcount when dmabuf export fails
       media: amd: isp4: Fix possible use-after-free in isp4vid_vb2_put()
       media: amd: isp4: Always export a new dmabuf from get_dmabuf memop
       media: amd: isp4: Fix implicit dmabuf lifetime tracking
       media: amd: isp4: Fix possible use-after-free when putting implicit dmabuf
       media: amd: isp4: Remove 'refcount > 0' warning in isp4vid_vb2_put()
       media: amd: isp4: Fix comment in isp4vid_vb2_dmabuf_ops_release()
       media: amd: isp4: Simplify isp4vid_get_dmabuf() arguments
       media: amd: isp4: Add comment to CONFIG_HAS_DMA #endif
       media: amd: isp4: Move up buf->vaddr check in isp4vid_get_dmabuf()
       media: amd: isp4: Remove unused userptr memops
       media: amd: isp4: Make isp4vid_vb2_memops static
       media: amd: isp4: Add missing cleanup on error in isp4vid_vb2_alloc()
       media: amd: isp4: Release queued buffers on error in start_streaming

  drivers/media/platform/amd/Kconfig               |  18 +- drivers/media/platform/amd/Makefile              |   6 +- drivers/media/platform/amd/isp4/Kconfig          |  14 ++ drivers/media/platform/amd/isp4/Makefile         |  42 +--- drivers/media/platform/amd/isp4/isp4.c           |   9 +- drivers/media/platform/amd/isp4/isp4.h           |  11 - drivers/media/platform/amd/isp4/isp4_debug.c     |   4 +- drivers/media/platform/amd/isp4/isp4_interface.c |  46 +--- drivers/media/platform/amd/isp4/isp4_interface.h |   6 +- drivers/media/platform/amd/isp4/isp4_subdev.c    |   8 +- drivers/media/platform/amd/isp4/isp4_subdev.h    |   4 +- drivers/media/platform/amd/isp4/isp4_video.c     | 297 +++++

+-----------------
  drivers/media/platform/amd/isp4/isp4_video.h     |   4 +-
  13 files changed, 122 insertions(+), 347 deletions(-)
Quoted 8 lines
Quoted 5 lines

At least 1 of them should be redundant for your internal branch (9f394a7af2cf "media: amd: isp4: Use amdgpu helper functions for ISP buffers").

Cheers,
Sultan

Wow, amazing, fantastic work. Will compare to our latest internal branch to see what might be missing and try to merge, then have some test. Will let you know the result, it might cost some time. Thanks again.

We cherry-picked your buffer related changes and did some quick test, it works well. Will merge them into our internal branch and check other changes later. Really appreciate for your time, effort and good work.

Signature · 2 lines
Regards,
Bin
#62 lore raw

Re: [PATCH v2 6/8] media: platform: amd: isp4 video node and buffers handling added

Quoted 101 lines · Bin
Quoted 100 lines · Bin

Many thanks, Sultan.

Quoted 96 lines · Sultan Alsawaf
Quoted 17 lines · Bin
Quoted 17 lines
Quoted 1 line

[ old quotes trimmed out ]

Following up on this: there were several more issues hiding in the driver that
needed to be fixed unfortunately. I ended up making a lot of changes to fix all
of the buffer lifetime issues, and I created individual commits for this work
because it was getting hard to keep track of all the changes.

I think I'll have this completed tomorrow and will provide a link to my tree
with my commits. Hopefully your internal branch hasn't diverged too much from
this patchset submission, otherwise there may be some conflicts to resolve.

Quoted 7 lines
Quoted 1 line

Sultan

Really appreciate your time and effort. Yes, big code changes happen in our
internal branch to address upstream comments, but i believe your commits will be extremely valuable. We can check and test after fixing possbile conflict.

I've pushed my work here:

   https://github.com/kerneltoast/kernel_x86_laptop.git
v6.16-drm-tip- isp4-for-amd

Please see my changes since commit 48d18b6e58c6dadbc79374773736924d2f532da5 on
that branch, up to commit 5cdacf4ff108d83869414a00465e7a612bcd04b1.

There are 29 patches:

Sultan Alsawaf (29):
       media: amd: isp4: Use amdgpu helper functions for ISP buffers
       media: amd: isp4: Remove -mpreferred-stack-boundary=4 cflag
       media: amd: isp4: Remove -DCONFIG_COMPAT from ccflags-y
       media: amd: isp4: Remove the myriad of redundant ccflags includes
       media: amd: isp4: Pass warning flags through cc-option
       media: amd: isp4: Clean up amd_capture-objs formatting
       media: amd: isp4: Don't set CONFIG_AMD_ISP4 to y by default
       media: amd: isp4: Clean up AMD_ISP4 Kconfig dependencies
       media: amd: Fix Kconfig/Makefile directory structure
       media: amd: isp4: Remove superfluous NULL pointer checks
       media: amd: isp4: Remove superfluous void pointer casts
       media: amd: isp4: Remove superfluous memset in isp4vid_vb2_map_dmabuf()
       media: amd: isp4: Don't read refcount counter directly
       media: amd: isp4: Add missing refcount tracking to mmap memop
       media: amd: isp4: Don't put or unmap the dmabuf when detaching
       media: amd: isp4: Don't increment refcount when dmabuf export fails
       media: amd: isp4: Fix possible use-after-free in isp4vid_vb2_put()
       media: amd: isp4: Always export a new dmabuf from get_dmabuf memop
       media: amd: isp4: Fix implicit dmabuf lifetime tracking
       media: amd: isp4: Fix possible use-after-free when putting implicit dmabuf
       media: amd: isp4: Remove 'refcount > 0' warning in isp4vid_vb2_put()
       media: amd: isp4: Fix comment in isp4vid_vb2_dmabuf_ops_release()
       media: amd: isp4: Simplify isp4vid_get_dmabuf() arguments
       media: amd: isp4: Add comment to CONFIG_HAS_DMA #endif
       media: amd: isp4: Move up buf->vaddr check in isp4vid_get_dmabuf()
       media: amd: isp4: Remove unused userptr memops
       media: amd: isp4: Make isp4vid_vb2_memops static
       media: amd: isp4: Add missing cleanup on error in isp4vid_vb2_alloc()
       media: amd: isp4: Release queued buffers on error in start_streaming

  drivers/media/platform/amd/Kconfig               |  18 +- drivers/media/platform/amd/Makefile              |   6 +- drivers/media/platform/amd/isp4/Kconfig          |  14 ++ drivers/media/platform/amd/isp4/Makefile         |  42 +--- drivers/media/platform/amd/isp4/isp4.c           |   9 +- drivers/media/platform/amd/isp4/isp4.h           |  11 - drivers/media/platform/amd/isp4/isp4_debug.c     |   4 +- drivers/media/platform/amd/isp4/isp4_interface.c |  46 +--- drivers/media/platform/amd/isp4/isp4_interface.h |   6 +- drivers/media/platform/amd/isp4/isp4_subdev.c    |   8 +- drivers/media/platform/amd/isp4/isp4_subdev.h    |   4 +- drivers/media/platform/amd/isp4/isp4_video.c     | 297 +++++

+-----------------
  drivers/media/platform/amd/isp4/isp4_video.h     |   4 +-
  13 files changed, 122 insertions(+), 347 deletions(-)

At least 1 of them should be redundant for your internal branch (9f394a7af2cf
"media: amd: isp4: Use amdgpu helper functions for ISP buffers").

Cheers,
Sultan

Quoted 8 lines
Quoted 4 lines

Wow, amazing, fantastic work. Will compare to our latest internal branch to see what might be missing and try to merge, then have some test. Will let you know the result, it might cost some time. Thanks again.

We cherry-picked your buffer related changes and did some quick test, it works well. Will merge them into our internal branch and check other changes later. Really appreciate for your time, effort and good work.

Glad to hear it! :)

Sultan

author maintainer
#63 lore raw

PATCH v2 7/8 media: platform: amd: isp4 debug fs logging and more descriptive errors

In reply to Bin Du

Add debug fs for isp4 driver and add more detailed descriptive error info to some of the log message

---
 drivers/media/platform/amd/isp4/Makefile      |   1 +
 drivers/media/platform/amd/isp4/isp4.c        |   5 +
 drivers/media/platform/amd/isp4/isp4_debug.c  | 272 ++++++++++++++++++
 drivers/media/platform/amd/isp4/isp4_debug.h  |  41 +++
 .../media/platform/amd/isp4/isp4_interface.c  |  38 ++-
 drivers/media/platform/amd/isp4/isp4_subdev.c |  29 +-
 6 files changed, 366 insertions(+), 20 deletions(-)
 create mode 100644 drivers/media/platform/amd/isp4/isp4_debug.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4_debug.h
drivers/media/platform/amd/isp4/Makefile +1 -0
index 99285fe45b52..99d16ecfb64b 100644
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_AMD_ISP4) += amd_capture.o
 amd_capture-objs := isp4_subdev.o \
+			isp4_debug.o \
 			isp4_phy.o \
 			isp4_interface.o \
 			isp4.o	\
drivers/media/platform/amd/isp4/isp4.c +5 -0
index 3d971fe2813d..c3d2e52e0201 100644
@@ -11,6 +11,7 @@
 #include "amdgpu_object.h"
 
 #include "isp4.h"
+#include "isp4_debug.h"
 #include "isp4_hw.h"
 
 #define ISP4_DRV_NAME "amd_isp_capture"
@@ -352,6 +353,8 @@ static int isp4_capture_probe(struct platform_device *pdev)
 	pm_runtime_set_suspended(dev);
 	pm_runtime_enable(dev);
 
+	isp_debugfs_create(isp_dev);
+
 	return 0;
 
 err_unreg_video_dev_notifier:
@@ -369,6 +372,8 @@ static void isp4_capture_remove(struct platform_device *pdev)
 {
 	struct isp4_device *isp_dev = platform_get_drvdata(pdev);
 
+	isp_debugfs_remove(isp_dev);
+
 	v4l2_async_nf_unregister(&isp_dev->notifier);
 	v4l2_async_nf_cleanup(&isp_dev->notifier);
 	v4l2_device_unregister_subdev(&isp_dev->isp_sdev.sdev);
drivers/media/platform/amd/isp4/isp4_debug.c new file +272 -0

272 lines not on this page · raw message

drivers/media/platform/amd/isp4/isp4_debug.h new file +41 -0

41 lines not on this page · raw message

drivers/media/platform/amd/isp4/isp4_interface.c +25 -13
index 0e1eb22a0de5..b79b0f2a4776 100644
@@ -7,6 +7,7 @@
 
 #include "amdgpu_object.h"
 
+#include "isp4_debug.h"
 #include "isp4_fw_cmd_resp.h"
 #include "isp4_hw.h"
 #include "isp4_hw_reg.h"
@@ -392,7 +393,8 @@ static int isp4if_insert_isp_fw_cmd(struct isp4_interface *ispif,
 	len = rb_config->val_size;
 
 	if (isp4if_is_cmdq_rb_full(ispif, stream)) {
-		dev_err(dev, "fail no cmdslot (%d)\n", stream);
+		dev_err(dev, "fail no cmdslot %s(%d)\n",
+			isp4dbg_get_if_stream_str(stream), stream);
 		return -EINVAL;
 	}
 
@@ -400,13 +402,15 @@ static int isp4if_insert_isp_fw_cmd(struct isp4_interface *ispif,
 	rd_ptr = isp4hw_rreg(ispif->mmio, rreg);
 
 	if (rd_ptr > len) {
-		dev_err(dev, "fail (%u),rd_ptr %u(should<=%u),wr_ptr %u\n",
+		dev_err(dev, "fail %s(%u),rd_ptr %u(should<=%u),wr_ptr %u\n",
+			isp4dbg_get_if_stream_str(stream),
 			stream, rd_ptr, len, wr_ptr);
 		return -EINVAL;
 	}
 
 	if (wr_ptr > len) {
-		dev_err(dev, "fail (%u),wr_ptr %u(should<=%u), rd_ptr %u\n",
+		dev_err(dev, "fail %s(%u),wr_ptr %u(should<=%u), rd_ptr %u\n",
+			isp4dbg_get_if_stream_str(stream),
 			stream, wr_ptr, len, rd_ptr);
 		return -EINVAL;
 	}
@@ -498,7 +502,9 @@ static int isp4if_send_fw_cmd(struct isp4_interface *ispif,
 			}
 			rd_ptr = isp4hw_rreg(ispif->mmio, rreg);
 			wr_ptr = isp4hw_rreg(ispif->mmio, wreg);
-			dev_err(dev, "fail to get cmdq slot,stream (%d), rd %u, wr %u\n",
+			dev_err(dev,
+				"failed to get free cmdq slot, stream %s(%d),rd %u, wr %u\n",
+				isp4dbg_get_if_stream_str(stream),
 				stream, rd_ptr, wr_ptr);
 			return -ETIMEDOUT;
 		}
@@ -550,8 +556,8 @@ static int isp4if_send_fw_cmd(struct isp4_interface *ispif,
 
 	ret = isp4if_insert_isp_fw_cmd(ispif, stream, &cmd);
 	if (ret) {
-		dev_err(dev, "fail for insert_isp_fw_cmd camId (0x%08x)\n",
-			cmd_id);
+		dev_err(dev, "fail for insert_isp_fw_cmd camId %s(0x%08x)\n",
+			isp4dbg_get_cmd_str(cmd_id), cmd_id);
 		if (cmd_ele) {
 			isp4if_rm_cmd_from_cmdq(ispif, cmd_ele->seq_num,
 						cmd_ele->cmd_id);
@@ -780,13 +786,15 @@ int isp4if_f2h_resp(struct isp4_interface *ispif,
 	wr_ptr_dbg = wr_ptr;
 
 	if (rd_ptr > len) {
-		dev_err(dev, "fail (%u),rd_ptr %u(should<=%u),wr_ptr %u\n",
+		dev_err(dev, "fail %s(%u),rd_ptr %u(should<=%u),wr_ptr %u\n",
+			isp4dbg_get_if_stream_str(stream),
 			stream, rd_ptr, len, wr_ptr);
 		return -EINVAL;
 	}
 
 	if (wr_ptr > len) {
-		dev_err(dev, "fail (%u),wr_ptr %u(should<=%u), rd_ptr %u\n",
+		dev_err(dev, "fail %s(%u),wr_ptr %u(should<=%u), rd_ptr %u\n",
+			isp4dbg_get_if_stream_str(stream),
 			stream, wr_ptr, len, rd_ptr);
 		return -EINVAL;
 	}
@@ -801,7 +809,8 @@ int isp4if_f2h_resp(struct isp4_interface *ispif,
 				isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif),
 					    rreg, rd_ptr);
 			} else {
-				dev_err(dev, "(%u),rd %u(should<=%u),wr %u\n",
+				dev_err(dev, "%s(%u),rd %u(should<=%u),wr %u\n",
+					isp4dbg_get_if_stream_str(stream),
 					stream, rd_ptr, len, wr_ptr);
 				return -EINVAL;
 			}
@@ -827,7 +836,8 @@ int isp4if_f2h_resp(struct isp4_interface *ispif,
 				isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif),
 					    rreg, rd_ptr);
 			} else {
-				dev_err(dev, "(%u),rd %u(should<=%u),wr %u\n",
+				dev_err(dev, "%s(%u),rd %u(should<=%u),wr %u\n",
+					isp4dbg_get_if_stream_str(stream),
 					stream, rd_ptr, len, wr_ptr);
 				return -EINVAL;
 			}
@@ -849,7 +859,8 @@ int isp4if_f2h_resp(struct isp4_interface *ispif,
 				isp4hw_wreg(GET_ISP4IF_REG_BASE(ispif),
 					    rreg, rd_ptr);
 			} else {
-				dev_err(dev, "(%u),rd %u(should<=%u),wr %u\n",
+				dev_err(dev, "%s(%u),rd %u(should<=%u),wr %u\n",
+					isp4dbg_get_if_stream_str(stream),
 					stream, rd_ptr, len, wr_ptr);
 				return -EINVAL;
 			}
@@ -866,9 +877,10 @@ int isp4if_f2h_resp(struct isp4_interface *ispif,
 			checksum, response->resp_check_sum,
 			rd_ptr_dbg, wr_ptr_dbg);
 
-		dev_err(dev, "(%u), seqNo %u, resp_id (0x%x)\n",
-			stream,
+		dev_err(dev, "%s(%u), seqNo %u, resp_id %s(0x%x)\n",
+			isp4dbg_get_if_stream_str(stream), stream,
 			response->resp_seq_num,
+			isp4dbg_get_resp_str(response->resp_id),
 			response->resp_id);
 
 		return -EINVAL;
drivers/media/platform/amd/isp4/isp4_subdev.c +22 -7
index d93cdb9dc477..e2084030c79d 100644
@@ -7,6 +7,7 @@
 #include <linux/pm_domain.h>
 #include <linux/pm_runtime.h>
 
+#include "isp4_debug.h"
 #include "isp4_fw_cmd_resp.h"
 #include "isp4_hw.h"
 #include "isp4_interface.h"
@@ -305,7 +306,9 @@ static int isp4sd_setup_output(struct isp4_subdev *isp_subdev,
 		return -EINVAL;
 	}
 
-	dev_dbg(dev, "channel: w:h=%u:%u,lp:%u,cp%u\n",
+	dev_dbg(dev, "channel:%s,fmt %s,w:h=%u:%u,lp:%u,cp%u\n",
+		isp4dbg_get_out_ch_str(cmd_ch_prop.ch),
+		isp4dbg_get_img_fmt_str(cmd_ch_prop.image_prop.image_format),
 		cmd_ch_prop.image_prop.width, cmd_ch_prop.image_prop.height,
 		cmd_ch_prop.image_prop.luma_pitch,
 		cmd_ch_prop.image_prop.chroma_pitch);
@@ -328,6 +331,9 @@ static int isp4sd_setup_output(struct isp4_subdev *isp_subdev,
 		return ret;
 	}
 
+	dev_dbg(dev, "enable channel %s\n",
+		isp4dbg_get_out_ch_str(cmd_ch_en.ch));
+
 	if (!sensor_info->start_stream_cmd_sent) {
 		ret = isp4sd_kickoff_stream(isp_subdev, out_prop->width,
 					    out_prop->height);
@@ -490,8 +496,9 @@ static void isp4sd_fw_resp_cmd_done(struct isp4_subdev *isp_subdev,
 		isp4if_rm_cmd_from_cmdq(ispif, para->cmd_seq_num, para->cmd_id);
 	struct device *dev = isp_subdev->dev;
 
-	dev_dbg(dev, "stream %d,cmd (0x%08x)(%d),seq %u, ele %p\n",
+	dev_dbg(dev, "stream %d,cmd %s(0x%08x)(%d),seq %u, ele %p\n",
 		stream_id,
+		isp4dbg_get_cmd_str(para->cmd_id),
 		para->cmd_id, para->cmd_status, para->cmd_seq_num,
 		ele);
 
@@ -548,8 +555,9 @@ isp4sd_preview_done(struct isp4_subdev *isp_subdev,
 			pcb->preview.status = ISP4VID_BUF_DONE_STATUS_SUCCESS;
 		}
 	} else if (meta->preview.enabled) {
-		dev_err(dev, "fail bad preview status %u\n",
-			meta->preview.status);
+		dev_err(dev, "fail bad preview status %u(%s)\n",
+			meta->preview.status,
+			isp4dbg_get_buf_done_str(meta->preview.status));
 	}
 
 	return prev;
@@ -609,14 +617,18 @@ static void isp4sd_fw_resp_frame_done(struct isp4_subdev *isp_subdev,
 	pcb.poc = meta->poc;
 	pcb.cam_id = 0;
 
-	dev_dbg(dev, "ts:%llu,streamId:%d,poc:%u,preview_en:%u,(%i)\n",
+	dev_dbg(dev, "ts:%llu,streamId:%d,poc:%u,preview_en:%u,%s(%i)\n",
 		ktime_get_ns(), stream_id, meta->poc,
 		meta->preview.enabled,
+		isp4dbg_get_buf_done_str(meta->preview.status),
 		meta->preview.status);
 
 	prev = isp4sd_preview_done(isp_subdev, meta, &pcb);
-	if (pcb.preview.status != ISP4VID_BUF_DONE_STATUS_ABSENT)
+	if (pcb.preview.status != ISP4VID_BUF_DONE_STATUS_ABSENT) {
+		isp4dbg_show_bufmeta_info(dev, "prev", &meta->preview,
+					  &pcb.preview.buf);
 		isp4vid_notify(&isp_subdev->isp_vdev, &pcb);
+	}
 
 	isp4if_dealloc_buffer_node(prev);
 
@@ -637,6 +649,8 @@ static void isp4sd_fw_resp_func(struct isp4_subdev *isp_subdev,
 	if (ispif->status < ISP4IF_STATUS_FW_RUNNING)
 		return;
 
+	isp_fw_log_print(isp_subdev);
+
 	while (true) {
 		s32 ret;
 
@@ -654,7 +668,8 @@ static void isp4sd_fw_resp_func(struct isp4_subdev *isp_subdev,
 						  &resp.param.frame_done);
 			break;
 		default:
-			dev_err(dev, "-><- fail respid (0x%x)\n",
+			dev_err(dev, "-><- fail respid %s(0x%x)\n",
+				isp4dbg_get_resp_str(resp.resp_id),
 				resp.resp_id);
 			break;
 		}
Signature · 1 line
2.34.1
author maintainer
#64 lore raw

PATCH v2 8/8 Documentation: add documentation of AMD isp 4 driver

In reply to Bin Du

Add documentation for AMD isp 4 and describe the main components

---
 Documentation/admin-guide/media/amdisp4-1.rst | 64 +++++++++++++++++++
 Documentation/admin-guide/media/amdisp4.dot   |  8 +++
 MAINTAINERS                                   |  2 +
 3 files changed, 74 insertions(+)
 create mode 100644 Documentation/admin-guide/media/amdisp4-1.rst
 create mode 100644 Documentation/admin-guide/media/amdisp4.dot
Documentation/admin-guide/media/amdisp4-1.rst new file +64 -0
new file mode 100644
index 000000000000..417b15af689a
@@ -0,0 +1,64 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+.. include:: <isonum.txt>
+
+====================================
+AMD Image Signal Processor (amdisp4)
+====================================
+
+Introduction
+============
+
+This file documents the driver for the AMD ISP4 that is part of
+AMD Ryzen AI Max 385 SoC.
+
+The driver is located under drivers/media/platform/amd/isp4 and uses
+the Media-Controller API.
+
+Topology
+========
+
+.. _amdisp4_topology_graph:
+
+.. kernel-figure:: amdisp4.dot
+     :alt:   Diagram of the media pipeline topology
+     :align: center
+
+
+
+The driver has 1 sub-device:
+
+- isp: used to resize and process bayer raw frames in to yuv.
+
+The driver has 1 video device:
+
+- <capture video device: capture device for retrieving images.
+
+
+  - ISP4 Image Signal Processing Subdevice Node
+-----------------------------------------------
+
+The isp4 is represented as a single V4L2 subdev, the sub-device does not
+provide interface to the user space. The sub-device is connected to one video node
+(isp4_capture) with immutable active link. The isp entity is connected
+to sensor pad 0 and receives the frames using CSI-2 protocol. The sub-device is
+also responsible to configure CSI2-2 receiver.
+The sub-device processes bayer raw data from the connected sensor and output
+them to different YUV formats. The isp also has scaling capabilities.
+
+  - isp4_capture - Frames Capture Video Node
+--------------------------------------------
+
+Isp4_capture is a capture device to capture frames to memory.
+This entity is the DMA engine that write the frames to memory.
+The entity is connected to isp4 sub-device.
+
+Capturing Video Frames Example
+==============================
+
+.. code-block:: bash
+
+         # set the links
+
+         # start streaming:
+         v4l2-ctl "-d" "/dev/video0" "--set-fmt-video=width=1920,height=1080,pixelformat=NV12" "--stream-mmap" "--stream-count=10"
Documentation/admin-guide/media/amdisp4.dot new file +8 -0
new file mode 100644
index 000000000000..a4c2f0cceb30
@@ -0,0 +1,8 @@
+digraph board {
+	rankdir=TB
+	n00000001 [label="{{<port0> 0} | amd isp4\n | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
+	n00000001:port1 -> n00000004 [style=bold]
+	n00000004 [label="Preview\n/dev/video0", shape=box, style=filled, fillcolor=yellow]
+	n0000000a [label="{{} | ov05c10 22-0010\n/dev/v4l-subdev0 | {<port0> 0}}", shape=Mrecord, style=filled, fillcolor=green]
+	n0000000a:port0 -> n00000001:port0 [style=bold]
+}
MAINTAINERS +2 -0
index 15070afb14b5..e4455bde376f 100644
@@ -1113,6 +1113,8 @@ M:	Nirujogi Pratap <[email protected]>
 L:	[email protected]
 S:	Maintained
 T:	git git://linuxtv.org/media.git
+F:	Documentation/admin-guide/media/amdisp4-1.rst
+F:	Documentation/admin-guide/media/amdisp4.dot
 F:	drivers/media/platform/amd/Kconfig
 F:	drivers/media/platform/amd/Makefile
 F:	drivers/media/platform/amd/isp4/*
Signature · 1 line
2.34.1
#65 lore raw

Re: [PATCH v2 8/8] Documentation: add documentation of AMD isp 4 driver

Quoted 50 lines · Bin Du

Add documentation for AMD isp 4 and describe the main components

---
 Documentation/admin-guide/media/amdisp4-1.rst | 64 +++++++++++++++++++
 Documentation/admin-guide/media/amdisp4.dot   |  8 +++
 MAINTAINERS                                   |  2 +
 3 files changed, 74 insertions(+)
 create mode 100644 Documentation/admin-guide/media/amdisp4-1.rst
 create mode 100644 Documentation/admin-guide/media/amdisp4.dot
Documentation/admin-guide/media/amdisp4-1.rst new file +32 -0
new file mode 100644
index 000000000000..417b15af689a
@@ -0,0 +1,64 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+.. include:: <isonum.txt>
+
+====================================
+AMD Image Signal Processor (amdisp4)
+====================================
+
+Introduction
+============
+
+This file documents the driver for the AMD ISP4 that is part of
+AMD Ryzen AI Max 385 SoC.
+
+The driver is located under drivers/media/platform/amd/isp4 and uses
+the Media-Controller API.
+
+Topology
+========
+
+.. _amdisp4_topology_graph:
+
+.. kernel-figure:: amdisp4.dot
+     :alt:   Diagram of the media pipeline topology
+     :align: center
+
+
+
+The driver has 1 sub-device:
+
+- isp: used to resize and process bayer raw frames in to yuv.
+

Documentation/admin-guide/media/amdisp4-1.rst line 42

Quoted 10 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +10 -0
+The driver has 1 video device:
+
+- <capture video device: capture device for retrieving images.
+
+
+  - ISP4 Image Signal Processing Subdevice Node
+-----------------------------------------------
+
+The isp4 is represented as a single V4L2 subdev, the sub-device does not
+provide interface to the user space.

Doesn't it ? The driver sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag for the subdev, and calls v4l2_device_register_subdev_nodes().

As far as I understand, the camera is exposed by the firmware with a webcam-like interface. We need to better understand your plans with this driver. If everything is handled by the firmware, why are the sensor and subdev exposed to userspace ? Why can't you expose a single video capture device, with a media device, and handle everything behind the scene ? I assume there may be more features coming later. Please document the plan, we can't provide feedback on the architecture otherwise.

Quoted 10 lines

The sub-device is connected to one video node

Documentation/admin-guide/media/amdisp4-1.rst new file +9 -0
+(isp4_capture) with immutable active link. The isp entity is connected
+to sensor pad 0 and receives the frames using CSI-2 protocol. The sub-device is
+also responsible to configure CSI2-2 receiver.
+The sub-device processes bayer raw data from the connected sensor and output
+them to different YUV formats. The isp also has scaling capabilities.
+
+  - isp4_capture - Frames Capture Video Node
+--------------------------------------------
+

Documentation/admin-guide/media/amdisp4-1.rst line 61

Quoted 10 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +10 -0
+Isp4_capture is a capture device to capture frames to memory.
+This entity is the DMA engine that write the frames to memory.
+The entity is connected to isp4 sub-device.
+
+Capturing Video Frames Example
+==============================
+
+.. code-block:: bash
+
+         # set the links

This seems very under-documented.

Quoted 30 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +3 -0
+
+         # start streaming:
+         v4l2-ctl "-d" "/dev/video0" "--set-fmt-video=width=1920,height=1080,pixelformat=NV12" "--stream-mmap" "--stream-count=10"
Documentation/admin-guide/media/amdisp4.dot new file +8 -0
new file mode 100644
index 000000000000..a4c2f0cceb30
@@ -0,0 +1,8 @@
+digraph board {
+	rankdir=TB
+	n00000001 [label="{{<port0> 0} | amd isp4\n | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
+	n00000001:port1 -> n00000004 [style=bold]
+	n00000004 [label="Preview\n/dev/video0", shape=box, style=filled, fillcolor=yellow]
+	n0000000a [label="{{} | ov05c10 22-0010\n/dev/v4l-subdev0 | {<port0> 0}}", shape=Mrecord, style=filled, fillcolor=green]
+	n0000000a:port0 -> n00000001:port0 [style=bold]
+}
MAINTAINERS +2 -0
index 15070afb14b5..e4455bde376f 100644
@@ -1113,6 +1113,8 @@ M:	Nirujogi Pratap <[email protected]>
 L:	[email protected]
 S:	Maintained
 T:	git git://linuxtv.org/media.git
+F:	Documentation/admin-guide/media/amdisp4-1.rst
+F:	Documentation/admin-guide/media/amdisp4.dot
 F:	drivers/media/platform/amd/Kconfig
 F:	drivers/media/platform/amd/Makefile
 F:	drivers/media/platform/amd/isp4/*
Signature · 3 lines
Regards,

Laurent Pinchart
author maintainer
#66 lore raw

Re: [PATCH v2 8/8] Documentation: add documentation of AMD isp 4 driver

Many thanks Laurent Pinchart for the review.

Quoted 56 lines · Laurent Pinchart
Quoted 55 lines · Bin Du

Add documentation for AMD isp 4 and describe the main components

---
  Documentation/admin-guide/media/amdisp4-1.rst | 64 +++++++++++++++++++
  Documentation/admin-guide/media/amdisp4.dot   |  8 +++
  MAINTAINERS                                   |  2 +
  3 files changed, 74 insertions(+)

create mode 100644 Documentation/admin-guide/media/amdisp4-1.rst create mode 100644 Documentation/admin-guide/media/amdisp4.dot

Documentation/admin-guide/media/amdisp4-1.rst new file +37 -0
new file mode 100644
index 000000000000..417b15af689a
@@ -0,0 +1,64 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+.. include:: <isonum.txt>
+
+====================================
+AMD Image Signal Processor (amdisp4)
+====================================
+
+Introduction
+============
+
+This file documents the driver for the AMD ISP4 that is part of
+AMD Ryzen AI Max 385 SoC.
+
+The driver is located under drivers/media/platform/amd/isp4 and uses
+the Media-Controller API.
+
+Topology
+========
+
+.. _amdisp4_topology_graph:
+
+.. kernel-figure:: amdisp4.dot
+     :alt:   Diagram of the media pipeline topology
+     :align: center
+
+
+
+The driver has 1 sub-device:
+
+- isp: used to resize and process bayer raw frames in to yuv.
+
+The driver has 1 video device:
+
+- <capture video device: capture device for retrieving images.
+
+
Quoted 9 lines

Documentation/admin-guide/media/amdisp4-1.rst

Quoted 5 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +5 -0
+  - ISP4 Image Signal Processing Subdevice Node
+-----------------------------------------------
+
+The isp4 is represented as a single V4L2 subdev, the sub-device does not
+provide interface to the user space.

Doesn't it ? The driver sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag for the subdev, and calls v4l2_device_register_subdev_nodes().

We have exported subdev device to user space during the testing with libcamera sample pipeline.

Quoted 9 lines

As far as I understand, the camera is exposed by the firmware with a webcam-like interface. We need to better understand your plans with this driver. If everything is handled by the firmware, why are the sensor and subdev exposed to userspace ? Why can't you expose a single video capture device, with a media device, and handle everything behind the scene ? I assume there may be more features coming later. Please document the plan, we can't provide feedback on the architecture otherwise.

Currently, isp fw is controlling the sensor to update just the exposure and gain, since the 3A algorithms run on ISP HW rather than on x86. In a future version, we plan to introduce raw output support in the ISP driver, allowing users to choose between AMD’s 3A running on ISP hardware or a custom 3A running on x86. If the user opts for the x86-based 3A, the firmware will relinquish control of the sensor, and hands over full control to the x86 system.

Quoted 14 lines
Quoted 14 lines

The sub-device is connected to one video node

Documentation/admin-guide/media/amdisp4-1.rst new file +13 -0
+(isp4_capture) with immutable active link. The isp entity is connected
+to sensor pad 0 and receives the frames using CSI-2 protocol. The sub-device is
+also responsible to configure CSI2-2 receiver.
+The sub-device processes bayer raw data from the connected sensor and output
+them to different YUV formats. The isp also has scaling capabilities.
+
+  - isp4_capture - Frames Capture Video Node
+--------------------------------------------
+
+Isp4_capture is a capture device to capture frames to memory.
+This entity is the DMA engine that write the frames to memory.
+The entity is connected to isp4 sub-device.
+
Quoted 9 lines

Documentation/admin-guide/media/amdisp4-1.rst line 61

Quoted 6 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +6 -0
+Capturing Video Frames Example
+==============================
+
+.. code-block:: bash
+
+         # set the links

This seems very under-documented.

Yes, documentation needs to be updated.

Quoted 31 lines
Quoted 30 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +3 -0
+
+         # start streaming:
+         v4l2-ctl "-d" "/dev/video0" "--set-fmt-video=width=1920,height=1080,pixelformat=NV12" "--stream-mmap" "--stream-count=10"
Documentation/admin-guide/media/amdisp4.dot new file +8 -0
new file mode 100644
index 000000000000..a4c2f0cceb30
@@ -0,0 +1,8 @@
+digraph board {
+	rankdir=TB
+	n00000001 [label="{{<port0> 0} | amd isp4\n | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
+	n00000001:port1 -> n00000004 [style=bold]
+	n00000004 [label="Preview\n/dev/video0", shape=box, style=filled, fillcolor=yellow]
+	n0000000a [label="{{} | ov05c10 22-0010\n/dev/v4l-subdev0 | {<port0> 0}}", shape=Mrecord, style=filled, fillcolor=green]
+	n0000000a:port0 -> n00000001:port0 [style=bold]
+}
MAINTAINERS +2 -0
index 15070afb14b5..e4455bde376f 100644
@@ -1113,6 +1113,8 @@ M:	Nirujogi Pratap <[email protected]>
  L:	[email protected]
  S:	Maintained
  T:	git git://linuxtv.org/media.git
+F:	Documentation/admin-guide/media/amdisp4-1.rst
+F:	Documentation/admin-guide/media/amdisp4.dot
  F:	drivers/media/platform/amd/Kconfig
  F:	drivers/media/platform/amd/Makefile
  F:	drivers/media/platform/amd/isp4/*
#67 lore raw

Re: [PATCH v2 8/8] Documentation: add documentation of AMD isp 4 driver

Quoted 66 lines · Bin
Quoted 56 lines · Laurent Pinchart
Quoted 55 lines · Bin Du

Add documentation for AMD isp 4 and describe the main components

---
  Documentation/admin-guide/media/amdisp4-1.rst | 64 +++++++++++++++++++
  Documentation/admin-guide/media/amdisp4.dot   |  8 +++
  MAINTAINERS                                   |  2 +
  3 files changed, 74 insertions(+)

create mode 100644 Documentation/admin-guide/media/amdisp4-1.rst create mode 100644 Documentation/admin-guide/media/amdisp4.dot

Documentation/admin-guide/media/amdisp4-1.rst new file +37 -0
new file mode 100644
index 000000000000..417b15af689a
@@ -0,0 +1,64 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+.. include:: <isonum.txt>
+
+====================================
+AMD Image Signal Processor (amdisp4)
+====================================
+
+Introduction
+============
+
+This file documents the driver for the AMD ISP4 that is part of
+AMD Ryzen AI Max 385 SoC.
+
+The driver is located under drivers/media/platform/amd/isp4 and uses
+the Media-Controller API.
+
+Topology
+========
+
+.. _amdisp4_topology_graph:
+
+.. kernel-figure:: amdisp4.dot
+     :alt:   Diagram of the media pipeline topology
+     :align: center
+
+
+
+The driver has 1 sub-device:
+
+- isp: used to resize and process bayer raw frames in to yuv.
+
+The driver has 1 video device:
+
+- <capture video device: capture device for retrieving images.
+
+
Quoted 8 lines

Documentation/admin-guide/media/amdisp4-1.rst

Quoted 5 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +5 -0
+  - ISP4 Image Signal Processing Subdevice Node
+-----------------------------------------------
+
+The isp4 is represented as a single V4L2 subdev, the sub-device does not
+provide interface to the user space.

Doesn't it ? The driver sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag for the subdev, and calls v4l2_device_register_subdev_nodes().

Quoted 2 lines

We have exported subdev device to user space during the testing with libcamera sample pipeline.

But it's not needed anymore ? If not, you could stop exposing the subdev to userspace for the time being.

Quoted 11 lines
Quoted 8 lines

As far as I understand, the camera is exposed by the firmware with a webcam-like interface. We need to better understand your plans with this driver. If everything is handled by the firmware, why are the sensor and subdev exposed to userspace ? Why can't you expose a single video capture device, with a media device, and handle everything behind the scene ? I assume there may be more features coming later. Please document the plan, we can't provide feedback on the architecture otherwise.

Currently, isp fw is controlling the sensor to update just the exposure and gain, since the 3A algorithms run on ISP HW rather than on x86.

This design decision makes my hair stand on end :-( Exposing the camera sensor to both the firmware and the host concurrently is asking for trouble. If you really want to abstract the camera behind a firmware and only expose a webcam-like API (or not even that in this version, as the driver exposes no control as far as I can see), then you should push the whole sensor handling to the firmware too. In my opinion that would not be a good solution compared to exposing the ISP to the host, but it would be better than this hybrid model.

Quoted 6 lines

In a
future version, we plan to introduce raw output support in the ISP driver, allowing users to choose between AMD’s 3A running on ISP hardware or a custom 3A running on x86. If the user opts for the x86-based 3A, the firmware will relinquish control of the sensor, and hands over full control to the x86 system.

Will the firmware at that point expose to Linux all the ISP statistics needed to implement auto-exposure ? What if I want to also set digital gain ? Or have manual white balance (requiring statistics), and manual CCM ?

Quoted 55 lines
Quoted 14 lines
Quoted 14 lines

The sub-device is connected to one video node

Documentation/admin-guide/media/amdisp4-1.rst new file +13 -0
+(isp4_capture) with immutable active link. The isp entity is connected
+to sensor pad 0 and receives the frames using CSI-2 protocol. The sub-device is
+also responsible to configure CSI2-2 receiver.
+The sub-device processes bayer raw data from the connected sensor and output
+them to different YUV formats. The isp also has scaling capabilities.
+
+  - isp4_capture - Frames Capture Video Node
+--------------------------------------------
+
+Isp4_capture is a capture device to capture frames to memory.
+This entity is the DMA engine that write the frames to memory.
+The entity is connected to isp4 sub-device.
+
Quoted 8 lines

Documentation/admin-guide/media/amdisp4-1.rst line 61

Quoted 6 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +6 -0
+Capturing Video Frames Example
+==============================
+
+.. code-block:: bash
+
+         # set the links

This seems very under-documented.

Yes, documentation needs to be updated.

Quoted 30 lines
Quoted 30 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +3 -0
+
+         # start streaming:
+         v4l2-ctl "-d" "/dev/video0" "--set-fmt-video=width=1920,height=1080,pixelformat=NV12" "--stream-mmap" "--stream-count=10"
Documentation/admin-guide/media/amdisp4.dot new file +8 -0
new file mode 100644
index 000000000000..a4c2f0cceb30
@@ -0,0 +1,8 @@
+digraph board {
+	rankdir=TB
+	n00000001 [label="{{<port0> 0} | amd isp4\n | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
+	n00000001:port1 -> n00000004 [style=bold]
+	n00000004 [label="Preview\n/dev/video0", shape=box, style=filled, fillcolor=yellow]
+	n0000000a [label="{{} | ov05c10 22-0010\n/dev/v4l-subdev0 | {<port0> 0}}", shape=Mrecord, style=filled, fillcolor=green]
+	n0000000a:port0 -> n00000001:port0 [style=bold]
+}
MAINTAINERS +2 -0
index 15070afb14b5..e4455bde376f 100644
@@ -1113,6 +1113,8 @@ M:	Nirujogi Pratap <[email protected]>
  L:	[email protected]
  S:	Maintained
  T:	git git://linuxtv.org/media.git
+F:	Documentation/admin-guide/media/amdisp4-1.rst
+F:	Documentation/admin-guide/media/amdisp4.dot
  F:	drivers/media/platform/amd/Kconfig
  F:	drivers/media/platform/amd/Makefile
  F:	drivers/media/platform/amd/isp4/*
Signature · 3 lines
Regards,

Laurent Pinchart
author maintainer
#68 lore raw

Re: [PATCH v2 8/8] Documentation: add documentation of AMD isp 4 driver

Many thanks Laurent, sorry for the late response because we had a lot of internal discussion and did possible solution prototype validation.

Quoted 67 lines · Laurent Pinchart
Quoted 66 lines · Bin
Quoted 64 lines · Laurent Pinchart
Quoted 50 lines · Bin Du

Add documentation for AMD isp 4 and describe the main components

---
   Documentation/admin-guide/media/amdisp4-1.rst | 64 +++++++++++++++++++
   Documentation/admin-guide/media/amdisp4.dot   |  8 +++
   MAINTAINERS                                   |  2 +
   3 files changed, 74 insertions(+)

create mode 100644 Documentation/admin-guide/media/amdisp4-1.rst create mode 100644 Documentation/admin-guide/media/amdisp4.dot

Documentation/admin-guide/media/amdisp4-1.rst new file +32 -0
new file mode 100644
index 000000000000..417b15af689a
@@ -0,0 +1,64 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+.. include:: <isonum.txt>
+
+====================================
+AMD Image Signal Processor (amdisp4)
+====================================
+
+Introduction
+============
+
+This file documents the driver for the AMD ISP4 that is part of
+AMD Ryzen AI Max 385 SoC.
+
+The driver is located under drivers/media/platform/amd/isp4 and uses
+the Media-Controller API.
+
+Topology
+========
+
+.. _amdisp4_topology_graph:
+
+.. kernel-figure:: amdisp4.dot
+     :alt:   Diagram of the media pipeline topology
+     :align: center
+
+
+
+The driver has 1 sub-device:
+
+- isp: used to resize and process bayer raw frames in to yuv.
+

Documentation/admin-guide/media/amdisp4-1.rst line 42

Quoted 10 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +10 -0
+The driver has 1 video device:
+
+- <capture video device: capture device for retrieving images.
+
+
+  - ISP4 Image Signal Processing Subdevice Node
+-----------------------------------------------
+
+The isp4 is represented as a single V4L2 subdev, the sub-device does not
+provide interface to the user space.

Doesn't it ? The driver sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag for the subdev, and calls v4l2_device_register_subdev_nodes().

Quoted 6 lines
Quoted 2 lines

We have exported subdev device to user space during the testing with libcamera sample pipeline.

But it's not needed anymore ? If not, you could stop exposing the subdev to userspace for the time being.

Yes, will not expose ISP subdev to userspace. Please see more details below.

Quoted 12 lines
Quoted 11 lines
Quoted 8 lines

As far as I understand, the camera is exposed by the firmware with a webcam-like interface. We need to better understand your plans with this driver. If everything is handled by the firmware, why are the sensor and subdev exposed to userspace ? Why can't you expose a single video capture device, with a media device, and handle everything behind the scene ? I assume there may be more features coming later. Please document the plan, we can't provide feedback on the architecture otherwise.

Currently, isp fw is controlling the sensor to update just the exposure and gain, since the 3A algorithms run on ISP HW rather than on x86.

Quoted 9 lines

This design decision makes my hair stand on end :-( Exposing the camera sensor to both the firmware and the host concurrently is asking for trouble. If you really want to abstract the camera behind a firmware and only expose a webcam-like API (or not even that in this version, as the driver exposes no control as far as I can see), then you should push the whole sensor handling to the firmware too. In my opinion that would not be a good solution compared to exposing the ISP to the host, but it would be better than this hybrid model.

Totally agree, yes, the hybrid model is really a bad idea, based on our internal validation, we plan to take one of your suggestions, that is to push the whole sensor handling to the firmware, so our ISP driver will work like UVC driver.

Quoted 7 lines
Quoted 6 lines

In a
future version, we plan to introduce raw output support in the ISP driver, allowing users to choose between AMD’s 3A running on ISP hardware or a custom 3A running on x86. If the user opts for the x86-based 3A, the firmware will relinquish control of the sensor, and hands over full control to the x86 system.

Quoted 5 lines

Will the firmware at that point expose to Linux all the ISP statistics needed to implement auto-exposure ? What if I want to also set digital gain ? Or have manual white balance (requiring statistics), and manual CCM ?

No, because all 3A, ISP and camera control will be put inside firmware. Currently, we only support auto mode, so, no manual control will be exposed

Quoted 56 lines
Quoted 55 lines
Quoted 14 lines
Quoted 14 lines

The sub-device is connected to one video node

Documentation/admin-guide/media/amdisp4-1.rst new file +13 -0
+(isp4_capture) with immutable active link. The isp entity is connected
+to sensor pad 0 and receives the frames using CSI-2 protocol. The sub-device is
+also responsible to configure CSI2-2 receiver.
+The sub-device processes bayer raw data from the connected sensor and output
+them to different YUV formats. The isp also has scaling capabilities.
+
+  - isp4_capture - Frames Capture Video Node
+--------------------------------------------
+
+Isp4_capture is a capture device to capture frames to memory.
+This entity is the DMA engine that write the frames to memory.
+The entity is connected to isp4 sub-device.
+
Quoted 8 lines

Documentation/admin-guide/media/amdisp4-1.rst line 61

Quoted 6 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +6 -0
+Capturing Video Frames Example
+==============================
+
+.. code-block:: bash
+
+         # set the links

This seems very under-documented.

Yes, documentation needs to be updated.

Quoted 30 lines
Quoted 30 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +3 -0
+
+         # start streaming:
+         v4l2-ctl "-d" "/dev/video0" "--set-fmt-video=width=1920,height=1080,pixelformat=NV12" "--stream-mmap" "--stream-count=10"
Documentation/admin-guide/media/amdisp4.dot new file +8 -0
new file mode 100644
index 000000000000..a4c2f0cceb30
@@ -0,0 +1,8 @@
+digraph board {
+	rankdir=TB
+	n00000001 [label="{{<port0> 0} | amd isp4\n | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
+	n00000001:port1 -> n00000004 [style=bold]
+	n00000004 [label="Preview\n/dev/video0", shape=box, style=filled, fillcolor=yellow]
+	n0000000a [label="{{} | ov05c10 22-0010\n/dev/v4l-subdev0 | {<port0> 0}}", shape=Mrecord, style=filled, fillcolor=green]
+	n0000000a:port0 -> n00000001:port0 [style=bold]
+}
MAINTAINERS +2 -0
index 15070afb14b5..e4455bde376f 100644
@@ -1113,6 +1113,8 @@ M:	Nirujogi Pratap <[email protected]>
   L:	[email protected]
   S:	Maintained
   T:	git git://linuxtv.org/media.git
+F:	Documentation/admin-guide/media/amdisp4-1.rst
+F:	Documentation/admin-guide/media/amdisp4.dot
   F:	drivers/media/platform/amd/Kconfig
   F:	drivers/media/platform/amd/Makefile
   F:	drivers/media/platform/amd/isp4/*
Signature · 2 lines
Regards,
Bin
maintainer
#69 lore raw

Re: [PATCH v2 8/8] Documentation: add documentation of AMD isp 4 driver

In reply to Du, Bin

Hi Bin,

Quoted 82 lines · Bin

Many thanks Laurent Pinchart for the review.

Quoted 56 lines · Laurent Pinchart
Quoted 55 lines · Bin Du

Add documentation for AMD isp 4 and describe the main components

---
  Documentation/admin-guide/media/amdisp4-1.rst | 64 +++++++++++++++++++
  Documentation/admin-guide/media/amdisp4.dot   |  8 +++
  MAINTAINERS                                   |  2 +
  3 files changed, 74 insertions(+)

create mode 100644 Documentation/admin-guide/media/amdisp4-1.rst create mode 100644 Documentation/admin-guide/media/amdisp4.dot

Documentation/admin-guide/media/amdisp4-1.rst new file +37 -0
new file mode 100644
index 000000000000..417b15af689a
@@ -0,0 +1,64 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+.. include:: <isonum.txt>
+
+====================================
+AMD Image Signal Processor (amdisp4)
+====================================
+
+Introduction
+============
+
+This file documents the driver for the AMD ISP4 that is part of
+AMD Ryzen AI Max 385 SoC.
+
+The driver is located under drivers/media/platform/amd/isp4 and uses
+the Media-Controller API.
+
+Topology
+========
+
+.. _amdisp4_topology_graph:
+
+.. kernel-figure:: amdisp4.dot
+     :alt:   Diagram of the media pipeline topology
+     :align: center
+
+
+
+The driver has 1 sub-device:
+
+- isp: used to resize and process bayer raw frames in to yuv.
+
+The driver has 1 video device:
+
+- <capture video device: capture device for retrieving images.
+
+
Quoted 9 lines

Documentation/admin-guide/media/amdisp4-1.rst

Quoted 5 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +5 -0
+  - ISP4 Image Signal Processing Subdevice Node
+-----------------------------------------------
+
+The isp4 is represented as a single V4L2 subdev, the sub-device does not
+provide interface to the user space.

Doesn't it ? The driver sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag for the subdev, and calls v4l2_device_register_subdev_nodes().

We have exported subdev device to user space during the testing with libcamera sample pipeline.

Quoted 9 lines

As far as I understand, the camera is exposed by the firmware with a webcam-like interface. We need to better understand your plans with this driver. If everything is handled by the firmware, why are the sensor and subdev exposed to userspace ? Why can't you expose a single video capture device, with a media device, and handle everything behind the scene ? I assume there may be more features coming later. Please document the plan, we can't provide feedback on the architecture otherwise.

Quoted 7 lines

Currently, isp fw is controlling the sensor to update just the exposure and gain, since the 3A algorithms run on ISP HW rather than on x86. In a future version, we plan to introduce raw output support in the ISP driver, allowing users to choose between AMD’s 3A running on ISP hardware or a custom 3A running on x86. If the user opts for the x86-based 3A, the firmware will relinquish control of the sensor, and hands over full control to the x86 system.

There are a few problems I see with this approach.

Camera sensors are separate devices from the ISP and they're expected to be controlled by the respective camera sensor drivers and these drivers only. The firmware contains the camera control algorithms as well as tuning; this is something that's better located outside of it.

The complex camera system comprising of a camera sensor, an ISP and a microcontroller within you have is right now semi-integrated to the SoC and I think it needs to be either fully unintegrated (the ISPs we currently support) or fully integrated (e.g. UVC webcams).

There are two options that I can see here, in descending order of preference:

1. Control the ISP processing blocks from the AMD ISP driver, via a documented UAPI. This includes setting processing block parameters and being able to obtain statistics from the ISP. This is aligned with the other currently supported ISP drivers.

This option could include support for the CSI-2 receiver only, with the processing taking place in SoftISP. Fully supported ISP would of course be preferred.

Right now I don't have an opinion on whether or not this needs to include libcamera support, but the ISP driver wouldn't be of much use without that anyway.

2. Move sensor control to firmware and make the AMD ISP driver expose an interface that looks like a webcam, akin to the UVC driver. In this case there's also no use for the sensor driver you've posted earlier. Overall, the ISP/sensor combination will probably be limited to use as a webcam in this case.

Signature · 3 lines
Kind regards,

Sakari Ailus
author maintainer
#70 lore raw

Re: [PATCH v2 8/8] Documentation: add documentation of AMD isp 4 driver

Many thanks Sakari Ailus for your deep insight

Quoted 121 lines · Sakari Ailus

Hi Bin,

Quoted 82 lines · Bin

Many thanks Laurent Pinchart for the review.

Quoted 56 lines · Laurent Pinchart
Quoted 55 lines · Bin Du

Add documentation for AMD isp 4 and describe the main components

---
   Documentation/admin-guide/media/amdisp4-1.rst | 64 +++++++++++++++++++
   Documentation/admin-guide/media/amdisp4.dot   |  8 +++
   MAINTAINERS                                   |  2 +
   3 files changed, 74 insertions(+)

create mode 100644 Documentation/admin-guide/media/amdisp4-1.rst create mode 100644 Documentation/admin-guide/media/amdisp4.dot

Documentation/admin-guide/media/amdisp4-1.rst new file +37 -0
new file mode 100644
index 000000000000..417b15af689a
@@ -0,0 +1,64 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+.. include:: <isonum.txt>
+
+====================================
+AMD Image Signal Processor (amdisp4)
+====================================
+
+Introduction
+============
+
+This file documents the driver for the AMD ISP4 that is part of
+AMD Ryzen AI Max 385 SoC.
+
+The driver is located under drivers/media/platform/amd/isp4 and uses
+the Media-Controller API.
+
+Topology
+========
+
+.. _amdisp4_topology_graph:
+
+.. kernel-figure:: amdisp4.dot
+     :alt:   Diagram of the media pipeline topology
+     :align: center
+
+
+
+The driver has 1 sub-device:
+
+- isp: used to resize and process bayer raw frames in to yuv.
+
+The driver has 1 video device:
+
+- <capture video device: capture device for retrieving images.
+
+
Quoted 9 lines

Documentation/admin-guide/media/amdisp4-1.rst

Quoted 5 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +5 -0
+  - ISP4 Image Signal Processing Subdevice Node
+-----------------------------------------------
+
+The isp4 is represented as a single V4L2 subdev, the sub-device does not
+provide interface to the user space.

Doesn't it ? The driver sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag for the subdev, and calls v4l2_device_register_subdev_nodes().

We have exported subdev device to user space during the testing with libcamera sample pipeline.

Quoted 9 lines

As far as I understand, the camera is exposed by the firmware with a webcam-like interface. We need to better understand your plans with this driver. If everything is handled by the firmware, why are the sensor and subdev exposed to userspace ? Why can't you expose a single video capture device, with a media device, and handle everything behind the scene ? I assume there may be more features coming later. Please document the plan, we can't provide feedback on the architecture otherwise.

Quoted 7 lines

Currently, isp fw is controlling the sensor to update just the exposure and gain, since the 3A algorithms run on ISP HW rather than on x86. In a future version, we plan to introduce raw output support in the ISP driver, allowing users to choose between AMD’s 3A running on ISP hardware or a custom 3A running on x86. If the user opts for the x86-based 3A, the firmware will relinquish control of the sensor, and hands over full control to the x86 system.

There are a few problems I see with this approach.

Camera sensors are separate devices from the ISP and they're expected to be controlled by the respective camera sensor drivers and these drivers only. The firmware contains the camera control algorithms as well as tuning; this is something that's better located outside of it.

The complex camera system comprising of a camera sensor, an ISP and a microcontroller within you have is right now semi-integrated to the SoC and I think it needs to be either fully unintegrated (the ISPs we currently support) or fully integrated (e.g. UVC webcams).

There are two options that I can see here, in descending order of preference:

1. Control the ISP processing blocks from the AMD ISP driver, via a documented UAPI. This includes setting processing block parameters and being able to obtain statistics from the ISP. This is aligned with the other currently supported ISP drivers.

This option could include support for the CSI-2 receiver only, with the processing taking place in SoftISP. Fully supported ISP would of course be preferred.

Right now I don't have an opinion on whether or not this needs to include libcamera support, but the ISP driver wouldn't be of much use without that anyway.

Quoted 6 lines

2. Move sensor control to firmware and make the AMD ISP driver expose an interface that looks like a webcam, akin to the UVC driver. In this case there's also no use for the sensor driver you've posted earlier. Overall, the ISP/sensor combination will probably be limited to use as a webcam in this case.

Based on our internal discussion and validation, will make option 2 as our current upstream target, after that, will plan option 1 with more considerations, a. Whether to provide the capability and interface so user can do switch between option 1 and 2. b. Whether and how to expose interface so host can leverage the ISP HW feature to accelerat some processing. Though sensor driver is not used in option 2, we still plan to upstream it because of option 1 as next step.

Signature · 2 lines
Regards,
Bin
maintainer
#71 lore raw

Re: [PATCH v2 8/8] Documentation: add documentation of AMD isp 4 driver

Hi Bin,

Quoted 129 lines · Bin

Many thanks Sakari Ailus for your deep insight

Quoted 119 lines · Sakari Ailus

Hi Bin,

Quoted 82 lines · Bin

Many thanks Laurent Pinchart for the review.

Quoted 56 lines · Laurent Pinchart
Quoted 55 lines · Bin Du

Add documentation for AMD isp 4 and describe the main components

---
   Documentation/admin-guide/media/amdisp4-1.rst | 64 +++++++++++++++++++
   Documentation/admin-guide/media/amdisp4.dot   |  8 +++
   MAINTAINERS                                   |  2 +
   3 files changed, 74 insertions(+)

create mode 100644 Documentation/admin-guide/media/amdisp4-1.rst create mode 100644 Documentation/admin-guide/media/amdisp4.dot

Documentation/admin-guide/media/amdisp4-1.rst new file +37 -0
new file mode 100644
index 000000000000..417b15af689a
@@ -0,0 +1,64 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+.. include:: <isonum.txt>
+
+====================================
+AMD Image Signal Processor (amdisp4)
+====================================
+
+Introduction
+============
+
+This file documents the driver for the AMD ISP4 that is part of
+AMD Ryzen AI Max 385 SoC.
+
+The driver is located under drivers/media/platform/amd/isp4 and uses
+the Media-Controller API.
+
+Topology
+========
+
+.. _amdisp4_topology_graph:
+
+.. kernel-figure:: amdisp4.dot
+     :alt:   Diagram of the media pipeline topology
+     :align: center
+
+
+
+The driver has 1 sub-device:
+
+- isp: used to resize and process bayer raw frames in to yuv.
+
+The driver has 1 video device:
+
+- <capture video device: capture device for retrieving images.
+
+
Quoted 9 lines

Documentation/admin-guide/media/amdisp4-1.rst

Quoted 5 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +5 -0
+  - ISP4 Image Signal Processing Subdevice Node
+-----------------------------------------------
+
+The isp4 is represented as a single V4L2 subdev, the sub-device does not
+provide interface to the user space.

Doesn't it ? The driver sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag for the subdev, and calls v4l2_device_register_subdev_nodes().

We have exported subdev device to user space during the testing with libcamera sample pipeline.

Quoted 9 lines

As far as I understand, the camera is exposed by the firmware with a webcam-like interface. We need to better understand your plans with this driver. If everything is handled by the firmware, why are the sensor and subdev exposed to userspace ? Why can't you expose a single video capture device, with a media device, and handle everything behind the scene ? I assume there may be more features coming later. Please document the plan, we can't provide feedback on the architecture otherwise.

Quoted 7 lines

Currently, isp fw is controlling the sensor to update just the exposure and gain, since the 3A algorithms run on ISP HW rather than on x86. In a future version, we plan to introduce raw output support in the ISP driver, allowing users to choose between AMD’s 3A running on ISP hardware or a custom 3A running on x86. If the user opts for the x86-based 3A, the firmware will relinquish control of the sensor, and hands over full control to the x86 system.

There are a few problems I see with this approach.

Camera sensors are separate devices from the ISP and they're expected to be controlled by the respective camera sensor drivers and these drivers only. The firmware contains the camera control algorithms as well as tuning; this is something that's better located outside of it.

The complex camera system comprising of a camera sensor, an ISP and a microcontroller within you have is right now semi-integrated to the SoC and I think it needs to be either fully unintegrated (the ISPs we currently support) or fully integrated (e.g. UVC webcams).

There are two options that I can see here, in descending order of preference:

1. Control the ISP processing blocks from the AMD ISP driver, via a documented UAPI. This includes setting processing block parameters and being able to obtain statistics from the ISP. This is aligned with the other currently supported ISP drivers.
This option could include support for the CSI-2 receiver only, with the processing taking place in SoftISP. Fully supported ISP would of course be preferred.
Right now I don't have an opinion on whether or not this needs to include libcamera support, but the ISP driver wouldn't be of much use without that anyway.

Quoted 6 lines

2. Move sensor control to firmware and make the AMD ISP driver expose an interface that looks like a webcam, akin to the UVC driver. In this case there's also no use for the sensor driver you've posted earlier. Overall, the ISP/sensor combination will probably be limited to use as a webcam in this case.

Quoted 7 lines

Based on our internal discussion and validation, will make option 2 as our current upstream target, after that, will plan option 1 with more considerations, a. Whether to provide the capability and interface so user can do switch between option 1 and 2. b. Whether and how to expose interface so host can leverage the ISP HW feature to accelerat some processing. Though sensor driver is not used in option 2, we still plan to upstream it because of option 1 as next step.

I expect this to take some time.

In the meantime, I'm inclined to merge ov02c05 driver from Bingbu / Hao in case they can provide an upstreamable one as that driver would have users already today. That being said, it won't be a problem accommodating the needs of both into the same driver.

Signature · 3 lines
Kind regards,

Sakari Ailus
author maintainer
#72 lore raw

Re: [PATCH v2 8/8] Documentation: add documentation of AMD isp 4 driver

Many thanks Sakari for the update. All clear on my end — feel free to take the time you need.

Quoted 147 lines · Sakari Ailus

Hi Bin,

Quoted 129 lines · Bin

Many thanks Sakari Ailus for your deep insight

Quoted 119 lines · Sakari Ailus

Hi Bin,

Quoted 82 lines · Bin

Many thanks Laurent Pinchart for the review.

Quoted 56 lines · Laurent Pinchart
Quoted 55 lines · Bin Du

Add documentation for AMD isp 4 and describe the main components

---
    Documentation/admin-guide/media/amdisp4-1.rst | 64 +++++++++++++++++++
    Documentation/admin-guide/media/amdisp4.dot   |  8 +++
    MAINTAINERS                                   |  2 +
    3 files changed, 74 insertions(+)

create mode 100644 Documentation/admin-guide/media/amdisp4-1.rst create mode 100644 Documentation/admin-guide/media/amdisp4.dot

Documentation/admin-guide/media/amdisp4-1.rst new file +37 -0
new file mode 100644
index 000000000000..417b15af689a
@@ -0,0 +1,64 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+.. include:: <isonum.txt>
+
+====================================
+AMD Image Signal Processor (amdisp4)
+====================================
+
+Introduction
+============
+
+This file documents the driver for the AMD ISP4 that is part of
+AMD Ryzen AI Max 385 SoC.
+
+The driver is located under drivers/media/platform/amd/isp4 and uses
+the Media-Controller API.
+
+Topology
+========
+
+.. _amdisp4_topology_graph:
+
+.. kernel-figure:: amdisp4.dot
+     :alt:   Diagram of the media pipeline topology
+     :align: center
+
+
+
+The driver has 1 sub-device:
+
+- isp: used to resize and process bayer raw frames in to yuv.
+
+The driver has 1 video device:
+
+- <capture video device: capture device for retrieving images.
+
+
Quoted 9 lines

Documentation/admin-guide/media/amdisp4-1.rst

Quoted 5 lines
Documentation/admin-guide/media/amdisp4-1.rst new file +5 -0
+  - ISP4 Image Signal Processing Subdevice Node
+-----------------------------------------------
+
+The isp4 is represented as a single V4L2 subdev, the sub-device does not
+provide interface to the user space.

Doesn't it ? The driver sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag for the subdev, and calls v4l2_device_register_subdev_nodes().

We have exported subdev device to user space during the testing with libcamera sample pipeline.

Quoted 9 lines

As far as I understand, the camera is exposed by the firmware with a webcam-like interface. We need to better understand your plans with this driver. If everything is handled by the firmware, why are the sensor and subdev exposed to userspace ? Why can't you expose a single video capture device, with a media device, and handle everything behind the scene ? I assume there may be more features coming later. Please document the plan, we can't provide feedback on the architecture otherwise.

Quoted 7 lines

Currently, isp fw is controlling the sensor to update just the exposure and gain, since the 3A algorithms run on ISP HW rather than on x86. In a future version, we plan to introduce raw output support in the ISP driver, allowing users to choose between AMD’s 3A running on ISP hardware or a custom 3A running on x86. If the user opts for the x86-based 3A, the firmware will relinquish control of the sensor, and hands over full control to the x86 system.

There are a few problems I see with this approach.

Camera sensors are separate devices from the ISP and they're expected to be controlled by the respective camera sensor drivers and these drivers only. The firmware contains the camera control algorithms as well as tuning; this is something that's better located outside of it.

The complex camera system comprising of a camera sensor, an ISP and a microcontroller within you have is right now semi-integrated to the SoC and I think it needs to be either fully unintegrated (the ISPs we currently support) or fully integrated (e.g. UVC webcams).

There are two options that I can see here, in descending order of preference:

1. Control the ISP processing blocks from the AMD ISP driver, via a documented UAPI. This includes setting processing block parameters and being able to obtain statistics from the ISP. This is aligned with the other currently supported ISP drivers.
This option could include support for the CSI-2 receiver only, with the processing taking place in SoftISP. Fully supported ISP would of course be preferred.
Right now I don't have an opinion on whether or not this needs to include libcamera support, but the ISP driver wouldn't be of much use without that anyway.

Quoted 6 lines

2. Move sensor control to firmware and make the AMD ISP driver expose an interface that looks like a webcam, akin to the UVC driver. In this case there's also no use for the sensor driver you've posted earlier. Overall, the ISP/sensor combination will probably be limited to use as a webcam in this case.

Quoted 7 lines

Based on our internal discussion and validation, will make option 2 as our current upstream target, after that, will plan option 1 with more considerations, a. Whether to provide the capability and interface so user can do switch between option 1 and 2. b. Whether and how to expose interface so host can leverage the ISP HW feature to accelerat some processing. Though sensor driver is not used in option 2, we still plan to upstream it because of option 1 as next step.

I expect this to take some time.

In the meantime, I'm inclined to merge ov02c05 driver from Bingbu / Hao in case they can provide an upstreamable one as that driver would have users already today. That being said, it won't be a problem accommodating the needs of both into the same driver.

Signature · 2 lines
Regards,
Bin
#73 lore raw
In reply to Bin Du
Quoted 15 lines · Bin Du

AMD ISP4 is the AMD image processing gen 4 which can be found in HP ZBook Ultra G1a 14 inch Mobile Workstation PC ( Ryzen AI Max 385)
(https://ubuntu.com/certified/202411-36043)
This patch series introduces the initial driver support for the AMD ISP4.

Patch summary:
- Powers up/off and initializes ISP HW
- Configures and kicks off ISP FW
- Interacts with APP using standard V4l2 interface by video node
- Controls ISP HW and interacts with ISP FW to do image processing
- Support enum/set output image format and resolution
- Support queueing buffer from app and dequeueing ISP filled buffer to App
- Starts/stops sensor and mipi csi when camera App starts/stops streaming.
- It supports libcamera ver0.2 SimplePipeline
- It is verified on qv4l2, cheese and qcam
- It is verified together with following patches

Quoted 5 lines
	platform/x86: Add AMD ISP platform config for OV05C10 (https://lore.kernel.org/all/[email protected]/)
	media: i2c: Add OV05C10 camera sensor driver (https://lore.kernel.org/all/[email protected]/)
	pinctrl: amd: isp411: Add amdisp GPIO pinctrl (https://github.com/torvalds/linux/commit/e97435ab09f3ad7b6a588dd7c4e45a96699bbb4a)
	i2c: amd-isp: Add ISP i2c-designware driver (https://github.com/torvalds/linux/commit/d6263c468a761cd4c89887073614394ac48441e1)
	drm/amd/amdgpu: Add GPIO resources required for amdisp (https://gitlab.freedesktop.org/agd5f/linux/-/commit/ad0f5966ed8297aa47b3184192b00b7379ae0758)

Hi,

I have the Ryzen AI MAX+ 395 SKU of the HP ZBook Ultra G1a 14.

I cannot for the life of me get the webcam working under Linux. The webcam works under Windows so it's not a hardware issue.

With this patchset and all of the patches you link here applied to 6.15, I get the following errors:

  [   11.970038] amd_isp_i2c_designware amd_isp_i2c_designware: Unknown Synopsys component type: 0xffffffff
  [   11.973162] amd_isp_i2c_designware amd_isp_i2c_designware: error -19: i2c_dw_probe failed

With the old ispkernel code from February [1] applied on 6.15, the webcam indicator LED lights up but there's no image. I see these messages at boot:

  [    9.449005] amd_isp_capture amd_isp_capture.1.auto: amdgpu: AMD ISP v4l2 device registered
  [    9.489005] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: The OV05 sensor device is added to the ISP I2C bus
  [    9.529012] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout while trying to abort current transfer
  [    9.554046] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout in disabling adapter
  [    9.554174] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout while trying to abort current transfer
  [    9.580022] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout in disabling adapter

And then the kernel crashes due to the same use-after-free issues I pointed out in my other email [2].

Any idea what's going on?

[1] https://github.com/amd/Linux_ISP_Kernel/commit/c6d42584fbd0aa42cc91ecf16dc5c4f3dfea0bb4
[2] https://lore.kernel.org/r/aIEiJL83pOYO8lUJ@sultan-box

Thanks,
Sultan

author maintainer
#74 lore raw

Hi Sultan,

[1] is for kernel 6.8, believe it can't be applied to 6.15. We didn't verify on 6.15 but we are really glad to help, would you please provide some info,
1. Suppose you are using Ubuntu, right? What's the version?
2. 6.15, do you mean https://github.com/torvalds/linux/tree/v6.15 ?

After your confirmation, we'll see what we can do to enable your camera quickly and easily

Regards,
Bin

Quoted 53 lines · Sultan Alsawaf
Quoted 15 lines · Bin Du

AMD ISP4 is the AMD image processing gen 4 which can be found in HP ZBook Ultra G1a 14 inch Mobile Workstation PC ( Ryzen AI Max 385)
(https://ubuntu.com/certified/202411-36043)
This patch series introduces the initial driver support for the AMD ISP4.

Patch summary:
- Powers up/off and initializes ISP HW
- Configures and kicks off ISP FW
- Interacts with APP using standard V4l2 interface by video node
- Controls ISP HW and interacts with ISP FW to do image processing
- Support enum/set output image format and resolution
- Support queueing buffer from app and dequeueing ISP filled buffer to App
- Starts/stops sensor and mipi csi when camera App starts/stops streaming.
- It supports libcamera ver0.2 SimplePipeline
- It is verified on qv4l2, cheese and qcam
- It is verified together with following patches

Quoted 5 lines
	platform/x86: Add AMD ISP platform config for OV05C10 (https://lore.kernel.org/all/[email protected]/)
	media: i2c: Add OV05C10 camera sensor driver (https://lore.kernel.org/all/[email protected]/)
	pinctrl: amd: isp411: Add amdisp GPIO pinctrl (https://github.com/torvalds/linux/commit/e97435ab09f3ad7b6a588dd7c4e45a96699bbb4a)
	i2c: amd-isp: Add ISP i2c-designware driver (https://github.com/torvalds/linux/commit/d6263c468a761cd4c89887073614394ac48441e1)
	drm/amd/amdgpu: Add GPIO resources required for amdisp (https://gitlab.freedesktop.org/agd5f/linux/-/commit/ad0f5966ed8297aa47b3184192b00b7379ae0758)

Hi,

I have the Ryzen AI MAX+ 395 SKU of the HP ZBook Ultra G1a 14.

I cannot for the life of me get the webcam working under Linux. The webcam works under Windows so it's not a hardware issue.

With this patchset and all of the patches you link here applied to 6.15, I get the following errors:

   [   11.970038] amd_isp_i2c_designware amd_isp_i2c_designware: Unknown Synopsys component type: 0xffffffff
   [   11.973162] amd_isp_i2c_designware amd_isp_i2c_designware: error -19: i2c_dw_probe failed

With the old ispkernel code from February [1] applied on 6.15, the webcam indicator LED lights up but there's no image. I see these messages at boot:

   [    9.449005] amd_isp_capture amd_isp_capture.1.auto: amdgpu: AMD ISP v4l2 device registered
   [    9.489005] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: The OV05 sensor device is added to the ISP I2C bus
   [    9.529012] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout while trying to abort current transfer
   [    9.554046] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout in disabling adapter
   [    9.554174] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout while trying to abort current transfer
   [    9.580022] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout in disabling adapter

And then the kernel crashes due to the same use-after-free issues I pointed out in my other email [2].

Any idea what's going on?

[1] https://github.com/amd/Linux_ISP_Kernel/commit/c6d42584fbd0aa42cc91ecf16dc5c4f3dfea0bb4
[2] https://lore.kernel.org/r/aIEiJL83pOYO8lUJ@sultan-box

Thanks,
Sultan

#75 lore raw
Quoted 33 lines · Bin
Quoted 19 lines

I have the Ryzen AI MAX+ 395 SKU of the HP ZBook Ultra G1a 14.

I cannot for the life of me get the webcam working under Linux. The webcam works under Windows so it's not a hardware issue.

With this patchset and all of the patches you link here applied to 6.15, I get the following errors:

   [   11.970038] amd_isp_i2c_designware amd_isp_i2c_designware: Unknown Synopsys component type: 0xffffffff
   [   11.973162] amd_isp_i2c_designware amd_isp_i2c_designware: error -19: i2c_dw_probe failed

With the old ispkernel code from February [1] applied on 6.15, the webcam indicator LED lights up but there's no image. I see these messages at boot:

   [    9.449005] amd_isp_capture amd_isp_capture.1.auto: amdgpu: AMD ISP v4l2 device registered
   [    9.489005] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: The OV05 sensor device is added to the ISP I2C bus
   [    9.529012] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout while trying to abort current transfer
   [    9.554046] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout in disabling adapter
   [    9.554174] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout while trying to abort current transfer
   [    9.580022] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout in disabling adapter
Quoted 7 lines

And then the kernel crashes due to the same use-after-free issues I pointed out in my other email [2].

Any idea what's going on?

[1] https://github.com/amd/Linux_ISP_Kernel/commit/c6d42584fbd0aa42cc91ecf16dc5c4f3dfea0bb4
[2] https://lore.kernel.org/r/aIEiJL83pOYO8lUJ@sultan-box

Hi Sultan,

[1] is for kernel 6.8, believe it can't be applied to 6.15. We didn't verify on 6.15 but we are really glad to help, would you please provide some info,
1. Suppose you are using Ubuntu, right? What's the version?
2. 6.15, do you mean https://github.com/torvalds/linux/tree/v6.15 ?

Quoted 5 lines

After your confirmation, we'll see what we can do to enable your camera quickly and easily

Regards,
Bin

Thank you, Bin!

1. I'm using Arch Linux with the ISP4-patched libcamera [1].
2. Yes, here is my kernel source [2].

I have some more findings:

Currently, the first blocking issue is that the I2C adapter fails to initialize. This is because the ISP tile isn't powered on.

I noticed that in the old version of amd_isp_i2c_designware [3], there were calls to isp_power_set(), which is available in the old ISP4 sources [4]. Without isp_power_set(), the I2C adapter always fails to initialize for me.

How is the ISP tile supposed to get powered on in the current ISP4 code?

Also, I noticed that the driver init ordering matters between all of the drivers needed for the ISP4 camera. In particular, amd_isp_i2c_designware and amd_isp4 must be initialized before amd_capture, otherwise amd_capture will fail to find the fwnode properties for the OV05C10 device attached to the I2C bus.

But there is no driver init ordering enforced, which also caused some issues for me until I figured it out. Maybe probe deferral (-EPROBE_DEFER) should be used to ensure each driver waits for its dependencies to init first?

[1] https://github.com/amd/Linux_ISP_libcamera/tree/3.0
[2] https://github.com/kerneltoast/kernel_x86_laptop/tree/v6.15-sultan-isp4
[3] https://lore.kernel.org/all/[email protected]
[4] https://github.com/amd/Linux_ISP_Kernel/blob/c6d42584fbd0aa42cc91ecf16dc5c4f3dfea0bb4/drivers/media/platform/amd/isp4/isp_hwa.c#L378-L385

Sultan

author maintainer
#76 lore raw

Thanks Sultan, please see my comments

Quoted 47 lines · Sultan Alsawaf
Quoted 33 lines · Bin
Quoted 19 lines

I have the Ryzen AI MAX+ 395 SKU of the HP ZBook Ultra G1a 14.

I cannot for the life of me get the webcam working under Linux. The webcam works under Windows so it's not a hardware issue.

With this patchset and all of the patches you link here applied to 6.15, I get the following errors:

    [   11.970038] amd_isp_i2c_designware amd_isp_i2c_designware: Unknown Synopsys component type: 0xffffffff
    [   11.973162] amd_isp_i2c_designware amd_isp_i2c_designware: error -19: i2c_dw_probe failed

With the old ispkernel code from February [1] applied on 6.15, the webcam indicator LED lights up but there's no image. I see these messages at boot:

    [    9.449005] amd_isp_capture amd_isp_capture.1.auto: amdgpu: AMD ISP v4l2 device registered
    [    9.489005] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: The OV05 sensor device is added to the ISP I2C bus
    [    9.529012] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout while trying to abort current transfer
    [    9.554046] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout in disabling adapter
    [    9.554174] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout while trying to abort current transfer
    [    9.580022] amd_isp_i2c_designware amd_isp_i2c_designware.2.auto: timeout in disabling adapter
Quoted 7 lines

And then the kernel crashes due to the same use-after-free issues I pointed out in my other email [2].

Any idea what's going on?

[1] https://github.com/amd/Linux_ISP_Kernel/commit/c6d42584fbd0aa42cc91ecf16dc5c4f3dfea0bb4
[2] https://lore.kernel.org/r/aIEiJL83pOYO8lUJ@sultan-box

Hi Sultan,

[1] is for kernel 6.8, believe it can't be applied to 6.15. We didn't verify on 6.15 but we are really glad to help, would you please provide some info,
1. Suppose you are using Ubuntu, right? What's the version?
2. 6.15, do you mean https://github.com/torvalds/linux/tree/v6.15 ?

Quoted 5 lines

After your confirmation, we'll see what we can do to enable your camera quickly and easily

Regards,
Bin

Thank you, Bin!

1. I'm using Arch Linux with the ISP4-patched libcamera [1].
2. Yes, here is my kernel source [2].

I have some more findings:

Quoted 9 lines

Currently, the first blocking issue is that the I2C adapter fails to initialize. This is because the ISP tile isn't powered on.

I noticed that in the old version of amd_isp_i2c_designware [3], there were calls to isp_power_set(), which is available in the old ISP4 sources [4]. Without isp_power_set(), the I2C adapter always fails to initialize for me.

How is the ISP tile supposed to get powered on in the current ISP4 code?

You are correct, yes, i believe the I2C adapter failure is caused by ISP not being powered up. Currently in latest code, isp_power_set is no longer available, instead, we implemented genPD for ISP in amdgpu
https://lore.kernel.org/all/[email protected]/
Both amd_isp_i2c and amd_isp_capture are in the power domain and use the standard runtime PM API to do the power control

Quoted 9 lines

Also, I noticed that the driver init ordering matters between all of the drivers needed for the ISP4 camera. In particular, amd_isp_i2c_designware and amd_isp4 must be initialized before amd_capture, otherwise amd_capture will fail to find the fwnode properties for the OV05C10 device attached to the I2C bus.

But there is no driver init ordering enforced, which also caused some issues for me until I figured it out. Maybe probe deferral (-EPROBE_DEFER) should be used to ensure each driver waits for its dependencies to init first?

amd_isp_capture only has dependency on amd_isp4 which is the ACPI platform driver, it is init before amd_isp_catpure.
Do you see in your side the amd_capture probe failure caused by failing to read fwnode properties? If that's the case please help to check if amd_isp4 is loaded successfully

Quoted 6 lines

[1] https://github.com/amd/Linux_ISP_libcamera/tree/3.0
[2] https://github.com/kerneltoast/kernel_x86_laptop/tree/v6.15-sultan-isp4
[3] https://lore.kernel.org/all/[email protected]
[4] https://github.com/amd/Linux_ISP_Kernel/blob/c6d42584fbd0aa42cc91ecf16dc5c4f3dfea0bb4/drivers/media/platform/amd/isp4/isp_hwa.c#L378-L385

Sultan