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