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