]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/CpuHotplugSmm/CpuHotplug.c
OvmfPkg/CpuHotplugSmm: complete root MMI handler for CPU hotplug
[mirror_edk2.git] / OvmfPkg / CpuHotplugSmm / CpuHotplug.c
1 /** @file
2 Root SMI handler for VCPU hotplug SMIs.
3
4 Copyright (c) 2020, Red Hat, Inc.
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 **/
8
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
20
21 #include "ApicId.h" // APIC_ID
22 #include "QemuCpuhp.h" // QemuCpuhpWriteCpuSelector()
23 #include "Smbase.h" // SmbaseAllocatePostSmmPen()
24
25 //
26 // We use this protocol for accessing IO Ports.
27 //
28 STATIC EFI_MM_CPU_IO_PROTOCOL *mMmCpuIo;
29 //
30 // The following protocol is used to report the addition or removal of a CPU to
31 // the SMM CPU driver (PiSmmCpuDxeSmm).
32 //
33 STATIC EFI_SMM_CPU_SERVICE_PROTOCOL *mMmCpuService;
34 //
35 // This structure is a communication side-channel between the
36 // EFI_SMM_CPU_SERVICE_PROTOCOL consumer (i.e., this driver) and provider
37 // (i.e., PiSmmCpuDxeSmm).
38 //
39 STATIC CPU_HOT_PLUG_DATA *mCpuHotPlugData;
40 //
41 // SMRAM arrays for fetching the APIC IDs of processors with pending events (of
42 // known event types), for the time of just one MMI.
43 //
44 // The lifetimes of these arrays match that of this driver only because we
45 // don't want to allocate SMRAM at OS runtime, and potentially fail (or
46 // fragment the SMRAM map).
47 //
48 // These arrays provide room for ("possible CPU count" minus one) APIC IDs
49 // each, as we don't expect every possible CPU to appear, or disappear, in a
50 // single MMI. The numbers of used (populated) elements in the arrays are
51 // determined on every MMI separately.
52 //
53 STATIC APIC_ID *mPluggedApicIds;
54 STATIC APIC_ID *mToUnplugApicIds;
55 //
56 // Address of the non-SMRAM reserved memory page that contains the Post-SMM Pen
57 // for hot-added CPUs.
58 //
59 STATIC UINT32 mPostSmmPenAddress;
60 //
61 // Represents the registration of the CPU Hotplug MMI handler.
62 //
63 STATIC EFI_HANDLE mDispatchHandle;
64
65
66 /**
67 CPU Hotplug MMI handler function.
68
69 This is a root MMI handler.
70
71 @param[in] DispatchHandle The unique handle assigned to this handler by
72 EFI_MM_SYSTEM_TABLE.MmiHandlerRegister().
73
74 @param[in] Context Context passed in by
75 EFI_MM_SYSTEM_TABLE.MmiManage(). Due to
76 CpuHotplugMmi() being a root MMI handler,
77 Context is ASSERT()ed to be NULL.
78
79 @param[in,out] CommBuffer Ignored, due to CpuHotplugMmi() being a root
80 MMI handler.
81
82 @param[in,out] CommBufferSize Ignored, due to CpuHotplugMmi() being a root
83 MMI handler.
84
85 @retval EFI_SUCCESS The MMI was handled and the MMI
86 source was quiesced. When returned
87 by a non-root MMI handler,
88 EFI_SUCCESS terminates the
89 processing of MMI handlers in
90 EFI_MM_SYSTEM_TABLE.MmiManage().
91 For a root MMI handler (i.e., for
92 the present function too),
93 EFI_SUCCESS behaves identically to
94 EFI_WARN_INTERRUPT_SOURCE_QUIESCED,
95 as further root MMI handlers are
96 going to be called by
97 EFI_MM_SYSTEM_TABLE.MmiManage()
98 anyway.
99
100 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The MMI source has been quiesced,
101 but other handlers should still
102 be called.
103
104 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The MMI source is still pending,
105 and other handlers should still
106 be called.
107
108 @retval EFI_INTERRUPT_PENDING The MMI source could not be
109 quiesced.
110 **/
111 STATIC
112 EFI_STATUS
113 EFIAPI
114 CpuHotplugMmi (
115 IN EFI_HANDLE DispatchHandle,
116 IN CONST VOID *Context OPTIONAL,
117 IN OUT VOID *CommBuffer OPTIONAL,
118 IN OUT UINTN *CommBufferSize OPTIONAL
119 )
120 {
121 EFI_STATUS Status;
122 UINT8 ApmControl;
123 UINT32 PluggedCount;
124 UINT32 ToUnplugCount;
125 UINT32 PluggedIdx;
126 UINT32 NewSlot;
127
128 //
129 // Assert that we are entering this function due to our root MMI handler
130 // registration.
131 //
132 ASSERT (DispatchHandle == mDispatchHandle);
133 //
134 // When MmiManage() is invoked to process root MMI handlers, the caller (the
135 // MM Core) is expected to pass in a NULL Context. MmiManage() then passes
136 // the same NULL Context to individual handlers.
137 //
138 ASSERT (Context == NULL);
139 //
140 // Read the MMI command value from the APM Control Port, to see if this is an
141 // MMI we should care about.
142 //
143 Status = mMmCpuIo->Io.Read (mMmCpuIo, MM_IO_UINT8, ICH9_APM_CNT, 1,
144 &ApmControl);
145 if (EFI_ERROR (Status)) {
146 DEBUG ((DEBUG_ERROR, "%a: failed to read ICH9_APM_CNT: %r\n", __FUNCTION__,
147 Status));
148 //
149 // We couldn't even determine if the MMI was for us or not.
150 //
151 goto Fatal;
152 }
153
154 if (ApmControl != ICH9_APM_CNT_CPU_HOTPLUG) {
155 //
156 // The MMI is not for us.
157 //
158 return EFI_WARN_INTERRUPT_SOURCE_QUIESCED;
159 }
160
161 //
162 // Collect the CPUs with pending events.
163 //
164 Status = QemuCpuhpCollectApicIds (
165 mMmCpuIo,
166 mCpuHotPlugData->ArrayLength, // PossibleCpuCount
167 mCpuHotPlugData->ArrayLength - 1, // ApicIdCount
168 mPluggedApicIds,
169 &PluggedCount,
170 mToUnplugApicIds,
171 &ToUnplugCount
172 );
173 if (EFI_ERROR (Status)) {
174 goto Fatal;
175 }
176 if (ToUnplugCount > 0) {
177 DEBUG ((DEBUG_ERROR, "%a: hot-unplug is not supported yet\n",
178 __FUNCTION__));
179 goto Fatal;
180 }
181
182 //
183 // Process hot-added CPUs.
184 //
185 // The Post-SMM Pen need not be reinstalled multiple times within a single
186 // root MMI handling. Even reinstalling once per root MMI is only prudence;
187 // in theory installing the pen in the driver's entry point function should
188 // suffice.
189 //
190 SmbaseReinstallPostSmmPen (mPostSmmPenAddress);
191
192 PluggedIdx = 0;
193 NewSlot = 0;
194 while (PluggedIdx < PluggedCount) {
195 APIC_ID NewApicId;
196 UINTN NewProcessorNumberByProtocol;
197
198 NewApicId = mPluggedApicIds[PluggedIdx];
199 //
200 // Find the first empty slot in CPU_HOT_PLUG_DATA.
201 //
202 while (NewSlot < mCpuHotPlugData->ArrayLength &&
203 mCpuHotPlugData->ApicId[NewSlot] != MAX_UINT64) {
204 NewSlot++;
205 }
206 if (NewSlot == mCpuHotPlugData->ArrayLength) {
207 DEBUG ((DEBUG_ERROR, "%a: no room for APIC ID " FMT_APIC_ID "\n",
208 __FUNCTION__, NewApicId));
209 goto Fatal;
210 }
211
212 //
213 // Store the APIC ID of the new processor to the slot.
214 //
215 mCpuHotPlugData->ApicId[NewSlot] = NewApicId;
216
217 //
218 // Relocate the SMBASE of the new CPU.
219 //
220 Status = SmbaseRelocate (NewApicId, mCpuHotPlugData->SmBase[NewSlot],
221 mPostSmmPenAddress);
222 if (EFI_ERROR (Status)) {
223 goto RevokeNewSlot;
224 }
225
226 //
227 // Add the new CPU with EFI_SMM_CPU_SERVICE_PROTOCOL.
228 //
229 Status = mMmCpuService->AddProcessor (mMmCpuService, NewApicId,
230 &NewProcessorNumberByProtocol);
231 if (EFI_ERROR (Status)) {
232 DEBUG ((DEBUG_ERROR, "%a: AddProcessor(" FMT_APIC_ID "): %r\n",
233 __FUNCTION__, NewApicId, Status));
234 goto RevokeNewSlot;
235 }
236
237 DEBUG ((DEBUG_INFO, "%a: hot-added APIC ID " FMT_APIC_ID ", SMBASE 0x%Lx, "
238 "EFI_SMM_CPU_SERVICE_PROTOCOL assigned number %Lu\n", __FUNCTION__,
239 NewApicId, (UINT64)mCpuHotPlugData->SmBase[NewSlot],
240 (UINT64)NewProcessorNumberByProtocol));
241
242 NewSlot++;
243 PluggedIdx++;
244 }
245
246 //
247 // We've handled this MMI.
248 //
249 return EFI_SUCCESS;
250
251 RevokeNewSlot:
252 mCpuHotPlugData->ApicId[NewSlot] = MAX_UINT64;
253
254 Fatal:
255 ASSERT (FALSE);
256 CpuDeadLoop ();
257 //
258 // We couldn't handle this MMI.
259 //
260 return EFI_INTERRUPT_PENDING;
261 }
262
263
264 //
265 // Entry point function of this driver.
266 //
267 EFI_STATUS
268 EFIAPI
269 CpuHotplugEntry (
270 IN EFI_HANDLE ImageHandle,
271 IN EFI_SYSTEM_TABLE *SystemTable
272 )
273 {
274 EFI_STATUS Status;
275 UINTN Size;
276
277 //
278 // This module should only be included when SMM support is required.
279 //
280 ASSERT (FeaturePcdGet (PcdSmmSmramRequire));
281 //
282 // This driver depends on the dynamically detected "SMRAM at default SMBASE"
283 // feature.
284 //
285 if (!PcdGetBool (PcdQ35SmramAtDefaultSmbase)) {
286 return EFI_UNSUPPORTED;
287 }
288
289 //
290 // Errors from here on are fatal; we cannot allow the boot to proceed if we
291 // can't set up this driver to handle CPU hotplug.
292 //
293 // First, collect the protocols needed later. All of these protocols are
294 // listed in our module DEPEX.
295 //
296 Status = gMmst->MmLocateProtocol (&gEfiMmCpuIoProtocolGuid,
297 NULL /* Registration */, (VOID **)&mMmCpuIo);
298 if (EFI_ERROR (Status)) {
299 DEBUG ((DEBUG_ERROR, "%a: locate MmCpuIo: %r\n", __FUNCTION__, Status));
300 goto Fatal;
301 }
302 Status = gMmst->MmLocateProtocol (&gEfiSmmCpuServiceProtocolGuid,
303 NULL /* Registration */, (VOID **)&mMmCpuService);
304 if (EFI_ERROR (Status)) {
305 DEBUG ((DEBUG_ERROR, "%a: locate MmCpuService: %r\n", __FUNCTION__,
306 Status));
307 goto Fatal;
308 }
309
310 //
311 // Our DEPEX on EFI_SMM_CPU_SERVICE_PROTOCOL guarantees that PiSmmCpuDxeSmm
312 // has pointed PcdCpuHotPlugDataAddress to CPU_HOT_PLUG_DATA in SMRAM.
313 //
314 mCpuHotPlugData = (VOID *)(UINTN)PcdGet64 (PcdCpuHotPlugDataAddress);
315 if (mCpuHotPlugData == NULL) {
316 Status = EFI_NOT_FOUND;
317 DEBUG ((DEBUG_ERROR, "%a: CPU_HOT_PLUG_DATA: %r\n", __FUNCTION__, Status));
318 goto Fatal;
319 }
320 //
321 // If the possible CPU count is 1, there's nothing for this driver to do.
322 //
323 if (mCpuHotPlugData->ArrayLength == 1) {
324 return EFI_UNSUPPORTED;
325 }
326 //
327 // Allocate the data structures that depend on the possible CPU count.
328 //
329 if (RETURN_ERROR (SafeUintnSub (mCpuHotPlugData->ArrayLength, 1, &Size)) ||
330 RETURN_ERROR (SafeUintnMult (sizeof (APIC_ID), Size, &Size))) {
331 Status = EFI_ABORTED;
332 DEBUG ((DEBUG_ERROR, "%a: invalid CPU_HOT_PLUG_DATA\n", __FUNCTION__));
333 goto Fatal;
334 }
335 Status = gMmst->MmAllocatePool (EfiRuntimeServicesData, Size,
336 (VOID **)&mPluggedApicIds);
337 if (EFI_ERROR (Status)) {
338 DEBUG ((DEBUG_ERROR, "%a: MmAllocatePool(): %r\n", __FUNCTION__, Status));
339 goto Fatal;
340 }
341 Status = gMmst->MmAllocatePool (EfiRuntimeServicesData, Size,
342 (VOID **)&mToUnplugApicIds);
343 if (EFI_ERROR (Status)) {
344 DEBUG ((DEBUG_ERROR, "%a: MmAllocatePool(): %r\n", __FUNCTION__, Status));
345 goto ReleasePluggedApicIds;
346 }
347
348 //
349 // Allocate the Post-SMM Pen for hot-added CPUs.
350 //
351 Status = SmbaseAllocatePostSmmPen (&mPostSmmPenAddress,
352 SystemTable->BootServices);
353 if (EFI_ERROR (Status)) {
354 goto ReleaseToUnplugApicIds;
355 }
356
357 //
358 // Sanity-check the CPU hotplug interface.
359 //
360 // Both of the following features are part of QEMU 5.0, introduced primarily
361 // in commit range 3e08b2b9cb64..3a61c8db9d25:
362 //
363 // (a) the QEMU_CPUHP_CMD_GET_ARCH_ID command of the modern CPU hotplug
364 // interface,
365 //
366 // (b) the "SMRAM at default SMBASE" feature.
367 //
368 // From these, (b) is restricted to 5.0+ machine type versions, while (a)
369 // does not depend on machine type version. Because we ensured the stricter
370 // condition (b) through PcdQ35SmramAtDefaultSmbase above, the (a)
371 // QEMU_CPUHP_CMD_GET_ARCH_ID command must now be available too. While we
372 // can't verify the presence of precisely that command, we can still verify
373 // (sanity-check) that the modern interface is active, at least.
374 //
375 // Consult the "Typical usecases | Detecting and enabling modern CPU hotplug
376 // interface" section in QEMU's "docs/specs/acpi_cpu_hotplug.txt", on the
377 // following.
378 //
379 QemuCpuhpWriteCpuSelector (mMmCpuIo, 0);
380 QemuCpuhpWriteCpuSelector (mMmCpuIo, 0);
381 QemuCpuhpWriteCommand (mMmCpuIo, QEMU_CPUHP_CMD_GET_PENDING);
382 if (QemuCpuhpReadCommandData2 (mMmCpuIo) != 0) {
383 Status = EFI_NOT_FOUND;
384 DEBUG ((DEBUG_ERROR, "%a: modern CPU hotplug interface: %r\n",
385 __FUNCTION__, Status));
386 goto ReleasePostSmmPen;
387 }
388
389 //
390 // Register the handler for the CPU Hotplug MMI.
391 //
392 Status = gMmst->MmiHandlerRegister (
393 CpuHotplugMmi,
394 NULL, // HandlerType: root MMI handler
395 &mDispatchHandle
396 );
397 if (EFI_ERROR (Status)) {
398 DEBUG ((DEBUG_ERROR, "%a: MmiHandlerRegister(): %r\n", __FUNCTION__,
399 Status));
400 goto ReleasePostSmmPen;
401 }
402
403 //
404 // Install the handler for the hot-added CPUs' first SMI.
405 //
406 SmbaseInstallFirstSmiHandler ();
407
408 return EFI_SUCCESS;
409
410 ReleasePostSmmPen:
411 SmbaseReleasePostSmmPen (mPostSmmPenAddress, SystemTable->BootServices);
412 mPostSmmPenAddress = 0;
413
414 ReleaseToUnplugApicIds:
415 gMmst->MmFreePool (mToUnplugApicIds);
416 mToUnplugApicIds = NULL;
417
418 ReleasePluggedApicIds:
419 gMmst->MmFreePool (mPluggedApicIds);
420 mPluggedApicIds = NULL;
421
422 Fatal:
423 ASSERT (FALSE);
424 CpuDeadLoop ();
425 return Status;
426 }