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 PEI_CORE_INSTANCE
*PrivateData
,
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 PrivateData
->SwitchStackSignal
= FALSE
;
54 if (OldCoreData
== NULL
) {
56 PrivateData
->PeiMemoryInstalled
= FALSE
;
58 PrivateData
->BottomOfCarHeap
= SecCoreData
->PeiTemporaryRamBase
;
59 PrivateData
->TopOfCarHeap
= (VOID
*)((UINTN
)(PrivateData
->BottomOfCarHeap
) + SecCoreData
->PeiTemporaryRamSize
);
62 PrivateData
->SizeOfCacheAsRam
= SecCoreData
->PeiTemporaryRamSize
+ SecCoreData
->StackSize
;
63 PrivateData
->MaxTopOfCarHeap
= (VOID
*) ((UINTN
) PrivateData
->BottomOfCarHeap
+ (UINTN
) PrivateData
->SizeOfCacheAsRam
);
64 PrivateData
->StackBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) SecCoreData
->StackBase
;
65 PrivateData
->StackSize
= (UINT64
) SecCoreData
->StackSize
;
68 PrivateData
->HobList
.Raw
= PrivateData
->BottomOfCarHeap
;
70 PeiCoreBuildHobHandoffInfoTable (
71 BOOT_WITH_FULL_CONFIGURATION
,
72 (EFI_PHYSICAL_ADDRESS
) (UINTN
) PrivateData
->BottomOfCarHeap
,
73 (UINTN
) SecCoreData
->PeiTemporaryRamSize
77 // Set PS to point to ServiceTableShadow in Cache
79 PrivateData
->PS
= &(PrivateData
->ServiceTableShadow
);
88 IN CONST EFI_PEI_SERVICES
**PeiServices
,
89 IN EFI_PHYSICAL_ADDRESS MemoryBegin
,
90 IN UINT64 MemoryLength
96 Install the permanent memory is now available.
97 Creates HOB (PHIT and Stack).
101 PeiServices - The PEI core services table.
102 MemoryBegin - Start of memory address.
103 MemoryLength - Length of memory.
111 PEI_CORE_INSTANCE
*PrivateData
;
112 EFI_HOB_HANDOFF_INFO_TABLE
*OldHandOffHob
;
113 EFI_HOB_HANDOFF_INFO_TABLE
*NewHandOffHob
;
115 UINT64 EfiFreeMemorySize
;
116 EFI_PHYSICAL_ADDRESS PhysicalAddressOfOldHob
;
118 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
120 PrivateData
->SwitchStackSignal
= TRUE
;
121 PrivateData
->PeiMemoryInstalled
= TRUE
;
123 PrivateData
->StackBase
= MemoryBegin
;
125 PeiStackSize
= RShiftU64 (MemoryLength
, 1);
126 if (PEI_STACK_SIZE
> PeiStackSize
) {
127 PrivateData
->StackSize
= PeiStackSize
;
129 PrivateData
->StackSize
= PEI_STACK_SIZE
;
132 OldHandOffHob
= PrivateData
->HobList
.HandoffInformationTable
;
134 PrivateData
->HobList
.Raw
= (VOID
*)((UINTN
)(MemoryBegin
+ PrivateData
->StackSize
));
135 NewHandOffHob
= PrivateData
->HobList
.HandoffInformationTable
;
136 PhysicalAddressOfOldHob
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) OldHandOffHob
;
138 EfiFreeMemorySize
= OldHandOffHob
->EfiFreeMemoryBottom
- PhysicalAddressOfOldHob
;
140 DEBUG ((EFI_D_INFO
, "HOBLIST address before memory init = 0x%08x\n", OldHandOffHob
));
141 DEBUG ((EFI_D_INFO
, "HOBLIST address after memory init = 0x%08x\n", NewHandOffHob
));
146 (UINTN
)EfiFreeMemorySize
149 NewHandOffHob
->EfiMemoryTop
= MemoryBegin
+ MemoryLength
;
150 NewHandOffHob
->EfiFreeMemoryTop
= NewHandOffHob
->EfiMemoryTop
;
151 NewHandOffHob
->EfiMemoryBottom
= MemoryBegin
;
153 NewHandOffHob
->EfiFreeMemoryBottom
= (UINTN
)NewHandOffHob
+ EfiFreeMemorySize
;
155 NewHandOffHob
->EfiEndOfHobList
= (UINTN
)NewHandOffHob
+
156 (OldHandOffHob
->EfiEndOfHobList
-
157 PhysicalAddressOfOldHob
);
159 ConvertPpiPointers (PeiServices
, OldHandOffHob
, NewHandOffHob
);
161 BuildStackHob (PrivateData
->StackBase
, PrivateData
->StackSize
);
170 IN CONST EFI_PEI_SERVICES
**PeiServices
,
171 IN EFI_MEMORY_TYPE MemoryType
,
173 OUT EFI_PHYSICAL_ADDRESS
*Memory
179 Memory allocation service on permanent memory,
180 not usable prior to the memory installation.
184 PeiServices - The PEI core services table.
185 MemoryType - Type of memory to allocate.
186 Pages - Number of pages to allocate.
187 Memory - Pointer of memory allocated.
191 Status - EFI_SUCCESS The allocation was successful
192 EFI_INVALID_PARAMETER Only AllocateAnyAddress is supported.
193 EFI_NOT_AVAILABLE_YET Called with permanent memory not available
194 EFI_OUT_OF_RESOURCES There is not enough HOB heap to satisfy the requirement
195 to allocate the number of pages.
199 PEI_CORE_INSTANCE
*PrivateData
;
200 EFI_PEI_HOB_POINTERS Hob
;
201 EFI_PHYSICAL_ADDRESS Offset
;
203 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
206 // Check if Hob already available
208 if (!PrivateData
->PeiMemoryInstalled
) {
209 return EFI_NOT_AVAILABLE_YET
;
212 Hob
.Raw
= PrivateData
->HobList
.Raw
;
215 // Check to see if on 4k boundary
217 Offset
= Hob
.HandoffInformationTable
->EfiFreeMemoryTop
& 0xFFF;
220 // If not aligned, make the allocation aligned.
223 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
-= Offset
;
227 // Verify that there is sufficient memory to satisfy the allocation
229 if (Hob
.HandoffInformationTable
->EfiFreeMemoryTop
- ((Pages
* EFI_PAGE_SIZE
) + sizeof (EFI_HOB_MEMORY_ALLOCATION
)) <
230 Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
) {
231 DEBUG ((EFI_D_ERROR
, "AllocatePages failed: No 0x%x Pages is available.\n", Pages
));
232 DEBUG ((EFI_D_ERROR
, "There is only left 0x%x pages memory resource to be allocated.\n", \
233 EFI_SIZE_TO_PAGES ((UINTN
) (Hob
.HandoffInformationTable
->EfiFreeMemoryTop
- Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
))));
234 return EFI_OUT_OF_RESOURCES
;
237 // Update the PHIT to reflect the memory usage
239 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
-= Pages
* EFI_PAGE_SIZE
;
242 // Update the value for the caller
244 *Memory
= Hob
.HandoffInformationTable
->EfiFreeMemoryTop
;
247 // Create a memory allocation HOB.
249 BuildMemoryAllocationHob (
250 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
,
251 Pages
* EFI_PAGE_SIZE
,
263 IN CONST EFI_PEI_SERVICES
**PeiServices
,
271 Memory allocation service on the CAR.
275 PeiServices - The PEI core services table.
277 Size - Amount of memory required
279 Buffer - Address of pointer to the buffer
283 Status - EFI_SUCCESS The allocation was successful
284 EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement
285 to allocate the requested size.
290 EFI_HOB_MEMORY_POOL
*Hob
;
293 // If some "post-memory" PEIM wishes to allocate larger pool,
294 // it should use AllocatePages service instead.
296 ASSERT (Size
< 0x10000 - sizeof (EFI_HOB_MEMORY_POOL
));
297 Status
= PeiServicesCreateHob (
298 EFI_HOB_TYPE_MEMORY_POOL
,
299 (UINT16
)(sizeof (EFI_HOB_MEMORY_POOL
) + Size
),