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>
20 #define AP_CHECK_INTERVAL (EFI_TIMER_PERIOD_MILLISECONDS (100))
21 #define AP_SAFE_STACK_SIZE 128
23 CPU_MP_DATA
*mCpuMpData
= NULL
;
24 EFI_EVENT mCheckAllApsEvent
= NULL
;
25 EFI_EVENT mMpInitExitBootServicesEvent
= NULL
;
26 EFI_EVENT mLegacyBootEvent
= NULL
;
27 volatile BOOLEAN mStopCheckAllApsStatus
= TRUE
;
28 VOID
*mReservedApLoopFunc
= NULL
;
29 UINTN mReservedTopOfApStack
;
30 volatile UINT32 mNumberToFinish
= 0;
33 Get the pointer to CPU MP Data structure.
35 @return The pointer to CPU MP Data structure.
42 ASSERT (mCpuMpData
!= NULL
);
47 Save the pointer to CPU MP Data structure.
49 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
53 IN CPU_MP_DATA
*CpuMpData
56 mCpuMpData
= CpuMpData
;
60 Allocate reset vector buffer.
62 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
66 IN OUT CPU_MP_DATA
*CpuMpData
70 UINTN ApResetVectorSize
;
71 EFI_PHYSICAL_ADDRESS StartAddress
;
73 if (CpuMpData
->SaveRestoreFlag
) {
74 BackupAndPrepareWakeupBuffer (CpuMpData
);
76 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
77 sizeof (MP_CPU_EXCHANGE_INFO
);
79 StartAddress
= BASE_1MB
;
80 Status
= gBS
->AllocatePages (
83 EFI_SIZE_TO_PAGES (ApResetVectorSize
),
86 ASSERT_EFI_ERROR (Status
);
88 CpuMpData
->WakeupBuffer
= (UINTN
) StartAddress
;
89 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
90 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
92 // copy AP reset code in it
95 (VOID
*) CpuMpData
->WakeupBuffer
,
96 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
97 CpuMpData
->AddressMap
.RendezvousFunnelSize
103 Free AP reset vector buffer.
105 @param[in] CpuMpData The pointer to CPU MP Data structure.
109 IN CPU_MP_DATA
*CpuMpData
113 UINTN ApResetVectorSize
;
115 if (CpuMpData
->SaveRestoreFlag
) {
116 RestoreWakeupBuffer (CpuMpData
);
118 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
119 sizeof (MP_CPU_EXCHANGE_INFO
);
120 Status
= gBS
->FreePages(
121 (EFI_PHYSICAL_ADDRESS
)CpuMpData
->WakeupBuffer
,
122 EFI_SIZE_TO_PAGES (ApResetVectorSize
)
124 ASSERT_EFI_ERROR (Status
);
129 Checks APs status and updates APs status if needed.
133 CheckAndUpdateApsStatus (
137 UINTN ProcessorNumber
;
139 CPU_MP_DATA
*CpuMpData
;
141 CpuMpData
= GetCpuMpData ();
144 // First, check whether pending StartupAllAPs() exists.
146 if (CpuMpData
->WaitEvent
!= NULL
) {
148 Status
= CheckAllAPs ();
150 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.
152 if (Status
!= EFI_NOT_READY
) {
153 Status
= gBS
->SignalEvent (CpuMpData
->WaitEvent
);
154 CpuMpData
->WaitEvent
= NULL
;
159 // Second, check whether pending StartupThisAPs() callings exist.
161 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
163 if (CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
== NULL
) {
167 Status
= CheckThisAP (ProcessorNumber
);
169 if (Status
!= EFI_NOT_READY
) {
170 gBS
->SignalEvent (CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
);
171 CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
= NULL
;
177 Checks APs' status periodically.
179 This function is triggered by timer periodically to check the
180 state of APs for StartupAllAPs() and StartupThisAP() executed
181 in non-blocking mode.
183 @param[in] Event Event triggered.
184 @param[in] Context Parameter passed with the event.
195 // If CheckApsStatus() is not stopped, otherwise return immediately.
197 if (!mStopCheckAllApsStatus
) {
198 CheckAndUpdateApsStatus ();
203 Get Protected mode code segment from current GDT table.
205 @return Protected mode code segment value.
212 IA32_DESCRIPTOR GdtrDesc
;
213 IA32_SEGMENT_DESCRIPTOR
*GdtEntry
;
218 AsmReadGdtr (&GdtrDesc
);
219 GdtEntryCount
= (GdtrDesc
.Limit
+ 1) / sizeof (IA32_SEGMENT_DESCRIPTOR
);
220 GdtEntry
= (IA32_SEGMENT_DESCRIPTOR
*) GdtrDesc
.Base
;
221 for (Index
= 0; Index
< GdtEntryCount
; Index
++) {
222 if (GdtEntry
->Bits
.L
== 0) {
223 if (GdtEntry
->Bits
.Type
> 8 && GdtEntry
->Bits
.L
== 0) {
229 ASSERT (Index
!= -1);
236 @param[in, out] Buffer Pointer to private data buffer.
244 CPU_MP_DATA
*CpuMpData
;
245 BOOLEAN MwaitSupport
;
246 ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc
;
247 UINTN ProcessorNumber
;
249 MpInitLibWhoAmI (&ProcessorNumber
);
250 CpuMpData
= GetCpuMpData ();
251 MwaitSupport
= IsMwaitSupport ();
252 AsmRelocateApLoopFunc
= (ASM_RELOCATE_AP_LOOP
) (UINTN
) mReservedApLoopFunc
;
253 AsmRelocateApLoopFunc (
255 CpuMpData
->ApTargetCState
,
256 CpuMpData
->PmCodeSegment
,
257 mReservedTopOfApStack
- ProcessorNumber
* AP_SAFE_STACK_SIZE
,
258 (UINTN
) &mNumberToFinish
261 // It should never reach here
267 Callback function for ExitBootServices.
269 @param[in] Event Event whose notification function is being invoked.
270 @param[in] Context The pointer to the notification function's context,
271 which is implementation-dependent.
276 MpInitChangeApLoopCallback (
281 CPU_MP_DATA
*CpuMpData
;
283 CpuMpData
= GetCpuMpData ();
284 CpuMpData
->SaveRestoreFlag
= TRUE
;
285 CpuMpData
->PmCodeSegment
= GetProtectedModeCS ();
286 CpuMpData
->ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
287 mNumberToFinish
= CpuMpData
->CpuCount
- 1;
288 WakeUpAP (CpuMpData
, TRUE
, 0, RelocateApLoop
, NULL
);
289 while (mNumberToFinish
> 0) {
292 DEBUG ((DEBUG_INFO
, "%a() done!\n", __FUNCTION__
));
296 Initialize global data for MP support.
298 @param[in] CpuMpData The pointer to CPU MP Data structure.
302 IN CPU_MP_DATA
*CpuMpData
306 EFI_PHYSICAL_ADDRESS Address
;
307 UINTN ApSafeBufferSize
;
309 SaveCpuMpData (CpuMpData
);
311 if (CpuMpData
->CpuCount
== 1) {
313 // If only BSP exists, return
319 // Avoid APs access invalid buffer data which allocated by BootServices,
320 // so we will allocate reserved data for AP loop code. We also need to
321 // allocate this buffer below 4GB due to APs may be transferred to 32bit
322 // protected mode on long mode DXE.
323 // Allocating it in advance since memory services are not available in
324 // Exit Boot Services callback function.
326 ApSafeBufferSize
= CpuMpData
->AddressMap
.RelocateApLoopFuncSize
;
327 ApSafeBufferSize
+= CpuMpData
->CpuCount
* AP_SAFE_STACK_SIZE
;
329 Address
= BASE_4GB
- 1;
330 Status
= gBS
->AllocatePages (
332 EfiReservedMemoryType
,
333 EFI_SIZE_TO_PAGES (ApSafeBufferSize
),
336 ASSERT_EFI_ERROR (Status
);
337 mReservedApLoopFunc
= (VOID
*) (UINTN
) Address
;
338 ASSERT (mReservedApLoopFunc
!= NULL
);
339 mReservedTopOfApStack
= (UINTN
) Address
+ EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (ApSafeBufferSize
));
340 ASSERT ((mReservedTopOfApStack
& (UINTN
)(CPU_STACK_ALIGNMENT
- 1)) == 0);
343 CpuMpData
->AddressMap
.RelocateApLoopFuncAddress
,
344 CpuMpData
->AddressMap
.RelocateApLoopFuncSize
347 Status
= gBS
->CreateEvent (
348 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
354 ASSERT_EFI_ERROR (Status
);
357 // Set timer to check all APs status.
359 Status
= gBS
->SetTimer (
364 ASSERT_EFI_ERROR (Status
);
366 Status
= gBS
->CreateEvent (
367 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
369 MpInitChangeApLoopCallback
,
371 &mMpInitExitBootServicesEvent
373 ASSERT_EFI_ERROR (Status
);
375 Status
= gBS
->CreateEventEx (
378 MpInitChangeApLoopCallback
,
380 &gEfiEventLegacyBootGuid
,
383 ASSERT_EFI_ERROR (Status
);
387 This service executes a caller provided function on all enabled APs.
389 @param[in] Procedure A pointer to the function to be run on
390 enabled APs of the system. See type
392 @param[in] SingleThread If TRUE, then all the enabled APs execute
393 the function specified by Procedure one by
394 one, in ascending order of processor handle
395 number. If FALSE, then all the enabled APs
396 execute the function specified by Procedure
398 @param[in] WaitEvent The event created by the caller with CreateEvent()
399 service. If it is NULL, then execute in
400 blocking mode. BSP waits until all APs finish
401 or TimeoutInMicroSeconds expires. If it's
402 not NULL, then execute in non-blocking mode.
403 BSP requests the function specified by
404 Procedure to be started on all the enabled
405 APs, and go on executing immediately. If
406 all return from Procedure, or TimeoutInMicroSeconds
407 expires, this event is signaled. The BSP
408 can use the CheckEvent() or WaitForEvent()
409 services to check the state of event. Type
410 EFI_EVENT is defined in CreateEvent() in
411 the Unified Extensible Firmware Interface
413 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
414 APs to return from Procedure, either for
415 blocking or non-blocking mode. Zero means
416 infinity. If the timeout expires before
417 all APs return from Procedure, then Procedure
418 on the failed APs is terminated. All enabled
419 APs are available for next function assigned
420 by MpInitLibStartupAllAPs() or
421 MPInitLibStartupThisAP().
422 If the timeout expires in blocking mode,
423 BSP returns EFI_TIMEOUT. If the timeout
424 expires in non-blocking mode, WaitEvent
425 is signaled with SignalEvent().
426 @param[in] ProcedureArgument The parameter passed into Procedure for
428 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
429 if all APs finish successfully, then its
430 content is set to NULL. If not all APs
431 finish before timeout expires, then its
432 content is set to address of the buffer
433 holding handle numbers of the failed APs.
434 The buffer is allocated by MP Initialization
435 library, and it's the caller's responsibility to
436 free the buffer with FreePool() service.
437 In blocking mode, it is ready for consumption
438 when the call returns. In non-blocking mode,
439 it is ready when WaitEvent is signaled. The
440 list of failed CPU is terminated by
443 @retval EFI_SUCCESS In blocking mode, all APs have finished before
445 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
447 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
448 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
450 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
452 @retval EFI_DEVICE_ERROR Caller processor is AP.
453 @retval EFI_NOT_STARTED No enabled APs exist in the system.
454 @retval EFI_NOT_READY Any enabled APs are busy.
455 @retval EFI_NOT_READY MP Initialize Library is not initialized.
456 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
457 all enabled APs have finished.
458 @retval EFI_INVALID_PARAMETER Procedure is NULL.
463 MpInitLibStartupAllAPs (
464 IN EFI_AP_PROCEDURE Procedure
,
465 IN BOOLEAN SingleThread
,
466 IN EFI_EVENT WaitEvent OPTIONAL
,
467 IN UINTN TimeoutInMicroseconds
,
468 IN VOID
*ProcedureArgument OPTIONAL
,
469 OUT UINTN
**FailedCpuList OPTIONAL
475 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
477 mStopCheckAllApsStatus
= TRUE
;
479 Status
= StartupAllAPsWorker (
483 TimeoutInMicroseconds
,
489 // Start checkAllApsStatus
491 mStopCheckAllApsStatus
= FALSE
;
497 This service lets the caller get one enabled AP to execute a caller-provided
500 @param[in] Procedure A pointer to the function to be run on the
501 designated AP of the system. See type
503 @param[in] ProcessorNumber The handle number of the AP. The range is
504 from 0 to the total number of logical
505 processors minus 1. The total number of
506 logical processors can be retrieved by
507 MpInitLibGetNumberOfProcessors().
508 @param[in] WaitEvent The event created by the caller with CreateEvent()
509 service. If it is NULL, then execute in
510 blocking mode. BSP waits until this AP finish
511 or TimeoutInMicroSeconds expires. If it's
512 not NULL, then execute in non-blocking mode.
513 BSP requests the function specified by
514 Procedure to be started on this AP,
515 and go on executing immediately. If this AP
516 return from Procedure or TimeoutInMicroSeconds
517 expires, this event is signaled. The BSP
518 can use the CheckEvent() or WaitForEvent()
519 services to check the state of event. Type
520 EFI_EVENT is defined in CreateEvent() in
521 the Unified Extensible Firmware Interface
523 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
524 this AP to finish this Procedure, either for
525 blocking or non-blocking mode. Zero means
526 infinity. If the timeout expires before
527 this AP returns from Procedure, then Procedure
528 on the AP is terminated. The
529 AP is available for next function assigned
530 by MpInitLibStartupAllAPs() or
531 MpInitLibStartupThisAP().
532 If the timeout expires in blocking mode,
533 BSP returns EFI_TIMEOUT. If the timeout
534 expires in non-blocking mode, WaitEvent
535 is signaled with SignalEvent().
536 @param[in] ProcedureArgument The parameter passed into Procedure on the
538 @param[out] Finished If NULL, this parameter is ignored. In
539 blocking mode, this parameter is ignored.
540 In non-blocking mode, if AP returns from
541 Procedure before the timeout expires, its
542 content is set to TRUE. Otherwise, the
543 value is set to FALSE. The caller can
544 determine if the AP returned from Procedure
545 by evaluating this value.
547 @retval EFI_SUCCESS In blocking mode, specified AP finished before
549 @retval EFI_SUCCESS In non-blocking mode, the function has been
550 dispatched to specified AP.
551 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
552 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
554 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
556 @retval EFI_DEVICE_ERROR The calling processor is an AP.
557 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
558 the specified AP has finished.
559 @retval EFI_NOT_READY The specified AP is busy.
560 @retval EFI_NOT_READY MP Initialize Library is not initialized.
561 @retval EFI_NOT_FOUND The processor with the handle specified by
562 ProcessorNumber does not exist.
563 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
564 @retval EFI_INVALID_PARAMETER Procedure is NULL.
569 MpInitLibStartupThisAP (
570 IN EFI_AP_PROCEDURE Procedure
,
571 IN UINTN ProcessorNumber
,
572 IN EFI_EVENT WaitEvent OPTIONAL
,
573 IN UINTN TimeoutInMicroseconds
,
574 IN VOID
*ProcedureArgument OPTIONAL
,
575 OUT BOOLEAN
*Finished OPTIONAL
581 // temporarily stop checkAllApsStatus for avoid resource dead-lock.
583 mStopCheckAllApsStatus
= TRUE
;
585 Status
= StartupThisAPWorker (
589 TimeoutInMicroseconds
,
594 mStopCheckAllApsStatus
= FALSE
;
600 This service switches the requested AP to be the BSP from that point onward.
601 This service changes the BSP for all purposes. This call can only be performed
604 @param[in] ProcessorNumber The handle number of AP that is to become the new
605 BSP. The range is from 0 to the total number of
606 logical processors minus 1. The total number of
607 logical processors can be retrieved by
608 MpInitLibGetNumberOfProcessors().
609 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
610 enabled AP. Otherwise, it will be disabled.
612 @retval EFI_SUCCESS BSP successfully switched.
613 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
614 this service returning.
615 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
616 @retval EFI_DEVICE_ERROR The calling processor is an AP.
617 @retval EFI_NOT_FOUND The processor with the handle specified by
618 ProcessorNumber does not exist.
619 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
621 @retval EFI_NOT_READY The specified AP is busy.
622 @retval EFI_NOT_READY MP Initialize Library is not initialized.
628 IN UINTN ProcessorNumber
,
629 IN BOOLEAN EnableOldBSP
633 BOOLEAN OldInterruptState
;
636 // Before send both BSP and AP to a procedure to exchange their roles,
637 // interrupt must be disabled. This is because during the exchange role
638 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
639 // be corrupted, since interrupt return address will be pushed to stack
642 OldInterruptState
= SaveAndDisableInterrupts ();
645 // Mask LINT0 & LINT1 for the old BSP
647 DisableLvtInterrupts ();
649 Status
= SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
652 // Restore interrupt state.
654 SetInterruptState (OldInterruptState
);
660 This service lets the caller enable or disable an AP from this point onward.
661 This service may only be called from the BSP.
663 @param[in] ProcessorNumber The handle number of AP.
664 The range is from 0 to the total number of
665 logical processors minus 1. The total number of
666 logical processors can be retrieved by
667 MpInitLibGetNumberOfProcessors().
668 @param[in] EnableAP Specifies the new state for the processor for
669 enabled, FALSE for disabled.
670 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
671 the new health status of the AP. This flag
672 corresponds to StatusFlag defined in
673 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
674 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
675 bits are ignored. If it is NULL, this parameter
678 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
679 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
680 prior to this service returning.
681 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
682 @retval EFI_DEVICE_ERROR The calling processor is an AP.
683 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
685 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
686 @retval EFI_NOT_READY MP Initialize Library is not initialized.
691 MpInitLibEnableDisableAP (
692 IN UINTN ProcessorNumber
,
694 IN UINT32
*HealthFlag OPTIONAL
698 BOOLEAN TempStopCheckState
;
700 TempStopCheckState
= FALSE
;
702 // temporarily stop checkAllAPsStatus for initialize parameters.
704 if (!mStopCheckAllApsStatus
) {
705 mStopCheckAllApsStatus
= TRUE
;
706 TempStopCheckState
= TRUE
;
709 Status
= EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);
711 if (TempStopCheckState
) {
712 mStopCheckAllApsStatus
= FALSE
;