3 Copyright (c) 2009, Hewlett-Packard Company. All rights reserved.<BR>
4 Portions copyright (c) 2010, Apple Inc. All rights reserved.<BR>
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.
18 //FIXME: Remove this ARMv7 specific header
19 #include <Chipset/ArmV7.h>
21 // First Level Descriptors
22 typedef UINT32 ARM_FIRST_LEVEL_DESCRIPTOR
;
24 // Second Level Descriptors
25 typedef UINT32 ARM_PAGE_TABLE_ENTRY
;
28 SectionToGcdAttributes (
29 IN UINT32 SectionAttributes
,
30 OUT UINT64
*GcdAttributes
35 // determine cacheability attributes
36 switch(SectionAttributes
& TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK
) {
37 case TT_DESCRIPTOR_SECTION_CACHE_POLICY_STRONGLY_ORDERED
:
38 *GcdAttributes
|= EFI_MEMORY_UC
;
40 case TT_DESCRIPTOR_SECTION_CACHE_POLICY_SHAREABLE_DEVICE
:
41 *GcdAttributes
|= EFI_MEMORY_UC
;
43 case TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC
:
44 *GcdAttributes
|= EFI_MEMORY_WT
;
46 case TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_BACK_NO_ALLOC
:
47 *GcdAttributes
|= EFI_MEMORY_WB
;
49 case TT_DESCRIPTOR_SECTION_CACHE_POLICY_NON_CACHEABLE
:
50 *GcdAttributes
|= EFI_MEMORY_WC
;
52 case TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_BACK_ALLOC
:
53 *GcdAttributes
|= EFI_MEMORY_WB
;
55 case TT_DESCRIPTOR_SECTION_CACHE_POLICY_NON_SHAREABLE_DEVICE
:
56 *GcdAttributes
|= EFI_MEMORY_UC
;
59 return EFI_UNSUPPORTED
;
62 // determine protection attributes
63 switch(SectionAttributes
& TT_DESCRIPTOR_SECTION_AP_MASK
) {
64 case TT_DESCRIPTOR_SECTION_AP_NO_NO
: // no read, no write
65 //*GcdAttributes |= EFI_MEMORY_WP | EFI_MEMORY_RP;
68 case TT_DESCRIPTOR_SECTION_AP_RW_NO
:
69 case TT_DESCRIPTOR_SECTION_AP_RW_RW
:
70 // normal read/write access, do not add additional attributes
73 // read only cases map to write-protect
74 case TT_DESCRIPTOR_SECTION_AP_RO_NO
:
75 case TT_DESCRIPTOR_SECTION_AP_RO_RO
:
76 *GcdAttributes
|= EFI_MEMORY_WP
;
80 return EFI_UNSUPPORTED
;
83 // now process eXectue Never attribute
84 if ((SectionAttributes
& TT_DESCRIPTOR_SECTION_XN_MASK
) != 0 ) {
85 *GcdAttributes
|= EFI_MEMORY_XP
;
92 Searches memory descriptors covered by given memory range.
94 This function searches into the Gcd Memory Space for descriptors
95 (from StartIndex to EndIndex) that contains the memory range
96 specified by BaseAddress and Length.
98 @param MemorySpaceMap Gcd Memory Space Map as array.
99 @param NumberOfDescriptors Number of descriptors in map.
100 @param BaseAddress BaseAddress for the requested range.
101 @param Length Length for the requested range.
102 @param StartIndex Start index into the Gcd Memory Space Map.
103 @param EndIndex End index into the Gcd Memory Space Map.
105 @retval EFI_SUCCESS Search successfully.
106 @retval EFI_NOT_FOUND The requested descriptors does not exist.
110 SearchGcdMemorySpaces (
111 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
,
112 IN UINTN NumberOfDescriptors
,
113 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
115 OUT UINTN
*StartIndex
,
123 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
124 if (BaseAddress
>= MemorySpaceMap
[Index
].BaseAddress
&&
125 BaseAddress
< MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) {
128 if (BaseAddress
+ Length
- 1 >= MemorySpaceMap
[Index
].BaseAddress
&&
129 BaseAddress
+ Length
- 1 < MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) {
134 return EFI_NOT_FOUND
;
139 Sets the attributes for a specified range in Gcd Memory Space Map.
141 This function sets the attributes for a specified range in
142 Gcd Memory Space Map.
144 @param MemorySpaceMap Gcd Memory Space Map as array
145 @param NumberOfDescriptors Number of descriptors in map
146 @param BaseAddress BaseAddress for the range
147 @param Length Length for the range
148 @param Attributes Attributes to set
150 @retval EFI_SUCCESS Memory attributes set successfully
151 @retval EFI_NOT_FOUND The specified range does not exist in Gcd Memory Space
155 SetGcdMemorySpaceAttributes (
156 IN EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
,
157 IN UINTN NumberOfDescriptors
,
158 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
167 EFI_PHYSICAL_ADDRESS RegionStart
;
171 // Get all memory descriptors covered by the memory range
173 Status
= SearchGcdMemorySpaces (
181 if (EFI_ERROR (Status
)) {
186 // Go through all related descriptors and set attributes accordingly
188 for (Index
= StartIndex
; Index
<= EndIndex
; Index
++) {
189 if (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
193 // Calculate the start and end address of the overlapping range
195 if (BaseAddress
>= MemorySpaceMap
[Index
].BaseAddress
) {
196 RegionStart
= BaseAddress
;
198 RegionStart
= MemorySpaceMap
[Index
].BaseAddress
;
200 if (BaseAddress
+ Length
- 1 < MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) {
201 RegionLength
= BaseAddress
+ Length
- RegionStart
;
203 RegionLength
= MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- RegionStart
;
206 // Set memory attributes according to MTRR attribute and the original attribute of descriptor
208 gDS
->SetMemorySpaceAttributes (
211 (MemorySpaceMap
[Index
].Attributes
& ~EFI_MEMORY_CACHETYPE_MASK
) | (MemorySpaceMap
[Index
].Capabilities
& Attributes
)
221 IN EFI_CPU_ARCH_PROTOCOL
*CpuProtocol
227 UINT32 SectionAttributes
;
228 EFI_PHYSICAL_ADDRESS NextRegionBase
;
229 UINT64 NextRegionLength
;
230 UINT64 GcdAttributes
;
231 UINT32 NextRegionAttributes
= 0;
232 volatile ARM_FIRST_LEVEL_DESCRIPTOR
*FirstLevelTable
;
233 UINTN NumberOfDescriptors
;
234 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
237 DEBUG ((EFI_D_PAGE
, "SyncCacheConfig()\n"));
239 // This code assumes MMU is enabled and filed with section translations
240 ASSERT (ArmMmuEnabled ());
243 // Get the memory space map from GCD
245 MemorySpaceMap
= NULL
;
246 Status
= gDS
->GetMemorySpaceMap (&NumberOfDescriptors
, &MemorySpaceMap
);
247 ASSERT_EFI_ERROR (Status
);
250 // The GCD implementation maintains its own copy of the state of memory space attributes. GCD needs
251 // to know what the initial memory space attributes are. The CPU Arch. Protocol does not provide a
252 // GetMemoryAttributes function for GCD to get this so we must resort to calling GCD (as if we were
253 // a client) to update its copy of the attributes. This is bad architecture and should be replaced
254 // with a way for GCD to query the CPU Arch. driver of the existing memory space attributes instead.
256 // obtain page table base
257 FirstLevelTable
= (ARM_FIRST_LEVEL_DESCRIPTOR
*)(ArmGetTTBR0BaseAddress ());
260 // iterate through each 1MB descriptor
261 NextRegionBase
= NextRegionLength
= 0;
262 for (i
=0; i
< TRANSLATION_TABLE_SECTION_COUNT
; i
++) {
264 // obtain existing descriptor and make sure it contains a valid Base Address even if it is a fault section
265 Descriptor
= FirstLevelTable
[i
] | TT_DESCRIPTOR_SECTION_BASE_ADDRESS(i
<< TT_DESCRIPTOR_SECTION_BASE_SHIFT
);
267 // extract attributes (cacheability and permissions)
268 SectionAttributes
= Descriptor
& (TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK
| TT_DESCRIPTOR_SECTION_AP_MASK
);
270 // do we already have an existing region (or are we about to finish)?
271 // Skip the first entry, and make sure we close on the last entry
272 if ( (NextRegionLength
> 0) || (i
== (TRANSLATION_TABLE_SECTION_COUNT
-1)) ) {
273 // attributes are changing, update attributes in GCD
274 if (SectionAttributes
!= NextRegionAttributes
) {
276 // convert section entry attributes to GCD bitmask
277 Status
= SectionToGcdAttributes (NextRegionAttributes
, &GcdAttributes
);
278 ASSERT_EFI_ERROR (Status
);
280 // update GCD with these changes (this will recurse into our own CpuSetMemoryAttributes below which is OK)
281 SetGcdMemorySpaceAttributes (MemorySpaceMap
, NumberOfDescriptors
, NextRegionBase
, NextRegionLength
, GcdAttributes
);
284 // start on a new region
285 NextRegionLength
= 0;
286 NextRegionBase
= TT_DESCRIPTOR_SECTION_BASE_ADDRESS(Descriptor
);
290 // starting a new region?
291 if (NextRegionLength
== 0) {
292 NextRegionAttributes
= SectionAttributes
;
295 NextRegionLength
+= TT_DESCRIPTOR_SECTION_SIZE
;
297 } // section entry loop
306 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
308 IN UINT64 Attributes
,
309 IN EFI_PHYSICAL_ADDRESS VirtualMask
315 UINT32 FirstLevelIdx
;
317 UINT32 NumPageEntries
;
320 UINT32 PageTableIndex
;
321 UINT32 PageTableEntry
;
322 UINT32 CurrentPageTableEntry
;
325 volatile ARM_FIRST_LEVEL_DESCRIPTOR
*FirstLevelTable
;
326 volatile ARM_PAGE_TABLE_ENTRY
*PageTable
;
328 Status
= EFI_SUCCESS
;
330 // EntryMask: bitmask of values to change (1 = change this value, 0 = leave alone)
331 // EntryValue: values at bit positions specified by EntryMask
332 EntryMask
= TT_DESCRIPTOR_PAGE_TYPE_MASK
;
333 EntryValue
= TT_DESCRIPTOR_PAGE_TYPE_PAGE
;
334 // Although the PI spec is unclear on this the GCD guarantees that only
335 // one Attribute bit is set at a time, so we can safely use a switch statement
336 switch (Attributes
) {
338 // modify cacheability attributes
339 EntryMask
|= TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK
;
340 if (FeaturePcdGet(PcdEfiUncachedMemoryToStronglyOrdered
)) {
341 \s\s\s\s
// map to strongly ordered
342 \s\s\s\s EntryValue
|= TT_DESCRIPTOR_PAGE_CACHE_POLICY_STRONGLY_ORDERED
; // TEX[2:0] = 0, C=0, B=0
344 \s\s
// map to normal non-cachable
345 \s\s EntryValue
|= TT_DESCRIPTOR_PAGE_CACHE_POLICY_NON_CACHEABLE
; // TEX [2:0]= 001 = 0x2, B=0, C=0
350 // modify cacheability attributes
351 EntryMask
|= TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK
;
352 // map to normal non-cachable
353 EntryValue
|= TT_DESCRIPTOR_PAGE_CACHE_POLICY_NON_CACHEABLE
; // TEX [2:0]= 001 = 0x2, B=0, C=0
357 // modify cacheability attributes
358 EntryMask
|= TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK
;
359 // write through with no-allocate
360 EntryValue
|= TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC
; // TEX [2:0] = 0, C=1, B=0
364 // modify cacheability attributes
365 EntryMask
|= TT_DESCRIPTOR_PAGE_CACHE_POLICY_MASK
;
366 // write back (with allocate)
367 EntryValue
|= TT_DESCRIPTOR_PAGE_CACHE_POLICY_WRITE_BACK_ALLOC
; // TEX [2:0] = 001, C=1, B=1
373 // cannot be implemented UEFI definition unclear for ARM
374 // Cause a page fault if these ranges are accessed.
375 EntryValue
= TT_DESCRIPTOR_PAGE_TYPE_FAULT
;
376 DEBUG ((EFI_D_PAGE
, "SetMemoryAttributes(): setting page %lx with unsupported attribute %x will page fault on access\n", BaseAddress
, Attributes
));
380 return EFI_UNSUPPORTED
;
383 // obtain page table base
384 FirstLevelTable
= (ARM_FIRST_LEVEL_DESCRIPTOR
*)ArmGetTTBR0BaseAddress ();
386 // calculate number of 4KB page table entries to change
387 NumPageEntries
= Length
/SIZE_4KB
;
389 // iterate for the number of 4KB pages to change
391 for(p
=0; p
<NumPageEntries
; p
++) {
392 // calculate index into first level translation table for page table value
394 FirstLevelIdx
= TT_DESCRIPTOR_SECTION_BASE_ADDRESS(BaseAddress
+ Offset
) >> TT_DESCRIPTOR_SECTION_BASE_SHIFT
;
395 ASSERT (FirstLevelIdx
< TRANSLATION_TABLE_SECTION_COUNT
);
397 // read the descriptor from the first level page table
398 Descriptor
= FirstLevelTable
[FirstLevelIdx
];
400 // does this descriptor need to be converted from section entry to 4K pages?
401 if (!TT_DESCRIPTOR_SECTION_TYPE_IS_PAGE_TABLE(Descriptor
)) {
402 Status
= ConvertSectionToPages (FirstLevelIdx
<< TT_DESCRIPTOR_SECTION_BASE_SHIFT
);
403 if (EFI_ERROR(Status
)) {
408 // re-read descriptor
409 Descriptor
= FirstLevelTable
[FirstLevelIdx
];
412 // obtain page table base address
413 PageTable
= (ARM_PAGE_TABLE_ENTRY
*)TT_DESCRIPTOR_PAGE_BASE_ADDRESS(Descriptor
);
415 // calculate index into the page table
416 PageTableIndex
= ((BaseAddress
+ Offset
) & TT_DESCRIPTOR_PAGE_INDEX_MASK
) >> TT_DESCRIPTOR_PAGE_BASE_SHIFT
;
417 ASSERT (PageTableIndex
< TRANSLATION_TABLE_PAGE_COUNT
);
420 CurrentPageTableEntry
= PageTable
[PageTableIndex
];
422 // mask off appropriate fields
423 PageTableEntry
= CurrentPageTableEntry
& ~EntryMask
;
425 // mask in new attributes and/or permissions
426 PageTableEntry
|= EntryValue
;
428 if (VirtualMask
!= 0) {
429 // Make this virtual address point at a physical page
430 PageTableEntry
&= ~VirtualMask
;
433 if (CurrentPageTableEntry
!= PageTableEntry
) {
434 Mva
= (VOID
*)(UINTN
)((((UINTN
)FirstLevelIdx
) << TT_DESCRIPTOR_SECTION_BASE_SHIFT
) + (PageTableIndex
<< TT_DESCRIPTOR_PAGE_BASE_SHIFT
));
435 if ((CurrentPageTableEntry
& TT_DESCRIPTOR_PAGE_CACHEABLE_MASK
) == TT_DESCRIPTOR_PAGE_CACHEABLE_MASK
) {
436 // The current section mapping is cacheable so Clean/Invalidate the MVA of the page
437 // Note assumes switch(Attributes), not ARMv7 possibilities
438 WriteBackInvalidateDataCacheRange (Mva
, SIZE_4KB
);
441 // Only need to update if we are changing the entry
442 PageTable
[PageTableIndex
] = PageTableEntry
;
443 ArmUpdateTranslationTableEntry ((VOID
*)&PageTable
[PageTableIndex
], Mva
);
446 Status
= EFI_SUCCESS
;
449 } // end first level translation table loop
457 UpdateSectionEntries (
458 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
460 IN UINT64 Attributes
,
461 IN EFI_PHYSICAL_ADDRESS VirtualMask
464 EFI_STATUS Status
= EFI_SUCCESS
;
467 UINT32 FirstLevelIdx
;
470 UINT32 CurrentDescriptor
;
473 volatile ARM_FIRST_LEVEL_DESCRIPTOR
*FirstLevelTable
;
475 // EntryMask: bitmask of values to change (1 = change this value, 0 = leave alone)
476 // EntryValue: values at bit positions specified by EntryMask
478 // Make sure we handle a section range that is unmapped
479 EntryMask
= TT_DESCRIPTOR_SECTION_TYPE_MASK
;
480 EntryValue
= TT_DESCRIPTOR_SECTION_TYPE_SECTION
;
482 // Although the PI spec is unclear on this the GCD guarantees that only
483 // one Attribute bit is set at a time, so we can safely use a switch statement
486 // modify cacheability attributes
487 EntryMask
|= TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK
;
488 if (FeaturePcdGet(PcdEfiUncachedMemoryToStronglyOrdered
)) {
489 \s\s\s\s
// map to strongly ordered
490 \s\s\s\s EntryValue
|= TT_DESCRIPTOR_SECTION_CACHE_POLICY_STRONGLY_ORDERED
; // TEX[2:0] = 0, C=0, B=0
492 \s\s
// map to normal non-cachable
493 \s\s EntryValue
|= TT_DESCRIPTOR_SECTION_CACHE_POLICY_NON_CACHEABLE
; // TEX [2:0]= 001 = 0x2, B=0, C=0
498 // modify cacheability attributes
499 EntryMask
|= TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK
;
500 // map to normal non-cachable
501 EntryValue
|= TT_DESCRIPTOR_SECTION_CACHE_POLICY_NON_CACHEABLE
; // TEX [2:0]= 001 = 0x2, B=0, C=0
505 // modify cacheability attributes
506 EntryMask
|= TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK
;
507 // write through with no-allocate
508 EntryValue
|= TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC
; // TEX [2:0] = 0, C=1, B=0
512 // modify cacheability attributes
513 EntryMask
|= TT_DESCRIPTOR_SECTION_CACHE_POLICY_MASK
;
514 // write back (with allocate)
515 EntryValue
|= TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_BACK_ALLOC
; // TEX [2:0] = 001, C=1, B=1
522 // cannot be implemented UEFI definition unclear for ARM
523 // Cause a page fault if these ranges are accessed.
524 EntryValue
= TT_DESCRIPTOR_SECTION_TYPE_FAULT
;
525 DEBUG ((EFI_D_PAGE
, "SetMemoryAttributes(): setting section %lx with unsupported attribute %x will page fault on access\n", BaseAddress
, Attributes
));
530 return EFI_UNSUPPORTED
;
533 // obtain page table base
534 FirstLevelTable
= (ARM_FIRST_LEVEL_DESCRIPTOR
*)ArmGetTTBR0BaseAddress ();
536 // calculate index into first level translation table for start of modification
537 FirstLevelIdx
= TT_DESCRIPTOR_SECTION_BASE_ADDRESS(BaseAddress
) >> TT_DESCRIPTOR_SECTION_BASE_SHIFT
;
538 ASSERT (FirstLevelIdx
< TRANSLATION_TABLE_SECTION_COUNT
);
540 // calculate number of 1MB first level entries this applies to
541 NumSections
= Length
/ TT_DESCRIPTOR_SECTION_SIZE
;
543 // iterate through each descriptor
544 for(i
=0; i
<NumSections
; i
++) {
545 CurrentDescriptor
= FirstLevelTable
[FirstLevelIdx
+ i
];
547 // has this descriptor already been coverted to pages?
548 if (TT_DESCRIPTOR_SECTION_TYPE_IS_PAGE_TABLE(CurrentDescriptor
)) {
549 // forward this 1MB range to page table function instead
550 Status
= UpdatePageEntries ((FirstLevelIdx
+ i
) << TT_DESCRIPTOR_SECTION_BASE_SHIFT
, TT_DESCRIPTOR_SECTION_SIZE
, Attributes
, VirtualMask
);
552 // still a section entry
554 // mask off appropriate fields
555 Descriptor
= CurrentDescriptor
& ~EntryMask
;
557 // mask in new attributes and/or permissions
558 Descriptor
|= EntryValue
;
559 if (VirtualMask
!= 0) {
560 Descriptor
&= ~VirtualMask
;
563 if (CurrentDescriptor
!= Descriptor
) {
564 Mva
= (VOID
*)(UINTN
)(((UINTN
)FirstLevelTable
) << TT_DESCRIPTOR_SECTION_BASE_SHIFT
);
565 if ((CurrentDescriptor
& TT_DESCRIPTOR_SECTION_CACHEABLE_MASK
) == TT_DESCRIPTOR_SECTION_CACHEABLE_MASK
) {
566 // The current section mapping is cacheable so Clean/Invalidate the MVA of the section
567 // Note assumes switch(Attributes), not ARMv7 possabilities
568 WriteBackInvalidateDataCacheRange (Mva
, SIZE_1MB
);
571 // Only need to update if we are changing the descriptor
572 FirstLevelTable
[FirstLevelIdx
+ i
] = Descriptor
;
573 ArmUpdateTranslationTableEntry ((VOID
*)&FirstLevelTable
[FirstLevelIdx
+ i
], Mva
);
576 Status
= EFI_SUCCESS
;
584 ConvertSectionToPages (
585 IN EFI_PHYSICAL_ADDRESS BaseAddress
589 EFI_PHYSICAL_ADDRESS PageTableAddr
;
590 UINT32 FirstLevelIdx
;
591 UINT32 SectionDescriptor
;
592 UINT32 PageTableDescriptor
;
593 UINT32 PageDescriptor
;
596 volatile ARM_FIRST_LEVEL_DESCRIPTOR
*FirstLevelTable
;
597 volatile ARM_PAGE_TABLE_ENTRY
*PageTable
;
599 DEBUG ((EFI_D_PAGE
, "Converting section at 0x%x to pages\n", (UINTN
)BaseAddress
));
601 // obtain page table base
602 FirstLevelTable
= (ARM_FIRST_LEVEL_DESCRIPTOR
*)ArmGetTTBR0BaseAddress ();
604 // calculate index into first level translation table for start of modification
605 FirstLevelIdx
= TT_DESCRIPTOR_SECTION_BASE_ADDRESS(BaseAddress
) >> TT_DESCRIPTOR_SECTION_BASE_SHIFT
;
606 ASSERT (FirstLevelIdx
< TRANSLATION_TABLE_SECTION_COUNT
);
608 // get section attributes and convert to page attributes
609 SectionDescriptor
= FirstLevelTable
[FirstLevelIdx
];
610 PageDescriptor
= TT_DESCRIPTOR_PAGE_TYPE_PAGE
;
611 PageDescriptor
|= TT_DESCRIPTOR_CONVERT_TO_PAGE_CACHE_POLICY(SectionDescriptor
,0);
612 PageDescriptor
|= TT_DESCRIPTOR_CONVERT_TO_PAGE_AP(SectionDescriptor
);
613 PageDescriptor
|= TT_DESCRIPTOR_CONVERT_TO_PAGE_XN(SectionDescriptor
,0);
614 PageDescriptor
|= TT_DESCRIPTOR_CONVERT_TO_PAGE_NG(SectionDescriptor
);
615 PageDescriptor
|= TT_DESCRIPTOR_CONVERT_TO_PAGE_S(SectionDescriptor
);
617 // allocate a page table for the 4KB entries (we use up a full page even though we only need 1KB)
618 Status
= gBS
->AllocatePages (AllocateAnyPages
, EfiBootServicesData
, 1, &PageTableAddr
);
619 if (EFI_ERROR(Status
)) {
623 PageTable
= (volatile ARM_PAGE_TABLE_ENTRY
*)(UINTN
)PageTableAddr
;
625 // write the page table entries out
626 for (i
=0; i
< TRANSLATION_TABLE_PAGE_COUNT
; i
++) {
627 PageTable
[i
] = TT_DESCRIPTOR_PAGE_BASE_ADDRESS(BaseAddress
+ (i
<< 12)) | PageDescriptor
;
630 // flush d-cache so descriptors make it back to uncached memory for subsequent table walks
631 WriteBackInvalidateDataCacheRange ((VOID
*)(UINTN
)PageTableAddr
, SIZE_4KB
);
633 // formulate page table entry, Domain=0, NS=0
634 PageTableDescriptor
= (((UINTN
)PageTableAddr
) & TT_DESCRIPTOR_SECTION_PAGETABLE_ADDRESS_MASK
) | TT_DESCRIPTOR_SECTION_TYPE_PAGE_TABLE
;
636 // write the page table entry out, repalcing section entry
637 FirstLevelTable
[FirstLevelIdx
] = PageTableDescriptor
;
645 SetMemoryAttributes (
646 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
648 IN UINT64 Attributes
,
649 IN EFI_PHYSICAL_ADDRESS VirtualMask
654 if(((BaseAddress
& 0xFFFFF) == 0) && ((Length
& 0xFFFFF) == 0)) {
655 // is the base and length a multiple of 1 MB?
656 DEBUG ((EFI_D_PAGE
, "SetMemoryAttributes(): MMU section 0x%x length 0x%x to %lx\n", (UINTN
)BaseAddress
, (UINTN
)Length
, Attributes
));
657 Status
= UpdateSectionEntries (BaseAddress
, Length
, Attributes
, VirtualMask
);
659 // base and/or length is not a multiple of 1 MB
660 DEBUG ((EFI_D_PAGE
, "SetMemoryAttributes(): MMU page 0x%x length 0x%x to %lx\n", (UINTN
)BaseAddress
, (UINTN
)Length
, Attributes
));
661 Status
= UpdatePageEntries (BaseAddress
, Length
, Attributes
, VirtualMask
);
664 // flush d-cache so descriptors make it back to uncached memory for subsequent table walks
665 // flush and invalidate pages
666 ArmCleanInvalidateDataCache ();
668 ArmInvalidateInstructionCache ();
670 // invalidate all TLB entries so changes are synced
678 This function modifies the attributes for the memory region specified by BaseAddress and
679 Length from their current attributes to the attributes specified by Attributes.
681 @param This The EFI_CPU_ARCH_PROTOCOL instance.
682 @param BaseAddress The physical address that is the start address of a memory region.
683 @param Length The size in bytes of the memory region.
684 @param Attributes The bit mask of attributes to set for the memory region.
686 @retval EFI_SUCCESS The attributes were set for the memory region.
687 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by
688 BaseAddress and Length cannot be modified.
689 @retval EFI_INVALID_PARAMETER Length is zero.
690 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
691 the memory resource range.
692 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
693 resource range specified by BaseAddress and Length.
694 The bit mask of attributes is not support for the memory resource
695 range specified by BaseAddress and Length.
700 CpuSetMemoryAttributes (
701 IN EFI_CPU_ARCH_PROTOCOL
*This
,
702 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
707 DEBUG ((EFI_D_PAGE
, "SetMemoryAttributes(%lx, %lx, %lx)\n", BaseAddress
, Length
, Attributes
));
708 if ( ((BaseAddress
& (SIZE_4KB
-1)) != 0) || ((Length
& (SIZE_4KB
-1)) != 0)){
709 // minimum granularity is SIZE_4KB (4KB on ARM)
710 DEBUG ((EFI_D_PAGE
, "SetMemoryAttributes(%lx, %lx, %lx): minimum ganularity is SIZE_4KB\n", BaseAddress
, Length
, Attributes
));
711 return EFI_UNSUPPORTED
;
714 return SetMemoryAttributes (BaseAddress
, Length
, Attributes
, 0);
720 // Add a new protocol to support
725 CpuConvertPagesToUncachedVirtualAddress (
726 IN VIRTUAL_UNCACHED_PAGES_PROTOCOL
*This
,
727 IN EFI_PHYSICAL_ADDRESS Address
,
729 IN EFI_PHYSICAL_ADDRESS VirtualMask
,
730 OUT UINT64
*Attributes OPTIONAL
734 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor
;
737 if (Attributes
!= NULL
) {
738 Status
= gDS
->GetMemorySpaceDescriptor (Address
, &GcdDescriptor
);
739 if (!EFI_ERROR (Status
)) {
740 *Attributes
= GcdDescriptor
.Attributes
;
745 // Make this address range page fault if accessed. If it is a DMA buffer than this would
746 // be the PCI address. Code should always use the CPU address, and we will or in VirtualMask
749 Status
= SetMemoryAttributes (Address
, Length
, EFI_MEMORY_WP
, 0);
750 if (!EFI_ERROR (Status
)) {
751 Status
= SetMemoryAttributes (Address
| VirtualMask
, Length
, EFI_MEMORY_UC
, VirtualMask
);
754 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "ConvertPagesToUncachedVirtualAddress()\n Unmapped 0x%08lx Mapped 0x%08lx 0x%x bytes\n", Address
, Address
| VirtualMask
, Length
));
763 IN VIRTUAL_UNCACHED_PAGES_PROTOCOL
*This
,
764 IN EFI_PHYSICAL_ADDRESS Address
,
766 IN EFI_PHYSICAL_ADDRESS VirtualMask
,
772 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "CpuReconvertPages(%lx, %x, %lx, %lx)\n", Address
, Length
, VirtualMask
, Attributes
));
775 // Unmap the alaised Address
777 Status
= SetMemoryAttributes (Address
| VirtualMask
, Length
, EFI_MEMORY_WP
, 0);
778 if (!EFI_ERROR (Status
)) {
780 // Restore atttributes
782 Status
= SetMemoryAttributes (Address
, Length
, Attributes
, 0);
789 VIRTUAL_UNCACHED_PAGES_PROTOCOL gVirtualUncachedPages
= {
790 CpuConvertPagesToUncachedVirtualAddress
,