]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/PeilessStartupLib/Hob.c
OvmfPkg/PlatformInitLib: Add PlatformGetLowMemoryCB
[mirror_edk2.git] / OvmfPkg / Library / PeilessStartupLib / Hob.c
CommitLineData
4fe26784
MX
1/** @file\r
2 Main SEC phase code. Handles initial TDX Hob List Processing\r
3\r
4 Copyright (c) 2008, Intel Corporation. All rights reserved.<BR>\r
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
6\r
7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8\r
9**/\r
10\r
11#include <PiPei.h>\r
12#include <Library/BaseLib.h>\r
13#include <Library/DebugLib.h>\r
14#include <Library/HobLib.h>\r
15#include <Library/BaseMemoryLib.h>\r
16#include <Library/PciLib.h>\r
17#include <Library/PrePiLib.h>\r
18#include <Library/QemuFwCfgLib.h>\r
19#include <IndustryStandard/Tdx.h>\r
20#include <IndustryStandard/UefiTcgPlatform.h>\r
21#include <Library/PlatformInitLib.h>\r
22#include <OvmfPlatforms.h>\r
9b648112 23#include <Pi/PrePiHob.h>\r
4fe26784
MX
24#include "PeilessStartupInternal.h"\r
25\r
26/**\r
27 * Construct the HobList in SEC phase.\r
28 *\r
29 * @return EFI_SUCCESS Successfully construct the firmware hoblist.\r
30 * @return EFI_NOT_FOUND Cannot find a memory region to be the fw hoblist.\r
31 */\r
32EFI_STATUS\r
33EFIAPI\r
34ConstructSecHobList (\r
35 )\r
36{\r
37 UINT32 LowMemorySize;\r
38 UINT32 LowMemoryStart;\r
39\r
40 EFI_HOB_HANDOFF_INFO_TABLE *HobList;\r
41 EFI_HOB_PLATFORM_INFO PlatformInfoHob;\r
42\r
43 ZeroMem (&PlatformInfoHob, sizeof (PlatformInfoHob));\r
44 PlatformInfoHob.HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
124b7650
GH
45 PlatformGetSystemMemorySizeBelow4gb (&PlatformInfoHob);\r
46 LowMemorySize = PlatformInfoHob.LowMemory;\r
4fe26784
MX
47 ASSERT (LowMemorySize != 0);\r
48 LowMemoryStart = FixedPcdGet32 (PcdOvmfDxeMemFvBase) + FixedPcdGet32 (PcdOvmfDxeMemFvSize);\r
49 LowMemorySize -= LowMemoryStart;\r
50\r
51 DEBUG ((DEBUG_INFO, "LowMemory Start and End: %x, %x\n", LowMemoryStart, LowMemoryStart + LowMemorySize));\r
52 HobList = HobConstructor (\r
53 (VOID *)(UINTN)LowMemoryStart,\r
54 LowMemorySize,\r
55 (VOID *)(UINTN)LowMemoryStart,\r
56 (VOID *)(UINTN)(LowMemoryStart + LowMemorySize)\r
57 );\r
58\r
59 SetHobList ((VOID *)(UINT64)HobList);\r
60\r
61 return EFI_SUCCESS;\r
62}\r
63\r
64/**\r
65 * This function is to find a memory region which is the largest one below 4GB.\r
66 * It will be used as the firmware hoblist.\r
67 *\r
68 * @param VmmHobList Vmm passed hoblist which constains the memory information.\r
69 * @return EFI_SUCCESS Successfully construct the firmware hoblist.\r
70 */\r
71EFI_STATUS\r
72EFIAPI\r
73ConstructFwHobList (\r
74 IN CONST VOID *VmmHobList\r
75 )\r
76{\r
77 EFI_PEI_HOB_POINTERS Hob;\r
78 EFI_PHYSICAL_ADDRESS PhysicalEnd;\r
79 UINT64 ResourceLength;\r
80 EFI_PHYSICAL_ADDRESS LowMemoryStart;\r
81 UINT64 LowMemoryLength;\r
82\r
83 ASSERT (VmmHobList != NULL);\r
84\r
85 Hob.Raw = (UINT8 *)VmmHobList;\r
86\r
87 LowMemoryLength = 0;\r
88 LowMemoryStart = 0;\r
89\r
90 //\r
91 // Parse the HOB list until end of list or matching type is found.\r
92 //\r
93 while (!END_OF_HOB_LIST (Hob)) {\r
94 if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
9b648112 95 if (Hob.ResourceDescriptor->ResourceType == BZ3937_EFI_RESOURCE_MEMORY_UNACCEPTED) {\r
4fe26784
MX
96 PhysicalEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength;\r
97 ResourceLength = Hob.ResourceDescriptor->ResourceLength;\r
98\r
99 if (PhysicalEnd <= BASE_4GB) {\r
100 if (ResourceLength > LowMemoryLength) {\r
101 LowMemoryStart = Hob.ResourceDescriptor->PhysicalStart;\r
102 LowMemoryLength = ResourceLength;\r
103 }\r
104 } else {\r
105 break;\r
106 }\r
107 }\r
108 }\r
109\r
110 Hob.Raw = GET_NEXT_HOB (Hob);\r
111 }\r
112\r
113 if (LowMemoryLength == 0) {\r
114 DEBUG ((DEBUG_ERROR, "Cannot find a memory region under 4GB for Fw hoblist.\n"));\r
115 return EFI_NOT_FOUND;\r
116 }\r
117\r
118 //\r
119 // HobLib doesn't like HobStart at address 0 so adjust is needed\r
120 //\r
121 if (LowMemoryStart == 0) {\r
122 LowMemoryStart += EFI_PAGE_SIZE;\r
123 LowMemoryLength -= EFI_PAGE_SIZE;\r
124 }\r
125\r
126 DEBUG ((DEBUG_INFO, "LowMemory Start and End: %x, %x\n", LowMemoryStart, LowMemoryStart + LowMemoryLength));\r
127 HobConstructor (\r
128 (VOID *)LowMemoryStart,\r
129 LowMemoryLength,\r
130 (VOID *)LowMemoryStart,\r
131 (VOID *)(LowMemoryStart + LowMemoryLength)\r
132 );\r
133\r
134 SetHobList ((VOID *)(UINT64)LowMemoryStart);\r
135\r
136 return EFI_SUCCESS;\r
137}\r