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