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 CpuArchAttributes
= Attributes
& NONEXCLUSIVE_MEMORY_ATTRIBUTES
;
678 if ( (Attributes
& EFI_MEMORY_UC
) == EFI_MEMORY_UC
) {
679 CpuArchAttributes
|= EFI_MEMORY_UC
;
680 } else if ( (Attributes
& EFI_MEMORY_WC
) == EFI_MEMORY_WC
) {
681 CpuArchAttributes
|= EFI_MEMORY_WC
;
682 } else if ( (Attributes
& EFI_MEMORY_WT
) == EFI_MEMORY_WT
) {
683 CpuArchAttributes
|= EFI_MEMORY_WT
;
684 } else if ( (Attributes
& EFI_MEMORY_WB
) == EFI_MEMORY_WB
) {
685 CpuArchAttributes
|= EFI_MEMORY_WB
;
686 } else if ( (Attributes
& EFI_MEMORY_UCE
) == EFI_MEMORY_UCE
) {
687 CpuArchAttributes
|= EFI_MEMORY_UCE
;
688 } else if ( (Attributes
& EFI_MEMORY_WP
) == EFI_MEMORY_WP
) {
689 CpuArchAttributes
|= EFI_MEMORY_WP
;
692 return CpuArchAttributes
;
697 Do operation on a segment of memory space specified (add, free, remove, change attribute ...).
699 @param Operation The type of the operation
700 @param GcdMemoryType Additional information for the operation
701 @param GcdIoType Additional information for the operation
702 @param BaseAddress Start address of the segment
703 @param Length length of the segment
704 @param Capabilities The alterable attributes of a newly added entry
705 @param Attributes The attributes needs to be set
707 @retval EFI_INVALID_PARAMETER Length is 0 or address (length) not aligned when
709 @retval EFI_SUCCESS Action successfully done.
710 @retval EFI_UNSUPPORTED Could not find the proper descriptor on this
711 segment or set an upsupported attribute.
712 @retval EFI_ACCESS_DENIED Operate on an space non-exist or is used for an
714 @retval EFI_NOT_FOUND Free a non-using space or remove a non-exist
716 @retval EFI_OUT_OF_RESOURCES No buffer could be allocated.
717 @retval EFI_NOT_AVAILABLE_YET The attributes cannot be set because CPU architectural protocol
718 is not available yet.
723 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
724 IN EFI_GCD_IO_TYPE GcdIoType
,
725 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
727 IN UINT64 Capabilities
,
734 EFI_GCD_MAP_ENTRY
*Entry
;
735 EFI_GCD_MAP_ENTRY
*TopEntry
;
736 EFI_GCD_MAP_ENTRY
*BottomEntry
;
737 LIST_ENTRY
*StartLink
;
739 UINT64 CpuArchAttributes
;
742 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
743 return EFI_INVALID_PARAMETER
;
747 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
748 CoreAcquireGcdMemoryLock ();
749 Map
= &mGcdMemorySpaceMap
;
750 } else if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
751 CoreAcquireGcdIoLock ();
752 Map
= &mGcdIoSpaceMap
;
758 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
760 Status
= CoreSearchGcdMapEntry (BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
761 if (EFI_ERROR (Status
)) {
762 Status
= EFI_UNSUPPORTED
;
766 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
769 // Verify that the list of descriptors are unallocated non-existent memory.
772 while (Link
!= EndLink
->ForwardLink
) {
773 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
778 case GCD_ADD_MEMORY_OPERATION
:
779 if (Entry
->GcdMemoryType
!= EfiGcdMemoryTypeNonExistent
||
780 Entry
->ImageHandle
!= NULL
) {
781 Status
= EFI_ACCESS_DENIED
;
785 case GCD_ADD_IO_OPERATION
:
786 if (Entry
->GcdIoType
!= EfiGcdIoTypeNonExistent
||
787 Entry
->ImageHandle
!= NULL
) {
788 Status
= EFI_ACCESS_DENIED
;
795 case GCD_FREE_MEMORY_OPERATION
:
796 case GCD_FREE_IO_OPERATION
:
797 if (Entry
->ImageHandle
== NULL
) {
798 Status
= EFI_NOT_FOUND
;
805 case GCD_REMOVE_MEMORY_OPERATION
:
806 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
807 Status
= EFI_NOT_FOUND
;
810 if (Entry
->ImageHandle
!= NULL
) {
811 Status
= EFI_ACCESS_DENIED
;
815 case GCD_REMOVE_IO_OPERATION
:
816 if (Entry
->GcdIoType
== EfiGcdIoTypeNonExistent
) {
817 Status
= EFI_NOT_FOUND
;
820 if (Entry
->ImageHandle
!= NULL
) {
821 Status
= EFI_ACCESS_DENIED
;
826 // Set attributes operation
828 case GCD_SET_ATTRIBUTES_MEMORY_OPERATION
:
829 if ((Attributes
& EFI_MEMORY_RUNTIME
) != 0) {
830 if ((BaseAddress
& EFI_PAGE_MASK
) != 0 || (Length
& EFI_PAGE_MASK
) != 0) {
831 Status
= EFI_INVALID_PARAMETER
;
835 if ((Entry
->Capabilities
& Attributes
) != Attributes
) {
836 Status
= EFI_UNSUPPORTED
;
841 // Set capabilities operation
843 case GCD_SET_CAPABILITIES_MEMORY_OPERATION
:
844 if ((BaseAddress
& EFI_PAGE_MASK
) != 0 || (Length
& EFI_PAGE_MASK
) != 0) {
845 Status
= EFI_INVALID_PARAMETER
;
850 // Current attributes must still be supported with new capabilities
852 if ((Capabilities
& Entry
->Attributes
) != Entry
->Attributes
) {
853 Status
= EFI_UNSUPPORTED
;
858 Link
= Link
->ForwardLink
;
862 // Allocate work space to perform this operation
864 Status
= CoreAllocateGcdMapEntry (&TopEntry
, &BottomEntry
);
865 if (EFI_ERROR (Status
)) {
866 Status
= EFI_OUT_OF_RESOURCES
;
869 ASSERT (TopEntry
!= NULL
&& BottomEntry
!= NULL
);
871 if (Operation
== GCD_SET_ATTRIBUTES_MEMORY_OPERATION
) {
873 // Call CPU Arch Protocol to attempt to set attributes on the range
875 CpuArchAttributes
= ConverToCpuArchAttributes (Attributes
);
876 if (CpuArchAttributes
!= INVALID_CPU_ARCH_ATTRIBUTES
) {
878 Status
= EFI_NOT_AVAILABLE_YET
;
880 Status
= gCpu
->SetMemoryAttributes (
887 if (EFI_ERROR (Status
)) {
888 CoreFreePool (TopEntry
);
889 CoreFreePool (BottomEntry
);
896 // Convert/Insert the list of descriptors from StartLink to EndLink
899 while (Link
!= EndLink
->ForwardLink
) {
900 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
901 CoreInsertGcdMapEntry (Link
, Entry
, BaseAddress
, Length
, TopEntry
, BottomEntry
);
906 case GCD_ADD_MEMORY_OPERATION
:
907 Entry
->GcdMemoryType
= GcdMemoryType
;
908 if (GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
909 Entry
->Capabilities
= Capabilities
| EFI_MEMORY_RUNTIME
| EFI_MEMORY_PORT_IO
;
911 Entry
->Capabilities
= Capabilities
| EFI_MEMORY_RUNTIME
;
914 case GCD_ADD_IO_OPERATION
:
915 Entry
->GcdIoType
= GcdIoType
;
920 case GCD_FREE_MEMORY_OPERATION
:
921 case GCD_FREE_IO_OPERATION
:
922 Entry
->ImageHandle
= NULL
;
923 Entry
->DeviceHandle
= NULL
;
928 case GCD_REMOVE_MEMORY_OPERATION
:
929 Entry
->GcdMemoryType
= EfiGcdMemoryTypeNonExistent
;
930 Entry
->Capabilities
= 0;
932 case GCD_REMOVE_IO_OPERATION
:
933 Entry
->GcdIoType
= EfiGcdIoTypeNonExistent
;
936 // Set attributes operation
938 case GCD_SET_ATTRIBUTES_MEMORY_OPERATION
:
939 Entry
->Attributes
= Attributes
;
942 // Set capabilities operation
944 case GCD_SET_CAPABILITIES_MEMORY_OPERATION
:
945 Entry
->Capabilities
= Capabilities
;
948 Link
= Link
->ForwardLink
;
954 Status
= CoreCleanupGcdMapEntry (TopEntry
, BottomEntry
, StartLink
, EndLink
, Map
);
957 DEBUG ((DEBUG_GCD
, " Status = %r\n", Status
));
959 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
960 CoreReleaseGcdMemoryLock ();
961 CoreDumpGcdMemorySpaceMap (FALSE
);
963 if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
964 CoreReleaseGcdIoLock ();
965 CoreDumpGcdIoSpaceMap (FALSE
);
973 Check whether an entry could be used to allocate space.
975 @param Operation Allocate memory or IO
976 @param Entry The entry to be tested
977 @param GcdMemoryType The desired memory type
978 @param GcdIoType The desired IO type
980 @retval EFI_NOT_FOUND The memory type does not match or there's an
981 image handle on the entry.
982 @retval EFI_UNSUPPORTED The operation unsupported.
983 @retval EFI_SUCCESS It's ok for this entry to be used to allocate
988 CoreAllocateSpaceCheckEntry (
990 IN EFI_GCD_MAP_ENTRY
*Entry
,
991 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
992 IN EFI_GCD_IO_TYPE GcdIoType
995 if (Entry
->ImageHandle
!= NULL
) {
996 return EFI_NOT_FOUND
;
999 case GCD_ALLOCATE_MEMORY_OPERATION
:
1000 if (Entry
->GcdMemoryType
!= GcdMemoryType
) {
1001 return EFI_NOT_FOUND
;
1004 case GCD_ALLOCATE_IO_OPERATION
:
1005 if (Entry
->GcdIoType
!= GcdIoType
) {
1006 return EFI_NOT_FOUND
;
1010 return EFI_UNSUPPORTED
;
1017 Allocate space on specified address and length.
1019 @param Operation The type of operation (memory or IO)
1020 @param GcdAllocateType The type of allocate operation
1021 @param GcdMemoryType The desired memory type
1022 @param GcdIoType The desired IO type
1023 @param Alignment Align with 2^Alignment
1024 @param Length Length to allocate
1025 @param BaseAddress Base address to allocate
1026 @param ImageHandle The image handle consume the allocated space.
1027 @param DeviceHandle The device handle consume the allocated space.
1029 @retval EFI_INVALID_PARAMETER Invalid parameter.
1030 @retval EFI_NOT_FOUND No descriptor for the desired space exists.
1031 @retval EFI_SUCCESS Space successfully allocated.
1037 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1038 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1039 IN EFI_GCD_IO_TYPE GcdIoType
,
1042 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1043 IN EFI_HANDLE ImageHandle
,
1044 IN EFI_HANDLE DeviceHandle OPTIONAL
1048 EFI_PHYSICAL_ADDRESS AlignmentMask
;
1049 EFI_PHYSICAL_ADDRESS MaxAddress
;
1052 LIST_ENTRY
*SubLink
;
1053 EFI_GCD_MAP_ENTRY
*Entry
;
1054 EFI_GCD_MAP_ENTRY
*TopEntry
;
1055 EFI_GCD_MAP_ENTRY
*BottomEntry
;
1056 LIST_ENTRY
*StartLink
;
1057 LIST_ENTRY
*EndLink
;
1061 // Make sure parameters are valid
1063 if ((UINT32
)GcdAllocateType
>= EfiGcdMaxAllocateType
) {
1064 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1065 return EFI_INVALID_PARAMETER
;
1067 if ((UINT32
)GcdMemoryType
>= EfiGcdMemoryTypeMaximum
) {
1068 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1069 return EFI_INVALID_PARAMETER
;
1071 if ((UINT32
)GcdIoType
>= EfiGcdIoTypeMaximum
) {
1072 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1073 return EFI_INVALID_PARAMETER
;
1075 if (BaseAddress
== NULL
) {
1076 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1077 return EFI_INVALID_PARAMETER
;
1079 if (ImageHandle
== NULL
) {
1080 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1081 return EFI_INVALID_PARAMETER
;
1083 if (Alignment
>= 64) {
1084 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_NOT_FOUND
));
1085 return EFI_NOT_FOUND
;
1088 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1089 return EFI_INVALID_PARAMETER
;
1093 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
1094 CoreAcquireGcdMemoryLock ();
1095 Map
= &mGcdMemorySpaceMap
;
1096 } else if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
1097 CoreAcquireGcdIoLock ();
1098 Map
= &mGcdIoSpaceMap
;
1107 // Compute alignment bit mask
1109 AlignmentMask
= LShiftU64 (1, Alignment
) - 1;
1111 if (GcdAllocateType
== EfiGcdAllocateAddress
) {
1113 // Verify that the BaseAddress passed in is aligned correctly
1115 if ((*BaseAddress
& AlignmentMask
) != 0) {
1116 Status
= EFI_NOT_FOUND
;
1121 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
1123 Status
= CoreSearchGcdMapEntry (*BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
1124 if (EFI_ERROR (Status
)) {
1125 Status
= EFI_NOT_FOUND
;
1128 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1131 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1134 while (Link
!= EndLink
->ForwardLink
) {
1135 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1136 Link
= Link
->ForwardLink
;
1137 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1138 if (EFI_ERROR (Status
)) {
1145 Entry
= CR (Map
->BackLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1148 // Compute the maximum address to use in the search algorithm
1150 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchBottomUp
||
1151 GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
) {
1152 MaxAddress
= *BaseAddress
;
1154 MaxAddress
= Entry
->EndAddress
;
1158 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1160 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
1161 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
1162 Link
= Map
->BackLink
;
1164 Link
= Map
->ForwardLink
;
1166 while (Link
!= Map
) {
1167 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1169 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
1170 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
1171 Link
= Link
->BackLink
;
1173 Link
= Link
->ForwardLink
;
1176 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1177 if (EFI_ERROR (Status
)) {
1181 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
1182 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
1183 if ((Entry
->BaseAddress
+ Length
) > MaxAddress
) {
1186 if (Length
> (Entry
->EndAddress
+ 1)) {
1187 Status
= EFI_NOT_FOUND
;
1190 if (Entry
->EndAddress
> MaxAddress
) {
1191 *BaseAddress
= MaxAddress
;
1193 *BaseAddress
= Entry
->EndAddress
;
1195 *BaseAddress
= (*BaseAddress
+ 1 - Length
) & (~AlignmentMask
);
1197 *BaseAddress
= (Entry
->BaseAddress
+ AlignmentMask
) & (~AlignmentMask
);
1198 if ((*BaseAddress
+ Length
- 1) > MaxAddress
) {
1199 Status
= EFI_NOT_FOUND
;
1205 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
1207 Status
= CoreSearchGcdMapEntry (*BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
1208 if (EFI_ERROR (Status
)) {
1209 Status
= EFI_NOT_FOUND
;
1212 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1216 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1219 SubLink
= StartLink
;
1220 while (SubLink
!= EndLink
->ForwardLink
) {
1221 Entry
= CR (SubLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1222 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1223 if (EFI_ERROR (Status
)) {
1228 SubLink
= SubLink
->ForwardLink
;
1236 Status
= EFI_NOT_FOUND
;
1241 // Allocate work space to perform this operation
1243 Status
= CoreAllocateGcdMapEntry (&TopEntry
, &BottomEntry
);
1244 if (EFI_ERROR (Status
)) {
1245 Status
= EFI_OUT_OF_RESOURCES
;
1248 ASSERT (TopEntry
!= NULL
&& BottomEntry
!= NULL
);
1251 // Convert/Insert the list of descriptors from StartLink to EndLink
1254 while (Link
!= EndLink
->ForwardLink
) {
1255 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1256 CoreInsertGcdMapEntry (Link
, Entry
, *BaseAddress
, Length
, TopEntry
, BottomEntry
);
1257 Entry
->ImageHandle
= ImageHandle
;
1258 Entry
->DeviceHandle
= DeviceHandle
;
1259 Link
= Link
->ForwardLink
;
1265 Status
= CoreCleanupGcdMapEntry (TopEntry
, BottomEntry
, StartLink
, EndLink
, Map
);
1268 DEBUG ((DEBUG_GCD
, " Status = %r", Status
));
1269 if (!EFI_ERROR (Status
)) {
1270 DEBUG ((DEBUG_GCD
, " (BaseAddress = %016lx)", *BaseAddress
));
1272 DEBUG ((DEBUG_GCD
, "\n"));
1274 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
1275 CoreReleaseGcdMemoryLock ();
1276 CoreDumpGcdMemorySpaceMap (FALSE
);
1278 if ((Operation
& GCD_IO_SPACE_OPERATION
) !=0) {
1279 CoreReleaseGcdIoLock ();
1280 CoreDumpGcdIoSpaceMap (FALSE
);
1288 Add a segment of memory to GCD map.
1290 @param GcdMemoryType Memory type of the segment.
1291 @param BaseAddress Base address of the segment.
1292 @param Length Length of the segment.
1293 @param Capabilities alterable attributes of the segment.
1295 @retval EFI_INVALID_PARAMETER Invalid parameters.
1296 @retval EFI_SUCCESS Successfully add a segment of memory space.
1300 CoreInternalAddMemorySpace (
1301 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1302 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1304 IN UINT64 Capabilities
1307 DEBUG ((DEBUG_GCD
, "GCD:AddMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1308 DEBUG ((DEBUG_GCD
, " GcdMemoryType = %a\n", mGcdMemoryTypeNames
[MIN (GcdMemoryType
, EfiGcdMemoryTypeMaximum
)]));
1309 DEBUG ((DEBUG_GCD
, " Capabilities = %016lx\n", Capabilities
));
1312 // Make sure parameters are valid
1314 if (GcdMemoryType
<= EfiGcdMemoryTypeNonExistent
|| GcdMemoryType
>= EfiGcdMemoryTypeMaximum
) {
1315 return EFI_INVALID_PARAMETER
;
1318 return CoreConvertSpace (GCD_ADD_MEMORY_OPERATION
, GcdMemoryType
, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, Capabilities
, 0);
1322 // GCD Core Services
1326 Allocates nonexistent memory, reserved memory, system memory, or memorymapped
1327 I/O resources from the global coherency domain of the processor.
1329 @param GcdAllocateType The type of allocate operation
1330 @param GcdMemoryType The desired memory type
1331 @param Alignment Align with 2^Alignment
1332 @param Length Length to allocate
1333 @param BaseAddress Base address to allocate
1334 @param ImageHandle The image handle consume the allocated space.
1335 @param DeviceHandle The device handle consume the allocated space.
1337 @retval EFI_INVALID_PARAMETER Invalid parameter.
1338 @retval EFI_NOT_FOUND No descriptor contains the desired space.
1339 @retval EFI_SUCCESS Memory space successfully allocated.
1344 CoreAllocateMemorySpace (
1345 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1346 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1349 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1350 IN EFI_HANDLE ImageHandle
,
1351 IN EFI_HANDLE DeviceHandle OPTIONAL
1354 if (BaseAddress
!= NULL
) {
1355 DEBUG ((DEBUG_GCD
, "GCD:AllocateMemorySpace(Base=%016lx,Length=%016lx)\n", *BaseAddress
, Length
));
1357 DEBUG ((DEBUG_GCD
, "GCD:AllocateMemorySpace(Base=<NULL>,Length=%016lx)\n", Length
));
1359 DEBUG ((DEBUG_GCD
, " GcdAllocateType = %a\n", mGcdAllocationTypeNames
[MIN (GcdAllocateType
, EfiGcdMaxAllocateType
)]));
1360 DEBUG ((DEBUG_GCD
, " GcdMemoryType = %a\n", mGcdMemoryTypeNames
[MIN (GcdMemoryType
, EfiGcdMemoryTypeMaximum
)]));
1361 DEBUG ((DEBUG_GCD
, " Alignment = %016lx\n", LShiftU64 (1, Alignment
)));
1362 DEBUG ((DEBUG_GCD
, " ImageHandle = %p\n", ImageHandle
));
1363 DEBUG ((DEBUG_GCD
, " DeviceHandle = %p\n", DeviceHandle
));
1365 return CoreAllocateSpace (
1366 GCD_ALLOCATE_MEMORY_OPERATION
,
1369 (EFI_GCD_IO_TYPE
) 0,
1380 Adds reserved memory, system memory, or memory-mapped I/O resources to the
1381 global coherency domain of the processor.
1383 @param GcdMemoryType Memory type of the memory space.
1384 @param BaseAddress Base address of the memory space.
1385 @param Length Length of the memory space.
1386 @param Capabilities alterable attributes of the memory space.
1388 @retval EFI_SUCCESS Merged this memory space into GCD map.
1393 CoreAddMemorySpace (
1394 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1395 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1397 IN UINT64 Capabilities
1401 EFI_PHYSICAL_ADDRESS PageBaseAddress
;
1404 Status
= CoreInternalAddMemorySpace (GcdMemoryType
, BaseAddress
, Length
, Capabilities
);
1406 if (!EFI_ERROR (Status
) && ((GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) || (GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
))) {
1408 PageBaseAddress
= PageAlignAddress (BaseAddress
);
1409 PageLength
= PageAlignLength (BaseAddress
+ Length
- PageBaseAddress
);
1411 Status
= CoreAllocateMemorySpace (
1412 EfiGcdAllocateAddress
,
1417 gDxeCoreImageHandle
,
1421 if (!EFI_ERROR (Status
)) {
1422 CoreAddMemoryDescriptor (
1423 EfiConventionalMemory
,
1425 RShiftU64 (PageLength
, EFI_PAGE_SHIFT
),
1429 for (; PageLength
!= 0; PageLength
-= EFI_PAGE_SIZE
, PageBaseAddress
+= EFI_PAGE_SIZE
) {
1430 Status
= CoreAllocateMemorySpace (
1431 EfiGcdAllocateAddress
,
1436 gDxeCoreImageHandle
,
1440 if (!EFI_ERROR (Status
)) {
1441 CoreAddMemoryDescriptor (
1442 EfiConventionalMemory
,
1456 Frees nonexistent memory, reserved memory, system memory, or memory-mapped
1457 I/O resources from the global coherency domain of the processor.
1459 @param BaseAddress Base address of the memory space.
1460 @param Length Length of the memory space.
1462 @retval EFI_SUCCESS Space successfully freed.
1467 CoreFreeMemorySpace (
1468 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1472 DEBUG ((DEBUG_GCD
, "GCD:FreeMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1474 return CoreConvertSpace (GCD_FREE_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1479 Removes reserved memory, system memory, or memory-mapped I/O resources from
1480 the global coherency domain of the processor.
1482 @param BaseAddress Base address of the memory space.
1483 @param Length Length of the memory space.
1485 @retval EFI_SUCCESS Successfully remove a segment of memory space.
1490 CoreRemoveMemorySpace (
1491 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1495 DEBUG ((DEBUG_GCD
, "GCD:RemoveMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1497 return CoreConvertSpace (GCD_REMOVE_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1502 Build a memory descriptor according to an entry.
1504 @param Descriptor The descriptor to be built
1505 @param Entry According to this entry
1509 BuildMemoryDescriptor (
1510 IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
,
1511 IN EFI_GCD_MAP_ENTRY
*Entry
1514 Descriptor
->BaseAddress
= Entry
->BaseAddress
;
1515 Descriptor
->Length
= Entry
->EndAddress
- Entry
->BaseAddress
+ 1;
1516 Descriptor
->Capabilities
= Entry
->Capabilities
;
1517 Descriptor
->Attributes
= Entry
->Attributes
;
1518 Descriptor
->GcdMemoryType
= Entry
->GcdMemoryType
;
1519 Descriptor
->ImageHandle
= Entry
->ImageHandle
;
1520 Descriptor
->DeviceHandle
= Entry
->DeviceHandle
;
1525 Retrieves the descriptor for a memory region containing a specified address.
1527 @param BaseAddress Specified start address
1528 @param Descriptor Specified length
1530 @retval EFI_INVALID_PARAMETER Invalid parameter
1531 @retval EFI_SUCCESS Successfully get memory space descriptor.
1536 CoreGetMemorySpaceDescriptor (
1537 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1538 OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
1542 LIST_ENTRY
*StartLink
;
1543 LIST_ENTRY
*EndLink
;
1544 EFI_GCD_MAP_ENTRY
*Entry
;
1547 // Make sure parameters are valid
1549 if (Descriptor
== NULL
) {
1550 return EFI_INVALID_PARAMETER
;
1553 CoreAcquireGcdMemoryLock ();
1556 // Search for the list of descriptors that contain BaseAddress
1558 Status
= CoreSearchGcdMapEntry (BaseAddress
, 1, &StartLink
, &EndLink
, &mGcdMemorySpaceMap
);
1559 if (EFI_ERROR (Status
)) {
1560 Status
= EFI_NOT_FOUND
;
1562 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1564 // Copy the contents of the found descriptor into Descriptor
1566 Entry
= CR (StartLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1567 BuildMemoryDescriptor (Descriptor
, Entry
);
1570 CoreReleaseGcdMemoryLock ();
1577 Modifies the attributes for a memory region in the global coherency domain of the
1580 @param BaseAddress Specified start address
1581 @param Length Specified length
1582 @param Attributes Specified attributes
1584 @retval EFI_SUCCESS The attributes were set for the memory region.
1585 @retval EFI_INVALID_PARAMETER Length is zero.
1586 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
1587 resource range specified by BaseAddress and Length.
1588 @retval EFI_UNSUPPORTED The bit mask of attributes is not support for the memory resource
1589 range specified by BaseAddress and Length.
1590 @retval EFI_ACCESS_DEFINED The attributes for the memory resource range specified by
1591 BaseAddress and Length cannot be modified.
1592 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
1593 the memory resource range.
1594 @retval EFI_NOT_AVAILABLE_YET The attributes cannot be set because CPU architectural protocol is
1600 CoreSetMemorySpaceAttributes (
1601 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1603 IN UINT64 Attributes
1606 DEBUG ((DEBUG_GCD
, "GCD:SetMemorySpaceAttributes(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1607 DEBUG ((DEBUG_GCD
, " Attributes = %016lx\n", Attributes
));
1609 return CoreConvertSpace (GCD_SET_ATTRIBUTES_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, Attributes
);
1614 Modifies the capabilities for a memory region in the global coherency domain of the
1617 @param BaseAddress The physical address that is the start address of a memory region.
1618 @param Length The size in bytes of the memory region.
1619 @param Capabilities The bit mask of capabilities that the memory region supports.
1621 @retval EFI_SUCCESS The capabilities were set for the memory region.
1622 @retval EFI_INVALID_PARAMETER Length is zero.
1623 @retval EFI_UNSUPPORTED The capabilities specified by Capabilities do not include the
1624 memory region attributes currently in use.
1625 @retval EFI_ACCESS_DENIED The capabilities for the memory resource range specified by
1626 BaseAddress and Length cannot be modified.
1627 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the capabilities
1628 of the memory resource range.
1632 CoreSetMemorySpaceCapabilities (
1633 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1635 IN UINT64 Capabilities
1640 DEBUG ((DEBUG_GCD
, "GCD:CoreSetMemorySpaceCapabilities(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1641 DEBUG ((DEBUG_GCD
, " Capabilities = %016lx\n", Capabilities
));
1643 Status
= CoreConvertSpace (GCD_SET_CAPABILITIES_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, Capabilities
, 0);
1644 if (!EFI_ERROR(Status
)) {
1645 CoreUpdateMemoryAttributes(BaseAddress
, RShiftU64(Length
, EFI_PAGE_SHIFT
), Capabilities
& (~EFI_MEMORY_RUNTIME
));
1653 Returns a map of the memory resources in the global coherency domain of the
1656 @param NumberOfDescriptors Number of descriptors.
1657 @param MemorySpaceMap Descriptor array
1659 @retval EFI_INVALID_PARAMETER Invalid parameter
1660 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1661 @retval EFI_SUCCESS Successfully get memory space map.
1666 CoreGetMemorySpaceMap (
1667 OUT UINTN
*NumberOfDescriptors
,
1668 OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
**MemorySpaceMap
1673 EFI_GCD_MAP_ENTRY
*Entry
;
1674 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
;
1677 // Make sure parameters are valid
1679 if (NumberOfDescriptors
== NULL
) {
1680 return EFI_INVALID_PARAMETER
;
1682 if (MemorySpaceMap
== NULL
) {
1683 return EFI_INVALID_PARAMETER
;
1686 CoreAcquireGcdMemoryLock ();
1689 // Count the number of descriptors
1691 *NumberOfDescriptors
= CoreCountGcdMapEntry (&mGcdMemorySpaceMap
);
1694 // Allocate the MemorySpaceMap
1696 *MemorySpaceMap
= AllocatePool (*NumberOfDescriptors
* sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR
));
1697 if (*MemorySpaceMap
== NULL
) {
1698 Status
= EFI_OUT_OF_RESOURCES
;
1703 // Fill in the MemorySpaceMap
1705 Descriptor
= *MemorySpaceMap
;
1706 Link
= mGcdMemorySpaceMap
.ForwardLink
;
1707 while (Link
!= &mGcdMemorySpaceMap
) {
1708 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1709 BuildMemoryDescriptor (Descriptor
, Entry
);
1711 Link
= Link
->ForwardLink
;
1713 Status
= EFI_SUCCESS
;
1716 CoreReleaseGcdMemoryLock ();
1722 Adds reserved I/O or I/O resources to the global coherency domain of the processor.
1724 @param GcdIoType IO type of the segment.
1725 @param BaseAddress Base address of the segment.
1726 @param Length Length of the segment.
1728 @retval EFI_SUCCESS Merged this segment into GCD map.
1729 @retval EFI_INVALID_PARAMETER Parameter not valid
1735 IN EFI_GCD_IO_TYPE GcdIoType
,
1736 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1740 DEBUG ((DEBUG_GCD
, "GCD:AddIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1741 DEBUG ((DEBUG_GCD
, " GcdIoType = %a\n", mGcdIoTypeNames
[MIN (GcdIoType
, EfiGcdIoTypeMaximum
)]));
1744 // Make sure parameters are valid
1746 if (GcdIoType
<= EfiGcdIoTypeNonExistent
|| GcdIoType
>= EfiGcdIoTypeMaximum
) {
1747 return EFI_INVALID_PARAMETER
;
1749 return CoreConvertSpace (GCD_ADD_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, GcdIoType
, BaseAddress
, Length
, 0, 0);
1754 Allocates nonexistent I/O, reserved I/O, or I/O resources from the global coherency
1755 domain of the processor.
1757 @param GcdAllocateType The type of allocate operation
1758 @param GcdIoType The desired IO type
1759 @param Alignment Align with 2^Alignment
1760 @param Length Length to allocate
1761 @param BaseAddress Base address to allocate
1762 @param ImageHandle The image handle consume the allocated space.
1763 @param DeviceHandle The device handle consume the allocated space.
1765 @retval EFI_INVALID_PARAMETER Invalid parameter.
1766 @retval EFI_NOT_FOUND No descriptor contains the desired space.
1767 @retval EFI_SUCCESS IO space successfully allocated.
1772 CoreAllocateIoSpace (
1773 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1774 IN EFI_GCD_IO_TYPE GcdIoType
,
1777 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1778 IN EFI_HANDLE ImageHandle
,
1779 IN EFI_HANDLE DeviceHandle OPTIONAL
1782 if (BaseAddress
!= NULL
) {
1783 DEBUG ((DEBUG_GCD
, "GCD:AllocateIoSpace(Base=%016lx,Length=%016lx)\n", *BaseAddress
, Length
));
1785 DEBUG ((DEBUG_GCD
, "GCD:AllocateIoSpace(Base=<NULL>,Length=%016lx)\n", Length
));
1787 DEBUG ((DEBUG_GCD
, " GcdAllocateType = %a\n", mGcdAllocationTypeNames
[MIN (GcdAllocateType
, EfiGcdMaxAllocateType
)]));
1788 DEBUG ((DEBUG_GCD
, " GcdIoType = %a\n", mGcdIoTypeNames
[MIN (GcdIoType
, EfiGcdIoTypeMaximum
)]));
1789 DEBUG ((DEBUG_GCD
, " Alignment = %016lx\n", LShiftU64 (1, Alignment
)));
1790 DEBUG ((DEBUG_GCD
, " ImageHandle = %p\n", ImageHandle
));
1791 DEBUG ((DEBUG_GCD
, " DeviceHandle = %p\n", DeviceHandle
));
1793 return CoreAllocateSpace (
1794 GCD_ALLOCATE_IO_OPERATION
,
1796 (EFI_GCD_MEMORY_TYPE
) 0,
1808 Frees nonexistent I/O, reserved I/O, or I/O resources from the global coherency
1809 domain of the processor.
1811 @param BaseAddress Base address of the segment.
1812 @param Length Length of the segment.
1814 @retval EFI_SUCCESS Space successfully freed.
1820 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1824 DEBUG ((DEBUG_GCD
, "GCD:FreeIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1826 return CoreConvertSpace (GCD_FREE_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1831 Removes reserved I/O or I/O resources from the global coherency domain of the
1834 @param BaseAddress Base address of the segment.
1835 @param Length Length of the segment.
1837 @retval EFI_SUCCESS Successfully removed a segment of IO space.
1843 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1847 DEBUG ((DEBUG_GCD
, "GCD:RemoveIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1849 return CoreConvertSpace (GCD_REMOVE_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1854 Build a IO descriptor according to an entry.
1856 @param Descriptor The descriptor to be built
1857 @param Entry According to this entry
1862 IN EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
,
1863 IN EFI_GCD_MAP_ENTRY
*Entry
1866 Descriptor
->BaseAddress
= Entry
->BaseAddress
;
1867 Descriptor
->Length
= Entry
->EndAddress
- Entry
->BaseAddress
+ 1;
1868 Descriptor
->GcdIoType
= Entry
->GcdIoType
;
1869 Descriptor
->ImageHandle
= Entry
->ImageHandle
;
1870 Descriptor
->DeviceHandle
= Entry
->DeviceHandle
;
1875 Retrieves the descriptor for an I/O region containing a specified address.
1877 @param BaseAddress Specified start address
1878 @param Descriptor Specified length
1880 @retval EFI_INVALID_PARAMETER Descriptor is NULL.
1881 @retval EFI_SUCCESS Successfully get the IO space descriptor.
1886 CoreGetIoSpaceDescriptor (
1887 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1888 OUT EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
1892 LIST_ENTRY
*StartLink
;
1893 LIST_ENTRY
*EndLink
;
1894 EFI_GCD_MAP_ENTRY
*Entry
;
1897 // Make sure parameters are valid
1899 if (Descriptor
== NULL
) {
1900 return EFI_INVALID_PARAMETER
;
1903 CoreAcquireGcdIoLock ();
1906 // Search for the list of descriptors that contain BaseAddress
1908 Status
= CoreSearchGcdMapEntry (BaseAddress
, 1, &StartLink
, &EndLink
, &mGcdIoSpaceMap
);
1909 if (EFI_ERROR (Status
)) {
1910 Status
= EFI_NOT_FOUND
;
1912 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1914 // Copy the contents of the found descriptor into Descriptor
1916 Entry
= CR (StartLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1917 BuildIoDescriptor (Descriptor
, Entry
);
1920 CoreReleaseGcdIoLock ();
1927 Returns a map of the I/O resources in the global coherency domain of the processor.
1929 @param NumberOfDescriptors Number of descriptors.
1930 @param IoSpaceMap Descriptor array
1932 @retval EFI_INVALID_PARAMETER Invalid parameter
1933 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1934 @retval EFI_SUCCESS Successfully get IO space map.
1940 OUT UINTN
*NumberOfDescriptors
,
1941 OUT EFI_GCD_IO_SPACE_DESCRIPTOR
**IoSpaceMap
1946 EFI_GCD_MAP_ENTRY
*Entry
;
1947 EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
;
1950 // Make sure parameters are valid
1952 if (NumberOfDescriptors
== NULL
) {
1953 return EFI_INVALID_PARAMETER
;
1955 if (IoSpaceMap
== NULL
) {
1956 return EFI_INVALID_PARAMETER
;
1959 CoreAcquireGcdIoLock ();
1962 // Count the number of descriptors
1964 *NumberOfDescriptors
= CoreCountGcdMapEntry (&mGcdIoSpaceMap
);
1967 // Allocate the IoSpaceMap
1969 *IoSpaceMap
= AllocatePool (*NumberOfDescriptors
* sizeof (EFI_GCD_IO_SPACE_DESCRIPTOR
));
1970 if (*IoSpaceMap
== NULL
) {
1971 Status
= EFI_OUT_OF_RESOURCES
;
1976 // Fill in the IoSpaceMap
1978 Descriptor
= *IoSpaceMap
;
1979 Link
= mGcdIoSpaceMap
.ForwardLink
;
1980 while (Link
!= &mGcdIoSpaceMap
) {
1981 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1982 BuildIoDescriptor (Descriptor
, Entry
);
1984 Link
= Link
->ForwardLink
;
1986 Status
= EFI_SUCCESS
;
1989 CoreReleaseGcdIoLock ();
1995 Converts a Resource Descriptor HOB attributes mask to an EFI Memory Descriptor
1998 @param GcdMemoryType Type of resource in the GCD memory map.
1999 @param Attributes The attribute mask in the Resource Descriptor
2002 @return The capabilities mask for an EFI Memory Descriptor.
2006 CoreConvertResourceDescriptorHobAttributesToCapabilities (
2007 EFI_GCD_MEMORY_TYPE GcdMemoryType
,
2011 UINT64 Capabilities
;
2012 GCD_ATTRIBUTE_CONVERSION_ENTRY
*Conversion
;
2015 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2017 for (Capabilities
= 0, Conversion
= mAttributeConversionTable
; Conversion
->Attribute
!= 0; Conversion
++) {
2018 if (Conversion
->Memory
|| ((GcdMemoryType
!= EfiGcdMemoryTypeSystemMemory
) && (GcdMemoryType
!= EfiGcdMemoryTypeMoreReliable
))) {
2019 if (Attributes
& Conversion
->Attribute
) {
2020 Capabilities
|= Conversion
->Capability
;
2025 return Capabilities
;
2029 Calculate total memory bin size neeeded.
2031 @return The total memory bin size neeeded.
2035 CalculateTotalMemoryBinSizeNeeded (
2043 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
2046 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
2047 TotalSize
+= LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
);
2054 External function. Initializes memory services based on the memory
2055 descriptor HOBs. This function is responsible for priming the memory
2056 map, so memory allocations and resource allocations can be made.
2057 The first part of this function can not depend on any memory services
2058 until at least one memory descriptor is provided to the memory services.
2060 @param HobStart The start address of the HOB.
2061 @param MemoryBaseAddress Start address of memory region found to init DXE
2063 @param MemoryLength Length of memory region found to init DXE core.
2065 @retval EFI_SUCCESS Memory services successfully initialized.
2069 CoreInitializeMemoryServices (
2071 OUT EFI_PHYSICAL_ADDRESS
*MemoryBaseAddress
,
2072 OUT UINT64
*MemoryLength
2075 EFI_PEI_HOB_POINTERS Hob
;
2076 EFI_MEMORY_TYPE_INFORMATION
*EfiMemoryTypeInformation
;
2079 EFI_HOB_HANDOFF_INFO_TABLE
*PhitHob
;
2080 EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
;
2081 EFI_HOB_RESOURCE_DESCRIPTOR
*PhitResourceHob
;
2082 EFI_PHYSICAL_ADDRESS BaseAddress
;
2085 UINT64 Capabilities
;
2086 EFI_PHYSICAL_ADDRESS TestedMemoryBaseAddress
;
2087 UINT64 TestedMemoryLength
;
2088 EFI_PHYSICAL_ADDRESS HighAddress
;
2089 EFI_HOB_GUID_TYPE
*GuidHob
;
2090 UINT32 ReservedCodePageNumber
;
2091 UINT64 MinimalMemorySizeNeeded
;
2094 // Point at the first HOB. This must be the PHIT HOB.
2096 Hob
.Raw
= *HobStart
;
2097 ASSERT (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_HANDOFF
);
2100 // Initialize the spin locks and maps in the memory services.
2101 // Also fill in the memory services into the EFI Boot Services Table
2103 CoreInitializePool ();
2106 // Initialize Local Variables
2108 PhitResourceHob
= NULL
;
2115 // Cache the PHIT HOB for later use
2117 PhitHob
= Hob
.HandoffInformationTable
;
2119 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
2120 ReservedCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
2121 ReservedCodePageNumber
+= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
2124 // cache the Top address for loading modules at Fixed Address
2126 gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
= PhitHob
->EfiMemoryTop
2127 + EFI_PAGES_TO_SIZE(ReservedCodePageNumber
);
2130 // See if a Memory Type Information HOB is available
2132 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
2133 if (GuidHob
!= NULL
) {
2134 EfiMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
2135 DataSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
2136 if (EfiMemoryTypeInformation
!= NULL
&& DataSize
> 0 && DataSize
<= (EfiMaxMemoryType
+ 1) * sizeof (EFI_MEMORY_TYPE_INFORMATION
)) {
2137 CopyMem (&gMemoryTypeInformation
, EfiMemoryTypeInformation
, DataSize
);
2142 // Include the total memory bin size needed to make sure memory bin could be allocated successfully.
2144 MinimalMemorySizeNeeded
= MINIMUM_INITIAL_MEMORY_SIZE
+ CalculateTotalMemoryBinSizeNeeded ();
2147 // Find the Resource Descriptor HOB that contains PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop
2150 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2152 // Skip all HOBs except Resource Descriptor HOBs
2154 if (GET_HOB_TYPE (Hob
) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2159 // Skip Resource Descriptor HOBs that do not describe tested system memory
2161 ResourceHob
= Hob
.ResourceDescriptor
;
2162 if (ResourceHob
->ResourceType
!= EFI_RESOURCE_SYSTEM_MEMORY
) {
2165 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) != TESTED_MEMORY_ATTRIBUTES
) {
2170 // Skip Resource Descriptor HOBs that do not contain the PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop
2172 if (PhitHob
->EfiFreeMemoryBottom
< ResourceHob
->PhysicalStart
) {
2175 if (PhitHob
->EfiFreeMemoryTop
> (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
)) {
2180 // Cache the resource descriptor HOB for the memory region described by the PHIT HOB
2182 PhitResourceHob
= ResourceHob
;
2186 // Compute range between PHIT EfiMemoryTop and the end of the Resource Descriptor HOB
2188 Attributes
= PhitResourceHob
->ResourceAttribute
;
2189 BaseAddress
= PageAlignAddress (PhitHob
->EfiMemoryTop
);
2190 Length
= PageAlignLength (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
- BaseAddress
);
2191 if (Length
< MinimalMemorySizeNeeded
) {
2193 // If that range is not large enough to intialize the DXE Core, then
2194 // Compute range between PHIT EfiFreeMemoryBottom and PHIT EfiFreeMemoryTop
2196 BaseAddress
= PageAlignAddress (PhitHob
->EfiFreeMemoryBottom
);
2197 Length
= PageAlignLength (PhitHob
->EfiFreeMemoryTop
- BaseAddress
);
2198 if (Length
< MinimalMemorySizeNeeded
) {
2200 // If that range is not large enough to intialize the DXE Core, then
2201 // Compute range between the start of the Resource Descriptor HOB and the start of the HOB List
2203 BaseAddress
= PageAlignAddress (ResourceHob
->PhysicalStart
);
2204 Length
= PageAlignLength ((UINT64
)((UINTN
)*HobStart
- BaseAddress
));
2211 // Assert if a resource descriptor HOB for the memory region described by the PHIT was not found
2216 // Take the range in the resource descriptor HOB for the memory region described
2217 // by the PHIT as higher priority if it is big enough. It can make the memory bin
2218 // allocated to be at the same memory region with PHIT that has more better compatibility
2219 // to avoid memory fragmentation for some code practices assume and allocate <4G ACPI memory.
2221 if (Length
< MinimalMemorySizeNeeded
) {
2223 // Search all the resource descriptor HOBs from the highest possible addresses down for a memory
2224 // region that is big enough to initialize the DXE core. Always skip the PHIT Resource HOB.
2225 // The max address must be within the physically addressible range for the processor.
2227 HighAddress
= MAX_ADDRESS
;
2228 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2230 // Skip the Resource Descriptor HOB that contains the PHIT
2232 if (Hob
.ResourceDescriptor
== PhitResourceHob
) {
2236 // Skip all HOBs except Resource Descriptor HOBs
2238 if (GET_HOB_TYPE (Hob
) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2243 // Skip Resource Descriptor HOBs that do not describe tested system memory below MAX_ADDRESS
2245 ResourceHob
= Hob
.ResourceDescriptor
;
2246 if (ResourceHob
->ResourceType
!= EFI_RESOURCE_SYSTEM_MEMORY
) {
2249 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) != TESTED_MEMORY_ATTRIBUTES
) {
2252 if ((ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
) > (EFI_PHYSICAL_ADDRESS
)MAX_ADDRESS
) {
2257 // Skip Resource Descriptor HOBs that are below a previously found Resource Descriptor HOB
2259 if (HighAddress
!= (EFI_PHYSICAL_ADDRESS
)MAX_ADDRESS
&& ResourceHob
->PhysicalStart
<= HighAddress
) {
2264 // Skip Resource Descriptor HOBs that are not large enough to initilize the DXE Core
2266 TestedMemoryBaseAddress
= PageAlignAddress (ResourceHob
->PhysicalStart
);
2267 TestedMemoryLength
= PageAlignLength (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
- TestedMemoryBaseAddress
);
2268 if (TestedMemoryLength
< MinimalMemorySizeNeeded
) {
2273 // Save the range described by the Resource Descriptor that is large enough to initilize the DXE Core
2275 BaseAddress
= TestedMemoryBaseAddress
;
2276 Length
= TestedMemoryLength
;
2277 Attributes
= ResourceHob
->ResourceAttribute
;
2278 HighAddress
= ResourceHob
->PhysicalStart
;
2282 DEBUG ((EFI_D_INFO
, "CoreInitializeMemoryServices:\n"));
2283 DEBUG ((EFI_D_INFO
, " BaseAddress - 0x%lx Length - 0x%lx MinimalMemorySizeNeeded - 0x%lx\n", BaseAddress
, Length
, MinimalMemorySizeNeeded
));
2286 // If no memory regions are found that are big enough to initialize the DXE core, then ASSERT().
2288 ASSERT (Length
>= MinimalMemorySizeNeeded
);
2291 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2293 if ((Attributes
& EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) == EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) {
2294 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeMoreReliable
, Attributes
);
2296 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeSystemMemory
, Attributes
);
2300 // Declare the very first memory region, so the EFI Memory Services are available.
2302 CoreAddMemoryDescriptor (
2303 EfiConventionalMemory
,
2305 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2309 *MemoryBaseAddress
= BaseAddress
;
2310 *MemoryLength
= Length
;
2317 External function. Initializes the GCD and memory services based on the memory
2318 descriptor HOBs. This function is responsible for priming the GCD map and the
2319 memory map, so memory allocations and resource allocations can be made. The
2320 HobStart will be relocated to a pool buffer.
2322 @param HobStart The start address of the HOB
2323 @param MemoryBaseAddress Start address of memory region found to init DXE
2325 @param MemoryLength Length of memory region found to init DXE core.
2327 @retval EFI_SUCCESS GCD services successfully initialized.
2331 CoreInitializeGcdServices (
2332 IN OUT VOID
**HobStart
,
2333 IN EFI_PHYSICAL_ADDRESS MemoryBaseAddress
,
2334 IN UINT64 MemoryLength
2337 EFI_PEI_HOB_POINTERS Hob
;
2339 EFI_HOB_HANDOFF_INFO_TABLE
*PhitHob
;
2340 UINT8 SizeOfMemorySpace
;
2341 UINT8 SizeOfIoSpace
;
2342 EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
;
2343 EFI_PHYSICAL_ADDRESS BaseAddress
;
2346 EFI_GCD_MAP_ENTRY
*Entry
;
2347 EFI_GCD_MEMORY_TYPE GcdMemoryType
;
2348 EFI_GCD_IO_TYPE GcdIoType
;
2349 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
2350 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
2351 EFI_HOB_FIRMWARE_VOLUME
*FirmwareVolumeHob
;
2352 UINTN NumberOfDescriptors
;
2353 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
2355 UINT64 Capabilities
;
2356 EFI_HOB_CPU
* CpuHob
;
2357 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMapHobList
;
2360 // Cache the PHIT HOB for later use
2362 PhitHob
= (EFI_HOB_HANDOFF_INFO_TABLE
*)(*HobStart
);
2365 // Get the number of address lines in the I/O and Memory space for the CPU
2367 CpuHob
= GetFirstHob (EFI_HOB_TYPE_CPU
);
2368 ASSERT (CpuHob
!= NULL
);
2369 SizeOfMemorySpace
= CpuHob
->SizeOfMemorySpace
;
2370 SizeOfIoSpace
= CpuHob
->SizeOfIoSpace
;
2373 // Initialize the GCD Memory Space Map
2375 Entry
= AllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY
), &mGcdMemorySpaceMapEntryTemplate
);
2376 ASSERT (Entry
!= NULL
);
2378 Entry
->EndAddress
= LShiftU64 (1, SizeOfMemorySpace
) - 1;
2380 InsertHeadList (&mGcdMemorySpaceMap
, &Entry
->Link
);
2382 CoreDumpGcdMemorySpaceMap (TRUE
);
2385 // Initialize the GCD I/O Space Map
2387 Entry
= AllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY
), &mGcdIoSpaceMapEntryTemplate
);
2388 ASSERT (Entry
!= NULL
);
2390 Entry
->EndAddress
= LShiftU64 (1, SizeOfIoSpace
) - 1;
2392 InsertHeadList (&mGcdIoSpaceMap
, &Entry
->Link
);
2394 CoreDumpGcdIoSpaceMap (TRUE
);
2397 // Walk the HOB list and add all resource descriptors to the GCD
2399 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2401 GcdMemoryType
= EfiGcdMemoryTypeNonExistent
;
2402 GcdIoType
= EfiGcdIoTypeNonExistent
;
2404 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2406 ResourceHob
= Hob
.ResourceDescriptor
;
2408 switch (ResourceHob
->ResourceType
) {
2409 case EFI_RESOURCE_SYSTEM_MEMORY
:
2410 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == TESTED_MEMORY_ATTRIBUTES
) {
2411 if ((ResourceHob
->ResourceAttribute
& EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) == EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) {
2412 GcdMemoryType
= EfiGcdMemoryTypeMoreReliable
;
2414 GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
2417 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == INITIALIZED_MEMORY_ATTRIBUTES
) {
2418 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2420 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == PRESENT_MEMORY_ATTRIBUTES
) {
2421 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2423 if ((ResourceHob
->ResourceAttribute
& EFI_RESOURCE_ATTRIBUTE_PERSISTENT
) == EFI_RESOURCE_ATTRIBUTE_PERSISTENT
) {
2424 GcdMemoryType
= EfiGcdMemoryTypePersistent
;
2427 case EFI_RESOURCE_MEMORY_MAPPED_IO
:
2428 case EFI_RESOURCE_FIRMWARE_DEVICE
:
2429 GcdMemoryType
= EfiGcdMemoryTypeMemoryMappedIo
;
2431 case EFI_RESOURCE_MEMORY_MAPPED_IO_PORT
:
2432 case EFI_RESOURCE_MEMORY_RESERVED
:
2433 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2435 case EFI_RESOURCE_IO
:
2436 GcdIoType
= EfiGcdIoTypeIo
;
2438 case EFI_RESOURCE_IO_RESERVED
:
2439 GcdIoType
= EfiGcdIoTypeReserved
;
2443 if (GcdMemoryType
!= EfiGcdMemoryTypeNonExistent
) {
2445 // Validate the Resource HOB Attributes
2447 CoreValidateResourceDescriptorHobAttributes (ResourceHob
->ResourceAttribute
);
2450 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2452 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (
2454 ResourceHob
->ResourceAttribute
2457 Status
= CoreInternalAddMemorySpace (
2459 ResourceHob
->PhysicalStart
,
2460 ResourceHob
->ResourceLength
,
2465 if (GcdIoType
!= EfiGcdIoTypeNonExistent
) {
2466 Status
= CoreAddIoSpace (
2468 ResourceHob
->PhysicalStart
,
2469 ResourceHob
->ResourceLength
2476 // Allocate first memory region from the GCD by the DXE core
2478 Status
= CoreGetMemorySpaceDescriptor (MemoryBaseAddress
, &Descriptor
);
2479 if (!EFI_ERROR (Status
)) {
2480 ASSERT ((Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) ||
2481 (Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
));
2482 Status
= CoreAllocateMemorySpace (
2483 EfiGcdAllocateAddress
,
2484 Descriptor
.GcdMemoryType
,
2488 gDxeCoreImageHandle
,
2494 // Walk the HOB list and allocate all memory space that is consumed by memory allocation HOBs,
2495 // and Firmware Volume HOBs. Also update the EFI Memory Map with the memory allocation HOBs.
2497 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2498 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
2499 MemoryHob
= Hob
.MemoryAllocation
;
2500 BaseAddress
= MemoryHob
->AllocDescriptor
.MemoryBaseAddress
;
2501 Status
= CoreGetMemorySpaceDescriptor (BaseAddress
, &Descriptor
);
2502 if (!EFI_ERROR (Status
)) {
2503 Status
= CoreAllocateMemorySpace (
2504 EfiGcdAllocateAddress
,
2505 Descriptor
.GcdMemoryType
,
2507 MemoryHob
->AllocDescriptor
.MemoryLength
,
2509 gDxeCoreImageHandle
,
2512 if (!EFI_ERROR (Status
) &&
2513 ((Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) ||
2514 (Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
))) {
2515 CoreAddMemoryDescriptor (
2516 MemoryHob
->AllocDescriptor
.MemoryType
,
2517 MemoryHob
->AllocDescriptor
.MemoryBaseAddress
,
2518 RShiftU64 (MemoryHob
->AllocDescriptor
.MemoryLength
, EFI_PAGE_SHIFT
),
2519 Descriptor
.Capabilities
& (~EFI_MEMORY_RUNTIME
)
2525 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_FV
) {
2526 FirmwareVolumeHob
= Hob
.FirmwareVolume
;
2527 BaseAddress
= FirmwareVolumeHob
->BaseAddress
;
2528 Status
= CoreAllocateMemorySpace (
2529 EfiGcdAllocateAddress
,
2530 EfiGcdMemoryTypeMemoryMappedIo
,
2532 FirmwareVolumeHob
->Length
,
2534 gDxeCoreImageHandle
,
2541 // Add and allocate the remaining unallocated system memory to the memory services.
2543 Status
= CoreGetMemorySpaceMap (&NumberOfDescriptors
, &MemorySpaceMap
);
2544 ASSERT (Status
== EFI_SUCCESS
);
2546 MemorySpaceMapHobList
= NULL
;
2547 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
2548 if ((MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) ||
2549 (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
)) {
2550 if (MemorySpaceMap
[Index
].ImageHandle
== NULL
) {
2551 BaseAddress
= PageAlignAddress (MemorySpaceMap
[Index
].BaseAddress
);
2552 Length
= PageAlignLength (MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- BaseAddress
);
2553 if (Length
== 0 || MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
< BaseAddress
) {
2556 if (((UINTN
) MemorySpaceMap
[Index
].BaseAddress
<= (UINTN
) (*HobStart
)) &&
2557 ((UINTN
) (MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) >= (UINTN
) PhitHob
->EfiFreeMemoryBottom
)) {
2559 // Skip the memory space that covers HOB List, it should be processed
2560 // after HOB List relocation to avoid the resources allocated by others
2561 // to corrupt HOB List before its relocation.
2563 MemorySpaceMapHobList
= &MemorySpaceMap
[Index
];
2566 CoreAddMemoryDescriptor (
2567 EfiConventionalMemory
,
2569 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2570 MemorySpaceMap
[Index
].Capabilities
& (~EFI_MEMORY_RUNTIME
)
2572 Status
= CoreAllocateMemorySpace (
2573 EfiGcdAllocateAddress
,
2574 MemorySpaceMap
[Index
].GcdMemoryType
,
2578 gDxeCoreImageHandle
,
2586 // Relocate HOB List to an allocated pool buffer.
2587 // The relocation should be at after all the tested memory resources added
2588 // (except the memory space that covers HOB List) to the memory services,
2589 // because the memory resource found in CoreInitializeMemoryServices()
2590 // may have not enough remaining resource for HOB List.
2592 NewHobList
= AllocateCopyPool (
2593 (UINTN
) PhitHob
->EfiFreeMemoryBottom
- (UINTN
) (*HobStart
),
2596 ASSERT (NewHobList
!= NULL
);
2598 *HobStart
= NewHobList
;
2599 gHobList
= NewHobList
;
2601 if (MemorySpaceMapHobList
!= NULL
) {
2603 // Add and allocate the memory space that covers HOB List to the memory services
2604 // after HOB List relocation.
2606 BaseAddress
= PageAlignAddress (MemorySpaceMapHobList
->BaseAddress
);
2607 Length
= PageAlignLength (MemorySpaceMapHobList
->BaseAddress
+ MemorySpaceMapHobList
->Length
- BaseAddress
);
2608 CoreAddMemoryDescriptor (
2609 EfiConventionalMemory
,
2611 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2612 MemorySpaceMapHobList
->Capabilities
& (~EFI_MEMORY_RUNTIME
)
2614 Status
= CoreAllocateMemorySpace (
2615 EfiGcdAllocateAddress
,
2616 MemorySpaceMapHobList
->GcdMemoryType
,
2620 gDxeCoreImageHandle
,
2625 CoreFreePool (MemorySpaceMap
);