1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * PSCI CPU idle driver.
4  *
5  * Copyright (C) 2019 ARM Ltd.
6  * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
7  */
8 
9 #define pr_fmt(fmt) "CPUidle PSCI: " fmt
10 
11 #include <linux/cpuhotplug.h>
12 #include <linux/cpu_cooling.h>
13 #include <linux/cpuidle.h>
14 #include <linux/cpumask.h>
15 #include <linux/cpu_pm.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/psci.h>
22 #include <linux/pm_domain.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/syscore_ops.h>
27 
28 #include <asm/cpuidle.h>
29 
30 #include "cpuidle-psci.h"
31 #include "dt_idle_states.h"
32 
33 struct psci_cpuidle_data {
34 	u32 *psci_states;
35 	struct device *dev;
36 };
37 
38 static DEFINE_PER_CPU_READ_MOSTLY(struct psci_cpuidle_data, psci_cpuidle_data);
39 static DEFINE_PER_CPU(u32, domain_state);
40 static bool psci_cpuidle_use_cpuhp;
41 
42 void psci_set_domain_state(u32 state)
43 {
44 	__this_cpu_write(domain_state, state);
45 }
46 
47 static inline u32 psci_get_domain_state(void)
48 {
49 	return __this_cpu_read(domain_state);
50 }
51 
52 static inline int psci_enter_state(int idx, u32 state)
53 {
54 	return CPU_PM_CPU_IDLE_ENTER_PARAM(psci_cpu_suspend_enter, idx, state);
55 }
56 
57 static int __psci_enter_domain_idle_state(struct cpuidle_device *dev,
58 					  struct cpuidle_driver *drv, int idx,
59 					  bool s2idle)
60 {
61 	struct psci_cpuidle_data *data = this_cpu_ptr(&psci_cpuidle_data);
62 	u32 *states = data->psci_states;
63 	struct device *pd_dev = data->dev;
64 	u32 state;
65 	int ret;
66 
67 	ret = cpu_pm_enter();
68 	if (ret)
69 		return -1;
70 
71 	/* Do runtime PM to manage a hierarchical CPU toplogy. */
72 	if (s2idle)
73 		dev_pm_genpd_suspend(pd_dev);
74 	else
75 		pm_runtime_put_sync_suspend(pd_dev);
76 
77 	ct_idle_enter();
78 
79 	state = psci_get_domain_state();
80 	if (!state)
81 		state = states[idx];
82 
83 	ret = psci_cpu_suspend_enter(state) ? -1 : idx;
84 
85 	ct_idle_exit();
86 
87 	if (s2idle)
88 		dev_pm_genpd_resume(pd_dev);
89 	else
90 		pm_runtime_get_sync(pd_dev);
91 
92 	cpu_pm_exit();
93 
94 	/* Clear the domain state to start fresh when back from idle. */
95 	psci_set_domain_state(0);
96 	return ret;
97 }
98 
99 static int psci_enter_domain_idle_state(struct cpuidle_device *dev,
100 					struct cpuidle_driver *drv, int idx)
101 {
102 	return __psci_enter_domain_idle_state(dev, drv, idx, false);
103 }
104 
105 static int psci_enter_s2idle_domain_idle_state(struct cpuidle_device *dev,
106 					       struct cpuidle_driver *drv,
107 					       int idx)
108 {
109 	return __psci_enter_domain_idle_state(dev, drv, idx, true);
110 }
111 
112 static int psci_idle_cpuhp_up(unsigned int cpu)
113 {
114 	struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
115 
116 	if (pd_dev)
117 		pm_runtime_get_sync(pd_dev);
118 
119 	return 0;
120 }
121 
122 static int psci_idle_cpuhp_down(unsigned int cpu)
123 {
124 	struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
125 
126 	if (pd_dev) {
127 		pm_runtime_put_sync(pd_dev);
128 		/* Clear domain state to start fresh at next online. */
129 		psci_set_domain_state(0);
130 	}
131 
132 	return 0;
133 }
134 
135 static void psci_idle_syscore_switch(bool suspend)
136 {
137 	bool cleared = false;
138 	struct device *dev;
139 	int cpu;
140 
141 	for_each_possible_cpu(cpu) {
142 		dev = per_cpu_ptr(&psci_cpuidle_data, cpu)->dev;
143 
144 		if (dev && suspend) {
145 			dev_pm_genpd_suspend(dev);
146 		} else if (dev) {
147 			dev_pm_genpd_resume(dev);
148 
149 			/* Account for userspace having offlined a CPU. */
150 			if (pm_runtime_status_suspended(dev))
151 				pm_runtime_set_active(dev);
152 
153 			/* Clear domain state to re-start fresh. */
154 			if (!cleared) {
155 				psci_set_domain_state(0);
156 				cleared = true;
157 			}
158 		}
159 	}
160 }
161 
162 static int psci_idle_syscore_suspend(void)
163 {
164 	psci_idle_syscore_switch(true);
165 	return 0;
166 }
167 
168 static void psci_idle_syscore_resume(void)
169 {
170 	psci_idle_syscore_switch(false);
171 }
172 
173 static struct syscore_ops psci_idle_syscore_ops = {
174 	.suspend = psci_idle_syscore_suspend,
175 	.resume = psci_idle_syscore_resume,
176 };
177 
178 static void psci_idle_init_cpuhp(void)
179 {
180 	int err;
181 
182 	if (!psci_cpuidle_use_cpuhp)
183 		return;
184 
185 	register_syscore_ops(&psci_idle_syscore_ops);
186 
187 	err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
188 					"cpuidle/psci:online",
189 					psci_idle_cpuhp_up,
190 					psci_idle_cpuhp_down);
191 	if (err)
192 		pr_warn("Failed %d while setup cpuhp state\n", err);
193 }
194 
195 static int psci_enter_idle_state(struct cpuidle_device *dev,
196 				struct cpuidle_driver *drv, int idx)
197 {
198 	u32 *state = __this_cpu_read(psci_cpuidle_data.psci_states);
199 
200 	return psci_enter_state(idx, state[idx]);
201 }
202 
203 static const struct of_device_id psci_idle_state_match[] = {
204 	{ .compatible = "arm,idle-state",
205 	  .data = psci_enter_idle_state },
206 	{ },
207 };
208 
209 int psci_dt_parse_state_node(struct device_node *np, u32 *state)
210 {
211 	int err = of_property_read_u32(np, "arm,psci-suspend-param", state);
212 
213 	if (err) {
214 		pr_warn("%pOF missing arm,psci-suspend-param property\n", np);
215 		return err;
216 	}
217 
218 	if (!psci_power_state_is_valid(*state)) {
219 		pr_warn("Invalid PSCI power state %#x\n", *state);
220 		return -EINVAL;
221 	}
222 
223 	return 0;
224 }
225 
226 static int psci_dt_cpu_init_topology(struct cpuidle_driver *drv,
227 				     struct psci_cpuidle_data *data,
228 				     unsigned int state_count, int cpu)
229 {
230 	/* Currently limit the hierarchical topology to be used in OSI mode. */
231 	if (!psci_has_osi_support())
232 		return 0;
233 
234 	data->dev = psci_dt_attach_cpu(cpu);
235 	if (IS_ERR_OR_NULL(data->dev))
236 		return PTR_ERR_OR_ZERO(data->dev);
237 
238 	/*
239 	 * Using the deepest state for the CPU to trigger a potential selection
240 	 * of a shared state for the domain, assumes the domain states are all
241 	 * deeper states.
242 	 */
243 	drv->states[state_count - 1].flags |= CPUIDLE_FLAG_RCU_IDLE;
244 	drv->states[state_count - 1].enter = psci_enter_domain_idle_state;
245 	drv->states[state_count - 1].enter_s2idle = psci_enter_s2idle_domain_idle_state;
246 	psci_cpuidle_use_cpuhp = true;
247 
248 	return 0;
249 }
250 
251 static int psci_dt_cpu_init_idle(struct device *dev, struct cpuidle_driver *drv,
252 				 struct device_node *cpu_node,
253 				 unsigned int state_count, int cpu)
254 {
255 	int i, ret = 0;
256 	u32 *psci_states;
257 	struct device_node *state_node;
258 	struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
259 
260 	state_count++; /* Add WFI state too */
261 	psci_states = devm_kcalloc(dev, state_count, sizeof(*psci_states),
262 				   GFP_KERNEL);
263 	if (!psci_states)
264 		return -ENOMEM;
265 
266 	for (i = 1; i < state_count; i++) {
267 		state_node = of_get_cpu_state_node(cpu_node, i - 1);
268 		if (!state_node)
269 			break;
270 
271 		ret = psci_dt_parse_state_node(state_node, &psci_states[i]);
272 		of_node_put(state_node);
273 
274 		if (ret)
275 			return ret;
276 
277 		pr_debug("psci-power-state %#x index %d\n", psci_states[i], i);
278 	}
279 
280 	if (i != state_count)
281 		return -ENODEV;
282 
283 	/* Initialize optional data, used for the hierarchical topology. */
284 	ret = psci_dt_cpu_init_topology(drv, data, state_count, cpu);
285 	if (ret < 0)
286 		return ret;
287 
288 	/* Idle states parsed correctly, store them in the per-cpu struct. */
289 	data->psci_states = psci_states;
290 	return 0;
291 }
292 
293 static int psci_cpu_init_idle(struct device *dev, struct cpuidle_driver *drv,
294 			      unsigned int cpu, unsigned int state_count)
295 {
296 	struct device_node *cpu_node;
297 	int ret;
298 
299 	/*
300 	 * If the PSCI cpu_suspend function hook has not been initialized
301 	 * idle states must not be enabled, so bail out
302 	 */
303 	if (!psci_ops.cpu_suspend)
304 		return -EOPNOTSUPP;
305 
306 	cpu_node = of_cpu_device_node_get(cpu);
307 	if (!cpu_node)
308 		return -ENODEV;
309 
310 	ret = psci_dt_cpu_init_idle(dev, drv, cpu_node, state_count, cpu);
311 
312 	of_node_put(cpu_node);
313 
314 	return ret;
315 }
316 
317 static void psci_cpu_deinit_idle(int cpu)
318 {
319 	struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
320 
321 	psci_dt_detach_cpu(data->dev);
322 	psci_cpuidle_use_cpuhp = false;
323 }
324 
325 static int psci_idle_init_cpu(struct device *dev, int cpu)
326 {
327 	struct cpuidle_driver *drv;
328 	struct device_node *cpu_node;
329 	const char *enable_method;
330 	int ret = 0;
331 
332 	cpu_node = of_cpu_device_node_get(cpu);
333 	if (!cpu_node)
334 		return -ENODEV;
335 
336 	/*
337 	 * Check whether the enable-method for the cpu is PSCI, fail
338 	 * if it is not.
339 	 */
340 	enable_method = of_get_property(cpu_node, "enable-method", NULL);
341 	if (!enable_method || (strcmp(enable_method, "psci")))
342 		ret = -ENODEV;
343 
344 	of_node_put(cpu_node);
345 	if (ret)
346 		return ret;
347 
348 	drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
349 	if (!drv)
350 		return -ENOMEM;
351 
352 	drv->name = "psci_idle";
353 	drv->owner = THIS_MODULE;
354 	drv->cpumask = (struct cpumask *)cpumask_of(cpu);
355 
356 	/*
357 	 * PSCI idle states relies on architectural WFI to be represented as
358 	 * state index 0.
359 	 */
360 	drv->states[0].enter = psci_enter_idle_state;
361 	drv->states[0].exit_latency = 1;
362 	drv->states[0].target_residency = 1;
363 	drv->states[0].power_usage = UINT_MAX;
364 	strcpy(drv->states[0].name, "WFI");
365 	strcpy(drv->states[0].desc, "ARM WFI");
366 
367 	/*
368 	 * If no DT idle states are detected (ret == 0) let the driver
369 	 * initialization fail accordingly since there is no reason to
370 	 * initialize the idle driver if only wfi is supported, the
371 	 * default archictectural back-end already executes wfi
372 	 * on idle entry.
373 	 */
374 	ret = dt_init_idle_driver(drv, psci_idle_state_match, 1);
375 	if (ret <= 0)
376 		return ret ? : -ENODEV;
377 
378 	/*
379 	 * Initialize PSCI idle states.
380 	 */
381 	ret = psci_cpu_init_idle(dev, drv, cpu, ret);
382 	if (ret) {
383 		pr_err("CPU %d failed to PSCI idle\n", cpu);
384 		return ret;
385 	}
386 
387 	ret = cpuidle_register(drv, NULL);
388 	if (ret)
389 		goto deinit;
390 
391 	cpuidle_cooling_register(drv);
392 
393 	return 0;
394 deinit:
395 	psci_cpu_deinit_idle(cpu);
396 	return ret;
397 }
398 
399 /*
400  * psci_idle_probe - Initializes PSCI cpuidle driver
401  *
402  * Initializes PSCI cpuidle driver for all CPUs, if any CPU fails
403  * to register cpuidle driver then rollback to cancel all CPUs
404  * registration.
405  */
406 static int psci_cpuidle_probe(struct platform_device *pdev)
407 {
408 	int cpu, ret;
409 	struct cpuidle_driver *drv;
410 	struct cpuidle_device *dev;
411 
412 	for_each_possible_cpu(cpu) {
413 		ret = psci_idle_init_cpu(&pdev->dev, cpu);
414 		if (ret)
415 			goto out_fail;
416 	}
417 
418 	psci_idle_init_cpuhp();
419 	return 0;
420 
421 out_fail:
422 	while (--cpu >= 0) {
423 		dev = per_cpu(cpuidle_devices, cpu);
424 		drv = cpuidle_get_cpu_driver(dev);
425 		cpuidle_unregister(drv);
426 		psci_cpu_deinit_idle(cpu);
427 	}
428 
429 	return ret;
430 }
431 
432 static struct platform_driver psci_cpuidle_driver = {
433 	.probe = psci_cpuidle_probe,
434 	.driver = {
435 		.name = "psci-cpuidle",
436 	},
437 };
438 
439 static int __init psci_idle_init(void)
440 {
441 	struct platform_device *pdev;
442 	int ret;
443 
444 	ret = platform_driver_register(&psci_cpuidle_driver);
445 	if (ret)
446 		return ret;
447 
448 	pdev = platform_device_register_simple("psci-cpuidle", -1, NULL, 0);
449 	if (IS_ERR(pdev)) {
450 		platform_driver_unregister(&psci_cpuidle_driver);
451 		return PTR_ERR(pdev);
452 	}
453 
454 	return 0;
455 }
456 device_initcall(psci_idle_init);
457