1 // SPDX-License-Identifier: GPL-2.0
3 * PM domains for CPUs via genpd - managed by cpuidle-psci.
5 * Copyright (C) 2019 Linaro Ltd.
6 * Author: Ulf Hansson <ulf.hansson@linaro.org>
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"
24 struct psci_pd_provider {
25 struct list_head link;
26 struct device_node *node;
29 static LIST_HEAD(psci_pd_providers);
30 static bool psci_pd_allow_domain_state;
32 static int psci_pd_power_off(struct generic_pm_domain *pd)
34 struct genpd_power_state *state = &pd->states[pd->state_idx];
40 if (!psci_pd_allow_domain_state)
43 /* OSI mode is enabled, set the corresponding domain state. */
44 pd_state = state->data;
45 psci_set_domain_state(*pd_state);
50 static int psci_pd_init(struct device_node *np, bool use_osi)
52 struct generic_pm_domain *pd;
53 struct psci_pd_provider *pd_provider;
54 struct dev_power_governor *pd_gov;
57 pd = dt_idle_pd_alloc(np, psci_dt_parse_state_node);
61 pd_provider = kzalloc(sizeof(*pd_provider), GFP_KERNEL);
65 pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;
67 /* Allow power off when OSI has been successfully enabled. */
69 pd->power_off = psci_pd_power_off;
71 pd->flags |= GENPD_FLAG_ALWAYS_ON;
73 /* Use governor for CPU PM domains if it has some states to manage. */
74 pd_gov = pd->states ? &pm_domain_cpu_gov : NULL;
76 ret = pm_genpd_init(pd, pd_gov, false);
80 ret = of_genpd_add_provider_simple(np, pd);
84 pd_provider->node = of_node_get(np);
85 list_add(&pd_provider->link, &psci_pd_providers);
87 pr_debug("init PM domain %s\n", pd->name);
97 pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);
101 static void psci_pd_remove(void)
103 struct psci_pd_provider *pd_provider, *it;
104 struct generic_pm_domain *genpd;
106 list_for_each_entry_safe(pd_provider, it, &psci_pd_providers, link) {
107 of_genpd_del_provider(pd_provider->node);
109 genpd = of_genpd_remove_last(pd_provider->node);
113 of_node_put(pd_provider->node);
114 list_del(&pd_provider->link);
119 static bool psci_pd_try_set_osi_mode(void)
123 if (!psci_has_osi_support())
126 ret = psci_set_osi_mode(true);
133 static void psci_cpuidle_domain_sync_state(struct device *dev)
136 * All devices have now been attached/probed to the PM domain topology,
137 * hence it's fine to allow domain states to be picked.
139 psci_pd_allow_domain_state = true;
142 static const struct of_device_id psci_of_match[] = {
143 { .compatible = "arm,psci-1.0" },
147 static int psci_cpuidle_domain_probe(struct platform_device *pdev)
149 struct device_node *np = pdev->dev.of_node;
150 struct device_node *node;
152 int ret = 0, pd_count = 0;
157 /* If OSI mode is supported, let's try to enable it. */
158 use_osi = psci_pd_try_set_osi_mode();
161 * Parse child nodes for the "#power-domain-cells" property and
162 * initialize a genpd/genpd-of-provider pair when it's found.
164 for_each_child_of_node(np, node) {
165 if (!of_find_property(node, "#power-domain-cells", NULL))
168 ret = psci_pd_init(node, use_osi);
175 /* Bail out if not using the hierarchical CPU topology. */
179 /* Link genpd masters/subdomains to model the CPU topology. */
180 ret = dt_idle_pd_init_topology(np);
184 pr_info("Initialized CPU PM domain topology using %s mode\n",
185 use_osi ? "OSI" : "PC");
192 pr_err("failed to create CPU PM domains ret=%d\n", ret);
195 psci_set_osi_mode(false);
199 static struct platform_driver psci_cpuidle_domain_driver = {
200 .probe = psci_cpuidle_domain_probe,
202 .name = "psci-cpuidle-domain",
203 .of_match_table = psci_of_match,
204 .sync_state = psci_cpuidle_domain_sync_state,
208 static int __init psci_idle_init_domains(void)
210 return platform_driver_register(&psci_cpuidle_domain_driver);
212 subsys_initcall(psci_idle_init_domains);