]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/MpLib.c
UefiCpuPkg/MpService: GetProcessorInfo returns 6-level topology
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.c
1 /** @file
2 CPU MP Initialize Library common functions.
3
4 Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2020, AMD Inc. All rights reserved.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include "MpLib.h"
12
13 EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID;
14
15
16 /**
17 Determine if the standard CPU signature is "AuthenticAMD".
18
19 @retval TRUE The CPU signature matches.
20 @retval FALSE The CPU signature does not match.
21
22 **/
23 STATIC
24 BOOLEAN
25 StandardSignatureIsAuthenticAMD (
26 VOID
27 )
28 {
29 UINT32 RegEbx;
30 UINT32 RegEcx;
31 UINT32 RegEdx;
32
33 AsmCpuid (CPUID_SIGNATURE, NULL, &RegEbx, &RegEcx, &RegEdx);
34 return (RegEbx == CPUID_SIGNATURE_AUTHENTIC_AMD_EBX &&
35 RegEcx == CPUID_SIGNATURE_AUTHENTIC_AMD_ECX &&
36 RegEdx == CPUID_SIGNATURE_AUTHENTIC_AMD_EDX);
37 }
38
39 /**
40 The function will check if BSP Execute Disable is enabled.
41
42 DxeIpl may have enabled Execute Disable for BSP, APs need to
43 get the status and sync up the settings.
44 If BSP's CR0.Paging is not set, BSP execute Disble feature is
45 not working actually.
46
47 @retval TRUE BSP Execute Disable is enabled.
48 @retval FALSE BSP Execute Disable is not enabled.
49 **/
50 BOOLEAN
51 IsBspExecuteDisableEnabled (
52 VOID
53 )
54 {
55 UINT32 Eax;
56 CPUID_EXTENDED_CPU_SIG_EDX Edx;
57 MSR_IA32_EFER_REGISTER EferMsr;
58 BOOLEAN Enabled;
59 IA32_CR0 Cr0;
60
61 Enabled = FALSE;
62 Cr0.UintN = AsmReadCr0 ();
63 if (Cr0.Bits.PG != 0) {
64 //
65 // If CR0 Paging bit is set
66 //
67 AsmCpuid (CPUID_EXTENDED_FUNCTION, &Eax, NULL, NULL, NULL);
68 if (Eax >= CPUID_EXTENDED_CPU_SIG) {
69 AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, NULL, &Edx.Uint32);
70 //
71 // CPUID 0x80000001
72 // Bit 20: Execute Disable Bit available.
73 //
74 if (Edx.Bits.NX != 0) {
75 EferMsr.Uint64 = AsmReadMsr64 (MSR_IA32_EFER);
76 //
77 // MSR 0xC0000080
78 // Bit 11: Execute Disable Bit enable.
79 //
80 if (EferMsr.Bits.NXE != 0) {
81 Enabled = TRUE;
82 }
83 }
84 }
85 }
86
87 return Enabled;
88 }
89
90 /**
91 Worker function for SwitchBSP().
92
93 Worker function for SwitchBSP(), assigned to the AP which is intended
94 to become BSP.
95
96 @param[in] Buffer Pointer to CPU MP Data
97 **/
98 VOID
99 EFIAPI
100 FutureBSPProc (
101 IN VOID *Buffer
102 )
103 {
104 CPU_MP_DATA *DataInHob;
105
106 DataInHob = (CPU_MP_DATA *) Buffer;
107 AsmExchangeRole (&DataInHob->APInfo, &DataInHob->BSPInfo);
108 }
109
110 /**
111 Get the Application Processors state.
112
113 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
114
115 @return The AP status
116 **/
117 CPU_STATE
118 GetApState (
119 IN CPU_AP_DATA *CpuData
120 )
121 {
122 return CpuData->State;
123 }
124
125 /**
126 Set the Application Processors state.
127
128 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
129 @param[in] State The AP status
130 **/
131 VOID
132 SetApState (
133 IN CPU_AP_DATA *CpuData,
134 IN CPU_STATE State
135 )
136 {
137 AcquireSpinLock (&CpuData->ApLock);
138 CpuData->State = State;
139 ReleaseSpinLock (&CpuData->ApLock);
140 }
141
142 /**
143 Save BSP's local APIC timer setting.
144
145 @param[in] CpuMpData Pointer to CPU MP Data
146 **/
147 VOID
148 SaveLocalApicTimerSetting (
149 IN CPU_MP_DATA *CpuMpData
150 )
151 {
152 //
153 // Record the current local APIC timer setting of BSP
154 //
155 GetApicTimerState (
156 &CpuMpData->DivideValue,
157 &CpuMpData->PeriodicMode,
158 &CpuMpData->Vector
159 );
160 CpuMpData->CurrentTimerCount = GetApicTimerCurrentCount ();
161 CpuMpData->TimerInterruptState = GetApicTimerInterruptState ();
162 }
163
164 /**
165 Sync local APIC timer setting from BSP to AP.
166
167 @param[in] CpuMpData Pointer to CPU MP Data
168 **/
169 VOID
170 SyncLocalApicTimerSetting (
171 IN CPU_MP_DATA *CpuMpData
172 )
173 {
174 //
175 // Sync local APIC timer setting from BSP to AP
176 //
177 InitializeApicTimer (
178 CpuMpData->DivideValue,
179 CpuMpData->CurrentTimerCount,
180 CpuMpData->PeriodicMode,
181 CpuMpData->Vector
182 );
183 //
184 // Disable AP's local APIC timer interrupt
185 //
186 DisableApicTimerInterrupt ();
187 }
188
189 /**
190 Save the volatile registers required to be restored following INIT IPI.
191
192 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
193 **/
194 VOID
195 SaveVolatileRegisters (
196 OUT CPU_VOLATILE_REGISTERS *VolatileRegisters
197 )
198 {
199 CPUID_VERSION_INFO_EDX VersionInfoEdx;
200
201 VolatileRegisters->Cr0 = AsmReadCr0 ();
202 VolatileRegisters->Cr3 = AsmReadCr3 ();
203 VolatileRegisters->Cr4 = AsmReadCr4 ();
204
205 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);
206 if (VersionInfoEdx.Bits.DE != 0) {
207 //
208 // If processor supports Debugging Extensions feature
209 // by CPUID.[EAX=01H]:EDX.BIT2
210 //
211 VolatileRegisters->Dr0 = AsmReadDr0 ();
212 VolatileRegisters->Dr1 = AsmReadDr1 ();
213 VolatileRegisters->Dr2 = AsmReadDr2 ();
214 VolatileRegisters->Dr3 = AsmReadDr3 ();
215 VolatileRegisters->Dr6 = AsmReadDr6 ();
216 VolatileRegisters->Dr7 = AsmReadDr7 ();
217 }
218
219 AsmReadGdtr (&VolatileRegisters->Gdtr);
220 AsmReadIdtr (&VolatileRegisters->Idtr);
221 VolatileRegisters->Tr = AsmReadTr ();
222 }
223
224 /**
225 Restore the volatile registers following INIT IPI.
226
227 @param[in] VolatileRegisters Pointer to volatile resisters
228 @param[in] IsRestoreDr TRUE: Restore DRx if supported
229 FALSE: Do not restore DRx
230 **/
231 VOID
232 RestoreVolatileRegisters (
233 IN CPU_VOLATILE_REGISTERS *VolatileRegisters,
234 IN BOOLEAN IsRestoreDr
235 )
236 {
237 CPUID_VERSION_INFO_EDX VersionInfoEdx;
238 IA32_TSS_DESCRIPTOR *Tss;
239
240 AsmWriteCr3 (VolatileRegisters->Cr3);
241 AsmWriteCr4 (VolatileRegisters->Cr4);
242 AsmWriteCr0 (VolatileRegisters->Cr0);
243
244 if (IsRestoreDr) {
245 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);
246 if (VersionInfoEdx.Bits.DE != 0) {
247 //
248 // If processor supports Debugging Extensions feature
249 // by CPUID.[EAX=01H]:EDX.BIT2
250 //
251 AsmWriteDr0 (VolatileRegisters->Dr0);
252 AsmWriteDr1 (VolatileRegisters->Dr1);
253 AsmWriteDr2 (VolatileRegisters->Dr2);
254 AsmWriteDr3 (VolatileRegisters->Dr3);
255 AsmWriteDr6 (VolatileRegisters->Dr6);
256 AsmWriteDr7 (VolatileRegisters->Dr7);
257 }
258 }
259
260 AsmWriteGdtr (&VolatileRegisters->Gdtr);
261 AsmWriteIdtr (&VolatileRegisters->Idtr);
262 if (VolatileRegisters->Tr != 0 &&
263 VolatileRegisters->Tr < VolatileRegisters->Gdtr.Limit) {
264 Tss = (IA32_TSS_DESCRIPTOR *)(VolatileRegisters->Gdtr.Base +
265 VolatileRegisters->Tr);
266 if (Tss->Bits.P == 1) {
267 Tss->Bits.Type &= 0xD; // 1101 - Clear busy bit just in case
268 AsmWriteTr (VolatileRegisters->Tr);
269 }
270 }
271 }
272
273 /**
274 Detect whether Mwait-monitor feature is supported.
275
276 @retval TRUE Mwait-monitor feature is supported.
277 @retval FALSE Mwait-monitor feature is not supported.
278 **/
279 BOOLEAN
280 IsMwaitSupport (
281 VOID
282 )
283 {
284 CPUID_VERSION_INFO_ECX VersionInfoEcx;
285
286 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, &VersionInfoEcx.Uint32, NULL);
287 return (VersionInfoEcx.Bits.MONITOR == 1) ? TRUE : FALSE;
288 }
289
290 /**
291 Get AP loop mode.
292
293 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
294
295 @return The AP loop mode.
296 **/
297 UINT8
298 GetApLoopMode (
299 OUT UINT32 *MonitorFilterSize
300 )
301 {
302 UINT8 ApLoopMode;
303 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx;
304
305 ASSERT (MonitorFilterSize != NULL);
306
307 ApLoopMode = PcdGet8 (PcdCpuApLoopMode);
308 ASSERT (ApLoopMode >= ApInHltLoop && ApLoopMode <= ApInRunLoop);
309 if (ApLoopMode == ApInMwaitLoop) {
310 if (!IsMwaitSupport ()) {
311 //
312 // If processor does not support MONITOR/MWAIT feature,
313 // force AP in Hlt-loop mode
314 //
315 ApLoopMode = ApInHltLoop;
316 }
317 }
318
319 if (ApLoopMode != ApInMwaitLoop) {
320 *MonitorFilterSize = sizeof (UINT32);
321 } else {
322 //
323 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
324 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
325 //
326 AsmCpuid (CPUID_MONITOR_MWAIT, NULL, &MonitorMwaitEbx.Uint32, NULL, NULL);
327 *MonitorFilterSize = MonitorMwaitEbx.Bits.LargestMonitorLineSize;
328 }
329
330 return ApLoopMode;
331 }
332
333 /**
334 Sort the APIC ID of all processors.
335
336 This function sorts the APIC ID of all processors so that processor number is
337 assigned in the ascending order of APIC ID which eases MP debugging.
338
339 @param[in] CpuMpData Pointer to PEI CPU MP Data
340 **/
341 VOID
342 SortApicId (
343 IN CPU_MP_DATA *CpuMpData
344 )
345 {
346 UINTN Index1;
347 UINTN Index2;
348 UINTN Index3;
349 UINT32 ApicId;
350 CPU_INFO_IN_HOB CpuInfo;
351 UINT32 ApCount;
352 CPU_INFO_IN_HOB *CpuInfoInHob;
353 volatile UINT32 *StartupApSignal;
354
355 ApCount = CpuMpData->CpuCount - 1;
356 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
357 if (ApCount != 0) {
358 for (Index1 = 0; Index1 < ApCount; Index1++) {
359 Index3 = Index1;
360 //
361 // Sort key is the hardware default APIC ID
362 //
363 ApicId = CpuInfoInHob[Index1].ApicId;
364 for (Index2 = Index1 + 1; Index2 <= ApCount; Index2++) {
365 if (ApicId > CpuInfoInHob[Index2].ApicId) {
366 Index3 = Index2;
367 ApicId = CpuInfoInHob[Index2].ApicId;
368 }
369 }
370 if (Index3 != Index1) {
371 CopyMem (&CpuInfo, &CpuInfoInHob[Index3], sizeof (CPU_INFO_IN_HOB));
372 CopyMem (
373 &CpuInfoInHob[Index3],
374 &CpuInfoInHob[Index1],
375 sizeof (CPU_INFO_IN_HOB)
376 );
377 CopyMem (&CpuInfoInHob[Index1], &CpuInfo, sizeof (CPU_INFO_IN_HOB));
378
379 //
380 // Also exchange the StartupApSignal.
381 //
382 StartupApSignal = CpuMpData->CpuData[Index3].StartupApSignal;
383 CpuMpData->CpuData[Index3].StartupApSignal =
384 CpuMpData->CpuData[Index1].StartupApSignal;
385 CpuMpData->CpuData[Index1].StartupApSignal = StartupApSignal;
386 }
387 }
388
389 //
390 // Get the processor number for the BSP
391 //
392 ApicId = GetInitialApicId ();
393 for (Index1 = 0; Index1 < CpuMpData->CpuCount; Index1++) {
394 if (CpuInfoInHob[Index1].ApicId == ApicId) {
395 CpuMpData->BspNumber = (UINT32) Index1;
396 break;
397 }
398 }
399 }
400 }
401
402 /**
403 Enable x2APIC mode on APs.
404
405 @param[in, out] Buffer Pointer to private data buffer.
406 **/
407 VOID
408 EFIAPI
409 ApFuncEnableX2Apic (
410 IN OUT VOID *Buffer
411 )
412 {
413 SetApicMode (LOCAL_APIC_MODE_X2APIC);
414 }
415
416 /**
417 Do sync on APs.
418
419 @param[in, out] Buffer Pointer to private data buffer.
420 **/
421 VOID
422 EFIAPI
423 ApInitializeSync (
424 IN OUT VOID *Buffer
425 )
426 {
427 CPU_MP_DATA *CpuMpData;
428 UINTN ProcessorNumber;
429 EFI_STATUS Status;
430
431 CpuMpData = (CPU_MP_DATA *) Buffer;
432 Status = GetProcessorNumber (CpuMpData, &ProcessorNumber);
433 ASSERT_EFI_ERROR (Status);
434 //
435 // Load microcode on AP
436 //
437 MicrocodeDetect (CpuMpData, ProcessorNumber);
438 //
439 // Sync BSP's MTRR table to AP
440 //
441 MtrrSetAllMtrrs (&CpuMpData->MtrrTable);
442 }
443
444 /**
445 Find the current Processor number by APIC ID.
446
447 @param[in] CpuMpData Pointer to PEI CPU MP Data
448 @param[out] ProcessorNumber Return the pocessor number found
449
450 @retval EFI_SUCCESS ProcessorNumber is found and returned.
451 @retval EFI_NOT_FOUND ProcessorNumber is not found.
452 **/
453 EFI_STATUS
454 GetProcessorNumber (
455 IN CPU_MP_DATA *CpuMpData,
456 OUT UINTN *ProcessorNumber
457 )
458 {
459 UINTN TotalProcessorNumber;
460 UINTN Index;
461 CPU_INFO_IN_HOB *CpuInfoInHob;
462 UINT32 CurrentApicId;
463
464 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
465
466 TotalProcessorNumber = CpuMpData->CpuCount;
467 CurrentApicId = GetApicId ();
468 for (Index = 0; Index < TotalProcessorNumber; Index ++) {
469 if (CpuInfoInHob[Index].ApicId == CurrentApicId) {
470 *ProcessorNumber = Index;
471 return EFI_SUCCESS;
472 }
473 }
474
475 return EFI_NOT_FOUND;
476 }
477
478 /**
479 This function will get CPU count in the system.
480
481 @param[in] CpuMpData Pointer to PEI CPU MP Data
482
483 @return CPU count detected
484 **/
485 UINTN
486 CollectProcessorCount (
487 IN CPU_MP_DATA *CpuMpData
488 )
489 {
490 UINTN Index;
491 CPU_INFO_IN_HOB *CpuInfoInHob;
492 BOOLEAN X2Apic;
493
494 //
495 // Send 1st broadcast IPI to APs to wakeup APs
496 //
497 CpuMpData->InitFlag = ApInitConfig;
498 WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL, TRUE);
499 CpuMpData->InitFlag = ApInitDone;
500 ASSERT (CpuMpData->CpuCount <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));
501 //
502 // Wait for all APs finished the initialization
503 //
504 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
505 CpuPause ();
506 }
507
508
509 //
510 // Enable x2APIC mode if
511 // 1. Number of CPU is greater than 255; or
512 // 2. There are any logical processors reporting an Initial APIC ID of 255 or greater.
513 //
514 X2Apic = FALSE;
515 if (CpuMpData->CpuCount > 255) {
516 //
517 // If there are more than 255 processor found, force to enable X2APIC
518 //
519 X2Apic = TRUE;
520 } else {
521 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
522 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
523 if (CpuInfoInHob[Index].InitialApicId >= 0xFF) {
524 X2Apic = TRUE;
525 break;
526 }
527 }
528 }
529
530 if (X2Apic) {
531 DEBUG ((DEBUG_INFO, "Force x2APIC mode!\n"));
532 //
533 // Wakeup all APs to enable x2APIC mode
534 //
535 WakeUpAP (CpuMpData, TRUE, 0, ApFuncEnableX2Apic, NULL, TRUE);
536 //
537 // Wait for all known APs finished
538 //
539 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
540 CpuPause ();
541 }
542 //
543 // Enable x2APIC on BSP
544 //
545 SetApicMode (LOCAL_APIC_MODE_X2APIC);
546 //
547 // Set BSP/Aps state to IDLE
548 //
549 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
550 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);
551 }
552 }
553 DEBUG ((DEBUG_INFO, "APIC MODE is %d\n", GetApicMode ()));
554 //
555 // Sort BSP/Aps by CPU APIC ID in ascending order
556 //
557 SortApicId (CpuMpData);
558
559 DEBUG ((DEBUG_INFO, "MpInitLib: Find %d processors in system.\n", CpuMpData->CpuCount));
560
561 return CpuMpData->CpuCount;
562 }
563
564 /**
565 Initialize CPU AP Data when AP is wakeup at the first time.
566
567 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
568 @param[in] ProcessorNumber The handle number of processor
569 @param[in] BistData Processor BIST data
570 @param[in] ApTopOfStack Top of AP stack
571
572 **/
573 VOID
574 InitializeApData (
575 IN OUT CPU_MP_DATA *CpuMpData,
576 IN UINTN ProcessorNumber,
577 IN UINT32 BistData,
578 IN UINT64 ApTopOfStack
579 )
580 {
581 CPU_INFO_IN_HOB *CpuInfoInHob;
582 MSR_IA32_PLATFORM_ID_REGISTER PlatformIdMsr;
583
584 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
585 CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();
586 CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();
587 CpuInfoInHob[ProcessorNumber].Health = BistData;
588 CpuInfoInHob[ProcessorNumber].ApTopOfStack = ApTopOfStack;
589
590 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
591 CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE;
592
593 //
594 // NOTE: PlatformId is not relevant on AMD platforms.
595 //
596 if (!StandardSignatureIsAuthenticAMD ()) {
597 PlatformIdMsr.Uint64 = AsmReadMsr64 (MSR_IA32_PLATFORM_ID);
598 CpuMpData->CpuData[ProcessorNumber].PlatformId = (UINT8)PlatformIdMsr.Bits.PlatformId;
599 }
600
601 AsmCpuid (
602 CPUID_VERSION_INFO,
603 &CpuMpData->CpuData[ProcessorNumber].ProcessorSignature,
604 NULL,
605 NULL,
606 NULL
607 );
608
609 InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock);
610 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
611 }
612
613 /**
614 This function will be called from AP reset code if BSP uses WakeUpAP.
615
616 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
617 @param[in] ApIndex Number of current executing AP
618 **/
619 VOID
620 EFIAPI
621 ApWakeupFunction (
622 IN MP_CPU_EXCHANGE_INFO *ExchangeInfo,
623 IN UINTN ApIndex
624 )
625 {
626 CPU_MP_DATA *CpuMpData;
627 UINTN ProcessorNumber;
628 EFI_AP_PROCEDURE Procedure;
629 VOID *Parameter;
630 UINT32 BistData;
631 volatile UINT32 *ApStartupSignalBuffer;
632 CPU_INFO_IN_HOB *CpuInfoInHob;
633 UINT64 ApTopOfStack;
634 UINTN CurrentApicMode;
635
636 //
637 // AP finished assembly code and begin to execute C code
638 //
639 CpuMpData = ExchangeInfo->CpuMpData;
640
641 //
642 // AP's local APIC settings will be lost after received INIT IPI
643 // We need to re-initialize them at here
644 //
645 ProgramVirtualWireMode ();
646 //
647 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
648 //
649 DisableLvtInterrupts ();
650 SyncLocalApicTimerSetting (CpuMpData);
651
652 CurrentApicMode = GetApicMode ();
653 while (TRUE) {
654 if (CpuMpData->InitFlag == ApInitConfig) {
655 //
656 // Add CPU number
657 //
658 InterlockedIncrement ((UINT32 *) &CpuMpData->CpuCount);
659 ProcessorNumber = ApIndex;
660 //
661 // This is first time AP wakeup, get BIST information from AP stack
662 //
663 ApTopOfStack = CpuMpData->Buffer + (ProcessorNumber + 1) * CpuMpData->CpuApStackSize;
664 BistData = *(UINT32 *) ((UINTN) ApTopOfStack - sizeof (UINTN));
665 //
666 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
667 // to initialize AP in InitConfig path.
668 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
669 //
670 RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);
671 InitializeApData (CpuMpData, ProcessorNumber, BistData, ApTopOfStack);
672 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
673
674 InterlockedDecrement ((UINT32 *) &CpuMpData->MpCpuExchangeInfo->NumApsExecuting);
675 } else {
676 //
677 // Execute AP function if AP is ready
678 //
679 GetProcessorNumber (CpuMpData, &ProcessorNumber);
680 //
681 // Clear AP start-up signal when AP waken up
682 //
683 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
684 InterlockedCompareExchange32 (
685 (UINT32 *) ApStartupSignalBuffer,
686 WAKEUP_AP_SIGNAL,
687 0
688 );
689
690 if (CpuMpData->InitFlag == ApInitReconfig) {
691 //
692 // ApInitReconfig happens when:
693 // 1. AP is re-enabled after it's disabled, in either PEI or DXE phase.
694 // 2. AP is initialized in DXE phase.
695 // In either case, use the volatile registers value derived from BSP.
696 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a
697 // different IDT shared by all APs.
698 //
699 RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);
700 } else {
701 if (CpuMpData->ApLoopMode == ApInHltLoop) {
702 //
703 // Restore AP's volatile registers saved before AP is halted
704 //
705 RestoreVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters, TRUE);
706 } else {
707 //
708 // The CPU driver might not flush TLB for APs on spot after updating
709 // page attributes. AP in mwait loop mode needs to take care of it when
710 // woken up.
711 //
712 CpuFlushTlb ();
713 }
714 }
715
716 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateReady) {
717 Procedure = (EFI_AP_PROCEDURE)CpuMpData->CpuData[ProcessorNumber].ApFunction;
718 Parameter = (VOID *) CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument;
719 if (Procedure != NULL) {
720 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateBusy);
721 //
722 // Enable source debugging on AP function
723 //
724 EnableDebugAgent ();
725 //
726 // Invoke AP function here
727 //
728 Procedure (Parameter);
729 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
730 if (CpuMpData->SwitchBspFlag) {
731 //
732 // Re-get the processor number due to BSP/AP maybe exchange in AP function
733 //
734 GetProcessorNumber (CpuMpData, &ProcessorNumber);
735 CpuMpData->CpuData[ProcessorNumber].ApFunction = 0;
736 CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument = 0;
737 ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
738 CpuInfoInHob[ProcessorNumber].ApTopOfStack = CpuInfoInHob[CpuMpData->NewBspNumber].ApTopOfStack;
739 } else {
740 if (CpuInfoInHob[ProcessorNumber].ApicId != GetApicId () ||
741 CpuInfoInHob[ProcessorNumber].InitialApicId != GetInitialApicId ()) {
742 if (CurrentApicMode != GetApicMode ()) {
743 //
744 // If APIC mode change happened during AP function execution,
745 // we do not support APIC ID value changed.
746 //
747 ASSERT (FALSE);
748 CpuDeadLoop ();
749 } else {
750 //
751 // Re-get the CPU APICID and Initial APICID if they are changed
752 //
753 CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();
754 CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();
755 }
756 }
757 }
758 }
759 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateFinished);
760 }
761 }
762
763 //
764 // AP finished executing C code
765 //
766 InterlockedIncrement ((UINT32 *) &CpuMpData->FinishedCount);
767
768 //
769 // Place AP is specified loop mode
770 //
771 if (CpuMpData->ApLoopMode == ApInHltLoop) {
772 //
773 // Save AP volatile registers
774 //
775 SaveVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters);
776 //
777 // Place AP in HLT-loop
778 //
779 while (TRUE) {
780 DisableInterrupts ();
781 CpuSleep ();
782 CpuPause ();
783 }
784 }
785 while (TRUE) {
786 DisableInterrupts ();
787 if (CpuMpData->ApLoopMode == ApInMwaitLoop) {
788 //
789 // Place AP in MWAIT-loop
790 //
791 AsmMonitor ((UINTN) ApStartupSignalBuffer, 0, 0);
792 if (*ApStartupSignalBuffer != WAKEUP_AP_SIGNAL) {
793 //
794 // Check AP start-up signal again.
795 // If AP start-up signal is not set, place AP into
796 // the specified C-state
797 //
798 AsmMwait (CpuMpData->ApTargetCState << 4, 0);
799 }
800 } else if (CpuMpData->ApLoopMode == ApInRunLoop) {
801 //
802 // Place AP in Run-loop
803 //
804 CpuPause ();
805 } else {
806 ASSERT (FALSE);
807 }
808
809 //
810 // If AP start-up signal is written, AP is waken up
811 // otherwise place AP in loop again
812 //
813 if (*ApStartupSignalBuffer == WAKEUP_AP_SIGNAL) {
814 break;
815 }
816 }
817 }
818 }
819
820 /**
821 Wait for AP wakeup and write AP start-up signal till AP is waken up.
822
823 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
824 **/
825 VOID
826 WaitApWakeup (
827 IN volatile UINT32 *ApStartupSignalBuffer
828 )
829 {
830 //
831 // If AP is waken up, StartupApSignal should be cleared.
832 // Otherwise, write StartupApSignal again till AP waken up.
833 //
834 while (InterlockedCompareExchange32 (
835 (UINT32 *) ApStartupSignalBuffer,
836 WAKEUP_AP_SIGNAL,
837 WAKEUP_AP_SIGNAL
838 ) != 0) {
839 CpuPause ();
840 }
841 }
842
843 /**
844 This function will fill the exchange info structure.
845
846 @param[in] CpuMpData Pointer to CPU MP Data
847
848 **/
849 VOID
850 FillExchangeInfoData (
851 IN CPU_MP_DATA *CpuMpData
852 )
853 {
854 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;
855 UINTN Size;
856 IA32_SEGMENT_DESCRIPTOR *Selector;
857 IA32_CR4 Cr4;
858
859 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;
860 ExchangeInfo->Lock = 0;
861 ExchangeInfo->StackStart = CpuMpData->Buffer;
862 ExchangeInfo->StackSize = CpuMpData->CpuApStackSize;
863 ExchangeInfo->BufferStart = CpuMpData->WakeupBuffer;
864 ExchangeInfo->ModeOffset = CpuMpData->AddressMap.ModeEntryOffset;
865
866 ExchangeInfo->CodeSegment = AsmReadCs ();
867 ExchangeInfo->DataSegment = AsmReadDs ();
868
869 ExchangeInfo->Cr3 = AsmReadCr3 ();
870
871 ExchangeInfo->CFunction = (UINTN) ApWakeupFunction;
872 ExchangeInfo->ApIndex = 0;
873 ExchangeInfo->NumApsExecuting = 0;
874 ExchangeInfo->InitFlag = (UINTN) CpuMpData->InitFlag;
875 ExchangeInfo->CpuInfo = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
876 ExchangeInfo->CpuMpData = CpuMpData;
877
878 ExchangeInfo->EnableExecuteDisable = IsBspExecuteDisableEnabled ();
879
880 ExchangeInfo->InitializeFloatingPointUnitsAddress = (UINTN)InitializeFloatingPointUnits;
881
882 //
883 // We can check either CPUID(7).ECX[bit16] or check CR4.LA57[bit12]
884 // to determin whether 5-Level Paging is enabled.
885 // CPUID(7).ECX[bit16] shows CPU's capability, CR4.LA57[bit12] shows
886 // current system setting.
887 // Using latter way is simpler because it also eliminates the needs to
888 // check whether platform wants to enable it.
889 //
890 Cr4.UintN = AsmReadCr4 ();
891 ExchangeInfo->Enable5LevelPaging = (BOOLEAN) (Cr4.Bits.LA57 == 1);
892 DEBUG ((DEBUG_INFO, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName, ExchangeInfo->Enable5LevelPaging));
893
894 //
895 // Get the BSP's data of GDT and IDT
896 //
897 AsmReadGdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->GdtrProfile);
898 AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);
899
900 //
901 // Find a 32-bit code segment
902 //
903 Selector = (IA32_SEGMENT_DESCRIPTOR *)ExchangeInfo->GdtrProfile.Base;
904 Size = ExchangeInfo->GdtrProfile.Limit + 1;
905 while (Size > 0) {
906 if (Selector->Bits.L == 0 && Selector->Bits.Type >= 8) {
907 ExchangeInfo->ModeTransitionSegment =
908 (UINT16)((UINTN)Selector - ExchangeInfo->GdtrProfile.Base);
909 break;
910 }
911 Selector += 1;
912 Size -= sizeof (IA32_SEGMENT_DESCRIPTOR);
913 }
914
915 //
916 // Copy all 32-bit code and 64-bit code into memory with type of
917 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
918 //
919 if (CpuMpData->WakeupBufferHigh != 0) {
920 Size = CpuMpData->AddressMap.RendezvousFunnelSize -
921 CpuMpData->AddressMap.ModeTransitionOffset;
922 CopyMem (
923 (VOID *)CpuMpData->WakeupBufferHigh,
924 CpuMpData->AddressMap.RendezvousFunnelAddress +
925 CpuMpData->AddressMap.ModeTransitionOffset,
926 Size
927 );
928
929 ExchangeInfo->ModeTransitionMemory = (UINT32)CpuMpData->WakeupBufferHigh;
930 } else {
931 ExchangeInfo->ModeTransitionMemory = (UINT32)
932 (ExchangeInfo->BufferStart + CpuMpData->AddressMap.ModeTransitionOffset);
933 }
934
935 ExchangeInfo->ModeHighMemory = ExchangeInfo->ModeTransitionMemory +
936 (UINT32)ExchangeInfo->ModeOffset -
937 (UINT32)CpuMpData->AddressMap.ModeTransitionOffset;
938 ExchangeInfo->ModeHighSegment = (UINT16)ExchangeInfo->CodeSegment;
939 }
940
941 /**
942 Helper function that waits until the finished AP count reaches the specified
943 limit, or the specified timeout elapses (whichever comes first).
944
945 @param[in] CpuMpData Pointer to CPU MP Data.
946 @param[in] FinishedApLimit The number of finished APs to wait for.
947 @param[in] TimeLimit The number of microseconds to wait for.
948 **/
949 VOID
950 TimedWaitForApFinish (
951 IN CPU_MP_DATA *CpuMpData,
952 IN UINT32 FinishedApLimit,
953 IN UINT32 TimeLimit
954 );
955
956 /**
957 Get available system memory below 1MB by specified size.
958
959 @param[in] CpuMpData The pointer to CPU MP Data structure.
960 **/
961 VOID
962 BackupAndPrepareWakeupBuffer(
963 IN CPU_MP_DATA *CpuMpData
964 )
965 {
966 CopyMem (
967 (VOID *) CpuMpData->BackupBuffer,
968 (VOID *) CpuMpData->WakeupBuffer,
969 CpuMpData->BackupBufferSize
970 );
971 CopyMem (
972 (VOID *) CpuMpData->WakeupBuffer,
973 (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,
974 CpuMpData->AddressMap.RendezvousFunnelSize
975 );
976 }
977
978 /**
979 Restore wakeup buffer data.
980
981 @param[in] CpuMpData The pointer to CPU MP Data structure.
982 **/
983 VOID
984 RestoreWakeupBuffer(
985 IN CPU_MP_DATA *CpuMpData
986 )
987 {
988 CopyMem (
989 (VOID *) CpuMpData->WakeupBuffer,
990 (VOID *) CpuMpData->BackupBuffer,
991 CpuMpData->BackupBufferSize
992 );
993 }
994
995 /**
996 Allocate reset vector buffer.
997
998 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
999 **/
1000 VOID
1001 AllocateResetVector (
1002 IN OUT CPU_MP_DATA *CpuMpData
1003 )
1004 {
1005 UINTN ApResetVectorSize;
1006
1007 if (CpuMpData->WakeupBuffer == (UINTN) -1) {
1008 ApResetVectorSize = CpuMpData->AddressMap.RendezvousFunnelSize +
1009 sizeof (MP_CPU_EXCHANGE_INFO);
1010
1011 CpuMpData->WakeupBuffer = GetWakeupBuffer (ApResetVectorSize);
1012 CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN)
1013 (CpuMpData->WakeupBuffer + CpuMpData->AddressMap.RendezvousFunnelSize);
1014 CpuMpData->WakeupBufferHigh = GetModeTransitionBuffer (
1015 CpuMpData->AddressMap.RendezvousFunnelSize -
1016 CpuMpData->AddressMap.ModeTransitionOffset
1017 );
1018 }
1019 BackupAndPrepareWakeupBuffer (CpuMpData);
1020 }
1021
1022 /**
1023 Free AP reset vector buffer.
1024
1025 @param[in] CpuMpData The pointer to CPU MP Data structure.
1026 **/
1027 VOID
1028 FreeResetVector (
1029 IN CPU_MP_DATA *CpuMpData
1030 )
1031 {
1032 RestoreWakeupBuffer (CpuMpData);
1033 }
1034
1035 /**
1036 This function will be called by BSP to wakeup AP.
1037
1038 @param[in] CpuMpData Pointer to CPU MP Data
1039 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
1040 FALSE: Send IPI to AP by ApicId
1041 @param[in] ProcessorNumber The handle number of specified processor
1042 @param[in] Procedure The function to be invoked by AP
1043 @param[in] ProcedureArgument The argument to be passed into AP function
1044 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
1045 **/
1046 VOID
1047 WakeUpAP (
1048 IN CPU_MP_DATA *CpuMpData,
1049 IN BOOLEAN Broadcast,
1050 IN UINTN ProcessorNumber,
1051 IN EFI_AP_PROCEDURE Procedure, OPTIONAL
1052 IN VOID *ProcedureArgument, OPTIONAL
1053 IN BOOLEAN WakeUpDisabledAps
1054 )
1055 {
1056 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;
1057 UINTN Index;
1058 CPU_AP_DATA *CpuData;
1059 BOOLEAN ResetVectorRequired;
1060 CPU_INFO_IN_HOB *CpuInfoInHob;
1061
1062 CpuMpData->FinishedCount = 0;
1063 ResetVectorRequired = FALSE;
1064
1065 if (CpuMpData->WakeUpByInitSipiSipi ||
1066 CpuMpData->InitFlag != ApInitDone) {
1067 ResetVectorRequired = TRUE;
1068 AllocateResetVector (CpuMpData);
1069 FillExchangeInfoData (CpuMpData);
1070 SaveLocalApicTimerSetting (CpuMpData);
1071 }
1072
1073 if (CpuMpData->ApLoopMode == ApInMwaitLoop) {
1074 //
1075 // Get AP target C-state each time when waking up AP,
1076 // for it maybe updated by platform again
1077 //
1078 CpuMpData->ApTargetCState = PcdGet8 (PcdCpuApTargetCstate);
1079 }
1080
1081 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;
1082
1083 if (Broadcast) {
1084 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1085 if (Index != CpuMpData->BspNumber) {
1086 CpuData = &CpuMpData->CpuData[Index];
1087 //
1088 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1089 // the AP procedure will be skipped for disabled AP because AP state
1090 // is not CpuStateReady.
1091 //
1092 if (GetApState (CpuData) == CpuStateDisabled && !WakeUpDisabledAps) {
1093 continue;
1094 }
1095
1096 CpuData->ApFunction = (UINTN) Procedure;
1097 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;
1098 SetApState (CpuData, CpuStateReady);
1099 if (CpuMpData->InitFlag != ApInitConfig) {
1100 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;
1101 }
1102 }
1103 }
1104 if (ResetVectorRequired) {
1105 //
1106 // Wakeup all APs
1107 //
1108 SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);
1109 }
1110 if (CpuMpData->InitFlag == ApInitConfig) {
1111 if (PcdGet32 (PcdCpuBootLogicalProcessorNumber) > 0) {
1112 //
1113 // The AP enumeration algorithm below is suitable only when the
1114 // platform can tell us the *exact* boot CPU count in advance.
1115 //
1116 // The wait below finishes only when the detected AP count reaches
1117 // (PcdCpuBootLogicalProcessorNumber - 1), regardless of how long that
1118 // takes. If at least one AP fails to check in (meaning a platform
1119 // hardware bug), the detection hangs forever, by design. If the actual
1120 // boot CPU count in the system is higher than
1121 // PcdCpuBootLogicalProcessorNumber (meaning a platform
1122 // misconfiguration), then some APs may complete initialization after
1123 // the wait finishes, and cause undefined behavior.
1124 //
1125 TimedWaitForApFinish (
1126 CpuMpData,
1127 PcdGet32 (PcdCpuBootLogicalProcessorNumber) - 1,
1128 MAX_UINT32 // approx. 71 minutes
1129 );
1130 } else {
1131 //
1132 // The AP enumeration algorithm below is suitable for two use cases.
1133 //
1134 // (1) The check-in time for an individual AP is bounded, and APs run
1135 // through their initialization routines strongly concurrently. In
1136 // particular, the number of concurrently running APs
1137 // ("NumApsExecuting") is never expected to fall to zero
1138 // *temporarily* -- it is expected to fall to zero only when all
1139 // APs have checked-in.
1140 //
1141 // In this case, the platform is supposed to set
1142 // PcdCpuApInitTimeOutInMicroSeconds to a low-ish value (just long
1143 // enough for one AP to start initialization). The timeout will be
1144 // reached soon, and remaining APs are collected by watching
1145 // NumApsExecuting fall to zero. If NumApsExecuting falls to zero
1146 // mid-process, while some APs have not completed initialization,
1147 // the behavior is undefined.
1148 //
1149 // (2) The check-in time for an individual AP is unbounded, and/or APs
1150 // may complete their initializations widely spread out. In
1151 // particular, some APs may finish initialization before some APs
1152 // even start.
1153 //
1154 // In this case, the platform is supposed to set
1155 // PcdCpuApInitTimeOutInMicroSeconds to a high-ish value. The AP
1156 // enumeration will always take that long (except when the boot CPU
1157 // count happens to be maximal, that is,
1158 // PcdCpuMaxLogicalProcessorNumber). All APs are expected to
1159 // check-in before the timeout, and NumApsExecuting is assumed zero
1160 // at timeout. APs that miss the time-out may cause undefined
1161 // behavior.
1162 //
1163 TimedWaitForApFinish (
1164 CpuMpData,
1165 PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,
1166 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)
1167 );
1168
1169 while (CpuMpData->MpCpuExchangeInfo->NumApsExecuting != 0) {
1170 CpuPause();
1171 }
1172 }
1173 } else {
1174 //
1175 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1176 //
1177 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1178 CpuData = &CpuMpData->CpuData[Index];
1179 if (Index != CpuMpData->BspNumber) {
1180 WaitApWakeup (CpuData->StartupApSignal);
1181 }
1182 }
1183 }
1184 } else {
1185 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1186 CpuData->ApFunction = (UINTN) Procedure;
1187 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;
1188 SetApState (CpuData, CpuStateReady);
1189 //
1190 // Wakeup specified AP
1191 //
1192 ASSERT (CpuMpData->InitFlag != ApInitConfig);
1193 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;
1194 if (ResetVectorRequired) {
1195 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
1196 SendInitSipiSipi (
1197 CpuInfoInHob[ProcessorNumber].ApicId,
1198 (UINT32) ExchangeInfo->BufferStart
1199 );
1200 }
1201 //
1202 // Wait specified AP waken up
1203 //
1204 WaitApWakeup (CpuData->StartupApSignal);
1205 }
1206
1207 if (ResetVectorRequired) {
1208 FreeResetVector (CpuMpData);
1209 }
1210
1211 //
1212 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1213 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1214 // S3SmmInitDone Ppi.
1215 //
1216 CpuMpData->WakeUpByInitSipiSipi = (CpuMpData->ApLoopMode == ApInHltLoop);
1217 }
1218
1219 /**
1220 Calculate timeout value and return the current performance counter value.
1221
1222 Calculate the number of performance counter ticks required for a timeout.
1223 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1224 as infinity.
1225
1226 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1227 @param[out] CurrentTime Returns the current value of the performance counter.
1228
1229 @return Expected time stamp counter for timeout.
1230 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1231 as infinity.
1232
1233 **/
1234 UINT64
1235 CalculateTimeout (
1236 IN UINTN TimeoutInMicroseconds,
1237 OUT UINT64 *CurrentTime
1238 )
1239 {
1240 UINT64 TimeoutInSeconds;
1241 UINT64 TimestampCounterFreq;
1242
1243 //
1244 // Read the current value of the performance counter
1245 //
1246 *CurrentTime = GetPerformanceCounter ();
1247
1248 //
1249 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1250 // as infinity.
1251 //
1252 if (TimeoutInMicroseconds == 0) {
1253 return 0;
1254 }
1255
1256 //
1257 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1258 // in Hz.
1259 //
1260 TimestampCounterFreq = GetPerformanceCounterProperties (NULL, NULL);
1261
1262 //
1263 // Check the potential overflow before calculate the number of ticks for the timeout value.
1264 //
1265 if (DivU64x64Remainder (MAX_UINT64, TimeoutInMicroseconds, NULL) < TimestampCounterFreq) {
1266 //
1267 // Convert microseconds into seconds if direct multiplication overflows
1268 //
1269 TimeoutInSeconds = DivU64x32 (TimeoutInMicroseconds, 1000000);
1270 //
1271 // Assertion if the final tick count exceeds MAX_UINT64
1272 //
1273 ASSERT (DivU64x64Remainder (MAX_UINT64, TimeoutInSeconds, NULL) >= TimestampCounterFreq);
1274 return MultU64x64 (TimestampCounterFreq, TimeoutInSeconds);
1275 } else {
1276 //
1277 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1278 // it by 1,000,000, to get the number of ticks for the timeout value.
1279 //
1280 return DivU64x32 (
1281 MultU64x64 (
1282 TimestampCounterFreq,
1283 TimeoutInMicroseconds
1284 ),
1285 1000000
1286 );
1287 }
1288 }
1289
1290 /**
1291 Checks whether timeout expires.
1292
1293 Check whether the number of elapsed performance counter ticks required for
1294 a timeout condition has been reached.
1295 If Timeout is zero, which means infinity, return value is always FALSE.
1296
1297 @param[in, out] PreviousTime On input, the value of the performance counter
1298 when it was last read.
1299 On output, the current value of the performance
1300 counter
1301 @param[in] TotalTime The total amount of elapsed time in performance
1302 counter ticks.
1303 @param[in] Timeout The number of performance counter ticks required
1304 to reach a timeout condition.
1305
1306 @retval TRUE A timeout condition has been reached.
1307 @retval FALSE A timeout condition has not been reached.
1308
1309 **/
1310 BOOLEAN
1311 CheckTimeout (
1312 IN OUT UINT64 *PreviousTime,
1313 IN UINT64 *TotalTime,
1314 IN UINT64 Timeout
1315 )
1316 {
1317 UINT64 Start;
1318 UINT64 End;
1319 UINT64 CurrentTime;
1320 INT64 Delta;
1321 INT64 Cycle;
1322
1323 if (Timeout == 0) {
1324 return FALSE;
1325 }
1326 GetPerformanceCounterProperties (&Start, &End);
1327 Cycle = End - Start;
1328 if (Cycle < 0) {
1329 Cycle = -Cycle;
1330 }
1331 Cycle++;
1332 CurrentTime = GetPerformanceCounter();
1333 Delta = (INT64) (CurrentTime - *PreviousTime);
1334 if (Start > End) {
1335 Delta = -Delta;
1336 }
1337 if (Delta < 0) {
1338 Delta += Cycle;
1339 }
1340 *TotalTime += Delta;
1341 *PreviousTime = CurrentTime;
1342 if (*TotalTime > Timeout) {
1343 return TRUE;
1344 }
1345 return FALSE;
1346 }
1347
1348 /**
1349 Helper function that waits until the finished AP count reaches the specified
1350 limit, or the specified timeout elapses (whichever comes first).
1351
1352 @param[in] CpuMpData Pointer to CPU MP Data.
1353 @param[in] FinishedApLimit The number of finished APs to wait for.
1354 @param[in] TimeLimit The number of microseconds to wait for.
1355 **/
1356 VOID
1357 TimedWaitForApFinish (
1358 IN CPU_MP_DATA *CpuMpData,
1359 IN UINT32 FinishedApLimit,
1360 IN UINT32 TimeLimit
1361 )
1362 {
1363 //
1364 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1365 // "infinity", so check for (TimeLimit == 0) explicitly.
1366 //
1367 if (TimeLimit == 0) {
1368 return;
1369 }
1370
1371 CpuMpData->TotalTime = 0;
1372 CpuMpData->ExpectedTime = CalculateTimeout (
1373 TimeLimit,
1374 &CpuMpData->CurrentTime
1375 );
1376 while (CpuMpData->FinishedCount < FinishedApLimit &&
1377 !CheckTimeout (
1378 &CpuMpData->CurrentTime,
1379 &CpuMpData->TotalTime,
1380 CpuMpData->ExpectedTime
1381 )) {
1382 CpuPause ();
1383 }
1384
1385 if (CpuMpData->FinishedCount >= FinishedApLimit) {
1386 DEBUG ((
1387 DEBUG_VERBOSE,
1388 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1389 __FUNCTION__,
1390 FinishedApLimit,
1391 DivU64x64Remainder (
1392 MultU64x32 (CpuMpData->TotalTime, 1000000),
1393 GetPerformanceCounterProperties (NULL, NULL),
1394 NULL
1395 )
1396 ));
1397 }
1398 }
1399
1400 /**
1401 Reset an AP to Idle state.
1402
1403 Any task being executed by the AP will be aborted and the AP
1404 will be waiting for a new task in Wait-For-SIPI state.
1405
1406 @param[in] ProcessorNumber The handle number of processor.
1407 **/
1408 VOID
1409 ResetProcessorToIdleState (
1410 IN UINTN ProcessorNumber
1411 )
1412 {
1413 CPU_MP_DATA *CpuMpData;
1414
1415 CpuMpData = GetCpuMpData ();
1416
1417 CpuMpData->InitFlag = ApInitReconfig;
1418 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, NULL, NULL, TRUE);
1419 while (CpuMpData->FinishedCount < 1) {
1420 CpuPause ();
1421 }
1422 CpuMpData->InitFlag = ApInitDone;
1423
1424 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
1425 }
1426
1427 /**
1428 Searches for the next waiting AP.
1429
1430 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1431
1432 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1433
1434 @retval EFI_SUCCESS The next waiting AP has been found.
1435 @retval EFI_NOT_FOUND No waiting AP exists.
1436
1437 **/
1438 EFI_STATUS
1439 GetNextWaitingProcessorNumber (
1440 OUT UINTN *NextProcessorNumber
1441 )
1442 {
1443 UINTN ProcessorNumber;
1444 CPU_MP_DATA *CpuMpData;
1445
1446 CpuMpData = GetCpuMpData ();
1447
1448 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1449 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1450 *NextProcessorNumber = ProcessorNumber;
1451 return EFI_SUCCESS;
1452 }
1453 }
1454
1455 return EFI_NOT_FOUND;
1456 }
1457
1458 /** Checks status of specified AP.
1459
1460 This function checks whether the specified AP has finished the task assigned
1461 by StartupThisAP(), and whether timeout expires.
1462
1463 @param[in] ProcessorNumber The handle number of processor.
1464
1465 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1466 @retval EFI_TIMEOUT The timeout expires.
1467 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1468 **/
1469 EFI_STATUS
1470 CheckThisAP (
1471 IN UINTN ProcessorNumber
1472 )
1473 {
1474 CPU_MP_DATA *CpuMpData;
1475 CPU_AP_DATA *CpuData;
1476
1477 CpuMpData = GetCpuMpData ();
1478 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1479
1480 //
1481 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1482 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1483 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1484 //
1485 //
1486 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1487 //
1488 if (GetApState(CpuData) == CpuStateFinished) {
1489 if (CpuData->Finished != NULL) {
1490 *(CpuData->Finished) = TRUE;
1491 }
1492 SetApState (CpuData, CpuStateIdle);
1493 return EFI_SUCCESS;
1494 } else {
1495 //
1496 // If timeout expires for StartupThisAP(), report timeout.
1497 //
1498 if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {
1499 if (CpuData->Finished != NULL) {
1500 *(CpuData->Finished) = FALSE;
1501 }
1502 //
1503 // Reset failed AP to idle state
1504 //
1505 ResetProcessorToIdleState (ProcessorNumber);
1506
1507 return EFI_TIMEOUT;
1508 }
1509 }
1510 return EFI_NOT_READY;
1511 }
1512
1513 /**
1514 Checks status of all APs.
1515
1516 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1517 and whether timeout expires.
1518
1519 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1520 @retval EFI_TIMEOUT The timeout expires.
1521 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1522 **/
1523 EFI_STATUS
1524 CheckAllAPs (
1525 VOID
1526 )
1527 {
1528 UINTN ProcessorNumber;
1529 UINTN NextProcessorNumber;
1530 UINTN ListIndex;
1531 EFI_STATUS Status;
1532 CPU_MP_DATA *CpuMpData;
1533 CPU_AP_DATA *CpuData;
1534
1535 CpuMpData = GetCpuMpData ();
1536
1537 NextProcessorNumber = 0;
1538
1539 //
1540 // Go through all APs that are responsible for the StartupAllAPs().
1541 //
1542 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1543 if (!CpuMpData->CpuData[ProcessorNumber].Waiting) {
1544 continue;
1545 }
1546
1547 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1548 //
1549 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1550 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1551 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1552 //
1553 if (GetApState(CpuData) == CpuStateFinished) {
1554 CpuMpData->RunningCount --;
1555 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
1556 SetApState(CpuData, CpuStateIdle);
1557
1558 //
1559 // If in Single Thread mode, then search for the next waiting AP for execution.
1560 //
1561 if (CpuMpData->SingleThread) {
1562 Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);
1563
1564 if (!EFI_ERROR (Status)) {
1565 WakeUpAP (
1566 CpuMpData,
1567 FALSE,
1568 (UINT32) NextProcessorNumber,
1569 CpuMpData->Procedure,
1570 CpuMpData->ProcArguments,
1571 TRUE
1572 );
1573 }
1574 }
1575 }
1576 }
1577
1578 //
1579 // If all APs finish, return EFI_SUCCESS.
1580 //
1581 if (CpuMpData->RunningCount == 0) {
1582 return EFI_SUCCESS;
1583 }
1584
1585 //
1586 // If timeout expires, report timeout.
1587 //
1588 if (CheckTimeout (
1589 &CpuMpData->CurrentTime,
1590 &CpuMpData->TotalTime,
1591 CpuMpData->ExpectedTime)
1592 ) {
1593 //
1594 // If FailedCpuList is not NULL, record all failed APs in it.
1595 //
1596 if (CpuMpData->FailedCpuList != NULL) {
1597 *CpuMpData->FailedCpuList =
1598 AllocatePool ((CpuMpData->RunningCount + 1) * sizeof (UINTN));
1599 ASSERT (*CpuMpData->FailedCpuList != NULL);
1600 }
1601 ListIndex = 0;
1602
1603 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1604 //
1605 // Check whether this processor is responsible for StartupAllAPs().
1606 //
1607 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1608 //
1609 // Reset failed APs to idle state
1610 //
1611 ResetProcessorToIdleState (ProcessorNumber);
1612 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
1613 if (CpuMpData->FailedCpuList != NULL) {
1614 (*CpuMpData->FailedCpuList)[ListIndex++] = ProcessorNumber;
1615 }
1616 }
1617 }
1618 if (CpuMpData->FailedCpuList != NULL) {
1619 (*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST;
1620 }
1621 return EFI_TIMEOUT;
1622 }
1623 return EFI_NOT_READY;
1624 }
1625
1626 /**
1627 MP Initialize Library initialization.
1628
1629 This service will allocate AP reset vector and wakeup all APs to do APs
1630 initialization.
1631
1632 This service must be invoked before all other MP Initialize Library
1633 service are invoked.
1634
1635 @retval EFI_SUCCESS MP initialization succeeds.
1636 @retval Others MP initialization fails.
1637
1638 **/
1639 EFI_STATUS
1640 EFIAPI
1641 MpInitLibInitialize (
1642 VOID
1643 )
1644 {
1645 CPU_MP_DATA *OldCpuMpData;
1646 CPU_INFO_IN_HOB *CpuInfoInHob;
1647 UINT32 MaxLogicalProcessorNumber;
1648 UINT32 ApStackSize;
1649 MP_ASSEMBLY_ADDRESS_MAP AddressMap;
1650 CPU_VOLATILE_REGISTERS VolatileRegisters;
1651 UINTN BufferSize;
1652 UINT32 MonitorFilterSize;
1653 VOID *MpBuffer;
1654 UINTN Buffer;
1655 CPU_MP_DATA *CpuMpData;
1656 UINT8 ApLoopMode;
1657 UINT8 *MonitorBuffer;
1658 UINTN Index;
1659 UINTN ApResetVectorSize;
1660 UINTN BackupBufferAddr;
1661 UINTN ApIdtBase;
1662
1663 OldCpuMpData = GetCpuMpDataFromGuidedHob ();
1664 if (OldCpuMpData == NULL) {
1665 MaxLogicalProcessorNumber = PcdGet32(PcdCpuMaxLogicalProcessorNumber);
1666 } else {
1667 MaxLogicalProcessorNumber = OldCpuMpData->CpuCount;
1668 }
1669 ASSERT (MaxLogicalProcessorNumber != 0);
1670
1671 AsmGetAddressMap (&AddressMap);
1672 ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);
1673 ApStackSize = PcdGet32(PcdCpuApStackSize);
1674 ApLoopMode = GetApLoopMode (&MonitorFilterSize);
1675
1676 //
1677 // Save BSP's Control registers for APs.
1678 //
1679 SaveVolatileRegisters (&VolatileRegisters);
1680
1681 BufferSize = ApStackSize * MaxLogicalProcessorNumber;
1682 BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;
1683 BufferSize += ApResetVectorSize;
1684 BufferSize = ALIGN_VALUE (BufferSize, 8);
1685 BufferSize += VolatileRegisters.Idtr.Limit + 1;
1686 BufferSize += sizeof (CPU_MP_DATA);
1687 BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;
1688 MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));
1689 ASSERT (MpBuffer != NULL);
1690 ZeroMem (MpBuffer, BufferSize);
1691 Buffer = (UINTN) MpBuffer;
1692
1693 //
1694 // The layout of the Buffer is as below:
1695 //
1696 // +--------------------+ <-- Buffer
1697 // AP Stacks (N)
1698 // +--------------------+ <-- MonitorBuffer
1699 // AP Monitor Filters (N)
1700 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1701 // Backup Buffer
1702 // +--------------------+
1703 // Padding
1704 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1705 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1706 // +--------------------+ <-- CpuMpData
1707 // CPU_MP_DATA
1708 // +--------------------+ <-- CpuMpData->CpuData
1709 // CPU_AP_DATA (N)
1710 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1711 // CPU_INFO_IN_HOB (N)
1712 // +--------------------+
1713 //
1714 MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);
1715 BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;
1716 ApIdtBase = ALIGN_VALUE (BackupBufferAddr + ApResetVectorSize, 8);
1717 CpuMpData = (CPU_MP_DATA *) (ApIdtBase + VolatileRegisters.Idtr.Limit + 1);
1718 CpuMpData->Buffer = Buffer;
1719 CpuMpData->CpuApStackSize = ApStackSize;
1720 CpuMpData->BackupBuffer = BackupBufferAddr;
1721 CpuMpData->BackupBufferSize = ApResetVectorSize;
1722 CpuMpData->WakeupBuffer = (UINTN) -1;
1723 CpuMpData->CpuCount = 1;
1724 CpuMpData->BspNumber = 0;
1725 CpuMpData->WaitEvent = NULL;
1726 CpuMpData->SwitchBspFlag = FALSE;
1727 CpuMpData->CpuData = (CPU_AP_DATA *) (CpuMpData + 1);
1728 CpuMpData->CpuInfoInHob = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber);
1729 InitializeSpinLock(&CpuMpData->MpLock);
1730
1731 //
1732 // Make sure no memory usage outside of the allocated buffer.
1733 //
1734 ASSERT ((CpuMpData->CpuInfoInHob + sizeof (CPU_INFO_IN_HOB) * MaxLogicalProcessorNumber) ==
1735 Buffer + BufferSize);
1736
1737 //
1738 // Duplicate BSP's IDT to APs.
1739 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1740 //
1741 CopyMem ((VOID *)ApIdtBase, (VOID *)VolatileRegisters.Idtr.Base, VolatileRegisters.Idtr.Limit + 1);
1742 VolatileRegisters.Idtr.Base = ApIdtBase;
1743 //
1744 // Don't pass BSP's TR to APs to avoid AP init failure.
1745 //
1746 VolatileRegisters.Tr = 0;
1747 CopyMem (&CpuMpData->CpuData[0].VolatileRegisters, &VolatileRegisters, sizeof (VolatileRegisters));
1748 //
1749 // Set BSP basic information
1750 //
1751 InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer + ApStackSize);
1752 //
1753 // Save assembly code information
1754 //
1755 CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));
1756 //
1757 // Finally set AP loop mode
1758 //
1759 CpuMpData->ApLoopMode = ApLoopMode;
1760 DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));
1761
1762 CpuMpData->WakeUpByInitSipiSipi = (CpuMpData->ApLoopMode == ApInHltLoop);
1763
1764 //
1765 // Set up APs wakeup signal buffer
1766 //
1767 for (Index = 0; Index < MaxLogicalProcessorNumber; Index++) {
1768 CpuMpData->CpuData[Index].StartupApSignal =
1769 (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);
1770 }
1771 //
1772 // Enable the local APIC for Virtual Wire Mode.
1773 //
1774 ProgramVirtualWireMode ();
1775
1776 if (OldCpuMpData == NULL) {
1777 if (MaxLogicalProcessorNumber > 1) {
1778 //
1779 // Wakeup all APs and calculate the processor count in system
1780 //
1781 CollectProcessorCount (CpuMpData);
1782 }
1783 } else {
1784 //
1785 // APs have been wakeup before, just get the CPU Information
1786 // from HOB
1787 //
1788 CpuMpData->CpuCount = OldCpuMpData->CpuCount;
1789 CpuMpData->BspNumber = OldCpuMpData->BspNumber;
1790 CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;
1791 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
1792 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1793 InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);
1794 CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE;
1795 CpuMpData->CpuData[Index].ApFunction = 0;
1796 }
1797 }
1798
1799 if (!GetMicrocodePatchInfoFromHob (
1800 &CpuMpData->MicrocodePatchAddress,
1801 &CpuMpData->MicrocodePatchRegionSize
1802 )) {
1803 //
1804 // The microcode patch information cache HOB does not exist, which means
1805 // the microcode patches data has not been loaded into memory yet
1806 //
1807 ShadowMicrocodeUpdatePatch (CpuMpData);
1808 }
1809
1810 //
1811 // Detect and apply Microcode on BSP
1812 //
1813 MicrocodeDetect (CpuMpData, CpuMpData->BspNumber);
1814 //
1815 // Store BSP's MTRR setting
1816 //
1817 MtrrGetAllMtrrs (&CpuMpData->MtrrTable);
1818
1819 //
1820 // Wakeup APs to do some AP initialize sync (Microcode & MTRR)
1821 //
1822 if (CpuMpData->CpuCount > 1) {
1823 if (OldCpuMpData != NULL) {
1824 //
1825 // Only needs to use this flag for DXE phase to update the wake up
1826 // buffer. Wakeup buffer allocated in PEI phase is no longer valid
1827 // in DXE.
1828 //
1829 CpuMpData->InitFlag = ApInitReconfig;
1830 }
1831 WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData, TRUE);
1832 //
1833 // Wait for all APs finished initialization
1834 //
1835 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
1836 CpuPause ();
1837 }
1838 if (OldCpuMpData != NULL) {
1839 CpuMpData->InitFlag = ApInitDone;
1840 }
1841 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1842 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);
1843 }
1844 }
1845
1846 //
1847 // Initialize global data for MP support
1848 //
1849 InitMpGlobalData (CpuMpData);
1850
1851 return EFI_SUCCESS;
1852 }
1853
1854 /**
1855 Gets detailed MP-related information on the requested processor at the
1856 instant this call is made. This service may only be called from the BSP.
1857
1858 @param[in] ProcessorNumber The handle number of processor.
1859 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1860 the requested processor is deposited.
1861 @param[out] HealthData Return processor health data.
1862
1863 @retval EFI_SUCCESS Processor information was returned.
1864 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1865 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1866 @retval EFI_NOT_FOUND The processor with the handle specified by
1867 ProcessorNumber does not exist in the platform.
1868 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1869
1870 **/
1871 EFI_STATUS
1872 EFIAPI
1873 MpInitLibGetProcessorInfo (
1874 IN UINTN ProcessorNumber,
1875 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer,
1876 OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL
1877 )
1878 {
1879 CPU_MP_DATA *CpuMpData;
1880 UINTN CallerNumber;
1881 CPU_INFO_IN_HOB *CpuInfoInHob;
1882 UINTN OriginalProcessorNumber;
1883
1884 CpuMpData = GetCpuMpData ();
1885 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
1886
1887 //
1888 // Lower 24 bits contains the actual processor number.
1889 //
1890 OriginalProcessorNumber = ProcessorNumber;
1891 ProcessorNumber &= BIT24 - 1;
1892
1893 //
1894 // Check whether caller processor is BSP
1895 //
1896 MpInitLibWhoAmI (&CallerNumber);
1897 if (CallerNumber != CpuMpData->BspNumber) {
1898 return EFI_DEVICE_ERROR;
1899 }
1900
1901 if (ProcessorInfoBuffer == NULL) {
1902 return EFI_INVALID_PARAMETER;
1903 }
1904
1905 if (ProcessorNumber >= CpuMpData->CpuCount) {
1906 return EFI_NOT_FOUND;
1907 }
1908
1909 ProcessorInfoBuffer->ProcessorId = (UINT64) CpuInfoInHob[ProcessorNumber].ApicId;
1910 ProcessorInfoBuffer->StatusFlag = 0;
1911 if (ProcessorNumber == CpuMpData->BspNumber) {
1912 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;
1913 }
1914 if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) {
1915 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;
1916 }
1917 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {
1918 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;
1919 } else {
1920 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;
1921 }
1922
1923 //
1924 // Get processor location information
1925 //
1926 GetProcessorLocationByApicId (
1927 CpuInfoInHob[ProcessorNumber].ApicId,
1928 &ProcessorInfoBuffer->Location.Package,
1929 &ProcessorInfoBuffer->Location.Core,
1930 &ProcessorInfoBuffer->Location.Thread
1931 );
1932
1933 if ((OriginalProcessorNumber & CPU_V2_EXTENDED_TOPOLOGY) != 0) {
1934 GetProcessorLocation2ByApicId (
1935 CpuInfoInHob[ProcessorNumber].ApicId,
1936 &ProcessorInfoBuffer->ExtendedInformation.Location2.Package,
1937 &ProcessorInfoBuffer->ExtendedInformation.Location2.Die,
1938 &ProcessorInfoBuffer->ExtendedInformation.Location2.Tile,
1939 &ProcessorInfoBuffer->ExtendedInformation.Location2.Module,
1940 &ProcessorInfoBuffer->ExtendedInformation.Location2.Core,
1941 &ProcessorInfoBuffer->ExtendedInformation.Location2.Thread
1942 );
1943 }
1944
1945 if (HealthData != NULL) {
1946 HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;
1947 }
1948
1949 return EFI_SUCCESS;
1950 }
1951
1952 /**
1953 Worker function to switch the requested AP to be the BSP from that point onward.
1954
1955 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1956 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1957 enabled AP. Otherwise, it will be disabled.
1958
1959 @retval EFI_SUCCESS BSP successfully switched.
1960 @retval others Failed to switch BSP.
1961
1962 **/
1963 EFI_STATUS
1964 SwitchBSPWorker (
1965 IN UINTN ProcessorNumber,
1966 IN BOOLEAN EnableOldBSP
1967 )
1968 {
1969 CPU_MP_DATA *CpuMpData;
1970 UINTN CallerNumber;
1971 CPU_STATE State;
1972 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;
1973 BOOLEAN OldInterruptState;
1974 BOOLEAN OldTimerInterruptState;
1975
1976 //
1977 // Save and Disable Local APIC timer interrupt
1978 //
1979 OldTimerInterruptState = GetApicTimerInterruptState ();
1980 DisableApicTimerInterrupt ();
1981 //
1982 // Before send both BSP and AP to a procedure to exchange their roles,
1983 // interrupt must be disabled. This is because during the exchange role
1984 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1985 // be corrupted, since interrupt return address will be pushed to stack
1986 // by hardware.
1987 //
1988 OldInterruptState = SaveAndDisableInterrupts ();
1989
1990 //
1991 // Mask LINT0 & LINT1 for the old BSP
1992 //
1993 DisableLvtInterrupts ();
1994
1995 CpuMpData = GetCpuMpData ();
1996
1997 //
1998 // Check whether caller processor is BSP
1999 //
2000 MpInitLibWhoAmI (&CallerNumber);
2001 if (CallerNumber != CpuMpData->BspNumber) {
2002 return EFI_DEVICE_ERROR;
2003 }
2004
2005 if (ProcessorNumber >= CpuMpData->CpuCount) {
2006 return EFI_NOT_FOUND;
2007 }
2008
2009 //
2010 // Check whether specified AP is disabled
2011 //
2012 State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);
2013 if (State == CpuStateDisabled) {
2014 return EFI_INVALID_PARAMETER;
2015 }
2016
2017 //
2018 // Check whether ProcessorNumber specifies the current BSP
2019 //
2020 if (ProcessorNumber == CpuMpData->BspNumber) {
2021 return EFI_INVALID_PARAMETER;
2022 }
2023
2024 //
2025 // Check whether specified AP is busy
2026 //
2027 if (State == CpuStateBusy) {
2028 return EFI_NOT_READY;
2029 }
2030
2031 CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;
2032 CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;
2033 CpuMpData->SwitchBspFlag = TRUE;
2034 CpuMpData->NewBspNumber = ProcessorNumber;
2035
2036 //
2037 // Clear the BSP bit of MSR_IA32_APIC_BASE
2038 //
2039 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);
2040 ApicBaseMsr.Bits.BSP = 0;
2041 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);
2042
2043 //
2044 // Need to wakeUp AP (future BSP).
2045 //
2046 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData, TRUE);
2047
2048 AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);
2049
2050 //
2051 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
2052 //
2053 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);
2054 ApicBaseMsr.Bits.BSP = 1;
2055 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);
2056 ProgramVirtualWireMode ();
2057
2058 //
2059 // Wait for old BSP finished AP task
2060 //
2061 while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {
2062 CpuPause ();
2063 }
2064
2065 CpuMpData->SwitchBspFlag = FALSE;
2066 //
2067 // Set old BSP enable state
2068 //
2069 if (!EnableOldBSP) {
2070 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);
2071 } else {
2072 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle);
2073 }
2074 //
2075 // Save new BSP number
2076 //
2077 CpuMpData->BspNumber = (UINT32) ProcessorNumber;
2078
2079 //
2080 // Restore interrupt state.
2081 //
2082 SetInterruptState (OldInterruptState);
2083
2084 if (OldTimerInterruptState) {
2085 EnableApicTimerInterrupt ();
2086 }
2087
2088 return EFI_SUCCESS;
2089 }
2090
2091 /**
2092 Worker function to let the caller enable or disable an AP from this point onward.
2093 This service may only be called from the BSP.
2094
2095 @param[in] ProcessorNumber The handle number of AP.
2096 @param[in] EnableAP Specifies the new state for the processor for
2097 enabled, FALSE for disabled.
2098 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
2099 the new health status of the AP.
2100
2101 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
2102 @retval others Failed to Enable/Disable AP.
2103
2104 **/
2105 EFI_STATUS
2106 EnableDisableApWorker (
2107 IN UINTN ProcessorNumber,
2108 IN BOOLEAN EnableAP,
2109 IN UINT32 *HealthFlag OPTIONAL
2110 )
2111 {
2112 CPU_MP_DATA *CpuMpData;
2113 UINTN CallerNumber;
2114
2115 CpuMpData = GetCpuMpData ();
2116
2117 //
2118 // Check whether caller processor is BSP
2119 //
2120 MpInitLibWhoAmI (&CallerNumber);
2121 if (CallerNumber != CpuMpData->BspNumber) {
2122 return EFI_DEVICE_ERROR;
2123 }
2124
2125 if (ProcessorNumber == CpuMpData->BspNumber) {
2126 return EFI_INVALID_PARAMETER;
2127 }
2128
2129 if (ProcessorNumber >= CpuMpData->CpuCount) {
2130 return EFI_NOT_FOUND;
2131 }
2132
2133 if (!EnableAP) {
2134 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);
2135 } else {
2136 ResetProcessorToIdleState (ProcessorNumber);
2137 }
2138
2139 if (HealthFlag != NULL) {
2140 CpuMpData->CpuData[ProcessorNumber].CpuHealthy =
2141 (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);
2142 }
2143
2144 return EFI_SUCCESS;
2145 }
2146
2147 /**
2148 This return the handle number for the calling processor. This service may be
2149 called from the BSP and APs.
2150
2151 @param[out] ProcessorNumber Pointer to the handle number of AP.
2152 The range is from 0 to the total number of
2153 logical processors minus 1. The total number of
2154 logical processors can be retrieved by
2155 MpInitLibGetNumberOfProcessors().
2156
2157 @retval EFI_SUCCESS The current processor handle number was returned
2158 in ProcessorNumber.
2159 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2160 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2161
2162 **/
2163 EFI_STATUS
2164 EFIAPI
2165 MpInitLibWhoAmI (
2166 OUT UINTN *ProcessorNumber
2167 )
2168 {
2169 CPU_MP_DATA *CpuMpData;
2170
2171 if (ProcessorNumber == NULL) {
2172 return EFI_INVALID_PARAMETER;
2173 }
2174
2175 CpuMpData = GetCpuMpData ();
2176
2177 return GetProcessorNumber (CpuMpData, ProcessorNumber);
2178 }
2179
2180 /**
2181 Retrieves the number of logical processor in the platform and the number of
2182 those logical processors that are enabled on this boot. This service may only
2183 be called from the BSP.
2184
2185 @param[out] NumberOfProcessors Pointer to the total number of logical
2186 processors in the system, including the BSP
2187 and disabled APs.
2188 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2189 processors that exist in system, including
2190 the BSP.
2191
2192 @retval EFI_SUCCESS The number of logical processors and enabled
2193 logical processors was retrieved.
2194 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2195 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2196 is NULL.
2197 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2198
2199 **/
2200 EFI_STATUS
2201 EFIAPI
2202 MpInitLibGetNumberOfProcessors (
2203 OUT UINTN *NumberOfProcessors, OPTIONAL
2204 OUT UINTN *NumberOfEnabledProcessors OPTIONAL
2205 )
2206 {
2207 CPU_MP_DATA *CpuMpData;
2208 UINTN CallerNumber;
2209 UINTN ProcessorNumber;
2210 UINTN EnabledProcessorNumber;
2211 UINTN Index;
2212
2213 CpuMpData = GetCpuMpData ();
2214
2215 if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {
2216 return EFI_INVALID_PARAMETER;
2217 }
2218
2219 //
2220 // Check whether caller processor is BSP
2221 //
2222 MpInitLibWhoAmI (&CallerNumber);
2223 if (CallerNumber != CpuMpData->BspNumber) {
2224 return EFI_DEVICE_ERROR;
2225 }
2226
2227 ProcessorNumber = CpuMpData->CpuCount;
2228 EnabledProcessorNumber = 0;
2229 for (Index = 0; Index < ProcessorNumber; Index++) {
2230 if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {
2231 EnabledProcessorNumber ++;
2232 }
2233 }
2234
2235 if (NumberOfProcessors != NULL) {
2236 *NumberOfProcessors = ProcessorNumber;
2237 }
2238 if (NumberOfEnabledProcessors != NULL) {
2239 *NumberOfEnabledProcessors = EnabledProcessorNumber;
2240 }
2241
2242 return EFI_SUCCESS;
2243 }
2244
2245
2246 /**
2247 Worker function to execute a caller provided function on all enabled APs.
2248
2249 @param[in] Procedure A pointer to the function to be run on
2250 enabled APs of the system.
2251 @param[in] SingleThread If TRUE, then all the enabled APs execute
2252 the function specified by Procedure one by
2253 one, in ascending order of processor handle
2254 number. If FALSE, then all the enabled APs
2255 execute the function specified by Procedure
2256 simultaneously.
2257 @param[in] ExcludeBsp Whether let BSP also trig this task.
2258 @param[in] WaitEvent The event created by the caller with CreateEvent()
2259 service.
2260 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2261 APs to return from Procedure, either for
2262 blocking or non-blocking mode.
2263 @param[in] ProcedureArgument The parameter passed into Procedure for
2264 all APs.
2265 @param[out] FailedCpuList If all APs finish successfully, then its
2266 content is set to NULL. If not all APs
2267 finish before timeout expires, then its
2268 content is set to address of the buffer
2269 holding handle numbers of the failed APs.
2270
2271 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2272 the timeout expired.
2273 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2274 to all enabled APs.
2275 @retval others Failed to Startup all APs.
2276
2277 **/
2278 EFI_STATUS
2279 StartupAllCPUsWorker (
2280 IN EFI_AP_PROCEDURE Procedure,
2281 IN BOOLEAN SingleThread,
2282 IN BOOLEAN ExcludeBsp,
2283 IN EFI_EVENT WaitEvent OPTIONAL,
2284 IN UINTN TimeoutInMicroseconds,
2285 IN VOID *ProcedureArgument OPTIONAL,
2286 OUT UINTN **FailedCpuList OPTIONAL
2287 )
2288 {
2289 EFI_STATUS Status;
2290 CPU_MP_DATA *CpuMpData;
2291 UINTN ProcessorCount;
2292 UINTN ProcessorNumber;
2293 UINTN CallerNumber;
2294 CPU_AP_DATA *CpuData;
2295 BOOLEAN HasEnabledAp;
2296 CPU_STATE ApState;
2297
2298 CpuMpData = GetCpuMpData ();
2299
2300 if (FailedCpuList != NULL) {
2301 *FailedCpuList = NULL;
2302 }
2303
2304 if (CpuMpData->CpuCount == 1 && ExcludeBsp) {
2305 return EFI_NOT_STARTED;
2306 }
2307
2308 if (Procedure == NULL) {
2309 return EFI_INVALID_PARAMETER;
2310 }
2311
2312 //
2313 // Check whether caller processor is BSP
2314 //
2315 MpInitLibWhoAmI (&CallerNumber);
2316 if (CallerNumber != CpuMpData->BspNumber) {
2317 return EFI_DEVICE_ERROR;
2318 }
2319
2320 //
2321 // Update AP state
2322 //
2323 CheckAndUpdateApsStatus ();
2324
2325 ProcessorCount = CpuMpData->CpuCount;
2326 HasEnabledAp = FALSE;
2327 //
2328 // Check whether all enabled APs are idle.
2329 // If any enabled AP is not idle, return EFI_NOT_READY.
2330 //
2331 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
2332 CpuData = &CpuMpData->CpuData[ProcessorNumber];
2333 if (ProcessorNumber != CpuMpData->BspNumber) {
2334 ApState = GetApState (CpuData);
2335 if (ApState != CpuStateDisabled) {
2336 HasEnabledAp = TRUE;
2337 if (ApState != CpuStateIdle) {
2338 //
2339 // If any enabled APs are busy, return EFI_NOT_READY.
2340 //
2341 return EFI_NOT_READY;
2342 }
2343 }
2344 }
2345 }
2346
2347 if (!HasEnabledAp && ExcludeBsp) {
2348 //
2349 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2350 //
2351 return EFI_NOT_STARTED;
2352 }
2353
2354 CpuMpData->RunningCount = 0;
2355 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
2356 CpuData = &CpuMpData->CpuData[ProcessorNumber];
2357 CpuData->Waiting = FALSE;
2358 if (ProcessorNumber != CpuMpData->BspNumber) {
2359 if (CpuData->State == CpuStateIdle) {
2360 //
2361 // Mark this processor as responsible for current calling.
2362 //
2363 CpuData->Waiting = TRUE;
2364 CpuMpData->RunningCount++;
2365 }
2366 }
2367 }
2368
2369 CpuMpData->Procedure = Procedure;
2370 CpuMpData->ProcArguments = ProcedureArgument;
2371 CpuMpData->SingleThread = SingleThread;
2372 CpuMpData->FinishedCount = 0;
2373 CpuMpData->FailedCpuList = FailedCpuList;
2374 CpuMpData->ExpectedTime = CalculateTimeout (
2375 TimeoutInMicroseconds,
2376 &CpuMpData->CurrentTime
2377 );
2378 CpuMpData->TotalTime = 0;
2379 CpuMpData->WaitEvent = WaitEvent;
2380
2381 if (!SingleThread) {
2382 WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument, FALSE);
2383 } else {
2384 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
2385 if (ProcessorNumber == CallerNumber) {
2386 continue;
2387 }
2388 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
2389 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument, TRUE);
2390 break;
2391 }
2392 }
2393 }
2394
2395 if (!ExcludeBsp) {
2396 //
2397 // Start BSP.
2398 //
2399 Procedure (ProcedureArgument);
2400 }
2401
2402 Status = EFI_SUCCESS;
2403 if (WaitEvent == NULL) {
2404 do {
2405 Status = CheckAllAPs ();
2406 } while (Status == EFI_NOT_READY);
2407 }
2408
2409 return Status;
2410 }
2411
2412 /**
2413 Worker function to let the caller get one enabled AP to execute a caller-provided
2414 function.
2415
2416 @param[in] Procedure A pointer to the function to be run on
2417 enabled APs of the system.
2418 @param[in] ProcessorNumber The handle number of the AP.
2419 @param[in] WaitEvent The event created by the caller with CreateEvent()
2420 service.
2421 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2422 APs to return from Procedure, either for
2423 blocking or non-blocking mode.
2424 @param[in] ProcedureArgument The parameter passed into Procedure for
2425 all APs.
2426 @param[out] Finished If AP returns from Procedure before the
2427 timeout expires, its content is set to TRUE.
2428 Otherwise, the value is set to FALSE.
2429
2430 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2431 the timeout expires.
2432 @retval others Failed to Startup AP.
2433
2434 **/
2435 EFI_STATUS
2436 StartupThisAPWorker (
2437 IN EFI_AP_PROCEDURE Procedure,
2438 IN UINTN ProcessorNumber,
2439 IN EFI_EVENT WaitEvent OPTIONAL,
2440 IN UINTN TimeoutInMicroseconds,
2441 IN VOID *ProcedureArgument OPTIONAL,
2442 OUT BOOLEAN *Finished OPTIONAL
2443 )
2444 {
2445 EFI_STATUS Status;
2446 CPU_MP_DATA *CpuMpData;
2447 CPU_AP_DATA *CpuData;
2448 UINTN CallerNumber;
2449
2450 CpuMpData = GetCpuMpData ();
2451
2452 if (Finished != NULL) {
2453 *Finished = FALSE;
2454 }
2455
2456 //
2457 // Check whether caller processor is BSP
2458 //
2459 MpInitLibWhoAmI (&CallerNumber);
2460 if (CallerNumber != CpuMpData->BspNumber) {
2461 return EFI_DEVICE_ERROR;
2462 }
2463
2464 //
2465 // Check whether processor with the handle specified by ProcessorNumber exists
2466 //
2467 if (ProcessorNumber >= CpuMpData->CpuCount) {
2468 return EFI_NOT_FOUND;
2469 }
2470
2471 //
2472 // Check whether specified processor is BSP
2473 //
2474 if (ProcessorNumber == CpuMpData->BspNumber) {
2475 return EFI_INVALID_PARAMETER;
2476 }
2477
2478 //
2479 // Check parameter Procedure
2480 //
2481 if (Procedure == NULL) {
2482 return EFI_INVALID_PARAMETER;
2483 }
2484
2485 //
2486 // Update AP state
2487 //
2488 CheckAndUpdateApsStatus ();
2489
2490 //
2491 // Check whether specified AP is disabled
2492 //
2493 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {
2494 return EFI_INVALID_PARAMETER;
2495 }
2496
2497 //
2498 // If WaitEvent is not NULL, execute in non-blocking mode.
2499 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2500 // CheckAPsStatus() will check completion and timeout periodically.
2501 //
2502 CpuData = &CpuMpData->CpuData[ProcessorNumber];
2503 CpuData->WaitEvent = WaitEvent;
2504 CpuData->Finished = Finished;
2505 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);
2506 CpuData->TotalTime = 0;
2507
2508 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument, TRUE);
2509
2510 //
2511 // If WaitEvent is NULL, execute in blocking mode.
2512 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2513 //
2514 Status = EFI_SUCCESS;
2515 if (WaitEvent == NULL) {
2516 do {
2517 Status = CheckThisAP (ProcessorNumber);
2518 } while (Status == EFI_NOT_READY);
2519 }
2520
2521 return Status;
2522 }
2523
2524 /**
2525 Get pointer to CPU MP Data structure from GUIDed HOB.
2526
2527 @return The pointer to CPU MP Data structure.
2528 **/
2529 CPU_MP_DATA *
2530 GetCpuMpDataFromGuidedHob (
2531 VOID
2532 )
2533 {
2534 EFI_HOB_GUID_TYPE *GuidHob;
2535 VOID *DataInHob;
2536 CPU_MP_DATA *CpuMpData;
2537
2538 CpuMpData = NULL;
2539 GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);
2540 if (GuidHob != NULL) {
2541 DataInHob = GET_GUID_HOB_DATA (GuidHob);
2542 CpuMpData = (CPU_MP_DATA *) (*(UINTN *) DataInHob);
2543 }
2544 return CpuMpData;
2545 }
2546
2547 /**
2548 This service executes a caller provided function on all enabled CPUs.
2549
2550 @param[in] Procedure A pointer to the function to be run on
2551 enabled APs of the system. See type
2552 EFI_AP_PROCEDURE.
2553 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2554 APs to return from Procedure, either for
2555 blocking or non-blocking mode. Zero means
2556 infinity. TimeoutInMicroseconds is ignored
2557 for BSP.
2558 @param[in] ProcedureArgument The parameter passed into Procedure for
2559 all APs.
2560
2561 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2562 the timeout expired.
2563 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2564 to all enabled CPUs.
2565 @retval EFI_DEVICE_ERROR Caller processor is AP.
2566 @retval EFI_NOT_READY Any enabled APs are busy.
2567 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2568 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2569 all enabled APs have finished.
2570 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2571
2572 **/
2573 EFI_STATUS
2574 EFIAPI
2575 MpInitLibStartupAllCPUs (
2576 IN EFI_AP_PROCEDURE Procedure,
2577 IN UINTN TimeoutInMicroseconds,
2578 IN VOID *ProcedureArgument OPTIONAL
2579 )
2580 {
2581 return StartupAllCPUsWorker (
2582 Procedure,
2583 FALSE,
2584 FALSE,
2585 NULL,
2586 TimeoutInMicroseconds,
2587 ProcedureArgument,
2588 NULL
2589 );
2590 }