Skip to contentSkip to conversation

soc: mediatek: add MMDVFS support for MT8196

PATCH NAK Echo Zhang 5 participants 23 messages Nov 25

Subsystems (3)
lore
Share & tools
lore

Echo Zhang

author
#0 lore raw

cover soc: mediatek: add MMDVFS support for MT8196

This series adds support for the MediaTek Multimedia Dynamic Voltage and Frequency Scaling (MMDVFS) controller on MT8196. The controller provides virtual clocks for multimedia hardware blocks and coordinates their performance requests with platform voltage resources. The driver aggregates consumer rate requests and programs the MT8196 hardware clock control fabric (HWCCF) voter. Its VCP/MMUP backend coordinates shared memory and IPI communication with firmware.

The series introduces the clock IDs and device tree bindings, followed by the common clock provider, VCP backend and MT8196 topology and voting data. It also adds a read-only debugfs status view and a MAINTAINERS entry.

This series depends on Xiangzhi Tang's MediaTek VCP remoteproc series, which provides the public VCP API used by MMDVFS:
https://lore.kernel.org/r/[email protected]

Patch layout:
1/7: Define the MT8196 MMDVFS clock IDs used by DT consumers. 2/7: Document the common MMDVFS and MT8196 DT bindings. 3/7: Add the common driver, virtual clock provider and vote handling. 4/7: Add the VCP/MMUP shared-memory and IPI backend.
5/7: Add MT8196 topology data and HWCCF voting operations. 6/7: Add a read-only debugfs status view.
7/7: Add the MMDVFS files and contacts to MAINTAINERS.

Validation: the two MMDVFS bindings passed dt_binding_check. Each driver commit built cumulatively with arm64 Clang on top of the VCP dependency. The production changes were also ported to the MT8196 sapphire validation tree, retaining its older VCP API compatibility and separate internal test hooks. Its full package build and board boot passed. The status interface reported initialization with three resource controllers, seven muxes. The user, vote_step and force_step test completed with zero functional failures;

Echo Zhang (7):
dt-bindings: clock: mediatek: add MT8196 MMDVFS clock IDs
dt-bindings: soc: mediatek: add MMDVFS binding
soc: mediatek: add MMDVFS core driver
soc: mediatek: mmdvfs: add VCP backend
soc: mediatek: mmdvfs: add MT8196 support
soc: mediatek: mmdvfs: add read-only status debug
MAINTAINERS: add MediaTek MMDVFS entry

 .../soc/mediatek/mediatek,mmdvfs.yaml         |   60 +
 .../soc/mediatek/mediatek,mt8196-mmdvfs.yaml  |   87 ++
 MAINTAINERS                                   |   11 +
 drivers/soc/mediatek/Kconfig                  |   19 +
 drivers/soc/mediatek/Makefile                 |    1 +
 drivers/soc/mediatek/mmdvfs/Makefile          |   10 +
 drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c      |  447 +++++++
 drivers/soc/mediatek/mmdvfs/mmdvfs-core.c     |  651 ++++++++++
 drivers/soc/mediatek/mmdvfs/mmdvfs-debug.c    |  200 +++
 drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h |  361 ++++++
 drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c   |  637 ++++++++++
 drivers/soc/mediatek/mmdvfs/mmdvfs-up-priv.h  |   57 +
 drivers/soc/mediatek/mmdvfs/mmdvfs-up.c       |  275 +++++
 .../soc/mediatek/mmdvfs/mmdvfs-vcp-memory.h   |  104 ++
 drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c      | 1075 +++++++++++++++++
 .../clock/mediatek,mt8196-mmdvfs.h            |   74 ++
 include/soc/mediatek/mmdvfs.h                 |   90 ++
 17 files changed, 4159 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml
 create mode 100644 Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml
 create mode 100644 drivers/soc/mediatek/mmdvfs/Makefile
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-core.c
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-debug.c
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-up-priv.h
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-up.c
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-vcp-memory.h
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c
 create mode 100644 include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h
 create mode 100644 include/soc/mediatek/mmdvfs.h
Signature · 1 line
2.45.2
author
#1 lore raw

PATCH 1/7 dt-bindings: clock: mediatek: add MT8196 MMDVFS clock IDs

Add clock IDs for the MT8196 MMDVFS clock provider.

These IDs describe the clocks exposed by the MMDVFS block and are used by device tree consumers to request multimedia performance levels through the standard clock framework.

---
 .../clock/mediatek,mt8196-mmdvfs.h            | 74 +++++++++++++++++++
 1 file changed, 74 insertions(+)
 create mode 100644 include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h
include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h new file +74 -0
new file mode 100644
index 000000000000..428d8a35abdb
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+/*
+ * Copyright (c) 2024 MediaTek Inc.
+ * Author: MediaTek MMDVFS Team
+ */
+
+#ifndef _DT_BINDINGS_MMDVFS_CLK_MT8196_H
+#define _DT_BINDINGS_MMDVFS_CLK_MT8196_H
+
+/*
+ * MMDVFS Clock IDs for MT8196
+ *
+ * These IDs are used by multimedia clients in device tree to reference
+ * MMDVFS virtual clocks for DVFS voting.
+ *
+ * MT8196 uses HWCCF (Hardware Clock Control Fabric) architecture.
+ *
+ * Usage in device tree:
+ *   clocks = <&mmdvfs MMDVFS_USER_VDEC>;
+ *   clock-names = "mmdvfs";
+ */
+
+/* Core user clocks used by multimedia hardware drivers */
+#define MMDVFS_USER_DISP	0	/* user-disp */
+#define MMDVFS_USER_MDP		1	/* user-mdp */
+#define MMDVFS_USER_MML		2	/* user-mml */
+#define MMDVFS_USER_SMI		3	/* user-smi */
+#define MMDVFS_USER_JPEGDEC	4	/* user-jpegdec */
+#define MMDVFS_USER_JPEGENC	5	/* user-jpegenc */
+#define MMDVFS_USER_VENC	6	/* user-venc */
+#define MMDVFS_USER_VFMT	7	/* user-vfmt */
+#define MMDVFS_USER_IMG		8	/* user-img */
+#define MMDVFS_USER_CAM		9	/* user-cam */
+#define MMDVFS_USER_VCORE	10	/* user-vcore */
+#define MMDVFS_USER_VMM		11	/* user-vmm */
+#define MMDVFS_USER_VDISP	12	/* user-vdisp */
+#define MMDVFS_USER_VDEC	13	/* user-vdec */
+#define DUMMY_USER_DIS		14	/* dummy-dis */
+#define DUMMY_USER_MDP		15	/* dummy-mdp */
+#define DUMMY_USER_MML		16	/* dummy-mml */
+#define DUMMY_USER_SMI		17	/* dummy-smi */
+#define DUMMY_USER_VEN		18	/* dummy-ven */
+#define DUMMY_USER_VDE		19	/* dummy-vde */
+#define DUMMY_USER_IMG		20	/* dummy-img */
+#define DUMMY_USER_CAM		21	/* dummy-cam */
+#define MMDVFS_USER_CAM_SMI	22	/* user-cam-smi */
+#define MMDVFS_USER_VCP_SMI	23	/* user-vcp-smi */
+
+/* AP voter clocks (24-33) - System level control */
+#define MMDVFS_AP_CAM		24
+#define MMDVFS_AP_CAMTM		25
+#define MMDVFS_AP_VDE		26
+#define MMDVFS_AP_IMG		27
+#define MMDVFS_AP_IPE		28
+#define MMDVFS_AP_DIS		29
+#define MMDVFS_AP_MML		30
+#define MMDVFS_AP_SMI		31
+#define MMDVFS_AP_VEN		32
+#define MMDVFS_AP_MDP		33
+
+/* VCP voter clocks (34-43) - VCP firmware control */
+#define MMDVFS_VCP_CAM		34
+#define MMDVFS_VCP_CAMTM	35
+#define MMDVFS_VCP_VDE		36
+#define MMDVFS_VCP_IMG		37
+#define MMDVFS_VCP_IPE		38
+#define MMDVFS_VCP_DIS		39
+#define MMDVFS_VCP_MML		40
+#define MMDVFS_VCP_SMI		41
+#define MMDVFS_VCP_VEN		42
+#define MMDVFS_VCP_MDP		43
+
+#define MMDVFS_USER_NUM		44
+#endif /* _DT_BINDINGS_MMDVFS_CLK_MT8196_H */
Signature · 1 line
2.45.2
#2 lore raw

Re: [PATCH 1/7] dt-bindings: clock: mediatek: add MT8196 MMDVFS clock IDs

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] dt-bindings: clock: mediatek: software DVFS voters and dummy IDs in binding - [Low] dt-bindings…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] dt-bindings: clock: mediatek: software DVFS voters and dummy IDs in binding
- [Low] dt-bindings: clock: mediatek: clock count macro MMDVFS_USER_NUM in binding
--

commit 8ccadc25b913bfdd48ee17134dbe19aa8303b626
Author: Echo Zhang <[email protected]>

dt-bindings: clock: mediatek: add MT8196 MMDVFS clock IDs

This commit adds Devicetree clock binding IDs for the MediaTek MT8196 MMDVFS clock provider.

Quoted 6 lines
include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h new file +0 -0
new file mode 100644
index 0000000000000..428d8a35abdba
@@ -0,0 +1,74 @@

[ ... ]

Quoted 39 lines
include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h new file +39 -0
+/* Core user clocks used by multimedia hardware drivers */
+#define MMDVFS_USER_DISP	0	/* user-disp */
+#define MMDVFS_USER_MDP		1	/* user-mdp */
+#define MMDVFS_USER_MML		2	/* user-mml */
+#define MMDVFS_USER_SMI		3	/* user-smi */
+#define MMDVFS_USER_JPEGDEC	4	/* user-jpegdec */
+#define MMDVFS_USER_JPEGENC	5	/* user-jpegenc */
+#define MMDVFS_USER_VENC	6	/* user-venc */
+#define MMDVFS_USER_VFMT	7	/* user-vfmt */
+#define MMDVFS_USER_IMG		8	/* user-img */
+#define MMDVFS_USER_CAM		9	/* user-cam */
+#define MMDVFS_USER_VCORE	10	/* user-vcore */
+#define MMDVFS_USER_VMM		11	/* user-vmm */
+#define MMDVFS_USER_VDISP	12	/* user-vdisp */
+#define MMDVFS_USER_VDEC	13	/* user-vdec */
+#define DUMMY_USER_DIS		14	/* dummy-dis */
+#define DUMMY_USER_MDP		15	/* dummy-mdp */
+#define DUMMY_USER_MML		16	/* dummy-mml */
+#define DUMMY_USER_SMI		17	/* dummy-smi */
+#define DUMMY_USER_VEN		18	/* dummy-ven */
+#define DUMMY_USER_VDE		19	/* dummy-vde */
+#define DUMMY_USER_IMG		20	/* dummy-img */
+#define DUMMY_USER_CAM		21	/* dummy-cam */
+#define MMDVFS_USER_CAM_SMI	22	/* user-cam-smi */
+#define MMDVFS_USER_VCP_SMI	23	/* user-vcp-smi */
+
+/* AP voter clocks (24-33) - System level control */
+#define MMDVFS_AP_CAM		24
+#define MMDVFS_AP_CAMTM		25
+#define MMDVFS_AP_VDE		26
+#define MMDVFS_AP_IMG		27
+#define MMDVFS_AP_IPE		28
+#define MMDVFS_AP_DIS		29
+#define MMDVFS_AP_MML		30
+#define MMDVFS_AP_SMI		31
+#define MMDVFS_AP_VEN		32
+#define MMDVFS_AP_MDP		33
+
+/* VCP voter clocks (34-43) - VCP firmware control */

include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h line 71

Quoted 10 lines
include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h new file +10 -0
+#define MMDVFS_VCP_CAM		34
+#define MMDVFS_VCP_CAMTM	35
+#define MMDVFS_VCP_VDE		36
+#define MMDVFS_VCP_IMG		37
+#define MMDVFS_VCP_IPE		38
+#define MMDVFS_VCP_DIS		39
+#define MMDVFS_VCP_MML		40
+#define MMDVFS_VCP_SMI		41
+#define MMDVFS_VCP_VEN		42
+#define MMDVFS_VCP_MDP		43

[Severity: Medium]
Should virtual DVFS voting channels, voltage rail voters (MMDVFS_USER_VCORE, MMDVFS_USER_VMM, MMDVFS_USER_VDISP), unprefixed dummy placeholders (DUMMY_USER_DIS through DUMMY_USER_CAM), and duplicate AP/VCP software voter slots be exposed as Devicetree clock binding IDs?

Devicetree bindings describe hardware rather than software voting channels. The actual MT8196 multimedia hardware clocks (such as CLK_TOP2_IMG1, CLK_TOP2_CAM, CLK_TOP2_VDEC, CLK_TOP2_VENC, CLK_TOP2_DISP, and CLK_TOP2_MDP) are already defined in include/dt-bindings/clock/mediatek,mt8196-clock.h, whereas DVFS operating points, voltage rails, and bandwidth requests are normally described with OPP, regulator, and interconnect bindings.

include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h line 74

Quoted 3 lines
include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h new file +3 -0
+
+#define MMDVFS_USER_NUM		44
+#endif /* _DT_BINDINGS_MMDVFS_CLK_MT8196_H */

[Severity: Low]
Should MMDVFS_USER_NUM be dropped from the Devicetree binding header? Count macros are not clock IDs, and defining the total count in include/dt-bindings/ means adding any new ID later changes the macro value and breaks the binding ABI.

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

Re: [PATCH 1/7] dt-bindings: clock: mediatek: add MT8196 MMDVFS clock IDs

In reply to Echo Zhang
Quoted 9 lines · Echo Zhang

Add clock IDs for the MT8196 MMDVFS clock provider.

These IDs describe the clocks exposed by the MMDVFS block and are used by device tree consumers to request multimedia performance levels through the standard clock framework.

---
 .../clock/mediatek,mt8196-mmdvfs.h            | 74 +++++++++++++++++++

This is not a separate commit.

Quoted 72 lines
 1 file changed, 74 insertions(+)
 create mode 100644 include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h
include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h new file +63 -0
new file mode 100644
index 000000000000..428d8a35abdb
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+/*
+ * Copyright (c) 2024 MediaTek Inc.
+ * Author: MediaTek MMDVFS Team
+ */
+
+#ifndef _DT_BINDINGS_MMDVFS_CLK_MT8196_H
+#define _DT_BINDINGS_MMDVFS_CLK_MT8196_H
+
+/*
+ * MMDVFS Clock IDs for MT8196
+ *
+ * These IDs are used by multimedia clients in device tree to reference
+ * MMDVFS virtual clocks for DVFS voting.
+ *
+ * MT8196 uses HWCCF (Hardware Clock Control Fabric) architecture.
+ *
+ * Usage in device tree:
+ *   clocks = <&mmdvfs MMDVFS_USER_VDEC>;
+ *   clock-names = "mmdvfs";
+ */
+
+/* Core user clocks used by multimedia hardware drivers */
+#define MMDVFS_USER_DISP	0	/* user-disp */
+#define MMDVFS_USER_MDP		1	/* user-mdp */
+#define MMDVFS_USER_MML		2	/* user-mml */
+#define MMDVFS_USER_SMI		3	/* user-smi */
+#define MMDVFS_USER_JPEGDEC	4	/* user-jpegdec */
+#define MMDVFS_USER_JPEGENC	5	/* user-jpegenc */
+#define MMDVFS_USER_VENC	6	/* user-venc */
+#define MMDVFS_USER_VFMT	7	/* user-vfmt */
+#define MMDVFS_USER_IMG		8	/* user-img */
+#define MMDVFS_USER_CAM		9	/* user-cam */
+#define MMDVFS_USER_VCORE	10	/* user-vcore */
+#define MMDVFS_USER_VMM		11	/* user-vmm */
+#define MMDVFS_USER_VDISP	12	/* user-vdisp */
+#define MMDVFS_USER_VDEC	13	/* user-vdec */
+#define DUMMY_USER_DIS		14	/* dummy-dis */
+#define DUMMY_USER_MDP		15	/* dummy-mdp */
+#define DUMMY_USER_MML		16	/* dummy-mml */
+#define DUMMY_USER_SMI		17	/* dummy-smi */
+#define DUMMY_USER_VEN		18	/* dummy-ven */
+#define DUMMY_USER_VDE		19	/* dummy-vde */
+#define DUMMY_USER_IMG		20	/* dummy-img */
+#define DUMMY_USER_CAM		21	/* dummy-cam */
+#define MMDVFS_USER_CAM_SMI	22	/* user-cam-smi */
+#define MMDVFS_USER_VCP_SMI	23	/* user-vcp-smi */
+
+/* AP voter clocks (24-33) - System level control */
+#define MMDVFS_AP_CAM		24
+#define MMDVFS_AP_CAMTM		25
+#define MMDVFS_AP_VDE		26
+#define MMDVFS_AP_IMG		27
+#define MMDVFS_AP_IPE		28
+#define MMDVFS_AP_DIS		29
+#define MMDVFS_AP_MML		30
+#define MMDVFS_AP_SMI		31
+#define MMDVFS_AP_VEN		32
+#define MMDVFS_AP_MDP		33
+
+/* VCP voter clocks (34-43) - VCP firmware control */
+#define MMDVFS_VCP_CAM		34
+#define MMDVFS_VCP_CAMTM	35

include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h line 73

Quoted 10 lines
include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h new file +10 -0
+#define MMDVFS_VCP_VDE		36
+#define MMDVFS_VCP_IMG		37
+#define MMDVFS_VCP_IPE		38
+#define MMDVFS_VCP_DIS		39
+#define MMDVFS_VCP_MML		40
+#define MMDVFS_VCP_SMI		41
+#define MMDVFS_VCP_VEN		42
+#define MMDVFS_VCP_MDP		43
+
+#define MMDVFS_USER_NUM		44

Not a binding, drop.

Are you sending us some downstream old driver? Which upstream code was used as a base/example for this work?

Best regards,
Krzysztof

author
#4 lore raw

PATCH 2/7 dt-bindings: soc: mediatek: add MMDVFS binding

In reply to Echo Zhang

Describe the MMDVFS clock provider and its connection to VCP firmware in a common binding. Document the optional MMUP shared memory and IPI settings used by the firmware backend.

Add an MT8196 binding for the HWCCF syscon and seven mux OPP tables so the driver can map clock rate requests to hardware votes.

---
 .../soc/mediatek/mediatek,mmdvfs.yaml         | 60 +++++++++++++
 .../soc/mediatek/mediatek,mt8196-mmdvfs.yaml  | 87 +++++++++++++++++++
 2 files changed, 147 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml
 create mode 100644 Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml
Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml new file +60 -0
new file mode 100644
index 000000000000..d65f4c9084cc
@@ -0,0 +1,60 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/soc/mediatek/mediatek,mmdvfs.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek Multimedia Dynamic Voltage and Frequency Scaling (MMDVFS)
+
+maintainers:
+  - MediaTek MMDVFS Team <[email protected]>
+
+description:
+  MediaTek MMDVFS provides dynamic voltage and frequency scaling for multimedia
+  hardware accelerators. The controller exposes virtual clocks that multimedia
+  consumers use through the common clock framework. The driver aggregates
+  consumer rate requests and sends the resulting DVFS votes to the multimedia
+  hardware voter and VCP firmware.
+
+select: false
+
+properties:
+  mediatek,vcp:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description:
+      Phandle to the MediaTek VCP remote processor used for MMDVFS shared
+      memory, feature registration, notifier, and IPI operations.
+
+  mediatek,mmup-sram:
+    type: boolean
+    description:
+      Use the MMUP SRAM window for MMDVFS callback records and status data.
+
+  mediatek,mmup-enable:
+    type: boolean
+    description:
+      Enable the MMUP path for MMDVFS shared memory and IPI handling.
+
+  mediatek,vcp-ipi-id:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      VCP IPI channel id for MMDVFS. If omitted, the driver uses the platform
+      default.
+
+  mediatek,mmup-ipi-id:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      MMUP IPI channel id for MMDVFS. If omitted, the driver uses the platform
+      default.
+
+  "#clock-cells":
+    const: 1
+    description:
+      MMDVFS clock specifier. Consumers pass an SoC-specific MMDVFS clock
+      index.
+
+required:
+  - mediatek,vcp
+  - "#clock-cells"
+
+additionalProperties: true
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +87 -0

87 lines not on this page · raw message

Signature · 1 line
2.45.2
#5 lore raw

Re: [PATCH 2/7] dt-bindings: soc: mediatek: add MMDVFS binding

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [Low] dt-bindings: soc: mediatek: fixed-length mux-opp-tables lacks items list - [Low] dt-bindings: soc…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [Low] dt-bindings: soc: mediatek: fixed-length mux-opp-tables lacks items list
- [Low] dt-bindings: soc: mediatek: redundant word binding in commit subject
- [Low] dt-bindings: soc: mediatek: undocumented provider nodes in example
- [Low] dt-bindings: soc: mediatek: #clock-cells ordered after vendor properties
- [Low] dt-bindings: soc: mediatek: redundant select block and mailing list maintainer
--

commit 6ff5784de956cad70abff1bec7106c11df1f2345
Author: Echo Zhang <[email protected]>

dt-bindings: soc: mediatek: add MMDVFS binding

This commit adds the common MediaTek MMDVFS DT binding schema along with the MT8196-specific MMDVFS binding for the HWCCF syscon and mux OPP tables.

Quoted 1 line

dt-bindings: soc: mediatek: add MMDVFS binding

[Severity: Low]
The "dt-bindings:" prefix already indicates that this patch adds a binding, so the trailing word "binding" in the subject line is redundant and can be dropped.

Quoted 16 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml new file +10 -0
new file mode 100644
index 0000000000000..d65f4c9084cc0
@@ -0,0 +1,60 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/soc/mediatek/mediatek,mmdvfs.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek Multimedia Dynamic Voltage and Frequency Scaling (MMDVFS)
+
+maintainers:
+  - MediaTek MMDVFS Team <[email protected]>

[ ... ]

Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml line 58

Quoted 9 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml new file +9 -0
+  "#clock-cells":
+    const: 1
+    description:
+      MMDVFS clock specifier. Consumers pass an SoC-specific MMDVFS clock
+      index.
+
+required:
+  - mediatek,vcp
+  - "#clock-cells"

[Severity: Low]
Standard properties without vendor prefixes such as "#clock-cells" should be listed before vendor-specific "mediatek,*" properties in both
properties: and required: (and in the example node in mediatek,mt8196-mmdvfs.yaml).

Quoted 18 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +12 -0
new file mode 100644
index 0000000000000..13381c78dbd19
@@ -0,0 +1,87 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/soc/mediatek/mediatek,mt8196-mmdvfs.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT8196 Multimedia Dynamic Voltage and Frequency Scaling
+
+maintainers:
+  - MediaTek MMDVFS Team <[email protected]>
+
+allOf:

Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml line 22

Quoted 10 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +10 -0
+  - $ref: mediatek,mmdvfs.yaml#
+
+select:
+  properties:
+    compatible:
+      contains:
+        enum:
+          - mediatek,mt8196-mmdvfs
+  required:
+    - compatible

[Severity: Low]
Is this explicit select: block needed? dtschema automatically generates an identical select rule from the compatible property below.

Also, maintainers: in both mediatek,mt8196-mmdvfs.yaml and mediatek,mmdvfs.yaml lists a mailing list rather than the individual maintainer(s) responsible for the binding.

Quoted 10 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +10 -0
+
+properties:
+  compatible:
+    enum:
+      - mediatek,mt8196-mmdvfs
+
+  mediatek,hwccf:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description:
+      Phandle to the MT8196 multimedia hardware clock control fabric syscon

Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml line 42

Quoted 10 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +10 -0
+      used for AP-side hardware votes.
+
+  mediatek,mux-opp-tables:
+    $ref: /schemas/types.yaml#/definitions/phandle-array
+    minItems: 7
+    maxItems: 7
+    description: |
+      OPP tables for the seven MT8196 MMDVFS muxes, in this order:
+      display, MDP, SMI, VENC, VDEC, IMG, CAM. MDP and MML share
+      the MDP mux. Each OPP entry must have opp-hz and opp-level.

[Severity: Low]
Since mediatek,mux-opp-tables has a fixed set of seven ordered entries, could it define an items: list with a per-entry description for each mux instead of using minItems/maxItems and enumerating the entries and count in the description text?

Quoted 35 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +35 -0
+
+required:
+  - compatible
+  - mediatek,hwccf
+  - mediatek,mux-opp-tables
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/mediatek,mt8196-mmdvfs.h>
+
+    soc {
+        #address-cells = <2>;
+        #size-cells = <2>;
+
+        vcp: remoteproc@31800000 {
+            compatible = "mediatek,mt8196-vcp";
+            reg = <0 0x31800000 0 0x100000>;
+        };
+
+        mm_hwv: syscon@31b00000 {
+            compatible = "mediatek,mt8196-mm-hwv", "syscon";
+            reg = <0 0x31b00000 0 0x6000>;
+        };
+
+        mmdvfs_opp: opp-table {
+            compatible = "operating-points-v2";
+            opp-0 {
+                opp-hz = /bits/ 64 <100000000>;
+                opp-level = <0>;
+            };
+        };
+
+        mmdvfs: mmdvfs-mux {

Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml line 87

Quoted 10 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +10 -0
+            compatible = "mediatek,mt8196-mmdvfs";
+            mediatek,vcp = <&vcp>;
+            mediatek,hwccf = <&mm_hwv>;
+            mediatek,mux-opp-tables = <&mmdvfs_opp>, <&mmdvfs_opp>,
+                                      <&mmdvfs_opp>, <&mmdvfs_opp>,
+                                      <&mmdvfs_opp>, <&mmdvfs_opp>,
+                                      <&mmdvfs_opp>;
+            #clock-cells = <1>;
+        };
+    };

[Severity: Low]
Does this example pass make dt_binding_check?

The remoteproc@31800000 and syscon@31b00000 provider nodes use undocumented compatibles ("mediatek,mt8196-vcp" and "mediatek,mt8196-mm-hwv"), causing syscon.yaml validation to fail on syscon@31b00000. Phandles in examples do not need to be resolvable, so those unrelated provider nodes (and the outer soc wrapper) can be dropped.

In addition, <dt-bindings/clock/mediatek,mt8196-mmdvfs.h> and the "mmdvfs:" label on mmdvfs-mux are unused, and DTS coding style requires a blank line before the opp-0 child node.

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

Re: [PATCH 2/7] dt-bindings: soc: mediatek: add MMDVFS binding

In reply to Echo Zhang

Hi,

Quoted 21 lines · Echo Zhang

Describe the MMDVFS clock provider and its connection to VCP firmware in a common binding. Document the optional MMUP shared memory and IPI settings used by the firmware backend.

Add an MT8196 binding for the HWCCF syscon and seven mux OPP tables so the driver can map clock rate requests to hardware votes.

---
 .../soc/mediatek/mediatek,mmdvfs.yaml         | 60 +++++++++++++
 .../soc/mediatek/mediatek,mt8196-mmdvfs.yaml  | 87 +++++++++++++++++++
 2 files changed, 147 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml
 create mode 100644 Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml
Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml new file +0 -0
new file mode 100644
index 000000000000..d65f4c9084cc

Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml line 10

Quoted 10 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml new file +10 -0
@@ -0,0 +1,60 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/soc/mediatek/mediatek,mmdvfs.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek Multimedia Dynamic Voltage and Frequency Scaling (MMDVFS)
+
+maintainers:
+  - MediaTek MMDVFS Team <[email protected]>

Maintainers should be real people, not a mailing list.

Quoted 56 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml new file +50 -0
+
+description:
+  MediaTek MMDVFS provides dynamic voltage and frequency scaling for multimedia
+  hardware accelerators. The controller exposes virtual clocks that multimedia
+  consumers use through the common clock framework. The driver aggregates
+  consumer rate requests and sends the resulting DVFS votes to the multimedia
+  hardware voter and VCP firmware.
+
+select: false
+
+properties:
+  mediatek,vcp:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description:
+      Phandle to the MediaTek VCP remote processor used for MMDVFS shared
+      memory, feature registration, notifier, and IPI operations.
+
+  mediatek,mmup-sram:
+    type: boolean
+    description:
+      Use the MMUP SRAM window for MMDVFS callback records and status data.
+
+  mediatek,mmup-enable:
+    type: boolean
+    description:
+      Enable the MMUP path for MMDVFS shared memory and IPI handling.
+
+  mediatek,vcp-ipi-id:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      VCP IPI channel id for MMDVFS. If omitted, the driver uses the platform
+      default.
+
+  mediatek,mmup-ipi-id:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      MMUP IPI channel id for MMDVFS. If omitted, the driver uses the platform
+      default.
+
+  "#clock-cells":
+    const: 1
+    description:
+      MMDVFS clock specifier. Consumers pass an SoC-specific MMDVFS clock
+      index.
+
+required:
+  - mediatek,vcp
+  - "#clock-cells"
+
+additionalProperties: true
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +0 -0
new file mode 100644
index 000000000000..13381c78dbd1

Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml line 7

Quoted 7 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +7 -0
@@ -0,0 +1,87 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/soc/mediatek/mediatek,mt8196-mmdvfs.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT8196 Multimedia Dynamic Voltage and Frequency Scaling

Why there's mediatek,mmdvfs.yaml file if this exists?

Quoted 5 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +5 -0
+
+maintainers:
+  - MediaTek MMDVFS Team <[email protected]>
+
+allOf:

Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml line 22

Quoted 10 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +10 -0
+  - $ref: mediatek,mmdvfs.yaml#
+
+select:
+  properties:
+    compatible:
+      contains:
+        enum:
+          - mediatek,mt8196-mmdvfs
+  required:
+    - compatible

Drop. select is automatically inferred from the compatible.

Best regards,
Roman

#7 lore raw

Re: [PATCH 2/7] dt-bindings: soc: mediatek: add MMDVFS binding

In reply to Echo Zhang

Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml line 19

Quoted 8 lines · Echo Zhang
Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml new file +8 -0
+description:
+  MediaTek MMDVFS provides dynamic voltage and frequency scaling for multimedia
+  hardware accelerators. The controller exposes virtual clocks that multimedia
+  consumers use through the common clock framework. The driver aggregates
+  consumer rate requests and sends the resulting DVFS votes to the multimedia
+  hardware voter and VCP firmware.
+
+select: false

What?

Quoted 50 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml new file +41 -0
+
+properties:
+  mediatek,vcp:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description:
+      Phandle to the MediaTek VCP remote processor used for MMDVFS shared
+      memory, feature registration, notifier, and IPI operations.
+
+  mediatek,mmup-sram:
+    type: boolean
+    description:
+      Use the MMUP SRAM window for MMDVFS callback records and status data.
+
+  mediatek,mmup-enable:
+    type: boolean
+    description:
+      Enable the MMUP path for MMDVFS shared memory and IPI handling.
+
+  mediatek,vcp-ipi-id:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      VCP IPI channel id for MMDVFS. If omitted, the driver uses the platform
+      default.
+
+  mediatek,mmup-ipi-id:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      MMUP IPI channel id for MMDVFS. If omitted, the driver uses the platform
+      default.
+
+  "#clock-cells":
+    const: 1
+    description:
+      MMDVFS clock specifier. Consumers pass an SoC-specific MMDVFS clock
+      index.
+
+required:
+  - mediatek,vcp
+  - "#clock-cells"
+
+additionalProperties: true
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +3 -0
new file mode 100644
index 000000000000..13381c78dbd1
@@ -0,0 +1,87 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---

Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml line 13

Quoted 10 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +10 -0
+$id: http://devicetree.org/schemas/soc/mediatek/mediatek,mt8196-mmdvfs.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: MediaTek MT8196 Multimedia Dynamic Voltage and Frequency Scaling
+
+maintainers:
+  - MediaTek MMDVFS Team <[email protected]>
+
+allOf:
+  - $ref: mediatek,mmdvfs.yaml#

What?

Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml line 22

Quoted 9 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +9 -0
+
+select:
+  properties:
+    compatible:
+      contains:
+        enum:
+          - mediatek,mt8196-mmdvfs
+  required:
+    - compatible

What?

Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml line 29

Quoted 7 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +7 -0
+
+properties:
+  compatible:
+    enum:
+      - mediatek,mt8196-mmdvfs
+
+  mediatek,hwccf:

clock control is via clocks, no?

Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml line 36

Quoted 7 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +7 -0
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description:
+      Phandle to the MT8196 multimedia hardware clock control fabric syscon
+      used for AP-side hardware votes.
+
+  mediatek,mux-opp-tables:
+    $ref: /schemas/types.yaml#/definitions/phandle-array

NAK, you are messing with us...

Quoted 22 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +22 -0
+    minItems: 7
+    maxItems: 7
+    description: |
+      OPP tables for the seven MT8196 MMDVFS muxes, in this order:
+      display, MDP, SMI, VENC, VDEC, IMG, CAM. MDP and MML share
+      the MDP mux. Each OPP entry must have opp-hz and opp-level.
+
+required:
+  - compatible
+  - mediatek,hwccf
+  - mediatek,mux-opp-tables
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/mediatek,mt8196-mmdvfs.h>
+
+    soc {
+        #address-cells = <2>;
+        #size-cells = <2>;
+

Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml line 68

Quoted 10 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +10 -0
+        vcp: remoteproc@31800000 {
+            compatible = "mediatek,mt8196-vcp";
+            reg = <0 0x31800000 0 0x100000>;
+        };
+
+        mm_hwv: syscon@31b00000 {
+            compatible = "mediatek,mt8196-mm-hwv", "syscon";
+            reg = <0 0x31b00000 0 0x6000>;
+        };
+

What are all these nodes? Drop.

Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml line 77

Quoted 9 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +9 -0
+        mmdvfs_opp: opp-table {
+            compatible = "operating-points-v2";
+            opp-0 {
+                opp-hz = /bits/ 64 <100000000>;
+                opp-level = <0>;
+            };
+        };
+
+        mmdvfs: mmdvfs-mux {

Node names should be generic. See also an explanation and list of examples (not exhaustive) in DT specification:
https://devicetree-specification.readthedocs.io/en/latest/chapter2-devicetree-basics.html#generic-names-recommendation
If you cannot find a name matching your device, please check in kernel sources for similar cases or you can grow the spec (via pull request to DT spec repo).

Honestly, not a separate device. You just added it for Linux drivers.

NAK for entire binding.

Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml line 87

Quoted 10 lines
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml new file +10 -0
+            compatible = "mediatek,mt8196-mmdvfs";
+            mediatek,vcp = <&vcp>;
+            mediatek,hwccf = <&mm_hwv>;
+            mediatek,mux-opp-tables = <&mmdvfs_opp>, <&mmdvfs_opp>,
+                                      <&mmdvfs_opp>, <&mmdvfs_opp>,
+                                      <&mmdvfs_opp>, <&mmdvfs_opp>,
+                                      <&mmdvfs_opp>;
+            #clock-cells = <1>;
+        };
+    };

Best regards,
Krzysztof

#8 lore raw

Re: [PATCH 2/7] dt-bindings: soc: mediatek: add MMDVFS binding

In reply to Echo Zhang
Quoted 11 lines · Echo Zhang

Describe the MMDVFS clock provider and its connection to VCP firmware in a common binding. Document the optional MMUP shared memory and IPI settings used by the firmware backend.

Add an MT8196 binding for the HWCCF syscon and seven mux OPP tables so the driver can map clock rate requests to hardware votes.

---
 .../soc/mediatek/mediatek,mmdvfs.yaml         | 60 +++++++++++++
 .../soc/mediatek/mediatek,mt8196-mmdvfs.yaml  | 87 +++++++++++++++++++

.... and this has build check failures :/

Best regards,
Krzysztof

Signature · 1 line
Krzysztof Kozlowski <[email protected]>
author
#9 lore raw

PATCH 3/7 soc: mediatek: add MMDVFS core driver

In reply to Echo Zhang

Add the common MediaTek MMDVFS driver framework.

The core driver handles device probing, SoC data selection, clock provider registration, power-management notification, and dispatch to the microprocessor transport used for runtime performance voting.

---
 drivers/soc/mediatek/Kconfig                  |  19 +
 drivers/soc/mediatek/Makefile                 |   1 +
 drivers/soc/mediatek/mmdvfs/Makefile          |   6 +
 drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c      | 462 ++++++++++++++
 drivers/soc/mediatek/mmdvfs/mmdvfs-core.c     | 581 ++++++++++++++++++
 drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h | 305 +++++++++
 include/soc/mediatek/mmdvfs.h                 |  72 +++
 7 files changed, 1446 insertions(+)
 create mode 100644 drivers/soc/mediatek/mmdvfs/Makefile
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-core.c
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h
 create mode 100644 include/soc/mediatek/mmdvfs.h
drivers/soc/mediatek/Kconfig +19 -0
index d7293977f06e..bb273a4461f0 100644
@@ -60,6 +60,25 @@ config MTK_REGULATOR_COUPLER
 	default ARCH_MEDIATEK
 	depends on REGULATOR
 
+config MTK_MMDVFS
+	tristate "MediaTek MMDVFS support"
+	depends on ARCH_MEDIATEK || COMPILE_TEST
+	depends on MTK_VCP_RPROC
+	help
+	  Say yes here to add support for the MediaTek Multimedia Dynamic
+	  Voltage and Frequency Scaling controller. The controller provides
+	  virtual clocks for multimedia hardware blocks and coordinates
+	  performance votes with platform voltage resources.
+
+config MTK_MMDVFS_MT8196
+	bool "MediaTek MT8196 MMDVFS support"
+	depends on MTK_MMDVFS
+	help
+	  Enable MMDVFS support for the MediaTek MT8196 platform.
+	  This adds the MT8196 multimedia clock voter topology and
+	  hardware voter operations used by the common MMDVFS driver.
+	  Select this option only on MT8196 based systems.
+
 config MTK_MMSYS
 	tristate "MediaTek MMSYS Support"
 	default ARCH_MEDIATEK
drivers/soc/mediatek/Makefile +1 -0
index 0665573e3c4b..f54ee4e2775c 100644
@@ -5,6 +5,7 @@ obj-$(CONFIG_MTK_DVFSRC) += mtk-dvfsrc.o
 obj-$(CONFIG_MTK_INFRACFG) += mtk-infracfg.o
 obj-$(CONFIG_MTK_PMIC_WRAP) += mtk-pmic-wrap.o
 obj-$(CONFIG_MTK_REGULATOR_COUPLER) += mtk-regulator-coupler.o
+obj-$(CONFIG_MTK_MMDVFS) += mmdvfs/
 obj-$(CONFIG_MTK_MMSYS) += mtk-mmsys.o
 obj-$(CONFIG_MTK_MMSYS) += mtk-mutex.o
 obj-$(CONFIG_MTK_SVS) += mtk-svs.o
drivers/soc/mediatek/mmdvfs/Makefile new file +6 -0
new file mode 100644
index 000000000000..33a280827006
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-$(CONFIG_MTK_MMDVFS) += mtk-mmdvfs.o
+
+mtk-mmdvfs-y += mmdvfs-core.o
+mtk-mmdvfs-y += mmdvfs-clk.o
drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c new file +462 -0

462 lines not on this page · raw message

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +581 -0

581 lines not on this page · raw message

drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h new file +305 -0

305 lines not on this page · raw message

include/soc/mediatek/mmdvfs.h new file +72 -0

72 lines not on this page · raw message

Signature · 1 line
2.45.2
#10 lore raw

Re: [PATCH 3/7] soc: mediatek: add MMDVFS core driver

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] soc: mediatek: mmdvfs: dependency on undefined Kconfig symbol MTK_VCP_RPROC - [High] soc: mediatek…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] soc: mediatek: mmdvfs: dependency on undefined Kconfig symbol MTK_VCP_RPROC
- [High] soc: mediatek: mmdvfs: premature MT8196 symbol references cause link failure
- [Medium] soc: mediatek: mmdvfs: UAF and teardown order bug in mmdvfs_remove()
- [Medium] soc: mediatek: mmdvfs: clocks remain active during deinit_hw() teardown
- [Low] soc: mediatek: mmdvfs: duplicate enum and declarations across headers
--

commit 5b3edaa4a79dffb92cf9cae0a066c56967485e24
Author: Echo Zhang <[email protected]>

soc: mediatek: add MMDVFS core driver

This commit adds the common MediaTek MMDVFS driver framework, including device probing, SoC topology validation, clock provider registration, power-management notification, and exported consumer helpers.

drivers/soc/mediatek/Kconfig config MTK_REGULATOR_COUPLER · line 66

Quoted 12 lines
drivers/soc/mediatek/Kconfig +4 -0
index d7293977f06e8..bb273a4461f0a 100644
@@ -60,6 +60,25 @@ config MTK_REGULATOR_COUPLER
 	default ARCH_MEDIATEK
 	depends on REGULATOR
 
+config MTK_MMDVFS
+	tristate "MediaTek MMDVFS support"
+	depends on ARCH_MEDIATEK || COMPILE_TEST
+	depends on MTK_VCP_RPROC

[Severity: High]
Is MTK_VCP_RPROC defined anywhere in the Kconfig tree? Because that symbol does not exist, CONFIG_MTK_MMDVFS and CONFIG_MTK_MMDVFS_MT8196 can never be enabled, and the IS_ENABLED(CONFIG_MTK_VCP_RPROC) guard in include/soc/mediatek/mmdvfs.h always selects the no-op inline stubs instead of the public declarations.

Quoted 5 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c new file +0 -0
new file mode 100644
index 0000000000000..114f3148cf461

[ ... ]

drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c line 462

Quoted 9 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c new file +9 -0
+/*
+ * mmdvfs_unregister_clocks - Unregister clock providers
+ * @mmdvfs: MMDVFS device structure
+ */
+void mmdvfs_unregister_clocks(struct mmdvfs_device *mmdvfs)
+{
+	if (mmdvfs && mmdvfs->dev)
+		mmdvfs_dbg(mmdvfs->dev, "MMDVFS clocks unregistered\n");
+}

[Severity: Medium]
Can clock consumers still call mmdvfs_set_rate() -> data->ops->hw_vote() while or after data->ops->deinit_hw() tears down the hardware?

mmdvfs_register_clocks() registers the clocks and OF provider with devm_clk_hw_register() and devm_of_clk_add_hw_provider(), while mmdvfs_unregister_clocks() only logs a debug message and leaves data->init_done set to true. When mmdvfs_remove() or the err_unregister_clocks path in mmdvfs_probe() calls mmdvfs_unregister_clocks() followed by data->ops->deinit_hw(), the devres- managed clock provider remains active until mmdvfs_remove() or mmdvfs_probe() returns.

Quoted 5 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +0 -0
new file mode 100644
index 0000000000000..c32fa11557aff

[ ... ]

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c line 431

Quoted 7 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +7 -0
+/* Platform data definitions for device match table */
+#if IS_ENABLED(CONFIG_MTK_MMDVFS_MT8196)
+static const struct mmdvfs_platform_data mmdvfs_mt8196_pdata = {
+	.soc_data = &mmdvfs_mt8196_soc_data,
+	.ops = &mmdvfs_mt8196_ops,
+};
+#endif

[Severity: High]
Does enabling CONFIG_MTK_MMDVFS_MT8196 at this commit fail to link due to undefined references to mmdvfs_mt8196_soc_data and mmdvfs_mt8196_ops?

drivers/soc/mediatek/Kconfig already exposes CONFIG_MTK_MMDVFS_MT8196 here, but drivers/soc/mediatek/mmdvfs/Makefile only links mmdvfs-core.o and mmdvfs-clk.o until commit "soc: mediatek: mmdvfs: add MT8196 support" adds mmdvfs-mt8196.c, breaking bisectability when CONFIG_MTK_MMDVFS_MT8196 is selected.

[ ... ]

Quoted 18 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +18 -0
+struct mmdvfs_device *mmdvfs_find_device_by_version(enum mmdvfs_version_type version)
+{
+	struct mmdvfs_device_node *node;
+	struct mmdvfs_device *found = NULL;
+
+	mutex_lock(&mmdvfs_device_list_lock);
+	list_for_each_entry(node, &mmdvfs_device_list, list) {
+		if (node->mmdvfs && node->mmdvfs->data &&
+		    node->mmdvfs->data->version == version) {
+			found = node->mmdvfs;
+			break;
+		}
+	}
+	mutex_unlock(&mmdvfs_device_list_lock);
+
+	return found;
+}
+EXPORT_SYMBOL_GPL(mmdvfs_find_device_by_version);

[ ... ]

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c line 526

Quoted 12 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +12 -0
+int mtk_mmdvfs_camera_notify(bool enable)
+{
+	struct mmdvfs_device *mmdvfs;
+
+	mmdvfs = mmdvfs_find_device_any();
+	if (!mmdvfs || !mmdvfs->data || !mmdvfs->data->ops ||
+	    !mmdvfs->data->ops->camera_notify)
+		return -EOPNOTSUPP;
+
+	return mmdvfs->data->ops->camera_notify(mmdvfs, enable);
+}
+EXPORT_SYMBOL_GPL(mtk_mmdvfs_camera_notify);

[Severity: Medium]
Can concurrent callers of mtk_mmdvfs_camera_notify() and the other exported helpers race with mmdvfs_remove() and hit a use-after-free?

CPU0 CPU1
mtk_mmdvfs_camera_notify()
mmdvfs = mmdvfs_find_device_any()

    lock(&mmdvfs_device_list_lock)
    found = node->mmdvfs
    unlock(&mmdvfs_device_list_lock)
                                        mmdvfs_remove()
                                          data->ops->deinit_hw(mmdvfs)
                                          mmdvfs_unregister_device(mmdvfs)
                                        devres_release_all()
                                          /* frees mmdvfs and mmdvfs->data */

mmdvfs->data->ops->camera_notify(...) <-- use-after-free

mmdvfs_find_device_by_version() drops mmdvfs_device_list_lock and returns a raw pointer to devm-allocated memory without taking a reference or holding a lock across the callback. In addition, mmdvfs_remove() runs data->ops->deinit_hw(mmdvfs) before mmdvfs_unregister_device(mmdvfs) unlinks the device from mmdvfs_device_list, and mmdvfs_probe() has no teardown callback for state allocated by data->ops->probe() if mmdvfs_validate_topology() or data->ops->init_hw() fails.

Quoted 6 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h new file +0 -0
new file mode 100644
index 0000000000000..df16e38d78c89
@@ -0,0 +1,305 @@

[ ... ]

Quoted 5 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h new file +5 -0
+/* MMDVFS voting mechanism types */
+enum mmdvfs_version_type {
+	MMDVFS_VERSION_NONE = 0,		/* No hardware voter */
+	MMDVFS_VERSION_HWCCF_DVFS_VOTER = 1,	/* HWCCF DVFS voter */
+};

[ ... ]

drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h line 272

Quoted 8 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h new file +8 -0
+/* Platform-specific operations */
+u32 mmdvfs_get_version(void);
+int mtk_mmdvfs_enable_ccu(const bool enable, const u8 idx);
+int mtk_mmdvfs_camera_notify(bool enable);
+int mtk_mmdvfs_camsv_dc_enable(const u8 idx, const bool enable);
+int mtk_mmdvfs_genpd_notify(u8 idx, bool enable);
+int mtk_mmdvfs_set_avs(u8 idx, u32 aging, u32 fresh);
+int mmdvfs_set_lp_mode(bool enable);

[Severity: Low]
Should mmdvfs-internal.h include <soc/mediatek/mmdvfs.h> instead of redefining enum mmdvfs_version_type and redeclaring these public API functions? Any file that includes both headers will fail to compile due to the duplicate enum definition.

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

Re: [PATCH 3/7] soc: mediatek: add MMDVFS core driver

In reply to Echo Zhang
Quoted 8 lines · Echo Zhang

Add the common MediaTek MMDVFS driver framework.

The core driver handles device probing, SoC data selection, clock provider registration, power-management notification, and dispatch to the microprocessor transport used for runtime performance voting.

---

...

Quoted 33 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +27 -0
new file mode 100644
index 000000000000..c32fa11557af
@@ -0,0 +1,581 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2026 MediaTek Inc.
+ *
+ * MediaTek MMDVFS Core Framework
+ */
+
+#include <linux/atomic.h>
+#include <linux/clk-provider.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/notifier.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/platform_device.h>
+#include <linux/sched/clock.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/suspend.h>
+#include <linux/workqueue.h>
+
+#include "mmdvfs-internal.h"
+
+static int mmdvfs_register_device(struct mmdvfs_device *mmdvfs);

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c line 37

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +10 -0
+static void mmdvfs_unregister_device(struct mmdvfs_device *mmdvfs);
+
+struct mmdvfs_device *mmdvfs_find_device_by_version(enum mmdvfs_version_type version);
+struct mmdvfs_device *mmdvfs_find_device_any(void);
+
+struct mmdvfs_device *mmdvfs_find_device_any(void)
+{
+	return mmdvfs_find_device_by_version(MMDVFS_VERSION_HWCCF_DVFS_VOTER);
+}
+EXPORT_SYMBOL_GPL(mmdvfs_find_device_any);

Ambiguous function name. any would mean *any* version, but it uses hardware here.

Quoted 102 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +102 -0
+
+/**
+ * mmdvfs_get_version - Get MMDVFS voting mechanism type
+ *
+ * Return:
+ *   0 - No hardware voter
+ *   1 - HWCCF DVFS voter
+ */
+u32 mmdvfs_get_version(void)
+{
+	struct mmdvfs_device *mmdvfs;
+
+	mmdvfs = mmdvfs_find_device_any();
+	if (!mmdvfs || !mmdvfs->data)
+		return MMDVFS_VERSION_NONE;
+
+	return mmdvfs->data->version;
+}
+EXPORT_SYMBOL_GPL(mmdvfs_get_version);
+
+/*
+ * mmdvfs_validate_topology - Validate RC/MUX/User topology consistency
+ * @mmdvfs: MMDVFS device structure
+ *
+ * Return: 0 on success, -EINVAL if topology validation fails
+ */
+static int mmdvfs_validate_topology(struct mmdvfs_device *mmdvfs)
+{
+	struct mmdvfs_data *data = mmdvfs->data;
+	int i, j;
+
+	if (!data || !data->mux || !data->rc) {
+		mmdvfs_err(mmdvfs->dev, "Invalid data structures for validation\n");
+		return -EINVAL;
+	}
+
+	for (i = 0; i < data->mux_num; i++) {
+		struct mmdvfs_mux *mux = &data->mux[i];
+		struct mmdvfs_rc *rc;
+
+		if (!mux->name || strlen(mux->name) == 0) {
+			mmdvfs_err(mmdvfs->dev, "MUX %d has empty name\n", i);
+			return -EINVAL;
+		}
+
+		if (mux->rc >= data->rc_num) {
+			mmdvfs_err(mmdvfs->dev,
+				   "MUX %d (%s) has invalid RC reference: %d (max: %d)\n",
+				   i, mux->name, mux->rc, data->rc_num);
+			return -EINVAL;
+		}
+
+		rc = &data->rc[mux->rc];
+
+		if (rc->level_num == 0 || rc->level_num > MMDVFS_MAX_LEVELS) {
+			mmdvfs_err(mmdvfs->dev,
+				   "MUX %d (%s): RC %d has invalid level_num: %d\n",
+				   i, mux->name, mux->rc, rc->level_num);
+			return -EINVAL;
+		}
+
+		if (mux->freq_num == 0 || mux->freq_num > MMDVFS_MAX_LEVELS) {
+			mmdvfs_err(mmdvfs->dev,
+				   "MUX %d (%s): invalid freq_num: %d\n",
+				   i, mux->name, mux->freq_num);
+			return -EINVAL;
+		}
+
+		for (j = 0; j < mux->freq_num; j++) {
+			if (mux->freq[j] == 0) {
+				mmdvfs_err(mmdvfs->dev,
+					   "MUX %d (%s): incomplete freq table at level %d/%d (freq=0)\n",
+					   i, mux->name, j, mux->freq_num);
+				return -EINVAL;
+			}
+
+			if (j > 0 && mux->freq[j] <= mux->freq[j - 1]) {
+				mmdvfs_warn(mmdvfs->dev,
+					    "MUX %d (%s): freq not ascending: freq[%d]=%llu <= freq[%d]=%llu\n",
+					    i, mux->name, j, mux->freq[j],
+					    j - 1, mux->freq[j - 1]);
+			}
+		}
+
+		mmdvfs_dbg(mmdvfs->dev,
+			   "MUX[%d] validated: name=%s, rc=%d, freq_num=%d (RC levels=%d), freq_min=%llu, freq_max=%llu\n",
+			   i, mux->name, mux->rc, mux->freq_num,
+			   rc->level_num, mux->freq[0],
+			   mux->freq[mux->freq_num - 1]);
+	}
+
+	for (i = 0; i < data->user_num; i++) {
+		struct mmdvfs_user *user = &data->user[i];
+
+		if (!user->name || strlen(user->name) == 0) {
+			mmdvfs_err(mmdvfs->dev, "User %d has empty name\n", i);
+			return -EINVAL;
+		}
+
+		if (user->mux >= data->mux_num) {
+			mmdvfs_err(mmdvfs->dev,
+				   "User %d (%s) has invalid MUX reference: %d (max: %d)\n",

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c line 149

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +10 -0
+				   i, user->name, user->mux, data->mux_num);
+			return -EINVAL;
+		}
+
+		mmdvfs_dbg(mmdvfs->dev,
+			   "User[%d] validated: name=%s, id=%d, mux=%d\n",
+			   i, user->name, user->id, user->mux);
+	}
+
+	mmdvfs_info(mmdvfs->dev, "Topology validation successful\n");

Do we really need these logs? This applies to the rest of the driver too.

...

Quoted 8 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +8 -0
+/*
+ * mmdvfs_parse_dt - Parse device tree configuration
+ * @mmdvfs: MMDVFS device structure
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+int mmdvfs_parse_dt(struct mmdvfs_device *mmdvfs)
+{

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c line 249

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +10 -0
+	struct device_node *node = mmdvfs->dev->of_node;
+
+	if (!node) {
+		mmdvfs_err(mmdvfs->dev, "No device tree node found\n");
+		return -ENODEV;
+	}
+
+	mmdvfs_info(mmdvfs->dev, "Device tree parsed (hardware attributes)\n");
+
+	return 0;

This is not DT parsing.

Quoted 64 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +64 -0
+}
+
+/*
+ * mmdvfs_pm_notifier_callback - PM notifier callback for suspend/resume handling
+ * @nb: Notifier block
+ * @event: PM event type
+ * @data: Event data
+ *
+ * Return: NOTIFY_DONE
+ */
+static int mmdvfs_pm_notifier_callback(struct notifier_block *nb, unsigned long event,
+				       void *data)
+{
+	struct mmdvfs_data *mdata =
+		container_of(nb, struct mmdvfs_data, pm_notifier);
+	struct mmdvfs_device *mmdvfs = dev_get_drvdata(mdata->dev);
+
+	switch (event) {
+	case PM_SUSPEND_PREPARE:
+		mmdvfs_dbg(mmdvfs->dev, "System suspending...\n");
+
+		if (mdata->ops && mdata->ops->suspend_prepare)
+			mdata->ops->suspend_prepare(mmdvfs);
+
+		mdata->pm_suspended = true;
+		break;
+
+	case PM_POST_SUSPEND:
+		mmdvfs_dbg(mmdvfs->dev, "System resumed\n");
+
+		mdata->pm_suspended = false;
+
+		if (mdata->ops && mdata->ops->post_suspend)
+			mdata->ops->post_suspend(mmdvfs);
+		break;
+
+	default:
+		break;
+	}
+
+	return NOTIFY_DONE;
+}
+
+/*
+ * mmdvfs_probe - Main probe function
+ * @pdev: Platform device
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+static int mmdvfs_probe(struct platform_device *pdev)
+{
+	struct mmdvfs_device *mmdvfs;
+	struct mmdvfs_data *data;
+	const struct mmdvfs_platform_data *pdata;
+	int ret;
+
+	mmdvfs = devm_kzalloc(&pdev->dev, sizeof(*mmdvfs), GFP_KERNEL);
+	if (!mmdvfs)
+		return -ENOMEM;
+
+	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c line 323

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +10 -0
+	mmdvfs->dev = &pdev->dev;
+	mmdvfs->data = data;
+	data->dev = &pdev->dev;
+
+	mutex_init(&data->lock);
+
+	platform_set_drvdata(pdev, mmdvfs);
+
+	pdata = of_device_get_match_data(&pdev->dev);
+	if (!pdata) {

This is not possible, since you already have mt8196 platform data in the table. Drop the check.

Quoted 11 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +11 -0
+		mmdvfs_err(mmdvfs->dev, "No matching platform data\n");
+		return -ENODEV;
+	}
+
+	data->ops = pdata->ops;
+	data->version = pdata->soc_data->version;
+
+	if (data->version == MMDVFS_VERSION_HWCCF_DVFS_VOTER) {
+		data->hwccf_voter_priv = devm_kzalloc(&pdev->dev,
+						      sizeof(*data->hwccf_voter_priv),
+						      GFP_KERNEL);

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c line 344

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +10 -0
+		if (!data->hwccf_voter_priv)
+			return -ENOMEM;
+
+		data->hwccf_voter_priv->mmdvfs = mmdvfs;
+	}
+
+	ret = mmdvfs_parse_dt(mmdvfs);
+	if (ret) {
+		mmdvfs_err(mmdvfs->dev, "Failed to parse device tree: %d\n", ret);
+		return ret;

dev_err_probe

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c line 350

Quoted 6 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +6 -0
+	}
+
+	ret = mmdvfs_init_from_soc_data(mmdvfs, pdata->soc_data);
+	if (ret) {
+		mmdvfs_err(mmdvfs->dev, "Failed to initialize from SoC data: %d\n", ret);
+		return ret;

...same...

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c line 357

Quoted 7 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +7 -0
+	}
+
+	if (data->ops && data->ops->init_hw) {
+		ret = data->ops->init_hw(mmdvfs);
+		if (ret) {
+			mmdvfs_err(mmdvfs->dev, "Failed to initialize hardware: %d\n", ret);
+			return ret;

here too.

Quoted 43 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +43 -0
+		}
+	}
+
+	data->init_done = true;
+
+	ret = mmdvfs_register_clocks(mmdvfs);
+	if (ret) {
+		mmdvfs_err(mmdvfs->dev, "Failed to register clocks: %d\n", ret);
+		goto err_deinit_hw;
+	}
+
+	data->pm_notifier.notifier_call = mmdvfs_pm_notifier_callback;
+	data->pm_notifier.priority = 0;
+	data->pm_suspended = false;
+	ret = register_pm_notifier(&data->pm_notifier);
+	if (ret) {
+		mmdvfs_err(mmdvfs->dev, "Failed to register PM notifier: %d\n", ret);
+		goto err_unregister_clocks;
+	}
+	mmdvfs_info(mmdvfs->dev, "PM notifier registered\n");
+
+	ret = mmdvfs_register_device(mmdvfs);
+	if (ret) {
+		mmdvfs_err(mmdvfs->dev, "Failed to register device: %d\n", ret);
+		goto err_unregister_pm_notifier;
+	}
+
+	mmdvfs_info(mmdvfs->dev, "MMDVFS driver probed successfully\n");
+	return 0;
+
+err_unregister_pm_notifier:
+	unregister_pm_notifier(&data->pm_notifier);
+
+err_unregister_clocks:
+	mmdvfs_unregister_clocks(mmdvfs);
+
+err_deinit_hw:
+	if (data->ops && data->ops->deinit_hw)
+		data->ops->deinit_hw(mmdvfs);
+
+	return ret;
+}
+

...

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c line 469

Quoted 6 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +6 -0
+struct mmdvfs_device *mmdvfs_find_device_by_version(enum mmdvfs_version_type version)
+{
+	struct mmdvfs_device_node *node;
+	struct mmdvfs_device *found = NULL;
+
+	mutex_lock(&mmdvfs_device_list_lock);

guard(mutex)(&mmdvfs_device_list_lock)

Quoted 13 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c new file +13 -0
+	list_for_each_entry(node, &mmdvfs_device_list, list) {
+		if (node->mmdvfs && node->mmdvfs->data &&
+		    node->mmdvfs->data->version == version) {
+			found = node->mmdvfs;
+			break;
+		}
+	}
+	mutex_unlock(&mmdvfs_device_list_lock);
+
+	return found;
+}
+EXPORT_SYMBOL_GPL(mmdvfs_find_device_by_version);
+

...

Quoted 45 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h new file +39 -0
new file mode 100644
index 000000000000..df16e38d78c8
@@ -0,0 +1,305 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2026 MediaTek Inc.
+ *
+ * MediaTek MMDVFS (MultiMedia Dynamic Voltage and Frequency Scaling) Driver
+ */
+
+#ifndef __MMDVFS_H__
+#define __MMDVFS_H__
+
+#include <linux/clk-provider.h>
+#include <linux/device.h>
+#include <linux/mutex.h>
+#include <linux/notifier.h>
+#include <linux/platform_device.h>
+
+struct dentry;
+
+#define MMDVFS_MAX_LEVELS		8
+#define MMDVFS_MAX_USERS		64
+#define MMDVFS_MAX_MUXES		16
+#define MMDVFS_MAX_RCS			4
+
+/* MMDVFS voting mechanism types */
+enum mmdvfs_version_type {
+	MMDVFS_VERSION_NONE = 0,		/* No hardware voter */
+	MMDVFS_VERSION_HWCCF_DVFS_VOTER = 1,	/* HWCCF DVFS voter */
+};
+
+#define MAX_OPP				8
+
+/* MMDVFS RC domains */
+enum mmdvfs_rc_domain {
+	MMDVFS_PWR_VCORE = 0,  /* PWR_MMDVFS_VCORE */
+	MMDVFS_PWR_VMM   = 1,  /* PWR_MMDVFS_VMM */
+	MMDVFS_PWR_VDISP = 2,  /* PWR_MMDVFS_VDISP */
+	MMDVFS_PWR_NUM   = 3   /* PWR_MMDVFS_NUM */
+};
+

drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h line 49

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h new file +10 -0
+/**
+ * struct mmdvfs_rc - Resource Controller
+ * @id: RC identifier
+ * @pa: Physical address base
+ * @level_num: Number of supported levels
+ * @rc_base: Virtual address base (mapped from pa)
+ */
+struct mmdvfs_rc {
+	u8 id;
+	u32 pa;

phys_addr_t

Quoted 3 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h new file +3 -0
+	u8 level_num;
+	void __iomem *rc_base;
+};

...

drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h line 303

Quoted 9 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h new file +9 -0
+/* Logging macros */
+#define mmdvfs_dbg(dev, fmt, args...) \
+	dev_dbg(dev, "[mmdvfs] " fmt, ##args)
+#define mmdvfs_info(dev, fmt, args...) \
+	dev_info(dev, "[mmdvfs] " fmt, ##args)
+#define mmdvfs_warn(dev, fmt, args...) \
+	dev_warn(dev, "[mmdvfs] " fmt, ##args)
+#define mmdvfs_err(dev, fmt, args...) \
+	dev_err(dev, "[mmdvfs] " fmt, ##args)

The driver name is already printed when using dev_* APIs. Use them directly.

Best regards,
Roman

#12 lore raw

Re: [PATCH 3/7] soc: mediatek: add MMDVFS core driver

In reply to Echo Zhang
Quoted 5 lines · Echo Zhang

Add the common MediaTek MMDVFS driver framework.

The core driver handles device probing, SoC data selection, clock provider registration, power-management notification, and dispatch to the microprocessor transport used for runtime performance voting.

I don't get why DVFS driver is a soc driver. This should use proper Linux subsystems and be placed there.

Quoted 22 lines
---
 drivers/soc/mediatek/Kconfig                  |  19 +
 drivers/soc/mediatek/Makefile                 |   1 +
 drivers/soc/mediatek/mmdvfs/Makefile          |   6 +
 drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c      | 462 ++++++++++++++
 drivers/soc/mediatek/mmdvfs/mmdvfs-core.c     | 581 ++++++++++++++++++
 drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h | 305 +++++++++
 include/soc/mediatek/mmdvfs.h                 |  72 +++
 7 files changed, 1446 insertions(+)
 create mode 100644 drivers/soc/mediatek/mmdvfs/Makefile
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-core.c
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h
 create mode 100644 include/soc/mediatek/mmdvfs.h
drivers/soc/mediatek/Kconfig +0 -0
index d7293977f06e..bb273a4461f0 100644

drivers/soc/mediatek/Kconfig config MTK_REGULATOR_COUPLER · line 66

Quoted 7 lines
drivers/soc/mediatek/Kconfig +4 -0
@@ -60,6 +60,25 @@ config MTK_REGULATOR_COUPLER
 	default ARCH_MEDIATEK
 	depends on REGULATOR
 
+config MTK_MMDVFS
+	tristate "MediaTek MMDVFS support"
+	depends on ARCH_MEDIATEK || COMPILE_TEST
+	depends on MTK_VCP_RPROC

Does not exist.

Quoted 6 lines
drivers/soc/mediatek/Kconfig +6 -0
+	help
+	  Say yes here to add support for the MediaTek Multimedia Dynamic
+	  Voltage and Frequency Scaling controller. The controller provides
+	  virtual clocks for multimedia hardware blocks and coordinates
+	  performance votes with platform voltage resources.
+

...

drivers/soc/mediatek/Kconfig

Quoted 10 lines
drivers/soc/mediatek/Kconfig +10 -0
+/* Logging macros */
+#define mmdvfs_dbg(dev, fmt, args...) \
+	dev_dbg(dev, "[mmdvfs] " fmt, ##args)
+#define mmdvfs_info(dev, fmt, args...) \
+	dev_info(dev, "[mmdvfs] " fmt, ##args)
+#define mmdvfs_warn(dev, fmt, args...) \
+	dev_warn(dev, "[mmdvfs] " fmt, ##args)
+#define mmdvfs_err(dev, fmt, args...) \
+	dev_err(dev, "[mmdvfs] " fmt, ##args)
+

No, do not reimplement standard Linux kernel APIs/frameworks.

This applies EVERYWHERE here.

Best regards,
Krzysztof

author
#13 lore raw

PATCH 4/7 soc: mediatek: mmdvfs: add VCP backend

In reply to Echo Zhang

Add the VCP transport used by MMDVFS to map AP and firmware shared memory, manage feature power references and send IPI commands.

Wait for VCP readiness and handle the selected MMUP or VCP READY notification to initialize firmware after boot and resume. Serialize IPI transactions while shared memory and acknowledgment state are in use.

---
 drivers/soc/mediatek/mmdvfs/Makefile          |    2 +
 drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c      |   39 +-
 drivers/soc/mediatek/mmdvfs/mmdvfs-core.c     |  272 +++--
 drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h |  160 ++-
 drivers/soc/mediatek/mmdvfs/mmdvfs-up-priv.h  |   57 +
 drivers/soc/mediatek/mmdvfs/mmdvfs-up.c       |  275 +++++
 .../soc/mediatek/mmdvfs/mmdvfs-vcp-memory.h   |  104 ++
 drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c      | 1075 +++++++++++++++++
 include/soc/mediatek/mmdvfs.h                 |   66 +-
 9 files changed, 1846 insertions(+), 204 deletions(-)
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-up-priv.h
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-up.c
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-vcp-memory.h
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c
drivers/soc/mediatek/mmdvfs/Makefile +2 -0
index 33a280827006..16ecee30bff6 100644
@@ -4,3 +4,5 @@ obj-$(CONFIG_MTK_MMDVFS) += mtk-mmdvfs.o
 
 mtk-mmdvfs-y += mmdvfs-core.o
 mtk-mmdvfs-y += mmdvfs-clk.o
+mtk-mmdvfs-y += mmdvfs-up.o
+mtk-mmdvfs-y += mmdvfs-vcp.o
drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c +12 -27
index 114f3148cf46..b9025427782d 100644
@@ -15,7 +15,6 @@
 #include <linux/of.h>
 #include <linux/of_platform.h>
 #include <linux/platform_device.h>
-#include <linux/pm_runtime.h>
 #include <linux/property.h>
 #include <linux/slab.h>
 #include <linux/string.h>
@@ -27,9 +26,18 @@
 
 static const char *mmdvfs_get_clkdev_name(const char *name, char *buf, size_t size)
 {
+	int ret;
+
 	if (strlen(name) < MMDVFS_CLKDEV_CON_ID_MAX)
 		return name;
 
+	if (str_has_prefix(name, "mmdvfs-vcp-")) {
+		ret = snprintf(buf, size, "mmdvfs-v-%s",
+			       name + strlen("mmdvfs-vcp-"));
+		if (ret > 0 && ret < (int)size)
+			return buf;
+	}
+
 	return NULL;
 }
 
@@ -66,7 +74,7 @@ static int mmdvfs_set_rate(struct clk_hw *hw, unsigned long rate,
 
 	data = mmdvfs->data;
 
-	if (!data->init_done)
+	if (!READ_ONCE(data->init_done))
 		return -EPROBE_DEFER;
 
 	if (!mmdvfs_is_valid_user_id(user->id) ||
@@ -76,12 +84,6 @@ static int mmdvfs_set_rate(struct clk_hw *hw, unsigned long rate,
 		return -EINVAL;
 	}
 
-	if (rate == 0) {
-		mmdvfs_dbg(mmdvfs->dev, "Zero rate request for user %s\n",
-			   user->name);
-		return 0;
-	}
-
 	mux = &data->mux[user->mux];
 	if (!mmdvfs_is_valid_rc_id(mux->rc)) {
 		mmdvfs_err(mmdvfs->dev, "Invalid mux config: rc=%d for user %s\n",
@@ -118,17 +120,17 @@ static int mmdvfs_set_rate(struct clk_hw *hw, unsigned long rate,
 		return -EINVAL;
 	}
 
+	mutex_lock(&mux->lock);
 	if (user->level == level) {
 		mmdvfs_dbg(mmdvfs->dev, "user %s: already at level %d, skip\n",
 			   user->name, level);
+		mutex_unlock(&mux->lock);
 		return 0;
 	}
 
 	mmdvfs_dbg(mmdvfs->dev, "user %s: rate %lu -> level %d (freq=%llu)\n",
 		   user->name, rate, level, mux->freq[level]);
 
-	mutex_lock(&mux->lock);
-
 	if (data->ops && data->ops->hw_vote) {
 		ret = data->ops->hw_vote(mmdvfs, user->id, level);
 		if (!ret) {
@@ -237,13 +239,6 @@ static long mmdvfs_round_rate(struct clk_hw *hw, unsigned long rate,
 		return -EINVAL;
 	}
 
-	if (rate == 0) {
-		mmdvfs_dbg(mmdvfs->dev,
-			   "Zero rate request in round_rate for user %s\n",
-			   user->name);
-		return 0;
-	}
-
 	mux = &data->mux[user->mux];
 	if (!mmdvfs_is_valid_rc_id(mux->rc)) {
 		mmdvfs_err(mmdvfs->dev,
@@ -450,13 +445,3 @@ int mmdvfs_register_clocks(struct mmdvfs_device *mmdvfs)
 	mmdvfs_err(mmdvfs->dev, "Clock registration failed, cleanup completed\n");
 	return ret;
 }
-
-/*
- * mmdvfs_unregister_clocks - Unregister clock providers
- * @mmdvfs: MMDVFS device structure
- */
-void mmdvfs_unregister_clocks(struct mmdvfs_device *mmdvfs)
-{
-	if (mmdvfs && mmdvfs->dev)
-		mmdvfs_dbg(mmdvfs->dev, "MMDVFS clocks unregistered\n");
-}
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c +171 -101

419 lines not on this page · raw message

drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h +108 -52

254 lines not on this page · raw message

drivers/soc/mediatek/mmdvfs/mmdvfs-up-priv.h new file +57 -0

57 lines not on this page · raw message

drivers/soc/mediatek/mmdvfs/mmdvfs-up.c new file +275 -0

275 lines not on this page · raw message

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp-memory.h new file +104 -0

104 lines not on this page · raw message

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +1075 -0

1,075 lines not on this page · raw message

include/soc/mediatek/mmdvfs.h +42 -24

96 lines not on this page · raw message

Signature · 1 line
2.45.2
#14 lore raw

Re: [PATCH 4/7] soc: mediatek: mmdvfs: add VCP backend

Thank you for your contribution! Sashiko AI review found 18 potential issues to consider: - [High] soc: mediatek: mmdvfs: missing mtk_vcp_public.h breaks build - [High] soc: mediatek: mmdvfs…

Thank you for your contribution! Sashiko AI review found 18 potential issues to consider:
- [High] soc: mediatek: mmdvfs: missing mtk_vcp_public.h breaks build
- [High] soc: mediatek: mmdvfs: use-after-free on init_task in mmdvfs_remove()
- [High] soc: mediatek: mmdvfs: unlocked up_instances access and init/suspend races
- [Medium] soc: mediatek: mmdvfs: invalid async EPROBE_DEFER in mmdvfs_vcp_init_impl()
- [Medium] soc: mediatek: mmdvfs: stale MEM_IPI_SYNC_FUNC bit on error and UB shift
- [Medium] soc: mediatek: mmdvfs: missing NULL check for get_vcp_sram_virt() callback
- [Medium] soc: mediatek: mmdvfs: use-after-put and unvalidated length in get_bootmode()
- [Medium] soc: mediatek: mmdvfs: premature break in mmdvfs_vcp_wait_target_ready()
- [Medium] soc: mediatek: mmdvfs: broken readiness wait in mmdvfs_vcp_wait_target_ready()
- [Medium] soc: mediatek: mmdvfs: incomplete mux table init in mmdvfs_vcp_init_impl()
- [Medium] soc: mediatek: mmdvfs: unlocked state access on VCP_EVENT_SUSPEND
- [Medium] soc: mediatek: mmdvfs: stale init_done and late unregister on VCP teardown
- [Medium] soc: mediatek: mmdvfs: multi-second CPU busy-wait in sleepable VCP paths
- [Medium] soc: mediatek: mmdvfs: 100s ssleep(1) polling loops in mmdvfs_init_up()
- [Low] soc: mediatek: mmdvfs: duplicate definitions in mmdvfs-internal.h
- [Low] soc: mediatek: mmdvfs: __iomem stripped in mmdvfs_vcp_get_base_impl()
- [Low] soc: mediatek: mmdvfs: __iomem qualifier dropped in get_base() helpers
- [Low] soc: mediatek: mmdvfs: kernel-doc mismatches and unused fields in VCP backend
--

commit d167aba7bcc45b8ea038c59647e59a2dd799bbb8
Author: Echo Zhang <[email protected]>

soc: mediatek: mmdvfs: add VCP backend

This commit adds the VCP and MMUP microprocessor abstraction and backend for MediaTek MMDVFS to manage shared memory, feature power reference counts, readiness notifications, and IPI commands.

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c +1 -0
index c32fa11557aff..3388a5a3a1459 100644
@@ -7,22 +7,23 @@
 
 #include <linux/atomic.h>
 #include <linux/clk-provider.h>
+#include <linux/delay.h>
 #include <linux/device.h>

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c line 20

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c +2 -1
 #include <linux/err.h>
 #include <linux/io.h>
+#include <linux/kthread.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
-#include <linux/notifier.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
 #include <linux/platform_device.h>
+#include <linux/remoteproc/mtk_vcp_public.h>

[Severity: High]
Does this build without include/linux/remoteproc/mtk_vcp_public.h in the tree? Both mmdvfs-core.c and mmdvfs-vcp.c include this header and rely on VCP types, constants (such as MMDVFS_MMUP_MEM_ID and MMDVFS_VCP_MEM_ID), and helpers (vcp_get(), vcp_put(), vcp_get_ipidev()) that are not defined in the repository, so compiling this driver fails.

[ ... ]

Quoted 41 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c +14 -19
@@ -249,45 +251,61 @@ int mmdvfs_parse_dt(struct mmdvfs_device *mmdvfs)
 	return 0;
 }
 
-/*
- * mmdvfs_pm_notifier_callback - PM notifier callback for suspend/resume handling
- * @nb: Notifier block
- * @event: PM event type
- * @data: Event data
- *
- * Return: NOTIFY_DONE
- */
-static int mmdvfs_pm_notifier_callback(struct notifier_block *nb, unsigned long event,
-				       void *data)
+static int mmdvfs_init_up(struct mmdvfs_device *mmdvfs, bool stoppable)
 {
-	struct mmdvfs_data *mdata =
-		container_of(nb, struct mmdvfs_data, pm_notifier);
-	struct mmdvfs_device *mmdvfs = dev_get_drvdata(mdata->dev);
+	struct mmdvfs_data *mdata = mmdvfs->data;
+	int retry = 0;
+	int ret;
 
-	switch (event) {
-	case PM_SUSPEND_PREPARE:
-		mmdvfs_dbg(mmdvfs->dev, "System suspending...\n");
+	ret = mmdvfs_up_register(mmdvfs, MMDVFS_UP_VCP);
+	if (ret) {
+		mmdvfs_err(mmdvfs->dev, "Failed to register HFRP: %d\n", ret);
+		return ret;
+	}
 
-		if (mdata->ops && mdata->ops->suspend_prepare)
-			mdata->ops->suspend_prepare(mmdvfs);
+	while (!mmdvfs_up_get_cb_ready(mmdvfs, MMDVFS_UP_VCP)) {
+		if (stoppable && kthread_should_stop()) {
+			ret = -ECANCELED;
+			goto err_unregister_up;
+		}
 
-		mdata->pm_suspended = true;

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c int mmdvfs_parse_dt(struct mmdvfs_device *mmdvfs) · line 280

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c +9 -1
-		break;
+		if (++retry > 100) {
+			mmdvfs_err(mmdvfs->dev,
+				   "Init timeout: VCP/MMUP callback not ready after %d seconds\n",
+				   retry);
+			ret = -ETIMEDOUT;
+			goto err_unregister_up;
+		}
+		ssleep(1);
+	}

[Severity: Medium]
Can this ssleep(1) loop stall synchronous probe for up to 100 seconds? When vote_requires_up_ready is true, mmdvfs_probe() runs mmdvfs_init_up() synchronously; polling in 1-second increments delays probe whenever VCP_EVENT_READY arrives asynchronously, and hangs probe for 100 seconds if FUNC_MMDVFS_INIT or FUNC_MMDVFSRC_INIT fails in mmdvfs_mmup_notifier_callback(). mmdvfs_vcp_init_impl() also runs two duplicate 100-second ssleep(1) loops polling mmdvfs_vcp_get_ipidev() into an unused local variable when mmup_enable is set. Could this wait on a completion signaled by the notifier callback instead of open-coded 1-second polling?

[ ... ]

Quoted 4 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c +4 -0
+static int mmdvfs_init_kthread(void *data)
+{
+	return mmdvfs_init_up(data, true);
+}

[ ... ]

Quoted 75 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c +32 -18
@@ -358,38 +366,52 @@ static int mmdvfs_probe(struct platform_device *pdev)
 		}
 	}
 
-	data->init_done = true;
+	if (pdata->soc_data->vote_requires_up_ready) {
+		ret = mmdvfs_init_up(mmdvfs, false);
+		if (ret) {
+			mmdvfs_err(mmdvfs->dev,
+				   "Failed to initialize UP before clock registration: %d\n",
+				   ret);
+			goto err_deinit_hw;
+		}
+	}
 
 	ret = mmdvfs_register_clocks(mmdvfs);
 	if (ret) {
 		mmdvfs_err(mmdvfs->dev, "Failed to register clocks: %d\n", ret);
-		goto err_deinit_hw;
-	}
-
-	data->pm_notifier.notifier_call = mmdvfs_pm_notifier_callback;
-	data->pm_notifier.priority = 0;
-	data->pm_suspended = false;
-	ret = register_pm_notifier(&data->pm_notifier);
-	if (ret) {
-		mmdvfs_err(mmdvfs->dev, "Failed to register PM notifier: %d\n", ret);
-		goto err_unregister_clocks;
+		goto err_unregister_up;
 	}
-	mmdvfs_info(mmdvfs->dev, "PM notifier registered\n");
 
 	ret = mmdvfs_register_device(mmdvfs);
 	if (ret) {
 		mmdvfs_err(mmdvfs->dev, "Failed to register device: %d\n", ret);
-		goto err_unregister_pm_notifier;
+		goto err_unregister_up;
 	}
 
+	if (pdata->soc_data->vote_requires_up_ready) {
+		mmdvfs_dbg(mmdvfs->dev,
+			   "UP ready before MMDVFS clock provider registration\n");
+	} else {
+		/* Initialize VCP/MMUP asynchronously and wait for callback ready. */
+		data->init_task = kthread_run(mmdvfs_init_kthread, mmdvfs,
+					      "mmdvfs-init-kthread");
+		if (IS_ERR(data->init_task)) {
+			mmdvfs_warn(mmdvfs->dev,
+				    "Failed to launch init kthread: %ld (non-fatal)\n",
+				    PTR_ERR(data->init_task));
+			data->init_task = NULL;
+		} else {
+			mmdvfs_dbg(mmdvfs->dev, "Init kthread launched successfully\n");
+		}
+	}
+
+	mmdvfs_debug_init(mmdvfs);
 	mmdvfs_info(mmdvfs->dev, "MMDVFS driver probed successfully\n");
 	return 0;
 
-err_unregister_pm_notifier:
-	unregister_pm_notifier(&data->pm_notifier);
-
-err_unregister_clocks:
-	mmdvfs_unregister_clocks(mmdvfs);
+err_unregister_up:
+	if (data->up_count)
+		mmdvfs_up_unregister(mmdvfs, MMDVFS_UP_VCP);
 
 err_deinit_hw:
 	if (data->ops && data->ops->deinit_hw)
@@ -409,14 +431,20 @@ static void mmdvfs_remove(struct platform_device *pdev)
 	struct mmdvfs_device *mmdvfs = platform_get_drvdata(pdev);
 	struct mmdvfs_data *data = mmdvfs->data;
 

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c static void mmdvfs_remove(struct platform_device *pdev) · line 440

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c +6 -3
-	unregister_pm_notifier(&data->pm_notifier);
-	mmdvfs_dbg(mmdvfs->dev, "PM notifier unregistered\n");
+	mmdvfs_debug_exit(mmdvfs);
 
-	mmdvfs_unregister_clocks(mmdvfs);
+	if (data->init_task) {
+		kthread_stop(data->init_task);
+		data->init_task = NULL;
+		mmdvfs_dbg(mmdvfs->dev, "Init kthread stopped\n");
+	}

[Severity: High]
Doesn't kthread_stop(data->init_task) trigger a use-after-free once mmdvfs_init_kthread() finishes?

mmdvfs_probe() starts mmdvfs_init_kthread() with kthread_run() without taking a task_struct reference via get_task_struct(). Because mmdvfs_init_kthread() returns as soon as mmdvfs_init_up() completes or times out, the thread exits on its own and frees its task_struct while data->init_task remains non-NULL. When mmdvfs_remove() later runs, kthread_stop(data->init_task) dereferences the freed task_struct.

[ ... ]

Quoted 21 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c +7 -5
@@ -512,69 +540,111 @@ static void mmdvfs_unregister_device(struct mmdvfs_device *mmdvfs)
 	mutex_unlock(&mmdvfs_device_list_lock);
 }
 
-int mtk_mmdvfs_camera_notify(bool enable)
+/* HWCCF voter - Memory and SRAM management via multi-UP architecture */
+void *mmdvfs_get_mmup_base(phys_addr_t *pa)
 {
 	struct mmdvfs_device *mmdvfs;
+	struct mmdvfs_up_instance *hfrp_instance;
 
 	mmdvfs = mmdvfs_find_device_any();
-	if (!mmdvfs || !mmdvfs->data || !mmdvfs->data->ops ||
-	    !mmdvfs->data->ops->camera_notify)
-		return -EOPNOTSUPP;
+	if (!mmdvfs || !mmdvfs->data)
+		return NULL;
 
-	return mmdvfs->data->ops->camera_notify(mmdvfs, enable);
+	/* HFRP UP returns MMUP base with MMDVFS_MMUP_MEM_ID */
+	hfrp_instance = mmdvfs_up_get_instance(mmdvfs, MMDVFS_UP_VCP);

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c static void mmdvfs_unregister_device(struct mmdvfs_device *mmdvfs) · line 562

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c +6 -2
+	if (!hfrp_instance || !hfrp_instance->ops || !hfrp_instance->ops->get_base)
+		return NULL;
+
+	return hfrp_instance->ops->get_base(mmdvfs, MMDVFS_MMUP_MEM_ID, pa);
 }
-EXPORT_SYMBOL_GPL(mtk_mmdvfs_camera_notify);
+EXPORT_SYMBOL_GPL(mmdvfs_get_mmup_base);
 
-int mtk_mmdvfs_genpd_notify(u8 idx, bool enable)
+void *mmdvfs_get_vcp_base(phys_addr_t *pa)

[Severity: Low]
Should mmdvfs_get_mmup_base() and mmdvfs_get_vcp_base() return void __iomem * instead of void *? The underlying pointers priv->mmup_va and priv->vcp_va are void __iomem *, so returning void * strips the __iomem address-space qualifier and triggers Sparse warnings.

Quoted 26 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h +18 -0
index df16e38d78c89..2aaec44a8db7a 100644
@@ -27,6 +29,60 @@ enum mmdvfs_version_type {
 	MMDVFS_VERSION_HWCCF_DVFS_VOTER = 1,	/* HWCCF DVFS voter */
 };
 
+/* HWCCF voter configuration - Power User Definitions */
+/*
+ * VCP Power User Index - must match VCP firmware definition
+ * Used by mtk_mmdvfs_enable_vcp() to track per-user power reference count
+ */
+enum mmdvfs_vcp_pwr_usr {
+	VCP_PWR_USR_MMDVFS_INIT = 0,
+	VCP_PWR_USR_MMDVFS_RESERVED1,
+	VCP_PWR_USR_MMDVFS_RESERVED2,
+	VCP_PWR_USR_MMDVFS_VOTE,
+	VCP_PWR_USR_MMDVFS_CCU,
+	VCP_PWR_USR_MMDVFS_RST,
+	VCP_PWR_USR_MMQOS,
+	VCP_PWR_USR_CAM,
+	VCP_PWR_USR_IMG,
+	VCP_PWR_USR_PDA,
+	VCP_PWR_USR_SENIF,
+	VCP_PWR_USR_VDEC,

drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h enum mmdvfs_version_type { · line 59

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h +10 -0
+	VCP_PWR_USR_VFMT,
+	VCP_PWR_USR_SMI,
+	VCP_PWR_USR_DISP,
+	VCP_PWR_USR_MDP,
+	VCP_PWR_USR_MML,
+	VCP_PWR_USR_VENC,
+	VCP_PWR_USR_JPEGDEC,
+	VCP_PWR_USR_JPEGENC,
+	VCP_PWR_USR_NUM
+};

[Severity: Low]
mmdvfs-internal.h duplicates enum mmdvfs_version_type, the VCP_PWR_USR_* enumerators, and the public API prototypes from include/soc/mediatek/mmdvfs.h instead of including <soc/mediatek/mmdvfs.h>. Any file that includes both headers will fail to compile with redefinition errors, and the public prototypes in include/soc/mediatek/mmdvfs.h are never type-checked against their definitions in mmdvfs-core.c.

Quoted 15 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h +15 -0
+
+/* IPI function codes used by this driver; values match VCP/MMUP firmware. */
+enum mmdvfs_ipi_func {
+	FUNC_MMDVFS_INIT = 0,		/* Initialize firmware-side MMDVFS */
+	FUNC_MMDVFSRC_INIT = 9,	/* Initialize resource controller */
+};
+
+/**
+ * struct mmdvfs_vcp_ipi_data - VCP IPI message format
+ * @func: Function code
+ * @idx: Index parameter
+ * @opp: OPP level
+ * @ack: High 32-bit of 64-bit parameter
+ * @base: Low 32-bit of 64-bit parameter
+ *

drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h enum mmdvfs_version_type { · line 84

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h +10 -0
+ * For FUNC_MMDVFS_INIT/MMDVFSRC_INIT, the 64-bit shared memory IOVA is split
+ * across ack and base.
+ */
+struct mmdvfs_vcp_ipi_data {
+	u8 func;
+	u8 idx;
+	u8 opp;
+	u8 ack;
+	u32 base;
+} __packed;

[Severity: Low]
The kernel-doc for struct mmdvfs_vcp_ipi_data describes @ack as the high 32 bits of a 64-bit parameter, but ack is a u8 and only holds bits 32..39 of target_iova.

Also, struct mmdvfs_device adds debugfs_dir without a matching @debugfs_dir kernel-doc entry, and struct mmdvfs_vcp_priv in mmdvfs-vcp.c is missing kernel-doc entries for @vcp_device, @vcp_stop, @rst_clk_done, @release_step_done, and @mmup_notifier (while vcp_pdev, mmup_pdev, vcp_stop, rst_clk_done, and release_step_done are never meaningfully used).

Quoted 6 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-up.c new file +0 -0
new file mode 100644
index 0000000000000..cdf9948893909
@@ -0,0 +1,275 @@

[ ... ]

Quoted 53 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-up.c new file +53 -0
+int mmdvfs_up_register(struct mmdvfs_device *mmdvfs, enum mmdvfs_up_type type)
+{
+	struct mmdvfs_data *data;
+	struct mmdvfs_up_instance *up;
+	const struct mmdvfs_up_ops *ops;
+	int ret = 0;
+
+	if (!mmdvfs || !mmdvfs->data) {
+		pr_err("mmdvfs: Invalid device reference\n");
+		return -EINVAL;
+	}
+
+	data = mmdvfs->data;
+
+	if (data->up_count >= MMDVFS_UP_MAX) {
+		mmdvfs_err(mmdvfs->dev, "Maximum UP instances reached\n");
+		return -ENOSPC;
+	}
+
+	ops = mmdvfs_up_get_ops(type);
+	if (!ops) {
+		mmdvfs_err(mmdvfs->dev, "UP operations not found for type %d\n", type);
+		return -ENODEV;
+	}
+
+	mutex_lock(&data->lock);
+
+	for (int i = 0; i < data->up_count; i++) {
+		if (data->up_instances[i].type == type) {
+			mmdvfs_err(mmdvfs->dev,
+				   "UP type %d already registered\n", type);
+			ret = -EEXIST;
+			goto unlock;
+		}
+	}
+
+	up = &data->up_instances[data->up_count];
+	up->type = type;
+	up->ops = ops;
+	up->priv_data = NULL;
+
+	data->up_count++;
+
+	if (ops->init) {
+		ret = ops->init(mmdvfs);
+		if (ret) {
+			mmdvfs_err(mmdvfs->dev,
+				   "Failed to initialize UP type %d: %d\n",
+				   type, ret);
+			data->up_count--;
+			goto unlock;
+		}
+	}

[ ... ]

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-up.c new file +10 -0
+struct mmdvfs_up_instance *
+mmdvfs_up_get_instance(struct mmdvfs_device *mmdvfs, enum mmdvfs_up_type type)
+{
+	struct mmdvfs_data *data;
+	int i;
+
+	if (!mmdvfs || !mmdvfs->data) {
+		pr_err("mmdvfs: Invalid device reference\n");
+		return NULL;
+	}

drivers/soc/mediatek/mmdvfs/mmdvfs-up.c line 158

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-up.c new file +10 -0
+
+	data = mmdvfs->data;
+
+	for (i = 0; i < data->up_count; i++) {
+		if (data->up_instances[i].type == type)
+			return &data->up_instances[i];
+	}
+
+	return NULL;
+}

[Severity: High]
Can concurrent callers of mtk_mmdvfs_enable_vcp() or mmdvfs_get_vcp_base() race with asynchronous UP registration and teardown?

When vote_requires_up_ready is false, mmdvfs_probe() publishes the device via mmdvfs_register_device() before mmdvfs_init_kthread() calls mmdvfs_up_register(). mmdvfs_up_register() increments data->up_count before ops->init() finishes, and mmdvfs_up_get_instance() reads data->up_count and data->up_instances[] without holding data->lock:

mmdvfs-init-kthread CPU1
mmdvfs_init_up()
mmdvfs_up_register()

    data->up_count++
    mmdvfs_vcp_init_impl()
      mmdvfs_up_set_priv(..., priv)
      ...
                                  mtk_mmdvfs_enable_vcp()
                                    mmdvfs_up_get_instance()
      /* init times out */
      vcp_put(priv->vcp_device)
                                    hfrp_instance->ops->enable()
                                      /* uses put vcp_device */

Similarly, mmdvfs_remove() calls deinit_hw() and mmdvfs_up_unregister() before mmdvfs_unregister_device(), and VCP_EVENT_SUSPEND accesses priv->pwr_usage[], init_ready_mask, and cb_ready without pwr_mutex or cb_mutex.

Quoted 6 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +0 -0
new file mode 100644
index 0000000000000..fc6db1c89294c
@@ -0,0 +1,1075 @@

[ ... ]

Quoted 17 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +17 -0
+static void mmdvfs_get_bootmode(void)
+{
+	struct device_node *np_chosen;
+	struct tag_bootmode {
+		u32 size;
+		u32 tag;
+		u32 bootmode;
+		u32 boottype;
+	} *tag;
+
+	if (bootmode < UNKNOWN_BOOT)
+		return;
+
+	np_chosen = of_find_node_by_path("/chosen");
+	if (!np_chosen) {
+		bootmode = NORMAL_BOOT;
+		return;

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 209

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +10 -0
+	}
+
+	tag = (struct tag_bootmode *)of_get_property(np_chosen, "atag,boot", NULL);
+	of_node_put(np_chosen);
+
+	if (tag)
+		bootmode = tag->bootmode;
+	else
+		bootmode = NORMAL_BOOT;
+}

[Severity: Medium]
Doesn't mmdvfs_get_bootmode() dereference tag->bootmode after dropping the reference to np_chosen with of_node_put(), without checking the property length?

If "atag,boot" is shorter than offsetofend(struct tag_bootmode, bootmode), reading tag->bootmode reads out of bounds, and the raw struct cast skips FDT endianness conversion. In addition, bootmode is read and written without locking before priv->ipi_mutex is taken, returning -EACCES on RECOVERY_BOOT causes VCP_EVENT_READY to abort before setting cb_ready (making mmdvfs_init_up() time out after 100 seconds), and neither "atag,boot" nor the unused "mediatek,mmup-core" lookup in mmdvfs_vcp_init_impl() is documented in Devicetree bindings.

Quoted 9 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +9 -0
+
+/*
+ * mmdvfs_vcp_wait_target_ready - Wait for VCP/MMUP ready
+ */
+static int
+mmdvfs_vcp_wait_target_ready(struct mmdvfs_device *mmdvfs,
+			     struct mmdvfs_vcp_priv *priv,
+			     enum vcp_feature_id target_feature,
+			     const char *target_name,

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 228

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +10 -0
+			     struct mmdvfs_vcp_ipi_data *ipi_data)
+{
+	int retry = 0;
+	bool hw_ready = mmdvfs_vcp_is_ready(priv, target_feature);
+
+	while (!hw_ready || !READ_ONCE(priv->cb_ready) || priv->rst_clk_done) {
+		if (!READ_ONCE(priv->cb_ready) &&
+		    (ipi_data->func == FUNC_MMDVFS_INIT ||
+		     ipi_data->func == FUNC_MMDVFSRC_INIT))
+			break;

[Severity: Medium]
Does this break out of the readiness wait loop on the first iteration for FUNC_MMDVFS_INIT and FUNC_MMDVFSRC_INIT even when hw_ready is false? Because priv->cb_ready is always false while sending those two init IPIs, the condition is unconditionally true and skips waiting for mmdvfs_vcp_is_ready() and !priv->rst_clk_done.

Quoted 30 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +30 -0
+
+		if (++retry > VCP_SYNC_TIMEOUT_MS) {
+			mmdvfs_err(mmdvfs->dev,
+				   "%s not ready after %d ms (func=%u)\n",
+				   target_name, VCP_SYNC_TIMEOUT_MS, ipi_data->func);
+			return -ETIMEDOUT;
+		}
+		mdelay(1);
+		hw_ready = mmdvfs_vcp_is_ready(priv, target_feature);
+	}
+
+	return 0;
+}
+
+/*
+ * mmdvfs_vcp_send_and_sync - Send IPI and sync
+ */
+static int
+mmdvfs_vcp_send_and_sync(struct mmdvfs_device *mmdvfs,
+			 struct mmdvfs_vcp_priv *priv,
+			 struct mtk_ipi_device *ipidev,
+			 enum vcp_feature_id target_feature,
+			 u32 target_ipi_id,
+			 const char *target_name,
+			 void *data,
+			 struct mmdvfs_vcp_ipi_data *ipi_data,
+			 void __iomem *mem_base,
+			 u32 sync_func_val)
+{
+	int ret, retry;

[ ... ]

Quoted 38 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +38 -0
+	mutex_lock(&priv->pwr_mutex);
+
+	writel(0, mem_base + MEM_IPI_SYNC_DATA);
+	writel(sync_func_val | (1 << ipi_data->func),
+	       mem_base + MEM_IPI_SYNC_FUNC);
+
+	mutex_lock(&priv->cb_mutex);
+	if (!priv->cb_ready &&
+	    ipi_data->func != FUNC_MMDVFS_INIT &&
+	    ipi_data->func != FUNC_MMDVFSRC_INIT) {
+		mutex_unlock(&priv->cb_mutex);
+		mmdvfs_err(mmdvfs->dev, "%s cb_ready false before IPI\n", target_name);
+		mutex_unlock(&priv->pwr_mutex);
+		return -ETIMEDOUT;
+	}
+	mutex_unlock(&priv->cb_mutex);
+	mutex_unlock(&priv->pwr_mutex);
+
+	ret = priv->vcp_device->ipi_ops->ipi_send(ipidev, target_ipi_id,
+						  data, PIN_OUT_SIZE_MMDVFS);
+	if (ret != IPI_ACTION_DONE) {
+		mmdvfs_err(mmdvfs->dev, "IPI send to %s failed: %d\n",
+			   target_name, ret);
+		ret = -EIO;
+		goto cleanup_sync;
+	}
+
+	retry = 0;
+	while (1) {
+		u32 sync_data = readl(mem_base + MEM_IPI_SYNC_DATA);
+
+		if (sync_data & (1 << ipi_data->func))
+			break;
+
+		if (++retry > 1000000) {
+			mmdvfs_err(mmdvfs->dev, "IPI timeout (func=%u)\n", ipi_data->func);
+			ret = -ETIMEDOUT;
+			goto cleanup_sync;

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 324

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +10 -0
+		}
+
+		if (!mmdvfs_vcp_is_ready(priv, target_feature)) {
+			mmdvfs_err(mmdvfs->dev, "%s not ready during wait (func=%u)\n",
+				   target_name, ipi_data->func);
+			ret = -ETIMEDOUT;
+			goto cleanup_sync;
+		}
+		udelay(1);
+	}

[Severity: Medium]
Can this udelay(1) loop busy-spin on the CPU for over a second while holding priv->ipi_mutex in sleepable process context?

mmdvfs_vcp_wait_target_ready() and mmdvfs_vcp_init_impl() similarly busy-wait with mdelay(1) for up to 1000 ms each while holding mutexes in process context. Could these loops use sleeping delays such as usleep_range() or msleep() instead?

Quoted 5 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +5 -0
+
+	ret = 0;
+
+cleanup_sync:
+	mutex_lock(&priv->pwr_mutex);

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 339

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +10 -0
+	if (ret == 0) {
+		u32 sync_func = readl(mem_base + MEM_IPI_SYNC_FUNC);
+		u32 sync_data = readl(mem_base + MEM_IPI_SYNC_DATA);
+
+		writel(sync_func & ~sync_data, mem_base + MEM_IPI_SYNC_FUNC);
+	}
+	mutex_unlock(&priv->pwr_mutex);
+
+	return ret;
+}

[Severity: Medium]
If ipi_send() fails, cb_ready is false, or the acknowledgment wait times out, doesn't MEM_IPI_SYNC_FUNC keep the (1 << ipi_data->func) bit set?

Because MEM_IPI_SYNC_FUNC is written before checking cb_ready and cleanup_sync only clears bits when ret == 0, any failed IPI leaves its function bit permanently set in MEM_IPI_SYNC_FUNC across subsequent IPIs (which read back MEM_IPI_SYNC_FUNC into sync_func_val). Also, (1 << ipi_data->func) shifts a signed 32-bit int by an unvalidated u8; using BIT(ipi_data->func) after checking ipi_data->func < 32 would avoid undefined shift behavior.

Quoted 8 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +8 -0
+
+/*
+ * mmdvfs_vcp_send_ipi_impl - Send IPI to VCP/MMUP
+ */
+static int
+mmdvfs_vcp_send_ipi_impl(struct mmdvfs_device *mmdvfs, void *data, u32 len,
+			 const bool vcp)
+{

[ ... ]

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 403

Quoted 11 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +11 -0
+	mutex_lock(&priv->ipi_mutex);
+	if (!mem_base) {
+		mmdvfs_err(mmdvfs->dev, "Shared memory base is NULL\n");
+		ret = -ENOMEM;
+		goto unlock_ipi;
+	}
+
+	sync_func_val = readl(mem_base + MEM_IPI_SYNC_FUNC);
+
+	ret = mmdvfs_vcp_wait_target_ready(mmdvfs, priv, target_feature,
+					   target_name, ipi_data);

[Severity: Medium]
If a non-init IPI calls mmdvfs_vcp_send_ipi_impl() while priv->cb_ready is false, doesn't holding priv->ipi_mutex across mmdvfs_vcp_wait_target_ready() prevent VCP_EVENT_READY from ever setting priv->cb_ready = true?

CPU0 CPU1 (VCP_EVENT_READY)
mmdvfs_vcp_send_ipi_impl()
mutex_lock(&priv->ipi_mutex)
mmdvfs_vcp_wait_target_ready()

    /* polls priv->cb_ready */
                                  mmdvfs_mmup_notifier_callback()
                                    mmdvfs_vcp_send_ipi_impl(FUNC_MMDVFS_INIT)
                                      mutex_lock(&priv->ipi_mutex)
                                      <-- blocked until CPU0 times out
                                    mmdvfs_vcp_set_init_ready(..., true)

Because VCP_EVENT_READY must acquire priv->ipi_mutex to send FUNC_MMDVFS_INIT and FUNC_MMDVFSRC_INIT before setting priv->cb_ready, CPU0 is guaranteed to spin for the full 1000 ms and fail with -ETIMEDOUT.

[ ... ]

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +10 -0
+static int mmdvfs_mmup_notifier_callback(struct notifier_block *nb,
+					 unsigned long event, void *data)
+{
+	struct mmdvfs_vcp_priv *priv =
+		container_of(nb, struct mmdvfs_vcp_priv, mmup_notifier);
+	struct mmdvfs_device *mmdvfs = priv->mmdvfs;
+	int ret;
+
+	switch (event) {
+	case VCP_EVENT_READY:

[ ... ]

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 526

Quoted 7 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +7 -0
+		/* Initialize MMUP SRAM */
+		if (priv->mmup_enable && priv->mmup_sram_enable && !priv->mmup_sram_va) {
+			void __iomem *sram_base;
+			void __iomem *mem_base;
+			u32 sram_offset;
+
+			sram_base = priv->vcp_device->ops->get_vcp_sram_virt(priv->vcp_device);

[Severity: Medium]
Can priv->vcp_device->ops->get_vcp_sram_virt be NULL here? mmdvfs_vcp_init_impl() checks get_mem_phys, get_mem_iova, and get_mem_virt, but never verifies that get_vcp_sram_virt is populated before this unconditional call.

Quoted 13 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +13 -0
+			if (sram_base) {
+				mem_base = priv->mmup_enable ? priv->mmup_va : priv->vcp_va;
+				sram_offset = readl(mem_base + MEM_SRAM_OFFSET);
+				priv->mmup_sram_va = sram_base + sram_offset;
+			}
+		}
+
+		mmdvfs_vcp_set_init_ready(priv, MMDVFS_UP_INIT_READY, true);
+
+		break;
+
+	case VCP_EVENT_STOP:
+		priv->vcp_stop = true;

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 549

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +10 -0
+		mmdvfs_vcp_set_init_ready(priv, MMDVFS_UP_INIT_READY, false);
+		break;
+
+	case VCP_EVENT_SUSPEND:
+		priv->release_step_done = true;
+		mmdvfs_reset_vcp(priv);
+		WRITE_ONCE(priv->init_ready_mask, 0);
+		WRITE_ONCE(priv->cb_ready, false);
+		WRITE_ONCE(mmdvfs->data->init_done, false);
+		break;

[Severity: Medium]
Should VCP_EVENT_SUSPEND hold priv->pwr_mutex around mmdvfs_reset_vcp() and update init_ready_mask, cb_ready, and init_done through mmdvfs_vcp_set_init_ready() under priv->cb_mutex? Modifying those fields without cb_mutex can race with concurrent mmdvfs_vcp_set_init_ready() or mmdvfs_vcp_send_and_sync() calls.

[ ... ]

Quoted 12 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +12 -0
+static void *mmdvfs_vcp_get_base_impl(struct mmdvfs_device *mmdvfs, u32 mem_id, phys_addr_t *pa)
+{
+	struct mmdvfs_vcp_priv *priv = mmdvfs_vcp_priv(mmdvfs);
+
+	if (!priv)
+		return NULL;
+
+	if (mem_id == MMDVFS_MMUP_MEM_ID) {
+		if (pa)
+			*pa = priv->mmup_pa;
+		return priv->mmup_va;
+	} else if (mem_id == MMDVFS_VCP_MEM_ID) {

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 671

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +10 -0
+		if (pa)
+			*pa = priv->vcp_pa;
+		return priv->vcp_va;
+	}
+
+	mmdvfs_warn(mmdvfs->dev, "Unknown mem_id %u, defaulting to VCP\n", mem_id);
+	if (pa)
+		*pa = priv->vcp_pa;
+	return priv->vcp_va;
+}

[Severity: Low]
mmdvfs_vcp_get_base_impl() and struct mmdvfs_up_ops::get_base return void * instead of void __iomem * when returning priv->mmup_va and priv->vcp_va, unlike get_sram() right below which preserves __iomem.

[ ... ]

Quoted 2 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +2 -0
+static int mmdvfs_vcp_init_impl(struct mmdvfs_device *mmdvfs)
+{

[ ... ]

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 726

Quoted 5 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +5 -0
+	priv->vcp_device = vcp_get(to_platform_device(mmdvfs->dev));
+	if (!priv->vcp_device) {
+		return dev_err_probe(mmdvfs->dev, -EPROBE_DEFER,
+				     "VCP device is not ready\n");
+	}

[Severity: Medium]
Is it valid to call dev_err_probe(..., -EPROBE_DEFER, ...) when mmdvfs_vcp_init_impl() is invoked from the asynchronous mmdvfs_init_kthread()?

When vote_requires_up_ready is false, mmdvfs_probe() has already returned 0 by the time mmdvfs_init_kthread() runs. Calling dev_err_probe() outside probe context mutates dev->p->deferred_probe_reason without driver-core probe locking, and returning -EPROBE_DEFER from the kthread will not trigger deferred re-probing, leaving data->init_done permanently false.

[ ... ]

Quoted 4 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +4 -0
+		/* Keep firmware-owned control slots inactive unless firmware changes them. */
+		for (i = 0; i < MEM_FW_RSVD_OPP_PWR_COUNT; i++) {
+			writel(MAX_OPP, mem_base + MEM_FW_RSVD_OPP_PWR(i));
+			writel(MAX_OPP, mem_base + MEM_VOTE_OPP_PWR(i));

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 886

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +10 -0
+			writel(MAX_OPP, mem_base + MEM_PWR_OPP(i));
+			writel(MAX_OPP, mem_base + MEM_PWR_CUR_GEAR(i));
+			writel(MAX_OPP, mem_base + MEM_FW_RSVD_VOL(i));
+		}
+		for (i = 0; i < MEM_VOTE_OPP_USR_COUNT; i++) {
+			writel(MAX_OPP, mem_base + MEM_VOTE_OPP_USR(i));
+			writel(MAX_OPP, mem_base + MEM_MUX_OPP(i));
+			writel(MAX_OPP, mem_base + MEM_MUX_MIN(i));
+			writel(MAX_OPP, mem_base + MEM_FW_RSVD_CLK(i));
+		}

[Severity: Medium]
Doesn't bounding this loop by MEM_VOTE_OPP_USR_COUNT (14) leave mux slots 14 and 15 of MEM_MUX_OPP, MEM_MUX_MIN, and MEM_FW_RSVD_CLK uninitialized at 0 instead of MAX_OPP? In mmdvfs-vcp-memory.h those three mux register arrays have 16 entries (matching MMDVFS_MAX_MUXES = 16).

[ ... ]

Quoted 20 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +20 -0
+static void mmdvfs_vcp_deinit_impl(struct mmdvfs_device *mmdvfs)
+{
+	struct mmdvfs_vcp_priv *priv = mmdvfs_vcp_priv(mmdvfs);
+
+	if (!priv)
+		return;
+
+	if (!priv->initialized) {
+		mmdvfs_up_set_priv(mmdvfs, MMDVFS_UP_VCP, NULL);
+		return;
+	}
+
+	mmdvfs_vcp_unregister_notify(priv,
+				     priv->mmup_enable ? priv->mmup_feature_id :
+				     priv->vcp_feature_id,
+				     &priv->mmup_notifier);
+
+	if (priv->pwr_usage[VCP_PWR_USR_MMDVFS_INIT])
+		mmdvfs_vcp_enable_impl(mmdvfs, false, VCP_PWR_USR_MMDVFS_INIT);
+

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 957

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +10 -0
+	if (priv->vcp_pdev)
+		platform_device_put(priv->vcp_pdev);
+	if (priv->mmup_pdev)
+		platform_device_put(priv->mmup_pdev);
+	if (priv->vcp_device && priv->vcp_device->dev)
+		vcp_put(priv->vcp_device);
+
+	priv->initialized = false;
+	mmdvfs_up_set_priv(mmdvfs, MMDVFS_UP_VCP, NULL);
+}

[Severity: Medium]
Should mmdvfs_vcp_deinit_impl() clear priv->cb_ready and mmdvfs->data->init_done via mmdvfs_vcp_set_init_ready(priv, MMDVFS_UP_INIT_READY, false)?

If mmdvfs_init_up() fails in mmdvfs_vcp_release_init() or stops after VCP_EVENT_READY has already set data->init_done = true, tearing down the VCP backend leaves data->init_done set, allowing mmdvfs_set_rate() votes to proceed after VCP unregistration. Also, in mmdvfs_remove(), should mmdvfs_unregister_device() run before deinit_hw() and mmdvfs_up_unregister() so exported helpers cannot look up the device while it is being torn down?

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

Re: [PATCH 4/7] soc: mediatek: mmdvfs: add VCP backend

In reply to Echo Zhang
Quoted 10 lines · Echo Zhang

Add the VCP transport used by MMDVFS to map AP and firmware shared memory, manage feature power references and send IPI commands.

Wait for VCP readiness and handle the selected MMUP or VCP READY notification to initialize firmware after boot and resume. Serialize IPI transactions while shared memory and acknowledgment state are in use.

---

...

Quoted 60 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c +7 -6
index c32fa11557af..3388a5a3a145 100644
@@ -7,22 +7,23 @@
 
 #include <linux/atomic.h>
 #include <linux/clk-provider.h>
+#include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/io.h>
+#include <linux/kthread.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
-#include <linux/notifier.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
 #include <linux/platform_device.h>
+#include <linux/remoteproc/mtk_vcp_public.h>
 #include <linux/sched/clock.h>
 #include <linux/slab.h>
 #include <linux/string.h>
-#include <linux/suspend.h>
-#include <linux/workqueue.h>
 
 #include "mmdvfs-internal.h"
+#include "mmdvfs-up-priv.h"
 
 static int mmdvfs_register_device(struct mmdvfs_device *mmdvfs);
 static void mmdvfs_unregister_device(struct mmdvfs_device *mmdvfs);
@@ -34,14 +35,13 @@ struct mmdvfs_device *mmdvfs_find_device_any(void)
 {
 	return mmdvfs_find_device_by_version(MMDVFS_VERSION_HWCCF_DVFS_VOTER);
 }
-EXPORT_SYMBOL_GPL(mmdvfs_find_device_any);
 
 /**
  * mmdvfs_get_version - Get MMDVFS voting mechanism type
  *
  * Return:
  *   0 - No hardware voter
- *   1 - HWCCF DVFS voter
+ *   1 - HWCCF DVFS voter (VCP IPI with MMUP SRAM)
  */
 u32 mmdvfs_get_version(void)
 {
@@ -96,7 +96,8 @@ static int mmdvfs_validate_topology(struct mmdvfs_device *mmdvfs)
 			return -EINVAL;
 		}
 
-		if (mux->freq_num == 0 || mux->freq_num > MMDVFS_MAX_LEVELS) {
+		if (mux->freq_num == 0 || mux->freq_num > rc->level_num ||
+		    mux->freq_num > MMDVFS_MAX_LEVELS) {
 			mmdvfs_err(mmdvfs->dev,
 				   "MUX %d (%s): invalid freq_num: %d\n",
 				   i, mux->name, mux->freq_num);
@@ -112,10 +113,11 @@ static int mmdvfs_validate_topology(struct mmdvfs_device *mmdvfs)
 			}
 

drivers/soc/mediatek/mmdvfs/mmdvfs-core.c static int mmdvfs_validate_topology(struct mmdvfs_device *mmdvfs) · line 120

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-core.c +5 -4
 			if (j > 0 && mux->freq[j] <= mux->freq[j - 1]) {
-				mmdvfs_warn(mmdvfs->dev,
-					    "MUX %d (%s): freq not ascending: freq[%d]=%llu <= freq[%d]=%llu\n",
-					    i, mux->name, j, mux->freq[j],
-					    j - 1, mux->freq[j - 1]);
+				mmdvfs_err(mmdvfs->dev,
+					   "MUX %d (%s): freq not ascending: freq[%d]=%llu <= freq[%d]=%llu\n",
+					   i, mux->name, j, mux->freq[j],
+					   j - 1, mux->freq[j - 1]);
+				return -EINVAL;

Why this can't be in the previous patch?

...

Quoted 5 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +0 -0
new file mode 100644
index 000000000000..fc6db1c89294

...

Quoted 22 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +22 -0
+/*
+ * mmdvfs_get_bootmode - Get system boot mode
+ *
+ * Query boot mode from device tree /chosen node
+ */
+static void mmdvfs_get_bootmode(void)
+{
+	struct device_node *np_chosen;
+	struct tag_bootmode {
+		u32 size;
+		u32 tag;
+		u32 bootmode;
+		u32 boottype;
+	} *tag;
+
+	if (bootmode < UNKNOWN_BOOT)
+		return;
+
+	np_chosen = of_find_node_by_path("/chosen");
+	if (!np_chosen) {
+		bootmode = NORMAL_BOOT;
+		return;

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 209

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +10 -0
+	}
+
+	tag = (struct tag_bootmode *)of_get_property(np_chosen, "atag,boot", NULL);
+	of_node_put(np_chosen);
+
+	if (tag)
+		bootmode = tag->bootmode;
+	else
+		bootmode = NORMAL_BOOT;
+}

No. Please don't add your downstream code.

I'll stop here. This patch modifies too many things from the previous one, and the series overall needs more cleanup.

Best regards,
Roman

#16 lore raw

Re: [PATCH 4/7] soc: mediatek: mmdvfs: add VCP backend

In reply to Echo Zhang
Quoted 40 lines · Echo Zhang

Add the VCP transport used by MMDVFS to map AP and firmware shared memory, manage feature power references and send IPI commands.

Wait for VCP readiness and handle the selected MMUP or VCP READY notification to initialize firmware after boot and resume. Serialize IPI transactions while shared memory and acknowledgment state are in use.

---
 drivers/soc/mediatek/mmdvfs/Makefile          |    2 +
 drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c      |   39 +-
 drivers/soc/mediatek/mmdvfs/mmdvfs-core.c     |  272 +++--
 drivers/soc/mediatek/mmdvfs/mmdvfs-internal.h |  160 ++-
 drivers/soc/mediatek/mmdvfs/mmdvfs-up-priv.h  |   57 +
 drivers/soc/mediatek/mmdvfs/mmdvfs-up.c       |  275 +++++
 .../soc/mediatek/mmdvfs/mmdvfs-vcp-memory.h   |  104 ++
 drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c      | 1075 +++++++++++++++++
 include/soc/mediatek/mmdvfs.h                 |   66 +-
 9 files changed, 1846 insertions(+), 204 deletions(-)
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-up-priv.h
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-up.c
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-vcp-memory.h
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c
drivers/soc/mediatek/mmdvfs/Makefile +2 -0
index 33a280827006..16ecee30bff6 100644
@@ -4,3 +4,5 @@ obj-$(CONFIG_MTK_MMDVFS) += mtk-mmdvfs.o
 
 mtk-mmdvfs-y += mmdvfs-core.o
 mtk-mmdvfs-y += mmdvfs-clk.o
+mtk-mmdvfs-y += mmdvfs-up.o
+mtk-mmdvfs-y += mmdvfs-vcp.o
drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c +0 -0
index 114f3148cf46..b9025427782d 100644

drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c line 18

Quoted 4 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c +0 -1
@@ -15,7 +15,6 @@
 #include <linux/of.h>
 #include <linux/of_platform.h>
 #include <linux/platform_device.h>
-#include <linux/pm_runtime.h>

Srsly, you just added this. So you add buggy code just to fix it...

Quoted 32 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c +10 -1
 #include <linux/property.h>
 #include <linux/slab.h>
 #include <linux/string.h>
@@ -27,9 +26,18 @@
 
 static const char *mmdvfs_get_clkdev_name(const char *name, char *buf, size_t size)
 {
+	int ret;
+
 	if (strlen(name) < MMDVFS_CLKDEV_CON_ID_MAX)
 		return name;
 
+	if (str_has_prefix(name, "mmdvfs-vcp-")) {
+		ret = snprintf(buf, size, "mmdvfs-v-%s",
+			       name + strlen("mmdvfs-vcp-"));
+		if (ret > 0 && ret < (int)size)
+			return buf;
+	}
+
 	return NULL;
 }
 
@@ -66,7 +74,7 @@ static int mmdvfs_set_rate(struct clk_hw *hw, unsigned long rate,
 
 	data = mmdvfs->data;
 
-	if (!data->init_done)
+	if (!READ_ONCE(data->init_done))
 		return -EPROBE_DEFER;
 
 	if (!mmdvfs_is_valid_user_id(user->id) ||

drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c static int mmdvfs_set_rate(struct clk_hw *hw, unsigned long rate, · line 83

Quoted 8 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-clk.c +0 -5
@@ -76,12 +84,6 @@ static int mmdvfs_set_rate(struct clk_hw *hw, unsigned long rate,
 		return -EINVAL;
 	}
 
-	if (rate == 0) {
-		mmdvfs_dbg(mmdvfs->dev, "Zero rate request for user %s\n",
-			   user->name);
-		return 0;
-	}

NAK, this is just random coding, without any architectural oversight.

Best regards,
Krzysztof

#17 lore raw

Re: [PATCH 4/7] soc: mediatek: mmdvfs: add VCP backend

In reply to Echo Zhang

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 202

Quoted 11 lines · Echo Zhang
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +11 -0
+
+	if (bootmode < UNKNOWN_BOOT)
+		return;
+
+	np_chosen = of_find_node_by_path("/chosen");
+	if (!np_chosen) {
+		bootmode = NORMAL_BOOT;
+		return;
+	}
+
+	tag = (struct tag_bootmode *)of_get_property(np_chosen, "atag,boot", NULL);

NAK, stop sending us downstream.

Quoted 1 line

Best regards,
Krzysztof

#18 lore raw

Re: [PATCH 4/7] soc: mediatek: mmdvfs: add VCP backend

In reply to Echo Zhang
Quoted 7 lines · On Wed, 25 Nov 2026 14:00:09 +0800 "Echo Zhang" <[email protected]> wrote:

Add the VCP transport used by MMDVFS to map AP and firmware shared memory, manage feature power references and send IPI commands.

Wait for VCP readiness and handle the selected MMUP or VCP READY notification to initialize firmware after boot and resume. Serialize IPI transactions while shared memory and acknowledgment state are in use.

[...]

Quoted 28 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +28 -0
+static void mmdvfs_get_bootmode(void)
+{
+	struct device_node *np_chosen;
+	struct tag_bootmode {
+		u32 size;
+		u32 tag;
+		u32 bootmode;
+		u32 boottype;
+	} *tag;
+
+	if (bootmode < UNKNOWN_BOOT)
+		return;
+
+	np_chosen = of_find_node_by_path("/chosen");
+	if (!np_chosen) {
+		bootmode = NORMAL_BOOT;
+		return;
+	}
+
+	tag = (struct tag_bootmode *)of_get_property(np_chosen, "atag,boot", NULL);
+	of_node_put(np_chosen);
+
+	if (tag)
+		bootmode = tag->bootmode;
+	else
+		bootmode = NORMAL_BOOT;
+}
+

[...]

Quoted 4 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +4 -0
+static int
+mmdvfs_vcp_send_ipi_impl(struct mmdvfs_device *mmdvfs, void *data, u32 len,
+			 const bool vcp)
+{

[...]

drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c line 363

Quoted 6 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-vcp.c new file +6 -0
+	mmdvfs_get_bootmode();
+	if (bootmode == RECOVERY_BOOT) {
+		mmdvfs_err(mmdvfs->dev, "IPI skipped: bootmode=%d\n", bootmode);
+		return -EACCES;
+	}
+

Allow me to give my two cents here, even if I am a nobody :)

Android bootmodes. Whilst I do respect the reason for Android bootmodes existing, what I do not like to see is people checking for the *Android* bootmode in their drivers.

Android kernels these days are *modular* thanks to GKI. I *do not* understand why people still check for bootmodes in their drivers when there is a much better option of *not loading them* if they are not wanted in the current bootmode.

Having your driver depend on the behaviour of one specific userspace is terrible. And tracking it in firmware (given that this code is checking for an "atag,boot" property presumably added by the bootloader) is even worse.

Aside from enabling these questionable bootmode checks in the drivers, I have no idea why the bootloader needs to get involved with putting non-standard state / boot info inside of /chosen (i.e. that "atag,boot" property), when Android solved this problem by using bootargs, and more recently, bootconfig.

I will acknowledge that "just not loading modules" does get a bit more nuanced if you bring something like udev / Android init into the mix, but there are still better solutions to this problem: either gracefully handling the state where the hardware isn't ready in your driver, making your bootloader set status = "disabled"; on the relavent DT node, or blocklisting the module from userspace in boot modes where it can't work.

Inter-driver dependencies are not a good excuse either (e.g. a core clock driver depending on some DVFS driver that is a no-op in recovery, forcing you to load it ANYWAY, even if the bootloader doesn't start the required firmware on recovery boot). If that is the case, then there is some serious refactoring work to do.

First I began seeing these sorts of checks in vendor touch screen drivers, but now it seems to be infecting SoC side drivers as well.

*Kernel* drivers should work the same regardless of what *userspace* environment is running (with the only real exception being the absence of firmware or explicit configurations from userspace). And in situations where they genuinely can't work, they should not be loaded at all.

There should be no such thing as "kernel drivers designed for Android", only "kernel drivers designed for Linux".

Best regards,
Ben Grisdale

author
#19 lore raw

PATCH 5/7 soc: mediatek: mmdvfs: add MT8196 support

In reply to Echo Zhang

Describe the MT8196 resource controllers, seven multimedia muxes and their users. Read mux performance levels from device tree OPP tables and program HWCCF votes for aggregated user clock requests.

---
 drivers/soc/mediatek/mmdvfs/Makefile        |   1 +
 drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c | 637 ++++++++++++++++++++
 2 files changed, 638 insertions(+)
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c
drivers/soc/mediatek/mmdvfs/Makefile +1 -0
index 16ecee30bff6..d02ebd63e6ae 100644
@@ -6,3 +6,4 @@ mtk-mmdvfs-y += mmdvfs-core.o
 mtk-mmdvfs-y += mmdvfs-clk.o
 mtk-mmdvfs-y += mmdvfs-up.o
 mtk-mmdvfs-y += mmdvfs-vcp.o
+mtk-mmdvfs-$(CONFIG_MTK_MMDVFS_MT8196) += mmdvfs-mt8196.o
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +637 -0

637 lines not on this page · raw message

Signature · 1 line
2.45.2
#20 lore raw

Re: [PATCH 5/7] soc: mediatek: mmdvfs: add MT8196 support

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] soc: mediatek: mmdvfs: wrong bitmask in mt8196_hwccf_vote() prepare poll - [Medium] soc: mediatek…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] soc: mediatek: mmdvfs: wrong bitmask in mt8196_hwccf_vote() prepare poll
- [Medium] soc: mediatek: mmdvfs: spurious update in mt8196_hwccf_vote() on zero delta
- [Medium] soc: mediatek: mmdvfs: premature devm_kfree() in mt8196_deinit_hw()
- [Medium] soc: mediatek: mmdvfs: 5-second udelay() spin in mt8196_hwccf_vote()
- [Low] soc: mediatek: mmdvfs: wrong comment in mt8196_hwccf_vote() and unused macros
--

commit ed153f3d0764b28cb3e0456d69d5fe0a30a25f4f
Author: Echo Zhang <[email protected]>

soc: mediatek: mmdvfs: add MT8196 support

This commit adds MT8196 MMDVFS platform data, OPP table parsing, and HWCCF DVFS voter support for multimedia clock muxes.

Quoted 6 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +0 -0
new file mode 100644
index 0000000000000..1075884791e45
@@ -0,0 +1,637 @@

[ ... ]

Quoted 11 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +11 -0
+/* HWCCF timing constants */
+#define MTK_WAIT_HWV_PREPARE_CNT	200
+#define MTK_WAIT_HWV_PREPARE_US		1
+#define MTK_WAIT_HWV_DONE_CNT		5000000
+#define MTK_WAIT_HWV_DONE_US		1
+#define MT8196_HWV_LEVELS		7
+
+/* HWCCF DVFS Voter control bits */
+#define HWCCF_VOTER_ENABLE		BIT(0)
+#define HWCCF_VOTER_REQ_VALID		BIT(1)
+#define HWCCF_VOTER_ACK_VALID		BIT(2)

[ ... ]

Quoted 27 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +27 -0
+/*
+ * mt8196_hwccf_vote - perform HWCCF DVFS voter operation
+ * @mmdvfs: MMDVFS device structure
+ * @user: user structure
+ * @level: target performance level (higher value = higher performance)
+ *
+ * Implement HWCCF DVFS voter logic using the standard voting sequence:
+ *
+ * 1. WRITE: issue vote to SET/CLEAR register based on OPP delta
+ * 2. PREPARE: poll hwv_set_ofs until HW accepts (200us timeout)
+ * 3. DONE: poll hwv_sta_ofs until operation completes (5s timeout)
+ *
+ * OPP encoding uses cumulative bitmasks where index = level_num - 1 - level.
+ *
+ * Return: 0 on success, negative error code on failure
+ */
+static int mt8196_hwccf_vote(struct mmdvfs_device *mmdvfs,
+			     struct mmdvfs_user *user, u8 level)
+{
+	struct mt8196_mmdvfs_data *mt8196_data = mmdvfs->data->platform_data;
+	struct mmdvfs_data *data = mmdvfs->data;
+	struct mmdvfs_mux *mux;
+	struct mmdvfs_rc *rc;
+	u32 mask, opp_mask;
+	u32 val = 0, orig = 0, renew = 0;
+	u8 index;
+	int i = 0, ret = 0;

[ ... ]

drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c line 121

Quoted 3 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +3 -0
+	index = level;
+	mask = GENMASK(6, 0) << mux->shift;
+	opp_mask = GENMASK(index, 0) << mux->shift;

[Severity: Low]
This isn't a bug, but the function header comment above states that "index = level_num - 1 - level", whereas the implementation uses "index = level" directly. Also, HWCCF_VOTER_ENABLE, HWCCF_VOTER_REQ_VALID, and HWCCF_VOTER_ACK_VALID are unused.

Quoted 24 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +24 -0
+
+	/* Phase 1: Read current setting and calculate delta */
+	ret = regmap_read(mt8196_data->hwv_regmap, mux->hwccf.hwv_set_ofs, &orig);
+	if (ret) {
+		dev_err(mmdvfs->dev, "Failed to read HWV set register: %d\n", ret);
+		goto hwv_fail;
+	}
+
+	val = orig & mask;
+	val ^= opp_mask;
+
+	/* Write to SET or CLEAR register based on delta direction */
+	if (val > opp_mask)
+		ret = regmap_write(mt8196_data->hwv_regmap, mux->hwccf.hwv_clr_ofs, val);
+	else
+		ret = regmap_write(mt8196_data->hwv_regmap, mux->hwccf.hwv_set_ofs, val);
+
+	if (ret) {
+		dev_err(mmdvfs->dev, "Failed to write HWV register (ofs=0x%x): %d\n",
+			val > opp_mask ? mux->hwccf.hwv_clr_ofs : mux->hwccf.hwv_set_ofs, ret);
+		goto hwv_fail;
+	}
+
+	/* Write UPDATE register to trigger hardware processing. */

drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c line 155

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +10 -0
+	if (mux->hwccf.hwv_upd_id != 0xFF) {
+		ret = regmap_write(mt8196_data->hwv_regmap,
+				   mux->hwccf.hwv_upd_ofs,
+				   BIT(mux->hwccf.hwv_upd_id));
+		if (ret) {
+			dev_err(mmdvfs->dev, "Failed to write HWV update register (ofs=0x%x bit=%u): %d\n",
+				mux->hwccf.hwv_upd_ofs, mux->hwccf.hwv_upd_id, ret);
+			goto hwv_fail;
+		}
+	}

[Severity: Medium]
Should mt8196_hwccf_vote() return 0 early when val == 0?

Every mux in mt8196_mux_user_map is shared across multiple users, and mmdvfs_set_rate() only skips calling hw_vote() when the calling user's own user->level is unchanged. When a user updates its vote while another user on the same mux already holds an equal or higher vote, aggregated_level matches what is already in hwv_set_ofs, so val is 0. Without an early return, this writes 0 to hwv_set_ofs, pulses BIT(mux->hwccf.hwv_upd_id) in hwv_upd_ofs to trigger a hardware update transaction, and then exits Phase 3 without waiting because (renew & 0) == 0 is always true.

Quoted 17 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +17 -0
+
+	/* Phase 2: Poll until hardware accepts the vote (200us timeout) */
+	i = 0;
+	while (1) {
+		ret = regmap_read(mt8196_data->hwv_regmap, mux->hwccf.hwv_set_ofs, &renew);
+		if (ret) {
+			dev_err(mmdvfs->dev, "Failed to read HWV set status: %d\n", ret);
+			goto hwv_fail;
+		}
+
+		if ((renew & opp_mask) == opp_mask)
+			break;
+
+		if (i < MTK_WAIT_HWV_PREPARE_CNT) {
+			udelay(MTK_WAIT_HWV_PREPARE_US);
+			i++;
+		} else {

drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c line 182

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +10 -0
+			dev_err(mmdvfs->dev,
+				"HWV prepare timeout(%d loops, %dus): renew=0x%x opp_mask=0x%x mask=0x%x\n",
+				i, i * MTK_WAIT_HWV_PREPARE_US, renew, opp_mask, mask);
+			dev_err(mmdvfs->dev,
+				"  mux_id=%u shift=%u hwv_set_ofs=0x%x renew&mask=0x%x\n",
+				user->mux, mux->shift, mux->hwccf.hwv_set_ofs, renew & mask);
+			ret = -ETIMEDOUT;
+			goto hwv_fail;
+		}
+	}

[Severity: High]
Should the Phase 2 condition check (renew & mask) == opp_mask instead of (renew & opp_mask) == opp_mask?

When lowering a mux OPP level, the lower bits in opp_mask were already set in hwv_set_ofs by the previous higher level, while the upper bits in val are being cleared via hwv_clr_ofs. Masking renew with opp_mask ignores those upper bits, so (renew & opp_mask) == opp_mask succeeds on the first read before the hardware voter clears the upper bits or latches the downward vote, allowing Phase 3 to read pre-transition DONE bits in hwv_sta_ofs and return before the transition completes.

Quoted 14 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +14 -0
+
+	/* Phase 3: Poll until operation completes (5s timeout) */
+	i = 0;
+	while (1) {
+		ret = regmap_read(mt8196_data->hwv_regmap, mux->hwccf.hwv_sta_ofs, &renew);
+		if (ret) {
+			dev_err(mmdvfs->dev, "Failed to read HWV status: %d\n", ret);
+			goto hwv_fail;
+		}
+
+		if ((renew & val) == val)
+			break;
+
+		if (i < MTK_WAIT_HWV_DONE_CNT) {

drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c line 206

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +10 -0
+			udelay(MTK_WAIT_HWV_DONE_US);
+			i++;
+		} else {
+			dev_err(mmdvfs->dev,
+				"HWV done timeout(%d loops, %dms): renew=0x%x val=0x%x\n",
+				i, i * MTK_WAIT_HWV_DONE_US / 1000, renew, val);
+			ret = -ETIMEDOUT;
+			goto hwv_fail;
+		}
+	}

[Severity: Medium]
Can this Phase 3 loop use a sleeping poll such as regmap_read_poll_timeout() or a bounded microsecond timeout instead of spinning with udelay(1) for up to 5 seconds?

mt8196_hwccf_vote() is called from mmdvfs_set_rate() in sleepable process context while holding mux->lock and the global clock framework prepare_lock. If the hardware voter is slow or times out waiting for hwv_sta_ofs, 5,000,000 iterations of udelay(1) monopolize the CPU for 5 seconds without yielding and block all clock prepare and rate operations system-wide.

[ ... ]

Quoted 4 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +4 -0
+/*
+ * mt8196_deinit_hw - Deinitialize hardware
+ */
+static void mt8196_deinit_hw(struct mmdvfs_device *mmdvfs)

drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c line 386

Quoted 10 lines
drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c new file +10 -0
+{
+	struct mmdvfs_data *data = mmdvfs->data;
+
+	if (data->platform_data) {
+		devm_kfree(mmdvfs->dev, data->platform_data);
+		data->platform_data = NULL;
+	}
+
+	dev_info(mmdvfs->dev, "MT8196 HWCCF DVFS Voter deinitialized\n");
+}

[Severity: Medium]
Can calling devm_kfree() in mt8196_deinit_hw() cause a use-after-free in a concurrent mt8196_hwccf_vote()?

mmdvfs_probe() registers the user clocks via devm_clk_hw_register() after mt8196_init_hw() allocates data->platform_data with devm_kzalloc(), and mmdvfs_remove() invokes deinit_hw() before devres unregisters those clock providers:

CPU0 CPU1
mmdvfs_set_rate()
mutex_lock(&mux->lock)
mt8196_hw_vote()

    mt8196_hwccf_vote()
      mt8196_data =
        mmdvfs->data->platform_data
                                      mmdvfs_remove()
                                        mt8196_deinit_hw()
                                          devm_kfree(...,
                                            data->platform_data)
      regmap_read(
        mt8196_data->hwv_regmap, ...)
        <-- use-after-free

Dropping the manual devm_kfree() lets devres free data->platform_data on device release after the devm-managed clocks have been unregistered.

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

PATCH 6/7 soc: mediatek: mmdvfs: add read-only status debug

In reply to Echo Zhang

Expose MMDVFS topology and software power references through a read-only debugfs status file. Use seq_file so large topologies are not limited by a PAGE_SIZE module parameter buffer. Firmware shared memory and SRAM contents remain outside this diagnostic path.

---
 drivers/soc/mediatek/mmdvfs/Makefile       |   1 +
 drivers/soc/mediatek/mmdvfs/mmdvfs-debug.c | 200 +++++++++++++++++++++
 2 files changed, 201 insertions(+)
 create mode 100644 drivers/soc/mediatek/mmdvfs/mmdvfs-debug.c
drivers/soc/mediatek/mmdvfs/Makefile +1 -0
index d02ebd63e6ae..3d780b29efe7 100644
@@ -7,3 +7,4 @@ mtk-mmdvfs-y += mmdvfs-clk.o
 mtk-mmdvfs-y += mmdvfs-up.o
 mtk-mmdvfs-y += mmdvfs-vcp.o
 mtk-mmdvfs-$(CONFIG_MTK_MMDVFS_MT8196) += mmdvfs-mt8196.o
+mtk-mmdvfs-$(CONFIG_DEBUG_FS) += mmdvfs-debug.o
drivers/soc/mediatek/mmdvfs/mmdvfs-debug.c new file +200 -0

200 lines not on this page · raw message

Signature · 1 line
2.45.2
author
#22 lore raw

PATCH 7/7 MAINTAINERS: add MediaTek MMDVFS entry

In reply to Echo Zhang

Add a maintainer entry for the MediaTek MMDVFS driver, including the device-tree bindings, driver sources, clock IDs, and public header.

---
 MAINTAINERS | 11 +++++++++++
 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
index 27461b2161a7..eed0df58178b 100644
@@ -17179,6 +17179,17 @@ F:	Documentation/devicetree/bindings/media/mediatek,mt8173-vpu.yaml
 F:	drivers/media/platform/mediatek/vcodec/
 F:	drivers/media/platform/mediatek/vpu/
 
+MEDIATEK MMDVFS DRIVER
+M:	Echo Zhang <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Supported
+F:	Documentation/devicetree/bindings/soc/mediatek/mediatek,mmdvfs.yaml
+F:	Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-mmdvfs.yaml
+F:	drivers/soc/mediatek/mmdvfs/
+F:	include/dt-bindings/clock/mediatek,mt8196-mmdvfs.h
+F:	include/soc/mediatek/mmdvfs.h
+
 MEDIATEK MIPI-CSI CDPHY DRIVER
 M:	Julien Stephan <[email protected]>
 M:	Andy Hsieh <[email protected]>
Signature · 1 line
2.45.2