xref: /rk3399_ARM-atf/bl1/aarch32/bl1_context_mgmt.c (revision 82cb2c1ad9897473743f08437d0a3995bed561b9)
1 /*
2  * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <arch_helpers.h>
8 #include <assert.h>
9 #include <context.h>
10 #include <context_mgmt.h>
11 #include <debug.h>
12 #include <platform.h>
13 #include <smcc_helpers.h>
14 
15 /*
16  * Following arrays will be used for context management.
17  * There are 2 instances, for the Secure and Non-Secure contexts.
18  */
19 static cpu_context_t bl1_cpu_context[2];
20 static smc_ctx_t bl1_smc_context[2];
21 
22 /* Following contains the next cpu context pointer. */
23 static void *bl1_next_cpu_context_ptr;
24 
25 /* Following contains the next smc context pointer. */
26 static void *bl1_next_smc_context_ptr;
27 
28 /* Following functions are used for SMC context handling */
29 void *smc_get_ctx(int security_state)
30 {
31 	assert(sec_state_is_valid(security_state));
32 	return &bl1_smc_context[security_state];
33 }
34 
35 void smc_set_next_ctx(int security_state)
36 {
37 	assert(sec_state_is_valid(security_state));
38 	bl1_next_smc_context_ptr = &bl1_smc_context[security_state];
39 }
40 
41 void *smc_get_next_ctx(void)
42 {
43 	return bl1_next_smc_context_ptr;
44 }
45 
46 /* Following functions are used for CPU context handling */
47 void *cm_get_context(uint32_t security_state)
48 {
49 	assert(sec_state_is_valid(security_state));
50 	return &bl1_cpu_context[security_state];
51 }
52 
53 void cm_set_next_context(void *cpu_context)
54 {
55 	assert(cpu_context);
56 	bl1_next_cpu_context_ptr = cpu_context;
57 }
58 
59 void *cm_get_next_context(void)
60 {
61 	return bl1_next_cpu_context_ptr;
62 }
63 
64 /*******************************************************************************
65  * Following function copies GP regs r0-r4, lr and spsr,
66  * from the CPU context to the SMC context structures.
67  ******************************************************************************/
68 static void copy_cpu_ctx_to_smc_ctx(const regs_t *cpu_reg_ctx,
69 		smc_ctx_t *next_smc_ctx)
70 {
71 	next_smc_ctx->r0 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R0);
72 	next_smc_ctx->r1 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R1);
73 	next_smc_ctx->r2 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R2);
74 	next_smc_ctx->r3 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R3);
75 	next_smc_ctx->lr_mon = read_ctx_reg(cpu_reg_ctx, CTX_LR);
76 	next_smc_ctx->spsr_mon = read_ctx_reg(cpu_reg_ctx, CTX_SPSR);
77 }
78 
79 /*******************************************************************************
80  * Following function flushes the SMC & CPU context pointer and its data.
81  ******************************************************************************/
82 static void flush_smc_and_cpu_ctx(void)
83 {
84 	flush_dcache_range((uintptr_t)&bl1_next_smc_context_ptr,
85 		sizeof(bl1_next_smc_context_ptr));
86 	flush_dcache_range((uintptr_t)bl1_next_smc_context_ptr,
87 		sizeof(smc_ctx_t));
88 
89 	flush_dcache_range((uintptr_t)&bl1_next_cpu_context_ptr,
90 		sizeof(bl1_next_cpu_context_ptr));
91 	flush_dcache_range((uintptr_t)bl1_next_cpu_context_ptr,
92 		sizeof(cpu_context_t));
93 }
94 
95 /*******************************************************************************
96  * This function prepares the context for Secure/Normal world images.
97  * Normal world images are transitioned to HYP(if supported) else SVC.
98  ******************************************************************************/
99 void bl1_prepare_next_image(unsigned int image_id)
100 {
101 	unsigned int security_state;
102 	image_desc_t *image_desc;
103 	entry_point_info_t *next_bl_ep;
104 
105 	/* Get the image descriptor. */
106 	image_desc = bl1_plat_get_image_desc(image_id);
107 	assert(image_desc);
108 
109 	/* Get the entry point info. */
110 	next_bl_ep = &image_desc->ep_info;
111 
112 	/* Get the image security state. */
113 	security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);
114 
115 	/* Prepare the SPSR for the next BL image. */
116 	if (security_state == SECURE) {
117 		next_bl_ep->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
118 			SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
119 	} else {
120 		/* Use HYP mode if supported else use SVC. */
121 		if (GET_VIRT_EXT(read_id_pfr1())) {
122 			next_bl_ep->spsr = SPSR_MODE32(MODE32_hyp, SPSR_T_ARM,
123 				SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
124 		} else {
125 			next_bl_ep->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
126 				SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
127 		}
128 	}
129 
130 	/* Allow platform to make change */
131 	bl1_plat_set_ep_info(image_id, next_bl_ep);
132 
133 	/* Prepare the cpu context for the next BL image. */
134 	cm_init_my_context(next_bl_ep);
135 	cm_prepare_el3_exit(security_state);
136 	cm_set_next_context(cm_get_context(security_state));
137 
138 	/* Prepare the smc context for the next BL image. */
139 	smc_set_next_ctx(security_state);
140 	copy_cpu_ctx_to_smc_ctx(get_regs_ctx(cm_get_next_context()),
141 		smc_get_next_ctx());
142 
143 	/*
144 	 * Flush the SMC & CPU context and the (next)pointers,
145 	 * to access them after caches are disabled.
146 	 */
147 	flush_smc_and_cpu_ctx();
148 
149 	/* Indicate that image is in execution state. */
150 	image_desc->state = IMAGE_STATE_EXECUTED;
151 
152 	print_entry_point_info(next_bl_ep);
153 }
154