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
;
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 @returen 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
;
261 VOID
*ReservedApLoopFunc
;
263 // Avoid APs access invalid buff data which allocated by BootServices,
264 // so we will allocate reserved data for AP loop code.
266 CpuMpData
= GetCpuMpData ();
267 CpuMpData
->PmCodeSegment
= GetProtectedModeCS ();
268 CpuMpData
->ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
269 ReservedApLoopFunc
= AllocateReservedCopyPool (
270 CpuMpData
->AddressMap
.RelocateApLoopFuncSize
,
271 CpuMpData
->AddressMap
.RelocateApLoopFuncAddress
273 WakeUpAP (CpuMpData
, TRUE
, 0, RelocateApLoop
, ReservedApLoopFunc
);
274 DEBUG ((DEBUG_INFO
, "MpInitExitBootServicesCallback() done!\n"));
278 Initialize global data for MP support.
280 @param[in] CpuMpData The pointer to CPU MP Data structure.
284 IN CPU_MP_DATA
*CpuMpData
289 SaveCpuMpData (CpuMpData
);
291 Status
= gBS
->CreateEvent (
292 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
298 ASSERT_EFI_ERROR (Status
);
301 // Set timer to check all APs status.
303 Status
= gBS
->SetTimer (
308 ASSERT_EFI_ERROR (Status
);
309 Status
= gBS
->CreateEvent (
310 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
312 MpInitExitBootServicesCallback
,
314 &mMpInitExitBootServicesEvent
316 ASSERT_EFI_ERROR (Status
);
320 This service executes a caller provided function on all enabled APs.
322 @param[in] Procedure A pointer to the function to be run on
323 enabled APs of the system. See type
325 @param[in] SingleThread If TRUE, then all the enabled APs execute
326 the function specified by Procedure one by
327 one, in ascending order of processor handle
328 number. If FALSE, then all the enabled APs
329 execute the function specified by Procedure
331 @param[in] WaitEvent The event created by the caller with CreateEvent()
332 service. If it is NULL, then execute in
333 blocking mode. BSP waits until all APs finish
334 or TimeoutInMicroSeconds expires. If it's
335 not NULL, then execute in non-blocking mode.
336 BSP requests the function specified by
337 Procedure to be started on all the enabled
338 APs, and go on executing immediately. If
339 all return from Procedure, or TimeoutInMicroSeconds
340 expires, this event is signaled. The BSP
341 can use the CheckEvent() or WaitForEvent()
342 services to check the state of event. Type
343 EFI_EVENT is defined in CreateEvent() in
344 the Unified Extensible Firmware Interface
346 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
347 APs to return from Procedure, either for
348 blocking or non-blocking mode. Zero means
349 infinity. If the timeout expires before
350 all APs return from Procedure, then Procedure
351 on the failed APs is terminated. All enabled
352 APs are available for next function assigned
353 by MpInitLibStartupAllAPs() or
354 MPInitLibStartupThisAP().
355 If the timeout expires in blocking mode,
356 BSP returns EFI_TIMEOUT. If the timeout
357 expires in non-blocking mode, WaitEvent
358 is signaled with SignalEvent().
359 @param[in] ProcedureArgument The parameter passed into Procedure for
361 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
362 if all APs finish successfully, then its
363 content is set to NULL. If not all APs
364 finish before timeout expires, then its
365 content is set to address of the buffer
366 holding handle numbers of the failed APs.
367 The buffer is allocated by MP Initialization
368 library, and it's the caller's responsibility to
369 free the buffer with FreePool() service.
370 In blocking mode, it is ready for consumption
371 when the call returns. In non-blocking mode,
372 it is ready when WaitEvent is signaled. The
373 list of failed CPU is terminated by
376 @retval EFI_SUCCESS In blocking mode, all APs have finished before
378 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
380 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
381 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
383 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
385 @retval EFI_DEVICE_ERROR Caller processor is AP.
386 @retval EFI_NOT_STARTED No enabled APs exist in the system.
387 @retval EFI_NOT_READY Any enabled APs are busy.
388 @retval EFI_NOT_READY MP Initialize Library is not initialized.
389 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
390 all enabled APs have finished.
391 @retval EFI_INVALID_PARAMETER Procedure is NULL.
396 MpInitLibStartupAllAPs (
397 IN EFI_AP_PROCEDURE Procedure
,
398 IN BOOLEAN SingleThread
,
399 IN EFI_EVENT WaitEvent OPTIONAL
,
400 IN UINTN TimeoutInMicroseconds
,
401 IN VOID
*ProcedureArgument OPTIONAL
,
402 OUT UINTN
**FailedCpuList OPTIONAL
408 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
410 mStopCheckAllApsStatus
= TRUE
;
412 Status
= StartupAllAPsWorker (
416 TimeoutInMicroseconds
,
422 // Start checkAllApsStatus
424 mStopCheckAllApsStatus
= FALSE
;
430 This service lets the caller get one enabled AP to execute a caller-provided
433 @param[in] Procedure A pointer to the function to be run on the
434 designated AP of the system. See type
436 @param[in] ProcessorNumber The handle number of the AP. The range is
437 from 0 to the total number of logical
438 processors minus 1. The total number of
439 logical processors can be retrieved by
440 MpInitLibGetNumberOfProcessors().
441 @param[in] WaitEvent The event created by the caller with CreateEvent()
442 service. If it is NULL, then execute in
443 blocking mode. BSP waits until this AP finish
444 or TimeoutInMicroSeconds expires. If it's
445 not NULL, then execute in non-blocking mode.
446 BSP requests the function specified by
447 Procedure to be started on this AP,
448 and go on executing immediately. If this AP
449 return from Procedure or TimeoutInMicroSeconds
450 expires, this event is signaled. The BSP
451 can use the CheckEvent() or WaitForEvent()
452 services to check the state of event. Type
453 EFI_EVENT is defined in CreateEvent() in
454 the Unified Extensible Firmware Interface
456 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
457 this AP to finish this Procedure, either for
458 blocking or non-blocking mode. Zero means
459 infinity. If the timeout expires before
460 this AP returns from Procedure, then Procedure
461 on the AP is terminated. The
462 AP is available for next function assigned
463 by MpInitLibStartupAllAPs() or
464 MpInitLibStartupThisAP().
465 If the timeout expires in blocking mode,
466 BSP returns EFI_TIMEOUT. If the timeout
467 expires in non-blocking mode, WaitEvent
468 is signaled with SignalEvent().
469 @param[in] ProcedureArgument The parameter passed into Procedure on the
471 @param[out] Finished If NULL, this parameter is ignored. In
472 blocking mode, this parameter is ignored.
473 In non-blocking mode, if AP returns from
474 Procedure before the timeout expires, its
475 content is set to TRUE. Otherwise, the
476 value is set to FALSE. The caller can
477 determine if the AP returned from Procedure
478 by evaluating this value.
480 @retval EFI_SUCCESS In blocking mode, specified AP finished before
482 @retval EFI_SUCCESS In non-blocking mode, the function has been
483 dispatched to specified AP.
484 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
485 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
487 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
489 @retval EFI_DEVICE_ERROR The calling processor is an AP.
490 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
491 the specified AP has finished.
492 @retval EFI_NOT_READY The specified AP is busy.
493 @retval EFI_NOT_READY MP Initialize Library is not initialized.
494 @retval EFI_NOT_FOUND The processor with the handle specified by
495 ProcessorNumber does not exist.
496 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
497 @retval EFI_INVALID_PARAMETER Procedure is NULL.
502 MpInitLibStartupThisAP (
503 IN EFI_AP_PROCEDURE Procedure
,
504 IN UINTN ProcessorNumber
,
505 IN EFI_EVENT WaitEvent OPTIONAL
,
506 IN UINTN TimeoutInMicroseconds
,
507 IN VOID
*ProcedureArgument OPTIONAL
,
508 OUT BOOLEAN
*Finished OPTIONAL
514 // temporarily stop checkAllApsStatus for avoid resource dead-lock.
516 mStopCheckAllApsStatus
= TRUE
;
518 Status
= StartupThisAPWorker (
522 TimeoutInMicroseconds
,
527 mStopCheckAllApsStatus
= FALSE
;
533 This service switches the requested AP to be the BSP from that point onward.
534 This service changes the BSP for all purposes. This call can only be performed
537 @param[in] ProcessorNumber The handle number of AP that is to become the new
538 BSP. The range is from 0 to the total number of
539 logical processors minus 1. The total number of
540 logical processors can be retrieved by
541 MpInitLibGetNumberOfProcessors().
542 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
543 enabled AP. Otherwise, it will be disabled.
545 @retval EFI_SUCCESS BSP successfully switched.
546 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
547 this service returning.
548 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
549 @retval EFI_DEVICE_ERROR The calling processor is an AP.
550 @retval EFI_NOT_FOUND The processor with the handle specified by
551 ProcessorNumber does not exist.
552 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
554 @retval EFI_NOT_READY The specified AP is busy.
555 @retval EFI_NOT_READY MP Initialize Library is not initialized.
561 IN UINTN ProcessorNumber
,
562 IN BOOLEAN EnableOldBSP
566 BOOLEAN OldInterruptState
;
569 // Before send both BSP and AP to a procedure to exchange their roles,
570 // interrupt must be disabled. This is because during the exchange role
571 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
572 // be corrupted, since interrupt return address will be pushed to stack
575 OldInterruptState
= SaveAndDisableInterrupts ();
578 // Mask LINT0 & LINT1 for the old BSP
580 DisableLvtInterrupts ();
582 Status
= SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
585 // Restore interrupt state.
587 SetInterruptState (OldInterruptState
);
593 This service lets the caller enable or disable an AP from this point onward.
594 This service may only be called from the BSP.
596 @param[in] ProcessorNumber The handle number of AP.
597 The range is from 0 to the total number of
598 logical processors minus 1. The total number of
599 logical processors can be retrieved by
600 MpInitLibGetNumberOfProcessors().
601 @param[in] EnableAP Specifies the new state for the processor for
602 enabled, FALSE for disabled.
603 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
604 the new health status of the AP. This flag
605 corresponds to StatusFlag defined in
606 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
607 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
608 bits are ignored. If it is NULL, this parameter
611 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
612 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
613 prior to this service returning.
614 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
615 @retval EFI_DEVICE_ERROR The calling processor is an AP.
616 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
618 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
619 @retval EFI_NOT_READY MP Initialize Library is not initialized.
624 MpInitLibEnableDisableAP (
625 IN UINTN ProcessorNumber
,
627 IN UINT32
*HealthFlag OPTIONAL
631 BOOLEAN TempStopCheckState
;
633 TempStopCheckState
= FALSE
;
635 // temporarily stop checkAllAPsStatus for initialize parameters.
637 if (!mStopCheckAllApsStatus
) {
638 mStopCheckAllApsStatus
= TRUE
;
639 TempStopCheckState
= TRUE
;
642 Status
= EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);
644 if (TempStopCheckState
) {
645 mStopCheckAllApsStatus
= FALSE
;