4 This local APIC library instance supports xAPIC mode only.
6 Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
7 Copyright (c) 2017 - 2020, AMD Inc. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Register/Intel/Cpuid.h>
14 #include <Register/Amd/Cpuid.h>
15 #include <Register/Intel/Msr.h>
16 #include <Register/Intel/LocalApic.h>
18 #include <Library/BaseLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/LocalApicLib.h>
21 #include <Library/IoLib.h>
22 #include <Library/TimerLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/CpuLib.h>
25 #include <Library/UefiCpuLib.h>
28 // Library internal functions
32 Determine if the CPU supports the Local APIC Base Address MSR.
34 @retval TRUE The CPU supports the Local APIC Base Address MSR.
35 @retval FALSE The CPU does not support the Local APIC Base Address MSR.
39 LocalApicBaseAddressMsrSupported (
46 AsmCpuid (1, &RegEax
, NULL
, NULL
, NULL
);
47 FamilyId
= BitFieldRead32 (RegEax
, 8, 11);
48 if ((FamilyId
== 0x04) || (FamilyId
== 0x05)) {
50 // CPUs with a FamilyId of 0x04 or 0x05 do not support the
51 // Local APIC Base Address MSR
60 Retrieve the base address of local APIC.
62 @return The base address of local APIC.
67 GetLocalApicBaseAddress (
71 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
73 if (!LocalApicBaseAddressMsrSupported ()) {
75 // If CPU does not support Local APIC Base Address MSR, then retrieve
76 // Local APIC Base Address from PCD
78 return PcdGet32 (PcdCpuLocalApicBaseAddress
);
81 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
83 return (UINTN
)(LShiftU64 ((UINT64
)ApicBaseMsr
.Bits
.ApicBaseHi
, 32)) +
84 (((UINTN
)ApicBaseMsr
.Bits
.ApicBase
) << 12);
88 Set the base address of local APIC.
90 If BaseAddress is not aligned on a 4KB boundary, then ASSERT().
92 @param[in] BaseAddress Local APIC base address to be set.
97 SetLocalApicBaseAddress (
101 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
103 ASSERT ((BaseAddress
& (SIZE_4KB
- 1)) == 0);
105 if (!LocalApicBaseAddressMsrSupported ()) {
107 // Ignore set request if the CPU does not support APIC Base Address MSR
112 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
114 ApicBaseMsr
.Bits
.ApicBase
= (UINT32
)(BaseAddress
>> 12);
115 ApicBaseMsr
.Bits
.ApicBaseHi
= (UINT32
)(RShiftU64 ((UINT64
)BaseAddress
, 32));
117 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
121 Read from a local APIC register.
123 This function reads from a local APIC register either in xAPIC or x2APIC mode.
124 It is required that in xAPIC mode wider registers (64-bit or 256-bit) must be
125 accessed using multiple 32-bit loads or stores, so this function only performs
128 @param MmioOffset The MMIO offset of the local APIC register in xAPIC mode.
129 It must be 16-byte aligned.
131 @return 32-bit Value read from the register.
139 ASSERT ((MmioOffset
& 0xf) == 0);
140 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC
);
142 return MmioRead32 (GetLocalApicBaseAddress () + MmioOffset
);
146 Write to a local APIC register.
148 This function writes to a local APIC register either in xAPIC or x2APIC mode.
149 It is required that in xAPIC mode wider registers (64-bit or 256-bit) must be
150 accessed using multiple 32-bit loads or stores, so this function only performs
153 if the register index is invalid or unsupported in current APIC mode, then ASSERT.
155 @param MmioOffset The MMIO offset of the local APIC register in xAPIC mode.
156 It must be 16-byte aligned.
157 @param Value Value to be written to the register.
166 ASSERT ((MmioOffset
& 0xf) == 0);
167 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC
);
169 MmioWrite32 (GetLocalApicBaseAddress () + MmioOffset
, Value
);
173 Send an IPI by writing to ICR.
175 This function returns after the IPI has been accepted by the target processor.
177 @param IcrLow 32-bit value to be written to the low half of ICR.
178 @param ApicId APIC ID of the target processor if this IPI is targeted for a specific processor.
186 LOCAL_APIC_ICR_LOW IcrLowReg
;
188 BOOLEAN InterruptState
;
190 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC
);
191 ASSERT (ApicId
<= 0xff);
193 InterruptState
= SaveAndDisableInterrupts ();
196 // Save existing contents of ICR high 32 bits
198 IcrHigh
= ReadLocalApicReg (XAPIC_ICR_HIGH_OFFSET
);
201 // Wait for DeliveryStatus clear in case a previous IPI
202 // is still being sent
205 IcrLowReg
.Uint32
= ReadLocalApicReg (XAPIC_ICR_LOW_OFFSET
);
206 } while (IcrLowReg
.Bits
.DeliveryStatus
!= 0);
209 // For xAPIC, the act of writing to the low doubleword of the ICR causes the IPI to be sent.
211 WriteLocalApicReg (XAPIC_ICR_HIGH_OFFSET
, ApicId
<< 24);
212 WriteLocalApicReg (XAPIC_ICR_LOW_OFFSET
, IcrLow
);
215 // Wait for DeliveryStatus clear again
218 IcrLowReg
.Uint32
= ReadLocalApicReg (XAPIC_ICR_LOW_OFFSET
);
219 } while (IcrLowReg
.Bits
.DeliveryStatus
!= 0);
222 // And restore old contents of ICR high
224 WriteLocalApicReg (XAPIC_ICR_HIGH_OFFSET
, IcrHigh
);
226 SetInterruptState (InterruptState
);
230 // Library API implementation functions
234 Get the current local APIC mode.
236 If local APIC is disabled, then ASSERT.
238 @retval LOCAL_APIC_MODE_XAPIC current APIC mode is xAPIC.
239 @retval LOCAL_APIC_MODE_X2APIC current APIC mode is x2APIC.
249 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
252 // Check to see if the CPU supports the APIC Base Address MSR
254 if (LocalApicBaseAddressMsrSupported ()) {
255 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
257 // Local APIC should have been enabled
259 ASSERT (ApicBaseMsr
.Bits
.EN
!= 0);
260 ASSERT (ApicBaseMsr
.Bits
.EXTD
== 0);
264 return LOCAL_APIC_MODE_XAPIC
;
268 Set the current local APIC mode.
270 If the specified local APIC mode is not valid, then ASSERT.
271 If the specified local APIC mode can't be set as current, then ASSERT.
273 @param ApicMode APIC mode to be set.
275 @note This API must not be called from an interrupt handler or SMI handler.
276 It may result in unpredictable behavior.
284 ASSERT (ApicMode
== LOCAL_APIC_MODE_XAPIC
);
285 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC
);
289 Get the initial local APIC ID of the executing processor assigned by hardware upon power on or reset.
291 In xAPIC mode, the initial local APIC ID may be different from current APIC ID.
292 In x2APIC mode, the local APIC ID can't be changed and there is no concept of initial APIC ID. In this case,
293 the 32-bit local APIC ID is returned as initial APIC ID.
295 @return 32-bit initial local APIC ID of the executing processor.
304 UINT32 MaxCpuIdIndex
;
307 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC
);
310 // Get the max index of basic CPUID
312 AsmCpuid (CPUID_SIGNATURE
, &MaxCpuIdIndex
, NULL
, NULL
, NULL
);
315 // If CPUID Leaf B is supported,
316 // And CPUID.0BH:EBX[15:0] reports a non-zero value,
317 // Then the initial 32-bit APIC ID = CPUID.0BH:EDX
318 // Else the initial 8-bit APIC ID = CPUID.1:EBX[31:24]
320 if (MaxCpuIdIndex
>= CPUID_EXTENDED_TOPOLOGY
) {
321 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY
, 0, NULL
, &RegEbx
, NULL
, &ApicId
);
322 if ((RegEbx
& (BIT16
- 1)) != 0) {
327 AsmCpuid (CPUID_VERSION_INFO
, NULL
, &RegEbx
, NULL
, NULL
);
332 Get the local APIC ID of the executing processor.
334 @return 32-bit local APIC ID of the executing processor.
344 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC
);
346 if ((ApicId
= GetInitialApicId ()) < 0x100) {
348 // If the initial local APIC ID is less 0x100, read APIC ID from
349 // XAPIC_ID_OFFSET, otherwise return the initial local APIC ID.
351 ApicId
= ReadLocalApicReg (XAPIC_ID_OFFSET
);
359 Get the value of the local APIC version register.
361 @return the value of the local APIC version register.
369 return ReadLocalApicReg (XAPIC_VERSION_OFFSET
);
373 Send a Fixed IPI to a specified target processor.
375 This function returns after the IPI has been accepted by the target processor.
377 @param ApicId The local APIC ID of the target processor.
378 @param Vector The vector number of the interrupt being sent.
387 LOCAL_APIC_ICR_LOW IcrLow
;
390 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_FIXED
;
391 IcrLow
.Bits
.Level
= 1;
392 IcrLow
.Bits
.Vector
= Vector
;
393 SendIpi (IcrLow
.Uint32
, ApicId
);
397 Send a Fixed IPI to all processors excluding self.
399 This function returns after the IPI has been accepted by the target processors.
401 @param Vector The vector number of the interrupt being sent.
405 SendFixedIpiAllExcludingSelf (
409 LOCAL_APIC_ICR_LOW IcrLow
;
412 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_FIXED
;
413 IcrLow
.Bits
.Level
= 1;
414 IcrLow
.Bits
.DestinationShorthand
= LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF
;
415 IcrLow
.Bits
.Vector
= Vector
;
416 SendIpi (IcrLow
.Uint32
, 0);
420 Send a SMI IPI to a specified target processor.
422 This function returns after the IPI has been accepted by the target processor.
424 @param ApicId Specify the local APIC ID of the target processor.
432 LOCAL_APIC_ICR_LOW IcrLow
;
435 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_SMI
;
436 IcrLow
.Bits
.Level
= 1;
437 SendIpi (IcrLow
.Uint32
, ApicId
);
441 Send a SMI IPI to all processors excluding self.
443 This function returns after the IPI has been accepted by the target processors.
447 SendSmiIpiAllExcludingSelf (
451 LOCAL_APIC_ICR_LOW IcrLow
;
454 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_SMI
;
455 IcrLow
.Bits
.Level
= 1;
456 IcrLow
.Bits
.DestinationShorthand
= LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF
;
457 SendIpi (IcrLow
.Uint32
, 0);
461 Send an INIT IPI to a specified target processor.
463 This function returns after the IPI has been accepted by the target processor.
465 @param ApicId Specify the local APIC ID of the target processor.
473 LOCAL_APIC_ICR_LOW IcrLow
;
476 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_INIT
;
477 IcrLow
.Bits
.Level
= 1;
478 SendIpi (IcrLow
.Uint32
, ApicId
);
482 Send an INIT IPI to all processors excluding self.
484 This function returns after the IPI has been accepted by the target processors.
488 SendInitIpiAllExcludingSelf (
492 LOCAL_APIC_ICR_LOW IcrLow
;
495 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_INIT
;
496 IcrLow
.Bits
.Level
= 1;
497 IcrLow
.Bits
.DestinationShorthand
= LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF
;
498 SendIpi (IcrLow
.Uint32
, 0);
502 Send an INIT-Start-up-Start-up IPI sequence to a specified target processor.
504 This function returns after the IPI has been accepted by the target processor.
506 if StartupRoutine >= 1M, then ASSERT.
507 if StartupRoutine is not multiple of 4K, then ASSERT.
509 @param ApicId Specify the local APIC ID of the target processor.
510 @param StartupRoutine Points to a start-up routine which is below 1M physical
511 address and 4K aligned.
517 IN UINT32 StartupRoutine
520 LOCAL_APIC_ICR_LOW IcrLow
;
522 ASSERT (StartupRoutine
< 0x100000);
523 ASSERT ((StartupRoutine
& 0xfff) == 0);
525 SendInitIpi (ApicId
);
526 MicroSecondDelay (PcdGet32 (PcdCpuInitIpiDelayInMicroSeconds
));
528 IcrLow
.Bits
.Vector
= (StartupRoutine
>> 12);
529 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_STARTUP
;
530 IcrLow
.Bits
.Level
= 1;
531 SendIpi (IcrLow
.Uint32
, ApicId
);
532 if (!StandardSignatureIsAuthenticAMD ()) {
533 MicroSecondDelay (200);
534 SendIpi (IcrLow
.Uint32
, ApicId
);
539 Send an INIT-Start-up-Start-up IPI sequence to all processors excluding self.
541 This function returns after the IPI has been accepted by the target processors.
543 if StartupRoutine >= 1M, then ASSERT.
544 if StartupRoutine is not multiple of 4K, then ASSERT.
546 @param StartupRoutine Points to a start-up routine which is below 1M physical
547 address and 4K aligned.
551 SendInitSipiSipiAllExcludingSelf (
552 IN UINT32 StartupRoutine
555 LOCAL_APIC_ICR_LOW IcrLow
;
557 ASSERT (StartupRoutine
< 0x100000);
558 ASSERT ((StartupRoutine
& 0xfff) == 0);
560 SendInitIpiAllExcludingSelf ();
561 MicroSecondDelay (PcdGet32 (PcdCpuInitIpiDelayInMicroSeconds
));
563 IcrLow
.Bits
.Vector
= (StartupRoutine
>> 12);
564 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_STARTUP
;
565 IcrLow
.Bits
.Level
= 1;
566 IcrLow
.Bits
.DestinationShorthand
= LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF
;
567 SendIpi (IcrLow
.Uint32
, 0);
568 if (!StandardSignatureIsAuthenticAMD ()) {
569 MicroSecondDelay (200);
570 SendIpi (IcrLow
.Uint32
, 0);
575 Initialize the state of the SoftwareEnable bit in the Local APIC
576 Spurious Interrupt Vector register.
578 @param Enable If TRUE, then set SoftwareEnable to 1
579 If FALSE, then set SoftwareEnable to 0.
584 InitializeLocalApicSoftwareEnable (
591 // Set local APIC software-enabled bit.
593 Svr
.Uint32
= ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
);
595 if (Svr
.Bits
.SoftwareEnable
== 0) {
596 Svr
.Bits
.SoftwareEnable
= 1;
597 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
, Svr
.Uint32
);
600 if (Svr
.Bits
.SoftwareEnable
== 1) {
601 Svr
.Bits
.SoftwareEnable
= 0;
602 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
, Svr
.Uint32
);
608 Programming Virtual Wire Mode.
610 This function programs the local APIC for virtual wire mode following
611 the example described in chapter A.3 of the MP 1.4 spec.
613 IOxAPIC is not involved in this type of virtual wire mode.
617 ProgramVirtualWireMode (
622 LOCAL_APIC_LVT_LINT Lint
;
625 // Enable the APIC via SVR and set the spurious interrupt to use Int 00F.
627 Svr
.Uint32
= ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
);
628 Svr
.Bits
.SpuriousVector
= 0xf;
629 Svr
.Bits
.SoftwareEnable
= 1;
630 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
, Svr
.Uint32
);
633 // Program the LINT0 vector entry as ExtInt. Not masked, edge, active high.
635 Lint
.Uint32
= ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET
);
636 Lint
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_EXTINT
;
637 Lint
.Bits
.InputPinPolarity
= 0;
638 Lint
.Bits
.TriggerMode
= 0;
640 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET
, Lint
.Uint32
);
643 // Program the LINT0 vector entry as NMI. Not masked, edge, active high.
645 Lint
.Uint32
= ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET
);
646 Lint
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_NMI
;
647 Lint
.Bits
.InputPinPolarity
= 0;
648 Lint
.Bits
.TriggerMode
= 0;
650 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET
, Lint
.Uint32
);
654 Disable LINT0 & LINT1 interrupts.
656 This function sets the mask flag in the LVT LINT0 & LINT1 registers.
660 DisableLvtInterrupts (
664 LOCAL_APIC_LVT_LINT LvtLint
;
666 LvtLint
.Uint32
= ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET
);
667 LvtLint
.Bits
.Mask
= 1;
668 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET
, LvtLint
.Uint32
);
670 LvtLint
.Uint32
= ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET
);
671 LvtLint
.Bits
.Mask
= 1;
672 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET
, LvtLint
.Uint32
);
676 Read the initial count value from the init-count register.
678 @return The initial count value read from the init-count register.
682 GetApicTimerInitCount (
686 return ReadLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET
);
690 Read the current count value from the current-count register.
692 @return The current count value read from the current-count register.
696 GetApicTimerCurrentCount (
700 return ReadLocalApicReg (XAPIC_TIMER_CURRENT_COUNT_OFFSET
);
704 Initialize the local APIC timer.
706 The local APIC timer is initialized and enabled.
708 @param DivideValue The divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.
709 If it is 0, then use the current divide value in the DCR.
710 @param InitCount The initial count value.
711 @param PeriodicMode If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.
712 @param Vector The timer interrupt vector number.
716 InitializeApicTimer (
717 IN UINTN DivideValue
,
719 IN BOOLEAN PeriodicMode
,
724 LOCAL_APIC_LVT_TIMER LvtTimer
;
728 // Ensure local APIC is in software-enabled state.
730 InitializeLocalApicSoftwareEnable (TRUE
);
733 // Program init-count register.
735 WriteLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET
, InitCount
);
737 if (DivideValue
!= 0) {
738 ASSERT (DivideValue
<= 128);
739 ASSERT (DivideValue
== GetPowerOfTwo32 ((UINT32
)DivideValue
));
740 Divisor
= (UINT32
)((HighBitSet32 ((UINT32
)DivideValue
) - 1) & 0x7);
742 Dcr
.Uint32
= ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET
);
743 Dcr
.Bits
.DivideValue1
= (Divisor
& 0x3);
744 Dcr
.Bits
.DivideValue2
= (Divisor
>> 2);
745 WriteLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET
, Dcr
.Uint32
);
749 // Enable APIC timer interrupt with specified timer mode.
751 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
753 LvtTimer
.Bits
.TimerMode
= 1;
755 LvtTimer
.Bits
.TimerMode
= 0;
758 LvtTimer
.Bits
.Mask
= 0;
759 LvtTimer
.Bits
.Vector
= Vector
;
760 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET
, LvtTimer
.Uint32
);
764 Get the state of the local APIC timer.
766 This function will ASSERT if the local APIC is not software enabled.
768 @param DivideValue Return the divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.
769 @param PeriodicMode Return the timer mode. If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.
770 @param Vector Return the timer interrupt vector number.
775 OUT UINTN
*DivideValue OPTIONAL
,
776 OUT BOOLEAN
*PeriodicMode OPTIONAL
,
777 OUT UINT8
*Vector OPTIONAL
782 LOCAL_APIC_LVT_TIMER LvtTimer
;
785 // Check the APIC Software Enable/Disable bit (bit 8) in Spurious-Interrupt
787 // This bit will be 1, if local APIC is software enabled.
789 ASSERT ((ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
) & BIT8
) != 0);
791 if (DivideValue
!= NULL
) {
792 Dcr
.Uint32
= ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET
);
793 Divisor
= Dcr
.Bits
.DivideValue1
| (Dcr
.Bits
.DivideValue2
<< 2);
794 Divisor
= (Divisor
+ 1) & 0x7;
795 *DivideValue
= ((UINTN
)1) << Divisor
;
798 if ((PeriodicMode
!= NULL
) || (Vector
!= NULL
)) {
799 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
800 if (PeriodicMode
!= NULL
) {
801 if (LvtTimer
.Bits
.TimerMode
== 1) {
802 *PeriodicMode
= TRUE
;
804 *PeriodicMode
= FALSE
;
808 if (Vector
!= NULL
) {
809 *Vector
= (UINT8
)LvtTimer
.Bits
.Vector
;
815 Enable the local APIC timer interrupt.
819 EnableApicTimerInterrupt (
823 LOCAL_APIC_LVT_TIMER LvtTimer
;
825 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
826 LvtTimer
.Bits
.Mask
= 0;
827 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET
, LvtTimer
.Uint32
);
831 Disable the local APIC timer interrupt.
835 DisableApicTimerInterrupt (
839 LOCAL_APIC_LVT_TIMER LvtTimer
;
841 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
842 LvtTimer
.Bits
.Mask
= 1;
843 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET
, LvtTimer
.Uint32
);
847 Get the local APIC timer interrupt state.
849 @retval TRUE The local APIC timer interrupt is enabled.
850 @retval FALSE The local APIC timer interrupt is disabled.
854 GetApicTimerInterruptState (
858 LOCAL_APIC_LVT_TIMER LvtTimer
;
860 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
861 return (BOOLEAN
)(LvtTimer
.Bits
.Mask
== 0);
865 Send EOI to the local APIC.
873 WriteLocalApicReg (XAPIC_EOI_OFFSET
, 0);
877 Get the 32-bit address that a device should use to send a Message Signaled
878 Interrupt (MSI) to the Local APIC of the currently executing processor.
880 @return 32-bit address used to send an MSI to the Local APIC.
888 LOCAL_APIC_MSI_ADDRESS MsiAddress
;
891 // Return address for an MSI interrupt to be delivered only to the APIC ID
892 // of the currently executing processor.
894 MsiAddress
.Uint32
= 0;
895 MsiAddress
.Bits
.BaseAddress
= 0xFEE;
896 MsiAddress
.Bits
.DestinationId
= GetApicId ();
897 return MsiAddress
.Uint32
;
901 Get the 64-bit data value that a device should use to send a Message Signaled
902 Interrupt (MSI) to the Local APIC of the currently executing processor.
904 If Vector is not in range 0x10..0xFE, then ASSERT().
905 If DeliveryMode is not supported, then ASSERT().
907 @param Vector The 8-bit interrupt vector associated with the MSI.
908 Must be in the range 0x10..0xFE
909 @param DeliveryMode A 3-bit value that specifies how the recept of the MSI
910 is handled. The only supported values are:
911 0: LOCAL_APIC_DELIVERY_MODE_FIXED
912 1: LOCAL_APIC_DELIVERY_MODE_LOWEST_PRIORITY
913 2: LOCAL_APIC_DELIVERY_MODE_SMI
914 4: LOCAL_APIC_DELIVERY_MODE_NMI
915 5: LOCAL_APIC_DELIVERY_MODE_INIT
916 7: LOCAL_APIC_DELIVERY_MODE_EXTINT
918 @param LevelTriggered TRUE specifies a level triggered interrupt.
919 FALSE specifies an edge triggered interrupt.
920 @param AssertionLevel Ignored if LevelTriggered is FALSE.
921 TRUE specifies a level triggered interrupt that active
922 when the interrupt line is asserted.
923 FALSE specifies a level triggered interrupt that active
924 when the interrupt line is deasserted.
926 @return 64-bit data value used to send an MSI to the Local APIC.
932 IN UINTN DeliveryMode
,
933 IN BOOLEAN LevelTriggered
,
934 IN BOOLEAN AssertionLevel
937 LOCAL_APIC_MSI_DATA MsiData
;
939 ASSERT (Vector
>= 0x10 && Vector
<= 0xFE);
940 ASSERT (DeliveryMode
< 8 && DeliveryMode
!= 6 && DeliveryMode
!= 3);
943 MsiData
.Bits
.Vector
= Vector
;
944 MsiData
.Bits
.DeliveryMode
= (UINT32
)DeliveryMode
;
945 if (LevelTriggered
) {
946 MsiData
.Bits
.TriggerMode
= 1;
947 if (AssertionLevel
) {
948 MsiData
.Bits
.Level
= 1;
952 return MsiData
.Uint64
;
956 Get Package ID/Core ID/Thread ID of a processor.
958 The algorithm assumes the target system has symmetry across physical
959 package boundaries with respect to the number of logical processors
960 per package, number of cores per package.
962 @param[in] InitialApicId Initial APIC ID of the target logical processor.
963 @param[out] Package Returns the processor package ID.
964 @param[out] Core Returns the processor core ID.
965 @param[out] Thread Returns the processor thread ID.
969 GetProcessorLocationByApicId (
970 IN UINT32 InitialApicId
,
971 OUT UINT32
*Package OPTIONAL
,
972 OUT UINT32
*Core OPTIONAL
,
973 OUT UINT32
*Thread OPTIONAL
976 BOOLEAN TopologyLeafSupported
;
977 CPUID_VERSION_INFO_EBX VersionInfoEbx
;
978 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
979 CPUID_CACHE_PARAMS_EAX CacheParamsEax
;
980 CPUID_EXTENDED_TOPOLOGY_EAX ExtendedTopologyEax
;
981 CPUID_EXTENDED_TOPOLOGY_EBX ExtendedTopologyEbx
;
982 CPUID_EXTENDED_TOPOLOGY_ECX ExtendedTopologyEcx
;
983 CPUID_AMD_EXTENDED_CPU_SIG_ECX AmdExtendedCpuSigEcx
;
984 CPUID_AMD_PROCESSOR_TOPOLOGY_EBX AmdProcessorTopologyEbx
;
985 CPUID_AMD_VIR_PHY_ADDRESS_SIZE_ECX AmdVirPhyAddressSizeEcx
;
986 UINT32 MaxStandardCpuIdIndex
;
987 UINT32 MaxExtendedCpuIdIndex
;
990 UINT32 MaxLogicProcessorsPerPackage
;
991 UINT32 MaxCoresPerPackage
;
996 // Check if the processor is capable of supporting more than one logical processor.
998 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
999 if (VersionInfoEdx
.Bits
.HTT
== 0) {
1000 if (Thread
!= NULL
) {
1008 if (Package
!= NULL
) {
1016 // Assume three-level mapping of APIC ID: Package|Core|Thread.
1022 // Get max index of CPUID
1024 AsmCpuid (CPUID_SIGNATURE
, &MaxStandardCpuIdIndex
, NULL
, NULL
, NULL
);
1025 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &MaxExtendedCpuIdIndex
, NULL
, NULL
, NULL
);
1028 // If the extended topology enumeration leaf is available, it
1029 // is the preferred mechanism for enumerating topology.
1031 TopologyLeafSupported
= FALSE
;
1032 if (MaxStandardCpuIdIndex
>= CPUID_EXTENDED_TOPOLOGY
) {
1034 CPUID_EXTENDED_TOPOLOGY
,
1036 &ExtendedTopologyEax
.Uint32
,
1037 &ExtendedTopologyEbx
.Uint32
,
1038 &ExtendedTopologyEcx
.Uint32
,
1042 // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for
1043 // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not
1044 // supported on that processor.
1046 if (ExtendedTopologyEbx
.Uint32
!= 0) {
1047 TopologyLeafSupported
= TRUE
;
1050 // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract
1051 // the SMT sub-field of x2APIC ID.
1053 LevelType
= ExtendedTopologyEcx
.Bits
.LevelType
;
1054 ASSERT (LevelType
== CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT
);
1055 ThreadBits
= ExtendedTopologyEax
.Bits
.ApicIdShift
;
1058 // Software must not assume any "level type" encoding
1059 // value to be related to any sub-leaf index, except sub-leaf 0.
1064 CPUID_EXTENDED_TOPOLOGY
,
1066 &ExtendedTopologyEax
.Uint32
,
1068 &ExtendedTopologyEcx
.Uint32
,
1071 LevelType
= ExtendedTopologyEcx
.Bits
.LevelType
;
1072 if (LevelType
== CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE
) {
1073 CoreBits
= ExtendedTopologyEax
.Bits
.ApicIdShift
- ThreadBits
;
1078 } while (LevelType
!= CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID
);
1082 if (!TopologyLeafSupported
) {
1084 // Get logical processor count
1086 AsmCpuid (CPUID_VERSION_INFO
, NULL
, &VersionInfoEbx
.Uint32
, NULL
, NULL
);
1087 MaxLogicProcessorsPerPackage
= VersionInfoEbx
.Bits
.MaximumAddressableIdsForLogicalProcessors
;
1090 // Assume single-core processor
1092 MaxCoresPerPackage
= 1;
1095 // Check for topology extensions on AMD processor
1097 if (StandardSignatureIsAuthenticAMD ()) {
1098 if (MaxExtendedCpuIdIndex
>= CPUID_AMD_PROCESSOR_TOPOLOGY
) {
1099 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, &AmdExtendedCpuSigEcx
.Uint32
, NULL
);
1100 if (AmdExtendedCpuSigEcx
.Bits
.TopologyExtensions
!= 0) {
1102 // Account for max possible thread count to decode ApicId
1104 AsmCpuid (CPUID_VIR_PHY_ADDRESS_SIZE
, NULL
, NULL
, &AmdVirPhyAddressSizeEcx
.Uint32
, NULL
);
1105 MaxLogicProcessorsPerPackage
= 1 << AmdVirPhyAddressSizeEcx
.Bits
.ApicIdCoreIdSize
;
1108 // Get cores per processor package
1110 AsmCpuid (CPUID_AMD_PROCESSOR_TOPOLOGY
, NULL
, &AmdProcessorTopologyEbx
.Uint32
, NULL
, NULL
);
1111 MaxCoresPerPackage
= MaxLogicProcessorsPerPackage
/ (AmdProcessorTopologyEbx
.Bits
.ThreadsPerCore
+ 1);
1116 // Extract core count based on CACHE information
1118 if (MaxStandardCpuIdIndex
>= CPUID_CACHE_PARAMS
) {
1119 AsmCpuidEx (CPUID_CACHE_PARAMS
, 0, &CacheParamsEax
.Uint32
, NULL
, NULL
, NULL
);
1120 if (CacheParamsEax
.Uint32
!= 0) {
1121 MaxCoresPerPackage
= CacheParamsEax
.Bits
.MaximumAddressableIdsForLogicalProcessors
+ 1;
1126 ThreadBits
= (UINTN
)(HighBitSet32 (MaxLogicProcessorsPerPackage
/ MaxCoresPerPackage
- 1) + 1);
1127 CoreBits
= (UINTN
)(HighBitSet32 (MaxCoresPerPackage
- 1) + 1);
1130 if (Thread
!= NULL
) {
1131 *Thread
= InitialApicId
& ((1 << ThreadBits
) - 1);
1135 *Core
= (InitialApicId
>> ThreadBits
) & ((1 << CoreBits
) - 1);
1138 if (Package
!= NULL
) {
1139 *Package
= (InitialApicId
>> (ThreadBits
+ CoreBits
));
1144 Get Package ID/Die ID/Tile ID/Module ID/Core ID/Thread ID of a processor.
1146 The algorithm assumes the target system has symmetry across physical
1147 package boundaries with respect to the number of threads per core, number of
1148 cores per module, number of modules per tile, number of tiles per die, number
1149 of dies per package.
1151 @param[in] InitialApicId Initial APIC ID of the target logical processor.
1152 @param[out] Package Returns the processor package ID.
1153 @param[out] Die Returns the processor die ID.
1154 @param[out] Tile Returns the processor tile ID.
1155 @param[out] Module Returns the processor module ID.
1156 @param[out] Core Returns the processor core ID.
1157 @param[out] Thread Returns the processor thread ID.
1161 GetProcessorLocation2ByApicId (
1162 IN UINT32 InitialApicId
,
1163 OUT UINT32
*Package OPTIONAL
,
1164 OUT UINT32
*Die OPTIONAL
,
1165 OUT UINT32
*Tile OPTIONAL
,
1166 OUT UINT32
*Module OPTIONAL
,
1167 OUT UINT32
*Core OPTIONAL
,
1168 OUT UINT32
*Thread OPTIONAL
1171 CPUID_EXTENDED_TOPOLOGY_EAX ExtendedTopologyEax
;
1172 CPUID_EXTENDED_TOPOLOGY_ECX ExtendedTopologyEcx
;
1173 UINT32 MaxStandardCpuIdIndex
;
1176 UINT32 Bits
[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_DIE
+ 2];
1177 UINT32
*Location
[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_DIE
+ 2];
1179 for (LevelType
= 0; LevelType
< ARRAY_SIZE (Bits
); LevelType
++) {
1180 Bits
[LevelType
] = 0;
1184 // Get max index of CPUID
1186 AsmCpuid (CPUID_SIGNATURE
, &MaxStandardCpuIdIndex
, NULL
, NULL
, NULL
);
1187 if (MaxStandardCpuIdIndex
< CPUID_V2_EXTENDED_TOPOLOGY
) {
1196 if (Module
!= NULL
) {
1200 GetProcessorLocationByApicId (InitialApicId
, Package
, Core
, Thread
);
1205 // If the V2 extended topology enumeration leaf is available, it
1206 // is the preferred mechanism for enumerating topology.
1208 for (Index
= 0; ; Index
++) {
1210 CPUID_V2_EXTENDED_TOPOLOGY
,
1212 &ExtendedTopologyEax
.Uint32
,
1214 &ExtendedTopologyEcx
.Uint32
,
1218 LevelType
= ExtendedTopologyEcx
.Bits
.LevelType
;
1221 // first level reported should be SMT.
1223 ASSERT ((Index
!= 0) || (LevelType
== CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT
));
1224 if (LevelType
== CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID
) {
1228 ASSERT (LevelType
< ARRAY_SIZE (Bits
));
1229 Bits
[LevelType
] = ExtendedTopologyEax
.Bits
.ApicIdShift
;
1232 for (LevelType
= CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE
; LevelType
< ARRAY_SIZE (Bits
); LevelType
++) {
1234 // If there are more levels between level-1 (low-level) and level-2 (high-level), the unknown levels will be ignored
1235 // and treated as an extension of the last known level (i.e., level-1 in this case).
1237 if (Bits
[LevelType
] == 0) {
1238 Bits
[LevelType
] = Bits
[LevelType
- 1];
1242 Location
[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_DIE
+ 1] = Package
;
1243 Location
[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_DIE
] = Die
;
1244 Location
[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_TILE
] = Tile
;
1245 Location
[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_MODULE
] = Module
;
1246 Location
[CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE
] = Core
;
1247 Location
[CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT
] = Thread
;
1249 Bits
[CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_DIE
+ 1] = 32;
1251 for ( LevelType
= CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT
1252 ; LevelType
<= CPUID_V2_EXTENDED_TOPOLOGY_LEVEL_TYPE_DIE
+ 1
1256 if (Location
[LevelType
] != NULL
) {
1258 // Bits[i] holds the number of bits to shift right on x2APIC ID to get a unique
1259 // topology ID of the next level type.
1261 *Location
[LevelType
] = InitialApicId
>> Bits
[LevelType
- 1];
1264 // Bits[i] - Bits[i-1] holds the number of bits for the next ONE level type.
1266 *Location
[LevelType
] &= (1 << (Bits
[LevelType
] - Bits
[LevelType
- 1])) - 1;