]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/MpLib.c
UefiCpuPkg/MpLib: fix potential overflow issue.
[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 NumApsExecuting
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 = NumApsExecuting;
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
666 //
667 // Place AP is specified loop mode
668 //
669 if (CpuMpData->ApLoopMode == ApInHltLoop) {
670 //
671 // Save AP volatile registers
672 //
673 SaveVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters);
674 //
675 // Place AP in HLT-loop
676 //
677 while (TRUE) {
678 DisableInterrupts ();
679 CpuSleep ();
680 CpuPause ();
681 }
682 }
683 while (TRUE) {
684 DisableInterrupts ();
685 if (CpuMpData->ApLoopMode == ApInMwaitLoop) {
686 //
687 // Place AP in MWAIT-loop
688 //
689 AsmMonitor ((UINTN) ApStartupSignalBuffer, 0, 0);
690 if (*ApStartupSignalBuffer != WAKEUP_AP_SIGNAL) {
691 //
692 // Check AP start-up signal again.
693 // If AP start-up signal is not set, place AP into
694 // the specified C-state
695 //
696 AsmMwait (CpuMpData->ApTargetCState << 4, 0);
697 }
698 } else if (CpuMpData->ApLoopMode == ApInRunLoop) {
699 //
700 // Place AP in Run-loop
701 //
702 CpuPause ();
703 } else {
704 ASSERT (FALSE);
705 }
706
707 //
708 // If AP start-up signal is written, AP is waken up
709 // otherwise place AP in loop again
710 //
711 if (*ApStartupSignalBuffer == WAKEUP_AP_SIGNAL) {
712 break;
713 }
714 }
715 }
716 }
717
718 /**
719 Wait for AP wakeup and write AP start-up signal till AP is waken up.
720
721 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
722 **/
723 VOID
724 WaitApWakeup (
725 IN volatile UINT32 *ApStartupSignalBuffer
726 )
727 {
728 //
729 // If AP is waken up, StartupApSignal should be cleared.
730 // Otherwise, write StartupApSignal again till AP waken up.
731 //
732 while (InterlockedCompareExchange32 (
733 (UINT32 *) ApStartupSignalBuffer,
734 WAKEUP_AP_SIGNAL,
735 WAKEUP_AP_SIGNAL
736 ) != 0) {
737 CpuPause ();
738 }
739 }
740
741 /**
742 This function will fill the exchange info structure.
743
744 @param[in] CpuMpData Pointer to CPU MP Data
745
746 **/
747 VOID
748 FillExchangeInfoData (
749 IN CPU_MP_DATA *CpuMpData
750 )
751 {
752 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;
753
754 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;
755 ExchangeInfo->Lock = 0;
756 ExchangeInfo->StackStart = CpuMpData->Buffer;
757 ExchangeInfo->StackSize = CpuMpData->CpuApStackSize;
758 ExchangeInfo->BufferStart = CpuMpData->WakeupBuffer;
759 ExchangeInfo->ModeOffset = CpuMpData->AddressMap.ModeEntryOffset;
760
761 ExchangeInfo->CodeSegment = AsmReadCs ();
762 ExchangeInfo->DataSegment = AsmReadDs ();
763
764 ExchangeInfo->Cr3 = AsmReadCr3 ();
765
766 ExchangeInfo->CFunction = (UINTN) ApWakeupFunction;
767 ExchangeInfo->NumApsExecuting = 0;
768 ExchangeInfo->InitFlag = (UINTN) CpuMpData->InitFlag;
769 ExchangeInfo->CpuInfo = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
770 ExchangeInfo->CpuMpData = CpuMpData;
771
772 ExchangeInfo->EnableExecuteDisable = IsBspExecuteDisableEnabled ();
773
774 ExchangeInfo->InitializeFloatingPointUnitsAddress = (UINTN)InitializeFloatingPointUnits;
775
776 //
777 // Get the BSP's data of GDT and IDT
778 //
779 AsmReadGdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->GdtrProfile);
780 AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);
781 }
782
783 /**
784 Helper function that waits until the finished AP count reaches the specified
785 limit, or the specified timeout elapses (whichever comes first).
786
787 @param[in] CpuMpData Pointer to CPU MP Data.
788 @param[in] FinishedApLimit The number of finished APs to wait for.
789 @param[in] TimeLimit The number of microseconds to wait for.
790 **/
791 VOID
792 TimedWaitForApFinish (
793 IN CPU_MP_DATA *CpuMpData,
794 IN UINT32 FinishedApLimit,
795 IN UINT32 TimeLimit
796 );
797
798 /**
799 Get available system memory below 1MB by specified size.
800
801 @param[in] CpuMpData The pointer to CPU MP Data structure.
802 **/
803 VOID
804 BackupAndPrepareWakeupBuffer(
805 IN CPU_MP_DATA *CpuMpData
806 )
807 {
808 CopyMem (
809 (VOID *) CpuMpData->BackupBuffer,
810 (VOID *) CpuMpData->WakeupBuffer,
811 CpuMpData->BackupBufferSize
812 );
813 CopyMem (
814 (VOID *) CpuMpData->WakeupBuffer,
815 (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,
816 CpuMpData->AddressMap.RendezvousFunnelSize
817 );
818 }
819
820 /**
821 Restore wakeup buffer data.
822
823 @param[in] CpuMpData The pointer to CPU MP Data structure.
824 **/
825 VOID
826 RestoreWakeupBuffer(
827 IN CPU_MP_DATA *CpuMpData
828 )
829 {
830 CopyMem (
831 (VOID *) CpuMpData->WakeupBuffer,
832 (VOID *) CpuMpData->BackupBuffer,
833 CpuMpData->BackupBufferSize
834 );
835 }
836
837 /**
838 Allocate reset vector buffer.
839
840 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
841 **/
842 VOID
843 AllocateResetVector (
844 IN OUT CPU_MP_DATA *CpuMpData
845 )
846 {
847 UINTN ApResetVectorSize;
848
849 if (CpuMpData->WakeupBuffer == (UINTN) -1) {
850 ApResetVectorSize = CpuMpData->AddressMap.RendezvousFunnelSize +
851 sizeof (MP_CPU_EXCHANGE_INFO);
852
853 CpuMpData->WakeupBuffer = GetWakeupBuffer (ApResetVectorSize);
854 CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN)
855 (CpuMpData->WakeupBuffer + CpuMpData->AddressMap.RendezvousFunnelSize);
856 }
857 BackupAndPrepareWakeupBuffer (CpuMpData);
858 }
859
860 /**
861 Free AP reset vector buffer.
862
863 @param[in] CpuMpData The pointer to CPU MP Data structure.
864 **/
865 VOID
866 FreeResetVector (
867 IN CPU_MP_DATA *CpuMpData
868 )
869 {
870 RestoreWakeupBuffer (CpuMpData);
871 }
872
873 /**
874 This function will be called by BSP to wakeup AP.
875
876 @param[in] CpuMpData Pointer to CPU MP Data
877 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
878 FALSE: Send IPI to AP by ApicId
879 @param[in] ProcessorNumber The handle number of specified processor
880 @param[in] Procedure The function to be invoked by AP
881 @param[in] ProcedureArgument The argument to be passed into AP function
882 **/
883 VOID
884 WakeUpAP (
885 IN CPU_MP_DATA *CpuMpData,
886 IN BOOLEAN Broadcast,
887 IN UINTN ProcessorNumber,
888 IN EFI_AP_PROCEDURE Procedure, OPTIONAL
889 IN VOID *ProcedureArgument OPTIONAL
890 )
891 {
892 volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;
893 UINTN Index;
894 CPU_AP_DATA *CpuData;
895 BOOLEAN ResetVectorRequired;
896 CPU_INFO_IN_HOB *CpuInfoInHob;
897
898 CpuMpData->FinishedCount = 0;
899 ResetVectorRequired = FALSE;
900
901 if (CpuMpData->ApLoopMode == ApInHltLoop ||
902 CpuMpData->InitFlag != ApInitDone) {
903 ResetVectorRequired = TRUE;
904 AllocateResetVector (CpuMpData);
905 FillExchangeInfoData (CpuMpData);
906 SaveLocalApicTimerSetting (CpuMpData);
907 } else if (CpuMpData->ApLoopMode == ApInMwaitLoop) {
908 //
909 // Get AP target C-state each time when waking up AP,
910 // for it maybe updated by platform again
911 //
912 CpuMpData->ApTargetCState = PcdGet8 (PcdCpuApTargetCstate);
913 }
914
915 ExchangeInfo = CpuMpData->MpCpuExchangeInfo;
916
917 if (Broadcast) {
918 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
919 if (Index != CpuMpData->BspNumber) {
920 CpuData = &CpuMpData->CpuData[Index];
921 CpuData->ApFunction = (UINTN) Procedure;
922 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;
923 SetApState (CpuData, CpuStateReady);
924 if (CpuMpData->InitFlag != ApInitConfig) {
925 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;
926 }
927 }
928 }
929 if (ResetVectorRequired) {
930 //
931 // Wakeup all APs
932 //
933 SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);
934 }
935 if (CpuMpData->InitFlag == ApInitConfig) {
936 //
937 // Wait for all potential APs waken up in one specified period
938 //
939 TimedWaitForApFinish (
940 CpuMpData,
941 PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,
942 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)
943 );
944 } else {
945 //
946 // Wait all APs waken up if this is not the 1st broadcast of SIPI
947 //
948 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
949 CpuData = &CpuMpData->CpuData[Index];
950 if (Index != CpuMpData->BspNumber) {
951 WaitApWakeup (CpuData->StartupApSignal);
952 }
953 }
954 }
955 } else {
956 CpuData = &CpuMpData->CpuData[ProcessorNumber];
957 CpuData->ApFunction = (UINTN) Procedure;
958 CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;
959 SetApState (CpuData, CpuStateReady);
960 //
961 // Wakeup specified AP
962 //
963 ASSERT (CpuMpData->InitFlag != ApInitConfig);
964 *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;
965 if (ResetVectorRequired) {
966 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
967 SendInitSipiSipi (
968 CpuInfoInHob[ProcessorNumber].ApicId,
969 (UINT32) ExchangeInfo->BufferStart
970 );
971 }
972 //
973 // Wait specified AP waken up
974 //
975 WaitApWakeup (CpuData->StartupApSignal);
976 }
977
978 if (ResetVectorRequired) {
979 FreeResetVector (CpuMpData);
980 }
981 }
982
983 /**
984 Calculate timeout value and return the current performance counter value.
985
986 Calculate the number of performance counter ticks required for a timeout.
987 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
988 as infinity.
989
990 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
991 @param[out] CurrentTime Returns the current value of the performance counter.
992
993 @return Expected time stamp counter for timeout.
994 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
995 as infinity.
996
997 **/
998 UINT64
999 CalculateTimeout (
1000 IN UINTN TimeoutInMicroseconds,
1001 OUT UINT64 *CurrentTime
1002 )
1003 {
1004 UINT64 TimeoutInSeconds;
1005 UINT64 TimestampCounterFreq;
1006
1007 //
1008 // Read the current value of the performance counter
1009 //
1010 *CurrentTime = GetPerformanceCounter ();
1011
1012 //
1013 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1014 // as infinity.
1015 //
1016 if (TimeoutInMicroseconds == 0) {
1017 return 0;
1018 }
1019
1020 //
1021 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1022 // in Hz.
1023 //
1024 TimestampCounterFreq = GetPerformanceCounterProperties (NULL, NULL);
1025
1026 //
1027 // Check the potential overflow before calculate the number of ticks for the timeout value.
1028 //
1029 if (DivU64x64Remainder (MAX_UINT64, TimeoutInMicroseconds, NULL) < TimestampCounterFreq) {
1030 //
1031 // Convert microseconds into seconds if direct multiplication overflows
1032 //
1033 TimeoutInSeconds = DivU64x32 (TimeoutInMicroseconds, 1000000);
1034 //
1035 // Assertion if the final tick count exceeds MAX_UINT64
1036 //
1037 ASSERT (DivU64x64Remainder (MAX_UINT64, TimeoutInSeconds, NULL) >= TimestampCounterFreq);
1038 return MultU64x64 (TimestampCounterFreq, TimeoutInSeconds);
1039 } else {
1040 //
1041 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1042 // it by 1,000,000, to get the number of ticks for the timeout value.
1043 //
1044 return DivU64x32 (
1045 MultU64x64 (
1046 TimestampCounterFreq,
1047 TimeoutInMicroseconds
1048 ),
1049 1000000
1050 );
1051 }
1052 }
1053
1054 /**
1055 Checks whether timeout expires.
1056
1057 Check whether the number of elapsed performance counter ticks required for
1058 a timeout condition has been reached.
1059 If Timeout is zero, which means infinity, return value is always FALSE.
1060
1061 @param[in, out] PreviousTime On input, the value of the performance counter
1062 when it was last read.
1063 On output, the current value of the performance
1064 counter
1065 @param[in] TotalTime The total amount of elapsed time in performance
1066 counter ticks.
1067 @param[in] Timeout The number of performance counter ticks required
1068 to reach a timeout condition.
1069
1070 @retval TRUE A timeout condition has been reached.
1071 @retval FALSE A timeout condition has not been reached.
1072
1073 **/
1074 BOOLEAN
1075 CheckTimeout (
1076 IN OUT UINT64 *PreviousTime,
1077 IN UINT64 *TotalTime,
1078 IN UINT64 Timeout
1079 )
1080 {
1081 UINT64 Start;
1082 UINT64 End;
1083 UINT64 CurrentTime;
1084 INT64 Delta;
1085 INT64 Cycle;
1086
1087 if (Timeout == 0) {
1088 return FALSE;
1089 }
1090 GetPerformanceCounterProperties (&Start, &End);
1091 Cycle = End - Start;
1092 if (Cycle < 0) {
1093 Cycle = -Cycle;
1094 }
1095 Cycle++;
1096 CurrentTime = GetPerformanceCounter();
1097 Delta = (INT64) (CurrentTime - *PreviousTime);
1098 if (Start > End) {
1099 Delta = -Delta;
1100 }
1101 if (Delta < 0) {
1102 Delta += Cycle;
1103 }
1104 *TotalTime += Delta;
1105 *PreviousTime = CurrentTime;
1106 if (*TotalTime > Timeout) {
1107 return TRUE;
1108 }
1109 return FALSE;
1110 }
1111
1112 /**
1113 Helper function that waits until the finished AP count reaches the specified
1114 limit, or the specified timeout elapses (whichever comes first).
1115
1116 @param[in] CpuMpData Pointer to CPU MP Data.
1117 @param[in] FinishedApLimit The number of finished APs to wait for.
1118 @param[in] TimeLimit The number of microseconds to wait for.
1119 **/
1120 VOID
1121 TimedWaitForApFinish (
1122 IN CPU_MP_DATA *CpuMpData,
1123 IN UINT32 FinishedApLimit,
1124 IN UINT32 TimeLimit
1125 )
1126 {
1127 //
1128 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1129 // "infinity", so check for (TimeLimit == 0) explicitly.
1130 //
1131 if (TimeLimit == 0) {
1132 return;
1133 }
1134
1135 CpuMpData->TotalTime = 0;
1136 CpuMpData->ExpectedTime = CalculateTimeout (
1137 TimeLimit,
1138 &CpuMpData->CurrentTime
1139 );
1140 while (CpuMpData->FinishedCount < FinishedApLimit &&
1141 !CheckTimeout (
1142 &CpuMpData->CurrentTime,
1143 &CpuMpData->TotalTime,
1144 CpuMpData->ExpectedTime
1145 )) {
1146 CpuPause ();
1147 }
1148
1149 if (CpuMpData->FinishedCount >= FinishedApLimit) {
1150 DEBUG ((
1151 DEBUG_VERBOSE,
1152 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1153 __FUNCTION__,
1154 FinishedApLimit,
1155 DivU64x64Remainder (
1156 MultU64x32 (CpuMpData->TotalTime, 1000000),
1157 GetPerformanceCounterProperties (NULL, NULL),
1158 NULL
1159 )
1160 ));
1161 }
1162 }
1163
1164 /**
1165 Reset an AP to Idle state.
1166
1167 Any task being executed by the AP will be aborted and the AP
1168 will be waiting for a new task in Wait-For-SIPI state.
1169
1170 @param[in] ProcessorNumber The handle number of processor.
1171 **/
1172 VOID
1173 ResetProcessorToIdleState (
1174 IN UINTN ProcessorNumber
1175 )
1176 {
1177 CPU_MP_DATA *CpuMpData;
1178
1179 CpuMpData = GetCpuMpData ();
1180
1181 CpuMpData->InitFlag = ApInitReconfig;
1182 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, NULL, NULL);
1183 while (CpuMpData->FinishedCount < 1) {
1184 CpuPause ();
1185 }
1186 CpuMpData->InitFlag = ApInitDone;
1187
1188 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
1189 }
1190
1191 /**
1192 Searches for the next waiting AP.
1193
1194 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1195
1196 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1197
1198 @retval EFI_SUCCESS The next waiting AP has been found.
1199 @retval EFI_NOT_FOUND No waiting AP exists.
1200
1201 **/
1202 EFI_STATUS
1203 GetNextWaitingProcessorNumber (
1204 OUT UINTN *NextProcessorNumber
1205 )
1206 {
1207 UINTN ProcessorNumber;
1208 CPU_MP_DATA *CpuMpData;
1209
1210 CpuMpData = GetCpuMpData ();
1211
1212 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1213 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1214 *NextProcessorNumber = ProcessorNumber;
1215 return EFI_SUCCESS;
1216 }
1217 }
1218
1219 return EFI_NOT_FOUND;
1220 }
1221
1222 /** Checks status of specified AP.
1223
1224 This function checks whether the specified AP has finished the task assigned
1225 by StartupThisAP(), and whether timeout expires.
1226
1227 @param[in] ProcessorNumber The handle number of processor.
1228
1229 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1230 @retval EFI_TIMEOUT The timeout expires.
1231 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1232 **/
1233 EFI_STATUS
1234 CheckThisAP (
1235 IN UINTN ProcessorNumber
1236 )
1237 {
1238 CPU_MP_DATA *CpuMpData;
1239 CPU_AP_DATA *CpuData;
1240
1241 CpuMpData = GetCpuMpData ();
1242 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1243
1244 //
1245 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1246 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1247 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1248 //
1249 //
1250 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1251 //
1252 if (GetApState(CpuData) == CpuStateFinished) {
1253 if (CpuData->Finished != NULL) {
1254 *(CpuData->Finished) = TRUE;
1255 }
1256 SetApState (CpuData, CpuStateIdle);
1257 return EFI_SUCCESS;
1258 } else {
1259 //
1260 // If timeout expires for StartupThisAP(), report timeout.
1261 //
1262 if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {
1263 if (CpuData->Finished != NULL) {
1264 *(CpuData->Finished) = FALSE;
1265 }
1266 //
1267 // Reset failed AP to idle state
1268 //
1269 ResetProcessorToIdleState (ProcessorNumber);
1270
1271 return EFI_TIMEOUT;
1272 }
1273 }
1274 return EFI_NOT_READY;
1275 }
1276
1277 /**
1278 Checks status of all APs.
1279
1280 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1281 and whether timeout expires.
1282
1283 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1284 @retval EFI_TIMEOUT The timeout expires.
1285 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1286 **/
1287 EFI_STATUS
1288 CheckAllAPs (
1289 VOID
1290 )
1291 {
1292 UINTN ProcessorNumber;
1293 UINTN NextProcessorNumber;
1294 UINTN ListIndex;
1295 EFI_STATUS Status;
1296 CPU_MP_DATA *CpuMpData;
1297 CPU_AP_DATA *CpuData;
1298
1299 CpuMpData = GetCpuMpData ();
1300
1301 NextProcessorNumber = 0;
1302
1303 //
1304 // Go through all APs that are responsible for the StartupAllAPs().
1305 //
1306 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1307 if (!CpuMpData->CpuData[ProcessorNumber].Waiting) {
1308 continue;
1309 }
1310
1311 CpuData = &CpuMpData->CpuData[ProcessorNumber];
1312 //
1313 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1314 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1315 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1316 //
1317 if (GetApState(CpuData) == CpuStateFinished) {
1318 CpuMpData->RunningCount ++;
1319 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
1320 SetApState(CpuData, CpuStateIdle);
1321
1322 //
1323 // If in Single Thread mode, then search for the next waiting AP for execution.
1324 //
1325 if (CpuMpData->SingleThread) {
1326 Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);
1327
1328 if (!EFI_ERROR (Status)) {
1329 WakeUpAP (
1330 CpuMpData,
1331 FALSE,
1332 (UINT32) NextProcessorNumber,
1333 CpuMpData->Procedure,
1334 CpuMpData->ProcArguments
1335 );
1336 }
1337 }
1338 }
1339 }
1340
1341 //
1342 // If all APs finish, return EFI_SUCCESS.
1343 //
1344 if (CpuMpData->RunningCount == CpuMpData->StartCount) {
1345 return EFI_SUCCESS;
1346 }
1347
1348 //
1349 // If timeout expires, report timeout.
1350 //
1351 if (CheckTimeout (
1352 &CpuMpData->CurrentTime,
1353 &CpuMpData->TotalTime,
1354 CpuMpData->ExpectedTime)
1355 ) {
1356 //
1357 // If FailedCpuList is not NULL, record all failed APs in it.
1358 //
1359 if (CpuMpData->FailedCpuList != NULL) {
1360 *CpuMpData->FailedCpuList =
1361 AllocatePool ((CpuMpData->StartCount - CpuMpData->FinishedCount + 1) * sizeof (UINTN));
1362 ASSERT (*CpuMpData->FailedCpuList != NULL);
1363 }
1364 ListIndex = 0;
1365
1366 for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {
1367 //
1368 // Check whether this processor is responsible for StartupAllAPs().
1369 //
1370 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
1371 //
1372 // Reset failed APs to idle state
1373 //
1374 ResetProcessorToIdleState (ProcessorNumber);
1375 CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;
1376 if (CpuMpData->FailedCpuList != NULL) {
1377 (*CpuMpData->FailedCpuList)[ListIndex++] = ProcessorNumber;
1378 }
1379 }
1380 }
1381 if (CpuMpData->FailedCpuList != NULL) {
1382 (*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST;
1383 }
1384 return EFI_TIMEOUT;
1385 }
1386 return EFI_NOT_READY;
1387 }
1388
1389 /**
1390 MP Initialize Library initialization.
1391
1392 This service will allocate AP reset vector and wakeup all APs to do APs
1393 initialization.
1394
1395 This service must be invoked before all other MP Initialize Library
1396 service are invoked.
1397
1398 @retval EFI_SUCCESS MP initialization succeeds.
1399 @retval Others MP initialization fails.
1400
1401 **/
1402 EFI_STATUS
1403 EFIAPI
1404 MpInitLibInitialize (
1405 VOID
1406 )
1407 {
1408 CPU_MP_DATA *OldCpuMpData;
1409 CPU_INFO_IN_HOB *CpuInfoInHob;
1410 UINT32 MaxLogicalProcessorNumber;
1411 UINT32 ApStackSize;
1412 MP_ASSEMBLY_ADDRESS_MAP AddressMap;
1413 UINTN BufferSize;
1414 UINT32 MonitorFilterSize;
1415 VOID *MpBuffer;
1416 UINTN Buffer;
1417 CPU_MP_DATA *CpuMpData;
1418 UINT8 ApLoopMode;
1419 UINT8 *MonitorBuffer;
1420 UINTN Index;
1421 UINTN ApResetVectorSize;
1422 UINTN BackupBufferAddr;
1423
1424 OldCpuMpData = GetCpuMpDataFromGuidedHob ();
1425 if (OldCpuMpData == NULL) {
1426 MaxLogicalProcessorNumber = PcdGet32(PcdCpuMaxLogicalProcessorNumber);
1427 } else {
1428 MaxLogicalProcessorNumber = OldCpuMpData->CpuCount;
1429 }
1430 ASSERT (MaxLogicalProcessorNumber != 0);
1431
1432 AsmGetAddressMap (&AddressMap);
1433 ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);
1434 ApStackSize = PcdGet32(PcdCpuApStackSize);
1435 ApLoopMode = GetApLoopMode (&MonitorFilterSize);
1436
1437 BufferSize = ApStackSize * MaxLogicalProcessorNumber;
1438 BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;
1439 BufferSize += sizeof (CPU_MP_DATA);
1440 BufferSize += ApResetVectorSize;
1441 BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;
1442 MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));
1443 ASSERT (MpBuffer != NULL);
1444 ZeroMem (MpBuffer, BufferSize);
1445 Buffer = (UINTN) MpBuffer;
1446
1447 MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);
1448 BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;
1449 CpuMpData = (CPU_MP_DATA *) (BackupBufferAddr + ApResetVectorSize);
1450 CpuMpData->Buffer = Buffer;
1451 CpuMpData->CpuApStackSize = ApStackSize;
1452 CpuMpData->BackupBuffer = BackupBufferAddr;
1453 CpuMpData->BackupBufferSize = ApResetVectorSize;
1454 CpuMpData->WakeupBuffer = (UINTN) -1;
1455 CpuMpData->CpuCount = 1;
1456 CpuMpData->BspNumber = 0;
1457 CpuMpData->WaitEvent = NULL;
1458 CpuMpData->SwitchBspFlag = FALSE;
1459 CpuMpData->CpuData = (CPU_AP_DATA *) (CpuMpData + 1);
1460 CpuMpData->CpuInfoInHob = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber);
1461 InitializeSpinLock(&CpuMpData->MpLock);
1462 //
1463 // Save BSP's Control registers to APs
1464 //
1465 SaveVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters);
1466 //
1467 // Set BSP basic information
1468 //
1469 InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer);
1470 //
1471 // Save assembly code information
1472 //
1473 CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));
1474 //
1475 // Finally set AP loop mode
1476 //
1477 CpuMpData->ApLoopMode = ApLoopMode;
1478 DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));
1479 //
1480 // Set up APs wakeup signal buffer
1481 //
1482 for (Index = 0; Index < MaxLogicalProcessorNumber; Index++) {
1483 CpuMpData->CpuData[Index].StartupApSignal =
1484 (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);
1485 }
1486 //
1487 // Load Microcode on BSP
1488 //
1489 MicrocodeDetect (CpuMpData);
1490 //
1491 // Store BSP's MTRR setting
1492 //
1493 MtrrGetAllMtrrs (&CpuMpData->MtrrTable);
1494 //
1495 // Enable the local APIC for Virtual Wire Mode.
1496 //
1497 ProgramVirtualWireMode ();
1498
1499 if (OldCpuMpData == NULL) {
1500 if (MaxLogicalProcessorNumber > 1) {
1501 //
1502 // Wakeup all APs and calculate the processor count in system
1503 //
1504 CollectProcessorCount (CpuMpData);
1505 }
1506 } else {
1507 //
1508 // APs have been wakeup before, just get the CPU Information
1509 // from HOB
1510 //
1511 CpuMpData->CpuCount = OldCpuMpData->CpuCount;
1512 CpuMpData->BspNumber = OldCpuMpData->BspNumber;
1513 CpuMpData->InitFlag = ApInitReconfig;
1514 CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;
1515 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
1516 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1517 InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);
1518 if (CpuInfoInHob[Index].InitialApicId >= 255 || Index > 254) {
1519 CpuMpData->X2ApicEnable = TRUE;
1520 }
1521 CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE;
1522 CpuMpData->CpuData[Index].ApFunction = 0;
1523 CopyMem (
1524 &CpuMpData->CpuData[Index].VolatileRegisters,
1525 &CpuMpData->CpuData[0].VolatileRegisters,
1526 sizeof (CPU_VOLATILE_REGISTERS)
1527 );
1528 }
1529 if (MaxLogicalProcessorNumber > 1) {
1530 //
1531 // Wakeup APs to do some AP initialize sync
1532 //
1533 WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData);
1534 //
1535 // Wait for all APs finished initialization
1536 //
1537 while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
1538 CpuPause ();
1539 }
1540 CpuMpData->InitFlag = ApInitDone;
1541 for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
1542 SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);
1543 }
1544 }
1545 }
1546
1547 //
1548 // Initialize global data for MP support
1549 //
1550 InitMpGlobalData (CpuMpData);
1551
1552 return EFI_SUCCESS;
1553 }
1554
1555 /**
1556 Gets detailed MP-related information on the requested processor at the
1557 instant this call is made. This service may only be called from the BSP.
1558
1559 @param[in] ProcessorNumber The handle number of processor.
1560 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1561 the requested processor is deposited.
1562 @param[out] HealthData Return processor health data.
1563
1564 @retval EFI_SUCCESS Processor information was returned.
1565 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1566 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1567 @retval EFI_NOT_FOUND The processor with the handle specified by
1568 ProcessorNumber does not exist in the platform.
1569 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1570
1571 **/
1572 EFI_STATUS
1573 EFIAPI
1574 MpInitLibGetProcessorInfo (
1575 IN UINTN ProcessorNumber,
1576 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer,
1577 OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL
1578 )
1579 {
1580 CPU_MP_DATA *CpuMpData;
1581 UINTN CallerNumber;
1582 CPU_INFO_IN_HOB *CpuInfoInHob;
1583
1584 CpuMpData = GetCpuMpData ();
1585 CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
1586
1587 //
1588 // Check whether caller processor is BSP
1589 //
1590 MpInitLibWhoAmI (&CallerNumber);
1591 if (CallerNumber != CpuMpData->BspNumber) {
1592 return EFI_DEVICE_ERROR;
1593 }
1594
1595 if (ProcessorInfoBuffer == NULL) {
1596 return EFI_INVALID_PARAMETER;
1597 }
1598
1599 if (ProcessorNumber >= CpuMpData->CpuCount) {
1600 return EFI_NOT_FOUND;
1601 }
1602
1603 ProcessorInfoBuffer->ProcessorId = (UINT64) CpuInfoInHob[ProcessorNumber].ApicId;
1604 ProcessorInfoBuffer->StatusFlag = 0;
1605 if (ProcessorNumber == CpuMpData->BspNumber) {
1606 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;
1607 }
1608 if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) {
1609 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;
1610 }
1611 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {
1612 ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;
1613 } else {
1614 ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;
1615 }
1616
1617 //
1618 // Get processor location information
1619 //
1620 GetProcessorLocationByApicId (
1621 CpuInfoInHob[ProcessorNumber].ApicId,
1622 &ProcessorInfoBuffer->Location.Package,
1623 &ProcessorInfoBuffer->Location.Core,
1624 &ProcessorInfoBuffer->Location.Thread
1625 );
1626
1627 if (HealthData != NULL) {
1628 HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;
1629 }
1630
1631 return EFI_SUCCESS;
1632 }
1633
1634 /**
1635 Worker function to switch the requested AP to be the BSP from that point onward.
1636
1637 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1638 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1639 enabled AP. Otherwise, it will be disabled.
1640
1641 @retval EFI_SUCCESS BSP successfully switched.
1642 @retval others Failed to switch BSP.
1643
1644 **/
1645 EFI_STATUS
1646 SwitchBSPWorker (
1647 IN UINTN ProcessorNumber,
1648 IN BOOLEAN EnableOldBSP
1649 )
1650 {
1651 CPU_MP_DATA *CpuMpData;
1652 UINTN CallerNumber;
1653 CPU_STATE State;
1654 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;
1655 BOOLEAN OldInterruptState;
1656 BOOLEAN OldTimerInterruptState;
1657
1658 //
1659 // Save and Disable Local APIC timer interrupt
1660 //
1661 OldTimerInterruptState = GetApicTimerInterruptState ();
1662 DisableApicTimerInterrupt ();
1663 //
1664 // Before send both BSP and AP to a procedure to exchange their roles,
1665 // interrupt must be disabled. This is because during the exchange role
1666 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1667 // be corrupted, since interrupt return address will be pushed to stack
1668 // by hardware.
1669 //
1670 OldInterruptState = SaveAndDisableInterrupts ();
1671
1672 //
1673 // Mask LINT0 & LINT1 for the old BSP
1674 //
1675 DisableLvtInterrupts ();
1676
1677 CpuMpData = GetCpuMpData ();
1678
1679 //
1680 // Check whether caller processor is BSP
1681 //
1682 MpInitLibWhoAmI (&CallerNumber);
1683 if (CallerNumber != CpuMpData->BspNumber) {
1684 return EFI_DEVICE_ERROR;
1685 }
1686
1687 if (ProcessorNumber >= CpuMpData->CpuCount) {
1688 return EFI_NOT_FOUND;
1689 }
1690
1691 //
1692 // Check whether specified AP is disabled
1693 //
1694 State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);
1695 if (State == CpuStateDisabled) {
1696 return EFI_INVALID_PARAMETER;
1697 }
1698
1699 //
1700 // Check whether ProcessorNumber specifies the current BSP
1701 //
1702 if (ProcessorNumber == CpuMpData->BspNumber) {
1703 return EFI_INVALID_PARAMETER;
1704 }
1705
1706 //
1707 // Check whether specified AP is busy
1708 //
1709 if (State == CpuStateBusy) {
1710 return EFI_NOT_READY;
1711 }
1712
1713 CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;
1714 CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;
1715 CpuMpData->SwitchBspFlag = TRUE;
1716 CpuMpData->NewBspNumber = ProcessorNumber;
1717
1718 //
1719 // Clear the BSP bit of MSR_IA32_APIC_BASE
1720 //
1721 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);
1722 ApicBaseMsr.Bits.BSP = 0;
1723 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);
1724
1725 //
1726 // Need to wakeUp AP (future BSP).
1727 //
1728 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData);
1729
1730 AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);
1731
1732 //
1733 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1734 //
1735 ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);
1736 ApicBaseMsr.Bits.BSP = 1;
1737 AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);
1738
1739 //
1740 // Wait for old BSP finished AP task
1741 //
1742 while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {
1743 CpuPause ();
1744 }
1745
1746 CpuMpData->SwitchBspFlag = FALSE;
1747 //
1748 // Set old BSP enable state
1749 //
1750 if (!EnableOldBSP) {
1751 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);
1752 } else {
1753 SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle);
1754 }
1755 //
1756 // Save new BSP number
1757 //
1758 CpuMpData->BspNumber = (UINT32) ProcessorNumber;
1759
1760 //
1761 // Restore interrupt state.
1762 //
1763 SetInterruptState (OldInterruptState);
1764
1765 if (OldTimerInterruptState) {
1766 EnableApicTimerInterrupt ();
1767 }
1768
1769 return EFI_SUCCESS;
1770 }
1771
1772 /**
1773 Worker function to let the caller enable or disable an AP from this point onward.
1774 This service may only be called from the BSP.
1775
1776 @param[in] ProcessorNumber The handle number of AP.
1777 @param[in] EnableAP Specifies the new state for the processor for
1778 enabled, FALSE for disabled.
1779 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1780 the new health status of the AP.
1781
1782 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1783 @retval others Failed to Enable/Disable AP.
1784
1785 **/
1786 EFI_STATUS
1787 EnableDisableApWorker (
1788 IN UINTN ProcessorNumber,
1789 IN BOOLEAN EnableAP,
1790 IN UINT32 *HealthFlag OPTIONAL
1791 )
1792 {
1793 CPU_MP_DATA *CpuMpData;
1794 UINTN CallerNumber;
1795
1796 CpuMpData = GetCpuMpData ();
1797
1798 //
1799 // Check whether caller processor is BSP
1800 //
1801 MpInitLibWhoAmI (&CallerNumber);
1802 if (CallerNumber != CpuMpData->BspNumber) {
1803 return EFI_DEVICE_ERROR;
1804 }
1805
1806 if (ProcessorNumber == CpuMpData->BspNumber) {
1807 return EFI_INVALID_PARAMETER;
1808 }
1809
1810 if (ProcessorNumber >= CpuMpData->CpuCount) {
1811 return EFI_NOT_FOUND;
1812 }
1813
1814 if (!EnableAP) {
1815 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);
1816 } else {
1817 SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
1818 }
1819
1820 if (HealthFlag != NULL) {
1821 CpuMpData->CpuData[ProcessorNumber].CpuHealthy =
1822 (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);
1823 }
1824
1825 return EFI_SUCCESS;
1826 }
1827
1828 /**
1829 This return the handle number for the calling processor. This service may be
1830 called from the BSP and APs.
1831
1832 @param[out] ProcessorNumber Pointer to the handle number of AP.
1833 The range is from 0 to the total number of
1834 logical processors minus 1. The total number of
1835 logical processors can be retrieved by
1836 MpInitLibGetNumberOfProcessors().
1837
1838 @retval EFI_SUCCESS The current processor handle number was returned
1839 in ProcessorNumber.
1840 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1841 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1842
1843 **/
1844 EFI_STATUS
1845 EFIAPI
1846 MpInitLibWhoAmI (
1847 OUT UINTN *ProcessorNumber
1848 )
1849 {
1850 CPU_MP_DATA *CpuMpData;
1851
1852 if (ProcessorNumber == NULL) {
1853 return EFI_INVALID_PARAMETER;
1854 }
1855
1856 CpuMpData = GetCpuMpData ();
1857
1858 return GetProcessorNumber (CpuMpData, ProcessorNumber);
1859 }
1860
1861 /**
1862 Retrieves the number of logical processor in the platform and the number of
1863 those logical processors that are enabled on this boot. This service may only
1864 be called from the BSP.
1865
1866 @param[out] NumberOfProcessors Pointer to the total number of logical
1867 processors in the system, including the BSP
1868 and disabled APs.
1869 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1870 processors that exist in system, including
1871 the BSP.
1872
1873 @retval EFI_SUCCESS The number of logical processors and enabled
1874 logical processors was retrieved.
1875 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1876 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1877 is NULL.
1878 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1879
1880 **/
1881 EFI_STATUS
1882 EFIAPI
1883 MpInitLibGetNumberOfProcessors (
1884 OUT UINTN *NumberOfProcessors, OPTIONAL
1885 OUT UINTN *NumberOfEnabledProcessors OPTIONAL
1886 )
1887 {
1888 CPU_MP_DATA *CpuMpData;
1889 UINTN CallerNumber;
1890 UINTN ProcessorNumber;
1891 UINTN EnabledProcessorNumber;
1892 UINTN Index;
1893
1894 CpuMpData = GetCpuMpData ();
1895
1896 if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {
1897 return EFI_INVALID_PARAMETER;
1898 }
1899
1900 //
1901 // Check whether caller processor is BSP
1902 //
1903 MpInitLibWhoAmI (&CallerNumber);
1904 if (CallerNumber != CpuMpData->BspNumber) {
1905 return EFI_DEVICE_ERROR;
1906 }
1907
1908 ProcessorNumber = CpuMpData->CpuCount;
1909 EnabledProcessorNumber = 0;
1910 for (Index = 0; Index < ProcessorNumber; Index++) {
1911 if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {
1912 EnabledProcessorNumber ++;
1913 }
1914 }
1915
1916 if (NumberOfProcessors != NULL) {
1917 *NumberOfProcessors = ProcessorNumber;
1918 }
1919 if (NumberOfEnabledProcessors != NULL) {
1920 *NumberOfEnabledProcessors = EnabledProcessorNumber;
1921 }
1922
1923 return EFI_SUCCESS;
1924 }
1925
1926
1927 /**
1928 Worker function to execute a caller provided function on all enabled APs.
1929
1930 @param[in] Procedure A pointer to the function to be run on
1931 enabled APs of the system.
1932 @param[in] SingleThread If TRUE, then all the enabled APs execute
1933 the function specified by Procedure one by
1934 one, in ascending order of processor handle
1935 number. If FALSE, then all the enabled APs
1936 execute the function specified by Procedure
1937 simultaneously.
1938 @param[in] WaitEvent The event created by the caller with CreateEvent()
1939 service.
1940 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1941 APs to return from Procedure, either for
1942 blocking or non-blocking mode.
1943 @param[in] ProcedureArgument The parameter passed into Procedure for
1944 all APs.
1945 @param[out] FailedCpuList If all APs finish successfully, then its
1946 content is set to NULL. If not all APs
1947 finish before timeout expires, then its
1948 content is set to address of the buffer
1949 holding handle numbers of the failed APs.
1950
1951 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1952 the timeout expired.
1953 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1954 to all enabled APs.
1955 @retval others Failed to Startup all APs.
1956
1957 **/
1958 EFI_STATUS
1959 StartupAllAPsWorker (
1960 IN EFI_AP_PROCEDURE Procedure,
1961 IN BOOLEAN SingleThread,
1962 IN EFI_EVENT WaitEvent OPTIONAL,
1963 IN UINTN TimeoutInMicroseconds,
1964 IN VOID *ProcedureArgument OPTIONAL,
1965 OUT UINTN **FailedCpuList OPTIONAL
1966 )
1967 {
1968 EFI_STATUS Status;
1969 CPU_MP_DATA *CpuMpData;
1970 UINTN ProcessorCount;
1971 UINTN ProcessorNumber;
1972 UINTN CallerNumber;
1973 CPU_AP_DATA *CpuData;
1974 BOOLEAN HasEnabledAp;
1975 CPU_STATE ApState;
1976
1977 CpuMpData = GetCpuMpData ();
1978
1979 if (FailedCpuList != NULL) {
1980 *FailedCpuList = NULL;
1981 }
1982
1983 if (CpuMpData->CpuCount == 1) {
1984 return EFI_NOT_STARTED;
1985 }
1986
1987 if (Procedure == NULL) {
1988 return EFI_INVALID_PARAMETER;
1989 }
1990
1991 //
1992 // Check whether caller processor is BSP
1993 //
1994 MpInitLibWhoAmI (&CallerNumber);
1995 if (CallerNumber != CpuMpData->BspNumber) {
1996 return EFI_DEVICE_ERROR;
1997 }
1998
1999 //
2000 // Update AP state
2001 //
2002 CheckAndUpdateApsStatus ();
2003
2004 ProcessorCount = CpuMpData->CpuCount;
2005 HasEnabledAp = FALSE;
2006 //
2007 // Check whether all enabled APs are idle.
2008 // If any enabled AP is not idle, return EFI_NOT_READY.
2009 //
2010 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
2011 CpuData = &CpuMpData->CpuData[ProcessorNumber];
2012 if (ProcessorNumber != CpuMpData->BspNumber) {
2013 ApState = GetApState (CpuData);
2014 if (ApState != CpuStateDisabled) {
2015 HasEnabledAp = TRUE;
2016 if (ApState != CpuStateIdle) {
2017 //
2018 // If any enabled APs are busy, return EFI_NOT_READY.
2019 //
2020 return EFI_NOT_READY;
2021 }
2022 }
2023 }
2024 }
2025
2026 if (!HasEnabledAp) {
2027 //
2028 // If no enabled AP exists, return EFI_NOT_STARTED.
2029 //
2030 return EFI_NOT_STARTED;
2031 }
2032
2033 CpuMpData->StartCount = 0;
2034 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
2035 CpuData = &CpuMpData->CpuData[ProcessorNumber];
2036 CpuData->Waiting = FALSE;
2037 if (ProcessorNumber != CpuMpData->BspNumber) {
2038 if (CpuData->State == CpuStateIdle) {
2039 //
2040 // Mark this processor as responsible for current calling.
2041 //
2042 CpuData->Waiting = TRUE;
2043 CpuMpData->StartCount++;
2044 }
2045 }
2046 }
2047
2048 CpuMpData->Procedure = Procedure;
2049 CpuMpData->ProcArguments = ProcedureArgument;
2050 CpuMpData->SingleThread = SingleThread;
2051 CpuMpData->FinishedCount = 0;
2052 CpuMpData->RunningCount = 0;
2053 CpuMpData->FailedCpuList = FailedCpuList;
2054 CpuMpData->ExpectedTime = CalculateTimeout (
2055 TimeoutInMicroseconds,
2056 &CpuMpData->CurrentTime
2057 );
2058 CpuMpData->TotalTime = 0;
2059 CpuMpData->WaitEvent = WaitEvent;
2060
2061 if (!SingleThread) {
2062 WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument);
2063 } else {
2064 for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {
2065 if (ProcessorNumber == CallerNumber) {
2066 continue;
2067 }
2068 if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
2069 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);
2070 break;
2071 }
2072 }
2073 }
2074
2075 Status = EFI_SUCCESS;
2076 if (WaitEvent == NULL) {
2077 do {
2078 Status = CheckAllAPs ();
2079 } while (Status == EFI_NOT_READY);
2080 }
2081
2082 return Status;
2083 }
2084
2085 /**
2086 Worker function to let the caller get one enabled AP to execute a caller-provided
2087 function.
2088
2089 @param[in] Procedure A pointer to the function to be run on
2090 enabled APs of the system.
2091 @param[in] ProcessorNumber The handle number of the AP.
2092 @param[in] WaitEvent The event created by the caller with CreateEvent()
2093 service.
2094 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2095 APs to return from Procedure, either for
2096 blocking or non-blocking mode.
2097 @param[in] ProcedureArgument The parameter passed into Procedure for
2098 all APs.
2099 @param[out] Finished If AP returns from Procedure before the
2100 timeout expires, its content is set to TRUE.
2101 Otherwise, the value is set to FALSE.
2102
2103 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2104 the timeout expires.
2105 @retval others Failed to Startup AP.
2106
2107 **/
2108 EFI_STATUS
2109 StartupThisAPWorker (
2110 IN EFI_AP_PROCEDURE Procedure,
2111 IN UINTN ProcessorNumber,
2112 IN EFI_EVENT WaitEvent OPTIONAL,
2113 IN UINTN TimeoutInMicroseconds,
2114 IN VOID *ProcedureArgument OPTIONAL,
2115 OUT BOOLEAN *Finished OPTIONAL
2116 )
2117 {
2118 EFI_STATUS Status;
2119 CPU_MP_DATA *CpuMpData;
2120 CPU_AP_DATA *CpuData;
2121 UINTN CallerNumber;
2122
2123 CpuMpData = GetCpuMpData ();
2124
2125 if (Finished != NULL) {
2126 *Finished = FALSE;
2127 }
2128
2129 //
2130 // Check whether caller processor is BSP
2131 //
2132 MpInitLibWhoAmI (&CallerNumber);
2133 if (CallerNumber != CpuMpData->BspNumber) {
2134 return EFI_DEVICE_ERROR;
2135 }
2136
2137 //
2138 // Check whether processor with the handle specified by ProcessorNumber exists
2139 //
2140 if (ProcessorNumber >= CpuMpData->CpuCount) {
2141 return EFI_NOT_FOUND;
2142 }
2143
2144 //
2145 // Check whether specified processor is BSP
2146 //
2147 if (ProcessorNumber == CpuMpData->BspNumber) {
2148 return EFI_INVALID_PARAMETER;
2149 }
2150
2151 //
2152 // Check parameter Procedure
2153 //
2154 if (Procedure == NULL) {
2155 return EFI_INVALID_PARAMETER;
2156 }
2157
2158 //
2159 // Update AP state
2160 //
2161 CheckAndUpdateApsStatus ();
2162
2163 //
2164 // Check whether specified AP is disabled
2165 //
2166 if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {
2167 return EFI_INVALID_PARAMETER;
2168 }
2169
2170 //
2171 // If WaitEvent is not NULL, execute in non-blocking mode.
2172 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2173 // CheckAPsStatus() will check completion and timeout periodically.
2174 //
2175 CpuData = &CpuMpData->CpuData[ProcessorNumber];
2176 CpuData->WaitEvent = WaitEvent;
2177 CpuData->Finished = Finished;
2178 CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);
2179 CpuData->TotalTime = 0;
2180
2181 WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);
2182
2183 //
2184 // If WaitEvent is NULL, execute in blocking mode.
2185 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2186 //
2187 Status = EFI_SUCCESS;
2188 if (WaitEvent == NULL) {
2189 do {
2190 Status = CheckThisAP (ProcessorNumber);
2191 } while (Status == EFI_NOT_READY);
2192 }
2193
2194 return Status;
2195 }
2196
2197 /**
2198 Get pointer to CPU MP Data structure from GUIDed HOB.
2199
2200 @return The pointer to CPU MP Data structure.
2201 **/
2202 CPU_MP_DATA *
2203 GetCpuMpDataFromGuidedHob (
2204 VOID
2205 )
2206 {
2207 EFI_HOB_GUID_TYPE *GuidHob;
2208 VOID *DataInHob;
2209 CPU_MP_DATA *CpuMpData;
2210
2211 CpuMpData = NULL;
2212 GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);
2213 if (GuidHob != NULL) {
2214 DataInHob = GET_GUID_HOB_DATA (GuidHob);
2215 CpuMpData = (CPU_MP_DATA *) (*(UINTN *) DataInHob);
2216 }
2217 return CpuMpData;
2218 }
2219