Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>\r
\r
- This program and the accompanying materials\r
- are licensed and made available under the terms and conditions of the BSD License\r
- which accompanies this distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php\r
-\r
- THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
\r
**/\r
\r
//\r
// The Library classes this module consumes\r
//\r
+#include <Library/BaseMemoryLib.h>\r
#include <Library/BaseLib.h>\r
#include <Library/DebugLib.h>\r
#include <Library/HobLib.h>\r
#include <Library/PeiServicesLib.h>\r
#include <Library/QemuFwCfgLib.h>\r
#include <Library/QemuFwCfgS3Lib.h>\r
+#include <Library/QemuFwCfgSimpleParserLib.h>\r
#include <Library/ResourcePublicationLib.h>\r
-#include <Guid/MemoryTypeInformation.h>\r
#include <Ppi/MasterBootMode.h>\r
+#include <IndustryStandard/I440FxPiix4.h>\r
+#include <IndustryStandard/Microvm.h>\r
#include <IndustryStandard/Pci22.h>\r
+#include <IndustryStandard/Q35MchIch9.h>\r
+#include <IndustryStandard/QemuCpuHotplug.h>\r
+#include <Library/MemEncryptSevLib.h>\r
#include <OvmfPlatforms.h>\r
\r
#include "Platform.h"\r
-#include "Cmos.h"\r
-\r
-EFI_MEMORY_TYPE_INFORMATION mDefaultMemoryTypeInformation[] = {\r
- { EfiACPIMemoryNVS, 0x004 },\r
- { EfiACPIReclaimMemory, 0x008 },\r
- { EfiReservedMemoryType, 0x004 },\r
- { EfiRuntimeServicesData, 0x024 },\r
- { EfiRuntimeServicesCode, 0x030 },\r
- { EfiBootServicesCode, 0x180 },\r
- { EfiBootServicesData, 0xF00 },\r
- { EfiMaxMemoryType, 0x000 }\r
-};\r
\r
+EFI_HOB_PLATFORM_INFO mPlatformInfoHob = { 0 };\r
\r
-EFI_PEI_PPI_DESCRIPTOR mPpiBootMode[] = {\r
+EFI_PEI_PPI_DESCRIPTOR mPpiBootMode[] = {\r
{\r
EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
&gEfiPeiMasterBootModePpiGuid,\r
}\r
};\r
\r
-\r
-UINT16 mHostBridgeDevId;\r
-\r
-EFI_BOOT_MODE mBootMode = BOOT_WITH_FULL_CONFIGURATION;\r
-\r
-BOOLEAN mS3Supported = FALSE;\r
-\r
-UINT32 mMaxCpuCount;\r
-\r
-VOID\r
-AddIoMemoryBaseSizeHob (\r
- EFI_PHYSICAL_ADDRESS MemoryBase,\r
- UINT64 MemorySize\r
- )\r
-{\r
- BuildResourceDescriptorHob (\r
- EFI_RESOURCE_MEMORY_MAPPED_IO,\r
- EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
- EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
- EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
- EFI_RESOURCE_ATTRIBUTE_TESTED,\r
- MemoryBase,\r
- MemorySize\r
- );\r
-}\r
-\r
-VOID\r
-AddReservedMemoryBaseSizeHob (\r
- EFI_PHYSICAL_ADDRESS MemoryBase,\r
- UINT64 MemorySize,\r
- BOOLEAN Cacheable\r
- )\r
-{\r
- BuildResourceDescriptorHob (\r
- EFI_RESOURCE_MEMORY_RESERVED,\r
- EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
- EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
- EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
- (Cacheable ?\r
- EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
- EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
- EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE :\r
- 0\r
- ) |\r
- EFI_RESOURCE_ATTRIBUTE_TESTED,\r
- MemoryBase,\r
- MemorySize\r
- );\r
-}\r
-\r
-VOID\r
-AddIoMemoryRangeHob (\r
- EFI_PHYSICAL_ADDRESS MemoryBase,\r
- EFI_PHYSICAL_ADDRESS MemoryLimit\r
- )\r
-{\r
- AddIoMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));\r
-}\r
-\r
-\r
-VOID\r
-AddMemoryBaseSizeHob (\r
- EFI_PHYSICAL_ADDRESS MemoryBase,\r
- UINT64 MemorySize\r
- )\r
-{\r
- BuildResourceDescriptorHob (\r
- EFI_RESOURCE_SYSTEM_MEMORY,\r
- EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
- EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
- EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
- EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
- EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
- EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |\r
- EFI_RESOURCE_ATTRIBUTE_TESTED,\r
- MemoryBase,\r
- MemorySize\r
- );\r
-}\r
-\r
-\r
-VOID\r
-AddMemoryRangeHob (\r
- EFI_PHYSICAL_ADDRESS MemoryBase,\r
- EFI_PHYSICAL_ADDRESS MemoryLimit\r
- )\r
-{\r
- AddMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase));\r
-}\r
-\r
-\r
VOID\r
MemMapInitialization (\r
- VOID\r
+ IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
)\r
{\r
- UINT64 PciIoBase;\r
- UINT64 PciIoSize;\r
- RETURN_STATUS PcdStatus;\r
+ UINT64 PciIoBase;\r
+ UINT64 PciIoSize;\r
+ RETURN_STATUS PcdStatus;\r
+ UINT32 TopOfLowRam;\r
+ UINT64 PciExBarBase;\r
+ UINT32 PciBase;\r
+ UINT32 PciSize;\r
\r
PciIoBase = 0xC000;\r
PciIoSize = 0x4000;\r
\r
- //\r
- // Create Memory Type Information HOB\r
- //\r
- BuildGuidDataHob (\r
- &gEfiMemoryTypeInformationGuid,\r
- mDefaultMemoryTypeInformation,\r
- sizeof(mDefaultMemoryTypeInformation)\r
- );\r
-\r
//\r
// Video memory + Legacy BIOS region\r
//\r
- AddIoMemoryRangeHob (0x0A0000, BASE_1MB);\r
-\r
- if (!mXen) {\r
- UINT32 TopOfLowRam;\r
- UINT64 PciExBarBase;\r
- UINT32 PciBase;\r
- UINT32 PciSize;\r
+ PlatformAddIoMemoryRangeHob (0x0A0000, BASE_1MB);\r
\r
- TopOfLowRam = GetSystemMemorySizeBelow4gb ();\r
- PciExBarBase = 0;\r
- if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
- //\r
- // The MMCONFIG area is expected to fall between the top of low RAM and\r
- // the base of the 32-bit PCI host aperture.\r
- //\r
- PciExBarBase = FixedPcdGet64 (PcdPciExpressBaseAddress);\r
- ASSERT (TopOfLowRam <= PciExBarBase);\r
- ASSERT (PciExBarBase <= MAX_UINT32 - SIZE_256MB);\r
- PciBase = (UINT32)(PciExBarBase + SIZE_256MB);\r
- } else {\r
- PciBase = (TopOfLowRam < BASE_2GB) ? BASE_2GB : TopOfLowRam;\r
- }\r
+ if (PlatformInfoHob->HostBridgeDevId == 0xffff /* microvm */) {\r
+ PlatformAddIoMemoryBaseSizeHob (MICROVM_GED_MMIO_BASE, SIZE_4KB);\r
+ PlatformAddIoMemoryBaseSizeHob (0xFEC00000, SIZE_4KB); /* ioapic #1 */\r
+ PlatformAddIoMemoryBaseSizeHob (0xFEC10000, SIZE_4KB); /* ioapic #2 */\r
+ return;\r
+ }\r
\r
+ TopOfLowRam = PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r
+ PciExBarBase = 0;\r
+ if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
//\r
- // address purpose size\r
- // ------------ -------- -------------------------\r
- // max(top, 2g) PCI MMIO 0xFC000000 - max(top, 2g)\r
- // 0xFC000000 gap 44 MB\r
- // 0xFEC00000 IO-APIC 4 KB\r
- // 0xFEC01000 gap 1020 KB\r
- // 0xFED00000 HPET 1 KB\r
- // 0xFED00400 gap 111 KB\r
- // 0xFED1C000 gap (PIIX4) / RCRB (ICH9) 16 KB\r
- // 0xFED20000 gap 896 KB\r
- // 0xFEE00000 LAPIC 1 MB\r
- //\r
- PciSize = 0xFC000000 - PciBase;\r
- AddIoMemoryBaseSizeHob (PciBase, PciSize);\r
- PcdStatus = PcdSet64S (PcdPciMmio32Base, PciBase);\r
- ASSERT_RETURN_ERROR (PcdStatus);\r
- PcdStatus = PcdSet64S (PcdPciMmio32Size, PciSize);\r
- ASSERT_RETURN_ERROR (PcdStatus);\r
-\r
- AddIoMemoryBaseSizeHob (0xFEC00000, SIZE_4KB);\r
- AddIoMemoryBaseSizeHob (0xFED00000, SIZE_1KB);\r
- if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
- AddIoMemoryBaseSizeHob (ICH9_ROOT_COMPLEX_BASE, SIZE_16KB);\r
- //\r
- // Note: there should be an\r
- //\r
- // AddIoMemoryBaseSizeHob (PciExBarBase, SIZE_256MB);\r
- //\r
- // call below, just like the one above for RCBA. However, Linux insists\r
- // that the MMCONFIG area be marked in the E820 or UEFI memory map as\r
- // "reserved memory" -- Linux does not content itself with a simple gap\r
- // in the memory map wherever the MCFG ACPI table points to.\r
- //\r
- // This appears to be a safety measure. The PCI Firmware Specification\r
- // (rev 3.1) says in 4.1.2. "MCFG Table Description": "The resources can\r
- // *optionally* be returned in [...] EFIGetMemoryMap as reserved memory\r
- // [...]". (Emphasis added here.)\r
- //\r
- // Normally we add memory resource descriptor HOBs in\r
- // QemuInitializeRam(), and pre-allocate from those with memory\r
- // allocation HOBs in InitializeRamRegions(). However, the MMCONFIG area\r
- // is most definitely not RAM; so, as an exception, cover it with\r
- // uncacheable reserved memory right here.\r
- //\r
- AddReservedMemoryBaseSizeHob (PciExBarBase, SIZE_256MB, FALSE);\r
- BuildMemoryAllocationHob (PciExBarBase, SIZE_256MB,\r
- EfiReservedMemoryType);\r
- }\r
- AddIoMemoryBaseSizeHob (PcdGet32(PcdCpuLocalApicBaseAddress), SIZE_1MB);\r
+ // The MMCONFIG area is expected to fall between the top of low RAM and\r
+ // the base of the 32-bit PCI host aperture.\r
+ //\r
+ PciExBarBase = FixedPcdGet64 (PcdPciExpressBaseAddress);\r
+ ASSERT (TopOfLowRam <= PciExBarBase);\r
+ ASSERT (PciExBarBase <= MAX_UINT32 - SIZE_256MB);\r
+ PciBase = (UINT32)(PciExBarBase + SIZE_256MB);\r
+ } else {\r
+ ASSERT (TopOfLowRam <= PlatformInfoHob->Uc32Base);\r
+ PciBase = PlatformInfoHob->Uc32Base;\r
+ }\r
+\r
+ //\r
+ // address purpose size\r
+ // ------------ -------- -------------------------\r
+ // max(top, 2g) PCI MMIO 0xFC000000 - max(top, 2g)\r
+ // 0xFC000000 gap 44 MB\r
+ // 0xFEC00000 IO-APIC 4 KB\r
+ // 0xFEC01000 gap 1020 KB\r
+ // 0xFED00000 HPET 1 KB\r
+ // 0xFED00400 gap 111 KB\r
+ // 0xFED1C000 gap (PIIX4) / RCRB (ICH9) 16 KB\r
+ // 0xFED20000 gap 896 KB\r
+ // 0xFEE00000 LAPIC 1 MB\r
+ //\r
+ PciSize = 0xFC000000 - PciBase;\r
+ PlatformAddIoMemoryBaseSizeHob (PciBase, PciSize);\r
+ PcdStatus = PcdSet64S (PcdPciMmio32Base, PciBase);\r
+ ASSERT_RETURN_ERROR (PcdStatus);\r
+ PcdStatus = PcdSet64S (PcdPciMmio32Size, PciSize);\r
+ ASSERT_RETURN_ERROR (PcdStatus);\r
\r
+ PlatformInfoHob->PcdPciMmio32Base = PciBase;\r
+ PlatformInfoHob->PcdPciMmio32Size = PciSize;\r
+\r
+ PlatformAddIoMemoryBaseSizeHob (0xFEC00000, SIZE_4KB);\r
+ PlatformAddIoMemoryBaseSizeHob (0xFED00000, SIZE_1KB);\r
+ if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
+ PlatformAddIoMemoryBaseSizeHob (ICH9_ROOT_COMPLEX_BASE, SIZE_16KB);\r
//\r
- // On Q35, the IO Port space is available for PCI resource allocations from\r
- // 0x6000 up.\r
+ // Note: there should be an\r
//\r
- if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
- PciIoBase = 0x6000;\r
- PciIoSize = 0xA000;\r
- ASSERT ((ICH9_PMBASE_VALUE & 0xF000) < PciIoBase);\r
- }\r
+ // PlatformAddIoMemoryBaseSizeHob (PciExBarBase, SIZE_256MB);\r
+ //\r
+ // call below, just like the one above for RCBA. However, Linux insists\r
+ // that the MMCONFIG area be marked in the E820 or UEFI memory map as\r
+ // "reserved memory" -- Linux does not content itself with a simple gap\r
+ // in the memory map wherever the MCFG ACPI table points to.\r
+ //\r
+ // This appears to be a safety measure. The PCI Firmware Specification\r
+ // (rev 3.1) says in 4.1.2. "MCFG Table Description": "The resources can\r
+ // *optionally* be returned in [...] EFIGetMemoryMap as reserved memory\r
+ // [...]". (Emphasis added here.)\r
+ //\r
+ // Normally we add memory resource descriptor HOBs in\r
+ // QemuInitializeRam(), and pre-allocate from those with memory\r
+ // allocation HOBs in InitializeRamRegions(). However, the MMCONFIG area\r
+ // is most definitely not RAM; so, as an exception, cover it with\r
+ // uncacheable reserved memory right here.\r
+ //\r
+ PlatformAddReservedMemoryBaseSizeHob (PciExBarBase, SIZE_256MB, FALSE);\r
+ BuildMemoryAllocationHob (\r
+ PciExBarBase,\r
+ SIZE_256MB,\r
+ EfiReservedMemoryType\r
+ );\r
+ }\r
+\r
+ PlatformAddIoMemoryBaseSizeHob (PcdGet32 (PcdCpuLocalApicBaseAddress), SIZE_1MB);\r
+\r
+ //\r
+ // On Q35, the IO Port space is available for PCI resource allocations from\r
+ // 0x6000 up.\r
+ //\r
+ if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
+ PciIoBase = 0x6000;\r
+ PciIoSize = 0xA000;\r
+ ASSERT ((ICH9_PMBASE_VALUE & 0xF000) < PciIoBase);\r
}\r
\r
//\r
ASSERT_RETURN_ERROR (PcdStatus);\r
PcdStatus = PcdSet64S (PcdPciIoSize, PciIoSize);\r
ASSERT_RETURN_ERROR (PcdStatus);\r
-}\r
\r
-EFI_STATUS\r
-GetNamedFwCfgBoolean (\r
- IN CHAR8 *FwCfgFileName,\r
- OUT BOOLEAN *Setting\r
- )\r
-{\r
- EFI_STATUS Status;\r
- FIRMWARE_CONFIG_ITEM FwCfgItem;\r
- UINTN FwCfgSize;\r
- UINT8 Value[3];\r
-\r
- Status = QemuFwCfgFindFile (FwCfgFileName, &FwCfgItem, &FwCfgSize);\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
- if (FwCfgSize > sizeof Value) {\r
- return EFI_BAD_BUFFER_SIZE;\r
- }\r
- QemuFwCfgSelectItem (FwCfgItem);\r
- QemuFwCfgReadBytes (FwCfgSize, Value);\r
-\r
- if ((FwCfgSize == 1) ||\r
- (FwCfgSize == 2 && Value[1] == '\n') ||\r
- (FwCfgSize == 3 && Value[1] == '\r' && Value[2] == '\n')) {\r
- switch (Value[0]) {\r
- case '0':\r
- case 'n':\r
- case 'N':\r
- *Setting = FALSE;\r
- return EFI_SUCCESS;\r
-\r
- case '1':\r
- case 'y':\r
- case 'Y':\r
- *Setting = TRUE;\r
- return EFI_SUCCESS;\r
-\r
- default:\r
- break;\r
- }\r
- }\r
- return EFI_PROTOCOL_ERROR;\r
+ PlatformInfoHob->PcdPciIoBase = PciIoBase;\r
+ PlatformInfoHob->PcdPciIoSize = PciIoSize;\r
}\r
\r
#define UPDATE_BOOLEAN_PCD_FROM_FW_CFG(TokenName) \\r
BOOLEAN Setting; \\r
RETURN_STATUS PcdStatus; \\r
\\r
- if (!EFI_ERROR (GetNamedFwCfgBoolean ( \\r
+ if (!RETURN_ERROR (QemuFwCfgParseBool ( \\r
"opt/ovmf/" #TokenName, &Setting))) { \\r
PcdStatus = PcdSetBoolS (TokenName, Setting); \\r
ASSERT_RETURN_ERROR (PcdStatus); \\r
VOID\r
)\r
{\r
- UPDATE_BOOLEAN_PCD_FROM_FW_CFG (PcdPropertiesTableEnable);\r
UPDATE_BOOLEAN_PCD_FROM_FW_CFG (PcdSetNxForStack);\r
}\r
\r
)\r
{\r
union {\r
- UINT64 Uint64;\r
- UINT32 Uint32[2];\r
+ UINT64 Uint64;\r
+ UINT32 Uint32[2];\r
} PciExBarBase;\r
\r
//\r
);\r
}\r
\r
+static const UINT8 EmptyFdt[] = {\r
+ 0xd0, 0x0d, 0xfe, 0xed, 0x00, 0x00, 0x00, 0x48,\r
+ 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x48,\r
+ 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x11,\r
+ 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\r
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\r
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\r
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\r
+ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,\r
+ 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x09,\r
+};\r
+\r
VOID\r
-MiscInitialization (\r
+MicrovmInitialization (\r
VOID\r
)\r
{\r
- UINTN PmCmd;\r
- UINTN Pmba;\r
- UINT32 PmbaAndVal;\r
- UINT32 PmbaOrVal;\r
- UINTN AcpiCtlReg;\r
- UINT8 AcpiEnBit;\r
- RETURN_STATUS PcdStatus;\r
+ FIRMWARE_CONFIG_ITEM FdtItem;\r
+ UINTN FdtSize;\r
+ UINTN FdtPages;\r
+ EFI_STATUS Status;\r
+ UINT64 *FdtHobData;\r
+ VOID *NewBase;\r
+\r
+ Status = QemuFwCfgFindFile ("etc/fdt", &FdtItem, &FdtSize);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_INFO, "%a: no etc/fdt found in fw_cfg, using dummy\n", __FUNCTION__));\r
+ FdtItem = 0;\r
+ FdtSize = sizeof (EmptyFdt);\r
+ }\r
+\r
+ FdtPages = EFI_SIZE_TO_PAGES (FdtSize);\r
+ NewBase = AllocatePages (FdtPages);\r
+ if (NewBase == NULL) {\r
+ DEBUG ((DEBUG_INFO, "%a: AllocatePages failed\n", __FUNCTION__));\r
+ return;\r
+ }\r
+\r
+ if (FdtItem) {\r
+ QemuFwCfgSelectItem (FdtItem);\r
+ QemuFwCfgReadBytes (FdtSize, NewBase);\r
+ } else {\r
+ CopyMem (NewBase, EmptyFdt, FdtSize);\r
+ }\r
+\r
+ FdtHobData = BuildGuidHob (&gFdtHobGuid, sizeof (*FdtHobData));\r
+ if (FdtHobData == NULL) {\r
+ DEBUG ((DEBUG_INFO, "%a: BuildGuidHob failed\n", __FUNCTION__));\r
+ return;\r
+ }\r
+\r
+ DEBUG ((\r
+ DEBUG_INFO,\r
+ "%a: fdt at 0x%x (size %d)\n",\r
+ __FUNCTION__,\r
+ NewBase,\r
+ FdtSize\r
+ ));\r
+ *FdtHobData = (UINTN)NewBase;\r
+}\r
+\r
+VOID\r
+MiscInitializationForMicrovm (\r
+ IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
+ )\r
+{\r
+ RETURN_STATUS PcdStatus;\r
+\r
+ ASSERT (PlatformInfoHob->HostBridgeDevId == 0xffff);\r
+\r
+ DEBUG ((DEBUG_INFO, "%a: microvm\n", __FUNCTION__));\r
+ //\r
+ // Disable A20 Mask\r
+ //\r
+ IoOr8 (0x92, BIT1);\r
+\r
+ //\r
+ // Build the CPU HOB with guest RAM size dependent address width and 16-bits\r
+ // of IO space. (Side note: unlike other HOBs, the CPU HOB is needed during\r
+ // S3 resume as well, so we build it unconditionally.)\r
+ //\r
+ BuildCpuHob (PlatformInfoHob->PhysMemAddressWidth, 16);\r
+\r
+ MicrovmInitialization ();\r
+ PcdStatus = PcdSet16S (\r
+ PcdOvmfHostBridgePciDevId,\r
+ MICROVM_PSEUDO_DEVICE_ID\r
+ );\r
+ ASSERT_RETURN_ERROR (PcdStatus);\r
+}\r
+\r
+VOID\r
+MiscInitialization (\r
+ IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
+ )\r
+{\r
+ UINTN PmCmd;\r
+ UINTN Pmba;\r
+ UINT32 PmbaAndVal;\r
+ UINT32 PmbaOrVal;\r
+ UINTN AcpiCtlReg;\r
+ UINT8 AcpiEnBit;\r
+ RETURN_STATUS PcdStatus;\r
\r
//\r
// Disable A20 Mask\r
// of IO space. (Side note: unlike other HOBs, the CPU HOB is needed during\r
// S3 resume as well, so we build it unconditionally.)\r
//\r
- BuildCpuHob (mPhysMemAddressWidth, 16);\r
+ BuildCpuHob (PlatformInfoHob->PhysMemAddressWidth, 16);\r
\r
//\r
// Determine platform type and save Host Bridge DID to PCD\r
//\r
- switch (mHostBridgeDevId) {\r
+ switch (PlatformInfoHob->HostBridgeDevId) {\r
case INTEL_82441_DEVICE_ID:\r
PmCmd = POWER_MGMT_REGISTER_PIIX4 (PCI_COMMAND_OFFSET);\r
Pmba = POWER_MGMT_REGISTER_PIIX4 (PIIX4_PMBA);\r
AcpiCtlReg = POWER_MGMT_REGISTER_Q35 (ICH9_ACPI_CNTL);\r
AcpiEnBit = ICH9_ACPI_CNTL_ACPI_EN;\r
break;\r
+ case CLOUDHV_DEVICE_ID:\r
+ break;\r
default:\r
- DEBUG ((EFI_D_ERROR, "%a: Unknown Host Bridge Device ID: 0x%04x\n",\r
- __FUNCTION__, mHostBridgeDevId));\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "%a: Unknown Host Bridge Device ID: 0x%04x\n",\r
+ __FUNCTION__,\r
+ PlatformInfoHob->HostBridgeDevId\r
+ ));\r
ASSERT (FALSE);\r
return;\r
}\r
- PcdStatus = PcdSet16S (PcdOvmfHostBridgePciDevId, mHostBridgeDevId);\r
+\r
+ PcdStatus = PcdSet16S (PcdOvmfHostBridgePciDevId, PlatformInfoHob->HostBridgeDevId);\r
ASSERT_RETURN_ERROR (PcdStatus);\r
\r
+ if (PlatformInfoHob->HostBridgeDevId == CLOUDHV_DEVICE_ID) {\r
+ DEBUG ((DEBUG_INFO, "%a: Cloud Hypervisor is done.\n", __FUNCTION__));\r
+ return;\r
+ }\r
+\r
//\r
- // If the appropriate IOspace enable bit is set, assume the ACPI PMBA\r
- // has been configured (e.g., by Xen) and skip the setup here.\r
- // This matches the logic in AcpiTimerLibConstructor ().\r
+ // If the appropriate IOspace enable bit is set, assume the ACPI PMBA has\r
+ // been configured and skip the setup here. This matches the logic in\r
+ // AcpiTimerLibConstructor ().\r
//\r
if ((PciRead8 (AcpiCtlReg) & AcpiEnBit) == 0) {\r
//\r
PciOr8 (AcpiCtlReg, AcpiEnBit);\r
}\r
\r
- if (mHostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
+ if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
//\r
// Set Root Complex Register Block BAR\r
//\r
}\r
}\r
\r
-\r
VOID\r
BootModeInitialization (\r
- VOID\r
+ IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
)\r
{\r
- EFI_STATUS Status;\r
+ EFI_STATUS Status;\r
\r
- if (CmosRead8 (0xF) == 0xFE) {\r
- mBootMode = BOOT_ON_S3_RESUME;\r
+ if (PlatformCmosRead8 (0xF) == 0xFE) {\r
+ PlatformInfoHob->BootMode = BOOT_ON_S3_RESUME;\r
}\r
- CmosWrite8 (0xF, 0x00);\r
\r
- Status = PeiServicesSetBootMode (mBootMode);\r
+ PlatformCmosWrite8 (0xF, 0x00);\r
+\r
+ Status = PeiServicesSetBootMode (PlatformInfoHob->BootMode);\r
ASSERT_EFI_ERROR (Status);\r
\r
Status = PeiServicesInstallPpi (mPpiBootMode);\r
ASSERT_EFI_ERROR (Status);\r
}\r
\r
-\r
VOID\r
ReserveEmuVariableNvStore (\r
)\r
{\r
- EFI_PHYSICAL_ADDRESS VariableStore;\r
- RETURN_STATUS PcdStatus;\r
+ EFI_PHYSICAL_ADDRESS VariableStore;\r
+ RETURN_STATUS PcdStatus;\r
\r
//\r
// Allocate storage for NV variables early on so it will be\r
//\r
VariableStore =\r
(EFI_PHYSICAL_ADDRESS)(UINTN)\r
- AllocateRuntimePages (\r
- EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize))\r
- );\r
- DEBUG ((EFI_D_INFO,\r
- "Reserved variable store memory: 0x%lX; size: %dkb\n",\r
- VariableStore,\r
- (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / 1024\r
- ));\r
+ AllocateRuntimePages (\r
+ EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize))\r
+ );\r
+ DEBUG ((\r
+ DEBUG_INFO,\r
+ "Reserved variable store memory: 0x%lX; size: %dkb\n",\r
+ VariableStore,\r
+ (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / 1024\r
+ ));\r
PcdStatus = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore);\r
ASSERT_RETURN_ERROR (PcdStatus);\r
}\r
\r
-\r
VOID\r
-DebugDumpCmos (\r
+S3Verification (\r
VOID\r
)\r
{\r
- UINT32 Loop;\r
-\r
- DEBUG ((EFI_D_INFO, "CMOS:\n"));\r
-\r
- for (Loop = 0; Loop < 0x80; Loop++) {\r
- if ((Loop % 0x10) == 0) {\r
- DEBUG ((EFI_D_INFO, "%02x:", Loop));\r
- }\r
- DEBUG ((EFI_D_INFO, " %02x", CmosRead8 (Loop)));\r
- if ((Loop % 0x10) == 0xf) {\r
- DEBUG ((EFI_D_INFO, "\n"));\r
- }\r
+ #if defined (MDE_CPU_X64)\r
+ if (mPlatformInfoHob.SmmSmramRequire && mPlatformInfoHob.S3Supported) {\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "%a: S3Resume2Pei doesn't support X64 PEI + SMM yet.\n",\r
+ __FUNCTION__\r
+ ));\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "%a: Please disable S3 on the QEMU command line (see the README),\n",\r
+ __FUNCTION__\r
+ ));\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "%a: or build OVMF with \"OvmfPkgIa32X64.dsc\".\n",\r
+ __FUNCTION__\r
+ ));\r
+ ASSERT (FALSE);\r
+ CpuDeadLoop ();\r
}\r
-}\r
\r
+ #endif\r
+}\r
\r
VOID\r
-S3Verification (\r
+Q35BoardVerification (\r
VOID\r
)\r
{\r
-#if defined (MDE_CPU_X64)\r
- if (FeaturePcdGet (PcdSmmSmramRequire) && mS3Supported) {\r
- DEBUG ((EFI_D_ERROR,\r
- "%a: S3Resume2Pei doesn't support X64 PEI + SMM yet.\n", __FUNCTION__));\r
- DEBUG ((EFI_D_ERROR,\r
- "%a: Please disable S3 on the QEMU command line (see the README),\n",\r
- __FUNCTION__));\r
- DEBUG ((EFI_D_ERROR,\r
- "%a: or build OVMF with \"OvmfPkgIa32X64.dsc\".\n", __FUNCTION__));\r
- ASSERT (FALSE);\r
- CpuDeadLoop ();\r
+ if (mPlatformInfoHob.HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
+ return;\r
}\r
-#endif\r
-}\r
\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "%a: no TSEG (SMRAM) on host bridge DID=0x%04x; "\r
+ "only DID=0x%04x (Q35) is supported\n",\r
+ __FUNCTION__,\r
+ mPlatformInfoHob.HostBridgeDevId,\r
+ INTEL_Q35_MCH_DEVICE_ID\r
+ ));\r
+ ASSERT (FALSE);\r
+ CpuDeadLoop ();\r
+}\r
\r
/**\r
- Fetch the number of boot CPUs from QEMU and expose it to UefiCpuPkg modules.\r
- Set the mMaxCpuCount variable.\r
+ Fetch the boot CPU count and the possible CPU count from QEMU, and expose\r
+ them to UefiCpuPkg modules. Set the MaxCpuCount field in PlatformInfoHob.\r
**/\r
VOID\r
-MaxCpuCountInitialization (\r
- VOID\r
+PlatformMaxCpuCountInitialization (\r
+ IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
)\r
{\r
- UINT16 ProcessorCount;\r
- RETURN_STATUS PcdStatus;\r
+ UINT16 BootCpuCount;\r
+ UINT32 MaxCpuCount;\r
\r
- QemuFwCfgSelectItem (QemuFwCfgItemSmpCpuCount);\r
- ProcessorCount = QemuFwCfgRead16 ();\r
//\r
- // If the fw_cfg key or fw_cfg entirely is unavailable, load mMaxCpuCount\r
- // from the PCD default. No change to PCDs.\r
+ // Try to fetch the boot CPU count.\r
//\r
- if (ProcessorCount == 0) {\r
- mMaxCpuCount = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
- return;\r
+ QemuFwCfgSelectItem (QemuFwCfgItemSmpCpuCount);\r
+ BootCpuCount = QemuFwCfgRead16 ();\r
+ if (BootCpuCount == 0) {\r
+ //\r
+ // QEMU doesn't report the boot CPU count. (BootCpuCount == 0) will let\r
+ // MpInitLib count APs up to (PcdCpuMaxLogicalProcessorNumber - 1), or\r
+ // until PcdCpuApInitTimeOutInMicroSeconds elapses (whichever is reached\r
+ // first).\r
+ //\r
+ DEBUG ((DEBUG_WARN, "%a: boot CPU count unavailable\n", __FUNCTION__));\r
+ MaxCpuCount = PlatformInfoHob->DefaultMaxCpuNumber;\r
+ } else {\r
+ //\r
+ // We will expose BootCpuCount to MpInitLib. MpInitLib will count APs up to\r
+ // (BootCpuCount - 1) precisely, regardless of timeout.\r
+ //\r
+ // Now try to fetch the possible CPU count.\r
+ //\r
+ UINTN CpuHpBase;\r
+ UINT32 CmdData2;\r
+\r
+ CpuHpBase = ((PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) ?\r
+ ICH9_CPU_HOTPLUG_BASE : PIIX4_CPU_HOTPLUG_BASE);\r
+\r
+ //\r
+ // If only legacy mode is available in the CPU hotplug register block, or\r
+ // the register block is completely missing, then the writes below are\r
+ // no-ops.\r
+ //\r
+ // 1. Switch the hotplug register block to modern mode.\r
+ //\r
+ IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, 0);\r
+ //\r
+ // 2. Select a valid CPU for deterministic reading of\r
+ // QEMU_CPUHP_R_CMD_DATA2.\r
+ //\r
+ // CPU#0 is always valid; it is the always present and non-removable\r
+ // BSP.\r
+ //\r
+ IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, 0);\r
+ //\r
+ // 3. Send a command after which QEMU_CPUHP_R_CMD_DATA2 is specified to\r
+ // read as zero, and which does not invalidate the selector. (The\r
+ // selector may change, but it must not become invalid.)\r
+ //\r
+ // Send QEMU_CPUHP_CMD_GET_PENDING, as it will prove useful later.\r
+ //\r
+ IoWrite8 (CpuHpBase + QEMU_CPUHP_W_CMD, QEMU_CPUHP_CMD_GET_PENDING);\r
+ //\r
+ // 4. Read QEMU_CPUHP_R_CMD_DATA2.\r
+ //\r
+ // If the register block is entirely missing, then this is an unassigned\r
+ // IO read, returning all-bits-one.\r
+ //\r
+ // If only legacy mode is available, then bit#0 stands for CPU#0 in the\r
+ // "CPU present bitmap". CPU#0 is always present.\r
+ //\r
+ // Otherwise, QEMU_CPUHP_R_CMD_DATA2 is either still reserved (returning\r
+ // all-bits-zero), or it is specified to read as zero after the above\r
+ // steps. Both cases confirm modern mode.\r
+ //\r
+ CmdData2 = IoRead32 (CpuHpBase + QEMU_CPUHP_R_CMD_DATA2);\r
+ DEBUG ((DEBUG_VERBOSE, "%a: CmdData2=0x%x\n", __FUNCTION__, CmdData2));\r
+ if (CmdData2 != 0) {\r
+ //\r
+ // QEMU doesn't support the modern CPU hotplug interface. Assume that the\r
+ // possible CPU count equals the boot CPU count (precluding hotplug).\r
+ //\r
+ DEBUG ((\r
+ DEBUG_WARN,\r
+ "%a: modern CPU hotplug interface unavailable\n",\r
+ __FUNCTION__\r
+ ));\r
+ MaxCpuCount = BootCpuCount;\r
+ } else {\r
+ //\r
+ // Grab the possible CPU count from the modern CPU hotplug interface.\r
+ //\r
+ UINT32 Present, Possible, Selected;\r
+\r
+ Present = 0;\r
+ Possible = 0;\r
+\r
+ //\r
+ // We've sent QEMU_CPUHP_CMD_GET_PENDING last; this ensures\r
+ // QEMU_CPUHP_RW_CMD_DATA can now be read usefully. However,\r
+ // QEMU_CPUHP_CMD_GET_PENDING may have selected a CPU with actual pending\r
+ // hotplug events; therefore, select CPU#0 forcibly.\r
+ //\r
+ IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, Possible);\r
+\r
+ do {\r
+ UINT8 CpuStatus;\r
+\r
+ //\r
+ // Read the status of the currently selected CPU. This will help with a\r
+ // sanity check against "BootCpuCount".\r
+ //\r
+ CpuStatus = IoRead8 (CpuHpBase + QEMU_CPUHP_R_CPU_STAT);\r
+ if ((CpuStatus & QEMU_CPUHP_STAT_ENABLED) != 0) {\r
+ ++Present;\r
+ }\r
+\r
+ //\r
+ // Attempt to select the next CPU.\r
+ //\r
+ ++Possible;\r
+ IoWrite32 (CpuHpBase + QEMU_CPUHP_W_CPU_SEL, Possible);\r
+ //\r
+ // If the selection is successful, then the following read will return\r
+ // the selector (which we know is positive at this point). Otherwise,\r
+ // the read will return 0.\r
+ //\r
+ Selected = IoRead32 (CpuHpBase + QEMU_CPUHP_RW_CMD_DATA);\r
+ ASSERT (Selected == Possible || Selected == 0);\r
+ } while (Selected > 0);\r
+\r
+ //\r
+ // Sanity check: fw_cfg and the modern CPU hotplug interface should\r
+ // return the same boot CPU count.\r
+ //\r
+ if (BootCpuCount != Present) {\r
+ DEBUG ((\r
+ DEBUG_WARN,\r
+ "%a: QEMU v2.7 reset bug: BootCpuCount=%d "\r
+ "Present=%u\n",\r
+ __FUNCTION__,\r
+ BootCpuCount,\r
+ Present\r
+ ));\r
+ //\r
+ // The handling of QemuFwCfgItemSmpCpuCount, across CPU hotplug plus\r
+ // platform reset (including S3), was corrected in QEMU commit\r
+ // e3cadac073a9 ("pc: fix FW_CFG_NB_CPUS to account for -device added\r
+ // CPUs", 2016-11-16), part of release v2.8.0.\r
+ //\r
+ BootCpuCount = (UINT16)Present;\r
+ }\r
+\r
+ MaxCpuCount = Possible;\r
+ }\r
}\r
- //\r
- // Otherwise, set mMaxCpuCount to the value reported by QEMU.\r
- //\r
- mMaxCpuCount = ProcessorCount;\r
- //\r
- // Additionally, tell UefiCpuPkg modules (a) the exact number of VCPUs, (b)\r
- // to wait, in the initial AP bringup, exactly as long as it takes for all of\r
- // the APs to report in. For this, we set the longest representable timeout\r
- // (approx. 71 minutes).\r
- //\r
- PcdStatus = PcdSet32S (PcdCpuMaxLogicalProcessorNumber, ProcessorCount);\r
+\r
+ DEBUG ((\r
+ DEBUG_INFO,\r
+ "%a: BootCpuCount=%d MaxCpuCount=%u\n",\r
+ __FUNCTION__,\r
+ BootCpuCount,\r
+ MaxCpuCount\r
+ ));\r
+ ASSERT (BootCpuCount <= MaxCpuCount);\r
+\r
+ PlatformInfoHob->PcdCpuMaxLogicalProcessorNumber = MaxCpuCount;\r
+ PlatformInfoHob->PcdCpuBootLogicalProcessorNumber = BootCpuCount;\r
+}\r
+\r
+/**\r
+ Fetch the boot CPU count and the possible CPU count from QEMU, and expose\r
+ them to UefiCpuPkg modules. Set the MaxCpuCount field in PlatformInfoHob.\r
+**/\r
+VOID\r
+MaxCpuCountInitialization (\r
+ IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob\r
+ )\r
+{\r
+ RETURN_STATUS PcdStatus;\r
+\r
+ PlatformMaxCpuCountInitialization (PlatformInfoHob);\r
+\r
+ PcdStatus = PcdSet32S (PcdCpuBootLogicalProcessorNumber, PlatformInfoHob->PcdCpuBootLogicalProcessorNumber);\r
ASSERT_RETURN_ERROR (PcdStatus);\r
- PcdStatus = PcdSet32S (PcdCpuApInitTimeOutInMicroSeconds, MAX_UINT32);\r
+ PcdStatus = PcdSet32S (PcdCpuMaxLogicalProcessorNumber, PlatformInfoHob->PcdCpuMaxLogicalProcessorNumber);\r
ASSERT_RETURN_ERROR (PcdStatus);\r
- DEBUG ((DEBUG_INFO, "%a: QEMU reports %d processor(s)\n", __FUNCTION__,\r
- ProcessorCount));\r
}\r
\r
-\r
/**\r
Perform Platform PEI initialization.\r
\r
IN CONST EFI_PEI_SERVICES **PeiServices\r
)\r
{\r
- EFI_STATUS Status;\r
+ EFI_STATUS Status;\r
\r
- DEBUG ((EFI_D_ERROR, "Platform PEIM Loaded\n"));\r
+ DEBUG ((DEBUG_INFO, "Platform PEIM Loaded\n"));\r
\r
- DebugDumpCmos ();\r
+ mPlatformInfoHob.SmmSmramRequire = FeaturePcdGet (PcdSmmSmramRequire);\r
+ mPlatformInfoHob.SevEsIsEnabled = MemEncryptSevEsIsEnabled ();\r
+ mPlatformInfoHob.PcdPciMmio64Size = PcdGet64 (PcdPciMmio64Size);\r
+ mPlatformInfoHob.DefaultMaxCpuNumber = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
\r
- XenDetect ();\r
+ PlatformDebugDumpCmos ();\r
\r
if (QemuFwCfgS3Enabled ()) {\r
- DEBUG ((EFI_D_INFO, "S3 support was detected on QEMU\n"));\r
- mS3Supported = TRUE;\r
- Status = PcdSetBoolS (PcdAcpiS3Enable, TRUE);\r
+ DEBUG ((DEBUG_INFO, "S3 support was detected on QEMU\n"));\r
+ mPlatformInfoHob.S3Supported = TRUE;\r
+ Status = PcdSetBoolS (PcdAcpiS3Enable, TRUE);\r
ASSERT_EFI_ERROR (Status);\r
}\r
\r
S3Verification ();\r
- BootModeInitialization ();\r
- AddressWidthInitialization ();\r
- MaxCpuCountInitialization ();\r
+ BootModeInitialization (&mPlatformInfoHob);\r
+ AddressWidthInitialization (&mPlatformInfoHob);\r
\r
- if (FeaturePcdGet (PcdSmmSmramRequire)) {\r
+ //\r
+ // Query Host Bridge DID\r
+ //\r
+ mPlatformInfoHob.HostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
+\r
+ MaxCpuCountInitialization (&mPlatformInfoHob);\r
+\r
+ if (mPlatformInfoHob.SmmSmramRequire) {\r
+ Q35BoardVerification ();\r
Q35TsegMbytesInitialization ();\r
+ Q35SmramAtDefaultSmbaseInitialization ();\r
}\r
\r
PublishPeiMemory ();\r
\r
- InitializeRamRegions ();\r
-\r
- if (mXen) {\r
- DEBUG ((EFI_D_INFO, "Xen was detected\n"));\r
- InitializeXen ();\r
- }\r
+ PlatformQemuUc32BaseInitialization (&mPlatformInfoHob);\r
\r
- //\r
- // Query Host Bridge DID\r
- //\r
- mHostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
+ InitializeRamRegions (&mPlatformInfoHob);\r
\r
- if (mBootMode != BOOT_ON_S3_RESUME) {\r
- if (!FeaturePcdGet (PcdSmmSmramRequire)) {\r
+ if (mPlatformInfoHob.BootMode != BOOT_ON_S3_RESUME) {\r
+ if (!mPlatformInfoHob.SmmSmramRequire) {\r
ReserveEmuVariableNvStore ();\r
}\r
+\r
PeiFvInitialization ();\r
- MemMapInitialization ();\r
+ MemTypeInfoInitialization ();\r
+ MemMapInitialization (&mPlatformInfoHob);\r
NoexecDxeInitialization ();\r
}\r
\r
- MiscInitialization ();\r
+ InstallClearCacheCallback ();\r
+ AmdSevInitialize ();\r
+ if (mPlatformInfoHob.HostBridgeDevId == 0xffff) {\r
+ MiscInitializationForMicrovm (&mPlatformInfoHob);\r
+ } else {\r
+ MiscInitialization (&mPlatformInfoHob);\r
+ }\r
+\r
InstallFeatureControlCallback ();\r
\r
return EFI_SUCCESS;\r