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 if (MemoryLength
> (MAX_ADDRESS
- MemoryBegin
+ 1))
119 return EFI_INVALID_PARAMETER
;
122 DEBUG ((EFI_D_INFO
, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin
, MemoryLength
));
124 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
126 PrivateData
->SwitchStackSignal
= TRUE
;
127 PrivateData
->PeiMemoryInstalled
= TRUE
;
130 // Ensure the stack base is in page alignment
132 PrivateData
->StackBase
= ((UINTN
)MemoryBegin
+ (EFI_PAGE_SIZE
- 1)) & EFI_PAGE_SIZE
;
134 PeiStackSize
= (RShiftU64 (MemoryLength
, 1) + (EFI_PAGE_SIZE
- 1)) & EFI_PAGE_SIZE
;
135 if (PEI_STACK_SIZE
> PeiStackSize
) {
136 PrivateData
->StackSize
= PeiStackSize
;
138 PrivateData
->StackSize
= PEI_STACK_SIZE
;
141 OldHandOffHob
= PrivateData
->HobList
.HandoffInformationTable
;
143 PrivateData
->HobList
.Raw
= (VOID
*)((UINTN
)(PrivateData
->StackBase
+ PrivateData
->StackSize
));
144 NewHandOffHob
= PrivateData
->HobList
.HandoffInformationTable
;
145 PhysicalAddressOfOldHob
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) OldHandOffHob
;
147 EfiFreeMemorySize
= OldHandOffHob
->EfiFreeMemoryBottom
- PhysicalAddressOfOldHob
;
149 DEBUG ((EFI_D_INFO
, "HOBLIST address before memory init = 0x%p\n", OldHandOffHob
));
150 DEBUG ((EFI_D_INFO
, "HOBLIST address after memory init = 0x%p\n", NewHandOffHob
));
155 (UINTN
)EfiFreeMemorySize
158 NewHandOffHob
->EfiMemoryTop
= MemoryBegin
+ MemoryLength
;
159 NewHandOffHob
->EfiFreeMemoryTop
= NewHandOffHob
->EfiMemoryTop
;
160 NewHandOffHob
->EfiMemoryBottom
= MemoryBegin
;
162 NewHandOffHob
->EfiFreeMemoryBottom
= (UINTN
)NewHandOffHob
+ EfiFreeMemorySize
;
164 NewHandOffHob
->EfiEndOfHobList
= (UINTN
)NewHandOffHob
+
165 (OldHandOffHob
->EfiEndOfHobList
-
166 PhysicalAddressOfOldHob
);
169 // For IPF in CAR mode the real memory access is uncached,in InstallPeiMemory()
170 // the 63-bit of address is set to 1.
172 SWITCH_TO_CACHE_MODE (PrivateData
);
174 ConvertPpiPointers (PeiServices
, OldHandOffHob
, NewHandOffHob
);
176 BuildStackHob (PrivateData
->StackBase
, PrivateData
->StackSize
);
185 IN CONST EFI_PEI_SERVICES
**PeiServices
,
186 IN EFI_MEMORY_TYPE MemoryType
,
188 OUT EFI_PHYSICAL_ADDRESS
*Memory
194 Memory allocation service on permanent memory,
195 not usable prior to the memory installation.
199 PeiServices - The PEI core services table.
200 MemoryType - Type of memory to allocate.
201 Pages - Number of pages to allocate.
202 Memory - Pointer of memory allocated.
206 Status - EFI_SUCCESS The allocation was successful
207 EFI_INVALID_PARAMETER Only AllocateAnyAddress is supported.
208 EFI_NOT_AVAILABLE_YET Called with permanent memory not available
209 EFI_OUT_OF_RESOURCES There is not enough HOB heap to satisfy the requirement
210 to allocate the number of pages.
214 PEI_CORE_INSTANCE
*PrivateData
;
215 EFI_PEI_HOB_POINTERS Hob
;
216 EFI_PHYSICAL_ADDRESS Offset
;
218 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
221 // Check if Hob already available
223 if (!PrivateData
->PeiMemoryInstalled
) {
224 return EFI_NOT_AVAILABLE_YET
;
227 Hob
.Raw
= PrivateData
->HobList
.Raw
;
230 // Check to see if on 4k boundary
232 Offset
= Hob
.HandoffInformationTable
->EfiFreeMemoryTop
& 0xFFF;
235 // If not aligned, make the allocation aligned.
238 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
-= Offset
;
241 ASSERT (Hob
.HandoffInformationTable
->EfiFreeMemoryTop
>= Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
);
243 // Verify that there is sufficient memory to satisfy the allocation
245 if (Hob
.HandoffInformationTable
->EfiFreeMemoryTop
- ((Pages
* EFI_PAGE_SIZE
) + sizeof (EFI_HOB_MEMORY_ALLOCATION
)) <
246 Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
) {
247 DEBUG ((EFI_D_ERROR
, "AllocatePages failed: No 0x%x Pages is available.\n", Pages
));
248 DEBUG ((EFI_D_ERROR
, "There is only left 0x%x pages memory resource to be allocated.\n", \
249 EFI_SIZE_TO_PAGES ((UINTN
) (Hob
.HandoffInformationTable
->EfiFreeMemoryTop
- Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
))));
250 return EFI_OUT_OF_RESOURCES
;
253 // Update the PHIT to reflect the memory usage
255 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
-= Pages
* EFI_PAGE_SIZE
;
258 // Update the value for the caller
260 *Memory
= Hob
.HandoffInformationTable
->EfiFreeMemoryTop
;
263 // Create a memory allocation HOB.
265 BuildMemoryAllocationHob (
266 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
,
267 Pages
* EFI_PAGE_SIZE
,
279 IN CONST EFI_PEI_SERVICES
**PeiServices
,
287 Memory allocation service on the CAR.
291 PeiServices - The PEI core services table.
293 Size - Amount of memory required
295 Buffer - Address of pointer to the buffer
299 Status - EFI_SUCCESS The allocation was successful
300 EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement
301 to allocate the requested size.
306 EFI_HOB_MEMORY_POOL
*Hob
;
309 // If some "post-memory" PEIM wishes to allocate larger pool,
310 // it should use AllocatePages service instead.
312 ASSERT (Size
< 0x10000 - sizeof (EFI_HOB_MEMORY_POOL
));
313 Status
= PeiServicesCreateHob (
314 EFI_HOB_TYPE_MEMORY_POOL
,
315 (UINT16
)(sizeof (EFI_HOB_MEMORY_POOL
) + Size
),