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