]>
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 | |
29 | #include <Library/DebugLib.h>\r | |
30 | #include <Library/HobLib.h>\r | |
31 | #include <Library/IoLib.h>\r | |
32 | #include <Library/MemEncryptSevLib.h>\r | |
33 | #include <Library/PcdLib.h>\r | |
34 | #include <Library/PciLib.h>\r | |
35 | #include <Library/PeimEntryPoint.h>\r | |
36 | #include <Library/ResourcePublicationLib.h>\r | |
37 | #include <Library/MtrrLib.h>\r | |
38 | #include <Library/QemuFwCfgLib.h>\r | |
39 | #include <Library/QemuFwCfgSimpleParserLib.h>\r | |
e23f8f52 MX |
40 | #include <Library/TdxLib.h>\r |
41 | \r | |
10460942 MX |
42 | #include <Library/PlatformInitLib.h>\r |
43 | \r | |
44 | VOID\r | |
45 | EFIAPI\r | |
46 | PlatformQemuUc32BaseInitialization (\r | |
47 | IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
48 | )\r | |
49 | {\r | |
50 | UINT32 LowerMemorySize;\r | |
51 | \r | |
52 | if (PlatformInfoHob->HostBridgeDevId == 0xffff /* microvm */) {\r | |
53 | return;\r | |
54 | }\r | |
55 | \r | |
56 | if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r | |
57 | //\r | |
58 | // On q35, the 32-bit area that we'll mark as UC, through variable MTRRs,\r | |
59 | // starts at PcdPciExpressBaseAddress. The platform DSC is responsible for\r | |
60 | // setting PcdPciExpressBaseAddress such that describing the\r | |
61 | // [PcdPciExpressBaseAddress, 4GB) range require a very small number of\r | |
62 | // variable MTRRs (preferably 1 or 2).\r | |
63 | //\r | |
47f44097 GH |
64 | ASSERT (PcdGet64 (PcdPciExpressBaseAddress) <= MAX_UINT32);\r |
65 | PlatformInfoHob->Uc32Base = (UINT32)PcdGet64 (PcdPciExpressBaseAddress);\r | |
10460942 MX |
66 | return;\r |
67 | }\r | |
68 | \r | |
69 | if (PlatformInfoHob->HostBridgeDevId == CLOUDHV_DEVICE_ID) {\r | |
70 | PlatformInfoHob->Uc32Size = CLOUDHV_MMIO_HOLE_SIZE;\r | |
71 | PlatformInfoHob->Uc32Base = CLOUDHV_MMIO_HOLE_ADDRESS;\r | |
72 | return;\r | |
73 | }\r | |
74 | \r | |
75 | ASSERT (PlatformInfoHob->HostBridgeDevId == INTEL_82441_DEVICE_ID);\r | |
76 | //\r | |
77 | // On i440fx, start with the [LowerMemorySize, 4GB) range. Make sure one\r | |
78 | // variable MTRR suffices by truncating the size to a whole power of two,\r | |
79 | // while keeping the end affixed to 4GB. This will round the base up.\r | |
80 | //\r | |
81 | LowerMemorySize = PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r | |
82 | PlatformInfoHob->Uc32Size = GetPowerOfTwo32 ((UINT32)(SIZE_4GB - LowerMemorySize));\r | |
83 | PlatformInfoHob->Uc32Base = (UINT32)(SIZE_4GB - PlatformInfoHob->Uc32Size);\r | |
84 | //\r | |
85 | // Assuming that LowerMemorySize is at least 1 byte, Uc32Size is at most 2GB.\r | |
86 | // Therefore Uc32Base is at least 2GB.\r | |
87 | //\r | |
88 | ASSERT (PlatformInfoHob->Uc32Base >= BASE_2GB);\r | |
89 | \r | |
90 | if (PlatformInfoHob->Uc32Base != LowerMemorySize) {\r | |
91 | DEBUG ((\r | |
92 | DEBUG_VERBOSE,\r | |
93 | "%a: rounded UC32 base from 0x%x up to 0x%x, for "\r | |
94 | "an UC32 size of 0x%x\n",\r | |
95 | __FUNCTION__,\r | |
96 | LowerMemorySize,\r | |
97 | PlatformInfoHob->Uc32Base,\r | |
98 | PlatformInfoHob->Uc32Size\r | |
99 | ));\r | |
100 | }\r | |
101 | }\r | |
102 | \r | |
103 | /**\r | |
104 | Iterate over the RAM entries in QEMU's fw_cfg E820 RAM map that start outside\r | |
105 | of the 32-bit address range.\r | |
106 | \r | |
107 | Find the highest exclusive >=4GB RAM address, or produce memory resource\r | |
108 | descriptor HOBs for RAM entries that start at or above 4GB.\r | |
109 | \r | |
110 | @param[out] MaxAddress If MaxAddress is NULL, then PlatformScanOrAdd64BitE820Ram()\r | |
111 | produces memory resource descriptor HOBs for RAM\r | |
112 | entries that start at or above 4GB.\r | |
113 | \r | |
114 | Otherwise, MaxAddress holds the highest exclusive\r | |
115 | >=4GB RAM address on output. If QEMU's fw_cfg E820\r | |
116 | RAM map contains no RAM entry that starts outside of\r | |
117 | the 32-bit address range, then MaxAddress is exactly\r | |
118 | 4GB on output.\r | |
119 | \r | |
120 | @retval EFI_SUCCESS The fw_cfg E820 RAM map was found and processed.\r | |
121 | \r | |
122 | @retval EFI_PROTOCOL_ERROR The RAM map was found, but its size wasn't a\r | |
123 | whole multiple of sizeof(EFI_E820_ENTRY64). No\r | |
124 | RAM entry was processed.\r | |
125 | \r | |
126 | @return Error codes from QemuFwCfgFindFile(). No RAM\r | |
127 | entry was processed.\r | |
128 | **/\r | |
129 | STATIC\r | |
130 | EFI_STATUS\r | |
131 | PlatformScanOrAdd64BitE820Ram (\r | |
132 | IN BOOLEAN AddHighHob,\r | |
133 | OUT UINT64 *LowMemory OPTIONAL,\r | |
134 | OUT UINT64 *MaxAddress OPTIONAL\r | |
135 | )\r | |
136 | {\r | |
137 | EFI_STATUS Status;\r | |
138 | FIRMWARE_CONFIG_ITEM FwCfgItem;\r | |
139 | UINTN FwCfgSize;\r | |
140 | EFI_E820_ENTRY64 E820Entry;\r | |
141 | UINTN Processed;\r | |
142 | \r | |
143 | Status = QemuFwCfgFindFile ("etc/e820", &FwCfgItem, &FwCfgSize);\r | |
144 | if (EFI_ERROR (Status)) {\r | |
145 | return Status;\r | |
146 | }\r | |
147 | \r | |
148 | if (FwCfgSize % sizeof E820Entry != 0) {\r | |
149 | return EFI_PROTOCOL_ERROR;\r | |
150 | }\r | |
151 | \r | |
152 | if (LowMemory != NULL) {\r | |
153 | *LowMemory = 0;\r | |
154 | }\r | |
155 | \r | |
156 | if (MaxAddress != NULL) {\r | |
157 | *MaxAddress = BASE_4GB;\r | |
158 | }\r | |
159 | \r | |
160 | QemuFwCfgSelectItem (FwCfgItem);\r | |
161 | for (Processed = 0; Processed < FwCfgSize; Processed += sizeof E820Entry) {\r | |
162 | QemuFwCfgReadBytes (sizeof E820Entry, &E820Entry);\r | |
163 | DEBUG ((\r | |
164 | DEBUG_VERBOSE,\r | |
165 | "%a: Base=0x%Lx Length=0x%Lx Type=%u\n",\r | |
166 | __FUNCTION__,\r | |
167 | E820Entry.BaseAddr,\r | |
168 | E820Entry.Length,\r | |
169 | E820Entry.Type\r | |
170 | ));\r | |
171 | if (E820Entry.Type == EfiAcpiAddressRangeMemory) {\r | |
172 | if (AddHighHob && (E820Entry.BaseAddr >= BASE_4GB)) {\r | |
173 | UINT64 Base;\r | |
174 | UINT64 End;\r | |
175 | \r | |
176 | //\r | |
177 | // Round up the start address, and round down the end address.\r | |
178 | //\r | |
179 | Base = ALIGN_VALUE (E820Entry.BaseAddr, (UINT64)EFI_PAGE_SIZE);\r | |
180 | End = (E820Entry.BaseAddr + E820Entry.Length) &\r | |
181 | ~(UINT64)EFI_PAGE_MASK;\r | |
182 | if (Base < End) {\r | |
183 | PlatformAddMemoryRangeHob (Base, End);\r | |
184 | DEBUG ((\r | |
185 | DEBUG_VERBOSE,\r | |
186 | "%a: PlatformAddMemoryRangeHob [0x%Lx, 0x%Lx)\n",\r | |
187 | __FUNCTION__,\r | |
188 | Base,\r | |
189 | End\r | |
190 | ));\r | |
191 | }\r | |
192 | }\r | |
193 | \r | |
194 | if (MaxAddress || LowMemory) {\r | |
195 | UINT64 Candidate;\r | |
196 | \r | |
197 | Candidate = E820Entry.BaseAddr + E820Entry.Length;\r | |
198 | if (MaxAddress && (Candidate > *MaxAddress)) {\r | |
199 | *MaxAddress = Candidate;\r | |
200 | DEBUG ((\r | |
201 | DEBUG_VERBOSE,\r | |
202 | "%a: MaxAddress=0x%Lx\n",\r | |
203 | __FUNCTION__,\r | |
204 | *MaxAddress\r | |
205 | ));\r | |
206 | }\r | |
207 | \r | |
208 | if (LowMemory && (Candidate > *LowMemory) && (Candidate < BASE_4GB)) {\r | |
209 | *LowMemory = Candidate;\r | |
210 | DEBUG ((\r | |
211 | DEBUG_VERBOSE,\r | |
212 | "%a: LowMemory=0x%Lx\n",\r | |
213 | __FUNCTION__,\r | |
214 | *LowMemory\r | |
215 | ));\r | |
216 | }\r | |
217 | }\r | |
218 | }\r | |
219 | }\r | |
220 | \r | |
221 | return EFI_SUCCESS;\r | |
222 | }\r | |
223 | \r | |
224 | /**\r | |
225 | Returns PVH memmap\r | |
226 | \r | |
227 | @param Entries Pointer to PVH memmap\r | |
228 | @param Count Number of entries\r | |
229 | \r | |
230 | @return EFI_STATUS\r | |
231 | **/\r | |
232 | EFI_STATUS\r | |
233 | GetPvhMemmapEntries (\r | |
234 | struct hvm_memmap_table_entry **Entries,\r | |
235 | UINT32 *Count\r | |
236 | )\r | |
237 | {\r | |
238 | UINT32 *PVHResetVectorData;\r | |
239 | struct hvm_start_info *pvh_start_info;\r | |
240 | \r | |
241 | PVHResetVectorData = (VOID *)(UINTN)PcdGet32 (PcdXenPvhStartOfDayStructPtr);\r | |
242 | if (PVHResetVectorData == 0) {\r | |
243 | return EFI_NOT_FOUND;\r | |
244 | }\r | |
245 | \r | |
246 | pvh_start_info = (struct hvm_start_info *)(UINTN)PVHResetVectorData[0];\r | |
247 | \r | |
248 | *Entries = (struct hvm_memmap_table_entry *)(UINTN)pvh_start_info->memmap_paddr;\r | |
249 | *Count = pvh_start_info->memmap_entries;\r | |
250 | \r | |
251 | return EFI_SUCCESS;\r | |
252 | }\r | |
253 | \r | |
254 | STATIC\r | |
255 | UINT64\r | |
256 | GetHighestSystemMemoryAddressFromPvhMemmap (\r | |
257 | BOOLEAN Below4gb\r | |
258 | )\r | |
259 | {\r | |
260 | struct hvm_memmap_table_entry *Memmap;\r | |
261 | UINT32 MemmapEntriesCount;\r | |
262 | struct hvm_memmap_table_entry *Entry;\r | |
263 | EFI_STATUS Status;\r | |
264 | UINT32 Loop;\r | |
265 | UINT64 HighestAddress;\r | |
266 | UINT64 EntryEnd;\r | |
267 | \r | |
268 | HighestAddress = 0;\r | |
269 | \r | |
270 | Status = GetPvhMemmapEntries (&Memmap, &MemmapEntriesCount);\r | |
271 | ASSERT_EFI_ERROR (Status);\r | |
272 | \r | |
273 | for (Loop = 0; Loop < MemmapEntriesCount; Loop++) {\r | |
274 | Entry = Memmap + Loop;\r | |
275 | EntryEnd = Entry->addr + Entry->size;\r | |
276 | \r | |
277 | if ((Entry->type == XEN_HVM_MEMMAP_TYPE_RAM) &&\r | |
278 | (EntryEnd > HighestAddress))\r | |
279 | {\r | |
280 | if (Below4gb && (EntryEnd <= BASE_4GB)) {\r | |
281 | HighestAddress = EntryEnd;\r | |
282 | } else if (!Below4gb && (EntryEnd >= BASE_4GB)) {\r | |
283 | HighestAddress = EntryEnd;\r | |
284 | }\r | |
285 | }\r | |
286 | }\r | |
287 | \r | |
288 | return HighestAddress;\r | |
289 | }\r | |
290 | \r | |
291 | UINT32\r | |
292 | EFIAPI\r | |
293 | PlatformGetSystemMemorySizeBelow4gb (\r | |
294 | IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
295 | )\r | |
296 | {\r | |
297 | EFI_STATUS Status;\r | |
298 | UINT64 LowerMemorySize = 0;\r | |
299 | UINT8 Cmos0x34;\r | |
300 | UINT8 Cmos0x35;\r | |
301 | \r | |
302 | if (PlatformInfoHob->HostBridgeDevId == CLOUDHV_DEVICE_ID) {\r | |
303 | // Get the information from PVH memmap\r | |
304 | return (UINT32)GetHighestSystemMemoryAddressFromPvhMemmap (TRUE);\r | |
305 | }\r | |
306 | \r | |
307 | Status = PlatformScanOrAdd64BitE820Ram (FALSE, &LowerMemorySize, NULL);\r | |
308 | if ((Status == EFI_SUCCESS) && (LowerMemorySize > 0)) {\r | |
309 | return (UINT32)LowerMemorySize;\r | |
310 | }\r | |
311 | \r | |
312 | //\r | |
313 | // CMOS 0x34/0x35 specifies the system memory above 16 MB.\r | |
314 | // * CMOS(0x35) is the high byte\r | |
315 | // * CMOS(0x34) is the low byte\r | |
316 | // * The size is specified in 64kb chunks\r | |
317 | // * Since this is memory above 16MB, the 16MB must be added\r | |
318 | // into the calculation to get the total memory size.\r | |
319 | //\r | |
320 | \r | |
321 | Cmos0x34 = (UINT8)PlatformCmosRead8 (0x34);\r | |
322 | Cmos0x35 = (UINT8)PlatformCmosRead8 (0x35);\r | |
323 | \r | |
324 | return (UINT32)(((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r | |
325 | }\r | |
326 | \r | |
327 | STATIC\r | |
328 | UINT64\r | |
329 | PlatformGetSystemMemorySizeAbove4gb (\r | |
330 | )\r | |
331 | {\r | |
332 | UINT32 Size;\r | |
333 | UINTN CmosIndex;\r | |
334 | \r | |
335 | //\r | |
336 | // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.\r | |
337 | // * CMOS(0x5d) is the most significant size byte\r | |
338 | // * CMOS(0x5c) is the middle size byte\r | |
339 | // * CMOS(0x5b) is the least significant size byte\r | |
340 | // * The size is specified in 64kb chunks\r | |
341 | //\r | |
342 | \r | |
343 | Size = 0;\r | |
344 | for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {\r | |
345 | Size = (UINT32)(Size << 8) + (UINT32)PlatformCmosRead8 (CmosIndex);\r | |
346 | }\r | |
347 | \r | |
348 | return LShiftU64 (Size, 16);\r | |
349 | }\r | |
350 | \r | |
351 | /**\r | |
352 | Return the highest address that DXE could possibly use, plus one.\r | |
353 | **/\r | |
354 | STATIC\r | |
355 | UINT64\r | |
356 | PlatformGetFirstNonAddress (\r | |
357 | IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
358 | )\r | |
359 | {\r | |
360 | UINT64 FirstNonAddress;\r | |
361 | UINT32 FwCfgPciMmio64Mb;\r | |
362 | EFI_STATUS Status;\r | |
363 | FIRMWARE_CONFIG_ITEM FwCfgItem;\r | |
364 | UINTN FwCfgSize;\r | |
365 | UINT64 HotPlugMemoryEnd;\r | |
366 | \r | |
367 | //\r | |
368 | // set FirstNonAddress to suppress incorrect compiler/analyzer warnings\r | |
369 | //\r | |
370 | FirstNonAddress = 0;\r | |
371 | \r | |
372 | //\r | |
373 | // If QEMU presents an E820 map, then get the highest exclusive >=4GB RAM\r | |
374 | // address from it. This can express an address >= 4GB+1TB.\r | |
375 | //\r | |
376 | // Otherwise, get the flat size of the memory above 4GB from the CMOS (which\r | |
377 | // can only express a size smaller than 1TB), and add it to 4GB.\r | |
378 | //\r | |
379 | Status = PlatformScanOrAdd64BitE820Ram (FALSE, NULL, &FirstNonAddress);\r | |
380 | if (EFI_ERROR (Status)) {\r | |
381 | FirstNonAddress = BASE_4GB + PlatformGetSystemMemorySizeAbove4gb ();\r | |
382 | }\r | |
383 | \r | |
384 | //\r | |
385 | // If DXE is 32-bit, then we're done; PciBusDxe will degrade 64-bit MMIO\r | |
386 | // resources to 32-bit anyway. See DegradeResource() in\r | |
387 | // "PciResourceSupport.c".\r | |
388 | //\r | |
389 | #ifdef MDE_CPU_IA32\r | |
390 | if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r | |
391 | return FirstNonAddress;\r | |
392 | }\r | |
393 | \r | |
394 | #endif\r | |
395 | \r | |
396 | //\r | |
397 | // See if the user specified the number of megabytes for the 64-bit PCI host\r | |
398 | // aperture. Accept an aperture size up to 16TB.\r | |
399 | //\r | |
400 | // As signaled by the "X-" prefix, this knob is experimental, and might go\r | |
401 | // away at any time.\r | |
402 | //\r | |
403 | Status = QemuFwCfgParseUint32 (\r | |
404 | "opt/ovmf/X-PciMmio64Mb",\r | |
405 | FALSE,\r | |
406 | &FwCfgPciMmio64Mb\r | |
407 | );\r | |
408 | switch (Status) {\r | |
409 | case EFI_UNSUPPORTED:\r | |
410 | case EFI_NOT_FOUND:\r | |
411 | break;\r | |
412 | case EFI_SUCCESS:\r | |
413 | if (FwCfgPciMmio64Mb <= 0x1000000) {\r | |
414 | PlatformInfoHob->PcdPciMmio64Size = LShiftU64 (FwCfgPciMmio64Mb, 20);\r | |
415 | break;\r | |
416 | }\r | |
417 | \r | |
418 | //\r | |
419 | // fall through\r | |
420 | //\r | |
421 | default:\r | |
422 | DEBUG ((\r | |
423 | DEBUG_WARN,\r | |
424 | "%a: ignoring malformed 64-bit PCI host aperture size from fw_cfg\n",\r | |
425 | __FUNCTION__\r | |
426 | ));\r | |
427 | break;\r | |
428 | }\r | |
429 | \r | |
430 | if (PlatformInfoHob->PcdPciMmio64Size == 0) {\r | |
431 | if (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME) {\r | |
432 | DEBUG ((\r | |
433 | DEBUG_INFO,\r | |
434 | "%a: disabling 64-bit PCI host aperture\n",\r | |
435 | __FUNCTION__\r | |
436 | ));\r | |
437 | }\r | |
438 | \r | |
439 | //\r | |
440 | // There's nothing more to do; the amount of memory above 4GB fully\r | |
441 | // determines the highest address plus one. The memory hotplug area (see\r | |
442 | // below) plays no role for the firmware in this case.\r | |
443 | //\r | |
444 | return FirstNonAddress;\r | |
445 | }\r | |
446 | \r | |
447 | //\r | |
448 | // The "etc/reserved-memory-end" fw_cfg file, when present, contains an\r | |
449 | // absolute, exclusive end address for the memory hotplug area. This area\r | |
450 | // starts right at the end of the memory above 4GB. The 64-bit PCI host\r | |
451 | // aperture must be placed above it.\r | |
452 | //\r | |
453 | Status = QemuFwCfgFindFile (\r | |
454 | "etc/reserved-memory-end",\r | |
455 | &FwCfgItem,\r | |
456 | &FwCfgSize\r | |
457 | );\r | |
458 | if (!EFI_ERROR (Status) && (FwCfgSize == sizeof HotPlugMemoryEnd)) {\r | |
459 | QemuFwCfgSelectItem (FwCfgItem);\r | |
460 | QemuFwCfgReadBytes (FwCfgSize, &HotPlugMemoryEnd);\r | |
461 | DEBUG ((\r | |
462 | DEBUG_VERBOSE,\r | |
463 | "%a: HotPlugMemoryEnd=0x%Lx\n",\r | |
464 | __FUNCTION__,\r | |
465 | HotPlugMemoryEnd\r | |
466 | ));\r | |
467 | \r | |
468 | ASSERT (HotPlugMemoryEnd >= FirstNonAddress);\r | |
469 | FirstNonAddress = HotPlugMemoryEnd;\r | |
470 | }\r | |
471 | \r | |
472 | //\r | |
473 | // SeaBIOS aligns both boundaries of the 64-bit PCI host aperture to 1GB, so\r | |
474 | // that the host can map it with 1GB hugepages. Follow suit.\r | |
475 | //\r | |
476 | PlatformInfoHob->PcdPciMmio64Base = ALIGN_VALUE (FirstNonAddress, (UINT64)SIZE_1GB);\r | |
477 | PlatformInfoHob->PcdPciMmio64Size = ALIGN_VALUE (PlatformInfoHob->PcdPciMmio64Size, (UINT64)SIZE_1GB);\r | |
478 | \r | |
479 | //\r | |
480 | // The 64-bit PCI host aperture should also be "naturally" aligned. The\r | |
481 | // alignment is determined by rounding the size of the aperture down to the\r | |
482 | // next smaller or equal power of two. That is, align the aperture by the\r | |
483 | // largest BAR size that can fit into it.\r | |
484 | //\r | |
485 | PlatformInfoHob->PcdPciMmio64Base = ALIGN_VALUE (PlatformInfoHob->PcdPciMmio64Base, GetPowerOfTwo64 (PlatformInfoHob->PcdPciMmio64Size));\r | |
486 | \r | |
487 | //\r | |
488 | // The useful address space ends with the 64-bit PCI host aperture.\r | |
489 | //\r | |
490 | FirstNonAddress = PlatformInfoHob->PcdPciMmio64Base + PlatformInfoHob->PcdPciMmio64Size;\r | |
491 | return FirstNonAddress;\r | |
492 | }\r | |
493 | \r | |
494 | /**\r | |
495 | Initialize the PhysMemAddressWidth field in PlatformInfoHob based on guest RAM size.\r | |
496 | **/\r | |
497 | VOID\r | |
498 | EFIAPI\r | |
499 | PlatformAddressWidthInitialization (\r | |
500 | IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
501 | )\r | |
502 | {\r | |
503 | UINT64 FirstNonAddress;\r | |
504 | UINT8 PhysMemAddressWidth;\r | |
505 | \r | |
506 | //\r | |
507 | // As guest-physical memory size grows, the permanent PEI RAM requirements\r | |
508 | // are dominated by the identity-mapping page tables built by the DXE IPL.\r | |
509 | // The DXL IPL keys off of the physical address bits advertized in the CPU\r | |
510 | // HOB. To conserve memory, we calculate the minimum address width here.\r | |
511 | //\r | |
512 | FirstNonAddress = PlatformGetFirstNonAddress (PlatformInfoHob);\r | |
513 | PhysMemAddressWidth = (UINT8)HighBitSet64 (FirstNonAddress);\r | |
514 | \r | |
515 | //\r | |
516 | // If FirstNonAddress is not an integral power of two, then we need an\r | |
517 | // additional bit.\r | |
518 | //\r | |
519 | if ((FirstNonAddress & (FirstNonAddress - 1)) != 0) {\r | |
520 | ++PhysMemAddressWidth;\r | |
521 | }\r | |
522 | \r | |
523 | //\r | |
524 | // The minimum address width is 36 (covers up to and excluding 64 GB, which\r | |
525 | // is the maximum for Ia32 + PAE). The theoretical architecture maximum for\r | |
526 | // X64 long mode is 52 bits, but the DXE IPL clamps that down to 48 bits. We\r | |
527 | // can simply assert that here, since 48 bits are good enough for 256 TB.\r | |
528 | //\r | |
529 | if (PhysMemAddressWidth <= 36) {\r | |
530 | PhysMemAddressWidth = 36;\r | |
531 | }\r | |
532 | \r | |
e23f8f52 MX |
533 | #if defined (MDE_CPU_X64)\r |
534 | if (TdIsEnabled ()) {\r | |
535 | if (TdSharedPageMask () == (1ULL << 47)) {\r | |
536 | PhysMemAddressWidth = 48;\r | |
537 | } else {\r | |
538 | PhysMemAddressWidth = 52;\r | |
539 | }\r | |
540 | }\r | |
541 | \r | |
542 | ASSERT (PhysMemAddressWidth <= 52);\r | |
543 | #else\r | |
10460942 | 544 | ASSERT (PhysMemAddressWidth <= 48);\r |
e23f8f52 | 545 | #endif\r |
10460942 MX |
546 | \r |
547 | PlatformInfoHob->FirstNonAddress = FirstNonAddress;\r | |
548 | PlatformInfoHob->PhysMemAddressWidth = PhysMemAddressWidth;\r | |
549 | }\r | |
550 | \r | |
551 | STATIC\r | |
552 | VOID\r | |
553 | QemuInitializeRamBelow1gb (\r | |
554 | IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
555 | )\r | |
556 | {\r | |
557 | if (PlatformInfoHob->SmmSmramRequire && PlatformInfoHob->Q35SmramAtDefaultSmbase) {\r | |
558 | PlatformAddMemoryRangeHob (0, SMM_DEFAULT_SMBASE);\r | |
559 | PlatformAddReservedMemoryBaseSizeHob (\r | |
560 | SMM_DEFAULT_SMBASE,\r | |
561 | MCH_DEFAULT_SMBASE_SIZE,\r | |
562 | TRUE /* Cacheable */\r | |
563 | );\r | |
564 | STATIC_ASSERT (\r | |
565 | SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE < BASE_512KB + BASE_128KB,\r | |
566 | "end of SMRAM at default SMBASE ends at, or exceeds, 640KB"\r | |
567 | );\r | |
568 | PlatformAddMemoryRangeHob (\r | |
569 | SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE,\r | |
570 | BASE_512KB + BASE_128KB\r | |
571 | );\r | |
572 | } else {\r | |
573 | PlatformAddMemoryRangeHob (0, BASE_512KB + BASE_128KB);\r | |
574 | }\r | |
575 | }\r | |
576 | \r | |
577 | /**\r | |
578 | Peform Memory Detection for QEMU / KVM\r | |
579 | \r | |
580 | **/\r | |
581 | VOID\r | |
582 | EFIAPI\r | |
583 | PlatformQemuInitializeRam (\r | |
584 | IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
585 | )\r | |
586 | {\r | |
587 | UINT64 LowerMemorySize;\r | |
588 | UINT64 UpperMemorySize;\r | |
589 | MTRR_SETTINGS MtrrSettings;\r | |
590 | EFI_STATUS Status;\r | |
591 | \r | |
592 | DEBUG ((DEBUG_INFO, "%a called\n", __FUNCTION__));\r | |
593 | \r | |
594 | //\r | |
595 | // Determine total memory size available\r | |
596 | //\r | |
597 | LowerMemorySize = PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r | |
598 | \r | |
599 | if (PlatformInfoHob->BootMode == BOOT_ON_S3_RESUME) {\r | |
600 | //\r | |
601 | // Create the following memory HOB as an exception on the S3 boot path.\r | |
602 | //\r | |
603 | // Normally we'd create memory HOBs only on the normal boot path. However,\r | |
604 | // CpuMpPei specifically needs such a low-memory HOB on the S3 path as\r | |
605 | // well, for "borrowing" a subset of it temporarily, for the AP startup\r | |
606 | // vector.\r | |
607 | //\r | |
608 | // CpuMpPei saves the original contents of the borrowed area in permanent\r | |
609 | // PEI RAM, in a backup buffer allocated with the normal PEI services.\r | |
610 | // CpuMpPei restores the original contents ("returns" the borrowed area) at\r | |
611 | // End-of-PEI. End-of-PEI in turn is emitted by S3Resume2Pei before\r | |
612 | // transferring control to the OS's wakeup vector in the FACS.\r | |
613 | //\r | |
614 | // We expect any other PEIMs that "borrow" memory similarly to CpuMpPei to\r | |
615 | // restore the original contents. Furthermore, we expect all such PEIMs\r | |
616 | // (CpuMpPei included) to claim the borrowed areas by producing memory\r | |
617 | // allocation HOBs, and to honor preexistent memory allocation HOBs when\r | |
618 | // looking for an area to borrow.\r | |
619 | //\r | |
620 | QemuInitializeRamBelow1gb (PlatformInfoHob);\r | |
621 | } else {\r | |
622 | //\r | |
623 | // Create memory HOBs\r | |
624 | //\r | |
625 | QemuInitializeRamBelow1gb (PlatformInfoHob);\r | |
626 | \r | |
627 | if (PlatformInfoHob->SmmSmramRequire) {\r | |
628 | UINT32 TsegSize;\r | |
629 | \r | |
630 | TsegSize = PlatformInfoHob->Q35TsegMbytes * SIZE_1MB;\r | |
631 | PlatformAddMemoryRangeHob (BASE_1MB, LowerMemorySize - TsegSize);\r | |
632 | PlatformAddReservedMemoryBaseSizeHob (\r | |
633 | LowerMemorySize - TsegSize,\r | |
634 | TsegSize,\r | |
635 | TRUE\r | |
636 | );\r | |
637 | } else {\r | |
638 | PlatformAddMemoryRangeHob (BASE_1MB, LowerMemorySize);\r | |
639 | }\r | |
640 | \r | |
641 | //\r | |
642 | // If QEMU presents an E820 map, then create memory HOBs for the >=4GB RAM\r | |
643 | // entries. Otherwise, create a single memory HOB with the flat >=4GB\r | |
644 | // memory size read from the CMOS.\r | |
645 | //\r | |
646 | Status = PlatformScanOrAdd64BitE820Ram (TRUE, NULL, NULL);\r | |
647 | if (EFI_ERROR (Status)) {\r | |
648 | UpperMemorySize = PlatformGetSystemMemorySizeAbove4gb ();\r | |
649 | if (UpperMemorySize != 0) {\r | |
650 | PlatformAddMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);\r | |
651 | }\r | |
652 | }\r | |
653 | }\r | |
654 | \r | |
655 | //\r | |
656 | // We'd like to keep the following ranges uncached:\r | |
657 | // - [640 KB, 1 MB)\r | |
658 | // - [LowerMemorySize, 4 GB)\r | |
659 | //\r | |
660 | // Everything else should be WB. Unfortunately, programming the inverse (ie.\r | |
661 | // keeping the default UC, and configuring the complement set of the above as\r | |
662 | // WB) is not reliable in general, because the end of the upper RAM can have\r | |
663 | // practically any alignment, and we may not have enough variable MTRRs to\r | |
664 | // cover it exactly.\r | |
665 | //\r | |
666 | if (IsMtrrSupported () && (PlatformInfoHob->HostBridgeDevId != CLOUDHV_DEVICE_ID)) {\r | |
667 | MtrrGetAllMtrrs (&MtrrSettings);\r | |
668 | \r | |
669 | //\r | |
670 | // MTRRs disabled, fixed MTRRs disabled, default type is uncached\r | |
671 | //\r | |
672 | ASSERT ((MtrrSettings.MtrrDefType & BIT11) == 0);\r | |
673 | ASSERT ((MtrrSettings.MtrrDefType & BIT10) == 0);\r | |
674 | ASSERT ((MtrrSettings.MtrrDefType & 0xFF) == 0);\r | |
675 | \r | |
676 | //\r | |
677 | // flip default type to writeback\r | |
678 | //\r | |
679 | SetMem (&MtrrSettings.Fixed, sizeof MtrrSettings.Fixed, 0x06);\r | |
680 | ZeroMem (&MtrrSettings.Variables, sizeof MtrrSettings.Variables);\r | |
681 | MtrrSettings.MtrrDefType |= BIT11 | BIT10 | 6;\r | |
682 | MtrrSetAllMtrrs (&MtrrSettings);\r | |
683 | \r | |
684 | //\r | |
685 | // Set memory range from 640KB to 1MB to uncacheable\r | |
686 | //\r | |
687 | Status = MtrrSetMemoryAttribute (\r | |
688 | BASE_512KB + BASE_128KB,\r | |
689 | BASE_1MB - (BASE_512KB + BASE_128KB),\r | |
690 | CacheUncacheable\r | |
691 | );\r | |
692 | ASSERT_EFI_ERROR (Status);\r | |
693 | \r | |
694 | //\r | |
695 | // Set the memory range from the start of the 32-bit MMIO area (32-bit PCI\r | |
696 | // MMIO aperture on i440fx, PCIEXBAR on q35) to 4GB as uncacheable.\r | |
697 | //\r | |
698 | Status = MtrrSetMemoryAttribute (\r | |
699 | PlatformInfoHob->Uc32Base,\r | |
700 | SIZE_4GB - PlatformInfoHob->Uc32Base,\r | |
701 | CacheUncacheable\r | |
702 | );\r | |
703 | ASSERT_EFI_ERROR (Status);\r | |
704 | }\r | |
705 | }\r | |
706 | \r | |
707 | VOID\r | |
708 | EFIAPI\r | |
709 | PlatformQemuInitializeRamForS3 (\r | |
710 | IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r | |
711 | )\r | |
712 | {\r | |
713 | if (PlatformInfoHob->S3Supported && (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME)) {\r | |
714 | //\r | |
715 | // This is the memory range that will be used for PEI on S3 resume\r | |
716 | //\r | |
717 | BuildMemoryAllocationHob (\r | |
718 | PlatformInfoHob->S3AcpiReservedMemoryBase,\r | |
719 | PlatformInfoHob->S3AcpiReservedMemorySize,\r | |
720 | EfiACPIMemoryNVS\r | |
721 | );\r | |
722 | \r | |
723 | //\r | |
724 | // Cover the initial RAM area used as stack and temporary PEI heap.\r | |
725 | //\r | |
726 | // This is reserved as ACPI NVS so it can be used on S3 resume.\r | |
727 | //\r | |
728 | BuildMemoryAllocationHob (\r | |
729 | PcdGet32 (PcdOvmfSecPeiTempRamBase),\r | |
730 | PcdGet32 (PcdOvmfSecPeiTempRamSize),\r | |
731 | EfiACPIMemoryNVS\r | |
732 | );\r | |
733 | \r | |
734 | //\r | |
735 | // SEC stores its table of GUIDed section handlers here.\r | |
736 | //\r | |
737 | BuildMemoryAllocationHob (\r | |
738 | PcdGet64 (PcdGuidedExtractHandlerTableAddress),\r | |
739 | PcdGet32 (PcdGuidedExtractHandlerTableSize),\r | |
740 | EfiACPIMemoryNVS\r | |
741 | );\r | |
742 | \r | |
743 | #ifdef MDE_CPU_X64\r | |
744 | //\r | |
745 | // Reserve the initial page tables built by the reset vector code.\r | |
746 | //\r | |
747 | // Since this memory range will be used by the Reset Vector on S3\r | |
748 | // resume, it must be reserved as ACPI NVS.\r | |
749 | //\r | |
750 | BuildMemoryAllocationHob (\r | |
751 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecPageTablesBase),\r | |
752 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecPageTablesSize),\r | |
753 | EfiACPIMemoryNVS\r | |
754 | );\r | |
755 | \r | |
756 | if (PlatformInfoHob->SevEsIsEnabled) {\r | |
757 | //\r | |
758 | // If SEV-ES is enabled, reserve the GHCB-related memory area. This\r | |
759 | // includes the extra page table used to break down the 2MB page\r | |
760 | // mapping into 4KB page entries where the GHCB resides and the\r | |
761 | // GHCB area itself.\r | |
762 | //\r | |
763 | // Since this memory range will be used by the Reset Vector on S3\r | |
764 | // resume, it must be reserved as ACPI NVS.\r | |
765 | //\r | |
766 | BuildMemoryAllocationHob (\r | |
767 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbPageTableBase),\r | |
768 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbPageTableSize),\r | |
769 | EfiACPIMemoryNVS\r | |
770 | );\r | |
771 | BuildMemoryAllocationHob (\r | |
772 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbBase),\r | |
773 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbSize),\r | |
774 | EfiACPIMemoryNVS\r | |
775 | );\r | |
776 | BuildMemoryAllocationHob (\r | |
777 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupBase),\r | |
778 | (UINT64)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupSize),\r | |
779 | EfiACPIMemoryNVS\r | |
780 | );\r | |
781 | }\r | |
782 | \r | |
783 | #endif\r | |
784 | }\r | |
785 | \r | |
786 | if (PlatformInfoHob->BootMode != BOOT_ON_S3_RESUME) {\r | |
787 | if (!PlatformInfoHob->SmmSmramRequire) {\r | |
788 | //\r | |
789 | // Reserve the lock box storage area\r | |
790 | //\r | |
791 | // Since this memory range will be used on S3 resume, it must be\r | |
792 | // reserved as ACPI NVS.\r | |
793 | //\r | |
794 | // If S3 is unsupported, then various drivers might still write to the\r | |
795 | // LockBox area. We ought to prevent DXE from serving allocation requests\r | |
796 | // such that they would overlap the LockBox storage.\r | |
797 | //\r | |
798 | ZeroMem (\r | |
799 | (VOID *)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase),\r | |
800 | (UINTN)PcdGet32 (PcdOvmfLockBoxStorageSize)\r | |
801 | );\r | |
802 | BuildMemoryAllocationHob (\r | |
803 | (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageBase),\r | |
804 | (UINT64)(UINTN)PcdGet32 (PcdOvmfLockBoxStorageSize),\r | |
805 | PlatformInfoHob->S3Supported ? EfiACPIMemoryNVS : EfiBootServicesData\r | |
806 | );\r | |
807 | }\r | |
808 | \r | |
809 | if (PlatformInfoHob->SmmSmramRequire) {\r | |
810 | UINT32 TsegSize;\r | |
811 | \r | |
812 | //\r | |
813 | // Make sure the TSEG area that we reported as a reserved memory resource\r | |
814 | // cannot be used for reserved memory allocations.\r | |
815 | //\r | |
816 | TsegSize = PlatformInfoHob->Q35TsegMbytes * SIZE_1MB;\r | |
817 | BuildMemoryAllocationHob (\r | |
818 | PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob) - TsegSize,\r | |
819 | TsegSize,\r | |
820 | EfiReservedMemoryType\r | |
821 | );\r | |
822 | //\r | |
823 | // Similarly, allocate away the (already reserved) SMRAM at the default\r | |
824 | // SMBASE, if it exists.\r | |
825 | //\r | |
826 | if (PlatformInfoHob->Q35SmramAtDefaultSmbase) {\r | |
827 | BuildMemoryAllocationHob (\r | |
828 | SMM_DEFAULT_SMBASE,\r | |
829 | MCH_DEFAULT_SMBASE_SIZE,\r | |
830 | EfiReservedMemoryType\r | |
831 | );\r | |
832 | }\r | |
833 | }\r | |
834 | \r | |
835 | #ifdef MDE_CPU_X64\r | |
836 | if (FixedPcdGet32 (PcdOvmfWorkAreaSize) != 0) {\r | |
837 | //\r | |
838 | // Reserve the work area.\r | |
839 | //\r | |
840 | // Since this memory range will be used by the Reset Vector on S3\r | |
841 | // resume, it must be reserved as ACPI NVS.\r | |
842 | //\r | |
843 | // If S3 is unsupported, then various drivers might still write to the\r | |
844 | // work area. We ought to prevent DXE from serving allocation requests\r | |
845 | // such that they would overlap the work area.\r | |
846 | //\r | |
847 | BuildMemoryAllocationHob (\r | |
848 | (EFI_PHYSICAL_ADDRESS)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaBase),\r | |
849 | (UINT64)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaSize),\r | |
850 | PlatformInfoHob->S3Supported ? EfiACPIMemoryNVS : EfiBootServicesData\r | |
851 | );\r | |
852 | }\r | |
853 | \r | |
854 | #endif\r | |
855 | }\r | |
856 | }\r |