xref: /linux/drivers/firmware/psci/psci_checker.c (revision ae814200e8393fa504dd246e98fcba8f5493de28)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2016 ARM Limited
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/atomic.h>
10 #include <linux/completion.h>
11 #include <linux/cpu.h>
12 #include <linux/cpuidle.h>
13 #include <linux/cpu_pm.h>
14 #include <linux/kernel.h>
15 #include <linux/kthread.h>
16 #include <uapi/linux/sched/types.h>
17 #include <linux/module.h>
18 #include <linux/preempt.h>
19 #include <linux/psci.h>
20 #include <linux/slab.h>
21 #include <linux/tick.h>
22 #include <linux/topology.h>
23 
24 #include <asm/cpuidle.h>
25 
26 #include <uapi/linux/psci.h>
27 
28 #define NUM_SUSPEND_CYCLE (10)
29 
30 static unsigned int nb_available_cpus;
31 static int tos_resident_cpu = -1;
32 
33 static atomic_t nb_active_threads;
34 static struct completion suspend_threads_started =
35 	COMPLETION_INITIALIZER(suspend_threads_started);
36 static struct completion suspend_threads_done =
37 	COMPLETION_INITIALIZER(suspend_threads_done);
38 
39 /*
40  * We assume that PSCI operations are used if they are available. This is not
41  * necessarily true on arm64, since the decision is based on the
42  * "enable-method" property of each CPU in the DT, but given that there is no
43  * arch-specific way to check this, we assume that the DT is sensible.
44  */
psci_ops_check(void)45 static int psci_ops_check(void)
46 {
47 	int migrate_type = -1;
48 	int cpu;
49 
50 	if (!(psci_ops.cpu_off && psci_ops.cpu_on && psci_ops.cpu_suspend)) {
51 		pr_warn("Missing PSCI operations, aborting tests\n");
52 		return -EOPNOTSUPP;
53 	}
54 
55 	if (psci_ops.migrate_info_type)
56 		migrate_type = psci_ops.migrate_info_type();
57 
58 	if (migrate_type == PSCI_0_2_TOS_UP_MIGRATE ||
59 	    migrate_type == PSCI_0_2_TOS_UP_NO_MIGRATE) {
60 		/* There is a UP Trusted OS, find on which core it resides. */
61 		for_each_online_cpu(cpu)
62 			if (psci_tos_resident_on(cpu)) {
63 				tos_resident_cpu = cpu;
64 				break;
65 			}
66 		if (tos_resident_cpu == -1)
67 			pr_warn("UP Trusted OS resides on no online CPU\n");
68 	}
69 
70 	return 0;
71 }
72 
73 /*
74  * offlined_cpus is a temporary array but passing it as an argument avoids
75  * multiple allocations.
76  */
down_and_up_cpus(const struct cpumask * cpus,struct cpumask * offlined_cpus)77 static unsigned int down_and_up_cpus(const struct cpumask *cpus,
78 				     struct cpumask *offlined_cpus)
79 {
80 	int cpu;
81 	int err = 0;
82 
83 	cpumask_clear(offlined_cpus);
84 
85 	/* Try to power down all CPUs in the mask. */
86 	for_each_cpu(cpu, cpus) {
87 		int ret = remove_cpu(cpu);
88 
89 		/*
90 		 * cpu_down() checks the number of online CPUs before the TOS
91 		 * resident CPU.
92 		 */
93 		if (cpumask_weight(offlined_cpus) + 1 == nb_available_cpus) {
94 			if (ret != -EBUSY) {
95 				pr_err("Unexpected return code %d while trying "
96 				       "to power down last online CPU %d\n",
97 				       ret, cpu);
98 				++err;
99 			}
100 		} else if (cpu == tos_resident_cpu) {
101 			if (ret != -EPERM) {
102 				pr_err("Unexpected return code %d while trying "
103 				       "to power down TOS resident CPU %d\n",
104 				       ret, cpu);
105 				++err;
106 			}
107 		} else if (ret != 0) {
108 			pr_err("Error occurred (%d) while trying "
109 			       "to power down CPU %d\n", ret, cpu);
110 			++err;
111 		}
112 
113 		if (ret == 0)
114 			cpumask_set_cpu(cpu, offlined_cpus);
115 	}
116 
117 	/* Try to power up all the CPUs that have been offlined. */
118 	for_each_cpu(cpu, offlined_cpus) {
119 		int ret = add_cpu(cpu);
120 
121 		if (ret != 0) {
122 			pr_err("Error occurred (%d) while trying "
123 			       "to power up CPU %d\n", ret, cpu);
124 			++err;
125 		} else {
126 			cpumask_clear_cpu(cpu, offlined_cpus);
127 		}
128 	}
129 
130 	/*
131 	 * Something went bad at some point and some CPUs could not be turned
132 	 * back on.
133 	 */
134 	WARN_ON(!cpumask_empty(offlined_cpus) ||
135 		num_online_cpus() != nb_available_cpus);
136 
137 	return err;
138 }
139 
free_cpu_groups(int num,cpumask_var_t ** pcpu_groups)140 static void free_cpu_groups(int num, cpumask_var_t **pcpu_groups)
141 {
142 	int i;
143 	cpumask_var_t *cpu_groups = *pcpu_groups;
144 
145 	for (i = 0; i < num; ++i)
146 		free_cpumask_var(cpu_groups[i]);
147 	kfree(cpu_groups);
148 }
149 
alloc_init_cpu_groups(cpumask_var_t ** pcpu_groups)150 static int alloc_init_cpu_groups(cpumask_var_t **pcpu_groups)
151 {
152 	int num_groups = 0;
153 	cpumask_var_t tmp, *cpu_groups;
154 
155 	if (!alloc_cpumask_var(&tmp, GFP_KERNEL))
156 		return -ENOMEM;
157 
158 	cpu_groups = kzalloc_objs(*cpu_groups, nb_available_cpus);
159 	if (!cpu_groups) {
160 		free_cpumask_var(tmp);
161 		return -ENOMEM;
162 	}
163 
164 	cpumask_copy(tmp, cpu_online_mask);
165 
166 	while (!cpumask_empty(tmp)) {
167 		const struct cpumask *cpu_group =
168 			topology_core_cpumask(cpumask_any(tmp));
169 
170 		if (!alloc_cpumask_var(&cpu_groups[num_groups], GFP_KERNEL)) {
171 			free_cpumask_var(tmp);
172 			free_cpu_groups(num_groups, &cpu_groups);
173 			return -ENOMEM;
174 		}
175 		cpumask_copy(cpu_groups[num_groups++], cpu_group);
176 		cpumask_andnot(tmp, tmp, cpu_group);
177 	}
178 
179 	free_cpumask_var(tmp);
180 	*pcpu_groups = cpu_groups;
181 
182 	return num_groups;
183 }
184 
hotplug_tests(void)185 static int hotplug_tests(void)
186 {
187 	int i, nb_cpu_group, err = -ENOMEM;
188 	cpumask_var_t offlined_cpus, *cpu_groups;
189 
190 	if (!alloc_cpumask_var(&offlined_cpus, GFP_KERNEL))
191 		return err;
192 
193 	nb_cpu_group = alloc_init_cpu_groups(&cpu_groups);
194 	if (nb_cpu_group < 0)
195 		goto out_free_cpus;
196 	/*
197 	 * Of course the last CPU cannot be powered down and cpu_down() should
198 	 * refuse doing that.
199 	 */
200 	pr_info("Trying to turn off and on again all CPUs\n");
201 	err = down_and_up_cpus(cpu_online_mask, offlined_cpus);
202 
203 	/*
204 	 * Take down CPUs by cpu group this time. When the last CPU is turned
205 	 * off, the cpu group itself should shut down.
206 	 */
207 	for (i = 0; i < nb_cpu_group; ++i) {
208 		pr_info("Trying to turn off and on again group %d (CPUs %*pbl)\n",
209 			i, cpumask_pr_args(cpu_groups[i]));
210 		err += down_and_up_cpus(cpu_groups[i], offlined_cpus);
211 	}
212 
213 	free_cpu_groups(nb_cpu_group, &cpu_groups);
214 out_free_cpus:
215 	free_cpumask_var(offlined_cpus);
216 	return err;
217 }
218 
dummy_callback(struct timer_list * unused)219 static void dummy_callback(struct timer_list *unused) {}
220 
suspend_cpu(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)221 static int suspend_cpu(struct cpuidle_device *dev,
222 		       struct cpuidle_driver *drv, int index)
223 {
224 	struct cpuidle_state *state = &drv->states[index];
225 	bool broadcast = state->flags & CPUIDLE_FLAG_TIMER_STOP;
226 	int ret;
227 
228 	arch_cpu_idle_enter();
229 
230 	if (broadcast) {
231 		/*
232 		 * The local timer will be shut down, we need to enter tick
233 		 * broadcast.
234 		 */
235 		ret = tick_broadcast_enter();
236 		if (ret) {
237 			/*
238 			 * In the absence of hardware broadcast mechanism,
239 			 * this CPU might be used to broadcast wakeups, which
240 			 * may be why entering tick broadcast has failed.
241 			 * There is little the kernel can do to work around
242 			 * that, so enter WFI instead (idle state 0).
243 			 */
244 			cpu_do_idle();
245 			ret = 0;
246 			goto out_arch_exit;
247 		}
248 	}
249 
250 	ret = state->enter(dev, drv, index);
251 
252 	if (broadcast)
253 		tick_broadcast_exit();
254 
255 out_arch_exit:
256 	arch_cpu_idle_exit();
257 
258 	return ret;
259 }
260 
suspend_test_thread(void * arg)261 static int suspend_test_thread(void *arg)
262 {
263 	int cpu = (long)arg;
264 	int i, nb_suspend = 0, nb_shallow_sleep = 0, nb_err = 0;
265 	struct cpuidle_device *dev;
266 	struct cpuidle_driver *drv;
267 	/* No need for an actual callback, we just want to wake up the CPU. */
268 	struct timer_list wakeup_timer;
269 
270 	/* Wait for the main thread to give the start signal. */
271 	wait_for_completion(&suspend_threads_started);
272 
273 	/* Set maximum priority to preempt all other threads on this CPU. */
274 	sched_set_fifo(current);
275 
276 	dev = this_cpu_read(cpuidle_devices);
277 	drv = cpuidle_get_cpu_driver(dev);
278 
279 	pr_info("CPU %d entering suspend cycles, states 1 through %d\n",
280 		cpu, drv->state_count - 1);
281 
282 	timer_setup_on_stack(&wakeup_timer, dummy_callback, 0);
283 	for (i = 0; i < NUM_SUSPEND_CYCLE; ++i) {
284 		int index;
285 		/*
286 		 * Test all possible states, except 0 (which is usually WFI and
287 		 * doesn't use PSCI).
288 		 */
289 		for (index = 1; index < drv->state_count; ++index) {
290 			int ret;
291 			struct cpuidle_state *state = &drv->states[index];
292 
293 			/*
294 			 * Set the timer to wake this CPU up in some time (which
295 			 * should be largely sufficient for entering suspend).
296 			 * If the local tick is disabled when entering suspend,
297 			 * suspend_cpu() takes care of switching to a broadcast
298 			 * tick, so the timer will still wake us up.
299 			 */
300 			mod_timer(&wakeup_timer, jiffies +
301 				  usecs_to_jiffies(state->target_residency));
302 
303 			/* IRQs must be disabled during suspend operations. */
304 			local_irq_disable();
305 
306 			ret = suspend_cpu(dev, drv, index);
307 
308 			/*
309 			 * We have woken up. Re-enable IRQs to handle any
310 			 * pending interrupt, do not wait until the end of the
311 			 * loop.
312 			 */
313 			local_irq_enable();
314 
315 			if (ret == index) {
316 				++nb_suspend;
317 			} else if (ret >= 0) {
318 				/* We did not enter the expected state. */
319 				++nb_shallow_sleep;
320 			} else {
321 				pr_err("Failed to suspend CPU %d: error %d "
322 				       "(requested state %d, cycle %d)\n",
323 				       cpu, ret, index, i);
324 				++nb_err;
325 			}
326 		}
327 	}
328 
329 	/*
330 	 * Disable the timer to make sure that the timer will not trigger
331 	 * later.
332 	 */
333 	timer_delete(&wakeup_timer);
334 	timer_destroy_on_stack(&wakeup_timer);
335 
336 	if (atomic_dec_return_relaxed(&nb_active_threads) == 0)
337 		complete(&suspend_threads_done);
338 
339 	for (;;) {
340 		/* Needs to be set first to avoid missing a wakeup. */
341 		set_current_state(TASK_INTERRUPTIBLE);
342 		if (kthread_should_park())
343 			break;
344 		schedule();
345 	}
346 
347 	pr_info("CPU %d suspend test results: success %d, shallow states %d, errors %d\n",
348 		cpu, nb_suspend, nb_shallow_sleep, nb_err);
349 
350 	kthread_parkme();
351 
352 	return nb_err;
353 }
354 
suspend_tests(void)355 static int suspend_tests(void)
356 {
357 	int i, cpu, err = 0;
358 	struct task_struct **threads;
359 	int nb_threads = 0;
360 
361 	threads = kmalloc_objs(*threads, nb_available_cpus);
362 	if (!threads)
363 		return -ENOMEM;
364 
365 	/*
366 	 * Stop cpuidle to prevent the idle tasks from entering a deep sleep
367 	 * mode, as it might interfere with the suspend threads on other CPUs.
368 	 * This does not prevent the suspend threads from using cpuidle (only
369 	 * the idle tasks check this status). Take the idle lock so that
370 	 * the cpuidle driver and device look-up can be carried out safely.
371 	 */
372 	cpuidle_pause_and_lock();
373 
374 	for_each_online_cpu(cpu) {
375 		struct task_struct *thread;
376 		/* Check that cpuidle is available on that CPU. */
377 		struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
378 		struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
379 
380 		if (!dev || !drv) {
381 			pr_warn("cpuidle not available on CPU %d, ignoring\n",
382 				cpu);
383 			continue;
384 		}
385 
386 		thread = kthread_create_on_cpu(suspend_test_thread,
387 					       (void *)(long)cpu, cpu,
388 					       "psci_suspend_test");
389 		if (IS_ERR(thread))
390 			pr_err("Failed to create kthread on CPU %d\n", cpu);
391 		else
392 			threads[nb_threads++] = thread;
393 	}
394 
395 	if (nb_threads < 1) {
396 		err = -ENODEV;
397 		goto out;
398 	}
399 
400 	atomic_set(&nb_active_threads, nb_threads);
401 
402 	/*
403 	 * Wake up the suspend threads. To avoid the main thread being preempted
404 	 * before all the threads have been unparked, the suspend threads will
405 	 * wait for the completion of suspend_threads_started.
406 	 */
407 	for (i = 0; i < nb_threads; ++i)
408 		wake_up_process(threads[i]);
409 	complete_all(&suspend_threads_started);
410 
411 	wait_for_completion(&suspend_threads_done);
412 
413 
414 	/* Stop and destroy all threads, get return status. */
415 	for (i = 0; i < nb_threads; ++i) {
416 		err += kthread_park(threads[i]);
417 		err += kthread_stop(threads[i]);
418 	}
419  out:
420 	cpuidle_resume_and_unlock();
421 	kfree(threads);
422 	return err;
423 }
424 
psci_checker(void)425 static int __init psci_checker(void)
426 {
427 	int ret;
428 
429 	/*
430 	 * Since we're in an initcall, we assume that all the CPUs that all
431 	 * CPUs that can be onlined have been onlined.
432 	 *
433 	 * The tests assume that hotplug is enabled but nobody else is using it,
434 	 * otherwise the results will be unpredictable. However, since there
435 	 * is no userspace yet in initcalls, that should be fine, as long as
436 	 * no torture test is running at the same time (see Kconfig).
437 	 */
438 	nb_available_cpus = num_online_cpus();
439 
440 	/* Check PSCI operations are set up and working. */
441 	ret = psci_ops_check();
442 	if (ret)
443 		return ret;
444 
445 	pr_info("PSCI checker started using %u CPUs\n", nb_available_cpus);
446 
447 	pr_info("Starting hotplug tests\n");
448 	ret = hotplug_tests();
449 	if (ret == 0)
450 		pr_info("Hotplug tests passed OK\n");
451 	else if (ret > 0)
452 		pr_err("%d error(s) encountered in hotplug tests\n", ret);
453 	else {
454 		pr_err("Out of memory\n");
455 		return ret;
456 	}
457 
458 	pr_info("Starting suspend tests (%d cycles per state)\n",
459 		NUM_SUSPEND_CYCLE);
460 	ret = suspend_tests();
461 	if (ret == 0)
462 		pr_info("Suspend tests passed OK\n");
463 	else if (ret > 0)
464 		pr_err("%d error(s) encountered in suspend tests\n", ret);
465 	else {
466 		switch (ret) {
467 		case -ENOMEM:
468 			pr_err("Out of memory\n");
469 			break;
470 		case -ENODEV:
471 			pr_warn("Could not start suspend tests on any CPU\n");
472 			break;
473 		}
474 	}
475 
476 	pr_info("PSCI checker completed\n");
477 	return ret < 0 ? ret : 0;
478 }
479 late_initcall(psci_checker);
480