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
308 return EFI_UNSUPPORTED
;
312 This service lets the caller get one enabled AP to execute a caller-provided
315 @param[in] Procedure A pointer to the function to be run on the
316 designated AP of the system. See type
318 @param[in] ProcessorNumber The handle number of the AP. The range is
319 from 0 to the total number of logical
320 processors minus 1. The total number of
321 logical processors can be retrieved by
322 MpInitLibGetNumberOfProcessors().
323 @param[in] WaitEvent The event created by the caller with CreateEvent()
324 service. If it is NULL, then execute in
325 blocking mode. BSP waits until this AP finish
326 or TimeoutInMicroSeconds expires. If it's
327 not NULL, then execute in non-blocking mode.
328 BSP requests the function specified by
329 Procedure to be started on this AP,
330 and go on executing immediately. If this AP
331 return from Procedure or TimeoutInMicroSeconds
332 expires, this event is signaled. The BSP
333 can use the CheckEvent() or WaitForEvent()
334 services to check the state of event. Type
335 EFI_EVENT is defined in CreateEvent() in
336 the Unified Extensible Firmware Interface
338 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
339 this AP to finish this Procedure, either for
340 blocking or non-blocking mode. Zero means
341 infinity. If the timeout expires before
342 this AP returns from Procedure, then Procedure
343 on the AP is terminated. The
344 AP is available for next function assigned
345 by MpInitLibStartupAllAPs() or
346 MpInitLibStartupThisAP().
347 If the timeout expires in blocking mode,
348 BSP returns EFI_TIMEOUT. If the timeout
349 expires in non-blocking mode, WaitEvent
350 is signaled with SignalEvent().
351 @param[in] ProcedureArgument The parameter passed into Procedure on the
353 @param[out] Finished If NULL, this parameter is ignored. In
354 blocking mode, this parameter is ignored.
355 In non-blocking mode, if AP returns from
356 Procedure before the timeout expires, its
357 content is set to TRUE. Otherwise, the
358 value is set to FALSE. The caller can
359 determine if the AP returned from Procedure
360 by evaluating this value.
362 @retval EFI_SUCCESS In blocking mode, specified AP finished before
364 @retval EFI_SUCCESS In non-blocking mode, the function has been
365 dispatched to specified AP.
366 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
367 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
369 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
371 @retval EFI_DEVICE_ERROR The calling processor is an AP.
372 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
373 the specified AP has finished.
374 @retval EFI_NOT_READY The specified AP is busy.
375 @retval EFI_NOT_READY MP Initialize Library is not initialized.
376 @retval EFI_NOT_FOUND The processor with the handle specified by
377 ProcessorNumber does not exist.
378 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
379 @retval EFI_INVALID_PARAMETER Procedure is NULL.
384 MpInitLibStartupThisAP (
385 IN EFI_AP_PROCEDURE Procedure
,
386 IN UINTN ProcessorNumber
,
387 IN EFI_EVENT WaitEvent OPTIONAL
,
388 IN UINTN TimeoutInMicroseconds
,
389 IN VOID
*ProcedureArgument OPTIONAL
,
390 OUT BOOLEAN
*Finished OPTIONAL
393 return EFI_UNSUPPORTED
;
397 This service switches the requested AP to be the BSP from that point onward.
398 This service changes the BSP for all purposes. This call can only be performed
401 @param[in] ProcessorNumber The handle number of AP that is to become the new
402 BSP. The range is from 0 to the total number of
403 logical processors minus 1. The total number of
404 logical processors can be retrieved by
405 MpInitLibGetNumberOfProcessors().
406 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
407 enabled AP. Otherwise, it will be disabled.
409 @retval EFI_SUCCESS BSP successfully switched.
410 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
411 this service returning.
412 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
413 @retval EFI_DEVICE_ERROR The calling processor is an AP.
414 @retval EFI_NOT_FOUND The processor with the handle specified by
415 ProcessorNumber does not exist.
416 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
418 @retval EFI_NOT_READY The specified AP is busy.
419 @retval EFI_NOT_READY MP Initialize Library is not initialized.
425 IN UINTN ProcessorNumber
,
426 IN BOOLEAN EnableOldBSP
430 BOOLEAN OldInterruptState
;
433 // Before send both BSP and AP to a procedure to exchange their roles,
434 // interrupt must be disabled. This is because during the exchange role
435 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
436 // be corrupted, since interrupt return address will be pushed to stack
439 OldInterruptState
= SaveAndDisableInterrupts ();
442 // Mask LINT0 & LINT1 for the old BSP
444 DisableLvtInterrupts ();
446 Status
= SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
449 // Restore interrupt state.
451 SetInterruptState (OldInterruptState
);
457 This service lets the caller enable or disable an AP from this point onward.
458 This service may only be called from the BSP.
460 @param[in] ProcessorNumber The handle number of AP.
461 The range is from 0 to the total number of
462 logical processors minus 1. The total number of
463 logical processors can be retrieved by
464 MpInitLibGetNumberOfProcessors().
465 @param[in] EnableAP Specifies the new state for the processor for
466 enabled, FALSE for disabled.
467 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
468 the new health status of the AP. This flag
469 corresponds to StatusFlag defined in
470 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
471 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
472 bits are ignored. If it is NULL, this parameter
475 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
476 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
477 prior to this service returning.
478 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
479 @retval EFI_DEVICE_ERROR The calling processor is an AP.
480 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
482 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
483 @retval EFI_NOT_READY MP Initialize Library is not initialized.
488 MpInitLibEnableDisableAP (
489 IN UINTN ProcessorNumber
,
491 IN UINT32
*HealthFlag OPTIONAL
495 BOOLEAN TempStopCheckState
;
497 TempStopCheckState
= FALSE
;
499 // temporarily stop checkAllAPsStatus for initialize parameters.
501 if (!mStopCheckAllApsStatus
) {
502 mStopCheckAllApsStatus
= TRUE
;
503 TempStopCheckState
= TRUE
;
506 Status
= EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);
508 if (TempStopCheckState
) {
509 mStopCheckAllApsStatus
= FALSE
;