2 * File managing the MMU for ARMv8 architecture
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.
17 #include <Chipset/AArch64.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/ArmLib.h>
21 #include <Library/BaseLib.h>
22 #include <Library/DebugLib.h>
23 #include "AArch64Lib.h"
24 #include "ArmLibPrivate.h"
26 // We use this index definition to define an invalid block entry
27 #define TT_ATTR_INDX_INVALID ((UINT32)~0)
29 INT32 HaveMmuRoutines
= 1;
33 ArmMemoryAttributeToPageAttribute (
34 IN ARM_MEMORY_REGION_ATTRIBUTES Attributes
38 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
:
39 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK
:
40 return TT_ATTR_INDX_MEMORY_WRITE_BACK
| TT_SH_INNER_SHAREABLE
;
42 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
:
43 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH
:
44 return TT_ATTR_INDX_MEMORY_WRITE_THROUGH
| TT_SH_INNER_SHAREABLE
;
46 // Uncached and device mappings are treated as outer shareable by default,
47 case ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
:
48 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED
:
49 return TT_ATTR_INDX_MEMORY_NON_CACHEABLE
;
53 case ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
:
54 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_DEVICE
:
55 if (ArmReadCurrentEL () == AARCH64_EL2
)
56 return TT_ATTR_INDX_DEVICE_MEMORY
| TT_XN_MASK
;
58 return TT_ATTR_INDX_DEVICE_MEMORY
| TT_UXN_MASK
| TT_PXN_MASK
;
63 PageAttributeToGcdAttribute (
64 IN UINT64 PageAttributes
69 switch (PageAttributes
& TT_ATTR_INDX_MASK
) {
70 case TT_ATTR_INDX_DEVICE_MEMORY
:
71 GcdAttributes
= EFI_MEMORY_UC
;
73 case TT_ATTR_INDX_MEMORY_NON_CACHEABLE
:
74 GcdAttributes
= EFI_MEMORY_WC
;
76 case TT_ATTR_INDX_MEMORY_WRITE_THROUGH
:
77 GcdAttributes
= EFI_MEMORY_WT
;
79 case TT_ATTR_INDX_MEMORY_WRITE_BACK
:
80 GcdAttributes
= EFI_MEMORY_WB
;
83 DEBUG ((EFI_D_ERROR
, "PageAttributeToGcdAttribute: PageAttributes:0x%lX not supported.\n", PageAttributes
));
85 // The Global Coherency Domain (GCD) value is defined as a bit set.
86 // Returning 0 means no attribute has been set.
90 // Determine protection attributes
91 if (((PageAttributes
& TT_AP_MASK
) == TT_AP_NO_RO
) || ((PageAttributes
& TT_AP_MASK
) == TT_AP_RO_RO
)) {
92 // Read only cases map to write-protect
93 GcdAttributes
|= EFI_MEMORY_WP
;
96 // Process eXecute Never attribute
97 if ((PageAttributes
& (TT_PXN_MASK
| TT_UXN_MASK
)) != 0 ) {
98 GcdAttributes
|= EFI_MEMORY_XP
;
101 return GcdAttributes
;
104 ARM_MEMORY_REGION_ATTRIBUTES
105 GcdAttributeToArmAttribute (
106 IN UINT64 GcdAttributes
109 switch (GcdAttributes
& 0xFF) {
111 return ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
;
113 return ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
;
115 return ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
;
117 return ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
;
119 DEBUG ((EFI_D_ERROR
, "GcdAttributeToArmAttribute: 0x%lX attributes is not supported.\n", GcdAttributes
));
121 return ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
;
125 // Describe the T0SZ values for each translation table level
129 UINTN LargestT0SZ
; // Generally (MaxT0SZ == LargestT0SZ) but at the Level3 Table
130 // the MaxT0SZ is not at the boundary of the table
131 } T0SZ_DESCRIPTION_PER_LEVEL
;
133 // Map table for the corresponding Level of Table
134 STATIC CONST T0SZ_DESCRIPTION_PER_LEVEL T0SZPerTableLevel
[] = {
135 { 16, 24, 24 }, // Table Level 0
136 { 25, 33, 33 }, // Table Level 1
137 { 34, 39, 42 } // Table Level 2
141 GetRootTranslationTableInfo (
143 OUT UINTN
*TableLevel
,
144 OUT UINTN
*TableEntryCount
149 // Identify the level of the root table from the given T0SZ
150 for (Index
= 0; Index
< sizeof (T0SZPerTableLevel
) / sizeof (T0SZ_DESCRIPTION_PER_LEVEL
); Index
++) {
151 if (T0SZ
<= T0SZPerTableLevel
[Index
].MaxT0SZ
) {
156 // If we have not found the corresponding maximum T0SZ then we use the last one
157 if (Index
== sizeof (T0SZPerTableLevel
) / sizeof (T0SZ_DESCRIPTION_PER_LEVEL
)) {
161 // Get the level of the root table
166 // The Size of the Table is 2^(T0SZ-LargestT0SZ)
167 if (TableEntryCount
) {
168 *TableEntryCount
= 1 << (T0SZPerTableLevel
[Index
].LargestT0SZ
- T0SZ
+ 1);
179 if (!ArmMmuEnabled ()) {
182 ArmReplaceLiveTranslationEntry (Entry
, Value
);
188 LookupAddresstoRootTable (
189 IN UINT64 MaxAddress
,
191 OUT UINTN
*TableEntryCount
196 // Check the parameters are not NULL
197 ASSERT ((T0SZ
!= NULL
) && (TableEntryCount
!= NULL
));
199 // Look for the highest bit set in MaxAddress
200 for (TopBit
= 63; TopBit
!= 0; TopBit
--) {
201 if ((1ULL << TopBit
) & MaxAddress
) {
202 // MaxAddress top bit is found
207 ASSERT (TopBit
!= 0);
209 // Calculate T0SZ from the top bit of the MaxAddress
212 // Get the Table info from T0SZ
213 GetRootTranslationTableInfo (*T0SZ
, NULL
, TableEntryCount
);
218 GetBlockEntryListFromAddress (
219 IN UINT64
*RootTable
,
220 IN UINT64 RegionStart
,
221 OUT UINTN
*TableLevel
,
222 IN OUT UINT64
*BlockEntrySize
,
223 OUT UINT64
**LastBlockEntry
226 UINTN RootTableLevel
;
227 UINTN RootTableEntryCount
;
228 UINT64
*TranslationTable
;
230 UINT64
*SubTableBlockEntry
;
231 UINT64 BlockEntryAddress
;
232 UINTN BaseAddressAlignment
;
238 UINT64 TableAttributes
;
240 // Initialize variable
243 // Ensure the parameters are valid
244 if (!(TableLevel
&& BlockEntrySize
&& LastBlockEntry
)) {
245 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
249 // Ensure the Region is aligned on 4KB boundary
250 if ((RegionStart
& (SIZE_4KB
- 1)) != 0) {
251 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
255 // Ensure the required size is aligned on 4KB boundary and not 0
256 if ((*BlockEntrySize
& (SIZE_4KB
- 1)) != 0 || *BlockEntrySize
== 0) {
257 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
261 T0SZ
= ArmGetTCR () & TCR_T0SZ_MASK
;
262 // Get the Table info from T0SZ
263 GetRootTranslationTableInfo (T0SZ
, &RootTableLevel
, &RootTableEntryCount
);
265 // If the start address is 0x0 then we use the size of the region to identify the alignment
266 if (RegionStart
== 0) {
267 // Identify the highest possible alignment for the Region Size
268 BaseAddressAlignment
= LowBitSet64 (*BlockEntrySize
);
270 // Identify the highest possible alignment for the Base Address
271 BaseAddressAlignment
= LowBitSet64 (RegionStart
);
274 // Identify the Page Level the RegionStart must belong to. Note that PageLevel
275 // should be at least 1 since block translations are not supported at level 0
276 PageLevel
= MAX (3 - ((BaseAddressAlignment
- 12) / 9), 1);
278 // If the required size is smaller than the current block size then we need to go to the page below.
279 // The PageLevel was calculated on the Base Address alignment but did not take in account the alignment
280 // of the allocation size
281 while (*BlockEntrySize
< TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel
)) {
282 // It does not fit so we need to go a page level above
287 // Get the Table Descriptor for the corresponding PageLevel. We need to decompose RegionStart to get appropriate entries
290 TranslationTable
= RootTable
;
291 for (IndexLevel
= RootTableLevel
; IndexLevel
<= PageLevel
; IndexLevel
++) {
292 BlockEntry
= (UINT64
*)TT_GET_ENTRY_FOR_ADDRESS (TranslationTable
, IndexLevel
, RegionStart
);
294 if ((IndexLevel
!= 3) && ((*BlockEntry
& TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
)) {
295 // Go to the next table
296 TranslationTable
= (UINT64
*)(*BlockEntry
& TT_ADDRESS_MASK_DESCRIPTION_TABLE
);
298 // If we are at the last level then update the last level to next level
299 if (IndexLevel
== PageLevel
) {
300 // Enter the next level
303 } else if ((*BlockEntry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY
) {
304 // If we are not at the last level then we need to split this BlockEntry
305 if (IndexLevel
!= PageLevel
) {
306 // Retrieve the attributes from the block entry
307 Attributes
= *BlockEntry
& TT_ATTRIBUTES_MASK
;
309 // Convert the block entry attributes into Table descriptor attributes
310 TableAttributes
= TT_TABLE_AP_NO_PERMISSION
;
311 if (Attributes
& TT_NS
) {
312 TableAttributes
= TT_TABLE_NS
;
315 // Get the address corresponding at this entry
316 BlockEntryAddress
= RegionStart
;
317 BlockEntryAddress
= BlockEntryAddress
>> TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel
);
318 // Shift back to right to set zero before the effective address
319 BlockEntryAddress
= BlockEntryAddress
<< TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel
);
321 // Set the correct entry type for the next page level
322 if ((IndexLevel
+ 1) == 3) {
323 Attributes
|= TT_TYPE_BLOCK_ENTRY_LEVEL3
;
325 Attributes
|= TT_TYPE_BLOCK_ENTRY
;
328 // Create a new translation table
329 TranslationTable
= (UINT64
*)AllocateAlignedPages (EFI_SIZE_TO_PAGES(TT_ENTRY_COUNT
* sizeof(UINT64
)), TT_ALIGNMENT_DESCRIPTION_TABLE
);
330 if (TranslationTable
== NULL
) {
334 // Populate the newly created lower level table
335 SubTableBlockEntry
= TranslationTable
;
336 for (Index
= 0; Index
< TT_ENTRY_COUNT
; Index
++) {
337 *SubTableBlockEntry
= Attributes
| (BlockEntryAddress
+ (Index
<< TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel
+ 1)));
338 SubTableBlockEntry
++;
341 // Fill the BlockEntry with the new TranslationTable
342 ReplaceLiveEntry (BlockEntry
,
343 ((UINTN
)TranslationTable
& TT_ADDRESS_MASK_DESCRIPTION_TABLE
) | TableAttributes
| TT_TYPE_TABLE_ENTRY
);
346 if (IndexLevel
!= PageLevel
) {
348 // Case when we have an Invalid Entry and we are at a page level above of the one targetted.
351 // Create a new translation table
352 TranslationTable
= (UINT64
*)AllocateAlignedPages (EFI_SIZE_TO_PAGES(TT_ENTRY_COUNT
* sizeof(UINT64
)), TT_ALIGNMENT_DESCRIPTION_TABLE
);
353 if (TranslationTable
== NULL
) {
357 ZeroMem (TranslationTable
, TT_ENTRY_COUNT
* sizeof(UINT64
));
359 // Fill the new BlockEntry with the TranslationTable
360 *BlockEntry
= ((UINTN
)TranslationTable
& TT_ADDRESS_MASK_DESCRIPTION_TABLE
) | TT_TYPE_TABLE_ENTRY
;
365 // Expose the found PageLevel to the caller
366 *TableLevel
= PageLevel
;
368 // Now, we have the Table Level we can get the Block Size associated to this table
369 *BlockEntrySize
= TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel
);
371 // The last block of the root table depends on the number of entry in this table,
372 // otherwise it is always the (TT_ENTRY_COUNT - 1)th entry in the table.
373 *LastBlockEntry
= TT_LAST_BLOCK_ADDRESS(TranslationTable
,
374 (PageLevel
== RootTableLevel
) ? RootTableEntryCount
: TT_ENTRY_COUNT
);
381 UpdateRegionMapping (
382 IN UINT64
*RootTable
,
383 IN UINT64 RegionStart
,
384 IN UINT64 RegionLength
,
385 IN UINT64 Attributes
,
386 IN UINT64 BlockEntryMask
391 UINT64
*LastBlockEntry
;
392 UINT64 BlockEntrySize
;
395 // Ensure the Length is aligned on 4KB boundary
396 if ((RegionLength
== 0) || ((RegionLength
& (SIZE_4KB
- 1)) != 0)) {
397 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
398 return RETURN_INVALID_PARAMETER
;
402 // Get the first Block Entry that matches the Virtual Address and also the information on the Table Descriptor
403 // such as the the size of the Block Entry and the address of the last BlockEntry of the Table Descriptor
404 BlockEntrySize
= RegionLength
;
405 BlockEntry
= GetBlockEntryListFromAddress (RootTable
, RegionStart
, &TableLevel
, &BlockEntrySize
, &LastBlockEntry
);
406 if (BlockEntry
== NULL
) {
407 // GetBlockEntryListFromAddress() return NULL when it fails to allocate new pages from the Translation Tables
408 return RETURN_OUT_OF_RESOURCES
;
411 if (TableLevel
!= 3) {
412 Type
= TT_TYPE_BLOCK_ENTRY
;
414 Type
= TT_TYPE_BLOCK_ENTRY_LEVEL3
;
418 // Fill the Block Entry with attribute and output block address
419 *BlockEntry
&= BlockEntryMask
;
420 *BlockEntry
|= (RegionStart
& TT_ADDRESS_MASK_BLOCK_ENTRY
) | Attributes
| Type
;
422 // Go to the next BlockEntry
423 RegionStart
+= BlockEntrySize
;
424 RegionLength
-= BlockEntrySize
;
427 // Break the inner loop when next block is a table
428 // Rerun GetBlockEntryListFromAddress to avoid page table memory leak
429 if (TableLevel
!= 3 &&
430 (*BlockEntry
& TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
) {
433 } while ((RegionLength
>= BlockEntrySize
) && (BlockEntry
<= LastBlockEntry
));
434 } while (RegionLength
!= 0);
436 return RETURN_SUCCESS
;
441 FillTranslationTable (
442 IN UINT64
*RootTable
,
443 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryRegion
446 return UpdateRegionMapping (
448 MemoryRegion
->VirtualBase
,
449 MemoryRegion
->Length
,
450 ArmMemoryAttributeToPageAttribute (MemoryRegion
->Attributes
) | TT_AF
,
456 SetMemoryAttributes (
457 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
459 IN UINT64 Attributes
,
460 IN EFI_PHYSICAL_ADDRESS VirtualMask
463 RETURN_STATUS Status
;
464 ARM_MEMORY_REGION_DESCRIPTOR MemoryRegion
;
465 UINT64
*TranslationTable
;
467 MemoryRegion
.PhysicalBase
= BaseAddress
;
468 MemoryRegion
.VirtualBase
= BaseAddress
;
469 MemoryRegion
.Length
= Length
;
470 MemoryRegion
.Attributes
= GcdAttributeToArmAttribute (Attributes
);
472 TranslationTable
= ArmGetTTBR0BaseAddress ();
474 Status
= FillTranslationTable (TranslationTable
, &MemoryRegion
);
475 if (RETURN_ERROR (Status
)) {
479 // Invalidate all TLB entries so changes are synced
482 return RETURN_SUCCESS
;
487 SetMemoryRegionAttribute (
488 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
490 IN UINT64 Attributes
,
491 IN UINT64 BlockEntryMask
494 RETURN_STATUS Status
;
497 RootTable
= ArmGetTTBR0BaseAddress ();
499 Status
= UpdateRegionMapping (RootTable
, BaseAddress
, Length
, Attributes
, BlockEntryMask
);
500 if (RETURN_ERROR (Status
)) {
504 // Invalidate all TLB entries so changes are synced
507 return RETURN_SUCCESS
;
511 ArmSetMemoryRegionNoExec (
512 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
518 if (ArmReadCurrentEL () == AARCH64_EL1
) {
519 Val
= TT_PXN_MASK
| TT_UXN_MASK
;
524 return SetMemoryRegionAttribute (
528 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
532 ArmClearMemoryRegionNoExec (
533 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
539 // XN maps to UXN in the EL1&0 translation regime
540 Mask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_PXN_MASK
| TT_XN_MASK
);
542 return SetMemoryRegionAttribute (
550 ArmSetMemoryRegionReadOnly (
551 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
555 return SetMemoryRegionAttribute (
559 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
563 ArmClearMemoryRegionReadOnly (
564 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
568 return SetMemoryRegionAttribute (
572 ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
));
578 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTable
,
579 OUT VOID
**TranslationTableBase OPTIONAL
,
580 OUT UINTN
*TranslationTableSize OPTIONAL
583 VOID
* TranslationTable
;
584 UINTN TranslationTablePageCount
;
585 UINT32 TranslationTableAttribute
;
586 ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTableEntry
;
590 UINTN RootTableEntryCount
;
592 RETURN_STATUS Status
;
594 if(MemoryTable
== NULL
) {
595 ASSERT (MemoryTable
!= NULL
);
596 return RETURN_INVALID_PARAMETER
;
599 // Identify the highest address of the memory table
600 MaxAddress
= MemoryTable
->PhysicalBase
+ MemoryTable
->Length
- 1;
601 MemoryTableEntry
= MemoryTable
;
602 while (MemoryTableEntry
->Length
!= 0) {
603 TopAddress
= MemoryTableEntry
->PhysicalBase
+ MemoryTableEntry
->Length
- 1;
604 if (TopAddress
> MaxAddress
) {
605 MaxAddress
= TopAddress
;
610 // Lookup the Table Level to get the information
611 LookupAddresstoRootTable (MaxAddress
, &T0SZ
, &RootTableEntryCount
);
614 // Set TCR that allows us to retrieve T0SZ in the subsequent functions
616 // Ideally we will be running at EL2, but should support EL1 as well.
617 // UEFI should not run at EL3.
618 if (ArmReadCurrentEL () == AARCH64_EL2
) {
619 //Note: Bits 23 and 31 are reserved(RES1) bits in TCR_EL2
620 TCR
= T0SZ
| (1UL << 31) | (1UL << 23) | TCR_TG0_4KB
;
622 // Set the Physical Address Size using MaxAddress
623 if (MaxAddress
< SIZE_4GB
) {
625 } else if (MaxAddress
< SIZE_64GB
) {
627 } else if (MaxAddress
< SIZE_1TB
) {
629 } else if (MaxAddress
< SIZE_4TB
) {
631 } else if (MaxAddress
< SIZE_16TB
) {
633 } else if (MaxAddress
< SIZE_256TB
) {
636 DEBUG ((EFI_D_ERROR
, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress
));
637 ASSERT (0); // Bigger than 48-bit memory space are not supported
638 return RETURN_UNSUPPORTED
;
640 } else if (ArmReadCurrentEL () == AARCH64_EL1
) {
641 // Due to Cortex-A57 erratum #822227 we must set TG1[1] == 1, regardless of EPD1.
642 TCR
= T0SZ
| TCR_TG0_4KB
| TCR_TG1_4KB
| TCR_EPD1
;
644 // Set the Physical Address Size using MaxAddress
645 if (MaxAddress
< SIZE_4GB
) {
647 } else if (MaxAddress
< SIZE_64GB
) {
649 } else if (MaxAddress
< SIZE_1TB
) {
651 } else if (MaxAddress
< SIZE_4TB
) {
653 } else if (MaxAddress
< SIZE_16TB
) {
655 } else if (MaxAddress
< SIZE_256TB
) {
656 TCR
|= TCR_IPS_256TB
;
658 DEBUG ((EFI_D_ERROR
, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress
));
659 ASSERT (0); // Bigger than 48-bit memory space are not supported
660 return RETURN_UNSUPPORTED
;
663 ASSERT (0); // UEFI is only expected to run at EL2 and EL1, not EL3.
664 return RETURN_UNSUPPORTED
;
670 // Allocate pages for translation table
671 TranslationTablePageCount
= EFI_SIZE_TO_PAGES(RootTableEntryCount
* sizeof(UINT64
));
672 TranslationTable
= (UINT64
*)AllocateAlignedPages (TranslationTablePageCount
, TT_ALIGNMENT_DESCRIPTION_TABLE
);
673 if (TranslationTable
== NULL
) {
674 return RETURN_OUT_OF_RESOURCES
;
676 // We set TTBR0 just after allocating the table to retrieve its location from the subsequent
677 // functions without needing to pass this value across the functions. The MMU is only enabled
678 // after the translation tables are populated.
679 ArmSetTTBR0 (TranslationTable
);
681 if (TranslationTableBase
!= NULL
) {
682 *TranslationTableBase
= TranslationTable
;
685 if (TranslationTableSize
!= NULL
) {
686 *TranslationTableSize
= RootTableEntryCount
* sizeof(UINT64
);
689 ZeroMem (TranslationTable
, RootTableEntryCount
* sizeof(UINT64
));
691 // Disable MMU and caches. ArmDisableMmu() also invalidates the TLBs
693 ArmDisableDataCache ();
694 ArmDisableInstructionCache ();
696 // Make sure nothing sneaked into the cache
697 ArmCleanInvalidateDataCache ();
698 ArmInvalidateInstructionCache ();
700 TranslationTableAttribute
= TT_ATTR_INDX_INVALID
;
701 while (MemoryTable
->Length
!= 0) {
702 // Find the memory attribute for the Translation Table
703 if (((UINTN
)TranslationTable
>= MemoryTable
->PhysicalBase
) &&
704 ((UINTN
)TranslationTable
<= MemoryTable
->PhysicalBase
- 1 + MemoryTable
->Length
)) {
705 TranslationTableAttribute
= MemoryTable
->Attributes
;
708 Status
= FillTranslationTable (TranslationTable
, MemoryTable
);
709 if (RETURN_ERROR (Status
)) {
710 goto FREE_TRANSLATION_TABLE
;
715 // Translate the Memory Attributes into Translation Table Register Attributes
716 if ((TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
) ||
717 (TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED
)) {
718 TCR
|= TCR_SH_NON_SHAREABLE
| TCR_RGN_OUTER_NON_CACHEABLE
| TCR_RGN_INNER_NON_CACHEABLE
;
719 } else if ((TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
) ||
720 (TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK
)) {
721 TCR
|= TCR_SH_INNER_SHAREABLE
| TCR_RGN_OUTER_WRITE_BACK_ALLOC
| TCR_RGN_INNER_WRITE_BACK_ALLOC
;
722 } else if ((TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
) ||
723 (TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH
)) {
724 TCR
|= TCR_SH_NON_SHAREABLE
| TCR_RGN_OUTER_WRITE_THROUGH
| TCR_RGN_INNER_WRITE_THROUGH
;
726 // If we failed to find a mapping that contains the root translation table then it probably means the translation table
727 // is not mapped in the given memory map.
729 Status
= RETURN_UNSUPPORTED
;
730 goto FREE_TRANSLATION_TABLE
;
733 // Set again TCR after getting the Translation Table attributes
736 ArmSetMAIR (MAIR_ATTR(TT_ATTR_INDX_DEVICE_MEMORY
, MAIR_ATTR_DEVICE_MEMORY
) | // mapped to EFI_MEMORY_UC
737 MAIR_ATTR(TT_ATTR_INDX_MEMORY_NON_CACHEABLE
, MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
) | // mapped to EFI_MEMORY_WC
738 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_THROUGH
, MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
) | // mapped to EFI_MEMORY_WT
739 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_BACK
, MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
)); // mapped to EFI_MEMORY_WB
741 ArmDisableAlignmentCheck ();
742 ArmEnableInstructionCache ();
743 ArmEnableDataCache ();
746 return RETURN_SUCCESS
;
748 FREE_TRANSLATION_TABLE
:
749 FreePages (TranslationTable
, TranslationTablePageCount
);