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