2 MP initialize support functions for PEI phase.
4 Copyright (c) 2016 - 2018, 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 <Library/PeiServicesLib.h>
17 #include <Guid/S3SmmInitDone.h>
20 S3 SMM Init Done notification function.
22 @param PeiServices Indirect reference to the PEI Services Table.
23 @param NotifyDesc Address of the notification descriptor data structure.
24 @param InvokePpi Address of the PPI that was invoked.
26 @retval EFI_SUCCESS The function completes successfully.
31 NotifyOnS3SmmInitDonePpi (
32 IN EFI_PEI_SERVICES
**PeiServices
,
33 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDesc
,
41 EFI_PEI_NOTIFY_DESCRIPTOR mS3SmmInitDoneNotifyDesc
= {
42 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
43 &gEdkiiS3SmmInitDoneGuid
,
44 NotifyOnS3SmmInitDonePpi
48 S3 SMM Init Done notification function.
50 @param PeiServices Indirect reference to the PEI Services Table.
51 @param NotifyDesc Address of the notification descriptor data structure.
52 @param InvokePpi Address of the PPI that was invoked.
54 @retval EFI_SUCCESS The function completes successfully.
59 NotifyOnS3SmmInitDonePpi (
60 IN EFI_PEI_SERVICES
**PeiServices
,
61 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDesc
,
65 CPU_MP_DATA
*CpuMpData
;
67 CpuMpData
= GetCpuMpData ();
70 // PiSmmCpuDxeSmm driver hardcode change the loop mode to HLT mode.
71 // So in this notify function, code need to check the current loop
72 // mode, if it is not HLT mode, code need to change loop mode back
73 // to the original mode.
75 if (CpuMpData
->ApLoopMode
!= ApInHltLoop
) {
76 CpuMpData
->WakeUpByInitSipiSipi
= TRUE
;
84 Enable Debug Agent to support source debugging on AP function.
95 Get pointer to CPU MP Data structure.
96 For BSP, the pointer is retrieved from HOB.
97 For AP, the structure is just after IDT.
99 @return The pointer to CPU MP Data structure.
106 CPU_MP_DATA
*CpuMpData
;
107 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
108 IA32_DESCRIPTOR Idtr
;
110 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
111 if (ApicBaseMsr
.Bits
.BSP
== 1) {
112 CpuMpData
= GetCpuMpDataFromGuidedHob ();
113 ASSERT (CpuMpData
!= NULL
);
116 CpuMpData
= (CPU_MP_DATA
*) (Idtr
.Base
+ Idtr
.Limit
+ 1);
122 Save the pointer to CPU MP Data structure.
124 @param[in] CpuMpData The pointer to CPU MP Data structure will be saved.
128 IN CPU_MP_DATA
*CpuMpData
133 // Build location of CPU MP DATA buffer in HOB
135 Data64
= (UINT64
) (UINTN
) CpuMpData
;
137 &mCpuInitMpLibHobGuid
,
144 Check if AP wakeup buffer is overlapped with existing allocated buffer.
146 @param[in] WakeupBufferStart AP wakeup buffer start address.
147 @param[in] WakeupBufferEnd AP wakeup buffer end address.
149 @retval TRUE There is overlap.
150 @retval FALSE There is no overlap.
153 CheckOverlapWithAllocatedBuffer (
154 IN UINT64 WakeupBufferStart
,
155 IN UINT64 WakeupBufferEnd
158 EFI_PEI_HOB_POINTERS Hob
;
159 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
166 // Get the HOB list for processing
168 Hob
.Raw
= GetHobList ();
170 // Collect memory ranges
172 while (!END_OF_HOB_LIST (Hob
)) {
173 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
174 MemoryHob
= Hob
.MemoryAllocation
;
175 MemoryStart
= MemoryHob
->AllocDescriptor
.MemoryBaseAddress
;
176 MemoryEnd
= MemoryHob
->AllocDescriptor
.MemoryBaseAddress
+ MemoryHob
->AllocDescriptor
.MemoryLength
;
177 if (!((WakeupBufferStart
>= MemoryEnd
) || (WakeupBufferEnd
<= MemoryStart
))) {
182 Hob
.Raw
= GET_NEXT_HOB (Hob
);
188 Get available system memory below 1MB by specified size.
190 @param[in] WakeupBufferSize Wakeup buffer size required
192 @retval other Return wakeup buffer address below 1MB.
193 @retval -1 Cannot find free memory below 1MB.
197 IN UINTN WakeupBufferSize
200 EFI_PEI_HOB_POINTERS Hob
;
201 UINT64 WakeupBufferStart
;
202 UINT64 WakeupBufferEnd
;
204 WakeupBufferSize
= (WakeupBufferSize
+ SIZE_4KB
- 1) & ~(SIZE_4KB
- 1);
207 // Get the HOB list for processing
209 Hob
.Raw
= GetHobList ();
212 // Collect memory ranges
214 while (!END_OF_HOB_LIST (Hob
)) {
215 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
216 if ((Hob
.ResourceDescriptor
->PhysicalStart
< BASE_1MB
) &&
217 (Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
) &&
218 ((Hob
.ResourceDescriptor
->ResourceAttribute
&
219 (EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED
|
220 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED
|
221 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED
225 // Need memory under 1MB to be collected here
227 WakeupBufferEnd
= Hob
.ResourceDescriptor
->PhysicalStart
+ Hob
.ResourceDescriptor
->ResourceLength
;
228 if (WakeupBufferEnd
> BASE_1MB
) {
230 // Wakeup buffer should be under 1MB
232 WakeupBufferEnd
= BASE_1MB
;
234 while (WakeupBufferEnd
> WakeupBufferSize
) {
236 // Wakeup buffer should be aligned on 4KB
238 WakeupBufferStart
= (WakeupBufferEnd
- WakeupBufferSize
) & ~(SIZE_4KB
- 1);
239 if (WakeupBufferStart
< Hob
.ResourceDescriptor
->PhysicalStart
) {
242 if (CheckOverlapWithAllocatedBuffer (WakeupBufferStart
, WakeupBufferEnd
)) {
244 // If this range is overlapped with existing allocated buffer, skip it
245 // and find the next range
247 WakeupBufferEnd
-= WakeupBufferSize
;
250 DEBUG ((DEBUG_INFO
, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",
251 WakeupBufferStart
, WakeupBufferSize
));
252 return (UINTN
)WakeupBufferStart
;
259 Hob
.Raw
= GET_NEXT_HOB (Hob
);
266 Get available EfiBootServicesCode memory below 4GB by specified size.
268 This buffer is required to safely transfer AP from real address mode to
269 protected mode or long mode, due to the fact that the buffer returned by
270 GetWakeupBuffer() may be marked as non-executable.
272 @param[in] BufferSize Wakeup transition buffer size.
274 @retval other Return wakeup transition buffer address below 4GB.
275 @retval 0 Cannot find free memory below 4GB.
278 GetModeTransitionBuffer (
283 // PEI phase doesn't need to do such transition. So simply return 0.
289 Checks APs status and updates APs status if needed.
293 CheckAndUpdateApsStatus (
300 Initialize global data for MP support.
302 @param[in] CpuMpData The pointer to CPU MP Data structure.
306 IN CPU_MP_DATA
*CpuMpData
311 SaveCpuMpData (CpuMpData
);
316 Status
= PeiServicesNotifyPpi (&mS3SmmInitDoneNotifyDesc
);
317 ASSERT_EFI_ERROR (Status
);
321 This service executes a caller provided function on all enabled APs.
323 @param[in] Procedure A pointer to the function to be run on
324 enabled APs of the system. See type
326 @param[in] SingleThread If TRUE, then all the enabled APs execute
327 the function specified by Procedure one by
328 one, in ascending order of processor handle
329 number. If FALSE, then all the enabled APs
330 execute the function specified by Procedure
332 @param[in] WaitEvent The event created by the caller with CreateEvent()
333 service. If it is NULL, then execute in
334 blocking mode. BSP waits until all APs finish
335 or TimeoutInMicroSeconds expires. If it's
336 not NULL, then execute in non-blocking mode.
337 BSP requests the function specified by
338 Procedure to be started on all the enabled
339 APs, and go on executing immediately. If
340 all return from Procedure, or TimeoutInMicroSeconds
341 expires, this event is signaled. The BSP
342 can use the CheckEvent() or WaitForEvent()
343 services to check the state of event. Type
344 EFI_EVENT is defined in CreateEvent() in
345 the Unified Extensible Firmware Interface
347 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
348 APs to return from Procedure, either for
349 blocking or non-blocking mode. Zero means
350 infinity. If the timeout expires before
351 all APs return from Procedure, then Procedure
352 on the failed APs is terminated. All enabled
353 APs are available for next function assigned
354 by MpInitLibStartupAllAPs() or
355 MPInitLibStartupThisAP().
356 If the timeout expires in blocking mode,
357 BSP returns EFI_TIMEOUT. If the timeout
358 expires in non-blocking mode, WaitEvent
359 is signaled with SignalEvent().
360 @param[in] ProcedureArgument The parameter passed into Procedure for
362 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,
363 if all APs finish successfully, then its
364 content is set to NULL. If not all APs
365 finish before timeout expires, then its
366 content is set to address of the buffer
367 holding handle numbers of the failed APs.
368 The buffer is allocated by MP Initialization
369 library, and it's the caller's responsibility to
370 free the buffer with FreePool() service.
371 In blocking mode, it is ready for consumption
372 when the call returns. In non-blocking mode,
373 it is ready when WaitEvent is signaled. The
374 list of failed CPU is terminated by
377 @retval EFI_SUCCESS In blocking mode, all APs have finished before
379 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
381 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
382 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
384 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
386 @retval EFI_DEVICE_ERROR Caller processor is AP.
387 @retval EFI_NOT_STARTED No enabled APs exist in the system.
388 @retval EFI_NOT_READY Any enabled APs are busy.
389 @retval EFI_NOT_READY MP Initialize Library is not initialized.
390 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
391 all enabled APs have finished.
392 @retval EFI_INVALID_PARAMETER Procedure is NULL.
397 MpInitLibStartupAllAPs (
398 IN EFI_AP_PROCEDURE Procedure
,
399 IN BOOLEAN SingleThread
,
400 IN EFI_EVENT WaitEvent OPTIONAL
,
401 IN UINTN TimeoutInMicroseconds
,
402 IN VOID
*ProcedureArgument OPTIONAL
,
403 OUT UINTN
**FailedCpuList OPTIONAL
406 if (WaitEvent
!= NULL
) {
407 return EFI_UNSUPPORTED
;
410 return StartupAllAPsWorker (
414 TimeoutInMicroseconds
,
421 This service lets the caller get one enabled AP to execute a caller-provided
424 @param[in] Procedure A pointer to the function to be run on the
425 designated AP of the system. See type
427 @param[in] ProcessorNumber The handle number of the AP. The range is
428 from 0 to the total number of logical
429 processors minus 1. The total number of
430 logical processors can be retrieved by
431 MpInitLibGetNumberOfProcessors().
432 @param[in] WaitEvent The event created by the caller with CreateEvent()
433 service. If it is NULL, then execute in
434 blocking mode. BSP waits until this AP finish
435 or TimeoutInMicroSeconds expires. If it's
436 not NULL, then execute in non-blocking mode.
437 BSP requests the function specified by
438 Procedure to be started on this AP,
439 and go on executing immediately. If this AP
440 return from Procedure or TimeoutInMicroSeconds
441 expires, this event is signaled. The BSP
442 can use the CheckEvent() or WaitForEvent()
443 services to check the state of event. Type
444 EFI_EVENT is defined in CreateEvent() in
445 the Unified Extensible Firmware Interface
447 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
448 this AP to finish this Procedure, either for
449 blocking or non-blocking mode. Zero means
450 infinity. If the timeout expires before
451 this AP returns from Procedure, then Procedure
452 on the AP is terminated. The
453 AP is available for next function assigned
454 by MpInitLibStartupAllAPs() or
455 MpInitLibStartupThisAP().
456 If the timeout expires in blocking mode,
457 BSP returns EFI_TIMEOUT. If the timeout
458 expires in non-blocking mode, WaitEvent
459 is signaled with SignalEvent().
460 @param[in] ProcedureArgument The parameter passed into Procedure on the
462 @param[out] Finished If NULL, this parameter is ignored. In
463 blocking mode, this parameter is ignored.
464 In non-blocking mode, if AP returns from
465 Procedure before the timeout expires, its
466 content is set to TRUE. Otherwise, the
467 value is set to FALSE. The caller can
468 determine if the AP returned from Procedure
469 by evaluating this value.
471 @retval EFI_SUCCESS In blocking mode, specified AP finished before
473 @retval EFI_SUCCESS In non-blocking mode, the function has been
474 dispatched to specified AP.
475 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the
476 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was
478 @retval EFI_UNSUPPORTED WaitEvent is not NULL if non-blocking mode is not
480 @retval EFI_DEVICE_ERROR The calling processor is an AP.
481 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
482 the specified AP has finished.
483 @retval EFI_NOT_READY The specified AP is busy.
484 @retval EFI_NOT_READY MP Initialize Library is not initialized.
485 @retval EFI_NOT_FOUND The processor with the handle specified by
486 ProcessorNumber does not exist.
487 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.
488 @retval EFI_INVALID_PARAMETER Procedure is NULL.
493 MpInitLibStartupThisAP (
494 IN EFI_AP_PROCEDURE Procedure
,
495 IN UINTN ProcessorNumber
,
496 IN EFI_EVENT WaitEvent OPTIONAL
,
497 IN UINTN TimeoutInMicroseconds
,
498 IN VOID
*ProcedureArgument OPTIONAL
,
499 OUT BOOLEAN
*Finished OPTIONAL
502 if (WaitEvent
!= NULL
) {
503 return EFI_UNSUPPORTED
;
506 return StartupThisAPWorker (
510 TimeoutInMicroseconds
,
517 This service switches the requested AP to be the BSP from that point onward.
518 This service changes the BSP for all purposes. This call can only be performed
521 @param[in] ProcessorNumber The handle number of AP that is to become the new
522 BSP. The range is from 0 to the total number of
523 logical processors minus 1. The total number of
524 logical processors can be retrieved by
525 MpInitLibGetNumberOfProcessors().
526 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
527 enabled AP. Otherwise, it will be disabled.
529 @retval EFI_SUCCESS BSP successfully switched.
530 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to
531 this service returning.
532 @retval EFI_UNSUPPORTED Switching the BSP is not supported.
533 @retval EFI_DEVICE_ERROR The calling processor is an AP.
534 @retval EFI_NOT_FOUND The processor with the handle specified by
535 ProcessorNumber does not exist.
536 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or
538 @retval EFI_NOT_READY The specified AP is busy.
539 @retval EFI_NOT_READY MP Initialize Library is not initialized.
545 IN UINTN ProcessorNumber
,
546 IN BOOLEAN EnableOldBSP
549 return SwitchBSPWorker (ProcessorNumber
, EnableOldBSP
);
553 This service lets the caller enable or disable an AP from this point onward.
554 This service may only be called from the BSP.
556 @param[in] ProcessorNumber The handle number of AP.
557 The range is from 0 to the total number of
558 logical processors minus 1. The total number of
559 logical processors can be retrieved by
560 MpInitLibGetNumberOfProcessors().
561 @param[in] EnableAP Specifies the new state for the processor for
562 enabled, FALSE for disabled.
563 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
564 the new health status of the AP. This flag
565 corresponds to StatusFlag defined in
566 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only
567 the PROCESSOR_HEALTH_STATUS_BIT is used. All other
568 bits are ignored. If it is NULL, this parameter
571 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
572 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed
573 prior to this service returning.
574 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.
575 @retval EFI_DEVICE_ERROR The calling processor is an AP.
576 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber
578 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.
579 @retval EFI_NOT_READY MP Initialize Library is not initialized.
584 MpInitLibEnableDisableAP (
585 IN UINTN ProcessorNumber
,
587 IN UINT32
*HealthFlag OPTIONAL
590 return EnableDisableApWorker (ProcessorNumber
, EnableAP
, HealthFlag
);