1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park * Copyright (c) 2016-2020, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park *
4*54fd6939SJiyong Park * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park */
6*54fd6939SJiyong Park
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park
9*54fd6939SJiyong Park #include <arch_helpers.h>
10*54fd6939SJiyong Park #include <context.h>
11*54fd6939SJiyong Park #include <common/debug.h>
12*54fd6939SJiyong Park #include <lib/el3_runtime/context_mgmt.h>
13*54fd6939SJiyong Park #include <plat/common/platform.h>
14*54fd6939SJiyong Park #include <smccc_helpers.h>
15*54fd6939SJiyong Park
16*54fd6939SJiyong Park #include "../bl1_private.h"
17*54fd6939SJiyong Park
18*54fd6939SJiyong Park /*
19*54fd6939SJiyong Park * Following arrays will be used for context management.
20*54fd6939SJiyong Park * There are 2 instances, for the Secure and Non-Secure contexts.
21*54fd6939SJiyong Park */
22*54fd6939SJiyong Park static cpu_context_t bl1_cpu_context[2];
23*54fd6939SJiyong Park static smc_ctx_t bl1_smc_context[2];
24*54fd6939SJiyong Park
25*54fd6939SJiyong Park /* Following contains the next cpu context pointer. */
26*54fd6939SJiyong Park static void *bl1_next_cpu_context_ptr;
27*54fd6939SJiyong Park
28*54fd6939SJiyong Park /* Following contains the next smc context pointer. */
29*54fd6939SJiyong Park static void *bl1_next_smc_context_ptr;
30*54fd6939SJiyong Park
31*54fd6939SJiyong Park /* Following functions are used for SMC context handling */
smc_get_ctx(unsigned int security_state)32*54fd6939SJiyong Park void *smc_get_ctx(unsigned int security_state)
33*54fd6939SJiyong Park {
34*54fd6939SJiyong Park assert(sec_state_is_valid(security_state));
35*54fd6939SJiyong Park return &bl1_smc_context[security_state];
36*54fd6939SJiyong Park }
37*54fd6939SJiyong Park
smc_set_next_ctx(unsigned int security_state)38*54fd6939SJiyong Park void smc_set_next_ctx(unsigned int security_state)
39*54fd6939SJiyong Park {
40*54fd6939SJiyong Park assert(sec_state_is_valid(security_state));
41*54fd6939SJiyong Park bl1_next_smc_context_ptr = &bl1_smc_context[security_state];
42*54fd6939SJiyong Park }
43*54fd6939SJiyong Park
smc_get_next_ctx(void)44*54fd6939SJiyong Park void *smc_get_next_ctx(void)
45*54fd6939SJiyong Park {
46*54fd6939SJiyong Park return bl1_next_smc_context_ptr;
47*54fd6939SJiyong Park }
48*54fd6939SJiyong Park
49*54fd6939SJiyong Park /* Following functions are used for CPU context handling */
cm_get_context(uint32_t security_state)50*54fd6939SJiyong Park void *cm_get_context(uint32_t security_state)
51*54fd6939SJiyong Park {
52*54fd6939SJiyong Park assert(sec_state_is_valid(security_state));
53*54fd6939SJiyong Park return &bl1_cpu_context[security_state];
54*54fd6939SJiyong Park }
55*54fd6939SJiyong Park
cm_set_next_context(void * context)56*54fd6939SJiyong Park void cm_set_next_context(void *context)
57*54fd6939SJiyong Park {
58*54fd6939SJiyong Park assert(context != NULL);
59*54fd6939SJiyong Park bl1_next_cpu_context_ptr = context;
60*54fd6939SJiyong Park }
61*54fd6939SJiyong Park
cm_get_next_context(void)62*54fd6939SJiyong Park void *cm_get_next_context(void)
63*54fd6939SJiyong Park {
64*54fd6939SJiyong Park return bl1_next_cpu_context_ptr;
65*54fd6939SJiyong Park }
66*54fd6939SJiyong Park
67*54fd6939SJiyong Park /*******************************************************************************
68*54fd6939SJiyong Park * Following function copies GP regs r0-r4, lr and spsr,
69*54fd6939SJiyong Park * from the CPU context to the SMC context structures.
70*54fd6939SJiyong Park ******************************************************************************/
copy_cpu_ctx_to_smc_ctx(const regs_t * cpu_reg_ctx,smc_ctx_t * next_smc_ctx)71*54fd6939SJiyong Park static void copy_cpu_ctx_to_smc_ctx(const regs_t *cpu_reg_ctx,
72*54fd6939SJiyong Park smc_ctx_t *next_smc_ctx)
73*54fd6939SJiyong Park {
74*54fd6939SJiyong Park next_smc_ctx->r0 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R0);
75*54fd6939SJiyong Park next_smc_ctx->r1 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R1);
76*54fd6939SJiyong Park next_smc_ctx->r2 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R2);
77*54fd6939SJiyong Park next_smc_ctx->r3 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R3);
78*54fd6939SJiyong Park next_smc_ctx->lr_mon = read_ctx_reg(cpu_reg_ctx, CTX_LR);
79*54fd6939SJiyong Park next_smc_ctx->spsr_mon = read_ctx_reg(cpu_reg_ctx, CTX_SPSR);
80*54fd6939SJiyong Park next_smc_ctx->scr = read_ctx_reg(cpu_reg_ctx, CTX_SCR);
81*54fd6939SJiyong Park }
82*54fd6939SJiyong Park
83*54fd6939SJiyong Park /*******************************************************************************
84*54fd6939SJiyong Park * Following function flushes the SMC & CPU context pointer and its data.
85*54fd6939SJiyong Park ******************************************************************************/
flush_smc_and_cpu_ctx(void)86*54fd6939SJiyong Park static void flush_smc_and_cpu_ctx(void)
87*54fd6939SJiyong Park {
88*54fd6939SJiyong Park flush_dcache_range((uintptr_t)&bl1_next_smc_context_ptr,
89*54fd6939SJiyong Park sizeof(bl1_next_smc_context_ptr));
90*54fd6939SJiyong Park flush_dcache_range((uintptr_t)bl1_next_smc_context_ptr,
91*54fd6939SJiyong Park sizeof(smc_ctx_t));
92*54fd6939SJiyong Park
93*54fd6939SJiyong Park flush_dcache_range((uintptr_t)&bl1_next_cpu_context_ptr,
94*54fd6939SJiyong Park sizeof(bl1_next_cpu_context_ptr));
95*54fd6939SJiyong Park flush_dcache_range((uintptr_t)bl1_next_cpu_context_ptr,
96*54fd6939SJiyong Park sizeof(cpu_context_t));
97*54fd6939SJiyong Park }
98*54fd6939SJiyong Park
99*54fd6939SJiyong Park /*******************************************************************************
100*54fd6939SJiyong Park * This function prepares the context for Secure/Normal world images.
101*54fd6939SJiyong Park * Normal world images are transitioned to HYP(if supported) else SVC.
102*54fd6939SJiyong Park ******************************************************************************/
bl1_prepare_next_image(unsigned int image_id)103*54fd6939SJiyong Park void bl1_prepare_next_image(unsigned int image_id)
104*54fd6939SJiyong Park {
105*54fd6939SJiyong Park unsigned int security_state, mode = MODE32_svc;
106*54fd6939SJiyong Park image_desc_t *desc;
107*54fd6939SJiyong Park entry_point_info_t *next_bl_ep;
108*54fd6939SJiyong Park
109*54fd6939SJiyong Park /* Get the image descriptor. */
110*54fd6939SJiyong Park desc = bl1_plat_get_image_desc(image_id);
111*54fd6939SJiyong Park assert(desc != NULL);
112*54fd6939SJiyong Park
113*54fd6939SJiyong Park /* Get the entry point info. */
114*54fd6939SJiyong Park next_bl_ep = &desc->ep_info;
115*54fd6939SJiyong Park
116*54fd6939SJiyong Park /* Get the image security state. */
117*54fd6939SJiyong Park security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);
118*54fd6939SJiyong Park
119*54fd6939SJiyong Park /* Prepare the SPSR for the next BL image. */
120*54fd6939SJiyong Park if ((security_state != SECURE) && (GET_VIRT_EXT(read_id_pfr1()) != 0U)) {
121*54fd6939SJiyong Park mode = MODE32_hyp;
122*54fd6939SJiyong Park }
123*54fd6939SJiyong Park
124*54fd6939SJiyong Park next_bl_ep->spsr = SPSR_MODE32(mode, SPSR_T_ARM,
125*54fd6939SJiyong Park SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
126*54fd6939SJiyong Park
127*54fd6939SJiyong Park /* Allow platform to make change */
128*54fd6939SJiyong Park bl1_plat_set_ep_info(image_id, next_bl_ep);
129*54fd6939SJiyong Park
130*54fd6939SJiyong Park /* Prepare the cpu context for the next BL image. */
131*54fd6939SJiyong Park cm_init_my_context(next_bl_ep);
132*54fd6939SJiyong Park cm_prepare_el3_exit(security_state);
133*54fd6939SJiyong Park cm_set_next_context(cm_get_context(security_state));
134*54fd6939SJiyong Park
135*54fd6939SJiyong Park /* Prepare the smc context for the next BL image. */
136*54fd6939SJiyong Park smc_set_next_ctx(security_state);
137*54fd6939SJiyong Park copy_cpu_ctx_to_smc_ctx(get_regs_ctx(cm_get_next_context()),
138*54fd6939SJiyong Park smc_get_next_ctx());
139*54fd6939SJiyong Park
140*54fd6939SJiyong Park /*
141*54fd6939SJiyong Park * If the next image is non-secure, then we need to program the banked
142*54fd6939SJiyong Park * non secure sctlr. This is not required when the next image is secure
143*54fd6939SJiyong Park * because in AArch32, we expect the secure world to have the same
144*54fd6939SJiyong Park * SCTLR settings.
145*54fd6939SJiyong Park */
146*54fd6939SJiyong Park if (security_state == NON_SECURE) {
147*54fd6939SJiyong Park cpu_context_t *ctx = cm_get_context(security_state);
148*54fd6939SJiyong Park u_register_t ns_sctlr;
149*54fd6939SJiyong Park
150*54fd6939SJiyong Park /* Temporarily set the NS bit to access NS SCTLR */
151*54fd6939SJiyong Park write_scr(read_scr() | SCR_NS_BIT);
152*54fd6939SJiyong Park isb();
153*54fd6939SJiyong Park
154*54fd6939SJiyong Park ns_sctlr = read_ctx_reg(get_regs_ctx(ctx), CTX_NS_SCTLR);
155*54fd6939SJiyong Park write_sctlr(ns_sctlr);
156*54fd6939SJiyong Park isb();
157*54fd6939SJiyong Park
158*54fd6939SJiyong Park write_scr(read_scr() & ~SCR_NS_BIT);
159*54fd6939SJiyong Park isb();
160*54fd6939SJiyong Park }
161*54fd6939SJiyong Park
162*54fd6939SJiyong Park /*
163*54fd6939SJiyong Park * Flush the SMC & CPU context and the (next)pointers,
164*54fd6939SJiyong Park * to access them after caches are disabled.
165*54fd6939SJiyong Park */
166*54fd6939SJiyong Park flush_smc_and_cpu_ctx();
167*54fd6939SJiyong Park
168*54fd6939SJiyong Park /* Indicate that image is in execution state. */
169*54fd6939SJiyong Park desc->state = IMAGE_STATE_EXECUTED;
170*54fd6939SJiyong Park
171*54fd6939SJiyong Park print_entry_point_info(next_bl_ep);
172*54fd6939SJiyong Park }
173