2 The file contains the GCD related services in the EFI Boot Services Table.
3 The GCD services are used to manage the memory and I/O regions that
4 are accessible to the CPU that is executing the DXE core.
6 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
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.
19 #include "Mem/HeapGuard.h"
21 #define MINIMUM_INITIAL_MEMORY_SIZE 0x10000
23 #define MEMORY_ATTRIBUTE_MASK (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
24 EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \
25 EFI_RESOURCE_ATTRIBUTE_TESTED | \
26 EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED | \
27 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED | \
28 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED | \
29 EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED | \
30 EFI_RESOURCE_ATTRIBUTE_16_BIT_IO | \
31 EFI_RESOURCE_ATTRIBUTE_32_BIT_IO | \
32 EFI_RESOURCE_ATTRIBUTE_64_BIT_IO | \
33 EFI_RESOURCE_ATTRIBUTE_PERSISTENT )
35 #define TESTED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
36 EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \
37 EFI_RESOURCE_ATTRIBUTE_TESTED )
39 #define INITIALIZED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
40 EFI_RESOURCE_ATTRIBUTE_INITIALIZED )
42 #define PRESENT_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT)
44 #define EXCLUSIVE_MEMORY_ATTRIBUTES (EFI_MEMORY_UC | EFI_MEMORY_WC | \
45 EFI_MEMORY_WT | EFI_MEMORY_WB | \
46 EFI_MEMORY_WP | EFI_MEMORY_UCE)
48 #define NONEXCLUSIVE_MEMORY_ATTRIBUTES (EFI_MEMORY_XP | EFI_MEMORY_RP | \
51 #define INVALID_CPU_ARCH_ATTRIBUTES 0xffffffff
56 EFI_LOCK mGcdMemorySpaceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
57 EFI_LOCK mGcdIoSpaceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
58 LIST_ENTRY mGcdMemorySpaceMap
= INITIALIZE_LIST_HEAD_VARIABLE (mGcdMemorySpaceMap
);
59 LIST_ENTRY mGcdIoSpaceMap
= INITIALIZE_LIST_HEAD_VARIABLE (mGcdIoSpaceMap
);
61 EFI_GCD_MAP_ENTRY mGcdMemorySpaceMapEntryTemplate
= {
62 EFI_GCD_MAP_SIGNATURE
,
71 EfiGcdMemoryTypeNonExistent
,
77 EFI_GCD_MAP_ENTRY mGcdIoSpaceMapEntryTemplate
= {
78 EFI_GCD_MAP_SIGNATURE
,
87 (EFI_GCD_MEMORY_TYPE
) 0,
88 EfiGcdIoTypeNonExistent
,
93 GCD_ATTRIBUTE_CONVERSION_ENTRY mAttributeConversionTable
[] = {
94 { EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
, EFI_MEMORY_UC
, TRUE
},
95 { EFI_RESOURCE_ATTRIBUTE_UNCACHED_EXPORTED
, EFI_MEMORY_UCE
, TRUE
},
96 { EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
, EFI_MEMORY_WC
, TRUE
},
97 { EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
, EFI_MEMORY_WT
, TRUE
},
98 { EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
, EFI_MEMORY_WB
, TRUE
},
99 { EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE
, EFI_MEMORY_RP
, TRUE
},
100 { EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE
, EFI_MEMORY_WP
, TRUE
},
101 { EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE
, EFI_MEMORY_XP
, TRUE
},
102 { EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE
, EFI_MEMORY_RO
, TRUE
},
103 { EFI_RESOURCE_ATTRIBUTE_PRESENT
, EFI_MEMORY_PRESENT
, FALSE
},
104 { EFI_RESOURCE_ATTRIBUTE_INITIALIZED
, EFI_MEMORY_INITIALIZED
, FALSE
},
105 { EFI_RESOURCE_ATTRIBUTE_TESTED
, EFI_MEMORY_TESTED
, FALSE
},
106 { EFI_RESOURCE_ATTRIBUTE_PERSISTABLE
, EFI_MEMORY_NV
, TRUE
},
107 { EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
, EFI_MEMORY_MORE_RELIABLE
, TRUE
},
112 /// Lookup table used to print GCD Memory Space Map
114 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mGcdMemoryTypeNames
[] = {
115 "NonExist ", // EfiGcdMemoryTypeNonExistent
116 "Reserved ", // EfiGcdMemoryTypeReserved
117 "SystemMem", // EfiGcdMemoryTypeSystemMemory
118 "MMIO ", // EfiGcdMemoryTypeMemoryMappedIo
119 "PersisMem", // EfiGcdMemoryTypePersistent
120 "MoreRelia", // EfiGcdMemoryTypeMoreReliable
121 "Unknown " // EfiGcdMemoryTypeMaximum
125 /// Lookup table used to print GCD I/O Space Map
127 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mGcdIoTypeNames
[] = {
128 "NonExist", // EfiGcdIoTypeNonExistent
129 "Reserved", // EfiGcdIoTypeReserved
130 "I/O ", // EfiGcdIoTypeIo
131 "Unknown " // EfiGcdIoTypeMaximum
135 /// Lookup table used to print GCD Allocation Types
137 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mGcdAllocationTypeNames
[] = {
138 "AnySearchBottomUp ", // EfiGcdAllocateAnySearchBottomUp
139 "MaxAddressSearchBottomUp ", // EfiGcdAllocateMaxAddressSearchBottomUp
140 "AtAddress ", // EfiGcdAllocateAddress
141 "AnySearchTopDown ", // EfiGcdAllocateAnySearchTopDown
142 "MaxAddressSearchTopDown ", // EfiGcdAllocateMaxAddressSearchTopDown
143 "Unknown " // EfiGcdMaxAllocateType
147 Dump the entire contents if the GCD Memory Space Map using DEBUG() macros when
148 PcdDebugPrintErrorLevel has the DEBUG_GCD bit set.
150 @param InitialMap TRUE if the initial GCD Memory Map is being dumped. Otherwise, FALSE.
155 CoreDumpGcdMemorySpaceMap (
161 UINTN NumberOfDescriptors
;
162 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
165 Status
= CoreGetMemorySpaceMap (&NumberOfDescriptors
, &MemorySpaceMap
);
166 ASSERT (Status
== EFI_SUCCESS
&& MemorySpaceMap
!= NULL
);
169 DEBUG ((DEBUG_GCD
, "GCD:Initial GCD Memory Space Map\n"));
171 DEBUG ((DEBUG_GCD
, "GCDMemType Range Capabilities Attributes \n"));
172 DEBUG ((DEBUG_GCD
, "========== ================================= ================ ================\n"));
173 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
174 DEBUG ((DEBUG_GCD
, "%a %016lx-%016lx %016lx %016lx%c\n",
175 mGcdMemoryTypeNames
[MIN (MemorySpaceMap
[Index
].GcdMemoryType
, EfiGcdMemoryTypeMaximum
)],
176 MemorySpaceMap
[Index
].BaseAddress
,
177 MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- 1,
178 MemorySpaceMap
[Index
].Capabilities
,
179 MemorySpaceMap
[Index
].Attributes
,
180 MemorySpaceMap
[Index
].ImageHandle
== NULL
? ' ' : '*'
183 DEBUG ((DEBUG_GCD
, "\n"));
184 FreePool (MemorySpaceMap
);
189 Dump the entire contents if the GCD I/O Space Map using DEBUG() macros when
190 PcdDebugPrintErrorLevel has the DEBUG_GCD bit set.
192 @param InitialMap TRUE if the initial GCD I/O Map is being dumped. Otherwise, FALSE.
197 CoreDumpGcdIoSpaceMap (
203 UINTN NumberOfDescriptors
;
204 EFI_GCD_IO_SPACE_DESCRIPTOR
*IoSpaceMap
;
207 Status
= CoreGetIoSpaceMap (&NumberOfDescriptors
, &IoSpaceMap
);
208 ASSERT (Status
== EFI_SUCCESS
&& IoSpaceMap
!= NULL
);
211 DEBUG ((DEBUG_GCD
, "GCD:Initial GCD I/O Space Map\n"));
214 DEBUG ((DEBUG_GCD
, "GCDIoType Range \n"));
215 DEBUG ((DEBUG_GCD
, "========== =================================\n"));
216 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
217 DEBUG ((DEBUG_GCD
, "%a %016lx-%016lx%c\n",
218 mGcdIoTypeNames
[MIN (IoSpaceMap
[Index
].GcdIoType
, EfiGcdIoTypeMaximum
)],
219 IoSpaceMap
[Index
].BaseAddress
,
220 IoSpaceMap
[Index
].BaseAddress
+ IoSpaceMap
[Index
].Length
- 1,
221 IoSpaceMap
[Index
].ImageHandle
== NULL
? ' ' : '*'
224 DEBUG ((DEBUG_GCD
, "\n"));
225 FreePool (IoSpaceMap
);
230 Validate resource descriptor HOB's attributes.
232 If Attributes includes some memory resource's settings, it should include
233 the corresponding capabilites also.
235 @param Attributes Resource descriptor HOB attributes.
239 CoreValidateResourceDescriptorHobAttributes (
243 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED
) == 0) ||
244 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE
) != 0));
245 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED
) == 0) ||
246 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE
) != 0));
247 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED
) == 0) ||
248 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE
) != 0));
249 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED
) == 0) ||
250 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE
) != 0));
251 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_PERSISTENT
) == 0) ||
252 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_PERSISTABLE
) != 0));
256 Acquire memory lock on mGcdMemorySpaceLock.
260 CoreAcquireGcdMemoryLock (
264 CoreAcquireLock (&mGcdMemorySpaceLock
);
270 Release memory lock on mGcdMemorySpaceLock.
274 CoreReleaseGcdMemoryLock (
278 CoreReleaseLock (&mGcdMemorySpaceLock
);
284 Acquire memory lock on mGcdIoSpaceLock.
288 CoreAcquireGcdIoLock (
292 CoreAcquireLock (&mGcdIoSpaceLock
);
297 Release memory lock on mGcdIoSpaceLock.
301 CoreReleaseGcdIoLock (
305 CoreReleaseLock (&mGcdIoSpaceLock
);
311 // GCD Initialization Worker Functions
314 Aligns a value to the specified boundary.
316 @param Value 64 bit value to align
317 @param Alignment Log base 2 of the boundary to align Value to
318 @param RoundUp TRUE if Value is to be rounded up to the nearest
319 aligned boundary. FALSE is Value is to be
320 rounded down to the nearest aligned boundary.
322 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
332 UINT64 AlignmentMask
;
334 AlignmentMask
= LShiftU64 (1, Alignment
) - 1;
336 Value
+= AlignmentMask
;
338 return Value
& (~AlignmentMask
);
343 Aligns address to the page boundary.
345 @param Value 64 bit address to align
347 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
355 return AlignValue (Value
, EFI_PAGE_SHIFT
, TRUE
);
360 Aligns length to the page boundary.
362 @param Value 64 bit length to align
364 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
372 return AlignValue (Value
, EFI_PAGE_SHIFT
, FALSE
);
376 // GCD Memory Space Worker Functions
380 Allocate pool for two entries.
382 @param TopEntry An entry of GCD map
383 @param BottomEntry An entry of GCD map
385 @retval EFI_OUT_OF_RESOURCES No enough buffer to be allocated.
386 @retval EFI_SUCCESS Both entries successfully allocated.
390 CoreAllocateGcdMapEntry (
391 IN OUT EFI_GCD_MAP_ENTRY
**TopEntry
,
392 IN OUT EFI_GCD_MAP_ENTRY
**BottomEntry
396 // Set to mOnGuarding to TRUE before memory allocation. This will make sure
397 // that the entry memory is not "guarded" by HeapGuard. Otherwise it might
398 // cause problem when it's freed (if HeapGuard is enabled).
401 *TopEntry
= AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY
));
403 if (*TopEntry
== NULL
) {
404 return EFI_OUT_OF_RESOURCES
;
408 *BottomEntry
= AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY
));
410 if (*BottomEntry
== NULL
) {
411 CoreFreePool (*TopEntry
);
412 return EFI_OUT_OF_RESOURCES
;
420 Internal function. Inserts a new descriptor into a sorted list
422 @param Link The linked list to insert the range BaseAddress
424 @param Entry A pointer to the entry that is inserted
425 @param BaseAddress The base address of the new range
426 @param Length The length of the new range in bytes
427 @param TopEntry Top pad entry to insert if needed.
428 @param BottomEntry Bottom pad entry to insert if needed.
430 @retval EFI_SUCCESS The new range was inserted into the linked list
434 CoreInsertGcdMapEntry (
436 IN EFI_GCD_MAP_ENTRY
*Entry
,
437 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
439 IN EFI_GCD_MAP_ENTRY
*TopEntry
,
440 IN EFI_GCD_MAP_ENTRY
*BottomEntry
443 ASSERT (Length
!= 0);
445 if (BaseAddress
> Entry
->BaseAddress
) {
446 ASSERT (BottomEntry
->Signature
== 0);
448 CopyMem (BottomEntry
, Entry
, sizeof (EFI_GCD_MAP_ENTRY
));
449 Entry
->BaseAddress
= BaseAddress
;
450 BottomEntry
->EndAddress
= BaseAddress
- 1;
451 InsertTailList (Link
, &BottomEntry
->Link
);
454 if ((BaseAddress
+ Length
- 1) < Entry
->EndAddress
) {
455 ASSERT (TopEntry
->Signature
== 0);
457 CopyMem (TopEntry
, Entry
, sizeof (EFI_GCD_MAP_ENTRY
));
458 TopEntry
->BaseAddress
= BaseAddress
+ Length
;
459 Entry
->EndAddress
= BaseAddress
+ Length
- 1;
460 InsertHeadList (Link
, &TopEntry
->Link
);
468 Merge the Gcd region specified by Link and its adjacent entry.
470 @param Link Specify the entry to be merged (with its
472 @param Forward Direction (forward or backward).
475 @retval EFI_SUCCESS Successfully returned.
476 @retval EFI_UNSUPPORTED These adjacent regions could not merge.
480 CoreMergeGcdMapEntry (
486 LIST_ENTRY
*AdjacentLink
;
487 EFI_GCD_MAP_ENTRY
*Entry
;
488 EFI_GCD_MAP_ENTRY
*AdjacentEntry
;
491 // Get adjacent entry
494 AdjacentLink
= Link
->ForwardLink
;
496 AdjacentLink
= Link
->BackLink
;
500 // If AdjacentLink is the head of the list, then no merge can be performed
502 if (AdjacentLink
== Map
) {
506 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
507 AdjacentEntry
= CR (AdjacentLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
509 if (Entry
->Capabilities
!= AdjacentEntry
->Capabilities
) {
510 return EFI_UNSUPPORTED
;
512 if (Entry
->Attributes
!= AdjacentEntry
->Attributes
) {
513 return EFI_UNSUPPORTED
;
515 if (Entry
->GcdMemoryType
!= AdjacentEntry
->GcdMemoryType
) {
516 return EFI_UNSUPPORTED
;
518 if (Entry
->GcdIoType
!= AdjacentEntry
->GcdIoType
) {
519 return EFI_UNSUPPORTED
;
521 if (Entry
->ImageHandle
!= AdjacentEntry
->ImageHandle
) {
522 return EFI_UNSUPPORTED
;
524 if (Entry
->DeviceHandle
!= AdjacentEntry
->DeviceHandle
) {
525 return EFI_UNSUPPORTED
;
529 Entry
->EndAddress
= AdjacentEntry
->EndAddress
;
531 Entry
->BaseAddress
= AdjacentEntry
->BaseAddress
;
533 RemoveEntryList (AdjacentLink
);
534 CoreFreePool (AdjacentEntry
);
541 Merge adjacent entries on total chain.
543 @param TopEntry Top entry of GCD map.
544 @param BottomEntry Bottom entry of GCD map.
545 @param StartLink Start link of the list for this loop.
546 @param EndLink End link of the list for this loop.
549 @retval EFI_SUCCESS GCD map successfully cleaned up.
553 CoreCleanupGcdMapEntry (
554 IN EFI_GCD_MAP_ENTRY
*TopEntry
,
555 IN EFI_GCD_MAP_ENTRY
*BottomEntry
,
556 IN LIST_ENTRY
*StartLink
,
557 IN LIST_ENTRY
*EndLink
,
563 if (TopEntry
->Signature
== 0) {
564 CoreFreePool (TopEntry
);
566 if (BottomEntry
->Signature
== 0) {
567 CoreFreePool (BottomEntry
);
571 while (Link
!= EndLink
->ForwardLink
) {
572 CoreMergeGcdMapEntry (Link
, FALSE
, Map
);
573 Link
= Link
->ForwardLink
;
575 CoreMergeGcdMapEntry (EndLink
, TRUE
, Map
);
582 Search a segment of memory space in GCD map. The result is a range of GCD entry list.
584 @param BaseAddress The start address of the segment.
585 @param Length The length of the segment.
586 @param StartLink The first GCD entry involves this segment of
588 @param EndLink The first GCD entry involves this segment of
590 @param Map Points to the start entry to search.
592 @retval EFI_SUCCESS Successfully found the entry.
593 @retval EFI_NOT_FOUND Not found.
597 CoreSearchGcdMapEntry (
598 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
600 OUT LIST_ENTRY
**StartLink
,
601 OUT LIST_ENTRY
**EndLink
,
606 EFI_GCD_MAP_ENTRY
*Entry
;
608 ASSERT (Length
!= 0);
613 Link
= Map
->ForwardLink
;
614 while (Link
!= Map
) {
615 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
616 if (BaseAddress
>= Entry
->BaseAddress
&& BaseAddress
<= Entry
->EndAddress
) {
619 if (*StartLink
!= NULL
) {
620 if ((BaseAddress
+ Length
- 1) >= Entry
->BaseAddress
&&
621 (BaseAddress
+ Length
- 1) <= Entry
->EndAddress
) {
626 Link
= Link
->ForwardLink
;
629 return EFI_NOT_FOUND
;
634 Count the amount of GCD map entries.
636 @param Map Points to the start entry to do the count loop.
642 CoreCountGcdMapEntry (
650 Link
= Map
->ForwardLink
;
651 while (Link
!= Map
) {
653 Link
= Link
->ForwardLink
;
662 Return the memory attribute specified by Attributes
664 @param Attributes A num with some attribute bits on.
666 @return The enum value of memory attribute.
670 ConverToCpuArchAttributes (
674 UINT64 CpuArchAttributes
;
676 if ((Attributes
& ~(EXCLUSIVE_MEMORY_ATTRIBUTES
|
677 NONEXCLUSIVE_MEMORY_ATTRIBUTES
)) != 0) {
678 return INVALID_CPU_ARCH_ATTRIBUTES
;
681 CpuArchAttributes
= Attributes
& NONEXCLUSIVE_MEMORY_ATTRIBUTES
;
683 if ( (Attributes
& EFI_MEMORY_UC
) == EFI_MEMORY_UC
) {
684 CpuArchAttributes
|= EFI_MEMORY_UC
;
685 } else if ( (Attributes
& EFI_MEMORY_WC
) == EFI_MEMORY_WC
) {
686 CpuArchAttributes
|= EFI_MEMORY_WC
;
687 } else if ( (Attributes
& EFI_MEMORY_WT
) == EFI_MEMORY_WT
) {
688 CpuArchAttributes
|= EFI_MEMORY_WT
;
689 } else if ( (Attributes
& EFI_MEMORY_WB
) == EFI_MEMORY_WB
) {
690 CpuArchAttributes
|= EFI_MEMORY_WB
;
691 } else if ( (Attributes
& EFI_MEMORY_UCE
) == EFI_MEMORY_UCE
) {
692 CpuArchAttributes
|= EFI_MEMORY_UCE
;
693 } else if ( (Attributes
& EFI_MEMORY_WP
) == EFI_MEMORY_WP
) {
694 CpuArchAttributes
|= EFI_MEMORY_WP
;
697 return CpuArchAttributes
;
702 Do operation on a segment of memory space specified (add, free, remove, change attribute ...).
704 @param Operation The type of the operation
705 @param GcdMemoryType Additional information for the operation
706 @param GcdIoType Additional information for the operation
707 @param BaseAddress Start address of the segment
708 @param Length length of the segment
709 @param Capabilities The alterable attributes of a newly added entry
710 @param Attributes The attributes needs to be set
712 @retval EFI_INVALID_PARAMETER Length is 0 or address (length) not aligned when
714 @retval EFI_SUCCESS Action successfully done.
715 @retval EFI_UNSUPPORTED Could not find the proper descriptor on this
716 segment or set an upsupported attribute.
717 @retval EFI_ACCESS_DENIED Operate on an space non-exist or is used for an
719 @retval EFI_NOT_FOUND Free a non-using space or remove a non-exist
721 @retval EFI_OUT_OF_RESOURCES No buffer could be allocated.
722 @retval EFI_NOT_AVAILABLE_YET The attributes cannot be set because CPU architectural protocol
723 is not available yet.
728 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
729 IN EFI_GCD_IO_TYPE GcdIoType
,
730 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
732 IN UINT64 Capabilities
,
739 EFI_GCD_MAP_ENTRY
*Entry
;
740 EFI_GCD_MAP_ENTRY
*TopEntry
;
741 EFI_GCD_MAP_ENTRY
*BottomEntry
;
742 LIST_ENTRY
*StartLink
;
744 UINT64 CpuArchAttributes
;
747 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
748 return EFI_INVALID_PARAMETER
;
752 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
753 CoreAcquireGcdMemoryLock ();
754 Map
= &mGcdMemorySpaceMap
;
755 } else if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
756 CoreAcquireGcdIoLock ();
757 Map
= &mGcdIoSpaceMap
;
763 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
765 Status
= CoreSearchGcdMapEntry (BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
766 if (EFI_ERROR (Status
)) {
767 Status
= EFI_UNSUPPORTED
;
771 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
774 // Verify that the list of descriptors are unallocated non-existent memory.
777 while (Link
!= EndLink
->ForwardLink
) {
778 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
783 case GCD_ADD_MEMORY_OPERATION
:
784 if (Entry
->GcdMemoryType
!= EfiGcdMemoryTypeNonExistent
||
785 Entry
->ImageHandle
!= NULL
) {
786 Status
= EFI_ACCESS_DENIED
;
790 case GCD_ADD_IO_OPERATION
:
791 if (Entry
->GcdIoType
!= EfiGcdIoTypeNonExistent
||
792 Entry
->ImageHandle
!= NULL
) {
793 Status
= EFI_ACCESS_DENIED
;
800 case GCD_FREE_MEMORY_OPERATION
:
801 case GCD_FREE_IO_OPERATION
:
802 if (Entry
->ImageHandle
== NULL
) {
803 Status
= EFI_NOT_FOUND
;
810 case GCD_REMOVE_MEMORY_OPERATION
:
811 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
812 Status
= EFI_NOT_FOUND
;
815 if (Entry
->ImageHandle
!= NULL
) {
816 Status
= EFI_ACCESS_DENIED
;
820 case GCD_REMOVE_IO_OPERATION
:
821 if (Entry
->GcdIoType
== EfiGcdIoTypeNonExistent
) {
822 Status
= EFI_NOT_FOUND
;
825 if (Entry
->ImageHandle
!= NULL
) {
826 Status
= EFI_ACCESS_DENIED
;
831 // Set attributes operation
833 case GCD_SET_ATTRIBUTES_MEMORY_OPERATION
:
834 if ((Attributes
& EFI_MEMORY_RUNTIME
) != 0) {
835 if ((BaseAddress
& EFI_PAGE_MASK
) != 0 || (Length
& EFI_PAGE_MASK
) != 0) {
836 Status
= EFI_INVALID_PARAMETER
;
840 if ((Entry
->Capabilities
& Attributes
) != Attributes
) {
841 Status
= EFI_UNSUPPORTED
;
846 // Set capabilities operation
848 case GCD_SET_CAPABILITIES_MEMORY_OPERATION
:
849 if ((BaseAddress
& EFI_PAGE_MASK
) != 0 || (Length
& EFI_PAGE_MASK
) != 0) {
850 Status
= EFI_INVALID_PARAMETER
;
855 // Current attributes must still be supported with new capabilities
857 if ((Capabilities
& Entry
->Attributes
) != Entry
->Attributes
) {
858 Status
= EFI_UNSUPPORTED
;
863 Link
= Link
->ForwardLink
;
867 // Allocate work space to perform this operation
869 Status
= CoreAllocateGcdMapEntry (&TopEntry
, &BottomEntry
);
870 if (EFI_ERROR (Status
)) {
871 Status
= EFI_OUT_OF_RESOURCES
;
874 ASSERT (TopEntry
!= NULL
&& BottomEntry
!= NULL
);
876 if (Operation
== GCD_SET_ATTRIBUTES_MEMORY_OPERATION
) {
878 // Call CPU Arch Protocol to attempt to set attributes on the range
880 CpuArchAttributes
= ConverToCpuArchAttributes (Attributes
);
881 if (CpuArchAttributes
!= INVALID_CPU_ARCH_ATTRIBUTES
) {
883 Status
= EFI_NOT_AVAILABLE_YET
;
885 Status
= gCpu
->SetMemoryAttributes (
892 if (EFI_ERROR (Status
)) {
893 CoreFreePool (TopEntry
);
894 CoreFreePool (BottomEntry
);
901 // Convert/Insert the list of descriptors from StartLink to EndLink
904 while (Link
!= EndLink
->ForwardLink
) {
905 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
906 CoreInsertGcdMapEntry (Link
, Entry
, BaseAddress
, Length
, TopEntry
, BottomEntry
);
911 case GCD_ADD_MEMORY_OPERATION
:
912 Entry
->GcdMemoryType
= GcdMemoryType
;
913 if (GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
914 Entry
->Capabilities
= Capabilities
| EFI_MEMORY_RUNTIME
| EFI_MEMORY_PORT_IO
;
916 Entry
->Capabilities
= Capabilities
| EFI_MEMORY_RUNTIME
;
919 case GCD_ADD_IO_OPERATION
:
920 Entry
->GcdIoType
= GcdIoType
;
925 case GCD_FREE_MEMORY_OPERATION
:
926 case GCD_FREE_IO_OPERATION
:
927 Entry
->ImageHandle
= NULL
;
928 Entry
->DeviceHandle
= NULL
;
933 case GCD_REMOVE_MEMORY_OPERATION
:
934 Entry
->GcdMemoryType
= EfiGcdMemoryTypeNonExistent
;
935 Entry
->Capabilities
= 0;
937 case GCD_REMOVE_IO_OPERATION
:
938 Entry
->GcdIoType
= EfiGcdIoTypeNonExistent
;
941 // Set attributes operation
943 case GCD_SET_ATTRIBUTES_MEMORY_OPERATION
:
944 Entry
->Attributes
= Attributes
;
947 // Set capabilities operation
949 case GCD_SET_CAPABILITIES_MEMORY_OPERATION
:
950 Entry
->Capabilities
= Capabilities
;
953 Link
= Link
->ForwardLink
;
959 Status
= CoreCleanupGcdMapEntry (TopEntry
, BottomEntry
, StartLink
, EndLink
, Map
);
962 DEBUG ((DEBUG_GCD
, " Status = %r\n", Status
));
964 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
965 CoreReleaseGcdMemoryLock ();
966 CoreDumpGcdMemorySpaceMap (FALSE
);
968 if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
969 CoreReleaseGcdIoLock ();
970 CoreDumpGcdIoSpaceMap (FALSE
);
978 Check whether an entry could be used to allocate space.
980 @param Operation Allocate memory or IO
981 @param Entry The entry to be tested
982 @param GcdMemoryType The desired memory type
983 @param GcdIoType The desired IO type
985 @retval EFI_NOT_FOUND The memory type does not match or there's an
986 image handle on the entry.
987 @retval EFI_UNSUPPORTED The operation unsupported.
988 @retval EFI_SUCCESS It's ok for this entry to be used to allocate
993 CoreAllocateSpaceCheckEntry (
995 IN EFI_GCD_MAP_ENTRY
*Entry
,
996 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
997 IN EFI_GCD_IO_TYPE GcdIoType
1000 if (Entry
->ImageHandle
!= NULL
) {
1001 return EFI_NOT_FOUND
;
1003 switch (Operation
) {
1004 case GCD_ALLOCATE_MEMORY_OPERATION
:
1005 if (Entry
->GcdMemoryType
!= GcdMemoryType
) {
1006 return EFI_NOT_FOUND
;
1009 case GCD_ALLOCATE_IO_OPERATION
:
1010 if (Entry
->GcdIoType
!= GcdIoType
) {
1011 return EFI_NOT_FOUND
;
1015 return EFI_UNSUPPORTED
;
1022 Allocate space on specified address and length.
1024 @param Operation The type of operation (memory or IO)
1025 @param GcdAllocateType The type of allocate operation
1026 @param GcdMemoryType The desired memory type
1027 @param GcdIoType The desired IO type
1028 @param Alignment Align with 2^Alignment
1029 @param Length Length to allocate
1030 @param BaseAddress Base address to allocate
1031 @param ImageHandle The image handle consume the allocated space.
1032 @param DeviceHandle The device handle consume the allocated space.
1034 @retval EFI_INVALID_PARAMETER Invalid parameter.
1035 @retval EFI_NOT_FOUND No descriptor for the desired space exists.
1036 @retval EFI_SUCCESS Space successfully allocated.
1042 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1043 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1044 IN EFI_GCD_IO_TYPE GcdIoType
,
1047 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1048 IN EFI_HANDLE ImageHandle
,
1049 IN EFI_HANDLE DeviceHandle OPTIONAL
1053 EFI_PHYSICAL_ADDRESS AlignmentMask
;
1054 EFI_PHYSICAL_ADDRESS MaxAddress
;
1057 LIST_ENTRY
*SubLink
;
1058 EFI_GCD_MAP_ENTRY
*Entry
;
1059 EFI_GCD_MAP_ENTRY
*TopEntry
;
1060 EFI_GCD_MAP_ENTRY
*BottomEntry
;
1061 LIST_ENTRY
*StartLink
;
1062 LIST_ENTRY
*EndLink
;
1066 // Make sure parameters are valid
1068 if ((UINT32
)GcdAllocateType
>= EfiGcdMaxAllocateType
) {
1069 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1070 return EFI_INVALID_PARAMETER
;
1072 if ((UINT32
)GcdMemoryType
>= EfiGcdMemoryTypeMaximum
) {
1073 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1074 return EFI_INVALID_PARAMETER
;
1076 if ((UINT32
)GcdIoType
>= EfiGcdIoTypeMaximum
) {
1077 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1078 return EFI_INVALID_PARAMETER
;
1080 if (BaseAddress
== NULL
) {
1081 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1082 return EFI_INVALID_PARAMETER
;
1084 if (ImageHandle
== NULL
) {
1085 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1086 return EFI_INVALID_PARAMETER
;
1088 if (Alignment
>= 64) {
1089 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_NOT_FOUND
));
1090 return EFI_NOT_FOUND
;
1093 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1094 return EFI_INVALID_PARAMETER
;
1098 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
1099 CoreAcquireGcdMemoryLock ();
1100 Map
= &mGcdMemorySpaceMap
;
1101 } else if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
1102 CoreAcquireGcdIoLock ();
1103 Map
= &mGcdIoSpaceMap
;
1112 // Compute alignment bit mask
1114 AlignmentMask
= LShiftU64 (1, Alignment
) - 1;
1116 if (GcdAllocateType
== EfiGcdAllocateAddress
) {
1118 // Verify that the BaseAddress passed in is aligned correctly
1120 if ((*BaseAddress
& AlignmentMask
) != 0) {
1121 Status
= EFI_NOT_FOUND
;
1126 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
1128 Status
= CoreSearchGcdMapEntry (*BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
1129 if (EFI_ERROR (Status
)) {
1130 Status
= EFI_NOT_FOUND
;
1133 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1136 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1139 while (Link
!= EndLink
->ForwardLink
) {
1140 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1141 Link
= Link
->ForwardLink
;
1142 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1143 if (EFI_ERROR (Status
)) {
1150 Entry
= CR (Map
->BackLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1153 // Compute the maximum address to use in the search algorithm
1155 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchBottomUp
||
1156 GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
) {
1157 MaxAddress
= *BaseAddress
;
1159 MaxAddress
= Entry
->EndAddress
;
1163 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1165 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
1166 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
1167 Link
= Map
->BackLink
;
1169 Link
= Map
->ForwardLink
;
1171 while (Link
!= Map
) {
1172 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1174 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
1175 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
1176 Link
= Link
->BackLink
;
1178 Link
= Link
->ForwardLink
;
1181 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1182 if (EFI_ERROR (Status
)) {
1186 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
1187 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
1188 if ((Entry
->BaseAddress
+ Length
) > MaxAddress
) {
1191 if (Length
> (Entry
->EndAddress
+ 1)) {
1192 Status
= EFI_NOT_FOUND
;
1195 if (Entry
->EndAddress
> MaxAddress
) {
1196 *BaseAddress
= MaxAddress
;
1198 *BaseAddress
= Entry
->EndAddress
;
1200 *BaseAddress
= (*BaseAddress
+ 1 - Length
) & (~AlignmentMask
);
1202 *BaseAddress
= (Entry
->BaseAddress
+ AlignmentMask
) & (~AlignmentMask
);
1203 if ((*BaseAddress
+ Length
- 1) > MaxAddress
) {
1204 Status
= EFI_NOT_FOUND
;
1210 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
1212 Status
= CoreSearchGcdMapEntry (*BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
1213 if (EFI_ERROR (Status
)) {
1214 Status
= EFI_NOT_FOUND
;
1217 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1221 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1224 SubLink
= StartLink
;
1225 while (SubLink
!= EndLink
->ForwardLink
) {
1226 Entry
= CR (SubLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1227 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1228 if (EFI_ERROR (Status
)) {
1233 SubLink
= SubLink
->ForwardLink
;
1241 Status
= EFI_NOT_FOUND
;
1246 // Allocate work space to perform this operation
1248 Status
= CoreAllocateGcdMapEntry (&TopEntry
, &BottomEntry
);
1249 if (EFI_ERROR (Status
)) {
1250 Status
= EFI_OUT_OF_RESOURCES
;
1253 ASSERT (TopEntry
!= NULL
&& BottomEntry
!= NULL
);
1256 // Convert/Insert the list of descriptors from StartLink to EndLink
1259 while (Link
!= EndLink
->ForwardLink
) {
1260 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1261 CoreInsertGcdMapEntry (Link
, Entry
, *BaseAddress
, Length
, TopEntry
, BottomEntry
);
1262 Entry
->ImageHandle
= ImageHandle
;
1263 Entry
->DeviceHandle
= DeviceHandle
;
1264 Link
= Link
->ForwardLink
;
1270 Status
= CoreCleanupGcdMapEntry (TopEntry
, BottomEntry
, StartLink
, EndLink
, Map
);
1273 DEBUG ((DEBUG_GCD
, " Status = %r", Status
));
1274 if (!EFI_ERROR (Status
)) {
1275 DEBUG ((DEBUG_GCD
, " (BaseAddress = %016lx)", *BaseAddress
));
1277 DEBUG ((DEBUG_GCD
, "\n"));
1279 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
1280 CoreReleaseGcdMemoryLock ();
1281 CoreDumpGcdMemorySpaceMap (FALSE
);
1283 if ((Operation
& GCD_IO_SPACE_OPERATION
) !=0) {
1284 CoreReleaseGcdIoLock ();
1285 CoreDumpGcdIoSpaceMap (FALSE
);
1293 Add a segment of memory to GCD map.
1295 @param GcdMemoryType Memory type of the segment.
1296 @param BaseAddress Base address of the segment.
1297 @param Length Length of the segment.
1298 @param Capabilities alterable attributes of the segment.
1300 @retval EFI_INVALID_PARAMETER Invalid parameters.
1301 @retval EFI_SUCCESS Successfully add a segment of memory space.
1305 CoreInternalAddMemorySpace (
1306 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1307 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1309 IN UINT64 Capabilities
1312 DEBUG ((DEBUG_GCD
, "GCD:AddMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1313 DEBUG ((DEBUG_GCD
, " GcdMemoryType = %a\n", mGcdMemoryTypeNames
[MIN (GcdMemoryType
, EfiGcdMemoryTypeMaximum
)]));
1314 DEBUG ((DEBUG_GCD
, " Capabilities = %016lx\n", Capabilities
));
1317 // Make sure parameters are valid
1319 if (GcdMemoryType
<= EfiGcdMemoryTypeNonExistent
|| GcdMemoryType
>= EfiGcdMemoryTypeMaximum
) {
1320 return EFI_INVALID_PARAMETER
;
1323 return CoreConvertSpace (GCD_ADD_MEMORY_OPERATION
, GcdMemoryType
, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, Capabilities
, 0);
1327 // GCD Core Services
1331 Allocates nonexistent memory, reserved memory, system memory, or memorymapped
1332 I/O resources from the global coherency domain of the processor.
1334 @param GcdAllocateType The type of allocate operation
1335 @param GcdMemoryType The desired memory type
1336 @param Alignment Align with 2^Alignment
1337 @param Length Length to allocate
1338 @param BaseAddress Base address to allocate
1339 @param ImageHandle The image handle consume the allocated space.
1340 @param DeviceHandle The device handle consume the allocated space.
1342 @retval EFI_INVALID_PARAMETER Invalid parameter.
1343 @retval EFI_NOT_FOUND No descriptor contains the desired space.
1344 @retval EFI_SUCCESS Memory space successfully allocated.
1349 CoreAllocateMemorySpace (
1350 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1351 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1354 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1355 IN EFI_HANDLE ImageHandle
,
1356 IN EFI_HANDLE DeviceHandle OPTIONAL
1359 if (BaseAddress
!= NULL
) {
1360 DEBUG ((DEBUG_GCD
, "GCD:AllocateMemorySpace(Base=%016lx,Length=%016lx)\n", *BaseAddress
, Length
));
1362 DEBUG ((DEBUG_GCD
, "GCD:AllocateMemorySpace(Base=<NULL>,Length=%016lx)\n", Length
));
1364 DEBUG ((DEBUG_GCD
, " GcdAllocateType = %a\n", mGcdAllocationTypeNames
[MIN (GcdAllocateType
, EfiGcdMaxAllocateType
)]));
1365 DEBUG ((DEBUG_GCD
, " GcdMemoryType = %a\n", mGcdMemoryTypeNames
[MIN (GcdMemoryType
, EfiGcdMemoryTypeMaximum
)]));
1366 DEBUG ((DEBUG_GCD
, " Alignment = %016lx\n", LShiftU64 (1, Alignment
)));
1367 DEBUG ((DEBUG_GCD
, " ImageHandle = %p\n", ImageHandle
));
1368 DEBUG ((DEBUG_GCD
, " DeviceHandle = %p\n", DeviceHandle
));
1370 return CoreAllocateSpace (
1371 GCD_ALLOCATE_MEMORY_OPERATION
,
1374 (EFI_GCD_IO_TYPE
) 0,
1385 Adds reserved memory, system memory, or memory-mapped I/O resources to the
1386 global coherency domain of the processor.
1388 @param GcdMemoryType Memory type of the memory space.
1389 @param BaseAddress Base address of the memory space.
1390 @param Length Length of the memory space.
1391 @param Capabilities alterable attributes of the memory space.
1393 @retval EFI_SUCCESS Merged this memory space into GCD map.
1398 CoreAddMemorySpace (
1399 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1400 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1402 IN UINT64 Capabilities
1406 EFI_PHYSICAL_ADDRESS PageBaseAddress
;
1409 Status
= CoreInternalAddMemorySpace (GcdMemoryType
, BaseAddress
, Length
, Capabilities
);
1411 if (!EFI_ERROR (Status
) && ((GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) || (GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
))) {
1413 PageBaseAddress
= PageAlignAddress (BaseAddress
);
1414 PageLength
= PageAlignLength (BaseAddress
+ Length
- PageBaseAddress
);
1416 Status
= CoreAllocateMemorySpace (
1417 EfiGcdAllocateAddress
,
1422 gDxeCoreImageHandle
,
1426 if (!EFI_ERROR (Status
)) {
1427 CoreAddMemoryDescriptor (
1428 EfiConventionalMemory
,
1430 RShiftU64 (PageLength
, EFI_PAGE_SHIFT
),
1434 for (; PageLength
!= 0; PageLength
-= EFI_PAGE_SIZE
, PageBaseAddress
+= EFI_PAGE_SIZE
) {
1435 Status
= CoreAllocateMemorySpace (
1436 EfiGcdAllocateAddress
,
1441 gDxeCoreImageHandle
,
1445 if (!EFI_ERROR (Status
)) {
1446 CoreAddMemoryDescriptor (
1447 EfiConventionalMemory
,
1461 Frees nonexistent memory, reserved memory, system memory, or memory-mapped
1462 I/O resources from the global coherency domain of the processor.
1464 @param BaseAddress Base address of the memory space.
1465 @param Length Length of the memory space.
1467 @retval EFI_SUCCESS Space successfully freed.
1472 CoreFreeMemorySpace (
1473 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1477 DEBUG ((DEBUG_GCD
, "GCD:FreeMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1479 return CoreConvertSpace (GCD_FREE_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1484 Removes reserved memory, system memory, or memory-mapped I/O resources from
1485 the global coherency domain of the processor.
1487 @param BaseAddress Base address of the memory space.
1488 @param Length Length of the memory space.
1490 @retval EFI_SUCCESS Successfully remove a segment of memory space.
1495 CoreRemoveMemorySpace (
1496 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1500 DEBUG ((DEBUG_GCD
, "GCD:RemoveMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1502 return CoreConvertSpace (GCD_REMOVE_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1507 Build a memory descriptor according to an entry.
1509 @param Descriptor The descriptor to be built
1510 @param Entry According to this entry
1514 BuildMemoryDescriptor (
1515 IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
,
1516 IN EFI_GCD_MAP_ENTRY
*Entry
1519 Descriptor
->BaseAddress
= Entry
->BaseAddress
;
1520 Descriptor
->Length
= Entry
->EndAddress
- Entry
->BaseAddress
+ 1;
1521 Descriptor
->Capabilities
= Entry
->Capabilities
;
1522 Descriptor
->Attributes
= Entry
->Attributes
;
1523 Descriptor
->GcdMemoryType
= Entry
->GcdMemoryType
;
1524 Descriptor
->ImageHandle
= Entry
->ImageHandle
;
1525 Descriptor
->DeviceHandle
= Entry
->DeviceHandle
;
1530 Retrieves the descriptor for a memory region containing a specified address.
1532 @param BaseAddress Specified start address
1533 @param Descriptor Specified length
1535 @retval EFI_INVALID_PARAMETER Invalid parameter
1536 @retval EFI_SUCCESS Successfully get memory space descriptor.
1541 CoreGetMemorySpaceDescriptor (
1542 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1543 OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
1547 LIST_ENTRY
*StartLink
;
1548 LIST_ENTRY
*EndLink
;
1549 EFI_GCD_MAP_ENTRY
*Entry
;
1552 // Make sure parameters are valid
1554 if (Descriptor
== NULL
) {
1555 return EFI_INVALID_PARAMETER
;
1558 CoreAcquireGcdMemoryLock ();
1561 // Search for the list of descriptors that contain BaseAddress
1563 Status
= CoreSearchGcdMapEntry (BaseAddress
, 1, &StartLink
, &EndLink
, &mGcdMemorySpaceMap
);
1564 if (EFI_ERROR (Status
)) {
1565 Status
= EFI_NOT_FOUND
;
1567 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1569 // Copy the contents of the found descriptor into Descriptor
1571 Entry
= CR (StartLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1572 BuildMemoryDescriptor (Descriptor
, Entry
);
1575 CoreReleaseGcdMemoryLock ();
1582 Modifies the attributes for a memory region in the global coherency domain of the
1585 @param BaseAddress Specified start address
1586 @param Length Specified length
1587 @param Attributes Specified attributes
1589 @retval EFI_SUCCESS The attributes were set for the memory region.
1590 @retval EFI_INVALID_PARAMETER Length is zero.
1591 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
1592 resource range specified by BaseAddress and Length.
1593 @retval EFI_UNSUPPORTED The bit mask of attributes is not support for the memory resource
1594 range specified by BaseAddress and Length.
1595 @retval EFI_ACCESS_DEFINED The attributes for the memory resource range specified by
1596 BaseAddress and Length cannot be modified.
1597 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
1598 the memory resource range.
1599 @retval EFI_NOT_AVAILABLE_YET The attributes cannot be set because CPU architectural protocol is
1605 CoreSetMemorySpaceAttributes (
1606 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1608 IN UINT64 Attributes
1611 DEBUG ((DEBUG_GCD
, "GCD:SetMemorySpaceAttributes(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1612 DEBUG ((DEBUG_GCD
, " Attributes = %016lx\n", Attributes
));
1614 return CoreConvertSpace (GCD_SET_ATTRIBUTES_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, Attributes
);
1619 Modifies the capabilities for a memory region in the global coherency domain of the
1622 @param BaseAddress The physical address that is the start address of a memory region.
1623 @param Length The size in bytes of the memory region.
1624 @param Capabilities The bit mask of capabilities that the memory region supports.
1626 @retval EFI_SUCCESS The capabilities were set for the memory region.
1627 @retval EFI_INVALID_PARAMETER Length is zero.
1628 @retval EFI_UNSUPPORTED The capabilities specified by Capabilities do not include the
1629 memory region attributes currently in use.
1630 @retval EFI_ACCESS_DENIED The capabilities for the memory resource range specified by
1631 BaseAddress and Length cannot be modified.
1632 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the capabilities
1633 of the memory resource range.
1637 CoreSetMemorySpaceCapabilities (
1638 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1640 IN UINT64 Capabilities
1645 DEBUG ((DEBUG_GCD
, "GCD:CoreSetMemorySpaceCapabilities(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1646 DEBUG ((DEBUG_GCD
, " Capabilities = %016lx\n", Capabilities
));
1648 Status
= CoreConvertSpace (GCD_SET_CAPABILITIES_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, Capabilities
, 0);
1649 if (!EFI_ERROR(Status
)) {
1650 CoreUpdateMemoryAttributes(BaseAddress
, RShiftU64(Length
, EFI_PAGE_SHIFT
), Capabilities
& (~EFI_MEMORY_RUNTIME
));
1658 Returns a map of the memory resources in the global coherency domain of the
1661 @param NumberOfDescriptors Number of descriptors.
1662 @param MemorySpaceMap Descriptor array
1664 @retval EFI_INVALID_PARAMETER Invalid parameter
1665 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1666 @retval EFI_SUCCESS Successfully get memory space map.
1671 CoreGetMemorySpaceMap (
1672 OUT UINTN
*NumberOfDescriptors
,
1673 OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
**MemorySpaceMap
1678 EFI_GCD_MAP_ENTRY
*Entry
;
1679 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
;
1682 // Make sure parameters are valid
1684 if (NumberOfDescriptors
== NULL
) {
1685 return EFI_INVALID_PARAMETER
;
1687 if (MemorySpaceMap
== NULL
) {
1688 return EFI_INVALID_PARAMETER
;
1691 CoreAcquireGcdMemoryLock ();
1694 // Count the number of descriptors
1696 *NumberOfDescriptors
= CoreCountGcdMapEntry (&mGcdMemorySpaceMap
);
1699 // Allocate the MemorySpaceMap
1701 *MemorySpaceMap
= AllocatePool (*NumberOfDescriptors
* sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR
));
1702 if (*MemorySpaceMap
== NULL
) {
1703 Status
= EFI_OUT_OF_RESOURCES
;
1708 // Fill in the MemorySpaceMap
1710 Descriptor
= *MemorySpaceMap
;
1711 Link
= mGcdMemorySpaceMap
.ForwardLink
;
1712 while (Link
!= &mGcdMemorySpaceMap
) {
1713 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1714 BuildMemoryDescriptor (Descriptor
, Entry
);
1716 Link
= Link
->ForwardLink
;
1718 Status
= EFI_SUCCESS
;
1721 CoreReleaseGcdMemoryLock ();
1727 Adds reserved I/O or I/O resources to the global coherency domain of the processor.
1729 @param GcdIoType IO type of the segment.
1730 @param BaseAddress Base address of the segment.
1731 @param Length Length of the segment.
1733 @retval EFI_SUCCESS Merged this segment into GCD map.
1734 @retval EFI_INVALID_PARAMETER Parameter not valid
1740 IN EFI_GCD_IO_TYPE GcdIoType
,
1741 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1745 DEBUG ((DEBUG_GCD
, "GCD:AddIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1746 DEBUG ((DEBUG_GCD
, " GcdIoType = %a\n", mGcdIoTypeNames
[MIN (GcdIoType
, EfiGcdIoTypeMaximum
)]));
1749 // Make sure parameters are valid
1751 if (GcdIoType
<= EfiGcdIoTypeNonExistent
|| GcdIoType
>= EfiGcdIoTypeMaximum
) {
1752 return EFI_INVALID_PARAMETER
;
1754 return CoreConvertSpace (GCD_ADD_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, GcdIoType
, BaseAddress
, Length
, 0, 0);
1759 Allocates nonexistent I/O, reserved I/O, or I/O resources from the global coherency
1760 domain of the processor.
1762 @param GcdAllocateType The type of allocate operation
1763 @param GcdIoType The desired IO type
1764 @param Alignment Align with 2^Alignment
1765 @param Length Length to allocate
1766 @param BaseAddress Base address to allocate
1767 @param ImageHandle The image handle consume the allocated space.
1768 @param DeviceHandle The device handle consume the allocated space.
1770 @retval EFI_INVALID_PARAMETER Invalid parameter.
1771 @retval EFI_NOT_FOUND No descriptor contains the desired space.
1772 @retval EFI_SUCCESS IO space successfully allocated.
1777 CoreAllocateIoSpace (
1778 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1779 IN EFI_GCD_IO_TYPE GcdIoType
,
1782 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1783 IN EFI_HANDLE ImageHandle
,
1784 IN EFI_HANDLE DeviceHandle OPTIONAL
1787 if (BaseAddress
!= NULL
) {
1788 DEBUG ((DEBUG_GCD
, "GCD:AllocateIoSpace(Base=%016lx,Length=%016lx)\n", *BaseAddress
, Length
));
1790 DEBUG ((DEBUG_GCD
, "GCD:AllocateIoSpace(Base=<NULL>,Length=%016lx)\n", Length
));
1792 DEBUG ((DEBUG_GCD
, " GcdAllocateType = %a\n", mGcdAllocationTypeNames
[MIN (GcdAllocateType
, EfiGcdMaxAllocateType
)]));
1793 DEBUG ((DEBUG_GCD
, " GcdIoType = %a\n", mGcdIoTypeNames
[MIN (GcdIoType
, EfiGcdIoTypeMaximum
)]));
1794 DEBUG ((DEBUG_GCD
, " Alignment = %016lx\n", LShiftU64 (1, Alignment
)));
1795 DEBUG ((DEBUG_GCD
, " ImageHandle = %p\n", ImageHandle
));
1796 DEBUG ((DEBUG_GCD
, " DeviceHandle = %p\n", DeviceHandle
));
1798 return CoreAllocateSpace (
1799 GCD_ALLOCATE_IO_OPERATION
,
1801 (EFI_GCD_MEMORY_TYPE
) 0,
1813 Frees nonexistent I/O, reserved I/O, or I/O resources from the global coherency
1814 domain of the processor.
1816 @param BaseAddress Base address of the segment.
1817 @param Length Length of the segment.
1819 @retval EFI_SUCCESS Space successfully freed.
1825 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1829 DEBUG ((DEBUG_GCD
, "GCD:FreeIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1831 return CoreConvertSpace (GCD_FREE_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1836 Removes reserved I/O or I/O resources from the global coherency domain of the
1839 @param BaseAddress Base address of the segment.
1840 @param Length Length of the segment.
1842 @retval EFI_SUCCESS Successfully removed a segment of IO space.
1848 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1852 DEBUG ((DEBUG_GCD
, "GCD:RemoveIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1854 return CoreConvertSpace (GCD_REMOVE_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1859 Build a IO descriptor according to an entry.
1861 @param Descriptor The descriptor to be built
1862 @param Entry According to this entry
1867 IN EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
,
1868 IN EFI_GCD_MAP_ENTRY
*Entry
1871 Descriptor
->BaseAddress
= Entry
->BaseAddress
;
1872 Descriptor
->Length
= Entry
->EndAddress
- Entry
->BaseAddress
+ 1;
1873 Descriptor
->GcdIoType
= Entry
->GcdIoType
;
1874 Descriptor
->ImageHandle
= Entry
->ImageHandle
;
1875 Descriptor
->DeviceHandle
= Entry
->DeviceHandle
;
1880 Retrieves the descriptor for an I/O region containing a specified address.
1882 @param BaseAddress Specified start address
1883 @param Descriptor Specified length
1885 @retval EFI_INVALID_PARAMETER Descriptor is NULL.
1886 @retval EFI_SUCCESS Successfully get the IO space descriptor.
1891 CoreGetIoSpaceDescriptor (
1892 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1893 OUT EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
1897 LIST_ENTRY
*StartLink
;
1898 LIST_ENTRY
*EndLink
;
1899 EFI_GCD_MAP_ENTRY
*Entry
;
1902 // Make sure parameters are valid
1904 if (Descriptor
== NULL
) {
1905 return EFI_INVALID_PARAMETER
;
1908 CoreAcquireGcdIoLock ();
1911 // Search for the list of descriptors that contain BaseAddress
1913 Status
= CoreSearchGcdMapEntry (BaseAddress
, 1, &StartLink
, &EndLink
, &mGcdIoSpaceMap
);
1914 if (EFI_ERROR (Status
)) {
1915 Status
= EFI_NOT_FOUND
;
1917 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1919 // Copy the contents of the found descriptor into Descriptor
1921 Entry
= CR (StartLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1922 BuildIoDescriptor (Descriptor
, Entry
);
1925 CoreReleaseGcdIoLock ();
1932 Returns a map of the I/O resources in the global coherency domain of the processor.
1934 @param NumberOfDescriptors Number of descriptors.
1935 @param IoSpaceMap Descriptor array
1937 @retval EFI_INVALID_PARAMETER Invalid parameter
1938 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1939 @retval EFI_SUCCESS Successfully get IO space map.
1945 OUT UINTN
*NumberOfDescriptors
,
1946 OUT EFI_GCD_IO_SPACE_DESCRIPTOR
**IoSpaceMap
1951 EFI_GCD_MAP_ENTRY
*Entry
;
1952 EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
;
1955 // Make sure parameters are valid
1957 if (NumberOfDescriptors
== NULL
) {
1958 return EFI_INVALID_PARAMETER
;
1960 if (IoSpaceMap
== NULL
) {
1961 return EFI_INVALID_PARAMETER
;
1964 CoreAcquireGcdIoLock ();
1967 // Count the number of descriptors
1969 *NumberOfDescriptors
= CoreCountGcdMapEntry (&mGcdIoSpaceMap
);
1972 // Allocate the IoSpaceMap
1974 *IoSpaceMap
= AllocatePool (*NumberOfDescriptors
* sizeof (EFI_GCD_IO_SPACE_DESCRIPTOR
));
1975 if (*IoSpaceMap
== NULL
) {
1976 Status
= EFI_OUT_OF_RESOURCES
;
1981 // Fill in the IoSpaceMap
1983 Descriptor
= *IoSpaceMap
;
1984 Link
= mGcdIoSpaceMap
.ForwardLink
;
1985 while (Link
!= &mGcdIoSpaceMap
) {
1986 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1987 BuildIoDescriptor (Descriptor
, Entry
);
1989 Link
= Link
->ForwardLink
;
1991 Status
= EFI_SUCCESS
;
1994 CoreReleaseGcdIoLock ();
2000 Converts a Resource Descriptor HOB attributes mask to an EFI Memory Descriptor
2003 @param GcdMemoryType Type of resource in the GCD memory map.
2004 @param Attributes The attribute mask in the Resource Descriptor
2007 @return The capabilities mask for an EFI Memory Descriptor.
2011 CoreConvertResourceDescriptorHobAttributesToCapabilities (
2012 EFI_GCD_MEMORY_TYPE GcdMemoryType
,
2016 UINT64 Capabilities
;
2017 GCD_ATTRIBUTE_CONVERSION_ENTRY
*Conversion
;
2020 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2022 for (Capabilities
= 0, Conversion
= mAttributeConversionTable
; Conversion
->Attribute
!= 0; Conversion
++) {
2023 if (Conversion
->Memory
|| ((GcdMemoryType
!= EfiGcdMemoryTypeSystemMemory
) && (GcdMemoryType
!= EfiGcdMemoryTypeMoreReliable
))) {
2024 if (Attributes
& Conversion
->Attribute
) {
2025 Capabilities
|= Conversion
->Capability
;
2030 return Capabilities
;
2034 Calculate total memory bin size neeeded.
2036 @return The total memory bin size neeeded.
2040 CalculateTotalMemoryBinSizeNeeded (
2048 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
2051 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
2052 TotalSize
+= LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
);
2059 External function. Initializes memory services based on the memory
2060 descriptor HOBs. This function is responsible for priming the memory
2061 map, so memory allocations and resource allocations can be made.
2062 The first part of this function can not depend on any memory services
2063 until at least one memory descriptor is provided to the memory services.
2065 @param HobStart The start address of the HOB.
2066 @param MemoryBaseAddress Start address of memory region found to init DXE
2068 @param MemoryLength Length of memory region found to init DXE core.
2070 @retval EFI_SUCCESS Memory services successfully initialized.
2074 CoreInitializeMemoryServices (
2076 OUT EFI_PHYSICAL_ADDRESS
*MemoryBaseAddress
,
2077 OUT UINT64
*MemoryLength
2080 EFI_PEI_HOB_POINTERS Hob
;
2081 EFI_MEMORY_TYPE_INFORMATION
*EfiMemoryTypeInformation
;
2084 EFI_HOB_HANDOFF_INFO_TABLE
*PhitHob
;
2085 EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
;
2086 EFI_HOB_RESOURCE_DESCRIPTOR
*PhitResourceHob
;
2087 EFI_PHYSICAL_ADDRESS BaseAddress
;
2090 UINT64 Capabilities
;
2091 EFI_PHYSICAL_ADDRESS TestedMemoryBaseAddress
;
2092 UINT64 TestedMemoryLength
;
2093 EFI_PHYSICAL_ADDRESS HighAddress
;
2094 EFI_HOB_GUID_TYPE
*GuidHob
;
2095 UINT32 ReservedCodePageNumber
;
2096 UINT64 MinimalMemorySizeNeeded
;
2099 // Point at the first HOB. This must be the PHIT HOB.
2101 Hob
.Raw
= *HobStart
;
2102 ASSERT (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_HANDOFF
);
2105 // Initialize the spin locks and maps in the memory services.
2106 // Also fill in the memory services into the EFI Boot Services Table
2108 CoreInitializePool ();
2111 // Initialize Local Variables
2113 PhitResourceHob
= NULL
;
2120 // Cache the PHIT HOB for later use
2122 PhitHob
= Hob
.HandoffInformationTable
;
2124 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
2125 ReservedCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
2126 ReservedCodePageNumber
+= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
2129 // cache the Top address for loading modules at Fixed Address
2131 gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
= PhitHob
->EfiMemoryTop
2132 + EFI_PAGES_TO_SIZE(ReservedCodePageNumber
);
2135 // See if a Memory Type Information HOB is available
2137 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
2138 if (GuidHob
!= NULL
) {
2139 EfiMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
2140 DataSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
2141 if (EfiMemoryTypeInformation
!= NULL
&& DataSize
> 0 && DataSize
<= (EfiMaxMemoryType
+ 1) * sizeof (EFI_MEMORY_TYPE_INFORMATION
)) {
2142 CopyMem (&gMemoryTypeInformation
, EfiMemoryTypeInformation
, DataSize
);
2147 // Include the total memory bin size needed to make sure memory bin could be allocated successfully.
2149 MinimalMemorySizeNeeded
= MINIMUM_INITIAL_MEMORY_SIZE
+ CalculateTotalMemoryBinSizeNeeded ();
2152 // Find the Resource Descriptor HOB that contains PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop
2155 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2157 // Skip all HOBs except Resource Descriptor HOBs
2159 if (GET_HOB_TYPE (Hob
) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2164 // Skip Resource Descriptor HOBs that do not describe tested system memory
2166 ResourceHob
= Hob
.ResourceDescriptor
;
2167 if (ResourceHob
->ResourceType
!= EFI_RESOURCE_SYSTEM_MEMORY
) {
2170 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) != TESTED_MEMORY_ATTRIBUTES
) {
2175 // Skip Resource Descriptor HOBs that do not contain the PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop
2177 if (PhitHob
->EfiFreeMemoryBottom
< ResourceHob
->PhysicalStart
) {
2180 if (PhitHob
->EfiFreeMemoryTop
> (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
)) {
2185 // Cache the resource descriptor HOB for the memory region described by the PHIT HOB
2187 PhitResourceHob
= ResourceHob
;
2191 // Compute range between PHIT EfiMemoryTop and the end of the Resource Descriptor HOB
2193 Attributes
= PhitResourceHob
->ResourceAttribute
;
2194 BaseAddress
= PageAlignAddress (PhitHob
->EfiMemoryTop
);
2195 Length
= PageAlignLength (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
- BaseAddress
);
2196 if (Length
< MinimalMemorySizeNeeded
) {
2198 // If that range is not large enough to intialize the DXE Core, then
2199 // Compute range between PHIT EfiFreeMemoryBottom and PHIT EfiFreeMemoryTop
2201 BaseAddress
= PageAlignAddress (PhitHob
->EfiFreeMemoryBottom
);
2202 Length
= PageAlignLength (PhitHob
->EfiFreeMemoryTop
- BaseAddress
);
2203 if (Length
< MinimalMemorySizeNeeded
) {
2205 // If that range is not large enough to intialize the DXE Core, then
2206 // Compute range between the start of the Resource Descriptor HOB and the start of the HOB List
2208 BaseAddress
= PageAlignAddress (ResourceHob
->PhysicalStart
);
2209 Length
= PageAlignLength ((UINT64
)((UINTN
)*HobStart
- BaseAddress
));
2216 // Assert if a resource descriptor HOB for the memory region described by the PHIT was not found
2221 // Take the range in the resource descriptor HOB for the memory region described
2222 // by the PHIT as higher priority if it is big enough. It can make the memory bin
2223 // allocated to be at the same memory region with PHIT that has more better compatibility
2224 // to avoid memory fragmentation for some code practices assume and allocate <4G ACPI memory.
2226 if (Length
< MinimalMemorySizeNeeded
) {
2228 // Search all the resource descriptor HOBs from the highest possible addresses down for a memory
2229 // region that is big enough to initialize the DXE core. Always skip the PHIT Resource HOB.
2230 // The max address must be within the physically addressible range for the processor.
2232 HighAddress
= MAX_ADDRESS
;
2233 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2235 // Skip the Resource Descriptor HOB that contains the PHIT
2237 if (Hob
.ResourceDescriptor
== PhitResourceHob
) {
2241 // Skip all HOBs except Resource Descriptor HOBs
2243 if (GET_HOB_TYPE (Hob
) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2248 // Skip Resource Descriptor HOBs that do not describe tested system memory below MAX_ADDRESS
2250 ResourceHob
= Hob
.ResourceDescriptor
;
2251 if (ResourceHob
->ResourceType
!= EFI_RESOURCE_SYSTEM_MEMORY
) {
2254 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) != TESTED_MEMORY_ATTRIBUTES
) {
2257 if ((ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
) > (EFI_PHYSICAL_ADDRESS
)MAX_ADDRESS
) {
2262 // Skip Resource Descriptor HOBs that are below a previously found Resource Descriptor HOB
2264 if (HighAddress
!= (EFI_PHYSICAL_ADDRESS
)MAX_ADDRESS
&& ResourceHob
->PhysicalStart
<= HighAddress
) {
2269 // Skip Resource Descriptor HOBs that are not large enough to initilize the DXE Core
2271 TestedMemoryBaseAddress
= PageAlignAddress (ResourceHob
->PhysicalStart
);
2272 TestedMemoryLength
= PageAlignLength (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
- TestedMemoryBaseAddress
);
2273 if (TestedMemoryLength
< MinimalMemorySizeNeeded
) {
2278 // Save the range described by the Resource Descriptor that is large enough to initilize the DXE Core
2280 BaseAddress
= TestedMemoryBaseAddress
;
2281 Length
= TestedMemoryLength
;
2282 Attributes
= ResourceHob
->ResourceAttribute
;
2283 HighAddress
= ResourceHob
->PhysicalStart
;
2287 DEBUG ((EFI_D_INFO
, "CoreInitializeMemoryServices:\n"));
2288 DEBUG ((EFI_D_INFO
, " BaseAddress - 0x%lx Length - 0x%lx MinimalMemorySizeNeeded - 0x%lx\n", BaseAddress
, Length
, MinimalMemorySizeNeeded
));
2291 // If no memory regions are found that are big enough to initialize the DXE core, then ASSERT().
2293 ASSERT (Length
>= MinimalMemorySizeNeeded
);
2296 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2298 if ((Attributes
& EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) == EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) {
2299 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeMoreReliable
, Attributes
);
2301 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeSystemMemory
, Attributes
);
2305 // Declare the very first memory region, so the EFI Memory Services are available.
2307 CoreAddMemoryDescriptor (
2308 EfiConventionalMemory
,
2310 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2314 *MemoryBaseAddress
= BaseAddress
;
2315 *MemoryLength
= Length
;
2322 External function. Initializes the GCD and memory services based on the memory
2323 descriptor HOBs. This function is responsible for priming the GCD map and the
2324 memory map, so memory allocations and resource allocations can be made. The
2325 HobStart will be relocated to a pool buffer.
2327 @param HobStart The start address of the HOB
2328 @param MemoryBaseAddress Start address of memory region found to init DXE
2330 @param MemoryLength Length of memory region found to init DXE core.
2332 @retval EFI_SUCCESS GCD services successfully initialized.
2336 CoreInitializeGcdServices (
2337 IN OUT VOID
**HobStart
,
2338 IN EFI_PHYSICAL_ADDRESS MemoryBaseAddress
,
2339 IN UINT64 MemoryLength
2342 EFI_PEI_HOB_POINTERS Hob
;
2344 EFI_HOB_HANDOFF_INFO_TABLE
*PhitHob
;
2345 UINT8 SizeOfMemorySpace
;
2346 UINT8 SizeOfIoSpace
;
2347 EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
;
2348 EFI_PHYSICAL_ADDRESS BaseAddress
;
2351 EFI_GCD_MAP_ENTRY
*Entry
;
2352 EFI_GCD_MEMORY_TYPE GcdMemoryType
;
2353 EFI_GCD_IO_TYPE GcdIoType
;
2354 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
2355 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
2356 EFI_HOB_FIRMWARE_VOLUME
*FirmwareVolumeHob
;
2357 UINTN NumberOfDescriptors
;
2358 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
2360 UINT64 Capabilities
;
2361 EFI_HOB_CPU
* CpuHob
;
2362 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMapHobList
;
2365 // Cache the PHIT HOB for later use
2367 PhitHob
= (EFI_HOB_HANDOFF_INFO_TABLE
*)(*HobStart
);
2370 // Get the number of address lines in the I/O and Memory space for the CPU
2372 CpuHob
= GetFirstHob (EFI_HOB_TYPE_CPU
);
2373 ASSERT (CpuHob
!= NULL
);
2374 SizeOfMemorySpace
= CpuHob
->SizeOfMemorySpace
;
2375 SizeOfIoSpace
= CpuHob
->SizeOfIoSpace
;
2378 // Initialize the GCD Memory Space Map
2380 Entry
= AllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY
), &mGcdMemorySpaceMapEntryTemplate
);
2381 ASSERT (Entry
!= NULL
);
2383 Entry
->EndAddress
= LShiftU64 (1, SizeOfMemorySpace
) - 1;
2385 InsertHeadList (&mGcdMemorySpaceMap
, &Entry
->Link
);
2387 CoreDumpGcdMemorySpaceMap (TRUE
);
2390 // Initialize the GCD I/O Space Map
2392 Entry
= AllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY
), &mGcdIoSpaceMapEntryTemplate
);
2393 ASSERT (Entry
!= NULL
);
2395 Entry
->EndAddress
= LShiftU64 (1, SizeOfIoSpace
) - 1;
2397 InsertHeadList (&mGcdIoSpaceMap
, &Entry
->Link
);
2399 CoreDumpGcdIoSpaceMap (TRUE
);
2402 // Walk the HOB list and add all resource descriptors to the GCD
2404 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2406 GcdMemoryType
= EfiGcdMemoryTypeNonExistent
;
2407 GcdIoType
= EfiGcdIoTypeNonExistent
;
2409 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2411 ResourceHob
= Hob
.ResourceDescriptor
;
2413 switch (ResourceHob
->ResourceType
) {
2414 case EFI_RESOURCE_SYSTEM_MEMORY
:
2415 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == TESTED_MEMORY_ATTRIBUTES
) {
2416 if ((ResourceHob
->ResourceAttribute
& EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) == EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) {
2417 GcdMemoryType
= EfiGcdMemoryTypeMoreReliable
;
2419 GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
2422 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == INITIALIZED_MEMORY_ATTRIBUTES
) {
2423 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2425 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == PRESENT_MEMORY_ATTRIBUTES
) {
2426 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2428 if ((ResourceHob
->ResourceAttribute
& EFI_RESOURCE_ATTRIBUTE_PERSISTENT
) == EFI_RESOURCE_ATTRIBUTE_PERSISTENT
) {
2429 GcdMemoryType
= EfiGcdMemoryTypePersistent
;
2432 case EFI_RESOURCE_MEMORY_MAPPED_IO
:
2433 case EFI_RESOURCE_FIRMWARE_DEVICE
:
2434 GcdMemoryType
= EfiGcdMemoryTypeMemoryMappedIo
;
2436 case EFI_RESOURCE_MEMORY_MAPPED_IO_PORT
:
2437 case EFI_RESOURCE_MEMORY_RESERVED
:
2438 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2440 case EFI_RESOURCE_IO
:
2441 GcdIoType
= EfiGcdIoTypeIo
;
2443 case EFI_RESOURCE_IO_RESERVED
:
2444 GcdIoType
= EfiGcdIoTypeReserved
;
2448 if (GcdMemoryType
!= EfiGcdMemoryTypeNonExistent
) {
2450 // Validate the Resource HOB Attributes
2452 CoreValidateResourceDescriptorHobAttributes (ResourceHob
->ResourceAttribute
);
2455 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2457 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (
2459 ResourceHob
->ResourceAttribute
2462 Status
= CoreInternalAddMemorySpace (
2464 ResourceHob
->PhysicalStart
,
2465 ResourceHob
->ResourceLength
,
2470 if (GcdIoType
!= EfiGcdIoTypeNonExistent
) {
2471 Status
= CoreAddIoSpace (
2473 ResourceHob
->PhysicalStart
,
2474 ResourceHob
->ResourceLength
2481 // Allocate first memory region from the GCD by the DXE core
2483 Status
= CoreGetMemorySpaceDescriptor (MemoryBaseAddress
, &Descriptor
);
2484 if (!EFI_ERROR (Status
)) {
2485 ASSERT ((Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) ||
2486 (Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
));
2487 Status
= CoreAllocateMemorySpace (
2488 EfiGcdAllocateAddress
,
2489 Descriptor
.GcdMemoryType
,
2493 gDxeCoreImageHandle
,
2499 // Walk the HOB list and allocate all memory space that is consumed by memory allocation HOBs,
2500 // and Firmware Volume HOBs. Also update the EFI Memory Map with the memory allocation HOBs.
2502 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2503 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
2504 MemoryHob
= Hob
.MemoryAllocation
;
2505 BaseAddress
= MemoryHob
->AllocDescriptor
.MemoryBaseAddress
;
2506 Status
= CoreGetMemorySpaceDescriptor (BaseAddress
, &Descriptor
);
2507 if (!EFI_ERROR (Status
)) {
2508 Status
= CoreAllocateMemorySpace (
2509 EfiGcdAllocateAddress
,
2510 Descriptor
.GcdMemoryType
,
2512 MemoryHob
->AllocDescriptor
.MemoryLength
,
2514 gDxeCoreImageHandle
,
2517 if (!EFI_ERROR (Status
) &&
2518 ((Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) ||
2519 (Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
))) {
2520 CoreAddMemoryDescriptor (
2521 MemoryHob
->AllocDescriptor
.MemoryType
,
2522 MemoryHob
->AllocDescriptor
.MemoryBaseAddress
,
2523 RShiftU64 (MemoryHob
->AllocDescriptor
.MemoryLength
, EFI_PAGE_SHIFT
),
2524 Descriptor
.Capabilities
& (~EFI_MEMORY_RUNTIME
)
2530 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_FV
) {
2531 FirmwareVolumeHob
= Hob
.FirmwareVolume
;
2532 BaseAddress
= FirmwareVolumeHob
->BaseAddress
;
2533 Status
= CoreAllocateMemorySpace (
2534 EfiGcdAllocateAddress
,
2535 EfiGcdMemoryTypeMemoryMappedIo
,
2537 FirmwareVolumeHob
->Length
,
2539 gDxeCoreImageHandle
,
2546 // Add and allocate the remaining unallocated system memory to the memory services.
2548 Status
= CoreGetMemorySpaceMap (&NumberOfDescriptors
, &MemorySpaceMap
);
2549 ASSERT (Status
== EFI_SUCCESS
);
2551 MemorySpaceMapHobList
= NULL
;
2552 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
2553 if ((MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) ||
2554 (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
)) {
2555 if (MemorySpaceMap
[Index
].ImageHandle
== NULL
) {
2556 BaseAddress
= PageAlignAddress (MemorySpaceMap
[Index
].BaseAddress
);
2557 Length
= PageAlignLength (MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- BaseAddress
);
2558 if (Length
== 0 || MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
< BaseAddress
) {
2561 if (((UINTN
) MemorySpaceMap
[Index
].BaseAddress
<= (UINTN
) (*HobStart
)) &&
2562 ((UINTN
) (MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) >= (UINTN
) PhitHob
->EfiFreeMemoryBottom
)) {
2564 // Skip the memory space that covers HOB List, it should be processed
2565 // after HOB List relocation to avoid the resources allocated by others
2566 // to corrupt HOB List before its relocation.
2568 MemorySpaceMapHobList
= &MemorySpaceMap
[Index
];
2571 CoreAddMemoryDescriptor (
2572 EfiConventionalMemory
,
2574 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2575 MemorySpaceMap
[Index
].Capabilities
& (~EFI_MEMORY_RUNTIME
)
2577 Status
= CoreAllocateMemorySpace (
2578 EfiGcdAllocateAddress
,
2579 MemorySpaceMap
[Index
].GcdMemoryType
,
2583 gDxeCoreImageHandle
,
2591 // Relocate HOB List to an allocated pool buffer.
2592 // The relocation should be at after all the tested memory resources added
2593 // (except the memory space that covers HOB List) to the memory services,
2594 // because the memory resource found in CoreInitializeMemoryServices()
2595 // may have not enough remaining resource for HOB List.
2597 NewHobList
= AllocateCopyPool (
2598 (UINTN
) PhitHob
->EfiFreeMemoryBottom
- (UINTN
) (*HobStart
),
2601 ASSERT (NewHobList
!= NULL
);
2603 *HobStart
= NewHobList
;
2604 gHobList
= NewHobList
;
2606 if (MemorySpaceMapHobList
!= NULL
) {
2608 // Add and allocate the memory space that covers HOB List to the memory services
2609 // after HOB List relocation.
2611 BaseAddress
= PageAlignAddress (MemorySpaceMapHobList
->BaseAddress
);
2612 Length
= PageAlignLength (MemorySpaceMapHobList
->BaseAddress
+ MemorySpaceMapHobList
->Length
- BaseAddress
);
2613 CoreAddMemoryDescriptor (
2614 EfiConventionalMemory
,
2616 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2617 MemorySpaceMapHobList
->Capabilities
& (~EFI_MEMORY_RUNTIME
)
2619 Status
= CoreAllocateMemorySpace (
2620 EfiGcdAllocateAddress
,
2621 MemorySpaceMapHobList
->GcdMemoryType
,
2625 gDxeCoreImageHandle
,
2630 CoreFreePool (MemorySpaceMap
);