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 return TT_ATTR_INDX_MEMORY_WRITE_BACK
;
38 case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
:
39 return TT_ATTR_INDX_MEMORY_WRITE_THROUGH
;
40 case ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
:
41 return TT_ATTR_INDX_DEVICE_MEMORY
;
42 case ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
:
43 return TT_ATTR_INDX_MEMORY_NON_CACHEABLE
;
44 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK
:
45 return TT_ATTR_INDX_MEMORY_WRITE_BACK
;
46 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH
:
47 return TT_ATTR_INDX_MEMORY_WRITE_THROUGH
;
48 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_DEVICE
:
49 return TT_ATTR_INDX_DEVICE_MEMORY
;
50 case ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED
:
51 return TT_ATTR_INDX_MEMORY_NON_CACHEABLE
;
54 return TT_ATTR_INDX_DEVICE_MEMORY
;
59 PageAttributeToGcdAttribute (
60 IN UINT64 PageAttributes
65 switch (PageAttributes
& TT_ATTR_INDX_MASK
) {
66 case TT_ATTR_INDX_DEVICE_MEMORY
:
67 GcdAttributes
= EFI_MEMORY_UC
;
69 case TT_ATTR_INDX_MEMORY_NON_CACHEABLE
:
70 GcdAttributes
= EFI_MEMORY_WC
;
72 case TT_ATTR_INDX_MEMORY_WRITE_THROUGH
:
73 GcdAttributes
= EFI_MEMORY_WT
;
75 case TT_ATTR_INDX_MEMORY_WRITE_BACK
:
76 GcdAttributes
= EFI_MEMORY_WB
;
79 DEBUG ((EFI_D_ERROR
, "PageAttributeToGcdAttribute: PageAttributes:0x%lX not supported.\n", PageAttributes
));
81 // The Global Coherency Domain (GCD) value is defined as a bit set.
82 // Returning 0 means no attribute has been set.
86 // Determine protection attributes
87 if (((PageAttributes
& TT_AP_MASK
) == TT_AP_NO_RO
) || ((PageAttributes
& TT_AP_MASK
) == TT_AP_RO_RO
)) {
88 // Read only cases map to write-protect
89 GcdAttributes
|= EFI_MEMORY_WP
;
92 // Process eXecute Never attribute
93 if ((PageAttributes
& (TT_PXN_MASK
| TT_UXN_MASK
)) != 0 ) {
94 GcdAttributes
|= EFI_MEMORY_XP
;
101 GcdAttributeToPageAttribute (
102 IN UINT64 GcdAttributes
105 UINT64 PageAttributes
;
107 switch (GcdAttributes
& 0xFF) {
109 PageAttributes
= TT_ATTR_INDX_DEVICE_MEMORY
;
112 PageAttributes
= TT_ATTR_INDX_MEMORY_NON_CACHEABLE
;
115 PageAttributes
= TT_ATTR_INDX_MEMORY_WRITE_THROUGH
;
118 PageAttributes
= TT_ATTR_INDX_MEMORY_WRITE_BACK
;
121 DEBUG ((EFI_D_ERROR
, "GcdAttributeToPageAttribute: 0x%X attributes is not supported.\n", GcdAttributes
));
123 // If no match has been found then we mark the memory as device memory.
124 // The only side effect of using device memory should be a slow down in the performance.
125 PageAttributes
= TT_ATTR_INDX_DEVICE_MEMORY
;
128 // Determine protection attributes
129 if (GcdAttributes
& EFI_MEMORY_WP
) {
130 // Read only cases map to write-protect
131 PageAttributes
|= TT_AP_RO_RO
;
134 // Process eXecute Never attribute
135 if (GcdAttributes
& EFI_MEMORY_XP
) {
136 PageAttributes
|= (TT_PXN_MASK
| TT_UXN_MASK
);
139 return PageAttributes
;
142 ARM_MEMORY_REGION_ATTRIBUTES
143 GcdAttributeToArmAttribute (
144 IN UINT64 GcdAttributes
147 switch (GcdAttributes
& 0xFF) {
149 return ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
;
151 return ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
;
153 return ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
;
155 return ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
;
157 DEBUG ((EFI_D_ERROR
, "GcdAttributeToArmAttribute: 0x%lX attributes is not supported.\n", GcdAttributes
));
159 return ARM_MEMORY_REGION_ATTRIBUTE_DEVICE
;
163 // Describe the T0SZ values for each translation table level
167 UINTN LargestT0SZ
; // Generally (MaxT0SZ == LargestT0SZ) but at the Level3 Table
168 // the MaxT0SZ is not at the boundary of the table
169 } T0SZ_DESCRIPTION_PER_LEVEL
;
171 // Map table for the corresponding Level of Table
172 STATIC CONST T0SZ_DESCRIPTION_PER_LEVEL T0SZPerTableLevel
[] = {
173 { 16, 24, 24 }, // Table Level 0
174 { 25, 33, 33 }, // Table Level 1
175 { 34, 39, 42 } // Table Level 2
179 GetRootTranslationTableInfo (
181 OUT UINTN
*TableLevel
,
182 OUT UINTN
*TableEntryCount
187 // Identify the level of the root table from the given T0SZ
188 for (Index
= 0; Index
< sizeof (T0SZPerTableLevel
) / sizeof (T0SZ_DESCRIPTION_PER_LEVEL
); Index
++) {
189 if (T0SZ
<= T0SZPerTableLevel
[Index
].MaxT0SZ
) {
194 // If we have not found the corresponding maximum T0SZ then we use the last one
195 if (Index
== sizeof (T0SZPerTableLevel
) / sizeof (T0SZ_DESCRIPTION_PER_LEVEL
)) {
199 // Get the level of the root table
204 // The Size of the Table is 2^(T0SZ-LargestT0SZ)
205 if (TableEntryCount
) {
206 *TableEntryCount
= 1 << (T0SZPerTableLevel
[Index
].LargestT0SZ
- T0SZ
+ 1);
212 LookupAddresstoRootTable (
213 IN UINT64 MaxAddress
,
215 OUT UINTN
*TableEntryCount
220 // Check the parameters are not NULL
221 ASSERT ((T0SZ
!= NULL
) && (TableEntryCount
!= NULL
));
223 // Look for the highest bit set in MaxAddress
224 for (TopBit
= 63; TopBit
!= 0; TopBit
--) {
225 if ((1ULL << TopBit
) & MaxAddress
) {
226 // MaxAddress top bit is found
231 ASSERT (TopBit
!= 0);
233 // Calculate T0SZ from the top bit of the MaxAddress
236 // Get the Table info from T0SZ
237 GetRootTranslationTableInfo (*T0SZ
, NULL
, TableEntryCount
);
242 GetBlockEntryListFromAddress (
243 IN UINT64
*RootTable
,
244 IN UINT64 RegionStart
,
245 OUT UINTN
*TableLevel
,
246 IN OUT UINT64
*BlockEntrySize
,
247 OUT UINT64
**LastBlockEntry
250 UINTN RootTableLevel
;
251 UINTN RootTableEntryCount
;
252 UINT64
*TranslationTable
;
254 UINT64
*SubTableBlockEntry
;
255 UINT64 BlockEntryAddress
;
256 UINTN BaseAddressAlignment
;
262 UINT64 TableAttributes
;
264 // Initialize variable
267 // Ensure the parameters are valid
268 if (!(TableLevel
&& BlockEntrySize
&& LastBlockEntry
)) {
269 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
273 // Ensure the Region is aligned on 4KB boundary
274 if ((RegionStart
& (SIZE_4KB
- 1)) != 0) {
275 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
279 // Ensure the required size is aligned on 4KB boundary and not 0
280 if ((*BlockEntrySize
& (SIZE_4KB
- 1)) != 0 || *BlockEntrySize
== 0) {
281 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
285 T0SZ
= ArmGetTCR () & TCR_T0SZ_MASK
;
286 // Get the Table info from T0SZ
287 GetRootTranslationTableInfo (T0SZ
, &RootTableLevel
, &RootTableEntryCount
);
289 // If the start address is 0x0 then we use the size of the region to identify the alignment
290 if (RegionStart
== 0) {
291 // Identify the highest possible alignment for the Region Size
292 BaseAddressAlignment
= LowBitSet64 (*BlockEntrySize
);
294 // Identify the highest possible alignment for the Base Address
295 BaseAddressAlignment
= LowBitSet64 (RegionStart
);
298 // Identify the Page Level the RegionStart must belong to. Note that PageLevel
299 // should be at least 1 since block translations are not supported at level 0
300 PageLevel
= MAX (3 - ((BaseAddressAlignment
- 12) / 9), 1);
302 // If the required size is smaller than the current block size then we need to go to the page below.
303 // The PageLevel was calculated on the Base Address alignment but did not take in account the alignment
304 // of the allocation size
305 while (*BlockEntrySize
< TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel
)) {
306 // It does not fit so we need to go a page level above
311 // Get the Table Descriptor for the corresponding PageLevel. We need to decompose RegionStart to get appropriate entries
314 TranslationTable
= RootTable
;
315 for (IndexLevel
= RootTableLevel
; IndexLevel
<= PageLevel
; IndexLevel
++) {
316 BlockEntry
= (UINT64
*)TT_GET_ENTRY_FOR_ADDRESS (TranslationTable
, IndexLevel
, RegionStart
);
318 if ((IndexLevel
!= 3) && ((*BlockEntry
& TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
)) {
319 // Go to the next table
320 TranslationTable
= (UINT64
*)(*BlockEntry
& TT_ADDRESS_MASK_DESCRIPTION_TABLE
);
322 // If we are at the last level then update the last level to next level
323 if (IndexLevel
== PageLevel
) {
324 // Enter the next level
327 } else if ((*BlockEntry
& TT_TYPE_MASK
) == TT_TYPE_BLOCK_ENTRY
) {
328 // If we are not at the last level then we need to split this BlockEntry
329 if (IndexLevel
!= PageLevel
) {
330 // Retrieve the attributes from the block entry
331 Attributes
= *BlockEntry
& TT_ATTRIBUTES_MASK
;
333 // Convert the block entry attributes into Table descriptor attributes
334 TableAttributes
= TT_TABLE_AP_NO_PERMISSION
;
335 if (Attributes
& TT_PXN_MASK
) {
336 TableAttributes
= TT_TABLE_PXN
;
338 if (Attributes
& TT_UXN_MASK
) {
339 TableAttributes
= TT_TABLE_XN
;
341 if (Attributes
& TT_NS
) {
342 TableAttributes
= TT_TABLE_NS
;
345 // Get the address corresponding at this entry
346 BlockEntryAddress
= RegionStart
;
347 BlockEntryAddress
= BlockEntryAddress
>> TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel
);
348 // Shift back to right to set zero before the effective address
349 BlockEntryAddress
= BlockEntryAddress
<< TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel
);
351 // Set the correct entry type for the next page level
352 if ((IndexLevel
+ 1) == 3) {
353 Attributes
|= TT_TYPE_BLOCK_ENTRY_LEVEL3
;
355 Attributes
|= TT_TYPE_BLOCK_ENTRY
;
358 // Create a new translation table
359 TranslationTable
= (UINT64
*)AllocateAlignedPages (EFI_SIZE_TO_PAGES(TT_ENTRY_COUNT
* sizeof(UINT64
)), TT_ALIGNMENT_DESCRIPTION_TABLE
);
360 if (TranslationTable
== NULL
) {
364 // Populate the newly created lower level table
365 SubTableBlockEntry
= TranslationTable
;
366 for (Index
= 0; Index
< TT_ENTRY_COUNT
; Index
++) {
367 *SubTableBlockEntry
= Attributes
| (BlockEntryAddress
+ (Index
<< TT_ADDRESS_OFFSET_AT_LEVEL(IndexLevel
+ 1)));
368 SubTableBlockEntry
++;
371 // Fill the BlockEntry with the new TranslationTable
372 *BlockEntry
= ((UINTN
)TranslationTable
& TT_ADDRESS_MASK_DESCRIPTION_TABLE
) | TableAttributes
| TT_TYPE_TABLE_ENTRY
;
375 if (IndexLevel
!= PageLevel
) {
377 // Case when we have an Invalid Entry and we are at a page level above of the one targetted.
380 // Create a new translation table
381 TranslationTable
= (UINT64
*)AllocateAlignedPages (EFI_SIZE_TO_PAGES(TT_ENTRY_COUNT
* sizeof(UINT64
)), TT_ALIGNMENT_DESCRIPTION_TABLE
);
382 if (TranslationTable
== NULL
) {
386 ZeroMem (TranslationTable
, TT_ENTRY_COUNT
* sizeof(UINT64
));
388 // Fill the new BlockEntry with the TranslationTable
389 *BlockEntry
= ((UINTN
)TranslationTable
& TT_ADDRESS_MASK_DESCRIPTION_TABLE
) | TT_TYPE_TABLE_ENTRY
;
394 // Expose the found PageLevel to the caller
395 *TableLevel
= PageLevel
;
397 // Now, we have the Table Level we can get the Block Size associated to this table
398 *BlockEntrySize
= TT_BLOCK_ENTRY_SIZE_AT_LEVEL (PageLevel
);
400 // The last block of the root table depends on the number of entry in this table,
401 // otherwise it is always the (TT_ENTRY_COUNT - 1)th entry in the table.
402 *LastBlockEntry
= TT_LAST_BLOCK_ADDRESS(TranslationTable
,
403 (PageLevel
== RootTableLevel
) ? RootTableEntryCount
: TT_ENTRY_COUNT
);
410 FillTranslationTable (
411 IN UINT64
*RootTable
,
412 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryRegion
418 UINT64 RemainingRegionLength
;
420 UINT64
*LastBlockEntry
;
421 UINT64 BlockEntrySize
;
424 // Ensure the Length is aligned on 4KB boundary
425 if ((MemoryRegion
->Length
== 0) || ((MemoryRegion
->Length
& (SIZE_4KB
- 1)) != 0)) {
426 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER
);
427 return RETURN_INVALID_PARAMETER
;
430 // Variable initialization
431 Attributes
= ArmMemoryAttributeToPageAttribute (MemoryRegion
->Attributes
) | TT_AF
;
432 RemainingRegionLength
= MemoryRegion
->Length
;
433 RegionStart
= MemoryRegion
->VirtualBase
;
436 // Get the first Block Entry that matches the Virtual Address and also the information on the Table Descriptor
437 // such as the the size of the Block Entry and the address of the last BlockEntry of the Table Descriptor
438 BlockEntrySize
= RemainingRegionLength
;
439 BlockEntry
= GetBlockEntryListFromAddress (RootTable
, RegionStart
, &TableLevel
, &BlockEntrySize
, &LastBlockEntry
);
440 if (BlockEntry
== NULL
) {
441 // GetBlockEntryListFromAddress() return NULL when it fails to allocate new pages from the Translation Tables
442 return RETURN_OUT_OF_RESOURCES
;
445 if (TableLevel
!= 3) {
446 Type
= TT_TYPE_BLOCK_ENTRY
;
448 Type
= TT_TYPE_BLOCK_ENTRY_LEVEL3
;
452 // Fill the Block Entry with attribute and output block address
453 *BlockEntry
= (RegionStart
& TT_ADDRESS_MASK_BLOCK_ENTRY
) | Attributes
| Type
;
455 // Go to the next BlockEntry
456 RegionStart
+= BlockEntrySize
;
457 RemainingRegionLength
-= BlockEntrySize
;
460 // Break the inner loop when next block is a table
461 // Rerun GetBlockEntryListFromAddress to avoid page table memory leak
462 if (TableLevel
!= 3 &&
463 (*BlockEntry
& TT_TYPE_MASK
) == TT_TYPE_TABLE_ENTRY
) {
466 } while ((RemainingRegionLength
>= BlockEntrySize
) && (BlockEntry
<= LastBlockEntry
));
467 } while (RemainingRegionLength
!= 0);
469 return RETURN_SUCCESS
;
473 SetMemoryAttributes (
474 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
476 IN UINT64 Attributes
,
477 IN EFI_PHYSICAL_ADDRESS VirtualMask
480 RETURN_STATUS Status
;
481 ARM_MEMORY_REGION_DESCRIPTOR MemoryRegion
;
482 UINT64
*TranslationTable
;
484 MemoryRegion
.PhysicalBase
= BaseAddress
;
485 MemoryRegion
.VirtualBase
= BaseAddress
;
486 MemoryRegion
.Length
= Length
;
487 MemoryRegion
.Attributes
= GcdAttributeToArmAttribute (Attributes
);
489 TranslationTable
= ArmGetTTBR0BaseAddress ();
491 Status
= FillTranslationTable (TranslationTable
, &MemoryRegion
);
492 if (RETURN_ERROR (Status
)) {
496 // Invalidate all TLB entries so changes are synced
499 return RETURN_SUCCESS
;
505 IN ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTable
,
506 OUT VOID
**TranslationTableBase OPTIONAL
,
507 OUT UINTN
*TranslationTableSize OPTIONAL
510 VOID
* TranslationTable
;
511 UINTN TranslationTablePageCount
;
512 UINT32 TranslationTableAttribute
;
513 ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTableEntry
;
517 UINTN RootTableEntryCount
;
519 RETURN_STATUS Status
;
521 if(MemoryTable
== NULL
) {
522 ASSERT (MemoryTable
!= NULL
);
523 return RETURN_INVALID_PARAMETER
;
526 // Identify the highest address of the memory table
527 MaxAddress
= MemoryTable
->PhysicalBase
+ MemoryTable
->Length
- 1;
528 MemoryTableEntry
= MemoryTable
;
529 while (MemoryTableEntry
->Length
!= 0) {
530 TopAddress
= MemoryTableEntry
->PhysicalBase
+ MemoryTableEntry
->Length
- 1;
531 if (TopAddress
> MaxAddress
) {
532 MaxAddress
= TopAddress
;
537 // Lookup the Table Level to get the information
538 LookupAddresstoRootTable (MaxAddress
, &T0SZ
, &RootTableEntryCount
);
541 // Set TCR that allows us to retrieve T0SZ in the subsequent functions
543 // Ideally we will be running at EL2, but should support EL1 as well.
544 // UEFI should not run at EL3.
545 if (ArmReadCurrentEL () == AARCH64_EL2
) {
546 //Note: Bits 23 and 31 are reserved(RES1) bits in TCR_EL2
547 TCR
= T0SZ
| (1UL << 31) | (1UL << 23) | TCR_TG0_4KB
;
549 // Set the Physical Address Size using MaxAddress
550 if (MaxAddress
< SIZE_4GB
) {
552 } else if (MaxAddress
< SIZE_64GB
) {
554 } else if (MaxAddress
< SIZE_1TB
) {
556 } else if (MaxAddress
< SIZE_4TB
) {
558 } else if (MaxAddress
< SIZE_16TB
) {
560 } else if (MaxAddress
< SIZE_256TB
) {
563 DEBUG ((EFI_D_ERROR
, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress
));
564 ASSERT (0); // Bigger than 48-bit memory space are not supported
565 return RETURN_UNSUPPORTED
;
567 } else if (ArmReadCurrentEL () == AARCH64_EL1
) {
568 TCR
= T0SZ
| TCR_TG0_4KB
;
570 // Set the Physical Address Size using MaxAddress
571 if (MaxAddress
< SIZE_4GB
) {
573 } else if (MaxAddress
< SIZE_64GB
) {
575 } else if (MaxAddress
< SIZE_1TB
) {
577 } else if (MaxAddress
< SIZE_4TB
) {
579 } else if (MaxAddress
< SIZE_16TB
) {
581 } else if (MaxAddress
< SIZE_256TB
) {
582 TCR
|= TCR_IPS_256TB
;
584 DEBUG ((EFI_D_ERROR
, "ArmConfigureMmu: The MaxAddress 0x%lX is not supported by this MMU configuration.\n", MaxAddress
));
585 ASSERT (0); // Bigger than 48-bit memory space are not supported
586 return RETURN_UNSUPPORTED
;
589 ASSERT (0); // UEFI is only expected to run at EL2 and EL1, not EL3.
590 return RETURN_UNSUPPORTED
;
596 // Allocate pages for translation table
597 TranslationTablePageCount
= EFI_SIZE_TO_PAGES(RootTableEntryCount
* sizeof(UINT64
));
598 TranslationTable
= (UINT64
*)AllocateAlignedPages (TranslationTablePageCount
, TT_ALIGNMENT_DESCRIPTION_TABLE
);
599 if (TranslationTable
== NULL
) {
600 return RETURN_OUT_OF_RESOURCES
;
602 // We set TTBR0 just after allocating the table to retrieve its location from the subsequent
603 // functions without needing to pass this value across the functions. The MMU is only enabled
604 // after the translation tables are populated.
605 ArmSetTTBR0 (TranslationTable
);
607 if (TranslationTableBase
!= NULL
) {
608 *TranslationTableBase
= TranslationTable
;
611 if (TranslationTableSize
!= NULL
) {
612 *TranslationTableSize
= RootTableEntryCount
* sizeof(UINT64
);
615 ZeroMem (TranslationTable
, RootTableEntryCount
* sizeof(UINT64
));
617 // Disable MMU and caches. ArmDisableMmu() also invalidates the TLBs
619 ArmDisableDataCache ();
620 ArmDisableInstructionCache ();
622 // Make sure nothing sneaked into the cache
623 ArmCleanInvalidateDataCache ();
624 ArmInvalidateInstructionCache ();
626 TranslationTableAttribute
= TT_ATTR_INDX_INVALID
;
627 while (MemoryTable
->Length
!= 0) {
628 // Find the memory attribute for the Translation Table
629 if (((UINTN
)TranslationTable
>= MemoryTable
->PhysicalBase
) &&
630 ((UINTN
)TranslationTable
<= MemoryTable
->PhysicalBase
- 1 + MemoryTable
->Length
)) {
631 TranslationTableAttribute
= MemoryTable
->Attributes
;
634 Status
= FillTranslationTable (TranslationTable
, MemoryTable
);
635 if (RETURN_ERROR (Status
)) {
636 goto FREE_TRANSLATION_TABLE
;
641 // Translate the Memory Attributes into Translation Table Register Attributes
642 if ((TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
) ||
643 (TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_UNCACHED_UNBUFFERED
)) {
644 TCR
|= TCR_SH_NON_SHAREABLE
| TCR_RGN_OUTER_NON_CACHEABLE
| TCR_RGN_INNER_NON_CACHEABLE
;
645 } else if ((TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
) ||
646 (TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_BACK
)) {
647 TCR
|= TCR_SH_INNER_SHAREABLE
| TCR_RGN_OUTER_WRITE_BACK_ALLOC
| TCR_RGN_INNER_WRITE_BACK_ALLOC
;
648 } else if ((TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH
) ||
649 (TranslationTableAttribute
== ARM_MEMORY_REGION_ATTRIBUTE_NONSECURE_WRITE_THROUGH
)) {
650 TCR
|= TCR_SH_NON_SHAREABLE
| TCR_RGN_OUTER_WRITE_THROUGH
| TCR_RGN_INNER_WRITE_THROUGH
;
652 // If we failed to find a mapping that contains the root translation table then it probably means the translation table
653 // is not mapped in the given memory map.
655 Status
= RETURN_UNSUPPORTED
;
656 goto FREE_TRANSLATION_TABLE
;
659 // Set again TCR after getting the Translation Table attributes
662 ArmSetMAIR (MAIR_ATTR(TT_ATTR_INDX_DEVICE_MEMORY
, MAIR_ATTR_DEVICE_MEMORY
) | // mapped to EFI_MEMORY_UC
663 MAIR_ATTR(TT_ATTR_INDX_MEMORY_NON_CACHEABLE
, MAIR_ATTR_NORMAL_MEMORY_NON_CACHEABLE
) | // mapped to EFI_MEMORY_WC
664 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_THROUGH
, MAIR_ATTR_NORMAL_MEMORY_WRITE_THROUGH
) | // mapped to EFI_MEMORY_WT
665 MAIR_ATTR(TT_ATTR_INDX_MEMORY_WRITE_BACK
, MAIR_ATTR_NORMAL_MEMORY_WRITE_BACK
)); // mapped to EFI_MEMORY_WB
667 ArmDisableAlignmentCheck ();
668 ArmEnableInstructionCache ();
669 ArmEnableDataCache ();
672 return RETURN_SUCCESS
;
674 FREE_TRANSLATION_TABLE
:
675 FreePages (TranslationTable
, TranslationTablePageCount
);