blob: 2e968039b9e3a6a63671be73cd4e8fde2020391a [file] [log] [blame]
Achin Gupta383b7c52019-10-11 15:41:16 +01001/*
2 * Copyright (c) 2019, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8#include <errno.h>
9#include <string.h>
10
11#include <arch_helpers.h>
12#include <bl31/bl31.h>
13#include <common/debug.h>
14#include <common/runtime_svc.h>
15#include <lib/el3_runtime/context_mgmt.h>
16#include <lib/smccc.h>
17#include <lib/spinlock.h>
18#include <lib/utils.h>
19#include <lib/xlat_tables/xlat_tables_v2.h>
20#include <platform_def.h>
21#include <plat/common/common_def.h>
22#include <plat/common/platform.h>
23#include <services/spci_beta0.h>
24#include <smccc_helpers.h>
25#include <services/spmd_svc.h>
26#include "spmd_private.h"
27
28/*******************************************************************************
29 * SPM Core context information.
30 ******************************************************************************/
31spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
32
33/*******************************************************************************
34 * SPM Core attribute information read from its manifest.
35 ******************************************************************************/
36spmc_manifest_sect_attribute_t spmc_attrs;
37
38/*******************************************************************************
Achin Guptad302e802019-10-28 08:52:45 +000039 * SPM Core entry point information. Discovered on the primary core and reused
40 * on secondary cores.
41 ******************************************************************************/
42entry_point_info_t *spmc_ep_info;
43
44/*******************************************************************************
Achin Gupta383b7c52019-10-11 15:41:16 +010045 * This function takes an SP context pointer and performs a synchronous entry
46 * into it.
47 ******************************************************************************/
48uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
49{
50 uint64_t rc;
51
52 assert(spmc_ctx != NULL);
53
54 cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
55
56 /* Restore the context assigned above */
57 cm_el1_sysregs_context_restore(SECURE);
58 cm_set_next_eret_context(SECURE);
59
60 /* Invalidate TLBs at EL1. */
61 tlbivmalle1();
62 dsbish();
63
64 /* Enter Secure Partition */
65 rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
66
67 /* Save secure state */
68 cm_el1_sysregs_context_save(SECURE);
69
70 return rc;
71}
72
73/*******************************************************************************
74 * This function returns to the place where spm_sp_synchronous_entry() was
75 * called originally.
76 ******************************************************************************/
77__dead2 void spmd_spm_core_sync_exit(uint64_t rc)
78{
79 spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()];
80
81 /* Get context of the SP in use by this CPU. */
82 assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
83
84 /*
85 * The SPMD must have initiated the original request through a
86 * synchronous entry into SPMC. Jump back to the original C runtime
87 * context with the value of rc in x0;
88 */
89 spmd_spm_core_exit(ctx->c_rt_ctx, rc);
90
91 panic();
92}
93
94/*******************************************************************************
95 * Jump to the SPM core for the first time.
96 ******************************************************************************/
97static int32_t spmd_init(void)
98{
99 uint64_t rc = 0;
Achin Guptaf533b602019-10-28 09:03:13 +0000100 uint32_t cnt = 0;
Achin Gupta383b7c52019-10-11 15:41:16 +0100101 spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()];
102
103 INFO("SPM Core init start.\n");
Achin Guptaf533b602019-10-28 09:03:13 +0000104 ctx->state = AFF_STATE_ON_PENDING;
105
106 /* Set the state of SPMC contexts on other cpus to OFF */
107 for (cnt = 0; cnt < PLATFORM_CORE_COUNT; cnt++) {
108 if (spm_core_context[cnt].state == AFF_STATE_ON_PENDING)
109 continue;
110 spm_core_context[cnt].state = AFF_STATE_OFF;
111 }
Achin Gupta383b7c52019-10-11 15:41:16 +0100112
113 rc = spmd_spm_core_sync_entry(ctx);
114 if (rc) {
115 ERROR("SPMC initialisation failed 0x%llx\n", rc);
116 panic();
117 }
118
Achin Guptaf533b602019-10-28 09:03:13 +0000119 ctx->state = AFF_STATE_ON;
120
Achin Gupta383b7c52019-10-11 15:41:16 +0100121 INFO("SPM Core init end.\n");
122
123 return 1;
124}
125
126/*******************************************************************************
127 * Initialize context of SPM core.
128 ******************************************************************************/
129int32_t spmd_setup(void)
130{
131 int rc;
132 void *rd_base;
133 size_t rd_size;
Achin Gupta383b7c52019-10-11 15:41:16 +0100134 uintptr_t rd_base_align;
135 uintptr_t rd_size_align;
136 uint32_t ep_attr;
137
138 spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
139 if (!spmc_ep_info) {
140 WARN("No SPM core image provided by BL2 boot loader, Booting "
141 "device without SP initialization. SMC`s destined for SPM "
142 "core will return SMC_UNK\n");
143 return 1;
144 }
145
146 /* Under no circumstances will this parameter be 0 */
147 assert (spmc_ep_info->pc != 0U);
148
149 /*
150 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
151 * be used as a manifest for the SPM core at the next lower EL/mode.
152 */
153 if (spmc_ep_info->args.arg0 == 0U || spmc_ep_info->args.arg2 == 0U) {
154 ERROR("Invalid or absent SPM core manifest \n");
155 panic();
156 }
157
158 /* Obtain whereabouts of SPM core manifest */
159 rd_base = (void *) spmc_ep_info->args.arg0;
160 rd_size = spmc_ep_info->args.arg2;
161
162 rd_base_align = page_align((uintptr_t) rd_base, DOWN);
163 rd_size_align = page_align((uintptr_t) rd_size, UP);
164
165 /* Map the manifest in the SPMD translation regime first */
166 VERBOSE("SPM core manifest base : 0x%lx \n", rd_base_align);
167 VERBOSE("SPM core manifest size : 0x%lx \n", rd_size_align);
168 rc = mmap_add_dynamic_region((unsigned long long) rd_base_align,
169 (uintptr_t) rd_base_align,
170 rd_size_align,
171 MT_RO_DATA);
172 if (rc < 0) {
173 ERROR("Error while mapping SPM core manifest (%d).\n", rc);
174 panic();
175 }
176
177 /* Load the SPM core manifest */
178 rc = plat_spm_core_manifest_load(&spmc_attrs, rd_base, rd_size);
179 if (rc < 0) {
180 WARN("No or invalid SPM core manifest image provided by BL2 "
181 "boot loader. ");
182 goto error;
183 }
184
185 /*
186 * Ensure that the SPM core version is compatible with the SPM
187 * dispatcher version
188 */
189 if ((spmc_attrs.major_version != SPCI_VERSION_MAJOR) ||
190 (spmc_attrs.minor_version > SPCI_VERSION_MINOR)) {
191 WARN("Unsupported SPCI version (%x.%x) specified in SPM core "
192 "manifest image provided by BL2 boot loader.\n",
193 spmc_attrs.major_version, spmc_attrs.minor_version);
194 goto error;
195 }
196
197 INFO("SPCI version (%x.%x).\n", spmc_attrs.major_version,
198 spmc_attrs.minor_version);
199
200 /* Validate the SPM core runtime EL */
201 if ((spmc_attrs.runtime_el != MODE_EL1) &&
202 (spmc_attrs.runtime_el != MODE_EL2)) {
203 WARN("Unsupported SPM core run time EL%x specified in "
204 "manifest image provided by BL2 boot loader.\n",
205 spmc_attrs.runtime_el);
206 goto error;
207 }
208
209 INFO("SPM core run time EL%x.\n", spmc_attrs.runtime_el);
210
211 /* Validate the SPM core execution state */
212 if ((spmc_attrs.exec_state != MODE_RW_64) &&
213 (spmc_attrs.exec_state != MODE_RW_32)) {
214 WARN("Unsupported SPM core execution state %x specified in "
215 "manifest image provided by BL2 boot loader.\n",
216 spmc_attrs.exec_state);
217 goto error;
218 }
219
220 INFO("SPM core execution state %x.\n", spmc_attrs.exec_state);
221
222 /* Ensure manifest has not requested S-EL2 in AArch32 state */
223 if ((spmc_attrs.exec_state == MODE_RW_32) &&
224 (spmc_attrs.runtime_el == MODE_EL2)) {
225 WARN("Invalid combination of SPM core execution state (%x) "
226 "and run time EL (%x).\n", spmc_attrs.exec_state,
227 spmc_attrs.runtime_el);
228 goto error;
229 }
230
231 /* Enable S-EL2 for SPM core if required */
232 if (spmc_attrs.runtime_el == MODE_EL2) {
233 /* First check if S-EL2 is supported on this system */
234 uint64_t sel2 = read_id_aa64pfr0_el1();
235
236 sel2 >>= ID_AA64PFR0_SEL2_SHIFT;
237 sel2 &= ID_AA64PFR0_SEL2_MASK;
238
239 if (!sel2) {
240 WARN("SPM core run time EL: S-EL%x is not supported "
241 "but specified in manifest image provided by "
242 "BL2 boot loader.\n", spmc_attrs.runtime_el);
243 goto error;
244 }
245 }
246
247 /* Initialise an entrypoint to set up the CPU context */
248 ep_attr = SECURE | EP_ST_ENABLE;
249 if (read_sctlr_el3() & SCTLR_EE_BIT)
250 ep_attr |= EP_EE_BIG;
251 SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
252 assert (spmc_ep_info->pc == BL32_BASE);
253
254 /*
255 * Populate SPSR for SPM core based upon validated parameters from the
256 * manifest
257 */
258 if (spmc_attrs.exec_state == MODE_RW_32) {
259 spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
260 SPSR_E_LITTLE,
261 DAIF_FIQ_BIT |
262 DAIF_IRQ_BIT |
263 DAIF_ABT_BIT);
264 } else {
265 spmc_ep_info->spsr = SPSR_64(spmc_attrs.runtime_el,
266 MODE_SP_ELX,
267 DISABLE_ALL_EXCEPTIONS);
268 }
269
270 /* Initialise SPM core context with this entry point information */
271 cm_setup_context(&(spm_core_context[plat_my_core_pos()].cpu_ctx),
272 spmc_ep_info);
273
Achin Guptaf533b602019-10-28 09:03:13 +0000274 /* Reuse PSCI affinity states to mark this SPMC context as off */
275 spm_core_context[plat_my_core_pos()].state = AFF_STATE_OFF;
276
Achin Gupta383b7c52019-10-11 15:41:16 +0100277 INFO("SPM core setup done.\n");
278
279 /* Register init function for deferred init. */
280 bl31_register_bl32_init(&spmd_init);
281
282 return 0;
283
284error:
285 WARN("Booting device without SPM initialization. "
286 "SPCI SMCs destined for SPM core will return "
287 "ENOTSUPPORTED\n");
288
289 rc = mmap_remove_dynamic_region(rd_base_align, rd_size_align);
290 if (rc < 0) {
291 ERROR("Error while unmapping SPM core manifest (%d).\n",
292 rc);
293 panic();
294 }
295
296 return 1;
297}
298
299/*******************************************************************************
300 * This function handles all SMCs in the range reserved for SPCI. Each call is
301 * either forwarded to the other security state or handled by the SPM dispatcher
302 ******************************************************************************/
303uint64_t spmd_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
304 uint64_t x3, uint64_t x4, void *cookie, void *handle,
305 uint64_t flags)
306{
307 unsigned int in_sstate, out_sstate;
308 int32_t ret;
309 spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()];
310
311 /* Determine which security state this SMC originated from */
312 in_sstate = is_caller_non_secure(flags);
313 out_sstate = !in_sstate;
314
Achin Gupta383b7c52019-10-11 15:41:16 +0100315 switch (smc_fid) {
316 case SPCI_ERROR:
317 /*
318 * Check if this is the first invocation of this interface on
319 * this CPU. If so, then indicate that the SPM core initialised
320 * unsuccessfully.
321 */
Achin Guptaf533b602019-10-28 09:03:13 +0000322 if ((in_sstate == SECURE) &&
323 (ctx->state == AFF_STATE_ON_PENDING))
Achin Gupta383b7c52019-10-11 15:41:16 +0100324 spmd_spm_core_sync_exit(x2);
325
326 /* Save incoming security state */
327 cm_el1_sysregs_context_save(in_sstate);
328
329 /* Restore outgoing security state */
330 cm_el1_sysregs_context_restore(out_sstate);
331 cm_set_next_eret_context(out_sstate);
332
333 SMC_RET8(cm_get_context(out_sstate), smc_fid, x1, x2, x3, x4,
334 SMC_GET_GP(handle, CTX_GPREG_X5),
335 SMC_GET_GP(handle, CTX_GPREG_X6),
336 SMC_GET_GP(handle, CTX_GPREG_X7));
337
338 case SPCI_VERSION:
339 /*
340 * TODO: This is an optimization that the version information
341 * provided by the SPM core manifest is returned by the SPM
342 * dispatcher. It might be a better idea to simply forward this
343 * call to the SPM core and wash our hands completely.
344 */
345 ret = MAKE_SPCI_VERSION(spmc_attrs.major_version,
346 spmc_attrs.minor_version);
347 SMC_RET8(handle, SPCI_SUCCESS, SPCI_TARGET_INFO_MBZ, ret,
348 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
349 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
350
351 case SPCI_FEATURES:
352 /*
353 * This is an optional interface. Do the minimal checks and
354 * forward to SPM core which will handle it if implemented.
355 */
356
357 /*
358 * Check if w1 holds a valid SPCI fid. This is an
359 * optimization.
360 */
361 if (!is_spci_fid(x1))
362 SMC_RET8(handle, SPCI_ERROR,
363 SPCI_TARGET_INFO_MBZ, SPCI_NOT_SUPPORTED,
364 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
365 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
366
367 /* Forward SMC from Normal world to the SPM core */
368 if (in_sstate == NON_SECURE) {
369 /* Save incoming security state */
370 cm_el1_sysregs_context_save(in_sstate);
371
372 /* Restore outgoing security state */
373 cm_el1_sysregs_context_restore(out_sstate);
374 cm_set_next_eret_context(out_sstate);
375
376 SMC_RET8(cm_get_context(out_sstate), smc_fid,
377 x1, x2, x3, x4,
378 SMC_GET_GP(handle, CTX_GPREG_X5),
379 SMC_GET_GP(handle, CTX_GPREG_X6),
380 SMC_GET_GP(handle, CTX_GPREG_X7));
381 } else {
382 /*
383 * Return success if call was from secure world i.e. all
384 * SPCI functions are supported. This is essentially a
385 * nop.
386 */
387 SMC_RET8(handle, SPCI_SUCCESS, x1, x2, x3, x4,
388 SMC_GET_GP(handle, CTX_GPREG_X5),
389 SMC_GET_GP(handle, CTX_GPREG_X6),
390 SMC_GET_GP(handle, CTX_GPREG_X7));
391 }
392
393 case SPCI_RX_RELEASE:
394 case SPCI_RXTX_MAP_SMC32:
395 case SPCI_RXTX_MAP_SMC64:
396 case SPCI_RXTX_UNMAP:
397 case SPCI_MSG_RUN:
398 /* This interface must be invoked only by the Normal world */
399 if (in_sstate == SECURE)
400 SMC_RET8(handle, SPCI_ERROR,
401 SPCI_TARGET_INFO_MBZ, SPCI_NOT_SUPPORTED,
402 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
403 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
404
405 /* Fall through to forward the call to the other world */
406 case SPCI_PARTITION_INFO_GET:
407 case SPCI_MSG_SEND:
408 case SPCI_MSG_SEND_DIRECT_REQ_SMC32:
409 case SPCI_MSG_SEND_DIRECT_REQ_SMC64:
410 case SPCI_MSG_SEND_DIRECT_RESP_SMC32:
411 case SPCI_MSG_SEND_DIRECT_RESP_SMC64:
412 case SPCI_MEM_DONATE_SMC32:
413 case SPCI_MEM_DONATE_SMC64:
414 case SPCI_MEM_LEND_SMC32:
415 case SPCI_MEM_LEND_SMC64:
416 case SPCI_MEM_SHARE_SMC32:
417 case SPCI_MEM_SHARE_SMC64:
418 case SPCI_MEM_RETRIEVE_REQ_SMC32:
419 case SPCI_MEM_RETRIEVE_REQ_SMC64:
420 case SPCI_MEM_RETRIEVE_RESP:
421 case SPCI_MEM_RELINQUISH:
422 case SPCI_MEM_RECLAIM:
423 case SPCI_SUCCESS:
424 /*
425 * TODO: Assume that no requests originate from EL3 at the
426 * moment. This will change if a SP service is required in
427 * response to secure interrupts targeted to EL3. Until then
428 * simply forward the call to the Normal world.
429 */
430
431 /* Save incoming security state */
432 cm_el1_sysregs_context_save(in_sstate);
433
434 /* Restore outgoing security state */
435 cm_el1_sysregs_context_restore(out_sstate);
436 cm_set_next_eret_context(out_sstate);
437
438 SMC_RET8(cm_get_context(out_sstate), smc_fid, x1, x2, x3, x4,
439 SMC_GET_GP(handle, CTX_GPREG_X5),
440 SMC_GET_GP(handle, CTX_GPREG_X6),
441 SMC_GET_GP(handle, CTX_GPREG_X7));
442
443 case SPCI_MSG_WAIT:
444 /*
445 * Check if this is the first invocation of this interface on
446 * this CPU from the Secure world. If so, then indicate that the
447 * SPM core initialised successfully.
448 */
Achin Guptaf533b602019-10-28 09:03:13 +0000449 if ((in_sstate == SECURE) &&
450 (ctx->state == AFF_STATE_ON_PENDING))
Achin Gupta383b7c52019-10-11 15:41:16 +0100451 spmd_spm_core_sync_exit(0);
452 case SPCI_MSG_YIELD:
453 /* This interface must be invoked only by the Secure world */
454 if (in_sstate == NON_SECURE)
455 SMC_RET8(handle, SPCI_ERROR,
456 SPCI_TARGET_INFO_MBZ, SPCI_NOT_SUPPORTED,
457 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
458 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
459
460 /* Save incoming security state */
461 cm_el1_sysregs_context_save(in_sstate);
462
463 /* Restore outgoing security state */
464 cm_el1_sysregs_context_restore(out_sstate);
465 cm_set_next_eret_context(out_sstate);
466
467 SMC_RET8(cm_get_context(out_sstate), smc_fid, x1, x2, x3, x4,
468 SMC_GET_GP(handle, CTX_GPREG_X5),
469 SMC_GET_GP(handle, CTX_GPREG_X6),
470 SMC_GET_GP(handle, CTX_GPREG_X7));
471
472 default:
473 WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
474 SMC_RET8(handle, SPCI_ERROR,
475 SPCI_TARGET_INFO_MBZ, SPCI_NOT_SUPPORTED,
476 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
477 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
478 }
479}