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 | |
Ulf Hansson | 9c6ceec | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 11 | #include <linux/cpuhotplug.h> |
Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 12 | #include <linux/cpuidle.h> |
| 13 | #include <linux/cpumask.h> |
| 14 | #include <linux/cpu_pm.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/of_device.h> |
| 19 | #include <linux/psci.h> |
Ulf Hansson | ce85aef | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 20 | #include <linux/pm_runtime.h> |
Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | |
| 23 | #include <asm/cpuidle.h> |
| 24 | |
Ulf Hansson | 8554951 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 25 | #include "cpuidle-psci.h" |
Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 26 | #include "dt_idle_states.h" |
| 27 | |
Ulf Hansson | 8554951 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 28 | struct psci_cpuidle_data { |
| 29 | u32 *psci_states; |
| 30 | struct device *dev; |
| 31 | }; |
| 32 | |
| 33 | static DEFINE_PER_CPU_READ_MOSTLY(struct psci_cpuidle_data, psci_cpuidle_data); |
Ulf Hansson | a0cf319 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 34 | static DEFINE_PER_CPU(u32, domain_state); |
Ulf Hansson | 9c6ceec | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 35 | static bool psci_cpuidle_use_cpuhp __initdata; |
Ulf Hansson | a0cf319 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 36 | |
Ulf Hansson | a65a397 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 37 | void psci_set_domain_state(u32 state) |
Ulf Hansson | a0cf319 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 38 | { |
| 39 | __this_cpu_write(domain_state, state); |
| 40 | } |
| 41 | |
| 42 | static inline u32 psci_get_domain_state(void) |
| 43 | { |
| 44 | return __this_cpu_read(domain_state); |
| 45 | } |
| 46 | |
| 47 | static inline int psci_enter_state(int idx, u32 state) |
| 48 | { |
| 49 | return CPU_PM_CPU_IDLE_ENTER_PARAM(psci_cpu_suspend_enter, idx, state); |
| 50 | } |
| 51 | |
| 52 | static int psci_enter_domain_idle_state(struct cpuidle_device *dev, |
| 53 | struct cpuidle_driver *drv, int idx) |
| 54 | { |
| 55 | struct psci_cpuidle_data *data = this_cpu_ptr(&psci_cpuidle_data); |
| 56 | u32 *states = data->psci_states; |
Ulf Hansson | ce85aef | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 57 | struct device *pd_dev = data->dev; |
| 58 | u32 state; |
Ulf Hansson | a0cf319 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 59 | int ret; |
| 60 | |
Ulf Hansson | ce85aef | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 61 | /* Do runtime PM to manage a hierarchical CPU toplogy. */ |
| 62 | pm_runtime_put_sync_suspend(pd_dev); |
| 63 | |
| 64 | state = psci_get_domain_state(); |
Ulf Hansson | a0cf319 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 65 | if (!state) |
| 66 | state = states[idx]; |
| 67 | |
| 68 | ret = psci_enter_state(idx, state); |
| 69 | |
Ulf Hansson | ce85aef | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 70 | pm_runtime_get_sync(pd_dev); |
| 71 | |
Ulf Hansson | a0cf319 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 72 | /* Clear the domain state to start fresh when back from idle. */ |
| 73 | psci_set_domain_state(0); |
| 74 | return ret; |
| 75 | } |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 76 | |
Ulf Hansson | 9c6ceec | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 77 | static int psci_idle_cpuhp_up(unsigned int cpu) |
| 78 | { |
| 79 | struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev); |
| 80 | |
| 81 | if (pd_dev) |
| 82 | pm_runtime_get_sync(pd_dev); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int psci_idle_cpuhp_down(unsigned int cpu) |
| 88 | { |
| 89 | struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev); |
| 90 | |
| 91 | if (pd_dev) { |
| 92 | pm_runtime_put_sync(pd_dev); |
| 93 | /* Clear domain state to start fresh at next online. */ |
| 94 | psci_set_domain_state(0); |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static void __init psci_idle_init_cpuhp(void) |
| 101 | { |
| 102 | int err; |
| 103 | |
| 104 | if (!psci_cpuidle_use_cpuhp) |
| 105 | return; |
| 106 | |
| 107 | err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING, |
| 108 | "cpuidle/psci:online", |
| 109 | psci_idle_cpuhp_up, |
| 110 | psci_idle_cpuhp_down); |
| 111 | if (err) |
| 112 | pr_warn("Failed %d while setup cpuhp state\n", err); |
| 113 | } |
| 114 | |
Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 115 | static int psci_enter_idle_state(struct cpuidle_device *dev, |
| 116 | struct cpuidle_driver *drv, int idx) |
| 117 | { |
Ulf Hansson | 8554951 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 118 | u32 *state = __this_cpu_read(psci_cpuidle_data.psci_states); |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 119 | |
Ulf Hansson | a0cf319 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 120 | return psci_enter_state(idx, state[idx]); |
Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static struct cpuidle_driver psci_idle_driver __initdata = { |
| 124 | .name = "psci_idle", |
| 125 | .owner = THIS_MODULE, |
| 126 | /* |
| 127 | * PSCI idle states relies on architectural WFI to |
| 128 | * be represented as state index 0. |
| 129 | */ |
| 130 | .states[0] = { |
| 131 | .enter = psci_enter_idle_state, |
| 132 | .exit_latency = 1, |
| 133 | .target_residency = 1, |
| 134 | .power_usage = UINT_MAX, |
| 135 | .name = "WFI", |
| 136 | .desc = "ARM WFI", |
| 137 | } |
| 138 | }; |
| 139 | |
| 140 | static const struct of_device_id psci_idle_state_match[] __initconst = { |
| 141 | { .compatible = "arm,idle-state", |
| 142 | .data = psci_enter_idle_state }, |
| 143 | { }, |
| 144 | }; |
| 145 | |
Ulf Hansson | a65a397 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 146 | int __init psci_dt_parse_state_node(struct device_node *np, u32 *state) |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 147 | { |
| 148 | int err = of_property_read_u32(np, "arm,psci-suspend-param", state); |
| 149 | |
| 150 | if (err) { |
| 151 | pr_warn("%pOF missing arm,psci-suspend-param property\n", np); |
| 152 | return err; |
| 153 | } |
| 154 | |
| 155 | if (!psci_power_state_is_valid(*state)) { |
| 156 | pr_warn("Invalid PSCI power state %#x\n", *state); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Ulf Hansson | 7fbee48 | 2020-03-10 11:40:39 +0100 | [diff] [blame^] | 163 | static int __init psci_dt_cpu_init_topology(struct cpuidle_driver *drv, |
| 164 | struct psci_cpuidle_data *data, |
| 165 | unsigned int state_count, int cpu) |
| 166 | { |
| 167 | /* Currently limit the hierarchical topology to be used in OSI mode. */ |
| 168 | if (!psci_has_osi_support()) |
| 169 | return 0; |
| 170 | |
| 171 | data->dev = psci_dt_attach_cpu(cpu); |
| 172 | if (IS_ERR_OR_NULL(data->dev)) |
| 173 | return PTR_ERR_OR_ZERO(data->dev); |
| 174 | |
| 175 | /* |
| 176 | * Using the deepest state for the CPU to trigger a potential selection |
| 177 | * of a shared state for the domain, assumes the domain states are all |
| 178 | * deeper states. |
| 179 | */ |
| 180 | drv->states[state_count - 1].enter = psci_enter_domain_idle_state; |
| 181 | psci_cpuidle_use_cpuhp = true; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Ulf Hansson | a0cf319 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 186 | static int __init psci_dt_cpu_init_idle(struct cpuidle_driver *drv, |
| 187 | struct device_node *cpu_node, |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 188 | unsigned int state_count, int cpu) |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 189 | { |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 190 | int i, ret = 0; |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 191 | u32 *psci_states; |
| 192 | struct device_node *state_node; |
Ulf Hansson | 8554951 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 193 | struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu); |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 194 | |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 195 | state_count++; /* Add WFI state too */ |
| 196 | psci_states = kcalloc(state_count, sizeof(*psci_states), GFP_KERNEL); |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 197 | if (!psci_states) |
| 198 | return -ENOMEM; |
| 199 | |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 200 | for (i = 1; i < state_count; i++) { |
Ulf Hansson | f08cfbf | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 201 | state_node = of_get_cpu_state_node(cpu_node, i - 1); |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 202 | if (!state_node) |
| 203 | break; |
| 204 | |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 205 | ret = psci_dt_parse_state_node(state_node, &psci_states[i]); |
| 206 | of_node_put(state_node); |
| 207 | |
| 208 | if (ret) |
| 209 | goto free_mem; |
| 210 | |
| 211 | pr_debug("psci-power-state %#x index %d\n", psci_states[i], i); |
| 212 | } |
| 213 | |
Ulf Hansson | 1595e4b | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 214 | if (i != state_count) { |
| 215 | ret = -ENODEV; |
| 216 | goto free_mem; |
| 217 | } |
| 218 | |
Ulf Hansson | 7fbee48 | 2020-03-10 11:40:39 +0100 | [diff] [blame^] | 219 | /* Initialize optional data, used for the hierarchical topology. */ |
| 220 | ret = psci_dt_cpu_init_topology(drv, data, state_count, cpu); |
| 221 | if (ret < 0) |
| 222 | goto free_mem; |
Ulf Hansson | 8554951 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 223 | |
| 224 | /* Idle states parsed correctly, store them in the per-cpu struct. */ |
| 225 | data->psci_states = psci_states; |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 226 | return 0; |
| 227 | |
| 228 | free_mem: |
| 229 | kfree(psci_states); |
| 230 | return ret; |
| 231 | } |
| 232 | |
Ulf Hansson | a0cf319 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 233 | static __init int psci_cpu_init_idle(struct cpuidle_driver *drv, |
| 234 | unsigned int cpu, unsigned int state_count) |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 235 | { |
| 236 | struct device_node *cpu_node; |
| 237 | int ret; |
| 238 | |
| 239 | /* |
| 240 | * If the PSCI cpu_suspend function hook has not been initialized |
| 241 | * idle states must not be enabled, so bail out |
| 242 | */ |
| 243 | if (!psci_ops.cpu_suspend) |
| 244 | return -EOPNOTSUPP; |
| 245 | |
| 246 | cpu_node = of_cpu_device_node_get(cpu); |
| 247 | if (!cpu_node) |
| 248 | return -ENODEV; |
| 249 | |
Ulf Hansson | a0cf319 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 250 | ret = psci_dt_cpu_init_idle(drv, cpu_node, state_count, cpu); |
Lorenzo Pieralisi | 9ffeb6d | 2019-08-09 12:03:12 +0100 | [diff] [blame] | 251 | |
| 252 | of_node_put(cpu_node); |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 257 | static int __init psci_idle_init_cpu(int cpu) |
| 258 | { |
| 259 | struct cpuidle_driver *drv; |
| 260 | struct device_node *cpu_node; |
| 261 | const char *enable_method; |
| 262 | int ret = 0; |
| 263 | |
| 264 | cpu_node = of_cpu_device_node_get(cpu); |
| 265 | if (!cpu_node) |
| 266 | return -ENODEV; |
| 267 | |
| 268 | /* |
| 269 | * Check whether the enable-method for the cpu is PSCI, fail |
| 270 | * if it is not. |
| 271 | */ |
| 272 | enable_method = of_get_property(cpu_node, "enable-method", NULL); |
| 273 | if (!enable_method || (strcmp(enable_method, "psci"))) |
| 274 | ret = -ENODEV; |
| 275 | |
| 276 | of_node_put(cpu_node); |
| 277 | if (ret) |
| 278 | return ret; |
| 279 | |
| 280 | drv = kmemdup(&psci_idle_driver, sizeof(*drv), GFP_KERNEL); |
| 281 | if (!drv) |
| 282 | return -ENOMEM; |
| 283 | |
| 284 | drv->cpumask = (struct cpumask *)cpumask_of(cpu); |
| 285 | |
| 286 | /* |
| 287 | * Initialize idle states data, starting at index 1, since |
| 288 | * by default idle state 0 is the quiescent state reached |
| 289 | * by the cpu by executing the wfi instruction. |
| 290 | * |
| 291 | * If no DT idle states are detected (ret == 0) let the driver |
| 292 | * initialization fail accordingly since there is no reason to |
| 293 | * initialize the idle driver if only wfi is supported, the |
| 294 | * default archictectural back-end already executes wfi |
| 295 | * on idle entry. |
| 296 | */ |
| 297 | ret = dt_init_idle_driver(drv, psci_idle_state_match, 1); |
| 298 | if (ret <= 0) { |
| 299 | ret = ret ? : -ENODEV; |
| 300 | goto out_kfree_drv; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Initialize PSCI idle states. |
| 305 | */ |
Ulf Hansson | a0cf319 | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 306 | ret = psci_cpu_init_idle(drv, cpu, ret); |
Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 307 | if (ret) { |
| 308 | pr_err("CPU %d failed to PSCI idle\n", cpu); |
| 309 | goto out_kfree_drv; |
| 310 | } |
| 311 | |
| 312 | ret = cpuidle_register(drv, NULL); |
| 313 | if (ret) |
| 314 | goto out_kfree_drv; |
| 315 | |
| 316 | return 0; |
| 317 | |
| 318 | out_kfree_drv: |
| 319 | kfree(drv); |
| 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * psci_idle_init - Initializes PSCI cpuidle driver |
| 325 | * |
| 326 | * Initializes PSCI cpuidle driver for all CPUs, if any CPU fails |
| 327 | * to register cpuidle driver then rollback to cancel all CPUs |
| 328 | * registration. |
| 329 | */ |
| 330 | static int __init psci_idle_init(void) |
| 331 | { |
| 332 | int cpu, ret; |
| 333 | struct cpuidle_driver *drv; |
| 334 | struct cpuidle_device *dev; |
| 335 | |
| 336 | for_each_possible_cpu(cpu) { |
| 337 | ret = psci_idle_init_cpu(cpu); |
| 338 | if (ret) |
| 339 | goto out_fail; |
| 340 | } |
| 341 | |
Ulf Hansson | 9c6ceec | 2019-10-10 12:01:48 +0200 | [diff] [blame] | 342 | psci_idle_init_cpuhp(); |
Lorenzo Pieralisi | 81d549e | 2019-08-09 12:03:10 +0100 | [diff] [blame] | 343 | return 0; |
| 344 | |
| 345 | out_fail: |
| 346 | while (--cpu >= 0) { |
| 347 | dev = per_cpu(cpuidle_devices, cpu); |
| 348 | drv = cpuidle_get_cpu_driver(dev); |
| 349 | cpuidle_unregister(drv); |
| 350 | kfree(drv); |
| 351 | } |
| 352 | |
| 353 | return ret; |
| 354 | } |
| 355 | device_initcall(psci_idle_init); |