Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Will Deacon | e790f1d | 2012-12-18 17:53:14 +0000 | [diff] [blame] | 2 | /* |
Will Deacon | e790f1d | 2012-12-18 17:53:14 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2013 ARM Limited |
| 5 | * |
| 6 | * Author: Will Deacon <will.deacon@arm.com> |
| 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) "psci: " fmt |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/of.h> |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 13 | #include <linux/smp.h> |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 14 | #include <linux/delay.h> |
Mark Rutland | bff6079 | 2015-07-31 15:46:16 +0100 | [diff] [blame] | 15 | #include <linux/psci.h> |
Laura Abbott | 2077be6 | 2017-01-10 13:35:49 -0800 | [diff] [blame] | 16 | #include <linux/mm.h> |
Mark Rutland | bff6079 | 2015-07-31 15:46:16 +0100 | [diff] [blame] | 17 | |
Ashwin Chaugule | e71246a | 2014-04-17 14:38:41 -0400 | [diff] [blame] | 18 | #include <uapi/linux/psci.h> |
Will Deacon | e790f1d | 2012-12-18 17:53:14 +0000 | [diff] [blame] | 19 | |
Mark Rutland | cd1aebf | 2013-10-24 20:30:15 +0100 | [diff] [blame] | 20 | #include <asm/cpu_ops.h> |
Will Deacon | e790f1d | 2012-12-18 17:53:14 +0000 | [diff] [blame] | 21 | #include <asm/errno.h> |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 22 | #include <asm/smp_plat.h> |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 23 | |
Lorenzo Pieralisi | 819a882 | 2015-05-13 14:12:46 +0100 | [diff] [blame] | 24 | static int __init cpu_psci_cpu_init(unsigned int cpu) |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 25 | { |
| 26 | return 0; |
| 27 | } |
| 28 | |
Mark Rutland | cd1aebf | 2013-10-24 20:30:15 +0100 | [diff] [blame] | 29 | static int __init cpu_psci_cpu_prepare(unsigned int cpu) |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 30 | { |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 31 | if (!psci_ops.cpu_on) { |
| 32 | pr_err("no cpu_on method, not booting CPU%d\n", cpu); |
| 33 | return -ENODEV; |
| 34 | } |
| 35 | |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 36 | return 0; |
| 37 | } |
| 38 | |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 39 | static int cpu_psci_cpu_boot(unsigned int cpu) |
| 40 | { |
Laura Abbott | 2077be6 | 2017-01-10 13:35:49 -0800 | [diff] [blame] | 41 | int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa_symbol(secondary_entry)); |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 42 | if (err) |
Vladimir Murzin | 288ac26 | 2014-02-28 09:57:47 +0000 | [diff] [blame] | 43 | pr_err("failed to boot CPU%d (%d)\n", cpu, err); |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 44 | |
| 45 | return err; |
| 46 | } |
| 47 | |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 48 | #ifdef CONFIG_HOTPLUG_CPU |
| 49 | static int cpu_psci_cpu_disable(unsigned int cpu) |
| 50 | { |
| 51 | /* Fail early if we don't have CPU_OFF support */ |
| 52 | if (!psci_ops.cpu_off) |
| 53 | return -EOPNOTSUPP; |
Mark Rutland | ff3010e | 2015-04-22 18:10:26 +0100 | [diff] [blame] | 54 | |
| 55 | /* Trusted OS will deny CPU_OFF */ |
| 56 | if (psci_tos_resident_on(cpu)) |
| 57 | return -EPERM; |
| 58 | |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static void cpu_psci_cpu_die(unsigned int cpu) |
| 63 | { |
| 64 | int ret; |
| 65 | /* |
| 66 | * There are no known implementations of PSCI actually using the |
| 67 | * power state field, pass a sensible default for now. |
| 68 | */ |
Mark Rutland | c8cc427 | 2015-04-30 17:59:03 +0100 | [diff] [blame] | 69 | u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN << |
| 70 | PSCI_0_2_POWER_STATE_TYPE_SHIFT; |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 71 | |
| 72 | ret = psci_ops.cpu_off(state); |
| 73 | |
Vladimir Murzin | 288ac26 | 2014-02-28 09:57:47 +0000 | [diff] [blame] | 74 | pr_crit("unable to power off CPU%u (%d)\n", cpu, ret); |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 75 | } |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 76 | |
| 77 | static int cpu_psci_cpu_kill(unsigned int cpu) |
| 78 | { |
| 79 | int err, i; |
| 80 | |
| 81 | if (!psci_ops.affinity_info) |
Mark Rutland | 6b99c68c | 2015-04-20 17:55:30 +0100 | [diff] [blame] | 82 | return 0; |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 83 | /* |
| 84 | * cpu_kill could race with cpu_die and we can |
| 85 | * potentially end up declaring this cpu undead |
| 86 | * while it is dying. So, try again a few times. |
| 87 | */ |
| 88 | |
| 89 | for (i = 0; i < 10; i++) { |
| 90 | err = psci_ops.affinity_info(cpu_logical_map(cpu), 0); |
| 91 | if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) { |
| 92 | pr_info("CPU%d killed.\n", cpu); |
Mark Rutland | 6b99c68c | 2015-04-20 17:55:30 +0100 | [diff] [blame] | 93 | return 0; |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | msleep(10); |
| 97 | pr_info("Retrying again to check for CPU kill\n"); |
| 98 | } |
| 99 | |
| 100 | pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n", |
| 101 | cpu, err); |
Mark Rutland | 6b99c68c | 2015-04-20 17:55:30 +0100 | [diff] [blame] | 102 | return -ETIMEDOUT; |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 103 | } |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 104 | #endif |
| 105 | |
Mark Rutland | cd1aebf | 2013-10-24 20:30:15 +0100 | [diff] [blame] | 106 | const struct cpu_operations cpu_psci_ops = { |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 107 | .name = "psci", |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 108 | #ifdef CONFIG_CPU_IDLE |
Lorenzo Pieralisi | 8b6f249 | 2016-02-01 18:01:30 +0100 | [diff] [blame] | 109 | .cpu_init_idle = psci_cpu_init_idle, |
| 110 | .cpu_suspend = psci_cpu_suspend_enter, |
Lorenzo Pieralisi | 18910ab | 2013-09-27 10:25:02 +0100 | [diff] [blame] | 111 | #endif |
Mark Rutland | cd1aebf | 2013-10-24 20:30:15 +0100 | [diff] [blame] | 112 | .cpu_init = cpu_psci_cpu_init, |
| 113 | .cpu_prepare = cpu_psci_cpu_prepare, |
Mark Rutland | 652af89 | 2013-10-24 20:30:16 +0100 | [diff] [blame] | 114 | .cpu_boot = cpu_psci_cpu_boot, |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 115 | #ifdef CONFIG_HOTPLUG_CPU |
| 116 | .cpu_disable = cpu_psci_cpu_disable, |
| 117 | .cpu_die = cpu_psci_cpu_die, |
Ashwin Chaugule | c814ca0 | 2014-05-07 10:18:36 -0400 | [diff] [blame] | 118 | .cpu_kill = cpu_psci_cpu_kill, |
Mark Rutland | 831ccf7 | 2013-10-24 20:30:19 +0100 | [diff] [blame] | 119 | #endif |
Mark Rutland | 00ef54b | 2013-10-24 20:30:14 +0100 | [diff] [blame] | 120 | }; |
| 121 | |