Skip to contentSkip to conversation

media: add arm Mali-V76 video decoder driver

RFC PATCH In review Charvi Mehta 4 participants 63 messages Nov 19 – Nov 25 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover media: add arm Mali-V76 video decoder driver

This series introduces support for the Arm Mali-V76 Video Processing Unit (VPU), providing H.264 video stream decoding through the V4L2 stateful memory-to-memory interface.

The Mali-V76 offloads video decoding from the application processor. The host driver loads and manages the codec firmware, communicating with it through asynchronous message queues, interrupts and RPC requests. It also manages the firmware address space using the V76 internal MMU and controls hardware resources through the LSID and job-queue interfaces.

The driver currently supports:

- H.264 decoding through the V4L2 stateful memory-to-memory interface.
- Multi-planar capture using V4L2_PIX_FMT_YUV420M, V4L2_PIX_FMT_NV12M and V4L2_PIX_FMT_NV21M, with MMAP and DMA-BUF buffer support.
- Variant-specific definitions of supported formats, frame sizes, controls and hardware resources.
- Explicit management of decoder and firmware states throughout stream setup, decoding and teardown.
- Asynchronous firmware loading and caching, with interrupt-driven communication for commands, buffer completion and firmware events.
- Separate control of OUTPUT and CAPTURE streaming, allowing CAPTURE buffers to be reconfigured during dynamic resolution changes.
- Decoder drain, end-of-stream and dynamic resolution change handling, including source-change events and LAST buffer signalling.
- Timestamp propagation and buffer metadata handling.
- Firmware error and unsupported-stream reporting.

The initial implementation supports a single active decoding session, with hardware resource management handled through the V76 LSID and job-queue interfaces. Support for additional codecs and concurrent sessions is outside the scope of this series.

The series is based on media-committers/next.

To simplify the testing a tree with the new patches on top has been made available at [1].

[1] https://gitlab.arm.com/media/vpu/charvi/linux.git mali/v76/v1

Cc Mauro Carvalho Chehab <[email protected]>
Cc Rob Herring <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Conor Dooley <[email protected]>
Cc Philipp Zabel <[email protected]>

Testing
=======
The driver has been tested on Mali-V76 pre-silicon FPGA.

Static tools like checkpatch, smatch, dt_binding_check, sparse, checkstack and Coccinelle run successfully with this driver.

The driver has been tested with v4l2-ctl[2] and Gstreamer[3], v4l2-compliance [4] and Fluster [5].

[2] v4l2-ctl -d /dev/video0 --verbose \
--set-fmt-video-out=width=1920,height=1080,pixelformat=H264 \
--set-fmt-video=width=1920,height=1080,pixelformat=NM12 \
--stream-mmap=4 --stream-out-mmap=1 \
--stream-from /media/CrowdRun_1080p.264 --stream-to decoded.yuv

[3] gst-launch-1.0 -v filesrc location=/media/CrowdRun_720p.264 ! h264parse \
! v4l2h264dec ! filesink location=decoded.yuv sync=false

[4] V4l2 compliance was tested using v4l2-compliance 1.28.1:

v4l2-compliance -d /dev/video0 --stream-from /media/CrowdRun_1080p.264 -s 200

v4l2-compliance 1.28.1, 64 bits, 64-bit time_t

Compliance test for mali-vpu device /dev/video0:

Driver Info:

	Driver name      : mali-vpu
	Card type        : mali-vpu-decoder
	Bus info         : platform:6f030000.video-codec
	Driver version   : 7.4.0
	Capabilities     : 0x84204000

Video Memory-to-Memory Multiplanar Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x04204000
Video Memory-to-Memory Multiplanar Streaming
Extended Pix Format
Detected Stateful Decoder

Required ioctls:
test VIDIOC_QUERYCAP: OK
test invalid ioctls: OK

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

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

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

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

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

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

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

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

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

Test input 0:

Streaming ioctls:
test read/write: OK (Not Supported)
test blocking wait: OK
Video Capture Multiplanar: Captured 632 buffers test MMAP (select): OK
Video Capture Multiplanar: Captured 632 buffers test MMAP (epoll): OK
test USERPTR (select): OK (Not Supported) test DMABUF: Cannot test, specify --expbuf-device

Total for mali-vpu device /dev/video0: 52, Succeeded: 52, Failed: 0, Warnings: 0

[5] Fluster was run with

GStreamer-H.264-V4L2 decoder:

Running test suite JVT-AVC_V1 with decoder GStreamer-H.264-V4L2 Ran 78/135 tests successfully.

The failing tests:

52 test vectors fail as they are interlaced clips which mali-vpu does not support.

List of these test vectors:
- cabac_mot_fld0_full
- cabac_mot_mbaff0_full
- cabac_mot_picaff0_full
- CABREF3_Sand_D
- CAFI1_SVA_C
- CAMA1_Sony_C
- CAMA1_TOSHIBA_B
- cama1_vtc_c
- cama2_vtc_b
- CAMA3_Sand_E
- cama3_vtc_b
- CAMACI3_Sony_C
- CAMANL1_TOSHIBA_B
- CAMANL2_TOSHIBA_B
- CAMANL3_Sand_E
- CAMASL3_Sony_B
- CAMP_MOT_MBAFF_L30
- CAMP_MOT_MBAFF_L31
- CANLMA2_Sony_C
- CANLMA3_Sony_C
- CAPA1_TOSHIBA_B
- CAPAMA3_Sand_F
- cavlc_mot_fld0_full_B
- cavlc_mot_mbaff0_full_B
- cavlc_mot_picaff0_full_B
- CVCANLMA2_Sony_C
- CVFI1_Sony_D
- CVFI1_SVA_C
- CVFI2_Sony_H
- CVFI2_SVA_C
- CVMA1_Sony_D
- CVMA1_TOSHIBA_B
- CVMANL1_TOSHIBA_B
- CVMANL2_TOSHIBA_B
- CVMAPAQP3_Sony_E
- CVMAQP2_Sony_G
- CVMAQP3_Sony_D
- CVMP_MOT_FLD_L30_B
- CVMP_MOT_FRM_L31_B
- CVNLFI1_Sony_C
- CVNLFI2_Sony_H
- CVPA1_TOSHIBA_B
- FI1_Sony_E
- MR6_BT_B
- MR7_BT_B
- MR8_BT_B
- MR9_BT_B
- Sharp_MP_Field_1_B
- Sharp_MP_Field_2_B
- Sharp_MP_Field_3_B
- Sharp_MP_PAFF_1r2
- Sharp_MP_PAFF_2r
2 test vectors failed because FMO is unsupported.
Test vectors: FM1_BT_B FM1_FT_E
2 test vectors failed because SP_SLICE type is unsupported.
Test vectors: SP1_BT_A sp2_bt_b
1 additional test vector, CVFC1_Sony_C completes decoding but fails checksum verification due to a stride-handling issue in GStreamer 1.24.13. GStreamer replaces the driver-reported 150-byte planar chroma stride with 152 bytes, causing the decoded chroma planes to be read incorrectly.

Running test suite JVT-FR-EXT with decoder GStreamer-H.264-V4L2 Ran 25/69 tests successfully.

The failing tests:

21 test vectors failed due to interlaced clips. Interlaced decoding is not supported in mali-vpu driver.

Test Vectors:
- brcm_freh10
- brcm_freh11
- brcm_freh4
- brcm_freh5
- brcm_freh6
- freh7_b
- FREXT01_JVC_D
- FREXT02_JVC_C
- FRExt2_Panasonic_C
- FRExt4_Panasonic_B
- HCAFF1_HHI_B
- HCAMFF1_HHI_B
- HCHP3_HHI_A
- HPCAFL_BRCM_C
- HPCAFLNL_BRCM_C
- HPCAMAPALQ_BRCM_B
- HPCVFL_BRCM_A HPCVFLNL_BRCM_A
- HVLCFI0_Sony_B
- HVLCMFF0_Sony_B
- HVLCPFF0_Sony_B

2 test vectors fail due to 10bit bitstream. 10 bit decoding support to be enabled in subequent patches.
Test vectors: FREH10-1 FREH10-2

21 test vectors failed because 4:2:2 is not supported in this driver. Test vectors:
- FREXT1_TANDBERG_A
- FREXT2_TANDBERG_A
- FREXT3_TANDBERG_A
- Hi422FR10_SONY_A
- Hi422FR11_SONY_A
- Hi422FR12_SONY_A
- Hi422FR1_SONY_A
- Hi422FR2_SONY_A
- Hi422FR3_SONY_A
- Hi422FR4_SONY_A
- Hi422FR13_SONY_A
- Hi422FR14_SONY_A
- Hi422FR15_SONY_A
- Hi422FR6_SONY_A
- Hi422FR7_SONY_A
- Hi422FR8_SONY_A
- Hi422FR9_SONY_A
- Hi422FREXT16_SONY_A
- Hi422FREXT17_SONY_A
- Hi422FREXT18_SONY_A
- Hi422FREXT19_SONY_A

Charvi Mehta (21):
dt-bindings: media: add Arm Mali-V76 VPU
media: mali-vpu: add Mali VPU platform driver
media: mali-vpu: register V4L2 decoder device and add querycap ioctl
media: mali-vpu: initialize decoder format state
media: mali-vpu: add V4L2 mem2mem and VB2 queue support
media: mali-vpu: implement format query ioctls
media: mali-vpu: implement TRY_FMT and S_FMT ioctls
media: mali-vpu: register V76 decoder controls and introduce UAPI
controls
media: mali-vpu: add firmware cache infrastructure
media: mali-vpu: load firmware for decoder sessions
media: mali-vpu: prepare firmware address space
media: mali-vpu: add firmware message transport
media: mali-vpu: route decoder controls to firmware
media: mali-vpu: handle firmware RPC requests
media: mali-vpu: add LSID hardware support
media: mali-vpu: add single session scheduler
media: mali-vpu: handle firmware interrupts
media: mali-vpu: start decoder firmware through LSID scheduling
media: mali-vpu: propagate buffer metadata
media: mali-vpu: handle firmware event responses
media: MAINTAINERS: add Arm Mali-V76 VPU driver

Rath Sankaralingam (4):
media: mali-vpu: add MMU software page table manager
media: mali-vpu: implement vb2 buffer queue handling
media: mali-vpu: handle firmware buffer exchange
media: mali-vpu: handle fatal firmware error

Shobhit Pandey (3):
media: mali-vpu: add decoder EOS and DRC handling
media: mali-vpu: handle firmware buffer parameters
media: mali-vpu: add stateful decoder state machine

 .../bindings/media/arm,mali-v76.yaml          |   70 +
 MAINTAINERS                                   |   11 +
 drivers/media/platform/arm/Kconfig            |    1 +
 drivers/media/platform/arm/Makefile           |    1 +
 drivers/media/platform/arm/mali-vpu/Kconfig   |   17 +
 drivers/media/platform/arm/mali-vpu/Makefile  |   18 +
 .../platform/arm/mali-vpu/mali-vpu-buffer.c   |  426 +++++
 .../platform/arm/mali-vpu/mali-vpu-buffer.h   |  109 ++
 .../platform/arm/mali-vpu/mali-vpu-controls.c |  351 ++++
 .../platform/arm/mali-vpu/mali-vpu-controls.h |   71 +
 .../platform/arm/mali-vpu/mali-vpu-dec.c      |  226 +++
 .../platform/arm/mali-vpu/mali-vpu-dec.h      |   27 +
 .../platform/arm/mali-vpu/mali-vpu-device.h   |   72 +
 .../arm/mali-vpu/mali-vpu-firmware-cache.c    |  549 ++++++
 .../arm/mali-vpu/mali-vpu-firmware-cache.h    |  180 ++
 .../platform/arm/mali-vpu/mali-vpu-firmware.c | 1647 +++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-firmware.h |  324 ++++
 .../platform/arm/mali-vpu/mali-vpu-formats.c  |   95 +
 .../platform/arm/mali-vpu/mali-vpu-formats.h  |   39 +
 .../platform/arm/mali-vpu/mali-vpu-lsid.c     |  271 +++
 .../platform/arm/mali-vpu/mali-vpu-lsid.h     |   58 +
 .../platform/arm/mali-vpu/mali-vpu-mmu.c      |  699 +++++++
 .../platform/arm/mali-vpu/mali-vpu-mmu.h      |  154 ++
 .../platform/arm/mali-vpu/mali-vpu-probe.c    |  387 ++++
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   |  710 +++++++
 .../arm/mali-vpu/mali-vpu-registers.h         |   81 +
 .../arm/mali-vpu/mali-vpu-scheduler.c         |  248 +++
 .../arm/mali-vpu/mali-vpu-scheduler.h         |   58 +
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 1260 +++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-session.h  |  174 ++
 .../platform/arm/mali-vpu/mali-vpu-state.h    |  136 ++
 .../arm/mali-vpu/mali-vpu-variant-v76.c       |  188 ++
 .../platform/arm/mali-vpu/mali-vpu-variant.h  |   74 +
 .../platform/arm/mali-vpu/mali-vpu-vb2.c      |  187 ++
 .../platform/arm/mali-vpu/mali-vpu-vb2.h      |   13 +
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   |  365 ++++
 .../platform/arm/mali-vpu/mali-vpu-vidioc.h   |   17 +
 .../uapi/linux/media/arm/mali-vpu-config.h    |   47 +
 include/uapi/linux/v4l2-controls.h            |    6 +
 39 files changed, 9367 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/arm,mali-v76.yaml
 create mode 100644 drivers/media/platform/arm/mali-vpu/Kconfig
 create mode 100644 drivers/media/platform/arm/mali-vpu/Makefile
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-controls.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-controls.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-dec.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-device.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-formats.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-lsid.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-lsid.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-registers.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-scheduler.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-scheduler.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-session.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-session.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-state.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-vb2.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-vb2.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.h
 create mode 100644 include/uapi/linux/media/arm/mali-vpu-config.h
Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH 1/28 dt-bindings: media: add Arm Mali-V76 VPU

Add devicetree binding for the Arm Mali-V76 video processing unit, a hardware accelerator for video encoding and decoding.

---
 .../bindings/media/arm,mali-v76.yaml          | 70 +++++++++++++++++++
 1 file changed, 70 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/arm,mali-v76.yaml
Documentation/devicetree/bindings/media/arm,mali-v76.yaml new file +70 -0
new file mode 100644
index 0000000000000..2ac4934ddaf4d
@@ -0,0 +1,70 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/arm,mali-v76.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: ARM Mali-V76 Video Processing Unit
+
+maintainers:
+  - Charvi Mehta <[email protected]>
+
+description:
+  The ARM Mali-V76 VPU is a hardware accelerator for video encoding and
+  decoding present on ARM platforms.
+
+properties:
+  compatible:
+    const: arm,mali-v76
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: VPU core clock
+
+  clock-names:
+    items:
+      - const: core
+
+  resets:
+    description:
+      Optional VPU warm reset control. Platforms without this control must
+      ensure that the VPU is released from external reset before the driver
+      accesses it. The driver performs initialization and recovery resets
+      through the VPU's software reset register.
+    items:
+      - description: VPU warm reset
+
+  reset-names:
+    items:
+      - const: warm
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - clock-names
+
+dependencies:
+  resets: [ reset-names ]
+  reset-names: [ resets ]
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+
+    mali_v76: video-codec@6f030000 {
+        compatible = "arm,mali-v76";
+        reg = <0x6f030000 0x300>;
+        interrupts = <GIC_SPI 168 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&clock 0>;
+        clock-names = "core";
+    };
Signature · 1 line
2.43.0
#2 lore raw

RFC PATCH 2/28 media: mali-vpu: add Mali VPU platform driver

In reply to Charvi Mehta

In preparation for adding H.264 video decode support, add the platform driver for Arm Mali-VPU.

Add probe and remove operations to acquire and manage the MMIO region, interrupt, clocks and reset controls. Add the variant data for Mali-V76 and validate the hardware product ID during probe.

Restrict the driver to little-endian kernels, as the firmware interface uses little-endian shared memory structures accessed in native-byte order.

---
 drivers/media/platform/arm/Kconfig            |   1 +
 drivers/media/platform/arm/Makefile           |   1 +
 drivers/media/platform/arm/mali-vpu/Kconfig   |  14 ++
 drivers/media/platform/arm/mali-vpu/Makefile  |   6 +
 .../platform/arm/mali-vpu/mali-vpu-device.h   |  38 ++++
 .../platform/arm/mali-vpu/mali-vpu-probe.c    | 215 ++++++++++++++++++
 .../arm/mali-vpu/mali-vpu-registers.h         |  24 ++
 .../arm/mali-vpu/mali-vpu-variant-v76.c       |  21 ++
 .../platform/arm/mali-vpu/mali-vpu-variant.h  |  25 ++
 9 files changed, 345 insertions(+)
 create mode 100644 drivers/media/platform/arm/mali-vpu/Kconfig
 create mode 100644 drivers/media/platform/arm/mali-vpu/Makefile
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-device.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-registers.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h
drivers/media/platform/arm/Kconfig +1 -0
index 4f0764c329c74..339969d0ef4ce 100644
@@ -3,3 +3,4 @@
 comment "ARM media platform drivers"
 
 source "drivers/media/platform/arm/mali-c55/Kconfig"
+source "drivers/media/platform/arm/mali-vpu/Kconfig"
drivers/media/platform/arm/Makefile +1 -0
index 8cc4918725efa..3c38ad267e59a 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y += mali-c55/
+obj-y += mali-vpu/
drivers/media/platform/arm/mali-vpu/Kconfig new file +14 -0
new file mode 100644
index 0000000000000..86403d7f424f9
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config VIDEO_MALI_VPU
+	tristate "ARM Mali VPU driver"
+	depends on VIDEO_DEV
+	depends on !CPU_BIG_ENDIAN
+	help
+	  Enable this to support Arm's Mali Video hardware accelerator.
+	  This is a V4L2 driver.
+
+	  Only little-endian kernels are supported. The firmware interface
+	  uses little-endian shared-memory structures.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called mali-vpu.
drivers/media/platform/arm/mali-vpu/Makefile new file +6 -0
new file mode 100644
index 0000000000000..22acf82e99a88
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+mali-vpu-objs := mali-vpu-probe.o \
+		mali-vpu-variant-v76.o
+
+obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-device.h new file +38 -0
new file mode 100644
index 0000000000000..04160090e64df
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_DEVICE_H__
+#define __MALI_VPU_DEVICE_H__
+
+#include <linux/device.h>
+
+struct clk_bulk_data;
+struct mali_vpu_variant;
+struct reset_control_bulk_data;
+
+/**
+ * struct mali_vpu_dev - Mali VPU device
+ * @dev: Reference to device structure.
+ * @regs_base: MMIO base address for VPU registers.
+ * @clk_list: List of VPU clocks.
+ * @clk_count: Number of clocks in @clk_list.
+ * @rst_list: List of reset controls.
+ * @rst_count: Number of reset controls in @rst_list.
+ * @irq: IRQ number for VPU.
+ * @variant: Structure describing variant-specific features and capabilities.
+ */
+struct mali_vpu_dev {
+	struct device			*dev;
+	void __iomem			*regs_base;
+	struct clk_bulk_data		*clk_list;
+	unsigned int			clk_count;
+	struct reset_control_bulk_data	*rst_list;
+	unsigned int			rst_count;
+	int				irq;
+
+	const struct mali_vpu_variant	*variant;
+};
+
+#endif /* __MALI_VPU_DEVICE_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +215 -0

215 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-registers.h new file +24 -0
new file mode 100644
index 0000000000000..471a1f2412a7a
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_REGISTERS_H__
+#define __MALI_VPU_REGISTERS_H__
+
+#include <linux/bits.h>
+
+/*
+ * Mali VPU register offsets and bitfield definitions.
+ *
+ * Offsets are relative to the VPU MMIO base.
+ */
+#define MALI_VPU_REG_VERSION 0x00000
+
+/* Version register fields */
+#define MALI_VPU_VERSION_PRODUCT_ID_MASK GENMASK(31, 16)
+#define MALI_VPU_VERSION_REVISION_MASK   GENMASK(15, 8)
+#define MALI_VPU_VERSION_PATCH_MASK      GENMASK(7, 0)
+#define MALI_VPU_PID_SUFFIX_MASK         GENMASK(15, 12)
+
+#endif /* __MALI_VPU_REGISTERS_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c new file +21 -0
new file mode 100644
index 0000000000000..36509d6a1c5d5
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#include <linux/array_size.h>
+
+#include "mali-vpu-variant.h"
+
+#define MALI_VPU_HW_PID_V76 0x56627
+
+static const char * const mali_v76_rst_names[] = {
+	"warm",
+};
+
+const struct mali_vpu_variant mali_vpu_v76_variant = {
+	.expected_hw_pid = MALI_VPU_HW_PID_V76,
+
+	.rst_names = mali_v76_rst_names,
+	.num_rsts = ARRAY_SIZE(mali_v76_rst_names),
+};
drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h new file +25 -0
new file mode 100644
index 0000000000000..dadfd686275db
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_VARIANT_H__
+#define __MALI_VPU_VARIANT_H__
+
+#include <linux/types.h>
+
+/**
+ * struct mali_vpu_variant - Variant-specific hardware capability descriptor.
+ * @rst_names: Array of reset control names for this variant.
+ * @num_rsts: Number of reset controls for this variant.
+ * @expected_hw_pid: Expected hardware product ID read from the version register.
+ */
+struct mali_vpu_variant {
+	const char * const *rst_names;
+	unsigned int num_rsts;
+	u32 expected_hw_pid;
+};
+
+extern const struct mali_vpu_variant mali_vpu_v76_variant;
+
+#endif /* __MALI_VPU_VARIANT_H__ */
Signature · 1 line
2.43.0
#3 lore raw

Re: [RFC PATCH 02/28] media: mali-vpu: add Mali VPU platform driver

Hi,

Le jeudi 19 novembre 2026 à 15:37 +0000, Charvi Mehta a écrit :

Quoted 145 lines

In preparation for adding H.264 video decode support, add the platform driver for Arm Mali-VPU.

Add probe and remove operations to acquire and manage the MMIO region, interrupt, clocks and reset controls. Add the variant data for Mali-V76 and validate the hardware product ID during probe.

Restrict the driver to little-endian kernels, as the firmware interface uses little-endian shared memory structures accessed in native-byte order.

---
 drivers/media/platform/arm/Kconfig            |   1 +
 drivers/media/platform/arm/Makefile           |   1 +
 drivers/media/platform/arm/mali-vpu/Kconfig   |  14 ++
 drivers/media/platform/arm/mali-vpu/Makefile  |   6 +
 .../platform/arm/mali-vpu/mali-vpu-device.h   |  38 ++++
 .../platform/arm/mali-vpu/mali-vpu-probe.c    | 215 ++++++++++++++++++
 .../arm/mali-vpu/mali-vpu-registers.h         |  24 ++
 .../arm/mali-vpu/mali-vpu-variant-v76.c       |  21 ++
 .../platform/arm/mali-vpu/mali-vpu-variant.h  |  25 ++
 9 files changed, 345 insertions(+)
 create mode 100644 drivers/media/platform/arm/mali-vpu/Kconfig
 create mode 100644 drivers/media/platform/arm/mali-vpu/Makefile
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-device.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-registers.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h
drivers/media/platform/arm/Kconfig +1 -0
index 4f0764c329c74..339969d0ef4ce 100644
@@ -3,3 +3,4 @@
 comment "ARM media platform drivers"
 
 source "drivers/media/platform/arm/mali-c55/Kconfig"
+source "drivers/media/platform/arm/mali-vpu/Kconfig"
drivers/media/platform/arm/Makefile +1 -0
index 8cc4918725efa..3c38ad267e59a 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y += mali-c55/
+obj-y += mali-vpu/
drivers/media/platform/arm/mali-vpu/Kconfig new file +14 -0
new file mode 100644
index 0000000000000..86403d7f424f9
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config VIDEO_MALI_VPU
+	tristate "ARM Mali VPU driver"
+	depends on VIDEO_DEV
+	depends on !CPU_BIG_ENDIAN
+	help
+	  Enable this to support Arm's Mali Video hardware accelerator.
+	  This is a V4L2 driver.
+
+	  Only little-endian kernels are supported. The firmware interface
+	  uses little-endian shared-memory structures.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called mali-vpu.
drivers/media/platform/arm/mali-vpu/Makefile new file +6 -0
new file mode 100644
index 0000000000000..22acf82e99a88
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+mali-vpu-objs := mali-vpu-probe.o \
+		mali-vpu-variant-v76.o
+
+obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-device.h new file +38 -0
new file mode 100644
index 0000000000000..04160090e64df
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_DEVICE_H__
+#define __MALI_VPU_DEVICE_H__
+
+#include <linux/device.h>
+
+struct clk_bulk_data;
+struct mali_vpu_variant;
+struct reset_control_bulk_data;
+
+/**
+ * struct mali_vpu_dev - Mali VPU device
+ * @dev: Reference to device structure.
+ * @regs_base: MMIO base address for VPU registers.
+ * @clk_list: List of VPU clocks.
+ * @clk_count: Number of clocks in @clk_list.
+ * @rst_list: List of reset controls.
+ * @rst_count: Number of reset controls in @rst_list.
+ * @irq: IRQ number for VPU.
+ * @variant: Structure describing variant-specific features and capabilities.
+ */
+struct mali_vpu_dev {
+	struct device			*dev;
+	void __iomem			*regs_base;
+	struct clk_bulk_data		*clk_list;
+	unsigned int			clk_count;
+	struct reset_control_bulk_data	*rst_list;
+	unsigned int			rst_count;
+	int				irq;
+
+	const struct mali_vpu_variant	*variant;
+};
+
+#endif /* __MALI_VPU_DEVICE_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +16 -0
new file mode 100644
index 0000000000000..49ecd6ac4554c
@@ -0,0 +1,215 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/reset.h>

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 26

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +10 -0
+#include <linux/slab.h>
+
+#include "mali-vpu-device.h"
+#include "mali-vpu-registers.h"
+#include "mali-vpu-variant.h"
+
+/* VPU register access helpers */
+static u32 mali_vpu_read_reg(void __iomem *regs_base, u32 off)
+{
+	return readl(regs_base + off);

That wrapper does nothing other then increasing the code length. Why not take a vpu_dev as parameter?

You're chosen namespace is also very long, worth considering, but not strictly a problem.

Quoted 16 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +16 -0
+}
+
+static void mali_vpu_read_version(struct mali_vpu_dev *vpu_dev, u32 *product_id, u32 *rev,
+				  u32 *patch)
+{
+	u32 pid_prefix, pid_suffix;
+
+	u32 hwver = mali_vpu_read_reg(vpu_dev->regs_base, MALI_VPU_REG_VERSION);
+
+	pid_prefix = FIELD_GET(MALI_VPU_VERSION_PRODUCT_ID_MASK, hwver);
+	pid_suffix = FIELD_GET(MALI_VPU_PID_SUFFIX_MASK, hwver);
+
+	*product_id	= (pid_prefix << 4) | pid_suffix;
+	*rev		= FIELD_GET(MALI_VPU_VERSION_REVISION_MASK, hwver);
+	*patch		= FIELD_GET(MALI_VPU_VERSION_PATCH_MASK, hwver);
+}

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 52

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +10 -0
+
+static int mali_vpu_get_clocks(struct mali_vpu_dev *vpu_dev)
+{
+	int ret;
+
+	ret = devm_clk_bulk_get_all(vpu_dev->dev, &vpu_dev->clk_list);
+	if (ret < 0)
+		return dev_err_probe(vpu_dev->dev, ret, "failed to get clocks\n");
+	if (!ret)
+		return dev_err_probe(vpu_dev->dev, -EINVAL, "no clocks found\n");

I don't think the second check is required, that would mean you have zero clock in your DT, which is invalid according to your binding.

Quoted 31 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +31 -0
+
+	vpu_dev->clk_count = ret;
+
+	return 0;
+}
+
+static int mali_vpu_get_resets(struct mali_vpu_dev *vpu_dev)
+{
+	unsigned int i;
+	int ret;
+
+	vpu_dev->rst_count = vpu_dev->variant->num_rsts;
+	if (vpu_dev->rst_count == 0)
+		return 0;
+
+	vpu_dev->rst_list = devm_kcalloc(vpu_dev->dev, vpu_dev->rst_count,
+					 sizeof(*vpu_dev->rst_list), GFP_KERNEL);
+	if (!vpu_dev->rst_list)
+		return -ENOMEM;
+
+	for (i = 0; i < vpu_dev->rst_count; i++)
+		vpu_dev->rst_list[i].id = vpu_dev->variant->rst_names[i];
+
+	ret = devm_reset_control_bulk_get_optional_exclusive(vpu_dev->dev,
+							     vpu_dev->rst_count,
+							     vpu_dev->rst_list);
+	if (ret)
+		return dev_err_probe(vpu_dev->dev, ret, "failed to get reset controls\n");
+
+	return 0;
+}

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 93

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +10 -0
+
+static int mali_vpu_enable_clk(struct mali_vpu_dev *vpu_dev)
+{
+	return clk_bulk_prepare_enable(vpu_dev->clk_count, vpu_dev->clk_list);
+}
+
+static void mali_vpu_disable_clk(struct mali_vpu_dev *vpu_dev)
+{
+	clk_bulk_disable_unprepare(vpu_dev->clk_count, vpu_dev->clk_list);
+}

The two wrapper above do nothing.

Quoted 57 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +57 -0
+
+static void mali_vpu_assert_reset(struct mali_vpu_dev *vpu_dev)
+{
+	if (!vpu_dev->rst_count)
+		return;
+
+	reset_control_bulk_assert(vpu_dev->rst_count, vpu_dev->rst_list);
+}
+
+static int mali_vpu_deassert_reset(struct mali_vpu_dev *vpu_dev)
+{
+	if (!vpu_dev->rst_count)
+		return 0;
+
+	return reset_control_bulk_deassert(vpu_dev->rst_count, vpu_dev->rst_list);
+}
+
+static int mali_vpu_get_resources(struct mali_vpu_dev *vpu_dev)
+{
+	int ret;
+
+	ret = mali_vpu_get_clocks(vpu_dev);
+	if (ret)
+		return ret;
+
+	return mali_vpu_get_resets(vpu_dev);
+}
+
+static int mali_vpu_probe(struct platform_device *pdev)
+{
+	struct mali_vpu_dev *vpu_dev;
+	u32 pid, rev, patch;
+	int ret;
+
+	vpu_dev = devm_kzalloc(&pdev->dev, sizeof(*vpu_dev), GFP_KERNEL);
+	if (!vpu_dev)
+		return -ENOMEM;
+	vpu_dev->dev = &pdev->dev;
+
+	vpu_dev->variant = of_device_get_match_data(&pdev->dev);
+	if (!vpu_dev->variant)
+		return -ENODEV;
+
+	vpu_dev->regs_base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(vpu_dev->regs_base))
+		return PTR_ERR(vpu_dev->regs_base);
+
+	ret = platform_get_irq(pdev, 0);
+	if (ret < 0)
+		return ret;
+	vpu_dev->irq = ret;
+
+	ret = mali_vpu_get_resources(vpu_dev);
+	if (ret)
+		return ret;
+
+	ret = mali_vpu_enable_clk(vpu_dev);

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 160

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +10 -0
+	if (ret) {
+		dev_err(vpu_dev->dev, "failed to enable core clock\n");
+		return ret;
+	}
+
+	ret = mali_vpu_deassert_reset(vpu_dev);
+	if (ret) {
+		dev_err(vpu_dev->dev, "failed to deassert reset\n");
+		goto err_clk;
+	}

In practice the two above should be inside your PM runtime ops, not sure if that's coming after, in which case just ignore. It best to do this early, PM as after thought would be bad.

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 171

Quoted 11 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +11 -0
+
+	mali_vpu_read_version(vpu_dev, &pid, &rev, &patch);
+	dev_info(&pdev->dev, "Mali VPU device product ID: %#x, revision: %#x, patch: %#x\n",
+		 pid, rev, patch);
+
+	if (pid != vpu_dev->variant->expected_hw_pid) {
+		dev_err(vpu_dev->dev, "unexpected hardware product ID: %#x (expected %#x)\n",
+			pid, vpu_dev->variant->expected_hw_pid);
+		ret = -ENODEV;
+		goto err_rst_clk;
+	}

Both ways work, but you could also pick the variant based on the version instead, and only depend on DT for the "families" (e.g. the version register layout have changed but you still want this under the same driver).

Quoted 124 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +44 -0
+
+	platform_set_drvdata(pdev, vpu_dev);
+
+	return 0;
+
+err_rst_clk:
+	mali_vpu_assert_reset(vpu_dev);
+err_clk:
+	mali_vpu_disable_clk(vpu_dev);
+
+	return ret;
+}
+
+static void mali_vpu_remove(struct platform_device *pdev)
+{
+	struct mali_vpu_dev *vpu_dev = platform_get_drvdata(pdev);
+
+	mali_vpu_assert_reset(vpu_dev);
+	mali_vpu_disable_clk(vpu_dev);
+
+	platform_set_drvdata(pdev, NULL);
+}
+
+static const struct of_device_id mali_vpu_dt_match[] = {
+	{
+		.compatible = "arm,mali-v76",
+		.data = &mali_vpu_v76_variant,
+	},
+	{},
+};
+MODULE_DEVICE_TABLE(of, mali_vpu_dt_match);
+
+static struct platform_driver mali_vpu_driver = {
+	.probe = mali_vpu_probe,
+	.remove = mali_vpu_remove,
+	.driver = {
+		.name = "mali-vpu",
+		.of_match_table = mali_vpu_dt_match,
+	},
+};
+
+module_platform_driver(mali_vpu_driver);
+MODULE_DESCRIPTION("Arm Mali VPU driver");
+MODULE_LICENSE("GPL");
drivers/media/platform/arm/mali-vpu/mali-vpu-registers.h new file +24 -0
new file mode 100644
index 0000000000000..471a1f2412a7a
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_REGISTERS_H__
+#define __MALI_VPU_REGISTERS_H__
+
+#include <linux/bits.h>
+
+/*
+ * Mali VPU register offsets and bitfield definitions.
+ *
+ * Offsets are relative to the VPU MMIO base.
+ */
+#define MALI_VPU_REG_VERSION 0x00000
+
+/* Version register fields */
+#define MALI_VPU_VERSION_PRODUCT_ID_MASK GENMASK(31, 16)
+#define MALI_VPU_VERSION_REVISION_MASK   GENMASK(15, 8)
+#define MALI_VPU_VERSION_PATCH_MASK      GENMASK(7, 0)
+#define MALI_VPU_PID_SUFFIX_MASK         GENMASK(15, 12)
+
+#endif /* __MALI_VPU_REGISTERS_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c new file +21 -0
new file mode 100644
index 0000000000000..36509d6a1c5d5
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#include <linux/array_size.h>
+
+#include "mali-vpu-variant.h"
+
+#define MALI_VPU_HW_PID_V76 0x56627
+
+static const char * const mali_v76_rst_names[] = {
+	"warm",
+};
+
+const struct mali_vpu_variant mali_vpu_v76_variant = {
+	.expected_hw_pid = MALI_VPU_HW_PID_V76,
+
+	.rst_names = mali_v76_rst_names,
+	.num_rsts = ARRAY_SIZE(mali_v76_rst_names),
+};
drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h new file +15 -0
new file mode 100644
index 0000000000000..dadfd686275db
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_VARIANT_H__
+#define __MALI_VPU_VARIANT_H__
+
+#include <linux/types.h>
+
+/**
+ * struct mali_vpu_variant - Variant-specific hardware capability descriptor.
+ * @rst_names: Array of reset control names for this variant.
+ * @num_rsts: Number of reset controls for this variant.
+ * @expected_hw_pid: Expected hardware product ID read from the version register.

drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h line 25

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h new file +10 -0
+ */
+struct mali_vpu_variant {
+	const char * const *rst_names;
+	unsigned int num_rsts;
+	u32 expected_hw_pid;
+};
+
+extern const struct mali_vpu_variant mali_vpu_v76_variant;
+
+#endif /* __MALI_VPU_VARIANT_H__ */

Nicolas

#4 lore raw

Re: [RFC PATCH 02/28] media: mali-vpu: add Mali VPU platform driver

Quoted 7 lines · Nicolas Dufresne

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 26

Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +5 -0
+
+/* VPU register access helpers */
+static u32 mali_vpu_read_reg(void __iomem *regs_base, u32 off)
+{
+	return readl(regs_base + off);

That wrapper does nothing other then increasing the code length. Why not take a vpu_dev as parameter?

Passing the MMIO base keeps the helpers independent of the device structure, but taking struct mali_vpu_dev * would simplify the callers. I’ll make that change in v2.

Quoted 2 lines

You're chosen namespace is also very long, worth considering, but not strictly a problem.

I'd prefer to keep the mali_vpu_ prefix for consistency with the driver name and rest of the series.

Quoted 11 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 52

Quoted 9 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +9 -0
+static int mali_vpu_get_clocks(struct mali_vpu_dev *vpu_dev)
+{
+	int ret;
+
+	ret = devm_clk_bulk_get_all(vpu_dev->dev, &vpu_dev->clk_list);
+	if (ret < 0)
+		return dev_err_probe(vpu_dev->dev, ret, "failed to get clocks\n");
+	if (!ret)
+		return dev_err_probe(vpu_dev->dev, -EINVAL, "no clocks found\n");

I don't think the second check is required, that would mean you have zero clock in your DT, which is invalid according to your binding.

The VPU requires a core clock, but devm_clk_bulk_get_all() returns zero when none are found. Although this violates the binding, schema validation is not enabled by default or performed at runtime, and the bootloader can supply a separate DTB. I’d prefer to retain the check so probe reports the missing clock before attempting register access.

Quoted 10 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 93

Quoted 9 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +9 -0
+static int mali_vpu_enable_clk(struct mali_vpu_dev *vpu_dev)
+{
+	return clk_bulk_prepare_enable(vpu_dev->clk_count, vpu_dev->clk_list);
+}
+
+static void mali_vpu_disable_clk(struct mali_vpu_dev *vpu_dev)
+{
+	clk_bulk_disable_unprepare(vpu_dev->clk_count, vpu_dev->clk_list);
+}

The two wrapper above do nothing.

Fair point. I’ll remove them and call the bulk clock APIs directly.

Quoted 10 lines
Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +10 -0
+	ret = mali_vpu_get_resources(vpu_dev);
+	if (ret)
+		return ret;
+
+	ret = mali_vpu_enable_clk(vpu_dev);
+	if (ret) {
+		dev_err(vpu_dev->dev, "failed to enable core clock\n");
+		return ret;
+	}
+
Quoted 8 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 160

Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +5 -0
+	ret = mali_vpu_deassert_reset(vpu_dev);
+	if (ret) {
+		dev_err(vpu_dev->dev, "failed to deassert reset\n");
+		goto err_clk;
+	}

In practice the two above should be inside your PM runtime ops, not sure if that's coming after, in which case just ignore. It best to do this early, PM as after thought would be bad.

Thanks, runtime PM support will follow in subsequent patches.

Quoted 5 lines
Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +5 -0
+	mali_vpu_read_version(vpu_dev, &pid, &rev, &patch);
+	dev_info(&pdev->dev, "Mali VPU device product ID: %#x, revision: %#x, patch: %#x\n",
+		 pid, rev, patch);
+
+	if (pid != vpu_dev->variant->expected_hw_pid) {
Quoted 8 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 171

Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +5 -0
+		dev_err(vpu_dev->dev, "unexpected hardware product ID: %#x (expected %#x)\n",
+			pid, vpu_dev->variant->expected_hw_pid);
+		ret = -ENODEV;
+		goto err_rst_clk;
+	}

Both ways work, but you could also pick the variant based on the version instead, and only depend on DT for the "families" (e.g. the version register layout have changed but you still want this under the same driver).

Thanks for the suggestion. For V76, I prefer to retain the DT-selected variant and use the product ID as a sanity check.
I'll revisit product-ID-based variant selection when another variant is added.

Regards,
Charvi

#5 lore raw

Re: [RFC PATCH 02/28] media: mali-vpu: add Mali VPU platform driver

In reply to Charvi Mehta

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 12

Quoted 7 lines · Charvi Mehta
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +7 -0
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+#include <linux/mod_devicetable.h>

Please don't include <linux/mod_devicetable.h>.

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 15

Quoted 3 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +3 -0
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>

<linux/platform_device.h> already provides of_device_id, so <linux/mod_devicetable.h> can just be dropped.

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 200

Quoted 7 lines

[...]

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +6 -0
+static const struct of_device_id mali_vpu_dt_match[] = {
+	{
+		.compatible = "arm,mali-v76",
+		.data = &mali_vpu_v76_variant,
+	},
+	{},

s/{},/{ }/

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 202

Quoted 2 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +2 -0
+};
+MODULE_DEVICE_TABLE(of, mali_vpu_dt_match);

Best regards
Uwe

#6 lore raw

Re: [RFC PATCH 02/28] media: mali-vpu: add Mali VPU platform driver

Quoted 9 lines · Uwe Kleine-König

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 12

Quoted 7 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +7 -0
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+#include <linux/mod_devicetable.h>

Please don't include <linux/mod_devicetable.h>.

Quoted 5 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 15

Quoted 3 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +3 -0
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>

<linux/platform_device.h> already provides of_device_id, so <linux/mod_devicetable.h> can just be dropped.

Thanks, I’ll drop the redundant include.

Quoted 8 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 200

Quoted 7 lines

[...]

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +6 -0
+static const struct of_device_id mali_vpu_dt_match[] = {
+	{
+		.compatible = "arm,mali-v76",
+		.data = &mali_vpu_v76_variant,
+	},
+	{},

s/{},/{ }/

I’ll add the space, keeping the trailing comma for consistency with the other initializers in the driver.

Regards,
Charvi

#7 lore raw

Re: [RFC PATCH 02/28] media: mali-vpu: add Mali VPU platform driver

Quoted 27 lines · Charvi Mehta
Quoted 9 lines · Uwe Kleine-König

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 12

Quoted 7 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +7 -0
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/io.h>
+#include <linux/mod_devicetable.h>

Please don't include <linux/mod_devicetable.h>.

Quoted 5 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 15

Quoted 3 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +3 -0
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>

<linux/platform_device.h> already provides of_device_id, so <linux/mod_devicetable.h> can just be dropped.

Thanks, I’ll drop the redundant include.

Quoted 8 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 200

Quoted 7 lines

[...]

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +6 -0
+static const struct of_device_id mali_vpu_dt_match[] = {
+	{
+		.compatible = "arm,mali-v76",
+		.data = &mali_vpu_v76_variant,
+	},
+	{},

s/{},/{ }/

Quoted 2 lines

I’ll add the space, keeping the trailing comma for consistency with the other initializers in the driver.

Which driver? With your full series applied this is the only list terminator below drivers/media/platform/arm. Note this is different from

static struct platform_driver mali_vpu_driver = {
.probe = mali_vpu_probe,
.remove = mali_vpu_remove,
.driver = {
.name = MALI_VPU_DRV_NAME, .of_match_table = mali_vpu_dt_match,
},
};

where the trailing , is right. My request to drop it only applies to arrays where the empty initializer signals the array's end.

I don't find that documented in the coding style description, but with next-20261123 checked out:

	$ git ls-files drivers | xargs -n 1 perl -g -n -e 'print "$1\n" if m/device_id\s*[a-zA-Z_0-9]*\[\]\s*=\s*{(?:\s*{[^}]*},\s*({\s*(?:\s\/\*[^*]*\*\/)?},?))/' | sort | uniq -c
	      1 	}
	      1 {
	      6 {  }
	   1957 { }
	    405 { },
	    490 {}
	    576 {},
	      1 { /* end */},
	      2 { /* sentinel */}
	      1 { /* sentinel */},

So arrays of type \*device_id[] typically end with the terminator having no trailing comma. (But honestly I'm surprised about nearly 1000 trailing commas, I would have expected that to be much less common, and drivers/media/platform even has more terminators with a trailing comma than without. I'm embarrassed.)

Best regards
Uwe

#8 lore raw

Re: [RFC PATCH 02/28] media: mali-vpu: add Mali VPU platform driver

Quoted 41 lines · Uwe Kleine-König
Quoted 10 lines
Quoted 8 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c line 200

Quoted 7 lines

[...]

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c new file +6 -0
+static const struct of_device_id mali_vpu_dt_match[] = {
+	{
+		.compatible = "arm,mali-v76",
+		.data = &mali_vpu_v76_variant,
+	},
+	{},

s/{},/{ }/

I’ll add the space, keeping the trailing comma for consistency with the other initializers in the driver.

Which driver? With your full series applied this is the only list terminator below drivers/media/platform/arm. Note this is different from

static struct platform_driver mali_vpu_driver = {
.probe = mali_vpu_probe,
.remove = mali_vpu_remove,
.driver = {
.name = MALI_VPU_DRV_NAME, .of_match_table = mali_vpu_dt_match,
},
};

where the trailing , is right. My request to drop it only applies to arrays where the empty initializer signals the array's end.

I don't find that documented in the coding style description, but with next-20261123 checked out:

	$ git ls-files drivers | xargs -n 1 perl -g -n -e 'print "$1\n" if m/device_id\s*[a-zA-Z_0-9]*\[\]\s*=\s*{(?:\s*{[^}]*},\s*({\s*(?:\s\/\*[^*]*\*\/)?},?))/' | sort | uniq -c
	      1 	}
	      1 {
	      6 {  }
	   1957 { }
	    405 { },
	    490 {}
	    576 {},
	      1 { /* end */},
	      2 { /* sentinel */}
	      1 { /* sentinel */},
Quoted 5 lines

So arrays of type \*device_id[] typically end with the terminator having no trailing comma. (But honestly I'm surprised about nearly 1000 trailing commas, I would have expected that to be much less common, and drivers/media/platform even has more terminators with a trailing comma than without. I'm embarrassed.)

I’ll use |{ }|without the trailing comma for the match-table terminator in v2.

Regards,
Charvi

#9 lore raw

RFC PATCH 3/28 media: mali-vpu: register V4L2 decoder device and add querycap ioctl

In reply to Charvi Mehta

Register a V4L2 M2M decoder video device for the Mali VPU. Add open and release file operations that allocate a per-file session.

Implement VIDIOC_QUERYCAP to report the driver and decoder identity.

---
 drivers/media/platform/arm/mali-vpu/Kconfig   |  1 +
 drivers/media/platform/arm/mali-vpu/Makefile  |  3 +-
 .../platform/arm/mali-vpu/mali-vpu-device.h   | 15 ++++
 .../platform/arm/mali-vpu/mali-vpu-probe.c    | 59 +++++++++++++-
 .../platform/arm/mali-vpu/mali-vpu-session.h  | 26 +++++++
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   | 78 +++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-vidioc.h   | 15 ++++
 7 files changed, 195 insertions(+), 2 deletions(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-session.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.h
drivers/media/platform/arm/mali-vpu/Kconfig +1 -0
index 86403d7f424f9..d98872f2b7ec0 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 config VIDEO_MALI_VPU
 	tristate "ARM Mali VPU driver"
+	depends on V4L_MEM2MEM_DRIVERS
 	depends on VIDEO_DEV
 	depends on !CPU_BIG_ENDIAN
 	help
drivers/media/platform/arm/mali-vpu/Makefile +2 -1
index 22acf82e99a88..e659101bd0eac 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
 mali-vpu-objs := mali-vpu-probe.o \
-		mali-vpu-variant-v76.o
+		mali-vpu-variant-v76.o \
+		mali-vpu-vidioc.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-device.h +15 -0
index 04160090e64df..d9b0801cbb520 100644
@@ -7,11 +7,22 @@
 #define __MALI_VPU_DEVICE_H__
 
 #include <linux/device.h>
+#include <media/v4l2-device.h>
+
+#define MALI_VPU_DRV_NAME "mali-vpu"
 
 struct clk_bulk_data;
 struct mali_vpu_variant;
 struct reset_control_bulk_data;
 
+/**
+ * enum mali_vpu_session_role - Mali VPU session role
+ * @MALI_VPU_DECODER: Video decoding session
+ */
+enum mali_vpu_session_role {
+	MALI_VPU_DECODER = 0,
+};
+
 /**
  * struct mali_vpu_dev - Mali VPU device
  * @dev: Reference to device structure.
@@ -22,6 +33,8 @@ struct reset_control_bulk_data;
  * @rst_count: Number of reset controls in @rst_list.
  * @irq: IRQ number for VPU.
  * @variant: Structure describing variant-specific features and capabilities.
+ * @v4l2_dev: V4L2 device structure for mali vpu.
+ * @vdev_dec: Video device structure for decoder.
  */
 struct mali_vpu_dev {
 	struct device			*dev;
@@ -33,6 +46,8 @@ struct mali_vpu_dev {
 	int				irq;
 
 	const struct mali_vpu_variant	*variant;
+	struct v4l2_device		v4l2_dev;
+	struct video_device		*vdev_dec;
 };
 
 #endif /* __MALI_VPU_DEVICE_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c +58 -1

97 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h new file +26 -0
new file mode 100644
index 0000000000000..7166c53236f1a
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_SESSION_H__
+#define __MALI_VPU_SESSION_H__
+
+#include <media/v4l2-fh.h>
+
+#include "mali-vpu-device.h"
+
+/**
+ * struct mali_vpu_session - holds per session state and resources.
+ * @vpu_dev: Reference to the parent device structure.
+ * @role: Role of this session.
+ * @fh: V4L2 file handle for this session.
+ */
+struct mali_vpu_session {
+	struct mali_vpu_dev		*vpu_dev;
+	enum mali_vpu_session_role	role;
+
+	struct v4l2_fh		fh;
+};
+
+#endif /* __MALI_VPU_SESSION_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c new file +78 -0

78 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.h new file +15 -0
new file mode 100644
index 0000000000000..e1aedea5bbcb6
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_VIDIOC_H__
+#define __MALI_VPU_VIDIOC_H__
+
+struct v4l2_file_operations;
+struct v4l2_ioctl_ops;
+
+extern const struct v4l2_file_operations mali_vpu_fops;
+extern const struct v4l2_ioctl_ops mali_vpu_ioctl_ops_dec;
+
+#endif /* __MALI_VPU_VIDIOC_H__ */
Signature · 1 line
2.43.0
Reviewed-by on v1 3/28
#10 lore raw

Re: [RFC PATCH 03/28] media: mali-vpu: register V4L2 decoder device and add querycap ioctl

Le jeudi 19 novembre 2026 à 15:37 +0000, Charvi Mehta a écrit :

Quoted 6 lines

Register a V4L2 M2M decoder video device for the Mali VPU. Add open and release file operations that allocate a per-file session.

Implement VIDIOC_QUERYCAP to report the driver and decoder identity.

Quoted 8 lines
---
 drivers/media/platform/arm/mali-vpu/Kconfig   |  1 +
 drivers/media/platform/arm/mali-vpu/Makefile  |  3 +-
 .../platform/arm/mali-vpu/mali-vpu-device.h   | 15 ++++
 .../platform/arm/mali-vpu/mali-vpu-probe.c    | 59 +++++++++++++-
 .../platform/arm/mali-vpu/mali-vpu-session.h  | 26 +++++++
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   | 78 +++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-vidioc.h   | 15 ++++

nit: I'd say rename to mali-vpu-v4l2.c to be consistent with nearly all other drivers.

#11 lore raw

Re: [RFC PATCH 03/28] media: mali-vpu: register V4L2 decoder device and add querycap ioctl

Quoted 6 lines · Nicolas Dufresne
Quoted 2 lines
  .../platform/arm/mali-vpu/mali-vpu-vidioc.c   | 78 +++++++++++++++++++
  .../platform/arm/mali-vpu/mali-vpu-vidioc.h   | 15 ++++

nit: I'd say rename to mali-vpu-v4l2.c to be consistent with nearly all other drivers.

The file also contains the V4L2 file operations and mem2mem callbacks, so mali-vpu-v4l2.c would indeed better describe its scope. I’ll rename it in v2. Thanks for the review.

Regards,
Charvi

#12 lore raw

RFC PATCH 4/28 media: mali-vpu: initialize decoder format state

In reply to Charvi Mehta

Describe the H.264 OUTPUT format and YUV420M, NV12M and NV21M CAPTURE formats supported by Mali-V76, together with its frame-size limits.

Add source and destination format state to each decoder session and initialize both ports from variant defaults. Add the MVE format identifiers required by the format descriptors.

---
 drivers/media/platform/arm/mali-vpu/Makefile  |  4 +-
 .../platform/arm/mali-vpu/mali-vpu-dec.c      | 16 ++++
 .../platform/arm/mali-vpu/mali-vpu-dec.h      | 13 +++
 .../platform/arm/mali-vpu/mali-vpu-formats.h  | 27 ++++++
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   | 51 ++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 83 +++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-session.h  | 18 ++++
 .../arm/mali-vpu/mali-vpu-variant-v76.c       | 63 ++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-variant.h  | 30 +++++++
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   |  7 ++
 10 files changed, 311 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-dec.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-formats.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-session.c
drivers/media/platform/arm/mali-vpu/Makefile +3 -1
index e659101bd0eac..0a42c53ae5088 100644
@@ -2,6 +2,8 @@
 
 mali-vpu-objs := mali-vpu-probe.o \
 		mali-vpu-variant-v76.o \
-		mali-vpu-vidioc.o
+		mali-vpu-vidioc.o \
+		mali-vpu-session.o \
+		mali-vpu-dec.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c new file +16 -0
new file mode 100644
index 0000000000000..cb6eeee2f04df
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#include "mali-vpu-dec.h"
+#include "mali-vpu-session.h"
+#include "mali-vpu-variant.h"
+
+void mali_vpu_session_init_dec(struct mali_vpu_session *sess)
+{
+	const struct mali_vpu_format_caps *variant_fmts = &sess->vpu_dev->variant->fmt_caps;
+
+	sess->port_src.supported_fmts = variant_fmts->dec_src;
+	sess->port_dst.supported_fmts = variant_fmts->dec_dst;
+}
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.h new file +13 -0
new file mode 100644
index 0000000000000..2514c897587c4
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_DEC_H__
+#define __MALI_VPU_DEC_H__
+
+struct mali_vpu_session;
+
+void mali_vpu_session_init_dec(struct mali_vpu_session *sess);
+
+#endif /* __MALI_VPU_DEC_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.h new file +27 -0
new file mode 100644
index 0000000000000..13031809fdb4d
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_FORMATS_H__
+#define __MALI_VPU_FORMATS_H__
+
+#include <linux/types.h>
+
+enum mali_vpu_codec_fmt {
+	MALI_VPU_FORMAT_CODEC_H264,
+};
+
+struct mali_vpu_format_desc {
+	u32 fourcc;
+	u16 mve_frame_fmt;
+	u16 codec_fmt;
+	u32 flags;
+};
+
+struct mali_vpu_format_list {
+	const struct mali_vpu_format_desc *fmts;
+	u32 num_fmts;
+};
+
+#endif /* __MALI_VPU_FORMATS_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h new file +51 -0

51 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c new file +83 -0

83 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +18 -0
index 7166c53236f1a..a2778ca496381 100644
@@ -6,21 +6,39 @@
 #ifndef __MALI_VPU_SESSION_H__
 #define __MALI_VPU_SESSION_H__
 
+#include <linux/videodev2.h>
 #include <media/v4l2-fh.h>
 
 #include "mali-vpu-device.h"
+#include "mali-vpu-formats.h"
+
+/**
+ * struct mali_vpu_port - format state of a src/dst queue in a session.
+ * @curr_fmt: V4L2 format currently configured for the port
+ * @supported_fmts: List of format descriptors supported by the queue.
+ */
+struct mali_vpu_port {
+	struct v4l2_format		curr_fmt;
+	struct mali_vpu_format_list	supported_fmts;
+};
 
 /**
  * struct mali_vpu_session - holds per session state and resources.
  * @vpu_dev: Reference to the parent device structure.
  * @role: Role of this session.
+ * @port_src: Source (OUTPUT) port for this session.
+ * @port_dst: Destination (CAPTURE) port for this session.
  * @fh: V4L2 file handle for this session.
  */
 struct mali_vpu_session {
 	struct mali_vpu_dev		*vpu_dev;
 	enum mali_vpu_session_role	role;
+	struct mali_vpu_port		port_src;
+	struct mali_vpu_port		port_dst;
 
 	struct v4l2_fh		fh;
 };
 
+int mali_vpu_session_init(struct mali_vpu_session *sess);
+
 #endif /* __MALI_VPU_SESSION_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +63 -0

81 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h +30 -0
index dadfd686275db..a46c25a94f18b 100644
@@ -7,17 +7,47 @@
 #define __MALI_VPU_VARIANT_H__
 
 #include <linux/types.h>
+#include <linux/videodev2.h>
+
+#include "mali-vpu-formats.h"
+
+/**
+ * struct mali_vpu_framesize_caps - Variant-specific frame size capability.
+ * @stepwise: Stepwise frame size limits and increments supported by the hardware.
+ * @width_default: Default width used when userspace does not request one.
+ * @height_default: Default height used when userspace does not request one.
+ */
+struct mali_vpu_framesize_caps {
+	struct v4l2_frmsize_stepwise stepwise;
+	u32 width_default;
+	u32 height_default;
+};
+
+/**
+ * struct mali_vpu_format_caps - Pixel/coded format capability tables.
+ * @dec_src: List of formats supported on decoder OUTPUT queue (compressed bitstream in).
+ * @dec_dst: List of formats supported on decoder CAPTURE queue (decoded raw frames).
+ */
+struct mali_vpu_format_caps {
+	const struct mali_vpu_format_list dec_src;
+	const struct mali_vpu_format_list dec_dst;
+};
 
 /**
  * struct mali_vpu_variant - Variant-specific hardware capability descriptor.
  * @rst_names: Array of reset control names for this variant.
  * @num_rsts: Number of reset controls for this variant.
  * @expected_hw_pid: Expected hardware product ID read from the version register.
+ * @fmt_caps: Format capability tables for encode/decode paths.
+ * @frmsz_caps: Frame dimension limits and defaults.
  */
 struct mali_vpu_variant {
 	const char * const *rst_names;
 	unsigned int num_rsts;
 	u32 expected_hw_pid;
+
+	struct mali_vpu_format_caps	fmt_caps;
+	struct mali_vpu_framesize_caps	frmsz_caps;
 };
 
 extern const struct mali_vpu_variant mali_vpu_v76_variant;
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +7 -0
index 7a169b7b14bf3..13468c6baba76 100644
@@ -37,8 +37,15 @@ static int mali_vpu_open(struct file *filp)
 	v4l2_fh_init(&sess->fh, vdev);
 	v4l2_fh_add(&sess->fh, filp);
 
+	ret = mali_vpu_session_init(sess);
+	if (ret)
+		goto err_v4l2_fh_deinit;
+
 	return 0;
 
+err_v4l2_fh_deinit:
+	v4l2_fh_del(&sess->fh, filp);
+	v4l2_fh_exit(&sess->fh);
 err_free_sess:
 	kfree(sess);
 	return ret;
Signature · 1 line
2.43.0
#13 lore raw

Re: [RFC PATCH 04/28] media: mali-vpu: initialize decoder format state

Le jeudi 19 novembre 2026 à 15:37 +0000, Charvi Mehta a écrit :

Quoted 51 lines

Describe the H.264 OUTPUT format and YUV420M, NV12M and NV21M CAPTURE formats supported by Mali-V76, together with its frame-size limits.

Add source and destination format state to each decoder session and initialize both ports from variant defaults. Add the MVE format identifiers required by the format descriptors.

---
 drivers/media/platform/arm/mali-vpu/Makefile  |  4 +-
 .../platform/arm/mali-vpu/mali-vpu-dec.c      | 16 ++++
 .../platform/arm/mali-vpu/mali-vpu-dec.h      | 13 +++
 .../platform/arm/mali-vpu/mali-vpu-formats.h  | 27 ++++++
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   | 51 ++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 83 +++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-session.h  | 18 ++++
 .../arm/mali-vpu/mali-vpu-variant-v76.c       | 63 ++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-variant.h  | 30 +++++++
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   |  7 ++
 10 files changed, 311 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-dec.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-formats.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-session.c
drivers/media/platform/arm/mali-vpu/Makefile +3 -1
index e659101bd0eac..0a42c53ae5088 100644
@@ -2,6 +2,8 @@
 
 mali-vpu-objs := mali-vpu-probe.o \
 		mali-vpu-variant-v76.o \
-		mali-vpu-vidioc.o
+		mali-vpu-vidioc.o \
+		mali-vpu-session.o \
+		mali-vpu-dec.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c new file +5 -0
new file mode 100644
index 0000000000000..cb6eeee2f04df
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+

drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c line 15

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c new file +10 -0
+#include "mali-vpu-dec.h"
+#include "mali-vpu-session.h"
+#include "mali-vpu-variant.h"
+
+void mali_vpu_session_init_dec(struct mali_vpu_session *sess)
+{
+	const struct mali_vpu_format_caps *variant_fmts = &sess->vpu_dev->variant->fmt_caps;
+
+	sess->port_src.supported_fmts = variant_fmts->dec_src;
+	sess->port_dst.supported_fmts = variant_fmts->dec_dst;

Why? the variant are constant, you can just read the information from the variant. Make a getter or point to it.

p.s. is that "port" like in OMX IL?

Quoted 153 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c new file +1 -0
+}
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.h new file +13 -0
new file mode 100644
index 0000000000000..2514c897587c4
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_DEC_H__
+#define __MALI_VPU_DEC_H__
+
+struct mali_vpu_session;
+
+void mali_vpu_session_init_dec(struct mali_vpu_session *sess);
+
+#endif /* __MALI_VPU_DEC_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.h new file +27 -0
new file mode 100644
index 0000000000000..13031809fdb4d
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_FORMATS_H__
+#define __MALI_VPU_FORMATS_H__
+
+#include <linux/types.h>
+
+enum mali_vpu_codec_fmt {
+	MALI_VPU_FORMAT_CODEC_H264,
+};
+
+struct mali_vpu_format_desc {
+	u32 fourcc;
+	u16 mve_frame_fmt;
+	u16 codec_fmt;
+	u32 flags;
+};
+
+struct mali_vpu_format_list {
+	const struct mali_vpu_format_desc *fmts;
+	u32 num_fmts;
+};
+
+#endif /* __MALI_VPU_FORMATS_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h new file +51 -0
new file mode 100644
index 0000000000000..aed51eec6ca9c
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_PROTOCOL_DEF_V3_H__
+#define __MALI_VPU_PROTOCOL_DEF_V3_H__
+
+/*****************************************************************************
+ *
+ * Communication protocol between the host/driver and the MVE firmware,
+ * the 'host interface'.
+ *
+ * MVE == MALI Video Engine
+ *
+ * Protocol version 3.1
+ *
+ * Note: Message structs may be expanded in the future; the host should
+ * use the 'size' of the message to determine how many bytes to
+ * read from the message queue, rather than a sizeof(struct).
+ *
+ * All multibyte integer fields in the shared-memory interface are little-endian.
+ * The driver supports only little-endian kernels and therefore accesses these
+ * fields in native byte order.
+ *
+ ****************************************************************************/
+
+ #define MVE_CHROMA_FORMAT_420 (0x1)
+
+/* format bitfield: */
+#define MVE_FORMAT_BF_C  (0) /* 3 bits, chroma subsampling   */
+#define MVE_FORMAT_BF_B  (4) /* 4 bits, max bitdepth minus 8 */
+#define MVE_FORMAT_BF_N  (8) /* 2 bits, number of planes     */
+#define MVE_FORMAT_BF_V (12) /* 2 bits, format variant       */
+/* formats: */
+#define MVE_FORMAT_YUV420_I420	((MVE_CHROMA_FORMAT_420  << MVE_FORMAT_BF_C) | \
+				((8 - 8) << MVE_FORMAT_BF_B) | \
+				(3 << MVE_FORMAT_BF_N) | \
+				(0 << MVE_FORMAT_BF_V))
+
+#define MVE_FORMAT_YUV420_NV12	((MVE_CHROMA_FORMAT_420  << MVE_FORMAT_BF_C) | \
+				((8 - 8) << MVE_FORMAT_BF_B) | \
+				(2 << MVE_FORMAT_BF_N) | \
+				(0 << MVE_FORMAT_BF_V))
+
+#define MVE_FORMAT_YUV420_NV21	((MVE_CHROMA_FORMAT_420  << MVE_FORMAT_BF_C) | \
+				((8 - 8) << MVE_FORMAT_BF_B) | \
+				(2 << MVE_FORMAT_BF_N) | \
+				(1 << MVE_FORMAT_BF_V))
+
+#endif /* __MALI_VPU_PROTOCOL_DEF_V3_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c new file +37 -0
new file mode 100644
index 0000000000000..9bcb3a73a582e
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#include <linux/errno.h>
+#include <linux/minmax.h>
+#include <linux/mm.h>
+#include <linux/string.h>
+#include <media/v4l2-common.h>
+
+#include "mali-vpu-session.h"
+#include "mali-vpu-dec.h"
+#include "mali-vpu-variant.h"
+
+static int mali_vpu_set_port_fmt(struct mali_vpu_port *port,
+				 enum v4l2_buf_type type,
+				 u32 width, u32 height)
+{
+	struct v4l2_pix_format_mplane *pix_mp = &port->curr_fmt.fmt.pix_mp;
+	const struct mali_vpu_format_desc *fmt_default;
+	int ret;
+
+	if (!port->supported_fmts.fmts || !port->supported_fmts.num_fmts)
+		return -EINVAL;
+
+	fmt_default = &port->supported_fmts.fmts[0];
+
+	memset(&port->curr_fmt, 0, sizeof(port->curr_fmt));
+	port->curr_fmt.type = type;
+
+	if (fmt_default->flags & V4L2_FMT_FLAG_COMPRESSED) {
+		pix_mp->pixelformat = fmt_default->fourcc;
+		pix_mp->num_planes = 1;
+		pix_mp->width = width;
+		pix_mp->height = height;
+		pix_mp->plane_fmt[0].bytesperline = 0;

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c line 47

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c new file +10 -0
+
+		/*
+		 * Use one byte per pixel as the default compressed sizeimage,
+		 * with a minimum of one page, aligned to a page boundary.
+		 */
+		pix_mp->plane_fmt[0].sizeimage = PAGE_ALIGN(max_t(u32, PAGE_SIZE, width * height));
+	} else {
+		ret = v4l2_fill_pixfmt_mp(pix_mp, fmt_default->fourcc, width, height);
+		if (ret)
+			return ret;

I might have to come back to this code once we have TRY/SET_FMT in place, meanwhile it seems more like a default setting helper then something that will be part of the ioctl interface.

Quoted 63 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c new file +36 -0
+	}
+
+	pix_mp->field = V4L2_FIELD_NONE;
+
+	return 0;
+}
+
+static int mali_vpu_set_port_defaults(struct mali_vpu_session *sess)
+{
+	const struct mali_vpu_framesize_caps *fmsz = &sess->vpu_dev->variant->frmsz_caps;
+	int ret;
+
+	ret = mali_vpu_set_port_fmt(&sess->port_src, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
+				    fmsz->width_default, fmsz->height_default);
+	if (ret)
+		return ret;
+
+	return mali_vpu_set_port_fmt(&sess->port_dst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
+				    fmsz->width_default, fmsz->height_default);
+}
+
+int mali_vpu_session_init(struct mali_vpu_session *sess)
+{
+	int ret;
+
+	if (sess->role != MALI_VPU_DECODER)
+		return -EINVAL;
+
+	mali_vpu_session_init_dec(sess);
+
+	ret = mali_vpu_set_port_defaults(sess);
+	if (ret)
+		return ret;
+
+	return 0;
+}
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +12 -0
index 7166c53236f1a..a2778ca496381 100644
@@ -6,21 +6,39 @@
 #ifndef __MALI_VPU_SESSION_H__
 #define __MALI_VPU_SESSION_H__
 
+#include <linux/videodev2.h>
 #include <media/v4l2-fh.h>
 
 #include "mali-vpu-device.h"
+#include "mali-vpu-formats.h"
+
+/**
+ * struct mali_vpu_port - format state of a src/dst queue in a session.
+ * @curr_fmt: V4L2 format currently configured for the port
+ * @supported_fmts: List of format descriptors supported by the queue.
+ */
+struct mali_vpu_port {
+	struct v4l2_format		curr_fmt;
+	struct mali_vpu_format_list	supported_fmts;
+};
 
 /**
  * struct mali_vpu_session - holds per session state and resources.
  * @vpu_dev: Reference to the parent device structure.

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h line 37

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +4 -0
  * @role: Role of this session.
+ * @port_src: Source (OUTPUT) port for this session.
+ * @port_dst: Destination (CAPTURE) port for this session.
  * @fh: V4L2 file handle for this session.
  */
 struct mali_vpu_session {
 	struct mali_vpu_dev		*vpu_dev;
 	enum mali_vpu_session_role	role;
+	struct mali_vpu_port		port_src;
+	struct mali_vpu_port		port_dst;

Ok, its time to comment, src_port and dst_port is a lot easier to read imho.

Quoted 79 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +2 -0
 	struct v4l2_fh		fh;
 };
 
+int mali_vpu_session_init(struct mali_vpu_session *sess);
+
 #endif /* __MALI_VPU_SESSION_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +50 -0
index 36509d6a1c5d5..7b04b51eeedc2 100644
@@ -4,18 +4,81 @@
  */
 
 #include <linux/array_size.h>
+#include <linux/videodev2.h>
 
+#include "mali-vpu-formats.h"
+#include "mali-vpu-protocol-def-v3.h"
 #include "mali-vpu-variant.h"
 
 #define MALI_VPU_HW_PID_V76 0x56627
 
+#define MALI_V76_MAX_FRAME_WIDTH 8192
+#define MALI_V76_MAX_FRAME_HEIGHT 8192
+#define MALI_V76_MIN_FRAME_WIDTH 2
+#define MALI_V76_MIN_FRAME_HEIGHT 2
+#define MALI_V76_DEFAULT_FRAME_WIDTH 640
+#define MALI_V76_DEFAULT_FRAME_HEIGHT 480
+
 static const char * const mali_v76_rst_names[] = {
 	"warm",
 };
 
+static const struct mali_vpu_format_desc mali_v76_dec_src_fmts[] = {
+	{
+		.fourcc		= V4L2_PIX_FMT_H264,
+		.mve_frame_fmt	= 0,
+		.codec_fmt	= MALI_VPU_FORMAT_CODEC_H264,
+		.flags		= V4L2_FMT_FLAG_COMPRESSED | V4L2_FMT_FLAG_DYN_RESOLUTION,
+	},
+};
+
+static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = {
+	{
+		.fourcc		= V4L2_PIX_FMT_YUV420M,
+		.mve_frame_fmt	= MVE_FORMAT_YUV420_I420,
+		.codec_fmt	= 0,
+		.flags		= 0,
+	},
+	{
+		.fourcc		= V4L2_PIX_FMT_NV12M,
+		.mve_frame_fmt	= MVE_FORMAT_YUV420_NV12,
+		.codec_fmt	= 0,
+		.flags		= 0,
+	},
+	{
+		.fourcc		= V4L2_PIX_FMT_NV21M,
+		.mve_frame_fmt	= MVE_FORMAT_YUV420_NV21,
+		.codec_fmt	= 0,
+		.flags		= 0,
+	},
+};
+
 const struct mali_vpu_variant mali_vpu_v76_variant = {
 	.expected_hw_pid = MALI_VPU_HW_PID_V76,
 
 	.rst_names = mali_v76_rst_names,
 	.num_rsts = ARRAY_SIZE(mali_v76_rst_names),
+
+	.fmt_caps = {
+		.dec_src = {
+			.fmts = mali_v76_dec_src_fmts,
+			.num_fmts = ARRAY_SIZE(mali_v76_dec_src_fmts),
+		},
+		.dec_dst = {
+			.fmts = mali_v76_dec_dst_fmts,
+			.num_fmts = ARRAY_SIZE(mali_v76_dec_dst_fmts),
+		},

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c line 80

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +10 -0
+	},
+	.frmsz_caps = {
+		.stepwise = {
+			.min_width = MALI_V76_MIN_FRAME_WIDTH,
+			.max_width = MALI_V76_MAX_FRAME_WIDTH,
+			.step_width = 2,
+			.min_height = MALI_V76_MIN_FRAME_HEIGHT,
+			.max_height = MALI_V76_MAX_FRAME_HEIGHT,
+			.step_height = 2,
+		},

Are you sure this will be the same for all codecs and all formats?

Quoted 26 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +3 -0
+		.width_default = MALI_V76_DEFAULT_FRAME_WIDTH,
+		.height_default = MALI_V76_DEFAULT_FRAME_HEIGHT,
+	},
 };
drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h +14 -0
index dadfd686275db..a46c25a94f18b 100644
@@ -7,17 +7,47 @@
 #define __MALI_VPU_VARIANT_H__
 
 #include <linux/types.h>
+#include <linux/videodev2.h>
+
+#include "mali-vpu-formats.h"
+
+/**
+ * struct mali_vpu_framesize_caps - Variant-specific frame size capability.
+ * @stepwise: Stepwise frame size limits and increments supported by the hardware.
+ * @width_default: Default width used when userspace does not request one.
+ * @height_default: Default height used when userspace does not request one.
+ */
+struct mali_vpu_framesize_caps {
+	struct v4l2_frmsize_stepwise stepwise;
+	u32 width_default;
+	u32 height_default;

drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h line 33

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h +10 -0
+};
+
+/**
+ * struct mali_vpu_format_caps - Pixel/coded format capability tables.
+ * @dec_src: List of formats supported on decoder OUTPUT queue (compressed bitstream in).
+ * @dec_dst: List of formats supported on decoder CAPTURE queue (decoded raw frames).
+ */
+struct mali_vpu_format_caps {
+	const struct mali_vpu_format_list dec_src;
+	const struct mali_vpu_format_list dec_dst;

Strange naming, perhaps src_formats/dst_formats?

Quoted 40 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h +6 -0
+};
 
 /**
  * struct mali_vpu_variant - Variant-specific hardware capability descriptor.
  * @rst_names: Array of reset control names for this variant.
  * @num_rsts: Number of reset controls for this variant.
  * @expected_hw_pid: Expected hardware product ID read from the version register.
+ * @fmt_caps: Format capability tables for encode/decode paths.
+ * @frmsz_caps: Frame dimension limits and defaults.
  */
 struct mali_vpu_variant {
 	const char * const *rst_names;
 	unsigned int num_rsts;
 	u32 expected_hw_pid;
+
+	struct mali_vpu_format_caps	fmt_caps;
+	struct mali_vpu_framesize_caps	frmsz_caps;
 };
 
 extern const struct mali_vpu_variant mali_vpu_v76_variant;
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +7 -0
index 7a169b7b14bf3..13468c6baba76 100644
@@ -37,8 +37,15 @@ static int mali_vpu_open(struct file *filp)
 	v4l2_fh_init(&sess->fh, vdev);
 	v4l2_fh_add(&sess->fh, filp);
 
+	ret = mali_vpu_session_init(sess);
+	if (ret)
+		goto err_v4l2_fh_deinit;
+
 	return 0;
 
+err_v4l2_fh_deinit:
+	v4l2_fh_del(&sess->fh, filp);
+	v4l2_fh_exit(&sess->fh);
 err_free_sess:
 	kfree(sess);
 	return ret;
#14 lore raw

Re: [RFC PATCH 04/28] media: mali-vpu: initialize decoder format state

Quoted 8 lines · Nicolas Dufresne

drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c line 15

Quoted 6 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c new file +6 -0
+void mali_vpu_session_init_dec(struct mali_vpu_session *sess)
+{
+	const struct mali_vpu_format_caps *variant_fmts = &sess->vpu_dev->variant->fmt_caps;
+
+	sess->port_src.supported_fmts = variant_fmts->dec_src;
+	sess->port_dst.supported_fmts = variant_fmts->dec_dst;

Why? the variant are constant, you can just read the information from the variant. Make a getter or point to it.

The format arrays are already static const and shared; only the list descriptor (the pointer and count) is copied. However, you’re right that each port could simply point to the const list in the variant, avoiding that copy too. I’ll make that change.

Quoted 1 line

p.s. is that "port" like in OMX IL?

Here, “port” just groups the format and streaming state for a V4L2 OUTPUT or CAPTURE queue.

Quoted 27 lines
Quoted 27 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c new file +27 -0
+static int mali_vpu_set_port_fmt(struct mali_vpu_port *port,
+				 enum v4l2_buf_type type,
+				 u32 width, u32 height)
+{
+	struct v4l2_pix_format_mplane *pix_mp = &port->curr_fmt.fmt.pix_mp;
+	const struct mali_vpu_format_desc *fmt_default;
+	int ret;
+
+	if (!port->supported_fmts.fmts || !port->supported_fmts.num_fmts)
+		return -EINVAL;
+
+	fmt_default = &port->supported_fmts.fmts[0];
+
+	memset(&port->curr_fmt, 0, sizeof(port->curr_fmt));
+	port->curr_fmt.type = type;
+
+	if (fmt_default->flags & V4L2_FMT_FLAG_COMPRESSED) {
+		pix_mp->pixelformat = fmt_default->fourcc;
+		pix_mp->num_planes = 1;
+		pix_mp->width = width;
+		pix_mp->height = height;
+		pix_mp->plane_fmt[0].bytesperline = 0;
+
+		/*
+		 * Use one byte per pixel as the default compressed sizeimage,
+		 * with a minimum of one page, aligned to a page boundary.
+		 */
Quoted 8 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c line 47

Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c new file +5 -0
+		pix_mp->plane_fmt[0].sizeimage = PAGE_ALIGN(max_t(u32, PAGE_SIZE, width * height));
+	} else {
+		ret = v4l2_fill_pixfmt_mp(pix_mp, fmt_default->fourcc, width, height);
+		if (ret)
+			return ret;

I might have to come back to this code once we have TRY/SET_FMT in place, meanwhile it seems more like a default setting helper then something that will be part of the ioctl interface.

Thanks, yes, this helper initializes the queue’s default format. I’ll rename it to mali_vpu_init_port_fmt() to make that clear. Patch 7 extracts the format-filling logic into mali_vpu_fill_pixmp_fmt(), shared by initialization and TRY_FMT/S_FMT.

Quoted 6 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h line 37

Quoted 5 lines
  struct mali_vpu_session {
  	struct mali_vpu_dev		*vpu_dev;
  	enum mali_vpu_session_role	role;
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +2 -0
+	struct mali_vpu_port		port_src;
+	struct mali_vpu_port		port_dst;

Ok, its time to comment, src_port and dst_port is a lot easier to read imho.

I’ve generally placed the direction last, as in dec_src/dec_dst and pixmp_src/pixmp_dst. I’d prefer to retain port_src/port_dst to follow that pattern.

Quoted 11 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c line 80

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +10 -0
+	},
+	.frmsz_caps = {
+		.stepwise = {
+			.min_width = MALI_V76_MIN_FRAME_WIDTH,
+			.max_width = MALI_V76_MAX_FRAME_WIDTH,
+			.step_width = 2,
+			.min_height = MALI_V76_MIN_FRAME_HEIGHT,
+			.max_height = MALI_V76_MAX_FRAME_HEIGHT,
+			.step_height = 2,
+		},

Are you sure this will be the same for all codecs and all formats?

These limits apply to H.264 and the three 8-bit YUV420 capture formats currently exposed by the driver. The plan is to introduce separate ranges when adding a codec or format that needs different limits.

Quoted 4 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h line 33

Quoted 3 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h +3 -0
+struct mali_vpu_format_caps {
+	const struct mali_vpu_format_list dec_src;
+	const struct mali_vpu_format_list dec_dst;

Strange naming, perhaps src_formats/dst_formats?

Here, dec identifies the decoder role and src/dst the queue direction; fmt_caps already provides the format context. I’d prefer to retain it.

Regards,
Charvi

#15 lore raw

Re: [RFC PATCH 04/28] media: mali-vpu: initialize decoder format state

Hi,

Le mardi 24 novembre 2026 à 14:56 +0000, Charvi Mehta a écrit :

Quoted 9 lines
Quoted 6 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h line 37

Quoted 5 lines

   struct mali_vpu_session {
    struct mali_vpu_dev *vpu_dev;
    enum mali_vpu_session_role role;

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +2 -0
+	struct mali_vpu_port		port_src;
+	struct mali_vpu_port		port_dst;

Ok, its time to comment, src_port and dst_port is a lot easier to read imho.

I’ve generally placed the direction last, as in dec_src/dec_dst and pixmp_src/pixmp_dst.
I’d prefer to retain port_src/port_dst to follow that pattern.

I heard your preference, but readability when up-streaming is important, we dont say "the port source", we say "the source port" (at least in english, which we program in), and so when a human reads your code, having things in reverse makes it harder.

Nicolas

#16 lore raw

Re: [RFC PATCH 04/28] media: mali-vpu: initialize decoder format state

Quoted 6 lines · Nicolas Dufresne
Quoted 6 lines
Quoted 6 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h line 37

Quoted 5 lines

   struct mali_vpu_session {
    struct mali_vpu_dev *vpu_dev;
    enum mali_vpu_session_role role;

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +2 -0
+	struct mali_vpu_port		port_src;
+	struct mali_vpu_port		port_dst;

Ok, its time to comment, src_port and dst_port is a lot easier to read imho.

Quoted 7 lines
Quoted 3 lines

I’ve generally placed the direction last, as in dec_src/dec_dst and pixmp_src/pixmp_dst.
I’d prefer to retain port_src/port_dst to follow that pattern.

I heard your preference, but readability when up-streaming is important, we dont say "the port source", we say "the source port" (at least in english, which we program in), and so when a human reads your code, having things in reverse makes it harder.

I'll rename these members in v2.

Regards,
Charvi

#17 lore raw

RFC PATCH 5/28 media: mali-vpu: add V4L2 mem2mem and VB2 queue support

In reply to Charvi Mehta

Initialize the V4L2 mem2mem device and create a mem2mem context for each session.

Add VB2 operations for queue setup, buffer initialization, validation, preparation and queueing using the DMA scatter-gather allocator. Wire up the standard mem2mem buffer ioctls together with mmap and poll support.

---
 drivers/media/platform/arm/mali-vpu/Kconfig   |  2 +
 drivers/media/platform/arm/mali-vpu/Makefile  |  4 +-
 .../platform/arm/mali-vpu/mali-vpu-buffer.c   | 51 ++++++++++
 .../platform/arm/mali-vpu/mali-vpu-buffer.h   | 21 ++++
 .../platform/arm/mali-vpu/mali-vpu-device.h   |  3 +
 .../platform/arm/mali-vpu/mali-vpu-probe.c    | 12 ++-
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 37 ++++++-
 .../platform/arm/mali-vpu/mali-vpu-session.h  |  8 ++
 .../platform/arm/mali-vpu/mali-vpu-vb2.c      | 99 +++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-vb2.h      | 13 +++
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   | 35 ++++++-
 .../platform/arm/mali-vpu/mali-vpu-vidioc.h   |  2 +
 12 files changed, 281 insertions(+), 6 deletions(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-vb2.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-vb2.h
drivers/media/platform/arm/mali-vpu/Kconfig +2 -0
index d98872f2b7ec0..2728830f11a4a 100644
@@ -4,6 +4,8 @@ config VIDEO_MALI_VPU
 	depends on V4L_MEM2MEM_DRIVERS
 	depends on VIDEO_DEV
 	depends on !CPU_BIG_ENDIAN
+	select V4L2_MEM2MEM_DEV
+	select VIDEOBUF2_DMA_SG
 	help
 	  Enable this to support Arm's Mali Video hardware accelerator.
 	  This is a V4L2 driver.
drivers/media/platform/arm/mali-vpu/Makefile +3 -1
index 0a42c53ae5088..94ddde02fc528 100644
@@ -4,6 +4,8 @@ mali-vpu-objs := mali-vpu-probe.o \
 		mali-vpu-variant-v76.o \
 		mali-vpu-vidioc.o \
 		mali-vpu-session.o \
-		mali-vpu-dec.o
+		mali-vpu-dec.o \
+		mali-vpu-buffer.o \
+		mali-vpu-vb2.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c new file +51 -0

51 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.h new file +21 -0
new file mode 100644
index 0000000000000..f5d67d48e5921
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_BUFFER_H__
+#define __MALI_VPU_BUFFER_H__
+
+#include <media/v4l2-mem2mem.h>
+
+#define MALI_VPU_BUFFER_MAX_PLANES 3
+
+struct mali_vpu_buffer {
+	struct v4l2_m2m_buffer m2m_buf;
+};
+
+int mali_vpu_vb2_buffer_init(struct vb2_buffer *vb2_buf);
+int mali_vpu_vb2_buffer_prepare(struct vb2_buffer *vb2_buf);
+int mali_vpu_vb2_buffer_out_validate(struct vb2_buffer *vb2_buf);
+
+#endif /* __MALI_VPU_BUFFER_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-device.h +3 -0
index d9b0801cbb520..26b2bf9e1588e 100644
@@ -12,6 +12,7 @@
 #define MALI_VPU_DRV_NAME "mali-vpu"
 
 struct clk_bulk_data;
+struct v4l2_m2m_dev;
 struct mali_vpu_variant;
 struct reset_control_bulk_data;
 
@@ -35,6 +36,7 @@ enum mali_vpu_session_role {
  * @variant: Structure describing variant-specific features and capabilities.
  * @v4l2_dev: V4L2 device structure for mali vpu.
  * @vdev_dec: Video device structure for decoder.
+ * @m2m_dev: V4L2 mem2mem device structure for mali vpu.
  */
 struct mali_vpu_dev {
 	struct device			*dev;
@@ -48,6 +50,7 @@ struct mali_vpu_dev {
 	const struct mali_vpu_variant	*variant;
 	struct v4l2_device		v4l2_dev;
 	struct video_device		*vdev_dec;
+	struct v4l2_m2m_dev		*m2m_dev;
 };
 
 #endif /* __MALI_VPU_DEVICE_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c +11 -1
index 6d21cb731ee1d..36a25030d7324 100644
@@ -18,6 +18,7 @@
 #include <linux/string.h>
 #include <media/v4l2-dev.h>
 #include <media/v4l2-device.h>
+#include <media/v4l2-mem2mem.h>
 
 #include "mali-vpu-device.h"
 #include "mali-vpu-registers.h"
@@ -218,14 +219,22 @@ static int mali_vpu_probe(struct platform_device *pdev)
 		goto err_rst_clk;
 	}
 
+	vpu_dev->m2m_dev = v4l2_m2m_init(&mali_vpu_m2m_ops);
+	if (IS_ERR(vpu_dev->m2m_dev)) {
+		ret = PTR_ERR(vpu_dev->m2m_dev);
+		goto err_unreg_v4l2;
+	}
+
 	ret = mali_vpu_register_video_device(vpu_dev, MALI_VPU_DECODER);
 	if (ret)
-		goto err_unreg_v4l2;
+		goto err_release_m2m;
 
 	platform_set_drvdata(pdev, vpu_dev);
 
 	return 0;
 
+err_release_m2m:
+	v4l2_m2m_release(vpu_dev->m2m_dev);
 err_unreg_v4l2:
 	v4l2_device_unregister(&vpu_dev->v4l2_dev);
 err_rst_clk:
@@ -241,6 +250,7 @@ static void mali_vpu_remove(struct platform_device *pdev)
 	struct mali_vpu_dev *vpu_dev = platform_get_drvdata(pdev);
 
 	video_unregister_device(vpu_dev->vdev_dec);
+	v4l2_m2m_release(vpu_dev->m2m_dev);
 	v4l2_device_unregister(&vpu_dev->v4l2_dev);
 
 	mali_vpu_assert_reset(vpu_dev);
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +34 -3
index 9bcb3a73a582e..95050296f9904 100644
@@ -3,15 +3,18 @@
  * Copyright (C) 2026 ARM Limited.
  */
 
+#include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/minmax.h>
 #include <linux/mm.h>
 #include <linux/string.h>
 #include <media/v4l2-common.h>
+#include <media/v4l2-mem2mem.h>
 
 #include "mali-vpu-session.h"
 #include "mali-vpu-dec.h"
 #include "mali-vpu-variant.h"
+#include "mali-vpu-vb2.h"
 
 static int mali_vpu_set_port_fmt(struct mali_vpu_port *port,
 				 enum v4l2_buf_type type,
@@ -70,14 +73,42 @@ int mali_vpu_session_init(struct mali_vpu_session *sess)
 {
 	int ret;
 
-	if (sess->role != MALI_VPU_DECODER)
-		return -EINVAL;
+	mutex_init(&sess->vq_lock);
+
+	if (sess->role != MALI_VPU_DECODER) {
+		ret = -EINVAL;
+		goto err_mutex_destroy;
+	}
 
 	mali_vpu_session_init_dec(sess);
 
 	ret = mali_vpu_set_port_defaults(sess);
 	if (ret)
-		return ret;
+		goto err_mutex_destroy;
+
+	sess->m2m_ctx = v4l2_m2m_ctx_init(sess->vpu_dev->m2m_dev, sess, mali_vpu_queue_init);
+	if (IS_ERR(sess->m2m_ctx)) {
+		ret = PTR_ERR(sess->m2m_ctx);
+		goto err_mutex_destroy;
+	}
 
 	return 0;
+
+err_mutex_destroy:
+	mutex_destroy(&sess->vq_lock);
+	return ret;
+}
+
+void mali_vpu_session_cleanup(struct mali_vpu_session *sess)
+{
+	mutex_lock(&sess->vq_lock);
+
+	if (sess->m2m_ctx) {
+		v4l2_m2m_ctx_release(sess->m2m_ctx);
+		sess->m2m_ctx = NULL;
+		sess->fh.m2m_ctx = NULL;
+	}
+
+	mutex_unlock(&sess->vq_lock);
+	mutex_destroy(&sess->vq_lock);
 }
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +8 -0
index a2778ca496381..dfd1833f884af 100644
@@ -6,12 +6,15 @@
 #ifndef __MALI_VPU_SESSION_H__
 #define __MALI_VPU_SESSION_H__
 
+#include <linux/mutex.h>
 #include <linux/videodev2.h>
 #include <media/v4l2-fh.h>
 
 #include "mali-vpu-device.h"
 #include "mali-vpu-formats.h"
 
+struct v4l2_m2m_ctx;
+
 /**
  * struct mali_vpu_port - format state of a src/dst queue in a session.
  * @curr_fmt: V4L2 format currently configured for the port
@@ -28,17 +31,22 @@ struct mali_vpu_port {
  * @role: Role of this session.
  * @port_src: Source (OUTPUT) port for this session.
  * @port_dst: Destination (CAPTURE) port for this session.
+ * @vq_lock: Mutex lock to serialise per-session queue related ioctls.
  * @fh: V4L2 file handle for this session.
+ * @m2m_ctx: V4L2 mem2mem context for this session.
  */
 struct mali_vpu_session {
 	struct mali_vpu_dev		*vpu_dev;
 	enum mali_vpu_session_role	role;
 	struct mali_vpu_port		port_src;
 	struct mali_vpu_port		port_dst;
+	struct mutex			vq_lock; /* lock to serialise per-session ioctls */
 
 	struct v4l2_fh		fh;
+	struct v4l2_m2m_ctx	*m2m_ctx;
 };
 
 int mali_vpu_session_init(struct mali_vpu_session *sess);
+void mali_vpu_session_cleanup(struct mali_vpu_session *sess);
 
 #endif /* __MALI_VPU_SESSION_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-vb2.c new file +99 -0

99 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-vb2.h new file +13 -0
new file mode 100644
index 0000000000000..4aa80d2136c2f
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_VB2_H__
+#define __MALI_VPU_VB2_H__
+
+struct vb2_queue;
+
+int mali_vpu_queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq);
+
+#endif /* __MALI_VPU_VB2_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +34 -1
index 13468c6baba76..aaf604beff93a 100644
@@ -10,6 +10,7 @@
 #include <media/v4l2-dev.h>
 #include <media/v4l2-fh.h>
 #include <media/v4l2-ioctl.h>
+#include <media/v4l2-mem2mem.h>
 
 #include "mali-vpu-session.h"
 #include "mali-vpu-vidioc.h"
@@ -41,6 +42,8 @@ static int mali_vpu_open(struct file *filp)
 	if (ret)
 		goto err_v4l2_fh_deinit;
 
+	sess->fh.m2m_ctx = sess->m2m_ctx;
+
 	return 0;
 
 err_v4l2_fh_deinit:
@@ -56,6 +59,7 @@ static int mali_vpu_release(struct file *filp)
 	struct mali_vpu_session *sess = container_of(file_to_v4l2_fh(filp),
 						     struct mali_vpu_session, fh);
 
+	mali_vpu_session_cleanup(sess);
 	v4l2_fh_del(&sess->fh, filp);
 	v4l2_fh_exit(&sess->fh);
 	kfree(sess);
@@ -67,6 +71,27 @@ const struct v4l2_file_operations mali_vpu_fops = {
 	.open		= mali_vpu_open,
 	.release	= mali_vpu_release,
 	.unlocked_ioctl = video_ioctl2,
+	.poll		= v4l2_m2m_fop_poll,
+	.mmap		= v4l2_m2m_fop_mmap,
+};
+
+static int mali_vpu_m2m_job_ready(void *priv)
+{
+	/*
+	 * The Mali scheduler/LSID path owns hardware dispatch.
+	 * v4l2-mem2mem is used only for vb2 queue bookkeeping.
+	 */
+	return 0;
+}
+
+static void mali_vpu_m2m_device_run(void *priv)
+{
+	WARN_ON_ONCE(1);
+}
+
+const struct v4l2_m2m_ops mali_vpu_m2m_ops = {
+	.job_ready = mali_vpu_m2m_job_ready,
+	.device_run = mali_vpu_m2m_device_run,
 };
 
 static int mali_vpu_querycap(struct file *filp, void *fh,
@@ -81,5 +106,13 @@ static int mali_vpu_querycap(struct file *filp, void *fh,
 }
 
 const struct v4l2_ioctl_ops mali_vpu_ioctl_ops_dec = {
-	.vidioc_querycap = mali_vpu_querycap,
+	.vidioc_querycap	= mali_vpu_querycap,
+	.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_expbuf		= v4l2_m2m_ioctl_expbuf,
+	.vidioc_qbuf		= v4l2_m2m_ioctl_qbuf,
+	.vidioc_dqbuf		= v4l2_m2m_ioctl_dqbuf,
+	.vidioc_remove_bufs	= v4l2_m2m_ioctl_remove_bufs,
 };
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.h +2 -0
index e1aedea5bbcb6..e967cae8be137 100644
@@ -8,8 +8,10 @@
 
 struct v4l2_file_operations;
 struct v4l2_ioctl_ops;
+struct v4l2_m2m_ops;
 
 extern const struct v4l2_file_operations mali_vpu_fops;
+extern const struct v4l2_m2m_ops mali_vpu_m2m_ops;
 extern const struct v4l2_ioctl_ops mali_vpu_ioctl_ops_dec;
 
 #endif /* __MALI_VPU_VIDIOC_H__ */
Signature · 1 line
2.43.0
#18 lore raw

RFC PATCH 6/28 media: mali-vpu: implement format query ioctls

In reply to Charvi Mehta

Expose H.264 as the decoder OUTPUT format and YUV420M, NV12M, NV21M as the CAPTURE formats through VIDIOC_ENUM_FMT.

Implement VIDIOC_ENUM_FRAMESIZES to report the variant-specific stepwise frame size limits. Add VIDIOC_G_FMT support to return the current source and destination formats for each session.

---
 .../platform/arm/mali-vpu/mali-vpu-session.h  |   5 +
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   | 152 ++++++++++++++++--
 2 files changed, 146 insertions(+), 11 deletions(-)
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +5 -0
index dfd1833f884af..f8a602cb59006 100644
@@ -49,4 +49,9 @@ struct mali_vpu_session {
 int mali_vpu_session_init(struct mali_vpu_session *sess);
 void mali_vpu_session_cleanup(struct mali_vpu_session *sess);
 
+static inline struct mali_vpu_session *mali_vpu_get_session(struct file *filp)
+{
+	return container_of(file_to_v4l2_fh(filp), struct mali_vpu_session, fh);
+}
+
 #endif /* __MALI_VPU_SESSION_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +141 -11

170 lines not on this page · raw message

Signature · 1 line
2.43.0
#19 lore raw

RFC PATCH 7/28 media: mali-vpu: implement TRY_FMT and S_FMT ioctls

In reply to Charvi Mehta

Implement VIDIOC_TRY_FMT and VIDIOC_S_FMT for the decoder OUTPUT and CAPTURE queues.

Move format lookup and buffer layout calculation into shared helpers so session initialization and format negotiation use the same sizing rules.

---
 drivers/media/platform/arm/mali-vpu/Makefile  |   3 +-
 .../platform/arm/mali-vpu/mali-vpu-formats.c  |  81 +++++++++++
 .../platform/arm/mali-vpu/mali-vpu-formats.h  |  10 ++
 .../platform/arm/mali-vpu/mali-vpu-session.c  |  24 +---
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   | 132 +++++++++++++++---
 5 files changed, 209 insertions(+), 41 deletions(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c
drivers/media/platform/arm/mali-vpu/Makefile +2 -1
index 94ddde02fc528..1dc62621d9dc0 100644
@@ -6,6 +6,7 @@ mali-vpu-objs := mali-vpu-probe.o \
 		mali-vpu-session.o \
 		mali-vpu-dec.o \
 		mali-vpu-buffer.o \
-		mali-vpu-vb2.o
+		mali-vpu-vb2.o \
+		mali-vpu-formats.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c new file +81 -0

81 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-formats.h +10 -0
index 13031809fdb4d..5703142b93b46 100644
@@ -7,6 +7,9 @@
 #define __MALI_VPU_FORMATS_H__
 
 #include <linux/types.h>
+#include <linux/videodev2.h>
+
+struct mali_vpu_session;
 
 enum mali_vpu_codec_fmt {
 	MALI_VPU_FORMAT_CODEC_H264,
@@ -24,4 +27,11 @@ struct mali_vpu_format_list {
 	u32 num_fmts;
 };
 
+const struct mali_vpu_format_desc *mali_vpu_find_format(struct mali_vpu_session *sess,
+							u32 pixfmt,
+							enum v4l2_buf_type type);
+int mali_vpu_fill_pixmp_fmt(const struct mali_vpu_format_desc *fmt,
+			    u32 width, u32 height,
+			    struct v4l2_pix_format_mplane *pix_mp);
+
 #endif /* __MALI_VPU_FORMATS_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +4 -20
index 95050296f9904..6867f51287f48 100644
@@ -5,14 +5,12 @@
 
 #include <linux/err.h>
 #include <linux/errno.h>
-#include <linux/minmax.h>
-#include <linux/mm.h>
 #include <linux/string.h>
-#include <media/v4l2-common.h>
 #include <media/v4l2-mem2mem.h>
 
 #include "mali-vpu-session.h"
 #include "mali-vpu-dec.h"
+#include "mali-vpu-formats.h"
 #include "mali-vpu-variant.h"
 #include "mali-vpu-vb2.h"
 
@@ -32,23 +30,9 @@ static int mali_vpu_set_port_fmt(struct mali_vpu_port *port,
 	memset(&port->curr_fmt, 0, sizeof(port->curr_fmt));
 	port->curr_fmt.type = type;
 
-	if (fmt_default->flags & V4L2_FMT_FLAG_COMPRESSED) {
-		pix_mp->pixelformat = fmt_default->fourcc;
-		pix_mp->num_planes = 1;
-		pix_mp->width = width;
-		pix_mp->height = height;
-		pix_mp->plane_fmt[0].bytesperline = 0;
-
-		/*
-		 * Use one byte per pixel as the default compressed sizeimage,
-		 * with a minimum of one page, aligned to a page boundary.
-		 */
-		pix_mp->plane_fmt[0].sizeimage = PAGE_ALIGN(max_t(u32, PAGE_SIZE, width * height));
-	} else {
-		ret = v4l2_fill_pixfmt_mp(pix_mp, fmt_default->fourcc, width, height);
-		if (ret)
-			return ret;
-	}
+	ret = mali_vpu_fill_pixmp_fmt(fmt_default, width, height, pix_mp);
+	if (ret)
+		return ret;
 
 	pix_mp->field = V4L2_FIELD_NONE;
 
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +112 -20

156 lines not on this page · raw message

Signature · 1 line
2.43.0
#20 lore raw

Re: [RFC PATCH 07/28] media: mali-vpu: implement TRY_FMT and S_FMT ioctls

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] media: mali-vpu: missing CAPTURE busy check in mali_vpu_s_fmt() on OUTPUT - [Medium] media…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] media: mali-vpu: missing CAPTURE busy check in mali_vpu_s_fmt() on OUTPUT
- [Medium] media: mali-vpu: CAPTURE colorimetry lost in mali_vpu_s_fmt() and try_fmt
- [Medium] media: mali-vpu: incomplete field check in mali_vpu_try_format_common()
- [Medium] media: mali-vpu: early return on bad pixfmt in mali_vpu_try_format_common()
--

commit 5a51818e198650b05f2b6b84f5fbe1b7fdc55bf4
Author: Charvi Mehta <[email protected]>

media: mali-vpu: implement TRY_FMT and S_FMT ioctls

This commit implements the VIDIOC_TRY_FMT and VIDIOC_S_FMT ioctls for the decoder OUTPUT and CAPTURE queues and moves format lookup and buffer sizing into shared helpers.

Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +0 -0
index b993255b8c817..dd142527a1a0c 100644

[ ... ]

Quoted 29 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +8 -15
@@ -168,32 +169,119 @@ static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt)
 	return ret;
 }
 
-static const struct mali_vpu_format_desc *mali_vpu_find_format(struct mali_vpu_session *sess,
-							       u32 pixfmt, u32 type)
+static int mali_vpu_try_format_common(struct mali_vpu_session *sess, struct v4l2_format *fmt)
 {
-	const struct mali_vpu_format_desc *fmt_list;
-	u32 num_fmts;
-	u32 i;
+	const struct mali_vpu_framesize_caps *fmsz = &sess->vpu_dev->variant->frmsz_caps;
+	struct v4l2_pix_format_mplane *pix_mp = &fmt->fmt.pix_mp;
+	const struct mali_vpu_format_desc *vpu_fmt;
+	const struct v4l2_format *port_fmt, *port_fmt_dec;
+	struct vb2_queue *src_q;
+	u32 w, h;
+	int ret = 0;
 
-	switch (type) {
-	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
-		fmt_list = sess->port_src.supported_fmts.fmts;
-		num_fmts = sess->port_src.supported_fmts.num_fmts;
-		break;
-	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
-		fmt_list = sess->port_dst.supported_fmts.fmts;
-		num_fmts = sess->port_dst.supported_fmts.num_fmts;
-		break;
-	default:

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 190

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +9 -1
-		return NULL;
+	vpu_fmt = mali_vpu_find_format(sess, pix_mp->pixelformat, fmt->type);
+	if (!vpu_fmt) {
+		port_fmt = mali_vpu_get_port_fmt(sess, fmt->type);
+		if (!port_fmt)
+			return -EINVAL;
+
+		*pix_mp = port_fmt->fmt.pix_mp;
+		return 0;
+	}

[Severity: Medium]
When pix_mp->pixelformat is unrecognized, should mali_vpu_try_format_common() fall back to a supported format descriptor and continue validating the rest of pix_mp instead of overwriting *pix_mp with port_fmt->fmt.pix_mp and returning early?

Returning here discards the caller's requested width, height, and OUTPUT colorimetry and skips v4l2_apply_frmsize_constraints() as well as the streaming CAPTURE geometry lock.

Quoted 13 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +13 -0
+
+	/* Requested format found, validate other fields */
+	w = pix_mp->width ? pix_mp->width : fmsz->width_default;
+	h = pix_mp->height ? pix_mp->height : fmsz->height_default;
+
+	v4l2_apply_frmsize_constraints(&w, &h, &fmsz->stepwise);
+
+	/* Decode CAPTURE queue: lock geometry to input if streaming */
+	if (sess->role == MALI_VPU_DECODER && fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		src_q = v4l2_m2m_get_src_vq(sess->m2m_ctx);
+		if (vb2_is_streaming(src_q)) {
+			port_fmt_dec = &sess->port_src.curr_fmt;
+			w = port_fmt_dec->fmt.pix_mp.width;

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 213

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +10 -0
+			h = port_fmt_dec->fmt.pix_mp.height;
+		}
+	}
+
+	ret = mali_vpu_fill_pixmp_fmt(vpu_fmt, w, h, pix_mp);
+	if (ret)
+		return ret;
+
+	if (pix_mp->field == V4L2_FIELD_ANY)
+		pix_mp->field = V4L2_FIELD_NONE;

[Severity: Medium]
Should pix_mp->field be set unconditionally to V4L2_FIELD_NONE? Because the driver only supports progressive frames, checking only for V4L2_FIELD_ANY leaves any other field value (such as V4L2_FIELD_INTERLACED or an invalid value from userspace) intact in fmt and port->curr_fmt.

Quoted 45 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +45 -0
+
+	return 0;
+}
+
+static int mali_vpu_try_fmt(struct file *filp, void *fh, struct v4l2_format *fmt)
+{
+	struct mali_vpu_session *sess = mali_vpu_get_session(filp);
+	int ret;
+
+	mutex_lock(&sess->vq_lock);
+
+	ret = mali_vpu_try_format_common(sess, fmt);
+
+	mutex_unlock(&sess->vq_lock);
+	return ret;
+}
+
+static int mali_vpu_s_fmt(struct file *filp, void *fh, struct v4l2_format *fmt)
+{
+	struct mali_vpu_session *sess = mali_vpu_get_session(filp);
+	struct vb2_queue *vq;
+	struct v4l2_pix_format_mplane *pixmp_src = &sess->port_src.curr_fmt.fmt.pix_mp;
+	struct v4l2_pix_format_mplane *pixmp_dst = &sess->port_dst.curr_fmt.fmt.pix_mp;
+	int ret = 0;
+
+	mutex_lock(&sess->vq_lock);
+
+	vq = v4l2_m2m_get_vq(sess->m2m_ctx, fmt->type);
+	if (vb2_is_busy(vq)) {
+		ret = -EBUSY;
+		goto mutex_unlock_return;
+	}
+
+	ret = mali_vpu_try_format_common(sess, fmt);
+	if (ret)
+		goto mutex_unlock_return;
+
+	/* OUTPUT provides initial CAPTURE geometry */
+	if (V4L2_TYPE_IS_OUTPUT(fmt->type)) {
+		const struct mali_vpu_format_desc *cap_fmt;
+		struct v4l2_pix_format_mplane *cap;
+		struct v4l2_pix_format_mplane *fmt_mp = &fmt->fmt.pix_mp;
+
+		cap = &sess->port_dst.curr_fmt.fmt.pix_mp;
+		cap_fmt = mali_vpu_find_format(sess, cap->pixelformat,

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt)

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +10 -0
+					       V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
+		if (!cap_fmt) {
+			ret = -EINVAL;
+			goto mutex_unlock_return;
+		}
+
+		ret = mali_vpu_fill_pixmp_fmt(cap_fmt, fmt_mp->width, fmt_mp->height, cap);
+		if (ret)
+			goto mutex_unlock_return;
+	}

[Severity: Medium]
Can VIDIOC_S_FMT on the OUTPUT queue overwrite sess->port_dst.curr_fmt while CAPTURE buffers are already allocated?

Only the queue for fmt->type is checked with vb2_is_busy(vq). If userspace allocates CAPTURE buffers and then calls VIDIOC_S_FMT on OUTPUT with a new resolution, sess->port_dst.curr_fmt.fmt.pix_mp is updated in place without checking whether the destination queue is busy, which violates the V4L2 stateful decoder specification and causes later VIDIOC_PREPARE_BUF or VIDIOC_QBUF calls on existing CAPTURE buffers to fail size validation in mali_vpu_vb2_buffer_prepare().

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 281

Quoted 13 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +13 -0
+
+	if (V4L2_TYPE_IS_OUTPUT(fmt->type))
+		sess->port_src.curr_fmt = *fmt;
+	else
+		sess->port_dst.curr_fmt = *fmt;
+
+	/* Keep peer queue colorimetry aligned with input */
+	if (V4L2_TYPE_IS_OUTPUT(fmt->type)) {
+		pixmp_dst->colorspace	= pixmp_src->colorspace;
+		pixmp_dst->ycbcr_enc	= pixmp_src->ycbcr_enc;
+		pixmp_dst->quantization = pixmp_src->quantization;
+		pixmp_dst->xfer_func	= pixmp_src->xfer_func;
+	}

[Severity: Medium]
Does calling VIDIOC_TRY_FMT or VIDIOC_S_FMT on the CAPTURE queue lose the OUTPUT colorimetry?

mali_vpu_try_format_common() does not copy colorspace, ycbcr_enc, quantization, and xfer_func from sess->port_src.curr_fmt.fmt.pix_mp when fmt->type is V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE. As a result, VIDIOC_TRY_FMT on CAPTURE returns the caller's unaligned colorimetry, and VIDIOC_S_FMT on CAPTURE overwrites sess->port_dst.curr_fmt with *fmt, clobbering the colorimetry propagated during VIDIOC_S_FMT on OUTPUT.

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

Re: [RFC PATCH 07/28] media: mali-vpu: implement TRY_FMT and S_FMT ioctls

In reply to Charvi Mehta

Hi,

skipped a bit, while I remember...

Le jeudi 19 novembre 2026 à 15:37 +0000, Charvi Mehta a écrit :

Quoted 95 lines

Implement VIDIOC_TRY_FMT and VIDIOC_S_FMT for the decoder OUTPUT and CAPTURE queues.

Move format lookup and buffer layout calculation into shared helpers so session initialization and format negotiation use the same sizing rules.

---
 drivers/media/platform/arm/mali-vpu/Makefile  |   3 +-
 .../platform/arm/mali-vpu/mali-vpu-formats.c  |  81 +++++++++++
 .../platform/arm/mali-vpu/mali-vpu-formats.h  |  10 ++
 .../platform/arm/mali-vpu/mali-vpu-session.c  |  24 +---
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   | 132 +++++++++++++++---
 5 files changed, 209 insertions(+), 41 deletions(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c
drivers/media/platform/arm/mali-vpu/Makefile +2 -1
index 94ddde02fc528..1dc62621d9dc0 100644
@@ -6,6 +6,7 @@ mali-vpu-objs := mali-vpu-probe.o \
 		mali-vpu-session.o \
 		mali-vpu-dec.o \
 		mali-vpu-buffer.o \
-		mali-vpu-vb2.o
+		mali-vpu-vb2.o \
+		mali-vpu-formats.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c new file +60 -0
new file mode 100644
index 0000000000000..d268435432371
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#include <linux/errno.h>
+#include <linux/minmax.h>
+#include <linux/mm.h>
+#include <linux/overflow.h>
+#include <linux/string.h>
+#include <media/v4l2-common.h>
+
+#include "mali-vpu-formats.h"
+#include "mali-vpu-session.h"
+
+const struct mali_vpu_format_desc *
+mali_vpu_find_format(struct mali_vpu_session *sess, u32 pixfmt,
+		     enum v4l2_buf_type type)
+{
+	const struct mali_vpu_format_list *fmt_list;
+	u32 i;
+
+	switch (type) {
+	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
+		fmt_list = &sess->port_src.supported_fmts;
+		break;
+	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
+		fmt_list = &sess->port_dst.supported_fmts;
+		break;
+	default:
+		return NULL;
+	}
+
+	for (i = 0; i < fmt_list->num_fmts; i++) {
+		if (fmt_list->fmts[i].fourcc == pixfmt)
+			return &fmt_list->fmts[i];
+	}
+
+	return NULL;
+}
+
+int mali_vpu_fill_pixmp_fmt(const struct mali_vpu_format_desc *fmt,
+			    u32 width, u32 height,
+			    struct v4l2_pix_format_mplane *pix_mp)
+{
+	u32 sizeimage;
+	int ret;
+
+	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
+	memset(pix_mp->plane_fmt, 0, sizeof(pix_mp->plane_fmt));
+	pix_mp->flags = 0;
+
+	if (fmt->flags & V4L2_FMT_FLAG_COMPRESSED) {
+		pix_mp->width = width;
+		pix_mp->height = height;
+		pix_mp->pixelformat = fmt->fourcc;
+		pix_mp->num_planes = 1;
+		pix_mp->plane_fmt[0].bytesperline = 0;
+
+		/*

drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c line 70

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c new file +10 -0
+		 * Use one byte per pixel as the default compressed sizeimage,
+		 * with a minimum of one page, aligned to a page boundary.
+		 */
+		if (check_mul_overflow(width, height, &sizeimage))
+			return -EOVERFLOW;
+
+		sizeimage = max_t(u32, PAGE_SIZE, sizeimage);
+
+		if (sizeimage > U32_MAX - (PAGE_SIZE - 1))
+			return -EOVERFLOW;

The "moved" code is not exactly identical, and this is not said in the commit message. Depending on you max_width/height, this might all be exaggerated.

Quoted 119 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c new file +11 -0
+
+		pix_mp->plane_fmt[0].sizeimage = PAGE_ALIGN(sizeimage);
+
+	} else {
+		ret = v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, width, height);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.h +10 -0
index 13031809fdb4d..5703142b93b46 100644
@@ -7,6 +7,9 @@
 #define __MALI_VPU_FORMATS_H__
 
 #include <linux/types.h>
+#include <linux/videodev2.h>
+
+struct mali_vpu_session;
 
 enum mali_vpu_codec_fmt {
 	MALI_VPU_FORMAT_CODEC_H264,
@@ -24,4 +27,11 @@ struct mali_vpu_format_list {
 	u32 num_fmts;
 };
 
+const struct mali_vpu_format_desc *mali_vpu_find_format(struct mali_vpu_session *sess,
+							u32 pixfmt,
+							enum v4l2_buf_type type);
+int mali_vpu_fill_pixmp_fmt(const struct mali_vpu_format_desc *fmt,
+			    u32 width, u32 height,
+			    struct v4l2_pix_format_mplane *pix_mp);
+
 #endif /* __MALI_VPU_FORMATS_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +4 -20
index 95050296f9904..6867f51287f48 100644
@@ -5,14 +5,12 @@
 
 #include <linux/err.h>
 #include <linux/errno.h>
-#include <linux/minmax.h>
-#include <linux/mm.h>
 #include <linux/string.h>
-#include <media/v4l2-common.h>
 #include <media/v4l2-mem2mem.h>
 
 #include "mali-vpu-session.h"
 #include "mali-vpu-dec.h"
+#include "mali-vpu-formats.h"
 #include "mali-vpu-variant.h"
 #include "mali-vpu-vb2.h"
 
@@ -32,23 +30,9 @@ static int mali_vpu_set_port_fmt(struct mali_vpu_port *port,
 	memset(&port->curr_fmt, 0, sizeof(port->curr_fmt));
 	port->curr_fmt.type = type;
 
-	if (fmt_default->flags & V4L2_FMT_FLAG_COMPRESSED) {
-		pix_mp->pixelformat = fmt_default->fourcc;
-		pix_mp->num_planes = 1;
-		pix_mp->width = width;
-		pix_mp->height = height;
-		pix_mp->plane_fmt[0].bytesperline = 0;
-
-		/*
-		 * Use one byte per pixel as the default compressed sizeimage,
-		 * with a minimum of one page, aligned to a page boundary.
-		 */
-		pix_mp->plane_fmt[0].sizeimage = PAGE_ALIGN(max_t(u32, PAGE_SIZE, width * height));
-	} else {
-		ret = v4l2_fill_pixfmt_mp(pix_mp, fmt_default->fourcc, width, height);
-		if (ret)
-			return ret;
-	}
+	ret = mali_vpu_fill_pixmp_fmt(fmt_default, width, height, pix_mp);
+	if (ret)
+		return ret;
 
 	pix_mp->field = V4L2_FIELD_NONE;
 
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +9 -9
index b993255b8c817..dd142527a1a0c 100644
@@ -7,6 +7,7 @@
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <media/v4l2-common.h>
 #include <media/v4l2-dev.h>
 #include <media/v4l2-fh.h>
 #include <media/v4l2-ioctl.h>
@@ -168,32 +169,119 @@ static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt)
 	return ret;
 }
 
-static const struct mali_vpu_format_desc *mali_vpu_find_format(struct mali_vpu_session *sess,
-							       u32 pixfmt, u32 type)
+static int mali_vpu_try_format_common(struct mali_vpu_session *sess, struct v4l2_format *fmt)
 {
-	const struct mali_vpu_format_desc *fmt_list;
-	u32 num_fmts;
-	u32 i;
+	const struct mali_vpu_framesize_caps *fmsz = &sess->vpu_dev->variant->frmsz_caps;
+	struct v4l2_pix_format_mplane *pix_mp = &fmt->fmt.pix_mp;
+	const struct mali_vpu_format_desc *vpu_fmt;
+	const struct v4l2_format *port_fmt, *port_fmt_dec;
+	struct vb2_queue *src_q;
+	u32 w, h;
+	int ret = 0;
 
-	switch (type) {
-	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
-		fmt_list = sess->port_src.supported_fmts.fmts;
-		num_fmts = sess->port_src.supported_fmts.num_fmts;

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 184

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +3 -7
-		break;
-	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
-		fmt_list = sess->port_dst.supported_fmts.fmts;
-		num_fmts = sess->port_dst.supported_fmts.num_fmts;
-		break;
-	default:
-		return NULL;
+	vpu_fmt = mali_vpu_find_format(sess, pix_mp->pixelformat, fmt->type);
+	if (!vpu_fmt) {
+		port_fmt = mali_vpu_get_port_fmt(sess, fmt->type);

Maybe it would be better called get_default_port_fmt() ?

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 194

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +10 -0
+		if (!port_fmt)
+			return -EINVAL;
+
+		*pix_mp = port_fmt->fmt.pix_mp;
+		return 0;
+	}
+
+	/* Requested format found, validate other fields */
+	w = pix_mp->width ? pix_mp->width : fmsz->width_default;
+	h = pix_mp->height ? pix_mp->height : fmsz->height_default;

No, width = 0 should return min_width, same for height, just drop, and let the below function do that. The default width/height are what you set on a fresh session.

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 205

Quoted 11 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +11 -0
+
+	v4l2_apply_frmsize_constraints(&w, &h, &fmsz->stepwise);
+
+	/* Decode CAPTURE queue: lock geometry to input if streaming */
+	if (sess->role == MALI_VPU_DECODER && fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		src_q = v4l2_m2m_get_src_vq(sess->m2m_ctx);
+		if (vb2_is_streaming(src_q)) {
+			port_fmt_dec = &sess->port_src.curr_fmt;
+			w = port_fmt_dec->fmt.pix_mp.width;
+			h = port_fmt_dec->fmt.pix_mp.height;
+		}

This is try_fmt, it should not care if you are streaming or not.

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 213

Quoted 8 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +8 -0
+	}
+
+	ret = mali_vpu_fill_pixmp_fmt(vpu_fmt, w, h, pix_mp);
+	if (ret)
+		return ret;
+
+	if (pix_mp->field == V4L2_FIELD_ANY)
+		pix_mp->field = V4L2_FIELD_NONE;

Well, only if you support interlaced.

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 223

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +10 -0
+
+	return 0;
+}
+
+static int mali_vpu_try_fmt(struct file *filp, void *fh, struct v4l2_format *fmt)
+{
+	struct mali_vpu_session *sess = mali_vpu_get_session(filp);
+	int ret;
+
+	mutex_lock(&sess->vq_lock);

This is suspicious, the vb queue already do some locking in most of the vidioc call.

Quoted 79 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +61 -4
+
+	ret = mali_vpu_try_format_common(sess, fmt);
+
+	mutex_unlock(&sess->vq_lock);
+	return ret;
+}
+
+static int mali_vpu_s_fmt(struct file *filp, void *fh, struct v4l2_format *fmt)
+{
+	struct mali_vpu_session *sess = mali_vpu_get_session(filp);
+	struct vb2_queue *vq;
+	struct v4l2_pix_format_mplane *pixmp_src = &sess->port_src.curr_fmt.fmt.pix_mp;
+	struct v4l2_pix_format_mplane *pixmp_dst = &sess->port_dst.curr_fmt.fmt.pix_mp;
+	int ret = 0;
+
+	mutex_lock(&sess->vq_lock);
+
+	vq = v4l2_m2m_get_vq(sess->m2m_ctx, fmt->type);
+	if (vb2_is_busy(vq)) {
+		ret = -EBUSY;
+		goto mutex_unlock_return;
 	}
 
-	for (i = 0; i < num_fmts; i++) {
-		if (fmt_list[i].fourcc == pixfmt)
-			return &fmt_list[i];
+	ret = mali_vpu_try_format_common(sess, fmt);
+	if (ret)
+		goto mutex_unlock_return;
+
+	/* OUTPUT provides initial CAPTURE geometry */
+	if (V4L2_TYPE_IS_OUTPUT(fmt->type)) {
+		const struct mali_vpu_format_desc *cap_fmt;
+		struct v4l2_pix_format_mplane *cap;
+		struct v4l2_pix_format_mplane *fmt_mp = &fmt->fmt.pix_mp;
+
+		cap = &sess->port_dst.curr_fmt.fmt.pix_mp;
+		cap_fmt = mali_vpu_find_format(sess, cap->pixelformat,
+					       V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
+		if (!cap_fmt) {
+			ret = -EINVAL;
+			goto mutex_unlock_return;
+		}
+
+		ret = mali_vpu_fill_pixmp_fmt(cap_fmt, fmt_mp->width, fmt_mp->height, cap);
+		if (ret)
+			goto mutex_unlock_return;
 	}
 
-	return NULL;
+	if (V4L2_TYPE_IS_OUTPUT(fmt->type))
+		sess->port_src.curr_fmt = *fmt;
+	else
+		sess->port_dst.curr_fmt = *fmt;
+
+	/* Keep peer queue colorimetry aligned with input */
+	if (V4L2_TYPE_IS_OUTPUT(fmt->type)) {
+		pixmp_dst->colorspace	= pixmp_src->colorspace;
+		pixmp_dst->ycbcr_enc	= pixmp_src->ycbcr_enc;
+		pixmp_dst->quantization = pixmp_src->quantization;
+		pixmp_dst->xfer_func	= pixmp_src->xfer_func;
+	}
+mutex_unlock_return:
+	mutex_unlock(&sess->vq_lock);
+	return ret;
 }
 
 static int mali_vpu_validate_format(struct mali_vpu_session *sess, u32 pixfmt)
@@ -236,6 +324,10 @@ const struct v4l2_ioctl_ops mali_vpu_ioctl_ops_dec = {
 	.vidioc_enum_fmt_vid_cap	= mali_vpu_enum_fmt,
 	.vidioc_g_fmt_vid_out_mplane	= mali_vpu_g_fmt,
 	.vidioc_g_fmt_vid_cap_mplane	= mali_vpu_g_fmt,
+	.vidioc_s_fmt_vid_out_mplane	= mali_vpu_s_fmt,
+	.vidioc_s_fmt_vid_cap_mplane	= mali_vpu_s_fmt,
+	.vidioc_try_fmt_vid_out_mplane	= mali_vpu_try_fmt,
+	.vidioc_try_fmt_vid_cap_mplane	= mali_vpu_try_fmt,
 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
#22 lore raw

Re: [RFC PATCH 07/28] media: mali-vpu: implement TRY_FMT and S_FMT ioctls

Quoted 23 lines · Nicolas Dufresne
Quoted 23 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c new file +23 -0
+int mali_vpu_fill_pixmp_fmt(const struct mali_vpu_format_desc *fmt,
+			    u32 width, u32 height,
+			    struct v4l2_pix_format_mplane *pix_mp)
+{
+	u32 sizeimage;
+	int ret;
+
+	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
+	memset(pix_mp->plane_fmt, 0, sizeof(pix_mp->plane_fmt));
+	pix_mp->flags = 0;
+
+	if (fmt->flags & V4L2_FMT_FLAG_COMPRESSED) {
+		pix_mp->width = width;
+		pix_mp->height = height;
+		pix_mp->pixelformat = fmt->fourcc;
+		pix_mp->num_planes = 1;
+		pix_mp->plane_fmt[0].bytesperline = 0;
+
+		/*
+		 * Use one byte per pixel as the default compressed sizeimage,
+		 * with a minimum of one page, aligned to a page boundary.
+		 */
+		if (check_mul_overflow(width, height, &sizeimage))
Quoted 8 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c line 70

Quoted 6 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c new file +6 -0
+			return -EOVERFLOW;
+
+		sizeimage = max_t(u32, PAGE_SIZE, sizeimage);
+
+		if (sizeimage > U32_MAX - (PAGE_SIZE - 1))
+			return -EOVERFLOW;

The "moved" code is not exactly identical, and this is not said in the commit message. Depending on you max_width/height, this might all be exaggerated.

The checks were intended as defensive handling in a common helper for future variants. However, given the frame-size constraints applied before this calculation, they are overly defensive. I’ll remove them and preserve the original calculation.

Quoted 16 lines
Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +0 -4
-	switch (type) {
-	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
-		fmt_list = sess->port_src.supported_fmts.fmts;
-		num_fmts = sess->port_src.supported_fmts.num_fmts;

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 184

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +3 -7
-		break;
-	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
-		fmt_list = sess->port_dst.supported_fmts.fmts;
-		num_fmts = sess->port_dst.supported_fmts.num_fmts;
-		break;
-	default:
-		return NULL;
+	vpu_fmt = mali_vpu_find_format(sess, pix_mp->pixelformat, fmt->type);
+	if (!vpu_fmt) {
+		port_fmt = mali_vpu_get_port_fmt(sess, fmt->type);

Maybe it would be better called get_default_port_fmt() ?

This helper actually returns the current port format, and it is also used by G_FMT. On a fresh session that contains the defaults, but it subsequently contains the negotiated format. I’ll rename it to mali_vpu_get_current_port_fmt() to make that clear.

Quoted 5 lines
Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +5 -0
+		if (!port_fmt)
+			return -EINVAL;
+
+		*pix_mp = port_fmt->fmt.pix_mp;
+		return 0;
Quoted 8 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 194

Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +5 -0
+	}
+
+	/* Requested format found, validate other fields */
+	w = pix_mp->width ? pix_mp->width : fmsz->width_default;
+	h = pix_mp->height ? pix_mp->height : fmsz->height_default;

No, width = 0 should return min_width, same for height, just drop, and let the below function do that. The default width/height are what you set on a fresh session.

Agreed. In v2, I’ll pass the requested width and height directly to v4l2_apply_frmsize_constraints(), and let it adjust zero to the supported minimum.

Quoted 4 lines
Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +4 -0
+
+	v4l2_apply_frmsize_constraints(&w, &h, &fmsz->stepwise);
+
+	/* Decode CAPTURE queue: lock geometry to input if streaming */
Quoted 8 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 205

Quoted 7 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +7 -0
+	if (sess->role == MALI_VPU_DECODER && fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+		src_q = v4l2_m2m_get_src_vq(sess->m2m_ctx);
+		if (vb2_is_streaming(src_q)) {
+			port_fmt_dec = &sess->port_src.curr_fmt;
+			w = port_fmt_dec->fmt.pix_mp.width;
+			h = port_fmt_dec->fmt.pix_mp.height;
+		}

This is try_fmt, it should not care if you are streaming or not.

The intention was to use the parsed CAPTURE dimensions once available, but OUTPUT streaming does not guarantee that parsing has completed. I’ll track whether firmware has supplied the parameters instead I'll keep the adjustment logic shared with S_FMT, so both return the same format for the same request and known stream parameters.

Quoted 9 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 213

Quoted 8 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +8 -0
+	}
+
+	ret = mali_vpu_fill_pixmp_fmt(vpu_fmt, w, h, pix_mp);
+	if (ret)
+		return ret;
+
+	if (pix_mp->field == V4L2_FIELD_ANY)
+		pix_mp->field = V4L2_FIELD_NONE;

Well, only if you support interlaced.

Since I’m adding interlaced decoding support in v2, I’ll normalize field to V4L2_FIELD_NONE on the compressed OUTPUT queue and derive the CAPTURE field layout from the stream information reported by firmware.

Quoted 8 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c static int mali_vpu_g_fmt(struct file *filp, void *fh, struct v4l2_format *fmt) · line 223

Quoted 6 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +6 -0
+static int mali_vpu_try_fmt(struct file *filp, void *fh, struct v4l2_format *fmt)
+{
+	struct mali_vpu_session *sess = mali_vpu_get_session(filp);
+	int ret;
+
+	mutex_lock(&sess->vq_lock);

This is suspicious, the vb queue already do some locking in most of the vidioc call.

Both queues assign &sess->vq_lock to vb2_queue::lock, but video_device::lock is unset. The V4L2 core acquires it for queue-management ioctls marked INFO_FL_QUEUE, but not for G_FMT, TRY_FMT or S_FMT.

video_device::lock is unset, as this driver uses per-session locking and format ioctls are not required to be serialized among different sessions. The explicit locks therefore protect format access against queue operations and asynchronous firmware updates, which use the same mutex. They do not reacquire a lock already held by the ioctl core.

Regards,
Charvi

#23 lore raw

Re: [RFC PATCH 07/28] media: mali-vpu: implement TRY_FMT and S_FMT ioctls

Le lundi 23 novembre 2026 à 17:26 +0000, Charvi Mehta a écrit :

Quoted 7 lines
Quoted 2 lines

This is suspicious, the vb queue already do some locking in most of the vidioc call.

Both queues assign &sess->vq_lock to vb2_queue::lock, but video_device::lock
is unset. The V4L2 core acquires it for queue-management ioctls marked INFO_FL_QUEUE, but not for G_FMT, TRY_FMT or S_FMT.

Quoted 5 lines

video_device::lock is unset, as this driver uses per-session locking and format ioctls are not required to be serialized among different sessions. The explicit locks therefore protect format access against queue operations and asynchronous firmware updates, which use the same mutex. They do not reacquire a lock already held by the ioctl core.

I missed that in the review, please document carefully your locking somewhere in text.

Nicolas

#24 lore raw

Re: [RFC PATCH 07/28] media: mali-vpu: implement TRY_FMT and S_FMT ioctls

Quoted 7 lines · Nicolas Dufresne
Quoted 7 lines
Quoted 2 lines

This is suspicious, the vb queue already do some locking in most of the vidioc call.

Both queues assign &sess->vq_lock to vb2_queue::lock, but video_device::lock
is unset. The V4L2 core acquires it for queue-management ioctls marked INFO_FL_QUEUE, but not for G_FMT, TRY_FMT or S_FMT.

Quoted 7 lines
Quoted 5 lines

video_device::lock is unset, as this driver uses per-session locking and format ioctls are not required to be serialized among different sessions. The explicit locks therefore protect format access against queue operations and asynchronous firmware updates, which use the same mutex. They do not reacquire a lock already held by the ioctl core.

I missed that in the review, please document carefully your locking somewhere in text.

The session mutex is already documented in mali-vpu-session.h. I’ll expand that description to cover format state and asynchronous firmware updates.

Regards,
Charvi

#25 lore raw

RFC PATCH 8/28 media: mali-vpu: register V76 decoder controls and introduce UAPI controls

In reply to Charvi Mehta

Register a V4L2 control handler for each decoder session and attach it to the session file handle. Describe the controls supported by V76 in the variant data and maintain their values in per-session state.

Add Mali VPU userspace controls for NALU layout, stream escaping, intermediate buffer size, stream-header handling, frame reordering and firmware debug output. Also expose the standard read-only V4L2_CID_MIN_BUFFERS_FOR_CAPTURE control.

Stage control values set before the firmware is ready and reject changes to non-dynamic controls while the OUTPUT queue is streaming. Allow userspace to subscribe to control events.

---
 drivers/media/platform/arm/mali-vpu/Makefile  |   3 +-
 .../platform/arm/mali-vpu/mali-vpu-controls.c | 339 ++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-controls.h |  71 ++++
 .../platform/arm/mali-vpu/mali-vpu-dec.c      |  44 +++
 .../platform/arm/mali-vpu/mali-vpu-dec.h      |   6 +
 .../platform/arm/mali-vpu/mali-vpu-session.c  |  15 +
 .../platform/arm/mali-vpu/mali-vpu-session.h  |  14 +
 .../arm/mali-vpu/mali-vpu-variant-v76.c       |  99 +++++
 .../platform/arm/mali-vpu/mali-vpu-variant.h  |  13 +
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   |  19 +
 .../uapi/linux/media/arm/mali-vpu-config.h    |  47 +++
 include/uapi/linux/v4l2-controls.h            |   6 +
 12 files changed, 675 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-controls.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-controls.h
 create mode 100644 include/uapi/linux/media/arm/mali-vpu-config.h
drivers/media/platform/arm/mali-vpu/Makefile +2 -1
index 1dc62621d9dc0..95bad821eb466 100644
@@ -7,6 +7,7 @@ mali-vpu-objs := mali-vpu-probe.o \
 		mali-vpu-dec.o \
 		mali-vpu-buffer.o \
 		mali-vpu-vb2.o \
-		mali-vpu-formats.o
+		mali-vpu-formats.o \
+		mali-vpu-controls.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-controls.c new file +339 -0

339 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-controls.h new file +71 -0

71 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c +44 -0

54 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-dec.h +6 -0
index 2514c897587c4..e323b38475c81 100644
@@ -6,8 +6,14 @@
 #ifndef __MALI_VPU_DEC_H__
 #define __MALI_VPU_DEC_H__
 
+#include <linux/types.h>
+
 struct mali_vpu_session;
 
 void mali_vpu_session_init_dec(struct mali_vpu_session *sess);
 
+/* Decoder controls setters */
+int mali_vpu_ctrl_set_ignore_stream_headers(struct mali_vpu_session *sess, u32 cid, s64 value);
+int mali_vpu_ctrl_set_frame_reordering(struct mali_vpu_session *sess, u32 cid, s64 value);
+
 #endif /* __MALI_VPU_DEC_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +15 -0
index 6867f51287f48..d1e8680de5556 100644
@@ -3,12 +3,14 @@
  * Copyright (C) 2026 ARM Limited.
  */
 
+#include <linux/device.h>
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/string.h>
 #include <media/v4l2-mem2mem.h>
 
 #include "mali-vpu-session.h"
+#include "mali-vpu-controls.h"
 #include "mali-vpu-dec.h"
 #include "mali-vpu-formats.h"
 #include "mali-vpu-variant.h"
@@ -76,8 +78,19 @@ int mali_vpu_session_init(struct mali_vpu_session *sess)
 		goto err_mutex_destroy;
 	}
 
+	sess->is_fw_ready = false;
+
+	ret = mali_vpu_ctrls_init(sess);
+	if (ret) {
+		dev_err(sess->vpu_dev->dev, "failed to initialise controls: %d\n", ret);
+		goto err_m2m_ctx_release;
+	}
+
 	return 0;
 
+err_m2m_ctx_release:
+	v4l2_m2m_ctx_release(sess->m2m_ctx);
+
 err_mutex_destroy:
 	mutex_destroy(&sess->vq_lock);
 	return ret;
@@ -94,5 +107,7 @@ void mali_vpu_session_cleanup(struct mali_vpu_session *sess)
 	}
 
 	mutex_unlock(&sess->vq_lock);
+
+	mali_vpu_ctrls_cleanup(sess);
 	mutex_destroy(&sess->vq_lock);
 }
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +14 -0
index f8a602cb59006..766912aec0b8e 100644
@@ -8,6 +8,7 @@
 
 #include <linux/mutex.h>
 #include <linux/videodev2.h>
+#include <media/v4l2-ctrls.h>
 #include <media/v4l2-fh.h>
 
 #include "mali-vpu-device.h"
@@ -32,6 +33,12 @@ struct mali_vpu_port {
  * @port_src: Source (OUTPUT) port for this session.
  * @port_dst: Destination (CAPTURE) port for this session.
  * @vq_lock: Mutex lock to serialise per-session queue related ioctls.
+ * @is_fw_ready: True if the firmware is loaded and ready to accept commands.
+ * @min_buffers_required: Driver-reported minimum buffers for role-specific queue.
+ * @ctrls_caps: Variant-provided immutable control capability table.
+ * @num_ctrls_caps: Number of entries in @ctrls_caps.
+ * @ctrls_state: Per-session control cache and staging state.
+ * @ctrl_handler: V4L2 control handler bound to this session.
  * @fh: V4L2 file handle for this session.
  * @m2m_ctx: V4L2 mem2mem context for this session.
  */
@@ -41,6 +48,13 @@ struct mali_vpu_session {
 	struct mali_vpu_port		port_src;
 	struct mali_vpu_port		port_dst;
 	struct mutex			vq_lock; /* lock to serialise per-session ioctls */
+	bool				is_fw_ready;
+	u32				min_buffers_required;
+
+	const struct mali_vpu_ctrl_desc *ctrls_caps;
+	u32				num_ctrls_caps;
+	struct mali_vpu_ctrl_state	*ctrls_state;
+	struct v4l2_ctrl_handler	ctrl_handler;
 
 	struct v4l2_fh		fh;
 	struct v4l2_m2m_ctx	*m2m_ctx;
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +99 -0

117 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h +13 -0
index a46c25a94f18b..ad20896c90b0f 100644
@@ -9,6 +9,7 @@
 #include <linux/types.h>
 #include <linux/videodev2.h>
 
+#include "mali-vpu-controls.h"
 #include "mali-vpu-formats.h"
 
 /**
@@ -33,6 +34,16 @@ struct mali_vpu_format_caps {
 	const struct mali_vpu_format_list dec_dst;
 };
 
+/**
+ * struct mali_vpu_control_caps - Control capability tables per role.
+ * @dec_ctrls: Array of controls supported by the decoder.
+ * @num_dec_ctrls: Number of entries in @dec_ctrls.
+ */
+struct mali_vpu_control_caps {
+	const struct mali_vpu_ctrl_desc *dec_ctrls;
+	u32 num_dec_ctrls;
+};
+
 /**
  * struct mali_vpu_variant - Variant-specific hardware capability descriptor.
  * @rst_names: Array of reset control names for this variant.
@@ -40,6 +51,7 @@ struct mali_vpu_format_caps {
  * @expected_hw_pid: Expected hardware product ID read from the version register.
  * @fmt_caps: Format capability tables for encode/decode paths.
  * @frmsz_caps: Frame dimension limits and defaults.
+ * @ctrl_caps:  Control capability tables for encode/decode paths.
  */
 struct mali_vpu_variant {
 	const char * const *rst_names;
@@ -48,6 +60,7 @@ struct mali_vpu_variant {
 
 	struct mali_vpu_format_caps	fmt_caps;
 	struct mali_vpu_framesize_caps	frmsz_caps;
+	struct mali_vpu_control_caps	ctrl_caps;
 };
 
 extern const struct mali_vpu_variant mali_vpu_v76_variant;
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +19 -0
index dd142527a1a0c..88e706be6705e 100644
@@ -8,7 +8,9 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <media/v4l2-common.h>
+#include <media/v4l2-ctrls.h>
 #include <media/v4l2-dev.h>
+#include <media/v4l2-event.h>
 #include <media/v4l2-fh.h>
 #include <media/v4l2-ioctl.h>
 #include <media/v4l2-mem2mem.h>
@@ -318,6 +320,21 @@ static int mali_vpu_enum_framesizes(struct file *filp, void *fh,
 	return 0;
 }
 
+static int mali_vpu_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
+{
+	switch (sub->type) {
+	case V4L2_EVENT_CTRL:
+		return v4l2_ctrl_subscribe_event(fh, sub);
+	case V4L2_EVENT_EOS:
+		return v4l2_event_subscribe(fh, sub, 2, NULL);
+	case V4L2_EVENT_SOURCE_CHANGE:
+		return v4l2_event_subscribe(fh, sub, 2, NULL);
+
+	default:
+		return -EINVAL;
+	}
+}
+
 const struct v4l2_ioctl_ops mali_vpu_ioctl_ops_dec = {
 	.vidioc_querycap		= mali_vpu_querycap,
 	.vidioc_enum_fmt_vid_out	= mali_vpu_enum_fmt,
@@ -337,4 +354,6 @@ const struct v4l2_ioctl_ops mali_vpu_ioctl_ops_dec = {
 	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
 	.vidioc_remove_bufs		= v4l2_m2m_ioctl_remove_bufs,
 	.vidioc_enum_framesizes		= mali_vpu_enum_framesizes,
+	.vidioc_subscribe_event		= mali_vpu_subscribe_event,
+	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
 };
include/uapi/linux/media/arm/mali-vpu-config.h new file +47 -0

47 lines not on this page · raw message

include/uapi/linux/v4l2-controls.h +6 -0
index d17e41d51d2ec..b9f7bc3aac1de 100644
@@ -255,6 +255,12 @@ enum v4l2_colorfx {
  */
 #define V4L2_CID_USER_IT6625_BASE		(V4L2_CID_USER_BASE + 0x1250)
 
+/*
+ * The base for Arm Mali-VPU driver controls.
+ * We reserve 64 controls for this driver.
+ */
+#define V4L2_CID_USER_MALI_VPU_BASE		(V4L2_CID_USER_BASE + 0x1260)
+
 /* MPEG-class control IDs */
 /* The MPEG controls are applicable to all codec controls
  * and the 'MPEG' part of the define is historical */
Signature · 1 line
2.43.0
#26 lore raw

Re: [RFC PATCH 08/28] media: mali-vpu: register V76 decoder controls and introduce UAPI controls

Le jeudi 19 novembre 2026 à 15:37 +0000, Charvi Mehta a écrit :

Quoted 8 lines

Register a V4L2 control handler for each decoder session and attach it to the session file handle. Describe the controls supported by V76 in the variant data and maintain their values in per-session state.

Add Mali VPU userspace controls for NALU layout, stream escaping, intermediate buffer size, stream-header handling, frame reordering and firmware debug output. Also expose the standard read-only V4L2_CID_MIN_BUFFERS_FOR_CAPTURE control.

I would strongly recommend to keep each vendor control in its own patch. These are API, and they need documentation. In practice, you'll prefer to not introduce vendor control as a first introduction of your driver, its just a bad idea. You completely missed Documentation/

Quoted 660 lines

Stage control values set before the firmware is ready and reject changes to non-dynamic controls while the OUTPUT queue is streaming. Allow userspace to subscribe to control events.

---
 drivers/media/platform/arm/mali-vpu/Makefile  |   3 +-
 .../platform/arm/mali-vpu/mali-vpu-controls.c | 339 ++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-controls.h |  71 ++++
 .../platform/arm/mali-vpu/mali-vpu-dec.c      |  44 +++
 .../platform/arm/mali-vpu/mali-vpu-dec.h      |   6 +
 .../platform/arm/mali-vpu/mali-vpu-session.c  |  15 +
 .../platform/arm/mali-vpu/mali-vpu-session.h  |  14 +
 .../arm/mali-vpu/mali-vpu-variant-v76.c       |  99 +++++
 .../platform/arm/mali-vpu/mali-vpu-variant.h  |  13 +
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   |  19 +
 .../uapi/linux/media/arm/mali-vpu-config.h    |  47 +++
 include/uapi/linux/v4l2-controls.h            |   6 +
 12 files changed, 675 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-controls.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-controls.h
 create mode 100644 include/uapi/linux/media/arm/mali-vpu-config.h
drivers/media/platform/arm/mali-vpu/Makefile +2 -1
index 1dc62621d9dc0..95bad821eb466 100644
@@ -7,6 +7,7 @@ mali-vpu-objs := mali-vpu-probe.o \
 		mali-vpu-dec.o \
 		mali-vpu-buffer.o \
 		mali-vpu-vb2.o \
-		mali-vpu-formats.o
+		mali-vpu-formats.o \
+		mali-vpu-controls.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-controls.c new file +339 -0
new file mode 100644
index 0000000000000..d4921eddf0fcf
@@ -0,0 +1,339 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#include <linux/errno.h>
+#include <linux/limits.h>
+#include <linux/slab.h>
+#include <media/v4l2-mem2mem.h>
+
+#include <linux/media/arm/mali-vpu-config.h>
+
+#include "mali-vpu-controls.h"
+#include "mali-vpu-session.h"
+
+static int mali_vpu_ctrl_slot_from_cid(const struct mali_vpu_session *sess,
+				       u32 cid, u32 *slot)
+{
+	const struct mali_vpu_ctrl_state *ctrl_state = sess->ctrls_state;
+	u32 i;
+
+	for (i = 0; i < sess->num_ctrls_caps; i++) {
+		if (ctrl_state[i].cid == cid) {
+			*slot = i;
+			return 0;
+		}
+	}
+
+	return -EINVAL;
+}
+
+static int mali_vpu_get_volatile_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct mali_vpu_session *sess = container_of(ctrl->handler,
+						     struct mali_vpu_session,
+						     ctrl_handler);
+
+	switch (ctrl->id) {
+	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
+		if (sess->role != MALI_VPU_DECODER)
+			return -EINVAL;
+		ctrl->val = sess->min_buffers_required;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int mali_vpu_apply_or_stage_ctrl(struct mali_vpu_session *sess,
+					const struct mali_vpu_ctrl_desc *desc,
+					struct mali_vpu_ctrl_state *state,
+					s64 value)
+{
+	bool staged;
+	int ret;
+
+	staged = !sess->is_fw_ready;
+
+	if (!staged) {
+		ret = desc->set(sess, state->cid, value);
+		if (ret)
+			return ret;
+	}
+
+	state->value = value;
+	state->client_set = true;
+	state->is_staged = staged;
+
+	return 0;
+}
+
+static int mali_vpu_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct mali_vpu_session *sess = container_of(ctrl->handler,
+						     struct mali_vpu_session,
+						     ctrl_handler);
+	const struct mali_vpu_ctrl_desc *desc;
+	struct mali_vpu_ctrl_state *state;
+	struct vb2_queue *src_vq;
+	u32 slot;
+	int ret;
+
+	ret = mali_vpu_ctrl_slot_from_cid(sess, ctrl->id, &slot);
+	if (ret)
+		return ret;
+
+	desc = &sess->ctrls_caps[slot];
+	state = &sess->ctrls_state[slot];
+
+	src_vq = v4l2_m2m_get_src_vq(sess->m2m_ctx);
+	if (vb2_is_streaming(src_vq) &&
+	    !(desc->flags & MALI_VPU_CTRL_FLAG_DYNAMIC))
+		return -EBUSY;
+
+	return mali_vpu_apply_or_stage_ctrl(sess, desc, state, ctrl->val);
+}
+
+static const struct v4l2_ctrl_ops mali_vpu_ctrl_ops = {
+	.g_volatile_ctrl = mali_vpu_get_volatile_ctrl,
+	.s_ctrl          = mali_vpu_set_ctrl,
+};
+
+static int mali_vpu_register_control(struct mali_vpu_session *sess,
+				     const struct mali_vpu_ctrl_desc *d)
+{
+	struct v4l2_ctrl *ctrl;
+
+	if (d->is_custom) {
+		struct v4l2_ctrl_config cfg = {
+			.ops = &mali_vpu_ctrl_ops,
+			.id = d->cid,
+			.name = d->name,
+			.type = d->type,
+			.min = d->min_val,
+			.max = d->max_val,
+			.step = (d->type == V4L2_CTRL_TYPE_MENU) ? 0 : d->step_or_mask,
+			.def = d->default_val,
+			.menu_skip_mask = (d->type == V4L2_CTRL_TYPE_MENU) ? ~(d->step_or_mask) : 0,
+			.qmenu = d->qmenu,
+		};
+
+		ctrl = v4l2_ctrl_new_custom(&sess->ctrl_handler, &cfg, NULL);
+
+	} else if (d->type == V4L2_CTRL_TYPE_MENU) {
+		ctrl = v4l2_ctrl_new_std_menu(&sess->ctrl_handler,
+					      &mali_vpu_ctrl_ops,
+					      d->cid,
+					      d->max_val,
+					      ~(d->step_or_mask),
+					      d->default_val);
+	} else {
+		ctrl = v4l2_ctrl_new_std(&sess->ctrl_handler,
+					 &mali_vpu_ctrl_ops,
+					 d->cid,
+					 d->min_val,
+					 d->max_val,
+					 d->step_or_mask,
+					 d->default_val);
+	}
+
+	if (!ctrl || sess->ctrl_handler.error)
+		return sess->ctrl_handler.error ? sess->ctrl_handler.error : -EINVAL;
+
+	return 0;
+}
+
+static int mali_vpu_validate_ctrl_caps(const struct mali_vpu_session *sess)
+{
+	u32 i, j;
+
+	if (!sess->ctrls_caps || !sess->num_ctrls_caps) {
+		dev_err(sess->vpu_dev->dev, "invalid ctrl caps structure\n");
+		return -EINVAL;
+	}
+
+	for (i = 0; i < sess->num_ctrls_caps; i++) {
+		const struct mali_vpu_ctrl_desc *d = &sess->ctrls_caps[i];
+
+		if (!d->cid || !d->set)
+			return -EINVAL;
+
+		if (d->min_val > d->max_val)
+			return -EINVAL;
+
+		if (d->default_val < d->min_val || d->default_val > d->max_val)
+			return -EINVAL;
+
+		if (d->is_custom && !d->name)
+			return -EINVAL;
+
+		if (d->is_custom && d->type == V4L2_CTRL_TYPE_MENU && !d->qmenu)
+			return -EINVAL;
+
+		/* Reject duplicate entries */
+		for (j = i + 1; j < sess->num_ctrls_caps; j++) {
+			if (d->cid == sess->ctrls_caps[j].cid)
+				return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+
+int mali_vpu_ctrls_init(struct mali_vpu_session *sess)
+{
+	const struct mali_vpu_ctrl_desc *ctrls_cap = sess->ctrls_caps;
+	struct mali_vpu_ctrl_state *ctrl_state;
+	struct v4l2_ctrl *volatile_ctrl;
+	u32 i;
+	int ret;
+
+	ret = mali_vpu_validate_ctrl_caps(sess);
+	if (ret)
+		return ret;
+
+	sess->ctrls_state = kcalloc(sess->num_ctrls_caps, sizeof(*sess->ctrls_state), GFP_KERNEL);
+	if (!sess->ctrls_state)
+		return -ENOMEM;
+
+	/*
+	 * Adding 1 to num_ctrls to include
+	 * V4L2_CID_MIN_BUFFERS_FOR_CAPTURE for decoder
+	 */
+	ret = v4l2_ctrl_handler_init(&sess->ctrl_handler, sess->num_ctrls_caps + 1);
+	if (ret)
+		goto err_free_state;
+
+	sess->ctrl_handler.lock = &sess->vq_lock;
+
+	ctrl_state = sess->ctrls_state;
+
+	for (i = 0; i < sess->num_ctrls_caps; i++) {
+		const struct mali_vpu_ctrl_desc *d = &ctrls_cap[i];
+
+		ctrl_state[i].cid = d->cid;
+		ctrl_state[i].value = d->default_val;
+		ctrl_state[i].client_set = false;
+		ctrl_state[i].is_staged = false;
+
+		ret = mali_vpu_register_control(sess, d);
+		if (ret)
+			goto err_free_handler;
+	}
+
+	if (sess->role == MALI_VPU_DECODER) {
+		volatile_ctrl = v4l2_ctrl_new_std(&sess->ctrl_handler,
+						  &mali_vpu_ctrl_ops,
+						  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
+						  1, 32, 1, 1);
+	} else {
+		ret = -EINVAL;
+		goto err_free_handler;
+	}
+
+	if (!volatile_ctrl || sess->ctrl_handler.error) {
+		ret = sess->ctrl_handler.error ? sess->ctrl_handler.error : -EINVAL;
+		goto err_free_handler;
+	}
+
+	volatile_ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY;
+
+	sess->fh.ctrl_handler = &sess->ctrl_handler;
+
+	return 0;
+
+err_free_handler:
+	v4l2_ctrl_handler_free(&sess->ctrl_handler);
+
+err_free_state:
+	kfree(sess->ctrls_state);
+	sess->ctrls_state = NULL;
+	return ret;
+}
+
+void mali_vpu_ctrls_cleanup(struct mali_vpu_session *sess)
+{
+	v4l2_ctrl_handler_free(&sess->ctrl_handler);
+	kfree(sess->ctrls_state);
+	sess->ctrls_state = NULL;
+	sess->fh.ctrl_handler = NULL;
+}
+
+int mali_vpu_apply_staged_controls(struct mali_vpu_session *sess)
+{
+	u32 i;
+	int ret;
+
+	if (!sess->is_fw_ready)
+		return -EINVAL;
+
+	for (i = 0; i < sess->num_ctrls_caps; i++) {
+		const struct mali_vpu_ctrl_desc *d = &sess->ctrls_caps[i];
+		struct mali_vpu_ctrl_state *s = &sess->ctrls_state[i];
+
+		if (!s->client_set || !s->is_staged)
+			continue;
+
+		ret = mali_vpu_apply_or_stage_ctrl(sess, d, s, s->value);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+int mali_vpu_ctrl_set_nalu_format(struct mali_vpu_session *sess, u32 cid,
+				  s64 value)
+{
+	if (cid != V4L2_CID_MALI_VPU_NALU_FORMAT)
+		return -EINVAL;
+
+	(void)sess;
+	(void)value;
+	return 0;
+}
+
+int mali_vpu_ctrl_set_intbuf_size(struct mali_vpu_session *sess, u32 cid,
+				  s64 value)
+{
+	if (cid != V4L2_CID_MALI_VPU_INTBUF_SIZE || value < 0 || value > INT_MAX)
+		return -EINVAL;
+
+	(void)sess;
+	(void)value;
+	return 0;
+}
+
+int mali_vpu_ctrl_set_stream_escaping(struct mali_vpu_session *sess, u32 cid,
+				      s64 value)
+{
+	if (cid != V4L2_CID_MALI_VPU_STREAM_ESCAPING)
+		return -EINVAL;
+
+	if (value == -1)
+		return 0; /* tristate no-op */
+
+	if (value != 0 && value != 1)
+		return -EINVAL;
+
+	(void)sess;
+	(void)value;
+	return 0;
+}
+
+int mali_vpu_ctrl_set_host_print_enable(struct mali_vpu_session *sess, u32 cid,
+					s64 value)
+{
+	if (cid != V4L2_CID_MALI_VPU_HOST_PRINT_ENABLE)
+		return -EINVAL;
+
+	if (value != 0 && value != 1)
+		return -EINVAL;
+
+	(void)sess;
+	(void)value;
+	return 0;
+}
drivers/media/platform/arm/mali-vpu/mali-vpu-controls.h new file +71 -0
new file mode 100644
index 0000000000000..d7f5898963478
@@ -0,0 +1,71 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2026 ARM Limited.
+ */
+
+#ifndef __MALI_VPU_CONTROLS_H__
+#define __MALI_VPU_CONTROLS_H__
+
+#include <linux/bits.h>
+#include <linux/types.h>
+#include <media/v4l2-ctrls.h>
+
+struct mali_vpu_session;
+
+enum mali_vpu_ctrl_flags {
+	MALI_VPU_CTRL_FLAG_DYNAMIC	= BIT(0),
+};
+
+/**
+ * struct mali_vpu_ctrl_desc - static description of one driver control.
+ * @cid: V4L2 control ID.
+ * @type: V4L2 control type.
+ * @name: Control name used for custom registration. NULL for standard controls.
+ * @qmenu: Menu labels for V4L2_CTRL_TYPE_MENU custom controls.
+ * @min_val: Minimum allowed value for this control.
+ * @max_val: Maximum allowed value for this control.
+ * @default_val: Default value for this control at session init.
+ * @step_or_mask: Step for integer controls, or enabled-item mask for menu controls.
+ * @is_custom: Flag to indicate if a control is a custom Mali VPU control.
+ * @flags: Driver-specific behavior flags from enum mali_vpu_ctrl_flags.
+ * @set: Setter callback invoked on client control changes, to apply value to firmware.
+ */
+struct mali_vpu_ctrl_desc {
+	u32 cid;
+	enum v4l2_ctrl_type type;
+	const char *name;
+	const char * const *qmenu;
+	s64 min_val;
+	s64 max_val;
+	s64 default_val;
+	u64 step_or_mask;
+	bool is_custom;
+	enum mali_vpu_ctrl_flags flags;
+	int (*set)(struct mali_vpu_session *sess, u32 cid, s64 value);
+};
+
+/**
+ * struct mali_vpu_ctrl_state - per-session runtime state of one driver control.
+ * @cid: V4L2 control ID.
+ * @value: Last accepted value for the control (from userspace or default at init).
+ * @client_set: True if userspace explicitly set this control at least once.
+ * @is_staged: True if value is accepted but not yet sent to firmware.
+ */
+struct mali_vpu_ctrl_state {
+	u32 cid;
+	s64 value;
+	bool client_set;
+	bool is_staged;
+};
+
+int mali_vpu_ctrls_init(struct mali_vpu_session *sess);
+void mali_vpu_ctrls_cleanup(struct mali_vpu_session *sess);
+int mali_vpu_apply_staged_controls(struct mali_vpu_session *sess);
+
+/* Common controls setters */
+int mali_vpu_ctrl_set_nalu_format(struct mali_vpu_session *sess, u32 cid, s64 value);
+int mali_vpu_ctrl_set_intbuf_size(struct mali_vpu_session *sess, u32 cid, s64 value);
+int mali_vpu_ctrl_set_stream_escaping(struct mali_vpu_session *sess, u32 cid, s64 value);
+int mali_vpu_ctrl_set_host_print_enable(struct mali_vpu_session *sess, u32 cid, s64 value);
+
+#endif /* __MALI_VPU_CONTROLS_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c +44 -0
index cb6eeee2f04df..7e78c475a4ff3 100644
@@ -3,6 +3,9 @@
  * Copyright (C) 2026 ARM Limited.
  */
 
+#include <linux/errno.h>
+#include <linux/media/arm/mali-vpu-config.h>
+
 #include "mali-vpu-dec.h"
 #include "mali-vpu-session.h"
 #include "mali-vpu-variant.h"
@@ -13,4 +16,45 @@ void mali_vpu_session_init_dec(struct mali_vpu_session *sess)
 
 	sess->port_src.supported_fmts = variant_fmts->dec_src;
 	sess->port_dst.supported_fmts = variant_fmts->dec_dst;
+
+	const struct mali_vpu_control_caps *variant_ctrls = &sess->vpu_dev->variant->ctrl_caps;
+
+	sess->ctrls_caps	= variant_ctrls->dec_ctrls;
+	sess->num_ctrls_caps	= variant_ctrls->num_dec_ctrls;
+
+	sess->min_buffers_required = 3;	/* For CAPTURE/dst queue */
+}
+
+int mali_vpu_ctrl_set_ignore_stream_headers(struct mali_vpu_session *sess,
+					    u32 cid, s64 value)
+{
+	if (cid != V4L2_CID_MALI_VPU_IGNORE_STREAM_HEADERS)
+		return -EINVAL;
+
+	if (value == -1)
+		return 0; /* tristate no-op */
+
+	if (value != 0 && value != 1)
+		return -EINVAL;
+
+	(void)sess;
+	(void)value;
+	return 0;
+}
+
+int mali_vpu_ctrl_set_frame_reordering(struct mali_vpu_session *sess, u32 cid,
+				       s64 value)
+{
+	if (cid != V4L2_CID_MALI_VPU_FRAME_REORDERING)
+		return -EINVAL;
+
+	if (value == -1)
+		return 0; /* tristate no-op */
+
+	if (value != 0 && value != 1)
+		return -EINVAL;
+
+	(void)sess;
+	(void)value;
+	return 0;
 }
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.h +6 -0
index 2514c897587c4..e323b38475c81 100644
@@ -6,8 +6,14 @@
 #ifndef __MALI_VPU_DEC_H__
 #define __MALI_VPU_DEC_H__
 
+#include <linux/types.h>
+
 struct mali_vpu_session;
 
 void mali_vpu_session_init_dec(struct mali_vpu_session *sess);
 
+/* Decoder controls setters */
+int mali_vpu_ctrl_set_ignore_stream_headers(struct mali_vpu_session *sess, u32 cid, s64 value);
+int mali_vpu_ctrl_set_frame_reordering(struct mali_vpu_session *sess, u32 cid, s64 value);
+
 #endif /* __MALI_VPU_DEC_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +15 -0
index 6867f51287f48..d1e8680de5556 100644
@@ -3,12 +3,14 @@
  * Copyright (C) 2026 ARM Limited.
  */
 
+#include <linux/device.h>
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/string.h>
 #include <media/v4l2-mem2mem.h>
 
 #include "mali-vpu-session.h"
+#include "mali-vpu-controls.h"
 #include "mali-vpu-dec.h"
 #include "mali-vpu-formats.h"
 #include "mali-vpu-variant.h"
@@ -76,8 +78,19 @@ int mali_vpu_session_init(struct mali_vpu_session *sess)
 		goto err_mutex_destroy;
 	}
 
+	sess->is_fw_ready = false;
+
+	ret = mali_vpu_ctrls_init(sess);
+	if (ret) {
+		dev_err(sess->vpu_dev->dev, "failed to initialise controls: %d\n", ret);
+		goto err_m2m_ctx_release;
+	}
+
 	return 0;
 
+err_m2m_ctx_release:
+	v4l2_m2m_ctx_release(sess->m2m_ctx);
+
 err_mutex_destroy:
 	mutex_destroy(&sess->vq_lock);
 	return ret;
@@ -94,5 +107,7 @@ void mali_vpu_session_cleanup(struct mali_vpu_session *sess)
 	}
 
 	mutex_unlock(&sess->vq_lock);
+
+	mali_vpu_ctrls_cleanup(sess);
 	mutex_destroy(&sess->vq_lock);
 }
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +14 -0
index f8a602cb59006..766912aec0b8e 100644
@@ -8,6 +8,7 @@
 
 #include <linux/mutex.h>
 #include <linux/videodev2.h>
+#include <media/v4l2-ctrls.h>
 #include <media/v4l2-fh.h>
 
 #include "mali-vpu-device.h"
@@ -32,6 +33,12 @@ struct mali_vpu_port {
  * @port_src: Source (OUTPUT) port for this session.
  * @port_dst: Destination (CAPTURE) port for this session.
  * @vq_lock: Mutex lock to serialise per-session queue related ioctls.
+ * @is_fw_ready: True if the firmware is loaded and ready to accept commands.
+ * @min_buffers_required: Driver-reported minimum buffers for role-specific queue.
+ * @ctrls_caps: Variant-provided immutable control capability table.
+ * @num_ctrls_caps: Number of entries in @ctrls_caps.
+ * @ctrls_state: Per-session control cache and staging state.
+ * @ctrl_handler: V4L2 control handler bound to this session.
  * @fh: V4L2 file handle for this session.
  * @m2m_ctx: V4L2 mem2mem context for this session.
  */
@@ -41,6 +48,13 @@ struct mali_vpu_session {
 	struct mali_vpu_port		port_src;
 	struct mali_vpu_port		port_dst;
 	struct mutex			vq_lock; /* lock to serialise per-session ioctls */
+	bool				is_fw_ready;
+	u32				min_buffers_required;
+
+	const struct mali_vpu_ctrl_desc *ctrls_caps;
+	u32				num_ctrls_caps;
+	struct mali_vpu_ctrl_state	*ctrls_state;
+	struct v4l2_ctrl_handler	ctrl_handler;
 
 	struct v4l2_fh		fh;
 	struct v4l2_m2m_ctx	*m2m_ctx;
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +19 -0
index 7b04b51eeedc2..b807f8d888222 100644
@@ -4,8 +4,14 @@
  */
 
 #include <linux/array_size.h>
+#include <linux/bits.h>
+#include <linux/limits.h>
 #include <linux/videodev2.h>
 
+#include <linux/media/arm/mali-vpu-config.h>
+
+#include "mali-vpu-controls.h"
+#include "mali-vpu-dec.h"
 #include "mali-vpu-formats.h"
 #include "mali-vpu-protocol-def-v3.h"
 #include "mali-vpu-variant.h"
@@ -53,6 +59,95 @@ static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = {
 	},
 };
 
+static const char *const mali_v76_nalu_format_qmenu[] = {
+	"Start Codes",
+	"One NALU Per Buffer",
+	"One Byte Length Field",
+	"Two Byte Length Field",
+	"Four Byte Length Field",
+	NULL,
+};
+
+static const struct mali_vpu_ctrl_desc mali_v76_dec_ctrls[] = {
+	{
+		.cid = V4L2_CID_MALI_VPU_NALU_FORMAT,
+		.type = V4L2_CTRL_TYPE_MENU,

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = { · line 84

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +10 -0
+		.name = "NALU format",
+		.qmenu = mali_v76_nalu_format_qmenu,
+		.min_val = MALI_VPU_NALU_FORMAT_START_CODES,
+		.max_val = MALI_VPU_NALU_FORMAT_FOUR_BYTE_LENGTH_FIELD,
+		.default_val = MALI_VPU_NALU_FORMAT_START_CODES,
+		.step_or_mask = BIT(MALI_VPU_NALU_FORMAT_START_CODES) |
+				BIT(MALI_VPU_NALU_FORMAT_ONE_NALU_PER_BUFFER) |
+				BIT(MALI_VPU_NALU_FORMAT_ONE_BYTE_LENGTH_FIELD) |
+				BIT(MALI_VPU_NALU_FORMAT_TWO_BYTE_LENGTH_FIELD) |
+				BIT(MALI_VPU_NALU_FORMAT_FOUR_BYTE_LENGTH_FIELD),

This mixes alignment and NAL format. Its historical, but the format is bound to V4L2_PIX_FMT_H264 and is documented as Annex B. If you want to add something such as AVCc formata instead, you'd need a new pixel format. There is V4L2_PIX_FMT_H264_SC, but its hill define, nobody knows what it was originally suppose to do. Venus (Qualcomm) maps it as if it was the same, perhaps they have a clue, but I would stay away.

For the alignment, V4L2_CID_MPEG_VIDEO_HEADER_MODE does not cover your 1 buffer per nalue case, it only does the split header like OMX IL was doing (sometimes, interoperanility killed OMX).

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = { · line 92

Quoted 8 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +8 -0
+		.is_custom = true,
+		.flags = 0,
+		.set = mali_vpu_ctrl_set_nalu_format,
+	},
+	{
+		.cid = V4L2_CID_MALI_VPU_INTBUF_SIZE,
+		.type = V4L2_CTRL_TYPE_INTEGER,
+		.name = "Internal buffer size",

No general equivalent, and to be fair, that seems like something the driver should get correctly by itself, I'd say drop.

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = { · line 102

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +10 -0
+		.min_val = 0,
+		.max_val = INT_MAX,
+		.default_val = 0,
+		.step_or_mask = 1,
+		.is_custom = true,
+		.flags = 0,
+		.set = mali_vpu_ctrl_set_intbuf_size,
+	},
+	{
+		.cid = V4L2_CID_MALI_VPU_STREAM_ESCAPING,

No doc, I suppose it was about emulation bytes. Nobody needs that drop.

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = { · line 114

Quoted 12 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +12 -0
+		.type = V4L2_CTRL_TYPE_INTEGER,
+		.name = "Stream Escaping",
+		.min_val = -1,
+		.max_val = 1,
+		.default_val = -1,
+		.step_or_mask = 1,
+		.is_custom = true,
+		.flags = 0,
+		.set = mali_vpu_ctrl_set_stream_escaping,
+	},
+	{
+		.cid = V4L2_CID_MALI_VPU_HOST_PRINT_ENABLE,

Debugging goes into sysfs or debugfs, drop.

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = { · line 126

Quoted 12 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +12 -0
+		.type = V4L2_CTRL_TYPE_BOOLEAN,
+		.name = "Firmware debug prints",
+		.min_val = false,
+		.max_val = true,
+		.default_val = false,
+		.step_or_mask = 1,
+		.is_custom = true,
+		.flags = MALI_VPU_CTRL_FLAG_DYNAMIC,
+		.set = mali_vpu_ctrl_set_host_print_enable,
+	},
+	{
+		.cid = V4L2_CID_MALI_VPU_IGNORE_STREAM_HEADERS,

If userspace want that, it can just strip the headers itself. Its not like a stateful decoder can work without headers according to the spec. It will always break one way or another.

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = { · line 138

Quoted 12 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +12 -0
+		.type = V4L2_CTRL_TYPE_INTEGER,
+		.name = "Ignore Stream Headers",
+		.min_val = -1,
+		.max_val = 1,
+		.default_val = -1,
+		.step_or_mask = 1,
+		.is_custom = true,
+		.flags = 0,
+		.set = mali_vpu_ctrl_set_ignore_stream_headers,
+	},
+	{
+		.cid = V4L2_CID_MALI_VPU_FRAME_REORDERING,

You can thank Samsung, instead you need V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE and add support for V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY 0. That will effectively disable reordering. Some vendors calls this the thumbnail mode to add to the fun.

Quoted 172 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +16 -0
+		.type = V4L2_CTRL_TYPE_INTEGER,
+		.name = "Frame Reordering",
+		.min_val = -1,
+		.max_val = 1,
+		.default_val = -1,
+		.step_or_mask = 1,
+		.is_custom = true,
+		.flags = 0,
+		.set = mali_vpu_ctrl_set_frame_reordering,
+	},
+};
+
 const struct mali_vpu_variant mali_vpu_v76_variant = {
 	.expected_hw_pid = MALI_VPU_HW_PID_V76,
 
@@ -81,4 +176,8 @@ const struct mali_vpu_variant mali_vpu_v76_variant = {
 		.width_default = MALI_V76_DEFAULT_FRAME_WIDTH,
 		.height_default = MALI_V76_DEFAULT_FRAME_HEIGHT,
 	},
+	.ctrl_caps = {
+		.dec_ctrls = mali_v76_dec_ctrls,
+		.num_dec_ctrls = ARRAY_SIZE(mali_v76_dec_ctrls),
+	},
 };
drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h +13 -0
index a46c25a94f18b..ad20896c90b0f 100644
@@ -9,6 +9,7 @@
 #include <linux/types.h>
 #include <linux/videodev2.h>
 
+#include "mali-vpu-controls.h"
 #include "mali-vpu-formats.h"
 
 /**
@@ -33,6 +34,16 @@ struct mali_vpu_format_caps {
 	const struct mali_vpu_format_list dec_dst;
 };
 
+/**
+ * struct mali_vpu_control_caps - Control capability tables per role.
+ * @dec_ctrls: Array of controls supported by the decoder.
+ * @num_dec_ctrls: Number of entries in @dec_ctrls.
+ */
+struct mali_vpu_control_caps {
+	const struct mali_vpu_ctrl_desc *dec_ctrls;
+	u32 num_dec_ctrls;
+};
+
 /**
  * struct mali_vpu_variant - Variant-specific hardware capability descriptor.
  * @rst_names: Array of reset control names for this variant.
@@ -40,6 +51,7 @@ struct mali_vpu_format_caps {
  * @expected_hw_pid: Expected hardware product ID read from the version register.
  * @fmt_caps: Format capability tables for encode/decode paths.
  * @frmsz_caps: Frame dimension limits and defaults.
+ * @ctrl_caps:  Control capability tables for encode/decode paths.
  */
 struct mali_vpu_variant {
 	const char * const *rst_names;
@@ -48,6 +60,7 @@ struct mali_vpu_variant {
 
 	struct mali_vpu_format_caps	fmt_caps;
 	struct mali_vpu_framesize_caps	frmsz_caps;
+	struct mali_vpu_control_caps	ctrl_caps;
 };
 
 extern const struct mali_vpu_variant mali_vpu_v76_variant;
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +19 -0
index dd142527a1a0c..88e706be6705e 100644
@@ -8,7 +8,9 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <media/v4l2-common.h>
+#include <media/v4l2-ctrls.h>
 #include <media/v4l2-dev.h>
+#include <media/v4l2-event.h>
 #include <media/v4l2-fh.h>
 #include <media/v4l2-ioctl.h>
 #include <media/v4l2-mem2mem.h>
@@ -318,6 +320,21 @@ static int mali_vpu_enum_framesizes(struct file *filp, void *fh,
 	return 0;
 }
 
+static int mali_vpu_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
+{
+	switch (sub->type) {
+	case V4L2_EVENT_CTRL:
+		return v4l2_ctrl_subscribe_event(fh, sub);
+	case V4L2_EVENT_EOS:
+		return v4l2_event_subscribe(fh, sub, 2, NULL);
+	case V4L2_EVENT_SOURCE_CHANGE:
+		return v4l2_event_subscribe(fh, sub, 2, NULL);
+
+	default:
+		return -EINVAL;
+	}
+}
+
 const struct v4l2_ioctl_ops mali_vpu_ioctl_ops_dec = {
 	.vidioc_querycap		= mali_vpu_querycap,
 	.vidioc_enum_fmt_vid_out	= mali_vpu_enum_fmt,
@@ -337,4 +354,6 @@ const struct v4l2_ioctl_ops mali_vpu_ioctl_ops_dec = {
 	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
 	.vidioc_remove_bufs		= v4l2_m2m_ioctl_remove_bufs,
 	.vidioc_enum_framesizes		= mali_vpu_enum_framesizes,
+	.vidioc_subscribe_event		= mali_vpu_subscribe_event,
+	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
 };
include/uapi/linux/media/arm/mali-vpu-config.h new file +47 -0
new file mode 100644
index 0000000000000..a82fd69f16b75
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Arm Mali-VPU Driver - Userspace API
+ *
+ * (C) COPYRIGHT 2026 ARM Limited.
+ */
+
+#ifndef __UAPI_MALI_VPU_CONFIG_H__
+#define __UAPI_MALI_VPU_CONFIG_H__
+
+#include <linux/v4l2-controls.h>
+
+/* Mali VPU specific V4L2 driver controls */
+
+/* Encoded stream NALU encapsulation mode. */
+#define V4L2_CID_MALI_VPU_NALU_FORMAT		(V4L2_CID_USER_MALI_VPU_BASE + 0x00)
+
+/**
+ * enum mali_vpu_nalu_format - Encoded stream NALU OUTPUT format.
+ *
+ * @MALI_VPU_NALU_FORMAT_START_CODES: Each NAL unit is prefixed by an Annex B start code.
+ * @MALI_VPU_NALU_FORMAT_ONE_NALU_PER_BUFFER: Exactly one NALU in each output buffer.
+ * @MALI_VPU_NALU_FORMAT_ONE_BYTE_LENGTH_FIELD: Length-prefixed NALU with 1-byte size field.
+ * @MALI_VPU_NALU_FORMAT_TWO_BYTE_LENGTH_FIELD: Length-prefixed NALU with 2-byte size field.
+ * @MALI_VPU_NALU_FORMAT_FOUR_BYTE_LENGTH_FIELD: Length-prefixed NALU with 4-byte size field.
+ *
+ * Options for V4L2_CID_MALI_VPU_NALU_FORMAT control, which determines how the firmware
+ * encapsulates NALUs in the output stream buffers H.264.
+ */
+enum mali_vpu_nalu_format {
+	MALI_VPU_NALU_FORMAT_START_CODES		= 0,
+	MALI_VPU_NALU_FORMAT_ONE_NALU_PER_BUFFER	= 1,
+	MALI_VPU_NALU_FORMAT_ONE_BYTE_LENGTH_FIELD	= 2,
+	MALI_VPU_NALU_FORMAT_TWO_BYTE_LENGTH_FIELD	= 3,
+	MALI_VPU_NALU_FORMAT_FOUR_BYTE_LENGTH_FIELD	= 4,
+};
+
+#define V4L2_CID_MALI_VPU_STREAM_ESCAPING	(V4L2_CID_USER_MALI_VPU_BASE + 0x01)
+#define V4L2_CID_MALI_VPU_IGNORE_STREAM_HEADERS	(V4L2_CID_USER_MALI_VPU_BASE + 0x02)
+#define V4L2_CID_MALI_VPU_FRAME_REORDERING	(V4L2_CID_USER_MALI_VPU_BASE + 0x03)
+/* Suggested firmware intermediate buffer size limit. */
+#define V4L2_CID_MALI_VPU_INTBUF_SIZE		(V4L2_CID_USER_MALI_VPU_BASE + 0x04)
+
+/* Enable firmware protocol to send debug prints to host. */
+#define V4L2_CID_MALI_VPU_HOST_PRINT_ENABLE	(V4L2_CID_USER_MALI_VPU_BASE + 0x05)
+
+#endif /* __UAPI_MALI_VPU_CONFIG_H__ */
include/uapi/linux/v4l2-controls.h +0 -0
index d17e41d51d2ec..b9f7bc3aac1de 100644
@@ -255,6 +255,12 @@ enum v4l2_colorfx {
  */
 #define V4L2_CID_USER_IT6625_BASE		(V4L2_CID_USER_BASE + 0x1250)

include/uapi/linux/v4l2-controls.h enum v4l2_colorfx { · line 266

Quoted 10 lines
include/uapi/linux/v4l2-controls.h +6 -0
 
+/*
+ * The base for Arm Mali-VPU driver controls.
+ * We reserve 64 controls for this driver.
+ */
+#define V4L2_CID_USER_MALI_VPU_BASE		(V4L2_CID_USER_BASE + 0x1260)
+
 /* MPEG-class control IDs */
 /* The MPEG controls are applicable to all codec controls
  * and the 'MPEG' part of the define is historical */

My recommendation, drop all the vendor controls, legacy users can use your downstream kernels.

Nicolas

#27 lore raw

Re: [RFC PATCH 08/28] media: mali-vpu: register V76 decoder controls and introduce UAPI controls

Quoted 8 lines · Nicolas Dufresne
Quoted 4 lines

Add Mali VPU userspace controls for NALU layout, stream escaping, intermediate buffer size, stream-header handling, frame reordering and firmware debug output. Also expose the standard read-only V4L2_CID_MIN_BUFFERS_FOR_CAPTURE control.

I would strongly recommend to keep each vendor control in its own patch. These are API, and they need documentation. In practice, you'll prefer to not introduce vendor control as a first introduction of your driver, its just a bad idea. You completely missed Documentation/

I had planned to add the documentation for these subsequently, but, I agree, it should accompany the controls. The initial driver series may not be the right place for these vendor controls, so I’ll remove them and their UAPI definitions from this patchset.

Quoted 19 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = { · line 84

Quoted 12 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +12 -0
+		.cid = V4L2_CID_MALI_VPU_NALU_FORMAT,
+		.type = V4L2_CTRL_TYPE_MENU,
+		.name = "NALU format",
+		.qmenu = mali_v76_nalu_format_qmenu,
+		.min_val = MALI_VPU_NALU_FORMAT_START_CODES,
+		.max_val = MALI_VPU_NALU_FORMAT_FOUR_BYTE_LENGTH_FIELD,
+		.default_val = MALI_VPU_NALU_FORMAT_START_CODES,
+		.step_or_mask = BIT(MALI_VPU_NALU_FORMAT_START_CODES) |
+				BIT(MALI_VPU_NALU_FORMAT_ONE_NALU_PER_BUFFER) |
+				BIT(MALI_VPU_NALU_FORMAT_ONE_BYTE_LENGTH_FIELD) |
+				BIT(MALI_VPU_NALU_FORMAT_TWO_BYTE_LENGTH_FIELD) |
+				BIT(MALI_VPU_NALU_FORMAT_FOUR_BYTE_LENGTH_FIELD),

This mixes alignment and NAL format. Its historical, but the format is bound to V4L2_PIX_FMT_H264 and is documented as Annex B. If you want to add something such as AVCc formata instead, you'd need a new pixel format. There is V4L2_PIX_FMT_H264_SC, but its hill define, nobody knows what it was originally suppose to do. Venus (Qualcomm) maps it as if it was the same, perhaps they have a clue, but I would stay away.

Quoted 3 lines

For the alignment, V4L2_CID_MPEG_VIDEO_HEADER_MODE does not cover your 1 buffer per nalue case, it only does the split header like OMX IL was doing (sometimes, interoperanility killed OMX).

The firmware defaults to Annex B, which matches V4L2_PIX_FMT_H264, so this control is not needed for that input format. I'll drop this completely.

Quoted 5 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = { · line 92

Quoted 3 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +3 -0
+		.cid = V4L2_CID_MALI_VPU_INTBUF_SIZE,
+		.type = V4L2_CTRL_TYPE_INTEGER,
+		.name = "Internal buffer size",

No general equivalent, and to be fair, that seems like something the driver should get correctly by itself, I'd say drop.

The firmware already calculates the intermediate working-buffer size when no override is supplied. I’ll remove this control completely.

Quoted 4 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = { · line 102

Quoted 3 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +3 -0
+	},
+	{
+		.cid = V4L2_CID_MALI_VPU_STREAM_ESCAPING,

No doc, I suppose it was about emulation bytes. Nobody needs that drop.

Yes, this controls emulation-prevention byte handling. Agreed that this can be dropped.

Quoted 3 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	{
+		.cid = V4L2_CID_MALI_VPU_HOST_PRINT_ENABLE,

Debugging goes into sysfs or debugfs, drop.

Ack. I’ll use an appropriate interface as suggested when I reintroduce this.

Quoted 6 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = { · line 126

Quoted 3 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +3 -0
+	},
+	{
+		.cid = V4L2_CID_MALI_VPU_IGNORE_STREAM_HEADERS,

If userspace want that, it can just strip the headers itself. Its not like a stateful decoder can work without headers according to the spec. It will always break one way or another.

This does not skip the initial headers; it retains existing SPS/PPS entries rather than accepting replacements with the same ID. It was intended to prevent a heavily corrupted stream from replacing valid settings, but it would also ignore legitimate changes. Agreed to drop it.

Quoted 7 lines

drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c static const struct mali_vpu_format_desc mali_v76_dec_dst_fmts[] = { · line 138

Quoted 3 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +3 -0
+	},
+	{
+		.cid = V4L2_CID_MALI_VPU_FRAME_REORDERING,

You can thank Samsung, instead you need V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE and add support for V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY 0. That will effectively disable reordering. Some vendors calls this the thumbnail mode to add to the fun.

Agreed, I’ll drop the vendor control. I’ll use the suggested standard controls with a delay of zero, when I reintroduce this in subsequent patches.

Quoted 11 lines

include/uapi/linux/v4l2-controls.h enum v4l2_colorfx { · line 266

Quoted 9 lines
include/uapi/linux/v4l2-controls.h +6 -0
+/*
+ * The base for Arm Mali-VPU driver controls.
+ * We reserve 64 controls for this driver.
+ */
+#define V4L2_CID_USER_MALI_VPU_BASE		(V4L2_CID_USER_BASE + 0x1260)
+
  /* MPEG-class control IDs */
  /* The MPEG controls are applicable to all codec controls
   * and the 'MPEG' part of the define is historical */

My recommendation, drop all the vendor controls, legacy users can use your downstream kernels.

I’ll drop the vendor controls from this series.

Regards,
Charvi

#28 lore raw

RFC PATCH 9/28 media: mali-vpu: add MMU software page table manager

In reply to Charvi Mehta

From: Rath Sankaralingam <[email protected]>

Add Mali VPU internal MMU page table manager for managing firmware-visible virtual address space. The MMU code builds and maintains the VPU two-level page tables, allocating backing pages and maps scatter-gather and dma-buf backed memory. It also exposes helpers for mapping, unmapping, resizing, synchronizing and writing into mapped regions.

The page table entries describe the addresses consumed by the VPU peripheral MMU, allowing firmware communication queues and video buffers to be mapped into the VPU virtual address layout with the required bus attributes and access permissions.

This also configures the V76 DMA mask for its 40-bit addressing capability and sets the maximum DMA segment size used by DMA-backed buffers.

---
 drivers/media/platform/arm/mali-vpu/Makefile  |   3 +-
 .../platform/arm/mali-vpu/mali-vpu-device.h   |   2 +
 .../platform/arm/mali-vpu/mali-vpu-mmu.c      | 693 ++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-mmu.h      | 152 ++++
 .../platform/arm/mali-vpu/mali-vpu-probe.c    |   8 +
 .../platform/arm/mali-vpu/mali-vpu-session.c  |  10 +
 .../platform/arm/mali-vpu/mali-vpu-session.h  |   3 +
 .../arm/mali-vpu/mali-vpu-variant-v76.c       |   1 +
 .../platform/arm/mali-vpu/mali-vpu-variant.h  |   2 +
 9 files changed, 873 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.h
drivers/media/platform/arm/mali-vpu/Makefile +2 -1
index 95bad821eb466..0716a2b0c8712 100644
@@ -8,6 +8,7 @@ mali-vpu-objs := mali-vpu-probe.o \
 		mali-vpu-buffer.o \
 		mali-vpu-vb2.o \
 		mali-vpu-formats.o \
-		mali-vpu-controls.o
+		mali-vpu-controls.o \
+		mali-vpu-mmu.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-device.h +2 -0
index 26b2bf9e1588e..20361df05bdcd 100644
@@ -32,6 +32,7 @@ enum mali_vpu_session_role {
  * @clk_count: Number of clocks in @clk_list.
  * @rst_list: List of reset controls.
  * @rst_count: Number of reset controls in @rst_list.
+ * @dma_mask: DMA mask for the device, indicating supported address width.
  * @irq: IRQ number for VPU.
  * @variant: Structure describing variant-specific features and capabilities.
  * @v4l2_dev: V4L2 device structure for mali vpu.
@@ -45,6 +46,7 @@ struct mali_vpu_dev {
 	unsigned int			clk_count;
 	struct reset_control_bulk_data	*rst_list;
 	unsigned int			rst_count;
+	u64				dma_mask;
 	int				irq;
 
 	const struct mali_vpu_variant	*variant;
drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c new file +693 -0

693 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.h new file +152 -0

152 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c +8 -0
index 36a25030d7324..2d63dc058c91a 100644
@@ -213,6 +213,13 @@ static int mali_vpu_probe(struct platform_device *pdev)
 		goto err_rst_clk;
 	}
 
+	vpu_dev->dma_mask = vpu_dev->variant->dma_mask;
+	ret = dma_set_mask_and_coherent(vpu_dev->dev, vpu_dev->dma_mask);
+	if (ret)
+		goto err_rst_clk;
+
+	dma_set_max_seg_size(vpu_dev->dev, SZ_2G);
+
 	ret = v4l2_device_register(&pdev->dev, &vpu_dev->v4l2_dev);
 	if (ret) {
 		dev_err(vpu_dev->dev, "failed to register V4L2 device\n");
@@ -280,3 +287,4 @@ static struct platform_driver mali_vpu_driver = {
 module_platform_driver(mali_vpu_driver);
 MODULE_DESCRIPTION("Arm Mali VPU driver");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS("DMA_BUF");
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +10 -0
index d1e8680de5556..10481ba16d68a 100644
@@ -86,8 +86,17 @@ int mali_vpu_session_init(struct mali_vpu_session *sess)
 		goto err_m2m_ctx_release;
 	}
 
+	ret = mali_vpu_mmu_init(&sess->mmu, sess->vpu_dev->dev);
+	if (ret) {
+		dev_err(sess->vpu_dev->dev, "failed to initialise MMU: %d\n", ret);
+		goto err_ctrls_cleanup;
+	}
+
 	return 0;
 
+err_ctrls_cleanup:
+	mali_vpu_ctrls_cleanup(sess);
+
 err_m2m_ctx_release:
 	v4l2_m2m_ctx_release(sess->m2m_ctx);
 
@@ -108,6 +117,7 @@ void mali_vpu_session_cleanup(struct mali_vpu_session *sess)
 
 	mutex_unlock(&sess->vq_lock);
 
+	mali_vpu_mmu_cleanup(&sess->mmu);
 	mali_vpu_ctrls_cleanup(sess);
 	mutex_destroy(&sess->vq_lock);
 }
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +3 -0
index 766912aec0b8e..4dd6c8bc22dd9 100644
@@ -13,6 +13,7 @@
 
 #include "mali-vpu-device.h"
 #include "mali-vpu-formats.h"
+#include "mali-vpu-mmu.h"
 
 struct v4l2_m2m_ctx;
 
@@ -32,6 +33,7 @@ struct mali_vpu_port {
  * @role: Role of this session.
  * @port_src: Source (OUTPUT) port for this session.
  * @port_dst: Destination (CAPTURE) port for this session.
+ * @mmu: MMU handle for this session
  * @vq_lock: Mutex lock to serialise per-session queue related ioctls.
  * @is_fw_ready: True if the firmware is loaded and ready to accept commands.
  * @min_buffers_required: Driver-reported minimum buffers for role-specific queue.
@@ -47,6 +49,7 @@ struct mali_vpu_session {
 	enum mali_vpu_session_role	role;
 	struct mali_vpu_port		port_src;
 	struct mali_vpu_port		port_dst;
+	struct mali_vpu_mmu		mmu;
 	struct mutex			vq_lock; /* lock to serialise per-session ioctls */
 	bool				is_fw_ready;
 	u32				min_buffers_required;
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +1 -0
index b807f8d888222..19ad2667f2597 100644
@@ -149,6 +149,7 @@ static const struct mali_vpu_ctrl_desc mali_v76_dec_ctrls[] = {
 };
 
 const struct mali_vpu_variant mali_vpu_v76_variant = {
+	.dma_mask = DMA_BIT_MASK(40),
 	.expected_hw_pid = MALI_VPU_HW_PID_V76,
 
 	.rst_names = mali_v76_rst_names,
drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h +2 -0
index ad20896c90b0f..d316e010fff48 100644
@@ -49,6 +49,7 @@ struct mali_vpu_control_caps {
  * @rst_names: Array of reset control names for this variant.
  * @num_rsts: Number of reset controls for this variant.
  * @expected_hw_pid: Expected hardware product ID read from the version register.
+ * @dma_mask: DMA addressing capability mask.
  * @fmt_caps: Format capability tables for encode/decode paths.
  * @frmsz_caps: Frame dimension limits and defaults.
  * @ctrl_caps:  Control capability tables for encode/decode paths.
@@ -57,6 +58,7 @@ struct mali_vpu_variant {
 	const char * const *rst_names;
 	unsigned int num_rsts;
 	u32 expected_hw_pid;
+	u64 dma_mask;
 
 	struct mali_vpu_format_caps	fmt_caps;
 	struct mali_vpu_framesize_caps	frmsz_caps;
Signature · 1 line
2.43.0
#29 lore raw

Re: [RFC PATCH 09/28] media: mali-vpu: add MMU software page table manager

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] media: mali-vpu: tautological VA range check and 32-bit VA truncation - [Medium] media: mali-vpu…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] media: mali-vpu: tautological VA range check and 32-bit VA truncation
- [Medium] media: mali-vpu: devm_kzalloc() lifetime and size mismatch for MMU pages
--

commit 635e5f0a32c7b907a3a802ff85c63c68b877a3e5
Author: Rath Sankaralingam <[email protected]>

media: mali-vpu: add MMU software page table manager

This commit adds the software MMU two-level page table manager for the Mali VPU driver and configures the 40-bit DMA mask and maximum DMA segment size.

Quoted 6 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c new file +0 -0
new file mode 100644
index 0000000000000..00cf5251eff5e
@@ -0,0 +1,693 @@

[ ... ]

Quoted 3 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c new file +3 -0
+/* Number of bits for the virtual address space. */
+#define MALI_VPU_VA_BITS		32
+#define MALI_VPU_VA_MASK		GENMASK(MALI_VPU_VA_BITS - 1, 0)

[ ... ]

Quoted 11 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c new file +11 -0
+static int mali_vpu_map_page(struct mali_vpu_mmu *mmu,
+			     mali_vpu_mmu_va va,
+			     dma_addr_t dma,
+			     enum mali_vpu_mmu_attr attr,
+			     enum mali_vpu_mmu_access access)
+{
+	struct mali_vpu_mmu_pt_page *l2;
+	mali_vpu_mmu_pte *pte;
+
+	/* Check that both VA and DMA address are page aligned. */
+	if ((va | dma) & MALI_VPU_PAGE_MASK) {

drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c line 133

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c new file +10 -0
+		dev_err(mmu->dev, "VA and DMA address must be page aligned. va=0x%x, dma=%pad",
+			va, &dma);
+		return -EFAULT;
+	}
+
+	/* Check that VA is within valid address range. */
+	if (va & ~MALI_VPU_VA_MASK) {
+		dev_err(mmu->dev, "VA out of valid range. va=0x%x", va);
+		return -EFAULT;
+	}

[Severity: Medium]
Can (va & ~MALI_VPU_VA_MASK) ever evaluate to true here? mali_vpu_mmu_va is a u32 and MALI_VPU_VA_MASK is GENMASK(31, 0), so the check is always zero, and 64-bit address arithmetic in callers such as mali_vpu_mmu_map_pages() (va + i * MALI_VPU_PAGE_SIZE) and mali_vpu_mmu_map_dma() (va + offset) is truncated to u32 before reaching mali_vpu_map_page(), wrapping around to address 0 if a mapping crosses 4 GiB.

[ ... ]

Quoted 16 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c new file +16 -0
+struct mali_vpu_mmu_pages *mali_vpu_mmu_alloc_pages(struct device *dev,
+						    size_t count,
+						    size_t capacity)
+{
+	struct mali_vpu_mmu_pages *pages;
+	int ret;
+
+	count = roundup(count, MALI_VPU_PAGES_PER_LINUX_PAGE);
+	capacity = roundup(capacity, MALI_VPU_PAGES_PER_LINUX_PAGE);
+	capacity = max(count, capacity);
+
+	pages = devm_kzalloc(dev,
+			     sizeof(*pages) + sizeof(pages->pages[0]) * capacity,
+			     GFP_KERNEL);
+	if (!pages)
+		return ERR_PTR(-ENOMEM);

[ ... ]

Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c new file +4 -0
+void mali_vpu_mmu_free_pages(struct mali_vpu_mmu_pages *pages)
+{
+	unsigned int i;
+

drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c line 543

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c new file +10 -0
+	mali_vpu_mmu_unmap_pages(pages);
+
+	if (!pages->is_external)
+		for (i = 0; i < pages->count; i += MALI_VPU_PAGES_PER_LINUX_PAGE)
+			mali_vpu_mmu_free_page(pages->dev, pages->pages[i].dma,
+					       pages->pages[i].linux_page,
+					       DMA_BIDIRECTIONAL);
+
+	devm_kfree(pages->dev, pages);
+}

[Severity: Medium]
Should mali_vpu_mmu_alloc_pages() and mali_vpu_mmu_alloc_pages_sg() use kvzalloc(struct_size(pages, pages, capacity), GFP_KERNEL) and kvfree() instead of devm_kzalloc() and devm_kfree()?

Because the backing pages and DMA mappings are not devres-managed, unbinding the platform device while a V4L2 session fd is open frees the struct mali_vpu_mmu_pages container in devres_release_all() (leaking the backing pages), and when the fd is later closed, mali_vpu_mmu_free_pages() reads the freed pages struct and hits WARN_ON(rc) in devm_kfree(). In addition, devm_kzalloc() requires physically contiguous slab memory and will fail above KMALLOC_MAX_SIZE for large buffer capacities.

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

RFC PATCH 10/28 media: mali-vpu: add firmware cache infrastructure

In reply to Charvi Mehta

Add a per-device cache for VPU firmware binaries and wire its lifetime into probe and remove paths. Key cache entries by codec, session role and compatible firmware major version, and load new entries asynchronously through the firmware loader.

Validate the firmware and host protocol versions from the firmware header and derive the number of text, private BSS and shared BSS pages. Notify registered waiters when loading completes while retaining each binary through cache, loader and caller references. Failed entries are evicted immediately.

For now, keep the cache policy deliberately simple: a background thread periodically prunes entries that are only held by the cache itself. This is intentional for initial upstreaming, so lifetime and concurrency behavior stays easy to reason about before adding a smarter eviction policy.

---
 drivers/media/platform/arm/mali-vpu/Makefile  |   3 +-
 .../platform/arm/mali-vpu/mali-vpu-device.h   |   3 +
 .../arm/mali-vpu/mali-vpu-firmware-cache.c    | 461 ++++++++++++++++++
 .../arm/mali-vpu/mali-vpu-firmware-cache.h    | 172 +++++++
 .../platform/arm/mali-vpu/mali-vpu-probe.c    |  16 +-
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   |   3 +
 6 files changed, 656 insertions(+), 2 deletions(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.h
drivers/media/platform/arm/mali-vpu/Makefile +2 -1
index 0716a2b0c8712..74e1455c01d8b 100644
@@ -9,6 +9,7 @@ mali-vpu-objs := mali-vpu-probe.o \
 		mali-vpu-vb2.o \
 		mali-vpu-formats.o \
 		mali-vpu-controls.o \
-		mali-vpu-mmu.o
+		mali-vpu-mmu.o \
+		mali-vpu-firmware-cache.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-device.h +3 -0
index 20361df05bdcd..a35c1b901aa3f 100644
@@ -14,6 +14,7 @@
 struct clk_bulk_data;
 struct v4l2_m2m_dev;
 struct mali_vpu_variant;
+struct mali_vpu_fw_cache;
 struct reset_control_bulk_data;
 
 /**
@@ -38,6 +39,7 @@ enum mali_vpu_session_role {
  * @v4l2_dev: V4L2 device structure for mali vpu.
  * @vdev_dec: Video device structure for decoder.
  * @m2m_dev: V4L2 mem2mem device structure for mali vpu.
+ * @fw_cache: Firmware cache for sharing or reusing firmware blobs.
  */
 struct mali_vpu_dev {
 	struct device			*dev;
@@ -53,6 +55,7 @@ struct mali_vpu_dev {
 	struct v4l2_device		v4l2_dev;
 	struct video_device		*vdev_dec;
 	struct v4l2_m2m_dev		*m2m_dev;
+	struct mali_vpu_fw_cache	*fw_cache;
 };
 
 #endif /* __MALI_VPU_DEVICE_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c new file +461 -0

461 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.h new file +172 -0

172 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c +15 -1
index 2d63dc058c91a..b87725d7d4cab 100644
@@ -21,6 +21,7 @@
 #include <media/v4l2-mem2mem.h>
 
 #include "mali-vpu-device.h"
+#include "mali-vpu-firmware-cache.h"
 #include "mali-vpu-registers.h"
 #include "mali-vpu-variant.h"
 #include "mali-vpu-vidioc.h"
@@ -232,14 +233,26 @@ static int mali_vpu_probe(struct platform_device *pdev)
 		goto err_unreg_v4l2;
 	}
 
-	ret = mali_vpu_register_video_device(vpu_dev, MALI_VPU_DECODER);
+	vpu_dev->fw_cache = devm_kzalloc(&pdev->dev, sizeof(*vpu_dev->fw_cache), GFP_KERNEL);
+	if (!vpu_dev->fw_cache) {
+		ret = -ENOMEM;
+		goto err_release_m2m;
+	}
+
+	ret = mali_vpu_fw_cache_init(vpu_dev->fw_cache, vpu_dev->dev);
 	if (ret)
 		goto err_release_m2m;
 
+	ret = mali_vpu_register_video_device(vpu_dev, MALI_VPU_DECODER);
+	if (ret)
+		goto err_cleanup_fw_cache;
+
 	platform_set_drvdata(pdev, vpu_dev);
 
 	return 0;
 
+err_cleanup_fw_cache:
+	mali_vpu_fw_cache_cleanup(vpu_dev->fw_cache);
 err_release_m2m:
 	v4l2_m2m_release(vpu_dev->m2m_dev);
 err_unreg_v4l2:
@@ -257,6 +270,7 @@ static void mali_vpu_remove(struct platform_device *pdev)
 	struct mali_vpu_dev *vpu_dev = platform_get_drvdata(pdev);
 
 	video_unregister_device(vpu_dev->vdev_dec);
+	mali_vpu_fw_cache_cleanup(vpu_dev->fw_cache);
 	v4l2_m2m_release(vpu_dev->m2m_dev);
 	v4l2_device_unregister(&vpu_dev->v4l2_dev);
 
drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h +3 -0
index aed51eec6ca9c..f3a61731191bc 100644
@@ -25,6 +25,9 @@
  *
  ****************************************************************************/
 
+#define MALI_VPU_HOST_PROTOCOL_MAJOR 3
+#define MALI_VPU_HOST_PROTOCOL_MINOR 1
+
  #define MVE_CHROMA_FORMAT_420 (0x1)
 
 /* format bitfield: */
Signature · 1 line
2.43.0
#31 lore raw

Re: [RFC PATCH 10/28] media: mali-vpu: add firmware cache infrastructure

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] media: mali-vpu: race and UAF in mali_vpu_fw_cache_cleanup() - [Low] media: mali-vpu: %zu format…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] media: mali-vpu: race and UAF in mali_vpu_fw_cache_cleanup()
- [Low] media: mali-vpu: %zu format mismatch in mali_vpu_fw_parse_header()
--

commit c4904ccc1e8387726744e3d73159243657fc5c24
Author: Charvi Mehta <[email protected]>

media: mali-vpu: add firmware cache infrastructure

This commit adds a per-device firmware cache for Mali VPU firmware binaries, loading entries asynchronously and pruning unused binaries in a background thread.

Quoted 6 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c new file +0 -0
new file mode 100644
index 0000000000000..746d30f179305
@@ -0,0 +1,461 @@

[ ... ]

Quoted 3 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c new file +3 -0
+static int mali_vpu_fw_parse_header(const struct firmware *fw,
+				    struct mali_vpu_fw_bin *vpu_fw_bin)
+{

[ ... ]

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c line 107

Quoted 6 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c new file +6 -0
+	if (bss_bitmap_size > ARRAY_SIZE(header->bss_bitmap) * 32) {
+		dev_err(vpu_fw_bin->cache->dev,
+			"invalid firmware BSS bitmap size: %u (max %zu)\n",
+			bss_bitmap_size, ARRAY_SIZE(header->bss_bitmap) * 32UL);
+		return -EBADMSG;
+	}

[Severity: Low]
Does multiplying ARRAY_SIZE(header->bss_bitmap) by 32UL promote the argument to unsigned long, causing a -Wformat warning with %zu on 32-bit builds where size_t is unsigned int? Multiplying by 32 instead of 32UL preserves size_t.

[ ... ]

Quoted 41 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c new file +41 -0
+static void mali_vpu_fw_request_done(const struct firmware *fw, void *context)
+{
+	struct mali_vpu_fw_bin *vpu_fw_bin = context;
+	const struct mali_vpu_fw_header *hdr;
+	LIST_HEAD(local_event_list);
+	int ret = 0;
+
+	mutex_lock(&vpu_fw_bin->cache->lock);
+	mutex_lock(&vpu_fw_bin->lock);
+
+	if (!fw) {
+		ret = -ENOENT;
+		vpu_fw_bin->fw = NULL;
+	} else {
+		ret = mali_vpu_fw_parse_header(fw, vpu_fw_bin);
+		if (ret) {
+			release_firmware(fw);
+			vpu_fw_bin->fw = NULL;
+		} else {
+			vpu_fw_bin->fw = fw;
+		}
+	}
+
+	/* We have to notify waiters in failure or success case */
+	vpu_fw_bin->load_status = ret;
+	vpu_fw_bin->state = ret ? MALI_VPU_FW_BIN_FAILED : MALI_VPU_FW_BIN_READY;
+
+	/*
+	 * Take a local copy of events/waiters
+	 * so that notify callback can be called outside vpu_fw_bin->lock
+	 */
+	list_splice_init(&vpu_fw_bin->event_list, &local_event_list);
+
+	if (ret) {
+		/* Firmware load failed, remove cached entry */
+		list_del_init(&vpu_fw_bin->cache_link);
+		kref_put(&vpu_fw_bin->refcount, mali_vpu_fw_bin_release); /* Drop cache-ref */
+	}
+
+	mutex_unlock(&vpu_fw_bin->lock);
+	mutex_unlock(&vpu_fw_bin->cache->lock);

[ ... ]

Quoted 8 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c new file +8 -0
+void mali_vpu_fw_cache_cleanup(struct mali_vpu_fw_cache *cache)
+{
+	struct mali_vpu_fw_bin *bin;
+	struct mali_vpu_fw_bin *temp;
+	LIST_HEAD(local);
+
+	if (cache->cache_thread)
+		kthread_stop(cache->cache_thread);

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c line 461

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c new file +10 -0
+
+	mutex_lock(&cache->lock);
+	list_splice_init(&cache->fw_bins, &local);
+	mutex_unlock(&cache->lock);
+
+	list_for_each_entry_safe(bin, temp, &local, cache_link) {
+		list_del_init(&bin->cache_link);
+		kref_put(&bin->refcount, mali_vpu_fw_bin_release);
+	}
+}

[Severity: High]
Can an in-flight request_firmware_nowait() callback race with mali_vpu_fw_cache_cleanup() during device removal, causing list corruption, a double kref_put() of the cache reference, and use-after-free of both vpu_fw_bin and vpu_dev->fw_cache?

CPU0 CPU1 (firmware loader work)
mali_vpu_remove()
mali_vpu_fw_cache_cleanup()

    mutex_lock(&cache->lock)
    list_splice_init(&cache->fw_bins,
                     &local)
    mutex_unlock(&cache->lock)
                                      mali_vpu_fw_request_done()
                                        mutex_lock(&bin->cache->lock)
                                        /* load fails: ret != 0 */
                                        list_del_init(&bin->cache_link)
                                          <-- races with local traversal
                                        kref_put(&bin->refcount, ...)
                                          <-- drops cache-ref
    list_for_each_entry_safe(...,
                             &local, ...)
      list_del_init(&bin->cache_link)
      kref_put(&bin->refcount, ...)
        <-- drops cache-ref a second time

/* devres frees vpu_dev->fw_cache */
dev_warn(bin->cache->dev, ...)
<-- use-after-free of cache
kref_put(&bin->refcount, ...)
<-- use-after-free of bin

mali_vpu_fw_cache_cleanup() releases cache->lock while each bin->cache_link is still linked into the stack-allocated local list, so a failing mali_vpu_fw_request_done() can mutate bin->cache_link while local is being walked unlocked. Even if the list operations do not overlap, both mali_vpu_fw_cache_cleanup() and the failure path in mali_vpu_fw_request_done() unconditionally drop the single cache-owned reference on bin->refcount, freeing vpu_fw_bin before mali_vpu_fw_request_done() finishes logging and dropping its loader reference. Finally, because mali_vpu_fw_cache_cleanup() does not wait for pending request_firmware_nowait() callbacks before mali_vpu_remove() lets devres free vpu_dev->fw_cache, any later callback dereferences freed memory through vpu_fw_bin->cache.

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

Re: [RFC PATCH 10/28] media: mali-vpu: add firmware cache infrastructure

In reply to Charvi Mehta

Before I move on, since I think you have enough early feedback,

Le jeudi 19 novembre 2026 à 15:37 +0000, Charvi Mehta a écrit :

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c line 58

Quoted 2 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c new file +2 -0
+	n = snprintf(name, name_sz, "arm/mali-vpu/v%u/%s%s.fwb",
+		     key->fw_ver_major, codec, role_str);

Your firmware will have to be published into linux-firmware project for your driver to be accepted upstream. Depending on the state, feedback and clarity of your driver, I may or may not ask to ship some HW to verify your work. No immediate action needed. Since you only offered a generic DT binding, its not clear what upstream SoC can actually make use of this driver. Some information about this would be appreciated in your next cover letter.

Nicolas

#33 lore raw

Re: [RFC PATCH 10/28] media: mali-vpu: add firmware cache infrastructure

Quoted 8 lines · Nicolas Dufresne

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c line 58

Quoted 2 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c new file +2 -0
+	n = snprintf(name, name_sz, "arm/mali-vpu/v%u/%s%s.fwb",
+		     key->fw_ver_major, codec, role_str);

Your firmware will have to be published into linux-firmware project for your driver to be accepted upstream. Depending on the state, feedback and clarity of your driver, I may or may not ask to ship some HW to verify your work. No immediate action needed. Since you only offered a generic DT binding, its not clear what upstream SoC can actually make use of this driver. Some information about this would be appreciated in your next cover letter.

I am in the process of submitting the H.264 decoder firmware blob to linux-firmware. Testing is currently supported on FPGA platforms and software simulation models. A possible SoC integration of a related variant is also being verified. This work lays the groundwork for the next generation of the IP, which is intended for SoC integration and is expected to reuse most of the driver with minor variant-specific changes.

Regards,
Charvi

#34 lore raw

RFC PATCH 11/28 media: mali-vpu: load firmware for decoder sessions

In reply to Charvi Mehta

Request the firmware binary during preparation of the decoder coded queue. Build the cache key from the coded format, session role and variant firmware major version. Propagate request submission errors through VB2 prepare_streaming callback.

Track asynchronous firmware loading per session. Retain the cache reference after a successful load and release failed or late results.

During the session teardown, wait for firmware loading with a timeout. On timeout, cancel the pending cache waiter. If completion has already claimed the waiter, wait for its callback before releasing session resources.

This establishes firmware-binary loading and ownership only. Firmware address-space construction and execution are added in subsequent changes.

---
 .../arm/mali-vpu/mali-vpu-firmware-cache.c    |  45 +++++
 .../arm/mali-vpu/mali-vpu-firmware-cache.h    |   2 +
 .../platform/arm/mali-vpu/mali-vpu-formats.c  |   7 +
 .../platform/arm/mali-vpu/mali-vpu-formats.h  |   1 +
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 175 +++++++++++++++++-
 .../platform/arm/mali-vpu/mali-vpu-session.h  |  40 +++-
 .../arm/mali-vpu/mali-vpu-variant-v76.c       |   2 +
 .../platform/arm/mali-vpu/mali-vpu-variant.h  |   2 +
 .../platform/arm/mali-vpu/mali-vpu-vb2.c      |  28 +++
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   |   5 +-
 10 files changed, 303 insertions(+), 4 deletions(-)
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c +45 -0
index 746d30f179305..2cc69df71bd93 100644
@@ -390,6 +390,51 @@ void mali_vpu_fw_cache_put(struct mali_vpu_fw_bin *bin)
 	kref_put(&bin->refcount, mali_vpu_fw_bin_release);
 }
 
+bool mali_vpu_fw_cache_cancel_waiter(struct mali_vpu_fw_cache *cache,
+				     struct mali_vpu_fw_event *event)
+{
+	struct mali_vpu_fw_bin *cancelled_bin = NULL;
+	struct mali_vpu_fw_event *waiter;
+	struct mali_vpu_fw_bin *bin;
+
+	if (!cache || !event)
+		return false;
+
+	mutex_lock(&cache->lock);
+
+	list_for_each_entry(bin, &cache->fw_bins, cache_link) {
+		mutex_lock(&bin->lock);
+
+		list_for_each_entry(waiter, &bin->event_list, link) {
+			if (waiter != event)
+				continue;
+
+			list_del_init(&event->link);
+			cancelled_bin = bin;
+			break;
+		}
+
+		mutex_unlock(&bin->lock);
+
+		if (cancelled_bin)
+			break;
+	}
+
+	mutex_unlock(&cache->lock);
+
+	if (!cancelled_bin)
+		return false;
+
+	/*
+	 * Every successful cache_get() owns one caller reference. Since the
+	 * callback will no longer run for this waiter, release that reference
+	 * here.
+	 */
+	mali_vpu_fw_cache_put(cancelled_bin);
+
+	return true;
+}
+
 static void mali_vpu_fw_cache_prune(struct mali_vpu_fw_cache *cache)
 {
 	LIST_HEAD(prune_list);
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.h +2 -0
index f8b024222d2d2..ea7d35ef8ca60 100644
@@ -168,5 +168,7 @@ int mali_vpu_fw_cache_get(struct mali_vpu_fw_cache *cache,
 			  const struct mali_vpu_fw_key *key,
 			  struct mali_vpu_fw_event *event);
 void mali_vpu_fw_cache_put(struct mali_vpu_fw_bin *bin);
+bool mali_vpu_fw_cache_cancel_waiter(struct mali_vpu_fw_cache *cache,
+				     struct mali_vpu_fw_event *event);
 
 #endif /* __MALI_VPU_FIRMWARE_CACHE_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c +7 -0
index d268435432371..57aa1b3baf40f 100644
@@ -13,6 +13,13 @@
 #include "mali-vpu-formats.h"
 #include "mali-vpu-session.h"
 
+bool mali_vpu_is_coded_q(const struct mali_vpu_session *sess, enum v4l2_buf_type type)
+{
+	if (sess->role == MALI_VPU_DECODER)
+		return V4L2_TYPE_IS_OUTPUT(type);
+	return V4L2_TYPE_IS_CAPTURE(type);
+}
+
 const struct mali_vpu_format_desc *
 mali_vpu_find_format(struct mali_vpu_session *sess, u32 pixfmt,
 		     enum v4l2_buf_type type)
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.h +1 -0
index 5703142b93b46..37198325ee481 100644
@@ -33,5 +33,6 @@ const struct mali_vpu_format_desc *mali_vpu_find_format(struct mali_vpu_session
 int mali_vpu_fill_pixmp_fmt(const struct mali_vpu_format_desc *fmt,
 			    u32 width, u32 height,
 			    struct v4l2_pix_format_mplane *pix_mp);
+bool mali_vpu_is_coded_q(const struct mali_vpu_session *sess, enum v4l2_buf_type type);
 
 #endif /* __MALI_VPU_FORMATS_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +174 -1

227 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +39 -1
index 4dd6c8bc22dd9..0b810d7d6a036 100644
@@ -6,6 +6,7 @@
 #ifndef __MALI_VPU_SESSION_H__
 #define __MALI_VPU_SESSION_H__
 
+#include <linux/completion.h>
 #include <linux/mutex.h>
 #include <linux/videodev2.h>
 #include <media/v4l2-ctrls.h>
@@ -16,15 +17,42 @@
 #include "mali-vpu-mmu.h"
 
 struct v4l2_m2m_ctx;
+struct mali_vpu_fw_bin;
+struct mali_vpu_fw_event;
 
 /**
  * struct mali_vpu_port - format state of a src/dst queue in a session.
  * @curr_fmt: V4L2 format currently configured for the port
  * @supported_fmts: List of format descriptors supported by the queue.
+ * @stream_requested: Port/Queue is being prepared for streaming or has
+ *                    successfully completed preparation. Protected by vq_lock.
  */
 struct mali_vpu_port {
 	struct v4l2_format		curr_fmt;
 	struct mali_vpu_format_list	supported_fmts;
+	bool				stream_requested;
+};
+
+/*
+ * Firmware binary load lifecycle
+ *
+ * Preparing the coded queue starts an asynchronous firmware request.
+ * The binary request does not require queued buffers and does not wait for
+ * loading to complete. READY and LOADING requests are reused.
+ *
+ * A cache hit may invoke the callback synchronously. An asynchronous callback
+ * discards its result if neither port requests streaming. A successfully
+ * loaded binary is retained until session cleanup.
+ *
+ * Session cleanup waits for an in-flight callback before releasing the
+ * session-owned binary reference and callback event. On timeout, it cancels
+ * the pending cache waiter or waits for an already claimed callback.
+ */
+enum mali_vpu_session_fw_bin_state {
+	MALI_VPU_SESSION_FW_BIN_UNINITIALIZED = 0,
+	MALI_VPU_SESSION_FW_BIN_LOADING,
+	MALI_VPU_SESSION_FW_BIN_READY,
+	MALI_VPU_SESSION_FW_BIN_FAILED,
 };
 
 /**
@@ -43,6 +71,10 @@ struct mali_vpu_port {
  * @ctrl_handler: V4L2 control handler bound to this session.
  * @fh: V4L2 file handle for this session.
  * @m2m_ctx: V4L2 mem2mem context for this session.
+ * @fw_bin: Pointer to firmware binary loaded for this session.
+ * @fw_event: Firmware binary load event callback structure for this session.
+ * @fw_bin_state: Session firmware binary status.
+ * @fw_load_done: Completion used to synchronize teardown with an in-flight firmware load.
  */
 struct mali_vpu_session {
 	struct mali_vpu_dev		*vpu_dev;
@@ -61,10 +93,16 @@ struct mali_vpu_session {
 
 	struct v4l2_fh		fh;
 	struct v4l2_m2m_ctx	*m2m_ctx;
+
+	struct mali_vpu_fw_bin			*fw_bin;
+	struct mali_vpu_fw_event		*fw_event;
+	enum mali_vpu_session_fw_bin_state	fw_bin_state;
+	struct completion			fw_load_done;
 };
 
 int mali_vpu_session_init(struct mali_vpu_session *sess);
-void mali_vpu_session_cleanup(struct mali_vpu_session *sess);
+int mali_vpu_session_cleanup(struct mali_vpu_session *sess);
+int mali_vpu_session_request_fw_load(struct mali_vpu_session *sess);
 
 static inline struct mali_vpu_session *mali_vpu_get_session(struct file *filp)
 {
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +2 -0
index 19ad2667f2597..0a7481d97c717 100644
@@ -17,6 +17,7 @@
 #include "mali-vpu-variant.h"
 
 #define MALI_VPU_HW_PID_V76 0x56627
+#define MALI_VPU_FW_VER_MAJOR_V76 1
 
 #define MALI_V76_MAX_FRAME_WIDTH 8192
 #define MALI_V76_MAX_FRAME_HEIGHT 8192
@@ -151,6 +152,7 @@ static const struct mali_vpu_ctrl_desc mali_v76_dec_ctrls[] = {
 const struct mali_vpu_variant mali_vpu_v76_variant = {
 	.dma_mask = DMA_BIT_MASK(40),
 	.expected_hw_pid = MALI_VPU_HW_PID_V76,
+	.fw_ver_major = MALI_VPU_FW_VER_MAJOR_V76,
 
 	.rst_names = mali_v76_rst_names,
 	.num_rsts = ARRAY_SIZE(mali_v76_rst_names),
drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h +2 -0
index d316e010fff48..3c37cd70bd81f 100644
@@ -50,6 +50,7 @@ struct mali_vpu_control_caps {
  * @num_rsts: Number of reset controls for this variant.
  * @expected_hw_pid: Expected hardware product ID read from the version register.
  * @dma_mask: DMA addressing capability mask.
+ * @fw_ver_major: Major firmware version compatible with this variant.
  * @fmt_caps: Format capability tables for encode/decode paths.
  * @frmsz_caps: Frame dimension limits and defaults.
  * @ctrl_caps:  Control capability tables for encode/decode paths.
@@ -59,6 +60,7 @@ struct mali_vpu_variant {
 	unsigned int num_rsts;
 	u32 expected_hw_pid;
 	u64 dma_mask;
+	u32 fw_ver_major;
 
 	struct mali_vpu_format_caps	fmt_caps;
 	struct mali_vpu_framesize_caps	frmsz_caps;
drivers/media/platform/arm/mali-vpu/mali-vpu-vb2.c +28 -0
index acdbb06785f6b..4262758a5be26 100644
@@ -59,12 +59,40 @@ static void mali_vpu_vb2_buffer_queue(struct vb2_buffer *vb2_buf)
 	v4l2_m2m_buf_queue(sess->m2m_ctx, v4l2_buf);
 }
 
+static int mali_vpu_vb2_prepare_streaming(struct vb2_queue *q)
+{
+	struct mali_vpu_session *sess = vb2_get_drv_priv(q);
+	struct mali_vpu_port *port = V4L2_TYPE_IS_OUTPUT(q->type) ?
+					&sess->port_src : &sess->port_dst;
+	int ret;
+
+	if (mali_vpu_is_coded_q(sess, q->type)) {
+		ret = mali_vpu_session_request_fw_load(sess);
+		if (ret)
+			return ret;
+	}
+
+	port->stream_requested = true;
+	return 0;
+}
+
+static void mali_vpu_vb2_unprepare_streaming(struct vb2_queue *q)
+{
+	struct mali_vpu_session *sess = vb2_get_drv_priv(q);
+	struct mali_vpu_port *port = V4L2_TYPE_IS_OUTPUT(q->type) ?
+					&sess->port_src : &sess->port_dst;
+
+	port->stream_requested = false;
+}
+
 static const struct vb2_ops mali_vpu_vb2_ops = {
 	.queue_setup		= mali_vpu_vb2_queue_setup,
 	.buf_out_validate	= mali_vpu_vb2_buffer_out_validate,
 	.buf_init		= mali_vpu_vb2_buffer_init,
 	.buf_prepare		= mali_vpu_vb2_buffer_prepare,
 	.buf_queue		= mali_vpu_vb2_buffer_queue,
+	.prepare_streaming	= mali_vpu_vb2_prepare_streaming,
+	.unprepare_streaming	= mali_vpu_vb2_unprepare_streaming,
 };
 
 static void mali_vpu_queue_init_type(struct mali_vpu_session *sess,
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +3 -2
index 88e706be6705e..d9196cc86cdf4 100644
@@ -62,12 +62,13 @@ static int mali_vpu_open(struct file *filp)
 static int mali_vpu_release(struct file *filp)
 {
 	struct mali_vpu_session *sess = mali_vpu_get_session(filp);
+	int ret = 0;
 
-	mali_vpu_session_cleanup(sess);
+	ret = mali_vpu_session_cleanup(sess);
 	v4l2_fh_del(&sess->fh, filp);
 	v4l2_fh_exit(&sess->fh);
 	kfree(sess);
-	return 0;
+	return ret;
 }
 
 const struct v4l2_file_operations mali_vpu_fops = {
Signature · 1 line
2.43.0
#35 lore raw

RFC PATCH 12/28 media: mali-vpu: prepare firmware address space

In reply to Charvi Mehta

Add a per-session firmware object containing the memory required to communicate with and execute the firmware.

Define the fixed firmware virtual-address layout and allocate the host communication pages. Allocate firmware text pages shared by each core instance, private BSS pages for each core and shared BSS pages mapped across all instances. Copy the loaded firmware text into the executable mapping.

Construct the firmware address space when the firmware binary finishes loading, using the number of cores reported by the hardware. Release all the firmware mappings and backing pages during session teardown.

---
 drivers/media/platform/arm/mali-vpu/Makefile  |   3 +-
 .../arm/mali-vpu/mali-vpu-firmware-cache.c    |  55 ++-
 .../arm/mali-vpu/mali-vpu-firmware-cache.h    |   6 +
 .../platform/arm/mali-vpu/mali-vpu-firmware.c | 383 ++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-firmware.h | 108 +++++
 .../platform/arm/mali-vpu/mali-vpu-probe.c    |   6 -
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   |  94 +++++
 .../arm/mali-vpu/mali-vpu-registers.h         |   7 +
 .../platform/arm/mali-vpu/mali-vpu-session.c  |  20 +
 .../platform/arm/mali-vpu/mali-vpu-session.h  |   8 +-
 10 files changed, 675 insertions(+), 15 deletions(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.h
drivers/media/platform/arm/mali-vpu/Makefile +2 -1
index 74e1455c01d8b..168b4bc43d54d 100644
@@ -10,6 +10,7 @@ mali-vpu-objs := mali-vpu-probe.o \
 		mali-vpu-formats.o \
 		mali-vpu-controls.o \
 		mali-vpu-mmu.o \
-		mali-vpu-firmware-cache.o
+		mali-vpu-firmware-cache.o \
+		mali-vpu-firmware.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c +49 -6

89 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.h +6 -0
index ea7d35ef8ca60..181bf164b49a7 100644
@@ -7,6 +7,7 @@
 #define __MALI_VPU_FIRMWARE_CACHE_H__
 
 #include <asm/byteorder.h>
+#include <linux/bits.h>
 #include <linux/firmware.h>
 #include <linux/kref.h>
 #include <linux/list.h>
@@ -171,4 +172,9 @@ void mali_vpu_fw_cache_put(struct mali_vpu_fw_bin *bin);
 bool mali_vpu_fw_cache_cancel_waiter(struct mali_vpu_fw_cache *cache,
 				     struct mali_vpu_fw_event *event);
 
+static inline bool mali_vpu_test_bit_le32(u32 bit, const __le32 *addr)
+{
+	return !!(le32_to_cpu(addr[bit >> 5]) & BIT(bit & 0x1F));
+}
+
 #endif /* __MALI_VPU_FIRMWARE_CACHE_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c new file +383 -0

383 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.h new file +108 -0

108 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c +0 -6
index b87725d7d4cab..123ff4d8fc937 100644
@@ -26,12 +26,6 @@
 #include "mali-vpu-variant.h"
 #include "mali-vpu-vidioc.h"
 
-/* VPU register access helpers */
-static u32 mali_vpu_read_reg(void __iomem *regs_base, u32 off)
-{
-	return readl(regs_base + off);
-}
-
 static void mali_vpu_read_version(struct mali_vpu_dev *vpu_dev, u32 *product_id, u32 *rev,
 				  u32 *patch)
 {
drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h +94 -0

100 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-registers.h +7 -0
index 471a1f2412a7a..b7883b1ea47f3 100644
@@ -7,6 +7,7 @@
 #define __MALI_VPU_REGISTERS_H__
 
 #include <linux/bits.h>
+#include <linux/io.h>
 
 /*
  * Mali VPU register offsets and bitfield definitions.
@@ -14,6 +15,7 @@
  * Offsets are relative to the VPU MMIO base.
  */
 #define MALI_VPU_REG_VERSION 0x00000
+#define MALI_VPU_REG_NCORES  0x00008
 
 /* Version register fields */
 #define MALI_VPU_VERSION_PRODUCT_ID_MASK GENMASK(31, 16)
@@ -21,4 +23,9 @@
 #define MALI_VPU_VERSION_PATCH_MASK      GENMASK(7, 0)
 #define MALI_VPU_PID_SUFFIX_MASK         GENMASK(15, 12)
 
+static inline u32 mali_vpu_read_reg(void __iomem *regs_base, u32 off)
+{
+	return readl(regs_base + off);
+}
+
 #endif /* __MALI_VPU_REGISTERS_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +20 -0
index b9a3960bca4c7..c9a2c683b8dab 100644
@@ -13,8 +13,10 @@
 #include "mali-vpu-session.h"
 #include "mali-vpu-controls.h"
 #include "mali-vpu-dec.h"
+#include "mali-vpu-firmware.h"
 #include "mali-vpu-firmware-cache.h"
 #include "mali-vpu-formats.h"
+#include "mali-vpu-registers.h"
 #include "mali-vpu-variant.h"
 #include "mali-vpu-vb2.h"
 
@@ -136,6 +138,7 @@ static void mali_vpu_session_release_fw_bin(struct mali_vpu_session *sess)
 	lockdep_assert_held(&sess->vq_lock);
 
 	if (sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_READY) {
+		mali_vpu_fw_cleanup(&sess->fw);
 		mali_vpu_fw_cache_put(sess->fw_bin);
 		sess->fw_bin = NULL;
 		sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_UNINITIALIZED;
@@ -208,6 +211,8 @@ static void mali_vpu_session_fw_bin_ready(struct mali_vpu_fw_bin *vpu_fw_bin,
 					  bool same_thread)
 {
 	struct mali_vpu_session *sess = arg;
+	u32 ncores;
+	int ret;
 
 	if (!same_thread)
 		mutex_lock(&sess->vq_lock);
@@ -228,6 +233,21 @@ static void mali_vpu_session_fw_bin_ready(struct mali_vpu_fw_bin *vpu_fw_bin,
 
 	sess->fw_bin = vpu_fw_bin;
 
+	/*
+	 * Get total number of cores on VPU element.
+	 * This will be scheduler decision in subsequent patches.
+	 */
+	ncores = mali_vpu_read_reg(sess->vpu_dev->regs_base, MALI_VPU_REG_NCORES);
+
+	ret = mali_vpu_fw_construct(&sess->fw, sess->fw_bin, sess->vpu_dev->dev,
+				    &sess->mmu, ncores);
+	if (ret) {
+		dev_err(sess->vpu_dev->dev,
+			"failed to initialise firmware: %d\n", ret);
+		sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_FAILED;
+		goto err_cache_put;
+	}
+
 	sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_READY;
 
 	goto out_complete;
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +6 -2
index 0b810d7d6a036..ecd048e3e5c01 100644
@@ -13,6 +13,7 @@
 #include <media/v4l2-fh.h>
 
 #include "mali-vpu-device.h"
+#include "mali-vpu-firmware.h"
 #include "mali-vpu-formats.h"
 #include "mali-vpu-mmu.h"
 
@@ -41,8 +42,9 @@ struct mali_vpu_port {
  * loading to complete. READY and LOADING requests are reused.
  *
  * A cache hit may invoke the callback synchronously. An asynchronous callback
- * discards its result if neither port requests streaming. A successfully
- * loaded binary is retained until session cleanup.
+ * discards its result if neither port requests streaming. Successful callbacks
+ * construct the firmware address space before making the binary READY. The
+ * session retains its binary reference until cleanup.
  *
  * Session cleanup waits for an in-flight callback before releasing the
  * session-owned binary reference and callback event. On timeout, it cancels
@@ -71,6 +73,7 @@ enum mali_vpu_session_fw_bin_state {
  * @ctrl_handler: V4L2 control handler bound to this session.
  * @fh: V4L2 file handle for this session.
  * @m2m_ctx: V4L2 mem2mem context for this session.
+ * @fw: Per-session firmware address space object.
  * @fw_bin: Pointer to firmware binary loaded for this session.
  * @fw_event: Firmware binary load event callback structure for this session.
  * @fw_bin_state: Session firmware binary status.
@@ -94,6 +97,7 @@ struct mali_vpu_session {
 	struct v4l2_fh		fh;
 	struct v4l2_m2m_ctx	*m2m_ctx;
 
+	struct mali_vpu_fw			fw;
 	struct mali_vpu_fw_bin			*fw_bin;
 	struct mali_vpu_fw_event		*fw_event;
 	enum mali_vpu_session_fw_bin_state	fw_bin_state;
Signature · 1 line
2.43.0
#36 lore raw

Re: [RFC PATCH 12/28] media: mali-vpu: prepare firmware address space

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] media: mali-vpu: rounded-up text_pages mapping in mali_vpu_fw_map_one_core() - [Medium] media…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] media: mali-vpu: rounded-up text_pages mapping in mali_vpu_fw_map_one_core()
- [Medium] media: mali-vpu: unmapped core walk in mali_vpu_fw_unmap_core_regions()
--

commit df911b80c99884b502e4cb4ba2acac413ae6c21d
Author: Charvi Mehta <[email protected]>

media: mali-vpu: prepare firmware address space

This commit adds per-session firmware address space management, allocating and mapping firmware text, BSS, shared BSS, and host communication protocol pages across the hardware cores.

Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c new file +0 -0
new file mode 100644
index 0000000000000..3c552b5fb2d9b

[ ... ]

Quoted 18 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c new file +18 -0
+static int mali_vpu_fw_map_one_core(struct mali_vpu_fw *fw,
+				    const struct mali_vpu_fw_bin *fw_bin,
+				    u32 core)
+{
+	/* Map pages for the text and BSS segments for one core. */
+	enum mali_vpu_fw_region region = MALI_VPU_FW_REGION_CORE_0 + core;
+	mali_vpu_mmu_va begin, end, va;
+	const struct mali_vpu_fw_header *hdr = fw_bin->header;
+	u32 bss_count = core * fw_bin->bss_page_count;
+	u32 shared_bss_count = 0;
+	u32 master_rw_start, master_rw_size, bss_bitmap_size, i;
+	bool is_shared;
+	int ret;
+
+	/*
+	 * Get the base address where the pages for this core should be
+	 * mapped.
+	 */

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c line 218

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c new file +10 -0
+	ret = mali_vpu_fw_get_region(region, &begin, &end);
+	if (ret)
+		return ret;
+
+	/* Map text segment. */
+	ret = mali_vpu_mmu_map_pages(fw->mmu, begin + MALI_VPU_FW_TEXT_BASE_ADDR,
+				     fw->text_pages, MALI_VPU_ATTR_PRIVATE,
+				     MALI_VPU_ACCESS_EXECUTABLE);
+	if (ret)
+		return ret;

[Severity: Medium]
Can mali_vpu_mmu_map_pages() map trailing VPU subpages of fw->text_pages into the BSS range or past the end of the core region on 16K/64K PAGE_SIZE kernels?

mali_vpu_mmu_alloc_pages() rounds fw->text_pages->count up to a multiple of MALI_VPU_PAGES_PER_LINUX_PAGE, whereas mali_vpu_fw_parse_header() validates bss_start >= text_end and text_end <= region_size at 4 KB VPU page granularity:

drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c:mali_vpu_mmu_alloc_pages() {
    ...
	count = roundup(count, MALI_VPU_PAGES_PER_LINUX_PAGE);
	capacity = roundup(capacity, MALI_VPU_PAGES_PER_LINUX_PAGE);
    ...
}

When fw_bin->text_page_count is not aligned to MALI_VPU_PAGES_PER_LINUX_PAGE and the 4 KB-aligned bss_start falls within the trailing subpages of the last Linux page, mali_vpu_mmu_map_dma() below fails with -EAGAIN because the PTE is already occupied. If fw_bin->text_page_count lies in the final MALI_VPU_PAGES_PER_LINUX_PAGE - 1 pages of the 256-page core region, the rounded-up mapping at begin + MALI_VPU_FW_TEXT_BASE_ADDR also spills into begin + 0x100000 (outside [begin, end)), leaving dangling PTEs on teardown for cores 0 .. ncores - 2 because mali_vpu_fw_unmap_core_regions() only unmaps [begin, end) and mali_vpu_mmu_free_pages() only unmaps the single fw->text_pages->va recorded by the last core.

Quoted 55 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c new file +55 -0
+
+	bss_bitmap_size = le32_to_cpu(hdr->bss_bitmap_size);
+	master_rw_start = le32_to_cpu(hdr->master_rw_start_address);
+	master_rw_size = le32_to_cpu(hdr->master_rw_size);
+
+	/* Map bss shared and private pages. */
+	va = le32_to_cpu(hdr->bss_start_address);
+	for (i = 0; i < bss_bitmap_size; i++) {
+		dma_addr_t dma;
+
+		is_shared = (va >= master_rw_start &&
+			     va < (master_rw_start + master_rw_size));
+
+		if (is_shared) {
+			dma = fw->shared_bss_pages->pages[shared_bss_count++].dma;
+		} else if (mali_vpu_test_bit_le32(i, hdr->bss_bitmap)) {
+			dma = fw->bss_pages->pages[bss_count++].dma;
+		} else {
+			va += MALI_VPU_PAGE_SIZE;
+			continue;
+		}
+
+		ret = mali_vpu_mmu_map_dma(fw->mmu, begin + va, dma, MALI_VPU_PAGE_SIZE,
+					   MALI_VPU_ATTR_PRIVATE, MALI_VPU_ACCESS_READ_WRITE);
+		if (ret)
+			return ret;
+
+		va += MALI_VPU_PAGE_SIZE;
+	}
+
+	return 0;
+}
+
+static void mali_vpu_fw_unmap_core_regions(struct mali_vpu_fw *fw)
+{
+	enum mali_vpu_fw_region region;
+	mali_vpu_mmu_va begin, end;
+	u32 core;
+	int ret;
+
+	if (!fw)
+		return;
+
+	for (core = 0; core < fw->ncores; core++) {
+		region = MALI_VPU_FW_REGION_CORE_0 + core;
+		ret = mali_vpu_fw_get_region(region, &begin, &end);
+		if (!ret)
+			mali_vpu_mmu_unmap_va(fw->mmu, begin, end - begin);
+	}
+
+	if (!IS_ERR_OR_NULL(fw->text_pages)) {
+		mali_vpu_mmu_free_pages(fw->text_pages);
+		fw->text_pages = NULL;
+	}
+

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c line 283

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c new file +10 -0
+	if (!IS_ERR_OR_NULL(fw->bss_pages)) {
+		mali_vpu_mmu_free_pages(fw->bss_pages);
+		fw->bss_pages = NULL;
+	}
+
+	if (!IS_ERR_OR_NULL(fw->shared_bss_pages)) {
+		mali_vpu_mmu_free_pages(fw->shared_bss_pages);
+		fw->shared_bss_pages = NULL;
+	}
+}

[Severity: Medium]
Does calling mali_vpu_fw_unmap_core_regions() from the err_unmap path in mali_vpu_fw_map_core_regions() walk unallocated L2 page tables for cores that were never mapped?

mali_vpu_fw_construct() sets fw->ncores before calling mali_vpu_fw_map_core_regions(), and each core's L2 page table is only allocated on demand when mali_vpu_fw_map_one_core() maps its pages. If page allocation or per-core mapping fails before all fw->ncores cores are mapped, mali_vpu_mmu_unmap_va() walks 256 pages per unmapped core and mali_vpu_page_table_walk() logs a dev_err() on every 4 KB page:

drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c:mali_vpu_page_table_walk() {
    ...
	if (!l2->linux_page) {
		if (!alloc) {
			dev_err(mmu->dev, "missing L2 page in PTW. va=0x%x", va);
			return ERR_PTR(-EFAULT);
		}
    ...
}

Also, when mali_vpu_mmu_alloc_pages() fails, the !IS_ERR_OR_NULL() checks above leave the ERR_PTR value stored in fw->text_pages, fw->bss_pages, or fw->shared_bss_pages instead of clearing it to NULL.

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

RFC PATCH 13/28 media: mali-vpu: add firmware message transport

In reply to Charvi Mehta

Add transport support for exchanging messages with the firmware over the shared circular message queues.

Provide helpers for submitting job, ping and state-change requests and for parsing firmware state-change, error, job, switch and pong responses. Validate queue positions and message boundaries, and synchronize the shared pages around CPU and device access.

This provides the common message transport needed by the subsequent firmware control and scheduler support.

---
 .../platform/arm/mali-vpu/mali-vpu-firmware.c | 370 ++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-firmware.h |  83 ++++
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   | 145 +++++++
 3 files changed, 598 insertions(+)
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +370 -0
index 3c552b5fb2d9b..4aead9a062260 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/device.h>
+#include <linux/dma-mapping.h>
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/string.h>
@@ -381,3 +382,372 @@ void mali_vpu_fw_cleanup(struct mali_vpu_fw *fw)
 	mali_vpu_fw_unmap_core_regions(fw);
 	memset(fw, 0, sizeof(*fw));
 }
+
+/*
+ * Read arbitrary byte count from circular u32 queue payload area.
+ * Return new read position in the queue.
+ */
+static u16 mali_vpu_fw_read32n(u32 *src, u16 offset, u32 *dst, size_t size)
+{
+	u32 v;
+
+	for (; size >= sizeof(u32); size -= sizeof(u32)) {
+		*dst++ = READ_ONCE(src[offset]);
+		offset = (offset + 1) % MVE_COMM_QUEUE_SIZE_IN_WORDS;
+	}
+
+	if (size) {
+		v = READ_ONCE(src[offset]);
+		memcpy(dst, &v, size);
+		offset = (offset + 1) % MVE_COMM_QUEUE_SIZE_IN_WORDS;
+	}
+
+	return offset;
+}
+
+static int mali_vpu_fw_read_msg(struct mali_vpu_fw *fw,
+				struct mali_vpu_fw_proto_page *host_page,
+				struct mali_vpu_fw_proto_page *mve_page,
+				u16 *code, void *payload, size_t *size)
+{
+	struct mve_comm_area_host *host = host_page->cpu;
+	struct mve_comm_area_mve *mve = mve_page->cpu;
+	struct mve_msg_header hdr;
+	u16 rpos, out_wpos;
+	ssize_t capacity;
+
+	if (!host || !mve)
+		return -EINVAL;
+
+	dma_sync_single_for_cpu(fw->dev, mve_page->dma, MALI_VPU_PAGE_SIZE,
+				DMA_FROM_DEVICE);
+
+	rpos = READ_ONCE(host->out_rpos);
+	out_wpos = READ_ONCE(mve->out_wpos);
+
+	if (rpos >= MVE_COMM_QUEUE_SIZE_IN_WORDS ||
+	    out_wpos >= MVE_COMM_QUEUE_SIZE_IN_WORDS)
+		return -EPROTO;
+
+	/* Calculate how much data that is available in the buffer. */
+	capacity = out_wpos - rpos;
+	if (capacity < 0)
+		capacity += MVE_COMM_QUEUE_SIZE_IN_WORDS;
+
+	if (capacity <= 0)
+		return 0;
+
+	/* Firmware publishes out_wpos after writing the message. */
+	dma_rmb();
+
+	rpos = mali_vpu_fw_read32n(mve->out_data, rpos, (u32 *)&hdr, sizeof(hdr));
+
+	/* Make sure there is enough space for both header and message. */
+	capacity -= DIV_ROUND_UP(sizeof(hdr) + hdr.size, sizeof(u32));
+	if (capacity < 0) {
+		dev_err(fw->dev,
+			"fw msg larger than capacity code=%u size=%u, wpos=%u, rpos=%u\n",
+			hdr.code, hdr.size, out_wpos, rpos);
+		return -EPROTO;
+	}
+
+	if (hdr.size > *size)
+		return -EMSGSIZE;
+
+	rpos = mali_vpu_fw_read32n(mve->out_data, rpos,
+				   (u32 *)payload, hdr.size);
+
+	/*
+	 * Finish reading the message before publishing updated consumed queue
+	 * positions to the firmware.
+	 */
+	dma_mb();
+	WRITE_ONCE(host->out_rpos, rpos);
+
+	dma_sync_single_for_device(fw->dev,
+				   host_page->dma + offsetof(struct mve_comm_area_host, out_rpos),
+				   sizeof(host->out_rpos), DMA_TO_DEVICE);
+
+	*code = hdr.code;
+	*size = hdr.size;
+
+	return 1;
+}
+
+/*
+ * Write arbitrary byte count to circular u32 queue payload area.
+ * Return new write position in the queue.
+ */
+static u16 mali_vpu_fw_write32n(u32 *dst, u16 offset, const u32 *src, size_t size)
+{
+	u32 v;
+
+	for (; size >= sizeof(u32); size -= sizeof(u32)) {
+		WRITE_ONCE(dst[offset], *src++);
+		offset = (offset + 1) % MVE_COMM_QUEUE_SIZE_IN_WORDS;
+	}
+
+	if (size) {
+		v = 0;
+		memcpy(&v, src, size);
+		WRITE_ONCE(dst[offset], v);
+		offset = (offset + 1) % MVE_COMM_QUEUE_SIZE_IN_WORDS;
+	}
+
+	return offset;
+}
+
+static int mali_vpu_fw_write_msg(struct mali_vpu_fw *fw,
+				 struct mali_vpu_fw_proto_page *host_page,
+				 struct mali_vpu_fw_proto_page *mve_page,
+				 u16 code, const void *payload, size_t size)
+{
+	struct mve_comm_area_host *host = host_page->cpu;
+	struct mve_comm_area_mve *mve = mve_page->cpu;
+	struct mve_msg_header hdr = { .code = code, .size = size};
+	u16 wpos, in_rpos;
+	int capacity;
+
+	if (!host || !mve)
+		return -EINVAL;
+
+	dma_sync_single_for_cpu(fw->dev,
+				mve_page->dma + offsetof(struct mve_comm_area_mve, in_rpos),
+				sizeof(mve->in_rpos), DMA_FROM_DEVICE);
+
+	wpos = READ_ONCE(host->in_wpos);
+	in_rpos = READ_ONCE(mve->in_rpos);
+
+	if (wpos >= MVE_COMM_QUEUE_SIZE_IN_WORDS || in_rpos >= MVE_COMM_QUEUE_SIZE_IN_WORDS)
+		return -EPROTO;
+
+	/* Calculate how much space that is available in the buffer. */
+	capacity = in_rpos - wpos;
+	if (capacity <= 0)
+		capacity += MVE_COMM_QUEUE_SIZE_IN_WORDS;
+
+	/* Make sure there is enough space for both header and message. */
+	capacity -= DIV_ROUND_UP(sizeof(hdr) + size, sizeof(u32));
+	if (capacity <= 0)
+		return -ENOMEM;
+
+	wpos = mali_vpu_fw_write32n(host->in_data, wpos, (const u32 *)&hdr, sizeof(hdr));
+	wpos = mali_vpu_fw_write32n(host->in_data, wpos, (const u32 *)payload, size);
+
+	/*
+	 * Publish the producer position only after the complete message has
+	 * been written.
+	 */
+	dma_wmb();
+	WRITE_ONCE(host->in_wpos, wpos);
+
+	dma_sync_single_for_device(fw->dev, host_page->dma, sizeof(*host), DMA_TO_DEVICE);
+
+	return 0;
+}
+
+static size_t mali_vpu_get_capacity_bytes(int rpos, int wpos)
+{
+	ssize_t capacity;
+
+	capacity = wpos - rpos;
+	if (capacity < 0)
+		capacity += MVE_COMM_QUEUE_SIZE_IN_WORDS;
+
+	return capacity * sizeof(u32);
+}
+
+static void mali_vpu_fw_print_debug(struct mali_vpu_fw *fw)
+{
+	struct mve_comm_area_host *host = fw->msg_host.cpu;
+	struct mve_comm_area_mve *mve = fw->msg_mve.cpu;
+	struct mve_msg_header hdr;
+	u16 rpos, wpos;
+	size_t capacity;
+
+	dma_sync_single_for_cpu(fw->dev, fw->msg_mve.dma, MALI_VPU_PAGE_SIZE,
+				DMA_FROM_DEVICE);
+
+	dev_dbg(fw->dev,
+		"dump message q. host={out_rpos=%u, in_wpos=%u}, mve={out_wpos=%u, in_rpos=%u}\n",
+		host->out_rpos, host->in_wpos, mve->out_wpos, mve->in_rpos);
+
+	rpos = READ_ONCE(host->out_rpos);
+	wpos = READ_ONCE(mve->out_wpos);
+
+	if (rpos >= MVE_COMM_QUEUE_SIZE_IN_WORDS ||
+	    wpos >= MVE_COMM_QUEUE_SIZE_IN_WORDS)
+		return;
+
+	/* Firmware publishes out_wpos after writing the message. */
+	dma_rmb();
+
+	while ((capacity = mali_vpu_get_capacity_bytes(rpos, wpos)) >= sizeof(hdr)) {
+		unsigned int pos;
+
+		pos = mali_vpu_fw_read32n(mve->out_data, rpos, (u32 *)&hdr, sizeof(hdr));
+
+		dev_dbg(fw->dev,
+			"queue={rpos=%u, wpos=%u, capacity=%zu}, msg={code=%u, size=%u}\n",
+			rpos, wpos, capacity, hdr.code, hdr.size);
+
+		capacity = mali_vpu_get_capacity_bytes(pos, wpos);
+		if (hdr.size > capacity) {
+			dev_dbg(fw->dev,
+				"size is larger than capacity. capacity=%zu, size=%u.\n",
+				capacity, hdr.size);
+			return;
+		}
+
+		rpos = (pos + DIV_ROUND_UP(hdr.size, sizeof(u32))) % MVE_COMM_QUEUE_SIZE_IN_WORDS;
+	}
+}
+
+int mali_vpu_fw_send_job(struct mali_vpu_fw *fw, u16 cores, u16 frames)
+{
+	struct mve_request_job job = {
+		.cores = cores,
+		.frames = frames,
+		.flags = 0,
+	};
+
+	return mali_vpu_fw_write_msg(fw, &fw->msg_host, &fw->msg_mve,
+				     MVE_REQUEST_CODE_JOB, &job, sizeof(job));
+}
+
+int mali_vpu_fw_send_ping(struct mali_vpu_fw *fw)
+{
+	return mali_vpu_fw_write_msg(fw, &fw->msg_host, &fw->msg_mve,
+				     MVE_REQUEST_CODE_PING, NULL, 0);
+}
+
+int mali_vpu_fw_send_state_change(struct mali_vpu_fw *fw, enum mali_vpu_fw_state state)
+{
+	u16 code;
+
+	switch (state) {
+	case MALI_VPU_FW_STATE_STOPPED:
+		code = MVE_REQUEST_CODE_STOP;
+		break;
+	case MALI_VPU_FW_STATE_RUNNING:
+		code = MVE_REQUEST_CODE_GO;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return mali_vpu_fw_write_msg(fw, &fw->msg_host, &fw->msg_mve, code, NULL, 0);
+}
+
+int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg)
+{
+	u16 code;
+	union {
+		struct mve_response_state_change state_change;
+		struct mve_response_error error;
+	} fw_msg = {};
+	size_t size = sizeof(fw_msg);
+	int ret;
+
+	if (!fw || !msg)
+		return -EINVAL;
+
+	ret = mali_vpu_fw_read_msg(fw, &fw->msg_host, &fw->msg_mve, &code,
+				   &fw_msg, &size);
+	if (ret <= 0)
+		return ret; /* 0 = no msg, <0 = error */
+
+	memset(msg, 0, sizeof(*msg));
+
+	switch (code) {
+	case MVE_RESPONSE_CODE_SWITCHED_IN:
+		msg->code = MALI_VPU_FW_CODE_SWITCH_IN;
+		break;
+	case MVE_RESPONSE_CODE_SWITCHED_OUT:
+		msg->code = MALI_VPU_FW_CODE_SWITCH_OUT;
+		break;
+	case MVE_RESPONSE_CODE_JOB_DEQUEUED:
+		msg->code = MALI_VPU_FW_CODE_JOB;
+		break;
+	case MVE_RESPONSE_CODE_PONG:
+		msg->code = MALI_VPU_FW_CODE_PONG;
+		break;
+	case MVE_RESPONSE_CODE_IDLE:
+		msg->code = MALI_VPU_FW_CODE_IDLE;
+		break;
+	case MVE_RESPONSE_CODE_STATE_CHANGE: {
+		if (size != sizeof(fw_msg.state_change))
+			return -EPROTO;
+
+		msg->code = MALI_VPU_FW_CODE_STATE_CHANGE;
+
+		switch (fw_msg.state_change.new_state) {
+		case MVE_STATE_STOPPED:
+			msg->data.state = MALI_VPU_FW_STATE_STOPPED;
+			break;
+		case MVE_STATE_RUNNING:
+			msg->data.state = MALI_VPU_FW_STATE_RUNNING;
+			break;
+		default:
+			return -EPROTO;
+		}
+		break;
+	}
+	case MVE_RESPONSE_CODE_ERROR: {
+		size_t message_offset = offsetof(struct mve_response_error, message);
+		size_t message_size;
+
+		if (size < message_offset + 1 || size > sizeof(fw_msg.error))
+			return -EPROTO;
+
+		message_size = size - message_offset;
+		if (fw_msg.error.message[message_size - 1] != '\0')
+			return -EPROTO;
+
+		msg->code = MALI_VPU_FW_CODE_ERROR;
+
+		switch (fw_msg.error.error_code) {
+		case MVE_ERROR_ABORT:
+			msg->data.error.error_code = MALI_VPU_FW_ERROR_ABORT;
+			break;
+		case MVE_ERROR_OUT_OF_MEMORY:
+			msg->data.error.error_code = MALI_VPU_FW_ERROR_OUT_OF_MEMORY;
+			break;
+		case MVE_ERROR_ASSERT:
+			msg->data.error.error_code = MALI_VPU_FW_ERROR_ASSERT;
+			break;
+		case MVE_ERROR_UNSUPPORTED:
+			msg->data.error.error_code = MALI_VPU_FW_ERROR_UNSUPPORTED;
+			break;
+		case MVE_ERROR_INVALID_BUFFER:
+			msg->data.error.error_code = MALI_VPU_FW_ERROR_INVALID_BUFFER;
+			break;
+		case MVE_ERROR_INVALID_STATE:
+			msg->data.error.error_code = MALI_VPU_FW_ERROR_INVALID_STATE;
+			break;
+		case MVE_ERROR_WATCHDOG:
+			msg->data.error.error_code = MALI_VPU_FW_ERROR_WATCHDOG;
+			break;
+		default:
+			msg->data.error.error_code = MALI_VPU_FW_ERROR_UNKNOWN;
+			dev_warn(fw->dev, "unsupported fw error code. code=%u.\n",
+				 fw_msg.error.error_code);
+			break;
+		}
+
+		strscpy(msg->data.error.message, fw_msg.error.message,
+			min(sizeof(msg->data.error.message), sizeof(fw_msg.error.message)));
+
+		break;
+	}
+	default:
+		dev_warn(fw->dev,
+			 "unknown fw message code. code=%u, size=%zu.\n",
+			 code, size);
+		msg->code = MALI_VPU_FW_CODE_UNKNOWN;
+		mali_vpu_fw_print_debug(fw);
+		break;
+	}
+
+	return ret;
+}
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.h +83 -0

94 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h +145 -0

151 lines not on this page · raw message

Signature · 1 line
2.43.0
#38 lore raw

RFC PATCH 14/28 media: mali-vpu: route decoder controls to firmware

In reply to Charvi Mehta

Translate decoder controls to firmware SET_OPTION requests and add parsing for option confirmation and failures. Validate message payload sizes and distinguish rejected options from successful requests.

Track outstanding requests awaiting a firmware response.

Use the firmware binary state as the single source of firmware readiness and remove the redundant readiness boolean.

---
 .../platform/arm/mali-vpu/mali-vpu-controls.c |  40 +++--
 .../platform/arm/mali-vpu/mali-vpu-dec.c      |  18 +-
 .../platform/arm/mali-vpu/mali-vpu-firmware.c | 159 +++++++++++++++++-
 .../platform/arm/mali-vpu/mali-vpu-firmware.h |  66 +++++++-
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   |  68 +++++++-
 .../platform/arm/mali-vpu/mali-vpu-session.c  |   7 +-
 .../platform/arm/mali-vpu/mali-vpu-session.h  |   4 +-
 7 files changed, 328 insertions(+), 34 deletions(-)
drivers/media/platform/arm/mali-vpu/mali-vpu-controls.c +26 -14
index d4921eddf0fcf..100f9185dbec1 100644
@@ -56,7 +56,7 @@ static int mali_vpu_apply_or_stage_ctrl(struct mali_vpu_session *sess,
 	bool staged;
 	int ret;
 
-	staged = !sess->is_fw_ready;
+	staged = sess->fw_bin_state != MALI_VPU_SESSION_FW_BIN_READY;
 
 	if (!staged) {
 		ret = desc->set(sess, state->cid, value);
@@ -267,7 +267,7 @@ int mali_vpu_apply_staged_controls(struct mali_vpu_session *sess)
 	u32 i;
 	int ret;
 
-	if (!sess->is_fw_ready)
+	if (sess->fw_bin_state != MALI_VPU_SESSION_FW_BIN_READY)
 		return -EINVAL;
 
 	for (i = 0; i < sess->num_ctrls_caps; i++) {
@@ -288,28 +288,36 @@ int mali_vpu_apply_staged_controls(struct mali_vpu_session *sess)
 int mali_vpu_ctrl_set_nalu_format(struct mali_vpu_session *sess, u32 cid,
 				  s64 value)
 {
+	struct mali_vpu_fw_set_option option;
+
 	if (cid != V4L2_CID_MALI_VPU_NALU_FORMAT)
 		return -EINVAL;
 
-	(void)sess;
-	(void)value;
-	return 0;
+	option.code = MALI_VPU_FW_SET_NALU_FORMAT;
+	option.nalu_format = value;
+
+	return mali_vpu_session_fw_send_set_option(sess, &option);
 }
 
 int mali_vpu_ctrl_set_intbuf_size(struct mali_vpu_session *sess, u32 cid,
 				  s64 value)
 {
+	struct mali_vpu_fw_set_option option;
+
 	if (cid != V4L2_CID_MALI_VPU_INTBUF_SIZE || value < 0 || value > INT_MAX)
 		return -EINVAL;
 
-	(void)sess;
-	(void)value;
-	return 0;
+	option.code = MALI_VPU_FW_SET_INTBUF_SIZE;
+	option.intbuf_size = value;
+
+	return mali_vpu_session_fw_send_set_option(sess, &option);
 }
 
 int mali_vpu_ctrl_set_stream_escaping(struct mali_vpu_session *sess, u32 cid,
 				      s64 value)
 {
+	struct mali_vpu_fw_set_option option;
+
 	if (cid != V4L2_CID_MALI_VPU_STREAM_ESCAPING)
 		return -EINVAL;
 
@@ -319,21 +327,25 @@ int mali_vpu_ctrl_set_stream_escaping(struct mali_vpu_session *sess, u32 cid,
 	if (value != 0 && value != 1)
 		return -EINVAL;
 
-	(void)sess;
-	(void)value;
-	return 0;
+	option.code = MALI_VPU_FW_SET_STREAM_ESCAPING;
+	option.stream_escaping = value;
+
+	return mali_vpu_session_fw_send_set_option(sess, &option);
 }
 
 int mali_vpu_ctrl_set_host_print_enable(struct mali_vpu_session *sess, u32 cid,
 					s64 value)
 {
+	struct mali_vpu_fw_set_option option;
+
 	if (cid != V4L2_CID_MALI_VPU_HOST_PRINT_ENABLE)
 		return -EINVAL;
 
 	if (value != 0 && value != 1)
 		return -EINVAL;
 
-	(void)sess;
-	(void)value;
-	return 0;
+	option.code = MALI_VPU_FW_SET_HOST_PROTOCOL_PRINTS;
+	option.host_protocol_prints = value;
+
+	return mali_vpu_session_fw_send_set_option(sess, &option);
 }
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c +12 -6
index 7e78c475a4ff3..51d509abb9dd1 100644
@@ -28,6 +28,8 @@ void mali_vpu_session_init_dec(struct mali_vpu_session *sess)
 int mali_vpu_ctrl_set_ignore_stream_headers(struct mali_vpu_session *sess,
 					    u32 cid, s64 value)
 {
+	struct mali_vpu_fw_set_option option;
+
 	if (cid != V4L2_CID_MALI_VPU_IGNORE_STREAM_HEADERS)
 		return -EINVAL;
 
@@ -37,14 +39,17 @@ int mali_vpu_ctrl_set_ignore_stream_headers(struct mali_vpu_session *sess,
 	if (value != 0 && value != 1)
 		return -EINVAL;
 
-	(void)sess;
-	(void)value;
-	return 0;
+	option.code = MALI_VPU_FW_SET_IGNORE_STREAM_HEADERS;
+	option.ignore_stream_headers = value;
+
+	return mali_vpu_session_fw_send_set_option(sess, &option);
 }
 
 int mali_vpu_ctrl_set_frame_reordering(struct mali_vpu_session *sess, u32 cid,
 				       s64 value)
 {
+	struct mali_vpu_fw_set_option option;
+
 	if (cid != V4L2_CID_MALI_VPU_FRAME_REORDERING)
 		return -EINVAL;
 
@@ -54,7 +59,8 @@ int mali_vpu_ctrl_set_frame_reordering(struct mali_vpu_session *sess, u32 cid,
 	if (value != 0 && value != 1)
 		return -EINVAL;
 
-	(void)sess;
-	(void)value;
-	return 0;
+	option.code = MALI_VPU_FW_SET_FRAME_REORDERING;
+	option.frame_reordering = value;
+
+	return mali_vpu_session_fw_send_set_option(sess, &option);
 }
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +158 -1

183 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.h +57 -9

108 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h +67 -1

98 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +6 -1
index c9a2c683b8dab..36197e82461be 100644
@@ -68,6 +68,12 @@ mali_vpu_both_ports_unrequested(struct mali_vpu_session *sess)
 	       !sess->port_dst.stream_requested;
 }
 
+int mali_vpu_session_fw_send_set_option(struct mali_vpu_session *sess,
+					struct mali_vpu_fw_set_option *opt)
+{
+	return mali_vpu_fw_send_set_option(&sess->fw, opt);
+}
+
 int mali_vpu_session_init(struct mali_vpu_session *sess)
 {
 	int ret;
@@ -91,7 +97,6 @@ int mali_vpu_session_init(struct mali_vpu_session *sess)
 		goto err_mutex_destroy;
 	}
 
-	sess->is_fw_ready = false;
 	sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_UNINITIALIZED;
 
 	ret = mali_vpu_ctrls_init(sess);
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +2 -2
index ecd048e3e5c01..f0aa98005e92a 100644
@@ -65,7 +65,6 @@ enum mali_vpu_session_fw_bin_state {
  * @port_dst: Destination (CAPTURE) port for this session.
  * @mmu: MMU handle for this session
  * @vq_lock: Mutex lock to serialise per-session queue related ioctls.
- * @is_fw_ready: True if the firmware is loaded and ready to accept commands.
  * @min_buffers_required: Driver-reported minimum buffers for role-specific queue.
  * @ctrls_caps: Variant-provided immutable control capability table.
  * @num_ctrls_caps: Number of entries in @ctrls_caps.
@@ -86,7 +85,6 @@ struct mali_vpu_session {
 	struct mali_vpu_port		port_dst;
 	struct mali_vpu_mmu		mmu;
 	struct mutex			vq_lock; /* lock to serialise per-session ioctls */
-	bool				is_fw_ready;
 	u32				min_buffers_required;
 
 	const struct mali_vpu_ctrl_desc *ctrls_caps;
@@ -107,6 +105,8 @@ struct mali_vpu_session {
 int mali_vpu_session_init(struct mali_vpu_session *sess);
 int mali_vpu_session_cleanup(struct mali_vpu_session *sess);
 int mali_vpu_session_request_fw_load(struct mali_vpu_session *sess);
+int mali_vpu_session_fw_send_set_option(struct mali_vpu_session *sess,
+					struct mali_vpu_fw_set_option *opt);
 
 static inline struct mali_vpu_session *mali_vpu_get_session(struct file *filp)
 {
Signature · 1 line
2.43.0
#39 lore raw

RFC PATCH 15/28 media: mali-vpu: handle firmware RPC requests

In reply to Charvi Mehta

Handle firmware requests for debug output and memory allocation, resizing and release. Track allocations by firmware virtual address and release outstanding allocations during firmware teardown.

Flag resize and free requests so the caller can invalidate cached firmware MMU translations before resuming execution.

---
 .../platform/arm/mali-vpu/mali-vpu-firmware.c | 277 ++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-firmware.h |  12 +
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   |  53 ++++
 3 files changed, 342 insertions(+)
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +277 -0
index 91a989c098cbc..c11d5c6c0ec5f 100644
@@ -7,6 +7,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/err.h>
 #include <linux/errno.h>
+#include <linux/sizes.h>
 #include <linux/string.h>
 
 #include "mali-vpu-firmware.h"
@@ -15,6 +16,7 @@
 #include "mali-vpu-protocol-def-v3.h"
 
 #define MALI_VPU_FW_MAX_CORES 8
+#define MALI_VPU_RPC_VA_GRANULE SZ_16M
 
 static int mali_vpu_fw_get_region(enum mali_vpu_fw_region region,
 				  mali_vpu_mmu_va *begin, mali_vpu_mmu_va *end)
@@ -80,6 +82,14 @@ static int mali_vpu_fw_get_region(enum mali_vpu_fw_region region,
 		*begin = MVE_MEM_REGION_FW_INSTANCE7_ADDR_BEGIN;
 		*end = MVE_MEM_REGION_FW_INSTANCE7_ADDR_END;
 		break;
+	case MALI_VPU_FW_REGION_PROTECTED:
+		*begin = MVE_MEM_REGION_PROTECTED_ADDR_BEGIN;
+		*end = MVE_MEM_REGION_PROTECTED_ADDR_END;
+		break;
+	case MALI_VPU_FW_REGION_FRAMEBUF:
+		*begin = MVE_MEM_REGION_FRAMEBUF_ADDR_BEGIN;
+		*end = MVE_MEM_REGION_FRAMEBUF_ADDR_END;
+		break;
 	default:
 		return -EINVAL;
 	}
@@ -352,6 +362,7 @@ int mali_vpu_fw_construct(struct mali_vpu_fw *fw, const struct mali_vpu_fw_bin *
 	fw->dev = dev;
 	fw->mmu = mmu;
 	fw->ncores = ncores;
+	hash_init(fw->rpc_mem);
 
 	/* Initialise firmware core and protocol regions */
 	ret = mali_vpu_fw_map_core_regions(fw, fw_bin);
@@ -375,11 +386,21 @@ int mali_vpu_fw_construct(struct mali_vpu_fw *fw, const struct mali_vpu_fw_bin *
 
 void mali_vpu_fw_cleanup(struct mali_vpu_fw *fw)
 {
+	struct mali_vpu_mmu_pages *pages;
+	struct hlist_node *tmp;
+	unsigned int bkt;
+
 	if (!fw)
 		return;
 
 	mali_vpu_fw_unmap_protocol(fw);
 	mali_vpu_fw_unmap_core_regions(fw);
+
+	hash_for_each_safe(fw->rpc_mem, bkt, tmp, pages, node) {
+		hash_del(&pages->node);
+		mali_vpu_mmu_free_pages(pages);
+	}
+
 	memset(fw, 0, sizeof(*fw));
 }
 
@@ -908,3 +929,259 @@ int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg)
 
 	return ret;
 }
+
+static void mali_vpu_fw_dbg_rpc(struct mali_vpu_fw *fw, const char *direction,
+				const struct mve_rpc_communication_area *rpc)
+{
+	dev_dbg(fw->dev, "rpc %s: state=%u call_id=%u size=%u\n",
+		direction, rpc->state, rpc->call_id, rpc->size);
+}
+
+static struct mali_vpu_mmu_pages *mali_vpu_find_pages(struct mali_vpu_fw *fw,
+						      mali_vpu_mmu_va va)
+{
+	struct mali_vpu_mmu_pages *pages;
+
+	hash_for_each_possible(fw->rpc_mem, pages, node, va) {
+		if (pages->va == va)
+			return pages;
+	}
+
+	return NULL;
+}
+
+static void mali_vpu_rpc_mem_resize(struct mali_vpu_fw *fw,
+				    struct mve_rpc_communication_area *rpc_area)
+{
+	union mve_rpc_params *p = &rpc_area->params;
+	struct mali_vpu_mmu_pages *pages;
+	mali_vpu_mmu_va va = 0;
+	int ret;
+
+	pages = mali_vpu_find_pages(fw, p->mem_resize.ve_pointer);
+	if (pages) {
+		size_t npages;
+
+		/* Resize the allocated pages. */
+		npages = DIV_ROUND_UP(p->mem_resize.new_size, MALI_VPU_PAGE_SIZE);
+		ret = mali_vpu_mmu_resize_pages(pages, npages);
+		if (ret) {
+			dev_warn(fw->dev,
+				 "failed to resize RPC mapped pages. ret=%d\n",
+				 ret);
+			goto err;
+		}
+
+		va = pages->va;
+	} else {
+		dev_warn(fw->dev, "could not find pages for RPC resize. va=0x%x\n",
+			 p->mem_resize.ve_pointer);
+	}
+
+	dev_dbg(fw->dev, "RPC resize memory. va=0x%x, new_size=%u\n",
+		p->mem_resize.ve_pointer, p->mem_resize.new_size);
+
+err:
+	/*
+	 * The memory RPC ABI returns the resulting firmware VA. Firmware treats
+	 * a zero VA as allocation failure and reports MVE_ERROR_OUT_OF_MEMORY.
+	 * The RPC itself was still handled successfully.
+	 */
+	rpc_area->size = sizeof(u32);
+	p->data[0] = va;
+}
+
+static void mali_vpu_rpc_mem_alloc(struct mali_vpu_fw *fw,
+				   struct mve_rpc_communication_area *rpc_area)
+{
+	union mve_rpc_params *p = &rpc_area->params;
+	enum mali_vpu_fw_region region;
+	struct mali_vpu_mmu_pages *pages;
+	size_t npages, max_pages;
+	mali_vpu_mmu_va end, begin = 0;
+	int ret;
+
+	if (!p->mem_alloc.size ||
+	    p->mem_alloc.size > p->mem_alloc.max_size) {
+		dev_warn(fw->dev,
+			 "invalid RPC allocation:size=%u max_size=%u alignment=%u\n",
+			 p->mem_alloc.size, p->mem_alloc.max_size,
+			 p->mem_alloc.log2_alignment);
+		goto err;
+	}
+
+	switch (p->mem_alloc.region) {
+	case MVE_MEM_REGION_PROTECTED:
+		region = MALI_VPU_FW_REGION_PROTECTED;
+		break;
+	case MVE_MEM_REGION_OUTBUF:
+		region = MALI_VPU_FW_REGION_FRAMEBUF;
+		break;
+	default:
+		dev_err(fw->dev,
+			"unsupported RPC mem alloc region. region=%u\n",
+			p->mem_alloc.region);
+		goto err;
+	}
+
+	ret = mali_vpu_fw_get_region(region, &begin, &end);
+	if (ret)
+		goto err;
+
+	npages = DIV_ROUND_UP(p->mem_alloc.size, MALI_VPU_PAGE_SIZE);
+	max_pages = DIV_ROUND_UP(p->mem_alloc.max_size, MALI_VPU_PAGE_SIZE);
+
+	pages = mali_vpu_mmu_alloc_pages(fw->dev, npages, max_pages);
+	if (IS_ERR(pages)) {
+		begin = 0;
+		goto err;
+	}
+
+	ret = -EINVAL;
+	while (begin < end) {
+		ret = mali_vpu_mmu_map_pages(fw->mmu, begin, pages,
+					     MALI_VPU_ATTR_SHARED_RW,
+					     MALI_VPU_ACCESS_READ_WRITE);
+		if (!ret)
+			break;
+
+		begin += MALI_VPU_RPC_VA_GRANULE;
+	}
+
+	if (ret) {
+		dev_err(fw->dev,
+			"failed to find memory region for RPC alloc.\n");
+		mali_vpu_mmu_free_pages(pages);
+		begin = 0;
+		goto err;
+	}
+
+	hash_add(fw->rpc_mem, &pages->node, pages->va);
+	begin = pages->va;
+
+	dev_dbg(fw->dev,
+		"RPC alloc memory. size=%u, max_size=%u, region=%u, npages=%zu, begin=0x%x\n",
+		p->mem_alloc.size, p->mem_alloc.max_size, p->mem_alloc.region, npages, begin);
+
+err:
+	rpc_area->size = sizeof(u32);
+	p->data[0] = begin;
+}
+
+static void mali_vpu_rpc_mem_free(struct mali_vpu_fw *fw,
+				  struct mve_rpc_communication_area *rpc_area)
+{
+	union mve_rpc_params *p = &rpc_area->params;
+	struct mali_vpu_mmu_pages *pages;
+
+	pages = mali_vpu_find_pages(fw, p->mem_free.ve_pointer);
+	if (pages) {
+		hash_del(&pages->node);
+		mali_vpu_mmu_free_pages(pages);
+	} else {
+		dev_warn(fw->dev,
+			 "could not find pages for RPC free. va=0x%x\n",
+			 p->mem_free.ve_pointer);
+	}
+
+	dev_dbg(fw->dev, "RPC free memory. va=0x%x\n", p->mem_free.ve_pointer);
+
+	rpc_area->size = 0;
+}
+
+int mali_vpu_fw_handle_rpc(struct mali_vpu_fw *fw, bool *flush_mmu)
+{
+	struct mve_rpc_communication_area *rpc_area = fw->rpc.cpu;
+	int ret = 0;
+
+	if (flush_mmu)
+		*flush_mmu = false;
+
+	dma_sync_single_for_cpu(fw->dev, fw->rpc.dma, sizeof(*rpc_area),
+				DMA_FROM_DEVICE);
+
+	if (READ_ONCE(rpc_area->state) == MVE_RPC_STATE_PARAM) {
+		/*
+		 * Firmware publishes param only after writing the RPC request.
+		 */
+		dma_rmb();
+
+		ret = 1;
+
+		mali_vpu_fw_dbg_rpc(fw, "fw->host", rpc_area);
+
+		switch (rpc_area->call_id) {
+		case MVE_RPC_FUNCTION_DEBUG_PRINTF: {
+			if (!rpc_area->size ||
+			    rpc_area->size > sizeof(rpc_area->params.debug_print.string)) {
+				ret = -EPROTO;
+				rpc_area->size = 0;
+				break;
+			}
+
+			const char *s = rpc_area->params.debug_print.string;
+			size_t len = strnlen(s, rpc_area->size);
+
+			/* Strip trailing characters */
+			while (len && (s[len - 1] == '\n' || s[len - 1] == '\r'))
+				len--;
+
+			dev_info(fw->dev,
+				 "firmware DEBUG Print: %.*s\n", (int)len, s);
+			break;
+		}
+		case MVE_RPC_FUNCTION_MEM_ALLOC: {
+			if (rpc_area->size != sizeof(rpc_area->params.mem_alloc)) {
+				ret = -EPROTO;
+				break;
+			}
+
+			mali_vpu_rpc_mem_alloc(fw, rpc_area);
+			break;
+		}
+		case MVE_RPC_FUNCTION_MEM_RESIZE: {
+			if (rpc_area->size != sizeof(rpc_area->params.mem_resize)) {
+				ret = -EPROTO;
+				break;
+			}
+
+			mali_vpu_rpc_mem_resize(fw, rpc_area);
+			if (flush_mmu)
+				*flush_mmu = true;
+			break;
+		}
+		case MVE_RPC_FUNCTION_MEM_FREE: {
+			if (rpc_area->size != sizeof(rpc_area->params.mem_free)) {
+				ret = -EPROTO;
+				break;
+			}
+
+			mali_vpu_rpc_mem_free(fw, rpc_area);
+			if (flush_mmu)
+				*flush_mmu = true;
+			break;
+		}
+		default:
+			dev_warn_ratelimited(fw->dev,
+					     "unsupported RPC request. call_id=%u\n",
+					     rpc_area->call_id);
+			ret = -EOPNOTSUPP;
+			break;
+		}
+
+		/*
+		 * Make sure the whole RPC message body has been written before
+		 * the RPC message area is returned to the firmware.
+		 */
+		dma_wmb();
+		WRITE_ONCE(rpc_area->state, MVE_RPC_STATE_RETURN);
+
+		dma_sync_single_for_device(fw->dev, fw->rpc.dma,
+					   sizeof(*rpc_area), DMA_TO_DEVICE);
+
+		/* Log RPC response. */
+		mali_vpu_fw_dbg_rpc(fw, "host->fw", rpc_area);
+	}
+
+	return ret;
+}
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.h +12 -0
index e5dd67ce3397a..c03a07d994873 100644
@@ -6,9 +6,11 @@
 #ifndef __MALI_VPU_FIRMWARE_H__
 #define __MALI_VPU_FIRMWARE_H__
 
+#include <linux/hashtable.h>
 #include <linux/media/arm/mali-vpu-config.h>
 #include <linux/types.h>
 
+#define MALI_VPU_FW_HTABLE_BITS 3
 #define MALI_VPU_FW_TEXT_BASE_ADDR 0x1000u
 
 struct device;
@@ -156,6 +158,8 @@ struct mali_vpu_fw_msg {
  * @MALI_VPU_FW_REGION_CORE_5: Firmware instance region for core 5.
  * @MALI_VPU_FW_REGION_CORE_6: Firmware instance region for core 6.
  * @MALI_VPU_FW_REGION_CORE_7: Firmware instance region for core 7.
+ * @MALI_VPU_FW_REGION_PROTECTED: Protected firmware memory region.
+ * @MALI_VPU_FW_REGION_FRAMEBUF: Frame-buffer memory region.
  */
 enum mali_vpu_fw_region {
 	MALI_VPU_FW_REGION_MSG_HOST,
@@ -175,6 +179,9 @@ enum mali_vpu_fw_region {
 	MALI_VPU_FW_REGION_CORE_5,
 	MALI_VPU_FW_REGION_CORE_6,
 	MALI_VPU_FW_REGION_CORE_7,
+
+	MALI_VPU_FW_REGION_PROTECTED,
+	MALI_VPU_FW_REGION_FRAMEBUF,
 };
 
 /**
@@ -207,6 +214,8 @@ struct mali_vpu_fw_proto_page {
  * @shared_bss_pages: Shared BSS pages mapped into every core instance.
  * @ncores: Number of firmware core-instance regions constructed.
  * @pending_response_count: Number of requests awaiting a firmware response.
+ * @rpc_mem: Keeps track of RPC allocated memory. Maps MVE virtual address to
+ *		'struct mali_vpu_mmu_pages' object.
  */
 struct mali_vpu_fw {
 	struct device *dev;
@@ -225,6 +234,8 @@ struct mali_vpu_fw {
 	struct mali_vpu_mmu_pages *shared_bss_pages;
 	u32 ncores;
 	u32 pending_response_count;
+
+	DECLARE_HASHTABLE(rpc_mem, MALI_VPU_FW_HTABLE_BITS);
 };
 
 int mali_vpu_fw_construct(struct mali_vpu_fw *fw, const struct mali_vpu_fw_bin *fw_bin,
@@ -235,5 +246,6 @@ int mali_vpu_fw_send_ping(struct mali_vpu_fw *fw);
 int mali_vpu_fw_send_state_change(struct mali_vpu_fw *fw, enum mali_vpu_fw_state state);
 int mali_vpu_fw_send_set_option(struct mali_vpu_fw *fw, struct mali_vpu_fw_set_option *option);
 int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg);
+int mali_vpu_fw_handle_rpc(struct mali_vpu_fw *fw, bool *flush_mmu);
 
 #endif /* __MALI_VPU_FIRMWARE_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h +53 -0

65 lines not on this page · raw message

Signature · 1 line
2.43.0
#40 lore raw

Re: [RFC PATCH 15/28] media: mali-vpu: handle firmware RPC requests

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] media: mali-vpu: out-of-bounds VA mapping in mali_vpu_rpc_mem_alloc() - [Medium] media: mali-vpu…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] media: mali-vpu: out-of-bounds VA mapping in mali_vpu_rpc_mem_alloc()
- [Medium] media: mali-vpu: stale RPC return payload in mali_vpu_fw_handle_rpc()
- [Low] media: mali-vpu: log2_alignment ignored in mali_vpu_rpc_mem_alloc()
--

commit 6596ddd7663ec38dc1e5a77c8bd517be369bf9ab
Author: Charvi Mehta <[email protected]>

media: mali-vpu: handle firmware RPC requests

This commit adds handling for firmware RPC requests covering debug print output and firmware memory allocation, resizing, and freeing, tracking allocated pages by virtual address in a hash table.

Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +0 -0
index 91a989c098cbc..c11d5c6c0ec5f 100644

[ ... ]

Quoted 1 line
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +0 -0
@@ -908,3 +929,259 @@ int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg)

[ ... ]

Quoted 8 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +8 -0
+static void mali_vpu_rpc_mem_alloc(struct mali_vpu_fw *fw,
+				   struct mve_rpc_communication_area *rpc_area)
+{
+	union mve_rpc_params *p = &rpc_area->params;
+	enum mali_vpu_fw_region region;
+	struct mali_vpu_mmu_pages *pages;
+	size_t npages, max_pages;
+	mali_vpu_mmu_va end, begin = 0;

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg) · line 1011

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +10 -0
+	int ret;
+
+	if (!p->mem_alloc.size ||
+	    p->mem_alloc.size > p->mem_alloc.max_size) {
+		dev_warn(fw->dev,
+			 "invalid RPC allocation:size=%u max_size=%u alignment=%u\n",
+			 p->mem_alloc.size, p->mem_alloc.max_size,
+			 p->mem_alloc.log2_alignment);
+		goto err;
+	}

[Severity: Low]
Should mali_vpu_rpc_mem_alloc() validate or apply
p->mem_alloc.log2_alignment?

union mve_rpc_params documents that the allocation must be placed on at least a 2^(log2_alignment) boundary, and the warning above logs log2_alignment, but the placement loop always steps candidate addresses by MALI_VPU_RPC_VA_GRANULE (SZ_16M) without rejecting or honouring larger alignments.

Quoted 28 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +28 -0
+
+	switch (p->mem_alloc.region) {
+	case MVE_MEM_REGION_PROTECTED:
+		region = MALI_VPU_FW_REGION_PROTECTED;
+		break;
+	case MVE_MEM_REGION_OUTBUF:
+		region = MALI_VPU_FW_REGION_FRAMEBUF;
+		break;
+	default:
+		dev_err(fw->dev,
+			"unsupported RPC mem alloc region. region=%u\n",
+			p->mem_alloc.region);
+		goto err;
+	}
+
+	ret = mali_vpu_fw_get_region(region, &begin, &end);
+	if (ret)
+		goto err;
+
+	npages = DIV_ROUND_UP(p->mem_alloc.size, MALI_VPU_PAGE_SIZE);
+	max_pages = DIV_ROUND_UP(p->mem_alloc.max_size, MALI_VPU_PAGE_SIZE);
+
+	pages = mali_vpu_mmu_alloc_pages(fw->dev, npages, max_pages);
+	if (IS_ERR(pages)) {
+		begin = 0;
+		goto err;
+	}
+

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg) · line 1049

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +10 -0
+	ret = -EINVAL;
+	while (begin < end) {
+		ret = mali_vpu_mmu_map_pages(fw->mmu, begin, pages,
+					     MALI_VPU_ATTR_SHARED_RW,
+					     MALI_VPU_ACCESS_READ_WRITE);
+		if (!ret)
+			break;
+
+		begin += MALI_VPU_RPC_VA_GRANULE;
+	}

[Severity: Medium]
Can this loop map pages past the end of the target region?

mali_vpu_mmu_map_pages() maps and reserves pages->capacity pages starting at begin, but the loop only checks begin < end without verifying that the full reserved range fits within end - begin. When earlier 16 MiB granules are occupied and begin advances near end (or when max_size exceeds the region size), the mapping spills past end into the adjacent address region.

In addition, p->mem_alloc.max_size and p->mem_resize.new_size in mali_vpu_rpc_mem_resize() are passed directly to DIV_ROUND_UP(..., MALI_VPU_PAGE_SIZE) without an upper bound check. On 32-bit kernels MALI_VPU_PAGE_SIZE is a 32-bit unsigned long, so values above 0xfffff000 wrap DIV_ROUND_UP() to 0 (causing mali_vpu_rpc_mem_resize() to shrink the allocation to 0 pages while returning pages->va as a success), and on 64-bit kernels an unbounded max_size can trigger a slab allocator warning in devm_kzalloc().

[ ... ]

Quoted 79 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +79 -0
+int mali_vpu_fw_handle_rpc(struct mali_vpu_fw *fw, bool *flush_mmu)
+{
+	struct mve_rpc_communication_area *rpc_area = fw->rpc.cpu;
+	int ret = 0;
+
+	if (flush_mmu)
+		*flush_mmu = false;
+
+	dma_sync_single_for_cpu(fw->dev, fw->rpc.dma, sizeof(*rpc_area),
+				DMA_FROM_DEVICE);
+
+	if (READ_ONCE(rpc_area->state) == MVE_RPC_STATE_PARAM) {
+		/*
+		 * Firmware publishes param only after writing the RPC request.
+		 */
+		dma_rmb();
+
+		ret = 1;
+
+		mali_vpu_fw_dbg_rpc(fw, "fw->host", rpc_area);
+
+		switch (rpc_area->call_id) {
+		case MVE_RPC_FUNCTION_DEBUG_PRINTF: {
+			if (!rpc_area->size ||
+			    rpc_area->size > sizeof(rpc_area->params.debug_print.string)) {
+				ret = -EPROTO;
+				rpc_area->size = 0;
+				break;
+			}
+
+			const char *s = rpc_area->params.debug_print.string;
+			size_t len = strnlen(s, rpc_area->size);
+
+			/* Strip trailing characters */
+			while (len && (s[len - 1] == '\n' || s[len - 1] == '\r'))
+				len--;
+
+			dev_info(fw->dev,
+				 "firmware DEBUG Print: %.*s\n", (int)len, s);
+			break;
+		}
+		case MVE_RPC_FUNCTION_MEM_ALLOC: {
+			if (rpc_area->size != sizeof(rpc_area->params.mem_alloc)) {
+				ret = -EPROTO;
+				break;
+			}
+
+			mali_vpu_rpc_mem_alloc(fw, rpc_area);
+			break;
+		}
+		case MVE_RPC_FUNCTION_MEM_RESIZE: {
+			if (rpc_area->size != sizeof(rpc_area->params.mem_resize)) {
+				ret = -EPROTO;
+				break;
+			}
+
+			mali_vpu_rpc_mem_resize(fw, rpc_area);
+			if (flush_mmu)
+				*flush_mmu = true;
+			break;
+		}
+		case MVE_RPC_FUNCTION_MEM_FREE: {
+			if (rpc_area->size != sizeof(rpc_area->params.mem_free)) {
+				ret = -EPROTO;
+				break;
+			}
+
+			mali_vpu_rpc_mem_free(fw, rpc_area);
+			if (flush_mmu)
+				*flush_mmu = true;
+			break;
+		}
+		default:
+			dev_warn_ratelimited(fw->dev,
+					     "unsupported RPC request. call_id=%u\n",
+					     rpc_area->call_id);
+			ret = -EOPNOTSUPP;
+			break;
+		}

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg) · line 1180

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +10 -0
+
+		/*
+		 * Make sure the whole RPC message body has been written before
+		 * the RPC message area is returned to the firmware.
+		 */
+		dma_wmb();
+		WRITE_ONCE(rpc_area->state, MVE_RPC_STATE_RETURN);
+
+		dma_sync_single_for_device(fw->dev, fw->rpc.dma,
+					   sizeof(*rpc_area), DMA_TO_DEVICE);

[Severity: Medium]
Does returning MVE_RPC_STATE_RETURN after a size mismatch leave stale request words in the RPC response payload?

union mve_rpc_params aliases data[0] with mem_alloc.size and mem_resize.ve_pointer. When MVE_RPC_FUNCTION_MEM_ALLOC or MVE_RPC_FUNCTION_MEM_RESIZE breaks out with -EPROTO, rpc_area->state is still set to MVE_RPC_STATE_RETURN and synced back to the device with rpc_area->params.data[0] holding the non-zero input size or old ve_pointer, which the firmware ABI interprets as a valid allocated virtual address rather than 0 for failure.

Also, MVE_RPC_FUNCTION_DEBUG_PRINTF sets rpc_area->size = 0 on its error path, but leaves rpc_area->size set to the input string length on normal completion, and the other error paths leave rpc_area->size untouched.

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

RFC PATCH 16/28 media: mali-vpu: add LSID hardware support

In reply to Charvi Mehta

Add low-level support of managing the VPU local session IDs (LSIDs). Define the per-LSID and job queue registers and provide helpers to map and unmap sessions, manage the job queue, signal firmware, flush the session MMU and terminate execution.

These operations provide hardware interface for the scheduler added in a subsequent patch.

---
 drivers/media/platform/arm/mali-vpu/Makefile  |   3 +-
 .../platform/arm/mali-vpu/mali-vpu-lsid.c     | 271 ++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-lsid.h     |  55 ++++
 .../arm/mali-vpu/mali-vpu-registers.h         |  44 ++-
 4 files changed, 370 insertions(+), 3 deletions(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-lsid.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-lsid.h
drivers/media/platform/arm/mali-vpu/Makefile +2 -1
index 168b4bc43d54d..51d49d815f1fb 100644
@@ -11,6 +11,7 @@ mali-vpu-objs := mali-vpu-probe.o \
 		mali-vpu-controls.o \
 		mali-vpu-mmu.o \
 		mali-vpu-firmware-cache.o \
-		mali-vpu-firmware.o
+		mali-vpu-firmware.o \
+		mali-vpu-lsid.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-lsid.c new file +271 -0

271 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-lsid.h new file +55 -0

55 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-registers.h +42 -2

59 lines not on this page · raw message

Signature · 1 line
2.43.0
#42 lore raw

RFC PATCH 17/28 media: mali-vpu: add single session scheduler

In reply to Charvi Mehta

Add a scheduler with a simple policy that assigns all available VPU cores to a single session mapped at LSID 0. Provide operations for switching the session in, signalling firmware, flushing its MMU context and terminating execution.

Initialize the scheduler during probe and prepare the session hardware context when its firmware is loaded.

---
 drivers/media/platform/arm/mali-vpu/Makefile  |   3 +-
 .../platform/arm/mali-vpu/mali-vpu-device.h   |   4 +
 .../platform/arm/mali-vpu/mali-vpu-lsid.h     |   3 +
 .../platform/arm/mali-vpu/mali-vpu-mmu.c      |   6 +
 .../platform/arm/mali-vpu/mali-vpu-mmu.h      |   2 +
 .../platform/arm/mali-vpu/mali-vpu-probe.c    |  11 +-
 .../arm/mali-vpu/mali-vpu-registers.h         |   9 +
 .../arm/mali-vpu/mali-vpu-scheduler.c         | 224 ++++++++++++++++++
 .../arm/mali-vpu/mali-vpu-scheduler.h         |  57 +++++
 .../platform/arm/mali-vpu/mali-vpu-session.c  |  14 +-
 .../platform/arm/mali-vpu/mali-vpu-session.h  |   6 +
 .../arm/mali-vpu/mali-vpu-variant-v76.c       |   2 +
 .../platform/arm/mali-vpu/mali-vpu-variant.h  |   2 +
 13 files changed, 334 insertions(+), 9 deletions(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-scheduler.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-scheduler.h
drivers/media/platform/arm/mali-vpu/Makefile +2 -1
index 51d49d815f1fb..5860cda6055cc 100644
@@ -12,6 +12,7 @@ mali-vpu-objs := mali-vpu-probe.o \
 		mali-vpu-mmu.o \
 		mali-vpu-firmware-cache.o \
 		mali-vpu-firmware.o \
-		mali-vpu-lsid.o
+		mali-vpu-lsid.o \
+		mali-vpu-scheduler.o
 
 obj-$(CONFIG_VIDEO_MALI_VPU) += mali-vpu.o
drivers/media/platform/arm/mali-vpu/mali-vpu-device.h +4 -0
index a35c1b901aa3f..9be36e47d0879 100644
@@ -9,6 +9,8 @@
 #include <linux/device.h>
 #include <media/v4l2-device.h>
 
+#include "mali-vpu-scheduler.h"
+
 #define MALI_VPU_DRV_NAME "mali-vpu"
 
 struct clk_bulk_data;
@@ -40,6 +42,7 @@ enum mali_vpu_session_role {
  * @vdev_dec: Video device structure for decoder.
  * @m2m_dev: V4L2 mem2mem device structure for mali vpu.
  * @fw_cache: Firmware cache for sharing or reusing firmware blobs.
+ * @sched: Mali VPU hardware scheduler state.
  */
 struct mali_vpu_dev {
 	struct device			*dev;
@@ -56,6 +59,7 @@ struct mali_vpu_dev {
 	struct video_device		*vdev_dec;
 	struct v4l2_m2m_dev		*m2m_dev;
 	struct mali_vpu_fw_cache	*fw_cache;
+	struct mali_vpu_sched		sched;
 };
 
 #endif /* __MALI_VPU_DEVICE_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-lsid.h +3 -0
index 97c32f9ac9aa0..57733304d4e17 100644
@@ -9,6 +9,7 @@
 #include <linux/types.h>
 
 struct mali_vpu_dev;
+struct mali_vpu_sched_session;
 
 /*
  * A local session identifier (LSID) represents a hardware execution
@@ -36,10 +37,12 @@ struct mali_vpu_lsid_pcb {
 /**
  * struct mali_vpu_lsid - Hardware LSID instance.
  * @vpu_dev: Back pointer to parent Mali VPU device.
+ * @sched_sess: Scheduler session currently mapped to this LSID, if any.
  * @id: Hardware LSID instance.
  */
 struct mali_vpu_lsid {
 	struct mali_vpu_dev *vpu_dev;
+	struct mali_vpu_sched_session *sched_sess;
 	u32 id;
 };
 
drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c +6 -0
index 00cf5251eff5e..115539fe1cb3e 100644
@@ -691,3 +691,9 @@ int mali_vpu_mmu_write(struct mali_vpu_mmu_pages *pages,
 
 	return 0;
 }
+
+mali_vpu_mmu_pte mali_vpu_mmu_get_root_pte(const struct mali_vpu_mmu *mmu)
+{
+	return mali_vpu_mmu_set_pte(MALI_VPU_ATTR_PRIVATE, mmu->root.dma,
+				    MALI_VPU_ACCESS_READ_WRITE);
+}
drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.h +2 -0
index 6ad645a2dc6bb..4baf1daaf2983 100644
@@ -149,4 +149,6 @@ int mali_vpu_mmu_map_dma(struct mali_vpu_mmu *mmu,
 void mali_vpu_mmu_unmap_va(struct mali_vpu_mmu *mmu, mali_vpu_mmu_va va, size_t size);
 int mali_vpu_mmu_write(struct mali_vpu_mmu_pages *pages, const void *data, size_t size);
 
+mali_vpu_mmu_pte mali_vpu_mmu_get_root_pte(const struct mali_vpu_mmu *mmu);
+
 #endif /* __MALI_VPU_MMU_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c +9 -2
index 123ff4d8fc937..0c7ea9035fb50 100644
@@ -208,17 +208,21 @@ static int mali_vpu_probe(struct platform_device *pdev)
 		goto err_rst_clk;
 	}
 
+	ret = mali_vpu_sched_init(&vpu_dev->sched, vpu_dev);
+	if (ret)
+		goto err_rst_clk;
+
 	vpu_dev->dma_mask = vpu_dev->variant->dma_mask;
 	ret = dma_set_mask_and_coherent(vpu_dev->dev, vpu_dev->dma_mask);
 	if (ret)
-		goto err_rst_clk;
+		goto err_cleanup_sched;
 
 	dma_set_max_seg_size(vpu_dev->dev, SZ_2G);
 
 	ret = v4l2_device_register(&pdev->dev, &vpu_dev->v4l2_dev);
 	if (ret) {
 		dev_err(vpu_dev->dev, "failed to register V4L2 device\n");
-		goto err_rst_clk;
+		goto err_cleanup_sched;
 	}
 
 	vpu_dev->m2m_dev = v4l2_m2m_init(&mali_vpu_m2m_ops);
@@ -251,6 +255,8 @@ static int mali_vpu_probe(struct platform_device *pdev)
 	v4l2_m2m_release(vpu_dev->m2m_dev);
 err_unreg_v4l2:
 	v4l2_device_unregister(&vpu_dev->v4l2_dev);
+err_cleanup_sched:
+	mali_vpu_sched_cleanup(&vpu_dev->sched);
 err_rst_clk:
 	mali_vpu_assert_reset(vpu_dev);
 err_clk:
@@ -268,6 +274,7 @@ static void mali_vpu_remove(struct platform_device *pdev)
 	v4l2_m2m_release(vpu_dev->m2m_dev);
 	v4l2_device_unregister(&vpu_dev->v4l2_dev);
 
+	mali_vpu_sched_cleanup(&vpu_dev->sched);
 	mali_vpu_assert_reset(vpu_dev);
 	mali_vpu_disable_clk(vpu_dev);
 
drivers/media/platform/arm/mali-vpu/mali-vpu-registers.h +9 -0
index f2386bdc4943a..1a3e0fcdba4c6 100644
@@ -17,7 +17,10 @@
 #define MALI_VPU_REG_VERSION  0x00000
 #define MALI_VPU_REG_ENABLE   0x00004
 #define MALI_VPU_REG_NCORES   0x00008
+#define MALI_VPU_REG_NLSID    0x0000C
 #define MALI_VPU_REG_JOBQUEUE 0x00014
+#define MALI_VPU_REG_CLKFORCE 0x00024
+#define MALI_VPU_REG_RESET    0x00050
 
 #define MALI_VPU_LSID_BASE    0x00200
 #define MALI_VPU_LSID_STRIDE  0x40
@@ -54,6 +57,12 @@
 #define MALI_VPU_JOBQUEUE_JOB_INVALID 0xf
 #define MALI_VPU_JOBQUEUE_NJOBS       4
 
+/* IRQVE contains one interrupt bit per LSID. */
+#define MALI_VPU_IRQVE_NUM_BITS 32
+
+/* CTRL register fields */
+#define MALI_VPU_CTRL_MAXCORES GENMASK(11, 8)
+
 #define MALI_VPU_ALLOC_FREE          0
 #define MALI_VPU_ALLOC_NON_PROTECTED 1
 #define MALI_VPU_ALLOC_PROTECTED     2
drivers/media/platform/arm/mali-vpu/mali-vpu-scheduler.c new file +224 -0

224 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-scheduler.h new file +57 -0

57 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +8 -6
index 36197e82461be..c5579edfe627f 100644
@@ -16,7 +16,6 @@
 #include "mali-vpu-firmware.h"
 #include "mali-vpu-firmware-cache.h"
 #include "mali-vpu-formats.h"
-#include "mali-vpu-registers.h"
 #include "mali-vpu-variant.h"
 #include "mali-vpu-vb2.h"
 
@@ -216,7 +215,6 @@ static void mali_vpu_session_fw_bin_ready(struct mali_vpu_fw_bin *vpu_fw_bin,
 					  bool same_thread)
 {
 	struct mali_vpu_session *sess = arg;
-	u32 ncores;
 	int ret;
 
 	if (!same_thread)
@@ -239,13 +237,17 @@ static void mali_vpu_session_fw_bin_ready(struct mali_vpu_fw_bin *vpu_fw_bin,
 	sess->fw_bin = vpu_fw_bin;
 
 	/*
-	 * Get total number of cores on VPU element.
-	 * This will be scheduler decision in subsequent patches.
+	 * Prepare the scheduler's saved session hardware context.
+	 * Scheduler assigns the session's core count.
 	 */
-	ncores = mali_vpu_read_reg(sess->vpu_dev->regs_base, MALI_VPU_REG_NCORES);
+	ret = mali_vpu_sched_session_prepare(&sess->sched_sess, sess);
+	if (ret) {
+		sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_UNINITIALIZED;
+		goto err_cache_put;
+	}
 
 	ret = mali_vpu_fw_construct(&sess->fw, sess->fw_bin, sess->vpu_dev->dev,
-				    &sess->mmu, ncores);
+				    &sess->mmu, sess->ncores);
 	if (ret) {
 		dev_err(sess->vpu_dev->dev,
 			"failed to initialise firmware: %d\n", ret);
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +6 -0
index f0aa98005e92a..bf5923494773b 100644
@@ -20,6 +20,7 @@
 struct v4l2_m2m_ctx;
 struct mali_vpu_fw_bin;
 struct mali_vpu_fw_event;
+struct mali_vpu_sched_session;
 
 /**
  * struct mali_vpu_port - format state of a src/dst queue in a session.
@@ -77,6 +78,8 @@ enum mali_vpu_session_fw_bin_state {
  * @fw_event: Firmware binary load event callback structure for this session.
  * @fw_bin_state: Session firmware binary status.
  * @fw_load_done: Completion used to synchronize teardown with an in-flight firmware load.
+ * @sched_sess: Scheduler state used to map this session to an LSID.
+ * @ncores: Number of cores assigned to this session by scheduler.
  */
 struct mali_vpu_session {
 	struct mali_vpu_dev		*vpu_dev;
@@ -100,6 +103,9 @@ struct mali_vpu_session {
 	struct mali_vpu_fw_event		*fw_event;
 	enum mali_vpu_session_fw_bin_state	fw_bin_state;
 	struct completion			fw_load_done;
+
+	struct mali_vpu_sched_session	sched_sess;
+	u32				ncores;
 };
 
 int mali_vpu_session_init(struct mali_vpu_session *sess);
drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c +2 -0
index 0a7481d97c717..30ff5c25417e0 100644
@@ -18,6 +18,7 @@
 
 #define MALI_VPU_HW_PID_V76 0x56627
 #define MALI_VPU_FW_VER_MAJOR_V76 1
+#define MALI_VPU_MAX_LSID_V76 4
 
 #define MALI_V76_MAX_FRAME_WIDTH 8192
 #define MALI_V76_MAX_FRAME_HEIGHT 8192
@@ -153,6 +154,7 @@ const struct mali_vpu_variant mali_vpu_v76_variant = {
 	.dma_mask = DMA_BIT_MASK(40),
 	.expected_hw_pid = MALI_VPU_HW_PID_V76,
 	.fw_ver_major = MALI_VPU_FW_VER_MAJOR_V76,
+	.max_lsid = MALI_VPU_MAX_LSID_V76,
 
 	.rst_names = mali_v76_rst_names,
 	.num_rsts = ARRAY_SIZE(mali_v76_rst_names),
drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h +2 -0
index 3c37cd70bd81f..ac0bc3d899877 100644
@@ -51,6 +51,7 @@ struct mali_vpu_control_caps {
  * @expected_hw_pid: Expected hardware product ID read from the version register.
  * @dma_mask: DMA addressing capability mask.
  * @fw_ver_major: Major firmware version compatible with this variant.
+ * @max_lsid: Maximum number of hardware LSID slots in a VPU element.
  * @fmt_caps: Format capability tables for encode/decode paths.
  * @frmsz_caps: Frame dimension limits and defaults.
  * @ctrl_caps:  Control capability tables for encode/decode paths.
@@ -61,6 +62,7 @@ struct mali_vpu_variant {
 	u32 expected_hw_pid;
 	u64 dma_mask;
 	u32 fw_ver_major;
+	u32 max_lsid;
 
 	struct mali_vpu_format_caps	fmt_caps;
 	struct mali_vpu_framesize_caps	frmsz_caps;
Signature · 1 line
2.43.0
#43 lore raw

RFC PATCH 18/28 media: mali-vpu: handle firmware interrupts

In reply to Charvi Mehta

Firmware responses and service requests are delivered through per-LSID interrupts. Route these interrupts to the session currently assigned to the LSID and process them outside hard IRQ context.

This allows the driver to service firmware RPC requests and consume responses needed to complete asynchronous firmware commands.

---
 .../platform/arm/mali-vpu/mali-vpu-device.h   |  7 ++
 .../platform/arm/mali-vpu/mali-vpu-probe.c    | 84 +++++++++++++++++-
 .../arm/mali-vpu/mali-vpu-registers.h         |  1 +
 .../arm/mali-vpu/mali-vpu-scheduler.c         | 24 ++++++
 .../arm/mali-vpu/mali-vpu-scheduler.h         |  1 +
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 86 +++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-session.h  |  3 +
 7 files changed, 205 insertions(+), 1 deletion(-)
drivers/media/platform/arm/mali-vpu/mali-vpu-device.h +7 -0
index 9be36e47d0879..cacdd86fe517e 100644
@@ -7,6 +7,7 @@
 #define __MALI_VPU_DEVICE_H__
 
 #include <linux/device.h>
+#include <linux/workqueue.h>
 #include <media/v4l2-device.h>
 
 #include "mali-vpu-scheduler.h"
@@ -37,6 +38,9 @@ enum mali_vpu_session_role {
  * @rst_count: Number of reset controls in @rst_list.
  * @dma_mask: DMA mask for the device, indicating supported address width.
  * @irq: IRQ number for VPU.
+ * @irqve_mask: Bitmap of pending LSID IRQs.
+ * @irq_wq: Workqueue used to process LSID IRQs outside hard IRQ context.
+ * @irq_work: Work item for deferred LSID IRQ handling.
  * @variant: Structure describing variant-specific features and capabilities.
  * @v4l2_dev: V4L2 device structure for mali vpu.
  * @vdev_dec: Video device structure for decoder.
@@ -53,6 +57,9 @@ struct mali_vpu_dev {
 	unsigned int			rst_count;
 	u64				dma_mask;
 	int				irq;
+	unsigned long			irqve_mask;
+	struct workqueue_struct		*irq_wq;
+	struct work_struct		irq_work;
 
 	const struct mali_vpu_variant	*variant;
 	struct v4l2_device		v4l2_dev;
drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c +83 -1

117 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-registers.h +1 -0
index 1a3e0fcdba4c6..467f2cdb4c278 100644
@@ -19,6 +19,7 @@
 #define MALI_VPU_REG_NCORES   0x00008
 #define MALI_VPU_REG_NLSID    0x0000C
 #define MALI_VPU_REG_JOBQUEUE 0x00014
+#define MALI_VPU_REG_IRQVE    0x00018
 #define MALI_VPU_REG_CLKFORCE 0x00024
 #define MALI_VPU_REG_RESET    0x00050
 
drivers/media/platform/arm/mali-vpu/mali-vpu-scheduler.c +24 -0
index cae9ac0093be9..77503f20e4b29 100644
@@ -222,3 +222,27 @@ int mali_vpu_sched_session_terminate(struct mali_vpu_sched_session *sched_sess,
 
 	return ret;
 }
+
+void mali_vpu_sched_handle_lsid_irq(struct mali_vpu_sched *sched, u32 lsid)
+{
+	struct mali_vpu_session *sess = NULL;
+	struct mali_vpu_sched_session *sched_sess;
+
+	/* Only single active session policy - always mapped to lsid 0 */
+	if (lsid != 0) {
+		dev_warn(sched->vpu_dev->dev,
+			 "unexpected interrupt from LSID %u\n", lsid);
+		return;
+	}
+
+	mutex_lock(&sched->lock);
+	if (sched->nlsid) {
+		sched_sess = sched->lsid_list[lsid].sched_sess;
+		if (sched_sess)
+			sess = container_of(sched_sess, struct mali_vpu_session, sched_sess);
+	}
+	mutex_unlock(&sched->lock);
+
+	if (sess)
+		mali_vpu_session_irq(sess);
+}
drivers/media/platform/arm/mali-vpu/mali-vpu-scheduler.h +1 -0
index 23167aef6ff62..ffa93495e8b47 100644
@@ -53,5 +53,6 @@ void mali_vpu_sched_flush_mmu(struct mali_vpu_sched_session *sched_sess,
 			      struct mali_vpu_sched *sched);
 int mali_vpu_sched_session_terminate(struct mali_vpu_sched_session *sched_sess,
 				     struct mali_vpu_sched *sched);
+void mali_vpu_sched_handle_lsid_irq(struct mali_vpu_sched *sched, u32 lsid);
 
 #endif /* __MALI_VPU_SCHEDULER_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +86 -0

101 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +3 -0
index bf5923494773b..e6b9de3b272cf 100644
@@ -78,6 +78,7 @@ enum mali_vpu_session_fw_bin_state {
  * @fw_event: Firmware binary load event callback structure for this session.
  * @fw_bin_state: Session firmware binary status.
  * @fw_load_done: Completion used to synchronize teardown with an in-flight firmware load.
+ * @fw_state: Last state confirmed by the firmware.
  * @sched_sess: Scheduler state used to map this session to an LSID.
  * @ncores: Number of cores assigned to this session by scheduler.
  */
@@ -103,6 +104,7 @@ struct mali_vpu_session {
 	struct mali_vpu_fw_event		*fw_event;
 	enum mali_vpu_session_fw_bin_state	fw_bin_state;
 	struct completion			fw_load_done;
+	enum mali_vpu_fw_state			fw_state;
 
 	struct mali_vpu_sched_session	sched_sess;
 	u32				ncores;
@@ -113,6 +115,7 @@ int mali_vpu_session_cleanup(struct mali_vpu_session *sess);
 int mali_vpu_session_request_fw_load(struct mali_vpu_session *sess);
 int mali_vpu_session_fw_send_set_option(struct mali_vpu_session *sess,
 					struct mali_vpu_fw_set_option *opt);
+void mali_vpu_session_irq(struct mali_vpu_session *sess);
 
 static inline struct mali_vpu_session *mali_vpu_get_session(struct file *filp)
 {
Signature · 1 line
2.43.0
#44 lore raw

RFC PATCH 19/28 media: mali-vpu: start decoder firmware through LSID scheduling

In reply to Charvi Mehta

Connect decoder firmware execution to queue preparation and asynchronous firmware loading. Start the decoder when OUTPUT requests streaming, without requiring CAPTURE to be streaming.

Activate the LSID and wait for pending option responses before requesting the firmware running state. Track requested and confirmed firmware states and coordinate shutdown with firmware responses and interrupt processing.

---
 .../platform/arm/mali-vpu/mali-vpu-formats.c  |   7 +
 .../platform/arm/mali-vpu/mali-vpu-formats.h  |   1 +
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 297 +++++++++++++++++-
 .../platform/arm/mali-vpu/mali-vpu-session.h  |  12 +
 .../platform/arm/mali-vpu/mali-vpu-vb2.c      |  46 ++-
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   |   2 +
 6 files changed, 356 insertions(+), 9 deletions(-)
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c +7 -0
index 57aa1b3baf40f..072e3b4aa9473 100644
@@ -20,6 +20,13 @@ bool mali_vpu_is_coded_q(const struct mali_vpu_session *sess, enum v4l2_buf_type
 	return V4L2_TYPE_IS_CAPTURE(type);
 }
 
+bool mali_vpu_is_raw_q(const struct mali_vpu_session *sess, enum v4l2_buf_type type)
+{
+	if (sess->role == MALI_VPU_DECODER)
+		return V4L2_TYPE_IS_CAPTURE(type);
+	return V4L2_TYPE_IS_OUTPUT(type);
+}
+
 const struct mali_vpu_format_desc *
 mali_vpu_find_format(struct mali_vpu_session *sess, u32 pixfmt,
 		     enum v4l2_buf_type type)
drivers/media/platform/arm/mali-vpu/mali-vpu-formats.h +1 -0
index 37198325ee481..6901e295d7c33 100644
@@ -34,5 +34,6 @@ int mali_vpu_fill_pixmp_fmt(const struct mali_vpu_format_desc *fmt,
 			    u32 width, u32 height,
 			    struct v4l2_pix_format_mplane *pix_mp);
 bool mali_vpu_is_coded_q(const struct mali_vpu_session *sess, enum v4l2_buf_type type);
+bool mali_vpu_is_raw_q(const struct mali_vpu_session *sess, enum v4l2_buf_type type);
 
 #endif /* __MALI_VPU_FORMATS_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +294 -3

364 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +12 -0
index e6b9de3b272cf..e331965d32fd8 100644
@@ -47,6 +47,11 @@ struct mali_vpu_port {
  * construct the firmware address space before making the binary READY. The
  * session retains its binary reference until cleanup.
  *
+ * A synchronous callback leaves firmware startup to session preparation.
+ * An asynchronous callback starts firmware if the required ports still request
+ * streaming: OUTPUT for a decoder, both ports for an encoder.
+ * Binary READY and firmware RUNNING are separate states.
+ *
  * Session cleanup waits for an in-flight callback before releasing the
  * session-owned binary reference and callback event. On timeout, it cancels
  * the pending cache waiter or waits for an already claimed callback.
@@ -78,7 +83,9 @@ enum mali_vpu_session_fw_bin_state {
  * @fw_event: Firmware binary load event callback structure for this session.
  * @fw_bin_state: Session firmware binary status.
  * @fw_load_done: Completion used to synchronize teardown with an in-flight firmware load.
+ * @fw_waitq: Wait queue used while waiting for firmware responses.
  * @fw_state: Last state confirmed by the firmware.
+ * @fw_requested_state: Last firmware state requested by the driver.
  * @sched_sess: Scheduler state used to map this session to an LSID.
  * @ncores: Number of cores assigned to this session by scheduler.
  */
@@ -104,7 +111,9 @@ struct mali_vpu_session {
 	struct mali_vpu_fw_event		*fw_event;
 	enum mali_vpu_session_fw_bin_state	fw_bin_state;
 	struct completion			fw_load_done;
+	wait_queue_head_t			fw_waitq;
 	enum mali_vpu_fw_state			fw_state;
+	enum mali_vpu_fw_state			fw_requested_state;
 
 	struct mali_vpu_sched_session	sched_sess;
 	u32				ncores;
@@ -115,6 +124,9 @@ int mali_vpu_session_cleanup(struct mali_vpu_session *sess);
 int mali_vpu_session_request_fw_load(struct mali_vpu_session *sess);
 int mali_vpu_session_fw_send_set_option(struct mali_vpu_session *sess,
 					struct mali_vpu_fw_set_option *opt);
+int mali_vpu_session_prepare_streaming(struct mali_vpu_session *sess,
+				       enum v4l2_buf_type type);
+int mali_vpu_session_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type type);
 void mali_vpu_session_irq(struct mali_vpu_session *sess);
 
 static inline struct mali_vpu_session *mali_vpu_get_session(struct file *filp)
drivers/media/platform/arm/mali-vpu/mali-vpu-vb2.c +40 -6

71 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +2 -0
index d9196cc86cdf4..0fcd3835e52d3 100644
@@ -357,4 +357,6 @@ const struct v4l2_ioctl_ops mali_vpu_ioctl_ops_dec = {
 	.vidioc_enum_framesizes		= mali_vpu_enum_framesizes,
 	.vidioc_subscribe_event		= mali_vpu_subscribe_event,
 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
+	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
+	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
 };
Signature · 1 line
2.43.0
#45 lore raw

Re: [RFC PATCH 19/28] media: mali-vpu: start decoder firmware through LSID scheduling

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] media: mali-vpu: vq_lock drop in mali_vpu_session_wait_pending() races - [Medium] media: mali-vpu…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] media: mali-vpu: vq_lock drop in mali_vpu_session_wait_pending() races
- [Medium] media: mali-vpu: 10s hang in mali_vpu_session_cleanup() on unmapped LSID
--

commit df41ad5c55cc78aaaf50be9e6b1f9cc272e547c5
Author: Charvi Mehta <[email protected]>

media: mali-vpu: start decoder firmware through LSID scheduling

This commit connects decoder firmware execution to VB2 queue preparation and asynchronous firmware loading, activating the LSID and tracking requested and confirmed firmware states across stream startup and shutdown.

Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +0 -0
index 09ec6d14f4ad0..6d22a5d479008 100644

[ ... ]

Quoted 69 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +65 -0
@@ -123,6 +211,102 @@ static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess,
 	}
 }
 
+static int mali_vpu_session_wait_pending(struct mali_vpu_session *sess)
+{
+	long timeout;
+
+	lockdep_assert_held(&sess->vq_lock);
+
+	while (sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_READY &&
+	       sess->fw.pending_response_count) {
+		mutex_unlock(&sess->vq_lock);
+
+		timeout = wait_event_timeout(sess->fw_waitq,
+					     READ_ONCE(sess->fw_bin_state) !=
+					     MALI_VPU_SESSION_FW_BIN_READY ||
+					     !READ_ONCE(sess->fw.pending_response_count),
+					     msecs_to_jiffies(mali_vpu_fw_response_timeout_ms));
+
+		mutex_lock(&sess->vq_lock);
+
+		if (!timeout && sess->fw.pending_response_count)
+			return -ETIMEDOUT;
+	}
+
+	return 0;
+}
+
+static int mali_vpu_session_fw_init(struct mali_vpu_session *sess)
+{
+	int ret;
+
+	lockdep_assert_held(&sess->vq_lock);
+
+	if (sess->fw_requested_state != sess->fw_state) {
+		if (sess->fw_requested_state != MALI_VPU_FW_STATE_RUNNING)
+			return -EBUSY;
+
+		return mali_vpu_session_sched_activate(sess);
+	}
+
+	if (sess->fw_state == MALI_VPU_FW_STATE_RUNNING)
+		return 0;
+
+	ret = mali_vpu_session_fw_send_job(sess, 0);
+	if (ret)
+		return ret;
+
+	/*
+	 * Map the LSID and allow the stopped firmware to process the
+	 * queued SET_OPTION requests before it can be RUNNING.
+	 */
+	ret = mali_vpu_session_sched_activate(sess);
+	if (ret)
+		return ret;
+
+	ret = mali_vpu_session_fw_send_ping(sess);
+	if (ret)
+		return ret;
+
+	ret = mali_vpu_apply_staged_controls(sess);
+	if (ret)
+		return ret;
+
+	/*
+	 * SET_OPTION is only valid while stopped. Wait for pending option
+	 * responses before requesting RUNNING.
+	 */

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess, · line 288

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +10 -0
+	ret = mali_vpu_session_wait_pending(sess);
+	if (ret)
+		return ret;
+
+	/* Waiting drops vq_lock, so streaming may have been withdrawn. */
+	if (!mali_vpu_session_can_stream(sess))
+		return 0;
+
+	return mali_vpu_session_fw_send_state(sess, MALI_VPU_FW_STATE_RUNNING);
+}

[Severity: High]
Can dropping sess->vq_lock in mali_vpu_session_wait_pending() race with concurrent queue ioctls during vb2_core_streamon()?

sess->vq_lock is also assigned to vq->lock and m2m_ctx->q_lock, which videobuf2 expects to stay held across prepare_streaming() and start_streaming() until q->streaming is set. When staged controls are applied, mali_vpu_session_prepare_streaming() sets port->stream_requested = true and then drops sess->vq_lock inside mali_vpu_session_wait_pending() while q->streaming is still 0:

CPU0 CPU1
vb2_core_streamon(q)
mali_vpu_vb2_prepare_streaming(q)

    port->stream_requested = true
    mali_vpu_session_fw_init()
      mali_vpu_apply_staged_controls()
      mali_vpu_session_wait_pending()
        mutex_unlock(&sess->vq_lock)
                                      vb2_core_streamon(q)
                                        /* q->streaming is still 0 */
                                        mali_vpu_vb2_prepare_streaming(q)
                                          if (port->stream_requested)
                                            return 0;
                                        vb2_start_streaming(q)
        mutex_lock(&sess->vq_lock)

vb2_start_streaming(q) <-- corrupts q_ctx->rdy_queue list

Because both threads run vb2_start_streaming(q), the queued buffers are passed to mali_vpu_vb2_buffer_queue() -> v4l2_m2m_buf_queue() twice, corrupting q_ctx->rdy_queue via double list_add_tail() and double incrementing q->owned_by_drv_count.

Dropping sess->vq_lock here exposes three other races while waiting:
- sess->fw_requested_state is not set to MALI_VPU_FW_STATE_RUNNING until mali_vpu_session_fw_send_state() after mali_vpu_session_wait_pending() returns, so a concurrent streamon on the other port (or against the asynchronous mali_vpu_session_fw_bin_ready() callback) sees fw_requested_state == fw_state == MALI_VPU_FW_STATE_STOPPED and re-enters mali_vpu_session_fw_init(), sending duplicate job and ping requests.
- If another thread calls VIDIOC_STREAMOFF while sess->vq_lock is dropped, !mali_vpu_session_can_stream(sess) returns 0 to vb2_core_streamon(), which still sets q->streaming = 1 even though port->stream_requested was cleared and the firmware remains stopped.
- A concurrent VIDIOC_REQBUFS(0) can enter __vb2_queue_cancel() and free buffers while vb2_core_streamon() is still in flight.

[ ... ]

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c int mali_vpu_session_cleanup(struct mali_vpu_session *sess) · line 437

Quoted 6 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +2 -0
@@ -243,6 +433,21 @@ int mali_vpu_session_cleanup(struct mali_vpu_session *sess)
 			sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_FAILED;
 	}
 
+	if (sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_READY)
+		fw_stop_ret = mali_vpu_session_fw_stop(sess);

[Severity: Medium]
Can mali_vpu_session_cleanup() hang for 10 seconds in mali_vpu_session_fw_stop() when the firmware binary is ready but no LSID is mapped?

Unlike mali_vpu_session_streamoff(), which returns early when !sess->sched_sess.lsid, mali_vpu_session_cleanup() calls mali_vpu_session_fw_stop() -> mali_vpu_session_wait_pending() without checking sess->sched_sess.lsid. Meanwhile, mali_vpu_apply_or_stage_ctrl() sends MVE_REQUEST_CODE_SET_OPTION immediately whenever fw_bin_state is MALI_VPU_SESSION_FW_BIN_READY, even if sess->sched_sess.lsid is NULL:

drivers/media/platform/arm/mali-vpu/mali-vpu-controls.c:mali_vpu_apply_or_stage_ctrl() {
	...
	staged = sess->fw_bin_state != MALI_VPU_SESSION_FW_BIN_READY;

	if (!staged) {
		ret = desc->set(sess, state->cid, value);
		if (ret)
			return ret;
	}
	...
}

If fw_bin_state reaches MALI_VPU_SESSION_FW_BIN_READY without an active LSID (for example, when CAPTURE is prepared while OUTPUT is stopped, or when mali_vpu_sched_switch_in() fails with -EBUSY because another session holds LSID 0) and userspace sets a control before closing the fd, sess->fw.pending_response_count is incremented for an unscheduled firmware instance and mali_vpu_session_cleanup() blocks until the 10-second mali_vpu_fw_response_timeout_ms expires.

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

RFC PATCH 20/28 media: mali-vpu: implement vb2 buffer queue handling

In reply to Charvi Mehta

From: Rath Sankaralingam <[email protected]>

Implement the vb2 callbacks needed to initialise, queue and clean up Mali VPU buffers.

Record each DMA-SG plane in an MMU pages object during buffer initialisation. When a buffer is queued, copy its timestamp, flags and per-plane payload information into the driver buffer, add it to the v4l2-m2m queue and prepare it for firmware submission.

Add helpers to map and unmap buffer planes in the VPU MMU, synchronise their cache state and configure decoded frame layouts. Defer each buffer until its port requests streaming and the firmware is running, then submit deferred buffers when firmware reports RUNNING. Release the per-plane MMU bookkeeping when vb2 cleans up the buffer.

---
 .../platform/arm/mali-vpu/mali-vpu-buffer.c   | 275 ++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-buffer.h   |  74 +++++
 .../platform/arm/mali-vpu/mali-vpu-firmware.c |   4 +-
 .../platform/arm/mali-vpu/mali-vpu-firmware.h |   7 +
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 145 +++++++++
 .../platform/arm/mali-vpu/mali-vpu-session.h  |   6 +
 .../platform/arm/mali-vpu/mali-vpu-vb2.c      |  23 ++
 7 files changed, 532 insertions(+), 2 deletions(-)
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +275 -0
index 7ace0489996a8..c1df3e2d4eacf 100644
@@ -4,13 +4,44 @@
  */
 
 #include <linux/errno.h>
+#include <media/videobuf2-dma-sg.h>
 
 #include "mali-vpu-buffer.h"
 #include "mali-vpu-session.h"
 
+static int mali_vpu_buffer_alloc_planes(struct mali_vpu_buffer *vpu_buf,
+					struct sg_table **sgt)
+{
+	u32 i;
+	int ret;
+
+	for (i = 0; i < vpu_buf->nplanes; ++i) {
+		struct mali_vpu_buffer_plane *plane = &vpu_buf->planes[i];
+
+		plane->pages = mali_vpu_mmu_alloc_pages_sg(vpu_buf->dev, sgt[i], 0);
+		if (IS_ERR(plane->pages)) {
+			ret = PTR_ERR(plane->pages);
+			goto free_pages;
+		}
+	}
+
+	return 0;
+
+free_pages:
+	while (i--)
+		mali_vpu_mmu_free_pages(vpu_buf->planes[i].pages);
+
+	return ret;
+}
+
 int mali_vpu_vb2_buffer_init(struct vb2_buffer *vb2_buf)
 {
+	struct vb2_v4l2_buffer *v4l2_buf = to_vb2_v4l2_buffer(vb2_buf);
+	struct mali_vpu_buffer *vpu_buf = to_mali_vpu_buffer(v4l2_buf);
 	struct mali_vpu_session *sess = vb2_get_drv_priv(vb2_buf->vb2_queue);
+	struct sg_table *sgt[MALI_VPU_BUFFER_MAX_PLANES] = { 0 };
+	u32 i;
+	int ret;
 
 	if (vb2_buf->num_planes > MALI_VPU_BUFFER_MAX_PLANES) {
 		dev_err(sess->vpu_dev->dev,
@@ -19,6 +50,82 @@ int mali_vpu_vb2_buffer_init(struct vb2_buffer *vb2_buf)
 		return -EINVAL;
 	}
 
+	vpu_buf->dev = sess->vpu_dev->dev;
+	vpu_buf->mmu = &sess->mmu;
+	vpu_buf->nplanes = vb2_buf->num_planes;
+	vpu_buf->attr = MALI_VPU_BUF_ATTR_NONE;
+
+	for (i = 0; i < vb2_buf->num_planes; ++i) {
+		sgt[i] = vb2_dma_sg_plane_desc(vb2_buf, i);
+		if (!sgt[i]) {
+			dev_err(sess->vpu_dev->dev,
+				"cannot fetch SG descriptor. vb=%p, plane=%u",
+				vb2_buf, i);
+			return -ENOMEM;
+		}
+	}
+
+	ret = mali_vpu_buffer_alloc_planes(vpu_buf, sgt);
+
+	return ret;
+}
+
+bool mali_vpu_is_buffer_mapped(struct mali_vpu_buffer *vpu_buf)
+{
+	return (vpu_buf->planes[0].pages &&  vpu_buf->planes[0].pages->va);
+}
+
+static size_t mali_vpu_buffer_get_bytesused(struct vb2_buffer *b)
+{
+	size_t size;
+	u32 i;
+
+	for (i = 0, size = 0; i < b->num_planes; i++)
+		size += b->planes[i].bytesused;
+
+	return size;
+}
+
+static void mali_vpu_buffer_update_flags_from_v4l2(struct vb2_buffer *vb2_buf)
+{
+	struct vb2_v4l2_buffer *v4l2_buf = to_vb2_v4l2_buffer(vb2_buf);
+	struct mali_vpu_buffer *vpu_buf = to_mali_vpu_buffer(v4l2_buf);
+
+	u32 flags = v4l2_buf->flags;
+
+	vpu_buf->flags = 0;
+
+	if (V4L2_TYPE_IS_OUTPUT(vb2_buf->type) && mali_vpu_buffer_get_bytesused(vb2_buf) == 0)
+		flags |= V4L2_BUF_FLAG_LAST;
+
+	if (flags & V4L2_BUF_FLAG_LAST)
+		vpu_buf->flags |= MALI_VPU_BUFFER_EOS;
+}
+
+int mali_vpu_buffer_update_info_from_v4l2(struct vb2_buffer *vb2_buf)
+{
+	struct vb2_v4l2_buffer *v4l2_buf = to_vb2_v4l2_buffer(vb2_buf);
+	struct mali_vpu_buffer *vpu_buf = to_mali_vpu_buffer(v4l2_buf);
+	u32 offset;
+	u32 i;
+	int ret;
+
+	vpu_buf->user_data = vb2_buf->timestamp;
+	vpu_buf->type = vb2_buf->type;
+
+	for (i = 0; i < vb2_buf->num_planes; i++) {
+		offset = vb2_buf->planes[i].data_offset;
+
+		/* set plane payload size and offset */
+		ret = mali_vpu_buffer_set_plane_payload(vpu_buf, i, vb2_buf->planes[i].bytesused,
+							offset);
+		if (ret)
+			return ret;
+	}
+
+	/* update flags */
+	mali_vpu_buffer_update_flags_from_v4l2(vb2_buf);
+
 	return 0;
 }
 
@@ -49,3 +156,171 @@ int mali_vpu_vb2_buffer_out_validate(struct vb2_buffer *vb2_buf)
 
 	return 0;
 }
+
+static int mali_vpu_buffer_plane_map(struct mali_vpu_buffer *vpu_buf, mali_vpu_mmu_va begin,
+				     mali_vpu_mmu_va end, u32 plane)
+{
+	while (begin < end) {
+		struct mali_vpu_buffer_plane *p = &vpu_buf->planes[plane];
+		int ret;
+
+		ret = mali_vpu_mmu_map_pages(vpu_buf->mmu, begin, p->pages,
+					     MALI_VPU_ATTR_SHARED_RW, MALI_VPU_ACCESS_READ_WRITE);
+		if (!ret) {
+			dev_dbg(vpu_buf->dev,
+				"memory map buffer. buf=%p, plane=%u, va=0x%x, size=%zu.",
+				vpu_buf, plane, p->pages->va,
+				mali_vpu_mmu_size_pages(p->pages));
+			return 0;
+		}
+
+		if (ret != -EAGAIN)
+			return ret;
+
+		begin += SZ_1M;
+	}
+
+	return -ENOMEM;
+}
+
+int mali_vpu_buffer_map(struct mali_vpu_buffer *vpu_buf, mali_vpu_mmu_va begin, mali_vpu_mmu_va end)
+{
+	u32 i;
+	int ret;
+
+	for (i = 0; i < vpu_buf->nplanes; i++) {
+		struct mali_vpu_buffer_plane *plane = &vpu_buf->planes[i];
+
+		if (plane->pages) {
+			ret = mali_vpu_buffer_plane_map(vpu_buf, begin, end, i);
+			if (ret) {
+				mali_vpu_buffer_unmap(vpu_buf);
+				return ret;
+			}
+		}
+	}
+	return 0;
+}
+
+void mali_vpu_buffer_unmap(struct mali_vpu_buffer *vpu_buf)
+{
+	u32 i;
+
+	for (i = 0; i < vpu_buf->nplanes; i++) {
+		struct mali_vpu_buffer_plane *plane = &vpu_buf->planes[i];
+
+		if (plane->pages && plane->pages->va != 0)
+			mali_vpu_mmu_unmap_pages(plane->pages);
+	}
+}
+
+size_t mali_vpu_buffer_plane_size(struct mali_vpu_buffer *vpu_buf, u32 plane)
+{
+	struct vb2_buffer *vb2_buf = &vpu_buf->m2m_buf.vb.vb2_buf;
+
+	if (plane >= vb2_buf->num_planes)
+		return 0;
+
+	return vb2_plane_size(vb2_buf, plane);
+}
+
+int mali_vpu_buffer_sync(struct mali_vpu_buffer *vpu_buf, enum dma_data_direction dir)
+{
+	u32 i;
+	int ret;
+
+	for (i = 0; i < vpu_buf->nplanes; i++) {
+		struct mali_vpu_buffer_plane *plane = &vpu_buf->planes[i];
+
+		if (plane->pages) {
+			ret = mali_vpu_mmu_sync_pages(plane->pages, dir);
+			if (ret)
+				return ret;
+		}
+	}
+
+	return 0;
+}
+
+int mali_vpu_buffer_set_plane_payload(struct mali_vpu_buffer *vpu_buf, u32 plane,
+				      u32 bytesused, u32 offset)
+{
+	struct mali_vpu_buffer_plane *p;
+	size_t capacity;
+
+	if (plane >= vpu_buf->nplanes)
+		return -EINVAL;
+
+	capacity = mali_vpu_buffer_plane_size(vpu_buf, plane);
+
+	if (offset > capacity || bytesused > capacity || offset > bytesused) {
+		dev_err(vpu_buf->dev,
+			"invalid plane payload. Plane: %u, capacity: %zu, bytesused=%u, offset=%u.\n",
+			plane, capacity, bytesused, offset);
+		return -EINVAL;
+	}
+
+	p = &vpu_buf->planes[plane];
+
+	p->payload_size = bytesused - offset;
+	p->offset = offset;
+
+	return 0;
+}
+
+int mali_vpu_buffer_set_frame_layout(struct mali_vpu_buffer *vpu_buf)
+{
+	struct vb2_buffer *vb2_buf = &vpu_buf->m2m_buf.vb.vb2_buf;
+	struct mali_vpu_session *sess = vb2_get_drv_priv(vb2_buf->vb2_queue);
+	struct mali_vpu_port *port;
+	struct v4l2_pix_format_mplane *pix_mp;
+	int ret;
+	size_t size;
+	u32 i;
+
+	port = V4L2_TYPE_IS_OUTPUT(vb2_buf->vb2_queue->type) ? &sess->port_src :
+							       &sess->port_dst;
+
+	pix_mp = &port->curr_fmt.fmt.pix_mp;
+
+	if (vpu_buf->nplanes != pix_mp->num_planes)
+		return -EINVAL;
+
+	for (i = 0; i < vpu_buf->nplanes; i++) {
+		struct mali_vpu_buffer_plane *plane = &vpu_buf->planes[i];
+
+		size = pix_mp->plane_fmt[i].sizeimage;
+		plane->stride = pix_mp->plane_fmt[i].bytesperline;
+		plane->offset = 0;
+
+		if (!V4L2_TYPE_IS_OUTPUT(vpu_buf->type)) {
+			ret = mali_vpu_buffer_set_plane_payload(vpu_buf, i, size, 0);
+			if (ret)
+				return ret;
+		}
+
+		if (plane->payload_size != size) {
+			dev_err(vpu_buf->dev,
+				"payload size mismatch. Plane: %u, payload_size: %u, actual: %zu.\n",
+				i, plane->payload_size, size);
+			return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+
+void mali_vpu_vb2_buffer_cleanup(struct vb2_buffer *vb2_buf)
+{
+	struct vb2_v4l2_buffer *v4l2_buf = to_vb2_v4l2_buffer(vb2_buf);
+	struct mali_vpu_buffer *vpu_buf = to_mali_vpu_buffer(v4l2_buf);
+	u32 i;
+
+	for (i = 0; i < vpu_buf->nplanes; i++) {
+		if (!vpu_buf->planes[i].pages)
+			continue;
+
+		mali_vpu_mmu_free_pages(vpu_buf->planes[i].pages);
+		vpu_buf->planes[i].pages = NULL;
+	}
+}
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.h +74 -0

88 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +2 -2
index c11d5c6c0ec5f..820853ca4519a 100644
@@ -18,8 +18,8 @@
 #define MALI_VPU_FW_MAX_CORES 8
 #define MALI_VPU_RPC_VA_GRANULE SZ_16M
 
-static int mali_vpu_fw_get_region(enum mali_vpu_fw_region region,
-				  mali_vpu_mmu_va *begin, mali_vpu_mmu_va *end)
+int mali_vpu_fw_get_region(enum mali_vpu_fw_region region,
+			   mali_vpu_mmu_va *begin, mali_vpu_mmu_va *end)
 {
 	switch (region) {
 	case MALI_VPU_FW_REGION_MSG_HOST:
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.h +7 -0
index c03a07d994873..5431959befb24 100644
@@ -10,6 +10,8 @@
 #include <linux/media/arm/mali-vpu-config.h>
 #include <linux/types.h>
 
+#include "mali-vpu-mmu.h"
+
 #define MALI_VPU_FW_HTABLE_BITS 3
 #define MALI_VPU_FW_TEXT_BASE_ADDR 0x1000u
 
@@ -101,6 +103,7 @@ struct mali_vpu_fw_error {
  * enum mali_vpu_fw_code - Driver representation of firmware messages.
  * @MALI_VPU_FW_CODE_ERROR: Firmware reported fatal error.
  * @MALI_VPU_FW_CODE_JOB: Firmware dequeued a job.
+ * @MALI_VPU_FW_CODE_BUFFER: Send and receive buffer.
  * @MALI_VPU_FW_CODE_PING: Ping request sent to firmware.
  * @MALI_VPU_FW_CODE_PONG: Firmware responded to a ping request.
  * @MALI_VPU_FW_CODE_IDLE: Firmware informs the driver that it's idle.
@@ -115,6 +118,7 @@ struct mali_vpu_fw_error {
 enum mali_vpu_fw_code {
 	MALI_VPU_FW_CODE_ERROR,              /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_JOB,                /* Driver -> Firmware. */
+	MALI_VPU_FW_CODE_BUFFER,             /* Driver <-> Firmware. */
 	MALI_VPU_FW_CODE_PING,               /* Driver -> Firmware. */
 	MALI_VPU_FW_CODE_PONG,               /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_IDLE,               /* Driver <- Firmware. */
@@ -138,6 +142,7 @@ struct mali_vpu_fw_msg {
 		enum mali_vpu_fw_state state;
 		struct mali_vpu_fw_job job;
 		struct mali_vpu_fw_error error;
+		struct mali_vpu_buffer *vpu_buf;
 	} data;
 };
 
@@ -246,6 +251,8 @@ int mali_vpu_fw_send_ping(struct mali_vpu_fw *fw);
 int mali_vpu_fw_send_state_change(struct mali_vpu_fw *fw, enum mali_vpu_fw_state state);
 int mali_vpu_fw_send_set_option(struct mali_vpu_fw *fw, struct mali_vpu_fw_set_option *option);
 int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg);
+int mali_vpu_fw_get_region(enum mali_vpu_fw_region region, mali_vpu_mmu_va *begin,
+			   mali_vpu_mmu_va *end);
 int mali_vpu_fw_handle_rpc(struct mali_vpu_fw *fw, bool *flush_mmu);
 
 #endif /* __MALI_VPU_FIRMWARE_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +145 -0

163 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +6 -0
index e331965d32fd8..5cabb7844131b 100644
@@ -12,6 +12,7 @@
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-fh.h>
 
+#include "mali-vpu-buffer.h"
 #include "mali-vpu-device.h"
 #include "mali-vpu-firmware.h"
 #include "mali-vpu-formats.h"
@@ -52,6 +53,10 @@ struct mali_vpu_port {
  * streaming: OUTPUT for a decoder, both ports for an encoder.
  * Binary READY and firmware RUNNING are separate states.
  *
+ * Buffers received before the firmware confirms RUNNING are deferred. The
+ * RUNNING response submits those buffers. Buffers received afterwards are
+ * submitted directly.
+ *
  * Session cleanup waits for an in-flight callback before releasing the
  * session-owned binary reference and callback event. On timeout, it cancels
  * the pending cache waiter or waits for an already claimed callback.
@@ -128,6 +133,7 @@ int mali_vpu_session_prepare_streaming(struct mali_vpu_session *sess,
 				       enum v4l2_buf_type type);
 int mali_vpu_session_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type type);
 void mali_vpu_session_irq(struct mali_vpu_session *sess);
+int mali_vpu_session_qbuf(struct mali_vpu_session *sess, struct vb2_buffer *vb2_buf);
 
 static inline struct mali_vpu_session *mali_vpu_get_session(struct file *filp)
 {
drivers/media/platform/arm/mali-vpu/mali-vpu-vb2.c +23 -0
index b9f6968e9f13f..41158bf4e0ecb 100644
@@ -55,8 +55,30 @@ static void mali_vpu_vb2_buffer_queue(struct vb2_buffer *vb2_buf)
 {
 	struct mali_vpu_session *sess = vb2_get_drv_priv(vb2_buf->vb2_queue);
 	struct vb2_v4l2_buffer *v4l2_buf = to_vb2_v4l2_buffer(vb2_buf);
+	int ret;
+
+	/* Update vb2 metadata and payload info onto mali_vpu_buffer. */
+	ret = mali_vpu_buffer_update_info_from_v4l2(vb2_buf);
+	if (ret)
+		goto err_buf_done;
 
 	v4l2_m2m_buf_queue(sess->m2m_ctx, v4l2_buf);
+
+	ret = mali_vpu_session_qbuf(sess, vb2_buf);
+	if (ret)
+		goto err_m2m_buf_remove;
+
+	return;
+
+err_m2m_buf_remove:
+	if (V4L2_TYPE_IS_OUTPUT(vb2_buf->type))
+		v4l2_m2m_src_buf_remove_by_buf(sess->m2m_ctx, v4l2_buf);
+	else
+		v4l2_m2m_dst_buf_remove_by_buf(sess->m2m_ctx, v4l2_buf);
+
+err_buf_done:
+	v4l2_m2m_buf_done(v4l2_buf, VB2_BUF_STATE_ERROR);
+
 }
 
 static int mali_vpu_vb2_prepare_streaming(struct vb2_queue *q)
@@ -123,6 +145,7 @@ static const struct vb2_ops mali_vpu_vb2_ops = {
 	.buf_init		= mali_vpu_vb2_buffer_init,
 	.buf_prepare		= mali_vpu_vb2_buffer_prepare,
 	.buf_queue		= mali_vpu_vb2_buffer_queue,
+	.buf_cleanup		= mali_vpu_vb2_buffer_cleanup,
 	.prepare_streaming	= mali_vpu_vb2_prepare_streaming,
 	.start_streaming	= mali_vpu_vb2_start_streaming,
 	.stop_streaming		= mali_vpu_vb2_stop_streaming,
Signature · 1 line
2.43.0
#47 lore raw

Re: [RFC PATCH 20/28] media: mali-vpu: implement vb2 buffer queue handling

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] media: mali-vpu: out-of-bounds VA mapping in mali_vpu_buffer_plane_map() - [High] media: mali-vpu…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] media: mali-vpu: out-of-bounds VA mapping in mali_vpu_buffer_plane_map()
- [High] media: mali-vpu: wrong capture DMA sync in mali_vpu_queue_buffer()
- [High] media: mali-vpu: missing MMU flush in mali_vpu_vb2_buffer_cleanup()
- [Medium] media: mali-vpu: stranded buffers on error in mali_vpu_submit_deferred_bufs()
- [Medium] media: mali-vpu: unreachable 0-byte EOS check without allow_zero_bytesused
--

commit 8e09878f27886489a304a46715a5c2d9a14beef9
Author: Rath Sankaralingam <[email protected]>

media: mali-vpu: implement vb2 buffer queue handling

This commit implements the vb2 callbacks for initializing, queuing, and cleaning up Mali VPU buffers, along with MMU mapping, cache synchronization, and deferred buffer submission when the firmware enters the RUNNING state.

Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +0 -0
index 7ace0489996a8..c1df3e2d4eacf 100644

[ ... ]

Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +1 -0
@@ -19,6 +50,82 @@ int mali_vpu_vb2_buffer_init(struct vb2_buffer *vb2_buf)
 		return -EINVAL;
 	}
 
+	vpu_buf->dev = sess->vpu_dev->dev;

[ ... ]

Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +5 -0
+static void mali_vpu_buffer_update_flags_from_v4l2(struct vb2_buffer *vb2_buf)
+{
+	struct vb2_v4l2_buffer *v4l2_buf = to_vb2_v4l2_buffer(vb2_buf);
+	struct mali_vpu_buffer *vpu_buf = to_mali_vpu_buffer(v4l2_buf);
+

drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c int mali_vpu_vb2_buffer_init(struct vb2_buffer *vb2_buf) · line 103

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +10 -0
+	u32 flags = v4l2_buf->flags;
+
+	vpu_buf->flags = 0;
+
+	if (V4L2_TYPE_IS_OUTPUT(vb2_buf->type) && mali_vpu_buffer_get_bytesused(vb2_buf) == 0)
+		flags |= V4L2_BUF_FLAG_LAST;
+
+	if (flags & V4L2_BUF_FLAG_LAST)
+		vpu_buf->flags |= MALI_VPU_BUFFER_EOS;
+}

[Severity: Medium]
Can mali_vpu_buffer_get_bytesused(vb2_buf) == 0 ever be reached on an OUTPUT buffer without vq->allow_zero_bytesused set?

mali_vpu_queue_init_type() does not set allow_zero_bytesused on the OUTPUT vb2_queue, so vb2_fill_vb2_v4l2_buffer() replaces a 0-byte bytesused from userspace with the full plane length. As a result, a 0-byte OUTPUT buffer queued to signal EOS will instead be given a payload_size equal to the full buffer capacity rather than setting MALI_VPU_BUFFER_EOS. Also, mali_vpu_buffer_get_bytesused() sums b->planes[i].bytesused without subtracting data_offset, so an empty payload with a non-zero data_offset will not be treated as 0 bytes used either.

[ ... ]

Quoted 20 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +16 -0
@@ -49,3 +156,171 @@ int mali_vpu_vb2_buffer_out_validate(struct vb2_buffer *vb2_buf)
 
 	return 0;
 }
+
+static int mali_vpu_buffer_plane_map(struct mali_vpu_buffer *vpu_buf, mali_vpu_mmu_va begin,
+				     mali_vpu_mmu_va end, u32 plane)
+{
+	while (begin < end) {
+		struct mali_vpu_buffer_plane *p = &vpu_buf->planes[plane];
+		int ret;
+
+		ret = mali_vpu_mmu_map_pages(vpu_buf->mmu, begin, p->pages,
+					     MALI_VPU_ATTR_SHARED_RW, MALI_VPU_ACCESS_READ_WRITE);
+		if (!ret) {
+			dev_dbg(vpu_buf->dev,
+				"memory map buffer. buf=%p, plane=%u, va=0x%x, size=%zu.",
+				vpu_buf, plane, p->pages->va,
+				mali_vpu_mmu_size_pages(p->pages));
+			return 0;

drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c int mali_vpu_vb2_buffer_out_validate(struct vb2_buffer *vb2_buf) · line 184

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +10 -0
+		}
+
+		if (ret != -EAGAIN)
+			return ret;
+
+		begin += SZ_1M;
+	}
+
+	return -ENOMEM;
+}

[Severity: High]
Can mali_vpu_buffer_plane_map() map a plane past the end of the firmware virtual address region?

The loop only checks begin < end, and mali_vpu_mmu_map_pages() maps p->pages->capacity contiguous VPU pages starting at begin without taking an upper bound. When earlier mappings advance begin close to end (or a large DMA-BUF is imported) so that the plane size exceeds end - begin, the mapping spills past end into the adjacent firmware region or wraps around the 32-bit VPU virtual address space.

[ ... ]

Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +4 -0
+void mali_vpu_vb2_buffer_cleanup(struct vb2_buffer *vb2_buf)
+{
+	struct vb2_v4l2_buffer *v4l2_buf = to_vb2_v4l2_buffer(vb2_buf);
+	struct mali_vpu_buffer *vpu_buf = to_mali_vpu_buffer(v4l2_buf);

drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c int mali_vpu_vb2_buffer_out_validate(struct vb2_buffer *vb2_buf) · line 326

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +10 -0
+	u32 i;
+
+	for (i = 0; i < vpu_buf->nplanes; i++) {
+		if (!vpu_buf->planes[i].pages)
+			continue;
+
+		mali_vpu_mmu_free_pages(vpu_buf->planes[i].pages);
+		vpu_buf->planes[i].pages = NULL;
+	}
+}

[Severity: High]
Does unmapping buffer pages in mali_vpu_vb2_buffer_cleanup() without calling mali_vpu_sched_flush_mmu() leave stale TLB entries in the VPU MMU?

mali_vpu_mmu_free_pages() -> mali_vpu_mmu_unmap_pages() clears the L2 PTEs in sess->mmu, and when a new DMA-BUF is queued on the same VB2 slot (or CAPTURE buffers are reallocated while the OUTPUT queue keeps the session LSID active), mali_vpu_buffer_plane_map() immediately reuses the lowest unmapped virtual addresses from the start of the region. Without an LSID MMU flush, the hardware can continue using cached translations pointing to the previous buffer's freed physical pages.

Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +0 -0
index 6d22a5d479008..540104bcd38e2 100644
@@ -150,6 +150,122 @@ int mali_vpu_session_fw_send_set_option(struct mali_vpu_session *sess,

[ ... ]

Quoted 18 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +18 -0
+static int mali_vpu_queue_buffer(struct mali_vpu_session *sess, struct mali_vpu_buffer *vpu_buf)
+{
+	int ret;
+	enum dma_data_direction dma_dir;
+
+	dma_dir = (vpu_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
+		   DMA_TO_DEVICE : DMA_FROM_DEVICE;
+
+	/* Map MMU buffer pages if it is not already mapped */
+	if (!mali_vpu_is_buffer_mapped(vpu_buf)) {
+		ret = mali_vpu_buffer_map_fw_region(sess, vpu_buf);
+		if (ret)
+			return ret;
+	}
+
+	if (!mali_vpu_is_coded_q(sess, vpu_buf->type) && sess->role == MALI_VPU_DECODER) {
+		ret = mali_vpu_buffer_set_frame_layout(vpu_buf);
+		if (ret)

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c int mali_vpu_session_fw_send_set_option(struct mali_vpu_session *sess, · line 202

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +10 -0
+			return ret;
+	}
+
+	/* dma sync for cache coherency */
+	ret = mali_vpu_buffer_sync(vpu_buf, dma_dir);
+	if (ret)
+		return ret;
+
+	return 0;
+}

[Severity: High]
Does calling mali_vpu_buffer_sync(vpu_buf, DMA_FROM_DEVICE) on a CAPTURE buffer before VPU DMA runs sync in the wrong direction (for the CPU instead of the device)?

drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c:mali_vpu_mmu_sync_pages() {
	if (dir == DMA_FROM_DEVICE) {
		for (i = 0; i < pages->count; i += MALI_VPU_PAGES_PER_LINUX_PAGE)
			dma_sync_single_for_cpu(pages->dev, pages->pages[i].dma,
						PAGE_SIZE, DMA_FROM_DEVICE);
}

When queuing a buffer to hardware before DMA starts, pages need to be synced for the device (which vb2_dma_sg_prepare() already does during buffer prepare), whereas dma_sync_single_for_cpu() transfers ownership back to the CPU before the VPU writes the decoded frame.

Quoted 55 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +55 -0
+
+static int mali_vpu_submit_deferred_bufs_for_queue(struct mali_vpu_session *sess,
+						   enum v4l2_buf_type type)
+{
+	struct v4l2_m2m_buffer *buffer;
+	struct mali_vpu_buffer *vpu_buf;
+	int ret;
+
+	if (!mali_vpu_session_get_port(sess, type)->stream_requested ||
+	    !mali_vpu_session_can_stream(sess) ||
+	    sess->fw_state != MALI_VPU_FW_STATE_RUNNING)
+		return 0;
+
+	if (V4L2_TYPE_IS_OUTPUT(type)) {
+		v4l2_m2m_for_each_src_buf(sess->m2m_ctx, buffer) {
+			vpu_buf = to_mali_vpu_buffer(&buffer->vb);
+			if (vpu_buf->attr != MALI_VPU_BUF_ATTR_DEFERRED)
+				continue;
+
+			ret = mali_vpu_queue_buffer(sess, vpu_buf);
+			if (ret)
+				return ret;
+
+			vpu_buf->attr = MALI_VPU_BUF_ATTR_QUEUED;
+		}
+	} else if (V4L2_TYPE_IS_CAPTURE(type)) {
+		v4l2_m2m_for_each_dst_buf(sess->m2m_ctx, buffer) {
+			vpu_buf = to_mali_vpu_buffer(&buffer->vb);
+			if (vpu_buf->attr != MALI_VPU_BUF_ATTR_DEFERRED)
+				continue;
+
+			ret = mali_vpu_queue_buffer(sess, vpu_buf);
+			if (ret)
+				return ret;
+
+			vpu_buf->attr = MALI_VPU_BUF_ATTR_QUEUED;
+		}
+	} else {
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int mali_vpu_submit_deferred_bufs(struct mali_vpu_session *sess)
+{
+	int ret;
+
+	ret = mali_vpu_submit_deferred_bufs_for_queue(sess, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
+	if (ret)
+		goto err_submit;
+
+	ret = mali_vpu_submit_deferred_bufs_for_queue(sess,
+						      V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
+	if (ret)

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c int mali_vpu_session_fw_send_set_option(struct mali_vpu_session *sess, · line 267

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +10 -0
+		goto err_submit;
+
+	mali_vpu_sched_send_irq(&sess->sched_sess, &sess->vpu_dev->sched);
+
+	return 0;
+
+err_submit:
+	dev_err(sess->vpu_dev->dev, "failed to queue deferred buffers\n");
+	return ret;
+}

[Severity: Medium]
If mali_vpu_queue_buffer() fails for a deferred buffer here, doesn't this leave both the failed buffer and the remaining buffers in the batch stranded?

Unlike mali_vpu_vb2_buffer_queue(), which removes a failed buffer from sess->m2m_ctx and completes it with VB2_BUF_STATE_ERROR, an error in mali_vpu_submit_deferred_bufs_for_queue() returns immediately while the buffer is still on the m2m ready queue in VB2_BUF_STATE_ACTIVE. Bailing out to err_submit also skips remaining deferred buffers and skips mali_vpu_sched_send_irq() for any earlier buffers in the loop that were already marked MALI_VPU_BUF_ATTR_QUEUED.

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

RFC PATCH 21/28 media: mali-vpu: handle firmware buffer exchange

In reply to Charvi Mehta

From: Rath Sankaralingam <[email protected]>

Send queued bitstream and frame buffers to firmware using the v3 buffer protocol. Populate the buffer descriptors with firmware-visible plane addresses, allocation and payload sizes, strides, frame dimensions, formats, flags and user data as appropriate for each buffer type.

Handle input and output buffer responses from firmware. Recover the associated driver buffer, propagate returned user data, dimensions and status flags to V4L2, synchronize capture buffers for the CPU, and complete the corresponding v4l2-m2m buffer with the appropriate vb2 state.

Use non-zero buffer IDs to associate firmware responses with tracked in-flight buffers.

---
 .../platform/arm/mali-vpu/mali-vpu-buffer.c   | 100 ++++++
 .../platform/arm/mali-vpu/mali-vpu-buffer.h   |  14 +
 .../platform/arm/mali-vpu/mali-vpu-firmware.c | 300 ++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-firmware.h |  10 +
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   | 128 ++++++++
 .../platform/arm/mali-vpu/mali-vpu-session.c  |  49 +++
 6 files changed, 601 insertions(+)
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +100 -0
index c1df3e2d4eacf..e404fb933195f 100644
@@ -53,6 +53,7 @@ int mali_vpu_vb2_buffer_init(struct vb2_buffer *vb2_buf)
 	vpu_buf->dev = sess->vpu_dev->dev;
 	vpu_buf->mmu = &sess->mmu;
 	vpu_buf->nplanes = vb2_buf->num_planes;
+	vpu_buf->fw_buffer_id = 0;
 	vpu_buf->attr = MALI_VPU_BUF_ATTR_NONE;
 
 	for (i = 0; i < vb2_buf->num_planes; ++i) {
@@ -86,6 +87,40 @@ static size_t mali_vpu_buffer_get_bytesused(struct vb2_buffer *b)
 	return size;
 }
 
+static int mali_vpu_buffer_clear_bytesused(struct mali_vpu_buffer *vpu_buf)
+{
+	struct vb2_buffer *vb2_buf = &vpu_buf->m2m_buf.vb.vb2_buf;
+	u32 i;
+
+	for (i = 0; i < vb2_buf->num_planes; i++) {
+		vb2_set_plane_payload(vb2_buf, i, 0);
+		vb2_buf->planes[i].data_offset = 0;
+	}
+
+	return 0;
+}
+
+static int mali_vpu_buffer_update_bytesused_to_v4l2(struct mali_vpu_buffer *vpu_buf)
+{
+	struct vb2_buffer *vb2_buf = &vpu_buf->m2m_buf.vb.vb2_buf;
+	u32 i;
+
+	if (vpu_buf->nplanes != vb2_buf->num_planes) {
+		dev_dbg(vpu_buf->dev,
+			"mali VPU buf and vb2 buf have different num_planes. vpu_buf=%u vb2_buf=%u.\n",
+			vpu_buf->nplanes, vb2_buf->num_planes);
+		return -EINVAL;
+	}
+
+	for (i = 0; i < vpu_buf->nplanes; i++) {
+		vb2_buf->planes[i].bytesused =
+			vpu_buf->planes[i].payload_size + vpu_buf->planes[i].offset;
+		vb2_buf->planes[i].data_offset = vpu_buf->planes[i].offset;
+	}
+
+	return 0;
+}
+
 static void mali_vpu_buffer_update_flags_from_v4l2(struct vb2_buffer *vb2_buf)
 {
 	struct vb2_v4l2_buffer *v4l2_buf = to_vb2_v4l2_buffer(vb2_buf);
@@ -102,6 +137,27 @@ static void mali_vpu_buffer_update_flags_from_v4l2(struct vb2_buffer *vb2_buf)
 		vpu_buf->flags |= MALI_VPU_BUFFER_EOS;
 }
 
+static void mali_vpu_buffer_update_flags_to_v4l2(struct mali_vpu_buffer *vpu_buf)
+{
+	struct vb2_v4l2_buffer *v4l2_buf = &vpu_buf->m2m_buf.vb;
+
+	v4l2_buf->flags &= ~(V4L2_BUF_FLAG_ERROR |
+			     V4L2_BUF_FLAG_KEYFRAME |
+			     V4L2_BUF_FLAG_LAST);
+
+	if (vpu_buf->flags & MALI_VPU_BUFFER_EOS ||
+	    (V4L2_TYPE_IS_CAPTURE(vpu_buf->type) &&
+	     mali_vpu_buffer_get_bytesused(&vpu_buf->m2m_buf.vb.vb2_buf) == 0))
+		v4l2_buf->flags |= V4L2_BUF_FLAG_LAST;
+
+	if (vpu_buf->flags & MALI_VPU_BUFFER_CORRUPT)
+		v4l2_buf->flags |= V4L2_BUF_FLAG_ERROR;
+
+	if (V4L2_TYPE_IS_CAPTURE(vpu_buf->type) &&
+	    vpu_buf->flags & MALI_VPU_BUFFER_REJECTED)
+		mali_vpu_buffer_clear_bytesused(vpu_buf);
+}
+
 int mali_vpu_buffer_update_info_from_v4l2(struct vb2_buffer *vb2_buf)
 {
 	struct vb2_v4l2_buffer *v4l2_buf = to_vb2_v4l2_buffer(vb2_buf);
@@ -129,6 +185,30 @@ int mali_vpu_buffer_update_info_from_v4l2(struct vb2_buffer *vb2_buf)
 	return 0;
 }
 
+enum vb2_buffer_state mali_vpu_buffer_update_info_to_v4l2(struct mali_vpu_buffer *vpu_buf)
+{
+	struct vb2_v4l2_buffer *v4l2_buf = &vpu_buf->m2m_buf.vb;
+	struct vb2_buffer *vb2_buf = &v4l2_buf->vb2_buf;
+	int ret;
+
+	vb2_buf->timestamp = vpu_buf->user_data;
+
+	if (V4L2_TYPE_IS_CAPTURE(vpu_buf->type)) {
+		ret = mali_vpu_buffer_update_bytesused_to_v4l2(vpu_buf);
+		if (ret)
+			goto error;
+	}
+
+	mali_vpu_buffer_update_flags_to_v4l2(vpu_buf);
+	if ((v4l2_buf->flags & V4L2_BUF_FLAG_ERROR) != 0)
+		goto error;
+
+	return VB2_BUF_STATE_DONE;
+
+error:
+	return VB2_BUF_STATE_ERROR;
+}
+
 int mali_vpu_vb2_buffer_prepare(struct vb2_buffer *vb2_buf)
 {
 	struct mali_vpu_session *sess = vb2_get_drv_priv(vb2_buf->vb2_queue);
@@ -157,6 +237,20 @@ int mali_vpu_vb2_buffer_out_validate(struct vb2_buffer *vb2_buf)
 	return 0;
 }
 
+mali_vpu_mmu_va mali_vpu_buffer_plane_va(struct mali_vpu_buffer *vpu_buf, u32 plane)
+{
+	struct mali_vpu_buffer_plane *vpu_buf_plane;
+
+	if (plane >= vpu_buf->nplanes)
+		return 0;
+
+	vpu_buf_plane = &vpu_buf->planes[plane];
+	if (!vpu_buf_plane->pages)
+		return 0;
+
+	return vpu_buf_plane->pages->va + vpu_buf_plane->pages->offset;
+}
+
 static int mali_vpu_buffer_plane_map(struct mali_vpu_buffer *vpu_buf, mali_vpu_mmu_va begin,
 				     mali_vpu_mmu_va end, u32 plane)
 {
@@ -283,6 +377,12 @@ int mali_vpu_buffer_set_frame_layout(struct mali_vpu_buffer *vpu_buf)
 
 	pix_mp = &port->curr_fmt.fmt.pix_mp;
 
+	vpu_buf->width = pix_mp->width;
+	vpu_buf->height = pix_mp->height;
+	vpu_buf->format = mali_vpu_find_format(sess, pix_mp->pixelformat, vpu_buf->type);
+	if (!vpu_buf->format)
+		return -EINVAL;
+
 	if (vpu_buf->nplanes != pix_mp->num_planes)
 		return -EINVAL;
 
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.h +14 -0
index 645dcc775cd2c..01dd49082fdcc 100644
@@ -13,6 +13,8 @@
 #define MALI_VPU_BUFFER_MAX_PLANES 3
 
 #define MALI_VPU_BUFFER_EOS                  0x00000001
+#define MALI_VPU_BUFFER_CORRUPT              0x00000004
+#define MALI_VPU_BUFFER_REJECTED             0x00000008
 
 /**
  * enum mali_vpu_buffer_attributes - Buffer submission state.
@@ -44,6 +46,8 @@ struct mali_vpu_buffer_plane {
 	u32 stride;
 };
 
+struct mali_vpu_format_desc;
+
 /**
  * struct mali_vpu_buffer - Buffer descriptor.
  * @m2m_buf: Embedded v4l2-mem2mem buffer.
@@ -53,6 +57,10 @@ struct mali_vpu_buffer_plane {
  * @type: Type of the buffer (output/capture).
  * @user_data: Userdata(timestamp) to be copied to capture buffer.
  * @flags: Buffer Flags.
+ * @width: Frame width in pixels.
+ * @height: Frame height in pixels.
+ * @fw_buffer_id: Non-zero identifier while registered for firmware submission.
+ * @format: Buffer format.
  * @planes: Array of planes.
  * @attr: Firmware Queuing Status Attribute.
  */
@@ -64,6 +72,10 @@ struct mali_vpu_buffer {
 	u32 type;
 	u64 user_data;
 	u32 flags;
+	u32 width;
+	u32 height;
+	u32 fw_buffer_id;
+	const struct mali_vpu_format_desc *format;
 	struct mali_vpu_buffer_plane planes[MALI_VPU_BUFFER_MAX_PLANES];
 	enum mali_vpu_buffer_attributes attr;
 };
@@ -85,11 +97,13 @@ int mali_vpu_buffer_map(struct mali_vpu_buffer *vpu_buf, mali_vpu_mmu_va begin,
 void mali_vpu_buffer_unmap(struct mali_vpu_buffer *vpu_buf);
 int mali_vpu_buffer_sync(struct mali_vpu_buffer *vpu_buf, enum dma_data_direction dir);
 bool mali_vpu_is_buffer_mapped(struct mali_vpu_buffer *vpu_buf);
+mali_vpu_mmu_va mali_vpu_buffer_plane_va(struct mali_vpu_buffer *vpu_buf, u32 plane);
 
 int mali_vpu_buffer_update_info_from_v4l2(struct vb2_buffer *vb2_buf);
 int mali_vpu_buffer_set_plane_payload(struct mali_vpu_buffer *vpu_buf, u32 plane,
 				      u32 bytesused, u32 offset);
 int mali_vpu_buffer_set_frame_layout(struct mali_vpu_buffer *vpu_buf);
 size_t mali_vpu_buffer_plane_size(struct mali_vpu_buffer *vpu_buf, u32 plane);
+enum vb2_buffer_state mali_vpu_buffer_update_info_to_v4l2(struct mali_vpu_buffer *vpu_buf);
 
 #endif /* __MALI_VPU_BUFFER_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +300 -0

348 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.h +10 -0
index 5431959befb24..c936730698837 100644
@@ -9,7 +9,9 @@
 #include <linux/hashtable.h>
 #include <linux/media/arm/mali-vpu-config.h>
 #include <linux/types.h>
+#include <linux/xarray.h>
 
+#include "mali-vpu-buffer.h"
 #include "mali-vpu-mmu.h"
 
 #define MALI_VPU_FW_HTABLE_BITS 3
@@ -19,6 +21,8 @@ struct device;
 struct mali_vpu_mmu;
 struct mali_vpu_fw_bin;
 struct mali_vpu_mmu_pages;
+struct mve_buffer_frame;
+struct mve_buffer_bitstream;
 
 /**
  * enum mali_vpu_fw_state - Firmware execution state.
@@ -219,6 +223,9 @@ struct mali_vpu_fw_proto_page {
  * @shared_bss_pages: Shared BSS pages mapped into every core instance.
  * @ncores: Number of firmware core-instance regions constructed.
  * @pending_response_count: Number of requests awaiting a firmware response.
+ * @inflight_buffers: XArray mapping non-zero firmware-visible buffer IDs to
+ *			buffers currently owned by firmware.
+ * @next_buffer_id: Next candidate ID for cyclic buffer-ID allocation.
  * @rpc_mem: Keeps track of RPC allocated memory. Maps MVE virtual address to
  *		'struct mali_vpu_mmu_pages' object.
  */
@@ -239,6 +246,8 @@ struct mali_vpu_fw {
 	struct mali_vpu_mmu_pages *shared_bss_pages;
 	u32 ncores;
 	u32 pending_response_count;
+	struct xarray inflight_buffers;
+	u32 next_buffer_id;
 
 	DECLARE_HASHTABLE(rpc_mem, MALI_VPU_FW_HTABLE_BITS);
 };
@@ -247,6 +256,7 @@ int mali_vpu_fw_construct(struct mali_vpu_fw *fw, const struct mali_vpu_fw_bin *
 			  struct device *dev, struct mali_vpu_mmu *mmu, u32 ncores);
 void mali_vpu_fw_cleanup(struct mali_vpu_fw *fw);
 int mali_vpu_fw_send_job(struct mali_vpu_fw *fw, u16 cores, u16 frames);
+int mali_vpu_fw_send_buffer(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *fw_msg);
 int mali_vpu_fw_send_ping(struct mali_vpu_fw *fw);
 int mali_vpu_fw_send_state_change(struct mali_vpu_fw *fw, enum mali_vpu_fw_state state);
 int mali_vpu_fw_send_set_option(struct mali_vpu_fw *fw, struct mali_vpu_fw_set_option *option);
drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h +128 -0

144 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +49 -0

73 lines not on this page · raw message

Signature · 1 line
2.43.0
#49 lore raw

Re: [RFC PATCH 21/28] media: mali-vpu: handle firmware buffer exchange

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] media: mali-vpu: in-flight buffers not untracked on streamoff or cleanup - [Medium] media: mali-vpu…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] media: mali-vpu: in-flight buffers not untracked on streamoff or cleanup
- [Medium] media: mali-vpu: bogus error and early loop exit in mali_vpu_fw_get_buffer()
- [Medium] media: mali-vpu: spurious V4L2_BUF_FLAG_LAST on rejected capture buffers
--

commit 470c746dd3c53a73aac1ec2091f9e9c5b26e72a8
Author: Rath Sankaralingam <[email protected]>

media: mali-vpu: handle firmware buffer exchange

This commit implements buffer submission to the firmware and handling of returned input and output buffer responses using the v3 buffer protocol, tracking in-flight buffers in an xarray by non-zero buffer IDs.

Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +0 -0
index c1df3e2d4eacf..e404fb933195f 100644

[ ... ]

Quoted 14 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +10 -0
@@ -102,6 +137,27 @@ static void mali_vpu_buffer_update_flags_from_v4l2(struct vb2_buffer *vb2_buf)
 		vpu_buf->flags |= MALI_VPU_BUFFER_EOS;
 }
 
+static void mali_vpu_buffer_update_flags_to_v4l2(struct mali_vpu_buffer *vpu_buf)
+{
+	struct vb2_v4l2_buffer *v4l2_buf = &vpu_buf->m2m_buf.vb;
+
+	v4l2_buf->flags &= ~(V4L2_BUF_FLAG_ERROR |
+			     V4L2_BUF_FLAG_KEYFRAME |
+			     V4L2_BUF_FLAG_LAST);
+
+	if (vpu_buf->flags & MALI_VPU_BUFFER_EOS ||
+	    (V4L2_TYPE_IS_CAPTURE(vpu_buf->type) &&

drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c static void mali_vpu_buffer_update_flags_from_v4l2(struct vb2_buffer *vb2_buf) · line 159

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c +10 -0
+	     mali_vpu_buffer_get_bytesused(&vpu_buf->m2m_buf.vb.vb2_buf) == 0))
+		v4l2_buf->flags |= V4L2_BUF_FLAG_LAST;
+
+	if (vpu_buf->flags & MALI_VPU_BUFFER_CORRUPT)
+		v4l2_buf->flags |= V4L2_BUF_FLAG_ERROR;
+
+	if (V4L2_TYPE_IS_CAPTURE(vpu_buf->type) &&
+	    vpu_buf->flags & MALI_VPU_BUFFER_REJECTED)
+		mali_vpu_buffer_clear_bytesused(vpu_buf);
+}

[Severity: Medium]
Can a rejected capture buffer be returned to userspace with V4L2_BUF_FLAG_LAST spuriously set?

When firmware rejects a capture frame buffer with MVE_BUFFER_FRAME_FLAG_REJECTED and zero visible dimensions, mali_vpu_convert_buffer_frame() zeroes plane payload_size and offset, and mali_vpu_buffer_update_bytesused_to_v4l2() propagates bytesused = 0 to vb2_buf before mali_vpu_buffer_update_flags_to_v4l2() runs. As a result, mali_vpu_buffer_get_bytesused() is already 0 here and sets V4L2_BUF_FLAG_LAST even when MALI_VPU_BUFFER_EOS is not set.

Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +0 -0
index 820853ca4519a..4e3eaecb8ebee 100644

[ ... ]

Quoted 7 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +3 -0
@@ -347,6 +348,20 @@ static int mali_vpu_fw_map_core_regions(struct mali_vpu_fw *fw,
 	return ret;
 }
 
+static void mali_vpu_fw_clear_buffers(struct mali_vpu_fw *fw)
+{
+	struct mali_vpu_buffer *vpu_buf;

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c static int mali_vpu_fw_map_core_regions(struct mali_vpu_fw *fw, · line 363

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +10 -0
+	unsigned long id;
+
+	xa_for_each(&fw->inflight_buffers, id, vpu_buf) {
+		xa_erase(&fw->inflight_buffers, id);
+		vpu_buf->fw_buffer_id = 0;
+		vpu_buf->attr = MALI_VPU_BUF_ATTR_NONE;
+	}
+
+	xa_destroy(&fw->inflight_buffers);
+}

[Severity: High]
Can this cause a use-after-free on vpu_buf if buffers are freed after VIDIOC_STREAMOFF?

mali_vpu_vb2_stop_streaming() completes queued buffers back to vb2 without removing in-flight buffers from fw->inflight_buffers or clearing vpu_buf->fw_buffer_id, and mali_vpu_vb2_buffer_cleanup() does not untrack them either. If userspace frees the buffers after streamoff (e.g. via VIDIOC_REQBUFS(0)), dangling vpu_buf pointers remain in fw->inflight_buffers and are written to here during session close (or in mali_vpu_fw_resolve_returned_buffer() if firmware returns the buffer).

If userspace instead restarts streaming and re-queues the same buffers after streamoff, vpu_buf->fw_buffer_id is still non-zero, so mali_vpu_fw_track_buffer() fails with -EBUSY. Also, when firmware returns a buffer that was already completed by stop_streaming, mali_vpu_session_handle_fw_msg() calls mali_vpu_buffer_update_info_to_v4l2() and mutates vb2_buf metadata before mali_vpu_session_buffer_done() checks whether vb2_buf->state is still VB2_BUF_STATE_ACTIVE.

[ ... ]

Quoted 37 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +37 -0
+static int mali_vpu_fw_get_buffer(struct mali_vpu_fw *fw,
+				  struct mali_vpu_fw_proto_page *buf_host_page,
+				  struct mali_vpu_fw_proto_page *buf_mve_page,
+				  struct mali_vpu_fw_msg *msg, u32 buf_type)
+{
+	u16 code;
+	union {
+		struct mve_buffer_frame frame;
+		struct mve_buffer_bitstream bitstream;
+	} fw_msg;
+	size_t size = sizeof(fw_msg);
+	int ret;
+
+	ret = mali_vpu_fw_read_msg(fw, buf_host_page, buf_mve_page, &code,
+				   &fw_msg, &size);
+	if (ret <= 0)
+		return ret;
+
+	switch (code) {
+	case MVE_BUFFER_CODE_FRAME:
+		if (size != sizeof(fw_msg.frame) ||
+		    (!fw_msg.frame.host_handle &&
+		     !(fw_msg.frame.frame_flags & MVE_BUFFER_FRAME_FLAG_EOS)))
+			return -EPROTO;
+
+		ret = mali_vpu_convert_buffer_frame(fw, msg, buf_type, &fw_msg.frame);
+		if (ret)
+			return ret;
+		break;
+	case MVE_BUFFER_CODE_BITSTREAM:
+		if (size != sizeof(fw_msg.bitstream) ||
+		    (!fw_msg.bitstream.host_handle &&
+		     !(fw_msg.bitstream.bitstream_flags & MVE_BUFFER_BITSTREAM_FLAG_EOS)))
+			return -EPROTO;
+
+		ret = mali_vpu_convert_buffer_bitstream(fw, msg, buf_type,
+							&fw_msg.bitstream);

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c int mali_vpu_fw_send_set_option(struct mali_vpu_fw *fw, · line 948

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +10 -0
+		if (ret)
+			return ret;
+		break;
+	default:
+		dev_dbg(fw->dev, "unknown buf code. code: %u.\n", code);
+		break;
+	}
+
+	return 1;
+}

[Severity: Medium]
Does an unknown buffer code in mali_vpu_fw_get_buffer() leave msg->code at 0 (MALI_VPU_FW_CODE_ERROR) while returning 1?

mali_vpu_fw_parse_msg() zeroes *msg before calling mali_vpu_fw_get_buffer(), so hitting the default branch returns 1 with msg->code == MALI_VPU_FW_CODE_ERROR, causing mali_vpu_session_handle_fw_msg() to treat it as a fatal firmware error and fail with -EIO. In addition, if mali_vpu_fw_read_msg() returns 0 because the buffer queue is unexpectedly empty after MVE_RESPONSE_CODE_INPUT or MVE_RESPONSE_CODE_OUTPUT was read from msg_mve, propagating 0 causes mali_vpu_session_irq() to treat msg_mve as empty and stop draining remaining messages instead of flagging -EPROTO.

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

RFC PATCH 22/28 media: mali-vpu: handle fatal firmware error

In reply to Charvi Mehta

From: Rath Sankaralingam <[email protected]>

Record the first fatal error encountered by a session so that failures are propagated consistently instead of allowing firmware operations to continue on an unusable session.

On firmware loading, initialization, message handling or response timeout errors, stop the firmware state machine, terminate the scheduled session, mark both vb2 queues as failed and wake any firmware waiters. Reject subsequent buffer and streaming operations with the recorded error.

---
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 97 ++++++++++++++++---
 .../platform/arm/mali-vpu/mali-vpu-session.h  |  5 +
 2 files changed, 90 insertions(+), 12 deletions(-)
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +85 -12
index 60e4b7e25eff8..9cbd116124109 100644
@@ -142,6 +142,9 @@ int mali_vpu_session_fw_send_set_option(struct mali_vpu_session *sess,
 {
 	int ret;
 
+	if (sess->error)
+		return sess->error;
+
 	ret = mali_vpu_fw_send_set_option(&sess->fw, opt);
 	if (ret)
 		return ret;
@@ -307,6 +310,31 @@ static void mali_vpu_session_buffer_done(struct mali_vpu_session *sess,
 	v4l2_m2m_buf_done(vbuf, state);
 }
 
+static void mali_vpu_session_set_error(struct mali_vpu_session *sess, int error)
+{
+	struct vb2_queue *src_q;
+	struct vb2_queue *dst_q;
+
+	lockdep_assert_held(&sess->vq_lock);
+
+	if (sess->error)
+		return;
+
+	sess->error = error;
+	sess->fw_requested_state = MALI_VPU_FW_STATE_STOPPED;
+	sess->fw_state = MALI_VPU_FW_STATE_STOPPED;
+
+	mali_vpu_sched_session_terminate(&sess->sched_sess, &sess->vpu_dev->sched);
+
+	src_q = v4l2_m2m_get_src_vq(sess->m2m_ctx);
+	dst_q = v4l2_m2m_get_dst_vq(sess->m2m_ctx);
+
+	vb2_queue_error(src_q);
+	vb2_queue_error(dst_q);
+
+	wake_up(&sess->fw_waitq);
+}
+
 static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess,
 					  const struct mali_vpu_fw_msg *fw_msg)
 {
@@ -385,11 +413,13 @@ static int mali_vpu_session_wait_pending(struct mali_vpu_session *sess)
 
 	lockdep_assert_held(&sess->vq_lock);
 
-	while (sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_READY &&
+	while (!sess->error &&
+	       sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_READY &&
 	       sess->fw.pending_response_count) {
 		mutex_unlock(&sess->vq_lock);
 
 		timeout = wait_event_timeout(sess->fw_waitq,
+					     READ_ONCE(sess->error) ||
 					     READ_ONCE(sess->fw_bin_state) !=
 					     MALI_VPU_SESSION_FW_BIN_READY ||
 					     !READ_ONCE(sess->fw.pending_response_count),
@@ -397,11 +427,14 @@ static int mali_vpu_session_wait_pending(struct mali_vpu_session *sess)
 
 		mutex_lock(&sess->vq_lock);
 
+		if (sess->error)
+			return sess->error;
+
 		if (!timeout && sess->fw.pending_response_count)
-			return -ETIMEDOUT;
+			mali_vpu_session_set_error(sess, -ETIMEDOUT);
 	}
 
-	return 0;
+	return sess->error;
 }
 
 static int mali_vpu_session_fw_init(struct mali_vpu_session *sess)
@@ -518,6 +551,7 @@ int mali_vpu_session_init(struct mali_vpu_session *sess)
 		goto err_mmu_cleanup;
 	}
 
+	sess->error = 0;
 	sess->fw_bin = NULL;
 	sess->port_src.stream_requested = false;
 	sess->port_dst.stream_requested = false;
@@ -566,6 +600,9 @@ int mali_vpu_session_qbuf(struct mali_vpu_session *sess, struct vb2_buffer *vb2_
 			mali_vpu_session_get_port(sess, vb2_buf->type);
 	int ret;
 
+	if (sess->error)
+		return sess->error;
+
 	if (!port->stream_requested ||
 	    !mali_vpu_session_can_stream(sess) ||
 	    sess->fw_state != MALI_VPU_FW_STATE_RUNNING) {
@@ -674,6 +711,9 @@ int mali_vpu_session_prepare_streaming(struct mali_vpu_session *sess,
 
 	lockdep_assert_held(&sess->vq_lock);
 
+	if (sess->error)
+		return sess->error;
+
 	if (port->stream_requested)
 		return 0;
 
@@ -687,8 +727,10 @@ int mali_vpu_session_prepare_streaming(struct mali_vpu_session *sess,
 	if (sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_READY &&
 	    mali_vpu_session_can_stream(sess)) {
 		ret = mali_vpu_session_fw_init(sess);
-		if (ret)
+		if (ret) {
+			mali_vpu_session_set_error(sess, ret);
 			goto err_rollback;
+		}
 	}
 
 	return 0;
@@ -711,18 +753,31 @@ int mali_vpu_session_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type
 
 	port->stream_requested = false;
 
+	if (sess->error)
+		return 0;
+
 	if (sess->fw_bin_state != MALI_VPU_SESSION_FW_BIN_READY ||
 	    !sess->sched_sess.lsid)
 		return 0;
 
-	if (mali_vpu_is_raw_q(sess, type))
-		return mali_vpu_session_wait_pending(sess);
+	if (mali_vpu_is_raw_q(sess, type)) {
+		ret = mali_vpu_session_wait_pending(sess);
+		if (ret)
+			goto err_session;
+
+		return 0;
+	}
 
 	ret = mali_vpu_session_fw_stop(sess);
 	if (ret)
-		return ret;
+		goto err_session;
 
 	return mali_vpu_session_wait_pending(sess);
+
+err_session:
+	mali_vpu_session_set_error(sess, ret);
+
+	return ret;
 }
 
 static void mali_vpu_session_fw_bin_ready(struct mali_vpu_fw_bin *vpu_fw_bin,
@@ -739,13 +794,15 @@ static void mali_vpu_session_fw_bin_ready(struct mali_vpu_fw_bin *vpu_fw_bin,
 	 * A synchronous cache hit precedes marking the port requested.
 	 * An asynchronous callback acquires vq_lock after preparation.
 	 */
-	if (!same_thread && mali_vpu_both_ports_unrequested(sess)) {
+	if (sess->error ||
+	    (!same_thread && mali_vpu_both_ports_unrequested(sess))) {
 		sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_UNINITIALIZED;
 		goto err_cache_put;
 	}
 
 	if (vpu_fw_bin->load_status) {
 		sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_FAILED;
+		mali_vpu_session_set_error(sess, vpu_fw_bin->load_status);
 		goto err_cache_put;
 	}
 
@@ -758,6 +815,7 @@ static void mali_vpu_session_fw_bin_ready(struct mali_vpu_fw_bin *vpu_fw_bin,
 	ret = mali_vpu_sched_session_prepare(&sess->sched_sess, sess);
 	if (ret) {
 		sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_UNINITIALIZED;
+		mali_vpu_session_set_error(sess, ret);
 		goto err_cache_put;
 	}
 
@@ -767,6 +825,7 @@ static void mali_vpu_session_fw_bin_ready(struct mali_vpu_fw_bin *vpu_fw_bin,
 		dev_err(sess->vpu_dev->dev,
 			"failed to initialise firmware: %d\n", ret);
 		sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_FAILED;
+		mali_vpu_session_set_error(sess, ret);
 		goto err_cache_put;
 	}
 
@@ -782,6 +841,7 @@ static void mali_vpu_session_fw_bin_ready(struct mali_vpu_fw_bin *vpu_fw_bin,
 			dev_err(sess->vpu_dev->dev,
 				"failed to start streaming: %d\n", ret);
 			sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_UNINITIALIZED;
+			mali_vpu_session_set_error(sess, ret);
 			goto err_fw_cleanup;
 		}
 	}
@@ -833,6 +893,9 @@ int mali_vpu_session_request_fw_load(struct mali_vpu_session *sess)
 
 	lockdep_assert_held(&sess->vq_lock);
 
+	if (sess->error)
+		return sess->error;
+
 	if (sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_READY ||
 	    sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_LOADING)
 		return 0;
@@ -849,6 +912,7 @@ int mali_vpu_session_request_fw_load(struct mali_vpu_session *sess)
 	ret = mali_vpu_fw_cache_get(sess->vpu_dev->fw_cache, &fw_key, sess->fw_event);
 	if (ret) {
 		sess->fw_bin_state = MALI_VPU_SESSION_FW_BIN_FAILED;
+		mali_vpu_session_set_error(sess, ret);
 		complete_all(&sess->fw_load_done);
 		return ret;
 	}
@@ -864,31 +928,40 @@ void mali_vpu_session_irq(struct mali_vpu_session *sess)
 
 	mutex_lock(&sess->vq_lock);
 
+	if (sess->error)
+		goto unlock_mutex;
+
 	ret = mali_vpu_fw_handle_rpc(&sess->fw, &flush_mmu);
 	if (ret) {
 		if (flush_mmu)
 			mali_vpu_sched_flush_mmu(&sess->sched_sess, &sess->vpu_dev->sched);
 		mali_vpu_sched_send_irq(&sess->sched_sess, &sess->vpu_dev->sched);
 
-		if (ret < 0)
+		if (ret < 0) {
+			mali_vpu_session_set_error(sess, ret);
 			goto unlock_mutex;
+		}
 	}
 
 	do {
 		ret = mali_vpu_fw_parse_msg(&sess->fw, &msg);
-		if (ret < 0)
+		if (ret < 0) {
+			mali_vpu_session_set_error(sess, ret);
 			goto unlock_mutex;
+		}
 
 		if (!ret)
 			break;
 
 		ret = mali_vpu_session_handle_fw_msg(sess, &msg);
-		if (ret)
+		if (ret < 0) {
+			mali_vpu_session_set_error(sess, ret);
 			goto unlock_mutex;
+		}
 	} while (ret >= 0);
 
-unlock_mutex:
 	wake_up(&sess->fw_waitq);
 
+unlock_mutex:
 	mutex_unlock(&sess->vq_lock);
 }
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +5 -0
index 5cabb7844131b..c6e1add34a8f1 100644
@@ -60,6 +60,9 @@ struct mali_vpu_port {
  * Session cleanup waits for an in-flight callback before releasing the
  * session-owned binary reference and callback event. On timeout, it cancels
  * the pending cache waiter or waits for an already claimed callback.
+ *
+ * Errors follow the session error-handling policy. A FAILED binary state alone
+ * does not imply that another STREAMON can recover the session.
  */
 enum mali_vpu_session_fw_bin_state {
 	MALI_VPU_SESSION_FW_BIN_UNINITIALIZED = 0,
@@ -93,6 +96,7 @@ enum mali_vpu_session_fw_bin_state {
  * @fw_requested_state: Last firmware state requested by the driver.
  * @sched_sess: Scheduler state used to map this session to an LSID.
  * @ncores: Number of cores assigned to this session by scheduler.
+ * @error: Fatal session error code.
  */
 struct mali_vpu_session {
 	struct mali_vpu_dev		*vpu_dev;
@@ -122,6 +126,7 @@ struct mali_vpu_session {
 
 	struct mali_vpu_sched_session	sched_sess;
 	u32				ncores;
+	int				error;
 };
 
 int mali_vpu_session_init(struct mali_vpu_session *sess);
Signature · 1 line
2.43.0
#51 lore raw

Re: [RFC PATCH 22/28] media: mali-vpu: handle fatal firmware error

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] media: mali-vpu: use-after-free in mali_vpu_fw_clear_buffers() on error - [High] media: mali-vpu…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] media: mali-vpu: use-after-free in mali_vpu_fw_clear_buffers() on error
- [High] media: mali-vpu: re-entrancy race in mali_vpu_session_fw_init()
- [Medium] media: mali-vpu: unrejected buffer ops on failed session --

commit c9e2be7008da2e65bfc676db9f65394d1e577986
Author: Rath Sankaralingam <[email protected]>

media: mali-vpu: handle fatal firmware error

This commit records the first fatal error on a session, terminates the scheduled session, marks the VB2 queues in error, and rejects subsequent session operations with the recorded error code.

Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +0 -0
index 60e4b7e25eff8..9cbd116124109 100644

[ ... ]

Quoted 18 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +14 -0
@@ -307,6 +310,31 @@ static void mali_vpu_session_buffer_done(struct mali_vpu_session *sess,
 	v4l2_m2m_buf_done(vbuf, state);
 }
 
+static void mali_vpu_session_set_error(struct mali_vpu_session *sess, int error)
+{
+	struct vb2_queue *src_q;
+	struct vb2_queue *dst_q;
+
+	lockdep_assert_held(&sess->vq_lock);
+
+	if (sess->error)
+		return;
+
+	sess->error = error;
+	sess->fw_requested_state = MALI_VPU_FW_STATE_STOPPED;
+	sess->fw_state = MALI_VPU_FW_STATE_STOPPED;
+

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c static void mali_vpu_session_buffer_done(struct mali_vpu_session *sess, · line 336

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +10 -0
+	mali_vpu_sched_session_terminate(&sess->sched_sess, &sess->vpu_dev->sched);
+
+	src_q = v4l2_m2m_get_src_vq(sess->m2m_ctx);
+	dst_q = v4l2_m2m_get_dst_vq(sess->m2m_ctx);
+
+	vb2_queue_error(src_q);
+	vb2_queue_error(dst_q);
+
+	wake_up(&sess->fw_waitq);
+}

[Severity: High]
Can terminating a session with in-flight buffers cause a use-after-free in mali_vpu_fw_clear_buffers() when the session is later closed?

Buffers submitted to firmware are recorded in sess->fw.inflight_buffers and are normally removed when firmware returns them, or during final session teardown in mali_vpu_fw_cleanup() -> mali_vpu_fw_clear_buffers(). When a fatal error occurs with buffers in flight, mali_vpu_session_set_error() terminates the LSID and prevents further interrupt handling, so firmware never returns those buffers.

If userspace then calls VIDIOC_STREAMOFF followed by VIDIOC_REQBUFS(0) or VIDIOC_REMOVE_BUFS before closing the fd, mali_vpu_vb2_stop_streaming() completes all active m2m buffers back to VB2 and __vb2_queue_free() frees the struct mali_vpu_buffer allocations without removing them from sess->fw.inflight_buffers. Closing the file descriptor then dereferences the freed buffers:

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c:mali_vpu_fw_clear_buffers() {
	xa_for_each(&fw->inflight_buffers, id, vpu_buf) {
		xa_erase(&fw->inflight_buffers, id);
		vpu_buf->fw_buffer_id = 0;
		vpu_buf->attr = MALI_VPU_BUF_ATTR_NONE;
	}
}

Untracking any in-flight buffer in mali_vpu_vb2_buffer_cleanup() (or clearing sess->fw.inflight_buffers when terminating the session on error) would avoid leaving dangling pointers in the XArray.

Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +0 -1
@@ -385,11 +413,13 @@ static int mali_vpu_session_wait_pending(struct mali_vpu_session *sess)
 
 	lockdep_assert_held(&sess->vq_lock);
 
-	while (sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_READY &&

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c static int mali_vpu_session_wait_pending(struct mali_vpu_session *sess) · line 425

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +3 -0
+	while (!sess->error &&
+	       sess->fw_bin_state == MALI_VPU_SESSION_FW_BIN_READY &&
 	       sess->fw.pending_response_count) {
 		mutex_unlock(&sess->vq_lock);
 
 		timeout = wait_event_timeout(sess->fw_waitq,
+					     READ_ONCE(sess->error) ||
 					     READ_ONCE(sess->fw_bin_state) !=
 					     MALI_VPU_SESSION_FW_BIN_READY ||
 					     !READ_ONCE(sess->fw.pending_response_count),

[Severity: High]
Does dropping sess->vq_lock in mali_vpu_session_wait_pending() allow concurrent re-entry into mali_vpu_session_fw_init()?

Both mali_vpu_session_prepare_streaming() and mali_vpu_session_fw_init() detect an in-progress firmware startup by checking whether sess->fw_requested_state != sess->fw_state. However, mali_vpu_session_fw_init() only updates sess->fw_requested_state to MALI_VPU_FW_STATE_RUNNING in mali_vpu_session_fw_send_state() after mali_vpu_apply_staged_controls() and mali_vpu_session_wait_pending() complete.

When staged controls are present, mali_vpu_session_wait_pending() drops sess->vq_lock while both sess->fw_state and sess->fw_requested_state are still MALI_VPU_FW_STATE_STOPPED and sess->fw_bin_state is MALI_VPU_SESSION_FW_BIN_READY:

CPU0 CPU1
mali_vpu_session_fw_bin_ready()
sess->fw_bin_state = ..._READY
mali_vpu_session_fw_init()

    mali_vpu_session_fw_send_job()
    mali_vpu_apply_staged_controls()
    mali_vpu_session_wait_pending()
      mutex_unlock(&sess->vq_lock)
                                      VIDIOC_STREAMON(CAPTURE)
                                      mutex_lock(&sess->vq_lock)
                                      mali_vpu_session_prepare_streaming()
                                        mali_vpu_session_fw_init()
                                          mali_vpu_session_fw_send_job()
                                          mali_vpu_session_wait_pending()
                                            mutex_unlock(&sess->vq_lock)
      /* wait fails or times out */
      mutex_lock(&sess->vq_lock)
  mali_vpu_fw_cleanup(&sess->fw)
  mutex_unlock(&sess->vq_lock)
                                            mutex_lock(&sess->vq_lock)
                                          mali_vpu_session_fw_send_state()
                                            <-- use-after-cleanup of sess->fw

In addition, because sess->vq_lock is the VB2 queue lock, dropping it inside mali_vpu_vb2_prepare_streaming() before vb2_core_streamon() sets q->streaming = 1 allows a second VIDIOC_STREAMON on the same queue to see port->stream_requested == true, return 0 from mali_vpu_session_prepare_streaming(), and run vb2_start_streaming() twice on the same queue.

[ ... ]

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c int mali_vpu_session_qbuf(struct mali_vpu_session *sess, struct vb2_buffer *vb2_ · line 608

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +3 -0
@@ -566,6 +600,9 @@ int mali_vpu_session_qbuf(struct mali_vpu_session *sess, struct vb2_buffer *vb2_
 			mali_vpu_session_get_port(sess, vb2_buf->type);
 	int ret;
 
+	if (sess->error)
+		return sess->error;
+
 	if (!port->stream_requested ||
 	    !mali_vpu_session_can_stream(sess) ||
 	    sess->fw_state != MALI_VPU_FW_STATE_RUNNING) {

[Severity: Medium]
Does checking sess->error in mali_vpu_session_qbuf() reject buffer operations with the recorded error as described in the commit message?

mali_vpu_session_qbuf() is only called from mali_vpu_vb2_buffer_queue(), which has a void return type in struct vb2_ops and is only invoked by VB2 while the queue is already streaming and q->error is 0. If VIDIOC_STREAMOFF is called after a session error, __vb2_queue_cancel() clears q->error and q->start_streaming_called; subsequent VIDIOC_QBUF and VIDIOC_PREPARE_BUF calls only invoke mali_vpu_vb2_buffer_prepare(), which does not check sess->error and therefore succeeds with 0. Checking sess->error in mali_vpu_vb2_buffer_prepare() would reject those buffer operations with the recorded error.

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

RFC PATCH 23/28 media: mali-vpu: propagate buffer metadata

In reply to Charvi Mehta

Firmware may return compressed buffers before producing their corresponding decoded frames and may reorder decoded output. Preserve timestamp-associated flags and timecodes across firmware processing and restore them on the matching CAPTURE buffers.

Report the negotiated field on CAPTURE buffers and maintain independent sequence counters for the OUTPUT and CAPTURE queues.

---
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 74 +++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-session.h  | 21 ++++++
 .../platform/arm/mali-vpu/mali-vpu-vb2.c      |  3 +
 3 files changed, 98 insertions(+)
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +74 -0
index 9cbd116124109..ea80aacd930d8 100644
@@ -3,6 +3,7 @@
  * Copyright (C) 2026 ARM Limited.
  */
 
+#include <linux/array_size.h>
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/errno.h>
@@ -19,6 +20,9 @@
 #include "mali-vpu-variant.h"
 #include "mali-vpu-vb2.h"
 
+#define MALI_VPU_TS_METADATA_FLAGS \
+	(V4L2_BUF_FLAG_TIMECODE | V4L2_BUF_FLAG_TSTAMP_SRC_MASK)
+
 static const unsigned int mali_vpu_fw_response_timeout_ms = 10000;
 static const unsigned int mali_vpu_fw_load_timeout_ms     = 10000;
 
@@ -288,12 +292,65 @@ static int mali_vpu_session_sched_activate(struct mali_vpu_session *sess)
 	return mali_vpu_sched_switch_in(&sess->sched_sess, &sess->vpu_dev->sched);
 }
 
+static void mali_vpu_session_store_ts_metadata(struct mali_vpu_session *sess,
+					       const struct vb2_v4l2_buffer *vbuf)
+{
+	struct mali_vpu_ts_metadata *metadata;
+
+	if (sess->ts_metadata_idx >= ARRAY_SIZE(sess->ts_metadata))
+		sess->ts_metadata_idx = 0;
+
+	metadata = &sess->ts_metadata[sess->ts_metadata_idx++];
+	metadata->timestamp = vbuf->vb2_buf.timestamp;
+	metadata->flags = vbuf->flags & MALI_VPU_TS_METADATA_FLAGS;
+	metadata->valid = true;
+	metadata->timecode = vbuf->timecode;
+}
+
+static void mali_vpu_session_restore_ts_metadata(struct mali_vpu_session *sess,
+						 struct vb2_v4l2_buffer *vbuf)
+{
+	u64 timestamp = vbuf->vb2_buf.timestamp;
+	u32 i;
+
+	/*
+	 * Matching the auxiliary metadata by timestamp is ambiguous when
+	 * OUTPUT buffers have duplicate timestamps: timecode and timestamp-
+	 * source flags may be restored from a different OUTPUT buffer.
+	 *
+	 * The timestamp itself is returned independently through firmware.
+	 * Unique association tags require reliable metadata lifetime tracking.
+	 */
+
+	vbuf->flags &= ~MALI_VPU_TS_METADATA_FLAGS;
+
+	for (i = 0; i < ARRAY_SIZE(sess->ts_metadata); i++) {
+		struct mali_vpu_ts_metadata *metadata = &sess->ts_metadata[i];
+
+		if (!metadata->valid || metadata->timestamp != timestamp)
+			continue;
+
+		vbuf->flags |= metadata->flags;
+		if (metadata->flags & V4L2_BUF_FLAG_TIMECODE)
+			vbuf->timecode = metadata->timecode;
+
+		return;
+	}
+}
+
+void mali_vpu_session_reset_ts_metadata(struct mali_vpu_session *sess)
+{
+	memset(sess->ts_metadata, 0, sizeof(sess->ts_metadata));
+	sess->ts_metadata_idx = 0;
+}
+
 static void mali_vpu_session_buffer_done(struct mali_vpu_session *sess,
 					 struct mali_vpu_buffer *vpu_buf,
 					 enum vb2_buffer_state state)
 {
 	struct vb2_v4l2_buffer *vbuf = &vpu_buf->m2m_buf.vb;
 	struct vb2_buffer *vb2_buf = &vbuf->vb2_buf;
+	struct mali_vpu_port *port;
 
 	if (vb2_buf->state != VB2_BUF_STATE_ACTIVE) {
 		dev_dbg(sess->vpu_dev->dev,
@@ -302,6 +359,14 @@ static void mali_vpu_session_buffer_done(struct mali_vpu_session *sess,
 		return;
 	}
 
+	port = V4L2_TYPE_IS_OUTPUT(vpu_buf->type) ? &sess->port_src : &sess->port_dst;
+
+	vbuf->sequence = port->sequence++;
+	if (V4L2_TYPE_IS_CAPTURE(vpu_buf->type)) {
+		vbuf->field = port->curr_fmt.fmt.pix_mp.field;
+		mali_vpu_session_restore_ts_metadata(sess, vbuf);
+	}
+
 	if (V4L2_TYPE_IS_OUTPUT(vpu_buf->type))
 		v4l2_m2m_src_buf_remove_by_buf(sess->m2m_ctx, vbuf);
 	else
@@ -607,6 +672,10 @@ int mali_vpu_session_qbuf(struct mali_vpu_session *sess, struct vb2_buffer *vb2_
 	    !mali_vpu_session_can_stream(sess) ||
 	    sess->fw_state != MALI_VPU_FW_STATE_RUNNING) {
 		vpu_buf->attr = MALI_VPU_BUF_ATTR_DEFERRED;
+
+		if (V4L2_TYPE_IS_OUTPUT(vb2_buf->type))
+			mali_vpu_session_store_ts_metadata(sess, v4l2_buf);
+
 		return 0;
 	}
 
@@ -616,6 +685,9 @@ int mali_vpu_session_qbuf(struct mali_vpu_session *sess, struct vb2_buffer *vb2_
 
 	vpu_buf->attr = MALI_VPU_BUF_ATTR_QUEUED;
 
+	if (V4L2_TYPE_IS_OUTPUT(vb2_buf->type))
+		mali_vpu_session_store_ts_metadata(sess, v4l2_buf);
+
 	mali_vpu_sched_send_irq(&sess->sched_sess, &sess->vpu_dev->sched);
 
 	return 0;
@@ -733,6 +805,8 @@ int mali_vpu_session_prepare_streaming(struct mali_vpu_session *sess,
 		}
 	}
 
+	port->sequence = 0;
+
 	return 0;
 
 err_rollback:
drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +21 -0
index c6e1add34a8f1..7565a680397a8 100644
@@ -29,11 +29,27 @@ struct mali_vpu_sched_session;
  * @supported_fmts: List of format descriptors supported by the queue.
  * @stream_requested: Port/Queue is being prepared for streaming or has
  *                    successfully completed preparation. Protected by vq_lock.
+ * @sequence: Sequence number assigned to the next completed buffer.
  */
 struct mali_vpu_port {
 	struct v4l2_format		curr_fmt;
 	struct mali_vpu_format_list	supported_fmts;
 	bool				stream_requested;
+	u32				sequence;
+};
+
+/**
+ * struct mali_vpu_ts_metadata - OUTPUT metadata propagated to CAPTURE.
+ * @timestamp: Timestamp used as the firmware association tag.
+ * @flags: Timestamp-source and timecode-presence flags.
+ * @valid: Whether this cache entry has been populated.
+ * @timecode: Timecode associated with @timestamp.
+ */
+struct mali_vpu_ts_metadata {
+	u64 timestamp;
+	u32 flags;
+	bool valid;
+	struct v4l2_timecode timecode;
 };
 
 /*
@@ -80,6 +96,8 @@ enum mali_vpu_session_fw_bin_state {
  * @mmu: MMU handle for this session
  * @vq_lock: Mutex lock to serialise per-session queue related ioctls.
  * @min_buffers_required: Driver-reported minimum buffers for role-specific queue.
+ * @ts_metadata: OUTPUT metadata indexed by timestamp for CAPTURE propagation.
+ * @ts_metadata_idx: Next timestamp metadata slot to use.
  * @ctrls_caps: Variant-provided immutable control capability table.
  * @num_ctrls_caps: Number of entries in @ctrls_caps.
  * @ctrls_state: Per-session control cache and staging state.
@@ -106,6 +124,8 @@ struct mali_vpu_session {
 	struct mali_vpu_mmu		mmu;
 	struct mutex			vq_lock; /* lock to serialise per-session ioctls */
 	u32				min_buffers_required;
+	struct mali_vpu_ts_metadata	ts_metadata[VIDEO_MAX_FRAME];
+	u32				ts_metadata_idx;
 
 	const struct mali_vpu_ctrl_desc *ctrls_caps;
 	u32				num_ctrls_caps;
@@ -139,6 +159,7 @@ int mali_vpu_session_prepare_streaming(struct mali_vpu_session *sess,
 int mali_vpu_session_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type type);
 void mali_vpu_session_irq(struct mali_vpu_session *sess);
 int mali_vpu_session_qbuf(struct mali_vpu_session *sess, struct vb2_buffer *vb2_buf);
+void mali_vpu_session_reset_ts_metadata(struct mali_vpu_session *sess);
 
 static inline struct mali_vpu_session *mali_vpu_get_session(struct file *filp)
 {
drivers/media/platform/arm/mali-vpu/mali-vpu-vb2.c +3 -0
index 41158bf4e0ecb..81a447ceeb755 100644
@@ -137,6 +137,9 @@ static void mali_vpu_vb2_unprepare_streaming(struct vb2_queue *q)
 
 	if (!vb2_start_streaming_called(q))
 		v4l2_m2m_update_stop_streaming_state(sess->m2m_ctx, q);
+
+	if (!sess->port_src.stream_requested && !sess->port_dst.stream_requested)
+		mali_vpu_session_reset_ts_metadata(sess);
 }
 
 static const struct vb2_ops mali_vpu_vb2_ops = {
Signature · 1 line
2.43.0
#53 lore raw

RFC PATCH 24/28 media: mali-vpu: add decoder EOS and DRC handling

In reply to Charvi Mehta

From: Shobhit Pandey <[email protected]>

Add decoder START/STOP command support and propagate EOS through the firmware protocol. Handle sequence and allocation parameters, queue flush responses, dynamic resolution changes, and V4L2 EOS/ source-change events.
Track capture reconfiguration state and prevent stale pre-DRC buffers from being submitted back to firmware.

---
 .../platform/arm/mali-vpu/mali-vpu-dec.c      |  29 ++++
 .../platform/arm/mali-vpu/mali-vpu-dec.h      |   4 +
 .../platform/arm/mali-vpu/mali-vpu-firmware.c | 102 ++++++++++-
 .../platform/arm/mali-vpu/mali-vpu-firmware.h |  38 +++++
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   |  64 +++++++
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 158 +++++++++++++++++-
 .../platform/arm/mali-vpu/mali-vpu-session.h  |   3 +
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   |   3 +
 8 files changed, 390 insertions(+), 11 deletions(-)
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c +29 -0
index 51d509abb9dd1..7e34c24ae050f 100644
@@ -23,6 +23,7 @@ void mali_vpu_session_init_dec(struct mali_vpu_session *sess)
 	sess->num_ctrls_caps	= variant_ctrls->num_dec_ctrls;
 
 	sess->min_buffers_required = 3;	/* For CAPTURE/dst queue */
+	sess->drc_pending	= false;
 }
 
 int mali_vpu_ctrl_set_ignore_stream_headers(struct mali_vpu_session *sess,
@@ -64,3 +65,31 @@ int mali_vpu_ctrl_set_frame_reordering(struct mali_vpu_session *sess, u32 cid,
 
 	return mali_vpu_session_fw_send_set_option(sess, &option);
 }
+
+int mali_vpu_decoder_cmd(struct file *filp, void *priv, struct v4l2_decoder_cmd *cmd)
+{
+	struct mali_vpu_session *sess = mali_vpu_get_session(filp);
+	int ret;
+
+	ret = v4l2_m2m_ioctl_try_decoder_cmd(filp, priv, cmd);
+	if (ret)
+		return ret;
+
+	mutex_lock(&sess->vq_lock);
+
+	switch (cmd->cmd) {
+	case V4L2_DEC_CMD_STOP:
+		ret = mali_vpu_session_fw_send_eos(sess);
+		break;
+	case V4L2_DEC_CMD_START:
+		vb2_clear_last_buffer_dequeued(v4l2_m2m_get_dst_vq(sess->m2m_ctx));
+		ret = 0;
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+
+	mutex_unlock(&sess->vq_lock);
+	return ret;
+}
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.h +4 -0
index e323b38475c81..a108aaaa4a959 100644
@@ -8,7 +8,9 @@
 
 #include <linux/types.h>
 
+struct file;
 struct mali_vpu_session;
+struct v4l2_decoder_cmd;
 
 void mali_vpu_session_init_dec(struct mali_vpu_session *sess);
 
@@ -16,4 +18,6 @@ void mali_vpu_session_init_dec(struct mali_vpu_session *sess);
 int mali_vpu_ctrl_set_ignore_stream_headers(struct mali_vpu_session *sess, u32 cid, s64 value);
 int mali_vpu_ctrl_set_frame_reordering(struct mali_vpu_session *sess, u32 cid, s64 value);
 
+int mali_vpu_decoder_cmd(struct file *filp, void *priv, struct v4l2_decoder_cmd *cmd);
+
 #endif /* __MALI_VPU_DEC_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +93 -9

136 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.h +38 -0
index c936730698837..ca90cc71ac64e 100644
@@ -84,6 +84,29 @@ struct mali_vpu_fw_set_option {
 	};
 };
 
+/**
+ * struct mali_vpu_fw_seq_param - Sequence parameters.
+ * @planar: Planar output-buffer requirements.
+ * @planar.buffers_min:	Minimum number of planar buffers required.
+ */
+struct mali_vpu_fw_seq_param {
+	struct {
+		u32 buffers_min;
+	} planar;
+};
+
+/**
+ * struct mali_vpu_fw_alloc_param - Allocation parameters
+ * @height: in pixels
+ * @width: in pixels
+ *
+ * Dimensions of the decoded frame buffer
+ */
+struct mali_vpu_fw_alloc_param {
+	u32 height;
+	u32 width;
+};
+
 /**
  * struct mali_vpu_fw_error - Firmware error message.
  * @error_code: What kind of error that was reported.
@@ -116,6 +139,11 @@ struct mali_vpu_fw_error {
  * @MALI_VPU_FW_CODE_STATE_CHANGE: Firmware state-change request or response.
  * @MALI_VPU_FW_CODE_SWITCH_IN: Firmware was booted on a Core in a session.
  * @MALI_VPU_FW_CODE_SWITCH_OUT: The last core in a session was switched out.
+ * @MALI_VPU_FW_CODE_EOS: Firmware returned an end-of-stream marker.
+ * @MALI_VPU_FW_CODE_INPUT_FLUSH: Firmware successfully completed a OUTPUT queue flush.
+ * @MALI_VPU_FW_CODE_OUTPUT_FLUSH: Firmware successfully completed a CAPTURE queue flush.
+ * @MALI_VPU_FW_CODE_SEQUENCE_PARAMS: Firmware reported new sequence parameters.
+ * @MALI_VPU_FW_CODE_ALLOC_PARAM: Firmware reported new frame allocation parameters.
  * @MALI_VPU_FW_CODE_UNKNOWN: Unrecognized firmware message.
  * @MALI_VPU_FW_CODE_MAX: Sentinel marking the end of the message-code enum.
  */
@@ -131,6 +159,11 @@ enum mali_vpu_fw_code {
 	MALI_VPU_FW_CODE_STATE_CHANGE,       /* Driver <-> Firmware. */
 	MALI_VPU_FW_CODE_SWITCH_IN,          /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_SWITCH_OUT,         /* Driver <-> Firmware. */
+	MALI_VPU_FW_CODE_EOS,                /* Driver <- Firmware. */
+	MALI_VPU_FW_CODE_INPUT_FLUSH,        /* Driver <- Firmware. */
+	MALI_VPU_FW_CODE_OUTPUT_FLUSH,       /* Driver <- Firmware. */
+	MALI_VPU_FW_CODE_SEQUENCE_PARAMS,    /* Driver <- Firmware. */
+	MALI_VPU_FW_CODE_ALLOC_PARAM,        /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_UNKNOWN,            /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_MAX
 };
@@ -147,6 +180,8 @@ struct mali_vpu_fw_msg {
 		struct mali_vpu_fw_job job;
 		struct mali_vpu_fw_error error;
 		struct mali_vpu_buffer *vpu_buf;
+		struct mali_vpu_fw_alloc_param alloc_params;
+		struct mali_vpu_fw_seq_param seq_params;
 	} data;
 };
 
@@ -260,6 +295,9 @@ int mali_vpu_fw_send_buffer(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *fw_m
 int mali_vpu_fw_send_ping(struct mali_vpu_fw *fw);
 int mali_vpu_fw_send_state_change(struct mali_vpu_fw *fw, enum mali_vpu_fw_state state);
 int mali_vpu_fw_send_set_option(struct mali_vpu_fw *fw, struct mali_vpu_fw_set_option *option);
+int mali_vpu_fw_send_eos(struct mali_vpu_fw *fw);
+int mali_vpu_fw_send_flush_src(struct mali_vpu_fw *fw);
+int mali_vpu_fw_send_flush_dst(struct mali_vpu_fw *fw);
 int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg);
 int mali_vpu_fw_get_region(enum mali_vpu_fw_region region, mali_vpu_mmu_va *begin,
 			   mali_vpu_mmu_va *end);
drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h +64 -0

88 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +156 -2

224 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +3 -0
index 7565a680397a8..45d0b6710aced 100644
@@ -115,6 +115,7 @@ enum mali_vpu_session_fw_bin_state {
  * @sched_sess: Scheduler state used to map this session to an LSID.
  * @ncores: Number of cores assigned to this session by scheduler.
  * @error: Fatal session error code.
+ * @drc_pending: Dynamic resolution change is waiting for CAPTURE reconfigure.
  */
 struct mali_vpu_session {
 	struct mali_vpu_dev		*vpu_dev;
@@ -147,6 +148,7 @@ struct mali_vpu_session {
 	struct mali_vpu_sched_session	sched_sess;
 	u32				ncores;
 	int				error;
+	bool				drc_pending;
 };
 
 int mali_vpu_session_init(struct mali_vpu_session *sess);
@@ -160,6 +162,7 @@ int mali_vpu_session_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type
 void mali_vpu_session_irq(struct mali_vpu_session *sess);
 int mali_vpu_session_qbuf(struct mali_vpu_session *sess, struct vb2_buffer *vb2_buf);
 void mali_vpu_session_reset_ts_metadata(struct mali_vpu_session *sess);
+int mali_vpu_session_fw_send_eos(struct mali_vpu_session *sess);
 
 static inline struct mali_vpu_session *mali_vpu_get_session(struct file *filp)
 {
drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c +3 -0
index 0fcd3835e52d3..f8d93c849a34f 100644
@@ -15,6 +15,7 @@
 #include <media/v4l2-ioctl.h>
 #include <media/v4l2-mem2mem.h>
 
+#include "mali-vpu-dec.h"
 #include "mali-vpu-formats.h"
 #include "mali-vpu-session.h"
 #include "mali-vpu-variant.h"
@@ -359,4 +360,6 @@ const struct v4l2_ioctl_ops mali_vpu_ioctl_ops_dec = {
 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
 	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
+	.vidioc_decoder_cmd		= mali_vpu_decoder_cmd,
+	.vidioc_try_decoder_cmd		= v4l2_m2m_ioctl_try_decoder_cmd,
 };
Signature · 1 line
2.43.0
#54 lore raw

Re: [RFC PATCH 24/28] media: mali-vpu: add decoder EOS and DRC handling

Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] media: mali-vpu: broken EOS state handling in mali_vpu_session_fw_send_eos() - [High] media…

Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [High] media: mali-vpu: broken EOS state handling in mali_vpu_session_fw_send_eos()
- [High] media: mali-vpu: pre-DRC buffer requeue fails in mali_vpu_vb2_buffer_prepare()
- [High] media: mali-vpu: spurious V4L2_EVENT_EOS in mali_vpu_session_buffer_done()
- [High] media: mali-vpu: premature DRC arming and missing reset on V4L2_DEC_CMD_START
- [High] media: mali-vpu: use-after-free of in-flight buffers after streamoff
- [High] media: mali-vpu: missing MMU TLB flush when unmapping VB2 buffers during DRC
--

commit ce97dbb7890d62bd32d99505c6b3bd32d36ac969
Author: Shobhit Pandey <[email protected]>

media: mali-vpu: add decoder EOS and DRC handling

This commit adds V4L2 decoder START/STOP command handling, firmware queue flushing, end-of-stream propagation, and dynamic resolution change support to the Mali VPU driver.

Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +0 -0
index ea80aacd930d8..fcfad581223be 100644

[ ... ]

Quoted 11 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +7 -0
@@ -157,6 +166,67 @@ int mali_vpu_session_fw_send_set_option(struct mali_vpu_session *sess,
 	return 0;
 }
 
+int mali_vpu_session_fw_send_eos(struct mali_vpu_session *sess)
+{
+	int ret;
+
+	if (sess->error)
+		return sess->error;
+

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c int mali_vpu_session_fw_send_set_option(struct mali_vpu_session *sess, · line 185

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +10 -0
+	if (!mali_vpu_both_queues_streaming(sess))
+		return 0;
+
+	ret = mali_vpu_fw_send_eos(&sess->fw);
+	if (ret)
+		return ret;
+
+	mali_vpu_sched_send_irq(&sess->sched_sess, &sess->vpu_dev->sched);
+	return 0;
+}

[Severity: High]
Can mali_vpu_session_fw_send_eos() send the EOS marker ahead of deferred OUTPUT buffers or fail before the firmware reaches MALI_VPU_FW_STATE_RUNNING?

When VIDIOC_STREAMON starts the queues, firmware loading and the transition to MALI_VPU_FW_STATE_RUNNING complete asynchronously. During that window, mali_vpu_session_qbuf() holds queued OUTPUT buffers with MALI_VPU_BUF_ATTR_DEFERRED until MALI_VPU_FW_CODE_STATE_CHANGE calls mali_vpu_submit_deferred_bufs(). Because mali_vpu_session_fw_send_eos() only checks mali_vpu_both_queues_streaming(), calling VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) right after VIDIOC_STREAMON fails with -EINVAL if sess->fw_bin_state is still MALI_VPU_SESSION_FW_BIN_LOADING, or writes the EOS packet into sess->fw.buf_in_host ahead of the deferred OUTPUT buffers if sess->fw_state is still MALI_VPU_FW_STATE_STOPPED.

Also, if V4L2_DEC_CMD_STOP is issued while only the OUTPUT queue is streaming (for example, while waiting for the initial V4L2_EVENT_SOURCE_CHANGE or while CAPTURE is stopped during DRC), mali_vpu_both_queues_streaming() is false and the EOS request is dropped without staging it.

Quoted 33 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +33 -0
+
+static int mali_vpu_session_fw_send_flush(struct mali_vpu_session *sess, bool to_src)
+{
+	int ret;
+
+	if (to_src)
+		ret = mali_vpu_fw_send_flush_src(&sess->fw);
+	else
+		ret = mali_vpu_fw_send_flush_dst(&sess->fw);
+
+	if (ret)
+		return ret;
+
+	mali_vpu_sched_send_irq(&sess->sched_sess, &sess->vpu_dev->sched);
+	return 0;
+}
+
+static int mali_vpu_session_send_drc_event(struct mali_vpu_session *sess)
+{
+	const struct v4l2_event event = {
+		.type                 = V4L2_EVENT_SOURCE_CHANGE,
+		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION
+	};
+
+	v4l2_event_queue_fh(&sess->fh, &event);
+	return 0;
+}
+
+static int
+mali_vpu_session_update_capture_format(struct mali_vpu_session *sess,
+				       const struct mali_vpu_fw_alloc_param alloc_params)
+{
+	struct v4l2_pix_format_mplane *src = &sess->port_src.curr_fmt.fmt.pix_mp;

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c int mali_vpu_session_fw_send_set_option(struct mali_vpu_session *sess, · line 228

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +10 -0
+	struct v4l2_pix_format_mplane *dst = &sess->port_dst.curr_fmt.fmt.pix_mp;
+	int ret = 0;
+
+	src->width = alloc_params.width;
+	src->height = alloc_params.height;
+
+	ret = v4l2_fill_pixfmt_mp(dst, dst->pixelformat, alloc_params.width,
+				  alloc_params.height);
+	return ret;
+}

[Severity: High]
Does updating sess->port_dst.curr_fmt in place here cause pre-DRC CAPTURE buffers requeued via VIDIOC_QBUF to fail in mali_vpu_vb2_buffer_prepare() when the resolution increases?

v4l2_fill_pixfmt_mp() immediately overwrites dst->plane_fmt[i].sizeimage with the post-DRC plane sizes. When userspace requeues a pre-DRC CAPTURE buffer before processing V4L2_EVENT_SOURCE_CHANGE, vb2_core_qbuf() runs mali_vpu_vb2_buffer_prepare() before calling mali_vpu_session_qbuf():

drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c:mali_vpu_vb2_buffer_prepare() {
	...
	for (i = 0; i < pix_mp->num_planes; i++) {
		if (vb2_plane_size(vb2_buf, i) < pix_mp->plane_fmt[i].sizeimage)
			return -EINVAL;
	}
	...
}

Whenever the new resolution requires a larger sizeimage, mali_vpu_vb2_buffer_prepare() rejects the pre-DRC buffer with -EINVAL and the sess->drc_pending handling in mali_vpu_session_qbuf() is never reached. Conversely, when the new resolution is smaller or equal, the sess->drc_pending branch in mali_vpu_session_qbuf() completes the buffer with V4L2_BUF_FLAG_LAST without zeroing planes[i].bytesused, returning stale payload sizes from the previous frame.

[ ... ]

Quoted 6 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +2 -0
@@ -373,6 +444,17 @@ static void mali_vpu_session_buffer_done(struct mali_vpu_session *sess,
 		v4l2_m2m_dst_buf_remove_by_buf(sess->m2m_ctx, vbuf);
 
 	v4l2_m2m_buf_done(vbuf, state);
+
+	if (V4L2_TYPE_IS_CAPTURE(vpu_buf->type) &&

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c static void mali_vpu_session_buffer_done(struct mali_vpu_session *sess, · line 458

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +9 -0
+	    !q->last_buffer_dequeued &&
+	    (vbuf->flags & V4L2_BUF_FLAG_LAST) &&
+	    !sess->drc_pending) {
+		const struct v4l2_event event = {
+			.type = V4L2_EVENT_EOS,
+		};
+
+		v4l2_event_queue_fh(&sess->fh, &event);
+	}
 }

[Severity: High]
Does this queue a spurious V4L2_EVENT_EOS for every unused CAPTURE buffer flushed during a normal VIDIOC_STREAMOFF?

When mali_vpu_session_streamoff() flushes the CAPTURE queue while sess->drc_pending is false, the firmware returns all unused in-flight CAPTURE buffers with visible_frame_width == 0 and visible_frame_height == 0. mali_vpu_convert_buffer_frame() zeroes their plane payloads, and mali_vpu_buffer_update_flags_to_v4l2() sets V4L2_BUF_FLAG_LAST on any CAPTURE buffer with bytesused == 0:

drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c:mali_vpu_buffer_update_flags_to_v4l2() {
	...
	if (vpu_buf->flags & MALI_VPU_BUFFER_EOS ||
	    (V4L2_TYPE_IS_CAPTURE(vpu_buf->type) &&
	     mali_vpu_buffer_get_bytesused(&vpu_buf->m2m_buf.vb.vb2_buf) == 0))
		v4l2_buf->flags |= V4L2_BUF_FLAG_LAST;
}

Because q->last_buffer_dequeued is only set when userspace calls VIDIOC_DQBUF, every flushed CAPTURE buffer satisfies this check and queues V4L2_EVENT_EOS on sess->fh.

[ ... ]

Quoted 37 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +33 -0
@@ -464,6 +547,49 @@ static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess,
 	case MALI_VPU_FW_CODE_IDLE:
 		dev_dbg(sess->vpu_dev->dev, "fw entered Idle/sleeping state.\n");
 		return 0;
+	case MALI_VPU_FW_CODE_EOS:
+		/*
+		 * This is driver generated dummy EOS buffer response no action
+		 * required as EOS is handled via buffer flags.
+		 */
+		return 0;
+	case MALI_VPU_FW_CODE_INPUT_FLUSH:
+		/*
+		 * Acknowledge completion of the input flush. No additional session
+		 * state update is required.
+		 */
+		return 0;
+	case MALI_VPU_FW_CODE_OUTPUT_FLUSH:
+		dev_dbg(sess->vpu_dev->dev, "fw rsp: Dst queue flushed\n");
+		if (sess->role == MALI_VPU_DECODER && sess->drc_pending)
+			return mali_vpu_session_send_drc_event(sess);
+
+		return 0;
+	case MALI_VPU_FW_CODE_ALLOC_PARAM:
+		ret = mali_vpu_session_update_capture_format(sess, fw_msg->data.alloc_params);
+		if (!ret)
+			sess->drc_pending = true;
+
+		return ret;
+	case MALI_VPU_FW_CODE_SEQUENCE_PARAMS: {
+		struct vb2_queue *dst_q;
+		u32 buffers_min;
+
+		buffers_min = fw_msg->data.seq_params.planar.buffers_min;
+		dst_q = v4l2_m2m_get_dst_vq(sess->m2m_ctx);
+
+		if (!buffers_min || buffers_min > dst_q->max_num_buffers) {
+			dev_err(sess->vpu_dev->dev,

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess, · line 592

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +10 -0
+				"invalid minimum buffer count %u, max is %u\n",
+				buffers_min, dst_q->max_num_buffers);
+			return -EPROTO;
+		}
+
+		sess->min_buffers_required = buffers_min;
+		dst_q->min_reqbufs_allocation = sess->min_buffers_required;
+
+		return mali_vpu_session_fw_send_flush(sess, false);
+	}

[Severity: High]
What happens if MALI_VPU_FW_CODE_SEQUENCE_PARAMS arrives without a preceding MALI_VPU_FW_CODE_ALLOC_PARAM, or if userspace resumes from DRC using VIDIOC_DECODER_CMD(V4L2_DEC_CMD_START)?

Because sess->drc_pending is only armed in MALI_VPU_FW_CODE_ALLOC_PARAM, a sequence parameter change that only sends MALI_VPU_FW_CODE_SEQUENCE_PARAMS (such as a change in num_buffers_planar) flushes the CAPTURE queue with sess->drc_pending == false; mali_vpu_session_buffer_done() then emits a spurious V4L2_EVENT_EOS on the flushed buffer and MALI_VPU_FW_CODE_OUTPUT_FLUSH never sends V4L2_EVENT_SOURCE_CHANGE.

In addition, sess->drc_pending is only cleared in mali_vpu_session_prepare_streaming(). If userspace keeps the CAPTURE queue streaming across V4L2_EVENT_SOURCE_CHANGE and resumes decoding via VIDIOC_DECODER_CMD(V4L2_DEC_CMD_START) in mali_vpu_decoder_cmd(), sess->drc_pending remains true and mali_vpu_session_qbuf() immediately completes every newly queued CAPTURE buffer with V4L2_BUF_FLAG_LAST instead of submitting it to the firmware. (The later commit "media: mali-vpu: add stateful decoder state machine" in this series replaces sess->drc_pending with sess->sess_state, but still only transitions state on MALI_VPU_FW_CODE_ALLOC_PARAM and rejects V4L2_DEC_CMD_START in MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE.)

[ ... ]

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c int mali_vpu_session_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type · line 986

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +2 -1
@@ -834,7 +979,8 @@ int mali_vpu_session_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type
 	    !sess->sched_sess.lsid)
 		return 0;
 
-	if (mali_vpu_is_raw_q(sess, type)) {
+	if (sess->drc_pending && sess->role == MALI_VPU_DECODER &&
+	    mali_vpu_is_raw_q(sess, type)) {
 		ret = mali_vpu_session_wait_pending(sess);
 		if (ret)
 			goto err_session;

[Severity: High]
Can this early return leave in-flight CAPTURE buffers in sess->fw.inflight_buffers across VIDIOC_STREAMOFF, leading to a use-after-free when the buffers are freed?

MALI_VPU_FW_CODE_ALLOC_PARAM sets sess->drc_pending = true before MALI_VPU_FW_CODE_SEQUENCE_PARAMS validates the parameters and calls mali_vpu_session_fw_send_flush() to increment sess->fw.pending_response_count. If userspace calls VIDIOC_STREAMOFF(CAPTURE) in the window between ALLOC_PARAM and SEQUENCE_PARAMS, mali_vpu_session_wait_pending() sees pending_response_count == 0 and returns immediately without stopping the firmware or flushing CAPTURE. (The same happens if SEQUENCE_PARAMS fails validation and sets sess->error, taking the earlier sess->error return in mali_vpu_session_streamoff().)

mali_vpu_vb2_stop_streaming() then returns the buffers to VB2, and VIDIOC_REQBUFS(0) frees the struct mali_vpu_buffer objects while their pointers remain in sess->fw.inflight_buffers. Any later buffer return in mali_vpu_fw_resolve_returned_buffer() or session teardown in mali_vpu_fw_clear_buffers() then writes to the freed vpu_buf:

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c:mali_vpu_fw_clear_buffers() {
	...
	xa_for_each(&fw->inflight_buffers, id, vpu_buf) {
		xa_erase(&fw->inflight_buffers, id);
		vpu_buf->fw_buffer_id = 0;
		vpu_buf->attr = MALI_VPU_BUF_ATTR_NONE;
	}
}
Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +0 -0
@@ -846,7 +992,15 @@ int mali_vpu_session_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type
 	if (ret)
 		goto err_session;
 

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c int mali_vpu_session_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type · line 1003

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +9 -1
-	return mali_vpu_session_wait_pending(sess);
+	ret = mali_vpu_session_fw_send_flush(sess, V4L2_TYPE_IS_OUTPUT(type));
+	if (ret)
+		goto err_session;
+
+	ret = mali_vpu_session_wait_pending(sess);
+	if (ret)
+		goto err_session;
+
+	return 0;

[Severity: High]
When CAPTURE buffers are unmapped and reallocated during DRC or after VIDIOC_STREAMOFF, does anything flush the VPU LSID MMU TLB before the VPU reuses those virtual addresses?

mali_vpu_session_streamoff() leaves sess->sched_sess.lsid mapped on the hardware. When userspace frees the old CAPTURE buffers with VIDIOC_REQBUFS(0), mali_vpu_vb2_buffer_cleanup() calls mali_vpu_mmu_free_pages() to clear the session L2 page table entries and VB2 returns the physical pages to the kernel, but no MMU TLB flush is issued. When new CAPTURE buffers are then queued, mali_vpu_buffer_plane_map() allocates from MVE_MEM_REGION_FRAMEBUF_ADDR_BEGIN (0x70000000) and reuses the same VPU virtual addresses. Without a call to mali_vpu_sched_flush_mmu() after unmapping the old buffer pages, the VPU can DMA decoded frames through stale TLB entries into freed physical pages.

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

RFC PATCH 25/28 media: mali-vpu: handle firmware buffer parameters

In reply to Charvi Mehta

From: Shobhit Pandey <[email protected]>

Add protocol definitions and parsing for firmware buffer-parameter messages. Recognize colour-description and DPB-held-frame notifications, log and ignore them when no session action is required.

---
 .../platform/arm/mali-vpu/mali-vpu-firmware.c | 32 +++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-firmware.h |  5 +++
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   | 19 +++++++++++
 .../platform/arm/mali-vpu/mali-vpu-session.c  |  9 ++++++
 4 files changed, 65 insertions(+)
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +32 -0
index 55bad502cae0b..34d57b288a682 100644
@@ -8,6 +8,7 @@
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/sizes.h>
+#include <linux/stddef.h>
 #include <linux/string.h>
 
 #include "mali-vpu-firmware.h"
@@ -950,6 +951,33 @@ mali_vpu_convert_buffer_bitstream(struct mali_vpu_fw *fw,
 	return 0;
 }
 
+static int mali_vpu_convert_buffer_param(struct mali_vpu_fw_msg *msg,
+					 struct mve_buffer_param *p,
+					 size_t size)
+{
+	if (size < sizeof(p->type))
+		return -EPROTO;
+
+	switch (p->type) {
+	case MVE_BUFFER_PARAM_TYPE_COLOUR_DESCRIPTION:
+		/* The colour-description payload is currently ignored. */
+		msg->code = MALI_VPU_FW_CODE_COLOR_DESC;
+		break;
+	case MVE_BUFFER_PARAM_TYPE_DPB_HELD_FRAMES:
+		if (size < offsetofend(struct mve_buffer_param, data.arg))
+			return -EPROTO;
+
+		msg->code = MALI_VPU_FW_CODE_DPB_HELD_FRAMES;
+		msg->data.dpb_held_frames = p->data.arg;
+		break;
+	default:
+		msg->code = MALI_VPU_FW_CODE_UNKNOWN;
+		break;
+	}
+
+	return 1;
+}
+
 static int mali_vpu_fw_get_buffer(struct mali_vpu_fw *fw,
 				  struct mali_vpu_fw_proto_page *buf_host_page,
 				  struct mali_vpu_fw_proto_page *buf_mve_page,
@@ -1007,6 +1035,7 @@ int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg)
 		struct mve_response_state_change state_change;
 		struct mve_response_frame_alloc_parameters alloc_params;
 		struct mve_response_sequence_parameters seq_params;
+		struct mve_buffer_param buffer_param;
 		struct mve_response_error error;
 	} fw_msg = {};
 	size_t size = sizeof(fw_msg);
@@ -1137,6 +1166,9 @@ int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg)
 
 		fw->pending_response_count--;
 		break;
+	case MVE_BUFFER_CODE_PARAM:
+		ret =  mali_vpu_convert_buffer_param(msg, &fw_msg.buffer_param, size);
+		break;
 	case MVE_RESPONSE_CODE_ERROR: {
 		size_t message_offset = offsetof(struct mve_response_error, message);
 		size_t message_size;
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.h +5 -0
index ca90cc71ac64e..6c46f23ac8638 100644
@@ -144,6 +144,8 @@ struct mali_vpu_fw_error {
  * @MALI_VPU_FW_CODE_OUTPUT_FLUSH: Firmware successfully completed a CAPTURE queue flush.
  * @MALI_VPU_FW_CODE_SEQUENCE_PARAMS: Firmware reported new sequence parameters.
  * @MALI_VPU_FW_CODE_ALLOC_PARAM: Firmware reported new frame allocation parameters.
+ * @MALI_VPU_FW_CODE_COLOR_DESC: Firmware reported colour-description metadata.
+ * @MALI_VPU_FW_CODE_DPB_HELD_FRAMES: Firmware reported the number of frames held in the DPB.
  * @MALI_VPU_FW_CODE_UNKNOWN: Unrecognized firmware message.
  * @MALI_VPU_FW_CODE_MAX: Sentinel marking the end of the message-code enum.
  */
@@ -164,6 +166,8 @@ enum mali_vpu_fw_code {
 	MALI_VPU_FW_CODE_OUTPUT_FLUSH,       /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_SEQUENCE_PARAMS,    /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_ALLOC_PARAM,        /* Driver <- Firmware. */
+	MALI_VPU_FW_CODE_COLOR_DESC,         /* Driver <- Firmware. */
+	MALI_VPU_FW_CODE_DPB_HELD_FRAMES,    /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_UNKNOWN,            /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_MAX
 };
@@ -182,6 +186,7 @@ struct mali_vpu_fw_msg {
 		struct mali_vpu_buffer *vpu_buf;
 		struct mali_vpu_fw_alloc_param alloc_params;
 		struct mali_vpu_fw_seq_param seq_params;
+		u32 dpb_held_frames;
 	} data;
 };
 
drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h +19 -0
index cb4533a367957..76dc7bf076c8f 100644
@@ -269,6 +269,8 @@ struct mve_msg_header {
 		#define MVE_BUFFER_CODE_FRAME                 (3001)
 		/* struct mve_buffer_bitstream */
 		#define MVE_BUFFER_CODE_BITSTREAM             (3002)
+		/* struct mve_buffer_param */
+		#define MVE_BUFFER_CODE_PARAM                 (3003)
 
 	u16 size; /* size in bytes of trailing data, 0 if none */
 };
@@ -601,4 +603,21 @@ struct mve_buffer_bitstream {
 	u32 reserved;
 };
 
+/* Parameters that are sent in the same communication channels
+ * as the buffers. A parameter applies to all subsequent buffers.
+ * Some types are only valid for decode, and some only for encode.
+ */
+struct mve_buffer_param {
+	u32 type;                                                   /* Extra data: */
+		#define MVE_BUFFER_PARAM_TYPE_COLOUR_DESCRIPTION        (15)
+		/* arg, number of output buffers that are complete and held by firmware in the
+		 * DPB for reordering purposes. Valid after the next frame is output.
+		 */
+		#define MVE_BUFFER_PARAM_TYPE_DPB_HELD_FRAMES           (19)
+
+	union {
+		u32                                        arg;
+	} data;
+};
+
 #endif /* __MALI_VPU_PROTOCOL_DEF_V3_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +9 -0
index fcfad581223be..411421e175e71 100644
@@ -559,6 +559,15 @@ static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess,
 		 * state update is required.
 		 */
 		return 0;
+	case MALI_VPU_FW_CODE_COLOR_DESC:
+		/* Colour metadata is currently not consumed by the driver. */
+		return 0;
+	case MALI_VPU_FW_CODE_DPB_HELD_FRAMES:
+		/* DPB occupancy is informational. */
+		dev_dbg(sess->vpu_dev->dev,
+			"firmware holds %u frames in the DPB\n",
+			fw_msg->data.dpb_held_frames);
+		return 0;
 	case MALI_VPU_FW_CODE_OUTPUT_FLUSH:
 		dev_dbg(sess->vpu_dev->dev, "fw rsp: Dst queue flushed\n");
 		if (sess->role == MALI_VPU_DECODER && sess->drc_pending)
Signature · 1 line
2.43.0
#56 lore raw

RFC PATCH 26/28 media: mali-vpu: handle firmware event responses

In reply to Charvi Mehta

Add protocol definitions needed to parse firmware event responses. Report corrupt and unsupported streams, and propagate unsupported-stream events to the session as -EOPNOTSUPP. Ignore informational events that require no session action.

Also retain sequence bit-depth and interlace information, and reject interlaced or non-8-bit streams that aren't supported by the driver yet.

---
 .../platform/arm/mali-vpu/mali-vpu-firmware.c | 44 ++++++++++
 .../platform/arm/mali-vpu/mali-vpu-firmware.h | 13 +++
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   | 87 +++++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 19 ++++
 4 files changed, 163 insertions(+)
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c +44 -0
index 34d57b288a682..99d191b194bfe 100644
@@ -1036,6 +1036,7 @@ int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg)
 		struct mve_response_frame_alloc_parameters alloc_params;
 		struct mve_response_sequence_parameters seq_params;
 		struct mve_buffer_param buffer_param;
+		struct mve_response_event event;
 		struct mve_response_error error;
 	} fw_msg = {};
 	size_t size = sizeof(fw_msg);
@@ -1149,6 +1150,9 @@ int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg)
 
 		msg->code = MALI_VPU_FW_CODE_SEQUENCE_PARAMS;
 		msg->data.seq_params.planar.buffers_min = fw_msg.seq_params.num_buffers_planar;
+		msg->data.seq_params.bitdepth.luma = fw_msg.seq_params.bitdepth_luma;
+		msg->data.seq_params.bitdepth.chroma = fw_msg.seq_params.bitdepth_chroma;
+		msg->data.seq_params.interlaced = fw_msg.seq_params.interlace;
 		break;
 	case MVE_RESPONSE_CODE_INPUT_FLUSHED:
 		msg->code = MALI_VPU_FW_CODE_INPUT_FLUSH;
@@ -1169,6 +1173,46 @@ int mali_vpu_fw_parse_msg(struct mali_vpu_fw *fw, struct mali_vpu_fw_msg *msg)
 	case MVE_BUFFER_CODE_PARAM:
 		ret =  mali_vpu_convert_buffer_param(msg, &fw_msg.buffer_param, size);
 		break;
+	case MVE_RESPONSE_CODE_EVENT: {
+		size_t message_offset = offsetof(struct mve_response_event, event_data.message);
+		size_t message_size;
+
+		msg->code = MALI_VPU_FW_CODE_EVENT;
+
+		if (size < sizeof(fw_msg.event.event_code))
+			return -EPROTO;
+
+		switch (fw_msg.event.event_code) {
+		case MVE_EVENT_ERROR_STREAM_CORRUPT:
+			if (size <= message_offset)
+				return -EPROTO;
+
+			message_size = min(size - message_offset,
+					   sizeof(fw_msg.event.event_data.message));
+
+			dev_warn_ratelimited(fw->dev,
+					     "firmware reports corrupt stream: %.*s\n",
+					     (int)message_size,
+					     fw_msg.event.event_data.message);
+
+			break;
+		case MVE_EVENT_ERROR_STREAM_NOT_SUPPORTED:
+			if (size <= message_offset)
+				return -EPROTO;
+
+			message_size = min(size - message_offset,
+					   sizeof(fw_msg.event.event_data.message));
+			dev_warn_ratelimited(fw->dev,
+					     "firmware reports unsupported stream: %.*s\n",
+					     (int)message_size,
+					     fw_msg.event.event_data.message);
+			msg->code = MALI_VPU_FW_CODE_STREAM_NOT_SUPPORTED;
+			break;
+		default:
+			break;
+		}
+		break;
+	}
 	case MVE_RESPONSE_CODE_ERROR: {
 		size_t message_offset = offsetof(struct mve_response_error, message);
 		size_t message_size;
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.h +13 -0
index 6c46f23ac8638..d3928fdc9133f 100644
@@ -86,13 +86,22 @@ struct mali_vpu_fw_set_option {
 
 /**
  * struct mali_vpu_fw_seq_param - Sequence parameters.
+ * @interlaced: Whether the decoded sequence is interlaced.
  * @planar: Planar output-buffer requirements.
  * @planar.buffers_min:	Minimum number of planar buffers required.
+ * @bitdepth: Decoded luma and chroma bitdepths.
+ * @bitdepth.luma: Decoded luma bitdepth.
+ * @bitdepth.chroma: Decoded chroma bitdepth.
  */
 struct mali_vpu_fw_seq_param {
+	bool interlaced;
 	struct {
 		u32 buffers_min;
 	} planar;
+	struct mali_vpu_fw_bitdepth {
+		u32 chroma;
+		u32 luma;
+	} bitdepth;
 };
 
 /**
@@ -146,6 +155,8 @@ struct mali_vpu_fw_error {
  * @MALI_VPU_FW_CODE_ALLOC_PARAM: Firmware reported new frame allocation parameters.
  * @MALI_VPU_FW_CODE_COLOR_DESC: Firmware reported colour-description metadata.
  * @MALI_VPU_FW_CODE_DPB_HELD_FRAMES: Firmware reported the number of frames held in the DPB.
+ * @MALI_VPU_FW_CODE_STREAM_NOT_SUPPORTED: Firmware rejected the coded stream.
+ * @MALI_VPU_FW_CODE_EVENT: Firmware reported an informational event.
  * @MALI_VPU_FW_CODE_UNKNOWN: Unrecognized firmware message.
  * @MALI_VPU_FW_CODE_MAX: Sentinel marking the end of the message-code enum.
  */
@@ -168,6 +179,8 @@ enum mali_vpu_fw_code {
 	MALI_VPU_FW_CODE_ALLOC_PARAM,        /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_COLOR_DESC,         /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_DPB_HELD_FRAMES,    /* Driver <- Firmware. */
+	MALI_VPU_FW_CODE_STREAM_NOT_SUPPORTED, /* Driver <- Firmware. */
+	MALI_VPU_FW_CODE_EVENT,              /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_UNKNOWN,            /* Driver <- Firmware. */
 	MALI_VPU_FW_CODE_MAX
 };
drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h +87 -0

99 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +19 -0
index 411421e175e71..500d80788d0eb 100644
@@ -584,6 +584,20 @@ static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess,
 		struct vb2_queue *dst_q;
 		u32 buffers_min;
 
+		if (fw_msg->data.seq_params.interlaced) {
+			dev_err(sess->vpu_dev->dev, "interlaced streams are not supported\n");
+			return -EOPNOTSUPP;
+		}
+
+		if (fw_msg->data.seq_params.bitdepth.luma != 8 ||
+		    fw_msg->data.seq_params.bitdepth.chroma != 8) {
+			dev_err(sess->vpu_dev->dev,
+				"bitdepth luma=%u chroma=%u is not supported\n",
+				fw_msg->data.seq_params.bitdepth.luma,
+				fw_msg->data.seq_params.bitdepth.chroma);
+			return -EOPNOTSUPP;
+		}
+
 		buffers_min = fw_msg->data.seq_params.planar.buffers_min;
 		dst_q = v4l2_m2m_get_dst_vq(sess->m2m_ctx);
 
@@ -599,6 +613,11 @@ static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess,
 
 		return mali_vpu_session_fw_send_flush(sess, false);
 	}
+	case MALI_VPU_FW_CODE_STREAM_NOT_SUPPORTED:
+		return -EOPNOTSUPP;
+	case MALI_VPU_FW_CODE_EVENT:
+		/* No session action is required for informational events. */
+		return 0;
 	case MALI_VPU_FW_CODE_UNKNOWN:
 	case MALI_VPU_FW_CODE_MAX:
 	default:
Signature · 1 line
2.43.0
#57 lore raw

RFC PATCH 27/28 media: mali-vpu: add stateful decoder state machine

In reply to Charvi Mehta

From: Shobhit Pandey <[email protected]>

Replace the decoder drc_pending flag with explicit session-state tracking during FW SOURCE CHANGE and STREAMON/STREAMOFF. Validate decoder state transitions and update the state during queue STREAMON/STREAMOFF, decoder START/STOP commands, EOS handling and firmware responses.
Distinguish initial picture-size discovery from runtime resolution changes, and delay the source-change event until the firmware has completed the CAPTURE queue flush. Track draining until the final CAPTURE buffer is returned with EOS.

---
 .../platform/arm/mali-vpu/mali-vpu-dec.c      | 139 +++++++++++++++++-
 .../platform/arm/mali-vpu/mali-vpu-dec.h      |   4 +
 .../platform/arm/mali-vpu/mali-vpu-session.c  |  67 +++++++--
 .../platform/arm/mali-vpu/mali-vpu-session.h  |   6 +-
 .../platform/arm/mali-vpu/mali-vpu-state.h    | 136 +++++++++++++++++
 5 files changed, 331 insertions(+), 21 deletions(-)
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-state.h
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c +135 -4
index 7e34c24ae050f..6ce61de22a802 100644
@@ -9,6 +9,7 @@
 #include "mali-vpu-dec.h"
 #include "mali-vpu-session.h"
 #include "mali-vpu-variant.h"
+#include "mali-vpu-state.h"
 
 void mali_vpu_session_init_dec(struct mali_vpu_session *sess)
 {
@@ -23,7 +24,7 @@ void mali_vpu_session_init_dec(struct mali_vpu_session *sess)
 	sess->num_ctrls_caps	= variant_ctrls->num_dec_ctrls;
 
 	sess->min_buffers_required = 3;	/* For CAPTURE/dst queue */
-	sess->drc_pending	= false;
+	sess->sess_state	= MALI_VPU_STATE_INITIALIZATION;
 }
 
 int mali_vpu_ctrl_set_ignore_stream_headers(struct mali_vpu_session *sess,
@@ -69,6 +70,7 @@ int mali_vpu_ctrl_set_frame_reordering(struct mali_vpu_session *sess, u32 cid,
 int mali_vpu_decoder_cmd(struct file *filp, void *priv, struct v4l2_decoder_cmd *cmd)
 {
 	struct mali_vpu_session *sess = mali_vpu_get_session(filp);
+	struct vb2_queue *dst_q = v4l2_m2m_get_dst_vq(sess->m2m_ctx);
 	int ret;
 
 	ret = v4l2_m2m_ioctl_try_decoder_cmd(filp, priv, cmd);
@@ -79,11 +81,31 @@ int mali_vpu_decoder_cmd(struct file *filp, void *priv, struct v4l2_decoder_cmd
 
 	switch (cmd->cmd) {
 	case V4L2_DEC_CMD_STOP:
-		ret = mali_vpu_session_fw_send_eos(sess);
+		if (sess->sess_state == MALI_VPU_STATE_DRAIN) {
+			ret = -EBUSY;
+			break;
+		} else if (sess->sess_state == MALI_VPU_STATE_STREAMING) {
+			ret = mali_vpu_session_fw_send_eos(sess);
+			if (!ret)
+				ret = mali_vpu_dec_update_state(sess, MALI_VPU_STATE_DRAIN);
+		}
 		break;
 	case V4L2_DEC_CMD_START:
-		vb2_clear_last_buffer_dequeued(v4l2_m2m_get_dst_vq(sess->m2m_ctx));
-		ret = 0;
+		if (sess->sess_state == MALI_VPU_STATE_DRAIN &&
+		    !dst_q->last_buffer_dequeued) {
+			ret = -EBUSY;
+			break;
+		}
+
+		ret = mali_vpu_submit_deferred_bufs(sess);
+		if (ret)
+			break;
+
+		ret = mali_vpu_dec_update_state(sess, MALI_VPU_STATE_STREAMING);
+		if (ret)
+			break;
+
+		vb2_clear_last_buffer_dequeued(dst_q);
 		break;
 	default:
 		ret = -EINVAL;
@@ -93,3 +115,112 @@ int mali_vpu_decoder_cmd(struct file *filp, void *priv, struct v4l2_decoder_cmd
 	mutex_unlock(&sess->vq_lock);
 	return ret;
 }
+
+static bool mali_vpu_dec_allow_state_change(struct mali_vpu_session *sess,
+					    enum mali_vpu_sess_state new_state)
+{
+	switch (sess->sess_state) {
+	case MALI_VPU_STATE_INITIALIZATION:
+		return (new_state == MALI_VPU_STATE_RESET ||
+			new_state == MALI_VPU_STATE_STOPPED);
+	case MALI_VPU_STATE_STREAMING:
+		return (new_state == MALI_VPU_STATE_RESET ||
+			new_state == MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE ||
+			new_state == MALI_VPU_STATE_DRAIN);
+	case MALI_VPU_STATE_RESET:
+		return (new_state == MALI_VPU_STATE_INITIALIZATION ||
+			new_state == MALI_VPU_STATE_STREAMING);
+	case MALI_VPU_STATE_DRAIN:
+		return (new_state == MALI_VPU_STATE_STOPPED ||
+			new_state == MALI_VPU_STATE_RESET);
+	case MALI_VPU_STATE_STOPPED:
+		return (new_state == MALI_VPU_STATE_INITIALIZATION ||
+			new_state == MALI_VPU_DEC_STATE_FIRST_SOURCE_CHANGE ||
+			new_state == MALI_VPU_STATE_STREAMING ||
+			new_state == MALI_VPU_STATE_RESET);
+	case MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE:
+		return new_state == MALI_VPU_STATE_STOPPED;
+	case MALI_VPU_DEC_STATE_FIRST_SOURCE_CHANGE:
+		return new_state == MALI_VPU_STATE_STREAMING;
+	default:
+		return false;
+	}
+}
+
+int mali_vpu_dec_update_state(struct mali_vpu_session *sess, u32 new_state)
+{
+	if (sess->role != MALI_VPU_DECODER)
+		return -EINVAL;
+
+	if (sess->sess_state == new_state)
+		return 0;
+
+	if (!mali_vpu_dec_allow_state_change(sess, new_state))
+		return -EINVAL;
+
+	sess->sess_state = new_state;
+	return 0;
+}
+
+int mali_vpu_dec_state_change_streamon(struct mali_vpu_session *sess, enum v4l2_buf_type type)
+{
+	enum mali_vpu_sess_state new_state;
+
+	switch (sess->sess_state) {
+	case MALI_VPU_STATE_INITIALIZATION:
+		new_state = V4L2_TYPE_IS_OUTPUT(type) ?
+			MALI_VPU_STATE_STOPPED : MALI_VPU_STATE_RESET;
+		break;
+	case MALI_VPU_STATE_STOPPED:
+	case MALI_VPU_DEC_STATE_FIRST_SOURCE_CHANGE:
+		if (V4L2_TYPE_IS_OUTPUT(type))
+			return 0;
+		new_state = MALI_VPU_STATE_STREAMING;
+		break;
+	case MALI_VPU_STATE_RESET:
+		if (!V4L2_TYPE_IS_OUTPUT(type))
+			return 0;
+		new_state = MALI_VPU_STATE_STREAMING;
+		break;
+	default:
+		return 0;
+	}
+
+	return mali_vpu_dec_update_state(sess, new_state);
+}
+
+int mali_vpu_dec_state_change_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type type)
+{
+	enum mali_vpu_sess_state new_state;
+
+	if (V4L2_TYPE_IS_OUTPUT(type)) {
+		switch (sess->sess_state) {
+		case MALI_VPU_STATE_STOPPED:
+			new_state = MALI_VPU_STATE_INITIALIZATION;
+			break;
+		case MALI_VPU_STATE_STREAMING:
+		case MALI_VPU_STATE_DRAIN:
+			new_state = MALI_VPU_STATE_RESET;
+			break;
+		default:
+			return 0;
+		}
+	} else {
+		switch (sess->sess_state) {
+		case MALI_VPU_STATE_RESET:
+			new_state = MALI_VPU_STATE_INITIALIZATION;
+			break;
+		case MALI_VPU_STATE_STREAMING:
+		case MALI_VPU_STATE_DRAIN:
+			new_state = MALI_VPU_STATE_STOPPED;
+			break;
+		case MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE:
+			new_state = MALI_VPU_STATE_STOPPED;
+			break;
+		default:
+			return 0;
+		}
+	}
+
+	return mali_vpu_dec_update_state(sess, new_state);
+}
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.h +4 -0
index a108aaaa4a959..fccda5b3d5b2e 100644
@@ -7,6 +7,7 @@
 #define __MALI_VPU_DEC_H__
 
 #include <linux/types.h>
+#include <linux/videodev2.h>
 
 struct file;
 struct mali_vpu_session;
@@ -19,5 +20,8 @@ int mali_vpu_ctrl_set_ignore_stream_headers(struct mali_vpu_session *sess, u32 c
 int mali_vpu_ctrl_set_frame_reordering(struct mali_vpu_session *sess, u32 cid, s64 value);
 
 int mali_vpu_decoder_cmd(struct file *filp, void *priv, struct v4l2_decoder_cmd *cmd);
+int mali_vpu_dec_update_state(struct mali_vpu_session *sess, u32 new_state);
+int mali_vpu_dec_state_change_streamon(struct mali_vpu_session *sess, enum v4l2_buf_type type);
+int mali_vpu_dec_state_change_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type type);
 
 #endif /* __MALI_VPU_DEC_H__ */
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +52 -15

149 lines not on this page · raw message

drivers/media/platform/arm/mali-vpu/mali-vpu-session.h +4 -2
index 45d0b6710aced..b37ed569ffb7a 100644
@@ -17,6 +17,7 @@
 #include "mali-vpu-firmware.h"
 #include "mali-vpu-formats.h"
 #include "mali-vpu-mmu.h"
+#include "mali-vpu-state.h"
 
 struct v4l2_m2m_ctx;
 struct mali_vpu_fw_bin;
@@ -115,7 +116,7 @@ enum mali_vpu_session_fw_bin_state {
  * @sched_sess: Scheduler state used to map this session to an LSID.
  * @ncores: Number of cores assigned to this session by scheduler.
  * @error: Fatal session error code.
- * @drc_pending: Dynamic resolution change is waiting for CAPTURE reconfigure.
+ * @sess_state: Session main state.
  */
 struct mali_vpu_session {
 	struct mali_vpu_dev		*vpu_dev;
@@ -148,7 +149,7 @@ struct mali_vpu_session {
 	struct mali_vpu_sched_session	sched_sess;
 	u32				ncores;
 	int				error;
-	bool				drc_pending;
+	enum mali_vpu_sess_state	sess_state;
 };
 
 int mali_vpu_session_init(struct mali_vpu_session *sess);
@@ -163,6 +164,7 @@ void mali_vpu_session_irq(struct mali_vpu_session *sess);
 int mali_vpu_session_qbuf(struct mali_vpu_session *sess, struct vb2_buffer *vb2_buf);
 void mali_vpu_session_reset_ts_metadata(struct mali_vpu_session *sess);
 int mali_vpu_session_fw_send_eos(struct mali_vpu_session *sess);
+int mali_vpu_submit_deferred_bufs(struct mali_vpu_session *sess);
 
 static inline struct mali_vpu_session *mali_vpu_get_session(struct file *filp)
 {
drivers/media/platform/arm/mali-vpu/mali-vpu-state.h new file +136 -0

136 lines not on this page · raw message

Signature · 1 line
2.43.0
#58 lore raw

Re: [RFC PATCH 27/28] media: mali-vpu: add stateful decoder state machine

Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] media: mali-vpu: allow STREAMING to STOPPED on CAPTURE streamoff - [High] media: mali-vpu: missing…

Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [High] media: mali-vpu: allow STREAMING to STOPPED on CAPTURE streamoff
- [High] media: mali-vpu: missing DRAIN to STOPPED transition breaks CMD_START
- [High] media: mali-vpu: handle OUTPUT streamoff in source-change states
- [High] media: mali-vpu: handle MALI_VPU_FW_CODE_ALLOC_PARAM during DRAIN state
- [Medium] media: mali-vpu: validate queue state on V4L2_DEC_CMD_START in decoder_cmd()
- [Medium] media: mali-vpu: avoid premature state transition in session_streamoff()
--

commit 97c2307773c544ec658afbe30255359b9cb184c5
Author: Shobhit Pandey <[email protected]>

media: mali-vpu: add stateful decoder state machine

This commit replaces the decoder drc_pending flag with an explicit session state machine tracking queue streaming states, drain handling, and source-change events.

Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c +0 -0
index 7e34c24ae050f..6ce61de22a802 100644

[ ... ]

Quoted 23 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c +14 -3
@@ -79,11 +81,31 @@ int mali_vpu_decoder_cmd(struct file *filp, void *priv, struct v4l2_decoder_cmd
 
 	switch (cmd->cmd) {
 	case V4L2_DEC_CMD_STOP:
-		ret = mali_vpu_session_fw_send_eos(sess);
+		if (sess->sess_state == MALI_VPU_STATE_DRAIN) {
+			ret = -EBUSY;
+			break;
+		} else if (sess->sess_state == MALI_VPU_STATE_STREAMING) {
+			ret = mali_vpu_session_fw_send_eos(sess);
+			if (!ret)
+				ret = mali_vpu_dec_update_state(sess, MALI_VPU_STATE_DRAIN);
+		}
 		break;
 	case V4L2_DEC_CMD_START:
-		vb2_clear_last_buffer_dequeued(v4l2_m2m_get_dst_vq(sess->m2m_ctx));
-		ret = 0;
+		if (sess->sess_state == MALI_VPU_STATE_DRAIN &&
+		    !dst_q->last_buffer_dequeued) {
+			ret = -EBUSY;
+			break;
+		}
+

drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c int mali_vpu_decoder_cmd(struct file *filp, void *priv, struct v4l2_decoder_cmd · line 109

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c +9 -0
+		ret = mali_vpu_submit_deferred_bufs(sess);
+		if (ret)
+			break;
+
+		ret = mali_vpu_dec_update_state(sess, MALI_VPU_STATE_STREAMING);
+		if (ret)
+			break;
+
+		vb2_clear_last_buffer_dequeued(dst_q);
 		break;

[Severity: Medium]
Can V4L2_DEC_CMD_START transition sess->sess_state to MALI_VPU_STATE_STREAMING when only one queue has been started?

Unlike V4L2_DEC_CMD_STOP, V4L2_DEC_CMD_START does not check whether both queues are streaming before calling mali_vpu_submit_deferred_bufs() and mali_vpu_dec_update_state(). Because mali_vpu_dec_allow_state_change() allows MALI_VPU_STATE_STOPPED, MALI_VPU_DEC_STATE_FIRST_SOURCE_CHANGE, and MALI_VPU_STATE_RESET to enter MALI_VPU_STATE_STREAMING, calling V4L2_DEC_CMD_START while only OUTPUT is streaming moves sess->sess_state to MALI_VPU_STATE_STREAMING while CAPTURE is still off. When MALI_VPU_FW_CODE_ALLOC_PARAM later arrives during initial header parsing, mali_vpu_session_handle_fw_msg() then enters MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE instead of MALI_VPU_DEC_STATE_FIRST_SOURCE_CHANGE, and subsequent CAPTURE STREAMON cannot exit that state, causing mali_vpu_session_qbuf() to immediately return every queued CAPTURE buffer with V4L2_BUF_FLAG_LAST.

In addition, calling V4L2_DEC_CMD_START in MALI_VPU_STATE_INITIALIZATION runs mali_vpu_submit_deferred_bufs() and then fails in mali_vpu_dec_update_state() with -EINVAL.

Quoted 8 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c +2 -0
 	default:
 		ret = -EINVAL;
@@ -93,3 +115,112 @@ int mali_vpu_decoder_cmd(struct file *filp, void *priv, struct v4l2_decoder_cmd
 	mutex_unlock(&sess->vq_lock);
 	return ret;
 }
+
+static bool mali_vpu_dec_allow_state_change(struct mali_vpu_session *sess,

drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c int mali_vpu_decoder_cmd(struct file *filp, void *priv, struct v4l2_decoder_cmd · line 129

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c +10 -0
+					    enum mali_vpu_sess_state new_state)
+{
+	switch (sess->sess_state) {
+	case MALI_VPU_STATE_INITIALIZATION:
+		return (new_state == MALI_VPU_STATE_RESET ||
+			new_state == MALI_VPU_STATE_STOPPED);
+	case MALI_VPU_STATE_STREAMING:
+		return (new_state == MALI_VPU_STATE_RESET ||
+			new_state == MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE ||
+			new_state == MALI_VPU_STATE_DRAIN);

[Severity: High]
Does MALI_VPU_STATE_STREAMING also need to allow transitioning to MALI_VPU_STATE_STOPPED here?

In mali_vpu_dec_state_change_streamoff(), stopping the CAPTURE queue while in MALI_VPU_STATE_STREAMING requests a transition to MALI_VPU_STATE_STOPPED. Because MALI_VPU_STATE_STOPPED is omitted here, mali_vpu_dec_update_state() returns -EINVAL, causing mali_vpu_session_streamoff() to jump to err_session, skip stopping and flushing the firmware, and put the session into a fatal error state via mali_vpu_session_set_error().

[ ... ]

Quoted 7 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c +7 -0
+int mali_vpu_dec_state_change_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type type)
+{
+	enum mali_vpu_sess_state new_state;
+
+	if (V4L2_TYPE_IS_OUTPUT(type)) {
+		switch (sess->sess_state) {
+		case MALI_VPU_STATE_STOPPED:

drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c int mali_vpu_decoder_cmd(struct file *filp, void *priv, struct v4l2_decoder_cmd · line 208

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c +10 -0
+			new_state = MALI_VPU_STATE_INITIALIZATION;
+			break;
+		case MALI_VPU_STATE_STREAMING:
+		case MALI_VPU_STATE_DRAIN:
+			new_state = MALI_VPU_STATE_RESET;
+			break;
+		default:
+			return 0;
+		}
+	} else {

[Severity: High]
What happens when VIDIOC_STREAMOFF is called on the OUTPUT queue while in MALI_VPU_DEC_STATE_FIRST_SOURCE_CHANGE or MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE?

Both states hit the default branch and return 0 without updating sess->sess_state (and mali_vpu_dec_allow_state_change() does not allow either state to transition to MALI_VPU_STATE_INITIALIZATION or MALI_VPU_STATE_RESET). If OUTPUT is streamed off during MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE (for example to seek or tear down the stream), sess->sess_state remains MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE; a subsequent OUTPUT STREAMON stays trapped in that state and bounces every queued CAPTURE buffer with V4L2_BUF_FLAG_LAST, while a subsequent CAPTURE STREAMOFF sees drc_capture as true, skips stopping and flushing the firmware, and leaves sess->sess_state in MALI_VPU_STATE_STOPPED when both queues are off. Similarly, OUTPUT STREAMOFF during MALI_VPU_DEC_STATE_FIRST_SOURCE_CHANGE leaves sess->sess_state unchanged when neither queue is streaming.

Quoted 4 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +0 -0
index 500d80788d0eb..3ff4635e682eb 100644

[ ... ]

Quoted 5 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +0 -1
@@ -513,7 +513,14 @@ static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess,
 		state = mali_vpu_buffer_update_info_to_v4l2(vpu_buf);
 
 		mali_vpu_session_buffer_done(sess, vpu_buf, state);
-

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess, · line 525

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +8 -0
+		if (vpu_buf->flags & MALI_VPU_BUFFER_EOS) {
+			if (V4L2_TYPE_IS_OUTPUT(vpu_buf->type) &&
+			    sess->sess_state == MALI_VPU_STATE_STREAMING) {
+				ret = mali_vpu_dec_update_state(sess, MALI_VPU_STATE_DRAIN);
+				if (ret)
+					return ret;
+			}
+		}
 		return 0;
 	}

[Severity: High]
Where does sess->sess_state transition from MALI_VPU_STATE_DRAIN to MALI_VPU_STATE_STOPPED when the final CAPTURE buffer is returned with MALI_VPU_BUFFER_EOS?

Only OUTPUT buffers with MALI_VPU_BUFFER_EOS update sess->sess_state here, so sess->sess_state remains MALI_VPU_STATE_DRAIN after the final CAPTURE buffer is returned and dequeued. Because mali_vpu_dec_allow_state_change() does not allow MALI_VPU_STATE_DRAIN to transition directly to MALI_VPU_STATE_STREAMING, a subsequent V4L2_DEC_CMD_START in mali_vpu_decoder_cmd() submits deferred buffers and then fails in mali_vpu_dec_update_state(sess, MALI_VPU_STATE_STREAMING) with -EINVAL before clearing dst_q->last_buffer_dequeued.

Note that if sess->sess_state moves to MALI_VPU_STATE_STOPPED as soon as the firmware returns the CAPTURE EOS buffer while CAPTURE is still streaming, V4L2_DEC_CMD_START and mali_vpu_session_qbuf() also need to preserve drain gating until dst_q->last_buffer_dequeued is set, and OUTPUT STREAMOFF in mali_vpu_dec_state_change_streamoff() needs to transition to MALI_VPU_STATE_RESET rather than MALI_VPU_STATE_INITIALIZATION when CAPTURE is still streaming.

Quoted 24 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +9 -5
@@ -569,17 +576,31 @@ static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess,
 			fw_msg->data.dpb_held_frames);
 		return 0;
 	case MALI_VPU_FW_CODE_OUTPUT_FLUSH:
-		dev_dbg(sess->vpu_dev->dev, "fw rsp: Dst queue flushed\n");
-		if (sess->role == MALI_VPU_DECODER && sess->drc_pending)
+		if (sess->sess_state == MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE ||
+		    sess->sess_state == MALI_VPU_DEC_STATE_FIRST_SOURCE_CHANGE) {
 			return mali_vpu_session_send_drc_event(sess);
+		}
 
 		return 0;
 	case MALI_VPU_FW_CODE_ALLOC_PARAM:
 		ret = mali_vpu_session_update_capture_format(sess, fw_msg->data.alloc_params);
-		if (!ret)
-			sess->drc_pending = true;
+		if (ret)
+			return ret;
 
-		return ret;
+		if (sess->sess_state == MALI_VPU_STATE_STREAMING) {
+			ret =
+			mali_vpu_dec_update_state(sess,
+						  MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE);

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c static int mali_vpu_session_handle_fw_msg(struct mali_vpu_session *sess, · line 603

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +10 -0
+			if (ret)
+				return ret;
+		} else if (sess->sess_state == MALI_VPU_STATE_STOPPED) {
+			ret = mali_vpu_dec_update_state(sess,
+							MALI_VPU_DEC_STATE_FIRST_SOURCE_CHANGE);
+			if (ret)
+				return ret;
+		}
+
+		return 0;

[Severity: High]
Can MALI_VPU_FW_CODE_ALLOC_PARAM arrive while sess->sess_state is already MALI_VPU_STATE_DRAIN?

If userspace queues bitstream buffers containing headers or a resolution change and immediately initiates a drain via V4L2_DEC_CMD_STOP (or an EOS OUTPUT buffer), sess->sess_state enters MALI_VPU_STATE_DRAIN before the firmware finishes parsing the queued buffers. When MALI_VPU_FW_CODE_ALLOC_PARAM then arrives, sess->sess_state is neither MALI_VPU_STATE_STREAMING nor MALI_VPU_STATE_STOPPED, so it stays in MALI_VPU_STATE_DRAIN. The CAPTURE flush triggered by MALI_VPU_FW_CODE_SEQUENCE_PARAMS then returns a 0-byte buffer with V4L2_BUF_FLAG_LAST that queues a premature V4L2_EVENT_EOS in mali_vpu_session_buffer_done(), and MALI_VPU_FW_CODE_OUTPUT_FLUSH drops V4L2_EVENT_SOURCE_CHANGE.

[ ... ]

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +6 -0
@@ -1003,12 +1033,19 @@ int mali_vpu_session_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type
 	if (sess->error)
 		return 0;
 
+	drc_capture =
+		sess->sess_state == MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE &&
+		mali_vpu_is_raw_q(sess, type);
+
+	ret = mali_vpu_dec_state_change_streamoff(sess, type);
+	if (ret)

drivers/media/platform/arm/mali-vpu/mali-vpu-session.c int mali_vpu_session_streamoff(struct mali_vpu_session *sess, enum v4l2_buf_type · line 1049

Quoted 10 lines
drivers/media/platform/arm/mali-vpu/mali-vpu-session.c +3 -2
+		goto err_session;
+
 	if (sess->fw_bin_state != MALI_VPU_SESSION_FW_BIN_READY ||
 	    !sess->sched_sess.lsid)
 		return 0;
 
-	if (sess->drc_pending && sess->role == MALI_VPU_DECODER &&
-	    mali_vpu_is_raw_q(sess, type)) {
+	if (drc_capture) {
 		ret = mali_vpu_session_wait_pending(sess);

[Severity: Medium]
Does updating sess->sess_state in mali_vpu_dec_state_change_streamoff() before mali_vpu_session_wait_pending() cause in-flight firmware flush responses to see the wrong state?

In the drc_capture path, mali_vpu_dec_state_change_streamoff() immediately changes sess->sess_state from MALI_VPU_DEC_STATE_DYNAMIC_RESOLUTION_CHANGE to MALI_VPU_STATE_STOPPED before mali_vpu_session_wait_pending() drops sess->vq_lock to wait for pending firmware responses. If MALI_VPU_FW_CODE_OUTPUT_FLUSH or flushed 0-byte CAPTURE buffers (marked with V4L2_BUF_FLAG_LAST) are processed during that wait, mali_vpu_session_handle_fw_msg() sees MALI_VPU_STATE_STOPPED, which drops V4L2_EVENT_SOURCE_CHANGE and causes mali_vpu_session_buffer_done() to emit a spurious V4L2_EVENT_EOS. Similarly, during a normal CAPTURE STREAMOFF, mali_vpu_session_fw_send_flush() returns active CAPTURE buffers with 0 payload size while sess->sess_state is already MALI_VPU_STATE_STOPPED or MALI_VPU_STATE_INITIALIZATION, also triggering V4L2_EVENT_EOS in mali_vpu_session_buffer_done().

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

RFC PATCH 28/28 media: MAINTAINERS: add Arm Mali-V76 VPU driver

In reply to Charvi Mehta

Add maintenance contacts and file patterns for Arm Mali-V76 VPU driver.

---
 MAINTAINERS | 11 +++++++++++
 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
index c08aade46f61d..3150c3bef7d54 100644
@@ -2409,6 +2409,17 @@ F:	Documentation/userspace-api/media/v4l/metafmt-arm-mali-c55.rst
 F:	drivers/media/platform/arm/mali-c55/
 F:	include/uapi/linux/media/arm/mali-c55-config.h
 
+ARM MALI-V76 VPU DRIVER
+M:	Charvi Mehta <[email protected]>
+M:	Vincenzo Frascino <[email protected]>
+R:	Rathinavel Sankaralingam <[email protected]>
+R:	Shobhit Pandey <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/media/arm,mali-v76.yaml
+F:	drivers/media/platform/arm/mali-vpu/
+F:	include/uapi/linux/media/arm/mali-vpu-config.h
+
 ARM MALI PANTHOR DRM DRIVER
 M:	Boris Brezillon <[email protected]>
 M:	Steven Price <[email protected]>
Signature · 1 line
2.43.0
#60 lore raw
In reply to Charvi Mehta

Hi,

its nice to see ARM working on upstream drivers.

Le jeudi 19 novembre 2026 à 15:37 +0000, Charvi Mehta a écrit :

Quoted 181 lines

This series introduces support for the Arm Mali-V76 Video Processing Unit (VPU), providing H.264 video stream decoding through the V4L2 stateful memory-to-memory interface.

The Mali-V76 offloads video decoding from the application processor. The host driver loads and manages the codec firmware, communicating with it through asynchronous message queues, interrupts and RPC requests. It also manages the firmware address space using the V76 internal MMU and controls hardware resources through the LSID and job-queue interfaces.

The driver currently supports:

- H.264 decoding through the V4L2 stateful memory-to-memory interface.
- Multi-planar capture using V4L2_PIX_FMT_YUV420M, V4L2_PIX_FMT_NV12M and V4L2_PIX_FMT_NV21M, with MMAP and DMA-BUF buffer support.
- Variant-specific definitions of supported formats, frame sizes, controls and hardware resources.
- Explicit management of decoder and firmware states throughout stream setup, decoding and teardown.
- Asynchronous firmware loading and caching, with interrupt-driven communication for commands, buffer completion and firmware events.
- Separate control of OUTPUT and CAPTURE streaming, allowing CAPTURE buffers to be reconfigured during dynamic resolution changes.
- Decoder drain, end-of-stream and dynamic resolution change handling, including source-change events and LAST buffer signalling.
- Timestamp propagation and buffer metadata handling.
- Firmware error and unsupported-stream reporting.

The initial implementation supports a single active decoding session, with hardware resource management handled through the V76 LSID and job-queue interfaces. Support for additional codecs and concurrent sessions is outside the scope of this series.

The series is based on media-committers/next.

To simplify the testing a tree with the new patches on top has been made available at [1].

[1] https://gitlab.arm.com/media/vpu/charvi/linux.git mali/v76/v1

Cc Mauro Carvalho Chehab <[email protected]>
Cc Rob Herring <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Conor Dooley <[email protected]>
Cc Philipp Zabel <[email protected]>

Testing
=======
The driver has been tested on Mali-V76 pre-silicon FPGA.

Static tools like checkpatch, smatch, dt_binding_check, sparse, checkstack and Coccinelle run successfully with this driver.

The driver has been tested with v4l2-ctl[2] and Gstreamer[3], v4l2-compliance [4] and Fluster [5].

[2] v4l2-ctl -d /dev/video0 --verbose \
--set-fmt-video-out=width=1920,height=1080,pixelformat=H264 \
--set-fmt-video=width=1920,height=1080,pixelformat=NM12 \
--stream-mmap=4 --stream-out-mmap=1 \
--stream-from /media/CrowdRun_1080p.264 --stream-to decoded.yuv

[3] gst-launch-1.0 -v filesrc location=/media/CrowdRun_720p.264 ! h264parse \
! v4l2h264dec ! filesink location=decoded.yuv sync=false

[4] V4l2 compliance was tested using v4l2-compliance 1.28.1:

v4l2-compliance -d /dev/video0 --stream-from /media/CrowdRun_1080p.264 -s 200

v4l2-compliance 1.28.1, 64 bits, 64-bit time_t

Compliance test for mali-vpu device /dev/video0:

Driver Info:

	Driver name      : mali-vpu
	Card type        : mali-vpu-decoder
	Bus info         : platform:6f030000.video-codec
	Driver version   : 7.4.0
	Capabilities     : 0x84204000

Video Memory-to-Memory Multiplanar Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x04204000
Video Memory-to-Memory Multiplanar Streaming
Extended Pix Format
Detected Stateful Decoder

Required ioctls:
test VIDIOC_QUERYCAP: OK
test invalid ioctls: OK

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

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

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

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

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

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

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

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

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

Test input 0:

Streaming ioctls:
test read/write: OK (Not Supported)
test blocking wait: OK
Video Capture Multiplanar: Captured 632 buffers test MMAP (select): OK
Video Capture Multiplanar: Captured 632 buffers test MMAP (epoll): OK
test USERPTR (select): OK (Not Supported) test DMABUF: Cannot test, specify --expbuf-device

Total for mali-vpu device /dev/video0: 52, Succeeded: 52, Failed: 0, Warnings: 0

[5] Fluster was run with

GStreamer-H.264-V4L2 decoder:

Quoted 7 lines

Running test suite JVT-AVC_V1 with decoder GStreamer-H.264-V4L2 Ran 78/135 tests successfully.

The failing tests:

52 test vectors fail as they are interlaced clips which mali-vpu does not support.

Is that a real hardware limitation, or you are just skipping it in firmware or even in this driver? Its not like you have a compliant decoder without it. I've recently asked Amlogic to not cut corner also, and they accepted to fix their new driver.

Quoted 54 lines

List of these test vectors:
- cabac_mot_fld0_full
- cabac_mot_mbaff0_full
- cabac_mot_picaff0_full
- CABREF3_Sand_D
- CAFI1_SVA_C
- CAMA1_Sony_C
- CAMA1_TOSHIBA_B
- cama1_vtc_c
- cama2_vtc_b
- CAMA3_Sand_E
- cama3_vtc_b
- CAMACI3_Sony_C
- CAMANL1_TOSHIBA_B
- CAMANL2_TOSHIBA_B
- CAMANL3_Sand_E
- CAMASL3_Sony_B
- CAMP_MOT_MBAFF_L30
- CAMP_MOT_MBAFF_L31
- CANLMA2_Sony_C
- CANLMA3_Sony_C
- CAPA1_TOSHIBA_B
- CAPAMA3_Sand_F
- cavlc_mot_fld0_full_B
- cavlc_mot_mbaff0_full_B
- cavlc_mot_picaff0_full_B
- CVCANLMA2_Sony_C
- CVFI1_Sony_D
- CVFI1_SVA_C
- CVFI2_Sony_H
- CVFI2_SVA_C
- CVMA1_Sony_D
- CVMA1_TOSHIBA_B
- CVMANL1_TOSHIBA_B
- CVMANL2_TOSHIBA_B
- CVMAPAQP3_Sony_E
- CVMAQP2_Sony_G
- CVMAQP3_Sony_D
- CVMP_MOT_FLD_L30_B
- CVMP_MOT_FRM_L31_B
- CVNLFI1_Sony_C
- CVNLFI2_Sony_H
- CVPA1_TOSHIBA_B
- FI1_Sony_E
- MR6_BT_B
- MR7_BT_B
- MR8_BT_B
- MR9_BT_B
- Sharp_MP_Field_1_B
- Sharp_MP_Field_2_B
- Sharp_MP_Field_3_B
- Sharp_MP_PAFF_1r2
- Sharp_MP_PAFF_2r

Quoted 8 lines

2 test vectors failed because FMO is unsupported.
Test vectors: FM1_BT_B FM1_FT_E
2 test vectors failed because SP_SLICE type is unsupported.
Test vectors: SP1_BT_A sp2_bt_b
1 additional test vector, CVFC1_Sony_C completes decoding but fails checksum verification due to a stride-handling issue in GStreamer 1.24.13. GStreamer replaces the driver-reported 150-byte planar chroma stride with 152 bytes, causing the decoded chroma planes to be read incorrectly.

1.24 is very old by now, but I think this specific file needs more work. One part of the fix has been made recently:

https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/13059

Quoted 31 lines

Running test suite JVT-FR-EXT with decoder GStreamer-H.264-V4L2 Ran 25/69 tests successfully.

The failing tests:

21 test vectors failed due to interlaced clips. Interlaced decoding is not supported in mali-vpu driver.

Test Vectors:
- brcm_freh10
- brcm_freh11
- brcm_freh4
- brcm_freh5
- brcm_freh6
- freh7_b
- FREXT01_JVC_D
- FREXT02_JVC_C
- FRExt2_Panasonic_C
- FRExt4_Panasonic_B
- HCAFF1_HHI_B
- HCAMFF1_HHI_B
- HCHP3_HHI_A
- HPCAFL_BRCM_C
- HPCAFLNL_BRCM_C
- HPCAMAPALQ_BRCM_B
- HPCVFL_BRCM_A HPCVFLNL_BRCM_A
- HVLCFI0_Sony_B
- HVLCMFF0_Sony_B
- HVLCPFF0_Sony_B

Quoted 3 lines

2 test vectors fail due to 10bit bitstream. 10 bit decoding support to be enabled in subequent patches.
Test vectors: FREH10-1 FREH10-2

Ack.

Nicolas

Quoted 135 lines

21 test vectors failed because 4:2:2 is not supported in this driver. Test vectors:
- FREXT1_TANDBERG_A
- FREXT2_TANDBERG_A
- FREXT3_TANDBERG_A
- Hi422FR10_SONY_A
- Hi422FR11_SONY_A
- Hi422FR12_SONY_A
- Hi422FR1_SONY_A
- Hi422FR2_SONY_A
- Hi422FR3_SONY_A
- Hi422FR4_SONY_A
- Hi422FR13_SONY_A
- Hi422FR14_SONY_A
- Hi422FR15_SONY_A
- Hi422FR6_SONY_A
- Hi422FR7_SONY_A
- Hi422FR8_SONY_A
- Hi422FR9_SONY_A
- Hi422FREXT16_SONY_A
- Hi422FREXT17_SONY_A
- Hi422FREXT18_SONY_A
- Hi422FREXT19_SONY_A

Charvi Mehta (21):
dt-bindings: media: add Arm Mali-V76 VPU
media: mali-vpu: add Mali VPU platform driver
media: mali-vpu: register V4L2 decoder device and add querycap ioctl
media: mali-vpu: initialize decoder format state
media: mali-vpu: add V4L2 mem2mem and VB2 queue support
media: mali-vpu: implement format query ioctls
media: mali-vpu: implement TRY_FMT and S_FMT ioctls
media: mali-vpu: register V76 decoder controls and introduce UAPI
controls
media: mali-vpu: add firmware cache infrastructure
media: mali-vpu: load firmware for decoder sessions
media: mali-vpu: prepare firmware address space
media: mali-vpu: add firmware message transport
media: mali-vpu: route decoder controls to firmware
media: mali-vpu: handle firmware RPC requests
media: mali-vpu: add LSID hardware support
media: mali-vpu: add single session scheduler
media: mali-vpu: handle firmware interrupts
media: mali-vpu: start decoder firmware through LSID scheduling
media: mali-vpu: propagate buffer metadata
media: mali-vpu: handle firmware event responses
media: MAINTAINERS: add Arm Mali-V76 VPU driver

Rath Sankaralingam (4):
media: mali-vpu: add MMU software page table manager
media: mali-vpu: implement vb2 buffer queue handling
media: mali-vpu: handle firmware buffer exchange
media: mali-vpu: handle fatal firmware error

Shobhit Pandey (3):
media: mali-vpu: add decoder EOS and DRC handling
media: mali-vpu: handle firmware buffer parameters
media: mali-vpu: add stateful decoder state machine

 .../bindings/media/arm,mali-v76.yaml          |   70 +
 MAINTAINERS                                   |   11 +
 drivers/media/platform/arm/Kconfig            |    1 +
 drivers/media/platform/arm/Makefile           |    1 +
 drivers/media/platform/arm/mali-vpu/Kconfig   |   17 +
 drivers/media/platform/arm/mali-vpu/Makefile  |   18 +
 .../platform/arm/mali-vpu/mali-vpu-buffer.c   |  426 +++++
 .../platform/arm/mali-vpu/mali-vpu-buffer.h   |  109 ++
 .../platform/arm/mali-vpu/mali-vpu-controls.c |  351 ++++
 .../platform/arm/mali-vpu/mali-vpu-controls.h |   71 +
 .../platform/arm/mali-vpu/mali-vpu-dec.c      |  226 +++
 .../platform/arm/mali-vpu/mali-vpu-dec.h      |   27 +
 .../platform/arm/mali-vpu/mali-vpu-device.h   |   72 +
 .../arm/mali-vpu/mali-vpu-firmware-cache.c    |  549 ++++++
 .../arm/mali-vpu/mali-vpu-firmware-cache.h    |  180 ++
 .../platform/arm/mali-vpu/mali-vpu-firmware.c | 1647 +++++++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-firmware.h |  324 ++++
 .../platform/arm/mali-vpu/mali-vpu-formats.c  |   95 +
 .../platform/arm/mali-vpu/mali-vpu-formats.h  |   39 +
 .../platform/arm/mali-vpu/mali-vpu-lsid.c     |  271 +++
 .../platform/arm/mali-vpu/mali-vpu-lsid.h     |   58 +
 .../platform/arm/mali-vpu/mali-vpu-mmu.c      |  699 +++++++
 .../platform/arm/mali-vpu/mali-vpu-mmu.h      |  154 ++
 .../platform/arm/mali-vpu/mali-vpu-probe.c    |  387 ++++
 .../arm/mali-vpu/mali-vpu-protocol-def-v3.h   |  710 +++++++
 .../arm/mali-vpu/mali-vpu-registers.h         |   81 +
 .../arm/mali-vpu/mali-vpu-scheduler.c         |  248 +++
 .../arm/mali-vpu/mali-vpu-scheduler.h         |   58 +
 .../platform/arm/mali-vpu/mali-vpu-session.c  | 1260 +++++++++++++
 .../platform/arm/mali-vpu/mali-vpu-session.h  |  174 ++
 .../platform/arm/mali-vpu/mali-vpu-state.h    |  136 ++
 .../arm/mali-vpu/mali-vpu-variant-v76.c       |  188 ++
 .../platform/arm/mali-vpu/mali-vpu-variant.h  |   74 +
 .../platform/arm/mali-vpu/mali-vpu-vb2.c      |  187 ++
 .../platform/arm/mali-vpu/mali-vpu-vb2.h      |   13 +
 .../platform/arm/mali-vpu/mali-vpu-vidioc.c   |  365 ++++
 .../platform/arm/mali-vpu/mali-vpu-vidioc.h   |   17 +
 .../uapi/linux/media/arm/mali-vpu-config.h    |   47 +
 include/uapi/linux/v4l2-controls.h            |    6 +
 39 files changed, 9367 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/arm,mali-v76.yaml
 create mode 100644 drivers/media/platform/arm/mali-vpu/Kconfig
 create mode 100644 drivers/media/platform/arm/mali-vpu/Makefile
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-buffer.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-controls.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-controls.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-dec.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-dec.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-device.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-firmware.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-formats.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-formats.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-lsid.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-lsid.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-mmu.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-probe.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-protocol-def-v3.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-registers.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-scheduler.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-scheduler.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-session.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-session.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-state.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-variant-v76.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-variant.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-vb2.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-vb2.h
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.c
 create mode 100644 drivers/media/platform/arm/mali-vpu/mali-vpu-vidioc.h
 create mode 100644 include/uapi/linux/media/arm/mali-vpu-config.h
#61 lore raw

Hi Nicolas,

Thanks for reviewing the series.

Quoted 7 lines · Nicolas Dufresne
Quoted 7 lines

[5] Fluster was run with

GStreamer-H.264-V4L2 decoder:

Running test suite JVT-AVC_V1 with decoder GStreamer-H.264-V4L2 Ran 78/135 tests successfully.

Quoted 8 lines
Quoted 4 lines

The failing tests:

52 test vectors fail as they are interlaced clips which mali-vpu does not support.

Is that a real hardware limitation, or you are just skipping it in firmware or even in this driver? Its not like you have a compliant decoder without it. I've recently asked Amlogic to not cut corner also, and they accepted to fix their new driver.

V76 supports interlaced decoding, but it was not exposed in the initial driver submission because the capability is being phased out in later hardware generations. In v2, I’ll enable it through a variant-specific capability flag, allowing it to be disabled for future hardware generations that do not support it.

Quoted 8 lines
Quoted 4 lines

1 additional test vector, CVFC1_Sony_C completes decoding but fails checksum verification due to a stride-handling issue in GStreamer 1.24.13. GStreamer replaces the driver-reported 150-byte planar chroma stride with 152 bytes, causing the decoded chroma planes to be read incorrectly.

1.24 is very old by now, but I think this specific file needs more work. One part of the fix has been made recently:

https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/13059

Thanks for the pointer. I retested CVFC1_Sony_C with GStreamer 1.28.9 and the 1.29.2 development snapshot, both with that fix backported. The Fluster GStreamer backend still reports a checksum mismatch in both cases.

Decoding CVFC1_Sony_C with our V4L2 test application produces all 50 frames and matches Fluster’s expected checksum. This points to a remaining issue in the GStreamer path in this setup. I'll update the v2 cover letter to describe these results.

Regards,
Charvi

A message that is not in the archive (sent off-list or not yet received): [email protected]

#63 lore raw

Hi,

Quoted 2 lines · Patchwork Integration

# Test virtme64:test-virtme
Error running test. Final Summary not found

The virtme64 log shows vim2m passing all 68 compliance tests, including streaming tests. The kernel then panics in kobject_delayed_cleanup after unloading vim2m, before test-media can produce its final summary. This appears unrelated to mali-vpu. Not sure if this is a known issue.

Quoted 23 lines

# Test build-basic:x86
In file included from ./include/linux/device.h:15,
from drivers/media/platform/arm/mali-vpu/mali-vpu-device.h:9,
from drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c:9:
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c: In function ‘mali_vpu_fw_parse_header’:
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c:111:25: warning: format ‘%zu’ expects argument of type ‘size_t’, but argument 4 has type ‘long unsigned int’ [-Wformat=]

   111 |                         "invalid firmware BSS bitmap size: %u (max %zu)\n",
       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

./include/linux/dev_printk.h:110:30: note: in definition of macro ‘dev_printk_index_wrap’
110 | _p_func(dev, fmt, ##__VA_ARGS__); \
| ^~~
./include/linux/dev_printk.h:154:56: note: in expansion of macro ‘dev_fmt’
154 | dev_printk_index_wrap(_dev_err, KERN_ERR, dev, dev_fmt(fmt), ##__VA_ARGS__)
| ^~~~~~~
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c:110:17: note: in expansion of macro ‘dev_err’

   110 |                 dev_err(vpu_fw_bin->cache->dev,
       |                 ^~~~~~~
drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c:111:70: note: format string is defined here
   111 |                         "invalid firmware BSS bitmap size: %u (max %zu)\n",
       |                                                                    ~~^
       |                                                                      |
       |                                                                      unsigned int
       |                                                                    %lu
Quoted 1 line

drivers/media/platform/arm/mali-vpu/mali-vpu-firmware-cache.c:111:25: warning: format ‘%zu’ expects argument of type ‘size_t’, but argument 4 has type ‘long unsigned int’ [-Wformat=]

This is a valid warning caused by the 32UL multiplier. I'll fix it in v2.

Quoted 2 lines

# Test cocci:test-coccinelle ./platform/arm/mali-vpu/mali-vpu-session.c:1159:0-1: preceding lock on line 1084

This looks like a false positive. In mali_vpu_session_fw_bin_ready(), both the lock and unlock are conditional on !same_thread. A synchronous callback runs with the caller's lock already held; an asynchronous callback acquires and releases it itself, including on error paths.

Quoted 1 line

./platform/arm/mali-vpu/mali-vpu-session.c:671:3-9: preceding lock on line 668

Appears to be false positive as well. mali_vpu_session_wait_pending() requires sess->vq_lock to be held on entry and returns with it held. It temporarily releases the lock while waiting, then reacquires it before checking sess->error and returning. The reported return therefore preserves the caller's lock ownership.

Quoted 2 lines

For more details, check the full report at:
https://linux-media.pages.freedesktop.org/-/users/patchwork/-/jobs/116657798/artifacts/report.htm .

Regards,
Charvi