]>
Commit | Line | Data |
---|---|---|
49ba9447 | 1 | /**@file\r |
2 | Memory Detection for Virtual Machines.\r | |
3 | \r | |
035ce3b3 | 4 | Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r |
b26f0cf9 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
49ba9447 | 6 | \r |
7 | Module Name:\r | |
8 | \r | |
9 | MemDetect.c\r | |
10 | \r | |
11 | **/\r | |
12 | \r | |
13 | //\r | |
14 | // The package level header files this module uses\r | |
15 | //\r | |
1fceaddb | 16 | #include <IndustryStandard/E820.h>\r |
49edde15 | 17 | #include <IndustryStandard/I440FxPiix4.h>\r |
d5e06444 | 18 | #include <IndustryStandard/Q35MchIch9.h>\r |
9afcd48a | 19 | #include <IndustryStandard/CloudHv.h>\r |
49ba9447 | 20 | #include <PiPei.h>\r |
adec2bd5 | 21 | #include <Register/Intel/SmramSaveStateMap.h>\r |
49ba9447 | 22 | \r |
23 | //\r | |
24 | // The Library classes this module consumes\r | |
25 | //\r | |
d5e06444 | 26 | #include <Library/BaseLib.h>\r |
6a7cba79 | 27 | #include <Library/BaseMemoryLib.h>\r |
49ba9447 | 28 | #include <Library/DebugLib.h>\r |
29 | #include <Library/HobLib.h>\r | |
30 | #include <Library/IoLib.h>\r | |
0bbed066 | 31 | #include <Library/MemEncryptSevLib.h>\r |
c1c2669c | 32 | #include <Library/PcdLib.h>\r |
d5e06444 | 33 | #include <Library/PciLib.h>\r |
49ba9447 | 34 | #include <Library/PeimEntryPoint.h>\r |
35 | #include <Library/ResourcePublicationLib.h>\r | |
e8e5cd4a | 36 | #include <Library/MtrrLib.h>\r |
7e5b1b67 | 37 | #include <Library/QemuFwCfgLib.h>\r |
98800cce | 38 | #include <Library/QemuFwCfgSimpleParserLib.h>\r |
49ba9447 | 39 | \r |
40 | #include "Platform.h"\r | |
e6ea1464 | 41 | #include "Cmos.h"\r |
49ba9447 | 42 | \r |
ac0a286f | 43 | UINT8 mPhysMemAddressWidth;\r |
bc89fe48 | 44 | \r |
ac0a286f MK |
45 | STATIC UINT32 mS3AcpiReservedMemoryBase;\r |
46 | STATIC UINT32 mS3AcpiReservedMemorySize;\r | |
45d87081 | 47 | \r |
ac0a286f | 48 | STATIC UINT16 mQ35TsegMbytes;\r |
23bfb5c0 | 49 | \r |
ac0a286f | 50 | BOOLEAN mQ35SmramAtDefaultSmbase;\r |
73974f80 | 51 | \r |
ac0a286f | 52 | UINT32 mQemuUc32Base;\r |
49edde15 | 53 | \r |
23bfb5c0 LE |
54 | VOID\r |
55 | Q35TsegMbytesInitialization (\r | |
56 | VOID\r | |
57 | )\r | |
58 | {\r | |
ac0a286f MK |
59 | UINT16 ExtendedTsegMbytes;\r |
60 | RETURN_STATUS PcdStatus;\r | |
d5e06444 | 61 | \r |
e0ed7a9b | 62 | ASSERT (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID);\r |
d5e06444 LE |
63 | \r |
64 | //\r | |
65 | // Check if QEMU offers an extended TSEG.\r | |
66 | //\r | |
67 | // This can be seen from writing MCH_EXT_TSEG_MB_QUERY to the MCH_EXT_TSEG_MB\r | |
68 | // register, and reading back the register.\r | |
69 | //\r | |
70 | // On a QEMU machine type that does not offer an extended TSEG, the initial\r | |
71 | // write overwrites whatever value a malicious guest OS may have placed in\r | |
72 | // the (unimplemented) register, before entering S3 or rebooting.\r | |
73 | // Subsequently, the read returns MCH_EXT_TSEG_MB_QUERY unchanged.\r | |
74 | //\r | |
75 | // On a QEMU machine type that offers an extended TSEG, the initial write\r | |
76 | // triggers an update to the register. Subsequently, the value read back\r | |
77 | // (which is guaranteed to differ from MCH_EXT_TSEG_MB_QUERY) tells us the\r | |
78 | // number of megabytes.\r | |
79 | //\r | |
80 | PciWrite16 (DRAMC_REGISTER_Q35 (MCH_EXT_TSEG_MB), MCH_EXT_TSEG_MB_QUERY);\r | |
81 | ExtendedTsegMbytes = PciRead16 (DRAMC_REGISTER_Q35 (MCH_EXT_TSEG_MB));\r | |
82 | if (ExtendedTsegMbytes == MCH_EXT_TSEG_MB_QUERY) {\r | |
83 | mQ35TsegMbytes = PcdGet16 (PcdQ35TsegMbytes);\r | |
84 | return;\r | |
85 | }\r | |
86 | \r | |
87 | DEBUG ((\r | |
88 | DEBUG_INFO,\r | |
89 | "%a: QEMU offers an extended TSEG (%d MB)\n",\r | |
90 | __FUNCTION__,\r | |
91 | ExtendedTsegMbytes\r | |
92 | ));\r | |
93 | PcdStatus = PcdSet16S (PcdQ35TsegMbytes, ExtendedTsegMbytes);\r | |
94 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
95 | mQ35TsegMbytes = ExtendedTsegMbytes;\r | |
23bfb5c0 LE |
96 | }\r |
97 | \r | |
73974f80 LE |
98 | VOID\r |
99 | Q35SmramAtDefaultSmbaseInitialization (\r | |
100 | VOID\r | |
101 | )\r | |
102 | {\r | |
ac0a286f | 103 | RETURN_STATUS PcdStatus;\r |
73974f80 LE |
104 | \r |
105 | ASSERT (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID);\r | |
106 | \r | |
107 | mQ35SmramAtDefaultSmbase = FALSE;\r | |
75839f97 | 108 | if (FeaturePcdGet (PcdCsmEnable)) {\r |
ac0a286f MK |
109 | DEBUG ((\r |
110 | DEBUG_INFO,\r | |
111 | "%a: SMRAM at default SMBASE not checked due to CSM\n",\r | |
112 | __FUNCTION__\r | |
113 | ));\r | |
75839f97 | 114 | } else {\r |
ac0a286f MK |
115 | UINTN CtlReg;\r |
116 | UINT8 CtlRegVal;\r | |
75839f97 LE |
117 | \r |
118 | CtlReg = DRAMC_REGISTER_Q35 (MCH_DEFAULT_SMBASE_CTL);\r | |
119 | PciWrite8 (CtlReg, MCH_DEFAULT_SMBASE_QUERY);\r | |
ac0a286f | 120 | CtlRegVal = PciRead8 (CtlReg);\r |
75839f97 LE |
121 | mQ35SmramAtDefaultSmbase = (BOOLEAN)(CtlRegVal ==\r |
122 | MCH_DEFAULT_SMBASE_IN_RAM);\r | |
ac0a286f MK |
123 | DEBUG ((\r |
124 | DEBUG_INFO,\r | |
125 | "%a: SMRAM at default SMBASE %a\n",\r | |
126 | __FUNCTION__,\r | |
127 | mQ35SmramAtDefaultSmbase ? "found" : "not found"\r | |
128 | ));\r | |
75839f97 LE |
129 | }\r |
130 | \r | |
ac0a286f MK |
131 | PcdStatus = PcdSetBoolS (\r |
132 | PcdQ35SmramAtDefaultSmbase,\r | |
133 | mQ35SmramAtDefaultSmbase\r | |
134 | );\r | |
73974f80 LE |
135 | ASSERT_RETURN_ERROR (PcdStatus);\r |
136 | }\r | |
137 | \r | |
49edde15 LE |
138 | VOID\r |
139 | QemuUc32BaseInitialization (\r | |
140 | VOID\r | |
141 | )\r | |
142 | {\r | |
ac0a286f MK |
143 | UINT32 LowerMemorySize;\r |
144 | UINT32 Uc32Size;\r | |
49edde15 | 145 | \r |
6a8e9ad2 GH |
146 | if (mHostBridgeDevId == 0xffff /* microvm */) {\r |
147 | return;\r | |
148 | }\r | |
149 | \r | |
49edde15 LE |
150 | if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r |
151 | //\r | |
152 | // On q35, the 32-bit area that we'll mark as UC, through variable MTRRs,\r | |
153 | // starts at PcdPciExpressBaseAddress. The platform DSC is responsible for\r | |
154 | // setting PcdPciExpressBaseAddress such that describing the\r | |
155 | // [PcdPciExpressBaseAddress, 4GB) range require a very small number of\r | |
156 | // variable MTRRs (preferably 1 or 2).\r | |
157 | //\r | |
158 | ASSERT (FixedPcdGet64 (PcdPciExpressBaseAddress) <= MAX_UINT32);\r | |
159 | mQemuUc32Base = (UINT32)FixedPcdGet64 (PcdPciExpressBaseAddress);\r | |
160 | return;\r | |
161 | }\r | |
162 | \r | |
9afcd48a SB |
163 | if (mHostBridgeDevId == CLOUDHV_DEVICE_ID) {\r |
164 | Uc32Size = CLOUDHV_MMIO_HOLE_SIZE;\r | |
165 | mQemuUc32Base = CLOUDHV_MMIO_HOLE_ADDRESS;\r | |
166 | return;\r | |
167 | }\r | |
168 | \r | |
49edde15 LE |
169 | ASSERT (mHostBridgeDevId == INTEL_82441_DEVICE_ID);\r |
170 | //\r | |
171 | // On i440fx, start with the [LowerMemorySize, 4GB) range. Make sure one\r | |
172 | // variable MTRR suffices by truncating the size to a whole power of two,\r | |
173 | // while keeping the end affixed to 4GB. This will round the base up.\r | |
174 | //\r | |
175 | LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r | |
ac0a286f MK |
176 | Uc32Size = GetPowerOfTwo32 ((UINT32)(SIZE_4GB - LowerMemorySize));\r |
177 | mQemuUc32Base = (UINT32)(SIZE_4GB - Uc32Size);\r | |
49edde15 LE |
178 | //\r |
179 | // Assuming that LowerMemorySize is at least 1 byte, Uc32Size is at most 2GB.\r | |
180 | // Therefore mQemuUc32Base is at least 2GB.\r | |
181 | //\r | |
182 | ASSERT (mQemuUc32Base >= BASE_2GB);\r | |
183 | \r | |
184 | if (mQemuUc32Base != LowerMemorySize) {\r | |
ac0a286f MK |
185 | DEBUG ((\r |
186 | DEBUG_VERBOSE,\r | |
187 | "%a: rounded UC32 base from 0x%x up to 0x%x, for "\r | |
188 | "an UC32 size of 0x%x\n",\r | |
189 | __FUNCTION__,\r | |
190 | LowerMemorySize,\r | |
191 | mQemuUc32Base,\r | |
192 | Uc32Size\r | |
193 | ));\r | |
49edde15 LE |
194 | }\r |
195 | }\r | |
196 | \r | |
1fceaddb LE |
197 | /**\r |
198 | Iterate over the RAM entries in QEMU's fw_cfg E820 RAM map that start outside\r | |
199 | of the 32-bit address range.\r | |
200 | \r | |
201 | Find the highest exclusive >=4GB RAM address, or produce memory resource\r | |
202 | descriptor HOBs for RAM entries that start at or above 4GB.\r | |
203 | \r | |
204 | @param[out] MaxAddress If MaxAddress is NULL, then ScanOrAdd64BitE820Ram()\r | |
205 | produces memory resource descriptor HOBs for RAM\r | |
206 | entries that start at or above 4GB.\r | |
207 | \r | |
208 | Otherwise, MaxAddress holds the highest exclusive\r | |
209 | >=4GB RAM address on output. If QEMU's fw_cfg E820\r | |
210 | RAM map contains no RAM entry that starts outside of\r | |
211 | the 32-bit address range, then MaxAddress is exactly\r | |
212 | 4GB on output.\r | |
213 | \r | |
214 | @retval EFI_SUCCESS The fw_cfg E820 RAM map was found and processed.\r | |
215 | \r | |
216 | @retval EFI_PROTOCOL_ERROR The RAM map was found, but its size wasn't a\r | |
217 | whole multiple of sizeof(EFI_E820_ENTRY64). No\r | |
218 | RAM entry was processed.\r | |
219 | \r | |
220 | @return Error codes from QemuFwCfgFindFile(). No RAM\r | |
221 | entry was processed.\r | |
222 | **/\r | |
223 | STATIC\r | |
224 | EFI_STATUS\r | |
225 | ScanOrAdd64BitE820Ram (\r | |
557dede8 GH |
226 | IN BOOLEAN AddHighHob,\r |
227 | OUT UINT64 *LowMemory OPTIONAL,\r | |
ac0a286f | 228 | OUT UINT64 *MaxAddress OPTIONAL\r |
1fceaddb LE |
229 | )\r |
230 | {\r | |
ac0a286f MK |
231 | EFI_STATUS Status;\r |
232 | FIRMWARE_CONFIG_ITEM FwCfgItem;\r | |
233 | UINTN FwCfgSize;\r | |
234 | EFI_E820_ENTRY64 E820Entry;\r | |
235 | UINTN Processed;\r | |
1fceaddb LE |
236 | \r |
237 | Status = QemuFwCfgFindFile ("etc/e820", &FwCfgItem, &FwCfgSize);\r | |
238 | if (EFI_ERROR (Status)) {\r | |
239 | return Status;\r | |
240 | }\r | |
ac0a286f | 241 | \r |
1fceaddb LE |
242 | if (FwCfgSize % sizeof E820Entry != 0) {\r |
243 | return EFI_PROTOCOL_ERROR;\r | |
244 | }\r | |
245 | \r | |
557dede8 GH |
246 | if (LowMemory != NULL) {\r |
247 | *LowMemory = 0;\r | |
248 | }\r | |
249 | \r | |
1fceaddb LE |
250 | if (MaxAddress != NULL) {\r |
251 | *MaxAddress = BASE_4GB;\r | |
252 | }\r | |
253 | \r | |
254 | QemuFwCfgSelectItem (FwCfgItem);\r | |
255 | for (Processed = 0; Processed < FwCfgSize; Processed += sizeof E820Entry) {\r | |
256 | QemuFwCfgReadBytes (sizeof E820Entry, &E820Entry);\r | |
257 | DEBUG ((\r | |
258 | DEBUG_VERBOSE,\r | |
259 | "%a: Base=0x%Lx Length=0x%Lx Type=%u\n",\r | |
260 | __FUNCTION__,\r | |
261 | E820Entry.BaseAddr,\r | |
262 | E820Entry.Length,\r | |
263 | E820Entry.Type\r | |
264 | ));\r | |
557dede8 GH |
265 | if (E820Entry.Type == EfiAcpiAddressRangeMemory) {\r |
266 | if (AddHighHob && (E820Entry.BaseAddr >= BASE_4GB)) {\r | |
ac0a286f MK |
267 | UINT64 Base;\r |
268 | UINT64 End;\r | |
1fceaddb LE |
269 | \r |
270 | //\r | |
271 | // Round up the start address, and round down the end address.\r | |
272 | //\r | |
273 | Base = ALIGN_VALUE (E820Entry.BaseAddr, (UINT64)EFI_PAGE_SIZE);\r | |
ac0a286f MK |
274 | End = (E820Entry.BaseAddr + E820Entry.Length) &\r |
275 | ~(UINT64)EFI_PAGE_MASK;\r | |
1fceaddb LE |
276 | if (Base < End) {\r |
277 | AddMemoryRangeHob (Base, End);\r | |
278 | DEBUG ((\r | |
279 | DEBUG_VERBOSE,\r | |
280 | "%a: AddMemoryRangeHob [0x%Lx, 0x%Lx)\n",\r | |
281 | __FUNCTION__,\r | |
282 | Base,\r | |
283 | End\r | |
284 | ));\r | |
285 | }\r | |
557dede8 GH |
286 | }\r |
287 | \r | |
288 | if (MaxAddress || LowMemory) {\r | |
ac0a286f | 289 | UINT64 Candidate;\r |
1fceaddb LE |
290 | \r |
291 | Candidate = E820Entry.BaseAddr + E820Entry.Length;\r | |
557dede8 | 292 | if (MaxAddress && (Candidate > *MaxAddress)) {\r |
1fceaddb LE |
293 | *MaxAddress = Candidate;\r |
294 | DEBUG ((\r | |
295 | DEBUG_VERBOSE,\r | |
296 | "%a: MaxAddress=0x%Lx\n",\r | |
297 | __FUNCTION__,\r | |
298 | *MaxAddress\r | |
299 | ));\r | |
300 | }\r | |
557dede8 GH |
301 | \r |
302 | if (LowMemory && (Candidate > *LowMemory) && (Candidate < BASE_4GB)) {\r | |
303 | *LowMemory = Candidate;\r | |
304 | DEBUG ((\r | |
305 | DEBUG_VERBOSE,\r | |
306 | "%a: LowMemory=0x%Lx\n",\r | |
307 | __FUNCTION__,\r | |
308 | *LowMemory\r | |
309 | ));\r | |
310 | }\r | |
1fceaddb LE |
311 | }\r |
312 | }\r | |
313 | }\r | |
ac0a286f | 314 | \r |
1fceaddb LE |
315 | return EFI_SUCCESS;\r |
316 | }\r | |
317 | \r | |
4b455f7b | 318 | UINT32\r |
c0e10976 | 319 | GetSystemMemorySizeBelow4gb (\r |
4b455f7b | 320 | VOID\r |
49ba9447 | 321 | )\r |
322 | {\r | |
759e3c6d GH |
323 | EFI_STATUS Status;\r |
324 | UINT64 LowerMemorySize = 0;\r | |
e6ea1464 AB |
325 | UINT8 Cmos0x34;\r |
326 | UINT8 Cmos0x35;\r | |
759e3c6d GH |
327 | \r |
328 | Status = ScanOrAdd64BitE820Ram (FALSE, &LowerMemorySize, NULL);\r | |
e6ea1464 AB |
329 | if ((Status == EFI_SUCCESS) && (LowerMemorySize > 0)) {\r |
330 | return (UINT32)LowerMemorySize;\r | |
331 | }\r | |
332 | \r | |
333 | //\r | |
334 | // CMOS 0x34/0x35 specifies the system memory above 16 MB.\r | |
335 | // * CMOS(0x35) is the high byte\r | |
336 | // * CMOS(0x34) is the low byte\r | |
337 | // * The size is specified in 64kb chunks\r | |
338 | // * Since this is memory above 16MB, the 16MB must be added\r | |
339 | // into the calculation to get the total memory size.\r | |
340 | //\r | |
341 | \r | |
342 | Cmos0x34 = (UINT8)CmosRead8 (0x34);\r | |
343 | Cmos0x35 = (UINT8)CmosRead8 (0x35);\r | |
344 | \r | |
345 | return (UINT32)(((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r | |
346 | }\r | |
347 | \r | |
348 | STATIC\r | |
349 | UINT64\r | |
350 | GetSystemMemorySizeAbove4gb (\r | |
351 | )\r | |
352 | {\r | |
353 | UINT32 Size;\r | |
354 | UINTN CmosIndex;\r | |
355 | \r | |
356 | //\r | |
357 | // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.\r | |
358 | // * CMOS(0x5d) is the most significant size byte\r | |
359 | // * CMOS(0x5c) is the middle size byte\r | |
360 | // * CMOS(0x5b) is the least significant size byte\r | |
361 | // * The size is specified in 64kb chunks\r | |
362 | //\r | |
363 | \r | |
364 | Size = 0;\r | |
365 | for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {\r | |
366 | Size = (UINT32)(Size << 8) + (UINT32)CmosRead8 (CmosIndex);\r | |
367 | }\r | |
368 | \r | |
369 | return LShiftU64 (Size, 16);\r | |
c0e10976 | 370 | }\r |
371 | \r | |
d5371680 LE |
372 | /**\r |
373 | Return the highest address that DXE could possibly use, plus one.\r | |
374 | **/\r | |
375 | STATIC\r | |
376 | UINT64\r | |
377 | GetFirstNonAddress (\r | |
378 | VOID\r | |
379 | )\r | |
380 | {\r | |
ac0a286f MK |
381 | UINT64 FirstNonAddress;\r |
382 | UINT64 Pci64Base, Pci64Size;\r | |
383 | UINT32 FwCfgPciMmio64Mb;\r | |
384 | EFI_STATUS Status;\r | |
385 | FIRMWARE_CONFIG_ITEM FwCfgItem;\r | |
386 | UINTN FwCfgSize;\r | |
387 | UINT64 HotPlugMemoryEnd;\r | |
388 | RETURN_STATUS PcdStatus;\r | |
d5371680 | 389 | \r |
1fceaddb LE |
390 | //\r |
391 | // set FirstNonAddress to suppress incorrect compiler/analyzer warnings\r | |
392 | //\r | |
393 | FirstNonAddress = 0;\r | |
394 | \r | |
395 | //\r | |
396 | // If QEMU presents an E820 map, then get the highest exclusive >=4GB RAM\r | |
397 | // address from it. This can express an address >= 4GB+1TB.\r | |
398 | //\r | |
e6ea1464 AB |
399 | // Otherwise, get the flat size of the memory above 4GB from the CMOS (which\r |
400 | // can only express a size smaller than 1TB), and add it to 4GB.\r | |
401 | //\r | |
557dede8 | 402 | Status = ScanOrAdd64BitE820Ram (FALSE, NULL, &FirstNonAddress);\r |
1fceaddb | 403 | if (EFI_ERROR (Status)) {\r |
e6ea1464 | 404 | FirstNonAddress = BASE_4GB + GetSystemMemorySizeAbove4gb ();\r |
1fceaddb | 405 | }\r |
7e5b1b67 LE |
406 | \r |
407 | //\r | |
408 | // If DXE is 32-bit, then we're done; PciBusDxe will degrade 64-bit MMIO\r | |
409 | // resources to 32-bit anyway. See DegradeResource() in\r | |
410 | // "PciResourceSupport.c".\r | |
411 | //\r | |
ac0a286f | 412 | #ifdef MDE_CPU_IA32\r |
7e5b1b67 LE |
413 | if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r |
414 | return FirstNonAddress;\r | |
415 | }\r | |
ac0a286f MK |
416 | \r |
417 | #endif\r | |
7e5b1b67 LE |
418 | \r |
419 | //\r | |
420 | // Otherwise, in order to calculate the highest address plus one, we must\r | |
421 | // consider the 64-bit PCI host aperture too. Fetch the default size.\r | |
422 | //\r | |
423 | Pci64Size = PcdGet64 (PcdPciMmio64Size);\r | |
424 | \r | |
425 | //\r | |
426 | // See if the user specified the number of megabytes for the 64-bit PCI host\r | |
98800cce | 427 | // aperture. Accept an aperture size up to 16TB.\r |
7e5b1b67 LE |
428 | //\r |
429 | // As signaled by the "X-" prefix, this knob is experimental, and might go\r | |
430 | // away at any time.\r | |
431 | //\r | |
ac0a286f MK |
432 | Status = QemuFwCfgParseUint32 (\r |
433 | "opt/ovmf/X-PciMmio64Mb",\r | |
434 | FALSE,\r | |
435 | &FwCfgPciMmio64Mb\r | |
436 | );\r | |
98800cce | 437 | switch (Status) {\r |
ac0a286f MK |
438 | case EFI_UNSUPPORTED:\r |
439 | case EFI_NOT_FOUND:\r | |
98800cce | 440 | break;\r |
ac0a286f MK |
441 | case EFI_SUCCESS:\r |
442 | if (FwCfgPciMmio64Mb <= 0x1000000) {\r | |
443 | Pci64Size = LShiftU64 (FwCfgPciMmio64Mb, 20);\r | |
444 | break;\r | |
445 | }\r | |
446 | \r | |
98800cce LE |
447 | //\r |
448 | // fall through\r | |
449 | //\r | |
ac0a286f MK |
450 | default:\r |
451 | DEBUG ((\r | |
452 | DEBUG_WARN,\r | |
453 | "%a: ignoring malformed 64-bit PCI host aperture size from fw_cfg\n",\r | |
454 | __FUNCTION__\r | |
455 | ));\r | |
456 | break;\r | |
7e5b1b67 LE |
457 | }\r |
458 | \r | |
459 | if (Pci64Size == 0) {\r | |
460 | if (mBootMode != BOOT_ON_S3_RESUME) {\r | |
ac0a286f MK |
461 | DEBUG ((\r |
462 | DEBUG_INFO,\r | |
463 | "%a: disabling 64-bit PCI host aperture\n",\r | |
464 | __FUNCTION__\r | |
465 | ));\r | |
32e083c7 LE |
466 | PcdStatus = PcdSet64S (PcdPciMmio64Size, 0);\r |
467 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
7e5b1b67 LE |
468 | }\r |
469 | \r | |
470 | //\r | |
471 | // There's nothing more to do; the amount of memory above 4GB fully\r | |
472 | // determines the highest address plus one. The memory hotplug area (see\r | |
473 | // below) plays no role for the firmware in this case.\r | |
474 | //\r | |
475 | return FirstNonAddress;\r | |
476 | }\r | |
477 | \r | |
478 | //\r | |
479 | // The "etc/reserved-memory-end" fw_cfg file, when present, contains an\r | |
480 | // absolute, exclusive end address for the memory hotplug area. This area\r | |
481 | // starts right at the end of the memory above 4GB. The 64-bit PCI host\r | |
482 | // aperture must be placed above it.\r | |
483 | //\r | |
ac0a286f MK |
484 | Status = QemuFwCfgFindFile (\r |
485 | "etc/reserved-memory-end",\r | |
486 | &FwCfgItem,\r | |
487 | &FwCfgSize\r | |
488 | );\r | |
489 | if (!EFI_ERROR (Status) && (FwCfgSize == sizeof HotPlugMemoryEnd)) {\r | |
7e5b1b67 LE |
490 | QemuFwCfgSelectItem (FwCfgItem);\r |
491 | QemuFwCfgReadBytes (FwCfgSize, &HotPlugMemoryEnd);\r | |
ac0a286f MK |
492 | DEBUG ((\r |
493 | DEBUG_VERBOSE,\r | |
494 | "%a: HotPlugMemoryEnd=0x%Lx\n",\r | |
495 | __FUNCTION__,\r | |
496 | HotPlugMemoryEnd\r | |
497 | ));\r | |
7e5b1b67 LE |
498 | \r |
499 | ASSERT (HotPlugMemoryEnd >= FirstNonAddress);\r | |
500 | FirstNonAddress = HotPlugMemoryEnd;\r | |
501 | }\r | |
502 | \r | |
503 | //\r | |
504 | // SeaBIOS aligns both boundaries of the 64-bit PCI host aperture to 1GB, so\r | |
505 | // that the host can map it with 1GB hugepages. Follow suit.\r | |
506 | //\r | |
507 | Pci64Base = ALIGN_VALUE (FirstNonAddress, (UINT64)SIZE_1GB);\r | |
508 | Pci64Size = ALIGN_VALUE (Pci64Size, (UINT64)SIZE_1GB);\r | |
509 | \r | |
510 | //\r | |
511 | // The 64-bit PCI host aperture should also be "naturally" aligned. The\r | |
512 | // alignment is determined by rounding the size of the aperture down to the\r | |
513 | // next smaller or equal power of two. That is, align the aperture by the\r | |
514 | // largest BAR size that can fit into it.\r | |
515 | //\r | |
516 | Pci64Base = ALIGN_VALUE (Pci64Base, GetPowerOfTwo64 (Pci64Size));\r | |
517 | \r | |
518 | if (mBootMode != BOOT_ON_S3_RESUME) {\r | |
519 | //\r | |
520 | // The core PciHostBridgeDxe driver will automatically add this range to\r | |
521 | // the GCD memory space map through our PciHostBridgeLib instance; here we\r | |
522 | // only need to set the PCDs.\r | |
523 | //\r | |
32e083c7 LE |
524 | PcdStatus = PcdSet64S (PcdPciMmio64Base, Pci64Base);\r |
525 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
526 | PcdStatus = PcdSet64S (PcdPciMmio64Size, Pci64Size);\r | |
527 | ASSERT_RETURN_ERROR (PcdStatus);\r | |
528 | \r | |
ac0a286f MK |
529 | DEBUG ((\r |
530 | DEBUG_INFO,\r | |
531 | "%a: Pci64Base=0x%Lx Pci64Size=0x%Lx\n",\r | |
532 | __FUNCTION__,\r | |
533 | Pci64Base,\r | |
534 | Pci64Size\r | |
535 | ));\r | |
7e5b1b67 LE |
536 | }\r |
537 | \r | |
538 | //\r | |
539 | // The useful address space ends with the 64-bit PCI host aperture.\r | |
540 | //\r | |
541 | FirstNonAddress = Pci64Base + Pci64Size;\r | |
d5371680 LE |
542 | return FirstNonAddress;\r |
543 | }\r | |
544 | \r | |
bc89fe48 LE |
545 | /**\r |
546 | Initialize the mPhysMemAddressWidth variable, based on guest RAM size.\r | |
547 | **/\r | |
548 | VOID\r | |
549 | AddressWidthInitialization (\r | |
550 | VOID\r | |
551 | )\r | |
552 | {\r | |
ac0a286f | 553 | UINT64 FirstNonAddress;\r |
bc89fe48 LE |
554 | \r |
555 | //\r | |
556 | // As guest-physical memory size grows, the permanent PEI RAM requirements\r | |
557 | // are dominated by the identity-mapping page tables built by the DXE IPL.\r | |
558 | // The DXL IPL keys off of the physical address bits advertized in the CPU\r | |
559 | // HOB. To conserve memory, we calculate the minimum address width here.\r | |
560 | //\r | |
d5371680 | 561 | FirstNonAddress = GetFirstNonAddress ();\r |
bc89fe48 LE |
562 | mPhysMemAddressWidth = (UINT8)HighBitSet64 (FirstNonAddress);\r |
563 | \r | |
564 | //\r | |
565 | // If FirstNonAddress is not an integral power of two, then we need an\r | |
566 | // additional bit.\r | |
567 | //\r | |
568 | if ((FirstNonAddress & (FirstNonAddress - 1)) != 0) {\r | |
569 | ++mPhysMemAddressWidth;\r | |
570 | }\r | |
571 | \r | |
572 | //\r | |
573 | // The minimum address width is 36 (covers up to and excluding 64 GB, which\r | |
574 | // is the maximum for Ia32 + PAE). The theoretical architecture maximum for\r | |
575 | // X64 long mode is 52 bits, but the DXE IPL clamps that down to 48 bits. We\r | |
576 | // can simply assert that here, since 48 bits are good enough for 256 TB.\r | |
577 | //\r | |
578 | if (mPhysMemAddressWidth <= 36) {\r | |
579 | mPhysMemAddressWidth = 36;\r | |
580 | }\r | |
ac0a286f | 581 | \r |
bc89fe48 LE |
582 | ASSERT (mPhysMemAddressWidth <= 48);\r |
583 | }\r | |
584 | \r | |
bc89fe48 LE |
585 | /**\r |
586 | Calculate the cap for the permanent PEI memory.\r | |
587 | **/\r | |
588 | STATIC\r | |
589 | UINT32\r | |
590 | GetPeiMemoryCap (\r | |
591 | VOID\r | |
592 | )\r | |
593 | {\r | |
ac0a286f MK |
594 | BOOLEAN Page1GSupport;\r |
595 | UINT32 RegEax;\r | |
596 | UINT32 RegEdx;\r | |
597 | UINT32 Pml4Entries;\r | |
598 | UINT32 PdpEntries;\r | |
599 | UINTN TotalPages;\r | |
bc89fe48 LE |
600 | \r |
601 | //\r | |
602 | // If DXE is 32-bit, then just return the traditional 64 MB cap.\r | |
603 | //\r | |
ac0a286f | 604 | #ifdef MDE_CPU_IA32\r |
bc89fe48 LE |
605 | if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r |
606 | return SIZE_64MB;\r | |
607 | }\r | |
ac0a286f MK |
608 | \r |
609 | #endif\r | |
bc89fe48 LE |
610 | \r |
611 | //\r | |
612 | // Dependent on physical address width, PEI memory allocations can be\r | |
613 | // dominated by the page tables built for 64-bit DXE. So we key the cap off\r | |
614 | // of those. The code below is based on CreateIdentityMappingPageTables() in\r | |
615 | // "MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c".\r | |
616 | //\r | |
617 | Page1GSupport = FALSE;\r | |
618 | if (PcdGetBool (PcdUse1GPageTable)) {\r | |
619 | AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r | |
620 | if (RegEax >= 0x80000001) {\r | |
621 | AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r | |
622 | if ((RegEdx & BIT26) != 0) {\r | |
623 | Page1GSupport = TRUE;\r | |
624 | }\r | |
625 | }\r | |
626 | }\r | |
627 | \r | |
628 | if (mPhysMemAddressWidth <= 39) {\r | |
629 | Pml4Entries = 1;\r | |
ac0a286f | 630 | PdpEntries = 1 << (mPhysMemAddressWidth - 30);\r |
bc89fe48 LE |
631 | ASSERT (PdpEntries <= 0x200);\r |
632 | } else {\r | |
633 | Pml4Entries = 1 << (mPhysMemAddressWidth - 39);\r | |
634 | ASSERT (Pml4Entries <= 0x200);\r | |
635 | PdpEntries = 512;\r | |
636 | }\r | |
637 | \r | |
638 | TotalPages = Page1GSupport ? Pml4Entries + 1 :\r | |
ac0a286f | 639 | (PdpEntries + 1) * Pml4Entries + 1;\r |
bc89fe48 LE |
640 | ASSERT (TotalPages <= 0x40201);\r |
641 | \r | |
642 | //\r | |
643 | // Add 64 MB for miscellaneous allocations. Note that for\r | |
644 | // mPhysMemAddressWidth values close to 36, the cap will actually be\r | |
645 | // dominated by this increment.\r | |
646 | //\r | |
647 | return (UINT32)(EFI_PAGES_TO_SIZE (TotalPages) + SIZE_64MB);\r | |
648 | }\r | |
649 | \r | |
36658fff WL |
650 | /**\r |
651 | Publish PEI core memory\r | |
652 | \r | |
653 | @return EFI_SUCCESS The PEIM initialized successfully.\r | |
654 | \r | |
655 | **/\r | |
656 | EFI_STATUS\r | |
657 | PublishPeiMemory (\r | |
658 | VOID\r | |
659 | )\r | |
660 | {\r | |
ac0a286f MK |
661 | EFI_STATUS Status;\r |
662 | EFI_PHYSICAL_ADDRESS MemoryBase;\r | |
663 | UINT64 MemorySize;\r | |
664 | UINT32 LowerMemorySize;\r | |
665 | UINT32 PeiMemoryCap;\r | |
36658fff | 666 | \r |
45d87081 LE |
667 | LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r |
668 | if (FeaturePcdGet (PcdSmmSmramRequire)) {\r | |
669 | //\r | |
670 | // TSEG is chipped from the end of low RAM\r | |
671 | //\r | |
23bfb5c0 | 672 | LowerMemorySize -= mQ35TsegMbytes * SIZE_1MB;\r |
45d87081 LE |
673 | }\r |
674 | \r | |
675 | //\r | |
676 | // If S3 is supported, then the S3 permanent PEI memory is placed next,\r | |
677 | // downwards. Its size is primarily dictated by CpuMpPei. The formula below\r | |
678 | // is an approximation.\r | |
679 | //\r | |
680 | if (mS3Supported) {\r | |
681 | mS3AcpiReservedMemorySize = SIZE_512KB +\r | |
ac0a286f MK |
682 | mMaxCpuCount *\r |
683 | PcdGet32 (PcdCpuApStackSize);\r | |
45d87081 | 684 | mS3AcpiReservedMemoryBase = LowerMemorySize - mS3AcpiReservedMemorySize;\r |
ac0a286f | 685 | LowerMemorySize = mS3AcpiReservedMemoryBase;\r |
45d87081 LE |
686 | }\r |
687 | \r | |
8e54500f | 688 | if (mBootMode == BOOT_ON_S3_RESUME) {\r |
45d87081 LE |
689 | MemoryBase = mS3AcpiReservedMemoryBase;\r |
690 | MemorySize = mS3AcpiReservedMemorySize;\r | |
8e54500f | 691 | } else {\r |
bc89fe48 | 692 | PeiMemoryCap = GetPeiMemoryCap ();\r |
ac0a286f MK |
693 | DEBUG ((\r |
694 | DEBUG_INFO,\r | |
695 | "%a: mPhysMemAddressWidth=%d PeiMemoryCap=%u KB\n",\r | |
696 | __FUNCTION__,\r | |
697 | mPhysMemAddressWidth,\r | |
698 | PeiMemoryCap >> 10\r | |
699 | ));\r | |
bc89fe48 | 700 | \r |
8e54500f JJ |
701 | //\r |
702 | // Determine the range of memory to use during PEI\r | |
703 | //\r | |
efb0f16e LE |
704 | // Technically we could lay the permanent PEI RAM over SEC's temporary\r |
705 | // decompression and scratch buffer even if "secure S3" is needed, since\r | |
706 | // their lifetimes don't overlap. However, PeiFvInitialization() will cover\r | |
707 | // RAM up to PcdOvmfDecompressionScratchEnd with an EfiACPIMemoryNVS memory\r | |
708 | // allocation HOB, and other allocations served from the permanent PEI RAM\r | |
709 | // shouldn't overlap with that HOB.\r | |
710 | //\r | |
711 | MemoryBase = mS3Supported && FeaturePcdGet (PcdSmmSmramRequire) ?\r | |
ac0a286f MK |
712 | PcdGet32 (PcdOvmfDecompressionScratchEnd) :\r |
713 | PcdGet32 (PcdOvmfDxeMemFvBase) + PcdGet32 (PcdOvmfDxeMemFvSize);\r | |
8e54500f | 714 | MemorySize = LowerMemorySize - MemoryBase;\r |
bc89fe48 LE |
715 | if (MemorySize > PeiMemoryCap) {\r |
716 | MemoryBase = LowerMemorySize - PeiMemoryCap;\r | |
717 | MemorySize = PeiMemoryCap;\r | |
8e54500f | 718 | }\r |
36658fff WL |
719 | }\r |
720 | \r | |
adec2bd5 LE |
721 | //\r |
722 | // MEMFD_BASE_ADDRESS separates the SMRAM at the default SMBASE from the\r | |
723 | // normal boot permanent PEI RAM. Regarding the S3 boot path, the S3\r | |
724 | // permanent PEI RAM is located even higher.\r | |
725 | //\r | |
726 | if (FeaturePcdGet (PcdSmmSmramRequire) && mQ35SmramAtDefaultSmbase) {\r | |
727 | ASSERT (SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE <= MemoryBase);\r | |
728 | }\r | |
729 | \r | |
36658fff WL |
730 | //\r |
731 | // Publish this memory to the PEI Core\r | |
732 | //\r | |
ac0a286f | 733 | Status = PublishSystemMemory (MemoryBase, MemorySize);\r |
36658fff WL |
734 | ASSERT_EFI_ERROR (Status);\r |
735 | \r | |
736 | return Status;\r | |
737 | }\r | |
738 | \r | |
84b223c1 LE |
739 | STATIC\r |
740 | VOID\r | |
741 | QemuInitializeRamBelow1gb (\r | |
742 | VOID\r | |
743 | )\r | |
744 | {\r | |
745 | if (FeaturePcdGet (PcdSmmSmramRequire) && mQ35SmramAtDefaultSmbase) {\r | |
746 | AddMemoryRangeHob (0, SMM_DEFAULT_SMBASE);\r | |
ac0a286f MK |
747 | AddReservedMemoryBaseSizeHob (\r |
748 | SMM_DEFAULT_SMBASE,\r | |
749 | MCH_DEFAULT_SMBASE_SIZE,\r | |
750 | TRUE /* Cacheable */\r | |
751 | );\r | |
84b223c1 LE |
752 | STATIC_ASSERT (\r |
753 | SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE < BASE_512KB + BASE_128KB,\r | |
754 | "end of SMRAM at default SMBASE ends at, or exceeds, 640KB"\r | |
755 | );\r | |
ac0a286f MK |
756 | AddMemoryRangeHob (\r |
757 | SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE,\r | |
758 | BASE_512KB + BASE_128KB\r | |
759 | );\r | |
84b223c1 LE |
760 | } else {\r |
761 | AddMemoryRangeHob (0, BASE_512KB + BASE_128KB);\r | |
762 | }\r | |
763 | }\r | |
764 | \r | |
49ba9447 | 765 | /**\r |
c034906e | 766 | Peform Memory Detection for QEMU / KVM\r |
49ba9447 | 767 | \r |
768 | **/\r | |
c034906e JJ |
769 | STATIC\r |
770 | VOID\r | |
771 | QemuInitializeRam (\r | |
772 | VOID\r | |
49ba9447 | 773 | )\r |
774 | {\r | |
ac0a286f | 775 | UINT64 LowerMemorySize;\r |
e6ea1464 | 776 | UINT64 UpperMemorySize;\r |
ac0a286f MK |
777 | MTRR_SETTINGS MtrrSettings;\r |
778 | EFI_STATUS Status;\r | |
49ba9447 | 779 | \r |
70d5086c | 780 | DEBUG ((DEBUG_INFO, "%a called\n", __FUNCTION__));\r |
49ba9447 | 781 | \r |
782 | //\r | |
783 | // Determine total memory size available\r | |
784 | //\r | |
c0e10976 | 785 | LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r |
49ba9447 | 786 | \r |
e3e3090a LE |
787 | if (mBootMode == BOOT_ON_S3_RESUME) {\r |
788 | //\r | |
789 | // Create the following memory HOB as an exception on the S3 boot path.\r | |
790 | //\r | |
791 | // Normally we'd create memory HOBs only on the normal boot path. However,\r | |
792 | // CpuMpPei specifically needs such a low-memory HOB on the S3 path as\r | |
793 | // well, for "borrowing" a subset of it temporarily, for the AP startup\r | |
794 | // vector.\r | |
795 | //\r | |
796 | // CpuMpPei saves the original contents of the borrowed area in permanent\r | |
797 | // PEI RAM, in a backup buffer allocated with the normal PEI services.\r | |
798 | // CpuMpPei restores the original contents ("returns" the borrowed area) at\r | |
799 | // End-of-PEI. End-of-PEI in turn is emitted by S3Resume2Pei before\r | |
8c0b0b34 | 800 | // transferring control to the OS's wakeup vector in the FACS.\r |
e3e3090a LE |
801 | //\r |
802 | // We expect any other PEIMs that "borrow" memory similarly to CpuMpPei to\r | |
803 | // restore the original contents. Furthermore, we expect all such PEIMs\r | |
804 | // (CpuMpPei included) to claim the borrowed areas by producing memory\r | |
805 | // allocation HOBs, and to honor preexistent memory allocation HOBs when\r | |
806 | // looking for an area to borrow.\r | |
807 | //\r | |
84b223c1 | 808 | QemuInitializeRamBelow1gb ();\r |
e3e3090a | 809 | } else {\r |
bd386eaf JJ |
810 | //\r |
811 | // Create memory HOBs\r | |
812 | //\r | |
84b223c1 | 813 | QemuInitializeRamBelow1gb ();\r |
b09c1c6f LE |
814 | \r |
815 | if (FeaturePcdGet (PcdSmmSmramRequire)) {\r | |
ac0a286f | 816 | UINT32 TsegSize;\r |
b09c1c6f | 817 | \r |
23bfb5c0 | 818 | TsegSize = mQ35TsegMbytes * SIZE_1MB;\r |
b09c1c6f | 819 | AddMemoryRangeHob (BASE_1MB, LowerMemorySize - TsegSize);\r |
ac0a286f MK |
820 | AddReservedMemoryBaseSizeHob (\r |
821 | LowerMemorySize - TsegSize,\r | |
822 | TsegSize,\r | |
823 | TRUE\r | |
824 | );\r | |
b09c1c6f LE |
825 | } else {\r |
826 | AddMemoryRangeHob (BASE_1MB, LowerMemorySize);\r | |
827 | }\r | |
828 | \r | |
1fceaddb LE |
829 | //\r |
830 | // If QEMU presents an E820 map, then create memory HOBs for the >=4GB RAM\r | |
831 | // entries. Otherwise, create a single memory HOB with the flat >=4GB\r | |
832 | // memory size read from the CMOS.\r | |
833 | //\r | |
557dede8 | 834 | Status = ScanOrAdd64BitE820Ram (TRUE, NULL, NULL);\r |
e6ea1464 AB |
835 | if (EFI_ERROR (Status)) {\r |
836 | UpperMemorySize = GetSystemMemorySizeAbove4gb ();\r | |
837 | if (UpperMemorySize != 0) {\r | |
838 | AddMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);\r | |
839 | }\r | |
840 | }\r | |
bd386eaf | 841 | }\r |
49ba9447 | 842 | \r |
79d274b8 LE |
843 | //\r |
844 | // We'd like to keep the following ranges uncached:\r | |
845 | // - [640 KB, 1 MB)\r | |
846 | // - [LowerMemorySize, 4 GB)\r | |
847 | //\r | |
848 | // Everything else should be WB. Unfortunately, programming the inverse (ie.\r | |
849 | // keeping the default UC, and configuring the complement set of the above as\r | |
850 | // WB) is not reliable in general, because the end of the upper RAM can have\r | |
851 | // practically any alignment, and we may not have enough variable MTRRs to\r | |
852 | // cover it exactly.\r | |
853 | //\r | |
9afcd48a | 854 | if (IsMtrrSupported () && (mHostBridgeDevId != CLOUDHV_DEVICE_ID)) {\r |
79d274b8 LE |
855 | MtrrGetAllMtrrs (&MtrrSettings);\r |
856 | \r | |
857 | //\r | |
858 | // MTRRs disabled, fixed MTRRs disabled, default type is uncached\r | |
859 | //\r | |
860 | ASSERT ((MtrrSettings.MtrrDefType & BIT11) == 0);\r | |
861 | ASSERT ((MtrrSettings.MtrrDefType & BIT10) == 0);\r | |
862 | ASSERT ((MtrrSettings.MtrrDefType & 0xFF) == 0);\r | |
863 | \r | |
864 | //\r | |
865 | // flip default type to writeback\r | |
866 | //\r | |
867 | SetMem (&MtrrSettings.Fixed, sizeof MtrrSettings.Fixed, 0x06);\r | |
868 | ZeroMem (&MtrrSettings.Variables, sizeof MtrrSettings.Variables);\r | |
869 | MtrrSettings.MtrrDefType |= BIT11 | BIT10 | 6;\r | |
870 | MtrrSetAllMtrrs (&MtrrSettings);\r | |
e8e5cd4a | 871 | \r |
79d274b8 LE |
872 | //\r |
873 | // Set memory range from 640KB to 1MB to uncacheable\r | |
874 | //\r | |
ac0a286f MK |
875 | Status = MtrrSetMemoryAttribute (\r |
876 | BASE_512KB + BASE_128KB,\r | |
877 | BASE_1MB - (BASE_512KB + BASE_128KB),\r | |
878 | CacheUncacheable\r | |
879 | );\r | |
79d274b8 | 880 | ASSERT_EFI_ERROR (Status);\r |
e8e5cd4a | 881 | \r |
79d274b8 | 882 | //\r |
49edde15 LE |
883 | // Set the memory range from the start of the 32-bit MMIO area (32-bit PCI\r |
884 | // MMIO aperture on i440fx, PCIEXBAR on q35) to 4GB as uncacheable.\r | |
79d274b8 | 885 | //\r |
ac0a286f MK |
886 | Status = MtrrSetMemoryAttribute (\r |
887 | mQemuUc32Base,\r | |
888 | SIZE_4GB - mQemuUc32Base,\r | |
889 | CacheUncacheable\r | |
890 | );\r | |
79d274b8 | 891 | ASSERT_EFI_ERROR (Status);\r |
c0e10976 | 892 | }\r |
49ba9447 | 893 | }\r |
894 | \r | |
c034906e JJ |
895 | /**\r |
896 | Publish system RAM and reserve memory regions\r | |
897 | \r | |
898 | **/\r | |
899 | VOID\r | |
900 | InitializeRamRegions (\r | |
901 | VOID\r | |
902 | )\r | |
903 | {\r | |
d06eb2d1 | 904 | QemuInitializeRam ();\r |
8e54500f | 905 | \r |
ea3a12d9 BS |
906 | SevInitializeRam ();\r |
907 | \r | |
ac0a286f | 908 | if (mS3Supported && (mBootMode != BOOT_ON_S3_RESUME)) {\r |
8e54500f JJ |
909 | //\r |
910 | // This is the memory range that will be used for PEI on S3 resume\r | |
911 | //\r | |
912 | BuildMemoryAllocationHob (\r | |
45d87081 LE |
913 | mS3AcpiReservedMemoryBase,\r |
914 | mS3AcpiReservedMemorySize,\r | |
8e54500f JJ |
915 | EfiACPIMemoryNVS\r |
916 | );\r | |
e249f906 LE |
917 | \r |
918 | //\r | |
919 | // Cover the initial RAM area used as stack and temporary PEI heap.\r | |
920 | //\r | |
921 | // This is reserved as ACPI NVS so it can be used on S3 resume.\r | |
922 | //\r | |
923 | BuildMemoryAllocationHob (\r | |
924 | PcdGet32 (PcdOvmfSecPeiTempRamBase),\r | |
925 | PcdGet32 (PcdOvmfSecPeiTempRamSize),\r | |
926 | EfiACPIMemoryNVS\r | |
927 | );\r | |
78a38b73 | 928 | \r |
ad43bc6b LE |
929 | //\r |
930 | // SEC stores its table of GUIDed section handlers here.\r | |
931 | //\r | |
932 | BuildMemoryAllocationHob (\r | |
933 | PcdGet64 (PcdGuidedExtractHandlerTableAddress),\r | |
934 | PcdGet32 (PcdGuidedExtractHandlerTableSize),\r | |
935 | EfiACPIMemoryNVS\r | |
936 | );\r | |
937 | \r | |
ac0a286f | 938 | #ifdef MDE_CPU_X64\r |
78a38b73 LE |
939 | //\r |
940 | // Reserve the initial page tables built by the reset vector code.\r | |
941 | //\r | |
942 | // Since this memory range will be used by the Reset Vector on S3\r | |
943 | // resume, it must be reserved as ACPI NVS.\r | |
944 | //\r | |
945 | BuildMemoryAllocationHob (\r | |
ac0a286f MK |
946 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecPageTablesBase),\r |
947 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecPageTablesSize),\r | |
78a38b73 LE |
948 | EfiACPIMemoryNVS\r |
949 | );\r | |
0bbed066 TL |
950 | \r |
951 | if (MemEncryptSevEsIsEnabled ()) {\r | |
952 | //\r | |
953 | // If SEV-ES is enabled, reserve the GHCB-related memory area. This\r | |
954 | // includes the extra page table used to break down the 2MB page\r | |
955 | // mapping into 4KB page entries where the GHCB resides and the\r | |
956 | // GHCB area itself.\r | |
957 | //\r | |
958 | // Since this memory range will be used by the Reset Vector on S3\r | |
959 | // resume, it must be reserved as ACPI NVS.\r | |
960 | //\r | |
961 | BuildMemoryAllocationHob (\r | |
ac0a286f MK |
962 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbPageTableBase),\r |
963 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbPageTableSize),\r | |
0bbed066 TL |
964 | EfiACPIMemoryNVS\r |
965 | );\r | |
966 | BuildMemoryAllocationHob (\r | |
ac0a286f MK |
967 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbBase),\r |
968 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbSize),\r | |
0bbed066 TL |
969 | EfiACPIMemoryNVS\r |
970 | );\r | |
36265424 | 971 | BuildMemoryAllocationHob (\r |
ac0a286f MK |
972 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupBase),\r |
973 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupSize),\r | |
36265424 TL |
974 | EfiACPIMemoryNVS\r |
975 | );\r | |
0bbed066 | 976 | }\r |
ac0a286f MK |
977 | \r |
978 | #endif\r | |
0e8a31f5 | 979 | }\r |
6a7cba79 | 980 | \r |
0e8a31f5 | 981 | if (mBootMode != BOOT_ON_S3_RESUME) {\r |
1a7edbbc LE |
982 | if (!FeaturePcdGet (PcdSmmSmramRequire)) {\r |
983 | //\r | |
984 | // Reserve the lock box storage area\r | |
985 | //\r | |
986 | // Since this memory range will be used on S3 resume, it must be\r | |
987 | // reserved as ACPI NVS.\r | |
988 | //\r | |
989 | // If S3 is unsupported, then various drivers might still write to the\r | |
990 | // LockBox area. We ought to prevent DXE from serving allocation requests\r | |
991 | // such that they would overlap the LockBox storage.\r | |
992 | //\r | |
993 | ZeroMem (\r | |
ac0a286f MK |
994 | (VOID *)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase),\r |
995 | (UINTN)PcdGet32 (PcdOvmfLockBoxStorageSize)\r | |
1a7edbbc LE |
996 | );\r |
997 | BuildMemoryAllocationHob (\r | |
ac0a286f MK |
998 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase),\r |
999 | (UINT64)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageSize),\r | |
1a7edbbc LE |
1000 | mS3Supported ? EfiACPIMemoryNVS : EfiBootServicesData\r |
1001 | );\r | |
1002 | }\r | |
b09c1c6f LE |
1003 | \r |
1004 | if (FeaturePcdGet (PcdSmmSmramRequire)) {\r | |
ac0a286f | 1005 | UINT32 TsegSize;\r |
b09c1c6f LE |
1006 | \r |
1007 | //\r | |
1008 | // Make sure the TSEG area that we reported as a reserved memory resource\r | |
1009 | // cannot be used for reserved memory allocations.\r | |
1010 | //\r | |
23bfb5c0 | 1011 | TsegSize = mQ35TsegMbytes * SIZE_1MB;\r |
b09c1c6f | 1012 | BuildMemoryAllocationHob (\r |
ac0a286f | 1013 | GetSystemMemorySizeBelow4gb () - TsegSize,\r |
b09c1c6f LE |
1014 | TsegSize,\r |
1015 | EfiReservedMemoryType\r | |
1016 | );\r | |
84b223c1 LE |
1017 | //\r |
1018 | // Similarly, allocate away the (already reserved) SMRAM at the default\r | |
1019 | // SMBASE, if it exists.\r | |
1020 | //\r | |
1021 | if (mQ35SmramAtDefaultSmbase) {\r | |
1022 | BuildMemoryAllocationHob (\r | |
1023 | SMM_DEFAULT_SMBASE,\r | |
1024 | MCH_DEFAULT_SMBASE_SIZE,\r | |
1025 | EfiReservedMemoryType\r | |
1026 | );\r | |
1027 | }\r | |
b09c1c6f | 1028 | }\r |
0731236f | 1029 | \r |
ac0a286f | 1030 | #ifdef MDE_CPU_X64\r |
80e67af9 | 1031 | if (FixedPcdGet32 (PcdOvmfWorkAreaSize) != 0) {\r |
0731236f | 1032 | //\r |
80e67af9 | 1033 | // Reserve the work area.\r |
0731236f TL |
1034 | //\r |
1035 | // Since this memory range will be used by the Reset Vector on S3\r | |
1036 | // resume, it must be reserved as ACPI NVS.\r | |
1037 | //\r | |
1038 | // If S3 is unsupported, then various drivers might still write to the\r | |
1039 | // work area. We ought to prevent DXE from serving allocation requests\r | |
1040 | // such that they would overlap the work area.\r | |
1041 | //\r | |
1042 | BuildMemoryAllocationHob (\r | |
ac0a286f MK |
1043 | (EFI_PHYSICAL_ADDRESS)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaBase),\r |
1044 | (UINT64)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaSize),\r | |
0731236f TL |
1045 | mS3Supported ? EfiACPIMemoryNVS : EfiBootServicesData\r |
1046 | );\r | |
1047 | }\r | |
ac0a286f MK |
1048 | \r |
1049 | #endif\r | |
8e54500f | 1050 | }\r |
c034906e | 1051 | }\r |