VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/UefiCpuPkg/PiSmmCpuDxeSmm/CpuService.c@ 108793

Last change on this file since 108793 was 105670, checked in by vboxsync, 8 months ago

Devices/EFI/FirmwareNew: Merge edk2-stable-202405 and make it build on aarch64, bugref:4643

  • Property svn:eol-style set to native
File size: 13.4 KB
Line 
1/** @file
2Implementation of SMM CPU Services Protocol.
3
4Copyright (c) 2011 - 2023, Intel Corporation. All rights reserved.<BR>
5SPDX-License-Identifier: BSD-2-Clause-Patent
6
7**/
8
9#include "PiSmmCpuDxeSmm.h"
10
11//
12// SMM CPU Service Protocol instance
13//
14EFI_SMM_CPU_SERVICE_PROTOCOL mSmmCpuService = {
15 SmmGetProcessorInfo,
16 SmmSwitchBsp,
17 SmmAddProcessor,
18 SmmRemoveProcessor,
19 SmmWhoAmI,
20 SmmRegisterExceptionHandler
21};
22
23//
24// EDKII SMM CPU Rendezvous Service Protocol instance
25//
26EDKII_SMM_CPU_RENDEZVOUS_PROTOCOL mSmmCpuRendezvousService = {
27 SmmCpuRendezvous
28};
29
30/**
31 Gets processor information on the requested processor at the instant this call is made.
32
33 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
34 @param[in] ProcessorNumber The handle number of processor.
35 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
36 the requested processor is deposited.
37
38 @retval EFI_SUCCESS Processor information was returned.
39 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
40 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.
41 @retval EFI_NOT_FOUND The processor with the handle specified by
42 ProcessorNumber does not exist in the platform.
43
44**/
45EFI_STATUS
46EFIAPI
47SmmGetProcessorInfo (
48 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
49 IN UINTN ProcessorNumber,
50 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer
51 )
52{
53 //
54 // Check parameter
55 //
56 if ((ProcessorNumber >= mMaxNumberOfCpus) || (ProcessorInfoBuffer == NULL)) {
57 return EFI_INVALID_PARAMETER;
58 }
59
60 if (gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID) {
61 return EFI_NOT_FOUND;
62 }
63
64 //
65 // Fill in processor information
66 //
67 CopyMem (ProcessorInfoBuffer, &gSmmCpuPrivate->ProcessorInfo[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));
68 return EFI_SUCCESS;
69}
70
71/**
72 This service switches the requested AP to be the BSP since the next SMI.
73
74 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
75 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
76
77 @retval EFI_SUCCESS BSP will be switched in next SMI.
78 @retval EFI_UNSUPPORTED Switching the BSP or a processor to be hot-removed is not supported.
79 @retval EFI_NOT_FOUND The processor with the handle specified by ProcessorNumber does not exist.
80 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.
81**/
82EFI_STATUS
83EFIAPI
84SmmSwitchBsp (
85 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
86 IN UINTN ProcessorNumber
87 )
88{
89 //
90 // Check parameter
91 //
92 if (ProcessorNumber >= mMaxNumberOfCpus) {
93 return EFI_INVALID_PARAMETER;
94 }
95
96 if (gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID) {
97 return EFI_NOT_FOUND;
98 }
99
100 if ((gSmmCpuPrivate->Operation[ProcessorNumber] != SmmCpuNone) ||
101 (gSmst->CurrentlyExecutingCpu == ProcessorNumber))
102 {
103 return EFI_UNSUPPORTED;
104 }
105
106 //
107 // Setting of the BSP for next SMI is pending until all SMI handlers are finished
108 //
109 gSmmCpuPrivate->Operation[ProcessorNumber] = SmmCpuSwitchBsp;
110 return EFI_SUCCESS;
111}
112
113/**
114 Notify that a processor was hot-added.
115
116 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
117 @param[in] ProcessorId Local APIC ID of the hot-added processor.
118 @param[out] ProcessorNumber The handle number of the hot-added processor.
119
120 @retval EFI_SUCCESS The hot-addition of the specified processors was successfully notified.
121 @retval EFI_UNSUPPORTED Hot addition of processor is not supported.
122 @retval EFI_NOT_FOUND The processor with the handle specified by ProcessorNumber does not exist.
123 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.
124 @retval EFI_ALREADY_STARTED The processor is already online in the system.
125**/
126EFI_STATUS
127EFIAPI
128SmmAddProcessor (
129 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
130 IN UINT64 ProcessorId,
131 OUT UINTN *ProcessorNumber
132 )
133{
134 UINTN Index;
135
136 if (!FeaturePcdGet (PcdCpuHotPlugSupport)) {
137 return EFI_UNSUPPORTED;
138 }
139
140 //
141 // Check parameter
142 //
143 if ((ProcessorNumber == NULL) || (ProcessorId == INVALID_APIC_ID)) {
144 return EFI_INVALID_PARAMETER;
145 }
146
147 //
148 // Check if the processor already exists
149 //
150
151 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
152 if (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == ProcessorId) {
153 return EFI_ALREADY_STARTED;
154 }
155 }
156
157 //
158 // Check CPU hot plug data. The CPU RAS handler should have created the mapping
159 // of the APIC ID to SMBASE.
160 //
161 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
162 if ((mCpuHotPlugData.ApicId[Index] == ProcessorId) &&
163 (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == INVALID_APIC_ID))
164 {
165 gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId = ProcessorId;
166 gSmmCpuPrivate->ProcessorInfo[Index].StatusFlag = 0;
167 GetProcessorLocationByApicId (
168 (UINT32)ProcessorId,
169 &gSmmCpuPrivate->ProcessorInfo[Index].Location.Package,
170 &gSmmCpuPrivate->ProcessorInfo[Index].Location.Core,
171 &gSmmCpuPrivate->ProcessorInfo[Index].Location.Thread
172 );
173
174 GetProcessorLocation2ByApicId (
175 (UINT32)ProcessorId,
176 &gSmmCpuPrivate->ProcessorInfo[Index].ExtendedInformation.Location2.Package,
177 &gSmmCpuPrivate->ProcessorInfo[Index].ExtendedInformation.Location2.Die,
178 &gSmmCpuPrivate->ProcessorInfo[Index].ExtendedInformation.Location2.Tile,
179 &gSmmCpuPrivate->ProcessorInfo[Index].ExtendedInformation.Location2.Module,
180 &gSmmCpuPrivate->ProcessorInfo[Index].ExtendedInformation.Location2.Core,
181 &gSmmCpuPrivate->ProcessorInfo[Index].ExtendedInformation.Location2.Thread
182 );
183
184 *ProcessorNumber = Index;
185 gSmmCpuPrivate->Operation[Index] = SmmCpuAdd;
186 return EFI_SUCCESS;
187 }
188 }
189
190 return EFI_INVALID_PARAMETER;
191}
192
193/**
194 Notify that a processor was hot-removed.
195
196 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
197 @param[in] ProcessorNumber The handle number of the hot-added processor.
198
199 @retval EFI_SUCCESS The hot-removal of the specified processors was successfully notified.
200 @retval EFI_UNSUPPORTED Hot removal of processor is not supported.
201 @retval EFI_UNSUPPORTED Hot removal of BSP is not supported.
202 @retval EFI_UNSUPPORTED Hot removal of a processor with pending hot-plug operation is not supported.
203 @retval EFI_INVALID_PARAMETER ProcessorNumber is invalid.
204**/
205EFI_STATUS
206EFIAPI
207SmmRemoveProcessor (
208 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
209 IN UINTN ProcessorNumber
210 )
211{
212 if (!FeaturePcdGet (PcdCpuHotPlugSupport)) {
213 return EFI_UNSUPPORTED;
214 }
215
216 //
217 // Check parameter
218 //
219 if ((ProcessorNumber >= mMaxNumberOfCpus) ||
220 (gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId == INVALID_APIC_ID))
221 {
222 return EFI_INVALID_PARAMETER;
223 }
224
225 //
226 // Can't remove BSP
227 //
228 if (ProcessorNumber == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu) {
229 return EFI_UNSUPPORTED;
230 }
231
232 if (gSmmCpuPrivate->Operation[ProcessorNumber] != SmmCpuNone) {
233 return EFI_UNSUPPORTED;
234 }
235
236 gSmmCpuPrivate->ProcessorInfo[ProcessorNumber].ProcessorId = INVALID_APIC_ID;
237 mCpuHotPlugData.ApicId[ProcessorNumber] = INVALID_APIC_ID;
238
239 //
240 // Removal of the processor from the CPU list is pending until all SMI handlers are finished
241 //
242 gSmmCpuPrivate->Operation[ProcessorNumber] = SmmCpuRemove;
243 return EFI_SUCCESS;
244}
245
246/**
247 This return the handle number for the calling processor.
248
249 @param[in] This A pointer to the EFI_SMM_CPU_SERVICE_PROTOCOL instance.
250 @param[out] ProcessorNumber The handle number of currently executing processor.
251
252 @retval EFI_SUCCESS The current processor handle number was returned
253 in ProcessorNumber.
254 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
255
256**/
257EFI_STATUS
258EFIAPI
259SmmWhoAmI (
260 IN CONST EFI_SMM_CPU_SERVICE_PROTOCOL *This,
261 OUT UINTN *ProcessorNumber
262 )
263{
264 UINTN Index;
265 UINT64 ApicId;
266
267 //
268 // Check parameter
269 //
270 if (ProcessorNumber == NULL) {
271 return EFI_INVALID_PARAMETER;
272 }
273
274 ApicId = GetApicId ();
275
276 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
277 if (gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId == ApicId) {
278 *ProcessorNumber = Index;
279 return EFI_SUCCESS;
280 }
281 }
282
283 //
284 // This should not happen
285 //
286 ASSERT (FALSE);
287 return EFI_NOT_FOUND;
288}
289
290/**
291 Update the SMM CPU list per the pending operation.
292
293 This function is called after return from SMI handlers.
294**/
295VOID
296SmmCpuUpdate (
297 VOID
298 )
299{
300 UINTN Index;
301
302 //
303 // Handle pending BSP switch operations
304 //
305 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
306 if (gSmmCpuPrivate->Operation[Index] == SmmCpuSwitchBsp) {
307 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;
308 mSmmMpSyncData->SwitchBsp = TRUE;
309 mSmmMpSyncData->CandidateBsp[Index] = TRUE;
310 }
311 }
312
313 //
314 // Handle pending hot-add operations
315 //
316 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
317 if (gSmmCpuPrivate->Operation[Index] == SmmCpuAdd) {
318 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;
319 mNumberOfCpus++;
320 }
321 }
322
323 //
324 // Handle pending hot-remove operations
325 //
326 for (Index = 0; Index < mMaxNumberOfCpus; Index++) {
327 if (gSmmCpuPrivate->Operation[Index] == SmmCpuRemove) {
328 gSmmCpuPrivate->Operation[Index] = SmmCpuNone;
329 mNumberOfCpus--;
330 }
331 }
332}
333
334/**
335 Register exception handler.
336
337 @param This A pointer to the SMM_CPU_SERVICE_PROTOCOL instance.
338 @param ExceptionType Defines which interrupt or exception to hook. Type EFI_EXCEPTION_TYPE and
339 the valid values for this parameter are defined in EFI_DEBUG_SUPPORT_PROTOCOL
340 of the UEFI 2.0 specification.
341 @param InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER
342 that is called when a processor interrupt occurs.
343 If this parameter is NULL, then the handler will be uninstalled.
344
345 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
346 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was previously installed.
347 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not previously installed.
348 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported.
349
350**/
351EFI_STATUS
352EFIAPI
353SmmRegisterExceptionHandler (
354 IN EFI_SMM_CPU_SERVICE_PROTOCOL *This,
355 IN EFI_EXCEPTION_TYPE ExceptionType,
356 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
357 )
358{
359 return RegisterCpuInterruptHandler (ExceptionType, InterruptHandler);
360}
361
362/**
363 Initialize SMM CPU Services.
364
365 It installs EFI SMM CPU Services Protocol.
366
367 @param ImageHandle The firmware allocated handle for the EFI image.
368
369 @retval EFI_SUCCESS EFI SMM CPU Services Protocol was installed successfully.
370 @retval OTHER Fail to install Protocol.
371**/
372EFI_STATUS
373InitializeSmmCpuServices (
374 IN EFI_HANDLE Handle
375 )
376{
377 EFI_STATUS Status;
378
379 Status = gSmst->SmmInstallProtocolInterface (
380 &Handle,
381 &gEfiSmmCpuServiceProtocolGuid,
382 EFI_NATIVE_INTERFACE,
383 &mSmmCpuService
384 );
385 ASSERT_EFI_ERROR (Status);
386 if (EFI_ERROR (Status)) {
387 return Status;
388 }
389
390 Status = gSmst->SmmInstallProtocolInterface (
391 &Handle,
392 &gEdkiiSmmCpuRendezvousProtocolGuid,
393 EFI_NATIVE_INTERFACE,
394 &mSmmCpuRendezvousService
395 );
396 ASSERT_EFI_ERROR (Status);
397 return Status;
398}
399
400/**
401 Wait for all processors enterring SMM until all CPUs are already synchronized or not.
402
403 If BlockingMode is False, timeout value is zero.
404
405 @param This A pointer to the EDKII_SMM_CPU_RENDEZVOUS_PROTOCOL instance.
406 @param BlockingMode Blocking mode or non-blocking mode.
407
408 @retval EFI_SUCCESS All avaiable APs arrived.
409 @retval EFI_TIMEOUT Wait for all APs until timeout.
410
411**/
412EFI_STATUS
413EFIAPI
414SmmCpuRendezvous (
415 IN EDKII_SMM_CPU_RENDEZVOUS_PROTOCOL *This,
416 IN BOOLEAN BlockingMode
417 )
418{
419 EFI_STATUS Status;
420
421 //
422 // Return success immediately if all CPUs are already synchronized.
423 //
424 if (mSmmMpSyncData->AllApArrivedWithException) {
425 Status = EFI_SUCCESS;
426 goto ON_EXIT;
427 }
428
429 if (!BlockingMode) {
430 Status = EFI_TIMEOUT;
431 goto ON_EXIT;
432 }
433
434 if ((mSmmMpSyncData->EffectiveSyncMode != SmmCpuSyncModeTradition) && !SmmCpuFeaturesNeedConfigureMtrrs ()) {
435 //
436 // There are some APs outside SMM, Wait for all avaiable APs to arrive.
437 //
438 SmmWaitForApArrival ();
439 Status = mSmmMpSyncData->AllApArrivedWithException ? EFI_SUCCESS : EFI_TIMEOUT;
440 } else {
441 //
442 // BSP has already waitted for APs to arrive SMM if SmmCpuSyncMode selected or need config MTRR.
443 //
444 Status = EFI_TIMEOUT;
445 }
446
447ON_EXIT:
448 if (!mSmmMpSyncData->AllApArrivedWithException) {
449 DEBUG ((DEBUG_INFO, "EdkiiSmmWaitForAllApArrival: Timeout to wait all APs arrival\n"));
450 }
451
452 return Status;
453}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette