1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PM domains for CPUs via genpd - managed by cpuidle-psci.
4 *
5 * Copyright (C) 2019 Linaro Ltd.
6 * Author: Ulf Hansson <ulf.hansson@linaro.org>
7 *
8 */
9
10 #define pr_fmt(fmt) "CPUidle PSCI: " fmt
11
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>
21
22 #include "cpuidle-psci.h"
23
24 struct psci_pd_provider {
25 struct list_head link;
26 struct device_node *node;
27 };
28
29 static LIST_HEAD(psci_pd_providers);
30 static bool psci_pd_allow_domain_state;
31
psci_pd_power_off(struct generic_pm_domain * pd)32 static int psci_pd_power_off(struct generic_pm_domain *pd)
33 {
34 struct genpd_power_state *state = &pd->states[pd->state_idx];
35 u32 *pd_state;
36
37 if (!state->data)
38 return 0;
39
40 if (!psci_pd_allow_domain_state)
41 return -EBUSY;
42
43 /* OSI mode is enabled, set the corresponding domain state. */
44 pd_state = state->data;
45 psci_set_domain_state(*pd_state);
46
47 return 0;
48 }
49
psci_pd_init(struct device_node * np,bool use_osi)50 static int psci_pd_init(struct device_node *np, bool use_osi)
51 {
52 struct generic_pm_domain *pd;
53 struct psci_pd_provider *pd_provider;
54 struct dev_power_governor *pd_gov;
55 int ret = -ENOMEM;
56
57 pd = dt_idle_pd_alloc(np, psci_dt_parse_state_node);
58 if (!pd)
59 goto out;
60
61 pd_provider = kzalloc(sizeof(*pd_provider), GFP_KERNEL);
62 if (!pd_provider)
63 goto free_pd;
64
65 pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;
66
67 /*
68 * Allow power off when OSI has been successfully enabled.
69 * PREEMPT_RT is not yet ready to enter domain idle states.
70 */
71 if (use_osi && !IS_ENABLED(CONFIG_PREEMPT_RT))
72 pd->power_off = psci_pd_power_off;
73 else
74 pd->flags |= GENPD_FLAG_ALWAYS_ON;
75
76 /* Use governor for CPU PM domains if it has some states to manage. */
77 pd_gov = pd->states ? &pm_domain_cpu_gov : NULL;
78
79 ret = pm_genpd_init(pd, pd_gov, false);
80 if (ret)
81 goto free_pd_prov;
82
83 ret = of_genpd_add_provider_simple(np, pd);
84 if (ret)
85 goto remove_pd;
86
87 pd_provider->node = of_node_get(np);
88 list_add(&pd_provider->link, &psci_pd_providers);
89
90 pr_debug("init PM domain %s\n", pd->name);
91 return 0;
92
93 remove_pd:
94 pm_genpd_remove(pd);
95 free_pd_prov:
96 kfree(pd_provider);
97 free_pd:
98 dt_idle_pd_free(pd);
99 out:
100 pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);
101 return ret;
102 }
103
psci_pd_remove(void)104 static void psci_pd_remove(void)
105 {
106 struct psci_pd_provider *pd_provider, *it;
107 struct generic_pm_domain *genpd;
108
109 list_for_each_entry_safe(pd_provider, it, &psci_pd_providers, link) {
110 of_genpd_del_provider(pd_provider->node);
111
112 genpd = of_genpd_remove_last(pd_provider->node);
113 if (!IS_ERR(genpd))
114 kfree(genpd);
115
116 of_node_put(pd_provider->node);
117 list_del(&pd_provider->link);
118 kfree(pd_provider);
119 }
120 }
121
psci_pd_try_set_osi_mode(void)122 static bool psci_pd_try_set_osi_mode(void)
123 {
124 int ret;
125
126 if (!psci_has_osi_support())
127 return false;
128
129 ret = psci_set_osi_mode(true);
130 if (ret)
131 return false;
132
133 return true;
134 }
135
psci_cpuidle_domain_sync_state(struct device * dev)136 static void psci_cpuidle_domain_sync_state(struct device *dev)
137 {
138 /*
139 * All devices have now been attached/probed to the PM domain topology,
140 * hence it's fine to allow domain states to be picked.
141 */
142 psci_pd_allow_domain_state = true;
143 }
144
145 static const struct of_device_id psci_of_match[] = {
146 { .compatible = "arm,psci-1.0" },
147 {}
148 };
149
psci_cpuidle_domain_probe(struct platform_device * pdev)150 static int psci_cpuidle_domain_probe(struct platform_device *pdev)
151 {
152 struct device_node *np = pdev->dev.of_node;
153 struct device_node *node;
154 bool use_osi;
155 int ret = 0, pd_count = 0;
156
157 if (!np)
158 return -ENODEV;
159
160 /* If OSI mode is supported, let's try to enable it. */
161 use_osi = psci_pd_try_set_osi_mode();
162
163 /*
164 * Parse child nodes for the "#power-domain-cells" property and
165 * initialize a genpd/genpd-of-provider pair when it's found.
166 */
167 for_each_child_of_node(np, node) {
168 if (!of_find_property(node, "#power-domain-cells", NULL))
169 continue;
170
171 ret = psci_pd_init(node, use_osi);
172 if (ret)
173 goto put_node;
174
175 pd_count++;
176 }
177
178 /* Bail out if not using the hierarchical CPU topology. */
179 if (!pd_count)
180 goto no_pd;
181
182 /* Link genpd masters/subdomains to model the CPU topology. */
183 ret = dt_idle_pd_init_topology(np);
184 if (ret)
185 goto remove_pd;
186
187 pr_info("Initialized CPU PM domain topology using %s mode\n",
188 use_osi ? "OSI" : "PC");
189 return 0;
190
191 put_node:
192 of_node_put(node);
193 remove_pd:
194 psci_pd_remove();
195 pr_err("failed to create CPU PM domains ret=%d\n", ret);
196 no_pd:
197 if (use_osi)
198 psci_set_osi_mode(false);
199 return ret;
200 }
201
202 static struct platform_driver psci_cpuidle_domain_driver = {
203 .probe = psci_cpuidle_domain_probe,
204 .driver = {
205 .name = "psci-cpuidle-domain",
206 .of_match_table = psci_of_match,
207 .sync_state = psci_cpuidle_domain_sync_state,
208 },
209 };
210
psci_idle_init_domains(void)211 static int __init psci_idle_init_domains(void)
212 {
213 return platform_driver_register(&psci_cpuidle_domain_driver);
214 }
215 subsys_initcall(psci_idle_init_domains);
216