[PATCH/RFC 2/3] pmdomain: core: Avoid earlycon power-down

From: Geert Uytterhoeven
Date: Mon May 27 2024 - 08:41:48 EST


If the earlycon serial port's PM Domain is shared with another device,
and that other device is runtime-suspended before the full serial driver
has overtaken earlycon, the serial port's PM Domain will be disabled
inadvertently. Any subsequent serial console output will cause a crash
or system lock-up.

Avoid this by introducing a new flag to handle a PM domain that must be
kept powered-on during early boot, and by setting this flag if the PM
Domain contains the serial console.

Note that the PM Domain can still be powered off later, when the serial
port's power state agrees, e.g. during s2ram without no_console_suspend.

Signed-off-by: Geert Uytterhoeven <geert+renesas@xxxxxxxxx>
---
drivers/pmdomain/core.c | 24 ++++++++++++++++++++++--
include/linux/pm_domain.h | 4 ++++
2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/drivers/pmdomain/core.c b/drivers/pmdomain/core.c
index 342779464c0d7e84..97b9b50257eb354c 100644
--- a/drivers/pmdomain/core.c
+++ b/drivers/pmdomain/core.c
@@ -22,6 +22,7 @@
#include <linux/export.h>
#include <linux/cpu.h>
#include <linux/debugfs.h>
+#include <linux/serial_core.h>

#define GENPD_RETRY_MAX_MS 250 /* Approximate */

@@ -129,6 +130,7 @@ static const struct genpd_lock_ops genpd_spin_ops = {
#define genpd_is_cpu_domain(genpd) (genpd->flags & GENPD_FLAG_CPU_DOMAIN)
#define genpd_is_rpm_always_on(genpd) (genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
#define genpd_is_opp_table_fw(genpd) (genpd->flags & GENPD_FLAG_OPP_TABLE_FW)
+#define genpd_is_early_on(genpd) (genpd->flags & GENPD_FLAG_EARLY_ON)

static inline bool irq_safe_dev_in_sleep_domain(struct device *dev,
const struct generic_pm_domain *genpd)
@@ -725,8 +727,9 @@ static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
* (2) When the domain has a subdomain being powered on.
*/
if (genpd_is_always_on(genpd) ||
- genpd_is_rpm_always_on(genpd) ||
- atomic_read(&genpd->sd_count) > 0)
+ (genpd_is_early_on(genpd) && system_state < SYSTEM_RUNNING) ||
+ genpd_is_rpm_always_on(genpd) ||
+ atomic_read(&genpd->sd_count) > 0)
return -EBUSY;

/*
@@ -2367,6 +2370,10 @@ int of_genpd_add_provider_simple(struct device_node *np,

genpd->dev.of_node = np;

+ if (earlycon_power_domains &&
+ np->phandle == be32_to_cpup(earlycon_power_domains))
+ genpd->flags |= GENPD_FLAG_EARLY_ON;
+
/* Parse genpd OPP table */
if (!genpd_is_opp_table_fw(genpd) && genpd->set_performance_state) {
ret = dev_pm_opp_of_add_table(&genpd->dev);
@@ -2447,6 +2454,19 @@ int of_genpd_add_provider_onecell(struct device_node *np,
genpd->has_provider = true;
}

+ if (earlycon_power_domains && earlycon_power_domains_ncells == 2 &&
+ np->phandle == be32_to_cpup(earlycon_power_domains)) {
+ struct of_phandle_args genpdspec = {
+ .np = np,
+ .args_count = 1,
+ .args[0] = be32_to_cpup(earlycon_power_domains + 1),
+ };
+
+ genpd = data->xlate(&genpdspec, data);
+ if (!IS_ERR(genpd))
+ genpd->flags |= GENPD_FLAG_EARLY_ON;
+ }
+
ret = genpd_add_provider(np, data->xlate, data);
if (ret < 0)
goto error;
diff --git a/include/linux/pm_domain.h b/include/linux/pm_domain.h
index 772d3280d35fafa2..012d58ffc7059e0d 100644
--- a/include/linux/pm_domain.h
+++ b/include/linux/pm_domain.h
@@ -92,6 +92,9 @@ struct dev_pm_domain_list {
* GENPD_FLAG_OPP_TABLE_FW: The genpd provider supports performance states,
* but its corresponding OPP tables are not
* described in DT, but are given directly by FW.
+ *
+ * GENPD_FLAG_EARLY_ON: Instructs genpd to keep the PM domain powered
+ * on during early boot.
*/
#define GENPD_FLAG_PM_CLK (1U << 0)
#define GENPD_FLAG_IRQ_SAFE (1U << 1)
@@ -101,6 +104,7 @@ struct dev_pm_domain_list {
#define GENPD_FLAG_RPM_ALWAYS_ON (1U << 5)
#define GENPD_FLAG_MIN_RESIDENCY (1U << 6)
#define GENPD_FLAG_OPP_TABLE_FW (1U << 7)
+#define GENPD_FLAG_EARLY_ON (1U << 8)

enum gpd_status {
GENPD_STATE_ON = 0, /* PM domain is on */
--
2.34.1