1
2
3
4
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
30static unsigned int nb_available_cpus;
31static int tos_resident_cpu = -1;
32
33static atomic_t nb_active_threads;
34static struct completion suspend_threads_started =
35 COMPLETION_INITIALIZER(suspend_threads_started);
36static struct completion suspend_threads_done =
37 COMPLETION_INITIALIZER(suspend_threads_done);
38
39
40
41
42
43
44
45static 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
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
75
76
77static 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
86 for_each_cpu(cpu, cpus) {
87 int ret = cpu_down(cpu);
88
89
90
91
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
118 for_each_cpu(cpu, offlined_cpus) {
119 int ret = cpu_up(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
132
133
134 WARN_ON(!cpumask_empty(offlined_cpus) ||
135 num_online_cpus() != nb_available_cpus);
136
137 return err;
138}
139
140static 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
150static 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 = kcalloc(nb_available_cpus, sizeof(cpu_groups),
159 GFP_KERNEL);
160 if (!cpu_groups)
161 return -ENOMEM;
162
163 cpumask_copy(tmp, cpu_online_mask);
164
165 while (!cpumask_empty(tmp)) {
166 const struct cpumask *cpu_group =
167 topology_core_cpumask(cpumask_any(tmp));
168
169 if (!alloc_cpumask_var(&cpu_groups[num_groups], GFP_KERNEL)) {
170 free_cpu_groups(num_groups, &cpu_groups);
171 return -ENOMEM;
172 }
173 cpumask_copy(cpu_groups[num_groups++], cpu_group);
174 cpumask_andnot(tmp, tmp, cpu_group);
175 }
176
177 free_cpumask_var(tmp);
178 *pcpu_groups = cpu_groups;
179
180 return num_groups;
181}
182
183static int hotplug_tests(void)
184{
185 int i, nb_cpu_group, err = -ENOMEM;
186 cpumask_var_t offlined_cpus, *cpu_groups;
187 char *page_buf;
188
189 if (!alloc_cpumask_var(&offlined_cpus, GFP_KERNEL))
190 return err;
191
192 nb_cpu_group = alloc_init_cpu_groups(&cpu_groups);
193 if (nb_cpu_group < 0)
194 goto out_free_cpus;
195 page_buf = (char *)__get_free_page(GFP_KERNEL);
196 if (!page_buf)
197 goto out_free_cpu_groups;
198
199 err = 0;
200
201
202
203
204 pr_info("Trying to turn off and on again all CPUs\n");
205 err += down_and_up_cpus(cpu_online_mask, offlined_cpus);
206
207
208
209
210
211 for (i = 0; i < nb_cpu_group; ++i) {
212 ssize_t len = cpumap_print_to_pagebuf(true, page_buf,
213 cpu_groups[i]);
214
215 page_buf[len - 1] = '\0';
216 pr_info("Trying to turn off and on again group %d (CPUs %s)\n",
217 i, page_buf);
218 err += down_and_up_cpus(cpu_groups[i], offlined_cpus);
219 }
220
221 free_page((unsigned long)page_buf);
222out_free_cpu_groups:
223 free_cpu_groups(nb_cpu_group, &cpu_groups);
224out_free_cpus:
225 free_cpumask_var(offlined_cpus);
226 return err;
227}
228
229static void dummy_callback(struct timer_list *unused) {}
230
231static int suspend_cpu(int index, bool broadcast)
232{
233 int ret;
234
235 arch_cpu_idle_enter();
236
237 if (broadcast) {
238
239
240
241
242 ret = tick_broadcast_enter();
243 if (ret) {
244
245
246
247
248
249
250
251 cpu_do_idle();
252 ret = 0;
253 goto out_arch_exit;
254 }
255 }
256
257
258
259
260
261 ret = CPU_PM_CPU_IDLE_ENTER(arm_cpuidle_suspend, index);
262
263 if (broadcast)
264 tick_broadcast_exit();
265
266out_arch_exit:
267 arch_cpu_idle_exit();
268
269 return ret;
270}
271
272static int suspend_test_thread(void *arg)
273{
274 int cpu = (long)arg;
275 int i, nb_suspend = 0, nb_shallow_sleep = 0, nb_err = 0;
276 struct sched_param sched_priority = { .sched_priority = MAX_RT_PRIO-1 };
277 struct cpuidle_device *dev;
278 struct cpuidle_driver *drv;
279
280 struct timer_list wakeup_timer;
281
282
283 wait_for_completion(&suspend_threads_started);
284
285
286 if (sched_setscheduler_nocheck(current, SCHED_FIFO, &sched_priority))
287 pr_warn("Failed to set suspend thread scheduler on CPU %d\n",
288 cpu);
289
290 dev = this_cpu_read(cpuidle_devices);
291 drv = cpuidle_get_cpu_driver(dev);
292
293 pr_info("CPU %d entering suspend cycles, states 1 through %d\n",
294 cpu, drv->state_count - 1);
295
296 timer_setup_on_stack(&wakeup_timer, dummy_callback, 0);
297 for (i = 0; i < NUM_SUSPEND_CYCLE; ++i) {
298 int index;
299
300
301
302
303 for (index = 1; index < drv->state_count; ++index) {
304 struct cpuidle_state *state = &drv->states[index];
305 bool broadcast = state->flags & CPUIDLE_FLAG_TIMER_STOP;
306 int ret;
307
308
309
310
311
312
313
314
315 mod_timer(&wakeup_timer, jiffies +
316 usecs_to_jiffies(state->target_residency));
317
318
319 local_irq_disable();
320
321 ret = suspend_cpu(index, broadcast);
322
323
324
325
326
327
328 local_irq_enable();
329
330 if (ret == index) {
331 ++nb_suspend;
332 } else if (ret >= 0) {
333
334 ++nb_shallow_sleep;
335 } else {
336 pr_err("Failed to suspend CPU %d: error %d "
337 "(requested state %d, cycle %d)\n",
338 cpu, ret, index, i);
339 ++nb_err;
340 }
341 }
342 }
343
344
345
346
347
348 del_timer(&wakeup_timer);
349 destroy_timer_on_stack(&wakeup_timer);
350
351 if (atomic_dec_return_relaxed(&nb_active_threads) == 0)
352 complete(&suspend_threads_done);
353
354
355 sched_priority.sched_priority = 0;
356 if (sched_setscheduler_nocheck(current, SCHED_NORMAL, &sched_priority))
357 pr_warn("Failed to set suspend thread scheduler on CPU %d\n",
358 cpu);
359 for (;;) {
360
361 set_current_state(TASK_INTERRUPTIBLE);
362 if (kthread_should_park())
363 break;
364 schedule();
365 }
366
367 pr_info("CPU %d suspend test results: success %d, shallow states %d, errors %d\n",
368 cpu, nb_suspend, nb_shallow_sleep, nb_err);
369
370 kthread_parkme();
371
372 return nb_err;
373}
374
375static int suspend_tests(void)
376{
377 int i, cpu, err = 0;
378 struct task_struct **threads;
379 int nb_threads = 0;
380
381 threads = kmalloc_array(nb_available_cpus, sizeof(*threads),
382 GFP_KERNEL);
383 if (!threads)
384 return -ENOMEM;
385
386
387
388
389
390
391
392
393 cpuidle_pause_and_lock();
394
395 for_each_online_cpu(cpu) {
396 struct task_struct *thread;
397
398 struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
399 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
400
401 if (!dev || !drv) {
402 pr_warn("cpuidle not available on CPU %d, ignoring\n",
403 cpu);
404 continue;
405 }
406
407 thread = kthread_create_on_cpu(suspend_test_thread,
408 (void *)(long)cpu, cpu,
409 "psci_suspend_test");
410 if (IS_ERR(thread))
411 pr_err("Failed to create kthread on CPU %d\n", cpu);
412 else
413 threads[nb_threads++] = thread;
414 }
415
416 if (nb_threads < 1) {
417 err = -ENODEV;
418 goto out;
419 }
420
421 atomic_set(&nb_active_threads, nb_threads);
422
423
424
425
426
427
428 for (i = 0; i < nb_threads; ++i)
429 wake_up_process(threads[i]);
430 complete_all(&suspend_threads_started);
431
432 wait_for_completion(&suspend_threads_done);
433
434
435
436 for (i = 0; i < nb_threads; ++i) {
437 err += kthread_park(threads[i]);
438 err += kthread_stop(threads[i]);
439 }
440 out:
441 cpuidle_resume_and_unlock();
442 kfree(threads);
443 return err;
444}
445
446static int __init psci_checker(void)
447{
448 int ret;
449
450
451
452
453
454
455
456
457
458
459 nb_available_cpus = num_online_cpus();
460
461
462 ret = psci_ops_check();
463 if (ret)
464 return ret;
465
466 pr_info("PSCI checker started using %u CPUs\n", nb_available_cpus);
467
468 pr_info("Starting hotplug tests\n");
469 ret = hotplug_tests();
470 if (ret == 0)
471 pr_info("Hotplug tests passed OK\n");
472 else if (ret > 0)
473 pr_err("%d error(s) encountered in hotplug tests\n", ret);
474 else {
475 pr_err("Out of memory\n");
476 return ret;
477 }
478
479 pr_info("Starting suspend tests (%d cycles per state)\n",
480 NUM_SUSPEND_CYCLE);
481 ret = suspend_tests();
482 if (ret == 0)
483 pr_info("Suspend tests passed OK\n");
484 else if (ret > 0)
485 pr_err("%d error(s) encountered in suspend tests\n", ret);
486 else {
487 switch (ret) {
488 case -ENOMEM:
489 pr_err("Out of memory\n");
490 break;
491 case -ENODEV:
492 pr_warn("Could not start suspend tests on any CPU\n");
493 break;
494 }
495 }
496
497 pr_info("PSCI checker completed\n");
498 return ret < 0 ? ret : 0;
499}
500late_initcall(psci_checker);
501