1 #------------------------------------------------------------------------------
3 # Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
4 # Copyright (c) 2011 - 2014, ARM Limited. All rights reserved.
6 # This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #------------------------------------------------------------------------------
16 #include <Chipset/AArch64.h>
17 #include <AsmMacroIoLibV8.h>
22 GCC_ASM_EXPORT (ArmInvalidateInstructionCache)
23 GCC_ASM_EXPORT (ArmInvalidateDataCacheEntryByMVA)
24 GCC_ASM_EXPORT (ArmCleanDataCacheEntryByMVA)
25 GCC_ASM_EXPORT (ArmCleanDataCacheEntryToPoUByMVA)
26 GCC_ASM_EXPORT (ArmCleanInvalidateDataCacheEntryByMVA)
27 GCC_ASM_EXPORT (ArmInvalidateDataCacheEntryBySetWay)
28 GCC_ASM_EXPORT (ArmCleanDataCacheEntryBySetWay)
29 GCC_ASM_EXPORT (ArmCleanInvalidateDataCacheEntryBySetWay)
30 GCC_ASM_EXPORT (ArmEnableMmu)
31 GCC_ASM_EXPORT (ArmDisableMmu)
32 GCC_ASM_EXPORT (ArmDisableCachesAndMmu)
33 GCC_ASM_EXPORT (ArmMmuEnabled)
34 GCC_ASM_EXPORT (ArmEnableDataCache)
35 GCC_ASM_EXPORT (ArmDisableDataCache)
36 GCC_ASM_EXPORT (ArmEnableInstructionCache)
37 GCC_ASM_EXPORT (ArmDisableInstructionCache)
38 GCC_ASM_EXPORT (ArmDisableAlignmentCheck)
39 GCC_ASM_EXPORT (ArmEnableAlignmentCheck)
40 GCC_ASM_EXPORT (ArmEnableBranchPrediction)
41 GCC_ASM_EXPORT (ArmDisableBranchPrediction)
42 GCC_ASM_EXPORT (AArch64AllDataCachesOperation)
43 GCC_ASM_EXPORT (ArmDataMemoryBarrier)
44 GCC_ASM_EXPORT (ArmDataSynchronizationBarrier)
45 GCC_ASM_EXPORT (ArmInstructionSynchronizationBarrier)
46 GCC_ASM_EXPORT (ArmWriteVBar)
47 GCC_ASM_EXPORT (ArmReadVBar)
48 GCC_ASM_EXPORT (ArmEnableVFP)
49 GCC_ASM_EXPORT (ArmCallWFI)
50 GCC_ASM_EXPORT (ArmReadMpidr)
51 GCC_ASM_EXPORT (ArmReadTpidrurw)
52 GCC_ASM_EXPORT (ArmWriteTpidrurw)
53 GCC_ASM_EXPORT (ArmIsArchTimerImplemented)
54 GCC_ASM_EXPORT (ArmReadIdPfr0)
55 GCC_ASM_EXPORT (ArmReadIdPfr1)
56 GCC_ASM_EXPORT (ArmWriteHcr)
57 GCC_ASM_EXPORT (ArmReadHcr)
58 GCC_ASM_EXPORT (ArmReadCurrentEL)
60 .set CTRL_M_BIT, (1 << 0)
61 .set CTRL_A_BIT, (1 << 1)
62 .set CTRL_C_BIT, (1 << 2)
63 .set CTRL_I_BIT, (1 << 12)
64 .set CTRL_V_BIT, (1 << 12)
65 .set CPACR_VFP_BITS, (3 << 20)
67 ASM_PFX(ArmInvalidateDataCacheEntryByMVA):
68 dc ivac, x0 // Invalidate single data cache line
72 ASM_PFX(ArmCleanDataCacheEntryByMVA):
73 dc cvac, x0 // Clean single data cache line
77 ASM_PFX(ArmCleanDataCacheEntryToPoUByMVA):
78 dc cvau, x0 // Clean single data cache line to PoU
82 ASM_PFX(ArmCleanInvalidateDataCacheEntryByMVA):
83 dc civac, x0 // Clean and invalidate single data cache line
87 ASM_PFX(ArmInvalidateDataCacheEntryBySetWay):
88 dc isw, x0 // Invalidate this line
92 ASM_PFX(ArmCleanInvalidateDataCacheEntryBySetWay):
93 dc cisw, x0 // Clean and Invalidate this line
97 ASM_PFX(ArmCleanDataCacheEntryBySetWay):
98 dc csw, x0 // Clean this line
102 ASM_PFX(ArmInvalidateInstructionCache):
103 ic iallu // Invalidate entire instruction cache
109 ASM_PFX(ArmEnableMmu):
110 EL1_OR_EL2_OR_EL3(x1)
111 1: mrs x0, sctlr_el1 // Read System control register EL1
113 2: mrs x0, sctlr_el2 // Read System control register EL2
115 3: mrs x0, sctlr_el3 // Read System control register EL3
116 4: orr x0, x0, #CTRL_M_BIT // Set MMU enable bit
117 EL1_OR_EL2_OR_EL3(x1)
121 msr sctlr_el1, x0 // Write back
126 msr sctlr_el2, x0 // Write back
131 msr sctlr_el3, x0 // Write back
136 ASM_PFX(ArmDisableMmu):
137 EL1_OR_EL2_OR_EL3(x1)
138 1: mrs x0, sctlr_el1 // Read System Control Register EL1
140 2: mrs x0, sctlr_el2 // Read System Control Register EL2
142 3: mrs x0, sctlr_el3 // Read System Control Register EL3
143 4: and x0, x0, #~CTRL_M_BIT // Clear MMU enable bit
144 EL1_OR_EL2_OR_EL3(x1)
145 1: msr sctlr_el1, x0 // Write back
148 2: msr sctlr_el2, x0 // Write back
151 3: msr sctlr_el3, x0 // Write back
158 ASM_PFX(ArmDisableCachesAndMmu):
159 EL1_OR_EL2_OR_EL3(x1)
160 1: mrs x0, sctlr_el1 // Get control register EL1
162 2: mrs x0, sctlr_el2 // Get control register EL2
164 3: mrs x0, sctlr_el3 // Get control register EL3
165 4: mov x1, #~(CTRL_M_BIT | CTRL_C_BIT | CTRL_I_BIT) // Disable MMU, D & I caches
167 EL1_OR_EL2_OR_EL3(x1)
168 1: msr sctlr_el1, x0 // Write back control register
170 2: msr sctlr_el2, x0 // Write back control register
172 3: msr sctlr_el3, x0 // Write back control register
178 ASM_PFX(ArmMmuEnabled):
179 EL1_OR_EL2_OR_EL3(x1)
180 1: mrs x0, sctlr_el1 // Get control register EL1
182 2: mrs x0, sctlr_el2 // Get control register EL2
184 3: mrs x0, sctlr_el3 // Get control register EL3
185 4: and x0, x0, #CTRL_M_BIT
189 ASM_PFX(ArmEnableDataCache):
190 EL1_OR_EL2_OR_EL3(x1)
191 1: mrs x0, sctlr_el1 // Get control register EL1
193 2: mrs x0, sctlr_el2 // Get control register EL2
195 3: mrs x0, sctlr_el3 // Get control register EL3
196 4: orr x0, x0, #CTRL_C_BIT // Set C bit
197 EL1_OR_EL2_OR_EL3(x1)
198 1: msr sctlr_el1, x0 // Write back control register
200 2: msr sctlr_el2, x0 // Write back control register
202 3: msr sctlr_el3, x0 // Write back control register
208 ASM_PFX(ArmDisableDataCache):
209 EL1_OR_EL2_OR_EL3(x1)
210 1: mrs x0, sctlr_el1 // Get control register EL1
212 2: mrs x0, sctlr_el2 // Get control register EL2
214 3: mrs x0, sctlr_el3 // Get control register EL3
215 4: and x0, x0, #~CTRL_C_BIT // Clear C bit
216 EL1_OR_EL2_OR_EL3(x1)
217 1: msr sctlr_el1, x0 // Write back control register
219 2: msr sctlr_el2, x0 // Write back control register
221 3: msr sctlr_el3, x0 // Write back control register
227 ASM_PFX(ArmEnableInstructionCache):
228 EL1_OR_EL2_OR_EL3(x1)
229 1: mrs x0, sctlr_el1 // Get control register EL1
231 2: mrs x0, sctlr_el2 // Get control register EL2
233 3: mrs x0, sctlr_el3 // Get control register EL3
234 4: orr x0, x0, #CTRL_I_BIT // Set I bit
235 EL1_OR_EL2_OR_EL3(x1)
236 1: msr sctlr_el1, x0 // Write back control register
238 2: msr sctlr_el2, x0 // Write back control register
240 3: msr sctlr_el3, x0 // Write back control register
246 ASM_PFX(ArmDisableInstructionCache):
247 EL1_OR_EL2_OR_EL3(x1)
248 1: mrs x0, sctlr_el1 // Get control register EL1
250 2: mrs x0, sctlr_el2 // Get control register EL2
252 3: mrs x0, sctlr_el3 // Get control register EL3
253 4: and x0, x0, #~CTRL_I_BIT // Clear I bit
254 EL1_OR_EL2_OR_EL3(x1)
255 1: msr sctlr_el1, x0 // Write back control register
257 2: msr sctlr_el2, x0 // Write back control register
259 3: msr sctlr_el3, x0 // Write back control register
265 ASM_PFX(ArmEnableAlignmentCheck):
267 1: mrs x0, sctlr_el1 // Get control register EL1
269 2: mrs x0, sctlr_el2 // Get control register EL2
270 3: orr x0, x0, #CTRL_A_BIT // Set A (alignment check) bit
272 1: msr sctlr_el1, x0 // Write back control register
274 2: msr sctlr_el2, x0 // Write back control register
280 ASM_PFX(ArmDisableAlignmentCheck):
281 EL1_OR_EL2_OR_EL3(x1)
282 1: mrs x0, sctlr_el1 // Get control register EL1
284 2: mrs x0, sctlr_el2 // Get control register EL2
286 3: mrs x0, sctlr_el3 // Get control register EL3
287 4: and x0, x0, #~CTRL_A_BIT // Clear A (alignment check) bit
288 EL1_OR_EL2_OR_EL3(x1)
289 1: msr sctlr_el1, x0 // Write back control register
291 2: msr sctlr_el2, x0 // Write back control register
293 3: msr sctlr_el3, x0 // Write back control register
299 // Always turned on in AArch64. Else implementation specific. Leave in for C compatibility for now
300 ASM_PFX(ArmEnableBranchPrediction):
304 // Always turned on in AArch64. Else implementation specific. Leave in for C compatibility for now.
305 ASM_PFX(ArmDisableBranchPrediction):
309 ASM_PFX(AArch64AllDataCachesOperation):
310 // We can use regs 0-7 and 9-15 without having to save/restore.
311 // Save our link register on the stack. - The stack must always be quad-word aligned
313 mov x1, x0 // Save Function call in x1
314 mrs x6, clidr_el1 // Read EL1 CLIDR
315 and x3, x6, #0x7000000 // Mask out all but Level of Coherency (LoC)
316 lsr x3, x3, #23 // Left align cache level value - the level is shifted by 1 to the
317 // right to ease the access to CSSELR and the Set/Way operation.
318 cbz x3, L_Finished // No need to clean if LoC is 0
319 mov x10, #0 // Start clean at cache level 0
322 add x2, x10, x10, lsr #1 // Work out 3x cachelevel for cache info
323 lsr x12, x6, x2 // bottom 3 bits are the Cache type for this level
324 and x12, x12, #7 // get those 3 bits alone
325 cmp x12, #2 // what cache at this level?
326 b.lt L_Skip // no cache or only instruction cache at this level
327 msr csselr_el1, x10 // write the Cache Size selection register with current level (CSSELR)
328 isb // isb to sync the change to the CacheSizeID reg
329 mrs x12, ccsidr_el1 // reads current Cache Size ID register (CCSIDR)
330 and x2, x12, #0x7 // extract the line length field
331 add x2, x2, #4 // add 4 for the line length offset (log2 16 bytes)
334 and x4, x4, x12, lsr #3 // x4 is the max number on the way size (right aligned)
335 clz w5, w4 // w5 is the bit position of the way size increment
338 and x7, x7, x12, lsr #13 // x7 is the max number of the index size (right aligned)
341 mov x9, x4 // x9 working copy of the max way size (right aligned)
345 orr x0, x10, x11 // factor in the way number and cache number
347 orr x0, x0, x11 // factor in the index number
349 blr x1 // Goto requested cache operation
351 subs x9, x9, #1 // decrement the way number
353 subs x7, x7, #1 // decrement the index
356 add x10, x10, #2 // increment the cache number
367 ASM_PFX(ArmDataMemoryBarrier):
372 ASM_PFX(ArmDataSynchronizationBarrier):
377 ASM_PFX(ArmInstructionSynchronizationBarrier):
382 ASM_PFX(ArmWriteVBar):
383 EL1_OR_EL2_OR_EL3(x1)
384 1: msr vbar_el1, x0 // Set the Address of the EL1 Vector Table in the VBAR register
386 2: msr vbar_el2, x0 // Set the Address of the EL2 Vector Table in the VBAR register
388 3: msr vbar_el3, x0 // Set the Address of the EL3 Vector Table in the VBAR register
392 ASM_PFX(ArmReadVBar):
393 EL1_OR_EL2_OR_EL3(x1)
394 1: mrs x0, vbar_el1 // Set the Address of the EL1 Vector Table in the VBAR register
396 2: mrs x0, vbar_el2 // Set the Address of the EL2 Vector Table in the VBAR register
398 3: mrs x0, vbar_el3 // Set the Address of the EL3 Vector Table in the VBAR register
402 ASM_PFX(ArmEnableVFP):
403 // Check whether floating-point is implemented in the processor.
404 mov x1, x30 // Save LR
405 bl ArmReadIdPfr0 // Read EL1 Processor Feature Register (PFR0)
406 mov x30, x1 // Restore LR
407 ands x0, x0, #AARCH64_PFR0_FP// Extract bits indicating VFP implementation
408 cmp x0, #0 // VFP is implemented if '0'.
409 b.ne 4f // Exit if VFP not implemented.
410 // FVP is implemented.
411 // Make sure VFP exceptions are not trapped (to any exception level).
412 mrs x0, cpacr_el1 // Read EL1 Coprocessor Access Control Register (CPACR)
413 orr x0, x0, #CPACR_VFP_BITS // Disable FVP traps to EL1
414 msr cpacr_el1, x0 // Write back EL1 Coprocessor Access Control Register (CPACR)
415 mov x1, #AARCH64_CPTR_TFP // TFP Bit for trapping VFP Exceptions
416 EL1_OR_EL2_OR_EL3(x2)
417 1:ret // Not configurable in EL1
418 2:mrs x0, cptr_el2 // Disable VFP traps to EL2
422 3:mrs x0, cptr_el3 // Disable VFP traps to EL3
433 ASM_PFX(ArmReadMpidr):
434 mrs x0, mpidr_el1 // read EL1 MPIDR
438 // Keep old function names for C compatibilty for now. Change later?
439 ASM_PFX(ArmReadTpidrurw):
440 mrs x0, tpidr_el0 // read tpidr_el0 (v7 TPIDRURW) -> (v8 TPIDR_EL0)
444 // Keep old function names for C compatibilty for now. Change later?
445 ASM_PFX(ArmWriteTpidrurw):
446 msr tpidr_el0, x0 // write tpidr_el0 (v7 TPIDRURW) -> (v8 TPIDR_EL0)
450 // Arch timers are mandatory on AArch64
451 ASM_PFX(ArmIsArchTimerImplemented):
456 ASM_PFX(ArmReadIdPfr0):
457 mrs x0, id_aa64pfr0_el1 // Read ID_AA64PFR0 Register
461 // Q: id_aa64pfr1_el1 not defined yet. What does this funtion want to access?
462 // A: used to setup arch timer. Check if we have security extensions, permissions to set stuff.
463 // See: ArmPkg/Library/ArmArchTimerLib/AArch64/ArmArchTimerLib.c
464 // Not defined yet, but stick in here for now, should read all zeros.
465 ASM_PFX(ArmReadIdPfr1):
466 mrs x0, id_aa64pfr1_el1 // Read ID_PFR1 Register
469 // VOID ArmWriteHcr(UINTN Hcr)
470 ASM_PFX(ArmWriteHcr):
471 msr hcr_el2, x0 // Write the passed HCR value
474 // UINTN ArmReadHcr(VOID)
479 // UINTN ArmReadCurrentEL(VOID)
480 ASM_PFX(ArmReadCurrentEL):
484 ASM_FUNCTION_REMOVE_IF_UNREFERENCED