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 | \
54 EFI_LOCK mGcdMemorySpaceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
55 EFI_LOCK mGcdIoSpaceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
56 LIST_ENTRY mGcdMemorySpaceMap
= INITIALIZE_LIST_HEAD_VARIABLE (mGcdMemorySpaceMap
);
57 LIST_ENTRY mGcdIoSpaceMap
= INITIALIZE_LIST_HEAD_VARIABLE (mGcdIoSpaceMap
);
59 EFI_GCD_MAP_ENTRY mGcdMemorySpaceMapEntryTemplate
= {
60 EFI_GCD_MAP_SIGNATURE
,
69 EfiGcdMemoryTypeNonExistent
,
75 EFI_GCD_MAP_ENTRY mGcdIoSpaceMapEntryTemplate
= {
76 EFI_GCD_MAP_SIGNATURE
,
85 (EFI_GCD_MEMORY_TYPE
) 0,
86 EfiGcdIoTypeNonExistent
,
91 GCD_ATTRIBUTE_CONVERSION_ENTRY mAttributeConversionTable
[] = {
92 { EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
, EFI_MEMORY_UC
, TRUE
},
93 { EFI_RESOURCE_ATTRIBUTE_UNCACHED_EXPORTED
, EFI_MEMORY_UCE
, TRUE
},
94 { EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
, EFI_MEMORY_WC
, TRUE
},
95 { EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
, EFI_MEMORY_WT
, TRUE
},
96 { EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
, EFI_MEMORY_WB
, TRUE
},
97 { EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE
, EFI_MEMORY_RP
, TRUE
},
98 { EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE
, EFI_MEMORY_WP
, TRUE
},
99 { EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE
, EFI_MEMORY_XP
, TRUE
},
100 { EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE
, EFI_MEMORY_RO
, TRUE
},
101 { EFI_RESOURCE_ATTRIBUTE_PRESENT
, EFI_MEMORY_PRESENT
, FALSE
},
102 { EFI_RESOURCE_ATTRIBUTE_INITIALIZED
, EFI_MEMORY_INITIALIZED
, FALSE
},
103 { EFI_RESOURCE_ATTRIBUTE_TESTED
, EFI_MEMORY_TESTED
, FALSE
},
104 { EFI_RESOURCE_ATTRIBUTE_PERSISTABLE
, EFI_MEMORY_NV
, TRUE
},
105 { EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
, EFI_MEMORY_MORE_RELIABLE
, TRUE
},
110 /// Lookup table used to print GCD Memory Space Map
112 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mGcdMemoryTypeNames
[] = {
113 "NonExist ", // EfiGcdMemoryTypeNonExistent
114 "Reserved ", // EfiGcdMemoryTypeReserved
115 "SystemMem", // EfiGcdMemoryTypeSystemMemory
116 "MMIO ", // EfiGcdMemoryTypeMemoryMappedIo
117 "PersisMem", // EfiGcdMemoryTypePersistent
118 "MoreRelia", // EfiGcdMemoryTypeMoreReliable
119 "Unknown " // EfiGcdMemoryTypeMaximum
123 /// Lookup table used to print GCD I/O Space Map
125 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mGcdIoTypeNames
[] = {
126 "NonExist", // EfiGcdIoTypeNonExistent
127 "Reserved", // EfiGcdIoTypeReserved
128 "I/O ", // EfiGcdIoTypeIo
129 "Unknown " // EfiGcdIoTypeMaximum
133 /// Lookup table used to print GCD Allocation Types
135 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8
*mGcdAllocationTypeNames
[] = {
136 "AnySearchBottomUp ", // EfiGcdAllocateAnySearchBottomUp
137 "MaxAddressSearchBottomUp ", // EfiGcdAllocateMaxAddressSearchBottomUp
138 "AtAddress ", // EfiGcdAllocateAddress
139 "AnySearchTopDown ", // EfiGcdAllocateAnySearchTopDown
140 "MaxAddressSearchTopDown ", // EfiGcdAllocateMaxAddressSearchTopDown
141 "Unknown " // EfiGcdMaxAllocateType
145 Dump the entire contents if the GCD Memory Space Map using DEBUG() macros when
146 PcdDebugPrintErrorLevel has the DEBUG_GCD bit set.
148 @param InitialMap TRUE if the initial GCD Memory Map is being dumped. Otherwise, FALSE.
153 CoreDumpGcdMemorySpaceMap (
159 UINTN NumberOfDescriptors
;
160 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
163 Status
= CoreGetMemorySpaceMap (&NumberOfDescriptors
, &MemorySpaceMap
);
164 ASSERT (Status
== EFI_SUCCESS
&& MemorySpaceMap
!= NULL
);
167 DEBUG ((DEBUG_GCD
, "GCD:Initial GCD Memory Space Map\n"));
169 DEBUG ((DEBUG_GCD
, "GCDMemType Range Capabilities Attributes \n"));
170 DEBUG ((DEBUG_GCD
, "========== ================================= ================ ================\n"));
171 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
172 DEBUG ((DEBUG_GCD
, "%a %016lx-%016lx %016lx %016lx%c\n",
173 mGcdMemoryTypeNames
[MIN (MemorySpaceMap
[Index
].GcdMemoryType
, EfiGcdMemoryTypeMaximum
)],
174 MemorySpaceMap
[Index
].BaseAddress
,
175 MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- 1,
176 MemorySpaceMap
[Index
].Capabilities
,
177 MemorySpaceMap
[Index
].Attributes
,
178 MemorySpaceMap
[Index
].ImageHandle
== NULL
? ' ' : '*'
181 DEBUG ((DEBUG_GCD
, "\n"));
182 FreePool (MemorySpaceMap
);
187 Dump the entire contents if the GCD I/O Space Map using DEBUG() macros when
188 PcdDebugPrintErrorLevel has the DEBUG_GCD bit set.
190 @param InitialMap TRUE if the initial GCD I/O Map is being dumped. Otherwise, FALSE.
195 CoreDumpGcdIoSpaceMap (
201 UINTN NumberOfDescriptors
;
202 EFI_GCD_IO_SPACE_DESCRIPTOR
*IoSpaceMap
;
205 Status
= CoreGetIoSpaceMap (&NumberOfDescriptors
, &IoSpaceMap
);
206 ASSERT (Status
== EFI_SUCCESS
&& IoSpaceMap
!= NULL
);
209 DEBUG ((DEBUG_GCD
, "GCD:Initial GCD I/O Space Map\n"));
212 DEBUG ((DEBUG_GCD
, "GCDIoType Range \n"));
213 DEBUG ((DEBUG_GCD
, "========== =================================\n"));
214 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
215 DEBUG ((DEBUG_GCD
, "%a %016lx-%016lx%c\n",
216 mGcdIoTypeNames
[MIN (IoSpaceMap
[Index
].GcdIoType
, EfiGcdIoTypeMaximum
)],
217 IoSpaceMap
[Index
].BaseAddress
,
218 IoSpaceMap
[Index
].BaseAddress
+ IoSpaceMap
[Index
].Length
- 1,
219 IoSpaceMap
[Index
].ImageHandle
== NULL
? ' ' : '*'
222 DEBUG ((DEBUG_GCD
, "\n"));
223 FreePool (IoSpaceMap
);
228 Validate resource descriptor HOB's attributes.
230 If Attributes includes some memory resource's settings, it should include
231 the corresponding capabilites also.
233 @param Attributes Resource descriptor HOB attributes.
237 CoreValidateResourceDescriptorHobAttributes (
241 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED
) == 0) ||
242 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE
) != 0));
243 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED
) == 0) ||
244 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE
) != 0));
245 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED
) == 0) ||
246 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE
) != 0));
247 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED
) == 0) ||
248 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE
) != 0));
249 ASSERT (((Attributes
& EFI_RESOURCE_ATTRIBUTE_PERSISTENT
) == 0) ||
250 ((Attributes
& EFI_RESOURCE_ATTRIBUTE_PERSISTABLE
) != 0));
254 Acquire memory lock on mGcdMemorySpaceLock.
258 CoreAcquireGcdMemoryLock (
262 CoreAcquireLock (&mGcdMemorySpaceLock
);
268 Release memory lock on mGcdMemorySpaceLock.
272 CoreReleaseGcdMemoryLock (
276 CoreReleaseLock (&mGcdMemorySpaceLock
);
282 Acquire memory lock on mGcdIoSpaceLock.
286 CoreAcquireGcdIoLock (
290 CoreAcquireLock (&mGcdIoSpaceLock
);
295 Release memory lock on mGcdIoSpaceLock.
299 CoreReleaseGcdIoLock (
303 CoreReleaseLock (&mGcdIoSpaceLock
);
309 // GCD Initialization Worker Functions
312 Aligns a value to the specified boundary.
314 @param Value 64 bit value to align
315 @param Alignment Log base 2 of the boundary to align Value to
316 @param RoundUp TRUE if Value is to be rounded up to the nearest
317 aligned boundary. FALSE is Value is to be
318 rounded down to the nearest aligned boundary.
320 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
330 UINT64 AlignmentMask
;
332 AlignmentMask
= LShiftU64 (1, Alignment
) - 1;
334 Value
+= AlignmentMask
;
336 return Value
& (~AlignmentMask
);
341 Aligns address to the page boundary.
343 @param Value 64 bit address to align
345 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
353 return AlignValue (Value
, EFI_PAGE_SHIFT
, TRUE
);
358 Aligns length to the page boundary.
360 @param Value 64 bit length to align
362 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
370 return AlignValue (Value
, EFI_PAGE_SHIFT
, FALSE
);
374 // GCD Memory Space Worker Functions
378 Allocate pool for two entries.
380 @param TopEntry An entry of GCD map
381 @param BottomEntry An entry of GCD map
383 @retval EFI_OUT_OF_RESOURCES No enough buffer to be allocated.
384 @retval EFI_SUCCESS Both entries successfully allocated.
388 CoreAllocateGcdMapEntry (
389 IN OUT EFI_GCD_MAP_ENTRY
**TopEntry
,
390 IN OUT EFI_GCD_MAP_ENTRY
**BottomEntry
394 // Set to mOnGuarding to TRUE before memory allocation. This will make sure
395 // that the entry memory is not "guarded" by HeapGuard. Otherwise it might
396 // cause problem when it's freed (if HeapGuard is enabled).
399 *TopEntry
= AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY
));
401 if (*TopEntry
== NULL
) {
402 return EFI_OUT_OF_RESOURCES
;
406 *BottomEntry
= AllocateZeroPool (sizeof (EFI_GCD_MAP_ENTRY
));
408 if (*BottomEntry
== NULL
) {
409 CoreFreePool (*TopEntry
);
410 return EFI_OUT_OF_RESOURCES
;
418 Internal function. Inserts a new descriptor into a sorted list
420 @param Link The linked list to insert the range BaseAddress
422 @param Entry A pointer to the entry that is inserted
423 @param BaseAddress The base address of the new range
424 @param Length The length of the new range in bytes
425 @param TopEntry Top pad entry to insert if needed.
426 @param BottomEntry Bottom pad entry to insert if needed.
428 @retval EFI_SUCCESS The new range was inserted into the linked list
432 CoreInsertGcdMapEntry (
434 IN EFI_GCD_MAP_ENTRY
*Entry
,
435 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
437 IN EFI_GCD_MAP_ENTRY
*TopEntry
,
438 IN EFI_GCD_MAP_ENTRY
*BottomEntry
441 ASSERT (Length
!= 0);
443 if (BaseAddress
> Entry
->BaseAddress
) {
444 ASSERT (BottomEntry
->Signature
== 0);
446 CopyMem (BottomEntry
, Entry
, sizeof (EFI_GCD_MAP_ENTRY
));
447 Entry
->BaseAddress
= BaseAddress
;
448 BottomEntry
->EndAddress
= BaseAddress
- 1;
449 InsertTailList (Link
, &BottomEntry
->Link
);
452 if ((BaseAddress
+ Length
- 1) < Entry
->EndAddress
) {
453 ASSERT (TopEntry
->Signature
== 0);
455 CopyMem (TopEntry
, Entry
, sizeof (EFI_GCD_MAP_ENTRY
));
456 TopEntry
->BaseAddress
= BaseAddress
+ Length
;
457 Entry
->EndAddress
= BaseAddress
+ Length
- 1;
458 InsertHeadList (Link
, &TopEntry
->Link
);
466 Merge the Gcd region specified by Link and its adjacent entry.
468 @param Link Specify the entry to be merged (with its
470 @param Forward Direction (forward or backward).
473 @retval EFI_SUCCESS Successfully returned.
474 @retval EFI_UNSUPPORTED These adjacent regions could not merge.
478 CoreMergeGcdMapEntry (
484 LIST_ENTRY
*AdjacentLink
;
485 EFI_GCD_MAP_ENTRY
*Entry
;
486 EFI_GCD_MAP_ENTRY
*AdjacentEntry
;
489 // Get adjacent entry
492 AdjacentLink
= Link
->ForwardLink
;
494 AdjacentLink
= Link
->BackLink
;
498 // If AdjacentLink is the head of the list, then no merge can be performed
500 if (AdjacentLink
== Map
) {
504 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
505 AdjacentEntry
= CR (AdjacentLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
507 if (Entry
->Capabilities
!= AdjacentEntry
->Capabilities
) {
508 return EFI_UNSUPPORTED
;
510 if (Entry
->Attributes
!= AdjacentEntry
->Attributes
) {
511 return EFI_UNSUPPORTED
;
513 if (Entry
->GcdMemoryType
!= AdjacentEntry
->GcdMemoryType
) {
514 return EFI_UNSUPPORTED
;
516 if (Entry
->GcdIoType
!= AdjacentEntry
->GcdIoType
) {
517 return EFI_UNSUPPORTED
;
519 if (Entry
->ImageHandle
!= AdjacentEntry
->ImageHandle
) {
520 return EFI_UNSUPPORTED
;
522 if (Entry
->DeviceHandle
!= AdjacentEntry
->DeviceHandle
) {
523 return EFI_UNSUPPORTED
;
527 Entry
->EndAddress
= AdjacentEntry
->EndAddress
;
529 Entry
->BaseAddress
= AdjacentEntry
->BaseAddress
;
531 RemoveEntryList (AdjacentLink
);
532 CoreFreePool (AdjacentEntry
);
539 Merge adjacent entries on total chain.
541 @param TopEntry Top entry of GCD map.
542 @param BottomEntry Bottom entry of GCD map.
543 @param StartLink Start link of the list for this loop.
544 @param EndLink End link of the list for this loop.
547 @retval EFI_SUCCESS GCD map successfully cleaned up.
551 CoreCleanupGcdMapEntry (
552 IN EFI_GCD_MAP_ENTRY
*TopEntry
,
553 IN EFI_GCD_MAP_ENTRY
*BottomEntry
,
554 IN LIST_ENTRY
*StartLink
,
555 IN LIST_ENTRY
*EndLink
,
561 if (TopEntry
->Signature
== 0) {
562 CoreFreePool (TopEntry
);
564 if (BottomEntry
->Signature
== 0) {
565 CoreFreePool (BottomEntry
);
569 while (Link
!= EndLink
->ForwardLink
) {
570 CoreMergeGcdMapEntry (Link
, FALSE
, Map
);
571 Link
= Link
->ForwardLink
;
573 CoreMergeGcdMapEntry (EndLink
, TRUE
, Map
);
580 Search a segment of memory space in GCD map. The result is a range of GCD entry list.
582 @param BaseAddress The start address of the segment.
583 @param Length The length of the segment.
584 @param StartLink The first GCD entry involves this segment of
586 @param EndLink The first GCD entry involves this segment of
588 @param Map Points to the start entry to search.
590 @retval EFI_SUCCESS Successfully found the entry.
591 @retval EFI_NOT_FOUND Not found.
595 CoreSearchGcdMapEntry (
596 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
598 OUT LIST_ENTRY
**StartLink
,
599 OUT LIST_ENTRY
**EndLink
,
604 EFI_GCD_MAP_ENTRY
*Entry
;
606 ASSERT (Length
!= 0);
611 Link
= Map
->ForwardLink
;
612 while (Link
!= Map
) {
613 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
614 if (BaseAddress
>= Entry
->BaseAddress
&& BaseAddress
<= Entry
->EndAddress
) {
617 if (*StartLink
!= NULL
) {
618 if ((BaseAddress
+ Length
- 1) >= Entry
->BaseAddress
&&
619 (BaseAddress
+ Length
- 1) <= Entry
->EndAddress
) {
624 Link
= Link
->ForwardLink
;
627 return EFI_NOT_FOUND
;
632 Count the amount of GCD map entries.
634 @param Map Points to the start entry to do the count loop.
640 CoreCountGcdMapEntry (
648 Link
= Map
->ForwardLink
;
649 while (Link
!= Map
) {
651 Link
= Link
->ForwardLink
;
660 Return the memory attribute specified by Attributes
662 @param Attributes A num with some attribute bits on.
664 @return The enum value of memory attribute.
668 ConverToCpuArchAttributes (
672 UINT64 CpuArchAttributes
;
674 CpuArchAttributes
= Attributes
& NONEXCLUSIVE_MEMORY_ATTRIBUTES
;
676 if ( (Attributes
& EFI_MEMORY_UC
) == EFI_MEMORY_UC
) {
677 CpuArchAttributes
|= EFI_MEMORY_UC
;
678 } else if ( (Attributes
& EFI_MEMORY_WC
) == EFI_MEMORY_WC
) {
679 CpuArchAttributes
|= EFI_MEMORY_WC
;
680 } else if ( (Attributes
& EFI_MEMORY_WT
) == EFI_MEMORY_WT
) {
681 CpuArchAttributes
|= EFI_MEMORY_WT
;
682 } else if ( (Attributes
& EFI_MEMORY_WB
) == EFI_MEMORY_WB
) {
683 CpuArchAttributes
|= EFI_MEMORY_WB
;
684 } else if ( (Attributes
& EFI_MEMORY_UCE
) == EFI_MEMORY_UCE
) {
685 CpuArchAttributes
|= EFI_MEMORY_UCE
;
686 } else if ( (Attributes
& EFI_MEMORY_WP
) == EFI_MEMORY_WP
) {
687 CpuArchAttributes
|= EFI_MEMORY_WP
;
690 return CpuArchAttributes
;
695 Do operation on a segment of memory space specified (add, free, remove, change attribute ...).
697 @param Operation The type of the operation
698 @param GcdMemoryType Additional information for the operation
699 @param GcdIoType Additional information for the operation
700 @param BaseAddress Start address of the segment
701 @param Length length of the segment
702 @param Capabilities The alterable attributes of a newly added entry
703 @param Attributes The attributes needs to be set
705 @retval EFI_INVALID_PARAMETER Length is 0 or address (length) not aligned when
707 @retval EFI_SUCCESS Action successfully done.
708 @retval EFI_UNSUPPORTED Could not find the proper descriptor on this
709 segment or set an upsupported attribute.
710 @retval EFI_ACCESS_DENIED Operate on an space non-exist or is used for an
712 @retval EFI_NOT_FOUND Free a non-using space or remove a non-exist
714 @retval EFI_OUT_OF_RESOURCES No buffer could be allocated.
715 @retval EFI_NOT_AVAILABLE_YET The attributes cannot be set because CPU architectural protocol
716 is not available yet.
721 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
722 IN EFI_GCD_IO_TYPE GcdIoType
,
723 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
725 IN UINT64 Capabilities
,
732 EFI_GCD_MAP_ENTRY
*Entry
;
733 EFI_GCD_MAP_ENTRY
*TopEntry
;
734 EFI_GCD_MAP_ENTRY
*BottomEntry
;
735 LIST_ENTRY
*StartLink
;
737 UINT64 CpuArchAttributes
;
740 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
741 return EFI_INVALID_PARAMETER
;
745 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
746 CoreAcquireGcdMemoryLock ();
747 Map
= &mGcdMemorySpaceMap
;
748 } else if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
749 CoreAcquireGcdIoLock ();
750 Map
= &mGcdIoSpaceMap
;
756 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
758 Status
= CoreSearchGcdMapEntry (BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
759 if (EFI_ERROR (Status
)) {
760 Status
= EFI_UNSUPPORTED
;
764 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
767 // Verify that the list of descriptors are unallocated non-existent memory.
770 while (Link
!= EndLink
->ForwardLink
) {
771 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
776 case GCD_ADD_MEMORY_OPERATION
:
777 if (Entry
->GcdMemoryType
!= EfiGcdMemoryTypeNonExistent
||
778 Entry
->ImageHandle
!= NULL
) {
779 Status
= EFI_ACCESS_DENIED
;
783 case GCD_ADD_IO_OPERATION
:
784 if (Entry
->GcdIoType
!= EfiGcdIoTypeNonExistent
||
785 Entry
->ImageHandle
!= NULL
) {
786 Status
= EFI_ACCESS_DENIED
;
793 case GCD_FREE_MEMORY_OPERATION
:
794 case GCD_FREE_IO_OPERATION
:
795 if (Entry
->ImageHandle
== NULL
) {
796 Status
= EFI_NOT_FOUND
;
803 case GCD_REMOVE_MEMORY_OPERATION
:
804 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
805 Status
= EFI_NOT_FOUND
;
808 if (Entry
->ImageHandle
!= NULL
) {
809 Status
= EFI_ACCESS_DENIED
;
813 case GCD_REMOVE_IO_OPERATION
:
814 if (Entry
->GcdIoType
== EfiGcdIoTypeNonExistent
) {
815 Status
= EFI_NOT_FOUND
;
818 if (Entry
->ImageHandle
!= NULL
) {
819 Status
= EFI_ACCESS_DENIED
;
824 // Set attributes operation
826 case GCD_SET_ATTRIBUTES_MEMORY_OPERATION
:
827 if ((Attributes
& EFI_MEMORY_RUNTIME
) != 0) {
828 if ((BaseAddress
& EFI_PAGE_MASK
) != 0 || (Length
& EFI_PAGE_MASK
) != 0) {
829 Status
= EFI_INVALID_PARAMETER
;
833 if ((Entry
->Capabilities
& Attributes
) != Attributes
) {
834 Status
= EFI_UNSUPPORTED
;
839 // Set capabilities operation
841 case GCD_SET_CAPABILITIES_MEMORY_OPERATION
:
842 if ((BaseAddress
& EFI_PAGE_MASK
) != 0 || (Length
& EFI_PAGE_MASK
) != 0) {
843 Status
= EFI_INVALID_PARAMETER
;
848 // Current attributes must still be supported with new capabilities
850 if ((Capabilities
& Entry
->Attributes
) != Entry
->Attributes
) {
851 Status
= EFI_UNSUPPORTED
;
856 Link
= Link
->ForwardLink
;
860 // Allocate work space to perform this operation
862 Status
= CoreAllocateGcdMapEntry (&TopEntry
, &BottomEntry
);
863 if (EFI_ERROR (Status
)) {
864 Status
= EFI_OUT_OF_RESOURCES
;
867 ASSERT (TopEntry
!= NULL
&& BottomEntry
!= NULL
);
869 if (Operation
== GCD_SET_ATTRIBUTES_MEMORY_OPERATION
) {
871 // Call CPU Arch Protocol to attempt to set attributes on the range
873 CpuArchAttributes
= ConverToCpuArchAttributes (Attributes
);
875 // CPU arch attributes include page attributes and cache attributes.
876 // Only page attributes supports to be cleared, but not cache attributes.
877 // Caller is expected to use GetMemorySpaceDescriptor() to get the current
878 // attributes, AND/OR attributes, and then calls SetMemorySpaceAttributes()
879 // to set the new attributes.
880 // So 0 CPU arch attributes should not happen as memory should always have
881 // a cache attribute (no matter UC or WB, etc).
883 // Here, 0 CPU arch attributes will be filtered to be compatible with the
884 // case that caller just calls SetMemorySpaceAttributes() with none CPU
885 // arch attributes (for example, RUNTIME) as the purpose of the case is not
886 // to clear CPU arch attributes.
888 if (CpuArchAttributes
!= 0) {
890 Status
= EFI_NOT_AVAILABLE_YET
;
892 Status
= gCpu
->SetMemoryAttributes (
899 if (EFI_ERROR (Status
)) {
900 CoreFreePool (TopEntry
);
901 CoreFreePool (BottomEntry
);
908 // Convert/Insert the list of descriptors from StartLink to EndLink
911 while (Link
!= EndLink
->ForwardLink
) {
912 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
913 CoreInsertGcdMapEntry (Link
, Entry
, BaseAddress
, Length
, TopEntry
, BottomEntry
);
918 case GCD_ADD_MEMORY_OPERATION
:
919 Entry
->GcdMemoryType
= GcdMemoryType
;
920 if (GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
921 Entry
->Capabilities
= Capabilities
| EFI_MEMORY_RUNTIME
| EFI_MEMORY_PORT_IO
;
923 Entry
->Capabilities
= Capabilities
| EFI_MEMORY_RUNTIME
;
926 case GCD_ADD_IO_OPERATION
:
927 Entry
->GcdIoType
= GcdIoType
;
932 case GCD_FREE_MEMORY_OPERATION
:
933 case GCD_FREE_IO_OPERATION
:
934 Entry
->ImageHandle
= NULL
;
935 Entry
->DeviceHandle
= NULL
;
940 case GCD_REMOVE_MEMORY_OPERATION
:
941 Entry
->GcdMemoryType
= EfiGcdMemoryTypeNonExistent
;
942 Entry
->Capabilities
= 0;
944 case GCD_REMOVE_IO_OPERATION
:
945 Entry
->GcdIoType
= EfiGcdIoTypeNonExistent
;
948 // Set attributes operation
950 case GCD_SET_ATTRIBUTES_MEMORY_OPERATION
:
951 if (CpuArchAttributes
== 0) {
953 // Keep original CPU arch attributes when caller just calls
954 // SetMemorySpaceAttributes() with none CPU arch attributes (for example, RUNTIME).
956 Attributes
|= (Entry
->Attributes
& (EXCLUSIVE_MEMORY_ATTRIBUTES
| NONEXCLUSIVE_MEMORY_ATTRIBUTES
));
958 Entry
->Attributes
= Attributes
;
961 // Set capabilities operation
963 case GCD_SET_CAPABILITIES_MEMORY_OPERATION
:
964 Entry
->Capabilities
= Capabilities
;
967 Link
= Link
->ForwardLink
;
973 Status
= CoreCleanupGcdMapEntry (TopEntry
, BottomEntry
, StartLink
, EndLink
, Map
);
976 DEBUG ((DEBUG_GCD
, " Status = %r\n", Status
));
978 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
979 CoreReleaseGcdMemoryLock ();
980 CoreDumpGcdMemorySpaceMap (FALSE
);
982 if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
983 CoreReleaseGcdIoLock ();
984 CoreDumpGcdIoSpaceMap (FALSE
);
992 Check whether an entry could be used to allocate space.
994 @param Operation Allocate memory or IO
995 @param Entry The entry to be tested
996 @param GcdMemoryType The desired memory type
997 @param GcdIoType The desired IO type
999 @retval EFI_NOT_FOUND The memory type does not match or there's an
1000 image handle on the entry.
1001 @retval EFI_UNSUPPORTED The operation unsupported.
1002 @retval EFI_SUCCESS It's ok for this entry to be used to allocate
1007 CoreAllocateSpaceCheckEntry (
1009 IN EFI_GCD_MAP_ENTRY
*Entry
,
1010 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1011 IN EFI_GCD_IO_TYPE GcdIoType
1014 if (Entry
->ImageHandle
!= NULL
) {
1015 return EFI_NOT_FOUND
;
1017 switch (Operation
) {
1018 case GCD_ALLOCATE_MEMORY_OPERATION
:
1019 if (Entry
->GcdMemoryType
!= GcdMemoryType
) {
1020 return EFI_NOT_FOUND
;
1023 case GCD_ALLOCATE_IO_OPERATION
:
1024 if (Entry
->GcdIoType
!= GcdIoType
) {
1025 return EFI_NOT_FOUND
;
1029 return EFI_UNSUPPORTED
;
1036 Allocate space on specified address and length.
1038 @param Operation The type of operation (memory or IO)
1039 @param GcdAllocateType The type of allocate operation
1040 @param GcdMemoryType The desired memory type
1041 @param GcdIoType The desired IO type
1042 @param Alignment Align with 2^Alignment
1043 @param Length Length to allocate
1044 @param BaseAddress Base address to allocate
1045 @param ImageHandle The image handle consume the allocated space.
1046 @param DeviceHandle The device handle consume the allocated space.
1048 @retval EFI_INVALID_PARAMETER Invalid parameter.
1049 @retval EFI_NOT_FOUND No descriptor for the desired space exists.
1050 @retval EFI_SUCCESS Space successfully allocated.
1056 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1057 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1058 IN EFI_GCD_IO_TYPE GcdIoType
,
1061 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1062 IN EFI_HANDLE ImageHandle
,
1063 IN EFI_HANDLE DeviceHandle OPTIONAL
1067 EFI_PHYSICAL_ADDRESS AlignmentMask
;
1068 EFI_PHYSICAL_ADDRESS MaxAddress
;
1071 LIST_ENTRY
*SubLink
;
1072 EFI_GCD_MAP_ENTRY
*Entry
;
1073 EFI_GCD_MAP_ENTRY
*TopEntry
;
1074 EFI_GCD_MAP_ENTRY
*BottomEntry
;
1075 LIST_ENTRY
*StartLink
;
1076 LIST_ENTRY
*EndLink
;
1080 // Make sure parameters are valid
1082 if ((UINT32
)GcdAllocateType
>= EfiGcdMaxAllocateType
) {
1083 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1084 return EFI_INVALID_PARAMETER
;
1086 if ((UINT32
)GcdMemoryType
>= EfiGcdMemoryTypeMaximum
) {
1087 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1088 return EFI_INVALID_PARAMETER
;
1090 if ((UINT32
)GcdIoType
>= EfiGcdIoTypeMaximum
) {
1091 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1092 return EFI_INVALID_PARAMETER
;
1094 if (BaseAddress
== NULL
) {
1095 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1096 return EFI_INVALID_PARAMETER
;
1098 if (ImageHandle
== NULL
) {
1099 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1100 return EFI_INVALID_PARAMETER
;
1102 if (Alignment
>= 64) {
1103 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_NOT_FOUND
));
1104 return EFI_NOT_FOUND
;
1107 DEBUG ((DEBUG_GCD
, " Status = %r\n", EFI_INVALID_PARAMETER
));
1108 return EFI_INVALID_PARAMETER
;
1112 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
1113 CoreAcquireGcdMemoryLock ();
1114 Map
= &mGcdMemorySpaceMap
;
1115 } else if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
1116 CoreAcquireGcdIoLock ();
1117 Map
= &mGcdIoSpaceMap
;
1126 // Compute alignment bit mask
1128 AlignmentMask
= LShiftU64 (1, Alignment
) - 1;
1130 if (GcdAllocateType
== EfiGcdAllocateAddress
) {
1132 // Verify that the BaseAddress passed in is aligned correctly
1134 if ((*BaseAddress
& AlignmentMask
) != 0) {
1135 Status
= EFI_NOT_FOUND
;
1140 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
1142 Status
= CoreSearchGcdMapEntry (*BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
1143 if (EFI_ERROR (Status
)) {
1144 Status
= EFI_NOT_FOUND
;
1147 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1150 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1153 while (Link
!= EndLink
->ForwardLink
) {
1154 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1155 Link
= Link
->ForwardLink
;
1156 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1157 if (EFI_ERROR (Status
)) {
1164 Entry
= CR (Map
->BackLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1167 // Compute the maximum address to use in the search algorithm
1169 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchBottomUp
||
1170 GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
) {
1171 MaxAddress
= *BaseAddress
;
1173 MaxAddress
= Entry
->EndAddress
;
1177 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1179 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
1180 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
1181 Link
= Map
->BackLink
;
1183 Link
= Map
->ForwardLink
;
1185 while (Link
!= Map
) {
1186 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1188 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
1189 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
1190 Link
= Link
->BackLink
;
1192 Link
= Link
->ForwardLink
;
1195 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1196 if (EFI_ERROR (Status
)) {
1200 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
1201 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
1202 if ((Entry
->BaseAddress
+ Length
) > MaxAddress
) {
1205 if (Length
> (Entry
->EndAddress
+ 1)) {
1206 Status
= EFI_NOT_FOUND
;
1209 if (Entry
->EndAddress
> MaxAddress
) {
1210 *BaseAddress
= MaxAddress
;
1212 *BaseAddress
= Entry
->EndAddress
;
1214 *BaseAddress
= (*BaseAddress
+ 1 - Length
) & (~AlignmentMask
);
1216 *BaseAddress
= (Entry
->BaseAddress
+ AlignmentMask
) & (~AlignmentMask
);
1217 if ((*BaseAddress
+ Length
- 1) > MaxAddress
) {
1218 Status
= EFI_NOT_FOUND
;
1224 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
1226 Status
= CoreSearchGcdMapEntry (*BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
1227 if (EFI_ERROR (Status
)) {
1228 Status
= EFI_NOT_FOUND
;
1231 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1235 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1238 SubLink
= StartLink
;
1239 while (SubLink
!= EndLink
->ForwardLink
) {
1240 Entry
= CR (SubLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1241 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1242 if (EFI_ERROR (Status
)) {
1247 SubLink
= SubLink
->ForwardLink
;
1255 Status
= EFI_NOT_FOUND
;
1260 // Allocate work space to perform this operation
1262 Status
= CoreAllocateGcdMapEntry (&TopEntry
, &BottomEntry
);
1263 if (EFI_ERROR (Status
)) {
1264 Status
= EFI_OUT_OF_RESOURCES
;
1267 ASSERT (TopEntry
!= NULL
&& BottomEntry
!= NULL
);
1270 // Convert/Insert the list of descriptors from StartLink to EndLink
1273 while (Link
!= EndLink
->ForwardLink
) {
1274 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1275 CoreInsertGcdMapEntry (Link
, Entry
, *BaseAddress
, Length
, TopEntry
, BottomEntry
);
1276 Entry
->ImageHandle
= ImageHandle
;
1277 Entry
->DeviceHandle
= DeviceHandle
;
1278 Link
= Link
->ForwardLink
;
1284 Status
= CoreCleanupGcdMapEntry (TopEntry
, BottomEntry
, StartLink
, EndLink
, Map
);
1287 DEBUG ((DEBUG_GCD
, " Status = %r", Status
));
1288 if (!EFI_ERROR (Status
)) {
1289 DEBUG ((DEBUG_GCD
, " (BaseAddress = %016lx)", *BaseAddress
));
1291 DEBUG ((DEBUG_GCD
, "\n"));
1293 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
1294 CoreReleaseGcdMemoryLock ();
1295 CoreDumpGcdMemorySpaceMap (FALSE
);
1297 if ((Operation
& GCD_IO_SPACE_OPERATION
) !=0) {
1298 CoreReleaseGcdIoLock ();
1299 CoreDumpGcdIoSpaceMap (FALSE
);
1307 Add a segment of memory to GCD map.
1309 @param GcdMemoryType Memory type of the segment.
1310 @param BaseAddress Base address of the segment.
1311 @param Length Length of the segment.
1312 @param Capabilities alterable attributes of the segment.
1314 @retval EFI_INVALID_PARAMETER Invalid parameters.
1315 @retval EFI_SUCCESS Successfully add a segment of memory space.
1319 CoreInternalAddMemorySpace (
1320 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1321 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1323 IN UINT64 Capabilities
1326 DEBUG ((DEBUG_GCD
, "GCD:AddMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1327 DEBUG ((DEBUG_GCD
, " GcdMemoryType = %a\n", mGcdMemoryTypeNames
[MIN (GcdMemoryType
, EfiGcdMemoryTypeMaximum
)]));
1328 DEBUG ((DEBUG_GCD
, " Capabilities = %016lx\n", Capabilities
));
1331 // Make sure parameters are valid
1333 if (GcdMemoryType
<= EfiGcdMemoryTypeNonExistent
|| GcdMemoryType
>= EfiGcdMemoryTypeMaximum
) {
1334 return EFI_INVALID_PARAMETER
;
1337 return CoreConvertSpace (GCD_ADD_MEMORY_OPERATION
, GcdMemoryType
, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, Capabilities
, 0);
1341 // GCD Core Services
1345 Allocates nonexistent memory, reserved memory, system memory, or memorymapped
1346 I/O resources from the global coherency domain of the processor.
1348 @param GcdAllocateType The type of allocate operation
1349 @param GcdMemoryType The desired memory type
1350 @param Alignment Align with 2^Alignment
1351 @param Length Length to allocate
1352 @param BaseAddress Base address to allocate
1353 @param ImageHandle The image handle consume the allocated space.
1354 @param DeviceHandle The device handle consume the allocated space.
1356 @retval EFI_INVALID_PARAMETER Invalid parameter.
1357 @retval EFI_NOT_FOUND No descriptor contains the desired space.
1358 @retval EFI_SUCCESS Memory space successfully allocated.
1363 CoreAllocateMemorySpace (
1364 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1365 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1368 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1369 IN EFI_HANDLE ImageHandle
,
1370 IN EFI_HANDLE DeviceHandle OPTIONAL
1373 if (BaseAddress
!= NULL
) {
1374 DEBUG ((DEBUG_GCD
, "GCD:AllocateMemorySpace(Base=%016lx,Length=%016lx)\n", *BaseAddress
, Length
));
1376 DEBUG ((DEBUG_GCD
, "GCD:AllocateMemorySpace(Base=<NULL>,Length=%016lx)\n", Length
));
1378 DEBUG ((DEBUG_GCD
, " GcdAllocateType = %a\n", mGcdAllocationTypeNames
[MIN (GcdAllocateType
, EfiGcdMaxAllocateType
)]));
1379 DEBUG ((DEBUG_GCD
, " GcdMemoryType = %a\n", mGcdMemoryTypeNames
[MIN (GcdMemoryType
, EfiGcdMemoryTypeMaximum
)]));
1380 DEBUG ((DEBUG_GCD
, " Alignment = %016lx\n", LShiftU64 (1, Alignment
)));
1381 DEBUG ((DEBUG_GCD
, " ImageHandle = %p\n", ImageHandle
));
1382 DEBUG ((DEBUG_GCD
, " DeviceHandle = %p\n", DeviceHandle
));
1384 return CoreAllocateSpace (
1385 GCD_ALLOCATE_MEMORY_OPERATION
,
1388 (EFI_GCD_IO_TYPE
) 0,
1399 Adds reserved memory, system memory, or memory-mapped I/O resources to the
1400 global coherency domain of the processor.
1402 @param GcdMemoryType Memory type of the memory space.
1403 @param BaseAddress Base address of the memory space.
1404 @param Length Length of the memory space.
1405 @param Capabilities alterable attributes of the memory space.
1407 @retval EFI_SUCCESS Merged this memory space into GCD map.
1412 CoreAddMemorySpace (
1413 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1414 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1416 IN UINT64 Capabilities
1420 EFI_PHYSICAL_ADDRESS PageBaseAddress
;
1423 Status
= CoreInternalAddMemorySpace (GcdMemoryType
, BaseAddress
, Length
, Capabilities
);
1425 if (!EFI_ERROR (Status
) && ((GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) || (GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
))) {
1427 PageBaseAddress
= PageAlignAddress (BaseAddress
);
1428 PageLength
= PageAlignLength (BaseAddress
+ Length
- PageBaseAddress
);
1430 Status
= CoreAllocateMemorySpace (
1431 EfiGcdAllocateAddress
,
1436 gDxeCoreImageHandle
,
1440 if (!EFI_ERROR (Status
)) {
1441 CoreAddMemoryDescriptor (
1442 EfiConventionalMemory
,
1444 RShiftU64 (PageLength
, EFI_PAGE_SHIFT
),
1448 for (; PageLength
!= 0; PageLength
-= EFI_PAGE_SIZE
, PageBaseAddress
+= EFI_PAGE_SIZE
) {
1449 Status
= CoreAllocateMemorySpace (
1450 EfiGcdAllocateAddress
,
1455 gDxeCoreImageHandle
,
1459 if (!EFI_ERROR (Status
)) {
1460 CoreAddMemoryDescriptor (
1461 EfiConventionalMemory
,
1475 Frees nonexistent memory, reserved memory, system memory, or memory-mapped
1476 I/O resources from the global coherency domain of the processor.
1478 @param BaseAddress Base address of the memory space.
1479 @param Length Length of the memory space.
1481 @retval EFI_SUCCESS Space successfully freed.
1486 CoreFreeMemorySpace (
1487 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1491 DEBUG ((DEBUG_GCD
, "GCD:FreeMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1493 return CoreConvertSpace (GCD_FREE_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1498 Removes reserved memory, system memory, or memory-mapped I/O resources from
1499 the global coherency domain of the processor.
1501 @param BaseAddress Base address of the memory space.
1502 @param Length Length of the memory space.
1504 @retval EFI_SUCCESS Successfully remove a segment of memory space.
1509 CoreRemoveMemorySpace (
1510 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1514 DEBUG ((DEBUG_GCD
, "GCD:RemoveMemorySpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1516 return CoreConvertSpace (GCD_REMOVE_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1521 Build a memory descriptor according to an entry.
1523 @param Descriptor The descriptor to be built
1524 @param Entry According to this entry
1528 BuildMemoryDescriptor (
1529 IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
,
1530 IN EFI_GCD_MAP_ENTRY
*Entry
1533 Descriptor
->BaseAddress
= Entry
->BaseAddress
;
1534 Descriptor
->Length
= Entry
->EndAddress
- Entry
->BaseAddress
+ 1;
1535 Descriptor
->Capabilities
= Entry
->Capabilities
;
1536 Descriptor
->Attributes
= Entry
->Attributes
;
1537 Descriptor
->GcdMemoryType
= Entry
->GcdMemoryType
;
1538 Descriptor
->ImageHandle
= Entry
->ImageHandle
;
1539 Descriptor
->DeviceHandle
= Entry
->DeviceHandle
;
1544 Retrieves the descriptor for a memory region containing a specified address.
1546 @param BaseAddress Specified start address
1547 @param Descriptor Specified length
1549 @retval EFI_INVALID_PARAMETER Invalid parameter
1550 @retval EFI_SUCCESS Successfully get memory space descriptor.
1555 CoreGetMemorySpaceDescriptor (
1556 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1557 OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
1561 LIST_ENTRY
*StartLink
;
1562 LIST_ENTRY
*EndLink
;
1563 EFI_GCD_MAP_ENTRY
*Entry
;
1566 // Make sure parameters are valid
1568 if (Descriptor
== NULL
) {
1569 return EFI_INVALID_PARAMETER
;
1572 CoreAcquireGcdMemoryLock ();
1575 // Search for the list of descriptors that contain BaseAddress
1577 Status
= CoreSearchGcdMapEntry (BaseAddress
, 1, &StartLink
, &EndLink
, &mGcdMemorySpaceMap
);
1578 if (EFI_ERROR (Status
)) {
1579 Status
= EFI_NOT_FOUND
;
1581 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1583 // Copy the contents of the found descriptor into Descriptor
1585 Entry
= CR (StartLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1586 BuildMemoryDescriptor (Descriptor
, Entry
);
1589 CoreReleaseGcdMemoryLock ();
1596 Modifies the attributes for a memory region in the global coherency domain of the
1599 @param BaseAddress Specified start address
1600 @param Length Specified length
1601 @param Attributes Specified attributes
1603 @retval EFI_SUCCESS The attributes were set for the memory region.
1604 @retval EFI_INVALID_PARAMETER Length is zero.
1605 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
1606 resource range specified by BaseAddress and Length.
1607 @retval EFI_UNSUPPORTED The bit mask of attributes is not support for the memory resource
1608 range specified by BaseAddress and Length.
1609 @retval EFI_ACCESS_DEFINED The attributes for the memory resource range specified by
1610 BaseAddress and Length cannot be modified.
1611 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
1612 the memory resource range.
1613 @retval EFI_NOT_AVAILABLE_YET The attributes cannot be set because CPU architectural protocol is
1619 CoreSetMemorySpaceAttributes (
1620 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1622 IN UINT64 Attributes
1625 DEBUG ((DEBUG_GCD
, "GCD:SetMemorySpaceAttributes(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1626 DEBUG ((DEBUG_GCD
, " Attributes = %016lx\n", Attributes
));
1628 return CoreConvertSpace (GCD_SET_ATTRIBUTES_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, Attributes
);
1633 Modifies the capabilities for a memory region in the global coherency domain of the
1636 @param BaseAddress The physical address that is the start address of a memory region.
1637 @param Length The size in bytes of the memory region.
1638 @param Capabilities The bit mask of capabilities that the memory region supports.
1640 @retval EFI_SUCCESS The capabilities were set for the memory region.
1641 @retval EFI_INVALID_PARAMETER Length is zero.
1642 @retval EFI_UNSUPPORTED The capabilities specified by Capabilities do not include the
1643 memory region attributes currently in use.
1644 @retval EFI_ACCESS_DENIED The capabilities for the memory resource range specified by
1645 BaseAddress and Length cannot be modified.
1646 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the capabilities
1647 of the memory resource range.
1651 CoreSetMemorySpaceCapabilities (
1652 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1654 IN UINT64 Capabilities
1659 DEBUG ((DEBUG_GCD
, "GCD:CoreSetMemorySpaceCapabilities(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1660 DEBUG ((DEBUG_GCD
, " Capabilities = %016lx\n", Capabilities
));
1662 Status
= CoreConvertSpace (GCD_SET_CAPABILITIES_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, Capabilities
, 0);
1663 if (!EFI_ERROR(Status
)) {
1664 CoreUpdateMemoryAttributes(BaseAddress
, RShiftU64(Length
, EFI_PAGE_SHIFT
), Capabilities
& (~EFI_MEMORY_RUNTIME
));
1672 Returns a map of the memory resources in the global coherency domain of the
1675 @param NumberOfDescriptors Number of descriptors.
1676 @param MemorySpaceMap Descriptor array
1678 @retval EFI_INVALID_PARAMETER Invalid parameter
1679 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1680 @retval EFI_SUCCESS Successfully get memory space map.
1685 CoreGetMemorySpaceMap (
1686 OUT UINTN
*NumberOfDescriptors
,
1687 OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
**MemorySpaceMap
1692 EFI_GCD_MAP_ENTRY
*Entry
;
1693 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
;
1696 // Make sure parameters are valid
1698 if (NumberOfDescriptors
== NULL
) {
1699 return EFI_INVALID_PARAMETER
;
1701 if (MemorySpaceMap
== NULL
) {
1702 return EFI_INVALID_PARAMETER
;
1705 CoreAcquireGcdMemoryLock ();
1708 // Count the number of descriptors
1710 *NumberOfDescriptors
= CoreCountGcdMapEntry (&mGcdMemorySpaceMap
);
1713 // Allocate the MemorySpaceMap
1715 *MemorySpaceMap
= AllocatePool (*NumberOfDescriptors
* sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR
));
1716 if (*MemorySpaceMap
== NULL
) {
1717 Status
= EFI_OUT_OF_RESOURCES
;
1722 // Fill in the MemorySpaceMap
1724 Descriptor
= *MemorySpaceMap
;
1725 Link
= mGcdMemorySpaceMap
.ForwardLink
;
1726 while (Link
!= &mGcdMemorySpaceMap
) {
1727 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1728 BuildMemoryDescriptor (Descriptor
, Entry
);
1730 Link
= Link
->ForwardLink
;
1732 Status
= EFI_SUCCESS
;
1735 CoreReleaseGcdMemoryLock ();
1741 Adds reserved I/O or I/O resources to the global coherency domain of the processor.
1743 @param GcdIoType IO type of the segment.
1744 @param BaseAddress Base address of the segment.
1745 @param Length Length of the segment.
1747 @retval EFI_SUCCESS Merged this segment into GCD map.
1748 @retval EFI_INVALID_PARAMETER Parameter not valid
1754 IN EFI_GCD_IO_TYPE GcdIoType
,
1755 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1759 DEBUG ((DEBUG_GCD
, "GCD:AddIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1760 DEBUG ((DEBUG_GCD
, " GcdIoType = %a\n", mGcdIoTypeNames
[MIN (GcdIoType
, EfiGcdIoTypeMaximum
)]));
1763 // Make sure parameters are valid
1765 if (GcdIoType
<= EfiGcdIoTypeNonExistent
|| GcdIoType
>= EfiGcdIoTypeMaximum
) {
1766 return EFI_INVALID_PARAMETER
;
1768 return CoreConvertSpace (GCD_ADD_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, GcdIoType
, BaseAddress
, Length
, 0, 0);
1773 Allocates nonexistent I/O, reserved I/O, or I/O resources from the global coherency
1774 domain of the processor.
1776 @param GcdAllocateType The type of allocate operation
1777 @param GcdIoType The desired IO type
1778 @param Alignment Align with 2^Alignment
1779 @param Length Length to allocate
1780 @param BaseAddress Base address to allocate
1781 @param ImageHandle The image handle consume the allocated space.
1782 @param DeviceHandle The device handle consume the allocated space.
1784 @retval EFI_INVALID_PARAMETER Invalid parameter.
1785 @retval EFI_NOT_FOUND No descriptor contains the desired space.
1786 @retval EFI_SUCCESS IO space successfully allocated.
1791 CoreAllocateIoSpace (
1792 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1793 IN EFI_GCD_IO_TYPE GcdIoType
,
1796 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1797 IN EFI_HANDLE ImageHandle
,
1798 IN EFI_HANDLE DeviceHandle OPTIONAL
1801 if (BaseAddress
!= NULL
) {
1802 DEBUG ((DEBUG_GCD
, "GCD:AllocateIoSpace(Base=%016lx,Length=%016lx)\n", *BaseAddress
, Length
));
1804 DEBUG ((DEBUG_GCD
, "GCD:AllocateIoSpace(Base=<NULL>,Length=%016lx)\n", Length
));
1806 DEBUG ((DEBUG_GCD
, " GcdAllocateType = %a\n", mGcdAllocationTypeNames
[MIN (GcdAllocateType
, EfiGcdMaxAllocateType
)]));
1807 DEBUG ((DEBUG_GCD
, " GcdIoType = %a\n", mGcdIoTypeNames
[MIN (GcdIoType
, EfiGcdIoTypeMaximum
)]));
1808 DEBUG ((DEBUG_GCD
, " Alignment = %016lx\n", LShiftU64 (1, Alignment
)));
1809 DEBUG ((DEBUG_GCD
, " ImageHandle = %p\n", ImageHandle
));
1810 DEBUG ((DEBUG_GCD
, " DeviceHandle = %p\n", DeviceHandle
));
1812 return CoreAllocateSpace (
1813 GCD_ALLOCATE_IO_OPERATION
,
1815 (EFI_GCD_MEMORY_TYPE
) 0,
1827 Frees nonexistent I/O, reserved I/O, or I/O resources from the global coherency
1828 domain of the processor.
1830 @param BaseAddress Base address of the segment.
1831 @param Length Length of the segment.
1833 @retval EFI_SUCCESS Space successfully freed.
1839 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1843 DEBUG ((DEBUG_GCD
, "GCD:FreeIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1845 return CoreConvertSpace (GCD_FREE_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1850 Removes reserved I/O or I/O resources from the global coherency domain of the
1853 @param BaseAddress Base address of the segment.
1854 @param Length Length of the segment.
1856 @retval EFI_SUCCESS Successfully removed a segment of IO space.
1862 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1866 DEBUG ((DEBUG_GCD
, "GCD:RemoveIoSpace(Base=%016lx,Length=%016lx)\n", BaseAddress
, Length
));
1868 return CoreConvertSpace (GCD_REMOVE_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1873 Build a IO descriptor according to an entry.
1875 @param Descriptor The descriptor to be built
1876 @param Entry According to this entry
1881 IN EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
,
1882 IN EFI_GCD_MAP_ENTRY
*Entry
1885 Descriptor
->BaseAddress
= Entry
->BaseAddress
;
1886 Descriptor
->Length
= Entry
->EndAddress
- Entry
->BaseAddress
+ 1;
1887 Descriptor
->GcdIoType
= Entry
->GcdIoType
;
1888 Descriptor
->ImageHandle
= Entry
->ImageHandle
;
1889 Descriptor
->DeviceHandle
= Entry
->DeviceHandle
;
1894 Retrieves the descriptor for an I/O region containing a specified address.
1896 @param BaseAddress Specified start address
1897 @param Descriptor Specified length
1899 @retval EFI_INVALID_PARAMETER Descriptor is NULL.
1900 @retval EFI_SUCCESS Successfully get the IO space descriptor.
1905 CoreGetIoSpaceDescriptor (
1906 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1907 OUT EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
1911 LIST_ENTRY
*StartLink
;
1912 LIST_ENTRY
*EndLink
;
1913 EFI_GCD_MAP_ENTRY
*Entry
;
1916 // Make sure parameters are valid
1918 if (Descriptor
== NULL
) {
1919 return EFI_INVALID_PARAMETER
;
1922 CoreAcquireGcdIoLock ();
1925 // Search for the list of descriptors that contain BaseAddress
1927 Status
= CoreSearchGcdMapEntry (BaseAddress
, 1, &StartLink
, &EndLink
, &mGcdIoSpaceMap
);
1928 if (EFI_ERROR (Status
)) {
1929 Status
= EFI_NOT_FOUND
;
1931 ASSERT (StartLink
!= NULL
&& EndLink
!= NULL
);
1933 // Copy the contents of the found descriptor into Descriptor
1935 Entry
= CR (StartLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1936 BuildIoDescriptor (Descriptor
, Entry
);
1939 CoreReleaseGcdIoLock ();
1946 Returns a map of the I/O resources in the global coherency domain of the processor.
1948 @param NumberOfDescriptors Number of descriptors.
1949 @param IoSpaceMap Descriptor array
1951 @retval EFI_INVALID_PARAMETER Invalid parameter
1952 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1953 @retval EFI_SUCCESS Successfully get IO space map.
1959 OUT UINTN
*NumberOfDescriptors
,
1960 OUT EFI_GCD_IO_SPACE_DESCRIPTOR
**IoSpaceMap
1965 EFI_GCD_MAP_ENTRY
*Entry
;
1966 EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
;
1969 // Make sure parameters are valid
1971 if (NumberOfDescriptors
== NULL
) {
1972 return EFI_INVALID_PARAMETER
;
1974 if (IoSpaceMap
== NULL
) {
1975 return EFI_INVALID_PARAMETER
;
1978 CoreAcquireGcdIoLock ();
1981 // Count the number of descriptors
1983 *NumberOfDescriptors
= CoreCountGcdMapEntry (&mGcdIoSpaceMap
);
1986 // Allocate the IoSpaceMap
1988 *IoSpaceMap
= AllocatePool (*NumberOfDescriptors
* sizeof (EFI_GCD_IO_SPACE_DESCRIPTOR
));
1989 if (*IoSpaceMap
== NULL
) {
1990 Status
= EFI_OUT_OF_RESOURCES
;
1995 // Fill in the IoSpaceMap
1997 Descriptor
= *IoSpaceMap
;
1998 Link
= mGcdIoSpaceMap
.ForwardLink
;
1999 while (Link
!= &mGcdIoSpaceMap
) {
2000 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
2001 BuildIoDescriptor (Descriptor
, Entry
);
2003 Link
= Link
->ForwardLink
;
2005 Status
= EFI_SUCCESS
;
2008 CoreReleaseGcdIoLock ();
2014 Converts a Resource Descriptor HOB attributes mask to an EFI Memory Descriptor
2017 @param GcdMemoryType Type of resource in the GCD memory map.
2018 @param Attributes The attribute mask in the Resource Descriptor
2021 @return The capabilities mask for an EFI Memory Descriptor.
2025 CoreConvertResourceDescriptorHobAttributesToCapabilities (
2026 EFI_GCD_MEMORY_TYPE GcdMemoryType
,
2030 UINT64 Capabilities
;
2031 GCD_ATTRIBUTE_CONVERSION_ENTRY
*Conversion
;
2034 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2036 for (Capabilities
= 0, Conversion
= mAttributeConversionTable
; Conversion
->Attribute
!= 0; Conversion
++) {
2037 if (Conversion
->Memory
|| ((GcdMemoryType
!= EfiGcdMemoryTypeSystemMemory
) && (GcdMemoryType
!= EfiGcdMemoryTypeMoreReliable
))) {
2038 if (Attributes
& Conversion
->Attribute
) {
2039 Capabilities
|= Conversion
->Capability
;
2044 return Capabilities
;
2048 Calculate total memory bin size neeeded.
2050 @return The total memory bin size neeeded.
2054 CalculateTotalMemoryBinSizeNeeded (
2062 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
2065 for (Index
= 0; gMemoryTypeInformation
[Index
].Type
!= EfiMaxMemoryType
; Index
++) {
2066 TotalSize
+= LShiftU64 (gMemoryTypeInformation
[Index
].NumberOfPages
, EFI_PAGE_SHIFT
);
2073 External function. Initializes memory services based on the memory
2074 descriptor HOBs. This function is responsible for priming the memory
2075 map, so memory allocations and resource allocations can be made.
2076 The first part of this function can not depend on any memory services
2077 until at least one memory descriptor is provided to the memory services.
2079 @param HobStart The start address of the HOB.
2080 @param MemoryBaseAddress Start address of memory region found to init DXE
2082 @param MemoryLength Length of memory region found to init DXE core.
2084 @retval EFI_SUCCESS Memory services successfully initialized.
2088 CoreInitializeMemoryServices (
2090 OUT EFI_PHYSICAL_ADDRESS
*MemoryBaseAddress
,
2091 OUT UINT64
*MemoryLength
2094 EFI_PEI_HOB_POINTERS Hob
;
2095 EFI_MEMORY_TYPE_INFORMATION
*EfiMemoryTypeInformation
;
2098 EFI_HOB_HANDOFF_INFO_TABLE
*PhitHob
;
2099 EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
;
2100 EFI_HOB_RESOURCE_DESCRIPTOR
*PhitResourceHob
;
2101 EFI_PHYSICAL_ADDRESS BaseAddress
;
2104 UINT64 Capabilities
;
2105 EFI_PHYSICAL_ADDRESS TestedMemoryBaseAddress
;
2106 UINT64 TestedMemoryLength
;
2107 EFI_PHYSICAL_ADDRESS HighAddress
;
2108 EFI_HOB_GUID_TYPE
*GuidHob
;
2109 UINT32 ReservedCodePageNumber
;
2110 UINT64 MinimalMemorySizeNeeded
;
2113 // Point at the first HOB. This must be the PHIT HOB.
2115 Hob
.Raw
= *HobStart
;
2116 ASSERT (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_HANDOFF
);
2119 // Initialize the spin locks and maps in the memory services.
2120 // Also fill in the memory services into the EFI Boot Services Table
2122 CoreInitializePool ();
2125 // Initialize Local Variables
2127 PhitResourceHob
= NULL
;
2134 // Cache the PHIT HOB for later use
2136 PhitHob
= Hob
.HandoffInformationTable
;
2138 if (PcdGet64(PcdLoadModuleAtFixAddressEnable
) != 0) {
2139 ReservedCodePageNumber
= PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber
);
2140 ReservedCodePageNumber
+= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber
);
2143 // cache the Top address for loading modules at Fixed Address
2145 gLoadModuleAtFixAddressConfigurationTable
.DxeCodeTopAddress
= PhitHob
->EfiMemoryTop
2146 + EFI_PAGES_TO_SIZE(ReservedCodePageNumber
);
2149 // See if a Memory Type Information HOB is available
2151 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
2152 if (GuidHob
!= NULL
) {
2153 EfiMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
2154 DataSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
2155 if (EfiMemoryTypeInformation
!= NULL
&& DataSize
> 0 && DataSize
<= (EfiMaxMemoryType
+ 1) * sizeof (EFI_MEMORY_TYPE_INFORMATION
)) {
2156 CopyMem (&gMemoryTypeInformation
, EfiMemoryTypeInformation
, DataSize
);
2161 // Include the total memory bin size needed to make sure memory bin could be allocated successfully.
2163 MinimalMemorySizeNeeded
= MINIMUM_INITIAL_MEMORY_SIZE
+ CalculateTotalMemoryBinSizeNeeded ();
2166 // Find the Resource Descriptor HOB that contains PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop
2169 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2171 // Skip all HOBs except Resource Descriptor HOBs
2173 if (GET_HOB_TYPE (Hob
) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2178 // Skip Resource Descriptor HOBs that do not describe tested system memory
2180 ResourceHob
= Hob
.ResourceDescriptor
;
2181 if (ResourceHob
->ResourceType
!= EFI_RESOURCE_SYSTEM_MEMORY
) {
2184 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) != TESTED_MEMORY_ATTRIBUTES
) {
2189 // Skip Resource Descriptor HOBs that do not contain the PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop
2191 if (PhitHob
->EfiFreeMemoryBottom
< ResourceHob
->PhysicalStart
) {
2194 if (PhitHob
->EfiFreeMemoryTop
> (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
)) {
2199 // Cache the resource descriptor HOB for the memory region described by the PHIT HOB
2201 PhitResourceHob
= ResourceHob
;
2205 // Compute range between PHIT EfiMemoryTop and the end of the Resource Descriptor HOB
2207 Attributes
= PhitResourceHob
->ResourceAttribute
;
2208 BaseAddress
= PageAlignAddress (PhitHob
->EfiMemoryTop
);
2209 Length
= PageAlignLength (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
- BaseAddress
);
2210 if (Length
< MinimalMemorySizeNeeded
) {
2212 // If that range is not large enough to intialize the DXE Core, then
2213 // Compute range between PHIT EfiFreeMemoryBottom and PHIT EfiFreeMemoryTop
2215 BaseAddress
= PageAlignAddress (PhitHob
->EfiFreeMemoryBottom
);
2216 Length
= PageAlignLength (PhitHob
->EfiFreeMemoryTop
- BaseAddress
);
2217 if (Length
< MinimalMemorySizeNeeded
) {
2219 // If that range is not large enough to intialize the DXE Core, then
2220 // Compute range between the start of the Resource Descriptor HOB and the start of the HOB List
2222 BaseAddress
= PageAlignAddress (ResourceHob
->PhysicalStart
);
2223 Length
= PageAlignLength ((UINT64
)((UINTN
)*HobStart
- BaseAddress
));
2230 // Assert if a resource descriptor HOB for the memory region described by the PHIT was not found
2235 // Take the range in the resource descriptor HOB for the memory region described
2236 // by the PHIT as higher priority if it is big enough. It can make the memory bin
2237 // allocated to be at the same memory region with PHIT that has more better compatibility
2238 // to avoid memory fragmentation for some code practices assume and allocate <4G ACPI memory.
2240 if (Length
< MinimalMemorySizeNeeded
) {
2242 // Search all the resource descriptor HOBs from the highest possible addresses down for a memory
2243 // region that is big enough to initialize the DXE core. Always skip the PHIT Resource HOB.
2244 // The max address must be within the physically addressible range for the processor.
2246 HighAddress
= MAX_ADDRESS
;
2247 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2249 // Skip the Resource Descriptor HOB that contains the PHIT
2251 if (Hob
.ResourceDescriptor
== PhitResourceHob
) {
2255 // Skip all HOBs except Resource Descriptor HOBs
2257 if (GET_HOB_TYPE (Hob
) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2262 // Skip Resource Descriptor HOBs that do not describe tested system memory below MAX_ADDRESS
2264 ResourceHob
= Hob
.ResourceDescriptor
;
2265 if (ResourceHob
->ResourceType
!= EFI_RESOURCE_SYSTEM_MEMORY
) {
2268 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) != TESTED_MEMORY_ATTRIBUTES
) {
2271 if ((ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
) > (EFI_PHYSICAL_ADDRESS
)MAX_ADDRESS
) {
2276 // Skip Resource Descriptor HOBs that are below a previously found Resource Descriptor HOB
2278 if (HighAddress
!= (EFI_PHYSICAL_ADDRESS
)MAX_ADDRESS
&& ResourceHob
->PhysicalStart
<= HighAddress
) {
2283 // Skip Resource Descriptor HOBs that are not large enough to initilize the DXE Core
2285 TestedMemoryBaseAddress
= PageAlignAddress (ResourceHob
->PhysicalStart
);
2286 TestedMemoryLength
= PageAlignLength (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
- TestedMemoryBaseAddress
);
2287 if (TestedMemoryLength
< MinimalMemorySizeNeeded
) {
2292 // Save the range described by the Resource Descriptor that is large enough to initilize the DXE Core
2294 BaseAddress
= TestedMemoryBaseAddress
;
2295 Length
= TestedMemoryLength
;
2296 Attributes
= ResourceHob
->ResourceAttribute
;
2297 HighAddress
= ResourceHob
->PhysicalStart
;
2301 DEBUG ((EFI_D_INFO
, "CoreInitializeMemoryServices:\n"));
2302 DEBUG ((EFI_D_INFO
, " BaseAddress - 0x%lx Length - 0x%lx MinimalMemorySizeNeeded - 0x%lx\n", BaseAddress
, Length
, MinimalMemorySizeNeeded
));
2305 // If no memory regions are found that are big enough to initialize the DXE core, then ASSERT().
2307 ASSERT (Length
>= MinimalMemorySizeNeeded
);
2310 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2312 if ((Attributes
& EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) == EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) {
2313 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeMoreReliable
, Attributes
);
2315 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeSystemMemory
, Attributes
);
2319 // Declare the very first memory region, so the EFI Memory Services are available.
2321 CoreAddMemoryDescriptor (
2322 EfiConventionalMemory
,
2324 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2328 *MemoryBaseAddress
= BaseAddress
;
2329 *MemoryLength
= Length
;
2336 External function. Initializes the GCD and memory services based on the memory
2337 descriptor HOBs. This function is responsible for priming the GCD map and the
2338 memory map, so memory allocations and resource allocations can be made. The
2339 HobStart will be relocated to a pool buffer.
2341 @param HobStart The start address of the HOB
2342 @param MemoryBaseAddress Start address of memory region found to init DXE
2344 @param MemoryLength Length of memory region found to init DXE core.
2346 @retval EFI_SUCCESS GCD services successfully initialized.
2350 CoreInitializeGcdServices (
2351 IN OUT VOID
**HobStart
,
2352 IN EFI_PHYSICAL_ADDRESS MemoryBaseAddress
,
2353 IN UINT64 MemoryLength
2356 EFI_PEI_HOB_POINTERS Hob
;
2358 EFI_HOB_HANDOFF_INFO_TABLE
*PhitHob
;
2359 UINT8 SizeOfMemorySpace
;
2360 UINT8 SizeOfIoSpace
;
2361 EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
;
2362 EFI_PHYSICAL_ADDRESS BaseAddress
;
2365 EFI_GCD_MAP_ENTRY
*Entry
;
2366 EFI_GCD_MEMORY_TYPE GcdMemoryType
;
2367 EFI_GCD_IO_TYPE GcdIoType
;
2368 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
2369 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
2370 EFI_HOB_FIRMWARE_VOLUME
*FirmwareVolumeHob
;
2371 UINTN NumberOfDescriptors
;
2372 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
2374 UINT64 Capabilities
;
2375 EFI_HOB_CPU
* CpuHob
;
2376 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMapHobList
;
2379 // Cache the PHIT HOB for later use
2381 PhitHob
= (EFI_HOB_HANDOFF_INFO_TABLE
*)(*HobStart
);
2384 // Get the number of address lines in the I/O and Memory space for the CPU
2386 CpuHob
= GetFirstHob (EFI_HOB_TYPE_CPU
);
2387 ASSERT (CpuHob
!= NULL
);
2388 SizeOfMemorySpace
= CpuHob
->SizeOfMemorySpace
;
2389 SizeOfIoSpace
= CpuHob
->SizeOfIoSpace
;
2392 // Initialize the GCD Memory Space Map
2394 Entry
= AllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY
), &mGcdMemorySpaceMapEntryTemplate
);
2395 ASSERT (Entry
!= NULL
);
2397 Entry
->EndAddress
= LShiftU64 (1, SizeOfMemorySpace
) - 1;
2399 InsertHeadList (&mGcdMemorySpaceMap
, &Entry
->Link
);
2401 CoreDumpGcdMemorySpaceMap (TRUE
);
2404 // Initialize the GCD I/O Space Map
2406 Entry
= AllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY
), &mGcdIoSpaceMapEntryTemplate
);
2407 ASSERT (Entry
!= NULL
);
2409 Entry
->EndAddress
= LShiftU64 (1, SizeOfIoSpace
) - 1;
2411 InsertHeadList (&mGcdIoSpaceMap
, &Entry
->Link
);
2413 CoreDumpGcdIoSpaceMap (TRUE
);
2416 // Walk the HOB list and add all resource descriptors to the GCD
2418 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2420 GcdMemoryType
= EfiGcdMemoryTypeNonExistent
;
2421 GcdIoType
= EfiGcdIoTypeNonExistent
;
2423 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2425 ResourceHob
= Hob
.ResourceDescriptor
;
2427 switch (ResourceHob
->ResourceType
) {
2428 case EFI_RESOURCE_SYSTEM_MEMORY
:
2429 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == TESTED_MEMORY_ATTRIBUTES
) {
2430 if ((ResourceHob
->ResourceAttribute
& EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) == EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE
) {
2431 GcdMemoryType
= EfiGcdMemoryTypeMoreReliable
;
2433 GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
2436 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == INITIALIZED_MEMORY_ATTRIBUTES
) {
2437 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2439 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == PRESENT_MEMORY_ATTRIBUTES
) {
2440 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2442 if ((ResourceHob
->ResourceAttribute
& EFI_RESOURCE_ATTRIBUTE_PERSISTENT
) == EFI_RESOURCE_ATTRIBUTE_PERSISTENT
) {
2443 GcdMemoryType
= EfiGcdMemoryTypePersistent
;
2446 case EFI_RESOURCE_MEMORY_MAPPED_IO
:
2447 case EFI_RESOURCE_FIRMWARE_DEVICE
:
2448 GcdMemoryType
= EfiGcdMemoryTypeMemoryMappedIo
;
2450 case EFI_RESOURCE_MEMORY_MAPPED_IO_PORT
:
2451 case EFI_RESOURCE_MEMORY_RESERVED
:
2452 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2454 case EFI_RESOURCE_IO
:
2455 GcdIoType
= EfiGcdIoTypeIo
;
2457 case EFI_RESOURCE_IO_RESERVED
:
2458 GcdIoType
= EfiGcdIoTypeReserved
;
2462 if (GcdMemoryType
!= EfiGcdMemoryTypeNonExistent
) {
2464 // Validate the Resource HOB Attributes
2466 CoreValidateResourceDescriptorHobAttributes (ResourceHob
->ResourceAttribute
);
2469 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2471 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (
2473 ResourceHob
->ResourceAttribute
2476 Status
= CoreInternalAddMemorySpace (
2478 ResourceHob
->PhysicalStart
,
2479 ResourceHob
->ResourceLength
,
2484 if (GcdIoType
!= EfiGcdIoTypeNonExistent
) {
2485 Status
= CoreAddIoSpace (
2487 ResourceHob
->PhysicalStart
,
2488 ResourceHob
->ResourceLength
2495 // Allocate first memory region from the GCD by the DXE core
2497 Status
= CoreGetMemorySpaceDescriptor (MemoryBaseAddress
, &Descriptor
);
2498 if (!EFI_ERROR (Status
)) {
2499 ASSERT ((Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) ||
2500 (Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
));
2501 Status
= CoreAllocateMemorySpace (
2502 EfiGcdAllocateAddress
,
2503 Descriptor
.GcdMemoryType
,
2507 gDxeCoreImageHandle
,
2513 // Walk the HOB list and allocate all memory space that is consumed by memory allocation HOBs,
2514 // and Firmware Volume HOBs. Also update the EFI Memory Map with the memory allocation HOBs.
2516 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2517 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
2518 MemoryHob
= Hob
.MemoryAllocation
;
2519 BaseAddress
= MemoryHob
->AllocDescriptor
.MemoryBaseAddress
;
2520 Status
= CoreGetMemorySpaceDescriptor (BaseAddress
, &Descriptor
);
2521 if (!EFI_ERROR (Status
)) {
2522 Status
= CoreAllocateMemorySpace (
2523 EfiGcdAllocateAddress
,
2524 Descriptor
.GcdMemoryType
,
2526 MemoryHob
->AllocDescriptor
.MemoryLength
,
2528 gDxeCoreImageHandle
,
2531 if (!EFI_ERROR (Status
) &&
2532 ((Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) ||
2533 (Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
))) {
2534 CoreAddMemoryDescriptor (
2535 MemoryHob
->AllocDescriptor
.MemoryType
,
2536 MemoryHob
->AllocDescriptor
.MemoryBaseAddress
,
2537 RShiftU64 (MemoryHob
->AllocDescriptor
.MemoryLength
, EFI_PAGE_SHIFT
),
2538 Descriptor
.Capabilities
& (~EFI_MEMORY_RUNTIME
)
2544 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_FV
) {
2545 FirmwareVolumeHob
= Hob
.FirmwareVolume
;
2546 BaseAddress
= FirmwareVolumeHob
->BaseAddress
;
2547 Status
= CoreAllocateMemorySpace (
2548 EfiGcdAllocateAddress
,
2549 EfiGcdMemoryTypeMemoryMappedIo
,
2551 FirmwareVolumeHob
->Length
,
2553 gDxeCoreImageHandle
,
2560 // Add and allocate the remaining unallocated system memory to the memory services.
2562 Status
= CoreGetMemorySpaceMap (&NumberOfDescriptors
, &MemorySpaceMap
);
2563 ASSERT (Status
== EFI_SUCCESS
);
2565 MemorySpaceMapHobList
= NULL
;
2566 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
2567 if ((MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) ||
2568 (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeMoreReliable
)) {
2569 if (MemorySpaceMap
[Index
].ImageHandle
== NULL
) {
2570 BaseAddress
= PageAlignAddress (MemorySpaceMap
[Index
].BaseAddress
);
2571 Length
= PageAlignLength (MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- BaseAddress
);
2572 if (Length
== 0 || MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
< BaseAddress
) {
2575 if (((UINTN
) MemorySpaceMap
[Index
].BaseAddress
<= (UINTN
) (*HobStart
)) &&
2576 ((UINTN
) (MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
) >= (UINTN
) PhitHob
->EfiFreeMemoryBottom
)) {
2578 // Skip the memory space that covers HOB List, it should be processed
2579 // after HOB List relocation to avoid the resources allocated by others
2580 // to corrupt HOB List before its relocation.
2582 MemorySpaceMapHobList
= &MemorySpaceMap
[Index
];
2585 CoreAddMemoryDescriptor (
2586 EfiConventionalMemory
,
2588 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2589 MemorySpaceMap
[Index
].Capabilities
& (~EFI_MEMORY_RUNTIME
)
2591 Status
= CoreAllocateMemorySpace (
2592 EfiGcdAllocateAddress
,
2593 MemorySpaceMap
[Index
].GcdMemoryType
,
2597 gDxeCoreImageHandle
,
2605 // Relocate HOB List to an allocated pool buffer.
2606 // The relocation should be at after all the tested memory resources added
2607 // (except the memory space that covers HOB List) to the memory services,
2608 // because the memory resource found in CoreInitializeMemoryServices()
2609 // may have not enough remaining resource for HOB List.
2611 NewHobList
= AllocateCopyPool (
2612 (UINTN
) PhitHob
->EfiFreeMemoryBottom
- (UINTN
) (*HobStart
),
2615 ASSERT (NewHobList
!= NULL
);
2617 *HobStart
= NewHobList
;
2618 gHobList
= NewHobList
;
2620 if (MemorySpaceMapHobList
!= NULL
) {
2622 // Add and allocate the memory space that covers HOB List to the memory services
2623 // after HOB List relocation.
2625 BaseAddress
= PageAlignAddress (MemorySpaceMapHobList
->BaseAddress
);
2626 Length
= PageAlignLength (MemorySpaceMapHobList
->BaseAddress
+ MemorySpaceMapHobList
->Length
- BaseAddress
);
2627 CoreAddMemoryDescriptor (
2628 EfiConventionalMemory
,
2630 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2631 MemorySpaceMapHobList
->Capabilities
& (~EFI_MEMORY_RUNTIME
)
2633 Status
= CoreAllocateMemorySpace (
2634 EfiGcdAllocateAddress
,
2635 MemorySpaceMapHobList
->GcdMemoryType
,
2639 gDxeCoreImageHandle
,
2644 CoreFreePool (MemorySpaceMap
);