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))
22 CPU_MP_DATA
*mCpuMpData
= NULL
;
23 EFI_EVENT mCheckAllApsEvent
= NULL
;
24 EFI_EVENT mMpInitExitBootServicesEvent
= NULL
;
25 EFI_EVENT mLegacyBootEvent
= NULL
;
26 volatile BOOLEAN mStopCheckAllApsStatus
= TRUE
;
27 VOID
*mReservedApLoopFunc
= NULL
;
30 Get the pointer to CPU MP Data structure.
32 @return The pointer to CPU MP Data structure.
39 ASSERT (mCpuMpData
!= NULL
);
44 Save the pointer to CPU MP Data structure.
46 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
50 IN CPU_MP_DATA
*CpuMpData
53 mCpuMpData
= CpuMpData
;
57 Allocate reset vector buffer.
59 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
63 IN OUT CPU_MP_DATA
*CpuMpData
67 UINTN ApResetVectorSize
;
68 EFI_PHYSICAL_ADDRESS StartAddress
;
70 if (CpuMpData
->SaveRestoreFlag
) {
71 BackupAndPrepareWakeupBuffer (CpuMpData
);
73 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
74 sizeof (MP_CPU_EXCHANGE_INFO
);
76 StartAddress
= BASE_1MB
;
77 Status
= gBS
->AllocatePages (
80 EFI_SIZE_TO_PAGES (ApResetVectorSize
),
83 ASSERT_EFI_ERROR (Status
);
85 CpuMpData
->WakeupBuffer
= (UINTN
) StartAddress
;
86 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
87 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
89 // copy AP reset code in it
92 (VOID
*) CpuMpData
->WakeupBuffer
,
93 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
94 CpuMpData
->AddressMap
.RendezvousFunnelSize
100 Free AP reset vector buffer.
102 @param[in] CpuMpData The pointer to CPU MP Data structure.
106 IN CPU_MP_DATA
*CpuMpData
110 UINTN ApResetVectorSize
;
112 if (CpuMpData
->SaveRestoreFlag
) {
113 RestoreWakeupBuffer (CpuMpData
);
115 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
116 sizeof (MP_CPU_EXCHANGE_INFO
);
117 Status
= gBS
->FreePages(
118 (EFI_PHYSICAL_ADDRESS
)CpuMpData
->WakeupBuffer
,
119 EFI_SIZE_TO_PAGES (ApResetVectorSize
)
121 ASSERT_EFI_ERROR (Status
);
126 Checks APs status and updates APs status if needed.
130 CheckAndUpdateApsStatus (
134 UINTN ProcessorNumber
;
136 CPU_MP_DATA
*CpuMpData
;
138 CpuMpData
= GetCpuMpData ();
141 // First, check whether pending StartupAllAPs() exists.
143 if (CpuMpData
->WaitEvent
!= NULL
) {
145 Status
= CheckAllAPs ();
147 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.
149 if (Status
!= EFI_NOT_READY
) {
150 Status
= gBS
->SignalEvent (CpuMpData
->WaitEvent
);
151 CpuMpData
->WaitEvent
= NULL
;
156 // Second, check whether pending StartupThisAPs() callings exist.
158 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
160 if (CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
== NULL
) {
164 Status
= CheckThisAP (ProcessorNumber
);
166 if (Status
!= EFI_NOT_READY
) {
167 gBS
->SignalEvent (CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
);
168 CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
= NULL
;
174 Checks APs' status periodically.
176 This function is triggered by timer periodically to check the
177 state of APs for StartupAllAPs() and StartupThisAP() executed
178 in non-blocking mode.
180 @param[in] Event Event triggered.
181 @param[in] Context Parameter passed with the event.
192 // If CheckApsStatus() is not stopped, otherwise return immediately.
194 if (!mStopCheckAllApsStatus
) {
195 CheckAndUpdateApsStatus ();
200 Get Protected mode code segment from current GDT table.
202 @return Protected mode code segment value.
209 IA32_DESCRIPTOR GdtrDesc
;
210 IA32_SEGMENT_DESCRIPTOR
*GdtEntry
;
215 AsmReadGdtr (&GdtrDesc
);
216 GdtEntryCount
= (GdtrDesc
.Limit
+ 1) / sizeof (IA32_SEGMENT_DESCRIPTOR
);
217 GdtEntry
= (IA32_SEGMENT_DESCRIPTOR
*) GdtrDesc
.Base
;
218 for (Index
= 0; Index
< GdtEntryCount
; Index
++) {
219 if (GdtEntry
->Bits
.L
== 0) {
220 if (GdtEntry
->Bits
.Type
> 8 && GdtEntry
->Bits
.L
== 0) {
226 ASSERT (Index
!= -1);
233 @param[in, out] Buffer Pointer to private data buffer.
241 CPU_MP_DATA
*CpuMpData
;
242 BOOLEAN MwaitSupport
;
243 ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc
;
245 CpuMpData
= GetCpuMpData ();
246 MwaitSupport
= IsMwaitSupport ();
247 AsmRelocateApLoopFunc
= (ASM_RELOCATE_AP_LOOP
) (UINTN
) mReservedApLoopFunc
;
248 AsmRelocateApLoopFunc (MwaitSupport
, CpuMpData
->ApTargetCState
, CpuMpData
->PmCodeSegment
);
250 // It should never reach here
256 Callback function for ExitBootServices.
258 @param[in] Event Event whose notification function is being invoked.
259 @param[in] Context The pointer to the notification function's context,
260 which is implementation-dependent.
265 MpInitChangeApLoopCallback (
270 CPU_MP_DATA
*CpuMpData
;
272 CpuMpData
= GetCpuMpData ();
273 CpuMpData
->SaveRestoreFlag
= TRUE
;
274 CpuMpData
->PmCodeSegment
= GetProtectedModeCS ();
275 CpuMpData
->ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
276 WakeUpAP (CpuMpData
, TRUE
, 0, RelocateApLoop
, NULL
);
277 DEBUG ((DEBUG_INFO
, "%a() done!\n", __FUNCTION__
));
281 Initialize global data for MP support.
283 @param[in] CpuMpData The pointer to CPU MP Data structure.
287 IN CPU_MP_DATA
*CpuMpData
291 EFI_PHYSICAL_ADDRESS Address
;
293 SaveCpuMpData (CpuMpData
);
295 if (CpuMpData
->CpuCount
== 1) {
297 // If only BSP exists, return
303 // Avoid APs access invalid buffer data which allocated by BootServices,
304 // so we will allocate reserved data for AP loop code. We also need to
305 // allocate this buffer below 4GB due to APs may be transferred to 32bit
306 // protected mode on long mode DXE.
307 // Allocating it in advance since memory services are not available in
308 // Exit Boot Services callback function.
310 Address
= BASE_4GB
- 1;
311 Status
= gBS
->AllocatePages (
313 EfiReservedMemoryType
,
314 EFI_SIZE_TO_PAGES (sizeof (CpuMpData
->AddressMap
.RelocateApLoopFuncSize
)),
317 ASSERT_EFI_ERROR (Status
);
318 mReservedApLoopFunc
= (VOID
*) (UINTN
) Address
;
319 ASSERT (mReservedApLoopFunc
!= NULL
);
322 CpuMpData
->AddressMap
.RelocateApLoopFuncAddress
,
323 CpuMpData
->AddressMap
.RelocateApLoopFuncSize
326 Status
= gBS
->CreateEvent (
327 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
333 ASSERT_EFI_ERROR (Status
);
336 // Set timer to check all APs status.
338 Status
= gBS
->SetTimer (
343 ASSERT_EFI_ERROR (Status
);
345 Status
= gBS
->CreateEvent (
346 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
348 MpInitChangeApLoopCallback
,
350 &mMpInitExitBootServicesEvent
352 ASSERT_EFI_ERROR (Status
);
354 Status
= gBS
->CreateEventEx (
357 MpInitChangeApLoopCallback
,
359 &gEfiEventLegacyBootGuid
,
362 ASSERT_EFI_ERROR (Status
);
366 This service executes a caller provided function on all enabled APs.
368 @param[in] Procedure A pointer to the function to be run on
369 enabled APs of the system. See type
371 @param[in] SingleThread If TRUE, then all the enabled APs execute
372 the function specified by Procedure one by
373 one, in ascending order of processor handle
374 number. If FALSE, then all the enabled APs
375 execute the function specified by Procedure
377 @param[in] WaitEvent The event created by the caller with CreateEvent()
378 service. If it is NULL, then execute in
379 blocking mode. BSP waits until all APs finish
380 or TimeoutInMicroSeconds expires. If it's
381 not NULL, then execute in non-blocking mode.
382 BSP requests the function specified by
383 Procedure to be started on all the enabled
384 APs, and go on executing immediately. If
385 all return from Procedure, or TimeoutInMicroSeconds
386 expires, this event is signaled. The BSP
387 can use the CheckEvent() or WaitForEvent()
388 services to check the state of event. Type
389 EFI_EVENT is defined in CreateEvent() in
390 the Unified Extensible Firmware Interface
392 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
393 APs to return from Procedure, either for
394 blocking or non-blocking mode. Zero means
395 infinity. If the timeout expires before
396 all APs return from Procedure, then Procedure
397 on the failed APs is terminated. All enabled
398 APs are available for next function assigned
399 by MpInitLibStartupAllAPs() or
400 MPInitLibStartupThisAP().
401 If the timeout expires in blocking mode,
402 BSP returns EFI_TIMEOUT. If the timeout
403 expires in non-blocking mode, WaitEvent
404 is signaled with SignalEvent().
405 @param[in] ProcedureArgument The parameter passed into Procedure for
407 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
408 if all APs finish successfully, then its
409 content is set to NULL. If not all APs
410 finish before timeout expires, then its
411 content is set to address of the buffer
412 holding handle numbers of the failed APs.
413 The buffer is allocated by MP Initialization
414 library, and it's the caller's responsibility to
415 free the buffer with FreePool() service.
416 In blocking mode, it is ready for consumption
417 when the call returns. In non-blocking mode,
418 it is ready when WaitEvent is signaled. The
419 list of failed CPU is terminated by
422 @retval EFI_SUCCESS In blocking mode, all APs have finished before
424 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
426 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
427 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
429 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
431 @retval EFI_DEVICE_ERROR Caller processor is AP.
432 @retval EFI_NOT_STARTED No enabled APs exist in the system.
433 @retval EFI_NOT_READY Any enabled APs are busy.
434 @retval EFI_NOT_READY MP Initialize Library is not initialized.
435 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
436 all enabled APs have finished.
437 @retval EFI_INVALID_PARAMETER Procedure is NULL.
442 MpInitLibStartupAllAPs (
443 IN EFI_AP_PROCEDURE Procedure
,
444 IN BOOLEAN SingleThread
,
445 IN EFI_EVENT WaitEvent OPTIONAL
,
446 IN UINTN TimeoutInMicroseconds
,
447 IN VOID
*ProcedureArgument OPTIONAL
,
448 OUT UINTN
**FailedCpuList OPTIONAL
454 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
456 mStopCheckAllApsStatus
= TRUE
;
458 Status
= StartupAllAPsWorker (
462 TimeoutInMicroseconds
,
468 // Start checkAllApsStatus
470 mStopCheckAllApsStatus
= FALSE
;
476 This service lets the caller get one enabled AP to execute a caller-provided
479 @param[in] Procedure A pointer to the function to be run on the
480 designated AP of the system. See type
482 @param[in] ProcessorNumber The handle number of the AP. The range is
483 from 0 to the total number of logical
484 processors minus 1. The total number of
485 logical processors can be retrieved by
486 MpInitLibGetNumberOfProcessors().
487 @param[in] WaitEvent The event created by the caller with CreateEvent()
488 service. If it is NULL, then execute in
489 blocking mode. BSP waits until this AP finish
490 or TimeoutInMicroSeconds expires. If it's
491 not NULL, then execute in non-blocking mode.
492 BSP requests the function specified by
493 Procedure to be started on this AP,
494 and go on executing immediately. If this AP
495 return from Procedure or TimeoutInMicroSeconds
496 expires, this event is signaled. The BSP
497 can use the CheckEvent() or WaitForEvent()
498 services to check the state of event. Type
499 EFI_EVENT is defined in CreateEvent() in
500 the Unified Extensible Firmware Interface
502 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
503 this AP to finish this Procedure, either for
504 blocking or non-blocking mode. Zero means
505 infinity. If the timeout expires before
506 this AP returns from Procedure, then Procedure
507 on the AP is terminated. The
508 AP is available for next function assigned
509 by MpInitLibStartupAllAPs() or
510 MpInitLibStartupThisAP().
511 If the timeout expires in blocking mode,
512 BSP returns EFI_TIMEOUT. If the timeout
513 expires in non-blocking mode, WaitEvent
514 is signaled with SignalEvent().
515 @param[in] ProcedureArgument The parameter passed into Procedure on the
517 @param[out] Finished If NULL, this parameter is ignored. In
518 blocking mode, this parameter is ignored.
519 In non-blocking mode, if AP returns from
520 Procedure before the timeout expires, its
521 content is set to TRUE. Otherwise, the
522 value is set to FALSE. The caller can
523 determine if the AP returned from Procedure
524 by evaluating this value.
526 @retval EFI_SUCCESS In blocking mode, specified AP finished before
528 @retval EFI_SUCCESS In non-blocking mode, the function has been
529 dispatched to specified AP.
530 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
531 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
533 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
535 @retval EFI_DEVICE_ERROR The calling processor is an AP.
536 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
537 the specified AP has finished.
538 @retval EFI_NOT_READY The specified AP is busy.
539 @retval EFI_NOT_READY MP Initialize Library is not initialized.
540 @retval EFI_NOT_FOUND The processor with the handle specified by
541 ProcessorNumber does not exist.
542 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
543 @retval EFI_INVALID_PARAMETER Procedure is NULL.
548 MpInitLibStartupThisAP (
549 IN EFI_AP_PROCEDURE Procedure
,
550 IN UINTN ProcessorNumber
,
551 IN EFI_EVENT WaitEvent OPTIONAL
,
552 IN UINTN TimeoutInMicroseconds
,
553 IN VOID
*ProcedureArgument OPTIONAL
,
554 OUT BOOLEAN
*Finished OPTIONAL
560 // temporarily stop checkAllApsStatus for avoid resource dead-lock.
562 mStopCheckAllApsStatus
= TRUE
;
564 Status
= StartupThisAPWorker (
568 TimeoutInMicroseconds
,
573 mStopCheckAllApsStatus
= FALSE
;
579 This service switches the requested AP to be the BSP from that point onward.
580 This service changes the BSP for all purposes. This call can only be performed
583 @param[in] ProcessorNumber The handle number of AP that is to become the new
584 BSP. The range is from 0 to the total number of
585 logical processors minus 1. The total number of
586 logical processors can be retrieved by
587 MpInitLibGetNumberOfProcessors().
588 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
589 enabled AP. Otherwise, it will be disabled.
591 @retval EFI_SUCCESS BSP successfully switched.
592 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
593 this service returning.
594 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
595 @retval EFI_DEVICE_ERROR The calling processor is an AP.
596 @retval EFI_NOT_FOUND The processor with the handle specified by
597 ProcessorNumber does not exist.
598 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
600 @retval EFI_NOT_READY The specified AP is busy.
601 @retval EFI_NOT_READY MP Initialize Library is not initialized.
607 IN UINTN ProcessorNumber
,
608 IN BOOLEAN EnableOldBSP
612 BOOLEAN OldInterruptState
;
615 // Before send both BSP and AP to a procedure to exchange their roles,
616 // interrupt must be disabled. This is because during the exchange role
617 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
618 // be corrupted, since interrupt return address will be pushed to stack
621 OldInterruptState
= SaveAndDisableInterrupts ();
624 // Mask LINT0 & LINT1 for the old BSP
626 DisableLvtInterrupts ();
628 Status
= SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
631 // Restore interrupt state.
633 SetInterruptState (OldInterruptState
);
639 This service lets the caller enable or disable an AP from this point onward.
640 This service may only be called from the BSP.
642 @param[in] ProcessorNumber The handle number of AP.
643 The range is from 0 to the total number of
644 logical processors minus 1. The total number of
645 logical processors can be retrieved by
646 MpInitLibGetNumberOfProcessors().
647 @param[in] EnableAP Specifies the new state for the processor for
648 enabled, FALSE for disabled.
649 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
650 the new health status of the AP. This flag
651 corresponds to StatusFlag defined in
652 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
653 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
654 bits are ignored. If it is NULL, this parameter
657 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
658 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
659 prior to this service returning.
660 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
661 @retval EFI_DEVICE_ERROR The calling processor is an AP.
662 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
664 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
665 @retval EFI_NOT_READY MP Initialize Library is not initialized.
670 MpInitLibEnableDisableAP (
671 IN UINTN ProcessorNumber
,
673 IN UINT32
*HealthFlag OPTIONAL
677 BOOLEAN TempStopCheckState
;
679 TempStopCheckState
= FALSE
;
681 // temporarily stop checkAllAPsStatus for initialize parameters.
683 if (!mStopCheckAllApsStatus
) {
684 mStopCheckAllApsStatus
= TRUE
;
685 TempStopCheckState
= TRUE
;
688 Status
= EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);
690 if (TempStopCheckState
) {
691 mStopCheckAllApsStatus
= FALSE
;