blob: 2a8d58efd16d1bee1f8a932681add02029a33351 [file] [log] [blame]
Yatharth Kochara65be2f2015-10-09 18:06:13 +01001/*
Zelaleme8dadb12020-02-05 14:12:39 -06002 * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
Yatharth Kochara65be2f2015-10-09 18:06:13 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Yatharth Kochara65be2f2015-10-09 18:06:13 +01005 */
6
Yatharth Kochara65be2f2015-10-09 18:06:13 +01007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
9#include <arch_helpers.h>
Yatharth Kochara65be2f2015-10-09 18:06:13 +010010#include <context.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <common/debug.h>
12#include <lib/el3_runtime/context_mgmt.h>
13#include <plat/common/platform.h>
14
Etienne Carriereba7c3d52017-06-07 16:41:50 +020015#include "../bl1_private.h"
Yatharth Kochara65be2f2015-10-09 18:06:13 +010016
Yatharth Kochara65be2f2015-10-09 18:06:13 +010017/* Following contains the cpu context pointers. */
18static void *bl1_cpu_context_ptr[2];
19
20
21void *cm_get_context(uint32_t security_state)
22{
23 assert(sec_state_is_valid(security_state));
24 return bl1_cpu_context_ptr[security_state];
25}
26
27void cm_set_context(void *context, uint32_t security_state)
28{
29 assert(sec_state_is_valid(security_state));
30 bl1_cpu_context_ptr[security_state] = context;
31}
32
33/*******************************************************************************
34 * This function prepares the context for Secure/Normal world images.
35 * Normal world images are transitioned to EL2(if supported) else EL1.
36 ******************************************************************************/
37void bl1_prepare_next_image(unsigned int image_id)
38{
Jimmy Brissonf94399a2020-08-04 16:27:51 -050039
40 /*
41 * Following array will be used for context management.
42 * There are 2 instances, for the Secure and Non-Secure contexts.
43 */
44 static cpu_context_t bl1_cpu_context[2];
45
John Tsichritzisa533ae72019-07-01 14:27:33 +010046 unsigned int security_state, mode = MODE_EL1;
John Powella5c66362020-03-20 14:21:05 -050047 image_desc_t *desc;
Yatharth Kochara65be2f2015-10-09 18:06:13 +010048 entry_point_info_t *next_bl_ep;
49
Soby Mathewd75d2ba2016-05-17 14:01:32 +010050#if CTX_INCLUDE_AARCH32_REGS
51 /*
52 * Ensure that the build flag to save AArch32 system registers in CPU
53 * context is not set for AArch64-only platforms.
54 */
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +000055 if (el_implemented(1) == EL_IMPL_A64ONLY) {
Soby Mathewd75d2ba2016-05-17 14:01:32 +010056 ERROR("EL1 supports AArch64-only. Please set build flag "
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +000057 "CTX_INCLUDE_AARCH32_REGS = 0\n");
Soby Mathewd75d2ba2016-05-17 14:01:32 +010058 panic();
59 }
60#endif
61
Yatharth Kochara65be2f2015-10-09 18:06:13 +010062 /* Get the image descriptor. */
John Powella5c66362020-03-20 14:21:05 -050063 desc = bl1_plat_get_image_desc(image_id);
64 assert(desc != NULL);
Yatharth Kochara65be2f2015-10-09 18:06:13 +010065
66 /* Get the entry point info. */
John Powella5c66362020-03-20 14:21:05 -050067 next_bl_ep = &desc->ep_info;
Yatharth Kochara65be2f2015-10-09 18:06:13 +010068
69 /* Get the image security state. */
Yatharth Kocharf11b29a2016-02-01 11:04:46 +000070 security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);
Yatharth Kochara65be2f2015-10-09 18:06:13 +010071
72 /* Setup the Secure/Non-Secure context if not done already. */
Zelaleme8dadb12020-02-05 14:12:39 -060073 if (cm_get_context(security_state) == NULL)
Yatharth Kochara65be2f2015-10-09 18:06:13 +010074 cm_set_context(&bl1_cpu_context[security_state], security_state);
75
76 /* Prepare the SPSR for the next BL image. */
John Tsichritzisa533ae72019-07-01 14:27:33 +010077 if ((security_state != SECURE) && (el_implemented(2) != EL_IMPL_NONE)) {
78 mode = MODE_EL2;
Yatharth Kochara65be2f2015-10-09 18:06:13 +010079 }
80
Jimmy Brissoned202072020-08-04 16:18:52 -050081 next_bl_ep->spsr = (uint32_t)SPSR_64((uint64_t) mode,
82 (uint64_t)MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
John Tsichritzisa533ae72019-07-01 14:27:33 +010083
Yatharth Kochara65be2f2015-10-09 18:06:13 +010084 /* Allow platform to make change */
85 bl1_plat_set_ep_info(image_id, next_bl_ep);
86
87 /* Prepare the context for the next BL image. */
88 cm_init_my_context(next_bl_ep);
89 cm_prepare_el3_exit(security_state);
90
91 /* Indicate that image is in execution state. */
John Powella5c66362020-03-20 14:21:05 -050092 desc->state = IMAGE_STATE_EXECUTED;
Yatharth Kochara65be2f2015-10-09 18:06:13 +010093
94 print_entry_point_info(next_bl_ep);
95}