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