blob: 41418372f3ae0d436dba74aef9fa8f58546b48fd [file] [log] [blame]
Achin Gupta7aea9082014-02-01 07:51:28 +00001/*
2 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
Achin Guptac429b5e2014-05-04 18:38:28 +010031#include <arch.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000032#include <arch_helpers.h>
Dan Handley97043ac2014-04-09 13:14:54 +010033#include <assert.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000034#include <bl_common.h>
Soby Mathewa43d4312014-04-07 15:28:55 +010035#include <bl31.h>
Dan Handley97043ac2014-04-09 13:14:54 +010036#include <context.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000037#include <context_mgmt.h>
Dan Handley97043ac2014-04-09 13:14:54 +010038#include <platform.h>
39#include <runtime_svc.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000040
41/*******************************************************************************
42 * Data structure which holds the pointers to non-secure and secure security
43 * state contexts for each cpu. It is aligned to the cache line boundary to
44 * allow efficient concurrent manipulation of these pointers on different cpus
45 ******************************************************************************/
46typedef struct {
47 void *ptr[2];
Dan Handleyfb037bf2014-04-10 15:37:22 +010048} __aligned (CACHE_WRITEBACK_GRANULE) context_info_t;
Achin Gupta7aea9082014-02-01 07:51:28 +000049
Dan Handleyfb037bf2014-04-10 15:37:22 +010050static context_info_t cm_context_info[PLATFORM_CORE_COUNT];
Achin Gupta7aea9082014-02-01 07:51:28 +000051
Soby Mathewa43d4312014-04-07 15:28:55 +010052/* The per_cpu_ptr_cache_t space allocation */
53static per_cpu_ptr_cache_t per_cpu_ptr_cache_space[PLATFORM_CORE_COUNT];
54
Achin Gupta7aea9082014-02-01 07:51:28 +000055/*******************************************************************************
56 * Context management library initialisation routine. This library is used by
57 * runtime services to share pointers to 'cpu_context' structures for the secure
58 * and non-secure states. Management of the structures and their associated
59 * memory is not done by the context management library e.g. the PSCI service
60 * manages the cpu context used for entry from and exit to the non-secure state.
61 * The Secure payload dispatcher service manages the context(s) corresponding to
62 * the secure state. It also uses this library to get access to the non-secure
63 * state cpu context pointers.
64 * Lastly, this library provides the api to make SP_EL3 point to the cpu context
65 * which will used for programming an entry into a lower EL. The same context
66 * will used to save state upon exception entry from that EL.
67 ******************************************************************************/
68void cm_init()
69{
70 /*
71 * The context management library has only global data to intialize, but
72 * that will be done when the BSS is zeroed out
73 */
74}
75
76/*******************************************************************************
77 * This function returns a pointer to the most recent 'cpu_context' structure
78 * that was set as the context for the specified security state. NULL is
79 * returned if no such structure has been specified.
80 ******************************************************************************/
81void *cm_get_context(uint64_t mpidr, uint32_t security_state)
82{
83 uint32_t linear_id = platform_get_core_pos(mpidr);
84
85 assert(security_state <= NON_SECURE);
86
87 return cm_context_info[linear_id].ptr[security_state];
88}
89
90/*******************************************************************************
91 * This function sets the pointer to the current 'cpu_context' structure for the
92 * specified security state.
93 ******************************************************************************/
94void cm_set_context(uint64_t mpidr, void *context, uint32_t security_state)
95{
96 uint32_t linear_id = platform_get_core_pos(mpidr);
97
98 assert(security_state <= NON_SECURE);
99
100 cm_context_info[linear_id].ptr[security_state] = context;
101}
102
103/*******************************************************************************
104 * The next four functions are used by runtime services to save and restore EL3
105 * and EL1 contexts on the 'cpu_context' structure for the specified security
106 * state.
107 ******************************************************************************/
108void cm_el3_sysregs_context_save(uint32_t security_state)
109{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100110 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000111
112 ctx = cm_get_context(read_mpidr(), security_state);
113 assert(ctx);
114
115 el3_sysregs_context_save(get_el3state_ctx(ctx));
116}
117
118void cm_el3_sysregs_context_restore(uint32_t security_state)
119{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100120 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000121
122 ctx = cm_get_context(read_mpidr(), security_state);
123 assert(ctx);
124
125 el3_sysregs_context_restore(get_el3state_ctx(ctx));
126}
127
128void cm_el1_sysregs_context_save(uint32_t security_state)
129{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100130 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000131
132 ctx = cm_get_context(read_mpidr(), security_state);
133 assert(ctx);
134
135 el1_sysregs_context_save(get_sysregs_ctx(ctx));
136}
137
138void cm_el1_sysregs_context_restore(uint32_t security_state)
139{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100140 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000141
142 ctx = cm_get_context(read_mpidr(), security_state);
143 assert(ctx);
144
145 el1_sysregs_context_restore(get_sysregs_ctx(ctx));
146}
147
148/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +0100149 * This function populates 'cpu_context' pertaining to the given security state
150 * with the entrypoint, SPSR and SCR values so that an ERET from this security
151 * state correctly restores corresponding values to drop the CPU to the next
152 * exception level
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000153 ******************************************************************************/
154void cm_set_el3_eret_context(uint32_t security_state, uint64_t entrypoint,
155 uint32_t spsr, uint32_t scr)
156{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100157 cpu_context_t *ctx;
158 el3_state_t *state;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000159
160 ctx = cm_get_context(read_mpidr(), security_state);
161 assert(ctx);
162
163 /* Populate EL3 state so that we've the right context before doing ERET */
164 state = get_el3state_ctx(ctx);
165 write_ctx_reg(state, CTX_SPSR_EL3, spsr);
166 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
167 write_ctx_reg(state, CTX_SCR_EL3, scr);
168}
169
170/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +0100171 * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
172 * given security state with the given entrypoint
Achin Gupta607084e2014-02-09 18:24:19 +0000173 ******************************************************************************/
Achin Guptac429b5e2014-05-04 18:38:28 +0100174void cm_set_elr_el3(uint32_t security_state, uint64_t entrypoint)
Achin Gupta607084e2014-02-09 18:24:19 +0000175{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100176 cpu_context_t *ctx;
177 el3_state_t *state;
Achin Gupta607084e2014-02-09 18:24:19 +0000178
179 ctx = cm_get_context(read_mpidr(), security_state);
180 assert(ctx);
181
182 /* Populate EL3 state so that ERET jumps to the correct entry */
183 state = get_el3state_ctx(ctx);
184 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
185}
186
187/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +0100188 * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
189 * pertaining to the given security state using the value and bit position
190 * specified in the parameters. It preserves all other bits.
191 ******************************************************************************/
192void cm_write_scr_el3_bit(uint32_t security_state,
193 uint32_t bit_pos,
194 uint32_t value)
195{
196 cpu_context_t *ctx;
197 el3_state_t *state;
198 uint32_t scr_el3;
199
200 ctx = cm_get_context(read_mpidr(), security_state);
201 assert(ctx);
202
203 /* Ensure that the bit position is a valid one */
204 assert((1 << bit_pos) & SCR_VALID_BIT_MASK);
205
206 /* Ensure that the 'value' is only a bit wide */
207 assert(value <= 1);
208
209 /*
210 * Get the SCR_EL3 value from the cpu context, clear the desired bit
211 * and set it to its new value.
212 */
213 state = get_el3state_ctx(ctx);
214 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
215 scr_el3 &= ~(1 << bit_pos);
216 scr_el3 |= value << bit_pos;
217 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
218}
219
220/*******************************************************************************
221 * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
222 * given security state.
223 ******************************************************************************/
224uint32_t cm_get_scr_el3(uint32_t security_state)
225{
226 cpu_context_t *ctx;
227 el3_state_t *state;
228
229 ctx = cm_get_context(read_mpidr(), security_state);
230 assert(ctx);
231
232 /* Populate EL3 state so that ERET jumps to the correct entry */
233 state = get_el3state_ctx(ctx);
234 return read_ctx_reg(state, CTX_SCR_EL3);
235}
236
237/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000238 * This function is used to program the context that's used for exception
239 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
240 * the required security state
Achin Gupta7aea9082014-02-01 07:51:28 +0000241 ******************************************************************************/
242void cm_set_next_eret_context(uint32_t security_state)
243{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100244 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000245#if DEBUG
246 uint64_t sp_mode;
247#endif
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000248
Achin Gupta7aea9082014-02-01 07:51:28 +0000249 ctx = cm_get_context(read_mpidr(), security_state);
250 assert(ctx);
251
252#if DEBUG
253 /*
254 * Check that this function is called with SP_EL0 as the stack
255 * pointer
256 */
257 __asm__ volatile("mrs %0, SPSel\n"
258 : "=r" (sp_mode));
259
260 assert(sp_mode == MODE_SP_EL0);
261#endif
262
263 __asm__ volatile("msr spsel, #1\n"
264 "mov sp, %0\n"
265 "msr spsel, #0\n"
266 : : "r" (ctx));
267}
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000268
Soby Mathewa43d4312014-04-07 15:28:55 +0100269/************************************************************************
270 * The following function is used to populate the per cpu pointer cache.
271 * The pointer will be stored in the tpidr_el3 register.
272 *************************************************************************/
273void cm_init_pcpu_ptr_cache()
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000274{
Soby Mathewa43d4312014-04-07 15:28:55 +0100275 unsigned long mpidr = read_mpidr();
276 uint32_t linear_id = platform_get_core_pos(mpidr);
277 per_cpu_ptr_cache_t *pcpu_ptr_cache;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000278
Soby Mathewa43d4312014-04-07 15:28:55 +0100279 pcpu_ptr_cache = &per_cpu_ptr_cache_space[linear_id];
280 assert(pcpu_ptr_cache);
281 pcpu_ptr_cache->crash_stack = get_crash_stack(mpidr);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000282
Soby Mathewa43d4312014-04-07 15:28:55 +0100283 cm_set_pcpu_ptr_cache(pcpu_ptr_cache);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000284}
Soby Mathewa43d4312014-04-07 15:28:55 +0100285
286
287void cm_set_pcpu_ptr_cache(const void *pcpu_ptr)
288{
289 write_tpidr_el3((unsigned long)pcpu_ptr);
290}
291
292void *cm_get_pcpu_ptr_cache(void)
293{
294 return (void *)read_tpidr_el3();
295}
296