3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 EFI PEI Core memory services
25 InitializeMemoryServices (
26 IN EFI_PEI_SERVICES
**PeiServices
,
27 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
,
28 IN PEI_CORE_INSTANCE
*OldCoreData
34 Initialize the memory services.
38 PeiServices - The PEI core services table.
39 SecCoreData - Points to a data structure containing information about the PEI core's operating
40 environment, such as the size and location of temporary RAM, the stack location and
43 OldCoreData - Pointer to the PEI Core data.
44 NULL if being run in non-permament memory mode.
52 PEI_CORE_INSTANCE
*PrivateData
;
54 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
55 PrivateData
->SwitchStackSignal
= FALSE
;
57 if (OldCoreData
== NULL
) {
59 PrivateData
->PeiMemoryInstalled
= FALSE
;
61 PrivateData
->BottomOfCarHeap
= SecCoreData
->PeiTemporaryRamBase
;
62 PrivateData
->TopOfCarHeap
= (VOID
*)((UINTN
)(PrivateData
->BottomOfCarHeap
) + SecCoreData
->PeiTemporaryRamSize
);
65 PrivateData
->SizeOfCacheAsRam
= SecCoreData
->PeiTemporaryRamSize
+ SecCoreData
->StackSize
;
66 PrivateData
->MaxTopOfCarHeap
= (VOID
*) ((UINTN
) PrivateData
->BottomOfCarHeap
+ (UINTN
) PrivateData
->SizeOfCacheAsRam
);
69 PrivateData
->HobList
.Raw
= PrivateData
->BottomOfCarHeap
;
71 PeiCoreBuildHobHandoffInfoTable (
72 BOOT_WITH_FULL_CONFIGURATION
,
73 (EFI_PHYSICAL_ADDRESS
) (UINTN
) PrivateData
->BottomOfCarHeap
,
74 (UINTN
) SecCoreData
->PeiTemporaryRamSize
77 // Copy PeiServices from ROM to Cache in PrivateData
79 CopyMem (&(PrivateData
->ServiceTableShadow
), *PeiServices
, sizeof (EFI_PEI_SERVICES
));
82 // Set PS to point to ServiceTableShadow in Cache
84 PrivateData
->PS
= &(PrivateData
->ServiceTableShadow
);
87 // Set PS to point to ServiceTableShadow in Cache one time after the
88 // stack switched to main memory
90 PrivateData
->PS
= &(PrivateData
->ServiceTableShadow
);
99 IN CONST EFI_PEI_SERVICES
**PeiServices
,
100 IN EFI_PHYSICAL_ADDRESS MemoryBegin
,
101 IN UINT64 MemoryLength
107 Install the permanent memory is now available.
108 Creates HOB (PHIT and Stack).
112 PeiServices - The PEI core services table.
113 MemoryBegin - Start of memory address.
114 MemoryLength - Length of memory.
122 PEI_CORE_INSTANCE
*PrivateData
;
123 EFI_HOB_HANDOFF_INFO_TABLE
*OldHandOffHob
;
124 EFI_HOB_HANDOFF_INFO_TABLE
*NewHandOffHob
;
126 UINT64 EfiFreeMemorySize
;
127 EFI_PHYSICAL_ADDRESS PhysicalAddressOfOldHob
;
129 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
131 PrivateData
->SwitchStackSignal
= TRUE
;
132 PrivateData
->PeiMemoryInstalled
= TRUE
;
134 PrivateData
->StackBase
= MemoryBegin
;
136 PeiStackSize
= RShiftU64 (MemoryLength
, 1);
137 if (PEI_STACK_SIZE
> PeiStackSize
) {
138 PrivateData
->StackSize
= PeiStackSize
;
140 PrivateData
->StackSize
= PEI_STACK_SIZE
;
143 OldHandOffHob
= PrivateData
->HobList
.HandoffInformationTable
;
145 PrivateData
->HobList
.Raw
= (VOID
*)((UINTN
)(MemoryBegin
+ PrivateData
->StackSize
));
146 NewHandOffHob
= PrivateData
->HobList
.HandoffInformationTable
;
147 PhysicalAddressOfOldHob
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) OldHandOffHob
;
149 EfiFreeMemorySize
= OldHandOffHob
->EfiFreeMemoryBottom
- PhysicalAddressOfOldHob
;
151 DEBUG ((EFI_D_INFO
, "HOBLIST address before memory init = 0x%08x\n", OldHandOffHob
));
152 DEBUG ((EFI_D_INFO
, "HOBLIST address after memory init = 0x%08x\n", NewHandOffHob
));
157 (UINTN
)EfiFreeMemorySize
160 NewHandOffHob
->EfiMemoryTop
= MemoryBegin
+ MemoryLength
;
161 NewHandOffHob
->EfiFreeMemoryTop
= NewHandOffHob
->EfiMemoryTop
;
162 NewHandOffHob
->EfiMemoryBottom
= MemoryBegin
;
164 NewHandOffHob
->EfiFreeMemoryBottom
= (UINTN
)NewHandOffHob
+ EfiFreeMemorySize
;
166 NewHandOffHob
->EfiEndOfHobList
= (UINTN
)NewHandOffHob
+
167 (OldHandOffHob
->EfiEndOfHobList
-
168 PhysicalAddressOfOldHob
);
170 ConvertPpiPointers (PeiServices
, OldHandOffHob
, NewHandOffHob
);
172 BuildStackHob (PrivateData
->StackBase
, PrivateData
->StackSize
);
181 IN CONST EFI_PEI_SERVICES
**PeiServices
,
182 IN EFI_MEMORY_TYPE MemoryType
,
184 OUT EFI_PHYSICAL_ADDRESS
*Memory
190 Memory allocation service on permanent memory,
191 not usable prior to the memory installation.
195 PeiServices - The PEI core services table.
196 MemoryType - Type of memory to allocate.
197 Pages - Number of pages to allocate.
198 Memory - Pointer of memory allocated.
202 Status - EFI_SUCCESS The allocation was successful
203 EFI_INVALID_PARAMETER Only AllocateAnyAddress is supported.
204 EFI_NOT_AVAILABLE_YET Called with permanent memory not available
205 EFI_OUT_OF_RESOURCES There is not enough HOB heap to satisfy the requirement
206 to allocate the number of pages.
210 PEI_CORE_INSTANCE
*PrivateData
;
211 EFI_PEI_HOB_POINTERS Hob
;
212 EFI_PHYSICAL_ADDRESS Offset
;
214 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
217 // Check if Hob already available
219 if (!PrivateData
->PeiMemoryInstalled
) {
220 return EFI_NOT_AVAILABLE_YET
;
223 Hob
.Raw
= PrivateData
->HobList
.Raw
;
226 // Check to see if on 4k boundary
228 Offset
= Hob
.HandoffInformationTable
->EfiFreeMemoryTop
& 0xFFF;
231 // If not aligned, make the allocation aligned.
234 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
-= Offset
;
238 // Verify that there is sufficient memory to satisfy the allocation
240 if (Hob
.HandoffInformationTable
->EfiFreeMemoryTop
- ((Pages
* EFI_PAGE_SIZE
) + sizeof (EFI_HOB_MEMORY_ALLOCATION
)) <
241 Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
) {
242 DEBUG ((EFI_D_ERROR
, "AllocatePages failed: No 0x%x Pages is available.\n", Pages
));
243 DEBUG ((EFI_D_ERROR
, "There is only left 0x%x pages memory resource to be allocated.\n", \
244 EFI_SIZE_TO_PAGES ((UINTN
) (Hob
.HandoffInformationTable
->EfiFreeMemoryTop
- Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
))));
245 return EFI_OUT_OF_RESOURCES
;
248 // Update the PHIT to reflect the memory usage
250 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
-= Pages
* EFI_PAGE_SIZE
;
253 // Update the value for the caller
255 *Memory
= Hob
.HandoffInformationTable
->EfiFreeMemoryTop
;
258 // Create a memory allocation HOB.
260 BuildMemoryAllocationHob (
261 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
,
262 Pages
* EFI_PAGE_SIZE
+ Offset
,
274 IN CONST EFI_PEI_SERVICES
**PeiServices
,
282 Memory allocation service on the CAR.
286 PeiServices - The PEI core services table.
288 Size - Amount of memory required
290 Buffer - Address of pointer to the buffer
294 Status - EFI_SUCCESS The allocation was successful
295 EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement
296 to allocate the requested size.
301 EFI_HOB_MEMORY_POOL
*Hob
;
304 // If some "post-memory" PEIM wishes to allocate larger pool,
305 // it should use AllocatePages service instead.
307 ASSERT (Size
< 0x10000 - sizeof (EFI_HOB_MEMORY_POOL
));
308 Status
= PeiServicesCreateHob (
309 EFI_HOB_TYPE_MEMORY_POOL
,
310 (UINT16
)(sizeof (EFI_HOB_MEMORY_POOL
) + Size
),