--- /dev/null
+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation \r
+All rights reserved. This program and the accompanying materials \r
+are licensed and made available under the terms and conditions of the BSD License \r
+which accompanies this distribution. The full text of the license may be found at \r
+http://opensource.org/licenses/bsd-license.php \r
+ \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
+\r
+Module Name:\r
+\r
+ gcd.c\r
+\r
+Abstract:\r
+ The file contains the GCD related services in the EFI Boot Services Table.\r
+ The GCD services are used to manage the memory and I/O regions that \r
+ are accessible to the CPU that is executing the DXE core.\r
+\r
+--*/\r
+\r
+#include <DxeMain.h>\r
+\r
+#define MINIMUM_INITIAL_MEMORY_SIZE 0x10000\r
+\r
+#define MEMORY_ATTRIBUTE_MASK (EFI_RESOURCE_ATTRIBUTE_PRESENT | \\r
+ EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \\r
+ EFI_RESOURCE_ATTRIBUTE_TESTED | \\r
+ EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED | \\r
+ EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED | \\r
+ EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED | \\r
+ EFI_RESOURCE_ATTRIBUTE_16_BIT_IO | \\r
+ EFI_RESOURCE_ATTRIBUTE_32_BIT_IO | \\r
+ EFI_RESOURCE_ATTRIBUTE_64_BIT_IO ) \r
+\r
+#define TESTED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \\r
+ EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \\r
+ EFI_RESOURCE_ATTRIBUTE_TESTED )\r
+\r
+#define INITIALIZED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \\r
+ EFI_RESOURCE_ATTRIBUTE_INITIALIZED )\r
+\r
+#define PRESENT_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT)\r
+\r
+#define INVALID_CPU_ARCH_ATTRIBUTES 0xffffffff\r
+\r
+//\r
+// Module Variables\r
+//\r
+EFI_LOCK mGcdMemorySpaceLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);\r
+EFI_LOCK mGcdIoSpaceLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);\r
+LIST_ENTRY mGcdMemorySpaceMap = INITIALIZE_LIST_HEAD_VARIABLE (mGcdMemorySpaceMap);\r
+LIST_ENTRY mGcdIoSpaceMap = INITIALIZE_LIST_HEAD_VARIABLE (mGcdIoSpaceMap);\r
+\r
+EFI_GCD_MAP_ENTRY mGcdMemorySpaceMapEntryTemplate = {\r
+ EFI_GCD_MAP_SIGNATURE,\r
+ { NULL, NULL },\r
+ 0,\r
+ 0,\r
+ 0,\r
+ 0,\r
+ EfiGcdMemoryTypeNonExistent,\r
+ (EFI_GCD_IO_TYPE) 0,\r
+ NULL,\r
+ NULL\r
+};\r
+\r
+EFI_GCD_MAP_ENTRY mGcdIoSpaceMapEntryTemplate = {\r
+ EFI_GCD_MAP_SIGNATURE,\r
+ { NULL, NULL },\r
+ 0,\r
+ 0,\r
+ 0,\r
+ 0,\r
+ (EFI_GCD_MEMORY_TYPE) 0,\r
+ EfiGcdIoTypeNonExistent,\r
+ NULL,\r
+ NULL\r
+};\r
+\r
+GCD_ATTRIBUTE_CONVERSION_ENTRY mAttributeConversionTable[] = {\r
+ { EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE, EFI_MEMORY_UC, TRUE },\r
+ { EFI_RESOURCE_ATTRIBUTE_UNCACHED_EXPORTED, EFI_MEMORY_UCE, TRUE }, \r
+ { EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE, EFI_MEMORY_WC, TRUE },\r
+ { EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE, EFI_MEMORY_WT, TRUE },\r
+ { EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE, EFI_MEMORY_WB, TRUE },\r
+ { EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED, EFI_MEMORY_RP, TRUE },\r
+ { EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED, EFI_MEMORY_WP, TRUE },\r
+ { EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED, EFI_MEMORY_XP, TRUE },\r
+ { EFI_RESOURCE_ATTRIBUTE_PRESENT, EFI_MEMORY_PRESENT, FALSE },\r
+ { EFI_RESOURCE_ATTRIBUTE_INITIALIZED, EFI_MEMORY_INITIALIZED, FALSE },\r
+ { EFI_RESOURCE_ATTRIBUTE_TESTED, EFI_MEMORY_TESTED, FALSE },\r
+ { 0, 0, FALSE }\r
+};\r
+\r
+VOID\r
+CoreAcquireGcdMemoryLock (\r
+ VOID\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Acquire memory lock on mGcdMemorySpaceLock\r
+\r
+Arguments:\r
+ None\r
+\r
+Returns:\r
+ None\r
+\r
+--*/\r
+{\r
+ CoreAcquireLock (&mGcdMemorySpaceLock);\r
+}\r
+\r
+\r
+VOID\r
+CoreReleaseGcdMemoryLock (\r
+ VOID\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Release memory lock on mGcdMemorySpaceLock\r
+\r
+Arguments:\r
+ None\r
+\r
+Returns:\r
+ None\r
+\r
+--*/\r
+{\r
+ CoreReleaseLock (&mGcdMemorySpaceLock);\r
+}\r
+\r
+\r
+STATIC\r
+VOID\r
+CoreAcquireGcdIoLock (\r
+ VOID\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Acquire memory lock on mGcdIoSpaceLock\r
+\r
+Arguments:\r
+ None\r
+\r
+Returns:\r
+ None\r
+\r
+--*/\r
+{\r
+ CoreAcquireLock (&mGcdIoSpaceLock);\r
+}\r
+\r
+STATIC\r
+VOID\r
+CoreReleaseGcdIoLock (\r
+ VOID\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Release memory lock on mGcdIoSpaceLock\r
+\r
+Arguments:\r
+ None\r
+\r
+Returns:\r
+ None\r
+\r
+--*/\r
+{\r
+ CoreReleaseLock (&mGcdIoSpaceLock);\r
+}\r
+\r
+\r
+\r
+//\r
+// GCD Initialization Worker Functions\r
+//\r
+STATIC\r
+UINT64\r
+AlignValue (\r
+ IN UINT64 Value,\r
+ IN UINTN Alignment,\r
+ IN BOOLEAN RoundUp\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Aligns a value to the specified boundary.\r
+\r
+Arguments:\r
+\r
+ Value - 64 bit value to align\r
+ Alignment - Log base 2 of the boundary to align Value to\r
+ RoundUp - TRUE if Value is to be rounded up to the nearest aligned boundary. \r
+ FALSE is Value is to be rounded down to the nearest aligned boundary.\r
+\r
+Returns:\r
+\r
+ A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.\r
+\r
+--*/\r
+{\r
+ UINT64 AlignmentMask;\r
+\r
+ AlignmentMask = LShiftU64 (1, Alignment) - 1;\r
+ if (RoundUp) {\r
+ Value += AlignmentMask;\r
+ }\r
+ return Value & (~AlignmentMask);\r
+}\r
+\r
+STATIC\r
+UINT64\r
+PageAlignAddress (\r
+ IN UINT64 Value\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Aligns address to the page boundary.\r
+\r
+Arguments:\r
+\r
+ Value - 64 bit address to align\r
+\r
+Returns:\r
+\r
+ A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.\r
+\r
+--*/\r
+{\r
+ return AlignValue (Value, EFI_PAGE_SHIFT, TRUE);\r
+}\r
+\r
+STATIC\r
+UINT64\r
+PageAlignLength (\r
+ IN UINT64 Value\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Aligns length to the page boundary.\r
+\r
+Arguments:\r
+\r
+ Value - 64 bit length to align\r
+\r
+Returns:\r
+\r
+ A 64 bit value is the aligned to the value nearest Value with an alignment by Alignment.\r
+\r
+--*/\r
+{\r
+ return AlignValue (Value, EFI_PAGE_SHIFT, FALSE);\r
+}\r
+\r
+//\r
+// GCD Memory Space Worker Functions\r
+//\r
+STATIC\r
+EFI_STATUS\r
+CoreAllocateGcdMapEntry (\r
+ IN OUT EFI_GCD_MAP_ENTRY **TopEntry,\r
+ IN OUT EFI_GCD_MAP_ENTRY **BottomEntry\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Allocate pool for two entries.\r
+\r
+Arguments:\r
+\r
+ TopEntry - An entry of GCD map\r
+ BottomEntry - An entry of GCD map\r
+\r
+Returns:\r
+\r
+ EFI_OUT_OF_RESOURCES - No enough buffer to be allocated.\r
+ EFI_SUCCESS - Both entries successfully allocated.\r
+\r
+--*/\r
+{\r
+ *TopEntry = CoreAllocateZeroBootServicesPool (sizeof (EFI_GCD_MAP_ENTRY));\r
+ if (*TopEntry == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ *BottomEntry = CoreAllocateZeroBootServicesPool (sizeof (EFI_GCD_MAP_ENTRY));\r
+ if (*BottomEntry == NULL) {\r
+ CoreFreePool (*TopEntry);\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+CoreInsertGcdMapEntry (\r
+ IN LIST_ENTRY *Link,\r
+ IN EFI_GCD_MAP_ENTRY *Entry,\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length,\r
+ IN EFI_GCD_MAP_ENTRY *TopEntry,\r
+ IN EFI_GCD_MAP_ENTRY *BottomEntry\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Internal function. Inserts a new descriptor into a sorted list\r
+\r
+Arguments:\r
+\r
+ Link - The linked list to insert the range BaseAddress and Length into\r
+\r
+ Entry - A pointer to the entry that is inserted\r
+\r
+ BaseAddress - The base address of the new range\r
+ \r
+ Length - The length of the new range in bytes\r
+ \r
+ TopEntry - Top pad entry to insert if needed.\r
+\r
+ BottomEntry - Bottom pad entry to insert if needed.\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - The new range was inserted into the linked list\r
+ \r
+--*/\r
+{\r
+ ASSERT (Length != 0);\r
+ ASSERT (TopEntry->Signature == 0);\r
+ ASSERT (BottomEntry->Signature == 0);\r
+\r
+ if (BaseAddress > Entry->BaseAddress) {\r
+ CopyMem (BottomEntry, Entry, sizeof (EFI_GCD_MAP_ENTRY));\r
+ Entry->BaseAddress = BaseAddress;\r
+ BottomEntry->EndAddress = BaseAddress - 1;\r
+ InsertTailList (Link, &BottomEntry->Link);\r
+ } \r
+\r
+ if ((BaseAddress + Length - 1) < Entry->EndAddress) {\r
+ CopyMem (TopEntry, Entry, sizeof (EFI_GCD_MAP_ENTRY));\r
+ TopEntry->BaseAddress = BaseAddress + Length;\r
+ Entry->EndAddress = BaseAddress + Length - 1;\r
+ InsertHeadList (Link, &TopEntry->Link);\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+CoreMergeGcdMapEntry (\r
+ IN LIST_ENTRY *Link,\r
+ IN BOOLEAN Forward,\r
+ IN LIST_ENTRY *Map\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Merge the Gcd region specified by Link and its adjacent entry\r
+\r
+Arguments:\r
+\r
+ Link - Specify the entry to be merged (with its adjacent entry).\r
+ \r
+ Forward - Direction (forward or backward).\r
+ \r
+ Map - Boundary.\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - Successfully returned.\r
+ \r
+ EFI_UNSUPPORTED - These adjacent regions could not merge.\r
+\r
+--*/\r
+{\r
+ LIST_ENTRY *AdjacentLink;\r
+ EFI_GCD_MAP_ENTRY *Entry;\r
+ EFI_GCD_MAP_ENTRY *AdjacentEntry;\r
+\r
+ //\r
+ // Get adjacent entry\r
+ //\r
+ if (Forward) {\r
+ AdjacentLink = Link->ForwardLink;\r
+ } else {\r
+ AdjacentLink = Link->BackLink;\r
+ }\r
+\r
+ //\r
+ // If AdjacentLink is the head of the list, then no merge can be performed\r
+ //\r
+ if (AdjacentLink == Map) {\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+ AdjacentEntry = CR (AdjacentLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+\r
+ if (Entry->Capabilities != AdjacentEntry->Capabilities) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ if (Entry->Attributes != AdjacentEntry->Attributes) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ if (Entry->GcdMemoryType != AdjacentEntry->GcdMemoryType) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ if (Entry->GcdIoType != AdjacentEntry->GcdIoType) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ if (Entry->ImageHandle != AdjacentEntry->ImageHandle) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ if (Entry->DeviceHandle != AdjacentEntry->DeviceHandle) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ if (Forward) {\r
+ Entry->EndAddress = AdjacentEntry->EndAddress;\r
+ } else {\r
+ Entry->BaseAddress = AdjacentEntry->BaseAddress;\r
+ }\r
+ RemoveEntryList (AdjacentLink);\r
+ CoreFreePool (AdjacentEntry);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+CoreCleanupGcdMapEntry (\r
+ IN EFI_GCD_MAP_ENTRY *TopEntry,\r
+ IN EFI_GCD_MAP_ENTRY *BottomEntry,\r
+ IN LIST_ENTRY *StartLink,\r
+ IN LIST_ENTRY *EndLink,\r
+ IN LIST_ENTRY *Map\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Merge adjacent entries on total chain.\r
+\r
+Arguments:\r
+\r
+ TopEntry - Top entry of GCD map.\r
+ \r
+ BottomEntry - Bottom entry of GCD map.\r
+ \r
+ StartLink - Start link of the list for this loop.\r
+ \r
+ EndLink - End link of the list for this loop.\r
+ \r
+ Map - Boundary.\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - GCD map successfully cleaned up.\r
+\r
+--*/\r
+{\r
+ LIST_ENTRY *Link;\r
+\r
+ if (TopEntry->Signature == 0) {\r
+ CoreFreePool (TopEntry);\r
+ }\r
+ if (BottomEntry->Signature == 0) {\r
+ CoreFreePool (BottomEntry);\r
+ }\r
+\r
+ Link = StartLink;\r
+ while (Link != EndLink->ForwardLink) {\r
+ CoreMergeGcdMapEntry (Link, FALSE, Map);\r
+ Link = Link->ForwardLink;\r
+ }\r
+ CoreMergeGcdMapEntry (EndLink, TRUE, Map);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+CoreSearchGcdMapEntry (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length,\r
+ OUT LIST_ENTRY **StartLink,\r
+ OUT LIST_ENTRY **EndLink,\r
+ IN LIST_ENTRY *Map\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Search a segment of memory space in GCD map. The result is a range of GCD entry list.\r
+\r
+Arguments:\r
+\r
+ BaseAddress - The start address of the segment.\r
+ \r
+ Length - The length of the segment.\r
+ \r
+ StartLink - The first GCD entry involves this segment of memory space.\r
+ \r
+ EndLink - The first GCD entry involves this segment of memory space.\r
+ \r
+ Map - Points to the start entry to search.\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - Successfully found the entry.\r
+ \r
+ EFI_NOT_FOUND - Not found.\r
+\r
+--*/\r
+{\r
+ LIST_ENTRY *Link;\r
+ EFI_GCD_MAP_ENTRY *Entry;\r
+\r
+ ASSERT (Length != 0);\r
+\r
+ *StartLink = NULL;\r
+ *EndLink = NULL;\r
+\r
+ Link = Map->ForwardLink;\r
+ while (Link != Map) {\r
+ Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+ if (BaseAddress >= Entry->BaseAddress && BaseAddress <= Entry->EndAddress) {\r
+ *StartLink = Link;\r
+ }\r
+ if (*StartLink != NULL) {\r
+ if ((BaseAddress + Length - 1) >= Entry->BaseAddress && \r
+ (BaseAddress + Length - 1) <= Entry->EndAddress ) {\r
+ *EndLink = Link;\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+ Link = Link->ForwardLink;\r
+ }\r
+ return EFI_NOT_FOUND;\r
+}\r
+\r
+STATIC\r
+UINTN\r
+CoreCountGcdMapEntry (\r
+ IN LIST_ENTRY *Map\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Count the amount of GCD map entries.\r
+\r
+Arguments:\r
+\r
+ Map - Points to the start entry to do the count loop.\r
+\r
+Returns:\r
+\r
+ The count.\r
+\r
+--*/\r
+{\r
+ UINTN Count;\r
+ LIST_ENTRY *Link;\r
+\r
+ Count = 0;\r
+ Link = Map->ForwardLink;\r
+ while (Link != Map) {\r
+ Count++;\r
+ Link = Link->ForwardLink;\r
+ }\r
+ return Count;\r
+}\r
+\r
+\r
+STATIC\r
+UINT64\r
+ConverToCpuArchAttributes (\r
+ UINT64 Attributes\r
+ ) \r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Return the memory attribute specified by Attributes\r
+\r
+Arguments:\r
+\r
+ Attributes - A num with some attribute bits on.\r
+\r
+Returns:\r
+\r
+ The enum value of memory attribute.\r
+\r
+--*/\r
+{\r
+ if ( (Attributes & EFI_MEMORY_UC) == EFI_MEMORY_UC) {\r
+ return EFI_MEMORY_UC;\r
+ }\r
+\r
+ if ( (Attributes & EFI_MEMORY_WC ) == EFI_MEMORY_WC) {\r
+ return EFI_MEMORY_WC;\r
+ }\r
+\r
+ if ( (Attributes & EFI_MEMORY_WT ) == EFI_MEMORY_WT) {\r
+ return EFI_MEMORY_WT;\r
+ }\r
+\r
+ if ( (Attributes & EFI_MEMORY_WB) == EFI_MEMORY_WB) {\r
+ return EFI_MEMORY_WB;\r
+ }\r
+\r
+ if ( (Attributes & EFI_MEMORY_WP) == EFI_MEMORY_WP) {\r
+ return EFI_MEMORY_WP;\r
+ }\r
+\r
+ return INVALID_CPU_ARCH_ATTRIBUTES;\r
+\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+CoreConvertSpace (\r
+ IN UINTN Operation,\r
+ IN EFI_GCD_MEMORY_TYPE GcdMemoryType,\r
+ IN EFI_GCD_IO_TYPE GcdIoType,\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length,\r
+ IN UINT64 Capabilities,\r
+ IN UINT64 Attributes\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Do operation on a segment of memory space specified (add, free, remove, change attribute ...).\r
+\r
+Arguments:\r
+\r
+ Operation - The type of the operation\r
+ \r
+ GcdMemoryType - Additional information for the operation\r
+ \r
+ GcdIoType - Additional information for the operation\r
+ \r
+ BaseAddress - Start address of the segment\r
+ \r
+ Length - length of the segment\r
+ \r
+ Capabilities - The alterable attributes of a newly added entry\r
+ \r
+ Attributes - The attributes needs to be set\r
+ \r
+Returns:\r
+\r
+ EFI_INVALID_PARAMETER - Length is 0 or address (length) not aligned when setting attribute.\r
+ \r
+ EFI_SUCCESS - Action successfully done.\r
+ \r
+ EFI_UNSUPPORTED - Could not find the proper descriptor on this segment or \r
+ set an upsupported attribute.\r
+ \r
+ EFI_ACCESS_DENIED - Operate on an space non-exist or is used for an image.\r
+ \r
+ EFI_NOT_FOUND - Free a non-using space or remove a non-exist space, and so on.\r
+ \r
+ EFI_OUT_OF_RESOURCES - No buffer could be allocated.\r
+\r
+Returns:\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ LIST_ENTRY *Map;\r
+ LIST_ENTRY *Link;\r
+ EFI_GCD_MAP_ENTRY *Entry;\r
+ EFI_GCD_MAP_ENTRY *TopEntry;\r
+ EFI_GCD_MAP_ENTRY *BottomEntry;\r
+ LIST_ENTRY *StartLink;\r
+ LIST_ENTRY *EndLink;\r
+ \r
+ EFI_CPU_ARCH_PROTOCOL *CpuArch;\r
+ UINT64 CpuArchAttributes;\r
+\r
+ if (Length == 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Map = NULL;\r
+ if (Operation & GCD_MEMORY_SPACE_OPERATION) {\r
+ CoreAcquireGcdMemoryLock ();\r
+ Map = &mGcdMemorySpaceMap;\r
+ }\r
+ if (Operation & GCD_IO_SPACE_OPERATION) {\r
+ CoreAcquireGcdIoLock ();\r
+ Map = &mGcdIoSpaceMap;\r
+ }\r
+\r
+ //\r
+ // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length\r
+ //\r
+ Status = CoreSearchGcdMapEntry (BaseAddress, Length, &StartLink, &EndLink, Map);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_UNSUPPORTED;\r
+\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Verify that the list of descriptors are unallocated non-existent memory.\r
+ //\r
+ Link = StartLink;\r
+ while (Link != EndLink->ForwardLink) {\r
+ Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+ switch (Operation) {\r
+ //\r
+ // Add operations\r
+ //\r
+ case GCD_ADD_MEMORY_OPERATION:\r
+ if (Entry->GcdMemoryType != EfiGcdMemoryTypeNonExistent ||\r
+ Entry->ImageHandle != NULL ) {\r
+ Status = EFI_ACCESS_DENIED;\r
+ goto Done;\r
+ }\r
+ break;\r
+ case GCD_ADD_IO_OPERATION:\r
+ if (Entry->GcdIoType != EfiGcdIoTypeNonExistent ||\r
+ Entry->ImageHandle != NULL ) {\r
+ Status = EFI_ACCESS_DENIED;\r
+ goto Done;\r
+ }\r
+ break;\r
+ //\r
+ // Free operations\r
+ //\r
+ case GCD_FREE_MEMORY_OPERATION:\r
+ case GCD_FREE_IO_OPERATION:\r
+ if (Entry->ImageHandle == NULL) {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+ break;\r
+ //\r
+ // Remove operations\r
+ //\r
+ case GCD_REMOVE_MEMORY_OPERATION:\r
+ if (Entry->GcdMemoryType == EfiGcdMemoryTypeNonExistent) {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+ if (Entry->ImageHandle != NULL) {\r
+ Status = EFI_ACCESS_DENIED;\r
+ goto Done;\r
+ }\r
+ break;\r
+ case GCD_REMOVE_IO_OPERATION:\r
+ if (Entry->GcdIoType == EfiGcdIoTypeNonExistent) {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+ if (Entry->ImageHandle != NULL) {\r
+ Status = EFI_ACCESS_DENIED;\r
+ goto Done;\r
+ }\r
+ break;\r
+ //\r
+ // Set attribute operations\r
+ //\r
+ case GCD_SET_ATTRIBUTES_MEMORY_OPERATION:\r
+ if (Attributes & EFI_MEMORY_RUNTIME) {\r
+ if ((BaseAddress & EFI_PAGE_MASK) != 0 || (Length & EFI_PAGE_MASK) != 0) {\r
+ Status = EFI_INVALID_PARAMETER;\r
+\r
+ goto Done;\r
+ }\r
+ }\r
+ if ((Entry->Capabilities & Attributes) != Attributes) {\r
+ Status = EFI_UNSUPPORTED;\r
+ goto Done;\r
+ }\r
+ break;\r
+ }\r
+ Link = Link->ForwardLink;\r
+ }\r
+\r
+ //\r
+ // Allocate work space to perform this operation\r
+ //\r
+ Status = CoreAllocateGcdMapEntry (&TopEntry, &BottomEntry);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ //\r
+ //\r
+ if (Operation == GCD_SET_ATTRIBUTES_MEMORY_OPERATION) {\r
+ //\r
+ // Call CPU Arch Protocol to attempt to set attributes on the range\r
+ //\r
+ CpuArchAttributes = ConverToCpuArchAttributes (Attributes);\r
+ if ( CpuArchAttributes != INVALID_CPU_ARCH_ATTRIBUTES ) {\r
+ Status = CoreLocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_ACCESS_DENIED;\r
+ goto Done;\r
+ }\r
+\r
+ Status = CpuArch->SetMemoryAttributes (\r
+ CpuArch,\r
+ BaseAddress,\r
+ Length,\r
+ CpuArchAttributes\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+ }\r
+\r
+ }\r
+\r
+ //\r
+ // Convert/Insert the list of descriptors from StartLink to EndLink\r
+ //\r
+ Link = StartLink;\r
+ while (Link != EndLink->ForwardLink) {\r
+ Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+ CoreInsertGcdMapEntry (Link, Entry, BaseAddress, Length, TopEntry, BottomEntry);\r
+ switch (Operation) {\r
+ //\r
+ // Add operations\r
+ //\r
+ case GCD_ADD_MEMORY_OPERATION:\r
+ Entry->GcdMemoryType = GcdMemoryType;\r
+ if (GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {\r
+ Entry->Capabilities = Capabilities | EFI_MEMORY_RUNTIME | EFI_MEMORY_PORT_IO;\r
+ } else {\r
+ Entry->Capabilities = Capabilities | EFI_MEMORY_RUNTIME;\r
+ }\r
+ break;\r
+ case GCD_ADD_IO_OPERATION:\r
+ Entry->GcdIoType = GcdIoType;\r
+ break;\r
+ //\r
+ // Free operations\r
+ //\r
+ case GCD_FREE_MEMORY_OPERATION:\r
+ case GCD_FREE_IO_OPERATION:\r
+ Entry->ImageHandle = NULL;\r
+ Entry->DeviceHandle = NULL;\r
+ break;\r
+ //\r
+ // Remove operations\r
+ //\r
+ case GCD_REMOVE_MEMORY_OPERATION:\r
+ Entry->GcdMemoryType = EfiGcdMemoryTypeNonExistent;\r
+ Entry->Capabilities = 0;\r
+ break;\r
+ case GCD_REMOVE_IO_OPERATION:\r
+ Entry->GcdIoType = EfiGcdIoTypeNonExistent;\r
+ break;\r
+ //\r
+ // Set attribute operations\r
+ //\r
+ case GCD_SET_ATTRIBUTES_MEMORY_OPERATION:\r
+ Entry->Attributes = Attributes;\r
+ break;\r
+ }\r
+ Link = Link->ForwardLink;\r
+ }\r
+\r
+ //\r
+ // Cleanup\r
+ //\r
+ Status = CoreCleanupGcdMapEntry (TopEntry, BottomEntry, StartLink, EndLink, Map);\r
+\r
+Done:\r
+ if (Operation & GCD_MEMORY_SPACE_OPERATION) {\r
+ CoreReleaseGcdMemoryLock ();\r
+ }\r
+ if (Operation & GCD_IO_SPACE_OPERATION) {\r
+ CoreReleaseGcdIoLock ();\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+CoreAllocateSpaceCheckEntry (\r
+ IN UINTN Operation,\r
+ IN EFI_GCD_MAP_ENTRY *Entry,\r
+ IN EFI_GCD_MEMORY_TYPE GcdMemoryType,\r
+ IN EFI_GCD_IO_TYPE GcdIoType\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Check whether an entry could be used to allocate space.\r
+\r
+Arguments:\r
+\r
+ Operation - Allocate memory or IO\r
+ \r
+ Entry - The entry to be tested\r
+ \r
+ GcdMemoryType - The desired memory type\r
+ \r
+ GcdIoType - The desired IO type\r
+ \r
+Returns:\r
+\r
+ EFI_NOT_FOUND - The memory type does not match or there's an image handle on the entry.\r
+ \r
+ EFI_UNSUPPORTED - The operation unsupported.\r
+ \r
+ EFI_SUCCESS - It's ok for this entry to be used to allocate space.\r
+\r
+--*/\r
+{\r
+ if (Entry->ImageHandle != NULL) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ switch (Operation) {\r
+ case GCD_ALLOCATE_MEMORY_OPERATION:\r
+ if (Entry->GcdMemoryType != GcdMemoryType) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ break;\r
+ case GCD_ALLOCATE_IO_OPERATION:\r
+ if (Entry->GcdIoType != GcdIoType) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ break;\r
+ default:\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+CoreAllocateSpace (\r
+ IN UINTN Operation,\r
+ IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType,\r
+ IN EFI_GCD_MEMORY_TYPE GcdMemoryType,\r
+ IN EFI_GCD_IO_TYPE GcdIoType,\r
+ IN UINTN Alignment,\r
+ IN UINT64 Length,\r
+ IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_HANDLE DeviceHandle OPTIONAL\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Allocate space on specified address and length.\r
+\r
+Arguments:\r
+\r
+ Operation - The type of operation (memory or IO)\r
+ \r
+ GcdAllocateType - The type of allocate operation\r
+ \r
+ GcdMemoryType - The desired memory type\r
+ \r
+ GcdIoType - The desired IO type\r
+ \r
+ Alignment - Align with 2^Alignment\r
+ \r
+ Length - Length to allocate\r
+ \r
+ BaseAddress - Base address to allocate\r
+ \r
+ ImageHandle - The image handle consume the allocated space.\r
+ \r
+ DeviceHandle - The device handle consume the allocated space.\r
+\r
+Returns:\r
+\r
+ EFI_INVALID_PARAMETER - Invalid parameter.\r
+ \r
+ EFI_NOT_FOUND - No descriptor for the desired space exists.\r
+ \r
+ EFI_SUCCESS - Space successfully allocated.\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_PHYSICAL_ADDRESS AlignmentMask;\r
+ EFI_PHYSICAL_ADDRESS MaxAddress;\r
+ LIST_ENTRY *Map;\r
+ LIST_ENTRY *Link;\r
+ LIST_ENTRY *SubLink;\r
+ EFI_GCD_MAP_ENTRY *Entry;\r
+ EFI_GCD_MAP_ENTRY *TopEntry;\r
+ EFI_GCD_MAP_ENTRY *BottomEntry;\r
+ LIST_ENTRY *StartLink;\r
+ LIST_ENTRY *EndLink;\r
+ BOOLEAN Found;\r
+\r
+ //\r
+ // Make sure parameters are valid\r
+ //\r
+ if (GcdAllocateType < 0 || GcdAllocateType >= EfiGcdMaxAllocateType) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ if (GcdMemoryType < 0 || GcdMemoryType >= EfiGcdMemoryTypeMaximum) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ if (GcdIoType < 0 || GcdIoType >= EfiGcdIoTypeMaximum) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ if (BaseAddress == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ if (ImageHandle == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ if (Alignment >= 64) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ if (Length == 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Map = NULL;\r
+ if (Operation & GCD_MEMORY_SPACE_OPERATION) {\r
+ CoreAcquireGcdMemoryLock ();\r
+ Map = &mGcdMemorySpaceMap;\r
+ }\r
+ if (Operation & GCD_IO_SPACE_OPERATION) {\r
+ CoreAcquireGcdIoLock ();\r
+ Map = &mGcdIoSpaceMap;\r
+ }\r
+\r
+ Found = FALSE;\r
+ StartLink = NULL;\r
+ EndLink = NULL;\r
+ //\r
+ // Compute alignment bit mask\r
+ //\r
+ AlignmentMask = LShiftU64 (1, Alignment) - 1;\r
+\r
+ if (GcdAllocateType == EfiGcdAllocateAddress) {\r
+ //\r
+ // Verify that the BaseAddress passed in is aligned correctly\r
+ //\r
+ if ((*BaseAddress & AlignmentMask) != 0) {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length\r
+ //\r
+ Status = CoreSearchGcdMapEntry (*BaseAddress, Length, &StartLink, &EndLink, Map);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.\r
+ //\r
+ Link = StartLink;\r
+ while (Link != EndLink->ForwardLink) {\r
+ Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+ Link = Link->ForwardLink;\r
+ Status = CoreAllocateSpaceCheckEntry (Operation, Entry, GcdMemoryType, GcdIoType);\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+ }\r
+ Found = TRUE;\r
+ } else {\r
+\r
+ Entry = CR (Map->BackLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+\r
+ //\r
+ // Compute the maximum address to use in the search algorithm\r
+ //\r
+ if (GcdAllocateType == EfiGcdAllocateMaxAddressSearchBottomUp ||\r
+ GcdAllocateType == EfiGcdAllocateMaxAddressSearchTopDown ) {\r
+ MaxAddress = *BaseAddress;\r
+ } else {\r
+ MaxAddress = Entry->EndAddress;\r
+ }\r
+\r
+ //\r
+ // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.\r
+ //\r
+ if (GcdAllocateType == EfiGcdAllocateMaxAddressSearchTopDown ||\r
+ GcdAllocateType == EfiGcdAllocateAnySearchTopDown ) {\r
+ Link = Map->BackLink;\r
+ } else {\r
+ Link = Map->ForwardLink;\r
+ }\r
+ while (Link != Map) {\r
+ Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+\r
+ if (GcdAllocateType == EfiGcdAllocateMaxAddressSearchTopDown ||\r
+ GcdAllocateType == EfiGcdAllocateAnySearchTopDown ) {\r
+ Link = Link->BackLink;\r
+ } else {\r
+ Link = Link->ForwardLink;\r
+ }\r
+\r
+ Status = CoreAllocateSpaceCheckEntry (Operation, Entry, GcdMemoryType, GcdIoType);\r
+ if (EFI_ERROR (Status)) {\r
+ continue;\r
+ }\r
+\r
+ if (GcdAllocateType == EfiGcdAllocateMaxAddressSearchTopDown ||\r
+ GcdAllocateType == EfiGcdAllocateAnySearchTopDown ) {\r
+ if ((Entry->BaseAddress + Length) > MaxAddress) {\r
+ continue;\r
+ }\r
+ if (Length > (Entry->EndAddress + 1)) {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+ if (Entry->EndAddress > MaxAddress) {\r
+ *BaseAddress = MaxAddress;\r
+ } else {\r
+ *BaseAddress = Entry->EndAddress;\r
+ }\r
+ *BaseAddress = (*BaseAddress + 1 - Length) & (~AlignmentMask);\r
+ } else {\r
+ *BaseAddress = (Entry->BaseAddress + AlignmentMask) & (~AlignmentMask);\r
+ if ((*BaseAddress + Length - 1) > MaxAddress) {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Search for the list of descriptors that cover the range BaseAddress to BaseAddress+Length\r
+ //\r
+ Status = CoreSearchGcdMapEntry (*BaseAddress, Length, &StartLink, &EndLink, Map);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+\r
+ Link = StartLink;\r
+ //\r
+ // Verify that the list of descriptors are unallocated memory matching GcdMemoryType.\r
+ //\r
+ Found = TRUE;\r
+ SubLink = StartLink;\r
+ while (SubLink != EndLink->ForwardLink) {\r
+ Entry = CR (SubLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+ Status = CoreAllocateSpaceCheckEntry (Operation, Entry, GcdMemoryType, GcdIoType);\r
+ if (EFI_ERROR (Status)) {\r
+ Link = SubLink;\r
+ Found = FALSE;\r
+ break;\r
+ }\r
+ SubLink = SubLink->ForwardLink;\r
+ }\r
+ if (Found) {\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ if (!Found) {\r
+ Status = EFI_NOT_FOUND;\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Allocate work space to perform this operation\r
+ //\r
+ Status = CoreAllocateGcdMapEntry (&TopEntry, &BottomEntry);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Convert/Insert the list of descriptors from StartLink to EndLink\r
+ //\r
+ Link = StartLink;\r
+ while (Link != EndLink->ForwardLink) {\r
+ Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+ CoreInsertGcdMapEntry (Link, Entry, *BaseAddress, Length, TopEntry, BottomEntry);\r
+ Entry->ImageHandle = ImageHandle;\r
+ Entry->DeviceHandle = DeviceHandle;\r
+ Link = Link->ForwardLink;\r
+ }\r
+\r
+ //\r
+ // Cleanup\r
+ //\r
+ Status = CoreCleanupGcdMapEntry (TopEntry, BottomEntry, StartLink, EndLink, Map);\r
+\r
+Done:\r
+ if (Operation & GCD_MEMORY_SPACE_OPERATION) {\r
+ CoreReleaseGcdMemoryLock ();\r
+ }\r
+ if (Operation & GCD_IO_SPACE_OPERATION) {\r
+ CoreReleaseGcdIoLock ();\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+CoreInternalAddMemorySpace (\r
+ IN EFI_GCD_MEMORY_TYPE GcdMemoryType,\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length,\r
+ IN UINT64 Capabilities\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Add a segment of memory to GCD map.\r
+\r
+Arguments:\r
+\r
+ GcdMemoryType - Memory type of the segment.\r
+ \r
+ BaseAddress - Base address of the segment.\r
+ \r
+ Length - Length of the segment.\r
+ \r
+ Capabilities - alterable attributes of the segment.\r
+\r
+Returns:\r
+\r
+ EFI_INVALID_PARAMETER - Invalid parameters.\r
+ \r
+ EFI_SUCCESS - Successfully add a segment of memory space.\r
+\r
+--*/\r
+{\r
+ //\r
+ // Make sure parameters are valid\r
+ //\r
+ if (GcdMemoryType <= EfiGcdMemoryTypeNonExistent || GcdMemoryType >= EfiGcdMemoryTypeMaximum) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ return CoreConvertSpace (GCD_ADD_MEMORY_OPERATION, GcdMemoryType, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, Capabilities, 0);\r
+}\r
+\r
+//\r
+// GCD Core Services\r
+//\r
+EFI_STATUS\r
+CoreAllocateMemorySpace (\r
+ IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType,\r
+ IN EFI_GCD_MEMORY_TYPE GcdMemoryType,\r
+ IN UINTN Alignment,\r
+ IN UINT64 Length,\r
+ IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_HANDLE DeviceHandle OPTIONAL\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Allocates nonexistent memory, reserved memory, system memory, or memorymapped\r
+I/O resources from the global coherency domain of the processor.\r
+\r
+Arguments:\r
+\r
+ GcdAllocateType - The type of allocate operation\r
+ \r
+ GcdMemoryType - The desired memory type\r
+ \r
+ Alignment - Align with 2^Alignment\r
+ \r
+ Length - Length to allocate\r
+ \r
+ BaseAddress - Base address to allocate\r
+ \r
+ ImageHandle - The image handle consume the allocated space.\r
+ \r
+ DeviceHandle - The device handle consume the allocated space.\r
+\r
+Returns:\r
+\r
+ EFI_INVALID_PARAMETER - Invalid parameter.\r
+ \r
+ EFI_NOT_FOUND - No descriptor contains the desired space.\r
+ \r
+ EFI_SUCCESS - Memory space successfully allocated.\r
+\r
+--*/\r
+{\r
+ return CoreAllocateSpace (\r
+ GCD_ALLOCATE_MEMORY_OPERATION, \r
+ GcdAllocateType, \r
+ GcdMemoryType, \r
+ (EFI_GCD_IO_TYPE) 0, \r
+ Alignment, \r
+ Length, \r
+ BaseAddress, \r
+ ImageHandle, \r
+ DeviceHandle\r
+ );\r
+}\r
+\r
+EFI_STATUS\r
+CoreAddMemorySpace (\r
+ IN EFI_GCD_MEMORY_TYPE GcdMemoryType,\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length,\r
+ IN UINT64 Capabilities\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Adds reserved memory, system memory, or memory-mapped I/O resources to the\r
+global coherency domain of the processor.\r
+\r
+Arguments:\r
+\r
+ GcdMemoryType - Memory type of the memory space.\r
+ \r
+ BaseAddress - Base address of the memory space.\r
+ \r
+ Length - Length of the memory space.\r
+ \r
+ Capabilities - alterable attributes of the memory space.\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - Merged this memory space into GCD map. \r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_PHYSICAL_ADDRESS PageBaseAddress;\r
+ UINT64 PageLength;\r
+\r
+ Status = CoreInternalAddMemorySpace (GcdMemoryType, BaseAddress, Length, Capabilities);\r
+\r
+ if (!EFI_ERROR (Status) && GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {\r
+\r
+ PageBaseAddress = PageAlignLength (BaseAddress);\r
+ PageLength = PageAlignLength (BaseAddress + Length - PageBaseAddress);\r
+\r
+ Status = CoreAllocateMemorySpace (\r
+ EfiGcdAllocateAddress,\r
+ GcdMemoryType,\r
+ EFI_PAGE_SHIFT, \r
+ PageLength,\r
+ &PageBaseAddress,\r
+ gDxeCoreImageHandle,\r
+ NULL\r
+ );\r
+\r
+ if (!EFI_ERROR (Status)) {\r
+ CoreAddMemoryDescriptor (\r
+ EfiConventionalMemory,\r
+ PageBaseAddress,\r
+ RShiftU64 (PageLength, EFI_PAGE_SHIFT),\r
+ Capabilities\r
+ );\r
+ } else {\r
+ for (; PageLength != 0; PageLength -= EFI_PAGE_SIZE, PageBaseAddress += EFI_PAGE_SIZE) {\r
+ Status = CoreAllocateMemorySpace (\r
+ EfiGcdAllocateAddress,\r
+ GcdMemoryType,\r
+ EFI_PAGE_SHIFT, \r
+ EFI_PAGE_SIZE,\r
+ &PageBaseAddress,\r
+ gDxeCoreImageHandle,\r
+ NULL\r
+ );\r
+\r
+ if (!EFI_ERROR (Status)) {\r
+ CoreAddMemoryDescriptor (\r
+ EfiConventionalMemory,\r
+ PageBaseAddress,\r
+ 1,\r
+ Capabilities\r
+ );\r
+ }\r
+ }\r
+ }\r
+ }\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+CoreFreeMemorySpace (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Frees nonexistent memory, reserved memory, system memory, or memory-mapped\r
+I/O resources from the global coherency domain of the processor.\r
+\r
+Arguments:\r
+\r
+ BaseAddress - Base address of the memory space.\r
+ \r
+ Length - Length of the memory space.\r
+ \r
+Returns:\r
+\r
+ EFI_SUCCESS - Space successfully freed.\r
+\r
+--*/\r
+{\r
+ return CoreConvertSpace (GCD_FREE_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);\r
+}\r
+\r
+EFI_STATUS\r
+CoreRemoveMemorySpace (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Removes reserved memory, system memory, or memory-mapped I/O resources from\r
+the global coherency domain of the processor.\r
+\r
+Arguments:\r
+\r
+ BaseAddress - Base address of the memory space.\r
+ \r
+ Length - Length of the memory space.\r
+ \r
+Returns:\r
+\r
+ EFI_SUCCESS - Successfully remove a segment of memory space.\r
+\r
+--*/\r
+{\r
+ return CoreConvertSpace (GCD_REMOVE_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);\r
+}\r
+\r
+STATIC\r
+VOID\r
+BuildMemoryDescriptor (\r
+ IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor,\r
+ IN EFI_GCD_MAP_ENTRY *Entry\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Build a memory descriptor according to an entry.\r
+\r
+Arguments:\r
+\r
+ Descriptor - The descriptor to be built\r
+ \r
+ Entry - According to this entry\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+{\r
+ Descriptor->BaseAddress = Entry->BaseAddress;\r
+ Descriptor->Length = Entry->EndAddress - Entry->BaseAddress + 1;\r
+ Descriptor->Capabilities = Entry->Capabilities;\r
+ Descriptor->Attributes = Entry->Attributes;\r
+ Descriptor->GcdMemoryType = Entry->GcdMemoryType;\r
+ Descriptor->ImageHandle = Entry->ImageHandle;\r
+ Descriptor->DeviceHandle = Entry->DeviceHandle;\r
+}\r
+\r
+EFI_STATUS\r
+CoreGetMemorySpaceDescriptor (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Retrieves the descriptor for a memory region containing a specified address.\r
+\r
+Arguments:\r
+\r
+ BaseAddress - Specified start address\r
+ \r
+ Descriptor - Specified length\r
+\r
+Returns:\r
+\r
+ EFI_INVALID_PARAMETER - Invalid parameter\r
+ \r
+ EFI_SUCCESS - Successfully get memory space descriptor.\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ LIST_ENTRY *StartLink;\r
+ LIST_ENTRY *EndLink;\r
+ EFI_GCD_MAP_ENTRY *Entry;\r
+\r
+ //\r
+ // Make sure parameters are valid\r
+ //\r
+ if (Descriptor == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ CoreAcquireGcdMemoryLock ();\r
+\r
+ //\r
+ // Search for the list of descriptors that contain BaseAddress \r
+ //\r
+ Status = CoreSearchGcdMapEntry (BaseAddress, 1, &StartLink, &EndLink, &mGcdMemorySpaceMap);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_NOT_FOUND;\r
+ } else {\r
+ //\r
+ // Copy the contents of the found descriptor into Descriptor\r
+ //\r
+ Entry = CR (StartLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+ BuildMemoryDescriptor (Descriptor, Entry);\r
+ }\r
+\r
+ CoreReleaseGcdMemoryLock ();\r
+\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+CoreSetMemorySpaceAttributes (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length,\r
+ IN UINT64 Attributes\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Modifies the attributes for a memory region in the global coherency domain of the\r
+processor.\r
+\r
+Arguments:\r
+\r
+ BaseAddress - Specified start address\r
+ \r
+ Length - Specified length\r
+ \r
+ Attributes - Specified attributes\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - Successfully set attribute of a segment of memory space.\r
+\r
+--*/\r
+{\r
+ return CoreConvertSpace (GCD_SET_ATTRIBUTES_MEMORY_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, Attributes);\r
+}\r
+\r
+EFI_STATUS\r
+CoreGetMemorySpaceMap (\r
+ OUT UINTN *NumberOfDescriptors,\r
+ OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR **MemorySpaceMap\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Returns a map of the memory resources in the global coherency domain of the\r
+processor.\r
+\r
+Arguments:\r
+\r
+ NumberOfDescriptors - Number of descriptors.\r
+ \r
+ MemorySpaceMap - Descriptor array\r
+\r
+Returns:\r
+\r
+ EFI_INVALID_PARAMETER - Invalid parameter\r
+ \r
+ EFI_OUT_OF_RESOURCES - No enough buffer to allocate\r
+ \r
+ EFI_SUCCESS - Successfully get memory space map.\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ LIST_ENTRY *Link;\r
+ EFI_GCD_MAP_ENTRY *Entry;\r
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor;\r
+\r
+ //\r
+ // Make sure parameters are valid\r
+ //\r
+ if (NumberOfDescriptors == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ if (MemorySpaceMap == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ CoreAcquireGcdMemoryLock ();\r
+\r
+ //\r
+ // Count the number of descriptors\r
+ //\r
+ *NumberOfDescriptors = CoreCountGcdMapEntry (&mGcdMemorySpaceMap);\r
+\r
+ //\r
+ // Allocate the MemorySpaceMap\r
+ //\r
+ *MemorySpaceMap = CoreAllocateBootServicesPool (*NumberOfDescriptors * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR));\r
+ if (*MemorySpaceMap == NULL) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Fill in the MemorySpaceMap\r
+ //\r
+ Descriptor = *MemorySpaceMap;\r
+ Link = mGcdMemorySpaceMap.ForwardLink;\r
+ while (Link != &mGcdMemorySpaceMap) {\r
+ Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+ BuildMemoryDescriptor (Descriptor, Entry);\r
+ Descriptor++;\r
+ Link = Link->ForwardLink;\r
+ }\r
+ Status = EFI_SUCCESS;\r
+\r
+Done:\r
+ CoreReleaseGcdMemoryLock ();\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+CoreAddIoSpace (\r
+ IN EFI_GCD_IO_TYPE GcdIoType,\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Adds reserved I/O or I/O resources to the global coherency domain of the processor.\r
+\r
+Arguments:\r
+\r
+ GcdIoType - IO type of the segment.\r
+ \r
+ BaseAddress - Base address of the segment.\r
+ \r
+ Length - Length of the segment.\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - Merged this segment into GCD map.\r
+ EFI_INVALID_PARAMETER - Parameter not valid\r
+\r
+--*/\r
+{\r
+ //\r
+ // Make sure parameters are valid\r
+ //\r
+ if (GcdIoType <= EfiGcdIoTypeNonExistent || GcdIoType >= EfiGcdIoTypeMaximum) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ return CoreConvertSpace (GCD_ADD_IO_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, GcdIoType, BaseAddress, Length, 0, 0);\r
+}\r
+\r
+EFI_STATUS\r
+CoreAllocateIoSpace (\r
+ IN EFI_GCD_ALLOCATE_TYPE GcdAllocateType,\r
+ IN EFI_GCD_IO_TYPE GcdIoType,\r
+ IN UINTN Alignment,\r
+ IN UINT64 Length,\r
+ IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_HANDLE DeviceHandle OPTIONAL\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Allocates nonexistent I/O, reserved I/O, or I/O resources from the global coherency\r
+domain of the processor.\r
+\r
+Arguments:\r
+\r
+ GcdAllocateType - The type of allocate operation\r
+ \r
+ GcdIoType - The desired IO type\r
+ \r
+ Alignment - Align with 2^Alignment\r
+ \r
+ Length - Length to allocate\r
+ \r
+ BaseAddress - Base address to allocate\r
+ \r
+ ImageHandle - The image handle consume the allocated space.\r
+ \r
+ DeviceHandle - The device handle consume the allocated space.\r
+\r
+Returns:\r
+\r
+ EFI_INVALID_PARAMETER - Invalid parameter.\r
+ \r
+ EFI_NOT_FOUND - No descriptor contains the desired space.\r
+ \r
+ EFI_SUCCESS - IO space successfully allocated.\r
+\r
+--*/\r
+{\r
+ return CoreAllocateSpace (\r
+ GCD_ALLOCATE_IO_OPERATION, \r
+ GcdAllocateType, \r
+ (EFI_GCD_MEMORY_TYPE) 0, \r
+ GcdIoType, \r
+ Alignment, \r
+ Length, \r
+ BaseAddress, \r
+ ImageHandle, \r
+ DeviceHandle\r
+ );\r
+}\r
+\r
+EFI_STATUS\r
+CoreFreeIoSpace (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Frees nonexistent I/O, reserved I/O, or I/O resources from the global coherency\r
+domain of the processor.\r
+\r
+Arguments:\r
+\r
+ BaseAddress - Base address of the segment.\r
+ \r
+ Length - Length of the segment.\r
+ \r
+Returns:\r
+\r
+ EFI_SUCCESS - Space successfully freed.\r
+\r
+--*/\r
+{\r
+ return CoreConvertSpace (GCD_FREE_IO_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);\r
+}\r
+\r
+EFI_STATUS\r
+CoreRemoveIoSpace (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ IN UINT64 Length\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Removes reserved I/O or I/O resources from the global coherency domain of the\r
+processor.\r
+\r
+Arguments:\r
+\r
+ BaseAddress - Base address of the segment.\r
+ \r
+ Length - Length of the segment.\r
+ \r
+Returns:\r
+\r
+ EFI_SUCCESS - Successfully removed a segment of IO space.\r
+\r
+--*/\r
+{\r
+ return CoreConvertSpace (GCD_REMOVE_IO_OPERATION, (EFI_GCD_MEMORY_TYPE) 0, (EFI_GCD_IO_TYPE) 0, BaseAddress, Length, 0, 0);\r
+}\r
+\r
+STATIC\r
+VOID\r
+BuildIoDescriptor (\r
+ IN EFI_GCD_IO_SPACE_DESCRIPTOR *Descriptor,\r
+ IN EFI_GCD_MAP_ENTRY *Entry\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Build a IO descriptor according to an entry.\r
+\r
+Arguments:\r
+\r
+ Descriptor - The descriptor to be built\r
+ \r
+ Entry - According to this entry\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+{\r
+ Descriptor->BaseAddress = Entry->BaseAddress;\r
+ Descriptor->Length = Entry->EndAddress - Entry->BaseAddress + 1;\r
+ Descriptor->GcdIoType = Entry->GcdIoType;\r
+ Descriptor->ImageHandle = Entry->ImageHandle;\r
+ Descriptor->DeviceHandle = Entry->DeviceHandle;\r
+}\r
+\r
+EFI_STATUS\r
+CoreGetIoSpaceDescriptor (\r
+ IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
+ OUT EFI_GCD_IO_SPACE_DESCRIPTOR *Descriptor\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Retrieves the descriptor for an I/O region containing a specified address.\r
+\r
+Arguments:\r
+\r
+ BaseAddress - Specified start address\r
+ \r
+ Descriptor - Specified length\r
+\r
+Returns:\r
+\r
+ EFI_INVALID_PARAMETER - Descriptor is NULL.\r
+ \r
+ EFI_SUCCESS - Successfully get the IO space descriptor.\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ LIST_ENTRY *StartLink;\r
+ LIST_ENTRY *EndLink;\r
+ EFI_GCD_MAP_ENTRY *Entry;\r
+\r
+ //\r
+ // Make sure parameters are valid\r
+ //\r
+ if (Descriptor == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ CoreAcquireGcdIoLock ();\r
+\r
+ //\r
+ // Search for the list of descriptors that contain BaseAddress \r
+ //\r
+ Status = CoreSearchGcdMapEntry (BaseAddress, 1, &StartLink, &EndLink, &mGcdIoSpaceMap);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_NOT_FOUND;\r
+ } else {\r
+ //\r
+ // Copy the contents of the found descriptor into Descriptor\r
+ //\r
+ Entry = CR (StartLink, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+ BuildIoDescriptor (Descriptor, Entry);\r
+ }\r
+\r
+ CoreReleaseGcdIoLock ();\r
+\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+CoreGetIoSpaceMap (\r
+ OUT UINTN *NumberOfDescriptors,\r
+ OUT EFI_GCD_IO_SPACE_DESCRIPTOR **IoSpaceMap\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Returns a map of the I/O resources in the global coherency domain of the processor.\r
+\r
+Arguments:\r
+\r
+ NumberOfDescriptors - Number of descriptors.\r
+ \r
+ IoSpaceMap - Descriptor array\r
+\r
+Returns:\r
+\r
+ EFI_INVALID_PARAMETER - Invalid parameter\r
+ \r
+ EFI_OUT_OF_RESOURCES - No enough buffer to allocate\r
+ \r
+ EFI_SUCCESS - Successfully get IO space map.\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ LIST_ENTRY *Link;\r
+ EFI_GCD_MAP_ENTRY *Entry;\r
+ EFI_GCD_IO_SPACE_DESCRIPTOR *Descriptor;\r
+\r
+ //\r
+ // Make sure parameters are valid\r
+ //\r
+ if (NumberOfDescriptors == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ if (IoSpaceMap == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ CoreAcquireGcdIoLock ();\r
+\r
+ //\r
+ // Count the number of descriptors\r
+ //\r
+ *NumberOfDescriptors = CoreCountGcdMapEntry (&mGcdIoSpaceMap);\r
+\r
+ //\r
+ // Allocate the IoSpaceMap\r
+ //\r
+ *IoSpaceMap = CoreAllocateBootServicesPool (*NumberOfDescriptors * sizeof (EFI_GCD_IO_SPACE_DESCRIPTOR));\r
+ if (*IoSpaceMap == NULL) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Fill in the IoSpaceMap\r
+ //\r
+ Descriptor = *IoSpaceMap;\r
+ Link = mGcdIoSpaceMap.ForwardLink;\r
+ while (Link != &mGcdIoSpaceMap) {\r
+ Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
+ BuildIoDescriptor (Descriptor, Entry);\r
+ Descriptor++;\r
+ Link = Link->ForwardLink;\r
+ }\r
+ Status = EFI_SUCCESS;\r
+\r
+Done:\r
+ CoreReleaseGcdIoLock ();\r
+ return Status;\r
+} \r
+\r
+STATIC\r
+UINT64\r
+CoreConvertResourceDescriptorHobAttributesToCapabilities (\r
+ EFI_GCD_MEMORY_TYPE GcdMemoryType,\r
+ UINT64 Attributes\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Converts a Resource Descriptor HOB attributes mask to an EFI Memory Descriptor \r
+ capabilities mask\r
+\r
+Arguments:\r
+\r
+ GcdMemoryType - Type of resource in the GCD memory map.\r
+ Attributes - The attribute mask in the Resource Descriptor HOB.\r
+\r
+Returns:\r
+\r
+ The capabilities mask for an EFI Memory Descriptor.\r
+\r
+--*/\r
+{\r
+ UINT64 Capabilities;\r
+ GCD_ATTRIBUTE_CONVERSION_ENTRY *Conversion;\r
+ \r
+ //\r
+ // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask\r
+ //\r
+ for (Capabilities = 0, Conversion = mAttributeConversionTable; Conversion->Attribute != 0; Conversion++) {\r
+ if (Conversion->Memory || (GcdMemoryType != EfiGcdMemoryTypeSystemMemory)) {\r
+ if (Attributes & Conversion->Attribute) {\r
+ Capabilities |= Conversion->Capability;\r
+ }\r
+ }\r
+ }\r
+ \r
+ return Capabilities;\r
+}\r
+\r
+EFI_STATUS\r
+CoreInitializeMemoryServices (\r
+ IN VOID **HobStart,\r
+ OUT EFI_PHYSICAL_ADDRESS *MemoryBaseAddress,\r
+ OUT UINT64 *MemoryLength\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ External function. Initializes the GCD and memory services based on the memory \r
+ descriptor HOBs. This function is responsible for priming the GCD map and the\r
+ memory map, so memory allocations and resource allocations can be made. The first\r
+ part of this function can not depend on any memory services until at least one\r
+ memory descriptor is provided to the memory services. Then the memory services\r
+ can be used to intialize the GCD map.\r
+\r
+Arguments:\r
+\r
+ HobStart - The start address of the HOB.\r
+ MemoryBaseAddress - Start address of memory region found to init DXE core.\r
+ MemoryLength - Length of memory region found to init DXE core.\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - Memory services successfully initialized.\r
+\r
+--*/\r
+{\r
+ EFI_PEI_HOB_POINTERS Hob;\r
+ EFI_MEMORY_TYPE_INFORMATION *EfiMemoryTypeInformation;\r
+ UINTN DataSize;\r
+ BOOLEAN Found;\r
+ EFI_HOB_HANDOFF_INFO_TABLE *PhitHob;\r
+ EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;\r
+ EFI_HOB_RESOURCE_DESCRIPTOR *PhitResourceHob;\r
+ EFI_PHYSICAL_ADDRESS BaseAddress;\r
+ UINT64 Length;\r
+ UINT64 Attributes;\r
+ UINT64 Capabilities;\r
+ EFI_PHYSICAL_ADDRESS MaxMemoryBaseAddress;\r
+ UINT64 MaxMemoryLength;\r
+ UINT64 MaxMemoryAttributes;\r
+ EFI_PHYSICAL_ADDRESS MaxAddress;\r
+ EFI_PHYSICAL_ADDRESS HighAddress;\r
+ EFI_HOB_RESOURCE_DESCRIPTOR *MaxResourceHob;\r
+ EFI_HOB_GUID_TYPE *GuidHob;\r
+\r
+ //\r
+ // Point at the first HOB. This must be the PHIT HOB.\r
+ //\r
+ Hob.Raw = *HobStart;\r
+ ASSERT (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_HANDOFF);\r
+\r
+ //\r
+ // Initialize the spin locks and maps in the memory services.\r
+ // Also fill in the memory services into the EFI Boot Services Table\r
+ //\r
+ CoreInitializePool ();\r
+\r
+ //\r
+ // Initialize Local Variables\r
+ //\r
+ PhitResourceHob = NULL;\r
+ MaxResourceHob = NULL;\r
+ ResourceHob = NULL;\r
+ BaseAddress = 0;\r
+ Length = 0;\r
+ Attributes = 0;\r
+ MaxMemoryBaseAddress = 0;\r
+ MaxMemoryLength = 0;\r
+ MaxMemoryAttributes = 0;\r
+\r
+ //\r
+ // Cache the PHIT HOB for later use\r
+ //\r
+ PhitHob = Hob.HandoffInformationTable;\r
+\r
+ //\r
+ // See if a Memory Type Information HOB is available\r
+ //\r
+ GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);\r
+ if (GuidHob != NULL) {\r
+ EfiMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);\r
+ DataSize = GET_GUID_HOB_DATA_SIZE (GuidHob);\r
+ if (EfiMemoryTypeInformation != NULL && DataSize > 0 && DataSize <= (EfiMaxMemoryType + 1) * sizeof (EFI_MEMORY_TYPE_INFORMATION)) {\r
+ CopyMem (&gMemoryTypeInformation, EfiMemoryTypeInformation, DataSize);\r
+ }\r
+ }\r
+\r
+ //\r
+ // Find the Resource Descriptor HOB that contains range FreeMemoryBaseAddress..FreeMemoryLength\r
+ //\r
+ Length = 0;\r
+ Found = FALSE;\r
+ for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
+\r
+ if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
+\r
+ ResourceHob = Hob.ResourceDescriptor;\r
+\r
+ if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
+ (ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES ) {\r
+\r
+ if (PhitHob->EfiFreeMemoryBottom >= ResourceHob->PhysicalStart && \r
+ PhitHob->EfiFreeMemoryTop <= (ResourceHob->PhysicalStart + ResourceHob->ResourceLength) ) {\r
+\r
+ //\r
+ // Cache the resource descriptor HOB for the memory region described by the PHIT HOB\r
+ //\r
+ PhitResourceHob = ResourceHob;\r
+ Found = TRUE;\r
+\r
+ Attributes = PhitResourceHob->ResourceAttribute;\r
+ BaseAddress = PageAlignAddress (PhitHob->EfiMemoryTop);\r
+ Length = PageAlignLength (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - BaseAddress);\r
+ if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {\r
+ BaseAddress = PageAlignAddress (PhitHob->EfiFreeMemoryBottom);\r
+ Length = PageAlignLength (PhitHob->EfiFreeMemoryTop - BaseAddress);\r
+ if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {\r
+ BaseAddress = PageAlignAddress (ResourceHob->PhysicalStart);\r
+ Length = PageAlignLength ((UINT64)((UINTN)*HobStart - BaseAddress));\r
+ }\r
+ }\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ //\r
+ // Assert if a resource descriptor HOB for the memory region described by the PHIT was not found\r
+ //\r
+ ASSERT (Found);\r
+\r
+ //\r
+ // Search all the resource descriptor HOBs from the highest possible addresses down for a memory\r
+ // region that is big enough to initialize the DXE core. Always skip the PHIT Resource HOB.\r
+ // The max address must be within the physically addressible range for the processor.\r
+ //\r
+ MaxMemoryLength = 0;\r
+ MaxAddress = EFI_MAX_ADDRESS;\r
+ do {\r
+ HighAddress = 0;\r
+ Found = FALSE;\r
+ //\r
+ // Search for a tested memory region that is below MaxAddress\r
+ //\r
+ for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
+\r
+ //\r
+ // See if this is a resource descriptor HOB that does not contain the PHIT.\r
+ //\r
+ if (Hob.ResourceDescriptor != PhitResourceHob && GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
+\r
+ ResourceHob = Hob.ResourceDescriptor;\r
+ //\r
+ // See if this resource descrior HOB describes tested system memory below MaxAddress\r
+ //\r
+ if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
+ (ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES &&\r
+ ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MaxAddress ) {\r
+\r
+ //\r
+ // See if this is the highest tested system memory region below MaxAddress\r
+ //\r
+ if (ResourceHob->PhysicalStart > HighAddress) {\r
+\r
+ MaxResourceHob = ResourceHob;\r
+ HighAddress = MaxResourceHob->PhysicalStart;\r
+ Found = TRUE;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ if (Found) {\r
+ //\r
+ // Compute the size of the tested memory region below MaxAddrees\r
+ //\r
+ MaxMemoryBaseAddress = PageAlignAddress (MaxResourceHob->PhysicalStart);\r
+ MaxMemoryLength = PageAlignLength (MaxResourceHob->PhysicalStart + MaxResourceHob->ResourceLength - MaxMemoryBaseAddress);\r
+ MaxMemoryAttributes = MaxResourceHob->ResourceAttribute;\r
+ }\r
+ MaxAddress = ResourceHob->PhysicalStart;\r
+ } while (Found && MaxMemoryLength < MINIMUM_INITIAL_MEMORY_SIZE);\r
+\r
+ //\r
+ //\r
+ //\r
+ if ((Length < MINIMUM_INITIAL_MEMORY_SIZE) ||\r
+ (MaxMemoryBaseAddress > BaseAddress && MaxMemoryLength >= MINIMUM_INITIAL_MEMORY_SIZE) ) {\r
+ BaseAddress = MaxMemoryBaseAddress;\r
+ Length = MaxMemoryLength;\r
+ Attributes = MaxMemoryAttributes;\r
+ }\r
+\r
+ //\r
+ // If no memory regions are found that are big enough to initialize the DXE core, then ASSERT().\r
+ //\r
+ ASSERT (Length >= MINIMUM_INITIAL_MEMORY_SIZE);\r
+\r
+ //\r
+ // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask\r
+ //\r
+ Capabilities = CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeSystemMemory, Attributes);\r
+\r
+ //\r
+ // Declare the very first memory region, so the EFI Memory Services are available.\r
+ //\r
+ CoreAddMemoryDescriptor (\r
+ EfiConventionalMemory,\r
+ BaseAddress,\r
+ RShiftU64 (Length, EFI_PAGE_SHIFT),\r
+ Capabilities\r
+ );\r
+\r
+ *MemoryBaseAddress = BaseAddress;\r
+ *MemoryLength = Length;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+CoreInitializeGcdServices (\r
+ IN VOID **HobStart,\r
+ IN EFI_PHYSICAL_ADDRESS MemoryBaseAddress,\r
+ IN UINT64 MemoryLength\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ External function. Initializes the GCD and memory services based on the memory \r
+ descriptor HOBs. This function is responsible for priming the GCD map and the\r
+ memory map, so memory allocations and resource allocations can be made. The first\r
+ part of this function can not depend on any memory services until at least one\r
+ memory descriptor is provided to the memory services. Then the memory services\r
+ can be used to intialize the GCD map.\r
+\r
+Arguments:\r
+\r
+ HobStart - The start address of the HOB\r
+\r
+ MemoryBaseAddress - Start address of memory region found to init DXE core.\r
+ \r
+ MemoryLength - Length of memory region found to init DXE core.\r
+\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - GCD services successfully initialized.\r
+\r
+--*/\r
+{\r
+ EFI_PEI_HOB_POINTERS Hob;\r
+ VOID *NewHobList;\r
+ EFI_HOB_HANDOFF_INFO_TABLE *PhitHob;\r
+ UINT8 SizeOfMemorySpace;\r
+ UINT8 SizeOfIoSpace;\r
+ EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;\r
+ EFI_PHYSICAL_ADDRESS BaseAddress;\r
+ UINT64 Length;\r
+ EFI_STATUS Status;\r
+ EFI_GCD_MAP_ENTRY *Entry;\r
+ EFI_GCD_MEMORY_TYPE GcdMemoryType;\r
+ EFI_GCD_IO_TYPE GcdIoType;\r
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;\r
+ EFI_HOB_MEMORY_ALLOCATION *MemoryHob;\r
+ EFI_HOB_FIRMWARE_VOLUME *FirmwareVolumeHob;\r
+ UINTN NumberOfDescriptors;\r
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;\r
+ UINTN Index;\r
+ UINT64 Capabilities;\r
+ EFI_HOB_CPU * CpuHob;\r
+ //\r
+ // Cache the PHIT HOB for later use\r
+ //\r
+ PhitHob = (EFI_HOB_HANDOFF_INFO_TABLE *)(*HobStart);\r
+\r
+ //\r
+ // Get the number of address lines in the I/O and Memory space for the CPU\r
+ //\r
+ CpuHob = GetFirstHob (EFI_HOB_TYPE_CPU);\r
+ ASSERT (CpuHob != NULL);\r
+ SizeOfMemorySpace = CpuHob->SizeOfMemorySpace;\r
+ SizeOfIoSpace = CpuHob->SizeOfIoSpace;\r
+ \r
+ //\r
+ // Initialize the GCD Memory Space Map\r
+ //\r
+ Entry = CoreAllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY), &mGcdMemorySpaceMapEntryTemplate);\r
+ ASSERT (Entry != NULL);\r
+\r
+ Entry->EndAddress = LShiftU64 (1, SizeOfMemorySpace) - 1;\r
+\r
+ InsertHeadList (&mGcdMemorySpaceMap, &Entry->Link);\r
+\r
+ //\r
+ // Initialize the GCD I/O Space Map\r
+ //\r
+ Entry = CoreAllocateCopyPool (sizeof (EFI_GCD_MAP_ENTRY), &mGcdIoSpaceMapEntryTemplate);\r
+ ASSERT (Entry != NULL);\r
+\r
+ Entry->EndAddress = LShiftU64 (1, SizeOfIoSpace) - 1;\r
+\r
+ InsertHeadList (&mGcdIoSpaceMap, &Entry->Link);\r
+\r
+ //\r
+ // Walk the HOB list and add all resource descriptors to the GCD \r
+ //\r
+ for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
+\r
+ GcdMemoryType = EfiGcdMemoryTypeNonExistent;\r
+ GcdIoType = EfiGcdIoTypeNonExistent;\r
+\r
+ if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
+\r
+ ResourceHob = Hob.ResourceDescriptor;\r
+\r
+ switch (ResourceHob->ResourceType) {\r
+ case EFI_RESOURCE_SYSTEM_MEMORY:\r
+ if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == TESTED_MEMORY_ATTRIBUTES) {\r
+ GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r
+ }\r
+ if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == INITIALIZED_MEMORY_ATTRIBUTES) {\r
+ GcdMemoryType = EfiGcdMemoryTypeReserved;\r
+ }\r
+ if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) == PRESENT_MEMORY_ATTRIBUTES) {\r
+ GcdMemoryType = EfiGcdMemoryTypeReserved;\r
+ }\r
+ break;\r
+ case EFI_RESOURCE_MEMORY_MAPPED_IO:\r
+ case EFI_RESOURCE_FIRMWARE_DEVICE:\r
+ GcdMemoryType = EfiGcdMemoryTypeMemoryMappedIo;\r
+ break;\r
+ case EFI_RESOURCE_MEMORY_MAPPED_IO_PORT:\r
+ case EFI_RESOURCE_MEMORY_RESERVED:\r
+ GcdMemoryType = EfiGcdMemoryTypeReserved;\r
+ break;\r
+ case EFI_RESOURCE_IO:\r
+ GcdIoType = EfiGcdIoTypeIo;\r
+ break;\r
+ case EFI_RESOURCE_IO_RESERVED:\r
+ GcdIoType = EfiGcdIoTypeReserved;\r
+ break;\r
+ }\r
+\r
+ if (GcdMemoryType != EfiGcdMemoryTypeNonExistent) {\r
+\r
+ //\r
+ // Convert the Resource HOB Attributes to an EFI Memory Capabilities mask\r
+ //\r
+ Capabilities = CoreConvertResourceDescriptorHobAttributesToCapabilities (\r
+ GcdMemoryType,\r
+ ResourceHob->ResourceAttribute\r
+ );\r
+\r
+ Status = CoreInternalAddMemorySpace (\r
+ GcdMemoryType,\r
+ ResourceHob->PhysicalStart,\r
+ ResourceHob->ResourceLength,\r
+ Capabilities\r
+ );\r
+ }\r
+\r
+ if (GcdIoType != EfiGcdIoTypeNonExistent) {\r
+ Status = CoreAddIoSpace (\r
+ GcdIoType,\r
+ ResourceHob->PhysicalStart,\r
+ ResourceHob->ResourceLength\r
+ );\r
+ }\r
+ }\r
+ }\r
+\r
+ //\r
+ // Allocate first memory region from the GCD by the DXE core\r
+ //\r
+ Status = CoreAllocateMemorySpace (\r
+ EfiGcdAllocateAddress,\r
+ EfiGcdMemoryTypeSystemMemory,\r
+ 0,\r
+ MemoryLength,\r
+ &MemoryBaseAddress,\r
+ gDxeCoreImageHandle,\r
+ NULL\r
+ );\r
+\r
+ //\r
+ // Walk the HOB list and allocate all memory space that is consumed by memory allocation HOBs,\r
+ // and Firmware Volume HOBs. Also update the EFI Memory Map with the memory allocation HOBs.\r
+ //\r
+ for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
+ if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
+ MemoryHob = Hob.MemoryAllocation;\r
+ BaseAddress = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
+ Status = CoreAllocateMemorySpace (\r
+ EfiGcdAllocateAddress,\r
+ EfiGcdMemoryTypeSystemMemory, \r
+ 0,\r
+ MemoryHob->AllocDescriptor.MemoryLength,\r
+ &BaseAddress,\r
+ gDxeCoreImageHandle,\r
+ NULL\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ Status = CoreGetMemorySpaceDescriptor (MemoryHob->AllocDescriptor.MemoryBaseAddress, &Descriptor);\r
+ if (!EFI_ERROR (Status)) {\r
+ CoreAddMemoryDescriptor (\r
+ MemoryHob->AllocDescriptor.MemoryType,\r
+ MemoryHob->AllocDescriptor.MemoryBaseAddress,\r
+ RShiftU64 (MemoryHob->AllocDescriptor.MemoryLength, EFI_PAGE_SHIFT),\r
+ Descriptor.Capabilities & (~EFI_MEMORY_RUNTIME)\r
+ );\r
+ }\r
+ }\r
+ }\r
+\r
+ if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV) {\r
+ FirmwareVolumeHob = Hob.FirmwareVolume;\r
+ BaseAddress = FirmwareVolumeHob->BaseAddress;\r
+ Status = CoreAllocateMemorySpace (\r
+ EfiGcdAllocateAddress,\r
+ EfiGcdMemoryTypeMemoryMappedIo, \r
+ 0,\r
+ FirmwareVolumeHob->Length,\r
+ &BaseAddress,\r
+ gDxeCoreImageHandle,\r
+ NULL\r
+ );\r
+ }\r
+ }\r
+\r
+ //\r
+ // Relocate HOB List to an allocated pool buffer.\r
+ //\r
+ NewHobList = CoreAllocateCopyPool (\r
+ (UINTN)PhitHob->EfiFreeMemoryBottom - (UINTN)(*HobStart), \r
+ *HobStart\r
+ );\r
+ ASSERT (NewHobList != NULL);\r
+\r
+ *HobStart = NewHobList;\r
+\r
+ //\r
+ // Add and allocate the remaining unallocated system memory to the memory services.\r
+ //\r
+ Status = CoreGetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r
+ for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
+ if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeSystemMemory) {\r
+ if (MemorySpaceMap[Index].ImageHandle == NULL) {\r
+ BaseAddress = PageAlignAddress (MemorySpaceMap[Index].BaseAddress);\r
+ Length = PageAlignLength (MemorySpaceMap[Index].BaseAddress + MemorySpaceMap[Index].Length - BaseAddress);\r
+ CoreAddMemoryDescriptor (\r
+ EfiConventionalMemory,\r
+ BaseAddress,\r
+ RShiftU64 (Length, EFI_PAGE_SHIFT),\r
+ MemorySpaceMap[Index].Capabilities & (~EFI_MEMORY_RUNTIME)\r
+ );\r
+ Status = CoreAllocateMemorySpace (\r
+ EfiGcdAllocateAddress,\r
+ EfiGcdMemoryTypeSystemMemory,\r
+ 0,\r
+ Length,\r
+ &BaseAddress,\r
+ gDxeCoreImageHandle,\r
+ NULL\r
+ );\r
+ }\r
+ }\r
+ }\r
+ CoreFreePool (MemorySpaceMap);\r
+\r
+ return EFI_SUCCESS;\r
+}\r