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.
53 PrivateData
->SwitchStackSignal
= FALSE
;
55 if (OldCoreData
== NULL
) {
57 PrivateData
->PeiMemoryInstalled
= FALSE
;
59 PrivateData
->BottomOfCarHeap
= SecCoreData
->PeiTemporaryRamBase
;
60 PrivateData
->TopOfCarHeap
= (VOID
*)((UINTN
)(PrivateData
->BottomOfCarHeap
) + SecCoreData
->PeiTemporaryRamSize
);
61 PrivateData
->SizeOfTemporaryMemory
= SecCoreData
->TemporaryRamSize
;
62 PrivateData
->StackSize
= (UINT64
) SecCoreData
->StackSize
;
65 PrivateData
->SizeOfCacheAsRam
= SecCoreData
->PeiTemporaryRamSize
+ SecCoreData
->StackSize
;
66 PrivateData
->MaxTopOfCarHeap
= (VOID
*) ((UINTN
) PrivateData
->BottomOfCarHeap
+ (UINTN
) PrivateData
->SizeOfCacheAsRam
);
67 PrivateData
->StackBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) SecCoreData
->StackBase
;
68 PrivateData
->StackSize
= (UINT64
) SecCoreData
->StackSize
;
71 PrivateData
->HobList
.Raw
= PrivateData
->BottomOfCarHeap
;
73 PeiCoreBuildHobHandoffInfoTable (
74 BOOT_WITH_FULL_CONFIGURATION
,
75 (EFI_PHYSICAL_ADDRESS
) (UINTN
) PrivateData
->BottomOfCarHeap
,
76 (UINTN
) SecCoreData
->PeiTemporaryRamSize
80 // Set PS to point to ServiceTableShadow in Cache
82 PrivateData
->PS
= &(PrivateData
->ServiceTableShadow
);
91 IN CONST EFI_PEI_SERVICES
**PeiServices
,
92 IN EFI_PHYSICAL_ADDRESS MemoryBegin
,
93 IN UINT64 MemoryLength
99 Install the permanent memory is now available.
100 Creates HOB (PHIT and Stack).
104 PeiServices - The PEI core services table.
105 MemoryBegin - Start of memory address.
106 MemoryLength - Length of memory.
114 PEI_CORE_INSTANCE
*PrivateData
;
116 DEBUG ((EFI_D_INFO
, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin
, MemoryLength
));
117 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
119 PrivateData
->PhysicalMemoryBegin
= MemoryBegin
;
120 PrivateData
->PhysicalMemoryLength
= MemoryLength
;
121 PrivateData
->FreePhysicalMemoryTop
= MemoryBegin
+ MemoryLength
;
123 PrivateData
->SwitchStackSignal
= TRUE
;
131 IN CONST EFI_PEI_SERVICES
**PeiServices
,
132 IN EFI_MEMORY_TYPE MemoryType
,
134 OUT EFI_PHYSICAL_ADDRESS
*Memory
140 Memory allocation service on permanent memory,
141 not usable prior to the memory installation.
145 PeiServices - The PEI core services table.
146 MemoryType - Type of memory to allocate.
147 Pages - Number of pages to allocate.
148 Memory - Pointer of memory allocated.
152 Status - EFI_SUCCESS The allocation was successful
153 EFI_INVALID_PARAMETER Only AllocateAnyAddress is supported.
154 EFI_NOT_AVAILABLE_YET Called with permanent memory not available
155 EFI_OUT_OF_RESOURCES There is not enough HOB heap to satisfy the requirement
156 to allocate the number of pages.
160 PEI_CORE_INSTANCE
*PrivateData
;
161 EFI_PEI_HOB_POINTERS Hob
;
162 EFI_PHYSICAL_ADDRESS Offset
;
163 EFI_PHYSICAL_ADDRESS
*FreeMemoryTop
;
164 EFI_PHYSICAL_ADDRESS
*FreeMemoryBottom
;
166 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
167 Hob
.Raw
= PrivateData
->HobList
.Raw
;
170 // Check if Hob already available
172 if (!PrivateData
->PeiMemoryInstalled
) {
174 // When PeiInstallMemory is called but CAR has *not* been moved to temporary memory,
175 // the AllocatePage will dependent the field of PEI_CORE_INSTANCE structure.
177 if (!PrivateData
->SwitchStackSignal
) {
178 return EFI_NOT_AVAILABLE_YET
;
180 FreeMemoryTop
= &(PrivateData
->FreePhysicalMemoryTop
);
181 FreeMemoryBottom
= &(PrivateData
->PhysicalMemoryBegin
);
184 FreeMemoryTop
= &(Hob
.HandoffInformationTable
->EfiFreeMemoryTop
);
185 FreeMemoryBottom
= &(Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
);
191 // Check to see if on 4k boundary
193 Offset
= *(FreeMemoryTop
) & 0xFFF;
196 // If not aligned, make the allocation aligned.
199 *(FreeMemoryTop
) -= Offset
;
203 // Verify that there is sufficient memory to satisfy the allocation
205 if (*(FreeMemoryTop
) - ((Pages
* EFI_PAGE_SIZE
) + sizeof (EFI_HOB_MEMORY_ALLOCATION
)) <
206 *(FreeMemoryBottom
)) {
207 DEBUG ((EFI_D_ERROR
, "AllocatePages failed: No 0x%x Pages is available.\n", Pages
));
208 DEBUG ((EFI_D_ERROR
, "There is only left 0x%x pages memory resource to be allocated.\n", \
209 EFI_SIZE_TO_PAGES ((UINTN
) (*(FreeMemoryTop
) - *(FreeMemoryBottom
)))));
210 return EFI_OUT_OF_RESOURCES
;
213 // Update the PHIT to reflect the memory usage
215 *(FreeMemoryTop
) -= Pages
* EFI_PAGE_SIZE
;
218 // Update the value for the caller
220 *Memory
= *(FreeMemoryTop
);
223 // Create a memory allocation HOB.
225 BuildMemoryAllocationHob (
227 Pages
* EFI_PAGE_SIZE
,
239 IN CONST EFI_PEI_SERVICES
**PeiServices
,
247 Memory allocation service on the CAR.
251 PeiServices - The PEI core services table.
253 Size - Amount of memory required
255 Buffer - Address of pointer to the buffer
259 Status - EFI_SUCCESS The allocation was successful
260 EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement
261 to allocate the requested size.
266 EFI_HOB_MEMORY_POOL
*Hob
;
269 // If some "post-memory" PEIM wishes to allocate larger pool,
270 // it should use AllocatePages service instead.
272 ASSERT (Size
< 0x10000 - sizeof (EFI_HOB_MEMORY_POOL
));
273 Status
= PeiServicesCreateHob (
274 EFI_HOB_TYPE_MEMORY_POOL
,
275 (UINT16
)(sizeof (EFI_HOB_MEMORY_POOL
) + Size
),