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 volatile BOOLEAN mStopCheckAllApsStatus
= TRUE
;
28 Get the pointer to CPU MP Data structure.
30 @return The pointer to CPU MP Data structure.
37 ASSERT (mCpuMpData
!= NULL
);
42 Save the pointer to CPU MP Data structure.
44 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
48 IN CPU_MP_DATA
*CpuMpData
51 mCpuMpData
= CpuMpData
;
55 Allocate reset vector buffer.
57 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
61 IN OUT CPU_MP_DATA
*CpuMpData
65 UINTN ApResetVectorSize
;
66 EFI_PHYSICAL_ADDRESS StartAddress
;
68 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
69 sizeof (MP_CPU_EXCHANGE_INFO
);
71 StartAddress
= BASE_1MB
;
72 Status
= gBS
->AllocatePages (
75 EFI_SIZE_TO_PAGES (ApResetVectorSize
),
78 ASSERT_EFI_ERROR (Status
);
80 CpuMpData
->WakeupBuffer
= (UINTN
) StartAddress
;
81 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
82 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
84 // copy AP reset code in it
87 (VOID
*) CpuMpData
->WakeupBuffer
,
88 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
89 CpuMpData
->AddressMap
.RendezvousFunnelSize
94 Free AP reset vector buffer.
96 @param[in] CpuMpData The pointer to CPU MP Data structure.
100 IN CPU_MP_DATA
*CpuMpData
104 UINTN ApResetVectorSize
;
105 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
106 sizeof (MP_CPU_EXCHANGE_INFO
);
107 Status
= gBS
->FreePages(
108 (EFI_PHYSICAL_ADDRESS
)CpuMpData
->WakeupBuffer
,
109 EFI_SIZE_TO_PAGES (ApResetVectorSize
)
111 ASSERT_EFI_ERROR (Status
);
115 Checks APs status and updates APs status if needed.
119 CheckAndUpdateApsStatus (
123 UINTN ProcessorNumber
;
125 CPU_MP_DATA
*CpuMpData
;
127 CpuMpData
= GetCpuMpData ();
130 // First, check whether pending StartupAllAPs() exists.
132 if (CpuMpData
->WaitEvent
!= NULL
) {
134 Status
= CheckAllAPs ();
136 // If all APs finish for StartupAllAPs(), signal the WaitEvent for it.
138 if (Status
!= EFI_NOT_READY
) {
139 Status
= gBS
->SignalEvent (CpuMpData
->WaitEvent
);
140 CpuMpData
->WaitEvent
= NULL
;
145 // Second, check whether pending StartupThisAPs() callings exist.
147 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
149 if (CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
== NULL
) {
153 Status
= CheckThisAP (ProcessorNumber
);
155 if (Status
!= EFI_NOT_READY
) {
156 gBS
->SignalEvent (CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
);
157 CpuMpData
->CpuData
[ProcessorNumber
].WaitEvent
= NULL
;
163 Checks APs' status periodically.
165 This function is triggerred by timer perodically to check the
166 state of APs for StartupAllAPs() and StartupThisAP() executed
167 in non-blocking mode.
169 @param[in] Event Event triggered.
170 @param[in] Context Parameter passed with the event.
181 // If CheckApsStatus() is not stopped, otherwise return immediately.
183 if (!mStopCheckAllApsStatus
) {
184 CheckAndUpdateApsStatus ();
189 Initialize global data for MP support.
191 @param[in] CpuMpData The pointer to CPU MP Data structure.
195 IN CPU_MP_DATA
*CpuMpData
200 SaveCpuMpData (CpuMpData
);
202 Status
= gBS
->CreateEvent (
203 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
209 ASSERT_EFI_ERROR (Status
);
212 // Set timer to check all APs status.
214 Status
= gBS
->SetTimer (
219 ASSERT_EFI_ERROR (Status
);
223 This service executes a caller provided function on all enabled APs.
225 @param[in] Procedure A pointer to the function to be run on
226 enabled APs of the system. See type
228 @param[in] SingleThread If TRUE, then all the enabled APs execute
229 the function specified by Procedure one by
230 one, in ascending order of processor handle
231 number. If FALSE, then all the enabled APs
232 execute the function specified by Procedure
234 @param[in] WaitEvent The event created by the caller with CreateEvent()
235 service. If it is NULL, then execute in
236 blocking mode. BSP waits until all APs finish
237 or TimeoutInMicroSeconds expires. If it's
238 not NULL, then execute in non-blocking mode.
239 BSP requests the function specified by
240 Procedure to be started on all the enabled
241 APs, and go on executing immediately. If
242 all return from Procedure, or TimeoutInMicroSeconds
243 expires, this event is signaled. The BSP
244 can use the CheckEvent() or WaitForEvent()
245 services to check the state of event. Type
246 EFI_EVENT is defined in CreateEvent() in
247 the Unified Extensible Firmware Interface
249 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
250 APs to return from Procedure, either for
251 blocking or non-blocking mode. Zero means
252 infinity. If the timeout expires before
253 all APs return from Procedure, then Procedure
254 on the failed APs is terminated. All enabled
255 APs are available for next function assigned
256 by MpInitLibStartupAllAPs() or
257 MPInitLibStartupThisAP().
258 If the timeout expires in blocking mode,
259 BSP returns EFI_TIMEOUT. If the timeout
260 expires in non-blocking mode, WaitEvent
261 is signaled with SignalEvent().
262 @param[in] ProcedureArgument The parameter passed into Procedure for
264 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
265 if all APs finish successfully, then its
266 content is set to NULL. If not all APs
267 finish before timeout expires, then its
268 content is set to address of the buffer
269 holding handle numbers of the failed APs.
270 The buffer is allocated by MP Initialization
271 library, and it's the caller's responsibility to
272 free the buffer with FreePool() service.
273 In blocking mode, it is ready for consumption
274 when the call returns. In non-blocking mode,
275 it is ready when WaitEvent is signaled. The
276 list of failed CPU is terminated by
279 @retval EFI_SUCCESS In blocking mode, all APs have finished before
281 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
283 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
284 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
286 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
288 @retval EFI_DEVICE_ERROR Caller processor is AP.
289 @retval EFI_NOT_STARTED No enabled APs exist in the system.
290 @retval EFI_NOT_READY Any enabled APs are busy.
291 @retval EFI_NOT_READY MP Initialize Library is not initialized.
292 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
293 all enabled APs have finished.
294 @retval EFI_INVALID_PARAMETER Procedure is NULL.
299 MpInitLibStartupAllAPs (
300 IN EFI_AP_PROCEDURE Procedure
,
301 IN BOOLEAN SingleThread
,
302 IN EFI_EVENT WaitEvent OPTIONAL
,
303 IN UINTN TimeoutInMicroseconds
,
304 IN VOID
*ProcedureArgument OPTIONAL
,
305 OUT UINTN
**FailedCpuList OPTIONAL
311 // Temporarily stop checkAllApsStatus for avoid resource dead-lock.
313 mStopCheckAllApsStatus
= TRUE
;
315 Status
= StartupAllAPsWorker (
319 TimeoutInMicroseconds
,
325 // Start checkAllApsStatus
327 mStopCheckAllApsStatus
= FALSE
;
333 This service lets the caller get one enabled AP to execute a caller-provided
336 @param[in] Procedure A pointer to the function to be run on the
337 designated AP of the system. See type
339 @param[in] ProcessorNumber The handle number of the AP. The range is
340 from 0 to the total number of logical
341 processors minus 1. The total number of
342 logical processors can be retrieved by
343 MpInitLibGetNumberOfProcessors().
344 @param[in] WaitEvent The event created by the caller with CreateEvent()
345 service. If it is NULL, then execute in
346 blocking mode. BSP waits until this AP finish
347 or TimeoutInMicroSeconds expires. If it's
348 not NULL, then execute in non-blocking mode.
349 BSP requests the function specified by
350 Procedure to be started on this AP,
351 and go on executing immediately. If this AP
352 return from Procedure or TimeoutInMicroSeconds
353 expires, this event is signaled. The BSP
354 can use the CheckEvent() or WaitForEvent()
355 services to check the state of event. Type
356 EFI_EVENT is defined in CreateEvent() in
357 the Unified Extensible Firmware Interface
359 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
360 this AP to finish this Procedure, either for
361 blocking or non-blocking mode. Zero means
362 infinity. If the timeout expires before
363 this AP returns from Procedure, then Procedure
364 on the AP is terminated. The
365 AP is available for next function assigned
366 by MpInitLibStartupAllAPs() or
367 MpInitLibStartupThisAP().
368 If the timeout expires in blocking mode,
369 BSP returns EFI_TIMEOUT. If the timeout
370 expires in non-blocking mode, WaitEvent
371 is signaled with SignalEvent().
372 @param[in] ProcedureArgument The parameter passed into Procedure on the
374 @param[out] Finished If NULL, this parameter is ignored. In
375 blocking mode, this parameter is ignored.
376 In non-blocking mode, if AP returns from
377 Procedure before the timeout expires, its
378 content is set to TRUE. Otherwise, the
379 value is set to FALSE. The caller can
380 determine if the AP returned from Procedure
381 by evaluating this value.
383 @retval EFI_SUCCESS In blocking mode, specified AP finished before
385 @retval EFI_SUCCESS In non-blocking mode, the function has been
386 dispatched to specified AP.
387 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
388 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
390 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
392 @retval EFI_DEVICE_ERROR The calling processor is an AP.
393 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
394 the specified AP has finished.
395 @retval EFI_NOT_READY The specified AP is busy.
396 @retval EFI_NOT_READY MP Initialize Library is not initialized.
397 @retval EFI_NOT_FOUND The processor with the handle specified by
398 ProcessorNumber does not exist.
399 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
400 @retval EFI_INVALID_PARAMETER Procedure is NULL.
405 MpInitLibStartupThisAP (
406 IN EFI_AP_PROCEDURE Procedure
,
407 IN UINTN ProcessorNumber
,
408 IN EFI_EVENT WaitEvent OPTIONAL
,
409 IN UINTN TimeoutInMicroseconds
,
410 IN VOID
*ProcedureArgument OPTIONAL
,
411 OUT BOOLEAN
*Finished OPTIONAL
417 // temporarily stop checkAllApsStatus for avoid resource dead-lock.
419 mStopCheckAllApsStatus
= TRUE
;
421 Status
= StartupThisAPWorker (
425 TimeoutInMicroseconds
,
430 mStopCheckAllApsStatus
= FALSE
;
436 This service switches the requested AP to be the BSP from that point onward.
437 This service changes the BSP for all purposes. This call can only be performed
440 @param[in] ProcessorNumber The handle number of AP that is to become the new
441 BSP. The range is from 0 to the total number of
442 logical processors minus 1. The total number of
443 logical processors can be retrieved by
444 MpInitLibGetNumberOfProcessors().
445 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
446 enabled AP. Otherwise, it will be disabled.
448 @retval EFI_SUCCESS BSP successfully switched.
449 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
450 this service returning.
451 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
452 @retval EFI_DEVICE_ERROR The calling processor is an AP.
453 @retval EFI_NOT_FOUND The processor with the handle specified by
454 ProcessorNumber does not exist.
455 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
457 @retval EFI_NOT_READY The specified AP is busy.
458 @retval EFI_NOT_READY MP Initialize Library is not initialized.
464 IN UINTN ProcessorNumber
,
465 IN BOOLEAN EnableOldBSP
469 BOOLEAN OldInterruptState
;
472 // Before send both BSP and AP to a procedure to exchange their roles,
473 // interrupt must be disabled. This is because during the exchange role
474 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
475 // be corrupted, since interrupt return address will be pushed to stack
478 OldInterruptState
= SaveAndDisableInterrupts ();
481 // Mask LINT0 & LINT1 for the old BSP
483 DisableLvtInterrupts ();
485 Status
= SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
488 // Restore interrupt state.
490 SetInterruptState (OldInterruptState
);
496 This service lets the caller enable or disable an AP from this point onward.
497 This service may only be called from the BSP.
499 @param[in] ProcessorNumber The handle number of AP.
500 The range is from 0 to the total number of
501 logical processors minus 1. The total number of
502 logical processors can be retrieved by
503 MpInitLibGetNumberOfProcessors().
504 @param[in] EnableAP Specifies the new state for the processor for
505 enabled, FALSE for disabled.
506 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
507 the new health status of the AP. This flag
508 corresponds to StatusFlag defined in
509 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
510 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
511 bits are ignored. If it is NULL, this parameter
514 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
515 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
516 prior to this service returning.
517 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
518 @retval EFI_DEVICE_ERROR The calling processor is an AP.
519 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
521 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
522 @retval EFI_NOT_READY MP Initialize Library is not initialized.
527 MpInitLibEnableDisableAP (
528 IN UINTN ProcessorNumber
,
530 IN UINT32
*HealthFlag OPTIONAL
534 BOOLEAN TempStopCheckState
;
536 TempStopCheckState
= FALSE
;
538 // temporarily stop checkAllAPsStatus for initialize parameters.
540 if (!mStopCheckAllApsStatus
) {
541 mStopCheckAllApsStatus
= TRUE
;
542 TempStopCheckState
= TRUE
;
545 Status
= EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);
547 if (TempStopCheckState
) {
548 mStopCheckAllApsStatus
= FALSE
;