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