2 EFI PEI Core memory services
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Initialize the memory services.
22 @param PrivateData Add parameter description
23 @param SecCoreData Points to a data structure containing information about the PEI core's operating
24 environment, such as the size and location of temporary RAM, the stack location and
26 @param OldCoreData Pointer to the PEI Core data.
27 NULL if being run in non-permament memory mode.
31 InitializeMemoryServices (
32 IN PEI_CORE_INSTANCE
*PrivateData
,
33 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
,
34 IN PEI_CORE_INSTANCE
*OldCoreData
38 PrivateData
->SwitchStackSignal
= FALSE
;
41 // First entering PeiCore, following code will initialized some field
42 // in PeiCore's private data according to hand off data from sec core.
44 if (OldCoreData
== NULL
) {
46 PrivateData
->PeiMemoryInstalled
= FALSE
;
48 PrivateData
->BottomOfCarHeap
= SecCoreData
->PeiTemporaryRamBase
;
49 PrivateData
->TopOfCarHeap
= (VOID
*)((UINTN
)(PrivateData
->BottomOfCarHeap
) + SecCoreData
->PeiTemporaryRamSize
);
50 PrivateData
->SizeOfTemporaryMemory
= SecCoreData
->TemporaryRamSize
;
51 PrivateData
->StackSize
= (UINT64
) SecCoreData
->StackSize
;
54 PrivateData
->SizeOfCacheAsRam
= SecCoreData
->PeiTemporaryRamSize
+ SecCoreData
->StackSize
;
55 PrivateData
->MaxTopOfCarHeap
= (VOID
*) ((UINTN
) PrivateData
->BottomOfCarHeap
+ (UINTN
) PrivateData
->SizeOfCacheAsRam
);
56 PrivateData
->StackBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) SecCoreData
->StackBase
;
57 PrivateData
->StackSize
= (UINT64
) SecCoreData
->StackSize
;
60 PrivateData
->HobList
.Raw
= PrivateData
->BottomOfCarHeap
;
62 PeiCoreBuildHobHandoffInfoTable (
63 BOOT_WITH_FULL_CONFIGURATION
,
64 (EFI_PHYSICAL_ADDRESS
) (UINTN
) PrivateData
->BottomOfCarHeap
,
65 (UINTN
) SecCoreData
->PeiTemporaryRamSize
69 // Set PS to point to ServiceTableShadow in Cache
71 PrivateData
->PS
= &(PrivateData
->ServiceTableShadow
);
79 Install the permanent memory is now available.
80 Creates HOB (PHIT and Stack).
82 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
83 @param MemoryBegin Start of memory address.
84 @param MemoryLength Length of memory.
86 @return EFI_SUCCESS Always success.
92 IN CONST EFI_PEI_SERVICES
**PeiServices
,
93 IN EFI_PHYSICAL_ADDRESS MemoryBegin
,
94 IN UINT64 MemoryLength
97 PEI_CORE_INSTANCE
*PrivateData
;
99 DEBUG ((EFI_D_INFO
, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin
, MemoryLength
));
100 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
102 PrivateData
->PhysicalMemoryBegin
= MemoryBegin
;
103 PrivateData
->PhysicalMemoryLength
= MemoryLength
;
104 PrivateData
->FreePhysicalMemoryTop
= MemoryBegin
+ MemoryLength
;
106 PrivateData
->SwitchStackSignal
= TRUE
;
113 Memory allocation service on permanent memory,
114 not usable prior to the memory installation.
117 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
118 @param MemoryType Type of memory to allocate.
119 @param Pages Number of pages to allocate.
120 @param Memory Pointer of memory allocated.
122 @retval EFI_SUCCESS The allocation was successful
123 @retval EFI_INVALID_PARAMETER Only AllocateAnyAddress is supported.
124 @retval EFI_NOT_AVAILABLE_YET Called with permanent memory not available
125 @retval EFI_OUT_OF_RESOURCES There is not enough HOB heap to satisfy the requirement
126 to allocate the number of pages.
132 IN CONST EFI_PEI_SERVICES
**PeiServices
,
133 IN EFI_MEMORY_TYPE MemoryType
,
135 OUT EFI_PHYSICAL_ADDRESS
*Memory
138 PEI_CORE_INSTANCE
*PrivateData
;
139 EFI_PEI_HOB_POINTERS Hob
;
140 EFI_PHYSICAL_ADDRESS Offset
;
141 EFI_PHYSICAL_ADDRESS
*FreeMemoryTop
;
142 EFI_PHYSICAL_ADDRESS
*FreeMemoryBottom
;
144 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
145 Hob
.Raw
= PrivateData
->HobList
.Raw
;
148 // Check if Hob already available
150 if (!PrivateData
->PeiMemoryInstalled
) {
152 // When PeiInstallMemory is called but CAR has *not* been moved to temporary memory,
153 // the AllocatePage will dependent the field of PEI_CORE_INSTANCE structure.
155 if (!PrivateData
->SwitchStackSignal
) {
156 return EFI_NOT_AVAILABLE_YET
;
158 FreeMemoryTop
= &(PrivateData
->FreePhysicalMemoryTop
);
159 FreeMemoryBottom
= &(PrivateData
->PhysicalMemoryBegin
);
162 FreeMemoryTop
= &(Hob
.HandoffInformationTable
->EfiFreeMemoryTop
);
163 FreeMemoryBottom
= &(Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
);
167 // Check to see if on 4k boundary
169 Offset
= *(FreeMemoryTop
) & 0xFFF;
172 // If not aligned, make the allocation aligned.
175 *(FreeMemoryTop
) -= Offset
;
179 // Verify that there is sufficient memory to satisfy the allocation
181 if (*(FreeMemoryTop
) - ((Pages
* EFI_PAGE_SIZE
) + sizeof (EFI_HOB_MEMORY_ALLOCATION
)) <
182 *(FreeMemoryBottom
)) {
183 DEBUG ((EFI_D_ERROR
, "AllocatePages failed: No 0x%x Pages is available.\n", Pages
));
184 DEBUG ((EFI_D_ERROR
, "There is only left 0x%x pages memory resource to be allocated.\n", \
185 EFI_SIZE_TO_PAGES ((UINTN
) (*(FreeMemoryTop
) - *(FreeMemoryBottom
)))));
186 return EFI_OUT_OF_RESOURCES
;
189 // Update the PHIT to reflect the memory usage
191 *(FreeMemoryTop
) -= Pages
* EFI_PAGE_SIZE
;
194 // Update the value for the caller
196 *Memory
= *(FreeMemoryTop
);
199 // Create a memory allocation HOB.
201 BuildMemoryAllocationHob (
203 Pages
* EFI_PAGE_SIZE
,
213 Memory allocation service on the CAR.
216 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
217 @param Size Amount of memory required
218 @param Buffer Address of pointer to the buffer
220 @retval EFI_SUCCESS The allocation was successful
221 @retval EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement
222 to allocate the requested size.
228 IN CONST EFI_PEI_SERVICES
**PeiServices
,
234 EFI_HOB_MEMORY_POOL
*Hob
;
237 // If some "post-memory" PEIM wishes to allocate larger pool,
238 // it should use AllocatePages service instead.
240 ASSERT (Size
< 0x10000 - sizeof (EFI_HOB_MEMORY_POOL
));
241 Status
= PeiServicesCreateHob (
242 EFI_HOB_TYPE_MEMORY_POOL
,
243 (UINT16
)(sizeof (EFI_HOB_MEMORY_POOL
) + Size
),