blob: 52226cc55d1c0eca3fd8669e81cc40eb1dd74479 [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;
100 spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()];
101
102 INFO("SPM Core init start.\n");
103 ctx->state = SPMC_STATE_RESET;
104
105 rc = spmd_spm_core_sync_entry(ctx);
106 if (rc) {
107 ERROR("SPMC initialisation failed 0x%llx\n", rc);
108 panic();
109 }
110
111 ctx->state = SPMC_STATE_IDLE;
112 INFO("SPM Core init end.\n");
113
114 return 1;
115}
116
117/*******************************************************************************
118 * Initialize context of SPM core.
119 ******************************************************************************/
120int32_t spmd_setup(void)
121{
122 int rc;
123 void *rd_base;
124 size_t rd_size;
Achin Gupta383b7c52019-10-11 15:41:16 +0100125 uintptr_t rd_base_align;
126 uintptr_t rd_size_align;
127 uint32_t ep_attr;
128
129 spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
130 if (!spmc_ep_info) {
131 WARN("No SPM core image provided by BL2 boot loader, Booting "
132 "device without SP initialization. SMC`s destined for SPM "
133 "core will return SMC_UNK\n");
134 return 1;
135 }
136
137 /* Under no circumstances will this parameter be 0 */
138 assert (spmc_ep_info->pc != 0U);
139
140 /*
141 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
142 * be used as a manifest for the SPM core at the next lower EL/mode.
143 */
144 if (spmc_ep_info->args.arg0 == 0U || spmc_ep_info->args.arg2 == 0U) {
145 ERROR("Invalid or absent SPM core manifest \n");
146 panic();
147 }
148
149 /* Obtain whereabouts of SPM core manifest */
150 rd_base = (void *) spmc_ep_info->args.arg0;
151 rd_size = spmc_ep_info->args.arg2;
152
153 rd_base_align = page_align((uintptr_t) rd_base, DOWN);
154 rd_size_align = page_align((uintptr_t) rd_size, UP);
155
156 /* Map the manifest in the SPMD translation regime first */
157 VERBOSE("SPM core manifest base : 0x%lx \n", rd_base_align);
158 VERBOSE("SPM core manifest size : 0x%lx \n", rd_size_align);
159 rc = mmap_add_dynamic_region((unsigned long long) rd_base_align,
160 (uintptr_t) rd_base_align,
161 rd_size_align,
162 MT_RO_DATA);
163 if (rc < 0) {
164 ERROR("Error while mapping SPM core manifest (%d).\n", rc);
165 panic();
166 }
167
168 /* Load the SPM core manifest */
169 rc = plat_spm_core_manifest_load(&spmc_attrs, rd_base, rd_size);
170 if (rc < 0) {
171 WARN("No or invalid SPM core manifest image provided by BL2 "
172 "boot loader. ");
173 goto error;
174 }
175
176 /*
177 * Ensure that the SPM core version is compatible with the SPM
178 * dispatcher version
179 */
180 if ((spmc_attrs.major_version != SPCI_VERSION_MAJOR) ||
181 (spmc_attrs.minor_version > SPCI_VERSION_MINOR)) {
182 WARN("Unsupported SPCI version (%x.%x) specified in SPM core "
183 "manifest image provided by BL2 boot loader.\n",
184 spmc_attrs.major_version, spmc_attrs.minor_version);
185 goto error;
186 }
187
188 INFO("SPCI version (%x.%x).\n", spmc_attrs.major_version,
189 spmc_attrs.minor_version);
190
191 /* Validate the SPM core runtime EL */
192 if ((spmc_attrs.runtime_el != MODE_EL1) &&
193 (spmc_attrs.runtime_el != MODE_EL2)) {
194 WARN("Unsupported SPM core run time EL%x specified in "
195 "manifest image provided by BL2 boot loader.\n",
196 spmc_attrs.runtime_el);
197 goto error;
198 }
199
200 INFO("SPM core run time EL%x.\n", spmc_attrs.runtime_el);
201
202 /* Validate the SPM core execution state */
203 if ((spmc_attrs.exec_state != MODE_RW_64) &&
204 (spmc_attrs.exec_state != MODE_RW_32)) {
205 WARN("Unsupported SPM core execution state %x specified in "
206 "manifest image provided by BL2 boot loader.\n",
207 spmc_attrs.exec_state);
208 goto error;
209 }
210
211 INFO("SPM core execution state %x.\n", spmc_attrs.exec_state);
212
213 /* Ensure manifest has not requested S-EL2 in AArch32 state */
214 if ((spmc_attrs.exec_state == MODE_RW_32) &&
215 (spmc_attrs.runtime_el == MODE_EL2)) {
216 WARN("Invalid combination of SPM core execution state (%x) "
217 "and run time EL (%x).\n", spmc_attrs.exec_state,
218 spmc_attrs.runtime_el);
219 goto error;
220 }
221
222 /* Enable S-EL2 for SPM core if required */
223 if (spmc_attrs.runtime_el == MODE_EL2) {
224 /* First check if S-EL2 is supported on this system */
225 uint64_t sel2 = read_id_aa64pfr0_el1();
226
227 sel2 >>= ID_AA64PFR0_SEL2_SHIFT;
228 sel2 &= ID_AA64PFR0_SEL2_MASK;
229
230 if (!sel2) {
231 WARN("SPM core run time EL: S-EL%x is not supported "
232 "but specified in manifest image provided by "
233 "BL2 boot loader.\n", spmc_attrs.runtime_el);
234 goto error;
235 }
236 }
237
238 /* Initialise an entrypoint to set up the CPU context */
239 ep_attr = SECURE | EP_ST_ENABLE;
240 if (read_sctlr_el3() & SCTLR_EE_BIT)
241 ep_attr |= EP_EE_BIG;
242 SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
243 assert (spmc_ep_info->pc == BL32_BASE);
244
245 /*
246 * Populate SPSR for SPM core based upon validated parameters from the
247 * manifest
248 */
249 if (spmc_attrs.exec_state == MODE_RW_32) {
250 spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
251 SPSR_E_LITTLE,
252 DAIF_FIQ_BIT |
253 DAIF_IRQ_BIT |
254 DAIF_ABT_BIT);
255 } else {
256 spmc_ep_info->spsr = SPSR_64(spmc_attrs.runtime_el,
257 MODE_SP_ELX,
258 DISABLE_ALL_EXCEPTIONS);
259 }
260
261 /* Initialise SPM core context with this entry point information */
262 cm_setup_context(&(spm_core_context[plat_my_core_pos()].cpu_ctx),
263 spmc_ep_info);
264
265 INFO("SPM core setup done.\n");
266
267 /* Register init function for deferred init. */
268 bl31_register_bl32_init(&spmd_init);
269
270 return 0;
271
272error:
273 WARN("Booting device without SPM initialization. "
274 "SPCI SMCs destined for SPM core will return "
275 "ENOTSUPPORTED\n");
276
277 rc = mmap_remove_dynamic_region(rd_base_align, rd_size_align);
278 if (rc < 0) {
279 ERROR("Error while unmapping SPM core manifest (%d).\n",
280 rc);
281 panic();
282 }
283
284 return 1;
285}
286
287/*******************************************************************************
288 * This function handles all SMCs in the range reserved for SPCI. Each call is
289 * either forwarded to the other security state or handled by the SPM dispatcher
290 ******************************************************************************/
291uint64_t spmd_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
292 uint64_t x3, uint64_t x4, void *cookie, void *handle,
293 uint64_t flags)
294{
295 unsigned int in_sstate, out_sstate;
296 int32_t ret;
297 spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()];
298
299 /* Determine which security state this SMC originated from */
300 in_sstate = is_caller_non_secure(flags);
301 out_sstate = !in_sstate;
302
303 INFO("SPM: 0x%x, 0x%llx, 0x%llx, 0x%llx, 0x%llx, 0x%llx, 0x%llx, 0x%llx \n",
304 smc_fid, x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5),
305 SMC_GET_GP(handle, CTX_GPREG_X6),
306 SMC_GET_GP(handle, CTX_GPREG_X7));
307
308
309 switch (smc_fid) {
310 case SPCI_ERROR:
311 /*
312 * Check if this is the first invocation of this interface on
313 * this CPU. If so, then indicate that the SPM core initialised
314 * unsuccessfully.
315 */
316 if ((in_sstate == SECURE) && (ctx->state == SPMC_STATE_RESET))
317 spmd_spm_core_sync_exit(x2);
318
319 /* Save incoming security state */
320 cm_el1_sysregs_context_save(in_sstate);
321
322 /* Restore outgoing security state */
323 cm_el1_sysregs_context_restore(out_sstate);
324 cm_set_next_eret_context(out_sstate);
325
326 SMC_RET8(cm_get_context(out_sstate), smc_fid, x1, x2, x3, x4,
327 SMC_GET_GP(handle, CTX_GPREG_X5),
328 SMC_GET_GP(handle, CTX_GPREG_X6),
329 SMC_GET_GP(handle, CTX_GPREG_X7));
330
331 case SPCI_VERSION:
332 /*
333 * TODO: This is an optimization that the version information
334 * provided by the SPM core manifest is returned by the SPM
335 * dispatcher. It might be a better idea to simply forward this
336 * call to the SPM core and wash our hands completely.
337 */
338 ret = MAKE_SPCI_VERSION(spmc_attrs.major_version,
339 spmc_attrs.minor_version);
340 SMC_RET8(handle, SPCI_SUCCESS, SPCI_TARGET_INFO_MBZ, ret,
341 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
342 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
343
344 case SPCI_FEATURES:
345 /*
346 * This is an optional interface. Do the minimal checks and
347 * forward to SPM core which will handle it if implemented.
348 */
349
350 /*
351 * Check if w1 holds a valid SPCI fid. This is an
352 * optimization.
353 */
354 if (!is_spci_fid(x1))
355 SMC_RET8(handle, SPCI_ERROR,
356 SPCI_TARGET_INFO_MBZ, SPCI_NOT_SUPPORTED,
357 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
358 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
359
360 /* Forward SMC from Normal world to the SPM core */
361 if (in_sstate == NON_SECURE) {
362 /* Save incoming security state */
363 cm_el1_sysregs_context_save(in_sstate);
364
365 /* Restore outgoing security state */
366 cm_el1_sysregs_context_restore(out_sstate);
367 cm_set_next_eret_context(out_sstate);
368
369 SMC_RET8(cm_get_context(out_sstate), smc_fid,
370 x1, x2, x3, x4,
371 SMC_GET_GP(handle, CTX_GPREG_X5),
372 SMC_GET_GP(handle, CTX_GPREG_X6),
373 SMC_GET_GP(handle, CTX_GPREG_X7));
374 } else {
375 /*
376 * Return success if call was from secure world i.e. all
377 * SPCI functions are supported. This is essentially a
378 * nop.
379 */
380 SMC_RET8(handle, SPCI_SUCCESS, x1, x2, x3, x4,
381 SMC_GET_GP(handle, CTX_GPREG_X5),
382 SMC_GET_GP(handle, CTX_GPREG_X6),
383 SMC_GET_GP(handle, CTX_GPREG_X7));
384 }
385
386 case SPCI_RX_RELEASE:
387 case SPCI_RXTX_MAP_SMC32:
388 case SPCI_RXTX_MAP_SMC64:
389 case SPCI_RXTX_UNMAP:
390 case SPCI_MSG_RUN:
391 /* This interface must be invoked only by the Normal world */
392 if (in_sstate == SECURE)
393 SMC_RET8(handle, SPCI_ERROR,
394 SPCI_TARGET_INFO_MBZ, SPCI_NOT_SUPPORTED,
395 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
396 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
397
398 /* Fall through to forward the call to the other world */
399 case SPCI_PARTITION_INFO_GET:
400 case SPCI_MSG_SEND:
401 case SPCI_MSG_SEND_DIRECT_REQ_SMC32:
402 case SPCI_MSG_SEND_DIRECT_REQ_SMC64:
403 case SPCI_MSG_SEND_DIRECT_RESP_SMC32:
404 case SPCI_MSG_SEND_DIRECT_RESP_SMC64:
405 case SPCI_MEM_DONATE_SMC32:
406 case SPCI_MEM_DONATE_SMC64:
407 case SPCI_MEM_LEND_SMC32:
408 case SPCI_MEM_LEND_SMC64:
409 case SPCI_MEM_SHARE_SMC32:
410 case SPCI_MEM_SHARE_SMC64:
411 case SPCI_MEM_RETRIEVE_REQ_SMC32:
412 case SPCI_MEM_RETRIEVE_REQ_SMC64:
413 case SPCI_MEM_RETRIEVE_RESP:
414 case SPCI_MEM_RELINQUISH:
415 case SPCI_MEM_RECLAIM:
416 case SPCI_SUCCESS:
417 /*
418 * TODO: Assume that no requests originate from EL3 at the
419 * moment. This will change if a SP service is required in
420 * response to secure interrupts targeted to EL3. Until then
421 * simply forward the call to the Normal world.
422 */
423
424 /* Save incoming security state */
425 cm_el1_sysregs_context_save(in_sstate);
426
427 /* Restore outgoing security state */
428 cm_el1_sysregs_context_restore(out_sstate);
429 cm_set_next_eret_context(out_sstate);
430
431 SMC_RET8(cm_get_context(out_sstate), smc_fid, x1, x2, x3, x4,
432 SMC_GET_GP(handle, CTX_GPREG_X5),
433 SMC_GET_GP(handle, CTX_GPREG_X6),
434 SMC_GET_GP(handle, CTX_GPREG_X7));
435
436 case SPCI_MSG_WAIT:
437 /*
438 * Check if this is the first invocation of this interface on
439 * this CPU from the Secure world. If so, then indicate that the
440 * SPM core initialised successfully.
441 */
442 if ((in_sstate == SECURE) && (ctx->state == SPMC_STATE_RESET))
443 spmd_spm_core_sync_exit(0);
444 case SPCI_MSG_YIELD:
445 /* This interface must be invoked only by the Secure world */
446 if (in_sstate == NON_SECURE)
447 SMC_RET8(handle, SPCI_ERROR,
448 SPCI_TARGET_INFO_MBZ, SPCI_NOT_SUPPORTED,
449 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
450 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
451
452 /* Save incoming security state */
453 cm_el1_sysregs_context_save(in_sstate);
454
455 /* Restore outgoing security state */
456 cm_el1_sysregs_context_restore(out_sstate);
457 cm_set_next_eret_context(out_sstate);
458
459 SMC_RET8(cm_get_context(out_sstate), smc_fid, x1, x2, x3, x4,
460 SMC_GET_GP(handle, CTX_GPREG_X5),
461 SMC_GET_GP(handle, CTX_GPREG_X6),
462 SMC_GET_GP(handle, CTX_GPREG_X7));
463
464 default:
465 WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
466 SMC_RET8(handle, SPCI_ERROR,
467 SPCI_TARGET_INFO_MBZ, SPCI_NOT_SUPPORTED,
468 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
469 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
470 }
471}