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