ArmPlatformPkg: Allow PcdFirmwareVersionString to be a dynamic PCD
[mirror_edk2.git] / ArmPlatformPkg / MemoryInitPei / MemoryInitPeiLib.c
CommitLineData
3a6eaccf 1/** @file\r
2*\r
bb5420bb 3* Copyright (c) 2011-2014, ARM Limited. All rights reserved.\r
3a6eaccf 4*\r
5* This program and the accompanying materials\r
6* are licensed and made available under the terms and conditions of the BSD License\r
7* which accompanies this distribution. The full text of the license may be found at\r
8* http://opensource.org/licenses/bsd-license.php\r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12*\r
13**/\r
14\r
15#include <PiPei.h>\r
16\r
17#include <Library/ArmPlatformLib.h>\r
18#include <Library/DebugLib.h>\r
19#include <Library/HobLib.h>\r
20#include <Library/MemoryAllocationLib.h>\r
21#include <Library/PcdLib.h>\r
22\r
3a6eaccf 23VOID\r
24BuildMemoryTypeInformationHob (\r
25 VOID\r
26 );\r
27\r
28VOID\r
29InitMmu (\r
30 VOID\r
31 )\r
32{\r
33 ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable;\r
34 VOID *TranslationTableBase;\r
35 UINTN TranslationTableSize;\r
6f050ad6 36 RETURN_STATUS Status;\r
3a6eaccf 37\r
38 // Get Virtual Memory Map from the Platform Library\r
b34e4db3 39 ArmPlatformGetVirtualMemoryMap (&MemoryTable);\r
3a6eaccf 40\r
41 //Note: Because we called PeiServicesInstallPeiMemory() before to call InitMmu() the MMU Page Table resides in\r
42 // DRAM (even at the top of DRAM as it is the first permanent memory allocation)\r
6f050ad6
OM
43 Status = ArmConfigureMmu (MemoryTable, &TranslationTableBase, &TranslationTableSize);\r
44 if (EFI_ERROR (Status)) {\r
45 DEBUG ((EFI_D_ERROR, "Error: Failed to enable MMU\n"));\r
46 }\r
3a6eaccf 47}\r
48\r
49/*++\r
50\r
51Routine Description:\r
52\r
53\r
54\r
55Arguments:\r
56\r
57 FileHandle - Handle of the file being invoked.\r
58 PeiServices - Describes the list of possible PEI Services.\r
59\r
60Returns:\r
61\r
62 Status - EFI_SUCCESS if the boot mode could be set\r
63\r
64--*/\r
65EFI_STATUS\r
66EFIAPI\r
67MemoryPeim (\r
68 IN EFI_PHYSICAL_ADDRESS UefiMemoryBase,\r
69 IN UINT64 UefiMemorySize\r
70 )\r
71{\r
d269095b 72 EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttributes;\r
73 UINT64 ResourceLength;\r
74 EFI_PEI_HOB_POINTERS NextHob;\r
75 EFI_PHYSICAL_ADDRESS FdTop;\r
76 EFI_PHYSICAL_ADDRESS SystemMemoryTop;\r
77 EFI_PHYSICAL_ADDRESS ResourceTop;\r
78 BOOLEAN Found;\r
3a6eaccf 79\r
80 // Ensure PcdSystemMemorySize has been set\r
c357fd6a 81 ASSERT (PcdGet64 (PcdSystemMemorySize) != 0);\r
3a6eaccf 82\r
3a6eaccf 83 //\r
84 // Now, the permanent memory has been installed, we can call AllocatePages()\r
85 //\r
d269095b 86 ResourceAttributes = (\r
3a6eaccf 87 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
88 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
89 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
90 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
91 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
92 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |\r
93 EFI_RESOURCE_ATTRIBUTE_TESTED\r
94 );\r
95\r
d269095b 96 // Reserved the memory space occupied by the firmware volume\r
3a6eaccf 97 BuildResourceDescriptorHob (\r
98 EFI_RESOURCE_SYSTEM_MEMORY,\r
d269095b 99 ResourceAttributes,\r
c357fd6a
OM
100 PcdGet64 (PcdSystemMemoryBase),\r
101 PcdGet64 (PcdSystemMemorySize)\r
3a6eaccf 102 );\r
103\r
c357fd6a 104 SystemMemoryTop = (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdSystemMemoryBase) + (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdSystemMemorySize);\r
bb5420bb 105 FdTop = (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdFdBaseAddress) + (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdFdSize);\r
d269095b 106\r
107 // EDK2 does not have the concept of boot firmware copied into DRAM. To avoid the DXE\r
108 // core to overwrite this area we must mark the region with the attribute non-present\r
bb5420bb 109 if ((PcdGet64 (PcdFdBaseAddress) >= PcdGet64 (PcdSystemMemoryBase)) && (FdTop <= SystemMemoryTop)) {\r
d269095b 110 Found = FALSE;\r
111\r
112 // Search for System Memory Hob that contains the firmware\r
113 NextHob.Raw = GetHobList ();\r
114 while ((NextHob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, NextHob.Raw)) != NULL) {\r
115 if ((NextHob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&\r
bb5420bb 116 (PcdGet64 (PcdFdBaseAddress) >= NextHob.ResourceDescriptor->PhysicalStart) &&\r
d269095b 117 (FdTop <= NextHob.ResourceDescriptor->PhysicalStart + NextHob.ResourceDescriptor->ResourceLength))\r
118 {\r
119 ResourceAttributes = NextHob.ResourceDescriptor->ResourceAttribute;\r
120 ResourceLength = NextHob.ResourceDescriptor->ResourceLength;\r
121 ResourceTop = NextHob.ResourceDescriptor->PhysicalStart + ResourceLength;\r
122\r
bb5420bb 123 if (PcdGet64 (PcdFdBaseAddress) == NextHob.ResourceDescriptor->PhysicalStart) {\r
d269095b 124 if (SystemMemoryTop == FdTop) {\r
125 NextHob.ResourceDescriptor->ResourceAttribute = ResourceAttributes & ~EFI_RESOURCE_ATTRIBUTE_PRESENT;\r
126 } else {\r
127 // Create the System Memory HOB for the firmware with the non-present attribute\r
128 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,\r
129 ResourceAttributes & ~EFI_RESOURCE_ATTRIBUTE_PRESENT,\r
bb5420bb
LL
130 PcdGet64 (PcdFdBaseAddress),\r
131 PcdGet32 (PcdFdSize));\r
d269095b 132\r
133 // Top of the FD is system memory available for UEFI\r
f92b93c9 134 NextHob.ResourceDescriptor->PhysicalStart += PcdGet32(PcdFdSize);\r
135 NextHob.ResourceDescriptor->ResourceLength -= PcdGet32(PcdFdSize);\r
d269095b 136 }\r
137 } else {\r
138 // Create the System Memory HOB for the firmware with the non-present attribute\r
139 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,\r
140 ResourceAttributes & ~EFI_RESOURCE_ATTRIBUTE_PRESENT,\r
bb5420bb
LL
141 PcdGet64 (PcdFdBaseAddress),\r
142 PcdGet32 (PcdFdSize));\r
d269095b 143\r
144 // Update the HOB\r
bb5420bb 145 NextHob.ResourceDescriptor->ResourceLength = PcdGet64 (PcdFdBaseAddress) - NextHob.ResourceDescriptor->PhysicalStart;\r
d269095b 146\r
147 // If there is some memory available on the top of the FD then create a HOB\r
148 if (FdTop < NextHob.ResourceDescriptor->PhysicalStart + ResourceLength) {\r
149 // Create the System Memory HOB for the remaining region (top of the FD)\r
150 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,\r
151 ResourceAttributes,\r
152 FdTop,\r
153 ResourceTop - FdTop);\r
154 }\r
155 }\r
156 Found = TRUE;\r
157 break;\r
158 }\r
159 NextHob.Raw = GET_NEXT_HOB (NextHob);\r
3a6eaccf 160 }\r
d269095b 161\r
162 ASSERT(Found);\r
3a6eaccf 163 }\r
164\r
165 // Build Memory Allocation Hob\r
166 InitMmu ();\r
167\r
168 if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) {\r
169 // Optional feature that helps prevent EFI memory map fragmentation.\r
170 BuildMemoryTypeInformationHob ();\r
171 }\r
172\r
173 return EFI_SUCCESS;\r
174}\r