]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/CpuHotplugSmm: refactor hotplug logic
authorAnkur Arora <ankur.a.arora@oracle.com>
Fri, 12 Mar 2021 06:26:47 +0000 (22:26 -0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Tue, 16 Mar 2021 13:21:46 +0000 (13:21 +0000)
Refactor CpuHotplugMmi() to pull out the CPU hotplug logic into
ProcessHotAddedCpus(). This is in preparation for supporting CPU
hot-unplug.

Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Aaron Young <aaron.young@oracle.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3132
Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20210312062656.2477515-2-ankur.a.arora@oracle.com>

OvmfPkg/CpuHotplugSmm/CpuHotplug.c

index cfe698ed2b5e1a555dc38f41654204d35da48254..bf68fcd42914188f5c89be1180d687a877c24383 100644 (file)
@@ -62,6 +62,129 @@ STATIC UINT32 mPostSmmPenAddress;
 //\r
 STATIC EFI_HANDLE mDispatchHandle;\r
 \r
+/**\r
+  Process CPUs that have been hot-added, per QemuCpuhpCollectApicIds().\r
+\r
+  For each such CPU, relocate the SMBASE, and report the CPU to PiSmmCpuDxeSmm\r
+  via EFI_SMM_CPU_SERVICE_PROTOCOL. If the supposedly hot-added CPU is already\r
+  known, skip it silently.\r
+\r
+  @param[in] PluggedApicIds    The APIC IDs of the CPUs that have been\r
+                               hot-plugged.\r
+\r
+  @param[in] PluggedCount      The number of filled-in APIC IDs in\r
+                               PluggedApicIds.\r
+\r
+  @retval EFI_SUCCESS          CPUs corresponding to all the APIC IDs are\r
+                               populated.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES Out of APIC ID space in "mCpuHotPlugData".\r
+\r
+  @return                      Error codes propagated from SmbaseRelocate()\r
+                               and mMmCpuService->AddProcessor().\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+ProcessHotAddedCpus (\r
+  IN APIC_ID                      *PluggedApicIds,\r
+  IN UINT32                       PluggedCount\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+  UINT32     PluggedIdx;\r
+  UINT32     NewSlot;\r
+\r
+  //\r
+  // The Post-SMM Pen need not be reinstalled multiple times within a single\r
+  // root MMI handling. Even reinstalling once per root MMI is only prudence;\r
+  // in theory installing the pen in the driver's entry point function should\r
+  // suffice.\r
+  //\r
+  SmbaseReinstallPostSmmPen (mPostSmmPenAddress);\r
+\r
+  PluggedIdx = 0;\r
+  NewSlot = 0;\r
+  while (PluggedIdx < PluggedCount) {\r
+    APIC_ID NewApicId;\r
+    UINT32  CheckSlot;\r
+    UINTN   NewProcessorNumberByProtocol;\r
+\r
+    NewApicId = PluggedApicIds[PluggedIdx];\r
+\r
+    //\r
+    // Check if the supposedly hot-added CPU is already known to us.\r
+    //\r
+    for (CheckSlot = 0;\r
+         CheckSlot < mCpuHotPlugData->ArrayLength;\r
+         CheckSlot++) {\r
+      if (mCpuHotPlugData->ApicId[CheckSlot] == NewApicId) {\r
+        break;\r
+      }\r
+    }\r
+    if (CheckSlot < mCpuHotPlugData->ArrayLength) {\r
+      DEBUG ((DEBUG_VERBOSE, "%a: APIC ID " FMT_APIC_ID " was hot-plugged "\r
+        "before; ignoring it\n", __FUNCTION__, NewApicId));\r
+      PluggedIdx++;\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // Find the first empty slot in CPU_HOT_PLUG_DATA.\r
+    //\r
+    while (NewSlot < mCpuHotPlugData->ArrayLength &&\r
+           mCpuHotPlugData->ApicId[NewSlot] != MAX_UINT64) {\r
+      NewSlot++;\r
+    }\r
+    if (NewSlot == mCpuHotPlugData->ArrayLength) {\r
+      DEBUG ((DEBUG_ERROR, "%a: no room for APIC ID " FMT_APIC_ID "\n",\r
+        __FUNCTION__, NewApicId));\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    //\r
+    // Store the APIC ID of the new processor to the slot.\r
+    //\r
+    mCpuHotPlugData->ApicId[NewSlot] = NewApicId;\r
+\r
+    //\r
+    // Relocate the SMBASE of the new CPU.\r
+    //\r
+    Status = SmbaseRelocate (NewApicId, mCpuHotPlugData->SmBase[NewSlot],\r
+               mPostSmmPenAddress);\r
+    if (EFI_ERROR (Status)) {\r
+      goto RevokeNewSlot;\r
+    }\r
+\r
+    //\r
+    // Add the new CPU with EFI_SMM_CPU_SERVICE_PROTOCOL.\r
+    //\r
+    Status = mMmCpuService->AddProcessor (mMmCpuService, NewApicId,\r
+                              &NewProcessorNumberByProtocol);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_ERROR, "%a: AddProcessor(" FMT_APIC_ID "): %r\n",\r
+        __FUNCTION__, NewApicId, Status));\r
+      goto RevokeNewSlot;\r
+    }\r
+\r
+    DEBUG ((DEBUG_INFO, "%a: hot-added APIC ID " FMT_APIC_ID ", SMBASE 0x%Lx, "\r
+      "EFI_SMM_CPU_SERVICE_PROTOCOL assigned number %Lu\n", __FUNCTION__,\r
+      NewApicId, (UINT64)mCpuHotPlugData->SmBase[NewSlot],\r
+      (UINT64)NewProcessorNumberByProtocol));\r
+\r
+    NewSlot++;\r
+    PluggedIdx++;\r
+  }\r
+\r
+  //\r
+  // We've processed this batch of hot-added CPUs.\r
+  //\r
+  return EFI_SUCCESS;\r
+\r
+RevokeNewSlot:\r
+  mCpuHotPlugData->ApicId[NewSlot] = MAX_UINT64;\r
+\r
+  return Status;\r
+}\r
 \r
 /**\r
   CPU Hotplug MMI handler function.\r
@@ -122,8 +245,6 @@ CpuHotplugMmi (
   UINT8      ApmControl;\r
   UINT32     PluggedCount;\r
   UINT32     ToUnplugCount;\r
-  UINT32     PluggedIdx;\r
-  UINT32     NewSlot;\r
 \r
   //\r
   // Assert that we are entering this function due to our root MMI handler\r
@@ -179,87 +300,11 @@ CpuHotplugMmi (
     goto Fatal;\r
   }\r
 \r
-  //\r
-  // Process hot-added CPUs.\r
-  //\r
-  // The Post-SMM Pen need not be reinstalled multiple times within a single\r
-  // root MMI handling. Even reinstalling once per root MMI is only prudence;\r
-  // in theory installing the pen in the driver's entry point function should\r
-  // suffice.\r
-  //\r
-  SmbaseReinstallPostSmmPen (mPostSmmPenAddress);\r
-\r
-  PluggedIdx = 0;\r
-  NewSlot = 0;\r
-  while (PluggedIdx < PluggedCount) {\r
-    APIC_ID NewApicId;\r
-    UINT32  CheckSlot;\r
-    UINTN   NewProcessorNumberByProtocol;\r
-\r
-    NewApicId = mPluggedApicIds[PluggedIdx];\r
-\r
-    //\r
-    // Check if the supposedly hot-added CPU is already known to us.\r
-    //\r
-    for (CheckSlot = 0;\r
-         CheckSlot < mCpuHotPlugData->ArrayLength;\r
-         CheckSlot++) {\r
-      if (mCpuHotPlugData->ApicId[CheckSlot] == NewApicId) {\r
-        break;\r
-      }\r
-    }\r
-    if (CheckSlot < mCpuHotPlugData->ArrayLength) {\r
-      DEBUG ((DEBUG_VERBOSE, "%a: APIC ID " FMT_APIC_ID " was hot-plugged "\r
-        "before; ignoring it\n", __FUNCTION__, NewApicId));\r
-      PluggedIdx++;\r
-      continue;\r
-    }\r
-\r
-    //\r
-    // Find the first empty slot in CPU_HOT_PLUG_DATA.\r
-    //\r
-    while (NewSlot < mCpuHotPlugData->ArrayLength &&\r
-           mCpuHotPlugData->ApicId[NewSlot] != MAX_UINT64) {\r
-      NewSlot++;\r
-    }\r
-    if (NewSlot == mCpuHotPlugData->ArrayLength) {\r
-      DEBUG ((DEBUG_ERROR, "%a: no room for APIC ID " FMT_APIC_ID "\n",\r
-        __FUNCTION__, NewApicId));\r
-      goto Fatal;\r
-    }\r
-\r
-    //\r
-    // Store the APIC ID of the new processor to the slot.\r
-    //\r
-    mCpuHotPlugData->ApicId[NewSlot] = NewApicId;\r
-\r
-    //\r
-    // Relocate the SMBASE of the new CPU.\r
-    //\r
-    Status = SmbaseRelocate (NewApicId, mCpuHotPlugData->SmBase[NewSlot],\r
-               mPostSmmPenAddress);\r
-    if (EFI_ERROR (Status)) {\r
-      goto RevokeNewSlot;\r
-    }\r
-\r
-    //\r
-    // Add the new CPU with EFI_SMM_CPU_SERVICE_PROTOCOL.\r
-    //\r
-    Status = mMmCpuService->AddProcessor (mMmCpuService, NewApicId,\r
-                              &NewProcessorNumberByProtocol);\r
+  if (PluggedCount > 0) {\r
+    Status = ProcessHotAddedCpus (mPluggedApicIds, PluggedCount);\r
     if (EFI_ERROR (Status)) {\r
-      DEBUG ((DEBUG_ERROR, "%a: AddProcessor(" FMT_APIC_ID "): %r\n",\r
-        __FUNCTION__, NewApicId, Status));\r
-      goto RevokeNewSlot;\r
+      goto Fatal;\r
     }\r
-\r
-    DEBUG ((DEBUG_INFO, "%a: hot-added APIC ID " FMT_APIC_ID ", SMBASE 0x%Lx, "\r
-      "EFI_SMM_CPU_SERVICE_PROTOCOL assigned number %Lu\n", __FUNCTION__,\r
-      NewApicId, (UINT64)mCpuHotPlugData->SmBase[NewSlot],\r
-      (UINT64)NewProcessorNumberByProtocol));\r
-\r
-    NewSlot++;\r
-    PluggedIdx++;\r
   }\r
 \r
   //\r
@@ -267,9 +312,6 @@ CpuHotplugMmi (
   //\r
   return EFI_SUCCESS;\r
 \r
-RevokeNewSlot:\r
-  mCpuHotPlugData->ApicId[NewSlot] = MAX_UINT64;\r
-\r
 Fatal:\r
   ASSERT (FALSE);\r
   CpuDeadLoop ();\r