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 - 2008, Intel Corporation. <BR>
7 All rights reserved. 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 #define MINIMUM_INITIAL_MEMORY_SIZE 0x10000
21 #define MEMORY_ATTRIBUTE_MASK (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
22 EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \
23 EFI_RESOURCE_ATTRIBUTE_TESTED | \
24 EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED | \
25 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED | \
26 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED | \
27 EFI_RESOURCE_ATTRIBUTE_16_BIT_IO | \
28 EFI_RESOURCE_ATTRIBUTE_32_BIT_IO | \
29 EFI_RESOURCE_ATTRIBUTE_64_BIT_IO )
31 #define TESTED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
32 EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \
33 EFI_RESOURCE_ATTRIBUTE_TESTED )
35 #define INITIALIZED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
36 EFI_RESOURCE_ATTRIBUTE_INITIALIZED )
38 #define PRESENT_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT)
40 #define INVALID_CPU_ARCH_ATTRIBUTES 0xffffffff
45 EFI_LOCK mGcdMemorySpaceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
46 EFI_LOCK mGcdIoSpaceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY
);
47 LIST_ENTRY mGcdMemorySpaceMap
= INITIALIZE_LIST_HEAD_VARIABLE (mGcdMemorySpaceMap
);
48 LIST_ENTRY mGcdIoSpaceMap
= INITIALIZE_LIST_HEAD_VARIABLE (mGcdIoSpaceMap
);
50 EFI_GCD_MAP_ENTRY mGcdMemorySpaceMapEntryTemplate
= {
51 EFI_GCD_MAP_SIGNATURE
,
57 EfiGcdMemoryTypeNonExistent
,
63 EFI_GCD_MAP_ENTRY mGcdIoSpaceMapEntryTemplate
= {
64 EFI_GCD_MAP_SIGNATURE
,
70 (EFI_GCD_MEMORY_TYPE
) 0,
71 EfiGcdIoTypeNonExistent
,
76 GCD_ATTRIBUTE_CONVERSION_ENTRY mAttributeConversionTable
[] = {
77 { EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE
, EFI_MEMORY_UC
, TRUE
},
78 { EFI_RESOURCE_ATTRIBUTE_UNCACHED_EXPORTED
, EFI_MEMORY_UCE
, TRUE
},
79 { EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE
, EFI_MEMORY_WC
, TRUE
},
80 { EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE
, EFI_MEMORY_WT
, TRUE
},
81 { EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
, EFI_MEMORY_WB
, TRUE
},
82 { EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED
, EFI_MEMORY_RP
, TRUE
},
83 { EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED
, EFI_MEMORY_WP
, TRUE
},
84 { EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED
, EFI_MEMORY_XP
, TRUE
},
85 { EFI_RESOURCE_ATTRIBUTE_PRESENT
, EFI_MEMORY_PRESENT
, FALSE
},
86 { EFI_RESOURCE_ATTRIBUTE_INITIALIZED
, EFI_MEMORY_INITIALIZED
, FALSE
},
87 { EFI_RESOURCE_ATTRIBUTE_TESTED
, EFI_MEMORY_TESTED
, FALSE
},
93 Acquire memory lock on mGcdMemorySpaceLock.
97 CoreAcquireGcdMemoryLock (
101 CoreAcquireLock (&mGcdMemorySpaceLock
);
107 Release memory lock on mGcdMemorySpaceLock.
111 CoreReleaseGcdMemoryLock (
115 CoreReleaseLock (&mGcdMemorySpaceLock
);
121 Acquire memory lock on mGcdIoSpaceLock.
125 CoreAcquireGcdIoLock (
129 CoreAcquireLock (&mGcdIoSpaceLock
);
134 Release memory lock on mGcdIoSpaceLock.
138 CoreReleaseGcdIoLock (
142 CoreReleaseLock (&mGcdIoSpaceLock
);
148 // GCD Initialization Worker Functions
152 Aligns a value to the specified boundary.
154 @param Value 64 bit value to align
155 @param Alignment Log base 2 of the boundary to align Value to
156 @param RoundUp TRUE if Value is to be rounded up to the nearest
157 aligned boundary. FALSE is Value is to be
158 rounded down to the nearest aligned boundary.
160 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
170 UINT64 AlignmentMask
;
172 AlignmentMask
= LShiftU64 (1, Alignment
) - 1;
174 Value
+= AlignmentMask
;
176 return Value
& (~AlignmentMask
);
181 Aligns address to the page boundary.
183 @param Value 64 bit address to align
185 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
193 return AlignValue (Value
, EFI_PAGE_SHIFT
, TRUE
);
198 Aligns length to the page boundary.
200 @param Value 64 bit length to align
202 @return A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.
210 return AlignValue (Value
, EFI_PAGE_SHIFT
, FALSE
);
214 // GCD Memory Space Worker Functions
218 Allocate pool for two entries.
220 @param TopEntry An entry of GCD map
221 @param BottomEntry An entry of GCD map
223 @retval EFI_OUT_OF_RESOURCES No enough buffer to be allocated.
224 @retval EFI_SUCCESS Both entries successfully allocated.
228 CoreAllocateGcdMapEntry (
229 IN OUT EFI_GCD_MAP_ENTRY
**TopEntry
,
230 IN OUT EFI_GCD_MAP_ENTRY
**BottomEntry
233 *TopEntry
= CoreAllocateZeroBootServicesPool (sizeof (EFI_GCD_MAP_ENTRY
));
234 if (*TopEntry
== NULL
) {
235 return EFI_OUT_OF_RESOURCES
;
238 *BottomEntry
= CoreAllocateZeroBootServicesPool (sizeof (EFI_GCD_MAP_ENTRY
));
239 if (*BottomEntry
== NULL
) {
240 CoreFreePool (*TopEntry
);
241 return EFI_OUT_OF_RESOURCES
;
249 Internal function. Inserts a new descriptor into a sorted list
251 @param Link The linked list to insert the range BaseAddress
253 @param Entry A pointer to the entry that is inserted
254 @param BaseAddress The base address of the new range
255 @param Length The length of the new range in bytes
256 @param TopEntry Top pad entry to insert if needed.
257 @param BottomEntry Bottom pad entry to insert if needed.
259 @retval EFI_SUCCESS The new range was inserted into the linked list
263 CoreInsertGcdMapEntry (
265 IN EFI_GCD_MAP_ENTRY
*Entry
,
266 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
268 IN EFI_GCD_MAP_ENTRY
*TopEntry
,
269 IN EFI_GCD_MAP_ENTRY
*BottomEntry
272 ASSERT (Length
!= 0);
273 ASSERT (TopEntry
->Signature
== 0);
274 ASSERT (BottomEntry
->Signature
== 0);
276 if (BaseAddress
> Entry
->BaseAddress
) {
277 CopyMem (BottomEntry
, Entry
, sizeof (EFI_GCD_MAP_ENTRY
));
278 Entry
->BaseAddress
= BaseAddress
;
279 BottomEntry
->EndAddress
= BaseAddress
- 1;
280 InsertTailList (Link
, &BottomEntry
->Link
);
283 if ((BaseAddress
+ Length
- 1) < Entry
->EndAddress
) {
284 CopyMem (TopEntry
, Entry
, sizeof (EFI_GCD_MAP_ENTRY
));
285 TopEntry
->BaseAddress
= BaseAddress
+ Length
;
286 Entry
->EndAddress
= BaseAddress
+ Length
- 1;
287 InsertHeadList (Link
, &TopEntry
->Link
);
295 Merge the Gcd region specified by Link and its adjacent entry
297 @param Link Specify the entry to be merged (with its
299 @param Forward Direction (forward or backward).
302 @retval EFI_SUCCESS Successfully returned.
303 @retval EFI_UNSUPPORTED These adjacent regions could not merge.
307 CoreMergeGcdMapEntry (
313 LIST_ENTRY
*AdjacentLink
;
314 EFI_GCD_MAP_ENTRY
*Entry
;
315 EFI_GCD_MAP_ENTRY
*AdjacentEntry
;
318 // Get adjacent entry
321 AdjacentLink
= Link
->ForwardLink
;
323 AdjacentLink
= Link
->BackLink
;
327 // If AdjacentLink is the head of the list, then no merge can be performed
329 if (AdjacentLink
== Map
) {
333 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
334 AdjacentEntry
= CR (AdjacentLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
336 if (Entry
->Capabilities
!= AdjacentEntry
->Capabilities
) {
337 return EFI_UNSUPPORTED
;
339 if (Entry
->Attributes
!= AdjacentEntry
->Attributes
) {
340 return EFI_UNSUPPORTED
;
342 if (Entry
->GcdMemoryType
!= AdjacentEntry
->GcdMemoryType
) {
343 return EFI_UNSUPPORTED
;
345 if (Entry
->GcdIoType
!= AdjacentEntry
->GcdIoType
) {
346 return EFI_UNSUPPORTED
;
348 if (Entry
->ImageHandle
!= AdjacentEntry
->ImageHandle
) {
349 return EFI_UNSUPPORTED
;
351 if (Entry
->DeviceHandle
!= AdjacentEntry
->DeviceHandle
) {
352 return EFI_UNSUPPORTED
;
356 Entry
->EndAddress
= AdjacentEntry
->EndAddress
;
358 Entry
->BaseAddress
= AdjacentEntry
->BaseAddress
;
360 RemoveEntryList (AdjacentLink
);
361 CoreFreePool (AdjacentEntry
);
368 Merge adjacent entries on total chain.
370 @param TopEntry Top entry of GCD map.
371 @param BottomEntry Bottom entry of GCD map.
372 @param StartLink Start link of the list for this loop.
373 @param EndLink End link of the list for this loop.
376 @retval EFI_SUCCESS GCD map successfully cleaned up.
380 CoreCleanupGcdMapEntry (
381 IN EFI_GCD_MAP_ENTRY
*TopEntry
,
382 IN EFI_GCD_MAP_ENTRY
*BottomEntry
,
383 IN LIST_ENTRY
*StartLink
,
384 IN LIST_ENTRY
*EndLink
,
390 if (TopEntry
->Signature
== 0) {
391 CoreFreePool (TopEntry
);
393 if (BottomEntry
->Signature
== 0) {
394 CoreFreePool (BottomEntry
);
398 while (Link
!= EndLink
->ForwardLink
) {
399 CoreMergeGcdMapEntry (Link
, FALSE
, Map
);
400 Link
= Link
->ForwardLink
;
402 CoreMergeGcdMapEntry (EndLink
, TRUE
, Map
);
409 Search a segment of memory space in GCD map. The result is a range of GCD entry list.
411 @param BaseAddress The start address of the segment.
412 @param Length The length of the segment.
413 @param StartLink The first GCD entry involves this segment of
415 @param EndLink The first GCD entry involves this segment of
417 @param Map Points to the start entry to search.
419 @retval EFI_SUCCESS Successfully found the entry.
420 @retval EFI_NOT_FOUND Not found.
424 CoreSearchGcdMapEntry (
425 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
427 OUT LIST_ENTRY
**StartLink
,
428 OUT LIST_ENTRY
**EndLink
,
433 EFI_GCD_MAP_ENTRY
*Entry
;
435 ASSERT (Length
!= 0);
440 Link
= Map
->ForwardLink
;
441 while (Link
!= Map
) {
442 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
443 if (BaseAddress
>= Entry
->BaseAddress
&& BaseAddress
<= Entry
->EndAddress
) {
446 if (*StartLink
!= NULL
) {
447 if ((BaseAddress
+ Length
- 1) >= Entry
->BaseAddress
&&
448 (BaseAddress
+ Length
- 1) <= Entry
->EndAddress
) {
453 Link
= Link
->ForwardLink
;
455 return EFI_NOT_FOUND
;
460 Count the amount of GCD map entries.
462 @param Map Points to the start entry to do the count loop.
468 CoreCountGcdMapEntry (
476 Link
= Map
->ForwardLink
;
477 while (Link
!= Map
) {
479 Link
= Link
->ForwardLink
;
487 Return the memory attribute specified by Attributes
489 @param Attributes A num with some attribute bits on.
491 @return The enum value of memory attribute.
495 ConverToCpuArchAttributes (
499 if ( (Attributes
& EFI_MEMORY_UC
) == EFI_MEMORY_UC
) {
500 return EFI_MEMORY_UC
;
503 if ( (Attributes
& EFI_MEMORY_WC
) == EFI_MEMORY_WC
) {
504 return EFI_MEMORY_WC
;
507 if ( (Attributes
& EFI_MEMORY_WT
) == EFI_MEMORY_WT
) {
508 return EFI_MEMORY_WT
;
511 if ( (Attributes
& EFI_MEMORY_WB
) == EFI_MEMORY_WB
) {
512 return EFI_MEMORY_WB
;
515 if ( (Attributes
& EFI_MEMORY_WP
) == EFI_MEMORY_WP
) {
516 return EFI_MEMORY_WP
;
519 return INVALID_CPU_ARCH_ATTRIBUTES
;
525 Do operation on a segment of memory space specified (add, free, remove, change attribute ...).
527 @param Operation The type of the operation
528 @param GcdMemoryType Additional information for the operation
529 @param GcdIoType Additional information for the operation
530 @param BaseAddress Start address of the segment
531 @param Length length of the segment
532 @param Capabilities The alterable attributes of a newly added entry
533 @param Attributes The attributes needs to be set
535 @retval EFI_INVALID_PARAMETER Length is 0 or address (length) not aligned when
537 @retval EFI_SUCCESS Action successfully done.
538 @retval EFI_UNSUPPORTED Could not find the proper descriptor on this
539 segment or set an upsupported attribute.
540 @retval EFI_ACCESS_DENIED Operate on an space non-exist or is used for an
542 @retval EFI_NOT_FOUND Free a non-using space or remove a non-exist
544 @retval EFI_OUT_OF_RESOURCES No buffer could be allocated.
550 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
551 IN EFI_GCD_IO_TYPE GcdIoType
,
552 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
554 IN UINT64 Capabilities
,
561 EFI_GCD_MAP_ENTRY
*Entry
;
562 EFI_GCD_MAP_ENTRY
*TopEntry
;
563 EFI_GCD_MAP_ENTRY
*BottomEntry
;
564 LIST_ENTRY
*StartLink
;
567 EFI_CPU_ARCH_PROTOCOL
*CpuArch
;
568 UINT64 CpuArchAttributes
;
571 return EFI_INVALID_PARAMETER
;
575 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
576 CoreAcquireGcdMemoryLock ();
577 Map
= &mGcdMemorySpaceMap
;
579 if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
580 CoreAcquireGcdIoLock ();
581 Map
= &mGcdIoSpaceMap
;
585 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
587 Status
= CoreSearchGcdMapEntry (BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
588 if (EFI_ERROR (Status
)) {
589 Status
= EFI_UNSUPPORTED
;
595 // Verify that the list of descriptors are unallocated non-existent memory.
598 while (Link
!= EndLink
->ForwardLink
) {
599 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
604 case GCD_ADD_MEMORY_OPERATION
:
605 if (Entry
->GcdMemoryType
!= EfiGcdMemoryTypeNonExistent
||
606 Entry
->ImageHandle
!= NULL
) {
607 Status
= EFI_ACCESS_DENIED
;
611 case GCD_ADD_IO_OPERATION
:
612 if (Entry
->GcdIoType
!= EfiGcdIoTypeNonExistent
||
613 Entry
->ImageHandle
!= NULL
) {
614 Status
= EFI_ACCESS_DENIED
;
621 case GCD_FREE_MEMORY_OPERATION
:
622 case GCD_FREE_IO_OPERATION
:
623 if (Entry
->ImageHandle
== NULL
) {
624 Status
= EFI_NOT_FOUND
;
631 case GCD_REMOVE_MEMORY_OPERATION
:
632 if (Entry
->GcdMemoryType
== EfiGcdMemoryTypeNonExistent
) {
633 Status
= EFI_NOT_FOUND
;
636 if (Entry
->ImageHandle
!= NULL
) {
637 Status
= EFI_ACCESS_DENIED
;
641 case GCD_REMOVE_IO_OPERATION
:
642 if (Entry
->GcdIoType
== EfiGcdIoTypeNonExistent
) {
643 Status
= EFI_NOT_FOUND
;
646 if (Entry
->ImageHandle
!= NULL
) {
647 Status
= EFI_ACCESS_DENIED
;
652 // Set attribute operations
654 case GCD_SET_ATTRIBUTES_MEMORY_OPERATION
:
655 if ((Attributes
& EFI_MEMORY_RUNTIME
) != 0) {
656 if ((BaseAddress
& EFI_PAGE_MASK
) != 0 || (Length
& EFI_PAGE_MASK
) != 0) {
657 Status
= EFI_INVALID_PARAMETER
;
662 if ((Entry
->Capabilities
& Attributes
) != Attributes
) {
663 Status
= EFI_UNSUPPORTED
;
668 Link
= Link
->ForwardLink
;
672 // Allocate work space to perform this operation
674 Status
= CoreAllocateGcdMapEntry (&TopEntry
, &BottomEntry
);
675 if (EFI_ERROR (Status
)) {
676 Status
= EFI_OUT_OF_RESOURCES
;
683 if (Operation
== GCD_SET_ATTRIBUTES_MEMORY_OPERATION
) {
685 // Call CPU Arch Protocol to attempt to set attributes on the range
687 CpuArchAttributes
= ConverToCpuArchAttributes (Attributes
);
688 if ( CpuArchAttributes
!= INVALID_CPU_ARCH_ATTRIBUTES
) {
689 Status
= CoreLocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&CpuArch
);
690 if (EFI_ERROR (Status
)) {
691 Status
= EFI_ACCESS_DENIED
;
695 Status
= CpuArch
->SetMemoryAttributes (
701 if (EFI_ERROR (Status
)) {
709 // Convert/Insert the list of descriptors from StartLink to EndLink
712 while (Link
!= EndLink
->ForwardLink
) {
713 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
714 CoreInsertGcdMapEntry (Link
, Entry
, BaseAddress
, Length
, TopEntry
, BottomEntry
);
719 case GCD_ADD_MEMORY_OPERATION
:
720 Entry
->GcdMemoryType
= GcdMemoryType
;
721 if (GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
722 Entry
->Capabilities
= Capabilities
| EFI_MEMORY_RUNTIME
| EFI_MEMORY_PORT_IO
;
724 Entry
->Capabilities
= Capabilities
| EFI_MEMORY_RUNTIME
;
727 case GCD_ADD_IO_OPERATION
:
728 Entry
->GcdIoType
= GcdIoType
;
733 case GCD_FREE_MEMORY_OPERATION
:
734 case GCD_FREE_IO_OPERATION
:
735 Entry
->ImageHandle
= NULL
;
736 Entry
->DeviceHandle
= NULL
;
741 case GCD_REMOVE_MEMORY_OPERATION
:
742 Entry
->GcdMemoryType
= EfiGcdMemoryTypeNonExistent
;
743 Entry
->Capabilities
= 0;
745 case GCD_REMOVE_IO_OPERATION
:
746 Entry
->GcdIoType
= EfiGcdIoTypeNonExistent
;
749 // Set attribute operations
751 case GCD_SET_ATTRIBUTES_MEMORY_OPERATION
:
752 Entry
->Attributes
= Attributes
;
755 Link
= Link
->ForwardLink
;
761 Status
= CoreCleanupGcdMapEntry (TopEntry
, BottomEntry
, StartLink
, EndLink
, Map
);
764 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
765 CoreReleaseGcdMemoryLock ();
767 if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
768 CoreReleaseGcdIoLock ();
776 Check whether an entry could be used to allocate space.
778 @param Operation Allocate memory or IO
779 @param Entry The entry to be tested
780 @param GcdMemoryType The desired memory type
781 @param GcdIoType The desired IO type
783 @retval EFI_NOT_FOUND The memory type does not match or there's an
784 image handle on the entry.
785 @retval EFI_UNSUPPORTED The operation unsupported.
786 @retval EFI_SUCCESS It's ok for this entry to be used to allocate
791 CoreAllocateSpaceCheckEntry (
793 IN EFI_GCD_MAP_ENTRY
*Entry
,
794 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
795 IN EFI_GCD_IO_TYPE GcdIoType
798 if (Entry
->ImageHandle
!= NULL
) {
799 return EFI_NOT_FOUND
;
802 case GCD_ALLOCATE_MEMORY_OPERATION
:
803 if (Entry
->GcdMemoryType
!= GcdMemoryType
) {
804 return EFI_NOT_FOUND
;
807 case GCD_ALLOCATE_IO_OPERATION
:
808 if (Entry
->GcdIoType
!= GcdIoType
) {
809 return EFI_NOT_FOUND
;
813 return EFI_UNSUPPORTED
;
820 Allocate space on specified address and length.
822 @param Operation The type of operation (memory or IO)
823 @param GcdAllocateType The type of allocate operation
824 @param GcdMemoryType The desired memory type
825 @param GcdIoType The desired IO type
826 @param Alignment Align with 2^Alignment
827 @param Length Length to allocate
828 @param BaseAddress Base address to allocate
829 @param ImageHandle The image handle consume the allocated space.
830 @param DeviceHandle The device handle consume the allocated space.
832 @retval EFI_INVALID_PARAMETER Invalid parameter.
833 @retval EFI_NOT_FOUND No descriptor for the desired space exists.
834 @retval EFI_SUCCESS Space successfully allocated.
840 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
841 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
842 IN EFI_GCD_IO_TYPE GcdIoType
,
845 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
846 IN EFI_HANDLE ImageHandle
,
847 IN EFI_HANDLE DeviceHandle OPTIONAL
851 EFI_PHYSICAL_ADDRESS AlignmentMask
;
852 EFI_PHYSICAL_ADDRESS MaxAddress
;
856 EFI_GCD_MAP_ENTRY
*Entry
;
857 EFI_GCD_MAP_ENTRY
*TopEntry
;
858 EFI_GCD_MAP_ENTRY
*BottomEntry
;
859 LIST_ENTRY
*StartLink
;
864 // Make sure parameters are valid
866 if (GcdAllocateType
< 0 || GcdAllocateType
>= EfiGcdMaxAllocateType
) {
867 return EFI_INVALID_PARAMETER
;
869 if (GcdMemoryType
< 0 || GcdMemoryType
>= EfiGcdMemoryTypeMaximum
) {
870 return EFI_INVALID_PARAMETER
;
872 if (GcdIoType
< 0 || GcdIoType
>= EfiGcdIoTypeMaximum
) {
873 return EFI_INVALID_PARAMETER
;
875 if (BaseAddress
== NULL
) {
876 return EFI_INVALID_PARAMETER
;
878 if (ImageHandle
== NULL
) {
879 return EFI_INVALID_PARAMETER
;
881 if (Alignment
>= 64) {
882 return EFI_NOT_FOUND
;
885 return EFI_INVALID_PARAMETER
;
889 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
890 CoreAcquireGcdMemoryLock ();
891 Map
= &mGcdMemorySpaceMap
;
893 if ((Operation
& GCD_IO_SPACE_OPERATION
) != 0) {
894 CoreAcquireGcdIoLock ();
895 Map
= &mGcdIoSpaceMap
;
902 // Compute alignment bit mask
904 AlignmentMask
= LShiftU64 (1, Alignment
) - 1;
906 if (GcdAllocateType
== EfiGcdAllocateAddress
) {
908 // Verify that the BaseAddress passed in is aligned correctly
910 if ((*BaseAddress
& AlignmentMask
) != 0) {
911 Status
= EFI_NOT_FOUND
;
916 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
918 Status
= CoreSearchGcdMapEntry (*BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
919 if (EFI_ERROR (Status
)) {
920 Status
= EFI_NOT_FOUND
;
925 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
928 while (Link
!= EndLink
->ForwardLink
) {
929 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
930 Link
= Link
->ForwardLink
;
931 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
932 if (EFI_ERROR (Status
)) {
939 Entry
= CR (Map
->BackLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
942 // Compute the maximum address to use in the search algorithm
944 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchBottomUp
||
945 GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
) {
946 MaxAddress
= *BaseAddress
;
948 MaxAddress
= Entry
->EndAddress
;
952 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
954 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
955 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
956 Link
= Map
->BackLink
;
958 Link
= Map
->ForwardLink
;
960 while (Link
!= Map
) {
961 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
963 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
964 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
965 Link
= Link
->BackLink
;
967 Link
= Link
->ForwardLink
;
970 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
971 if (EFI_ERROR (Status
)) {
975 if (GcdAllocateType
== EfiGcdAllocateMaxAddressSearchTopDown
||
976 GcdAllocateType
== EfiGcdAllocateAnySearchTopDown
) {
977 if ((Entry
->BaseAddress
+ Length
) > MaxAddress
) {
980 if (Length
> (Entry
->EndAddress
+ 1)) {
981 Status
= EFI_NOT_FOUND
;
984 if (Entry
->EndAddress
> MaxAddress
) {
985 *BaseAddress
= MaxAddress
;
987 *BaseAddress
= Entry
->EndAddress
;
989 *BaseAddress
= (*BaseAddress
+ 1 - Length
) & (~AlignmentMask
);
991 *BaseAddress
= (Entry
->BaseAddress
+ AlignmentMask
) & (~AlignmentMask
);
992 if ((*BaseAddress
+ Length
- 1) > MaxAddress
) {
993 Status
= EFI_NOT_FOUND
;
999 // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length
1001 Status
= CoreSearchGcdMapEntry (*BaseAddress
, Length
, &StartLink
, &EndLink
, Map
);
1002 if (EFI_ERROR (Status
)) {
1003 Status
= EFI_NOT_FOUND
;
1009 // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.
1012 SubLink
= StartLink
;
1013 while (SubLink
!= EndLink
->ForwardLink
) {
1014 Entry
= CR (SubLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1015 Status
= CoreAllocateSpaceCheckEntry (Operation
, Entry
, GcdMemoryType
, GcdIoType
);
1016 if (EFI_ERROR (Status
)) {
1021 SubLink
= SubLink
->ForwardLink
;
1029 Status
= EFI_NOT_FOUND
;
1034 // Allocate work space to perform this operation
1036 Status
= CoreAllocateGcdMapEntry (&TopEntry
, &BottomEntry
);
1037 if (EFI_ERROR (Status
)) {
1038 Status
= EFI_OUT_OF_RESOURCES
;
1043 // Convert/Insert the list of descriptors from StartLink to EndLink
1046 while (Link
!= EndLink
->ForwardLink
) {
1047 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1048 CoreInsertGcdMapEntry (Link
, Entry
, *BaseAddress
, Length
, TopEntry
, BottomEntry
);
1049 Entry
->ImageHandle
= ImageHandle
;
1050 Entry
->DeviceHandle
= DeviceHandle
;
1051 Link
= Link
->ForwardLink
;
1057 Status
= CoreCleanupGcdMapEntry (TopEntry
, BottomEntry
, StartLink
, EndLink
, Map
);
1060 if ((Operation
& GCD_MEMORY_SPACE_OPERATION
) != 0) {
1061 CoreReleaseGcdMemoryLock ();
1063 if ((Operation
& GCD_IO_SPACE_OPERATION
) !=0) {
1064 CoreReleaseGcdIoLock ();
1072 Add a segment of memory to GCD map.
1074 @param GcdMemoryType Memory type of the segment.
1075 @param BaseAddress Base address of the segment.
1076 @param Length Length of the segment.
1077 @param Capabilities alterable attributes of the segment.
1079 @retval EFI_INVALID_PARAMETER Invalid parameters.
1080 @retval EFI_SUCCESS Successfully add a segment of memory space.
1084 CoreInternalAddMemorySpace (
1085 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1086 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1088 IN UINT64 Capabilities
1092 // Make sure parameters are valid
1094 if (GcdMemoryType
<= EfiGcdMemoryTypeNonExistent
|| GcdMemoryType
>= EfiGcdMemoryTypeMaximum
) {
1095 return EFI_INVALID_PARAMETER
;
1098 return CoreConvertSpace (GCD_ADD_MEMORY_OPERATION
, GcdMemoryType
, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, Capabilities
, 0);
1102 // GCD Core Services
1106 Allocates nonexistent memory, reserved memory, system memory, or memorymapped
1107 I/O resources from the global coherency domain of the processor.
1109 @param GcdAllocateType The type of allocate operation
1110 @param GcdMemoryType The desired memory type
1111 @param Alignment Align with 2^Alignment
1112 @param Length Length to allocate
1113 @param BaseAddress Base address to allocate
1114 @param ImageHandle The image handle consume the allocated space.
1115 @param DeviceHandle The device handle consume the allocated space.
1117 @retval EFI_INVALID_PARAMETER Invalid parameter.
1118 @retval EFI_NOT_FOUND No descriptor contains the desired space.
1119 @retval EFI_SUCCESS Memory space successfully allocated.
1123 CoreAllocateMemorySpace (
1124 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1125 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1128 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1129 IN EFI_HANDLE ImageHandle
,
1130 IN EFI_HANDLE DeviceHandle OPTIONAL
1133 return CoreAllocateSpace (
1134 GCD_ALLOCATE_MEMORY_OPERATION
,
1137 (EFI_GCD_IO_TYPE
) 0,
1148 Adds reserved memory, system memory, or memory-mapped I/O resources to the
1149 global coherency domain of the processor.
1151 @param GcdMemoryType Memory type of the memory space.
1152 @param BaseAddress Base address of the memory space.
1153 @param Length Length of the memory space.
1154 @param Capabilities alterable attributes of the memory space.
1156 @retval EFI_SUCCESS Merged this memory space into GCD map.
1160 CoreAddMemorySpace (
1161 IN EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1162 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1164 IN UINT64 Capabilities
1168 EFI_PHYSICAL_ADDRESS PageBaseAddress
;
1171 Status
= CoreInternalAddMemorySpace (GcdMemoryType
, BaseAddress
, Length
, Capabilities
);
1173 if (!EFI_ERROR (Status
) && GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) {
1175 PageBaseAddress
= PageAlignLength (BaseAddress
);
1176 PageLength
= PageAlignLength (BaseAddress
+ Length
- PageBaseAddress
);
1178 Status
= CoreAllocateMemorySpace (
1179 EfiGcdAllocateAddress
,
1184 gDxeCoreImageHandle
,
1188 if (!EFI_ERROR (Status
)) {
1189 CoreAddMemoryDescriptor (
1190 EfiConventionalMemory
,
1192 RShiftU64 (PageLength
, EFI_PAGE_SHIFT
),
1196 for (; PageLength
!= 0; PageLength
-= EFI_PAGE_SIZE
, PageBaseAddress
+= EFI_PAGE_SIZE
) {
1197 Status
= CoreAllocateMemorySpace (
1198 EfiGcdAllocateAddress
,
1203 gDxeCoreImageHandle
,
1207 if (!EFI_ERROR (Status
)) {
1208 CoreAddMemoryDescriptor (
1209 EfiConventionalMemory
,
1223 Frees nonexistent memory, reserved memory, system memory, or memory-mapped
1224 I/O resources from the global coherency domain of the processor.
1226 @param BaseAddress Base address of the memory space.
1227 @param Length Length of the memory space.
1229 @retval EFI_SUCCESS Space successfully freed.
1233 CoreFreeMemorySpace (
1234 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1238 return CoreConvertSpace (GCD_FREE_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1243 Removes reserved memory, system memory, or memory-mapped I/O resources from
1244 the global coherency domain of the processor.
1246 @param BaseAddress Base address of the memory space.
1247 @param Length Length of the memory space.
1249 @retval EFI_SUCCESS Successfully remove a segment of memory space.
1253 CoreRemoveMemorySpace (
1254 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1258 return CoreConvertSpace (GCD_REMOVE_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1263 Build a memory descriptor according to an entry.
1265 @param Descriptor The descriptor to be built
1266 @param Entry According to this entry
1270 BuildMemoryDescriptor (
1271 IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
,
1272 IN EFI_GCD_MAP_ENTRY
*Entry
1275 Descriptor
->BaseAddress
= Entry
->BaseAddress
;
1276 Descriptor
->Length
= Entry
->EndAddress
- Entry
->BaseAddress
+ 1;
1277 Descriptor
->Capabilities
= Entry
->Capabilities
;
1278 Descriptor
->Attributes
= Entry
->Attributes
;
1279 Descriptor
->GcdMemoryType
= Entry
->GcdMemoryType
;
1280 Descriptor
->ImageHandle
= Entry
->ImageHandle
;
1281 Descriptor
->DeviceHandle
= Entry
->DeviceHandle
;
1286 Retrieves the descriptor for a memory region containing a specified address.
1288 @param BaseAddress Specified start address
1289 @param Descriptor Specified length
1291 @retval EFI_INVALID_PARAMETER Invalid parameter
1292 @retval EFI_SUCCESS Successfully get memory space descriptor.
1296 CoreGetMemorySpaceDescriptor (
1297 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1298 OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
1302 LIST_ENTRY
*StartLink
;
1303 LIST_ENTRY
*EndLink
;
1304 EFI_GCD_MAP_ENTRY
*Entry
;
1307 // Make sure parameters are valid
1309 if (Descriptor
== NULL
) {
1310 return EFI_INVALID_PARAMETER
;
1313 CoreAcquireGcdMemoryLock ();
1316 // Search for the list of descriptors that contain BaseAddress
1318 Status
= CoreSearchGcdMapEntry (BaseAddress
, 1, &StartLink
, &EndLink
, &mGcdMemorySpaceMap
);
1319 if (EFI_ERROR (Status
)) {
1320 Status
= EFI_NOT_FOUND
;
1323 // Copy the contents of the found descriptor into Descriptor
1325 Entry
= CR (StartLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1326 BuildMemoryDescriptor (Descriptor
, Entry
);
1329 CoreReleaseGcdMemoryLock ();
1336 Modifies the attributes for a memory region in the global coherency domain of the
1339 @param BaseAddress Specified start address
1340 @param Length Specified length
1341 @param Attributes Specified attributes
1343 @retval EFI_SUCCESS Successfully set attribute of a segment of
1348 CoreSetMemorySpaceAttributes (
1349 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1351 IN UINT64 Attributes
1354 return CoreConvertSpace (GCD_SET_ATTRIBUTES_MEMORY_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, Attributes
);
1359 Returns a map of the memory resources in the global coherency domain of the
1362 @param NumberOfDescriptors Number of descriptors.
1363 @param MemorySpaceMap Descriptor array
1365 @retval EFI_INVALID_PARAMETER Invalid parameter
1366 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1367 @retval EFI_SUCCESS Successfully get memory space map.
1371 CoreGetMemorySpaceMap (
1372 OUT UINTN
*NumberOfDescriptors
,
1373 OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
**MemorySpaceMap
1378 EFI_GCD_MAP_ENTRY
*Entry
;
1379 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Descriptor
;
1382 // Make sure parameters are valid
1384 if (NumberOfDescriptors
== NULL
) {
1385 return EFI_INVALID_PARAMETER
;
1387 if (MemorySpaceMap
== NULL
) {
1388 return EFI_INVALID_PARAMETER
;
1391 CoreAcquireGcdMemoryLock ();
1394 // Count the number of descriptors
1396 *NumberOfDescriptors
= CoreCountGcdMapEntry (&mGcdMemorySpaceMap
);
1399 // Allocate the MemorySpaceMap
1401 *MemorySpaceMap
= CoreAllocateBootServicesPool (*NumberOfDescriptors
* sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR
));
1402 if (*MemorySpaceMap
== NULL
) {
1403 Status
= EFI_OUT_OF_RESOURCES
;
1408 // Fill in the MemorySpaceMap
1410 Descriptor
= *MemorySpaceMap
;
1411 Link
= mGcdMemorySpaceMap
.ForwardLink
;
1412 while (Link
!= &mGcdMemorySpaceMap
) {
1413 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1414 BuildMemoryDescriptor (Descriptor
, Entry
);
1416 Link
= Link
->ForwardLink
;
1418 Status
= EFI_SUCCESS
;
1421 CoreReleaseGcdMemoryLock ();
1427 Adds reserved I/O or I/O resources to the global coherency domain of the processor.
1429 @param GcdIoType IO type of the segment.
1430 @param BaseAddress Base address of the segment.
1431 @param Length Length of the segment.
1433 @retval EFI_SUCCESS Merged this segment into GCD map.
1434 @retval EFI_INVALID_PARAMETER Parameter not valid
1439 IN EFI_GCD_IO_TYPE GcdIoType
,
1440 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1445 // Make sure parameters are valid
1447 if (GcdIoType
<= EfiGcdIoTypeNonExistent
|| GcdIoType
>= EfiGcdIoTypeMaximum
) {
1448 return EFI_INVALID_PARAMETER
;
1450 return CoreConvertSpace (GCD_ADD_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, GcdIoType
, BaseAddress
, Length
, 0, 0);
1455 Allocates nonexistent I/O, reserved I/O, or I/O resources from the global coherency
1456 domain of the processor.
1458 @param GcdAllocateType The type of allocate operation
1459 @param GcdIoType The desired IO type
1460 @param Alignment Align with 2^Alignment
1461 @param Length Length to allocate
1462 @param BaseAddress Base address to allocate
1463 @param ImageHandle The image handle consume the allocated space.
1464 @param DeviceHandle The device handle consume the allocated space.
1466 @retval EFI_INVALID_PARAMETER Invalid parameter.
1467 @retval EFI_NOT_FOUND No descriptor contains the desired space.
1468 @retval EFI_SUCCESS IO space successfully allocated.
1472 CoreAllocateIoSpace (
1473 IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType
,
1474 IN EFI_GCD_IO_TYPE GcdIoType
,
1477 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
1478 IN EFI_HANDLE ImageHandle
,
1479 IN EFI_HANDLE DeviceHandle OPTIONAL
1482 return CoreAllocateSpace (
1483 GCD_ALLOCATE_IO_OPERATION
,
1485 (EFI_GCD_MEMORY_TYPE
) 0,
1497 Frees nonexistent I/O, reserved I/O, or I/O resources from the global coherency
1498 domain of the processor.
1500 @param BaseAddress Base address of the segment.
1501 @param Length Length of the segment.
1503 @retval EFI_SUCCESS Space successfully freed.
1508 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1512 return CoreConvertSpace (GCD_FREE_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1517 Removes reserved I/O or I/O resources from the global coherency domain of the
1520 @param BaseAddress Base address of the segment.
1521 @param Length Length of the segment.
1523 @retval EFI_SUCCESS Successfully removed a segment of IO space.
1528 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1532 return CoreConvertSpace (GCD_REMOVE_IO_OPERATION
, (EFI_GCD_MEMORY_TYPE
) 0, (EFI_GCD_IO_TYPE
) 0, BaseAddress
, Length
, 0, 0);
1537 Build a IO descriptor according to an entry.
1539 @param Descriptor The descriptor to be built
1540 @param Entry According to this entry
1545 IN EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
,
1546 IN EFI_GCD_MAP_ENTRY
*Entry
1549 Descriptor
->BaseAddress
= Entry
->BaseAddress
;
1550 Descriptor
->Length
= Entry
->EndAddress
- Entry
->BaseAddress
+ 1;
1551 Descriptor
->GcdIoType
= Entry
->GcdIoType
;
1552 Descriptor
->ImageHandle
= Entry
->ImageHandle
;
1553 Descriptor
->DeviceHandle
= Entry
->DeviceHandle
;
1558 Retrieves the descriptor for an I/O region containing a specified address.
1560 @param BaseAddress Specified start address
1561 @param Descriptor Specified length
1563 @retval EFI_INVALID_PARAMETER Descriptor is NULL.
1564 @retval EFI_SUCCESS Successfully get the IO space descriptor.
1568 CoreGetIoSpaceDescriptor (
1569 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1570 OUT EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
1574 LIST_ENTRY
*StartLink
;
1575 LIST_ENTRY
*EndLink
;
1576 EFI_GCD_MAP_ENTRY
*Entry
;
1579 // Make sure parameters are valid
1581 if (Descriptor
== NULL
) {
1582 return EFI_INVALID_PARAMETER
;
1585 CoreAcquireGcdIoLock ();
1588 // Search for the list of descriptors that contain BaseAddress
1590 Status
= CoreSearchGcdMapEntry (BaseAddress
, 1, &StartLink
, &EndLink
, &mGcdIoSpaceMap
);
1591 if (EFI_ERROR (Status
)) {
1592 Status
= EFI_NOT_FOUND
;
1595 // Copy the contents of the found descriptor into Descriptor
1597 Entry
= CR (StartLink
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1598 BuildIoDescriptor (Descriptor
, Entry
);
1601 CoreReleaseGcdIoLock ();
1608 Returns a map of the I/O resources in the global coherency domain of the processor.
1610 @param NumberOfDescriptors Number of descriptors.
1611 @param IoSpaceMap Descriptor array
1613 @retval EFI_INVALID_PARAMETER Invalid parameter
1614 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate
1615 @retval EFI_SUCCESS Successfully get IO space map.
1620 OUT UINTN
*NumberOfDescriptors
,
1621 OUT EFI_GCD_IO_SPACE_DESCRIPTOR
**IoSpaceMap
1626 EFI_GCD_MAP_ENTRY
*Entry
;
1627 EFI_GCD_IO_SPACE_DESCRIPTOR
*Descriptor
;
1630 // Make sure parameters are valid
1632 if (NumberOfDescriptors
== NULL
) {
1633 return EFI_INVALID_PARAMETER
;
1635 if (IoSpaceMap
== NULL
) {
1636 return EFI_INVALID_PARAMETER
;
1639 CoreAcquireGcdIoLock ();
1642 // Count the number of descriptors
1644 *NumberOfDescriptors
= CoreCountGcdMapEntry (&mGcdIoSpaceMap
);
1647 // Allocate the IoSpaceMap
1649 *IoSpaceMap
= CoreAllocateBootServicesPool (*NumberOfDescriptors
* sizeof (EFI_GCD_IO_SPACE_DESCRIPTOR
));
1650 if (*IoSpaceMap
== NULL
) {
1651 Status
= EFI_OUT_OF_RESOURCES
;
1656 // Fill in the IoSpaceMap
1658 Descriptor
= *IoSpaceMap
;
1659 Link
= mGcdIoSpaceMap
.ForwardLink
;
1660 while (Link
!= &mGcdIoSpaceMap
) {
1661 Entry
= CR (Link
, EFI_GCD_MAP_ENTRY
, Link
, EFI_GCD_MAP_SIGNATURE
);
1662 BuildIoDescriptor (Descriptor
, Entry
);
1664 Link
= Link
->ForwardLink
;
1666 Status
= EFI_SUCCESS
;
1669 CoreReleaseGcdIoLock ();
1675 Converts a Resource Descriptor HOB attributes mask to an EFI Memory Descriptor
1678 @param GcdMemoryType Type of resource in the GCD memory map.
1679 @param Attributes The attribute mask in the Resource Descriptor
1682 @return The capabilities mask for an EFI Memory Descriptor.
1686 CoreConvertResourceDescriptorHobAttributesToCapabilities (
1687 EFI_GCD_MEMORY_TYPE GcdMemoryType
,
1691 UINT64 Capabilities
;
1692 GCD_ATTRIBUTE_CONVERSION_ENTRY
*Conversion
;
1695 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
1697 for (Capabilities
= 0, Conversion
= mAttributeConversionTable
; Conversion
->Attribute
!= 0; Conversion
++) {
1698 if (Conversion
->Memory
|| (GcdMemoryType
!= EfiGcdMemoryTypeSystemMemory
)) {
1699 if (Attributes
& Conversion
->Attribute
) {
1700 Capabilities
|= Conversion
->Capability
;
1705 return Capabilities
;
1710 External function. Initializes the GCD and memory services based on the memory
1711 descriptor HOBs. This function is responsible for priming the GCD map and the
1712 memory map, so memory allocations and resource allocations can be made. The first
1713 part of this function can not depend on any memory services until at least one
1714 memory descriptor is provided to the memory services. Then the memory services
1715 can be used to intialize the GCD map.
1717 @param HobStart The start address of the HOB.
1718 @param MemoryBaseAddress Start address of memory region found to init DXE
1720 @param MemoryLength Length of memory region found to init DXE core.
1722 @retval EFI_SUCCESS Memory services successfully initialized.
1726 CoreInitializeMemoryServices (
1728 OUT EFI_PHYSICAL_ADDRESS
*MemoryBaseAddress
,
1729 OUT UINT64
*MemoryLength
1732 EFI_PEI_HOB_POINTERS Hob
;
1733 EFI_MEMORY_TYPE_INFORMATION
*EfiMemoryTypeInformation
;
1736 EFI_HOB_HANDOFF_INFO_TABLE
*PhitHob
;
1737 EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
;
1738 EFI_HOB_RESOURCE_DESCRIPTOR
*PhitResourceHob
;
1739 EFI_PHYSICAL_ADDRESS BaseAddress
;
1742 UINT64 Capabilities
;
1743 EFI_PHYSICAL_ADDRESS MaxMemoryBaseAddress
;
1744 UINT64 MaxMemoryLength
;
1745 UINT64 MaxMemoryAttributes
;
1746 EFI_PHYSICAL_ADDRESS MaxAddress
;
1747 EFI_PHYSICAL_ADDRESS HighAddress
;
1748 EFI_HOB_RESOURCE_DESCRIPTOR
*MaxResourceHob
;
1749 EFI_HOB_GUID_TYPE
*GuidHob
;
1752 // Point at the first HOB. This must be the PHIT HOB.
1754 Hob
.Raw
= *HobStart
;
1755 ASSERT (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_HANDOFF
);
1758 // Initialize the spin locks and maps in the memory services.
1759 // Also fill in the memory services into the EFI Boot Services Table
1761 CoreInitializePool ();
1764 // Initialize Local Variables
1766 PhitResourceHob
= NULL
;
1767 MaxResourceHob
= NULL
;
1772 MaxMemoryBaseAddress
= 0;
1773 MaxMemoryLength
= 0;
1774 MaxMemoryAttributes
= 0;
1777 // Cache the PHIT HOB for later use
1779 PhitHob
= Hob
.HandoffInformationTable
;
1782 // See if a Memory Type Information HOB is available
1784 GuidHob
= GetFirstGuidHob (&gEfiMemoryTypeInformationGuid
);
1785 if (GuidHob
!= NULL
) {
1786 EfiMemoryTypeInformation
= GET_GUID_HOB_DATA (GuidHob
);
1787 DataSize
= GET_GUID_HOB_DATA_SIZE (GuidHob
);
1788 if (EfiMemoryTypeInformation
!= NULL
&& DataSize
> 0 && DataSize
<= (EfiMaxMemoryType
+ 1) * sizeof (EFI_MEMORY_TYPE_INFORMATION
)) {
1789 CopyMem (&gMemoryTypeInformation
, EfiMemoryTypeInformation
, DataSize
);
1794 // Find the Resource Descriptor HOB that contains range FreeMemoryBaseAddress..FreeMemoryLength
1798 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
1800 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
1802 ResourceHob
= Hob
.ResourceDescriptor
;
1804 if (ResourceHob
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
&&
1805 (ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == TESTED_MEMORY_ATTRIBUTES
) {
1807 if (PhitHob
->EfiFreeMemoryBottom
>= ResourceHob
->PhysicalStart
&&
1808 PhitHob
->EfiFreeMemoryTop
<= (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
) ) {
1811 // Cache the resource descriptor HOB for the memory region described by the PHIT HOB
1813 PhitResourceHob
= ResourceHob
;
1816 Attributes
= PhitResourceHob
->ResourceAttribute
;
1817 BaseAddress
= PageAlignAddress (PhitHob
->EfiMemoryTop
);
1818 Length
= PageAlignLength (ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
- BaseAddress
);
1819 if (Length
< MINIMUM_INITIAL_MEMORY_SIZE
) {
1820 BaseAddress
= PageAlignAddress (PhitHob
->EfiFreeMemoryBottom
);
1821 Length
= PageAlignLength (PhitHob
->EfiFreeMemoryTop
- BaseAddress
);
1822 if (Length
< MINIMUM_INITIAL_MEMORY_SIZE
) {
1823 BaseAddress
= PageAlignAddress (ResourceHob
->PhysicalStart
);
1824 Length
= PageAlignLength ((UINT64
)((UINTN
)*HobStart
- BaseAddress
));
1834 // Assert if a resource descriptor HOB for the memory region described by the PHIT was not found
1839 // Search all the resource descriptor HOBs from the highest possible addresses down for a memory
1840 // region that is big enough to initialize the DXE core. Always skip the PHIT Resource HOB.
1841 // The max address must be within the physically addressible range for the processor.
1843 MaxMemoryLength
= 0;
1844 MaxAddress
= EFI_MAX_ADDRESS
;
1849 // Search for a tested memory region that is below MaxAddress
1851 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
1854 // See if this is a resource descriptor HOB that does not contain the PHIT.
1856 if (Hob
.ResourceDescriptor
!= PhitResourceHob
&& GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
1858 ResourceHob
= Hob
.ResourceDescriptor
;
1860 // See if this resource descrior HOB describes tested system memory below MaxAddress
1862 if (ResourceHob
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
&&
1863 (ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == TESTED_MEMORY_ATTRIBUTES
&&
1864 ResourceHob
->PhysicalStart
+ ResourceHob
->ResourceLength
<= MaxAddress
) {
1867 // See if this is the highest tested system memory region below MaxAddress
1869 if (ResourceHob
->PhysicalStart
> HighAddress
) {
1871 MaxResourceHob
= ResourceHob
;
1872 HighAddress
= MaxResourceHob
->PhysicalStart
;
1880 // Compute the size of the tested memory region below MaxAddrees
1882 MaxMemoryBaseAddress
= PageAlignAddress (MaxResourceHob
->PhysicalStart
);
1883 MaxMemoryLength
= PageAlignLength (MaxResourceHob
->PhysicalStart
+ MaxResourceHob
->ResourceLength
- MaxMemoryBaseAddress
);
1884 MaxMemoryAttributes
= MaxResourceHob
->ResourceAttribute
;
1886 MaxAddress
= ResourceHob
->PhysicalStart
;
1887 } while (Found
&& MaxMemoryLength
< MINIMUM_INITIAL_MEMORY_SIZE
);
1892 if ((Length
< MINIMUM_INITIAL_MEMORY_SIZE
) ||
1893 (MaxMemoryBaseAddress
> BaseAddress
&& MaxMemoryLength
>= MINIMUM_INITIAL_MEMORY_SIZE
) ) {
1894 BaseAddress
= MaxMemoryBaseAddress
;
1895 Length
= MaxMemoryLength
;
1896 Attributes
= MaxMemoryAttributes
;
1900 // If no memory regions are found that are big enough to initialize the DXE core, then ASSERT().
1902 ASSERT (Length
>= MINIMUM_INITIAL_MEMORY_SIZE
);
1905 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
1907 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeSystemMemory
, Attributes
);
1910 // Declare the very first memory region, so the EFI Memory Services are available.
1912 CoreAddMemoryDescriptor (
1913 EfiConventionalMemory
,
1915 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
1919 *MemoryBaseAddress
= BaseAddress
;
1920 *MemoryLength
= Length
;
1927 External function. Initializes the GCD and memory services based on the memory
1928 descriptor HOBs. This function is responsible for priming the GCD map and the
1929 memory map, so memory allocations and resource allocations can be made. The first
1930 part of this function can not depend on any memory services until at least one
1931 memory descriptor is provided to the memory services. Then the memory services
1932 can be used to intialize the GCD map. The HobStart will be relocated to a pool
1935 @param HobStart The start address of the HOB
1936 @param MemoryBaseAddress Start address of memory region found to init DXE
1938 @param MemoryLength Length of memory region found to init DXE core.
1940 @retval EFI_SUCCESS GCD services successfully initialized.
1944 CoreInitializeGcdServices (
1945 IN OUT VOID
**HobStart
,
1946 IN EFI_PHYSICAL_ADDRESS MemoryBaseAddress
,
1947 IN UINT64 MemoryLength
1950 EFI_PEI_HOB_POINTERS Hob
;
1952 EFI_HOB_HANDOFF_INFO_TABLE
*PhitHob
;
1953 UINT8 SizeOfMemorySpace
;
1954 UINT8 SizeOfIoSpace
;
1955 EFI_HOB_RESOURCE_DESCRIPTOR
*ResourceHob
;
1956 EFI_PHYSICAL_ADDRESS BaseAddress
;
1959 EFI_GCD_MAP_ENTRY
*Entry
;
1960 EFI_GCD_MEMORY_TYPE GcdMemoryType
;
1961 EFI_GCD_IO_TYPE GcdIoType
;
1962 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor
;
1963 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
1964 EFI_HOB_FIRMWARE_VOLUME
*FirmwareVolumeHob
;
1965 UINTN NumberOfDescriptors
;
1966 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
1968 UINT64 Capabilities
;
1969 EFI_HOB_CPU
* CpuHob
;
1971 // Cache the PHIT HOB for later use
1973 PhitHob
= (EFI_HOB_HANDOFF_INFO_TABLE
*)(*HobStart
);
1976 // Get the number of address lines in the I/O and Memory space for the CPU
1978 CpuHob
= GetFirstHob (EFI_HOB_TYPE_CPU
);
1979 ASSERT (CpuHob
!= NULL
);
1980 SizeOfMemorySpace
= CpuHob
->SizeOfMemorySpace
;
1981 SizeOfIoSpace
= CpuHob
->SizeOfIoSpace
;
1984 // Initialize the GCD Memory Space Map
1986 Entry
= CoreAllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY
), &mGcdMemorySpaceMapEntryTemplate
);
1987 ASSERT (Entry
!= NULL
);
1989 Entry
->EndAddress
= LShiftU64 (1, SizeOfMemorySpace
) - 1;
1991 InsertHeadList (&mGcdMemorySpaceMap
, &Entry
->Link
);
1994 // Initialize the GCD I/O Space Map
1996 Entry
= CoreAllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY
), &mGcdIoSpaceMapEntryTemplate
);
1997 ASSERT (Entry
!= NULL
);
1999 Entry
->EndAddress
= LShiftU64 (1, SizeOfIoSpace
) - 1;
2001 InsertHeadList (&mGcdIoSpaceMap
, &Entry
->Link
);
2004 // Walk the HOB list and add all resource descriptors to the GCD
2006 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2008 GcdMemoryType
= EfiGcdMemoryTypeNonExistent
;
2009 GcdIoType
= EfiGcdIoTypeNonExistent
;
2011 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
2013 ResourceHob
= Hob
.ResourceDescriptor
;
2015 switch (ResourceHob
->ResourceType
) {
2016 case EFI_RESOURCE_SYSTEM_MEMORY
:
2017 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == TESTED_MEMORY_ATTRIBUTES
) {
2018 GcdMemoryType
= EfiGcdMemoryTypeSystemMemory
;
2020 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == INITIALIZED_MEMORY_ATTRIBUTES
) {
2021 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2023 if ((ResourceHob
->ResourceAttribute
& MEMORY_ATTRIBUTE_MASK
) == PRESENT_MEMORY_ATTRIBUTES
) {
2024 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2027 case EFI_RESOURCE_MEMORY_MAPPED_IO
:
2028 case EFI_RESOURCE_FIRMWARE_DEVICE
:
2029 GcdMemoryType
= EfiGcdMemoryTypeMemoryMappedIo
;
2031 case EFI_RESOURCE_MEMORY_MAPPED_IO_PORT
:
2032 case EFI_RESOURCE_MEMORY_RESERVED
:
2033 GcdMemoryType
= EfiGcdMemoryTypeReserved
;
2035 case EFI_RESOURCE_IO
:
2036 GcdIoType
= EfiGcdIoTypeIo
;
2038 case EFI_RESOURCE_IO_RESERVED
:
2039 GcdIoType
= EfiGcdIoTypeReserved
;
2043 if (GcdMemoryType
!= EfiGcdMemoryTypeNonExistent
) {
2046 // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask
2048 Capabilities
= CoreConvertResourceDescriptorHobAttributesToCapabilities (
2050 ResourceHob
->ResourceAttribute
2053 Status
= CoreInternalAddMemorySpace (
2055 ResourceHob
->PhysicalStart
,
2056 ResourceHob
->ResourceLength
,
2061 if (GcdIoType
!= EfiGcdIoTypeNonExistent
) {
2062 Status
= CoreAddIoSpace (
2064 ResourceHob
->PhysicalStart
,
2065 ResourceHob
->ResourceLength
2072 // Allocate first memory region from the GCD by the DXE core
2074 Status
= CoreAllocateMemorySpace (
2075 EfiGcdAllocateAddress
,
2076 EfiGcdMemoryTypeSystemMemory
,
2080 gDxeCoreImageHandle
,
2085 // Walk the HOB list and allocate all memory space that is consumed by memory allocation HOBs,
2086 // and Firmware Volume HOBs. Also update the EFI Memory Map with the memory allocation HOBs.
2088 for (Hob
.Raw
= *HobStart
; !END_OF_HOB_LIST(Hob
); Hob
.Raw
= GET_NEXT_HOB(Hob
)) {
2089 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
2090 MemoryHob
= Hob
.MemoryAllocation
;
2091 BaseAddress
= MemoryHob
->AllocDescriptor
.MemoryBaseAddress
;
2092 Status
= CoreGetMemorySpaceDescriptor (BaseAddress
, &Descriptor
);
2093 if (!EFI_ERROR (Status
)) {
2094 Status
= CoreAllocateMemorySpace (
2095 EfiGcdAllocateAddress
,
2096 Descriptor
.GcdMemoryType
,
2098 MemoryHob
->AllocDescriptor
.MemoryLength
,
2100 gDxeCoreImageHandle
,
2103 if (!EFI_ERROR (Status
) && Descriptor
.GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) {
2104 CoreAddMemoryDescriptor (
2105 MemoryHob
->AllocDescriptor
.MemoryType
,
2106 MemoryHob
->AllocDescriptor
.MemoryBaseAddress
,
2107 RShiftU64 (MemoryHob
->AllocDescriptor
.MemoryLength
, EFI_PAGE_SHIFT
),
2108 Descriptor
.Capabilities
& (~EFI_MEMORY_RUNTIME
)
2114 if (GET_HOB_TYPE (Hob
) == EFI_HOB_TYPE_FV
) {
2115 FirmwareVolumeHob
= Hob
.FirmwareVolume
;
2116 BaseAddress
= FirmwareVolumeHob
->BaseAddress
;
2117 Status
= CoreAllocateMemorySpace (
2118 EfiGcdAllocateAddress
,
2119 EfiGcdMemoryTypeMemoryMappedIo
,
2121 FirmwareVolumeHob
->Length
,
2123 gDxeCoreImageHandle
,
2130 // Relocate HOB List to an allocated pool buffer.
2132 NewHobList
= CoreAllocateCopyPool (
2133 (UINTN
)PhitHob
->EfiFreeMemoryBottom
- (UINTN
)(*HobStart
),
2136 ASSERT (NewHobList
!= NULL
);
2138 *HobStart
= NewHobList
;
2139 gHobList
= NewHobList
;
2142 // Add and allocate the remaining unallocated system memory to the memory services.
2144 Status
= CoreGetMemorySpaceMap (&NumberOfDescriptors
, &MemorySpaceMap
);
2145 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
2146 if (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeSystemMemory
) {
2147 if (MemorySpaceMap
[Index
].ImageHandle
== NULL
) {
2148 BaseAddress
= PageAlignAddress (MemorySpaceMap
[Index
].BaseAddress
);
2149 Length
= PageAlignLength (MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
- BaseAddress
);
2150 if (Length
== 0 || MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
< BaseAddress
) {
2153 CoreAddMemoryDescriptor (
2154 EfiConventionalMemory
,
2156 RShiftU64 (Length
, EFI_PAGE_SHIFT
),
2157 MemorySpaceMap
[Index
].Capabilities
& (~EFI_MEMORY_RUNTIME
)
2159 Status
= CoreAllocateMemorySpace (
2160 EfiGcdAllocateAddress
,
2161 EfiGcdMemoryTypeSystemMemory
,
2165 gDxeCoreImageHandle
,
2171 CoreFreePool (MemorySpaceMap
);