]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Memory/MemoryServices.c
Rebuilt per the Hess's bug fixes
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Memory / MemoryServices.c
CommitLineData
192f6d4c 1/*++\r
2\r
3Copyright (c) 2006, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 MemoryServices.c\r
15\r
16Abstract:\r
17\r
18 EFI PEI Core memory services\r
19\r
20--*/\r
21\r
192f6d4c 22#include <PeiMain.h>\r
23\r
24VOID\r
25InitializeMemoryServices (\r
b0d803fe 26 IN PEI_CORE_INSTANCE *PrivateData,\r
5aae0aa7 27 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
192f6d4c 28 IN PEI_CORE_INSTANCE *OldCoreData\r
29 )\r
30/*++\r
31\r
32Routine Description:\r
33\r
34 Initialize the memory services.\r
35\r
36Arguments:\r
37\r
38 PeiServices - The PEI core services table.\r
5aae0aa7 39 SecCoreData - Points to a data structure containing information about the PEI core's operating\r
40 environment, such as the size and location of temporary RAM, the stack location and\r
41 the BFV location.\r
42\r
192f6d4c 43 OldCoreData - Pointer to the PEI Core data.\r
44 NULL if being run in non-permament memory mode.\r
45\r
46Returns:\r
47\r
48 None\r
49\r
50--*/\r
51{\r
192f6d4c 52 PrivateData->SwitchStackSignal = FALSE;\r
53\r
54 if (OldCoreData == NULL) {\r
55\r
56 PrivateData->PeiMemoryInstalled = FALSE;\r
57\r
5aae0aa7 58 PrivateData->BottomOfCarHeap = SecCoreData->PeiTemporaryRamBase; \r
59 PrivateData->TopOfCarHeap = (VOID *)((UINTN)(PrivateData->BottomOfCarHeap) + SecCoreData->PeiTemporaryRamSize);\r
192f6d4c 60 \r
61 DEBUG_CODE_BEGIN ();\r
5aae0aa7 62 PrivateData->SizeOfCacheAsRam = SecCoreData->PeiTemporaryRamSize + SecCoreData->StackSize;\r
63 PrivateData->MaxTopOfCarHeap = (VOID *) ((UINTN) PrivateData->BottomOfCarHeap + (UINTN) PrivateData->SizeOfCacheAsRam);\r
b0d803fe 64 PrivateData->StackBase = (EFI_PHYSICAL_ADDRESS) (UINTN) SecCoreData->StackBase;\r
65 PrivateData->StackSize = (UINT64) SecCoreData->StackSize;\r
192f6d4c 66 DEBUG_CODE_END ();\r
67\r
68 PrivateData->HobList.Raw = PrivateData->BottomOfCarHeap;\r
69 \r
70 PeiCoreBuildHobHandoffInfoTable (\r
71 BOOT_WITH_FULL_CONFIGURATION,\r
72 (EFI_PHYSICAL_ADDRESS) (UINTN) PrivateData->BottomOfCarHeap,\r
5aae0aa7 73 (UINTN) SecCoreData->PeiTemporaryRamSize\r
192f6d4c 74 );\r
192f6d4c 75\r
76 //\r
77 // Set PS to point to ServiceTableShadow in Cache\r
78 //\r
79 PrivateData->PS = &(PrivateData->ServiceTableShadow);\r
b0d803fe 80 }\r
81 \r
192f6d4c 82 return;\r
83}\r
84\r
85EFI_STATUS\r
86EFIAPI\r
87PeiInstallPeiMemory (\r
0c2b5da8 88 IN CONST EFI_PEI_SERVICES **PeiServices,\r
192f6d4c 89 IN EFI_PHYSICAL_ADDRESS MemoryBegin,\r
90 IN UINT64 MemoryLength\r
91 )\r
92/*++\r
93\r
94Routine Description:\r
95\r
96 Install the permanent memory is now available.\r
97 Creates HOB (PHIT and Stack).\r
98\r
99Arguments:\r
100\r
101 PeiServices - The PEI core services table.\r
102 MemoryBegin - Start of memory address.\r
103 MemoryLength - Length of memory.\r
104\r
105Returns:\r
106\r
107 Status - EFI_SUCCESS\r
108 \r
109--*/\r
110{\r
111 PEI_CORE_INSTANCE *PrivateData;\r
112 EFI_HOB_HANDOFF_INFO_TABLE *OldHandOffHob;\r
113 EFI_HOB_HANDOFF_INFO_TABLE *NewHandOffHob;\r
114 UINT64 PeiStackSize;\r
115 UINT64 EfiFreeMemorySize;\r
116 EFI_PHYSICAL_ADDRESS PhysicalAddressOfOldHob;\r
a3a15d21 117\r
39d262c4 118 if (MemoryLength > (MAX_ADDRESS - MemoryBegin + 1))\r
119 return EFI_INVALID_PARAMETER;\r
120 \r
121 \r
a3a15d21 122 DEBUG ((EFI_D_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));\r
192f6d4c 123 \r
124 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
125\r
126 PrivateData->SwitchStackSignal = TRUE;\r
127 PrivateData->PeiMemoryInstalled = TRUE;\r
192f6d4c 128 \r
39d262c4 129 //\r
130 // Ensure the stack base is in page alignment \r
131 //\r
132 PrivateData->StackBase = ((UINTN)MemoryBegin + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;\r
133 PeiStackSize = (RShiftU64 (MemoryLength, 1) + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;\r
134 \r
192f6d4c 135 if (PEI_STACK_SIZE > PeiStackSize) {\r
136 PrivateData->StackSize = PeiStackSize;\r
137 } else {\r
138 PrivateData->StackSize = PEI_STACK_SIZE;\r
139 }\r
140\r
141 OldHandOffHob = PrivateData->HobList.HandoffInformationTable;\r
142\r
39d262c4 143 PrivateData->HobList.Raw = (VOID *)((UINTN)(PrivateData->StackBase + PrivateData->StackSize));\r
192f6d4c 144 NewHandOffHob = PrivateData->HobList.HandoffInformationTable;\r
145 PhysicalAddressOfOldHob = (EFI_PHYSICAL_ADDRESS) (UINTN) OldHandOffHob;\r
146\r
147 EfiFreeMemorySize = OldHandOffHob->EfiFreeMemoryBottom - PhysicalAddressOfOldHob;\r
148 \r
a3a15d21 149 DEBUG ((EFI_D_INFO, "HOBLIST address before memory init = 0x%p\n", OldHandOffHob));\r
150 DEBUG ((EFI_D_INFO, "HOBLIST address after memory init = 0x%p\n", NewHandOffHob));\r
192f6d4c 151\r
152 CopyMem (\r
153 NewHandOffHob,\r
154 OldHandOffHob,\r
155 (UINTN)EfiFreeMemorySize\r
156 );\r
157\r
158 NewHandOffHob->EfiMemoryTop = MemoryBegin + MemoryLength;\r
159 NewHandOffHob->EfiFreeMemoryTop = NewHandOffHob->EfiMemoryTop;\r
160 NewHandOffHob->EfiMemoryBottom = MemoryBegin;\r
161 \r
162 NewHandOffHob->EfiFreeMemoryBottom = (UINTN)NewHandOffHob + EfiFreeMemorySize; \r
163 \r
164 NewHandOffHob->EfiEndOfHobList = (UINTN)NewHandOffHob +\r
165 (OldHandOffHob->EfiEndOfHobList -\r
166 PhysicalAddressOfOldHob);\r
a3a15d21 167 \r
168 //\r
169 // For IPF in CAR mode the real memory access is uncached,in InstallPeiMemory()\r
170 // the 63-bit of address is set to 1.\r
171 //\r
172 SWITCH_TO_CACHE_MODE (PrivateData);\r
173 \r
192f6d4c 174 ConvertPpiPointers (PeiServices, OldHandOffHob, NewHandOffHob);\r
175\r
176 BuildStackHob (PrivateData->StackBase, PrivateData->StackSize);\r
177 \r
178\r
179 return EFI_SUCCESS; \r
180}\r
181\r
182EFI_STATUS\r
183EFIAPI\r
184PeiAllocatePages (\r
0c2b5da8 185 IN CONST EFI_PEI_SERVICES **PeiServices,\r
192f6d4c 186 IN EFI_MEMORY_TYPE MemoryType,\r
187 IN UINTN Pages,\r
188 OUT EFI_PHYSICAL_ADDRESS *Memory\r
189 )\r
190/*++\r
191\r
192Routine Description:\r
193\r
194 Memory allocation service on permanent memory, \r
195 not usable prior to the memory installation.\r
196\r
197Arguments:\r
198\r
199 PeiServices - The PEI core services table.\r
200 MemoryType - Type of memory to allocate.\r
201 Pages - Number of pages to allocate.\r
202 Memory - Pointer of memory allocated.\r
203\r
204Returns:\r
205\r
206 Status - EFI_SUCCESS The allocation was successful\r
207 EFI_INVALID_PARAMETER Only AllocateAnyAddress is supported.\r
208 EFI_NOT_AVAILABLE_YET Called with permanent memory not available\r
209 EFI_OUT_OF_RESOURCES There is not enough HOB heap to satisfy the requirement\r
210 to allocate the number of pages.\r
211\r
212--*/\r
213{\r
214 PEI_CORE_INSTANCE *PrivateData;\r
215 EFI_PEI_HOB_POINTERS Hob;\r
216 EFI_PHYSICAL_ADDRESS Offset;\r
217\r
218 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
219\r
220 //\r
221 // Check if Hob already available\r
222 //\r
223 if (!PrivateData->PeiMemoryInstalled) {\r
224 return EFI_NOT_AVAILABLE_YET;\r
225 }\r
226\r
227 Hob.Raw = PrivateData->HobList.Raw;\r
228\r
229 //\r
230 // Check to see if on 4k boundary\r
231 //\r
232 Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;\r
233\r
234 //\r
235 // If not aligned, make the allocation aligned.\r
236 //\r
237 if (Offset != 0) {\r
238 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;\r
239 }\r
240\r
a3a15d21 241 ASSERT (Hob.HandoffInformationTable->EfiFreeMemoryTop >= Hob.HandoffInformationTable->EfiFreeMemoryBottom);\r
192f6d4c 242 //\r
243 // Verify that there is sufficient memory to satisfy the allocation\r
244 //\r
245 if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < \r
246 Hob.HandoffInformationTable->EfiFreeMemoryBottom) {\r
247 DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%x Pages is available.\n", Pages));\r
248 DEBUG ((EFI_D_ERROR, "There is only left 0x%x pages memory resource to be allocated.\n", \\r
249 EFI_SIZE_TO_PAGES ((UINTN) (Hob.HandoffInformationTable->EfiFreeMemoryTop - Hob.HandoffInformationTable->EfiFreeMemoryBottom))));\r
250 return EFI_OUT_OF_RESOURCES;\r
251 } else {\r
252 //\r
253 // Update the PHIT to reflect the memory usage\r
254 //\r
255 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;\r
256\r
257 //\r
258 // Update the value for the caller\r
259 //\r
260 *Memory = Hob.HandoffInformationTable->EfiFreeMemoryTop;\r
261\r
262 //\r
263 // Create a memory allocation HOB.\r
264 //\r
265 BuildMemoryAllocationHob (\r
266 Hob.HandoffInformationTable->EfiFreeMemoryTop,\r
0a7d0741 267 Pages * EFI_PAGE_SIZE,\r
192f6d4c 268 MemoryType\r
269 );\r
270\r
271 return EFI_SUCCESS;\r
272 }\r
273}\r
274\r
275\r
276EFI_STATUS\r
277EFIAPI\r
278PeiAllocatePool (\r
0c2b5da8 279 IN CONST EFI_PEI_SERVICES **PeiServices,\r
192f6d4c 280 IN UINTN Size,\r
281 OUT VOID **Buffer\r
282 )\r
283/*++\r
284\r
285Routine Description:\r
286\r
287 Memory allocation service on the CAR. \r
288\r
289Arguments:\r
290\r
291 PeiServices - The PEI core services table.\r
292\r
293 Size - Amount of memory required\r
294\r
295 Buffer - Address of pointer to the buffer\r
296\r
297Returns:\r
298\r
299 Status - EFI_SUCCESS The allocation was successful\r
300 EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement\r
301 to allocate the requested size.\r
302 \r
303--*/\r
304{\r
305 EFI_STATUS Status;\r
306 EFI_HOB_MEMORY_POOL *Hob;\r
307\r
308 //\r
309 // If some "post-memory" PEIM wishes to allocate larger pool,\r
310 // it should use AllocatePages service instead.\r
311 //\r
312 ASSERT (Size < 0x10000 - sizeof (EFI_HOB_MEMORY_POOL));\r
313 Status = PeiServicesCreateHob (\r
314 EFI_HOB_TYPE_MEMORY_POOL,\r
315 (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),\r
316 (VOID **)&Hob\r
317 );\r
318 *Buffer = Hob+1; \r
319\r
320\r
321 return Status;\r
322}\r