4 This local APIC library instance supports xAPIC mode only.
6 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Register/LocalApic.h>
19 #include <Library/BaseLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/LocalApicLib.h>
22 #include <Library/IoLib.h>
23 #include <Library/TimerLib.h>
24 #include <Library/PcdLib.h>
27 // Library internal functions
31 Read from a local APIC register.
33 This function reads from a local APIC register either in xAPIC or x2APIC mode.
34 It is required that in xAPIC mode wider registers (64-bit or 256-bit) must be
35 accessed using multiple 32-bit loads or stores, so this function only performs
38 @param MmioOffset The MMIO offset of the local APIC register in xAPIC mode.
39 It must be 16-byte aligned.
41 @return 32-bit Value read from the register.
49 ASSERT ((MmioOffset
& 0xf) == 0);
50 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC
);
52 return MmioRead32 (PcdGet32 (PcdCpuLocalApicBaseAddress
) + MmioOffset
);
56 Write to a local APIC register.
58 This function writes to a local APIC register either in xAPIC or x2APIC mode.
59 It is required that in xAPIC mode wider registers (64-bit or 256-bit) must be
60 accessed using multiple 32-bit loads or stores, so this function only performs
63 if the register index is invalid or unsupported in current APIC mode, then ASSERT.
65 @param MmioOffset The MMIO offset of the local APIC register in xAPIC mode.
66 It must be 16-byte aligned.
67 @param Value Value to be written to the register.
76 ASSERT ((MmioOffset
& 0xf) == 0);
77 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC
);
79 MmioWrite32 (PcdGet32 (PcdCpuLocalApicBaseAddress
) + MmioOffset
, Value
);
83 Send an IPI by writing to ICR.
85 This function returns after the IPI has been accepted by the target processor.
87 @param IcrLow 32-bit value to be written to the low half of ICR.
88 @param ApicId APIC ID of the target processor if this IPI is targeted for a specific processor.
96 LOCAL_APIC_ICR_LOW IcrLowReg
;
98 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC
);
99 ASSERT (ApicId
<= 0xff);
102 // For xAPIC, the act of writing to the low doubleword of the ICR causes the IPI to be sent.
104 WriteLocalApicReg (XAPIC_ICR_HIGH_OFFSET
, ApicId
<< 24);
105 WriteLocalApicReg (XAPIC_ICR_LOW_OFFSET
, IcrLow
);
107 IcrLowReg
.Uint32
= ReadLocalApicReg (XAPIC_ICR_LOW_OFFSET
);
108 } while (IcrLowReg
.Bits
.DeliveryStatus
!= 0);
112 // Library API implementation functions
116 Get the current local APIC mode.
118 If local APIC is disabled, then ASSERT.
120 @retval LOCAL_APIC_MODE_XAPIC current APIC mode is xAPIC.
121 @retval LOCAL_APIC_MODE_X2APIC current APIC mode is x2APIC.
131 MSR_IA32_APIC_BASE ApicBaseMsr
;
133 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE_ADDRESS
);
135 // Local APIC should have been enabled
137 ASSERT (ApicBaseMsr
.Bits
.En
!= 0);
138 ASSERT (ApicBaseMsr
.Bits
.Extd
== 0);
141 return LOCAL_APIC_MODE_XAPIC
;
145 Set the current local APIC mode.
147 If the specified local APIC mode is not valid, then ASSERT.
148 If the specified local APIC mode can't be set as current, then ASSERT.
150 @param ApicMode APIC mode to be set.
158 ASSERT (ApicMode
== LOCAL_APIC_MODE_XAPIC
);
159 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC
);
163 Get the initial local APIC ID of the executing processor assigned by hardware upon power on or reset.
165 In xAPIC mode, the initial local APIC ID is 8-bit, and may be different from current APIC ID.
166 In x2APIC mode, the local APIC ID can't be changed and there is no concept of initial APIC ID. In this case,
167 the 32-bit local APIC ID is returned as initial APIC ID.
169 @return 32-bit initial local APIC ID of the executing processor.
179 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC
);
181 AsmCpuid (CPUID_VERSION_INFO
, NULL
, &RegEbx
, NULL
, NULL
);
186 Get the local APIC ID of the executing processor.
188 @return 32-bit local APIC ID of the executing processor.
198 ASSERT (GetApicMode () == LOCAL_APIC_MODE_XAPIC
);
200 ApicId
= ReadLocalApicReg (XAPIC_ID_OFFSET
);
206 Get the value of the local APIC version register.
208 @return the value of the local APIC version register.
216 return ReadLocalApicReg (XAPIC_VERSION_OFFSET
);
220 Send a Fixed IPI to a specified target processor.
222 This function returns after the IPI has been accepted by the target processor.
224 @param ApicId The local APIC ID of the target processor.
225 @param Vector The vector number of the interrupt being sent.
234 LOCAL_APIC_ICR_LOW IcrLow
;
237 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_FIXED
;
238 IcrLow
.Bits
.Level
= 1;
239 IcrLow
.Bits
.Vector
= Vector
;
240 SendIpi (IcrLow
.Uint32
, ApicId
);
244 Send a Fixed IPI to all processors excluding self.
246 This function returns after the IPI has been accepted by the target processors.
248 @param Vector The vector number of the interrupt being sent.
252 SendFixedIpiAllExcludingSelf (
256 LOCAL_APIC_ICR_LOW IcrLow
;
259 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_FIXED
;
260 IcrLow
.Bits
.Level
= 1;
261 IcrLow
.Bits
.DestinationShorthand
= LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF
;
262 IcrLow
.Bits
.Vector
= Vector
;
263 SendIpi (IcrLow
.Uint32
, 0);
267 Send a SMI IPI to a specified target processor.
269 This function returns after the IPI has been accepted by the target processor.
271 @param ApicId Specify the local APIC ID of the target processor.
279 LOCAL_APIC_ICR_LOW IcrLow
;
282 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_SMI
;
283 IcrLow
.Bits
.Level
= 1;
284 SendIpi (IcrLow
.Uint32
, ApicId
);
288 Send a SMI IPI to all processors excluding self.
290 This function returns after the IPI has been accepted by the target processors.
294 SendSmiIpiAllExcludingSelf (
298 LOCAL_APIC_ICR_LOW IcrLow
;
301 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_SMI
;
302 IcrLow
.Bits
.Level
= 1;
303 IcrLow
.Bits
.DestinationShorthand
= LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF
;
304 SendIpi (IcrLow
.Uint32
, 0);
308 Send an INIT IPI to a specified target processor.
310 This function returns after the IPI has been accepted by the target processor.
312 @param ApicId Specify the local APIC ID of the target processor.
320 LOCAL_APIC_ICR_LOW IcrLow
;
323 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_INIT
;
324 IcrLow
.Bits
.Level
= 1;
325 SendIpi (IcrLow
.Uint32
, ApicId
);
329 Send an INIT IPI to all processors excluding self.
331 This function returns after the IPI has been accepted by the target processors.
335 SendInitIpiAllExcludingSelf (
339 LOCAL_APIC_ICR_LOW IcrLow
;
342 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_INIT
;
343 IcrLow
.Bits
.Level
= 1;
344 IcrLow
.Bits
.DestinationShorthand
= LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF
;
345 SendIpi (IcrLow
.Uint32
, 0);
349 Send an INIT-Start-up-Start-up IPI sequence to a specified target processor.
351 This function returns after the IPI has been accepted by the target processor.
353 if StartupRoutine >= 1M, then ASSERT.
354 if StartupRoutine is not multiple of 4K, then ASSERT.
356 @param ApicId Specify the local APIC ID of the target processor.
357 @param StartupRoutine Points to a start-up routine which is below 1M physical
358 address and 4K aligned.
364 IN UINT32 StartupRoutine
367 LOCAL_APIC_ICR_LOW IcrLow
;
369 ASSERT (StartupRoutine
< 0x100000);
370 ASSERT ((StartupRoutine
& 0xfff) == 0);
372 SendInitIpi (ApicId
);
373 MicroSecondDelay (10);
375 IcrLow
.Bits
.Vector
= (StartupRoutine
>> 12);
376 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_STARTUP
;
377 IcrLow
.Bits
.Level
= 1;
378 SendIpi (IcrLow
.Uint32
, ApicId
);
379 MicroSecondDelay (200);
380 SendIpi (IcrLow
.Uint32
, ApicId
);
384 Send an INIT-Start-up-Start-up IPI sequence to all processors excluding self.
386 This function returns after the IPI has been accepted by the target processors.
388 if StartupRoutine >= 1M, then ASSERT.
389 if StartupRoutine is not multiple of 4K, then ASSERT.
391 @param StartupRoutine Points to a start-up routine which is below 1M physical
392 address and 4K aligned.
396 SendInitSipiSipiAllExcludingSelf (
397 IN UINT32 StartupRoutine
400 LOCAL_APIC_ICR_LOW IcrLow
;
402 ASSERT (StartupRoutine
< 0x100000);
403 ASSERT ((StartupRoutine
& 0xfff) == 0);
405 SendInitIpiAllExcludingSelf ();
406 MicroSecondDelay (10);
408 IcrLow
.Bits
.Vector
= (StartupRoutine
>> 12);
409 IcrLow
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_STARTUP
;
410 IcrLow
.Bits
.Level
= 1;
411 IcrLow
.Bits
.DestinationShorthand
= LOCAL_APIC_DESTINATION_SHORTHAND_ALL_EXCLUDING_SELF
;
412 SendIpi (IcrLow
.Uint32
, 0);
413 MicroSecondDelay (200);
414 SendIpi (IcrLow
.Uint32
, 0);
418 Programming Virtual Wire Mode.
420 This function programs the local APIC for virtual wire mode following
421 the example described in chapter A.3 of the MP 1.4 spec.
423 IOxAPIC is not involved in this type of virtual wire mode.
427 ProgramVirtualWireMode (
432 LOCAL_APIC_LVT_LINT Lint
;
435 // Enable the APIC via SVR and set the spurious interrupt to use Int 00F.
437 Svr
.Uint32
= ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
);
438 Svr
.Bits
.SpuriousVector
= 0xf;
439 Svr
.Bits
.SoftwareEnable
= 1;
440 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
, Svr
.Uint32
);
443 // Program the LINT0 vector entry as ExtInt. Not masked, edge, active high.
445 Lint
.Uint32
= ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET
);
446 Lint
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_EXTINT
;
447 Lint
.Bits
.InputPinPolarity
= 0;
448 Lint
.Bits
.TriggerMode
= 0;
450 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET
, Lint
.Uint32
);
453 // Program the LINT0 vector entry as NMI. Not masked, edge, active high.
455 Lint
.Uint32
= ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET
);
456 Lint
.Bits
.DeliveryMode
= LOCAL_APIC_DELIVERY_MODE_NMI
;
457 Lint
.Bits
.InputPinPolarity
= 0;
458 Lint
.Bits
.TriggerMode
= 0;
460 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET
, Lint
.Uint32
);
464 Disable LINT0 & LINT1 interrupts.
466 This function sets the mask flag in the LVT LINT0 & LINT1 registers.
470 DisableLvtInterrupts (
474 LOCAL_APIC_LVT_LINT LvtLint
;
476 LvtLint
.Uint32
= ReadLocalApicReg (XAPIC_LVT_LINT0_OFFSET
);
477 LvtLint
.Bits
.Mask
= 1;
478 WriteLocalApicReg (XAPIC_LVT_LINT0_OFFSET
, LvtLint
.Uint32
);
480 LvtLint
.Uint32
= ReadLocalApicReg (XAPIC_LVT_LINT1_OFFSET
);
481 LvtLint
.Bits
.Mask
= 1;
482 WriteLocalApicReg (XAPIC_LVT_LINT1_OFFSET
, LvtLint
.Uint32
);
486 Read the initial count value from the init-count register.
488 @return The initial count value read from the init-count register.
492 GetApicTimerInitCount (
496 return ReadLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET
);
500 Read the current count value from the current-count register.
502 @return The current count value read from the current-count register.
506 GetApicTimerCurrentCount (
510 return ReadLocalApicReg (XAPIC_TIMER_CURRENT_COUNT_OFFSET
);
514 Initialize the local APIC timer.
516 The local APIC timer is initialized and enabled.
518 @param DivideValue The divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.
519 If it is 0, then use the current divide value in the DCR.
520 @param InitCount The initial count value.
521 @param PeriodicMode If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.
522 @param Vector The timer interrupt vector number.
526 InitializeApicTimer (
527 IN UINTN DivideValue
,
529 IN BOOLEAN PeriodicMode
,
535 LOCAL_APIC_LVT_TIMER LvtTimer
;
539 // Ensure local APIC is in software-enabled state.
541 Svr
.Uint32
= ReadLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
);
542 Svr
.Bits
.SoftwareEnable
= 1;
543 WriteLocalApicReg (XAPIC_SPURIOUS_VECTOR_OFFSET
, Svr
.Uint32
);
546 // Program init-count register.
548 WriteLocalApicReg (XAPIC_TIMER_INIT_COUNT_OFFSET
, InitCount
);
550 if (DivideValue
!= 0) {
551 ASSERT (DivideValue
<= 128);
552 ASSERT (DivideValue
== GetPowerOfTwo32((UINT32
)DivideValue
));
553 Divisor
= (UINT32
)((HighBitSet32 ((UINT32
)DivideValue
) - 1) & 0x7);
555 Dcr
.Uint32
= ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET
);
556 Dcr
.Bits
.DivideValue1
= (Divisor
& 0x3);
557 Dcr
.Bits
.DivideValue2
= (Divisor
>> 2);
558 WriteLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET
, Dcr
.Uint32
);
562 // Enable APIC timer interrupt with specified timer mode.
564 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
566 LvtTimer
.Bits
.TimerMode
= 1;
568 LvtTimer
.Bits
.TimerMode
= 0;
570 LvtTimer
.Bits
.Mask
= 0;
571 LvtTimer
.Bits
.Vector
= Vector
;
572 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET
, LvtTimer
.Uint32
);
576 Get the state of the local APIC timer.
578 @param DivideValue Return the divide value for the DCR. It is one of 1,2,4,8,16,32,64,128.
579 @param PeriodicMode Return the timer mode. If TRUE, timer mode is peridoic. Othewise, timer mode is one-shot.
580 @param Vector Return the timer interrupt vector number.
585 OUT UINTN
*DivideValue OPTIONAL
,
586 OUT BOOLEAN
*PeriodicMode OPTIONAL
,
587 OUT UINT8
*Vector OPTIONAL
592 LOCAL_APIC_LVT_TIMER LvtTimer
;
594 if (DivideValue
!= NULL
) {
595 Dcr
.Uint32
= ReadLocalApicReg (XAPIC_TIMER_DIVIDE_CONFIGURATION_OFFSET
);
596 Divisor
= Dcr
.Bits
.DivideValue1
| (Dcr
.Bits
.DivideValue2
<< 2);
597 Divisor
= (Divisor
+ 1) & 0x7;
598 *DivideValue
= ((UINTN
)1) << Divisor
;
601 if (PeriodicMode
!= NULL
|| Vector
!= NULL
) {
602 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
603 if (PeriodicMode
!= NULL
) {
604 if (LvtTimer
.Bits
.TimerMode
== 1) {
605 *PeriodicMode
= TRUE
;
607 *PeriodicMode
= FALSE
;
610 if (Vector
!= NULL
) {
611 *Vector
= (UINT8
) LvtTimer
.Bits
.Vector
;
617 Enable the local APIC timer interrupt.
621 EnableApicTimerInterrupt (
625 LOCAL_APIC_LVT_TIMER LvtTimer
;
627 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
628 LvtTimer
.Bits
.Mask
= 0;
629 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET
, LvtTimer
.Uint32
);
633 Disable the local APIC timer interrupt.
637 DisableApicTimerInterrupt (
641 LOCAL_APIC_LVT_TIMER LvtTimer
;
643 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
644 LvtTimer
.Bits
.Mask
= 1;
645 WriteLocalApicReg (XAPIC_LVT_TIMER_OFFSET
, LvtTimer
.Uint32
);
649 Get the local APIC timer interrupt state.
651 @retval TRUE The local APIC timer interrupt is enabled.
652 @retval FALSE The local APIC timer interrupt is disabled.
656 GetApicTimerInterruptState (
660 LOCAL_APIC_LVT_TIMER LvtTimer
;
662 LvtTimer
.Uint32
= ReadLocalApicReg (XAPIC_LVT_TIMER_OFFSET
);
663 return (BOOLEAN
)(LvtTimer
.Bits
.Mask
== 0);
667 Send EOI to the local APIC.
675 WriteLocalApicReg (XAPIC_EOI_OFFSET
, 0);