]>
Commit | Line | Data |
---|---|---|
615c6dd0 | 1 | /** @file\r |
b1f6a7c6 | 2 | EFI PEI Core memory services\r |
d1102dba | 3 | \r |
d39d1260 | 4 | Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r |
9d510e61 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
192f6d4c | 6 | \r |
615c6dd0 | 7 | **/\r |
192f6d4c | 8 | \r |
0d516397 | 9 | #include "PeiMain.h"\r |
192f6d4c | 10 | \r |
b1f6a7c6 | 11 | /**\r |
192f6d4c | 12 | \r |
13 | Initialize the memory services.\r | |
14 | \r | |
82b8c8df | 15 | @param PrivateData Points to PeiCore's private instance data.\r |
b1f6a7c6 | 16 | @param SecCoreData Points to a data structure containing information about the PEI core's operating\r |
5aae0aa7 | 17 | environment, such as the size and location of temporary RAM, the stack location and\r |
18 | the BFV location.\r | |
b1f6a7c6 | 19 | @param OldCoreData Pointer to the PEI Core data.\r |
d39d1260 | 20 | NULL if being run in non-permanent memory mode.\r |
192f6d4c | 21 | \r |
b1f6a7c6 | 22 | **/\r |
23 | VOID\r | |
24 | InitializeMemoryServices (\r | |
25 | IN PEI_CORE_INSTANCE *PrivateData,\r | |
26 | IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r | |
27 | IN PEI_CORE_INSTANCE *OldCoreData\r | |
28 | )\r | |
192f6d4c | 29 | {\r |
1436aea4 | 30 | PrivateData->SwitchStackSignal = FALSE;\r |
192f6d4c | 31 | \r |
40f26b8f | 32 | //\r |
33 | // First entering PeiCore, following code will initialized some field\r | |
d39d1260 | 34 | // in PeiCore's private data according to hand off data from SEC core.\r |
40f26b8f | 35 | //\r |
192f6d4c | 36 | if (OldCoreData == NULL) {\r |
192f6d4c | 37 | PrivateData->PeiMemoryInstalled = FALSE;\r |
63b62331 | 38 | PrivateData->HobList.Raw = SecCoreData->PeiTemporaryRamBase;\r |
d1102dba | 39 | \r |
192f6d4c | 40 | PeiCoreBuildHobHandoffInfoTable (\r |
41 | BOOT_WITH_FULL_CONFIGURATION,\r | |
1436aea4 MK |
42 | (EFI_PHYSICAL_ADDRESS)(UINTN)SecCoreData->PeiTemporaryRamBase,\r |
43 | (UINTN)SecCoreData->PeiTemporaryRamSize\r | |
192f6d4c | 44 | );\r |
192f6d4c | 45 | \r |
46 | //\r | |
4140a663 | 47 | // Set Ps to point to ServiceTableShadow in Cache\r |
192f6d4c | 48 | //\r |
4140a663 | 49 | PrivateData->Ps = &(PrivateData->ServiceTableShadow);\r |
b0d803fe | 50 | }\r |
d1102dba | 51 | \r |
192f6d4c | 52 | return;\r |
53 | }\r | |
54 | \r | |
b1f6a7c6 | 55 | /**\r |
56 | \r | |
82b8c8df | 57 | This function registers the found memory configuration with the PEI Foundation.\r |
b1f6a7c6 | 58 | \r |
82b8c8df | 59 | The usage model is that the PEIM that discovers the permanent memory shall invoke this service.\r |
60 | This routine will hold discoveried memory information into PeiCore's private data,\r | |
61 | and set SwitchStackSignal flag. After PEIM who discovery memory is dispatched,\r | |
d39d1260 | 62 | PeiDispatcher will migrate temporary memory to permanent memory.\r |
d1102dba | 63 | \r |
d73d93c3 | 64 | @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r |
65 | @param MemoryBegin Start of memory address.\r | |
66 | @param MemoryLength Length of memory.\r | |
b1f6a7c6 | 67 | \r |
68 | @return EFI_SUCCESS Always success.\r | |
69 | \r | |
70 | **/\r | |
192f6d4c | 71 | EFI_STATUS\r |
72 | EFIAPI\r | |
73 | PeiInstallPeiMemory (\r | |
58dcdada | 74 | IN CONST EFI_PEI_SERVICES **PeiServices,\r |
75 | IN EFI_PHYSICAL_ADDRESS MemoryBegin,\r | |
76 | IN UINT64 MemoryLength\r | |
192f6d4c | 77 | )\r |
192f6d4c | 78 | {\r |
1436aea4 | 79 | PEI_CORE_INSTANCE *PrivateData;\r |
58dcdada | 80 | \r |
87000d77 | 81 | DEBUG ((DEBUG_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));\r |
192f6d4c | 82 | PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r |
83 | \r | |
3152f167 | 84 | //\r |
85 | // PEI_SERVICE.InstallPeiMemory should only be called one time during whole PEI phase.\r | |
86 | // If it is invoked more than one time, ASSERT information is given for developer debugging in debug tip and\r | |
d39d1260 | 87 | // simply return EFI_SUCCESS in release tip to ignore it.\r |
d1102dba | 88 | //\r |
3152f167 | 89 | if (PrivateData->PeiMemoryInstalled) {\r |
87000d77 | 90 | DEBUG ((DEBUG_ERROR, "ERROR: PeiInstallPeiMemory is called more than once!\n"));\r |
305d3c8e | 91 | ASSERT (FALSE);\r |
3152f167 | 92 | return EFI_SUCCESS;\r |
93 | }\r | |
d1102dba | 94 | \r |
58dcdada | 95 | PrivateData->PhysicalMemoryBegin = MemoryBegin;\r |
96 | PrivateData->PhysicalMemoryLength = MemoryLength;\r | |
97 | PrivateData->FreePhysicalMemoryTop = MemoryBegin + MemoryLength;\r | |
d1102dba | 98 | \r |
1436aea4 | 99 | PrivateData->SwitchStackSignal = TRUE;\r |
192f6d4c | 100 | \r |
d1102dba | 101 | return EFI_SUCCESS;\r |
192f6d4c | 102 | }\r |
103 | \r | |
b1f6a7c6 | 104 | /**\r |
b2374cec SZ |
105 | Migrate memory pages allocated in pre-memory phase.\r |
106 | Copy memory pages at temporary heap top to permanent heap top.\r | |
107 | \r | |
108 | @param[in] Private Pointer to the private data passed in from caller.\r | |
109 | @param[in] TemporaryRamMigrated Temporary memory has been migrated to permanent memory.\r | |
110 | \r | |
111 | **/\r | |
112 | VOID\r | |
113 | MigrateMemoryPages (\r | |
1436aea4 MK |
114 | IN PEI_CORE_INSTANCE *Private,\r |
115 | IN BOOLEAN TemporaryRamMigrated\r | |
b2374cec SZ |
116 | )\r |
117 | {\r | |
1436aea4 MK |
118 | EFI_PHYSICAL_ADDRESS NewMemPagesBase;\r |
119 | EFI_PHYSICAL_ADDRESS MemPagesBase;\r | |
b2374cec | 120 | \r |
1436aea4 MK |
121 | Private->MemoryPages.Size = (UINTN)(Private->HobList.HandoffInformationTable->EfiMemoryTop -\r |
122 | Private->HobList.HandoffInformationTable->EfiFreeMemoryTop);\r | |
b2374cec SZ |
123 | if (Private->MemoryPages.Size == 0) {\r |
124 | //\r | |
125 | // No any memory page allocated in pre-memory phase.\r | |
126 | //\r | |
127 | return;\r | |
128 | }\r | |
1436aea4 | 129 | \r |
b2374cec SZ |
130 | Private->MemoryPages.Base = Private->HobList.HandoffInformationTable->EfiFreeMemoryTop;\r |
131 | \r | |
132 | ASSERT (Private->MemoryPages.Size <= Private->FreePhysicalMemoryTop);\r | |
1436aea4 | 133 | NewMemPagesBase = Private->FreePhysicalMemoryTop - Private->MemoryPages.Size;\r |
b2374cec SZ |
134 | NewMemPagesBase &= ~(UINT64)EFI_PAGE_MASK;\r |
135 | ASSERT (NewMemPagesBase >= Private->PhysicalMemoryBegin);\r | |
136 | //\r | |
137 | // Copy memory pages at temporary heap top to permanent heap top.\r | |
138 | //\r | |
139 | if (TemporaryRamMigrated) {\r | |
140 | //\r | |
141 | // Memory pages at temporary heap top has been migrated to permanent heap,\r | |
142 | // Here still needs to copy them from permanent heap to permanent heap top.\r | |
143 | //\r | |
144 | MemPagesBase = Private->MemoryPages.Base;\r | |
145 | if (Private->HeapOffsetPositive) {\r | |
146 | MemPagesBase += Private->HeapOffset;\r | |
147 | } else {\r | |
148 | MemPagesBase -= Private->HeapOffset;\r | |
149 | }\r | |
1436aea4 | 150 | \r |
b2374cec SZ |
151 | CopyMem ((VOID *)(UINTN)NewMemPagesBase, (VOID *)(UINTN)MemPagesBase, Private->MemoryPages.Size);\r |
152 | } else {\r | |
153 | CopyMem ((VOID *)(UINTN)NewMemPagesBase, (VOID *)(UINTN)Private->MemoryPages.Base, Private->MemoryPages.Size);\r | |
154 | }\r | |
155 | \r | |
156 | if (NewMemPagesBase >= Private->MemoryPages.Base) {\r | |
157 | Private->MemoryPages.OffsetPositive = TRUE;\r | |
1436aea4 | 158 | Private->MemoryPages.Offset = (UINTN)(NewMemPagesBase - Private->MemoryPages.Base);\r |
b2374cec SZ |
159 | } else {\r |
160 | Private->MemoryPages.OffsetPositive = FALSE;\r | |
1436aea4 | 161 | Private->MemoryPages.Offset = (UINTN)(Private->MemoryPages.Base - NewMemPagesBase);\r |
b2374cec SZ |
162 | }\r |
163 | \r | |
1436aea4 | 164 | DEBUG ((DEBUG_INFO, "Pages Offset = 0x%lX\n", (UINT64)Private->MemoryPages.Offset));\r |
b2374cec SZ |
165 | \r |
166 | Private->FreePhysicalMemoryTop = NewMemPagesBase;\r | |
167 | }\r | |
168 | \r | |
9bedaec0 MK |
169 | /**\r |
170 | Removes any FV HOBs whose base address is not in PEI installed memory.\r | |
171 | \r | |
172 | @param[in] Private Pointer to PeiCore's private data structure.\r | |
173 | \r | |
174 | **/\r | |
175 | VOID\r | |
176 | RemoveFvHobsInTemporaryMemory (\r | |
1436aea4 | 177 | IN PEI_CORE_INSTANCE *Private\r |
9bedaec0 MK |
178 | )\r |
179 | {\r | |
1436aea4 MK |
180 | EFI_PEI_HOB_POINTERS Hob;\r |
181 | EFI_HOB_FIRMWARE_VOLUME *FirmwareVolumeHob;\r | |
9bedaec0 MK |
182 | \r |
183 | DEBUG ((DEBUG_INFO, "Removing FVs in FV HOB not already migrated to permanent memory.\n"));\r | |
184 | \r | |
185 | for (Hob.Raw = GetHobList (); !END_OF_HOB_LIST (Hob); Hob.Raw = GET_NEXT_HOB (Hob)) {\r | |
1436aea4 | 186 | if ((GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV) || (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV2) || (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV3)) {\r |
9bedaec0 MK |
187 | FirmwareVolumeHob = Hob.FirmwareVolume;\r |
188 | DEBUG ((DEBUG_INFO, " Found FV HOB.\n"));\r | |
189 | DEBUG ((\r | |
1436aea4 MK |
190 | DEBUG_INFO,\r |
191 | " BA=%016lx L=%016lx\n",\r | |
192 | FirmwareVolumeHob->BaseAddress,\r | |
193 | FirmwareVolumeHob->Length\r | |
194 | ));\r | |
9bedaec0 | 195 | if (\r |
1436aea4 MK |
196 | !(\r |
197 | ((EFI_PHYSICAL_ADDRESS)(UINTN)FirmwareVolumeHob->BaseAddress >= Private->PhysicalMemoryBegin) &&\r | |
198 | (((EFI_PHYSICAL_ADDRESS)(UINTN)FirmwareVolumeHob->BaseAddress + (FirmwareVolumeHob->Length - 1)) < Private->FreePhysicalMemoryTop)\r | |
199 | )\r | |
9bedaec0 | 200 | )\r |
1436aea4 | 201 | {\r |
9bedaec0 MK |
202 | DEBUG ((DEBUG_INFO, " Removing FV HOB to an FV in T-RAM (was not migrated).\n"));\r |
203 | Hob.Header->HobType = EFI_HOB_TYPE_UNUSED;\r | |
204 | }\r | |
205 | }\r | |
206 | }\r | |
207 | }\r | |
208 | \r | |
209 | /**\r | |
210 | Migrate the base address in firmware volume allocation HOBs\r | |
211 | from temporary memory to PEI installed memory.\r | |
212 | \r | |
213 | @param[in] PrivateData Pointer to PeiCore's private data structure.\r | |
214 | @param[in] OrgFvHandle Address of FV Handle in temporary memory.\r | |
215 | @param[in] FvHandle Address of FV Handle in permanent memory.\r | |
216 | \r | |
217 | **/\r | |
218 | VOID\r | |
219 | ConvertFvHob (\r | |
1436aea4 MK |
220 | IN PEI_CORE_INSTANCE *PrivateData,\r |
221 | IN UINTN OrgFvHandle,\r | |
222 | IN UINTN FvHandle\r | |
9bedaec0 MK |
223 | )\r |
224 | {\r | |
1436aea4 MK |
225 | EFI_PEI_HOB_POINTERS Hob;\r |
226 | EFI_HOB_FIRMWARE_VOLUME *FirmwareVolumeHob;\r | |
227 | EFI_HOB_FIRMWARE_VOLUME2 *FirmwareVolume2Hob;\r | |
228 | EFI_HOB_FIRMWARE_VOLUME3 *FirmwareVolume3Hob;\r | |
9bedaec0 MK |
229 | \r |
230 | DEBUG ((DEBUG_INFO, "Converting FVs in FV HOB.\n"));\r | |
231 | \r | |
232 | for (Hob.Raw = GetHobList (); !END_OF_HOB_LIST (Hob); Hob.Raw = GET_NEXT_HOB (Hob)) {\r | |
233 | if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV) {\r | |
234 | FirmwareVolumeHob = Hob.FirmwareVolume;\r | |
235 | if (FirmwareVolumeHob->BaseAddress == OrgFvHandle) {\r | |
236 | FirmwareVolumeHob->BaseAddress = FvHandle;\r | |
237 | }\r | |
238 | } else if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV2) {\r | |
239 | FirmwareVolume2Hob = Hob.FirmwareVolume2;\r | |
240 | if (FirmwareVolume2Hob->BaseAddress == OrgFvHandle) {\r | |
241 | FirmwareVolume2Hob->BaseAddress = FvHandle;\r | |
242 | }\r | |
243 | } else if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_FV3) {\r | |
244 | FirmwareVolume3Hob = Hob.FirmwareVolume3;\r | |
245 | if (FirmwareVolume3Hob->BaseAddress == OrgFvHandle) {\r | |
246 | FirmwareVolume3Hob->BaseAddress = FvHandle;\r | |
247 | }\r | |
248 | }\r | |
249 | }\r | |
250 | }\r | |
251 | \r | |
b2374cec SZ |
252 | /**\r |
253 | Migrate MemoryBaseAddress in memory allocation HOBs\r | |
254 | from the temporary memory to PEI installed memory.\r | |
255 | \r | |
256 | @param[in] PrivateData Pointer to PeiCore's private data structure.\r | |
257 | \r | |
258 | **/\r | |
259 | VOID\r | |
260 | ConvertMemoryAllocationHobs (\r | |
1436aea4 | 261 | IN PEI_CORE_INSTANCE *PrivateData\r |
b2374cec SZ |
262 | )\r |
263 | {\r | |
1436aea4 MK |
264 | EFI_PEI_HOB_POINTERS Hob;\r |
265 | EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob;\r | |
266 | EFI_PHYSICAL_ADDRESS OldMemPagesBase;\r | |
267 | UINTN OldMemPagesSize;\r | |
b2374cec SZ |
268 | \r |
269 | if (PrivateData->MemoryPages.Size == 0) {\r | |
270 | //\r | |
271 | // No any memory page allocated in pre-memory phase.\r | |
272 | //\r | |
273 | return;\r | |
274 | }\r | |
275 | \r | |
276 | OldMemPagesBase = PrivateData->MemoryPages.Base;\r | |
277 | OldMemPagesSize = PrivateData->MemoryPages.Size;\r | |
278 | \r | |
279 | MemoryAllocationHob = NULL;\r | |
1436aea4 | 280 | Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r |
b2374cec | 281 | while (Hob.Raw != NULL) {\r |
1436aea4 | 282 | MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;\r |
b2374cec SZ |
283 | if ((MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress >= OldMemPagesBase) &&\r |
284 | (MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress < (OldMemPagesBase + OldMemPagesSize))\r | |
1436aea4 MK |
285 | )\r |
286 | {\r | |
b2374cec SZ |
287 | if (PrivateData->MemoryPages.OffsetPositive) {\r |
288 | MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress += PrivateData->MemoryPages.Offset;\r | |
289 | } else {\r | |
290 | MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress -= PrivateData->MemoryPages.Offset;\r | |
291 | }\r | |
292 | }\r | |
293 | \r | |
294 | Hob.Raw = GET_NEXT_HOB (Hob);\r | |
295 | Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r | |
296 | }\r | |
297 | }\r | |
298 | \r | |
299 | /**\r | |
300 | Internal function to build a HOB for the memory allocation.\r | |
301 | It will search and reuse the unused(freed) memory allocation HOB,\r | |
302 | or build memory allocation HOB normally if no unused(freed) memory allocation HOB found.\r | |
303 | \r | |
304 | @param[in] BaseAddress The 64 bit physical address of the memory.\r | |
305 | @param[in] Length The length of the memory allocation in bytes.\r | |
306 | @param[in] MemoryType The type of memory allocated by this HOB.\r | |
307 | \r | |
308 | **/\r | |
309 | VOID\r | |
310 | InternalBuildMemoryAllocationHob (\r | |
1436aea4 MK |
311 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r |
312 | IN UINT64 Length,\r | |
313 | IN EFI_MEMORY_TYPE MemoryType\r | |
b2374cec SZ |
314 | )\r |
315 | {\r | |
1436aea4 MK |
316 | EFI_PEI_HOB_POINTERS Hob;\r |
317 | EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob;\r | |
b2374cec SZ |
318 | \r |
319 | //\r | |
320 | // Search unused(freed) memory allocation HOB.\r | |
321 | //\r | |
322 | MemoryAllocationHob = NULL;\r | |
1436aea4 | 323 | Hob.Raw = GetFirstHob (EFI_HOB_TYPE_UNUSED);\r |
b2374cec SZ |
324 | while (Hob.Raw != NULL) {\r |
325 | if (Hob.Header->HobLength == sizeof (EFI_HOB_MEMORY_ALLOCATION)) {\r | |
1436aea4 | 326 | MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;\r |
b2374cec SZ |
327 | break;\r |
328 | }\r | |
329 | \r | |
330 | Hob.Raw = GET_NEXT_HOB (Hob);\r | |
331 | Hob.Raw = GetNextHob (EFI_HOB_TYPE_UNUSED, Hob.Raw);\r | |
332 | }\r | |
333 | \r | |
334 | if (MemoryAllocationHob != NULL) {\r | |
335 | //\r | |
336 | // Reuse the unused(freed) memory allocation HOB.\r | |
337 | //\r | |
338 | MemoryAllocationHob->Header.HobType = EFI_HOB_TYPE_MEMORY_ALLOCATION;\r | |
339 | ZeroMem (&(MemoryAllocationHob->AllocDescriptor.Name), sizeof (EFI_GUID));\r | |
340 | MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r | |
341 | MemoryAllocationHob->AllocDescriptor.MemoryLength = Length;\r | |
342 | MemoryAllocationHob->AllocDescriptor.MemoryType = MemoryType;\r | |
343 | //\r | |
344 | // Zero the reserved space to match HOB spec\r | |
345 | //\r | |
346 | ZeroMem (MemoryAllocationHob->AllocDescriptor.Reserved, sizeof (MemoryAllocationHob->AllocDescriptor.Reserved));\r | |
347 | } else {\r | |
348 | //\r | |
349 | // No unused(freed) memory allocation HOB found.\r | |
350 | // Build memory allocation HOB normally.\r | |
351 | //\r | |
352 | BuildMemoryAllocationHob (\r | |
353 | BaseAddress,\r | |
354 | Length,\r | |
355 | MemoryType\r | |
356 | );\r | |
357 | }\r | |
358 | }\r | |
359 | \r | |
360 | /**\r | |
361 | Update or split memory allocation HOB for memory pages allocate and free.\r | |
362 | \r | |
363 | @param[in, out] MemoryAllocationHob Pointer to the memory allocation HOB\r | |
364 | that needs to be updated or split.\r | |
365 | On output, it will be filled with\r | |
366 | the input Memory, Bytes and MemoryType.\r | |
367 | @param[in] Memory Memory to allocate or free.\r | |
368 | @param[in] Bytes Bytes to allocate or free.\r | |
369 | @param[in] MemoryType EfiConventionalMemory for pages free,\r | |
370 | others for pages allocate.\r | |
371 | \r | |
372 | **/\r | |
373 | VOID\r | |
374 | UpdateOrSplitMemoryAllocationHob (\r | |
1436aea4 MK |
375 | IN OUT EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob,\r |
376 | IN EFI_PHYSICAL_ADDRESS Memory,\r | |
377 | IN UINT64 Bytes,\r | |
378 | IN EFI_MEMORY_TYPE MemoryType\r | |
b2374cec SZ |
379 | )\r |
380 | {\r | |
381 | if ((Memory + Bytes) <\r | |
1436aea4 MK |
382 | (MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress + MemoryAllocationHob->AllocDescriptor.MemoryLength))\r |
383 | {\r | |
b2374cec SZ |
384 | //\r |
385 | // Last pages need to be split out.\r | |
386 | //\r | |
387 | InternalBuildMemoryAllocationHob (\r | |
388 | Memory + Bytes,\r | |
389 | (MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress + MemoryAllocationHob->AllocDescriptor.MemoryLength) - (Memory + Bytes),\r | |
390 | MemoryAllocationHob->AllocDescriptor.MemoryType\r | |
391 | );\r | |
392 | }\r | |
393 | \r | |
394 | if (Memory > MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress) {\r | |
395 | //\r | |
396 | // First pages need to be split out.\r | |
397 | //\r | |
398 | InternalBuildMemoryAllocationHob (\r | |
399 | MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress,\r | |
400 | Memory - MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress,\r | |
401 | MemoryAllocationHob->AllocDescriptor.MemoryType\r | |
402 | );\r | |
403 | }\r | |
404 | \r | |
405 | //\r | |
406 | // Update the memory allocation HOB.\r | |
407 | //\r | |
408 | MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress = Memory;\r | |
1436aea4 MK |
409 | MemoryAllocationHob->AllocDescriptor.MemoryLength = Bytes;\r |
410 | MemoryAllocationHob->AllocDescriptor.MemoryType = MemoryType;\r | |
b2374cec SZ |
411 | }\r |
412 | \r | |
413 | /**\r | |
414 | Merge adjacent free memory ranges in memory allocation HOBs.\r | |
415 | \r | |
416 | @retval TRUE There are free memory ranges merged.\r | |
417 | @retval FALSE No free memory ranges merged.\r | |
418 | \r | |
419 | **/\r | |
420 | BOOLEAN\r | |
421 | MergeFreeMemoryInMemoryAllocationHob (\r | |
422 | VOID\r | |
423 | )\r | |
424 | {\r | |
1436aea4 MK |
425 | EFI_PEI_HOB_POINTERS Hob;\r |
426 | EFI_PEI_HOB_POINTERS Hob2;\r | |
427 | EFI_HOB_MEMORY_ALLOCATION *MemoryHob;\r | |
428 | EFI_HOB_MEMORY_ALLOCATION *MemoryHob2;\r | |
429 | UINT64 Start;\r | |
430 | UINT64 End;\r | |
431 | BOOLEAN Merged;\r | |
b2374cec SZ |
432 | \r |
433 | Merged = FALSE;\r | |
434 | \r | |
435 | Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r | |
436 | while (Hob.Raw != NULL) {\r | |
437 | if (Hob.MemoryAllocation->AllocDescriptor.MemoryType == EfiConventionalMemory) {\r | |
1436aea4 MK |
438 | MemoryHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;\r |
439 | Start = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r | |
440 | End = MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength;\r | |
b2374cec SZ |
441 | \r |
442 | Hob2.Raw = GET_NEXT_HOB (Hob);\r | |
443 | Hob2.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r | |
444 | while (Hob2.Raw != NULL) {\r | |
445 | if (Hob2.MemoryAllocation->AllocDescriptor.MemoryType == EfiConventionalMemory) {\r | |
1436aea4 | 446 | MemoryHob2 = (EFI_HOB_MEMORY_ALLOCATION *)Hob2.Raw;\r |
b2374cec SZ |
447 | if (Start == (MemoryHob2->AllocDescriptor.MemoryBaseAddress + MemoryHob2->AllocDescriptor.MemoryLength)) {\r |
448 | //\r | |
449 | // Merge adjacent two free memory ranges.\r | |
450 | //\r | |
451 | MemoryHob2->AllocDescriptor.MemoryLength += MemoryHob->AllocDescriptor.MemoryLength;\r | |
1436aea4 | 452 | Merged = TRUE;\r |
b2374cec SZ |
453 | //\r |
454 | // Mark MemoryHob to be unused(freed).\r | |
455 | //\r | |
456 | MemoryHob->Header.HobType = EFI_HOB_TYPE_UNUSED;\r | |
457 | break;\r | |
458 | } else if (End == MemoryHob2->AllocDescriptor.MemoryBaseAddress) {\r | |
459 | //\r | |
460 | // Merge adjacent two free memory ranges.\r | |
461 | //\r | |
462 | MemoryHob2->AllocDescriptor.MemoryBaseAddress = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r | |
1436aea4 MK |
463 | MemoryHob2->AllocDescriptor.MemoryLength += MemoryHob->AllocDescriptor.MemoryLength;\r |
464 | Merged = TRUE;\r | |
b2374cec SZ |
465 | //\r |
466 | // Mark MemoryHob to be unused(freed).\r | |
467 | //\r | |
468 | MemoryHob->Header.HobType = EFI_HOB_TYPE_UNUSED;\r | |
469 | break;\r | |
470 | }\r | |
471 | }\r | |
1436aea4 | 472 | \r |
b2374cec SZ |
473 | Hob2.Raw = GET_NEXT_HOB (Hob2);\r |
474 | Hob2.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob2.Raw);\r | |
475 | }\r | |
476 | }\r | |
1436aea4 | 477 | \r |
b2374cec SZ |
478 | Hob.Raw = GET_NEXT_HOB (Hob);\r |
479 | Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r | |
480 | }\r | |
481 | \r | |
482 | return Merged;\r | |
483 | }\r | |
484 | \r | |
485 | /**\r | |
486 | Find free memory by searching memory allocation HOBs.\r | |
487 | \r | |
488 | @param[in] MemoryType The type of memory to allocate.\r | |
489 | @param[in] Pages The number of contiguous 4 KB pages to allocate.\r | |
490 | @param[in] Granularity Page allocation granularity.\r | |
491 | @param[out] Memory Pointer to a physical address. On output, the address is set to the base\r | |
492 | of the page range that was allocated.\r | |
493 | \r | |
494 | @retval EFI_SUCCESS The memory range was successfully allocated.\r | |
495 | @retval EFI_NOT_FOUND No memory allocation HOB with big enough free memory found.\r | |
496 | \r | |
497 | **/\r | |
498 | EFI_STATUS\r | |
499 | FindFreeMemoryFromMemoryAllocationHob (\r | |
1436aea4 MK |
500 | IN EFI_MEMORY_TYPE MemoryType,\r |
501 | IN UINTN Pages,\r | |
502 | IN UINTN Granularity,\r | |
503 | OUT EFI_PHYSICAL_ADDRESS *Memory\r | |
b2374cec SZ |
504 | )\r |
505 | {\r | |
1436aea4 MK |
506 | EFI_PEI_HOB_POINTERS Hob;\r |
507 | EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob;\r | |
508 | UINT64 Bytes;\r | |
509 | EFI_PHYSICAL_ADDRESS BaseAddress;\r | |
b2374cec SZ |
510 | \r |
511 | Bytes = LShiftU64 (Pages, EFI_PAGE_SHIFT);\r | |
512 | \r | |
1436aea4 | 513 | BaseAddress = 0;\r |
b2374cec | 514 | MemoryAllocationHob = NULL;\r |
1436aea4 | 515 | Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r |
b2374cec SZ |
516 | while (Hob.Raw != NULL) {\r |
517 | if ((Hob.MemoryAllocation->AllocDescriptor.MemoryType == EfiConventionalMemory) &&\r | |
1436aea4 MK |
518 | (Hob.MemoryAllocation->AllocDescriptor.MemoryLength >= Bytes))\r |
519 | {\r | |
b2374cec SZ |
520 | //\r |
521 | // Found one memory allocation HOB with big enough free memory.\r | |
522 | //\r | |
1436aea4 MK |
523 | MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;\r |
524 | BaseAddress = MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress +\r | |
525 | MemoryAllocationHob->AllocDescriptor.MemoryLength - Bytes;\r | |
b2374cec SZ |
526 | //\r |
527 | // Make sure the granularity could be satisfied.\r | |
528 | //\r | |
1436aea4 | 529 | BaseAddress &= ~((EFI_PHYSICAL_ADDRESS)Granularity - 1);\r |
b2374cec SZ |
530 | if (BaseAddress >= MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress) {\r |
531 | break;\r | |
532 | }\r | |
1436aea4 MK |
533 | \r |
534 | BaseAddress = 0;\r | |
b2374cec SZ |
535 | MemoryAllocationHob = NULL;\r |
536 | }\r | |
1436aea4 | 537 | \r |
b2374cec SZ |
538 | //\r |
539 | // Continue to find.\r | |
540 | //\r | |
541 | Hob.Raw = GET_NEXT_HOB (Hob);\r | |
542 | Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r | |
543 | }\r | |
544 | \r | |
545 | if (MemoryAllocationHob != NULL) {\r | |
546 | UpdateOrSplitMemoryAllocationHob (MemoryAllocationHob, BaseAddress, Bytes, MemoryType);\r | |
547 | *Memory = BaseAddress;\r | |
548 | return EFI_SUCCESS;\r | |
549 | } else {\r | |
550 | if (MergeFreeMemoryInMemoryAllocationHob ()) {\r | |
551 | //\r | |
552 | // Retry if there are free memory ranges merged.\r | |
553 | //\r | |
554 | return FindFreeMemoryFromMemoryAllocationHob (MemoryType, Pages, Granularity, Memory);\r | |
555 | }\r | |
1436aea4 | 556 | \r |
b2374cec SZ |
557 | return EFI_NOT_FOUND;\r |
558 | }\r | |
559 | }\r | |
560 | \r | |
561 | /**\r | |
562 | The purpose of the service is to publish an interface that allows\r | |
64ca6802 | 563 | PEIMs to allocate memory ranges that are managed by the PEI Foundation.\r |
b1f6a7c6 | 564 | \r |
b2374cec SZ |
565 | Prior to InstallPeiMemory() being called, PEI will allocate pages from the heap.\r |
566 | After InstallPeiMemory() is called, PEI will allocate pages within the region\r | |
567 | of memory provided by InstallPeiMemory() service in a best-effort fashion.\r | |
568 | Location-specific allocations are not managed by the PEI foundation code.\r | |
569 | \r | |
64ca6802 | 570 | @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r |
571 | @param MemoryType The type of memory to allocate.\r | |
572 | @param Pages The number of contiguous 4 KB pages to allocate.\r | |
b2374cec | 573 | @param Memory Pointer to a physical address. On output, the address is set to the base\r |
64ca6802 | 574 | of the page range that was allocated.\r |
b1f6a7c6 | 575 | \r |
64ca6802 | 576 | @retval EFI_SUCCESS The memory range was successfully allocated.\r |
577 | @retval EFI_OUT_OF_RESOURCES The pages could not be allocated.\r | |
b2374cec | 578 | @retval EFI_INVALID_PARAMETER Type is not equal to EfiLoaderCode, EfiLoaderData, EfiRuntimeServicesCode,\r |
64ca6802 | 579 | EfiRuntimeServicesData, EfiBootServicesCode, EfiBootServicesData,\r |
ab6fb25a | 580 | EfiACPIReclaimMemory, EfiReservedMemoryType, or EfiACPIMemoryNVS.\r |
b1f6a7c6 | 581 | \r |
582 | **/\r | |
192f6d4c | 583 | EFI_STATUS\r |
584 | EFIAPI\r | |
585 | PeiAllocatePages (\r | |
1436aea4 MK |
586 | IN CONST EFI_PEI_SERVICES **PeiServices,\r |
587 | IN EFI_MEMORY_TYPE MemoryType,\r | |
588 | IN UINTN Pages,\r | |
589 | OUT EFI_PHYSICAL_ADDRESS *Memory\r | |
192f6d4c | 590 | )\r |
192f6d4c | 591 | {\r |
1436aea4 MK |
592 | EFI_STATUS Status;\r |
593 | PEI_CORE_INSTANCE *PrivateData;\r | |
594 | EFI_PEI_HOB_POINTERS Hob;\r | |
595 | EFI_PHYSICAL_ADDRESS *FreeMemoryTop;\r | |
596 | EFI_PHYSICAL_ADDRESS *FreeMemoryBottom;\r | |
597 | UINTN RemainingPages;\r | |
598 | UINTN Granularity;\r | |
599 | UINTN Padding;\r | |
192f6d4c | 600 | \r |
64ca6802 | 601 | if ((MemoryType != EfiLoaderCode) &&\r |
602 | (MemoryType != EfiLoaderData) &&\r | |
603 | (MemoryType != EfiRuntimeServicesCode) &&\r | |
604 | (MemoryType != EfiRuntimeServicesData) &&\r | |
605 | (MemoryType != EfiBootServicesCode) &&\r | |
606 | (MemoryType != EfiBootServicesData) &&\r | |
607 | (MemoryType != EfiACPIReclaimMemory) &&\r | |
ab6fb25a | 608 | (MemoryType != EfiReservedMemoryType) &&\r |
1436aea4 MK |
609 | (MemoryType != EfiACPIMemoryNVS))\r |
610 | {\r | |
64ca6802 | 611 | return EFI_INVALID_PARAMETER;\r |
612 | }\r | |
613 | \r | |
e1e7e0fb AB |
614 | Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r |
615 | \r | |
b2374cec SZ |
616 | PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r |
617 | Hob.Raw = PrivateData->HobList.Raw;\r | |
618 | \r | |
619 | if (Hob.Raw == NULL) {\r | |
620 | //\r | |
621 | // HOB is not initialized yet.\r | |
622 | //\r | |
623 | return EFI_NOT_AVAILABLE_YET;\r | |
624 | }\r | |
625 | \r | |
1436aea4 MK |
626 | if ((RUNTIME_PAGE_ALLOCATION_GRANULARITY > DEFAULT_PAGE_ALLOCATION_GRANULARITY) &&\r |
627 | ((MemoryType == EfiACPIReclaimMemory) ||\r | |
628 | (MemoryType == EfiACPIMemoryNVS) ||\r | |
629 | (MemoryType == EfiRuntimeServicesCode) ||\r | |
630 | (MemoryType == EfiRuntimeServicesData)))\r | |
631 | {\r | |
e1e7e0fb AB |
632 | Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r |
633 | \r | |
1436aea4 MK |
634 | DEBUG ((\r |
635 | DEBUG_INFO,\r | |
636 | "AllocatePages: aligning allocation to %d KB\n",\r | |
637 | Granularity / SIZE_1KB\r | |
638 | ));\r | |
e1e7e0fb AB |
639 | }\r |
640 | \r | |
b2374cec | 641 | if (!PrivateData->PeiMemoryInstalled && PrivateData->SwitchStackSignal) {\r |
58dcdada | 642 | //\r |
b2374cec | 643 | // When PeiInstallMemory is called but temporary memory has *not* been moved to permanent memory,\r |
5f300691 | 644 | // the AllocatePage will depend on the field of PEI_CORE_INSTANCE structure.\r |
58dcdada | 645 | //\r |
1436aea4 MK |
646 | FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);\r |
647 | FreeMemoryBottom = &(PrivateData->PhysicalMemoryBegin);\r | |
58dcdada | 648 | } else {\r |
1436aea4 MK |
649 | FreeMemoryTop = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);\r |
650 | FreeMemoryBottom = &(Hob.HandoffInformationTable->EfiFreeMemoryBottom);\r | |
192f6d4c | 651 | }\r |
652 | \r | |
192f6d4c | 653 | //\r |
e1e7e0fb AB |
654 | // Check to see if on correct boundary for the memory type.\r |
655 | // If not aligned, make the allocation aligned.\r | |
192f6d4c | 656 | //\r |
e1e7e0fb | 657 | Padding = *(FreeMemoryTop) & (Granularity - 1);\r |
1436aea4 | 658 | if ((UINTN)(*FreeMemoryTop - *FreeMemoryBottom) < Padding) {\r |
e1e7e0fb AB |
659 | DEBUG ((DEBUG_ERROR, "AllocatePages failed: Out of space after padding.\n"));\r |
660 | return EFI_OUT_OF_RESOURCES;\r | |
661 | }\r | |
662 | \r | |
663 | *(FreeMemoryTop) -= Padding;\r | |
664 | if (Padding >= EFI_PAGE_SIZE) {\r | |
665 | //\r | |
666 | // Create a memory allocation HOB to cover\r | |
667 | // the pages that we will lose to rounding\r | |
668 | //\r | |
b2374cec | 669 | InternalBuildMemoryAllocationHob (\r |
e1e7e0fb AB |
670 | *(FreeMemoryTop),\r |
671 | Padding & ~(UINTN)EFI_PAGE_MASK,\r | |
672 | EfiConventionalMemory\r | |
673 | );\r | |
674 | }\r | |
5e574a01 | 675 | \r |
192f6d4c | 676 | //\r |
5e574a01 | 677 | // Verify that there is sufficient memory to satisfy the allocation.\r |
192f6d4c | 678 | //\r |
b2374cec | 679 | RemainingPages = (UINTN)(*FreeMemoryTop - *FreeMemoryBottom) >> EFI_PAGE_SHIFT;\r |
98a601b1 | 680 | //\r |
5e574a01 | 681 | // The number of remaining pages needs to be greater than or equal to that of the request pages.\r |
98a601b1 | 682 | //\r |
e1e7e0fb | 683 | Pages = ALIGN_VALUE (Pages, EFI_SIZE_TO_PAGES (Granularity));\r |
5e574a01 | 684 | if (RemainingPages < Pages) {\r |
b2374cec SZ |
685 | //\r |
686 | // Try to find free memory by searching memory allocation HOBs.\r | |
687 | //\r | |
688 | Status = FindFreeMemoryFromMemoryAllocationHob (MemoryType, Pages, Granularity, Memory);\r | |
689 | if (!EFI_ERROR (Status)) {\r | |
690 | return Status;\r | |
691 | }\r | |
1436aea4 MK |
692 | \r |
693 | DEBUG ((DEBUG_ERROR, "AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64)Pages));\r | |
694 | DEBUG ((DEBUG_ERROR, "There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64)RemainingPages));\r | |
695 | return EFI_OUT_OF_RESOURCES;\r | |
192f6d4c | 696 | } else {\r |
697 | //\r | |
698 | // Update the PHIT to reflect the memory usage\r | |
699 | //\r | |
58dcdada | 700 | *(FreeMemoryTop) -= Pages * EFI_PAGE_SIZE;\r |
192f6d4c | 701 | \r |
702 | //\r | |
703 | // Update the value for the caller\r | |
704 | //\r | |
58dcdada | 705 | *Memory = *(FreeMemoryTop);\r |
192f6d4c | 706 | \r |
707 | //\r | |
708 | // Create a memory allocation HOB.\r | |
709 | //\r | |
b2374cec | 710 | InternalBuildMemoryAllocationHob (\r |
58dcdada | 711 | *(FreeMemoryTop),\r |
0a7d0741 | 712 | Pages * EFI_PAGE_SIZE,\r |
192f6d4c | 713 | MemoryType\r |
714 | );\r | |
715 | \r | |
716 | return EFI_SUCCESS;\r | |
717 | }\r | |
718 | }\r | |
719 | \r | |
b2374cec SZ |
720 | /**\r |
721 | Mark the memory allocation HOB to be unused(freed) and update *FreeMemoryTop\r | |
722 | if MemoryBaseAddress == *FreeMemoryTop.\r | |
723 | \r | |
724 | @param[in] PrivateData Pointer to PeiCore's private data structure.\r | |
725 | @param[in, out] MemoryAllocationHobToFree Pointer to memory allocation HOB to be freed.\r | |
726 | \r | |
727 | **/\r | |
728 | VOID\r | |
729 | FreeMemoryAllocationHob (\r | |
1436aea4 MK |
730 | IN PEI_CORE_INSTANCE *PrivateData,\r |
731 | IN OUT EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHobToFree\r | |
b2374cec SZ |
732 | )\r |
733 | {\r | |
1436aea4 MK |
734 | EFI_PEI_HOB_POINTERS Hob;\r |
735 | EFI_PHYSICAL_ADDRESS *FreeMemoryTop;\r | |
736 | EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob;\r | |
b2374cec SZ |
737 | \r |
738 | Hob.Raw = PrivateData->HobList.Raw;\r | |
739 | \r | |
740 | if (!PrivateData->PeiMemoryInstalled && PrivateData->SwitchStackSignal) {\r | |
741 | //\r | |
742 | // When PeiInstallMemory is called but temporary memory has *not* been moved to permanent memory,\r | |
743 | // use the FreePhysicalMemoryTop field of PEI_CORE_INSTANCE structure.\r | |
744 | //\r | |
745 | FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);\r | |
746 | } else {\r | |
747 | FreeMemoryTop = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);\r | |
748 | }\r | |
749 | \r | |
750 | if (MemoryAllocationHobToFree->AllocDescriptor.MemoryBaseAddress == *FreeMemoryTop) {\r | |
751 | //\r | |
752 | // Update *FreeMemoryTop.\r | |
753 | //\r | |
754 | *FreeMemoryTop += MemoryAllocationHobToFree->AllocDescriptor.MemoryLength;\r | |
755 | //\r | |
756 | // Mark the memory allocation HOB to be unused(freed).\r | |
757 | //\r | |
758 | MemoryAllocationHobToFree->Header.HobType = EFI_HOB_TYPE_UNUSED;\r | |
759 | \r | |
760 | MemoryAllocationHob = NULL;\r | |
1436aea4 | 761 | Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r |
b2374cec SZ |
762 | while (Hob.Raw != NULL) {\r |
763 | if ((Hob.MemoryAllocation->AllocDescriptor.MemoryType == EfiConventionalMemory) &&\r | |
1436aea4 MK |
764 | (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress == *FreeMemoryTop))\r |
765 | {\r | |
b2374cec SZ |
766 | //\r |
767 | // Found memory allocation HOB that has EfiConventionalMemory MemoryType and\r | |
768 | // MemoryBaseAddress == new *FreeMemoryTop.\r | |
769 | //\r | |
1436aea4 | 770 | MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;\r |
b2374cec SZ |
771 | break;\r |
772 | }\r | |
1436aea4 | 773 | \r |
b2374cec SZ |
774 | Hob.Raw = GET_NEXT_HOB (Hob);\r |
775 | Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r | |
776 | }\r | |
1436aea4 | 777 | \r |
b2374cec SZ |
778 | //\r |
779 | // Free memory allocation HOB iteratively.\r | |
780 | //\r | |
781 | if (MemoryAllocationHob != NULL) {\r | |
782 | FreeMemoryAllocationHob (PrivateData, MemoryAllocationHob);\r | |
783 | }\r | |
784 | }\r | |
785 | }\r | |
786 | \r | |
787 | /**\r | |
788 | Frees memory pages.\r | |
789 | \r | |
790 | @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r | |
791 | @param[in] Memory The base physical address of the pages to be freed.\r | |
792 | @param[in] Pages The number of contiguous 4 KB pages to free.\r | |
793 | \r | |
794 | @retval EFI_SUCCESS The requested pages were freed.\r | |
795 | @retval EFI_INVALID_PARAMETER Memory is not a page-aligned address or Pages is invalid.\r | |
796 | @retval EFI_NOT_FOUND The requested memory pages were not allocated with\r | |
797 | AllocatePages().\r | |
798 | \r | |
799 | **/\r | |
800 | EFI_STATUS\r | |
801 | EFIAPI\r | |
802 | PeiFreePages (\r | |
1436aea4 MK |
803 | IN CONST EFI_PEI_SERVICES **PeiServices,\r |
804 | IN EFI_PHYSICAL_ADDRESS Memory,\r | |
805 | IN UINTN Pages\r | |
b2374cec SZ |
806 | )\r |
807 | {\r | |
1436aea4 MK |
808 | PEI_CORE_INSTANCE *PrivateData;\r |
809 | UINT64 Bytes;\r | |
810 | UINT64 Start;\r | |
811 | UINT64 End;\r | |
812 | EFI_PEI_HOB_POINTERS Hob;\r | |
813 | EFI_HOB_MEMORY_ALLOCATION *MemoryAllocationHob;\r | |
b2374cec SZ |
814 | \r |
815 | Bytes = LShiftU64 (Pages, EFI_PAGE_SHIFT);\r | |
816 | Start = Memory;\r | |
1436aea4 | 817 | End = Start + Bytes - 1;\r |
b2374cec | 818 | \r |
1436aea4 | 819 | if ((Pages == 0) || ((Start & EFI_PAGE_MASK) != 0) || (Start >= End)) {\r |
b2374cec SZ |
820 | return EFI_INVALID_PARAMETER;\r |
821 | }\r | |
822 | \r | |
823 | PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r | |
824 | Hob.Raw = PrivateData->HobList.Raw;\r | |
825 | \r | |
826 | if (Hob.Raw == NULL) {\r | |
827 | //\r | |
828 | // HOB is not initialized yet.\r | |
829 | //\r | |
830 | return EFI_NOT_AVAILABLE_YET;\r | |
831 | }\r | |
832 | \r | |
833 | MemoryAllocationHob = NULL;\r | |
1436aea4 | 834 | Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r |
b2374cec SZ |
835 | while (Hob.Raw != NULL) {\r |
836 | if ((Hob.MemoryAllocation->AllocDescriptor.MemoryType != EfiConventionalMemory) &&\r | |
837 | (Memory >= Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress) &&\r | |
1436aea4 MK |
838 | ((Memory + Bytes) <= (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress + Hob.MemoryAllocation->AllocDescriptor.MemoryLength)))\r |
839 | {\r | |
b2374cec SZ |
840 | //\r |
841 | // Found the memory allocation HOB that includes the memory pages to be freed.\r | |
842 | //\r | |
1436aea4 | 843 | MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;\r |
b2374cec SZ |
844 | break;\r |
845 | }\r | |
1436aea4 | 846 | \r |
b2374cec SZ |
847 | Hob.Raw = GET_NEXT_HOB (Hob);\r |
848 | Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r | |
849 | }\r | |
850 | \r | |
851 | if (MemoryAllocationHob != NULL) {\r | |
852 | UpdateOrSplitMemoryAllocationHob (MemoryAllocationHob, Memory, Bytes, EfiConventionalMemory);\r | |
853 | FreeMemoryAllocationHob (PrivateData, MemoryAllocationHob);\r | |
854 | return EFI_SUCCESS;\r | |
855 | } else {\r | |
856 | return EFI_NOT_FOUND;\r | |
857 | }\r | |
858 | }\r | |
859 | \r | |
b1f6a7c6 | 860 | /**\r |
861 | \r | |
d39d1260 | 862 | Pool allocation service. Before permanent memory is discovered, the pool will\r |
d3add11e MK |
863 | be allocated in the heap in temporary memory. Generally, the size of the heap in temporary\r |
864 | memory does not exceed 64K, so the biggest pool size could be allocated is\r | |
82b8c8df | 865 | 64K.\r |
b1f6a7c6 | 866 | \r |
82b8c8df | 867 | @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r |
868 | @param Size Amount of memory required\r | |
869 | @param Buffer Address of pointer to the buffer\r | |
b1f6a7c6 | 870 | \r |
871 | @retval EFI_SUCCESS The allocation was successful\r | |
872 | @retval EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement\r | |
873 | to allocate the requested size.\r | |
874 | \r | |
875 | **/\r | |
192f6d4c | 876 | EFI_STATUS\r |
877 | EFIAPI\r | |
878 | PeiAllocatePool (\r | |
1436aea4 MK |
879 | IN CONST EFI_PEI_SERVICES **PeiServices,\r |
880 | IN UINTN Size,\r | |
881 | OUT VOID **Buffer\r | |
192f6d4c | 882 | )\r |
192f6d4c | 883 | {\r |
1436aea4 MK |
884 | EFI_STATUS Status;\r |
885 | EFI_HOB_MEMORY_POOL *Hob;\r | |
192f6d4c | 886 | \r |
82b8c8df | 887 | //\r |
888 | // If some "post-memory" PEIM wishes to allocate larger pool,\r | |
889 | // it should use AllocatePages service instead.\r | |
890 | //\r | |
d1102dba | 891 | \r |
82b8c8df | 892 | //\r |
d3add11e | 893 | // Generally, the size of heap in temporary memory does not exceed 64K,\r |
d39d1260 | 894 | // HobLength is multiples of 8 bytes, so the maximum size of pool is 0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL)\r |
82b8c8df | 895 | //\r |
e94728b3 | 896 | if (Size > (0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL))) {\r |
2e76dc7e | 897 | return EFI_OUT_OF_RESOURCES;\r |
898 | }\r | |
d1102dba | 899 | \r |
82b8c8df | 900 | Status = PeiServicesCreateHob (\r |
192f6d4c | 901 | EFI_HOB_TYPE_MEMORY_POOL,\r |
902 | (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),\r | |
903 | (VOID **)&Hob\r | |
904 | );\r | |
772cba09 | 905 | ASSERT_EFI_ERROR (Status);\r |
8797683f BB |
906 | \r |
907 | if (EFI_ERROR (Status)) {\r | |
908 | *Buffer = NULL;\r | |
909 | } else {\r | |
910 | *Buffer = Hob + 1;\r | |
911 | }\r | |
192f6d4c | 912 | \r |
192f6d4c | 913 | return Status;\r |
914 | }\r |