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