]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmLib/AArch64/AArch64Support.S
ArmPkg: update InvalidateInstructionCacheRange to flush only to PoU
[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
25402f5d
HL
5#\r
6# This program and the accompanying materials\r
7# are licensed and made available under the terms and conditions of the BSD License\r
8# which accompanies this distribution. The full text of the license may be found at\r
9# http://opensource.org/licenses/bsd-license.php\r
10#\r
11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13#\r
14#------------------------------------------------------------------------------\r
15\r
16#include <Chipset/AArch64.h>\r
17#include <AsmMacroIoLibV8.h>\r
18\r
19.text\r
20.align 3\r
21\r
22GCC_ASM_EXPORT (ArmInvalidateInstructionCache)\r
23GCC_ASM_EXPORT (ArmInvalidateDataCacheEntryByMVA)\r
24GCC_ASM_EXPORT (ArmCleanDataCacheEntryByMVA)\r
b7de7e3c 25GCC_ASM_EXPORT (ArmCleanDataCacheEntryToPoUByMVA)\r
25402f5d
HL
26GCC_ASM_EXPORT (ArmCleanInvalidateDataCacheEntryByMVA)\r
27GCC_ASM_EXPORT (ArmInvalidateDataCacheEntryBySetWay)\r
28GCC_ASM_EXPORT (ArmCleanDataCacheEntryBySetWay)\r
29GCC_ASM_EXPORT (ArmCleanInvalidateDataCacheEntryBySetWay)\r
25402f5d
HL
30GCC_ASM_EXPORT (ArmEnableMmu)\r
31GCC_ASM_EXPORT (ArmDisableMmu)\r
32GCC_ASM_EXPORT (ArmDisableCachesAndMmu)\r
33GCC_ASM_EXPORT (ArmMmuEnabled)\r
34GCC_ASM_EXPORT (ArmEnableDataCache)\r
35GCC_ASM_EXPORT (ArmDisableDataCache)\r
36GCC_ASM_EXPORT (ArmEnableInstructionCache)\r
37GCC_ASM_EXPORT (ArmDisableInstructionCache)\r
38GCC_ASM_EXPORT (ArmDisableAlignmentCheck)\r
39GCC_ASM_EXPORT (ArmEnableAlignmentCheck)\r
40GCC_ASM_EXPORT (ArmEnableBranchPrediction)\r
41GCC_ASM_EXPORT (ArmDisableBranchPrediction)\r
42GCC_ASM_EXPORT (AArch64AllDataCachesOperation)\r
25402f5d 43GCC_ASM_EXPORT (ArmDataMemoryBarrier)\r
cf93a378 44GCC_ASM_EXPORT (ArmDataSynchronizationBarrier)\r
25402f5d
HL
45GCC_ASM_EXPORT (ArmInstructionSynchronizationBarrier)\r
46GCC_ASM_EXPORT (ArmWriteVBar)\r
f0247796 47GCC_ASM_EXPORT (ArmReadVBar)\r
25402f5d
HL
48GCC_ASM_EXPORT (ArmEnableVFP)\r
49GCC_ASM_EXPORT (ArmCallWFI)\r
25402f5d
HL
50GCC_ASM_EXPORT (ArmReadMpidr)\r
51GCC_ASM_EXPORT (ArmReadTpidrurw)\r
52GCC_ASM_EXPORT (ArmWriteTpidrurw)\r
53GCC_ASM_EXPORT (ArmIsArchTimerImplemented)\r
54GCC_ASM_EXPORT (ArmReadIdPfr0)\r
55GCC_ASM_EXPORT (ArmReadIdPfr1)\r
56GCC_ASM_EXPORT (ArmWriteHcr)\r
57GCC_ASM_EXPORT (ArmReadCurrentEL)\r
58\r
59.set CTRL_M_BIT, (1 << 0)\r
60.set CTRL_A_BIT, (1 << 1)\r
61.set CTRL_C_BIT, (1 << 2)\r
62.set CTRL_I_BIT, (1 << 12)\r
63.set CTRL_V_BIT, (1 << 12)\r
64.set CPACR_VFP_BITS, (3 << 20)\r
65\r
66ASM_PFX(ArmInvalidateDataCacheEntryByMVA):\r
67 dc ivac, x0 // Invalidate single data cache line\r
25402f5d
HL
68 ret\r
69\r
70\r
71ASM_PFX(ArmCleanDataCacheEntryByMVA):\r
72 dc cvac, x0 // Clean single data cache line\r
25402f5d
HL
73 ret\r
74\r
75\r
b7de7e3c
EC
76ASM_PFX(ArmCleanDataCacheEntryToPoUByMVA):\r
77 dc cvau, x0 // Clean single data cache line to PoU\r
78 ret\r
79\r
80\r
25402f5d
HL
81ASM_PFX(ArmCleanInvalidateDataCacheEntryByMVA):\r
82 dc civac, x0 // Clean and invalidate single data cache line\r
25402f5d
HL
83 ret\r
84\r
85\r
86ASM_PFX(ArmInvalidateDataCacheEntryBySetWay):\r
87 dc isw, x0 // Invalidate this line\r
25402f5d
HL
88 ret\r
89\r
90\r
91ASM_PFX(ArmCleanInvalidateDataCacheEntryBySetWay):\r
92 dc cisw, x0 // Clean and Invalidate this line\r
25402f5d
HL
93 ret\r
94\r
95\r
96ASM_PFX(ArmCleanDataCacheEntryBySetWay):\r
97 dc csw, x0 // Clean this line\r
25402f5d
HL
98 ret\r
99\r
100\r
101ASM_PFX(ArmInvalidateInstructionCache):\r
102 ic iallu // Invalidate entire instruction cache\r
103 dsb sy\r
104 isb\r
105 ret\r
106\r
107\r
108ASM_PFX(ArmEnableMmu):\r
109 EL1_OR_EL2_OR_EL3(x1)\r
1101: mrs x0, sctlr_el1 // Read System control register EL1\r
111 b 4f\r
1122: mrs x0, sctlr_el2 // Read System control register EL2\r
113 b 4f\r
1143: mrs x0, sctlr_el3 // Read System control register EL3\r
1154: orr x0, x0, #CTRL_M_BIT // Set MMU enable bit\r
116 EL1_OR_EL2_OR_EL3(x1)\r
70f89c0b 1171: tlbi vmalle1\r
ee95f9e1 118 dsb nsh\r
25402f5d
HL
119 isb\r
120 msr sctlr_el1, x0 // Write back\r
121 b 4f\r
1222: tlbi alle2\r
ee95f9e1 123 dsb nsh\r
25402f5d
HL
124 isb\r
125 msr sctlr_el2, x0 // Write back\r
126 b 4f\r
1273: tlbi alle3\r
ee95f9e1 128 dsb nsh\r
25402f5d
HL
129 isb\r
130 msr sctlr_el3, x0 // Write back\r
ee95f9e1 1314: isb\r
25402f5d
HL
132 ret\r
133\r
134\r
135ASM_PFX(ArmDisableMmu):\r
136 EL1_OR_EL2_OR_EL3(x1)\r
1371: mrs x0, sctlr_el1 // Read System Control Register EL1\r
138 b 4f\r
1392: mrs x0, sctlr_el2 // Read System Control Register EL2\r
140 b 4f\r
1413: mrs x0, sctlr_el3 // Read System Control Register EL3\r
73ca5009 1424: and x0, x0, #~CTRL_M_BIT // Clear MMU enable bit\r
25402f5d
HL
143 EL1_OR_EL2_OR_EL3(x1)\r
1441: msr sctlr_el1, x0 // Write back\r
70f89c0b 145 tlbi vmalle1\r
25402f5d
HL
146 b 4f\r
1472: msr sctlr_el2, x0 // Write back\r
148 tlbi alle2\r
149 b 4f\r
1503: msr sctlr_el3, x0 // Write back\r
151 tlbi alle3\r
1524: dsb sy\r
153 isb\r
154 ret\r
155\r
156\r
157ASM_PFX(ArmDisableCachesAndMmu):\r
158 EL1_OR_EL2_OR_EL3(x1)\r
1591: mrs x0, sctlr_el1 // Get control register EL1\r
160 b 4f\r
1612: mrs x0, sctlr_el2 // Get control register EL2\r
162 b 4f\r
1633: mrs x0, sctlr_el3 // Get control register EL3\r
73ca5009
BJ
1644: mov x1, #~(CTRL_M_BIT | CTRL_C_BIT | CTRL_I_BIT) // Disable MMU, D & I caches\r
165 and x0, x0, x1\r
25402f5d
HL
166 EL1_OR_EL2_OR_EL3(x1)\r
1671: msr sctlr_el1, x0 // Write back control register\r
168 b 4f\r
1692: msr sctlr_el2, x0 // Write back control register\r
170 b 4f\r
1713: msr sctlr_el3, x0 // Write back control register\r
1724: dsb sy\r
173 isb\r
174 ret\r
175\r
176\r
177ASM_PFX(ArmMmuEnabled):\r
178 EL1_OR_EL2_OR_EL3(x1)\r
1791: mrs x0, sctlr_el1 // Get control register EL1\r
180 b 4f\r
1812: mrs x0, sctlr_el2 // Get control register EL2\r
182 b 4f\r
1833: mrs x0, sctlr_el3 // Get control register EL3\r
1844: and x0, x0, #CTRL_M_BIT\r
185 ret\r
186\r
187\r
188ASM_PFX(ArmEnableDataCache):\r
189 EL1_OR_EL2_OR_EL3(x1)\r
1901: mrs x0, sctlr_el1 // Get control register EL1\r
191 b 4f\r
1922: mrs x0, sctlr_el2 // Get control register EL2\r
193 b 4f\r
1943: mrs x0, sctlr_el3 // Get control register EL3\r
1954: orr x0, x0, #CTRL_C_BIT // Set C bit\r
196 EL1_OR_EL2_OR_EL3(x1)\r
1971: msr sctlr_el1, x0 // Write back control register\r
198 b 4f\r
1992: msr sctlr_el2, x0 // Write back control register\r
200 b 4f\r
2013: msr sctlr_el3, x0 // Write back control register\r
2024: dsb sy\r
203 isb\r
204 ret\r
205\r
206\r
207ASM_PFX(ArmDisableDataCache):\r
208 EL1_OR_EL2_OR_EL3(x1)\r
2091: mrs x0, sctlr_el1 // Get control register EL1\r
210 b 4f\r
2112: mrs x0, sctlr_el2 // Get control register EL2\r
212 b 4f\r
2133: mrs x0, sctlr_el3 // Get control register EL3\r
73ca5009 2144: and x0, x0, #~CTRL_C_BIT // Clear C bit\r
25402f5d
HL
215 EL1_OR_EL2_OR_EL3(x1)\r
2161: msr sctlr_el1, x0 // Write back control register\r
217 b 4f\r
2182: msr sctlr_el2, x0 // Write back control register\r
219 b 4f\r
2203: msr sctlr_el3, x0 // Write back control register\r
2214: dsb sy\r
222 isb\r
223 ret\r
224\r
225\r
226ASM_PFX(ArmEnableInstructionCache):\r
227 EL1_OR_EL2_OR_EL3(x1)\r
2281: mrs x0, sctlr_el1 // Get control register EL1\r
229 b 4f\r
2302: mrs x0, sctlr_el2 // Get control register EL2\r
231 b 4f\r
2323: mrs x0, sctlr_el3 // Get control register EL3\r
2334: orr x0, x0, #CTRL_I_BIT // Set I bit\r
234 EL1_OR_EL2_OR_EL3(x1)\r
2351: msr sctlr_el1, x0 // Write back control register\r
236 b 4f\r
2372: msr sctlr_el2, x0 // Write back control register\r
238 b 4f\r
2393: msr sctlr_el3, x0 // Write back control register\r
2404: dsb sy\r
241 isb\r
242 ret\r
243\r
244\r
245ASM_PFX(ArmDisableInstructionCache):\r
246 EL1_OR_EL2_OR_EL3(x1)\r
2471: mrs x0, sctlr_el1 // Get control register EL1\r
248 b 4f\r
2492: mrs x0, sctlr_el2 // Get control register EL2\r
250 b 4f\r
2513: mrs x0, sctlr_el3 // Get control register EL3\r
73ca5009 2524: and x0, x0, #~CTRL_I_BIT // Clear I bit\r
25402f5d
HL
253 EL1_OR_EL2_OR_EL3(x1)\r
2541: msr sctlr_el1, x0 // Write back control register\r
255 b 4f\r
2562: msr sctlr_el2, x0 // Write back control register\r
257 b 4f\r
2583: msr sctlr_el3, x0 // Write back control register\r
2594: dsb sy\r
260 isb\r
261 ret\r
262\r
263\r
264ASM_PFX(ArmEnableAlignmentCheck):\r
265 EL1_OR_EL2(x1)\r
2661: mrs x0, sctlr_el1 // Get control register EL1\r
267 b 3f\r
2682: mrs x0, sctlr_el2 // Get control register EL2\r
2693: orr x0, x0, #CTRL_A_BIT // Set A (alignment check) bit\r
270 EL1_OR_EL2(x1)\r
2711: msr sctlr_el1, x0 // Write back control register\r
272 b 3f\r
2732: msr sctlr_el2, x0 // Write back control register\r
2743: dsb sy\r
275 isb\r
276 ret\r
277\r
278\r
279ASM_PFX(ArmDisableAlignmentCheck):\r
280 EL1_OR_EL2_OR_EL3(x1)\r
2811: mrs x0, sctlr_el1 // Get control register EL1\r
282 b 4f\r
2832: mrs x0, sctlr_el2 // Get control register EL2\r
284 b 4f\r
2853: mrs x0, sctlr_el3 // Get control register EL3\r
73ca5009 2864: and x0, x0, #~CTRL_A_BIT // Clear A (alignment check) bit\r
25402f5d
HL
287 EL1_OR_EL2_OR_EL3(x1)\r
2881: msr sctlr_el1, x0 // Write back control register\r
289 b 4f\r
2902: msr sctlr_el2, x0 // Write back control register\r
291 b 4f\r
2923: msr sctlr_el3, x0 // Write back control register\r
2934: dsb sy\r
294 isb\r
295 ret\r
296\r
297\r
298// Always turned on in AArch64. Else implementation specific. Leave in for C compatibility for now\r
299ASM_PFX(ArmEnableBranchPrediction):\r
300 ret\r
301\r
302\r
303// Always turned on in AArch64. Else implementation specific. Leave in for C compatibility for now.\r
304ASM_PFX(ArmDisableBranchPrediction):\r
305 ret\r
306\r
307\r
308ASM_PFX(AArch64AllDataCachesOperation):\r
309// We can use regs 0-7 and 9-15 without having to save/restore.\r
fb7ea611
OM
310// Save our link register on the stack. - The stack must always be quad-word aligned\r
311 str x30, [sp, #-16]!\r
25402f5d
HL
312 mov x1, x0 // Save Function call in x1\r
313 mrs x6, clidr_el1 // Read EL1 CLIDR\r
314 and x3, x6, #0x7000000 // Mask out all but Level of Coherency (LoC)\r
433a49a0
OM
315 lsr x3, x3, #23 // Left align cache level value - the level is shifted by 1 to the\r
316 // right to ease the access to CSSELR and the Set/Way operation.\r
25402f5d
HL
317 cbz x3, L_Finished // No need to clean if LoC is 0\r
318 mov x10, #0 // Start clean at cache level 0\r
25402f5d
HL
319\r
320Loop1:\r
321 add x2, x10, x10, lsr #1 // Work out 3x cachelevel for cache info\r
322 lsr x12, x6, x2 // bottom 3 bits are the Cache type for this level\r
323 and x12, x12, #7 // get those 3 bits alone\r
324 cmp x12, #2 // what cache at this level?\r
325 b.lt L_Skip // no cache or only instruction cache at this level\r
326 msr csselr_el1, x10 // write the Cache Size selection register with current level (CSSELR)\r
327 isb // isb to sync the change to the CacheSizeID reg\r
328 mrs x12, ccsidr_el1 // reads current Cache Size ID register (CCSIDR)\r
329 and x2, x12, #0x7 // extract the line length field\r
330 add x2, x2, #4 // add 4 for the line length offset (log2 16 bytes)\r
331 mov x4, #0x400\r
332 sub x4, x4, #1\r
333 and x4, x4, x12, lsr #3 // x4 is the max number on the way size (right aligned)\r
334 clz w5, w4 // w5 is the bit position of the way size increment\r
335 mov x7, #0x00008000\r
336 sub x7, x7, #1\r
337 and x7, x7, x12, lsr #13 // x7 is the max number of the index size (right aligned)\r
338\r
339Loop2:\r
340 mov x9, x4 // x9 working copy of the max way size (right aligned)\r
341\r
342Loop3:\r
343 lsl x11, x9, x5\r
344 orr x0, x10, x11 // factor in the way number and cache number\r
345 lsl x11, x7, x2\r
346 orr x0, x0, x11 // factor in the index number\r
347\r
348 blr x1 // Goto requested cache operation\r
349\r
350 subs x9, x9, #1 // decrement the way number\r
351 b.ge Loop3\r
352 subs x7, x7, #1 // decrement the index\r
353 b.ge Loop2\r
354L_Skip:\r
355 add x10, x10, #2 // increment the cache number\r
356 cmp x3, x10\r
357 b.gt Loop1\r
358\r
359L_Finished:\r
360 dsb sy\r
361 isb\r
362 ldr x30, [sp], #0x10\r
363 ret\r
364\r
365\r
366ASM_PFX(ArmDataMemoryBarrier):\r
367 dmb sy\r
368 ret\r
369\r
370\r
cf93a378 371ASM_PFX(ArmDataSynchronizationBarrier):\r
25402f5d
HL
372 dsb sy\r
373 ret\r
374\r
375\r
376ASM_PFX(ArmInstructionSynchronizationBarrier):\r
377 isb\r
378 ret\r
379\r
380\r
381ASM_PFX(ArmWriteVBar):\r
382 EL1_OR_EL2_OR_EL3(x1)\r
3831: msr vbar_el1, x0 // Set the Address of the EL1 Vector Table in the VBAR register\r
384 b 4f\r
3852: msr vbar_el2, x0 // Set the Address of the EL2 Vector Table in the VBAR register\r
386 b 4f\r
3873: msr vbar_el3, x0 // Set the Address of the EL3 Vector Table in the VBAR register\r
3884: isb\r
389 ret\r
390\r
f0247796
OM
391ASM_PFX(ArmReadVBar):\r
392 EL1_OR_EL2_OR_EL3(x1)\r
3931: mrs x0, vbar_el1 // Set the Address of the EL1 Vector Table in the VBAR register\r
394 ret\r
3952: mrs x0, vbar_el2 // Set the Address of the EL2 Vector Table in the VBAR register\r
396 ret\r
3973: mrs x0, vbar_el3 // Set the Address of the EL3 Vector Table in the VBAR register\r
398 ret\r
399\r
400\r
25402f5d
HL
401ASM_PFX(ArmEnableVFP):\r
402 // Check whether floating-point is implemented in the processor.\r
403 mov x1, x30 // Save LR\r
404 bl ArmReadIdPfr0 // Read EL1 Processor Feature Register (PFR0)\r
405 mov x30, x1 // Restore LR\r
406 ands x0, x0, #AARCH64_PFR0_FP// Extract bits indicating VFP implementation\r
407 cmp x0, #0 // VFP is implemented if '0'.\r
408 b.ne 4f // Exit if VFP not implemented.\r
409 // FVP is implemented.\r
410 // Make sure VFP exceptions are not trapped (to any exception level).\r
411 mrs x0, cpacr_el1 // Read EL1 Coprocessor Access Control Register (CPACR)\r
412 orr x0, x0, #CPACR_VFP_BITS // Disable FVP traps to EL1\r
413 msr cpacr_el1, x0 // Write back EL1 Coprocessor Access Control Register (CPACR)\r
414 mov x1, #AARCH64_CPTR_TFP // TFP Bit for trapping VFP Exceptions\r
415 EL1_OR_EL2_OR_EL3(x2)\r
4161:ret // Not configurable in EL1\r
4172:mrs x0, cptr_el2 // Disable VFP traps to EL2\r
418 bic x0, x0, x1\r
419 msr cptr_el2, x0\r
420 ret\r
4213:mrs x0, cptr_el3 // Disable VFP traps to EL3\r
422 bic x0, x0, x1\r
423 msr cptr_el3, x0\r
4244:ret\r
425\r
426\r
427ASM_PFX(ArmCallWFI):\r
428 wfi\r
429 ret\r
430\r
431\r
25402f5d
HL
432ASM_PFX(ArmReadMpidr):\r
433 mrs x0, mpidr_el1 // read EL1 MPIDR\r
434 ret\r
435\r
436\r
437// Keep old function names for C compatibilty for now. Change later?\r
438ASM_PFX(ArmReadTpidrurw):\r
439 mrs x0, tpidr_el0 // read tpidr_el0 (v7 TPIDRURW) -> (v8 TPIDR_EL0)\r
440 ret\r
441\r
442\r
443// Keep old function names for C compatibilty for now. Change later?\r
444ASM_PFX(ArmWriteTpidrurw):\r
445 msr tpidr_el0, x0 // write tpidr_el0 (v7 TPIDRURW) -> (v8 TPIDR_EL0)\r
446 ret\r
447\r
448\r
449// Arch timers are mandatory on AArch64\r
450ASM_PFX(ArmIsArchTimerImplemented):\r
451 mov x0, #1\r
452 ret\r
453\r
454\r
455ASM_PFX(ArmReadIdPfr0):\r
456 mrs x0, id_aa64pfr0_el1 // Read ID_AA64PFR0 Register\r
457 ret\r
458\r
459\r
460// Q: id_aa64pfr1_el1 not defined yet. What does this funtion want to access?\r
461// A: used to setup arch timer. Check if we have security extensions, permissions to set stuff.\r
462// See: ArmPkg/Library/ArmArchTimerLib/AArch64/ArmArchTimerLib.c\r
463// Not defined yet, but stick in here for now, should read all zeros.\r
464ASM_PFX(ArmReadIdPfr1):\r
465 mrs x0, id_aa64pfr1_el1 // Read ID_PFR1 Register\r
466 ret\r
467\r
468// VOID ArmWriteHcr(UINTN Hcr)\r
469ASM_PFX(ArmWriteHcr):\r
470 msr hcr_el2, x0 // Write the passed HCR value\r
471 ret\r
472\r
473// UINTN ArmReadCurrentEL(VOID)\r
474ASM_PFX(ArmReadCurrentEL):\r
475 mrs x0, CurrentEL\r
476 ret\r
477\r
25402f5d 478ASM_FUNCTION_REMOVE_IF_UNREFERENCED\r