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/CpuLib.h> // CpuSleep()
14 #include <Library/DebugLib.h> // ASSERT()
15 #include <Library/MmServicesTableLib.h> // gMmst
16 #include <Library/PcdLib.h> // PcdGetBool()
17 #include <Library/SafeIntLib.h> // SafeUintnSub()
18 #include <Pcd/CpuHotEjectData.h> // CPU_HOT_EJECT_DATA
19 #include <Protocol/MmCpuIo.h> // EFI_MM_CPU_IO_PROTOCOL
20 #include <Protocol/SmmCpuService.h> // EFI_SMM_CPU_SERVICE_PROTOCOL
21 #include <Register/Intel/ArchitecturalMsr.h> // MSR_IA32_APIC_BASE_REGISTER
22 #include <Uefi/UefiBaseType.h> // EFI_STATUS
24 #include "ApicId.h" // APIC_ID
25 #include "QemuCpuhp.h" // QemuCpuhpWriteCpuSelector()
26 #include "Smbase.h" // SmbaseAllocatePostSmmPen()
29 // We use this protocol for accessing IO Ports.
31 STATIC EFI_MM_CPU_IO_PROTOCOL
*mMmCpuIo
;
33 // The following protocol is used to report the addition or removal of a CPU to
34 // the SMM CPU driver (PiSmmCpuDxeSmm).
36 STATIC EFI_SMM_CPU_SERVICE_PROTOCOL
*mMmCpuService
;
38 // These structures serve as communication side-channels between the
39 // EFI_SMM_CPU_SERVICE_PROTOCOL consumer (i.e., this driver) and provider
40 // (i.e., PiSmmCpuDxeSmm).
42 STATIC CPU_HOT_PLUG_DATA
*mCpuHotPlugData
;
43 STATIC CPU_HOT_EJECT_DATA
*mCpuHotEjectData
;
45 // SMRAM arrays for fetching the APIC IDs of processors with pending events (of
46 // known event types), for the time of just one MMI.
48 // The lifetimes of these arrays match that of this driver only because we
49 // don't want to allocate SMRAM at OS runtime, and potentially fail (or
50 // fragment the SMRAM map).
52 // The first array stores APIC IDs for hot-plug events, the second and the
53 // third store APIC IDs and QEMU CPU Selectors (both indexed similarly) for
54 // hot-unplug events. All of these provide room for "possible CPU count" minus
55 // one elements as we don't expect every possible CPU to appear, or disappear,
56 // in a single MMI. The numbers of used (populated) elements in the arrays are
57 // determined on every MMI separately.
59 STATIC APIC_ID
*mPluggedApicIds
;
60 STATIC APIC_ID
*mToUnplugApicIds
;
61 STATIC UINT32
*mToUnplugSelectors
;
63 // Address of the non-SMRAM reserved memory page that contains the Post-SMM Pen
64 // for hot-added CPUs.
66 STATIC UINT32 mPostSmmPenAddress
;
68 // Represents the registration of the CPU Hotplug MMI handler.
70 STATIC EFI_HANDLE mDispatchHandle
;
73 Process CPUs that have been hot-added, per QemuCpuhpCollectApicIds().
75 For each such CPU, relocate the SMBASE, and report the CPU to PiSmmCpuDxeSmm
76 via EFI_SMM_CPU_SERVICE_PROTOCOL. If the supposedly hot-added CPU is already
77 known, skip it silently.
79 @param[in] PluggedApicIds The APIC IDs of the CPUs that have been
82 @param[in] PluggedCount The number of filled-in APIC IDs in
85 @retval EFI_SUCCESS CPUs corresponding to all the APIC IDs are
88 @retval EFI_OUT_OF_RESOURCES Out of APIC ID space in "mCpuHotPlugData".
90 @return Error codes propagated from SmbaseRelocate()
91 and mMmCpuService->AddProcessor().
96 IN APIC_ID
*PluggedApicIds
,
97 IN UINT32 PluggedCount
105 // The Post-SMM Pen need not be reinstalled multiple times within a single
106 // root MMI handling. Even reinstalling once per root MMI is only prudence;
107 // in theory installing the pen in the driver's entry point function should
110 SmbaseReinstallPostSmmPen (mPostSmmPenAddress
);
114 while (PluggedIdx
< PluggedCount
) {
117 UINTN NewProcessorNumberByProtocol
;
119 NewApicId
= PluggedApicIds
[PluggedIdx
];
122 // Check if the supposedly hot-added CPU is already known to us.
125 CheckSlot
< mCpuHotPlugData
->ArrayLength
;
128 if (mCpuHotPlugData
->ApicId
[CheckSlot
] == NewApicId
) {
133 if (CheckSlot
< mCpuHotPlugData
->ArrayLength
) {
136 "%a: APIC ID " FMT_APIC_ID
" was hot-plugged "
137 "before; ignoring it\n",
146 // Find the first empty slot in CPU_HOT_PLUG_DATA.
148 while (NewSlot
< mCpuHotPlugData
->ArrayLength
&&
149 mCpuHotPlugData
->ApicId
[NewSlot
] != MAX_UINT64
)
154 if (NewSlot
== mCpuHotPlugData
->ArrayLength
) {
157 "%a: no room for APIC ID " FMT_APIC_ID
"\n",
161 return EFI_OUT_OF_RESOURCES
;
165 // Store the APIC ID of the new processor to the slot.
167 mCpuHotPlugData
->ApicId
[NewSlot
] = NewApicId
;
170 // Relocate the SMBASE of the new CPU.
172 Status
= SmbaseRelocate (
174 mCpuHotPlugData
->SmBase
[NewSlot
],
177 if (EFI_ERROR (Status
)) {
182 // Add the new CPU with EFI_SMM_CPU_SERVICE_PROTOCOL.
184 Status
= mMmCpuService
->AddProcessor (
187 &NewProcessorNumberByProtocol
189 if (EFI_ERROR (Status
)) {
192 "%a: AddProcessor(" FMT_APIC_ID
"): %r\n",
202 "%a: hot-added APIC ID " FMT_APIC_ID
", SMBASE 0x%Lx, "
203 "EFI_SMM_CPU_SERVICE_PROTOCOL assigned number %Lu\n",
206 (UINT64
)mCpuHotPlugData
->SmBase
[NewSlot
],
207 (UINT64
)NewProcessorNumberByProtocol
215 // We've processed this batch of hot-added CPUs.
220 mCpuHotPlugData
->ApicId
[NewSlot
] = MAX_UINT64
;
226 EjectCpu needs to know the BSP at SMI exit at a point when
227 some of the EFI_SMM_CPU_SERVICE_PROTOCOL state has been torn
229 Reuse the logic from OvmfPkg::PlatformSmmBspElection() to
232 @retval TRUE If the CPU executing this function is the BSP.
234 @retval FALSE If the CPU executing this function is an AP.
242 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
245 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
246 IsBsp
= (BOOLEAN
)(ApicBaseMsr
.Bits
.BSP
== 1);
251 CPU Hot-eject handler, called from SmmCpuFeaturesRendezvousExit()
252 on each CPU at exit from SMM.
254 If, the executing CPU is neither the BSP, nor being ejected, nothing
256 If, the executing CPU is being ejected, wait in a halted loop
258 If, the executing CPU is the BSP, set QEMU CPU status to eject
259 for CPUs being ejected.
261 @param[in] ProcessorNum ProcessorNum denotes the CPU exiting SMM,
262 and will be used as an index into
263 CPU_HOT_EJECT_DATA->QemuSelectorMap. It is
264 identical to the processor handle number in
265 EFI_SMM_CPU_SERVICE_PROTOCOL.
270 IN UINTN ProcessorNum
278 for (Idx
= 0; Idx
< mCpuHotEjectData
->ArrayLength
; Idx
++) {
279 QemuSelector
= mCpuHotEjectData
->QemuSelectorMap
[Idx
];
281 if (QemuSelector
!= CPU_EJECT_QEMU_SELECTOR_INVALID
) {
283 // This to-be-ejected-CPU has already received the BSP's SMI exit
284 // signal and will execute SmmCpuFeaturesRendezvousExit()
285 // followed by this callback or is already penned in the
286 // CpuSleep() loop below.
288 // Tell QEMU to context-switch it out.
290 QemuCpuhpWriteCpuSelector (mMmCpuIo
, (UINT32
)QemuSelector
);
291 QemuCpuhpWriteCpuStatus (mMmCpuIo
, QEMU_CPUHP_STAT_EJECT
);
294 // Now that we've ejected the CPU corresponding to QemuSelectorMap[Idx],
295 // clear its eject status to ensure that an invalid future SMI does
296 // not end up trying a spurious eject or a newly hotplugged CPU does
297 // not get penned in the CpuSleep() loop.
299 // Note that the QemuCpuhpWriteCpuStatus() command above is a write to
300 // a different address space and uses the EFI_MM_CPU_IO_PROTOCOL.
302 // This means that we are guaranteed that the following assignment
303 // will not be reordered before the eject. And, so we can safely
304 // do this write here.
306 mCpuHotEjectData
->QemuSelectorMap
[Idx
] =
307 CPU_EJECT_QEMU_SELECTOR_INVALID
;
311 "%a: Unplugged ProcessorNum %u, "
312 "QemuSelector %Lu\n",
321 // We are done until the next hot-unplug; clear the handler.
323 // mCpuHotEjectData->Handler is a NOP for any CPU not under ejection.
324 // So, once we are done with all the ejections, we can safely reset it
325 // here since any CPU dereferencing it would only see either the old
326 // or the new value (since it is aligned at a natural boundary.)
328 mCpuHotEjectData
->Handler
= NULL
;
333 // Reached only on APs
337 // mCpuHotEjectData->QemuSelectorMap[ProcessorNum] is updated
338 // on the BSP in the ongoing SMI at two places:
340 // - UnplugCpus() where the BSP determines if a CPU is under ejection
341 // or not. As a comment in UnplugCpus() at set-up, and in
342 // SmmCpuFeaturesRendezvousExit() where it is dereferenced describe,
343 // any such updates are guaranteed to be ordered-before the
344 // dereference below.
346 // - EjectCpu() on the BSP (above) updates QemuSelectorMap[ProcessorNum]
347 // for a CPU once it's ejected.
349 // The CPU under ejection: might be executing anywhere between the
350 // AllCpusInSync loop in SmiRendezvous(), to about to dereference
351 // QemuSelectorMap[ProcessorNum].
352 // As described in the comment above where we do the reset, this
353 // is not a problem since the ejected CPU never sees the after value.
354 // CPUs not-under ejection: never see any changes so they are fine.
356 QemuSelector
= mCpuHotEjectData
->QemuSelectorMap
[ProcessorNum
];
357 if (QemuSelector
== CPU_EJECT_QEMU_SELECTOR_INVALID
) {
362 // APs being unplugged get here from SmmCpuFeaturesRendezvousExit()
363 // after having been cleared to exit the SMI and so have no SMM
364 // processing remaining.
366 // Keep them penned here until the BSP tells QEMU to eject them.
369 DisableInterrupts ();
375 Process to be hot-unplugged CPUs, per QemuCpuhpCollectApicIds().
377 For each such CPU, report the CPU to PiSmmCpuDxeSmm via
378 EFI_SMM_CPU_SERVICE_PROTOCOL and stash the QEMU Cpu Selectors for later
379 ejection. If the to be hot-unplugged CPU is unknown, skip it silently.
381 Additonally, if we do stash any Cpu Selectors, also install a CPU eject
382 handler which would handle the ejection.
384 @param[in] ToUnplugApicIds The APIC IDs of the CPUs that are about to be
387 @param[in] ToUnplugSelectors The QEMU Selectors of the CPUs that are about to
390 @param[in] ToUnplugCount The number of filled-in APIC IDs in
393 @retval EFI_ALREADY_STARTED For the ProcessorNum that
394 EFI_SMM_CPU_SERVICE_PROTOCOL had assigned to
395 one of the APIC IDs in ToUnplugApicIds,
396 mCpuHotEjectData->QemuSelectorMap already has
397 the QemuSelector value stashed. (This should
400 @retval EFI_SUCCESS Known APIC IDs have been removed from SMM data
403 @return Error codes propagated from
404 mMmCpuService->RemoveProcessor().
409 IN APIC_ID
*ToUnplugApicIds
,
410 IN UINT32
*ToUnplugSelectors
,
411 IN UINT32 ToUnplugCount
421 while (ToUnplugIdx
< ToUnplugCount
) {
422 APIC_ID RemoveApicId
;
425 RemoveApicId
= ToUnplugApicIds
[ToUnplugIdx
];
426 QemuSelector
= ToUnplugSelectors
[ToUnplugIdx
];
429 // mCpuHotPlugData->ApicId maps ProcessorNum -> ApicId. Use RemoveApicId
430 // to find the corresponding ProcessorNum for the CPU to be removed.
432 // With this we can establish a 3 way mapping:
433 // APIC_ID -- ProcessorNum -- QemuSelector
435 // We stash the ProcessorNum -> QemuSelector mapping so it can later be
436 // used for CPU hot-eject in SmmCpuFeaturesRendezvousExit() context (where
437 // we only have ProcessorNum available.)
440 for (ProcessorNum
= 0;
441 ProcessorNum
< mCpuHotPlugData
->ArrayLength
;
444 if (mCpuHotPlugData
->ApicId
[ProcessorNum
] == RemoveApicId
) {
450 // Ignore the unplug if APIC ID not found
452 if (ProcessorNum
== mCpuHotPlugData
->ArrayLength
) {
455 "%a: did not find APIC ID " FMT_APIC_ID
465 // Mark ProcessorNum for removal from SMM data structures
467 Status
= mMmCpuService
->RemoveProcessor (mMmCpuService
, ProcessorNum
);
468 if (EFI_ERROR (Status
)) {
471 "%a: RemoveProcessor(" FMT_APIC_ID
"): %r\n",
479 if (mCpuHotEjectData
->QemuSelectorMap
[ProcessorNum
] !=
480 CPU_EJECT_QEMU_SELECTOR_INVALID
)
483 // mCpuHotEjectData->QemuSelectorMap[ProcessorNum] is set to
484 // CPU_EJECT_QEMU_SELECTOR_INVALID when mCpuHotEjectData->QemuSelectorMap
485 // is allocated, and once the subject processsor is ejected.
487 // Additionally, mMmCpuService->RemoveProcessor(ProcessorNum) invalidates
488 // mCpuHotPlugData->ApicId[ProcessorNum], so a given ProcessorNum can
489 // never match more than one APIC ID -- nor, by transitivity, designate
490 // more than one QemuSelector -- in a single invocation of UnplugCpus().
494 "%a: ProcessorNum %Lu maps to QemuSelector %Lu, "
495 "cannot also map to %u\n",
497 (UINT64
)ProcessorNum
,
498 mCpuHotEjectData
->QemuSelectorMap
[ProcessorNum
],
502 return EFI_ALREADY_STARTED
;
506 // Stash the QemuSelector so we can do the actual ejection later.
508 mCpuHotEjectData
->QemuSelectorMap
[ProcessorNum
] = (UINT64
)QemuSelector
;
512 "%a: Started hot-unplug on ProcessorNum %Lu, APIC ID "
513 FMT_APIC_ID
", QemuSelector %u\n",
515 (UINT64
)ProcessorNum
,
524 if (EjectCount
!= 0) {
526 // We have processors to be ejected; install the handler.
528 mCpuHotEjectData
->Handler
= EjectCpu
;
531 // The BSP and APs load mCpuHotEjectData->Handler, and
532 // mCpuHotEjectData->QemuSelectorMap[] in SmmCpuFeaturesRendezvousExit()
535 // The comment in SmmCpuFeaturesRendezvousExit() details how we use
536 // the AllCpusInSync control-dependency to ensure that any loads are
537 // ordered-after the stores above.
539 // Ensure that the stores above are ordered-before the AllCpusInSync store
540 // by using a MemoryFence() with release semantics.
546 // We've removed this set of APIC IDs from SMM data structures and
547 // have installed an ejection handler if needed.
553 CPU Hotplug MMI handler function.
555 This is a root MMI handler.
557 @param[in] DispatchHandle The unique handle assigned to this handler by
558 EFI_MM_SYSTEM_TABLE.MmiHandlerRegister().
560 @param[in] Context Context passed in by
561 EFI_MM_SYSTEM_TABLE.MmiManage(). Due to
562 CpuHotplugMmi() being a root MMI handler,
563 Context is ASSERT()ed to be NULL.
565 @param[in,out] CommBuffer Ignored, due to CpuHotplugMmi() being a root
568 @param[in,out] CommBufferSize Ignored, due to CpuHotplugMmi() being a root
571 @retval EFI_SUCCESS The MMI was handled and the MMI
572 source was quiesced. When returned
573 by a non-root MMI handler,
574 EFI_SUCCESS terminates the
575 processing of MMI handlers in
576 EFI_MM_SYSTEM_TABLE.MmiManage().
577 For a root MMI handler (i.e., for
578 the present function too),
579 EFI_SUCCESS behaves identically to
580 EFI_WARN_INTERRUPT_SOURCE_QUIESCED,
581 as further root MMI handlers are
582 going to be called by
583 EFI_MM_SYSTEM_TABLE.MmiManage()
586 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The MMI source has been quiesced,
587 but other handlers should still
590 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The MMI source is still pending,
591 and other handlers should still
594 @retval EFI_INTERRUPT_PENDING The MMI source could not be
601 IN EFI_HANDLE DispatchHandle
,
602 IN CONST VOID
*Context OPTIONAL
,
603 IN OUT VOID
*CommBuffer OPTIONAL
,
604 IN OUT UINTN
*CommBufferSize OPTIONAL
610 UINT32 ToUnplugCount
;
613 // Assert that we are entering this function due to our root MMI handler
616 ASSERT (DispatchHandle
== mDispatchHandle
);
618 // When MmiManage() is invoked to process root MMI handlers, the caller (the
619 // MM Core) is expected to pass in a NULL Context. MmiManage() then passes
620 // the same NULL Context to individual handlers.
622 ASSERT (Context
== NULL
);
624 // Read the MMI command value from the APM Control Port, to see if this is an
625 // MMI we should care about.
627 Status
= mMmCpuIo
->Io
.Read (
634 if (EFI_ERROR (Status
)) {
637 "%a: failed to read ICH9_APM_CNT: %r\n",
642 // We couldn't even determine if the MMI was for us or not.
647 if (ApmControl
!= ICH9_APM_CNT_CPU_HOTPLUG
) {
649 // The MMI is not for us.
651 return EFI_WARN_INTERRUPT_SOURCE_QUIESCED
;
655 // Collect the CPUs with pending events.
657 Status
= QemuCpuhpCollectApicIds (
659 mCpuHotPlugData
->ArrayLength
, // PossibleCpuCount
660 mCpuHotPlugData
->ArrayLength
- 1, // ApicIdCount
667 if (EFI_ERROR (Status
)) {
671 if (PluggedCount
> 0) {
672 Status
= ProcessHotAddedCpus (mPluggedApicIds
, PluggedCount
);
673 if (EFI_ERROR (Status
)) {
678 if (ToUnplugCount
> 0) {
679 Status
= UnplugCpus (mToUnplugApicIds
, mToUnplugSelectors
, ToUnplugCount
);
680 if (EFI_ERROR (Status
)) {
686 // We've handled this MMI.
694 // We couldn't handle this MMI.
696 return EFI_INTERRUPT_PENDING
;
700 // Entry point function of this driver.
705 IN EFI_HANDLE ImageHandle
,
706 IN EFI_SYSTEM_TABLE
*SystemTable
715 // This module should only be included when SMM support is required.
717 ASSERT (FeaturePcdGet (PcdSmmSmramRequire
));
719 // This driver depends on the dynamically detected "SMRAM at default SMBASE"
722 if (!PcdGetBool (PcdQ35SmramAtDefaultSmbase
)) {
723 return EFI_UNSUPPORTED
;
727 // Errors from here on are fatal; we cannot allow the boot to proceed if we
728 // can't set up this driver to handle CPU hotplug.
730 // First, collect the protocols needed later. All of these protocols are
731 // listed in our module DEPEX.
733 Status
= gMmst
->MmLocateProtocol (
734 &gEfiMmCpuIoProtocolGuid
,
735 NULL
/* Registration */,
738 if (EFI_ERROR (Status
)) {
739 DEBUG ((DEBUG_ERROR
, "%a: locate MmCpuIo: %r\n", __FUNCTION__
, Status
));
743 Status
= gMmst
->MmLocateProtocol (
744 &gEfiSmmCpuServiceProtocolGuid
,
745 NULL
/* Registration */,
746 (VOID
**)&mMmCpuService
748 if (EFI_ERROR (Status
)) {
751 "%a: locate MmCpuService: %r\n",
759 // Our DEPEX on EFI_SMM_CPU_SERVICE_PROTOCOL guarantees that PiSmmCpuDxeSmm
761 // - PcdCpuHotPlugDataAddress to CPU_HOT_PLUG_DATA in SMRAM,
762 // - PcdCpuHotEjectDataAddress to CPU_HOT_EJECT_DATA in SMRAM, if the
763 // possible CPU count is greater than 1.
765 mCpuHotPlugData
= (VOID
*)(UINTN
)PcdGet64 (PcdCpuHotPlugDataAddress
);
766 mCpuHotEjectData
= (VOID
*)(UINTN
)PcdGet64 (PcdCpuHotEjectDataAddress
);
768 if (mCpuHotPlugData
== NULL
) {
769 Status
= EFI_NOT_FOUND
;
770 DEBUG ((DEBUG_ERROR
, "%a: CPU_HOT_PLUG_DATA: %r\n", __FUNCTION__
, Status
));
775 // If the possible CPU count is 1, there's nothing for this driver to do.
777 if (mCpuHotPlugData
->ArrayLength
== 1) {
778 return EFI_UNSUPPORTED
;
781 if (mCpuHotEjectData
== NULL
) {
782 Status
= EFI_NOT_FOUND
;
783 } else if (mCpuHotPlugData
->ArrayLength
!= mCpuHotEjectData
->ArrayLength
) {
784 Status
= EFI_INVALID_PARAMETER
;
786 Status
= EFI_SUCCESS
;
789 if (EFI_ERROR (Status
)) {
790 DEBUG ((DEBUG_ERROR
, "%a: CPU_HOT_EJECT_DATA: %r\n", __FUNCTION__
, Status
));
795 // Allocate the data structures that depend on the possible CPU count.
797 if (RETURN_ERROR (SafeUintnSub (mCpuHotPlugData
->ArrayLength
, 1, &Len
)) ||
798 RETURN_ERROR (SafeUintnMult (sizeof (APIC_ID
), Len
, &Size
)) ||
799 RETURN_ERROR (SafeUintnMult (sizeof (UINT32
), Len
, &SizeSel
)))
801 Status
= EFI_ABORTED
;
802 DEBUG ((DEBUG_ERROR
, "%a: invalid CPU_HOT_PLUG_DATA\n", __FUNCTION__
));
806 Status
= gMmst
->MmAllocatePool (
807 EfiRuntimeServicesData
,
809 (VOID
**)&mPluggedApicIds
811 if (EFI_ERROR (Status
)) {
812 DEBUG ((DEBUG_ERROR
, "%a: MmAllocatePool(): %r\n", __FUNCTION__
, Status
));
816 Status
= gMmst
->MmAllocatePool (
817 EfiRuntimeServicesData
,
819 (VOID
**)&mToUnplugApicIds
821 if (EFI_ERROR (Status
)) {
822 DEBUG ((DEBUG_ERROR
, "%a: MmAllocatePool(): %r\n", __FUNCTION__
, Status
));
823 goto ReleasePluggedApicIds
;
826 Status
= gMmst
->MmAllocatePool (
827 EfiRuntimeServicesData
,
829 (VOID
**)&mToUnplugSelectors
831 if (EFI_ERROR (Status
)) {
832 DEBUG ((DEBUG_ERROR
, "%a: MmAllocatePool(): %r\n", __FUNCTION__
, Status
));
833 goto ReleaseToUnplugApicIds
;
837 // Allocate the Post-SMM Pen for hot-added CPUs.
839 Status
= SmbaseAllocatePostSmmPen (
841 SystemTable
->BootServices
843 if (EFI_ERROR (Status
)) {
844 goto ReleaseToUnplugSelectors
;
848 // Sanity-check the CPU hotplug interface.
850 // Both of the following features are part of QEMU 5.0, introduced primarily
851 // in commit range 3e08b2b9cb64..3a61c8db9d25:
853 // (a) the QEMU_CPUHP_CMD_GET_ARCH_ID command of the modern CPU hotplug
856 // (b) the "SMRAM at default SMBASE" feature.
858 // From these, (b) is restricted to 5.0+ machine type versions, while (a)
859 // does not depend on machine type version. Because we ensured the stricter
860 // condition (b) through PcdQ35SmramAtDefaultSmbase above, the (a)
861 // QEMU_CPUHP_CMD_GET_ARCH_ID command must now be available too. While we
862 // can't verify the presence of precisely that command, we can still verify
863 // (sanity-check) that the modern interface is active, at least.
865 // Consult the "Typical usecases | Detecting and enabling modern CPU hotplug
866 // interface" section in QEMU's "docs/specs/acpi_cpu_hotplug.txt", on the
869 QemuCpuhpWriteCpuSelector (mMmCpuIo
, 0);
870 QemuCpuhpWriteCpuSelector (mMmCpuIo
, 0);
871 QemuCpuhpWriteCommand (mMmCpuIo
, QEMU_CPUHP_CMD_GET_PENDING
);
872 if (QemuCpuhpReadCommandData2 (mMmCpuIo
) != 0) {
873 Status
= EFI_NOT_FOUND
;
876 "%a: modern CPU hotplug interface: %r\n",
880 goto ReleasePostSmmPen
;
884 // Register the handler for the CPU Hotplug MMI.
886 Status
= gMmst
->MmiHandlerRegister (
888 NULL
, // HandlerType: root MMI handler
891 if (EFI_ERROR (Status
)) {
894 "%a: MmiHandlerRegister(): %r\n",
898 goto ReleasePostSmmPen
;
902 // Install the handler for the hot-added CPUs' first SMI.
904 SmbaseInstallFirstSmiHandler ();
909 SmbaseReleasePostSmmPen (mPostSmmPenAddress
, SystemTable
->BootServices
);
910 mPostSmmPenAddress
= 0;
912 ReleaseToUnplugSelectors
:
913 gMmst
->MmFreePool (mToUnplugSelectors
);
914 mToUnplugSelectors
= NULL
;
916 ReleaseToUnplugApicIds
:
917 gMmst
->MmFreePool (mToUnplugApicIds
);
918 mToUnplugApicIds
= NULL
;
920 ReleasePluggedApicIds
:
921 gMmst
->MmFreePool (mPluggedApicIds
);
922 mPluggedApicIds
= NULL
;