Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 2 | /* |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2012 ARM Limited |
| 5 | * |
| 6 | * Author: Will Deacon <will.deacon@arm.com> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/init.h> |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 10 | #include <linux/smp.h> |
| 11 | #include <linux/of.h> |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 12 | #include <linux/delay.h> |
Mark Rutland | be12039 | 2015-07-31 15:46:19 +0100 | [diff] [blame] | 13 | #include <linux/psci.h> |
| 14 | |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 15 | #include <uapi/linux/psci.h> |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 16 | |
| 17 | #include <asm/psci.h> |
| 18 | #include <asm/smp_plat.h> |
| 19 | |
| 20 | /* |
| 21 | * psci_smp assumes that the following is true about PSCI: |
| 22 | * |
| 23 | * cpu_suspend Suspend the execution on a CPU |
| 24 | * @state we don't currently describe affinity levels, so just pass 0. |
| 25 | * @entry_point the first instruction to be executed on return |
| 26 | * returns 0 success, < 0 on failure |
| 27 | * |
| 28 | * cpu_off Power down a CPU |
| 29 | * @state we don't currently describe affinity levels, so just pass 0. |
| 30 | * no return on successful call |
| 31 | * |
| 32 | * cpu_on Power up a CPU |
| 33 | * @cpuid cpuid of target CPU, as from MPIDR |
| 34 | * @entry_point the first instruction to be executed on return |
| 35 | * returns 0 success, < 0 on failure |
| 36 | * |
| 37 | * migrate Migrate the context to a different CPU |
| 38 | * @cpuid cpuid of target CPU, as from MPIDR |
| 39 | * returns 0 success, < 0 on failure |
| 40 | * |
| 41 | */ |
| 42 | |
| 43 | extern void secondary_startup(void); |
| 44 | |
Paul Gortmaker | 8bd26e3 | 2013-06-17 15:43:14 -0400 | [diff] [blame] | 45 | static int psci_boot_secondary(unsigned int cpu, struct task_struct *idle) |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 46 | { |
| 47 | if (psci_ops.cpu_on) |
| 48 | return psci_ops.cpu_on(cpu_logical_map(cpu), |
Grygorii Strashko | 37cf524 | 2015-07-31 15:46:18 +0100 | [diff] [blame] | 49 | virt_to_idmap(&secondary_startup)); |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 50 | return -ENODEV; |
| 51 | } |
| 52 | |
| 53 | #ifdef CONFIG_HOTPLUG_CPU |
Mark Rutland | be12039 | 2015-07-31 15:46:19 +0100 | [diff] [blame] | 54 | int psci_cpu_disable(unsigned int cpu) |
| 55 | { |
| 56 | /* Fail early if we don't have CPU_OFF support */ |
| 57 | if (!psci_ops.cpu_off) |
| 58 | return -EOPNOTSUPP; |
| 59 | |
| 60 | /* Trusted OS will deny CPU_OFF */ |
| 61 | if (psci_tos_resident_on(cpu)) |
| 62 | return -EPERM; |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Stephen Boyd | b96fc2f | 2015-10-19 13:05:33 -0700 | [diff] [blame] | 67 | void psci_cpu_die(unsigned int cpu) |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 68 | { |
Mark Rutland | be12039 | 2015-07-31 15:46:19 +0100 | [diff] [blame] | 69 | u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN << |
| 70 | PSCI_0_2_POWER_STATE_TYPE_SHIFT; |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 71 | |
Mark Rutland | be12039 | 2015-07-31 15:46:19 +0100 | [diff] [blame] | 72 | if (psci_ops.cpu_off) |
| 73 | psci_ops.cpu_off(state); |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 74 | |
Mark Rutland | be12039 | 2015-07-31 15:46:19 +0100 | [diff] [blame] | 75 | /* We should never return */ |
| 76 | panic("psci: cpu %d failed to shutdown\n", cpu); |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 77 | } |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 78 | |
Stephen Boyd | b96fc2f | 2015-10-19 13:05:33 -0700 | [diff] [blame] | 79 | int psci_cpu_kill(unsigned int cpu) |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 80 | { |
| 81 | int err, i; |
| 82 | |
| 83 | if (!psci_ops.affinity_info) |
| 84 | return 1; |
| 85 | /* |
| 86 | * cpu_kill could race with cpu_die and we can |
| 87 | * potentially end up declaring this cpu undead |
| 88 | * while it is dying. So, try again a few times. |
| 89 | */ |
| 90 | |
| 91 | for (i = 0; i < 10; i++) { |
| 92 | err = psci_ops.affinity_info(cpu_logical_map(cpu), 0); |
| 93 | if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) { |
| 94 | pr_info("CPU%d killed.\n", cpu); |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | msleep(10); |
| 99 | pr_info("Retrying again to check for CPU kill\n"); |
| 100 | } |
| 101 | |
| 102 | pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n", |
| 103 | cpu, err); |
| 104 | /* Make platform_cpu_kill() fail. */ |
| 105 | return 0; |
| 106 | } |
| 107 | |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 108 | #endif |
| 109 | |
| 110 | bool __init psci_smp_available(void) |
| 111 | { |
| 112 | /* is cpu_on available at least? */ |
| 113 | return (psci_ops.cpu_on != NULL); |
| 114 | } |
| 115 | |
Masahiro Yamada | 7530527 | 2015-11-15 10:39:53 +0900 | [diff] [blame] | 116 | const struct smp_operations psci_smp_ops __initconst = { |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 117 | .smp_boot_secondary = psci_boot_secondary, |
Arnd Bergmann | fdeb94b | 2013-06-03 23:09:14 +0100 | [diff] [blame] | 118 | #ifdef CONFIG_HOTPLUG_CPU |
Mark Rutland | be12039 | 2015-07-31 15:46:19 +0100 | [diff] [blame] | 119 | .cpu_disable = psci_cpu_disable, |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 120 | .cpu_die = psci_cpu_die, |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 121 | .cpu_kill = psci_cpu_kill, |
Arnd Bergmann | fdeb94b | 2013-06-03 23:09:14 +0100 | [diff] [blame] | 122 | #endif |
Stefano Stabellini | 0577408 | 2013-05-21 14:24:11 +0000 | [diff] [blame] | 123 | }; |