]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/Memory/MemoryServices.c
Re-integrate patch in r5626~r5628:Enhance PeiCore dispatcher and fix a bug in the...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Memory / MemoryServices.c
1 /** @file
2 EFI PEI Core memory services
3
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
9
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.
12
13 **/
14
15 #include <PeiMain.h>
16
17 /**
18
19 Initialize the memory services.
20
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
24 the BFV location.
25 @param OldCoreData Pointer to the PEI Core data.
26 NULL if being run in non-permament memory mode.
27
28 **/
29 VOID
30 InitializeMemoryServices (
31 IN PEI_CORE_INSTANCE *PrivateData,
32 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,
33 IN PEI_CORE_INSTANCE *OldCoreData
34 )
35 {
36
37 PrivateData->SwitchStackSignal = FALSE;
38
39 //
40 // First entering PeiCore, following code will initialized some field
41 // in PeiCore's private data according to hand off data from sec core.
42 //
43 if (OldCoreData == NULL) {
44
45 PrivateData->PeiMemoryInstalled = FALSE;
46
47 PrivateData->BottomOfCarHeap = SecCoreData->PeiTemporaryRamBase;
48 PrivateData->TopOfCarHeap = (VOID *)((UINTN)(PrivateData->BottomOfCarHeap) + SecCoreData->PeiTemporaryRamSize);
49 PrivateData->SizeOfTemporaryMemory = SecCoreData->TemporaryRamSize;
50 PrivateData->StackSize = (UINT64) SecCoreData->StackSize;
51
52 DEBUG_CODE_BEGIN ();
53 PrivateData->SizeOfCacheAsRam = SecCoreData->PeiTemporaryRamSize + SecCoreData->StackSize;
54 PrivateData->MaxTopOfCarHeap = (VOID *) ((UINTN) PrivateData->BottomOfCarHeap + (UINTN) PrivateData->SizeOfCacheAsRam);
55 PrivateData->StackBase = (EFI_PHYSICAL_ADDRESS) (UINTN) SecCoreData->StackBase;
56 PrivateData->StackSize = (UINT64) SecCoreData->StackSize;
57 DEBUG_CODE_END ();
58
59 PrivateData->HobList.Raw = PrivateData->BottomOfCarHeap;
60
61 PeiCoreBuildHobHandoffInfoTable (
62 BOOT_WITH_FULL_CONFIGURATION,
63 (EFI_PHYSICAL_ADDRESS) (UINTN) PrivateData->BottomOfCarHeap,
64 (UINTN) SecCoreData->PeiTemporaryRamSize
65 );
66
67 //
68 // Set PS to point to ServiceTableShadow in Cache
69 //
70 PrivateData->PS = &(PrivateData->ServiceTableShadow);
71 }
72
73 return;
74 }
75
76 /**
77
78 This function registers the found memory configuration with the PEI Foundation.
79
80 The usage model is that the PEIM that discovers the permanent memory shall invoke this service.
81 This routine will hold discoveried memory information into PeiCore's private data,
82 and set SwitchStackSignal flag. After PEIM who discovery memory is dispatched,
83 PeiDispatcher will migrate temporary memory to permenement memory.
84
85 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
86 @param MemoryBegin Start of memory address.
87 @param MemoryLength Length of memory.
88
89 @return EFI_SUCCESS Always success.
90
91 **/
92 EFI_STATUS
93 EFIAPI
94 PeiInstallPeiMemory (
95 IN CONST EFI_PEI_SERVICES **PeiServices,
96 IN EFI_PHYSICAL_ADDRESS MemoryBegin,
97 IN UINT64 MemoryLength
98 )
99 {
100 PEI_CORE_INSTANCE *PrivateData;
101
102 DEBUG ((EFI_D_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));
103 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
104
105 PrivateData->PhysicalMemoryBegin = MemoryBegin;
106 PrivateData->PhysicalMemoryLength = MemoryLength;
107 PrivateData->FreePhysicalMemoryTop = MemoryBegin + MemoryLength;
108
109 PrivateData->SwitchStackSignal = TRUE;
110
111 return EFI_SUCCESS;
112 }
113
114 /**
115
116 Memory allocation service on permanent memory,
117 not usable prior to the memory installation.
118
119
120 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
121 @param MemoryType Type of memory to allocate.
122 @param Pages Number of pages to allocate.
123 @param Memory Pointer of memory allocated.
124
125 @retval EFI_SUCCESS The allocation was successful
126 @retval EFI_NOT_AVAILABLE_YET Called with permanent memory not available
127 @retval EFI_OUT_OF_RESOURCES There is not enough HOB heap to satisfy the requirement
128 to allocate the number of pages.
129
130 **/
131 EFI_STATUS
132 EFIAPI
133 PeiAllocatePages (
134 IN CONST EFI_PEI_SERVICES **PeiServices,
135 IN EFI_MEMORY_TYPE MemoryType,
136 IN UINTN Pages,
137 OUT EFI_PHYSICAL_ADDRESS *Memory
138 )
139 {
140 PEI_CORE_INSTANCE *PrivateData;
141 EFI_PEI_HOB_POINTERS Hob;
142 EFI_PHYSICAL_ADDRESS *FreeMemoryTop;
143 EFI_PHYSICAL_ADDRESS *FreeMemoryBottom;
144
145 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
146 Hob.Raw = PrivateData->HobList.Raw;
147
148 //
149 // Check if Hob already available
150 //
151 if (!PrivateData->PeiMemoryInstalled) {
152 //
153 // When PeiInstallMemory is called but CAR has *not* been moved to temporary memory,
154 // the AllocatePage will dependent on the field of PEI_CORE_INSTANCE structure.
155 //
156 if (!PrivateData->SwitchStackSignal) {
157 return EFI_NOT_AVAILABLE_YET;
158 } else {
159 FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);
160 FreeMemoryBottom = &(PrivateData->PhysicalMemoryBegin);
161 }
162 } else {
163 FreeMemoryTop = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);
164 FreeMemoryBottom = &(Hob.HandoffInformationTable->EfiFreeMemoryBottom);
165 }
166
167 //
168 // Check to see if on 4k boundary, If not aligned, make the allocation aligned.
169 //
170 *(FreeMemoryTop) -= *(FreeMemoryTop) & 0xFFF;
171
172 //
173 // Verify that there is sufficient memory to satisfy the allocation
174 //
175 if (*(FreeMemoryTop) - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) <
176 *(FreeMemoryBottom)) {
177 DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%x Pages is available.\n", Pages));
178 DEBUG ((EFI_D_ERROR, "There is only left 0x%x pages memory resource to be allocated.\n", \
179 EFI_SIZE_TO_PAGES ((UINTN) (*(FreeMemoryTop) - *(FreeMemoryBottom)))));
180 return EFI_OUT_OF_RESOURCES;
181 } else {
182 //
183 // Update the PHIT to reflect the memory usage
184 //
185 *(FreeMemoryTop) -= Pages * EFI_PAGE_SIZE;
186
187 //
188 // Update the value for the caller
189 //
190 *Memory = *(FreeMemoryTop);
191
192 //
193 // Create a memory allocation HOB.
194 //
195 BuildMemoryAllocationHob (
196 *(FreeMemoryTop),
197 Pages * EFI_PAGE_SIZE,
198 MemoryType
199 );
200
201 return EFI_SUCCESS;
202 }
203 }
204
205 /**
206
207 Pool allocation service. Before permenent memory is discoveried, the pool will
208 be allocated the heap in the CAR. Genenrally, the size of heap in temporary
209 memory does not exceed to 64K, so the biggest pool size could be allocated is
210 64K.
211
212 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
213 @param Size Amount of memory required
214 @param Buffer Address of pointer to the buffer
215
216 @retval EFI_SUCCESS The allocation was successful
217 @retval EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement
218 to allocate the requested size.
219
220 **/
221 EFI_STATUS
222 EFIAPI
223 PeiAllocatePool (
224 IN CONST EFI_PEI_SERVICES **PeiServices,
225 IN UINTN Size,
226 OUT VOID **Buffer
227 )
228 {
229 EFI_STATUS Status;
230 EFI_HOB_MEMORY_POOL *Hob;
231
232 //
233 // If some "post-memory" PEIM wishes to allocate larger pool,
234 // it should use AllocatePages service instead.
235 //
236
237 //
238 // Generally, the size of heap in temporary memory does not exceed to 64K,
239 // so the maxmium size of pool is 0x10000 - sizeof (EFI_HOB_MEMORY_POOL)
240 //
241 ASSERT (Size < 0x10000 - sizeof (EFI_HOB_MEMORY_POOL));
242 Status = PeiServicesCreateHob (
243 EFI_HOB_TYPE_MEMORY_POOL,
244 (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),
245 (VOID **)&Hob
246 );
247 *Buffer = Hob+1;
248
249 return Status;
250 }