Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * PSCI CPU idle driver. |
| 4 | * |
| 5 | * Copyright (C) 2019 ARM Ltd. |
| 6 | * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> |
| 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) "CPUidle PSCI: " fmt |
| 10 | |
| 11 | #include <linux/cpuidle.h> |
| 12 | #include <linux/cpumask.h> |
| 13 | #include <linux/cpu_pm.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/of_device.h> |
| 18 | #include <linux/psci.h> |
| 19 | #include <linux/slab.h> |
| 20 | |
| 21 | #include <asm/cpuidle.h> |
| 22 | |
| 23 | #include "dt_idle_states.h" |
| 24 | |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 25 | static DEFINE_PER_CPU_READ_MOSTLY(u32 *, psci_power_state); |
| 26 | |
Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 27 | static int psci_enter_idle_state(struct cpuidle_device *dev, |
| 28 | struct cpuidle_driver *drv, int idx) |
| 29 | { |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 30 | u32 *state = __this_cpu_read(psci_power_state); |
| 31 | |
| 32 | return CPU_PM_CPU_IDLE_ENTER_PARAM(psci_cpu_suspend_enter, |
Sudeep Holla | 4386aa8 | 2019-10-24 16:18:34 +0100 | [diff] [blame] | 33 | idx, state[idx]); |
Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static struct cpuidle_driver psci_idle_driver __initdata = { |
| 37 | .name = "psci_idle", |
| 38 | .owner = THIS_MODULE, |
| 39 | /* |
| 40 | * PSCI idle states relies on architectural WFI to |
| 41 | * be represented as state index 0. |
| 42 | */ |
| 43 | .states[0] = { |
| 44 | .enter = psci_enter_idle_state, |
| 45 | .exit_latency = 1, |
| 46 | .target_residency = 1, |
| 47 | .power_usage = UINT_MAX, |
| 48 | .name = "WFI", |
| 49 | .desc = "ARM WFI", |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | static const struct of_device_id psci_idle_state_match[] __initconst = { |
| 54 | { .compatible = "arm,idle-state", |
| 55 | .data = psci_enter_idle_state }, |
| 56 | { }, |
| 57 | }; |
| 58 | |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 59 | static int __init psci_dt_parse_state_node(struct device_node *np, u32 *state) |
| 60 | { |
| 61 | int err = of_property_read_u32(np, "arm,psci-suspend-param", state); |
| 62 | |
| 63 | if (err) { |
| 64 | pr_warn("%pOF missing arm,psci-suspend-param property\n", np); |
| 65 | return err; |
| 66 | } |
| 67 | |
| 68 | if (!psci_power_state_is_valid(*state)) { |
| 69 | pr_warn("Invalid PSCI power state %#x\n", *state); |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame^] | 76 | static int __init psci_dt_cpu_init_idle(struct device_node *cpu_node, |
| 77 | unsigned int state_count, int cpu) |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 78 | { |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame^] | 79 | int i, ret = 0; |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 80 | u32 *psci_states; |
| 81 | struct device_node *state_node; |
| 82 | |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame^] | 83 | state_count++; /* Add WFI state too */ |
| 84 | psci_states = kcalloc(state_count, sizeof(*psci_states), GFP_KERNEL); |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 85 | if (!psci_states) |
| 86 | return -ENOMEM; |
| 87 | |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame^] | 88 | for (i = 1; i < state_count; i++) { |
Sudeep Holla | 4386aa8 | 2019-10-24 16:18:34 +0100 | [diff] [blame] | 89 | state_node = of_parse_phandle(cpu_node, "cpu-idle-states", |
| 90 | i - 1); |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame^] | 91 | if (!state_node) |
| 92 | break; |
| 93 | |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 94 | ret = psci_dt_parse_state_node(state_node, &psci_states[i]); |
| 95 | of_node_put(state_node); |
| 96 | |
| 97 | if (ret) |
| 98 | goto free_mem; |
| 99 | |
| 100 | pr_debug("psci-power-state %#x index %d\n", psci_states[i], i); |
| 101 | } |
| 102 | |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame^] | 103 | if (i != state_count) { |
| 104 | ret = -ENODEV; |
| 105 | goto free_mem; |
| 106 | } |
| 107 | |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 108 | /* Idle states parsed correctly, initialize per-cpu pointer */ |
| 109 | per_cpu(psci_power_state, cpu) = psci_states; |
| 110 | return 0; |
| 111 | |
| 112 | free_mem: |
| 113 | kfree(psci_states); |
| 114 | return ret; |
| 115 | } |
| 116 | |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame^] | 117 | static __init int psci_cpu_init_idle(unsigned int cpu, unsigned int state_count) |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 118 | { |
| 119 | struct device_node *cpu_node; |
| 120 | int ret; |
| 121 | |
| 122 | /* |
| 123 | * If the PSCI cpu_suspend function hook has not been initialized |
| 124 | * idle states must not be enabled, so bail out |
| 125 | */ |
| 126 | if (!psci_ops.cpu_suspend) |
| 127 | return -EOPNOTSUPP; |
| 128 | |
| 129 | cpu_node = of_cpu_device_node_get(cpu); |
| 130 | if (!cpu_node) |
| 131 | return -ENODEV; |
| 132 | |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame^] | 133 | ret = psci_dt_cpu_init_idle(cpu_node, state_count, cpu); |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 134 | |
| 135 | of_node_put(cpu_node); |
| 136 | |
| 137 | return ret; |
| 138 | } |
| 139 | |
Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 140 | static int __init psci_idle_init_cpu(int cpu) |
| 141 | { |
| 142 | struct cpuidle_driver *drv; |
| 143 | struct device_node *cpu_node; |
| 144 | const char *enable_method; |
| 145 | int ret = 0; |
| 146 | |
| 147 | cpu_node = of_cpu_device_node_get(cpu); |
| 148 | if (!cpu_node) |
| 149 | return -ENODEV; |
| 150 | |
| 151 | /* |
| 152 | * Check whether the enable-method for the cpu is PSCI, fail |
| 153 | * if it is not. |
| 154 | */ |
| 155 | enable_method = of_get_property(cpu_node, "enable-method", NULL); |
| 156 | if (!enable_method || (strcmp(enable_method, "psci"))) |
| 157 | ret = -ENODEV; |
| 158 | |
| 159 | of_node_put(cpu_node); |
| 160 | if (ret) |
| 161 | return ret; |
| 162 | |
| 163 | drv = kmemdup(&psci_idle_driver, sizeof(*drv), GFP_KERNEL); |
| 164 | if (!drv) |
| 165 | return -ENOMEM; |
| 166 | |
| 167 | drv->cpumask = (struct cpumask *)cpumask_of(cpu); |
| 168 | |
| 169 | /* |
| 170 | * Initialize idle states data, starting at index 1, since |
| 171 | * by default idle state 0 is the quiescent state reached |
| 172 | * by the cpu by executing the wfi instruction. |
| 173 | * |
| 174 | * If no DT idle states are detected (ret == 0) let the driver |
| 175 | * initialization fail accordingly since there is no reason to |
| 176 | * initialize the idle driver if only wfi is supported, the |
| 177 | * default archictectural back-end already executes wfi |
| 178 | * on idle entry. |
| 179 | */ |
| 180 | ret = dt_init_idle_driver(drv, psci_idle_state_match, 1); |
| 181 | if (ret <= 0) { |
| 182 | ret = ret ? : -ENODEV; |
| 183 | goto out_kfree_drv; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Initialize PSCI idle states. |
| 188 | */ |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame^] | 189 | ret = psci_cpu_init_idle(cpu, ret); |
Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 190 | if (ret) { |
| 191 | pr_err("CPU %d failed to PSCI idle\n", cpu); |
| 192 | goto out_kfree_drv; |
| 193 | } |
| 194 | |
| 195 | ret = cpuidle_register(drv, NULL); |
| 196 | if (ret) |
| 197 | goto out_kfree_drv; |
| 198 | |
| 199 | return 0; |
| 200 | |
| 201 | out_kfree_drv: |
| 202 | kfree(drv); |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * psci_idle_init - Initializes PSCI cpuidle driver |
| 208 | * |
| 209 | * Initializes PSCI cpuidle driver for all CPUs, if any CPU fails |
| 210 | * to register cpuidle driver then rollback to cancel all CPUs |
| 211 | * registration. |
| 212 | */ |
| 213 | static int __init psci_idle_init(void) |
| 214 | { |
| 215 | int cpu, ret; |
| 216 | struct cpuidle_driver *drv; |
| 217 | struct cpuidle_device *dev; |
| 218 | |
| 219 | for_each_possible_cpu(cpu) { |
| 220 | ret = psci_idle_init_cpu(cpu); |
| 221 | if (ret) |
| 222 | goto out_fail; |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | |
| 227 | out_fail: |
| 228 | while (--cpu >= 0) { |
| 229 | dev = per_cpu(cpuidle_devices, cpu); |
| 230 | drv = cpuidle_get_cpu_driver(dev); |
| 231 | cpuidle_unregister(drv); |
| 232 | kfree(drv); |
| 233 | } |
| 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | device_initcall(psci_idle_init); |