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.
20 #define MINIMUM_INITIAL_MEMORY_SIZE 0x10000
22 #define MEMORY_ATTRIBUTE_MASK (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
23 EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \
24 EFI_RESOURCE_ATTRIBUTE_TESTED | \
25 EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED | \
26 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED | \
27 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED | \
28 EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED | \
29 EFI_RESOURCE_ATTRIBUTE_16_BIT_IO | \
30 EFI_RESOURCE_ATTRIBUTE_32_BIT_IO | \
31 EFI_RESOURCE_ATTRIBUTE_64_BIT_IO | \
32 EFI_RESOURCE_ATTRIBUTE_PERSISTENT )
34 #define TESTED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
35 EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \
36 EFI_RESOURCE_ATTRIBUTE_TESTED )
38 #define INITIALIZED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
39 EFI_RESOURCE_ATTRIBUTE_INITIALIZED )
41 #define PRESENT_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT)
43 #define INVALID_CPU_ARCH_ATTRIBUTES 0xffffffff
48 EFI_LOCK mGcdMemorySpaceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
49 EFI_LOCK mGcdIoSpaceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
50 LIST_ENTRY mGcdMemorySpaceMap
= INITIALIZE_LIST_HEAD_VARIABLE (mGcdMemorySpaceMap
);
51 LIST_ENTRY mGcdIoSpaceMap
= INITIALIZE_LIST_HEAD_VARIABLE (mGcdIoSpaceMap
);
53 EFI_GCD_MAP_ENTRY mGcdMemorySpaceMapEntryTemplate
= {
54 EFI_GCD_MAP_SIGNATURE
,
63 EfiGcdMemoryTypeNonExistent
,
69 EFI_GCD_MAP_ENTRY mGcdIoSpaceMapEntryTemplate
= {
70 EFI_GCD_MAP_SIGNATURE
,
79 (EFI_GCD_MEMORY_TYPE
) 0,
80 EfiGcdIoTypeNonExistent
,
85 GCD_ATTRIBUTE_CONVERSION_ENTRY mAttributeConversionTable
[] = {
86 { EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
, EFI_MEMORY_UC
, TRUE
},
87 { EFI_RESOURCE_ATTRIBUTE_UNCACHED_EXPORTED
, EFI_MEMORY_UCE
, TRUE
},
88 { EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
, EFI_MEMORY_WC
, TRUE
},
89 { EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
, EFI_MEMORY_WT
, TRUE
},
90 { EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
, EFI_MEMORY_WB
, TRUE
},
91 { EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE
, EFI_MEMORY_RP
, TRUE
},
92 { EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE
, EFI_MEMORY_WP
, TRUE
},
93 { EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE
, EFI_MEMORY_XP
, TRUE
},
94 { EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE
, EFI_MEMORY_RO
, TRUE
},
95 { EFI_RESOURCE_ATTRIBUTE_PRESENT
, EFI_MEMORY_PRESENT
, FALSE
},
96 { EFI_RESOURCE_ATTRIBUTE_INITIALIZED
, EFI_MEMORY_INITIALIZED
, FALSE
},
97 { EFI_RESOURCE_ATTRIBUTE_TESTED
, EFI_MEMORY_TESTED
, FALSE
},
98 { EFI_RESOURCE_ATTRIBUTE_PERSISTABLE
, EFI_MEMORY_NV
, TRUE
},
99 { EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
, EFI_MEMORY_MORE_RELIABLE
, TRUE
},
104 /// Lookup table used to print GCD Memory Space Map
106 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mGcdMemoryTypeNames
[] = {
107 "NonExist ", // EfiGcdMemoryTypeNonExistent
108 "Reserved ", // EfiGcdMemoryTypeReserved
109 "SystemMem", // EfiGcdMemoryTypeSystemMemory
110 "MMIO ", // EfiGcdMemoryTypeMemoryMappedIo
111 "PersisMem", // EfiGcdMemoryTypePersistentMemory
112 "MoreRelia", // EfiGcdMemoryTypeMoreReliable
113 "Unknown " // EfiGcdMemoryTypeMaximum
117 /// Lookup table used to print GCD I/O Space Map
119 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mGcdIoTypeNames
[] = {
120 "NonExist", // EfiGcdIoTypeNonExistent
121 "Reserved", // EfiGcdIoTypeReserved
122 "I/O ", // EfiGcdIoTypeIo
123 "Unknown " // EfiGcdIoTypeMaximum
127 /// Lookup table used to print GCD Allocation Types
129 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mGcdAllocationTypeNames
[] = {
130 "AnySearchBottomUp ", // EfiGcdAllocateAnySearchBottomUp
131 "MaxAddressSearchBottomUp ", // EfiGcdAllocateMaxAddressSearchBottomUp
132 "AtAddress ", // EfiGcdAllocateAddress
133 "AnySearchTopDown ", // EfiGcdAllocateAnySearchTopDown
134 "MaxAddressSearchTopDown ", // EfiGcdAllocateMaxAddressSearchTopDown
135 "Unknown " // EfiGcdMaxAllocateType
139 Dump the entire contents if the GCD Memory Space Map using DEBUG() macros when
140 PcdDebugPrintErrorLevel has the DEBUG_GCD bit set.
142 @param InitialMap TRUE if the initial GCD Memory Map is being dumped. Otherwise, FALSE.
147 CoreDumpGcdMemorySpaceMap (
153 UINTN NumberOfDescriptors
;
154 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
157 Status
= CoreGetMemorySpaceMap (&NumberOfDescriptors
, &MemorySpaceMap
);
158 ASSERT (Status
== EFI_SUCCESS
&& MemorySpaceMap
!= NULL
);
161 DEBUG ((DEBUG_GCD
, "GCD:Initial GCD Memory Space Map\n"));
163 DEBUG ((DEBUG_GCD
, "GCDMemType Range Capabilities Attributes \n"));
164 DEBUG ((DEBUG_GCD
, "========== ================================= ================ ================\n"));
165 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
166 DEBUG ((DEBUG_GCD
, "%a %016lx-%016lx %016lx %016lx%c\n",
167 mGcdMemoryTypeNames
[MIN (MemorySpaceMap
[Index
].GcdMemoryType
, EfiGcdMemoryTypeMaximum
)],
168 MemorySpaceMap
[Index
].BaseAddress
,
169 MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- 1,
170 MemorySpaceMap
[Index
].Capabilities
,
171 MemorySpaceMap
[Index
].Attributes
,
172 MemorySpaceMap
[Index
].ImageHandle
== NULL
? ' ' : '*'
175 DEBUG ((DEBUG_GCD
, "\n"));
176 FreePool (MemorySpaceMap
);
181 Dump the entire contents if the GCD I/O Space Map using DEBUG() macros when
182 PcdDebugPrintErrorLevel has the DEBUG_GCD bit set.
184 @param InitialMap TRUE if the initial GCD I/O Map is being dumped. Otherwise, FALSE.
189 CoreDumpGcdIoSpaceMap (
195 UINTN NumberOfDescriptors
;
196 EFI_GCD_IO_SPACE_DESCRIPTOR
*IoSpaceMap
;
199 Status
= CoreGetIoSpaceMap (&NumberOfDescriptors
, &IoSpaceMap
);
200 ASSERT (Status
== EFI_SUCCESS
&& IoSpaceMap
!= NULL
);
203 DEBUG ((DEBUG_GCD
, "GCD:Initial GCD I/O Space Map\n"));
206 DEBUG ((DEBUG_GCD
, "GCDIoType Range \n"));
207 DEBUG ((DEBUG_GCD
, "========== =================================\n"));
208 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
209 DEBUG ((DEBUG_GCD
, "%a %016lx-%016lx%c\n",
210 mGcdIoTypeNames
[MIN (IoSpaceMap
[Index
].GcdIoType
, EfiGcdIoTypeMaximum
)],
211 IoSpaceMap
[Index
].BaseAddress
,
212 IoSpaceMap
[Index
].BaseAddress
+ IoSpaceMap
[Index
].Length
- 1,
213 IoSpaceMap
[Index
].ImageHandle
== NULL
? ' ' : '*'
216 DEBUG ((DEBUG_GCD
, "\n"));
217 FreePool (IoSpaceMap
);
222 Validate resource descriptor HOB's attributes.
224 If Attributes includes some memory resource's settings, it should include
225 the corresponding capabilites also.
227 @param Attributes Resource descriptor HOB attributes.
231 CoreValidateResourceDescriptorHobAttributes (
235 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED
) == 0) ||
236 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE
) != 0));
237 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED
) == 0) ||
238 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE
) != 0));
239 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED
) == 0) ||
240 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE
) != 0));
241 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED
) == 0) ||
242 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE
) != 0));
243 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_PERSISTENT
) == 0) ||
244 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_PERSISTABLE
) != 0));
248 Acquire memory lock on mGcdMemorySpaceLock.
252 CoreAcquireGcdMemoryLock (
256 CoreAcquireLock (&mGcdMemorySpaceLock
);
262 Release memory lock on mGcdMemorySpaceLock.
266 CoreReleaseGcdMemoryLock (
270 CoreReleaseLock (&mGcdMemorySpaceLock
);
276 Acquire memory lock on mGcdIoSpaceLock.
280 CoreAcquireGcdIoLock (
284 CoreAcquireLock (&mGcdIoSpaceLock
);
289 Release memory lock on mGcdIoSpaceLock.
293 CoreReleaseGcdIoLock (
297 CoreReleaseLock (&mGcdIoSpaceLock
);
303 // GCD Initialization Worker Functions
306 Aligns a value to the specified boundary.
308 @param Value 64 bit value to align
309 @param Alignment Log base 2 of the boundary to align Value to
310 @param RoundUp TRUE if Value is to be rounded up to the nearest
311 aligned boundary. FALSE is Value is to be
312 rounded down to the nearest aligned boundary.
314 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
324 UINT64 AlignmentMask
;
326 AlignmentMask
= LShiftU64 (1, Alignment
) - 1;
328 Value
+= AlignmentMask
;
330 return Value
& (~AlignmentMask
);
335 Aligns address to the page boundary.
337 @param Value 64 bit address to align
339 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
347 return AlignValue (Value
, EFI_PAGE_SHIFT
, TRUE
);
352 Aligns length to the page boundary.
354 @param Value 64 bit length to align
356 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
364 return AlignValue (Value
, EFI_PAGE_SHIFT
, FALSE
);
368 // GCD Memory Space Worker Functions
372 Allocate pool for two entries.
374 @param TopEntry An entry of GCD map
375 @param BottomEntry An entry of GCD map
377 @retval EFI_OUT_OF_RESOURCES No enough buffer to be allocated.
378 @retval EFI_SUCCESS Both entries successfully allocated.
382 CoreAllocateGcdMapEntry (
383 IN OUT EFI_GCD_MAP_ENTRY
**TopEntry
,
384 IN OUT EFI_GCD_MAP_ENTRY
**BottomEntry
387 *TopEntry
= AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY
));
388 if (*TopEntry
== NULL
) {
389 return EFI_OUT_OF_RESOURCES
;
392 *BottomEntry
= AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY
));
393 if (*BottomEntry
== NULL
) {
394 CoreFreePool (*TopEntry
);
395 return EFI_OUT_OF_RESOURCES
;
403 Internal function. Inserts a new descriptor into a sorted list
405 @param Link The linked list to insert the range BaseAddress
407 @param Entry A pointer to the entry that is inserted
408 @param BaseAddress The base address of the new range
409 @param Length The length of the new range in bytes
410 @param TopEntry Top pad entry to insert if needed.
411 @param BottomEntry Bottom pad entry to insert if needed.
413 @retval EFI_SUCCESS The new range was inserted into the linked list
417 CoreInsertGcdMapEntry (
419 IN EFI_GCD_MAP_ENTRY
*Entry
,
420 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
422 IN EFI_GCD_MAP_ENTRY
*TopEntry
,
423 IN EFI_GCD_MAP_ENTRY
*BottomEntry
426 ASSERT (Length
!= 0);
428 if (BaseAddress
> Entry
->BaseAddress
) {
429 ASSERT (BottomEntry
->Signature
== 0);
431 CopyMem (BottomEntry
, Entry
, sizeof (EFI_GCD_MAP_ENTRY
));
432 Entry
->BaseAddress
= BaseAddress
;
433 BottomEntry
->EndAddress
= BaseAddress
- 1;
434 InsertTailList (Link
, &BottomEntry
->Link
);
437 if ((BaseAddress
+ Length
- 1) < Entry
->EndAddress
) {
438 ASSERT (TopEntry
->Signature
== 0);
440 CopyMem (TopEntry
, Entry
, sizeof (EFI_GCD_MAP_ENTRY
));
441 TopEntry
->BaseAddress
= BaseAddress
+ Length
;
442 Entry
->EndAddress
= BaseAddress
+ Length
- 1;
443 InsertHeadList (Link
, &TopEntry
->Link
);
451 Merge the Gcd region specified by Link and its adjacent entry.
453 @param Link Specify the entry to be merged (with its
455 @param Forward Direction (forward or backward).
458 @retval EFI_SUCCESS Successfully returned.
459 @retval EFI_UNSUPPORTED These adjacent regions could not merge.
463 CoreMergeGcdMapEntry (
469 LIST_ENTRY
*AdjacentLink
;
470 EFI_GCD_MAP_ENTRY
*Entry
;
471 EFI_GCD_MAP_ENTRY
*AdjacentEntry
;
474 // Get adjacent entry
477 AdjacentLink
= Link
->ForwardLink
;
479 AdjacentLink
= Link
->BackLink
;
483 // If AdjacentLink is the head of the list, then no merge can be performed
485 if (AdjacentLink
== Map
) {
489 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
490 AdjacentEntry
= CR (AdjacentLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
492 if (Entry
->Capabilities
!= AdjacentEntry
->Capabilities
) {
493 return EFI_UNSUPPORTED
;
495 if (Entry
->Attributes
!= AdjacentEntry
->Attributes
) {
496 return EFI_UNSUPPORTED
;
498 if (Entry
->GcdMemoryType
!= AdjacentEntry
->GcdMemoryType
) {
499 return EFI_UNSUPPORTED
;
501 if (Entry
->GcdIoType
!= AdjacentEntry
->GcdIoType
) {
502 return EFI_UNSUPPORTED
;
504 if (Entry
->ImageHandle
!= AdjacentEntry
->ImageHandle
) {
505 return EFI_UNSUPPORTED
;
507 if (Entry
->DeviceHandle
!= AdjacentEntry
->DeviceHandle
) {
508 return EFI_UNSUPPORTED
;
512 Entry
->EndAddress
= AdjacentEntry
->EndAddress
;
514 Entry
->BaseAddress
= AdjacentEntry
->BaseAddress
;
516 RemoveEntryList (AdjacentLink
);
517 CoreFreePool (AdjacentEntry
);
524 Merge adjacent entries on total chain.
526 @param TopEntry Top entry of GCD map.
527 @param BottomEntry Bottom entry of GCD map.
528 @param StartLink Start link of the list for this loop.
529 @param EndLink End link of the list for this loop.
532 @retval EFI_SUCCESS GCD map successfully cleaned up.
536 CoreCleanupGcdMapEntry (
537 IN EFI_GCD_MAP_ENTRY
*TopEntry
,
538 IN EFI_GCD_MAP_ENTRY
*BottomEntry
,
539 IN LIST_ENTRY
*StartLink
,
540 IN LIST_ENTRY
*EndLink
,
546 if (TopEntry
->Signature
== 0) {
547 CoreFreePool (TopEntry
);
549 if (BottomEntry
->Signature
== 0) {
550 CoreFreePool (BottomEntry
);
554 while (Link
!= EndLink
->ForwardLink
) {
555 CoreMergeGcdMapEntry (Link
, FALSE
, Map
);
556 Link
= Link
->ForwardLink
;
558 CoreMergeGcdMapEntry (EndLink
, TRUE
, Map
);
565 Search a segment of memory space in GCD map. The result is a range of GCD entry list.
567 @param BaseAddress The start address of the segment.
568 @param Length The length of the segment.
569 @param StartLink The first GCD entry involves this segment of
571 @param EndLink The first GCD entry involves this segment of
573 @param Map Points to the start entry to search.
575 @retval EFI_SUCCESS Successfully found the entry.
576 @retval EFI_NOT_FOUND Not found.
580 CoreSearchGcdMapEntry (
581 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
583 OUT LIST_ENTRY
**StartLink
,
584 OUT LIST_ENTRY
**EndLink
,
589 EFI_GCD_MAP_ENTRY
*Entry
;
591 ASSERT (Length
!= 0);
596 Link
= Map
->ForwardLink
;
597 while (Link
!= Map
) {
598 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
599 if (BaseAddress
>= Entry
->BaseAddress
&& BaseAddress
<= Entry
->EndAddress
) {
602 if (*StartLink
!= NULL
) {
603 if ((BaseAddress
+ Length
- 1) >= Entry
->BaseAddress
&&
604 (BaseAddress
+ Length
- 1) <= Entry
->EndAddress
) {
609 Link
= Link
->ForwardLink
;
612 return EFI_NOT_FOUND
;
617 Count the amount of GCD map entries.
619 @param Map Points to the start entry to do the count loop.
625 CoreCountGcdMapEntry (
633 Link
= Map
->ForwardLink
;
634 while (Link
!= Map
) {
636 Link
= Link
->ForwardLink
;
645 Return the memory attribute specified by Attributes
647 @param Attributes A num with some attribute bits on.
649 @return The enum value of memory attribute.
653 ConverToCpuArchAttributes (
657 if ( (Attributes
& EFI_MEMORY_UC
) == EFI_MEMORY_UC
) {
658 return EFI_MEMORY_UC
;
661 if ( (Attributes
& EFI_MEMORY_WC
) == EFI_MEMORY_WC
) {
662 return EFI_MEMORY_WC
;
665 if ( (Attributes
& EFI_MEMORY_WT
) == EFI_MEMORY_WT
) {
666 return EFI_MEMORY_WT
;
669 if ( (Attributes
& EFI_MEMORY_WB
) == EFI_MEMORY_WB
) {
670 return EFI_MEMORY_WB
;
673 if ( (Attributes
& EFI_MEMORY_WP
) == EFI_MEMORY_WP
) {
674 return EFI_MEMORY_WP
;
677 return INVALID_CPU_ARCH_ATTRIBUTES
;
683 Do operation on a segment of memory space specified (add, free, remove, change attribute ...).
685 @param Operation The type of the operation
686 @param GcdMemoryType Additional information for the operation
687 @param GcdIoType Additional information for the operation
688 @param BaseAddress Start address of the segment
689 @param Length length of the segment
690 @param Capabilities The alterable attributes of a newly added entry
691 @param Attributes The attributes needs to be set
693 @retval EFI_INVALID_PARAMETER Length is 0 or address (length) not aligned when
695 @retval EFI_SUCCESS Action successfully done.
696 @retval EFI_UNSUPPORTED Could not find the proper descriptor on this
697 segment or set an upsupported attribute.
698 @retval EFI_ACCESS_DENIED Operate on an space non-exist or is used for an
700 @retval EFI_NOT_FOUND Free a non-using space or remove a non-exist
702 @retval EFI_OUT_OF_RESOURCES No buffer could be allocated.
703 @retval EFI_NOT_AVAILABLE_YET The attributes cannot be set because CPU architectural protocol
704 is not available yet.
709 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
710 IN EFI_GCD_IO_TYPE GcdIoType
,
711 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
713 IN UINT64 Capabilities
,
720 EFI_GCD_MAP_ENTRY
*Entry
;
721 EFI_GCD_MAP_ENTRY
*TopEntry
;
722 EFI_GCD_MAP_ENTRY
*BottomEntry
;
723 LIST_ENTRY
*StartLink
;
725 UINT64 CpuArchAttributes
;
728 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
729 return EFI_INVALID_PARAMETER
;
733 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
734 CoreAcquireGcdMemoryLock ();
735 Map
= &mGcdMemorySpaceMap
;
736 } else if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
737 CoreAcquireGcdIoLock ();
738 Map
= &mGcdIoSpaceMap
;
744 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
746 Status
= CoreSearchGcdMapEntry (BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
747 if (EFI_ERROR (Status
)) {
748 Status
= EFI_UNSUPPORTED
;
752 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
755 // Verify that the list of descriptors are unallocated non-existent memory.
758 while (Link
!= EndLink
->ForwardLink
) {
759 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
764 case GCD_ADD_MEMORY_OPERATION
:
765 if (Entry
->GcdMemoryType
!= EfiGcdMemoryTypeNonExistent
||
766 Entry
->ImageHandle
!= NULL
) {
767 Status
= EFI_ACCESS_DENIED
;
771 case GCD_ADD_IO_OPERATION
:
772 if (Entry
->GcdIoType
!= EfiGcdIoTypeNonExistent
||
773 Entry
->ImageHandle
!= NULL
) {
774 Status
= EFI_ACCESS_DENIED
;
781 case GCD_FREE_MEMORY_OPERATION
:
782 case GCD_FREE_IO_OPERATION
:
783 if (Entry
->ImageHandle
== NULL
) {
784 Status
= EFI_NOT_FOUND
;
791 case GCD_REMOVE_MEMORY_OPERATION
:
792 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
793 Status
= EFI_NOT_FOUND
;
796 if (Entry
->ImageHandle
!= NULL
) {
797 Status
= EFI_ACCESS_DENIED
;
801 case GCD_REMOVE_IO_OPERATION
:
802 if (Entry
->GcdIoType
== EfiGcdIoTypeNonExistent
) {
803 Status
= EFI_NOT_FOUND
;
806 if (Entry
->ImageHandle
!= NULL
) {
807 Status
= EFI_ACCESS_DENIED
;
812 // Set attributes operation
814 case GCD_SET_ATTRIBUTES_MEMORY_OPERATION
:
815 if ((Attributes
& EFI_MEMORY_RUNTIME
) != 0) {
816 if ((BaseAddress
& EFI_PAGE_MASK
) != 0 || (Length
& EFI_PAGE_MASK
) != 0) {
817 Status
= EFI_INVALID_PARAMETER
;
821 if ((Entry
->Capabilities
& Attributes
) != Attributes
) {
822 Status
= EFI_UNSUPPORTED
;
827 // Set capabilities operation
829 case GCD_SET_CAPABILITIES_MEMORY_OPERATION
:
830 if ((BaseAddress
& EFI_PAGE_MASK
) != 0 || (Length
& EFI_PAGE_MASK
) != 0) {
831 Status
= EFI_INVALID_PARAMETER
;
836 // Current attributes must still be supported with new capabilities
838 if ((Capabilities
& Entry
->Attributes
) != Entry
->Attributes
) {
839 Status
= EFI_UNSUPPORTED
;
844 Link
= Link
->ForwardLink
;
848 // Allocate work space to perform this operation
850 Status
= CoreAllocateGcdMapEntry (&TopEntry
, &BottomEntry
);
851 if (EFI_ERROR (Status
)) {
852 Status
= EFI_OUT_OF_RESOURCES
;
855 ASSERT (TopEntry
!= NULL
&& BottomEntry
!= NULL
);
857 if (Operation
== GCD_SET_ATTRIBUTES_MEMORY_OPERATION
) {
859 // Call CPU Arch Protocol to attempt to set attributes on the range
861 CpuArchAttributes
= ConverToCpuArchAttributes (Attributes
);
862 if (CpuArchAttributes
!= INVALID_CPU_ARCH_ATTRIBUTES
) {
864 Status
= EFI_NOT_AVAILABLE_YET
;
866 Status
= gCpu
->SetMemoryAttributes (
873 if (EFI_ERROR (Status
)) {
874 CoreFreePool (TopEntry
);
875 CoreFreePool (BottomEntry
);
882 // Convert/Insert the list of descriptors from StartLink to EndLink
885 while (Link
!= EndLink
->ForwardLink
) {
886 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
887 CoreInsertGcdMapEntry (Link
, Entry
, BaseAddress
, Length
, TopEntry
, BottomEntry
);
892 case GCD_ADD_MEMORY_OPERATION
:
893 Entry
->GcdMemoryType
= GcdMemoryType
;
894 if (GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
895 Entry
->Capabilities
= Capabilities
| EFI_MEMORY_RUNTIME
| EFI_MEMORY_PORT_IO
;
897 Entry
->Capabilities
= Capabilities
| EFI_MEMORY_RUNTIME
;
900 case GCD_ADD_IO_OPERATION
:
901 Entry
->GcdIoType
= GcdIoType
;
906 case GCD_FREE_MEMORY_OPERATION
:
907 case GCD_FREE_IO_OPERATION
:
908 Entry
->ImageHandle
= NULL
;
909 Entry
->DeviceHandle
= NULL
;
914 case GCD_REMOVE_MEMORY_OPERATION
:
915 Entry
->GcdMemoryType
= EfiGcdMemoryTypeNonExistent
;
916 Entry
->Capabilities
= 0;
918 case GCD_REMOVE_IO_OPERATION
:
919 Entry
->GcdIoType
= EfiGcdIoTypeNonExistent
;
922 // Set attributes operation
924 case GCD_SET_ATTRIBUTES_MEMORY_OPERATION
:
925 Entry
->Attributes
= Attributes
;
928 // Set capabilities operation
930 case GCD_SET_CAPABILITIES_MEMORY_OPERATION
:
931 Entry
->Capabilities
= Capabilities
;
934 Link
= Link
->ForwardLink
;
940 Status
= CoreCleanupGcdMapEntry (TopEntry
, BottomEntry
, StartLink
, EndLink
, Map
);
943 DEBUG ((DEBUG_GCD
, " Status = %r\n", Status
));
945 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
946 CoreReleaseGcdMemoryLock ();
947 CoreDumpGcdMemorySpaceMap (FALSE
);
949 if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
950 CoreReleaseGcdIoLock ();
951 CoreDumpGcdIoSpaceMap (FALSE
);
959 Check whether an entry could be used to allocate space.
961 @param Operation Allocate memory or IO
962 @param Entry The entry to be tested
963 @param GcdMemoryType The desired memory type
964 @param GcdIoType The desired IO type
966 @retval EFI_NOT_FOUND The memory type does not match or there's an
967 image handle on the entry.
968 @retval EFI_UNSUPPORTED The operation unsupported.
969 @retval EFI_SUCCESS It's ok for this entry to be used to allocate
974 CoreAllocateSpaceCheckEntry (
976 IN EFI_GCD_MAP_ENTRY
*Entry
,
977 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
978 IN EFI_GCD_IO_TYPE GcdIoType
981 if (Entry
->ImageHandle
!= NULL
) {
982 return EFI_NOT_FOUND
;
985 case GCD_ALLOCATE_MEMORY_OPERATION
:
986 if (Entry
->GcdMemoryType
!= GcdMemoryType
) {
987 return EFI_NOT_FOUND
;
990 case GCD_ALLOCATE_IO_OPERATION
:
991 if (Entry
->GcdIoType
!= GcdIoType
) {
992 return EFI_NOT_FOUND
;
996 return EFI_UNSUPPORTED
;
1003 Allocate space on specified address and length.
1005 @param Operation The type of operation (memory or IO)
1006 @param GcdAllocateType The type of allocate operation
1007 @param GcdMemoryType The desired memory type
1008 @param GcdIoType The desired IO type
1009 @param Alignment Align with 2^Alignment
1010 @param Length Length to allocate
1011 @param BaseAddress Base address to allocate
1012 @param ImageHandle The image handle consume the allocated space.
1013 @param DeviceHandle The device handle consume the allocated space.
1015 @retval EFI_INVALID_PARAMETER Invalid parameter.
1016 @retval EFI_NOT_FOUND No descriptor for the desired space exists.
1017 @retval EFI_SUCCESS Space successfully allocated.
1023 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1024 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1025 IN EFI_GCD_IO_TYPE GcdIoType
,
1028 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1029 IN EFI_HANDLE ImageHandle
,
1030 IN EFI_HANDLE DeviceHandle OPTIONAL
1034 EFI_PHYSICAL_ADDRESS AlignmentMask
;
1035 EFI_PHYSICAL_ADDRESS MaxAddress
;
1038 LIST_ENTRY
*SubLink
;
1039 EFI_GCD_MAP_ENTRY
*Entry
;
1040 EFI_GCD_MAP_ENTRY
*TopEntry
;
1041 EFI_GCD_MAP_ENTRY
*BottomEntry
;
1042 LIST_ENTRY
*StartLink
;
1043 LIST_ENTRY
*EndLink
;
1047 // Make sure parameters are valid
1049 if ((UINT32
)GcdAllocateType
>= EfiGcdMaxAllocateType
) {
1050 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1051 return EFI_INVALID_PARAMETER
;
1053 if ((UINT32
)GcdMemoryType
>= EfiGcdMemoryTypeMaximum
) {
1054 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1055 return EFI_INVALID_PARAMETER
;
1057 if ((UINT32
)GcdIoType
>= EfiGcdIoTypeMaximum
) {
1058 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1059 return EFI_INVALID_PARAMETER
;
1061 if (BaseAddress
== NULL
) {
1062 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1063 return EFI_INVALID_PARAMETER
;
1065 if (ImageHandle
== NULL
) {
1066 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1067 return EFI_INVALID_PARAMETER
;
1069 if (Alignment
>= 64) {
1070 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_NOT_FOUND
));
1071 return EFI_NOT_FOUND
;
1074 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1075 return EFI_INVALID_PARAMETER
;
1079 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
1080 CoreAcquireGcdMemoryLock ();
1081 Map
= &mGcdMemorySpaceMap
;
1082 } else if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
1083 CoreAcquireGcdIoLock ();
1084 Map
= &mGcdIoSpaceMap
;
1093 // Compute alignment bit mask
1095 AlignmentMask
= LShiftU64 (1, Alignment
) - 1;
1097 if (GcdAllocateType
== EfiGcdAllocateAddress
) {
1099 // Verify that the BaseAddress passed in is aligned correctly
1101 if ((*BaseAddress
& AlignmentMask
) != 0) {
1102 Status
= EFI_NOT_FOUND
;
1107 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
1109 Status
= CoreSearchGcdMapEntry (*BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
1110 if (EFI_ERROR (Status
)) {
1111 Status
= EFI_NOT_FOUND
;
1114 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1117 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1120 while (Link
!= EndLink
->ForwardLink
) {
1121 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1122 Link
= Link
->ForwardLink
;
1123 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1124 if (EFI_ERROR (Status
)) {
1131 Entry
= CR (Map
->BackLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1134 // Compute the maximum address to use in the search algorithm
1136 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchBottomUp
||
1137 GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
) {
1138 MaxAddress
= *BaseAddress
;
1140 MaxAddress
= Entry
->EndAddress
;
1144 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1146 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
1147 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
1148 Link
= Map
->BackLink
;
1150 Link
= Map
->ForwardLink
;
1152 while (Link
!= Map
) {
1153 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1155 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
1156 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
1157 Link
= Link
->BackLink
;
1159 Link
= Link
->ForwardLink
;
1162 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1163 if (EFI_ERROR (Status
)) {
1167 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
1168 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
1169 if ((Entry
->BaseAddress
+ Length
) > MaxAddress
) {
1172 if (Length
> (Entry
->EndAddress
+ 1)) {
1173 Status
= EFI_NOT_FOUND
;
1176 if (Entry
->EndAddress
> MaxAddress
) {
1177 *BaseAddress
= MaxAddress
;
1179 *BaseAddress
= Entry
->EndAddress
;
1181 *BaseAddress
= (*BaseAddress
+ 1 - Length
) & (~AlignmentMask
);
1183 *BaseAddress
= (Entry
->BaseAddress
+ AlignmentMask
) & (~AlignmentMask
);
1184 if ((*BaseAddress
+ Length
- 1) > MaxAddress
) {
1185 Status
= EFI_NOT_FOUND
;
1191 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
1193 Status
= CoreSearchGcdMapEntry (*BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
1194 if (EFI_ERROR (Status
)) {
1195 Status
= EFI_NOT_FOUND
;
1198 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1202 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1205 SubLink
= StartLink
;
1206 while (SubLink
!= EndLink
->ForwardLink
) {
1207 Entry
= CR (SubLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1208 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1209 if (EFI_ERROR (Status
)) {
1214 SubLink
= SubLink
->ForwardLink
;
1222 Status
= EFI_NOT_FOUND
;
1227 // Allocate work space to perform this operation
1229 Status
= CoreAllocateGcdMapEntry (&TopEntry
, &BottomEntry
);
1230 if (EFI_ERROR (Status
)) {
1231 Status
= EFI_OUT_OF_RESOURCES
;
1234 ASSERT (TopEntry
!= NULL
&& BottomEntry
!= NULL
);
1237 // Convert/Insert the list of descriptors from StartLink to EndLink
1240 while (Link
!= EndLink
->ForwardLink
) {
1241 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1242 CoreInsertGcdMapEntry (Link
, Entry
, *BaseAddress
, Length
, TopEntry
, BottomEntry
);
1243 Entry
->ImageHandle
= ImageHandle
;
1244 Entry
->DeviceHandle
= DeviceHandle
;
1245 Link
= Link
->ForwardLink
;
1251 Status
= CoreCleanupGcdMapEntry (TopEntry
, BottomEntry
, StartLink
, EndLink
, Map
);
1254 DEBUG ((DEBUG_GCD
, " Status = %r", Status
));
1255 if (!EFI_ERROR (Status
)) {
1256 DEBUG ((DEBUG_GCD
, " (BaseAddress = %016lx)", *BaseAddress
));
1258 DEBUG ((DEBUG_GCD
, "\n"));
1260 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
1261 CoreReleaseGcdMemoryLock ();
1262 CoreDumpGcdMemorySpaceMap (FALSE
);
1264 if ((Operation
& GCD_IO_SPACE_OPERATION
) !=0) {
1265 CoreReleaseGcdIoLock ();
1266 CoreDumpGcdIoSpaceMap (FALSE
);
1274 Add a segment of memory to GCD map.
1276 @param GcdMemoryType Memory type of the segment.
1277 @param BaseAddress Base address of the segment.
1278 @param Length Length of the segment.
1279 @param Capabilities alterable attributes of the segment.
1281 @retval EFI_INVALID_PARAMETER Invalid parameters.
1282 @retval EFI_SUCCESS Successfully add a segment of memory space.
1286 CoreInternalAddMemorySpace (
1287 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1288 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1290 IN UINT64 Capabilities
1293 DEBUG ((DEBUG_GCD
, "GCD:AddMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1294 DEBUG ((DEBUG_GCD
, " GcdMemoryType = %a\n", mGcdMemoryTypeNames
[MIN (GcdMemoryType
, EfiGcdMemoryTypeMaximum
)]));
1295 DEBUG ((DEBUG_GCD
, " Capabilities = %016lx\n", Capabilities
));
1298 // Make sure parameters are valid
1300 if (GcdMemoryType
<= EfiGcdMemoryTypeNonExistent
|| GcdMemoryType
>= EfiGcdMemoryTypeMaximum
) {
1301 return EFI_INVALID_PARAMETER
;
1304 return CoreConvertSpace (GCD_ADD_MEMORY_OPERATION
, GcdMemoryType
, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, Capabilities
, 0);
1308 // GCD Core Services
1312 Allocates nonexistent memory, reserved memory, system memory, or memorymapped
1313 I/O resources from the global coherency domain of the processor.
1315 @param GcdAllocateType The type of allocate operation
1316 @param GcdMemoryType The desired memory type
1317 @param Alignment Align with 2^Alignment
1318 @param Length Length to allocate
1319 @param BaseAddress Base address to allocate
1320 @param ImageHandle The image handle consume the allocated space.
1321 @param DeviceHandle The device handle consume the allocated space.
1323 @retval EFI_INVALID_PARAMETER Invalid parameter.
1324 @retval EFI_NOT_FOUND No descriptor contains the desired space.
1325 @retval EFI_SUCCESS Memory space successfully allocated.
1330 CoreAllocateMemorySpace (
1331 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1332 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1335 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1336 IN EFI_HANDLE ImageHandle
,
1337 IN EFI_HANDLE DeviceHandle OPTIONAL
1340 if (BaseAddress
!= NULL
) {
1341 DEBUG ((DEBUG_GCD
, "GCD:AllocateMemorySpace(Base=%016lx,Length=%016lx)\n", *BaseAddress
, Length
));
1343 DEBUG ((DEBUG_GCD
, "GCD:AllocateMemorySpace(Base=<NULL>,Length=%016lx)\n", Length
));
1345 DEBUG ((DEBUG_GCD
, " GcdAllocateType = %a\n", mGcdAllocationTypeNames
[MIN (GcdAllocateType
, EfiGcdMaxAllocateType
)]));
1346 DEBUG ((DEBUG_GCD
, " GcdMemoryType = %a\n", mGcdMemoryTypeNames
[MIN (GcdMemoryType
, EfiGcdMemoryTypeMaximum
)]));
1347 DEBUG ((DEBUG_GCD
, " Alignment = %016lx\n", LShiftU64 (1, Alignment
)));
1348 DEBUG ((DEBUG_GCD
, " ImageHandle = %p\n", ImageHandle
));
1349 DEBUG ((DEBUG_GCD
, " DeviceHandle = %p\n", DeviceHandle
));
1351 return CoreAllocateSpace (
1352 GCD_ALLOCATE_MEMORY_OPERATION
,
1355 (EFI_GCD_IO_TYPE
) 0,
1366 Adds reserved memory, system memory, or memory-mapped I/O resources to the
1367 global coherency domain of the processor.
1369 @param GcdMemoryType Memory type of the memory space.
1370 @param BaseAddress Base address of the memory space.
1371 @param Length Length of the memory space.
1372 @param Capabilities alterable attributes of the memory space.
1374 @retval EFI_SUCCESS Merged this memory space into GCD map.
1379 CoreAddMemorySpace (
1380 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1381 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1383 IN UINT64 Capabilities
1387 EFI_PHYSICAL_ADDRESS PageBaseAddress
;
1390 Status
= CoreInternalAddMemorySpace (GcdMemoryType
, BaseAddress
, Length
, Capabilities
);
1392 if (!EFI_ERROR (Status
) && ((GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) || (GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
))) {
1394 PageBaseAddress
= PageAlignAddress (BaseAddress
);
1395 PageLength
= PageAlignLength (BaseAddress
+ Length
- PageBaseAddress
);
1397 Status
= CoreAllocateMemorySpace (
1398 EfiGcdAllocateAddress
,
1403 gDxeCoreImageHandle
,
1407 if (!EFI_ERROR (Status
)) {
1408 CoreAddMemoryDescriptor (
1409 EfiConventionalMemory
,
1411 RShiftU64 (PageLength
, EFI_PAGE_SHIFT
),
1415 for (; PageLength
!= 0; PageLength
-= EFI_PAGE_SIZE
, PageBaseAddress
+= EFI_PAGE_SIZE
) {
1416 Status
= CoreAllocateMemorySpace (
1417 EfiGcdAllocateAddress
,
1422 gDxeCoreImageHandle
,
1426 if (!EFI_ERROR (Status
)) {
1427 CoreAddMemoryDescriptor (
1428 EfiConventionalMemory
,
1442 Frees nonexistent memory, reserved memory, system memory, or memory-mapped
1443 I/O resources from the global coherency domain of the processor.
1445 @param BaseAddress Base address of the memory space.
1446 @param Length Length of the memory space.
1448 @retval EFI_SUCCESS Space successfully freed.
1453 CoreFreeMemorySpace (
1454 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1458 DEBUG ((DEBUG_GCD
, "GCD:FreeMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1460 return CoreConvertSpace (GCD_FREE_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1465 Removes reserved memory, system memory, or memory-mapped I/O resources from
1466 the global coherency domain of the processor.
1468 @param BaseAddress Base address of the memory space.
1469 @param Length Length of the memory space.
1471 @retval EFI_SUCCESS Successfully remove a segment of memory space.
1476 CoreRemoveMemorySpace (
1477 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1481 DEBUG ((DEBUG_GCD
, "GCD:RemoveMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1483 return CoreConvertSpace (GCD_REMOVE_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1488 Build a memory descriptor according to an entry.
1490 @param Descriptor The descriptor to be built
1491 @param Entry According to this entry
1495 BuildMemoryDescriptor (
1496 IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
,
1497 IN EFI_GCD_MAP_ENTRY
*Entry
1500 Descriptor
->BaseAddress
= Entry
->BaseAddress
;
1501 Descriptor
->Length
= Entry
->EndAddress
- Entry
->BaseAddress
+ 1;
1502 Descriptor
->Capabilities
= Entry
->Capabilities
;
1503 Descriptor
->Attributes
= Entry
->Attributes
;
1504 Descriptor
->GcdMemoryType
= Entry
->GcdMemoryType
;
1505 Descriptor
->ImageHandle
= Entry
->ImageHandle
;
1506 Descriptor
->DeviceHandle
= Entry
->DeviceHandle
;
1511 Retrieves the descriptor for a memory region containing a specified address.
1513 @param BaseAddress Specified start address
1514 @param Descriptor Specified length
1516 @retval EFI_INVALID_PARAMETER Invalid parameter
1517 @retval EFI_SUCCESS Successfully get memory space descriptor.
1522 CoreGetMemorySpaceDescriptor (
1523 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1524 OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
1528 LIST_ENTRY
*StartLink
;
1529 LIST_ENTRY
*EndLink
;
1530 EFI_GCD_MAP_ENTRY
*Entry
;
1533 // Make sure parameters are valid
1535 if (Descriptor
== NULL
) {
1536 return EFI_INVALID_PARAMETER
;
1539 CoreAcquireGcdMemoryLock ();
1542 // Search for the list of descriptors that contain BaseAddress
1544 Status
= CoreSearchGcdMapEntry (BaseAddress
, 1, &StartLink
, &EndLink
, &mGcdMemorySpaceMap
);
1545 if (EFI_ERROR (Status
)) {
1546 Status
= EFI_NOT_FOUND
;
1548 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1550 // Copy the contents of the found descriptor into Descriptor
1552 Entry
= CR (StartLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1553 BuildMemoryDescriptor (Descriptor
, Entry
);
1556 CoreReleaseGcdMemoryLock ();
1563 Modifies the attributes for a memory region in the global coherency domain of the
1566 @param BaseAddress Specified start address
1567 @param Length Specified length
1568 @param Attributes Specified attributes
1570 @retval EFI_SUCCESS The attributes were set for the memory region.
1571 @retval EFI_INVALID_PARAMETER Length is zero.
1572 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
1573 resource range specified by BaseAddress and Length.
1574 @retval EFI_UNSUPPORTED The bit mask of attributes is not support for the memory resource
1575 range specified by BaseAddress and Length.
1576 @retval EFI_ACCESS_DEFINED The attributes for the memory resource range specified by
1577 BaseAddress and Length cannot be modified.
1578 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
1579 the memory resource range.
1580 @retval EFI_NOT_AVAILABLE_YET The attributes cannot be set because CPU architectural protocol is
1586 CoreSetMemorySpaceAttributes (
1587 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1589 IN UINT64 Attributes
1592 DEBUG ((DEBUG_GCD
, "GCD:SetMemorySpaceAttributes(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1593 DEBUG ((DEBUG_GCD
, " Attributes = %016lx\n", Attributes
));
1595 return CoreConvertSpace (GCD_SET_ATTRIBUTES_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, Attributes
);
1600 Modifies the capabilities for a memory region in the global coherency domain of the
1603 @param BaseAddress The physical address that is the start address of a memory region.
1604 @param Length The size in bytes of the memory region.
1605 @param Capabilities The bit mask of capabilities that the memory region supports.
1607 @retval EFI_SUCCESS The capabilities were set for the memory region.
1608 @retval EFI_INVALID_PARAMETER Length is zero.
1609 @retval EFI_UNSUPPORTED The capabilities specified by Capabilities do not include the
1610 memory region attributes currently in use.
1611 @retval EFI_ACCESS_DENIED The capabilities for the memory resource range specified by
1612 BaseAddress and Length cannot be modified.
1613 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the capabilities
1614 of the memory resource range.
1618 CoreSetMemorySpaceCapabilities (
1619 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1621 IN UINT64 Capabilities
1626 DEBUG ((DEBUG_GCD
, "GCD:CoreSetMemorySpaceCapabilities(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1627 DEBUG ((DEBUG_GCD
, " Capabilities = %016lx\n", Capabilities
));
1629 Status
= CoreConvertSpace (GCD_SET_CAPABILITIES_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, Capabilities
, 0);
1630 if (!EFI_ERROR(Status
)) {
1631 CoreUpdateMemoryAttributes(BaseAddress
, RShiftU64(Length
, EFI_PAGE_SHIFT
), Capabilities
& (~EFI_MEMORY_RUNTIME
));
1639 Returns a map of the memory resources in the global coherency domain of the
1642 @param NumberOfDescriptors Number of descriptors.
1643 @param MemorySpaceMap Descriptor array
1645 @retval EFI_INVALID_PARAMETER Invalid parameter
1646 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1647 @retval EFI_SUCCESS Successfully get memory space map.
1652 CoreGetMemorySpaceMap (
1653 OUT UINTN
*NumberOfDescriptors
,
1654 OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
**MemorySpaceMap
1659 EFI_GCD_MAP_ENTRY
*Entry
;
1660 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
;
1663 // Make sure parameters are valid
1665 if (NumberOfDescriptors
== NULL
) {
1666 return EFI_INVALID_PARAMETER
;
1668 if (MemorySpaceMap
== NULL
) {
1669 return EFI_INVALID_PARAMETER
;
1672 CoreAcquireGcdMemoryLock ();
1675 // Count the number of descriptors
1677 *NumberOfDescriptors
= CoreCountGcdMapEntry (&mGcdMemorySpaceMap
);
1680 // Allocate the MemorySpaceMap
1682 *MemorySpaceMap
= AllocatePool (*NumberOfDescriptors
* sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR
));
1683 if (*MemorySpaceMap
== NULL
) {
1684 Status
= EFI_OUT_OF_RESOURCES
;
1689 // Fill in the MemorySpaceMap
1691 Descriptor
= *MemorySpaceMap
;
1692 Link
= mGcdMemorySpaceMap
.ForwardLink
;
1693 while (Link
!= &mGcdMemorySpaceMap
) {
1694 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1695 BuildMemoryDescriptor (Descriptor
, Entry
);
1697 Link
= Link
->ForwardLink
;
1699 Status
= EFI_SUCCESS
;
1702 CoreReleaseGcdMemoryLock ();
1708 Adds reserved I/O or I/O resources to the global coherency domain of the processor.
1710 @param GcdIoType IO type of the segment.
1711 @param BaseAddress Base address of the segment.
1712 @param Length Length of the segment.
1714 @retval EFI_SUCCESS Merged this segment into GCD map.
1715 @retval EFI_INVALID_PARAMETER Parameter not valid
1721 IN EFI_GCD_IO_TYPE GcdIoType
,
1722 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1726 DEBUG ((DEBUG_GCD
, "GCD:AddIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1727 DEBUG ((DEBUG_GCD
, " GcdIoType = %a\n", mGcdIoTypeNames
[MIN (GcdIoType
, EfiGcdIoTypeMaximum
)]));
1730 // Make sure parameters are valid
1732 if (GcdIoType
<= EfiGcdIoTypeNonExistent
|| GcdIoType
>= EfiGcdIoTypeMaximum
) {
1733 return EFI_INVALID_PARAMETER
;
1735 return CoreConvertSpace (GCD_ADD_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, GcdIoType
, BaseAddress
, Length
, 0, 0);
1740 Allocates nonexistent I/O, reserved I/O, or I/O resources from the global coherency
1741 domain of the processor.
1743 @param GcdAllocateType The type of allocate operation
1744 @param GcdIoType The desired IO type
1745 @param Alignment Align with 2^Alignment
1746 @param Length Length to allocate
1747 @param BaseAddress Base address to allocate
1748 @param ImageHandle The image handle consume the allocated space.
1749 @param DeviceHandle The device handle consume the allocated space.
1751 @retval EFI_INVALID_PARAMETER Invalid parameter.
1752 @retval EFI_NOT_FOUND No descriptor contains the desired space.
1753 @retval EFI_SUCCESS IO space successfully allocated.
1758 CoreAllocateIoSpace (
1759 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1760 IN EFI_GCD_IO_TYPE GcdIoType
,
1763 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1764 IN EFI_HANDLE ImageHandle
,
1765 IN EFI_HANDLE DeviceHandle OPTIONAL
1768 if (BaseAddress
!= NULL
) {
1769 DEBUG ((DEBUG_GCD
, "GCD:AllocateIoSpace(Base=%016lx,Length=%016lx)\n", *BaseAddress
, Length
));
1771 DEBUG ((DEBUG_GCD
, "GCD:AllocateIoSpace(Base=<NULL>,Length=%016lx)\n", Length
));
1773 DEBUG ((DEBUG_GCD
, " GcdAllocateType = %a\n", mGcdAllocationTypeNames
[MIN (GcdAllocateType
, EfiGcdMaxAllocateType
)]));
1774 DEBUG ((DEBUG_GCD
, " GcdIoType = %a\n", mGcdIoTypeNames
[MIN (GcdIoType
, EfiGcdIoTypeMaximum
)]));
1775 DEBUG ((DEBUG_GCD
, " Alignment = %016lx\n", LShiftU64 (1, Alignment
)));
1776 DEBUG ((DEBUG_GCD
, " ImageHandle = %p\n", ImageHandle
));
1777 DEBUG ((DEBUG_GCD
, " DeviceHandle = %p\n", DeviceHandle
));
1779 return CoreAllocateSpace (
1780 GCD_ALLOCATE_IO_OPERATION
,
1782 (EFI_GCD_MEMORY_TYPE
) 0,
1794 Frees nonexistent I/O, reserved I/O, or I/O resources from the global coherency
1795 domain of the processor.
1797 @param BaseAddress Base address of the segment.
1798 @param Length Length of the segment.
1800 @retval EFI_SUCCESS Space successfully freed.
1806 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1810 DEBUG ((DEBUG_GCD
, "GCD:FreeIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1812 return CoreConvertSpace (GCD_FREE_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1817 Removes reserved I/O or I/O resources from the global coherency domain of the
1820 @param BaseAddress Base address of the segment.
1821 @param Length Length of the segment.
1823 @retval EFI_SUCCESS Successfully removed a segment of IO space.
1829 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1833 DEBUG ((DEBUG_GCD
, "GCD:RemoveIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1835 return CoreConvertSpace (GCD_REMOVE_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1840 Build a IO descriptor according to an entry.
1842 @param Descriptor The descriptor to be built
1843 @param Entry According to this entry
1848 IN EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
,
1849 IN EFI_GCD_MAP_ENTRY
*Entry
1852 Descriptor
->BaseAddress
= Entry
->BaseAddress
;
1853 Descriptor
->Length
= Entry
->EndAddress
- Entry
->BaseAddress
+ 1;
1854 Descriptor
->GcdIoType
= Entry
->GcdIoType
;
1855 Descriptor
->ImageHandle
= Entry
->ImageHandle
;
1856 Descriptor
->DeviceHandle
= Entry
->DeviceHandle
;
1861 Retrieves the descriptor for an I/O region containing a specified address.
1863 @param BaseAddress Specified start address
1864 @param Descriptor Specified length
1866 @retval EFI_INVALID_PARAMETER Descriptor is NULL.
1867 @retval EFI_SUCCESS Successfully get the IO space descriptor.
1872 CoreGetIoSpaceDescriptor (
1873 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1874 OUT EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
1878 LIST_ENTRY
*StartLink
;
1879 LIST_ENTRY
*EndLink
;
1880 EFI_GCD_MAP_ENTRY
*Entry
;
1883 // Make sure parameters are valid
1885 if (Descriptor
== NULL
) {
1886 return EFI_INVALID_PARAMETER
;
1889 CoreAcquireGcdIoLock ();
1892 // Search for the list of descriptors that contain BaseAddress
1894 Status
= CoreSearchGcdMapEntry (BaseAddress
, 1, &StartLink
, &EndLink
, &mGcdIoSpaceMap
);
1895 if (EFI_ERROR (Status
)) {
1896 Status
= EFI_NOT_FOUND
;
1898 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1900 // Copy the contents of the found descriptor into Descriptor
1902 Entry
= CR (StartLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1903 BuildIoDescriptor (Descriptor
, Entry
);
1906 CoreReleaseGcdIoLock ();
1913 Returns a map of the I/O resources in the global coherency domain of the processor.
1915 @param NumberOfDescriptors Number of descriptors.
1916 @param IoSpaceMap Descriptor array
1918 @retval EFI_INVALID_PARAMETER Invalid parameter
1919 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1920 @retval EFI_SUCCESS Successfully get IO space map.
1926 OUT UINTN
*NumberOfDescriptors
,
1927 OUT EFI_GCD_IO_SPACE_DESCRIPTOR
**IoSpaceMap
1932 EFI_GCD_MAP_ENTRY
*Entry
;
1933 EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
;
1936 // Make sure parameters are valid
1938 if (NumberOfDescriptors
== NULL
) {
1939 return EFI_INVALID_PARAMETER
;
1941 if (IoSpaceMap
== NULL
) {
1942 return EFI_INVALID_PARAMETER
;
1945 CoreAcquireGcdIoLock ();
1948 // Count the number of descriptors
1950 *NumberOfDescriptors
= CoreCountGcdMapEntry (&mGcdIoSpaceMap
);
1953 // Allocate the IoSpaceMap
1955 *IoSpaceMap
= AllocatePool (*NumberOfDescriptors
* sizeof (EFI_GCD_IO_SPACE_DESCRIPTOR
));
1956 if (*IoSpaceMap
== NULL
) {
1957 Status
= EFI_OUT_OF_RESOURCES
;
1962 // Fill in the IoSpaceMap
1964 Descriptor
= *IoSpaceMap
;
1965 Link
= mGcdIoSpaceMap
.ForwardLink
;
1966 while (Link
!= &mGcdIoSpaceMap
) {
1967 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1968 BuildIoDescriptor (Descriptor
, Entry
);
1970 Link
= Link
->ForwardLink
;
1972 Status
= EFI_SUCCESS
;
1975 CoreReleaseGcdIoLock ();
1981 Converts a Resource Descriptor HOB attributes mask to an EFI Memory Descriptor
1984 @param GcdMemoryType Type of resource in the GCD memory map.
1985 @param Attributes The attribute mask in the Resource Descriptor
1988 @return The capabilities mask for an EFI Memory Descriptor.
1992 CoreConvertResourceDescriptorHobAttributesToCapabilities (
1993 EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1997 UINT64 Capabilities
;
1998 GCD_ATTRIBUTE_CONVERSION_ENTRY
*Conversion
;
2001 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2003 for (Capabilities
= 0, Conversion
= mAttributeConversionTable
; Conversion
->Attribute
!= 0; Conversion
++) {
2004 if (Conversion
->Memory
|| ((GcdMemoryType
!= EfiGcdMemoryTypeSystemMemory
) && (GcdMemoryType
!= EfiGcdMemoryTypeMoreReliable
))) {
2005 if (Attributes
& Conversion
->Attribute
) {
2006 Capabilities
|= Conversion
->Capability
;
2011 return Capabilities
;
2015 Calculate total memory bin size neeeded.
2017 @return The total memory bin size neeeded.
2021 CalculateTotalMemoryBinSizeNeeded (
2029 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
2032 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
2033 TotalSize
+= LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
);
2040 External function. Initializes memory services based on the memory
2041 descriptor HOBs. This function is responsible for priming the memory
2042 map, so memory allocations and resource allocations can be made.
2043 The first part of this function can not depend on any memory services
2044 until at least one memory descriptor is provided to the memory services.
2046 @param HobStart The start address of the HOB.
2047 @param MemoryBaseAddress Start address of memory region found to init DXE
2049 @param MemoryLength Length of memory region found to init DXE core.
2051 @retval EFI_SUCCESS Memory services successfully initialized.
2055 CoreInitializeMemoryServices (
2057 OUT EFI_PHYSICAL_ADDRESS
*MemoryBaseAddress
,
2058 OUT UINT64
*MemoryLength
2061 EFI_PEI_HOB_POINTERS Hob
;
2062 EFI_MEMORY_TYPE_INFORMATION
*EfiMemoryTypeInformation
;
2065 EFI_HOB_HANDOFF_INFO_TABLE
*PhitHob
;
2066 EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
;
2067 EFI_HOB_RESOURCE_DESCRIPTOR
*PhitResourceHob
;
2068 EFI_PHYSICAL_ADDRESS BaseAddress
;
2071 UINT64 Capabilities
;
2072 EFI_PHYSICAL_ADDRESS TestedMemoryBaseAddress
;
2073 UINT64 TestedMemoryLength
;
2074 EFI_PHYSICAL_ADDRESS HighAddress
;
2075 EFI_HOB_GUID_TYPE
*GuidHob
;
2076 UINT32 ReservedCodePageNumber
;
2077 UINT64 MinimalMemorySizeNeeded
;
2080 // Point at the first HOB. This must be the PHIT HOB.
2082 Hob
.Raw
= *HobStart
;
2083 ASSERT (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_HANDOFF
);
2086 // Initialize the spin locks and maps in the memory services.
2087 // Also fill in the memory services into the EFI Boot Services Table
2089 CoreInitializePool ();
2092 // Initialize Local Variables
2094 PhitResourceHob
= NULL
;
2101 // Cache the PHIT HOB for later use
2103 PhitHob
= Hob
.HandoffInformationTable
;
2105 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
2106 ReservedCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
2107 ReservedCodePageNumber
+= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
2110 // cache the Top address for loading modules at Fixed Address
2112 gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
= PhitHob
->EfiMemoryTop
2113 + EFI_PAGES_TO_SIZE(ReservedCodePageNumber
);
2116 // See if a Memory Type Information HOB is available
2118 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
2119 if (GuidHob
!= NULL
) {
2120 EfiMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
2121 DataSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
2122 if (EfiMemoryTypeInformation
!= NULL
&& DataSize
> 0 && DataSize
<= (EfiMaxMemoryType
+ 1) * sizeof (EFI_MEMORY_TYPE_INFORMATION
)) {
2123 CopyMem (&gMemoryTypeInformation
, EfiMemoryTypeInformation
, DataSize
);
2128 // Include the total memory bin size needed to make sure memory bin could be allocated successfully.
2130 MinimalMemorySizeNeeded
= MINIMUM_INITIAL_MEMORY_SIZE
+ CalculateTotalMemoryBinSizeNeeded ();
2133 // Find the Resource Descriptor HOB that contains PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop
2136 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2138 // Skip all HOBs except Resource Descriptor HOBs
2140 if (GET_HOB_TYPE (Hob
) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2145 // Skip Resource Descriptor HOBs that do not describe tested system memory
2147 ResourceHob
= Hob
.ResourceDescriptor
;
2148 if (ResourceHob
->ResourceType
!= EFI_RESOURCE_SYSTEM_MEMORY
) {
2151 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) != TESTED_MEMORY_ATTRIBUTES
) {
2156 // Skip Resource Descriptor HOBs that do not contain the PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop
2158 if (PhitHob
->EfiFreeMemoryBottom
< ResourceHob
->PhysicalStart
) {
2161 if (PhitHob
->EfiFreeMemoryTop
> (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
)) {
2166 // Cache the resource descriptor HOB for the memory region described by the PHIT HOB
2168 PhitResourceHob
= ResourceHob
;
2172 // Compute range between PHIT EfiMemoryTop and the end of the Resource Descriptor HOB
2174 Attributes
= PhitResourceHob
->ResourceAttribute
;
2175 BaseAddress
= PageAlignAddress (PhitHob
->EfiMemoryTop
);
2176 Length
= PageAlignLength (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
- BaseAddress
);
2177 if (Length
< MinimalMemorySizeNeeded
) {
2179 // If that range is not large enough to intialize the DXE Core, then
2180 // Compute range between PHIT EfiFreeMemoryBottom and PHIT EfiFreeMemoryTop
2182 BaseAddress
= PageAlignAddress (PhitHob
->EfiFreeMemoryBottom
);
2183 Length
= PageAlignLength (PhitHob
->EfiFreeMemoryTop
- BaseAddress
);
2184 if (Length
< MinimalMemorySizeNeeded
) {
2186 // If that range is not large enough to intialize the DXE Core, then
2187 // Compute range between the start of the Resource Descriptor HOB and the start of the HOB List
2189 BaseAddress
= PageAlignAddress (ResourceHob
->PhysicalStart
);
2190 Length
= PageAlignLength ((UINT64
)((UINTN
)*HobStart
- BaseAddress
));
2197 // Assert if a resource descriptor HOB for the memory region described by the PHIT was not found
2202 // Take the range in the resource descriptor HOB for the memory region described
2203 // by the PHIT as higher priority if it is big enough. It can make the memory bin
2204 // allocated to be at the same memory region with PHIT that has more better compatibility
2205 // to avoid memory fragmentation for some code practices assume and allocate <4G ACPI memory.
2207 if (Length
< MinimalMemorySizeNeeded
) {
2209 // Search all the resource descriptor HOBs from the highest possible addresses down for a memory
2210 // region that is big enough to initialize the DXE core. Always skip the PHIT Resource HOB.
2211 // The max address must be within the physically addressible range for the processor.
2213 HighAddress
= MAX_ADDRESS
;
2214 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2216 // Skip the Resource Descriptor HOB that contains the PHIT
2218 if (Hob
.ResourceDescriptor
== PhitResourceHob
) {
2222 // Skip all HOBs except Resource Descriptor HOBs
2224 if (GET_HOB_TYPE (Hob
) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2229 // Skip Resource Descriptor HOBs that do not describe tested system memory below MAX_ADDRESS
2231 ResourceHob
= Hob
.ResourceDescriptor
;
2232 if (ResourceHob
->ResourceType
!= EFI_RESOURCE_SYSTEM_MEMORY
) {
2235 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) != TESTED_MEMORY_ATTRIBUTES
) {
2238 if ((ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
) > (EFI_PHYSICAL_ADDRESS
)MAX_ADDRESS
) {
2243 // Skip Resource Descriptor HOBs that are below a previously found Resource Descriptor HOB
2245 if (HighAddress
!= (EFI_PHYSICAL_ADDRESS
)MAX_ADDRESS
&& ResourceHob
->PhysicalStart
<= HighAddress
) {
2250 // Skip Resource Descriptor HOBs that are not large enough to initilize the DXE Core
2252 TestedMemoryBaseAddress
= PageAlignAddress (ResourceHob
->PhysicalStart
);
2253 TestedMemoryLength
= PageAlignLength (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
- TestedMemoryBaseAddress
);
2254 if (TestedMemoryLength
< MinimalMemorySizeNeeded
) {
2259 // Save the range described by the Resource Descriptor that is large enough to initilize the DXE Core
2261 BaseAddress
= TestedMemoryBaseAddress
;
2262 Length
= TestedMemoryLength
;
2263 Attributes
= ResourceHob
->ResourceAttribute
;
2264 HighAddress
= ResourceHob
->PhysicalStart
;
2268 DEBUG ((EFI_D_INFO
, "CoreInitializeMemoryServices:\n"));
2269 DEBUG ((EFI_D_INFO
, " BaseAddress - 0x%lx Length - 0x%lx MinimalMemorySizeNeeded - 0x%lx\n", BaseAddress
, Length
, MinimalMemorySizeNeeded
));
2272 // If no memory regions are found that are big enough to initialize the DXE core, then ASSERT().
2274 ASSERT (Length
>= MinimalMemorySizeNeeded
);
2277 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2279 if ((Attributes
& EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) == EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) {
2280 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeMoreReliable
, Attributes
);
2282 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeSystemMemory
, Attributes
);
2286 // Declare the very first memory region, so the EFI Memory Services are available.
2288 CoreAddMemoryDescriptor (
2289 EfiConventionalMemory
,
2291 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2295 *MemoryBaseAddress
= BaseAddress
;
2296 *MemoryLength
= Length
;
2303 External function. Initializes the GCD and memory services based on the memory
2304 descriptor HOBs. This function is responsible for priming the GCD map and the
2305 memory map, so memory allocations and resource allocations can be made. The
2306 HobStart will be relocated to a pool buffer.
2308 @param HobStart The start address of the HOB
2309 @param MemoryBaseAddress Start address of memory region found to init DXE
2311 @param MemoryLength Length of memory region found to init DXE core.
2313 @retval EFI_SUCCESS GCD services successfully initialized.
2317 CoreInitializeGcdServices (
2318 IN OUT VOID
**HobStart
,
2319 IN EFI_PHYSICAL_ADDRESS MemoryBaseAddress
,
2320 IN UINT64 MemoryLength
2323 EFI_PEI_HOB_POINTERS Hob
;
2325 EFI_HOB_HANDOFF_INFO_TABLE
*PhitHob
;
2326 UINT8 SizeOfMemorySpace
;
2327 UINT8 SizeOfIoSpace
;
2328 EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
;
2329 EFI_PHYSICAL_ADDRESS BaseAddress
;
2332 EFI_GCD_MAP_ENTRY
*Entry
;
2333 EFI_GCD_MEMORY_TYPE GcdMemoryType
;
2334 EFI_GCD_IO_TYPE GcdIoType
;
2335 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
2336 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
2337 EFI_HOB_FIRMWARE_VOLUME
*FirmwareVolumeHob
;
2338 UINTN NumberOfDescriptors
;
2339 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
2341 UINT64 Capabilities
;
2342 EFI_HOB_CPU
* CpuHob
;
2343 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMapHobList
;
2346 // Cache the PHIT HOB for later use
2348 PhitHob
= (EFI_HOB_HANDOFF_INFO_TABLE
*)(*HobStart
);
2351 // Get the number of address lines in the I/O and Memory space for the CPU
2353 CpuHob
= GetFirstHob (EFI_HOB_TYPE_CPU
);
2354 ASSERT (CpuHob
!= NULL
);
2355 SizeOfMemorySpace
= CpuHob
->SizeOfMemorySpace
;
2356 SizeOfIoSpace
= CpuHob
->SizeOfIoSpace
;
2359 // Initialize the GCD Memory Space Map
2361 Entry
= AllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY
), &mGcdMemorySpaceMapEntryTemplate
);
2362 ASSERT (Entry
!= NULL
);
2364 Entry
->EndAddress
= LShiftU64 (1, SizeOfMemorySpace
) - 1;
2366 InsertHeadList (&mGcdMemorySpaceMap
, &Entry
->Link
);
2368 CoreDumpGcdMemorySpaceMap (TRUE
);
2371 // Initialize the GCD I/O Space Map
2373 Entry
= AllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY
), &mGcdIoSpaceMapEntryTemplate
);
2374 ASSERT (Entry
!= NULL
);
2376 Entry
->EndAddress
= LShiftU64 (1, SizeOfIoSpace
) - 1;
2378 InsertHeadList (&mGcdIoSpaceMap
, &Entry
->Link
);
2380 CoreDumpGcdIoSpaceMap (TRUE
);
2383 // Walk the HOB list and add all resource descriptors to the GCD
2385 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2387 GcdMemoryType
= EfiGcdMemoryTypeNonExistent
;
2388 GcdIoType
= EfiGcdIoTypeNonExistent
;
2390 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2392 ResourceHob
= Hob
.ResourceDescriptor
;
2394 switch (ResourceHob
->ResourceType
) {
2395 case EFI_RESOURCE_SYSTEM_MEMORY
:
2396 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == TESTED_MEMORY_ATTRIBUTES
) {
2397 if ((ResourceHob
->ResourceAttribute
& EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) == EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) {
2398 GcdMemoryType
= EfiGcdMemoryTypeMoreReliable
;
2400 GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
2403 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == INITIALIZED_MEMORY_ATTRIBUTES
) {
2404 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2406 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == PRESENT_MEMORY_ATTRIBUTES
) {
2407 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2409 if ((ResourceHob
->ResourceAttribute
& EFI_RESOURCE_ATTRIBUTE_PERSISTENT
) == EFI_RESOURCE_ATTRIBUTE_PERSISTENT
) {
2410 GcdMemoryType
= EfiGcdMemoryTypePersistentMemory
;
2413 case EFI_RESOURCE_MEMORY_MAPPED_IO
:
2414 case EFI_RESOURCE_FIRMWARE_DEVICE
:
2415 GcdMemoryType
= EfiGcdMemoryTypeMemoryMappedIo
;
2417 case EFI_RESOURCE_MEMORY_MAPPED_IO_PORT
:
2418 case EFI_RESOURCE_MEMORY_RESERVED
:
2419 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2421 case EFI_RESOURCE_IO
:
2422 GcdIoType
= EfiGcdIoTypeIo
;
2424 case EFI_RESOURCE_IO_RESERVED
:
2425 GcdIoType
= EfiGcdIoTypeReserved
;
2429 if (GcdMemoryType
!= EfiGcdMemoryTypeNonExistent
) {
2431 // Validate the Resource HOB Attributes
2433 CoreValidateResourceDescriptorHobAttributes (ResourceHob
->ResourceAttribute
);
2436 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2438 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (
2440 ResourceHob
->ResourceAttribute
2443 Status
= CoreInternalAddMemorySpace (
2445 ResourceHob
->PhysicalStart
,
2446 ResourceHob
->ResourceLength
,
2451 if (GcdIoType
!= EfiGcdIoTypeNonExistent
) {
2452 Status
= CoreAddIoSpace (
2454 ResourceHob
->PhysicalStart
,
2455 ResourceHob
->ResourceLength
2462 // Allocate first memory region from the GCD by the DXE core
2464 Status
= CoreGetMemorySpaceDescriptor (MemoryBaseAddress
, &Descriptor
);
2465 if (!EFI_ERROR (Status
)) {
2466 ASSERT ((Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) ||
2467 (Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
));
2468 Status
= CoreAllocateMemorySpace (
2469 EfiGcdAllocateAddress
,
2470 Descriptor
.GcdMemoryType
,
2474 gDxeCoreImageHandle
,
2480 // Walk the HOB list and allocate all memory space that is consumed by memory allocation HOBs,
2481 // and Firmware Volume HOBs. Also update the EFI Memory Map with the memory allocation HOBs.
2483 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2484 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
2485 MemoryHob
= Hob
.MemoryAllocation
;
2486 BaseAddress
= MemoryHob
->AllocDescriptor
.MemoryBaseAddress
;
2487 Status
= CoreGetMemorySpaceDescriptor (BaseAddress
, &Descriptor
);
2488 if (!EFI_ERROR (Status
)) {
2489 Status
= CoreAllocateMemorySpace (
2490 EfiGcdAllocateAddress
,
2491 Descriptor
.GcdMemoryType
,
2493 MemoryHob
->AllocDescriptor
.MemoryLength
,
2495 gDxeCoreImageHandle
,
2498 if (!EFI_ERROR (Status
) &&
2499 ((Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) ||
2500 (Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
))) {
2501 CoreAddMemoryDescriptor (
2502 MemoryHob
->AllocDescriptor
.MemoryType
,
2503 MemoryHob
->AllocDescriptor
.MemoryBaseAddress
,
2504 RShiftU64 (MemoryHob
->AllocDescriptor
.MemoryLength
, EFI_PAGE_SHIFT
),
2505 Descriptor
.Capabilities
& (~EFI_MEMORY_RUNTIME
)
2511 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_FV
) {
2512 FirmwareVolumeHob
= Hob
.FirmwareVolume
;
2513 BaseAddress
= FirmwareVolumeHob
->BaseAddress
;
2514 Status
= CoreAllocateMemorySpace (
2515 EfiGcdAllocateAddress
,
2516 EfiGcdMemoryTypeMemoryMappedIo
,
2518 FirmwareVolumeHob
->Length
,
2520 gDxeCoreImageHandle
,
2527 // Add and allocate the remaining unallocated system memory to the memory services.
2529 Status
= CoreGetMemorySpaceMap (&NumberOfDescriptors
, &MemorySpaceMap
);
2530 ASSERT (Status
== EFI_SUCCESS
);
2532 MemorySpaceMapHobList
= NULL
;
2533 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
2534 if ((MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) ||
2535 (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
)) {
2536 if (MemorySpaceMap
[Index
].ImageHandle
== NULL
) {
2537 BaseAddress
= PageAlignAddress (MemorySpaceMap
[Index
].BaseAddress
);
2538 Length
= PageAlignLength (MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- BaseAddress
);
2539 if (Length
== 0 || MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
< BaseAddress
) {
2542 if (((UINTN
) MemorySpaceMap
[Index
].BaseAddress
<= (UINTN
) (*HobStart
)) &&
2543 ((UINTN
) (MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) >= (UINTN
) PhitHob
->EfiFreeMemoryBottom
)) {
2545 // Skip the memory space that covers HOB List, it should be processed
2546 // after HOB List relocation to avoid the resources allocated by others
2547 // to corrupt HOB List before its relocation.
2549 MemorySpaceMapHobList
= &MemorySpaceMap
[Index
];
2552 CoreAddMemoryDescriptor (
2553 EfiConventionalMemory
,
2555 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2556 MemorySpaceMap
[Index
].Capabilities
& (~EFI_MEMORY_RUNTIME
)
2558 Status
= CoreAllocateMemorySpace (
2559 EfiGcdAllocateAddress
,
2560 MemorySpaceMap
[Index
].GcdMemoryType
,
2564 gDxeCoreImageHandle
,
2572 // Relocate HOB List to an allocated pool buffer.
2573 // The relocation should be at after all the tested memory resources added
2574 // (except the memory space that covers HOB List) to the memory services,
2575 // because the memory resource found in CoreInitializeMemoryServices()
2576 // may have not enough remaining resource for HOB List.
2578 NewHobList
= AllocateCopyPool (
2579 (UINTN
) PhitHob
->EfiFreeMemoryBottom
- (UINTN
) (*HobStart
),
2582 ASSERT (NewHobList
!= NULL
);
2584 *HobStart
= NewHobList
;
2585 gHobList
= NewHobList
;
2587 if (MemorySpaceMapHobList
!= NULL
) {
2589 // Add and allocate the memory space that covers HOB List to the memory services
2590 // after HOB List relocation.
2592 BaseAddress
= PageAlignAddress (MemorySpaceMapHobList
->BaseAddress
);
2593 Length
= PageAlignLength (MemorySpaceMapHobList
->BaseAddress
+ MemorySpaceMapHobList
->Length
- BaseAddress
);
2594 CoreAddMemoryDescriptor (
2595 EfiConventionalMemory
,
2597 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2598 MemorySpaceMapHobList
->Capabilities
& (~EFI_MEMORY_RUNTIME
)
2600 Status
= CoreAllocateMemorySpace (
2601 EfiGcdAllocateAddress
,
2602 MemorySpaceMapHobList
->GcdMemoryType
,
2606 gDxeCoreImageHandle
,
2611 CoreFreePool (MemorySpaceMap
);