2 CPU PEI Module installs CPU Multiple Processor PPI.
4 Copyright (c) 2015, 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,
42 Sort the APIC ID of all processors.
44 This function sorts the APIC ID of all processors so that processor number is
45 assigned in the ascending order of APIC ID which eases MP debugging.
47 @param PeiCpuMpData Pointer to PEI CPU MP Data
51 IN PEI_CPU_MP_DATA
*PeiCpuMpData
58 EFI_HEALTH_FLAGS Health
;
61 ApCount
= PeiCpuMpData
->CpuCount
- 1;
64 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
67 // Sort key is the hardware default APIC ID
69 ApicId
= PeiCpuMpData
->CpuData
[Index1
].ApicId
;
70 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
71 if (ApicId
> PeiCpuMpData
->CpuData
[Index2
].ApicId
) {
73 ApicId
= PeiCpuMpData
->CpuData
[Index2
].ApicId
;
76 if (Index3
!= Index1
) {
77 PeiCpuMpData
->CpuData
[Index3
].ApicId
= PeiCpuMpData
->CpuData
[Index1
].ApicId
;
78 PeiCpuMpData
->CpuData
[Index1
].ApicId
= ApicId
;
79 Health
= PeiCpuMpData
->CpuData
[Index3
].Health
;
80 PeiCpuMpData
->CpuData
[Index3
].Health
= PeiCpuMpData
->CpuData
[Index1
].Health
;
81 PeiCpuMpData
->CpuData
[Index1
].Health
= Health
;
86 // Get the processor number for the BSP
88 ApicId
= GetInitialApicId ();
89 for (Index1
= 0; Index1
< PeiCpuMpData
->CpuCount
; Index1
++) {
90 if (PeiCpuMpData
->CpuData
[Index1
].ApicId
== ApicId
) {
91 PeiCpuMpData
->BspNumber
= (UINT32
) Index1
;
99 Get CPU MP Data pointer from the Guided HOB.
101 @return Pointer to Pointer to PEI CPU MP Data
108 EFI_HOB_GUID_TYPE
*GuidHob
;
110 PEI_CPU_MP_DATA
*CpuMpData
;
113 GuidHob
= GetFirstGuidHob (&gEfiCallerIdGuid
);
114 if (GuidHob
!= NULL
) {
115 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
116 CpuMpData
= (PEI_CPU_MP_DATA
*)(*(UINTN
*)DataInHob
);
118 ASSERT (CpuMpData
!= NULL
);
123 This function will be called from AP reset code if BSP uses WakeUpAP.
125 @param ExchangeInfo Pointer to the MP exchange info buffer
126 @param NumApsExecuting Number of curret executing AP
131 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
132 IN UINTN NumApsExecuting
135 PEI_CPU_MP_DATA
*PeiCpuMpData
;
136 UINTN ProcessorNumber
;
137 EFI_AP_PROCEDURE Procedure
;
140 PeiCpuMpData
= ExchangeInfo
->PeiCpuMpData
;
141 if (PeiCpuMpData
->InitFlag
) {
143 // This is first time AP wakeup, get BIST inforamtion from AP stack
145 BistData
= *(UINTN
*) (PeiCpuMpData
->Buffer
+ NumApsExecuting
* PeiCpuMpData
->CpuApStackSize
- sizeof (UINTN
));
146 PeiCpuMpData
->CpuData
[NumApsExecuting
].ApicId
= GetInitialApicId ();
147 PeiCpuMpData
->CpuData
[NumApsExecuting
].Health
.Uint32
= (UINT32
) BistData
;
149 // Sync BSP's Mtrr table to all wakeup APs and load microcode on APs.
151 MtrrSetAllMtrrs (&PeiCpuMpData
->MtrrTable
);
155 // Execute AP function if AP is not disabled
157 GetProcessorNumber (PeiCpuMpData
, &ProcessorNumber
);
158 if ((PeiCpuMpData
->CpuData
[ProcessorNumber
].State
!= CpuStateDisabled
) &&
159 (PeiCpuMpData
->ApFunction
!= 0)) {
160 PeiCpuMpData
->CpuData
[ProcessorNumber
].State
= CpuStateBusy
;
161 Procedure
= (EFI_AP_PROCEDURE
)(UINTN
)PeiCpuMpData
->ApFunction
;
162 Procedure ((VOID
*)(UINTN
)PeiCpuMpData
->ApFunctionArgument
);
163 PeiCpuMpData
->CpuData
[ProcessorNumber
].State
= CpuStateIdle
;
168 // AP finished executing C code
170 InterlockedIncrement ((UINT32
*)&PeiCpuMpData
->FinishedCount
);
175 This function will be called by BSP to wakeup AP.
177 @param PeiCpuMpData Pointer to PEI CPU MP Data
178 @param Broadcast TRUE: Send broadcast IPI to all APs
179 FALSE: Send IPI to AP by ApicId
180 @param ApicId Apic ID for the processor to be waked
181 @param Procedure The function to be invoked by AP
182 @param ProcedureArgument The argument to be passed into AP function
186 IN PEI_CPU_MP_DATA
*PeiCpuMpData
,
187 IN BOOLEAN Broadcast
,
189 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
190 IN VOID
*ProcedureArgument OPTIONAL
193 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
195 PeiCpuMpData
->ApFunction
= (UINTN
) Procedure
;
196 PeiCpuMpData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
197 PeiCpuMpData
->FinishedCount
= 0;
199 ExchangeInfo
= PeiCpuMpData
->MpCpuExchangeInfo
;
200 ExchangeInfo
->Lock
= 0;
201 ExchangeInfo
->StackStart
= PeiCpuMpData
->Buffer
;
202 ExchangeInfo
->StackSize
= PeiCpuMpData
->CpuApStackSize
;
203 ExchangeInfo
->BufferStart
= PeiCpuMpData
->WakeupBuffer
;
204 ExchangeInfo
->PmodeOffset
= PeiCpuMpData
->AddressMap
.PModeEntryOffset
;
205 ExchangeInfo
->LmodeOffset
= PeiCpuMpData
->AddressMap
.LModeEntryOffset
;
206 ExchangeInfo
->Cr3
= AsmReadCr3 ();
207 ExchangeInfo
->CFunction
= (UINTN
) ApCFunction
;
208 ExchangeInfo
->NumApsExecuting
= 0;
209 ExchangeInfo
->PeiCpuMpData
= PeiCpuMpData
;
212 // Get the BSP's data of GDT and IDT
214 CopyMem ((VOID
*)&ExchangeInfo
->GdtrProfile
, &mGdt
, sizeof(mGdt
));
215 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
218 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
220 SendInitSipiSipi (ApicId
, (UINT32
) ExchangeInfo
->BufferStart
);
227 Get available system memory below 1MB by specified size.
229 @param WakeupBufferSize Wakeup buffer size required
231 @retval other Return wakeup buffer address below 1MB.
232 @retval -1 Cannot find free memory below 1MB.
236 IN UINTN WakeupBufferSize
239 EFI_PEI_HOB_POINTERS Hob
;
240 UINTN WakeupBufferStart
;
241 UINTN WakeupBufferEnd
;
244 // Get the HOB list for processing
246 Hob
.Raw
= GetHobList ();
249 // Collect memory ranges
251 while (!END_OF_HOB_LIST (Hob
)) {
252 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
253 if ((Hob
.ResourceDescriptor
->PhysicalStart
< BASE_1MB
) &&
254 (Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
) &&
255 ((Hob
.ResourceDescriptor
->ResourceAttribute
&
256 (EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED
|
257 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED
|
258 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED
262 // Need memory under 1MB to be collected here
264 WakeupBufferEnd
= (UINTN
) (Hob
.ResourceDescriptor
->PhysicalStart
+ Hob
.ResourceDescriptor
->ResourceLength
);
265 if (WakeupBufferEnd
> BASE_1MB
) {
267 // Wakeup buffer should be under 1MB
269 WakeupBufferEnd
= BASE_1MB
;
272 // Wakeup buffer should be aligned on 4KB
274 WakeupBufferStart
= (WakeupBufferEnd
- WakeupBufferSize
) & ~(SIZE_4KB
- 1);
275 if (WakeupBufferStart
< Hob
.ResourceDescriptor
->PhysicalStart
) {
279 // Create a memory allocation HOB.
281 BuildMemoryAllocationHob (
286 return WakeupBufferStart
;
292 Hob
.Raw
= GET_NEXT_HOB (Hob
);
299 Get available system memory below 1MB by specified size.
301 @param PeiCpuMpData Pointer to PEI CPU MP Data
304 BackupAndPrepareWakeupBuffer(
305 IN PEI_CPU_MP_DATA
*PeiCpuMpData
309 (VOID
*) PeiCpuMpData
->BackupBuffer
,
310 (VOID
*) PeiCpuMpData
->WakeupBuffer
,
311 PeiCpuMpData
->BackupBufferSize
314 (VOID
*) PeiCpuMpData
->WakeupBuffer
,
315 (VOID
*) PeiCpuMpData
->AddressMap
.RendezvousFunnelAddress
,
316 PeiCpuMpData
->AddressMap
.RendezvousFunnelSize
320 This function will get CPU count in the system.
322 @param PeiCpuMpData Pointer to PEI CPU MP Data
324 @return AP processor count
327 CountProcessorNumber (
328 IN PEI_CPU_MP_DATA
*PeiCpuMpData
332 // Load Microcode on BSP
336 // Store BSP's MTRR setting
338 MtrrGetAllMtrrs (&PeiCpuMpData
->MtrrTable
);
340 // Send broadcast IPI to APs to wakeup APs
342 PeiCpuMpData
->InitFlag
= 1;
343 WakeUpAP (PeiCpuMpData
, TRUE
, 0, NULL
, NULL
);
345 // Wait for AP task to complete and then exit.
347 MicroSecondDelay (PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
));
348 PeiCpuMpData
->InitFlag
= 0;
349 PeiCpuMpData
->CpuCount
+= (UINT32
) PeiCpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
;
351 // Sort BSP/Aps by CPU APIC ID in ascending order
353 SortApicId (PeiCpuMpData
);
355 DEBUG ((EFI_D_INFO
, "CpuMpPei: Find %d processors in system.\n", PeiCpuMpData
->CpuCount
));
356 return PeiCpuMpData
->CpuCount
;
360 Prepare for AP wakeup buffer and copy AP reset code into it.
362 Get wakeup buffer below 1MB. Allocate memory for CPU MP Data and APs Stack.
364 @return Pointer to PEI CPU MP Data
367 PrepareAPStartupVector (
373 PEI_CPU_MP_DATA
*PeiCpuMpData
;
374 EFI_PHYSICAL_ADDRESS Buffer
;
377 UINTN WakeupBufferSize
;
378 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
380 AsmGetAddressMap (&AddressMap
);
381 WakeupBufferSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
382 WakeupBuffer
= GetWakeupBuffer ((WakeupBufferSize
+ SIZE_4KB
- 1) & ~(SIZE_4KB
- 1));
383 DEBUG ((EFI_D_INFO
, "CpuMpPei: WakeupBuffer = 0x%x\n", WakeupBuffer
));
386 // Allocate Pages for APs stack, CPU MP Data and backup buffer for wakeup buffer
388 MaxCpuCount
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
389 BufferSize
= PcdGet32 (PcdCpuApStackSize
) * MaxCpuCount
+ sizeof (PEI_CPU_MP_DATA
)
390 + WakeupBufferSize
+ sizeof (PEI_CPU_DATA
) * MaxCpuCount
;
391 Status
= PeiServicesAllocatePages (
393 EFI_SIZE_TO_PAGES (BufferSize
),
396 ASSERT_EFI_ERROR (Status
);
398 PeiCpuMpData
= (PEI_CPU_MP_DATA
*) (UINTN
) (Buffer
+ PcdGet32 (PcdCpuApStackSize
) * MaxCpuCount
);
399 PeiCpuMpData
->Buffer
= (UINTN
) Buffer
;
400 PeiCpuMpData
->CpuApStackSize
= PcdGet32 (PcdCpuApStackSize
);
401 PeiCpuMpData
->WakeupBuffer
= WakeupBuffer
;
402 PeiCpuMpData
->BackupBuffer
= (UINTN
)PeiCpuMpData
+ sizeof (PEI_CPU_MP_DATA
);
403 PeiCpuMpData
->BackupBufferSize
= WakeupBufferSize
;
404 PeiCpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
) (WakeupBuffer
+ AddressMap
.RendezvousFunnelSize
);
406 PeiCpuMpData
->CpuCount
= 1;
407 PeiCpuMpData
->BspNumber
= 0;
408 PeiCpuMpData
->CpuData
= (PEI_CPU_DATA
*) (PeiCpuMpData
->MpCpuExchangeInfo
+ 1);
409 PeiCpuMpData
->CpuData
[0].ApicId
= GetInitialApicId ();
410 PeiCpuMpData
->CpuData
[0].Health
.Uint32
= 0;
411 CopyMem (&PeiCpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
414 // Backup original data and copy AP reset code in it
416 BackupAndPrepareWakeupBuffer(PeiCpuMpData
);
421 The Entry point of the MP CPU PEIM.
423 This function will wakeup APs and collect CPU AP count and install the
426 @param FileHandle Handle of the file being invoked.
427 @param PeiServices Describes the list of possible PEI Services.
429 @retval EFI_SUCCESS MpServicePpi is installed successfully.
435 IN EFI_PEI_FILE_HANDLE FileHandle
,
436 IN CONST EFI_PEI_SERVICES
**PeiServices
440 PEI_CPU_MP_DATA
*PeiCpuMpData
;
441 UINT32 ProcessorCount
;
444 // Load new GDT table on BSP
446 AsmInitializeGdt (&mGdt
);
448 // Get wakeup buffer and copy AP reset code in it
450 PeiCpuMpData
= PrepareAPStartupVector ();
452 // Count processor number and collect processor information
454 ProcessorCount
= CountProcessorNumber (PeiCpuMpData
);
456 // Build location of PEI CPU MP DATA buffer in HOB
460 (VOID
*)&PeiCpuMpData
,
464 // Update and publish CPU BIST information
466 CollectBistDataFromPpi (PeiServices
, PeiCpuMpData
);
468 // Install CPU MP PPI
470 Status
= PeiServicesInstallPpi(&mPeiCpuMpPpiDesc
);
471 ASSERT_EFI_ERROR (Status
);