2 Root SMI handler for VCPU hotplug SMIs.
4 Copyright (c) 2020, Red Hat, Inc.
6 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <CpuHotPlugData.h> // CPU_HOT_PLUG_DATA
10 #include <IndustryStandard/Q35MchIch9.h> // ICH9_APM_CNT
11 #include <IndustryStandard/QemuCpuHotplug.h> // QEMU_CPUHP_CMD_GET_PENDING
12 #include <Library/BaseLib.h> // CpuDeadLoop()
13 #include <Library/DebugLib.h> // ASSERT()
14 #include <Library/MmServicesTableLib.h> // gMmst
15 #include <Library/PcdLib.h> // PcdGetBool()
16 #include <Library/SafeIntLib.h> // SafeUintnSub()
17 #include <Protocol/MmCpuIo.h> // EFI_MM_CPU_IO_PROTOCOL
18 #include <Protocol/SmmCpuService.h> // EFI_SMM_CPU_SERVICE_PROTOCOL
19 #include <Uefi/UefiBaseType.h> // EFI_STATUS
21 #include "ApicId.h" // APIC_ID
22 #include "QemuCpuhp.h" // QemuCpuhpWriteCpuSelector()
25 // We use this protocol for accessing IO Ports.
27 STATIC EFI_MM_CPU_IO_PROTOCOL
*mMmCpuIo
;
29 // The following protocol is used to report the addition or removal of a CPU to
30 // the SMM CPU driver (PiSmmCpuDxeSmm).
32 STATIC EFI_SMM_CPU_SERVICE_PROTOCOL
*mMmCpuService
;
34 // This structure is a communication side-channel between the
35 // EFI_SMM_CPU_SERVICE_PROTOCOL consumer (i.e., this driver) and provider
36 // (i.e., PiSmmCpuDxeSmm).
38 STATIC CPU_HOT_PLUG_DATA
*mCpuHotPlugData
;
40 // SMRAM arrays for fetching the APIC IDs of processors with pending events (of
41 // known event types), for the time of just one MMI.
43 // The lifetimes of these arrays match that of this driver only because we
44 // don't want to allocate SMRAM at OS runtime, and potentially fail (or
45 // fragment the SMRAM map).
47 // These arrays provide room for ("possible CPU count" minus one) APIC IDs
48 // each, as we don't expect every possible CPU to appear, or disappear, in a
49 // single MMI. The numbers of used (populated) elements in the arrays are
50 // determined on every MMI separately.
52 STATIC APIC_ID
*mPluggedApicIds
;
53 STATIC APIC_ID
*mToUnplugApicIds
;
55 // Represents the registration of the CPU Hotplug MMI handler.
57 STATIC EFI_HANDLE mDispatchHandle
;
61 CPU Hotplug MMI handler function.
63 This is a root MMI handler.
65 @param[in] DispatchHandle The unique handle assigned to this handler by
66 EFI_MM_SYSTEM_TABLE.MmiHandlerRegister().
68 @param[in] Context Context passed in by
69 EFI_MM_SYSTEM_TABLE.MmiManage(). Due to
70 CpuHotplugMmi() being a root MMI handler,
71 Context is ASSERT()ed to be NULL.
73 @param[in,out] CommBuffer Ignored, due to CpuHotplugMmi() being a root
76 @param[in,out] CommBufferSize Ignored, due to CpuHotplugMmi() being a root
79 @retval EFI_SUCCESS The MMI was handled and the MMI
80 source was quiesced. When returned
81 by a non-root MMI handler,
82 EFI_SUCCESS terminates the
83 processing of MMI handlers in
84 EFI_MM_SYSTEM_TABLE.MmiManage().
85 For a root MMI handler (i.e., for
86 the present function too),
87 EFI_SUCCESS behaves identically to
88 EFI_WARN_INTERRUPT_SOURCE_QUIESCED,
89 as further root MMI handlers are
91 EFI_MM_SYSTEM_TABLE.MmiManage()
94 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The MMI source has been quiesced,
95 but other handlers should still
98 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The MMI source is still pending,
99 and other handlers should still
102 @retval EFI_INTERRUPT_PENDING The MMI source could not be
109 IN EFI_HANDLE DispatchHandle
,
110 IN CONST VOID
*Context OPTIONAL
,
111 IN OUT VOID
*CommBuffer OPTIONAL
,
112 IN OUT UINTN
*CommBufferSize OPTIONAL
118 UINT32 ToUnplugCount
;
121 // Assert that we are entering this function due to our root MMI handler
124 ASSERT (DispatchHandle
== mDispatchHandle
);
126 // When MmiManage() is invoked to process root MMI handlers, the caller (the
127 // MM Core) is expected to pass in a NULL Context. MmiManage() then passes
128 // the same NULL Context to individual handlers.
130 ASSERT (Context
== NULL
);
132 // Read the MMI command value from the APM Control Port, to see if this is an
133 // MMI we should care about.
135 Status
= mMmCpuIo
->Io
.Read (mMmCpuIo
, MM_IO_UINT8
, ICH9_APM_CNT
, 1,
137 if (EFI_ERROR (Status
)) {
138 DEBUG ((DEBUG_ERROR
, "%a: failed to read ICH9_APM_CNT: %r\n", __FUNCTION__
,
141 // We couldn't even determine if the MMI was for us or not.
146 if (ApmControl
!= ICH9_APM_CNT_CPU_HOTPLUG
) {
148 // The MMI is not for us.
150 return EFI_WARN_INTERRUPT_SOURCE_QUIESCED
;
154 // Collect the CPUs with pending events.
156 Status
= QemuCpuhpCollectApicIds (
158 mCpuHotPlugData
->ArrayLength
, // PossibleCpuCount
159 mCpuHotPlugData
->ArrayLength
- 1, // ApicIdCount
165 if (EFI_ERROR (Status
)) {
168 if (ToUnplugCount
> 0) {
169 DEBUG ((DEBUG_ERROR
, "%a: hot-unplug is not supported yet\n",
175 // We've handled this MMI.
183 // We couldn't handle this MMI.
185 return EFI_INTERRUPT_PENDING
;
190 // Entry point function of this driver.
195 IN EFI_HANDLE ImageHandle
,
196 IN EFI_SYSTEM_TABLE
*SystemTable
203 // This module should only be included when SMM support is required.
205 ASSERT (FeaturePcdGet (PcdSmmSmramRequire
));
207 // This driver depends on the dynamically detected "SMRAM at default SMBASE"
210 if (!PcdGetBool (PcdQ35SmramAtDefaultSmbase
)) {
211 return EFI_UNSUPPORTED
;
215 // Errors from here on are fatal; we cannot allow the boot to proceed if we
216 // can't set up this driver to handle CPU hotplug.
218 // First, collect the protocols needed later. All of these protocols are
219 // listed in our module DEPEX.
221 Status
= gMmst
->MmLocateProtocol (&gEfiMmCpuIoProtocolGuid
,
222 NULL
/* Registration */, (VOID
**)&mMmCpuIo
);
223 if (EFI_ERROR (Status
)) {
224 DEBUG ((DEBUG_ERROR
, "%a: locate MmCpuIo: %r\n", __FUNCTION__
, Status
));
227 Status
= gMmst
->MmLocateProtocol (&gEfiSmmCpuServiceProtocolGuid
,
228 NULL
/* Registration */, (VOID
**)&mMmCpuService
);
229 if (EFI_ERROR (Status
)) {
230 DEBUG ((DEBUG_ERROR
, "%a: locate MmCpuService: %r\n", __FUNCTION__
,
236 // Our DEPEX on EFI_SMM_CPU_SERVICE_PROTOCOL guarantees that PiSmmCpuDxeSmm
237 // has pointed PcdCpuHotPlugDataAddress to CPU_HOT_PLUG_DATA in SMRAM.
239 mCpuHotPlugData
= (VOID
*)(UINTN
)PcdGet64 (PcdCpuHotPlugDataAddress
);
240 if (mCpuHotPlugData
== NULL
) {
241 Status
= EFI_NOT_FOUND
;
242 DEBUG ((DEBUG_ERROR
, "%a: CPU_HOT_PLUG_DATA: %r\n", __FUNCTION__
, Status
));
246 // If the possible CPU count is 1, there's nothing for this driver to do.
248 if (mCpuHotPlugData
->ArrayLength
== 1) {
249 return EFI_UNSUPPORTED
;
252 // Allocate the data structures that depend on the possible CPU count.
254 if (RETURN_ERROR (SafeUintnSub (mCpuHotPlugData
->ArrayLength
, 1, &Size
)) ||
255 RETURN_ERROR (SafeUintnMult (sizeof (APIC_ID
), Size
, &Size
))) {
256 Status
= EFI_ABORTED
;
257 DEBUG ((DEBUG_ERROR
, "%a: invalid CPU_HOT_PLUG_DATA\n", __FUNCTION__
));
260 Status
= gMmst
->MmAllocatePool (EfiRuntimeServicesData
, Size
,
261 (VOID
**)&mPluggedApicIds
);
262 if (EFI_ERROR (Status
)) {
263 DEBUG ((DEBUG_ERROR
, "%a: MmAllocatePool(): %r\n", __FUNCTION__
, Status
));
266 Status
= gMmst
->MmAllocatePool (EfiRuntimeServicesData
, Size
,
267 (VOID
**)&mToUnplugApicIds
);
268 if (EFI_ERROR (Status
)) {
269 DEBUG ((DEBUG_ERROR
, "%a: MmAllocatePool(): %r\n", __FUNCTION__
, Status
));
270 goto ReleasePluggedApicIds
;
274 // Sanity-check the CPU hotplug interface.
276 // Both of the following features are part of QEMU 5.0, introduced primarily
277 // in commit range 3e08b2b9cb64..3a61c8db9d25:
279 // (a) the QEMU_CPUHP_CMD_GET_ARCH_ID command of the modern CPU hotplug
282 // (b) the "SMRAM at default SMBASE" feature.
284 // From these, (b) is restricted to 5.0+ machine type versions, while (a)
285 // does not depend on machine type version. Because we ensured the stricter
286 // condition (b) through PcdQ35SmramAtDefaultSmbase above, the (a)
287 // QEMU_CPUHP_CMD_GET_ARCH_ID command must now be available too. While we
288 // can't verify the presence of precisely that command, we can still verify
289 // (sanity-check) that the modern interface is active, at least.
291 // Consult the "Typical usecases | Detecting and enabling modern CPU hotplug
292 // interface" section in QEMU's "docs/specs/acpi_cpu_hotplug.txt", on the
295 QemuCpuhpWriteCpuSelector (mMmCpuIo
, 0);
296 QemuCpuhpWriteCpuSelector (mMmCpuIo
, 0);
297 QemuCpuhpWriteCommand (mMmCpuIo
, QEMU_CPUHP_CMD_GET_PENDING
);
298 if (QemuCpuhpReadCommandData2 (mMmCpuIo
) != 0) {
299 Status
= EFI_NOT_FOUND
;
300 DEBUG ((DEBUG_ERROR
, "%a: modern CPU hotplug interface: %r\n",
301 __FUNCTION__
, Status
));
302 goto ReleaseToUnplugApicIds
;
306 // Register the handler for the CPU Hotplug MMI.
308 Status
= gMmst
->MmiHandlerRegister (
310 NULL
, // HandlerType: root MMI handler
313 if (EFI_ERROR (Status
)) {
314 DEBUG ((DEBUG_ERROR
, "%a: MmiHandlerRegister(): %r\n", __FUNCTION__
,
316 goto ReleaseToUnplugApicIds
;
321 ReleaseToUnplugApicIds
:
322 gMmst
->MmFreePool (mToUnplugApicIds
);
323 mToUnplugApicIds
= NULL
;
325 ReleasePluggedApicIds
:
326 gMmst
->MmFreePool (mPluggedApicIds
);
327 mPluggedApicIds
= NULL
;