Skip to contentSkip to conversation

Add support for Wave6 video codec driver

PATCH Superseded Nas Chung 4 participants 38 messages Sep 4 – Oct 2 Page 1 of 2

Subsystems (3)

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Nas Chung

author
#0 lore raw

v7 cover Add support for Wave6 video codec driver

This patch series introduces support for the Chips&Media Wave6 video codec IP, a completely different hardware architecture compared to Wave5.

The wave6 driver is a M2M stateful encoder/decoder driver.
It supports various video formats, including H.264 and H.265, for both encoding and decoding.
While other versions of the Wave6 IP may support VP9 decoding and AV1 decoding and encoding those formats are not implemented or validated in this driver at this time.

On NXP i.MX SoCs, the Wave6 hardware exposes one control register region and four interface register regions for one shared video processing engine. VPU Control region, Manages shared resources such as firmware memory. VPU Core region, Provides encoding and decoding capabilities.
The control and interface regions are distinct DMA requesters and can be associated with separate IOMMU stream IDs, allowing DMA isolation between them.

The interface regions are described as child nodes of the VPU node rather than folded into the parent with reg-names/interrupt-names, because each one is a separate DMA requester. This follows the same structure already accepted for the Qualcomm video codec bindings, and was mentioned on the v6 thread.

The firmware tested by this driver has been upstreamed in linux-firmware:
- Path: cnm/wave633c_imx9_codec_fw.bin

This driver has been tested with GStreamer on:
- NXP i.MX95 board
- pre-silicon FPGA environment

Test results for decoder fluster with -j2 option:

- JVT-AVC_V1, Ran 77/135 tests successfully              in 24.180 secs
- JVT-FR-EXT, Ran 25/69 tests successfully               in 11.157 secs
- JCT-VC-HEVC_V1, Ran 132/147 tests successfully         in 45.534 secs

- All failures are due to unsupported hardware features:
-- 10bit, Resolutions higher than 4K, FMO, MBAFF -- Extended profile, Field encoding and High422 sreams.

Test results for v4l2-compliance:
v4l2-compliance 1.31.0-5386, 64 bits, 64-bit time_t v4l2-compliance SHA: 48316b8a20aa 2025-08-12 12:44:56

Compliance test for wave6-dec device /dev/video0:
fail: v4l2-test-controls.cpp(1204): !have_source_change || !have_eos
test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: FAIL
Total for wave6-dec device /dev/video0: 48, Succeeded: 47, Failed: 1, Warnings: 0

Compliance test for wave6-enc device /dev/video1:
fail: v4l2-test-controls.cpp(1193): node->codec_mask & STATEFUL_ENCODER
test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: FAIL
Total for wave6-enc device /dev/video1: 48, Succeeded: 47, Failed: 1, Warnings: 0

Note: the failures are all related with the eos event.

Changelog:

v7:
- Moved the sram property from imx95-19x19-evk.dts to imx95.dtsi
- Simplified the sram binding property to maxItems: 1
- Fixed the issues reported by the sashiko-bot review of v6:
- Bounded frame buffer and aux buffer counts before indexing
- Added vb2_is_busy() checks to the decoder S_FMT handlers
- Fixed video_device lifetime and driver teardown ordering
- Propagated hardware init, runtime PM and thermal failures upward
- Fixed tracepoint field widths and debugfs handling
- Balanced the VPU reference on the core activation error path

v6:
- Fixed the existing P010 format_info entry
- Moved all encoder parameter validation from the HW layer into the V4L2 layer
- Removed redundant HW-abstraction wrappers from the VPU API layer
- Computed frame stride and sizeimage in the V4L2 layer
- Added explicit instance lifecycle helpers to the core driver
- Enabled the VPU only on the tested imx95-19x19-evk board

RFC v5:
- Move all shared resources to the parent node
- Drop child compatible and use data-only interface child nodes
- Update the VPU driver to create child devices and load the core driver

v4:
- Fixed build issues reported by CI tools
- Updated commit messages to use imperative mood
- Avoided using the same name for both nodes and labels in devicetree
- Removed unused labels from YAML examples
- Added description for child(vpu-core) node
- Added iommus property to both parent(vpu) and child(vpu-core) nodes
- Updated probe() functions to use dev_err_probe() when returning -EPROBE_DEFER
- Added wave6_vpu prefix to trace functions
- Updated HEVC decoder profile control to report MAIN_STILL profile
- Fixed bug in multiple instance creation by pre-allocating work buffer
- Fixed interrupt handling by checking INSTANCE_INFO register and instance list

v3:
- Removed ambiguous SUPPORT_FOLLOWER feature
- Used WARN_ON() for unexpected programming errors
- Split thermal device code into wave6-vpu-thermal.c/h
- Dropped wave6_cooling_disable module parameter
- Replaced mutex_lock() with guard()
- Added lockdep_assert_held() to clarify locking regions
- Removed exported function due to dual-license and used function pointer
- Added documentation and validation for state transitions
- Added documentation for device structures
- Added patch to enable VPU device in imx95 DTS
- Updated DT bindings and driver to align with parent(vpu) and child(vpu-core)
- Replaced magic numbers with mask and offset macros when accessing registers
- Placed goto statements after an empty line
- Printed HW info (e.g. product_code) via dev_dbg() for debugging
- Replaced wave6_vpu_dec_give_command() with dedicated functions

v2:
- Refined DT bindings to better represent the hardware
- Reworked driver to align with the parent(VPU) and child(CTRL, CORE)
- Fixed build issues reported by CI tools (Smatch, Sparse, TRACE)
- Improved commit messages with clearer descriptions
- Added kernel-doc for exported functions
- Removed redundant print statements and unused code
- Reordered patches to prevent build failures

Nas Chung (9):
media: v4l2-common: Fix P010 format info
dt-bindings: media: nxp: Add Wave6 video codec device
media: chips-media: wave6: Add Wave6 VPU interface
media: chips-media: wave6: Add v4l2 m2m driver support
media: chips-media: wave6: Add Wave6 core driver
media: chips-media: wave6: Improve debugging capabilities
media: chips-media: wave6: Add Wave6 thermal cooling device
media: chips-media: wave6: Add Wave6 control driver
arm64: dts: freescale: imx95: Add video codec node

 .../bindings/media/nxp,imx95-vpu.yaml         |  162 +
 MAINTAINERS                                   |    8 +
 .../boot/dts/freescale/imx95-19x19-evk.dts    |   10 +
 arch/arm64/boot/dts/freescale/imx95.dtsi      |   37 +
 drivers/media/platform/chips-media/Kconfig    |    1 +
 drivers/media/platform/chips-media/Makefile   |    1 +
 .../media/platform/chips-media/wave6/Kconfig  |   17 +
 .../media/platform/chips-media/wave6/Makefile |   17 +
 .../platform/chips-media/wave6/wave6-hw.c     | 2030 ++++++++++++
 .../platform/chips-media/wave6/wave6-hw.h     |   56 +
 .../chips-media/wave6/wave6-regdefine.h       |  649 ++++
 .../platform/chips-media/wave6/wave6-trace.h  |  289 ++
 .../platform/chips-media/wave6/wave6-vdi.h    |   92 +
 .../chips-media/wave6/wave6-vpu-core.c        |  448 +++
 .../chips-media/wave6/wave6-vpu-core.h        |  126 +
 .../chips-media/wave6/wave6-vpu-dbg.c         |  183 ++
 .../chips-media/wave6/wave6-vpu-dbg.h         |   14 +
 .../chips-media/wave6/wave6-vpu-dec.c         | 1843 +++++++++++
 .../chips-media/wave6/wave6-vpu-enc.c         | 2764 +++++++++++++++++
 .../chips-media/wave6/wave6-vpu-thermal.c     |  143 +
 .../chips-media/wave6/wave6-vpu-thermal.h     |   24 +
 .../chips-media/wave6/wave6-vpu-v4l2.c        |  516 +++
 .../platform/chips-media/wave6/wave6-vpu.c    |  826 +++++
 .../platform/chips-media/wave6/wave6-vpu.h    |  143 +
 .../platform/chips-media/wave6/wave6-vpuapi.c |  336 ++
 .../platform/chips-media/wave6/wave6-vpuapi.h | 1007 ++++++
 .../chips-media/wave6/wave6-vpuconfig.h       |   72 +
 .../chips-media/wave6/wave6-vpuerror.h        |  262 ++
 drivers/media/v4l2-core/v4l2-common.c         |    2 +-
 29 files changed, 12077 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml
 create mode 100644 drivers/media/platform/chips-media/wave6/Kconfig
 create mode 100644 drivers/media/platform/chips-media/wave6/Makefile
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-hw.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-hw.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-regdefine.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-trace.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vdi.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-core.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-core.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpuapi.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpuapi.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpuerror.h
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v7 1/9 media: v4l2-common: Fix P010 format info

V4L2_PIX_FMT_P010 is a 10-bit 4:2:0 semi-planar format, but its v4l2_format_info() entry described a 4:2:2 layout with a half-width chroma plane. Correct bpp and vdiv to match P010's real layout, consistent with the other semi-planar formats.

Fixes 5374d8fb75f3 ("media: Add P010 video format")
---
 drivers/media/v4l2-core/v4l2-common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-common.c +1 -1
index 65db7340ad38..4de8aa3ef7d2 100644
@@ -323,7 +323,7 @@ const struct v4l2_format_info *v4l2_format_info(u32 format)
 		{ .format = V4L2_PIX_FMT_NV20,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 5, 10, 0, 0 }, .bpp_div = { 4, 4, 1, 1 }, .hdiv = 2, .vdiv = 1 },
 		{ .format = V4L2_PIX_FMT_NV24,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
 		{ .format = V4L2_PIX_FMT_NV42,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 1, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 1, .vdiv = 1 },
-		{ .format = V4L2_PIX_FMT_P010,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 2, 2, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 1 },
+		{ .format = V4L2_PIX_FMT_P010,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 2, 4, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
 		{ .format = V4L2_PIX_FMT_P012,    .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 2, .bpp = { 2, 4, 0, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 2, .vdiv = 2 },
 
 		{ .format = V4L2_PIX_FMT_YUV410,  .pixel_enc = V4L2_PIXEL_ENC_YUV, .mem_planes = 1, .comp_planes = 3, .bpp = { 1, 1, 1, 0 }, .bpp_div = { 1, 1, 1, 1 }, .hdiv = 4, .vdiv = 4 },
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH v7 2/9 dt-bindings: media: nxp: Add Wave6 video codec device

In reply to Nas Chung

Add documentation for the Chips&Media Wave6 video codec on NXP i.MX SoCs.

The hardware contains one control register region and four interface register regions for a shared video processing engine. The control region manages shared resources such as firmware memory, while each interface region has its own MMIO range and interrupt.

The control region and each interface region are distinct DMA requesters and can be associated with separate IOMMU stream IDs. Represent the control region as the parent node and the interface register regions as child nodes to describe these resources.

---
 .../bindings/media/nxp,imx95-vpu.yaml         | 162 ++++++++++++++++++
 MAINTAINERS                                   |   7 +
 2 files changed, 169 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml
Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml new file +162 -0
new file mode 100644
index 000000000000..aa7dd8838af0
@@ -0,0 +1,162 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/nxp,imx95-vpu.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Chips&Media Wave6 Series multi-standard codec IP on NXP i.MX SoCs
+
+maintainers:
+  - Nas Chung <[email protected]>
+  - Jackson Lee <[email protected]>
+
+description:
+  The Chips&Media Wave6 codec IP is a multi-standard video encoder/decoder.
+  On NXP i.MX SoCs, the Wave6 codec IP exposes one control register region and
+  four interface register regions for a shared video processing engine.
+  The parent node describes the control region, which has its own MMIO range and
+  manages shared resources such as firmware memory. The child nodes describe the
+  interface register regions. Each interface region has its own MMIO range and
+  interrupt.
+  The control region and the interface regions are distinct DMA requesters.
+  The control region and each interface region can be associated with separate
+  IOMMU stream IDs, allowing DMA isolation between them.
+
+properties:
+  compatible:
+    enum:
+      - nxp,imx95-vpu
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: VPU core clock
+      - description: VPU associated block clock
+
+  clock-names:
+    items:
+      - const: core
+      - const: vpublk
+
+  power-domains:
+    items:
+      - description: Main VPU power domain
+      - description: Performance power domain
+
+  power-domain-names:
+    items:
+      - const: vpu
+      - const: perf
+
+  memory-region:
+    maxItems: 1
+
+  sram:
+    maxItems: 1
+    description:
+      Used to store reference data, reducing DMA memory bandwidth.
+
+  iommus:
+    maxItems: 1
+
+  "#cooling-cells":
+    const: 2
+
+  "#address-cells":
+    const: 2
+
+  "#size-cells":
+    const: 2
+
+  ranges: true
+
+patternProperties:
+  "^interface@[0-9a-f]+$":
+    type: object
+    description:
+      An interface register region within the Chips&Media Wave6 codec IP.
+      Each region has its own MMIO range and interrupt and can be associated
+      with a separate IOMMU stream ID for DMA isolation.
+    additionalProperties: false
+
+    properties:
+      reg:
+        maxItems: 1
+
+      interrupts:
+        maxItems: 1
+
+      iommus:
+        maxItems: 1
+
+    required:
+      - reg
+      - interrupts
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - power-domains
+  - power-domain-names
+  - memory-region
+  - "#address-cells"
+  - "#size-cells"
+  - ranges
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/clock/nxp,imx95-clock.h>
+
+    soc {
+      #address-cells = <2>;
+      #size-cells = <2>;
+
+      video-codec@4c4c0000 {
+        compatible = "nxp,imx95-vpu";
+        reg = <0x0 0x4c4c0000 0x0 0x10000>;
+        clocks = <&scmi_clk 115>,
+                 <&vpu_blk_ctrl IMX95_CLK_VPUBLK_WAVE>;
+        clock-names = "core", "vpublk";
+        power-domains = <&scmi_devpd 21>,
+                        <&scmi_perf 10>;
+        power-domain-names = "vpu", "perf";
+        memory-region = <&vpu_boot>;
+        sram = <&sram1>;
+        iommus = <&smmu 0x32>;
+        #cooling-cells = <2>;
+        #address-cells = <2>;
+        #size-cells = <2>;
+        ranges;
+
+        interface@4c480000 {
+          reg = <0x0 0x4c480000 0x0 0x10000>;
+          interrupts = <GIC_SPI 299 IRQ_TYPE_LEVEL_HIGH>;
+          iommus = <&smmu 0x33>;
+        };
+
+        interface@4c490000 {
+          reg = <0x0 0x4c490000 0x0 0x10000>;
+          interrupts = <GIC_SPI 300 IRQ_TYPE_LEVEL_HIGH>;
+          iommus = <&smmu 0x34>;
+        };
+
+        interface@4c4a0000 {
+          reg = <0x0 0x4c4a0000 0x0 0x10000>;
+          interrupts = <GIC_SPI 301 IRQ_TYPE_LEVEL_HIGH>;
+          iommus = <&smmu 0x35>;
+        };
+
+        interface@4c4b0000 {
+          reg = <0x0 0x4c4b0000 0x0 0x10000>;
+          interrupts = <GIC_SPI 302 IRQ_TYPE_LEVEL_HIGH>;
+          iommus = <&smmu 0x36>;
+        };
+      };
+    };
MAINTAINERS +7 -0
index 8d9648737e92..7387a11facbe 100644
@@ -29233,6 +29233,13 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/media/cnm,wave521c.yaml
 F:	drivers/media/platform/chips-media/wave5/
 
+WAVE6 VPU CODEC DRIVER
+M:	Nas Chung <[email protected]>
+M:	Jackson Lee <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml
+
 WHISKEYCOVE PMIC GPIO DRIVER
 M:	Kuppuswamy Sathyanarayanan <[email protected]>
 L:	[email protected]
Signature · 1 line
2.31.1
Reviewed-by on v7 2/9
#3 lore raw

Re: [PATCH v7 2/9] dt-bindings: media: nxp: Add Wave6 video codec device

Quoted 12 lines · Nas Chung

Add documentation for the Chips&Media Wave6 video codec on NXP i.MX SoCs.

The hardware contains one control register region and four interface register regions for a shared video processing engine. The control region manages shared resources such as firmware memory, while each interface region has its own MMIO range and interrupt.

The control region and each interface region are distinct DMA requesters and can be associated with separate IOMMU stream IDs. Represent the control region as the parent node and the interface register regions as child nodes to describe these resources.

Quoted 6 lines
---
 .../bindings/media/nxp,imx95-vpu.yaml         | 162 ++++++++++++++++++
 MAINTAINERS                                   |   7 +
 2 files changed, 169 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml

Best regards,
Krzysztof

Reviewed-by on v7 2/9
#4 lore raw

Re: [PATCH v7 2/9] dt-bindings: media: nxp: Add Wave6 video codec device

In reply to Nas Chung
Quoted 7 lines · Nas Chung

Add documentation for the Chips&Media Wave6 video codec on NXP i.MX SoCs.

The hardware contains one control register region and four interface register regions for a shared video processing engine. The control region manages shared resources such as firmware memory, while each interface region has its own MMIO range and interrupt.

Quoted 7 lines

The control region and each interface region are distinct DMA requesters and can be associated with separate IOMMU stream IDs. Represent the control region as the parent node and the interface register regions as child nodes to describe these resources.

---

Quoted 194 lines
 .../bindings/media/nxp,imx95-vpu.yaml         | 162 ++++++++++++++++++
 MAINTAINERS                                   |   7 +
 2 files changed, 169 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml
Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml new file +162 -0
new file mode 100644
index 000000000000..aa7dd8838af0
@@ -0,0 +1,162 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/nxp,imx95-vpu.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Chips&Media Wave6 Series multi-standard codec IP on NXP i.MX SoCs
+
+maintainers:
+  - Nas Chung <[email protected]>
+  - Jackson Lee <[email protected]>
+
+description:
+  The Chips&Media Wave6 codec IP is a multi-standard video encoder/decoder.
+  On NXP i.MX SoCs, the Wave6 codec IP exposes one control register region and
+  four interface register regions for a shared video processing engine.
+  The parent node describes the control region, which has its own MMIO range and
+  manages shared resources such as firmware memory. The child nodes describe the
+  interface register regions. Each interface region has its own MMIO range and
+  interrupt.
+  The control region and the interface regions are distinct DMA requesters.
+  The control region and each interface region can be associated with separate
+  IOMMU stream IDs, allowing DMA isolation between them.
+
+properties:
+  compatible:
+    enum:
+      - nxp,imx95-vpu
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: VPU core clock
+      - description: VPU associated block clock
+
+  clock-names:
+    items:
+      - const: core
+      - const: vpublk
+
+  power-domains:
+    items:
+      - description: Main VPU power domain
+      - description: Performance power domain
+
+  power-domain-names:
+    items:
+      - const: vpu
+      - const: perf
+
+  memory-region:
+    maxItems: 1
+
+  sram:
+    maxItems: 1
+    description:
+      Used to store reference data, reducing DMA memory bandwidth.
+
+  iommus:
+    maxItems: 1
+
+  "#cooling-cells":
+    const: 2
+
+  "#address-cells":
+    const: 2
+
+  "#size-cells":
+    const: 2
+
+  ranges: true
+
+patternProperties:
+  "^interface@[0-9a-f]+$":
+    type: object
+    description:
+      An interface register region within the Chips&Media Wave6 codec IP.
+      Each region has its own MMIO range and interrupt and can be associated
+      with a separate IOMMU stream ID for DMA isolation.
+    additionalProperties: false
+
+    properties:
+      reg:
+        maxItems: 1
+
+      interrupts:
+        maxItems: 1
+
+      iommus:
+        maxItems: 1
+
+    required:
+      - reg
+      - interrupts
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - power-domains
+  - power-domain-names
+  - memory-region
+  - "#address-cells"
+  - "#size-cells"
+  - ranges
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/clock/nxp,imx95-clock.h>
+
+    soc {
+      #address-cells = <2>;
+      #size-cells = <2>;
+
+      video-codec@4c4c0000 {
+        compatible = "nxp,imx95-vpu";
+        reg = <0x0 0x4c4c0000 0x0 0x10000>;
+        clocks = <&scmi_clk 115>,
+                 <&vpu_blk_ctrl IMX95_CLK_VPUBLK_WAVE>;
+        clock-names = "core", "vpublk";
+        power-domains = <&scmi_devpd 21>,
+                        <&scmi_perf 10>;
+        power-domain-names = "vpu", "perf";
+        memory-region = <&vpu_boot>;
+        sram = <&sram1>;
+        iommus = <&smmu 0x32>;
+        #cooling-cells = <2>;
+        #address-cells = <2>;
+        #size-cells = <2>;
+        ranges;
+
+        interface@4c480000 {
+          reg = <0x0 0x4c480000 0x0 0x10000>;
+          interrupts = <GIC_SPI 299 IRQ_TYPE_LEVEL_HIGH>;
+          iommus = <&smmu 0x33>;
+        };
+
+        interface@4c490000 {
+          reg = <0x0 0x4c490000 0x0 0x10000>;
+          interrupts = <GIC_SPI 300 IRQ_TYPE_LEVEL_HIGH>;
+          iommus = <&smmu 0x34>;
+        };
+
+        interface@4c4a0000 {
+          reg = <0x0 0x4c4a0000 0x0 0x10000>;
+          interrupts = <GIC_SPI 301 IRQ_TYPE_LEVEL_HIGH>;
+          iommus = <&smmu 0x35>;
+        };
+
+        interface@4c4b0000 {
+          reg = <0x0 0x4c4b0000 0x0 0x10000>;
+          interrupts = <GIC_SPI 302 IRQ_TYPE_LEVEL_HIGH>;
+          iommus = <&smmu 0x36>;
+        };
+      };
+    };
MAINTAINERS +7 -1
index 8d9648737e92..7387a11facbe 100644
@@ -29233,6 +29233,13 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/media/cnm,wave521c.yaml
 F:	drivers/media/platform/chips-media/wave5/
 
+WAVE6 VPU CODEC DRIVER
+M:	Nas Chung <[email protected]>
+M:	Jackson Lee <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml
+
 WHISKEYCOVE PMIC GPIO DRIVER
 M:	Kuppuswamy Sathyanarayanan <[email protected]>
 L:	[email protected]
--

2.31.1

author
#5 lore raw

PATCH v7 3/9 media: chips-media: wave6: Add Wave6 VPU interface

In reply to Nas Chung

Add an interface layer to manage hardware register configuration and communication with the Chips&Media Wave6 video codec IP.

The interface provides low-level helper functions used by the Wave6 core driver to implement video encoding and decoding operations. It handles command submission to the firmware via MMIO registers, and waits for a response by polling the firmware busy flag.

---
 MAINTAINERS                                   |    1 +
 .../platform/chips-media/wave6/wave6-hw.c     | 2030 +++++++++++++++++
 .../platform/chips-media/wave6/wave6-hw.h     |   56 +
 .../chips-media/wave6/wave6-regdefine.h       |  649 ++++++
 .../platform/chips-media/wave6/wave6-vdi.h    |   92 +
 .../platform/chips-media/wave6/wave6-vpuapi.c |  336 +++
 .../platform/chips-media/wave6/wave6-vpuapi.h | 1007 ++++++++
 .../chips-media/wave6/wave6-vpuconfig.h       |   72 +
 .../chips-media/wave6/wave6-vpuerror.h        |  262 +++
 9 files changed, 4505 insertions(+)
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-hw.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-hw.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-regdefine.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vdi.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpuapi.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpuapi.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpuerror.h
MAINTAINERS +1 -0
index 7387a11facbe..e29018c2546b 100644
@@ -29239,6 +29239,7 @@ M:	Jackson Lee <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml
+F:	drivers/media/platform/chips-media/wave6/
 
 WHISKEYCOVE PMIC GPIO DRIVER
 M:	Kuppuswamy Sathyanarayanan <[email protected]>
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2030 -0

2,030 lines not on this page · raw message

drivers/media/platform/chips-media/wave6/wave6-hw.h new file +56 -0

56 lines not on this page · raw message

drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +649 -0

649 lines not on this page · raw message

drivers/media/platform/chips-media/wave6/wave6-vdi.h new file +92 -0

92 lines not on this page · raw message

drivers/media/platform/chips-media/wave6/wave6-vpuapi.c new file +336 -0

166 lines not on this page · raw message

drivers/media/platform/chips-media/wave6/wave6-vpuapi.h new file +1007 -0

1,007 lines not on this page · raw message

drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h new file +72 -0

72 lines not on this page · raw message

drivers/media/platform/chips-media/wave6/wave6-vpuerror.h new file +262 -0

262 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.31.1

Part of the diff is not shown here. View the raw message.

#6 lore raw

Re: [PATCH v7 3/9] media: chips-media: wave6: Add Wave6 VPU interface

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Integer underflow when calculating end index for frame buffer registration causes out-of-bounds…

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Integer underflow when calculating end index for frame buffer registration causes out-of-bounds hardware configuration.
- [High] Encoder frame buffer registration fails to group configuration commands, allowing array index overflow into adjacent MMIO registers.
- [Medium] Silent truncation of RGB chroma format due to mismatched bitmask.
- [Medium] Display buffers are silently ignored when the metadata array is full.
- [Medium] Hardcoded little-endian memory layout breaks compatibility with big-endian host architectures.
--

commit 5d00a062981a74f9de3d4d2750b7f43dba64822e
Author: Nas Chung <[email protected]>

media: chips-media: wave6: Add Wave6 VPU interface

Add an interface layer to manage hardware register configuration and communication with the Chips&Media Wave6 video codec IP.

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +0 -0
@@ -0,0 +1,2030 @@

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c line 75

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +13 -0
+static void wave6_dec_set_display_buffer(struct vpu_instance *inst, struct frame_buffer fb)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int index;
+
+	for (index = 0; index < WAVE6_MAX_FBS; index++) {
+		if (!p_dec_info->disp_buf[index].buf_y) {
+			p_dec_info->disp_buf[index] = fb;
+			p_dec_info->disp_buf[index].index = index;
+			break;
+		}
+	}
+}

[Severity: Medium]
WAVE6_MAX_FBS is defined as 31. If userspace requests and registers 32 buffers, what happens when this loop completes without finding an empty slot?

Does this silently drop the tracking metadata for the buffer, leading to failures later in wave6_vpu_dec_get_output_info() when it tries to match the display buffer address?

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+int wave6_vpu_dec_register_frame_buffer(struct vpu_instance *inst)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c line 564

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +12 -0
+	fbc_remain = fbc_count;
+	mv_remain = mv_count;
+	group_num = DIV_ROUND_UP(max(fbc_count, mv_count), 16);
+	for (i = 0; i < group_num; i++) {
+		bool first_group = i == 0;
+		bool last_group = i == group_num - 1;
+		u32 set_fbc_num = (fbc_remain >= 16) ? 16 : fbc_remain;
+		u32 set_mv_num = (mv_remain >= 16) ? 16 : mv_remain;
+		u32 fbc_start_no = i * 16;
+		u32 fbc_end_no = fbc_start_no + set_fbc_num - 1;
+		u32 mv_start_no = i * 16;
+		u32 mv_end_no = mv_start_no + set_mv_num - 1;

[Severity: High]
If either fbc_count or mv_count is initially 0, or if they are uneven and one depletes before the other in subsequent loop iterations, set_fbc_num or set_mv_num becomes 0.

Does this cause fbc_end_no or mv_end_no to underflow when subtracting 1, sending invalid boundaries to the hardware?

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+int wave6_vpu_dec_register_display_buffer(struct vpu_instance *inst, struct frame_buffer fb)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c line 673

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+	reg_val = SET_DISP_COMMON_PIC_INFO_BWB_ON |
+		  SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(c_fmt_idc) |
+		  SET_DISP_COMMON_PIC_INFO_PIXEL_ORDER(PIXEL_ORDER_INCREASING) |

[Severity: Medium]
When the format is RGB, get_chroma_format_idc() returns C_FMT_IDC_RGB which evaluates to 4 (binary 100).

Since SET_DISP_COMMON_PIC_INFO_C_FMT_IDC uses a 2-bit mask, does this silently truncate the value to 0 (C_FMT_IDC_YUV400) and misconfigure the hardware for RGB displays?

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+int wave6_vpu_enc_register_frame_buffer(struct vpu_instance *inst)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c line 1598

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +4 -0
+	for (idx = 0; idx < fbc_count; idx++) {
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_Y(idx), fb_arr[idx].buf_y);
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_C(idx), fb_arr[idx].buf_cb);
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_CR(idx), fb_arr[idx].buf_cr);

[Severity: High]
Unlike the decoder buffer registration which batches commands into groups of 16, this loop writes sequentially up to fbc_count.

If fbc_count exceeds 16, could this overflow past the 16 available MMIO slots for W6_CMD_SET_FB_FBC_Y (and others) and corrupt adjacent hardware registers like W6_CMD_SET_FB_DEFAULT_CDF?

[ ... ]

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +0 -0
@@ -0,0 +1,649 @@

[ ... ]

Quoted 1 line

+#define SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(x) FIELD_PREP(GENMASK(25, 24), (x))

[Severity: Medium]
As noted in wave6_vpu_dec_register_display_buffer(), this mask is only 2 bits wide. Could this be extended to correctly support C_FMT_IDC_RGB (which requires at least 3 bits to store value 4)?

[ ... ]

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h new file +0 -0
@@ -0,0 +1,72 @@

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h line 53

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h new file +4 -0
+#define HOST_ENDIAN			VDI_128BIT_LITTLE_ENDIAN
+#define VPU_STREAM_ENDIAN		HOST_ENDIAN
+#define VPU_USER_DATA_ENDIAN		HOST_ENDIAN
+#define VPU_SOURCE_ENDIAN		HOST_ENDIAN

[Severity: Medium]
Does hardcoding the host endianness to VDI_128BIT_LITTLE_ENDIAN break stream decoding and encoding on big-endian architectures?

Could this be set conditionally using standard kernel endianness macros to support big-endian hosts?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

Re: [PATCH v7 3/9] media: chips-media: wave6: Add Wave6 VPU interface

Quoted 7 lines · On Fri, Sep 04, 2026 at 07:04:50AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Integer underflow when calculating end index for frame buffer registration causes out-of-bounds hardware configuration.
- [High] Encoder frame buffer registration fails to group configuration commands, allowing array index overflow into adjacent MMIO registers.
- [Medium] Silent truncation of RGB chroma format due to mismatched bitmask.
- [Medium] Display buffers are silently ignored when the metadata array is full.
- [Medium] Hardcoded little-endian memory layout breaks compatibility with big-endian host architectures.
--

Sorry, jump in at v7. Please fix AI report issues. or provide judgements

And please CC [email protected] next time.

Frank

Quoted 128 lines

commit 5d00a062981a74f9de3d4d2750b7f43dba64822e
Author: Nas Chung <[email protected]>

media: chips-media: wave6: Add Wave6 VPU interface

Add an interface layer to manage hardware register configuration and communication with the Chips&Media Wave6 video codec IP.

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +0 -0
@@ -0,0 +1,2030 @@

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c line 75

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +13 -0
+static void wave6_dec_set_display_buffer(struct vpu_instance *inst, struct frame_buffer fb)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int index;
+
+	for (index = 0; index < WAVE6_MAX_FBS; index++) {
+		if (!p_dec_info->disp_buf[index].buf_y) {
+			p_dec_info->disp_buf[index] = fb;
+			p_dec_info->disp_buf[index].index = index;
+			break;
+		}
+	}
+}

[Severity: Medium]
WAVE6_MAX_FBS is defined as 31. If userspace requests and registers 32 buffers, what happens when this loop completes without finding an empty slot?

Does this silently drop the tracking metadata for the buffer, leading to failures later in wave6_vpu_dec_get_output_info() when it tries to match the display buffer address?

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+int wave6_vpu_dec_register_frame_buffer(struct vpu_instance *inst)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c line 564

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +12 -0
+	fbc_remain = fbc_count;
+	mv_remain = mv_count;
+	group_num = DIV_ROUND_UP(max(fbc_count, mv_count), 16);
+	for (i = 0; i < group_num; i++) {
+		bool first_group = i == 0;
+		bool last_group = i == group_num - 1;
+		u32 set_fbc_num = (fbc_remain >= 16) ? 16 : fbc_remain;
+		u32 set_mv_num = (mv_remain >= 16) ? 16 : mv_remain;
+		u32 fbc_start_no = i * 16;
+		u32 fbc_end_no = fbc_start_no + set_fbc_num - 1;
+		u32 mv_start_no = i * 16;
+		u32 mv_end_no = mv_start_no + set_mv_num - 1;

[Severity: High]
If either fbc_count or mv_count is initially 0, or if they are uneven and one depletes before the other in subsequent loop iterations, set_fbc_num or set_mv_num becomes 0.

Does this cause fbc_end_no or mv_end_no to underflow when subtracting 1, sending invalid boundaries to the hardware?

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+int wave6_vpu_dec_register_display_buffer(struct vpu_instance *inst, struct frame_buffer fb)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c line 673

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+	reg_val = SET_DISP_COMMON_PIC_INFO_BWB_ON |
+		  SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(c_fmt_idc) |
+		  SET_DISP_COMMON_PIC_INFO_PIXEL_ORDER(PIXEL_ORDER_INCREASING) |

[Severity: Medium]
When the format is RGB, get_chroma_format_idc() returns C_FMT_IDC_RGB which evaluates to 4 (binary 100).

Since SET_DISP_COMMON_PIC_INFO_C_FMT_IDC uses a 2-bit mask, does this silently truncate the value to 0 (C_FMT_IDC_YUV400) and misconfigure the hardware for RGB displays?

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+int wave6_vpu_enc_register_frame_buffer(struct vpu_instance *inst)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c line 1598

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +4 -0
+	for (idx = 0; idx < fbc_count; idx++) {
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_Y(idx), fb_arr[idx].buf_y);
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_C(idx), fb_arr[idx].buf_cb);
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_CR(idx), fb_arr[idx].buf_cr);

[Severity: High]
Unlike the decoder buffer registration which batches commands into groups of 16, this loop writes sequentially up to fbc_count.

If fbc_count exceeds 16, could this overflow past the 16 available MMIO slots for W6_CMD_SET_FB_FBC_Y (and others) and corrupt adjacent hardware registers like W6_CMD_SET_FB_DEFAULT_CDF?

[ ... ]

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +0 -0
@@ -0,0 +1,649 @@

[ ... ]

Quoted 1 line

+#define SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(x) FIELD_PREP(GENMASK(25, 24), (x))

[Severity: Medium]
As noted in wave6_vpu_dec_register_display_buffer(), this mask is only 2 bits wide. Could this be extended to correctly support C_FMT_IDC_RGB (which requires at least 3 bits to store value 4)?

[ ... ]

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h new file +0 -0
@@ -0,0 +1,72 @@

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h line 53

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h new file +4 -0
+#define HOST_ENDIAN			VDI_128BIT_LITTLE_ENDIAN
+#define VPU_STREAM_ENDIAN		HOST_ENDIAN
+#define VPU_USER_DATA_ENDIAN		HOST_ENDIAN
+#define VPU_SOURCE_ENDIAN		HOST_ENDIAN

[Severity: Medium]
Does hardcoding the host endianness to VDI_128BIT_LITTLE_ENDIAN break stream decoding and encoding on big-endian architectures?

Could this be set conditionally using standard kernel endianness macros to support big-endian hosts?

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3

author
#8 lore raw

RE: [PATCH v7 3/9] media: chips-media: wave6: Add Wave6 VPU interface

Hi, Frank.

Quoted 17 lines

-----Original Message-----
From: Frank Li <[email protected]>
Sent: Thursday, September 10, 2026 1:40 AM
To: [email protected]
Cc: Nas Chung <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v7 3/9] media: chips-media: wave6: Add Wave6 VPU interface

Quoted 1 line · On Fri, Sep 04, 2026 at 07:04:50AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 5 potential

issue(s) to consider:

Quoted 1 line

- [High] Integer underflow when calculating end index for frame buffer

registration causes out-of-bounds hardware configuration.

Quoted 1 line

- [High] Encoder frame buffer registration fails to group configuration

commands, allowing array index overflow into adjacent MMIO registers.

Quoted 1 line

- [Medium] Silent truncation of RGB chroma format due to mismatched

Quoted 8 lines

bitmask.

Quoted 1 line

- [Medium] Display buffers are silently ignored when the metadata array

is full.

Quoted 1 line

- [Medium] Hardcoded little-endian memory layout breaks compatibility

with big-endian host architectures.

Quoted 1 line

--

Sorry, jump in at v7. Please fix AI report issues. or provide judgements

OK, I'll reply inline to the Sashiko report.

Quoted 2 lines

And please CC [email protected] next time.

Sure.

Thanks,
Nas.

Quoted 142 lines

Frank

Quoted 10 lines

commit 5d00a062981a74f9de3d4d2750b7f43dba64822e
Author: Nas Chung <[email protected]>

media: chips-media: wave6: Add Wave6 VPU interface

Add an interface layer to manage hardware register configuration and communication with the Chips&Media Wave6 video codec IP.

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 5 lines
Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +0 -0
@@ -0,0 +1,2030 @@

[ ... ]

Quoted 1 line

+static void wave6_dec_set_display_buffer(struct vpu_instance *inst,

struct frame_buffer fb)

Quoted 44 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 75

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +12 -0
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int index;
+
+	for (index = 0; index < WAVE6_MAX_FBS; index++) {
+		if (!p_dec_info->disp_buf[index].buf_y) {
+			p_dec_info->disp_buf[index] = fb;
+			p_dec_info->disp_buf[index].index = index;
+			break;
+		}
+	}
+}

[Severity: Medium]
WAVE6_MAX_FBS is defined as 31. If userspace requests and registers 32 buffers, what happens when this loop completes without finding an empty slot?

Does this silently drop the tracking metadata for the buffer, leading to failures later in wave6_vpu_dec_get_output_info() when it tries to match the display buffer address?

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+int wave6_vpu_dec_register_frame_buffer(struct vpu_instance *inst)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c line 564

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +12 -0
+	fbc_remain = fbc_count;
+	mv_remain = mv_count;
+	group_num = DIV_ROUND_UP(max(fbc_count, mv_count), 16);
+	for (i = 0; i < group_num; i++) {
+		bool first_group = i == 0;
+		bool last_group = i == group_num - 1;
+		u32 set_fbc_num = (fbc_remain >= 16) ? 16 : fbc_remain;
+		u32 set_mv_num = (mv_remain >= 16) ? 16 : mv_remain;
+		u32 fbc_start_no = i * 16;
+		u32 fbc_end_no = fbc_start_no + set_fbc_num - 1;
+		u32 mv_start_no = i * 16;
+		u32 mv_end_no = mv_start_no + set_mv_num - 1;

[Severity: High]
If either fbc_count or mv_count is initially 0, or if they are uneven and one depletes before the other in subsequent loop iterations, set_fbc_num or set_mv_num becomes 0.

Quoted 5 lines

Does this cause fbc_end_no or mv_end_no to underflow when subtracting 1, sending invalid boundaries to the hardware?

[ ... ]

Quoted 1 line

+int wave6_vpu_dec_register_display_buffer(struct vpu_instance *inst,

struct frame_buffer fb)

Quoted 5 lines
Quoted 1 line

+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+	reg_val = SET_DISP_COMMON_PIC_INFO_BWB_ON |
+		  SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(c_fmt_idc) |
+		  SET_DISP_COMMON_PIC_INFO_PIXEL_ORDER(PIXEL_ORDER_INCREASING)

|

Quoted 9 lines

[Severity: Medium]
When the format is RGB, get_chroma_format_idc() returns C_FMT_IDC_RGB which evaluates to 4 (binary 100).

Since SET_DISP_COMMON_PIC_INFO_C_FMT_IDC uses a 2-bit mask, does this silently truncate the value to 0 (C_FMT_IDC_YUV400) and misconfigure the hardware for RGB displays?

Quoted 6 lines

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+int wave6_vpu_enc_register_frame_buffer(struct vpu_instance *inst)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+	for (idx = 0; idx < fbc_count; idx++) {
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_Y(idx),

fb_arr[idx].buf_y);

Quoted 1 line

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_C(idx),

fb_arr[idx].buf_cb);

Quoted 1 line

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_CR(idx),

fb_arr[idx].buf_cr);

Quoted 11 lines

[Severity: High]
Unlike the decoder buffer registration which batches commands into groups of 16, this loop writes sequentially up to fbc_count.

If fbc_count exceeds 16, could this overflow past the 16 available MMIO slots for W6_CMD_SET_FB_FBC_Y (and others) and corrupt adjacent hardware registers like W6_CMD_SET_FB_DEFAULT_CDF?

[ ... ]

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/media/platform/chips-media/wave6/wave6-regdefine.h

Quoted 5 lines
Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +0 -0
@@ -0,0 +1,649 @@

[ ... ]

Quoted 1 line

+#define SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(x)

	FIELD_PREP(GENMASK(25, 24), (x))
Quoted 8 lines

[Severity: Medium]
As noted in wave6_vpu_dec_register_display_buffer(), this mask is only 2 bits wide. Could this be extended to correctly support C_FMT_IDC_RGB (which requires at least 3 bits to store value 4)?

[ ... ]

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h

Quoted 13 lines
Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h new file +0 -0
@@ -0,0 +1,72 @@

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h line 53

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h new file +4 -0
+#define HOST_ENDIAN			VDI_128BIT_LITTLE_ENDIAN
+#define VPU_STREAM_ENDIAN		HOST_ENDIAN
+#define VPU_USER_DATA_ENDIAN		HOST_ENDIAN
+#define VPU_SOURCE_ENDIAN		HOST_ENDIAN

[Severity: Medium]
Does hardcoding the host endianness to VDI_128BIT_LITTLE_ENDIAN break stream decoding and encoding on big-endian architectures?

Quoted 5 lines

Could this be set conditionally using standard kernel endianness macros to support big-endian hosts?

--
Sashiko AI review *

https://sashiko.dev/#/patchset/[email protected]
om?part=3

author
#9 lore raw

RE: [PATCH v7 3/9] media: chips-media: wave6: Add Wave6 VPU interface

In reply to [email protected]

Hi,

Quoted 56 lines

-----Original Message-----
From: [email protected] <[email protected]>
Sent: Friday, September 4, 2026 4:05 PM
To: Nas Chung <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v7 3/9] media: chips-media: wave6: Add Wave6 VPU interface

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Integer underflow when calculating end index for frame buffer registration causes out-of-bounds hardware configuration.
- [High] Encoder frame buffer registration fails to group configuration commands, allowing array index overflow into adjacent MMIO registers.
- [Medium] Silent truncation of RGB chroma format due to mismatched bitmask.
- [Medium] Display buffers are silently ignored when the metadata array is full.
- [Medium] Hardcoded little-endian memory layout breaks compatibility with big-endian host architectures.
--

commit 5d00a062981a74f9de3d4d2750b7f43dba64822e
Author: Nas Chung <[email protected]>

media: chips-media: wave6: Add Wave6 VPU interface

Add an interface layer to manage hardware register configuration and communication with the Chips&Media Wave6 video codec IP.

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +0 -0
@@ -0,0 +1,2030 @@

[ ... ]

Quoted 1 line

+static void wave6_dec_set_display_buffer(struct vpu_instance *inst,

struct frame_buffer fb)

drivers/media/platform/chips-media/wave6/wave6-hw.c line 75

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +12 -0
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int index;
+
+	for (index = 0; index < WAVE6_MAX_FBS; index++) {
+		if (!p_dec_info->disp_buf[index].buf_y) {
+			p_dec_info->disp_buf[index] = fb;
+			p_dec_info->disp_buf[index].index = index;
+			break;
+		}
+	}
+}

[Severity: Medium]
WAVE6_MAX_FBS is defined as 31. If userspace requests and registers 32 buffers, what happens when this loop completes without finding an empty slot?

Quoted 3 lines

Does this silently drop the tracking metadata for the buffer, leading to failures later in wave6_vpu_dec_get_output_info() when it tries to match the display buffer address?

This cannot happen. wave6_vpu_dec_handle_dst_buffer() stops registering once WAVE6_MAX_FBS buffers are consumed, so at most WAVE6_MAX_FBS display buffers are registered.

Quoted 23 lines

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+int wave6_vpu_dec_register_frame_buffer(struct vpu_instance *inst)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c line 564

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +12 -0
+	fbc_remain = fbc_count;
+	mv_remain = mv_count;
+	group_num = DIV_ROUND_UP(max(fbc_count, mv_count), 16);
+	for (i = 0; i < group_num; i++) {
+		bool first_group = i == 0;
+		bool last_group = i == group_num - 1;
+		u32 set_fbc_num = (fbc_remain >= 16) ? 16 : fbc_remain;
+		u32 set_mv_num = (mv_remain >= 16) ? 16 : mv_remain;
+		u32 fbc_start_no = i * 16;
+		u32 fbc_end_no = fbc_start_no + set_fbc_num - 1;
+		u32 mv_start_no = i * 16;
+		u32 mv_end_no = mv_start_no + set_mv_num - 1;

[Severity: High]
If either fbc_count or mv_count is initially 0, or if they are uneven and one depletes before the other in subsequent loop iterations, set_fbc_num or set_mv_num becomes 0.

Quoted 2 lines

Does this cause fbc_end_no or mv_end_no to underflow when subtracting 1, sending invalid boundaries to the hardware?

Only when a count is 0, which is not checked here. Will return -EINVAL in that case in v8, as the encoder already does.

Otherwise the counts can differ, but only the second group can run out, and then start is 16 and end is 15. The firmware takes
end - start + 1 = 0 as no entries for that group.

Quoted 11 lines

[ ... ]

Quoted 1 line

+int wave6_vpu_dec_register_display_buffer(struct vpu_instance *inst,

struct frame_buffer fb)

Quoted 1 line

+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+	reg_val = SET_DISP_COMMON_PIC_INFO_BWB_ON |
+		  SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(c_fmt_idc) |
+		  SET_DISP_COMMON_PIC_INFO_PIXEL_ORDER(PIXEL_ORDER_INCREASING)

|

Quoted 7 lines

[Severity: Medium]
When the format is RGB, get_chroma_format_idc() returns C_FMT_IDC_RGB which evaluates to 4 (binary 100).

Since SET_DISP_COMMON_PIC_INFO_C_FMT_IDC uses a 2-bit mask, does this silently truncate the value to 0 (C_FMT_IDC_YUV400) and misconfigure the hardware for RGB displays?

No. The decoder cannot output RGB, so this is intended. RGB is only an encoder source format, and that field (ENC_PIC_SRC_FMT_C_FMT_IDC) is 3 bits.

Quoted 13 lines

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+int wave6_vpu_enc_register_frame_buffer(struct vpu_instance *inst)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+	for (idx = 0; idx < fbc_count; idx++) {
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_Y(idx),

fb_arr[idx].buf_y);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_C(idx),

fb_arr[idx].buf_cb);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_CR(idx),

fb_arr[idx].buf_cr);

Quoted 7 lines

[Severity: High]
Unlike the decoder buffer registration which batches commands into groups of 16, this loop writes sequentially up to fbc_count.

If fbc_count exceeds 16, could this overflow past the 16 available MMIO slots for W6_CMD_SET_FB_FBC_Y (and others) and corrupt adjacent hardware registers like W6_CMD_SET_FB_DEFAULT_CDF?

No. The encoder firmware doesn't require more than 16 buffers, so a single group is enough. Will add a check in v8 as a safeguard.

Quoted 8 lines

[ ... ]

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/media/platform/chips-media/wave6/wave6-regdefine.h

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +0 -0
@@ -0,0 +1,649 @@

[ ... ]

Quoted 7 lines
Quoted 1 line

+#define SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(x)

	FIELD_PREP(GENMASK(25, 24), (x))

[Severity: Medium]
As noted in wave6_vpu_dec_register_display_buffer(), this mask is only 2 bits wide. Could this be extended to correctly support C_FMT_IDC_RGB (which requires at least 3 bits to store value 4)?

No, see above. 2 bits are enough for the decoder.

Quoted 17 lines

[ ... ]

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h new file +0 -0
@@ -0,0 +1,72 @@

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h line 53

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h new file +4 -0
+#define HOST_ENDIAN			VDI_128BIT_LITTLE_ENDIAN
+#define VPU_STREAM_ENDIAN		HOST_ENDIAN
+#define VPU_USER_DATA_ENDIAN		HOST_ENDIAN
+#define VPU_SOURCE_ENDIAN		HOST_ENDIAN

[Severity: Medium]
Does hardcoding the host endianness to VDI_128BIT_LITTLE_ENDIAN break stream decoding and encoding on big-endian architectures?

Quoted 2 lines

Could this be set conditionally using standard kernel endianness macros to support big-endian hosts?

No. These set the byte order of each buffer, regardless of the CPU architecture.

Quoted 5 lines

--
Sashiko AI review ·
https://sashiko.dev/#/patchset/[email protected]
om?part=3

#10 lore raw

Re: [PATCH v7 3/9] media: chips-media: wave6: Add Wave6 VPU interface

In reply to Nas Chung
Quoted 9 lines · Nas Chung

Add an interface layer to manage hardware register configuration and communication with the Chips&Media Wave6 video codec IP.

The interface provides low-level helper functions used by the Wave6 core driver to implement video encoding and decoding operations. It handles command submission to the firmware via MMIO registers, and waits for a response by polling the firmware busy flag.

Nit: your s-o-b is last one

Quoted 3 lines

---

...

MAINTAINERS M: Jackson Lee <[email protected]> · line 29242

Quoted 10 lines
MAINTAINERS +1 -0
index 7387a11facbe..e29018c2546b 100644
@@ -29239,6 +29239,7 @@ M:	Jackson Lee <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml
+F:	drivers/media/platform/chips-media/wave6/

Consider nxp is one major user, can you add

L: [email protected]

...

MAINTAINERS M: Jackson Lee <[email protected]>

Quoted 4 lines
MAINTAINERS +4 -0
+/*
+ * Wave6 series multi-standard codec IP - wave6 backend interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC

2026

MAINTAINERS

Quoted 3 lines

+ */

MAINTAINERS +2 -0
+
+#include <linux/iopoll.h>

Add space line here

Quoted 19 lines
MAINTAINERS +19 -0
+#include "wave6-vpu-core.h"
+#include "wave6-hw.h"
+#include "wave6-regdefine.h"
+#include "wave6-trace.h"
+
+void wave6_vpu_writel(struct vpu_core_device *core, u32 addr, u32 data)
+{
+	wave6_vdi_writel(core->reg_base, addr, data);
+	trace_wave6_vpu_writel(core->dev, addr, data);
+}
+
+u32 wave6_vpu_readl(struct vpu_core_device *core, u32 addr)
+{
+	u32 data;
+
+	data = wave6_vdi_readl(core->reg_base, addr);
+	trace_wave6_vpu_readl(core->dev, addr, data);
+
+	return data;

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+}
+
+static void wave6_print_reg_err(struct vpu_core_device *core, u32 fail_reason)
+{
+	void *caller = __builtin_return_address(0);
+	struct device *dev = core->dev;
+
+	switch (fail_reason) {
+	case WAVE6_SYSERR_QUEUEING_FAIL:
+		dev_dbg(dev, "%pS: queueing failure 0x%x\n", caller, fail_reason);

why here is dev_dbg(), other is dev_err()

Quoted 21 lines
MAINTAINERS +21 -0
+		break;
+	case WAVE6_SYSERR_RESULT_NOT_READY:
+		dev_err(dev, "%pS: result not ready 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_ACCESS_VIOLATION_HW:
+		dev_err(dev, "%pS: access violation 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_WATCHDOG_TIMEOUT:
+		dev_err(dev, "%pS: watchdog timeout 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_BUS_ERROR:
+		dev_err(dev, "%pS: bus error 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_DOUBLE_FAULT:
+		dev_err(dev, "%pS: double fault 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_VPU_STILL_RUNNING:
+		dev_err(dev, "%pS: still running 0x%x\n", caller, fail_reason);
+		break;
+	default:
+		dev_err(dev, "%pS: failure: 0x%x\n", caller, fail_reason);

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+		break;
+	}
+}
+
+static void wave6_dec_set_display_buffer(struct vpu_instance *inst, struct frame_buffer fb)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int index;
+
+	for (index = 0; index < WAVE6_MAX_FBS; index++) {

Now, Prefer

	for (int index = 0; ....)
Quoted 4 lines
MAINTAINERS +4 -0
+		if (!p_dec_info->disp_buf[index].buf_y) {
+			p_dec_info->disp_buf[index] = fb;
+			p_dec_info->disp_buf[index].index = index;
+			break;

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+		}
+	}
+}
+
+static struct frame_buffer wave6_dec_get_display_buffer(struct vpu_instance *inst,
+							dma_addr_t addr)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int i;
+	struct frame_buffer fb;

struct frame_buffer fb = { .index = -1;};

MAINTAINERS

Quoted 2 lines
MAINTAINERS +2 -0
+
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {

for (int i = 0; ..)

MAINTAINERS

Quoted 5 lines
MAINTAINERS +5 -0
+		if (p_dec_info->disp_buf[i].buf_y == addr)
+			return p_dec_info->disp_buf[i];
+	}
+
+	memset(&fb, 0, sizeof(struct frame_buffer));

needn't memset here if init at declear.

Quoted 5 lines
MAINTAINERS +5 -0
+	fb.index = -1;
+
+	return fb;
+}
+

...

MAINTAINERS

Quoted 6 lines
MAINTAINERS +6 -0
+
+static int wave6_send_query(struct vpu_core_device *core, u32 id, u32 std,
+			    enum wave6_query_option query_opt)
+{
+	int ret;
+	u32 reg_val;

try keep reverise Christmas tree order.

Quoted 35 lines
MAINTAINERS +35 -0
+
+	lockdep_assert_held(&core->hw_lock);
+
+	vpu_write_reg(core, W6_QUERY_OPTION, query_opt);
+	wave6_send_command(core, id, std, W6_CMD_QUERY);
+
+	ret = wave6_wait_vpu_busy(core, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(core->dev, "query timed out opt=0x%x\n", query_opt);
+		return ret;
+	}
+
+	if (!vpu_read_reg(core, W6_RET_SUCCESS)) {
+		reg_val = vpu_read_reg(core, W6_RET_FAIL_REASON);
+		wave6_print_reg_err(core, reg_val);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int wave6_vpu_get_version(struct vpu_core_device *core)
+{
+	struct vpu_attr *attr = &core->attr;
+	int ret;
+	u32 std_def1, conf_feature;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	ret = wave6_send_query(core, 0, 0, W6_QUERY_OPT_GET_VPU_INFO);
+	if (ret)
+		return ret;
+
+	attr->product_id = wave6_vpu_get_product_id(core);
+	attr->product_code = vpu_read_reg(core, W6_VPU_RET_PRODUCT_CODE);

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+	attr->product_version = vpu_read_reg(core, W6_RET_PRODUCT_VERSION);
+	attr->fw_version = vpu_read_reg(core, W6_RET_FW_API_VERSION);
+	attr->fw_revision = vpu_read_reg(core, W6_RET_FW_VERSION);
+	attr->hw_version = vpu_read_reg(core, W6_RET_CONF_HW_VERSION);
+	std_def1 = vpu_read_reg(core, W6_RET_STD_DEF1);
+	conf_feature = vpu_read_reg(core, W6_RET_CONF_FEATURE);
+
+	attr->support_decoders = 0;
+	attr->support_encoders = 0;
+	attr->support_decoders |= STD_DEF1_HEVC_DEC(std_def1) << W_HEVC_DEC;

This is depend on STD_DEF1_HEVC_DEC() is 1 bit field. I feel like below codes is easier to read

	STD_DEF1_HEVC_DEC(std_def1) ? BIT(W_HEVC_DEC) : 0;
Quoted 10 lines
MAINTAINERS +10 -0
+	attr->support_hevc10bit_dec = CONF_FEATURE_HEVC10BIT_DEC(conf_feature);
+	attr->support_decoders |= STD_DEF1_AVC_DEC(std_def1) << W_AVC_DEC;
+	attr->support_avc10bit_dec = CONF_FEATURE_AVC10BIT_DEC(conf_feature);
+	attr->support_encoders |= STD_DEF1_HEVC_ENC(std_def1) << W_HEVC_ENC;
+	attr->support_hevc10bit_enc = CONF_FEATURE_HEVC10BIT_ENC(conf_feature);
+	attr->support_encoders |= STD_DEF1_AVC_ENC(std_def1) << W_AVC_ENC;
+	attr->support_avc10bit_enc = CONF_FEATURE_AVC10BIT_ENC(conf_feature);
+
+	return 0;
+}

...

Quoted 42 lines
MAINTAINERS +42 -0
+
+int wave6_vpu_dec_register_display_buffer(struct vpu_instance *inst, struct frame_buffer fb)
+{
+	int ret;
+	struct dec_info *p_dec_info;
+	u32 reg_val;
+	u32 c_fmt_idc, out_fmt, out_mode;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_dec_info = &inst->codec_info->dec_info;
+
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_SCL_PARAM,
+		      inst->scaler_info.enable);
+	reg_val = SET_DISP_SCL_PIC_SIZE_WIDTH(inst->scaler_info.width) |
+		  SET_DISP_SCL_PIC_SIZE_HEIGHT(inst->scaler_info.height);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_SCL_PIC_SIZE, reg_val);
+	reg_val = SET_DISP_PIC_SIZE_WIDTH(p_dec_info->seq_info.pic_width) |
+		  SET_DISP_PIC_SIZE_HEIGHT(p_dec_info->seq_info.pic_height);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_PIC_SIZE, reg_val);
+
+	c_fmt_idc = get_chroma_format_idc(p_dec_info->wtl_format);
+	switch (p_dec_info->wtl_format) {
+	case FORMAT_420_P10_16BIT_MSB:
+	case FORMAT_422_P10_16BIT_MSB:
+	case FORMAT_444_P10_16BIT_MSB:
+	case FORMAT_400_P10_16BIT_MSB:
+		out_mode = (WTL_RIGHT_JUSTIFIED << 2) | WTL_PIXEL_16BIT;
+		break;
+	case FORMAT_420_P10_16BIT_LSB:
+	case FORMAT_422_P10_16BIT_LSB:
+	case FORMAT_444_P10_16BIT_LSB:
+	case FORMAT_400_P10_16BIT_LSB:
+		out_mode = (WTL_LEFT_JUSTIFIED << 2) | WTL_PIXEL_16BIT;
+		break;
+	case FORMAT_420_P10_32BIT_MSB:
+	case FORMAT_422_P10_32BIT_MSB:
+	case FORMAT_444_P10_32BIT_MSB:
+	case FORMAT_400_P10_32BIT_MSB:
+		out_mode = (WTL_RIGHT_JUSTIFIED << 2) | WTL_PIXEL_32BIT;
+		break;
+	case FORMAT_420_P10_32BIT_LSB:

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+	case FORMAT_422_P10_32BIT_LSB:
+	case FORMAT_444_P10_32BIT_LSB:
+	case FORMAT_400_P10_32BIT_LSB:
+		out_mode = (WTL_LEFT_JUSTIFIED << 2) | WTL_PIXEL_32BIT;
+		break;
+	default:
+		out_mode = (WTL_RIGHT_JUSTIFIED << 2) | WTL_PIXEL_8BIT;
+		break;
+	}
+	out_fmt = (inst->nv21 << 1) | inst->cbcr_interleave;

Can you use macro for 1 and 2. look like it fill into SET_DISP_COMMON_PIC_INFO_OUT_FMT()

Seem you need more detail out_fmt for WTL_PIXEL_16BIT/WTL_PIXEL_32BIT/ WTL_PIXEL_8BIT and *JUSTIFIED. should use FIELD_PREP() macro for these settings.

Quoted 33 lines
MAINTAINERS +33 -0
+
+	reg_val = SET_DISP_COMMON_PIC_INFO_BWB_ON |
+		  SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(c_fmt_idc) |
+		  SET_DISP_COMMON_PIC_INFO_PIXEL_ORDER(PIXEL_ORDER_INCREASING) |
+		  SET_DISP_COMMON_PIC_INFO_OUT_MODE(out_mode) |
+		  SET_DISP_COMMON_PIC_INFO_OUT_FMT(out_fmt) |
+		  SET_DISP_COMMON_PIC_INFO_STRIDE(fb.stride);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_COMMON_PIC_INFO, reg_val);
+	reg_val = SET_DISP_OPTION_ENDIAN(VDI_128BIT_BIG_ENDIAN);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_OPTION, reg_val);
+	reg_val = SET_DISP_PIC_INFO_L_BIT_DEPTH(fb.luma_bit_depth) |
+		  SET_DISP_PIC_INFO_C_BIT_DEPTH(fb.chroma_bit_depth) |
+		  SET_DISP_PIC_INFO_C_FMT_IDC(fb.c_fmt_idc);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_PIC_INFO, reg_val);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_Y_BASE, fb.buf_y);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_CB_BASE, fb.buf_cb);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_CR_BASE, fb.buf_cr);
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_DEC_SET_DISP);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return ret;
+	}
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS))
+		return -EIO;
+
+	wave6_dec_set_display_buffer(inst, fb);
+
+	return 0;
+}
+

...

Quoted 66 lines
MAINTAINERS +66 -0
+
+int wave6_vpu_dec_get_output_info(struct vpu_instance *inst, struct dec_output_info *info)
+{
+	struct dec_info *p_dec_info;
+	u32 reg_val, i;
+	int decoded_idx = -1, disp_idx = -1;
+	int ret;
+
+	if (WARN_ON(!info))
+		return -EINVAL;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_dec_info = &inst->codec_info->dec_info;
+
+	ret = wave6_send_query(inst->dev, inst->id, inst->std, W6_QUERY_OPT_GET_RESULT);
+	if (ret) {
+		info->rd_ptr = p_dec_info->stream_rd_ptr;
+		info->wr_ptr = p_dec_info->stream_wr_ptr;
+		return ret;
+	}
+
+	info->decoding_success = vpu_read_reg(inst->dev, W6_RET_DEC_DECODING_SUCCESS);
+	if (!info->decoding_success)
+		info->error_reason = vpu_read_reg(inst->dev, W6_RET_DEC_ERR_INFO);
+	else
+		info->warn_info = vpu_read_reg(inst->dev, W6_RET_DEC_WARN_INFO);
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_PIC_TYPE);
+	info->ctu_size = DEC_PIC_TYPE_CTU_SIZE(reg_val);
+	info->nal_type = DEC_PIC_TYPE_NAL_UNIT_TYPE(reg_val);
+
+	if (reg_val & DEC_PIC_TYPE_B)
+		info->pic_type = PIC_TYPE_B;
+	else if (reg_val & DEC_PIC_TYPE_P)
+		info->pic_type = PIC_TYPE_P;
+	else if (reg_val & DEC_PIC_TYPE_I)
+		info->pic_type = PIC_TYPE_I;
+	else
+		info->pic_type = PIC_TYPE_MAX;
+	if (inst->std == W_HEVC_DEC) {
+		if (info->pic_type == PIC_TYPE_I &&
+		    (info->nal_type == H265_NAL_UNIT_TYPE_IDR_W_RADL ||
+		     info->nal_type == H265_NAL_UNIT_TYPE_IDR_N_LP))
+			info->pic_type = PIC_TYPE_IDR;
+	} else if (inst->std == W_AVC_DEC) {
+		if (info->pic_type == PIC_TYPE_I &&
+		    info->nal_type == H264_NAL_UNIT_TYPE_IDR_PICTURE)
+			info->pic_type = PIC_TYPE_IDR;
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_DECODED_FLAG);
+	if (reg_val) {
+		struct frame_buffer fb;
+		dma_addr_t addr = vpu_read_reg(inst->dev, W6_RET_DEC_DECODED_ADDR);
+
+		fb = wave6_dec_get_display_buffer(inst, addr);
+		info->frame_decoded_addr = addr;
+		info->frame_decoded = true;
+		decoded_idx = fb.index;
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_DISPLAY_FLAG);
+	if (reg_val) {
+		struct frame_buffer fb;
+		dma_addr_t addr = vpu_read_reg(inst->dev, W6_RET_DEC_DISPLAY_ADDR);

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+
+		fb = wave6_dec_get_display_buffer(inst, addr);
+		info->frame_display_addr = addr;
+		info->frame_display = true;
+		disp_idx = fb.index;
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_DISP_IDC);
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {
+		if (reg_val & (1 << i)) {

for_each_set_bit()

MAINTAINERS

Quoted 11 lines
MAINTAINERS +11 -0
+			dma_addr_t addr;
+
+			addr = vpu_read_reg(inst->dev, W6_RET_DEC_DISP_LINEAR_ADDR(i));
+
+			info->disp_frame_addr[info->disp_frame_num] = addr;
+			info->disp_frame_num++;
+		}
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_RELEASE_IDC);
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {

ditto, check other similar logic.

Quoted 11 lines
MAINTAINERS +11 -0
+		if (reg_val & (1 << i)) {
+			dma_addr_t addr;
+
+			addr = vpu_read_reg(inst->dev, W6_RET_DEC_DISP_LINEAR_ADDR(i));
+
+			wave6_dec_remove_display_buffer(inst, addr);
+			info->release_disp_frame_addr[info->release_disp_frame_num] = addr;
+			info->release_disp_frame_num++;
+		}
+	}
+

...

MAINTAINERS

Quoted 5 lines
MAINTAINERS +5 -0
+
+int wave6_vpu_enc_start_one_frame(struct vpu_instance *inst, struct enc_param *param,
+				  u32 *fail_res)
+{
+	struct enc_cmd_enc_pic_reg reg;

struct enc_cmd_enc_pic_reg reg = {};

move set 0 out of mutex lock, slice better.

Quoted 38 lines
MAINTAINERS +38 -0
+	struct enc_info *p_enc_info;
+	int ret;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_enc_info = &inst->codec_info->enc_info;
+
+	memset(&reg, 0, sizeof(struct enc_cmd_enc_pic_reg));
+
+	wave6_gen_enc_pic_reg(p_enc_info, inst->cbcr_interleave,
+			      inst->nv21, param, &reg);
+
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_BS_START, reg.bs_start);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_BS_SIZE, reg.bs_size);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_BS_OPTION, reg.bs_option);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SEC_AXI, reg.sec_axi);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_REPORT, reg.report);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_MV_HISTO0, reg.mv_histo0);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_MV_HISTO1, reg.mv_histo1);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CUSTOM_MAP_PARAM, reg.custom_map_param);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CUSTOM_MAP_ADDR, reg.custom_map_addr);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_PIC_IDX, reg.src_pic_idx);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_ADDR_Y, reg.src_addr_y);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_ADDR_U, reg.src_addr_u);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_ADDR_V, reg.src_addr_v);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_STRIDE, reg.src_stride);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_FMT, reg.src_fmt);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_AXI_SEL, reg.src_axi_sel);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CODE_OPTION, reg.code_option);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_PARAM, reg.param);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_LONGTERM_PIC, reg.longterm_pic);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_PREFIX_SEI_NAL_ADDR, reg.prefix_sei_nal_addr);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_PREFIX_SEI_INFO, reg.prefix_sei_info);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SUFFIX_SEI_NAL_ADDR, reg.suffix_sei_nal_addr);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SUFFIX_SEI_INFO, reg.suffix_sei_info);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_TIMESTAMP_LOW, reg.timestamp_low);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_TIMESTAMP_HIGH, reg.timestamp_high);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CSC_COEFF0, reg.csc_coeff[0]);

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CSC_COEFF1, reg.csc_coeff[1]);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CSC_COEFF2, reg.csc_coeff[2]);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CSC_COEFF3, reg.csc_coeff[3]);
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_ENC_PIC);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return -ETIMEDOUT;
+	}

I not sure how heavy register read() write(), consider use readl(writel)_relex(), if need write/read many registers(). Only last one need writel() before trigger DMA.

Quoted 10 lines
MAINTAINERS +10 -0
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS)) {
+		*fail_res = vpu_read_reg(inst->dev, W6_RET_FAIL_REASON);
+		wave6_print_reg_err(inst->dev, *fail_res);
+		return -EIO;
+	}
+
+	return 0;
+}
+

...

drivers/media/platform/chips-media/wave6/wave6-regdefine.h line 6

Quoted 13 lines
MAINTAINERS +1 -0
+#endif /* __WAVE6_HW_H__ */
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +6 -0
new file mode 100644
index 000000000000..1d495145bbbd
@@ -0,0 +1,649 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
+/*
+ * Wave6 series multi-standard codec IP - wave6 register definitions
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */

2026

...

Quoted 9 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +9 -0
+
+#define W6_MAX_PIC_STRIDE		(4096U * 4)
+#define W6_PIC_STRIDE_ALIGNMENT		32
+#define W6_FBC_BUF_ALIGNMENT		32
+#define W6_DEC_BUF_ALIGNMENT		32
+#define W6_DEF_DEC_PIC_WIDTH		720U
+#define W6_DEF_DEC_PIC_HEIGHT		480U
+#define W6_MIN_DEC_PIC_WIDTH		64U
+#define W6_MIN_DEC_PIC_HEIGHT		64U

drivers/media/platform/chips-media/wave6/wave6-regdefine.h line 25

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +10 -0
+#define W6_MAX_DEC_PIC_WIDTH		4096U
+#define W6_MAX_DEC_PIC_HEIGHT		4096U
+#define W6_DEC_PIC_SIZE_STEP		1
+
+#define W6_DEF_ENC_PIC_WIDTH		416U
+#define W6_DEF_ENC_PIC_HEIGHT		240U
+#define W6_MIN_ENC_PIC_WIDTH		256U
+#define W6_MIN_ENC_PIC_HEIGHT		128U
+#define W6_MAX_ENC_PIC_WIDTH		4096U
+#define W6_MAX_ENC_PIC_HEIGHT		4096U

needn't U

Quoted 11 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +11 -0
+#define W6_ENC_PIC_SIZE_STEP		8
+#define W6_ENC_CROP_X_POS_STEP		32
+#define W6_ENC_CROP_Y_POS_STEP		2
+#define W6_ENC_CROP_STEP		2
+
+#define W6_VPU_POLL_DELAY_US		10
+#define W6_VPU_POLL_TIMEOUT		300000
+#define W6_BOOT_WAIT_TIMEOUT		10000
+#define W6_VPU_TIMEOUT			6000
+#define W6_VPU_TIMEOUT_CYCLE_COUNT	(8000000 * 4 * 4)
+

...

Quoted 14 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +14 -0
+#define __WAVE6_VPUERROR_H__
+
+/* WAVE6 COMMON SYSTEM ERROR (FAIL_REASON) */
+#define WAVE6_SYSERR_QUEUEING_FAIL			0x00000001
+#define WAVE6_SYSERR_DECODER_FUSE			0x00000002
+#define WAVE6_SYSERR_INSTRUCTION_ACCESS_VIOLATION	0x00000004
+#define WAVE6_SYSERR_PRIVILEGE_VIOLATION		0x00000008
+#define WAVE6_SYSERR_DATA_ADDR_ALIGNMENT		0x00000010
+#define WAVE6_SYSERR_DATA_ACCESS_VIOLATION		0x00000020
+#define WAVE6_SYSERR_ACCESS_VIOLATION_HW		0x00000040
+#define WAVE6_SYSERR_INSTRUCTION_ADDR_ALIGNMENT		0x00000080
+#define WAVE6_SYSERR_UNKNOWN				0x00000100
+#define WAVE6_SYSERR_BUS_ERROR				0x00000200
+#define WAVE6_SYSERR_DOUBLE_FAULT			0x00000400

drivers/media/platform/chips-media/wave6/wave6-regdefine.h

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +10 -0
+#define WAVE6_SYSERR_RESULT_NOT_READY			0x00000800
+#define WAVE6_SYSERR_VPU_STILL_RUNNING			0x00001000
+#define WAVE6_SYSERR_UNKNOWN_CMD			0x00002000
+#define WAVE6_SYSERR_UNKNOWN_CODEC_STD			0x00004000
+#define WAVE6_SYSERR_UNKNOWN_QUERY_OPTION		0x00008000
+#define WAVE6_SYSERR_WATCHDOG_TIMEOUT			0x00020000
+#define WAVE6_SYSERR_NOT_SUPPORT			0x00100000
+#define WAVE6_SYSERR_TEMP_SEC_BUF_OVERFLOW		0x00200000
+#define WAVE6_SYSERR_NOT_SUPPORT_PROFILE		0x00400000
+#define WAVE6_SYSERR_TIMEOUT_CODEC_FW			0x40000000

Suppose you should get check_patch warning, to prefer use BIT(n) for this defination.

Frank

Quoted 233 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +230 -1
+#define WAVE6_SYSERR_FATAL_VPU_HANGUP			0xf0000000
+
+/* WAVE6 COMMAND QUEUE ERROR (FAIL_REASON) */
+#define WAVE6_CMDQ_ERR_NOT_QUEABLE_CMD		0x00000001
+#define WAVE6_CMDQ_ERR_SKIP_MODE_ENABLE		0x00000002
+#define WAVE6_CMDQ_ERR_INST_FLUSHING		0x00000003
+#define WAVE6_CMDQ_ERR_INST_INACTIVE		0x00000004
+#define WAVE6_CMDQ_ERR_QUEUE_FAIL		0x00000005
+#define WAVE6_CMDQ_ERR_CMD_BUF_FULL		0x00000006
+
+/* WAVE6 ERROR ON DECODER (ERR_INFO) */
+#define HEVC_SPSERR_SEQ_PARAMETER_SET_ID				0x00001000
+#define HEVC_SPSERR_CHROMA_FORMAT_IDC					0x00001001
+#define HEVC_SPSERR_PIC_WIDTH_IN_LUMA_SAMPLES				0x00001002
+#define HEVC_SPSERR_PIC_HEIGHT_IN_LUMA_SAMPLES				0x00001003
+#define HEVC_SPSERR_CONF_WIN_LEFT_OFFSET				0x00001004
+#define HEVC_SPSERR_CONF_WIN_RIGHT_OFFSET				0x00001005
+#define HEVC_SPSERR_CONF_WIN_TOP_OFFSET					0x00001006
+#define HEVC_SPSERR_CONF_WIN_BOTTOM_OFFSET				0x00001007
+#define HEVC_SPSERR_BIT_DEPTH_LUMA_MINUS8				0x00001008
+#define HEVC_SPSERR_BIT_DEPTH_CHROMA_MINUS8				0x00001009
+#define HEVC_SPSERR_LOG2_MAX_PIC_ORDER_CNT_LSB_MINUS4			0x0000100A
+#define HEVC_SPSERR_SPS_MAX_DEC_PIC_BUFFERING				0x0000100B
+#define HEVC_SPSERR_SPS_MAX_NUM_REORDER_PICS				0x0000100C
+#define HEVC_SPSERR_SPS_MAX_LATENCY_INCREASE				0x0000100D
+#define HEVC_SPSERR_LOG2_MIN_LUMA_CODING_BLOCK_SIZE_MINUS3		0x0000100E
+#define HEVC_SPSERR_LOG2_DIFF_MAX_MIN_LUMA_CODING_BLOCK_SIZE		0x0000100F
+#define HEVC_SPSERR_LOG2_MIN_TRANSFORM_BLOCK_SIZE_MINUS2		0x00001010
+#define HEVC_SPSERR_LOG2_DIFF_MAX_MIN_TRANSFORM_BLOCK_SIZE		0x00001011
+#define HEVC_SPSERR_MAX_TRANSFORM_HIERARCHY_DEPTH_INTER			0x00001012
+#define HEVC_SPSERR_MAX_TRANSFORM_HIERARCHY_DEPTH_INTRA			0x00001013
+#define HEVC_SPSERR_SCALING_LIST					0x00001014
+#define HEVC_SPSERR_LOG2_DIFF_MIN_PCM_LUMA_CODING_BLOCK_SIZE_MINUS3	0x00001015
+#define HEVC_SPSERR_LOG2_DIFF_MAX_MIN_PCM_LUMA_CODING_BLOCK_SIZE	0x00001016
+#define HEVC_SPSERR_NUM_SHORTTERM_REF_PIC_SETS				0x00001017
+#define HEVC_SPSERR_NUM_LONGTERM_REF_PICS_SPS				0x00001018
+#define HEVC_SPSERR_GBU_PARSING_ERROR					0x00001019
+#define HEVC_SPSERR_EXTENSION_FLAG					0x0000101A
+#define HEVC_SPSERR_VUI_ERROR						0x0000101B
+#define HEVC_SPSERR_ACTIVATE_SPS					0x0000101C
+#define HEVC_SPSERR_PROFILE_SPACE					0x0000101D
+#define HEVC_PPSERR_PPS_PIC_PARAMETER_SET_ID				0x00002000
+#define HEVC_PPSERR_PPS_SEQ_PARAMETER_SET_ID				0x00002001
+#define HEVC_PPSERR_NUM_REF_IDX_L0_DEFAULT_ACTIVE_MINUS1		0x00002002
+#define HEVC_PPSERR_NUM_REF_IDX_L1_DEFAULT_ACTIVE_MINUS1		0x00002003
+#define HEVC_PPSERR_INIT_QP_MINUS26					0x00002004
+#define HEVC_PPSERR_DIFF_CU_QP_DELTA_DEPTH				0x00002005
+#define HEVC_PPSERR_PPS_CB_QP_OFFSET					0x00002006
+#define HEVC_PPSERR_PPS_CR_QP_OFFSET					0x00002007
+#define HEVC_PPSERR_NUM_TILE_COLUMNS_MINUS1				0x00002008
+#define HEVC_PPSERR_NUM_TILE_ROWS_MINUS1				0x00002009
+#define HEVC_PPSERR_COLUMN_WIDTH_MINUS1					0x0000200A
+#define HEVC_PPSERR_ROW_HEIGHT_MINUS1					0x0000200B
+#define HEVC_PPSERR_PPS_BETA_OFFSET_DIV2				0x0000200C
+#define HEVC_PPSERR_PPS_TC_OFFSET_DIV2					0x0000200D
+#define HEVC_PPSERR_SCALING_LIST					0x0000200E
+#define HEVC_PPSERR_LOG2_PARALLEL_MERGE_LEVEL_MINUS2			0x0000200F
+#define HEVC_PPSERR_NUM_TILE_COLUMNS_RANGE_OUT				0x00002010
+#define HEVC_PPSERR_NUM_TILE_ROWS_RANGE_OUT				0x00002011
+#define HEVC_PPSERR_MORE_RBSP_DATA_ERROR				0x00002012
+#define HEVC_PPSERR_PPS_PIC_PARAMETER_SET_ID_RANGE_OUT			0x00002013
+#define HEVC_PPSERR_PPS_SEQ_PARAMETER_SET_ID_RANGE_OUT			0x00002014
+#define HEVC_PPSERR_NUM_REF_IDX_L0_DEFAULT_ACTIVE_MINUS1_RANGE_OUT	0x00002015
+#define HEVC_PPSERR_NUM_REF_IDX_L1_DEFAULT_ACTIVE_MINUS1_RANGE_OUT	0x00002016
+#define HEVC_PPSERR_PPS_CB_QP_OFFSET_RANGE_OUT				0x00002017
+#define HEVC_PPSERR_PPS_CR_QP_OFFSET_RANGE_OUT				0x00002018
+#define HEVC_PPSERR_COLUMN_WIDTH_MINUS1_RANGE_OUT			0x00002019
+#define HEVC_PPSERR_ROW_HEIGHT_MINUS1_RANGE_OUT				0x00002020
+#define HEVC_PPSERR_PPS_BETA_OFFSET_DIV2_RANGE_OUT			0x00002021
+#define HEVC_PPSERR_PPS_TC_OFFSET_DIV2_RANGE_OUT			0x00002022
+#define HEVC_SHERR_SLICE_PIC_PARAMETER_SET_ID				0x00003000
+#define HEVC_SHERR_ACTIVATE_PPS						0x00003001
+#define HEVC_SHERR_ACTIVATE_SPS						0x00003002
+#define HEVC_SHERR_SLICE_TYPE						0x00003003
+#define HEVC_SHERR_FIRST_SLICE_IS_DEPENDENT_SLICE			0x00003004
+#define HEVC_SHERR_SHORTTERM_REF_PIC_SET_SPS_FLAG			0x00003005
+#define HEVC_SHERR_SHORTTERM_REF_PIC_SET				0x00003006
+#define HEVC_SHERR_SHORTTERM_REF_PIC_SET_IDX				0x00003007
+#define HEVC_SHERR_NUM_LONGTERM_SPS					0x00003008
+#define HEVC_SHERR_NUM_LONGTERM_PICS					0x00003009
+#define HEVC_SHERR_LT_IDX_SPS_IS_OUT_OF_RANGE				0x0000300A
+#define HEVC_SHERR_DELTA_POC_MSB_CYCLE_LT				0x0000300B
+#define HEVC_SHERR_NUM_REF_IDX_L0_ACTIVE_MINUS1				0x0000300C
+#define HEVC_SHERR_NUM_REF_IDX_L1_ACTIVE_MINUS1				0x0000300D
+#define HEVC_SHERR_COLLOCATED_REF_IDX					0x0000300E
+#define HEVC_SHERR_PRED_WEIGHT_TABLE					0x0000300F
+#define HEVC_SHERR_FIVE_MINUS_MAX_NUM_MERGE_CAND			0x00003010
+#define HEVC_SHERR_SLICE_QP_DELTA					0x00003011
+#define HEVC_SHERR_SLICE_QP_DELTA_IS_OUT_OF_RANGE			0x00003012
+#define HEVC_SHERR_SLICE_CB_QP_OFFSET					0x00003013
+#define HEVC_SHERR_SLICE_CR_QP_OFFSET					0x00003014
+#define HEVC_SHERR_SLICE_BETA_OFFSET_DIV2				0x00003015
+#define HEVC_SHERR_SLICE_TC_OFFSET_DIV2					0x00003016
+#define HEVC_SHERR_NUM_ENTRY_POINT_OFFSETS				0x00003017
+#define HEVC_SHERR_OFFSET_LEN_MINUS1					0x00003018
+#define HEVC_SHERR_SLICE_SEGMENT_HEADER_EXTENSION_LENGTH		0x00003019
+#define HEVC_SHERR_WRONG_POC_IN_STILL_PICTURE_PROFILE			0x0000301A
+#define HEVC_SHERR_SLICE_TYPE_ERROR_IN_STILL_PICTURE_PROFILE		0x0000301B
+#define HEVC_SHERR_PPS_ID_NOT_EQUAL_PREV_VALUE				0x0000301C
+#define HEVC_SPECERR_OVER_PICTURE_WIDTH_SIZE				0x00004000
+#define HEVC_SPECERR_OVER_PICTURE_HEIGHT_SIZE				0x00004001
+#define HEVC_SPECERR_OVER_CHROMA_FORMAT					0x00004002
+#define HEVC_SPECERR_OVER_BIT_DEPTH					0x00004003
+#define HEVC_SPECERR_OVER_BUFFER_OVER_FLOW				0x00004004
+#define HEVC_SPECERR_OVER_WRONG_BUFFER_ACCESS				0x00004005
+#define HEVC_ETCERR_INIT_SEQ_SPS_NOT_FOUND				0x00005000
+#define HEVC_ETCERR_DEC_PIC_VCL_NOT_FOUND				0x00005001
+#define HEVC_ETCERR_NO_VALID_SLICE_IN_AU				0x00005002
+#define HEVC_ETCERR_INPLACE_V						0x0000500F
+
+#define AVC_SPSERR_SEQ_PARAMETER_SET_ID					0x00001000
+#define AVC_SPSERR_CHROMA_FORMAT_IDC					0x00001001
+#define AVC_SPSERR_PIC_WIDTH_IN_LUMA_SAMPLES				0x00001002
+#define AVC_SPSERR_PIC_HEIGHT_IN_LUMA_SAMPLES				0x00001003
+#define AVC_SPSERR_CONF_WIN_LEFT_OFFSET					0x00001004
+#define AVC_SPSERR_CONF_WIN_RIGHT_OFFSET				0x00001005
+#define AVC_SPSERR_CONF_WIN_TOP_OFFSET					0x00001006
+#define AVC_SPSERR_CONF_WIN_BOTTOM_OFFSET				0x00001007
+#define AVC_SPSERR_BIT_DEPTH_LUMA_MINUS8				0x00001008
+#define AVC_SPSERR_BIT_DEPTH_CHROMA_MINUS8				0x00001009
+#define AVC_SPSERR_SPS_MAX_DEC_PIC_BUFFERING				0x0000100B
+#define AVC_SPSERR_SPS_MAX_NUM_REORDER_PICS				0x0000100C
+#define AVC_SPSERR_SCALING_LIST						0x00001014
+#define AVC_SPSERR_GBU_PARSING_ERROR					0x00001019
+#define AVC_SPSERR_VUI_ERROR						0x0000101B
+#define AVC_SPSERR_ACTIVATE_SPS						0x0000101C
+#define AVC_PPSERR_PPS_PIC_PARAMETER_SET_ID				0x00002000
+#define AVC_PPSERR_PPS_SEQ_PARAMETER_SET_ID				0x00002001
+#define AVC_PPSERR_NUM_REF_IDX_L0_DEFAULT_ACTIVE_MINUS1			0x00002002
+#define AVC_PPSERR_NUM_REF_IDX_L1_DEFAULT_ACTIVE_MINUS1			0x00002003
+#define AVC_PPSERR_INIT_QP_MINUS26					0x00002004
+#define AVC_PPSERR_PPS_CB_QP_OFFSET					0x00002006
+#define AVC_PPSERR_PPS_CR_QP_OFFSET					0x00002007
+#define AVC_PPSERR_SCALING_LIST						0x0000200E
+#define AVC_PPSERR_MORE_RBSP_DATA_ERROR					0x00002012
+#define AVC_PPSERR_PPS_PIC_PARAMETER_SET_ID_RANGE_OUT			0x00002013
+#define AVC_PPSERR_PPS_SEQ_PARAMETER_SET_ID_RANGE_OUT			0x00002014
+#define AVC_PPSERR_NUM_REF_IDX_L0_DEFAULT_ACTIVE_MINUS1_RANGE_OUT	0x00002015
+#define AVC_PPSERR_NUM_REF_IDX_L1_DEFAULT_ACTIVE_MINUS1_RANGE_OUT	0x00002016
+#define AVC_PPSERR_PPS_CB_QP_OFFSET_RANGE_OUT				0x00002017
+#define AVC_PPSERR_PPS_CR_QP_OFFSET_RANGE_OUT				0x00002018
+#define AVC_SHERR_SLICE_PIC_PARAMETER_SET_ID				0x00003000
+#define AVC_SHERR_ACTIVATE_PPS						0x00003001
+#define AVC_SHERR_ACTIVATE_SPS						0x00003002
+#define AVC_SHERR_SLICE_TYPE						0x00003003
+#define AVC_SHERR_FIRST_MB_IN_SLICE					0x00003004
+#define AVC_SHERR_RPLM							0x00003006
+#define AVC_SHERR_LT_IDX_SPS_IS_OUT_OF_RANGE				0x0000300A
+#define AVC_SHERR_NUM_REF_IDX_L0_ACTIVE_MINUS1				0x0000300C
+#define AVC_SHERR_NUM_REF_IDX_L1_ACTIVE_MINUS1				0x0000300D
+#define AVC_SHERR_PRED_WEIGHT_TABLE					0x0000300F
+#define AVC_SHERR_SLICE_QP_DELTA					0x00003011
+#define AVC_SHERR_SLICE_BETA_OFFSET_DIV2				0x00003015
+#define AVC_SHERR_SLICE_TC_OFFSET_DIV2					0x00003016
+#define AVC_SHERR_DISABLE_DEBLOCK_FILTER_IDC				0x00003017
+#define AVC_SPECERR_OVER_PICTURE_WIDTH_SIZE				0x00004000
+#define AVC_SPECERR_OVER_PICTURE_HEIGHT_SIZE				0x00004001
+#define AVC_SPECERR_OVER_CHROMA_FORMAT					0x00004002
+#define AVC_SPECERR_OVER_BIT_DEPTH					0x00004003
+#define AVC_SPECERR_OVER_BUFFER_OVER_FLOW				0x00004004
+#define AVC_SPECERR_OVER_WRONG_BUFFER_ACCESS				0x00004005
+#define AVC_ETCERR_INIT_SEQ_SPS_NOT_FOUND				0x00005000
+#define AVC_ETCERR_DEC_PIC_VCL_NOT_FOUND				0x00005001
+#define AVC_ETCERR_NO_VALID_SLICE_IN_AU					0x00005002
+#define AVC_ETCERR_ASO							0x00005004
+#define AVC_ETCERR_FMO							0x00005005
+#define AVC_ETCERR_INPLACE_V						0x0000500F
+
+/* WAVE6 WARNING ON DECODER (WARN_INFO) */
+#define HEVC_SPSWARN_MAX_SUB_LAYERS_MINUS1			0x00000001
+#define HEVC_SPSWARN_GENERAL_RESERVED_ZERO_44BITS		0x00000002
+#define HEVC_SPSWARN_RESERVED_ZERO_2BITS			0x00000004
+#define HEVC_SPSWARN_SUB_LAYER_RESERVED_ZERO_44BITS		0x00000008
+#define HEVC_SPSWARN_GENERAL_LEVEL_IDC				0x00000010
+#define HEVC_SPSWARN_SPS_MAX_DEC_PIC_BUFFERING_VALUE_OVER	0x00000020
+#define HEVC_SPSWARN_RBSP_TRAILING_BITS				0x00000040
+#define HEVC_SPSWARN_ST_RPS_UE_ERROR				0x00000080
+#define HEVC_SPSWARN_EXTENSION_FLAG				0x01000000
+#define HEVC_SPSWARN_REPLACED_WITH_PREV_SPS			0x02000000
+#define HEVC_PPSWARN_RBSP_TRAILING_BITS				0x00000100
+#define HEVC_PPSWARN_REPLACED_WITH_PREV_PPS			0x00000200
+#define HEVC_SHWARN_FIRST_SLICE_SEGMENT_IN_PIC_FLAG		0x00001000
+#define HEVC_SHWARN_NO_OUTPUT_OF_PRIOR_PICS_FLAG		0x00002000
+#define HEVC_SHWARN_PIC_OUTPUT_FLAG				0x00004000
+#define HEVC_SHWARN_DUPLICATED_SLICE_SEGMENT			0x00008000
+#define HEVC_ETCWARN_INIT_SEQ_VCL_NOT_FOUND			0x00010000
+#define HEVC_ETCWARN_MISSING_REFERENCE_PICTURE			0x00020000
+#define HEVC_ETCWARN_WRONG_TEMPORAL_ID				0x00040000
+#define HEVC_ETCWARN_ERROR_PICTURE_IS_REFERENCED		0x00080000
+#define HEVC_SPECWARN_OVER_PROFILE				0x00100000
+#define HEVC_SPECWARN_OVER_LEVEL				0x00200000
+#define HEVC_PRESWARN_PARSING_ERR				0x04000000
+#define HEVC_PRESWARN_MVD_OUT_OF_RANGE				0x08000000
+#define HEVC_PRESWARN_CU_QP_DELTA_VAL_OUT_OF_RANGE		0x09000000
+#define HEVC_PRESWARN_COEFF_LEVEL_REMAINING_OUT_OF_RANGE	0x0A000000
+#define HEVC_PRESWARN_PCM_ERR					0x0B000000
+#define HEVC_PRESWARN_OVERCONSUME				0x0C000000
+#define HEVC_PRESWARN_END_OF_SUBSET_ONE_BIT_ERR			0x10000000
+#define HEVC_PRESWARN_END_OF_SLICE_SEGMENT_FLAG			0x20000000
+
+#define AVC_SPSWARN_RESERVED_ZERO_2BITS				0x00000004
+#define AVC_SPSWARN_GENERAL_LEVEL_IDC				0x00000010
+#define AVC_SPSWARN_RBSP_TRAILING_BITS				0x00000040
+#define AVC_PPSWARN_RBSP_TRAILING_BITS				0x00000100
+#define AVC_SHWARN_NO_OUTPUT_OF_PRIOR_PICS_FLAG			0x00002000
+#define AVC_ETCWARN_INIT_SEQ_VCL_NOT_FOUND			0x00010000
+#define AVC_ETCWARN_MISSING_REFERENCE_PICTURE			0x00020000
+#define AVC_ETCWARN_ERROR_PICTURE_IS_REFERENCED			0x00080000
+#define AVC_SPECWARN_OVER_PROFILE				0x00100000
+#define AVC_SPECWARN_OVER_LEVEL					0x00200000
+#define AVC_PRESWARN_MVD_RANGE_OUT				0x00400000
+#define AVC_PRESWARN_MB_QPD_RANGE_OUT				0x00500000
+#define AVC_PRESWARN_COEFF_RANGE_OUT				0x00600000
+#define AVC_PRESWARN_MV_RANGE_OUT				0x00700000
+#define AVC_PRESWARN_MB_SKIP_RUN_RANGE_OUT			0x00800000
+#define AVC_PRESWARN_MB_TYPE_RANGE_OUT				0x00900000
+#define AVC_PRESWARN_SUB_MB_TYPE_RANGE_OUT			0x00A00000
+#define AVC_PRESWARN_CBP_RANGE_OUT				0x00B00000
+#define AVC_PRESWARN_INTRA_CHROMA_PRED_MODE_RANGE_OUT		0x00C00000
+#define AVC_PRESWARN_REF_IDX_RANGE_OUT				0x00D00000
+#define AVC_PRESWARN_COEFF_TOKEN_RANGE_OUT			0x00E00000
+#define AVC_PRESWARN_TOTAL_ZERO_RANGE_OUT			0x00F00000
+#define AVC_PRESWARN_RUN_BEFORE_RANGE_OUT			0x01000000
+#define AVC_PRESWARN_OVERCONSUME				0x01100000
+#define AVC_PRESWARN_MISSING_SLICE				0x01200000
+
+/* WAVE6 WARNING ON ENCODER (WARN_INFO) */
+#define WAVE6_ETCWARN_FORCED_SPLIT_BY_CU8X8	0x00000001
+
+#endif /* __WAVE6_VPUERROR_H__ */
--

2.31.1

author
#11 lore raw

RE: [PATCH v7 3/9] media: chips-media: wave6: Add Wave6 VPU interface

Hi, Frank.

Quoted 18 lines

-----Original Message-----
From: Frank Li <[email protected]>
Sent: Thursday, September 10, 2026 4:50 AM
To: Nas Chung <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jackson.lee <[email protected]>; lafley.kim <[email protected]>; [email protected]; Ming Qian <[email protected]>
Subject: Re: [PATCH v7 3/9] media: chips-media: wave6: Add Wave6 VPU interface

Quoted 3 lines · Nas Chung

Add an interface layer to manage hardware register configuration and communication with the Chips&Media Wave6 video codec IP.

Quoted 8 lines
Quoted 6 lines

The interface provides low-level helper functions used by the Wave6 core driver to implement video encoding and decoding operations. It handles command submission to the firmware via MMIO registers, and waits for a response by polling the firmware busy flag.

Nit: your s-o-b is last one

OK.

Quoted 18 lines
Quoted 4 lines

---

...

MAINTAINERS M: Jackson Lee <[email protected]> · line 29242

Quoted 10 lines
MAINTAINERS +1 -0
index 7387a11facbe..e29018c2546b 100644
@@ -29239,6 +29239,7 @@ M:	Jackson Lee <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml
+F:	drivers/media/platform/chips-media/wave6/

Consider nxp is one major user, can you add

Quoted 1 line

L: [email protected]

Sure, I'll add it in v8.

Quoted 8 lines

...

Quoted 4 lines

+/*
+ * Wave6 series multi-standard codec IP - wave6 backend interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC

2026

I'll update the other files as well.

Quoted 6 lines

MAINTAINERS

Quoted 4 lines

+ */

MAINTAINERS +2 -0
+
+#include <linux/iopoll.h>

Add space line here

OK.

Quoted 33 lines
Quoted 13 lines
MAINTAINERS +12 -0
+#include "wave6-vpu-core.h"
+#include "wave6-hw.h"
+#include "wave6-regdefine.h"
+#include "wave6-trace.h"
+
+void wave6_vpu_writel(struct vpu_core_device *core, u32 addr, u32 data)
+{
+	wave6_vdi_writel(core->reg_base, addr, data);
+	trace_wave6_vpu_writel(core->dev, addr, data);
+}
+
+u32 wave6_vpu_readl(struct vpu_core_device *core, u32 addr)

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+{
+	u32 data;
+
+	data = wave6_vdi_readl(core->reg_base, addr);
+	trace_wave6_vpu_readl(core->dev, addr, data);
+
+	return data;
+}
+
+static void wave6_print_reg_err(struct vpu_core_device *core, u32

fail_reason)

MAINTAINERS

Quoted 7 lines
MAINTAINERS +7 -0
+{
+	void *caller = __builtin_return_address(0);
+	struct device *dev = core->dev;
+
+	switch (fail_reason) {
+	case WAVE6_SYSERR_QUEUEING_FAIL:
+		dev_dbg(dev, "%pS: queueing failure 0x%x\n", caller,

fail_reason);

Quoted 1 line

why here is dev_dbg(), other is dev_err()

I'll change it to dev_err().

Quoted 40 lines

MAINTAINERS

Quoted 4 lines
MAINTAINERS +3 -0
+		break;
+	case WAVE6_SYSERR_RESULT_NOT_READY:
+		dev_err(dev, "%pS: result not ready 0x%x\n", caller,

fail_reason);

MAINTAINERS

Quoted 3 lines
MAINTAINERS +3 -0
+		break;
+	case WAVE6_SYSERR_ACCESS_VIOLATION_HW:
+		dev_err(dev, "%pS: access violation 0x%x\n", caller,

fail_reason);

MAINTAINERS

Quoted 3 lines
MAINTAINERS +3 -0
+		break;
+	case WAVE6_SYSERR_WATCHDOG_TIMEOUT:
+		dev_err(dev, "%pS: watchdog timeout 0x%x\n", caller,

fail_reason);

MAINTAINERS

Quoted 9 lines
MAINTAINERS +9 -0
+		break;
+	case WAVE6_SYSERR_BUS_ERROR:
+		dev_err(dev, "%pS: bus error 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_DOUBLE_FAULT:
+		dev_err(dev, "%pS: double fault 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_VPU_STILL_RUNNING:
+		dev_err(dev, "%pS: still running 0x%x\n", caller,

fail_reason);

MAINTAINERS

Quoted 8 lines
MAINTAINERS +8 -0
+		break;
+	default:
+		dev_err(dev, "%pS: failure: 0x%x\n", caller, fail_reason);
+		break;
+	}
+}
+
+static void wave6_dec_set_display_buffer(struct vpu_instance *inst,

struct frame_buffer fb)

MAINTAINERS

Quoted 5 lines
MAINTAINERS +5 -0
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int index;
+
+	for (index = 0; index < WAVE6_MAX_FBS; index++) {

Now, Prefer

Quoted 1 line
	for (int index = 0; ....)

Agreed. I'll check for the same pattern elsewhere.

Quoted 24 lines

MAINTAINERS

Quoted 10 lines
MAINTAINERS +9 -0
+		if (!p_dec_info->disp_buf[index].buf_y) {
+			p_dec_info->disp_buf[index] = fb;
+			p_dec_info->disp_buf[index].index = index;
+			break;
+		}
+	}
+}
+
+static struct frame_buffer wave6_dec_get_display_buffer(struct

vpu_instance *inst,

MAINTAINERS

Quoted 5 lines
MAINTAINERS +5 -0
+							dma_addr_t addr)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int i;
+	struct frame_buffer fb;

struct frame_buffer fb = { .index = -1;};

MAINTAINERS

Quoted 2 lines
MAINTAINERS +2 -0
+
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {

for (int i = 0; ..)

Quoted 7 lines

MAINTAINERS

Quoted 5 lines
MAINTAINERS +5 -0
+		if (p_dec_info->disp_buf[i].buf_y == addr)
+			return p_dec_info->disp_buf[i];
+	}
+
+	memset(&fb, 0, sizeof(struct frame_buffer));

needn't memset here if init at declear.

Agreed. I'll fix wave6_dec_get_display_buffer() in v8.

Quoted 10 lines
Quoted 6 lines
MAINTAINERS +5 -0
+	fb.index = -1;
+
+	return fb;
+}
+

...

MAINTAINERS

Quoted 2 lines
MAINTAINERS +2 -0
+
+static int wave6_send_query(struct vpu_core_device *core, u32 id, u32

std,

Quoted 6 lines

MAINTAINERS

Quoted 4 lines
MAINTAINERS +4 -0
+			    enum wave6_query_option query_opt)
+{
+	int ret;
+	u32 reg_val;

try keep reverise Christmas tree order.

OK, I'll fix it.

Quoted 50 lines
Quoted 36 lines
MAINTAINERS +35 -0
+
+	lockdep_assert_held(&core->hw_lock);
+
+	vpu_write_reg(core, W6_QUERY_OPTION, query_opt);
+	wave6_send_command(core, id, std, W6_CMD_QUERY);
+
+	ret = wave6_wait_vpu_busy(core, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(core->dev, "query timed out opt=0x%x\n", query_opt);
+		return ret;
+	}
+
+	if (!vpu_read_reg(core, W6_RET_SUCCESS)) {
+		reg_val = vpu_read_reg(core, W6_RET_FAIL_REASON);
+		wave6_print_reg_err(core, reg_val);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int wave6_vpu_get_version(struct vpu_core_device *core)
+{
+	struct vpu_attr *attr = &core->attr;
+	int ret;
+	u32 std_def1, conf_feature;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	ret = wave6_send_query(core, 0, 0, W6_QUERY_OPT_GET_VPU_INFO);
+	if (ret)
+		return ret;
+
+	attr->product_id = wave6_vpu_get_product_id(core);
+	attr->product_code = vpu_read_reg(core, W6_VPU_RET_PRODUCT_CODE);

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+	attr->product_version = vpu_read_reg(core, W6_RET_PRODUCT_VERSION);
+	attr->fw_version = vpu_read_reg(core, W6_RET_FW_API_VERSION);
+	attr->fw_revision = vpu_read_reg(core, W6_RET_FW_VERSION);
+	attr->hw_version = vpu_read_reg(core, W6_RET_CONF_HW_VERSION);
+	std_def1 = vpu_read_reg(core, W6_RET_STD_DEF1);
+	conf_feature = vpu_read_reg(core, W6_RET_CONF_FEATURE);
+
+	attr->support_decoders = 0;
+	attr->support_encoders = 0;
+	attr->support_decoders |= STD_DEF1_HEVC_DEC(std_def1) << W_HEVC_DEC;

This is depend on STD_DEF1_HEVC_DEC() is 1 bit field. I feel like below codes is easier to read

Quoted 1 line
	STD_DEF1_HEVC_DEC(std_def1) ? BIT(W_HEVC_DEC) : 0;

Agreed, I'll change it in v8.

Quoted 62 lines

MAINTAINERS

Quoted 2 lines
MAINTAINERS +1 -0
+	attr->support_hevc10bit_dec =

CONF_FEATURE_HEVC10BIT_DEC(conf_feature);

MAINTAINERS

Quoted 4 lines
MAINTAINERS +4 -0
+	attr->support_decoders |= STD_DEF1_AVC_DEC(std_def1) << W_AVC_DEC;
+	attr->support_avc10bit_dec = CONF_FEATURE_AVC10BIT_DEC(conf_feature);
+	attr->support_encoders |= STD_DEF1_HEVC_ENC(std_def1) << W_HEVC_ENC;
+	attr->support_hevc10bit_enc =

CONF_FEATURE_HEVC10BIT_ENC(conf_feature);

Quoted 5 lines
MAINTAINERS +5 -0
+	attr->support_encoders |= STD_DEF1_AVC_ENC(std_def1) << W_AVC_ENC;
+	attr->support_avc10bit_enc = CONF_FEATURE_AVC10BIT_ENC(conf_feature);
+
+	return 0;
+}

...

MAINTAINERS

Quoted 2 lines
MAINTAINERS +2 -0
+
+int wave6_vpu_dec_register_display_buffer(struct vpu_instance *inst,

struct frame_buffer fb)

Quoted 45 lines
MAINTAINERS +45 -0
+{
+	int ret;
+	struct dec_info *p_dec_info;
+	u32 reg_val;
+	u32 c_fmt_idc, out_fmt, out_mode;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_dec_info = &inst->codec_info->dec_info;
+
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_SCL_PARAM,
+		      inst->scaler_info.enable);
+	reg_val = SET_DISP_SCL_PIC_SIZE_WIDTH(inst->scaler_info.width) |
+		  SET_DISP_SCL_PIC_SIZE_HEIGHT(inst->scaler_info.height);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_SCL_PIC_SIZE, reg_val);
+	reg_val = SET_DISP_PIC_SIZE_WIDTH(p_dec_info->seq_info.pic_width) |
+		  SET_DISP_PIC_SIZE_HEIGHT(p_dec_info->seq_info.pic_height);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_PIC_SIZE, reg_val);
+
+	c_fmt_idc = get_chroma_format_idc(p_dec_info->wtl_format);
+	switch (p_dec_info->wtl_format) {
+	case FORMAT_420_P10_16BIT_MSB:
+	case FORMAT_422_P10_16BIT_MSB:
+	case FORMAT_444_P10_16BIT_MSB:
+	case FORMAT_400_P10_16BIT_MSB:
+		out_mode = (WTL_RIGHT_JUSTIFIED << 2) | WTL_PIXEL_16BIT;
+		break;
+	case FORMAT_420_P10_16BIT_LSB:
+	case FORMAT_422_P10_16BIT_LSB:
+	case FORMAT_444_P10_16BIT_LSB:
+	case FORMAT_400_P10_16BIT_LSB:
+		out_mode = (WTL_LEFT_JUSTIFIED << 2) | WTL_PIXEL_16BIT;
+		break;
+	case FORMAT_420_P10_32BIT_MSB:
+	case FORMAT_422_P10_32BIT_MSB:
+	case FORMAT_444_P10_32BIT_MSB:
+	case FORMAT_400_P10_32BIT_MSB:
+		out_mode = (WTL_RIGHT_JUSTIFIED << 2) | WTL_PIXEL_32BIT;
+		break;
+	case FORMAT_420_P10_32BIT_LSB:
+	case FORMAT_422_P10_32BIT_LSB:
+	case FORMAT_444_P10_32BIT_LSB:
+	case FORMAT_400_P10_32BIT_LSB:
+		out_mode = (WTL_LEFT_JUSTIFIED << 2) | WTL_PIXEL_32BIT;
+		break;
Quoted 8 lines

MAINTAINERS

Quoted 5 lines
MAINTAINERS +5 -0
+	default:
+		out_mode = (WTL_RIGHT_JUSTIFIED << 2) | WTL_PIXEL_8BIT;
+		break;
+	}
+	out_fmt = (inst->nv21 << 1) | inst->cbcr_interleave;

Can you use macro for 1 and 2. look like it fill into SET_DISP_COMMON_PIC_INFO_OUT_FMT()

Agreed.

Quoted 4 lines

Seem you need more detail out_fmt for WTL_PIXEL_16BIT/WTL_PIXEL_32BIT/ WTL_PIXEL_8BIT and *JUSTIFIED. should use FIELD_PREP() macro for these settings.

OK, I'll use FIELD_PREP() for out_mode.

Quoted 111 lines

MAINTAINERS

Quoted 5 lines
MAINTAINERS +4 -0
+
+	reg_val = SET_DISP_COMMON_PIC_INFO_BWB_ON |
+		  SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(c_fmt_idc) |
+		  SET_DISP_COMMON_PIC_INFO_PIXEL_ORDER(PIXEL_ORDER_INCREASING)

|

MAINTAINERS

Quoted 4 lines
MAINTAINERS +4 -0
+		  SET_DISP_COMMON_PIC_INFO_OUT_MODE(out_mode) |
+		  SET_DISP_COMMON_PIC_INFO_OUT_FMT(out_fmt) |
+		  SET_DISP_COMMON_PIC_INFO_STRIDE(fb.stride);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_COMMON_PIC_INFO,

reg_val);

MAINTAINERS

Quoted 11 lines
MAINTAINERS +11 -0
+	reg_val = SET_DISP_OPTION_ENDIAN(VDI_128BIT_BIG_ENDIAN);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_OPTION, reg_val);
+	reg_val = SET_DISP_PIC_INFO_L_BIT_DEPTH(fb.luma_bit_depth) |
+		  SET_DISP_PIC_INFO_C_BIT_DEPTH(fb.chroma_bit_depth) |
+		  SET_DISP_PIC_INFO_C_FMT_IDC(fb.c_fmt_idc);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_PIC_INFO, reg_val);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_Y_BASE, fb.buf_y);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_CB_BASE, fb.buf_cb);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_CR_BASE, fb.buf_cr);
+
+	wave6_send_command(inst->dev, inst->id, inst->std,

W6_CMD_DEC_SET_DISP);

Quoted 14 lines
MAINTAINERS +14 -0
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return ret;
+	}
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS))
+		return -EIO;
+
+	wave6_dec_set_display_buffer(inst, fb);
+
+	return 0;
+}
+

...

MAINTAINERS

Quoted 2 lines
MAINTAINERS +2 -0
+
+int wave6_vpu_dec_get_output_info(struct vpu_instance *inst, struct

dec_output_info *info)

Quoted 4 lines
MAINTAINERS +4 -0
+{
+	struct dec_info *p_dec_info;
+	u32 reg_val, i;
+	int decoded_idx = -1, disp_idx = -1;

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+	int ret;
+
+	if (WARN_ON(!info))
+		return -EINVAL;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_dec_info = &inst->codec_info->dec_info;
+
+	ret = wave6_send_query(inst->dev, inst->id, inst->std,

W6_QUERY_OPT_GET_RESULT);

MAINTAINERS

Quoted 7 lines
MAINTAINERS +7 -0
+	if (ret) {
+		info->rd_ptr = p_dec_info->stream_rd_ptr;
+		info->wr_ptr = p_dec_info->stream_wr_ptr;
+		return ret;
+	}
+
+	info->decoding_success = vpu_read_reg(inst->dev,

W6_RET_DEC_DECODING_SUCCESS);

MAINTAINERS

Quoted 2 lines
MAINTAINERS +2 -0
+	if (!info->decoding_success)
+		info->error_reason = vpu_read_reg(inst->dev,

W6_RET_DEC_ERR_INFO);

MAINTAINERS

Quoted 2 lines
MAINTAINERS +2 -0
+	else
+		info->warn_info = vpu_read_reg(inst->dev,

W6_RET_DEC_WARN_INFO);

Quoted 18 lines
MAINTAINERS +18 -0
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_PIC_TYPE);
+	info->ctu_size = DEC_PIC_TYPE_CTU_SIZE(reg_val);
+	info->nal_type = DEC_PIC_TYPE_NAL_UNIT_TYPE(reg_val);
+
+	if (reg_val & DEC_PIC_TYPE_B)
+		info->pic_type = PIC_TYPE_B;
+	else if (reg_val & DEC_PIC_TYPE_P)
+		info->pic_type = PIC_TYPE_P;
+	else if (reg_val & DEC_PIC_TYPE_I)
+		info->pic_type = PIC_TYPE_I;
+	else
+		info->pic_type = PIC_TYPE_MAX;
+	if (inst->std == W_HEVC_DEC) {
+		if (info->pic_type == PIC_TYPE_I &&
+		    (info->nal_type == H265_NAL_UNIT_TYPE_IDR_W_RADL ||
+		     info->nal_type == H265_NAL_UNIT_TYPE_IDR_N_LP))
+			info->pic_type = PIC_TYPE_IDR;

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+	} else if (inst->std == W_AVC_DEC) {
+		if (info->pic_type == PIC_TYPE_I &&
+		    info->nal_type == H264_NAL_UNIT_TYPE_IDR_PICTURE)
+			info->pic_type = PIC_TYPE_IDR;
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_DECODED_FLAG);
+	if (reg_val) {
+		struct frame_buffer fb;
+		dma_addr_t addr = vpu_read_reg(inst->dev,

W6_RET_DEC_DECODED_ADDR);

MAINTAINERS

Quoted 11 lines
MAINTAINERS +11 -0
+
+		fb = wave6_dec_get_display_buffer(inst, addr);
+		info->frame_decoded_addr = addr;
+		info->frame_decoded = true;
+		decoded_idx = fb.index;
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_DISPLAY_FLAG);
+	if (reg_val) {
+		struct frame_buffer fb;
+		dma_addr_t addr = vpu_read_reg(inst->dev,

W6_RET_DEC_DISPLAY_ADDR);

Quoted 12 lines

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+
+		fb = wave6_dec_get_display_buffer(inst, addr);
+		info->frame_display_addr = addr;
+		info->frame_display = true;
+		disp_idx = fb.index;
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_DISP_IDC);
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {
+		if (reg_val & (1 << i)) {

for_each_set_bit()

OK.

Quoted 5 lines

MAINTAINERS

Quoted 4 lines
MAINTAINERS +3 -0
+			dma_addr_t addr;
+
+			addr = vpu_read_reg(inst->dev,

W6_RET_DEC_DISP_LINEAR_ADDR(i));

Quoted 10 lines

MAINTAINERS

Quoted 8 lines
MAINTAINERS +8 -0
+
+			info->disp_frame_addr[info->disp_frame_num] = addr;
+			info->disp_frame_num++;
+		}
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_RELEASE_IDC);
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {

ditto, check other similar logic.

OK.

Quoted 18 lines

MAINTAINERS

Quoted 5 lines
MAINTAINERS +4 -0
+		if (reg_val & (1 << i)) {
+			dma_addr_t addr;
+
+			addr = vpu_read_reg(inst->dev,

W6_RET_DEC_DISP_LINEAR_ADDR(i));

Quoted 8 lines
MAINTAINERS +3 -0
+
+			wave6_dec_remove_display_buffer(inst, addr);
+			info->release_disp_frame_addr[info-

release_disp_frame_num] = addr;

MAINTAINERS +4 -0
+			info->release_disp_frame_num++;
+		}
+	}
+

...

MAINTAINERS

Quoted 2 lines
MAINTAINERS +2 -0
+
+int wave6_vpu_enc_start_one_frame(struct vpu_instance *inst, struct

enc_param *param,

Quoted 7 lines

MAINTAINERS

Quoted 3 lines
MAINTAINERS +3 -0
+				  u32 *fail_res)
+{
+	struct enc_cmd_enc_pic_reg reg;

struct enc_cmd_enc_pic_reg reg = {};

move set 0 out of mutex lock, slice better.

OK, I'll fix this in v8.

Quoted 58 lines
Quoted 11 lines
MAINTAINERS +10 -0
+	struct enc_info *p_enc_info;
+	int ret;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_enc_info = &inst->codec_info->enc_info;
+
+	memset(&reg, 0, sizeof(struct enc_cmd_enc_pic_reg));
+
+	wave6_gen_enc_pic_reg(p_enc_info, inst->cbcr_interleave,

MAINTAINERS

Quoted 10 lines
MAINTAINERS +10 -0
+			      inst->nv21, param, &reg);
+
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_BS_START, reg.bs_start);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_BS_SIZE, reg.bs_size);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_BS_OPTION, reg.bs_option);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SEC_AXI, reg.sec_axi);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_REPORT, reg.report);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_MV_HISTO0, reg.mv_histo0);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_MV_HISTO1, reg.mv_histo1);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CUSTOM_MAP_PARAM,

reg.custom_map_param);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CUSTOM_MAP_ADDR,

reg.custom_map_addr);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_PIC_IDX,

reg.src_pic_idx);

MAINTAINERS

Quoted 6 lines
MAINTAINERS +6 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_ADDR_Y, reg.src_addr_y);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_ADDR_U, reg.src_addr_u);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_ADDR_V, reg.src_addr_v);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_STRIDE, reg.src_stride);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_FMT, reg.src_fmt);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_AXI_SEL,

reg.src_axi_sel);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CODE_OPTION,

reg.code_option);

MAINTAINERS

Quoted 2 lines
MAINTAINERS +2 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_PARAM, reg.param);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_LONGTERM_PIC,

reg.longterm_pic);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_PREFIX_SEI_NAL_ADDR,

reg.prefix_sei_nal_addr);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_PREFIX_SEI_INFO,

reg.prefix_sei_info);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SUFFIX_SEI_NAL_ADDR,

reg.suffix_sei_nal_addr);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SUFFIX_SEI_INFO,

reg.suffix_sei_info);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_TIMESTAMP_LOW,

reg.timestamp_low);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_TIMESTAMP_HIGH,

reg.timestamp_high);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CSC_COEFF0,

reg.csc_coeff[0]);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CSC_COEFF1,

reg.csc_coeff[1]);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CSC_COEFF2,

reg.csc_coeff[2]);

MAINTAINERS

Quoted 1 line
MAINTAINERS +1 -0
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CSC_COEFF3,

reg.csc_coeff[3]);

Quoted 11 lines

MAINTAINERS

Quoted 7 lines
MAINTAINERS +7 -0
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_ENC_PIC);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return -ETIMEDOUT;
+	}

I not sure how heavy register read() write(), consider use readl(writel)_relex(), if need write/read many registers(). Only last one need writel() before trigger DMA.

This should be worth it, especially in the multi-instance case. I'll do this in v8 if I see no regression.

Quoted 15 lines
Quoted 11 lines
MAINTAINERS +10 -0
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS)) {
+		*fail_res = vpu_read_reg(inst->dev, W6_RET_FAIL_REASON);
+		wave6_print_reg_err(inst->dev, *fail_res);
+		return -EIO;
+	}
+
+	return 0;
+}
+

...

Review

Quoted 2 lines
MAINTAINERS +1 -0
+#endif /* __WAVE6_HW_H__ */
(quoted diff) +0 -0

b/drivers/media/platform/chips-media/wave6/wave6-regdefine.h

Quoted 13 lines

drivers/media/platform/chips-media/wave6/wave6-regdefine.h line 6

Quoted 11 lines

new file mode 100644
index 000000000000..1d495145bbbd

drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +6 -0
@@ -0,0 +1,649 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
+/*
+ * Wave6 series multi-standard codec IP - wave6 register definitions
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */

2026

Same as above.

Quoted 15 lines

...

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +13 -0
+
+#define W6_MAX_PIC_STRIDE		(4096U * 4)
+#define W6_PIC_STRIDE_ALIGNMENT		32
+#define W6_FBC_BUF_ALIGNMENT		32
+#define W6_DEC_BUF_ALIGNMENT		32
+#define W6_DEF_DEC_PIC_WIDTH		720U
+#define W6_DEF_DEC_PIC_HEIGHT		480U
+#define W6_MIN_DEC_PIC_WIDTH		64U
+#define W6_MIN_DEC_PIC_HEIGHT		64U
+#define W6_MAX_DEC_PIC_WIDTH		4096U
+#define W6_MAX_DEC_PIC_HEIGHT		4096U
+#define W6_DEC_PIC_SIZE_STEP		1
+
Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-regdefine.h

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +6 -0
+#define W6_DEF_ENC_PIC_WIDTH		416U
+#define W6_DEF_ENC_PIC_HEIGHT		240U
+#define W6_MIN_ENC_PIC_WIDTH		256U
+#define W6_MIN_ENC_PIC_HEIGHT		128U
+#define W6_MAX_ENC_PIC_WIDTH		4096U
+#define W6_MAX_ENC_PIC_HEIGHT		4096U

needn't U

OK.

Quoted 32 lines
Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +11 -0
+#define W6_ENC_PIC_SIZE_STEP		8
+#define W6_ENC_CROP_X_POS_STEP		32
+#define W6_ENC_CROP_Y_POS_STEP		2
+#define W6_ENC_CROP_STEP		2
+
+#define W6_VPU_POLL_DELAY_US		10
+#define W6_VPU_POLL_TIMEOUT		300000
+#define W6_BOOT_WAIT_TIMEOUT		10000
+#define W6_VPU_TIMEOUT			6000
+#define W6_VPU_TIMEOUT_CYCLE_COUNT	(8000000 * 4 * 4)
+

...

Quoted 19 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +19 -0
+#define __WAVE6_VPUERROR_H__
+
+/* WAVE6 COMMON SYSTEM ERROR (FAIL_REASON) */
+#define WAVE6_SYSERR_QUEUEING_FAIL			0x00000001
+#define WAVE6_SYSERR_DECODER_FUSE			0x00000002
+#define WAVE6_SYSERR_INSTRUCTION_ACCESS_VIOLATION	0x00000004
+#define WAVE6_SYSERR_PRIVILEGE_VIOLATION		0x00000008
+#define WAVE6_SYSERR_DATA_ADDR_ALIGNMENT		0x00000010
+#define WAVE6_SYSERR_DATA_ACCESS_VIOLATION		0x00000020
+#define WAVE6_SYSERR_ACCESS_VIOLATION_HW		0x00000040
+#define WAVE6_SYSERR_INSTRUCTION_ADDR_ALIGNMENT		0x00000080
+#define WAVE6_SYSERR_UNKNOWN				0x00000100
+#define WAVE6_SYSERR_BUS_ERROR				0x00000200
+#define WAVE6_SYSERR_DOUBLE_FAULT			0x00000400
+#define WAVE6_SYSERR_RESULT_NOT_READY			0x00000800
+#define WAVE6_SYSERR_VPU_STILL_RUNNING			0x00001000
+#define WAVE6_SYSERR_UNKNOWN_CMD			0x00002000
+#define WAVE6_SYSERR_UNKNOWN_CODEC_STD			0x00004000
+#define WAVE6_SYSERR_UNKNOWN_QUERY_OPTION		0x00008000
Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-regdefine.h

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +5 -0
+#define WAVE6_SYSERR_WATCHDOG_TIMEOUT			0x00020000
+#define WAVE6_SYSERR_NOT_SUPPORT			0x00100000
+#define WAVE6_SYSERR_TEMP_SEC_BUF_OVERFLOW		0x00200000
+#define WAVE6_SYSERR_NOT_SUPPORT_PROFILE		0x00400000
+#define WAVE6_SYSERR_TIMEOUT_CODEC_FW			0x40000000

Suppose you should get check_patch warning, to prefer use BIT(n) for this defination.

OK. I'll use BIT(n) where the value is a single bit.

Thanks.
Nas.

Quoted 2 lines

Frank

#12 lore raw

Re: [PATCH v7 3/9] media: chips-media: wave6: Add Wave6 VPU interface

In reply to Nas Chung
Quoted 11 lines · Nas Chung

Add an interface layer to manage hardware register configuration and communication with the Chips&Media Wave6 video codec IP.

The interface provides low-level helper functions used by the Wave6 core driver to implement video encoding and decoding operations. It handles command submission to the firmware via MMIO registers, and waits for a response by polling the firmware busy flag.

Quoted 10 lines
---
 MAINTAINERS                                   |    1 +
 .../platform/chips-media/wave6/wave6-hw.c     | 2030 +++++++++++++++++
 .../platform/chips-media/wave6/wave6-hw.h     |   56 +
 .../chips-media/wave6/wave6-regdefine.h       |  649 ++++++
 .../platform/chips-media/wave6/wave6-vdi.h    |   92 +
 .../platform/chips-media/wave6/wave6-vpuapi.c |  336 +++
 .../platform/chips-media/wave6/wave6-vpuapi.h | 1007 ++++++++
 .../chips-media/wave6/wave6-vpuconfig.h       |   72 +
 .../chips-media/wave6/wave6-vpuerror.h        |  262 +++

You need make this patch built-able. Add Makefile and Kconfig part.

Quoted 30 lines
 9 files changed, 4505 insertions(+)
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-hw.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-hw.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-regdefine.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vdi.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpuapi.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpuapi.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpuerror.h
MAINTAINERS +1 -0
index 7387a11facbe..e29018c2546b 100644
@@ -29239,6 +29239,7 @@ M:	Jackson Lee <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml
+F:	drivers/media/platform/chips-media/wave6/
 
 WHISKEYCOVE PMIC GPIO DRIVER
 M:	Kuppuswamy Sathyanarayanan <[email protected]>
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
new file mode 100644
index 000000000000..a91b582af023
@@ -0,0 +1,2030 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+/*

drivers/media/platform/chips-media/wave6/wave6-hw.c line 12

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+ * Wave6 series multi-standard codec IP - wave6 backend interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#include <linux/iopoll.h>
+#include "wave6-vpu-core.h"
+#include "wave6-hw.h"
+#include "wave6-regdefine.h"
+#include "wave6-trace.h"

Add trace support later, add with patch6.

drivers/media/platform/chips-media/wave6/wave6-hw.c line 17

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +5 -0
+
+void wave6_vpu_writel(struct vpu_core_device *core, u32 addr, u32 data)
+{
+	wave6_vdi_writel(core->reg_base, addr, data);
+	trace_wave6_vpu_writel(core->dev, addr, data);

Add trace support at later patches.

Quoted 9 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +9 -0
+}
+
+u32 wave6_vpu_readl(struct vpu_core_device *core, u32 addr)
+{
+	u32 data;
+
+	data = wave6_vdi_readl(core->reg_base, addr);
+	trace_wave6_vpu_readl(core->dev, addr, data);
+

drivers/media/platform/chips-media/wave6/wave6-hw.c line 36

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	return data;
+}
+
+static void wave6_print_reg_err(struct vpu_core_device *core, u32 fail_reason)
+{
+	void *caller = __builtin_return_address(0);
+	struct device *dev = core->dev;
+
+	switch (fail_reason) {
+	case WAVE6_SYSERR_QUEUEING_FAIL:

you define it as bit mask, is it possible more than 1 bits to set?

Quoted 25 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +25 -0
+		dev_dbg(dev, "%pS: queueing failure 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_RESULT_NOT_READY:
+		dev_err(dev, "%pS: result not ready 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_ACCESS_VIOLATION_HW:
+		dev_err(dev, "%pS: access violation 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_WATCHDOG_TIMEOUT:
+		dev_err(dev, "%pS: watchdog timeout 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_BUS_ERROR:
+		dev_err(dev, "%pS: bus error 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_DOUBLE_FAULT:
+		dev_err(dev, "%pS: double fault 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_VPU_STILL_RUNNING:
+		dev_err(dev, "%pS: still running 0x%x\n", caller, fail_reason);
+		break;
+	default:
+		dev_err(dev, "%pS: failure: 0x%x\n", caller, fail_reason);
+		break;
+	}
+}

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+
+static void wave6_dec_set_display_buffer(struct vpu_instance *inst, struct frame_buffer fb)
+
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int index;
+
+	for (index = 0; index < WAVE6_MAX_FBS; index++) {
+		if (!p_dec_info->disp_buf[index].buf_y) {
+			p_dec_info->disp_buf[index] = fb;
+			p_dec_info->disp_buf[index].index = index;

index is the same as nature array index p_dec_info->disp_buf, why need it.

drivers/media/platform/chips-media/wave6/wave6-hw.c line 72

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+			break;

where update .buf_y?

Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +8 -0
+		}
+	}
+}
+
+static struct frame_buffer wave6_dec_get_display_buffer(struct vpu_instance *inst,
+							dma_addr_t addr)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;

drivers/media/platform/chips-media/wave6/wave6-hw.c line 90

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	int i;
+	struct frame_buffer fb;
+
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {
+		if (p_dec_info->disp_buf[i].buf_y == addr)
+			return p_dec_info->disp_buf[i];
+	}
+
+	memset(&fb, 0, sizeof(struct frame_buffer));
+	fb.index = -1;

suppose this error case, did caller tolerent fb.index -1, I have not see check this value.

drivers/media/platform/chips-media/wave6/wave6-hw.c line 103

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +13 -0
+
+	return fb;
+}
+
+static void wave6_dec_remove_display_buffer(struct vpu_instance *inst,
+					    dma_addr_t addr)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int i;
+
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {
+		if (p_dec_info->disp_buf[i].buf_y == addr) {
+			memset(&p_dec_info->disp_buf[i], 0, sizeof(struct frame_buffer));

you suppose need set p_dec_info->disp_buf[i].buf_y to 0?

Quoted 100 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +100 -0
+			break;
+		}
+	}
+}
+
+static enum chroma_format_idc get_chroma_format_idc(enum frame_buffer_format fmt)
+{
+	switch (fmt) {
+	case FORMAT_420:
+	case FORMAT_420_P10_16BIT_MSB:
+	case FORMAT_420_P10_16BIT_LSB:
+	case FORMAT_420_P10_32BIT_MSB:
+	case FORMAT_420_P10_32BIT_LSB:
+		return C_FMT_IDC_YUV420;
+	case FORMAT_422:
+	case FORMAT_422_P10_16BIT_MSB:
+	case FORMAT_422_P10_16BIT_LSB:
+	case FORMAT_422_P10_32BIT_MSB:
+	case FORMAT_422_P10_32BIT_LSB:
+	case FORMAT_YUYV:
+	case FORMAT_YVYU:
+	case FORMAT_UYVY:
+	case FORMAT_VYUY:
+	case FORMAT_YUYV_P10_16BIT_MSB:
+	case FORMAT_YVYU_P10_16BIT_MSB:
+	case FORMAT_UYVY_P10_16BIT_MSB:
+	case FORMAT_VYUY_P10_16BIT_MSB:
+	case FORMAT_YUYV_P10_16BIT_LSB:
+	case FORMAT_YVYU_P10_16BIT_LSB:
+	case FORMAT_UYVY_P10_16BIT_LSB:
+	case FORMAT_VYUY_P10_16BIT_LSB:
+	case FORMAT_YUYV_P10_32BIT_MSB:
+	case FORMAT_YVYU_P10_32BIT_MSB:
+	case FORMAT_UYVY_P10_32BIT_MSB:
+	case FORMAT_VYUY_P10_32BIT_MSB:
+	case FORMAT_YUYV_P10_32BIT_LSB:
+	case FORMAT_YVYU_P10_32BIT_LSB:
+	case FORMAT_UYVY_P10_32BIT_LSB:
+	case FORMAT_VYUY_P10_32BIT_LSB:
+		return C_FMT_IDC_YUV422;
+	case FORMAT_444:
+	case FORMAT_444_P10_16BIT_MSB:
+	case FORMAT_444_P10_16BIT_LSB:
+	case FORMAT_444_P10_32BIT_MSB:
+	case FORMAT_444_P10_32BIT_LSB:
+		return C_FMT_IDC_YUV444;
+	case FORMAT_400:
+	case FORMAT_400_P10_16BIT_MSB:
+	case FORMAT_400_P10_16BIT_LSB:
+	case FORMAT_400_P10_32BIT_MSB:
+	case FORMAT_400_P10_32BIT_LSB:
+	case FORMAT_YUV444_24BIT:
+		return C_FMT_IDC_YUV400;
+	case FORMAT_RGB_24BIT_PACKED:
+	case FORMAT_YUV444_24BIT_PACKED:
+	case FORMAT_RGB_32BIT_PACKED:
+	case FORMAT_RGB_P10_32BIT_PACKED:
+	case FORMAT_YUV444_32BIT_PACKED:
+	case FORMAT_YUV444_P10_32BIT_PACKED:
+		return C_FMT_IDC_RGB;
+	default:
+		return C_FMT_IDC_YUV400;
+	}
+}
+
+static int wave6_wait_vpu_busy(struct vpu_core_device *core, unsigned int addr)
+{
+	u32 data;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	return read_poll_timeout(vpu_read_reg, data, !data,
+				 W6_VPU_POLL_DELAY_US, W6_VPU_POLL_TIMEOUT,
+				 false, core, addr);
+}
+
+void wave6_vpu_enable_interrupt(struct vpu_core_device *core)
+{
+	u32 data;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	data = BIT(W6_INT_BIT_ENC_SET_PARAM);
+	data |= BIT(W6_INT_BIT_ENC_PIC);
+	data |= BIT(W6_INT_BIT_INIT_SEQ);
+	data |= BIT(W6_INT_BIT_DEC_PIC);
+	data |= BIT(W6_INT_BIT_BSBUF_ERROR);
+	data |= BIT(W6_INT_BIT_REQ_WORK_BUF);
+	vpu_write_reg(core, W6_VPU_VINT_ENABLE, data);
+}
+
+bool wave6_vpu_is_init(struct vpu_core_device *core)
+{
+	lockdep_assert_held(&core->hw_lock);
+
+	return vpu_read_reg(core, W6_VPU_VCPU_CUR_PC) != 0;
+}
+
+static u32 wave6_vpu_get_product_id(struct vpu_core_device *core)
+{

drivers/media/platform/chips-media/wave6/wave6-hw.c line 213

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	u32 product_id = PRODUCT_ID_NONE;
+	u32 val;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	val = vpu_read_reg(core, W6_VPU_RET_PRODUCT_CODE);
+
+	switch (val) {
+	case WAVE617_CODE:
+		product_id = PRODUCT_ID_617;

return PRODUCT_ID_617;

drivers/media/platform/chips-media/wave6/wave6-hw.c line 217

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +4 -0
+		break;
+	case WAVE627_CODE:
+		product_id = PRODUCT_ID_627;
+		break;

return PRODUCT_ID_627;

drivers/media/platform/chips-media/wave6/wave6-hw.c line 223

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+	case WAVE633_CODE:
+	case WAVE637_CODE:
+	case WAVE663_CODE:
+	case WAVE677_CODE:
+		product_id = PRODUCT_ID_637;
+		break;

return PRODUCT_ID_637;

drivers/media/platform/chips-media/wave6/wave6-hw.c line 229

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+	default:
+		dev_err(core->dev, "Invalid product (%x)\n", val);
+		break;
+	}
+
+	return product_id;

return PRODUCT_ID_NONE; needn't variable product_id

drivers/media/platform/chips-media/wave6/wave6-hw.c line 241

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +12 -0
+}
+
+static void wave6_send_command(struct vpu_core_device *core, u32 id, u32 std, u32 cmd)
+{
+	u32 reg_val;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	if (cmd == W6_CMD_CREATE_INSTANCE)
+		reg_val = INSTANCE_INFO_CODEC_STD(std);
+	else
+		reg_val = INSTANCE_INFO_CODEC_STD(std) | INSTANCE_INFO_ID(id);
	reg_val = INSTANCE_INFO_CODEC_STD(std);
	if (cmd != W6_CMD_CREATE_INSTANCE)
		reg_val |= INSTANCE_INFO_ID(id);
Quoted 19 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +19 -0
+	vpu_write_reg(core, W6_CMD_INSTANCE_INFO, reg_val);
+
+	vpu_write_reg(core, W6_VPU_BUSY_STATUS, BUSY_STATUS_SET);
+	vpu_write_reg(core, W6_COMMAND, cmd);
+	vpu_write_reg(core, W6_VPU_HOST_INT_REQ, HOST_INT_REQ_ON);
+
+	trace_wave6_vpu_send_command(core, id, std, cmd);
+}
+
+static int wave6_send_query(struct vpu_core_device *core, u32 id, u32 std,
+			    enum wave6_query_option query_opt)
+{
+	int ret;
+	u32 reg_val;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	vpu_write_reg(core, W6_QUERY_OPTION, query_opt);
+	wave6_send_command(core, id, std, W6_CMD_QUERY);

drivers/media/platform/chips-media/wave6/wave6-hw.c line 270

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+
+	ret = wave6_wait_vpu_busy(core, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(core->dev, "query timed out opt=0x%x\n", query_opt);
+		return ret;
+	}
+
+	if (!vpu_read_reg(core, W6_RET_SUCCESS)) {
+		reg_val = vpu_read_reg(core, W6_RET_FAIL_REASON);
+		wave6_print_reg_err(core, reg_val);

You can add such debug helper function later to reduce each patch's change.

Quoted 195 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +195 -0
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int wave6_vpu_get_version(struct vpu_core_device *core)
+{
+	struct vpu_attr *attr = &core->attr;
+	int ret;
+	u32 std_def1, conf_feature;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	ret = wave6_send_query(core, 0, 0, W6_QUERY_OPT_GET_VPU_INFO);
+	if (ret)
+		return ret;
+
+	attr->product_id = wave6_vpu_get_product_id(core);
+	attr->product_code = vpu_read_reg(core, W6_VPU_RET_PRODUCT_CODE);
+	attr->product_version = vpu_read_reg(core, W6_RET_PRODUCT_VERSION);
+	attr->fw_version = vpu_read_reg(core, W6_RET_FW_API_VERSION);
+	attr->fw_revision = vpu_read_reg(core, W6_RET_FW_VERSION);
+	attr->hw_version = vpu_read_reg(core, W6_RET_CONF_HW_VERSION);
+	std_def1 = vpu_read_reg(core, W6_RET_STD_DEF1);
+	conf_feature = vpu_read_reg(core, W6_RET_CONF_FEATURE);
+
+	attr->support_decoders = 0;
+	attr->support_encoders = 0;
+	attr->support_decoders |= STD_DEF1_HEVC_DEC(std_def1) << W_HEVC_DEC;
+	attr->support_hevc10bit_dec = CONF_FEATURE_HEVC10BIT_DEC(conf_feature);
+	attr->support_decoders |= STD_DEF1_AVC_DEC(std_def1) << W_AVC_DEC;
+	attr->support_avc10bit_dec = CONF_FEATURE_AVC10BIT_DEC(conf_feature);
+	attr->support_encoders |= STD_DEF1_HEVC_ENC(std_def1) << W_HEVC_ENC;
+	attr->support_hevc10bit_enc = CONF_FEATURE_HEVC10BIT_ENC(conf_feature);
+	attr->support_encoders |= STD_DEF1_AVC_ENC(std_def1) << W_AVC_ENC;
+	attr->support_avc10bit_enc = CONF_FEATURE_AVC10BIT_ENC(conf_feature);
+
+	return 0;
+}
+
+int wave6_vpu_build_up_dec_param(struct vpu_instance *inst,
+				 struct dec_open_param *param)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	u32 reg_val;
+	int ret;
+
+	lockdep_assert_held(&inst->dev->hw_lock);
+
+	p_dec_info->sec_axi.use_dec_ip = true;
+	p_dec_info->sec_axi.use_dec_lf_row = true;
+	switch (inst->std) {
+	case W_HEVC_DEC:
+		p_dec_info->seq_change_mask = SEQ_CHANGE_ENABLE_ALL_HEVC;
+		break;
+	case W_AVC_DEC:
+		p_dec_info->seq_change_mask = SEQ_CHANGE_ENABLE_ALL_AVC;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_TEMP_BASE, param->inst_buffer.temp_base);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_TEMP_SIZE, param->inst_buffer.temp_size);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_BS_PARAM, VPU_STREAM_ENDIAN);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_ADDR_EXT, param->ext_addr_vcpu);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_DISP_MODE, param->disp_mode);
+
+	reg_val = CREATE_INST_CORE_INFO_CQ_DEPTH(COMMAND_QUEUE_DEPTH) |
+		  CREATE_INST_CORE_INFO_CORE_IDC(SINGLE_CORE_IDC) |
+		  CREATE_INST_CORE_INFO_CORE_NUM(SINGLE_CORE);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_CORE_INFO, reg_val);
+
+	reg_val = CREATE_INST_PRIORITY_SECURITY_FLAG(param->is_secure_inst) |
+		  CREATE_INST_PRIORITY_VALUE(param->inst_priority);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_PRIORITY, reg_val);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_TIMEOUT_CYCLE_COUNT,
+		      W6_VPU_TIMEOUT_CYCLE_COUNT);
+
+	wave6_send_command(inst->dev, 0, inst->std, W6_CMD_CREATE_INSTANCE);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return ret;
+	}
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS)) {
+		u32 reason_code = vpu_read_reg(inst->dev, W6_RET_FAIL_REASON);
+
+		wave6_print_reg_err(inst->dev, reason_code);
+		return -EIO;
+	}
+
+	inst->id = vpu_read_reg(inst->dev, W6_RET_INSTANCE_ID);
+
+	return 0;
+}
+
+int wave6_vpu_dec_issue_seq_init(struct vpu_instance *inst)
+{
+	struct dec_info *p_dec_info;
+	u32 reg_val;
+	int ret;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_dec_info = &inst->codec_info->dec_info;
+
+	vpu_write_reg(inst->dev, W6_CMD_DEC_INIT_SEQ_BS_RD_PTR, p_dec_info->stream_rd_ptr);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_INIT_SEQ_BS_WR_PTR, p_dec_info->stream_wr_ptr);
+
+	reg_val = DEC_PIC_BS_OPTION_STREAM_END(p_dec_info->stream_end) |
+		  DEC_PIC_BS_OPTION_EXPLICIT_END_ON;
+	vpu_write_reg(inst->dev, W6_CMD_DEC_INIT_SEQ_BS_OPTION, reg_val);
+	reg_val = INIT_SEQ_OPTION_MODE(W6_INIT_SEQ_OPT_NORMAL);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_INIT_SEQ_OPTION, reg_val);
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_INIT_SEQ);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return ret;
+	}
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS)) {
+		u32 reason_code = vpu_read_reg(inst->dev, W6_RET_FAIL_REASON);
+
+		wave6_print_reg_err(inst->dev, reason_code);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static void wave6_get_dec_seq_result(struct vpu_instance *inst, struct dec_seq_info *info)
+{
+	u32 reg_val;
+	u32 profile, profile_comp;
+
+	lockdep_assert_held(&inst->dev->hw_lock);
+
+	info->rd_ptr = wave6_vpu_dec_get_rd_ptr(inst);
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_PIC_SIZE);
+	info->pic_width = DEC_PIC_SIZE_WIDTH(reg_val);
+	info->pic_height = DEC_PIC_SIZE_HEIGHT(reg_val);
+
+	info->min_frame_buffer_count = vpu_read_reg(inst->dev, W6_RET_DEC_NUM_REQUIRED_FBC_FB);
+	info->frame_buf_delay = vpu_read_reg(inst->dev, W6_RET_DEC_NUM_REORDER_DELAY);
+	info->req_mv_buffer_count = vpu_read_reg(inst->dev, W6_RET_DEC_NUM_REQUIRED_COL_BUF);
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_CROP_TOP_BOTTOM);
+	info->pic_crop_rect.top = DEC_CROP_TOP(reg_val);
+	info->pic_crop_rect.bottom = info->pic_height - DEC_CROP_BOTTOM(reg_val);
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_CROP_LEFT_RIGHT);
+	info->pic_crop_rect.left = DEC_CROP_LEFT(reg_val);
+	info->pic_crop_rect.right = info->pic_width - DEC_CROP_RIGHT(reg_val);
+
+	info->f_rate_numerator = vpu_read_reg(inst->dev, W6_RET_DEC_FRAME_RATE_NR);
+	info->f_rate_denominator = vpu_read_reg(inst->dev, W6_RET_DEC_FRAME_RATE_DR);
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_COLOR_SAMPLE);
+	info->luma_bit_depth = DEC_COLOR_SAMPLE_L_BIT_DEPTH(reg_val);
+	info->chroma_bit_depth = DEC_COLOR_SAMPLE_C_BIT_DEPTH(reg_val);
+	info->c_fmt_idc = DEC_COLOR_SAMPLE_C_FMT_IDC(reg_val);
+	info->aspect_rate_info = DEC_COLOR_SAMPLE_ASPECT_RATIO_IDC(reg_val);
+	info->is_ext_sar = info->aspect_rate_info == H264_VUI_SAR_IDC_EXTENDED;
+	if (info->is_ext_sar)
+		info->aspect_rate_info = vpu_read_reg(inst->dev, W6_RET_DEC_ASPECT_RATIO);
+	info->bitrate = vpu_read_reg(inst->dev, W6_RET_DEC_BIT_RATE);
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_SEQ_PARAM);
+	info->level = DEC_SEQ_PARAM_LEVEL(reg_val);
+	info->tier = DEC_SEQ_PARAM_TIER(reg_val);
+
+	profile = DEC_SEQ_PARAM_PROFILE(reg_val);
+	profile_comp = DEC_SEQ_PARAM_PROFILE_COMPATIBILITY(reg_val);
+	if (inst->std == W_HEVC_DEC) {
+		info->profile = profile;
+		if (!info->profile) {
+			if ((profile_comp & PROFILE_COMPATIBILITY_MAIN) &&
+			    (profile_comp & PROFILE_COMPATIBILITY_MAIN10))
+				info->profile = HEVC_PROFILE_MAIN;
+			else if (profile_comp & PROFILE_COMPATIBILITY_MAIN10)
+				info->profile = HEVC_PROFILE_MAIN10;
+			else if (profile_comp & PROFILE_COMPATIBILITY_STILL_PICTURE)
+				info->profile = HEVC_PROFILE_STILLPICTURE;
+			else
+				info->profile = HEVC_PROFILE_MAIN;
+		}
+	} else if (inst->std == W_AVC_DEC) {
+		if (profile == PROFILE_H264_BP)
+			info->profile = H264_PROFILE_BP;

drivers/media/platform/chips-media/wave6/wave6-hw.c line 475

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+		else if (profile == PROFILE_H264_HP)
+			info->profile = H264_PROFILE_HP;
+		else if (profile == PROFILE_H264_MP)
+			info->profile = H264_PROFILE_MP;
+		else if (profile == PROFILE_H264_HIGH10)
+			info->profile = H264_PROFILE_HIGH10;
+		else if (profile == PROFILE_H264_EXTENDED)
+			info->profile = H264_PROFILE_EXTENDED;
+		else
+			info->profile = H264_PROFILE_BP;

use small helper function for such convertion, you can use switch case for this maping or static const array.

Quoted 55 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +55 -0
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_COLOR_CONFIG);
+	if (reg_val) {
+		info->color.video_signal_type_present = true;
+		info->color.color_description_present = DEC_COLOR_CONFIG_COLOR_PRESENT(reg_val);
+		info->color.color_primaries = DEC_COLOR_CONFIG_COLOR_PRIMARIES(reg_val);
+		info->color.transfer_characteristics = DEC_COLOR_CONFIG_TRANS_CHAR(reg_val);
+		info->color.matrix_coefficients = DEC_COLOR_CONFIG_MATRIX_COEFF(reg_val);
+		info->color.color_range = DEC_COLOR_CONFIG_COLOR_RANGE(reg_val);
+	} else {
+		info->color.video_signal_type_present = false;
+	}
+}
+
+int wave6_vpu_dec_get_seq_info(struct vpu_instance *inst, struct dec_seq_info *info)
+{
+	int ret;
+
+	lockdep_assert_held(&inst->dev->hw_lock);
+
+	ret = wave6_send_query(inst->dev, inst->id, inst->std, W6_QUERY_OPT_GET_RESULT);
+	if (ret)
+		return ret;
+
+	if (vpu_read_reg(inst->dev, W6_RET_DEC_DECODING_SUCCESS) != 1) {
+		info->err_reason = vpu_read_reg(inst->dev, W6_RET_DEC_ERR_INFO);
+		ret = -EIO;
+	} else {
+		info->warn_info = vpu_read_reg(inst->dev, W6_RET_DEC_WARN_INFO);
+	}
+
+	wave6_get_dec_seq_result(inst, info);
+
+	return ret;
+}
+
+int wave6_vpu_dec_register_frame_buffer(struct vpu_instance *inst)
+{
+	struct dec_info *p_dec_info;
+	struct frame_buffer *fb_arr;
+	size_t fbc_remain, mv_remain, fbc_idx = 0, mv_idx = 0;
+	size_t i, k, group_num, fbc_count, mv_count;
+	dma_addr_t fbc_cr_tbl_addr;
+	u32 reg_val;
+	int ret;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_dec_info = &inst->codec_info->dec_info;
+
+	fb_arr = &inst->frame_buf[0];
+	fbc_count = p_dec_info->seq_info.min_frame_buffer_count;
+	mv_count = p_dec_info->seq_info.req_mv_buffer_count;
+

drivers/media/platform/chips-media/wave6/wave6-hw.c line 540

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	for (i = 0; i < fbc_count; i++) {
+		if (!inst->aux_vbuf[AUX_BUF_FBC_Y_TBL][i].daddr)
+			return -EINVAL;
+		if (!inst->aux_vbuf[AUX_BUF_FBC_C_TBL][i].daddr)
+			return -EINVAL;
+	}
+	for (i = 0; i < mv_count; i++) {
+		if (!inst->aux_vbuf[AUX_BUF_MV_COL][i].daddr)
+			return -EINVAL;
+	}

use small helper function, like check_aux_vbuf().

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +5 -0
+
+	reg_val = SET_FB_PIC_SIZE_WIDTH(p_dec_info->seq_info.pic_width) |
+		  SET_FB_PIC_SIZE_HEIGHT(p_dec_info->seq_info.pic_height);
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_PIC_SIZE, reg_val);
+	reg_val = SET_FB_PIC_INFO_C_FMT_IDC(p_dec_info->seq_info.c_fmt_idc) |

drivers/media/platform/chips-media/wave6/wave6-hw.c line 555

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+		  SET_FB_PIC_INFO_L_BIT_DEPTH(p_dec_info->seq_info.luma_bit_depth) |
+		  SET_FB_PIC_INFO_C_BIT_DEPTH(p_dec_info->seq_info.chroma_bit_depth);
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_PIC_INFO, reg_val);
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_DEFAULT_CDF, 0);
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_SEGMAP, 0);
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_MV_COL_PRE_ENT, 0);
+
+	fbc_remain = fbc_count;
+	mv_remain = mv_count;
+	group_num = DIV_ROUND_UP(max(fbc_count, mv_count), 16);

Add empty line here.

Quoted 28 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +28 -0
+	for (i = 0; i < group_num; i++) {
+		bool first_group = i == 0;
+		bool last_group = i == group_num - 1;
+		u32 set_fbc_num = (fbc_remain >= 16) ? 16 : fbc_remain;
+		u32 set_mv_num = (mv_remain >= 16) ? 16 : mv_remain;
+		u32 fbc_start_no = i * 16;
+		u32 fbc_end_no = fbc_start_no + set_fbc_num - 1;
+		u32 mv_start_no = i * 16;
+		u32 mv_end_no = mv_start_no + set_mv_num - 1;
+
+		reg_val = SET_FB_OPTION_ENDIAN(VDI_128BIT_BIG_ENDIAN);
+		if (first_group)
+			reg_val |= SET_FB_OPTION_START;
+		if (last_group)
+			reg_val |= SET_FB_OPTION_END;
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_OPTION, reg_val);
+
+		reg_val = SET_FB_NUM_FBC_START_IDX(fbc_start_no) |
+			  SET_FB_NUM_FBC_END_IDX(fbc_end_no) |
+			  SET_FB_NUM_MV_COL_START_IDX(mv_start_no) |
+			  SET_FB_NUM_MV_COL_END_IDX(mv_end_no);
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_NUM, reg_val);
+
+		for (k = 0; k < set_fbc_num; k++) {
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_Y(k),
+				      fb_arr[fbc_idx].buf_y);
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_C(k),
+				      fb_arr[fbc_idx].buf_cb);

drivers/media/platform/chips-media/wave6/wave6-hw.c line 593

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_CR(k),
+				      fb_arr[fbc_idx].buf_cr);
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_Y_OFFSET(k),
+				      inst->aux_vbuf[AUX_BUF_FBC_Y_TBL][fbc_idx].daddr);
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_C_OFFSET(k),
+				      inst->aux_vbuf[AUX_BUF_FBC_C_TBL][fbc_idx].daddr);
+			fbc_cr_tbl_addr = inst->aux_vbuf[AUX_BUF_FBC_C_TBL][fbc_idx].daddr +
+					  (inst->aux_vbuf[AUX_BUF_FBC_C_TBL][fbc_idx].size >> 1);
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_CR_OFFSET(k),
+				      fbc_cr_tbl_addr);

I am not sure how big set_fbc_num, I suggest change to use writel_relax().

because you wave6_wait_vpu_busy() to check status, extra memory barry in writel() is not neccessary.

Quoted 65 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +65 -0
+			fbc_idx++;
+		}
+		fbc_remain -= k;
+
+		for (k = 0; k < set_mv_num; k++) {
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_MV_COL(k),
+				      inst->aux_vbuf[AUX_BUF_MV_COL][mv_idx].daddr);
+			mv_idx++;
+		}
+		mv_remain -= k;
+
+		wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_SET_FB);
+		ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+		if (ret) {
+			dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+			return ret;
+		}
+
+		if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS))
+			return -EIO;
+	}
+
+	return 0;
+}
+
+int wave6_vpu_dec_register_display_buffer(struct vpu_instance *inst, struct frame_buffer fb)
+{
+	int ret;
+	struct dec_info *p_dec_info;
+	u32 reg_val;
+	u32 c_fmt_idc, out_fmt, out_mode;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_dec_info = &inst->codec_info->dec_info;
+
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_SCL_PARAM,
+		      inst->scaler_info.enable);
+	reg_val = SET_DISP_SCL_PIC_SIZE_WIDTH(inst->scaler_info.width) |
+		  SET_DISP_SCL_PIC_SIZE_HEIGHT(inst->scaler_info.height);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_SCL_PIC_SIZE, reg_val);
+	reg_val = SET_DISP_PIC_SIZE_WIDTH(p_dec_info->seq_info.pic_width) |
+		  SET_DISP_PIC_SIZE_HEIGHT(p_dec_info->seq_info.pic_height);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_PIC_SIZE, reg_val);
+
+	c_fmt_idc = get_chroma_format_idc(p_dec_info->wtl_format);
+	switch (p_dec_info->wtl_format) {
+	case FORMAT_420_P10_16BIT_MSB:
+	case FORMAT_422_P10_16BIT_MSB:
+	case FORMAT_444_P10_16BIT_MSB:
+	case FORMAT_400_P10_16BIT_MSB:
+		out_mode = (WTL_RIGHT_JUSTIFIED << 2) | WTL_PIXEL_16BIT;
+		break;
+	case FORMAT_420_P10_16BIT_LSB:
+	case FORMAT_422_P10_16BIT_LSB:
+	case FORMAT_444_P10_16BIT_LSB:
+	case FORMAT_400_P10_16BIT_LSB:
+		out_mode = (WTL_LEFT_JUSTIFIED << 2) | WTL_PIXEL_16BIT;
+		break;
+	case FORMAT_420_P10_32BIT_MSB:
+	case FORMAT_422_P10_32BIT_MSB:
+	case FORMAT_444_P10_32BIT_MSB:
+	case FORMAT_400_P10_32BIT_MSB:
+		out_mode = (WTL_RIGHT_JUSTIFIED << 2) | WTL_PIXEL_32BIT;
+		break;

drivers/media/platform/chips-media/wave6/wave6-hw.c line 668

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	case FORMAT_420_P10_32BIT_LSB:
+	case FORMAT_422_P10_32BIT_LSB:
+	case FORMAT_444_P10_32BIT_LSB:
+	case FORMAT_400_P10_32BIT_LSB:
+		out_mode = (WTL_LEFT_JUSTIFIED << 2) | WTL_PIXEL_32BIT;
+		break;
+	default:
+		out_mode = (WTL_RIGHT_JUSTIFIED << 2) | WTL_PIXEL_8BIT;
+		break;
+	}

move such long convert to small helper function

Quoted 473 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +473 -0
+	out_fmt = (inst->nv21 << 1) | inst->cbcr_interleave;
+
+	reg_val = SET_DISP_COMMON_PIC_INFO_BWB_ON |
+		  SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(c_fmt_idc) |
+		  SET_DISP_COMMON_PIC_INFO_PIXEL_ORDER(PIXEL_ORDER_INCREASING) |
+		  SET_DISP_COMMON_PIC_INFO_OUT_MODE(out_mode) |
+		  SET_DISP_COMMON_PIC_INFO_OUT_FMT(out_fmt) |
+		  SET_DISP_COMMON_PIC_INFO_STRIDE(fb.stride);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_COMMON_PIC_INFO, reg_val);
+	reg_val = SET_DISP_OPTION_ENDIAN(VDI_128BIT_BIG_ENDIAN);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_OPTION, reg_val);
+	reg_val = SET_DISP_PIC_INFO_L_BIT_DEPTH(fb.luma_bit_depth) |
+		  SET_DISP_PIC_INFO_C_BIT_DEPTH(fb.chroma_bit_depth) |
+		  SET_DISP_PIC_INFO_C_FMT_IDC(fb.c_fmt_idc);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_PIC_INFO, reg_val);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_Y_BASE, fb.buf_y);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_CB_BASE, fb.buf_cb);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_CR_BASE, fb.buf_cr);
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_DEC_SET_DISP);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return ret;
+	}
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS))
+		return -EIO;
+
+	wave6_dec_set_display_buffer(inst, fb);
+
+	return 0;
+}
+
+int wave6_vpu_dec_start_one_frame(struct vpu_instance *inst, struct dec_param *param,
+				  u32 *fail_res)
+{
+	struct dec_info *p_dec_info;
+	u32 reg_val;
+	int ret;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_dec_info = &inst->codec_info->dec_info;
+
+	vpu_write_reg(inst->dev, W6_CMD_DEC_PIC_BS_RD_PTR, p_dec_info->stream_rd_ptr);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_PIC_BS_WR_PTR, p_dec_info->stream_wr_ptr);
+
+	reg_val = DEC_PIC_BS_OPTION_STREAM_END(p_dec_info->stream_end) |
+		  DEC_PIC_BS_OPTION_EXPLICIT_END_ON;
+	vpu_write_reg(inst->dev, W6_CMD_DEC_PIC_BS_OPTION, reg_val);
+
+	reg_val = DEC_PIC_SEC_AXI_IP_ENABLE(p_dec_info->sec_axi.use_dec_ip) |
+		  DEC_PIC_SEC_AXI_LF_ENABLE(p_dec_info->sec_axi.use_dec_lf_row);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_PIC_SEC_AXI, reg_val);
+
+	reg_val = DEC_PIC_OPTION_MODE(W6_DEC_PIC_OPT_NORMAL);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_PIC_OPTION, reg_val);
+
+	reg_val = DEC_PIC_TEMPORAL_ID_PLUS1_SPATIAL(DECODE_ALL_SPATIAL_LAYERS) |
+		  DEC_PIC_TEMPORAL_ID_PLUS1(DECODE_ALL_TEMPORAL_LAYERS);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_PIC_TEMPORAL_ID_PLUS1, reg_val);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_PIC_SEQ_CHANGE_ENABLE_FLAG,
+		      p_dec_info->seq_change_mask);
+	reg_val = lower_32_bits(param->timestamp);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_PIC_TIMESTAMP_LOW, reg_val);
+	reg_val = upper_32_bits(param->timestamp);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_PIC_TIMESTAMP_HIGH, reg_val);
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_DEC_PIC);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return ret;
+	}
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS)) {
+		*fail_res = vpu_read_reg(inst->dev, W6_RET_FAIL_REASON);
+		wave6_print_reg_err(inst->dev, *fail_res);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int wave6_vpu_dec_get_output_info(struct vpu_instance *inst, struct dec_output_info *info)
+{
+	struct dec_info *p_dec_info;
+	u32 reg_val, i;
+	int decoded_idx = -1, disp_idx = -1;
+	int ret;
+
+	if (WARN_ON(!info))
+		return -EINVAL;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_dec_info = &inst->codec_info->dec_info;
+
+	ret = wave6_send_query(inst->dev, inst->id, inst->std, W6_QUERY_OPT_GET_RESULT);
+	if (ret) {
+		info->rd_ptr = p_dec_info->stream_rd_ptr;
+		info->wr_ptr = p_dec_info->stream_wr_ptr;
+		return ret;
+	}
+
+	info->decoding_success = vpu_read_reg(inst->dev, W6_RET_DEC_DECODING_SUCCESS);
+	if (!info->decoding_success)
+		info->error_reason = vpu_read_reg(inst->dev, W6_RET_DEC_ERR_INFO);
+	else
+		info->warn_info = vpu_read_reg(inst->dev, W6_RET_DEC_WARN_INFO);
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_PIC_TYPE);
+	info->ctu_size = DEC_PIC_TYPE_CTU_SIZE(reg_val);
+	info->nal_type = DEC_PIC_TYPE_NAL_UNIT_TYPE(reg_val);
+
+	if (reg_val & DEC_PIC_TYPE_B)
+		info->pic_type = PIC_TYPE_B;
+	else if (reg_val & DEC_PIC_TYPE_P)
+		info->pic_type = PIC_TYPE_P;
+	else if (reg_val & DEC_PIC_TYPE_I)
+		info->pic_type = PIC_TYPE_I;
+	else
+		info->pic_type = PIC_TYPE_MAX;
+	if (inst->std == W_HEVC_DEC) {
+		if (info->pic_type == PIC_TYPE_I &&
+		    (info->nal_type == H265_NAL_UNIT_TYPE_IDR_W_RADL ||
+		     info->nal_type == H265_NAL_UNIT_TYPE_IDR_N_LP))
+			info->pic_type = PIC_TYPE_IDR;
+	} else if (inst->std == W_AVC_DEC) {
+		if (info->pic_type == PIC_TYPE_I &&
+		    info->nal_type == H264_NAL_UNIT_TYPE_IDR_PICTURE)
+			info->pic_type = PIC_TYPE_IDR;
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_DECODED_FLAG);
+	if (reg_val) {
+		struct frame_buffer fb;
+		dma_addr_t addr = vpu_read_reg(inst->dev, W6_RET_DEC_DECODED_ADDR);
+
+		fb = wave6_dec_get_display_buffer(inst, addr);
+		info->frame_decoded_addr = addr;
+		info->frame_decoded = true;
+		decoded_idx = fb.index;
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_DISPLAY_FLAG);
+	if (reg_val) {
+		struct frame_buffer fb;
+		dma_addr_t addr = vpu_read_reg(inst->dev, W6_RET_DEC_DISPLAY_ADDR);
+
+		fb = wave6_dec_get_display_buffer(inst, addr);
+		info->frame_display_addr = addr;
+		info->frame_display = true;
+		disp_idx = fb.index;
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_DISP_IDC);
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {
+		if (reg_val & (1 << i)) {
+			dma_addr_t addr;
+
+			addr = vpu_read_reg(inst->dev, W6_RET_DEC_DISP_LINEAR_ADDR(i));
+
+			info->disp_frame_addr[info->disp_frame_num] = addr;
+			info->disp_frame_num++;
+		}
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_RELEASE_IDC);
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {
+		if (reg_val & (1 << i)) {
+			dma_addr_t addr;
+
+			addr = vpu_read_reg(inst->dev, W6_RET_DEC_DISP_LINEAR_ADDR(i));
+
+			wave6_dec_remove_display_buffer(inst, addr);
+			info->release_disp_frame_addr[info->release_disp_frame_num] = addr;
+			info->release_disp_frame_num++;
+		}
+	}
+
+	if (inst->std == W_HEVC_DEC) {
+		info->decoded_poc = -1;
+		info->display_poc = -1;
+		if (decoded_idx >= 0)
+			info->decoded_poc = vpu_read_reg(inst->dev, W6_RET_DEC_PIC_POC);
+	} else if (inst->std == W_AVC_DEC) {
+		info->decoded_poc = -1;
+		info->display_poc = -1;
+		if (decoded_idx >= 0)
+			info->decoded_poc = vpu_read_reg(inst->dev, W6_RET_DEC_PIC_POC);
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_PIC_SIZE);
+	info->dec_pic_width = DEC_PIC_SIZE_WIDTH(reg_val);
+	info->dec_pic_height = DEC_PIC_SIZE_HEIGHT(reg_val);
+
+	info->rd_ptr = wave6_vpu_dec_get_rd_ptr(inst);
+	info->wr_ptr = p_dec_info->stream_wr_ptr;
+	info->byte_pos_frame_start = vpu_read_reg(inst->dev, W6_RET_DEC_AU_START_POS);
+	info->byte_pos_frame_end = vpu_read_reg(inst->dev, W6_RET_DEC_AU_END_POS);
+	info->stream_end = vpu_read_reg(inst->dev, W6_RET_DEC_STREAM_END);
+	info->notification_flags = vpu_read_reg(inst->dev, W6_RET_DEC_NOTIFICATION);
+	info->last_frame_in_au = vpu_read_reg(inst->dev, W6_RET_DEC_LAST_FRAME_FLAG);
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_TIMESTAMP_LOW);
+	info->timestamp = vpu_read_reg(inst->dev, W6_RET_DEC_TIMESTAMP_HIGH);
+	info->timestamp = (info->timestamp << 32) | reg_val;
+
+	info->cycle.host_cmd_s = vpu_read_reg(inst->dev, W6_RET_CQ_IN_TICK);
+	info->cycle.host_cmd_e = vpu_read_reg(inst->dev, W6_RET_RQ_OUT_TICK);
+	info->cycle.proc_s = vpu_read_reg(inst->dev, W6_RET_HW_RUN_TICK);
+	info->cycle.proc_e = vpu_read_reg(inst->dev, W6_RET_HW_DONE_TICK);
+	info->cycle.vpu_s = vpu_read_reg(inst->dev, W6_RET_FW_RUN_TICK);
+	info->cycle.vpu_e = vpu_read_reg(inst->dev, W6_RET_FW_DONE_TICK);
+	info->cycle.frame_cycle = (info->cycle.vpu_e - info->cycle.host_cmd_s) *
+				  CYCLE_PER_TICK_VALUE;
+	info->cycle.proc_cycle = (info->cycle.proc_e - info->cycle.proc_s) *
+				 CYCLE_PER_TICK_VALUE;
+	info->cycle.vpu_cycle = ((info->cycle.vpu_e - info->cycle.vpu_s) -
+				 (info->cycle.proc_e - info->cycle.proc_s)) *
+				 CYCLE_PER_TICK_VALUE;
+
+	if (decoded_idx >= 0 && decoded_idx < WAVE6_MAX_FBS)
+		p_dec_info->dec_out_info[decoded_idx].decoded_poc = info->decoded_poc;
+
+	if (disp_idx >= 0 && disp_idx < WAVE6_MAX_FBS) {
+		info->display_poc = p_dec_info->dec_out_info[disp_idx].decoded_poc;
+		info->disp_pic_width = p_dec_info->dec_out_info[disp_idx].dec_pic_width;
+		info->disp_pic_height = p_dec_info->dec_out_info[disp_idx].dec_pic_height;
+	}
+
+	info->sequence_no = p_dec_info->seq_info.sequence_no;
+	if (decoded_idx >= 0 && decoded_idx < WAVE6_MAX_FBS)
+		p_dec_info->dec_out_info[decoded_idx] = *info;
+
+	if (info->notification_flags & DEC_NOTI_FLAG_SEQ_CHANGE) {
+		wave6_get_dec_seq_result(inst, &p_dec_info->seq_info);
+		p_dec_info->seq_info.sequence_no++;
+	}
+
+	return 0;
+}
+
+int wave6_vpu_dec_fini_seq(struct vpu_instance *inst, u32 *fail_res)
+{
+	int ret;
+
+	lockdep_assert_held(&inst->dev->hw_lock);
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_DESTROY_INSTANCE);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret)
+		return -ETIMEDOUT;
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS)) {
+		*fail_res = vpu_read_reg(inst->dev, W6_RET_FAIL_REASON);
+		wave6_print_reg_err(inst->dev, *fail_res);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+dma_addr_t wave6_vpu_dec_get_rd_ptr(struct vpu_instance *inst)
+{
+	lockdep_assert_held(&inst->dev->hw_lock);
+
+	return vpu_read_reg(inst->dev, W6_RET_DEC_BS_RD_PTR);
+}
+
+int wave6_vpu_dec_flush_instance(struct vpu_instance *inst)
+{
+	struct dec_info *p_dec_info;
+	int ret, index;
+	u32 remove_idc;
+	u32 using_idc;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_dec_info = &inst->codec_info->dec_info;
+	if (!p_dec_info->seq_info_obtained)
+		return -EINVAL;
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_FLUSH_INSTANCE);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret)
+		return -ETIMEDOUT;
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS)) {
+		u32 reg_val;
+
+		reg_val = vpu_read_reg(inst->dev, W6_RET_FAIL_REASON);
+		wave6_print_reg_err(inst->dev, reg_val);
+		return -EIO;
+	}
+
+	ret = wave6_send_query(inst->dev, inst->id, inst->std, W6_QUERY_OPT_GET_FLUSH_CMD_INFO);
+	if (ret)
+		return ret;
+
+	remove_idc = vpu_read_reg(inst->dev, W6_RET_DEC_FLUSH_CMD_BUF_STATE_UNUSED_IDC) |
+		     vpu_read_reg(inst->dev, W6_RET_DEC_FLUSH_CMD_BUF_STATE_USED_IDC);
+	if (remove_idc)
+		dev_dbg(inst->dev->dev, "%s: remove_idc 0x%x\n", __func__, remove_idc);
+
+	using_idc = vpu_read_reg(inst->dev, W6_RET_DEC_FLUSH_CMD_BUF_STATE_USING_IDC);
+	if (using_idc)
+		dev_dbg(inst->dev->dev, "%s: using_idc 0x%x\n", __func__, using_idc);
+
+	for (index = 0; index < WAVE6_MAX_FBS; index++) {
+		dma_addr_t addr;
+
+		if (!((remove_idc >> index) & 0x1))
+			continue;
+
+		addr = vpu_read_reg(inst->dev, W6_RET_DEC_FLUSH_CMD_DISP_ADDR(index));
+		wave6_dec_remove_display_buffer(inst, addr);
+	}
+
+	return 0;
+}
+
+struct enc_cmd_set_param_reg {
+	u32 enable;
+	u32 src_size;
+	u32 custom_map_endian;
+	u32 sps;
+	u32 pps;
+	u32 gop;
+	u32 intra;
+	u32 conf_win0;
+	u32 conf_win1;
+	u32 rdo;
+	u32 slice;
+	u32 intra_refresh;
+	u32 intra_qp;
+	u32 rc_frame_rate;
+	u32 rc_target_rate;
+	u32 rc;
+	u32 hvs;
+	u32 rc_max_bitrate;
+	u32 rc_vbv_buffer_size;
+	u32 inter_qp;
+	u32 rot;
+	u32 num_units_in_tick;
+	u32 time_scale;
+	u32 num_ticks_poc_diff_one;
+	u32 max_intra_pic_bit;
+	u32 max_inter_pic_bit;
+	u32 bg;
+	u32 non_vcl_param;
+	u32 vui_rbsp_addr;
+	u32 hrd_rbsp_addr;
+	u32 qround_offset;
+	u32 quant1;
+	u32 quant2;
+	u32 custom_gop;
+	u32 custom_gop_pic[MAX_CUSTOM_GOP_NUM];
+	u32 tile_param;
+	u32 custom_lambda[MAX_CUSTOM_LAMBDA_NUM];
+	u32 temp_layer_qp[MAX_NUM_CHANGEABLE_TEMP_LAYER];
+	u32 scaler_size;
+	u32 scaler;
+	u32 color;
+	u32 sar;
+	u32 sar_extended;
+};
+
+struct enc_cmd_change_param_reg {
+	u32 enable;
+	u32 rc_target_rate;
+};
+
+int wave6_vpu_build_up_enc_param(struct device *dev, struct vpu_instance *inst,
+				 struct enc_open_param *param)
+{
+	struct enc_info *p_enc_info = &inst->codec_info->enc_info;
+	u32 reg_val;
+	int ret;
+
+	lockdep_assert_held(&inst->dev->hw_lock);
+
+	p_enc_info->seq_info_obtained = false;
+	p_enc_info->sec_axi.use_enc_rdo = true;
+	p_enc_info->sec_axi.use_enc_lf = true;
+	p_enc_info->mir_dir = param->mir_dir;
+	p_enc_info->rot_angle = param->rot_angle;
+
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_TEMP_BASE, param->inst_buffer.temp_base);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_TEMP_SIZE, param->inst_buffer.temp_size);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_AR_TABLE_BASE, param->inst_buffer.ar_base);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_BS_PARAM, VPU_STREAM_ENDIAN);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_SRC_OPT, 0);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_ADDR_EXT, param->ext_addr_vcpu);
+
+	reg_val = CREATE_INST_CORE_INFO_CQ_DEPTH(COMMAND_QUEUE_DEPTH) |
+		  CREATE_INST_CORE_INFO_CORE_IDC(SINGLE_CORE_IDC) |
+		  CREATE_INST_CORE_INFO_CORE_NUM(SINGLE_CORE);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_CORE_INFO, reg_val);
+
+	reg_val = CREATE_INST_PRIORITY_SECURITY_FLAG(param->is_secure_inst) |
+		  CREATE_INST_PRIORITY_VALUE(param->inst_priority);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_PRIORITY, reg_val);
+	vpu_write_reg(inst->dev, W6_CMD_CREATE_INST_TIMEOUT_CYCLE_COUNT,
+		      W6_VPU_TIMEOUT_CYCLE_COUNT);
+
+	wave6_send_command(inst->dev, 0, inst->std, W6_CMD_CREATE_INSTANCE);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return ret;
+	}
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS)) {
+		u32 reason_code = vpu_read_reg(inst->dev, W6_RET_FAIL_REASON);
+
+		wave6_print_reg_err(inst->dev, reason_code);
+		return -EIO;
+	}
+
+	inst->id = vpu_read_reg(inst->dev, W6_RET_INSTANCE_ID);
+
+	return 0;
+}
+
+static int wave6_set_enc_crop_info(u32 codec, struct enc_codec_param *param,
+				   u32 rot_mir, int width, int height)
+{
+	int aligned_width = (codec == W_HEVC_ENC) ? ALIGN(width, 32) : ALIGN(width, 16);
+	int aligned_height = (codec == W_HEVC_ENC) ? ALIGN(height, 32) : ALIGN(height, 16);
+	int pad_right, pad_bot;
+	int crop_right, crop_left, crop_top, crop_bot;
+
+	if (codec == W_HEVC_ENC &&
+	    (!rot_mir ||
+	     rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_MIRV | SET_PARAM_ROT_180)))
+		return 0;
+
+	pad_right = aligned_width - width;
+	pad_bot = aligned_height - height;
+
+	if (param->conf_win.right > 0)
+		crop_right = param->conf_win.right + pad_right;
+	else
+		crop_right = pad_right;
+
+	if (param->conf_win.bottom > 0)
+		crop_bot = param->conf_win.bottom + pad_bot;
+	else
+		crop_bot = pad_bot;
+
+	crop_top = param->conf_win.top;
+	crop_left = param->conf_win.left;
+
+	param->conf_win.top = crop_top;
+	param->conf_win.left = crop_left;
+	param->conf_win.bottom = crop_bot;
+	param->conf_win.right = crop_right;
+
+	if (rot_mir == SET_PARAM_ROT_90 ||
+	    rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_MIRV | SET_PARAM_ROT_270)) {
+		param->conf_win.top = crop_right;
+		param->conf_win.left = crop_top;
+		param->conf_win.bottom = crop_left;
+		param->conf_win.right = crop_bot;
+	} else if (rot_mir == SET_PARAM_ROT_180 ||
+		   rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_MIRV)) {
+		param->conf_win.top = crop_bot;
+		param->conf_win.left = crop_right;
+		param->conf_win.bottom = crop_top;
+		param->conf_win.right = crop_left;

drivers/media/platform/chips-media/wave6/wave6-hw.c line 1151

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	} else if (rot_mir == SET_PARAM_ROT_270 ||
+		   rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_MIRV | SET_PARAM_ROT_90)) {
+		param->conf_win.top = crop_left;
+		param->conf_win.left = crop_bot;
+		param->conf_win.bottom = crop_right;
+		param->conf_win.right = crop_top;
+	} else if (rot_mir == SET_PARAM_ROT_MIRV ||
+		   rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_180)) {
+		param->conf_win.top = crop_bot;
+		param->conf_win.bottom = crop_top;

put bottom/right here to make code consistent.

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +7 -0
+	} else if (rot_mir == SET_PARAM_ROT_MIRH ||
+		   rot_mir == (SET_PARAM_ROT_MIRV | SET_PARAM_ROT_180)) {
+		param->conf_win.left = crop_right;
+		param->conf_win.right = crop_left;
+	} else if (rot_mir == (SET_PARAM_ROT_MIRV | SET_PARAM_ROT_90) ||
+		   rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_270)) {
+		param->conf_win.top = crop_left;

drivers/media/platform/chips-media/wave6/wave6-hw.c line 1168

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+		param->conf_win.left = crop_top;
+		param->conf_win.bottom = crop_right;
+		param->conf_win.right = crop_bot;
+	} else if (rot_mir == (SET_PARAM_ROT_MIRV | SET_PARAM_ROT_270) ||
+		   rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_90)) {
+		param->conf_win.top = crop_right;
+		param->conf_win.left = crop_bot;
+		param->conf_win.bottom = crop_left;
+		param->conf_win.right = crop_top;
+	}
	else {
		param->conf_win.top = crop_top;
		param->conf_win.left = crop_left;
		param->conf_win.bottom = crop_bot;
		param->conf_win.right = crop_right;
	}

Avoid overwrite it again at if-else branch.

Quoted 38 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +38 -0
+
+	return 0;
+}
+
+static void wave6_update_enc_info(struct enc_info *p_enc_info)
+{
+	struct enc_open_param op = p_enc_info->open_param;
+
+	p_enc_info->width = op.pic_width;
+	p_enc_info->height = op.pic_height;
+	p_enc_info->c_fmt_idc = get_chroma_format_idc(op.output_format);
+}
+
+static void wave6_gen_set_param_reg_common(struct enc_info *p_enc_info, enum codec_std std,
+					   struct enc_cmd_set_param_reg *reg)
+{
+	struct enc_open_param *p_open_param = &p_enc_info->open_param;
+	struct enc_codec_param *p_param = &p_open_param->codec_param;
+	unsigned int i;
+	u32 rot_mir = 0;
+
+	switch (p_enc_info->rot_angle) {
+	case ROT_0:
+		break;
+	case ROT_90:
+		rot_mir |= SET_PARAM_ROT_90;
+		break;
+	case ROT_180:
+		rot_mir |= SET_PARAM_ROT_180;
+		break;
+	case ROT_270:
+		rot_mir |= SET_PARAM_ROT_270;
+		break;
+	}
+
+	switch (p_enc_info->mir_dir) {
+	case MIR_NONE:
+		break;

drivers/media/platform/chips-media/wave6/wave6-hw.c line 1216

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	case MIR_VER:
+		rot_mir |= SET_PARAM_ROT_MIRV;
+		break;
+	case MIR_HOR:
+		rot_mir |= SET_PARAM_ROT_MIRH;
+		break;
+	case MIR_HOR_VER:
+		rot_mir |= SET_PARAM_ROT_MIRH | SET_PARAM_ROT_MIRV;
+		break;
+	}

use small convert helper to make this function shorter

Quoted 544 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +544 -0
+
+	wave6_set_enc_crop_info(std, p_param, rot_mir, p_enc_info->width, p_enc_info->height);
+
+	reg->custom_map_endian = VPU_USER_DATA_ENDIAN;
+	if (rot_mir)
+		reg->rot = rot_mir | SET_PARAM_ROT_ENABLE;
+	reg->src_size = SET_PARAM_SRC_SIZE_HEIGHT(p_enc_info->height) |
+			SET_PARAM_SRC_SIZE_WIDTH(p_enc_info->width);
+	reg->conf_win0 = SET_PARAM_CONF_WIN0_BOTTOM(p_param->conf_win.bottom) |
+			 SET_PARAM_CONF_WIN0_TOP(p_param->conf_win.top);
+	reg->conf_win1 = SET_PARAM_CONF_WIN1_RIGHT(p_param->conf_win.right) |
+			 SET_PARAM_CONF_WIN1_LEFT(p_param->conf_win.left);
+	reg->gop = SET_PARAM_GOP_TEMP_LAYER_CNT(p_param->temp_layer_cnt) |
+		   SET_PARAM_GOP_TEMP_LAYER3_QP_ENABLE(p_param->temp_layer[3].change_qp) |
+		   SET_PARAM_GOP_TEMP_LAYER2_QP_ENABLE(p_param->temp_layer[2].change_qp) |
+		   SET_PARAM_GOP_TEMP_LAYER1_QP_ENABLE(p_param->temp_layer[1].change_qp) |
+		   SET_PARAM_GOP_TEMP_LAYER0_QP_ENABLE(p_param->temp_layer[0].change_qp) |
+		   SET_PARAM_GOP_PRESET_IDX(p_param->gop_preset_idx);
+	for (i = 0; i < MAX_NUM_CHANGEABLE_TEMP_LAYER; i++) {
+		reg->temp_layer_qp[i] = SET_PARAM_TEMP_LAYER_QP_B(p_param->temp_layer[i].qp_b) |
+					SET_PARAM_TEMP_LAYER_QP_P(p_param->temp_layer[i].qp_p) |
+					SET_PARAM_TEMP_LAYER_QP_I(p_param->temp_layer[i].qp_i);
+	}
+	reg->intra_refresh = SET_PARAM_INTRA_REFRESH_ARGUMENT(p_param->intra_refresh_arg) |
+			     SET_PARAM_INTRA_REFRESH_MODE(p_param->intra_refresh_mode);
+	reg->intra_qp = SET_PARAM_INTRA_QP_MAX(p_param->max_qp_i) |
+			SET_PARAM_INTRA_QP_MIN(p_param->min_qp_i);
+	reg->inter_qp = SET_PARAM_INTER_QP_MAX_B(p_param->max_qp_b) |
+			SET_PARAM_INTER_QP_MIN_B(p_param->min_qp_b) |
+			SET_PARAM_INTER_QP_MAX_P(p_param->max_qp_p) |
+			SET_PARAM_INTER_QP_MIN_P(p_param->min_qp_p);
+	reg->rc_frame_rate = p_param->frame_rate;
+	reg->rc_target_rate = p_param->bitrate;
+	reg->rc_max_bitrate = p_param->max_bitrate;
+	reg->rc_vbv_buffer_size = p_param->cpb_size;
+	reg->rc = SET_PARAM_RC_UPDATE_SPEED(p_param->rc_update_speed) |
+		  SET_PARAM_RC_INITIAL_LEVEL(p_param->rc_initial_level) |
+		  SET_PARAM_RC_INITIAL_QP(p_param->rc_initial_qp) |
+		  SET_PARAM_RC_MODE(p_param->rc_mode) |
+		  SET_PARAM_RC_PIC_LEVEL_MAX_DELTA_QP(p_param->pic_rc_max_dqp) |
+		  SET_PARAM_RC_VBV_OVERFLOW_DROP_FRAME(p_param->en_skip_frame) |
+		  SET_PARAM_RC_CU_LEVEL_ENABLE(p_param->en_cu_level_rate_control) |
+		  SET_PARAM_RC_ENABLE(p_param->en_rate_control);
+	reg->hvs = SET_PARAM_HVS_MAX_DELTA_QP(p_param->max_delta_qp) |
+		   SET_PARAM_HVS_QP_SCALE(p_param->hvs_qp_scale_div2);
+	reg->num_units_in_tick = p_param->num_units_in_tick;
+	reg->time_scale = p_param->time_scale;
+	reg->num_ticks_poc_diff_one = p_param->num_ticks_poc_diff_one;
+	reg->max_intra_pic_bit = p_param->max_intra_pic_bit;
+	reg->max_inter_pic_bit = p_param->max_inter_pic_bit;
+	reg->bg = SET_PARAM_BG_DELTA_QP(p_param->bg_delta_qp) |
+		  SET_PARAM_BG_THRESHOLD_MEAN_DIFF(p_param->bg_th_mean_diff) |
+		  SET_PARAM_BG_THRESHOLD_MAX_DIFF(p_param->bg_th_diff) |
+		  SET_PARAM_BG_ENABLE(p_param->en_bg_detect);
+	reg->qround_offset = SET_PARAM_QROUND_OFFSET_INTER(p_param->qround_inter) |
+			     SET_PARAM_QROUND_OFFSET_INTRA(p_param->qround_intra);
+	reg->custom_gop = p_param->gop_param.size;
+	for (i = 0; i < p_param->gop_param.size; i++) {
+		struct custom_gop_pic_param pic = p_param->gop_param.pic[i];
+
+		reg->custom_gop_pic[i] = SET_PARAM_CUSTOM_GOP_PIC_TEMP_ID(pic.temporal_id) |
+					 SET_PARAM_CUSTOM_GOP_PIC_REF1_POC(pic.ref_poc_l1) |
+					 SET_PARAM_CUSTOM_GOP_PIC_REF0_POC(pic.ref_poc_l0) |
+					 SET_PARAM_CUSTOM_GOP_PIC_MULTI_REF_P(pic.multi_ref_p) |
+					 SET_PARAM_CUSTOM_GOP_PIC_QP(pic.pic_qp) |
+					 SET_PARAM_CUSTOM_GOP_PIC_POC_OFFSET(pic.poc_offset) |
+					 SET_PARAM_CUSTOM_GOP_PIC_TYPE(pic.pic_type);
+	}
+	for (i = 0; i < MAX_CUSTOM_LAMBDA_NUM; i++) {
+		reg->custom_lambda[i] = SET_PARAM_CUSTOM_LAMBDA_SSD(p_param->lambda_ssd[i]) |
+					SET_PARAM_CUSTOM_LAMBDA_SAD(p_param->lambda_sad[i]);
+	}
+	reg->scaler_size = SET_PARAM_SCALER_SIZE_HEIGHT(p_enc_info->height) |
+			   SET_PARAM_SCALER_SIZE_WIDTH(p_enc_info->width);
+	reg->scaler = SET_PARAM_SCALER_COEF_MODE(p_enc_info->scaler.coef_mode) |
+		      SET_PARAM_SCALER_ENABLE(p_enc_info->scaler.enable);
+	reg->color = SET_PARAM_COLOR_RANGE(p_param->color.color_range) |
+		     SET_PARAM_COLOR_MATRIX_COEFF(p_param->color.matrix_coefficients) |
+		     SET_PARAM_COLOR_TRANS_CHAR(p_param->color.transfer_characteristics) |
+		     SET_PARAM_COLOR_PRIMARIES(p_param->color.color_primaries) |
+		     SET_PARAM_COLOR_DESCRIPTION_PRESENT_ON;
+	reg->sar = SET_PARAM_SAR_ASPECT_RATIO_IDC(p_param->sar.idc) |
+		   SET_PARAM_SAR_ASPECT_RATIO_ENABLE(p_param->sar.enable);
+	reg->sar_extended = SET_PARAM_SAR_EXTENDED_HEIGHT(p_param->sar.height) |
+			    SET_PARAM_SAR_EXTENDED_WIDTH(p_param->sar.width);
+}
+
+static void wave6_gen_set_param_reg_hevc(struct enc_info *p_enc_info,
+					 struct enc_cmd_set_param_reg *reg)
+{
+	struct enc_open_param *p_open_param = &p_enc_info->open_param;
+	struct enc_codec_param *p_param = &p_open_param->codec_param;
+
+	reg->sps = SET_PARAM_SPS_DEFAULT_SCALING_LIST(p_param->en_scaling_list) |
+		   SET_PARAM_SPS_STILL_PICTURE(p_param->en_still_picture) |
+		   SET_PARAM_SPS_AUTO_LEVEL_ADJUSTING_ON |
+		   SET_PARAM_SPS_STRONG_INTRA_SMOOTHING(p_param->en_intra_smooth) |
+		   SET_PARAM_SPS_INTRA_TRANSFORM_SKIP_ON |
+		   SET_PARAM_SPS_SAMPLE_ADAPTIVE_OFFSET(p_param->en_sao) |
+		   SET_PARAM_SPS_TEMPORAL_MVP(p_param->en_temporal_mvp) |
+		   SET_PARAM_SPS_LONGTERM_REFERENCE(p_param->en_longterm) |
+		   SET_PARAM_SPS_C_FMT_IDC(p_enc_info->c_fmt_idc) |
+		   SET_PARAM_SPS_BIT_DEPTH(p_param->internal_bit_depth) |
+		   SET_PARAM_SPS_TIER(p_param->tier) |
+		   SET_PARAM_SPS_LEVEL(p_param->level) |
+		   SET_PARAM_SPS_PROFILE(p_param->profile);
+	reg->pps = SET_PARAM_PPS_CR_QP_OFFSET(p_param->cr_qp_offset) |
+		   SET_PARAM_PPS_CB_QP_OFFSET(p_param->cb_qp_offset) |
+		   SET_PARAM_PPS_TC_OFFSET_DIV2(p_param->tc_offset_div2) |
+		   SET_PARAM_PPS_BETA_OFFSET_DIV2(p_param->beta_offset_div2) |
+		   SET_PARAM_PPS_DEBLOCKING_FILTER(!p_param->en_dbk) |
+		   SET_PARAM_PPS_LF_SLICE_BOUNDARY(p_param->en_lf_slice_boundary) |
+		   SET_PARAM_PPS_CONST_INTRA_PREDICTION(p_param->en_const_intra_pred);
+	reg->intra = SET_PARAM_INTRA_PERIOD(p_param->intra_period) |
+		     SET_PARAM_INTRA_HEADER_MODE(p_param->forced_idr_header) |
+		     SET_PARAM_INTRA_QP(p_param->qp) |
+		     SET_PARAM_INTRA_REFRESH_TYPE(p_param->decoding_refresh_type);
+	reg->rdo = SET_PARAM_RDO_CUSTOM_LAMBDA(p_param->en_custom_lambda) |
+		   SET_PARAM_RDO_ME_SEARCH_CENTER_ON |
+		   SET_PARAM_RDO_QROUND_OFFSET(p_param->en_qround_offset) |
+		   SET_PARAM_RDO_ADAPTIVE_ROUND_ON |
+		   SET_PARAM_RDO_HVS_QP(p_param->en_hvs_qp);
+	reg->slice = SET_PARAM_SLICE_ARGUMENT(p_param->slice_arg) |
+		     SET_PARAM_SLICE_MODE(p_param->slice_mode);
+	reg->quant2 = SET_PARAM_QUANT2_LAMBDA_DQP_INTER(p_param->lambda_dqp_inter) |
+		      SET_PARAM_QUANT2_LAMBDA_DQP_INTRA(p_param->lambda_dqp_intra);
+	reg->non_vcl_param = SET_PARAM_NON_VCL_PARAM_ENCODE_VUI;
+}
+
+static void wave6_gen_set_param_reg_avc(struct enc_info *p_enc_info,
+					struct enc_cmd_set_param_reg *reg)
+{
+	struct enc_open_param *p_open_param = &p_enc_info->open_param;
+	struct enc_codec_param *p_param = &p_open_param->codec_param;
+
+	reg->sps = SET_PARAM_SPS_DEFAULT_SCALING_LIST(p_param->en_scaling_list) |
+		   SET_PARAM_SPS_AUTO_LEVEL_ADJUSTING_ON |
+		   SET_PARAM_SPS_LONGTERM_REFERENCE(p_param->en_longterm) |
+		   SET_PARAM_SPS_C_FMT_IDC(p_enc_info->c_fmt_idc) |
+		   SET_PARAM_SPS_BIT_DEPTH(p_param->internal_bit_depth) |
+		   SET_PARAM_SPS_LEVEL(p_param->level) |
+		   SET_PARAM_SPS_PROFILE(p_param->profile);
+	reg->pps = SET_PARAM_PPS_ENTROPY_CODING_MODE(p_param->en_cabac) |
+		   SET_PARAM_PPS_TRANSFORM8X8(p_param->en_transform8x8) |
+		   SET_PARAM_PPS_CR_QP_OFFSET(p_param->cr_qp_offset) |
+		   SET_PARAM_PPS_CB_QP_OFFSET(p_param->cb_qp_offset) |
+		   SET_PARAM_PPS_TC_OFFSET_DIV2(p_param->tc_offset_div2) |
+		   SET_PARAM_PPS_BETA_OFFSET_DIV2(p_param->beta_offset_div2) |
+		   SET_PARAM_PPS_DEBLOCKING_FILTER(!p_param->en_dbk) |
+		   SET_PARAM_PPS_LF_SLICE_BOUNDARY(p_param->en_lf_slice_boundary) |
+		   SET_PARAM_PPS_CONST_INTRA_PREDICTION(p_param->en_const_intra_pred);
+	reg->intra = SET_PARAM_INTRA_HEADER_MODE_AVC(p_param->forced_idr_header) |
+		     SET_PARAM_INTRA_IDR_PERIOD_AVC(p_param->idr_period) |
+		     SET_PARAM_INTRA_PERIOD_AVC(p_param->intra_period) |
+		     SET_PARAM_INTRA_QP_AVC(p_param->qp);
+	reg->rdo = SET_PARAM_RDO_CUSTOM_LAMBDA(p_param->en_custom_lambda) |
+		   SET_PARAM_RDO_QROUND_OFFSET(p_param->en_qround_offset) |
+		   SET_PARAM_RDO_ADAPTIVE_ROUND_ON |
+		   SET_PARAM_RDO_HVS_QP(p_param->en_hvs_qp);
+	reg->slice = SET_PARAM_SLICE_ARGUMENT(p_param->slice_arg) |
+		     SET_PARAM_SLICE_MODE(p_param->slice_mode);
+	reg->quant2 = SET_PARAM_QUANT2_LAMBDA_DQP_INTER(p_param->lambda_dqp_inter) |
+		      SET_PARAM_QUANT2_LAMBDA_DQP_INTRA(p_param->lambda_dqp_intra);
+	reg->non_vcl_param = SET_PARAM_NON_VCL_PARAM_ENCODE_VUI;
+}
+
+static void wave6_gen_change_param_reg_common(struct vpu_instance *inst,
+					      struct enc_info *p_enc_info,
+					      struct enc_cmd_change_param_reg *reg)
+{
+	if (p_enc_info->open_param.codec_param.bitrate != inst->enc_ctrls.bitrate) {
+		reg->enable |= SET_PARAM_ENABLE_RC_TARGET_RATE;
+		reg->rc_target_rate = inst->enc_ctrls.bitrate;
+		p_enc_info->open_param.codec_param.bitrate = inst->enc_ctrls.bitrate;
+	}
+}
+
+int wave6_vpu_enc_issue_seq_init(struct vpu_instance *inst)
+{
+	struct enc_cmd_set_param_reg reg;
+	struct enc_info *p_enc_info;
+	u32 i;
+	int ret;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_enc_info = &inst->codec_info->enc_info;
+
+	memset(&reg, 0, sizeof(struct enc_cmd_set_param_reg));
+
+	wave6_update_enc_info(p_enc_info);
+
+	wave6_gen_set_param_reg_common(p_enc_info, inst->std, &reg);
+	if (inst->std == W_HEVC_ENC)
+		wave6_gen_set_param_reg_hevc(p_enc_info, &reg);
+	else if (inst->std == W_AVC_ENC)
+		wave6_gen_set_param_reg_avc(p_enc_info, &reg);
+
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_OPTION, W6_SET_PARAM_OPT_COMMON);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_ENABLE, reg.enable);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_SRC_SIZE, reg.src_size);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_CUSTOM_MAP_ENDIAN, reg.custom_map_endian);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_SPS, reg.sps);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_PPS, reg.pps);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_GOP, reg.gop);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_INTRA, reg.intra);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_CONF_WIN0, reg.conf_win0);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_CONF_WIN1, reg.conf_win1);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_RDO, reg.rdo);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_SLICE, reg.slice);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_INTRA_REFRESH, reg.intra_refresh);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_INTRA_QP, reg.intra_qp);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_RC_FRAME_RATE, reg.rc_frame_rate);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_RC_TARGET_RATE, reg.rc_target_rate);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_RC, reg.rc);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_HVS, reg.hvs);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_RC_MAX_BITRATE, reg.rc_max_bitrate);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_RC_VBV_BUFFER_SIZE, reg.rc_vbv_buffer_size);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_INTER_QP, reg.inter_qp);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_ROT, reg.rot);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_NUM_UNITS_IN_TICK, reg.num_units_in_tick);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_TIME_SCALE, reg.time_scale);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_NUM_TICKS_POC_DIFF_ONE,
+		      reg.num_ticks_poc_diff_one);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_MAX_INTRA_PIC_BIT, reg.max_intra_pic_bit);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_MAX_INTER_PIC_BIT, reg.max_inter_pic_bit);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_BG, reg.bg);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_NON_VCL_PARAM, reg.non_vcl_param);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_VUI_RBSP_ADDR, reg.vui_rbsp_addr);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_HRD_RBSP_ADDR, reg.hrd_rbsp_addr);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_QROUND_OFFSET, reg.qround_offset);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_QUANT1, reg.quant1);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_QUANT2, reg.quant2);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_CUSTOM_GOP, reg.custom_gop);
+	for (i = 0; i < MAX_CUSTOM_GOP_NUM; i++)
+		vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_CUSTOM_GOP_PIC(i),
+			      reg.custom_gop_pic[i]);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_TILE_PARAM, reg.tile_param);
+	for (i = 0; i < MAX_CUSTOM_LAMBDA_NUM; i++)
+		vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_CUSTOM_LAMBDA(i),
+			      reg.custom_lambda[i]);
+	for (i = 0; i < MAX_NUM_CHANGEABLE_TEMP_LAYER; i++)
+		vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_TEMP_LAYER_QP(i),
+			      reg.temp_layer_qp[i]);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_SCALER_SIZE, reg.scaler_size);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_SCALER, reg.scaler);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_COLOR, reg.color);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_SAR, reg.sar);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_SAR_EXTENDED, reg.sar_extended);
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_ENC_SET_PARAM);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return ret;
+	}
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS)) {
+		u32 reason_code = vpu_read_reg(inst->dev, W6_RET_FAIL_REASON);
+
+		wave6_print_reg_err(inst->dev, reason_code);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int wave6_vpu_enc_get_seq_info(struct vpu_instance *inst, struct enc_seq_info *info)
+{
+	int ret;
+
+	lockdep_assert_held(&inst->dev->hw_lock);
+
+	ret = wave6_send_query(inst->dev, inst->id, inst->std, W6_QUERY_OPT_GET_RESULT);
+	if (ret)
+		return ret;
+
+	if (vpu_read_reg(inst->dev, W6_RET_ENC_ENCODING_SUCCESS) != 1) {
+		info->err_reason = vpu_read_reg(inst->dev, W6_RET_ENC_ERR_INFO);
+		ret = -EIO;
+	} else {
+		info->warn_info = vpu_read_reg(inst->dev, W6_RET_ENC_WARN_INFO);
+	}
+
+	info->min_frame_buffer_count = vpu_read_reg(inst->dev, W6_RET_ENC_NUM_REQUIRED_FBC_FB);
+	info->min_src_frame_count = vpu_read_reg(inst->dev, W6_RET_ENC_MIN_SRC_BUF_NUM);
+	info->max_latency_pictures = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_MAX_LATENCY_PICTURES);
+	info->req_mv_buffer_count = vpu_read_reg(inst->dev, W6_RET_ENC_NUM_REQUIRED_COL_BUF);
+
+	return ret;
+}
+
+int wave6_vpu_enc_issue_seq_change(struct vpu_instance *inst, bool *changed)
+{
+	struct enc_cmd_change_param_reg reg;
+	struct enc_info *p_enc_info;
+	int ret;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_enc_info = &inst->codec_info->enc_info;
+
+	memset(&reg, 0, sizeof(struct enc_cmd_change_param_reg));
+
+	wave6_gen_change_param_reg_common(inst, p_enc_info, &reg);
+
+	if (!reg.enable)
+		return 0;
+
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_OPTION, W6_SET_PARAM_OPT_CHANGE_PARAM);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_ENABLE, reg.enable);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_SET_PARAM_RC_TARGET_RATE, reg.rc_target_rate);
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_ENC_SET_PARAM);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_warn(inst->dev->dev, "enc set param timed out\n");
+		return ret;
+	}
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS)) {
+		u32 reason_code = vpu_read_reg(inst->dev, W6_RET_FAIL_REASON);
+
+		wave6_print_reg_err(inst->dev, reason_code);
+		return -EIO;
+	}
+
+	*changed = true;
+
+	return 0;
+}
+
+int wave6_vpu_enc_register_frame_buffer(struct vpu_instance *inst)
+{
+	struct enc_info *p_enc_info;
+	struct frame_buffer *fb_arr;
+	dma_addr_t fbc_cr_tbl_addr;
+	u32 fbc_count, mv_count, idx;
+	int ret;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_enc_info = &inst->codec_info->enc_info;
+
+	fb_arr = &inst->frame_buf[0];
+	fbc_count = p_enc_info->seq_info.min_frame_buffer_count;
+	mv_count = p_enc_info->seq_info.req_mv_buffer_count;
+	if (!fbc_count || !mv_count)
+		return -EINVAL;
+
+	for (idx = 0; idx < fbc_count; idx++) {
+		if (!inst->aux_vbuf[AUX_BUF_FBC_Y_TBL][idx].daddr)
+			return -EINVAL;
+		if (!inst->aux_vbuf[AUX_BUF_FBC_C_TBL][idx].daddr)
+			return -EINVAL;
+		if (!inst->aux_vbuf[AUX_BUF_SUB_SAMPLE][idx].daddr)
+			return -EINVAL;
+	}
+	for (idx = 0; idx < mv_count; idx++) {
+		if (!inst->aux_vbuf[AUX_BUF_MV_COL][idx].daddr)
+			return -EINVAL;
+	}
+
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_OPTION,
+		      SET_FB_OPTION_END | SET_FB_OPTION_START);
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_PIC_INFO,
+		      SET_FB_PIC_INFO_STRIDE(fb_arr[0].stride));
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_PIC_SIZE,
+		      SET_FB_PIC_SIZE_WIDTH(fb_arr[0].stride) |
+		      SET_FB_PIC_SIZE_HEIGHT(fb_arr[0].height));
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_NUM,
+		      SET_FB_NUM_FBC_END_IDX(fbc_count - 1) |
+		      SET_FB_NUM_MV_COL_END_IDX(mv_count - 1));
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_STRIDE,
+		      SET_FB_FBC_STRIDE_L(fb_arr[0].stride) |
+		      SET_FB_FBC_STRIDE_C(fb_arr[0].stride_c));
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_DEFAULT_CDF, 0);
+
+	for (idx = 0; idx < fbc_count; idx++) {
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_Y(idx), fb_arr[idx].buf_y);
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_C(idx), fb_arr[idx].buf_cb);
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_CR(idx), fb_arr[idx].buf_cr);
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_Y_OFFSET(idx),
+			      inst->aux_vbuf[AUX_BUF_FBC_Y_TBL][idx].daddr);
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_C_OFFSET(idx),
+			      inst->aux_vbuf[AUX_BUF_FBC_C_TBL][idx].daddr);
+		fbc_cr_tbl_addr = inst->aux_vbuf[AUX_BUF_FBC_C_TBL][idx].daddr +
+				  (inst->aux_vbuf[AUX_BUF_FBC_C_TBL][idx].size >> 1);
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_CR_OFFSET(idx),
+			      fbc_cr_tbl_addr);
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_SUB_SAMPLED(idx),
+			      inst->aux_vbuf[AUX_BUF_SUB_SAMPLE][idx].daddr);
+	}
+	for (idx = 0; idx < mv_count; idx++)
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_MV_COL(idx),
+			      inst->aux_vbuf[AUX_BUF_MV_COL][idx].daddr);
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_SET_FB);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return ret;
+	}
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS))
+		return -EIO;
+
+	return 0;
+}
+
+struct enc_cmd_enc_pic_reg {
+	u32 bs_start;
+	u32 bs_size;
+	u32 bs_option;
+	u32 sec_axi;
+	u32 report;
+	u32 mv_histo0;
+	u32 mv_histo1;
+	u32 custom_map_param;
+	u32 custom_map_addr;
+	u32 src_pic_idx;
+	u32 src_addr_y;
+	u32 src_addr_u;
+	u32 src_addr_v;
+	u32 src_stride;
+	u32 src_fmt;
+	u32 src_axi_sel;
+	u32 code_option;
+	u32 param;
+	u32 longterm_pic;
+	u32 prefix_sei_nal_addr;
+	u32 prefix_sei_info;
+	u32 suffix_sei_nal_addr;
+	u32 suffix_sei_info;
+	u32 timestamp_low;
+	u32 timestamp_high;
+	u32 csc_coeff[MAX_CSC_COEFF_NUM];
+};
+
+static void wave6_gen_enc_pic_reg(struct enc_info *p_enc_info, bool cbcr_interleave, bool nv21,
+				  struct enc_param *opt, struct enc_cmd_enc_pic_reg *reg)
+{
+	struct enc_open_param open = p_enc_info->open_param;
+	struct enc_codec_param param = open.codec_param;
+	bool is_lsb = false;
+	bool is_10bit = false;
+	bool is_3p4b = false;
+	bool is_cr_first = nv21;
+	u32 c_fmt_idc = get_chroma_format_idc(open.src_format);
+	u32 stride_c = opt->source_frame->stride_c;
+	bool is_ayuv = false;
+	bool is_csc_format = false;
+	bool is_24bit = false;
+	bool is_packed = false;
+	bool is_packed_uv_first = false;
+
+	if ((c_fmt_idc == C_FMT_IDC_YUV422 || c_fmt_idc == C_FMT_IDC_YUV444) &&
+	    p_enc_info->c_fmt_idc == C_FMT_IDC_YUV420)
+		stride_c *= 2;
+
+	switch (open.src_format) {
+	case FORMAT_420:
+	case FORMAT_422:
+	case FORMAT_444:
+	case FORMAT_400:
+	case FORMAT_YUYV:
+	case FORMAT_YVYU:
+	case FORMAT_UYVY:
+	case FORMAT_VYUY:
+		is_lsb = false;
+		is_3p4b = false;
+		break;
+	case FORMAT_420_P10_16BIT_MSB:
+	case FORMAT_422_P10_16BIT_MSB:
+	case FORMAT_444_P10_16BIT_MSB:
+	case FORMAT_400_P10_16BIT_MSB:
+	case FORMAT_YUYV_P10_16BIT_MSB:
+	case FORMAT_YVYU_P10_16BIT_MSB:
+	case FORMAT_UYVY_P10_16BIT_MSB:
+	case FORMAT_VYUY_P10_16BIT_MSB:
+		is_lsb = false;
+		is_10bit = true;
+		is_3p4b = false;
+		break;
+	case FORMAT_420_P10_16BIT_LSB:
+	case FORMAT_422_P10_16BIT_LSB:
+	case FORMAT_444_P10_16BIT_LSB:
+	case FORMAT_400_P10_16BIT_LSB:
+	case FORMAT_YUYV_P10_16BIT_LSB:
+	case FORMAT_YVYU_P10_16BIT_LSB:
+	case FORMAT_UYVY_P10_16BIT_LSB:
+	case FORMAT_VYUY_P10_16BIT_LSB:
+		is_lsb = true;
+		is_10bit = true;
+		is_3p4b = false;
+		break;
+	case FORMAT_420_P10_32BIT_MSB:
+	case FORMAT_422_P10_32BIT_MSB:
+	case FORMAT_444_P10_32BIT_MSB:
+	case FORMAT_400_P10_32BIT_MSB:
+	case FORMAT_YUYV_P10_32BIT_MSB:
+	case FORMAT_YVYU_P10_32BIT_MSB:
+	case FORMAT_UYVY_P10_32BIT_MSB:
+	case FORMAT_VYUY_P10_32BIT_MSB:
+		is_lsb = false;
+		is_10bit = true;
+		is_3p4b = true;
+		break;
+	case FORMAT_420_P10_32BIT_LSB:
+	case FORMAT_422_P10_32BIT_LSB:
+	case FORMAT_444_P10_32BIT_LSB:
+	case FORMAT_400_P10_32BIT_LSB:
+	case FORMAT_YUYV_P10_32BIT_LSB:
+	case FORMAT_YVYU_P10_32BIT_LSB:
+	case FORMAT_UYVY_P10_32BIT_LSB:
+	case FORMAT_VYUY_P10_32BIT_LSB:
+		is_lsb = true;
+		is_10bit = true;
+		is_3p4b = true;
+		break;
+	case FORMAT_RGB_32BIT_PACKED:
+		is_ayuv = false;
+		is_csc_format = true;
+		break;
+	case FORMAT_RGB_P10_32BIT_PACKED:
+		is_ayuv = false;
+		is_csc_format = true;
+		is_10bit = true;
+		break;
+	case FORMAT_YUV444_32BIT_PACKED:
+		is_ayuv = true;
+		is_csc_format = true;
+		break;
+	case FORMAT_YUV444_P10_32BIT_PACKED:
+		is_ayuv = true;
+		is_csc_format = true;
+		is_10bit = true;
+		break;
+	case FORMAT_RGB_24BIT_PACKED:
+		is_ayuv = false;
+		is_csc_format = true;
+		is_24bit = true;
+		break;
+	case FORMAT_YUV444_24BIT_PACKED:

drivers/media/platform/chips-media/wave6/wave6-hw.c line 1770

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+		is_ayuv = true;
+		is_csc_format = true;
+		is_24bit = true;
+		break;
+	case FORMAT_YUV444_24BIT:
+		is_ayuv = true;
+		break;
+	default:
+		break;
+	}

move to small helper to this kinds of convert functions

Quoted 1,028 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +260 -0
+
+	switch (open.src_format) {
+	case FORMAT_YUYV:
+	case FORMAT_YUYV_P10_16BIT_MSB:
+	case FORMAT_YUYV_P10_16BIT_LSB:
+	case FORMAT_YUYV_P10_32BIT_MSB:
+	case FORMAT_YUYV_P10_32BIT_LSB:
+		is_packed = true;
+		break;
+	case FORMAT_YVYU:
+	case FORMAT_YVYU_P10_16BIT_MSB:
+	case FORMAT_YVYU_P10_16BIT_LSB:
+	case FORMAT_YVYU_P10_32BIT_MSB:
+	case FORMAT_YVYU_P10_32BIT_LSB:
+		is_packed = true;
+		is_cr_first = true;
+		break;
+	case FORMAT_UYVY:
+	case FORMAT_UYVY_P10_16BIT_MSB:
+	case FORMAT_UYVY_P10_16BIT_LSB:
+	case FORMAT_UYVY_P10_32BIT_MSB:
+	case FORMAT_UYVY_P10_32BIT_LSB:
+		is_packed = true;
+		is_packed_uv_first = true;
+		break;
+	case FORMAT_VYUY:
+	case FORMAT_VYUY_P10_16BIT_MSB:
+	case FORMAT_VYUY_P10_16BIT_LSB:
+	case FORMAT_VYUY_P10_32BIT_MSB:
+	case FORMAT_VYUY_P10_32BIT_LSB:
+		is_packed = true;
+		is_packed_uv_first = true;
+		is_cr_first = true;
+		break;
+	default:
+		break;
+	}
+
+	reg->src_fmt = ENC_PIC_SRC_FMT_C_FMT_IDC(c_fmt_idc) |
+		       ENC_PIC_SRC_FMT_CSC_24BIT(is_24bit) |
+		       ENC_PIC_SRC_FMT_CSC_AYUV(is_ayuv) |
+		       ENC_PIC_SRC_FMT_CSC_ENABLE(is_csc_format) |
+		       ENC_PIC_SRC_FMT_CSC_FMT_ORDER(opt->csc.fmt_order) |
+		       ENC_PIC_SRC_FMT_ENDIAN(open.source_endian) |
+		       ENC_PIC_SRC_FMT_10BIT_ORDER(is_lsb) |
+		       ENC_PIC_SRC_FMT_10BIT_3PIXEL_4BYTE(is_3p4b) |
+		       ENC_PIC_SRC_FMT_10BIT_ENABLE(is_10bit) |
+		       ENC_PIC_SRC_FMT_YUV422_PACKED_ORDER(is_packed_uv_first) |
+		       ENC_PIC_SRC_FMT_CBCR_ORDER(is_cr_first) |
+		       ENC_PIC_SRC_FMT_2PLANE(cbcr_interleave) |
+		       ENC_PIC_SRC_FMT_YUV422_PACKED(is_packed);
+	reg->bs_start = opt->pic_stream_buffer_addr;
+	reg->bs_size = opt->pic_stream_buffer_size;
+	reg->sec_axi = ENC_PIC_SEC_AXI_RDO_ENABLE(p_enc_info->sec_axi.use_enc_rdo) |
+		       ENC_PIC_SEC_AXI_LF_ENABLE(p_enc_info->sec_axi.use_enc_lf);
+	reg->report = ENC_PIC_REPORT_MV_HISTO_ENABLE(param.en_report_mv_histo);
+	reg->mv_histo0 = ENC_PIC_MV_HISTO0_THRESHOLD0(param.mv_histo_th0) |
+			 ENC_PIC_MV_HISTO0_THRESHOLD1(param.mv_histo_th1);
+	reg->mv_histo1 = ENC_PIC_MV_HISTO1_THRESHOLD2(param.mv_histo_th2) |
+			 ENC_PIC_MV_HISTO1_THRESHOLD3(param.mv_histo_th3);
+	reg->src_pic_idx = (opt->src_end) ? ENC_PIC_SRC_PIC_IDX_END : opt->src_idx;
+	reg->src_addr_y = opt->source_frame->buf_y;
+	reg->src_addr_u = opt->source_frame->buf_cb;
+	reg->src_addr_v = opt->source_frame->buf_cr;
+	reg->src_stride = ENC_PIC_SRC_STRIDE_L(opt->source_frame->stride) |
+			  ENC_PIC_SRC_STRIDE_C(stride_c);
+	reg->src_axi_sel = ENC_PIC_SRC_AXI_SEL_PRP_PORT;
+	reg->code_option = ENC_PIC_CODE_OPTION_ENCODE_VCL |
+			   ENC_PIC_CODE_OPTION_ENCODE_HEADER;
+	reg->param = ENC_PIC_PARAM_INTRA_4X4(param.intra_4x4) |
+		     ENC_PIC_PARAM_FORCE_PIC_TYPE(opt->force_pic_type) |
+		     ENC_PIC_PARAM_FORCE_PIC_TYPE_ENABLE(opt->force_pic) |
+		     ENC_PIC_PARAM_FORCE_PIC_QP_B(opt->force_pic_qp_b) |
+		     ENC_PIC_PARAM_FORCE_PIC_QP_P(opt->force_pic_qp_p) |
+		     ENC_PIC_PARAM_FORCE_PIC_QP_I(opt->force_pic_qp_i) |
+		     ENC_PIC_PARAM_FORCE_PIC_QP_ENABLE(opt->force_pic_qp) |
+		     ENC_PIC_PARAM_PIC_SKIP_FLAG(opt->skip_picture);
+	reg->timestamp_low = lower_32_bits(opt->timestamp);
+	reg->timestamp_high = upper_32_bits(opt->timestamp);
+	reg->csc_coeff[0] = ENC_PIC_CSC_COEFF0_RY(opt->csc.coef_ry) |
+			    ENC_PIC_CSC_COEFF0_GY(opt->csc.coef_gy) |
+			    ENC_PIC_CSC_COEFF0_BY(opt->csc.coef_by);
+	reg->csc_coeff[1] = ENC_PIC_CSC_COEFF1_RCB(opt->csc.coef_rcb) |
+			    ENC_PIC_CSC_COEFF1_GCB(opt->csc.coef_gcb) |
+			    ENC_PIC_CSC_COEFF1_BCB(opt->csc.coef_bcb);
+	reg->csc_coeff[2] = ENC_PIC_CSC_COEFF2_RCR(opt->csc.coef_rcr) |
+			    ENC_PIC_CSC_COEFF2_GCR(opt->csc.coef_gcr) |
+			    ENC_PIC_CSC_COEFF2_BCR(opt->csc.coef_bcr);
+	reg->csc_coeff[3] = ENC_PIC_CSC_COEFF3_OFFSET_Y(opt->csc.offset_y) |
+			    ENC_PIC_CSC_COEFF3_OFFSET_CB(opt->csc.offset_cb) |
+			    ENC_PIC_CSC_COEFF3_OFFSET_CR(opt->csc.offset_cr);
+}
+
+int wave6_vpu_enc_start_one_frame(struct vpu_instance *inst, struct enc_param *param,
+				  u32 *fail_res)
+{
+	struct enc_cmd_enc_pic_reg reg;
+	struct enc_info *p_enc_info;
+	int ret;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_enc_info = &inst->codec_info->enc_info;
+
+	memset(&reg, 0, sizeof(struct enc_cmd_enc_pic_reg));
+
+	wave6_gen_enc_pic_reg(p_enc_info, inst->cbcr_interleave,
+			      inst->nv21, param, &reg);
+
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_BS_START, reg.bs_start);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_BS_SIZE, reg.bs_size);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_BS_OPTION, reg.bs_option);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SEC_AXI, reg.sec_axi);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_REPORT, reg.report);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_MV_HISTO0, reg.mv_histo0);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_MV_HISTO1, reg.mv_histo1);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CUSTOM_MAP_PARAM, reg.custom_map_param);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CUSTOM_MAP_ADDR, reg.custom_map_addr);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_PIC_IDX, reg.src_pic_idx);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_ADDR_Y, reg.src_addr_y);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_ADDR_U, reg.src_addr_u);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_ADDR_V, reg.src_addr_v);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_STRIDE, reg.src_stride);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_FMT, reg.src_fmt);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SRC_AXI_SEL, reg.src_axi_sel);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CODE_OPTION, reg.code_option);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_PARAM, reg.param);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_LONGTERM_PIC, reg.longterm_pic);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_PREFIX_SEI_NAL_ADDR, reg.prefix_sei_nal_addr);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_PREFIX_SEI_INFO, reg.prefix_sei_info);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SUFFIX_SEI_NAL_ADDR, reg.suffix_sei_nal_addr);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_SUFFIX_SEI_INFO, reg.suffix_sei_info);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_TIMESTAMP_LOW, reg.timestamp_low);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_TIMESTAMP_HIGH, reg.timestamp_high);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CSC_COEFF0, reg.csc_coeff[0]);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CSC_COEFF1, reg.csc_coeff[1]);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CSC_COEFF2, reg.csc_coeff[2]);
+	vpu_write_reg(inst->dev, W6_CMD_ENC_PIC_CSC_COEFF3, reg.csc_coeff[3]);
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_ENC_PIC);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return -ETIMEDOUT;
+	}
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS)) {
+		*fail_res = vpu_read_reg(inst->dev, W6_RET_FAIL_REASON);
+		wave6_print_reg_err(inst->dev, *fail_res);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int wave6_vpu_enc_get_output_info(struct vpu_instance *inst, struct enc_output_info *info)
+{
+	u32 reg_val;
+	int ret;
+
+	if (WARN_ON(!info))
+		return -EINVAL;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	ret = wave6_send_query(inst->dev, inst->id, inst->std, W6_QUERY_OPT_GET_RESULT);
+	if (ret)
+		return ret;
+
+	info->encoding_success = vpu_read_reg(inst->dev, W6_RET_ENC_ENCODING_SUCCESS);
+	if (!info->encoding_success)
+		info->error_reason = vpu_read_reg(inst->dev, W6_RET_ENC_ERR_INFO);
+	else
+		info->warn_info = vpu_read_reg(inst->dev, W6_RET_ENC_WARN_INFO);
+
+	info->enc_pic_cnt = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_NUM);
+	info->pic_type = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_TYPE);
+	info->enc_vcl_nut = vpu_read_reg(inst->dev, W6_RET_ENC_VCL_NUT);
+	info->non_ref_pic = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_NON_REF_PIC_FLAG);
+	info->num_of_slices = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_SLICE_NUM);
+	info->pic_skipped = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_SKIP);
+	info->num_of_intra = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_NUM_INTRA);
+	info->num_of_merge = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_NUM_MERGE);
+	info->num_of_skip_block = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_NUM_SKIP);
+	info->avg_ctu_qp = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_AVG_CTU_QP);
+	info->enc_pic_byte = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_BYTE);
+	info->enc_gop_pic_idx = vpu_read_reg(inst->dev, W6_RET_ENC_GOP_PIC_IDX);
+	info->enc_pic_poc = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_POC);
+	info->enc_src_idx = vpu_read_reg(inst->dev, W6_RET_ENC_USED_SRC_IDX);
+	info->wr_ptr = vpu_read_reg(inst->dev, W6_RET_ENC_WR_PTR);
+	info->rd_ptr = vpu_read_reg(inst->dev, W6_RET_ENC_RD_PTR);
+	info->bitstream_buffer = vpu_read_reg(inst->dev, W6_RET_ENC_RD_PTR);
+	info->pic_distortion_low = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_DIST_LOW);
+	info->pic_distortion_high = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_DIST_HIGH);
+	info->mv_histo.cnt0 = vpu_read_reg(inst->dev, W6_RET_ENC_HISTO_CNT0);
+	info->mv_histo.cnt1 = vpu_read_reg(inst->dev, W6_RET_ENC_HISTO_CNT1);
+	info->mv_histo.cnt2 = vpu_read_reg(inst->dev, W6_RET_ENC_HISTO_CNT2);
+	info->mv_histo.cnt3 = vpu_read_reg(inst->dev, W6_RET_ENC_HISTO_CNT3);
+	info->mv_histo.cnt4 = vpu_read_reg(inst->dev, W6_RET_ENC_HISTO_CNT4);
+	info->fme_sum.lower_x0 = vpu_read_reg(inst->dev, W6_RET_ENC_SUM_ME0_X_DIR_LOWER);
+	info->fme_sum.higher_x0 = vpu_read_reg(inst->dev, W6_RET_ENC_SUM_ME0_X_DIR_HIGHER);
+	info->fme_sum.lower_y0 = vpu_read_reg(inst->dev, W6_RET_ENC_SUM_ME0_Y_DIR_LOWER);
+	info->fme_sum.higher_y0 = vpu_read_reg(inst->dev, W6_RET_ENC_SUM_ME0_Y_DIR_HIGHER);
+	info->fme_sum.lower_x1 = vpu_read_reg(inst->dev, W6_RET_ENC_SUM_ME1_X_DIR_LOWER);
+	info->fme_sum.higher_x1 = vpu_read_reg(inst->dev, W6_RET_ENC_SUM_ME1_X_DIR_HIGHER);
+	info->fme_sum.lower_y1 = vpu_read_reg(inst->dev, W6_RET_ENC_SUM_ME1_Y_DIR_LOWER);
+	info->fme_sum.higher_y1 = vpu_read_reg(inst->dev, W6_RET_ENC_SUM_ME1_Y_DIR_HIGHER);
+	info->src_y_addr = vpu_read_reg(inst->dev, W6_RET_ENC_SRC_Y_ADDR);
+	info->custom_map_addr = vpu_read_reg(inst->dev, W6_RET_ENC_CUSTOM_MAP_ADDR);
+	info->prefix_sei_nal_addr = vpu_read_reg(inst->dev, W6_RET_ENC_PREFIX_SEI_NAL_ADDR);
+	info->suffix_sei_nal_addr = vpu_read_reg(inst->dev, W6_RET_ENC_SUFFIX_SEI_NAL_ADDR);
+
+	info->recon_frame_index = vpu_read_reg(inst->dev, W6_RET_ENC_PIC_IDX);
+	if (info->recon_frame_index == RECON_IDX_FLAG_HEADER_ONLY)
+		info->bitstream_size = info->enc_pic_byte;
+	else if (info->recon_frame_index < 0)
+		info->bitstream_size = 0;
+	else
+		info->bitstream_size = info->enc_pic_byte;
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_ENC_TIMESTAMP_LOW);
+	info->timestamp = vpu_read_reg(inst->dev, W6_RET_ENC_TIMESTAMP_HIGH);
+	info->timestamp = (info->timestamp << 32) | reg_val;
+
+	info->cycle.host_cmd_s = vpu_read_reg(inst->dev, W6_RET_CQ_IN_TICK);
+	info->cycle.host_cmd_e = vpu_read_reg(inst->dev, W6_RET_RQ_OUT_TICK);
+	info->cycle.proc_s = vpu_read_reg(inst->dev, W6_RET_HW_RUN_TICK);
+	info->cycle.proc_e = vpu_read_reg(inst->dev, W6_RET_HW_DONE_TICK);
+	info->cycle.vpu_s = vpu_read_reg(inst->dev, W6_RET_FW_RUN_TICK);
+	info->cycle.vpu_e = vpu_read_reg(inst->dev, W6_RET_FW_DONE_TICK);
+	info->cycle.frame_cycle = (info->cycle.vpu_e - info->cycle.host_cmd_s) *
+				  CYCLE_PER_TICK_VALUE;
+	info->cycle.proc_cycle = (info->cycle.proc_e - info->cycle.proc_s) *
+				 CYCLE_PER_TICK_VALUE;
+	info->cycle.vpu_cycle = ((info->cycle.vpu_e - info->cycle.vpu_s) -
+				 (info->cycle.proc_e - info->cycle.proc_s)) *
+				 CYCLE_PER_TICK_VALUE;
+
+	return 0;
+}
+
+int wave6_vpu_enc_fini_seq(struct vpu_instance *inst, u32 *fail_res)
+{
+	int ret;
+
+	lockdep_assert_held(&inst->dev->hw_lock);
+
+	wave6_send_command(inst->dev, inst->id, inst->std, W6_CMD_DESTROY_INSTANCE);
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret)
+		return -ETIMEDOUT;
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS)) {
+		*fail_res = vpu_read_reg(inst->dev, W6_RET_FAIL_REASON);
+		wave6_print_reg_err(inst->dev, *fail_res);
+		return -EIO;
+	}
+
+	return 0;
+}
drivers/media/platform/chips-media/wave6/wave6-hw.h new file +56 -0
new file mode 100644
index 000000000000..956404e36b96
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
+/*
+ * Wave6 series multi-standard codec IP - wave6 backend interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#ifndef __WAVE6_HW_H__
+#define __WAVE6_HW_H__
+
+enum product_id {
+	PRODUCT_ID_617,
+	PRODUCT_ID_627,
+	PRODUCT_ID_637,
+	PRODUCT_ID_NONE,
+};
+
+#define COMMAND_QUEUE_DEPTH		1
+#define SINGLE_CORE_IDC			1
+#define SINGLE_CORE			1
+
+#define DECODE_ALL_TEMPORAL_LAYERS	0
+#define DECODE_ALL_SPATIAL_LAYERS	0
+
+#define PIXEL_ORDER_INCREASING		1
+
+#define WTL_RIGHT_JUSTIFIED		0
+#define WTL_LEFT_JUSTIFIED		1
+#define WTL_PIXEL_8BIT			0
+#define WTL_PIXEL_16BIT			1
+#define WTL_PIXEL_32BIT			2
+
+#define MAX_CSC_COEFF_NUM		4
+
+#define CYCLE_PER_TICK_VALUE		256
+
+#define H265_NAL_UNIT_TYPE_IDR_W_RADL	19
+#define H265_NAL_UNIT_TYPE_IDR_N_LP	20
+
+#define H264_NAL_UNIT_TYPE_IDR_PICTURE	5
+
+bool wave6_vpu_is_init(struct vpu_core_device *core);
+int wave6_vpu_get_version(struct vpu_core_device *core);
+void wave6_vpu_enable_interrupt(struct vpu_core_device *core);
+
+int wave6_vpu_build_up_dec_param(struct vpu_instance *inst, struct dec_open_param *param);
+int wave6_vpu_dec_get_seq_info(struct vpu_instance *inst, struct dec_seq_info *info);
+int wave6_vpu_dec_fini_seq(struct vpu_instance *inst, u32 *fail_res);
+dma_addr_t wave6_vpu_dec_get_rd_ptr(struct vpu_instance *inst);
+
+int wave6_vpu_build_up_enc_param(struct device *dev, struct vpu_instance *inst,
+				 struct enc_open_param *param);
+int wave6_vpu_enc_get_seq_info(struct vpu_instance *inst, struct enc_seq_info *info);
+int wave6_vpu_enc_fini_seq(struct vpu_instance *inst, u32 *fail_res);
+
+#endif /* __WAVE6_HW_H__ */
drivers/media/platform/chips-media/wave6/wave6-regdefine.h new file +649 -0
new file mode 100644
index 000000000000..1d495145bbbd
@@ -0,0 +1,649 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
+/*
+ * Wave6 series multi-standard codec IP - wave6 register definitions
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#ifndef __WAVE6_REGDEFINE_H__
+#define __WAVE6_REGDEFINE_H__
+
+#include <linux/bitfield.h>
+
+enum wave6_command {
+	W6_CMD_INIT_VPU		= 0x0001,
+	W6_CMD_WAKEUP_VPU	= 0x0002,
+	W6_CMD_SLEEP_VPU	= 0x0004,
+	W6_CMD_CREATE_INSTANCE	= 0x0008,
+	W6_CMD_FLUSH_INSTANCE	= 0x0010,
+	W6_CMD_DESTROY_INSTANCE	= 0x0020,
+	W6_CMD_INIT_SEQ		= 0x0040,
+	W6_CMD_SET_FB		= 0x0080,
+	W6_CMD_DEC_PIC		= 0x0100,
+	W6_CMD_ENC_PIC		= 0x0100,
+	W6_CMD_ENC_SET_PARAM	= 0x0200,
+	W6_CMD_DEC_SET_DISP	= 0x0400,
+	W6_CMD_INIT_WORK_BUF	= 0x1000,
+	W6_CMD_QUERY		= 0x4000,
+};
+
+enum wave6_init_seq_option {
+	W6_INIT_SEQ_OPT_NORMAL		= 1,
+	W6_INIT_SEQ_OPT_W_THUMBNAIL	= 17,
+};
+
+enum wave6_set_param_option {
+	W6_SET_PARAM_OPT_COMMON		= 0,
+	W6_SET_PARAM_OPT_CHANGE_PARAM	= 1,
+};
+
+enum wave6_dec_pic_option {
+	W6_DEC_PIC_OPT_NORMAL		= 0,
+	W6_DEC_PIC_OPT_W_THUMBNAIL	= 16,
+	W6_DEC_PIC_OPT_SKIP_NON_IRAP	= 17,
+	W6_DEC_PIC_OPT_SKIP_NON_REF_PIC	= 19,
+};
+
+enum wave6_query_option {
+	W6_QUERY_OPT_GET_VPU_INFO	= 0,
+	W6_QUERY_OPT_GET_RESULT		= 2,
+	W6_QUERY_OPT_GET_FLUSH_CMD_INFO	= 10,
+};
+
+enum wave6_interrupt_bit {
+	W6_INT_BIT_INIT_VPU		= 0,
+	W6_INT_BIT_WAKEUP_VPU		= 1,
+	W6_INT_BIT_SLEEP_VPU		= 2,
+	W6_INT_BIT_CREATE_INSTANCE	= 3,
+	W6_INT_BIT_FLUSH_INSTANCE	= 4,
+	W6_INT_BIT_DESTROY_INSTANCE	= 5,
+	W6_INT_BIT_INIT_SEQ		= 6,
+	W6_INT_BIT_SET_FB		= 7,
+	W6_INT_BIT_DEC_PIC		= 8,
+	W6_INT_BIT_ENC_PIC		= 8,
+	W6_INT_BIT_ENC_SET_PARAM	= 9,
+	W6_INT_BIT_SET_DISP		= 10,
+	W6_INT_BIT_REQ_WORK_BUF		= 12,
+	W6_INT_BIT_BSBUF_ERROR		= 15,
+};
+
+#define W6_REG_BASE	0x00000000
+#define W6_CMD_REG_BASE	0x00000200
+#define W6_CMD_REG_END	0x00000600
+
+#define W6_VPU_VCPU_CUR_PC				(W6_REG_BASE + 0x0004)
+#define W6_VPU_VINT_REASON_CLEAR			(W6_REG_BASE + 0x0034)
+#define W6_VPU_HOST_INT_REQ				(W6_REG_BASE + 0x0038)
+#define		HOST_INT_REQ_ON				BIT(0)
+#define W6_VPU_VINT_CLEAR				(W6_REG_BASE + 0x003C)
+#define		VINT_CLEAR				BIT(0)
+#define W6_VPU_VPU_INT_STS				(W6_REG_BASE + 0x0044)
+#define W6_VPU_VINT_ENABLE				(W6_REG_BASE + 0x0048)
+#define W6_VPU_VINT_REASON				(W6_REG_BASE + 0x004C)
+#define W6_VPU_REMAP_CTRL_GB				(W6_REG_BASE + 0x0060)
+#define		REMAP_CTRL_ON				BIT(31)
+#define		REMAP_CTRL_INDEX(x)			FIELD_PREP(GENMASK(15, 12), (x))
+#define		REMAP_CTRL_PAGE_SIZE_ON			BIT(11)
+#define		REMAP_CTRL_PAGE_SIZE(x)			FIELD_PREP(GENMASK(8, 0), ((x) >> 12))
+#define W6_VPU_REMAP_VADDR_GB				(W6_REG_BASE + 0x0064)
+#define W6_VPU_REMAP_PADDR_GB				(W6_REG_BASE + 0x0068)
+#define W6_VPU_REMAP_CORE_START_GB			(W6_REG_BASE + 0x006C)
+#define		REMAP_CORE_START_ON			BIT(0)
+#define W6_VPU_BUSY_STATUS				(W6_REG_BASE + 0x0070)
+#define		BUSY_STATUS_SET				BIT(0)
+#define W6_VPU_RET_PRODUCT_CODE				(W6_REG_BASE + 0x0094)
+
+/* COMMON */
+#define W6_COMMAND_GB					(W6_REG_BASE + 0x104)
+#define W6_COMMAND					(W6_REG_BASE + 0x200)
+#define W6_QUERY_OPTION					(W6_REG_BASE + 0x204)
+#define W6_CMD_INSTANCE_INFO				(W6_REG_BASE + 0x210)
+#define		INSTANCE_INFO_CODEC_STD(x)		FIELD_PREP(GENMASK(31, 16), (x))
+#define		INSTANCE_INFO_ID(x)			FIELD_PREP(GENMASK(15, 0), (x))
+#define W6_CMD_INIT_VPU_SEC_AXI_BASE_CORE0		(W6_REG_BASE + 0x364)
+#define W6_CMD_INIT_VPU_SEC_AXI_SIZE_CORE0		(W6_REG_BASE + 0x368)
+#define W6_CMD_SET_WORK_BUF_ADDR			(W6_REG_BASE + 0x5F0)
+#define W6_CMD_SET_WORK_BUF_SIZE			(W6_REG_BASE + 0x5F4)
+#define		SET_WORK_BUF_SIZE_ACK			0
+#define W6_RET_SUCCESS					(W6_REG_BASE + 0x208)
+#define W6_RET_FAIL_REASON				(W6_REG_BASE + 0x20C)
+#define W6_RET_INT_INSTANCE_INFO			(W6_REG_BASE + 0x21C)
+#define		INT_INSTANCE_INFO_CLEAR			0
+#define W6_RET_INSTANCE_ID				(W6_REG_BASE + 0x220)
+#define W6_RET_CQ_IN_TICK				(W6_REG_BASE + 0x23C)
+#define W6_RET_FW_RUN_TICK				(W6_REG_BASE + 0x240)
+#define W6_RET_HW_RUN_TICK				(W6_REG_BASE + 0x244)
+#define W6_RET_HW_DONE_TICK				(W6_REG_BASE + 0x248)
+#define W6_RET_FW_DONE_TICK				(W6_REG_BASE + 0x24C)
+#define W6_RET_RQ_OUT_TICK				(W6_REG_BASE + 0x250)
+
+/* COMMON - QUERY : GET_VPU_INFO */
+#define W6_RET_FW_VERSION				(W6_REG_BASE + 0x300)
+#define W6_RET_PRODUCT_NAME				(W6_REG_BASE + 0x304)
+#define W6_RET_PRODUCT_VERSION				(W6_REG_BASE + 0x308)
+#define W6_RET_STD_DEF0					(W6_REG_BASE + 0x30C)
+#define W6_RET_STD_DEF1					(W6_REG_BASE + 0x310)
+#define		STD_DEF1_AVC_DEC(x)			FIELD_GET(GENMASK(3, 3), (x))
+#define		STD_DEF1_HEVC_DEC(x)			FIELD_GET(GENMASK(2, 2), (x))
+#define		STD_DEF1_AVC_ENC(x)			FIELD_GET(GENMASK(1, 1), (x))
+#define		STD_DEF1_HEVC_ENC(x)			FIELD_GET(GENMASK(0, 0), (x))
+#define W6_RET_CONF_FEATURE				(W6_REG_BASE + 0x314)
+#define		CONF_FEATURE_AVC10BIT_ENC(x)		FIELD_GET(GENMASK(11, 11), (x))
+#define		CONF_FEATURE_AVC10BIT_DEC(x)		FIELD_GET(GENMASK(9, 9), (x))
+#define		CONF_FEATURE_HEVC10BIT_ENC(x)		FIELD_GET(GENMASK(3, 3), (x))
+#define		CONF_FEATURE_HEVC10BIT_DEC(x)		FIELD_GET(GENMASK(1, 1), (x))
+#define W6_RET_CONF_DATE				(W6_REG_BASE + 0x318)
+#define W6_RET_CONF_HW_VERSION				(W6_REG_BASE + 0x31C)
+#define W6_RET_CONF_TYPE				(W6_REG_BASE + 0x320)
+#define W6_RET_FW_API_VERSION				(W6_REG_BASE + 0x32C)
+#define W6_RET_SHA_ID					(W6_REG_BASE + 0x330)
+
+/* COMMON - CREATE_INSTANCE */
+#define W6_CMD_CREATE_INST_BS_PARAM			(W6_REG_BASE + 0x310)
+#define W6_CMD_CREATE_INST_SRC_OPT			(W6_REG_BASE + 0x314)
+#define W6_CMD_CREATE_INST_ADDR_EXT			(W6_REG_BASE + 0x318)
+#define W6_CMD_CREATE_INST_DISP_MODE			(W6_REG_BASE + 0x31C)
+#define W6_CMD_CREATE_INST_CORE_INFO			(W6_REG_BASE + 0x330)
+#define		CREATE_INST_CORE_INFO_CQ_DEPTH(x)	FIELD_PREP(GENMASK(10, 8), (x))
+#define		CREATE_INST_CORE_INFO_CORE_IDC(x)	FIELD_PREP(GENMASK(7, 4), (x))
+#define		CREATE_INST_CORE_INFO_CORE_NUM(x)	FIELD_PREP(GENMASK(3, 0), (x))
+#define W6_CMD_CREATE_INST_PRIORITY			(W6_REG_BASE + 0x334)
+#define		CREATE_INST_PRIORITY_SECURITY_FLAG(x)	FIELD_PREP(GENMASK(8, 8), (x))
+#define		CREATE_INST_PRIORITY_VALUE(x)		FIELD_PREP(GENMASK(4, 0), (x))
+#define W6_CMD_CREATE_INST_TEMP_BASE			(W6_REG_BASE + 0x348)
+#define W6_CMD_CREATE_INST_TEMP_SIZE			(W6_REG_BASE + 0x34C)
+#define W6_CMD_CREATE_INST_AR_TABLE_BASE		(W6_REG_BASE + 0x358)
+#define W6_CMD_CREATE_INST_TIMEOUT_CYCLE_COUNT		(W6_REG_BASE + 0x3A8)
+
+/* COMMON - SET_FB */
+#define W6_CMD_SET_FB_OPTION				(W6_REG_BASE + 0x204)
+#define		SET_FB_OPTION_ENDIAN(x)			FIELD_PREP(GENMASK(19, 16), (x))
+#define		SET_FB_OPTION_END			BIT(4)
+#define		SET_FB_OPTION_START			BIT(3)
+#define W6_CMD_SET_FB_PIC_INFO				(W6_REG_BASE + 0x300)
+#define		SET_FB_PIC_INFO_C_FMT_IDC(x)		FIELD_PREP(GENMASK(26, 25), (x))
+#define		SET_FB_PIC_INFO_L_BIT_DEPTH(x)		FIELD_PREP(GENMASK(24, 21), (x))
+#define		SET_FB_PIC_INFO_C_BIT_DEPTH(x)		FIELD_PREP(GENMASK(20, 17), (x))
+#define		SET_FB_PIC_INFO_STRIDE(x)		FIELD_PREP(GENMASK(15, 0), (x))
+#define W6_CMD_SET_FB_PIC_SIZE				(W6_REG_BASE + 0x304)
+#define		SET_FB_PIC_SIZE_WIDTH(x)		FIELD_PREP(GENMASK(31, 16), (x))
+#define		SET_FB_PIC_SIZE_HEIGHT(x)		FIELD_PREP(GENMASK(15, 0), (x))
+#define W6_CMD_SET_FB_NUM				(W6_REG_BASE + 0x308)
+#define		SET_FB_NUM_FBC_START_IDX(x)		FIELD_PREP(GENMASK(28, 24), (x))
+#define		SET_FB_NUM_FBC_END_IDX(x)		FIELD_PREP(GENMASK(20, 16), (x))
+#define		SET_FB_NUM_MV_COL_START_IDX(x)		FIELD_PREP(GENMASK(9, 5), (x))
+#define		SET_FB_NUM_MV_COL_END_IDX(x)		FIELD_PREP(GENMASK(4, 0), (x))
+#define W6_CMD_SET_FB_FBC_STRIDE			(W6_REG_BASE + 0x30C)
+#define		SET_FB_FBC_STRIDE_L(x)			FIELD_PREP(GENMASK(31, 16), (x))
+#define		SET_FB_FBC_STRIDE_C(x)			FIELD_PREP(GENMASK(15, 0), (x))
+
+/* W6_CMD_SET_FB_FBC_Y(x)
+ * Address range: 0x310, 0x328, ..., 0x478 (step = 0x18)
+ */
+#define W6_CMD_SET_FB_FBC_Y(x)				(W6_REG_BASE + 0x310 + ((x) * 0x18))
+
+/* W6_CMD_SET_FB_FBC_C(x)
+ * Address range: 0x314, 0x32C, ..., 0x47C (step = 0x18)
+ */
+#define W6_CMD_SET_FB_FBC_C(x)				(W6_REG_BASE + 0x314 + ((x) * 0x18))
+
+/* W6_CMD_SET_FB_FBC_Y_OFFSET(x)
+ * Address range: 0x318, 0x330, ..., 0x480 (step = 0x18)
+ */
+#define W6_CMD_SET_FB_FBC_Y_OFFSET(x)			(W6_REG_BASE + 0x318 + ((x) * 0x18))
+
+/* W6_CMD_SET_FB_FBC_C_OFFSET(x)
+ * Address range: 0x31C, 0x334, ..., 0x484 (step = 0x18)
+ */
+#define W6_CMD_SET_FB_FBC_C_OFFSET(x)			(W6_REG_BASE + 0x31C + ((x) * 0x18))
+
+/* W6_CMD_SET_FB_MV_COL(x)
+ * Address range: 0x320, 0x338, ..., 0x488 (step = 0x18)
+ */
+#define W6_CMD_SET_FB_MV_COL(x)				(W6_REG_BASE + 0x320 + ((x) * 0x18))
+
+/* W6_CMD_SET_FB_SUB_SAMPLED(x)
+ * Address range: 0x324, 0x33C, ..., 0x48C (step = 0x18)
+ */
+#define W6_CMD_SET_FB_SUB_SAMPLED(x)			(W6_REG_BASE + 0x324 + ((x) * 0x18))
+#define W6_CMD_SET_FB_DEFAULT_CDF			(W6_REG_BASE + 0x494)
+#define W6_CMD_SET_FB_SEGMAP				(W6_REG_BASE + 0x498)
+#define W6_CMD_SET_FB_MV_COL_PRE_ENT			(W6_REG_BASE + 0x4DC)
+
+/* W6_CMD_SET_FB_FBC_CR(x)
+ * Address range: 0x4F0, 0x4F8, ..., 0x568 (step = 0x8)
+ */
+#define W6_CMD_SET_FB_FBC_CR(x)				(W6_REG_BASE + 0x4F0 + ((x) * 0x8))
+
+/* W6_CMD_SET_FB_FBC_CR_OFFSET(x)
+ * Address range: 0x4F4, 0x4FC, ..., 0x56C (step = 0x8)
+ */
+#define W6_CMD_SET_FB_FBC_CR_OFFSET(x)			(W6_REG_BASE + 0x4F4 + ((x) * 0x8))
+
+/* DECODER - INIT_SEQ */
+#define W6_CMD_DEC_INIT_SEQ_OPTION			(W6_REG_BASE + 0x204)
+#define		INIT_SEQ_OPTION_MODE(x)			FIELD_PREP(GENMASK(5, 0), (x))
+#define W6_CMD_DEC_INIT_SEQ_BS_RD_PTR			(W6_REG_BASE + 0x300)
+#define W6_CMD_DEC_INIT_SEQ_BS_WR_PTR			(W6_REG_BASE + 0x304)
+#define W6_CMD_DEC_INIT_SEQ_BS_OPTION			(W6_REG_BASE + 0x308)
+
+/* DECODER - SET_DISP */
+#define W6_CMD_DEC_SET_DISP_OPTION			(W6_REG_BASE + 0x204)
+#define		SET_DISP_OPTION_ENDIAN(x)		FIELD_PREP(GENMASK(19, 16), (x))
+#define W6_CMD_DEC_SET_DISP_COMMON_PIC_INFO		(W6_REG_BASE + 0x300)
+#define		SET_DISP_COMMON_PIC_INFO_BWB_ON		BIT(28)
+#define		SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(x)	FIELD_PREP(GENMASK(25, 24), (x))
+#define		SET_DISP_COMMON_PIC_INFO_PIXEL_ORDER(x)	FIELD_PREP(GENMASK(23, 23), (x))
+#define		SET_DISP_COMMON_PIC_INFO_OUT_MODE(x)	FIELD_PREP(GENMASK(22, 20), (x))
+#define		SET_DISP_COMMON_PIC_INFO_OUT_FMT(x)	FIELD_PREP(GENMASK(18, 16), (x))
+#define		SET_DISP_COMMON_PIC_INFO_STRIDE(x)	FIELD_PREP(GENMASK(15, 0), (x))
+#define W6_CMD_DEC_SET_DISP_PIC_SIZE			(W6_REG_BASE + 0x304)
+#define		SET_DISP_PIC_SIZE_WIDTH(x)		FIELD_PREP(GENMASK(31, 16), (x))
+#define		SET_DISP_PIC_SIZE_HEIGHT(x)		FIELD_PREP(GENMASK(15, 0), (x))
+#define W6_CMD_DEC_SET_DISP_PIC_INFO			(W6_REG_BASE + 0x308)
+#define		SET_DISP_PIC_INFO_L_BIT_DEPTH(x)	FIELD_PREP(GENMASK(25, 22), (x))
+#define		SET_DISP_PIC_INFO_C_BIT_DEPTH(x)	FIELD_PREP(GENMASK(21, 18), (x))
+#define		SET_DISP_PIC_INFO_C_FMT_IDC(x)		FIELD_PREP(GENMASK(17, 16), (x))
+#define W6_CMD_DEC_SET_DISP_Y_BASE			(W6_REG_BASE + 0x30C)
+#define W6_CMD_DEC_SET_DISP_CB_BASE			(W6_REG_BASE + 0x310)
+#define W6_CMD_DEC_SET_DISP_CR_BASE			(W6_REG_BASE + 0x314)
+#define W6_CMD_DEC_SET_DISP_SCL_PARAM			(W6_REG_BASE + 0x318)
+#define W6_CMD_DEC_SET_DISP_SCL_PIC_SIZE		(W6_REG_BASE + 0x31C)
+#define		SET_DISP_SCL_PIC_SIZE_WIDTH(x)		FIELD_PREP(GENMASK(31, 16), (x))
+#define		SET_DISP_SCL_PIC_SIZE_HEIGHT(x)		FIELD_PREP(GENMASK(15, 0), (x))
+
+/* DECODER - DEC_PIC */
+#define W6_CMD_DEC_PIC_OPTION				(W6_REG_BASE + 0x204)
+#define		DEC_PIC_OPTION_MODE(x)			FIELD_PREP(GENMASK(5, 0), (x))
+#define W6_CMD_DEC_PIC_BS_RD_PTR			(W6_REG_BASE + 0x300)
+#define W6_CMD_DEC_PIC_BS_WR_PTR			(W6_REG_BASE + 0x304)
+#define W6_CMD_DEC_PIC_BS_OPTION			(W6_REG_BASE + 0x308)
+#define		DEC_PIC_BS_OPTION_STREAM_END(x)		FIELD_PREP(GENMASK(1, 1), (x))
+#define		DEC_PIC_BS_OPTION_EXPLICIT_END_ON	BIT(0)
+#define W6_CMD_DEC_PIC_SEC_AXI				(W6_REG_BASE + 0x30C)
+#define		DEC_PIC_SEC_AXI_IP_ENABLE(x)		FIELD_PREP(GENMASK(1, 1), (x))
+#define		DEC_PIC_SEC_AXI_LF_ENABLE(x)		FIELD_PREP(GENMASK(0, 0), (x))
+#define W6_CMD_DEC_PIC_SEQ_CHANGE_ENABLE_FLAG		(W6_REG_BASE + 0x310)
+#define W6_CMD_DEC_PIC_TEMPORAL_ID_PLUS1		(W6_REG_BASE + 0x318)
+#define		DEC_PIC_TEMPORAL_ID_PLUS1_SPATIAL(x)	FIELD_PREP(GENMASK(11, 9), (x))
+#define		DEC_PIC_TEMPORAL_ID_PLUS1(x)		FIELD_PREP(GENMASK(7, 0), (x))
+#define W6_CMD_DEC_PIC_TIMESTAMP_LOW			(W6_REG_BASE + 0x354)
+#define W6_CMD_DEC_PIC_TIMESTAMP_HIGH			(W6_REG_BASE + 0x358)
+
+/* DECODER - QUERY : GET_RESULT */
+#define W6_RET_DEC_BS_RD_PTR				(W6_REG_BASE + 0x30C)
+#define W6_RET_DEC_SEQ_PARAM				(W6_REG_BASE + 0x310)
+#define		DEC_SEQ_PARAM_TIER(x)			FIELD_GET(GENMASK(29, 29), (x))
+#define		DEC_SEQ_PARAM_PROFILE(x)		FIELD_GET(GENMASK(28, 24), (x))
+#define		PROFILE_H264_BP				2
+#define		PROFILE_H264_HP				4
+#define		PROFILE_H264_MP				13
+#define		PROFILE_H264_HIGH10			14
+#define		PROFILE_H264_EXTENDED			24
+#define		DEC_SEQ_PARAM_PROFILE_COMPATIBILITY(x)	FIELD_GET(GENMASK(19, 12), (x))
+#define		PROFILE_COMPATIBILITY_MAIN		BIT(1)
+#define		PROFILE_COMPATIBILITY_MAIN10		BIT(2)
+#define		PROFILE_COMPATIBILITY_STILL_PICTURE	BIT(3)
+#define		DEC_SEQ_PARAM_LEVEL(x)			FIELD_GET(GENMASK(7, 0), (x))
+#define W6_RET_DEC_COLOR_SAMPLE				(W6_REG_BASE + 0x314)
+#define		DEC_COLOR_SAMPLE_ASPECT_RATIO_IDC(x)	FIELD_GET(GENMASK(23, 16), (x))
+#define		DEC_COLOR_SAMPLE_C_FMT_IDC(x)		FIELD_GET(GENMASK(11, 8), (x))
+#define		DEC_COLOR_SAMPLE_C_BIT_DEPTH(x)		FIELD_GET(GENMASK(7, 4), (x))
+#define		DEC_COLOR_SAMPLE_L_BIT_DEPTH(x)		FIELD_GET(GENMASK(3, 0), (x))
+#define W6_RET_DEC_ASPECT_RATIO				(W6_REG_BASE + 0x318)
+#define W6_RET_DEC_BIT_RATE				(W6_REG_BASE + 0x31C)
+#define W6_RET_DEC_FRAME_RATE_NR			(W6_REG_BASE + 0x320)
+#define W6_RET_DEC_FRAME_RATE_DR			(W6_REG_BASE + 0x324)
+#define W6_RET_DEC_NUM_REQUIRED_FBC_FB			(W6_REG_BASE + 0x328)
+#define W6_RET_DEC_NUM_REORDER_DELAY			(W6_REG_BASE + 0x32C)
+#define W6_RET_DEC_NOTIFICATION				(W6_REG_BASE + 0x334)
+#define W6_RET_DEC_PIC_SIZE				(W6_REG_BASE + 0x33C)
+#define		DEC_PIC_SIZE_WIDTH(x)			FIELD_GET(GENMASK(31, 16), (x))
+#define		DEC_PIC_SIZE_HEIGHT(x)			FIELD_GET(GENMASK(15, 0), (x))
+#define W6_RET_DEC_CROP_TOP_BOTTOM			(W6_REG_BASE + 0x340)
+#define		DEC_CROP_TOP(x)				FIELD_GET(GENMASK(31, 16), (x))
+#define		DEC_CROP_BOTTOM(x)			FIELD_GET(GENMASK(15, 0), (x))
+#define W6_RET_DEC_CROP_LEFT_RIGHT			(W6_REG_BASE + 0x344)
+#define		DEC_CROP_LEFT(x)			FIELD_GET(GENMASK(31, 16), (x))
+#define		DEC_CROP_RIGHT(x)			FIELD_GET(GENMASK(15, 0), (x))
+#define W6_RET_DEC_AU_START_POS				(W6_REG_BASE + 0x348)
+#define W6_RET_DEC_AU_END_POS				(W6_REG_BASE + 0x34C)
+#define W6_RET_DEC_PIC_TYPE				(W6_REG_BASE + 0x350)
+#define		DEC_PIC_TYPE_CTU_SIZE(x)		(16 << FIELD_GET(GENMASK(11, 10), (x)))
+#define		DEC_PIC_TYPE_NAL_UNIT_TYPE(x)		FIELD_GET(GENMASK(9, 4), (x))
+#define		DEC_PIC_TYPE_B				BIT(2)
+#define		DEC_PIC_TYPE_P				BIT(1)
+#define		DEC_PIC_TYPE_I				BIT(0)
+#define W6_RET_DEC_PIC_POC				(W6_REG_BASE + 0x354)
+#define W6_RET_DEC_RECOVERY_POINT			(W6_REG_BASE + 0x358)
+#define W6_RET_DEC_DECODED_ADDR				(W6_REG_BASE + 0x360)
+#define W6_RET_DEC_DISPLAY_ADDR				(W6_REG_BASE + 0x364)
+#define W6_RET_DEC_ERR_CTB_NUM				(W6_REG_BASE + 0x370)
+#define W6_RET_DEC_DISPLAY_FLAG				(W6_REG_BASE + 0x3A8)
+#define W6_RET_DEC_RELEASE_IDC				(W6_REG_BASE + 0x3AC)
+#define W6_RET_DEC_DISP_IDC				(W6_REG_BASE + 0x3B0)
+#define W6_RET_DEC_STREAM_END				(W6_REG_BASE + 0x3C0)
+#define W6_RET_DEC_DECODED_FLAG				(W6_REG_BASE + 0x3C4)
+#define W6_RET_DEC_WARN_INFO				(W6_REG_BASE + 0x3CC)
+#define W6_RET_DEC_ERR_INFO				(W6_REG_BASE + 0x3D0)
+#define W6_RET_DEC_DECODING_SUCCESS			(W6_REG_BASE + 0x3D4)
+#define W6_RET_DEC_TIMESTAMP_LOW			(W6_REG_BASE + 0x3D8)
+#define W6_RET_DEC_TIMESTAMP_HIGH			(W6_REG_BASE + 0x3DC)
+#define W6_RET_DEC_LAST_FRAME_FLAG			(W6_REG_BASE + 0x3E0)
+#define W6_RET_DEC_NUM_REQUIRED_COL_BUF			(W6_REG_BASE + 0x3E4)
+
+/* W6_RET_DEC_DISP_LINEAR_ADDR(x)
+ * Address range: 0x3E8, 0x3EC, ..., 0x460 (step = 0x4)
+ */
+#define W6_RET_DEC_DISP_LINEAR_ADDR(x)			(W6_REG_BASE + 0x3E8 + ((x) * 0x4))
+#define W6_RET_DEC_COLOR_CONFIG				(W6_REG_BASE + 0x57C)
+#define		DEC_COLOR_CONFIG_COLOR_RANGE(x)		FIELD_GET(GENMASK(25, 25), (x))
+#define		DEC_COLOR_CONFIG_MATRIX_COEFF(x)	FIELD_GET(GENMASK(24, 17), (x))
+#define		DEC_COLOR_CONFIG_TRANS_CHAR(x)		FIELD_GET(GENMASK(16, 9), (x))
+#define		DEC_COLOR_CONFIG_COLOR_PRIMARIES(x)	FIELD_GET(GENMASK(8, 1), (x))
+#define		DEC_COLOR_CONFIG_COLOR_PRESENT(x)	FIELD_GET(GENMASK(0, 0), (x))
+
+/* DECODER - QUERY : GET_FLUSH_CMD_INFO */
+
+/* W6_RET_DEC_FLUSH_CMD_DISP_ADDR(x)
+ * Address range: 0x300, 0x304, ..., 0x378 (step = 0x4)
+ */
+#define W6_RET_DEC_FLUSH_CMD_DISP_ADDR(x)		(W6_REG_BASE + 0x300 + ((x) * 0x4))
+#define W6_RET_DEC_FLUSH_CMD_BUF_STATE_UNUSED_IDC	(W6_REG_BASE + 0x57C)
+#define W6_RET_DEC_FLUSH_CMD_BUF_STATE_USED_IDC		(W6_REG_BASE + 0x580)
+#define W6_RET_DEC_FLUSH_CMD_BUF_STATE_USING_IDC	(W6_REG_BASE + 0x584)
+
+/* ENCODER - SET_PARAM */
+#define W6_CMD_ENC_SET_PARAM_OPTION			(W6_REG_BASE + 0x204)
+#define W6_CMD_ENC_SET_PARAM_ENABLE			(W6_REG_BASE + 0x300)
+#define		SET_PARAM_ENABLE_RC_TARGET_RATE		BIT(10)
+#define W6_CMD_ENC_SET_PARAM_SRC_SIZE			(W6_REG_BASE + 0x304)
+#define		SET_PARAM_SRC_SIZE_HEIGHT(x)		FIELD_PREP(GENMASK(31, 16), (x))
+#define		SET_PARAM_SRC_SIZE_WIDTH(x)		FIELD_PREP(GENMASK(15, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_CUSTOM_MAP_ENDIAN		(W6_REG_BASE + 0x308)
+#define W6_CMD_ENC_SET_PARAM_SPS			(W6_REG_BASE + 0x30C)
+#define		SET_PARAM_SPS_DEFAULT_SCALING_LIST(x)	FIELD_PREP(GENMASK(31, 31), (x))
+#define		SET_PARAM_SPS_STILL_PICTURE(x)		FIELD_PREP(GENMASK(30, 30), (x))
+#define		SET_PARAM_SPS_AUTO_LEVEL_ADJUSTING_ON	BIT(28)
+#define		SET_PARAM_SPS_STRONG_INTRA_SMOOTHING(x)	FIELD_PREP(GENMASK(27, 27), (x))
+#define		SET_PARAM_SPS_INTRA_TRANSFORM_SKIP_ON	BIT(25)
+#define		SET_PARAM_SPS_SAMPLE_ADAPTIVE_OFFSET(x)	FIELD_PREP(GENMASK(24, 24), (x))
+#define		SET_PARAM_SPS_TEMPORAL_MVP(x)		FIELD_PREP(GENMASK(23, 23), (x))
+#define		SET_PARAM_SPS_LONGTERM_REFERENCE(x)	FIELD_PREP(GENMASK(21, 21), (x))
+#define		SET_PARAM_SPS_C_FMT_IDC(x)		FIELD_PREP(GENMASK(20, 19), (x))
+#define		SET_PARAM_SPS_BIT_DEPTH(x)		FIELD_PREP(GENMASK(18, 14), (x))
+#define		SET_PARAM_SPS_TIER(x)			FIELD_PREP(GENMASK(13, 12), (x))
+#define		SET_PARAM_SPS_LEVEL(x)			FIELD_PREP(GENMASK(11, 3), (x))
+#define		SET_PARAM_SPS_PROFILE(x)		FIELD_PREP(GENMASK(2, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_PPS			(W6_REG_BASE + 0x310)
+#define		SET_PARAM_PPS_ENTROPY_CODING_MODE(x)	FIELD_PREP(GENMASK(30, 30), (x))
+#define		SET_PARAM_PPS_TRANSFORM8X8(x)		FIELD_PREP(GENMASK(29, 29), (x))
+#define		SET_PARAM_PPS_CR_QP_OFFSET(x)		FIELD_PREP(GENMASK(23, 19), (x))
+#define		SET_PARAM_PPS_CB_QP_OFFSET(x)		FIELD_PREP(GENMASK(18, 14), (x))
+#define		SET_PARAM_PPS_TC_OFFSET_DIV2(x)		FIELD_PREP(GENMASK(13, 10), (x))
+#define		SET_PARAM_PPS_BETA_OFFSET_DIV2(x)	FIELD_PREP(GENMASK(9, 6), (x))
+#define		SET_PARAM_PPS_DEBLOCKING_FILTER(x)	FIELD_PREP(GENMASK(5, 5), (x))
+#define		SET_PARAM_PPS_LF_SLICE_BOUNDARY(x)	FIELD_PREP(GENMASK(2, 2), (x))
+#define		SET_PARAM_PPS_CONST_INTRA_PREDICTION(x)	FIELD_PREP(GENMASK(1, 1), (x))
+#define W6_CMD_ENC_SET_PARAM_GOP			(W6_REG_BASE + 0x314)
+#define		SET_PARAM_GOP_TEMP_LAYER_CNT(x)		FIELD_PREP(GENMASK(19, 16), (x))
+#define		SET_PARAM_GOP_TEMP_LAYER3_QP_ENABLE(x)	FIELD_PREP(GENMASK(11, 11), (x))
+#define		SET_PARAM_GOP_TEMP_LAYER2_QP_ENABLE(x)	FIELD_PREP(GENMASK(10, 10), (x))
+#define		SET_PARAM_GOP_TEMP_LAYER1_QP_ENABLE(x)	FIELD_PREP(GENMASK(9, 9), (x))
+#define		SET_PARAM_GOP_TEMP_LAYER0_QP_ENABLE(x)	FIELD_PREP(GENMASK(8, 8), (x))
+#define		SET_PARAM_GOP_PRESET_IDX(x)		FIELD_PREP(GENMASK(7, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_INTRA			(W6_REG_BASE + 0x318)
+#define		SET_PARAM_INTRA_PERIOD(x)		FIELD_PREP(GENMASK(31, 16), (x))
+#define		SET_PARAM_INTRA_HEADER_MODE(x)		FIELD_PREP(GENMASK(10, 9), (x))
+#define		SET_PARAM_INTRA_QP(x)			FIELD_PREP(GENMASK(8, 3), (x))
+#define		SET_PARAM_INTRA_REFRESH_TYPE(x)		FIELD_PREP(GENMASK(2, 0), (x))
+
+/* W6_CMD_ENC_SET_PARAM_INTRA for AVC specific fields */
+#define		SET_PARAM_INTRA_HEADER_MODE_AVC(x)	FIELD_PREP(GENMASK(29, 28), (x))
+#define		SET_PARAM_INTRA_IDR_PERIOD_AVC(x)	FIELD_PREP(GENMASK(27, 17), (x))
+#define		SET_PARAM_INTRA_PERIOD_AVC(x)		FIELD_PREP(GENMASK(16, 6), (x))
+#define		SET_PARAM_INTRA_QP_AVC(x)		FIELD_PREP(GENMASK(5, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_CONF_WIN0			(W6_REG_BASE + 0x31C)
+#define		SET_PARAM_CONF_WIN0_BOTTOM(x)		FIELD_PREP(GENMASK(31, 16), (x))
+#define		SET_PARAM_CONF_WIN0_TOP(x)		FIELD_PREP(GENMASK(15, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_CONF_WIN1			(W6_REG_BASE + 0x320)
+#define		SET_PARAM_CONF_WIN1_RIGHT(x)		FIELD_PREP(GENMASK(31, 16), (x))
+#define		SET_PARAM_CONF_WIN1_LEFT(x)		FIELD_PREP(GENMASK(15, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_RDO			(W6_REG_BASE + 0x324)
+#define		SET_PARAM_RDO_CUSTOM_LAMBDA(x)		FIELD_PREP(GENMASK(22, 22), (x))
+#define		SET_PARAM_RDO_ME_SEARCH_CENTER_ON	BIT(21)
+#define		SET_PARAM_RDO_QROUND_OFFSET(x)		FIELD_PREP(GENMASK(17, 17), (x))
+#define		SET_PARAM_RDO_ADAPTIVE_ROUND_ON		BIT(3)
+#define		SET_PARAM_RDO_HVS_QP(x)			FIELD_PREP(GENMASK(2, 2), (x))
+#define W6_CMD_ENC_SET_PARAM_SLICE			(W6_REG_BASE + 0x328)
+#define		SET_PARAM_SLICE_ARGUMENT(x)		FIELD_PREP(GENMASK(31, 3), (x))
+#define		SET_PARAM_SLICE_MODE(x)			FIELD_PREP(GENMASK(2, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_INTRA_REFRESH		(W6_REG_BASE + 0x32C)
+#define		SET_PARAM_INTRA_REFRESH_ARGUMENT(x)	FIELD_PREP(GENMASK(31, 16), (x))
+#define		SET_PARAM_INTRA_REFRESH_MODE(x)		FIELD_PREP(GENMASK(2, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_INTRA_QP			(W6_REG_BASE + 0x330)
+#define		SET_PARAM_INTRA_QP_MAX(x)		FIELD_PREP(GENMASK(11, 6), (x))
+#define		SET_PARAM_INTRA_QP_MIN(x)		FIELD_PREP(GENMASK(5, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_RC_FRAME_RATE		(W6_REG_BASE + 0x334)
+#define W6_CMD_ENC_SET_PARAM_RC_TARGET_RATE		(W6_REG_BASE + 0x338)
+#define W6_CMD_ENC_SET_PARAM_RC				(W6_REG_BASE + 0x33C)
+#define		SET_PARAM_RC_UPDATE_SPEED(x)		FIELD_PREP(GENMASK(31, 24), (x))
+#define		SET_PARAM_RC_INITIAL_LEVEL(x)		FIELD_PREP(GENMASK(23, 20), (x))
+#define		SET_PARAM_RC_INITIAL_QP(x)		FIELD_PREP(GENMASK(19, 14), (x))
+#define		SET_PARAM_RC_MODE(x)			FIELD_PREP(GENMASK(13, 13), (x))
+#define		SET_PARAM_RC_PIC_LEVEL_MAX_DELTA_QP(x)	FIELD_PREP(GENMASK(12, 7), (x))
+#define		SET_PARAM_RC_VBV_OVERFLOW_DROP_FRAME(x)	FIELD_PREP(GENMASK(3, 3), (x))
+#define		SET_PARAM_RC_CU_LEVEL_ENABLE(x)		FIELD_PREP(GENMASK(1, 1), (x))
+#define		SET_PARAM_RC_ENABLE(x)			FIELD_PREP(GENMASK(0, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_HVS			(W6_REG_BASE + 0x340)
+#define		SET_PARAM_HVS_MAX_DELTA_QP(x)		FIELD_PREP(GENMASK(17, 12), (x))
+#define		SET_PARAM_HVS_QP_SCALE(x)		FIELD_PREP(GENMASK(3, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_RC_MAX_BITRATE		(W6_REG_BASE + 0x344)
+#define W6_CMD_ENC_SET_PARAM_RC_VBV_BUFFER_SIZE		(W6_REG_BASE + 0x348)
+#define W6_CMD_ENC_SET_PARAM_INTER_QP			(W6_REG_BASE + 0x34C)
+#define		SET_PARAM_INTER_QP_MAX_B(x)		FIELD_PREP(GENMASK(23, 18), (x))
+#define		SET_PARAM_INTER_QP_MIN_B(x)		FIELD_PREP(GENMASK(17, 12), (x))
+#define		SET_PARAM_INTER_QP_MAX_P(x)		FIELD_PREP(GENMASK(11, 6), (x))
+#define		SET_PARAM_INTER_QP_MIN_P(x)		FIELD_PREP(GENMASK(5, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_ROT			(W6_REG_BASE + 0x350)
+#define		SET_PARAM_ROT_MIRH			BIT(4)
+#define		SET_PARAM_ROT_MIRV			BIT(3)
+#define		SET_PARAM_ROT_270			FIELD_PREP(GENMASK(2, 1), 3)
+#define		SET_PARAM_ROT_180			FIELD_PREP(GENMASK(2, 1), 2)
+#define		SET_PARAM_ROT_90			FIELD_PREP(GENMASK(2, 1), 1)
+#define		SET_PARAM_ROT_ENABLE			BIT(0)
+#define W6_CMD_ENC_SET_PARAM_NUM_UNITS_IN_TICK		(W6_REG_BASE + 0x354)
+#define W6_CMD_ENC_SET_PARAM_TIME_SCALE			(W6_REG_BASE + 0x358)
+#define W6_CMD_ENC_SET_PARAM_NUM_TICKS_POC_DIFF_ONE	(W6_REG_BASE + 0x35C)
+#define W6_CMD_ENC_SET_PARAM_MAX_INTRA_PIC_BIT		(W6_REG_BASE + 0x360)
+#define W6_CMD_ENC_SET_PARAM_MAX_INTER_PIC_BIT		(W6_REG_BASE + 0x364)
+#define W6_CMD_ENC_SET_PARAM_BG				(W6_REG_BASE + 0x36C)
+#define		SET_PARAM_BG_DELTA_QP(x)		FIELD_PREP(GENMASK(29, 24), (x))
+#define		SET_PARAM_BG_THRESHOLD_MEAN_DIFF(x)	FIELD_PREP(GENMASK(17, 10), (x))
+#define		SET_PARAM_BG_THRESHOLD_MAX_DIFF(x)	FIELD_PREP(GENMASK(9, 1), (x))
+#define		SET_PARAM_BG_ENABLE(x)			FIELD_PREP(GENMASK(0, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_NON_VCL_PARAM		(W6_REG_BASE + 0x370)
+#define		SET_PARAM_NON_VCL_PARAM_ENCODE_VUI	BIT(1)
+#define W6_CMD_ENC_SET_PARAM_VUI_RBSP_ADDR		(W6_REG_BASE + 0x374)
+#define W6_CMD_ENC_SET_PARAM_HRD_RBSP_ADDR		(W6_REG_BASE + 0x378)
+#define W6_CMD_ENC_SET_PARAM_QROUND_OFFSET		(W6_REG_BASE + 0x380)
+#define		SET_PARAM_QROUND_OFFSET_INTER(x)	FIELD_PREP(GENMASK(23, 13), (x))
+#define		SET_PARAM_QROUND_OFFSET_INTRA(x)	FIELD_PREP(GENMASK(12, 2), (x))
+#define W6_CMD_ENC_SET_PARAM_QUANT1			(W6_REG_BASE + 0x384)
+#define W6_CMD_ENC_SET_PARAM_QUANT2			(W6_REG_BASE + 0x388)
+#define		SET_PARAM_QUANT2_LAMBDA_DQP_INTER(x)	FIELD_PREP(GENMASK(19, 14), (x))
+#define		SET_PARAM_QUANT2_LAMBDA_DQP_INTRA(x)	FIELD_PREP(GENMASK(13, 8), (x))
+#define W6_CMD_ENC_SET_PARAM_CUSTOM_GOP			(W6_REG_BASE + 0x38C)
+
+/* W6_CMD_ENC_SET_PARAM_CUSTOM_GOP_PIC(x)
+ * Address range: 0x390, 0x394, ..., 0x3AC (step = 0x4)
+ */
+#define W6_CMD_ENC_SET_PARAM_CUSTOM_GOP_PIC(x)		(W6_REG_BASE + 0x390 + ((x) * 0x4))
+#define		SET_PARAM_CUSTOM_GOP_PIC_TEMP_ID(x)	FIELD_PREP(GENMASK(29, 26), (x))
+#define		SET_PARAM_CUSTOM_GOP_PIC_REF1_POC(x)	FIELD_PREP(GENMASK(25, 20), (x))
+#define		SET_PARAM_CUSTOM_GOP_PIC_REF0_POC(x)	FIELD_PREP(GENMASK(19, 14), (x))
+#define		SET_PARAM_CUSTOM_GOP_PIC_MULTI_REF_P(x)	FIELD_PREP(GENMASK(13, 13), (x))
+#define		SET_PARAM_CUSTOM_GOP_PIC_QP(x)		FIELD_PREP(GENMASK(12, 7), (x))
+#define		SET_PARAM_CUSTOM_GOP_PIC_POC_OFFSET(x)	FIELD_PREP(GENMASK(6, 2), (x))
+#define		SET_PARAM_CUSTOM_GOP_PIC_TYPE(x)	FIELD_PREP(GENMASK(1, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_TILE_PARAM			(W6_REG_BASE + 0x3D0)
+
+/* W6_CMD_ENC_SET_PARAM_CUSTOM_LAMBDA(x)
+ * Address range: 0x3D4, 0x3D8, ..., 0x4A0 (step = 0x4)
+ */
+#define W6_CMD_ENC_SET_PARAM_CUSTOM_LAMBDA(x)		(W6_REG_BASE + 0x3D4 + ((x) * 0x4))
+#define		SET_PARAM_CUSTOM_LAMBDA_SSD(x)		FIELD_PREP(GENMASK(20, 7), (x))
+#define		SET_PARAM_CUSTOM_LAMBDA_SAD(x)		FIELD_PREP(GENMASK(6, 0), (x))
+
+/* W6_CMD_ENC_SET_PARAM_TEMP_LAYER_QP(x)
+ * Address range: 0x4A4, 0x4A8, ..., 0x4B0 (step = 0x4)
+ */
+#define W6_CMD_ENC_SET_PARAM_TEMP_LAYER_QP(x)		(W6_REG_BASE + 0x4A4 + ((x) * 0x4))
+#define		SET_PARAM_TEMP_LAYER_QP_B(x)		FIELD_PREP(GENMASK(17, 12), (x))
+#define		SET_PARAM_TEMP_LAYER_QP_P(x)		FIELD_PREP(GENMASK(11, 6), (x))
+#define		SET_PARAM_TEMP_LAYER_QP_I(x)		FIELD_PREP(GENMASK(5, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_SCALER_SIZE		(W6_REG_BASE + 0x4B4)
+#define		SET_PARAM_SCALER_SIZE_HEIGHT(x)		FIELD_PREP(GENMASK(31, 16), (x))
+#define		SET_PARAM_SCALER_SIZE_WIDTH(x)		FIELD_PREP(GENMASK(15, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_SCALER			(W6_REG_BASE + 0x4B8)
+#define		SET_PARAM_SCALER_COEF_MODE(x)		FIELD_PREP(GENMASK(2, 1), (x))
+#define		SET_PARAM_SCALER_ENABLE(x)		FIELD_PREP(GENMASK(0, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_COLOR			(W6_REG_BASE + 0x4F8)
+#define		SET_PARAM_COLOR_RANGE(x)		FIELD_PREP(GENMASK(25, 25), (x))
+#define		SET_PARAM_COLOR_MATRIX_COEFF(x)		FIELD_PREP(GENMASK(24, 17), (x))
+#define		SET_PARAM_COLOR_TRANS_CHAR(x)		FIELD_PREP(GENMASK(16, 9), (x))
+#define		SET_PARAM_COLOR_PRIMARIES(x)		FIELD_PREP(GENMASK(8, 1), (x))
+#define		SET_PARAM_COLOR_DESCRIPTION_PRESENT_ON	BIT(0)
+#define W6_CMD_ENC_SET_PARAM_SAR			(W6_REG_BASE + 0x4FC)
+#define		SET_PARAM_SAR_ASPECT_RATIO_IDC(x)	FIELD_PREP(GENMASK(8, 1), (x))
+#define		SET_PARAM_SAR_ASPECT_RATIO_ENABLE(x)	FIELD_PREP(GENMASK(0, 0), (x))
+#define W6_CMD_ENC_SET_PARAM_SAR_EXTENDED		(W6_REG_BASE + 0x500)
+#define		SET_PARAM_SAR_EXTENDED_HEIGHT(x)	FIELD_PREP(GENMASK(31, 16), (x))
+#define		SET_PARAM_SAR_EXTENDED_WIDTH(x)		FIELD_PREP(GENMASK(15, 0), (x))
+
+/* ENCODER - ENC_PIC */
+#define W6_CMD_ENC_PIC_BS_START				(W6_REG_BASE + 0x300)
+#define W6_CMD_ENC_PIC_BS_SIZE				(W6_REG_BASE + 0x304)
+#define W6_CMD_ENC_PIC_BS_OPTION			(W6_REG_BASE + 0x308)
+#define W6_CMD_ENC_PIC_SEC_AXI				(W6_REG_BASE + 0x30C)
+#define		ENC_PIC_SEC_AXI_RDO_ENABLE(x)		FIELD_PREP(GENMASK(1, 1), (x))
+#define		ENC_PIC_SEC_AXI_LF_ENABLE(x)		FIELD_PREP(GENMASK(0, 0), (x))
+#define W6_CMD_ENC_PIC_REPORT				(W6_REG_BASE + 0x310)
+#define		ENC_PIC_REPORT_MV_HISTO_ENABLE(x)	FIELD_PREP(GENMASK(1, 1), (x))
+#define W6_CMD_ENC_PIC_MV_HISTO0			(W6_REG_BASE + 0x318)
+#define		ENC_PIC_MV_HISTO0_THRESHOLD0(x)		FIELD_PREP(GENMASK(31, 16), (x))
+#define		ENC_PIC_MV_HISTO0_THRESHOLD1(x)		FIELD_PREP(GENMASK(15, 0), (x))
+#define W6_CMD_ENC_PIC_MV_HISTO1			(W6_REG_BASE + 0x31C)
+#define		ENC_PIC_MV_HISTO1_THRESHOLD2(x)		FIELD_PREP(GENMASK(31, 16), (x))
+#define		ENC_PIC_MV_HISTO1_THRESHOLD3(x)		FIELD_PREP(GENMASK(15, 0), (x))
+#define W6_CMD_ENC_PIC_CUSTOM_MAP_PARAM			(W6_REG_BASE + 0x320)
+#define W6_CMD_ENC_PIC_CUSTOM_MAP_ADDR			(W6_REG_BASE + 0x324)
+#define W6_CMD_ENC_PIC_SRC_PIC_IDX			(W6_REG_BASE + 0x32C)
+#define		ENC_PIC_SRC_PIC_IDX_END			0xFFFFFFFF
+#define W6_CMD_ENC_PIC_SRC_ADDR_Y			(W6_REG_BASE + 0x330)
+#define W6_CMD_ENC_PIC_SRC_ADDR_U			(W6_REG_BASE + 0x334)
+#define W6_CMD_ENC_PIC_SRC_ADDR_V			(W6_REG_BASE + 0x338)
+#define W6_CMD_ENC_PIC_SRC_STRIDE			(W6_REG_BASE + 0x33C)
+#define		ENC_PIC_SRC_STRIDE_L(x)			FIELD_PREP(GENMASK(31, 16), (x))
+#define		ENC_PIC_SRC_STRIDE_C(x)			FIELD_PREP(GENMASK(15, 0), (x))
+#define W6_CMD_ENC_PIC_SRC_FMT				(W6_REG_BASE + 0x340)
+#define		ENC_PIC_SRC_FMT_C_FMT_IDC(x)		FIELD_PREP(GENMASK(30, 28), (x))
+#define		ENC_PIC_SRC_FMT_CSC_24BIT(x)		FIELD_PREP(GENMASK(25, 25), (x))
+#define		ENC_PIC_SRC_FMT_CSC_AYUV(x)		FIELD_PREP(GENMASK(24, 24), (x))
+#define		ENC_PIC_SRC_FMT_CSC_ENABLE(x)		FIELD_PREP(GENMASK(20, 20), (x))
+#define		ENC_PIC_SRC_FMT_CSC_FMT_ORDER(x)	FIELD_PREP(GENMASK(19, 16), (x))
+#define		ENC_PIC_SRC_FMT_ENDIAN(x)		FIELD_PREP(GENMASK(15, 12), (x))
+#define		ENC_PIC_SRC_FMT_10BIT_ORDER(x)		FIELD_PREP(GENMASK(6, 6), (x))
+#define		ENC_PIC_SRC_FMT_10BIT_3PIXEL_4BYTE(x)	FIELD_PREP(GENMASK(5, 5), (x))
+#define		ENC_PIC_SRC_FMT_10BIT_ENABLE(x)		FIELD_PREP(GENMASK(4, 4), (x))
+#define		ENC_PIC_SRC_FMT_YUV422_PACKED_ORDER(x)	FIELD_PREP(GENMASK(3, 3), (x))
+#define		ENC_PIC_SRC_FMT_CBCR_ORDER(x)		FIELD_PREP(GENMASK(2, 2), (x))
+#define		ENC_PIC_SRC_FMT_2PLANE(x)		FIELD_PREP(GENMASK(1, 1), (x))
+#define		ENC_PIC_SRC_FMT_YUV422_PACKED(x)	FIELD_PREP(GENMASK(0, 0), (x))
+#define W6_CMD_ENC_PIC_SRC_AXI_SEL			(W6_REG_BASE + 0x348)
+#define		ENC_PIC_SRC_AXI_SEL_PRIMARY_PORT	1
+#define		ENC_PIC_SRC_AXI_SEL_PRP_PORT		0
+#define W6_CMD_ENC_PIC_CODE_OPTION			(W6_REG_BASE + 0x34C)
+#define		ENC_PIC_CODE_OPTION_ENCODE_VCL		BIT(1)
+#define		ENC_PIC_CODE_OPTION_ENCODE_HEADER	BIT(0)
+#define W6_CMD_ENC_PIC_PARAM				(W6_REG_BASE + 0x350)
+#define		ENC_PIC_PARAM_INTRA_4X4(x)		FIELD_PREP(GENMASK(29, 28), (x))
+#define		ENC_PIC_PARAM_FORCE_PIC_TYPE(x)		FIELD_PREP(GENMASK(23, 21), (x))
+#define		ENC_PIC_PARAM_FORCE_PIC_TYPE_ENABLE(x)	FIELD_PREP(GENMASK(20, 20), (x))
+#define		ENC_PIC_PARAM_FORCE_PIC_QP_B(x)		FIELD_PREP(GENMASK(19, 14), (x))
+#define		ENC_PIC_PARAM_FORCE_PIC_QP_P(x)		FIELD_PREP(GENMASK(13, 8), (x))
+#define		ENC_PIC_PARAM_FORCE_PIC_QP_I(x)		FIELD_PREP(GENMASK(7, 2), (x))
+#define		ENC_PIC_PARAM_FORCE_PIC_QP_ENABLE(x)	FIELD_PREP(GENMASK(1, 1), (x))
+#define		ENC_PIC_PARAM_PIC_SKIP_FLAG(x)		FIELD_PREP(GENMASK(0, 0), (x))
+#define W6_CMD_ENC_PIC_LONGTERM_PIC			(W6_REG_BASE + 0x354)
+#define W6_CMD_ENC_PIC_PREFIX_SEI_NAL_ADDR		(W6_REG_BASE + 0x358)
+#define W6_CMD_ENC_PIC_PREFIX_SEI_INFO			(W6_REG_BASE + 0x35C)
+#define W6_CMD_ENC_PIC_SUFFIX_SEI_NAL_ADDR		(W6_REG_BASE + 0x360)
+#define W6_CMD_ENC_PIC_SUFFIX_SEI_INFO			(W6_REG_BASE + 0x364)
+#define W6_CMD_ENC_PIC_CSC_COEFF0			(W6_REG_BASE + 0x374)
+#define		ENC_PIC_CSC_COEFF0_RY(x)		FIELD_PREP(GENMASK(29, 20), (x))
+#define		ENC_PIC_CSC_COEFF0_GY(x)		FIELD_PREP(GENMASK(19, 10), (x))
+#define		ENC_PIC_CSC_COEFF0_BY(x)		FIELD_PREP(GENMASK(9, 0), (x))
+#define W6_CMD_ENC_PIC_CSC_COEFF1			(W6_REG_BASE + 0x378)
+#define		ENC_PIC_CSC_COEFF1_RCB(x)		FIELD_PREP(GENMASK(29, 20), (x))
+#define		ENC_PIC_CSC_COEFF1_GCB(x)		FIELD_PREP(GENMASK(19, 10), (x))
+#define		ENC_PIC_CSC_COEFF1_BCB(x)		FIELD_PREP(GENMASK(9, 0), (x))
+#define W6_CMD_ENC_PIC_CSC_COEFF2			(W6_REG_BASE + 0x37C)
+#define		ENC_PIC_CSC_COEFF2_RCR(x)		FIELD_PREP(GENMASK(29, 20), (x))
+#define		ENC_PIC_CSC_COEFF2_GCR(x)		FIELD_PREP(GENMASK(19, 10), (x))
+#define		ENC_PIC_CSC_COEFF2_BCR(x)		FIELD_PREP(GENMASK(9, 0), (x))
+#define W6_CMD_ENC_PIC_CSC_COEFF3			(W6_REG_BASE + 0x380)
+#define		ENC_PIC_CSC_COEFF3_OFFSET_Y(x)		FIELD_PREP(GENMASK(29, 20), (x))
+#define		ENC_PIC_CSC_COEFF3_OFFSET_CB(x)		FIELD_PREP(GENMASK(19, 10), (x))
+#define		ENC_PIC_CSC_COEFF3_OFFSET_CR(x)		FIELD_PREP(GENMASK(9, 0), (x))
+#define W6_CMD_ENC_PIC_TIMESTAMP_LOW			(W6_REG_BASE + 0x448)
+#define W6_CMD_ENC_PIC_TIMESTAMP_HIGH			(W6_REG_BASE + 0x44C)
+
+/* ENCODER - QUERY : GET_RESULT */
+#define W6_RET_ENC_RD_PTR				(W6_REG_BASE + 0x300)
+#define W6_RET_ENC_WR_PTR				(W6_REG_BASE + 0x304)
+#define W6_RET_ENC_NUM_REQUIRED_FBC_FB			(W6_REG_BASE + 0x308)
+#define W6_RET_ENC_MIN_SRC_BUF_NUM			(W6_REG_BASE + 0x30C)
+#define W6_RET_ENC_PIC_TYPE				(W6_REG_BASE + 0x310)
+#define W6_RET_ENC_PIC_POC				(W6_REG_BASE + 0x314)
+#define W6_RET_ENC_PIC_IDX				(W6_REG_BASE + 0x318)
+#define W6_RET_ENC_PIC_SLICE_NUM			(W6_REG_BASE + 0x31C)
+#define W6_RET_ENC_PIC_SKIP				(W6_REG_BASE + 0x320)
+#define W6_RET_ENC_PIC_NUM_INTRA			(W6_REG_BASE + 0x324)
+#define W6_RET_ENC_PIC_NUM_MERGE			(W6_REG_BASE + 0x328)
+#define W6_RET_ENC_PIC_NON_REF_PIC_FLAG			(W6_REG_BASE + 0x32C)
+#define W6_RET_ENC_PIC_NUM_SKIP				(W6_REG_BASE + 0x330)
+#define W6_RET_ENC_PIC_AVG_CTU_QP			(W6_REG_BASE + 0x334)
+#define W6_RET_ENC_PIC_BYTE				(W6_REG_BASE + 0x338)
+#define W6_RET_ENC_GOP_PIC_IDX				(W6_REG_BASE + 0x33C)
+#define W6_RET_ENC_USED_SRC_IDX				(W6_REG_BASE + 0x340)
+#define W6_RET_ENC_PIC_NUM				(W6_REG_BASE + 0x344)
+#define W6_RET_ENC_VCL_NUT				(W6_REG_BASE + 0x348)
+#define W6_RET_ENC_PIC_DIST_LOW				(W6_REG_BASE + 0x350)
+#define W6_RET_ENC_PIC_DIST_HIGH			(W6_REG_BASE + 0x354)
+#define W6_RET_ENC_PIC_MAX_LATENCY_PICTURES		(W6_REG_BASE + 0x358)
+#define W6_RET_ENC_HISTO_CNT0				(W6_REG_BASE + 0x360)
+#define W6_RET_ENC_HISTO_CNT1				(W6_REG_BASE + 0x364)
+#define W6_RET_ENC_HISTO_CNT2				(W6_REG_BASE + 0x368)
+#define W6_RET_ENC_HISTO_CNT3				(W6_REG_BASE + 0x36C)
+#define W6_RET_ENC_HISTO_CNT4				(W6_REG_BASE + 0x370)
+#define W6_RET_ENC_WARN_INFO				(W6_REG_BASE + 0x3AC)
+#define W6_RET_ENC_ERR_INFO				(W6_REG_BASE + 0x3B0)
+#define W6_RET_ENC_ENCODING_SUCCESS			(W6_REG_BASE + 0x3B4)
+#define W6_RET_ENC_SUM_ME0_X_DIR_LOWER			(W6_REG_BASE + 0x3B8)
+#define W6_RET_ENC_SUM_ME0_X_DIR_HIGHER			(W6_REG_BASE + 0x3BC)
+#define W6_RET_ENC_SUM_ME0_Y_DIR_LOWER			(W6_REG_BASE + 0x3C0)
+#define W6_RET_ENC_SUM_ME0_Y_DIR_HIGHER			(W6_REG_BASE + 0x3C4)
+#define W6_RET_ENC_SUM_ME1_X_DIR_LOWER			(W6_REG_BASE + 0x3C8)
+#define W6_RET_ENC_SUM_ME1_X_DIR_HIGHER			(W6_REG_BASE + 0x3CC)
+#define W6_RET_ENC_SUM_ME1_Y_DIR_LOWER			(W6_REG_BASE + 0x3D0)
+#define W6_RET_ENC_SUM_ME1_Y_DIR_HIGHER			(W6_REG_BASE + 0x3D4)
+#define W6_RET_ENC_SRC_Y_ADDR				(W6_REG_BASE + 0x3E8)
+#define W6_RET_ENC_CUSTOM_MAP_ADDR			(W6_REG_BASE + 0x3EC)
+#define W6_RET_ENC_PREFIX_SEI_NAL_ADDR			(W6_REG_BASE + 0x3F0)
+#define W6_RET_ENC_SUFFIX_SEI_NAL_ADDR			(W6_REG_BASE + 0x3F4)
+#define W6_RET_ENC_NUM_REQUIRED_COL_BUF			(W6_REG_BASE + 0x404)
+#define W6_RET_ENC_TIMESTAMP_LOW			(W6_REG_BASE + 0x44C)
+#define W6_RET_ENC_TIMESTAMP_HIGH			(W6_REG_BASE + 0x450)
+
+#endif /* __WAVE6_REGDEFINE_H__ */
drivers/media/platform/chips-media/wave6/wave6-vdi.h new file +45 -0
new file mode 100644
index 000000000000..746a4e128ba8
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
+/*
+ * Wave6 series multi-standard codec IP - low level access interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#ifndef __WAVE6_VDI_H__
+#define __WAVE6_VDI_H__
+
+#include <linux/string.h>
+#include <linux/slab.h>
+#include <linux/device.h>
+#include <media/videobuf2-dma-contig.h>
+#include "wave6-vpuconfig.h"
+
+enum endian_mode {
+	VDI_128BIT_BIG_ENDIAN = 0,
+	VDI_128BIT_BE_BYTE_SWAP,
+	VDI_128BIT_BE_WORD_SWAP,
+	VDI_128BIT_BE_WORD_BYTE_SWAP,
+	VDI_128BIT_BE_DWORD_SWAP,
+	VDI_128BIT_BE_DWORD_BYTE_SWAP,
+	VDI_128BIT_BE_DWORD_WORD_SWAP,
+	VDI_128BIT_BE_DWORD_WORD_BYTE_SWAP,
+	VDI_128BIT_LE_DWORD_WORD_BYTE_SWAP,
+	VDI_128BIT_LE_DWORD_WORD_SWAP,
+	VDI_128BIT_LE_DWORD_BYTE_SWAP,
+	VDI_128BIT_LE_DWORD_SWAP,
+	VDI_128BIT_LE_WORD_BYTE_SWAP,
+	VDI_128BIT_LE_WORD_SWAP,
+	VDI_128BIT_LE_BYTE_SWAP,
+	VDI_128BIT_LITTLE_ENDIAN = 15,
+	VDI_ENDIAN_MAX
+};
+
+/**
+ * struct vpu_buf - VPU buffer for a coherent DMA buffer
+ * @size:	Buffer size
+ * @daddr:	Mapped address for device access
+ * @vaddr:	Kernel virtual address
+ * @dev:	Device pointer for DMA API
+ *
+ * Represents a buffer allocated via dma_alloc_coherent().
+ */

drivers/media/platform/chips-media/wave6/wave6-vdi.h line 55

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vdi.h new file +10 -0
+struct vpu_buf {
+	size_t size;
+	dma_addr_t daddr;
+	void *vaddr;
+	struct device *dev;
+};
+
+static inline void wave6_vdi_writel(void __iomem *base, u32 addr, u32 data)
+{
+	writel(data, base + addr);

consider big amount reg read write, suggest use writel_relaxed() and readl_relaxed()

drivers/media/platform/chips-media/wave6/wave6-vpuapi.h

Quoted 771 lines
drivers/media/platform/chips-media/wave6/wave6-vdi.h new file +37 -0
+}
+
+static inline unsigned int wave6_vdi_readl(void __iomem *base, u32 addr)
+{
+	return readl(base + addr);
+}
+
+static inline int wave6_vdi_alloc_dma(struct device *dev, struct vpu_buf *vb)
+{
+	void *vaddr;
+	dma_addr_t daddr;
+
+	if (!vb || !vb->size)
+		return -EINVAL;
+
+	vaddr = dma_alloc_coherent(dev, vb->size, &daddr, GFP_KERNEL);
+	if (!vaddr)
+		return -ENOMEM;
+
+	vb->vaddr = vaddr;
+	vb->daddr = daddr;
+	vb->dev = dev;
+
+	return 0;
+}
+
+static inline void wave6_vdi_free_dma(struct vpu_buf *vb)
+{
+	if (!vb || !vb->size || !vb->vaddr)
+		return;
+
+	dma_free_coherent(vb->dev, vb->size, vb->vaddr, vb->daddr);
+
+	memset(vb, 0, sizeof(*vb));
+}
+
+#endif /* __WAVE6_VDI_H__ */
drivers/media/platform/chips-media/wave6/wave6-vpuapi.c new file +336 -0
new file mode 100644
index 000000000000..912ab9861dcc
@@ -0,0 +1,336 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+/*
+ * Wave6 series multi-standard codec IP - wave6 helper interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#include <linux/bug.h>
+#include "wave6-vpuapi.h"
+#include "wave6-regdefine.h"
+#include "wave6-hw.h"
+#include "wave6-vpu-dbg.h"
+#include "wave6-trace.h"
+
+int wave6_vpu_dec_open(struct vpu_instance *inst, struct dec_open_param *pop)
+{
+	struct dec_info *p_dec_info;
+	int ret;
+	struct vpu_core_device *core = inst->dev;
+
+	guard(mutex)(&core->hw_lock);
+
+	if (!wave6_vpu_is_init(core))
+		return -ENODEV;
+
+	inst->codec_info = kzalloc_obj(*inst->codec_info);
+	if (!inst->codec_info)
+		return -ENOMEM;
+
+	p_dec_info = &inst->codec_info->dec_info;
+	memcpy(&p_dec_info->open_param, pop, sizeof(struct dec_open_param));
+
+	ret = wave6_vpu_build_up_dec_param(inst, pop);
+	if (ret) {
+		kfree(inst->codec_info);
+		inst->codec_info = NULL;
+	}
+
+	return ret;
+}
+
+int wave6_vpu_dec_close(struct vpu_instance *inst, u32 *fail_res)
+{
+	int ret;
+
+	if (WARN_ON(!inst->codec_info))
+		return -EINVAL;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	ret = wave6_vpu_dec_fini_seq(inst, fail_res);
+	if (ret)
+		dev_warn(inst->dev->dev, "dec seq end timed out\n");
+
+	kfree(inst->codec_info);
+	inst->codec_info = NULL;
+
+	return ret;
+}
+
+int wave6_vpu_dec_complete_seq_init(struct vpu_instance *inst,
+				    struct dec_seq_info *info)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int ret;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	ret = wave6_vpu_dec_get_seq_info(inst, info);
+	if (!ret)
+		p_dec_info->seq_info_obtained = true;
+
+	info->rd_ptr = wave6_vpu_dec_get_rd_ptr(inst);
+	info->wr_ptr = p_dec_info->stream_wr_ptr;
+
+	p_dec_info->seq_info = *info;
+
+	return ret;
+}
+
+int wave6_vpu_dec_get_aux_buffer_size(struct vpu_instance *inst,
+				      struct aux_buffer_size_info info, u32 *size)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int width = info.width;
+	int height = info.height;
+	int buf_size, twice;
+
+	if (info.type == AUX_BUF_FBC_Y_TBL) {
+		switch (inst->std) {
+		case W_HEVC_DEC:
+		case W_AVC_DEC:
+			buf_size = WAVE6_FBC_LUMA_TABLE_SIZE(width, height);
+			break;
+		default:
+			return -EINVAL;
+		}
+		buf_size = ALIGN(buf_size, 16);
+	} else if (info.type == AUX_BUF_FBC_C_TBL) {
+		if (p_dec_info->seq_info.c_fmt_idc == C_FMT_IDC_YUV422)
+			twice = 2;
+		else if (p_dec_info->seq_info.c_fmt_idc == C_FMT_IDC_YUV444)
+			twice = 4;
+		else
+			twice = 1;
+
+		switch (inst->std) {
+		case W_HEVC_DEC:
+		case W_AVC_DEC:
+			buf_size = WAVE6_FBC_CHROMA_TABLE_SIZE(width, height);
+			break;
+		default:
+			return -EINVAL;
+		}
+		buf_size = buf_size * twice;
+		buf_size = ALIGN(buf_size, 16);
+	} else if (info.type == AUX_BUF_MV_COL) {
+		switch (inst->std) {
+		case W_HEVC_DEC:
+			buf_size = WAVE6_DEC_HEVC_MVCOL_BUF_SIZE(width, height);
+			break;
+		case W_AVC_DEC:
+			buf_size = WAVE6_DEC_AVC_MVCOL_BUF_SIZE(width, height);
+			break;
+		default:
+			return -EINVAL;
+		}
+		buf_size = ALIGN(buf_size, 16);
+	} else {
+		return -EINVAL;
+	}
+
+	*size = buf_size;
+
+	return 0;
+}
+
+void wave6_vpu_dec_get_bitstream_buffer(struct vpu_instance *inst,
+					dma_addr_t *p_rd_ptr,
+					dma_addr_t *p_wr_ptr)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	dma_addr_t rd_ptr;
+	dma_addr_t wr_ptr;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	rd_ptr = wave6_vpu_dec_get_rd_ptr(inst);
+	wr_ptr = p_dec_info->stream_wr_ptr;
+
+	if (p_rd_ptr)
+		*p_rd_ptr = rd_ptr;
+	if (p_wr_ptr)
+		*p_wr_ptr = wr_ptr;
+}
+
+int wave6_vpu_dec_update_bitstream_buffer(struct vpu_instance *inst, int size)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	dma_addr_t wr_ptr;
+	dma_addr_t rd_ptr;
+
+	wr_ptr = p_dec_info->stream_wr_ptr;
+	rd_ptr = p_dec_info->stream_rd_ptr;
+
+	if (size > 0) {
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

drivers/media/platform/chips-media/wave6/wave6-vpuapi.h new file +386 -0
new file mode 100644
index 000000000000..d7cfcb0b2835

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

you use bit field:
here
is_secure_inst: 1

drivers/media/platform/chips-media/wave6/wave6-vpuapi.h

Quoted 482 lines
drivers/media/platform/chips-media/wave6/wave6-vpuapi.h new file +482 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

You can add performance_info later, like trace and dbg.

Quoted 488 lines
drivers/media/platform/chips-media/wave6/wave6-vpuapi.h new file +139 -0

Lines of this file are not shown. View the raw message.

drivers/media/platform/chips-media/wave6/wave6-vpuconfig.h new file +72 -0
new file mode 100644
index 000000000000..962c99112178

Lines of this file are not shown. View the raw message.

drivers/media/platform/chips-media/wave6/wave6-vpuerror.h new file +262 -1
new file mode 100644
index 000000000000..5bcc7ecf62d3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

2.31.1

Part of the diff is not shown here. View the raw message.

author
#13 lore raw

RE: [PATCH v7 3/9] media: chips-media: wave6: Add Wave6 VPU interface

Hi, Frank.

Quoted 37 lines

-----Original Message-----
From: Frank Li <[email protected]>
Sent: Friday, September 11, 2026 4:51 AM
To: Nas Chung <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jackson.lee <[email protected]>; lafley.kim <[email protected]>; [email protected]; Ming Qian <[email protected]>
Subject: Re: [PATCH v7 3/9] media: chips-media: wave6: Add Wave6 VPU interface

Quoted 11 lines · Nas Chung

Add an interface layer to manage hardware register configuration and communication with the Chips&Media Wave6 video codec IP.

The interface provides low-level helper functions used by the Wave6 core driver to implement video encoding and decoding operations. It handles command submission to the firmware via MMIO registers, and waits for a response by polling the firmware busy flag.

Quoted 10 lines
---
 MAINTAINERS                                   |    1 +
 .../platform/chips-media/wave6/wave6-hw.c     | 2030 +++++++++++++++++
 .../platform/chips-media/wave6/wave6-hw.h     |   56 +
 .../chips-media/wave6/wave6-regdefine.h       |  649 ++++++
 .../platform/chips-media/wave6/wave6-vdi.h    |   92 +
 .../platform/chips-media/wave6/wave6-vpuapi.c |  336 +++
 .../platform/chips-media/wave6/wave6-vpuapi.h | 1007 ++++++++
 .../chips-media/wave6/wave6-vpuconfig.h       |   72 +
 .../chips-media/wave6/wave6-vpuerror.h        |  262 +++
Quoted 1 line

You need make this patch built-able. Add Makefile and Kconfig part.

OK. I'll move Makefile and Kconfig in v8.

Quoted 42 lines
Quoted 5 lines
 9 files changed, 4505 insertions(+)
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-hw.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-hw.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-

regdefine.h

Quoted 2 lines
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vdi.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-

vpuapi.c

Quoted 1 line
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-

vpuapi.h

Quoted 1 line
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-

vpuconfig.h

Quoted 1 line
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-

vpuerror.h

MAINTAINERS M: Jackson Lee <[email protected]> · line 29245

Quoted 13 lines
MAINTAINERS +1 -0
index 7387a11facbe..e29018c2546b 100644
@@ -29239,6 +29239,7 @@ M:	Jackson Lee <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/media/nxp,imx95-vpu.yaml
+F:	drivers/media/platform/chips-media/wave6/
 
 WHISKEYCOVE PMIC GPIO DRIVER
 M:	Kuppuswamy Sathyanarayanan

<[email protected]>

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 11 lines

new file mode 100644
index 000000000000..a91b582af023

drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
@@ -0,0 +1,2030 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+/*
+ * Wave6 series multi-standard codec IP - wave6 backend interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 12

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+
+#include <linux/iopoll.h>
+#include "wave6-vpu-core.h"
+#include "wave6-hw.h"
+#include "wave6-regdefine.h"
+#include "wave6-trace.h"

Add trace support later, add with patch6.

OK.

Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 17

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +5 -0
+
+void wave6_vpu_writel(struct vpu_core_device *core, u32 addr, u32 data)
+{
+	wave6_vdi_writel(core->reg_base, addr, data);
+	trace_wave6_vpu_writel(core->dev, addr, data);

Add trace support at later patches.

OK.

Quoted 15 lines
Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+}
+
+u32 wave6_vpu_readl(struct vpu_core_device *core, u32 addr)

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+{
+	u32 data;
+
+	data = wave6_vdi_readl(core->reg_base, addr);
+	trace_wave6_vpu_readl(core->dev, addr, data);
+
+	return data;
+}
+
+static void wave6_print_reg_err(struct vpu_core_device *core, u32

fail_reason)

Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 36

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+{
+	void *caller = __builtin_return_address(0);
+	struct device *dev = core->dev;
+
+	switch (fail_reason) {
+	case WAVE6_SYSERR_QUEUEING_FAIL:

you define it as bit mask, is it possible more than 1 bits to set?

No, the firmware reports one reason at a time in W6_RET_FAIL_REASON, so only one bit is ever set.

Quoted 34 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 37

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+		dev_dbg(dev, "%pS: queueing failure 0x%x\n", caller,

fail_reason);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+		break;
+	case WAVE6_SYSERR_RESULT_NOT_READY:
+		dev_err(dev, "%pS: result not ready 0x%x\n", caller,

fail_reason);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+		break;
+	case WAVE6_SYSERR_ACCESS_VIOLATION_HW:
+		dev_err(dev, "%pS: access violation 0x%x\n", caller,

fail_reason);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+		break;
+	case WAVE6_SYSERR_WATCHDOG_TIMEOUT:
+		dev_err(dev, "%pS: watchdog timeout 0x%x\n", caller,

fail_reason);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 9 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +9 -0
+		break;
+	case WAVE6_SYSERR_BUS_ERROR:
+		dev_err(dev, "%pS: bus error 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_DOUBLE_FAULT:
+		dev_err(dev, "%pS: double fault 0x%x\n", caller, fail_reason);
+		break;
+	case WAVE6_SYSERR_VPU_STILL_RUNNING:
+		dev_err(dev, "%pS: still running 0x%x\n", caller,

fail_reason);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +8 -0
+		break;
+	default:
+		dev_err(dev, "%pS: failure: 0x%x\n", caller, fail_reason);
+		break;
+	}
+}
+
+static void wave6_dec_set_display_buffer(struct vpu_instance *inst,

struct frame_buffer fb)

Quoted 10 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 71

Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +8 -0
+
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int index;
+
+	for (index = 0; index < WAVE6_MAX_FBS; index++) {
+		if (!p_dec_info->disp_buf[index].buf_y) {
+			p_dec_info->disp_buf[index] = fb;
+			p_dec_info->disp_buf[index].index = index;

index is the same as nature array index p_dec_info->disp_buf, why need it.

Agreed. I'll remove the index field.

Quoted 5 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 72

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+			break;

where update .buf_y?

p_dec_info->disp_buf[index] = fb; assigns the whole struct, buf_y included.

Quoted 7 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +5 -0
+		}
+	}
+}
+
+static struct frame_buffer wave6_dec_get_display_buffer(struct

vpu_instance *inst,

Quoted 16 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 90

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +13 -0
+							dma_addr_t addr)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int i;
+	struct frame_buffer fb;
+
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {
+		if (p_dec_info->disp_buf[i].buf_y == addr)
+			return p_dec_info->disp_buf[i];
+	}
+
+	memset(&fb, 0, sizeof(struct frame_buffer));
+	fb.index = -1;

suppose this error case, did caller tolerent fb.index -1, I have not see check this value.

In wave6_vpu_dec_get_output_info(), decoded_idx and disp_idx start at -1 and every dec_out_info[] access checks both bounds.

I'll remove the index and handle this in wave6_vpu_dec_get_output_info().

Quoted 16 lines
Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+
+	return fb;
+}

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+
+static void wave6_dec_remove_display_buffer(struct vpu_instance *inst,
+					    dma_addr_t addr)
+{
+	struct dec_info *p_dec_info = &inst->codec_info->dec_info;
+	int i;
+
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {
+		if (p_dec_info->disp_buf[i].buf_y == addr) {
+			memset(&p_dec_info->disp_buf[i], 0, sizeof(struct

frame_buffer));

Quoted 1 line

you suppose need set p_dec_info->disp_buf[i].buf_y to 0?

Agreed. I'll drop the memset and just set buf_y to 0.

Quoted 130 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+			break;
+		}
+	}
+}
+
+static enum chroma_format_idc get_chroma_format_idc(enum

frame_buffer_format fmt)

Quoted 50 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +50 -0
+{
+	switch (fmt) {
+	case FORMAT_420:
+	case FORMAT_420_P10_16BIT_MSB:
+	case FORMAT_420_P10_16BIT_LSB:
+	case FORMAT_420_P10_32BIT_MSB:
+	case FORMAT_420_P10_32BIT_LSB:
+		return C_FMT_IDC_YUV420;
+	case FORMAT_422:
+	case FORMAT_422_P10_16BIT_MSB:
+	case FORMAT_422_P10_16BIT_LSB:
+	case FORMAT_422_P10_32BIT_MSB:
+	case FORMAT_422_P10_32BIT_LSB:
+	case FORMAT_YUYV:
+	case FORMAT_YVYU:
+	case FORMAT_UYVY:
+	case FORMAT_VYUY:
+	case FORMAT_YUYV_P10_16BIT_MSB:
+	case FORMAT_YVYU_P10_16BIT_MSB:
+	case FORMAT_UYVY_P10_16BIT_MSB:
+	case FORMAT_VYUY_P10_16BIT_MSB:
+	case FORMAT_YUYV_P10_16BIT_LSB:
+	case FORMAT_YVYU_P10_16BIT_LSB:
+	case FORMAT_UYVY_P10_16BIT_LSB:
+	case FORMAT_VYUY_P10_16BIT_LSB:
+	case FORMAT_YUYV_P10_32BIT_MSB:
+	case FORMAT_YVYU_P10_32BIT_MSB:
+	case FORMAT_UYVY_P10_32BIT_MSB:
+	case FORMAT_VYUY_P10_32BIT_MSB:
+	case FORMAT_YUYV_P10_32BIT_LSB:
+	case FORMAT_YVYU_P10_32BIT_LSB:
+	case FORMAT_UYVY_P10_32BIT_LSB:
+	case FORMAT_VYUY_P10_32BIT_LSB:
+		return C_FMT_IDC_YUV422;
+	case FORMAT_444:
+	case FORMAT_444_P10_16BIT_MSB:
+	case FORMAT_444_P10_16BIT_LSB:
+	case FORMAT_444_P10_32BIT_MSB:
+	case FORMAT_444_P10_32BIT_LSB:
+		return C_FMT_IDC_YUV444;
+	case FORMAT_400:
+	case FORMAT_400_P10_16BIT_MSB:
+	case FORMAT_400_P10_16BIT_LSB:
+	case FORMAT_400_P10_32BIT_MSB:
+	case FORMAT_400_P10_32BIT_LSB:
+	case FORMAT_YUV444_24BIT:
+		return C_FMT_IDC_YUV400;
+	case FORMAT_RGB_24BIT_PACKED:
+	case FORMAT_YUV444_24BIT_PACKED:
+	case FORMAT_RGB_32BIT_PACKED:

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	case FORMAT_RGB_P10_32BIT_PACKED:
+	case FORMAT_YUV444_32BIT_PACKED:
+	case FORMAT_YUV444_P10_32BIT_PACKED:
+		return C_FMT_IDC_RGB;
+	default:
+		return C_FMT_IDC_YUV400;
+	}
+}
+
+static int wave6_wait_vpu_busy(struct vpu_core_device *core, unsigned

int addr)

Quoted 34 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +34 -0
+{
+	u32 data;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	return read_poll_timeout(vpu_read_reg, data, !data,
+				 W6_VPU_POLL_DELAY_US, W6_VPU_POLL_TIMEOUT,
+				 false, core, addr);
+}
+
+void wave6_vpu_enable_interrupt(struct vpu_core_device *core)
+{
+	u32 data;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	data = BIT(W6_INT_BIT_ENC_SET_PARAM);
+	data |= BIT(W6_INT_BIT_ENC_PIC);
+	data |= BIT(W6_INT_BIT_INIT_SEQ);
+	data |= BIT(W6_INT_BIT_DEC_PIC);
+	data |= BIT(W6_INT_BIT_BSBUF_ERROR);
+	data |= BIT(W6_INT_BIT_REQ_WORK_BUF);
+	vpu_write_reg(core, W6_VPU_VINT_ENABLE, data);
+}
+
+bool wave6_vpu_is_init(struct vpu_core_device *core)
+{
+	lockdep_assert_held(&core->hw_lock);
+
+	return vpu_read_reg(core, W6_VPU_VCPU_CUR_PC) != 0;
+}
+
+static u32 wave6_vpu_get_product_id(struct vpu_core_device *core)
+{

drivers/media/platform/chips-media/wave6/wave6-hw.c line 213

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	u32 product_id = PRODUCT_ID_NONE;
+	u32 val;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	val = vpu_read_reg(core, W6_VPU_RET_PRODUCT_CODE);
+
+	switch (val) {
+	case WAVE617_CODE:
+		product_id = PRODUCT_ID_617;

return PRODUCT_ID_617;

drivers/media/platform/chips-media/wave6/wave6-hw.c line 217

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +4 -0
+		break;
+	case WAVE627_CODE:
+		product_id = PRODUCT_ID_627;
+		break;

return PRODUCT_ID_627;

drivers/media/platform/chips-media/wave6/wave6-hw.c line 223

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+	case WAVE633_CODE:
+	case WAVE637_CODE:
+	case WAVE663_CODE:
+	case WAVE677_CODE:
+		product_id = PRODUCT_ID_637;
+		break;

return PRODUCT_ID_637;

Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 229

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+	default:
+		dev_err(core->dev, "Invalid product (%x)\n", val);
+		break;
+	}
+
+	return product_id;

return PRODUCT_ID_NONE; needn't variable product_id

OK.

Quoted 5 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+}
+
+static void wave6_send_command(struct vpu_core_device *core, u32 id, u32

std, u32 cmd)

Quoted 13 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 241

Quoted 9 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +9 -0
+{
+	u32 reg_val;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	if (cmd == W6_CMD_CREATE_INSTANCE)
+		reg_val = INSTANCE_INFO_CODEC_STD(std);
+	else
+		reg_val = INSTANCE_INFO_CODEC_STD(std) | INSTANCE_INFO_ID(id);
	reg_val = INSTANCE_INFO_CODEC_STD(std);
	if (cmd != W6_CMD_CREATE_INSTANCE)
		reg_val |= INSTANCE_INFO_ID(id);

OK.

Quoted 26 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	vpu_write_reg(core, W6_CMD_INSTANCE_INFO, reg_val);
+
+	vpu_write_reg(core, W6_VPU_BUSY_STATUS, BUSY_STATUS_SET);
+	vpu_write_reg(core, W6_COMMAND, cmd);
+	vpu_write_reg(core, W6_VPU_HOST_INT_REQ, HOST_INT_REQ_ON);
+
+	trace_wave6_vpu_send_command(core, id, std, cmd);
+}
+
+static int wave6_send_query(struct vpu_core_device *core, u32 id, u32

std,

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +13 -0
+			    enum wave6_query_option query_opt)
+{
+	int ret;
+	u32 reg_val;
+
+	lockdep_assert_held(&core->hw_lock);
+
+	vpu_write_reg(core, W6_QUERY_OPTION, query_opt);
+	wave6_send_command(core, id, std, W6_CMD_QUERY);
+
+	ret = wave6_wait_vpu_busy(core, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(core->dev, "query timed out opt=0x%x\n", query_opt);
Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 270

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+		return ret;
+	}
+
+	if (!vpu_read_reg(core, W6_RET_SUCCESS)) {
+		reg_val = vpu_read_reg(core, W6_RET_FAIL_REASON);
+		wave6_print_reg_err(core, reg_val);

You can add such debug helper function later to reduce each patch's change.

OK. I'll split the debug part to other patch.

Quoted 6 lines
Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +5 -0
+		return -EIO;
+	}
+
+	return 0;
+}

...

Quoted 76 lines
Quoted 1 line

+static void wave6_get_dec_seq_result(struct vpu_instance *inst, struct

dec_seq_info *info)

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +13 -0
+{
+	u32 reg_val;
+	u32 profile, profile_comp;
+
+	lockdep_assert_held(&inst->dev->hw_lock);
+
+	info->rd_ptr = wave6_vpu_dec_get_rd_ptr(inst);
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_PIC_SIZE);
+	info->pic_width = DEC_PIC_SIZE_WIDTH(reg_val);
+	info->pic_height = DEC_PIC_SIZE_HEIGHT(reg_val);
+
+	info->min_frame_buffer_count = vpu_read_reg(inst->dev,

W6_RET_DEC_NUM_REQUIRED_FBC_FB);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+	info->frame_buf_delay = vpu_read_reg(inst->dev,

W6_RET_DEC_NUM_REORDER_DELAY);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+	info->req_mv_buffer_count = vpu_read_reg(inst->dev,

W6_RET_DEC_NUM_REQUIRED_COL_BUF);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +4 -0
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_CROP_TOP_BOTTOM);
+	info->pic_crop_rect.top = DEC_CROP_TOP(reg_val);
+	info->pic_crop_rect.bottom = info->pic_height -

DEC_CROP_BOTTOM(reg_val);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +4 -0
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_CROP_LEFT_RIGHT);
+	info->pic_crop_rect.left = DEC_CROP_LEFT(reg_val);
+	info->pic_crop_rect.right = info->pic_width -

DEC_CROP_RIGHT(reg_val);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+
+	info->f_rate_numerator = vpu_read_reg(inst->dev,

W6_RET_DEC_FRAME_RATE_NR);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+	info->f_rate_denominator = vpu_read_reg(inst->dev,

W6_RET_DEC_FRAME_RATE_DR);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +7 -0
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_COLOR_SAMPLE);
+	info->luma_bit_depth = DEC_COLOR_SAMPLE_L_BIT_DEPTH(reg_val);
+	info->chroma_bit_depth = DEC_COLOR_SAMPLE_C_BIT_DEPTH(reg_val);
+	info->c_fmt_idc = DEC_COLOR_SAMPLE_C_FMT_IDC(reg_val);
+	info->aspect_rate_info = DEC_COLOR_SAMPLE_ASPECT_RATIO_IDC(reg_val);
+	info->is_ext_sar = info->aspect_rate_info ==

H264_VUI_SAR_IDC_EXTENDED;

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+	if (info->is_ext_sar)
+		info->aspect_rate_info = vpu_read_reg(inst->dev,

W6_RET_DEC_ASPECT_RATIO);

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +7 -0
+	info->bitrate = vpu_read_reg(inst->dev, W6_RET_DEC_BIT_RATE);
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_SEQ_PARAM);
+	info->level = DEC_SEQ_PARAM_LEVEL(reg_val);
+	info->tier = DEC_SEQ_PARAM_TIER(reg_val);
+
+	profile = DEC_SEQ_PARAM_PROFILE(reg_val);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	profile_comp = DEC_SEQ_PARAM_PROFILE_COMPATIBILITY(reg_val);
+	if (inst->std == W_HEVC_DEC) {
+		info->profile = profile;
+		if (!info->profile) {
+			if ((profile_comp & PROFILE_COMPATIBILITY_MAIN) &&
+			    (profile_comp & PROFILE_COMPATIBILITY_MAIN10))
+				info->profile = HEVC_PROFILE_MAIN;
+			else if (profile_comp & PROFILE_COMPATIBILITY_MAIN10)
+				info->profile = HEVC_PROFILE_MAIN10;
+			else if (profile_comp &

PROFILE_COMPATIBILITY_STILL_PICTURE)

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +12 -0
+				info->profile = HEVC_PROFILE_STILLPICTURE;
+			else
+				info->profile = HEVC_PROFILE_MAIN;
+		}
+	} else if (inst->std == W_AVC_DEC) {
+		if (profile == PROFILE_H264_BP)
+			info->profile = H264_PROFILE_BP;
+		else if (profile == PROFILE_H264_HP)
+			info->profile = H264_PROFILE_HP;
+		else if (profile == PROFILE_H264_MP)
+			info->profile = H264_PROFILE_MP;
+		else if (profile == PROFILE_H264_HIGH10)
Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 475

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +5 -0
+			info->profile = H264_PROFILE_HIGH10;
+		else if (profile == PROFILE_H264_EXTENDED)
+			info->profile = H264_PROFILE_EXTENDED;
+		else
+			info->profile = H264_PROFILE_BP;

use small helper function for such convertion, you can use switch case for this maping or static const array.

OK. I'll address this in v8.

Quoted 69 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+	}
+
+	reg_val = vpu_read_reg(inst->dev, W6_RET_DEC_COLOR_CONFIG);
+	if (reg_val) {
+		info->color.video_signal_type_present = true;
+		info->color.color_description_present =

DEC_COLOR_CONFIG_COLOR_PRESENT(reg_val);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+		info->color.color_primaries =

DEC_COLOR_CONFIG_COLOR_PRIMARIES(reg_val);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+		info->color.transfer_characteristics =

DEC_COLOR_CONFIG_TRANS_CHAR(reg_val);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+		info->color.matrix_coefficients =

DEC_COLOR_CONFIG_MATRIX_COEFF(reg_val);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+		info->color.color_range =

DEC_COLOR_CONFIG_COLOR_RANGE(reg_val);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+	} else {
+		info->color.video_signal_type_present = false;
+	}
+}
+
+int wave6_vpu_dec_get_seq_info(struct vpu_instance *inst, struct

dec_seq_info *info)

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+{
+	int ret;
+
+	lockdep_assert_held(&inst->dev->hw_lock);
+
+	ret = wave6_send_query(inst->dev, inst->id, inst->std,

W6_QUERY_OPT_GET_RESULT);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +5 -0
+	if (ret)
+		return ret;
+
+	if (vpu_read_reg(inst->dev, W6_RET_DEC_DECODING_SUCCESS) != 1) {
+		info->err_reason = vpu_read_reg(inst->dev,

W6_RET_DEC_ERR_INFO);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+		ret = -EIO;
+	} else {
+		info->warn_info = vpu_read_reg(inst->dev,

W6_RET_DEC_WARN_INFO);

Quoted 29 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +29 -0
+	}
+
+	wave6_get_dec_seq_result(inst, info);
+
+	return ret;
+}
+
+int wave6_vpu_dec_register_frame_buffer(struct vpu_instance *inst)
+{
+	struct dec_info *p_dec_info;
+	struct frame_buffer *fb_arr;
+	size_t fbc_remain, mv_remain, fbc_idx = 0, mv_idx = 0;
+	size_t i, k, group_num, fbc_count, mv_count;
+	dma_addr_t fbc_cr_tbl_addr;
+	u32 reg_val;
+	int ret;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_dec_info = &inst->codec_info->dec_info;
+
+	fb_arr = &inst->frame_buf[0];
+	fbc_count = p_dec_info->seq_info.min_frame_buffer_count;
+	mv_count = p_dec_info->seq_info.req_mv_buffer_count;
+
+	for (i = 0; i < fbc_count; i++) {
+		if (!inst->aux_vbuf[AUX_BUF_FBC_Y_TBL][i].daddr)
+			return -EINVAL;
+		if (!inst->aux_vbuf[AUX_BUF_FBC_C_TBL][i].daddr)
Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 540

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+			return -EINVAL;
+	}
+	for (i = 0; i < mv_count; i++) {
+		if (!inst->aux_vbuf[AUX_BUF_MV_COL][i].daddr)
+			return -EINVAL;
+	}

use small helper function, like check_aux_vbuf().

OK.

Quoted 7 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +5 -0
+
+	reg_val = SET_FB_PIC_SIZE_WIDTH(p_dec_info->seq_info.pic_width) |
+		  SET_FB_PIC_SIZE_HEIGHT(p_dec_info->seq_info.pic_height);
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_PIC_SIZE, reg_val);
+	reg_val = SET_FB_PIC_INFO_C_FMT_IDC(p_dec_info->seq_info.c_fmt_idc)

|

Quoted 14 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 555

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+		  SET_FB_PIC_INFO_L_BIT_DEPTH(p_dec_info-

seq_info.luma_bit_depth) |

drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+		  SET_FB_PIC_INFO_C_BIT_DEPTH(p_dec_info-

seq_info.chroma_bit_depth);

drivers/media/platform/chips-media/wave6/wave6-hw.c new file +8 -0
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_PIC_INFO, reg_val);
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_DEFAULT_CDF, 0);
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_SEGMAP, 0);
+	vpu_write_reg(inst->dev, W6_CMD_SET_FB_MV_COL_PRE_ENT, 0);
+
+	fbc_remain = fbc_count;
+	mv_remain = mv_count;
+	group_num = DIV_ROUND_UP(max(fbc_count, mv_count), 16);

Add empty line here.

OK.

Quoted 46 lines
Quoted 41 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +32 -0
+	for (i = 0; i < group_num; i++) {
+		bool first_group = i == 0;
+		bool last_group = i == group_num - 1;
+		u32 set_fbc_num = (fbc_remain >= 16) ? 16 : fbc_remain;
+		u32 set_mv_num = (mv_remain >= 16) ? 16 : mv_remain;
+		u32 fbc_start_no = i * 16;
+		u32 fbc_end_no = fbc_start_no + set_fbc_num - 1;
+		u32 mv_start_no = i * 16;
+		u32 mv_end_no = mv_start_no + set_mv_num - 1;
+
+		reg_val = SET_FB_OPTION_ENDIAN(VDI_128BIT_BIG_ENDIAN);
+		if (first_group)
+			reg_val |= SET_FB_OPTION_START;
+		if (last_group)
+			reg_val |= SET_FB_OPTION_END;
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_OPTION, reg_val);
+
+		reg_val = SET_FB_NUM_FBC_START_IDX(fbc_start_no) |
+			  SET_FB_NUM_FBC_END_IDX(fbc_end_no) |
+			  SET_FB_NUM_MV_COL_START_IDX(mv_start_no) |
+			  SET_FB_NUM_MV_COL_END_IDX(mv_end_no);
+		vpu_write_reg(inst->dev, W6_CMD_SET_FB_NUM, reg_val);
+
+		for (k = 0; k < set_fbc_num; k++) {
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_Y(k),
+				      fb_arr[fbc_idx].buf_y);
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_C(k),
+				      fb_arr[fbc_idx].buf_cb);
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_CR(k),
+				      fb_arr[fbc_idx].buf_cr);
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_Y_OFFSET(k),
+				      inst-

aux_vbuf[AUX_BUF_FBC_Y_TBL][fbc_idx].daddr);

drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_C_OFFSET(k),
+				      inst-

aux_vbuf[AUX_BUF_FBC_C_TBL][fbc_idx].daddr);

drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+			fbc_cr_tbl_addr = inst-

aux_vbuf[AUX_BUF_FBC_C_TBL][fbc_idx].daddr +

drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+					  (inst-

aux_vbuf[AUX_BUF_FBC_C_TBL][fbc_idx].size >> 1);

drivers/media/platform/chips-media/wave6/wave6-hw.c line 593

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_FBC_CR_OFFSET(k),
+				      fbc_cr_tbl_addr);

I am not sure how big set_fbc_num, I suggest change to use writel_relax().

Quoted 2 lines

because you wave6_wait_vpu_busy() to check status, extra memory barry in writel() is not neccessary.

OK, as I answered on your earlier mail. The barriered writel() stays in wave6_send_command().

Quoted 73 lines
Quoted 9 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +7 -0
+			fbc_idx++;
+		}
+		fbc_remain -= k;
+
+		for (k = 0; k < set_mv_num; k++) {
+			vpu_write_reg(inst->dev, W6_CMD_SET_FB_MV_COL(k),
+				      inst-

aux_vbuf[AUX_BUF_MV_COL][mv_idx].daddr);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +5 -0
+			mv_idx++;
+		}
+		mv_remain -= k;
+
+		wave6_send_command(inst->dev, inst->id, inst->std,

W6_CMD_SET_FB);

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +4 -0
+		ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+		if (ret) {
+			dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+			return ret;

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+		}
+
+		if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS))
+			return -EIO;
+	}
+
+	return 0;
+}
+
+int wave6_vpu_dec_register_display_buffer(struct vpu_instance *inst,

struct frame_buffer fb)

Quoted 43 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +43 -0
+{
+	int ret;
+	struct dec_info *p_dec_info;
+	u32 reg_val;
+	u32 c_fmt_idc, out_fmt, out_mode;
+
+	guard(mutex)(&inst->dev->hw_lock);
+
+	p_dec_info = &inst->codec_info->dec_info;
+
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_SCL_PARAM,
+		      inst->scaler_info.enable);
+	reg_val = SET_DISP_SCL_PIC_SIZE_WIDTH(inst->scaler_info.width) |
+		  SET_DISP_SCL_PIC_SIZE_HEIGHT(inst->scaler_info.height);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_SCL_PIC_SIZE, reg_val);
+	reg_val = SET_DISP_PIC_SIZE_WIDTH(p_dec_info->seq_info.pic_width) |
+		  SET_DISP_PIC_SIZE_HEIGHT(p_dec_info->seq_info.pic_height);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_PIC_SIZE, reg_val);
+
+	c_fmt_idc = get_chroma_format_idc(p_dec_info->wtl_format);
+	switch (p_dec_info->wtl_format) {
+	case FORMAT_420_P10_16BIT_MSB:
+	case FORMAT_422_P10_16BIT_MSB:
+	case FORMAT_444_P10_16BIT_MSB:
+	case FORMAT_400_P10_16BIT_MSB:
+		out_mode = (WTL_RIGHT_JUSTIFIED << 2) | WTL_PIXEL_16BIT;
+		break;
+	case FORMAT_420_P10_16BIT_LSB:
+	case FORMAT_422_P10_16BIT_LSB:
+	case FORMAT_444_P10_16BIT_LSB:
+	case FORMAT_400_P10_16BIT_LSB:
+		out_mode = (WTL_LEFT_JUSTIFIED << 2) | WTL_PIXEL_16BIT;
+		break;
+	case FORMAT_420_P10_32BIT_MSB:
+	case FORMAT_422_P10_32BIT_MSB:
+	case FORMAT_444_P10_32BIT_MSB:
+	case FORMAT_400_P10_32BIT_MSB:
+		out_mode = (WTL_RIGHT_JUSTIFIED << 2) | WTL_PIXEL_32BIT;
+		break;
+	case FORMAT_420_P10_32BIT_LSB:
+	case FORMAT_422_P10_32BIT_LSB:
+	case FORMAT_444_P10_32BIT_LSB:
+	case FORMAT_400_P10_32BIT_LSB:
Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 668

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+		out_mode = (WTL_LEFT_JUSTIFIED << 2) | WTL_PIXEL_32BIT;
+		break;
+	default:
+		out_mode = (WTL_RIGHT_JUSTIFIED << 2) | WTL_PIXEL_8BIT;
+		break;
+	}

move such long convert to small helper function

OK.

Quoted 37 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +5 -0
+	out_fmt = (inst->nv21 << 1) | inst->cbcr_interleave;
+
+	reg_val = SET_DISP_COMMON_PIC_INFO_BWB_ON |
+		  SET_DISP_COMMON_PIC_INFO_C_FMT_IDC(c_fmt_idc) |
+		  SET_DISP_COMMON_PIC_INFO_PIXEL_ORDER(PIXEL_ORDER_INCREASING)

|

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +4 -0
+		  SET_DISP_COMMON_PIC_INFO_OUT_MODE(out_mode) |
+		  SET_DISP_COMMON_PIC_INFO_OUT_FMT(out_fmt) |
+		  SET_DISP_COMMON_PIC_INFO_STRIDE(fb.stride);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_COMMON_PIC_INFO,

reg_val);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 11 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +11 -0
+	reg_val = SET_DISP_OPTION_ENDIAN(VDI_128BIT_BIG_ENDIAN);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_OPTION, reg_val);
+	reg_val = SET_DISP_PIC_INFO_L_BIT_DEPTH(fb.luma_bit_depth) |
+		  SET_DISP_PIC_INFO_C_BIT_DEPTH(fb.chroma_bit_depth) |
+		  SET_DISP_PIC_INFO_C_FMT_IDC(fb.c_fmt_idc);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_PIC_INFO, reg_val);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_Y_BASE, fb.buf_y);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_CB_BASE, fb.buf_cb);
+	vpu_write_reg(inst->dev, W6_CMD_DEC_SET_DISP_CR_BASE, fb.buf_cr);
+
+	wave6_send_command(inst->dev, inst->id, inst->std,

W6_CMD_DEC_SET_DISP);

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +13 -0
+	ret = wave6_wait_vpu_busy(inst->dev, W6_VPU_BUSY_STATUS);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: timeout\n", __func__);
+		return ret;
+	}
+
+	if (!vpu_read_reg(inst->dev, W6_RET_SUCCESS))
+		return -EIO;
+
+	wave6_dec_set_display_buffer(inst, fb);
+
+	return 0;
+}

...

Quoted 54 lines
Quoted 1 line

+static int wave6_set_enc_crop_info(u32 codec, struct enc_codec_param

*param,

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+				   u32 rot_mir, int width, int height)
+{
+	int aligned_width = (codec == W_HEVC_ENC) ? ALIGN(width, 32) :

ALIGN(width, 16);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+	int aligned_height = (codec == W_HEVC_ENC) ? ALIGN(height, 32) :

ALIGN(height, 16);

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+	int pad_right, pad_bot;
+	int crop_right, crop_left, crop_top, crop_bot;
+
+	if (codec == W_HEVC_ENC &&
+	    (!rot_mir ||
+	     rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_MIRV |

SET_PARAM_ROT_180)))

Quoted 15 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +15 -0
+		return 0;
+
+	pad_right = aligned_width - width;
+	pad_bot = aligned_height - height;
+
+	if (param->conf_win.right > 0)
+		crop_right = param->conf_win.right + pad_right;
+	else
+		crop_right = pad_right;
+
+	if (param->conf_win.bottom > 0)
+		crop_bot = param->conf_win.bottom + pad_bot;
+	else
+		crop_bot = pad_bot;
+

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	crop_top = param->conf_win.top;
+	crop_left = param->conf_win.left;
+
+	param->conf_win.top = crop_top;
+	param->conf_win.left = crop_left;
+	param->conf_win.bottom = crop_bot;
+	param->conf_win.right = crop_right;
+
+	if (rot_mir == SET_PARAM_ROT_90 ||
+	    rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_MIRV |

SET_PARAM_ROT_270)) {

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +12 -0
+		param->conf_win.top = crop_right;
+		param->conf_win.left = crop_top;
+		param->conf_win.bottom = crop_left;
+		param->conf_win.right = crop_bot;
+	} else if (rot_mir == SET_PARAM_ROT_180 ||
+		   rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_MIRV)) {
+		param->conf_win.top = crop_bot;
+		param->conf_win.left = crop_right;
+		param->conf_win.bottom = crop_top;
+		param->conf_win.right = crop_left;
+	} else if (rot_mir == SET_PARAM_ROT_270 ||
+		   rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_MIRV |

SET_PARAM_ROT_90)) {

Quoted 10 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 1151

Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +8 -0
+		param->conf_win.top = crop_left;
+		param->conf_win.left = crop_bot;
+		param->conf_win.bottom = crop_right;
+		param->conf_win.right = crop_top;
+	} else if (rot_mir == SET_PARAM_ROT_MIRV ||
+		   rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_180)) {
+		param->conf_win.top = crop_bot;
+		param->conf_win.bottom = crop_top;

put bottom/right here to make code consistent.

OK. I'll fix it.

Quoted 26 lines
Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +7 -0
+	} else if (rot_mir == SET_PARAM_ROT_MIRH ||
+		   rot_mir == (SET_PARAM_ROT_MIRV | SET_PARAM_ROT_180)) {
+		param->conf_win.left = crop_right;
+		param->conf_win.right = crop_left;
+	} else if (rot_mir == (SET_PARAM_ROT_MIRV | SET_PARAM_ROT_90) ||
+		   rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_270)) {
+		param->conf_win.top = crop_left;

drivers/media/platform/chips-media/wave6/wave6-hw.c line 1168

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+		param->conf_win.left = crop_top;
+		param->conf_win.bottom = crop_right;
+		param->conf_win.right = crop_bot;
+	} else if (rot_mir == (SET_PARAM_ROT_MIRV | SET_PARAM_ROT_270) ||
+		   rot_mir == (SET_PARAM_ROT_MIRH | SET_PARAM_ROT_90)) {
+		param->conf_win.top = crop_right;
+		param->conf_win.left = crop_bot;
+		param->conf_win.bottom = crop_left;
+		param->conf_win.right = crop_top;
+	}
	else {
		param->conf_win.top = crop_top;
		param->conf_win.left = crop_left;
		param->conf_win.bottom = crop_bot;
		param->conf_win.right = crop_right;
	}
Quoted 1 line

Avoid overwrite it again at if-else branch.

OK. I'll fix it.

Quoted 44 lines
Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +4 -0
+
+	return 0;
+}
+

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+static void wave6_update_enc_info(struct enc_info *p_enc_info)
+{
+	struct enc_open_param op = p_enc_info->open_param;
+
+	p_enc_info->width = op.pic_width;
+	p_enc_info->height = op.pic_height;
+	p_enc_info->c_fmt_idc = get_chroma_format_idc(op.output_format);
+}
+
+static void wave6_gen_set_param_reg_common(struct enc_info *p_enc_info,

enum codec_std std,

Quoted 28 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +28 -0
+					   struct enc_cmd_set_param_reg *reg)
+{
+	struct enc_open_param *p_open_param = &p_enc_info->open_param;
+	struct enc_codec_param *p_param = &p_open_param->codec_param;
+	unsigned int i;
+	u32 rot_mir = 0;
+
+	switch (p_enc_info->rot_angle) {
+	case ROT_0:
+		break;
+	case ROT_90:
+		rot_mir |= SET_PARAM_ROT_90;
+		break;
+	case ROT_180:
+		rot_mir |= SET_PARAM_ROT_180;
+		break;
+	case ROT_270:
+		rot_mir |= SET_PARAM_ROT_270;
+		break;
+	}
+
+	switch (p_enc_info->mir_dir) {
+	case MIR_NONE:
+		break;
+	case MIR_VER:
+		rot_mir |= SET_PARAM_ROT_MIRV;
+		break;
+	case MIR_HOR:
Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 1216

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+		rot_mir |= SET_PARAM_ROT_MIRH;
+		break;
+	case MIR_HOR_VER:
+		rot_mir |= SET_PARAM_ROT_MIRH | SET_PARAM_ROT_MIRV;
+		break;
+	}

use small convert helper to make this function shorter

OK. I'll address this in v8.

...

Quoted 112 lines
Quoted 1 line

+static void wave6_gen_enc_pic_reg(struct enc_info *p_enc_info, bool

cbcr_interleave, bool nv21,

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +1 -0
+				  struct enc_param *opt, struct

enc_cmd_enc_pic_reg *reg)

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+{
+	struct enc_open_param open = p_enc_info->open_param;
+	struct enc_codec_param param = open.codec_param;
+	bool is_lsb = false;
+	bool is_10bit = false;
+	bool is_3p4b = false;

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +10 -0
+	bool is_cr_first = nv21;
+	u32 c_fmt_idc = get_chroma_format_idc(open.src_format);
+	u32 stride_c = opt->source_frame->stride_c;
+	bool is_ayuv = false;
+	bool is_csc_format = false;
+	bool is_24bit = false;
+	bool is_packed = false;
+	bool is_packed_uv_first = false;
+
+	if ((c_fmt_idc == C_FMT_IDC_YUV422 || c_fmt_idc == C_FMT_IDC_YUV444)

&&

Quoted 91 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +91 -0
+	    p_enc_info->c_fmt_idc == C_FMT_IDC_YUV420)
+		stride_c *= 2;
+
+	switch (open.src_format) {
+	case FORMAT_420:
+	case FORMAT_422:
+	case FORMAT_444:
+	case FORMAT_400:
+	case FORMAT_YUYV:
+	case FORMAT_YVYU:
+	case FORMAT_UYVY:
+	case FORMAT_VYUY:
+		is_lsb = false;
+		is_3p4b = false;
+		break;
+	case FORMAT_420_P10_16BIT_MSB:
+	case FORMAT_422_P10_16BIT_MSB:
+	case FORMAT_444_P10_16BIT_MSB:
+	case FORMAT_400_P10_16BIT_MSB:
+	case FORMAT_YUYV_P10_16BIT_MSB:
+	case FORMAT_YVYU_P10_16BIT_MSB:
+	case FORMAT_UYVY_P10_16BIT_MSB:
+	case FORMAT_VYUY_P10_16BIT_MSB:
+		is_lsb = false;
+		is_10bit = true;
+		is_3p4b = false;
+		break;
+	case FORMAT_420_P10_16BIT_LSB:
+	case FORMAT_422_P10_16BIT_LSB:
+	case FORMAT_444_P10_16BIT_LSB:
+	case FORMAT_400_P10_16BIT_LSB:
+	case FORMAT_YUYV_P10_16BIT_LSB:
+	case FORMAT_YVYU_P10_16BIT_LSB:
+	case FORMAT_UYVY_P10_16BIT_LSB:
+	case FORMAT_VYUY_P10_16BIT_LSB:
+		is_lsb = true;
+		is_10bit = true;
+		is_3p4b = false;
+		break;
+	case FORMAT_420_P10_32BIT_MSB:
+	case FORMAT_422_P10_32BIT_MSB:
+	case FORMAT_444_P10_32BIT_MSB:
+	case FORMAT_400_P10_32BIT_MSB:
+	case FORMAT_YUYV_P10_32BIT_MSB:
+	case FORMAT_YVYU_P10_32BIT_MSB:
+	case FORMAT_UYVY_P10_32BIT_MSB:
+	case FORMAT_VYUY_P10_32BIT_MSB:
+		is_lsb = false;
+		is_10bit = true;
+		is_3p4b = true;
+		break;
+	case FORMAT_420_P10_32BIT_LSB:
+	case FORMAT_422_P10_32BIT_LSB:
+	case FORMAT_444_P10_32BIT_LSB:
+	case FORMAT_400_P10_32BIT_LSB:
+	case FORMAT_YUYV_P10_32BIT_LSB:
+	case FORMAT_YVYU_P10_32BIT_LSB:
+	case FORMAT_UYVY_P10_32BIT_LSB:
+	case FORMAT_VYUY_P10_32BIT_LSB:
+		is_lsb = true;
+		is_10bit = true;
+		is_3p4b = true;
+		break;
+	case FORMAT_RGB_32BIT_PACKED:
+		is_ayuv = false;
+		is_csc_format = true;
+		break;
+	case FORMAT_RGB_P10_32BIT_PACKED:
+		is_ayuv = false;
+		is_csc_format = true;
+		is_10bit = true;
+		break;
+	case FORMAT_YUV444_32BIT_PACKED:
+		is_ayuv = true;
+		is_csc_format = true;
+		break;
+	case FORMAT_YUV444_P10_32BIT_PACKED:
+		is_ayuv = true;
+		is_csc_format = true;
+		is_10bit = true;
+		break;
+	case FORMAT_RGB_24BIT_PACKED:
+		is_ayuv = false;
+		is_csc_format = true;
+		is_24bit = true;
+		break;
+	case FORMAT_YUV444_24BIT_PACKED:
+		is_ayuv = true;
+		is_csc_format = true;
+		is_24bit = true;
+		break;
Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c line 1770

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+	case FORMAT_YUV444_24BIT:
+		is_ayuv = true;
+		break;
+	default:
+		break;
+	}

move to small helper to this kinds of convert functions

OK. I'll address this in v8.

...

Quoted 7 lines
Quoted 1 line

+static inline void wave6_vdi_writel(void __iomem *base, u32 addr, u32

data)

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +2 -0
+{
+	writel(data, base + addr);

consider big amount reg read write, suggest use writel_relaxed() and readl_relaxed()

Same as above - I'll add a relaxed variant rather than change wave6_vdi_writel() itself, so the HOST_INT_REQ and the firmware start keep the barrier.

Quoted 7 lines
Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+}
+
+static inline unsigned int wave6_vdi_readl(void __iomem *base, u32 addr)
+{
+	return readl(base + addr);
+}

...

Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +4 -0
+struct dec_open_param {
+	enum display_mode disp_mode;
+	u32 ext_addr_vcpu: 8;
+	bool is_secure_inst;

you use bit field:
here
is_secure_inst: 1

OK. enc_open_param has the same pattern, I'll change both.

Quoted 4 lines
Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +3 -0
+	u32 inst_priority: 5;
+	struct instance_buffer inst_buffer;
+};

...

Quoted 5 lines
Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +5 -0
+struct vpu_performance_info {
+	ktime_t ts_start;
+	ktime_t ts_first;
+	ktime_t ts_last;
+	s64 latency_first;
Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-hw.c

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-hw.c new file +6 -0
+	s64 latency_max;
+	s64 min_process_time;
+	s64 max_process_time;
+	u64 total_sw_time;
+	u64 total_hw_time;
+};

You can add performance_info later, like trace and dbg.

OK. I'll address this in v8.

Thanks.
Nas.

author
#14 lore raw

PATCH v7 4/9 media: chips-media: wave6: Add v4l2 m2m driver support

In reply to Nas Chung

Add v4l2 m2m stateful driver support for the
Chips&Media Wave6 video codec IP, including both encoder and decoder.

The following features and formats are supported:
Encoder (8bit only):
- Supported codecs: AVC, HEVC
- Supported input formats:
* V4L2_PIX_FMT_YUV420/NV12/NV21
* V4L2_PIX_FMT_YUV422P/NV16/NV61
* V4L2_PIX_FMT_YUYV
* V4L2_PIX_FMT_YUV24/NV24/NV42
* V4L2_PIX_FMT_RGB24
* V4L2_PIX_FMT_P010
* V4L2_PIX_FMT_ARGB32
* V4L2_PIX_FMT_RGBA32
* V4L2_PIX_FMT_XRGB32
* V4L2_PIX_FMT_ARGB2101010
- Supports multiplanar format
- Although 10-bit input formats are supported (e.g., P010, ARGB2101010), encoded output is limited to 8-bit streams
- Supports crop
- Supports extensive v4l2 encoder control

Decoder (8bit only):
- Supported codecs: AVC, HEVC (8bit only)
- Supported output formats: V4L2_PIX_FMT_YUV420/NV12/NV21
- Supports multiplanar format
- Supports scale down
- Supports dynamic resolution change

Note: While other versions of the Wave6 IP may support VP9 decoding and AV1 decoding and encoding those formats are not implemented or validated in this driver at this time.

---
 .../chips-media/wave6/wave6-vpu-dec.c         | 1843 +++++++++++
 .../chips-media/wave6/wave6-vpu-enc.c         | 2764 +++++++++++++++++
 .../chips-media/wave6/wave6-vpu-v4l2.c        |  516 +++
 3 files changed, 5123 insertions(+)
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +1843 -0
new file mode 100644
index 000000000000..4b07a2249db1
@@ -0,0 +1,1843 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+/*
+ * Wave6 series multi-standard codec IP - v4l2 stateful decoder interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#include <linux/pm_runtime.h>
+#include <linux/delay.h>
+#include "wave6-vpu-core.h"
+#include "wave6-vpu-dbg.h"
+#include "wave6-trace.h"
+
+#define VPU_DEC_DEV_NAME "C&M Wave6 VPU decoder"
+#define VPU_DEC_DRV_NAME "wave6-dec"
+
+static const struct vpu_format wave6_vpu_dec_fmt_list[2][6] = {
+	[VPU_FMT_TYPE_CODEC] = {
+		{
+			.v4l2_pix_fmt = V4L2_PIX_FMT_HEVC,
+			.max_width = W6_MAX_DEC_PIC_WIDTH,
+			.min_width = W6_MIN_DEC_PIC_WIDTH,
+			.max_height = W6_MAX_DEC_PIC_HEIGHT,
+			.min_height = W6_MIN_DEC_PIC_HEIGHT,
+			.num_planes = 1,
+		},
+		{
+			.v4l2_pix_fmt = V4L2_PIX_FMT_H264,
+			.max_width = W6_MAX_DEC_PIC_WIDTH,
+			.min_width = W6_MIN_DEC_PIC_WIDTH,
+			.max_height = W6_MAX_DEC_PIC_HEIGHT,
+			.min_height = W6_MIN_DEC_PIC_HEIGHT,
+			.num_planes = 1,
+		},
+	},
+	[VPU_FMT_TYPE_RAW] = {
+		{
+			.v4l2_pix_fmt = V4L2_PIX_FMT_YUV420,
+			.max_width = W6_MAX_DEC_PIC_WIDTH,
+			.min_width = W6_MIN_DEC_PIC_WIDTH,
+			.max_height = W6_MAX_DEC_PIC_HEIGHT,
+			.min_height = W6_MIN_DEC_PIC_HEIGHT,
+			.num_planes = 1,
+			.fb_fmt = FORMAT_420,
+			.endian = VPU_SOURCE_ENDIAN,
+		},
+		{
+			.v4l2_pix_fmt = V4L2_PIX_FMT_NV12,
+			.max_width = W6_MAX_DEC_PIC_WIDTH,
+			.min_width = W6_MIN_DEC_PIC_WIDTH,
+			.max_height = W6_MAX_DEC_PIC_HEIGHT,
+			.min_height = W6_MIN_DEC_PIC_HEIGHT,
+			.num_planes = 1,
+			.fb_fmt = FORMAT_420,
+			.endian = VPU_SOURCE_ENDIAN,
+			.flags = VPU_FMT_FLAG_CBCR_INTERLEAVED,
+		},
+		{
+			.v4l2_pix_fmt = V4L2_PIX_FMT_NV21,
+			.max_width = W6_MAX_DEC_PIC_WIDTH,
+			.min_width = W6_MIN_DEC_PIC_WIDTH,
+			.max_height = W6_MAX_DEC_PIC_HEIGHT,
+			.min_height = W6_MIN_DEC_PIC_HEIGHT,
+			.num_planes = 1,
+			.fb_fmt = FORMAT_420,
+			.endian = VPU_SOURCE_ENDIAN,
+			.flags = VPU_FMT_FLAG_CBCR_INTERLEAVED |
+				 VPU_FMT_FLAG_CRCB_ORDER,
+		},
+		{
+			.v4l2_pix_fmt = V4L2_PIX_FMT_YUV420M,
+			.max_width = W6_MAX_DEC_PIC_WIDTH,
+			.min_width = W6_MIN_DEC_PIC_WIDTH,
+			.max_height = W6_MAX_DEC_PIC_HEIGHT,
+			.min_height = W6_MIN_DEC_PIC_HEIGHT,
+			.num_planes = 3,
+			.fb_fmt = FORMAT_420,
+			.endian = VPU_SOURCE_ENDIAN,
+		},
+		{
+			.v4l2_pix_fmt = V4L2_PIX_FMT_NV12M,
+			.max_width = W6_MAX_DEC_PIC_WIDTH,
+			.min_width = W6_MIN_DEC_PIC_WIDTH,
+			.max_height = W6_MAX_DEC_PIC_HEIGHT,
+			.min_height = W6_MIN_DEC_PIC_HEIGHT,
+			.num_planes = 2,
+			.fb_fmt = FORMAT_420,
+			.endian = VPU_SOURCE_ENDIAN,
+			.flags = VPU_FMT_FLAG_CBCR_INTERLEAVED,
+		},
+		{
+			.v4l2_pix_fmt = V4L2_PIX_FMT_NV21M,
+			.max_width = W6_MAX_DEC_PIC_WIDTH,
+			.min_width = W6_MIN_DEC_PIC_WIDTH,
+			.max_height = W6_MAX_DEC_PIC_HEIGHT,
+			.min_height = W6_MIN_DEC_PIC_HEIGHT,
+			.num_planes = 2,
+			.fb_fmt = FORMAT_420,
+			.endian = VPU_SOURCE_ENDIAN,
+			.flags = VPU_FMT_FLAG_CBCR_INTERLEAVED |
+				 VPU_FMT_FLAG_CRCB_ORDER,
+		},
+	}
+};
+
+static const struct vpu_format *wave6_find_vpu_fmt(unsigned int v4l2_pix_fmt,
+						   enum vpu_fmt_type type)
+{
+	unsigned int index;
+
+	for (index = 0; index < ARRAY_SIZE(wave6_vpu_dec_fmt_list[type]); index++) {
+		if (wave6_vpu_dec_fmt_list[type][index].v4l2_pix_fmt == v4l2_pix_fmt)
+			return &wave6_vpu_dec_fmt_list[type][index];
+	}
+
+	return NULL;
+}
+
+static const struct vpu_format *wave6_find_vpu_fmt_by_idx(unsigned int idx,
+							  enum vpu_fmt_type type)
+{
+	if (idx >= ARRAY_SIZE(wave6_vpu_dec_fmt_list[type]))
+		return NULL;
+
+	if (!wave6_vpu_dec_fmt_list[type][idx].v4l2_pix_fmt)
+		return NULL;
+
+	return &wave6_vpu_dec_fmt_list[type][idx];
+}
+
+static void wave6_vpu_dec_release_fb(struct vpu_instance *inst)
+{
+	int i;
+
+	for (i = 0; i < WAVE6_MAX_FBS; i++) {
+		wave6_vdi_free_dma(&inst->frame_vbuf[i]);
+		memset(&inst->frame_buf[i], 0, sizeof(struct frame_buffer));
+		wave6_vdi_free_dma(&inst->aux_vbuf[AUX_BUF_FBC_Y_TBL][i]);
+		wave6_vdi_free_dma(&inst->aux_vbuf[AUX_BUF_FBC_C_TBL][i]);
+		wave6_vdi_free_dma(&inst->aux_vbuf[AUX_BUF_MV_COL][i]);
+	}
+}
+
+static void wave6_vpu_dec_destroy_instance(struct vpu_instance *inst, bool remove_dbgfs)
+{
+	u32 fail_res = 0;
+	int ret;
+
+	wave6_vpu_disable_instance(inst);
+
+	if (remove_dbgfs)
+		wave6_vpu_remove_dbgfs_file(inst);
+
+	ret = wave6_vpu_dec_close(inst, &fail_res);
+	if (ret) {
+		dev_err(inst->dev->dev, "failed destroy instance: %d (%d)\n",
+			ret, fail_res);
+	}
+
+	scoped_guard(spinlock, &inst->dev->inst_lock)
+		list_del_init(&inst->list);
+	wave6_vpu_dec_release_fb(inst);
+	wave6_vpu_set_instance_state(inst, VPU_INST_STATE_NONE);
+
+	if (!pm_runtime_suspended(inst->dev->dev))
+		pm_runtime_put_sync(inst->dev->dev);
+}
+
+static void wave6_handle_bitstream_buffer(struct vpu_instance *inst)
+{
+	struct vb2_v4l2_buffer *src_buf;
+	u32 src_size = 0;
+	int ret;
+
+	src_buf = v4l2_m2m_next_src_buf(inst->v4l2_fh.m2m_ctx);
+	if (src_buf) {
+		struct vpu_buffer *vpu_buf = wave6_to_vpu_buf(src_buf);
+		dma_addr_t rd_ptr = wave6_get_dma_addr(src_buf, 0);
+
+		if (vpu_buf->consumed) {
+			dev_dbg(inst->dev->dev, "%s: Already consumed buffer\n",
+				__func__);
+			return;
+		}
+
+		vpu_buf->ts_start = ktime_get_raw();
+		vpu_buf->consumed = true;
+		wave6_vpu_dec_set_rd_ptr(inst, rd_ptr, true);
+
+		src_size = vb2_get_plane_payload(&src_buf->vb2_buf, 0);
+	}
+
+	if (!src_size) {
+		dma_addr_t rd = 0, wr = 0;
+
+		wave6_vpu_dec_get_bitstream_buffer(inst, &rd, &wr);
+		wave6_vpu_dec_set_rd_ptr(inst, wr, true);
+	}
+
+	trace_wave6_vpu_dec_pic(inst, src_buf ? src_buf->vb2_buf.index : -1, src_size);
+
+	ret = wave6_vpu_dec_update_bitstream_buffer(inst, src_size);
+	if (ret) {
+		dev_dbg(inst->dev->dev, "%s: Update bitstream buffer fail %d\n",
+			__func__, ret);
+		return;
+	}
+}
+
+static void wave6_update_pix_fmt_cap(struct v4l2_pix_format_mplane *pix_mp,
+				     unsigned int width,
+				     unsigned int height,
+				     bool new_resolution)
+{
+	unsigned int aligned_width;
+
+	if (new_resolution)
+		pix_mp->plane_fmt[0].bytesperline = 0;
+
+	aligned_width = round_up(width, W6_DEC_BUF_ALIGNMENT);
+	wave6_update_pix_fmt(pix_mp, aligned_width, height);
+}
+
+static int wave6_allocate_aux_buffer(struct vpu_instance *inst,
+				     enum aux_buffer_type type,
+				     int num)
+{
+	struct aux_buffer_size_info size_info;
+	unsigned int size;
+	int i, ret;
+
+	size_info.width = inst->src_fmt.width;
+	size_info.height = inst->src_fmt.height;
+	size_info.type = type;
+
+	ret = wave6_vpu_dec_get_aux_buffer_size(inst, size_info, &size);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: Get size fail (type %d)\n", __func__, type);
+		return ret;
+	}
+
+	num = min_t(u32, num, WAVE6_MAX_FBS);
+	for (i = 0; i < num; i++) {
+		inst->aux_vbuf[type][i].size = size;
+		ret = wave6_vdi_alloc_dma(inst->dev->dev, &inst->aux_vbuf[type][i]);
+		if (ret) {
+			dev_err(inst->dev->dev, "%s: Alloc fail (type %d)\n", __func__, type);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+static void wave6_vpu_dec_handle_dst_buffer(struct vpu_instance *inst)
+{
+	struct vb2_v4l2_buffer *dst_buf;
+	struct v4l2_m2m_buffer *v4l2_m2m_buf;
+	struct vpu_buffer *vpu_buf;
+	u32 fb_stride = inst->dst_fmt.plane_fmt[0].bytesperline;
+	u32 luma_size = fb_stride * inst->dst_fmt.height;
+	u32 chroma_size = (fb_stride / 2) * (inst->dst_fmt.height / 2);
+	struct frame_buffer disp_buffer = { };
+	struct dec_seq_info *seq_info = &inst->codec_info->dec_info.seq_info;
+	int consumed_num = wave6_vpu_get_consumed_fb_num(inst);
+	int ret;
+
+	v4l2_m2m_for_each_dst_buf(inst->v4l2_fh.m2m_ctx, v4l2_m2m_buf) {
+		dst_buf = &v4l2_m2m_buf->vb;
+		vpu_buf = wave6_to_vpu_buf(dst_buf);
+
+		if (vpu_buf->consumed)
+			continue;
+
+		if (consumed_num >= WAVE6_MAX_FBS)
+			break;
+
+		if (inst->dst_fmt.num_planes == 1) {
+			disp_buffer.buf_y = wave6_get_dma_addr(dst_buf, 0);
+			disp_buffer.buf_cb = disp_buffer.buf_y + luma_size;
+			disp_buffer.buf_cr = disp_buffer.buf_cb + chroma_size;
+		} else if (inst->dst_fmt.num_planes == 2) {
+			disp_buffer.buf_y = wave6_get_dma_addr(dst_buf, 0);
+			disp_buffer.buf_cb = wave6_get_dma_addr(dst_buf, 1);
+			disp_buffer.buf_cr = disp_buffer.buf_cb + chroma_size;
+		} else if (inst->dst_fmt.num_planes == 3) {
+			disp_buffer.buf_y = wave6_get_dma_addr(dst_buf, 0);
+			disp_buffer.buf_cb = wave6_get_dma_addr(dst_buf, 1);
+			disp_buffer.buf_cr = wave6_get_dma_addr(dst_buf, 2);
+		}
+		disp_buffer.height = inst->src_fmt.height;
+		disp_buffer.stride = fb_stride;
+		disp_buffer.map_type = LINEAR_FRAME_MAP;
+		disp_buffer.luma_bit_depth = seq_info->luma_bit_depth;
+		disp_buffer.chroma_bit_depth = seq_info->chroma_bit_depth;
+		disp_buffer.c_fmt_idc = seq_info->c_fmt_idc;
+
+		ret = wave6_vpu_dec_register_display_buffer(inst, disp_buffer);
+		if (ret) {
+			dev_err(inst->dev->dev, "fail register display buffer %d", ret);
+			break;
+		}
+
+		vpu_buf->consumed = true;
+		consumed_num++;
+	}
+
+	if (!inst->fbc_buf_registered) {
+		ret = wave6_vpu_dec_register_frame_buffer(inst);
+		if (ret)
+			dev_err(inst->dev->dev, "register frame buffer fail %d\n", ret);
+		else
+			inst->fbc_buf_registered = true;
+	}
+}
+
+static enum v4l2_quantization to_v4l2_quantization(u32 video_full_range_flag)
+{
+	switch (video_full_range_flag) {
+	case 0:
+		return V4L2_QUANTIZATION_LIM_RANGE;
+	case 1:
+		return V4L2_QUANTIZATION_FULL_RANGE;
+	default:
+		return V4L2_QUANTIZATION_DEFAULT;
+	}
+}
+
+static enum v4l2_colorspace to_v4l2_colorspace(u32 colour_primaries)
+{
+	switch (colour_primaries) {
+	case 1:
+		return V4L2_COLORSPACE_REC709;
+	case 4:
+		return V4L2_COLORSPACE_470_SYSTEM_M;
+	case 5:
+		return V4L2_COLORSPACE_470_SYSTEM_BG;
+	case 6:
+		return V4L2_COLORSPACE_SMPTE170M;
+	case 7:
+		return V4L2_COLORSPACE_SMPTE240M;
+	case 9:
+		return V4L2_COLORSPACE_BT2020;
+	case 11:
+		return V4L2_COLORSPACE_DCI_P3;
+	default:
+		return V4L2_COLORSPACE_DEFAULT;
+	}
+}
+
+static enum v4l2_xfer_func to_v4l2_xfer_func(u32 transfer_characteristics)
+{
+	switch (transfer_characteristics) {
+	case 1:
+		return V4L2_XFER_FUNC_709;
+	case 6:
+		return V4L2_XFER_FUNC_709;
+	case 7:
+		return V4L2_XFER_FUNC_SMPTE240M;
+	case 8:
+		return V4L2_XFER_FUNC_NONE;
+	case 13:
+		return V4L2_XFER_FUNC_SRGB;
+	case 14:
+		return V4L2_XFER_FUNC_709;
+	case 16:
+		return V4L2_XFER_FUNC_SMPTE2084;
+	default:
+		return V4L2_XFER_FUNC_DEFAULT;
+	}
+}
+
+static enum v4l2_ycbcr_encoding to_v4l2_ycbcr_encoding(u32 matrix_coeffs)
+{
+	switch (matrix_coeffs) {
+	case 1:
+		return V4L2_YCBCR_ENC_709;
+	case 5:
+		return V4L2_YCBCR_ENC_601;
+	case 6:
+		return V4L2_YCBCR_ENC_601;
+	case 7:
+		return V4L2_YCBCR_ENC_SMPTE240M;
+	case 9:
+		return V4L2_YCBCR_ENC_BT2020;
+	case 10:
+		return V4L2_YCBCR_ENC_BT2020_CONST_LUM;
+	default:
+		return V4L2_YCBCR_ENC_DEFAULT;
+	}
+}
+
+static void wave6_update_color_info(struct vpu_instance *inst,
+				    struct dec_seq_info *seq_info)
+{
+	struct color_param *color = &seq_info->color;
+
+	if (!color->video_signal_type_present)
+		goto set_default_all;
+
+	inst->quantization = to_v4l2_quantization(color->color_range);
+
+	if (!color->color_description_present)
+		goto set_default_color;
+
+	inst->colorspace = to_v4l2_colorspace(color->color_primaries);
+	inst->xfer_func = to_v4l2_xfer_func(color->transfer_characteristics);
+	inst->ycbcr_enc = to_v4l2_ycbcr_encoding(color->matrix_coefficients);
+
+	return;
+
+set_default_all:
+	inst->quantization = V4L2_QUANTIZATION_DEFAULT;
+set_default_color:
+	inst->colorspace = V4L2_COLORSPACE_DEFAULT;
+	inst->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
+	inst->xfer_func = V4L2_XFER_FUNC_DEFAULT;
+}
+
+static enum v4l2_mpeg_video_hevc_profile to_v4l2_hevc_profile(u32 profile)
+{
+	switch (profile) {
+	case HEVC_PROFILE_MAIN:
+		return V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN;
+	case HEVC_PROFILE_STILLPICTURE:
+		return V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE;
+	default:
+		return V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN;
+	}
+}
+
+static enum v4l2_mpeg_video_h264_profile to_v4l2_h264_profile(u32 profile)
+{
+	switch (profile) {
+	case H264_PROFILE_BP:
+		return V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
+	case H264_PROFILE_MP:
+		return V4L2_MPEG_VIDEO_H264_PROFILE_MAIN;
+	case H264_PROFILE_EXTENDED:
+		return V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED;
+	case H264_PROFILE_HP:
+		return V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
+	default:
+		return V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
+	}
+}
+
+static void wave6_update_v4l2_ctrls(struct vpu_instance *inst,
+				    struct dec_seq_info *info)
+{
+	struct v4l2_ctrl *ctrl;
+	u32 min_disp_cnt;
+
+	min_disp_cnt = info->frame_buf_delay + 1;
+	ctrl = v4l2_ctrl_find(&inst->v4l2_ctrl_hdl,
+			      V4L2_CID_MIN_BUFFERS_FOR_CAPTURE);
+	if (ctrl)
+		v4l2_ctrl_s_ctrl(ctrl, min_disp_cnt);
+
+	if (inst->src_fmt.pixelformat == V4L2_PIX_FMT_HEVC) {
+		ctrl = v4l2_ctrl_find(&inst->v4l2_ctrl_hdl,
+				      V4L2_CID_MPEG_VIDEO_HEVC_PROFILE);
+		if (ctrl)
+			v4l2_ctrl_s_ctrl(ctrl, to_v4l2_hevc_profile(info->profile));
+	} else if (inst->src_fmt.pixelformat == V4L2_PIX_FMT_H264) {
+		ctrl = v4l2_ctrl_find(&inst->v4l2_ctrl_hdl,
+				      V4L2_CID_MPEG_VIDEO_H264_PROFILE);
+		if (ctrl)
+			v4l2_ctrl_s_ctrl(ctrl, to_v4l2_h264_profile(info->profile));
+	}
+}
+
+static int wave6_vpu_dec_start_decode(struct vpu_instance *inst)
+{
+	struct dec_param pic_param;
+	int ret;
+	u32 fail_res = 0;
+
+	memset(&pic_param, 0, sizeof(struct dec_param));
+
+	wave6_handle_bitstream_buffer(inst);
+	wave6_vpu_dec_handle_dst_buffer(inst);
+
+	ret = wave6_vpu_dec_start_one_frame(inst, &pic_param, &fail_res);
+	if (ret) {
+		struct vb2_v4l2_buffer *src_buf = NULL;
+
+		dev_err(inst->dev->dev, "[%d] %s: fail %d\n", inst->id, __func__, ret);
+		wave6_vpu_set_instance_state(inst, VPU_INST_STATE_STOP);
+
+		src_buf = v4l2_m2m_src_buf_remove(inst->v4l2_fh.m2m_ctx);
+		if (src_buf) {
+			v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
+			inst->sequence++;
+			inst->processed_buf_num++;
+			inst->error_buf_num++;
+		}
+	}
+
+	return ret;
+}
+
+static void wave6_handle_decoded_frame(struct vpu_instance *inst,
+				       struct dec_output_info *info)
+{
+	struct vb2_v4l2_buffer *src_buf;
+	struct vb2_v4l2_buffer *dst_buf;
+	struct vpu_buffer *vpu_buf;
+	enum vb2_buffer_state state;
+
+	state = info->decoding_success ? VB2_BUF_STATE_DONE : VB2_BUF_STATE_ERROR;
+
+	src_buf = v4l2_m2m_next_src_buf(inst->v4l2_fh.m2m_ctx);
+	if (!src_buf) {
+		dev_err(inst->dev->dev, "[%d] decoder can't find src buffer\n", inst->id);
+		return;
+	}
+
+	vpu_buf = wave6_to_vpu_buf(src_buf);
+	if (!vpu_buf || !vpu_buf->consumed) {
+		dev_err(inst->dev->dev, "[%d] src buffer is not consumed\n", inst->id);
+		return;
+	}
+
+	dst_buf = wave6_get_dst_buf_by_addr(inst, info->frame_decoded_addr);
+	if (dst_buf) {
+		struct vpu_buffer *dst_vpu_buf = wave6_to_vpu_buf(dst_buf);
+
+		if (wave6_to_vpu_buf(dst_buf)->used) {
+			dev_warn(inst->dev->dev, "[%d] duplication frame buffer\n", inst->id);
+			inst->sequence++;
+		}
+		v4l2_m2m_buf_copy_metadata(src_buf, dst_buf);
+		dst_vpu_buf->used = true;
+		if (state == VB2_BUF_STATE_ERROR)
+			dst_vpu_buf->error = true;
+		dst_vpu_buf->ts_input = vpu_buf->ts_input;
+		dst_vpu_buf->ts_start = vpu_buf->ts_start;
+		dst_vpu_buf->ts_finish = ktime_get_raw();
+		dst_vpu_buf->hw_time = wave6_vpu_cycle_to_ns(inst->dev, info->cycle.frame_cycle);
+	}
+
+	v4l2_m2m_src_buf_remove_by_buf(inst->v4l2_fh.m2m_ctx, src_buf);
+	if (state == VB2_BUF_STATE_ERROR) {
+		dev_dbg(inst->dev->dev, "[%d] error frame %d\n", inst->id, inst->sequence);
+		inst->error_buf_num++;
+	}
+	v4l2_m2m_buf_done(src_buf, state);
+	inst->processed_buf_num++;
+}
+
+static void wave6_handle_skipped_frame(struct vpu_instance *inst)
+{
+	struct vb2_v4l2_buffer *src_buf;
+	struct vpu_buffer *vpu_buf;
+
+	src_buf = v4l2_m2m_next_src_buf(inst->v4l2_fh.m2m_ctx);
+	if (!src_buf)
+		return;
+
+	vpu_buf = wave6_to_vpu_buf(src_buf);
+	if (!vpu_buf || !vpu_buf->consumed)
+		return;
+
+	dev_dbg(inst->dev->dev, "[%d] skip frame %d\n", inst->id, inst->sequence);
+
+	inst->sequence++;
+	inst->processed_buf_num++;
+	inst->error_buf_num++;
+	v4l2_m2m_src_buf_remove_by_buf(inst->v4l2_fh.m2m_ctx, src_buf);
+	v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
+}
+
+static void wave6_handle_display_frame(struct vpu_instance *inst,
+				       dma_addr_t addr, enum vb2_buffer_state state)
+{
+	struct vb2_v4l2_buffer *dst_buf;
+	struct vpu_buffer *vpu_buf;
+	int i;
+
+	dst_buf = wave6_get_dst_buf_by_addr(inst, addr);
+	if (!dst_buf)
+		return;
+
+	vpu_buf = wave6_to_vpu_buf(dst_buf);
+	if (!vpu_buf->used) {
+		dev_dbg(inst->dev->dev, "[%d] recycle display buffer\n", inst->id);
+		vpu_buf->consumed = false;
+		return;
+	}
+
+	for (i = 0; i < inst->dst_fmt.num_planes; i++)
+		vb2_set_plane_payload(&dst_buf->vb2_buf, i,
+				      inst->dst_fmt.plane_fmt[i].sizeimage);
+
+	vpu_buf->ts_output = ktime_get_raw();
+	wave6_vpu_handle_performance(inst, vpu_buf);
+
+	if (vpu_buf->error)
+		state = VB2_BUF_STATE_ERROR;
+	dst_buf->sequence = inst->sequence++;
+	dst_buf->field = V4L2_FIELD_NONE;
+	if (state == VB2_BUF_STATE_ERROR)
+		dev_dbg(inst->dev->dev, "[%d] discard frame %d\n", inst->id, dst_buf->sequence);
+	v4l2_m2m_dst_buf_remove_by_buf(inst->v4l2_fh.m2m_ctx, dst_buf);
+	v4l2_m2m_buf_done(dst_buf, state);
+}
+
+static void wave6_handle_display_frames(struct vpu_instance *inst,
+					struct dec_output_info *info)
+{
+	int i;
+
+	for (i = 0; i < info->disp_frame_num; i++)
+		wave6_handle_display_frame(inst,
+					   info->disp_frame_addr[i],
+					   VB2_BUF_STATE_DONE);
+}
+
+static void wave6_handle_discard_frames(struct vpu_instance *inst,
+					struct dec_output_info *info)
+{
+	int i;
+
+	for (i = 0; i < info->release_disp_frame_num; i++)
+		wave6_handle_display_frame(inst,
+					   info->release_disp_frame_addr[i],
+					   VB2_BUF_STATE_ERROR);
+}
+
+static void wave6_handle_last_frame(struct vpu_instance *inst,
+				    struct vb2_v4l2_buffer *dst_buf)
+{
+	int i;
+
+	if (!dst_buf) {
+		dst_buf = v4l2_m2m_dst_buf_remove(inst->v4l2_fh.m2m_ctx);
+		if (!dst_buf) {
+			inst->next_buf_last = true;
+			return;
+		}
+	}
+
+	for (i = 0; i < inst->dst_fmt.num_planes; i++)
+		vb2_set_plane_payload(&dst_buf->vb2_buf, i, 0);
+
+	dst_buf->flags |= V4L2_BUF_FLAG_LAST;
+	dst_buf->field = V4L2_FIELD_NONE;
+	v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
+
+	if (inst->state != VPU_INST_STATE_INIT_SEQ) {
+		dev_dbg(inst->dev->dev, "[%d] eos\n", inst->id);
+		inst->eos = true;
+		v4l2_m2m_set_src_buffered(inst->v4l2_fh.m2m_ctx, false);
+	}
+}
+
+static void wave6_vpu_dec_retry_one_frame(struct vpu_instance *inst)
+{
+	struct vb2_v4l2_buffer *src_buf;
+	struct vpu_buffer *vpu_buf;
+
+	src_buf = v4l2_m2m_next_src_buf(inst->v4l2_fh.m2m_ctx);
+	if (!src_buf)
+		return;
+
+	vpu_buf = wave6_to_vpu_buf(src_buf);
+	vpu_buf->consumed = false;
+}
+
+static void wave6_vpu_dec_handle_source_change(struct vpu_instance *inst)
+{
+	static const struct v4l2_event vpu_event_src_ch = {
+		.type = V4L2_EVENT_SOURCE_CHANGE,
+		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
+	};
+	struct dec_seq_info *info = &inst->codec_info->dec_info.seq_info;
+
+	dev_dbg(inst->dev->dev, "pic size %dx%d profile %d, min_fb_cnt : %d | min_disp_cnt : %d\n",
+		info->pic_width, info->pic_height,
+		info->profile, info->min_frame_buffer_count, info->frame_buf_delay);
+
+	wave6_vpu_dec_retry_one_frame(inst);
+	wave6_vpu_dec_reset_frame_buffer_info(inst);
+	inst->fbc_buf_registered = false;
+
+	wave6_vpu_set_instance_state(inst, VPU_INST_STATE_INIT_SEQ);
+
+	inst->crop.left = info->pic_crop_rect.left;
+	inst->crop.top = info->pic_crop_rect.top;
+	inst->crop.width = info->pic_crop_rect.right - inst->crop.left;
+	inst->crop.height = info->pic_crop_rect.bottom - inst->crop.top;
+
+	wave6_update_v4l2_ctrls(inst, info);
+	wave6_update_color_info(inst, info);
+	wave6_update_pix_fmt(&inst->src_fmt, info->pic_width, info->pic_height);
+	wave6_update_pix_fmt_cap(&inst->dst_fmt,
+				 info->pic_width, info->pic_height,
+				 true);
+
+	trace_wave6_vpu_source_change(inst, info);
+
+	v4l2_event_queue_fh(&inst->v4l2_fh, &vpu_event_src_ch);
+}
+
+static void wave6_vpu_dec_handle_decoding_warn_error(struct vpu_instance *inst,
+						     struct dec_output_info *info)
+{
+	if (info->warn_info)
+		dev_dbg(inst->dev->dev, "[%d] decoding %d warning 0x%x\n",
+			inst->id, inst->processed_buf_num, info->warn_info);
+
+	if (info->error_reason)
+		dev_dbg(inst->dev->dev, "[%d] decoding %d error 0x%x\n",
+			inst->id, inst->processed_buf_num, info->error_reason);
+}
+
+static void wave6_vpu_dec_finish_decode(struct vpu_instance *inst, bool error)
+{
+	struct dec_output_info info;
+	struct v4l2_m2m_ctx *m2m_ctx = inst->v4l2_fh.m2m_ctx;
+	int ret;
+
+	memset(&info, 0, sizeof(info));
+
+	ret = wave6_vpu_dec_get_output_info(inst, &info);
+	if (ret)
+		goto finish_decode;
+
+	trace_wave6_vpu_dec_done(inst, &info);
+
+	dev_dbg(inst->dev->dev, "dec %d dis %d noti_flag %d stream_end %d\n",
+		info.frame_decoded, info.frame_display,
+		info.notification_flags, info.stream_end);
+
+	if (info.notification_flags & DEC_NOTI_FLAG_NO_FB) {
+		wave6_vpu_dec_retry_one_frame(inst);
+		goto finish_decode;
+	}
+
+	if (info.notification_flags & DEC_NOTI_FLAG_SEQ_CHANGE) {
+		v4l2_m2m_mark_stopped(m2m_ctx);
+
+		if (info.frame_display)
+			wave6_handle_display_frames(inst, &info);
+
+		if (info.release_disp_frame_num)
+			wave6_handle_discard_frames(inst, &info);
+
+		wave6_vpu_dec_handle_source_change(inst);
+
+		wave6_handle_last_frame(inst, NULL);
+
+		goto finish_decode;
+	}
+
+	wave6_vpu_dec_handle_decoding_warn_error(inst, &info);
+
+	if (info.frame_decoded)
+		wave6_handle_decoded_frame(inst, &info);
+	else
+		wave6_handle_skipped_frame(inst);
+
+	if (info.frame_display)
+		wave6_handle_display_frames(inst, &info);
+
+	if (info.release_disp_frame_num)
+		wave6_handle_discard_frames(inst, &info);
+
+	if (info.stream_end && !inst->eos)
+		wave6_handle_last_frame(inst, NULL);
+
+finish_decode:
+	wave6_vpu_finish_job(inst);
+}
+
+static int wave6_vpu_dec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
+{
+	strscpy(cap->driver, VPU_DEC_DRV_NAME, sizeof(cap->driver));
+	strscpy(cap->card, VPU_DEC_DRV_NAME, sizeof(cap->card));
+	strscpy(cap->bus_info, "platform:" VPU_DEC_DRV_NAME, sizeof(cap->bus_info));
+
+	return 0;
+}
+
+static int wave6_vpu_dec_enum_framesizes(struct file *f, void *fh, struct v4l2_frmsizeenum *fsize)
+{
+	const struct vpu_format *vpu_fmt;
+
+	if (fsize->index)
+		return -EINVAL;
+
+	vpu_fmt = wave6_find_vpu_fmt(fsize->pixel_format, VPU_FMT_TYPE_CODEC);
+	if (!vpu_fmt) {
+		vpu_fmt = wave6_find_vpu_fmt(fsize->pixel_format, VPU_FMT_TYPE_RAW);
+		if (!vpu_fmt)
+			return -EINVAL;
+	}
+
+	fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
+	fsize->stepwise.min_width = vpu_fmt->min_width;
+	fsize->stepwise.max_width = vpu_fmt->max_width;
+	fsize->stepwise.step_width = W6_DEC_PIC_SIZE_STEP;
+	fsize->stepwise.min_height = vpu_fmt->min_height;
+	fsize->stepwise.max_height = vpu_fmt->max_height;
+	fsize->stepwise.step_height = W6_DEC_PIC_SIZE_STEP;
+
+	return 0;
+}
+
+static int wave6_vpu_dec_enum_fmt_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
+{
+	const struct vpu_format *vpu_fmt;
+
+	vpu_fmt = wave6_find_vpu_fmt_by_idx(f->index, VPU_FMT_TYPE_RAW);
+	if (!vpu_fmt)
+		return -EINVAL;
+
+	f->pixelformat = vpu_fmt->v4l2_pix_fmt;
+	f->flags = 0;
+
+	return 0;
+}
+
+static int wave6_vpu_dec_try_fmt_cap(struct file *file, void *fh, struct v4l2_format *f)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	const struct vpu_format *vpu_fmt;
+	int width, height;
+
+	dev_dbg(inst->dev->dev, "%s: 4cc %d w %d h %d plane %d colorspace %d\n",
+		__func__, pix_mp->pixelformat, pix_mp->width, pix_mp->height,
+		pix_mp->num_planes, pix_mp->colorspace);
+
+	if (!V4L2_TYPE_IS_CAPTURE(f->type))
+		return -EINVAL;
+
+	vpu_fmt = wave6_find_vpu_fmt(pix_mp->pixelformat, VPU_FMT_TYPE_RAW);
+	if (!vpu_fmt) {
+		width = inst->dst_fmt.width;
+		height = inst->dst_fmt.height;
+		pix_mp->pixelformat = inst->dst_fmt.pixelformat;
+		pix_mp->num_planes = inst->dst_fmt.num_planes;
+	} else {
+		width = clamp(pix_mp->width,
+			      vpu_fmt->min_width,
+			      round_up(inst->src_fmt.width, W6_DEC_BUF_ALIGNMENT));
+		height = clamp(pix_mp->height,
+			       vpu_fmt->min_height, inst->src_fmt.height);
+		pix_mp->pixelformat = vpu_fmt->v4l2_pix_fmt;
+		pix_mp->num_planes = vpu_fmt->num_planes;
+	}
+
+	if (inst->state >= VPU_INST_STATE_INIT_SEQ) {
+		width = inst->dst_fmt.width;
+		height = inst->dst_fmt.height;
+	}
+
+	wave6_update_pix_fmt_cap(pix_mp, width, height, false);
+	pix_mp->colorspace = inst->colorspace;
+	pix_mp->ycbcr_enc = inst->ycbcr_enc;
+	pix_mp->quantization = inst->quantization;
+	pix_mp->xfer_func = inst->xfer_func;
+
+	return 0;
+}
+
+static int wave6_vpu_dec_s_fmt_cap(struct file *file, void *fh, struct v4l2_format *f)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	const struct vpu_format *vpu_fmt;
+	struct vb2_queue *q;
+	int i, ret;
+
+	dev_dbg(inst->dev->dev, "%s: 4cc %d w %d h %d plane %d colorspace %d\n",
+		__func__, pix_mp->pixelformat, pix_mp->width, pix_mp->height,
+		pix_mp->num_planes, pix_mp->colorspace);
+
+	q = v4l2_m2m_get_vq(inst->v4l2_fh.m2m_ctx, f->type);
+	if (vb2_is_busy(q))
+		return -EBUSY;
+
+	ret = wave6_vpu_dec_try_fmt_cap(file, fh, f);
+	if (ret)
+		return ret;
+
+	vpu_fmt = wave6_find_vpu_fmt(pix_mp->pixelformat, VPU_FMT_TYPE_RAW);
+	if (!vpu_fmt)
+		return -EINVAL;
+
+	inst->dst_fmt.width = pix_mp->width;
+	inst->dst_fmt.height = pix_mp->height;
+	inst->dst_fmt.pixelformat = pix_mp->pixelformat;
+	inst->dst_fmt.field = pix_mp->field;
+	inst->dst_fmt.flags = pix_mp->flags;
+	inst->dst_fmt.num_planes = pix_mp->num_planes;
+	for (i = 0; i < inst->dst_fmt.num_planes; i++) {
+		inst->dst_fmt.plane_fmt[i].bytesperline = pix_mp->plane_fmt[i].bytesperline;
+		inst->dst_fmt.plane_fmt[i].sizeimage = pix_mp->plane_fmt[i].sizeimage;
+	}
+
+	inst->cbcr_interleave = !!(vpu_fmt->flags & VPU_FMT_FLAG_CBCR_INTERLEAVED);
+	inst->nv21 = !!(vpu_fmt->flags & VPU_FMT_FLAG_CRCB_ORDER);
+
+	return 0;
+}
+
+static int wave6_vpu_dec_g_fmt_cap(struct file *file, void *fh, struct v4l2_format *f)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	int i;
+
+	pix_mp->width = inst->dst_fmt.width;
+	pix_mp->height = inst->dst_fmt.height;
+	pix_mp->pixelformat = inst->dst_fmt.pixelformat;
+	pix_mp->field = inst->dst_fmt.field;
+	pix_mp->flags = inst->dst_fmt.flags;
+	pix_mp->num_planes = inst->dst_fmt.num_planes;
+	for (i = 0; i < pix_mp->num_planes; i++) {
+		pix_mp->plane_fmt[i].bytesperline = inst->dst_fmt.plane_fmt[i].bytesperline;
+		pix_mp->plane_fmt[i].sizeimage = inst->dst_fmt.plane_fmt[i].sizeimage;
+	}
+
+	pix_mp->colorspace = inst->colorspace;
+	pix_mp->ycbcr_enc = inst->ycbcr_enc;
+	pix_mp->quantization = inst->quantization;
+	pix_mp->xfer_func = inst->xfer_func;
+
+	return 0;
+}
+
+static int wave6_vpu_dec_enum_fmt_out(struct file *file, void *fh, struct v4l2_fmtdesc *f)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	const struct vpu_format *vpu_fmt;
+
+	dev_dbg(inst->dev->dev, "%s: index %d\n", __func__, f->index);
+
+	vpu_fmt = wave6_find_vpu_fmt_by_idx(f->index, VPU_FMT_TYPE_CODEC);
+	if (!vpu_fmt)
+		return -EINVAL;
+
+	f->pixelformat = vpu_fmt->v4l2_pix_fmt;
+	f->flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED;
+
+	return 0;
+}
+
+static int wave6_vpu_dec_try_fmt_out(struct file *file, void *fh, struct v4l2_format *f)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	const struct vpu_format *vpu_fmt;
+	int width, height;
+
+	dev_dbg(inst->dev->dev, "%s: 4cc %d w %d h %d plane %d colorspace %d\n",
+		__func__, pix_mp->pixelformat, pix_mp->width, pix_mp->height,
+		pix_mp->num_planes, pix_mp->colorspace);
+
+	if (!V4L2_TYPE_IS_OUTPUT(f->type))
+		return -EINVAL;
+
+	vpu_fmt = wave6_find_vpu_fmt(pix_mp->pixelformat, VPU_FMT_TYPE_CODEC);
+	if (!vpu_fmt) {
+		width = inst->src_fmt.width;
+		height = inst->src_fmt.height;
+		pix_mp->pixelformat = inst->src_fmt.pixelformat;
+		pix_mp->num_planes = inst->src_fmt.num_planes;
+	} else {
+		width = pix_mp->width;
+		height = pix_mp->height;
+		pix_mp->pixelformat = vpu_fmt->v4l2_pix_fmt;
+		pix_mp->num_planes = vpu_fmt->num_planes;
+	}
+
+	wave6_update_pix_fmt(pix_mp, width, height);
+	pix_mp->colorspace = inst->colorspace;
+	pix_mp->ycbcr_enc = inst->ycbcr_enc;
+	pix_mp->quantization = inst->quantization;
+	pix_mp->xfer_func = inst->xfer_func;
+
+	return 0;
+}
+
+static int wave6_vpu_dec_s_fmt_out(struct file *file, void *fh, struct v4l2_format *f)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	struct v4l2_pix_format_mplane in_pix_mp = f->fmt.pix_mp;
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	struct vb2_queue *q;
+	int i, ret;
+
+	dev_dbg(inst->dev->dev, "%s: 4cc %d w %d h %d plane %d colorspace %d\n",
+		__func__, pix_mp->pixelformat, pix_mp->width, pix_mp->height,
+		pix_mp->num_planes, pix_mp->colorspace);
+
+	q = v4l2_m2m_get_vq(inst->v4l2_fh.m2m_ctx, f->type);
+	if (vb2_is_busy(q))
+		return -EBUSY;
+
+	ret = wave6_vpu_dec_try_fmt_out(file, fh, f);
+	if (ret)
+		return ret;
+
+	pix_mp->colorspace = in_pix_mp.colorspace;
+	pix_mp->ycbcr_enc = in_pix_mp.ycbcr_enc;
+	pix_mp->quantization = in_pix_mp.quantization;
+	pix_mp->xfer_func = in_pix_mp.xfer_func;
+
+	inst->src_fmt.width = pix_mp->width;
+	inst->src_fmt.height = pix_mp->height;
+	inst->src_fmt.pixelformat = pix_mp->pixelformat;
+	inst->src_fmt.field = pix_mp->field;
+	inst->src_fmt.flags = pix_mp->flags;
+	inst->src_fmt.num_planes = pix_mp->num_planes;
+	for (i = 0; i < inst->src_fmt.num_planes; i++) {
+		inst->src_fmt.plane_fmt[i].bytesperline = pix_mp->plane_fmt[i].bytesperline;
+		inst->src_fmt.plane_fmt[i].sizeimage = pix_mp->plane_fmt[i].sizeimage;
+	}
+
+	inst->colorspace = pix_mp->colorspace;
+	inst->ycbcr_enc = pix_mp->ycbcr_enc;
+	inst->quantization = pix_mp->quantization;
+	inst->xfer_func = pix_mp->xfer_func;
+
+	wave6_update_pix_fmt_cap(&inst->dst_fmt,
+				 pix_mp->width, pix_mp->height,
+				 true);
+
+	return 0;
+}
+
+static int wave6_vpu_dec_g_fmt_out(struct file *file, void *fh, struct v4l2_format *f)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	int i;
+
+	pix_mp->width = inst->src_fmt.width;
+	pix_mp->height = inst->src_fmt.height;
+	pix_mp->pixelformat = inst->src_fmt.pixelformat;
+	pix_mp->field = inst->src_fmt.field;
+	pix_mp->flags = inst->src_fmt.flags;
+	pix_mp->num_planes = inst->src_fmt.num_planes;
+	for (i = 0; i < pix_mp->num_planes; i++) {
+		pix_mp->plane_fmt[i].bytesperline = inst->src_fmt.plane_fmt[i].bytesperline;
+		pix_mp->plane_fmt[i].sizeimage = inst->src_fmt.plane_fmt[i].sizeimage;
+	}
+
+	pix_mp->colorspace = inst->colorspace;
+	pix_mp->ycbcr_enc = inst->ycbcr_enc;
+	pix_mp->quantization = inst->quantization;
+	pix_mp->xfer_func = inst->xfer_func;
+
+	return 0;
+}
+
+static int wave6_vpu_dec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+
+	dev_dbg(inst->dev->dev, "%s: type %d target %d\n",
+		__func__, s->type, s->target);
+
+	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
+	    s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
+		return -EINVAL;
+
+	switch (s->target) {
+	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
+		s->r.left = 0;
+		s->r.top = 0;
+		s->r.width = inst->dst_fmt.width;
+		s->r.height = inst->dst_fmt.height;
+		break;
+	case V4L2_SEL_TGT_COMPOSE_PADDED:
+	case V4L2_SEL_TGT_COMPOSE:
+		s->r.left = 0;
+		s->r.top = 0;
+		if (inst->scaler_info.enable) {
+			s->r.width = inst->scaler_info.width;
+			s->r.height = inst->scaler_info.height;
+		} else if (inst->crop.width && inst->crop.height) {
+			s->r = inst->crop;
+		} else {
+			s->r.width = inst->src_fmt.width;
+			s->r.height = inst->src_fmt.height;
+		}
+		break;
+	case V4L2_SEL_TGT_CROP:
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+	case V4L2_SEL_TGT_CROP_BOUNDS:
+	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
+		s->r.left = 0;
+		s->r.top = 0;
+		s->r.width = inst->src_fmt.width;
+		s->r.height = inst->src_fmt.height;
+		if (inst->crop.width && inst->crop.height)
+			s->r = inst->crop;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int wave6_vpu_dec_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	int step = 4;
+	int scale_width, scale_height;
+	int min_scale_width, min_scale_height;
+
+	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
+	    s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
+		return -EINVAL;
+
+	if (s->target != V4L2_SEL_TGT_COMPOSE)
+		return -EINVAL;
+
+	if (!(s->flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)))
+		s->flags |= V4L2_SEL_FLAG_LE;
+
+	scale_width = clamp(s->r.width, W6_MIN_DEC_PIC_WIDTH,
+			    round_up(inst->src_fmt.width, W6_DEC_BUF_ALIGNMENT));
+	scale_height = clamp(s->r.height, W6_MIN_DEC_PIC_HEIGHT,
+			     inst->src_fmt.height);
+	if (s->flags & V4L2_SEL_FLAG_GE) {
+		scale_width = round_up(scale_width, step);
+		scale_height = round_up(scale_height, step);
+	}
+	if (s->flags & V4L2_SEL_FLAG_LE) {
+		scale_width = round_down(scale_width, step);
+		scale_height = round_down(scale_height, step);
+	}
+
+	if (scale_width < inst->src_fmt.width ||
+	    scale_height < inst->src_fmt.height)
+		inst->scaler_info.enable = true;
+
+	if (inst->scaler_info.enable) {
+		min_scale_width = ALIGN((inst->src_fmt.width / 8), step);
+		min_scale_height = ALIGN((inst->src_fmt.height / 8), step);
+
+		if (scale_width < W6_MIN_DEC_PIC_WIDTH)
+			scale_width = W6_MIN_DEC_PIC_WIDTH;
+		if (scale_width < min_scale_width)
+			scale_width = min_scale_width;
+		if (scale_height < W6_MIN_DEC_PIC_HEIGHT)
+			scale_height = W6_MIN_DEC_PIC_HEIGHT;
+		if (scale_height < min_scale_height)
+			scale_height = min_scale_height;
+
+		inst->scaler_info.width = scale_width;
+		inst->scaler_info.height = scale_height;
+	}
+
+	s->r.left = 0;
+	s->r.top = 0;
+	s->r.width = scale_width;
+	s->r.height = scale_height;
+
+	return 0;
+}
+
+static int wave6_vpu_dec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dc)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	int ret;
+
+	dev_dbg(inst->dev->dev, "%s: cmd %d\n", __func__, dc->cmd);
+
+	ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, dc);
+	if (ret)
+		return ret;
+
+	switch (dc->cmd) {
+	case V4L2_DEC_CMD_STOP:
+		v4l2_m2m_set_src_buffered(inst->v4l2_fh.m2m_ctx, true);
+		v4l2_m2m_try_schedule(inst->v4l2_fh.m2m_ctx);
+		break;
+	case V4L2_DEC_CMD_START:
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static const struct v4l2_ioctl_ops wave6_vpu_dec_ioctl_ops = {
+	.vidioc_querycap = wave6_vpu_dec_querycap,
+	.vidioc_enum_framesizes = wave6_vpu_dec_enum_framesizes,
+
+	.vidioc_enum_fmt_vid_cap = wave6_vpu_dec_enum_fmt_cap,
+	.vidioc_s_fmt_vid_cap_mplane = wave6_vpu_dec_s_fmt_cap,
+	.vidioc_g_fmt_vid_cap_mplane = wave6_vpu_dec_g_fmt_cap,
+	.vidioc_try_fmt_vid_cap_mplane = wave6_vpu_dec_try_fmt_cap,
+
+	.vidioc_enum_fmt_vid_out = wave6_vpu_dec_enum_fmt_out,
+	.vidioc_s_fmt_vid_out_mplane = wave6_vpu_dec_s_fmt_out,
+	.vidioc_g_fmt_vid_out_mplane = wave6_vpu_dec_g_fmt_out,
+	.vidioc_try_fmt_vid_out_mplane = wave6_vpu_dec_try_fmt_out,
+
+	.vidioc_g_selection = wave6_vpu_dec_g_selection,
+	.vidioc_s_selection = wave6_vpu_dec_s_selection,
+
+	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
+	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
+	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
+	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
+	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
+	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
+	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
+	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
+	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
+
+	.vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
+	.vidioc_decoder_cmd = wave6_vpu_dec_decoder_cmd,
+
+	.vidioc_subscribe_event = wave6_vpu_subscribe_event,
+	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
+};
+
+static int wave6_vpu_dec_s_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct vpu_instance *inst = wave6_ctrl_to_vpu_inst(ctrl);
+
+	trace_wave6_vpu_s_ctrl(inst, ctrl);
+
+	dev_dbg(inst->dev->dev, "%s: name %s value %d\n",
+		__func__, ctrl->name, ctrl->val);
+
+	switch (ctrl->id) {
+	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
+		inst->disp_mode = ctrl->val;
+		break;
+	case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY:
+	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
+	case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
+	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static const struct v4l2_ctrl_ops wave6_vpu_dec_ctrl_ops = {
+	.s_ctrl = wave6_vpu_dec_s_ctrl,
+};
+
+static void wave6_set_dec_openparam(struct dec_open_param *open_param,
+				    struct vpu_instance *inst)
+{
+	open_param->inst_buffer.temp_base = inst->dev->temp_vbuf.daddr;
+	open_param->inst_buffer.temp_size = inst->dev->temp_vbuf.size;
+	open_param->disp_mode = inst->disp_mode;
+}
+
+static int wave6_vpu_dec_create_instance(struct vpu_instance *inst)
+{
+	int ret;
+	struct dec_open_param open_param;
+
+	memset(&open_param, 0, sizeof(struct dec_open_param));
+
+	wave6_vpu_record_performance_timestamps(inst);
+
+	wave6_vpu_core_activate(inst->dev);
+	ret = pm_runtime_resume_and_get(inst->dev->dev);
+	if (ret) {
+		dev_err(inst->dev->dev, "runtime_resume failed %d\n", ret);
+		return ret;
+	}
+
+	inst->std = wave6_to_codec_std(inst->type, inst->src_fmt.pixelformat);
+	if (inst->std == STD_UNKNOWN) {
+		dev_err(inst->dev->dev, "unsupported pixelformat: %.4s\n",
+			(char *)&inst->src_fmt.pixelformat);
+		ret = -EINVAL;
+		goto error_pm;
+	}
+
+	wave6_set_dec_openparam(&open_param, inst);
+
+	ret = wave6_vpu_dec_open(inst, &open_param);
+	if (ret) {
+		dev_err(inst->dev->dev, "failed create instance : %d\n", ret);
+		goto error_pm;
+	}
+
+	dev_dbg(inst->dev->dev, "[%d] decoder\n", inst->id);
+
+	scoped_guard(spinlock, &inst->dev->inst_lock)
+		list_add_tail(&inst->list, &inst->dev->instances);
+	wave6_vpu_enable_instance(inst);
+	wave6_vpu_create_dbgfs_file(inst);
+	wave6_vpu_set_instance_state(inst, VPU_INST_STATE_OPEN);
+
+	return 0;
+
+error_pm:
+	pm_runtime_put_sync(inst->dev->dev);
+
+	return ret;
+}
+
+static int wave6_vpu_dec_prepare_fb(struct vpu_instance *inst)
+{
+	int ret;
+	unsigned int i;
+	unsigned int fb_num;
+	unsigned int mv_num;
+	unsigned int fb_stride;
+	unsigned int fb_height;
+	unsigned int luma_size;
+	unsigned int chroma_size;
+	struct dec_seq_info *seq_info = &inst->codec_info->dec_info.seq_info;
+
+	fb_num = seq_info->min_frame_buffer_count;
+	mv_num = seq_info->req_mv_buffer_count;
+
+	fb_stride = ALIGN(inst->src_fmt.width, W6_FBC_BUF_ALIGNMENT);
+	fb_height = ALIGN(inst->src_fmt.height, W6_FBC_BUF_ALIGNMENT);
+
+	luma_size = fb_stride * fb_height;
+	chroma_size = ALIGN(fb_stride / 2, W6_FBC_BUF_ALIGNMENT) * fb_height;
+
+	for (i = 0; i < fb_num; i++) {
+		struct frame_buffer *frame = &inst->frame_buf[i];
+		struct vpu_buf *vframe = &inst->frame_vbuf[i];
+
+		vframe->size = luma_size + chroma_size;
+		ret = wave6_vdi_alloc_dma(inst->dev->dev, vframe);
+		if (ret) {
+			dev_err(inst->dev->dev, "alloc FBC buffer fail : %zu\n",
+				vframe->size);
+			goto error;
+		}
+
+		frame->buf_y = vframe->daddr;
+		frame->buf_cb = vframe->daddr + luma_size;
+		frame->buf_cr = (dma_addr_t)-1;
+		frame->stride = fb_stride;
+		frame->height = fb_height;
+		frame->map_type = COMPRESSED_FRAME_MAP;
+	}
+
+	ret = wave6_allocate_aux_buffer(inst, AUX_BUF_FBC_Y_TBL, fb_num);
+	if (ret)
+		goto error;
+
+	ret = wave6_allocate_aux_buffer(inst, AUX_BUF_FBC_C_TBL, fb_num);
+	if (ret)
+		goto error;
+
+	ret = wave6_allocate_aux_buffer(inst, AUX_BUF_MV_COL, mv_num);
+	if (ret)
+		goto error;
+
+	wave6_vpu_set_instance_state(inst, VPU_INST_STATE_PIC_RUN);
+
+	wave6_vpu_record_performance_timestamps(inst);
+
+	return 0;
+
+error:
+	wave6_vpu_dec_release_fb(inst);
+
+	return ret;
+}
+
+static int wave6_vpu_dec_queue_setup(struct vb2_queue *q, unsigned int *num_buffers,
+				     unsigned int *num_planes, unsigned int sizes[],
+				     struct device *alloc_devs[])
+{
+	struct vpu_instance *inst = vb2_get_drv_priv(q);
+	struct v4l2_pix_format_mplane inst_format =
+		(V4L2_TYPE_IS_OUTPUT(q->type)) ? inst->src_fmt : inst->dst_fmt;
+	unsigned int i;
+
+	dev_dbg(inst->dev->dev, "%s: num_buffers %d num_planes %d type %d\n",
+		__func__, *num_buffers, *num_planes, q->type);
+
+	if (*num_planes) {
+		if (inst_format.num_planes != *num_planes)
+			return -EINVAL;
+
+		for (i = 0; i < *num_planes; i++) {
+			if (sizes[i] < inst_format.plane_fmt[i].sizeimage)
+				return -EINVAL;
+		}
+	} else {
+		*num_planes = inst_format.num_planes;
+		for (i = 0; i < *num_planes; i++) {
+			sizes[i] = inst_format.plane_fmt[i].sizeimage;
+			dev_dbg(inst->dev->dev, "size[%d] : %d\n", i, sizes[i]);
+		}
+
+		if (V4L2_TYPE_IS_CAPTURE(q->type)) {
+			struct v4l2_ctrl *ctrl;
+			unsigned int min_disp_cnt = 0;
+
+			ctrl = v4l2_ctrl_find(&inst->v4l2_ctrl_hdl,
+					      V4L2_CID_MIN_BUFFERS_FOR_CAPTURE);
+			if (ctrl)
+				min_disp_cnt = v4l2_ctrl_g_ctrl(ctrl);
+
+			*num_buffers = max(*num_buffers, min_disp_cnt);
+
+			if (*num_buffers > WAVE6_MAX_FBS)
+				*num_buffers = min_disp_cnt;
+		}
+	}
+
+	if (V4L2_TYPE_IS_OUTPUT(q->type) && inst->state == VPU_INST_STATE_SEEK)
+		wave6_vpu_dec_destroy_instance(inst, true);
+
+	return 0;
+}
+
+static int wave6_vpu_dec_seek_header(struct vpu_instance *inst)
+{
+	struct dec_seq_info seq_info;
+	int ret;
+
+	memset(&seq_info, 0, sizeof(struct dec_seq_info));
+
+	ret = wave6_vpu_dec_issue_seq_init(inst);
+	if (ret) {
+		dev_err(inst->dev->dev, "failed wave6_vpu_dec_issue_seq_init %d\n", ret);
+		return ret;
+	}
+
+	ret = wave6_vpu_wait_interrupt(inst, W6_VPU_TIMEOUT);
+	if (ret) {
+		dev_err(inst->dev->dev, "failed to call vpu_wait_interrupt()\n");
+		return ret;
+	}
+
+	ret = wave6_vpu_dec_complete_seq_init(inst, &seq_info);
+	if (ret) {
+		dev_err(inst->dev->dev, "vpu_dec_complete_seq_init: %d, reason : 0x%x\n",
+			ret, seq_info.err_reason);
+		if ((seq_info.err_reason & WAVE6_SYSERR_NOT_SUPPORT) ||
+		    (seq_info.err_reason & WAVE6_SYSERR_NOT_SUPPORT_PROFILE)) {
+			ret = -EINVAL;
+		} else if ((seq_info.err_reason & HEVC_ETCERR_INIT_SEQ_SPS_NOT_FOUND) ||
+			   (seq_info.err_reason & AVC_ETCERR_INIT_SEQ_SPS_NOT_FOUND)) {
+			wave6_handle_skipped_frame(inst);
+			ret = 0;
+		}
+	} else {
+		wave6_vpu_dec_handle_source_change(inst);
+		inst->v4l2_fh.m2m_ctx->ignore_cap_streaming = false;
+		v4l2_m2m_set_dst_buffered(inst->v4l2_fh.m2m_ctx, false);
+		if (vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->v4l2_fh.m2m_ctx)))
+			wave6_handle_last_frame(inst, NULL);
+	}
+
+	return ret;
+}
+
+static void wave6_vpu_dec_prepare_decode(struct vpu_instance *inst)
+{
+	int ret = 0;
+
+	wave6_handle_bitstream_buffer(inst);
+	if (inst->state == VPU_INST_STATE_OPEN) {
+		ret = wave6_vpu_dec_seek_header(inst);
+		if (ret) {
+			vb2_queue_error(v4l2_m2m_get_src_vq(inst->v4l2_fh.m2m_ctx));
+			vb2_queue_error(v4l2_m2m_get_dst_vq(inst->v4l2_fh.m2m_ctx));
+		}
+	}
+}
+
+static void wave6_vpu_dec_buf_queue_src(struct vb2_buffer *vb)
+{
+	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+	struct vpu_buffer *vpu_buf = wave6_to_vpu_buf(vbuf);
+	struct vpu_instance *inst = vb2_get_drv_priv(vb->vb2_queue);
+
+	dev_dbg(inst->dev->dev, "type %4d index %4d size[0] %4ld size[1] : %4ld | size[2] : %4ld\n",
+		vb->type, vb->index, vb2_plane_size(&vbuf->vb2_buf, 0),
+		vb2_plane_size(&vbuf->vb2_buf, 1), vb2_plane_size(&vbuf->vb2_buf, 2));
+
+	vbuf->sequence = inst->queued_src_buf_num++;
+	vpu_buf->ts_input = ktime_get_raw();
+
+	v4l2_m2m_buf_queue(inst->v4l2_fh.m2m_ctx, vbuf);
+}
+
+static void wave6_vpu_dec_buf_queue_dst(struct vb2_buffer *vb)
+{
+	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+	struct vpu_instance *inst = vb2_get_drv_priv(vb->vb2_queue);
+
+	dev_dbg(inst->dev->dev, "type %4d index %4d size[0] %4ld size[1] : %4ld | size[2] : %4ld\n",
+		vb->type, vb->index, vb2_plane_size(&vbuf->vb2_buf, 0),
+		vb2_plane_size(&vbuf->vb2_buf, 1), vb2_plane_size(&vbuf->vb2_buf, 2));
+
+	inst->queued_dst_buf_num++;
+	if (inst->next_buf_last) {
+		wave6_handle_last_frame(inst, vbuf);
+		inst->next_buf_last = false;
+	} else {
+		v4l2_m2m_buf_queue(inst->v4l2_fh.m2m_ctx, vbuf);
+	}
+}
+
+static void wave6_vpu_dec_buf_queue(struct vb2_buffer *vb)
+{
+	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+	struct vpu_buffer *vpu_buf = wave6_to_vpu_buf(vbuf);
+
+	vpu_buf->consumed = false;
+	vpu_buf->used = false;
+	vpu_buf->error = false;
+	if (V4L2_TYPE_IS_OUTPUT(vb->type))
+		wave6_vpu_dec_buf_queue_src(vb);
+	else
+		wave6_vpu_dec_buf_queue_dst(vb);
+}
+
+static int wave6_vpu_dec_start_streaming(struct vb2_queue *q, unsigned int count)
+{
+	struct vpu_instance *inst = vb2_get_drv_priv(q);
+	struct v4l2_pix_format_mplane *fmt;
+	int ret = 0;
+
+	trace_wave6_vpu_start_streaming(inst, V4L2_TYPE_IS_OUTPUT(q->type));
+
+	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
+		fmt = &inst->src_fmt;
+		if (inst->state == VPU_INST_STATE_NONE) {
+			ret = wave6_vpu_dec_create_instance(inst);
+			if (!ret) {
+				inst->v4l2_fh.m2m_ctx->ignore_cap_streaming = true;
+				v4l2_m2m_set_dst_buffered(inst->v4l2_fh.m2m_ctx, true);
+			}
+		}
+		if (inst->state == VPU_INST_STATE_SEEK)
+			wave6_vpu_set_instance_state(inst, inst->state_in_seek);
+	} else {
+		fmt = &inst->dst_fmt;
+		if (inst->state == VPU_INST_STATE_INIT_SEQ) {
+			ret = wave6_vpu_dec_prepare_fb(inst);
+			if (ret)
+				goto exit;
+		}
+	}
+
+exit:
+	if (ret)
+		wave6_vpu_return_buffers(inst, q->type, VB2_BUF_STATE_QUEUED);
+
+	dev_dbg(inst->dev->dev, "[%d] %s %c%c%c%c %dx%d, %d buffers, ret = %d\n",
+		inst->id, V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture",
+		fmt->pixelformat,
+		fmt->pixelformat >> 8,
+		fmt->pixelformat >> 16,
+		fmt->pixelformat >> 24,
+		fmt->width, fmt->height, vb2_get_num_buffers(q), ret);
+
+	return ret;
+}
+
+static void wave6_vpu_dec_stop_streaming(struct vb2_queue *q)
+{
+	struct vpu_instance *inst = vb2_get_drv_priv(q);
+	struct v4l2_m2m_ctx *m2m_ctx = inst->v4l2_fh.m2m_ctx;
+
+	trace_wave6_vpu_stop_streaming(inst, V4L2_TYPE_IS_OUTPUT(q->type));
+
+	dev_dbg(inst->dev->dev, "[%d] %s, input %d, decode %d error %d\n",
+		inst->id, V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture",
+		inst->queued_src_buf_num, inst->processed_buf_num, inst->error_buf_num);
+
+	if (inst->state == VPU_INST_STATE_NONE)
+		goto exit;
+
+	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
+		wave6_vpu_reset_performance(inst);
+		inst->queued_src_buf_num = 0;
+		inst->processed_buf_num = 0;
+		inst->error_buf_num = 0;
+		inst->state_in_seek = inst->state;
+		v4l2_m2m_set_src_buffered(inst->v4l2_fh.m2m_ctx, false);
+		wave6_vpu_set_instance_state(inst, VPU_INST_STATE_SEEK);
+		inst->sequence = 0;
+	} else {
+		if (v4l2_m2m_has_stopped(m2m_ctx))
+			v4l2_m2m_clear_state(m2m_ctx);
+
+		inst->eos = false;
+		inst->queued_dst_buf_num = 0;
+		inst->sequence = 0;
+		wave6_vpu_dec_flush_instance(inst);
+	}
+
+exit:
+	wave6_vpu_return_buffers(inst, q->type, VB2_BUF_STATE_ERROR);
+}
+
+static int wave6_vpu_dec_buf_init(struct vb2_buffer *vb)
+{
+	struct vpu_instance *inst = vb2_get_drv_priv(vb->vb2_queue);
+	struct dec_seq_info *seq_info;
+	int i;
+
+	if (V4L2_TYPE_IS_OUTPUT(vb->type))
+		return 0;
+
+	if (inst->state == VPU_INST_STATE_NONE)
+		return 0;
+
+	seq_info = &inst->codec_info->dec_info.seq_info;
+	if (seq_info->c_fmt_idc != C_FMT_IDC_YUV400)
+		return 0;
+
+	for (i = 0; i < inst->dst_fmt.num_planes; i++) {
+		void *vaddr = vb2_plane_vaddr(vb, i);
+
+		if (vaddr)
+			memset(vaddr, 0x80, vb2_plane_size(vb, i));
+	}
+
+	return 0;
+}
+
+static const struct vb2_ops wave6_vpu_dec_vb2_ops = {
+	.queue_setup = wave6_vpu_dec_queue_setup,
+	.buf_queue = wave6_vpu_dec_buf_queue,
+	.start_streaming = wave6_vpu_dec_start_streaming,
+	.stop_streaming = wave6_vpu_dec_stop_streaming,
+	.buf_init = wave6_vpu_dec_buf_init,
+};
+
+static void wave6_set_default_format(struct v4l2_pix_format_mplane *src_fmt,
+				     struct v4l2_pix_format_mplane *dst_fmt)
+{
+	const struct vpu_format *vpu_fmt;
+
+	vpu_fmt = wave6_find_vpu_fmt_by_idx(0, VPU_FMT_TYPE_CODEC);
+	if (vpu_fmt) {
+		src_fmt->pixelformat = vpu_fmt->v4l2_pix_fmt;
+		src_fmt->num_planes = vpu_fmt->num_planes;
+		wave6_update_pix_fmt(src_fmt,
+				     W6_DEF_DEC_PIC_WIDTH, W6_DEF_DEC_PIC_HEIGHT);
+	}
+
+	vpu_fmt = wave6_find_vpu_fmt_by_idx(0, VPU_FMT_TYPE_RAW);
+	if (vpu_fmt) {
+		dst_fmt->pixelformat = vpu_fmt->v4l2_pix_fmt;
+		dst_fmt->num_planes = vpu_fmt->num_planes;
+		wave6_update_pix_fmt_cap(dst_fmt,
+					 W6_DEF_DEC_PIC_WIDTH, W6_DEF_DEC_PIC_HEIGHT,
+					 true);
+	}
+}
+
+static int wave6_vpu_dec_queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
+{
+	struct vpu_instance *inst = priv;
+	int ret;
+
+	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
+	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
+	src_vq->mem_ops = &vb2_dma_contig_memops;
+	src_vq->ops = &wave6_vpu_dec_vb2_ops;
+	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
+	src_vq->buf_struct_size = sizeof(struct vpu_buffer);
+	src_vq->drv_priv = inst;
+	src_vq->lock = &inst->dev->dev_lock;
+	src_vq->dev = inst->dev->v4l2_dev.dev;
+	ret = vb2_queue_init(src_vq);
+	if (ret)
+		return ret;
+
+	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
+	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
+	dst_vq->mem_ops = &vb2_dma_contig_memops;
+	dst_vq->ops = &wave6_vpu_dec_vb2_ops;
+	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
+	dst_vq->buf_struct_size = sizeof(struct vpu_buffer);
+	dst_vq->drv_priv = inst;
+	dst_vq->lock = &inst->dev->dev_lock;
+	dst_vq->dev = inst->dev->v4l2_dev.dev;
+	ret = vb2_queue_init(dst_vq);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static const struct vpu_instance_ops wave6_vpu_dec_inst_ops = {
+	.prepare_process = wave6_vpu_dec_prepare_decode,
+	.start_process = wave6_vpu_dec_start_decode,
+	.finish_process = wave6_vpu_dec_finish_decode,
+};
+
+static int wave6_vpu_open_dec(struct file *filp)
+{
+	struct video_device *vdev = video_devdata(filp);
+	struct vpu_core_device *core = video_drvdata(filp);
+	struct vpu_instance *inst = NULL;
+	int ret;
+
+	inst = kzalloc_obj(*inst);
+	if (!inst)
+		return -ENOMEM;
+
+	init_completion(&inst->irq_done);
+	inst->dev = core;
+	inst->type = VPU_INST_TYPE_DEC;
+	inst->ops = &wave6_vpu_dec_inst_ops;
+
+	v4l2_fh_init(&inst->v4l2_fh, vdev);
+	filp->private_data = &inst->v4l2_fh;
+	v4l2_fh_add(&inst->v4l2_fh, filp);
+
+	inst->v4l2_fh.m2m_ctx =
+		v4l2_m2m_ctx_init(core->m2m_dev, inst, wave6_vpu_dec_queue_init);
+	if (IS_ERR(inst->v4l2_fh.m2m_ctx)) {
+		ret = PTR_ERR(inst->v4l2_fh.m2m_ctx);
+		goto free_inst;
+	}
+
+	v4l2_ctrl_handler_init(&inst->v4l2_ctrl_hdl, 10);
+	v4l2_ctrl_new_std(&inst->v4l2_ctrl_hdl, &wave6_vpu_dec_ctrl_ops,
+			  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 1);
+	v4l2_ctrl_new_std(&inst->v4l2_ctrl_hdl, &wave6_vpu_dec_ctrl_ops,
+			  V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY,
+			  0, 0, 1, 0);
+	v4l2_ctrl_new_std(&inst->v4l2_ctrl_hdl, &wave6_vpu_dec_ctrl_ops,
+			  V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE,
+			  0, 1, 1, 0);
+	v4l2_ctrl_new_std_menu(&inst->v4l2_ctrl_hdl, &wave6_vpu_dec_ctrl_ops,
+			       V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
+			       V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE,
+			       0, V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN);
+	v4l2_ctrl_new_std_menu(&inst->v4l2_ctrl_hdl, &wave6_vpu_dec_ctrl_ops,
+			       V4L2_CID_MPEG_VIDEO_H264_PROFILE,
+			       V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, 0,
+			       V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
+
+	if (inst->v4l2_ctrl_hdl.error) {
+		ret = -ENODEV;
+		goto err_m2m_release;
+	}
+
+	inst->v4l2_fh.ctrl_handler = &inst->v4l2_ctrl_hdl;
+	ret = v4l2_ctrl_handler_setup(&inst->v4l2_ctrl_hdl);
+	if (ret)
+		goto err_m2m_release;
+
+	wave6_set_default_format(&inst->src_fmt, &inst->dst_fmt);
+	inst->colorspace = V4L2_COLORSPACE_DEFAULT;
+	inst->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
+	inst->quantization = V4L2_QUANTIZATION_DEFAULT;
+	inst->xfer_func = V4L2_XFER_FUNC_DEFAULT;
+
+	return 0;
+
+err_m2m_release:
+	v4l2_m2m_ctx_release(inst->v4l2_fh.m2m_ctx);
+	v4l2_ctrl_handler_free(&inst->v4l2_ctrl_hdl);
+free_inst:
+	v4l2_fh_del(&inst->v4l2_fh, filp);
+	v4l2_fh_exit(&inst->v4l2_fh);
+	kfree(inst);
+
+	return ret;
+}
+
+static int wave6_vpu_dec_release(struct file *filp)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(filp);
+
+	wave6_vpu_remove_dbgfs_file(inst);
+	v4l2_m2m_ctx_release(inst->v4l2_fh.m2m_ctx);
+
+	if (inst->state != VPU_INST_STATE_NONE)
+		wave6_vpu_dec_destroy_instance(inst, false);
+
+	v4l2_ctrl_handler_free(&inst->v4l2_ctrl_hdl);
+	v4l2_fh_del(&inst->v4l2_fh, filp);
+	v4l2_fh_exit(&inst->v4l2_fh);
+	kfree(inst);
+
+	return 0;
+}
+
+static const struct v4l2_file_operations wave6_vpu_dec_fops = {
+	.owner = THIS_MODULE,
+	.open = wave6_vpu_open_dec,
+	.release = wave6_vpu_dec_release,
+	.unlocked_ioctl = video_ioctl2,
+	.poll = v4l2_m2m_fop_poll,
+	.mmap = v4l2_m2m_fop_mmap,
+};
+
+int wave6_vpu_dec_register_device(struct vpu_core_device *core)
+{
+	struct video_device *vdev_dec;
+	int ret;
+
+	vdev_dec = video_device_alloc();
+	if (!vdev_dec)
+		return -ENOMEM;
+
+	strscpy(vdev_dec->name, VPU_DEC_DEV_NAME, sizeof(vdev_dec->name));
+	vdev_dec->fops = &wave6_vpu_dec_fops;
+	vdev_dec->ioctl_ops = &wave6_vpu_dec_ioctl_ops;
+	vdev_dec->release = video_device_release;
+	vdev_dec->v4l2_dev = &core->v4l2_dev;
+	vdev_dec->vfl_dir = VFL_DIR_M2M;
+	vdev_dec->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
+	vdev_dec->lock = &core->dev_lock;
+	video_set_drvdata(vdev_dec, core);
+
+	ret = video_register_device(vdev_dec, VFL_TYPE_VIDEO, -1);
+	if (ret) {
+		video_device_release(vdev_dec);
+		return ret;
+	}
+
+	core->video_dev_dec = vdev_dec;
+
+	return 0;
+}
+
+void wave6_vpu_dec_unregister_device(struct vpu_core_device *core)
+{
+	video_unregister_device(core->video_dev_dec);
+}
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +2764 -0

1,157 lines not on this page · raw message

drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +516 -0

516 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.31.1

Part of the diff is not shown here. View the raw message.

#15 lore raw

Re: [PATCH v7 4/9] media: chips-media: wave6: Add v4l2 m2m driver support

Thank you for your contribution! Sashiko AI review found 15 potential issue(s) to consider: - [High] Usage counter leak during instance destruction due to conditionally calling pm_runtime_put_sync()…

Thank you for your contribution! Sashiko AI review found 15 potential issue(s) to consider:
- [High] Usage counter leak during instance destruction due to conditionally calling pm_runtime_put_sync() based on pm_runtime_suspended().
- [High] Race condition and state corruption in M2M device run timeout handler causing double v4l2_m2m_job_finish, queue corruption, and missing VPU hardware abort.
- [High] Missing sizeimage update when the output format changes, and missing minimum clamping for compressed capture formats, leading to undersized buffers.
- [High] Data race on inst->next_buf_last causes decoded buffers to be permanently stranded in the ready queue.
- [High] Hardware-provided buffer counts are not validated against WAVE6_MAX_FBS, leading to out-of-bounds array accesses.
- [High] wave6_vpu_job_ready does not check for source buffer availability, leading to infinite CPU spinning when the source queue is empty.
- [High] stop_streaming returns buffers to userspace while the VPU hardware is still actively accessing them, leading to DMA use-after-free and memory corruption.
- [High] Error paths in wave6_vpu_enc_start_encode and wave6_vpu_dec_start_decode wrongly remove and abort in-flight buffers.
- [High] Missing cancel_delayed_work_sync during m2m device teardown.
- [High] Lockless iteration over V4L2 M2M ready queues races with VIDIOC_QBUF, leading to list corruption and kernel crashes.
- [Medium] The downscaler cannot be disabled via VIDIOC_S_SELECTION once enabled, and unsupported compose sizes are returned to userspace without applying them.
- [Medium] Encoder's VIDIOC_S_SELECTION incorrectly returns 0 without adjusting an invalid rectangle if the requested crop width or height is zero.
- [Medium] V4L2_SEL_TGT_CROP_BOUNDS and V4L2_SEL_TGT_CROP_DEFAULT incorrectly return the active crop instead of full frame bounds.
- [Medium] Modifying V4L2 formats and crop structures from the threaded IRQ context causes data races with userspace ioctls.
- [Medium] Concurrent unprotected accesses to inst->enc_ctrls between s_ctrl and buf_queue.
--

commit 2772c90afbf70493cce735a7f6c9ed38f93e4dbd
Author: Nas Chung <[email protected]>

media: chips-media: wave6: Add v4l2 m2m driver support

This commit adds v4l2 m2m stateful driver support for the Chips&Media Wave6 video codec IP, including both encoder and decoder. It supports AVC and HEVC codecs with multiple input and output formats, as well as multiplanar formats, cropping, scaling, and dynamic resolution change.

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +0 -0
new file mode 100644
index 0000000000000..4b07a2249db11

[ ... ]

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +13 -0
+static void wave6_vpu_dec_destroy_instance(struct vpu_instance *inst, bool remove_dbgfs)
+{
+	u32 fail_res = 0;
+	int ret;
+
+	wave6_vpu_disable_instance(inst);
+
+	if (remove_dbgfs)
+		wave6_vpu_remove_dbgfs_file(inst);
+
+	ret = wave6_vpu_dec_close(inst, &fail_res);
+	if (ret) {
+		dev_err(inst->dev->dev, "failed destroy instance: %d (%d)\n",

drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c line 166

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +10 -0
+			ret, fail_res);
+	}
+
+	scoped_guard(spinlock, &inst->dev->inst_lock)
+		list_del_init(&inst->list);
+	wave6_vpu_dec_release_fb(inst);
+	wave6_vpu_set_instance_state(inst, VPU_INST_STATE_NONE);
+
+	if (!pm_runtime_suspended(inst->dev->dev))
+		pm_runtime_put_sync(inst->dev->dev);

[Severity: High]
Can this conditionally leak the usage counter if the device is suspended?

Any instance destruction during a suspend cycle could trigger this leak, which prevents the device from ever entering runtime suspend again, breaking power management. pm_runtime_put_sync() handles suspended devices correctly and must be called unconditionally to balance the earlier pm_runtime_resume_and_get().

Quoted 1 line

+}

[ ... ]

Quoted 32 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +32 -0
+static int wave6_vpu_dec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+
+	dev_dbg(inst->dev->dev, "%s: type %d target %d\n",
+		__func__, s->type, s->target);
+
+	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
+	    s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
+		return -EINVAL;
+
+	switch (s->target) {
+	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
+		s->r.left = 0;
+		s->r.top = 0;
+		s->r.width = inst->dst_fmt.width;
+		s->r.height = inst->dst_fmt.height;
+		break;
+	case V4L2_SEL_TGT_COMPOSE_PADDED:
+	case V4L2_SEL_TGT_COMPOSE:
+		s->r.left = 0;
+		s->r.top = 0;
+		if (inst->scaler_info.enable) {
+			s->r.width = inst->scaler_info.width;
+			s->r.height = inst->scaler_info.height;
+		} else if (inst->crop.width && inst->crop.height) {
+			s->r = inst->crop;
+		} else {
+			s->r.width = inst->src_fmt.width;
+			s->r.height = inst->src_fmt.height;
+		}
+		break;

drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c line 1102

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +10 -0
+	case V4L2_SEL_TGT_CROP:
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+	case V4L2_SEL_TGT_CROP_BOUNDS:
+	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
+		s->r.left = 0;
+		s->r.top = 0;
+		s->r.width = inst->src_fmt.width;
+		s->r.height = inst->src_fmt.height;
+		if (inst->crop.width && inst->crop.height)
+			s->r = inst->crop;

[Severity: Medium]
Should V4L2_SEL_TGT_CROP_BOUNDS and V4L2_SEL_TGT_CROP_DEFAULT return the full frame bounds rather than the active crop?

If userspace sets an active crop, then queries V4L2_SEL_TGT_CROP_BOUNDS, the driver sets the result to the active crop (inst->crop) instead of the maximum supported uncropped resolution, causing userspace to incorrectly calculate dimensions.

Quoted 31 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +31 -0
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int wave6_vpu_dec_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	int step = 4;
+	int scale_width, scale_height;
+	int min_scale_width, min_scale_height;
+
+	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
+	    s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
+		return -EINVAL;
+
+	if (s->target != V4L2_SEL_TGT_COMPOSE)
+		return -EINVAL;
+
+	if (!(s->flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)))
+		s->flags |= V4L2_SEL_FLAG_LE;
+
+	scale_width = clamp(s->r.width, W6_MIN_DEC_PIC_WIDTH,
+			    round_up(inst->src_fmt.width, W6_DEC_BUF_ALIGNMENT));
+	scale_height = clamp(s->r.height, W6_MIN_DEC_PIC_HEIGHT,
+			     inst->src_fmt.height);
+	if (s->flags & V4L2_SEL_FLAG_GE) {
+		scale_width = round_up(scale_width, step);

drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c line 1143

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +10 -0
+		scale_height = round_up(scale_height, step);
+	}
+	if (s->flags & V4L2_SEL_FLAG_LE) {
+		scale_width = round_down(scale_width, step);
+		scale_height = round_down(scale_height, step);
+	}
+
+	if (scale_width < inst->src_fmt.width ||
+	    scale_height < inst->src_fmt.height)
+		inst->scaler_info.enable = true;

[Severity: Medium]
Is there a missing else path to disable the scaler when a 1:1 scale is requested?

If userspace requests a scaled down compose target, setting inst->scaler_info.enable = true, and later requests a 1:1 scale (scale_width == inst->src_fmt.width), the downscaler cannot be disabled via VIDIOC_S_SELECTION. Additionally, unsupported compose sizes are returned to userspace without applying them.

Quoted 25 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +25 -0
+
+	if (inst->scaler_info.enable) {
+		min_scale_width = ALIGN((inst->src_fmt.width / 8), step);
+		min_scale_height = ALIGN((inst->src_fmt.height / 8), step);
+
+		if (scale_width < W6_MIN_DEC_PIC_WIDTH)
+			scale_width = W6_MIN_DEC_PIC_WIDTH;
+		if (scale_width < min_scale_width)
+			scale_width = min_scale_width;
+		if (scale_height < W6_MIN_DEC_PIC_HEIGHT)
+			scale_height = W6_MIN_DEC_PIC_HEIGHT;
+		if (scale_height < min_scale_height)
+			scale_height = min_scale_height;
+
+		inst->scaler_info.width = scale_width;
+		inst->scaler_info.height = scale_height;
+	}
+
+	s->r.left = 0;
+	s->r.top = 0;
+	s->r.width = scale_width;
+	s->r.height = scale_height;
+
+	return 0;
+}

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c line 639

Quoted 9 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +9 -0
+static void wave6_handle_last_frame(struct vpu_instance *inst,
+				    struct vb2_v4l2_buffer *dst_buf)
+{
+	int i;
+
+	if (!dst_buf) {
+		dst_buf = v4l2_m2m_dst_buf_remove(inst->v4l2_fh.m2m_ctx);
+		if (!dst_buf) {
+			inst->next_buf_last = true;

[Severity: High]
Can this data race on inst->next_buf_last cause decoded buffers to be permanently stranded in the ready queue?

The worker thread executes wave6_handle_last_frame() without the queue lock and sets inst->next_buf_last. Concurrently, userspace calling VIDIOC_QBUF invokes wave6_vpu_dec_buf_queue_dst() under the queue lock. If QBUF checks the flag before the worker sets it, it queues the buffer normally. Then the worker sets the flag, and the next queued buffer will intercept the flag, stranding the first one.

Quoted 17 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +17 -0
+			return;
+		}
+	}
+
+	for (i = 0; i < inst->dst_fmt.num_planes; i++)
+		vb2_set_plane_payload(&dst_buf->vb2_buf, i, 0);
+
+	dst_buf->flags |= V4L2_BUF_FLAG_LAST;
+	dst_buf->field = V4L2_FIELD_NONE;
+	v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
+
+	if (inst->state != VPU_INST_STATE_INIT_SEQ) {
+		dev_dbg(inst->dev->dev, "[%d] eos\n", inst->id);
+		inst->eos = true;
+		v4l2_m2m_set_src_buffered(inst->v4l2_fh.m2m_ctx, false);
+	}
+}

[ ... ]

Quoted 19 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +19 -0
+static void wave6_vpu_dec_handle_source_change(struct vpu_instance *inst)
+{
+	static const struct v4l2_event vpu_event_src_ch = {
+		.type = V4L2_EVENT_SOURCE_CHANGE,
+		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
+	};
+	struct dec_seq_info *info = &inst->codec_info->dec_info.seq_info;
+
+	dev_dbg(inst->dev->dev, "pic size %dx%d profile %d, min_fb_cnt : %d | min_disp_cnt : %d\n",
+		info->pic_width, info->pic_height,
+		info->profile, info->min_frame_buffer_count, info->frame_buf_delay);
+
+	wave6_vpu_dec_retry_one_frame(inst);
+	wave6_vpu_dec_reset_frame_buffer_info(inst);
+	inst->fbc_buf_registered = false;
+
+	wave6_vpu_set_instance_state(inst, VPU_INST_STATE_INIT_SEQ);
+
+	inst->crop.left = info->pic_crop_rect.left;

drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c line 699

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +10 -0
+	inst->crop.top = info->pic_crop_rect.top;
+	inst->crop.width = info->pic_crop_rect.right - inst->crop.left;
+	inst->crop.height = info->pic_crop_rect.bottom - inst->crop.top;
+
+	wave6_update_v4l2_ctrls(inst, info);
+	wave6_update_color_info(inst, info);
+	wave6_update_pix_fmt(&inst->src_fmt, info->pic_width, info->pic_height);
+	wave6_update_pix_fmt_cap(&inst->dst_fmt,
+				 info->pic_width, info->pic_height,
+				 true);

[Severity: Medium]
Is there a data race when modifying these shared format and selection structures?

wave6_vpu_dec_handle_source_change() updates them from the threaded IRQ context without acquiring the device lock, which can race with userspace reading them via VIDIOC_G_FMT under the device lock, resulting in torn reads.

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +5 -0
+
+	trace_wave6_vpu_source_change(inst, info);
+
+	v4l2_event_queue_fh(&inst->v4l2_fh, &vpu_event_src_ch);
+}

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c line 917

Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +8 -0
+static int wave6_vpu_dec_g_fmt_cap(struct file *file, void *fh, struct v4l2_format *f)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	int i;
+
+	pix_mp->width = inst->dst_fmt.width;
+	pix_mp->height = inst->dst_fmt.height;

[Severity: Medium]
This reads the format structures under the device lock, racing with the lockless updates from the IRQ in wave6_vpu_dec_handle_source_change().

Quoted 16 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +16 -0
+	pix_mp->pixelformat = inst->dst_fmt.pixelformat;
+	pix_mp->field = inst->dst_fmt.field;
+	pix_mp->flags = inst->dst_fmt.flags;
+	pix_mp->num_planes = inst->dst_fmt.num_planes;
+	for (i = 0; i < pix_mp->num_planes; i++) {
+		pix_mp->plane_fmt[i].bytesperline = inst->dst_fmt.plane_fmt[i].bytesperline;
+		pix_mp->plane_fmt[i].sizeimage = inst->dst_fmt.plane_fmt[i].sizeimage;
+	}
+
+	pix_mp->colorspace = inst->colorspace;
+	pix_mp->ycbcr_enc = inst->ycbcr_enc;
+	pix_mp->quantization = inst->quantization;
+	pix_mp->xfer_func = inst->xfer_func;
+
+	return 0;
+}

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c line 1512

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +13 -0
+static void wave6_vpu_dec_buf_queue_dst(struct vb2_buffer *vb)
+{
+	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+	struct vpu_instance *inst = vb2_get_drv_priv(vb->vb2_queue);
+
+	dev_dbg(inst->dev->dev, "type %4d index %4d size[0] %4ld size[1] : %4ld | size[2] : %4ld\n",
+		vb->type, vb->index, vb2_plane_size(&vbuf->vb2_buf, 0),
+		vb2_plane_size(&vbuf->vb2_buf, 1), vb2_plane_size(&vbuf->vb2_buf, 2));
+
+	inst->queued_dst_buf_num++;
+	if (inst->next_buf_last) {
+		wave6_handle_last_frame(inst, vbuf);
+		inst->next_buf_last = false;

[Severity: High]
This QBUF path evaluates and resets inst->next_buf_last without synchronization against the worker thread executing wave6_handle_last_frame(), contributing to the data race.

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +4 -0
+	} else {
+		v4l2_m2m_buf_queue(inst->v4l2_fh.m2m_ctx, vbuf);
+	}
+}

[ ... ]

Quoted 25 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +25 -0
+static void wave6_vpu_dec_stop_streaming(struct vb2_queue *q)
+{
+	struct vpu_instance *inst = vb2_get_drv_priv(q);
+	struct v4l2_m2m_ctx *m2m_ctx = inst->v4l2_fh.m2m_ctx;
+
+	trace_wave6_vpu_stop_streaming(inst, V4L2_TYPE_IS_OUTPUT(q->type));
+
+	dev_dbg(inst->dev->dev, "[%d] %s, input %d, decode %d error %d\n",
+		inst->id, V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture",
+		inst->queued_src_buf_num, inst->processed_buf_num, inst->error_buf_num);
+
+	if (inst->state == VPU_INST_STATE_NONE)
+		goto exit;
+
+	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
+		wave6_vpu_reset_performance(inst);
+		inst->queued_src_buf_num = 0;
+		inst->processed_buf_num = 0;
+		inst->error_buf_num = 0;
+		inst->state_in_seek = inst->state;
+		v4l2_m2m_set_src_buffered(inst->v4l2_fh.m2m_ctx, false);
+		wave6_vpu_set_instance_state(inst, VPU_INST_STATE_SEEK);
+		inst->sequence = 0;
+	} else {
+		if (v4l2_m2m_has_stopped(m2m_ctx))

drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c line 1609

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +10 -0
+			v4l2_m2m_clear_state(m2m_ctx);
+
+		inst->eos = false;
+		inst->queued_dst_buf_num = 0;
+		inst->sequence = 0;
+		wave6_vpu_dec_flush_instance(inst);
+	}
+
+exit:
+	wave6_vpu_return_buffers(inst, q->type, VB2_BUF_STATE_ERROR);

[Severity: High]
Can this cause DMA use-after-free and memory corruption?

This unconditionally dequeues all buffers from the M2M ready queue and returns them to userspace without synchronously waiting for the active VPU job to complete. Buffers currently undergoing DMA could be handed back to userspace, which can unmap or free them while the hardware is still writing.

Quoted 1 line

+}

[ ... ]

Quoted 14 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +14 -0
+static int wave6_vpu_dec_prepare_fb(struct vpu_instance *inst)
+{
+	int ret;
+	unsigned int i;
+	unsigned int fb_num;
+	unsigned int mv_num;
+	unsigned int fb_stride;
+	unsigned int fb_height;
+	unsigned int luma_size;
+	unsigned int chroma_size;
+	struct dec_seq_info *seq_info = &inst->codec_info->dec_info.seq_info;
+
+	fb_num = seq_info->min_frame_buffer_count;
+	mv_num = seq_info->req_mv_buffer_count;

drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c line 1337

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +10 -0
+
+	fb_stride = ALIGN(inst->src_fmt.width, W6_FBC_BUF_ALIGNMENT);
+	fb_height = ALIGN(inst->src_fmt.height, W6_FBC_BUF_ALIGNMENT);
+
+	luma_size = fb_stride * fb_height;
+	chroma_size = ALIGN(fb_stride / 2, W6_FBC_BUF_ALIGNMENT) * fb_height;
+
+	for (i = 0; i < fb_num; i++) {
+		struct frame_buffer *frame = &inst->frame_buf[i];
+		struct vpu_buf *vframe = &inst->frame_vbuf[i];

[Severity: High]
Can this loop lead to out-of-bounds array accesses?

The firmware-provided min_frame_buffer_count is assigned to fb_num and used directly as the upper bound for the loop. Since inst->frame_buf and inst->frame_vbuf are fixed-size arrays (sized WAVE6_MAX_FBS), an excessive number of buffers could cause out-of-bounds writes if
fb_num > WAVE6_MAX_FBS.

Quoted 16 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +16 -0
+
+		vframe->size = luma_size + chroma_size;
+		ret = wave6_vdi_alloc_dma(inst->dev->dev, vframe);
+		if (ret) {
+			dev_err(inst->dev->dev, "alloc FBC buffer fail : %zu\n",
+				vframe->size);
+			goto error;
+		}
+
+		frame->buf_y = vframe->daddr;
+		frame->buf_cb = vframe->daddr + luma_size;
+		frame->buf_cr = (dma_addr_t)-1;
+		frame->stride = fb_stride;
+		frame->height = fb_height;
+		frame->map_type = COMPRESSED_FRAME_MAP;
+	}

[ ... ]

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +0 -0
new file mode 100644
index 0000000000000..9015631fc4299

[ ... ]

Quoted 14 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +14 -0
+static void wave6_vpu_enc_destroy_instance(struct vpu_instance *inst, bool remove_dbgfs)
+{
+	u32 fail_res = 0;
+	int ret;
+
+	wave6_vpu_disable_instance(inst);
+
+	if (remove_dbgfs)
+		wave6_vpu_remove_dbgfs_file(inst);
+
+	ret = wave6_vpu_enc_close(inst, &fail_res);
+	if (ret) {
+		dev_err(inst->dev->dev, "failed destroy instance: %d (%d)\n",
+			ret, fail_res);

drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c line 396

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +10 -0
+	}
+
+	scoped_guard(spinlock, &inst->dev->inst_lock)
+		list_del_init(&inst->list);
+	wave6_vpu_enc_release_fb(inst);
+	wave6_vdi_free_dma(&inst->ar_vbuf);
+	wave6_vpu_set_instance_state(inst, VPU_INST_STATE_NONE);
+
+	if (!pm_runtime_suspended(inst->dev->dev))
+		pm_runtime_put_sync(inst->dev->dev);

[Severity: High]
Does conditionally checking pm_runtime_suspended() here create a usage counter leak?

This shares the same logic flaw as wave6_vpu_dec_destroy_instance.

drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c line 405

Quoted 9 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +9 -0
+}
+
+static struct vb2_v4l2_buffer *wave6_get_valid_src_buf(struct vpu_instance *inst)
+{
+	struct vb2_v4l2_buffer *vb2_v4l2_buf;
+	struct v4l2_m2m_buffer *v4l2_m2m_buf;
+	struct vpu_buffer *vpu_buf = NULL;
+
+	v4l2_m2m_for_each_src_buf(inst->v4l2_fh.m2m_ctx, v4l2_m2m_buf) {

[Severity: High]
Can iterating over the M2M ready queues locklessly here race with concurrent queue modifications from VIDIOC_QBUF?

This function uses v4l2_m2m_for_each_src_buf() without acquiring the queue lock (&inst->dev->dev_lock), leading to linked list corruption and kernel crashes.

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +10 -0
+		vb2_v4l2_buf = &v4l2_m2m_buf->vb;
+		vpu_buf = wave6_to_vpu_buf(vb2_v4l2_buf);
+
+		if (!vpu_buf->consumed) {
+			dev_dbg(inst->dev->dev, "no consumed src idx : %d\n",
+				vb2_v4l2_buf->vb2_buf.index);
+			return vb2_v4l2_buf;
+		}
+	}
+

drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c line 425

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +10 -0
+	return NULL;
+}
+
+static struct vb2_v4l2_buffer *wave6_get_valid_dst_buf(struct vpu_instance *inst)
+{
+	struct vb2_v4l2_buffer *vb2_v4l2_buf;
+	struct v4l2_m2m_buffer *v4l2_m2m_buf;
+	struct vpu_buffer *vpu_buf;
+
+	v4l2_m2m_for_each_dst_buf(inst->v4l2_fh.m2m_ctx, v4l2_m2m_buf) {

[Severity: High]
This also performs lockless traversal of the ready queue.

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +12 -0
+		vb2_v4l2_buf = &v4l2_m2m_buf->vb;
+		vpu_buf = wave6_to_vpu_buf(vb2_v4l2_buf);
+
+		if (!vpu_buf->consumed) {
+			dev_dbg(inst->dev->dev, "no consumed dst idx : %d\n",
+				vb2_v4l2_buf->vb2_buf.index);
+			return vb2_v4l2_buf;
+		}
+	}
+
+	return NULL;
+}

[ ... ]

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +13 -0
+static int wave6_allocate_aux_buffer(struct vpu_instance *inst,
+				     enum aux_buffer_type type,
+				     unsigned int width,
+				     unsigned int height,
+				     int num)
+{
+	struct aux_buffer_size_info size_info;
+	unsigned int size;
+	int i, ret;
+
+	size_info.width = width;
+	size_info.height = height;
+	size_info.type = type;

drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c line 677

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +10 -0
+
+	ret = wave6_vpu_enc_get_aux_buffer_size(inst, size_info, &size);
+	if (ret) {
+		dev_err(inst->dev->dev, "%s: Get size fail (type %d)\n", __func__, type);
+		return ret;
+	}
+
+	for (i = 0; i < num; i++) {
+		inst->aux_vbuf[type][i].size = size;
+		ret = wave6_vdi_alloc_dma(inst->dev->dev, &inst->aux_vbuf[type][i]);

[Severity: High]
Unlike the decoder version, this encoder's aux buffer allocation lacks a min_t() bounds check against WAVE6_MAX_FBS, allowing OOB writes.

Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +8 -0
+		if (ret) {
+			dev_err(inst->dev->dev, "%s: Alloc fail (type %d)\n", __func__, type);
+			return ret;
+		}
+	}
+
+	return 0;
+}

[ ... ]

Quoted 32 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +32 -0
+static int wave6_vpu_enc_start_encode(struct vpu_instance *inst)
+{
+	int ret = -EINVAL;
+	struct vb2_v4l2_buffer *src_buf = NULL;
+	struct vb2_v4l2_buffer *dst_buf = NULL;
+	struct vpu_buffer *src_vbuf = NULL;
+	struct vpu_buffer *dst_vbuf = NULL;
+	struct frame_buffer frame_buf;
+	struct enc_param pic_param;
+	u32 stride;
+	u32 luma_size;
+	u32 stride_c;
+	u32 chroma_size;
+	u32 fail_res = 0;
+
+	memset(&pic_param, 0, sizeof(struct enc_param));
+	memset(&frame_buf, 0, sizeof(struct frame_buffer));
+
+	wave6_calc_source_frame_info(inst, &stride, &luma_size,
+				     &stride_c, &chroma_size);
+
+	ret = wave6_update_seq_param(inst);
+	if (ret)
+		goto exit;
+
+	src_buf = wave6_get_valid_src_buf(inst);
+	dst_buf = wave6_get_valid_dst_buf(inst);
+
+	if (!dst_buf) {
+		dev_dbg(inst->dev->dev, "no valid dst buf\n");
+		goto exit;
+	}

drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c line 800

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +10 -0
+
+	dst_vbuf = wave6_to_vpu_buf(dst_buf);
+	pic_param.pic_stream_buffer_addr = wave6_get_dma_addr(dst_buf, 0);
+	pic_param.pic_stream_buffer_size = vb2_plane_size(&dst_buf->vb2_buf, 0);
+	if (!src_buf) {
+		dev_dbg(inst->dev->dev, "no valid src buf\n");
+		if (inst->state == VPU_INST_STATE_STOP)
+			pic_param.src_end = true;
+		else
+			goto exit;

[Severity: High]
Does handling the missing buffer by failing here accelerate the endless loop in wave6_vpu_job_ready?

If userspace queues a capture buffer but no source buffer, wave6_vpu_job_ready() returns 1. This function will find no source buffer, immediately return -EINVAL, and the M2M framework will instantly evaluate job_ready again, resulting in an infinite CPU lockup.

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +3 -0
+	} else {
+		src_vbuf = wave6_to_vpu_buf(src_buf);
+		if (inst->src_fmt.num_planes == 1) {

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c line 857

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +12 -0
+	ret = wave6_vpu_enc_start_one_frame(inst, &pic_param, &fail_res);
+	if (ret) {
+		dev_err(inst->dev->dev, "[%d] %s: fail %d\n", inst->id, __func__, ret);
+		wave6_vpu_set_instance_state(inst, VPU_INST_STATE_STOP);
+
+		dst_buf = v4l2_m2m_dst_buf_remove(inst->v4l2_fh.m2m_ctx);
+		if (dst_buf) {
+			dst_buf->sequence = inst->sequence;
+			v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
+		}
+
+		src_buf = v4l2_m2m_src_buf_remove(inst->v4l2_fh.m2m_ctx);

[Severity: High]
Does the error recovery path wrongly remove and abort in-flight buffers?

When wave6_vpu_enc_start_one_frame() fails, this error recovery path blindly pops the absolute head of the ready queues. If there are buffers currently in flight (marked consumed = true), they sit at the head of the queue. The driver pops the active in-flight buffer and erroneously returns it, while the unconsumed buffer that actually caused the failure remains stuck in the queue.

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +13 -0
+		if (src_buf) {
+			v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
+			inst->sequence++;
+			inst->processed_buf_num++;
+			inst->error_buf_num++;
+		}
+	} else {
+		dev_dbg(inst->dev->dev, "%s: success\n", __func__);
+	}
+
+exit:
+	return ret;
+}

[ ... ]

Quoted 18 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +18 -0
+static int wave6_vpu_enc_try_fmt_cap(struct file *file, void *fh, struct v4l2_format *f)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	const struct vpu_format *vpu_fmt;
+	int width, height;
+
+	dev_dbg(inst->dev->dev, "%s: 4cc %d w %d h %d plane %d colorspace %d\n",
+		__func__, pix_mp->pixelformat, pix_mp->width, pix_mp->height,
+		pix_mp->num_planes, pix_mp->colorspace);
+
+	if (!V4L2_TYPE_IS_CAPTURE(f->type))
+		return -EINVAL;
+
+	vpu_fmt = wave6_find_vpu_fmt(pix_mp->pixelformat, VPU_FMT_TYPE_CODEC);
+	if (!vpu_fmt) {
+		width = inst->dst_fmt.width;
+		height = inst->dst_fmt.height;

drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c line 1070

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +10 -0
+		pix_mp->pixelformat = inst->dst_fmt.pixelformat;
+		pix_mp->num_planes = inst->dst_fmt.num_planes;
+	} else {
+		width = pix_mp->width;
+		height = pix_mp->height;
+		pix_mp->pixelformat = vpu_fmt->v4l2_pix_fmt;
+		pix_mp->num_planes = vpu_fmt->num_planes;
+	}
+
+	wave6_update_pix_fmt(pix_mp, width, height);

[Severity: High]
Does this accept arbitrarily small sizeimage values without minimum bounds clamping?

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +7 -0
+	pix_mp->colorspace = inst->colorspace;
+	pix_mp->ycbcr_enc = inst->ycbcr_enc;
+	pix_mp->quantization = inst->quantization;
+	pix_mp->xfer_func = inst->xfer_func;
+
+	return 0;
+}

[ ... ]

Quoted 33 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +33 -0
+static int wave6_vpu_enc_s_fmt_out(struct file *file, void *fh, struct v4l2_format *f)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+	const struct vpu_format *vpu_fmt;
+	struct vb2_queue *q;
+	int i, ret;
+
+	dev_dbg(inst->dev->dev, "%s: 4cc %d w %d h %d plane %d colorspace %d\n",
+		__func__, pix_mp->pixelformat, pix_mp->width, pix_mp->height,
+		pix_mp->num_planes, pix_mp->colorspace);
+
+	q = v4l2_m2m_get_vq(inst->v4l2_fh.m2m_ctx, f->type);
+	if (vb2_is_busy(q))
+		return -EBUSY;
+
+	ret = wave6_vpu_enc_try_fmt_out(file, fh, f);
+	if (ret)
+		return ret;
+
+	vpu_fmt = wave6_find_vpu_fmt(pix_mp->pixelformat, VPU_FMT_TYPE_RAW);
+	if (!vpu_fmt)
+		return -EINVAL;
+
+	inst->src_fmt.width = pix_mp->width;
+	inst->src_fmt.height = pix_mp->height;
+	inst->src_fmt.pixelformat = pix_mp->pixelformat;
+	inst->src_fmt.field = pix_mp->field;
+	inst->src_fmt.flags = pix_mp->flags;
+	inst->src_fmt.num_planes = pix_mp->num_planes;
+	for (i = 0; i < inst->src_fmt.num_planes; i++) {
+		inst->src_fmt.plane_fmt[i].bytesperline = pix_mp->plane_fmt[i].bytesperline;
+		inst->src_fmt.plane_fmt[i].sizeimage = pix_mp->plane_fmt[i].sizeimage;

drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c line 1246

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +10 -0
+	}
+
+	inst->cbcr_interleave = !!(vpu_fmt->flags & VPU_FMT_FLAG_CBCR_INTERLEAVED);
+	inst->nv21 = !!(vpu_fmt->flags & VPU_FMT_FLAG_CRCB_ORDER);
+	inst->colorspace = pix_mp->colorspace;
+	inst->ycbcr_enc = pix_mp->ycbcr_enc;
+	inst->quantization = pix_mp->quantization;
+	inst->xfer_func = pix_mp->xfer_func;
+
+	wave6_update_pix_fmt(&inst->dst_fmt, pix_mp->width, pix_mp->height);

[Severity: High]
Does this leave the encoder's capture buffer sized for the old resolution?

If userspace sets the source raw format to a high resolution via VIDIOC_S_FMT, the driver updates the destination format via wave6_update_pix_fmt(), but fails to update the existing sizeimage to match the new resolution requirements for compressed formats.

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +4 -0
+	wave6_update_crop_info(inst, 0, 0, pix_mp->width, pix_mp->height);
+
+	return 0;
+}

[ ... ]

Quoted 27 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +27 -0
+static int wave6_vpu_enc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
+{
+	struct vpu_instance *inst = wave6_file_to_vpu_inst(file);
+	u32 max_crop_w, max_crop_h;
+
+	if (!V4L2_TYPE_IS_OUTPUT(s->type))
+		return -EINVAL;
+
+	if (s->target != V4L2_SEL_TGT_CROP)
+		return -EINVAL;
+
+	if (!(s->flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)))
+		s->flags |= V4L2_SEL_FLAG_LE;
+
+	if (s->flags & V4L2_SEL_FLAG_GE) {
+		s->r.left = round_up(s->r.left, W6_ENC_CROP_STEP);
+		s->r.top = round_up(s->r.top, W6_ENC_CROP_STEP);
+		s->r.width = round_up(s->r.width, W6_ENC_CROP_STEP);
+		s->r.height = round_up(s->r.height, W6_ENC_CROP_STEP);
+	}
+	if (s->flags & V4L2_SEL_FLAG_LE) {
+		s->r.left = round_down(s->r.left, W6_ENC_CROP_STEP);
+		s->r.top = round_down(s->r.top, W6_ENC_CROP_STEP);
+		s->r.width = round_down(s->r.width, W6_ENC_CROP_STEP);
+		s->r.height = round_down(s->r.height, W6_ENC_CROP_STEP);
+	}
+

drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c line 1341

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +10 -0
+	s->r.left = clamp_t(s32, s->r.left, 0,
+			    inst->src_fmt.width - W6_MIN_ENC_PIC_WIDTH);
+	s->r.top = clamp_t(s32, s->r.top, 0,
+			   inst->src_fmt.height - W6_MIN_ENC_PIC_HEIGHT);
+
+	max_crop_w = inst->src_fmt.width - s->r.left;
+	max_crop_h = inst->src_fmt.height - s->r.top;
+
+	if (!s->r.width || !s->r.height)
+		return 0;

[Severity: Medium]
Does this early return bypass the required bound clamping and struct updating, violating V4L2 compliance?

If userspace requests a crop selection with width = 0 or height = 0, the driver returns 0, signaling success, but it does not adjust the rectangle to the minimum valid size as mandated by the V4L2 spec, leaving the application with false assumptions about the hardware configuration.

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +7 -0
+	if (max_crop_w < W6_MIN_ENC_PIC_WIDTH)
+		return 0;
+	if (max_crop_h < W6_MIN_ENC_PIC_HEIGHT)
+		return 0;
+
+	s->r.width = clamp(s->r.width, W6_MIN_ENC_PIC_WIDTH, max_crop_w);
+	s->r.height = clamp(s->r.height, W6_MIN_ENC_PIC_HEIGHT, max_crop_h);

[ ... ]

Quoted 23 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +23 -0
+static void wave6_vpu_enc_stop_streaming(struct vb2_queue *q)
+{
+	struct vpu_instance *inst = vb2_get_drv_priv(q);
+
+	trace_wave6_vpu_stop_streaming(inst, V4L2_TYPE_IS_OUTPUT(q->type));
+
+	dev_dbg(inst->dev->dev, "[%d] %s, input %d, encode %d\n",
+		inst->id, V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture",
+		inst->queued_src_buf_num, inst->sequence);
+
+	if (inst->state == VPU_INST_STATE_NONE)
+		goto exit;
+
+	if (wave6_vpu_both_queues_are_streaming(inst))
+		wave6_vpu_set_instance_state(inst, VPU_INST_STATE_STOP);
+
+	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
+		wave6_vpu_reset_performance(inst);
+		inst->queued_src_buf_num = 0;
+		inst->processed_buf_num = 0;
+		inst->error_buf_num = 0;
+		inst->sequence = 0;
+		v4l2_m2m_set_src_buffered(inst->v4l2_fh.m2m_ctx, false);

drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c line 2400

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +10 -0
+	} else {
+		inst->eos = false;
+		inst->queued_dst_buf_num = 0;
+	}
+
+	if (V4L2_TYPE_IS_CAPTURE(q->type) && inst->state != VPU_INST_STATE_NONE)
+		wave6_vpu_enc_destroy_instance(inst, true);
+
+exit:
+	wave6_vpu_return_buffers(inst, q->type, VB2_BUF_STATE_ERROR);

[Severity: High]
Does this free DMA buffers and return them to userspace without stopping or waiting for the active VPU job to complete?

This unconditionally dequeues all buffers and returns them, meaning buffers currently undergoing DMA could be unmapped or freed while the hardware is still writing, leading to silent memory corruption or IOMMU faults.

Quoted 1 line

+}

[ ... ]

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +6 -0
+static void wave6_vpu_enc_buf_queue(struct vb2_buffer *vb)
+{
+	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+	struct vpu_instance *inst = vb2_get_drv_priv(vb->vb2_queue);
+	struct vpu_buffer *vpu_buf = wave6_to_vpu_buf(vbuf);
+

drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c line 2295

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +10 -0
+	dev_dbg(inst->dev->dev, "type %4d index %4d size[0] %4ld size[1] : %4ld | size[2] : %4ld\n",
+		vb->type, vb->index, vb2_plane_size(&vbuf->vb2_buf, 0),
+		vb2_plane_size(&vbuf->vb2_buf, 1), vb2_plane_size(&vbuf->vb2_buf, 2));
+
+	if (V4L2_TYPE_IS_OUTPUT(vb->type)) {
+		vbuf->sequence = inst->queued_src_buf_num++;
+
+		vpu_buf->ts_input = ktime_get_raw();
+		vpu_buf->force_key_frame = inst->enc_ctrls.force_key_frame;
+		inst->enc_ctrls.force_key_frame = false;

[Severity: Medium]
Does reading and modifying the control structure locklessly here race with the V4L2 S_CTRL framework?

This runs from VIDIOC_QBUF under the queue lock, modifying the same variables that wave6_vpu_enc_s_ctrl() accesses from VIDIOC_S_CTRL under the control handler lock. Simultaneous QBUF and S_CTRL calls could cause race conditions.

Quoted 20 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +20 -0
+		vpu_buf->force_frame_qp = !inst->enc_ctrls.frame_rc_enable;
+		if (vpu_buf->force_frame_qp) {
+			if (inst->std == W_AVC_ENC) {
+				vpu_buf->force_i_frame_qp = inst->enc_ctrls.h264.i_frame_qp;
+				vpu_buf->force_p_frame_qp = inst->enc_ctrls.h264.p_frame_qp;
+				vpu_buf->force_b_frame_qp = inst->enc_ctrls.h264.b_frame_qp;
+			} else if (inst->std == W_HEVC_ENC) {
+				vpu_buf->force_i_frame_qp = inst->enc_ctrls.hevc.i_frame_qp;
+				vpu_buf->force_p_frame_qp = inst->enc_ctrls.hevc.p_frame_qp;
+				vpu_buf->force_b_frame_qp = inst->enc_ctrls.hevc.b_frame_qp;
+			}
+		}
+	} else {
+		inst->queued_dst_buf_num++;
+	}
+
+	vpu_buf->consumed = false;
+	vpu_buf->used = false;
+	v4l2_m2m_buf_queue(inst->v4l2_fh.m2m_ctx, vbuf);
+}

[ ... ]

Quoted 20 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +20 -0
+static int wave6_vpu_enc_prepare_fb(struct vpu_instance *inst)
+{
+	int ret;
+	unsigned int i;
+	unsigned int fb_num;
+	unsigned int mv_num;
+	unsigned int fb_stride, fb_stride_c;
+	unsigned int fb_height;
+	unsigned int luma_size;
+	unsigned int chroma_size;
+	struct enc_seq_info *seq_info = &inst->codec_info->enc_info.seq_info;
+	struct enc_open_param *open_param = &inst->codec_info->enc_info.open_param;
+
+	fb_num = seq_info->min_frame_buffer_count;
+	mv_num = seq_info->req_mv_buffer_count;
+
+	fb_stride = ALIGN(inst->codec_rect.width, W6_FBC_BUF_ALIGNMENT);
+	fb_height = ALIGN(inst->codec_rect.height, W6_FBC_BUF_ALIGNMENT);
+	if (open_param->rot_angle == ROT_90 || open_param->rot_angle == ROT_270) {
+		fb_stride = ALIGN(inst->codec_rect.height, W6_FBC_BUF_ALIGNMENT);

drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c line 2180

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +10 -0
+		fb_height = ALIGN(inst->codec_rect.width, W6_FBC_BUF_ALIGNMENT);
+	}
+	luma_size = fb_stride * fb_height;
+
+	fb_stride_c = ALIGN(fb_stride / 2, W6_FBC_BUF_ALIGNMENT);
+	chroma_size = fb_stride_c * fb_height;
+
+	for (i = 0; i < fb_num; i++) {
+		struct frame_buffer *frame = &inst->frame_buf[i];
+		struct vpu_buf *vframe = &inst->frame_vbuf[i];

[Severity: High]
Does this encoder loop blindly trust the hardware-provided frame count without validation against WAVE6_MAX_FBS, similar to the decoder?

Quoted 17 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c new file +17 -0
+
+		vframe->size = luma_size + chroma_size;
+		ret = wave6_vdi_alloc_dma(inst->dev->dev, vframe);
+		if (ret) {
+			dev_err(inst->dev->dev, "alloc FBC buffer fail : %zu\n",
+				vframe->size);
+			goto error;
+		}
+
+		frame->buf_y = vframe->daddr;
+		frame->buf_cb = vframe->daddr + luma_size;
+		frame->buf_cr = (dma_addr_t)-1;
+		frame->stride = fb_stride;
+		frame->stride_c = fb_stride_c;
+		frame->height = fb_height;
+		frame->map_type = COMPRESSED_FRAME_MAP;
+	}

[ ... ]

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +0 -0
new file mode 100644
index 0000000000000..9d3af14efd9d0

[ ... ]

Quoted 11 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +11 -0
+void wave6_update_pix_fmt(struct v4l2_pix_format_mplane *pix_mp,
+			  unsigned int width,
+			  unsigned int height)
+{
+	const struct v4l2_format_info *fmt_info;
+	unsigned int stride_y;
+	int i;
+
+	pix_mp->width = width;
+	pix_mp->height = height;
+	pix_mp->flags = 0;

drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c line 35

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +10 -0
+	pix_mp->field = V4L2_FIELD_NONE;
+	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
+
+	fmt_info = v4l2_format_info(pix_mp->pixelformat);
+	if (!fmt_info) {
+		pix_mp->plane_fmt[0].bytesperline = 0;
+		if (!pix_mp->plane_fmt[0].sizeimage)
+			pix_mp->plane_fmt[0].sizeimage = width * height;
+
+		return;

[Severity: High]
Does this fail to recalculate or clamp sizeimage if it is already non-zero for compressed formats?

Because fmt_info is NULL for compressed formats, it returns without updating the existing sizeimage to match the new resolution requirements, leading to undersized buffers that could corrupt memory.

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +5 -0
+	}
+
+	stride_y = width * fmt_info->bpp[0];
+	if (pix_mp->plane_fmt[0].bytesperline <= W6_MAX_PIC_STRIDE)
+		stride_y = max(stride_y, pix_mp->plane_fmt[0].bytesperline);

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c line 76

Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +8 -0
+struct vb2_v4l2_buffer *wave6_get_dst_buf_by_addr(struct vpu_instance *inst,
+						  dma_addr_t addr)
+{
+	struct vb2_v4l2_buffer *vb2_v4l2_buf;
+	struct v4l2_m2m_buffer *v4l2_m2m_buf;
+	struct vb2_v4l2_buffer *dst_buf = NULL;
+
+	v4l2_m2m_for_each_dst_buf(inst->v4l2_fh.m2m_ctx, v4l2_m2m_buf) {

[Severity: High]
Does this locklessly traverse the M2M ready queue?

Like the encoder functions, iterating the M2M ready queues from the worker thread or IRQ handler without acquiring the queue lock races with concurrent userspace calls to VIDIOC_QBUF modifying the same linked list.

Quoted 9 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +9 -0
+		vb2_v4l2_buf = &v4l2_m2m_buf->vb;
+		if (addr == wave6_get_dma_addr(vb2_v4l2_buf, 0)) {
+			dst_buf = vb2_v4l2_buf;
+			break;
+		}
+	}
+
+	return dst_buf;
+}

[ ... ]

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +7 -0
+static int wave6_vpu_job_ready(void *priv)
+{
+	struct vpu_instance *inst = priv;
+
+	dev_dbg(inst->dev->dev, "[%d]%s: state %d\n",
+		inst->id, __func__, inst->state);
+

drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c line 361

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +10 -0
+	if (inst->state == VPU_INST_STATE_OPEN)
+		return 1;
+	if (inst->state < VPU_INST_STATE_PIC_RUN)
+		return 0;
+	if (inst->state == VPU_INST_STATE_STOP && inst->eos)
+		return 0;
+	if (!wave6_vpu_check_fb_available(inst))
+		return 0;
+
+	return 1;

[Severity: High]
Does this fail to enforce that source buffers are ready before signaling the m2m core to run?

It checks only destination buffer availability. If no source buffer is queued, it still returns 1, leading wave6_vpu_enc_start_encode to fail and exit. The M2M framework will instantly evaluate job_ready again, resulting in an infinite CPU lockup.

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +7 -0
+}
+
+static void wave6_vpu_device_run_timeout(struct work_struct *work)
+{
+	struct delayed_work *dwork = to_delayed_work(work);
+	struct vpu_core_device *core = container_of(dwork, struct vpu_core_device, task_timer);
+	struct vpu_instance *inst = v4l2_m2m_get_curr_priv(core->m2m_dev);

drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c line 378

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +10 -0
+	struct vb2_v4l2_buffer *src_buf = NULL;
+	struct vb2_v4l2_buffer *dst_buf = NULL;
+
+	if (!inst)
+		return;
+
+	dev_err(inst->dev->dev, "[%d] sequence %d timeout\n", inst->id, inst->sequence);
+	src_buf = v4l2_m2m_src_buf_remove(inst->v4l2_fh.m2m_ctx);
+	if (src_buf) {
+		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);

[Severity: High]
Does the timeout handler remove and complete the source buffer without synchronization against the hardware IRQ path?

drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c line 391

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +13 -0
+		if (inst->type == VPU_INST_TYPE_DEC)
+			inst->processed_buf_num++;
+		inst->error_buf_num++;
+	}
+
+	dst_buf = v4l2_m2m_dst_buf_remove(inst->v4l2_fh.m2m_ctx);
+	if (dst_buf)
+		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
+
+	vb2_queue_error(v4l2_m2m_get_src_vq(inst->v4l2_fh.m2m_ctx));
+	vb2_queue_error(v4l2_m2m_get_dst_vq(inst->v4l2_fh.m2m_ctx));
+
+	v4l2_m2m_job_finish(inst->dev->m2m_dev, inst->v4l2_fh.m2m_ctx);

[Severity: High]
Does the timeout handler finish the M2M job without aborting the hardware sequence or synchronizing against the hardware completion path?

Because the underlying hardware job is not aborted, it can finish later and trigger wave6_vpu_finish_job(), which will call v4l2_m2m_job_finish() a second time for the same context, causing queue corruption.

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +4 -0
+}
+
+static void wave6_vpu_device_run(void *priv)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c line 419

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +4 -0
+void wave6_vpu_finish_job(struct vpu_instance *inst)
+{
+	cancel_delayed_work_sync(&inst->dev->task_timer);
+	v4l2_m2m_job_finish(inst->dev->m2m_dev, inst->v4l2_fh.m2m_ctx);

[Severity: High]
This path executes once the rogue hardware job completes, creating a duplicate job completion due to the earlier v4l2_m2m_job_finish() in the timeout handler.

Quoted 1 line

+}

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c line 516

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +4 -0
+void wave6_vpu_release_m2m_dev(struct vpu_core_device *core)
+{
+	v4l2_m2m_release(core->m2m_dev);
+}

[Severity: High]
Does this teardown function leave the asynchronous workqueue timer unattended?

If wave6_vpu_release_m2m_dev() is called while a VPU job is executing, completely omitting cancel_delayed_work_sync(&core->task_timer) can cause a use-after-free if the timeout fires after the driver memory is freed.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#16 lore raw

Re: [PATCH v7 4/9] media: chips-media: wave6: Add v4l2 m2m driver support

In reply to Nas Chung
Quoted 38 lines · Nas Chung

Add v4l2 m2m stateful driver support for the
Chips&Media Wave6 video codec IP, including both encoder and decoder.

The following features and formats are supported:
Encoder (8bit only):
- Supported codecs: AVC, HEVC
- Supported input formats:
* V4L2_PIX_FMT_YUV420/NV12/NV21
* V4L2_PIX_FMT_YUV422P/NV16/NV61
* V4L2_PIX_FMT_YUYV
* V4L2_PIX_FMT_YUV24/NV24/NV42
* V4L2_PIX_FMT_RGB24
* V4L2_PIX_FMT_P010
* V4L2_PIX_FMT_ARGB32
* V4L2_PIX_FMT_RGBA32
* V4L2_PIX_FMT_XRGB32
* V4L2_PIX_FMT_ARGB2101010
- Supports multiplanar format
- Although 10-bit input formats are supported (e.g., P010, ARGB2101010), encoded output is limited to 8-bit streams
- Supports crop
- Supports extensive v4l2 encoder control

Decoder (8bit only):
- Supported codecs: AVC, HEVC (8bit only)
- Supported output formats: V4L2_PIX_FMT_YUV420/NV12/NV21
- Supports multiplanar format
- Supports scale down
- Supports dynamic resolution change

Note: While other versions of the Wave6 IP may support VP9 decoding and AV1 decoding and encoding those formats are not implemented or validated in this driver at this time.

---

...

drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c line 254

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +13 -0
+
+u64 wave6_vpu_cycle_to_ns(struct vpu_core_device *core, u64 cycle)
+{
+	unsigned long rate;
+
+	if (!core->num_clks || !core->clks)
+		return 0;
+
+	rate = clk_get_rate(core->clks[0].clk);
+	if (!rate)
+		return 0;
+
+	return (cycle * NSEC_PER_SEC) / rate;

div_u64, otherwise 32bit compiler build failure.

Frank

Quoted 16 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +16 -0
+}
+
+int wave6_vpu_wait_interrupt(struct vpu_instance *inst, unsigned int timeout)
+{
+	int ret;
+
+	ret = wait_for_completion_timeout(&inst->irq_done,
+					  msecs_to_jiffies(timeout));
+	if (!ret)
+		return -ETIMEDOUT;
+
+	reinit_completion(&inst->irq_done);
+
+	return 0;
+}
+
author
#17 lore raw

RE: [PATCH v7 4/9] media: chips-media: wave6: Add v4l2 m2m driver support

Hi, Frank.

Quoted 54 lines

-----Original Message-----
From: Frank Li <[email protected]>
Sent: Friday, September 11, 2026 5:37 AM
To: Nas Chung <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jackson.lee <[email protected]>; lafley.kim <[email protected]>; [email protected]; Ming Qian <[email protected]>
Subject: Re: [PATCH v7 4/9] media: chips-media: wave6: Add v4l2 m2m driver support

Quoted 38 lines · Nas Chung

Add v4l2 m2m stateful driver support for the
Chips&Media Wave6 video codec IP, including both encoder and decoder.

The following features and formats are supported:
Encoder (8bit only):
- Supported codecs: AVC, HEVC
- Supported input formats:
* V4L2_PIX_FMT_YUV420/NV12/NV21
* V4L2_PIX_FMT_YUV422P/NV16/NV61
* V4L2_PIX_FMT_YUYV
* V4L2_PIX_FMT_YUV24/NV24/NV42
* V4L2_PIX_FMT_RGB24
* V4L2_PIX_FMT_P010
* V4L2_PIX_FMT_ARGB32
* V4L2_PIX_FMT_RGBA32
* V4L2_PIX_FMT_XRGB32
* V4L2_PIX_FMT_ARGB2101010
- Supports multiplanar format
- Although 10-bit input formats are supported (e.g., P010, ARGB2101010), encoded output is limited to 8-bit streams
- Supports crop
- Supports extensive v4l2 encoder control

Decoder (8bit only):
- Supported codecs: AVC, HEVC (8bit only)
- Supported output formats: V4L2_PIX_FMT_YUV420/NV12/NV21
- Supports multiplanar format
- Supports scale down
- Supports dynamic resolution change

Note: While other versions of the Wave6 IP may support VP9 decoding and AV1 decoding and encoding those formats are not implemented or validated in this driver at this time.

---

...

Quoted 15 lines

drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c line 254

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +13 -0
+
+u64 wave6_vpu_cycle_to_ns(struct vpu_core_device *core, u64 cycle)
+{
+	unsigned long rate;
+
+	if (!core->num_clks || !core->clks)
+		return 0;
+
+	rate = clk_get_rate(core->clks[0].clk);
+	if (!rate)
+		return 0;
+
+	return (cycle * NSEC_PER_SEC) / rate;

div_u64, otherwise 32bit compiler build failure.

I'll fix this in v8.

Thanks.
Nas.

Quoted 19 lines

Frank

drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dec.c new file +3 -0
+}
+
+int wave6_vpu_wait_interrupt(struct vpu_instance *inst, unsigned int

timeout)

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c new file +13 -0
+{
+	int ret;
+
+	ret = wait_for_completion_timeout(&inst->irq_done,
+					  msecs_to_jiffies(timeout));
+	if (!ret)
+		return -ETIMEDOUT;
+
+	reinit_completion(&inst->irq_done);
+
+	return 0;
+}
+
author
#18 lore raw

PATCH v7 5/9 media: chips-media: wave6: Add Wave6 core driver

In reply to Nas Chung

Add the core driver for the Chips&Media Wave6 video codec IP.

The hardware contains one control register region and four interface register regions for a shared video processing engine. This driver handles the interface register regions, each with its own MMIO range and interrupt, while relying on the control driver for firmware loading and shared resource management.

It configures the V4L2 mem2mem devices and communicates with the Wave6 hardware to perform video processing tasks.

---
 .../chips-media/wave6/wave6-vpu-core.c        | 448 ++++++++++++++++++
 .../chips-media/wave6/wave6-vpu-core.h        | 126 +++++
 2 files changed, 574 insertions(+)
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-core.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-core.h
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +448 -0
new file mode 100644
index 000000000000..89cce27802cf
@@ -0,0 +1,448 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+/*
+ * Wave6 series multi-standard codec IP - wave6 core driver
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/clk.h>
+#include <linux/firmware.h>
+#include <linux/interrupt.h>
+#include <linux/pm_runtime.h>
+#include <linux/debugfs.h>
+#include <linux/iopoll.h>
+#include "wave6-vpu-core.h"
+#include "wave6-regdefine.h"
+#include "wave6-vpuconfig.h"
+#include "wave6-hw.h"
+#include "wave6-vpu-dbg.h"
+
+#define CREATE_TRACE_POINTS
+#include "wave6-trace.h"
+
+#define WAVE6_VPU_DEBUGFS_DIR "wave6"
+
+static irqreturn_t wave6_vpu_core_irq(int irq, void *dev_id)
+{
+	struct vpu_core_device *core = dev_id;
+	struct vpu_irq irq_info;
+
+	if (!vpu_read_reg(core, W6_VPU_VPU_INT_STS))
+		return IRQ_NONE;
+
+	irq_info.status = vpu_read_reg(core, W6_VPU_VINT_REASON);
+	irq_info.inst_idc = vpu_read_reg(core, W6_RET_INT_INSTANCE_INFO);
+
+	vpu_write_reg(core, W6_RET_INT_INSTANCE_INFO, INT_INSTANCE_INFO_CLEAR);
+	vpu_write_reg(core, W6_VPU_VINT_REASON_CLEAR, irq_info.status);
+	vpu_write_reg(core, W6_VPU_VINT_CLEAR, VINT_CLEAR);
+
+	trace_wave6_vpu_irq(core, irq_info.status, irq_info.inst_idc);
+
+	if (irq_info.status & BIT(W6_INT_BIT_REQ_WORK_BUF)) {
+		if (core->vpu)
+			core->vpu->req_work_buffer(core->vpu, core);
+
+		return IRQ_HANDLED;
+	}
+
+	if (kfifo_in(&core->irq_fifo, &irq_info, sizeof(struct vpu_irq)) !=
+	    sizeof(struct vpu_irq))
+		dev_err(core->dev, "IRQ overflow status 0x%x idc 0x%x\n",
+			irq_info.status, irq_info.inst_idc);
+
+	return IRQ_WAKE_THREAD;
+}
+
+static struct vpu_instance *wave6_vpu_core_get_instance(struct vpu_core_device *core,
+							u32 inst_idc)
+{
+	struct vpu_instance *inst;
+
+	guard(spinlock)(&core->inst_lock);
+
+	list_for_each_entry(inst, &core->instances, list) {
+		if ((BIT(inst->id) & inst_idc) && inst->enable) {
+			atomic_inc(&inst->refcount);
+			return inst;
+		}
+	}
+
+	return NULL;
+}
+
+static void wave6_vpu_core_put_instance(struct vpu_instance *inst)
+{
+	if (!inst)
+		return;
+
+	guard(spinlock)(&inst->dev->inst_lock);
+	atomic_dec_if_positive(&inst->refcount);
+}
+
+void wave6_vpu_enable_instance(struct vpu_instance *inst)
+{
+	scoped_guard(spinlock, &inst->dev->inst_lock) {
+		atomic_set(&inst->refcount, 0);
+		inst->enable = true;
+	}
+}
+
+void wave6_vpu_disable_instance(struct vpu_instance *inst)
+{
+	int count;
+
+	scoped_guard(spinlock, &inst->dev->inst_lock)
+		inst->enable = false;
+
+	if (read_poll_timeout(atomic_read, count, !count,
+			      W6_VPU_POLL_DELAY_US,
+			      W6_VPU_POLL_TIMEOUT,
+			      true, &inst->refcount))
+		dev_dbg(inst->dev->dev, "[%d] disable timeout\n", inst->id);
+}
+
+static irqreturn_t wave6_vpu_core_irq_thread(int irq, void *dev_id)
+{
+	struct vpu_core_device *core = dev_id;
+	struct vpu_instance *inst;
+	struct vpu_irq irq_info;
+
+	while (kfifo_len(&core->irq_fifo)) {
+		bool error = false;
+
+		if (!kfifo_out(&core->irq_fifo, &irq_info, sizeof(struct vpu_irq)))
+			break;
+
+		inst = wave6_vpu_core_get_instance(core, irq_info.inst_idc);
+		if (!inst)
+			continue;
+
+		if ((irq_info.status & BIT(W6_INT_BIT_INIT_SEQ)) ||
+		    (irq_info.status & BIT(W6_INT_BIT_ENC_SET_PARAM))) {
+			complete(&inst->irq_done);
+			wave6_vpu_core_put_instance(inst);
+			continue;
+		}
+
+		if (irq_info.status & BIT(W6_INT_BIT_BSBUF_ERROR))
+			error = true;
+
+		if (inst->ops && inst->ops->finish_process)
+			inst->ops->finish_process(inst, error);
+
+		wave6_vpu_core_put_instance(inst);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static int wave6_vpu_core_check_state(struct vpu_core_device *core)
+{
+	u32 val;
+	int ret;
+
+	guard(mutex)(&core->hw_lock);
+
+	ret = read_poll_timeout(vpu_read_reg, val, val != 0,
+				W6_VPU_POLL_DELAY_US, W6_VPU_POLL_TIMEOUT,
+				false, core, W6_VPU_VCPU_CUR_PC);
+	if (ret)
+		return ret;
+
+	wave6_vpu_enable_interrupt(core);
+	ret = wave6_vpu_get_version(core);
+	if (ret) {
+		dev_err(core->dev, "wave6_vpu_get_version fail\n");
+		return ret;
+	}
+
+	dev_dbg(core->dev, "product 0x%x, fw_ver %d.%d.%d(r%d), hw_ver 0x%x\n",
+		core->attr.product_code,
+		FW_VERSION_MAJOR(core->attr.fw_version),
+		FW_VERSION_MINOR(core->attr.fw_version),
+		FW_VERSION_REL(core->attr.fw_version),
+		core->attr.fw_revision,
+		core->attr.hw_version);
+
+	if (core->attr.fw_version < core->res->compatible_fw_version)
+		dev_err(core->dev, "fw version is too low (< v%d.%d.%d)\n",
+			FW_VERSION_MAJOR(core->res->compatible_fw_version),
+			FW_VERSION_MINOR(core->res->compatible_fw_version),
+			FW_VERSION_REL(core->res->compatible_fw_version));
+
+	return 0;
+}
+
+void wave6_vpu_core_activate(struct vpu_core_device *core)
+{
+	core->active = true;
+}
+
+static int wave6_vpu_core_wait_activated(struct vpu_core_device *core)
+{
+	if (!core->active)
+		return 0;
+
+	return wave6_vpu_core_check_state(core);
+}
+
+static int wave6_vpu_core_probe(struct platform_device *pdev)
+{
+	struct vpu_core_device *core;
+	const struct wave6_vpu_core_resource *res;
+	int ret;
+	int irq;
+
+	res = dev_get_platdata(&pdev->dev);
+	if (!res)
+		return -ENODEV;
+
+	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
+	if (ret < 0) {
+		dev_err(&pdev->dev, "failed to set DMA mask: %d\n", ret);
+		return ret;
+	}
+
+	core = devm_kzalloc(&pdev->dev, sizeof(*core), GFP_KERNEL);
+	if (!core)
+		return -ENOMEM;
+
+	ret = devm_mutex_init(&pdev->dev, &core->dev_lock);
+	if (ret)
+		return ret;
+
+	ret = devm_mutex_init(&pdev->dev, &core->hw_lock);
+	if (ret)
+		return ret;
+
+	spin_lock_init(&core->inst_lock);
+	INIT_LIST_HEAD(&core->instances);
+	dev_set_drvdata(&pdev->dev, core);
+	core->dev = &pdev->dev;
+	core->res = res;
+
+	if (pdev->dev.parent->driver && pdev->dev.parent->driver->name &&
+	    !strcmp(pdev->dev.parent->driver->name, WAVE6_VPU_PLATFORM_DRIVER_NAME))
+		core->vpu = dev_get_drvdata(pdev->dev.parent);
+
+	core->reg_base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(core->reg_base))
+		return PTR_ERR(core->reg_base);
+
+	ret = devm_clk_bulk_get_all(&pdev->dev, &core->clks);
+	if (ret < 0)
+		return dev_err_probe(&pdev->dev, ret, "failed to get clocks\n");
+
+	core->num_clks = ret;
+
+	ret = kfifo_init(&core->irq_fifo, core->irq_buffer,
+			 sizeof(core->irq_buffer));
+	if (ret)
+		return ret;
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0)
+		return irq;
+
+	ret = devm_request_threaded_irq(&pdev->dev, irq,
+					wave6_vpu_core_irq,
+					wave6_vpu_core_irq_thread,
+					0, "vpu_irq", core);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to request IRQ: %d\n", ret);
+		return ret;
+	}
+
+	ret = v4l2_device_register(&pdev->dev, &core->v4l2_dev);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to register v4l2_dev: %d\n", ret);
+		return ret;
+	}
+
+	ret = wave6_vpu_init_m2m_dev(core);
+	if (ret)
+		goto err_v4l2_unregister;
+
+	core->temp_vbuf.size = ALIGN(W6_TEMPBUF_SIZE, 4096);
+	ret = wave6_vdi_alloc_dma(core->dev, &core->temp_vbuf);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to allocate temp_vbuf: %d\n", ret);
+		goto err_m2m_dev_release;
+	}
+
+	core->debugfs = debugfs_lookup(WAVE6_VPU_DEBUGFS_DIR, NULL);
+	if (!IS_ERR_OR_NULL(core->debugfs))
+		dput(core->debugfs);
+	else
+		core->debugfs = debugfs_create_dir(WAVE6_VPU_DEBUGFS_DIR, NULL);
+
+	pm_runtime_enable(&pdev->dev);
+
+	if (core->res->codec_types & WAVE6_IS_DEC) {
+		ret = wave6_vpu_dec_register_device(core);
+		if (ret) {
+			dev_err(&pdev->dev,
+				"failed to register video_dev_dec: %d\n", ret);
+			goto err_temp_vbuf_free;
+		}
+	}
+	if (core->res->codec_types & WAVE6_IS_ENC) {
+		ret = wave6_vpu_enc_register_device(core);
+		if (ret) {
+			dev_err(&pdev->dev,
+				"failed to register video_dev_enc: %d\n", ret);
+			goto err_dec_unreg;
+		}
+	}
+
+	dev_dbg(&pdev->dev, "Added wave6 driver with caps %s %s\n",
+		core->res->codec_types & WAVE6_IS_ENC ? "'ENCODE'" : "",
+		core->res->codec_types & WAVE6_IS_DEC ? "'DECODE'" : "");
+
+	return 0;
+
+err_dec_unreg:
+	if (core->res->codec_types & WAVE6_IS_DEC)
+		wave6_vpu_dec_unregister_device(core);
+err_temp_vbuf_free:
+	pm_runtime_disable(&pdev->dev);
+	wave6_vdi_free_dma(&core->temp_vbuf);
+err_m2m_dev_release:
+	wave6_vpu_release_m2m_dev(core);
+err_v4l2_unregister:
+	v4l2_device_unregister(&core->v4l2_dev);
+
+	return ret;
+}
+
+static void wave6_vpu_core_remove(struct platform_device *pdev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(&pdev->dev);
+
+	wave6_vpu_enc_unregister_device(core);
+	wave6_vpu_dec_unregister_device(core);
+	pm_runtime_disable(&pdev->dev);
+	wave6_vdi_free_dma(&core->temp_vbuf);
+	wave6_vpu_release_m2m_dev(core);
+	v4l2_device_unregister(&core->v4l2_dev);
+}
+
+static int wave6_vpu_core_runtime_suspend(struct device *dev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(dev);
+
+	if (WARN_ON(!core))
+		return -ENODEV;
+
+	/*
+	 * Only call parent VPU put_vpu if the core has a parent and is active.
+	 * - core->vpu: prevent access in core without parent VPU.
+	 * - core->active: execute sleep only after m2m streaming is started.
+	 */
+	if (core->vpu && core->active)
+		core->vpu->put_vpu(core->vpu, core);
+
+	if (core->num_clks)
+		clk_bulk_disable_unprepare(core->num_clks, core->clks);
+
+	return 0;
+}
+
+static int wave6_vpu_core_runtime_resume(struct device *dev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(dev);
+	bool boot_vpu;
+	int ret;
+
+	if (WARN_ON(!core))
+		return -ENODEV;
+
+	if (core->num_clks) {
+		ret = clk_bulk_prepare_enable(core->num_clks, core->clks);
+		if (ret) {
+			dev_err(dev, "failed to enable clocks: %d\n", ret);
+			return ret;
+		}
+	}
+
+	/*
+	 * Only call parent VPU get_vpu if the core has a parent and is active.
+	 * - core->vpu: prevent access in core without parent VPU.
+	 * - core->active: execute boot only after m2m streaming is started.
+	 */
+	boot_vpu = core->vpu && core->active;
+	if (boot_vpu) {
+		ret = core->vpu->get_vpu(core->vpu, core);
+		if (ret)
+			goto error_get_vpu;
+	}
+
+	ret = wave6_vpu_core_wait_activated(core);
+	if (ret)
+		goto error_activate;
+
+	return 0;
+
+error_activate:
+	if (boot_vpu)
+		core->vpu->put_vpu(core->vpu, core);
+error_get_vpu:
+	if (core->num_clks)
+		clk_bulk_disable_unprepare(core->num_clks, core->clks);
+
+	return ret;
+}
+
+static int wave6_vpu_core_suspend(struct device *dev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(dev);
+	int ret;
+
+	v4l2_m2m_suspend(core->m2m_dev);
+
+	ret = pm_runtime_force_suspend(dev);
+	if (ret)
+		v4l2_m2m_resume(core->m2m_dev);
+
+	return ret;
+}
+
+static int wave6_vpu_core_resume(struct device *dev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(dev);
+	int ret;
+
+	ret = pm_runtime_force_resume(dev);
+	if (ret)
+		return ret;
+
+	v4l2_m2m_resume(core->m2m_dev);
+
+	return 0;
+}
+
+static const struct dev_pm_ops wave6_vpu_core_pm_ops = {
+	RUNTIME_PM_OPS(wave6_vpu_core_runtime_suspend,
+		       wave6_vpu_core_runtime_resume, NULL)
+	SYSTEM_SLEEP_PM_OPS(wave6_vpu_core_suspend,
+			    wave6_vpu_core_resume)
+};
+
+static struct platform_driver wave6_vpu_core_driver = {
+	.driver = {
+		.name = WAVE6_VPU_CORE_PLATFORM_DRIVER_NAME,
+		.pm = pm_ptr(&wave6_vpu_core_pm_ops),
+	},
+	.probe = wave6_vpu_core_probe,
+	.remove = wave6_vpu_core_remove,
+};
+
+module_platform_driver(wave6_vpu_core_driver);
+MODULE_ALIAS("platform:" WAVE6_VPU_CORE_PLATFORM_DRIVER_NAME);
+MODULE_DESCRIPTION("chips&media Wave6 VPU CORE V4L2 driver");
+MODULE_AUTHOR("CHIPS&MEDIA INC");
+MODULE_LICENSE("Dual BSD/GPL");
drivers/media/platform/chips-media/wave6/wave6-vpu-core.h new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
2.31.1
#19 lore raw

Re: [PATCH v7 5/9] media: chips-media: wave6: Add Wave6 core driver

Thank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider: - [Critical] V4L2 lifecycle mismatch causes Use-After-Free of `core` structure when file descriptors are…

Thank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider:
- [Critical] V4L2 lifecycle mismatch causes Use-After-Free of core structure when file descriptors are closed after driver unbind or probe error.
- [High] Polling a reference count with a timeout guarantees a Use-After-Free under scheduling delays.
- [High] Hardware interrupt events are dropped due to early returns and incomplete bitmask processing, causing instances to hang.
- [High] Delayed work task_timer is leaked on module remove, leading to a use-after-free.
- [High] The IRQ handler and thread can execute after driver teardown destroys critical resources, leading to Use-After-Free.
- [High] Hardware registers are accessed in the interrupt handler without validating that the device is not in a runtime PM suspended state.
- [High] Requesting the IRQ before enabling clocks and runtime PM can lead to a bus error if a spurious or pending interrupt fires.
- [High] DMA memory core->temp_vbuf is freed while the hardware may still be running.
- [Medium] Storing a debugfs dentry pointer after dropping its reference leads to a dangling pointer and Use-After-Free.
- [Medium] Debugfs directory is never removed, causing a memory leak.
--

commit 7b3f41c4df55c9ee33568cd7f9d838b5f4de81b9
Author: Nas Chung <[email protected]>

media: chips-media: wave6: Add Wave6 core driver

Add the core driver for the Chips&Media Wave6 video codec IP.

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +0 -0
new file mode 100644
index 0000000000000..89cce27802cfe

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 34

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +7 -0
+static irqreturn_t wave6_vpu_core_irq(int irq, void *dev_id)
+{
+	struct vpu_core_device *core = dev_id;
+	struct vpu_irq irq_info;
+
+	if (!vpu_read_reg(core, W6_VPU_VPU_INT_STS))
+		return IRQ_NONE;

[Severity: High]
Does this code risk triggering a system bus error? The interrupt handler unconditionally reads W6_VPU_VPU_INT_STS without first checking if the device is runtime suspended. Subsystem guidelines recommend using pm_runtime_get_if_active() before accessing hardware registers in an IRQ handler to ensure clocks are enabled.

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +6 -0
+
+	irq_info.status = vpu_read_reg(core, W6_VPU_VINT_REASON);
+	irq_info.inst_idc = vpu_read_reg(core, W6_RET_INT_INSTANCE_INFO);
+
+	vpu_write_reg(core, W6_RET_INT_INSTANCE_INFO, INT_INSTANCE_INFO_CLEAR);
+	vpu_write_reg(core, W6_VPU_VINT_REASON_CLEAR, irq_info.status);

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 50

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +10 -0
+	vpu_write_reg(core, W6_VPU_VINT_CLEAR, VINT_CLEAR);
+
+	trace_wave6_vpu_irq(core, irq_info.status, irq_info.inst_idc);
+
+	if (irq_info.status & BIT(W6_INT_BIT_REQ_WORK_BUF)) {
+		if (core->vpu)
+			core->vpu->req_work_buffer(core->vpu, core);
+
+		return IRQ_HANDLED;
+	}

[Severity: High]
If W6_INT_BIT_REQ_WORK_BUF is set, this returns IRQ_HANDLED immediately. Doesn't this discard any other pending interrupt events (like INIT_SEQ) that might be present in irq_info.status, potentially causing other instances to hang?

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +12 -0
+
+	if (kfifo_in(&core->irq_fifo, &irq_info, sizeof(struct vpu_irq)) !=
+	    sizeof(struct vpu_irq))
+		dev_err(core->dev, "IRQ overflow status 0x%x idc 0x%x\n",
+			irq_info.status, irq_info.inst_idc);
+
+	return IRQ_WAKE_THREAD;
+}
+
+static struct vpu_instance *wave6_vpu_core_get_instance(struct vpu_core_device *core,
+							u32 inst_idc)
+{

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 72

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +10 -0
+	struct vpu_instance *inst;
+
+	guard(spinlock)(&core->inst_lock);
+
+	list_for_each_entry(inst, &core->instances, list) {
+		if ((BIT(inst->id) & inst_idc) && inst->enable) {
+			atomic_inc(&inst->refcount);
+			return inst;
+		}
+	}

[Severity: High]
This loops through the instances and returns the first one that matches a set bit in inst_idc. Since inst_idc is a bitmask, what happens if multiple instances complete simultaneously? The IRQ thread will process only this first instance and ignore the other set bits, causing them to hang.

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +3 -0
+
+	return NULL;
+}

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 106

Quoted 13 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +13 -0
+void wave6_vpu_disable_instance(struct vpu_instance *inst)
+{
+	int count;
+
+	scoped_guard(spinlock, &inst->dev->inst_lock)
+		inst->enable = false;
+
+	if (read_poll_timeout(atomic_read, count, !count,
+			      W6_VPU_POLL_DELAY_US,
+			      W6_VPU_POLL_TIMEOUT,
+			      true, &inst->refcount))
+		dev_dbg(inst->dev->dev, "[%d] disable timeout\n", inst->id);
+}

[Severity: High]
Is it safe to return from here if the polling times out? If the IRQ thread is preempted and delayed, this timeout can expire while the IRQ thread still holds a reference to the instance. The caller will assume the instance is disabled and free it, leading to a use-after-free when the delayed IRQ thread resumes and calls inst->ops->finish_process().

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +2 -0
+static int wave6_vpu_core_probe(struct platform_device *pdev)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 254

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +4 -0
+	ret = devm_request_threaded_irq(&pdev->dev, irq,
+					wave6_vpu_core_irq,
+					wave6_vpu_core_irq_thread,
+					0, "vpu_irq", core);

[Severity: High]
Can requesting the IRQ here lead to a crash? The IRQ is requested before pm_runtime_enable() is called and before the device clocks are powered on. If a pending or spurious interrupt fires immediately, the handler will read MMIO registers while the hardware clocks are disabled.

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +4 -0
+	if (ret) {
+		dev_err(&pdev->dev, "failed to request IRQ: %d\n", ret);
+		return ret;
+	}

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 281

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +5 -0
+	core->debugfs = debugfs_lookup(WAVE6_VPU_DEBUGFS_DIR, NULL);
+	if (!IS_ERR_OR_NULL(core->debugfs))
+		dput(core->debugfs);
+	else
+		core->debugfs = debugfs_create_dir(WAVE6_VPU_DEBUGFS_DIR, NULL);

[Severity: Medium]
Does this create a dangling pointer? The reference obtained from debugfs_lookup() is immediately dropped via dput(), but the raw pointer is retained in core->debugfs for later use. If the debugfs directory is removed manually, subsequent uses of this pointer will cause a use-after-free.

[Severity: Medium]
Additionally, it appears the debugfs directory created here is never tracked for removal. Repeatedly loading and unloading the driver, or failing probe after this point, seems to permanently leak the debugfs directory.

Quoted 9 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +9 -0
+
+	pm_runtime_enable(&pdev->dev);
+
+	if (core->res->codec_types & WAVE6_IS_DEC) {
+		ret = wave6_vpu_dec_register_device(core);
+		if (ret) {
+			dev_err(&pdev->dev,
+				"failed to register video_dev_dec: %d\n", ret);
+			goto err_temp_vbuf_free;

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 300

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +10 -0
+		}
+	}
+	if (core->res->codec_types & WAVE6_IS_ENC) {
+		ret = wave6_vpu_enc_register_device(core);
+		if (ret) {
+			dev_err(&pdev->dev,
+				"failed to register video_dev_enc: %d\n", ret);
+			goto err_dec_unreg;
+		}
+	}

[Severity: Critical]
Does this create a use-after-free condition via V4L2 lifecycle mismatch? The core structure is allocated earlier using devm_kzalloc(), tying its lifetime to the device binding. However, wave6_vpu_dec_register_device() exposes the decoder to userspace here.

If wave6_vpu_enc_register_device() fails on the next step (or if the driver is later unbound via sysfs), devres will free the core structure. If a userspace process has already opened the /dev/videoX node and holds a file descriptor, closing it later will invoke V4L2 release callbacks that dereference the freed core structure.

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 329

Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +8 -0
+static void wave6_vpu_core_remove(struct platform_device *pdev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(&pdev->dev);
+
+	wave6_vpu_enc_unregister_device(core);
+	wave6_vpu_dec_unregister_device(core);
+	pm_runtime_disable(&pdev->dev);
+	wave6_vdi_free_dma(&core->temp_vbuf);

[Severity: High]
Is it safe to free this shared DMA memory without first instructing the hardware to halt or waiting for active jobs to finish? If the VPU is still running, it will continue DMA operations into the freed pages.

[Severity: High]
Also, does this function leak the delayed work? The driver schedules core->task_timer during streaming, but cancel_delayed_work_sync() is never called here. If the work is pending when the device unbinds, it will execute later and dereference the freed core structure.

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +2 -0
+	wave6_vpu_release_m2m_dev(core);
+	v4l2_device_unregister(&core->v4l2_dev);

[Severity: High]
Can the devres-managed IRQ thread execute concurrently with or after this teardown? The IRQ was requested with devm_request_threaded_irq(), meaning it remains active until after this remove() function returns. However, wave6_vpu_release_m2m_dev() destroys M2M structures here. If an interrupt fires during or just after this step, the still-active IRQ thread will dereference the destroyed M2M and V4L2 structures.

Quoted 1 line

+}

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#20 lore raw

Re: [PATCH v7 5/9] media: chips-media: wave6: Add Wave6 core driver

In reply to Nas Chung
Quoted 246 lines · Nas Chung

Add the core driver for the Chips&Media Wave6 video codec IP.

The hardware contains one control register region and four interface register regions for a shared video processing engine. This driver handles the interface register regions, each with its own MMIO range and interrupt, while relying on the control driver for firmware loading and shared resource management.

It configures the V4L2 mem2mem devices and communicates with the Wave6 hardware to perform video processing tasks.

---
 .../chips-media/wave6/wave6-vpu-core.c        | 448 ++++++++++++++++++
 .../chips-media/wave6/wave6-vpu-core.h        | 126 +++++
 2 files changed, 574 insertions(+)
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-core.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-core.h
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +219 -0
new file mode 100644
index 000000000000..89cce27802cf
@@ -0,0 +1,448 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+/*
+ * Wave6 series multi-standard codec IP - wave6 core driver
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/clk.h>
+#include <linux/firmware.h>
+#include <linux/interrupt.h>
+#include <linux/pm_runtime.h>
+#include <linux/debugfs.h>
+#include <linux/iopoll.h>
+#include "wave6-vpu-core.h"
+#include "wave6-regdefine.h"
+#include "wave6-vpuconfig.h"
+#include "wave6-hw.h"
+#include "wave6-vpu-dbg.h"
+
+#define CREATE_TRACE_POINTS
+#include "wave6-trace.h"
+
+#define WAVE6_VPU_DEBUGFS_DIR "wave6"
+
+static irqreturn_t wave6_vpu_core_irq(int irq, void *dev_id)
+{
+	struct vpu_core_device *core = dev_id;
+	struct vpu_irq irq_info;
+
+	if (!vpu_read_reg(core, W6_VPU_VPU_INT_STS))
+		return IRQ_NONE;
+
+	irq_info.status = vpu_read_reg(core, W6_VPU_VINT_REASON);
+	irq_info.inst_idc = vpu_read_reg(core, W6_RET_INT_INSTANCE_INFO);
+
+	vpu_write_reg(core, W6_RET_INT_INSTANCE_INFO, INT_INSTANCE_INFO_CLEAR);
+	vpu_write_reg(core, W6_VPU_VINT_REASON_CLEAR, irq_info.status);
+	vpu_write_reg(core, W6_VPU_VINT_CLEAR, VINT_CLEAR);
+
+	trace_wave6_vpu_irq(core, irq_info.status, irq_info.inst_idc);
+
+	if (irq_info.status & BIT(W6_INT_BIT_REQ_WORK_BUF)) {
+		if (core->vpu)
+			core->vpu->req_work_buffer(core->vpu, core);
+
+		return IRQ_HANDLED;
+	}
+
+	if (kfifo_in(&core->irq_fifo, &irq_info, sizeof(struct vpu_irq)) !=
+	    sizeof(struct vpu_irq))
+		dev_err(core->dev, "IRQ overflow status 0x%x idc 0x%x\n",
+			irq_info.status, irq_info.inst_idc);
+
+	return IRQ_WAKE_THREAD;
+}
+
+static struct vpu_instance *wave6_vpu_core_get_instance(struct vpu_core_device *core,
+							u32 inst_idc)
+{
+	struct vpu_instance *inst;
+
+	guard(spinlock)(&core->inst_lock);
+
+	list_for_each_entry(inst, &core->instances, list) {
+		if ((BIT(inst->id) & inst_idc) && inst->enable) {
+			atomic_inc(&inst->refcount);
+			return inst;
+		}
+	}
+
+	return NULL;
+}
+
+static void wave6_vpu_core_put_instance(struct vpu_instance *inst)
+{
+	if (!inst)
+		return;
+
+	guard(spinlock)(&inst->dev->inst_lock);
+	atomic_dec_if_positive(&inst->refcount);
+}
+
+void wave6_vpu_enable_instance(struct vpu_instance *inst)
+{
+	scoped_guard(spinlock, &inst->dev->inst_lock) {
+		atomic_set(&inst->refcount, 0);
+		inst->enable = true;
+	}
+}
+
+void wave6_vpu_disable_instance(struct vpu_instance *inst)
+{
+	int count;
+
+	scoped_guard(spinlock, &inst->dev->inst_lock)
+		inst->enable = false;
+
+	if (read_poll_timeout(atomic_read, count, !count,
+			      W6_VPU_POLL_DELAY_US,
+			      W6_VPU_POLL_TIMEOUT,
+			      true, &inst->refcount))
+		dev_dbg(inst->dev->dev, "[%d] disable timeout\n", inst->id);
+}
+
+static irqreturn_t wave6_vpu_core_irq_thread(int irq, void *dev_id)
+{
+	struct vpu_core_device *core = dev_id;
+	struct vpu_instance *inst;
+	struct vpu_irq irq_info;
+
+	while (kfifo_len(&core->irq_fifo)) {
+		bool error = false;
+
+		if (!kfifo_out(&core->irq_fifo, &irq_info, sizeof(struct vpu_irq)))
+			break;
+
+		inst = wave6_vpu_core_get_instance(core, irq_info.inst_idc);
+		if (!inst)
+			continue;
+
+		if ((irq_info.status & BIT(W6_INT_BIT_INIT_SEQ)) ||
+		    (irq_info.status & BIT(W6_INT_BIT_ENC_SET_PARAM))) {
+			complete(&inst->irq_done);
+			wave6_vpu_core_put_instance(inst);
+			continue;
+		}
+
+		if (irq_info.status & BIT(W6_INT_BIT_BSBUF_ERROR))
+			error = true;
+
+		if (inst->ops && inst->ops->finish_process)
+			inst->ops->finish_process(inst, error);
+
+		wave6_vpu_core_put_instance(inst);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static int wave6_vpu_core_check_state(struct vpu_core_device *core)
+{
+	u32 val;
+	int ret;
+
+	guard(mutex)(&core->hw_lock);
+
+	ret = read_poll_timeout(vpu_read_reg, val, val != 0,
+				W6_VPU_POLL_DELAY_US, W6_VPU_POLL_TIMEOUT,
+				false, core, W6_VPU_VCPU_CUR_PC);
+	if (ret)
+		return ret;
+
+	wave6_vpu_enable_interrupt(core);
+	ret = wave6_vpu_get_version(core);
+	if (ret) {
+		dev_err(core->dev, "wave6_vpu_get_version fail\n");
+		return ret;
+	}
+
+	dev_dbg(core->dev, "product 0x%x, fw_ver %d.%d.%d(r%d), hw_ver 0x%x\n",
+		core->attr.product_code,
+		FW_VERSION_MAJOR(core->attr.fw_version),
+		FW_VERSION_MINOR(core->attr.fw_version),
+		FW_VERSION_REL(core->attr.fw_version),
+		core->attr.fw_revision,
+		core->attr.hw_version);
+
+	if (core->attr.fw_version < core->res->compatible_fw_version)
+		dev_err(core->dev, "fw version is too low (< v%d.%d.%d)\n",
+			FW_VERSION_MAJOR(core->res->compatible_fw_version),
+			FW_VERSION_MINOR(core->res->compatible_fw_version),
+			FW_VERSION_REL(core->res->compatible_fw_version));
+
+	return 0;
+}
+
+void wave6_vpu_core_activate(struct vpu_core_device *core)
+{
+	core->active = true;
+}
+
+static int wave6_vpu_core_wait_activated(struct vpu_core_device *core)
+{
+	if (!core->active)
+		return 0;
+
+	return wave6_vpu_core_check_state(core);
+}
+
+static int wave6_vpu_core_probe(struct platform_device *pdev)
+{
+	struct vpu_core_device *core;
+	const struct wave6_vpu_core_resource *res;
+	int ret;
+	int irq;
+
+	res = dev_get_platdata(&pdev->dev);
+	if (!res)
+		return -ENODEV;
+
+	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
+	if (ret < 0) {
+		dev_err(&pdev->dev, "failed to set DMA mask: %d\n", ret);
+		return ret;
+	}
+
+	core = devm_kzalloc(&pdev->dev, sizeof(*core), GFP_KERNEL);
+	if (!core)
+		return -ENOMEM;
+
+	ret = devm_mutex_init(&pdev->dev, &core->dev_lock);
+	if (ret)
+		return ret;
+
+	ret = devm_mutex_init(&pdev->dev, &core->hw_lock);
+	if (ret)

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 229

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +10 -0
+		return ret;
+
+	spin_lock_init(&core->inst_lock);
+	INIT_LIST_HEAD(&core->instances);
+	dev_set_drvdata(&pdev->dev, core);
+	core->dev = &pdev->dev;
+	core->res = res;
+
+	if (pdev->dev.parent->driver && pdev->dev.parent->driver->name &&
+	    !strcmp(pdev->dev.parent->driver->name, WAVE6_VPU_PLATFORM_DRIVER_NAME))

why need check this?

Quoted 44 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +44 -0
+		core->vpu = dev_get_drvdata(pdev->dev.parent);
+
+	core->reg_base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(core->reg_base))
+		return PTR_ERR(core->reg_base);
+
+	ret = devm_clk_bulk_get_all(&pdev->dev, &core->clks);
+	if (ret < 0)
+		return dev_err_probe(&pdev->dev, ret, "failed to get clocks\n");
+
+	core->num_clks = ret;
+
+	ret = kfifo_init(&core->irq_fifo, core->irq_buffer,
+			 sizeof(core->irq_buffer));
+	if (ret)
+		return ret;
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0)
+		return irq;
+
+	ret = devm_request_threaded_irq(&pdev->dev, irq,
+					wave6_vpu_core_irq,
+					wave6_vpu_core_irq_thread,
+					0, "vpu_irq", core);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to request IRQ: %d\n", ret);
+		return ret;
+	}
+
+	ret = v4l2_device_register(&pdev->dev, &core->v4l2_dev);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to register v4l2_dev: %d\n", ret);
+		return ret;
+	}
+
+	ret = wave6_vpu_init_m2m_dev(core);
+	if (ret)
+		goto err_v4l2_unregister;
+
+	core->temp_vbuf.size = ALIGN(W6_TEMPBUF_SIZE, 4096);
+	ret = wave6_vdi_alloc_dma(core->dev, &core->temp_vbuf);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to allocate temp_vbuf: %d\n", ret);

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 283

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +10 -0
+		goto err_m2m_dev_release;
+	}
+
+	core->debugfs = debugfs_lookup(WAVE6_VPU_DEBUGFS_DIR, NULL);
+	if (!IS_ERR_OR_NULL(core->debugfs))
+		dput(core->debugfs);
+	else
+		core->debugfs = debugfs_create_dir(WAVE6_VPU_DEBUGFS_DIR, NULL);
+
+	pm_runtime_enable(&pdev->dev);

devm_pm_runtime_enable()

Quoted 24 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +24 -0
+
+	if (core->res->codec_types & WAVE6_IS_DEC) {
+		ret = wave6_vpu_dec_register_device(core);
+		if (ret) {
+			dev_err(&pdev->dev,
+				"failed to register video_dev_dec: %d\n", ret);
+			goto err_temp_vbuf_free;
+		}
+	}
+	if (core->res->codec_types & WAVE6_IS_ENC) {
+		ret = wave6_vpu_enc_register_device(core);
+		if (ret) {
+			dev_err(&pdev->dev,
+				"failed to register video_dev_enc: %d\n", ret);
+			goto err_dec_unreg;
+		}
+	}
+
+	dev_dbg(&pdev->dev, "Added wave6 driver with caps %s %s\n",
+		core->res->codec_types & WAVE6_IS_ENC ? "'ENCODE'" : "",
+		core->res->codec_types & WAVE6_IS_DEC ? "'DECODE'" : "");
+
+	return 0;
+

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 317

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +10 -0
+err_dec_unreg:
+	if (core->res->codec_types & WAVE6_IS_DEC)
+		wave6_vpu_dec_unregister_device(core);
+err_temp_vbuf_free:
+	pm_runtime_disable(&pdev->dev);
+	wave6_vdi_free_dma(&core->temp_vbuf);
+err_m2m_dev_release:
+	wave6_vpu_release_m2m_dev(core);
+err_v4l2_unregister:
+	v4l2_device_unregister(&core->v4l2_dev);

can you try use devm_add_action_or_reset() to elimiate these error label?

Quoted 22 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +22 -0
+
+	return ret;
+}
+
+static void wave6_vpu_core_remove(struct platform_device *pdev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(&pdev->dev);
+
+	wave6_vpu_enc_unregister_device(core);
+	wave6_vpu_dec_unregister_device(core);
+	pm_runtime_disable(&pdev->dev);
+	wave6_vdi_free_dma(&core->temp_vbuf);
+	wave6_vpu_release_m2m_dev(core);
+	v4l2_device_unregister(&core->v4l2_dev);
+}
+
+static int wave6_vpu_core_runtime_suspend(struct device *dev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(dev);
+
+	if (WARN_ON(!core))
+		return -ENODEV;

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 349

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +10 -0
+
+	/*
+	 * Only call parent VPU put_vpu if the core has a parent and is active.
+	 * - core->vpu: prevent access in core without parent VPU.
+	 * - core->active: execute sleep only after m2m streaming is started.
+	 */
+	if (core->vpu && core->active)
+		core->vpu->put_vpu(core->vpu, core);
+
+	if (core->num_clks)

Need check it. clk_bulk_disable_unprepare() is empty ops when num_clks is 0

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +5 -0
+		clk_bulk_disable_unprepare(core->num_clks, core->clks);
+
+	return 0;
+}
+

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 364

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +10 -0
+static int wave6_vpu_core_runtime_resume(struct device *dev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(dev);
+	bool boot_vpu;
+	int ret;
+
+	if (WARN_ON(!core))
+		return -ENODEV;
+
+	if (core->num_clks) {

need checkit. clk_bulk_prepare_enable() is empty ops when num_clks is 0

Frank

Quoted 219 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +84 -0
+		ret = clk_bulk_prepare_enable(core->num_clks, core->clks);
+		if (ret) {
+			dev_err(dev, "failed to enable clocks: %d\n", ret);
+			return ret;
+		}
+	}
+
+	/*
+	 * Only call parent VPU get_vpu if the core has a parent and is active.
+	 * - core->vpu: prevent access in core without parent VPU.
+	 * - core->active: execute boot only after m2m streaming is started.
+	 */
+	boot_vpu = core->vpu && core->active;
+	if (boot_vpu) {
+		ret = core->vpu->get_vpu(core->vpu, core);
+		if (ret)
+			goto error_get_vpu;
+	}
+
+	ret = wave6_vpu_core_wait_activated(core);
+	if (ret)
+		goto error_activate;
+
+	return 0;
+
+error_activate:
+	if (boot_vpu)
+		core->vpu->put_vpu(core->vpu, core);
+error_get_vpu:
+	if (core->num_clks)
+		clk_bulk_disable_unprepare(core->num_clks, core->clks);
+
+	return ret;
+}
+
+static int wave6_vpu_core_suspend(struct device *dev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(dev);
+	int ret;
+
+	v4l2_m2m_suspend(core->m2m_dev);
+
+	ret = pm_runtime_force_suspend(dev);
+	if (ret)
+		v4l2_m2m_resume(core->m2m_dev);
+
+	return ret;
+}
+
+static int wave6_vpu_core_resume(struct device *dev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(dev);
+	int ret;
+
+	ret = pm_runtime_force_resume(dev);
+	if (ret)
+		return ret;
+
+	v4l2_m2m_resume(core->m2m_dev);
+
+	return 0;
+}
+
+static const struct dev_pm_ops wave6_vpu_core_pm_ops = {
+	RUNTIME_PM_OPS(wave6_vpu_core_runtime_suspend,
+		       wave6_vpu_core_runtime_resume, NULL)
+	SYSTEM_SLEEP_PM_OPS(wave6_vpu_core_suspend,
+			    wave6_vpu_core_resume)
+};
+
+static struct platform_driver wave6_vpu_core_driver = {
+	.driver = {
+		.name = WAVE6_VPU_CORE_PLATFORM_DRIVER_NAME,
+		.pm = pm_ptr(&wave6_vpu_core_pm_ops),
+	},
+	.probe = wave6_vpu_core_probe,
+	.remove = wave6_vpu_core_remove,
+};
+
+module_platform_driver(wave6_vpu_core_driver);
+MODULE_ALIAS("platform:" WAVE6_VPU_CORE_PLATFORM_DRIVER_NAME);
+MODULE_DESCRIPTION("chips&media Wave6 VPU CORE V4L2 driver");
+MODULE_AUTHOR("CHIPS&MEDIA INC");
+MODULE_LICENSE("Dual BSD/GPL");
drivers/media/platform/chips-media/wave6/wave6-vpu-core.h new file +126 -1
new file mode 100644
index 000000000000..779e8a132f57
@@ -0,0 +1,126 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
+/*
+ * Wave6 series multi-standard codec IP - wave6 core driver
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#ifndef __WAVE6_VPU_CORE_H__
+#define __WAVE6_VPU_CORE_H__
+
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-ioctl.h>
+#include <media/v4l2-event.h>
+#include <media/v4l2-fh.h>
+#include <media/videobuf2-v4l2.h>
+#include <media/videobuf2-dma-contig.h>
+#include "wave6-vpuconfig.h"
+#include "wave6-vpuapi.h"
+
+#define vpu_write_reg(CORE, ADDR, DATA) wave6_vpu_writel(CORE, ADDR, DATA)
+#define vpu_read_reg(CORE, ADDR) wave6_vpu_readl(CORE, ADDR)
+
+struct vpu_buffer {
+	struct v4l2_m2m_buffer v4l2_m2m_buf;
+	bool consumed;
+	bool used;
+	bool error;
+	bool force_key_frame;
+	bool force_frame_qp;
+	u32 force_i_frame_qp;
+	u32 force_p_frame_qp;
+	u32 force_b_frame_qp;
+	ktime_t ts_input;
+	ktime_t ts_start;
+	ktime_t ts_finish;
+	ktime_t ts_output;
+	u64 hw_time;
+	u32 average_qp;
+};
+
+enum vpu_fmt_type {
+	VPU_FMT_TYPE_CODEC	= 0,
+	VPU_FMT_TYPE_RAW	= 1
+};
+
+#define VPU_FMT_FLAG_CBCR_INTERLEAVED	BIT(0)
+#define VPU_FMT_FLAG_CRCB_ORDER		BIT(1)
+#define VPU_FMT_FLAG_10BIT		BIT(2)
+#define VPU_FMT_FLAG_RGB		BIT(3)
+#define VPU_FMT_FLAG_SUPPORT_ROT_MIR	BIT(4)
+
+struct vpu_format {
+	unsigned int v4l2_pix_fmt;
+	unsigned int max_width;
+	unsigned int min_width;
+	unsigned int max_height;
+	unsigned int min_height;
+	unsigned int num_planes;
+	enum frame_buffer_format fb_fmt;
+	enum endian_mode endian;
+	enum csc_format_order csc_fmt_order;
+	unsigned int flags;
+};
+
+static inline struct vpu_instance *wave6_fh_to_vpu_inst(struct v4l2_fh *vfh)
+{
+	return container_of(vfh, struct vpu_instance, v4l2_fh);
+}
+
+static inline struct vpu_instance *wave6_file_to_vpu_inst(struct file *filp)
+{
+	return wave6_fh_to_vpu_inst(file_to_v4l2_fh(filp));
+}
+
+static inline struct vpu_instance *wave6_ctrl_to_vpu_inst(struct v4l2_ctrl *vctrl)
+{
+	return container_of(vctrl->handler, struct vpu_instance, v4l2_ctrl_hdl);
+}
+
+static inline struct vpu_buffer *wave6_to_vpu_buf(struct vb2_v4l2_buffer *vbuf)
+{
+	return container_of(vbuf, struct vpu_buffer, v4l2_m2m_buf.vb);
+}
+
+static inline bool wave6_vpu_both_queues_are_streaming(struct vpu_instance *inst)
+{
+	struct vb2_queue *vq_cap = v4l2_m2m_get_dst_vq(inst->v4l2_fh.m2m_ctx);
+	struct vb2_queue *vq_out = v4l2_m2m_get_src_vq(inst->v4l2_fh.m2m_ctx);
+
+	return vb2_is_streaming(vq_cap) && vb2_is_streaming(vq_out);
+}
+
+u32 wave6_vpu_get_consumed_fb_num(struct vpu_instance *inst);
+void wave6_vpu_core_activate(struct vpu_core_device *core);
+void wave6_update_pix_fmt(struct v4l2_pix_format_mplane *pix_mp,
+			  unsigned int width,
+			  unsigned int height);
+struct vb2_v4l2_buffer *wave6_get_dst_buf_by_addr(struct vpu_instance *inst,
+						  dma_addr_t addr);
+dma_addr_t wave6_get_dma_addr(struct vb2_v4l2_buffer *buf,
+			      unsigned int plane_no);
+enum codec_std wave6_to_codec_std(enum vpu_instance_type type, unsigned int v4l2_pix_fmt);
+const char *wave6_vpu_instance_state_name(enum vpu_instance_state state);
+void wave6_vpu_set_instance_state(struct vpu_instance *inst,
+				  enum vpu_instance_state state);
+u64 wave6_vpu_cycle_to_ns(struct vpu_core_device *core, u64 cycle);
+int wave6_vpu_wait_interrupt(struct vpu_instance *inst, unsigned int timeout);
+int wave6_vpu_dec_register_device(struct vpu_core_device *core);
+void wave6_vpu_dec_unregister_device(struct vpu_core_device *core);
+int wave6_vpu_enc_register_device(struct vpu_core_device *core);
+void wave6_vpu_enc_unregister_device(struct vpu_core_device *core);
+void wave6_vpu_finish_job(struct vpu_instance *inst);
+void wave6_vpu_record_performance_timestamps(struct vpu_instance *inst);
+void wave6_vpu_handle_performance(struct vpu_instance *inst,
+				  struct vpu_buffer *vpu_buf);
+void wave6_vpu_reset_performance(struct vpu_instance *inst);
+int wave6_vpu_init_m2m_dev(struct vpu_core_device *core);
+void wave6_vpu_release_m2m_dev(struct vpu_core_device *core);
+int wave6_vpu_subscribe_event(struct v4l2_fh *fh,
+			      const struct v4l2_event_subscription *sub);
+void wave6_vpu_return_buffers(struct vpu_instance *inst,
+			      unsigned int type, enum vb2_buffer_state state);
+void wave6_vpu_enable_instance(struct vpu_instance *inst);
+void wave6_vpu_disable_instance(struct vpu_instance *inst);
+
+#endif /* __WAVE6_VPU_CORE_H__ */
--

2.31.1

author
#21 lore raw

RE: [PATCH v7 5/9] media: chips-media: wave6: Add Wave6 core driver

Hi, Frank.

Quoted 23 lines

-----Original Message-----
From: Frank Li <[email protected]>
Sent: Friday, September 11, 2026 5:49 AM
To: Nas Chung <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jackson.lee <[email protected]>; lafley.kim <[email protected]>; [email protected]; Ming Qian <[email protected]>
Subject: Re: [PATCH v7 5/9] media: chips-media: wave6: Add Wave6 core driver

Quoted 8 lines · Nas Chung

Add the core driver for the Chips&Media Wave6 video codec IP.

The hardware contains one control register region and four interface register regions for a shared video processing engine. This driver handles the interface register regions, each with its own MMIO range and interrupt, while relying on the control driver for firmware loading and shared resource management.

...

Quoted 39 lines
Quoted 27 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +27 -0
+static int wave6_vpu_core_probe(struct platform_device *pdev)
+{
+	struct vpu_core_device *core;
+	const struct wave6_vpu_core_resource *res;
+	int ret;
+	int irq;
+
+	res = dev_get_platdata(&pdev->dev);
+	if (!res)
+		return -ENODEV;
+
+	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
+	if (ret < 0) {
+		dev_err(&pdev->dev, "failed to set DMA mask: %d\n", ret);
+		return ret;
+	}
+
+	core = devm_kzalloc(&pdev->dev, sizeof(*core), GFP_KERNEL);
+	if (!core)
+		return -ENOMEM;
+
+	ret = devm_mutex_init(&pdev->dev, &core->dev_lock);
+	if (ret)
+		return ret;
+
+	ret = devm_mutex_init(&pdev->dev, &core->hw_lock);
+	if (ret)

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +10 -0
+		return ret;
+
+	spin_lock_init(&core->inst_lock);
+	INIT_LIST_HEAD(&core->instances);
+	dev_set_drvdata(&pdev->dev, core);
+	core->dev = &pdev->dev;
+	core->res = res;
+
+	if (pdev->dev.parent->driver && pdev->dev.parent->driver->name &&
+	    !strcmp(pdev->dev.parent->driver->name,

WAVE6_VPU_PLATFORM_DRIVER_NAME))

Quoted 1 line

why need check this?

I'll remove the condition in v8.

Quoted 56 lines

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +9 -0
+		core->vpu = dev_get_drvdata(pdev->dev.parent);
+
+	core->reg_base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(core->reg_base))
+		return PTR_ERR(core->reg_base);
+
+	ret = devm_clk_bulk_get_all(&pdev->dev, &core->clks);
+	if (ret < 0)
+		return dev_err_probe(&pdev->dev, ret, "failed to get

clocks\n");

Quoted 25 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +25 -0
+
+	core->num_clks = ret;
+
+	ret = kfifo_init(&core->irq_fifo, core->irq_buffer,
+			 sizeof(core->irq_buffer));
+	if (ret)
+		return ret;
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0)
+		return irq;
+
+	ret = devm_request_threaded_irq(&pdev->dev, irq,
+					wave6_vpu_core_irq,
+					wave6_vpu_core_irq_thread,
+					0, "vpu_irq", core);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to request IRQ: %d\n", ret);
+		return ret;
+	}
+
+	ret = v4l2_device_register(&pdev->dev, &core->v4l2_dev);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to register v4l2_dev: %d\n", ret);
+		return ret;

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +10 -0
+	}
+
+	ret = wave6_vpu_init_m2m_dev(core);
+	if (ret)
+		goto err_v4l2_unregister;
+
+	core->temp_vbuf.size = ALIGN(W6_TEMPBUF_SIZE, 4096);
+	ret = wave6_vdi_alloc_dma(core->dev, &core->temp_vbuf);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to allocate temp_vbuf: %d\n",

ret);

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c

Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +8 -0
+		goto err_m2m_dev_release;
+	}
+
+	core->debugfs = debugfs_lookup(WAVE6_VPU_DEBUGFS_DIR, NULL);
+	if (!IS_ERR_OR_NULL(core->debugfs))
+		dput(core->debugfs);
+	else
+		core->debugfs = debugfs_create_dir(WAVE6_VPU_DEBUGFS_DIR,

NULL);

Quoted 4 lines

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 283

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +2 -0
+
+	pm_runtime_enable(&pdev->dev);

devm_pm_runtime_enable()

OK.

Quoted 29 lines
Quoted 29 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +28 -0
+
+	if (core->res->codec_types & WAVE6_IS_DEC) {
+		ret = wave6_vpu_dec_register_device(core);
+		if (ret) {
+			dev_err(&pdev->dev,
+				"failed to register video_dev_dec: %d\n", ret);
+			goto err_temp_vbuf_free;
+		}
+	}
+	if (core->res->codec_types & WAVE6_IS_ENC) {
+		ret = wave6_vpu_enc_register_device(core);
+		if (ret) {
+			dev_err(&pdev->dev,
+				"failed to register video_dev_enc: %d\n", ret);
+			goto err_dec_unreg;
+		}
+	}
+
+	dev_dbg(&pdev->dev, "Added wave6 driver with caps %s %s\n",
+		core->res->codec_types & WAVE6_IS_ENC ? "'ENCODE'" : "",
+		core->res->codec_types & WAVE6_IS_DEC ? "'DECODE'" : "");
+
+	return 0;
+
+err_dec_unreg:
+	if (core->res->codec_types & WAVE6_IS_DEC)
+		wave6_vpu_dec_unregister_device(core);
+err_temp_vbuf_free:
Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 317

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +6 -0
+	pm_runtime_disable(&pdev->dev);
+	wave6_vdi_free_dma(&core->temp_vbuf);
+err_m2m_dev_release:
+	wave6_vpu_release_m2m_dev(core);
+err_v4l2_unregister:
+	v4l2_device_unregister(&core->v4l2_dev);

can you try use devm_add_action_or_reset() to elimiate these error label?

OK, I'll address this in v8.

Quoted 27 lines
Quoted 16 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +15 -0
+
+	return ret;
+}
+
+static void wave6_vpu_core_remove(struct platform_device *pdev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(&pdev->dev);
+
+	wave6_vpu_enc_unregister_device(core);
+	wave6_vpu_dec_unregister_device(core);
+	pm_runtime_disable(&pdev->dev);
+	wave6_vdi_free_dma(&core->temp_vbuf);
+	wave6_vpu_release_m2m_dev(core);
+	v4l2_device_unregister(&core->v4l2_dev);
+}

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +10 -0
+
+static int wave6_vpu_core_runtime_suspend(struct device *dev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(dev);
+
+	if (WARN_ON(!core))
+		return -ENODEV;
+
+	/*
+	 * Only call parent VPU put_vpu if the core has a parent and is

active.

Quoted 9 lines

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 349

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +7 -0
+	 * - core->vpu: prevent access in core without parent VPU.
+	 * - core->active: execute sleep only after m2m streaming is started.
+	 */
+	if (core->vpu && core->active)
+		core->vpu->put_vpu(core->vpu, core);
+
+	if (core->num_clks)

Need check it. clk_bulk_disable_unprepare() is empty ops when num_clks is 0

I'll drop the guard in v8.

Quoted 10 lines
Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +9 -0
+		clk_bulk_disable_unprepare(core->num_clks, core->clks);
+
+	return 0;
+}
+
+static int wave6_vpu_core_runtime_resume(struct device *dev)
+{
+	struct vpu_core_device *core = dev_get_drvdata(dev);
+	bool boot_vpu;
Quoted 8 lines

drivers/media/platform/chips-media/wave6/wave6-vpu-core.c line 364

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-core.c new file +6 -0
+	int ret;
+
+	if (WARN_ON(!core))
+		return -ENODEV;
+
+	if (core->num_clks) {

need checkit. clk_bulk_prepare_enable() is empty ops when num_clks is 0

Same here.

Thanks.
Nas.

Quoted 2 lines

Frank

author
#22 lore raw

PATCH v7 6/9 media: chips-media: wave6: Improve debugging capabilities

In reply to Nas Chung

Add debugfs entries and trace events to provide detailed debugging information.
These enhancements help diagnose issues and improve debugging capabilities for the Wave6 core driver.

---
 .../platform/chips-media/wave6/wave6-trace.h  | 289 ++++++++++++++++++
 .../chips-media/wave6/wave6-vpu-dbg.c         | 183 +++++++++++
 .../chips-media/wave6/wave6-vpu-dbg.h         |  14 +
 3 files changed, 486 insertions(+)
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-trace.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h
drivers/media/platform/chips-media/wave6/wave6-trace.h new file +289 -0
new file mode 100644
index 000000000000..e9589b2f9918
@@ -0,0 +1,289 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
+/*
+ * Wave6 series multi-standard codec IP - wave6 driver tracer
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM wave6
+
+#if !defined(__WAVE6_TRACE_H__) || defined(TRACE_HEADER_MULTI_READ)
+#define __WAVE6_TRACE_H__
+
+#include <linux/tracepoint.h>
+#include <media/videobuf2-v4l2.h>
+
+DECLARE_EVENT_CLASS(wave6_vpu_register_access,
+		    TP_PROTO(struct device *dev, u32 addr, u32 value),
+		    TP_ARGS(dev, addr, value),
+		    TP_STRUCT__entry(__string(name, dev_name(dev))
+				     __field(u32, addr)
+				     __field(u32, value)),
+		    TP_fast_assign(__assign_str(name);
+				   __entry->addr = addr;
+				   __entry->value = value;),
+		    TP_printk("%s:0x%03x 0x%08x",
+			      __get_str(name), __entry->addr, __entry->value));
+
+DEFINE_EVENT(wave6_vpu_register_access, wave6_vpu_writel,
+	     TP_PROTO(struct device *dev, u32 addr, u32 value),
+	     TP_ARGS(dev, addr, value));
+DEFINE_EVENT(wave6_vpu_register_access, wave6_vpu_readl,
+	     TP_PROTO(struct device *dev, u32 addr, u32 value),
+	     TP_ARGS(dev, addr, value));
+
+TRACE_EVENT(wave6_vpu_send_command,
+	    TP_PROTO(struct vpu_core_device *core, u32 id, u32 std, u32 cmd),
+	    TP_ARGS(core, id, std, cmd),
+	    TP_STRUCT__entry(__string(name, dev_name(core->dev))
+			     __field(u32, id)
+			     __field(u32, std)
+			     __field(u32, cmd)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = id;
+			   __entry->std = std;
+			   __entry->cmd = cmd;),
+	    TP_printk("%s: inst id %d, std 0x%x, cmd 0x%x",
+		      __get_str(name), __entry->id,
+		      __entry->std, __entry->cmd));
+
+TRACE_EVENT(wave6_vpu_irq,
+	    TP_PROTO(struct vpu_core_device *core, u32 irq, u32 idc),
+	    TP_ARGS(core, irq, idc),
+	    TP_STRUCT__entry(__string(name, dev_name(core->dev))
+			     __field(u32, irq)
+			     __field(u32, idc)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->irq = irq;
+			   __entry->idc = idc;),
+	    TP_printk("%s: irq 0x%x, idc 0x%x",
+		      __get_str(name), __entry->irq, __entry->idc));
+
+TRACE_EVENT(wave6_vpu_set_state,
+	    TP_PROTO(struct vpu_instance *inst, u32 state),
+	    TP_ARGS(inst, state),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __string(cur_state, wave6_vpu_instance_state_name(inst->state))
+			     __string(nxt_state, wave6_vpu_instance_state_name(state))),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __assign_str(cur_state);
+			   __assign_str(nxt_state);),
+	    TP_printk("%s: inst[%d] set state %s -> %s",
+		      __get_str(name), __entry->id,
+		      __get_str(cur_state), __get_str(nxt_state)));
+
+DECLARE_EVENT_CLASS(wave6_vpu_inst_internal,
+		    TP_PROTO(struct vpu_instance *inst, bool is_out),
+		    TP_ARGS(inst, is_out),
+		    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+				     __field(u32, id)
+				     __string(type, is_out ? "output" : "capture")
+				     __field(u32, pixelformat)
+				     __field(u32, width)
+				     __field(u32, height)
+				     __field(u32, buf_cnt_src)
+				     __field(u32, buf_cnt_dst)
+				     __field(u32, processed_cnt)
+				     __field(u32, error_cnt)),
+		    TP_fast_assign(__assign_str(name);
+				   __entry->id = inst->id;
+				   __assign_str(type);
+				   __entry->pixelformat = is_out ? inst->src_fmt.pixelformat :
+								   inst->dst_fmt.pixelformat;
+				   __entry->width = is_out ? inst->src_fmt.width :
+							     inst->dst_fmt.width;
+				   __entry->height = is_out ? inst->src_fmt.height :
+							      inst->dst_fmt.height;
+				   __entry->buf_cnt_src = inst->queued_src_buf_num;
+				   __entry->buf_cnt_dst = inst->queued_dst_buf_num;
+				   __entry->processed_cnt = inst->processed_buf_num;
+				   __entry->error_cnt = inst->error_buf_num;),
+		    TP_printk("%s: inst[%d] %s %c%c%c%c %dx%d, input %d, %d, process %d, error %d",
+			      __get_str(name), __entry->id, __get_str(type),
+			      __entry->pixelformat,
+			      __entry->pixelformat >> 8,
+			      __entry->pixelformat >> 16,
+			      __entry->pixelformat >> 24,
+			      __entry->width, __entry->height,
+			      __entry->buf_cnt_src, __entry->buf_cnt_dst,
+			      __entry->processed_cnt, __entry->error_cnt));
+
+DEFINE_EVENT(wave6_vpu_inst_internal, wave6_vpu_start_streaming,
+	     TP_PROTO(struct vpu_instance *inst, bool is_out),
+	     TP_ARGS(inst, is_out));
+
+DEFINE_EVENT(wave6_vpu_inst_internal, wave6_vpu_stop_streaming,
+	     TP_PROTO(struct vpu_instance *inst, bool is_out),
+	     TP_ARGS(inst, is_out));
+
+TRACE_EVENT(wave6_vpu_dec_pic,
+	    TP_PROTO(struct vpu_instance *inst, u32 srcidx, u32 size),
+	    TP_ARGS(inst, srcidx, size),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __field(u32, srcidx)
+			     __field(u32, start)
+			     __field(u32, size)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __entry->srcidx = srcidx;
+			   __entry->start = inst->codec_info->dec_info.stream_rd_ptr;
+			   __entry->size = size;),
+	    TP_printk("%s: inst[%d] src[%2d] %8x, %d",
+		      __get_str(name), __entry->id,
+		      __entry->srcidx, __entry->start, __entry->size));
+
+TRACE_EVENT(wave6_vpu_source_change,
+	    TP_PROTO(struct vpu_instance *inst, struct dec_seq_info *info),
+	    TP_ARGS(inst, info),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __field(u32, width)
+			     __field(u32, height)
+			     __field(u32, profile)
+			     __field(u32, level)
+			     __field(u32, tier)
+			     __field(u32, min_fb_cnt)
+			     __field(u32, disp_delay)
+			     __field(u32, quantization)
+			     __field(u32, colorspace)
+			     __field(u32, xfer_func)
+			     __field(u32, ycbcr_enc)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __entry->width = info->pic_width;
+			   __entry->height = info->pic_height;
+			   __entry->profile = info->profile;
+			   __entry->level = info->level;
+			   __entry->tier = info->tier;
+			   __entry->min_fb_cnt = info->min_frame_buffer_count;
+			   __entry->disp_delay = info->frame_buf_delay;
+			   __entry->quantization = inst->quantization;
+			   __entry->colorspace = inst->colorspace;
+			   __entry->xfer_func = inst->xfer_func;
+			   __entry->ycbcr_enc = inst->ycbcr_enc;),
+	    TP_printk("%s: inst[%d] %dx%d profile %d %d %d min_fb %d delay %d color %d %d %d %d",
+		      __get_str(name), __entry->id,
+		      __entry->width, __entry->height,
+		      __entry->profile, __entry->level, __entry->tier,
+		      __entry->min_fb_cnt, __entry->disp_delay,
+		      __entry->quantization, __entry->colorspace,
+		      __entry->xfer_func, __entry->ycbcr_enc));
+
+TRACE_EVENT(wave6_vpu_dec_done,
+	    TP_PROTO(struct vpu_instance *inst, struct dec_output_info *info),
+	    TP_ARGS(inst, info),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __field(u32, dec_flag)
+			     __field(u32, dec_poc)
+			     __field(u32, disp_flag)
+			     __field(u32, disp_cnt)
+			     __field(u32, rel_cnt)
+			     __field(u32, src_ch)
+			     __field(u32, eos)
+			     __field(u32, error)
+			     __field(u32, warn)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __entry->dec_flag = info->frame_decoded;
+			   __entry->dec_poc = info->decoded_poc;
+			   __entry->disp_flag = info->frame_display;
+			   __entry->disp_cnt = info->disp_frame_num;
+			   __entry->rel_cnt = info->release_disp_frame_num;
+			   __entry->src_ch = info->notification_flags & DEC_NOTI_FLAG_SEQ_CHANGE;
+			   __entry->eos = info->stream_end;
+			   __entry->error = info->error_reason;
+			   __entry->warn = info->warn_info;),
+	    TP_printk("%s: inst[%d] dec %d %d disp %d(%d) rel %d src_ch %d eos %d error 0x%x 0x%x",
+		      __get_str(name), __entry->id,
+		      __entry->dec_flag, __entry->dec_poc,
+		      __entry->disp_flag, __entry->disp_cnt,
+		      __entry->rel_cnt,
+		      __entry->src_ch, __entry->eos,
+		      __entry->error, __entry->warn));
+
+TRACE_EVENT(wave6_vpu_enc_pic,
+	    TP_PROTO(struct vpu_instance *inst, struct enc_param *param),
+	    TP_ARGS(inst, param),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __field(u32, srcidx)
+			     __field(u64, buf_y)
+			     __field(u64, buf_cb)
+			     __field(u64, buf_cr)
+			     __field(u32, stride)
+			     __field(u64, buf_strm)
+			     __field(u32, size_strm)
+			     __field(u32, force_type_enable)
+			     __field(u32, force_type)
+			     __field(u32, end_flag)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __entry->srcidx = param->src_idx;
+			   __entry->buf_y = param->source_frame->buf_y;
+			   __entry->buf_cb = param->source_frame->buf_cb;
+			   __entry->buf_cr = param->source_frame->buf_cr;
+			   __entry->stride = param->source_frame->stride;
+			   __entry->buf_strm = param->pic_stream_buffer_addr;
+			   __entry->size_strm = param->pic_stream_buffer_size;
+			   __entry->force_type_enable = param->force_pic;
+			   __entry->force_type = param->force_pic_type;
+			   __entry->end_flag = param->src_end;),
+	    TP_printk("%s: inst[%d] src[%2d] %8llx %8llx %8llx(%d) dst %8llx(%d) force type %d(%d) end %d",
+		      __get_str(name), __entry->id, __entry->srcidx,
+		      __entry->buf_y, __entry->buf_cb, __entry->buf_cr,
+		      __entry->stride, __entry->buf_strm, __entry->size_strm,
+		      __entry->force_type_enable, __entry->force_type,
+		      __entry->end_flag));
+
+TRACE_EVENT(wave6_vpu_enc_done,
+	    TP_PROTO(struct vpu_instance *inst, struct enc_output_info *info),
+	    TP_ARGS(inst, info),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __field(u32, srcidx)
+			     __field(u32, frmidx)
+			     __field(u32, size)
+			     __field(u32, type)
+			     __field(u32, avg_qp)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __entry->srcidx = info->enc_src_idx;
+			   __entry->frmidx = info->recon_frame_index;
+			   __entry->size = info->bitstream_size;
+			   __entry->type = info->pic_type;
+			   __entry->avg_qp = info->avg_ctu_qp;),
+	    TP_printk("%s: inst[%d] src %d, frame %d, size %d, type %d, qp %d, eos %d",
+		      __get_str(name), __entry->id,
+		      __entry->srcidx, __entry->frmidx,
+		      __entry->size, __entry->type, __entry->avg_qp,
+		      __entry->frmidx == RECON_IDX_FLAG_ENC_END));
+
+TRACE_EVENT(wave6_vpu_s_ctrl,
+	    TP_PROTO(struct vpu_instance *inst, struct v4l2_ctrl *ctrl),
+	    TP_ARGS(inst, ctrl),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __string(ctrl_name, ctrl->name)
+			     __field(u32, val)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __assign_str(ctrl_name);
+			   __entry->val = ctrl->val;),
+	    TP_printk("%s: inst[%d] %s = %d",
+		      __get_str(name), __entry->id,
+		      __get_str(ctrl_name), __entry->val));
+
+#endif /* __WAVE6_TRACE_H__ */
+
+#undef TRACE_INCLUDE_PATH
+#define TRACE_INCLUDE_PATH .
+#undef TRACE_INCLUDE_FILE
+#define TRACE_INCLUDE_FILE wave6-trace
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c new file +183 -0

183 lines not on this page · raw message

drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h new file +14 -0
new file mode 100644
index 000000000000..6453eb2de76f
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
+/*
+ * Wave6 series multi-standard codec IP - debug interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#ifndef __WAVE6_VPU_DBG_H__
+#define __WAVE6_VPU_DBG_H__
+
+int wave6_vpu_create_dbgfs_file(struct vpu_instance *inst);
+void wave6_vpu_remove_dbgfs_file(struct vpu_instance *inst);
+
+#endif /* __WAVE6_VPU_DBG_H__ */
Signature · 1 line
2.31.1
AI review Tested-by Ming Qian Tested-by Marek Vasut on v7 6/9
#23 lore raw

Re: [PATCH v7 6/9] media: chips-media: wave6: Improve debugging capabilities

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

New issues:
- [High] Lockless call to vb2_get_num_buffers() in debugfs read leads to use-after-free on q->bufs_bitmap.
- [Medium] Multiplication can overflow 32-bit bounds before assignment to a 64-bit variable in the debugfs FPS calculation.
- [Low] Direct use of 64-bit division and modulo operators (/ and %) will cause build failures on 32-bit architectures.

Pre-existing issues:
- [High] Unprotected iteration over V4L2 mem2mem ready queues without holding rdy_spinlock leads to list corruption. --

commit a789f4b587c983369a5ba74d2b58395c60628047
Author: Nas Chung <[email protected]>

media: chips-media: wave6: Improve debugging capabilities

This commit adds debugfs entries and trace events to provide detailed debugging information for the Wave6 core driver.

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c new file +0 -0
new file mode 100644
index 0000000000000..ae71edeae0888

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c line 43

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c new file +4 -0
+	vq = v4l2_m2m_get_src_vq(inst->v4l2_fh.m2m_ctx);
+	seq_printf(s, "output (%2d, %2d): fmt = %c%c%c%c %d x %d, %d;\n",
+		   vb2_is_streaming(vq),
+		   vb2_get_num_buffers(vq),

[Severity: High]
Does this locklessly access q->bufs_bitmap? Concurrently, an application can execute VIDIOC_REQBUFS with count=0, which acquires the queue lock, frees q->bufs_bitmap, and sets the pointer to NULL. Because this read doesn't hold m2m_ctx->q_lock or equivalent synchronization, it could read a non-NULL pointer right before it is freed by REQBUFS, executing bitmap_weight() on a freed pointer and causing a use-after-free.

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c new file +1 -0
+		   inst->src_fmt.pixelformat,

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c line 55

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c new file +4 -0
+	vq = v4l2_m2m_get_dst_vq(inst->v4l2_fh.m2m_ctx);
+	seq_printf(s, "capture(%2d, %2d): fmt = %c%c%c%c %d x %d, %d;\n",
+		   vb2_is_streaming(vq),
+		   vb2_get_num_buffers(vq),

[Severity: High]
This appears to have the same lockless access concern as the source queue above. Does this need queue lock protection to prevent a use-after-free if VIDIOC_REQBUFS is called concurrently?

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c new file +1 -0
+		   inst->dst_fmt.pixelformat,

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c line 84

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c new file +2 -0
+	seq_puts(s, "fps");
+	tmp = MSEC_PER_SEC * inst->processed_buf_num;

[Severity: Medium]
Will this operation overflow on 32-bit architectures? MSEC_PER_SEC is typically defined as 1000L (a 32-bit signed integer on 32-bit systems) and inst->processed_buf_num is a u32. If the buffer count exceeds ~4.29 million, the 32-bit multiplication might overflow before it is assigned and sign-extended into the 64-bit tmp variable.

drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c line 86

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c new file +2 -0
+	if (perf->ts_last > perf->ts_first + NSEC_PER_MSEC) {
+		fps = DIV_ROUND_CLOSEST(tmp, (perf->ts_last - perf->ts_first) / NSEC_PER_MSEC);

[Severity: Low]
Does this cause a build failure on 32-bit architectures? DIV_ROUND_CLOSEST internally expands to a division operation (/), and the denominator here also uses direct division. Using direct 64-bit division on s64 variables emits calls to undefined functions like __divdi3 on 32-bit systems. Should explicit 64-bit math helpers like div64_s64() be used here instead?

drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c line 89

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c new file +3 -0
+		seq_printf(s, " actual: %lld;", fps);
+	}
+	duration = perf->total_sw_time / NSEC_PER_MSEC;

[Severity: Low]
Similar to the above, this performs direct 64-bit division on a u64 variable, which can break 32-bit builds due to missing math symbols.

drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c line 94

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c new file +5 -0
+	if (duration > 0) {
+		fps = DIV_ROUND_CLOSEST(tmp, duration);
+		seq_printf(s, " sw: %lld;", fps);
+	}
+	duration = perf->total_hw_time / NSEC_PER_MSEC;

[Severity: Low]
Another instance of direct 64-bit division that might break 32-bit builds.

drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c line 103

Quoted 9 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c new file +9 -0
+	if (duration > 0) {
+		fps = DIV_ROUND_CLOSEST(tmp, duration);
+		seq_printf(s, " hw: %lld", fps);
+	}
+	seq_putc(s, '\n');
+
+	seq_printf(s, "latency(ms) first: %llu.%06llu, max %llu.%06llu, setup %llu.%06llu\n",
+		   perf->latency_first / NSEC_PER_MSEC,
+		   perf->latency_first % NSEC_PER_MSEC,

[Severity: Low]
This performs direct 64-bit division and modulo on an s64 variable, which is known to cause linking errors (like __moddi3 and __moddi3) on 32-bit architectures. Should this use explicit math helpers like div64_s64_rem()?

[Severity: High]
This is a pre-existing issue, but the series iterates over the V4L2 m2m ready queues locklessly using v4l2_m2m_for_each_src_buf and v4l2_m2m_for_each_dst_buf without holding rdy_spinlock.

For example, in drivers/media/platform/chips-media/wave6/wave6-vpu-enc.c:

wave6_get_valid_src_buf() {
    ...
    v4l2_m2m_for_each_src_buf(inst->v4l2_fh.m2m_ctx, v4l2_m2m_buf) {
    ...
}

And in drivers/media/platform/chips-media/wave6/wave6-vpu-v4l2.c:

wave6_get_dst_buf_by_addr() {
    ...
    v4l2_m2m_for_each_dst_buf(inst->v4l2_fh.m2m_ctx, v4l2_m2m_buf) {
    ...
}

Are these list traversals vulnerable to data races? These macros wrap list_for_each_entry, and they appear to be called from the m2m framework's worker thread without holding rdy_spinlock. Concurrently, userspace can call VIDIOC_QBUF, which dynamically adds elements to the rdy_queue while holding the lock. Modifying a linked list while a separate thread traverses it unprotected can result in structural data races and list pointer corruption, potentially leading to a use-after-free or kernel panic.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#24 lore raw

Re: [PATCH v7 6/9] media: chips-media: wave6: Improve debugging capabilities

In reply to Nas Chung

On Fri, Sep 04, 2026 at 03:46:32PM +0900, Nas Chung wrote:

media: chips-media: wave6: Add trace and debugfs

Quoted 2 lines

Add debugfs entries and trace events to provide detailed debugging information.

Nit: need extra space line here.

Quoted 1 line

These enhancements help diagnose issues and improve debugging

Remove "These enhancements".

Quoted 506 lines

capabilities for the Wave6 core driver.

---
 .../platform/chips-media/wave6/wave6-trace.h  | 289 ++++++++++++++++++
 .../chips-media/wave6/wave6-vpu-dbg.c         | 183 +++++++++++
 .../chips-media/wave6/wave6-vpu-dbg.h         |  14 +
 3 files changed, 486 insertions(+)
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-trace.h
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h
drivers/media/platform/chips-media/wave6/wave6-trace.h new file +289 -0
new file mode 100644
index 000000000000..e9589b2f9918
@@ -0,0 +1,289 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
+/*
+ * Wave6 series multi-standard codec IP - wave6 driver tracer
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM wave6
+
+#if !defined(__WAVE6_TRACE_H__) || defined(TRACE_HEADER_MULTI_READ)
+#define __WAVE6_TRACE_H__
+
+#include <linux/tracepoint.h>
+#include <media/videobuf2-v4l2.h>
+
+DECLARE_EVENT_CLASS(wave6_vpu_register_access,
+		    TP_PROTO(struct device *dev, u32 addr, u32 value),
+		    TP_ARGS(dev, addr, value),
+		    TP_STRUCT__entry(__string(name, dev_name(dev))
+				     __field(u32, addr)
+				     __field(u32, value)),
+		    TP_fast_assign(__assign_str(name);
+				   __entry->addr = addr;
+				   __entry->value = value;),
+		    TP_printk("%s:0x%03x 0x%08x",
+			      __get_str(name), __entry->addr, __entry->value));
+
+DEFINE_EVENT(wave6_vpu_register_access, wave6_vpu_writel,
+	     TP_PROTO(struct device *dev, u32 addr, u32 value),
+	     TP_ARGS(dev, addr, value));
+DEFINE_EVENT(wave6_vpu_register_access, wave6_vpu_readl,
+	     TP_PROTO(struct device *dev, u32 addr, u32 value),
+	     TP_ARGS(dev, addr, value));
+
+TRACE_EVENT(wave6_vpu_send_command,
+	    TP_PROTO(struct vpu_core_device *core, u32 id, u32 std, u32 cmd),
+	    TP_ARGS(core, id, std, cmd),
+	    TP_STRUCT__entry(__string(name, dev_name(core->dev))
+			     __field(u32, id)
+			     __field(u32, std)
+			     __field(u32, cmd)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = id;
+			   __entry->std = std;
+			   __entry->cmd = cmd;),
+	    TP_printk("%s: inst id %d, std 0x%x, cmd 0x%x",
+		      __get_str(name), __entry->id,
+		      __entry->std, __entry->cmd));
+
+TRACE_EVENT(wave6_vpu_irq,
+	    TP_PROTO(struct vpu_core_device *core, u32 irq, u32 idc),
+	    TP_ARGS(core, irq, idc),
+	    TP_STRUCT__entry(__string(name, dev_name(core->dev))
+			     __field(u32, irq)
+			     __field(u32, idc)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->irq = irq;
+			   __entry->idc = idc;),
+	    TP_printk("%s: irq 0x%x, idc 0x%x",
+		      __get_str(name), __entry->irq, __entry->idc));
+
+TRACE_EVENT(wave6_vpu_set_state,
+	    TP_PROTO(struct vpu_instance *inst, u32 state),
+	    TP_ARGS(inst, state),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __string(cur_state, wave6_vpu_instance_state_name(inst->state))
+			     __string(nxt_state, wave6_vpu_instance_state_name(state))),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __assign_str(cur_state);
+			   __assign_str(nxt_state);),
+	    TP_printk("%s: inst[%d] set state %s -> %s",
+		      __get_str(name), __entry->id,
+		      __get_str(cur_state), __get_str(nxt_state)));
+
+DECLARE_EVENT_CLASS(wave6_vpu_inst_internal,
+		    TP_PROTO(struct vpu_instance *inst, bool is_out),
+		    TP_ARGS(inst, is_out),
+		    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+				     __field(u32, id)
+				     __string(type, is_out ? "output" : "capture")
+				     __field(u32, pixelformat)
+				     __field(u32, width)
+				     __field(u32, height)
+				     __field(u32, buf_cnt_src)
+				     __field(u32, buf_cnt_dst)
+				     __field(u32, processed_cnt)
+				     __field(u32, error_cnt)),
+		    TP_fast_assign(__assign_str(name);
+				   __entry->id = inst->id;
+				   __assign_str(type);
+				   __entry->pixelformat = is_out ? inst->src_fmt.pixelformat :
+								   inst->dst_fmt.pixelformat;
+				   __entry->width = is_out ? inst->src_fmt.width :
+							     inst->dst_fmt.width;
+				   __entry->height = is_out ? inst->src_fmt.height :
+							      inst->dst_fmt.height;
+				   __entry->buf_cnt_src = inst->queued_src_buf_num;
+				   __entry->buf_cnt_dst = inst->queued_dst_buf_num;
+				   __entry->processed_cnt = inst->processed_buf_num;
+				   __entry->error_cnt = inst->error_buf_num;),
+		    TP_printk("%s: inst[%d] %s %c%c%c%c %dx%d, input %d, %d, process %d, error %d",
+			      __get_str(name), __entry->id, __get_str(type),
+			      __entry->pixelformat,
+			      __entry->pixelformat >> 8,
+			      __entry->pixelformat >> 16,
+			      __entry->pixelformat >> 24,
+			      __entry->width, __entry->height,
+			      __entry->buf_cnt_src, __entry->buf_cnt_dst,
+			      __entry->processed_cnt, __entry->error_cnt));
+
+DEFINE_EVENT(wave6_vpu_inst_internal, wave6_vpu_start_streaming,
+	     TP_PROTO(struct vpu_instance *inst, bool is_out),
+	     TP_ARGS(inst, is_out));
+
+DEFINE_EVENT(wave6_vpu_inst_internal, wave6_vpu_stop_streaming,
+	     TP_PROTO(struct vpu_instance *inst, bool is_out),
+	     TP_ARGS(inst, is_out));
+
+TRACE_EVENT(wave6_vpu_dec_pic,
+	    TP_PROTO(struct vpu_instance *inst, u32 srcidx, u32 size),
+	    TP_ARGS(inst, srcidx, size),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __field(u32, srcidx)
+			     __field(u32, start)
+			     __field(u32, size)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __entry->srcidx = srcidx;
+			   __entry->start = inst->codec_info->dec_info.stream_rd_ptr;
+			   __entry->size = size;),
+	    TP_printk("%s: inst[%d] src[%2d] %8x, %d",
+		      __get_str(name), __entry->id,
+		      __entry->srcidx, __entry->start, __entry->size));
+
+TRACE_EVENT(wave6_vpu_source_change,
+	    TP_PROTO(struct vpu_instance *inst, struct dec_seq_info *info),
+	    TP_ARGS(inst, info),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __field(u32, width)
+			     __field(u32, height)
+			     __field(u32, profile)
+			     __field(u32, level)
+			     __field(u32, tier)
+			     __field(u32, min_fb_cnt)
+			     __field(u32, disp_delay)
+			     __field(u32, quantization)
+			     __field(u32, colorspace)
+			     __field(u32, xfer_func)
+			     __field(u32, ycbcr_enc)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __entry->width = info->pic_width;
+			   __entry->height = info->pic_height;
+			   __entry->profile = info->profile;
+			   __entry->level = info->level;
+			   __entry->tier = info->tier;
+			   __entry->min_fb_cnt = info->min_frame_buffer_count;
+			   __entry->disp_delay = info->frame_buf_delay;
+			   __entry->quantization = inst->quantization;
+			   __entry->colorspace = inst->colorspace;
+			   __entry->xfer_func = inst->xfer_func;
+			   __entry->ycbcr_enc = inst->ycbcr_enc;),
+	    TP_printk("%s: inst[%d] %dx%d profile %d %d %d min_fb %d delay %d color %d %d %d %d",
+		      __get_str(name), __entry->id,
+		      __entry->width, __entry->height,
+		      __entry->profile, __entry->level, __entry->tier,
+		      __entry->min_fb_cnt, __entry->disp_delay,
+		      __entry->quantization, __entry->colorspace,
+		      __entry->xfer_func, __entry->ycbcr_enc));
+
+TRACE_EVENT(wave6_vpu_dec_done,
+	    TP_PROTO(struct vpu_instance *inst, struct dec_output_info *info),
+	    TP_ARGS(inst, info),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __field(u32, dec_flag)
+			     __field(u32, dec_poc)
+			     __field(u32, disp_flag)
+			     __field(u32, disp_cnt)
+			     __field(u32, rel_cnt)
+			     __field(u32, src_ch)
+			     __field(u32, eos)
+			     __field(u32, error)
+			     __field(u32, warn)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __entry->dec_flag = info->frame_decoded;
+			   __entry->dec_poc = info->decoded_poc;
+			   __entry->disp_flag = info->frame_display;
+			   __entry->disp_cnt = info->disp_frame_num;
+			   __entry->rel_cnt = info->release_disp_frame_num;
+			   __entry->src_ch = info->notification_flags & DEC_NOTI_FLAG_SEQ_CHANGE;
+			   __entry->eos = info->stream_end;
+			   __entry->error = info->error_reason;
+			   __entry->warn = info->warn_info;),
+	    TP_printk("%s: inst[%d] dec %d %d disp %d(%d) rel %d src_ch %d eos %d error 0x%x 0x%x",
+		      __get_str(name), __entry->id,
+		      __entry->dec_flag, __entry->dec_poc,
+		      __entry->disp_flag, __entry->disp_cnt,
+		      __entry->rel_cnt,
+		      __entry->src_ch, __entry->eos,
+		      __entry->error, __entry->warn));
+
+TRACE_EVENT(wave6_vpu_enc_pic,
+	    TP_PROTO(struct vpu_instance *inst, struct enc_param *param),
+	    TP_ARGS(inst, param),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __field(u32, srcidx)
+			     __field(u64, buf_y)
+			     __field(u64, buf_cb)
+			     __field(u64, buf_cr)
+			     __field(u32, stride)
+			     __field(u64, buf_strm)
+			     __field(u32, size_strm)
+			     __field(u32, force_type_enable)
+			     __field(u32, force_type)
+			     __field(u32, end_flag)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __entry->srcidx = param->src_idx;
+			   __entry->buf_y = param->source_frame->buf_y;
+			   __entry->buf_cb = param->source_frame->buf_cb;
+			   __entry->buf_cr = param->source_frame->buf_cr;
+			   __entry->stride = param->source_frame->stride;
+			   __entry->buf_strm = param->pic_stream_buffer_addr;
+			   __entry->size_strm = param->pic_stream_buffer_size;
+			   __entry->force_type_enable = param->force_pic;
+			   __entry->force_type = param->force_pic_type;
+			   __entry->end_flag = param->src_end;),
+	    TP_printk("%s: inst[%d] src[%2d] %8llx %8llx %8llx(%d) dst %8llx(%d) force type %d(%d) end %d",
+		      __get_str(name), __entry->id, __entry->srcidx,
+		      __entry->buf_y, __entry->buf_cb, __entry->buf_cr,
+		      __entry->stride, __entry->buf_strm, __entry->size_strm,
+		      __entry->force_type_enable, __entry->force_type,
+		      __entry->end_flag));
+
+TRACE_EVENT(wave6_vpu_enc_done,
+	    TP_PROTO(struct vpu_instance *inst, struct enc_output_info *info),
+	    TP_ARGS(inst, info),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __field(u32, srcidx)
+			     __field(u32, frmidx)
+			     __field(u32, size)
+			     __field(u32, type)
+			     __field(u32, avg_qp)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __entry->srcidx = info->enc_src_idx;
+			   __entry->frmidx = info->recon_frame_index;
+			   __entry->size = info->bitstream_size;
+			   __entry->type = info->pic_type;
+			   __entry->avg_qp = info->avg_ctu_qp;),
+	    TP_printk("%s: inst[%d] src %d, frame %d, size %d, type %d, qp %d, eos %d",
+		      __get_str(name), __entry->id,
+		      __entry->srcidx, __entry->frmidx,
+		      __entry->size, __entry->type, __entry->avg_qp,
+		      __entry->frmidx == RECON_IDX_FLAG_ENC_END));
+
+TRACE_EVENT(wave6_vpu_s_ctrl,
+	    TP_PROTO(struct vpu_instance *inst, struct v4l2_ctrl *ctrl),
+	    TP_ARGS(inst, ctrl),
+	    TP_STRUCT__entry(__string(name, dev_name(inst->dev->dev))
+			     __field(u32, id)
+			     __string(ctrl_name, ctrl->name)
+			     __field(u32, val)),
+	    TP_fast_assign(__assign_str(name);
+			   __entry->id = inst->id;
+			   __assign_str(ctrl_name);
+			   __entry->val = ctrl->val;),
+	    TP_printk("%s: inst[%d] %s = %d",
+		      __get_str(name), __entry->id,
+		      __get_str(ctrl_name), __entry->val));
+
+#endif /* __WAVE6_TRACE_H__ */
+
+#undef TRACE_INCLUDE_PATH
+#define TRACE_INCLUDE_PATH .
+#undef TRACE_INCLUDE_FILE
+#define TRACE_INCLUDE_FILE wave6-trace
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.c new file +183 -0
new file mode 100644
index 000000000000..ae71edeae088
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+/*
+ * Wave6 series multi-standard codec IP - debug interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#include <linux/types.h>
+#include <linux/debugfs.h>
+#include "wave6-vpu-core.h"
+#include "wave6-vpu-dbg.h"
+
+static int wave6_vpu_dbg_instance(struct seq_file *s, void *data)
+{
+	struct vpu_instance *inst = s->private;
+	struct vpu_performance_info *perf = &inst->performance;
+	struct vb2_queue *vq;
+	s64 tmp;
+	s64 fps;
+	s64 duration;
+
+	if (!inst->v4l2_fh.m2m_ctx)
+		return 0;
+
+	seq_printf(s, "[%s]\n",
+		   inst->type == VPU_INST_TYPE_DEC ? "Decoder" : "Encoder");
+
+	seq_printf(s, "%s : product 0x%x, fw_ver %d.%d.%d(r%d), hw_ver 0x%x\n",
+		   dev_name(inst->dev->dev),
+		   inst->dev->attr.product_code,
+		   FW_VERSION_MAJOR(inst->dev->attr.fw_version),
+		   FW_VERSION_MINOR(inst->dev->attr.fw_version),
+		   FW_VERSION_REL(inst->dev->attr.fw_version),
+		   inst->dev->attr.fw_revision,
+		   inst->dev->attr.hw_version);
+
+	seq_printf(s, "state = %s\n",
+		   wave6_vpu_instance_state_name(inst->state));
+
+	vq = v4l2_m2m_get_src_vq(inst->v4l2_fh.m2m_ctx);
+	seq_printf(s, "output (%2d, %2d): fmt = %c%c%c%c %d x %d, %d;\n",
+		   vb2_is_streaming(vq),
+		   vb2_get_num_buffers(vq),
+		   inst->src_fmt.pixelformat,
+		   inst->src_fmt.pixelformat >> 8,
+		   inst->src_fmt.pixelformat >> 16,
+		   inst->src_fmt.pixelformat >> 24,
+		   inst->src_fmt.width,
+		   inst->src_fmt.height,
+		   vq->last_buffer_dequeued);
+
+	vq = v4l2_m2m_get_dst_vq(inst->v4l2_fh.m2m_ctx);
+	seq_printf(s, "capture(%2d, %2d): fmt = %c%c%c%c %d x %d, %d;\n",
+		   vb2_is_streaming(vq),
+		   vb2_get_num_buffers(vq),
+		   inst->dst_fmt.pixelformat,
+		   inst->dst_fmt.pixelformat >> 8,
+		   inst->dst_fmt.pixelformat >> 16,
+		   inst->dst_fmt.pixelformat >> 24,
+		   inst->dst_fmt.width,
+		   inst->dst_fmt.height,
+		   vq->last_buffer_dequeued);
+
+	seq_printf(s, "crop: (%d, %d) %d x %d\n",
+		   inst->crop.left,
+		   inst->crop.top,
+		   inst->crop.width,
+		   inst->crop.height);
+
+	if (inst->scaler_info.enable)
+		seq_printf(s, "scale: %d x %d\n",
+			   inst->scaler_info.width, inst->scaler_info.height);
+
+	seq_printf(s, "queued src %d, dst %d, process %d, sequence %d, error %d, drain %d:%d\n",
+		   inst->queued_src_buf_num,
+		   inst->queued_dst_buf_num,
+		   inst->processed_buf_num,
+		   inst->sequence,
+		   inst->error_buf_num,
+		   inst->v4l2_fh.m2m_ctx->out_q_ctx.buffered,
+		   inst->eos);
+
+	seq_puts(s, "fps");
+	tmp = MSEC_PER_SEC * inst->processed_buf_num;
+	if (perf->ts_last > perf->ts_first + NSEC_PER_MSEC) {
+		fps = DIV_ROUND_CLOSEST(tmp, (perf->ts_last - perf->ts_first) / NSEC_PER_MSEC);
+		seq_printf(s, " actual: %lld;", fps);
+	}
+	duration = perf->total_sw_time / NSEC_PER_MSEC;
+	if (duration > 0) {
+		fps = DIV_ROUND_CLOSEST(tmp, duration);
+		seq_printf(s, " sw: %lld;", fps);
+	}
+	duration = perf->total_hw_time / NSEC_PER_MSEC;
+	if (duration > 0) {
+		fps = DIV_ROUND_CLOSEST(tmp, duration);
+		seq_printf(s, " hw: %lld", fps);
+	}
+	seq_putc(s, '\n');
+
+	seq_printf(s, "latency(ms) first: %llu.%06llu, max %llu.%06llu, setup %llu.%06llu\n",
+		   perf->latency_first / NSEC_PER_MSEC,
+		   perf->latency_first % NSEC_PER_MSEC,
+		   perf->latency_max / NSEC_PER_MSEC,
+		   perf->latency_max % NSEC_PER_MSEC,
+		   (perf->ts_first - perf->ts_start) / NSEC_PER_MSEC,
+		   (perf->ts_first - perf->ts_start) % NSEC_PER_MSEC);
+
+	seq_printf(s, "process frame time(ms) min: %llu.%06llu, max %llu.%06llu\n",
+		   perf->min_process_time / NSEC_PER_MSEC,
+		   perf->min_process_time % NSEC_PER_MSEC,
+		   perf->max_process_time / NSEC_PER_MSEC,
+		   perf->max_process_time % NSEC_PER_MSEC);
+
+	if (inst->type == VPU_INST_TYPE_DEC) {
+		seq_printf(s, "%s order\n",
+			   inst->disp_mode == DISP_MODE_DISP_ORDER ? "display" : "decode");
+	} else {
+		struct enc_info *p_enc_info = &inst->codec_info->enc_info;
+		struct enc_codec_param *param = &p_enc_info->open_param.codec_param;
+
+		seq_printf(s, "profile %d, level %d, tier %d\n",
+			   param->profile, param->level, param->tier);
+
+		seq_printf(s, "frame_rate %d, idr_period %d, intra_period %d\n",
+			   param->frame_rate, param->idr_period, param->intra_period);
+
+		seq_printf(s, "rc %d, mode %d, bitrate %d\n",
+			   param->en_rate_control,
+			   param->rc_mode,
+			   param->bitrate);
+
+		seq_printf(s, "qp %d, i_qp [%d, %d], p_qp [%d, %d], b_qp [%d, %d]\n",
+			   param->qp,
+			   param->min_qp_i, param->max_qp_i,
+			   param->min_qp_p, param->max_qp_p,
+			   param->min_qp_b, param->max_qp_b);
+	}
+
+	return 0;
+}
+
+static int wave6_vpu_dbg_open(struct inode *inode, struct file *filp)
+{
+	return single_open(filp, wave6_vpu_dbg_instance, inode->i_private);
+}
+
+static const struct file_operations wave6_vpu_dbg_fops = {
+	.owner = THIS_MODULE,
+	.open = wave6_vpu_dbg_open,
+	.release = single_release,
+	.read = seq_read,
+	.llseek = seq_lseek,
+};
+
+int wave6_vpu_create_dbgfs_file(struct vpu_instance *inst)
+{
+	char name[64];
+
+	if (WARN_ON(!inst || !inst->dev))
+		return -EINVAL;
+
+	if (IS_ERR_OR_NULL(inst->dev->debugfs))
+		return 0;
+
+	scnprintf(name, sizeof(name), "instance.%d", inst->id);
+	inst->debugfs = debugfs_create_file((const char *)name,
+					    0444,
+					    inst->dev->debugfs,
+					    inst,
+					    &wave6_vpu_dbg_fops);
+
+	return 0;
+}
+
+void wave6_vpu_remove_dbgfs_file(struct vpu_instance *inst)
+{
+	if (!inst || !inst->debugfs)
+		return;
+
+	debugfs_remove(inst->debugfs);
+	inst->debugfs = NULL;
+}
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h new file +2 -0
new file mode 100644
index 000000000000..6453eb2de76f
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
+/*

drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h line 12

Quoted 10 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h new file +10 -0
+ * Wave6 series multi-standard codec IP - debug interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ */
+
+#ifndef __WAVE6_VPU_DBG_H__
+#define __WAVE6_VPU_DBG_H__
+
+int wave6_vpu_create_dbgfs_file(struct vpu_instance *inst);
+void wave6_vpu_remove_dbgfs_file(struct vpu_instance *inst);

You add this patch 6. but call this funcation at patch 4.

You need make sure each patch can build without break bisect.

Frank

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h new file +2 -1
+
+#endif /* __WAVE6_VPU_DBG_H__ */
--

2.31.1

author
#25 lore raw

RE: [PATCH v7 6/9] media: chips-media: wave6: Improve debugging capabilities

Hi, Frank.

Quoted 14 lines

-----Original Message-----
From: Frank Li <[email protected]>
Sent: Thursday, September 10, 2026 5:41 AM
To: Nas Chung <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jackson.lee <[email protected]>; lafley.kim <[email protected]>; [email protected]; Ming Qian <[email protected]>
Subject: Re: [PATCH v7 6/9] media: chips-media: wave6: Improve debugging capabilities

Quoted 3 lines

On Fri, Sep 04, 2026 at 03:46:32PM +0900, Nas Chung wrote:

media: chips-media: wave6: Add trace and debugfs

I'll address this in v8.

Quoted 5 lines
Quoted 3 lines

Add debugfs entries and trace events to provide detailed debugging information.

Nit: need extra space line here.

OK.

Quoted 4 lines
Quoted 2 lines

These enhancements help diagnose issues and improve debugging

Remove "These enhancements".

I'll remove it.

...

Quoted 11 lines

drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h line 12

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h new file +6 -0
+
+#ifndef __WAVE6_VPU_DBG_H__
+#define __WAVE6_VPU_DBG_H__
+
+int wave6_vpu_create_dbgfs_file(struct vpu_instance *inst);
+void wave6_vpu_remove_dbgfs_file(struct vpu_instance *inst);

You add this patch 6. but call this funcation at patch 4.

You need make sure each patch can build without break bisect.

You are right that patch 4 calls what patch 6 declares.

But the build does not break: wave6/Kconfig and wave6/Makefile are added in patch 8, so nothing under wave6/ is compiled in patches 3-7. I built every commit individually.

Thanks.
Nas.

Quoted 8 lines

Frank

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h new file +2 -1
+
+#endif /* __WAVE6_VPU_DBG_H__ */
--

2.31.1

#26 lore raw

Re: [PATCH v7 6/9] media: chips-media: wave6: Improve debugging capabilities

Quoted 49 lines · Nas Chung

Hi, Frank.

Quoted 14 lines

-----Original Message-----
From: Frank Li <[email protected]>
Sent: Thursday, September 10, 2026 5:41 AM
To: Nas Chung <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jackson.lee <[email protected]>; lafley.kim <[email protected]>; [email protected]; Ming Qian <[email protected]>
Subject: Re: [PATCH v7 6/9] media: chips-media: wave6: Improve debugging capabilities

Quoted 3 lines

On Fri, Sep 04, 2026 at 03:46:32PM +0900, Nas Chung wrote:

media: chips-media: wave6: Add trace and debugfs

I'll address this in v8.

Quoted 5 lines
Quoted 3 lines

Add debugfs entries and trace events to provide detailed debugging information.

Nit: need extra space line here.

OK.

Quoted 4 lines
Quoted 2 lines

These enhancements help diagnose issues and improve debugging

Remove "These enhancements".

I'll remove it.

...

Quoted 10 lines

drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h line 12

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h new file +6 -0
+
+#ifndef __WAVE6_VPU_DBG_H__
+#define __WAVE6_VPU_DBG_H__
+
+int wave6_vpu_create_dbgfs_file(struct vpu_instance *inst);
+void wave6_vpu_remove_dbgfs_file(struct vpu_instance *inst);

You add this patch 6. but call this funcation at patch 4.

Quoted 7 lines
Quoted 1 line

You need make sure each patch can build without break bisect.

You are right that patch 4 calls what patch 6 declares.

But the build does not break: wave6/Kconfig and wave6/Makefile are added in patch 8, so nothing under wave6/ is compiled in patches 3-7. I built every commit individually.

Still not good yet, you can add call wave6_vpu_create_dbgfs_file() at patch 6, instead of patch4.

You need make sure your code is built and passed at every patch. You can add library/makefile firstly, then enable it gradulately. But need make sure pass build test, some kernel CI tools build each patch and easy to locate the problem, if build failure at last patch, which cause some difficult to locate the real patch, which cause problem.

Frank

Quoted 12 lines

Thanks.
Nas.

Quoted 8 lines

Frank

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h new file +2 -1
+
+#endif /* __WAVE6_VPU_DBG_H__ */
--

2.31.1

author
#27 lore raw

RE: [PATCH v7 6/9] media: chips-media: wave6: Improve debugging capabilities

Hi, Frank.

Quoted 75 lines

-----Original Message-----
From: Frank Li <[email protected]>
Sent: Friday, September 11, 2026 1:16 AM
To: Nas Chung <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jackson.lee <[email protected]>; lafley.kim <[email protected]>; [email protected]; Ming Qian <[email protected]>
Subject: Re: [PATCH v7 6/9] media: chips-media: wave6: Improve debugging capabilities

Quoted 10 lines · Nas Chung

Hi, Frank.

Quoted 8 lines

-----Original Message-----
From: Frank Li <[email protected]>
Sent: Thursday, September 10, 2026 5:41 AM
To: Nas Chung <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux-

[email protected];

Quoted 39 lines
Quoted 9 lines

[email protected]; jackson.lee <[email protected]>; lafley.kim <[email protected]>; [email protected]; Ming Qian <[email protected]>
Subject: Re: [PATCH v7 6/9] media: chips-media: wave6: Improve debugging capabilities

On Fri, Sep 04, 2026 at 03:46:32PM +0900, Nas Chung wrote:

media: chips-media: wave6: Add trace and debugfs

I'll address this in v8.

Quoted 5 lines
Quoted 3 lines

Add debugfs entries and trace events to provide detailed debugging information.

Nit: need extra space line here.

OK.

Quoted 4 lines
Quoted 2 lines

These enhancements help diagnose issues and improve debugging

Remove "These enhancements".

I'll remove it.

...

Quoted 10 lines

drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h line 12

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h new file +6 -0
+
+#ifndef __WAVE6_VPU_DBG_H__
+#define __WAVE6_VPU_DBG_H__
+
+int wave6_vpu_create_dbgfs_file(struct vpu_instance *inst);
+void wave6_vpu_remove_dbgfs_file(struct vpu_instance *inst);

You add this patch 6. but call this funcation at patch 4.

Quoted 5 lines
Quoted 1 line

You need make sure each patch can build without break bisect.

You are right that patch 4 calls what patch 6 declares.

But the build does not break: wave6/Kconfig and wave6/Makefile are added

in

Quoted 1 line

patch 8, so nothing under wave6/ is compiled in patches 3-7. I built

every

Quoted 1 line

commit individually.

Quoted 8 lines

Still not good yet, you can add call wave6_vpu_create_dbgfs_file() at patch 6, instead of patch4.

You need make sure your code is built and passed at every patch. You can add library/makefile firstly, then enable it gradulately. But need make sure pass build test, some kernel CI tools build each patch and easy to locate the problem, if build failure at last patch, which cause some difficult to locate the real patch, which cause problem.

Agreed. In v8 wave6/Kconfig and wave6/Makefile will come in the first wave6 patch so every patch compiles on its own. I'll build each patch to confirm.

Thanks.
Nas.

Quoted 15 lines

Frank

Quoted 12 lines

Thanks.
Nas.

Quoted 8 lines

Frank

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-dbg.h new file +2 -1
+
+#endif /* __WAVE6_VPU_DBG_H__ */
--

2.31.1

author
#28 lore raw

PATCH v7 7/9 media: chips-media: wave6: Add Wave6 thermal cooling device

In reply to Nas Chung

Add a thermal cooling device for the Wave6 VPU.
The device operates within the Linux thermal framework, adjusting the VPU performance state based on thermal conditions.

---
 .../chips-media/wave6/wave6-vpu-thermal.c     | 143 ++++++++++++++++++
 .../chips-media/wave6/wave6-vpu-thermal.h     |  24 +++
 2 files changed, 167 insertions(+)
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.h
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +143 -0
new file mode 100644
index 000000000000..1e67943b213b
@@ -0,0 +1,143 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+/*
+ * Wave6 series multi-standard codec IP - wave6 thermal cooling interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ *
+ */
+
+#include <linux/pm_domain.h>
+#include <linux/pm_opp.h>
+#include <linux/units.h>
+#include <linux/slab.h>
+#include "wave6-vpu-thermal.h"
+
+static int wave6_vpu_thermal_cooling_update(struct vpu_thermal_cooling *thermal,
+					    int state)
+{
+	unsigned long new_clock_rate;
+	int ret;
+
+	if (state > thermal->thermal_max || !thermal->cooling)
+		return 0;
+
+	new_clock_rate = DIV_ROUND_UP(thermal->freq_table[state], HZ_PER_KHZ);
+	dev_dbg(thermal->dev, "receive cooling state: %d, new clock rate %ld\n",
+		state, new_clock_rate);
+
+	ret = dev_pm_genpd_set_performance_state(thermal->dev, new_clock_rate);
+	if (ret && !((ret == -ENODEV) || (ret == -EOPNOTSUPP))) {
+		dev_err(thermal->dev, "failed to set perf to %lu, ret = %d\n",
+			new_clock_rate, ret);
+		return ret;
+	}
+
+	return 0;
+}
+
+static int wave6_vpu_cooling_get_max_state(struct thermal_cooling_device *cdev,
+					   unsigned long *state)
+{
+	struct vpu_thermal_cooling *thermal = cdev->devdata;
+
+	*state = thermal->thermal_max;
+
+	return 0;
+}
+
+static int wave6_vpu_cooling_get_cur_state(struct thermal_cooling_device *cdev,
+					   unsigned long *state)
+{
+	struct vpu_thermal_cooling *thermal = cdev->devdata;
+
+	*state = thermal->thermal_event;
+
+	return 0;
+}
+
+static int wave6_vpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
+					   unsigned long state)
+{
+	struct vpu_thermal_cooling *thermal = cdev->devdata;
+	int ret;
+
+	ret = wave6_vpu_thermal_cooling_update(thermal, state);
+	if (ret)
+		return ret;
+
+	thermal->thermal_event = state;
+
+	return 0;
+}
+
+static const struct thermal_cooling_device_ops wave6_cooling_ops = {
+	.get_max_state = wave6_vpu_cooling_get_max_state,
+	.get_cur_state = wave6_vpu_cooling_get_cur_state,
+	.set_cur_state = wave6_vpu_cooling_set_cur_state,
+};
+
+int wave6_vpu_cooling_init(struct device *dev, struct vpu_thermal_cooling *thermal)
+{
+	int i;
+	int num_opps;
+	unsigned long freq;
+	int ret = -EINVAL;
+
+	if (WARN_ON(!thermal || !thermal->dev))
+		return -EINVAL;
+
+	num_opps = dev_pm_opp_get_opp_count(thermal->dev);
+	if (num_opps < 0) {
+		dev_err(thermal->dev, "fail to get pm opp count, ret = %d\n", num_opps);
+		return num_opps;
+	}
+	if (num_opps == 0) {
+		dev_err(thermal->dev, "no OPP entries found\n");
+		return -ENODEV;
+	}
+
+	thermal->freq_table = devm_kcalloc(dev, num_opps,
+					   sizeof(*thermal->freq_table),
+					   GFP_KERNEL);
+	if (!thermal->freq_table) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
+	for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
+		struct dev_pm_opp *opp;
+
+		opp = dev_pm_opp_find_freq_floor(thermal->dev, &freq);
+		if (IS_ERR(opp))
+			break;
+
+		dev_pm_opp_put(opp);
+
+		dev_dbg(thermal->dev, "[%d] = %lu\n", i, freq);
+		if (freq < 100 * HZ_PER_MHZ)
+			break;
+
+		thermal->freq_table[i] = freq;
+		thermal->thermal_max = i;
+	}
+
+	if (!thermal->thermal_max)
+		goto error;
+
+	thermal->thermal_event = 0;
+	thermal->cooling = devm_thermal_of_child_cooling_device_register(dev,
+									 dev->of_node,
+									 dev_name(thermal->dev),
+									 thermal,
+									 &wave6_cooling_ops);
+	if (IS_ERR(thermal->cooling)) {
+		dev_err(thermal->dev, "register cooling device failed\n");
+		ret = PTR_ERR(thermal->cooling);
+		goto error;
+	}
+
+	return 0;
+
+error:
+	return ret;
+}
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.h new file +24 -0
new file mode 100644
index 000000000000..7c5e8aed6ef7
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
+/*
+ * Wave6 series multi-standard codec IP - wave6 thermal cooling interface
+ *
+ * Copyright (C) 2025 CHIPS&MEDIA INC
+ *
+ */
+
+#ifndef __WAVE6_VPU_THERMAL_H__
+#define __WAVE6_VPU_THERMAL_H__
+
+#include <linux/thermal.h>
+
+struct vpu_thermal_cooling {
+	struct device *dev;
+	int thermal_event;
+	int thermal_max;
+	struct thermal_cooling_device *cooling;
+	unsigned long *freq_table;
+};
+
+int wave6_vpu_cooling_init(struct device *dev, struct vpu_thermal_cooling *thermal);
+
+#endif /* __WAVE6_VPU_THERMAL_H__ */
Signature · 1 line
2.31.1
#29 lore raw

Re: [PATCH v7 7/9] media: chips-media: wave6: Add Wave6 thermal cooling device

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Medium] Initial thermal state commands from the framework are silently discarded during registration due…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] Initial thermal state commands from the framework are silently discarded during registration due to a premature check on thermal->cooling.
- [Medium] The cooling device acts as a silent placebo if PM domain performance states are unsupported.
- [Medium] The driver incorrectly uses dev_pm_genpd_set_performance_state() with a raw frequency in kHz, violating the OPP/genpd abstraction and risking hardware malfunction on platforms with abstract performance levels.
- [Low] Defensive out-of-bounds check returns success (0) instead of an error, masking invalid requests and caching wrong states.
--

commit 99914a36008f0afa814ae979e8bbaf4616a8b747
Author: Nas Chung <[email protected]>

media: chips-media: wave6: Add Wave6 thermal cooling device

Add a thermal cooling device for the Wave6 VPU.
The device operates within the Linux thermal framework, adjusting the VPU performance state based on thermal conditions.

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +0 -0
new file mode 100644
index 0000000000000..1e67943b213bd
@@ -0,0 +1,143 @@

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c line 22

Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +8 -0
+static int wave6_vpu_thermal_cooling_update(struct vpu_thermal_cooling *thermal,
+					    int state)
+{
+	unsigned long new_clock_rate;
+	int ret;
+
+	if (state > thermal->thermal_max || !thermal->cooling)
+		return 0;

[Severity: Low]
Does returning 0 for an invalid state request (state > thermal->thermal_max) mask the error from the thermal core? It seems this would incorrectly allow the caller wave6_vpu_cooling_set_cur_state() to cache this invalid state in thermal->thermal_event.

[Severity: Medium]
Will the !thermal->cooling check incorrectly trigger during the initial synchronous state assignment?

When devm_thermal_of_child_cooling_device_register() is called in wave6_vpu_cooling_init(), it synchronously calls wave6_cooling_ops.set_cur_state(). The registration function has not yet returned to assign the pointer to thermal->cooling, so this check evaluates to true. This would silently discard the initial cooling state set by the thermal governor.

drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c line 28

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +6 -0
+
+	new_clock_rate = DIV_ROUND_UP(thermal->freq_table[state], HZ_PER_KHZ);
+	dev_dbg(thermal->dev, "receive cooling state: %d, new clock rate %ld\n",
+		state, new_clock_rate);
+
+	ret = dev_pm_genpd_set_performance_state(thermal->dev, new_clock_rate);

[Severity: Medium]
Is it correct to use dev_pm_genpd_set_performance_state() with a raw frequency in kHz?

The dev_pm_genpd_set_performance_state() function expects a performance state index (OPP level). Calculating DIV_ROUND_UP(thermal->freq_table[state], HZ_PER_KHZ) instead of retrieving the actual OPP level via dev_pm_opp_get_level() could violate the OPP/genpd abstraction. On platforms where the performance domain uses abstract levels, dividing the frequency by 1000 could result in an invalid or zero performance state, breaking the cooling mitigation.

drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c line 29

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +1 -0
+	if (ret && !((ret == -ENODEV) || (ret == -EOPNOTSUPP))) {

[Severity: Medium]
Does ignoring -ENODEV and -EOPNOTSUPP errors create a situation where the cooling device acts as a silent placebo?

If the platform is missing genpd performance state support for the VPU, these errors are caught and 0 (success) is returned. The thermal governor will believe the device was throttled and will keep trying, but temperatures will not drop since the device is not actually being cooled.

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +7 -0
+		dev_err(thermal->dev, "failed to set perf to %lu, ret = %d\n",
+			new_clock_rate, ret);
+		return ret;
+	}
+
+	return 0;
+}

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +2 -0
+int wave6_vpu_cooling_init(struct device *dev, struct vpu_thermal_cooling *thermal)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c line 132

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +6 -0
+	thermal->thermal_event = 0;
+	thermal->cooling = devm_thermal_of_child_cooling_device_register(dev,
+									 dev->of_node,
+									 dev_name(thermal->dev),
+									 thermal,
+									 &wave6_cooling_ops);

[Severity: Medium]
As noted above in wave6_vpu_thermal_cooling_update(), since this is where registration happens, thermal->cooling is only assigned after the framework might have already attempted to set an initial cooling state. This causes that initial command to be silently discarded.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
author
#30 lore raw

RE: [PATCH v7 7/9] media: chips-media: wave6: Add Wave6 thermal cooling device

Hi,

Quoted 45 lines

-----Original Message-----
From: [email protected] <[email protected]>
Sent: Friday, September 4, 2026 4:00 PM
To: Nas Chung <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v7 7/9] media: chips-media: wave6: Add Wave6 thermal cooling device

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] Initial thermal state commands from the framework are silently discarded during registration due to a premature check on `thermal-

Quoted 1 line

cooling`.

- [Medium] The cooling device acts as a silent placebo if PM domain performance states are unsupported.
- [Medium] The driver incorrectly uses dev_pm_genpd_set_performance_state() with a raw frequency in kHz, violating the OPP/genpd abstraction and risking hardware malfunction on platforms with abstract performance levels.
- [Low] Defensive out-of-bounds check returns success (0) instead of an error, masking invalid requests and caching wrong states.
--

commit 99914a36008f0afa814ae979e8bbaf4616a8b747
Author: Nas Chung <[email protected]>

media: chips-media: wave6: Add Wave6 thermal cooling device

Add a thermal cooling device for the Wave6 VPU.
The device operates within the Linux thermal framework, adjusting the VPU performance state based on thermal conditions.

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c

Quoted 5 lines

new file mode 100644
index 0000000000000..1e67943b213bd

drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +0 -0
@@ -0,0 +1,143 @@

[ ... ]

Quoted 1 line

+static int wave6_vpu_thermal_cooling_update(struct vpu_thermal_cooling

*thermal,

Quoted 13 lines

drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c line 22

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +7 -0
+					    int state)
+{
+	unsigned long new_clock_rate;
+	int ret;
+
+	if (state > thermal->thermal_max || !thermal->cooling)
+		return 0;

[Severity: Low]
Does returning 0 for an invalid state request (state > thermal->thermal_max) mask the error from the thermal core? It seems this would incorrectly allow the caller wave6_vpu_cooling_set_cur_state() to cache this invalid state in thermal->thermal_event.

The thermal core already rejects state > cdev->max_state, so this is dead code. Will remove in v8.

Quoted 11 lines

[Severity: Medium]
Will the !thermal->cooling check incorrectly trigger during the initial synchronous state assignment?

When devm_thermal_of_child_cooling_device_register() is called in wave6_vpu_cooling_init(), it synchronously calls wave6_cooling_ops.set_cur_state(). The registration function has not yet returned to assign the pointer to thermal->cooling, so this check evaluates to true. This would silently discard the initial cooling state set by the thermal governor.

That can happen. Will drop the condition in v8.

Quoted 15 lines

drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +2 -0
+
+	new_clock_rate = DIV_ROUND_UP(thermal->freq_table[state],

HZ_PER_KHZ);

drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +1 -0
+	dev_dbg(thermal->dev, "receive cooling state: %d, new clock

rate %ld\n",

drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +3 -0
+		state, new_clock_rate);
+
+	ret = dev_pm_genpd_set_performance_state(thermal->dev,

new_clock_rate);

[Severity: Medium]
Is it correct to use dev_pm_genpd_set_performance_state() with a raw frequency in kHz?

Quoted 8 lines

The dev_pm_genpd_set_performance_state() function expects a performance state index (OPP level). Calculating DIV_ROUND_UP(thermal->freq_table[state], HZ_PER_KHZ) instead of retrieving the actual OPP level via dev_pm_opp_get_level() could violate the OPP/genpd abstraction. On platforms where the performance domain uses abstract levels, dividing the frequency by 1000 could result in an invalid or zero performance
state, breaking the cooling mitigation.

Will use the OPP level instead of computing the frequency in the driver, via dev_pm_opp_get_level().

Quoted 7 lines

drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c line 29

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +1 -0
+	if (ret && !((ret == -ENODEV) || (ret == -EOPNOTSUPP))) {

[Severity: Medium]
Does ignoring -ENODEV and -EOPNOTSUPP errors create a situation where the cooling device acts as a silent placebo?

Quoted 5 lines

If the platform is missing genpd performance state support for the VPU, these errors are caught and 0 (success) is returned. The thermal governor will believe the device was throttled and will keep trying, but temperatures
will not drop since the device is not actually being cooled.

Agreed. Will report the failure properly in v8.

Quoted 26 lines
Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +7 -0
+		dev_err(thermal->dev, "failed to set perf to %lu, ret = %d\n",
+			new_clock_rate, ret);
+		return ret;
+	}
+
+	return 0;
+}

[ ... ]

Quoted 1 line

+int wave6_vpu_cooling_init(struct device *dev, struct

vpu_thermal_cooling *thermal)

Quoted 1 line

+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c line 138

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +4 -0
+	thermal->thermal_event = 0;
+	thermal->cooling = devm_thermal_of_child_cooling_device_register(dev,
+									 dev->of_node,
+

dev_name(thermal->dev),

drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-vpu-thermal.c new file +2 -0
+									 thermal,
+

&wave6_cooling_ops);

Quoted 5 lines

[Severity: Medium]
As noted above in wave6_vpu_thermal_cooling_update(), since this is where registration happens, thermal->cooling is only assigned after the framework might have already attempted to set an initial cooling state. This causes that initial command to be silently discarded.

Same here. Will drop the condition in v8.

Thanks.
Nas.

Quoted 5 lines

--
Sashiko AI review *
https://sashiko.dev/#/patchset/[email protected]
om?part=7

author
#31 lore raw

PATCH v7 8/9 media: chips-media: wave6: Add Wave6 control driver

In reply to Nas Chung

Add the control driver for the Chips&Media Wave6 video codec IP.

The hardware contains one control register region and four interface register regions for a shared video processing engine.

This driver handles the control region and manages shared resources such as firmware loading, firmware memory allocation, and coordination required by the interface register regions.

It also instantiates and coordinates with the wave6-core child devices for firmware and power state management.

---
 drivers/media/platform/chips-media/Kconfig    |   1 +
 drivers/media/platform/chips-media/Makefile   |   1 +
 .../media/platform/chips-media/wave6/Kconfig  |  17 +
 .../media/platform/chips-media/wave6/Makefile |  17 +
 .../platform/chips-media/wave6/wave6-vpu.c    | 826 ++++++++++++++++++
 .../platform/chips-media/wave6/wave6-vpu.h    | 143 +++
 6 files changed, 1005 insertions(+)
 create mode 100644 drivers/media/platform/chips-media/wave6/Kconfig
 create mode 100644 drivers/media/platform/chips-media/wave6/Makefile
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu.c
 create mode 100644 drivers/media/platform/chips-media/wave6/wave6-vpu.h
drivers/media/platform/chips-media/Kconfig +1 -0
index ad350eb6b1fc..8ef7fc8029a4 100644
@@ -4,3 +4,4 @@ comment "Chips&Media media platform drivers"
 
 source "drivers/media/platform/chips-media/coda/Kconfig"
 source "drivers/media/platform/chips-media/wave5/Kconfig"
+source "drivers/media/platform/chips-media/wave6/Kconfig"
drivers/media/platform/chips-media/Makefile +1 -0
index 6b5d99de8b54..b9a07a91c9d6 100644
@@ -2,3 +2,4 @@
 
 obj-y += coda/
 obj-y += wave5/
+obj-y += wave6/
drivers/media/platform/chips-media/wave6/Kconfig new file +17 -0
new file mode 100644
index 000000000000..63d79c56c7fc
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config VIDEO_WAVE6_VPU
+	tristate "Chips&Media Wave6 Codec Driver"
+	depends on V4L_MEM2MEM_DRIVERS
+	depends on VIDEO_DEV && OF
+	depends on ARCH_MXC || COMPILE_TEST
+	select VIDEOBUF2_DMA_CONTIG
+	select V4L2_MEM2MEM_DEV
+	select GENERIC_ALLOCATOR
+	help
+	  Chips&Media Wave6 stateful codec driver.
+	  The wave6 driver manages shared resources such as firmware memory.
+	  The wave6-core driver provides encoding and decoding capabilities
+	  for H.264, HEVC, and other video formats.
+	  To compile this driver as modules, choose M here: the
+	  modules will be called wave6 and wave6-core.
drivers/media/platform/chips-media/wave6/Makefile new file +17 -0
new file mode 100644
index 000000000000..06f8ac9bef14
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0
+
+# tell define_trace.h where to find the trace header
+CFLAGS_wave6-vpu-core.o := -I$(src)
+
+wave6-objs += wave6-vpu.o \
+	      wave6-vpu-thermal.o
+obj-$(CONFIG_VIDEO_WAVE6_VPU) += wave6.o
+
+wave6-core-objs += wave6-vpu-core.o \
+		   wave6-vpu-v4l2.o \
+		   wave6-vpu-dbg.o \
+		   wave6-vpuapi.o \
+		   wave6-vpu-dec.o \
+		   wave6-vpu-enc.o \
+		   wave6-hw.o
+obj-$(CONFIG_VIDEO_WAVE6_VPU) += wave6-core.o
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +826 -0

826 lines not on this page · raw message

drivers/media/platform/chips-media/wave6/wave6-vpu.h new file +143 -0

143 lines not on this page · raw message

Signature · 1 line
2.31.1
#32 lore raw

Re: [PATCH v7 8/9] media: chips-media: wave6: Add Wave6 control driver

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [High] Data race on shared work_buffers_avail counter leads to DMA memory corruption. - [High]…

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] Data race on shared work_buffers_avail counter leads to DMA memory corruption.
- [High] Incompatible pointer type passed to gen_pool_dma_alloc causes structure memory corruption and invalid DMA mappings.
- [High] Use-after-free of the driver device structure in the async firmware load callback on unbind.
- [High] System deadlock caused by holding the parent's mutex while synchronously unregistering child devices.
- [High] Failure in work buffer initialization leaves the VPU state machine permanently corrupted.
- [High] Auxiliary power domains are never powered on because they lack device links.
- [Medium] Leaked runtime PM usage counter due to an improper conditional check on device suspend state.
- [Medium] Using a value of 0 to validate a DMA mapping leads to an IOMMU resource leak.
- [Medium] Mixing scoped guards with goto-based error handling violates kernel cleanup API rules.
--

commit 373df31fcd64a0ed065b0d792d9b1dac72e53fd5
Author: Nas Chung <[email protected]>
Subject: media: chips-media: wave6: Add Wave6 control driver

This commit adds the control driver for the Chips&Media Wave6 video codec IP. It manages shared resources, firmware memory allocation, and power states for the child core devices.

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +0 -0

[ ... ]

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +3 -0
+static int wave6_vpu_init_vpu(struct wave6_vpu_device *vpu,
+			      struct vpu_core_device *core)
+{

[ ... ]

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +1 -0
+	wave6_vpu_set_state(vpu, WAVE6_VPU_STATE_PREPARE);

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu.c line 274

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +4 -0
+init_done:
+	ret = wave6_vpu_init_work_buf(vpu, core);
+	if (ret)
+		return ret;

[Severity: High]
If wave6_vpu_init_work_buf() fails here, the function returns immediately without resetting vpu->state. Will this leave the driver permanently in WAVE6_VPU_STATE_PREPARE, causing subsequent attempts to boot to skip the init sequence?

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +5 -0
+
+	wave6_vpu_set_state(vpu, WAVE6_VPU_STATE_ON);
+
+	return 0;
+}

[ ... ]

Quoted 12 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +12 -0
+static int wave6_vpu_get(struct wave6_vpu_device *vpu,
+			 struct vpu_core_device *core)
+{
+	int ret;
+
+	if (WARN_ON(!vpu || !core))
+		return -EINVAL;
+
+	guard(mutex)(&vpu->lock);
+
+	if (!vpu->fw_available)
+		return -EINVAL;

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu.c line 414

Quoted 9 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +9 -0
+	ret = wave6_vpu_try_boot(vpu, core);
+	if (ret)
+		goto error_boot;
+
+	return 0;
+
+error_boot:
+	pm_runtime_put_sync(vpu->dev);
+error_pm:

[Severity: Medium]
Does this mix goto-based error handling with guard() cleanup in the same function? The kernel cleanup API expects that a function either uses scoped guards entirely or goto entirely to prevent confusing ownership and cleanup ordering.

Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +8 -0
+	atomic_dec(&vpu->core_count);
+
+	return ret;
+}
+
+static void wave6_vpu_put(struct wave6_vpu_device *vpu,
+			  struct vpu_core_device *core)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu.c

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +1 -0
+	guard(mutex)(&vpu->lock);

[Severity: High]
Will this cause a system deadlock when the parent unregisters child devices? During wave6_vpu_release(), the parent takes vpu->lock and triggers core destruction. The child's remove callback will then try to put the VPU, leading back into this function which attempts to acquire the exact same lock.

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +3 -0
+
+	if (!vpu->fw_available)
+		return;

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu.c line 438

Quoted 4 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +4 -0
+	wave6_vpu_sleep(vpu, core);
+
+	if (!pm_runtime_suspended(vpu->dev))
+		pm_runtime_put_sync(vpu->dev);

[Severity: Medium]
Does conditionally skipping the put operation cause a usage counter leak? If the device was suspended by other system events, skipping this symmetrical put prevents the device from ever entering lower power states in the future.

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +7 -0
+}
+
+static void wave6_vpu_require_work_buffer(struct wave6_vpu_device *vpu,
+					  struct vpu_core_device *core)
+{
+	struct vpu_buf *vb;
+	u32 size;

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu.c line 461

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +5 -0
+	if (WARN_ON(vpu->work_buffers_avail == 0))
+		goto exit;
+
+	vpu->work_buffers_avail--;
+	vb = &vpu->work_buffers[vpu->work_buffers_avail];

[Severity: High]
Is there a data race on the work_buffers_avail counter here? If multiple VPU cores trigger requests concurrently without holding vpu->lock, could the counter underflow and lead to an out-of-bounds array access that corrupts DMA memory?

Quoted 6 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +6 -0
+
+	wave6_vdi_writel(core->reg_base, W6_CMD_SET_WORK_BUF_ADDR, vb->daddr);
+
+exit:
+	wave6_vdi_writel(core->reg_base, W6_CMD_SET_WORK_BUF_SIZE, SET_WORK_BUF_SIZE_ACK);
+}

[ ... ]

Quoted 8 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +8 -0
+static void wave6_vpu_release(struct wave6_vpu_device *vpu)
+{
+	guard(mutex)(&vpu->lock);
+
+	vpu->fw_available = false;
+	wave6_vpu_destroy_cores(vpu);
+	wave6_vpu_free_work_buffers(vpu);
+	if (vpu->sram_pool && vpu->sram_buf.vaddr) {

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu.c line 554

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +3 -0
+	}
+	if (vpu->code_buf.dma_addr)
+		dma_unmap_resource(vpu->dev,

[Severity: Medium]
Could this condition leak valid IOMMU mappings if the DMA allocator returns 0? Since 0 is a valid DMA address on many platforms, skipping the unmap when the address evaluates to false can lead to a resource leak.

drivers/media/platform/chips-media/wave6/wave6-vpu.c line 565

Quoted 11 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +11 -0
+				   vpu->code_buf.dma_addr,
+				   vpu->code_buf.size,
+				   DMA_BIDIRECTIONAL,
+				   0);
+}
+
+static void wave6_vpu_load_firmware(const struct firmware *fw, void *context)
+{
+	struct wave6_vpu_device *vpu = context;
+
+	guard(mutex)(&vpu->lock);

[Severity: High]
Can this cause a use-after-free if the device unbinds while firmware loading is in progress? If the driver is removed and devres frees the vpu memory, this async callback might try to acquire a mutex that resides in freed memory.

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +2 -0
+
+	if (!fw || !fw->data) {

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu.c line 579

Quoted 5 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +5 -0
+	if (fw->size + W6_EXTRA_CODE_BUF_SIZE > wave6_vpu_get_code_buf_size(vpu)) {
+		dev_err(vpu->dev, "firmware size (%zd > %zd) is too big\n",
+			fw->size, vpu->code_buf.size);
+		vpu->fw_available = false;
+		goto exit;

[Severity: Medium]
Is this mixing goto-based error handling and scoped guard cleanup again? This violates the cleanup API guidelines.

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +1 -0
+	}

[ ... ]

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +3 -0
+static int wave6_vpu_attach_pm_domains(struct wave6_vpu_device *vpu)
+{
+	int ret;

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu.c line 632

Quoted 7 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +7 -0
+	if (vpu->num_pm_domains == 1) {
+		/* genpd_dev_pm_attach() attach automatically if count is 1 */
+		vpu->num_pm_domains = 0;
+		return 0;
+	}
+
+	ret = dev_pm_domain_attach_list(vpu->dev, NULL, &vpu->pd_list);

[Severity: High]
By passing NULL for the data parameter, does this skip creating device links for the auxiliary domains? Without DL_FLAG_PM_RUNTIME links, the PM core won't power on these domains during runtime, which might lead to exceptions when accessing unpowered hardware registers.

Quoted 1 line
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +1 -0
+	if (ret < 0) {

[ ... ]

Quoted 2 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +2 -0
+static int wave6_vpu_probe(struct platform_device *pdev)
+{

[ ... ]

drivers/media/platform/chips-media/wave6/wave6-vpu.c line 726

Quoted 11 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +11 -0
+	vpu->sram_pool = of_gen_pool_get(pdev->dev.of_node, "sram", 0);
+	if (vpu->sram_pool) {
+		vpu->sram_buf.size = vpu->res->sram_size;
+		vpu->sram_buf.vaddr = gen_pool_dma_alloc(vpu->sram_pool,
+							 vpu->sram_buf.size,
+							 &vpu->sram_buf.phys_addr);
+		if (!vpu->sram_buf.vaddr) {
+			vpu->sram_buf.size = 0;
+		} else {
+			vpu->sram_buf.dma_addr = dma_map_resource(&pdev->dev,
+								  vpu->sram_buf.phys_addr,

[Severity: High]
gen_pool_dma_alloc() expects a dma_addr_t pointer for its third argument to write back the DMA address, but a phys_addr_t pointer is being passed here. Could this cause out-of-bounds memory corruption on architectures where the sizes differ? Additionally, is the returned DMA address being incorrectly passed to dma_map_resource() as if it were a physical address?

Quoted 3 lines
drivers/media/platform/chips-media/wave6/wave6-vpu.c new file +3 -0
+								  vpu->sram_buf.size,
+								  DMA_BIDIRECTIONAL,
+								  0);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8