1 #------------------------------------------------------------------------------
3 # Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
4 # Copyright (c) 2011 - 2017, ARM Limited. All rights reserved.
5 # Copyright (c) 2016, Linaro Limited. All rights reserved.
7 # SPDX-License-Identifier: BSD-2-Clause-Patent
9 #------------------------------------------------------------------------------
11 #include <Chipset/AArch64.h>
12 #include <AsmMacroIoLibV8.h>
14 .set CTRL_M_BIT, (1 << 0)
15 .set CTRL_A_BIT, (1 << 1)
16 .set CTRL_C_BIT, (1 << 2)
17 .set CTRL_SA_BIT, (1 << 3)
18 .set CTRL_I_BIT, (1 << 12)
19 .set CTRL_V_BIT, (1 << 12)
20 .set CPACR_VFP_BITS, (3 << 20)
22 ASM_FUNC(ArmInvalidateDataCacheEntryByMVA)
23 dc ivac, x0 // Invalidate single data cache line
27 ASM_FUNC(ArmCleanDataCacheEntryByMVA)
28 dc cvac, x0 // Clean single data cache line
32 ASM_FUNC(ArmCleanDataCacheEntryToPoUByMVA)
33 dc cvau, x0 // Clean single data cache line to PoU
36 ASM_FUNC(ArmInvalidateInstructionCacheEntryToPoUByMVA)
37 ic ivau, x0 // Invalidate single instruction cache line to PoU
41 ASM_FUNC(ArmCleanInvalidateDataCacheEntryByMVA)
42 dc civac, x0 // Clean and invalidate single data cache line
46 ASM_FUNC(ArmInvalidateDataCacheEntryBySetWay)
47 dc isw, x0 // Invalidate this line
51 ASM_FUNC(ArmCleanInvalidateDataCacheEntryBySetWay)
52 dc cisw, x0 // Clean and Invalidate this line
56 ASM_FUNC(ArmCleanDataCacheEntryBySetWay)
57 dc csw, x0 // Clean this line
61 ASM_FUNC(ArmInvalidateInstructionCache)
62 ic iallu // Invalidate entire instruction cache
68 ASM_FUNC(ArmEnableMmu)
70 1: mrs x0, sctlr_el1 // Read System control register EL1
72 2: mrs x0, sctlr_el2 // Read System control register EL2
74 3: mrs x0, sctlr_el3 // Read System control register EL3
75 4: orr x0, x0, #CTRL_M_BIT // Set MMU enable bit
80 msr sctlr_el1, x0 // Write back
85 msr sctlr_el2, x0 // Write back
90 msr sctlr_el3, x0 // Write back
95 ASM_FUNC(ArmDisableMmu)
97 1: mrs x0, sctlr_el1 // Read System Control Register EL1
99 2: mrs x0, sctlr_el2 // Read System Control Register EL2
101 3: mrs x0, sctlr_el3 // Read System Control Register EL3
102 4: and x0, x0, #~CTRL_M_BIT // Clear MMU enable bit
103 EL1_OR_EL2_OR_EL3(x1)
104 1: msr sctlr_el1, x0 // Write back
107 2: msr sctlr_el2, x0 // Write back
110 3: msr sctlr_el3, x0 // Write back
117 ASM_FUNC(ArmDisableCachesAndMmu)
118 EL1_OR_EL2_OR_EL3(x1)
119 1: mrs x0, sctlr_el1 // Get control register EL1
121 2: mrs x0, sctlr_el2 // Get control register EL2
123 3: mrs x0, sctlr_el3 // Get control register EL3
124 4: mov x1, #~(CTRL_M_BIT | CTRL_C_BIT | CTRL_I_BIT) // Disable MMU, D & I caches
126 EL1_OR_EL2_OR_EL3(x1)
127 1: msr sctlr_el1, x0 // Write back control register
129 2: msr sctlr_el2, x0 // Write back control register
131 3: msr sctlr_el3, x0 // Write back control register
137 ASM_FUNC(ArmMmuEnabled)
138 EL1_OR_EL2_OR_EL3(x1)
139 1: mrs x0, sctlr_el1 // Get control register EL1
141 2: mrs x0, sctlr_el2 // Get control register EL2
143 3: mrs x0, sctlr_el3 // Get control register EL3
144 4: and x0, x0, #CTRL_M_BIT
148 ASM_FUNC(ArmEnableDataCache)
149 EL1_OR_EL2_OR_EL3(x1)
150 1: mrs x0, sctlr_el1 // Get control register EL1
152 2: mrs x0, sctlr_el2 // Get control register EL2
154 3: mrs x0, sctlr_el3 // Get control register EL3
155 4: orr x0, x0, #CTRL_C_BIT // Set C bit
156 EL1_OR_EL2_OR_EL3(x1)
157 1: msr sctlr_el1, x0 // Write back control register
159 2: msr sctlr_el2, x0 // Write back control register
161 3: msr sctlr_el3, x0 // Write back control register
167 ASM_FUNC(ArmDisableDataCache)
168 EL1_OR_EL2_OR_EL3(x1)
169 1: mrs x0, sctlr_el1 // Get control register EL1
171 2: mrs x0, sctlr_el2 // Get control register EL2
173 3: mrs x0, sctlr_el3 // Get control register EL3
174 4: and x0, x0, #~CTRL_C_BIT // Clear C bit
175 EL1_OR_EL2_OR_EL3(x1)
176 1: msr sctlr_el1, x0 // Write back control register
178 2: msr sctlr_el2, x0 // Write back control register
180 3: msr sctlr_el3, x0 // Write back control register
186 ASM_FUNC(ArmEnableInstructionCache)
187 EL1_OR_EL2_OR_EL3(x1)
188 1: mrs x0, sctlr_el1 // Get control register EL1
190 2: mrs x0, sctlr_el2 // Get control register EL2
192 3: mrs x0, sctlr_el3 // Get control register EL3
193 4: orr x0, x0, #CTRL_I_BIT // Set I bit
194 EL1_OR_EL2_OR_EL3(x1)
195 1: msr sctlr_el1, x0 // Write back control register
197 2: msr sctlr_el2, x0 // Write back control register
199 3: msr sctlr_el3, x0 // Write back control register
205 ASM_FUNC(ArmDisableInstructionCache)
206 EL1_OR_EL2_OR_EL3(x1)
207 1: mrs x0, sctlr_el1 // Get control register EL1
209 2: mrs x0, sctlr_el2 // Get control register EL2
211 3: mrs x0, sctlr_el3 // Get control register EL3
212 4: and x0, x0, #~CTRL_I_BIT // Clear I bit
213 EL1_OR_EL2_OR_EL3(x1)
214 1: msr sctlr_el1, x0 // Write back control register
216 2: msr sctlr_el2, x0 // Write back control register
218 3: msr sctlr_el3, x0 // Write back control register
224 ASM_FUNC(ArmEnableAlignmentCheck)
226 1: mrs x0, sctlr_el1 // Get control register EL1
228 2: mrs x0, sctlr_el2 // Get control register EL2
229 3: orr x0, x0, #CTRL_A_BIT // Set A (alignment check) bit
231 1: msr sctlr_el1, x0 // Write back control register
233 2: msr sctlr_el2, x0 // Write back control register
239 ASM_FUNC(ArmDisableAlignmentCheck)
240 EL1_OR_EL2_OR_EL3(x1)
241 1: mrs x0, sctlr_el1 // Get control register EL1
243 2: mrs x0, sctlr_el2 // Get control register EL2
245 3: mrs x0, sctlr_el3 // Get control register EL3
246 4: and x0, x0, #~CTRL_A_BIT // Clear A (alignment check) bit
247 EL1_OR_EL2_OR_EL3(x1)
248 1: msr sctlr_el1, x0 // Write back control register
250 2: msr sctlr_el2, x0 // Write back control register
252 3: msr sctlr_el3, x0 // Write back control register
257 ASM_FUNC(ArmEnableStackAlignmentCheck)
259 1: mrs x0, sctlr_el1 // Get control register EL1
261 2: mrs x0, sctlr_el2 // Get control register EL2
262 3: orr x0, x0, #CTRL_SA_BIT // Set SA (stack alignment check) bit
264 1: msr sctlr_el1, x0 // Write back control register
266 2: msr sctlr_el2, x0 // Write back control register
272 ASM_FUNC(ArmDisableStackAlignmentCheck)
273 EL1_OR_EL2_OR_EL3(x1)
274 1: mrs x0, sctlr_el1 // Get control register EL1
276 2: mrs x0, sctlr_el2 // Get control register EL2
278 3: mrs x0, sctlr_el3 // Get control register EL3
279 4: bic x0, x0, #CTRL_SA_BIT // Clear SA (stack alignment check) bit
280 EL1_OR_EL2_OR_EL3(x1)
281 1: msr sctlr_el1, x0 // Write back control register
283 2: msr sctlr_el2, x0 // Write back control register
285 3: msr sctlr_el3, x0 // Write back control register
291 // Always turned on in AArch64. Else implementation specific. Leave in for C compatibility for now
292 ASM_FUNC(ArmEnableBranchPrediction)
296 // Always turned on in AArch64. Else implementation specific. Leave in for C compatibility for now.
297 ASM_FUNC(ArmDisableBranchPrediction)
301 ASM_FUNC(AArch64AllDataCachesOperation)
302 // We can use regs 0-7 and 9-15 without having to save/restore.
303 // Save our link register on the stack. - The stack must always be quad-word aligned
304 stp x29, x30, [sp, #-16]!
306 mov x1, x0 // Save Function call in x1
307 mrs x6, clidr_el1 // Read EL1 CLIDR
308 and x3, x6, #0x7000000 // Mask out all but Level of Coherency (LoC)
309 lsr x3, x3, #23 // Left align cache level value - the level is shifted by 1 to the
310 // right to ease the access to CSSELR and the Set/Way operation.
311 cbz x3, L_Finished // No need to clean if LoC is 0
312 mov x10, #0 // Start clean at cache level 0
315 add x2, x10, x10, lsr #1 // Work out 3x cachelevel for cache info
316 lsr x12, x6, x2 // bottom 3 bits are the Cache type for this level
317 and x12, x12, #7 // get those 3 bits alone
318 cmp x12, #2 // what cache at this level?
319 b.lt L_Skip // no cache or only instruction cache at this level
320 msr csselr_el1, x10 // write the Cache Size selection register with current level (CSSELR)
321 isb // isb to sync the change to the CacheSizeID reg
322 mrs x12, ccsidr_el1 // reads current Cache Size ID register (CCSIDR)
323 and x2, x12, #0x7 // extract the line length field
324 add x2, x2, #4 // add 4 for the line length offset (log2 16 bytes)
327 and x4, x4, x12, lsr #3 // x4 is the max number on the way size (right aligned)
328 clz w5, w4 // w5 is the bit position of the way size increment
331 and x7, x7, x12, lsr #13 // x7 is the max number of the index size (right aligned)
334 mov x9, x4 // x9 working copy of the max way size (right aligned)
338 orr x0, x10, x11 // factor in the way number and cache number
340 orr x0, x0, x11 // factor in the index number
342 blr x1 // Goto requested cache operation
344 subs x9, x9, #1 // decrement the way number
346 subs x7, x7, #1 // decrement the index
349 add x10, x10, #2 // increment the cache number
356 ldp x29, x30, [sp], #0x10
360 ASM_FUNC(ArmDataMemoryBarrier)
365 ASM_FUNC(ArmDataSynchronizationBarrier)
370 ASM_FUNC(ArmInstructionSynchronizationBarrier)
375 ASM_FUNC(ArmWriteVBar)
376 EL1_OR_EL2_OR_EL3(x1)
377 1: msr vbar_el1, x0 // Set the Address of the EL1 Vector Table in the VBAR register
379 2: msr vbar_el2, x0 // Set the Address of the EL2 Vector Table in the VBAR register
381 3: msr vbar_el3, x0 // Set the Address of the EL3 Vector Table in the VBAR register
385 ASM_FUNC(ArmReadVBar)
386 EL1_OR_EL2_OR_EL3(x1)
387 1: mrs x0, vbar_el1 // Set the Address of the EL1 Vector Table in the VBAR register
389 2: mrs x0, vbar_el2 // Set the Address of the EL2 Vector Table in the VBAR register
391 3: mrs x0, vbar_el3 // Set the Address of the EL3 Vector Table in the VBAR register
395 ASM_FUNC(ArmEnableVFP)
396 // Check whether floating-point is implemented in the processor.
397 mov x1, x30 // Save LR
398 bl ArmReadIdPfr0 // Read EL1 Processor Feature Register (PFR0)
399 mov x30, x1 // Restore LR
400 ubfx x0, x0, #16, #4 // Extract the FP bits 16:19
401 cmp x0, #0xF // Check if FP bits are '1111b',
402 // i.e. Floating Point not implemented
403 b.eq 4f // Exit when VFP is not implemented.
405 // FVP is implemented.
406 // Make sure VFP exceptions are not trapped (to any exception level).
407 mrs x0, cpacr_el1 // Read EL1 Coprocessor Access Control Register (CPACR)
408 orr x0, x0, #CPACR_VFP_BITS // Disable FVP traps to EL1
409 msr cpacr_el1, x0 // Write back EL1 Coprocessor Access Control Register (CPACR)
410 mov x1, #AARCH64_CPTR_TFP // TFP Bit for trapping VFP Exceptions
411 EL1_OR_EL2_OR_EL3(x2)
412 1:ret // Not configurable in EL1
413 2:mrs x0, cptr_el2 // Disable VFP traps to EL2
417 3:mrs x0, cptr_el3 // Disable VFP traps to EL3
428 ASM_FUNC(ArmReadMpidr)
429 mrs x0, mpidr_el1 // read EL1 MPIDR
433 // Keep old function names for C compatibilty for now. Change later?
434 ASM_FUNC(ArmReadTpidrurw)
435 mrs x0, tpidr_el0 // read tpidr_el0 (v7 TPIDRURW) -> (v8 TPIDR_EL0)
439 // Keep old function names for C compatibilty for now. Change later?
440 ASM_FUNC(ArmWriteTpidrurw)
441 msr tpidr_el0, x0 // write tpidr_el0 (v7 TPIDRURW) -> (v8 TPIDR_EL0)
445 // Arch timers are mandatory on AArch64
446 ASM_FUNC(ArmIsArchTimerImplemented)
451 ASM_FUNC(ArmReadIdPfr0)
452 mrs x0, id_aa64pfr0_el1 // Read ID_AA64PFR0 Register
456 // Q: id_aa64pfr1_el1 not defined yet. What does this funtion want to access?
457 // A: used to setup arch timer. Check if we have security extensions, permissions to set stuff.
458 // See: ArmPkg/Library/ArmArchTimerLib/AArch64/ArmArchTimerLib.c
459 // Not defined yet, but stick in here for now, should read all zeros.
460 ASM_FUNC(ArmReadIdPfr1)
461 mrs x0, id_aa64pfr1_el1 // Read ID_PFR1 Register
464 // VOID ArmWriteHcr(UINTN Hcr)
465 ASM_FUNC(ArmWriteHcr)
466 msr hcr_el2, x0 // Write the passed HCR value
469 // UINTN ArmReadHcr(VOID)
474 // UINTN ArmReadCurrentEL(VOID)
475 ASM_FUNC(ArmReadCurrentEL)
479 // UINT32 ArmReadCntHctl(VOID)
480 ASM_FUNC(ArmReadCntHctl)
484 // VOID ArmWriteCntHctl(UINT32 CntHctl)
485 ASM_FUNC(ArmWriteCntHctl)
489 ASM_FUNCTION_REMOVE_IF_UNREFERENCED