\r
#include "ApicId.h" // APIC_ID\r
#include "QemuCpuhp.h" // QemuCpuhpWriteCpuSelector()\r
+#include "Smbase.h" // SmbaseAllocatePostSmmPen()\r
\r
//\r
// We use this protocol for accessing IO Ports.\r
STATIC APIC_ID *mPluggedApicIds;\r
STATIC APIC_ID *mToUnplugApicIds;\r
//\r
+// Address of the non-SMRAM reserved memory page that contains the Post-SMM Pen\r
+// for hot-added CPUs.\r
+//\r
+STATIC UINT32 mPostSmmPenAddress;\r
+//\r
// Represents the registration of the CPU Hotplug MMI handler.\r
//\r
STATIC EFI_HANDLE mDispatchHandle;\r
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
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
+ UINTN NewProcessorNumberByProtocol;\r
+\r
+ NewApicId = mPluggedApicIds[PluggedIdx];\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 (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 handled this MMI.\r
//\r
return EFI_SUCCESS;\r
\r
+RevokeNewSlot:\r
+ mCpuHotPlugData->ApicId[NewSlot] = MAX_UINT64;\r
+\r
Fatal:\r
ASSERT (FALSE);\r
CpuDeadLoop ();\r
goto ReleasePluggedApicIds;\r
}\r
\r
+ //\r
+ // Allocate the Post-SMM Pen for hot-added CPUs.\r
+ //\r
+ Status = SmbaseAllocatePostSmmPen (&mPostSmmPenAddress,\r
+ SystemTable->BootServices);\r
+ if (EFI_ERROR (Status)) {\r
+ goto ReleaseToUnplugApicIds;\r
+ }\r
+\r
//\r
// Sanity-check the CPU hotplug interface.\r
//\r
Status = EFI_NOT_FOUND;\r
DEBUG ((DEBUG_ERROR, "%a: modern CPU hotplug interface: %r\n",\r
__FUNCTION__, Status));\r
- goto ReleaseToUnplugApicIds;\r
+ goto ReleasePostSmmPen;\r
}\r
\r
//\r
if (EFI_ERROR (Status)) {\r
DEBUG ((DEBUG_ERROR, "%a: MmiHandlerRegister(): %r\n", __FUNCTION__,\r
Status));\r
- goto ReleaseToUnplugApicIds;\r
+ goto ReleasePostSmmPen;\r
}\r
\r
+ //\r
+ // Install the handler for the hot-added CPUs' first SMI.\r
+ //\r
+ SmbaseInstallFirstSmiHandler ();\r
+\r
return EFI_SUCCESS;\r
\r
+ReleasePostSmmPen:\r
+ SmbaseReleasePostSmmPen (mPostSmmPenAddress, SystemTable->BootServices);\r
+ mPostSmmPenAddress = 0;\r
+\r
ReleaseToUnplugApicIds:\r
gMmst->MmFreePool (mToUnplugApicIds);\r
mToUnplugApicIds = NULL;\r