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 // Register an event for EndOfPei
327 Status
= PeiServicesNotifyPpi (&mMpInitLibNotifyList
);
328 ASSERT_EFI_ERROR (Status
);
332 This service executes a caller provided function on all enabled APs.
334 @param[in] Procedure A pointer to the function to be run on
335 enabled APs of the system. See type
337 @param[in] SingleThread If TRUE, then all the enabled APs execute
338 the function specified by Procedure one by
339 one, in ascending order of processor handle
340 number. If FALSE, then all the enabled APs
341 execute the function specified by Procedure
343 @param[in] WaitEvent The event created by the caller with CreateEvent()
344 service. If it is NULL, then execute in
345 blocking mode. BSP waits until all APs finish
346 or TimeoutInMicroSeconds expires. If it's
347 not NULL, then execute in non-blocking mode.
348 BSP requests the function specified by
349 Procedure to be started on all the enabled
350 APs, and go on executing immediately. If
351 all return from Procedure, or TimeoutInMicroSeconds
352 expires, this event is signaled. The BSP
353 can use the CheckEvent() or WaitForEvent()
354 services to check the state of event. Type
355 EFI_EVENT is defined in CreateEvent() in
356 the Unified Extensible Firmware Interface
358 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
359 APs to return from Procedure, either for
360 blocking or non-blocking mode. Zero means
361 infinity. If the timeout expires before
362 all APs return from Procedure, then Procedure
363 on the failed APs is terminated. All enabled
364 APs are available for next function assigned
365 by MpInitLibStartupAllAPs() or
366 MPInitLibStartupThisAP().
367 If the timeout expires in blocking mode,
368 BSP returns EFI_TIMEOUT. If the timeout
369 expires in non-blocking mode, WaitEvent
370 is signaled with SignalEvent().
371 @param[in] ProcedureArgument The parameter passed into Procedure for
373 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
374 if all APs finish successfully, then its
375 content is set to NULL. If not all APs
376 finish before timeout expires, then its
377 content is set to address of the buffer
378 holding handle numbers of the failed APs.
379 The buffer is allocated by MP Initialization
380 library, and it's the caller's responsibility to
381 free the buffer with FreePool() service.
382 In blocking mode, it is ready for consumption
383 when the call returns. In non-blocking mode,
384 it is ready when WaitEvent is signaled. The
385 list of failed CPU is terminated by
388 @retval EFI_SUCCESS In blocking mode, all APs have finished before
390 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
392 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
393 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
395 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
397 @retval EFI_DEVICE_ERROR Caller processor is AP.
398 @retval EFI_NOT_STARTED No enabled APs exist in the system.
399 @retval EFI_NOT_READY Any enabled APs are busy.
400 @retval EFI_NOT_READY MP Initialize Library is not initialized.
401 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
402 all enabled APs have finished.
403 @retval EFI_INVALID_PARAMETER Procedure is NULL.
408 MpInitLibStartupAllAPs (
409 IN EFI_AP_PROCEDURE Procedure
,
410 IN BOOLEAN SingleThread
,
411 IN EFI_EVENT WaitEvent OPTIONAL
,
412 IN UINTN TimeoutInMicroseconds
,
413 IN VOID
*ProcedureArgument OPTIONAL
,
414 OUT UINTN
**FailedCpuList OPTIONAL
417 if (WaitEvent
!= NULL
) {
418 return EFI_UNSUPPORTED
;
421 return StartupAllAPsWorker (
425 TimeoutInMicroseconds
,
432 This service lets the caller get one enabled AP to execute a caller-provided
435 @param[in] Procedure A pointer to the function to be run on the
436 designated AP of the system. See type
438 @param[in] ProcessorNumber The handle number of the AP. The range is
439 from 0 to the total number of logical
440 processors minus 1. The total number of
441 logical processors can be retrieved by
442 MpInitLibGetNumberOfProcessors().
443 @param[in] WaitEvent The event created by the caller with CreateEvent()
444 service. If it is NULL, then execute in
445 blocking mode. BSP waits until this AP finish
446 or TimeoutInMicroSeconds expires. If it's
447 not NULL, then execute in non-blocking mode.
448 BSP requests the function specified by
449 Procedure to be started on this AP,
450 and go on executing immediately. If this AP
451 return from Procedure or TimeoutInMicroSeconds
452 expires, this event is signaled. The BSP
453 can use the CheckEvent() or WaitForEvent()
454 services to check the state of event. Type
455 EFI_EVENT is defined in CreateEvent() in
456 the Unified Extensible Firmware Interface
458 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
459 this AP to finish this Procedure, either for
460 blocking or non-blocking mode. Zero means
461 infinity. If the timeout expires before
462 this AP returns from Procedure, then Procedure
463 on the AP is terminated. The
464 AP is available for next function assigned
465 by MpInitLibStartupAllAPs() or
466 MpInitLibStartupThisAP().
467 If the timeout expires in blocking mode,
468 BSP returns EFI_TIMEOUT. If the timeout
469 expires in non-blocking mode, WaitEvent
470 is signaled with SignalEvent().
471 @param[in] ProcedureArgument The parameter passed into Procedure on the
473 @param[out] Finished If NULL, this parameter is ignored. In
474 blocking mode, this parameter is ignored.
475 In non-blocking mode, if AP returns from
476 Procedure before the timeout expires, its
477 content is set to TRUE. Otherwise, the
478 value is set to FALSE. The caller can
479 determine if the AP returned from Procedure
480 by evaluating this value.
482 @retval EFI_SUCCESS In blocking mode, specified AP finished before
484 @retval EFI_SUCCESS In non-blocking mode, the function has been
485 dispatched to specified AP.
486 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
487 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
489 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
491 @retval EFI_DEVICE_ERROR The calling processor is an AP.
492 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
493 the specified AP has finished.
494 @retval EFI_NOT_READY The specified AP is busy.
495 @retval EFI_NOT_READY MP Initialize Library is not initialized.
496 @retval EFI_NOT_FOUND The processor with the handle specified by
497 ProcessorNumber does not exist.
498 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
499 @retval EFI_INVALID_PARAMETER Procedure is NULL.
504 MpInitLibStartupThisAP (
505 IN EFI_AP_PROCEDURE Procedure
,
506 IN UINTN ProcessorNumber
,
507 IN EFI_EVENT WaitEvent OPTIONAL
,
508 IN UINTN TimeoutInMicroseconds
,
509 IN VOID
*ProcedureArgument OPTIONAL
,
510 OUT BOOLEAN
*Finished OPTIONAL
513 if (WaitEvent
!= NULL
) {
514 return EFI_UNSUPPORTED
;
517 return StartupThisAPWorker (
521 TimeoutInMicroseconds
,
528 This service switches the requested AP to be the BSP from that point onward.
529 This service changes the BSP for all purposes. This call can only be performed
532 @param[in] ProcessorNumber The handle number of AP that is to become the new
533 BSP. The range is from 0 to the total number of
534 logical processors minus 1. The total number of
535 logical processors can be retrieved by
536 MpInitLibGetNumberOfProcessors().
537 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
538 enabled AP. Otherwise, it will be disabled.
540 @retval EFI_SUCCESS BSP successfully switched.
541 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
542 this service returning.
543 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
544 @retval EFI_DEVICE_ERROR The calling processor is an AP.
545 @retval EFI_NOT_FOUND The processor with the handle specified by
546 ProcessorNumber does not exist.
547 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
549 @retval EFI_NOT_READY The specified AP is busy.
550 @retval EFI_NOT_READY MP Initialize Library is not initialized.
556 IN UINTN ProcessorNumber
,
557 IN BOOLEAN EnableOldBSP
560 return SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
564 This service lets the caller enable or disable an AP from this point onward.
565 This service may only be called from the BSP.
567 @param[in] ProcessorNumber The handle number of AP.
568 The range is from 0 to the total number of
569 logical processors minus 1. The total number of
570 logical processors can be retrieved by
571 MpInitLibGetNumberOfProcessors().
572 @param[in] EnableAP Specifies the new state for the processor for
573 enabled, FALSE for disabled.
574 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
575 the new health status of the AP. This flag
576 corresponds to StatusFlag defined in
577 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
578 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
579 bits are ignored. If it is NULL, this parameter
582 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
583 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
584 prior to this service returning.
585 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
586 @retval EFI_DEVICE_ERROR The calling processor is an AP.
587 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
589 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
590 @retval EFI_NOT_READY MP Initialize Library is not initialized.
595 MpInitLibEnableDisableAP (
596 IN UINTN ProcessorNumber
,
598 IN UINT32
*HealthFlag OPTIONAL
601 return EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);