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 volatile BOOLEAN mStopCheckAllApsStatus
= TRUE
;
26 VOID
*mReservedApLoopFunc
= NULL
;
29 Get the pointer to CPU MP Data structure.
31 @return The pointer to CPU MP Data structure.
38 ASSERT (mCpuMpData
!= NULL
);
43 Save the pointer to CPU MP Data structure.
45 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
49 IN CPU_MP_DATA
*CpuMpData
52 mCpuMpData
= CpuMpData
;
56 Allocate reset vector buffer.
58 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
62 IN OUT CPU_MP_DATA
*CpuMpData
66 UINTN ApResetVectorSize
;
67 EFI_PHYSICAL_ADDRESS StartAddress
;
69 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
70 sizeof (MP_CPU_EXCHANGE_INFO
);
72 StartAddress
= BASE_1MB
;
73 Status
= gBS
->AllocatePages (
76 EFI_SIZE_TO_PAGES (ApResetVectorSize
),
79 ASSERT_EFI_ERROR (Status
);
81 CpuMpData
->WakeupBuffer
= (UINTN
) StartAddress
;
82 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
83 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
85 // copy AP reset code in it
88 (VOID
*) CpuMpData
->WakeupBuffer
,
89 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
90 CpuMpData
->AddressMap
.RendezvousFunnelSize
95 Free AP reset vector buffer.
97 @param[in] CpuMpData The pointer to CPU MP Data structure.
101 IN CPU_MP_DATA
*CpuMpData
105 UINTN ApResetVectorSize
;
106 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
107 sizeof (MP_CPU_EXCHANGE_INFO
);
108 Status
= gBS
->FreePages(
109 (EFI_PHYSICAL_ADDRESS
)CpuMpData
->WakeupBuffer
,
110 EFI_SIZE_TO_PAGES (ApResetVectorSize
)
112 ASSERT_EFI_ERROR (Status
);
116 Checks APs status and updates APs status if needed.
120 CheckAndUpdateApsStatus (
124 UINTN ProcessorNumber
;
126 CPU_MP_DATA
*CpuMpData
;
128 CpuMpData
= GetCpuMpData ();
131 // First, check whether pending StartupAllAPs() exists.
133 if (CpuMpData
->WaitEvent
!= NULL
) {
135 Status
= CheckAllAPs ();
137 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.
139 if (Status
!= EFI_NOT_READY
) {
140 Status
= gBS
->SignalEvent (CpuMpData
->WaitEvent
);
141 CpuMpData
->WaitEvent
= NULL
;
146 // Second, check whether pending StartupThisAPs() callings exist.
148 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
150 if (CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
== NULL
) {
154 Status
= CheckThisAP (ProcessorNumber
);
156 if (Status
!= EFI_NOT_READY
) {
157 gBS
->SignalEvent (CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
);
158 CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
= NULL
;
164 Checks APs' status periodically.
166 This function is triggerred by timer perodically to check the
167 state of APs for StartupAllAPs() and StartupThisAP() executed
168 in non-blocking mode.
170 @param[in] Event Event triggered.
171 @param[in] Context Parameter passed with the event.
182 // If CheckApsStatus() is not stopped, otherwise return immediately.
184 if (!mStopCheckAllApsStatus
) {
185 CheckAndUpdateApsStatus ();
190 Get Protected mode code segment from current GDT table.
192 @return Protected mode code segment value.
199 IA32_DESCRIPTOR GdtrDesc
;
200 IA32_SEGMENT_DESCRIPTOR
*GdtEntry
;
205 AsmReadGdtr (&GdtrDesc
);
206 GdtEntryCount
= (GdtrDesc
.Limit
+ 1) / sizeof (IA32_SEGMENT_DESCRIPTOR
);
207 GdtEntry
= (IA32_SEGMENT_DESCRIPTOR
*) GdtrDesc
.Base
;
208 for (Index
= 0; Index
< GdtEntryCount
; Index
++) {
209 if (GdtEntry
->Bits
.L
== 0) {
210 if (GdtEntry
->Bits
.Type
> 8 && GdtEntry
->Bits
.L
== 0) {
216 ASSERT (Index
!= -1);
223 @param[in, out] Buffer Pointer to private data buffer.
231 CPU_MP_DATA
*CpuMpData
;
232 BOOLEAN MwaitSupport
;
233 ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc
;
235 CpuMpData
= GetCpuMpData ();
236 MwaitSupport
= IsMwaitSupport ();
237 AsmRelocateApLoopFunc
= (ASM_RELOCATE_AP_LOOP
) (UINTN
) Buffer
;
238 AsmRelocateApLoopFunc (MwaitSupport
, CpuMpData
->ApTargetCState
, CpuMpData
->PmCodeSegment
);
240 // It should never reach here
246 Callback function for ExitBootServices.
248 @param[in] Event Event whose notification function is being invoked.
249 @param[in] Context The pointer to the notification function's context,
250 which is implementation-dependent.
255 MpInitExitBootServicesCallback (
260 CPU_MP_DATA
*CpuMpData
;
262 CpuMpData
= GetCpuMpData ();
263 CpuMpData
->PmCodeSegment
= GetProtectedModeCS ();
264 CpuMpData
->ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
265 WakeUpAP (CpuMpData
, TRUE
, 0, RelocateApLoop
, mReservedApLoopFunc
);
266 DEBUG ((DEBUG_INFO
, "MpInitExitBootServicesCallback() done!\n"));
270 Initialize global data for MP support.
272 @param[in] CpuMpData The pointer to CPU MP Data structure.
276 IN CPU_MP_DATA
*CpuMpData
281 SaveCpuMpData (CpuMpData
);
284 // Avoid APs access invalid buff data which allocated by BootServices,
285 // so we will allocate reserved data for AP loop code.
286 // Allocating it in advance since memory services are not available in
287 // Exit Boot Services callback function.
289 mReservedApLoopFunc
= AllocateReservedCopyPool (
290 CpuMpData
->AddressMap
.RelocateApLoopFuncSize
,
291 CpuMpData
->AddressMap
.RelocateApLoopFuncAddress
293 ASSERT (mReservedApLoopFunc
!= NULL
);
295 Status
= gBS
->CreateEvent (
296 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
302 ASSERT_EFI_ERROR (Status
);
305 // Set timer to check all APs status.
307 Status
= gBS
->SetTimer (
312 ASSERT_EFI_ERROR (Status
);
313 Status
= gBS
->CreateEvent (
314 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
316 MpInitExitBootServicesCallback
,
318 &mMpInitExitBootServicesEvent
320 ASSERT_EFI_ERROR (Status
);
324 This service executes a caller provided function on all enabled APs.
326 @param[in] Procedure A pointer to the function to be run on
327 enabled APs of the system. See type
329 @param[in] SingleThread If TRUE, then all the enabled APs execute
330 the function specified by Procedure one by
331 one, in ascending order of processor handle
332 number. If FALSE, then all the enabled APs
333 execute the function specified by Procedure
335 @param[in] WaitEvent The event created by the caller with CreateEvent()
336 service. If it is NULL, then execute in
337 blocking mode. BSP waits until all APs finish
338 or TimeoutInMicroSeconds expires. If it's
339 not NULL, then execute in non-blocking mode.
340 BSP requests the function specified by
341 Procedure to be started on all the enabled
342 APs, and go on executing immediately. If
343 all return from Procedure, or TimeoutInMicroSeconds
344 expires, this event is signaled. The BSP
345 can use the CheckEvent() or WaitForEvent()
346 services to check the state of event. Type
347 EFI_EVENT is defined in CreateEvent() in
348 the Unified Extensible Firmware Interface
350 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
351 APs to return from Procedure, either for
352 blocking or non-blocking mode. Zero means
353 infinity. If the timeout expires before
354 all APs return from Procedure, then Procedure
355 on the failed APs is terminated. All enabled
356 APs are available for next function assigned
357 by MpInitLibStartupAllAPs() or
358 MPInitLibStartupThisAP().
359 If the timeout expires in blocking mode,
360 BSP returns EFI_TIMEOUT. If the timeout
361 expires in non-blocking mode, WaitEvent
362 is signaled with SignalEvent().
363 @param[in] ProcedureArgument The parameter passed into Procedure for
365 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
366 if all APs finish successfully, then its
367 content is set to NULL. If not all APs
368 finish before timeout expires, then its
369 content is set to address of the buffer
370 holding handle numbers of the failed APs.
371 The buffer is allocated by MP Initialization
372 library, and it's the caller's responsibility to
373 free the buffer with FreePool() service.
374 In blocking mode, it is ready for consumption
375 when the call returns. In non-blocking mode,
376 it is ready when WaitEvent is signaled. The
377 list of failed CPU is terminated by
380 @retval EFI_SUCCESS In blocking mode, all APs have finished before
382 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
384 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
385 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
387 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
389 @retval EFI_DEVICE_ERROR Caller processor is AP.
390 @retval EFI_NOT_STARTED No enabled APs exist in the system.
391 @retval EFI_NOT_READY Any enabled APs are busy.
392 @retval EFI_NOT_READY MP Initialize Library is not initialized.
393 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
394 all enabled APs have finished.
395 @retval EFI_INVALID_PARAMETER Procedure is NULL.
400 MpInitLibStartupAllAPs (
401 IN EFI_AP_PROCEDURE Procedure
,
402 IN BOOLEAN SingleThread
,
403 IN EFI_EVENT WaitEvent OPTIONAL
,
404 IN UINTN TimeoutInMicroseconds
,
405 IN VOID
*ProcedureArgument OPTIONAL
,
406 OUT UINTN
**FailedCpuList OPTIONAL
412 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
414 mStopCheckAllApsStatus
= TRUE
;
416 Status
= StartupAllAPsWorker (
420 TimeoutInMicroseconds
,
426 // Start checkAllApsStatus
428 mStopCheckAllApsStatus
= FALSE
;
434 This service lets the caller get one enabled AP to execute a caller-provided
437 @param[in] Procedure A pointer to the function to be run on the
438 designated AP of the system. See type
440 @param[in] ProcessorNumber The handle number of the AP. The range is
441 from 0 to the total number of logical
442 processors minus 1. The total number of
443 logical processors can be retrieved by
444 MpInitLibGetNumberOfProcessors().
445 @param[in] WaitEvent The event created by the caller with CreateEvent()
446 service. If it is NULL, then execute in
447 blocking mode. BSP waits until this AP finish
448 or TimeoutInMicroSeconds expires. If it's
449 not NULL, then execute in non-blocking mode.
450 BSP requests the function specified by
451 Procedure to be started on this AP,
452 and go on executing immediately. If this AP
453 return from Procedure or TimeoutInMicroSeconds
454 expires, this event is signaled. The BSP
455 can use the CheckEvent() or WaitForEvent()
456 services to check the state of event. Type
457 EFI_EVENT is defined in CreateEvent() in
458 the Unified Extensible Firmware Interface
460 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
461 this AP to finish this Procedure, either for
462 blocking or non-blocking mode. Zero means
463 infinity. If the timeout expires before
464 this AP returns from Procedure, then Procedure
465 on the AP is terminated. The
466 AP is available for next function assigned
467 by MpInitLibStartupAllAPs() or
468 MpInitLibStartupThisAP().
469 If the timeout expires in blocking mode,
470 BSP returns EFI_TIMEOUT. If the timeout
471 expires in non-blocking mode, WaitEvent
472 is signaled with SignalEvent().
473 @param[in] ProcedureArgument The parameter passed into Procedure on the
475 @param[out] Finished If NULL, this parameter is ignored. In
476 blocking mode, this parameter is ignored.
477 In non-blocking mode, if AP returns from
478 Procedure before the timeout expires, its
479 content is set to TRUE. Otherwise, the
480 value is set to FALSE. The caller can
481 determine if the AP returned from Procedure
482 by evaluating this value.
484 @retval EFI_SUCCESS In blocking mode, specified AP finished before
486 @retval EFI_SUCCESS In non-blocking mode, the function has been
487 dispatched to specified AP.
488 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
489 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
491 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
493 @retval EFI_DEVICE_ERROR The calling processor is an AP.
494 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
495 the specified AP has finished.
496 @retval EFI_NOT_READY The specified AP is busy.
497 @retval EFI_NOT_READY MP Initialize Library is not initialized.
498 @retval EFI_NOT_FOUND The processor with the handle specified by
499 ProcessorNumber does not exist.
500 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
501 @retval EFI_INVALID_PARAMETER Procedure is NULL.
506 MpInitLibStartupThisAP (
507 IN EFI_AP_PROCEDURE Procedure
,
508 IN UINTN ProcessorNumber
,
509 IN EFI_EVENT WaitEvent OPTIONAL
,
510 IN UINTN TimeoutInMicroseconds
,
511 IN VOID
*ProcedureArgument OPTIONAL
,
512 OUT BOOLEAN
*Finished OPTIONAL
518 // temporarily stop checkAllApsStatus for avoid resource dead-lock.
520 mStopCheckAllApsStatus
= TRUE
;
522 Status
= StartupThisAPWorker (
526 TimeoutInMicroseconds
,
531 mStopCheckAllApsStatus
= FALSE
;
537 This service switches the requested AP to be the BSP from that point onward.
538 This service changes the BSP for all purposes. This call can only be performed
541 @param[in] ProcessorNumber The handle number of AP that is to become the new
542 BSP. The range is from 0 to the total number of
543 logical processors minus 1. The total number of
544 logical processors can be retrieved by
545 MpInitLibGetNumberOfProcessors().
546 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
547 enabled AP. Otherwise, it will be disabled.
549 @retval EFI_SUCCESS BSP successfully switched.
550 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
551 this service returning.
552 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
553 @retval EFI_DEVICE_ERROR The calling processor is an AP.
554 @retval EFI_NOT_FOUND The processor with the handle specified by
555 ProcessorNumber does not exist.
556 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
558 @retval EFI_NOT_READY The specified AP is busy.
559 @retval EFI_NOT_READY MP Initialize Library is not initialized.
565 IN UINTN ProcessorNumber
,
566 IN BOOLEAN EnableOldBSP
570 BOOLEAN OldInterruptState
;
573 // Before send both BSP and AP to a procedure to exchange their roles,
574 // interrupt must be disabled. This is because during the exchange role
575 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
576 // be corrupted, since interrupt return address will be pushed to stack
579 OldInterruptState
= SaveAndDisableInterrupts ();
582 // Mask LINT0 & LINT1 for the old BSP
584 DisableLvtInterrupts ();
586 Status
= SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
589 // Restore interrupt state.
591 SetInterruptState (OldInterruptState
);
597 This service lets the caller enable or disable an AP from this point onward.
598 This service may only be called from the BSP.
600 @param[in] ProcessorNumber The handle number of AP.
601 The range is from 0 to the total number of
602 logical processors minus 1. The total number of
603 logical processors can be retrieved by
604 MpInitLibGetNumberOfProcessors().
605 @param[in] EnableAP Specifies the new state for the processor for
606 enabled, FALSE for disabled.
607 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
608 the new health status of the AP. This flag
609 corresponds to StatusFlag defined in
610 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
611 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
612 bits are ignored. If it is NULL, this parameter
615 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
616 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
617 prior to this service returning.
618 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
619 @retval EFI_DEVICE_ERROR The calling processor is an AP.
620 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
622 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
623 @retval EFI_NOT_READY MP Initialize Library is not initialized.
628 MpInitLibEnableDisableAP (
629 IN UINTN ProcessorNumber
,
631 IN UINT32
*HealthFlag OPTIONAL
635 BOOLEAN TempStopCheckState
;
637 TempStopCheckState
= FALSE
;
639 // temporarily stop checkAllAPsStatus for initialize parameters.
641 if (!mStopCheckAllApsStatus
) {
642 mStopCheckAllApsStatus
= TRUE
;
643 TempStopCheckState
= TRUE
;
646 Status
= EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);
648 if (TempStopCheckState
) {
649 mStopCheckAllApsStatus
= FALSE
;