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
,
29 Get pointer to CPU MP Data structure.
31 @return The pointer to CPU MP Data structure.
38 CPU_MP_DATA
*CpuMpData
;
40 CpuMpData
= GetCpuMpDataFromGuidedHob ();
41 ASSERT (CpuMpData
!= NULL
);
46 Save the pointer to CPU MP Data structure.
48 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
52 IN CPU_MP_DATA
*CpuMpData
57 // Build location of CPU MP DATA buffer in HOB
59 Data64
= (UINT64
) (UINTN
) CpuMpData
;
61 &mCpuInitMpLibHobGuid
,
68 Notify function on End Of PEI PPI.
70 On S3 boot, this function will restore wakeup buffer data.
71 On normal boot, this function will flag wakeup buffer to be un-used type.
73 @param[in] PeiServices The pointer to the PEI Services Table.
74 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
75 @param[in] Ppi Address of the PPI that was installed.
77 @retval EFI_SUCCESS When everything is OK.
81 CpuMpEndOfPeiCallback (
82 IN EFI_PEI_SERVICES
**PeiServices
,
83 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
88 EFI_BOOT_MODE BootMode
;
89 CPU_MP_DATA
*CpuMpData
;
90 EFI_PEI_HOB_POINTERS Hob
;
91 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
93 DEBUG ((DEBUG_INFO
, "PeiMpInitLib: CpuMpEndOfPeiCallback () invoked\n"));
95 Status
= PeiServicesGetBootMode (&BootMode
);
96 ASSERT_EFI_ERROR (Status
);
98 CpuMpData
= GetCpuMpData ();
99 if (BootMode
!= BOOT_ON_S3_RESUME
) {
101 // Get the HOB list for processing
103 Hob
.Raw
= GetHobList ();
105 // Collect memory ranges
107 while (!END_OF_HOB_LIST (Hob
)) {
108 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
109 MemoryHob
= Hob
.MemoryAllocation
;
110 if (MemoryHob
->AllocDescriptor
.MemoryBaseAddress
== CpuMpData
->WakeupBuffer
) {
112 // Flag this HOB type to un-used
114 GET_HOB_TYPE (Hob
) = EFI_HOB_TYPE_UNUSED
;
118 Hob
.Raw
= GET_NEXT_HOB (Hob
);
121 CpuMpData
->SaveRestoreFlag
= TRUE
;
122 RestoreWakeupBuffer (CpuMpData
);
128 Check if AP wakeup buffer is overlapped with existing allocated buffer.
130 @param[in] WakeupBufferStart AP wakeup buffer start address.
131 @param[in] WakeupBufferEnd AP wakeup buffer end address.
133 @retval TRUE There is overlap.
134 @retval FALSE There is no overlap.
137 CheckOverlapWithAllocatedBuffer (
138 IN UINTN WakeupBufferStart
,
139 IN UINTN WakeupBufferEnd
142 EFI_PEI_HOB_POINTERS Hob
;
143 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
150 // Get the HOB list for processing
152 Hob
.Raw
= GetHobList ();
154 // Collect memory ranges
156 while (!END_OF_HOB_LIST (Hob
)) {
157 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
158 MemoryHob
= Hob
.MemoryAllocation
;
159 MemoryStart
= (UINTN
) MemoryHob
->AllocDescriptor
.MemoryBaseAddress
;
160 MemoryEnd
= (UINTN
) (MemoryHob
->AllocDescriptor
.MemoryBaseAddress
+
161 MemoryHob
->AllocDescriptor
.MemoryLength
);
162 if (!((WakeupBufferStart
>= MemoryEnd
) || (WakeupBufferEnd
<= MemoryStart
))) {
167 Hob
.Raw
= GET_NEXT_HOB (Hob
);
173 Get available system memory below 1MB by specified size.
175 @param[in] WakeupBufferSize Wakeup buffer size required
177 @retval other Return wakeup buffer address below 1MB.
178 @retval -1 Cannot find free memory below 1MB.
182 IN UINTN WakeupBufferSize
185 EFI_PEI_HOB_POINTERS Hob
;
186 UINTN WakeupBufferStart
;
187 UINTN WakeupBufferEnd
;
189 WakeupBufferSize
= (WakeupBufferSize
+ SIZE_4KB
- 1) & ~(SIZE_4KB
- 1);
192 // Get the HOB list for processing
194 Hob
.Raw
= GetHobList ();
197 // Collect memory ranges
199 while (!END_OF_HOB_LIST (Hob
)) {
200 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
201 if ((Hob
.ResourceDescriptor
->PhysicalStart
< BASE_1MB
) &&
202 (Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
) &&
203 ((Hob
.ResourceDescriptor
->ResourceAttribute
&
204 (EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED
|
205 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED
|
206 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED
210 // Need memory under 1MB to be collected here
212 WakeupBufferEnd
= (UINTN
) (Hob
.ResourceDescriptor
->PhysicalStart
+ Hob
.ResourceDescriptor
->ResourceLength
);
213 if (WakeupBufferEnd
> BASE_1MB
) {
215 // Wakeup buffer should be under 1MB
217 WakeupBufferEnd
= BASE_1MB
;
219 while (WakeupBufferEnd
> WakeupBufferSize
) {
221 // Wakeup buffer should be aligned on 4KB
223 WakeupBufferStart
= (WakeupBufferEnd
- WakeupBufferSize
) & ~(SIZE_4KB
- 1);
224 if (WakeupBufferStart
< Hob
.ResourceDescriptor
->PhysicalStart
) {
227 if (CheckOverlapWithAllocatedBuffer (WakeupBufferStart
, WakeupBufferEnd
)) {
229 // If this range is overlapped with existing allocated buffer, skip it
230 // and find the next range
232 WakeupBufferEnd
-= WakeupBufferSize
;
235 DEBUG ((DEBUG_INFO
, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",
236 WakeupBufferStart
, WakeupBufferSize
));
238 // Create a memory allocation HOB.
240 BuildMemoryAllocationHob (
245 return WakeupBufferStart
;
252 Hob
.Raw
= GET_NEXT_HOB (Hob
);
259 Allocate reset vector buffer.
261 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
264 AllocateResetVector (
265 IN OUT CPU_MP_DATA
*CpuMpData
268 UINTN ApResetVectorSize
;
270 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
271 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
272 sizeof (MP_CPU_EXCHANGE_INFO
);
274 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
275 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
276 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
277 BackupAndPrepareWakeupBuffer (CpuMpData
);
280 if (CpuMpData
->SaveRestoreFlag
) {
281 BackupAndPrepareWakeupBuffer (CpuMpData
);
286 Free AP reset vector buffer.
288 @param[in] CpuMpData The pointer to CPU MP Data structure.
292 IN CPU_MP_DATA
*CpuMpData
295 if (CpuMpData
->SaveRestoreFlag
) {
296 RestoreWakeupBuffer (CpuMpData
);
301 Checks APs status and updates APs status if needed.
305 CheckAndUpdateApsStatus (
312 Initialize global data for MP support.
314 @param[in] CpuMpData The pointer to CPU MP Data structure.
318 IN CPU_MP_DATA
*CpuMpData
323 SaveCpuMpData (CpuMpData
);
325 if (CpuMpData
->CpuCount
== 1) {
327 // If only BSP exists, return
333 // Register an event for EndOfPei
335 Status
= PeiServicesNotifyPpi (&mMpInitLibNotifyList
);
336 ASSERT_EFI_ERROR (Status
);
340 This service executes a caller provided function on all enabled APs.
342 @param[in] Procedure A pointer to the function to be run on
343 enabled APs of the system. See type
345 @param[in] SingleThread If TRUE, then all the enabled APs execute
346 the function specified by Procedure one by
347 one, in ascending order of processor handle
348 number. If FALSE, then all the enabled APs
349 execute the function specified by Procedure
351 @param[in] WaitEvent The event created by the caller with CreateEvent()
352 service. If it is NULL, then execute in
353 blocking mode. BSP waits until all APs finish
354 or TimeoutInMicroSeconds expires. If it's
355 not NULL, then execute in non-blocking mode.
356 BSP requests the function specified by
357 Procedure to be started on all the enabled
358 APs, and go on executing immediately. If
359 all return from Procedure, or TimeoutInMicroSeconds
360 expires, this event is signaled. The BSP
361 can use the CheckEvent() or WaitForEvent()
362 services to check the state of event. Type
363 EFI_EVENT is defined in CreateEvent() in
364 the Unified Extensible Firmware Interface
366 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
367 APs to return from Procedure, either for
368 blocking or non-blocking mode. Zero means
369 infinity. If the timeout expires before
370 all APs return from Procedure, then Procedure
371 on the failed APs is terminated. All enabled
372 APs are available for next function assigned
373 by MpInitLibStartupAllAPs() or
374 MPInitLibStartupThisAP().
375 If the timeout expires in blocking mode,
376 BSP returns EFI_TIMEOUT. If the timeout
377 expires in non-blocking mode, WaitEvent
378 is signaled with SignalEvent().
379 @param[in] ProcedureArgument The parameter passed into Procedure for
381 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
382 if all APs finish successfully, then its
383 content is set to NULL. If not all APs
384 finish before timeout expires, then its
385 content is set to address of the buffer
386 holding handle numbers of the failed APs.
387 The buffer is allocated by MP Initialization
388 library, and it's the caller's responsibility to
389 free the buffer with FreePool() service.
390 In blocking mode, it is ready for consumption
391 when the call returns. In non-blocking mode,
392 it is ready when WaitEvent is signaled. The
393 list of failed CPU is terminated by
396 @retval EFI_SUCCESS In blocking mode, all APs have finished before
398 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
400 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
401 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
403 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
405 @retval EFI_DEVICE_ERROR Caller processor is AP.
406 @retval EFI_NOT_STARTED No enabled APs exist in the system.
407 @retval EFI_NOT_READY Any enabled APs are busy.
408 @retval EFI_NOT_READY MP Initialize Library is not initialized.
409 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
410 all enabled APs have finished.
411 @retval EFI_INVALID_PARAMETER Procedure is NULL.
416 MpInitLibStartupAllAPs (
417 IN EFI_AP_PROCEDURE Procedure
,
418 IN BOOLEAN SingleThread
,
419 IN EFI_EVENT WaitEvent OPTIONAL
,
420 IN UINTN TimeoutInMicroseconds
,
421 IN VOID
*ProcedureArgument OPTIONAL
,
422 OUT UINTN
**FailedCpuList OPTIONAL
425 if (WaitEvent
!= NULL
) {
426 return EFI_UNSUPPORTED
;
429 return StartupAllAPsWorker (
433 TimeoutInMicroseconds
,
440 This service lets the caller get one enabled AP to execute a caller-provided
443 @param[in] Procedure A pointer to the function to be run on the
444 designated AP of the system. See type
446 @param[in] ProcessorNumber The handle number of the AP. The range is
447 from 0 to the total number of logical
448 processors minus 1. The total number of
449 logical processors can be retrieved by
450 MpInitLibGetNumberOfProcessors().
451 @param[in] WaitEvent The event created by the caller with CreateEvent()
452 service. If it is NULL, then execute in
453 blocking mode. BSP waits until this AP finish
454 or TimeoutInMicroSeconds expires. If it's
455 not NULL, then execute in non-blocking mode.
456 BSP requests the function specified by
457 Procedure to be started on this AP,
458 and go on executing immediately. If this AP
459 return from Procedure or TimeoutInMicroSeconds
460 expires, this event is signaled. The BSP
461 can use the CheckEvent() or WaitForEvent()
462 services to check the state of event. Type
463 EFI_EVENT is defined in CreateEvent() in
464 the Unified Extensible Firmware Interface
466 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
467 this AP to finish this Procedure, either for
468 blocking or non-blocking mode. Zero means
469 infinity. If the timeout expires before
470 this AP returns from Procedure, then Procedure
471 on the AP is terminated. The
472 AP is available for next function assigned
473 by MpInitLibStartupAllAPs() or
474 MpInitLibStartupThisAP().
475 If the timeout expires in blocking mode,
476 BSP returns EFI_TIMEOUT. If the timeout
477 expires in non-blocking mode, WaitEvent
478 is signaled with SignalEvent().
479 @param[in] ProcedureArgument The parameter passed into Procedure on the
481 @param[out] Finished If NULL, this parameter is ignored. In
482 blocking mode, this parameter is ignored.
483 In non-blocking mode, if AP returns from
484 Procedure before the timeout expires, its
485 content is set to TRUE. Otherwise, the
486 value is set to FALSE. The caller can
487 determine if the AP returned from Procedure
488 by evaluating this value.
490 @retval EFI_SUCCESS In blocking mode, specified AP finished before
492 @retval EFI_SUCCESS In non-blocking mode, the function has been
493 dispatched to specified AP.
494 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
495 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
497 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
499 @retval EFI_DEVICE_ERROR The calling processor is an AP.
500 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
501 the specified AP has finished.
502 @retval EFI_NOT_READY The specified AP is busy.
503 @retval EFI_NOT_READY MP Initialize Library is not initialized.
504 @retval EFI_NOT_FOUND The processor with the handle specified by
505 ProcessorNumber does not exist.
506 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
507 @retval EFI_INVALID_PARAMETER Procedure is NULL.
512 MpInitLibStartupThisAP (
513 IN EFI_AP_PROCEDURE Procedure
,
514 IN UINTN ProcessorNumber
,
515 IN EFI_EVENT WaitEvent OPTIONAL
,
516 IN UINTN TimeoutInMicroseconds
,
517 IN VOID
*ProcedureArgument OPTIONAL
,
518 OUT BOOLEAN
*Finished OPTIONAL
521 if (WaitEvent
!= NULL
) {
522 return EFI_UNSUPPORTED
;
525 return StartupThisAPWorker (
529 TimeoutInMicroseconds
,
536 This service switches the requested AP to be the BSP from that point onward.
537 This service changes the BSP for all purposes. This call can only be performed
540 @param[in] ProcessorNumber The handle number of AP that is to become the new
541 BSP. The range is from 0 to the total number of
542 logical processors minus 1. The total number of
543 logical processors can be retrieved by
544 MpInitLibGetNumberOfProcessors().
545 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
546 enabled AP. Otherwise, it will be disabled.
548 @retval EFI_SUCCESS BSP successfully switched.
549 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
550 this service returning.
551 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
552 @retval EFI_DEVICE_ERROR The calling processor is an AP.
553 @retval EFI_NOT_FOUND The processor with the handle specified by
554 ProcessorNumber does not exist.
555 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
557 @retval EFI_NOT_READY The specified AP is busy.
558 @retval EFI_NOT_READY MP Initialize Library is not initialized.
564 IN UINTN ProcessorNumber
,
565 IN BOOLEAN EnableOldBSP
568 return SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
572 This service lets the caller enable or disable an AP from this point onward.
573 This service may only be called from the BSP.
575 @param[in] ProcessorNumber The handle number of AP.
576 The range is from 0 to the total number of
577 logical processors minus 1. The total number of
578 logical processors can be retrieved by
579 MpInitLibGetNumberOfProcessors().
580 @param[in] EnableAP Specifies the new state for the processor for
581 enabled, FALSE for disabled.
582 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
583 the new health status of the AP. This flag
584 corresponds to StatusFlag defined in
585 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
586 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
587 bits are ignored. If it is NULL, this parameter
590 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
591 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
592 prior to this service returning.
593 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
594 @retval EFI_DEVICE_ERROR The calling processor is an AP.
595 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
597 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
598 @retval EFI_NOT_READY MP Initialize Library is not initialized.
603 MpInitLibEnableDisableAP (
604 IN UINTN ProcessorNumber
,
606 IN UINT32
*HealthFlag OPTIONAL
609 return EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);