]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmLib/AArch64/AArch64Support.S
ArmPkg/ArmV7Lib: add support for reading the ID_MMFR0 system register
[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
25GCC_ASM_EXPORT (ArmCleanInvalidateDataCacheEntryByMVA)\r
26GCC_ASM_EXPORT (ArmInvalidateDataCacheEntryBySetWay)\r
27GCC_ASM_EXPORT (ArmCleanDataCacheEntryBySetWay)\r
28GCC_ASM_EXPORT (ArmCleanInvalidateDataCacheEntryBySetWay)\r
29GCC_ASM_EXPORT (ArmDrainWriteBuffer)\r
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
76ASM_PFX(ArmCleanInvalidateDataCacheEntryByMVA):\r
77 dc civac, x0 // Clean and invalidate single data cache line\r
25402f5d
HL
78 ret\r
79\r
80\r
81ASM_PFX(ArmInvalidateDataCacheEntryBySetWay):\r
82 dc isw, x0 // Invalidate this line\r
25402f5d
HL
83 ret\r
84\r
85\r
86ASM_PFX(ArmCleanInvalidateDataCacheEntryBySetWay):\r
87 dc cisw, x0 // Clean and Invalidate this line\r
25402f5d
HL
88 ret\r
89\r
90\r
91ASM_PFX(ArmCleanDataCacheEntryBySetWay):\r
92 dc csw, x0 // Clean this line\r
25402f5d
HL
93 ret\r
94\r
95\r
96ASM_PFX(ArmInvalidateInstructionCache):\r
97 ic iallu // Invalidate entire instruction cache\r
98 dsb sy\r
99 isb\r
100 ret\r
101\r
102\r
103ASM_PFX(ArmEnableMmu):\r
104 EL1_OR_EL2_OR_EL3(x1)\r
1051: mrs x0, sctlr_el1 // Read System control register EL1\r
106 b 4f\r
1072: mrs x0, sctlr_el2 // Read System control register EL2\r
108 b 4f\r
1093: mrs x0, sctlr_el3 // Read System control register EL3\r
1104: orr x0, x0, #CTRL_M_BIT // Set MMU enable bit\r
111 EL1_OR_EL2_OR_EL3(x1)\r
70f89c0b 1121: tlbi vmalle1\r
ee95f9e1 113 dsb nsh\r
25402f5d
HL
114 isb\r
115 msr sctlr_el1, x0 // Write back\r
116 b 4f\r
1172: tlbi alle2\r
ee95f9e1 118 dsb nsh\r
25402f5d
HL
119 isb\r
120 msr sctlr_el2, x0 // Write back\r
121 b 4f\r
1223: tlbi alle3\r
ee95f9e1 123 dsb nsh\r
25402f5d
HL
124 isb\r
125 msr sctlr_el3, x0 // Write back\r
ee95f9e1 1264: isb\r
25402f5d
HL
127 ret\r
128\r
129\r
130ASM_PFX(ArmDisableMmu):\r
131 EL1_OR_EL2_OR_EL3(x1)\r
1321: mrs x0, sctlr_el1 // Read System Control Register EL1\r
133 b 4f\r
1342: mrs x0, sctlr_el2 // Read System Control Register EL2\r
135 b 4f\r
1363: mrs x0, sctlr_el3 // Read System Control Register EL3\r
73ca5009 1374: and x0, x0, #~CTRL_M_BIT // Clear MMU enable bit\r
25402f5d
HL
138 EL1_OR_EL2_OR_EL3(x1)\r
1391: msr sctlr_el1, x0 // Write back\r
70f89c0b 140 tlbi vmalle1\r
25402f5d
HL
141 b 4f\r
1422: msr sctlr_el2, x0 // Write back\r
143 tlbi alle2\r
144 b 4f\r
1453: msr sctlr_el3, x0 // Write back\r
146 tlbi alle3\r
1474: dsb sy\r
148 isb\r
149 ret\r
150\r
151\r
152ASM_PFX(ArmDisableCachesAndMmu):\r
153 EL1_OR_EL2_OR_EL3(x1)\r
1541: mrs x0, sctlr_el1 // Get control register EL1\r
155 b 4f\r
1562: mrs x0, sctlr_el2 // Get control register EL2\r
157 b 4f\r
1583: mrs x0, sctlr_el3 // Get control register EL3\r
73ca5009
BJ
1594: mov x1, #~(CTRL_M_BIT | CTRL_C_BIT | CTRL_I_BIT) // Disable MMU, D & I caches\r
160 and x0, x0, x1\r
25402f5d
HL
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
172ASM_PFX(ArmMmuEnabled):\r
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
1794: and x0, x0, #CTRL_M_BIT\r
180 ret\r
181\r
182\r
183ASM_PFX(ArmEnableDataCache):\r
184 EL1_OR_EL2_OR_EL3(x1)\r
1851: mrs x0, sctlr_el1 // Get control register EL1\r
186 b 4f\r
1872: mrs x0, sctlr_el2 // Get control register EL2\r
188 b 4f\r
1893: mrs x0, sctlr_el3 // Get control register EL3\r
1904: orr x0, x0, #CTRL_C_BIT // Set C bit\r
191 EL1_OR_EL2_OR_EL3(x1)\r
1921: msr sctlr_el1, x0 // Write back control register\r
193 b 4f\r
1942: msr sctlr_el2, x0 // Write back control register\r
195 b 4f\r
1963: msr sctlr_el3, x0 // Write back control register\r
1974: dsb sy\r
198 isb\r
199 ret\r
200\r
201\r
202ASM_PFX(ArmDisableDataCache):\r
203 EL1_OR_EL2_OR_EL3(x1)\r
2041: mrs x0, sctlr_el1 // Get control register EL1\r
205 b 4f\r
2062: mrs x0, sctlr_el2 // Get control register EL2\r
207 b 4f\r
2083: mrs x0, sctlr_el3 // Get control register EL3\r
73ca5009 2094: and x0, x0, #~CTRL_C_BIT // Clear C bit\r
25402f5d
HL
210 EL1_OR_EL2_OR_EL3(x1)\r
2111: msr sctlr_el1, x0 // Write back control register\r
212 b 4f\r
2132: msr sctlr_el2, x0 // Write back control register\r
214 b 4f\r
2153: msr sctlr_el3, x0 // Write back control register\r
2164: dsb sy\r
217 isb\r
218 ret\r
219\r
220\r
221ASM_PFX(ArmEnableInstructionCache):\r
222 EL1_OR_EL2_OR_EL3(x1)\r
2231: mrs x0, sctlr_el1 // Get control register EL1\r
224 b 4f\r
2252: mrs x0, sctlr_el2 // Get control register EL2\r
226 b 4f\r
2273: mrs x0, sctlr_el3 // Get control register EL3\r
2284: orr x0, x0, #CTRL_I_BIT // Set I bit\r
229 EL1_OR_EL2_OR_EL3(x1)\r
2301: msr sctlr_el1, x0 // Write back control register\r
231 b 4f\r
2322: msr sctlr_el2, x0 // Write back control register\r
233 b 4f\r
2343: msr sctlr_el3, x0 // Write back control register\r
2354: dsb sy\r
236 isb\r
237 ret\r
238\r
239\r
240ASM_PFX(ArmDisableInstructionCache):\r
241 EL1_OR_EL2_OR_EL3(x1)\r
2421: mrs x0, sctlr_el1 // Get control register EL1\r
243 b 4f\r
2442: mrs x0, sctlr_el2 // Get control register EL2\r
245 b 4f\r
2463: mrs x0, sctlr_el3 // Get control register EL3\r
73ca5009 2474: and x0, x0, #~CTRL_I_BIT // Clear I bit\r
25402f5d
HL
248 EL1_OR_EL2_OR_EL3(x1)\r
2491: msr sctlr_el1, x0 // Write back control register\r
250 b 4f\r
2512: msr sctlr_el2, x0 // Write back control register\r
252 b 4f\r
2533: msr sctlr_el3, x0 // Write back control register\r
2544: dsb sy\r
255 isb\r
256 ret\r
257\r
258\r
259ASM_PFX(ArmEnableAlignmentCheck):\r
260 EL1_OR_EL2(x1)\r
2611: mrs x0, sctlr_el1 // Get control register EL1\r
262 b 3f\r
2632: mrs x0, sctlr_el2 // Get control register EL2\r
2643: orr x0, x0, #CTRL_A_BIT // Set A (alignment check) bit\r
265 EL1_OR_EL2(x1)\r
2661: msr sctlr_el1, x0 // Write back control register\r
267 b 3f\r
2682: msr sctlr_el2, x0 // Write back control register\r
2693: dsb sy\r
270 isb\r
271 ret\r
272\r
273\r
274ASM_PFX(ArmDisableAlignmentCheck):\r
275 EL1_OR_EL2_OR_EL3(x1)\r
2761: mrs x0, sctlr_el1 // Get control register EL1\r
277 b 4f\r
2782: mrs x0, sctlr_el2 // Get control register EL2\r
279 b 4f\r
2803: mrs x0, sctlr_el3 // Get control register EL3\r
73ca5009 2814: and x0, x0, #~CTRL_A_BIT // Clear A (alignment check) bit\r
25402f5d
HL
282 EL1_OR_EL2_OR_EL3(x1)\r
2831: msr sctlr_el1, x0 // Write back control register\r
284 b 4f\r
2852: msr sctlr_el2, x0 // Write back control register\r
286 b 4f\r
2873: msr sctlr_el3, x0 // Write back control register\r
2884: dsb sy\r
289 isb\r
290 ret\r
291\r
292\r
293// Always turned on in AArch64. Else implementation specific. Leave in for C compatibility for now\r
294ASM_PFX(ArmEnableBranchPrediction):\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(ArmDisableBranchPrediction):\r
300 ret\r
301\r
302\r
303ASM_PFX(AArch64AllDataCachesOperation):\r
304// We can use regs 0-7 and 9-15 without having to save/restore.\r
fb7ea611
OM
305// Save our link register on the stack. - The stack must always be quad-word aligned\r
306 str x30, [sp, #-16]!\r
25402f5d
HL
307 mov x1, x0 // Save Function call in x1\r
308 mrs x6, clidr_el1 // Read EL1 CLIDR\r
309 and x3, x6, #0x7000000 // Mask out all but Level of Coherency (LoC)\r
433a49a0
OM
310 lsr x3, x3, #23 // Left align cache level value - the level is shifted by 1 to the\r
311 // right to ease the access to CSSELR and the Set/Way operation.\r
25402f5d
HL
312 cbz x3, L_Finished // No need to clean if LoC is 0\r
313 mov x10, #0 // Start clean at cache level 0\r
25402f5d
HL
314\r
315Loop1:\r
316 add x2, x10, x10, lsr #1 // Work out 3x cachelevel for cache info\r
317 lsr x12, x6, x2 // bottom 3 bits are the Cache type for this level\r
318 and x12, x12, #7 // get those 3 bits alone\r
319 cmp x12, #2 // what cache at this level?\r
320 b.lt L_Skip // no cache or only instruction cache at this level\r
321 msr csselr_el1, x10 // write the Cache Size selection register with current level (CSSELR)\r
322 isb // isb to sync the change to the CacheSizeID reg\r
323 mrs x12, ccsidr_el1 // reads current Cache Size ID register (CCSIDR)\r
324 and x2, x12, #0x7 // extract the line length field\r
325 add x2, x2, #4 // add 4 for the line length offset (log2 16 bytes)\r
326 mov x4, #0x400\r
327 sub x4, x4, #1\r
328 and x4, x4, x12, lsr #3 // x4 is the max number on the way size (right aligned)\r
329 clz w5, w4 // w5 is the bit position of the way size increment\r
330 mov x7, #0x00008000\r
331 sub x7, x7, #1\r
332 and x7, x7, x12, lsr #13 // x7 is the max number of the index size (right aligned)\r
333\r
334Loop2:\r
335 mov x9, x4 // x9 working copy of the max way size (right aligned)\r
336\r
337Loop3:\r
338 lsl x11, x9, x5\r
339 orr x0, x10, x11 // factor in the way number and cache number\r
340 lsl x11, x7, x2\r
341 orr x0, x0, x11 // factor in the index number\r
342\r
343 blr x1 // Goto requested cache operation\r
344\r
345 subs x9, x9, #1 // decrement the way number\r
346 b.ge Loop3\r
347 subs x7, x7, #1 // decrement the index\r
348 b.ge Loop2\r
349L_Skip:\r
350 add x10, x10, #2 // increment the cache number\r
351 cmp x3, x10\r
352 b.gt Loop1\r
353\r
354L_Finished:\r
355 dsb sy\r
356 isb\r
357 ldr x30, [sp], #0x10\r
358 ret\r
359\r
360\r
361ASM_PFX(ArmDataMemoryBarrier):\r
362 dmb sy\r
363 ret\r
364\r
365\r
cf93a378 366ASM_PFX(ArmDataSynchronizationBarrier):\r
25402f5d
HL
367ASM_PFX(ArmDrainWriteBuffer):\r
368 dsb sy\r
369 ret\r
370\r
371\r
372ASM_PFX(ArmInstructionSynchronizationBarrier):\r
373 isb\r
374 ret\r
375\r
376\r
377ASM_PFX(ArmWriteVBar):\r
378 EL1_OR_EL2_OR_EL3(x1)\r
3791: msr vbar_el1, x0 // Set the Address of the EL1 Vector Table in the VBAR register\r
380 b 4f\r
3812: msr vbar_el2, x0 // Set the Address of the EL2 Vector Table in the VBAR register\r
382 b 4f\r
3833: msr vbar_el3, x0 // Set the Address of the EL3 Vector Table in the VBAR register\r
3844: isb\r
385 ret\r
386\r
f0247796
OM
387ASM_PFX(ArmReadVBar):\r
388 EL1_OR_EL2_OR_EL3(x1)\r
3891: mrs x0, vbar_el1 // Set the Address of the EL1 Vector Table in the VBAR register\r
390 ret\r
3912: mrs x0, vbar_el2 // Set the Address of the EL2 Vector Table in the VBAR register\r
392 ret\r
3933: mrs x0, vbar_el3 // Set the Address of the EL3 Vector Table in the VBAR register\r
394 ret\r
395\r
396\r
25402f5d
HL
397ASM_PFX(ArmEnableVFP):\r
398 // Check whether floating-point is implemented in the processor.\r
399 mov x1, x30 // Save LR\r
400 bl ArmReadIdPfr0 // Read EL1 Processor Feature Register (PFR0)\r
401 mov x30, x1 // Restore LR\r
402 ands x0, x0, #AARCH64_PFR0_FP// Extract bits indicating VFP implementation\r
403 cmp x0, #0 // VFP is implemented if '0'.\r
404 b.ne 4f // Exit if VFP not implemented.\r
405 // FVP is implemented.\r
406 // Make sure VFP exceptions are not trapped (to any exception level).\r
407 mrs x0, cpacr_el1 // Read EL1 Coprocessor Access Control Register (CPACR)\r
408 orr x0, x0, #CPACR_VFP_BITS // Disable FVP traps to EL1\r
409 msr cpacr_el1, x0 // Write back EL1 Coprocessor Access Control Register (CPACR)\r
410 mov x1, #AARCH64_CPTR_TFP // TFP Bit for trapping VFP Exceptions\r
411 EL1_OR_EL2_OR_EL3(x2)\r
4121:ret // Not configurable in EL1\r
4132:mrs x0, cptr_el2 // Disable VFP traps to EL2\r
414 bic x0, x0, x1\r
415 msr cptr_el2, x0\r
416 ret\r
4173:mrs x0, cptr_el3 // Disable VFP traps to EL3\r
418 bic x0, x0, x1\r
419 msr cptr_el3, x0\r
4204:ret\r
421\r
422\r
423ASM_PFX(ArmCallWFI):\r
424 wfi\r
425 ret\r
426\r
427\r
25402f5d
HL
428ASM_PFX(ArmReadMpidr):\r
429 mrs x0, mpidr_el1 // read EL1 MPIDR\r
430 ret\r
431\r
432\r
433// Keep old function names for C compatibilty for now. Change later?\r
434ASM_PFX(ArmReadTpidrurw):\r
435 mrs x0, tpidr_el0 // read tpidr_el0 (v7 TPIDRURW) -> (v8 TPIDR_EL0)\r
436 ret\r
437\r
438\r
439// Keep old function names for C compatibilty for now. Change later?\r
440ASM_PFX(ArmWriteTpidrurw):\r
441 msr tpidr_el0, x0 // write tpidr_el0 (v7 TPIDRURW) -> (v8 TPIDR_EL0)\r
442 ret\r
443\r
444\r
445// Arch timers are mandatory on AArch64\r
446ASM_PFX(ArmIsArchTimerImplemented):\r
447 mov x0, #1\r
448 ret\r
449\r
450\r
451ASM_PFX(ArmReadIdPfr0):\r
452 mrs x0, id_aa64pfr0_el1 // Read ID_AA64PFR0 Register\r
453 ret\r
454\r
455\r
456// Q: id_aa64pfr1_el1 not defined yet. What does this funtion want to access?\r
457// A: used to setup arch timer. Check if we have security extensions, permissions to set stuff.\r
458// See: ArmPkg/Library/ArmArchTimerLib/AArch64/ArmArchTimerLib.c\r
459// Not defined yet, but stick in here for now, should read all zeros.\r
460ASM_PFX(ArmReadIdPfr1):\r
461 mrs x0, id_aa64pfr1_el1 // Read ID_PFR1 Register\r
462 ret\r
463\r
464// VOID ArmWriteHcr(UINTN Hcr)\r
465ASM_PFX(ArmWriteHcr):\r
466 msr hcr_el2, x0 // Write the passed HCR value\r
467 ret\r
468\r
469// UINTN ArmReadCurrentEL(VOID)\r
470ASM_PFX(ArmReadCurrentEL):\r
471 mrs x0, CurrentEL\r
472 ret\r
473\r
25402f5d 474ASM_FUNCTION_REMOVE_IF_UNREFERENCED\r