2 CPU PEI Module installs CPU Multiple Processor PPI.
4 Copyright (c) 2015 - 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.
18 // Global Descriptor Table (GDT)
20 GLOBAL_REMOVE_IF_UNREFERENCED IA32_GDT mGdtEntries
[] = {
21 /* selector { Global Segment Descriptor } */
22 /* 0x00 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //null descriptor
23 /* 0x08 */ {{0xffff, 0, 0, 0x2, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //linear data segment descriptor
24 /* 0x10 */ {{0xffff, 0, 0, 0xf, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //linear code segment descriptor
25 /* 0x18 */ {{0xffff, 0, 0, 0x3, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system data segment descriptor
26 /* 0x20 */ {{0xffff, 0, 0, 0xa, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system code segment descriptor
27 /* 0x28 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //spare segment descriptor
28 /* 0x30 */ {{0xffff, 0, 0, 0x2, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system data segment descriptor
29 /* 0x38 */ {{0xffff, 0, 0, 0xa, 1, 0, 1, 0xf, 0, 1, 0, 1, 0}}, //system code segment descriptor
30 /* 0x40 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //spare segment descriptor
36 GLOBAL_REMOVE_IF_UNREFERENCED IA32_DESCRIPTOR mGdt
= {
37 sizeof (mGdtEntries
) - 1,
41 GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList
= {
42 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
43 &gEfiEndOfPeiSignalPpiGuid
,
48 Sort the APIC ID of all processors.
50 This function sorts the APIC ID of all processors so that processor number is
51 assigned in the ascending order of APIC ID which eases MP debugging.
53 @param PeiCpuMpData Pointer to PEI CPU MP Data
57 IN PEI_CPU_MP_DATA
*PeiCpuMpData
67 ApCount
= PeiCpuMpData
->CpuCount
- 1;
70 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
73 // Sort key is the hardware default APIC ID
75 ApicId
= PeiCpuMpData
->CpuData
[Index1
].ApicId
;
76 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
77 if (ApicId
> PeiCpuMpData
->CpuData
[Index2
].ApicId
) {
79 ApicId
= PeiCpuMpData
->CpuData
[Index2
].ApicId
;
82 if (Index3
!= Index1
) {
83 CopyMem (&CpuData
, &PeiCpuMpData
->CpuData
[Index3
], sizeof (PEI_CPU_DATA
));
85 &PeiCpuMpData
->CpuData
[Index3
],
86 &PeiCpuMpData
->CpuData
[Index1
],
89 CopyMem (&PeiCpuMpData
->CpuData
[Index1
], &CpuData
, sizeof (PEI_CPU_DATA
));
94 // Get the processor number for the BSP
96 ApicId
= GetInitialApicId ();
97 for (Index1
= 0; Index1
< PeiCpuMpData
->CpuCount
; Index1
++) {
98 if (PeiCpuMpData
->CpuData
[Index1
].ApicId
== ApicId
) {
99 PeiCpuMpData
->BspNumber
= (UINT32
) Index1
;
107 Enable x2APIC mode on APs.
109 @param Buffer Pointer to private data buffer.
117 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
123 @param MonitorFilterSize Returns the largest monitor-line size in bytes.
125 @return The AP loop mode.
129 OUT UINT16
*MonitorFilterSize
137 ASSERT (MonitorFilterSize
!= NULL
);
139 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
140 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
141 if (ApLoopMode
== ApInMwaitLoop
) {
142 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &RegEcx
, NULL
);
143 if ((RegEcx
& BIT3
) == 0) {
145 // If processor does not support MONITOR/MWAIT feature
146 // by CPUID.[EAX=01H]:ECX.BIT3, force AP in Hlt-loop mode
148 ApLoopMode
= ApInHltLoop
;
152 if (ApLoopMode
== ApInHltLoop
) {
153 *MonitorFilterSize
= 0;
154 } else if (ApLoopMode
== ApInRunLoop
) {
155 *MonitorFilterSize
= sizeof (UINT32
);
156 } else if (ApLoopMode
== ApInMwaitLoop
) {
158 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
159 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
161 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &RegEbx
, NULL
, &RegEdx
);
162 *MonitorFilterSize
= RegEbx
& 0xFFFF;
169 Get CPU MP Data pointer from the Guided HOB.
171 @return Pointer to Pointer to PEI CPU MP Data
178 EFI_HOB_GUID_TYPE
*GuidHob
;
180 PEI_CPU_MP_DATA
*CpuMpData
;
183 GuidHob
= GetFirstGuidHob (&gEfiCallerIdGuid
);
184 if (GuidHob
!= NULL
) {
185 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
186 CpuMpData
= (PEI_CPU_MP_DATA
*)(*(UINTN
*)DataInHob
);
188 ASSERT (CpuMpData
!= NULL
);
193 Save the volatile registers required to be restored following INIT IPI.
195 @param VolatileRegisters Returns buffer saved the volatile resisters
198 SaveVolatileRegisters (
199 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
204 VolatileRegisters
->Cr0
= AsmReadCr0 ();
205 VolatileRegisters
->Cr3
= AsmReadCr3 ();
206 VolatileRegisters
->Cr4
= AsmReadCr4 ();
208 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &RegEdx
);
209 if ((RegEdx
& BIT2
) != 0) {
211 // If processor supports Debugging Extensions feature
212 // by CPUID.[EAX=01H]:EDX.BIT2
214 VolatileRegisters
->Dr0
= AsmReadDr0 ();
215 VolatileRegisters
->Dr1
= AsmReadDr1 ();
216 VolatileRegisters
->Dr2
= AsmReadDr2 ();
217 VolatileRegisters
->Dr3
= AsmReadDr3 ();
218 VolatileRegisters
->Dr6
= AsmReadDr6 ();
219 VolatileRegisters
->Dr7
= AsmReadDr7 ();
224 Restore the volatile registers following INIT IPI.
226 @param VolatileRegisters Pointer to volatile resisters
227 @param IsRestoreDr TRUE: Restore DRx if supported
228 FALSE: Do not restore DRx
231 RestoreVolatileRegisters (
232 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
233 IN BOOLEAN IsRestoreDr
238 AsmWriteCr0 (VolatileRegisters
->Cr0
);
239 AsmWriteCr3 (VolatileRegisters
->Cr3
);
240 AsmWriteCr4 (VolatileRegisters
->Cr4
);
243 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &RegEdx
);
244 if ((RegEdx
& BIT2
) != 0) {
246 // If processor supports Debugging Extensions feature
247 // by CPUID.[EAX=01H]:EDX.BIT2
249 AsmWriteDr0 (VolatileRegisters
->Dr0
);
250 AsmWriteDr1 (VolatileRegisters
->Dr1
);
251 AsmWriteDr2 (VolatileRegisters
->Dr2
);
252 AsmWriteDr3 (VolatileRegisters
->Dr3
);
253 AsmWriteDr6 (VolatileRegisters
->Dr6
);
254 AsmWriteDr7 (VolatileRegisters
->Dr7
);
260 This function will be called from AP reset code if BSP uses WakeUpAP.
262 @param ExchangeInfo Pointer to the MP exchange info buffer
263 @param NumApsExecuting Number of current executing AP
268 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
269 IN UINTN NumApsExecuting
272 PEI_CPU_MP_DATA
*PeiCpuMpData
;
273 UINTN ProcessorNumber
;
274 EFI_AP_PROCEDURE Procedure
;
276 volatile UINT32
*ApStartupSignalBuffer
;
278 PeiCpuMpData
= ExchangeInfo
->PeiCpuMpData
;
280 if (PeiCpuMpData
->InitFlag
) {
281 ProcessorNumber
= NumApsExecuting
;
283 // Sync BSP's Control registers to APs
285 RestoreVolatileRegisters (&PeiCpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
287 // This is first time AP wakeup, get BIST information from AP stack
289 BistData
= *(UINTN
*) (PeiCpuMpData
->Buffer
+ ProcessorNumber
* PeiCpuMpData
->CpuApStackSize
- sizeof (UINTN
));
290 PeiCpuMpData
->CpuData
[ProcessorNumber
].Health
.Uint32
= (UINT32
) BistData
;
291 PeiCpuMpData
->CpuData
[ProcessorNumber
].ApicId
= GetInitialApicId ();
292 if (PeiCpuMpData
->CpuData
[ProcessorNumber
].ApicId
>= 0xFF) {
294 // Set x2APIC mode if there are any logical processor reporting
295 // an APIC ID of 255 or greater.
297 AcquireSpinLock(&PeiCpuMpData
->MpLock
);
298 PeiCpuMpData
->X2ApicEnable
= TRUE
;
299 ReleaseSpinLock(&PeiCpuMpData
->MpLock
);
302 // Sync BSP's Mtrr table to all wakeup APs and load microcode on APs.
304 MtrrSetAllMtrrs (&PeiCpuMpData
->MtrrTable
);
305 MicrocodeDetect (PeiCpuMpData
);
306 PeiCpuMpData
->CpuData
[ProcessorNumber
].State
= CpuStateIdle
;
309 // Execute AP function if AP is not disabled
311 GetProcessorNumber (PeiCpuMpData
, &ProcessorNumber
);
312 if (PeiCpuMpData
->ApLoopMode
== ApInHltLoop
) {
314 // Restore AP's volatile registers saved
316 RestoreVolatileRegisters (&PeiCpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
319 if ((PeiCpuMpData
->CpuData
[ProcessorNumber
].State
!= CpuStateDisabled
) &&
320 (PeiCpuMpData
->ApFunction
!= 0)) {
321 PeiCpuMpData
->CpuData
[ProcessorNumber
].State
= CpuStateBusy
;
322 Procedure
= (EFI_AP_PROCEDURE
)(UINTN
)PeiCpuMpData
->ApFunction
;
324 // Invoke AP function here
326 Procedure ((VOID
*)(UINTN
)PeiCpuMpData
->ApFunctionArgument
);
328 // Re-get the processor number due to BSP/AP maybe exchange in AP function
330 GetProcessorNumber (PeiCpuMpData
, &ProcessorNumber
);
331 PeiCpuMpData
->CpuData
[ProcessorNumber
].State
= CpuStateIdle
;
336 // AP finished executing C code
338 InterlockedIncrement ((UINT32
*)&PeiCpuMpData
->FinishedCount
);
341 // Place AP is specified loop mode
343 if (PeiCpuMpData
->ApLoopMode
== ApInHltLoop
) {
345 // Save AP volatile registers
347 SaveVolatileRegisters (&PeiCpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
349 // Place AP in Hlt-loop
352 DisableInterrupts ();
357 ApStartupSignalBuffer
= PeiCpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
359 DisableInterrupts ();
360 if (PeiCpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
362 // Place AP in Mwait-loop
364 AsmMonitor ((UINTN
)ApStartupSignalBuffer
, 0, 0);
365 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
367 // If AP start-up signal is not set, place AP into
368 // the maximum C-state
370 AsmMwait (PeiCpuMpData
->ApTargetCState
<< 4, 0);
372 } else if (PeiCpuMpData
->ApLoopMode
== ApInRunLoop
) {
374 // Place AP in Run-loop
382 // If AP start-up signal is written, AP is waken up
383 // otherwise place AP in loop again
385 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
387 // Clear AP start-up signal when AP waken up
389 InterlockedCompareExchange32 (
390 (UINT32
*)ApStartupSignalBuffer
,
401 Write AP start-up signal to wakeup AP.
403 @param ApStartupSignalBuffer Pointer to AP wakeup signal
407 IN
volatile UINT32
*ApStartupSignalBuffer
410 *ApStartupSignalBuffer
= WAKEUP_AP_SIGNAL
;
412 // If AP is waken up, StartupApSignal should be cleared.
413 // Otherwise, write StartupApSignal again till AP waken up.
415 while (InterlockedCompareExchange32 (
416 (UINT32
*)ApStartupSignalBuffer
,
425 This function will be called by BSP to wakeup AP.
427 @param PeiCpuMpData Pointer to PEI CPU MP Data
428 @param Broadcast TRUE: Send broadcast IPI to all APs
429 FALSE: Send IPI to AP by ApicId
430 @param ProcessorNumber The handle number of specified processor
431 @param Procedure The function to be invoked by AP
432 @param ProcedureArgument The argument to be passed into AP function
436 IN PEI_CPU_MP_DATA
*PeiCpuMpData
,
437 IN BOOLEAN Broadcast
,
438 IN UINTN ProcessorNumber
,
439 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
440 IN VOID
*ProcedureArgument OPTIONAL
443 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
446 PeiCpuMpData
->ApFunction
= (UINTN
) Procedure
;
447 PeiCpuMpData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
448 PeiCpuMpData
->FinishedCount
= 0;
450 ExchangeInfo
= PeiCpuMpData
->MpCpuExchangeInfo
;
451 ExchangeInfo
->Lock
= 0;
452 ExchangeInfo
->StackStart
= PeiCpuMpData
->Buffer
;
453 ExchangeInfo
->StackSize
= PeiCpuMpData
->CpuApStackSize
;
454 ExchangeInfo
->BufferStart
= PeiCpuMpData
->WakeupBuffer
;
455 ExchangeInfo
->PmodeOffset
= PeiCpuMpData
->AddressMap
.PModeEntryOffset
;
456 ExchangeInfo
->LmodeOffset
= PeiCpuMpData
->AddressMap
.LModeEntryOffset
;
457 ExchangeInfo
->Cr3
= AsmReadCr3 ();
458 ExchangeInfo
->CodeSegment
= AsmReadCs ();
459 ExchangeInfo
->DataSegment
= AsmReadDs ();
460 ExchangeInfo
->CFunction
= (UINTN
) ApCFunction
;
461 ExchangeInfo
->NumApsExecuting
= 0;
462 ExchangeInfo
->PeiCpuMpData
= PeiCpuMpData
;
465 // Get the BSP's data of GDT and IDT
467 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
468 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
470 if (PeiCpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
472 // Get AP target C-state each time when waking up AP,
473 // for it maybe updated by platform again
475 PeiCpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
479 // Wakeup APs per AP loop state
481 if (PeiCpuMpData
->ApLoopMode
== ApInHltLoop
|| PeiCpuMpData
->InitFlag
) {
483 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
486 PeiCpuMpData
->CpuData
[ProcessorNumber
].ApicId
,
487 (UINT32
) ExchangeInfo
->BufferStart
490 } else if ((PeiCpuMpData
->ApLoopMode
== ApInMwaitLoop
) ||
491 (PeiCpuMpData
->ApLoopMode
== ApInRunLoop
)) {
493 for (Index
= 0; Index
< PeiCpuMpData
->CpuCount
; Index
++) {
494 if (Index
!= PeiCpuMpData
->BspNumber
) {
495 WriteStartupSignal (PeiCpuMpData
->CpuData
[Index
].StartupApSignal
);
499 WriteStartupSignal (PeiCpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
);
508 Get available system memory below 1MB by specified size.
510 @param WakeupBufferSize Wakeup buffer size required
512 @retval other Return wakeup buffer address below 1MB.
513 @retval -1 Cannot find free memory below 1MB.
517 IN UINTN WakeupBufferSize
520 EFI_PEI_HOB_POINTERS Hob
;
521 UINTN WakeupBufferStart
;
522 UINTN WakeupBufferEnd
;
525 // Get the HOB list for processing
527 Hob
.Raw
= GetHobList ();
530 // Collect memory ranges
532 while (!END_OF_HOB_LIST (Hob
)) {
533 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
534 if ((Hob
.ResourceDescriptor
->PhysicalStart
< BASE_1MB
) &&
535 (Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
) &&
536 ((Hob
.ResourceDescriptor
->ResourceAttribute
&
537 (EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED
|
538 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED
|
539 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED
543 // Need memory under 1MB to be collected here
545 WakeupBufferEnd
= (UINTN
) (Hob
.ResourceDescriptor
->PhysicalStart
+ Hob
.ResourceDescriptor
->ResourceLength
);
546 if (WakeupBufferEnd
> BASE_1MB
) {
548 // Wakeup buffer should be under 1MB
550 WakeupBufferEnd
= BASE_1MB
;
553 // Wakeup buffer should be aligned on 4KB
555 WakeupBufferStart
= (WakeupBufferEnd
- WakeupBufferSize
) & ~(SIZE_4KB
- 1);
556 if (WakeupBufferStart
< Hob
.ResourceDescriptor
->PhysicalStart
) {
560 // Create a memory allocation HOB.
562 BuildMemoryAllocationHob (
567 return WakeupBufferStart
;
573 Hob
.Raw
= GET_NEXT_HOB (Hob
);
580 Get available system memory below 1MB by specified size.
582 @param PeiCpuMpData Pointer to PEI CPU MP Data
585 BackupAndPrepareWakeupBuffer(
586 IN PEI_CPU_MP_DATA
*PeiCpuMpData
590 (VOID
*) PeiCpuMpData
->BackupBuffer
,
591 (VOID
*) PeiCpuMpData
->WakeupBuffer
,
592 PeiCpuMpData
->BackupBufferSize
595 (VOID
*) PeiCpuMpData
->WakeupBuffer
,
596 (VOID
*) PeiCpuMpData
->AddressMap
.RendezvousFunnelAddress
,
597 PeiCpuMpData
->AddressMap
.RendezvousFunnelSize
602 Restore wakeup buffer data.
604 @param PeiCpuMpData Pointer to PEI CPU MP Data
608 IN PEI_CPU_MP_DATA
*PeiCpuMpData
611 CopyMem ((VOID
*) PeiCpuMpData
->WakeupBuffer
, (VOID
*) PeiCpuMpData
->BackupBuffer
, PeiCpuMpData
->BackupBufferSize
);
615 This function will get CPU count in the system.
617 @param PeiCpuMpData Pointer to PEI CPU MP Data
619 @return AP processor count
622 CountProcessorNumber (
623 IN PEI_CPU_MP_DATA
*PeiCpuMpData
627 // Load Microcode on BSP
629 MicrocodeDetect (PeiCpuMpData
);
631 // Store BSP's MTRR setting
633 MtrrGetAllMtrrs (&PeiCpuMpData
->MtrrTable
);
636 // Only perform AP detection if PcdCpuMaxLogicalProcessorNumber is greater than 1
638 if (PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) > 1) {
640 // Send 1st broadcast IPI to APs to wakeup APs
642 PeiCpuMpData
->InitFlag
= TRUE
;
643 PeiCpuMpData
->X2ApicEnable
= FALSE
;
644 WakeUpAP (PeiCpuMpData
, TRUE
, 0, NULL
, NULL
);
646 // Wait for AP task to complete and then exit.
648 MicroSecondDelay (PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
));
649 PeiCpuMpData
->InitFlag
= FALSE
;
650 PeiCpuMpData
->CpuCount
+= (UINT32
)PeiCpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
;
651 ASSERT (PeiCpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
653 // Wait for all APs finished the initialization
655 while (PeiCpuMpData
->FinishedCount
< (PeiCpuMpData
->CpuCount
- 1)) {
659 if (PeiCpuMpData
->X2ApicEnable
) {
660 DEBUG ((EFI_D_INFO
, "Force x2APIC mode!\n"));
662 // Wakeup all APs to enable x2APIC mode
664 WakeUpAP (PeiCpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
);
666 // Wait for all known APs finished
668 while (PeiCpuMpData
->FinishedCount
< (PeiCpuMpData
->CpuCount
- 1)) {
672 // Enable x2APIC on BSP
674 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
676 DEBUG ((EFI_D_INFO
, "APIC MODE is %d\n", GetApicMode ()));
678 // Sort BSP/Aps by CPU APIC ID in ascending order
680 SortApicId (PeiCpuMpData
);
683 DEBUG ((EFI_D_INFO
, "CpuMpPei: Find %d processors in system.\n", PeiCpuMpData
->CpuCount
));
684 return PeiCpuMpData
->CpuCount
;
688 Prepare for AP wakeup buffer and copy AP reset code into it.
690 Get wakeup buffer below 1MB. Allocate memory for CPU MP Data and APs Stack.
692 @return Pointer to PEI CPU MP Data
695 PrepareAPStartupVector (
701 PEI_CPU_MP_DATA
*PeiCpuMpData
;
702 EFI_PHYSICAL_ADDRESS Buffer
;
705 UINTN WakeupBufferSize
;
706 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
708 UINT16 MonitorFilterSize
;
709 UINT8
*MonitorBuffer
;
712 AsmGetAddressMap (&AddressMap
);
713 WakeupBufferSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
714 WakeupBuffer
= GetWakeupBuffer ((WakeupBufferSize
+ SIZE_4KB
- 1) & ~(SIZE_4KB
- 1));
715 ASSERT (WakeupBuffer
!= (UINTN
) -1);
716 DEBUG ((EFI_D_INFO
, "CpuMpPei: WakeupBuffer = 0x%x\n", WakeupBuffer
));
719 // Allocate Pages for APs stack, CPU MP Data, backup buffer for wakeup buffer,
720 // and monitor buffer if required.
722 MaxCpuCount
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
723 BufferSize
= PcdGet32 (PcdCpuApStackSize
) * MaxCpuCount
+ sizeof (PEI_CPU_MP_DATA
)
724 + WakeupBufferSize
+ sizeof (PEI_CPU_DATA
) * MaxCpuCount
;
725 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
726 BufferSize
+= MonitorFilterSize
* MaxCpuCount
;
727 Status
= PeiServicesAllocatePages (
729 EFI_SIZE_TO_PAGES (BufferSize
),
732 ASSERT_EFI_ERROR (Status
);
734 PeiCpuMpData
= (PEI_CPU_MP_DATA
*) (UINTN
) (Buffer
+ PcdGet32 (PcdCpuApStackSize
) * MaxCpuCount
);
735 PeiCpuMpData
->Buffer
= (UINTN
) Buffer
;
736 PeiCpuMpData
->CpuApStackSize
= PcdGet32 (PcdCpuApStackSize
);
737 PeiCpuMpData
->WakeupBuffer
= WakeupBuffer
;
738 PeiCpuMpData
->BackupBuffer
= (UINTN
)PeiCpuMpData
+ sizeof (PEI_CPU_MP_DATA
);
739 PeiCpuMpData
->BackupBufferSize
= WakeupBufferSize
;
740 PeiCpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
) (WakeupBuffer
+ AddressMap
.RendezvousFunnelSize
);
742 PeiCpuMpData
->CpuCount
= 1;
743 PeiCpuMpData
->BspNumber
= 0;
744 PeiCpuMpData
->CpuData
= (PEI_CPU_DATA
*) (PeiCpuMpData
->BackupBuffer
+
745 PeiCpuMpData
->BackupBufferSize
);
746 PeiCpuMpData
->CpuData
[0].ApicId
= GetInitialApicId ();
747 PeiCpuMpData
->CpuData
[0].Health
.Uint32
= 0;
748 PeiCpuMpData
->EndOfPeiFlag
= FALSE
;
749 InitializeSpinLock(&PeiCpuMpData
->MpLock
);
750 SaveVolatileRegisters (&PeiCpuMpData
->CpuData
[0].VolatileRegisters
);
751 CopyMem (&PeiCpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
753 // Initialize AP loop mode
755 PeiCpuMpData
->ApLoopMode
= ApLoopMode
;
756 DEBUG ((EFI_D_INFO
, "AP Loop Mode is %d\n", PeiCpuMpData
->ApLoopMode
));
757 MonitorBuffer
= (UINT8
*)(PeiCpuMpData
->CpuData
+ MaxCpuCount
);
758 if (PeiCpuMpData
->ApLoopMode
!= ApInHltLoop
) {
760 // Set up APs wakeup signal buffer
762 for (Index
= 0; Index
< MaxCpuCount
; Index
++) {
763 PeiCpuMpData
->CpuData
[Index
].StartupApSignal
=
764 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
768 // Backup original data and copy AP reset code in it
770 BackupAndPrepareWakeupBuffer(PeiCpuMpData
);
776 Notify function on End Of Pei PPI.
778 On S3 boot, this function will restore wakeup buffer data.
779 On normal boot, this function will flag wakeup buffer to be un-used type.
781 @param PeiServices The pointer to the PEI Services Table.
782 @param NotifyDescriptor Address of the notification descriptor data structure.
783 @param Ppi Address of the PPI that was installed.
785 @retval EFI_SUCCESS When everything is OK.
790 CpuMpEndOfPeiCallback (
791 IN EFI_PEI_SERVICES
**PeiServices
,
792 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
797 EFI_BOOT_MODE BootMode
;
798 PEI_CPU_MP_DATA
*PeiCpuMpData
;
799 EFI_PEI_HOB_POINTERS Hob
;
800 EFI_HOB_MEMORY_ALLOCATION
*MemoryHob
;
802 DEBUG ((EFI_D_INFO
, "CpuMpPei: CpuMpEndOfPeiCallback () invoked\n"));
804 Status
= PeiServicesGetBootMode (&BootMode
);
805 ASSERT_EFI_ERROR (Status
);
807 PeiCpuMpData
= GetMpHobData ();
808 ASSERT (PeiCpuMpData
!= NULL
);
810 if (BootMode
!= BOOT_ON_S3_RESUME
) {
812 // Get the HOB list for processing
814 Hob
.Raw
= GetHobList ();
816 // Collect memory ranges
818 while (!END_OF_HOB_LIST (Hob
)) {
819 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_MEMORY_ALLOCATION
) {
820 MemoryHob
= Hob
.MemoryAllocation
;
821 if(MemoryHob
->AllocDescriptor
.MemoryBaseAddress
== PeiCpuMpData
->WakeupBuffer
) {
823 // Flag this HOB type to un-used
825 GET_HOB_TYPE (Hob
) = EFI_HOB_TYPE_UNUSED
;
829 Hob
.Raw
= GET_NEXT_HOB (Hob
);
832 RestoreWakeupBuffer (PeiCpuMpData
);
833 PeiCpuMpData
->EndOfPeiFlag
= TRUE
;
839 The Entry point of the MP CPU PEIM.
841 This function will wakeup APs and collect CPU AP count and install the
844 @param FileHandle Handle of the file being invoked.
845 @param PeiServices Describes the list of possible PEI Services.
847 @retval EFI_SUCCESS MpServicePpi is installed successfully.
853 IN EFI_PEI_FILE_HANDLE FileHandle
,
854 IN CONST EFI_PEI_SERVICES
**PeiServices
858 PEI_CPU_MP_DATA
*PeiCpuMpData
;
859 EFI_VECTOR_HANDOFF_INFO
*VectorInfo
;
860 EFI_PEI_VECTOR_HANDOFF_INFO_PPI
*VectorHandoffInfoPpi
;
863 // Get Vector Hand-off Info PPI
866 Status
= PeiServicesLocatePpi (
867 &gEfiVectorHandoffInfoPpiGuid
,
870 (VOID
**)&VectorHandoffInfoPpi
872 if (Status
== EFI_SUCCESS
) {
873 VectorInfo
= VectorHandoffInfoPpi
->Info
;
875 Status
= InitializeCpuExceptionHandlers (VectorInfo
);
876 ASSERT_EFI_ERROR (Status
);
878 // Get wakeup buffer and copy AP reset code in it
880 PeiCpuMpData
= PrepareAPStartupVector ();
882 // Count processor number and collect processor information
884 CountProcessorNumber (PeiCpuMpData
);
886 // Build location of PEI CPU MP DATA buffer in HOB
890 (VOID
*)&PeiCpuMpData
,
894 // Update and publish CPU BIST information
896 CollectBistDataFromPpi (PeiServices
, PeiCpuMpData
);
898 // register an event for EndOfPei
900 Status
= PeiServicesNotifyPpi (&mNotifyList
);
901 ASSERT_EFI_ERROR (Status
);
903 // Install CPU MP PPI
905 Status
= PeiServicesInstallPpi(&mPeiCpuMpPpiDesc
);
906 ASSERT_EFI_ERROR (Status
);