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.
21 @param PrivateData Points to PeiCore's private instance data.
22 @param SecCoreData Points to a data structure containing information about the PEI core's operating
23 environment, such as the size and location of temporary RAM, the stack location and
25 @param OldCoreData Pointer to the PEI Core data.
26 NULL if being run in non-permament memory mode.
30 InitializeMemoryServices (
31 IN PEI_CORE_INSTANCE
*PrivateData
,
32 IN CONST EFI_SEC_PEI_HAND_OFF
*SecCoreData
,
33 IN PEI_CORE_INSTANCE
*OldCoreData
37 PrivateData
->SwitchStackSignal
= FALSE
;
40 // First entering PeiCore, following code will initialized some field
41 // in PeiCore's private data according to hand off data from sec core.
43 if (OldCoreData
== NULL
) {
45 PrivateData
->PeiMemoryInstalled
= FALSE
;
46 PrivateData
->HobList
.Raw
= SecCoreData
->PeiTemporaryRamBase
;
48 PeiCoreBuildHobHandoffInfoTable (
49 BOOT_WITH_FULL_CONFIGURATION
,
50 (EFI_PHYSICAL_ADDRESS
) (UINTN
) SecCoreData
->PeiTemporaryRamBase
,
51 (UINTN
) SecCoreData
->PeiTemporaryRamSize
55 // Set PS to point to ServiceTableShadow in Cache
57 PrivateData
->PS
= &(PrivateData
->ServiceTableShadow
);
65 This function registers the found memory configuration with the PEI Foundation.
67 The usage model is that the PEIM that discovers the permanent memory shall invoke this service.
68 This routine will hold discoveried memory information into PeiCore's private data,
69 and set SwitchStackSignal flag. After PEIM who discovery memory is dispatched,
70 PeiDispatcher will migrate temporary memory to permenement memory.
72 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
73 @param MemoryBegin Start of memory address.
74 @param MemoryLength Length of memory.
76 @return EFI_SUCCESS Always success.
82 IN CONST EFI_PEI_SERVICES
**PeiServices
,
83 IN EFI_PHYSICAL_ADDRESS MemoryBegin
,
84 IN UINT64 MemoryLength
87 PEI_CORE_INSTANCE
*PrivateData
;
89 DEBUG ((EFI_D_INFO
, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin
, MemoryLength
));
90 PrivateData
= PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices
);
93 // PEI_SERVICE.InstallPeiMemory should only be called one time during whole PEI phase.
94 // If it is invoked more than one time, ASSERT information is given for developer debugging in debug tip and
95 // simply return EFI_SUCESS in release tip to ignore it.
97 if (PrivateData
->PeiMemoryInstalled
) {
98 DEBUG ((EFI_D_ERROR
, "ERROR: PeiInstallPeiMemory is called more than once!\n"));
99 ASSERT (PrivateData
->PeiMemoryInstalled
);
103 PrivateData
->PhysicalMemoryBegin
= MemoryBegin
;
104 PrivateData
->PhysicalMemoryLength
= MemoryLength
;
105 PrivateData
->FreePhysicalMemoryTop
= MemoryBegin
+ MemoryLength
;
107 PrivateData
->SwitchStackSignal
= TRUE
;
114 Memory allocation service on permanent memory,
115 not usable prior to the memory installation.
118 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
119 @param MemoryType Type of memory to allocate.
120 @param Pages Number of pages to allocate.
121 @param Memory Pointer of memory allocated.
123 @retval EFI_SUCCESS The allocation was successful
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
*FreeMemoryTop
;
141 EFI_PHYSICAL_ADDRESS
*FreeMemoryBottom
;
142 UINTN RemainingPages
;
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 temporary memory has *not* been moved to temporary memory,
153 // the AllocatePage will depend on 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, If not aligned, make the allocation aligned.
169 *(FreeMemoryTop
) -= *(FreeMemoryTop
) & 0xFFF;
172 // Verify that there is sufficient memory to satisfy the allocation
174 RemainingPages
= EFI_SIZE_TO_PAGES ((UINTN
) (*FreeMemoryTop
- *FreeMemoryBottom
));
176 // For page allocation, the overhead sizeof (EFI_HOB_MEMORY_ALLOCATION) needs one extra page.
177 // So the number of remaining pages needs to be greater than that of the request pages.
179 if (RemainingPages
<= Pages
) {
180 DEBUG ((EFI_D_ERROR
, "AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64
) Pages
));
181 DEBUG ((EFI_D_ERROR
, "There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64
) RemainingPages
));
182 return EFI_OUT_OF_RESOURCES
;
185 // Update the PHIT to reflect the memory usage
187 *(FreeMemoryTop
) -= Pages
* EFI_PAGE_SIZE
;
190 // Update the value for the caller
192 *Memory
= *(FreeMemoryTop
);
195 // Create a memory allocation HOB.
197 BuildMemoryAllocationHob (
199 Pages
* EFI_PAGE_SIZE
,
209 Pool allocation service. Before permenent memory is discoveried, the pool will
210 be allocated the heap in the temporary memory. Genenrally, the size of heap in temporary
211 memory does not exceed to 64K, so the biggest pool size could be allocated is
214 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
215 @param Size Amount of memory required
216 @param Buffer Address of pointer to the buffer
218 @retval EFI_SUCCESS The allocation was successful
219 @retval EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement
220 to allocate the requested size.
226 IN CONST EFI_PEI_SERVICES
**PeiServices
,
232 EFI_HOB_MEMORY_POOL
*Hob
;
235 // If some "post-memory" PEIM wishes to allocate larger pool,
236 // it should use AllocatePages service instead.
240 // Generally, the size of heap in temporary memory does not exceed to 64K,
241 // so the maxmium size of pool is 0x10000 - sizeof (EFI_HOB_MEMORY_POOL)
243 ASSERT (Size
< 0x10000 - sizeof (EFI_HOB_MEMORY_POOL
));
244 Status
= PeiServicesCreateHob (
245 EFI_HOB_TYPE_MEMORY_POOL
,
246 (UINT16
)(sizeof (EFI_HOB_MEMORY_POOL
) + Size
),