Will Deacon | e790f1d | 2012-12-18 17:53:14 +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) 2013 ARM Limited |
| 12 | * |
| 13 | * Author: Will Deacon <will.deacon@arm.com> |
| 14 | */ |
| 15 | |
| 16 | #define pr_fmt(fmt) "psci: " fmt |
| 17 | |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/of.h> |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 20 | #include <linux/smp.h> |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 21 | #include <linux/delay.h> |
Mark Rutland | bff6079 | 2015-07-31 15:46:16 +0100 | [diff] [blame] | 22 | #include <linux/psci.h> |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 23 | #include <linux/slab.h> |
Mark Rutland | bff6079 | 2015-07-31 15:46:16 +0100 | [diff] [blame] | 24 | |
Ashwin Chaugule | e71246a | 2014-04-17 14:38:41 -0400 | [diff] [blame] | 25 | #include <uapi/linux/psci.h> |
Will Deacon | e790f1d | 2012-12-18 17:53:14 +0000 | [diff] [blame] | 26 | |
| 27 | #include <asm/compiler.h> |
Mark Rutland | cd1aebf | 2013-10-24 20:30:15 +0100 | [diff] [blame] | 28 | #include <asm/cpu_ops.h> |
Will Deacon | e790f1d | 2012-12-18 17:53:14 +0000 | [diff] [blame] | 29 | #include <asm/errno.h> |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 30 | #include <asm/smp_plat.h> |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 31 | #include <asm/suspend.h> |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 32 | |
Mark Rutland | c8cc427 | 2015-04-30 17:59:03 +0100 | [diff] [blame] | 33 | static DEFINE_PER_CPU_READ_MOSTLY(u32 *, psci_power_state); |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 34 | |
Lorenzo Pieralisi | 819a882 | 2015-05-13 14:12:46 +0100 | [diff] [blame] | 35 | static int __maybe_unused cpu_psci_cpu_init_idle(unsigned int cpu) |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 36 | { |
| 37 | int i, ret, count = 0; |
Mark Rutland | c8cc427 | 2015-04-30 17:59:03 +0100 | [diff] [blame] | 38 | u32 *psci_states; |
Lorenzo Pieralisi | 819a882 | 2015-05-13 14:12:46 +0100 | [diff] [blame] | 39 | struct device_node *state_node, *cpu_node; |
| 40 | |
| 41 | cpu_node = of_get_cpu_node(cpu, NULL); |
| 42 | if (!cpu_node) |
| 43 | return -ENODEV; |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * If the PSCI cpu_suspend function hook has not been initialized |
| 47 | * idle states must not be enabled, so bail out |
| 48 | */ |
| 49 | if (!psci_ops.cpu_suspend) |
| 50 | return -EOPNOTSUPP; |
| 51 | |
| 52 | /* Count idle states */ |
| 53 | while ((state_node = of_parse_phandle(cpu_node, "cpu-idle-states", |
| 54 | count))) { |
| 55 | count++; |
| 56 | of_node_put(state_node); |
| 57 | } |
| 58 | |
| 59 | if (!count) |
| 60 | return -ENODEV; |
| 61 | |
| 62 | psci_states = kcalloc(count, sizeof(*psci_states), GFP_KERNEL); |
| 63 | if (!psci_states) |
| 64 | return -ENOMEM; |
| 65 | |
| 66 | for (i = 0; i < count; i++) { |
Mark Rutland | c8cc427 | 2015-04-30 17:59:03 +0100 | [diff] [blame] | 67 | u32 state; |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 68 | |
| 69 | state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i); |
| 70 | |
| 71 | ret = of_property_read_u32(state_node, |
| 72 | "arm,psci-suspend-param", |
Mark Rutland | c8cc427 | 2015-04-30 17:59:03 +0100 | [diff] [blame] | 73 | &state); |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 74 | if (ret) { |
| 75 | pr_warn(" * %s missing arm,psci-suspend-param property\n", |
| 76 | state_node->full_name); |
| 77 | of_node_put(state_node); |
| 78 | goto free_mem; |
| 79 | } |
| 80 | |
| 81 | of_node_put(state_node); |
Mark Rutland | c8cc427 | 2015-04-30 17:59:03 +0100 | [diff] [blame] | 82 | pr_debug("psci-power-state %#x index %d\n", state, i); |
| 83 | if (!psci_power_state_is_valid(state)) { |
| 84 | pr_warn("Invalid PSCI power state %#x\n", state); |
| 85 | ret = -EINVAL; |
| 86 | goto free_mem; |
| 87 | } |
| 88 | psci_states[i] = state; |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 89 | } |
| 90 | /* Idle states parsed correctly, initialize per-cpu pointer */ |
| 91 | per_cpu(psci_power_state, cpu) = psci_states; |
| 92 | return 0; |
| 93 | |
| 94 | free_mem: |
| 95 | kfree(psci_states); |
| 96 | return ret; |
| 97 | } |
| 98 | |
Lorenzo Pieralisi | 819a882 | 2015-05-13 14:12:46 +0100 | [diff] [blame] | 99 | static int __init cpu_psci_cpu_init(unsigned int cpu) |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 100 | { |
| 101 | return 0; |
| 102 | } |
| 103 | |
Mark Rutland | cd1aebf | 2013-10-24 20:30:15 +0100 | [diff] [blame] | 104 | static int __init cpu_psci_cpu_prepare(unsigned int cpu) |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 105 | { |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 106 | if (!psci_ops.cpu_on) { |
| 107 | pr_err("no cpu_on method, not booting CPU%d\n", cpu); |
| 108 | return -ENODEV; |
| 109 | } |
| 110 | |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 114 | static int cpu_psci_cpu_boot(unsigned int cpu) |
| 115 | { |
| 116 | int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_entry)); |
| 117 | if (err) |
Vladimir Murzin | 288ac26 | 2014-02-28 09:57:47 +0000 | [diff] [blame] | 118 | pr_err("failed to boot CPU%d (%d)\n", cpu, err); |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 119 | |
| 120 | return err; |
| 121 | } |
| 122 | |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 123 | #ifdef CONFIG_HOTPLUG_CPU |
| 124 | static int cpu_psci_cpu_disable(unsigned int cpu) |
| 125 | { |
| 126 | /* Fail early if we don't have CPU_OFF support */ |
| 127 | if (!psci_ops.cpu_off) |
| 128 | return -EOPNOTSUPP; |
Mark Rutland | ff3010e | 2015-04-22 18:10:26 +0100 | [diff] [blame] | 129 | |
| 130 | /* Trusted OS will deny CPU_OFF */ |
| 131 | if (psci_tos_resident_on(cpu)) |
| 132 | return -EPERM; |
| 133 | |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static void cpu_psci_cpu_die(unsigned int cpu) |
| 138 | { |
| 139 | int ret; |
| 140 | /* |
| 141 | * There are no known implementations of PSCI actually using the |
| 142 | * power state field, pass a sensible default for now. |
| 143 | */ |
Mark Rutland | c8cc427 | 2015-04-30 17:59:03 +0100 | [diff] [blame] | 144 | u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN << |
| 145 | PSCI_0_2_POWER_STATE_TYPE_SHIFT; |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 146 | |
| 147 | ret = psci_ops.cpu_off(state); |
| 148 | |
Vladimir Murzin | 288ac26 | 2014-02-28 09:57:47 +0000 | [diff] [blame] | 149 | pr_crit("unable to power off CPU%u (%d)\n", cpu, ret); |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 150 | } |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 151 | |
| 152 | static int cpu_psci_cpu_kill(unsigned int cpu) |
| 153 | { |
| 154 | int err, i; |
| 155 | |
| 156 | if (!psci_ops.affinity_info) |
Mark Rutland | 6b99c68c | 2015-04-20 17:55:30 +0100 | [diff] [blame] | 157 | return 0; |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 158 | /* |
| 159 | * cpu_kill could race with cpu_die and we can |
| 160 | * potentially end up declaring this cpu undead |
| 161 | * while it is dying. So, try again a few times. |
| 162 | */ |
| 163 | |
| 164 | for (i = 0; i < 10; i++) { |
| 165 | err = psci_ops.affinity_info(cpu_logical_map(cpu), 0); |
| 166 | if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) { |
| 167 | pr_info("CPU%d killed.\n", cpu); |
Mark Rutland | 6b99c68c | 2015-04-20 17:55:30 +0100 | [diff] [blame] | 168 | return 0; |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | msleep(10); |
| 172 | pr_info("Retrying again to check for CPU kill\n"); |
| 173 | } |
| 174 | |
| 175 | pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n", |
| 176 | cpu, err); |
Mark Rutland | 6b99c68c | 2015-04-20 17:55:30 +0100 | [diff] [blame] | 177 | return -ETIMEDOUT; |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 178 | } |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 179 | #endif |
| 180 | |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 181 | static int psci_suspend_finisher(unsigned long index) |
| 182 | { |
Mark Rutland | c8cc427 | 2015-04-30 17:59:03 +0100 | [diff] [blame] | 183 | u32 *state = __this_cpu_read(psci_power_state); |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 184 | |
| 185 | return psci_ops.cpu_suspend(state[index - 1], |
| 186 | virt_to_phys(cpu_resume)); |
| 187 | } |
| 188 | |
| 189 | static int __maybe_unused cpu_psci_cpu_suspend(unsigned long index) |
| 190 | { |
| 191 | int ret; |
Mark Rutland | c8cc427 | 2015-04-30 17:59:03 +0100 | [diff] [blame] | 192 | u32 *state = __this_cpu_read(psci_power_state); |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 193 | /* |
| 194 | * idle state index 0 corresponds to wfi, should never be called |
| 195 | * from the cpu_suspend operations |
| 196 | */ |
| 197 | if (WARN_ON_ONCE(!index)) |
| 198 | return -EINVAL; |
| 199 | |
Mark Rutland | c8cc427 | 2015-04-30 17:59:03 +0100 | [diff] [blame] | 200 | if (!psci_power_state_loses_context(state[index - 1])) |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 201 | ret = psci_ops.cpu_suspend(state[index - 1], 0); |
| 202 | else |
Sudeep Holla | af391b1 | 2015-06-18 15:41:32 +0100 | [diff] [blame] | 203 | ret = cpu_suspend(index, psci_suspend_finisher); |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | |
Mark Rutland | cd1aebf | 2013-10-24 20:30:15 +0100 | [diff] [blame] | 208 | const struct cpu_operations cpu_psci_ops = { |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 209 | .name = "psci", |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 210 | #ifdef CONFIG_CPU_IDLE |
| 211 | .cpu_init_idle = cpu_psci_cpu_init_idle, |
| 212 | .cpu_suspend = cpu_psci_cpu_suspend, |
| 213 | #endif |
Mark Rutland | cd1aebf | 2013-10-24 20:30:15 +0100 | [diff] [blame] | 214 | .cpu_init = cpu_psci_cpu_init, |
| 215 | .cpu_prepare = cpu_psci_cpu_prepare, |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 216 | .cpu_boot = cpu_psci_cpu_boot, |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 217 | #ifdef CONFIG_HOTPLUG_CPU |
| 218 | .cpu_disable = cpu_psci_cpu_disable, |
| 219 | .cpu_die = cpu_psci_cpu_die, |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 220 | .cpu_kill = cpu_psci_cpu_kill, |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 221 | #endif |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 222 | }; |
| 223 | |