Skip to contentSkip to conversation

Add AMD ISP4 driver

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

Subsystems (9)

v2 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Bin Du

author maintainer
#0 lore raw

v2 cover Add AMD ISP4 driver

Hello,

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

	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)

AMD ISP4 Key features:
- Processes bayer raw data from the connected sensor and output them to different YUV formats
- Downscale input image to different output image resolution
- Pipeline to do image processing on the input image including demosaic, denoise, 3A, etc

Changes v1 -> v2:
- Fix media CI test errors and valid warnings
- Reduce patch number in the series from 9 to 8 by merging MAINTAINERS adding patch to the first patch
- In patch 5
- do modification to use remote endpoint instead of local endpoint
- use link frequency and port number as start phy parameter instead of extra added phy-id and phy-bit-rate property of endpoint

It passes v4l2 compliance test, the test reports for:

(a) ov05c10 device /dev/v4l-subdev0
(b) amd_isp_capture device /dev/video0

Compliance test for device /dev/v4l-subdev0:
--------------------------------------------

atg@isp-pv-linux:~/v4l-utils/build/utils/v4l2-compliance$ sudo ./v4l2-compliance -u 0 v4l2-compliance 1.29.0-5348, 64 bits, 64-bit time_t v4l2-compliance SHA: 75e3f0e2c2cb 2025-03-17 18:12:17

Compliance test for device /dev/v4l-subdev0:

Driver Info:

         Driver version   : 6.14.0
         Capabilities     : 0x00000000
         Client Capabilities: 0x0000000000000002 interval-uses-which Required ioctls:
         test VIDIOC_SUDBEV_QUERYCAP: OK
         test invalid ioctls: OK

Allow for multiple opens:
test second /dev/v4l-subdev0 open: OK
test VIDIOC_SUBDEV_QUERYCAP: OK
test for unlimited opens: OK

Debug ioctls:
test VIDIOC_LOG_STATUS: OK (Not Supported)

Input ioctls:
test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported) test VIDIOC_ENUMAUDIO: OK (Not Supported)
test VIDIOC_G/S/ENUMINPUT: OK (Not Supported) test VIDIOC_G/S_AUDIO: OK (Not Supported)
Inputs: 0 Audio Inputs: 0 Tuners: 0

Output ioctls:
test VIDIOC_G/S_MODULATOR: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_ENUMAUDOUT: OK (Not Supported) test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported) test VIDIOC_G/S_AUDOUT: OK (Not Supported)
Outputs: 0 Audio Outputs: 0 Modulators: 0

Input/Output configuration ioctls:
test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported) test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported) test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported) test VIDIOC_G/S_EDID: OK (Not Supported)

Control ioctls:
test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK test VIDIOC_QUERYCTRL: OK
test VIDIOC_G/S_CTRL: OK
test VIDIOC_G/S/TRY_EXT_CTRLS: OK test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK test VIDIOC_G/S_JPEGCOMP: OK (Not Supported) Standard Controls: 11 Private Controls: 0

Format ioctls:
test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK (Not Supported) test VIDIOC_G/S_PARM: OK (Not Supported)
test VIDIOC_G_FBUF: OK (Not Supported)
test VIDIOC_G_FMT: OK (Not Supported)
test VIDIOC_TRY_FMT: OK (Not Supported)
test VIDIOC_S_FMT: OK (Not Supported)
test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported)
test Cropping: OK (Not Supported)
test Composing: OK (Not Supported)
test Scaling: OK (Not Supported)

Codec ioctls:
test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported) test VIDIOC_G_ENC_INDEX: OK (Not Supported) test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)

Buffer ioctls:
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK (Not Supported) test CREATE_BUFS maximum buffers: OK
test VIDIOC_REMOVE_BUFS: OK
test VIDIOC_EXPBUF: OK (Not Supported)
test Requests: OK (Not Supported)
test blocking wait: OK (Not Supported)

Total for device /dev/v4l-subdev0: 46, Succeeded: 46, Failed: 0, Warnings: 0
---

Compliance test for amd_isp_capture device /dev/video0:
-------------------------------------------------------

atg@isp-pv-linux:~/test$ sudo ./v4l2-compliance -d /dev/video0
v4l2-compliance 1.29.0-5348, 64 bits, 64-bit time_t v4l2-compliance SHA: 75e3f0e2c2cb 2025-03-17 18:12:17

Compliance test for amd_isp_capture device /dev/video0:

Driver Info:

         Driver name      : amd_isp_capture
         Card type        : amd_isp_capture
         Bus info         : platform:amd_isp_capture
         Driver version   : 6.14.0
         Capabilities     : 0xa4200001

Video Capture
I/O MC
Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x24200001
Video Capture
I/O MC
Streaming
Extended Pix Format
Media Driver Info:

         Driver name      : amd_isp_capture
         Model            : amd_isp41_mdev
         Serial           :
         Bus info         : platform:amd_isp_capture
         Media version    : 6.14.0
         Hardware revision: 0x00000000 (0)
         Driver version   : 6.14.0

Interface Info:
ID : 0x03000003
Type : V4L Video
Entity Info:

         ID               : 0x00000001 (1)
         Name             : Preview
         Function         : V4L2 I/O
         Pad 0x01000002   : 0: Sink
           Link 0x02000007: from remote pad 0x1000006 of entity 'ov05c10 99-0010' (Camera Sensor): Data, Enabled, Immutable

Required ioctls:
test MC information (see 'Media Driver Info' above): OK test VIDIOC_QUERYCAP: OK
test invalid ioctls: OK

Allow for multiple opens:
test second /dev/video0 open: OK
test VIDIOC_QUERYCAP: OK
test VIDIOC_G/S_PRIORITY: OK
test for unlimited opens: OK

Debug ioctls:
test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported) test VIDIOC_LOG_STATUS: OK (Not Supported)

Input ioctls:
test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported) test VIDIOC_ENUMAUDIO: OK (Not Supported)
test VIDIOC_G/S/ENUMINPUT: OK
test VIDIOC_G/S_AUDIO: OK (Not Supported)
Inputs: 1 Audio Inputs: 0 Tuners: 0

Output ioctls:
test VIDIOC_G/S_MODULATOR: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_ENUMAUDOUT: OK (Not Supported) test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported) test VIDIOC_G/S_AUDOUT: OK (Not Supported)
Outputs: 0 Audio Outputs: 0 Modulators: 0

Input/Output configuration ioctls:
test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported) test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported) test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported) test VIDIOC_G/S_EDID: OK (Not Supported)

Control ioctls (Input 0):
test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK (Not Supported) test VIDIOC_QUERYCTRL: OK (Not Supported)
test VIDIOC_G/S_CTRL: OK (Not Supported)
test VIDIOC_G/S/TRY_EXT_CTRLS: OK (Not Supported) test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK (Not Supported) test VIDIOC_G/S_JPEGCOMP: OK (Not Supported) Standard Controls: 0 Private Controls: 0

Format ioctls (Input 0):
test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK test VIDIOC_G/S_PARM: OK
test VIDIOC_G_FBUF: OK (Not Supported) test VIDIOC_G_FMT: OK
test VIDIOC_TRY_FMT: OK
test VIDIOC_S_FMT: OK
test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported) test Cropping: OK (Not Supported)
test Composing: OK (Not Supported)
test Scaling: OK (Not Supported)

Codec ioctls (Input 0):
test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported) test VIDIOC_G_ENC_INDEX: OK (Not Supported) test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)

Buffer ioctls (Input 0):
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK test CREATE_BUFS maximum buffers: OK test VIDIOC_REMOVE_BUFS: OK
test VIDIOC_EXPBUF: OK
test Requests: OK (Not Supported) test blocking wait: OK

Total for amd_isp_capture device /dev/video0: 49, Succeeded: 49, Failed: 0, Warnings: 0

Please review and provide feedback.

Many thanks,

Bin Du (8):
media: platform: amd: Introduce amd isp4 capture driver
media: platform: amd: low level support for isp4 firmware
media: platform: amd: Add helpers to configure isp4 mipi phy
media: platform: amd: Add isp4 fw and hw interface
media: platform: amd: isp4 subdev and firmware loading handling added
media: platform: amd: isp4 video node and buffers handling added
media: platform: amd: isp4 debug fs logging and more descriptive
errors
Documentation: add documentation of AMD isp 4 driver

 Documentation/admin-guide/media/amdisp4-1.rst |   64 +
 Documentation/admin-guide/media/amdisp4.dot   |    8 +
 MAINTAINERS                                   |   12 +
 drivers/media/platform/Kconfig                |    1 +
 drivers/media/platform/Makefile               |    1 +
 drivers/media/platform/amd/Kconfig            |   17 +
 drivers/media/platform/amd/Makefile           |    5 +
 drivers/media/platform/amd/isp4/Makefile      |   38 +
 drivers/media/platform/amd/isp4/isp4.c        |  406 +++++
 drivers/media/platform/amd/isp4/isp4.h        |   37 +
 drivers/media/platform/amd/isp4/isp4_debug.c  |  272 +++
 drivers/media/platform/amd/isp4/isp4_debug.h  |   41 +
 .../platform/amd/isp4/isp4_fw_cmd_resp.h      |  318 ++++
 drivers/media/platform/amd/isp4/isp4_hw.c     |   46 +
 drivers/media/platform/amd/isp4/isp4_hw.h     |   14 +
 drivers/media/platform/amd/isp4/isp4_hw_reg.h |  116 ++
 .../media/platform/amd/isp4/isp4_interface.c  | 1064 ++++++++++++
 .../media/platform/amd/isp4/isp4_interface.h  |  164 ++
 drivers/media/platform/amd/isp4/isp4_phy.c    | 1547 +++++++++++++++++
 drivers/media/platform/amd/isp4/isp4_phy.h    |   14 +
 drivers/media/platform/amd/isp4/isp4_subdev.c | 1308 ++++++++++++++
 drivers/media/platform/amd/isp4/isp4_subdev.h |  147 ++
 drivers/media/platform/amd/isp4/isp4_video.c  | 1443 +++++++++++++++
 drivers/media/platform/amd/isp4/isp4_video.h  |   93 +
 24 files changed, 7176 insertions(+)
 create mode 100644 Documentation/admin-guide/media/amdisp4-1.rst
 create mode 100644 Documentation/admin-guide/media/amdisp4.dot
 create mode 100644 drivers/media/platform/amd/Kconfig
 create mode 100644 drivers/media/platform/amd/Makefile
 create mode 100644 drivers/media/platform/amd/isp4/Makefile
 create mode 100644 drivers/media/platform/amd/isp4/isp4.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4.h
 create mode 100644 drivers/media/platform/amd/isp4/isp4_debug.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4_debug.h
 create mode 100644 drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h
 create mode 100644 drivers/media/platform/amd/isp4/isp4_hw.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4_hw.h
 create mode 100644 drivers/media/platform/amd/isp4/isp4_hw_reg.h
 create mode 100644 drivers/media/platform/amd/isp4/isp4_interface.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4_interface.h
 create mode 100644 drivers/media/platform/amd/isp4/isp4_phy.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4_phy.h
 create mode 100644 drivers/media/platform/amd/isp4/isp4_subdev.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4_subdev.h
 create mode 100644 drivers/media/platform/amd/isp4/isp4_video.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4_video.h
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v2 1/8 media: platform: amd: Introduce amd isp4 capture driver

Amd isp4 capture is a v4l2 media device which implements media controller interface.
It has one sub-device (amd ISP4 sub-device) endpoint which can be connected to a remote CSI2 TX endpoint. It supports only one physical interface for now.
Also add ISP4 driver related entry info into the MAINAINERS file

---
 MAINTAINERS                              |  10 ++
 drivers/media/platform/Kconfig           |   1 +
 drivers/media/platform/Makefile          |   1 +
 drivers/media/platform/amd/Kconfig       |  17 +++
 drivers/media/platform/amd/Makefile      |   5 +
 drivers/media/platform/amd/isp4/Makefile |  21 ++++
 drivers/media/platform/amd/isp4/isp4.c   | 139 +++++++++++++++++++++++
 drivers/media/platform/amd/isp4/isp4.h   |  35 ++++++
 8 files changed, 229 insertions(+)
 create mode 100644 drivers/media/platform/amd/Kconfig
 create mode 100644 drivers/media/platform/amd/Makefile
 create mode 100644 drivers/media/platform/amd/isp4/Makefile
 create mode 100644 drivers/media/platform/amd/isp4/isp4.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4.h
MAINTAINERS +10 -0
index 10893c91b1c1..15070afb14b5 100644
@@ -1107,6 +1107,16 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux.git
 F:	drivers/iommu/amd/
 F:	include/linux/amd-iommu.h
 
+AMD ISP4 DRIVER
+M:	Bin Du <[email protected]>
+M:	Nirujogi Pratap <[email protected]>
+L:	[email protected]
+S:	Maintained
+T:	git git://linuxtv.org/media.git
+F:	drivers/media/platform/amd/Kconfig
+F:	drivers/media/platform/amd/Makefile
+F:	drivers/media/platform/amd/isp4/*
+
 AMD KFD
 M:	Felix Kuehling <[email protected]>
 L:	[email protected]
drivers/media/platform/Kconfig +1 -0
index 85d2627776b6..d525c2262a7d 100644
@@ -89,5 +89,6 @@ source "drivers/media/platform/ti/Kconfig"
 source "drivers/media/platform/verisilicon/Kconfig"
 source "drivers/media/platform/via/Kconfig"
 source "drivers/media/platform/xilinx/Kconfig"
+source "drivers/media/platform/amd/Kconfig"
 
 endif # MEDIA_PLATFORM_DRIVERS
drivers/media/platform/Makefile +1 -0
index ace4e34483dd..9f3d1693868d 100644
@@ -32,6 +32,7 @@ obj-y += ti/
 obj-y += verisilicon/
 obj-y += via/
 obj-y += xilinx/
+obj-y += amd/
 
 # Please place here only ancillary drivers that aren't SoC-specific
 # Please keep it alphabetically sorted by Kconfig name
drivers/media/platform/amd/Kconfig new file +17 -0
new file mode 100644
index 000000000000..3b1dba0400a0
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: MIT
+
+config AMD_ISP4
+	tristate "AMD ISP4 and camera driver"
+	default y
+	depends on VIDEO_DEV && VIDEO_V4L2_SUBDEV_API
+	select VIDEOBUF2_CORE
+	select VIDEOBUF2_V4L2
+	select VIDEOBUF2_MEMOPS
+	select VIDEOBUF2_VMALLOC
+	select VIDEOBUF2_DMA_CONTIG
+	select VIDEOBUF2_DMA_SG
+	help
+	  This is support for AMD ISP4 and camera subsystem driver.
+	  Say Y here to enable the ISP4 and camera device for video capture.
+	  To compile this driver as a module, choose M here. The module will
+	  be called amd_capture.
drivers/media/platform/amd/Makefile new file +5 -0
new file mode 100644
index 000000000000..76146efcd2bf
@@ -0,0 +1,5 @@
+# Copyright 2024 Advanced Micro Devices, Inc.
+# add isp block
+ifneq ($(CONFIG_AMD_ISP4),)
+obj-y += isp4/
+endif
drivers/media/platform/amd/isp4/Makefile new file +21 -0
new file mode 100644
index 000000000000..e9e84160517d
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2025 Advanced Micro Devices, Inc.
+
+obj-$(CONFIG_AMD_ISP4) += amd_capture.o
+amd_capture-objs := isp4.o
+
+ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
+ccflags-y += -I$(srctree)/include
+
+ifneq ($(call cc-option, -mpreferred-stack-boundary=4),)
+	cc_stack_align := -mpreferred-stack-boundary=4
+endif
+
+ccflags-y += $(cc_stack_align)
+ccflags-y += -DCONFIG_COMPAT
+ccflags-y += -Wunused-but-set-variable
+ccflags-y += -Wmissing-include-dirs
+ccflags-y += -Wunused-const-variable
+ccflags-y += -Wmaybe-uninitialized
+ccflags-y += -Wunused-value
drivers/media/platform/amd/isp4/isp4.c new file +139 -0

139 lines not on this page · raw message

drivers/media/platform/amd/isp4/isp4.h new file +35 -0
new file mode 100644
index 000000000000..27a7362ce6f9
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_H_
+#define _ISP4_H_
+
+#include <linux/mutex.h>
+#include <media/v4l2-device.h>
+#include <media/videobuf2-memops.h>
+#include <media/videobuf2-vmalloc.h>
+
+#define ISP4_GET_ISP_REG_BASE(isp4sd) (((isp4sd))->mmio)
+
+struct isp4_platform_data {
+	void *adev;
+	void *bo;
+	void *cpu_ptr;
+	u64 gpu_addr;
+	u32 size;
+	u32 asic_type;
+	resource_size_t base_rmmio_size;
+};
+
+struct isp4_device {
+	struct v4l2_device v4l2_dev;
+	struct media_device mdev;
+
+	struct isp4_platform_data *pltf_data;
+	struct platform_device *pdev;
+	struct notifier_block i2c_nb;
+};
+
+#endif /* isp4.h */
Signature · 1 line
2.34.1
author maintainer
#8 lore raw

PATCH v2 2/8 media: platform: amd: low level support for isp4 firmware

In reply to Bin Du

Low level functions for access the registers and mapping to their ranges. This change also includes register definitions for ring buffer used to communicate with ISP Firmware.
Ring buffer is the communication interface between driver and ISP Firmware. Command and responses are exchanged through the ring buffer.

---
 drivers/media/platform/amd/isp4/Makefile      |   3 +-
 drivers/media/platform/amd/isp4/isp4_hw.c     |  46 +++++++
 drivers/media/platform/amd/isp4/isp4_hw.h     |  14 +++
 drivers/media/platform/amd/isp4/isp4_hw_reg.h | 116 ++++++++++++++++++
 4 files changed, 178 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/platform/amd/isp4/isp4_hw.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4_hw.h
 create mode 100644 drivers/media/platform/amd/isp4/isp4_hw_reg.h
drivers/media/platform/amd/isp4/Makefile +2 -1
index e9e84160517d..8ca1c4dfe246 100644
@@ -3,7 +3,8 @@
 # Copyright (C) 2025 Advanced Micro Devices, Inc.
 
 obj-$(CONFIG_AMD_ISP4) += amd_capture.o
-amd_capture-objs := isp4.o
+amd_capture-objs := isp4.o	\
+			isp4_hw.o	\
 
 ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
 ccflags-y += -I$(srctree)/include
drivers/media/platform/amd/isp4/isp4_hw.c new file +46 -0

46 lines not on this page · raw message

drivers/media/platform/amd/isp4/isp4_hw.h new file +14 -0
new file mode 100644
index 000000000000..072d135b9e3a
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_HW_H_
+#define _ISP4_HW_H_
+
+#define RREG_FAILED_VAL 0xFFFFFFFF
+
+u32 isp4hw_rreg(void __iomem *base, u32 reg);
+void isp4hw_wreg(void __iomem *base, u32 reg, u32 val);
+
+#endif
drivers/media/platform/amd/isp4/isp4_hw_reg.h new file +116 -0

116 lines not on this page · raw message

Signature · 1 line
2.34.1
author maintainer
#13 lore raw

PATCH v2 3/8 media: platform: amd: Add helpers to configure isp4 mipi phy

In reply to Bin Du

The helper functions is for configuring, starting and stop the MIPI PHY. All configurations related to MIPI PHY configuration and calibration parameters are encapsulated in two helper functions: start and stop mipi phy.

---
 drivers/media/platform/amd/isp4/Makefile   |    1 +
 drivers/media/platform/amd/isp4/isp4_phy.c | 1547 ++++++++++++++++++++
 drivers/media/platform/amd/isp4/isp4_phy.h |   14 +
 3 files changed, 1562 insertions(+)
 create mode 100644 drivers/media/platform/amd/isp4/isp4_phy.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4_phy.h
drivers/media/platform/amd/isp4/Makefile +1 -0
index 8ca1c4dfe246..0e36201fbb30 100644
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_AMD_ISP4) += amd_capture.o
 amd_capture-objs := isp4.o	\
+			isp4_phy.o \
 			isp4_hw.o	\
 
 ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
drivers/media/platform/amd/isp4/isp4_phy.c new file +1547 -0

1,547 lines not on this page · raw message

drivers/media/platform/amd/isp4/isp4_phy.h new file +14 -0
new file mode 100644
index 000000000000..2909892dbd00
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_PHY_H_
+#define _ISP4_PHY_H_
+
+int isp4phy_start(struct device *dev,
+		  void __iomem *base, u32 phy_id, u64 bit_rate,
+		  u32 lane_num);
+int isp4phy_stop(void __iomem *base, u32 phy_id);
+
+#endif
Signature · 1 line
2.34.1
author maintainer
#22 lore raw

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

In reply to 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.

---
 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 +318 -0

318 lines not on this page · raw message

drivers/media/platform/amd/isp4/isp4_interface.c new file +1052 -0

1,052 lines not on this page · raw message

drivers/media/platform/amd/isp4/isp4_interface.h new file +164 -0

164 lines not on this page · raw message

Signature · 1 line
2.34.1
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
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
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
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
#2 lore raw

Re: [PATCH v2 1/8] media: platform: amd: Introduce amd isp4 capture driver

In reply to Bin Du
Quoted 1 line · Bin Du

Amd isp4 capture is a v4l2 media device which implements media controller

AMD

Quoted 2 lines

interface.
It has one sub-device (amd ISP4 sub-device) endpoint which can be connected

AMD

Quoted 3 lines

to a remote CSI2 TX endpoint. It supports only one physical interface for now.
Also add ISP4 driver related entry info into the MAINAINERS file

MAINTAINERS

Quoted 3 lines

Who actually developed? If both are developers there should be a Co-developed-by tag.

Quoted 53 lines
---
  MAINTAINERS                              |  10 ++
  drivers/media/platform/Kconfig           |   1 +
  drivers/media/platform/Makefile          |   1 +
  drivers/media/platform/amd/Kconfig       |  17 +++
  drivers/media/platform/amd/Makefile      |   5 +
  drivers/media/platform/amd/isp4/Makefile |  21 ++++
  drivers/media/platform/amd/isp4/isp4.c   | 139 +++++++++++++++++++++++
  drivers/media/platform/amd/isp4/isp4.h   |  35 ++++++
  8 files changed, 229 insertions(+)

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

MAINTAINERS +10 -0
index 10893c91b1c1..15070afb14b5 100644
@@ -1107,6 +1107,16 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux.git
  F:	drivers/iommu/amd/
  F:	include/linux/amd-iommu.h
 
+AMD ISP4 DRIVER
+M:	Bin Du <[email protected]>
+M:	Nirujogi Pratap <[email protected]>
+L:	[email protected]
+S:	Maintained
+T:	git git://linuxtv.org/media.git
+F:	drivers/media/platform/amd/Kconfig
+F:	drivers/media/platform/amd/Makefile
+F:	drivers/media/platform/amd/isp4/*
+
  AMD KFD
  M:	Felix Kuehling <[email protected]>
  L:	[email protected]
drivers/media/platform/Kconfig +1 -0
index 85d2627776b6..d525c2262a7d 100644
@@ -89,5 +89,6 @@ source "drivers/media/platform/ti/Kconfig"
  source "drivers/media/platform/verisilicon/Kconfig"
  source "drivers/media/platform/via/Kconfig"
  source "drivers/media/platform/xilinx/Kconfig"
+source "drivers/media/platform/amd/Kconfig"
 
  endif # MEDIA_PLATFORM_DRIVERS
drivers/media/platform/Makefile +0 -0
index ace4e34483dd..9f3d1693868d 100644

drivers/media/platform/Makefile obj-y += ti/ · line 35

Quoted 4 lines
drivers/media/platform/Makefile +1 -0
@@ -32,6 +32,7 @@ obj-y += ti/
  obj-y += verisilicon/
  obj-y += via/
  obj-y += xilinx/
+obj-y += amd/

Is this whole file alphabetical? If so this is the wrong place.

Quoted 9 lines
drivers/media/platform/Makefile +0 -0
  # Please place here only ancillary drivers that aren't SoC-specific
  # Please keep it alphabetically sorted by Kconfig name
drivers/media/platform/amd/Kconfig new file +0 -0
new file mode 100644
index 000000000000..3b1dba0400a0

drivers/media/platform/amd/Kconfig line 5

Quoted 5 lines
drivers/media/platform/amd/Kconfig new file +5 -0
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: MIT
+
+config AMD_ISP4
+	tristate "AMD ISP4 and camera driver"
+	default y

I don't believe this should default 'y'. Normally drivers need to be opt in.

Quoted 18 lines
drivers/media/platform/amd/Kconfig new file +12 -0
+	depends on VIDEO_DEV && VIDEO_V4L2_SUBDEV_API
+	select VIDEOBUF2_CORE
+	select VIDEOBUF2_V4L2
+	select VIDEOBUF2_MEMOPS
+	select VIDEOBUF2_VMALLOC
+	select VIDEOBUF2_DMA_CONTIG
+	select VIDEOBUF2_DMA_SG
+	help
+	  This is support for AMD ISP4 and camera subsystem driver.
+	  Say Y here to enable the ISP4 and camera device for video capture.
+	  To compile this driver as a module, choose M here. The module will
+	  be called amd_capture.
drivers/media/platform/amd/Makefile new file +0 -0
new file mode 100644
index 000000000000..76146efcd2bf

drivers/media/platform/amd/Makefile line 1

Quoted 1 line
drivers/media/platform/amd/Makefile new file +1 -0
@@ -0,0 +1,5 @@
+# Copyright 2024 Advanced Micro Devices, Inc.

2025

Quoted 10 lines
drivers/media/platform/amd/Makefile new file +4 -0
+# add isp block
+ifneq ($(CONFIG_AMD_ISP4),)
+obj-y += isp4/
+endif
drivers/media/platform/amd/isp4/Makefile new file +0 -0
new file mode 100644
index 000000000000..e9e84160517d

drivers/media/platform/amd/isp4/Makefile line 5

Quoted 5 lines
drivers/media/platform/amd/isp4/Makefile new file +5 -0
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2025 Advanced Micro Devices, Inc.
+
+obj-$(CONFIG_AMD_ISP4) += amd_capture.o

As the directory is already conditional on CONFIG_AMD_ISP4 is this obj-$() needed? Or should it really be obj-y?

Quoted 6 lines
drivers/media/platform/amd/isp4/Makefile new file +6 -0
+amd_capture-objs := isp4.o
+
+ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
+ccflags-y += -I$(srctree)/include
+
+ifneq ($(call cc-option, -mpreferred-stack-boundary=4),)

drivers/media/platform/amd/isp4/Makefile line 21

Quoted 10 lines
drivers/media/platform/amd/isp4/Makefile new file +10 -0
+	cc_stack_align := -mpreferred-stack-boundary=4
+endif
+
+ccflags-y += $(cc_stack_align)
+ccflags-y += -DCONFIG_COMPAT
+ccflags-y += -Wunused-but-set-variable
+ccflags-y += -Wmissing-include-dirs
+ccflags-y += -Wunused-const-variable
+ccflags-y += -Wmaybe-uninitialized
+ccflags-y += -Wunused-value

Do you really need to enforce all these flags just for this driver?

Was this just for development to avoid having to remember to call the build with W=1 or CONFIG_WERROR?

Quoted 31 lines
drivers/media/platform/amd/isp4/isp4.c new file +25 -0
new file mode 100644
index 000000000000..d0be90c5ec3b
@@ -0,0 +1,139 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/pm_runtime.h>
+#include <linux/vmalloc.h>
+#include <media/v4l2-ioctl.h>
+
+#include "isp4.h"
+
+#define VIDEO_BUF_NUM 5
+
+#define ISP4_DRV_NAME "amd_isp_capture"
+
+/* interrupt num */
+static const u32 isp4_ringbuf_interrupt_num[] = {
+	0, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT9 */
+	1, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT10 */
+	3, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT11 */
+	4, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT12 */
+};
+
+#define to_isp4_device(dev) \
+	((struct isp4_device *)container_of(dev, struct isp4_device, v4l2_dev))

drivers/media/platform/amd/isp4/isp4.c line 35

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+
+static irqreturn_t isp4_irq_handler(int irq, void *arg)
+{
+	struct isp4_device *isp_dev = dev_get_drvdata((struct device *)arg);
+
+	if (!isp_dev)
+		goto error_drv_data;
+
+error_drv_data:
+	return IRQ_HANDLED;

In patch 5 you change this function, including dropping the goto and label.

So I suggest that for patch 1 you KISS:

static irqreturn_t isp4_irq_handler(int irq, void *arg)

{
	return IRQ_HANDLED;
}

Then in patch 5 add the extra conditional code and real handling.

drivers/media/platform/amd/isp4/isp4.c line 40

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c new file +5 -0
+}
+
+/*
+ * amd capture module
+ */

Pointless comment, no?

drivers/media/platform/amd/isp4/isp4.c line 45

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c new file +5 -0
+static int isp4_capture_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct isp4_device *isp_dev;
+

Why newline here?

Quoted 15 lines
drivers/media/platform/amd/isp4/isp4.c new file +15 -0
+	int i, irq, ret;
+
+	isp_dev = devm_kzalloc(&pdev->dev, sizeof(*isp_dev), GFP_KERNEL);
+	if (!isp_dev)
+		return -ENOMEM;
+
+	isp_dev->pdev = pdev;
+	dev->init_name = ISP4_DRV_NAME;
+
+	for (i = 0; i < ARRAY_SIZE(isp4_ringbuf_interrupt_num); i++) {
+		irq = platform_get_irq(pdev, isp4_ringbuf_interrupt_num[i]);
+		if (irq < 0)
+			return dev_err_probe(dev, -ENODEV,
+					     "fail to get irq %d\n",
+					     isp4_ringbuf_interrupt_num[i]);

drivers/media/platform/amd/isp4/isp4.c line 70

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+		ret = devm_request_irq(&pdev->dev, irq, isp4_irq_handler, 0,
+				       "ISP_IRQ", &pdev->dev);
+		if (ret)
+			return dev_err_probe(dev, ret, "fail to req irq %d\n",
+					     irq);
+	}
+
+	isp_dev->pltf_data = pdev->dev.platform_data;
+
+	dev_dbg(dev, "isp irq registration successful\n");

As you have error handling in place with dev_err_probe() I think the lack of an error implies this message. I'd say drop it.

Quoted 11 lines
drivers/media/platform/amd/isp4/isp4.c new file +11 -0
+
+	/* Link the media device within the v4l2_device */
+	isp_dev->v4l2_dev.mdev = &isp_dev->mdev;
+
+	/* Initialize media device */
+	strscpy(isp_dev->mdev.model, "amd_isp41_mdev",
+		sizeof(isp_dev->mdev.model));
+	snprintf(isp_dev->mdev.bus_info, sizeof(isp_dev->mdev.bus_info),
+		 "platform:%s", ISP4_DRV_NAME);
+	isp_dev->mdev.dev = &pdev->dev;
+	media_device_init(&isp_dev->mdev);

drivers/media/platform/amd/isp4/isp4.c line 91

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+
+	/* register v4l2 device */
+	snprintf(isp_dev->v4l2_dev.name, sizeof(isp_dev->v4l2_dev.name),
+		 "AMD-V4L2-ROOT");
+	ret = v4l2_device_register(&pdev->dev, &isp_dev->v4l2_dev);
+	if (ret)
+		return dev_err_probe(dev, ret,
+				     "fail register v4l2 device\n");
+
+	dev_dbg(dev, "AMD ISP v4l2 device registered\n");

As you have error handling in place with dev_err_probe() I think the lack of an error implies this message. I'd say drop it.

Quoted 18 lines
drivers/media/platform/amd/isp4/isp4.c new file +18 -0
+
+	ret = media_device_register(&isp_dev->mdev);
+	if (ret) {
+		dev_err(dev, "fail to register media device %d\n", ret);
+		goto err_unreg_v4l2;
+	}
+
+	platform_set_drvdata(pdev, isp_dev);
+
+	pm_runtime_set_suspended(dev);
+	pm_runtime_enable(dev);
+
+	return 0;
+
+err_unreg_v4l2:
+	v4l2_device_unregister(&isp_dev->v4l2_dev);
+
+	return dev_err_probe(dev, ret, "isp probe fail\n");

drivers/media/platform/amd/isp4/isp4.c line 119

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+}
+
+static void isp4_capture_remove(struct platform_device *pdev)
+{
+	struct isp4_device *isp_dev = platform_get_drvdata(pdev);
+	struct device *dev = &pdev->dev;
+
+	media_device_unregister(&isp_dev->mdev);
+	v4l2_device_unregister(&isp_dev->v4l2_dev);
+	dev_dbg(dev, "AMD ISP v4l2 device unregistered\n");

Probably not needed message anymore, right?

Quoted 35 lines
drivers/media/platform/amd/isp4/isp4.c new file +20 -0
+}
+
+static struct platform_driver isp4_capture_drv = {
+	.probe = isp4_capture_probe,
+	.remove = isp4_capture_remove,
+	.driver = {
+		.name = ISP4_DRV_NAME,
+		.owner = THIS_MODULE,
+	}
+};
+
+module_platform_driver(isp4_capture_drv);
+
+MODULE_ALIAS("platform:" ISP4_DRV_NAME);
+MODULE_IMPORT_NS("DMA_BUF");
+
+MODULE_DESCRIPTION("AMD ISP4 Driver");
+MODULE_AUTHOR("Bin Du <[email protected]>");
+MODULE_AUTHOR("Pratap Nirujogi <[email protected]>");
+MODULE_LICENSE("GPL");
drivers/media/platform/amd/isp4/isp4.h new file +9 -0
new file mode 100644
index 000000000000..27a7362ce6f9
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_H_
+#define _ISP4_H_
+
+#include <linux/mutex.h>

drivers/media/platform/amd/isp4/isp4.h line 19

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.h new file +10 -0
+#include <media/v4l2-device.h>
+#include <media/videobuf2-memops.h>
+#include <media/videobuf2-vmalloc.h>
+
+#define ISP4_GET_ISP_REG_BASE(isp4sd) (((isp4sd))->mmio)
+
+struct isp4_platform_data {
+	void *adev;
+	void *bo;
+	void *cpu_ptr;

Will touch more on these in later patches, but I would say don't introduce them until the patch they're needed.

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.h new file +6 -0
+	u64 gpu_addr;
+	u32 size;
+	u32 asic_type;
+	resource_size_t base_rmmio_size;
+};
+

drivers/media/platform/amd/isp4/isp4.h line 35

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.h new file +10 -0
+struct isp4_device {
+	struct v4l2_device v4l2_dev;
+	struct media_device mdev;
+
+	struct isp4_platform_data *pltf_data;
+	struct platform_device *pdev;
+	struct notifier_block i2c_nb;
+};
+
+#endif /* isp4.h */

/* ISP4_H */

#9 lore raw

Re: [PATCH v2 2/8] media: platform: amd: low level support for isp4 firmware

In reply to Bin Du
Quoted 8 lines · Bin Du

Low level functions for access the registers and mapping to their ranges. This change also includes register definitions for ring buffer used to communicate with ISP Firmware.
Ring buffer is the communication interface between driver and ISP Firmware. Command and responses are exchanged through the ring buffer.

Same comment as first patch (and others), who wrote it? Is Co-developed-by missing?

Quoted 218 lines
---
  drivers/media/platform/amd/isp4/Makefile      |   3 +-
  drivers/media/platform/amd/isp4/isp4_hw.c     |  46 +++++++
  drivers/media/platform/amd/isp4/isp4_hw.h     |  14 +++
  drivers/media/platform/amd/isp4/isp4_hw_reg.h | 116 ++++++++++++++++++
  4 files changed, 178 insertions(+), 1 deletion(-)

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

drivers/media/platform/amd/isp4/Makefile +2 -1
index e9e84160517d..8ca1c4dfe246 100644
@@ -3,7 +3,8 @@
  # Copyright (C) 2025 Advanced Micro Devices, Inc.
 
  obj-$(CONFIG_AMD_ISP4) += amd_capture.o
-amd_capture-objs := isp4.o
+amd_capture-objs := isp4.o	\
+			isp4_hw.o	\
 
  ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
  ccflags-y += -I$(srctree)/include
drivers/media/platform/amd/isp4/isp4_hw.c new file +46 -0
new file mode 100644
index 000000000000..e5315330a514
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/io.h>
+#include <linux/types.h>
+
+#include "isp4_hw.h"
+#include "isp4_hw_reg.h"
+
+#define RMMIO_SIZE 524288
+
+u32 isp4hw_rreg(void __iomem *base, u32 reg)
+{
+	void __iomem *reg_addr;
+
+	if (reg >= RMMIO_SIZE)
+		return RREG_FAILED_VAL;
+
+	if (reg < ISP_MIPI_PHY0_REG0)
+		reg_addr = base + reg;
+	else if (reg <= ISP_MIPI_PHY0_REG0 + ISP_MIPI_PHY0_SIZE)
+		reg_addr = base + (reg - ISP_MIPI_PHY0_REG0);
+	else
+		return RREG_FAILED_VAL;
+
+	return readl(reg_addr);
+};
+
+void isp4hw_wreg(void __iomem *base, u32 reg, u32 val)
+{
+	void __iomem *reg_addr;
+
+	if (reg >= RMMIO_SIZE)
+		return;
+
+	if (reg < ISP_MIPI_PHY0_REG0)
+		reg_addr = base + reg;
+	else if (reg <= ISP_MIPI_PHY0_REG0 + ISP_MIPI_PHY0_SIZE)
+		reg_addr = base + (reg - ISP_MIPI_PHY0_REG0);
+	else
+		return;
+
+	writel(val, reg_addr);
+};
drivers/media/platform/amd/isp4/isp4_hw.h new file +14 -0
new file mode 100644
index 000000000000..072d135b9e3a
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_HW_H_
+#define _ISP4_HW_H_
+
+#define RREG_FAILED_VAL 0xFFFFFFFF
+
+u32 isp4hw_rreg(void __iomem *base, u32 reg);
+void isp4hw_wreg(void __iomem *base, u32 reg, u32 val);
+
+#endif
drivers/media/platform/amd/isp4/isp4_hw_reg.h new file +116 -0
new file mode 100644
index 000000000000..b11f12ba6c56
@@ -0,0 +1,116 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_HW_REG_H_
+#define _ISP4_HW_REG_H_
+
+#define ISP_SOFT_RESET		0x62000
+#define ISP_SYS_INT0_EN		0x62010
+#define ISP_SYS_INT0_STATUS	0x62014
+#define ISP_SYS_INT0_ACK	0x62018
+#define ISP_CCPU_CNTL		0x62054
+#define ISP_STATUS		0x62058
+#define ISP_LOG_RB_BASE_LO0	0x62148
+#define ISP_LOG_RB_BASE_HI0	0x6214C
+#define ISP_LOG_RB_SIZE0	0x62150
+#define ISP_LOG_RB_RPTR0	0x62154
+#define ISP_LOG_RB_WPTR0	0x62158
+#define ISP_RB_BASE_LO1		0x62170
+#define ISP_RB_BASE_HI1		0x62174
+#define ISP_RB_SIZE1		0x62178
+#define ISP_RB_RPTR1		0x6217C
+#define ISP_RB_WPTR1		0x62180
+#define ISP_RB_BASE_LO2		0x62184
+#define ISP_RB_BASE_HI2		0x62188
+#define ISP_RB_SIZE2		0x6218C
+#define ISP_RB_RPTR2		0x62190
+#define ISP_RB_WPTR2		0x62194
+#define ISP_RB_BASE_LO3		0x62198
+#define ISP_RB_BASE_HI3		0x6219C
+#define ISP_RB_SIZE3		0x621A0
+#define ISP_RB_RPTR3		0x621A4
+#define ISP_RB_WPTR3		0x621A8
+#define ISP_RB_BASE_LO4		0x621AC
+#define ISP_RB_BASE_HI4		0x621B0
+#define ISP_RB_SIZE4		0x621B4
+#define ISP_RB_RPTR4		0x621B8
+#define ISP_RB_WPTR4		0x621BC
+#define ISP_RB_BASE_LO5		0x621C0
+#define ISP_RB_BASE_HI5		0x621C4
+#define ISP_RB_SIZE5		0x621C8
+#define ISP_RB_RPTR5		0x621CC
+#define ISP_RB_WPTR5		0x621D0
+#define ISP_RB_BASE_LO6		0x621D4
+#define ISP_RB_BASE_HI6		0x621D8
+#define ISP_RB_SIZE6		0x621DC
+#define ISP_RB_RPTR6		0x621E0
+#define ISP_RB_WPTR6		0x621E4
+#define ISP_RB_BASE_LO7		0x621E8
+#define ISP_RB_BASE_HI7		0x621EC
+#define ISP_RB_SIZE7		0x621F0
+#define ISP_RB_RPTR7		0x621F4
+#define ISP_RB_WPTR7		0x621F8
+#define ISP_RB_BASE_LO8		0x621FC
+#define ISP_RB_BASE_HI8		0x62200
+#define ISP_RB_SIZE8		0x62204
+#define ISP_RB_RPTR8		0x62208
+#define ISP_RB_WPTR8		0x6220C
+#define ISP_RB_BASE_LO9		0x62210
+#define ISP_RB_BASE_HI9		0x62214
+#define ISP_RB_SIZE9		0x62218
+#define ISP_RB_RPTR9		0x6221C
+#define ISP_RB_WPTR9		0x62220
+#define ISP_RB_BASE_LO10	0x62224
+#define ISP_RB_BASE_HI10	0x62228
+#define ISP_RB_SIZE10		0x6222C
+#define ISP_RB_RPTR10		0x62230
+#define ISP_RB_WPTR10		0x62234
+#define ISP_RB_BASE_LO11	0x62238
+#define ISP_RB_BASE_HI11	0x6223C
+#define ISP_RB_SIZE11		0x62240
+#define ISP_RB_RPTR11		0x62244
+#define ISP_RB_WPTR11		0x62248
+#define ISP_RB_BASE_LO12	0x6224C
+#define ISP_RB_BASE_HI12	0x62250
+#define ISP_RB_SIZE12		0x62254
+#define ISP_RB_RPTR12		0x62258
+#define ISP_RB_WPTR12		0x6225C
+
+#define ISP_POWER_STATUS	0x60000
+
+#define ISP_MIPI_PHY0_REG0	0x66700
+#define ISP_MIPI_PHY1_REG0	0x66780
+#define ISP_MIPI_PHY2_REG0	0x67400
+
+#define ISP_MIPI_PHY0_SIZE	0xD30
+
+/* ISP_SOFT_RESET */
+#define ISP_SOFT_RESET__CCPU_SOFT_RESET_MASK			0x00000001UL
+
+/* ISP_CCPU_CNTL */
+#define ISP_CCPU_CNTL__CCPU_HOST_SOFT_RST_MASK			0x00040000UL
+
+/* ISP_STATUS */
+#define ISP_STATUS__CCPU_REPORT_MASK				0x000000feUL
+
+/* ISP_SYS_INT0_STATUS */
+#define ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT9_INT_MASK	0x00010000UL
+#define ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT10_INT_MASK	0x00040000UL
+#define ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT11_INT_MASK	0x00100000UL
+#define ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT12_INT_MASK	0x00400000UL
+
+/* ISP_SYS_INT0_EN */
+#define ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT9_EN_MASK	0x00010000UL
+#define ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT10_EN_MASK	0x00040000UL
+#define ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT11_EN_MASK	0x00100000UL
+#define ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT12_EN_MASK	0x00400000UL
+
+/* ISP_SYS_INT0_ACK */
+#define ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT9_ACK_MASK	0x00010000UL
+#define ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT10_ACK_MASK	0x00040000UL
+#define ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT11_ACK_MASK	0x00100000UL
+#define ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT12_ACK_MASK	0x00400000UL
+
+#endif
#23 lore raw

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

In reply to Bin Du

+Alex
+amd-gfx

Quoted 39 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.

---
  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 +3 -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

drivers/media/platform/amd/isp4/Makefile line 23

Quoted 10 lines
drivers/media/platform/amd/isp4/Makefile +9 -0
+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

IMO this feels like a hack and also fragile to be sharing so much across subsystems.

Either there should be a kernel wide include/ header that can be used by both or there should be a helper exported to get just the data that is needed.

Quoted 9 lines
drivers/media/platform/amd/isp4/Makefile +0 -0
  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 +0 -0
new file mode 100644
index 000000000000..437d89469af2

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

Quoted 9 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +9 -0
@@ -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_
+
+/*

Did you mean this to be kernel doc? If so you need:

/**

Quoted 23 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +23 -0
+ * @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       |<----  |          |
+ *|         |       --------------------------       |          |
+ *|         |                                        |          |

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

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +10 -0
+ *|         |                                        |          |
+ *|         |       --------------------------       |          |
+ *|         |  <----|  Stream Response       |<----  |          |
+ *|         |       --------------------------       |          |
+ *|         |                                        |          |
+ *|         |                                        |          |
+ *-----------                                        ------------
+ */
+
+/*

Same comment about kernel doc

Quoted 23 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +23 -0
+ * @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.
+ *        |<-Bit31 ~ Bit24->|<-Bit23 ~ Bit16->|<-Bit15 ~ Bit0->|
+ *        |      type       |      group      |       id       |
+ */
+
+#define CMD_TYPE_SHIFT (24)
+#define CMD_GROUP_SHIFT (16)
+#define CMD_TYPE_STREAM_CTRL ((u32)0x2 << CMD_TYPE_SHIFT)
+
+#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)

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

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +10 -0
+#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)
+
+/*

Same comment about kernel doc

Quoted 11 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +11 -0
+ * @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)

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

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +10 -0
+
+#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 */

Extra whitespace between words

Quoted 378 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +222 -0
+#define RESP_ID_CMD_DONE (RESP_GROUP_GENERAL | 0x1)
+
+/* 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)
+
+#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 +150 -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)
+{
+	struct isp4if_gpu_mem_info *mem_info;
+	struct amdgpu_bo *bo = NULL;
+	struct amdgpu_device *adev;
+	struct device *dev;
+
+	void *cpu_ptr;
+	u64 gpu_addr;
+	u32 ret;
+
+	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;
+	mem_info->mem_size = mem_size;

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

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +10 -0
+	mem_info->mem_align = ISP4IF_ISP_MC_ADDR_ALIGN;
+	mem_info->mem_domain = AMDGPU_GEM_DOMAIN_GTT;
+
+	ret = amdgpu_bo_create_kernel(adev,
+				      mem_info->mem_size,
+				      mem_info->mem_align,
+				      mem_info->mem_domain,
+				      &bo,
+				      &gpu_addr,
+				      &cpu_ptr);

IMO this is begging for an extra helper that has a true exported symbol and kernel-wide header for use rather than hacking around the includes for drm headers.

The kernel-wide header can describe the data types exchanged, so there wouldn't be a need to have all the amdgpu headers included.

Quoted 18 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +18 -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;

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

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

This also I think needs another helper

Quoted 56 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +56 -0
+
+	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;
+}
+
+static int isp4if_alloc_fw_gpumem(struct isp4_interface *ispif)
+{
+	struct device *dev = ispif->dev;
+	int i;
+
+	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] =

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

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +10 -0
+			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");

I don't think the error message is necessary, checkpatch usually complains about this.

Quoted 968 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +798 -0
+	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;
+
+	copy_command = kmalloc(sizeof(*copy_command), GFP_KERNEL);
+	if (!copy_command)
+		return NULL;
+
+	memcpy(copy_command, cmd_ele, sizeof(*copy_command));
+
+	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);
+			dev_err(dev, "fail to get cmdq slot,stream (%d), rd %u, wr %u\n",
+				stream, rd_ptr, wr_ptr);
+			return -ETIMEDOUT;
+		}
+		break;
+	}
+
+	cmd.cmd_id = cmd_id;
+	switch (stream) {
+	case ISP4IF_STREAM_ID_GLOBAL:
+		cmd.cmd_stream_id = (u16)STREAM_ID_INVALID;
+		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);
+		}
+	}
+
+	return ret;
+}
+
+static int isp4if_send_buffer(struct isp4_interface *ispif,
+			      struct isp4if_img_buf_info *buf_info)
+{
+	struct isp4fw_cmd_send_buffer cmd;
+
+	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);
+		if (reg_val & ISP_STATUS__CCPU_REPORT_MASK)
+			return 0;
+
+		msleep(interval_ms);
+		timeout++;
+	}
+
+	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;
+			}
+		}
+	} else {
+		return -ETIME;
+	}
+
+	checksum = isp4if_compute_check_sum((u8 *)response,
+					    (sizeof(struct isp4fw_resp) - 4));
+
+	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,
+			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);
+
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+int isp4if_send_command(struct isp4_interface *ispif,
+			u32 cmd_id,
+			void *package,
+			u32 package_size)
+{
+	return isp4if_send_fw_cmd(ispif,
+				  cmd_id, package,
+				  package_size, NULL, NULL, NULL);
+}
+
+int isp4if_send_command_sync(struct isp4_interface *ispif,
+			     u32 cmd_id,
+			     void *package,
+			     u32 package_size,
+			     u32 timeout)
+{
+	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,
+				 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));
+
+	/* timeout occurred */
+	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;
+	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) {
+		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;
+
+	guard(mutex)(&ispif->bufq_mutex);
+
+	buf_node = list_first_entry_or_null(&ispif->bufq,
+					    typeof(*buf_node),
+					    node);
+	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)
+		goto failed_fw_boot;
+
+	return 0;
+
+failed_gpumem_alloc:
+	return -ENOMEM;
+
+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 +164 -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;
+	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);
+	if (hi)
+		*hi = (u32)(addr >> 32);
+}
+
+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
#38 lore raw

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

In reply to Bin Du
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
#3 lore raw

Re: [PATCH v2 1/8] media: platform: amd: Introduce amd isp4 capture driver

In reply to Mario Limonciello

Many thx Mario for your comments, really helpful, will address all of them in the next patch. Add inline for some of your comments, pls check

Quoted 128 lines · Mario Limonciello
Quoted 1 line · Bin Du

Amd isp4 capture is a v4l2 media device which implements media controller

AMD

Quoted 2 lines

interface.
It has one sub-device (amd ISP4 sub-device) endpoint which can be connected

AMD

Quoted 3 lines

to a remote CSI2 TX endpoint. It supports only one physical interface for now.
Also add ISP4 driver related entry info into the MAINAINERS file

MAINTAINERS

Quoted 3 lines

Who actually developed?  If both are developers there should be a Co- developed-by tag.

Quoted 52 lines

---
  MAINTAINERS                              |  10 ++ drivers/media/platform/Kconfig           |   1 + drivers/media/platform/Makefile          |   1 + drivers/media/platform/amd/Kconfig       |  17 +++ drivers/media/platform/amd/Makefile      |   5 + drivers/media/platform/amd/isp4/Makefile |  21 ++++ drivers/media/platform/amd/isp4/isp4.c   | 139 +++++++++++++++++++++++ drivers/media/platform/amd/isp4/isp4.h   |  35 ++++++

  8 files changed, 229 insertions(+)

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

MAINTAINERS +0 -0
index 10893c91b1c1..15070afb14b5 100644
@@ -1107,6 +1107,16 @@ T:    git git://git.kernel.org/pub/scm/linux/ 

kernel/git/iommu/linux.git
  F:    drivers/iommu/amd/
  F:    include/linux/amd-iommu.h

MAINTAINERS +10 -0
+AMD ISP4 DRIVER
+M:    Bin Du <[email protected]>
+M:    Nirujogi Pratap <[email protected]>
+L:    [email protected]
+S:    Maintained
+T:    git git://linuxtv.org/media.git
+F:    drivers/media/platform/amd/Kconfig
+F:    drivers/media/platform/amd/Makefile
+F:    drivers/media/platform/amd/isp4/*
+

  AMD KFD
  M:    Felix Kuehling <[email protected]>
  L:    [email protected]

drivers/media/platform/Kconfig +0 -0
index 85d2627776b6..d525c2262a7d 100644
@@ -89,5 +89,6 @@ source "drivers/media/platform/ti/Kconfig"

  source "drivers/media/platform/verisilicon/Kconfig" source "drivers/media/platform/via/Kconfig" source "drivers/media/platform/xilinx/Kconfig" +source "drivers/media/platform/amd/Kconfig" endif # MEDIA_PLATFORM_DRIVERS

drivers/media/platform/Makefile +0 -0
index ace4e34483dd..9f3d1693868d 100644
@@ -32,6 +32,7 @@ obj-y += ti/
Quoted 4 lines

  obj-y += verisilicon/
  obj-y += via/
  obj-y += xilinx/
+obj-y += amd/

Is this whole file alphabetical?  If so this is the wrong place.

drivers/media/platform/amd/Kconfig line 5

Quoted 13 lines

  # Please place here only ancillary drivers that aren't SoC-specific # Please keep it alphabetically sorted by Kconfig name

drivers/media/platform/amd/Kconfig new file +5 -0
new file mode 100644
index 000000000000..3b1dba0400a0
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: MIT
+
+config AMD_ISP4
+    tristate "AMD ISP4 and camera driver"
+    default y

I don't believe this should default 'y'.  Normally drivers need to be opt in.

Quoted 18 lines
drivers/media/platform/amd/Kconfig new file +12 -0
+    depends on VIDEO_DEV && VIDEO_V4L2_SUBDEV_API
+    select VIDEOBUF2_CORE
+    select VIDEOBUF2_V4L2
+    select VIDEOBUF2_MEMOPS
+    select VIDEOBUF2_VMALLOC
+    select VIDEOBUF2_DMA_CONTIG
+    select VIDEOBUF2_DMA_SG
+    help
+      This is support for AMD ISP4 and camera subsystem driver.
+      Say Y here to enable the ISP4 and camera device for video capture.
+      To compile this driver as a module, choose M here. The module will
+      be called amd_capture.
drivers/media/platform/amd/Makefile new file +0 -0
new file mode 100644
index 000000000000..76146efcd2bf

drivers/media/platform/amd/Makefile line 1

Quoted 1 line
drivers/media/platform/amd/Makefile new file +1 -0
@@ -0,0 +1,5 @@
+# Copyright 2024 Advanced Micro Devices, Inc.

2025

Quoted 9 lines
drivers/media/platform/amd/Makefile new file +4 -0
+# add isp block
+ifneq ($(CONFIG_AMD_ISP4),)
+obj-y += isp4/
+endif
drivers/media/platform/amd/isp4/Makefile new file +0 -0
new file mode 100644
index 000000000000..e9e84160517d
Quoted 9 lines

drivers/media/platform/amd/isp4/Makefile line 5

Quoted 6 lines
drivers/media/platform/amd/isp4/Makefile new file +5 -0
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2025 Advanced Micro Devices, Inc.
+
+obj-$(CONFIG_AMD_ISP4) += amd_capture.o

As the directory is already conditional on CONFIG_AMD_ISP4 is this obj- $() needed?  Or should it really be obj-y?

Yes, it is needed, because AMD_ISP4 is trisate in Kconfig, it can be y or m

Quoted 19 lines
Quoted 6 lines
drivers/media/platform/amd/isp4/Makefile new file +6 -0
+amd_capture-objs := isp4.o
+
+ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
+ccflags-y += -I$(srctree)/include
+
+ifneq ($(call cc-option, -mpreferred-stack-boundary=4),)

drivers/media/platform/amd/isp4/Makefile line 21

Quoted 10 lines
drivers/media/platform/amd/isp4/Makefile new file +10 -0
+    cc_stack_align := -mpreferred-stack-boundary=4
+endif
+
+ccflags-y += $(cc_stack_align)
+ccflags-y += -DCONFIG_COMPAT
+ccflags-y += -Wunused-but-set-variable
+ccflags-y += -Wmissing-include-dirs
+ccflags-y += -Wunused-const-variable
+ccflags-y += -Wmaybe-uninitialized
+ccflags-y += -Wunused-value

Do you really need to enforce all these flags just for this driver?

Quoted 2 lines

Was this just for development to avoid having to remember to call the build with W=1 or CONFIG_WERROR?

We found after patch submission, Media CI robot test will be triggered, when it builds the patch it will set these flags, so adding these flags to align with Media CI robot test to discover potential issue before submission.

Quoted 220 lines
Quoted 32 lines
drivers/media/platform/amd/isp4/isp4.c new file +25 -0
new file mode 100644
index 000000000000..d0be90c5ec3b
@@ -0,0 +1,139 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/pm_runtime.h>
+#include <linux/vmalloc.h>
+#include <media/v4l2-ioctl.h>
+
+#include "isp4.h"
+
+#define VIDEO_BUF_NUM 5
+
+#define ISP4_DRV_NAME "amd_isp_capture"
+
+/* interrupt num */
+static const u32 isp4_ringbuf_interrupt_num[] = {
+    0, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT9 */
+    1, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT10 */
+    3, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT11 */
+    4, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT12 */
+};
+
+#define to_isp4_device(dev) \
+    ((struct isp4_device *)container_of(dev, struct isp4_device, 

v4l2_dev))

drivers/media/platform/amd/isp4/isp4.c line 35

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+
+static irqreturn_t isp4_irq_handler(int irq, void *arg)
+{
+    struct isp4_device *isp_dev = dev_get_drvdata((struct device *)arg);
+
+    if (!isp_dev)
+        goto error_drv_data;
+
+error_drv_data:
+    return IRQ_HANDLED;

In patch 5 you change this function, including dropping the goto and label.

So I suggest that for patch 1 you KISS:

static irqreturn_t isp4_irq_handler(int irq, void *arg) {
    return IRQ_HANDLED;
}

Then in patch 5 add the extra conditional code and real handling.

drivers/media/platform/amd/isp4/isp4.c line 40

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c new file +5 -0
+}
+
+/*
+ * amd capture module
+ */

Pointless comment, no?

drivers/media/platform/amd/isp4/isp4.c line 45

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c new file +5 -0
+static int isp4_capture_probe(struct platform_device *pdev)
+{
+    struct device *dev = &pdev->dev;
+    struct isp4_device *isp_dev;
+

Why newline here?

Quoted 15 lines

+    int i, irq, ret;

drivers/media/platform/amd/isp4/isp4.c new file +14 -0
+
+    isp_dev = devm_kzalloc(&pdev->dev, sizeof(*isp_dev), GFP_KERNEL);
+    if (!isp_dev)
+        return -ENOMEM;
+
+    isp_dev->pdev = pdev;
+    dev->init_name = ISP4_DRV_NAME;
+
+    for (i = 0; i < ARRAY_SIZE(isp4_ringbuf_interrupt_num); i++) {
+        irq = platform_get_irq(pdev, isp4_ringbuf_interrupt_num[i]);
+        if (irq < 0)
+            return dev_err_probe(dev, -ENODEV,
+                         "fail to get irq %d\n",
+                         isp4_ringbuf_interrupt_num[i]);

drivers/media/platform/amd/isp4/isp4.c line 70

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+        ret = devm_request_irq(&pdev->dev, irq, isp4_irq_handler, 0,
+                       "ISP_IRQ", &pdev->dev);
+        if (ret)
+            return dev_err_probe(dev, ret, "fail to req irq %d\n",
+                         irq);
+    }
+
+    isp_dev->pltf_data = pdev->dev.platform_data;
+
+    dev_dbg(dev, "isp irq registration successful\n");

As you have error handling in place with dev_err_probe() I think the lack of an error implies this message.  I'd say drop it.

Quoted 11 lines
drivers/media/platform/amd/isp4/isp4.c new file +11 -0
+
+    /* Link the media device within the v4l2_device */
+    isp_dev->v4l2_dev.mdev = &isp_dev->mdev;
+
+    /* Initialize media device */
+    strscpy(isp_dev->mdev.model, "amd_isp41_mdev",
+        sizeof(isp_dev->mdev.model));
+    snprintf(isp_dev->mdev.bus_info, sizeof(isp_dev->mdev.bus_info),
+         "platform:%s", ISP4_DRV_NAME);
+    isp_dev->mdev.dev = &pdev->dev;
+    media_device_init(&isp_dev->mdev);

drivers/media/platform/amd/isp4/isp4.c line 91

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+
+    /* register v4l2 device */
+    snprintf(isp_dev->v4l2_dev.name, sizeof(isp_dev->v4l2_dev.name),
+         "AMD-V4L2-ROOT");
+    ret = v4l2_device_register(&pdev->dev, &isp_dev->v4l2_dev);
+    if (ret)
+        return dev_err_probe(dev, ret,
+                     "fail register v4l2 device\n");
+
+    dev_dbg(dev, "AMD ISP v4l2 device registered\n");

As you have error handling in place with dev_err_probe() I think the lack of an error implies this message.  I'd say drop it.

Quoted 18 lines
drivers/media/platform/amd/isp4/isp4.c new file +18 -0
+
+    ret = media_device_register(&isp_dev->mdev);
+    if (ret) {
+        dev_err(dev, "fail to register media device %d\n", ret);
+        goto err_unreg_v4l2;
+    }
+
+    platform_set_drvdata(pdev, isp_dev);
+
+    pm_runtime_set_suspended(dev);
+    pm_runtime_enable(dev);
+
+    return 0;
+
+err_unreg_v4l2:
+    v4l2_device_unregister(&isp_dev->v4l2_dev);
+
+    return dev_err_probe(dev, ret, "isp probe fail\n");

drivers/media/platform/amd/isp4/isp4.c line 119

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+}
+
+static void isp4_capture_remove(struct platform_device *pdev)
+{
+    struct isp4_device *isp_dev = platform_get_drvdata(pdev);
+    struct device *dev = &pdev->dev;
+
+    media_device_unregister(&isp_dev->mdev);
+    v4l2_device_unregister(&isp_dev->v4l2_dev);
+    dev_dbg(dev, "AMD ISP v4l2 device unregistered\n");

Probably not needed message anymore, right?

Quoted 35 lines
drivers/media/platform/amd/isp4/isp4.c new file +20 -0
+}
+
+static struct platform_driver isp4_capture_drv = {
+    .probe = isp4_capture_probe,
+    .remove = isp4_capture_remove,
+    .driver = {
+        .name = ISP4_DRV_NAME,
+        .owner = THIS_MODULE,
+    }
+};
+
+module_platform_driver(isp4_capture_drv);
+
+MODULE_ALIAS("platform:" ISP4_DRV_NAME);
+MODULE_IMPORT_NS("DMA_BUF");
+
+MODULE_DESCRIPTION("AMD ISP4 Driver");
+MODULE_AUTHOR("Bin Du <[email protected]>");
+MODULE_AUTHOR("Pratap Nirujogi <[email protected]>");
+MODULE_LICENSE("GPL");
drivers/media/platform/amd/isp4/isp4.h new file +9 -0
new file mode 100644
index 000000000000..27a7362ce6f9
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_H_
+#define _ISP4_H_
+
+#include <linux/mutex.h>

drivers/media/platform/amd/isp4/isp4.h line 19

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.h new file +10 -0
+#include <media/v4l2-device.h>
+#include <media/videobuf2-memops.h>
+#include <media/videobuf2-vmalloc.h>
+
+#define ISP4_GET_ISP_REG_BASE(isp4sd) (((isp4sd))->mmio)
+
+struct isp4_platform_data {
+    void *adev;
+    void *bo;
+    void *cpu_ptr;

Will touch more on these in later patches, but I would say don't introduce them until the patch they're needed.

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.h new file +6 -0
+    u64 gpu_addr;
+    u32 size;
+    u32 asic_type;
+    resource_size_t base_rmmio_size;
+};
+

drivers/media/platform/amd/isp4/isp4.h line 35

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.h new file +10 -0
+struct isp4_device {
+    struct v4l2_device v4l2_dev;
+    struct media_device mdev;
+
+    struct isp4_platform_data *pltf_data;
+    struct platform_device *pdev;
+    struct notifier_block i2c_nb;
+};
+
+#endif /* isp4.h */

/* ISP4_H */

author maintainer
#10 lore raw

Re: [PATCH v2 2/8] media: platform: amd: low level support for isp4 firmware

In reply to Mario Limonciello

Thanks Mario, will add the Co-developed-by in all patches in the series.

Quoted 240 lines · Mario Limonciello
Quoted 8 lines · Bin Du

Low level functions for access the registers and mapping to their ranges. This change also includes register definitions for ring buffer used to communicate with ISP Firmware.
Ring buffer is the communication interface between driver and ISP Firmware. Command and responses are exchanged through the ring buffer.

Same comment as first patch (and others), who wrote it?  Is Co- developed-by missing?

Quoted 227 lines

---
  drivers/media/platform/amd/isp4/Makefile      |   3 +- drivers/media/platform/amd/isp4/isp4_hw.c     |  46 +++++++ drivers/media/platform/amd/isp4/isp4_hw.h     |  14 +++ drivers/media/platform/amd/isp4/isp4_hw_reg.h | 116 ++++++++++++++++++

  4 files changed, 178 insertions(+), 1 deletion(-)

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

drivers/media/platform/amd/isp4/Makefile +0 -0
index e9e84160517d..8ca1c4dfe246 100644
@@ -3,7 +3,8 @@
   # Copyright (C) 2025 Advanced Micro Devices, Inc.

  obj-$(CONFIG_AMD_ISP4) += amd_capture.o

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

  ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4 ccflags-y += -I$(srctree)/include

drivers/media/platform/amd/isp4/isp4_hw.c new file +46 -0
new file mode 100644
index 000000000000..e5315330a514
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/io.h>
+#include <linux/types.h>
+
+#include "isp4_hw.h"
+#include "isp4_hw_reg.h"
+
+#define RMMIO_SIZE 524288
+
+u32 isp4hw_rreg(void __iomem *base, u32 reg)
+{
+    void __iomem *reg_addr;
+
+    if (reg >= RMMIO_SIZE)
+        return RREG_FAILED_VAL;
+
+    if (reg < ISP_MIPI_PHY0_REG0)
+        reg_addr = base + reg;
+    else if (reg <= ISP_MIPI_PHY0_REG0 + ISP_MIPI_PHY0_SIZE)
+        reg_addr = base + (reg - ISP_MIPI_PHY0_REG0);
+    else
+        return RREG_FAILED_VAL;
+
+    return readl(reg_addr);
+};
+
+void isp4hw_wreg(void __iomem *base, u32 reg, u32 val)
+{
+    void __iomem *reg_addr;
+
+    if (reg >= RMMIO_SIZE)
+        return;
+
+    if (reg < ISP_MIPI_PHY0_REG0)
+        reg_addr = base + reg;
+    else if (reg <= ISP_MIPI_PHY0_REG0 + ISP_MIPI_PHY0_SIZE)
+        reg_addr = base + (reg - ISP_MIPI_PHY0_REG0);
+    else
+        return;
+
+    writel(val, reg_addr);
+};
drivers/media/platform/amd/isp4/isp4_hw.h new file +14 -0
new file mode 100644
index 000000000000..072d135b9e3a
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_HW_H_
+#define _ISP4_HW_H_
+
+#define RREG_FAILED_VAL 0xFFFFFFFF
+
+u32 isp4hw_rreg(void __iomem *base, u32 reg);
+void isp4hw_wreg(void __iomem *base, u32 reg, u32 val);
+
+#endif
drivers/media/platform/amd/isp4/isp4_hw_reg.h new file +99 -0
new file mode 100644
index 000000000000..b11f12ba6c56
@@ -0,0 +1,116 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_HW_REG_H_
+#define _ISP4_HW_REG_H_
+
+#define ISP_SOFT_RESET        0x62000
+#define ISP_SYS_INT0_EN        0x62010
+#define ISP_SYS_INT0_STATUS    0x62014
+#define ISP_SYS_INT0_ACK    0x62018
+#define ISP_CCPU_CNTL        0x62054
+#define ISP_STATUS        0x62058
+#define ISP_LOG_RB_BASE_LO0    0x62148
+#define ISP_LOG_RB_BASE_HI0    0x6214C
+#define ISP_LOG_RB_SIZE0    0x62150
+#define ISP_LOG_RB_RPTR0    0x62154
+#define ISP_LOG_RB_WPTR0    0x62158
+#define ISP_RB_BASE_LO1        0x62170
+#define ISP_RB_BASE_HI1        0x62174
+#define ISP_RB_SIZE1        0x62178
+#define ISP_RB_RPTR1        0x6217C
+#define ISP_RB_WPTR1        0x62180
+#define ISP_RB_BASE_LO2        0x62184
+#define ISP_RB_BASE_HI2        0x62188
+#define ISP_RB_SIZE2        0x6218C
+#define ISP_RB_RPTR2        0x62190
+#define ISP_RB_WPTR2        0x62194
+#define ISP_RB_BASE_LO3        0x62198
+#define ISP_RB_BASE_HI3        0x6219C
+#define ISP_RB_SIZE3        0x621A0
+#define ISP_RB_RPTR3        0x621A4
+#define ISP_RB_WPTR3        0x621A8
+#define ISP_RB_BASE_LO4        0x621AC
+#define ISP_RB_BASE_HI4        0x621B0
+#define ISP_RB_SIZE4        0x621B4
+#define ISP_RB_RPTR4        0x621B8
+#define ISP_RB_WPTR4        0x621BC
+#define ISP_RB_BASE_LO5        0x621C0
+#define ISP_RB_BASE_HI5        0x621C4
+#define ISP_RB_SIZE5        0x621C8
+#define ISP_RB_RPTR5        0x621CC
+#define ISP_RB_WPTR5        0x621D0
+#define ISP_RB_BASE_LO6        0x621D4
+#define ISP_RB_BASE_HI6        0x621D8
+#define ISP_RB_SIZE6        0x621DC
+#define ISP_RB_RPTR6        0x621E0
+#define ISP_RB_WPTR6        0x621E4
+#define ISP_RB_BASE_LO7        0x621E8
+#define ISP_RB_BASE_HI7        0x621EC
+#define ISP_RB_SIZE7        0x621F0
+#define ISP_RB_RPTR7        0x621F4
+#define ISP_RB_WPTR7        0x621F8
+#define ISP_RB_BASE_LO8        0x621FC
+#define ISP_RB_BASE_HI8        0x62200
+#define ISP_RB_SIZE8        0x62204
+#define ISP_RB_RPTR8        0x62208
+#define ISP_RB_WPTR8        0x6220C
+#define ISP_RB_BASE_LO9        0x62210
+#define ISP_RB_BASE_HI9        0x62214
+#define ISP_RB_SIZE9        0x62218
+#define ISP_RB_RPTR9        0x6221C
+#define ISP_RB_WPTR9        0x62220
+#define ISP_RB_BASE_LO10    0x62224
+#define ISP_RB_BASE_HI10    0x62228
+#define ISP_RB_SIZE10        0x6222C
+#define ISP_RB_RPTR10        0x62230
+#define ISP_RB_WPTR10        0x62234
+#define ISP_RB_BASE_LO11    0x62238
+#define ISP_RB_BASE_HI11    0x6223C
+#define ISP_RB_SIZE11        0x62240
+#define ISP_RB_RPTR11        0x62244
+#define ISP_RB_WPTR11        0x62248
+#define ISP_RB_BASE_LO12    0x6224C
+#define ISP_RB_BASE_HI12    0x62250
+#define ISP_RB_SIZE12        0x62254
+#define ISP_RB_RPTR12        0x62258
+#define ISP_RB_WPTR12        0x6225C
+
+#define ISP_POWER_STATUS    0x60000
+
+#define ISP_MIPI_PHY0_REG0    0x66700
+#define ISP_MIPI_PHY1_REG0    0x66780
+#define ISP_MIPI_PHY2_REG0    0x67400
+
+#define ISP_MIPI_PHY0_SIZE    0xD30
+
+/* ISP_SOFT_RESET */
+#define ISP_SOFT_RESET__CCPU_SOFT_RESET_MASK            0x00000001UL
+
+/* ISP_CCPU_CNTL */
+#define ISP_CCPU_CNTL__CCPU_HOST_SOFT_RST_MASK            0x00040000UL
+
+/* ISP_STATUS */
+#define ISP_STATUS__CCPU_REPORT_MASK                0x000000feUL
+
+/* ISP_SYS_INT0_STATUS */
+#define ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT9_INT_MASK    

0x00010000UL
+#define ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT10_INT_MASK 0x00040000UL
+#define ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT11_INT_MASK 0x00100000UL
+#define ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT12_INT_MASK 0x00400000UL

drivers/media/platform/amd/isp4/isp4_hw_reg.h new file +4 -0
+
+/* ISP_SYS_INT0_EN */
+#define ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT9_EN_MASK    0x00010000UL
+#define ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT10_EN_MASK    

0x00040000UL
+#define ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT11_EN_MASK 0x00100000UL
+#define ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT12_EN_MASK 0x00400000UL

drivers/media/platform/amd/isp4/isp4_hw_reg.h new file +3 -0
+
+/* ISP_SYS_INT0_ACK */
+#define ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT9_ACK_MASK    

0x00010000UL
+#define ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT10_ACK_MASK 0x00040000UL
+#define ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT11_ACK_MASK 0x00100000UL
+#define ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT12_ACK_MASK 0x00400000UL

drivers/media/platform/amd/isp4/isp4_hw_reg.h new file +2 -0
+
+#endif
author maintainer
#24 lore raw

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

In reply to Mario Limonciello

Thanks Mario, will fix in the next version and pls see some of my comments

Quoted 55 lines · Mario Limonciello
(quoted diff) +2 -0
+Alex
+amd-gfx
Quoted 41 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.

---
  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 +0 -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

drivers/media/platform/amd/isp4/Makefile +4 -0
+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

drivers/media/platform/amd/isp4/Makefile line 23

Quoted 7 lines
drivers/media/platform/amd/isp4/Makefile +7 -0
+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

IMO this feels like a hack and also fragile to be sharing so much across subsystems.

Quoted 1 line

Either there should be a kernel wide include/ header that can be used by both or there should be a helper exported to get just the data that is needed.

Yes, will refine to remove unnecessary ones in the next version, actually isp driver needs to access function amdgpu_bo_create_kernel which is exported by amdgpu and delared in amdgpu_object.h, because amdgpu_object.h also includes other amd gpu header files, so have to add these include path to avoid compilation error. It'll be greate if there is kernel wide include, any suggestion for this?

Quoted 21 lines
Quoted 9 lines

  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 +0 -0
new file mode 100644
index 000000000000..437d89469af2

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

Quoted 9 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +9 -0
@@ -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_
+
+/*

Did you mean this to be kernel doc?  If so you need:

Quoted 2 lines

/**

Many thx, will update in the next patch

Quoted 27 lines
Quoted 27 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +27 -0
+ * @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       |<----  |          |
+ *|         |       --------------------------       |          |
Quoted 9 lines

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

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +6 -0
+ *|         |                                        |          |
+ *|         |                                        |          |
+ *-----------                                        ------------
+ */
+
+/*

Same comment about kernel doc

Many thx, will update in the next patch>> + * @brief command ID format

Quoted 26 lines
Quoted 26 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +26 -0
+ *        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.
+ *        |<-Bit31 ~ Bit24->|<-Bit23 ~ Bit16->|<-Bit15 ~ Bit0->|
+ *        |      type       |      group      |       id       |
+ */
+
+#define CMD_TYPE_SHIFT (24)
+#define CMD_GROUP_SHIFT (16)
+#define CMD_TYPE_STREAM_CTRL ((u32)0x2 << CMD_TYPE_SHIFT)
+
+#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)
Quoted 9 lines

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

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +6 -0
+
+/* Stream Buffer Command */
+#define CMD_ID_SEND_BUFFER                                \
+    (CMD_TYPE_STREAM_CTRL | CMD_GROUP_STREAM_BUFFER | 0x1)
+
+/*

Same comment about kernel doc

Many thx, will update in the next patch>> + * @brief response ID format

Quoted 13 lines
Quoted 13 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +13 -0
+ *        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) >> \
Quoted 8 lines

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

Quoted 7 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +7 -0
+     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 */

Extra whitespace between words

Many thx, will update in the next patch

Quoted 399 lines
Quoted 386 lines
drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +20 -0
+#define RESP_ID_CMD_DONE (RESP_GROUP_GENERAL | 0x1)
+
+/* 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)
+
+#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 */

drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +34 -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 */

drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +41 -0
+};
+
+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];

drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +4 -0
+};
+
+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. */

drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +84 -0
+};
+
+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 */

drivers/media/platform/amd/isp4/isp4_fw_cmd_resp.h new file +22 -0
+};
+
+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 +127 -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)

drivers/media/platform/amd/isp4/isp4_interface.c new file +21 -0
+{
+    struct isp4if_gpu_mem_info *mem_info;
+    struct amdgpu_bo *bo = NULL;
+    struct amdgpu_device *adev;
+    struct device *dev;
+
+    void *cpu_ptr;
+    u64 gpu_addr;
+    u32 ret;
+
+    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;
+    mem_info->mem_size = mem_size;

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

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +10 -0
+    mem_info->mem_align = ISP4IF_ISP_MC_ADDR_ALIGN;
+    mem_info->mem_domain = AMDGPU_GEM_DOMAIN_GTT;
+
+    ret = amdgpu_bo_create_kernel(adev,
+                      mem_info->mem_size,
+                      mem_info->mem_align,
+                      mem_info->mem_domain,
+                      &bo,
+                      &gpu_addr,
+                      &cpu_ptr);

IMO this is begging for an extra helper that has a true exported symbol and kernel-wide header for use rather than hacking around the includes for drm headers.

Quoted 1 line

The kernel-wide header can describe the data types exchanged, so there wouldn't be a need to have all the amdgpu headers included.

Many thanks, will double check. Seems amdgpu_bo_create_kernel is a true exported symbol from amdgpu. If there is kernel-wide header and functions to achieve the same, that'll be great. Any suggestions for this?

Quoted 29 lines
Quoted 29 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +28 -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;
+
+    if (!mem_info) {
+        dev_err(dev, "invalid mem_info\n");
+        return -EINVAL;
+    }
+
+    bo = (struct amdgpu_bo *)mem_info->mem_handle;
+
+    amdgpu_bo_free_kernel(&bo, &mem_info->gpu_mc_addr, &mem_info- 
Quoted 3 lines
Quoted 1 line
Quoted 1 line

sys_addr);

This also I think needs another helper

Many thx, same comments as above>

Quoted 61 lines
Quoted 61 lines
drivers/media/platform/amd/isp4/isp4_interface.c new file +41 -0
+
+    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;
+}
+
+static int isp4if_alloc_fw_gpumem(struct isp4_interface *ispif)
+{
+    struct device *dev = ispif->dev;
+    int i;
+
+    ispif->fw_mem_pool = isp4if_gpu_mem_alloc(ispif, 

FW_MEMORY_POOL_SIZE);
+    if (!ispif->fw_mem_pool)
+        goto error_no_memory;

drivers/media/platform/amd/isp4/isp4_interface.c new file +17 -0
+
+    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;
Quoted 8 lines

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

Quoted 6 lines

+    }

drivers/media/platform/amd/isp4/isp4_interface.c new file +5 -0
+
+    return 0;
+
+error_no_memory:
+    dev_err(dev, "failed to allocate gpu memory");

I don't think the error message is necessary, checkpatch usually complains about this.

Many thanks, actually checkpatch doesn't say anything about it. Sure we can remove it if you think it better.

Quoted 975 lines
Quoted 974 lines

+    return -ENOMEM;

drivers/media/platform/amd/isp4/isp4_interface.c new file +246 -0
+}
+
+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;
+
+    copy_command = kmalloc(sizeof(*copy_command), GFP_KERNEL);
+    if (!copy_command)
+        return NULL;
+
+    memcpy(copy_command, cmd_ele, sizeof(*copy_command));
+
+    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);
+            dev_err(dev, "fail to get cmdq slot,stream (%d), rd %u, 

wr %u\n",
+                stream, rd_ptr, wr_ptr);
+            return -ETIMEDOUT;
+        }
+        break;
+    }

drivers/media/platform/amd/isp4/isp4_interface.c new file +536 -0
+
+    cmd.cmd_id = cmd_id;
+    switch (stream) {
+    case ISP4IF_STREAM_ID_GLOBAL:
+        cmd.cmd_stream_id = (u16)STREAM_ID_INVALID;
+        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);
+        }
+    }
+
+    return ret;
+}
+
+static int isp4if_send_buffer(struct isp4_interface *ispif,
+                  struct isp4if_img_buf_info *buf_info)
+{
+    struct isp4fw_cmd_send_buffer cmd;
+
+    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);
+        if (reg_val & ISP_STATUS__CCPU_REPORT_MASK)
+            return 0;
+
+        msleep(interval_ms);
+        timeout++;
+    }
+
+    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;
+            }
+        }
+    } else {
+        return -ETIME;
+    }
+
+    checksum = isp4if_compute_check_sum((u8 *)response,
+                        (sizeof(struct isp4fw_resp) - 4));
+
+    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,
+            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);
+
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+int isp4if_send_command(struct isp4_interface *ispif,
+            u32 cmd_id,
+            void *package,
+            u32 package_size)
+{
+    return isp4if_send_fw_cmd(ispif,
+                  cmd_id, package,
+                  package_size, NULL, NULL, NULL);
+}
+
+int isp4if_send_command_sync(struct isp4_interface *ispif,
+                 u32 cmd_id,
+                 void *package,
+                 u32 package_size,
+                 u32 timeout)
+{
+    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,
+                 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));
+
+    /* timeout occurred */
+    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;
+    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) {
+        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;
+
+    guard(mutex)(&ispif->bufq_mutex);
+
+    buf_node = list_first_entry_or_null(&ispif->bufq,
+                        typeof(*buf_node),
+                        node);
+    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)
+        goto failed_fw_boot;
+
+    return 0;
+
+failed_gpumem_alloc:
+    return -ENOMEM;
+
+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 */

drivers/media/platform/amd/isp4/isp4_interface.c new file +6 -0
+
+    INIT_LIST_HEAD(&ispif->cmdq);
+    INIT_LIST_HEAD(&ispif->bufq);
+
+    return 0;
+}
drivers/media/platform/amd/isp4/isp4_interface.h new file +24 -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)

drivers/media/platform/amd/isp4/isp4_interface.h new file +126 -0
+#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;
+    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);
+    if (hi)
+        *hi = (u32)(addr >> 32);
+}
+
+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);

drivers/media/platform/amd/isp4/isp4_interface.h new file +14 -0
+
+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
#4 lore raw

Re: [PATCH v2 1/8] media: platform: amd: Introduce amd isp4 capture driver

In reply to Du, Bin
Quoted 143 lines · Bin

Many thx Mario for your comments, really helpful, will address all of them in the next patch. Add inline for some of your comments, pls check

Quoted 130 lines · Mario Limonciello
Quoted 1 line · Bin Du

Amd isp4 capture is a v4l2 media device which implements media controller

AMD

Quoted 2 lines

interface.
It has one sub-device (amd ISP4 sub-device) endpoint which can be connected

AMD

Quoted 3 lines

to a remote CSI2 TX endpoint. It supports only one physical interface for now.
Also add ISP4 driver related entry info into the MAINAINERS file

MAINTAINERS

Quoted 3 lines

Who actually developed?  If both are developers there should be a Co- developed-by tag.

Quoted 52 lines

---
  MAINTAINERS                              |  10 ++ drivers/media/platform/Kconfig           |   1 + drivers/media/platform/Makefile          |   1 + drivers/media/platform/amd/Kconfig       |  17 +++ drivers/media/platform/amd/Makefile      |   5 + drivers/media/platform/amd/isp4/Makefile |  21 ++++ drivers/media/platform/amd/isp4/isp4.c   | 139 +++++++++++++++++++++++ drivers/media/platform/amd/isp4/isp4.h   |  35 ++++++

  8 files changed, 229 insertions(+)

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

MAINTAINERS +0 -0
index 10893c91b1c1..15070afb14b5 100644
@@ -1107,6 +1107,16 @@ T:    git git://git.kernel.org/pub/scm/linux/ 

kernel/git/iommu/linux.git
  F:    drivers/iommu/amd/
  F:    include/linux/amd-iommu.h

MAINTAINERS +10 -0
+AMD ISP4 DRIVER
+M:    Bin Du <[email protected]>
+M:    Nirujogi Pratap <[email protected]>
+L:    [email protected]
+S:    Maintained
+T:    git git://linuxtv.org/media.git
+F:    drivers/media/platform/amd/Kconfig
+F:    drivers/media/platform/amd/Makefile
+F:    drivers/media/platform/amd/isp4/*
+

  AMD KFD
  M:    Felix Kuehling <[email protected]>
  L:    [email protected]

drivers/media/platform/Kconfig +0 -0
index 85d2627776b6..d525c2262a7d 100644
@@ -89,5 +89,6 @@ source "drivers/media/platform/ti/Kconfig"

  source "drivers/media/platform/verisilicon/Kconfig" source "drivers/media/platform/via/Kconfig" source "drivers/media/platform/xilinx/Kconfig" +source "drivers/media/platform/amd/Kconfig" endif # MEDIA_PLATFORM_DRIVERS

drivers/media/platform/Makefile +0 -0
index ace4e34483dd..9f3d1693868d 100644
@@ -32,6 +32,7 @@ obj-y += ti/
Quoted 4 lines

  obj-y += verisilicon/
  obj-y += via/
  obj-y += xilinx/
+obj-y += amd/

Is this whole file alphabetical?  If so this is the wrong place.

drivers/media/platform/amd/Kconfig line 5

Quoted 13 lines

  # Please place here only ancillary drivers that aren't SoC-specific # Please keep it alphabetically sorted by Kconfig name

drivers/media/platform/amd/Kconfig new file +5 -0
new file mode 100644
index 000000000000..3b1dba0400a0
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: MIT
+
+config AMD_ISP4
+    tristate "AMD ISP4 and camera driver"
+    default y

I don't believe this should default 'y'.  Normally drivers need to be opt in.

Quoted 20 lines
drivers/media/platform/amd/Kconfig new file +10 -0
+    depends on VIDEO_DEV && VIDEO_V4L2_SUBDEV_API
+    select VIDEOBUF2_CORE
+    select VIDEOBUF2_V4L2
+    select VIDEOBUF2_MEMOPS
+    select VIDEOBUF2_VMALLOC
+    select VIDEOBUF2_DMA_CONTIG
+    select VIDEOBUF2_DMA_SG
+    help
+      This is support for AMD ISP4 and camera subsystem driver.
+      Say Y here to enable the ISP4 and camera device for video 

capture.
+      To compile this driver as a module, choose M here. The module will
+      be called amd_capture.

drivers/media/platform/amd/Makefile new file +0 -0
new file mode 100644
index 000000000000..76146efcd2bf

drivers/media/platform/amd/Makefile line 1

Quoted 1 line
drivers/media/platform/amd/Makefile new file +1 -0
@@ -0,0 +1,5 @@
+# Copyright 2024 Advanced Micro Devices, Inc.

2025

Quoted 9 lines
drivers/media/platform/amd/Makefile new file +4 -0
+# add isp block
+ifneq ($(CONFIG_AMD_ISP4),)
+obj-y += isp4/
+endif
drivers/media/platform/amd/isp4/Makefile new file +0 -0
new file mode 100644
index 000000000000..e9e84160517d
Quoted 9 lines

drivers/media/platform/amd/isp4/Makefile line 5

Quoted 6 lines
drivers/media/platform/amd/isp4/Makefile new file +5 -0
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2025 Advanced Micro Devices, Inc.
+
+obj-$(CONFIG_AMD_ISP4) += amd_capture.o

As the directory is already conditional on CONFIG_AMD_ISP4 is this obj- $() needed?  Or should it really be obj-y?

Quoted 1 line

Yes, it is needed, because AMD_ISP4 is trisate in Kconfig, it can be y or m

Got it, thanks for clarification.

Quoted 20 lines
Quoted 20 lines
Quoted 7 lines
drivers/media/platform/amd/isp4/Makefile new file +6 -0
+amd_capture-objs := isp4.o
+
+ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
+ccflags-y += -I$(srctree)/include
+
+ifneq ($(call cc-option, -mpreferred-stack-boundary=4),)

drivers/media/platform/amd/isp4/Makefile line 21

Quoted 10 lines
drivers/media/platform/amd/isp4/Makefile new file +10 -0
+    cc_stack_align := -mpreferred-stack-boundary=4
+endif
+
+ccflags-y += $(cc_stack_align)
+ccflags-y += -DCONFIG_COMPAT
+ccflags-y += -Wunused-but-set-variable
+ccflags-y += -Wmissing-include-dirs
+ccflags-y += -Wunused-const-variable
+ccflags-y += -Wmaybe-uninitialized
+ccflags-y += -Wunused-value

Do you really need to enforce all these flags just for this driver?

Quoted 3 lines
Quoted 2 lines

Was this just for development to avoid having to remember to call the build with W=1 or CONFIG_WERROR?

We found after patch submission, Media CI robot test will be triggered, when it builds the patch it will set these flags, so adding these flags to align with Media CI robot test to discover potential issue before submission.

I believe you can just compile with CONFIG_WERROR and get same result, no? If I'm wrong, nonetheless this should be set in your external build environment script not in the Makefile to be going upstream.

Quoted 223 lines
Quoted 222 lines
Quoted 38 lines
drivers/media/platform/amd/isp4/isp4.c new file +25 -0
new file mode 100644
index 000000000000..d0be90c5ec3b
@@ -0,0 +1,139 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/pm_runtime.h>
+#include <linux/vmalloc.h>
+#include <media/v4l2-ioctl.h>
+
+#include "isp4.h"
+
+#define VIDEO_BUF_NUM 5
+
+#define ISP4_DRV_NAME "amd_isp_capture"
+
+/* interrupt num */
+static const u32 isp4_ringbuf_interrupt_num[] = {
+    0, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT9 */
+    1, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT10 */
+    3, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT11 */
+    4, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT12 */
+};
+
+#define to_isp4_device(dev) \
+    ((struct isp4_device *)container_of(dev, struct isp4_device, 

v4l2_dev))

drivers/media/platform/amd/isp4/isp4.c new file +4 -0
+
+static irqreturn_t isp4_irq_handler(int irq, void *arg)
+{
+    struct isp4_device *isp_dev = dev_get_drvdata((struct device 

*)arg);

drivers/media/platform/amd/isp4/isp4.c line 35

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.c new file +6 -0
+
+    if (!isp_dev)
+        goto error_drv_data;
+
+error_drv_data:
+    return IRQ_HANDLED;

In patch 5 you change this function, including dropping the goto and label.

So I suggest that for patch 1 you KISS:

static irqreturn_t isp4_irq_handler(int irq, void *arg) {
     return IRQ_HANDLED;
}

Then in patch 5 add the extra conditional code and real handling.

drivers/media/platform/amd/isp4/isp4.c line 40

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c new file +5 -0
+}
+
+/*
+ * amd capture module
+ */

Pointless comment, no?

drivers/media/platform/amd/isp4/isp4.c line 45

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c new file +5 -0
+static int isp4_capture_probe(struct platform_device *pdev)
+{
+    struct device *dev = &pdev->dev;
+    struct isp4_device *isp_dev;
+

Why newline here?

Quoted 15 lines

+    int i, irq, ret;

drivers/media/platform/amd/isp4/isp4.c new file +14 -0
+
+    isp_dev = devm_kzalloc(&pdev->dev, sizeof(*isp_dev), GFP_KERNEL);
+    if (!isp_dev)
+        return -ENOMEM;
+
+    isp_dev->pdev = pdev;
+    dev->init_name = ISP4_DRV_NAME;
+
+    for (i = 0; i < ARRAY_SIZE(isp4_ringbuf_interrupt_num); i++) {
+        irq = platform_get_irq(pdev, isp4_ringbuf_interrupt_num[i]);
+        if (irq < 0)
+            return dev_err_probe(dev, -ENODEV,
+                         "fail to get irq %d\n",
+                         isp4_ringbuf_interrupt_num[i]);

drivers/media/platform/amd/isp4/isp4.c line 70

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+        ret = devm_request_irq(&pdev->dev, irq, isp4_irq_handler, 0,
+                       "ISP_IRQ", &pdev->dev);
+        if (ret)
+            return dev_err_probe(dev, ret, "fail to req irq %d\n",
+                         irq);
+    }
+
+    isp_dev->pltf_data = pdev->dev.platform_data;
+
+    dev_dbg(dev, "isp irq registration successful\n");

As you have error handling in place with dev_err_probe() I think the lack of an error implies this message.  I'd say drop it.

Quoted 11 lines
drivers/media/platform/amd/isp4/isp4.c new file +11 -0
+
+    /* Link the media device within the v4l2_device */
+    isp_dev->v4l2_dev.mdev = &isp_dev->mdev;
+
+    /* Initialize media device */
+    strscpy(isp_dev->mdev.model, "amd_isp41_mdev",
+        sizeof(isp_dev->mdev.model));
+    snprintf(isp_dev->mdev.bus_info, sizeof(isp_dev->mdev.bus_info),
+         "platform:%s", ISP4_DRV_NAME);
+    isp_dev->mdev.dev = &pdev->dev;
+    media_device_init(&isp_dev->mdev);

drivers/media/platform/amd/isp4/isp4.c line 91

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+
+    /* register v4l2 device */
+    snprintf(isp_dev->v4l2_dev.name, sizeof(isp_dev->v4l2_dev.name),
+         "AMD-V4L2-ROOT");
+    ret = v4l2_device_register(&pdev->dev, &isp_dev->v4l2_dev);
+    if (ret)
+        return dev_err_probe(dev, ret,
+                     "fail register v4l2 device\n");
+
+    dev_dbg(dev, "AMD ISP v4l2 device registered\n");

As you have error handling in place with dev_err_probe() I think the lack of an error implies this message.  I'd say drop it.

Quoted 18 lines
drivers/media/platform/amd/isp4/isp4.c new file +18 -0
+
+    ret = media_device_register(&isp_dev->mdev);
+    if (ret) {
+        dev_err(dev, "fail to register media device %d\n", ret);
+        goto err_unreg_v4l2;
+    }
+
+    platform_set_drvdata(pdev, isp_dev);
+
+    pm_runtime_set_suspended(dev);
+    pm_runtime_enable(dev);
+
+    return 0;
+
+err_unreg_v4l2:
+    v4l2_device_unregister(&isp_dev->v4l2_dev);
+
+    return dev_err_probe(dev, ret, "isp probe fail\n");

drivers/media/platform/amd/isp4/isp4.c line 119

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+}
+
+static void isp4_capture_remove(struct platform_device *pdev)
+{
+    struct isp4_device *isp_dev = platform_get_drvdata(pdev);
+    struct device *dev = &pdev->dev;
+
+    media_device_unregister(&isp_dev->mdev);
+    v4l2_device_unregister(&isp_dev->v4l2_dev);
+    dev_dbg(dev, "AMD ISP v4l2 device unregistered\n");

Probably not needed message anymore, right?

Quoted 35 lines
drivers/media/platform/amd/isp4/isp4.c new file +20 -0
+}
+
+static struct platform_driver isp4_capture_drv = {
+    .probe = isp4_capture_probe,
+    .remove = isp4_capture_remove,
+    .driver = {
+        .name = ISP4_DRV_NAME,
+        .owner = THIS_MODULE,
+    }
+};
+
+module_platform_driver(isp4_capture_drv);
+
+MODULE_ALIAS("platform:" ISP4_DRV_NAME);
+MODULE_IMPORT_NS("DMA_BUF");
+
+MODULE_DESCRIPTION("AMD ISP4 Driver");
+MODULE_AUTHOR("Bin Du <[email protected]>");
+MODULE_AUTHOR("Pratap Nirujogi <[email protected]>");
+MODULE_LICENSE("GPL");
drivers/media/platform/amd/isp4/isp4.h new file +9 -0
new file mode 100644
index 000000000000..27a7362ce6f9
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_H_
+#define _ISP4_H_
+
+#include <linux/mutex.h>

drivers/media/platform/amd/isp4/isp4.h line 19

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.h new file +10 -0
+#include <media/v4l2-device.h>
+#include <media/videobuf2-memops.h>
+#include <media/videobuf2-vmalloc.h>
+
+#define ISP4_GET_ISP_REG_BASE(isp4sd) (((isp4sd))->mmio)
+
+struct isp4_platform_data {
+    void *adev;
+    void *bo;
+    void *cpu_ptr;

Will touch more on these in later patches, but I would say don't introduce them until the patch they're needed.

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.h new file +6 -0
+    u64 gpu_addr;
+    u32 size;
+    u32 asic_type;
+    resource_size_t base_rmmio_size;
+};
+

drivers/media/platform/amd/isp4/isp4.h line 35

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.h new file +10 -0
+struct isp4_device {
+    struct v4l2_device v4l2_dev;
+    struct media_device mdev;
+
+    struct isp4_platform_data *pltf_data;
+    struct platform_device *pdev;
+    struct notifier_block i2c_nb;
+};
+
+#endif /* isp4.h */

/* ISP4_H */

#25 lore raw

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

In reply to Du, Bin
Quoted 58 lines · Bin

Thanks Mario, will fix in the next version and pls see some of my comments

Quoted 55 lines · Mario Limonciello
(quoted diff) +2 -0
+Alex
+amd-gfx
Quoted 41 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.

---
  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 +0 -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

drivers/media/platform/amd/isp4/Makefile +4 -0
+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

drivers/media/platform/amd/isp4/Makefile line 23

Quoted 7 lines
drivers/media/platform/amd/isp4/Makefile +7 -0
+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

IMO this feels like a hack and also fragile to be sharing so much across subsystems.

Quoted 5 lines
Quoted 1 line

Either there should be a kernel wide include/ header that can be used by both or there should be a helper exported to get just the data that is needed.

Yes, will refine to remove unnecessary ones in the next version, actually isp driver needs to access function amdgpu_bo_create_kernel which is exported by amdgpu and delared in amdgpu_object.h, because amdgpu_object.h also includes other amd gpu header files, so have to add these include path to avoid compilation error. It'll be greate if there is kernel wide include, any suggestion for this?

Ah yeah I do see exports as of https://git.kernel.org/torvalds/c/ebbe34edc0a90

So based on what is in the tree right now what you did "makes sense", but I don't think it's "correct". From another driver like the ISP driver I feel that the code should really just be something like:

#include <linux/amdgpu/isp.h>

I see a few ways forward.

1) A new set of helpers created for amdgpu that can take
opaque" data arguments (maybe the mfd device?) and return back a pointer to the new buffer. Describe those new helpers in that isp.h header.

2) Manage the lifecycle of the buffers in sw_init/sw_fini of the ISP in amdgpu. Store pointers to them for access in the mfd device, and let the ISP driver take references. This only works if you don't need new buffers after the driver is loaded.

I personally prefer option 2 if it is feasible.

Alex or others might have some other ideas too.

author maintainer
#5 lore raw

Re: [PATCH v2 1/8] media: platform: amd: Introduce amd isp4 capture driver

In reply to Mario Limonciello

Thanks Mario, just add one more comment for the CONFIG_WERROR

Quoted 169 lines · Mario Limonciello
Quoted 144 lines · Bin

Many thx Mario for your comments, really helpful, will address all of them in the next patch. Add inline for some of your comments, pls check

Quoted 131 lines · Mario Limonciello
Quoted 1 line · Bin Du

Amd isp4 capture is a v4l2 media device which implements media controller

AMD

Quoted 2 lines

interface.
It has one sub-device (amd ISP4 sub-device) endpoint which can be connected

AMD

Quoted 3 lines

to a remote CSI2 TX endpoint. It supports only one physical interface for now.
Also add ISP4 driver related entry info into the MAINAINERS file

MAINTAINERS

Quoted 3 lines

Who actually developed?  If both are developers there should be a Co- developed-by tag.

Quoted 53 lines

---
  MAINTAINERS                              |  10 ++ drivers/media/platform/Kconfig           |   1 + drivers/media/platform/Makefile          |   1 + drivers/media/platform/amd/Kconfig       |  17 +++ drivers/media/platform/amd/Makefile      |   5 + drivers/media/platform/amd/isp4/Makefile |  21 ++++ drivers/media/platform/amd/isp4/isp4.c   | 139 +++++++++++++++++++
++++
  drivers/media/platform/amd/isp4/isp4.h   |  35 ++++++

  8 files changed, 229 insertions(+)

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

MAINTAINERS +0 -0
index 10893c91b1c1..15070afb14b5 100644
@@ -1107,6 +1107,16 @@ T:    git git://git.kernel.org/pub/scm/linux/ 

kernel/git/iommu/linux.git
  F:    drivers/iommu/amd/
  F:    include/linux/amd-iommu.h

MAINTAINERS +10 -0
+AMD ISP4 DRIVER
+M:    Bin Du <[email protected]>
+M:    Nirujogi Pratap <[email protected]>
+L:    [email protected]
+S:    Maintained
+T:    git git://linuxtv.org/media.git
+F:    drivers/media/platform/amd/Kconfig
+F:    drivers/media/platform/amd/Makefile
+F:    drivers/media/platform/amd/isp4/*
+

  AMD KFD
  M:    Felix Kuehling <[email protected]>
  L:    [email protected]

drivers/media/platform/Kconfig +0 -0
index 85d2627776b6..d525c2262a7d 100644
@@ -89,5 +89,6 @@ source "drivers/media/platform/ti/Kconfig"

  source "drivers/media/platform/verisilicon/Kconfig" source "drivers/media/platform/via/Kconfig" source "drivers/media/platform/xilinx/Kconfig" +source "drivers/media/platform/amd/Kconfig" endif # MEDIA_PLATFORM_DRIVERS

drivers/media/platform/Makefile +0 -0
index ace4e34483dd..9f3d1693868d 100644
@@ -32,6 +32,7 @@ obj-y += ti/
Quoted 4 lines

  obj-y += verisilicon/
  obj-y += via/
  obj-y += xilinx/
+obj-y += amd/

Is this whole file alphabetical?  If so this is the wrong place.

drivers/media/platform/amd/Kconfig line 5

Quoted 13 lines

  # Please place here only ancillary drivers that aren't SoC-specific # Please keep it alphabetically sorted by Kconfig name

drivers/media/platform/amd/Kconfig new file +5 -0
new file mode 100644
index 000000000000..3b1dba0400a0
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: MIT
+
+config AMD_ISP4
+    tristate "AMD ISP4 and camera driver"
+    default y

I don't believe this should default 'y'.  Normally drivers need to be opt in.

Quoted 20 lines
drivers/media/platform/amd/Kconfig new file +10 -0
+    depends on VIDEO_DEV && VIDEO_V4L2_SUBDEV_API
+    select VIDEOBUF2_CORE
+    select VIDEOBUF2_V4L2
+    select VIDEOBUF2_MEMOPS
+    select VIDEOBUF2_VMALLOC
+    select VIDEOBUF2_DMA_CONTIG
+    select VIDEOBUF2_DMA_SG
+    help
+      This is support for AMD ISP4 and camera subsystem driver.
+      Say Y here to enable the ISP4 and camera device for video 

capture.
+      To compile this driver as a module, choose M here. The module will
+      be called amd_capture.

drivers/media/platform/amd/Makefile new file +0 -0
new file mode 100644
index 000000000000..76146efcd2bf

drivers/media/platform/amd/Makefile line 1

Quoted 1 line
drivers/media/platform/amd/Makefile new file +1 -0
@@ -0,0 +1,5 @@
+# Copyright 2024 Advanced Micro Devices, Inc.

2025

Quoted 9 lines
drivers/media/platform/amd/Makefile new file +4 -0
+# add isp block
+ifneq ($(CONFIG_AMD_ISP4),)
+obj-y += isp4/
+endif
drivers/media/platform/amd/isp4/Makefile new file +0 -0
new file mode 100644
index 000000000000..e9e84160517d
Quoted 9 lines

drivers/media/platform/amd/isp4/Makefile line 5

Quoted 6 lines
drivers/media/platform/amd/isp4/Makefile new file +5 -0
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2025 Advanced Micro Devices, Inc.
+
+obj-$(CONFIG_AMD_ISP4) += amd_capture.o

As the directory is already conditional on CONFIG_AMD_ISP4 is this obj- $() needed?  Or should it really be obj-y?

Quoted 1 line

Yes, it is needed, because AMD_ISP4 is trisate in Kconfig, it can be y or m

Got it, thanks for clarification.

Quoted 20 lines
Quoted 20 lines
Quoted 7 lines
drivers/media/platform/amd/isp4/Makefile new file +6 -0
+amd_capture-objs := isp4.o
+
+ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
+ccflags-y += -I$(srctree)/include
+
+ifneq ($(call cc-option, -mpreferred-stack-boundary=4),)

drivers/media/platform/amd/isp4/Makefile line 21

Quoted 10 lines
drivers/media/platform/amd/isp4/Makefile new file +10 -0
+    cc_stack_align := -mpreferred-stack-boundary=4
+endif
+
+ccflags-y += $(cc_stack_align)
+ccflags-y += -DCONFIG_COMPAT
+ccflags-y += -Wunused-but-set-variable
+ccflags-y += -Wmissing-include-dirs
+ccflags-y += -Wunused-const-variable
+ccflags-y += -Wmaybe-uninitialized
+ccflags-y += -Wunused-value

Do you really need to enforce all these flags just for this driver?

Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

Was this just for development to avoid having to remember to call the build with W=1 or CONFIG_WERROR?

We found after patch submission, Media CI robot test will be triggered, when it builds the patch it will set these flags, so adding these flags to align with Media CI robot test to discover potential issue before submission.

I believe you can just compile with CONFIG_WERROR and get same result, no?  If I'm wrong, nonetheless this should be set in your external build environment script not in the Makefile to be going upstream.

IMO, CONFIG_WERROR just treats warning as error, won't add other extra flags. Sure, will remove them from the makefile and only add them to the local build environment.

Quoted 224 lines
Quoted 223 lines
Quoted 222 lines
Quoted 38 lines
drivers/media/platform/amd/isp4/isp4.c new file +25 -0
new file mode 100644
index 000000000000..d0be90c5ec3b
@@ -0,0 +1,139 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/pm_runtime.h>
+#include <linux/vmalloc.h>
+#include <media/v4l2-ioctl.h>
+
+#include "isp4.h"
+
+#define VIDEO_BUF_NUM 5
+
+#define ISP4_DRV_NAME "amd_isp_capture"
+
+/* interrupt num */
+static const u32 isp4_ringbuf_interrupt_num[] = {
+    0, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT9 */
+    1, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT10 */
+    3, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT11 */
+    4, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT12 */
+};
+
+#define to_isp4_device(dev) \
+    ((struct isp4_device *)container_of(dev, struct isp4_device, 

v4l2_dev))

drivers/media/platform/amd/isp4/isp4.c new file +4 -0
+
+static irqreturn_t isp4_irq_handler(int irq, void *arg)
+{
+    struct isp4_device *isp_dev = dev_get_drvdata((struct device 

*)arg);

drivers/media/platform/amd/isp4/isp4.c line 35

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.c new file +6 -0
+
+    if (!isp_dev)
+        goto error_drv_data;
+
+error_drv_data:
+    return IRQ_HANDLED;

In patch 5 you change this function, including dropping the goto and label.

So I suggest that for patch 1 you KISS:

static irqreturn_t isp4_irq_handler(int irq, void *arg) {
     return IRQ_HANDLED;
}

Then in patch 5 add the extra conditional code and real handling.

drivers/media/platform/amd/isp4/isp4.c line 40

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c new file +5 -0
+}
+
+/*
+ * amd capture module
+ */

Pointless comment, no?

drivers/media/platform/amd/isp4/isp4.c line 45

Quoted 5 lines
drivers/media/platform/amd/isp4/isp4.c new file +5 -0
+static int isp4_capture_probe(struct platform_device *pdev)
+{
+    struct device *dev = &pdev->dev;
+    struct isp4_device *isp_dev;
+

Why newline here?

Quoted 15 lines

+    int i, irq, ret;

drivers/media/platform/amd/isp4/isp4.c new file +14 -0
+
+    isp_dev = devm_kzalloc(&pdev->dev, sizeof(*isp_dev), GFP_KERNEL);
+    if (!isp_dev)
+        return -ENOMEM;
+
+    isp_dev->pdev = pdev;
+    dev->init_name = ISP4_DRV_NAME;
+
+    for (i = 0; i < ARRAY_SIZE(isp4_ringbuf_interrupt_num); i++) {
+        irq = platform_get_irq(pdev, isp4_ringbuf_interrupt_num[i]);
+        if (irq < 0)
+            return dev_err_probe(dev, -ENODEV,
+                         "fail to get irq %d\n",
+                         isp4_ringbuf_interrupt_num[i]);

drivers/media/platform/amd/isp4/isp4.c line 70

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+        ret = devm_request_irq(&pdev->dev, irq, isp4_irq_handler, 0,
+                       "ISP_IRQ", &pdev->dev);
+        if (ret)
+            return dev_err_probe(dev, ret, "fail to req irq %d\n",
+                         irq);
+    }
+
+    isp_dev->pltf_data = pdev->dev.platform_data;
+
+    dev_dbg(dev, "isp irq registration successful\n");

As you have error handling in place with dev_err_probe() I think the lack of an error implies this message.  I'd say drop it.

Quoted 11 lines
drivers/media/platform/amd/isp4/isp4.c new file +11 -0
+
+    /* Link the media device within the v4l2_device */
+    isp_dev->v4l2_dev.mdev = &isp_dev->mdev;
+
+    /* Initialize media device */
+    strscpy(isp_dev->mdev.model, "amd_isp41_mdev",
+        sizeof(isp_dev->mdev.model));
+    snprintf(isp_dev->mdev.bus_info, sizeof(isp_dev->mdev.bus_info),
+         "platform:%s", ISP4_DRV_NAME);
+    isp_dev->mdev.dev = &pdev->dev;
+    media_device_init(&isp_dev->mdev);

drivers/media/platform/amd/isp4/isp4.c line 91

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+
+    /* register v4l2 device */
+    snprintf(isp_dev->v4l2_dev.name, sizeof(isp_dev->v4l2_dev.name),
+         "AMD-V4L2-ROOT");
+    ret = v4l2_device_register(&pdev->dev, &isp_dev->v4l2_dev);
+    if (ret)
+        return dev_err_probe(dev, ret,
+                     "fail register v4l2 device\n");
+
+    dev_dbg(dev, "AMD ISP v4l2 device registered\n");

As you have error handling in place with dev_err_probe() I think the lack of an error implies this message.  I'd say drop it.

Quoted 18 lines
drivers/media/platform/amd/isp4/isp4.c new file +18 -0
+
+    ret = media_device_register(&isp_dev->mdev);
+    if (ret) {
+        dev_err(dev, "fail to register media device %d\n", ret);
+        goto err_unreg_v4l2;
+    }
+
+    platform_set_drvdata(pdev, isp_dev);
+
+    pm_runtime_set_suspended(dev);
+    pm_runtime_enable(dev);
+
+    return 0;
+
+err_unreg_v4l2:
+    v4l2_device_unregister(&isp_dev->v4l2_dev);
+
+    return dev_err_probe(dev, ret, "isp probe fail\n");

drivers/media/platform/amd/isp4/isp4.c line 119

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+}
+
+static void isp4_capture_remove(struct platform_device *pdev)
+{
+    struct isp4_device *isp_dev = platform_get_drvdata(pdev);
+    struct device *dev = &pdev->dev;
+
+    media_device_unregister(&isp_dev->mdev);
+    v4l2_device_unregister(&isp_dev->v4l2_dev);
+    dev_dbg(dev, "AMD ISP v4l2 device unregistered\n");

Probably not needed message anymore, right?

Quoted 35 lines
drivers/media/platform/amd/isp4/isp4.c new file +20 -0
+}
+
+static struct platform_driver isp4_capture_drv = {
+    .probe = isp4_capture_probe,
+    .remove = isp4_capture_remove,
+    .driver = {
+        .name = ISP4_DRV_NAME,
+        .owner = THIS_MODULE,
+    }
+};
+
+module_platform_driver(isp4_capture_drv);
+
+MODULE_ALIAS("platform:" ISP4_DRV_NAME);
+MODULE_IMPORT_NS("DMA_BUF");
+
+MODULE_DESCRIPTION("AMD ISP4 Driver");
+MODULE_AUTHOR("Bin Du <[email protected]>");
+MODULE_AUTHOR("Pratap Nirujogi <[email protected]>");
+MODULE_LICENSE("GPL");
drivers/media/platform/amd/isp4/isp4.h new file +9 -0
new file mode 100644
index 000000000000..27a7362ce6f9
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_H_
+#define _ISP4_H_
+
+#include <linux/mutex.h>

drivers/media/platform/amd/isp4/isp4.h line 19

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.h new file +10 -0
+#include <media/v4l2-device.h>
+#include <media/videobuf2-memops.h>
+#include <media/videobuf2-vmalloc.h>
+
+#define ISP4_GET_ISP_REG_BASE(isp4sd) (((isp4sd))->mmio)
+
+struct isp4_platform_data {
+    void *adev;
+    void *bo;
+    void *cpu_ptr;

Will touch more on these in later patches, but I would say don't introduce them until the patch they're needed.

Quoted 6 lines
drivers/media/platform/amd/isp4/isp4.h new file +6 -0
+    u64 gpu_addr;
+    u32 size;
+    u32 asic_type;
+    resource_size_t base_rmmio_size;
+};
+

drivers/media/platform/amd/isp4/isp4.h line 35

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.h new file +10 -0
+struct isp4_device {
+    struct v4l2_device v4l2_dev;
+    struct media_device mdev;
+
+    struct isp4_platform_data *pltf_data;
+    struct platform_device *pdev;
+    struct notifier_block i2c_nb;
+};
+
+#endif /* isp4.h */

/* ISP4_H */

author maintainer
#26 lore raw

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

In reply to Mario Limonciello

Thanks Mario, add more comments

Quoted 74 lines · Mario Limonciello
Quoted 59 lines · Bin

Thanks Mario, will fix in the next version and pls see some of my comments

Quoted 56 lines · Mario Limonciello
(quoted diff) +2 -0
+Alex
+amd-gfx
Quoted 42 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.

---
  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 +0 -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

drivers/media/platform/amd/isp4/Makefile +4 -0
+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

drivers/media/platform/amd/isp4/Makefile line 23

Quoted 7 lines
drivers/media/platform/amd/isp4/Makefile +7 -0
+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

IMO this feels like a hack and also fragile to be sharing so much across subsystems.

Quoted 5 lines
Quoted 1 line

Either there should be a kernel wide include/ header that can be used by both or there should be a helper exported to get just the data that is needed.

Yes, will refine to remove unnecessary ones in the next version, actually isp driver needs to access function amdgpu_bo_create_kernel which is exported by amdgpu and delared in amdgpu_object.h, because amdgpu_object.h also includes other amd gpu header files, so have to add these include path to avoid compilation error. It'll be greate if there is kernel wide include, any suggestion for this?

Ah yeah I do see exports as of https://git.kernel.org/torvalds/c/ ebbe34edc0a90

So based on what is in the tree right now what you did "makes sense", but I don't think it's "correct".  From another driver like the ISP driver I feel that the code should really just be something like:

#include <linux/amdgpu/isp.h>

I see a few ways forward.

Quoted 8 lines

1) A new set of helpers created for amdgpu that can take
opaque" data arguments (maybe the mfd device?) and return back a pointer to the new buffer.  Describe those new helpers in that isp.h header.

2) Manage the lifecycle of the buffers in sw_init/sw_fini of the ISP in amdgpu.  Store pointers to them for access in the mfd device, and let the ISP driver take references.  This only works if you don't need new buffers after the driver is loaded.

I personally prefer option 2 if it is feasible.

Alex or others might have some other ideas too.

2 is not feasible, because the buffer size and number are decided by App at runtime. 1 is really a good idea, will have more investigation internally

author maintainer
#39 lore raw

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

In reply to Mario Limonciello

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]>>>>

#45 lore raw

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

In reply to Bin Du

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

#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

#46 lore raw

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

In reply to 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

#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
#47 lore raw

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

In reply to Sultan Alsawaf

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

author maintainer
#74 lore raw
In reply to Sultan Alsawaf

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

#49 lore raw

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

In reply to Du, Bin
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

#75 lore raw
In reply to Du, Bin
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

maintainer
#6 lore raw

Re: [PATCH v2 1/8] media: platform: amd: Introduce amd isp4 capture driver

In reply to Bin Du

Hi Bin,

Quoted 6 lines · Bin Du

Amd isp4 capture is a v4l2 media device which implements media controller interface.
It has one sub-device (amd ISP4 sub-device) endpoint which can be connected to a remote CSI2 TX endpoint. It supports only one physical interface for now.
Also add ISP4 driver related entry info into the MAINAINERS file

You could rewrap the text and use the full lines here -- up to 75 characters per line.

Quoted 71 lines
---
 MAINTAINERS                              |  10 ++
 drivers/media/platform/Kconfig           |   1 +
 drivers/media/platform/Makefile          |   1 +
 drivers/media/platform/amd/Kconfig       |  17 +++
 drivers/media/platform/amd/Makefile      |   5 +
 drivers/media/platform/amd/isp4/Makefile |  21 ++++
 drivers/media/platform/amd/isp4/isp4.c   | 139 +++++++++++++++++++++++
 drivers/media/platform/amd/isp4/isp4.h   |  35 ++++++
 8 files changed, 229 insertions(+)
 create mode 100644 drivers/media/platform/amd/Kconfig
 create mode 100644 drivers/media/platform/amd/Makefile
 create mode 100644 drivers/media/platform/amd/isp4/Makefile
 create mode 100644 drivers/media/platform/amd/isp4/isp4.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4.h
MAINTAINERS +10 -0
index 10893c91b1c1..15070afb14b5 100644
@@ -1107,6 +1107,16 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/iommu/linux.git
 F:	drivers/iommu/amd/
 F:	include/linux/amd-iommu.h
 
+AMD ISP4 DRIVER
+M:	Bin Du <[email protected]>
+M:	Nirujogi Pratap <[email protected]>
+L:	[email protected]
+S:	Maintained
+T:	git git://linuxtv.org/media.git
+F:	drivers/media/platform/amd/Kconfig
+F:	drivers/media/platform/amd/Makefile
+F:	drivers/media/platform/amd/isp4/*
+
 AMD KFD
 M:	Felix Kuehling <[email protected]>
 L:	[email protected]
drivers/media/platform/Kconfig +1 -0
index 85d2627776b6..d525c2262a7d 100644
@@ -89,5 +89,6 @@ source "drivers/media/platform/ti/Kconfig"
 source "drivers/media/platform/verisilicon/Kconfig"
 source "drivers/media/platform/via/Kconfig"
 source "drivers/media/platform/xilinx/Kconfig"
+source "drivers/media/platform/amd/Kconfig"
 
 endif # MEDIA_PLATFORM_DRIVERS
drivers/media/platform/Makefile +1 -0
index ace4e34483dd..9f3d1693868d 100644
@@ -32,6 +32,7 @@ obj-y += ti/
 obj-y += verisilicon/
 obj-y += via/
 obj-y += xilinx/
+obj-y += amd/
 
 # Please place here only ancillary drivers that aren't SoC-specific
 # Please keep it alphabetically sorted by Kconfig name
drivers/media/platform/amd/Kconfig new file +2 -0
new file mode 100644
index 000000000000..3b1dba0400a0
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: MIT
+

drivers/media/platform/amd/Kconfig line 12

Quoted 10 lines
drivers/media/platform/amd/Kconfig new file +10 -0
+config AMD_ISP4
+	tristate "AMD ISP4 and camera driver"
+	default y
+	depends on VIDEO_DEV && VIDEO_V4L2_SUBDEV_API
+	select VIDEOBUF2_CORE
+	select VIDEOBUF2_V4L2
+	select VIDEOBUF2_MEMOPS
+	select VIDEOBUF2_VMALLOC
+	select VIDEOBUF2_DMA_CONTIG
+	select VIDEOBUF2_DMA_SG

Do you need all these three? Most drivers need only one.

Quoted 25 lines
drivers/media/platform/amd/Kconfig new file +5 -0
+	help
+	  This is support for AMD ISP4 and camera subsystem driver.
+	  Say Y here to enable the ISP4 and camera device for video capture.
+	  To compile this driver as a module, choose M here. The module will
+	  be called amd_capture.
drivers/media/platform/amd/Makefile new file +5 -0
new file mode 100644
index 000000000000..76146efcd2bf
@@ -0,0 +1,5 @@
+# Copyright 2024 Advanced Micro Devices, Inc.
+# add isp block
+ifneq ($(CONFIG_AMD_ISP4),)
+obj-y += isp4/
+endif
drivers/media/platform/amd/isp4/Makefile new file +3 -0
new file mode 100644
index 000000000000..e9e84160517d
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2025 Advanced Micro Devices, Inc.

drivers/media/platform/amd/isp4/Makefile line 13

Quoted 10 lines
drivers/media/platform/amd/isp4/Makefile new file +10 -0
+
+obj-$(CONFIG_AMD_ISP4) += amd_capture.o
+amd_capture-objs := isp4.o
+
+ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
+ccflags-y += -I$(srctree)/include
+
+ifneq ($(call cc-option, -mpreferred-stack-boundary=4),)
+	cc_stack_align := -mpreferred-stack-boundary=4
+endif

Uh... does the driver actually depend on this?

Quoted 14 lines
drivers/media/platform/amd/isp4/Makefile new file +8 -0
+
+ccflags-y += $(cc_stack_align)
+ccflags-y += -DCONFIG_COMPAT
+ccflags-y += -Wunused-but-set-variable
+ccflags-y += -Wmissing-include-dirs
+ccflags-y += -Wunused-const-variable
+ccflags-y += -Wmaybe-uninitialized
+ccflags-y += -Wunused-value
drivers/media/platform/amd/isp4/isp4.c new file +0 -0
new file mode 100644
index 000000000000..d0be90c5ec3b

drivers/media/platform/amd/isp4/isp4.c line 8

Quoted 8 lines
drivers/media/platform/amd/isp4/isp4.c new file +8 -0
@@ -0,0 +1,139 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/pm_runtime.h>
+#include <linux/vmalloc.h>
+#include <media/v4l2-ioctl.h>

Alphabetic order, please.

drivers/media/platform/amd/isp4/isp4.c line 12

Quoted 4 lines
drivers/media/platform/amd/isp4/isp4.c new file +4 -0
+
+#include "isp4.h"
+
+#define VIDEO_BUF_NUM 5

Unused.

drivers/media/platform/amd/isp4/isp4.c line 25

Quoted 13 lines
drivers/media/platform/amd/isp4/isp4.c new file +13 -0
+
+#define ISP4_DRV_NAME "amd_isp_capture"
+
+/* interrupt num */
+static const u32 isp4_ringbuf_interrupt_num[] = {
+	0, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT9 */
+	1, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT10 */
+	3, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT11 */
+	4, /* ISP_4_1__SRCID__ISP_RINGBUFFER_WPT12 */
+};
+
+#define to_isp4_device(dev) \
+	((struct isp4_device *)container_of(dev, struct isp4_device, v4l2_dev))

No need for the cast.

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+
+static irqreturn_t isp4_irq_handler(int irq, void *arg)
+{
+	struct isp4_device *isp_dev = dev_get_drvdata((struct device *)arg);
+
+	if (!isp_dev)
+		goto error_drv_data;
+
+error_drv_data:
+	return IRQ_HANDLED;

drivers/media/platform/amd/isp4/isp4.c line 45

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+}
+
+/*
+ * amd capture module
+ */
+static int isp4_capture_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct isp4_device *isp_dev;
+

Extra newline.

drivers/media/platform/amd/isp4/isp4.c line 46

Quoted 1 line
drivers/media/platform/amd/isp4/isp4.c new file +1 -0
+	int i, irq, ret;

unsigned int i?

Quoted 14 lines
drivers/media/platform/amd/isp4/isp4.c new file +14 -0
+
+	isp_dev = devm_kzalloc(&pdev->dev, sizeof(*isp_dev), GFP_KERNEL);
+	if (!isp_dev)
+		return -ENOMEM;
+
+	isp_dev->pdev = pdev;
+	dev->init_name = ISP4_DRV_NAME;
+
+	for (i = 0; i < ARRAY_SIZE(isp4_ringbuf_interrupt_num); i++) {
+		irq = platform_get_irq(pdev, isp4_ringbuf_interrupt_num[i]);
+		if (irq < 0)
+			return dev_err_probe(dev, -ENODEV,
+					     "fail to get irq %d\n",
+					     isp4_ringbuf_interrupt_num[i]);

drivers/media/platform/amd/isp4/isp4.c line 70

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+		ret = devm_request_irq(&pdev->dev, irq, isp4_irq_handler, 0,
+				       "ISP_IRQ", &pdev->dev);
+		if (ret)
+			return dev_err_probe(dev, ret, "fail to req irq %d\n",
+					     irq);
+	}
+
+	isp_dev->pltf_data = pdev->dev.platform_data;
+
+	dev_dbg(dev, "isp irq registration successful\n");

Please leave this out.

Quoted 11 lines
drivers/media/platform/amd/isp4/isp4.c new file +11 -0
+
+	/* Link the media device within the v4l2_device */
+	isp_dev->v4l2_dev.mdev = &isp_dev->mdev;
+
+	/* Initialize media device */
+	strscpy(isp_dev->mdev.model, "amd_isp41_mdev",
+		sizeof(isp_dev->mdev.model));
+	snprintf(isp_dev->mdev.bus_info, sizeof(isp_dev->mdev.bus_info),
+		 "platform:%s", ISP4_DRV_NAME);
+	isp_dev->mdev.dev = &pdev->dev;
+	media_device_init(&isp_dev->mdev);

drivers/media/platform/amd/isp4/isp4.c line 91

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+
+	/* register v4l2 device */
+	snprintf(isp_dev->v4l2_dev.name, sizeof(isp_dev->v4l2_dev.name),
+		 "AMD-V4L2-ROOT");
+	ret = v4l2_device_register(&pdev->dev, &isp_dev->v4l2_dev);
+	if (ret)
+		return dev_err_probe(dev, ret,
+				     "fail register v4l2 device\n");
+
+	dev_dbg(dev, "AMD ISP v4l2 device registered\n");

This doesn't seem useful.

drivers/media/platform/amd/isp4/isp4.c line 102

Quoted 11 lines
drivers/media/platform/amd/isp4/isp4.c new file +11 -0
+
+	ret = media_device_register(&isp_dev->mdev);
+	if (ret) {
+		dev_err(dev, "fail to register media device %d\n", ret);
+		goto err_unreg_v4l2;
+	}
+
+	platform_set_drvdata(pdev, isp_dev);
+
+	pm_runtime_set_suspended(dev);
+	pm_runtime_enable(dev);

You'll need to enable runtime PM before registering any interfaces on UAPI. Same goes for setting driver data for the device.

Quoted 7 lines
drivers/media/platform/amd/isp4/isp4.c new file +7 -0
+
+	return 0;
+
+err_unreg_v4l2:
+	v4l2_device_unregister(&isp_dev->v4l2_dev);
+
+	return dev_err_probe(dev, ret, "isp probe fail\n");

drivers/media/platform/amd/isp4/isp4.c line 119

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.c new file +10 -0
+}
+
+static void isp4_capture_remove(struct platform_device *pdev)
+{
+	struct isp4_device *isp_dev = platform_get_drvdata(pdev);
+	struct device *dev = &pdev->dev;
+
+	media_device_unregister(&isp_dev->mdev);
+	v4l2_device_unregister(&isp_dev->v4l2_dev);
+	dev_dbg(dev, "AMD ISP v4l2 device unregistered\n");

I'd say this is redundant.

Quoted 51 lines
drivers/media/platform/amd/isp4/isp4.c new file +20 -0
+}
+
+static struct platform_driver isp4_capture_drv = {
+	.probe = isp4_capture_probe,
+	.remove = isp4_capture_remove,
+	.driver = {
+		.name = ISP4_DRV_NAME,
+		.owner = THIS_MODULE,
+	}
+};
+
+module_platform_driver(isp4_capture_drv);
+
+MODULE_ALIAS("platform:" ISP4_DRV_NAME);
+MODULE_IMPORT_NS("DMA_BUF");
+
+MODULE_DESCRIPTION("AMD ISP4 Driver");
+MODULE_AUTHOR("Bin Du <[email protected]>");
+MODULE_AUTHOR("Pratap Nirujogi <[email protected]>");
+MODULE_LICENSE("GPL");
drivers/media/platform/amd/isp4/isp4.h new file +25 -0
new file mode 100644
index 000000000000..27a7362ce6f9
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_H_
+#define _ISP4_H_
+
+#include <linux/mutex.h>
+#include <media/v4l2-device.h>
+#include <media/videobuf2-memops.h>
+#include <media/videobuf2-vmalloc.h>
+
+#define ISP4_GET_ISP_REG_BASE(isp4sd) (((isp4sd))->mmio)
+
+struct isp4_platform_data {
+	void *adev;
+	void *bo;
+	void *cpu_ptr;
+	u64 gpu_addr;
+	u32 size;
+	u32 asic_type;
+	resource_size_t base_rmmio_size;
+};
+

drivers/media/platform/amd/isp4/isp4.h line 35

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4.h new file +10 -0
+struct isp4_device {
+	struct v4l2_device v4l2_dev;
+	struct media_device mdev;
+
+	struct isp4_platform_data *pltf_data;
+	struct platform_device *pdev;
+	struct notifier_block i2c_nb;
+};
+
+#endif /* isp4.h */

Use the same name as for the macro here, please.

Signature · 3 lines
Kind regards,

Sakari Ailus
maintainer
#11 lore raw

Re: [PATCH v2 2/8] media: platform: amd: low level support for isp4 firmware

In reply to Bin Du

Hi Bin,

Quoted 5 lines · Bin Du

Low level functions for access the registers and mapping to their ranges. This change also includes register definitions for ring buffer used to communicate with ISP Firmware.
Ring buffer is the communication interface between driver and ISP Firmware. Command and responses are exchanged through the ring buffer.

Please rewrap this, the third line could well be longer.

Quoted 47 lines
---
 drivers/media/platform/amd/isp4/Makefile      |   3 +-
 drivers/media/platform/amd/isp4/isp4_hw.c     |  46 +++++++
 drivers/media/platform/amd/isp4/isp4_hw.h     |  14 +++
 drivers/media/platform/amd/isp4/isp4_hw_reg.h | 116 ++++++++++++++++++
 4 files changed, 178 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/platform/amd/isp4/isp4_hw.c
 create mode 100644 drivers/media/platform/amd/isp4/isp4_hw.h
 create mode 100644 drivers/media/platform/amd/isp4/isp4_hw_reg.h
drivers/media/platform/amd/isp4/Makefile +2 -1
index e9e84160517d..8ca1c4dfe246 100644
@@ -3,7 +3,8 @@
 # Copyright (C) 2025 Advanced Micro Devices, Inc.
 
 obj-$(CONFIG_AMD_ISP4) += amd_capture.o
-amd_capture-objs := isp4.o
+amd_capture-objs := isp4.o	\
+			isp4_hw.o	\
 
 ccflags-y += -I$(srctree)/drivers/media/platform/amd/isp4
 ccflags-y += -I$(srctree)/include
drivers/media/platform/amd/isp4/isp4_hw.c new file +14 -0
new file mode 100644
index 000000000000..e5315330a514
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/io.h>
+#include <linux/types.h>
+
+#include "isp4_hw.h"
+#include "isp4_hw_reg.h"
+
+#define RMMIO_SIZE 524288
+
+u32 isp4hw_rreg(void __iomem *base, u32 reg)

drivers/media/platform/amd/isp4/isp4_hw.c line 24

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_hw.c new file +10 -0
+{
+	void __iomem *reg_addr;
+
+	if (reg >= RMMIO_SIZE)
+		return RREG_FAILED_VAL;
+
+	if (reg < ISP_MIPI_PHY0_REG0)
+		reg_addr = base + reg;
+	else if (reg <= ISP_MIPI_PHY0_REG0 + ISP_MIPI_PHY0_SIZE)
+		reg_addr = base + (reg - ISP_MIPI_PHY0_REG0);

Redundant parentheses.

Quoted 7 lines
drivers/media/platform/amd/isp4/isp4_hw.c new file +7 -0
+	else
+		return RREG_FAILED_VAL;
+
+	return readl(reg_addr);
+};
+
+void isp4hw_wreg(void __iomem *base, u32 reg, u32 val)

drivers/media/platform/amd/isp4/isp4_hw.c line 41

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_hw.c new file +10 -0
+{
+	void __iomem *reg_addr;
+
+	if (reg >= RMMIO_SIZE)
+		return;
+
+	if (reg < ISP_MIPI_PHY0_REG0)
+		reg_addr = base + reg;
+	else if (reg <= ISP_MIPI_PHY0_REG0 + ISP_MIPI_PHY0_SIZE)
+		reg_addr = base + (reg - ISP_MIPI_PHY0_REG0);

Ditto.

Quoted 37 lines
drivers/media/platform/amd/isp4/isp4_hw.c new file +5 -0
+	else
+		return;
+
+	writel(val, reg_addr);
+};
drivers/media/platform/amd/isp4/isp4_hw.h new file +14 -0
new file mode 100644
index 000000000000..072d135b9e3a
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_HW_H_
+#define _ISP4_HW_H_
+
+#define RREG_FAILED_VAL 0xFFFFFFFF
+
+u32 isp4hw_rreg(void __iomem *base, u32 reg);
+void isp4hw_wreg(void __iomem *base, u32 reg, u32 val);
+
+#endif
drivers/media/platform/amd/isp4/isp4_hw_reg.h new file +6 -0
new file mode 100644
index 000000000000..b11f12ba6c56
@@ -0,0 +1,116 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _ISP4_HW_REG_H_

drivers/media/platform/amd/isp4/isp4_hw_reg.h line 16

Quoted 10 lines
drivers/media/platform/amd/isp4/isp4_hw_reg.h new file +10 -0
+#define _ISP4_HW_REG_H_
+
+#define ISP_SOFT_RESET		0x62000
+#define ISP_SYS_INT0_EN		0x62010
+#define ISP_SYS_INT0_STATUS	0x62014
+#define ISP_SYS_INT0_ACK	0x62018
+#define ISP_CCPU_CNTL		0x62054
+#define ISP_STATUS		0x62058
+#define ISP_LOG_RB_BASE_LO0	0x62148
+#define ISP_LOG_RB_BASE_HI0	0x6214C

Lower case hexadecimals, please.

Quoted 100 lines
drivers/media/platform/amd/isp4/isp4_hw_reg.h new file +100 -0
+#define ISP_LOG_RB_SIZE0	0x62150
+#define ISP_LOG_RB_RPTR0	0x62154
+#define ISP_LOG_RB_WPTR0	0x62158
+#define ISP_RB_BASE_LO1		0x62170
+#define ISP_RB_BASE_HI1		0x62174
+#define ISP_RB_SIZE1		0x62178
+#define ISP_RB_RPTR1		0x6217C
+#define ISP_RB_WPTR1		0x62180
+#define ISP_RB_BASE_LO2		0x62184
+#define ISP_RB_BASE_HI2		0x62188
+#define ISP_RB_SIZE2		0x6218C
+#define ISP_RB_RPTR2		0x62190
+#define ISP_RB_WPTR2		0x62194
+#define ISP_RB_BASE_LO3		0x62198
+#define ISP_RB_BASE_HI3		0x6219C
+#define ISP_RB_SIZE3		0x621A0
+#define ISP_RB_RPTR3		0x621A4
+#define ISP_RB_WPTR3		0x621A8
+#define ISP_RB_BASE_LO4		0x621AC
+#define ISP_RB_BASE_HI4		0x621B0
+#define ISP_RB_SIZE4		0x621B4
+#define ISP_RB_RPTR4		0x621B8
+#define ISP_RB_WPTR4		0x621BC
+#define ISP_RB_BASE_LO5		0x621C0
+#define ISP_RB_BASE_HI5		0x621C4
+#define ISP_RB_SIZE5		0x621C8
+#define ISP_RB_RPTR5		0x621CC
+#define ISP_RB_WPTR5		0x621D0
+#define ISP_RB_BASE_LO6		0x621D4
+#define ISP_RB_BASE_HI6		0x621D8
+#define ISP_RB_SIZE6		0x621DC
+#define ISP_RB_RPTR6		0x621E0
+#define ISP_RB_WPTR6		0x621E4
+#define ISP_RB_BASE_LO7		0x621E8
+#define ISP_RB_BASE_HI7		0x621EC
+#define ISP_RB_SIZE7		0x621F0
+#define ISP_RB_RPTR7		0x621F4
+#define ISP_RB_WPTR7		0x621F8
+#define ISP_RB_BASE_LO8		0x621FC
+#define ISP_RB_BASE_HI8		0x62200
+#define ISP_RB_SIZE8		0x62204
+#define ISP_RB_RPTR8		0x62208
+#define ISP_RB_WPTR8		0x6220C
+#define ISP_RB_BASE_LO9		0x62210
+#define ISP_RB_BASE_HI9		0x62214
+#define ISP_RB_SIZE9		0x62218
+#define ISP_RB_RPTR9		0x6221C
+#define ISP_RB_WPTR9		0x62220
+#define ISP_RB_BASE_LO10	0x62224
+#define ISP_RB_BASE_HI10	0x62228
+#define ISP_RB_SIZE10		0x6222C
+#define ISP_RB_RPTR10		0x62230
+#define ISP_RB_WPTR10		0x62234
+#define ISP_RB_BASE_LO11	0x62238
+#define ISP_RB_BASE_HI11	0x6223C
+#define ISP_RB_SIZE11		0x62240
+#define ISP_RB_RPTR11		0x62244
+#define ISP_RB_WPTR11		0x62248
+#define ISP_RB_BASE_LO12	0x6224C
+#define ISP_RB_BASE_HI12	0x62250
+#define ISP_RB_SIZE12		0x62254
+#define ISP_RB_RPTR12		0x62258
+#define ISP_RB_WPTR12		0x6225C
+
+#define ISP_POWER_STATUS	0x60000
+
+#define ISP_MIPI_PHY0_REG0	0x66700
+#define ISP_MIPI_PHY1_REG0	0x66780
+#define ISP_MIPI_PHY2_REG0	0x67400
+
+#define ISP_MIPI_PHY0_SIZE	0xD30
+
+/* ISP_SOFT_RESET */
+#define ISP_SOFT_RESET__CCPU_SOFT_RESET_MASK			0x00000001UL
+
+/* ISP_CCPU_CNTL */
+#define ISP_CCPU_CNTL__CCPU_HOST_SOFT_RST_MASK			0x00040000UL
+
+/* ISP_STATUS */
+#define ISP_STATUS__CCPU_REPORT_MASK				0x000000feUL
+
+/* ISP_SYS_INT0_STATUS */
+#define ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT9_INT_MASK	0x00010000UL
+#define ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT10_INT_MASK	0x00040000UL
+#define ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT11_INT_MASK	0x00100000UL
+#define ISP_SYS_INT0_STATUS__SYS_INT_RINGBUFFER_WPT12_INT_MASK	0x00400000UL
+
+/* ISP_SYS_INT0_EN */
+#define ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT9_EN_MASK	0x00010000UL
+#define ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT10_EN_MASK	0x00040000UL
+#define ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT11_EN_MASK	0x00100000UL
+#define ISP_SYS_INT0_EN__SYS_INT_RINGBUFFER_WPT12_EN_MASK	0x00400000UL
+
+/* ISP_SYS_INT0_ACK */
+#define ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT9_ACK_MASK	0x00010000UL
+#define ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT10_ACK_MASK	0x00040000UL
+#define ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT11_ACK_MASK	0x00100000UL
+#define ISP_SYS_INT0_ACK__SYS_INT_RINGBUFFER_WPT12_ACK_MASK	0x00400000UL
+
+#endif
Signature · 3 lines
Kind regards,

Sakari Ailus