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