]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/FspSupport/Library/PeiFspHobProcessLibVlv2/FspHobProcessLibVlv2.c
ArmPkg/CompilerIntrinsicsLib: Add uread, uwrite GCC assembly sources
[mirror_edk2.git] / Vlv2TbltDevicePkg / FspSupport / Library / PeiFspHobProcessLibVlv2 / FspHobProcessLibVlv2.c
1 /** @file
2 Null instance of Platform Sec Lib.
3
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiPei.h>
10
11 #include <Library/PeiServicesLib.h>
12 #include <Library/PeiServicesTablePointerLib.h>
13 #include <Library/BaseLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/HobLib.h>
17 #include <Library/PcdLib.h>
18 #include <Library/FspPlatformInfoLib.h>
19
20 #include <Guid/GuidHobFsp.h>
21 #include <Guid/MemoryTypeInformation.h>
22 #include <Ppi/Capsule.h>
23
24 #include <PlatformFspLib.h>
25 #include <Guid/SmramMemoryReserve.h>
26 EFI_GUID gFspReservedMemoryResourceHobTsegGuid = {0xd038747c, 0xd00c, 0x4980, {0xb3, 0x19, 0x49, 0x01, 0x99, 0xa4, 0x7d, 0x55}};
27
28 //
29 // Additional pages are used by DXE memory manager.
30 // It should be consistent between RetrieveRequiredMemorySize() and GetPeiMemSize()
31 //
32 #define PEI_ADDITIONAL_MEMORY_SIZE (16 * EFI_PAGE_SIZE)
33
34 /**
35 Get the mem size in memory type infromation table.
36
37 @param PeiServices PEI Services table.
38
39 @return the mem size in memory type infromation table.
40 **/
41 UINT64
42 GetMemorySizeInMemoryTypeInformation (
43 IN EFI_PEI_SERVICES **PeiServices
44 )
45 {
46 EFI_PEI_HOB_POINTERS Hob;
47 EFI_MEMORY_TYPE_INFORMATION *MemoryData;
48 UINT8 Index;
49 UINTN TempPageNum;
50
51 MemoryData = NULL;
52 (*PeiServices)->GetHobList ((CONST EFI_PEI_SERVICES **)PeiServices, (VOID **) &Hob.Raw);
53 while (!END_OF_HOB_LIST (Hob)) {
54 if (Hob.Header->HobType == EFI_HOB_TYPE_GUID_EXTENSION &&
55 CompareGuid (&Hob.Guid->Name, &gEfiMemoryTypeInformationGuid)) {
56 MemoryData = (EFI_MEMORY_TYPE_INFORMATION *) (Hob.Raw + sizeof (EFI_HOB_GENERIC_HEADER) + sizeof (EFI_GUID));
57 break;
58 }
59
60 Hob.Raw = GET_NEXT_HOB (Hob);
61 }
62
63 if (MemoryData == NULL) {
64 return 0;
65 }
66
67 TempPageNum = 0;
68 for (Index = 0; MemoryData[Index].Type != EfiMaxMemoryType; Index++) {
69 //
70 // Accumulate default memory size requirements
71 //
72 TempPageNum += MemoryData[Index].NumberOfPages;
73 }
74
75 return TempPageNum * EFI_PAGE_SIZE;
76 }
77
78 /**
79 Get the mem size need to be reserved in PEI phase.
80
81 @param PeiServices PEI Services table.
82
83 @return the mem size need to be reserved in PEI phase.
84 **/
85 UINT64
86 RetrieveRequiredMemorySize (
87 IN EFI_PEI_SERVICES **PeiServices
88 )
89 {
90 UINT64 Size;
91
92 Size = GetMemorySizeInMemoryTypeInformation (PeiServices);
93 return Size + PEI_ADDITIONAL_MEMORY_SIZE;
94 }
95
96 /**
97 Get the mem size need to be consumed and reserved in PEI phase.
98
99 @param PeiServices PEI Services table.
100 @param BootMode Current boot mode.
101
102 @return the mem size need to be consumed and reserved in PEI phase.
103 **/
104 UINT64
105 GetPeiMemSize (
106 IN EFI_PEI_SERVICES **PeiServices,
107 IN UINT32 BootMode
108 )
109 {
110 UINT64 Size;
111 UINT64 MinSize;
112
113 if (BootMode == BOOT_IN_RECOVERY_MODE) {
114 return PcdGet32 (PcdPeiRecoveryMinMemSize);
115 }
116
117 Size = GetMemorySizeInMemoryTypeInformation (PeiServices);
118
119 if (BootMode == BOOT_ON_FLASH_UPDATE) {
120 //
121 // Maybe more size when in CapsuleUpdate phase ?
122 //
123 MinSize = PcdGet32 (PcdPeiMinMemSize);
124 } else {
125 MinSize = PcdGet32 (PcdPeiMinMemSize);
126 }
127
128 return MinSize + Size + PEI_ADDITIONAL_MEMORY_SIZE;
129 }
130
131 /**
132 BIOS process FspBobList.
133
134 @param FspHobList Pointer to the HOB data structure produced by FSP.
135
136 @return If platform process the FSP hob list successfully.
137 **/
138 EFI_STATUS
139 EFIAPI
140 FspHobProcessForMemoryResource (
141 IN VOID *FspHobList
142 )
143 {
144 EFI_PEI_HOB_POINTERS Hob;
145 UINT64 LowMemorySize;
146 UINT64 FspMemorySize;
147 EFI_PHYSICAL_ADDRESS FspMemoryBase;
148 UINT64 PeiMemSize;
149 EFI_PHYSICAL_ADDRESS PeiMemBase;
150 UINT64 S3PeiMemSize;
151 EFI_PHYSICAL_ADDRESS S3PeiMemBase;
152 BOOLEAN FoundFspMemHob;
153 EFI_STATUS Status;
154 EFI_BOOT_MODE BootMode;
155 PEI_CAPSULE_PPI *Capsule;
156 VOID *CapsuleBuffer;
157 UINTN CapsuleBufferLength;
158 UINT64 RequiredMemSize;
159 EFI_PEI_SERVICES **PeiServices;
160 UINT64 TsegSize;
161 EFI_PHYSICAL_ADDRESS TsegBase;
162 BOOLEAN FoundTsegHob;
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 TsegSize = 0;
174 TsegBase = 0;
175 FoundTsegHob = FALSE;
176
177 //
178 // Parse the hob list from fsp
179 // Report all the resource hob except the memory between 1M and 4G
180 //
181 Hob.Raw = (UINT8 *)(UINTN)FspHobList;
182 DEBUG((DEBUG_INFO, "FspHobList - 0x%x\n", FspHobList));
183
184 while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw)) != NULL) {
185 DEBUG((DEBUG_INFO, "\nResourceType: 0x%x\n", Hob.ResourceDescriptor->ResourceType));
186 if ((Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) ||
187 (Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_MEMORY_RESERVED)) {
188 DEBUG((DEBUG_INFO, "ResourceAttribute: 0x%x\n", Hob.ResourceDescriptor->ResourceAttribute));
189 DEBUG((DEBUG_INFO, "PhysicalStart: 0x%x\n", Hob.ResourceDescriptor->PhysicalStart));
190 DEBUG((DEBUG_INFO, "ResourceLength: 0x%x\n", Hob.ResourceDescriptor->ResourceLength));
191 DEBUG((DEBUG_INFO, "Owner: %g\n\n", &Hob.ResourceDescriptor->Owner));
192 }
193
194 if ((Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) // Found the low memory length below 4G
195 && (Hob.ResourceDescriptor->PhysicalStart >= BASE_1MB)
196 && (Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength <= BASE_4GB)) {
197 LowMemorySize += Hob.ResourceDescriptor->ResourceLength;
198 Hob.Raw = GET_NEXT_HOB (Hob);
199 continue;
200 }
201
202 if ((Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_MEMORY_RESERVED) // Found the low memory length below 4G
203 && (Hob.ResourceDescriptor->PhysicalStart >= BASE_1MB)
204 && (Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength <= BASE_4GB)
205 && (CompareGuid (&Hob.ResourceDescriptor->Owner, &gFspReservedMemoryResourceHobGuid))) {
206 FoundFspMemHob = TRUE;
207 FspMemoryBase = Hob.ResourceDescriptor->PhysicalStart;
208 FspMemorySize = Hob.ResourceDescriptor->ResourceLength;
209 DEBUG((DEBUG_INFO, "Find fsp mem hob, base 0x%x, len 0x%x\n", FspMemoryBase, FspMemorySize));
210 }
211
212 if ((Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_MEMORY_RESERVED) // Found the low memory length below 4G
213 && (Hob.ResourceDescriptor->PhysicalStart >= 0x100000)
214 && (Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength <= 0x100000000)
215 && (CompareGuid (&Hob.ResourceDescriptor->Owner, &gFspReservedMemoryResourceHobTsegGuid))) {
216 FoundTsegHob = TRUE;
217 TsegBase = Hob.ResourceDescriptor->PhysicalStart;
218
219
220 if ((Hob.ResourceDescriptor->ResourceLength == 0 ) || (Hob.ResourceDescriptor->ResourceLength > 0x800000)){
221 Hob.ResourceDescriptor->ResourceLength = 0x800000;
222 }
223
224
225 TsegSize = Hob.ResourceDescriptor->ResourceLength;
226 DEBUG((EFI_D_ERROR, "Find Tseg mem hob, base 0x%lx, len 0x%lx\n", TsegBase, TsegSize));
227 }
228
229 //
230 // Report the resource hob
231 //
232 BuildResourceDescriptorHob (
233 Hob.ResourceDescriptor->ResourceType,
234 Hob.ResourceDescriptor->ResourceAttribute,
235 Hob.ResourceDescriptor->PhysicalStart,
236 Hob.ResourceDescriptor->ResourceLength
237 );
238
239 Hob.Raw = GET_NEXT_HOB (Hob);
240 }
241
242 if (!FoundFspMemHob) {
243 DEBUG((DEBUG_INFO, "Didn't find the fsp used memory information.\n"));
244 //ASSERT(FALSE);
245 }
246
247 DEBUG((DEBUG_INFO, "LowMemorySize: 0x%x.\n", LowMemorySize));
248 DEBUG((DEBUG_INFO, "FspMemoryBase: 0x%x.\n", FspMemoryBase));
249 DEBUG((DEBUG_INFO, "FspMemorySize: 0x%x.\n", FspMemorySize));
250
251 if (BootMode == BOOT_ON_S3_RESUME) {
252 BuildResourceDescriptorHob (
253 EFI_RESOURCE_SYSTEM_MEMORY,
254 (
255 EFI_RESOURCE_ATTRIBUTE_PRESENT |
256 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
257 // EFI_RESOURCE_ATTRIBUTE_TESTED |
258 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
259 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
260 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
261 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
262 ),
263 BASE_1MB,
264 LowMemorySize
265 );
266
267 Status = GetS3MemoryInfo (&S3PeiMemBase, &S3PeiMemSize);
268 ASSERT_EFI_ERROR (Status);
269 DEBUG((DEBUG_INFO, "S3 memory %Xh - %Xh bytes\n", S3PeiMemBase, S3PeiMemSize));
270
271 //
272 // Make sure Stack and PeiMemory are not overlap - JYAO1
273 //
274
275 Status = PeiServicesInstallPeiMemory (
276 S3PeiMemBase,
277 S3PeiMemSize
278 );
279 ASSERT_EFI_ERROR (Status);
280 } else {
281 PeiMemSize = GetPeiMemSize (PeiServices, BootMode);
282 DEBUG((DEBUG_INFO, "PEI memory size = %Xh bytes\n", PeiMemSize));
283
284 //
285 // Capsule mode
286 //
287 Capsule = NULL;
288 CapsuleBuffer = NULL;
289 CapsuleBufferLength = 0;
290 if (BootMode == BOOT_ON_FLASH_UPDATE) {
291 Status = PeiServicesLocatePpi (
292 &gPeiCapsulePpiGuid,
293 0,
294 NULL,
295 (VOID **) &Capsule
296 );
297 ASSERT_EFI_ERROR (Status);
298
299 if (Status == EFI_SUCCESS) {
300 //
301 // Make sure Stack and CapsuleBuffer are not overlap - JYAO1
302 //
303 CapsuleBuffer = (VOID *)(UINTN)BASE_1MB;
304 CapsuleBufferLength = (UINTN)(LowMemorySize - PeiMemSize);
305 //
306 // Call the Capsule PPI Coalesce function to coalesce the capsule data.
307 //
308 Status = Capsule->Coalesce (PeiServices, &CapsuleBuffer, &CapsuleBufferLength);
309 }
310 }
311
312 RequiredMemSize = RetrieveRequiredMemorySize (PeiServices);
313 DEBUG((DEBUG_INFO, "Required memory size = %Xh bytes\n", RequiredMemSize));
314
315 //
316 // Report the main memory
317 //
318 BuildResourceDescriptorHob (
319 EFI_RESOURCE_SYSTEM_MEMORY,
320 (
321 EFI_RESOURCE_ATTRIBUTE_PRESENT |
322 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
323 EFI_RESOURCE_ATTRIBUTE_TESTED |
324 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
325 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
326 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
327 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
328 ),
329 BASE_1MB,
330 LowMemorySize
331 );
332
333 //
334 // Make sure Stack and CapsuleBuffer are not overlap - JYAO1
335 //
336
337 //
338 // Install efi memory
339 //
340 PeiMemBase = BASE_1MB + LowMemorySize - PeiMemSize;
341 Status = PeiServicesInstallPeiMemory (
342 PeiMemBase,
343 PeiMemSize - RequiredMemSize
344 );
345 ASSERT_EFI_ERROR (Status);
346
347 if (Capsule != NULL) {
348 Status = Capsule->CreateState (PeiServices, CapsuleBuffer, CapsuleBufferLength);
349 }
350 }
351
352 //
353 // Report GUIDed HOB for reserving SMRAM regions
354 //
355 if (FoundTsegHob) {
356 EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *SmramHobDescriptorBlock;
357
358 SmramHobDescriptorBlock = BuildGuidHob (
359 &gEfiSmmPeiSmramMemoryReserveGuid,
360 sizeof (EFI_SMRAM_HOB_DESCRIPTOR_BLOCK)
361 );
362 ASSERT (SmramHobDescriptorBlock != NULL);
363
364 SmramHobDescriptorBlock->NumberOfSmmReservedRegions = 1;
365
366 SmramHobDescriptorBlock->Descriptor[0].PhysicalStart = TsegBase;
367 SmramHobDescriptorBlock->Descriptor[0].CpuStart = TsegBase;
368 SmramHobDescriptorBlock->Descriptor[0].PhysicalSize = TsegSize;
369 SmramHobDescriptorBlock->Descriptor[0].RegionState = EFI_SMRAM_CLOSED;
370 }
371 return EFI_SUCCESS;
372 }
373
374 /**
375 BIOS process FspBobList for other data (not Memory Resource Descriptor).
376
377 @param[in] FspHobList Pointer to the HOB data structure produced by FSP.
378
379 @return If platform process the FSP hob list successfully.
380 **/
381 EFI_STATUS
382 EFIAPI
383 FspHobProcessForOtherData (
384 IN VOID *FspHobList
385 )
386 {
387 EFI_PEI_SERVICES **PeiServices;
388
389 PeiServices = (EFI_PEI_SERVICES **)GetPeiServicesTablePointer ();
390
391 //
392 // Other hob for platform
393 //
394 PlatformHobCreateFromFsp ((CONST EFI_PEI_SERVICES **) PeiServices, FspHobList);
395
396 return EFI_SUCCESS;
397 }
398
399 /**
400 BIOS process FspBobList.
401
402 @param[in] FspHobList Pointer to the HOB data structure produced by FSP.
403
404 @return If platform process the FSP hob list successfully.
405 **/
406 EFI_STATUS
407 EFIAPI
408 FspHobProcess (
409 IN VOID *FspHobList
410 )
411 {
412 EFI_STATUS Status;
413
414 Status = FspHobProcessForMemoryResource (FspHobList);
415 if (EFI_ERROR (Status)) {
416 return Status;
417 }
418 Status = FspHobProcessForOtherData (FspHobList);
419
420 return Status;
421 }