]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Memory/MemoryServices.c
Add PeimDispatcherReenter to fix bug of PeiDispatch can not be quit correctly.
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Memory / MemoryServices.c
CommitLineData
615c6dd0 1/** @file\r
b1f6a7c6 2 EFI PEI Core memory services\r
3 \r
192f6d4c 4Copyright (c) 2006, Intel Corporation \r
5All rights reserved. This program and the accompanying materials \r
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
192f6d4c 15#include <PeiMain.h>\r
16\r
b1f6a7c6 17/**\r
192f6d4c 18\r
19 Initialize the memory services.\r
20\r
d9494916 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
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
46\r
58dcdada 47 PrivateData->BottomOfCarHeap = SecCoreData->PeiTemporaryRamBase; \r
48 PrivateData->TopOfCarHeap = (VOID *)((UINTN)(PrivateData->BottomOfCarHeap) + SecCoreData->PeiTemporaryRamSize);\r
49 PrivateData->SizeOfTemporaryMemory = SecCoreData->TemporaryRamSize;\r
50 PrivateData->StackSize = (UINT64) SecCoreData->StackSize;\r
51 \r
192f6d4c 52 DEBUG_CODE_BEGIN ();\r
5aae0aa7 53 PrivateData->SizeOfCacheAsRam = SecCoreData->PeiTemporaryRamSize + SecCoreData->StackSize;\r
54 PrivateData->MaxTopOfCarHeap = (VOID *) ((UINTN) PrivateData->BottomOfCarHeap + (UINTN) PrivateData->SizeOfCacheAsRam);\r
b0d803fe 55 PrivateData->StackBase = (EFI_PHYSICAL_ADDRESS) (UINTN) SecCoreData->StackBase;\r
56 PrivateData->StackSize = (UINT64) SecCoreData->StackSize;\r
192f6d4c 57 DEBUG_CODE_END ();\r
58\r
59 PrivateData->HobList.Raw = PrivateData->BottomOfCarHeap;\r
60 \r
61 PeiCoreBuildHobHandoffInfoTable (\r
62 BOOT_WITH_FULL_CONFIGURATION,\r
63 (EFI_PHYSICAL_ADDRESS) (UINTN) PrivateData->BottomOfCarHeap,\r
5aae0aa7 64 (UINTN) SecCoreData->PeiTemporaryRamSize\r
192f6d4c 65 );\r
192f6d4c 66\r
67 //\r
68 // Set PS to point to ServiceTableShadow in Cache\r
69 //\r
70 PrivateData->PS = &(PrivateData->ServiceTableShadow);\r
b0d803fe 71 }\r
72 \r
192f6d4c 73 return;\r
74}\r
75\r
b1f6a7c6 76/**\r
77\r
d9494916 78 This function registers the found memory configuration with the PEI Foundation.\r
b1f6a7c6 79\r
d9494916 80 The usage model is that the PEIM that discovers the permanent memory shall invoke this service.\r
81 This routine will hold discoveried memory information into PeiCore's private data,\r
82 and set SwitchStackSignal flag. After PEIM who discovery memory is dispatched,\r
83 PeiDispatcher will migrate temporary memory to permenement memory.\r
84 \r
d73d93c3 85 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
86 @param MemoryBegin Start of memory address.\r
87 @param MemoryLength Length of memory.\r
b1f6a7c6 88\r
89 @return EFI_SUCCESS Always success.\r
90\r
91**/\r
192f6d4c 92EFI_STATUS\r
93EFIAPI\r
94PeiInstallPeiMemory (\r
58dcdada 95 IN CONST EFI_PEI_SERVICES **PeiServices,\r
96 IN EFI_PHYSICAL_ADDRESS MemoryBegin,\r
97 IN UINT64 MemoryLength\r
192f6d4c 98 )\r
192f6d4c 99{\r
100 PEI_CORE_INSTANCE *PrivateData;\r
58dcdada 101\r
a3a15d21 102 DEBUG ((EFI_D_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));\r
192f6d4c 103 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
104\r
58dcdada 105 PrivateData->PhysicalMemoryBegin = MemoryBegin;\r
106 PrivateData->PhysicalMemoryLength = MemoryLength;\r
107 PrivateData->FreePhysicalMemoryTop = MemoryBegin + MemoryLength;\r
108 \r
109 PrivateData->SwitchStackSignal = TRUE;\r
192f6d4c 110\r
111 return EFI_SUCCESS; \r
112}\r
113\r
b1f6a7c6 114/**\r
115\r
116 Memory allocation service on permanent memory,\r
117 not usable prior to the memory installation.\r
118\r
119\r
d73d93c3 120 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
121 @param MemoryType Type of memory to allocate.\r
122 @param Pages Number of pages to allocate.\r
123 @param Memory Pointer of memory allocated.\r
b1f6a7c6 124\r
125 @retval EFI_SUCCESS The allocation was successful\r
b1f6a7c6 126 @retval EFI_NOT_AVAILABLE_YET Called with permanent memory not available\r
127 @retval EFI_OUT_OF_RESOURCES There is not enough HOB heap to satisfy the requirement\r
128 to allocate the number of pages.\r
129\r
130**/\r
192f6d4c 131EFI_STATUS\r
132EFIAPI\r
133PeiAllocatePages (\r
d9494916 134 IN CONST EFI_PEI_SERVICES **PeiServices,\r
135 IN EFI_MEMORY_TYPE MemoryType,\r
136 IN UINTN Pages,\r
137 OUT EFI_PHYSICAL_ADDRESS *Memory\r
192f6d4c 138 )\r
192f6d4c 139{\r
140 PEI_CORE_INSTANCE *PrivateData;\r
141 EFI_PEI_HOB_POINTERS Hob;\r
58dcdada 142 EFI_PHYSICAL_ADDRESS *FreeMemoryTop;\r
143 EFI_PHYSICAL_ADDRESS *FreeMemoryBottom;\r
192f6d4c 144\r
145 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
58dcdada 146 Hob.Raw = PrivateData->HobList.Raw;\r
147 \r
192f6d4c 148 //\r
149 // Check if Hob already available\r
150 //\r
151 if (!PrivateData->PeiMemoryInstalled) {\r
58dcdada 152 //\r
153 // When PeiInstallMemory is called but CAR has *not* been moved to temporary memory,\r
d9494916 154 // the AllocatePage will dependent on the field of PEI_CORE_INSTANCE structure.\r
58dcdada 155 //\r
156 if (!PrivateData->SwitchStackSignal) {\r
157 return EFI_NOT_AVAILABLE_YET;\r
158 } else {\r
159 FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);\r
160 FreeMemoryBottom = &(PrivateData->PhysicalMemoryBegin);\r
161 }\r
162 } else {\r
163 FreeMemoryTop = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);\r
164 FreeMemoryBottom = &(Hob.HandoffInformationTable->EfiFreeMemoryBottom);\r
192f6d4c 165 }\r
166\r
192f6d4c 167 //\r
d9494916 168 // Check to see if on 4k boundary, If not aligned, make the allocation aligned.\r
192f6d4c 169 //\r
d9494916 170 *(FreeMemoryTop) -= *(FreeMemoryTop) & 0xFFF;\r
58dcdada 171 \r
192f6d4c 172 //\r
173 // Verify that there is sufficient memory to satisfy the allocation\r
174 //\r
58dcdada 175 if (*(FreeMemoryTop) - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < \r
176 *(FreeMemoryBottom)) {\r
192f6d4c 177 DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%x Pages is available.\n", Pages));\r
178 DEBUG ((EFI_D_ERROR, "There is only left 0x%x pages memory resource to be allocated.\n", \\r
58dcdada 179 EFI_SIZE_TO_PAGES ((UINTN) (*(FreeMemoryTop) - *(FreeMemoryBottom)))));\r
192f6d4c 180 return EFI_OUT_OF_RESOURCES;\r
181 } else {\r
182 //\r
183 // Update the PHIT to reflect the memory usage\r
184 //\r
58dcdada 185 *(FreeMemoryTop) -= Pages * EFI_PAGE_SIZE;\r
192f6d4c 186\r
187 //\r
188 // Update the value for the caller\r
189 //\r
58dcdada 190 *Memory = *(FreeMemoryTop);\r
192f6d4c 191\r
192 //\r
193 // Create a memory allocation HOB.\r
194 //\r
195 BuildMemoryAllocationHob (\r
58dcdada 196 *(FreeMemoryTop),\r
0a7d0741 197 Pages * EFI_PAGE_SIZE,\r
192f6d4c 198 MemoryType\r
199 );\r
200\r
201 return EFI_SUCCESS;\r
202 }\r
203}\r
204\r
b1f6a7c6 205/**\r
206\r
d9494916 207 Pool allocation service. Before permenent memory is discoveried, the pool will \r
208 be allocated the heap in the CAR. Genenrally, the size of heap in temporary \r
209 memory does not exceed to 64K, so the biggest pool size could be allocated is \r
210 64K.\r
b1f6a7c6 211\r
d9494916 212 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
213 @param Size Amount of memory required\r
214 @param Buffer Address of pointer to the buffer\r
b1f6a7c6 215\r
216 @retval EFI_SUCCESS The allocation was successful\r
217 @retval EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement\r
218 to allocate the requested size.\r
219\r
220**/\r
192f6d4c 221EFI_STATUS\r
222EFIAPI\r
223PeiAllocatePool (\r
d9494916 224 IN CONST EFI_PEI_SERVICES **PeiServices,\r
225 IN UINTN Size,\r
226 OUT VOID **Buffer\r
192f6d4c 227 )\r
192f6d4c 228{\r
229 EFI_STATUS Status;\r
230 EFI_HOB_MEMORY_POOL *Hob;\r
231\r
d9494916 232 //\r
233 // If some "post-memory" PEIM wishes to allocate larger pool,\r
234 // it should use AllocatePages service instead.\r
235 //\r
236 \r
237 //\r
238 // Generally, the size of heap in temporary memory does not exceed to 64K,\r
239 // so the maxmium size of pool is 0x10000 - sizeof (EFI_HOB_MEMORY_POOL)\r
240 //\r
241 ASSERT (Size < 0x10000 - sizeof (EFI_HOB_MEMORY_POOL));\r
242 Status = PeiServicesCreateHob (\r
192f6d4c 243 EFI_HOB_TYPE_MEMORY_POOL,\r
244 (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),\r
245 (VOID **)&Hob\r
246 );\r
247 *Buffer = Hob+1; \r
248\r
192f6d4c 249 return Status;\r
250}\r