2 * File managing the MMU for ARMv8 architecture
4 * Copyright (c) 2011-2014, ARM Limited. All rights reserved.
5 * Copyright (c) 2016, Linaro Limited. All rights reserved.
7 * This program and the accompanying materials
8 * are licensed and made available under the terms and conditions of the BSD License
9 * which accompanies this distribution. The full text of the license may be found at
10 * http://opensource.org/licenses/bsd-license.php
12 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Chipset/AArch64.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/CacheMaintenanceLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/ArmLib.h>
23 #include <Library/ArmMmuLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/DebugLib.h>
27 // We use this index definition to define an invalid block entry
28 #define TT_ATTR_INDX_INVALID ((UINT32)~0)
32 ArmMemoryAttributeToPageAttribute (
33 IN ARM_MEMORY_REGION_ATTRIBUTES Attributes
37 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
:
38 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK
:
39 return TT_ATTR_INDX_MEMORY_WRITE_BACK
| TT_SH_INNER_SHAREABLE
;
41 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
:
42 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH
:
43 return TT_ATTR_INDX_MEMORY_WRITE_THROUGH
| TT_SH_INNER_SHAREABLE
;
45 // Uncached and device mappings are treated as outer shareable by default,
46 case ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
:
47 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED
:
48 return TT_ATTR_INDX_MEMORY_NON_CACHEABLE
;
52 case ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
:
53 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_DEVICE
:
54 if (ArmReadCurrentEL () == AARCH64_EL2
)
55 return TT_ATTR_INDX_DEVICE_MEMORY
| TT_XN_MASK
;
57 return TT_ATTR_INDX_DEVICE_MEMORY
| TT_UXN_MASK
| TT_PXN_MASK
;
62 PageAttributeToGcdAttribute (
63 IN UINT64 PageAttributes
68 switch (PageAttributes
& TT_ATTR_INDX_MASK
) {
69 case TT_ATTR_INDX_DEVICE_MEMORY
:
70 GcdAttributes
= EFI_MEMORY_UC
;
72 case TT_ATTR_INDX_MEMORY_NON_CACHEABLE
:
73 GcdAttributes
= EFI_MEMORY_WC
;
75 case TT_ATTR_INDX_MEMORY_WRITE_THROUGH
:
76 GcdAttributes
= EFI_MEMORY_WT
;
78 case TT_ATTR_INDX_MEMORY_WRITE_BACK
:
79 GcdAttributes
= EFI_MEMORY_WB
;
82 DEBUG ((EFI_D_ERROR
, "PageAttributeToGcdAttribute: PageAttributes:0x%lX not supported.\n", PageAttributes
));
84 // The Global Coherency Domain (GCD) value is defined as a bit set.
85 // Returning 0 means no attribute has been set.
89 // Determine protection attributes
90 if (((PageAttributes
& TT_AP_MASK
) == TT_AP_NO_RO
) || ((PageAttributes
& TT_AP_MASK
) == TT_AP_RO_RO
)) {
91 // Read only cases map to write-protect
92 GcdAttributes
|= EFI_MEMORY_WP
;
95 // Process eXecute Never attribute
96 if ((PageAttributes
& (TT_PXN_MASK
| TT_UXN_MASK
)) != 0 ) {
97 GcdAttributes
|= EFI_MEMORY_XP
;
100 return GcdAttributes
;
103 ARM_MEMORY_REGION_ATTRIBUTES
104 GcdAttributeToArmAttribute (
105 IN UINT64 GcdAttributes
108 switch (GcdAttributes
& 0xFF) {
110 return ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
;
112 return ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
;
114 return ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
;
116 return ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
;
118 DEBUG ((EFI_D_ERROR
, "GcdAttributeToArmAttribute: 0x%lX attributes is not supported.\n", GcdAttributes
));
120 return ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
;
124 // Describe the T0SZ values for each translation table level
128 UINTN LargestT0SZ
; // Generally (MaxT0SZ == LargestT0SZ) but at the Level3 Table
129 // the MaxT0SZ is not at the boundary of the table
130 } T0SZ_DESCRIPTION_PER_LEVEL
;
132 // Map table for the corresponding Level of Table
133 STATIC CONST T0SZ_DESCRIPTION_PER_LEVEL T0SZPerTableLevel
[] = {
134 { 16, 24, 24 }, // Table Level 0
135 { 25, 33, 33 }, // Table Level 1
136 { 34, 39, 42 } // Table Level 2
140 GetRootTranslationTableInfo (
142 OUT UINTN
*TableLevel
,
143 OUT UINTN
*TableEntryCount
148 // Identify the level of the root table from the given T0SZ
149 for (Index
= 0; Index
< sizeof (T0SZPerTableLevel
) / sizeof (T0SZ_DESCRIPTION_PER_LEVEL
); Index
++) {
150 if (T0SZ
<= T0SZPerTableLevel
[Index
].MaxT0SZ
) {
155 // If we have not found the corresponding maximum T0SZ then we use the last one
156 if (Index
== sizeof (T0SZPerTableLevel
) / sizeof (T0SZ_DESCRIPTION_PER_LEVEL
)) {
160 // Get the level of the root table
165 // The Size of the Table is 2^(T0SZ-LargestT0SZ)
166 if (TableEntryCount
) {
167 *TableEntryCount
= 1 << (T0SZPerTableLevel
[Index
].LargestT0SZ
- T0SZ
+ 1);
178 if (!ArmMmuEnabled ()) {
181 ArmReplaceLiveTranslationEntry (Entry
, Value
);
187 LookupAddresstoRootTable (
188 IN UINT64 MaxAddress
,
190 OUT UINTN
*TableEntryCount
195 // Check the parameters are not NULL
196 ASSERT ((T0SZ
!= NULL
) && (TableEntryCount
!= NULL
));
198 // Look for the highest bit set in MaxAddress
199 for (TopBit
= 63; TopBit
!= 0; TopBit
--) {
200 if ((1ULL << TopBit
) & MaxAddress
) {
201 // MaxAddress top bit is found
206 ASSERT (TopBit
!= 0);
208 // Calculate T0SZ from the top bit of the MaxAddress
211 // Get the Table info from T0SZ
212 GetRootTranslationTableInfo (*T0SZ
, NULL
, TableEntryCount
);
217 GetBlockEntryListFromAddress (
218 IN UINT64
*RootTable
,
219 IN UINT64 RegionStart
,
220 OUT UINTN
*TableLevel
,
221 IN OUT UINT64
*BlockEntrySize
,
222 OUT UINT64
**LastBlockEntry
225 UINTN RootTableLevel
;
226 UINTN RootTableEntryCount
;
227 UINT64
*TranslationTable
;
229 UINT64
*SubTableBlockEntry
;
230 UINT64 BlockEntryAddress
;
231 UINTN BaseAddressAlignment
;
237 UINT64 TableAttributes
;
239 // Initialize variable
242 // Ensure the parameters are valid
243 if (!(TableLevel
&& BlockEntrySize
&& LastBlockEntry
)) {
244 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
248 // Ensure the Region is aligned on 4KB boundary
249 if ((RegionStart
& (SIZE_4KB
- 1)) != 0) {
250 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
254 // Ensure the required size is aligned on 4KB boundary and not 0
255 if ((*BlockEntrySize
& (SIZE_4KB
- 1)) != 0 || *BlockEntrySize
== 0) {
256 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
260 T0SZ
= ArmGetTCR () & TCR_T0SZ_MASK
;
261 // Get the Table info from T0SZ
262 GetRootTranslationTableInfo (T0SZ
, &RootTableLevel
, &RootTableEntryCount
);
264 // If the start address is 0x0 then we use the size of the region to identify the alignment
265 if (RegionStart
== 0) {
266 // Identify the highest possible alignment for the Region Size
267 BaseAddressAlignment
= LowBitSet64 (*BlockEntrySize
);
269 // Identify the highest possible alignment for the Base Address
270 BaseAddressAlignment
= LowBitSet64 (RegionStart
);
273 // Identify the Page Level the RegionStart must belong to. Note that PageLevel
274 // should be at least 1 since block translations are not supported at level 0
275 PageLevel
= MAX (3 - ((BaseAddressAlignment
- 12) / 9), 1);
277 // If the required size is smaller than the current block size then we need to go to the page below.
278 // The PageLevel was calculated on the Base Address alignment but did not take in account the alignment
279 // of the allocation size
280 while (*BlockEntrySize
< TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel
)) {
281 // It does not fit so we need to go a page level above
286 // Get the Table Descriptor for the corresponding PageLevel. We need to decompose RegionStart to get appropriate entries
289 TranslationTable
= RootTable
;
290 for (IndexLevel
= RootTableLevel
; IndexLevel
<= PageLevel
; IndexLevel
++) {
291 BlockEntry
= (UINT64
*)TT_GET_ENTRY_FOR_ADDRESS (TranslationTable
, IndexLevel
, RegionStart
);
293 if ((IndexLevel
!= 3) && ((*BlockEntry
& TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
)) {
294 // Go to the next table
295 TranslationTable
= (UINT64
*)(*BlockEntry
& TT_ADDRESS_MASK_DESCRIPTION_TABLE
);
297 // If we are at the last level then update the last level to next level
298 if (IndexLevel
== PageLevel
) {
299 // Enter the next level
302 } else if ((*BlockEntry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY
) {
303 // If we are not at the last level then we need to split this BlockEntry
304 if (IndexLevel
!= PageLevel
) {
305 // Retrieve the attributes from the block entry
306 Attributes
= *BlockEntry
& TT_ATTRIBUTES_MASK
;
308 // Convert the block entry attributes into Table descriptor attributes
309 TableAttributes
= TT_TABLE_AP_NO_PERMISSION
;
310 if (Attributes
& TT_NS
) {
311 TableAttributes
= TT_TABLE_NS
;
314 // Get the address corresponding at this entry
315 BlockEntryAddress
= RegionStart
;
316 BlockEntryAddress
= BlockEntryAddress
>> TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel
);
317 // Shift back to right to set zero before the effective address
318 BlockEntryAddress
= BlockEntryAddress
<< TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel
);
320 // Set the correct entry type for the next page level
321 if ((IndexLevel
+ 1) == 3) {
322 Attributes
|= TT_TYPE_BLOCK_ENTRY_LEVEL3
;
324 Attributes
|= TT_TYPE_BLOCK_ENTRY
;
327 // Create a new translation table
328 TranslationTable
= (UINT64
*)AllocateAlignedPages (EFI_SIZE_TO_PAGES(TT_ENTRY_COUNT
* sizeof(UINT64
)), TT_ALIGNMENT_DESCRIPTION_TABLE
);
329 if (TranslationTable
== NULL
) {
333 // Populate the newly created lower level table
334 SubTableBlockEntry
= TranslationTable
;
335 for (Index
= 0; Index
< TT_ENTRY_COUNT
; Index
++) {
336 *SubTableBlockEntry
= Attributes
| (BlockEntryAddress
+ (Index
<< TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel
+ 1)));
337 SubTableBlockEntry
++;
340 // Fill the BlockEntry with the new TranslationTable
341 ReplaceLiveEntry (BlockEntry
,
342 ((UINTN
)TranslationTable
& TT_ADDRESS_MASK_DESCRIPTION_TABLE
) | TableAttributes
| TT_TYPE_TABLE_ENTRY
);
345 if (IndexLevel
!= PageLevel
) {
347 // Case when we have an Invalid Entry and we are at a page level above of the one targetted.
350 // Create a new translation table
351 TranslationTable
= (UINT64
*)AllocateAlignedPages (EFI_SIZE_TO_PAGES(TT_ENTRY_COUNT
* sizeof(UINT64
)), TT_ALIGNMENT_DESCRIPTION_TABLE
);
352 if (TranslationTable
== NULL
) {
356 ZeroMem (TranslationTable
, TT_ENTRY_COUNT
* sizeof(UINT64
));
358 // Fill the new BlockEntry with the TranslationTable
359 *BlockEntry
= ((UINTN
)TranslationTable
& TT_ADDRESS_MASK_DESCRIPTION_TABLE
) | TT_TYPE_TABLE_ENTRY
;
364 // Expose the found PageLevel to the caller
365 *TableLevel
= PageLevel
;
367 // Now, we have the Table Level we can get the Block Size associated to this table
368 *BlockEntrySize
= TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel
);
370 // The last block of the root table depends on the number of entry in this table,
371 // otherwise it is always the (TT_ENTRY_COUNT - 1)th entry in the table.
372 *LastBlockEntry
= TT_LAST_BLOCK_ADDRESS(TranslationTable
,
373 (PageLevel
== RootTableLevel
) ? RootTableEntryCount
: TT_ENTRY_COUNT
);
380 UpdateRegionMapping (
381 IN UINT64
*RootTable
,
382 IN UINT64 RegionStart
,
383 IN UINT64 RegionLength
,
384 IN UINT64 Attributes
,
385 IN UINT64 BlockEntryMask
390 UINT64
*LastBlockEntry
;
391 UINT64 BlockEntrySize
;
394 // Ensure the Length is aligned on 4KB boundary
395 if ((RegionLength
== 0) || ((RegionLength
& (SIZE_4KB
- 1)) != 0)) {
396 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
397 return RETURN_INVALID_PARAMETER
;
401 // Get the first Block Entry that matches the Virtual Address and also the information on the Table Descriptor
402 // such as the the size of the Block Entry and the address of the last BlockEntry of the Table Descriptor
403 BlockEntrySize
= RegionLength
;
404 BlockEntry
= GetBlockEntryListFromAddress (RootTable
, RegionStart
, &TableLevel
, &BlockEntrySize
, &LastBlockEntry
);
405 if (BlockEntry
== NULL
) {
406 // GetBlockEntryListFromAddress() return NULL when it fails to allocate new pages from the Translation Tables
407 return RETURN_OUT_OF_RESOURCES
;
410 if (TableLevel
!= 3) {
411 Type
= TT_TYPE_BLOCK_ENTRY
;
413 Type
= TT_TYPE_BLOCK_ENTRY_LEVEL3
;
417 // Fill the Block Entry with attribute and output block address
418 *BlockEntry
&= BlockEntryMask
;
419 *BlockEntry
|= (RegionStart
& TT_ADDRESS_MASK_BLOCK_ENTRY
) | Attributes
| Type
;
421 // Go to the next BlockEntry
422 RegionStart
+= BlockEntrySize
;
423 RegionLength
-= BlockEntrySize
;
426 // Break the inner loop when next block is a table
427 // Rerun GetBlockEntryListFromAddress to avoid page table memory leak
428 if (TableLevel
!= 3 &&
429 (*BlockEntry
& TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
) {
432 } while ((RegionLength
>= BlockEntrySize
) && (BlockEntry
<= LastBlockEntry
));
433 } while (RegionLength
!= 0);
435 return RETURN_SUCCESS
;
440 FillTranslationTable (
441 IN UINT64
*RootTable
,
442 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryRegion
445 return UpdateRegionMapping (
447 MemoryRegion
->VirtualBase
,
448 MemoryRegion
->Length
,
449 ArmMemoryAttributeToPageAttribute (MemoryRegion
->Attributes
) | TT_AF
,
455 SetMemoryAttributes (
456 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
458 IN UINT64 Attributes
,
459 IN EFI_PHYSICAL_ADDRESS VirtualMask
462 RETURN_STATUS Status
;
463 ARM_MEMORY_REGION_DESCRIPTOR MemoryRegion
;
464 UINT64
*TranslationTable
;
466 MemoryRegion
.PhysicalBase
= BaseAddress
;
467 MemoryRegion
.VirtualBase
= BaseAddress
;
468 MemoryRegion
.Length
= Length
;
469 MemoryRegion
.Attributes
= GcdAttributeToArmAttribute (Attributes
);
471 TranslationTable
= ArmGetTTBR0BaseAddress ();
473 Status
= FillTranslationTable (TranslationTable
, &MemoryRegion
);
474 if (RETURN_ERROR (Status
)) {
478 // Invalidate all TLB entries so changes are synced
481 return RETURN_SUCCESS
;
486 SetMemoryRegionAttribute (
487 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
489 IN UINT64 Attributes
,
490 IN UINT64 BlockEntryMask
493 RETURN_STATUS Status
;
496 RootTable
= ArmGetTTBR0BaseAddress ();
498 Status
= UpdateRegionMapping (RootTable
, BaseAddress
, Length
, Attributes
, BlockEntryMask
);
499 if (RETURN_ERROR (Status
)) {
503 // Invalidate all TLB entries so changes are synced
506 return RETURN_SUCCESS
;
510 ArmSetMemoryRegionNoExec (
511 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
517 if (ArmReadCurrentEL () == AARCH64_EL1
) {
518 Val
= TT_PXN_MASK
| TT_UXN_MASK
;
523 return SetMemoryRegionAttribute (
527 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
531 ArmClearMemoryRegionNoExec (
532 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
538 // XN maps to UXN in the EL1&0 translation regime
539 Mask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_PXN_MASK
| TT_XN_MASK
);
541 return SetMemoryRegionAttribute (
549 ArmSetMemoryRegionReadOnly (
550 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
554 return SetMemoryRegionAttribute (
558 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
562 ArmClearMemoryRegionReadOnly (
563 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
567 return SetMemoryRegionAttribute (
571 ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
));
577 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTable
,
578 OUT VOID
**TranslationTableBase OPTIONAL
,
579 OUT UINTN
*TranslationTableSize OPTIONAL
582 VOID
* TranslationTable
;
583 UINTN TranslationTablePageCount
;
584 UINT32 TranslationTableAttribute
;
585 ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTableEntry
;
589 UINTN RootTableEntryCount
;
591 RETURN_STATUS Status
;
593 if(MemoryTable
== NULL
) {
594 ASSERT (MemoryTable
!= NULL
);
595 return RETURN_INVALID_PARAMETER
;
598 // Identify the highest address of the memory table
599 MaxAddress
= MemoryTable
->PhysicalBase
+ MemoryTable
->Length
- 1;
600 MemoryTableEntry
= MemoryTable
;
601 while (MemoryTableEntry
->Length
!= 0) {
602 TopAddress
= MemoryTableEntry
->PhysicalBase
+ MemoryTableEntry
->Length
- 1;
603 if (TopAddress
> MaxAddress
) {
604 MaxAddress
= TopAddress
;
609 // Lookup the Table Level to get the information
610 LookupAddresstoRootTable (MaxAddress
, &T0SZ
, &RootTableEntryCount
);
613 // Set TCR that allows us to retrieve T0SZ in the subsequent functions
615 // Ideally we will be running at EL2, but should support EL1 as well.
616 // UEFI should not run at EL3.
617 if (ArmReadCurrentEL () == AARCH64_EL2
) {
618 //Note: Bits 23 and 31 are reserved(RES1) bits in TCR_EL2
619 TCR
= T0SZ
| (1UL << 31) | (1UL << 23) | TCR_TG0_4KB
;
621 // Set the Physical Address Size using MaxAddress
622 if (MaxAddress
< SIZE_4GB
) {
624 } else if (MaxAddress
< SIZE_64GB
) {
626 } else if (MaxAddress
< SIZE_1TB
) {
628 } else if (MaxAddress
< SIZE_4TB
) {
630 } else if (MaxAddress
< SIZE_16TB
) {
632 } else if (MaxAddress
< SIZE_256TB
) {
635 DEBUG ((EFI_D_ERROR
, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress
));
636 ASSERT (0); // Bigger than 48-bit memory space are not supported
637 return RETURN_UNSUPPORTED
;
639 } else if (ArmReadCurrentEL () == AARCH64_EL1
) {
640 // Due to Cortex-A57 erratum #822227 we must set TG1[1] == 1, regardless of EPD1.
641 TCR
= T0SZ
| TCR_TG0_4KB
| TCR_TG1_4KB
| TCR_EPD1
;
643 // Set the Physical Address Size using MaxAddress
644 if (MaxAddress
< SIZE_4GB
) {
646 } else if (MaxAddress
< SIZE_64GB
) {
648 } else if (MaxAddress
< SIZE_1TB
) {
650 } else if (MaxAddress
< SIZE_4TB
) {
652 } else if (MaxAddress
< SIZE_16TB
) {
654 } else if (MaxAddress
< SIZE_256TB
) {
655 TCR
|= TCR_IPS_256TB
;
657 DEBUG ((EFI_D_ERROR
, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress
));
658 ASSERT (0); // Bigger than 48-bit memory space are not supported
659 return RETURN_UNSUPPORTED
;
662 ASSERT (0); // UEFI is only expected to run at EL2 and EL1, not EL3.
663 return RETURN_UNSUPPORTED
;
669 // Allocate pages for translation table
670 TranslationTablePageCount
= EFI_SIZE_TO_PAGES(RootTableEntryCount
* sizeof(UINT64
));
671 TranslationTable
= (UINT64
*)AllocateAlignedPages (TranslationTablePageCount
, TT_ALIGNMENT_DESCRIPTION_TABLE
);
672 if (TranslationTable
== NULL
) {
673 return RETURN_OUT_OF_RESOURCES
;
675 // We set TTBR0 just after allocating the table to retrieve its location from the subsequent
676 // functions without needing to pass this value across the functions. The MMU is only enabled
677 // after the translation tables are populated.
678 ArmSetTTBR0 (TranslationTable
);
680 if (TranslationTableBase
!= NULL
) {
681 *TranslationTableBase
= TranslationTable
;
684 if (TranslationTableSize
!= NULL
) {
685 *TranslationTableSize
= RootTableEntryCount
* sizeof(UINT64
);
688 ZeroMem (TranslationTable
, RootTableEntryCount
* sizeof(UINT64
));
690 // Disable MMU and caches. ArmDisableMmu() also invalidates the TLBs
692 ArmDisableDataCache ();
693 ArmDisableInstructionCache ();
695 // Make sure nothing sneaked into the cache
696 ArmCleanInvalidateDataCache ();
697 ArmInvalidateInstructionCache ();
699 TranslationTableAttribute
= TT_ATTR_INDX_INVALID
;
700 while (MemoryTable
->Length
!= 0) {
701 // Find the memory attribute for the Translation Table
702 if (((UINTN
)TranslationTable
>= MemoryTable
->PhysicalBase
) &&
703 ((UINTN
)TranslationTable
<= MemoryTable
->PhysicalBase
- 1 + MemoryTable
->Length
)) {
704 TranslationTableAttribute
= MemoryTable
->Attributes
;
707 Status
= FillTranslationTable (TranslationTable
, MemoryTable
);
708 if (RETURN_ERROR (Status
)) {
709 goto FREE_TRANSLATION_TABLE
;
714 // Translate the Memory Attributes into Translation Table Register Attributes
715 if ((TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
) ||
716 (TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED
)) {
717 TCR
|= TCR_SH_NON_SHAREABLE
| TCR_RGN_OUTER_NON_CACHEABLE
| TCR_RGN_INNER_NON_CACHEABLE
;
718 } else if ((TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
) ||
719 (TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK
)) {
720 TCR
|= TCR_SH_INNER_SHAREABLE
| TCR_RGN_OUTER_WRITE_BACK_ALLOC
| TCR_RGN_INNER_WRITE_BACK_ALLOC
;
721 } else if ((TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
) ||
722 (TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH
)) {
723 TCR
|= TCR_SH_NON_SHAREABLE
| TCR_RGN_OUTER_WRITE_THROUGH
| TCR_RGN_INNER_WRITE_THROUGH
;
725 // If we failed to find a mapping that contains the root translation table then it probably means the translation table
726 // is not mapped in the given memory map.
728 Status
= RETURN_UNSUPPORTED
;
729 goto FREE_TRANSLATION_TABLE
;
732 // Set again TCR after getting the Translation Table attributes
735 ArmSetMAIR (MAIR_ATTR(TT_ATTR_INDX_DEVICE_MEMORY
, MAIR_ATTR_DEVICE_MEMORY
) | // mapped to EFI_MEMORY_UC
736 MAIR_ATTR(TT_ATTR_INDX_MEMORY_NON_CACHEABLE
, MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
) | // mapped to EFI_MEMORY_WC
737 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_THROUGH
, MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
) | // mapped to EFI_MEMORY_WT
738 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_BACK
, MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
)); // mapped to EFI_MEMORY_WB
740 ArmDisableAlignmentCheck ();
741 ArmEnableInstructionCache ();
742 ArmEnableDataCache ();
745 return RETURN_SUCCESS
;
747 FREE_TRANSLATION_TABLE
:
748 FreePages (TranslationTable
, TranslationTablePageCount
);
754 ArmMmuBaseLibConstructor (
758 extern UINT32 ArmReplaceLiveTranslationEntrySize
;
761 // The ArmReplaceLiveTranslationEntry () helper function may be invoked
762 // with the MMU off so we have to ensure that it gets cleaned to the PoC
764 WriteBackDataCacheRange (ArmReplaceLiveTranslationEntry
,
765 ArmReplaceLiveTranslationEntrySize
);
767 return RETURN_SUCCESS
;