blob: 761359be50f20468e01eebafe3a6fe761e561aa7 [file] [log] [blame]
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +01001// 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 Pieralisi9ffeb6d2019-08-09 12:03:12 +010025static DEFINE_PER_CPU_READ_MOSTLY(u32 *, psci_power_state);
26
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +010027static int psci_enter_idle_state(struct cpuidle_device *dev,
28 struct cpuidle_driver *drv, int idx)
29{
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +010030 u32 *state = __this_cpu_read(psci_power_state);
31
32 return CPU_PM_CPU_IDLE_ENTER_PARAM(psci_cpu_suspend_enter,
Sudeep Holla4386aa82019-10-24 16:18:34 +010033 idx, state[idx]);
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +010034}
35
36static 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
53static 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 Pieralisi9ffeb6d2019-08-09 12:03:12 +010059static 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 Hansson1595e4b2019-10-10 12:01:48 +020076static int __init psci_dt_cpu_init_idle(struct device_node *cpu_node,
77 unsigned int state_count, int cpu)
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +010078{
Ulf Hansson1595e4b2019-10-10 12:01:48 +020079 int i, ret = 0;
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +010080 u32 *psci_states;
81 struct device_node *state_node;
82
Ulf Hansson1595e4b2019-10-10 12:01:48 +020083 state_count++; /* Add WFI state too */
84 psci_states = kcalloc(state_count, sizeof(*psci_states), GFP_KERNEL);
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +010085 if (!psci_states)
86 return -ENOMEM;
87
Ulf Hansson1595e4b2019-10-10 12:01:48 +020088 for (i = 1; i < state_count; i++) {
Sudeep Holla4386aa82019-10-24 16:18:34 +010089 state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
90 i - 1);
Ulf Hansson1595e4b2019-10-10 12:01:48 +020091 if (!state_node)
92 break;
93
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +010094 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 Hansson1595e4b2019-10-10 12:01:48 +0200103 if (i != state_count) {
104 ret = -ENODEV;
105 goto free_mem;
106 }
107
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100108 /* Idle states parsed correctly, initialize per-cpu pointer */
109 per_cpu(psci_power_state, cpu) = psci_states;
110 return 0;
111
112free_mem:
113 kfree(psci_states);
114 return ret;
115}
116
Ulf Hansson1595e4b2019-10-10 12:01:48 +0200117static __init int psci_cpu_init_idle(unsigned int cpu, unsigned int state_count)
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100118{
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 Hansson1595e4b2019-10-10 12:01:48 +0200133 ret = psci_dt_cpu_init_idle(cpu_node, state_count, cpu);
Lorenzo Pieralisi9ffeb6d2019-08-09 12:03:12 +0100134
135 of_node_put(cpu_node);
136
137 return ret;
138}
139
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +0100140static 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 Hansson1595e4b2019-10-10 12:01:48 +0200189 ret = psci_cpu_init_idle(cpu, ret);
Lorenzo Pieralisi81d549e2019-08-09 12:03:10 +0100190 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
201out_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 */
213static 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
227out_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}
237device_initcall(psci_idle_init);