]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Memory/MemoryServices.c
MdeModulePkg/PeiCore: honour minimal runtime allocation granularity
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Memory / MemoryServices.c
CommitLineData
615c6dd0 1/** @file\r
b1f6a7c6 2 EFI PEI Core memory services\r
3 \r
ab6fb25a 4Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 5This program and the accompanying materials \r
192f6d4c 6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php \r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
615c6dd0 13**/\r
192f6d4c 14\r
0d516397 15#include "PeiMain.h"\r
192f6d4c 16\r
b1f6a7c6 17/**\r
192f6d4c 18\r
19 Initialize the memory services.\r
20\r
82b8c8df 21 @param PrivateData Points to PeiCore's private instance data.\r
b1f6a7c6 22 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
5aae0aa7 23 environment, such as the size and location of temporary RAM, the stack location and\r
24 the BFV location.\r
b1f6a7c6 25 @param OldCoreData Pointer to the PEI Core data.\r
192f6d4c 26 NULL if being run in non-permament memory mode.\r
27\r
b1f6a7c6 28**/\r
29VOID\r
30InitializeMemoryServices (\r
31 IN PEI_CORE_INSTANCE *PrivateData,\r
32 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
33 IN PEI_CORE_INSTANCE *OldCoreData\r
34 )\r
192f6d4c 35{\r
58dcdada 36 \r
63b62331 37 PrivateData->SwitchStackSignal = FALSE;\r
192f6d4c 38\r
40f26b8f 39 //\r
40 // First entering PeiCore, following code will initialized some field\r
41 // in PeiCore's private data according to hand off data from sec core.\r
42 //\r
192f6d4c 43 if (OldCoreData == NULL) {\r
44\r
45 PrivateData->PeiMemoryInstalled = FALSE;\r
63b62331 46 PrivateData->HobList.Raw = SecCoreData->PeiTemporaryRamBase;\r
192f6d4c 47 \r
48 PeiCoreBuildHobHandoffInfoTable (\r
49 BOOT_WITH_FULL_CONFIGURATION,\r
63b62331 50 (EFI_PHYSICAL_ADDRESS) (UINTN) SecCoreData->PeiTemporaryRamBase,\r
5aae0aa7 51 (UINTN) SecCoreData->PeiTemporaryRamSize\r
192f6d4c 52 );\r
192f6d4c 53\r
54 //\r
4140a663 55 // Set Ps to point to ServiceTableShadow in Cache\r
192f6d4c 56 //\r
4140a663 57 PrivateData->Ps = &(PrivateData->ServiceTableShadow);\r
b0d803fe 58 }\r
59 \r
192f6d4c 60 return;\r
61}\r
62\r
b1f6a7c6 63/**\r
64\r
82b8c8df 65 This function registers the found memory configuration with the PEI Foundation.\r
b1f6a7c6 66\r
82b8c8df 67 The usage model is that the PEIM that discovers the permanent memory shall invoke this service.\r
68 This routine will hold discoveried memory information into PeiCore's private data,\r
69 and set SwitchStackSignal flag. After PEIM who discovery memory is dispatched,\r
70 PeiDispatcher will migrate temporary memory to permenement memory.\r
71 \r
d73d93c3 72 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
73 @param MemoryBegin Start of memory address.\r
74 @param MemoryLength Length of memory.\r
b1f6a7c6 75\r
76 @return EFI_SUCCESS Always success.\r
77\r
78**/\r
192f6d4c 79EFI_STATUS\r
80EFIAPI\r
81PeiInstallPeiMemory (\r
58dcdada 82 IN CONST EFI_PEI_SERVICES **PeiServices,\r
83 IN EFI_PHYSICAL_ADDRESS MemoryBegin,\r
84 IN UINT64 MemoryLength\r
192f6d4c 85 )\r
192f6d4c 86{\r
87 PEI_CORE_INSTANCE *PrivateData;\r
58dcdada 88\r
a3a15d21 89 DEBUG ((EFI_D_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));\r
192f6d4c 90 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
91\r
3152f167 92 //\r
93 // PEI_SERVICE.InstallPeiMemory should only be called one time during whole PEI phase.\r
94 // If it is invoked more than one time, ASSERT information is given for developer debugging in debug tip and\r
95 // simply return EFI_SUCESS in release tip to ignore it.\r
96 // \r
97 if (PrivateData->PeiMemoryInstalled) {\r
98 DEBUG ((EFI_D_ERROR, "ERROR: PeiInstallPeiMemory is called more than once!\n"));\r
305d3c8e 99 ASSERT (FALSE);\r
3152f167 100 return EFI_SUCCESS;\r
101 }\r
102 \r
58dcdada 103 PrivateData->PhysicalMemoryBegin = MemoryBegin;\r
104 PrivateData->PhysicalMemoryLength = MemoryLength;\r
105 PrivateData->FreePhysicalMemoryTop = MemoryBegin + MemoryLength;\r
106 \r
107 PrivateData->SwitchStackSignal = TRUE;\r
192f6d4c 108\r
109 return EFI_SUCCESS; \r
110}\r
111\r
b1f6a7c6 112/**\r
64ca6802 113 The purpose of the service is to publish an interface that allows \r
114 PEIMs to allocate memory ranges that are managed by the PEI Foundation.\r
b1f6a7c6 115\r
64ca6802 116 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
117 @param MemoryType The type of memory to allocate.\r
118 @param Pages The number of contiguous 4 KB pages to allocate.\r
119 @param Memory Pointer to a physical address. On output, the address is set to the base \r
120 of the page range that was allocated.\r
b1f6a7c6 121\r
64ca6802 122 @retval EFI_SUCCESS The memory range was successfully allocated.\r
123 @retval EFI_OUT_OF_RESOURCES The pages could not be allocated.\r
124 @retval EFI_INVALID_PARAMETER Type is not equal to EfiLoaderCode, EfiLoaderData, EfiRuntimeServicesCode, \r
125 EfiRuntimeServicesData, EfiBootServicesCode, EfiBootServicesData,\r
ab6fb25a 126 EfiACPIReclaimMemory, EfiReservedMemoryType, or EfiACPIMemoryNVS.\r
b1f6a7c6 127\r
128**/\r
192f6d4c 129EFI_STATUS\r
130EFIAPI\r
131PeiAllocatePages (\r
82b8c8df 132 IN CONST EFI_PEI_SERVICES **PeiServices,\r
133 IN EFI_MEMORY_TYPE MemoryType,\r
134 IN UINTN Pages,\r
135 OUT EFI_PHYSICAL_ADDRESS *Memory\r
192f6d4c 136 )\r
192f6d4c 137{\r
138 PEI_CORE_INSTANCE *PrivateData;\r
139 EFI_PEI_HOB_POINTERS Hob;\r
58dcdada 140 EFI_PHYSICAL_ADDRESS *FreeMemoryTop;\r
141 EFI_PHYSICAL_ADDRESS *FreeMemoryBottom;\r
62031991 142 UINTN RemainingPages;\r
e1e7e0fb
AB
143 UINTN Granularity;\r
144 UINTN Padding;\r
192f6d4c 145\r
64ca6802 146 if ((MemoryType != EfiLoaderCode) &&\r
147 (MemoryType != EfiLoaderData) &&\r
148 (MemoryType != EfiRuntimeServicesCode) &&\r
149 (MemoryType != EfiRuntimeServicesData) &&\r
150 (MemoryType != EfiBootServicesCode) &&\r
151 (MemoryType != EfiBootServicesData) &&\r
152 (MemoryType != EfiACPIReclaimMemory) &&\r
ab6fb25a 153 (MemoryType != EfiReservedMemoryType) &&\r
64ca6802 154 (MemoryType != EfiACPIMemoryNVS)) {\r
155 return EFI_INVALID_PARAMETER;\r
156 }\r
157\r
e1e7e0fb
AB
158 Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r
159\r
160 if (RUNTIME_PAGE_ALLOCATION_GRANULARITY > DEFAULT_PAGE_ALLOCATION_GRANULARITY &&\r
161 (MemoryType == EfiACPIReclaimMemory ||\r
162 MemoryType == EfiACPIMemoryNVS ||\r
163 MemoryType == EfiRuntimeServicesCode ||\r
164 MemoryType == EfiRuntimeServicesData)) {\r
165\r
166 Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r
167\r
168 DEBUG ((DEBUG_INFO, "AllocatePages: aligning allocation to %d KB\n",\r
169 Granularity / SIZE_1KB));\r
170 }\r
171\r
192f6d4c 172 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
58dcdada 173 Hob.Raw = PrivateData->HobList.Raw;\r
174 \r
192f6d4c 175 //\r
176 // Check if Hob already available\r
177 //\r
178 if (!PrivateData->PeiMemoryInstalled) {\r
58dcdada 179 //\r
3d4d0c34 180 // When PeiInstallMemory is called but temporary memory has *not* been moved to temporary memory,\r
5f300691 181 // the AllocatePage will depend on the field of PEI_CORE_INSTANCE structure.\r
58dcdada 182 //\r
183 if (!PrivateData->SwitchStackSignal) {\r
184 return EFI_NOT_AVAILABLE_YET;\r
185 } else {\r
186 FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);\r
187 FreeMemoryBottom = &(PrivateData->PhysicalMemoryBegin);\r
188 }\r
189 } else {\r
190 FreeMemoryTop = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);\r
191 FreeMemoryBottom = &(Hob.HandoffInformationTable->EfiFreeMemoryBottom);\r
192f6d4c 192 }\r
193\r
192f6d4c 194 //\r
e1e7e0fb
AB
195 // Check to see if on correct boundary for the memory type.\r
196 // If not aligned, make the allocation aligned.\r
192f6d4c 197 //\r
e1e7e0fb
AB
198 Padding = *(FreeMemoryTop) & (Granularity - 1);\r
199 if ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom) < Padding) {\r
200 DEBUG ((DEBUG_ERROR, "AllocatePages failed: Out of space after padding.\n"));\r
201 return EFI_OUT_OF_RESOURCES;\r
202 }\r
203\r
204 *(FreeMemoryTop) -= Padding;\r
205 if (Padding >= EFI_PAGE_SIZE) {\r
206 //\r
207 // Create a memory allocation HOB to cover\r
208 // the pages that we will lose to rounding\r
209 //\r
210 BuildMemoryAllocationHob (\r
211 *(FreeMemoryTop),\r
212 Padding & ~(UINTN)EFI_PAGE_MASK,\r
213 EfiConventionalMemory\r
214 );\r
215 }\r
5e574a01 216\r
192f6d4c 217 //\r
5e574a01
SZ
218 // Verify that there is sufficient memory to satisfy the allocation.\r
219 // For page allocation, the overhead sizeof (EFI_HOB_MEMORY_ALLOCATION) needs to be considered.\r
192f6d4c 220 //\r
5e574a01
SZ
221 if ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom) < (UINTN) ALIGN_VALUE (sizeof (EFI_HOB_MEMORY_ALLOCATION), 8)) {\r
222 DEBUG ((EFI_D_ERROR, "AllocatePages failed: No space to build memory allocation hob.\n"));\r
223 return EFI_OUT_OF_RESOURCES;\r
224 }\r
225 RemainingPages = (UINTN)(*FreeMemoryTop - *FreeMemoryBottom - ALIGN_VALUE (sizeof (EFI_HOB_MEMORY_ALLOCATION), 8)) >> EFI_PAGE_SHIFT;\r
98a601b1 226 //\r
5e574a01 227 // The number of remaining pages needs to be greater than or equal to that of the request pages.\r
98a601b1 228 //\r
e1e7e0fb 229 Pages = ALIGN_VALUE (Pages, EFI_SIZE_TO_PAGES (Granularity));\r
5e574a01 230 if (RemainingPages < Pages) {\r
62031991 231 DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64) Pages));\r
232 DEBUG ((EFI_D_ERROR, "There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64) RemainingPages));\r
192f6d4c 233 return EFI_OUT_OF_RESOURCES;\r
234 } else {\r
235 //\r
236 // Update the PHIT to reflect the memory usage\r
237 //\r
58dcdada 238 *(FreeMemoryTop) -= Pages * EFI_PAGE_SIZE;\r
192f6d4c 239\r
240 //\r
241 // Update the value for the caller\r
242 //\r
58dcdada 243 *Memory = *(FreeMemoryTop);\r
192f6d4c 244\r
245 //\r
246 // Create a memory allocation HOB.\r
247 //\r
248 BuildMemoryAllocationHob (\r
58dcdada 249 *(FreeMemoryTop),\r
0a7d0741 250 Pages * EFI_PAGE_SIZE,\r
192f6d4c 251 MemoryType\r
252 );\r
253\r
254 return EFI_SUCCESS;\r
255 }\r
256}\r
257\r
b1f6a7c6 258/**\r
259\r
6393d9c8 260 Pool allocation service. Before permanent memory is discoveried, the pool will\r
3d4d0c34 261 be allocated the heap in the temporary memory. Genenrally, the size of heap in temporary \r
82b8c8df 262 memory does not exceed to 64K, so the biggest pool size could be allocated is \r
263 64K.\r
b1f6a7c6 264\r
82b8c8df 265 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
266 @param Size Amount of memory required\r
267 @param Buffer Address of pointer to the buffer\r
b1f6a7c6 268\r
269 @retval EFI_SUCCESS The allocation was successful\r
270 @retval EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement\r
271 to allocate the requested size.\r
272\r
273**/\r
192f6d4c 274EFI_STATUS\r
275EFIAPI\r
276PeiAllocatePool (\r
82b8c8df 277 IN CONST EFI_PEI_SERVICES **PeiServices,\r
278 IN UINTN Size,\r
279 OUT VOID **Buffer\r
192f6d4c 280 )\r
192f6d4c 281{\r
282 EFI_STATUS Status;\r
283 EFI_HOB_MEMORY_POOL *Hob;\r
284\r
82b8c8df 285 //\r
286 // If some "post-memory" PEIM wishes to allocate larger pool,\r
287 // it should use AllocatePages service instead.\r
288 //\r
289 \r
290 //\r
291 // Generally, the size of heap in temporary memory does not exceed to 64K,\r
e94728b3 292 // HobLength is multiples of 8 bytes, so the maxmium size of pool is 0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL)\r
82b8c8df 293 //\r
e94728b3 294 if (Size > (0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL))) {\r
2e76dc7e 295 return EFI_OUT_OF_RESOURCES;\r
296 }\r
297 \r
82b8c8df 298 Status = PeiServicesCreateHob (\r
192f6d4c 299 EFI_HOB_TYPE_MEMORY_POOL,\r
300 (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),\r
301 (VOID **)&Hob\r
302 );\r
772cba09 303 ASSERT_EFI_ERROR (Status);\r
192f6d4c 304 *Buffer = Hob+1; \r
305\r
192f6d4c 306 return Status;\r
307}\r