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