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