blob: 4fcae1348aac2865194911955d8e516b0dd54c23 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 */
5
6#include <granule.h>
7#include <psci.h>
8#include <realm.h>
9#include <rec.h>
AlexeiFedorov97844202023-04-27 15:17:35 +010010#include <rsi-handler.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000011#include <smc-rmi.h>
12#include <smc.h>
13#include <stdint.h>
14
AlexeiFedorov97844202023-04-27 15:17:35 +010015/*
16 * Copy @count GPRs from @rec to @rec_exit.
17 * The remaining @rec_exit.gprs[] values are zero filled.
18 */
19static void forward_args_to_host(unsigned int count, struct rec *rec,
20 struct rmi_rec_exit *rec_exit)
Soby Mathewb4c6df42022-11-09 11:13:29 +000021{
AlexeiFedorov97844202023-04-27 15:17:35 +010022 unsigned int i;
Soby Mathewb4c6df42022-11-09 11:13:29 +000023
AlexeiFedorov97844202023-04-27 15:17:35 +010024 assert(count <= 4U);
25
26 for (i = 0U; i < count; ++i) {
27 rec_exit->gprs[i] = rec->regs[i];
28 }
29
30 for (i = count; i < REC_EXIT_NR_GPRS; ++i) {
31 rec_exit->gprs[i] = 0UL;
32 }
Soby Mathewb4c6df42022-11-09 11:13:29 +000033}
34
AlexeiFedorov97844202023-04-27 15:17:35 +010035static void psci_version(struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +000036{
AlexeiFedorov97844202023-04-27 15:17:35 +010037 const unsigned long version_1_1 = (1UL << 16) | 1UL;
38
39 res->action = UPDATE_REC_RETURN_TO_REALM;
40 res->smc_res.x[0] = version_1_1;
41}
42
43static void psci_cpu_suspend(struct rec *rec, struct rmi_rec_exit *rec_exit,
44 struct rsi_result *res)
45{
46 res->action = UPDATE_REC_EXIT_TO_HOST;
Soby Mathewb4c6df42022-11-09 11:13:29 +000047
48 /*
AlexeiFedorov97844202023-04-27 15:17:35 +010049 * We treat all target power states as suspend requests,
50 * so all we need to do is forward the FID to the NS hypervisor,
51 * and we can ignore all the parameters.
Soby Mathewb4c6df42022-11-09 11:13:29 +000052 */
AlexeiFedorov97844202023-04-27 15:17:35 +010053 forward_args_to_host(1U, rec, rec_exit);
Soby Mathewb4c6df42022-11-09 11:13:29 +000054
AlexeiFedorov97844202023-04-27 15:17:35 +010055 /*
56 * The exit to the Host is just a notification; the Host does not need
57 * to complete a PSCI request before the next call to RMI_REC_ENTER.
58 * We therefore update the REC immediately with the results of the PSCI
59 * command.
60 */
61 res->smc_res.x[0] = PSCI_RETURN_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +000062}
63
AlexeiFedorov97844202023-04-27 15:17:35 +010064static void psci_cpu_off(struct rec *rec, struct rmi_rec_exit *rec_exit,
65 struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +000066{
AlexeiFedorov97844202023-04-27 15:17:35 +010067 res->action = UPDATE_REC_EXIT_TO_HOST;
Soby Mathewb4c6df42022-11-09 11:13:29 +000068
69 /*
70 * It should be fine to set this flag without holding a lock on the
71 * REC or without explicit memory barriers or ordering semantics
72 * operations, because we already ensure that a REC can only be in an
73 * executing state once at any given time, and we're in this execution
74 * context already, and we will be holding a reference count on the
75 * REC at this point, which will be dropped and re-evaluated with
76 * proper barriers before any CPU can evaluate the runnable field
77 * after this change.
78 */
79 rec->runnable = false;
80
AlexeiFedorov97844202023-04-27 15:17:35 +010081 /* Notify the Host, passing the FID only. */
82 forward_args_to_host(1U, rec, rec_exit);
83
84 /*
85 * The exit to the Host is just a notification; the Host does not need
86 * to complete a PSCI request before the next call to RMI_REC_ENTER.
87 * We therefore update the REC immediately with the results of the PSCI
88 * command.
89 */
90 res->smc_res.x[0] = PSCI_RETURN_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +000091}
92
93static void psci_reset_rec(struct rec *rec, unsigned long caller_sctlr_el1)
94{
95 /* Set execution level to EL1 (AArch64) and mask exceptions */
96 rec->pstate = SPSR_EL2_MODE_EL1h |
97 SPSR_EL2_nRW_AARCH64 |
98 SPSR_EL2_F_BIT |
99 SPSR_EL2_I_BIT |
100 SPSR_EL2_A_BIT |
101 SPSR_EL2_D_BIT;
102
103 /* Disable stage 1 MMU and caches */
104 rec->sysregs.sctlr_el1 = SCTLR_EL1_FLAGS;
105
106 /* Set the endianness of the target to that of the caller */
Arvind Ram Prakashbd36a1b2022-12-15 12:16:36 -0600107 rec->sysregs.sctlr_el1 |= caller_sctlr_el1 & SCTLR_ELx_EE_BIT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000108}
109
110static unsigned long rd_map_read_rec_count(struct granule *g_rd)
111{
112 unsigned long rec_count;
113 struct rd *rd = granule_map(g_rd, SLOT_RD);
114
115 rec_count = get_rd_rec_count_unlocked(rd);
116 buffer_unmap(rd);
117 return rec_count;
118}
119
AlexeiFedorov97844202023-04-27 15:17:35 +0100120static void psci_cpu_on(struct rec *rec, struct rmi_rec_exit *rec_exit,
121 struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000122{
AlexeiFedorov97844202023-04-27 15:17:35 +0100123 unsigned long target_cpu = rec->regs[1];
124 unsigned long entry_point_address = rec->regs[2];
Soby Mathewb4c6df42022-11-09 11:13:29 +0000125 unsigned long target_rec_idx;
126
AlexeiFedorov97844202023-04-27 15:17:35 +0100127 res->action = UPDATE_REC_RETURN_TO_REALM;
128
Soby Mathewb4c6df42022-11-09 11:13:29 +0000129 /* Check that entry_point_address is a Protected Realm Address */
130 if (!addr_in_rec_par(rec, entry_point_address)) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100131 res->smc_res.x[0] = PSCI_RETURN_INVALID_ADDRESS;
132 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000133 }
134
135 /* Get REC index from MPIDR */
136 target_rec_idx = mpidr_to_rec_idx(target_cpu);
137
138 /*
139 * Check that the target_cpu is a valid value.
140 * Note that the RMM enforces that the REC are created with
141 * consecutively increasing indexes starting from zero.
142 */
143 if (target_rec_idx >= rd_map_read_rec_count(rec->realm_info.g_rd)) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100144 res->smc_res.x[0] = PSCI_RETURN_INVALID_PARAMS;
145 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000146 }
147
148 /* Check if we're trying to turn ourselves on */
149 if (target_rec_idx == rec->rec_idx) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100150 res->smc_res.x[0] = PSCI_RETURN_ALREADY_ON;
151 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000152 }
153
AlexeiFedorov97844202023-04-27 15:17:35 +0100154 /* Record that a PSCI request is outstanding */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000155 rec->psci_info.pending = true;
156
AlexeiFedorov97844202023-04-27 15:17:35 +0100157 /*
158 * Notify the Host, passing the FID and MPIDR arguments.
159 * Leave REC registers unchanged; these will be read and updated by
160 * psci_complete_request.
161 */
162 forward_args_to_host(2U, rec, rec_exit);
163 res->action = EXIT_TO_HOST;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000164}
165
AlexeiFedorov97844202023-04-27 15:17:35 +0100166static void psci_affinity_info(struct rec *rec, struct rmi_rec_exit *rec_exit,
167 struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000168{
AlexeiFedorov97844202023-04-27 15:17:35 +0100169 unsigned long target_affinity = rec->regs[1];
170 unsigned long lowest_affinity_level = rec->regs[2];
Soby Mathewb4c6df42022-11-09 11:13:29 +0000171 unsigned long target_rec_idx;
172
AlexeiFedorov97844202023-04-27 15:17:35 +0100173 res->action = UPDATE_REC_RETURN_TO_REALM;
174
Soby Mathewb4c6df42022-11-09 11:13:29 +0000175 if (lowest_affinity_level != 0UL) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100176 res->smc_res.x[0] = PSCI_RETURN_INVALID_PARAMS;
177 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000178 }
179
180 /* Get REC index from MPIDR */
181 target_rec_idx = mpidr_to_rec_idx(target_affinity);
182
183 /*
184 * Check that the target_affinity is a valid value.
185 * Note that the RMM enforces that the REC are created with
186 * consecutively increasing indexes starting from zero.
187 */
188 if (target_rec_idx >= rd_map_read_rec_count(rec->realm_info.g_rd)) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100189 res->smc_res.x[0] = PSCI_RETURN_INVALID_PARAMS;
190 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000191 }
192
193 /* Check if the vCPU targets itself */
194 if (target_rec_idx == rec->rec_idx) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100195 res->smc_res.x[0] = PSCI_AFFINITY_INFO_ON;
196 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000197 }
198
AlexeiFedorov97844202023-04-27 15:17:35 +0100199 /* Record that a PSCI request is outstanding */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000200 rec->psci_info.pending = true;
201
AlexeiFedorov97844202023-04-27 15:17:35 +0100202 /*
203 * Notify the Host, passing the FID and MPIDR arguments.
204 * Leave REC registers unchanged; these will be read and updated
205 * by psci_complete_request.
206 */
207 forward_args_to_host(2U, rec, rec_exit);
208
209 res->action = EXIT_TO_HOST;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000210}
211
212/*
213 * Turning a system off or requesting a reboot of a realm is enforced by the
214 * RMM by preventing execution of a REC after the function has run. Reboot
215 * functionality must be provided by the host hypervisor by creating a new
216 * Realm with associated attestation, measurement etc.
217 */
218static void system_off_reboot(struct rec *rec)
219{
220 struct rd *rd;
221 struct granule *g_rd = rec->realm_info.g_rd;
222
223 /*
224 * The RECs (and, consequently, the PSCI calls) run without any
225 * RMM lock held. Therefore, we cannot cause a deadlock when we acquire
226 * the rd lock here before we set the Realm's new state.
227 */
228 granule_lock(g_rd, GRANULE_STATE_RD);
229 rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
230
231 set_rd_state(rd, REALM_STATE_SYSTEM_OFF);
232
233 buffer_unmap(rd);
234 granule_unlock(g_rd);
235
236 /* TODO: Invalidate all stage 2 entris to ensure REC exits */
237}
238
AlexeiFedorov97844202023-04-27 15:17:35 +0100239static void psci_system_off_reset(struct rec *rec,
240 struct rmi_rec_exit *rec_exit,
241 struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000242{
Soby Mathewb4c6df42022-11-09 11:13:29 +0000243 system_off_reboot(rec);
244
AlexeiFedorov97844202023-04-27 15:17:35 +0100245 /* Notify the Host, passing the FID only */
246 forward_args_to_host(1U, rec, rec_exit);
247
248 res->action = EXIT_TO_HOST;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000249}
250
AlexeiFedorov97844202023-04-27 15:17:35 +0100251static void psci_features(struct rec *rec, struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000252{
AlexeiFedorov97844202023-04-27 15:17:35 +0100253 unsigned int psci_func_id = (unsigned int)rec->regs[1];
Soby Mathewb4c6df42022-11-09 11:13:29 +0000254
255 switch (psci_func_id) {
256 case SMC32_PSCI_CPU_SUSPEND:
257 case SMC64_PSCI_CPU_SUSPEND:
258 case SMC32_PSCI_CPU_OFF:
259 case SMC32_PSCI_CPU_ON:
260 case SMC64_PSCI_CPU_ON:
261 case SMC32_PSCI_AFFINITY_INFO:
262 case SMC64_PSCI_AFFINITY_INFO:
263 case SMC32_PSCI_SYSTEM_OFF:
264 case SMC32_PSCI_SYSTEM_RESET:
265 case SMC32_PSCI_FEATURES:
266 case SMCCC_VERSION:
AlexeiFedorov97844202023-04-27 15:17:35 +0100267 res->smc_res.x[0] = PSCI_RETURN_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000268 break;
269 default:
AlexeiFedorov97844202023-04-27 15:17:35 +0100270 res->smc_res.x[0] = PSCI_RETURN_NOT_SUPPORTED;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000271 }
272
AlexeiFedorov97844202023-04-27 15:17:35 +0100273 res->action = UPDATE_REC_RETURN_TO_REALM;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000274}
275
AlexeiFedorov97844202023-04-27 15:17:35 +0100276void handle_psci(struct rec *rec,
277 struct rmi_rec_exit *rec_exit,
278 struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000279{
AlexeiFedorov97844202023-04-27 15:17:35 +0100280 unsigned int function_id = (unsigned int)rec->regs[0];
Soby Mathewb4c6df42022-11-09 11:13:29 +0000281
282 switch (function_id) {
283 case SMC32_PSCI_VERSION:
AlexeiFedorov97844202023-04-27 15:17:35 +0100284 psci_version(res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000285 break;
286 case SMC32_PSCI_CPU_SUSPEND:
287 case SMC64_PSCI_CPU_SUSPEND:
AlexeiFedorov97844202023-04-27 15:17:35 +0100288 psci_cpu_suspend(rec, rec_exit, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000289 break;
290 case SMC32_PSCI_CPU_OFF:
AlexeiFedorov97844202023-04-27 15:17:35 +0100291 psci_cpu_off(rec, rec_exit, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000292 break;
293 case SMC32_PSCI_CPU_ON:
Soby Mathewb4c6df42022-11-09 11:13:29 +0000294 case SMC64_PSCI_CPU_ON:
AlexeiFedorov97844202023-04-27 15:17:35 +0100295 psci_cpu_on(rec, rec_exit, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000296 break;
297 case SMC32_PSCI_AFFINITY_INFO:
Soby Mathewb4c6df42022-11-09 11:13:29 +0000298 case SMC64_PSCI_AFFINITY_INFO:
AlexeiFedorov97844202023-04-27 15:17:35 +0100299 psci_affinity_info(rec, rec_exit, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000300 break;
301 case SMC32_PSCI_SYSTEM_OFF:
Soby Mathewb4c6df42022-11-09 11:13:29 +0000302 case SMC32_PSCI_SYSTEM_RESET:
AlexeiFedorov97844202023-04-27 15:17:35 +0100303 psci_system_off_reset(rec, rec_exit, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000304 break;
305 case SMC32_PSCI_FEATURES:
AlexeiFedorov97844202023-04-27 15:17:35 +0100306 psci_features(rec, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000307 break;
308 default:
AlexeiFedorov97844202023-04-27 15:17:35 +0100309 res->action = UPDATE_REC_RETURN_TO_REALM;
310 res->smc_res.x[0] = PSCI_RETURN_NOT_SUPPORTED;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000311 break;
312 }
313
AlexeiFedorov97844202023-04-27 15:17:35 +0100314 if ((res->action & FLAG_EXIT_TO_HOST) != 0) {
315 rec_exit->exit_reason = RMI_EXIT_PSCI;
316 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000317}
318
319/*
320 * In the following two functions, it is only safe to access the runnable field
321 * on the target_rec once the target_rec is no longer running on another PE and
322 * all writes performed by the other PE as part of smc_rec_enter is also
323 * guaranteed to be observed here, which we know when we read a zero refcount
324 * on the target rec using acquire semantics paired with the release semantics
325 * on the reference count in smc_rec_enter. If we observe a non-zero refcount
326 * it simply means that the target_rec is running and we can return the
327 * corresponding value.
328 */
329static unsigned long complete_psci_cpu_on(struct rec *target_rec,
330 unsigned long entry_point_address,
331 unsigned long caller_sctlr_el1)
332{
333 if ((granule_refcount_read_acquire(target_rec->g_rec) != 0UL) ||
334 target_rec->runnable) {
335 return PSCI_RETURN_ALREADY_ON;
336 }
337
338 psci_reset_rec(target_rec, caller_sctlr_el1);
339 target_rec->pc = entry_point_address;
340 target_rec->runnable = true;
341 return PSCI_RETURN_SUCCESS;
342}
343
344static unsigned long complete_psci_affinity_info(struct rec *target_rec)
345{
346 if ((granule_refcount_read_acquire(target_rec->g_rec) != 0UL) ||
347 target_rec->runnable) {
348 return PSCI_AFFINITY_INFO_ON;
349 }
350
351 return PSCI_AFFINITY_INFO_OFF;
352}
353
354unsigned long psci_complete_request(struct rec *calling_rec,
355 struct rec *target_rec)
356{
357 unsigned long ret = PSCI_RETURN_NOT_SUPPORTED;
358 unsigned long mpidr = calling_rec->regs[1];
359
360 if (!calling_rec->psci_info.pending) {
361 return RMI_ERROR_INPUT;
362 }
363
364 if (calling_rec->realm_info.g_rd != target_rec->realm_info.g_rd) {
365 return RMI_ERROR_INPUT;
366 }
367
368 if (mpidr_to_rec_idx(mpidr) != target_rec->rec_idx) {
369 return RMI_ERROR_INPUT;
370 }
371
372 switch (calling_rec->regs[0]) {
373 case SMC32_PSCI_CPU_ON:
374 case SMC64_PSCI_CPU_ON:
375 ret = complete_psci_cpu_on(target_rec,
376 calling_rec->regs[2],
377 calling_rec->sysregs.sctlr_el1);
378 break;
379 case SMC32_PSCI_AFFINITY_INFO:
380 case SMC64_PSCI_AFFINITY_INFO:
381 ret = complete_psci_affinity_info(target_rec);
382 break;
383 default:
384 assert(false);
385 }
386
387 calling_rec->regs[0] = ret;
388 calling_rec->regs[1] = 0;
389 calling_rec->regs[2] = 0;
390 calling_rec->regs[3] = 0;
391 calling_rec->psci_info.pending = false;
392
393 return RMI_SUCCESS;
394}