]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFspWrapperPkg/Library/PeiFspHobProcessLibSample/FspHobProcessLibSample.c
f293dc8be8096c231ed8749ef073c36a5b1d32d0
[mirror_edk2.git] / IntelFspWrapperPkg / Library / PeiFspHobProcessLibSample / FspHobProcessLibSample.c
1 /** @file
2 Sample to provide FSP hob process related function.
3
4 Copyright (c) 2014, 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 <PiPei.h>
16
17 #include <Library/PeiServicesLib.h>
18 #include <Library/PeiServicesTablePointerLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/HobLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/FspPlatformInfoLib.h>
25
26 #include <Guid/GuidHobFspEas.h>
27 #include <Guid/MemoryTypeInformation.h>
28 #include <Ppi/Capsule.h>
29
30 //
31 // Additional pages are used by DXE memory manager.
32 // It should be consistent between RetrieveRequiredMemorySize() and GetPeiMemSize()
33 //
34 #define PEI_ADDITIONAL_MEMORY_SIZE (16 * EFI_PAGE_SIZE)
35
36 /**
37 Get the mem size in memory type infromation table.
38
39 @param[in] PeiServices PEI Services table.
40
41 @return the mem size in memory type infromation table.
42 **/
43 UINT64
44 GetMemorySizeInMemoryTypeInformation (
45 IN EFI_PEI_SERVICES **PeiServices
46 )
47 {
48 EFI_STATUS Status;
49 EFI_PEI_HOB_POINTERS Hob;
50 EFI_MEMORY_TYPE_INFORMATION *MemoryData;
51 UINT8 Index;
52 UINTN TempPageNum;
53
54 MemoryData = NULL;
55 Status = (*PeiServices)->GetHobList ((CONST EFI_PEI_SERVICES**)PeiServices, (VOID **) &Hob.Raw);
56 while (!END_OF_HOB_LIST (Hob)) {
57 if (Hob.Header->HobType == EFI_HOB_TYPE_GUID_EXTENSION &&
58 CompareGuid (&Hob.Guid->Name, &gEfiMemoryTypeInformationGuid)) {
59 MemoryData = (EFI_MEMORY_TYPE_INFORMATION *) (Hob.Raw + sizeof (EFI_HOB_GENERIC_HEADER) + sizeof (EFI_GUID));
60 break;
61 }
62
63 Hob.Raw = GET_NEXT_HOB (Hob);
64 }
65
66 if (MemoryData == NULL) {
67 return 0;
68 }
69
70 TempPageNum = 0;
71 for (Index = 0; MemoryData[Index].Type != EfiMaxMemoryType; Index++) {
72 //
73 // Accumulate default memory size requirements
74 //
75 TempPageNum += MemoryData[Index].NumberOfPages;
76 }
77
78 return TempPageNum * EFI_PAGE_SIZE;
79 }
80
81 /**
82 Get the mem size need to be reserved in PEI phase.
83
84 @param[in] PeiServices PEI Services table.
85
86 @return the mem size need to be reserved in PEI phase.
87 **/
88 UINT64
89 RetrieveRequiredMemorySize (
90 IN EFI_PEI_SERVICES **PeiServices
91 )
92 {
93 UINT64 Size;
94
95 Size = GetMemorySizeInMemoryTypeInformation (PeiServices);
96 return Size + PEI_ADDITIONAL_MEMORY_SIZE;
97 }
98
99 /**
100 Get the mem size need to be consumed and reserved in PEI phase.
101
102 @param[in] PeiServices PEI Services table.
103 @param[in] BootMode Current boot mode.
104
105 @return the mem size need to be consumed and reserved in PEI phase.
106 **/
107 UINT64
108 GetPeiMemSize (
109 IN EFI_PEI_SERVICES **PeiServices,
110 IN UINT32 BootMode
111 )
112 {
113 UINT64 Size;
114 UINT64 MinSize;
115
116 if (BootMode == BOOT_IN_RECOVERY_MODE) {
117 return PcdGet32 (PcdPeiRecoveryMinMemSize);
118 }
119
120 Size = GetMemorySizeInMemoryTypeInformation (PeiServices);
121
122 if (BootMode == BOOT_ON_FLASH_UPDATE) {
123 //
124 // Maybe more size when in CapsuleUpdate phase ?
125 //
126 MinSize = PcdGet32 (PcdPeiMinMemSize);
127 } else {
128 MinSize = PcdGet32 (PcdPeiMinMemSize);
129 }
130
131 return MinSize + Size + PEI_ADDITIONAL_MEMORY_SIZE;
132 }
133
134 /**
135 BIOS process FspBobList.
136
137 @param[in] FspHobList Pointer to the HOB data structure produced by FSP.
138
139 @return If platform process the FSP hob list successfully.
140 **/
141 EFI_STATUS
142 EFIAPI
143 FspHobProcess (
144 IN VOID *FspHobList
145 )
146 {
147 EFI_PEI_HOB_POINTERS Hob;
148 UINT64 LowMemorySize;
149 UINT64 FspMemorySize;
150 EFI_PHYSICAL_ADDRESS FspMemoryBase;
151 UINT64 PeiMemSize;
152 EFI_PHYSICAL_ADDRESS PeiMemBase;
153 UINT64 S3PeiMemSize;
154 EFI_PHYSICAL_ADDRESS S3PeiMemBase;
155 BOOLEAN FoundFspMemHob;
156 EFI_STATUS Status;
157 EFI_BOOT_MODE BootMode;
158 PEI_CAPSULE_PPI *Capsule;
159 VOID *CapsuleBuffer;
160 UINTN CapsuleBufferLength;
161 UINT64 RequiredMemSize;
162 EFI_PEI_SERVICES **PeiServices;
163
164 PeiServices = (EFI_PEI_SERVICES **)GetPeiServicesTablePointer ();
165
166 PeiServicesGetBootMode (&BootMode);
167
168 PeiMemBase = 0;
169 LowMemorySize = 0;
170 FspMemorySize = 0;
171 FspMemoryBase = 0;
172 FoundFspMemHob = FALSE;
173
174 //
175 // Parse the hob list from fsp
176 // Report all the resource hob except the memory between 1M and 4G
177 //
178 Hob.Raw = (UINT8 *)(UINTN)FspHobList;
179 DEBUG((DEBUG_INFO, "FspHobList - 0x%x\n", FspHobList));
180
181 while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw)) != NULL) {
182 DEBUG((DEBUG_INFO, "\nResourceType: 0x%x\n", Hob.ResourceDescriptor->ResourceType));
183 if ((Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) ||
184 (Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_MEMORY_RESERVED)) {
185 DEBUG((DEBUG_INFO, "ResourceAttribute: 0x%x\n", Hob.ResourceDescriptor->ResourceAttribute));
186 DEBUG((DEBUG_INFO, "PhysicalStart: 0x%x\n", Hob.ResourceDescriptor->PhysicalStart));
187 DEBUG((DEBUG_INFO, "ResourceLength: 0x%x\n", Hob.ResourceDescriptor->ResourceLength));
188 DEBUG((DEBUG_INFO, "Owner: %g\n\n", &Hob.ResourceDescriptor->Owner));
189 }
190
191 if ((Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) // Found the low memory length below 4G
192 && (Hob.ResourceDescriptor->PhysicalStart >= BASE_1MB)
193 && (Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength <= BASE_4GB)) {
194 LowMemorySize += Hob.ResourceDescriptor->ResourceLength;
195 Hob.Raw = GET_NEXT_HOB (Hob);
196 continue;
197 }
198
199 if ((Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_MEMORY_RESERVED) // Found the low memory length below 4G
200 && (Hob.ResourceDescriptor->PhysicalStart >= BASE_1MB)
201 && (Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength <= BASE_4GB)
202 && (CompareGuid (&Hob.ResourceDescriptor->Owner, &gFspReservedMemoryResourceHobGuid))) {
203 FoundFspMemHob = TRUE;
204 FspMemoryBase = Hob.ResourceDescriptor->PhysicalStart;
205 FspMemorySize = Hob.ResourceDescriptor->ResourceLength;
206 DEBUG((DEBUG_INFO, "Find fsp mem hob, base 0x%x, len 0x%x\n", FspMemoryBase, FspMemorySize));
207 }
208
209 //
210 // Report the resource hob
211 //
212 BuildResourceDescriptorHob (
213 Hob.ResourceDescriptor->ResourceType,
214 Hob.ResourceDescriptor->ResourceAttribute,
215 Hob.ResourceDescriptor->PhysicalStart,
216 Hob.ResourceDescriptor->ResourceLength
217 );
218
219 Hob.Raw = GET_NEXT_HOB (Hob);
220 }
221
222 if (!FoundFspMemHob) {
223 DEBUG((DEBUG_INFO, "Didn't find the fsp used memory information.\n"));
224 //ASSERT(FALSE);
225 }
226
227 DEBUG((DEBUG_INFO, "LowMemorySize: 0x%x.\n", LowMemorySize));
228 DEBUG((DEBUG_INFO, "FspMemoryBase: 0x%x.\n", FspMemoryBase));
229 DEBUG((DEBUG_INFO, "FspMemorySize: 0x%x.\n", FspMemorySize));
230
231 if (BootMode == BOOT_ON_S3_RESUME) {
232 BuildResourceDescriptorHob (
233 EFI_RESOURCE_SYSTEM_MEMORY,
234 (
235 EFI_RESOURCE_ATTRIBUTE_PRESENT |
236 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
237 // EFI_RESOURCE_ATTRIBUTE_TESTED |
238 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
239 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
240 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
241 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
242 ),
243 BASE_1MB,
244 LowMemorySize
245 );
246
247 Status = GetS3MemoryInfo (&S3PeiMemBase, &S3PeiMemSize);
248 ASSERT_EFI_ERROR (Status);
249 DEBUG((DEBUG_INFO, "S3 memory %Xh - %Xh bytes\n", S3PeiMemBase, S3PeiMemSize));
250
251 //
252 // Make sure Stack and PeiMemory are not overlap - JYAO1
253 //
254
255 Status = PeiServicesInstallPeiMemory (
256 S3PeiMemBase,
257 S3PeiMemSize
258 );
259 ASSERT_EFI_ERROR (Status);
260 } else {
261 PeiMemSize = GetPeiMemSize (PeiServices, BootMode);
262 DEBUG((DEBUG_INFO, "PEI memory size = %Xh bytes\n", PeiMemSize));
263
264 //
265 // Capsule mode
266 //
267 Capsule = NULL;
268 CapsuleBuffer = NULL;
269 CapsuleBufferLength = 0;
270 if (BootMode == BOOT_ON_FLASH_UPDATE) {
271 Status = PeiServicesLocatePpi (
272 &gPeiCapsulePpiGuid,
273 0,
274 NULL,
275 (VOID **) &Capsule
276 );
277 ASSERT_EFI_ERROR (Status);
278
279 if (Status == EFI_SUCCESS) {
280 //
281 // Make sure Stack and CapsuleBuffer are not overlap - JYAO1
282 //
283 CapsuleBuffer = (VOID *)(UINTN)BASE_1MB;
284 CapsuleBufferLength = (UINTN)(LowMemorySize - PeiMemSize);
285 //
286 // Call the Capsule PPI Coalesce function to coalesce the capsule data.
287 //
288 Status = Capsule->Coalesce (PeiServices, &CapsuleBuffer, &CapsuleBufferLength);
289 }
290 }
291
292 RequiredMemSize = RetrieveRequiredMemorySize (PeiServices);
293 DEBUG((DEBUG_INFO, "Required memory size = %Xh bytes\n", RequiredMemSize));
294
295 //
296 // Report the main memory
297 //
298 BuildResourceDescriptorHob (
299 EFI_RESOURCE_SYSTEM_MEMORY,
300 (
301 EFI_RESOURCE_ATTRIBUTE_PRESENT |
302 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
303 EFI_RESOURCE_ATTRIBUTE_TESTED |
304 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
305 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
306 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
307 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
308 ),
309 BASE_1MB,
310 LowMemorySize
311 );
312
313 //
314 // Make sure Stack and CapsuleBuffer are not overlap - JYAO1
315 //
316
317 //
318 // Install efi memory
319 //
320 PeiMemBase = BASE_1MB + LowMemorySize - PeiMemSize;
321 Status = PeiServicesInstallPeiMemory (
322 PeiMemBase,
323 PeiMemSize - RequiredMemSize
324 );
325 ASSERT_EFI_ERROR (Status);
326
327 if (Capsule != NULL) {
328 Status = Capsule->CreateState (PeiServices, CapsuleBuffer, CapsuleBufferLength);
329 }
330 }
331
332 //
333 // NV Storage Hob
334 //
335
336 return EFI_SUCCESS;
337 }