blob: 67a6e037c095edb8c33c3ff928dfbe204977cdc5 [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 Gupta27b895e2014-05-04 18:38:28 +010031#include <arch.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000032#include <arch_helpers.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010033#include <assert.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000034#include <bl_common.h>
Soby Mathew5e5c2072014-04-07 15:28:55 +010035#include <bl31.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010036#include <context.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000037#include <context_mgmt.h>
Andrew Thoelkec02dbd62014-06-02 10:00:25 +010038#include <cpu_data.h>
Achin Gupta191e86e2014-05-09 10:03:15 +010039#include <interrupt_mgmt.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010040#include <platform.h>
Dan Handleyed6ff952014-05-14 17:44:19 +010041#include <platform_def.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010042#include <runtime_svc.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000043
Achin Gupta7aea9082014-02-01 07:51:28 +000044
45/*******************************************************************************
46 * Context management library initialisation routine. This library is used by
47 * runtime services to share pointers to 'cpu_context' structures for the secure
48 * and non-secure states. Management of the structures and their associated
49 * memory is not done by the context management library e.g. the PSCI service
50 * manages the cpu context used for entry from and exit to the non-secure state.
51 * The Secure payload dispatcher service manages the context(s) corresponding to
52 * the secure state. It also uses this library to get access to the non-secure
53 * state cpu context pointers.
54 * Lastly, this library provides the api to make SP_EL3 point to the cpu context
55 * which will used for programming an entry into a lower EL. The same context
56 * will used to save state upon exception entry from that EL.
57 ******************************************************************************/
58void cm_init()
59{
60 /*
61 * The context management library has only global data to intialize, but
62 * that will be done when the BSS is zeroed out
63 */
64}
65
66/*******************************************************************************
67 * This function returns a pointer to the most recent 'cpu_context' structure
Andrew Thoelkea2f65532014-05-14 17:09:32 +010068 * for the CPU identified by MPIDR that was set as the context for the specified
69 * security state. NULL is returned if no such structure has been specified.
Achin Gupta7aea9082014-02-01 07:51:28 +000070 ******************************************************************************/
Andrew Thoelkea2f65532014-05-14 17:09:32 +010071void *cm_get_context_by_mpidr(uint64_t mpidr, uint32_t security_state)
Achin Gupta7aea9082014-02-01 07:51:28 +000072{
Andrew Thoelkea2f65532014-05-14 17:09:32 +010073 assert(security_state <= NON_SECURE);
74
Andrew Thoelkec02dbd62014-06-02 10:00:25 +010075 return get_cpu_data_by_mpidr(mpidr, cpu_context[security_state]);
Andrew Thoelkea2f65532014-05-14 17:09:32 +010076}
77
78/*******************************************************************************
Achin Gupta7aea9082014-02-01 07:51:28 +000079 * This function sets the pointer to the current 'cpu_context' structure for the
Andrew Thoelkea2f65532014-05-14 17:09:32 +010080 * specified security state for the CPU identified by MPIDR
Achin Gupta7aea9082014-02-01 07:51:28 +000081 ******************************************************************************/
Andrew Thoelkea2f65532014-05-14 17:09:32 +010082void cm_set_context_by_mpidr(uint64_t mpidr, void *context, uint32_t security_state)
Achin Gupta7aea9082014-02-01 07:51:28 +000083{
Achin Gupta7aea9082014-02-01 07:51:28 +000084 assert(security_state <= NON_SECURE);
85
Andrew Thoelkec02dbd62014-06-02 10:00:25 +010086 set_cpu_data_by_mpidr(mpidr, cpu_context[security_state], context);
Andrew Thoelkea2f65532014-05-14 17:09:32 +010087}
88
89/*******************************************************************************
Achin Gupta7aea9082014-02-01 07:51:28 +000090 * The next four functions are used by runtime services to save and restore EL3
91 * and EL1 contexts on the 'cpu_context' structure for the specified security
92 * state.
93 ******************************************************************************/
94void cm_el3_sysregs_context_save(uint32_t security_state)
95{
Dan Handleye2712bc2014-04-10 15:37:22 +010096 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +000097
Andrew Thoelkea2f65532014-05-14 17:09:32 +010098 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +000099 assert(ctx);
100
101 el3_sysregs_context_save(get_el3state_ctx(ctx));
102}
103
104void cm_el3_sysregs_context_restore(uint32_t security_state)
105{
Dan Handleye2712bc2014-04-10 15:37:22 +0100106 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000107
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100108 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +0000109 assert(ctx);
110
111 el3_sysregs_context_restore(get_el3state_ctx(ctx));
112}
113
114void cm_el1_sysregs_context_save(uint32_t security_state)
115{
Dan Handleye2712bc2014-04-10 15:37:22 +0100116 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000117
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100118 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +0000119 assert(ctx);
120
121 el1_sysregs_context_save(get_sysregs_ctx(ctx));
122}
123
124void cm_el1_sysregs_context_restore(uint32_t security_state)
125{
Dan Handleye2712bc2014-04-10 15:37:22 +0100126 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000127
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100128 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +0000129 assert(ctx);
130
131 el1_sysregs_context_restore(get_sysregs_ctx(ctx));
132}
133
134/*******************************************************************************
Achin Gupta27b895e2014-05-04 18:38:28 +0100135 * This function populates 'cpu_context' pertaining to the given security state
136 * with the entrypoint, SPSR and SCR values so that an ERET from this security
137 * state correctly restores corresponding values to drop the CPU to the next
138 * exception level
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000139 ******************************************************************************/
140void cm_set_el3_eret_context(uint32_t security_state, uint64_t entrypoint,
141 uint32_t spsr, uint32_t scr)
142{
Dan Handleye2712bc2014-04-10 15:37:22 +0100143 cpu_context_t *ctx;
144 el3_state_t *state;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000145
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100146 ctx = cm_get_context(security_state);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000147 assert(ctx);
148
Achin Gupta191e86e2014-05-09 10:03:15 +0100149 /* Program the interrupt routing model for this security state */
150 scr &= ~SCR_FIQ_BIT;
151 scr &= ~SCR_IRQ_BIT;
152 scr |= get_scr_el3_from_routing_model(security_state);
153
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000154 /* Populate EL3 state so that we've the right context before doing ERET */
155 state = get_el3state_ctx(ctx);
156 write_ctx_reg(state, CTX_SPSR_EL3, spsr);
157 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
158 write_ctx_reg(state, CTX_SCR_EL3, scr);
159}
160
161/*******************************************************************************
Achin Gupta27b895e2014-05-04 18:38:28 +0100162 * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
163 * given security state with the given entrypoint
Achin Gupta607084e2014-02-09 18:24:19 +0000164 ******************************************************************************/
Achin Gupta27b895e2014-05-04 18:38:28 +0100165void cm_set_elr_el3(uint32_t security_state, uint64_t entrypoint)
Achin Gupta607084e2014-02-09 18:24:19 +0000166{
Dan Handleye2712bc2014-04-10 15:37:22 +0100167 cpu_context_t *ctx;
168 el3_state_t *state;
Achin Gupta607084e2014-02-09 18:24:19 +0000169
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100170 ctx = cm_get_context(security_state);
Achin Gupta607084e2014-02-09 18:24:19 +0000171 assert(ctx);
172
173 /* Populate EL3 state so that ERET jumps to the correct entry */
174 state = get_el3state_ctx(ctx);
175 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
176}
177
178/*******************************************************************************
Achin Gupta27b895e2014-05-04 18:38:28 +0100179 * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
180 * pertaining to the given security state using the value and bit position
181 * specified in the parameters. It preserves all other bits.
182 ******************************************************************************/
183void cm_write_scr_el3_bit(uint32_t security_state,
184 uint32_t bit_pos,
185 uint32_t value)
186{
187 cpu_context_t *ctx;
188 el3_state_t *state;
189 uint32_t scr_el3;
190
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100191 ctx = cm_get_context(security_state);
Achin Gupta27b895e2014-05-04 18:38:28 +0100192 assert(ctx);
193
194 /* Ensure that the bit position is a valid one */
195 assert((1 << bit_pos) & SCR_VALID_BIT_MASK);
196
197 /* Ensure that the 'value' is only a bit wide */
198 assert(value <= 1);
199
200 /*
201 * Get the SCR_EL3 value from the cpu context, clear the desired bit
202 * and set it to its new value.
203 */
204 state = get_el3state_ctx(ctx);
205 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
206 scr_el3 &= ~(1 << bit_pos);
207 scr_el3 |= value << bit_pos;
208 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
209}
210
211/*******************************************************************************
212 * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
213 * given security state.
214 ******************************************************************************/
215uint32_t cm_get_scr_el3(uint32_t security_state)
216{
217 cpu_context_t *ctx;
218 el3_state_t *state;
219
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100220 ctx = cm_get_context(security_state);
Achin Gupta27b895e2014-05-04 18:38:28 +0100221 assert(ctx);
222
223 /* Populate EL3 state so that ERET jumps to the correct entry */
224 state = get_el3state_ctx(ctx);
225 return read_ctx_reg(state, CTX_SCR_EL3);
226}
227
228/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000229 * This function is used to program the context that's used for exception
230 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
231 * the required security state
Achin Gupta7aea9082014-02-01 07:51:28 +0000232 ******************************************************************************/
233void cm_set_next_eret_context(uint32_t security_state)
234{
Dan Handleye2712bc2014-04-10 15:37:22 +0100235 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000236#if DEBUG
237 uint64_t sp_mode;
238#endif
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000239
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100240 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +0000241 assert(ctx);
242
243#if DEBUG
244 /*
245 * Check that this function is called with SP_EL0 as the stack
246 * pointer
247 */
248 __asm__ volatile("mrs %0, SPSel\n"
249 : "=r" (sp_mode));
250
251 assert(sp_mode == MODE_SP_EL0);
252#endif
253
254 __asm__ volatile("msr spsel, #1\n"
255 "mov sp, %0\n"
256 "msr spsel, #0\n"
257 : : "r" (ctx));
258}