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