]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/PlatformPei/Platform.c
OvmfPkg/PlatformPei: rewrite MaxCpuCountInitialization() for CPU hotplug
[mirror_edk2.git] / OvmfPkg / PlatformPei / Platform.c
index ca1e6dc7e320169c1611a9de81c71e85c875fc6b..e5e8581752b5a430624f65666b2386e693e5a600 100644 (file)
@@ -4,13 +4,7 @@
   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
 #include <Library/PeimEntryPoint.h>\r
 #include <Library/PeiServicesLib.h>\r
 #include <Library/QemuFwCfgLib.h>\r
+#include <Library/QemuFwCfgS3Lib.h>\r
 #include <Library/ResourcePublicationLib.h>\r
 #include <Guid/MemoryTypeInformation.h>\r
 #include <Ppi/MasterBootMode.h>\r
+#include <IndustryStandard/I440FxPiix4.h>\r
 #include <IndustryStandard/Pci22.h>\r
+#include <IndustryStandard/Q35MchIch9.h>\r
+#include <IndustryStandard/QemuCpuHotplug.h>\r
 #include <OvmfPlatforms.h>\r
 \r
 #include "Platform.h"\r
@@ -68,6 +66,7 @@ EFI_BOOT_MODE mBootMode = BOOT_WITH_FULL_CONFIGURATION;
 \r
 BOOLEAN mS3Supported = FALSE;\r
 \r
+UINT32 mMaxCpuCount;\r
 \r
 VOID\r
 AddIoMemoryBaseSizeHob (\r
@@ -156,8 +155,9 @@ MemMapInitialization (
   VOID\r
   )\r
 {\r
-  UINT64 PciIoBase;\r
-  UINT64 PciIoSize;\r
+  UINT64        PciIoBase;\r
+  UINT64        PciIoSize;\r
+  RETURN_STATUS PcdStatus;\r
 \r
   PciIoBase = 0xC000;\r
   PciIoSize = 0x4000;\r
@@ -194,7 +194,8 @@ MemMapInitialization (
       ASSERT (PciExBarBase <= MAX_UINT32 - SIZE_256MB);\r
       PciBase = (UINT32)(PciExBarBase + SIZE_256MB);\r
     } else {\r
-      PciBase = (TopOfLowRam < BASE_2GB) ? BASE_2GB : TopOfLowRam;\r
+      ASSERT (TopOfLowRam <= mQemuUc32Base);\r
+      PciBase = mQemuUc32Base;\r
     }\r
 \r
     //\r
@@ -212,8 +213,11 @@ MemMapInitialization (
     //\r
     PciSize = 0xFC000000 - PciBase;\r
     AddIoMemoryBaseSizeHob (PciBase, PciSize);\r
-    PcdSet64 (PcdPciMmio32Base, PciBase);\r
-    PcdSet64 (PcdPciMmio32Size, 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
@@ -266,8 +270,10 @@ MemMapInitialization (
     PciIoBase,\r
     PciIoSize\r
     );\r
-  PcdSet64 (PcdPciIoBase, PciIoBase);\r
-  PcdSet64 (PcdPciIoSize, PciIoSize);\r
+  PcdStatus = PcdSet64S (PcdPciIoBase, PciIoBase);\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
+  PcdStatus = PcdSet64S (PcdPciIoSize, PciIoSize);\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
 }\r
 \r
 EFI_STATUS\r
@@ -316,11 +322,13 @@ GetNamedFwCfgBoolean (
 \r
 #define UPDATE_BOOLEAN_PCD_FROM_FW_CFG(TokenName)                   \\r
           do {                                                      \\r
-            BOOLEAN Setting;                                        \\r
+            BOOLEAN       Setting;                                  \\r
+            RETURN_STATUS PcdStatus;                                \\r
                                                                     \\r
             if (!EFI_ERROR (GetNamedFwCfgBoolean (                  \\r
                               "opt/ovmf/" #TokenName, &Setting))) { \\r
-              PcdSetBool (TokenName, Setting);                      \\r
+              PcdStatus = PcdSetBoolS (TokenName, Setting);         \\r
+              ASSERT_RETURN_ERROR (PcdStatus);                      \\r
             }                                                       \\r
           } while (0)\r
 \r
@@ -379,12 +387,13 @@ MiscInitialization (
   VOID\r
   )\r
 {\r
-  UINTN  PmCmd;\r
-  UINTN  Pmba;\r
-  UINT32 PmbaAndVal;\r
-  UINT32 PmbaOrVal;\r
-  UINTN  AcpiCtlReg;\r
-  UINT8  AcpiEnBit;\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
@@ -424,7 +433,8 @@ MiscInitialization (
       ASSERT (FALSE);\r
       return;\r
   }\r
-  PcdSet16 (PcdOvmfHostBridgePciDevId, mHostBridgeDevId);\r
+  PcdStatus = PcdSet16S (PcdOvmfHostBridgePciDevId, mHostBridgeDevId);\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
 \r
   //\r
   // If the appropriate IOspace enable bit is set, assume the ACPI PMBA\r
@@ -491,6 +501,7 @@ ReserveEmuVariableNvStore (
   )\r
 {\r
   EFI_PHYSICAL_ADDRESS VariableStore;\r
+  RETURN_STATUS        PcdStatus;\r
 \r
   //\r
   // Allocate storage for NV variables early on so it will be\r
@@ -500,16 +511,16 @@ ReserveEmuVariableNvStore (
   //\r
   VariableStore =\r
     (EFI_PHYSICAL_ADDRESS)(UINTN)\r
-      AllocateAlignedRuntimePages (\r
-        EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)),\r
-        PcdGet32 (PcdFlashNvStorageFtwSpareSize)\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
-  PcdSet64 (PcdEmuVariableNvStoreReserved, VariableStore);\r
+  PcdStatus = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore);\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
 }\r
 \r
 \r
@@ -555,6 +566,165 @@ S3Verification (
 }\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 mMaxCpuCount variable.\r
+**/\r
+VOID\r
+MaxCpuCountInitialization (\r
+  VOID\r
+  )\r
+{\r
+  UINT16        BootCpuCount;\r
+  RETURN_STATUS PcdStatus;\r
+\r
+  //\r
+  // Try to fetch the boot CPU count.\r
+  //\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
+    mMaxCpuCount = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\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 = ((mHostBridgeDevId == 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 ((DEBUG_WARN, "%a: modern CPU hotplug interface unavailable\n",\r
+        __FUNCTION__));\r
+      mMaxCpuCount = 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
+        // 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 ((DEBUG_WARN, "%a: QEMU v2.7 reset bug: BootCpuCount=%d "\r
+          "Present=%u\n", __FUNCTION__, BootCpuCount, Present));\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
+      mMaxCpuCount = Possible;\r
+    }\r
+  }\r
+\r
+  DEBUG ((DEBUG_INFO, "%a: BootCpuCount=%d mMaxCpuCount=%u\n", __FUNCTION__,\r
+    BootCpuCount, mMaxCpuCount));\r
+  ASSERT (BootCpuCount <= mMaxCpuCount);\r
+\r
+  PcdStatus = PcdSet32S (PcdCpuBootLogicalProcessorNumber, BootCpuCount);\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
+  PcdStatus = PcdSet32S (PcdCpuMaxLogicalProcessorNumber, mMaxCpuCount);\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
+}\r
+\r
+\r
 /**\r
   Perform Platform PEI initialization.\r
 \r
@@ -573,7 +743,7 @@ InitializePlatform (
 {\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
 \r
@@ -590,8 +760,21 @@ InitializePlatform (
   BootModeInitialization ();\r
   AddressWidthInitialization ();\r
 \r
+  //\r
+  // Query Host Bridge DID\r
+  //\r
+  mHostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
+\r
+  MaxCpuCountInitialization ();\r
+\r
+  if (FeaturePcdGet (PcdSmmSmramRequire)) {\r
+    Q35TsegMbytesInitialization ();\r
+  }\r
+\r
   PublishPeiMemory ();\r
 \r
+  QemuUc32BaseInitialization ();\r
+\r
   InitializeRamRegions ();\r
 \r
   if (mXen) {\r
@@ -599,18 +782,17 @@ InitializePlatform (
     InitializeXen ();\r
   }\r
 \r
-  //\r
-  // Query Host Bridge DID\r
-  //\r
-  mHostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
-\r
   if (mBootMode != BOOT_ON_S3_RESUME) {\r
-    ReserveEmuVariableNvStore ();\r
+    if (!FeaturePcdGet (PcdSmmSmramRequire)) {\r
+      ReserveEmuVariableNvStore ();\r
+    }\r
     PeiFvInitialization ();\r
     MemMapInitialization ();\r
     NoexecDxeInitialization ();\r
   }\r
 \r
+  InstallClearCacheCallback ();\r
+  AmdSevInitialize ();\r
   MiscInitialization ();\r
   InstallFeatureControlCallback ();\r
 \r