2 MP initialize support functions for DXE phase.
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/UefiLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/DebugAgentLib.h>
21 #define AP_CHECK_INTERVAL (EFI_TIMER_PERIOD_MILLISECONDS (100))
22 #define AP_SAFE_STACK_SIZE 128
24 CPU_MP_DATA
*mCpuMpData
= NULL
;
25 EFI_EVENT mCheckAllApsEvent
= NULL
;
26 EFI_EVENT mMpInitExitBootServicesEvent
= NULL
;
27 EFI_EVENT mLegacyBootEvent
= NULL
;
28 volatile BOOLEAN mStopCheckAllApsStatus
= TRUE
;
29 VOID
*mReservedApLoopFunc
= NULL
;
30 UINTN mReservedTopOfApStack
;
31 volatile UINT32 mNumberToFinish
= 0;
34 Enable Debug Agent to support source debugging on AP function.
43 // Initialize Debug Agent to support source level debug in DXE phase
45 InitializeDebugAgent (DEBUG_AGENT_INIT_DXE_AP
, NULL
, NULL
);
49 Get the pointer to CPU MP Data structure.
51 @return The pointer to CPU MP Data structure.
58 ASSERT (mCpuMpData
!= NULL
);
63 Save the pointer to CPU MP Data structure.
65 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
69 IN CPU_MP_DATA
*CpuMpData
72 mCpuMpData
= CpuMpData
;
76 Allocate reset vector buffer.
78 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
82 IN OUT CPU_MP_DATA
*CpuMpData
86 UINTN ApResetVectorSize
;
87 EFI_PHYSICAL_ADDRESS StartAddress
;
89 if (CpuMpData
->SaveRestoreFlag
) {
90 BackupAndPrepareWakeupBuffer (CpuMpData
);
92 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
93 sizeof (MP_CPU_EXCHANGE_INFO
);
95 StartAddress
= BASE_1MB
;
96 Status
= gBS
->AllocatePages (
99 EFI_SIZE_TO_PAGES (ApResetVectorSize
),
102 ASSERT_EFI_ERROR (Status
);
104 CpuMpData
->WakeupBuffer
= (UINTN
) StartAddress
;
105 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
106 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
108 // copy AP reset code in it
111 (VOID
*) CpuMpData
->WakeupBuffer
,
112 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
113 CpuMpData
->AddressMap
.RendezvousFunnelSize
119 Free AP reset vector buffer.
121 @param[in] CpuMpData The pointer to CPU MP Data structure.
125 IN CPU_MP_DATA
*CpuMpData
129 UINTN ApResetVectorSize
;
131 if (CpuMpData
->SaveRestoreFlag
) {
132 RestoreWakeupBuffer (CpuMpData
);
134 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
135 sizeof (MP_CPU_EXCHANGE_INFO
);
136 Status
= gBS
->FreePages(
137 (EFI_PHYSICAL_ADDRESS
)CpuMpData
->WakeupBuffer
,
138 EFI_SIZE_TO_PAGES (ApResetVectorSize
)
140 ASSERT_EFI_ERROR (Status
);
145 Checks APs status and updates APs status if needed.
149 CheckAndUpdateApsStatus (
153 UINTN ProcessorNumber
;
155 CPU_MP_DATA
*CpuMpData
;
157 CpuMpData
= GetCpuMpData ();
160 // First, check whether pending StartupAllAPs() exists.
162 if (CpuMpData
->WaitEvent
!= NULL
) {
164 Status
= CheckAllAPs ();
166 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.
168 if (Status
!= EFI_NOT_READY
) {
169 Status
= gBS
->SignalEvent (CpuMpData
->WaitEvent
);
170 CpuMpData
->WaitEvent
= NULL
;
175 // Second, check whether pending StartupThisAPs() callings exist.
177 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
179 if (CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
== NULL
) {
183 Status
= CheckThisAP (ProcessorNumber
);
185 if (Status
!= EFI_NOT_READY
) {
186 gBS
->SignalEvent (CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
);
187 CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
= NULL
;
193 Checks APs' status periodically.
195 This function is triggered by timer periodically to check the
196 state of APs for StartupAllAPs() and StartupThisAP() executed
197 in non-blocking mode.
199 @param[in] Event Event triggered.
200 @param[in] Context Parameter passed with the event.
211 // If CheckApsStatus() is not stopped, otherwise return immediately.
213 if (!mStopCheckAllApsStatus
) {
214 CheckAndUpdateApsStatus ();
219 Get Protected mode code segment from current GDT table.
221 @return Protected mode code segment value.
228 IA32_DESCRIPTOR GdtrDesc
;
229 IA32_SEGMENT_DESCRIPTOR
*GdtEntry
;
234 AsmReadGdtr (&GdtrDesc
);
235 GdtEntryCount
= (GdtrDesc
.Limit
+ 1) / sizeof (IA32_SEGMENT_DESCRIPTOR
);
236 GdtEntry
= (IA32_SEGMENT_DESCRIPTOR
*) GdtrDesc
.Base
;
237 for (Index
= 0; Index
< GdtEntryCount
; Index
++) {
238 if (GdtEntry
->Bits
.L
== 0) {
239 if (GdtEntry
->Bits
.Type
> 8 && GdtEntry
->Bits
.L
== 0) {
245 ASSERT (Index
!= -1);
252 @param[in, out] Buffer Pointer to private data buffer.
260 CPU_MP_DATA
*CpuMpData
;
261 BOOLEAN MwaitSupport
;
262 ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc
;
263 UINTN ProcessorNumber
;
265 MpInitLibWhoAmI (&ProcessorNumber
);
266 CpuMpData
= GetCpuMpData ();
267 MwaitSupport
= IsMwaitSupport ();
268 AsmRelocateApLoopFunc
= (ASM_RELOCATE_AP_LOOP
) (UINTN
) mReservedApLoopFunc
;
269 AsmRelocateApLoopFunc (
271 CpuMpData
->ApTargetCState
,
272 CpuMpData
->PmCodeSegment
,
273 mReservedTopOfApStack
- ProcessorNumber
* AP_SAFE_STACK_SIZE
,
274 (UINTN
) &mNumberToFinish
277 // It should never reach here
283 Callback function for ExitBootServices.
285 @param[in] Event Event whose notification function is being invoked.
286 @param[in] Context The pointer to the notification function's context,
287 which is implementation-dependent.
292 MpInitChangeApLoopCallback (
297 CPU_MP_DATA
*CpuMpData
;
299 CpuMpData
= GetCpuMpData ();
300 CpuMpData
->SaveRestoreFlag
= TRUE
;
301 CpuMpData
->PmCodeSegment
= GetProtectedModeCS ();
302 CpuMpData
->ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
303 mNumberToFinish
= CpuMpData
->CpuCount
- 1;
304 WakeUpAP (CpuMpData
, TRUE
, 0, RelocateApLoop
, NULL
);
305 while (mNumberToFinish
> 0) {
308 DEBUG ((DEBUG_INFO
, "%a() done!\n", __FUNCTION__
));
312 Initialize global data for MP support.
314 @param[in] CpuMpData The pointer to CPU MP Data structure.
318 IN CPU_MP_DATA
*CpuMpData
322 EFI_PHYSICAL_ADDRESS Address
;
323 UINTN ApSafeBufferSize
;
325 SaveCpuMpData (CpuMpData
);
327 if (CpuMpData
->CpuCount
== 1) {
329 // If only BSP exists, return
335 // Avoid APs access invalid buffer data which allocated by BootServices,
336 // so we will allocate reserved data for AP loop code. We also need to
337 // allocate this buffer below 4GB due to APs may be transferred to 32bit
338 // protected mode on long mode DXE.
339 // Allocating it in advance since memory services are not available in
340 // Exit Boot Services callback function.
342 ApSafeBufferSize
= CpuMpData
->AddressMap
.RelocateApLoopFuncSize
;
343 ApSafeBufferSize
+= CpuMpData
->CpuCount
* AP_SAFE_STACK_SIZE
;
345 Address
= BASE_4GB
- 1;
346 Status
= gBS
->AllocatePages (
348 EfiReservedMemoryType
,
349 EFI_SIZE_TO_PAGES (ApSafeBufferSize
),
352 ASSERT_EFI_ERROR (Status
);
353 mReservedApLoopFunc
= (VOID
*) (UINTN
) Address
;
354 ASSERT (mReservedApLoopFunc
!= NULL
);
355 mReservedTopOfApStack
= (UINTN
) Address
+ EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (ApSafeBufferSize
));
356 ASSERT ((mReservedTopOfApStack
& (UINTN
)(CPU_STACK_ALIGNMENT
- 1)) == 0);
359 CpuMpData
->AddressMap
.RelocateApLoopFuncAddress
,
360 CpuMpData
->AddressMap
.RelocateApLoopFuncSize
363 Status
= gBS
->CreateEvent (
364 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
370 ASSERT_EFI_ERROR (Status
);
373 // Set timer to check all APs status.
375 Status
= gBS
->SetTimer (
380 ASSERT_EFI_ERROR (Status
);
382 Status
= gBS
->CreateEvent (
383 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
385 MpInitChangeApLoopCallback
,
387 &mMpInitExitBootServicesEvent
389 ASSERT_EFI_ERROR (Status
);
391 Status
= gBS
->CreateEventEx (
394 MpInitChangeApLoopCallback
,
396 &gEfiEventLegacyBootGuid
,
399 ASSERT_EFI_ERROR (Status
);
403 This service executes a caller provided function on all enabled APs.
405 @param[in] Procedure A pointer to the function to be run on
406 enabled APs of the system. See type
408 @param[in] SingleThread If TRUE, then all the enabled APs execute
409 the function specified by Procedure one by
410 one, in ascending order of processor handle
411 number. If FALSE, then all the enabled APs
412 execute the function specified by Procedure
414 @param[in] WaitEvent The event created by the caller with CreateEvent()
415 service. If it is NULL, then execute in
416 blocking mode. BSP waits until all APs finish
417 or TimeoutInMicroSeconds expires. If it's
418 not NULL, then execute in non-blocking mode.
419 BSP requests the function specified by
420 Procedure to be started on all the enabled
421 APs, and go on executing immediately. If
422 all return from Procedure, or TimeoutInMicroSeconds
423 expires, this event is signaled. The BSP
424 can use the CheckEvent() or WaitForEvent()
425 services to check the state of event. Type
426 EFI_EVENT is defined in CreateEvent() in
427 the Unified Extensible Firmware Interface
429 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
430 APs to return from Procedure, either for
431 blocking or non-blocking mode. Zero means
432 infinity. If the timeout expires before
433 all APs return from Procedure, then Procedure
434 on the failed APs is terminated. All enabled
435 APs are available for next function assigned
436 by MpInitLibStartupAllAPs() or
437 MPInitLibStartupThisAP().
438 If the timeout expires in blocking mode,
439 BSP returns EFI_TIMEOUT. If the timeout
440 expires in non-blocking mode, WaitEvent
441 is signaled with SignalEvent().
442 @param[in] ProcedureArgument The parameter passed into Procedure for
444 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
445 if all APs finish successfully, then its
446 content is set to NULL. If not all APs
447 finish before timeout expires, then its
448 content is set to address of the buffer
449 holding handle numbers of the failed APs.
450 The buffer is allocated by MP Initialization
451 library, and it's the caller's responsibility to
452 free the buffer with FreePool() service.
453 In blocking mode, it is ready for consumption
454 when the call returns. In non-blocking mode,
455 it is ready when WaitEvent is signaled. The
456 list of failed CPU is terminated by
459 @retval EFI_SUCCESS In blocking mode, all APs have finished before
461 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
463 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
464 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
466 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
468 @retval EFI_DEVICE_ERROR Caller processor is AP.
469 @retval EFI_NOT_STARTED No enabled APs exist in the system.
470 @retval EFI_NOT_READY Any enabled APs are busy.
471 @retval EFI_NOT_READY MP Initialize Library is not initialized.
472 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
473 all enabled APs have finished.
474 @retval EFI_INVALID_PARAMETER Procedure is NULL.
479 MpInitLibStartupAllAPs (
480 IN EFI_AP_PROCEDURE Procedure
,
481 IN BOOLEAN SingleThread
,
482 IN EFI_EVENT WaitEvent OPTIONAL
,
483 IN UINTN TimeoutInMicroseconds
,
484 IN VOID
*ProcedureArgument OPTIONAL
,
485 OUT UINTN
**FailedCpuList OPTIONAL
491 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
493 mStopCheckAllApsStatus
= TRUE
;
495 Status
= StartupAllAPsWorker (
499 TimeoutInMicroseconds
,
505 // Start checkAllApsStatus
507 mStopCheckAllApsStatus
= FALSE
;
513 This service lets the caller get one enabled AP to execute a caller-provided
516 @param[in] Procedure A pointer to the function to be run on the
517 designated AP of the system. See type
519 @param[in] ProcessorNumber The handle number of the AP. The range is
520 from 0 to the total number of logical
521 processors minus 1. The total number of
522 logical processors can be retrieved by
523 MpInitLibGetNumberOfProcessors().
524 @param[in] WaitEvent The event created by the caller with CreateEvent()
525 service. If it is NULL, then execute in
526 blocking mode. BSP waits until this AP finish
527 or TimeoutInMicroSeconds expires. If it's
528 not NULL, then execute in non-blocking mode.
529 BSP requests the function specified by
530 Procedure to be started on this AP,
531 and go on executing immediately. If this AP
532 return from Procedure or TimeoutInMicroSeconds
533 expires, this event is signaled. The BSP
534 can use the CheckEvent() or WaitForEvent()
535 services to check the state of event. Type
536 EFI_EVENT is defined in CreateEvent() in
537 the Unified Extensible Firmware Interface
539 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
540 this AP to finish this Procedure, either for
541 blocking or non-blocking mode. Zero means
542 infinity. If the timeout expires before
543 this AP returns from Procedure, then Procedure
544 on the AP is terminated. The
545 AP is available for next function assigned
546 by MpInitLibStartupAllAPs() or
547 MpInitLibStartupThisAP().
548 If the timeout expires in blocking mode,
549 BSP returns EFI_TIMEOUT. If the timeout
550 expires in non-blocking mode, WaitEvent
551 is signaled with SignalEvent().
552 @param[in] ProcedureArgument The parameter passed into Procedure on the
554 @param[out] Finished If NULL, this parameter is ignored. In
555 blocking mode, this parameter is ignored.
556 In non-blocking mode, if AP returns from
557 Procedure before the timeout expires, its
558 content is set to TRUE. Otherwise, the
559 value is set to FALSE. The caller can
560 determine if the AP returned from Procedure
561 by evaluating this value.
563 @retval EFI_SUCCESS In blocking mode, specified AP finished before
565 @retval EFI_SUCCESS In non-blocking mode, the function has been
566 dispatched to specified AP.
567 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
568 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
570 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
572 @retval EFI_DEVICE_ERROR The calling processor is an AP.
573 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
574 the specified AP has finished.
575 @retval EFI_NOT_READY The specified AP is busy.
576 @retval EFI_NOT_READY MP Initialize Library is not initialized.
577 @retval EFI_NOT_FOUND The processor with the handle specified by
578 ProcessorNumber does not exist.
579 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
580 @retval EFI_INVALID_PARAMETER Procedure is NULL.
585 MpInitLibStartupThisAP (
586 IN EFI_AP_PROCEDURE Procedure
,
587 IN UINTN ProcessorNumber
,
588 IN EFI_EVENT WaitEvent OPTIONAL
,
589 IN UINTN TimeoutInMicroseconds
,
590 IN VOID
*ProcedureArgument OPTIONAL
,
591 OUT BOOLEAN
*Finished OPTIONAL
597 // temporarily stop checkAllApsStatus for avoid resource dead-lock.
599 mStopCheckAllApsStatus
= TRUE
;
601 Status
= StartupThisAPWorker (
605 TimeoutInMicroseconds
,
610 mStopCheckAllApsStatus
= FALSE
;
616 This service switches the requested AP to be the BSP from that point onward.
617 This service changes the BSP for all purposes. This call can only be performed
620 @param[in] ProcessorNumber The handle number of AP that is to become the new
621 BSP. The range is from 0 to the total number of
622 logical processors minus 1. The total number of
623 logical processors can be retrieved by
624 MpInitLibGetNumberOfProcessors().
625 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
626 enabled AP. Otherwise, it will be disabled.
628 @retval EFI_SUCCESS BSP successfully switched.
629 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
630 this service returning.
631 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
632 @retval EFI_DEVICE_ERROR The calling processor is an AP.
633 @retval EFI_NOT_FOUND The processor with the handle specified by
634 ProcessorNumber does not exist.
635 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
637 @retval EFI_NOT_READY The specified AP is busy.
638 @retval EFI_NOT_READY MP Initialize Library is not initialized.
644 IN UINTN ProcessorNumber
,
645 IN BOOLEAN EnableOldBSP
649 BOOLEAN OldInterruptState
;
652 // Before send both BSP and AP to a procedure to exchange their roles,
653 // interrupt must be disabled. This is because during the exchange role
654 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
655 // be corrupted, since interrupt return address will be pushed to stack
658 OldInterruptState
= SaveAndDisableInterrupts ();
661 // Mask LINT0 & LINT1 for the old BSP
663 DisableLvtInterrupts ();
665 Status
= SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
668 // Restore interrupt state.
670 SetInterruptState (OldInterruptState
);
676 This service lets the caller enable or disable an AP from this point onward.
677 This service may only be called from the BSP.
679 @param[in] ProcessorNumber The handle number of AP.
680 The range is from 0 to the total number of
681 logical processors minus 1. The total number of
682 logical processors can be retrieved by
683 MpInitLibGetNumberOfProcessors().
684 @param[in] EnableAP Specifies the new state for the processor for
685 enabled, FALSE for disabled.
686 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
687 the new health status of the AP. This flag
688 corresponds to StatusFlag defined in
689 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
690 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
691 bits are ignored. If it is NULL, this parameter
694 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
695 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
696 prior to this service returning.
697 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
698 @retval EFI_DEVICE_ERROR The calling processor is an AP.
699 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
701 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
702 @retval EFI_NOT_READY MP Initialize Library is not initialized.
707 MpInitLibEnableDisableAP (
708 IN UINTN ProcessorNumber
,
710 IN UINT32
*HealthFlag OPTIONAL
714 BOOLEAN TempStopCheckState
;
716 TempStopCheckState
= FALSE
;
718 // temporarily stop checkAllAPsStatus for initialize parameters.
720 if (!mStopCheckAllApsStatus
) {
721 mStopCheckAllApsStatus
= TRUE
;
722 TempStopCheckState
= TRUE
;
725 Status
= EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);
727 if (TempStopCheckState
) {
728 mStopCheckAllApsStatus
= FALSE
;