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