]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/PlatformInitLib/MemDetect.c
OvmfPkg/PlatformInitLib: Add PlatformScanE820 and GetFirstNonAddressCB
[mirror_edk2.git] / OvmfPkg / Library / PlatformInitLib / MemDetect.c
CommitLineData
10460942
MX
1/**@file\r
2 Memory Detection for Virtual Machines.\r
3\r
4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
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
16#include <IndustryStandard/E820.h>\r
17#include <IndustryStandard/I440FxPiix4.h>\r
18#include <IndustryStandard/Q35MchIch9.h>\r
19#include <IndustryStandard/CloudHv.h>\r
20#include <IndustryStandard/Xen/arch-x86/hvm/start_info.h>\r
21#include <PiPei.h>\r
22#include <Register/Intel/SmramSaveStateMap.h>\r
23\r
24//\r
25// The Library classes this module consumes\r
26//\r
27#include <Library/BaseLib.h>\r
28#include <Library/BaseMemoryLib.h>\r
e03b0d06 29#include <Library/CcProbeLib.h>\r
10460942 30#include <Library/DebugLib.h>\r
3497fd5c 31#include <Library/HardwareInfoLib.h>\r
10460942
MX
32#include <Library/HobLib.h>\r
33#include <Library/IoLib.h>\r
34#include <Library/MemEncryptSevLib.h>\r
35#include <Library/PcdLib.h>\r
36#include <Library/PciLib.h>\r
37#include <Library/PeimEntryPoint.h>\r
38#include <Library/ResourcePublicationLib.h>\r
39#include <Library/MtrrLib.h>\r
40#include <Library/QemuFwCfgLib.h>\r
41#include <Library/QemuFwCfgSimpleParserLib.h>\r
e23f8f52
MX
42#include <Library/TdxLib.h>\r
43\r
10460942
MX
44#include <Library/PlatformInitLib.h>\r
45\r
d1e41c62
MX
46#define MEGABYTE_SHIFT 20\r
47\r
10460942
MX
48VOID\r
49EFIAPI\r
50PlatformQemuUc32BaseInitialization (\r
51 IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
52 )\r
53{\r
54 UINT32 LowerMemorySize;\r
55\r
56 if (PlatformInfoHob->HostBridgeDevId == 0xffff /* microvm */) {\r
57 return;\r
58 }\r
59\r
60 if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
2a0bd3bf 61 LowerMemorySize = PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r
47f44097 62 ASSERT (PcdGet64 (PcdPciExpressBaseAddress) <= MAX_UINT32);\r
2a0bd3bf
GH
63 ASSERT (PcdGet64 (PcdPciExpressBaseAddress) >= LowerMemorySize);\r
64\r
65 if (LowerMemorySize <= BASE_2GB) {\r
66 // Newer qemu with gigabyte aligned memory,\r
67 // 32-bit pci mmio window is 2G -> 4G then.\r
68 PlatformInfoHob->Uc32Base = BASE_2GB;\r
69 } else {\r
70 //\r
71 // On q35, the 32-bit area that we'll mark as UC, through variable MTRRs,\r
72 // starts at PcdPciExpressBaseAddress. The platform DSC is responsible for\r
73 // setting PcdPciExpressBaseAddress such that describing the\r
74 // [PcdPciExpressBaseAddress, 4GB) range require a very small number of\r
75 // variable MTRRs (preferably 1 or 2).\r
76 //\r
77 PlatformInfoHob->Uc32Base = (UINT32)PcdGet64 (PcdPciExpressBaseAddress);\r
78 }\r
79\r
10460942
MX
80 return;\r
81 }\r
82\r
83 if (PlatformInfoHob->HostBridgeDevId == CLOUDHV_DEVICE_ID) {\r
84 PlatformInfoHob->Uc32Size = CLOUDHV_MMIO_HOLE_SIZE;\r
85 PlatformInfoHob->Uc32Base = CLOUDHV_MMIO_HOLE_ADDRESS;\r
86 return;\r
87 }\r
88\r
89 ASSERT (PlatformInfoHob->HostBridgeDevId == INTEL_82441_DEVICE_ID);\r
90 //\r
91 // On i440fx, start with the [LowerMemorySize, 4GB) range. Make sure one\r
92 // variable MTRR suffices by truncating the size to a whole power of two,\r
93 // while keeping the end affixed to 4GB. This will round the base up.\r
94 //\r
95 LowerMemorySize = PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r
96 PlatformInfoHob->Uc32Size = GetPowerOfTwo32 ((UINT32)(SIZE_4GB - LowerMemorySize));\r
97 PlatformInfoHob->Uc32Base = (UINT32)(SIZE_4GB - PlatformInfoHob->Uc32Size);\r
98 //\r
99 // Assuming that LowerMemorySize is at least 1 byte, Uc32Size is at most 2GB.\r
100 // Therefore Uc32Base is at least 2GB.\r
101 //\r
102 ASSERT (PlatformInfoHob->Uc32Base >= BASE_2GB);\r
103\r
104 if (PlatformInfoHob->Uc32Base != LowerMemorySize) {\r
105 DEBUG ((\r
106 DEBUG_VERBOSE,\r
107 "%a: rounded UC32 base from 0x%x up to 0x%x, for "\r
108 "an UC32 size of 0x%x\n",\r
109 __FUNCTION__,\r
110 LowerMemorySize,\r
111 PlatformInfoHob->Uc32Base,\r
112 PlatformInfoHob->Uc32Size\r
113 ));\r
114 }\r
115}\r
116\r
117/**\r
118 Iterate over the RAM entries in QEMU's fw_cfg E820 RAM map that start outside\r
119 of the 32-bit address range.\r
120\r
121 Find the highest exclusive >=4GB RAM address, or produce memory resource\r
122 descriptor HOBs for RAM entries that start at or above 4GB.\r
123\r
124 @param[out] MaxAddress If MaxAddress is NULL, then PlatformScanOrAdd64BitE820Ram()\r
125 produces memory resource descriptor HOBs for RAM\r
126 entries that start at or above 4GB.\r
127\r
128 Otherwise, MaxAddress holds the highest exclusive\r
129 >=4GB RAM address on output. If QEMU's fw_cfg E820\r
130 RAM map contains no RAM entry that starts outside of\r
131 the 32-bit address range, then MaxAddress is exactly\r
132 4GB on output.\r
133\r
134 @retval EFI_SUCCESS The fw_cfg E820 RAM map was found and processed.\r
135\r
136 @retval EFI_PROTOCOL_ERROR The RAM map was found, but its size wasn't a\r
137 whole multiple of sizeof(EFI_E820_ENTRY64). No\r
138 RAM entry was processed.\r
139\r
140 @return Error codes from QemuFwCfgFindFile(). No RAM\r
141 entry was processed.\r
142**/\r
143STATIC\r
144EFI_STATUS\r
145PlatformScanOrAdd64BitE820Ram (\r
146 IN BOOLEAN AddHighHob,\r
147 OUT UINT64 *LowMemory OPTIONAL,\r
148 OUT UINT64 *MaxAddress OPTIONAL\r
149 )\r
150{\r
151 EFI_STATUS Status;\r
152 FIRMWARE_CONFIG_ITEM FwCfgItem;\r
153 UINTN FwCfgSize;\r
154 EFI_E820_ENTRY64 E820Entry;\r
155 UINTN Processed;\r
156\r
157 Status = QemuFwCfgFindFile ("etc/e820", &FwCfgItem, &FwCfgSize);\r
158 if (EFI_ERROR (Status)) {\r
159 return Status;\r
160 }\r
161\r
162 if (FwCfgSize % sizeof E820Entry != 0) {\r
163 return EFI_PROTOCOL_ERROR;\r
164 }\r
165\r
166 if (LowMemory != NULL) {\r
167 *LowMemory = 0;\r
168 }\r
169\r
170 if (MaxAddress != NULL) {\r
171 *MaxAddress = BASE_4GB;\r
172 }\r
173\r
174 QemuFwCfgSelectItem (FwCfgItem);\r
175 for (Processed = 0; Processed < FwCfgSize; Processed += sizeof E820Entry) {\r
176 QemuFwCfgReadBytes (sizeof E820Entry, &E820Entry);\r
177 DEBUG ((\r
178 DEBUG_VERBOSE,\r
179 "%a: Base=0x%Lx Length=0x%Lx Type=%u\n",\r
180 __FUNCTION__,\r
181 E820Entry.BaseAddr,\r
182 E820Entry.Length,\r
183 E820Entry.Type\r
184 ));\r
185 if (E820Entry.Type == EfiAcpiAddressRangeMemory) {\r
186 if (AddHighHob && (E820Entry.BaseAddr >= BASE_4GB)) {\r
187 UINT64 Base;\r
188 UINT64 End;\r
189\r
190 //\r
191 // Round up the start address, and round down the end address.\r
192 //\r
193 Base = ALIGN_VALUE (E820Entry.BaseAddr, (UINT64)EFI_PAGE_SIZE);\r
194 End = (E820Entry.BaseAddr + E820Entry.Length) &\r
195 ~(UINT64)EFI_PAGE_MASK;\r
196 if (Base < End) {\r
197 PlatformAddMemoryRangeHob (Base, End);\r
198 DEBUG ((\r
199 DEBUG_VERBOSE,\r
200 "%a: PlatformAddMemoryRangeHob [0x%Lx, 0x%Lx)\n",\r
201 __FUNCTION__,\r
202 Base,\r
203 End\r
204 ));\r
205 }\r
206 }\r
207\r
208 if (MaxAddress || LowMemory) {\r
209 UINT64 Candidate;\r
210\r
211 Candidate = E820Entry.BaseAddr + E820Entry.Length;\r
212 if (MaxAddress && (Candidate > *MaxAddress)) {\r
213 *MaxAddress = Candidate;\r
214 DEBUG ((\r
215 DEBUG_VERBOSE,\r
216 "%a: MaxAddress=0x%Lx\n",\r
217 __FUNCTION__,\r
218 *MaxAddress\r
219 ));\r
220 }\r
221\r
222 if (LowMemory && (Candidate > *LowMemory) && (Candidate < BASE_4GB)) {\r
223 *LowMemory = Candidate;\r
224 DEBUG ((\r
225 DEBUG_VERBOSE,\r
226 "%a: LowMemory=0x%Lx\n",\r
227 __FUNCTION__,\r
228 *LowMemory\r
229 ));\r
230 }\r
231 }\r
bf65d7ee
GH
232 } else if (E820Entry.Type == EfiAcpiAddressRangeReserved) {\r
233 if (AddHighHob) {\r
234 DEBUG ((\r
235 DEBUG_INFO,\r
236 "%a: Reserved: Base=0x%Lx Length=0x%Lx\n",\r
237 __FUNCTION__,\r
238 E820Entry.BaseAddr,\r
239 E820Entry.Length\r
240 ));\r
241 BuildResourceDescriptorHob (\r
242 EFI_RESOURCE_MEMORY_RESERVED,\r
243 0,\r
244 E820Entry.BaseAddr,\r
245 E820Entry.Length\r
246 );\r
247 }\r
10460942
MX
248 }\r
249 }\r
250\r
251 return EFI_SUCCESS;\r
252}\r
253\r
e0375304
GH
254typedef VOID (*E820_SCAN_CALLBACK) (\r
255 EFI_E820_ENTRY64 *E820Entry,\r
256 EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
257 );\r
258\r
259/**\r
260 Store first address not used by e820 RAM entries in\r
261 PlatformInfoHob->FirstNonAddress\r
262**/\r
263STATIC\r
264VOID\r
265PlatformGetFirstNonAddressCB (\r
266 IN EFI_E820_ENTRY64 *E820Entry,\r
267 IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
268 )\r
269{\r
270 UINT64 Candidate;\r
271\r
272 if (E820Entry->Type != EfiAcpiAddressRangeMemory) {\r
273 return;\r
274 }\r
275\r
276 Candidate = E820Entry->BaseAddr + E820Entry->Length;\r
277 if (PlatformInfoHob->FirstNonAddress < Candidate) {\r
278 DEBUG ((DEBUG_INFO, "%a: FirstNonAddress=0x%Lx\n", __FUNCTION__, Candidate));\r
279 PlatformInfoHob->FirstNonAddress = Candidate;\r
280 }\r
281}\r
282\r
283/**\r
284 Iterate over the entries in QEMU's fw_cfg E820 RAM map, call the\r
285 passed callback for each entry.\r
286\r
287 @param[in] Callback The callback function to be called.\r
288\r
289 @param[in out] PlatformInfoHob PlatformInfo struct which is passed\r
290 through to the callback.\r
291\r
292 @retval EFI_SUCCESS The fw_cfg E820 RAM map was found and processed.\r
293\r
294 @retval EFI_PROTOCOL_ERROR The RAM map was found, but its size wasn't a\r
295 whole multiple of sizeof(EFI_E820_ENTRY64). No\r
296 RAM entry was processed.\r
297\r
298 @return Error codes from QemuFwCfgFindFile(). No RAM\r
299 entry was processed.\r
300**/\r
301STATIC\r
302EFI_STATUS\r
303PlatformScanE820 (\r
304 IN E820_SCAN_CALLBACK Callback,\r
305 IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
306 )\r
307{\r
308 EFI_STATUS Status;\r
309 FIRMWARE_CONFIG_ITEM FwCfgItem;\r
310 UINTN FwCfgSize;\r
311 EFI_E820_ENTRY64 E820Entry;\r
312 UINTN Processed;\r
313\r
314 Status = QemuFwCfgFindFile ("etc/e820", &FwCfgItem, &FwCfgSize);\r
315 if (EFI_ERROR (Status)) {\r
316 return Status;\r
317 }\r
318\r
319 if (FwCfgSize % sizeof E820Entry != 0) {\r
320 return EFI_PROTOCOL_ERROR;\r
321 }\r
322\r
323 QemuFwCfgSelectItem (FwCfgItem);\r
324 for (Processed = 0; Processed < FwCfgSize; Processed += sizeof E820Entry) {\r
325 QemuFwCfgReadBytes (sizeof E820Entry, &E820Entry);\r
326 Callback (&E820Entry, PlatformInfoHob);\r
327 }\r
328\r
329 return EFI_SUCCESS;\r
330}\r
331\r
10460942
MX
332/**\r
333 Returns PVH memmap\r
334\r
335 @param Entries Pointer to PVH memmap\r
336 @param Count Number of entries\r
337\r
338 @return EFI_STATUS\r
339**/\r
340EFI_STATUS\r
341GetPvhMemmapEntries (\r
342 struct hvm_memmap_table_entry **Entries,\r
343 UINT32 *Count\r
344 )\r
345{\r
346 UINT32 *PVHResetVectorData;\r
347 struct hvm_start_info *pvh_start_info;\r
348\r
349 PVHResetVectorData = (VOID *)(UINTN)PcdGet32 (PcdXenPvhStartOfDayStructPtr);\r
350 if (PVHResetVectorData == 0) {\r
351 return EFI_NOT_FOUND;\r
352 }\r
353\r
354 pvh_start_info = (struct hvm_start_info *)(UINTN)PVHResetVectorData[0];\r
355\r
356 *Entries = (struct hvm_memmap_table_entry *)(UINTN)pvh_start_info->memmap_paddr;\r
357 *Count = pvh_start_info->memmap_entries;\r
358\r
359 return EFI_SUCCESS;\r
360}\r
361\r
362STATIC\r
363UINT64\r
364GetHighestSystemMemoryAddressFromPvhMemmap (\r
365 BOOLEAN Below4gb\r
366 )\r
367{\r
368 struct hvm_memmap_table_entry *Memmap;\r
369 UINT32 MemmapEntriesCount;\r
370 struct hvm_memmap_table_entry *Entry;\r
371 EFI_STATUS Status;\r
372 UINT32 Loop;\r
373 UINT64 HighestAddress;\r
374 UINT64 EntryEnd;\r
375\r
376 HighestAddress = 0;\r
377\r
378 Status = GetPvhMemmapEntries (&Memmap, &MemmapEntriesCount);\r
379 ASSERT_EFI_ERROR (Status);\r
380\r
381 for (Loop = 0; Loop < MemmapEntriesCount; Loop++) {\r
382 Entry = Memmap + Loop;\r
383 EntryEnd = Entry->addr + Entry->size;\r
384\r
385 if ((Entry->type == XEN_HVM_MEMMAP_TYPE_RAM) &&\r
386 (EntryEnd > HighestAddress))\r
387 {\r
388 if (Below4gb && (EntryEnd <= BASE_4GB)) {\r
389 HighestAddress = EntryEnd;\r
390 } else if (!Below4gb && (EntryEnd >= BASE_4GB)) {\r
391 HighestAddress = EntryEnd;\r
392 }\r
393 }\r
394 }\r
395\r
396 return HighestAddress;\r
397}\r
398\r
399UINT32\r
400EFIAPI\r
401PlatformGetSystemMemorySizeBelow4gb (\r
402 IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
403 )\r
404{\r
405 EFI_STATUS Status;\r
406 UINT64 LowerMemorySize = 0;\r
407 UINT8 Cmos0x34;\r
408 UINT8 Cmos0x35;\r
409\r
e03b0d06
SB
410 if ((PlatformInfoHob->HostBridgeDevId == CLOUDHV_DEVICE_ID) &&\r
411 (CcProbe () != CcGuestTypeIntelTdx))\r
412 {\r
10460942
MX
413 // Get the information from PVH memmap\r
414 return (UINT32)GetHighestSystemMemoryAddressFromPvhMemmap (TRUE);\r
415 }\r
416\r
417 Status = PlatformScanOrAdd64BitE820Ram (FALSE, &LowerMemorySize, NULL);\r
418 if ((Status == EFI_SUCCESS) && (LowerMemorySize > 0)) {\r
419 return (UINT32)LowerMemorySize;\r
420 }\r
421\r
422 //\r
423 // CMOS 0x34/0x35 specifies the system memory above 16 MB.\r
424 // * CMOS(0x35) is the high byte\r
425 // * CMOS(0x34) is the low byte\r
426 // * The size is specified in 64kb chunks\r
427 // * Since this is memory above 16MB, the 16MB must be added\r
428 // into the calculation to get the total memory size.\r
429 //\r
430\r
431 Cmos0x34 = (UINT8)PlatformCmosRead8 (0x34);\r
432 Cmos0x35 = (UINT8)PlatformCmosRead8 (0x35);\r
433\r
434 return (UINT32)(((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r
435}\r
436\r
437STATIC\r
438UINT64\r
439PlatformGetSystemMemorySizeAbove4gb (\r
440 )\r
441{\r
442 UINT32 Size;\r
443 UINTN CmosIndex;\r
444\r
445 //\r
446 // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.\r
447 // * CMOS(0x5d) is the most significant size byte\r
448 // * CMOS(0x5c) is the middle size byte\r
449 // * CMOS(0x5b) is the least significant size byte\r
450 // * The size is specified in 64kb chunks\r
451 //\r
452\r
453 Size = 0;\r
454 for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {\r
455 Size = (UINT32)(Size << 8) + (UINT32)PlatformCmosRead8 (CmosIndex);\r
456 }\r
457\r
458 return LShiftU64 (Size, 16);\r
459}\r
460\r
461/**\r
462 Return the highest address that DXE could possibly use, plus one.\r
463**/\r
464STATIC\r
e0375304 465VOID\r
10460942
MX
466PlatformGetFirstNonAddress (\r
467 IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
468 )\r
469{\r
10460942
MX
470 UINT32 FwCfgPciMmio64Mb;\r
471 EFI_STATUS Status;\r
472 FIRMWARE_CONFIG_ITEM FwCfgItem;\r
473 UINTN FwCfgSize;\r
474 UINT64 HotPlugMemoryEnd;\r
475\r
10460942
MX
476 //\r
477 // If QEMU presents an E820 map, then get the highest exclusive >=4GB RAM\r
478 // address from it. This can express an address >= 4GB+1TB.\r
479 //\r
480 // Otherwise, get the flat size of the memory above 4GB from the CMOS (which\r
481 // can only express a size smaller than 1TB), and add it to 4GB.\r
482 //\r
e0375304
GH
483 PlatformInfoHob->FirstNonAddress = BASE_4GB;\r
484 Status = PlatformScanE820 (PlatformGetFirstNonAddressCB, PlatformInfoHob);\r
10460942 485 if (EFI_ERROR (Status)) {\r
e0375304 486 PlatformInfoHob->FirstNonAddress = BASE_4GB + PlatformGetSystemMemorySizeAbove4gb ();\r
10460942
MX
487 }\r
488\r
489 //\r
490 // If DXE is 32-bit, then we're done; PciBusDxe will degrade 64-bit MMIO\r
491 // resources to 32-bit anyway. See DegradeResource() in\r
492 // "PciResourceSupport.c".\r
493 //\r
494 #ifdef MDE_CPU_IA32\r
495 if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r
e0375304 496 return;\r
10460942
MX
497 }\r
498\r
499 #endif\r
500\r
501 //\r
502 // See if the user specified the number of megabytes for the 64-bit PCI host\r
503 // aperture. Accept an aperture size up to 16TB.\r
504 //\r
505 // As signaled by the "X-" prefix, this knob is experimental, and might go\r
506 // away at any time.\r
507 //\r
508 Status = QemuFwCfgParseUint32 (\r
509 "opt/ovmf/X-PciMmio64Mb",\r
510 FALSE,\r
511 &FwCfgPciMmio64Mb\r
512 );\r
513 switch (Status) {\r
514 case EFI_UNSUPPORTED:\r
515 case EFI_NOT_FOUND:\r
516 break;\r
517 case EFI_SUCCESS:\r
518 if (FwCfgPciMmio64Mb <= 0x1000000) {\r
519 PlatformInfoHob->PcdPciMmio64Size = LShiftU64 (FwCfgPciMmio64Mb, 20);\r
520 break;\r
521 }\r
522\r
523 //\r
524 // fall through\r
525 //\r
526 default:\r
527 DEBUG ((\r
528 DEBUG_WARN,\r
529 "%a: ignoring malformed 64-bit PCI host aperture size from fw_cfg\n",\r
530 __FUNCTION__\r
531 ));\r
532 break;\r
533 }\r
534\r
535 if (PlatformInfoHob->PcdPciMmio64Size == 0) {\r
536 if (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME) {\r
537 DEBUG ((\r
538 DEBUG_INFO,\r
539 "%a: disabling 64-bit PCI host aperture\n",\r
540 __FUNCTION__\r
541 ));\r
542 }\r
543\r
544 //\r
545 // There's nothing more to do; the amount of memory above 4GB fully\r
546 // determines the highest address plus one. The memory hotplug area (see\r
547 // below) plays no role for the firmware in this case.\r
548 //\r
e0375304 549 return;\r
10460942
MX
550 }\r
551\r
552 //\r
553 // The "etc/reserved-memory-end" fw_cfg file, when present, contains an\r
554 // absolute, exclusive end address for the memory hotplug area. This area\r
555 // starts right at the end of the memory above 4GB. The 64-bit PCI host\r
556 // aperture must be placed above it.\r
557 //\r
558 Status = QemuFwCfgFindFile (\r
559 "etc/reserved-memory-end",\r
560 &FwCfgItem,\r
561 &FwCfgSize\r
562 );\r
563 if (!EFI_ERROR (Status) && (FwCfgSize == sizeof HotPlugMemoryEnd)) {\r
564 QemuFwCfgSelectItem (FwCfgItem);\r
565 QemuFwCfgReadBytes (FwCfgSize, &HotPlugMemoryEnd);\r
566 DEBUG ((\r
567 DEBUG_VERBOSE,\r
568 "%a: HotPlugMemoryEnd=0x%Lx\n",\r
569 __FUNCTION__,\r
570 HotPlugMemoryEnd\r
571 ));\r
572\r
e0375304
GH
573 ASSERT (HotPlugMemoryEnd >= PlatformInfoHob->FirstNonAddress);\r
574 PlatformInfoHob->FirstNonAddress = HotPlugMemoryEnd;\r
10460942
MX
575 }\r
576\r
577 //\r
578 // SeaBIOS aligns both boundaries of the 64-bit PCI host aperture to 1GB, so\r
579 // that the host can map it with 1GB hugepages. Follow suit.\r
580 //\r
e0375304 581 PlatformInfoHob->PcdPciMmio64Base = ALIGN_VALUE (PlatformInfoHob->FirstNonAddress, (UINT64)SIZE_1GB);\r
10460942
MX
582 PlatformInfoHob->PcdPciMmio64Size = ALIGN_VALUE (PlatformInfoHob->PcdPciMmio64Size, (UINT64)SIZE_1GB);\r
583\r
584 //\r
585 // The 64-bit PCI host aperture should also be "naturally" aligned. The\r
586 // alignment is determined by rounding the size of the aperture down to the\r
587 // next smaller or equal power of two. That is, align the aperture by the\r
588 // largest BAR size that can fit into it.\r
589 //\r
590 PlatformInfoHob->PcdPciMmio64Base = ALIGN_VALUE (PlatformInfoHob->PcdPciMmio64Base, GetPowerOfTwo64 (PlatformInfoHob->PcdPciMmio64Size));\r
591\r
592 //\r
593 // The useful address space ends with the 64-bit PCI host aperture.\r
594 //\r
e0375304
GH
595 PlatformInfoHob->FirstNonAddress = PlatformInfoHob->PcdPciMmio64Base + PlatformInfoHob->PcdPciMmio64Size;\r
596 return;\r
10460942
MX
597}\r
598\r
bd10d4e2 599/*\r
8f9ef9c9 600 * Use CPUID to figure physical address width.\r
bd10d4e2 601 *\r
8f9ef9c9
GH
602 * Does *not* work reliable on qemu. For historical reasons qemu\r
603 * returns phys-bits=40 by default even in case the host machine\r
604 * supports less than that.\r
605 *\r
606 * So we apply the following rules (which can be enabled/disabled\r
607 * using the QemuQuirk parameter) to figure whenever we can work with\r
608 * the returned physical address width or not:\r
609 *\r
610 * (1) If it is 41 or higher consider it valid.\r
611 * (2) If it is 40 or lower consider it valid in case it matches a\r
612 * known-good value for the CPU vendor, which is:\r
613 * -> 36 or 39 for Intel\r
614 * -> 40 for AMD\r
615 * (3) Otherwise consider it invalid.\r
616 *\r
617 * Recommendation: Run qemu with host-phys-bits=on. That will make\r
618 * sure guest phys-bits is not larger than host phys-bits. Some\r
619 * distro builds do that by default.\r
bd10d4e2
GH
620 */\r
621VOID\r
622EFIAPI\r
623PlatformAddressWidthFromCpuid (\r
8f9ef9c9
GH
624 IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob,\r
625 IN BOOLEAN QemuQuirk\r
bd10d4e2
GH
626 )\r
627{\r
8f9ef9c9
GH
628 UINT32 RegEax, RegEbx, RegEcx, RegEdx, Max;\r
629 UINT8 PhysBits;\r
630 CHAR8 Signature[13] = { 0 };\r
631 BOOLEAN Valid = FALSE;\r
632 BOOLEAN Page1GSupport = FALSE;\r
633\r
634 AsmCpuid (0x80000000, &RegEax, &RegEbx, &RegEcx, &RegEdx);\r
635 *(UINT32 *)(Signature + 0) = RegEbx;\r
636 *(UINT32 *)(Signature + 4) = RegEdx;\r
637 *(UINT32 *)(Signature + 8) = RegEcx;\r
638 Max = RegEax;\r
639\r
640 if (Max >= 0x80000001) {\r
641 AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r
642 if ((RegEdx & BIT26) != 0) {\r
643 Page1GSupport = TRUE;\r
644 }\r
645 }\r
bd10d4e2 646\r
8f9ef9c9 647 if (Max >= 0x80000008) {\r
bd10d4e2 648 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r
8f9ef9c9 649 PhysBits = (UINT8)RegEax;\r
bd10d4e2 650 } else {\r
8f9ef9c9 651 PhysBits = 36;\r
bd10d4e2
GH
652 }\r
653\r
8f9ef9c9
GH
654 if (!QemuQuirk) {\r
655 Valid = TRUE;\r
656 } else if (PhysBits >= 41) {\r
657 Valid = TRUE;\r
658 } else if (AsciiStrCmp (Signature, "GenuineIntel") == 0) {\r
659 if ((PhysBits == 36) || (PhysBits == 39)) {\r
660 Valid = TRUE;\r
661 }\r
662 } else if (AsciiStrCmp (Signature, "AuthenticAMD") == 0) {\r
663 if (PhysBits == 40) {\r
664 Valid = TRUE;\r
665 }\r
666 }\r
bd10d4e2
GH
667\r
668 DEBUG ((\r
669 DEBUG_INFO,\r
8f9ef9c9 670 "%a: Signature: '%a', PhysBits: %d, QemuQuirk: %a, Valid: %a\n",\r
bd10d4e2 671 __FUNCTION__,\r
8f9ef9c9
GH
672 Signature,\r
673 PhysBits,\r
674 QemuQuirk ? "On" : "Off",\r
675 Valid ? "Yes" : "No"\r
bd10d4e2 676 ));\r
8f9ef9c9
GH
677\r
678 if (Valid) {\r
679 if (PhysBits > 47) {\r
680 /*\r
681 * Avoid 5-level paging altogether for now, which limits\r
682 * PhysBits to 48. Also avoid using address bit 48, due to sign\r
683 * extension we can't identity-map these addresses (and lots of\r
684 * places in edk2 assume we have everything identity-mapped).\r
685 * So the actual limit is 47.\r
686 */\r
687 DEBUG ((DEBUG_INFO, "%a: limit PhysBits to 47 (avoid 5-level paging)\n", __func__));\r
688 PhysBits = 47;\r
689 }\r
690\r
691 if (!Page1GSupport && (PhysBits > 40)) {\r
692 DEBUG ((DEBUG_INFO, "%a: limit PhysBits to 40 (no 1G pages available)\n", __func__));\r
693 PhysBits = 40;\r
694 }\r
695\r
696 PlatformInfoHob->PhysMemAddressWidth = PhysBits;\r
697 PlatformInfoHob->FirstNonAddress = LShiftU64 (1, PlatformInfoHob->PhysMemAddressWidth);\r
698 }\r
bd10d4e2
GH
699}\r
700\r
ecb778d0
GH
701VOID\r
702EFIAPI\r
703PlatformDynamicMmioWindow (\r
704 IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
705 )\r
706{\r
707 UINT64 AddrSpace, MmioSpace;\r
708\r
709 AddrSpace = LShiftU64 (1, PlatformInfoHob->PhysMemAddressWidth);\r
710 MmioSpace = LShiftU64 (1, PlatformInfoHob->PhysMemAddressWidth - 3);\r
711\r
712 if ((PlatformInfoHob->PcdPciMmio64Size < MmioSpace) &&\r
713 (PlatformInfoHob->PcdPciMmio64Base + MmioSpace < AddrSpace))\r
714 {\r
715 DEBUG ((DEBUG_INFO, "%a: using dynamic mmio window\n", __func__));\r
716 DEBUG ((DEBUG_INFO, "%a: Addr Space 0x%Lx (%Ld GB)\n", __func__, AddrSpace, RShiftU64 (AddrSpace, 30)));\r
717 DEBUG ((DEBUG_INFO, "%a: MMIO Space 0x%Lx (%Ld GB)\n", __func__, MmioSpace, RShiftU64 (MmioSpace, 30)));\r
718 PlatformInfoHob->PcdPciMmio64Size = MmioSpace;\r
719 PlatformInfoHob->PcdPciMmio64Base = AddrSpace - MmioSpace;\r
720 } else {\r
721 DEBUG ((DEBUG_INFO, "%a: using classic mmio window\n", __func__));\r
722 }\r
723\r
724 DEBUG ((DEBUG_INFO, "%a: Pci64 Base 0x%Lx\n", __func__, PlatformInfoHob->PcdPciMmio64Base));\r
725 DEBUG ((DEBUG_INFO, "%a: Pci64 Size 0x%Lx\n", __func__, PlatformInfoHob->PcdPciMmio64Size));\r
726}\r
727\r
3497fd5c
NOL
728/**\r
729 Iterate over the PCI host bridges resources information optionally provided\r
730 in fw-cfg and find the highest address contained in the PCI MMIO windows. If\r
731 the information is found, return the exclusive end; one past the last usable\r
732 address.\r
733\r
734 @param[out] PciMmioAddressEnd Pointer to one-after End Address updated with\r
735 information extracted from host-provided data\r
736 or zero if no information available or an\r
737 error happened\r
738\r
739 @retval EFI_SUCCESS PCI information was read and the output\r
740 parameter updated with the last valid\r
741 address in the 64-bit MMIO range.\r
742 @retval EFI_INVALID_PARAMETER Pointer parameter is invalid\r
743 @retval EFI_INCOMPATIBLE_VERSION Hardware information found in fw-cfg\r
744 has an incompatible format\r
745 @retval EFI_UNSUPPORTED Fw-cfg is not supported, thus host\r
746 provided information, if any, cannot be\r
747 read\r
748 @retval EFI_NOT_FOUND No PCI host bridge information provided\r
749 by the host.\r
750**/\r
751STATIC\r
752EFI_STATUS\r
753PlatformScanHostProvided64BitPciMmioEnd (\r
754 OUT UINT64 *PciMmioAddressEnd\r
755 )\r
756{\r
757 EFI_STATUS Status;\r
758 HOST_BRIDGE_INFO HostBridge;\r
759 FIRMWARE_CONFIG_ITEM FwCfgItem;\r
760 UINTN FwCfgSize;\r
761 UINTN FwCfgReadIndex;\r
762 UINTN ReadDataSize;\r
763 UINT64 Above4GMmioEnd;\r
764\r
765 if (PciMmioAddressEnd == NULL) {\r
766 return EFI_INVALID_PARAMETER;\r
767 }\r
768\r
769 *PciMmioAddressEnd = 0;\r
770 Above4GMmioEnd = 0;\r
771\r
772 Status = QemuFwCfgFindFile ("etc/hardware-info", &FwCfgItem, &FwCfgSize);\r
773 if (EFI_ERROR (Status)) {\r
774 return Status;\r
775 }\r
776\r
777 QemuFwCfgSelectItem (FwCfgItem);\r
778\r
779 FwCfgReadIndex = 0;\r
780 while (FwCfgReadIndex < FwCfgSize) {\r
781 Status = QemuFwCfgReadNextHardwareInfoByType (\r
782 HardwareInfoTypeHostBridge,\r
783 sizeof (HostBridge),\r
784 FwCfgSize,\r
785 &HostBridge,\r
786 &ReadDataSize,\r
787 &FwCfgReadIndex\r
788 );\r
789\r
790 if (Status != EFI_SUCCESS) {\r
791 //\r
792 // No more data available to read in the file, break\r
793 // loop and finish process\r
794 //\r
795 break;\r
796 }\r
797\r
798 Status = HardwareInfoPciHostBridgeLastMmioAddress (\r
799 &HostBridge,\r
800 ReadDataSize,\r
801 TRUE,\r
802 &Above4GMmioEnd\r
803 );\r
804\r
805 if (Status != EFI_SUCCESS) {\r
806 //\r
807 // Error parsing MMIO apertures and extracting last MMIO\r
808 // address, reset PciMmioAddressEnd as if no information was\r
809 // found, to avoid moving forward with incomplete data, and\r
810 // bail out\r
811 //\r
812 DEBUG ((\r
813 DEBUG_ERROR,\r
814 "%a: ignoring malformed hardware information from fw_cfg\n",\r
815 __FUNCTION__\r
816 ));\r
817 *PciMmioAddressEnd = 0;\r
818 return Status;\r
819 }\r
820\r
821 if (Above4GMmioEnd > *PciMmioAddressEnd) {\r
822 *PciMmioAddressEnd = Above4GMmioEnd;\r
823 }\r
824 }\r
825\r
826 if (*PciMmioAddressEnd > 0) {\r
827 //\r
828 // Host-provided PCI information was found and a MMIO window end\r
829 // derived from it.\r
830 // Increase the End address by one to have the output pointing to\r
831 // one after the address in use (exclusive end).\r
832 //\r
833 *PciMmioAddressEnd += 1;\r
834\r
835 DEBUG ((\r
836 DEBUG_INFO,\r
837 "%a: Pci64End=0x%Lx\n",\r
838 __FUNCTION__,\r
839 *PciMmioAddressEnd\r
840 ));\r
841\r
842 return EFI_SUCCESS;\r
843 }\r
844\r
845 return EFI_NOT_FOUND;\r
846}\r
847\r
10460942
MX
848/**\r
849 Initialize the PhysMemAddressWidth field in PlatformInfoHob based on guest RAM size.\r
850**/\r
851VOID\r
852EFIAPI\r
853PlatformAddressWidthInitialization (\r
854 IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
855 )\r
856{\r
3497fd5c
NOL
857 UINT8 PhysMemAddressWidth;\r
858 EFI_STATUS Status;\r
10460942 859\r
bd10d4e2 860 if (PlatformInfoHob->HostBridgeDevId == 0xffff /* microvm */) {\r
8f9ef9c9 861 PlatformAddressWidthFromCpuid (PlatformInfoHob, FALSE);\r
bd10d4e2
GH
862 return;\r
863 }\r
864\r
10460942 865 //\r
3497fd5c
NOL
866 // First scan host-provided hardware information to assess if the address\r
867 // space is already known. If so, guest must use those values.\r
10460942 868 //\r
e0375304 869 Status = PlatformScanHostProvided64BitPciMmioEnd (&PlatformInfoHob->FirstNonAddress);\r
3497fd5c
NOL
870\r
871 if (EFI_ERROR (Status)) {\r
872 //\r
873 // If the host did not provide valid hardware information leading to a\r
874 // hard-defined 64-bit MMIO end, fold back to calculating the minimum range\r
875 // needed.\r
876 // As guest-physical memory size grows, the permanent PEI RAM requirements\r
877 // are dominated by the identity-mapping page tables built by the DXE IPL.\r
878 // The DXL IPL keys off of the physical address bits advertized in the CPU\r
879 // HOB. To conserve memory, we calculate the minimum address width here.\r
880 //\r
e0375304 881 PlatformGetFirstNonAddress (PlatformInfoHob);\r
3497fd5c
NOL
882 }\r
883\r
bbda386d
GH
884 PlatformAddressWidthFromCpuid (PlatformInfoHob, TRUE);\r
885 if (PlatformInfoHob->PhysMemAddressWidth != 0) {\r
886 // physical address width is known\r
ecb778d0 887 PlatformDynamicMmioWindow (PlatformInfoHob);\r
bbda386d
GH
888 return;\r
889 }\r
890\r
891 //\r
892 // physical address width is NOT known\r
893 // -> do some guess work, mostly based on installed memory\r
894 // -> try be conservstibe to stay below the guaranteed minimum of\r
895 // 36 phys bits (aka 64 GB).\r
896 //\r
e0375304 897 PhysMemAddressWidth = (UINT8)HighBitSet64 (PlatformInfoHob->FirstNonAddress);\r
10460942
MX
898\r
899 //\r
900 // If FirstNonAddress is not an integral power of two, then we need an\r
901 // additional bit.\r
902 //\r
e0375304 903 if ((PlatformInfoHob->FirstNonAddress & (PlatformInfoHob->FirstNonAddress - 1)) != 0) {\r
10460942
MX
904 ++PhysMemAddressWidth;\r
905 }\r
906\r
907 //\r
908 // The minimum address width is 36 (covers up to and excluding 64 GB, which\r
909 // is the maximum for Ia32 + PAE). The theoretical architecture maximum for\r
910 // X64 long mode is 52 bits, but the DXE IPL clamps that down to 48 bits. We\r
911 // can simply assert that here, since 48 bits are good enough for 256 TB.\r
912 //\r
913 if (PhysMemAddressWidth <= 36) {\r
914 PhysMemAddressWidth = 36;\r
915 }\r
916\r
e23f8f52
MX
917 #if defined (MDE_CPU_X64)\r
918 if (TdIsEnabled ()) {\r
919 if (TdSharedPageMask () == (1ULL << 47)) {\r
920 PhysMemAddressWidth = 48;\r
921 } else {\r
922 PhysMemAddressWidth = 52;\r
923 }\r
924 }\r
925\r
926 ASSERT (PhysMemAddressWidth <= 52);\r
927 #else\r
10460942 928 ASSERT (PhysMemAddressWidth <= 48);\r
e23f8f52 929 #endif\r
10460942 930\r
10460942
MX
931 PlatformInfoHob->PhysMemAddressWidth = PhysMemAddressWidth;\r
932}\r
933\r
934STATIC\r
935VOID\r
936QemuInitializeRamBelow1gb (\r
937 IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
938 )\r
939{\r
940 if (PlatformInfoHob->SmmSmramRequire && PlatformInfoHob->Q35SmramAtDefaultSmbase) {\r
941 PlatformAddMemoryRangeHob (0, SMM_DEFAULT_SMBASE);\r
942 PlatformAddReservedMemoryBaseSizeHob (\r
943 SMM_DEFAULT_SMBASE,\r
944 MCH_DEFAULT_SMBASE_SIZE,\r
945 TRUE /* Cacheable */\r
946 );\r
947 STATIC_ASSERT (\r
948 SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE < BASE_512KB + BASE_128KB,\r
949 "end of SMRAM at default SMBASE ends at, or exceeds, 640KB"\r
950 );\r
951 PlatformAddMemoryRangeHob (\r
952 SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE,\r
953 BASE_512KB + BASE_128KB\r
954 );\r
955 } else {\r
956 PlatformAddMemoryRangeHob (0, BASE_512KB + BASE_128KB);\r
957 }\r
958}\r
959\r
960/**\r
961 Peform Memory Detection for QEMU / KVM\r
962\r
963**/\r
964VOID\r
965EFIAPI\r
966PlatformQemuInitializeRam (\r
967 IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
968 )\r
969{\r
970 UINT64 LowerMemorySize;\r
971 UINT64 UpperMemorySize;\r
972 MTRR_SETTINGS MtrrSettings;\r
973 EFI_STATUS Status;\r
974\r
975 DEBUG ((DEBUG_INFO, "%a called\n", __FUNCTION__));\r
976\r
977 //\r
978 // Determine total memory size available\r
979 //\r
980 LowerMemorySize = PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r
981\r
982 if (PlatformInfoHob->BootMode == BOOT_ON_S3_RESUME) {\r
983 //\r
984 // Create the following memory HOB as an exception on the S3 boot path.\r
985 //\r
986 // Normally we'd create memory HOBs only on the normal boot path. However,\r
987 // CpuMpPei specifically needs such a low-memory HOB on the S3 path as\r
988 // well, for "borrowing" a subset of it temporarily, for the AP startup\r
989 // vector.\r
990 //\r
991 // CpuMpPei saves the original contents of the borrowed area in permanent\r
992 // PEI RAM, in a backup buffer allocated with the normal PEI services.\r
993 // CpuMpPei restores the original contents ("returns" the borrowed area) at\r
994 // End-of-PEI. End-of-PEI in turn is emitted by S3Resume2Pei before\r
995 // transferring control to the OS's wakeup vector in the FACS.\r
996 //\r
997 // We expect any other PEIMs that "borrow" memory similarly to CpuMpPei to\r
998 // restore the original contents. Furthermore, we expect all such PEIMs\r
999 // (CpuMpPei included) to claim the borrowed areas by producing memory\r
1000 // allocation HOBs, and to honor preexistent memory allocation HOBs when\r
1001 // looking for an area to borrow.\r
1002 //\r
1003 QemuInitializeRamBelow1gb (PlatformInfoHob);\r
1004 } else {\r
1005 //\r
1006 // Create memory HOBs\r
1007 //\r
1008 QemuInitializeRamBelow1gb (PlatformInfoHob);\r
1009\r
1010 if (PlatformInfoHob->SmmSmramRequire) {\r
1011 UINT32 TsegSize;\r
1012\r
1013 TsegSize = PlatformInfoHob->Q35TsegMbytes * SIZE_1MB;\r
1014 PlatformAddMemoryRangeHob (BASE_1MB, LowerMemorySize - TsegSize);\r
1015 PlatformAddReservedMemoryBaseSizeHob (\r
1016 LowerMemorySize - TsegSize,\r
1017 TsegSize,\r
1018 TRUE\r
1019 );\r
1020 } else {\r
1021 PlatformAddMemoryRangeHob (BASE_1MB, LowerMemorySize);\r
1022 }\r
1023\r
1024 //\r
1025 // If QEMU presents an E820 map, then create memory HOBs for the >=4GB RAM\r
1026 // entries. Otherwise, create a single memory HOB with the flat >=4GB\r
1027 // memory size read from the CMOS.\r
1028 //\r
1029 Status = PlatformScanOrAdd64BitE820Ram (TRUE, NULL, NULL);\r
1030 if (EFI_ERROR (Status)) {\r
1031 UpperMemorySize = PlatformGetSystemMemorySizeAbove4gb ();\r
1032 if (UpperMemorySize != 0) {\r
1033 PlatformAddMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);\r
1034 }\r
1035 }\r
1036 }\r
1037\r
1038 //\r
1039 // We'd like to keep the following ranges uncached:\r
1040 // - [640 KB, 1 MB)\r
85fd05ab 1041 // - [Uc32Base, 4 GB)\r
10460942
MX
1042 //\r
1043 // Everything else should be WB. Unfortunately, programming the inverse (ie.\r
1044 // keeping the default UC, and configuring the complement set of the above as\r
1045 // WB) is not reliable in general, because the end of the upper RAM can have\r
1046 // practically any alignment, and we may not have enough variable MTRRs to\r
1047 // cover it exactly.\r
1048 //\r
1049 if (IsMtrrSupported () && (PlatformInfoHob->HostBridgeDevId != CLOUDHV_DEVICE_ID)) {\r
1050 MtrrGetAllMtrrs (&MtrrSettings);\r
1051\r
1052 //\r
1053 // MTRRs disabled, fixed MTRRs disabled, default type is uncached\r
1054 //\r
1055 ASSERT ((MtrrSettings.MtrrDefType & BIT11) == 0);\r
1056 ASSERT ((MtrrSettings.MtrrDefType & BIT10) == 0);\r
1057 ASSERT ((MtrrSettings.MtrrDefType & 0xFF) == 0);\r
1058\r
1059 //\r
1060 // flip default type to writeback\r
1061 //\r
1062 SetMem (&MtrrSettings.Fixed, sizeof MtrrSettings.Fixed, 0x06);\r
1063 ZeroMem (&MtrrSettings.Variables, sizeof MtrrSettings.Variables);\r
1064 MtrrSettings.MtrrDefType |= BIT11 | BIT10 | 6;\r
1065 MtrrSetAllMtrrs (&MtrrSettings);\r
1066\r
1067 //\r
1068 // Set memory range from 640KB to 1MB to uncacheable\r
1069 //\r
1070 Status = MtrrSetMemoryAttribute (\r
1071 BASE_512KB + BASE_128KB,\r
1072 BASE_1MB - (BASE_512KB + BASE_128KB),\r
1073 CacheUncacheable\r
1074 );\r
1075 ASSERT_EFI_ERROR (Status);\r
1076\r
1077 //\r
1078 // Set the memory range from the start of the 32-bit MMIO area (32-bit PCI\r
1079 // MMIO aperture on i440fx, PCIEXBAR on q35) to 4GB as uncacheable.\r
1080 //\r
1081 Status = MtrrSetMemoryAttribute (\r
1082 PlatformInfoHob->Uc32Base,\r
1083 SIZE_4GB - PlatformInfoHob->Uc32Base,\r
1084 CacheUncacheable\r
1085 );\r
1086 ASSERT_EFI_ERROR (Status);\r
1087 }\r
1088}\r
1089\r
1090VOID\r
1091EFIAPI\r
1092PlatformQemuInitializeRamForS3 (\r
1093 IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
1094 )\r
1095{\r
1096 if (PlatformInfoHob->S3Supported && (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME)) {\r
1097 //\r
1098 // This is the memory range that will be used for PEI on S3 resume\r
1099 //\r
1100 BuildMemoryAllocationHob (\r
1101 PlatformInfoHob->S3AcpiReservedMemoryBase,\r
1102 PlatformInfoHob->S3AcpiReservedMemorySize,\r
1103 EfiACPIMemoryNVS\r
1104 );\r
1105\r
1106 //\r
1107 // Cover the initial RAM area used as stack and temporary PEI heap.\r
1108 //\r
1109 // This is reserved as ACPI NVS so it can be used on S3 resume.\r
1110 //\r
1111 BuildMemoryAllocationHob (\r
1112 PcdGet32 (PcdOvmfSecPeiTempRamBase),\r
1113 PcdGet32 (PcdOvmfSecPeiTempRamSize),\r
1114 EfiACPIMemoryNVS\r
1115 );\r
1116\r
1117 //\r
1118 // SEC stores its table of GUIDed section handlers here.\r
1119 //\r
1120 BuildMemoryAllocationHob (\r
1121 PcdGet64 (PcdGuidedExtractHandlerTableAddress),\r
1122 PcdGet32 (PcdGuidedExtractHandlerTableSize),\r
1123 EfiACPIMemoryNVS\r
1124 );\r
1125\r
1126 #ifdef MDE_CPU_X64\r
1127 //\r
1128 // Reserve the initial page tables built by the reset vector code.\r
1129 //\r
1130 // Since this memory range will be used by the Reset Vector on S3\r
1131 // resume, it must be reserved as ACPI NVS.\r
1132 //\r
1133 BuildMemoryAllocationHob (\r
1134 (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecPageTablesBase),\r
1135 (UINT64)(UINTN)PcdGet32 (PcdOvmfSecPageTablesSize),\r
1136 EfiACPIMemoryNVS\r
1137 );\r
1138\r
1139 if (PlatformInfoHob->SevEsIsEnabled) {\r
1140 //\r
1141 // If SEV-ES is enabled, reserve the GHCB-related memory area. This\r
1142 // includes the extra page table used to break down the 2MB page\r
1143 // mapping into 4KB page entries where the GHCB resides and the\r
1144 // GHCB area itself.\r
1145 //\r
1146 // Since this memory range will be used by the Reset Vector on S3\r
1147 // resume, it must be reserved as ACPI NVS.\r
1148 //\r
1149 BuildMemoryAllocationHob (\r
1150 (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbPageTableBase),\r
1151 (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbPageTableSize),\r
1152 EfiACPIMemoryNVS\r
1153 );\r
1154 BuildMemoryAllocationHob (\r
1155 (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbBase),\r
1156 (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbSize),\r
1157 EfiACPIMemoryNVS\r
1158 );\r
1159 BuildMemoryAllocationHob (\r
1160 (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupBase),\r
1161 (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupSize),\r
1162 EfiACPIMemoryNVS\r
1163 );\r
1164 }\r
1165\r
1166 #endif\r
1167 }\r
1168\r
1169 if (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME) {\r
1170 if (!PlatformInfoHob->SmmSmramRequire) {\r
1171 //\r
1172 // Reserve the lock box storage area\r
1173 //\r
1174 // Since this memory range will be used on S3 resume, it must be\r
1175 // reserved as ACPI NVS.\r
1176 //\r
1177 // If S3 is unsupported, then various drivers might still write to the\r
1178 // LockBox area. We ought to prevent DXE from serving allocation requests\r
1179 // such that they would overlap the LockBox storage.\r
1180 //\r
1181 ZeroMem (\r
1182 (VOID *)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase),\r
1183 (UINTN)PcdGet32 (PcdOvmfLockBoxStorageSize)\r
1184 );\r
1185 BuildMemoryAllocationHob (\r
1186 (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase),\r
1187 (UINT64)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageSize),\r
1188 PlatformInfoHob->S3Supported ? EfiACPIMemoryNVS : EfiBootServicesData\r
1189 );\r
1190 }\r
1191\r
1192 if (PlatformInfoHob->SmmSmramRequire) {\r
1193 UINT32 TsegSize;\r
1194\r
1195 //\r
1196 // Make sure the TSEG area that we reported as a reserved memory resource\r
1197 // cannot be used for reserved memory allocations.\r
1198 //\r
1199 TsegSize = PlatformInfoHob->Q35TsegMbytes * SIZE_1MB;\r
1200 BuildMemoryAllocationHob (\r
1201 PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob) - TsegSize,\r
1202 TsegSize,\r
1203 EfiReservedMemoryType\r
1204 );\r
1205 //\r
1206 // Similarly, allocate away the (already reserved) SMRAM at the default\r
1207 // SMBASE, if it exists.\r
1208 //\r
1209 if (PlatformInfoHob->Q35SmramAtDefaultSmbase) {\r
1210 BuildMemoryAllocationHob (\r
1211 SMM_DEFAULT_SMBASE,\r
1212 MCH_DEFAULT_SMBASE_SIZE,\r
1213 EfiReservedMemoryType\r
1214 );\r
1215 }\r
1216 }\r
1217\r
1218 #ifdef MDE_CPU_X64\r
1219 if (FixedPcdGet32 (PcdOvmfWorkAreaSize) != 0) {\r
1220 //\r
1221 // Reserve the work area.\r
1222 //\r
1223 // Since this memory range will be used by the Reset Vector on S3\r
1224 // resume, it must be reserved as ACPI NVS.\r
1225 //\r
1226 // If S3 is unsupported, then various drivers might still write to the\r
1227 // work area. We ought to prevent DXE from serving allocation requests\r
1228 // such that they would overlap the work area.\r
1229 //\r
1230 BuildMemoryAllocationHob (\r
1231 (EFI_PHYSICAL_ADDRESS)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaBase),\r
1232 (UINT64)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaSize),\r
1233 PlatformInfoHob->S3Supported ? EfiACPIMemoryNVS : EfiBootServicesData\r
1234 );\r
1235 }\r
1236\r
1237 #endif\r
1238 }\r
1239}\r