4 This local APIC library instance supports x2APIC capable processors
5 which have xAPIC and x2APIC modes.
7 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Register/LocalApic.h>
20 #include <Library/BaseLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/LocalApicLib.h>
23 #include <Library/IoLib.h>
24 #include <Library/TimerLib.h>
27 // Library internal functions
31 Retrieve the base address of local APIC.
33 @return The base address of local APIC.
38 GetLocalApicBaseAddress (
42 MSR_IA32_APIC_BASE ApicBaseMsr
;
44 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS
);
46 return (UINTN
)(LShiftU64 ((UINT64
) ApicBaseMsr
.Bits
.ApicBaseHigh
, 32)) +
47 (((UINTN
)ApicBaseMsr
.Bits
.ApicBaseLow
) << 12);
51 Set the base address of local APIC.
53 If BaseAddress is not aligned on a 4KB boundary, then ASSERT().
55 @param[in] BaseAddress Local APIC base address to be set.
60 SetLocalApicBaseAddress (
64 MSR_IA32_APIC_BASE ApicBaseMsr
;
66 ASSERT ((BaseAddress
& (SIZE_4KB
- 1)) == 0);
68 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS
);
70 ApicBaseMsr
.Bits
.ApicBaseLow
= (UINT32
) (BaseAddress
>> 12);
71 ApicBaseMsr
.Bits
.ApicBaseHigh
= (UINT32
) (RShiftU64((UINT64
) BaseAddress
, 32));
73 AsmWriteMsr64 (MSR_IA32_APIC_BASE_ADDRESS
, ApicBaseMsr
.Uint64
);
77 Read from a local APIC register.
79 This function reads from a local APIC register either in xAPIC or x2APIC mode.
80 It is required that in xAPIC mode wider registers (64-bit or 256-bit) must be
81 accessed using multiple 32-bit loads or stores, so this function only performs
84 @param MmioOffset The MMIO offset of the local APIC register in xAPIC mode.
85 It must be 16-byte aligned.
87 @return 32-bit Value read from the register.
97 ASSERT ((MmioOffset
& 0xf) == 0);
99 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC
) {
100 return MmioRead32 (GetLocalApicBaseAddress() + MmioOffset
);
103 // DFR is not supported in x2APIC mode.
105 ASSERT (MmioOffset
!= XAPIC_ICR_DFR_OFFSET
);
107 // Note that in x2APIC mode, ICR is a 64-bit MSR that needs special treatment. It
108 // is not supported in this function for simplicity.
110 ASSERT (MmioOffset
!= XAPIC_ICR_HIGH_OFFSET
);
112 MsrIndex
= (UINT32
)(MmioOffset
>> 4) + X2APIC_MSR_BASE_ADDRESS
;
113 return AsmReadMsr32 (MsrIndex
);
118 Write to a local APIC register.
120 This function writes to a local APIC register either in xAPIC or x2APIC mode.
121 It is required that in xAPIC mode wider registers (64-bit or 256-bit) must be
122 accessed using multiple 32-bit loads or stores, so this function only performs
125 if the register index is invalid or unsupported in current APIC mode, then ASSERT.
127 @param MmioOffset The MMIO offset of the local APIC register in xAPIC mode.
128 It must be 16-byte aligned.
129 @param Value Value to be written to the register.
140 ASSERT ((MmioOffset
& 0xf) == 0);
142 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC
) {
143 MmioWrite32 (GetLocalApicBaseAddress() + MmioOffset
, Value
);
146 // DFR is not supported in x2APIC mode.
148 ASSERT (MmioOffset
!= XAPIC_ICR_DFR_OFFSET
);
150 // Note that in x2APIC mode, ICR is a 64-bit MSR that needs special treatment. It
151 // is not supported in this function for simplicity.
153 ASSERT (MmioOffset
!= XAPIC_ICR_HIGH_OFFSET
);
154 ASSERT (MmioOffset
!= XAPIC_ICR_LOW_OFFSET
);
156 MsrIndex
= (UINT32
)(MmioOffset
>> 4) + X2APIC_MSR_BASE_ADDRESS
;
158 // The serializing semantics of WRMSR are relaxed when writing to the APIC registers.
159 // Use memory fence here to force the serializing semantics to be consisent with xAPIC mode.
162 AsmWriteMsr32 (MsrIndex
, Value
);
167 Send an IPI by writing to ICR.
169 This function returns after the IPI has been accepted by the target processor.
171 @param IcrLow 32-bit value to be written to the low half of ICR.
172 @param ApicId APIC ID of the target processor if this IPI is targeted for a specific processor.
181 LOCAL_APIC_ICR_LOW IcrLowReg
;
182 UINTN LocalApciBaseAddress
;
184 BOOLEAN InterruptState
;
187 // Legacy APIC or X2APIC?
189 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC
) {
190 ASSERT (ApicId
<= 0xff);
192 InterruptState
= SaveAndDisableInterrupts ();
195 // Get base address of this LAPIC
197 LocalApciBaseAddress
= GetLocalApicBaseAddress();
200 // Save existing contents of ICR high 32 bits
202 IcrHigh
= MmioRead32 (LocalApciBaseAddress
+ XAPIC_ICR_HIGH_OFFSET
);
205 // Wait for DeliveryStatus clear in case a previous IPI
206 // is still being sent
209 IcrLowReg
.Uint32
= MmioRead32 (LocalApciBaseAddress
+ XAPIC_ICR_LOW_OFFSET
);
210 } while (IcrLowReg
.Bits
.DeliveryStatus
!= 0);
213 // For xAPIC, the act of writing to the low doubleword of the ICR causes the IPI to be sent.
215 MmioWrite32 (LocalApciBaseAddress
+ XAPIC_ICR_HIGH_OFFSET
, ApicId
<< 24);
216 MmioWrite32 (LocalApciBaseAddress
+ XAPIC_ICR_LOW_OFFSET
, IcrLow
);
219 // Wait for DeliveryStatus clear again
222 IcrLowReg
.Uint32
= MmioRead32 (LocalApciBaseAddress
+ XAPIC_ICR_LOW_OFFSET
);
223 } while (IcrLowReg
.Bits
.DeliveryStatus
!= 0);
226 // And restore old contents of ICR high
228 MmioWrite32 (LocalApciBaseAddress
+ XAPIC_ICR_HIGH_OFFSET
, IcrHigh
);
230 SetInterruptState (InterruptState
);
234 // For x2APIC, A single MSR write to the Interrupt Command Register is required for dispatching an
235 // interrupt in x2APIC mode.
237 MsrValue
= LShiftU64 ((UINT64
) ApicId
, 32) | IcrLow
;
238 AsmWriteMsr64 (X2APIC_MSR_ICR_ADDRESS
, MsrValue
);
243 // Library API implementation functions
247 Get the current local APIC mode.
249 If local APIC is disabled, then ASSERT.
251 @retval LOCAL_APIC_MODE_XAPIC current APIC mode is xAPIC.
252 @retval LOCAL_APIC_MODE_X2APIC current APIC mode is x2APIC.
260 MSR_IA32_APIC_BASE ApicBaseMsr
;
262 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS
);
264 // Local APIC should have been enabled
266 ASSERT (ApicBaseMsr
.Bits
.En
!= 0);
267 if (ApicBaseMsr
.Bits
.Extd
!= 0) {
268 return LOCAL_APIC_MODE_X2APIC
;
270 return LOCAL_APIC_MODE_XAPIC
;
275 Set the current local APIC mode.
277 If the specified local APIC mode is not valid, then ASSERT.
278 If the specified local APIC mode can't be set as current, then ASSERT.
280 @param ApicMode APIC mode to be set.
282 @note This API must not be called from an interrupt handler or SMI handler.
283 It may result in unpredictable behavior.
292 MSR_IA32_APIC_BASE ApicBaseMsr
;
294 CurrentMode
= GetApicMode ();
295 if (CurrentMode
== LOCAL_APIC_MODE_XAPIC
) {
297 case LOCAL_APIC_MODE_XAPIC
:
299 case LOCAL_APIC_MODE_X2APIC
:
300 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS
);
301 ApicBaseMsr
.Bits
.Extd
= 1;
302 AsmWriteMsr64 (MSR_IA32_APIC_BASE_ADDRESS
, ApicBaseMsr
.Uint64
);
309 case LOCAL_APIC_MODE_XAPIC
:
311 // Transition from x2APIC mode to xAPIC mode is a two-step process:
312 // x2APIC -> Local APIC disabled -> xAPIC
314 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS
);
315 ApicBaseMsr
.Bits
.Extd
= 0;
316 ApicBaseMsr
.Bits
.En
= 0;
317 AsmWriteMsr64 (MSR_IA32_APIC_BASE_ADDRESS
, ApicBaseMsr
.Uint64
);
318 ApicBaseMsr
.Bits
.En
= 1;
319 AsmWriteMsr64 (MSR_IA32_APIC_BASE_ADDRESS
, ApicBaseMsr
.Uint64
);
321 case LOCAL_APIC_MODE_X2APIC
:
330 Get the initial local APIC ID of the executing processor assigned by hardware upon power on or reset.
332 In xAPIC mode, the initial local APIC ID may be different from current APIC ID.
333 In x2APIC mode, the local APIC ID can't be changed and there is no concept of initial APIC ID. In this case,
334 the 32-bit local APIC ID is returned as initial APIC ID.
336 @return 32-bit initial local APIC ID of the executing processor.
345 UINT32 MaxCpuIdIndex
;
348 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC
) {
350 // Get the max index of basic CPUID
352 AsmCpuid (CPUID_SIGNATURE
, &MaxCpuIdIndex
, NULL
, NULL
, NULL
);
354 // If CPUID Leaf B is supported,
355 // Then the initial 32-bit APIC ID = CPUID.0BH:EDX
356 // Else the initial 8-bit APIC ID = CPUID.1:EBX[31:24]
358 if (MaxCpuIdIndex
>= CPUID_EXTENDED_TOPOLOGY
) {
359 AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY
, 0, NULL
, NULL
, NULL
, &ApicId
);
362 AsmCpuid (CPUID_VERSION_INFO
, NULL
, &RegEbx
, NULL
, NULL
);
370 Get the local APIC ID of the executing processor.
372 @return 32-bit local APIC ID of the executing processor.
383 ApicId
= ReadLocalApicReg (XAPIC_ID_OFFSET
);
384 if (GetApicMode () == LOCAL_APIC_MODE_XAPIC
) {
385 ApicId
= ((InitApicId
= GetInitialApicId ()) < 0x100) ? (ApicId
>> 24) : InitApicId
;
392 Get the value of the local APIC version register.
394 @return the value of the local APIC version register.
402 return ReadLocalApicReg (XAPIC_VERSION_OFFSET
);
406 Send a Fixed IPI to a specified target processor.
408 This function returns after the IPI has been accepted by the target processor.
410 @param ApicId The local APIC ID of the target processor.
411 @param Vector The vector number of the interrupt being sent.
420 LOCAL_APIC_ICR_LOW IcrLow
;
423 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_FIXED
;
424 IcrLow
.Bits
.Level
= 1;
425 IcrLow
.Bits
.Vector
= Vector
;
426 SendIpi (IcrLow
.Uint32
, ApicId
);
430 Send a Fixed IPI to all processors excluding self.
432 This function returns after the IPI has been accepted by the target processors.
434 @param Vector The vector number of the interrupt being sent.
438 SendFixedIpiAllExcludingSelf (
442 LOCAL_APIC_ICR_LOW IcrLow
;
445 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_FIXED
;
446 IcrLow
.Bits
.Level
= 1;
447 IcrLow
.Bits
.DestinationShorthand
= LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF
;
448 IcrLow
.Bits
.Vector
= Vector
;
449 SendIpi (IcrLow
.Uint32
, 0);
453 Send a SMI IPI to a specified target processor.
455 This function returns after the IPI has been accepted by the target processor.
457 @param ApicId Specify the local APIC ID of the target processor.
465 LOCAL_APIC_ICR_LOW IcrLow
;
468 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_SMI
;
469 IcrLow
.Bits
.Level
= 1;
470 SendIpi (IcrLow
.Uint32
, ApicId
);
474 Send a SMI IPI to all processors excluding self.
476 This function returns after the IPI has been accepted by the target processors.
480 SendSmiIpiAllExcludingSelf (
484 LOCAL_APIC_ICR_LOW IcrLow
;
487 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_SMI
;
488 IcrLow
.Bits
.Level
= 1;
489 IcrLow
.Bits
.DestinationShorthand
= LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF
;
490 SendIpi (IcrLow
.Uint32
, 0);
494 Send an INIT IPI to a specified target processor.
496 This function returns after the IPI has been accepted by the target processor.
498 @param ApicId Specify the local APIC ID of the target processor.
506 LOCAL_APIC_ICR_LOW IcrLow
;
509 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_INIT
;
510 IcrLow
.Bits
.Level
= 1;
511 SendIpi (IcrLow
.Uint32
, ApicId
);
515 Send an INIT IPI to all processors excluding self.
517 This function returns after the IPI has been accepted by the target processors.
521 SendInitIpiAllExcludingSelf (
525 LOCAL_APIC_ICR_LOW IcrLow
;
528 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_INIT
;
529 IcrLow
.Bits
.Level
= 1;
530 IcrLow
.Bits
.DestinationShorthand
= LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF
;
531 SendIpi (IcrLow
.Uint32
, 0);
535 Send an INIT-Start-up-Start-up IPI sequence to a specified target processor.
537 This function returns after the IPI has been accepted by the target processor.
539 if StartupRoutine >= 1M, then ASSERT.
540 if StartupRoutine is not multiple of 4K, then ASSERT.
542 @param ApicId Specify the local APIC ID of the target processor.
543 @param StartupRoutine Points to a start-up routine which is below 1M physical
544 address and 4K aligned.
550 IN UINT32 StartupRoutine
553 LOCAL_APIC_ICR_LOW IcrLow
;
555 ASSERT (StartupRoutine
< 0x100000);
556 ASSERT ((StartupRoutine
& 0xfff) == 0);
558 SendInitIpi (ApicId
);
559 MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds
));
561 IcrLow
.Bits
.Vector
= (StartupRoutine
>> 12);
562 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_STARTUP
;
563 IcrLow
.Bits
.Level
= 1;
564 SendIpi (IcrLow
.Uint32
, ApicId
);
565 MicroSecondDelay (200);
566 SendIpi (IcrLow
.Uint32
, ApicId
);
570 Send an INIT-Start-up-Start-up IPI sequence to all processors excluding self.
572 This function returns after the IPI has been accepted by the target processors.
574 if StartupRoutine >= 1M, then ASSERT.
575 if StartupRoutine is not multiple of 4K, then ASSERT.
577 @param StartupRoutine Points to a start-up routine which is below 1M physical
578 address and 4K aligned.
582 SendInitSipiSipiAllExcludingSelf (
583 IN UINT32 StartupRoutine
586 LOCAL_APIC_ICR_LOW IcrLow
;
588 ASSERT (StartupRoutine
< 0x100000);
589 ASSERT ((StartupRoutine
& 0xfff) == 0);
591 SendInitIpiAllExcludingSelf ();
592 MicroSecondDelay (PcdGet32(PcdCpuInitIpiDelayInMicroSeconds
));
594 IcrLow
.Bits
.Vector
= (StartupRoutine
>> 12);
595 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_STARTUP
;
596 IcrLow
.Bits
.Level
= 1;
597 IcrLow
.Bits
.DestinationShorthand
= LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF
;
598 SendIpi (IcrLow
.Uint32
, 0);
599 MicroSecondDelay (200);
600 SendIpi (IcrLow
.Uint32
, 0);
604 Programming Virtual Wire Mode.
606 This function programs the local APIC for virtual wire mode following
607 the example described in chapter A.3 of the MP 1.4 spec.
609 IOxAPIC is not involved in this type of virtual wire mode.
613 ProgramVirtualWireMode (
618 LOCAL_APIC_LVT_LINT Lint
;
621 // Enable the APIC via SVR and set the spurious interrupt to use Int 00F.
623 Svr
.Uint32
= ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
);
624 Svr
.Bits
.SpuriousVector
= 0xf;
625 Svr
.Bits
.SoftwareEnable
= 1;
626 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
, Svr
.Uint32
);
629 // Program the LINT0 vector entry as ExtInt. Not masked, edge, active high.
631 Lint
.Uint32
= ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET
);
632 Lint
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_EXTINT
;
633 Lint
.Bits
.InputPinPolarity
= 0;
634 Lint
.Bits
.TriggerMode
= 0;
636 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET
, Lint
.Uint32
);
639 // Program the LINT0 vector entry as NMI. Not masked, edge, active high.
641 Lint
.Uint32
= ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET
);
642 Lint
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_NMI
;
643 Lint
.Bits
.InputPinPolarity
= 0;
644 Lint
.Bits
.TriggerMode
= 0;
646 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET
, Lint
.Uint32
);
650 Disable LINT0 & LINT1 interrupts.
652 This function sets the mask flag in the LVT LINT0 & LINT1 registers.
656 DisableLvtInterrupts (
660 LOCAL_APIC_LVT_LINT LvtLint
;
662 LvtLint
.Uint32
= ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET
);
663 LvtLint
.Bits
.Mask
= 1;
664 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET
, LvtLint
.Uint32
);
666 LvtLint
.Uint32
= ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET
);
667 LvtLint
.Bits
.Mask
= 1;
668 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET
, LvtLint
.Uint32
);
672 Read the initial count value from the init-count register.
674 @return The initial count value read from the init-count register.
678 GetApicTimerInitCount (
682 return ReadLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET
);
686 Read the current count value from the current-count register.
688 @return The current count value read from the current-count register.
692 GetApicTimerCurrentCount (
696 return ReadLocalApicReg (XAPIC_TIMER_CURRENT_COUNT_OFFSET
);
700 Initialize the local APIC timer.
702 The local APIC timer is initialized and enabled.
704 @param DivideValue The divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.
705 If it is 0, then use the current divide value in the DCR.
706 @param InitCount The initial count value.
707 @param PeriodicMode If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.
708 @param Vector The timer interrupt vector number.
712 InitializeApicTimer (
713 IN UINTN DivideValue
,
715 IN BOOLEAN PeriodicMode
,
721 LOCAL_APIC_LVT_TIMER LvtTimer
;
725 // Ensure local APIC is in software-enabled state.
727 Svr
.Uint32
= ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
);
728 Svr
.Bits
.SoftwareEnable
= 1;
729 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
, Svr
.Uint32
);
732 // Program init-count register.
734 WriteLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET
, InitCount
);
736 if (DivideValue
!= 0) {
737 ASSERT (DivideValue
<= 128);
738 ASSERT (DivideValue
== GetPowerOfTwo32((UINT32
)DivideValue
));
739 Divisor
= (UINT32
)((HighBitSet32 ((UINT32
)DivideValue
) - 1) & 0x7);
741 Dcr
.Uint32
= ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET
);
742 Dcr
.Bits
.DivideValue1
= (Divisor
& 0x3);
743 Dcr
.Bits
.DivideValue2
= (Divisor
>> 2);
744 WriteLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET
, Dcr
.Uint32
);
748 // Enable APIC timer interrupt with specified timer mode.
750 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
752 LvtTimer
.Bits
.TimerMode
= 1;
754 LvtTimer
.Bits
.TimerMode
= 0;
756 LvtTimer
.Bits
.Mask
= 0;
757 LvtTimer
.Bits
.Vector
= Vector
;
758 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET
, LvtTimer
.Uint32
);
762 Get the state of the local APIC timer.
764 @param DivideValue Return the divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.
765 @param PeriodicMode Return the timer mode. If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.
766 @param Vector Return the timer interrupt vector number.
771 OUT UINTN
*DivideValue OPTIONAL
,
772 OUT BOOLEAN
*PeriodicMode OPTIONAL
,
773 OUT UINT8
*Vector OPTIONAL
778 LOCAL_APIC_LVT_TIMER LvtTimer
;
780 if (DivideValue
!= NULL
) {
781 Dcr
.Uint32
= ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET
);
782 Divisor
= Dcr
.Bits
.DivideValue1
| (Dcr
.Bits
.DivideValue2
<< 2);
783 Divisor
= (Divisor
+ 1) & 0x7;
784 *DivideValue
= ((UINTN
)1) << Divisor
;
787 if (PeriodicMode
!= NULL
|| Vector
!= NULL
) {
788 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
789 if (PeriodicMode
!= NULL
) {
790 if (LvtTimer
.Bits
.TimerMode
== 1) {
791 *PeriodicMode
= TRUE
;
793 *PeriodicMode
= FALSE
;
796 if (Vector
!= NULL
) {
797 *Vector
= (UINT8
) LvtTimer
.Bits
.Vector
;
803 Enable the local APIC timer interrupt.
807 EnableApicTimerInterrupt (
811 LOCAL_APIC_LVT_TIMER LvtTimer
;
813 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
814 LvtTimer
.Bits
.Mask
= 0;
815 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET
, LvtTimer
.Uint32
);
819 Disable the local APIC timer interrupt.
823 DisableApicTimerInterrupt (
827 LOCAL_APIC_LVT_TIMER LvtTimer
;
829 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
830 LvtTimer
.Bits
.Mask
= 1;
831 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET
, LvtTimer
.Uint32
);
835 Get the local APIC timer interrupt state.
837 @retval TRUE The local APIC timer interrupt is enabled.
838 @retval FALSE The local APIC timer interrupt is disabled.
842 GetApicTimerInterruptState (
846 LOCAL_APIC_LVT_TIMER LvtTimer
;
848 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
849 return (BOOLEAN
)(LvtTimer
.Bits
.Mask
== 0);
853 Send EOI to the local APIC.
861 WriteLocalApicReg (XAPIC_EOI_OFFSET
, 0);
865 Get the 32-bit address that a device should use to send a Message Signaled
866 Interrupt (MSI) to the Local APIC of the currently executing processor.
868 @return 32-bit address used to send an MSI to the Local APIC.
876 LOCAL_APIC_MSI_ADDRESS MsiAddress
;
879 // Return address for an MSI interrupt to be delivered only to the APIC ID
880 // of the currently executing processor.
882 MsiAddress
.Uint32
= 0;
883 MsiAddress
.Bits
.BaseAddress
= 0xFEE;
884 MsiAddress
.Bits
.DestinationId
= GetApicId ();
885 return MsiAddress
.Uint32
;
889 Get the 64-bit data value that a device should use to send a Message Signaled
890 Interrupt (MSI) to the Local APIC of the currently executing processor.
892 If Vector is not in range 0x10..0xFE, then ASSERT().
893 If DeliveryMode is not supported, then ASSERT().
895 @param Vector The 8-bit interrupt vector associated with the MSI.
896 Must be in the range 0x10..0xFE
897 @param DeliveryMode A 3-bit value that specifies how the recept of the MSI
898 is handled. The only supported values are:
899 0: LOCAL_APIC_DELIVERY_MODE_FIXED
900 1: LOCAL_APIC_DELIVERY_MODE_LOWEST_PRIORITY
901 2: LOCAL_APIC_DELIVERY_MODE_SMI
902 4: LOCAL_APIC_DELIVERY_MODE_NMI
903 5: LOCAL_APIC_DELIVERY_MODE_INIT
904 7: LOCAL_APIC_DELIVERY_MODE_EXTINT
906 @param LevelTriggered TRUE specifies a level triggered interrupt.
907 FALSE specifies an edge triggered interrupt.
908 @param AssertionLevel Ignored if LevelTriggered is FALSE.
909 TRUE specifies a level triggered interrupt that active
910 when the interrupt line is asserted.
911 FALSE specifies a level triggered interrupt that active
912 when the interrupt line is deasserted.
914 @return 64-bit data value used to send an MSI to the Local APIC.
920 IN UINTN DeliveryMode
,
921 IN BOOLEAN LevelTriggered
,
922 IN BOOLEAN AssertionLevel
925 LOCAL_APIC_MSI_DATA MsiData
;
927 ASSERT (Vector
>= 0x10 && Vector
<= 0xFE);
928 ASSERT (DeliveryMode
< 8 && DeliveryMode
!= 6 && DeliveryMode
!= 3);
931 MsiData
.Bits
.Vector
= Vector
;
932 MsiData
.Bits
.DeliveryMode
= (UINT32
)DeliveryMode
;
933 if (LevelTriggered
) {
934 MsiData
.Bits
.TriggerMode
= 1;
935 if (AssertionLevel
) {
936 MsiData
.Bits
.Level
= 1;
939 return MsiData
.Uint64
;