]>
Commit | Line | Data |
---|---|---|
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 | |
7 | Module 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 |
48 | VOID\r |
49 | EFIAPI\r | |
50 | PlatformQemuUc32BaseInitialization (\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 | |
143 | STATIC\r | |
144 | EFI_STATUS\r | |
145 | PlatformScanOrAdd64BitE820Ram (\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 | |
254 | /**\r | |
255 | Returns PVH memmap\r | |
256 | \r | |
257 | @param Entries Pointer to PVH memmap\r | |
258 | @param Count Number of entries\r | |
259 | \r | |
260 | @return EFI_STATUS\r | |
261 | **/\r | |
262 | EFI_STATUS\r | |
263 | GetPvhMemmapEntries (\r | |
264 | struct hvm_memmap_table_entry **Entries,\r | |
265 | UINT32 *Count\r | |
266 | )\r | |
267 | {\r | |
268 | UINT32 *PVHResetVectorData;\r | |
269 | struct hvm_start_info *pvh_start_info;\r | |
270 | \r | |
271 | PVHResetVectorData = (VOID *)(UINTN)PcdGet32 (PcdXenPvhStartOfDayStructPtr);\r | |
272 | if (PVHResetVectorData == 0) {\r | |
273 | return EFI_NOT_FOUND;\r | |
274 | }\r | |
275 | \r | |
276 | pvh_start_info = (struct hvm_start_info *)(UINTN)PVHResetVectorData[0];\r | |
277 | \r | |
278 | *Entries = (struct hvm_memmap_table_entry *)(UINTN)pvh_start_info->memmap_paddr;\r | |
279 | *Count = pvh_start_info->memmap_entries;\r | |
280 | \r | |
281 | return EFI_SUCCESS;\r | |
282 | }\r | |
283 | \r | |
284 | STATIC\r | |
285 | UINT64\r | |
286 | GetHighestSystemMemoryAddressFromPvhMemmap (\r | |
287 | BOOLEAN Below4gb\r | |
288 | )\r | |
289 | {\r | |
290 | struct hvm_memmap_table_entry *Memmap;\r | |
291 | UINT32 MemmapEntriesCount;\r | |
292 | struct hvm_memmap_table_entry *Entry;\r | |
293 | EFI_STATUS Status;\r | |
294 | UINT32 Loop;\r | |
295 | UINT64 HighestAddress;\r | |
296 | UINT64 EntryEnd;\r | |
297 | \r | |
298 | HighestAddress = 0;\r | |
299 | \r | |
300 | Status = GetPvhMemmapEntries (&Memmap, &MemmapEntriesCount);\r | |
301 | ASSERT_EFI_ERROR (Status);\r | |
302 | \r | |
303 | for (Loop = 0; Loop < MemmapEntriesCount; Loop++) {\r | |
304 | Entry = Memmap + Loop;\r | |
305 | EntryEnd = Entry->addr + Entry->size;\r | |
306 | \r | |
307 | if ((Entry->type == XEN_HVM_MEMMAP_TYPE_RAM) &&\r | |
308 | (EntryEnd > HighestAddress))\r | |
309 | {\r | |
310 | if (Below4gb && (EntryEnd <= BASE_4GB)) {\r | |
311 | HighestAddress = EntryEnd;\r | |
312 | } else if (!Below4gb && (EntryEnd >= BASE_4GB)) {\r | |
313 | HighestAddress = EntryEnd;\r | |
314 | }\r | |
315 | }\r | |
316 | }\r | |
317 | \r | |
318 | return HighestAddress;\r | |
319 | }\r | |
320 | \r | |
321 | UINT32\r | |
322 | EFIAPI\r | |
323 | PlatformGetSystemMemorySizeBelow4gb (\r | |
324 | IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
325 | )\r | |
326 | {\r | |
327 | EFI_STATUS Status;\r | |
328 | UINT64 LowerMemorySize = 0;\r | |
329 | UINT8 Cmos0x34;\r | |
330 | UINT8 Cmos0x35;\r | |
331 | \r | |
e03b0d06 SB |
332 | if ((PlatformInfoHob->HostBridgeDevId == CLOUDHV_DEVICE_ID) &&\r |
333 | (CcProbe () != CcGuestTypeIntelTdx))\r | |
334 | {\r | |
10460942 MX |
335 | // Get the information from PVH memmap\r |
336 | return (UINT32)GetHighestSystemMemoryAddressFromPvhMemmap (TRUE);\r | |
337 | }\r | |
338 | \r | |
339 | Status = PlatformScanOrAdd64BitE820Ram (FALSE, &LowerMemorySize, NULL);\r | |
340 | if ((Status == EFI_SUCCESS) && (LowerMemorySize > 0)) {\r | |
341 | return (UINT32)LowerMemorySize;\r | |
342 | }\r | |
343 | \r | |
344 | //\r | |
345 | // CMOS 0x34/0x35 specifies the system memory above 16 MB.\r | |
346 | // * CMOS(0x35) is the high byte\r | |
347 | // * CMOS(0x34) is the low byte\r | |
348 | // * The size is specified in 64kb chunks\r | |
349 | // * Since this is memory above 16MB, the 16MB must be added\r | |
350 | // into the calculation to get the total memory size.\r | |
351 | //\r | |
352 | \r | |
353 | Cmos0x34 = (UINT8)PlatformCmosRead8 (0x34);\r | |
354 | Cmos0x35 = (UINT8)PlatformCmosRead8 (0x35);\r | |
355 | \r | |
356 | return (UINT32)(((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r | |
357 | }\r | |
358 | \r | |
359 | STATIC\r | |
360 | UINT64\r | |
361 | PlatformGetSystemMemorySizeAbove4gb (\r | |
362 | )\r | |
363 | {\r | |
364 | UINT32 Size;\r | |
365 | UINTN CmosIndex;\r | |
366 | \r | |
367 | //\r | |
368 | // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.\r | |
369 | // * CMOS(0x5d) is the most significant size byte\r | |
370 | // * CMOS(0x5c) is the middle size byte\r | |
371 | // * CMOS(0x5b) is the least significant size byte\r | |
372 | // * The size is specified in 64kb chunks\r | |
373 | //\r | |
374 | \r | |
375 | Size = 0;\r | |
376 | for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {\r | |
377 | Size = (UINT32)(Size << 8) + (UINT32)PlatformCmosRead8 (CmosIndex);\r | |
378 | }\r | |
379 | \r | |
380 | return LShiftU64 (Size, 16);\r | |
381 | }\r | |
382 | \r | |
383 | /**\r | |
384 | Return the highest address that DXE could possibly use, plus one.\r | |
385 | **/\r | |
386 | STATIC\r | |
387 | UINT64\r | |
388 | PlatformGetFirstNonAddress (\r | |
389 | IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
390 | )\r | |
391 | {\r | |
392 | UINT64 FirstNonAddress;\r | |
393 | UINT32 FwCfgPciMmio64Mb;\r | |
394 | EFI_STATUS Status;\r | |
395 | FIRMWARE_CONFIG_ITEM FwCfgItem;\r | |
396 | UINTN FwCfgSize;\r | |
397 | UINT64 HotPlugMemoryEnd;\r | |
398 | \r | |
399 | //\r | |
400 | // set FirstNonAddress to suppress incorrect compiler/analyzer warnings\r | |
401 | //\r | |
402 | FirstNonAddress = 0;\r | |
403 | \r | |
404 | //\r | |
405 | // If QEMU presents an E820 map, then get the highest exclusive >=4GB RAM\r | |
406 | // address from it. This can express an address >= 4GB+1TB.\r | |
407 | //\r | |
408 | // Otherwise, get the flat size of the memory above 4GB from the CMOS (which\r | |
409 | // can only express a size smaller than 1TB), and add it to 4GB.\r | |
410 | //\r | |
411 | Status = PlatformScanOrAdd64BitE820Ram (FALSE, NULL, &FirstNonAddress);\r | |
412 | if (EFI_ERROR (Status)) {\r | |
413 | FirstNonAddress = BASE_4GB + PlatformGetSystemMemorySizeAbove4gb ();\r | |
414 | }\r | |
415 | \r | |
416 | //\r | |
417 | // If DXE is 32-bit, then we're done; PciBusDxe will degrade 64-bit MMIO\r | |
418 | // resources to 32-bit anyway. See DegradeResource() in\r | |
419 | // "PciResourceSupport.c".\r | |
420 | //\r | |
421 | #ifdef MDE_CPU_IA32\r | |
422 | if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r | |
423 | return FirstNonAddress;\r | |
424 | }\r | |
425 | \r | |
426 | #endif\r | |
427 | \r | |
428 | //\r | |
429 | // See if the user specified the number of megabytes for the 64-bit PCI host\r | |
430 | // aperture. Accept an aperture size up to 16TB.\r | |
431 | //\r | |
432 | // As signaled by the "X-" prefix, this knob is experimental, and might go\r | |
433 | // away at any time.\r | |
434 | //\r | |
435 | Status = QemuFwCfgParseUint32 (\r | |
436 | "opt/ovmf/X-PciMmio64Mb",\r | |
437 | FALSE,\r | |
438 | &FwCfgPciMmio64Mb\r | |
439 | );\r | |
440 | switch (Status) {\r | |
441 | case EFI_UNSUPPORTED:\r | |
442 | case EFI_NOT_FOUND:\r | |
443 | break;\r | |
444 | case EFI_SUCCESS:\r | |
445 | if (FwCfgPciMmio64Mb <= 0x1000000) {\r | |
446 | PlatformInfoHob->PcdPciMmio64Size = LShiftU64 (FwCfgPciMmio64Mb, 20);\r | |
447 | break;\r | |
448 | }\r | |
449 | \r | |
450 | //\r | |
451 | // fall through\r | |
452 | //\r | |
453 | default:\r | |
454 | DEBUG ((\r | |
455 | DEBUG_WARN,\r | |
456 | "%a: ignoring malformed 64-bit PCI host aperture size from fw_cfg\n",\r | |
457 | __FUNCTION__\r | |
458 | ));\r | |
459 | break;\r | |
460 | }\r | |
461 | \r | |
462 | if (PlatformInfoHob->PcdPciMmio64Size == 0) {\r | |
463 | if (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME) {\r | |
464 | DEBUG ((\r | |
465 | DEBUG_INFO,\r | |
466 | "%a: disabling 64-bit PCI host aperture\n",\r | |
467 | __FUNCTION__\r | |
468 | ));\r | |
469 | }\r | |
470 | \r | |
471 | //\r | |
472 | // There's nothing more to do; the amount of memory above 4GB fully\r | |
473 | // determines the highest address plus one. The memory hotplug area (see\r | |
474 | // below) plays no role for the firmware in this case.\r | |
475 | //\r | |
476 | return FirstNonAddress;\r | |
477 | }\r | |
478 | \r | |
479 | //\r | |
480 | // The "etc/reserved-memory-end" fw_cfg file, when present, contains an\r | |
481 | // absolute, exclusive end address for the memory hotplug area. This area\r | |
482 | // starts right at the end of the memory above 4GB. The 64-bit PCI host\r | |
483 | // aperture must be placed above it.\r | |
484 | //\r | |
485 | Status = QemuFwCfgFindFile (\r | |
486 | "etc/reserved-memory-end",\r | |
487 | &FwCfgItem,\r | |
488 | &FwCfgSize\r | |
489 | );\r | |
490 | if (!EFI_ERROR (Status) && (FwCfgSize == sizeof HotPlugMemoryEnd)) {\r | |
491 | QemuFwCfgSelectItem (FwCfgItem);\r | |
492 | QemuFwCfgReadBytes (FwCfgSize, &HotPlugMemoryEnd);\r | |
493 | DEBUG ((\r | |
494 | DEBUG_VERBOSE,\r | |
495 | "%a: HotPlugMemoryEnd=0x%Lx\n",\r | |
496 | __FUNCTION__,\r | |
497 | HotPlugMemoryEnd\r | |
498 | ));\r | |
499 | \r | |
500 | ASSERT (HotPlugMemoryEnd >= FirstNonAddress);\r | |
501 | FirstNonAddress = HotPlugMemoryEnd;\r | |
502 | }\r | |
503 | \r | |
504 | //\r | |
505 | // SeaBIOS aligns both boundaries of the 64-bit PCI host aperture to 1GB, so\r | |
506 | // that the host can map it with 1GB hugepages. Follow suit.\r | |
507 | //\r | |
508 | PlatformInfoHob->PcdPciMmio64Base = ALIGN_VALUE (FirstNonAddress, (UINT64)SIZE_1GB);\r | |
509 | PlatformInfoHob->PcdPciMmio64Size = ALIGN_VALUE (PlatformInfoHob->PcdPciMmio64Size, (UINT64)SIZE_1GB);\r | |
510 | \r | |
511 | //\r | |
512 | // The 64-bit PCI host aperture should also be "naturally" aligned. The\r | |
513 | // alignment is determined by rounding the size of the aperture down to the\r | |
514 | // next smaller or equal power of two. That is, align the aperture by the\r | |
515 | // largest BAR size that can fit into it.\r | |
516 | //\r | |
517 | PlatformInfoHob->PcdPciMmio64Base = ALIGN_VALUE (PlatformInfoHob->PcdPciMmio64Base, GetPowerOfTwo64 (PlatformInfoHob->PcdPciMmio64Size));\r | |
518 | \r | |
519 | //\r | |
520 | // The useful address space ends with the 64-bit PCI host aperture.\r | |
521 | //\r | |
522 | FirstNonAddress = PlatformInfoHob->PcdPciMmio64Base + PlatformInfoHob->PcdPciMmio64Size;\r | |
523 | return FirstNonAddress;\r | |
524 | }\r | |
525 | \r | |
bd10d4e2 | 526 | /*\r |
8f9ef9c9 | 527 | * Use CPUID to figure physical address width.\r |
bd10d4e2 | 528 | *\r |
8f9ef9c9 GH |
529 | * Does *not* work reliable on qemu. For historical reasons qemu\r |
530 | * returns phys-bits=40 by default even in case the host machine\r | |
531 | * supports less than that.\r | |
532 | *\r | |
533 | * So we apply the following rules (which can be enabled/disabled\r | |
534 | * using the QemuQuirk parameter) to figure whenever we can work with\r | |
535 | * the returned physical address width or not:\r | |
536 | *\r | |
537 | * (1) If it is 41 or higher consider it valid.\r | |
538 | * (2) If it is 40 or lower consider it valid in case it matches a\r | |
539 | * known-good value for the CPU vendor, which is:\r | |
540 | * -> 36 or 39 for Intel\r | |
541 | * -> 40 for AMD\r | |
542 | * (3) Otherwise consider it invalid.\r | |
543 | *\r | |
544 | * Recommendation: Run qemu with host-phys-bits=on. That will make\r | |
545 | * sure guest phys-bits is not larger than host phys-bits. Some\r | |
546 | * distro builds do that by default.\r | |
bd10d4e2 GH |
547 | */\r |
548 | VOID\r | |
549 | EFIAPI\r | |
550 | PlatformAddressWidthFromCpuid (\r | |
8f9ef9c9 GH |
551 | IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob,\r |
552 | IN BOOLEAN QemuQuirk\r | |
bd10d4e2 GH |
553 | )\r |
554 | {\r | |
8f9ef9c9 GH |
555 | UINT32 RegEax, RegEbx, RegEcx, RegEdx, Max;\r |
556 | UINT8 PhysBits;\r | |
557 | CHAR8 Signature[13] = { 0 };\r | |
558 | BOOLEAN Valid = FALSE;\r | |
559 | BOOLEAN Page1GSupport = FALSE;\r | |
560 | \r | |
561 | AsmCpuid (0x80000000, &RegEax, &RegEbx, &RegEcx, &RegEdx);\r | |
562 | *(UINT32 *)(Signature + 0) = RegEbx;\r | |
563 | *(UINT32 *)(Signature + 4) = RegEdx;\r | |
564 | *(UINT32 *)(Signature + 8) = RegEcx;\r | |
565 | Max = RegEax;\r | |
566 | \r | |
567 | if (Max >= 0x80000001) {\r | |
568 | AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r | |
569 | if ((RegEdx & BIT26) != 0) {\r | |
570 | Page1GSupport = TRUE;\r | |
571 | }\r | |
572 | }\r | |
bd10d4e2 | 573 | \r |
8f9ef9c9 | 574 | if (Max >= 0x80000008) {\r |
bd10d4e2 | 575 | AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r |
8f9ef9c9 | 576 | PhysBits = (UINT8)RegEax;\r |
bd10d4e2 | 577 | } else {\r |
8f9ef9c9 | 578 | PhysBits = 36;\r |
bd10d4e2 GH |
579 | }\r |
580 | \r | |
8f9ef9c9 GH |
581 | if (!QemuQuirk) {\r |
582 | Valid = TRUE;\r | |
583 | } else if (PhysBits >= 41) {\r | |
584 | Valid = TRUE;\r | |
585 | } else if (AsciiStrCmp (Signature, "GenuineIntel") == 0) {\r | |
586 | if ((PhysBits == 36) || (PhysBits == 39)) {\r | |
587 | Valid = TRUE;\r | |
588 | }\r | |
589 | } else if (AsciiStrCmp (Signature, "AuthenticAMD") == 0) {\r | |
590 | if (PhysBits == 40) {\r | |
591 | Valid = TRUE;\r | |
592 | }\r | |
593 | }\r | |
bd10d4e2 GH |
594 | \r |
595 | DEBUG ((\r | |
596 | DEBUG_INFO,\r | |
8f9ef9c9 | 597 | "%a: Signature: '%a', PhysBits: %d, QemuQuirk: %a, Valid: %a\n",\r |
bd10d4e2 | 598 | __FUNCTION__,\r |
8f9ef9c9 GH |
599 | Signature,\r |
600 | PhysBits,\r | |
601 | QemuQuirk ? "On" : "Off",\r | |
602 | Valid ? "Yes" : "No"\r | |
bd10d4e2 | 603 | ));\r |
8f9ef9c9 GH |
604 | \r |
605 | if (Valid) {\r | |
606 | if (PhysBits > 47) {\r | |
607 | /*\r | |
608 | * Avoid 5-level paging altogether for now, which limits\r | |
609 | * PhysBits to 48. Also avoid using address bit 48, due to sign\r | |
610 | * extension we can't identity-map these addresses (and lots of\r | |
611 | * places in edk2 assume we have everything identity-mapped).\r | |
612 | * So the actual limit is 47.\r | |
613 | */\r | |
614 | DEBUG ((DEBUG_INFO, "%a: limit PhysBits to 47 (avoid 5-level paging)\n", __func__));\r | |
615 | PhysBits = 47;\r | |
616 | }\r | |
617 | \r | |
618 | if (!Page1GSupport && (PhysBits > 40)) {\r | |
619 | DEBUG ((DEBUG_INFO, "%a: limit PhysBits to 40 (no 1G pages available)\n", __func__));\r | |
620 | PhysBits = 40;\r | |
621 | }\r | |
622 | \r | |
623 | PlatformInfoHob->PhysMemAddressWidth = PhysBits;\r | |
624 | PlatformInfoHob->FirstNonAddress = LShiftU64 (1, PlatformInfoHob->PhysMemAddressWidth);\r | |
625 | }\r | |
bd10d4e2 GH |
626 | }\r |
627 | \r | |
ecb778d0 GH |
628 | VOID\r |
629 | EFIAPI\r | |
630 | PlatformDynamicMmioWindow (\r | |
631 | IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
632 | )\r | |
633 | {\r | |
634 | UINT64 AddrSpace, MmioSpace;\r | |
635 | \r | |
636 | AddrSpace = LShiftU64 (1, PlatformInfoHob->PhysMemAddressWidth);\r | |
637 | MmioSpace = LShiftU64 (1, PlatformInfoHob->PhysMemAddressWidth - 3);\r | |
638 | \r | |
639 | if ((PlatformInfoHob->PcdPciMmio64Size < MmioSpace) &&\r | |
640 | (PlatformInfoHob->PcdPciMmio64Base + MmioSpace < AddrSpace))\r | |
641 | {\r | |
642 | DEBUG ((DEBUG_INFO, "%a: using dynamic mmio window\n", __func__));\r | |
643 | DEBUG ((DEBUG_INFO, "%a: Addr Space 0x%Lx (%Ld GB)\n", __func__, AddrSpace, RShiftU64 (AddrSpace, 30)));\r | |
644 | DEBUG ((DEBUG_INFO, "%a: MMIO Space 0x%Lx (%Ld GB)\n", __func__, MmioSpace, RShiftU64 (MmioSpace, 30)));\r | |
645 | PlatformInfoHob->PcdPciMmio64Size = MmioSpace;\r | |
646 | PlatformInfoHob->PcdPciMmio64Base = AddrSpace - MmioSpace;\r | |
647 | } else {\r | |
648 | DEBUG ((DEBUG_INFO, "%a: using classic mmio window\n", __func__));\r | |
649 | }\r | |
650 | \r | |
651 | DEBUG ((DEBUG_INFO, "%a: Pci64 Base 0x%Lx\n", __func__, PlatformInfoHob->PcdPciMmio64Base));\r | |
652 | DEBUG ((DEBUG_INFO, "%a: Pci64 Size 0x%Lx\n", __func__, PlatformInfoHob->PcdPciMmio64Size));\r | |
653 | }\r | |
654 | \r | |
3497fd5c NOL |
655 | /**\r |
656 | Iterate over the PCI host bridges resources information optionally provided\r | |
657 | in fw-cfg and find the highest address contained in the PCI MMIO windows. If\r | |
658 | the information is found, return the exclusive end; one past the last usable\r | |
659 | address.\r | |
660 | \r | |
661 | @param[out] PciMmioAddressEnd Pointer to one-after End Address updated with\r | |
662 | information extracted from host-provided data\r | |
663 | or zero if no information available or an\r | |
664 | error happened\r | |
665 | \r | |
666 | @retval EFI_SUCCESS PCI information was read and the output\r | |
667 | parameter updated with the last valid\r | |
668 | address in the 64-bit MMIO range.\r | |
669 | @retval EFI_INVALID_PARAMETER Pointer parameter is invalid\r | |
670 | @retval EFI_INCOMPATIBLE_VERSION Hardware information found in fw-cfg\r | |
671 | has an incompatible format\r | |
672 | @retval EFI_UNSUPPORTED Fw-cfg is not supported, thus host\r | |
673 | provided information, if any, cannot be\r | |
674 | read\r | |
675 | @retval EFI_NOT_FOUND No PCI host bridge information provided\r | |
676 | by the host.\r | |
677 | **/\r | |
678 | STATIC\r | |
679 | EFI_STATUS\r | |
680 | PlatformScanHostProvided64BitPciMmioEnd (\r | |
681 | OUT UINT64 *PciMmioAddressEnd\r | |
682 | )\r | |
683 | {\r | |
684 | EFI_STATUS Status;\r | |
685 | HOST_BRIDGE_INFO HostBridge;\r | |
686 | FIRMWARE_CONFIG_ITEM FwCfgItem;\r | |
687 | UINTN FwCfgSize;\r | |
688 | UINTN FwCfgReadIndex;\r | |
689 | UINTN ReadDataSize;\r | |
690 | UINT64 Above4GMmioEnd;\r | |
691 | \r | |
692 | if (PciMmioAddressEnd == NULL) {\r | |
693 | return EFI_INVALID_PARAMETER;\r | |
694 | }\r | |
695 | \r | |
696 | *PciMmioAddressEnd = 0;\r | |
697 | Above4GMmioEnd = 0;\r | |
698 | \r | |
699 | Status = QemuFwCfgFindFile ("etc/hardware-info", &FwCfgItem, &FwCfgSize);\r | |
700 | if (EFI_ERROR (Status)) {\r | |
701 | return Status;\r | |
702 | }\r | |
703 | \r | |
704 | QemuFwCfgSelectItem (FwCfgItem);\r | |
705 | \r | |
706 | FwCfgReadIndex = 0;\r | |
707 | while (FwCfgReadIndex < FwCfgSize) {\r | |
708 | Status = QemuFwCfgReadNextHardwareInfoByType (\r | |
709 | HardwareInfoTypeHostBridge,\r | |
710 | sizeof (HostBridge),\r | |
711 | FwCfgSize,\r | |
712 | &HostBridge,\r | |
713 | &ReadDataSize,\r | |
714 | &FwCfgReadIndex\r | |
715 | );\r | |
716 | \r | |
717 | if (Status != EFI_SUCCESS) {\r | |
718 | //\r | |
719 | // No more data available to read in the file, break\r | |
720 | // loop and finish process\r | |
721 | //\r | |
722 | break;\r | |
723 | }\r | |
724 | \r | |
725 | Status = HardwareInfoPciHostBridgeLastMmioAddress (\r | |
726 | &HostBridge,\r | |
727 | ReadDataSize,\r | |
728 | TRUE,\r | |
729 | &Above4GMmioEnd\r | |
730 | );\r | |
731 | \r | |
732 | if (Status != EFI_SUCCESS) {\r | |
733 | //\r | |
734 | // Error parsing MMIO apertures and extracting last MMIO\r | |
735 | // address, reset PciMmioAddressEnd as if no information was\r | |
736 | // found, to avoid moving forward with incomplete data, and\r | |
737 | // bail out\r | |
738 | //\r | |
739 | DEBUG ((\r | |
740 | DEBUG_ERROR,\r | |
741 | "%a: ignoring malformed hardware information from fw_cfg\n",\r | |
742 | __FUNCTION__\r | |
743 | ));\r | |
744 | *PciMmioAddressEnd = 0;\r | |
745 | return Status;\r | |
746 | }\r | |
747 | \r | |
748 | if (Above4GMmioEnd > *PciMmioAddressEnd) {\r | |
749 | *PciMmioAddressEnd = Above4GMmioEnd;\r | |
750 | }\r | |
751 | }\r | |
752 | \r | |
753 | if (*PciMmioAddressEnd > 0) {\r | |
754 | //\r | |
755 | // Host-provided PCI information was found and a MMIO window end\r | |
756 | // derived from it.\r | |
757 | // Increase the End address by one to have the output pointing to\r | |
758 | // one after the address in use (exclusive end).\r | |
759 | //\r | |
760 | *PciMmioAddressEnd += 1;\r | |
761 | \r | |
762 | DEBUG ((\r | |
763 | DEBUG_INFO,\r | |
764 | "%a: Pci64End=0x%Lx\n",\r | |
765 | __FUNCTION__,\r | |
766 | *PciMmioAddressEnd\r | |
767 | ));\r | |
768 | \r | |
769 | return EFI_SUCCESS;\r | |
770 | }\r | |
771 | \r | |
772 | return EFI_NOT_FOUND;\r | |
773 | }\r | |
774 | \r | |
10460942 MX |
775 | /**\r |
776 | Initialize the PhysMemAddressWidth field in PlatformInfoHob based on guest RAM size.\r | |
777 | **/\r | |
778 | VOID\r | |
779 | EFIAPI\r | |
780 | PlatformAddressWidthInitialization (\r | |
781 | IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
782 | )\r | |
783 | {\r | |
3497fd5c NOL |
784 | UINT64 FirstNonAddress;\r |
785 | UINT8 PhysMemAddressWidth;\r | |
786 | EFI_STATUS Status;\r | |
10460942 | 787 | \r |
bd10d4e2 | 788 | if (PlatformInfoHob->HostBridgeDevId == 0xffff /* microvm */) {\r |
8f9ef9c9 | 789 | PlatformAddressWidthFromCpuid (PlatformInfoHob, FALSE);\r |
bd10d4e2 GH |
790 | return;\r |
791 | }\r | |
792 | \r | |
10460942 | 793 | //\r |
3497fd5c NOL |
794 | // First scan host-provided hardware information to assess if the address\r |
795 | // space is already known. If so, guest must use those values.\r | |
10460942 | 796 | //\r |
3497fd5c NOL |
797 | Status = PlatformScanHostProvided64BitPciMmioEnd (&FirstNonAddress);\r |
798 | \r | |
799 | if (EFI_ERROR (Status)) {\r | |
800 | //\r | |
801 | // If the host did not provide valid hardware information leading to a\r | |
802 | // hard-defined 64-bit MMIO end, fold back to calculating the minimum range\r | |
803 | // needed.\r | |
804 | // As guest-physical memory size grows, the permanent PEI RAM requirements\r | |
805 | // are dominated by the identity-mapping page tables built by the DXE IPL.\r | |
806 | // The DXL IPL keys off of the physical address bits advertized in the CPU\r | |
807 | // HOB. To conserve memory, we calculate the minimum address width here.\r | |
808 | //\r | |
809 | FirstNonAddress = PlatformGetFirstNonAddress (PlatformInfoHob);\r | |
810 | }\r | |
811 | \r | |
bbda386d GH |
812 | PlatformAddressWidthFromCpuid (PlatformInfoHob, TRUE);\r |
813 | if (PlatformInfoHob->PhysMemAddressWidth != 0) {\r | |
814 | // physical address width is known\r | |
815 | PlatformInfoHob->FirstNonAddress = FirstNonAddress;\r | |
ecb778d0 | 816 | PlatformDynamicMmioWindow (PlatformInfoHob);\r |
bbda386d GH |
817 | return;\r |
818 | }\r | |
819 | \r | |
820 | //\r | |
821 | // physical address width is NOT known\r | |
822 | // -> do some guess work, mostly based on installed memory\r | |
823 | // -> try be conservstibe to stay below the guaranteed minimum of\r | |
824 | // 36 phys bits (aka 64 GB).\r | |
825 | //\r | |
10460942 MX |
826 | PhysMemAddressWidth = (UINT8)HighBitSet64 (FirstNonAddress);\r |
827 | \r | |
828 | //\r | |
829 | // If FirstNonAddress is not an integral power of two, then we need an\r | |
830 | // additional bit.\r | |
831 | //\r | |
832 | if ((FirstNonAddress & (FirstNonAddress - 1)) != 0) {\r | |
833 | ++PhysMemAddressWidth;\r | |
834 | }\r | |
835 | \r | |
836 | //\r | |
837 | // The minimum address width is 36 (covers up to and excluding 64 GB, which\r | |
838 | // is the maximum for Ia32 + PAE). The theoretical architecture maximum for\r | |
839 | // X64 long mode is 52 bits, but the DXE IPL clamps that down to 48 bits. We\r | |
840 | // can simply assert that here, since 48 bits are good enough for 256 TB.\r | |
841 | //\r | |
842 | if (PhysMemAddressWidth <= 36) {\r | |
843 | PhysMemAddressWidth = 36;\r | |
844 | }\r | |
845 | \r | |
e23f8f52 MX |
846 | #if defined (MDE_CPU_X64)\r |
847 | if (TdIsEnabled ()) {\r | |
848 | if (TdSharedPageMask () == (1ULL << 47)) {\r | |
849 | PhysMemAddressWidth = 48;\r | |
850 | } else {\r | |
851 | PhysMemAddressWidth = 52;\r | |
852 | }\r | |
853 | }\r | |
854 | \r | |
855 | ASSERT (PhysMemAddressWidth <= 52);\r | |
856 | #else\r | |
10460942 | 857 | ASSERT (PhysMemAddressWidth <= 48);\r |
e23f8f52 | 858 | #endif\r |
10460942 MX |
859 | \r |
860 | PlatformInfoHob->FirstNonAddress = FirstNonAddress;\r | |
861 | PlatformInfoHob->PhysMemAddressWidth = PhysMemAddressWidth;\r | |
862 | }\r | |
863 | \r | |
864 | STATIC\r | |
865 | VOID\r | |
866 | QemuInitializeRamBelow1gb (\r | |
867 | IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
868 | )\r | |
869 | {\r | |
870 | if (PlatformInfoHob->SmmSmramRequire && PlatformInfoHob->Q35SmramAtDefaultSmbase) {\r | |
871 | PlatformAddMemoryRangeHob (0, SMM_DEFAULT_SMBASE);\r | |
872 | PlatformAddReservedMemoryBaseSizeHob (\r | |
873 | SMM_DEFAULT_SMBASE,\r | |
874 | MCH_DEFAULT_SMBASE_SIZE,\r | |
875 | TRUE /* Cacheable */\r | |
876 | );\r | |
877 | STATIC_ASSERT (\r | |
878 | SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE < BASE_512KB + BASE_128KB,\r | |
879 | "end of SMRAM at default SMBASE ends at, or exceeds, 640KB"\r | |
880 | );\r | |
881 | PlatformAddMemoryRangeHob (\r | |
882 | SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE,\r | |
883 | BASE_512KB + BASE_128KB\r | |
884 | );\r | |
885 | } else {\r | |
886 | PlatformAddMemoryRangeHob (0, BASE_512KB + BASE_128KB);\r | |
887 | }\r | |
888 | }\r | |
889 | \r | |
890 | /**\r | |
891 | Peform Memory Detection for QEMU / KVM\r | |
892 | \r | |
893 | **/\r | |
894 | VOID\r | |
895 | EFIAPI\r | |
896 | PlatformQemuInitializeRam (\r | |
897 | IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
898 | )\r | |
899 | {\r | |
900 | UINT64 LowerMemorySize;\r | |
901 | UINT64 UpperMemorySize;\r | |
902 | MTRR_SETTINGS MtrrSettings;\r | |
903 | EFI_STATUS Status;\r | |
904 | \r | |
905 | DEBUG ((DEBUG_INFO, "%a called\n", __FUNCTION__));\r | |
906 | \r | |
907 | //\r | |
908 | // Determine total memory size available\r | |
909 | //\r | |
910 | LowerMemorySize = PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r | |
911 | \r | |
912 | if (PlatformInfoHob->BootMode == BOOT_ON_S3_RESUME) {\r | |
913 | //\r | |
914 | // Create the following memory HOB as an exception on the S3 boot path.\r | |
915 | //\r | |
916 | // Normally we'd create memory HOBs only on the normal boot path. However,\r | |
917 | // CpuMpPei specifically needs such a low-memory HOB on the S3 path as\r | |
918 | // well, for "borrowing" a subset of it temporarily, for the AP startup\r | |
919 | // vector.\r | |
920 | //\r | |
921 | // CpuMpPei saves the original contents of the borrowed area in permanent\r | |
922 | // PEI RAM, in a backup buffer allocated with the normal PEI services.\r | |
923 | // CpuMpPei restores the original contents ("returns" the borrowed area) at\r | |
924 | // End-of-PEI. End-of-PEI in turn is emitted by S3Resume2Pei before\r | |
925 | // transferring control to the OS's wakeup vector in the FACS.\r | |
926 | //\r | |
927 | // We expect any other PEIMs that "borrow" memory similarly to CpuMpPei to\r | |
928 | // restore the original contents. Furthermore, we expect all such PEIMs\r | |
929 | // (CpuMpPei included) to claim the borrowed areas by producing memory\r | |
930 | // allocation HOBs, and to honor preexistent memory allocation HOBs when\r | |
931 | // looking for an area to borrow.\r | |
932 | //\r | |
933 | QemuInitializeRamBelow1gb (PlatformInfoHob);\r | |
934 | } else {\r | |
935 | //\r | |
936 | // Create memory HOBs\r | |
937 | //\r | |
938 | QemuInitializeRamBelow1gb (PlatformInfoHob);\r | |
939 | \r | |
940 | if (PlatformInfoHob->SmmSmramRequire) {\r | |
941 | UINT32 TsegSize;\r | |
942 | \r | |
943 | TsegSize = PlatformInfoHob->Q35TsegMbytes * SIZE_1MB;\r | |
944 | PlatformAddMemoryRangeHob (BASE_1MB, LowerMemorySize - TsegSize);\r | |
945 | PlatformAddReservedMemoryBaseSizeHob (\r | |
946 | LowerMemorySize - TsegSize,\r | |
947 | TsegSize,\r | |
948 | TRUE\r | |
949 | );\r | |
950 | } else {\r | |
951 | PlatformAddMemoryRangeHob (BASE_1MB, LowerMemorySize);\r | |
952 | }\r | |
953 | \r | |
954 | //\r | |
955 | // If QEMU presents an E820 map, then create memory HOBs for the >=4GB RAM\r | |
956 | // entries. Otherwise, create a single memory HOB with the flat >=4GB\r | |
957 | // memory size read from the CMOS.\r | |
958 | //\r | |
959 | Status = PlatformScanOrAdd64BitE820Ram (TRUE, NULL, NULL);\r | |
960 | if (EFI_ERROR (Status)) {\r | |
961 | UpperMemorySize = PlatformGetSystemMemorySizeAbove4gb ();\r | |
962 | if (UpperMemorySize != 0) {\r | |
963 | PlatformAddMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);\r | |
964 | }\r | |
965 | }\r | |
966 | }\r | |
967 | \r | |
968 | //\r | |
969 | // We'd like to keep the following ranges uncached:\r | |
970 | // - [640 KB, 1 MB)\r | |
971 | // - [LowerMemorySize, 4 GB)\r | |
972 | //\r | |
973 | // Everything else should be WB. Unfortunately, programming the inverse (ie.\r | |
974 | // keeping the default UC, and configuring the complement set of the above as\r | |
975 | // WB) is not reliable in general, because the end of the upper RAM can have\r | |
976 | // practically any alignment, and we may not have enough variable MTRRs to\r | |
977 | // cover it exactly.\r | |
978 | //\r | |
979 | if (IsMtrrSupported () && (PlatformInfoHob->HostBridgeDevId != CLOUDHV_DEVICE_ID)) {\r | |
980 | MtrrGetAllMtrrs (&MtrrSettings);\r | |
981 | \r | |
982 | //\r | |
983 | // MTRRs disabled, fixed MTRRs disabled, default type is uncached\r | |
984 | //\r | |
985 | ASSERT ((MtrrSettings.MtrrDefType & BIT11) == 0);\r | |
986 | ASSERT ((MtrrSettings.MtrrDefType & BIT10) == 0);\r | |
987 | ASSERT ((MtrrSettings.MtrrDefType & 0xFF) == 0);\r | |
988 | \r | |
989 | //\r | |
990 | // flip default type to writeback\r | |
991 | //\r | |
992 | SetMem (&MtrrSettings.Fixed, sizeof MtrrSettings.Fixed, 0x06);\r | |
993 | ZeroMem (&MtrrSettings.Variables, sizeof MtrrSettings.Variables);\r | |
994 | MtrrSettings.MtrrDefType |= BIT11 | BIT10 | 6;\r | |
995 | MtrrSetAllMtrrs (&MtrrSettings);\r | |
996 | \r | |
997 | //\r | |
998 | // Set memory range from 640KB to 1MB to uncacheable\r | |
999 | //\r | |
1000 | Status = MtrrSetMemoryAttribute (\r | |
1001 | BASE_512KB + BASE_128KB,\r | |
1002 | BASE_1MB - (BASE_512KB + BASE_128KB),\r | |
1003 | CacheUncacheable\r | |
1004 | );\r | |
1005 | ASSERT_EFI_ERROR (Status);\r | |
1006 | \r | |
1007 | //\r | |
1008 | // Set the memory range from the start of the 32-bit MMIO area (32-bit PCI\r | |
1009 | // MMIO aperture on i440fx, PCIEXBAR on q35) to 4GB as uncacheable.\r | |
1010 | //\r | |
1011 | Status = MtrrSetMemoryAttribute (\r | |
1012 | PlatformInfoHob->Uc32Base,\r | |
1013 | SIZE_4GB - PlatformInfoHob->Uc32Base,\r | |
1014 | CacheUncacheable\r | |
1015 | );\r | |
1016 | ASSERT_EFI_ERROR (Status);\r | |
1017 | }\r | |
1018 | }\r | |
1019 | \r | |
1020 | VOID\r | |
1021 | EFIAPI\r | |
1022 | PlatformQemuInitializeRamForS3 (\r | |
1023 | IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
1024 | )\r | |
1025 | {\r | |
1026 | if (PlatformInfoHob->S3Supported && (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME)) {\r | |
1027 | //\r | |
1028 | // This is the memory range that will be used for PEI on S3 resume\r | |
1029 | //\r | |
1030 | BuildMemoryAllocationHob (\r | |
1031 | PlatformInfoHob->S3AcpiReservedMemoryBase,\r | |
1032 | PlatformInfoHob->S3AcpiReservedMemorySize,\r | |
1033 | EfiACPIMemoryNVS\r | |
1034 | );\r | |
1035 | \r | |
1036 | //\r | |
1037 | // Cover the initial RAM area used as stack and temporary PEI heap.\r | |
1038 | //\r | |
1039 | // This is reserved as ACPI NVS so it can be used on S3 resume.\r | |
1040 | //\r | |
1041 | BuildMemoryAllocationHob (\r | |
1042 | PcdGet32 (PcdOvmfSecPeiTempRamBase),\r | |
1043 | PcdGet32 (PcdOvmfSecPeiTempRamSize),\r | |
1044 | EfiACPIMemoryNVS\r | |
1045 | );\r | |
1046 | \r | |
1047 | //\r | |
1048 | // SEC stores its table of GUIDed section handlers here.\r | |
1049 | //\r | |
1050 | BuildMemoryAllocationHob (\r | |
1051 | PcdGet64 (PcdGuidedExtractHandlerTableAddress),\r | |
1052 | PcdGet32 (PcdGuidedExtractHandlerTableSize),\r | |
1053 | EfiACPIMemoryNVS\r | |
1054 | );\r | |
1055 | \r | |
1056 | #ifdef MDE_CPU_X64\r | |
1057 | //\r | |
1058 | // Reserve the initial page tables built by the reset vector code.\r | |
1059 | //\r | |
1060 | // Since this memory range will be used by the Reset Vector on S3\r | |
1061 | // resume, it must be reserved as ACPI NVS.\r | |
1062 | //\r | |
1063 | BuildMemoryAllocationHob (\r | |
1064 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecPageTablesBase),\r | |
1065 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecPageTablesSize),\r | |
1066 | EfiACPIMemoryNVS\r | |
1067 | );\r | |
1068 | \r | |
1069 | if (PlatformInfoHob->SevEsIsEnabled) {\r | |
1070 | //\r | |
1071 | // If SEV-ES is enabled, reserve the GHCB-related memory area. This\r | |
1072 | // includes the extra page table used to break down the 2MB page\r | |
1073 | // mapping into 4KB page entries where the GHCB resides and the\r | |
1074 | // GHCB area itself.\r | |
1075 | //\r | |
1076 | // Since this memory range will be used by the Reset Vector on S3\r | |
1077 | // resume, it must be reserved as ACPI NVS.\r | |
1078 | //\r | |
1079 | BuildMemoryAllocationHob (\r | |
1080 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbPageTableBase),\r | |
1081 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbPageTableSize),\r | |
1082 | EfiACPIMemoryNVS\r | |
1083 | );\r | |
1084 | BuildMemoryAllocationHob (\r | |
1085 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbBase),\r | |
1086 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbSize),\r | |
1087 | EfiACPIMemoryNVS\r | |
1088 | );\r | |
1089 | BuildMemoryAllocationHob (\r | |
1090 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupBase),\r | |
1091 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupSize),\r | |
1092 | EfiACPIMemoryNVS\r | |
1093 | );\r | |
1094 | }\r | |
1095 | \r | |
1096 | #endif\r | |
1097 | }\r | |
1098 | \r | |
1099 | if (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME) {\r | |
1100 | if (!PlatformInfoHob->SmmSmramRequire) {\r | |
1101 | //\r | |
1102 | // Reserve the lock box storage area\r | |
1103 | //\r | |
1104 | // Since this memory range will be used on S3 resume, it must be\r | |
1105 | // reserved as ACPI NVS.\r | |
1106 | //\r | |
1107 | // If S3 is unsupported, then various drivers might still write to the\r | |
1108 | // LockBox area. We ought to prevent DXE from serving allocation requests\r | |
1109 | // such that they would overlap the LockBox storage.\r | |
1110 | //\r | |
1111 | ZeroMem (\r | |
1112 | (VOID *)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase),\r | |
1113 | (UINTN)PcdGet32 (PcdOvmfLockBoxStorageSize)\r | |
1114 | );\r | |
1115 | BuildMemoryAllocationHob (\r | |
1116 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase),\r | |
1117 | (UINT64)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageSize),\r | |
1118 | PlatformInfoHob->S3Supported ? EfiACPIMemoryNVS : EfiBootServicesData\r | |
1119 | );\r | |
1120 | }\r | |
1121 | \r | |
1122 | if (PlatformInfoHob->SmmSmramRequire) {\r | |
1123 | UINT32 TsegSize;\r | |
1124 | \r | |
1125 | //\r | |
1126 | // Make sure the TSEG area that we reported as a reserved memory resource\r | |
1127 | // cannot be used for reserved memory allocations.\r | |
1128 | //\r | |
1129 | TsegSize = PlatformInfoHob->Q35TsegMbytes * SIZE_1MB;\r | |
1130 | BuildMemoryAllocationHob (\r | |
1131 | PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob) - TsegSize,\r | |
1132 | TsegSize,\r | |
1133 | EfiReservedMemoryType\r | |
1134 | );\r | |
1135 | //\r | |
1136 | // Similarly, allocate away the (already reserved) SMRAM at the default\r | |
1137 | // SMBASE, if it exists.\r | |
1138 | //\r | |
1139 | if (PlatformInfoHob->Q35SmramAtDefaultSmbase) {\r | |
1140 | BuildMemoryAllocationHob (\r | |
1141 | SMM_DEFAULT_SMBASE,\r | |
1142 | MCH_DEFAULT_SMBASE_SIZE,\r | |
1143 | EfiReservedMemoryType\r | |
1144 | );\r | |
1145 | }\r | |
1146 | }\r | |
1147 | \r | |
1148 | #ifdef MDE_CPU_X64\r | |
1149 | if (FixedPcdGet32 (PcdOvmfWorkAreaSize) != 0) {\r | |
1150 | //\r | |
1151 | // Reserve the work area.\r | |
1152 | //\r | |
1153 | // Since this memory range will be used by the Reset Vector on S3\r | |
1154 | // resume, it must be reserved as ACPI NVS.\r | |
1155 | //\r | |
1156 | // If S3 is unsupported, then various drivers might still write to the\r | |
1157 | // work area. We ought to prevent DXE from serving allocation requests\r | |
1158 | // such that they would overlap the work area.\r | |
1159 | //\r | |
1160 | BuildMemoryAllocationHob (\r | |
1161 | (EFI_PHYSICAL_ADDRESS)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaBase),\r | |
1162 | (UINT64)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaSize),\r | |
1163 | PlatformInfoHob->S3Supported ? EfiACPIMemoryNVS : EfiBootServicesData\r | |
1164 | );\r | |
1165 | }\r | |
1166 | \r | |
1167 | #endif\r | |
1168 | }\r | |
1169 | }\r |