blob: 79b5d19cd6ae86f0b1a0e8fd0a8b14ecaabb1cc0 [file] [log] [blame]
Achin Gupta9ac63c52014-01-16 12:08:03 +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
Dan Handley97043ac2014-04-09 13:14:54 +010031#include <arch.h>
Andrew Thoelke0a30cf52014-03-18 13:46:55 +000032#include <asm_macros.S>
Dan Handley97043ac2014-04-09 13:14:54 +010033#include <context.h>
Achin Gupta9ac63c52014-01-16 12:08:03 +000034
35/* -----------------------------------------------------
36 * The following function strictly follows the AArch64
37 * PCS to use x9-x17 (temporary caller-saved registers)
38 * to save essential EL3 system register context. It
39 * assumes that 'x0' is pointing to a 'el1_sys_regs'
40 * structure where the register context will be saved.
41 * -----------------------------------------------------
42 */
43 .global el3_sysregs_context_save
Andrew Thoelke0a30cf52014-03-18 13:46:55 +000044func el3_sysregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +000045
Achin Guptab51da822014-06-26 09:58:52 +010046 mrs x10, cptr_el3
47 mrs x11, cntfrq_el0
48 stp x10, x11, [x0, #CTX_CPTR_EL3]
Achin Gupta9ac63c52014-01-16 12:08:03 +000049
50 ret
51
52/* -----------------------------------------------------
53 * The following function strictly follows the AArch64
54 * PCS to use x9-x17 (temporary caller-saved registers)
55 * to restore essential EL3 system register context. It
56 * assumes that 'x0' is pointing to a 'el1_sys_regs'
57 * structure from where the register context will be
58 * restored.
59 *
60 * Note that the sequence differs from that of the save
61 * function as we want the MMU to be enabled last
62 * -----------------------------------------------------
63 */
64 .global el3_sysregs_context_restore
Andrew Thoelke0a30cf52014-03-18 13:46:55 +000065func el3_sysregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +000066
Achin Guptab51da822014-06-26 09:58:52 +010067 ldp x13, x14, [x0, #CTX_CPTR_EL3]
68 msr cptr_el3, x13
69 msr cntfrq_el0, x14
Achin Gupta9ac63c52014-01-16 12:08:03 +000070 isb
71
72 ret
73
74/* -----------------------------------------------------
75 * The following function strictly follows the AArch64
76 * PCS to use x9-x17 (temporary caller-saved registers)
77 * to save EL1 system register context. It assumes that
78 * 'x0' is pointing to a 'el1_sys_regs' structure where
79 * the register context will be saved.
80 * -----------------------------------------------------
81 */
82 .global el1_sysregs_context_save
Andrew Thoelke0a30cf52014-03-18 13:46:55 +000083func el1_sysregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +000084
85 mrs x9, spsr_el1
86 mrs x10, elr_el1
87 stp x9, x10, [x0, #CTX_SPSR_EL1]
88
89 mrs x11, spsr_abt
90 mrs x12, spsr_und
91 stp x11, x12, [x0, #CTX_SPSR_ABT]
92
93 mrs x13, spsr_irq
94 mrs x14, spsr_fiq
95 stp x13, x14, [x0, #CTX_SPSR_IRQ]
96
97 mrs x15, sctlr_el1
98 mrs x16, actlr_el1
99 stp x15, x16, [x0, #CTX_SCTLR_EL1]
100
101 mrs x17, cpacr_el1
102 mrs x9, csselr_el1
103 stp x17, x9, [x0, #CTX_CPACR_EL1]
104
105 mrs x10, sp_el1
106 mrs x11, esr_el1
107 stp x10, x11, [x0, #CTX_SP_EL1]
108
109 mrs x12, ttbr0_el1
110 mrs x13, ttbr1_el1
111 stp x12, x13, [x0, #CTX_TTBR0_EL1]
112
113 mrs x14, mair_el1
114 mrs x15, amair_el1
115 stp x14, x15, [x0, #CTX_MAIR_EL1]
116
117 mrs x16, tcr_el1
118 mrs x17, tpidr_el1
119 stp x16, x17, [x0, #CTX_TCR_EL1]
120
121 mrs x9, tpidr_el0
122 mrs x10, tpidrro_el0
123 stp x9, x10, [x0, #CTX_TPIDR_EL0]
124
125 mrs x11, dacr32_el2
126 mrs x12, ifsr32_el2
127 stp x11, x12, [x0, #CTX_DACR32_EL2]
128
129 mrs x13, par_el1
130 mrs x14, far_el1
131 stp x13, x14, [x0, #CTX_PAR_EL1]
132
133 mrs x15, afsr0_el1
134 mrs x16, afsr1_el1
135 stp x15, x16, [x0, #CTX_AFSR0_EL1]
136
137 mrs x17, contextidr_el1
138 mrs x9, vbar_el1
139 stp x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
140
Jeenu Viswambharan2da8d8b2014-05-12 15:28:47 +0100141 /* Save NS timer registers if the build has instructed so */
142#if NS_TIMER_SWITCH
Achin Gupta9ac63c52014-01-16 12:08:03 +0000143 mrs x10, cntp_ctl_el0
144 mrs x11, cntp_cval_el0
145 stp x10, x11, [x0, #CTX_CNTP_CTL_EL0]
146
147 mrs x12, cntv_ctl_el0
148 mrs x13, cntv_cval_el0
149 stp x12, x13, [x0, #CTX_CNTV_CTL_EL0]
150
151 mrs x14, cntkctl_el1
Jeenu Viswambharan2da8d8b2014-05-12 15:28:47 +0100152 str x14, [x0, #CTX_CNTKCTL_EL1]
153#endif
154
Achin Gupta9ac63c52014-01-16 12:08:03 +0000155 mrs x15, fpexc32_el2
Jeenu Viswambharan2da8d8b2014-05-12 15:28:47 +0100156 str x15, [x0, #CTX_FP_FPEXC32_EL2]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000157
158 ret
159
160/* -----------------------------------------------------
161 * The following function strictly follows the AArch64
162 * PCS to use x9-x17 (temporary caller-saved registers)
163 * to restore EL1 system register context. It assumes
164 * that 'x0' is pointing to a 'el1_sys_regs' structure
165 * from where the register context will be restored
166 * -----------------------------------------------------
167 */
168 .global el1_sysregs_context_restore
Andrew Thoelke0a30cf52014-03-18 13:46:55 +0000169func el1_sysregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000170
171 ldp x9, x10, [x0, #CTX_SPSR_EL1]
172 msr spsr_el1, x9
173 msr elr_el1, x10
174
175 ldp x11, x12, [x0, #CTX_SPSR_ABT]
176 msr spsr_abt, x11
177 msr spsr_und, x12
178
179 ldp x13, x14, [x0, #CTX_SPSR_IRQ]
180 msr spsr_irq, x13
181 msr spsr_fiq, x14
182
183 ldp x15, x16, [x0, #CTX_SCTLR_EL1]
184 msr sctlr_el1, x15
185 msr actlr_el1, x16
186
187 ldp x17, x9, [x0, #CTX_CPACR_EL1]
188 msr cpacr_el1, x17
189 msr csselr_el1, x9
190
191 ldp x10, x11, [x0, #CTX_SP_EL1]
192 msr sp_el1, x10
193 msr esr_el1, x11
194
195 ldp x12, x13, [x0, #CTX_TTBR0_EL1]
196 msr ttbr0_el1, x12
197 msr ttbr1_el1, x13
198
199 ldp x14, x15, [x0, #CTX_MAIR_EL1]
200 msr mair_el1, x14
201 msr amair_el1, x15
202
203 ldp x16, x17, [x0, #CTX_TCR_EL1]
204 msr tcr_el1, x16
205 msr tpidr_el1, x17
206
207 ldp x9, x10, [x0, #CTX_TPIDR_EL0]
208 msr tpidr_el0, x9
209 msr tpidrro_el0, x10
210
211 ldp x11, x12, [x0, #CTX_DACR32_EL2]
212 msr dacr32_el2, x11
213 msr ifsr32_el2, x12
214
215 ldp x13, x14, [x0, #CTX_PAR_EL1]
216 msr par_el1, x13
217 msr far_el1, x14
218
219 ldp x15, x16, [x0, #CTX_AFSR0_EL1]
220 msr afsr0_el1, x15
221 msr afsr1_el1, x16
222
223 ldp x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
224 msr contextidr_el1, x17
225 msr vbar_el1, x9
226
Jeenu Viswambharan2da8d8b2014-05-12 15:28:47 +0100227 /* Restore NS timer registers if the build has instructed so */
228#if NS_TIMER_SWITCH
Achin Gupta9ac63c52014-01-16 12:08:03 +0000229 ldp x10, x11, [x0, #CTX_CNTP_CTL_EL0]
230 msr cntp_ctl_el0, x10
231 msr cntp_cval_el0, x11
232
233 ldp x12, x13, [x0, #CTX_CNTV_CTL_EL0]
234 msr cntv_ctl_el0, x12
235 msr cntv_cval_el0, x13
236
Jeenu Viswambharan2da8d8b2014-05-12 15:28:47 +0100237 ldr x14, [x0, #CTX_CNTKCTL_EL1]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000238 msr cntkctl_el1, x14
Jeenu Viswambharan2da8d8b2014-05-12 15:28:47 +0100239#endif
240
241 ldr x15, [x0, #CTX_FP_FPEXC32_EL2]
Achin Gupta9ac63c52014-01-16 12:08:03 +0000242 msr fpexc32_el2, x15
243
244 /* No explict ISB required here as ERET covers it */
245
246 ret
247
248/* -----------------------------------------------------
249 * The followsing function follows the aapcs_64 strictly
250 * to use x9-x17 (temporary caller-saved registers
251 * according to AArch64 PCS) to save floating point
252 * register context. It assumes that 'x0' is pointing to
253 * a 'fp_regs' structure where the register context will
254 * be saved.
255 *
256 * Access to VFP registers will trap if CPTR_EL3.TFP is
257 * set. However currently we don't use VFP registers
258 * nor set traps in Trusted Firmware, and assume it's
259 * cleared
260 *
261 * TODO: Revisit when VFP is used in secure world
262 * -----------------------------------------------------
263 */
Juan Castillo0f21c542014-06-25 17:26:36 +0100264#if CTX_INCLUDE_FPREGS
Achin Gupta9ac63c52014-01-16 12:08:03 +0000265 .global fpregs_context_save
Andrew Thoelke0a30cf52014-03-18 13:46:55 +0000266func fpregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000267 stp q0, q1, [x0, #CTX_FP_Q0]
268 stp q2, q3, [x0, #CTX_FP_Q2]
269 stp q4, q5, [x0, #CTX_FP_Q4]
270 stp q6, q7, [x0, #CTX_FP_Q6]
271 stp q8, q9, [x0, #CTX_FP_Q8]
272 stp q10, q11, [x0, #CTX_FP_Q10]
273 stp q12, q13, [x0, #CTX_FP_Q12]
274 stp q14, q15, [x0, #CTX_FP_Q14]
275 stp q16, q17, [x0, #CTX_FP_Q16]
276 stp q18, q19, [x0, #CTX_FP_Q18]
277 stp q20, q21, [x0, #CTX_FP_Q20]
278 stp q22, q23, [x0, #CTX_FP_Q22]
279 stp q24, q25, [x0, #CTX_FP_Q24]
280 stp q26, q27, [x0, #CTX_FP_Q26]
281 stp q28, q29, [x0, #CTX_FP_Q28]
282 stp q30, q31, [x0, #CTX_FP_Q30]
283
284 mrs x9, fpsr
285 str x9, [x0, #CTX_FP_FPSR]
286
287 mrs x10, fpcr
288 str x10, [x0, #CTX_FP_FPCR]
289
290 ret
291
292/* -----------------------------------------------------
293 * The following function follows the aapcs_64 strictly
294 * to use x9-x17 (temporary caller-saved registers
295 * according to AArch64 PCS) to restore floating point
296 * register context. It assumes that 'x0' is pointing to
297 * a 'fp_regs' structure from where the register context
298 * will be restored.
299 *
300 * Access to VFP registers will trap if CPTR_EL3.TFP is
301 * set. However currently we don't use VFP registers
302 * nor set traps in Trusted Firmware, and assume it's
303 * cleared
304 *
305 * TODO: Revisit when VFP is used in secure world
306 * -----------------------------------------------------
307 */
308 .global fpregs_context_restore
Andrew Thoelke0a30cf52014-03-18 13:46:55 +0000309func fpregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000310 ldp q0, q1, [x0, #CTX_FP_Q0]
311 ldp q2, q3, [x0, #CTX_FP_Q2]
312 ldp q4, q5, [x0, #CTX_FP_Q4]
313 ldp q6, q7, [x0, #CTX_FP_Q6]
314 ldp q8, q9, [x0, #CTX_FP_Q8]
315 ldp q10, q11, [x0, #CTX_FP_Q10]
316 ldp q12, q13, [x0, #CTX_FP_Q12]
317 ldp q14, q15, [x0, #CTX_FP_Q14]
318 ldp q16, q17, [x0, #CTX_FP_Q16]
319 ldp q18, q19, [x0, #CTX_FP_Q18]
320 ldp q20, q21, [x0, #CTX_FP_Q20]
321 ldp q22, q23, [x0, #CTX_FP_Q22]
322 ldp q24, q25, [x0, #CTX_FP_Q24]
323 ldp q26, q27, [x0, #CTX_FP_Q26]
324 ldp q28, q29, [x0, #CTX_FP_Q28]
325 ldp q30, q31, [x0, #CTX_FP_Q30]
326
327 ldr x9, [x0, #CTX_FP_FPSR]
328 msr fpsr, x9
329
330 str x10, [x0, #CTX_FP_FPCR]
331 msr fpcr, x10
332
333 /*
334 * No explict ISB required here as ERET to
335 * swtich to secure EL1 or non-secure world
336 * covers it
337 */
338
339 ret
Juan Castillo0f21c542014-06-25 17:26:36 +0100340#endif /* CTX_INCLUDE_FPREGS */