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>
20 #include <Library/DxeServicesTableLib.h>
22 #include <Protocol/Timer.h>
24 #define AP_CHECK_INTERVAL (EFI_TIMER_PERIOD_MILLISECONDS (100))
25 #define AP_SAFE_STACK_SIZE 128
27 CPU_MP_DATA
*mCpuMpData
= NULL
;
28 EFI_EVENT mCheckAllApsEvent
= NULL
;
29 EFI_EVENT mMpInitExitBootServicesEvent
= NULL
;
30 EFI_EVENT mLegacyBootEvent
= NULL
;
31 volatile BOOLEAN mStopCheckAllApsStatus
= TRUE
;
32 VOID
*mReservedApLoopFunc
= NULL
;
33 UINTN mReservedTopOfApStack
;
34 volatile UINT32 mNumberToFinish
= 0;
37 Enable Debug Agent to support source debugging on AP function.
46 // Initialize Debug Agent to support source level debug in DXE phase
48 InitializeDebugAgent (DEBUG_AGENT_INIT_DXE_AP
, NULL
, NULL
);
52 Get the pointer to CPU MP Data structure.
54 @return The pointer to CPU MP Data structure.
61 ASSERT (mCpuMpData
!= NULL
);
66 Save the pointer to CPU MP Data structure.
68 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
72 IN CPU_MP_DATA
*CpuMpData
75 mCpuMpData
= CpuMpData
;
79 Get available system memory below 1MB by specified size.
81 @param[in] WakeupBufferSize Wakeup buffer size required
83 @retval other Return wakeup buffer address below 1MB.
84 @retval -1 Cannot find free memory below 1MB.
88 IN UINTN WakeupBufferSize
92 EFI_PHYSICAL_ADDRESS StartAddress
;
94 StartAddress
= BASE_1MB
;
95 Status
= gBS
->AllocatePages (
98 EFI_SIZE_TO_PAGES (WakeupBufferSize
),
101 ASSERT_EFI_ERROR (Status
);
102 if (!EFI_ERROR (Status
)) {
103 Status
= gBS
->FreePages(
105 EFI_SIZE_TO_PAGES (WakeupBufferSize
)
107 ASSERT_EFI_ERROR (Status
);
108 DEBUG ((DEBUG_INFO
, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",
109 (UINTN
) StartAddress
, WakeupBufferSize
));
111 StartAddress
= (EFI_PHYSICAL_ADDRESS
) -1;
113 return (UINTN
) StartAddress
;
117 Get available EfiBootServicesCode memory below 4GB by specified size.
119 This buffer is required to safely transfer AP from real address mode to
120 protected mode or long mode, due to the fact that the buffer returned by
121 GetWakeupBuffer() may be marked as non-executable.
123 @param[in] BufferSize Wakeup transition buffer size.
125 @retval other Return wakeup transition buffer address below 4GB.
126 @retval 0 Cannot find free memory below 4GB.
129 GetModeTransitionBuffer (
134 EFI_PHYSICAL_ADDRESS StartAddress
;
136 StartAddress
= BASE_4GB
- 1;
137 Status
= gBS
->AllocatePages (
140 EFI_SIZE_TO_PAGES (BufferSize
),
143 if (EFI_ERROR (Status
)) {
147 return (UINTN
)StartAddress
;
151 Checks APs status and updates APs status if needed.
155 CheckAndUpdateApsStatus (
159 UINTN ProcessorNumber
;
161 CPU_MP_DATA
*CpuMpData
;
163 CpuMpData
= GetCpuMpData ();
166 // First, check whether pending StartupAllAPs() exists.
168 if (CpuMpData
->WaitEvent
!= NULL
) {
170 Status
= CheckAllAPs ();
172 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.
174 if (Status
!= EFI_NOT_READY
) {
175 Status
= gBS
->SignalEvent (CpuMpData
->WaitEvent
);
176 CpuMpData
->WaitEvent
= NULL
;
181 // Second, check whether pending StartupThisAPs() callings exist.
183 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
185 if (CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
== NULL
) {
189 Status
= CheckThisAP (ProcessorNumber
);
191 if (Status
!= EFI_NOT_READY
) {
192 gBS
->SignalEvent (CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
);
193 CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
= NULL
;
199 Checks APs' status periodically.
201 This function is triggered by timer periodically to check the
202 state of APs for StartupAllAPs() and StartupThisAP() executed
203 in non-blocking mode.
205 @param[in] Event Event triggered.
206 @param[in] Context Parameter passed with the event.
217 // If CheckApsStatus() is not stopped, otherwise return immediately.
219 if (!mStopCheckAllApsStatus
) {
220 CheckAndUpdateApsStatus ();
225 Get Protected mode code segment from current GDT table.
227 @return Protected mode code segment value.
234 IA32_DESCRIPTOR GdtrDesc
;
235 IA32_SEGMENT_DESCRIPTOR
*GdtEntry
;
240 AsmReadGdtr (&GdtrDesc
);
241 GdtEntryCount
= (GdtrDesc
.Limit
+ 1) / sizeof (IA32_SEGMENT_DESCRIPTOR
);
242 GdtEntry
= (IA32_SEGMENT_DESCRIPTOR
*) GdtrDesc
.Base
;
243 for (Index
= 0; Index
< GdtEntryCount
; Index
++) {
244 if (GdtEntry
->Bits
.L
== 0) {
245 if (GdtEntry
->Bits
.Type
> 8 && GdtEntry
->Bits
.L
== 0) {
251 ASSERT (Index
!= -1);
258 @param[in, out] Buffer Pointer to private data buffer.
266 CPU_MP_DATA
*CpuMpData
;
267 BOOLEAN MwaitSupport
;
268 ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc
;
269 UINTN ProcessorNumber
;
271 MpInitLibWhoAmI (&ProcessorNumber
);
272 CpuMpData
= GetCpuMpData ();
273 MwaitSupport
= IsMwaitSupport ();
274 AsmRelocateApLoopFunc
= (ASM_RELOCATE_AP_LOOP
) (UINTN
) mReservedApLoopFunc
;
275 AsmRelocateApLoopFunc (
277 CpuMpData
->ApTargetCState
,
278 CpuMpData
->PmCodeSegment
,
279 mReservedTopOfApStack
- ProcessorNumber
* AP_SAFE_STACK_SIZE
,
280 (UINTN
) &mNumberToFinish
283 // It should never reach here
289 Callback function for ExitBootServices.
291 @param[in] Event Event whose notification function is being invoked.
292 @param[in] Context The pointer to the notification function's context,
293 which is implementation-dependent.
298 MpInitChangeApLoopCallback (
303 CPU_MP_DATA
*CpuMpData
;
305 CpuMpData
= GetCpuMpData ();
306 CpuMpData
->PmCodeSegment
= GetProtectedModeCS ();
307 CpuMpData
->ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
308 mNumberToFinish
= CpuMpData
->CpuCount
- 1;
309 WakeUpAP (CpuMpData
, TRUE
, 0, RelocateApLoop
, NULL
);
310 while (mNumberToFinish
> 0) {
313 DEBUG ((DEBUG_INFO
, "%a() done!\n", __FUNCTION__
));
317 Initialize global data for MP support.
319 @param[in] CpuMpData The pointer to CPU MP Data structure.
323 IN CPU_MP_DATA
*CpuMpData
327 EFI_PHYSICAL_ADDRESS Address
;
328 UINTN ApSafeBufferSize
;
330 EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc
;
332 CPU_INFO_IN_HOB
*CpuInfoInHob
;
334 SaveCpuMpData (CpuMpData
);
336 if (CpuMpData
->CpuCount
== 1) {
338 // If only BSP exists, return
343 if (PcdGetBool (PcdCpuStackGuard
)) {
345 // One extra page at the bottom of the stack is needed for Guard page.
347 if (CpuMpData
->CpuApStackSize
<= EFI_PAGE_SIZE
) {
348 DEBUG ((DEBUG_ERROR
, "PcdCpuApStackSize is not big enough for Stack Guard!\n"));
353 // DXE will reuse stack allocated for APs at PEI phase if it's available.
354 // Let's check it here.
356 // Note: BSP's stack guard is set at DxeIpl phase. But for the sake of
357 // BSP/AP exchange, stack guard for ApTopOfStack of cpu 0 will still be
360 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
361 for (Index
= 0; Index
< CpuMpData
->CpuCount
; ++Index
) {
362 if (CpuInfoInHob
!= NULL
&& CpuInfoInHob
[Index
].ApTopOfStack
!= 0) {
363 StackBase
= (UINTN
)CpuInfoInHob
[Index
].ApTopOfStack
- CpuMpData
->CpuApStackSize
;
365 StackBase
= CpuMpData
->Buffer
+ Index
* CpuMpData
->CpuApStackSize
;
368 Status
= gDS
->GetMemorySpaceDescriptor (StackBase
, &MemDesc
);
369 ASSERT_EFI_ERROR (Status
);
371 Status
= gDS
->SetMemorySpaceAttributes (
373 EFI_PAGES_TO_SIZE (1),
374 MemDesc
.Attributes
| EFI_MEMORY_RP
376 ASSERT_EFI_ERROR (Status
);
378 DEBUG ((DEBUG_INFO
, "Stack Guard set at %lx [cpu%lu]!\n",
379 (UINT64
)StackBase
, (UINT64
)Index
));
384 // Avoid APs access invalid buffer data which allocated by BootServices,
385 // so we will allocate reserved data for AP loop code. We also need to
386 // allocate this buffer below 4GB due to APs may be transferred to 32bit
387 // protected mode on long mode DXE.
388 // Allocating it in advance since memory services are not available in
389 // Exit Boot Services callback function.
391 ApSafeBufferSize
= CpuMpData
->AddressMap
.RelocateApLoopFuncSize
;
392 ApSafeBufferSize
+= CpuMpData
->CpuCount
* AP_SAFE_STACK_SIZE
;
394 Address
= BASE_4GB
- 1;
395 Status
= gBS
->AllocatePages (
397 EfiReservedMemoryType
,
398 EFI_SIZE_TO_PAGES (ApSafeBufferSize
),
401 ASSERT_EFI_ERROR (Status
);
402 mReservedApLoopFunc
= (VOID
*) (UINTN
) Address
;
403 ASSERT (mReservedApLoopFunc
!= NULL
);
404 mReservedTopOfApStack
= (UINTN
) Address
+ EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (ApSafeBufferSize
));
405 ASSERT ((mReservedTopOfApStack
& (UINTN
)(CPU_STACK_ALIGNMENT
- 1)) == 0);
408 CpuMpData
->AddressMap
.RelocateApLoopFuncAddress
,
409 CpuMpData
->AddressMap
.RelocateApLoopFuncSize
412 Status
= gBS
->CreateEvent (
413 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
419 ASSERT_EFI_ERROR (Status
);
422 // Set timer to check all APs status.
424 Status
= gBS
->SetTimer (
429 ASSERT_EFI_ERROR (Status
);
431 Status
= gBS
->CreateEvent (
432 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
434 MpInitChangeApLoopCallback
,
436 &mMpInitExitBootServicesEvent
438 ASSERT_EFI_ERROR (Status
);
440 Status
= gBS
->CreateEventEx (
443 MpInitChangeApLoopCallback
,
445 &gEfiEventLegacyBootGuid
,
448 ASSERT_EFI_ERROR (Status
);
452 This service executes a caller provided function on all enabled APs.
454 @param[in] Procedure A pointer to the function to be run on
455 enabled APs of the system. See type
457 @param[in] SingleThread If TRUE, then all the enabled APs execute
458 the function specified by Procedure one by
459 one, in ascending order of processor handle
460 number. If FALSE, then all the enabled APs
461 execute the function specified by Procedure
463 @param[in] WaitEvent The event created by the caller with CreateEvent()
464 service. If it is NULL, then execute in
465 blocking mode. BSP waits until all APs finish
466 or TimeoutInMicroSeconds expires. If it's
467 not NULL, then execute in non-blocking mode.
468 BSP requests the function specified by
469 Procedure to be started on all the enabled
470 APs, and go on executing immediately. If
471 all return from Procedure, or TimeoutInMicroSeconds
472 expires, this event is signaled. The BSP
473 can use the CheckEvent() or WaitForEvent()
474 services to check the state of event. Type
475 EFI_EVENT is defined in CreateEvent() in
476 the Unified Extensible Firmware Interface
478 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
479 APs to return from Procedure, either for
480 blocking or non-blocking mode. Zero means
481 infinity. If the timeout expires before
482 all APs return from Procedure, then Procedure
483 on the failed APs is terminated. All enabled
484 APs are available for next function assigned
485 by MpInitLibStartupAllAPs() or
486 MPInitLibStartupThisAP().
487 If the timeout expires in blocking mode,
488 BSP returns EFI_TIMEOUT. If the timeout
489 expires in non-blocking mode, WaitEvent
490 is signaled with SignalEvent().
491 @param[in] ProcedureArgument The parameter passed into Procedure for
493 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
494 if all APs finish successfully, then its
495 content is set to NULL. If not all APs
496 finish before timeout expires, then its
497 content is set to address of the buffer
498 holding handle numbers of the failed APs.
499 The buffer is allocated by MP Initialization
500 library, and it's the caller's responsibility to
501 free the buffer with FreePool() service.
502 In blocking mode, it is ready for consumption
503 when the call returns. In non-blocking mode,
504 it is ready when WaitEvent is signaled. The
505 list of failed CPU is terminated by
508 @retval EFI_SUCCESS In blocking mode, all APs have finished before
510 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
512 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
513 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
515 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
517 @retval EFI_DEVICE_ERROR Caller processor is AP.
518 @retval EFI_NOT_STARTED No enabled APs exist in the system.
519 @retval EFI_NOT_READY Any enabled APs are busy.
520 @retval EFI_NOT_READY MP Initialize Library is not initialized.
521 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
522 all enabled APs have finished.
523 @retval EFI_INVALID_PARAMETER Procedure is NULL.
528 MpInitLibStartupAllAPs (
529 IN EFI_AP_PROCEDURE Procedure
,
530 IN BOOLEAN SingleThread
,
531 IN EFI_EVENT WaitEvent OPTIONAL
,
532 IN UINTN TimeoutInMicroseconds
,
533 IN VOID
*ProcedureArgument OPTIONAL
,
534 OUT UINTN
**FailedCpuList OPTIONAL
540 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
542 mStopCheckAllApsStatus
= TRUE
;
544 Status
= StartupAllAPsWorker (
548 TimeoutInMicroseconds
,
554 // Start checkAllApsStatus
556 mStopCheckAllApsStatus
= FALSE
;
562 This service lets the caller get one enabled AP to execute a caller-provided
565 @param[in] Procedure A pointer to the function to be run on the
566 designated AP of the system. See type
568 @param[in] ProcessorNumber The handle number of the AP. The range is
569 from 0 to the total number of logical
570 processors minus 1. The total number of
571 logical processors can be retrieved by
572 MpInitLibGetNumberOfProcessors().
573 @param[in] WaitEvent The event created by the caller with CreateEvent()
574 service. If it is NULL, then execute in
575 blocking mode. BSP waits until this AP finish
576 or TimeoutInMicroSeconds expires. If it's
577 not NULL, then execute in non-blocking mode.
578 BSP requests the function specified by
579 Procedure to be started on this AP,
580 and go on executing immediately. If this AP
581 return from Procedure or TimeoutInMicroSeconds
582 expires, this event is signaled. The BSP
583 can use the CheckEvent() or WaitForEvent()
584 services to check the state of event. Type
585 EFI_EVENT is defined in CreateEvent() in
586 the Unified Extensible Firmware Interface
588 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
589 this AP to finish this Procedure, either for
590 blocking or non-blocking mode. Zero means
591 infinity. If the timeout expires before
592 this AP returns from Procedure, then Procedure
593 on the AP is terminated. The
594 AP is available for next function assigned
595 by MpInitLibStartupAllAPs() or
596 MpInitLibStartupThisAP().
597 If the timeout expires in blocking mode,
598 BSP returns EFI_TIMEOUT. If the timeout
599 expires in non-blocking mode, WaitEvent
600 is signaled with SignalEvent().
601 @param[in] ProcedureArgument The parameter passed into Procedure on the
603 @param[out] Finished If NULL, this parameter is ignored. In
604 blocking mode, this parameter is ignored.
605 In non-blocking mode, if AP returns from
606 Procedure before the timeout expires, its
607 content is set to TRUE. Otherwise, the
608 value is set to FALSE. The caller can
609 determine if the AP returned from Procedure
610 by evaluating this value.
612 @retval EFI_SUCCESS In blocking mode, specified AP finished before
614 @retval EFI_SUCCESS In non-blocking mode, the function has been
615 dispatched to specified AP.
616 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
617 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
619 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
621 @retval EFI_DEVICE_ERROR The calling processor is an AP.
622 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
623 the specified AP has finished.
624 @retval EFI_NOT_READY The specified AP is busy.
625 @retval EFI_NOT_READY MP Initialize Library is not initialized.
626 @retval EFI_NOT_FOUND The processor with the handle specified by
627 ProcessorNumber does not exist.
628 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
629 @retval EFI_INVALID_PARAMETER Procedure is NULL.
634 MpInitLibStartupThisAP (
635 IN EFI_AP_PROCEDURE Procedure
,
636 IN UINTN ProcessorNumber
,
637 IN EFI_EVENT WaitEvent OPTIONAL
,
638 IN UINTN TimeoutInMicroseconds
,
639 IN VOID
*ProcedureArgument OPTIONAL
,
640 OUT BOOLEAN
*Finished OPTIONAL
646 // temporarily stop checkAllApsStatus for avoid resource dead-lock.
648 mStopCheckAllApsStatus
= TRUE
;
650 Status
= StartupThisAPWorker (
654 TimeoutInMicroseconds
,
659 mStopCheckAllApsStatus
= FALSE
;
665 This service switches the requested AP to be the BSP from that point onward.
666 This service changes the BSP for all purposes. This call can only be performed
669 @param[in] ProcessorNumber The handle number of AP that is to become the new
670 BSP. The range is from 0 to the total number of
671 logical processors minus 1. The total number of
672 logical processors can be retrieved by
673 MpInitLibGetNumberOfProcessors().
674 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
675 enabled AP. Otherwise, it will be disabled.
677 @retval EFI_SUCCESS BSP successfully switched.
678 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
679 this service returning.
680 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
681 @retval EFI_DEVICE_ERROR The calling processor is an AP.
682 @retval EFI_NOT_FOUND The processor with the handle specified by
683 ProcessorNumber does not exist.
684 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
686 @retval EFI_NOT_READY The specified AP is busy.
687 @retval EFI_NOT_READY MP Initialize Library is not initialized.
693 IN UINTN ProcessorNumber
,
694 IN BOOLEAN EnableOldBSP
698 EFI_TIMER_ARCH_PROTOCOL
*Timer
;
703 // Locate Timer Arch Protocol
705 Status
= gBS
->LocateProtocol (&gEfiTimerArchProtocolGuid
, NULL
, (VOID
**) &Timer
);
706 if (EFI_ERROR (Status
)) {
712 // Save current rate of DXE Timer
714 Timer
->GetTimerPeriod (Timer
, &TimerPeriod
);
716 // Disable DXE Timer and drain pending interrupts
718 Timer
->SetTimerPeriod (Timer
, 0);
721 Status
= SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
725 // Enable and restore rate of DXE Timer
727 Timer
->SetTimerPeriod (Timer
, TimerPeriod
);
734 This service lets the caller enable or disable an AP from this point onward.
735 This service may only be called from the BSP.
737 @param[in] ProcessorNumber The handle number of AP.
738 The range is from 0 to the total number of
739 logical processors minus 1. The total number of
740 logical processors can be retrieved by
741 MpInitLibGetNumberOfProcessors().
742 @param[in] EnableAP Specifies the new state for the processor for
743 enabled, FALSE for disabled.
744 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
745 the new health status of the AP. This flag
746 corresponds to StatusFlag defined in
747 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
748 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
749 bits are ignored. If it is NULL, this parameter
752 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
753 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
754 prior to this service returning.
755 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
756 @retval EFI_DEVICE_ERROR The calling processor is an AP.
757 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
759 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
760 @retval EFI_NOT_READY MP Initialize Library is not initialized.
765 MpInitLibEnableDisableAP (
766 IN UINTN ProcessorNumber
,
768 IN UINT32
*HealthFlag OPTIONAL
772 BOOLEAN TempStopCheckState
;
774 TempStopCheckState
= FALSE
;
776 // temporarily stop checkAllAPsStatus for initialize parameters.
778 if (!mStopCheckAllApsStatus
) {
779 mStopCheckAllApsStatus
= TRUE
;
780 TempStopCheckState
= TRUE
;
783 Status
= EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);
785 if (TempStopCheckState
) {
786 mStopCheckAllApsStatus
= FALSE
;