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