MdeModulePkg/PeiCore: honour minimal runtime allocation granularity
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Memory / MemoryServices.c
1 /** @file
2 EFI PEI Core memory services
3
4 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
5 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 PrivateData->HobList.Raw = SecCoreData->PeiTemporaryRamBase;
47
48 PeiCoreBuildHobHandoffInfoTable (
49 BOOT_WITH_FULL_CONFIGURATION,
50 (EFI_PHYSICAL_ADDRESS) (UINTN) SecCoreData->PeiTemporaryRamBase,
51 (UINTN) SecCoreData->PeiTemporaryRamSize
52 );
53
54 //
55 // Set Ps to point to ServiceTableShadow in Cache
56 //
57 PrivateData->Ps = &(PrivateData->ServiceTableShadow);
58 }
59
60 return;
61 }
62
63 /**
64
65 This function registers the found memory configuration with the PEI Foundation.
66
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.
71
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.
75
76 @return EFI_SUCCESS Always success.
77
78 **/
79 EFI_STATUS
80 EFIAPI
81 PeiInstallPeiMemory (
82 IN CONST EFI_PEI_SERVICES **PeiServices,
83 IN EFI_PHYSICAL_ADDRESS MemoryBegin,
84 IN UINT64 MemoryLength
85 )
86 {
87 PEI_CORE_INSTANCE *PrivateData;
88
89 DEBUG ((EFI_D_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));
90 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
91
92 //
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.
96 //
97 if (PrivateData->PeiMemoryInstalled) {
98 DEBUG ((EFI_D_ERROR, "ERROR: PeiInstallPeiMemory is called more than once!\n"));
99 ASSERT (FALSE);
100 return EFI_SUCCESS;
101 }
102
103 PrivateData->PhysicalMemoryBegin = MemoryBegin;
104 PrivateData->PhysicalMemoryLength = MemoryLength;
105 PrivateData->FreePhysicalMemoryTop = MemoryBegin + MemoryLength;
106
107 PrivateData->SwitchStackSignal = TRUE;
108
109 return EFI_SUCCESS;
110 }
111
112 /**
113 The purpose of the service is to publish an interface that allows
114 PEIMs to allocate memory ranges that are managed by the PEI Foundation.
115
116 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
117 @param MemoryType The type of memory to allocate.
118 @param Pages The number of contiguous 4 KB pages to allocate.
119 @param Memory Pointer to a physical address. On output, the address is set to the base
120 of the page range that was allocated.
121
122 @retval EFI_SUCCESS The memory range was successfully allocated.
123 @retval EFI_OUT_OF_RESOURCES The pages could not be allocated.
124 @retval EFI_INVALID_PARAMETER Type is not equal to EfiLoaderCode, EfiLoaderData, EfiRuntimeServicesCode,
125 EfiRuntimeServicesData, EfiBootServicesCode, EfiBootServicesData,
126 EfiACPIReclaimMemory, EfiReservedMemoryType, or EfiACPIMemoryNVS.
127
128 **/
129 EFI_STATUS
130 EFIAPI
131 PeiAllocatePages (
132 IN CONST EFI_PEI_SERVICES **PeiServices,
133 IN EFI_MEMORY_TYPE MemoryType,
134 IN UINTN Pages,
135 OUT EFI_PHYSICAL_ADDRESS *Memory
136 )
137 {
138 PEI_CORE_INSTANCE *PrivateData;
139 EFI_PEI_HOB_POINTERS Hob;
140 EFI_PHYSICAL_ADDRESS *FreeMemoryTop;
141 EFI_PHYSICAL_ADDRESS *FreeMemoryBottom;
142 UINTN RemainingPages;
143 UINTN Granularity;
144 UINTN Padding;
145
146 if ((MemoryType != EfiLoaderCode) &&
147 (MemoryType != EfiLoaderData) &&
148 (MemoryType != EfiRuntimeServicesCode) &&
149 (MemoryType != EfiRuntimeServicesData) &&
150 (MemoryType != EfiBootServicesCode) &&
151 (MemoryType != EfiBootServicesData) &&
152 (MemoryType != EfiACPIReclaimMemory) &&
153 (MemoryType != EfiReservedMemoryType) &&
154 (MemoryType != EfiACPIMemoryNVS)) {
155 return EFI_INVALID_PARAMETER;
156 }
157
158 Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
159
160 if (RUNTIME_PAGE_ALLOCATION_GRANULARITY > DEFAULT_PAGE_ALLOCATION_GRANULARITY &&
161 (MemoryType == EfiACPIReclaimMemory ||
162 MemoryType == EfiACPIMemoryNVS ||
163 MemoryType == EfiRuntimeServicesCode ||
164 MemoryType == EfiRuntimeServicesData)) {
165
166 Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
167
168 DEBUG ((DEBUG_INFO, "AllocatePages: aligning allocation to %d KB\n",
169 Granularity / SIZE_1KB));
170 }
171
172 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
173 Hob.Raw = PrivateData->HobList.Raw;
174
175 //
176 // Check if Hob already available
177 //
178 if (!PrivateData->PeiMemoryInstalled) {
179 //
180 // When PeiInstallMemory is called but temporary memory has *not* been moved to temporary memory,
181 // the AllocatePage will depend on the field of PEI_CORE_INSTANCE structure.
182 //
183 if (!PrivateData->SwitchStackSignal) {
184 return EFI_NOT_AVAILABLE_YET;
185 } else {
186 FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);
187 FreeMemoryBottom = &(PrivateData->PhysicalMemoryBegin);
188 }
189 } else {
190 FreeMemoryTop = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);
191 FreeMemoryBottom = &(Hob.HandoffInformationTable->EfiFreeMemoryBottom);
192 }
193
194 //
195 // Check to see if on correct boundary for the memory type.
196 // If not aligned, make the allocation aligned.
197 //
198 Padding = *(FreeMemoryTop) & (Granularity - 1);
199 if ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom) < Padding) {
200 DEBUG ((DEBUG_ERROR, "AllocatePages failed: Out of space after padding.\n"));
201 return EFI_OUT_OF_RESOURCES;
202 }
203
204 *(FreeMemoryTop) -= Padding;
205 if (Padding >= EFI_PAGE_SIZE) {
206 //
207 // Create a memory allocation HOB to cover
208 // the pages that we will lose to rounding
209 //
210 BuildMemoryAllocationHob (
211 *(FreeMemoryTop),
212 Padding & ~(UINTN)EFI_PAGE_MASK,
213 EfiConventionalMemory
214 );
215 }
216
217 //
218 // Verify that there is sufficient memory to satisfy the allocation.
219 // For page allocation, the overhead sizeof (EFI_HOB_MEMORY_ALLOCATION) needs to be considered.
220 //
221 if ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom) < (UINTN) ALIGN_VALUE (sizeof (EFI_HOB_MEMORY_ALLOCATION), 8)) {
222 DEBUG ((EFI_D_ERROR, "AllocatePages failed: No space to build memory allocation hob.\n"));
223 return EFI_OUT_OF_RESOURCES;
224 }
225 RemainingPages = (UINTN)(*FreeMemoryTop - *FreeMemoryBottom - ALIGN_VALUE (sizeof (EFI_HOB_MEMORY_ALLOCATION), 8)) >> EFI_PAGE_SHIFT;
226 //
227 // The number of remaining pages needs to be greater than or equal to that of the request pages.
228 //
229 Pages = ALIGN_VALUE (Pages, EFI_SIZE_TO_PAGES (Granularity));
230 if (RemainingPages < Pages) {
231 DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64) Pages));
232 DEBUG ((EFI_D_ERROR, "There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64) RemainingPages));
233 return EFI_OUT_OF_RESOURCES;
234 } else {
235 //
236 // Update the PHIT to reflect the memory usage
237 //
238 *(FreeMemoryTop) -= Pages * EFI_PAGE_SIZE;
239
240 //
241 // Update the value for the caller
242 //
243 *Memory = *(FreeMemoryTop);
244
245 //
246 // Create a memory allocation HOB.
247 //
248 BuildMemoryAllocationHob (
249 *(FreeMemoryTop),
250 Pages * EFI_PAGE_SIZE,
251 MemoryType
252 );
253
254 return EFI_SUCCESS;
255 }
256 }
257
258 /**
259
260 Pool allocation service. Before permanent memory is discoveried, the pool will
261 be allocated the heap in the temporary memory. Genenrally, the size of heap in temporary
262 memory does not exceed to 64K, so the biggest pool size could be allocated is
263 64K.
264
265 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
266 @param Size Amount of memory required
267 @param Buffer Address of pointer to the buffer
268
269 @retval EFI_SUCCESS The allocation was successful
270 @retval EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement
271 to allocate the requested size.
272
273 **/
274 EFI_STATUS
275 EFIAPI
276 PeiAllocatePool (
277 IN CONST EFI_PEI_SERVICES **PeiServices,
278 IN UINTN Size,
279 OUT VOID **Buffer
280 )
281 {
282 EFI_STATUS Status;
283 EFI_HOB_MEMORY_POOL *Hob;
284
285 //
286 // If some "post-memory" PEIM wishes to allocate larger pool,
287 // it should use AllocatePages service instead.
288 //
289
290 //
291 // Generally, the size of heap in temporary memory does not exceed to 64K,
292 // HobLength is multiples of 8 bytes, so the maxmium size of pool is 0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL)
293 //
294 if (Size > (0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL))) {
295 return EFI_OUT_OF_RESOURCES;
296 }
297
298 Status = PeiServicesCreateHob (
299 EFI_HOB_TYPE_MEMORY_POOL,
300 (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),
301 (VOID **)&Hob
302 );
303 ASSERT_EFI_ERROR (Status);
304 *Buffer = Hob+1;
305
306 return Status;
307 }