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)
31 ArmMemoryAttributeToPageAttribute (
32 IN ARM_MEMORY_REGION_ATTRIBUTES Attributes
36 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
:
37 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK
:
38 return TT_ATTR_INDX_MEMORY_WRITE_BACK
| TT_SH_INNER_SHAREABLE
;
40 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
:
41 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH
:
42 return TT_ATTR_INDX_MEMORY_WRITE_THROUGH
| TT_SH_INNER_SHAREABLE
;
44 // Uncached and device mappings are treated as outer shareable by default,
45 case ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
:
46 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED
:
47 return TT_ATTR_INDX_MEMORY_NON_CACHEABLE
;
51 case ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
:
52 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_DEVICE
:
53 if (ArmReadCurrentEL () == AARCH64_EL2
)
54 return TT_ATTR_INDX_DEVICE_MEMORY
| TT_XN_MASK
;
56 return TT_ATTR_INDX_DEVICE_MEMORY
| TT_UXN_MASK
| TT_PXN_MASK
;
61 PageAttributeToGcdAttribute (
62 IN UINT64 PageAttributes
67 switch (PageAttributes
& TT_ATTR_INDX_MASK
) {
68 case TT_ATTR_INDX_DEVICE_MEMORY
:
69 GcdAttributes
= EFI_MEMORY_UC
;
71 case TT_ATTR_INDX_MEMORY_NON_CACHEABLE
:
72 GcdAttributes
= EFI_MEMORY_WC
;
74 case TT_ATTR_INDX_MEMORY_WRITE_THROUGH
:
75 GcdAttributes
= EFI_MEMORY_WT
;
77 case TT_ATTR_INDX_MEMORY_WRITE_BACK
:
78 GcdAttributes
= EFI_MEMORY_WB
;
81 DEBUG ((EFI_D_ERROR
, "PageAttributeToGcdAttribute: PageAttributes:0x%lX not supported.\n", PageAttributes
));
83 // The Global Coherency Domain (GCD) value is defined as a bit set.
84 // Returning 0 means no attribute has been set.
88 // Determine protection attributes
89 if (((PageAttributes
& TT_AP_MASK
) == TT_AP_NO_RO
) || ((PageAttributes
& TT_AP_MASK
) == TT_AP_RO_RO
)) {
90 // Read only cases map to write-protect
91 GcdAttributes
|= EFI_MEMORY_WP
;
94 // Process eXecute Never attribute
95 if ((PageAttributes
& (TT_PXN_MASK
| TT_UXN_MASK
)) != 0 ) {
96 GcdAttributes
|= EFI_MEMORY_XP
;
102 ARM_MEMORY_REGION_ATTRIBUTES
103 GcdAttributeToArmAttribute (
104 IN UINT64 GcdAttributes
107 switch (GcdAttributes
& 0xFF) {
109 return ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
;
111 return ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
;
113 return ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
;
115 return ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
;
117 DEBUG ((EFI_D_ERROR
, "GcdAttributeToArmAttribute: 0x%lX attributes is not supported.\n", GcdAttributes
));
119 return ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
;
123 // Describe the T0SZ values for each translation table level
127 UINTN LargestT0SZ
; // Generally (MaxT0SZ == LargestT0SZ) but at the Level3 Table
128 // the MaxT0SZ is not at the boundary of the table
129 } T0SZ_DESCRIPTION_PER_LEVEL
;
131 // Map table for the corresponding Level of Table
132 STATIC CONST T0SZ_DESCRIPTION_PER_LEVEL T0SZPerTableLevel
[] = {
133 { 16, 24, 24 }, // Table Level 0
134 { 25, 33, 33 }, // Table Level 1
135 { 34, 39, 42 } // Table Level 2
139 GetRootTranslationTableInfo (
141 OUT UINTN
*TableLevel
,
142 OUT UINTN
*TableEntryCount
147 // Identify the level of the root table from the given T0SZ
148 for (Index
= 0; Index
< sizeof (T0SZPerTableLevel
) / sizeof (T0SZ_DESCRIPTION_PER_LEVEL
); Index
++) {
149 if (T0SZ
<= T0SZPerTableLevel
[Index
].MaxT0SZ
) {
154 // If we have not found the corresponding maximum T0SZ then we use the last one
155 if (Index
== sizeof (T0SZPerTableLevel
) / sizeof (T0SZ_DESCRIPTION_PER_LEVEL
)) {
159 // Get the level of the root table
164 // The Size of the Table is 2^(T0SZ-LargestT0SZ)
165 if (TableEntryCount
) {
166 *TableEntryCount
= 1 << (T0SZPerTableLevel
[Index
].LargestT0SZ
- T0SZ
+ 1);
177 if (!ArmMmuEnabled ()) {
180 ArmReplaceLiveTranslationEntry (Entry
, Value
);
186 LookupAddresstoRootTable (
187 IN UINT64 MaxAddress
,
189 OUT UINTN
*TableEntryCount
194 // Check the parameters are not NULL
195 ASSERT ((T0SZ
!= NULL
) && (TableEntryCount
!= NULL
));
197 // Look for the highest bit set in MaxAddress
198 for (TopBit
= 63; TopBit
!= 0; TopBit
--) {
199 if ((1ULL << TopBit
) & MaxAddress
) {
200 // MaxAddress top bit is found
205 ASSERT (TopBit
!= 0);
207 // Calculate T0SZ from the top bit of the MaxAddress
210 // Get the Table info from T0SZ
211 GetRootTranslationTableInfo (*T0SZ
, NULL
, TableEntryCount
);
216 GetBlockEntryListFromAddress (
217 IN UINT64
*RootTable
,
218 IN UINT64 RegionStart
,
219 OUT UINTN
*TableLevel
,
220 IN OUT UINT64
*BlockEntrySize
,
221 OUT UINT64
**LastBlockEntry
224 UINTN RootTableLevel
;
225 UINTN RootTableEntryCount
;
226 UINT64
*TranslationTable
;
228 UINT64
*SubTableBlockEntry
;
229 UINT64 BlockEntryAddress
;
230 UINTN BaseAddressAlignment
;
236 UINT64 TableAttributes
;
238 // Initialize variable
241 // Ensure the parameters are valid
242 if (!(TableLevel
&& BlockEntrySize
&& LastBlockEntry
)) {
243 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
247 // Ensure the Region is aligned on 4KB boundary
248 if ((RegionStart
& (SIZE_4KB
- 1)) != 0) {
249 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
253 // Ensure the required size is aligned on 4KB boundary and not 0
254 if ((*BlockEntrySize
& (SIZE_4KB
- 1)) != 0 || *BlockEntrySize
== 0) {
255 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
259 T0SZ
= ArmGetTCR () & TCR_T0SZ_MASK
;
260 // Get the Table info from T0SZ
261 GetRootTranslationTableInfo (T0SZ
, &RootTableLevel
, &RootTableEntryCount
);
263 // If the start address is 0x0 then we use the size of the region to identify the alignment
264 if (RegionStart
== 0) {
265 // Identify the highest possible alignment for the Region Size
266 BaseAddressAlignment
= LowBitSet64 (*BlockEntrySize
);
268 // Identify the highest possible alignment for the Base Address
269 BaseAddressAlignment
= LowBitSet64 (RegionStart
);
272 // Identify the Page Level the RegionStart must belong to. Note that PageLevel
273 // should be at least 1 since block translations are not supported at level 0
274 PageLevel
= MAX (3 - ((BaseAddressAlignment
- 12) / 9), 1);
276 // If the required size is smaller than the current block size then we need to go to the page below.
277 // The PageLevel was calculated on the Base Address alignment but did not take in account the alignment
278 // of the allocation size
279 while (*BlockEntrySize
< TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel
)) {
280 // It does not fit so we need to go a page level above
285 // Get the Table Descriptor for the corresponding PageLevel. We need to decompose RegionStart to get appropriate entries
288 TranslationTable
= RootTable
;
289 for (IndexLevel
= RootTableLevel
; IndexLevel
<= PageLevel
; IndexLevel
++) {
290 BlockEntry
= (UINT64
*)TT_GET_ENTRY_FOR_ADDRESS (TranslationTable
, IndexLevel
, RegionStart
);
292 if ((IndexLevel
!= 3) && ((*BlockEntry
& TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
)) {
293 // Go to the next table
294 TranslationTable
= (UINT64
*)(*BlockEntry
& TT_ADDRESS_MASK_DESCRIPTION_TABLE
);
296 // If we are at the last level then update the last level to next level
297 if (IndexLevel
== PageLevel
) {
298 // Enter the next level
301 } else if ((*BlockEntry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY
) {
302 // If we are not at the last level then we need to split this BlockEntry
303 if (IndexLevel
!= PageLevel
) {
304 // Retrieve the attributes from the block entry
305 Attributes
= *BlockEntry
& TT_ATTRIBUTES_MASK
;
307 // Convert the block entry attributes into Table descriptor attributes
308 TableAttributes
= TT_TABLE_AP_NO_PERMISSION
;
309 if (Attributes
& TT_PXN_MASK
) {
310 TableAttributes
= TT_TABLE_PXN
;
312 // XN maps to UXN in the EL1&0 translation regime
313 if (Attributes
& TT_XN_MASK
) {
314 TableAttributes
= TT_TABLE_XN
;
316 if (Attributes
& TT_NS
) {
317 TableAttributes
= TT_TABLE_NS
;
320 // Get the address corresponding at this entry
321 BlockEntryAddress
= RegionStart
;
322 BlockEntryAddress
= BlockEntryAddress
>> TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel
);
323 // Shift back to right to set zero before the effective address
324 BlockEntryAddress
= BlockEntryAddress
<< TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel
);
326 // Set the correct entry type for the next page level
327 if ((IndexLevel
+ 1) == 3) {
328 Attributes
|= TT_TYPE_BLOCK_ENTRY_LEVEL3
;
330 Attributes
|= TT_TYPE_BLOCK_ENTRY
;
333 // Create a new translation table
334 TranslationTable
= (UINT64
*)AllocateAlignedPages (EFI_SIZE_TO_PAGES(TT_ENTRY_COUNT
* sizeof(UINT64
)), TT_ALIGNMENT_DESCRIPTION_TABLE
);
335 if (TranslationTable
== NULL
) {
339 // Populate the newly created lower level table
340 SubTableBlockEntry
= TranslationTable
;
341 for (Index
= 0; Index
< TT_ENTRY_COUNT
; Index
++) {
342 *SubTableBlockEntry
= Attributes
| (BlockEntryAddress
+ (Index
<< TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel
+ 1)));
343 SubTableBlockEntry
++;
346 // Fill the BlockEntry with the new TranslationTable
347 ReplaceLiveEntry (BlockEntry
,
348 ((UINTN
)TranslationTable
& TT_ADDRESS_MASK_DESCRIPTION_TABLE
) | TableAttributes
| TT_TYPE_TABLE_ENTRY
);
351 if (IndexLevel
!= PageLevel
) {
353 // Case when we have an Invalid Entry and we are at a page level above of the one targetted.
356 // Create a new translation table
357 TranslationTable
= (UINT64
*)AllocateAlignedPages (EFI_SIZE_TO_PAGES(TT_ENTRY_COUNT
* sizeof(UINT64
)), TT_ALIGNMENT_DESCRIPTION_TABLE
);
358 if (TranslationTable
== NULL
) {
362 ZeroMem (TranslationTable
, TT_ENTRY_COUNT
* sizeof(UINT64
));
364 // Fill the new BlockEntry with the TranslationTable
365 *BlockEntry
= ((UINTN
)TranslationTable
& TT_ADDRESS_MASK_DESCRIPTION_TABLE
) | TT_TYPE_TABLE_ENTRY
;
370 // Expose the found PageLevel to the caller
371 *TableLevel
= PageLevel
;
373 // Now, we have the Table Level we can get the Block Size associated to this table
374 *BlockEntrySize
= TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel
);
376 // The last block of the root table depends on the number of entry in this table,
377 // otherwise it is always the (TT_ENTRY_COUNT - 1)th entry in the table.
378 *LastBlockEntry
= TT_LAST_BLOCK_ADDRESS(TranslationTable
,
379 (PageLevel
== RootTableLevel
) ? RootTableEntryCount
: TT_ENTRY_COUNT
);
386 UpdateRegionMapping (
387 IN UINT64
*RootTable
,
388 IN UINT64 RegionStart
,
389 IN UINT64 RegionLength
,
390 IN UINT64 Attributes
,
391 IN UINT64 BlockEntryMask
396 UINT64
*LastBlockEntry
;
397 UINT64 BlockEntrySize
;
400 // Ensure the Length is aligned on 4KB boundary
401 if ((RegionLength
== 0) || ((RegionLength
& (SIZE_4KB
- 1)) != 0)) {
402 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
403 return RETURN_INVALID_PARAMETER
;
407 // Get the first Block Entry that matches the Virtual Address and also the information on the Table Descriptor
408 // such as the the size of the Block Entry and the address of the last BlockEntry of the Table Descriptor
409 BlockEntrySize
= RegionLength
;
410 BlockEntry
= GetBlockEntryListFromAddress (RootTable
, RegionStart
, &TableLevel
, &BlockEntrySize
, &LastBlockEntry
);
411 if (BlockEntry
== NULL
) {
412 // GetBlockEntryListFromAddress() return NULL when it fails to allocate new pages from the Translation Tables
413 return RETURN_OUT_OF_RESOURCES
;
416 if (TableLevel
!= 3) {
417 Type
= TT_TYPE_BLOCK_ENTRY
;
419 Type
= TT_TYPE_BLOCK_ENTRY_LEVEL3
;
423 // Fill the Block Entry with attribute and output block address
424 *BlockEntry
&= BlockEntryMask
;
425 *BlockEntry
|= (RegionStart
& TT_ADDRESS_MASK_BLOCK_ENTRY
) | Attributes
| Type
;
427 // Go to the next BlockEntry
428 RegionStart
+= BlockEntrySize
;
429 RegionLength
-= BlockEntrySize
;
432 // Break the inner loop when next block is a table
433 // Rerun GetBlockEntryListFromAddress to avoid page table memory leak
434 if (TableLevel
!= 3 &&
435 (*BlockEntry
& TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
) {
438 } while ((RegionLength
>= BlockEntrySize
) && (BlockEntry
<= LastBlockEntry
));
439 } while (RegionLength
!= 0);
441 return RETURN_SUCCESS
;
446 FillTranslationTable (
447 IN UINT64
*RootTable
,
448 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryRegion
451 return UpdateRegionMapping (
453 MemoryRegion
->VirtualBase
,
454 MemoryRegion
->Length
,
455 ArmMemoryAttributeToPageAttribute (MemoryRegion
->Attributes
) | TT_AF
,
461 SetMemoryAttributes (
462 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
464 IN UINT64 Attributes
,
465 IN EFI_PHYSICAL_ADDRESS VirtualMask
468 RETURN_STATUS Status
;
469 ARM_MEMORY_REGION_DESCRIPTOR MemoryRegion
;
470 UINT64
*TranslationTable
;
472 MemoryRegion
.PhysicalBase
= BaseAddress
;
473 MemoryRegion
.VirtualBase
= BaseAddress
;
474 MemoryRegion
.Length
= Length
;
475 MemoryRegion
.Attributes
= GcdAttributeToArmAttribute (Attributes
);
477 TranslationTable
= ArmGetTTBR0BaseAddress ();
479 Status
= FillTranslationTable (TranslationTable
, &MemoryRegion
);
480 if (RETURN_ERROR (Status
)) {
484 // Invalidate all TLB entries so changes are synced
487 return RETURN_SUCCESS
;
492 SetMemoryRegionAttribute (
493 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
495 IN UINT64 Attributes
,
496 IN UINT64 BlockEntryMask
499 RETURN_STATUS Status
;
502 RootTable
= ArmGetTTBR0BaseAddress ();
504 Status
= UpdateRegionMapping (RootTable
, BaseAddress
, Length
, Attributes
, BlockEntryMask
);
505 if (RETURN_ERROR (Status
)) {
509 // Invalidate all TLB entries so changes are synced
512 return RETURN_SUCCESS
;
516 ArmSetMemoryRegionNoExec (
517 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
523 if (ArmReadCurrentEL () == AARCH64_EL1
) {
524 Val
= TT_PXN_MASK
| TT_UXN_MASK
;
529 return SetMemoryRegionAttribute (
533 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
537 ArmClearMemoryRegionNoExec (
538 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
544 // XN maps to UXN in the EL1&0 translation regime
545 Mask
= ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_PXN_MASK
| TT_XN_MASK
);
547 return SetMemoryRegionAttribute (
555 ArmSetMemoryRegionReadOnly (
556 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
560 return SetMemoryRegionAttribute (
564 ~TT_ADDRESS_MASK_BLOCK_ENTRY
);
568 ArmClearMemoryRegionReadOnly (
569 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
573 return SetMemoryRegionAttribute (
577 ~(TT_ADDRESS_MASK_BLOCK_ENTRY
| TT_AP_MASK
));
583 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTable
,
584 OUT VOID
**TranslationTableBase OPTIONAL
,
585 OUT UINTN
*TranslationTableSize OPTIONAL
588 VOID
* TranslationTable
;
589 UINTN TranslationTablePageCount
;
590 UINT32 TranslationTableAttribute
;
591 ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTableEntry
;
595 UINTN RootTableEntryCount
;
597 RETURN_STATUS Status
;
599 if(MemoryTable
== NULL
) {
600 ASSERT (MemoryTable
!= NULL
);
601 return RETURN_INVALID_PARAMETER
;
604 // Identify the highest address of the memory table
605 MaxAddress
= MemoryTable
->PhysicalBase
+ MemoryTable
->Length
- 1;
606 MemoryTableEntry
= MemoryTable
;
607 while (MemoryTableEntry
->Length
!= 0) {
608 TopAddress
= MemoryTableEntry
->PhysicalBase
+ MemoryTableEntry
->Length
- 1;
609 if (TopAddress
> MaxAddress
) {
610 MaxAddress
= TopAddress
;
615 // Lookup the Table Level to get the information
616 LookupAddresstoRootTable (MaxAddress
, &T0SZ
, &RootTableEntryCount
);
619 // Set TCR that allows us to retrieve T0SZ in the subsequent functions
621 // Ideally we will be running at EL2, but should support EL1 as well.
622 // UEFI should not run at EL3.
623 if (ArmReadCurrentEL () == AARCH64_EL2
) {
624 //Note: Bits 23 and 31 are reserved(RES1) bits in TCR_EL2
625 TCR
= T0SZ
| (1UL << 31) | (1UL << 23) | TCR_TG0_4KB
;
627 // Set the Physical Address Size using MaxAddress
628 if (MaxAddress
< SIZE_4GB
) {
630 } else if (MaxAddress
< SIZE_64GB
) {
632 } else if (MaxAddress
< SIZE_1TB
) {
634 } else if (MaxAddress
< SIZE_4TB
) {
636 } else if (MaxAddress
< SIZE_16TB
) {
638 } else if (MaxAddress
< SIZE_256TB
) {
641 DEBUG ((EFI_D_ERROR
, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress
));
642 ASSERT (0); // Bigger than 48-bit memory space are not supported
643 return RETURN_UNSUPPORTED
;
645 } else if (ArmReadCurrentEL () == AARCH64_EL1
) {
646 // Due to Cortex-A57 erratum #822227 we must set TG1[1] == 1, regardless of EPD1.
647 TCR
= T0SZ
| TCR_TG0_4KB
| TCR_TG1_4KB
| TCR_EPD1
;
649 // Set the Physical Address Size using MaxAddress
650 if (MaxAddress
< SIZE_4GB
) {
652 } else if (MaxAddress
< SIZE_64GB
) {
654 } else if (MaxAddress
< SIZE_1TB
) {
656 } else if (MaxAddress
< SIZE_4TB
) {
658 } else if (MaxAddress
< SIZE_16TB
) {
660 } else if (MaxAddress
< SIZE_256TB
) {
661 TCR
|= TCR_IPS_256TB
;
663 DEBUG ((EFI_D_ERROR
, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress
));
664 ASSERT (0); // Bigger than 48-bit memory space are not supported
665 return RETURN_UNSUPPORTED
;
668 ASSERT (0); // UEFI is only expected to run at EL2 and EL1, not EL3.
669 return RETURN_UNSUPPORTED
;
675 // Allocate pages for translation table
676 TranslationTablePageCount
= EFI_SIZE_TO_PAGES(RootTableEntryCount
* sizeof(UINT64
));
677 TranslationTable
= (UINT64
*)AllocateAlignedPages (TranslationTablePageCount
, TT_ALIGNMENT_DESCRIPTION_TABLE
);
678 if (TranslationTable
== NULL
) {
679 return RETURN_OUT_OF_RESOURCES
;
681 // We set TTBR0 just after allocating the table to retrieve its location from the subsequent
682 // functions without needing to pass this value across the functions. The MMU is only enabled
683 // after the translation tables are populated.
684 ArmSetTTBR0 (TranslationTable
);
686 if (TranslationTableBase
!= NULL
) {
687 *TranslationTableBase
= TranslationTable
;
690 if (TranslationTableSize
!= NULL
) {
691 *TranslationTableSize
= RootTableEntryCount
* sizeof(UINT64
);
694 ZeroMem (TranslationTable
, RootTableEntryCount
* sizeof(UINT64
));
696 // Disable MMU and caches. ArmDisableMmu() also invalidates the TLBs
698 ArmDisableDataCache ();
699 ArmDisableInstructionCache ();
701 // Make sure nothing sneaked into the cache
702 ArmCleanInvalidateDataCache ();
703 ArmInvalidateInstructionCache ();
705 TranslationTableAttribute
= TT_ATTR_INDX_INVALID
;
706 while (MemoryTable
->Length
!= 0) {
707 // Find the memory attribute for the Translation Table
708 if (((UINTN
)TranslationTable
>= MemoryTable
->PhysicalBase
) &&
709 ((UINTN
)TranslationTable
<= MemoryTable
->PhysicalBase
- 1 + MemoryTable
->Length
)) {
710 TranslationTableAttribute
= MemoryTable
->Attributes
;
713 Status
= FillTranslationTable (TranslationTable
, MemoryTable
);
714 if (RETURN_ERROR (Status
)) {
715 goto FREE_TRANSLATION_TABLE
;
720 // Translate the Memory Attributes into Translation Table Register Attributes
721 if ((TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
) ||
722 (TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED
)) {
723 TCR
|= TCR_SH_NON_SHAREABLE
| TCR_RGN_OUTER_NON_CACHEABLE
| TCR_RGN_INNER_NON_CACHEABLE
;
724 } else if ((TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
) ||
725 (TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK
)) {
726 TCR
|= TCR_SH_INNER_SHAREABLE
| TCR_RGN_OUTER_WRITE_BACK_ALLOC
| TCR_RGN_INNER_WRITE_BACK_ALLOC
;
727 } else if ((TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
) ||
728 (TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH
)) {
729 TCR
|= TCR_SH_NON_SHAREABLE
| TCR_RGN_OUTER_WRITE_THROUGH
| TCR_RGN_INNER_WRITE_THROUGH
;
731 // If we failed to find a mapping that contains the root translation table then it probably means the translation table
732 // is not mapped in the given memory map.
734 Status
= RETURN_UNSUPPORTED
;
735 goto FREE_TRANSLATION_TABLE
;
738 // Set again TCR after getting the Translation Table attributes
741 ArmSetMAIR (MAIR_ATTR(TT_ATTR_INDX_DEVICE_MEMORY
, MAIR_ATTR_DEVICE_MEMORY
) | // mapped to EFI_MEMORY_UC
742 MAIR_ATTR(TT_ATTR_INDX_MEMORY_NON_CACHEABLE
, MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
) | // mapped to EFI_MEMORY_WC
743 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_THROUGH
, MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
) | // mapped to EFI_MEMORY_WT
744 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_BACK
, MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
)); // mapped to EFI_MEMORY_WB
746 ArmDisableAlignmentCheck ();
747 ArmEnableInstructionCache ();
748 ArmEnableDataCache ();
751 return RETURN_SUCCESS
;
753 FREE_TRANSLATION_TABLE
:
754 FreePages (TranslationTable
, TranslationTablePageCount
);