2 MP initialize support functions for PEI 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.
16 #include <Ppi/EndOfPeiPhase.h>
17 #include <Library/PeiServicesLib.h>
20 // Global PEI notify function descriptor on EndofPei event
22 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_NOTIFY_DESCRIPTOR mMpInitLibNotifyList
= {
23 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
24 &gEfiEndOfPeiSignalPpiGuid
,
30 Enable Debug Agent to support source debugging on AP function.
41 Get pointer to CPU MP Data structure.
43 @return The pointer to CPU MP Data structure.
50 CPU_MP_DATA
*CpuMpData
;
52 CpuMpData
= GetCpuMpDataFromGuidedHob ();
53 ASSERT (CpuMpData
!= NULL
);
58 Save the pointer to CPU MP Data structure.
60 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
64 IN CPU_MP_DATA
*CpuMpData
69 // Build location of CPU MP DATA buffer in HOB
71 Data64
= (UINT64
) (UINTN
) CpuMpData
;
73 &mCpuInitMpLibHobGuid
,
80 Notify function on End Of PEI PPI.
82 On S3 boot, this function will restore wakeup buffer data.
83 On normal boot, this function will flag wakeup buffer to be un-used type.
85 @param[in] PeiServices The pointer to the PEI Services Table.
86 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
87 @param[in] Ppi Address of the PPI that was installed.
89 @retval EFI_SUCCESS When everything is OK.
93 CpuMpEndOfPeiCallback (
94 IN EFI_PEI_SERVICES
**PeiServices
,
95 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
100 EFI_BOOT_MODE BootMode
;
101 CPU_MP_DATA
*CpuMpData
;
102 EFI_PEI_HOB_POINTERS Hob
;
103 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
105 DEBUG ((DEBUG_INFO
, "PeiMpInitLib: CpuMpEndOfPeiCallback () invoked\n"));
107 Status
= PeiServicesGetBootMode (&BootMode
);
108 ASSERT_EFI_ERROR (Status
);
110 CpuMpData
= GetCpuMpData ();
111 if (BootMode
!= BOOT_ON_S3_RESUME
) {
113 // Get the HOB list for processing
115 Hob
.Raw
= GetHobList ();
117 // Collect memory ranges
119 while (!END_OF_HOB_LIST (Hob
)) {
120 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
121 MemoryHob
= Hob
.MemoryAllocation
;
122 if (MemoryHob
->AllocDescriptor
.MemoryBaseAddress
== CpuMpData
->WakeupBuffer
) {
124 // Flag this HOB type to un-used
126 GET_HOB_TYPE (Hob
) = EFI_HOB_TYPE_UNUSED
;
130 Hob
.Raw
= GET_NEXT_HOB (Hob
);
133 CpuMpData
->SaveRestoreFlag
= TRUE
;
134 RestoreWakeupBuffer (CpuMpData
);
140 Check if AP wakeup buffer is overlapped with existing allocated buffer.
142 @param[in] WakeupBufferStart AP wakeup buffer start address.
143 @param[in] WakeupBufferEnd AP wakeup buffer end address.
145 @retval TRUE There is overlap.
146 @retval FALSE There is no overlap.
149 CheckOverlapWithAllocatedBuffer (
150 IN UINTN WakeupBufferStart
,
151 IN UINTN WakeupBufferEnd
154 EFI_PEI_HOB_POINTERS Hob
;
155 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
162 // Get the HOB list for processing
164 Hob
.Raw
= GetHobList ();
166 // Collect memory ranges
168 while (!END_OF_HOB_LIST (Hob
)) {
169 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
170 MemoryHob
= Hob
.MemoryAllocation
;
171 MemoryStart
= (UINTN
) MemoryHob
->AllocDescriptor
.MemoryBaseAddress
;
172 MemoryEnd
= (UINTN
) (MemoryHob
->AllocDescriptor
.MemoryBaseAddress
+
173 MemoryHob
->AllocDescriptor
.MemoryLength
);
174 if (!((WakeupBufferStart
>= MemoryEnd
) || (WakeupBufferEnd
<= MemoryStart
))) {
179 Hob
.Raw
= GET_NEXT_HOB (Hob
);
185 Get available system memory below 1MB by specified size.
187 @param[in] WakeupBufferSize Wakeup buffer size required
189 @retval other Return wakeup buffer address below 1MB.
190 @retval -1 Cannot find free memory below 1MB.
194 IN UINTN WakeupBufferSize
197 EFI_PEI_HOB_POINTERS Hob
;
198 UINTN WakeupBufferStart
;
199 UINTN WakeupBufferEnd
;
201 WakeupBufferSize
= (WakeupBufferSize
+ SIZE_4KB
- 1) & ~(SIZE_4KB
- 1);
204 // Get the HOB list for processing
206 Hob
.Raw
= GetHobList ();
209 // Collect memory ranges
211 while (!END_OF_HOB_LIST (Hob
)) {
212 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
213 if ((Hob
.ResourceDescriptor
->PhysicalStart
< BASE_1MB
) &&
214 (Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
) &&
215 ((Hob
.ResourceDescriptor
->ResourceAttribute
&
216 (EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED
|
217 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED
|
218 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED
222 // Need memory under 1MB to be collected here
224 WakeupBufferEnd
= (UINTN
) (Hob
.ResourceDescriptor
->PhysicalStart
+ Hob
.ResourceDescriptor
->ResourceLength
);
225 if (WakeupBufferEnd
> BASE_1MB
) {
227 // Wakeup buffer should be under 1MB
229 WakeupBufferEnd
= BASE_1MB
;
231 while (WakeupBufferEnd
> WakeupBufferSize
) {
233 // Wakeup buffer should be aligned on 4KB
235 WakeupBufferStart
= (WakeupBufferEnd
- WakeupBufferSize
) & ~(SIZE_4KB
- 1);
236 if (WakeupBufferStart
< Hob
.ResourceDescriptor
->PhysicalStart
) {
239 if (CheckOverlapWithAllocatedBuffer (WakeupBufferStart
, WakeupBufferEnd
)) {
241 // If this range is overlapped with existing allocated buffer, skip it
242 // and find the next range
244 WakeupBufferEnd
-= WakeupBufferSize
;
247 DEBUG ((DEBUG_INFO
, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",
248 WakeupBufferStart
, WakeupBufferSize
));
250 // Create a memory allocation HOB.
252 BuildMemoryAllocationHob (
257 return WakeupBufferStart
;
264 Hob
.Raw
= GET_NEXT_HOB (Hob
);
271 Allocate reset vector buffer.
273 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
276 AllocateResetVector (
277 IN OUT CPU_MP_DATA
*CpuMpData
280 UINTN ApResetVectorSize
;
282 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
283 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
284 sizeof (MP_CPU_EXCHANGE_INFO
);
286 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
287 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
288 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
289 BackupAndPrepareWakeupBuffer (CpuMpData
);
292 if (CpuMpData
->SaveRestoreFlag
) {
293 BackupAndPrepareWakeupBuffer (CpuMpData
);
298 Free AP reset vector buffer.
300 @param[in] CpuMpData The pointer to CPU MP Data structure.
304 IN CPU_MP_DATA
*CpuMpData
307 if (CpuMpData
->SaveRestoreFlag
) {
308 RestoreWakeupBuffer (CpuMpData
);
313 Checks APs status and updates APs status if needed.
317 CheckAndUpdateApsStatus (
324 Initialize global data for MP support.
326 @param[in] CpuMpData The pointer to CPU MP Data structure.
330 IN CPU_MP_DATA
*CpuMpData
335 SaveCpuMpData (CpuMpData
);
337 if (CpuMpData
->CpuCount
== 1) {
339 // If only BSP exists, return
345 // Register an event for EndOfPei
347 Status
= PeiServicesNotifyPpi (&mMpInitLibNotifyList
);
348 ASSERT_EFI_ERROR (Status
);
352 This service executes a caller provided function on all enabled APs.
354 @param[in] Procedure A pointer to the function to be run on
355 enabled APs of the system. See type
357 @param[in] SingleThread If TRUE, then all the enabled APs execute
358 the function specified by Procedure one by
359 one, in ascending order of processor handle
360 number. If FALSE, then all the enabled APs
361 execute the function specified by Procedure
363 @param[in] WaitEvent The event created by the caller with CreateEvent()
364 service. If it is NULL, then execute in
365 blocking mode. BSP waits until all APs finish
366 or TimeoutInMicroSeconds expires. If it's
367 not NULL, then execute in non-blocking mode.
368 BSP requests the function specified by
369 Procedure to be started on all the enabled
370 APs, and go on executing immediately. If
371 all return from Procedure, or TimeoutInMicroSeconds
372 expires, this event is signaled. The BSP
373 can use the CheckEvent() or WaitForEvent()
374 services to check the state of event. Type
375 EFI_EVENT is defined in CreateEvent() in
376 the Unified Extensible Firmware Interface
378 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
379 APs to return from Procedure, either for
380 blocking or non-blocking mode. Zero means
381 infinity. If the timeout expires before
382 all APs return from Procedure, then Procedure
383 on the failed APs is terminated. All enabled
384 APs are available for next function assigned
385 by MpInitLibStartupAllAPs() or
386 MPInitLibStartupThisAP().
387 If the timeout expires in blocking mode,
388 BSP returns EFI_TIMEOUT. If the timeout
389 expires in non-blocking mode, WaitEvent
390 is signaled with SignalEvent().
391 @param[in] ProcedureArgument The parameter passed into Procedure for
393 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
394 if all APs finish successfully, then its
395 content is set to NULL. If not all APs
396 finish before timeout expires, then its
397 content is set to address of the buffer
398 holding handle numbers of the failed APs.
399 The buffer is allocated by MP Initialization
400 library, and it's the caller's responsibility to
401 free the buffer with FreePool() service.
402 In blocking mode, it is ready for consumption
403 when the call returns. In non-blocking mode,
404 it is ready when WaitEvent is signaled. The
405 list of failed CPU is terminated by
408 @retval EFI_SUCCESS In blocking mode, all APs have finished before
410 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
412 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
413 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
415 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
417 @retval EFI_DEVICE_ERROR Caller processor is AP.
418 @retval EFI_NOT_STARTED No enabled APs exist in the system.
419 @retval EFI_NOT_READY Any enabled APs are busy.
420 @retval EFI_NOT_READY MP Initialize Library is not initialized.
421 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
422 all enabled APs have finished.
423 @retval EFI_INVALID_PARAMETER Procedure is NULL.
428 MpInitLibStartupAllAPs (
429 IN EFI_AP_PROCEDURE Procedure
,
430 IN BOOLEAN SingleThread
,
431 IN EFI_EVENT WaitEvent OPTIONAL
,
432 IN UINTN TimeoutInMicroseconds
,
433 IN VOID
*ProcedureArgument OPTIONAL
,
434 OUT UINTN
**FailedCpuList OPTIONAL
437 if (WaitEvent
!= NULL
) {
438 return EFI_UNSUPPORTED
;
441 return StartupAllAPsWorker (
445 TimeoutInMicroseconds
,
452 This service lets the caller get one enabled AP to execute a caller-provided
455 @param[in] Procedure A pointer to the function to be run on the
456 designated AP of the system. See type
458 @param[in] ProcessorNumber The handle number of the AP. The range is
459 from 0 to the total number of logical
460 processors minus 1. The total number of
461 logical processors can be retrieved by
462 MpInitLibGetNumberOfProcessors().
463 @param[in] WaitEvent The event created by the caller with CreateEvent()
464 service. If it is NULL, then execute in
465 blocking mode. BSP waits until this AP finish
466 or TimeoutInMicroSeconds expires. If it's
467 not NULL, then execute in non-blocking mode.
468 BSP requests the function specified by
469 Procedure to be started on this AP,
470 and go on executing immediately. If this AP
471 return from Procedure or TimeoutInMicroSeconds
472 expires, this event is signaled. The BSP
473 can use the CheckEvent() or WaitForEvent()
474 services to check the state of event. Type
475 EFI_EVENT is defined in CreateEvent() in
476 the Unified Extensible Firmware Interface
478 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
479 this AP to finish this Procedure, either for
480 blocking or non-blocking mode. Zero means
481 infinity. If the timeout expires before
482 this AP returns from Procedure, then Procedure
483 on the AP is terminated. The
484 AP is available for next function assigned
485 by MpInitLibStartupAllAPs() or
486 MpInitLibStartupThisAP().
487 If the timeout expires in blocking mode,
488 BSP returns EFI_TIMEOUT. If the timeout
489 expires in non-blocking mode, WaitEvent
490 is signaled with SignalEvent().
491 @param[in] ProcedureArgument The parameter passed into Procedure on the
493 @param[out] Finished If NULL, this parameter is ignored. In
494 blocking mode, this parameter is ignored.
495 In non-blocking mode, if AP returns from
496 Procedure before the timeout expires, its
497 content is set to TRUE. Otherwise, the
498 value is set to FALSE. The caller can
499 determine if the AP returned from Procedure
500 by evaluating this value.
502 @retval EFI_SUCCESS In blocking mode, specified AP finished before
504 @retval EFI_SUCCESS In non-blocking mode, the function has been
505 dispatched to specified AP.
506 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
507 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
509 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
511 @retval EFI_DEVICE_ERROR The calling processor is an AP.
512 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
513 the specified AP has finished.
514 @retval EFI_NOT_READY The specified AP is busy.
515 @retval EFI_NOT_READY MP Initialize Library is not initialized.
516 @retval EFI_NOT_FOUND The processor with the handle specified by
517 ProcessorNumber does not exist.
518 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
519 @retval EFI_INVALID_PARAMETER Procedure is NULL.
524 MpInitLibStartupThisAP (
525 IN EFI_AP_PROCEDURE Procedure
,
526 IN UINTN ProcessorNumber
,
527 IN EFI_EVENT WaitEvent OPTIONAL
,
528 IN UINTN TimeoutInMicroseconds
,
529 IN VOID
*ProcedureArgument OPTIONAL
,
530 OUT BOOLEAN
*Finished OPTIONAL
533 if (WaitEvent
!= NULL
) {
534 return EFI_UNSUPPORTED
;
537 return StartupThisAPWorker (
541 TimeoutInMicroseconds
,
548 This service switches the requested AP to be the BSP from that point onward.
549 This service changes the BSP for all purposes. This call can only be performed
552 @param[in] ProcessorNumber The handle number of AP that is to become the new
553 BSP. The range is from 0 to the total number of
554 logical processors minus 1. The total number of
555 logical processors can be retrieved by
556 MpInitLibGetNumberOfProcessors().
557 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
558 enabled AP. Otherwise, it will be disabled.
560 @retval EFI_SUCCESS BSP successfully switched.
561 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
562 this service returning.
563 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
564 @retval EFI_DEVICE_ERROR The calling processor is an AP.
565 @retval EFI_NOT_FOUND The processor with the handle specified by
566 ProcessorNumber does not exist.
567 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
569 @retval EFI_NOT_READY The specified AP is busy.
570 @retval EFI_NOT_READY MP Initialize Library is not initialized.
576 IN UINTN ProcessorNumber
,
577 IN BOOLEAN EnableOldBSP
580 return SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
584 This service lets the caller enable or disable an AP from this point onward.
585 This service may only be called from the BSP.
587 @param[in] ProcessorNumber The handle number of AP.
588 The range is from 0 to the total number of
589 logical processors minus 1. The total number of
590 logical processors can be retrieved by
591 MpInitLibGetNumberOfProcessors().
592 @param[in] EnableAP Specifies the new state for the processor for
593 enabled, FALSE for disabled.
594 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
595 the new health status of the AP. This flag
596 corresponds to StatusFlag defined in
597 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
598 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
599 bits are ignored. If it is NULL, this parameter
602 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
603 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
604 prior to this service returning.
605 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
606 @retval EFI_DEVICE_ERROR The calling processor is an AP.
607 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
609 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
610 @retval EFI_NOT_READY MP Initialize Library is not initialized.
615 MpInitLibEnableDisableAP (
616 IN UINTN ProcessorNumber
,
618 IN UINT32
*HealthFlag OPTIONAL
621 return EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);