blob: 59be748c2cde2f9896a02a6434567b88d821604d [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>
Achin Guptae1333f72014-05-09 10:03:15 +010038#include <interrupt_mgmt.h>
Dan Handley97043ac2014-04-09 13:14:54 +010039#include <platform.h>
Dan Handley5f0cdb02014-05-14 17:44:19 +010040#include <platform_def.h>
Dan Handley97043ac2014-04-09 13:14:54 +010041#include <runtime_svc.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000042
43/*******************************************************************************
44 * Data structure which holds the pointers to non-secure and secure security
45 * state contexts for each cpu. It is aligned to the cache line boundary to
46 * allow efficient concurrent manipulation of these pointers on different cpus
47 ******************************************************************************/
48typedef struct {
49 void *ptr[2];
Dan Handleyfb037bf2014-04-10 15:37:22 +010050} __aligned (CACHE_WRITEBACK_GRANULE) context_info_t;
Achin Gupta7aea9082014-02-01 07:51:28 +000051
Dan Handleyfb037bf2014-04-10 15:37:22 +010052static context_info_t cm_context_info[PLATFORM_CORE_COUNT];
Achin Gupta7aea9082014-02-01 07:51:28 +000053
54/*******************************************************************************
55 * Context management library initialisation routine. This library is used by
56 * runtime services to share pointers to 'cpu_context' structures for the secure
57 * and non-secure states. Management of the structures and their associated
58 * memory is not done by the context management library e.g. the PSCI service
59 * manages the cpu context used for entry from and exit to the non-secure state.
60 * The Secure payload dispatcher service manages the context(s) corresponding to
61 * the secure state. It also uses this library to get access to the non-secure
62 * state cpu context pointers.
63 * Lastly, this library provides the api to make SP_EL3 point to the cpu context
64 * which will used for programming an entry into a lower EL. The same context
65 * will used to save state upon exception entry from that EL.
66 ******************************************************************************/
67void cm_init()
68{
69 /*
70 * The context management library has only global data to intialize, but
71 * that will be done when the BSS is zeroed out
72 */
73}
74
75/*******************************************************************************
76 * This function returns a pointer to the most recent 'cpu_context' structure
Andrew Thoelke08ab89d2014-05-14 17:09:32 +010077 * for the CPU identified by MPIDR that was set as the context for the specified
78 * security state. NULL is returned if no such structure has been specified.
Achin Gupta7aea9082014-02-01 07:51:28 +000079 ******************************************************************************/
Andrew Thoelke08ab89d2014-05-14 17:09:32 +010080void *cm_get_context_by_mpidr(uint64_t mpidr, uint32_t security_state)
Achin Gupta7aea9082014-02-01 07:51:28 +000081{
82 uint32_t linear_id = platform_get_core_pos(mpidr);
83
84 assert(security_state <= NON_SECURE);
85
86 return cm_context_info[linear_id].ptr[security_state];
87}
88
89/*******************************************************************************
Andrew Thoelke08ab89d2014-05-14 17:09:32 +010090 * This function returns a pointer to the most recent 'cpu_context' structure
91 * for the calling CPU that was set as the context for the specified security
92 * state. NULL is returned if no such structure has been specified.
Achin Gupta7aea9082014-02-01 07:51:28 +000093 ******************************************************************************/
Andrew Thoelke08ab89d2014-05-14 17:09:32 +010094void *cm_get_context(uint32_t security_state)
95{
96 uint32_t linear_id = platform_get_core_pos(read_mpidr());
97
98 assert(security_state <= NON_SECURE);
99
100 return cm_context_info[linear_id].ptr[security_state];
101}
102
103/*******************************************************************************
104 * This function sets the pointer to the current 'cpu_context' structure for the
105 * specified security state for the CPU identified by MPIDR
106 ******************************************************************************/
107void cm_set_context_by_mpidr(uint64_t mpidr, void *context, uint32_t security_state)
Achin Gupta7aea9082014-02-01 07:51:28 +0000108{
109 uint32_t linear_id = platform_get_core_pos(mpidr);
110
111 assert(security_state <= NON_SECURE);
112
113 cm_context_info[linear_id].ptr[security_state] = context;
114}
115
116/*******************************************************************************
Andrew Thoelke08ab89d2014-05-14 17:09:32 +0100117 * This function sets the pointer to the current 'cpu_context' structure for the
118 * specified security state for the calling CPU
119 ******************************************************************************/
120void cm_set_context(void *context, uint32_t security_state)
121{
122 cm_set_context_by_mpidr(read_mpidr(), context, security_state);
123}
124
125/*******************************************************************************
Achin Gupta7aea9082014-02-01 07:51:28 +0000126 * The next four functions are used by runtime services to save and restore EL3
127 * and EL1 contexts on the 'cpu_context' structure for the specified security
128 * state.
129 ******************************************************************************/
130void cm_el3_sysregs_context_save(uint32_t security_state)
131{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100132 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000133
Andrew Thoelke08ab89d2014-05-14 17:09:32 +0100134 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +0000135 assert(ctx);
136
137 el3_sysregs_context_save(get_el3state_ctx(ctx));
138}
139
140void cm_el3_sysregs_context_restore(uint32_t security_state)
141{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100142 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000143
Andrew Thoelke08ab89d2014-05-14 17:09:32 +0100144 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +0000145 assert(ctx);
146
147 el3_sysregs_context_restore(get_el3state_ctx(ctx));
148}
149
150void cm_el1_sysregs_context_save(uint32_t security_state)
151{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100152 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000153
Andrew Thoelke08ab89d2014-05-14 17:09:32 +0100154 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +0000155 assert(ctx);
156
157 el1_sysregs_context_save(get_sysregs_ctx(ctx));
158}
159
160void cm_el1_sysregs_context_restore(uint32_t security_state)
161{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100162 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000163
Andrew Thoelke08ab89d2014-05-14 17:09:32 +0100164 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +0000165 assert(ctx);
166
167 el1_sysregs_context_restore(get_sysregs_ctx(ctx));
168}
169
170/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +0100171 * This function populates 'cpu_context' pertaining to the given security state
172 * with the entrypoint, SPSR and SCR values so that an ERET from this security
173 * state correctly restores corresponding values to drop the CPU to the next
174 * exception level
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000175 ******************************************************************************/
176void cm_set_el3_eret_context(uint32_t security_state, uint64_t entrypoint,
177 uint32_t spsr, uint32_t scr)
178{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100179 cpu_context_t *ctx;
180 el3_state_t *state;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000181
Andrew Thoelke08ab89d2014-05-14 17:09:32 +0100182 ctx = cm_get_context(security_state);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000183 assert(ctx);
184
Achin Guptae1333f72014-05-09 10:03:15 +0100185 /* Program the interrupt routing model for this security state */
186 scr &= ~SCR_FIQ_BIT;
187 scr &= ~SCR_IRQ_BIT;
188 scr |= get_scr_el3_from_routing_model(security_state);
189
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000190 /* Populate EL3 state so that we've the right context before doing ERET */
191 state = get_el3state_ctx(ctx);
192 write_ctx_reg(state, CTX_SPSR_EL3, spsr);
193 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
194 write_ctx_reg(state, CTX_SCR_EL3, scr);
195}
196
197/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +0100198 * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
199 * given security state with the given entrypoint
Achin Gupta607084e2014-02-09 18:24:19 +0000200 ******************************************************************************/
Achin Guptac429b5e2014-05-04 18:38:28 +0100201void cm_set_elr_el3(uint32_t security_state, uint64_t entrypoint)
Achin Gupta607084e2014-02-09 18:24:19 +0000202{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100203 cpu_context_t *ctx;
204 el3_state_t *state;
Achin Gupta607084e2014-02-09 18:24:19 +0000205
Andrew Thoelke08ab89d2014-05-14 17:09:32 +0100206 ctx = cm_get_context(security_state);
Achin Gupta607084e2014-02-09 18:24:19 +0000207 assert(ctx);
208
209 /* Populate EL3 state so that ERET jumps to the correct entry */
210 state = get_el3state_ctx(ctx);
211 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
212}
213
214/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +0100215 * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
216 * pertaining to the given security state using the value and bit position
217 * specified in the parameters. It preserves all other bits.
218 ******************************************************************************/
219void cm_write_scr_el3_bit(uint32_t security_state,
220 uint32_t bit_pos,
221 uint32_t value)
222{
223 cpu_context_t *ctx;
224 el3_state_t *state;
225 uint32_t scr_el3;
226
Andrew Thoelke08ab89d2014-05-14 17:09:32 +0100227 ctx = cm_get_context(security_state);
Achin Guptac429b5e2014-05-04 18:38:28 +0100228 assert(ctx);
229
230 /* Ensure that the bit position is a valid one */
231 assert((1 << bit_pos) & SCR_VALID_BIT_MASK);
232
233 /* Ensure that the 'value' is only a bit wide */
234 assert(value <= 1);
235
236 /*
237 * Get the SCR_EL3 value from the cpu context, clear the desired bit
238 * and set it to its new value.
239 */
240 state = get_el3state_ctx(ctx);
241 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
242 scr_el3 &= ~(1 << bit_pos);
243 scr_el3 |= value << bit_pos;
244 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
245}
246
247/*******************************************************************************
248 * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
249 * given security state.
250 ******************************************************************************/
251uint32_t cm_get_scr_el3(uint32_t security_state)
252{
253 cpu_context_t *ctx;
254 el3_state_t *state;
255
Andrew Thoelke08ab89d2014-05-14 17:09:32 +0100256 ctx = cm_get_context(security_state);
Achin Guptac429b5e2014-05-04 18:38:28 +0100257 assert(ctx);
258
259 /* Populate EL3 state so that ERET jumps to the correct entry */
260 state = get_el3state_ctx(ctx);
261 return read_ctx_reg(state, CTX_SCR_EL3);
262}
263
264/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000265 * This function is used to program the context that's used for exception
266 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
267 * the required security state
Achin Gupta7aea9082014-02-01 07:51:28 +0000268 ******************************************************************************/
269void cm_set_next_eret_context(uint32_t security_state)
270{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100271 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000272#if DEBUG
273 uint64_t sp_mode;
274#endif
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000275
Andrew Thoelke08ab89d2014-05-14 17:09:32 +0100276 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +0000277 assert(ctx);
278
279#if DEBUG
280 /*
281 * Check that this function is called with SP_EL0 as the stack
282 * pointer
283 */
284 __asm__ volatile("mrs %0, SPSel\n"
285 : "=r" (sp_mode));
286
287 assert(sp_mode == MODE_SP_EL0);
288#endif
289
290 __asm__ volatile("msr spsel, #1\n"
291 "mov sp, %0\n"
292 "msr spsel, #0\n"
293 : : "r" (ctx));
294}