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