]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - OvmfPkg/PlatformPei/Platform.c
OvmfPkg/Microvm: PlatformPei/Platform memory map tweaks
[mirror_edk2.git] / OvmfPkg / PlatformPei / Platform.c
... / ...
CommitLineData
1/**@file\r
2 Platform PEI driver\r
3\r
4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
5 Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>\r
6\r
7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
8\r
9**/\r
10\r
11//\r
12// The package level header files this module uses\r
13//\r
14#include <PiPei.h>\r
15\r
16//\r
17// The Library classes this module consumes\r
18//\r
19#include <Library/BaseLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/HobLib.h>\r
22#include <Library/IoLib.h>\r
23#include <Library/MemoryAllocationLib.h>\r
24#include <Library/PcdLib.h>\r
25#include <Library/PciLib.h>\r
26#include <Library/PeimEntryPoint.h>\r
27#include <Library/PeiServicesLib.h>\r
28#include <Library/QemuFwCfgLib.h>\r
29#include <Library/QemuFwCfgS3Lib.h>\r
30#include <Library/QemuFwCfgSimpleParserLib.h>\r
31#include <Library/ResourcePublicationLib.h>\r
32#include <Ppi/MasterBootMode.h>\r
33#include <IndustryStandard/I440FxPiix4.h>\r
34#include <IndustryStandard/Pci22.h>\r
35#include <IndustryStandard/Q35MchIch9.h>\r
36#include <IndustryStandard/QemuCpuHotplug.h>\r
37#include <OvmfPlatforms.h>\r
38\r
39#include "Platform.h"\r
40#include "Cmos.h"\r
41\r
42EFI_PEI_PPI_DESCRIPTOR mPpiBootMode[] = {\r
43 {\r
44 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
45 &gEfiPeiMasterBootModePpiGuid,\r
46 NULL\r
47 }\r
48};\r
49\r
50\r
51UINT16 mHostBridgeDevId;\r
52\r
53EFI_BOOT_MODE mBootMode = BOOT_WITH_FULL_CONFIGURATION;\r
54\r
55BOOLEAN mS3Supported = FALSE;\r
56\r
57UINT32 mMaxCpuCount;\r
58\r
59VOID\r
60AddIoMemoryBaseSizeHob (\r
61 EFI_PHYSICAL_ADDRESS MemoryBase,\r
62 UINT64 MemorySize\r
63 )\r
64{\r
65 BuildResourceDescriptorHob (\r
66 EFI_RESOURCE_MEMORY_MAPPED_IO,\r
67 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
68 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
69 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
70 EFI_RESOURCE_ATTRIBUTE_TESTED,\r
71 MemoryBase,\r
72 MemorySize\r
73 );\r
74}\r
75\r
76VOID\r
77AddReservedMemoryBaseSizeHob (\r
78 EFI_PHYSICAL_ADDRESS MemoryBase,\r
79 UINT64 MemorySize,\r
80 BOOLEAN Cacheable\r
81 )\r
82{\r
83 BuildResourceDescriptorHob (\r
84 EFI_RESOURCE_MEMORY_RESERVED,\r
85 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
86 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
87 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
88 (Cacheable ?\r
89 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
90 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
91 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE :\r
92 0\r
93 ) |\r
94 EFI_RESOURCE_ATTRIBUTE_TESTED,\r
95 MemoryBase,\r
96 MemorySize\r
97 );\r
98}\r
99\r
100VOID\r
101AddIoMemoryRangeHob (\r
102 EFI_PHYSICAL_ADDRESS MemoryBase,\r
103 EFI_PHYSICAL_ADDRESS MemoryLimit\r
104 )\r
105{\r
106 AddIoMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));\r
107}\r
108\r
109\r
110VOID\r
111AddMemoryBaseSizeHob (\r
112 EFI_PHYSICAL_ADDRESS MemoryBase,\r
113 UINT64 MemorySize\r
114 )\r
115{\r
116 BuildResourceDescriptorHob (\r
117 EFI_RESOURCE_SYSTEM_MEMORY,\r
118 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
119 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
120 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
121 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
122 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
123 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |\r
124 EFI_RESOURCE_ATTRIBUTE_TESTED,\r
125 MemoryBase,\r
126 MemorySize\r
127 );\r
128}\r
129\r
130\r
131VOID\r
132AddMemoryRangeHob (\r
133 EFI_PHYSICAL_ADDRESS MemoryBase,\r
134 EFI_PHYSICAL_ADDRESS MemoryLimit\r
135 )\r
136{\r
137 AddMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));\r
138}\r
139\r
140\r
141VOID\r
142MemMapInitialization (\r
143 VOID\r
144 )\r
145{\r
146 UINT64 PciIoBase;\r
147 UINT64 PciIoSize;\r
148 RETURN_STATUS PcdStatus;\r
149 UINT32 TopOfLowRam;\r
150 UINT64 PciExBarBase;\r
151 UINT32 PciBase;\r
152 UINT32 PciSize;\r
153\r
154 PciIoBase = 0xC000;\r
155 PciIoSize = 0x4000;\r
156\r
157 //\r
158 // Video memory + Legacy BIOS region\r
159 //\r
160 AddIoMemoryRangeHob (0x0A0000, BASE_1MB);\r
161\r
162 if (mHostBridgeDevId == 0xffff /* microvm */) {\r
163 AddIoMemoryBaseSizeHob (0xFEC00000, SIZE_4KB); /* ioapic #1 */\r
164 AddIoMemoryBaseSizeHob (0xFEC10000, SIZE_4KB); /* ioapic #2 */\r
165 return;\r
166 }\r
167\r
168 TopOfLowRam = GetSystemMemorySizeBelow4gb ();\r
169 PciExBarBase = 0;\r
170 if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
171 //\r
172 // The MMCONFIG area is expected to fall between the top of low RAM and\r
173 // the base of the 32-bit PCI host aperture.\r
174 //\r
175 PciExBarBase = FixedPcdGet64 (PcdPciExpressBaseAddress);\r
176 ASSERT (TopOfLowRam <= PciExBarBase);\r
177 ASSERT (PciExBarBase <= MAX_UINT32 - SIZE_256MB);\r
178 PciBase = (UINT32)(PciExBarBase + SIZE_256MB);\r
179 } else {\r
180 ASSERT (TopOfLowRam <= mQemuUc32Base);\r
181 PciBase = mQemuUc32Base;\r
182 }\r
183\r
184 //\r
185 // address purpose size\r
186 // ------------ -------- -------------------------\r
187 // max(top, 2g) PCI MMIO 0xFC000000 - max(top, 2g)\r
188 // 0xFC000000 gap 44 MB\r
189 // 0xFEC00000 IO-APIC 4 KB\r
190 // 0xFEC01000 gap 1020 KB\r
191 // 0xFED00000 HPET 1 KB\r
192 // 0xFED00400 gap 111 KB\r
193 // 0xFED1C000 gap (PIIX4) / RCRB (ICH9) 16 KB\r
194 // 0xFED20000 gap 896 KB\r
195 // 0xFEE00000 LAPIC 1 MB\r
196 //\r
197 PciSize = 0xFC000000 - PciBase;\r
198 AddIoMemoryBaseSizeHob (PciBase, PciSize);\r
199 PcdStatus = PcdSet64S (PcdPciMmio32Base, PciBase);\r
200 ASSERT_RETURN_ERROR (PcdStatus);\r
201 PcdStatus = PcdSet64S (PcdPciMmio32Size, PciSize);\r
202 ASSERT_RETURN_ERROR (PcdStatus);\r
203\r
204 AddIoMemoryBaseSizeHob (0xFEC00000, SIZE_4KB);\r
205 AddIoMemoryBaseSizeHob (0xFED00000, SIZE_1KB);\r
206 if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
207 AddIoMemoryBaseSizeHob (ICH9_ROOT_COMPLEX_BASE, SIZE_16KB);\r
208 //\r
209 // Note: there should be an\r
210 //\r
211 // AddIoMemoryBaseSizeHob (PciExBarBase, SIZE_256MB);\r
212 //\r
213 // call below, just like the one above for RCBA. However, Linux insists\r
214 // that the MMCONFIG area be marked in the E820 or UEFI memory map as\r
215 // "reserved memory" -- Linux does not content itself with a simple gap\r
216 // in the memory map wherever the MCFG ACPI table points to.\r
217 //\r
218 // This appears to be a safety measure. The PCI Firmware Specification\r
219 // (rev 3.1) says in 4.1.2. "MCFG Table Description": "The resources can\r
220 // *optionally* be returned in [...] EFIGetMemoryMap as reserved memory\r
221 // [...]". (Emphasis added here.)\r
222 //\r
223 // Normally we add memory resource descriptor HOBs in\r
224 // QemuInitializeRam(), and pre-allocate from those with memory\r
225 // allocation HOBs in InitializeRamRegions(). However, the MMCONFIG area\r
226 // is most definitely not RAM; so, as an exception, cover it with\r
227 // uncacheable reserved memory right here.\r
228 //\r
229 AddReservedMemoryBaseSizeHob (PciExBarBase, SIZE_256MB, FALSE);\r
230 BuildMemoryAllocationHob (PciExBarBase, SIZE_256MB,\r
231 EfiReservedMemoryType);\r
232 }\r
233 AddIoMemoryBaseSizeHob (PcdGet32(PcdCpuLocalApicBaseAddress), SIZE_1MB);\r
234\r
235 //\r
236 // On Q35, the IO Port space is available for PCI resource allocations from\r
237 // 0x6000 up.\r
238 //\r
239 if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
240 PciIoBase = 0x6000;\r
241 PciIoSize = 0xA000;\r
242 ASSERT ((ICH9_PMBASE_VALUE & 0xF000) < PciIoBase);\r
243 }\r
244\r
245 //\r
246 // Add PCI IO Port space available for PCI resource allocations.\r
247 //\r
248 BuildResourceDescriptorHob (\r
249 EFI_RESOURCE_IO,\r
250 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
251 EFI_RESOURCE_ATTRIBUTE_INITIALIZED,\r
252 PciIoBase,\r
253 PciIoSize\r
254 );\r
255 PcdStatus = PcdSet64S (PcdPciIoBase, PciIoBase);\r
256 ASSERT_RETURN_ERROR (PcdStatus);\r
257 PcdStatus = PcdSet64S (PcdPciIoSize, PciIoSize);\r
258 ASSERT_RETURN_ERROR (PcdStatus);\r
259}\r
260\r
261#define UPDATE_BOOLEAN_PCD_FROM_FW_CFG(TokenName) \\r
262 do { \\r
263 BOOLEAN Setting; \\r
264 RETURN_STATUS PcdStatus; \\r
265 \\r
266 if (!RETURN_ERROR (QemuFwCfgParseBool ( \\r
267 "opt/ovmf/" #TokenName, &Setting))) { \\r
268 PcdStatus = PcdSetBoolS (TokenName, Setting); \\r
269 ASSERT_RETURN_ERROR (PcdStatus); \\r
270 } \\r
271 } while (0)\r
272\r
273VOID\r
274NoexecDxeInitialization (\r
275 VOID\r
276 )\r
277{\r
278 UPDATE_BOOLEAN_PCD_FROM_FW_CFG (PcdSetNxForStack);\r
279}\r
280\r
281VOID\r
282PciExBarInitialization (\r
283 VOID\r
284 )\r
285{\r
286 union {\r
287 UINT64 Uint64;\r
288 UINT32 Uint32[2];\r
289 } PciExBarBase;\r
290\r
291 //\r
292 // We only support the 256MB size for the MMCONFIG area:\r
293 // 256 buses * 32 devices * 8 functions * 4096 bytes config space.\r
294 //\r
295 // The masks used below enforce the Q35 requirements that the MMCONFIG area\r
296 // be (a) correctly aligned -- here at 256 MB --, (b) located under 64 GB.\r
297 //\r
298 // Note that (b) also ensures that the minimum address width we have\r
299 // determined in AddressWidthInitialization(), i.e., 36 bits, will suffice\r
300 // for DXE's page tables to cover the MMCONFIG area.\r
301 //\r
302 PciExBarBase.Uint64 = FixedPcdGet64 (PcdPciExpressBaseAddress);\r
303 ASSERT ((PciExBarBase.Uint32[1] & MCH_PCIEXBAR_HIGHMASK) == 0);\r
304 ASSERT ((PciExBarBase.Uint32[0] & MCH_PCIEXBAR_LOWMASK) == 0);\r
305\r
306 //\r
307 // Clear the PCIEXBAREN bit first, before programming the high register.\r
308 //\r
309 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW), 0);\r
310\r
311 //\r
312 // Program the high register. Then program the low register, setting the\r
313 // MMCONFIG area size and enabling decoding at once.\r
314 //\r
315 PciWrite32 (DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_HIGH), PciExBarBase.Uint32[1]);\r
316 PciWrite32 (\r
317 DRAMC_REGISTER_Q35 (MCH_PCIEXBAR_LOW),\r
318 PciExBarBase.Uint32[0] | MCH_PCIEXBAR_BUS_FF | MCH_PCIEXBAR_EN\r
319 );\r
320}\r
321\r
322VOID\r
323MiscInitialization (\r
324 VOID\r
325 )\r
326{\r
327 UINTN PmCmd;\r
328 UINTN Pmba;\r
329 UINT32 PmbaAndVal;\r
330 UINT32 PmbaOrVal;\r
331 UINTN AcpiCtlReg;\r
332 UINT8 AcpiEnBit;\r
333 RETURN_STATUS PcdStatus;\r
334\r
335 //\r
336 // Disable A20 Mask\r
337 //\r
338 IoOr8 (0x92, BIT1);\r
339\r
340 //\r
341 // Build the CPU HOB with guest RAM size dependent address width and 16-bits\r
342 // of IO space. (Side note: unlike other HOBs, the CPU HOB is needed during\r
343 // S3 resume as well, so we build it unconditionally.)\r
344 //\r
345 BuildCpuHob (mPhysMemAddressWidth, 16);\r
346\r
347 //\r
348 // Determine platform type and save Host Bridge DID to PCD\r
349 //\r
350 switch (mHostBridgeDevId) {\r
351 case INTEL_82441_DEVICE_ID:\r
352 PmCmd = POWER_MGMT_REGISTER_PIIX4 (PCI_COMMAND_OFFSET);\r
353 Pmba = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);\r
354 PmbaAndVal = ~(UINT32)PIIX4_PMBA_MASK;\r
355 PmbaOrVal = PIIX4_PMBA_VALUE;\r
356 AcpiCtlReg = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMREGMISC);\r
357 AcpiEnBit = PIIX4_PMREGMISC_PMIOSE;\r
358 break;\r
359 case INTEL_Q35_MCH_DEVICE_ID:\r
360 PmCmd = POWER_MGMT_REGISTER_Q35 (PCI_COMMAND_OFFSET);\r
361 Pmba = POWER_MGMT_REGISTER_Q35 (ICH9_PMBASE);\r
362 PmbaAndVal = ~(UINT32)ICH9_PMBASE_MASK;\r
363 PmbaOrVal = ICH9_PMBASE_VALUE;\r
364 AcpiCtlReg = POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL);\r
365 AcpiEnBit = ICH9_ACPI_CNTL_ACPI_EN;\r
366 break;\r
367 default:\r
368 DEBUG ((DEBUG_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",\r
369 __FUNCTION__, mHostBridgeDevId));\r
370 ASSERT (FALSE);\r
371 return;\r
372 }\r
373 PcdStatus = PcdSet16S (PcdOvmfHostBridgePciDevId, mHostBridgeDevId);\r
374 ASSERT_RETURN_ERROR (PcdStatus);\r
375\r
376 //\r
377 // If the appropriate IOspace enable bit is set, assume the ACPI PMBA has\r
378 // been configured and skip the setup here. This matches the logic in\r
379 // AcpiTimerLibConstructor ().\r
380 //\r
381 if ((PciRead8 (AcpiCtlReg) & AcpiEnBit) == 0) {\r
382 //\r
383 // The PEI phase should be exited with fully accessibe ACPI PM IO space:\r
384 // 1. set PMBA\r
385 //\r
386 PciAndThenOr32 (Pmba, PmbaAndVal, PmbaOrVal);\r
387\r
388 //\r
389 // 2. set PCICMD/IOSE\r
390 //\r
391 PciOr8 (PmCmd, EFI_PCI_COMMAND_IO_SPACE);\r
392\r
393 //\r
394 // 3. set ACPI PM IO enable bit (PMREGMISC:PMIOSE or ACPI_CNTL:ACPI_EN)\r
395 //\r
396 PciOr8 (AcpiCtlReg, AcpiEnBit);\r
397 }\r
398\r
399 if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
400 //\r
401 // Set Root Complex Register Block BAR\r
402 //\r
403 PciWrite32 (\r
404 POWER_MGMT_REGISTER_Q35 (ICH9_RCBA),\r
405 ICH9_ROOT_COMPLEX_BASE | ICH9_RCBA_EN\r
406 );\r
407\r
408 //\r
409 // Set PCI Express Register Range Base Address\r
410 //\r
411 PciExBarInitialization ();\r
412 }\r
413}\r
414\r
415\r
416VOID\r
417BootModeInitialization (\r
418 VOID\r
419 )\r
420{\r
421 EFI_STATUS Status;\r
422\r
423 if (CmosRead8 (0xF) == 0xFE) {\r
424 mBootMode = BOOT_ON_S3_RESUME;\r
425 }\r
426 CmosWrite8 (0xF, 0x00);\r
427\r
428 Status = PeiServicesSetBootMode (mBootMode);\r
429 ASSERT_EFI_ERROR (Status);\r
430\r
431 Status = PeiServicesInstallPpi (mPpiBootMode);\r
432 ASSERT_EFI_ERROR (Status);\r
433}\r
434\r
435\r
436VOID\r
437ReserveEmuVariableNvStore (\r
438 )\r
439{\r
440 EFI_PHYSICAL_ADDRESS VariableStore;\r
441 RETURN_STATUS PcdStatus;\r
442\r
443 //\r
444 // Allocate storage for NV variables early on so it will be\r
445 // at a consistent address. Since VM memory is preserved\r
446 // across reboots, this allows the NV variable storage to survive\r
447 // a VM reboot.\r
448 //\r
449 VariableStore =\r
450 (EFI_PHYSICAL_ADDRESS)(UINTN)\r
451 AllocateRuntimePages (\r
452 EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize))\r
453 );\r
454 DEBUG ((DEBUG_INFO,\r
455 "Reserved variable store memory: 0x%lX; size: %dkb\n",\r
456 VariableStore,\r
457 (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / 1024\r
458 ));\r
459 PcdStatus = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore);\r
460 ASSERT_RETURN_ERROR (PcdStatus);\r
461}\r
462\r
463\r
464VOID\r
465DebugDumpCmos (\r
466 VOID\r
467 )\r
468{\r
469 UINT32 Loop;\r
470\r
471 DEBUG ((DEBUG_INFO, "CMOS:\n"));\r
472\r
473 for (Loop = 0; Loop < 0x80; Loop++) {\r
474 if ((Loop % 0x10) == 0) {\r
475 DEBUG ((DEBUG_INFO, "%02x:", Loop));\r
476 }\r
477 DEBUG ((DEBUG_INFO, " %02x", CmosRead8 (Loop)));\r
478 if ((Loop % 0x10) == 0xf) {\r
479 DEBUG ((DEBUG_INFO, "\n"));\r
480 }\r
481 }\r
482}\r
483\r
484\r
485VOID\r
486S3Verification (\r
487 VOID\r
488 )\r
489{\r
490#if defined (MDE_CPU_X64)\r
491 if (FeaturePcdGet (PcdSmmSmramRequire) && mS3Supported) {\r
492 DEBUG ((DEBUG_ERROR,\r
493 "%a: S3Resume2Pei doesn't support X64 PEI + SMM yet.\n", __FUNCTION__));\r
494 DEBUG ((DEBUG_ERROR,\r
495 "%a: Please disable S3 on the QEMU command line (see the README),\n",\r
496 __FUNCTION__));\r
497 DEBUG ((DEBUG_ERROR,\r
498 "%a: or build OVMF with \"OvmfPkgIa32X64.dsc\".\n", __FUNCTION__));\r
499 ASSERT (FALSE);\r
500 CpuDeadLoop ();\r
501 }\r
502#endif\r
503}\r
504\r
505\r
506VOID\r
507Q35BoardVerification (\r
508 VOID\r
509 )\r
510{\r
511 if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
512 return;\r
513 }\r
514\r
515 DEBUG ((\r
516 DEBUG_ERROR,\r
517 "%a: no TSEG (SMRAM) on host bridge DID=0x%04x; "\r
518 "only DID=0x%04x (Q35) is supported\n",\r
519 __FUNCTION__,\r
520 mHostBridgeDevId,\r
521 INTEL_Q35_MCH_DEVICE_ID\r
522 ));\r
523 ASSERT (FALSE);\r
524 CpuDeadLoop ();\r
525}\r
526\r
527\r
528/**\r
529 Fetch the boot CPU count and the possible CPU count from QEMU, and expose\r
530 them to UefiCpuPkg modules. Set the mMaxCpuCount variable.\r
531**/\r
532VOID\r
533MaxCpuCountInitialization (\r
534 VOID\r
535 )\r
536{\r
537 UINT16 BootCpuCount;\r
538 RETURN_STATUS PcdStatus;\r
539\r
540 //\r
541 // Try to fetch the boot CPU count.\r
542 //\r
543 QemuFwCfgSelectItem (QemuFwCfgItemSmpCpuCount);\r
544 BootCpuCount = QemuFwCfgRead16 ();\r
545 if (BootCpuCount == 0) {\r
546 //\r
547 // QEMU doesn't report the boot CPU count. (BootCpuCount == 0) will let\r
548 // MpInitLib count APs up to (PcdCpuMaxLogicalProcessorNumber - 1), or\r
549 // until PcdCpuApInitTimeOutInMicroSeconds elapses (whichever is reached\r
550 // first).\r
551 //\r
552 DEBUG ((DEBUG_WARN, "%a: boot CPU count unavailable\n", __FUNCTION__));\r
553 mMaxCpuCount = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
554 } else {\r
555 //\r
556 // We will expose BootCpuCount to MpInitLib. MpInitLib will count APs up to\r
557 // (BootCpuCount - 1) precisely, regardless of timeout.\r
558 //\r
559 // Now try to fetch the possible CPU count.\r
560 //\r
561 UINTN CpuHpBase;\r
562 UINT32 CmdData2;\r
563\r
564 CpuHpBase = ((mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) ?\r
565 ICH9_CPU_HOTPLUG_BASE : PIIX4_CPU_HOTPLUG_BASE);\r
566\r
567 //\r
568 // If only legacy mode is available in the CPU hotplug register block, or\r
569 // the register block is completely missing, then the writes below are\r
570 // no-ops.\r
571 //\r
572 // 1. Switch the hotplug register block to modern mode.\r
573 //\r
574 IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, 0);\r
575 //\r
576 // 2. Select a valid CPU for deterministic reading of\r
577 // QEMU_CPUHP_R_CMD_DATA2.\r
578 //\r
579 // CPU#0 is always valid; it is the always present and non-removable\r
580 // BSP.\r
581 //\r
582 IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, 0);\r
583 //\r
584 // 3. Send a command after which QEMU_CPUHP_R_CMD_DATA2 is specified to\r
585 // read as zero, and which does not invalidate the selector. (The\r
586 // selector may change, but it must not become invalid.)\r
587 //\r
588 // Send QEMU_CPUHP_CMD_GET_PENDING, as it will prove useful later.\r
589 //\r
590 IoWrite8 (CpuHpBase + QEMU_CPUHP_W_CMD, QEMU_CPUHP_CMD_GET_PENDING);\r
591 //\r
592 // 4. Read QEMU_CPUHP_R_CMD_DATA2.\r
593 //\r
594 // If the register block is entirely missing, then this is an unassigned\r
595 // IO read, returning all-bits-one.\r
596 //\r
597 // If only legacy mode is available, then bit#0 stands for CPU#0 in the\r
598 // "CPU present bitmap". CPU#0 is always present.\r
599 //\r
600 // Otherwise, QEMU_CPUHP_R_CMD_DATA2 is either still reserved (returning\r
601 // all-bits-zero), or it is specified to read as zero after the above\r
602 // steps. Both cases confirm modern mode.\r
603 //\r
604 CmdData2 = IoRead32 (CpuHpBase + QEMU_CPUHP_R_CMD_DATA2);\r
605 DEBUG ((DEBUG_VERBOSE, "%a: CmdData2=0x%x\n", __FUNCTION__, CmdData2));\r
606 if (CmdData2 != 0) {\r
607 //\r
608 // QEMU doesn't support the modern CPU hotplug interface. Assume that the\r
609 // possible CPU count equals the boot CPU count (precluding hotplug).\r
610 //\r
611 DEBUG ((DEBUG_WARN, "%a: modern CPU hotplug interface unavailable\n",\r
612 __FUNCTION__));\r
613 mMaxCpuCount = BootCpuCount;\r
614 } else {\r
615 //\r
616 // Grab the possible CPU count from the modern CPU hotplug interface.\r
617 //\r
618 UINT32 Present, Possible, Selected;\r
619\r
620 Present = 0;\r
621 Possible = 0;\r
622\r
623 //\r
624 // We've sent QEMU_CPUHP_CMD_GET_PENDING last; this ensures\r
625 // QEMU_CPUHP_RW_CMD_DATA can now be read usefully. However,\r
626 // QEMU_CPUHP_CMD_GET_PENDING may have selected a CPU with actual pending\r
627 // hotplug events; therefore, select CPU#0 forcibly.\r
628 //\r
629 IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, Possible);\r
630\r
631 do {\r
632 UINT8 CpuStatus;\r
633\r
634 //\r
635 // Read the status of the currently selected CPU. This will help with a\r
636 // sanity check against "BootCpuCount".\r
637 //\r
638 CpuStatus = IoRead8 (CpuHpBase + QEMU_CPUHP_R_CPU_STAT);\r
639 if ((CpuStatus & QEMU_CPUHP_STAT_ENABLED) != 0) {\r
640 ++Present;\r
641 }\r
642 //\r
643 // Attempt to select the next CPU.\r
644 //\r
645 ++Possible;\r
646 IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, Possible);\r
647 //\r
648 // If the selection is successful, then the following read will return\r
649 // the selector (which we know is positive at this point). Otherwise,\r
650 // the read will return 0.\r
651 //\r
652 Selected = IoRead32 (CpuHpBase + QEMU_CPUHP_RW_CMD_DATA);\r
653 ASSERT (Selected == Possible || Selected == 0);\r
654 } while (Selected > 0);\r
655\r
656 //\r
657 // Sanity check: fw_cfg and the modern CPU hotplug interface should\r
658 // return the same boot CPU count.\r
659 //\r
660 if (BootCpuCount != Present) {\r
661 DEBUG ((DEBUG_WARN, "%a: QEMU v2.7 reset bug: BootCpuCount=%d "\r
662 "Present=%u\n", __FUNCTION__, BootCpuCount, Present));\r
663 //\r
664 // The handling of QemuFwCfgItemSmpCpuCount, across CPU hotplug plus\r
665 // platform reset (including S3), was corrected in QEMU commit\r
666 // e3cadac073a9 ("pc: fix FW_CFG_NB_CPUS to account for -device added\r
667 // CPUs", 2016-11-16), part of release v2.8.0.\r
668 //\r
669 BootCpuCount = (UINT16)Present;\r
670 }\r
671\r
672 mMaxCpuCount = Possible;\r
673 }\r
674 }\r
675\r
676 DEBUG ((DEBUG_INFO, "%a: BootCpuCount=%d mMaxCpuCount=%u\n", __FUNCTION__,\r
677 BootCpuCount, mMaxCpuCount));\r
678 ASSERT (BootCpuCount <= mMaxCpuCount);\r
679\r
680 PcdStatus = PcdSet32S (PcdCpuBootLogicalProcessorNumber, BootCpuCount);\r
681 ASSERT_RETURN_ERROR (PcdStatus);\r
682 PcdStatus = PcdSet32S (PcdCpuMaxLogicalProcessorNumber, mMaxCpuCount);\r
683 ASSERT_RETURN_ERROR (PcdStatus);\r
684}\r
685\r
686\r
687/**\r
688 Perform Platform PEI initialization.\r
689\r
690 @param FileHandle Handle of the file being invoked.\r
691 @param PeiServices Describes the list of possible PEI Services.\r
692\r
693 @return EFI_SUCCESS The PEIM initialized successfully.\r
694\r
695**/\r
696EFI_STATUS\r
697EFIAPI\r
698InitializePlatform (\r
699 IN EFI_PEI_FILE_HANDLE FileHandle,\r
700 IN CONST EFI_PEI_SERVICES **PeiServices\r
701 )\r
702{\r
703 EFI_STATUS Status;\r
704\r
705 DEBUG ((DEBUG_INFO, "Platform PEIM Loaded\n"));\r
706\r
707 DebugDumpCmos ();\r
708\r
709 if (QemuFwCfgS3Enabled ()) {\r
710 DEBUG ((DEBUG_INFO, "S3 support was detected on QEMU\n"));\r
711 mS3Supported = TRUE;\r
712 Status = PcdSetBoolS (PcdAcpiS3Enable, TRUE);\r
713 ASSERT_EFI_ERROR (Status);\r
714 }\r
715\r
716 S3Verification ();\r
717 BootModeInitialization ();\r
718 AddressWidthInitialization ();\r
719\r
720 //\r
721 // Query Host Bridge DID\r
722 //\r
723 mHostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
724\r
725 MaxCpuCountInitialization ();\r
726\r
727 if (FeaturePcdGet (PcdSmmSmramRequire)) {\r
728 Q35BoardVerification ();\r
729 Q35TsegMbytesInitialization ();\r
730 Q35SmramAtDefaultSmbaseInitialization ();\r
731 }\r
732\r
733 PublishPeiMemory ();\r
734\r
735 QemuUc32BaseInitialization ();\r
736\r
737 InitializeRamRegions ();\r
738\r
739 if (mBootMode != BOOT_ON_S3_RESUME) {\r
740 if (!FeaturePcdGet (PcdSmmSmramRequire)) {\r
741 ReserveEmuVariableNvStore ();\r
742 }\r
743 PeiFvInitialization ();\r
744 MemTypeInfoInitialization ();\r
745 MemMapInitialization ();\r
746 NoexecDxeInitialization ();\r
747 }\r
748\r
749 InstallClearCacheCallback ();\r
750 AmdSevInitialize ();\r
751 MiscInitialization ();\r
752 InstallFeatureControlCallback ();\r
753\r
754 return EFI_SUCCESS;\r
755}\r