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