2 CPU PEI Module installs CPU Multiple Processor PPI.
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 // CPU MP PPI to be installed
14 EFI_PEI_MP_SERVICES_PPI mMpServicesPpi
= {
15 PeiGetNumberOfProcessors
,
24 EFI_PEI_PPI_DESCRIPTOR mPeiCpuMpPpiDesc
= {
25 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
26 &gEfiPeiMpServicesPpiGuid
,
31 This service retrieves the number of logical processor in the platform
32 and the number of those logical processors that are enabled on this boot.
33 This service may only be called from the BSP.
35 This function is used to retrieve the following information:
36 - The number of logical processors that are present in the system.
37 - The number of enabled logical processors in the system at the instant
40 Because MP Service Ppi provides services to enable and disable processors
41 dynamically, the number of enabled logical processors may vary during the
42 course of a boot session.
44 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.
45 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then
46 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors
47 is returned in NumberOfProcessors, the number of currently enabled processor
48 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.
50 @param[in] PeiServices An indirect pointer to the PEI Services Table
51 published by the PEI Foundation.
52 @param[in] This Pointer to this instance of the PPI.
53 @param[out] NumberOfProcessors Pointer to the total number of logical processors in
54 the system, including the BSP and disabled APs.
55 @param[out] NumberOfEnabledProcessors
56 Number of processors in the system that are enabled.
58 @retval EFI_SUCCESS The number of logical processors and enabled
59 logical processors was retrieved.
60 @retval EFI_DEVICE_ERROR The calling processor is an AP.
61 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.
62 NumberOfEnabledProcessors is NULL.
66 PeiGetNumberOfProcessors (
67 IN CONST EFI_PEI_SERVICES
**PeiServices
,
68 IN EFI_PEI_MP_SERVICES_PPI
*This
,
69 OUT UINTN
*NumberOfProcessors
,
70 OUT UINTN
*NumberOfEnabledProcessors
73 if ((NumberOfProcessors
== NULL
) || (NumberOfEnabledProcessors
== NULL
)) {
74 return EFI_INVALID_PARAMETER
;
77 return MpInitLibGetNumberOfProcessors (
79 NumberOfEnabledProcessors
84 Gets detailed MP-related information on the requested processor at the
85 instant this call is made. This service may only be called from the BSP.
87 This service retrieves detailed MP-related information about any processor
88 on the platform. Note the following:
89 - The processor information may change during the course of a boot session.
90 - The information presented here is entirely MP related.
92 Information regarding the number of caches and their sizes, frequency of operation,
93 slot numbers is all considered platform-related information and is not provided
96 @param[in] PeiServices An indirect pointer to the PEI Services Table
97 published by the PEI Foundation.
98 @param[in] This Pointer to this instance of the PPI.
99 @param[in] ProcessorNumber Pointer to the total number of logical processors in
100 the system, including the BSP and disabled APs.
101 @param[out] ProcessorInfoBuffer Number of processors in the system that are enabled.
103 @retval EFI_SUCCESS Processor information was returned.
104 @retval EFI_DEVICE_ERROR The calling processor is an AP.
105 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
106 @retval EFI_NOT_FOUND The processor with the handle specified by
107 ProcessorNumber does not exist in the platform.
111 PeiGetProcessorInfo (
112 IN CONST EFI_PEI_SERVICES
**PeiServices
,
113 IN EFI_PEI_MP_SERVICES_PPI
*This
,
114 IN UINTN ProcessorNumber
,
115 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
118 return MpInitLibGetProcessorInfo (ProcessorNumber
, ProcessorInfoBuffer
, NULL
);
122 This service executes a caller provided function on all enabled APs. APs can
123 run either simultaneously or one at a time in sequence. This service supports
124 both blocking requests only. This service may only
125 be called from the BSP.
127 This function is used to dispatch all the enabled APs to the function specified
128 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned
129 immediately and Procedure is not started on any AP.
131 If SingleThread is TRUE, all the enabled APs execute the function specified by
132 Procedure one by one, in ascending order of processor handle number. Otherwise,
133 all the enabled APs execute the function specified by Procedure simultaneously.
135 If the timeout specified by TimeoutInMicroSeconds expires before all APs return
136 from Procedure, then Procedure on the failed APs is terminated. All enabled APs
137 are always available for further calls to EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()
138 and EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If FailedCpuList is not NULL, its
139 content points to the list of processor handle numbers in which Procedure was
142 Note: It is the responsibility of the consumer of the EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()
143 to make sure that the nature of the code that is executed on the BSP and the
144 dispatched APs is well controlled. The MP Services Ppi does not guarantee
145 that the Procedure function is MP-safe. Hence, the tasks that can be run in
146 parallel are limited to certain independent tasks and well-controlled exclusive
147 code. PEI services and Ppis may not be called by APs unless otherwise
150 In blocking execution mode, BSP waits until all APs finish or
151 TimeoutInMicroSeconds expires.
153 @param[in] PeiServices An indirect pointer to the PEI Services Table
154 published by the PEI Foundation.
155 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
156 @param[in] Procedure A pointer to the function to be run on enabled APs of
158 @param[in] SingleThread If TRUE, then all the enabled APs execute the function
159 specified by Procedure one by one, in ascending order
160 of processor handle number. If FALSE, then all the
161 enabled APs execute the function specified by Procedure
163 @param[in] TimeoutInMicroSeconds
164 Indicates the time limit in microseconds for APs to
165 return from Procedure, for blocking mode only. Zero
166 means infinity. If the timeout expires before all APs
167 return from Procedure, then Procedure on the failed APs
168 is terminated. All enabled APs are available for next
169 function assigned by EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()
170 or EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If the
171 timeout expires in blocking mode, BSP returns
173 @param[in] ProcedureArgument The parameter passed into Procedure for all APs.
175 @retval EFI_SUCCESS In blocking mode, all APs have finished before the
177 @retval EFI_DEVICE_ERROR Caller processor is AP.
178 @retval EFI_NOT_STARTED No enabled APs exist in the system.
179 @retval EFI_NOT_READY Any enabled APs are busy.
180 @retval EFI_TIMEOUT In blocking mode, the timeout expired before all
181 enabled APs have finished.
182 @retval EFI_INVALID_PARAMETER Procedure is NULL.
187 IN CONST EFI_PEI_SERVICES
**PeiServices
,
188 IN EFI_PEI_MP_SERVICES_PPI
*This
,
189 IN EFI_AP_PROCEDURE Procedure
,
190 IN BOOLEAN SingleThread
,
191 IN UINTN TimeoutInMicroSeconds
,
192 IN VOID
*ProcedureArgument OPTIONAL
195 return MpInitLibStartupAllAPs (
199 TimeoutInMicroSeconds
,
206 This service lets the caller get one enabled AP to execute a caller-provided
207 function. The caller can request the BSP to wait for the completion
208 of the AP. This service may only be called from the BSP.
210 This function is used to dispatch one enabled AP to the function specified by
211 Procedure passing in the argument specified by ProcedureArgument.
212 The execution is in blocking mode. The BSP waits until the AP finishes or
213 TimeoutInMicroSecondss expires.
215 If the timeout specified by TimeoutInMicroseconds expires before the AP returns
216 from Procedure, then execution of Procedure by the AP is terminated. The AP is
217 available for subsequent calls to EFI_PEI_MP_SERVICES_PPI.StartupAllAPs() and
218 EFI_PEI_MP_SERVICES_PPI.StartupThisAP().
220 @param[in] PeiServices An indirect pointer to the PEI Services Table
221 published by the PEI Foundation.
222 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
223 @param[in] Procedure A pointer to the function to be run on enabled APs of
225 @param[in] ProcessorNumber The handle number of the AP. The range is from 0 to the
226 total number of logical processors minus 1. The total
227 number of logical processors can be retrieved by
228 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().
229 @param[in] TimeoutInMicroseconds
230 Indicates the time limit in microseconds for APs to
231 return from Procedure, for blocking mode only. Zero
232 means infinity. If the timeout expires before all APs
233 return from Procedure, then Procedure on the failed APs
234 is terminated. All enabled APs are available for next
235 function assigned by EFI_PEI_MP_SERVICES_PPI.StartupAllAPs()
236 or EFI_PEI_MP_SERVICES_PPI.StartupThisAP(). If the
237 timeout expires in blocking mode, BSP returns
239 @param[in] ProcedureArgument The parameter passed into Procedure for all APs.
241 @retval EFI_SUCCESS In blocking mode, specified AP finished before the
243 @retval EFI_DEVICE_ERROR The calling processor is an AP.
244 @retval EFI_TIMEOUT In blocking mode, the timeout expired before the
245 specified AP has finished.
246 @retval EFI_NOT_FOUND The processor with the handle specified by
247 ProcessorNumber does not exist.
248 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
249 @retval EFI_INVALID_PARAMETER Procedure is NULL.
254 IN CONST EFI_PEI_SERVICES
**PeiServices
,
255 IN EFI_PEI_MP_SERVICES_PPI
*This
,
256 IN EFI_AP_PROCEDURE Procedure
,
257 IN UINTN ProcessorNumber
,
258 IN UINTN TimeoutInMicroseconds
,
259 IN VOID
*ProcedureArgument OPTIONAL
262 return MpInitLibStartupThisAP (
266 TimeoutInMicroseconds
,
273 This service switches the requested AP to be the BSP from that point onward.
274 This service changes the BSP for all purposes. This call can only be performed
277 This service switches the requested AP to be the BSP from that point onward.
278 This service changes the BSP for all purposes. The new BSP can take over the
279 execution of the old BSP and continue seamlessly from where the old one left
282 If the BSP cannot be switched prior to the return from this service, then
283 EFI_UNSUPPORTED must be returned.
285 @param[in] PeiServices An indirect pointer to the PEI Services Table
286 published by the PEI Foundation.
287 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
288 @param[in] ProcessorNumber The handle number of the AP. The range is from 0 to the
289 total number of logical processors minus 1. The total
290 number of logical processors can be retrieved by
291 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().
292 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an enabled
293 AP. Otherwise, it will be disabled.
295 @retval EFI_SUCCESS BSP successfully switched.
296 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to this
298 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
299 @retval EFI_DEVICE_ERROR The calling processor is an AP.
300 @retval EFI_NOT_FOUND The processor with the handle specified by
301 ProcessorNumber does not exist.
302 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or a disabled
304 @retval EFI_NOT_READY The specified AP is busy.
309 IN CONST EFI_PEI_SERVICES
**PeiServices
,
310 IN EFI_PEI_MP_SERVICES_PPI
*This
,
311 IN UINTN ProcessorNumber
,
312 IN BOOLEAN EnableOldBSP
315 return MpInitLibSwitchBSP (ProcessorNumber
, EnableOldBSP
);
319 This service lets the caller enable or disable an AP from this point onward.
320 This service may only be called from the BSP.
322 This service allows the caller enable or disable an AP from this point onward.
323 The caller can optionally specify the health status of the AP by Health. If
324 an AP is being disabled, then the state of the disabled AP is implementation
325 dependent. If an AP is enabled, then the implementation must guarantee that a
326 complete initialization sequence is performed on the AP, so the AP is in a state
327 that is compatible with an MP operating system.
329 If the enable or disable AP operation cannot be completed prior to the return
330 from this service, then EFI_UNSUPPORTED must be returned.
332 @param[in] PeiServices An indirect pointer to the PEI Services Table
333 published by the PEI Foundation.
334 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
335 @param[in] ProcessorNumber The handle number of the AP. The range is from 0 to the
336 total number of logical processors minus 1. The total
337 number of logical processors can be retrieved by
338 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().
339 @param[in] EnableAP Specifies the new state for the processor for enabled,
341 @param[in] HealthFlag If not NULL, a pointer to a value that specifies the
342 new health status of the AP. This flag corresponds to
343 StatusFlag defined in EFI_PEI_MP_SERVICES_PPI.GetProcessorInfo().
344 Only the PROCESSOR_HEALTH_STATUS_BIT is used. All other
345 bits are ignored. If it is NULL, this parameter is
348 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
349 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed prior
350 to this service returning.
351 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
352 @retval EFI_DEVICE_ERROR The calling processor is an AP.
353 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
355 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
360 IN CONST EFI_PEI_SERVICES
**PeiServices
,
361 IN EFI_PEI_MP_SERVICES_PPI
*This
,
362 IN UINTN ProcessorNumber
,
364 IN UINT32
*HealthFlag OPTIONAL
367 return MpInitLibEnableDisableAP (ProcessorNumber
, EnableAP
, HealthFlag
);
371 This return the handle number for the calling processor. This service may be
372 called from the BSP and APs.
374 This service returns the processor handle number for the calling processor.
375 The returned value is in the range from 0 to the total number of logical
376 processors minus 1. The total number of logical processors can be retrieved
377 with EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors(). This service may be
378 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER
379 is returned. Otherwise, the current processors handle number is returned in
380 ProcessorNumber, and EFI_SUCCESS is returned.
382 @param[in] PeiServices An indirect pointer to the PEI Services Table
383 published by the PEI Foundation.
384 @param[in] This A pointer to the EFI_PEI_MP_SERVICES_PPI instance.
385 @param[out] ProcessorNumber The handle number of the AP. The range is from 0 to the
386 total number of logical processors minus 1. The total
387 number of logical processors can be retrieved by
388 EFI_PEI_MP_SERVICES_PPI.GetNumberOfProcessors().
390 @retval EFI_SUCCESS The current processor handle number was returned in
392 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
397 IN CONST EFI_PEI_SERVICES
**PeiServices
,
398 IN EFI_PEI_MP_SERVICES_PPI
*This
,
399 OUT UINTN
*ProcessorNumber
402 return MpInitLibWhoAmI (ProcessorNumber
);
406 Get GDT register value.
408 This function is mainly for AP purpose because AP may have different GDT
411 @param[in,out] Buffer The pointer to private data buffer.
420 AsmReadGdtr ((IA32_DESCRIPTOR
*)Buffer
);
424 Initializes CPU exceptions handlers for the sake of stack switch requirement.
426 This function is a wrapper of InitializeCpuExceptionHandlersEx. It's mainly
427 for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.
429 @param[in,out] Buffer The pointer to private data buffer.
434 InitializeExceptionStackSwitchHandlers (
438 CPU_EXCEPTION_INIT_DATA
*EssData
;
439 IA32_DESCRIPTOR Idtr
;
444 // We don't plan to replace IDT table with a new one, but we should not assume
445 // the AP's IDT is the same as BSP's IDT either.
448 EssData
->Ia32
.IdtTable
= (VOID
*)Idtr
.Base
;
449 EssData
->Ia32
.IdtTableSize
= Idtr
.Limit
+ 1;
450 Status
= InitializeCpuExceptionHandlersEx (NULL
, EssData
);
451 ASSERT_EFI_ERROR (Status
);
455 Initializes MP exceptions handlers for the sake of stack switch requirement.
457 This function will allocate required resources required to setup stack switch
458 and pass them through CPU_EXCEPTION_INIT_DATA to each logic processor.
462 InitializeMpExceptionStackSwitchHandlers (
469 UINTN ExceptionNumber
;
473 IA32_DESCRIPTOR Gdtr
;
474 CPU_EXCEPTION_INIT_DATA EssData
;
477 UINTN NumberOfProcessors
;
479 if (!PcdGetBool (PcdCpuStackGuard
)) {
483 MpInitLibGetNumberOfProcessors(&NumberOfProcessors
, NULL
);
484 MpInitLibWhoAmI (&Bsp
);
486 ExceptionNumber
= FixedPcdGetSize (PcdCpuStackSwitchExceptionList
);
487 NewStackSize
= FixedPcdGet32 (PcdCpuKnownGoodStackSize
) * ExceptionNumber
;
489 Status
= PeiServicesAllocatePool (
490 NewStackSize
* NumberOfProcessors
,
493 ASSERT(StackTop
!= NULL
);
494 if (EFI_ERROR (Status
)) {
495 ASSERT_EFI_ERROR (Status
);
498 StackTop
+= NewStackSize
* NumberOfProcessors
;
501 // The default exception handlers must have been initialized. Let's just skip
502 // it in this method.
504 EssData
.Ia32
.Revision
= CPU_EXCEPTION_INIT_DATA_REV
;
505 EssData
.Ia32
.InitDefaultHandlers
= FALSE
;
507 EssData
.Ia32
.StackSwitchExceptions
= FixedPcdGetPtr(PcdCpuStackSwitchExceptionList
);
508 EssData
.Ia32
.StackSwitchExceptionNumber
= ExceptionNumber
;
509 EssData
.Ia32
.KnownGoodStackSize
= FixedPcdGet32(PcdCpuKnownGoodStackSize
);
512 // Initialize Gdtr to suppress incorrect compiler/analyzer warnings.
516 for (Index
= 0; Index
< NumberOfProcessors
; ++Index
) {
518 // To support stack switch, we need to re-construct GDT but not IDT.
524 // AP might have different size of GDT from BSP.
526 MpInitLibStartupThisAP (GetGdtr
, Index
, NULL
, 0, (VOID
*)&Gdtr
, NULL
);
530 // X64 needs only one TSS of current task working for all exceptions
531 // because of its IST feature. IA32 needs one TSS for each exception
532 // in addition to current task. Since AP is not supposed to allocate
533 // memory, we have to do it in BSP. To simplify the code, we allocate
534 // memory for IA32 case to cover both IA32 and X64 exception stack
537 // Layout of memory to allocate for each processor:
538 // --------------------------------
539 // | Alignment | (just in case)
540 // --------------------------------
544 // --------------------------------
545 // | Current task descriptor |
546 // --------------------------------
548 // | Exception task descriptors | X ExceptionNumber
550 // --------------------------------
551 // | Current task-state segment |
552 // --------------------------------
554 // | Exception task-state segment | X ExceptionNumber
556 // --------------------------------
558 OldGdtSize
= Gdtr
.Limit
+ 1;
559 EssData
.Ia32
.ExceptionTssDescSize
= sizeof (IA32_TSS_DESCRIPTOR
) *
560 (ExceptionNumber
+ 1);
561 EssData
.Ia32
.ExceptionTssSize
= sizeof (IA32_TASK_STATE_SEGMENT
) *
562 (ExceptionNumber
+ 1);
563 NewGdtSize
= sizeof (IA32_TSS_DESCRIPTOR
) +
565 EssData
.Ia32
.ExceptionTssDescSize
+
566 EssData
.Ia32
.ExceptionTssSize
;
568 Status
= PeiServicesAllocatePool (
572 ASSERT (GdtBuffer
!= NULL
);
573 if (EFI_ERROR (Status
)) {
574 ASSERT_EFI_ERROR (Status
);
579 // Make sure GDT table alignment
581 EssData
.Ia32
.GdtTable
= ALIGN_POINTER(GdtBuffer
, sizeof (IA32_TSS_DESCRIPTOR
));
582 NewGdtSize
-= ((UINT8
*)EssData
.Ia32
.GdtTable
- GdtBuffer
);
583 EssData
.Ia32
.GdtTableSize
= NewGdtSize
;
585 EssData
.Ia32
.ExceptionTssDesc
= ((UINT8
*)EssData
.Ia32
.GdtTable
+ OldGdtSize
);
586 EssData
.Ia32
.ExceptionTss
= ((UINT8
*)EssData
.Ia32
.GdtTable
+ OldGdtSize
+
587 EssData
.Ia32
.ExceptionTssDescSize
);
589 EssData
.Ia32
.KnownGoodStackTop
= (UINTN
)StackTop
;
591 "Exception stack top[cpu%lu]: 0x%lX\n",
592 (UINT64
)(UINTN
)Index
,
593 (UINT64
)(UINTN
)StackTop
));
596 InitializeExceptionStackSwitchHandlers (&EssData
);
598 MpInitLibStartupThisAP (
599 InitializeExceptionStackSwitchHandlers
,
608 StackTop
-= NewStackSize
;
613 Initializes MP and exceptions handlers.
615 @param PeiServices The pointer to the PEI Services Table.
617 @retval EFI_SUCCESS MP was successfully initialized.
618 @retval others Error occurred in MP initialization.
622 InitializeCpuMpWorker (
623 IN CONST EFI_PEI_SERVICES
**PeiServices
627 EFI_VECTOR_HANDOFF_INFO
*VectorInfo
;
628 EFI_PEI_VECTOR_HANDOFF_INFO_PPI
*VectorHandoffInfoPpi
;
631 // Get Vector Hand-off Info PPI
634 Status
= PeiServicesLocatePpi (
635 &gEfiVectorHandoffInfoPpiGuid
,
638 (VOID
**)&VectorHandoffInfoPpi
640 if (Status
== EFI_SUCCESS
) {
641 VectorInfo
= VectorHandoffInfoPpi
->Info
;
645 // Initialize default handlers
647 Status
= InitializeCpuExceptionHandlers (VectorInfo
);
648 if (EFI_ERROR (Status
)) {
652 Status
= MpInitLibInitialize ();
653 if (EFI_ERROR (Status
)) {
658 // Special initialization for the sake of Stack Guard
660 InitializeMpExceptionStackSwitchHandlers ();
663 // Update and publish CPU BIST information
665 CollectBistDataFromPpi (PeiServices
);
668 // Install CPU MP PPI
670 Status
= PeiServicesInstallPpi(&mPeiCpuMpPpiDesc
);
671 ASSERT_EFI_ERROR (Status
);
677 The Entry point of the MP CPU PEIM.
679 This function will wakeup APs and collect CPU AP count and install the
682 @param FileHandle Handle of the file being invoked.
683 @param PeiServices Describes the list of possible PEI Services.
685 @retval EFI_SUCCESS MpServicePpi is installed successfully.
691 IN EFI_PEI_FILE_HANDLE FileHandle
,
692 IN CONST EFI_PEI_SERVICES
**PeiServices
698 // For the sake of special initialization needing to be done right after
701 Status
= PeiServicesNotifyPpi (&mPostMemNotifyList
[0]);
702 ASSERT_EFI_ERROR (Status
);