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