]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmLib/AArch64/AArch64Support.S
ArmPlatformPkg/ArmPlatformStackLib: use callee preserved registers
[mirror_edk2.git] / ArmPkg / Library / ArmLib / AArch64 / AArch64Support.S
CommitLineData
25402f5d
HL
1#------------------------------------------------------------------------------\r
2#\r
3# Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
9401d6f4 4# Copyright (c) 2011 - 2014, ARM Limited. All rights reserved.\r
0efaa42f 5# Copyright (c) 2016, Linaro Limited. All rights reserved.\r
25402f5d
HL
6#\r
7# This program and the accompanying materials\r
8# are licensed and made available under the terms and conditions of the BSD License\r
9# which accompanies this distribution. The full text of the license may be found at\r
10# http://opensource.org/licenses/bsd-license.php\r
11#\r
12# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14#\r
15#------------------------------------------------------------------------------\r
16\r
17#include <Chipset/AArch64.h>\r
18#include <AsmMacroIoLibV8.h>\r
19\r
25402f5d
HL
20.set CTRL_M_BIT, (1 << 0)\r
21.set CTRL_A_BIT, (1 << 1)\r
22.set CTRL_C_BIT, (1 << 2)\r
23.set CTRL_I_BIT, (1 << 12)\r
24.set CTRL_V_BIT, (1 << 12)\r
25.set CPACR_VFP_BITS, (3 << 20)\r
26\r
0efaa42f 27ASM_FUNC(ArmInvalidateDataCacheEntryByMVA)\r
25402f5d 28 dc ivac, x0 // Invalidate single data cache line\r
25402f5d
HL
29 ret\r
30\r
31\r
0efaa42f 32ASM_FUNC(ArmCleanDataCacheEntryByMVA)\r
25402f5d 33 dc cvac, x0 // Clean single data cache line\r
25402f5d
HL
34 ret\r
35\r
36\r
0efaa42f 37ASM_FUNC(ArmCleanDataCacheEntryToPoUByMVA)\r
b7de7e3c
EC
38 dc cvau, x0 // Clean single data cache line to PoU\r
39 ret\r
40\r
0efaa42f 41ASM_FUNC(ArmInvalidateInstructionCacheEntryToPoUByMVA)\r
cf580da1
AB
42 ic ivau, x0 // Invalidate single instruction cache line to PoU\r
43 ret\r
44\r
b7de7e3c 45\r
0efaa42f 46ASM_FUNC(ArmCleanInvalidateDataCacheEntryByMVA)\r
25402f5d 47 dc civac, x0 // Clean and invalidate single data cache line\r
25402f5d
HL
48 ret\r
49\r
50\r
0efaa42f 51ASM_FUNC(ArmInvalidateDataCacheEntryBySetWay)\r
25402f5d 52 dc isw, x0 // Invalidate this line\r
25402f5d
HL
53 ret\r
54\r
55\r
0efaa42f 56ASM_FUNC(ArmCleanInvalidateDataCacheEntryBySetWay)\r
25402f5d 57 dc cisw, x0 // Clean and Invalidate this line\r
25402f5d
HL
58 ret\r
59\r
60\r
0efaa42f 61ASM_FUNC(ArmCleanDataCacheEntryBySetWay)\r
25402f5d 62 dc csw, x0 // Clean this line\r
25402f5d
HL
63 ret\r
64\r
65\r
0efaa42f 66ASM_FUNC(ArmInvalidateInstructionCache)\r
25402f5d
HL
67 ic iallu // Invalidate entire instruction cache\r
68 dsb sy\r
69 isb\r
70 ret\r
71\r
72\r
0efaa42f 73ASM_FUNC(ArmEnableMmu)\r
25402f5d
HL
74 EL1_OR_EL2_OR_EL3(x1)\r
751: mrs x0, sctlr_el1 // Read System control register EL1\r
76 b 4f\r
772: mrs x0, sctlr_el2 // Read System control register EL2\r
78 b 4f\r
793: mrs x0, sctlr_el3 // Read System control register EL3\r
804: orr x0, x0, #CTRL_M_BIT // Set MMU enable bit\r
81 EL1_OR_EL2_OR_EL3(x1)\r
70f89c0b 821: tlbi vmalle1\r
ee95f9e1 83 dsb nsh\r
25402f5d
HL
84 isb\r
85 msr sctlr_el1, x0 // Write back\r
86 b 4f\r
872: tlbi alle2\r
ee95f9e1 88 dsb nsh\r
25402f5d
HL
89 isb\r
90 msr sctlr_el2, x0 // Write back\r
91 b 4f\r
923: tlbi alle3\r
ee95f9e1 93 dsb nsh\r
25402f5d
HL
94 isb\r
95 msr sctlr_el3, x0 // Write back\r
ee95f9e1 964: isb\r
25402f5d
HL
97 ret\r
98\r
99\r
0efaa42f 100ASM_FUNC(ArmDisableMmu)\r
25402f5d
HL
101 EL1_OR_EL2_OR_EL3(x1)\r
1021: mrs x0, sctlr_el1 // Read System Control Register EL1\r
103 b 4f\r
1042: mrs x0, sctlr_el2 // Read System Control Register EL2\r
105 b 4f\r
1063: mrs x0, sctlr_el3 // Read System Control Register EL3\r
73ca5009 1074: and x0, x0, #~CTRL_M_BIT // Clear MMU enable bit\r
25402f5d
HL
108 EL1_OR_EL2_OR_EL3(x1)\r
1091: msr sctlr_el1, x0 // Write back\r
70f89c0b 110 tlbi vmalle1\r
25402f5d
HL
111 b 4f\r
1122: msr sctlr_el2, x0 // Write back\r
113 tlbi alle2\r
114 b 4f\r
1153: msr sctlr_el3, x0 // Write back\r
116 tlbi alle3\r
1174: dsb sy\r
118 isb\r
119 ret\r
120\r
121\r
0efaa42f 122ASM_FUNC(ArmDisableCachesAndMmu)\r
25402f5d
HL
123 EL1_OR_EL2_OR_EL3(x1)\r
1241: mrs x0, sctlr_el1 // Get control register EL1\r
125 b 4f\r
1262: mrs x0, sctlr_el2 // Get control register EL2\r
127 b 4f\r
1283: mrs x0, sctlr_el3 // Get control register EL3\r
73ca5009
BJ
1294: mov x1, #~(CTRL_M_BIT | CTRL_C_BIT | CTRL_I_BIT) // Disable MMU, D & I caches\r
130 and x0, x0, x1\r
25402f5d
HL
131 EL1_OR_EL2_OR_EL3(x1)\r
1321: msr sctlr_el1, x0 // Write back control register\r
133 b 4f\r
1342: msr sctlr_el2, x0 // Write back control register\r
135 b 4f\r
1363: msr sctlr_el3, x0 // Write back control register\r
1374: dsb sy\r
138 isb\r
139 ret\r
140\r
141\r
0efaa42f 142ASM_FUNC(ArmMmuEnabled)\r
25402f5d
HL
143 EL1_OR_EL2_OR_EL3(x1)\r
1441: mrs x0, sctlr_el1 // Get control register EL1\r
145 b 4f\r
1462: mrs x0, sctlr_el2 // Get control register EL2\r
147 b 4f\r
1483: mrs x0, sctlr_el3 // Get control register EL3\r
1494: and x0, x0, #CTRL_M_BIT\r
150 ret\r
151\r
152\r
0efaa42f 153ASM_FUNC(ArmEnableDataCache)\r
25402f5d
HL
154 EL1_OR_EL2_OR_EL3(x1)\r
1551: mrs x0, sctlr_el1 // Get control register EL1\r
156 b 4f\r
1572: mrs x0, sctlr_el2 // Get control register EL2\r
158 b 4f\r
1593: mrs x0, sctlr_el3 // Get control register EL3\r
1604: orr x0, x0, #CTRL_C_BIT // Set C bit\r
161 EL1_OR_EL2_OR_EL3(x1)\r
1621: msr sctlr_el1, x0 // Write back control register\r
163 b 4f\r
1642: msr sctlr_el2, x0 // Write back control register\r
165 b 4f\r
1663: msr sctlr_el3, x0 // Write back control register\r
1674: dsb sy\r
168 isb\r
169 ret\r
170\r
171\r
0efaa42f 172ASM_FUNC(ArmDisableDataCache)\r
25402f5d
HL
173 EL1_OR_EL2_OR_EL3(x1)\r
1741: mrs x0, sctlr_el1 // Get control register EL1\r
175 b 4f\r
1762: mrs x0, sctlr_el2 // Get control register EL2\r
177 b 4f\r
1783: mrs x0, sctlr_el3 // Get control register EL3\r
73ca5009 1794: and x0, x0, #~CTRL_C_BIT // Clear C bit\r
25402f5d
HL
180 EL1_OR_EL2_OR_EL3(x1)\r
1811: msr sctlr_el1, x0 // Write back control register\r
182 b 4f\r
1832: msr sctlr_el2, x0 // Write back control register\r
184 b 4f\r
1853: msr sctlr_el3, x0 // Write back control register\r
1864: dsb sy\r
187 isb\r
188 ret\r
189\r
190\r
0efaa42f 191ASM_FUNC(ArmEnableInstructionCache)\r
25402f5d
HL
192 EL1_OR_EL2_OR_EL3(x1)\r
1931: mrs x0, sctlr_el1 // Get control register EL1\r
194 b 4f\r
1952: mrs x0, sctlr_el2 // Get control register EL2\r
196 b 4f\r
1973: mrs x0, sctlr_el3 // Get control register EL3\r
1984: orr x0, x0, #CTRL_I_BIT // Set I bit\r
199 EL1_OR_EL2_OR_EL3(x1)\r
2001: msr sctlr_el1, x0 // Write back control register\r
201 b 4f\r
2022: msr sctlr_el2, x0 // Write back control register\r
203 b 4f\r
2043: msr sctlr_el3, x0 // Write back control register\r
2054: dsb sy\r
206 isb\r
207 ret\r
208\r
209\r
0efaa42f 210ASM_FUNC(ArmDisableInstructionCache)\r
25402f5d
HL
211 EL1_OR_EL2_OR_EL3(x1)\r
2121: mrs x0, sctlr_el1 // Get control register EL1\r
213 b 4f\r
2142: mrs x0, sctlr_el2 // Get control register EL2\r
215 b 4f\r
2163: mrs x0, sctlr_el3 // Get control register EL3\r
73ca5009 2174: and x0, x0, #~CTRL_I_BIT // Clear I bit\r
25402f5d
HL
218 EL1_OR_EL2_OR_EL3(x1)\r
2191: msr sctlr_el1, x0 // Write back control register\r
220 b 4f\r
2212: msr sctlr_el2, x0 // Write back control register\r
222 b 4f\r
2233: msr sctlr_el3, x0 // Write back control register\r
2244: dsb sy\r
225 isb\r
226 ret\r
227\r
228\r
0efaa42f 229ASM_FUNC(ArmEnableAlignmentCheck)\r
25402f5d
HL
230 EL1_OR_EL2(x1)\r
2311: mrs x0, sctlr_el1 // Get control register EL1\r
232 b 3f\r
2332: mrs x0, sctlr_el2 // Get control register EL2\r
2343: orr x0, x0, #CTRL_A_BIT // Set A (alignment check) bit\r
235 EL1_OR_EL2(x1)\r
2361: msr sctlr_el1, x0 // Write back control register\r
237 b 3f\r
2382: msr sctlr_el2, x0 // Write back control register\r
2393: dsb sy\r
240 isb\r
241 ret\r
242\r
243\r
0efaa42f 244ASM_FUNC(ArmDisableAlignmentCheck)\r
25402f5d
HL
245 EL1_OR_EL2_OR_EL3(x1)\r
2461: mrs x0, sctlr_el1 // Get control register EL1\r
247 b 4f\r
2482: mrs x0, sctlr_el2 // Get control register EL2\r
249 b 4f\r
2503: mrs x0, sctlr_el3 // Get control register EL3\r
73ca5009 2514: and x0, x0, #~CTRL_A_BIT // Clear A (alignment check) bit\r
25402f5d
HL
252 EL1_OR_EL2_OR_EL3(x1)\r
2531: msr sctlr_el1, x0 // Write back control register\r
254 b 4f\r
2552: msr sctlr_el2, x0 // Write back control register\r
256 b 4f\r
2573: msr sctlr_el3, x0 // Write back control register\r
2584: dsb sy\r
259 isb\r
260 ret\r
261\r
262\r
263// Always turned on in AArch64. Else implementation specific. Leave in for C compatibility for now\r
0efaa42f 264ASM_FUNC(ArmEnableBranchPrediction)\r
25402f5d
HL
265 ret\r
266\r
267\r
268// Always turned on in AArch64. Else implementation specific. Leave in for C compatibility for now.\r
0efaa42f 269ASM_FUNC(ArmDisableBranchPrediction)\r
25402f5d
HL
270 ret\r
271\r
272\r
0efaa42f 273ASM_FUNC(AArch64AllDataCachesOperation)\r
25402f5d 274// We can use regs 0-7 and 9-15 without having to save/restore.\r
fb7ea611
OM
275// Save our link register on the stack. - The stack must always be quad-word aligned\r
276 str x30, [sp, #-16]!\r
25402f5d
HL
277 mov x1, x0 // Save Function call in x1\r
278 mrs x6, clidr_el1 // Read EL1 CLIDR\r
279 and x3, x6, #0x7000000 // Mask out all but Level of Coherency (LoC)\r
433a49a0
OM
280 lsr x3, x3, #23 // Left align cache level value - the level is shifted by 1 to the\r
281 // right to ease the access to CSSELR and the Set/Way operation.\r
25402f5d
HL
282 cbz x3, L_Finished // No need to clean if LoC is 0\r
283 mov x10, #0 // Start clean at cache level 0\r
25402f5d
HL
284\r
285Loop1:\r
286 add x2, x10, x10, lsr #1 // Work out 3x cachelevel for cache info\r
287 lsr x12, x6, x2 // bottom 3 bits are the Cache type for this level\r
288 and x12, x12, #7 // get those 3 bits alone\r
289 cmp x12, #2 // what cache at this level?\r
290 b.lt L_Skip // no cache or only instruction cache at this level\r
291 msr csselr_el1, x10 // write the Cache Size selection register with current level (CSSELR)\r
292 isb // isb to sync the change to the CacheSizeID reg\r
293 mrs x12, ccsidr_el1 // reads current Cache Size ID register (CCSIDR)\r
294 and x2, x12, #0x7 // extract the line length field\r
295 add x2, x2, #4 // add 4 for the line length offset (log2 16 bytes)\r
296 mov x4, #0x400\r
297 sub x4, x4, #1\r
298 and x4, x4, x12, lsr #3 // x4 is the max number on the way size (right aligned)\r
299 clz w5, w4 // w5 is the bit position of the way size increment\r
300 mov x7, #0x00008000\r
301 sub x7, x7, #1\r
302 and x7, x7, x12, lsr #13 // x7 is the max number of the index size (right aligned)\r
303\r
304Loop2:\r
305 mov x9, x4 // x9 working copy of the max way size (right aligned)\r
306\r
307Loop3:\r
308 lsl x11, x9, x5\r
309 orr x0, x10, x11 // factor in the way number and cache number\r
310 lsl x11, x7, x2\r
311 orr x0, x0, x11 // factor in the index number\r
312\r
313 blr x1 // Goto requested cache operation\r
314\r
315 subs x9, x9, #1 // decrement the way number\r
316 b.ge Loop3\r
317 subs x7, x7, #1 // decrement the index\r
318 b.ge Loop2\r
319L_Skip:\r
320 add x10, x10, #2 // increment the cache number\r
321 cmp x3, x10\r
322 b.gt Loop1\r
323\r
324L_Finished:\r
325 dsb sy\r
326 isb\r
327 ldr x30, [sp], #0x10\r
328 ret\r
329\r
330\r
0efaa42f 331ASM_FUNC(ArmDataMemoryBarrier)\r
25402f5d
HL
332 dmb sy\r
333 ret\r
334\r
335\r
0efaa42f 336ASM_FUNC(ArmDataSynchronizationBarrier)\r
25402f5d
HL
337 dsb sy\r
338 ret\r
339\r
340\r
0efaa42f 341ASM_FUNC(ArmInstructionSynchronizationBarrier)\r
25402f5d
HL
342 isb\r
343 ret\r
344\r
345\r
0efaa42f 346ASM_FUNC(ArmWriteVBar)\r
25402f5d
HL
347 EL1_OR_EL2_OR_EL3(x1)\r
3481: msr vbar_el1, x0 // Set the Address of the EL1 Vector Table in the VBAR register\r
349 b 4f\r
3502: msr vbar_el2, x0 // Set the Address of the EL2 Vector Table in the VBAR register\r
351 b 4f\r
3523: msr vbar_el3, x0 // Set the Address of the EL3 Vector Table in the VBAR register\r
3534: isb\r
354 ret\r
355\r
0efaa42f 356ASM_FUNC(ArmReadVBar)\r
f0247796
OM
357 EL1_OR_EL2_OR_EL3(x1)\r
3581: mrs x0, vbar_el1 // Set the Address of the EL1 Vector Table in the VBAR register\r
359 ret\r
3602: mrs x0, vbar_el2 // Set the Address of the EL2 Vector Table in the VBAR register\r
361 ret\r
3623: mrs x0, vbar_el3 // Set the Address of the EL3 Vector Table in the VBAR register\r
363 ret\r
364\r
365\r
0efaa42f 366ASM_FUNC(ArmEnableVFP)\r
25402f5d
HL
367 // Check whether floating-point is implemented in the processor.\r
368 mov x1, x30 // Save LR\r
369 bl ArmReadIdPfr0 // Read EL1 Processor Feature Register (PFR0)\r
370 mov x30, x1 // Restore LR\r
371 ands x0, x0, #AARCH64_PFR0_FP// Extract bits indicating VFP implementation\r
372 cmp x0, #0 // VFP is implemented if '0'.\r
373 b.ne 4f // Exit if VFP not implemented.\r
374 // FVP is implemented.\r
375 // Make sure VFP exceptions are not trapped (to any exception level).\r
376 mrs x0, cpacr_el1 // Read EL1 Coprocessor Access Control Register (CPACR)\r
377 orr x0, x0, #CPACR_VFP_BITS // Disable FVP traps to EL1\r
378 msr cpacr_el1, x0 // Write back EL1 Coprocessor Access Control Register (CPACR)\r
379 mov x1, #AARCH64_CPTR_TFP // TFP Bit for trapping VFP Exceptions\r
380 EL1_OR_EL2_OR_EL3(x2)\r
3811:ret // Not configurable in EL1\r
3822:mrs x0, cptr_el2 // Disable VFP traps to EL2\r
383 bic x0, x0, x1\r
384 msr cptr_el2, x0\r
385 ret\r
3863:mrs x0, cptr_el3 // Disable VFP traps to EL3\r
387 bic x0, x0, x1\r
388 msr cptr_el3, x0\r
3894:ret\r
390\r
391\r
0efaa42f 392ASM_FUNC(ArmCallWFI)\r
25402f5d
HL
393 wfi\r
394 ret\r
395\r
396\r
0efaa42f 397ASM_FUNC(ArmReadMpidr)\r
25402f5d
HL
398 mrs x0, mpidr_el1 // read EL1 MPIDR\r
399 ret\r
400\r
401\r
402// Keep old function names for C compatibilty for now. Change later?\r
0efaa42f 403ASM_FUNC(ArmReadTpidrurw)\r
25402f5d
HL
404 mrs x0, tpidr_el0 // read tpidr_el0 (v7 TPIDRURW) -> (v8 TPIDR_EL0)\r
405 ret\r
406\r
407\r
408// Keep old function names for C compatibilty for now. Change later?\r
0efaa42f 409ASM_FUNC(ArmWriteTpidrurw)\r
25402f5d
HL
410 msr tpidr_el0, x0 // write tpidr_el0 (v7 TPIDRURW) -> (v8 TPIDR_EL0)\r
411 ret\r
412\r
413\r
414// Arch timers are mandatory on AArch64\r
0efaa42f 415ASM_FUNC(ArmIsArchTimerImplemented)\r
25402f5d
HL
416 mov x0, #1\r
417 ret\r
418\r
419\r
0efaa42f 420ASM_FUNC(ArmReadIdPfr0)\r
25402f5d
HL
421 mrs x0, id_aa64pfr0_el1 // Read ID_AA64PFR0 Register\r
422 ret\r
423\r
424\r
425// Q: id_aa64pfr1_el1 not defined yet. What does this funtion want to access?\r
426// A: used to setup arch timer. Check if we have security extensions, permissions to set stuff.\r
427// See: ArmPkg/Library/ArmArchTimerLib/AArch64/ArmArchTimerLib.c\r
428// Not defined yet, but stick in here for now, should read all zeros.\r
0efaa42f 429ASM_FUNC(ArmReadIdPfr1)\r
25402f5d
HL
430 mrs x0, id_aa64pfr1_el1 // Read ID_PFR1 Register\r
431 ret\r
432\r
433// VOID ArmWriteHcr(UINTN Hcr)\r
0efaa42f 434ASM_FUNC(ArmWriteHcr)\r
25402f5d
HL
435 msr hcr_el2, x0 // Write the passed HCR value\r
436 ret\r
437\r
d2bb61a2 438// UINTN ArmReadHcr(VOID)\r
0efaa42f 439ASM_FUNC(ArmReadHcr)\r
d2bb61a2
EC
440 mrs x0, hcr_el2\r
441 ret\r
442\r
25402f5d 443// UINTN ArmReadCurrentEL(VOID)\r
0efaa42f 444ASM_FUNC(ArmReadCurrentEL)\r
25402f5d
HL
445 mrs x0, CurrentEL\r
446 ret\r
447\r
25402f5d 448ASM_FUNCTION_REMOVE_IF_UNREFERENCED\r