Skip to contentSkip to conversation

PM: spacemit: Add power domain support

PATCH Superseded Yixun Lan 5 participants 10 messages Oct 8 – Oct 15

Subsystems (4)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Yixun Lan

author maintainer
#0 lore raw

v2 cover PM: spacemit: Add power domain support

In this series, we try to add a Power Domain driver support for SpacemiT K1/K3 SoCs, the driver implement the power-switch functionaly which support power on/off, and it's based on Linux Generic PM Domain framework and similiar to vendor K1's implementation, but unlike the K3's vendor version which is more complicated.

The DTS patch is obsolete, so drop it. For devices required PM property,
the DT part can be sent in separate patch.

In order to test this series, a prerequisite CCU patches are needed,
which can be found here [1].

---
Changes in v2:
- convert to auxilary driver
- no need dt-binding doc, drop it
- drop dts patch
- support to build as module
- set always on for K3 audio pm
- no global variable for struct spacemit_pmu
- no force to do power-off in spacemit_pd_power_on()
- check return value of pm_genpd_init()
- adjust license header format
- Link to v1: https://patch.msgid.link/[email protected]

---
Yixun Lan (2):
dt-bindings: power: spacemit: Add power domain controller IDs
pmdomain: spacemit: Add power domain driver

 drivers/pmdomain/Kconfig                        |   1 +
 drivers/pmdomain/Makefile                       |   1 +
 drivers/pmdomain/spacemit/Kconfig               |  21 ++
 drivers/pmdomain/spacemit/Makefile              |   1 +
 drivers/pmdomain/spacemit/spacemit-pm-domains.c | 413 ++++++++++++++++++++++++
 include/dt-bindings/power/spacemit,k1-power.h   |  14 +
 include/dt-bindings/power/spacemit,k3-power.h   |  12 +
 7 files changed, 463 insertions(+)

---
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
change-id: 20260909-04-k3-pm-support-0bef40e87fd7

Best regards,

Signature · 1 line
Yixun Lan <[email protected]>
author maintainer
#1 lore raw

PATCH v2 1/2 dt-bindings: power: spacemit: Add power domain controller IDs

Add initial power domain driver support for SpacemiT K1/K3 SoC, The Power Domain Controller is SoC internal module which is part of APMU (Advanced Power Management Unit), it is responsible for managing power across multi power domains, which includes power on/off, isolation, power-on sequence.

---
 include/dt-bindings/power/spacemit,k1-power.h | 14 ++++++++++++++
 include/dt-bindings/power/spacemit,k3-power.h | 12 ++++++++++++
 2 files changed, 26 insertions(+)
include/dt-bindings/power/spacemit,k1-power.h new file +14 -0
new file mode 100644
index 000000000000..41ef540b5c44
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+#ifndef _DT_BINDINGS_SPACEMIT_K1_POWER_H_
+#define _DT_BINDINGS_SPACEMIT_K1_POWER_H_
+
+#define K1_PMDOMAIN_VPU		0
+#define K1_PMDOMAIN_GPU		1
+#define K1_PMDOMAIN_LCD		2
+#define K1_PMDOMAIN_ISP		3
+#define K1_PMDOMAIN_AUDIO	4
+#define K1_PMDOMAIN_GNSS	5
+#define K1_PMDOMAIN_HDMI	6
+
+#endif
include/dt-bindings/power/spacemit,k3-power.h new file +12 -0
new file mode 100644
index 000000000000..519fbf946312
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+#ifndef _DT_BINDINGS_SPACEMIT_K3_POWER_H_
+#define _DT_BINDINGS_SPACEMIT_K3_POWER_H_
+
+#define K3_PMDOMAIN_VPU		0
+#define K3_PMDOMAIN_GPU		1
+#define K3_PMDOMAIN_AUDIO	2
+#define K3_PMDOMAIN_LCD0	3
+#define K3_PMDOMAIN_LCD1	4
+
+#endif
Signature · 1 line
2.56.0
Acked-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] dt-bindings: power: spacemit: Add power domain controller IDs

Quoted 4 lines · Yixun Lan

Add initial power domain driver support for SpacemiT K1/K3 SoC, The Power Domain Controller is SoC internal module which is part of APMU (Advanced Power Management Unit), it is responsible for managing power across multi power domains, which includes power on/off, isolation, power-on sequence.

You should prob mention that this APMU is already documented elsewhere.

pw-bot: not-applicable

Thanks,
Conor.

Quoted 49 lines
---
 include/dt-bindings/power/spacemit,k1-power.h | 14 ++++++++++++++
 include/dt-bindings/power/spacemit,k3-power.h | 12 ++++++++++++
 2 files changed, 26 insertions(+)
include/dt-bindings/power/spacemit,k1-power.h new file +14 -0
new file mode 100644
index 000000000000..41ef540b5c44
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+#ifndef _DT_BINDINGS_SPACEMIT_K1_POWER_H_
+#define _DT_BINDINGS_SPACEMIT_K1_POWER_H_
+
+#define K1_PMDOMAIN_VPU		0
+#define K1_PMDOMAIN_GPU		1
+#define K1_PMDOMAIN_LCD		2
+#define K1_PMDOMAIN_ISP		3
+#define K1_PMDOMAIN_AUDIO	4
+#define K1_PMDOMAIN_GNSS	5
+#define K1_PMDOMAIN_HDMI	6
+
+#endif
include/dt-bindings/power/spacemit,k3-power.h new file +12 -0
new file mode 100644
index 000000000000..519fbf946312
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+#ifndef _DT_BINDINGS_SPACEMIT_K3_POWER_H_
+#define _DT_BINDINGS_SPACEMIT_K3_POWER_H_
+
+#define K3_PMDOMAIN_VPU		0
+#define K3_PMDOMAIN_GPU		1
+#define K3_PMDOMAIN_AUDIO	2
+#define K3_PMDOMAIN_LCD0	3
+#define K3_PMDOMAIN_LCD1	4
+
+#endif
Signature · 2 lines
-- 
2.56.0
author maintainer
#3 lore raw

Re: [PATCH v2 1/2] dt-bindings: power: spacemit: Add power domain controller IDs

Hi Conor,

Quoted 7 lines · Conor Dooley
Quoted 4 lines · Yixun Lan

Add initial power domain driver support for SpacemiT K1/K3 SoC, The Power Domain Controller is SoC internal module which is part of APMU (Advanced Power Management Unit), it is responsible for managing power across multi power domains, which includes power on/off, isolation, power-on sequence.

You should prob mention that this APMU is already documented elsewhere.

Right, the documentation already exists in
Documentation/devicetree/bindings/soc/spacemit/spacemit,k1-syscon.yaml

I can add a short note in next version

Quoted 1 line

Thanks

Signature · 1 line
Yixun Lan (dlan)
#4 lore raw

Re: [PATCH v2 1/2] dt-bindings: power: spacemit: Add power domain controller IDs

In reply to Conor Dooley
Quoted 10 lines · 在 2026-10-08四的 08:27 +0100,Conor Dooley写道:
Quoted 8 lines · Yixun Lan

Add initial power domain driver support for SpacemiT K1/K3 SoC, The Power
Domain Controller is SoC internal module which is part of APMU (Advanced
Power Management Unit), it is responsible for managing power across multi
power domains, which includes power on/off, isolation, power-on sequence.

Quoted 2 lines

You should prob mention that this APMU is already documented elsewhere.

Maybe it's also meaningful to mention that APMU already has #power- domain-cells property, because the first reaction of me is to add such a property (and then found it already there).

Thanks,
Icenowy

Quoted 57 lines

pw-bot: not-applicable

Thanks,
Conor.

Quoted 51 lines

---
 include/dt-bindings/power/spacemit,k1-power.h | 14 ++++++++++++++ include/dt-bindings/power/spacemit,k3-power.h | 12 ++++++++++++

 2 files changed, 26 insertions(+)
include/dt-bindings/power/spacemit,k1-power.h new file +14 -0
new file mode 100644
index 000000000000..41ef540b5c44
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+#ifndef _DT_BINDINGS_SPACEMIT_K1_POWER_H_
+#define _DT_BINDINGS_SPACEMIT_K1_POWER_H_
+
+#define K1_PMDOMAIN_VPU		0
+#define K1_PMDOMAIN_GPU		1
+#define K1_PMDOMAIN_LCD		2
+#define K1_PMDOMAIN_ISP		3
+#define K1_PMDOMAIN_AUDIO	4
+#define K1_PMDOMAIN_GNSS	5
+#define K1_PMDOMAIN_HDMI	6
+
+#endif
include/dt-bindings/power/spacemit,k3-power.h new file +12 -0
new file mode 100644
index 000000000000..519fbf946312
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+#ifndef _DT_BINDINGS_SPACEMIT_K3_POWER_H_
+#define _DT_BINDINGS_SPACEMIT_K3_POWER_H_
+
+#define K3_PMDOMAIN_VPU		0
+#define K3_PMDOMAIN_GPU		1
+#define K3_PMDOMAIN_AUDIO	2
+#define K3_PMDOMAIN_LCD0	3
+#define K3_PMDOMAIN_LCD1	4
+
+#endif
Signature · 2 lines
-- 
2.56.0
Tested-by Reviewed-by
#5 lore raw

Re: [PATCH v2 1/2] dt-bindings: power: spacemit: Add power domain controller IDs

Quoted 4 lines · Yixun Lan

Add initial power domain driver support for SpacemiT K1/K3 SoC, The Power Domain Controller is SoC internal module which is part of APMU (Advanced Power Management Unit), it is responsible for managing power across multi power domains, which includes power on/off, isolation, power-on sequence.

Tested on an OrangePi RV2 as part of basic enablement for the PowerVR GPU. An RFC series for basic GPU support will build on this.

Quoted 79 lines · Icenowy Zheng
Quoted 10 lines · 在 2026-10-08四的 08:27 +0100,Conor Dooley写道:
Quoted 8 lines · Yixun Lan

Add initial power domain driver support for SpacemiT K1/K3 SoC, The Power
Domain Controller is SoC internal module which is part of APMU (Advanced
Power Management Unit), it is responsible for managing power across multi
power domains, which includes power on/off, isolation, power-on sequence.

Quoted 2 lines

You should prob mention that this APMU is already documented elsewhere.

Maybe it's also meaningful to mention that APMU already has #power- domain-cells property, because the first reaction of me is to add such a property (and then found it already there).

Thanks,
Icenowy

Quoted 57 lines

pw-bot: not-applicable

Thanks,
Conor.

Quoted 51 lines
---
 include/dt-bindings/power/spacemit,k1-power.h | 14 ++++++++++++++
 include/dt-bindings/power/spacemit,k3-power.h | 12 ++++++++++++
 2 files changed, 26 insertions(+)
include/dt-bindings/power/spacemit,k1-power.h new file +14 -0
new file mode 100644
index 000000000000..41ef540b5c44
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+#ifndef _DT_BINDINGS_SPACEMIT_K1_POWER_H_
+#define _DT_BINDINGS_SPACEMIT_K1_POWER_H_
+
+#define K1_PMDOMAIN_VPU            0
+#define K1_PMDOMAIN_GPU            1
+#define K1_PMDOMAIN_LCD            2
+#define K1_PMDOMAIN_ISP            3
+#define K1_PMDOMAIN_AUDIO  4
+#define K1_PMDOMAIN_GNSS   5
+#define K1_PMDOMAIN_HDMI   6
+
+#endif
include/dt-bindings/power/spacemit,k3-power.h new file +12 -1
new file mode 100644
index 000000000000..519fbf946312
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+#ifndef _DT_BINDINGS_SPACEMIT_K3_POWER_H_
+#define _DT_BINDINGS_SPACEMIT_K3_POWER_H_
+
+#define K3_PMDOMAIN_VPU            0
+#define K3_PMDOMAIN_GPU            1
+#define K3_PMDOMAIN_AUDIO  2
+#define K3_PMDOMAIN_LCD0   3
+#define K3_PMDOMAIN_LCD1   4
+
+#endif
 
--

2.56.0

author maintainer
#6 lore raw

PATCH v2 2/2 pmdomain: spacemit: Add power domain driver

In reply to Yixun Lan

SpacemiT's PMU (Power Management Unit) consist of several power domains
which can be managed independently, depending on different application scenario, each domain can be powered on/off for saving power.

The driver is implemented based on Linux Generic PM Domain framework.
From a hardware perspective, either of two distinct power-on sequences are supported, in software mode (SW mode), the driver is responsible for controlling the states of bits such as sleep1, sleep2, isolation and pwr_state, while in hardware mode (HW mode), the PMU hardware will complete the sequence automatically without requiring software intervention.

---
 drivers/pmdomain/Kconfig                        |   1 +
 drivers/pmdomain/Makefile                       |   1 +
 drivers/pmdomain/spacemit/Kconfig               |  21 ++
 drivers/pmdomain/spacemit/Makefile              |   1 +
 drivers/pmdomain/spacemit/spacemit-pm-domains.c | 413 ++++++++++++++++++++++++
 5 files changed, 437 insertions(+)
drivers/pmdomain/Kconfig +1 -0
index 23076ae90e66..0610acd030f6 100644
@@ -13,6 +13,7 @@ source "drivers/pmdomain/qcom/Kconfig"
 source "drivers/pmdomain/renesas/Kconfig"
 source "drivers/pmdomain/rockchip/Kconfig"
 source "drivers/pmdomain/samsung/Kconfig"
+source "drivers/pmdomain/spacemit/Kconfig"
 source "drivers/pmdomain/st/Kconfig"
 source "drivers/pmdomain/starfive/Kconfig"
 source "drivers/pmdomain/sunxi/Kconfig"
drivers/pmdomain/Makefile +1 -0
index ebc802f13eb9..ba25b444e263 100644
@@ -11,6 +11,7 @@ obj-y					+= qcom/
 obj-y					+= renesas/
 obj-y					+= rockchip/
 obj-y					+= samsung/
+obj-y					+= spacemit/
 obj-y					+= st/
 obj-y					+= starfive/
 obj-y					+= sunxi/
drivers/pmdomain/spacemit/Kconfig new file +21 -0
new file mode 100644
index 000000000000..0dce5eef8da8
@@ -0,0 +1,21 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+menu "SpacemiT PM Domains"
+
+config SPACEMIT_PM_DOMAINS
+	tristate "PM Domain driver for SpacemiT SoC"
+	depends on ARCH_SPACEMIT || COMPILE_TEST
+	depends on PM && OF
+	select PM_GENERIC_DOMAINS
+	select PM_GENERIC_DOMAINS_OF
+	select AUXILIARY_BUS
+	help
+	  This driver enables power domain management for the SpacemiT
+	  SoC. On the SoC there are number of power domains available,
+	  which can be managed independently. For example VPU, GPU, LCD,
+	  and Audio reside in their own power domains which can be
+	  turned on/off.
+
+	  Say y to enable device power management through PM domains.
+
+endmenu
drivers/pmdomain/spacemit/Makefile new file +1 -0
new file mode 100644
index 000000000000..d0bd77a520c0
@@ -0,0 +1 @@
+obj-$(CONFIG_SPACEMIT_PM_DOMAINS)	+= spacemit-pm-domains.o
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +413 -0

413 lines not on this page · raw message

Signature · 1 line
2.56.0
#7 lore raw

Re: [PATCH v2 2/2] pmdomain: spacemit: Add power domain driver

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] pmdomain: missing .remove callback causes UAF on driver unbind - [High] pmdomain: probe fails…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] pmdomain: missing .remove callback causes UAF on driver unbind
- [High] pmdomain: probe fails unconditionally if ALWAYS_ON domain is powered off --

commit ce2204cb1ba682f1391fc6691c412bb036ad43ca

Author: Yixun Lan <[email protected]>
Date:   Thu Oct 8 03:44:30 2026 +0000

pmdomain: spacemit: Add power domain driver

This commit introduces a Generic PM Domain driver for SpacemiT SoCs. It provides support for managing power domains across different application scenarios through software or hardware control modes.

Quoted 4 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +0 -0
new file mode 100644

[ ... ]

Quoted 9 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +9 -0
+static int spacemit_pm_add_one_domain(struct spacemit_pmu *pmu, int id,
+				      const struct spacemit_pm_domain_param *param)
+{
+	struct spacemit_pm_domain *pd;
+	int ret;
+
+	pd = devm_kzalloc(pmu->dev, sizeof(*pd), GFP_KERNEL);
+	if (!pd)
+		return -ENOMEM;

drivers/pmdomain/spacemit/spacemit-pm-domains.c line 176

Quoted 10 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +10 -0
+
+	pd->pm_index        = id;
+	pd->param           = param;
+	pd->gpmu            = pmu;
+	pd->genpd.name      = param->name;
+	pd->genpd.flags     = param->flags;
+	pd->genpd.power_off = spacemit_pd_power_off;
+	pd->genpd.power_on  = spacemit_pd_power_on;
+
+	ret = pm_genpd_init(&pd->genpd, NULL, !spacemit_pm_get_state(pmu, pd));

[Severity: High]
Will this unconditionally fail probe if the bootloader leaves the audio domain powered off?

The k3_domain_params define the audio domain with GENPD_FLAG_ALWAYS_ON, but here pm_genpd_init() is called with the inverse of the current hardware state (is_off = !spacemit_pm_get_state()).

If the hardware happens to be off, is_off evaluates to true. However, the genpd core explicitly rejects initializing always-on domains in the off state:

drivers/pmdomain/core.c:pm_genpd_init() {
    ...
    /* Always-on domains must be powered on at initialization. */
    if ((genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd)) &&
            !genpd_status_on(genpd)) {
        pr_err("always-on PM domain %s is not on\n", genpd->name);
        return -EINVAL;
    }
    ...
}

Since a failure here aborts spacemit_pm_domain_probe(), does the driver need to explicitly power on always-on domains before passing them to the genpd framework?

Quoted 7 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +7 -0
+	if (ret)
+		return ret;
+
+	pmu->domains[id] = pd;
+
+	return 0;
+}

[ ... ]

drivers/pmdomain/spacemit/spacemit-pm-domains.c line 206

Quoted 12 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +12 -0
+static int spacemit_pm_domain_probe(struct auxiliary_device *adev,
+				    const struct auxiliary_device_id *id)
+{
+	struct spacemit_ccu_adev *rdev = to_spacemit_ccu_adev(adev);
+	const struct spacemit_pm_aux_data *data;
+	struct device *dev = &adev->dev;
+	struct spacemit_pmu *pmu;
+	int err, i;
+
+	data = (const struct spacemit_pm_aux_data *)id->driver_data;
+
+	pmu = devm_kzalloc(dev, sizeof(*pmu), GFP_KERNEL);

[Severity: High]
Could this cause a use-after-free on module unload or driver unbind?

The driver dynamically allocates the pmu structure and domain entries using devres APIs like devm_kzalloc() and devm_kcalloc(). During probe, these domains and the provider are registered with the genpd core using pm_genpd_init() and of_genpd_add_provider_onecell().

However, the spacemit_pm_domain_driver below lacks a .remove callback. When the driver is unbound, devres will automatically free the allocated pmu and domain memory, but pm_genpd_remove() and of_genpd_del_provider() are never called.

Does this leave dangling pointers in the core gpd_list and of_genpd_providers lists that could panic the kernel when accessed?

Quoted 2 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +2 -0
+	if (!pmu)
+		return -ENOMEM;

[ ... ]

Quoted 5 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +5 -0
+static struct auxiliary_driver spacemit_pm_domain_driver = {
+	.probe          = spacemit_pm_domain_probe,
+	.id_table       = spacemit_pm_domain_ids,
+};
+module_auxiliary_driver(spacemit_pm_domain_driver);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#8 lore raw

Re: [PATCH v2 2/2] pmdomain: spacemit: Add power domain driver

Hi ALL,

Quoted 37 lines · On 03:55 Thu 08 Oct , [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] pmdomain: missing .remove callback causes UAF on driver unbind
- [High] pmdomain: probe fails unconditionally if ALWAYS_ON domain is powered off --

commit ce2204cb1ba682f1391fc6691c412bb036ad43ca

Author: Yixun Lan <[email protected]>
Date:   Thu Oct 8 03:44:30 2026 +0000

pmdomain: spacemit: Add power domain driver

This commit introduces a Generic PM Domain driver for SpacemiT SoCs. It provides support for managing power domains across different application scenarios through software or hardware control modes.

Quoted 4 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +0 -0
new file mode 100644

[ ... ]

Quoted 15 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +15 -0
+static int spacemit_pm_add_one_domain(struct spacemit_pmu *pmu, int id,
+				      const struct spacemit_pm_domain_param *param)
+{
+	struct spacemit_pm_domain *pd;
+	int ret;
+
+	pd = devm_kzalloc(pmu->dev, sizeof(*pd), GFP_KERNEL);
+	if (!pd)
+		return -ENOMEM;
+
+	pd->pm_index        = id;
+	pd->param           = param;
+	pd->gpmu            = pmu;
+	pd->genpd.name      = param->name;
+	pd->genpd.flags     = param->flags;
Quoted 9 lines

drivers/pmdomain/spacemit/spacemit-pm-domains.c line 176

Quoted 4 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +4 -0
+	pd->genpd.power_off = spacemit_pd_power_off;
+	pd->genpd.power_on  = spacemit_pd_power_on;
+
+	ret = pm_genpd_init(&pd->genpd, NULL, !spacemit_pm_get_state(pmu, pd));

[Severity: High]
Will this unconditionally fail probe if the bootloader leaves the audio domain powered off?

I've checked, the bootloader always set audio domain on before linux starts

Quoted 4 lines

The k3_domain_params define the audio domain with GENPD_FLAG_ALWAYS_ON, but here pm_genpd_init() is called with the inverse of the current hardware state (is_off = !spacemit_pm_get_state()).

this is still correct, but the name of function makes people a bit confused,
I could change it to a more obvious one to match pm_genpd_init(.., is_off),
probably spacemit_pm_is_off_state()

Quoted 15 lines

If the hardware happens to be off, is_off evaluates to true. However, the genpd core explicitly rejects initializing always-on domains in the off state:

drivers/pmdomain/core.c:pm_genpd_init() {
    ...
    /* Always-on domains must be powered on at initialization. */
    if ((genpd_is_always_on(genpd) || genpd_is_rpm_always_on(genpd)) &&
            !genpd_status_on(genpd)) {
        pr_err("always-on PM domain %s is not on\n", genpd->name);
        return -EINVAL;
    }
    ...
}
Quoted 4 lines

Since a failure here aborts spacemit_pm_domain_probe(), does the driver need to explicitly power on always-on domains before passing them to the genpd framework?

The case said here won't happen, and the system will be in unusable state if audio domain in a 'off' state, so leave kernel yell and abort, that's expected.

Quoted 31 lines
Quoted 7 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +7 -0
+	if (ret)
+		return ret;
+
+	pmu->domains[id] = pd;
+
+	return 0;
+}

[ ... ]

drivers/pmdomain/spacemit/spacemit-pm-domains.c line 206

Quoted 12 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +12 -0
+static int spacemit_pm_domain_probe(struct auxiliary_device *adev,
+				    const struct auxiliary_device_id *id)
+{
+	struct spacemit_ccu_adev *rdev = to_spacemit_ccu_adev(adev);
+	const struct spacemit_pm_aux_data *data;
+	struct device *dev = &adev->dev;
+	struct spacemit_pmu *pmu;
+	int err, i;
+
+	data = (const struct spacemit_pm_aux_data *)id->driver_data;
+
+	pmu = devm_kzalloc(dev, sizeof(*pmu), GFP_KERNEL);

[Severity: High]
Could this cause a use-after-free on module unload or driver unbind?

The driver dynamically allocates the pmu structure and domain entries using devres APIs like devm_kzalloc() and devm_kcalloc(). During probe, these domains and the provider are registered with the genpd core using pm_genpd_init() and of_genpd_add_provider_onecell().

Quoted 8 lines

However, the spacemit_pm_domain_driver below lacks a .remove callback. When the driver is unbound, devres will automatically free the allocated pmu and domain memory, but pm_genpd_remove() and of_genpd_del_provider() are never called.

Does this leave dangling pointers in the core gpd_list and of_genpd_providers lists that could panic the kernel when accessed?

That's something I will fix in next version, I've changed the driver to support be built as a module.. while not implement a complete remove() function

Quoted 13 lines
Quoted 2 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +2 -0
+	if (!pmu)
+		return -ENOMEM;

[ ... ]

Quoted 5 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +5 -0
+static struct auxiliary_driver spacemit_pm_domain_driver = {
+	.probe          = spacemit_pm_domain_probe,
+	.id_table       = spacemit_pm_domain_ids,
+};
+module_auxiliary_driver(spacemit_pm_domain_driver);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Signature · 1 line
Yixun Lan (dlan)
Tested-by on v2 2/2
#9 lore raw

Re: [PATCH v2 2/2] pmdomain: spacemit: Add power domain driver

In reply to Yixun Lan
Quoted 4 lines · Yixun Lan

SpacemiT's PMU (Power Management Unit) consist of several power domains
which can be managed independently, depending on different application scenario, each domain can be powered on/off for saving power.

Tested on an OrangePi RV2 with the PowerVR GPU power domain. A few comments below.

drivers/pmdomain/spacemit/spacemit-pm-domains.c line 30

Quoted 10 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +10 -0
+struct spacemit_pm_domain_param {
+       int reg_pwr_ctrl;
+       int bit_hw_mode;
+       int bit_sleep2;
+       int bit_sleep1;
+       int bit_isolation;
+       int bit_auto_pwr_on;
+       int bit_hw_pwr_stat;
+       int bit_pwr_stat;
+       int use_hw;

bit_* fields hold values 0-15, u8 would be more precise than an int. Similarly, reg_pwr_ctrl fits in u16 and use_hw could be bool.

drivers/pmdomain/spacemit/spacemit-pm-domains.c line 138

Quoted 3 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +2 -0
+static int spacemit_pd_power_on(struct generic_pm_domain *domain)
...
+               dev_err(&domain->dev, "power-on domain: %d, error\n", spd->pm_index);

You could include the domain name failing, that would help identify what's causing the problem. You are already including the name in the "power-off" error message.

drivers/pmdomain/spacemit/spacemit-pm-domains.c line 227

Quoted 4 lines
drivers/pmdomain/spacemit/spacemit-pm-domains.c new file +3 -0
+static int spacemit_pm_domain_probe(struct auxiliary_device *adev,
+                                   const struct auxiliary_device_id *id)
...
+       pmu->genpd_data.domains     = (struct generic_pm_domain **)pmu->domains;

This cast relies on genpd being the first member of struct spacemit_pm_domain. Reordering the struct silently breaks it. Consider allocating a separate struct generic_pm_domain ** array and storing &pd->genpd explicitly