]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/PlatformPei/MemTypeInfo.c
OvmfPkg/PlatformPei: extract memory type info defaults to PCDs
[mirror_edk2.git] / OvmfPkg / PlatformPei / MemTypeInfo.c
CommitLineData
d42fdd6f 1/** @file\r
356b96b3 2 Produce the memory type information HOB.\r
d42fdd6f
LE
3\r
4 Copyright (C) 2017-2020, Red Hat, Inc.\r
5\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7**/\r
8\r
9#include <Guid/MemoryTypeInformation.h>\r
10#include <Library/BaseLib.h>\r
11#include <Library/DebugLib.h>\r
12#include <Library/HobLib.h>\r
13#include <Library/PcdLib.h>\r
14#include <Library/PeiServicesLib.h>\r
15#include <Ppi/ReadOnlyVariable2.h>\r
16#include <Uefi/UefiMultiPhase.h>\r
17\r
18#include "Platform.h"\r
19\r
8db87f98
LE
20#define MEMORY_TYPE_INFO_DEFAULT(Type) \\r
21 { Type, FixedPcdGet32 (PcdMemoryType ## Type) }\r
22\r
356b96b3 23STATIC EFI_MEMORY_TYPE_INFORMATION mMemoryTypeInformation[] = {\r
8db87f98
LE
24 MEMORY_TYPE_INFO_DEFAULT (EfiACPIMemoryNVS),\r
25 MEMORY_TYPE_INFO_DEFAULT (EfiACPIReclaimMemory),\r
26 MEMORY_TYPE_INFO_DEFAULT (EfiReservedMemoryType),\r
27 MEMORY_TYPE_INFO_DEFAULT (EfiRuntimeServicesCode),\r
28 MEMORY_TYPE_INFO_DEFAULT (EfiRuntimeServicesData),\r
29 { EfiMaxMemoryType, 0 }\r
d42fdd6f
LE
30};\r
31\r
32STATIC\r
33VOID\r
34BuildMemTypeInfoHob (\r
35 VOID\r
36 )\r
37{\r
38 BuildGuidDataHob (\r
39 &gEfiMemoryTypeInformationGuid,\r
356b96b3
LE
40 mMemoryTypeInformation,\r
41 sizeof mMemoryTypeInformation\r
d42fdd6f 42 );\r
d42fdd6f
LE
43}\r
44\r
45/**\r
356b96b3
LE
46 Refresh the mMemoryTypeInformation array (which we'll turn into the\r
47 MemoryTypeInformation HOB) from the MemoryTypeInformation UEFI variable.\r
d42fdd6f 48\r
356b96b3
LE
49 Normally, the DXE IPL PEIM builds the HOB from the UEFI variable. But it does\r
50 so *transparently*. Instead, we consider the UEFI variable as a list of\r
51 hints, for updating our HOB defaults:\r
d42fdd6f 52\r
356b96b3
LE
53 - Record types not covered in mMemoryTypeInformation are ignored. In\r
54 particular, this hides record types from the UEFI variable that may lead to\r
55 reboots without benefiting SMM security, such as EfiBootServicesData.\r
56\r
57 - Records that would lower the defaults in mMemoryTypeInformation are also\r
58 ignored.\r
59\r
60 @param[in] ReadOnlyVariable2 The EFI_PEI_READ_ONLY_VARIABLE2_PPI used for\r
61 retrieving the MemoryTypeInformation UEFI\r
62 variable.\r
d42fdd6f
LE
63**/\r
64STATIC\r
356b96b3
LE
65VOID\r
66RefreshMemTypeInfo (\r
67 IN EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadOnlyVariable2\r
d42fdd6f
LE
68 )\r
69{\r
356b96b3
LE
70 UINTN DataSize;\r
71 EFI_MEMORY_TYPE_INFORMATION Entries[EfiMaxMemoryType + 1];\r
72 EFI_STATUS Status;\r
73 UINTN NumEntries;\r
74 UINTN HobRecordIdx;\r
d42fdd6f
LE
75\r
76 //\r
356b96b3 77 // Read the MemoryTypeInformation UEFI variable from the\r
d42fdd6f
LE
78 // gEfiMemoryTypeInformationGuid namespace.\r
79 //\r
356b96b3 80 DataSize = sizeof Entries;\r
d42fdd6f
LE
81 Status = ReadOnlyVariable2->GetVariable (\r
82 ReadOnlyVariable2,\r
83 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,\r
84 &gEfiMemoryTypeInformationGuid,\r
85 NULL,\r
86 &DataSize,\r
356b96b3 87 Entries\r
d42fdd6f 88 );\r
356b96b3 89 if (EFI_ERROR (Status)) {\r
d42fdd6f 90 //\r
356b96b3
LE
91 // If the UEFI variable does not exist (EFI_NOT_FOUND), we can't use it for\r
92 // udpating mMemoryTypeInformation.\r
d42fdd6f 93 //\r
356b96b3
LE
94 // If the UEFI variable exists but Entries is too small to hold it\r
95 // (EFI_BUFFER_TOO_SMALL), then the variable contents are arguably invalid.\r
96 // That's because Entries has room for every distinct EFI_MEMORY_TYPE,\r
97 // including the terminator record with EfiMaxMemoryType. Thus, we can't\r
98 // use the UEFI variable for updating mMemoryTypeInformation.\r
d42fdd6f 99 //\r
356b96b3
LE
100 // If the UEFI variable couldn't be read for some other reason, we\r
101 // similarly can't use it for udpating mMemoryTypeInformation.\r
d42fdd6f 102 //\r
356b96b3
LE
103 DEBUG ((DEBUG_ERROR, "%a: GetVariable(): %r\n", __FUNCTION__, Status));\r
104 return;\r
d42fdd6f
LE
105 }\r
106\r
356b96b3
LE
107 //\r
108 // Sanity-check the UEFI variable size against the record size.\r
109 //\r
110 if (DataSize % sizeof Entries[0] != 0) {\r
111 DEBUG ((DEBUG_ERROR, "%a: invalid UEFI variable size %Lu\n", __FUNCTION__,\r
112 (UINT64)DataSize));\r
113 return;\r
114 }\r
115 NumEntries = DataSize / sizeof Entries[0];\r
116\r
117 //\r
118 // For each record in mMemoryTypeInformation, except the terminator record,\r
119 // look up the first match (if any) in the UEFI variable, based on the memory\r
120 // type.\r
121 //\r
122 for (HobRecordIdx = 0;\r
123 HobRecordIdx < ARRAY_SIZE (mMemoryTypeInformation) - 1;\r
124 HobRecordIdx++) {\r
125 EFI_MEMORY_TYPE_INFORMATION *HobRecord;\r
126 UINTN Idx;\r
127 EFI_MEMORY_TYPE_INFORMATION *VariableRecord;\r
128\r
129 HobRecord = &mMemoryTypeInformation[HobRecordIdx];\r
130\r
131 for (Idx = 0; Idx < NumEntries; Idx++) {\r
132 VariableRecord = &Entries[Idx];\r
133\r
134 if (VariableRecord->Type == HobRecord->Type) {\r
135 break;\r
136 }\r
137 }\r
138\r
139 //\r
140 // If there is a match, allow the UEFI variable to increase NumberOfPages.\r
141 //\r
142 if (Idx < NumEntries &&\r
143 HobRecord->NumberOfPages < VariableRecord->NumberOfPages) {\r
144 DEBUG ((DEBUG_VERBOSE, "%a: Type 0x%x: NumberOfPages 0x%x -> 0x%x\n",\r
145 __FUNCTION__, HobRecord->Type, HobRecord->NumberOfPages,\r
146 VariableRecord->NumberOfPages));\r
147\r
148 HobRecord->NumberOfPages = VariableRecord->NumberOfPages;\r
149 }\r
150 }\r
151}\r
152\r
153/**\r
154 Notification function called when EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes\r
155 available.\r
156\r
157 @param[in] PeiServices Indirect reference to the PEI Services Table.\r
158 @param[in] NotifyDescriptor Address of the notification descriptor data\r
159 structure.\r
160 @param[in] Ppi Address of the PPI that was installed.\r
161\r
162 @return Status of the notification. The status code returned from this\r
163 function is ignored.\r
164**/\r
165STATIC\r
166EFI_STATUS\r
167EFIAPI\r
168OnReadOnlyVariable2Available (\r
169 IN EFI_PEI_SERVICES **PeiServices,\r
170 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
171 IN VOID *Ppi\r
172 )\r
173{\r
174 DEBUG ((DEBUG_VERBOSE, "%a\n", __FUNCTION__));\r
175\r
176 RefreshMemTypeInfo (Ppi);\r
177 BuildMemTypeInfoHob ();\r
d42fdd6f
LE
178 return EFI_SUCCESS;\r
179}\r
180\r
181//\r
182// Notification object for registering the callback, for when\r
183// EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes available.\r
184//\r
185STATIC CONST EFI_PEI_NOTIFY_DESCRIPTOR mReadOnlyVariable2Notify = {\r
186 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH |\r
187 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), // Flags\r
188 &gEfiPeiReadOnlyVariable2PpiGuid, // Guid\r
189 OnReadOnlyVariable2Available // Notify\r
190};\r
191\r
192VOID\r
193MemTypeInfoInitialization (\r
194 VOID\r
195 )\r
196{\r
197 EFI_STATUS Status;\r
198\r
199 if (!FeaturePcdGet (PcdSmmSmramRequire)) {\r
200 //\r
201 // EFI_PEI_READ_ONLY_VARIABLE2_PPI will never be available; install\r
202 // the default memory type information HOB right away.\r
203 //\r
204 BuildMemTypeInfoHob ();\r
205 return;\r
206 }\r
207\r
208 Status = PeiServicesNotifyPpi (&mReadOnlyVariable2Notify);\r
209 if (EFI_ERROR (Status)) {\r
210 DEBUG ((DEBUG_ERROR, "%a: failed to set up R/O Variable 2 callback: %r\n",\r
211 __FUNCTION__, Status));\r
212 ASSERT (FALSE);\r
213 CpuDeadLoop ();\r
214 }\r
215}\r