blob: 2b2e7bf8435f8b0bd057ed77f8d12805ff6c9a91 [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
31#include <context.h>
Andrew Thoelke0a30cf52014-03-18 13:46:55 +000032#include <asm_macros.S>
Achin Gupta9ac63c52014-01-16 12:08:03 +000033
34/* -----------------------------------------------------
35 * The following function strictly follows the AArch64
36 * PCS to use x9-x17 (temporary caller-saved registers)
37 * to save essential EL3 system register context. It
38 * assumes that 'x0' is pointing to a 'el1_sys_regs'
39 * structure where the register context will be saved.
40 * -----------------------------------------------------
41 */
42 .global el3_sysregs_context_save
Andrew Thoelke0a30cf52014-03-18 13:46:55 +000043func el3_sysregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +000044
45 mrs x9, scr_el3
46 mrs x10, sctlr_el3
47 stp x9, x10, [x0, #CTX_SCR_EL3]
48
49 mrs x11, cptr_el3
50 stp x11, xzr, [x0, #CTX_CPTR_EL3]
51
52 mrs x13, cntfrq_el0
53 mrs x14, mair_el3
54 stp x13, x14, [x0, #CTX_CNTFRQ_EL0]
55
56 mrs x15, tcr_el3
57 mrs x16, ttbr0_el3
58 stp x15, x16, [x0, #CTX_TCR_EL3]
59
60 mrs x17, daif
61 and x17, x17, #(DAIF_ABT_BIT | DAIF_DBG_BIT)
62 stp x17, xzr, [x0, #CTX_DAIF_EL3]
63
64 ret
65
66/* -----------------------------------------------------
67 * The following function strictly follows the AArch64
68 * PCS to use x9-x17 (temporary caller-saved registers)
69 * to restore essential EL3 system register context. It
70 * assumes that 'x0' is pointing to a 'el1_sys_regs'
71 * structure from where the register context will be
72 * restored.
73 *
74 * Note that the sequence differs from that of the save
75 * function as we want the MMU to be enabled last
76 * -----------------------------------------------------
77 */
78 .global el3_sysregs_context_restore
Andrew Thoelke0a30cf52014-03-18 13:46:55 +000079func el3_sysregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +000080
81 ldp x11, xzr, [x0, #CTX_CPTR_EL3]
82 msr cptr_el3, x11
83
84 ldp x13, x14, [x0, #CTX_CNTFRQ_EL0]
85 msr cntfrq_el0, x13
86 msr mair_el3, x14
87
88 ldp x15, x16, [x0, #CTX_TCR_EL3]
89 msr tcr_el3, x15
90 msr ttbr0_el3, x16
91
92 ldp x17, xzr, [x0, #CTX_DAIF_EL3]
93 mrs x11, daif
94 orr x17, x17, x11
95 msr daif, x17
96
97 /* Make sure all the above changes are observed */
98 isb
99
100 ldp x9, x10, [x0, #CTX_SCR_EL3]
101 msr scr_el3, x9
102 msr sctlr_el3, x10
103 isb
104
105 ret
106
107/* -----------------------------------------------------
108 * The following function strictly follows the AArch64
109 * PCS to use x9-x17 (temporary caller-saved registers)
110 * to save EL1 system register context. It assumes that
111 * 'x0' is pointing to a 'el1_sys_regs' structure where
112 * the register context will be saved.
113 * -----------------------------------------------------
114 */
115 .global el1_sysregs_context_save
Andrew Thoelke0a30cf52014-03-18 13:46:55 +0000116func el1_sysregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000117
118 mrs x9, spsr_el1
119 mrs x10, elr_el1
120 stp x9, x10, [x0, #CTX_SPSR_EL1]
121
122 mrs x11, spsr_abt
123 mrs x12, spsr_und
124 stp x11, x12, [x0, #CTX_SPSR_ABT]
125
126 mrs x13, spsr_irq
127 mrs x14, spsr_fiq
128 stp x13, x14, [x0, #CTX_SPSR_IRQ]
129
130 mrs x15, sctlr_el1
131 mrs x16, actlr_el1
132 stp x15, x16, [x0, #CTX_SCTLR_EL1]
133
134 mrs x17, cpacr_el1
135 mrs x9, csselr_el1
136 stp x17, x9, [x0, #CTX_CPACR_EL1]
137
138 mrs x10, sp_el1
139 mrs x11, esr_el1
140 stp x10, x11, [x0, #CTX_SP_EL1]
141
142 mrs x12, ttbr0_el1
143 mrs x13, ttbr1_el1
144 stp x12, x13, [x0, #CTX_TTBR0_EL1]
145
146 mrs x14, mair_el1
147 mrs x15, amair_el1
148 stp x14, x15, [x0, #CTX_MAIR_EL1]
149
150 mrs x16, tcr_el1
151 mrs x17, tpidr_el1
152 stp x16, x17, [x0, #CTX_TCR_EL1]
153
154 mrs x9, tpidr_el0
155 mrs x10, tpidrro_el0
156 stp x9, x10, [x0, #CTX_TPIDR_EL0]
157
158 mrs x11, dacr32_el2
159 mrs x12, ifsr32_el2
160 stp x11, x12, [x0, #CTX_DACR32_EL2]
161
162 mrs x13, par_el1
163 mrs x14, far_el1
164 stp x13, x14, [x0, #CTX_PAR_EL1]
165
166 mrs x15, afsr0_el1
167 mrs x16, afsr1_el1
168 stp x15, x16, [x0, #CTX_AFSR0_EL1]
169
170 mrs x17, contextidr_el1
171 mrs x9, vbar_el1
172 stp x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
173
174 mrs x10, cntp_ctl_el0
175 mrs x11, cntp_cval_el0
176 stp x10, x11, [x0, #CTX_CNTP_CTL_EL0]
177
178 mrs x12, cntv_ctl_el0
179 mrs x13, cntv_cval_el0
180 stp x12, x13, [x0, #CTX_CNTV_CTL_EL0]
181
182 mrs x14, cntkctl_el1
183 mrs x15, fpexc32_el2
184 stp x14, x15, [x0, #CTX_CNTKCTL_EL1]
185
186 ret
187
188/* -----------------------------------------------------
189 * The following function strictly follows the AArch64
190 * PCS to use x9-x17 (temporary caller-saved registers)
191 * to restore EL1 system register context. It assumes
192 * that 'x0' is pointing to a 'el1_sys_regs' structure
193 * from where the register context will be restored
194 * -----------------------------------------------------
195 */
196 .global el1_sysregs_context_restore
Andrew Thoelke0a30cf52014-03-18 13:46:55 +0000197func el1_sysregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000198
199 ldp x9, x10, [x0, #CTX_SPSR_EL1]
200 msr spsr_el1, x9
201 msr elr_el1, x10
202
203 ldp x11, x12, [x0, #CTX_SPSR_ABT]
204 msr spsr_abt, x11
205 msr spsr_und, x12
206
207 ldp x13, x14, [x0, #CTX_SPSR_IRQ]
208 msr spsr_irq, x13
209 msr spsr_fiq, x14
210
211 ldp x15, x16, [x0, #CTX_SCTLR_EL1]
212 msr sctlr_el1, x15
213 msr actlr_el1, x16
214
215 ldp x17, x9, [x0, #CTX_CPACR_EL1]
216 msr cpacr_el1, x17
217 msr csselr_el1, x9
218
219 ldp x10, x11, [x0, #CTX_SP_EL1]
220 msr sp_el1, x10
221 msr esr_el1, x11
222
223 ldp x12, x13, [x0, #CTX_TTBR0_EL1]
224 msr ttbr0_el1, x12
225 msr ttbr1_el1, x13
226
227 ldp x14, x15, [x0, #CTX_MAIR_EL1]
228 msr mair_el1, x14
229 msr amair_el1, x15
230
231 ldp x16, x17, [x0, #CTX_TCR_EL1]
232 msr tcr_el1, x16
233 msr tpidr_el1, x17
234
235 ldp x9, x10, [x0, #CTX_TPIDR_EL0]
236 msr tpidr_el0, x9
237 msr tpidrro_el0, x10
238
239 ldp x11, x12, [x0, #CTX_DACR32_EL2]
240 msr dacr32_el2, x11
241 msr ifsr32_el2, x12
242
243 ldp x13, x14, [x0, #CTX_PAR_EL1]
244 msr par_el1, x13
245 msr far_el1, x14
246
247 ldp x15, x16, [x0, #CTX_AFSR0_EL1]
248 msr afsr0_el1, x15
249 msr afsr1_el1, x16
250
251 ldp x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
252 msr contextidr_el1, x17
253 msr vbar_el1, x9
254
255 ldp x10, x11, [x0, #CTX_CNTP_CTL_EL0]
256 msr cntp_ctl_el0, x10
257 msr cntp_cval_el0, x11
258
259 ldp x12, x13, [x0, #CTX_CNTV_CTL_EL0]
260 msr cntv_ctl_el0, x12
261 msr cntv_cval_el0, x13
262
263 ldp x14, x15, [x0, #CTX_CNTKCTL_EL1]
264 msr cntkctl_el1, x14
265 msr fpexc32_el2, x15
266
267 /* No explict ISB required here as ERET covers it */
268
269 ret
270
271/* -----------------------------------------------------
272 * The followsing function follows the aapcs_64 strictly
273 * to use x9-x17 (temporary caller-saved registers
274 * according to AArch64 PCS) to save floating point
275 * register context. It assumes that 'x0' is pointing to
276 * a 'fp_regs' structure where the register context will
277 * be saved.
278 *
279 * Access to VFP registers will trap if CPTR_EL3.TFP is
280 * set. However currently we don't use VFP registers
281 * nor set traps in Trusted Firmware, and assume it's
282 * cleared
283 *
284 * TODO: Revisit when VFP is used in secure world
285 * -----------------------------------------------------
286 */
287 .global fpregs_context_save
Andrew Thoelke0a30cf52014-03-18 13:46:55 +0000288func fpregs_context_save
Achin Gupta9ac63c52014-01-16 12:08:03 +0000289 stp q0, q1, [x0, #CTX_FP_Q0]
290 stp q2, q3, [x0, #CTX_FP_Q2]
291 stp q4, q5, [x0, #CTX_FP_Q4]
292 stp q6, q7, [x0, #CTX_FP_Q6]
293 stp q8, q9, [x0, #CTX_FP_Q8]
294 stp q10, q11, [x0, #CTX_FP_Q10]
295 stp q12, q13, [x0, #CTX_FP_Q12]
296 stp q14, q15, [x0, #CTX_FP_Q14]
297 stp q16, q17, [x0, #CTX_FP_Q16]
298 stp q18, q19, [x0, #CTX_FP_Q18]
299 stp q20, q21, [x0, #CTX_FP_Q20]
300 stp q22, q23, [x0, #CTX_FP_Q22]
301 stp q24, q25, [x0, #CTX_FP_Q24]
302 stp q26, q27, [x0, #CTX_FP_Q26]
303 stp q28, q29, [x0, #CTX_FP_Q28]
304 stp q30, q31, [x0, #CTX_FP_Q30]
305
306 mrs x9, fpsr
307 str x9, [x0, #CTX_FP_FPSR]
308
309 mrs x10, fpcr
310 str x10, [x0, #CTX_FP_FPCR]
311
312 ret
313
314/* -----------------------------------------------------
315 * The following function follows the aapcs_64 strictly
316 * to use x9-x17 (temporary caller-saved registers
317 * according to AArch64 PCS) to restore floating point
318 * register context. It assumes that 'x0' is pointing to
319 * a 'fp_regs' structure from where the register context
320 * will be restored.
321 *
322 * Access to VFP registers will trap if CPTR_EL3.TFP is
323 * set. However currently we don't use VFP registers
324 * nor set traps in Trusted Firmware, and assume it's
325 * cleared
326 *
327 * TODO: Revisit when VFP is used in secure world
328 * -----------------------------------------------------
329 */
330 .global fpregs_context_restore
Andrew Thoelke0a30cf52014-03-18 13:46:55 +0000331func fpregs_context_restore
Achin Gupta9ac63c52014-01-16 12:08:03 +0000332 ldp q0, q1, [x0, #CTX_FP_Q0]
333 ldp q2, q3, [x0, #CTX_FP_Q2]
334 ldp q4, q5, [x0, #CTX_FP_Q4]
335 ldp q6, q7, [x0, #CTX_FP_Q6]
336 ldp q8, q9, [x0, #CTX_FP_Q8]
337 ldp q10, q11, [x0, #CTX_FP_Q10]
338 ldp q12, q13, [x0, #CTX_FP_Q12]
339 ldp q14, q15, [x0, #CTX_FP_Q14]
340 ldp q16, q17, [x0, #CTX_FP_Q16]
341 ldp q18, q19, [x0, #CTX_FP_Q18]
342 ldp q20, q21, [x0, #CTX_FP_Q20]
343 ldp q22, q23, [x0, #CTX_FP_Q22]
344 ldp q24, q25, [x0, #CTX_FP_Q24]
345 ldp q26, q27, [x0, #CTX_FP_Q26]
346 ldp q28, q29, [x0, #CTX_FP_Q28]
347 ldp q30, q31, [x0, #CTX_FP_Q30]
348
349 ldr x9, [x0, #CTX_FP_FPSR]
350 msr fpsr, x9
351
352 str x10, [x0, #CTX_FP_FPCR]
353 msr fpcr, x10
354
355 /*
356 * No explict ISB required here as ERET to
357 * swtich to secure EL1 or non-secure world
358 * covers it
359 */
360
361 ret