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