1 // SPDX-License-Identifier: GPL-2.0
3 * PM domains for CPUs via genpd - managed by cpuidle-psci.
5 * Copyright (C) 2019 Linaro Ltd.
10 #define pr_fmt(fmt) "CPUidle PSCI: " fmt
12 #include <linux/cpu.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_domain.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/psci.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
22 #include "cpuidle-psci.h"
23 #include "dt_idle_genpd.h"
25 struct psci_pd_provider {
26 struct list_head link;
27 struct device_node *node;
30 static LIST_HEAD(psci_pd_providers);
31 static bool psci_pd_allow_domain_state;
33 static int psci_pd_power_off(struct generic_pm_domain *pd)
35 struct genpd_power_state *state = &pd->states[pd->state_idx];
41 if (!psci_pd_allow_domain_state)
44 /* OSI mode is enabled, set the corresponding domain state. */
45 pd_state = state->data;
46 psci_set_domain_state(*pd_state);
51 static int psci_pd_init(struct device_node *np, bool use_osi)
53 struct generic_pm_domain *pd;
54 struct psci_pd_provider *pd_provider;
55 struct dev_power_governor *pd_gov;
58 pd = dt_idle_pd_alloc(np, psci_dt_parse_state_node);
62 pd_provider = kzalloc(sizeof(*pd_provider), GFP_KERNEL);
66 pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;
69 * Allow power off when OSI has been successfully enabled.
70 * PREEMPT_RT is not yet ready to enter domain idle states.
72 if (use_osi && !IS_ENABLED(CONFIG_PREEMPT_RT))
73 pd->power_off = psci_pd_power_off;
75 pd->flags |= GENPD_FLAG_ALWAYS_ON;
77 /* Use governor for CPU PM domains if it has some states to manage. */
78 pd_gov = pd->states ? &pm_domain_cpu_gov : NULL;
80 ret = pm_genpd_init(pd, pd_gov, false);
84 ret = of_genpd_add_provider_simple(np, pd);
88 pd_provider->node = of_node_get(np);
89 list_add(&pd_provider->link, &psci_pd_providers);
91 pr_debug("init PM domain %s\n", pd->name);
101 pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);
105 static void psci_pd_remove(void)
107 struct psci_pd_provider *pd_provider, *it;
108 struct generic_pm_domain *genpd;
110 list_for_each_entry_safe_reverse(pd_provider, it,
111 &psci_pd_providers, link) {
112 of_genpd_del_provider(pd_provider->node);
114 genpd = of_genpd_remove_last(pd_provider->node);
118 of_node_put(pd_provider->node);
119 list_del(&pd_provider->link);
124 static void psci_cpuidle_domain_sync_state(struct device *dev)
127 * All devices have now been attached/probed to the PM domain topology,
128 * hence it's fine to allow domain states to be picked.
130 psci_pd_allow_domain_state = true;
133 static const struct of_device_id psci_of_match[] = {
134 { .compatible = "arm,psci-1.0" },
138 static int psci_cpuidle_domain_probe(struct platform_device *pdev)
140 struct device_node *np = pdev->dev.of_node;
141 struct device_node *node;
142 bool use_osi = psci_has_osi_support();
143 int ret = 0, pd_count = 0;
149 * Parse child nodes for the "#power-domain-cells" property and
150 * initialize a genpd/genpd-of-provider pair when it's found.
152 for_each_child_of_node(np, node) {
153 if (!of_property_present(node, "#power-domain-cells"))
156 ret = psci_pd_init(node, use_osi);
165 /* Bail out if not using the hierarchical CPU topology. */
169 /* Link genpd masters/subdomains to model the CPU topology. */
170 ret = dt_idle_pd_init_topology(np);
174 /* let's try to enable OSI. */
175 ret = psci_set_osi_mode(use_osi);
179 pr_info("Initialized CPU PM domain topology using %s mode\n",
180 use_osi ? "OSI" : "PC");
184 dt_idle_pd_remove_topology(np);
187 pr_err("failed to create CPU PM domains ret=%d\n", ret);
191 static struct platform_driver psci_cpuidle_domain_driver = {
192 .probe = psci_cpuidle_domain_probe,
194 .name = "psci-cpuidle-domain",
195 .of_match_table = psci_of_match,
196 .sync_state = psci_cpuidle_domain_sync_state,
200 static int __init psci_idle_init_domains(void)
202 return platform_driver_register(&psci_cpuidle_domain_driver);
204 core_initcall(psci_idle_init_domains);