2 MP initialize support functions for PEI phase.
4 Copyright (c) 2016 - 2017, 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
);
138 Check if AP wakeup buffer is overlapped with existing allocated buffer.
140 @param[in] WakeupBufferStart AP wakeup buffer start address.
141 @param[in] WakeupBufferEnd AP wakeup buffer end address.
143 @retval TRUE There is overlap.
144 @retval FALSE There is no overlap.
147 CheckOverlapWithAllocatedBuffer (
148 IN UINTN WakeupBufferStart
,
149 IN UINTN WakeupBufferEnd
152 EFI_PEI_HOB_POINTERS Hob
;
153 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
160 // Get the HOB list for processing
162 Hob
.Raw
= GetHobList ();
164 // Collect memory ranges
166 while (!END_OF_HOB_LIST (Hob
)) {
167 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
168 MemoryHob
= Hob
.MemoryAllocation
;
169 MemoryStart
= (UINTN
) MemoryHob
->AllocDescriptor
.MemoryBaseAddress
;
170 MemoryEnd
= (UINTN
) (MemoryHob
->AllocDescriptor
.MemoryBaseAddress
+
171 MemoryHob
->AllocDescriptor
.MemoryLength
);
172 if (!((WakeupBufferStart
>= MemoryEnd
) || (WakeupBufferEnd
<= MemoryStart
))) {
177 Hob
.Raw
= GET_NEXT_HOB (Hob
);
183 Get available system memory below 1MB by specified size.
185 @param[in] WakeupBufferSize Wakeup buffer size required
187 @retval other Return wakeup buffer address below 1MB.
188 @retval -1 Cannot find free memory below 1MB.
192 IN UINTN WakeupBufferSize
195 EFI_PEI_HOB_POINTERS Hob
;
196 UINTN WakeupBufferStart
;
197 UINTN WakeupBufferEnd
;
199 WakeupBufferSize
= (WakeupBufferSize
+ SIZE_4KB
- 1) & ~(SIZE_4KB
- 1);
202 // Get the HOB list for processing
204 Hob
.Raw
= GetHobList ();
207 // Collect memory ranges
209 while (!END_OF_HOB_LIST (Hob
)) {
210 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
211 if ((Hob
.ResourceDescriptor
->PhysicalStart
< BASE_1MB
) &&
212 (Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
) &&
213 ((Hob
.ResourceDescriptor
->ResourceAttribute
&
214 (EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED
|
215 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED
|
216 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED
220 // Need memory under 1MB to be collected here
222 WakeupBufferEnd
= (UINTN
) (Hob
.ResourceDescriptor
->PhysicalStart
+ Hob
.ResourceDescriptor
->ResourceLength
);
223 if (WakeupBufferEnd
> BASE_1MB
) {
225 // Wakeup buffer should be under 1MB
227 WakeupBufferEnd
= BASE_1MB
;
229 while (WakeupBufferEnd
> WakeupBufferSize
) {
231 // Wakeup buffer should be aligned on 4KB
233 WakeupBufferStart
= (WakeupBufferEnd
- WakeupBufferSize
) & ~(SIZE_4KB
- 1);
234 if (WakeupBufferStart
< Hob
.ResourceDescriptor
->PhysicalStart
) {
237 if (CheckOverlapWithAllocatedBuffer (WakeupBufferStart
, WakeupBufferEnd
)) {
239 // If this range is overlapped with existing allocated buffer, skip it
240 // and find the next range
242 WakeupBufferEnd
-= WakeupBufferSize
;
245 DEBUG ((DEBUG_INFO
, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",
246 WakeupBufferStart
, WakeupBufferSize
));
248 // Create a memory allocation HOB.
250 BuildMemoryAllocationHob (
255 return WakeupBufferStart
;
262 Hob
.Raw
= GET_NEXT_HOB (Hob
);
269 Allocate reset vector buffer.
271 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
274 AllocateResetVector (
275 IN OUT CPU_MP_DATA
*CpuMpData
278 UINTN ApResetVectorSize
;
280 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
281 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
282 sizeof (MP_CPU_EXCHANGE_INFO
);
284 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
285 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
286 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
288 BackupAndPrepareWakeupBuffer (CpuMpData
);
292 Free AP reset vector buffer.
294 @param[in] CpuMpData The pointer to CPU MP Data structure.
298 IN CPU_MP_DATA
*CpuMpData
301 RestoreWakeupBuffer (CpuMpData
);
305 Checks APs status and updates APs status if needed.
309 CheckAndUpdateApsStatus (
316 Initialize global data for MP support.
318 @param[in] CpuMpData The pointer to CPU MP Data structure.
322 IN CPU_MP_DATA
*CpuMpData
327 SaveCpuMpData (CpuMpData
);
329 if (CpuMpData
->CpuCount
== 1) {
331 // If only BSP exists, return
337 // Register an event for EndOfPei
339 Status
= PeiServicesNotifyPpi (&mMpInitLibNotifyList
);
340 ASSERT_EFI_ERROR (Status
);
344 This service executes a caller provided function on all enabled APs.
346 @param[in] Procedure A pointer to the function to be run on
347 enabled APs of the system. See type
349 @param[in] SingleThread If TRUE, then all the enabled APs execute
350 the function specified by Procedure one by
351 one, in ascending order of processor handle
352 number. If FALSE, then all the enabled APs
353 execute the function specified by Procedure
355 @param[in] WaitEvent The event created by the caller with CreateEvent()
356 service. If it is NULL, then execute in
357 blocking mode. BSP waits until all APs finish
358 or TimeoutInMicroSeconds expires. If it's
359 not NULL, then execute in non-blocking mode.
360 BSP requests the function specified by
361 Procedure to be started on all the enabled
362 APs, and go on executing immediately. If
363 all return from Procedure, or TimeoutInMicroSeconds
364 expires, this event is signaled. The BSP
365 can use the CheckEvent() or WaitForEvent()
366 services to check the state of event. Type
367 EFI_EVENT is defined in CreateEvent() in
368 the Unified Extensible Firmware Interface
370 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
371 APs to return from Procedure, either for
372 blocking or non-blocking mode. Zero means
373 infinity. If the timeout expires before
374 all APs return from Procedure, then Procedure
375 on the failed APs is terminated. All enabled
376 APs are available for next function assigned
377 by MpInitLibStartupAllAPs() or
378 MPInitLibStartupThisAP().
379 If the timeout expires in blocking mode,
380 BSP returns EFI_TIMEOUT. If the timeout
381 expires in non-blocking mode, WaitEvent
382 is signaled with SignalEvent().
383 @param[in] ProcedureArgument The parameter passed into Procedure for
385 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
386 if all APs finish successfully, then its
387 content is set to NULL. If not all APs
388 finish before timeout expires, then its
389 content is set to address of the buffer
390 holding handle numbers of the failed APs.
391 The buffer is allocated by MP Initialization
392 library, and it's the caller's responsibility to
393 free the buffer with FreePool() service.
394 In blocking mode, it is ready for consumption
395 when the call returns. In non-blocking mode,
396 it is ready when WaitEvent is signaled. The
397 list of failed CPU is terminated by
400 @retval EFI_SUCCESS In blocking mode, all APs have finished before
402 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
404 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
405 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
407 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
409 @retval EFI_DEVICE_ERROR Caller processor is AP.
410 @retval EFI_NOT_STARTED No enabled APs exist in the system.
411 @retval EFI_NOT_READY Any enabled APs are busy.
412 @retval EFI_NOT_READY MP Initialize Library is not initialized.
413 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
414 all enabled APs have finished.
415 @retval EFI_INVALID_PARAMETER Procedure is NULL.
420 MpInitLibStartupAllAPs (
421 IN EFI_AP_PROCEDURE Procedure
,
422 IN BOOLEAN SingleThread
,
423 IN EFI_EVENT WaitEvent OPTIONAL
,
424 IN UINTN TimeoutInMicroseconds
,
425 IN VOID
*ProcedureArgument OPTIONAL
,
426 OUT UINTN
**FailedCpuList OPTIONAL
429 if (WaitEvent
!= NULL
) {
430 return EFI_UNSUPPORTED
;
433 return StartupAllAPsWorker (
437 TimeoutInMicroseconds
,
444 This service lets the caller get one enabled AP to execute a caller-provided
447 @param[in] Procedure A pointer to the function to be run on the
448 designated AP of the system. See type
450 @param[in] ProcessorNumber The handle number of the AP. The range is
451 from 0 to the total number of logical
452 processors minus 1. The total number of
453 logical processors can be retrieved by
454 MpInitLibGetNumberOfProcessors().
455 @param[in] WaitEvent The event created by the caller with CreateEvent()
456 service. If it is NULL, then execute in
457 blocking mode. BSP waits until this AP finish
458 or TimeoutInMicroSeconds expires. If it's
459 not NULL, then execute in non-blocking mode.
460 BSP requests the function specified by
461 Procedure to be started on this AP,
462 and go on executing immediately. If this AP
463 return from Procedure or TimeoutInMicroSeconds
464 expires, this event is signaled. The BSP
465 can use the CheckEvent() or WaitForEvent()
466 services to check the state of event. Type
467 EFI_EVENT is defined in CreateEvent() in
468 the Unified Extensible Firmware Interface
470 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
471 this AP to finish this Procedure, either for
472 blocking or non-blocking mode. Zero means
473 infinity. If the timeout expires before
474 this AP returns from Procedure, then Procedure
475 on the AP is terminated. The
476 AP is available for next function assigned
477 by MpInitLibStartupAllAPs() or
478 MpInitLibStartupThisAP().
479 If the timeout expires in blocking mode,
480 BSP returns EFI_TIMEOUT. If the timeout
481 expires in non-blocking mode, WaitEvent
482 is signaled with SignalEvent().
483 @param[in] ProcedureArgument The parameter passed into Procedure on the
485 @param[out] Finished If NULL, this parameter is ignored. In
486 blocking mode, this parameter is ignored.
487 In non-blocking mode, if AP returns from
488 Procedure before the timeout expires, its
489 content is set to TRUE. Otherwise, the
490 value is set to FALSE. The caller can
491 determine if the AP returned from Procedure
492 by evaluating this value.
494 @retval EFI_SUCCESS In blocking mode, specified AP finished before
496 @retval EFI_SUCCESS In non-blocking mode, the function has been
497 dispatched to specified AP.
498 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
499 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
501 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
503 @retval EFI_DEVICE_ERROR The calling processor is an AP.
504 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
505 the specified AP has finished.
506 @retval EFI_NOT_READY The specified AP is busy.
507 @retval EFI_NOT_READY MP Initialize Library is not initialized.
508 @retval EFI_NOT_FOUND The processor with the handle specified by
509 ProcessorNumber does not exist.
510 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
511 @retval EFI_INVALID_PARAMETER Procedure is NULL.
516 MpInitLibStartupThisAP (
517 IN EFI_AP_PROCEDURE Procedure
,
518 IN UINTN ProcessorNumber
,
519 IN EFI_EVENT WaitEvent OPTIONAL
,
520 IN UINTN TimeoutInMicroseconds
,
521 IN VOID
*ProcedureArgument OPTIONAL
,
522 OUT BOOLEAN
*Finished OPTIONAL
525 if (WaitEvent
!= NULL
) {
526 return EFI_UNSUPPORTED
;
529 return StartupThisAPWorker (
533 TimeoutInMicroseconds
,
540 This service switches the requested AP to be the BSP from that point onward.
541 This service changes the BSP for all purposes. This call can only be performed
544 @param[in] ProcessorNumber The handle number of AP that is to become the new
545 BSP. The range is from 0 to the total number of
546 logical processors minus 1. The total number of
547 logical processors can be retrieved by
548 MpInitLibGetNumberOfProcessors().
549 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
550 enabled AP. Otherwise, it will be disabled.
552 @retval EFI_SUCCESS BSP successfully switched.
553 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
554 this service returning.
555 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
556 @retval EFI_DEVICE_ERROR The calling processor is an AP.
557 @retval EFI_NOT_FOUND The processor with the handle specified by
558 ProcessorNumber does not exist.
559 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
561 @retval EFI_NOT_READY The specified AP is busy.
562 @retval EFI_NOT_READY MP Initialize Library is not initialized.
568 IN UINTN ProcessorNumber
,
569 IN BOOLEAN EnableOldBSP
572 return SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
576 This service lets the caller enable or disable an AP from this point onward.
577 This service may only be called from the BSP.
579 @param[in] ProcessorNumber The handle number of AP.
580 The range is from 0 to the total number of
581 logical processors minus 1. The total number of
582 logical processors can be retrieved by
583 MpInitLibGetNumberOfProcessors().
584 @param[in] EnableAP Specifies the new state for the processor for
585 enabled, FALSE for disabled.
586 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
587 the new health status of the AP. This flag
588 corresponds to StatusFlag defined in
589 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
590 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
591 bits are ignored. If it is NULL, this parameter
594 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
595 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
596 prior to this service returning.
597 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
598 @retval EFI_DEVICE_ERROR The calling processor is an AP.
599 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
601 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
602 @retval EFI_NOT_READY MP Initialize Library is not initialized.
607 MpInitLibEnableDisableAP (
608 IN UINTN ProcessorNumber
,
610 IN UINT32
*HealthFlag OPTIONAL
613 return EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);