2 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 #include <mach/mach_types.h>
33 #include <mach/kern_return.h>
35 #include <kern/kern_types.h>
36 #include <kern/cpu_number.h>
37 #include <kern/cpu_data.h>
38 #include <kern/assert.h>
39 #include <kern/machine.h>
40 #include <kern/debug.h>
42 #include <vm/vm_map.h>
43 #include <vm/vm_kern.h>
45 #include <i386/lapic.h>
46 #include <i386/cpuid.h>
47 #include <i386/proc_reg.h>
48 #include <i386/machine_cpu.h>
49 #include <i386/misc_protos.h>
51 #include <i386/postcode.h>
52 #include <i386/cpu_threads.h>
53 #include <i386/machine_routines.h>
56 #include <i386/machine_check.h>
59 #include <sys/kdebug.h>
62 #define PAUSE delay(1000000)
63 #define DBG(x...) kprintf(x)
69 lapic_ops_table_t
*lapic_ops
; /* Lapic operations switch */
71 static vm_map_offset_t lapic_pbase
; /* Physical base memory-mapped regs */
72 static vm_offset_t lapic_vbase
; /* Virtual base memory-mapped regs */
74 static i386_intr_func_t lapic_intr_func
[LAPIC_FUNC_TABLE_SIZE
];
76 /* TRUE if local APIC was enabled by the OS not by the BIOS */
77 static boolean_t lapic_os_enabled
= FALSE
;
79 static boolean_t lapic_errors_masked
= FALSE
;
80 static uint64_t lapic_last_master_error
= 0;
81 static uint64_t lapic_error_time_threshold
= 0;
82 static unsigned lapic_master_error_count
= 0;
83 static unsigned lapic_error_count_threshold
= 5;
84 static boolean_t lapic_dont_panic
= FALSE
;
88 lapic_cpu_map_dump(void)
92 for (i
= 0; i
< MAX_CPUS
; i
++) {
93 if (cpu_to_lapic
[i
] == -1)
95 kprintf("cpu_to_lapic[%d]: %d\n",
98 for (i
= 0; i
< MAX_LAPICIDS
; i
++) {
99 if (lapic_to_cpu
[i
] == -1)
101 kprintf("lapic_to_cpu[%d]: %d\n",
105 #endif /* MP_DEBUG */
111 vm_map_entry_t entry
;
112 vm_map_offset_t lapic_vbase64
;
113 /* Establish a map to the local apic */
115 if (lapic_vbase
== 0) {
116 lapic_vbase64
= (vm_offset_t
)vm_map_min(kernel_map
);
117 result
= vm_map_find_space(kernel_map
,
119 round_page(LAPIC_SIZE
), 0,
120 VM_MAKE_TAG(VM_KERN_MEMORY_IOKIT
), &entry
);
121 /* Convert 64-bit vm_map_offset_t to "pointer sized" vm_offset_t
123 lapic_vbase
= (vm_offset_t
) lapic_vbase64
;
124 if (result
!= KERN_SUCCESS
) {
125 panic("legacy_init: vm_map_find_entry FAILED (err=%d)", result
);
127 vm_map_unlock(kernel_map
);
130 * Map in the local APIC non-cacheable, as recommended by Intel
131 * in section 8.4.1 of the "System Programming Guide".
132 * In fact, this is redundant because EFI will have assigned an
133 * MTRR physical range containing the local APIC's MMIO space as
134 * UC and this will override the default PAT setting.
136 pmap_enter(pmap_kernel(),
138 (ppnum_t
) i386_btop(lapic_pbase
),
139 VM_PROT_READ
|VM_PROT_WRITE
,
146 * Set flat delivery model, logical processor id
147 * This should already be the default set.
149 LAPIC_WRITE(DFR
, LAPIC_DFR_FLAT
);
150 LAPIC_WRITE(LDR
, (get_cpu_number()) << LAPIC_LDR_SHIFT
);
155 legacy_read(lapic_register_t reg
)
157 return *LAPIC_MMIO(reg
);
161 legacy_write(lapic_register_t reg
, uint32_t value
)
163 *LAPIC_MMIO(reg
) = value
;
167 legacy_read_icr(void)
169 return (((uint64_t)*LAPIC_MMIO(ICRD
)) << 32) | ((uint64_t)*LAPIC_MMIO(ICR
));
173 legacy_write_icr(uint32_t dst
, uint32_t cmd
)
175 *LAPIC_MMIO(ICRD
) = dst
<< LAPIC_ICRD_DEST_SHIFT
;
176 *LAPIC_MMIO(ICR
) = cmd
;
179 static lapic_ops_table_t legacy_ops
= {
187 static boolean_t is_x2apic
= FALSE
;
195 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
196 if ((lo
& MSR_IA32_APIC_BASE_EXTENDED
) == 0) {
197 lo
|= MSR_IA32_APIC_BASE_EXTENDED
;
198 wrmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
199 kprintf("x2APIC mode enabled\n");
204 x2apic_read(lapic_register_t reg
)
209 rdmsr(LAPIC_MSR(reg
), lo
, hi
);
214 x2apic_write(lapic_register_t reg
, uint32_t value
)
216 wrmsr(LAPIC_MSR(reg
), value
, 0);
220 x2apic_read_icr(void)
222 return rdmsr64(LAPIC_MSR(ICR
));;
226 x2apic_write_icr(uint32_t dst
, uint32_t cmd
)
228 wrmsr(LAPIC_MSR(ICR
), cmd
, dst
);
231 static lapic_ops_table_t x2apic_ops
= {
244 boolean_t is_boot_processor
;
245 boolean_t is_lapic_enabled
;
247 /* Examine the local APIC state */
248 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
249 is_boot_processor
= (lo
& MSR_IA32_APIC_BASE_BSP
) != 0;
250 is_lapic_enabled
= (lo
& MSR_IA32_APIC_BASE_ENABLE
) != 0;
251 is_x2apic
= (lo
& MSR_IA32_APIC_BASE_EXTENDED
) != 0;
252 lapic_pbase
= (lo
& MSR_IA32_APIC_BASE_BASE
);
253 kprintf("MSR_IA32_APIC_BASE 0x%llx %s %s mode %s\n", lapic_pbase
,
254 is_lapic_enabled
? "enabled" : "disabled",
255 is_x2apic
? "extended" : "legacy",
256 is_boot_processor
? "BSP" : "AP");
257 if (!is_boot_processor
|| !is_lapic_enabled
)
258 panic("Unexpected local APIC state\n");
261 * If x2APIC is available and not already enabled, enable it.
262 * Unless overriden by boot-arg.
264 if (!is_x2apic
&& (cpuid_features() & CPUID_FEATURE_x2APIC
)) {
265 PE_parse_boot_argn("-x2apic", &is_x2apic
, sizeof(is_x2apic
));
266 kprintf("x2APIC supported %s be enabled\n",
267 is_x2apic
? "and will" : "but will not");
270 lapic_ops
= is_x2apic
? &x2apic_ops
: &legacy_ops
;
274 kprintf("ID: 0x%x LDR: 0x%x\n", LAPIC_READ(ID
), LAPIC_READ(LDR
));
275 if ((LAPIC_READ(VERSION
)&LAPIC_VERSION_MASK
) < 0x14) {
276 panic("Local APIC version 0x%x, 0x14 or more expected\n",
277 (LAPIC_READ(VERSION
)&LAPIC_VERSION_MASK
));
280 /* Set up the lapic_id <-> cpu_number map and add this boot processor */
281 lapic_cpu_map_init();
282 lapic_cpu_map((LAPIC_READ(ID
)>>LAPIC_ID_SHIFT
)&LAPIC_ID_MASK
, 0);
283 current_cpu_datap()->cpu_phys_number
= cpu_to_lapic
[0];
284 kprintf("Boot cpu local APIC id 0x%x\n", cpu_to_lapic
[0]);
291 /* write-read register */
292 LAPIC_WRITE(ERROR_STATUS
, 0);
293 return LAPIC_READ(ERROR_STATUS
);
297 lapic_esr_clear(void)
299 LAPIC_WRITE(ERROR_STATUS
, 0);
300 LAPIC_WRITE(ERROR_STATUS
, 0);
303 static const char *DM_str
[8] = {
313 static const char *TMR_str
[] = {
325 #define BOOL(a) ((a)?' ':'!')
327 LAPIC_READ(lvt)&LAPIC_LVT_VECTOR_MASK
329 (LAPIC_READ(lvt)&LAPIC_LVT_DS_PENDING)?" SendPending" : "Idle"
331 DM_str[(LAPIC_READ(lvt)>>LAPIC_LVT_DM_SHIFT)&LAPIC_LVT_DM_MASK]
333 BOOL(LAPIC_READ(lvt)&LAPIC_LVT_MASKED)
335 (LAPIC_READ(lvt)&LAPIC_LVT_TM_LEVEL)? "Level" : "Edge"
337 (LAPIC_READ(lvt)&LAPIC_LVT_IP_PLRITY_LOW)? "Low " : "High"
339 kprintf("LAPIC %d at %p version 0x%x\n",
340 (LAPIC_READ(ID
)>>LAPIC_ID_SHIFT
)&LAPIC_ID_MASK
,
341 (void *) lapic_vbase
,
342 LAPIC_READ(VERSION
)&LAPIC_VERSION_MASK
);
343 kprintf("Priorities: Task 0x%x Arbitration 0x%x Processor 0x%x\n",
344 LAPIC_READ(TPR
)&LAPIC_TPR_MASK
,
345 LAPIC_READ(APR
)&LAPIC_APR_MASK
,
346 LAPIC_READ(PPR
)&LAPIC_PPR_MASK
);
347 kprintf("Destination Format 0x%x Logical Destination 0x%x\n",
348 is_x2apic
? 0 : LAPIC_READ(DFR
)>>LAPIC_DFR_SHIFT
,
349 LAPIC_READ(LDR
)>>LAPIC_LDR_SHIFT
);
350 kprintf("%cEnabled %cFocusChecking SV 0x%x\n",
351 BOOL(LAPIC_READ(SVR
)&LAPIC_SVR_ENABLE
),
352 BOOL(!(LAPIC_READ(SVR
)&LAPIC_SVR_FOCUS_OFF
)),
353 LAPIC_READ(SVR
) & LAPIC_SVR_MASK
);
355 if (mca_is_cmci_present())
356 kprintf("LVT_CMCI: Vector 0x%02x [%s] %s %cmasked\n",
362 kprintf("LVT_TIMER: Vector 0x%02x %s %cmasked %s\n",
366 TMR_str
[(LAPIC_READ(LVT_TIMER
) >> LAPIC_LVT_TMR_SHIFT
)
367 & LAPIC_LVT_TMR_MASK
]);
368 kprintf(" Initial Count: 0x%08x \n", LAPIC_READ(TIMER_INITIAL_COUNT
));
369 kprintf(" Current Count: 0x%08x \n", LAPIC_READ(TIMER_CURRENT_COUNT
));
370 kprintf(" Divide Config: 0x%08x \n", LAPIC_READ(TIMER_DIVIDE_CONFIG
));
371 kprintf("LVT_PERFCNT: Vector 0x%02x [%s] %s %cmasked\n",
376 kprintf("LVT_THERMAL: Vector 0x%02x [%s] %s %cmasked\n",
381 kprintf("LVT_LINT0: Vector 0x%02x [%s][%s][%s] %s %cmasked\n",
388 kprintf("LVT_LINT1: Vector 0x%02x [%s][%s][%s] %s %cmasked\n",
395 kprintf("LVT_ERROR: Vector 0x%02x %s %cmasked\n",
399 kprintf("ESR: %08x \n", lapic_esr_read());
401 for(i
=0xf; i
>=0; i
--)
402 kprintf("%x%x%x%x",i
,i
,i
,i
);
406 kprintf("%08x",LAPIC_READ_OFFSET(TMR_BASE
, i
));
410 kprintf("%08x",LAPIC_READ_OFFSET(IRR_BASE
, i
));
413 for(i
=7; i
>= 0; i
--)
414 kprintf("%08x",LAPIC_READ_OFFSET(ISR_BASE
, i
));
424 if (cpuid_features() & CPUID_FEATURE_APIC
)
427 if (cpuid_family() == 6 || cpuid_family() == 15) {
430 * There may be a local APIC which wasn't enabled by BIOS.
431 * So we try to enable it explicitly.
433 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
434 lo
&= ~MSR_IA32_APIC_BASE_BASE
;
435 lo
|= MSR_IA32_APIC_BASE_ENABLE
| LAPIC_START
;
436 lo
|= MSR_IA32_APIC_BASE_ENABLE
;
437 wrmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
440 * Re-initialize cpu features info and re-check.
443 /* We expect this codepath will never be traversed
444 * due to EFI enabling the APIC. Reducing the APIC
445 * interrupt base dynamically is not supported.
447 if (cpuid_features() & CPUID_FEATURE_APIC
) {
448 printf("Local APIC discovered and enabled\n");
449 lapic_os_enabled
= TRUE
;
450 lapic_interrupt_base
= LAPIC_REDUCED_INTERRUPT_BASE
;
465 /* Shutdown if local APIC was enabled by OS */
466 if (lapic_os_enabled
== FALSE
)
469 mp_disable_preemption();
472 if (get_cpu_number() == master_cpu
) {
473 value
= LAPIC_READ(LVT_LINT0
);
474 value
|= LAPIC_LVT_MASKED
;
475 LAPIC_WRITE(LVT_LINT0
, value
);
479 LAPIC_WRITE(LVT_ERROR
, LAPIC_READ(LVT_ERROR
) | LAPIC_LVT_MASKED
);
482 LAPIC_WRITE(LVT_TIMER
, LAPIC_READ(LVT_TIMER
) | LAPIC_LVT_MASKED
);
484 /* Perfmon: masked */
485 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_READ(LVT_PERFCNT
) | LAPIC_LVT_MASKED
);
487 /* APIC software disabled */
488 LAPIC_WRITE(SVR
, LAPIC_READ(SVR
) & ~LAPIC_SVR_ENABLE
);
490 /* Bypass the APIC completely and update cpu features */
491 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
492 lo
&= ~MSR_IA32_APIC_BASE_ENABLE
;
493 wrmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
496 mp_enable_preemption();
500 lapic_configure(void)
504 if (lapic_error_time_threshold
== 0 && cpu_number() == 0) {
505 nanoseconds_to_absolutetime(NSEC_PER_SEC
>> 2, &lapic_error_time_threshold
);
506 if (!PE_parse_boot_argn("lapic_dont_panic", &lapic_dont_panic
, sizeof(lapic_dont_panic
))) {
507 lapic_dont_panic
= FALSE
;
514 LAPIC_WRITE(SVR
, LAPIC_VECTOR(SPURIOUS
) | LAPIC_SVR_ENABLE
);
517 if (get_cpu_number() == master_cpu
) {
518 value
= LAPIC_READ(LVT_LINT0
);
519 value
&= ~LAPIC_LVT_MASKED
;
520 value
|= LAPIC_LVT_DM_EXTINT
;
521 LAPIC_WRITE(LVT_LINT0
, value
);
524 /* Timer: unmasked, one-shot */
525 LAPIC_WRITE(LVT_TIMER
, LAPIC_VECTOR(TIMER
));
527 /* Perfmon: unmasked */
528 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_VECTOR(PERFCNT
));
530 /* Thermal: unmasked */
531 LAPIC_WRITE(LVT_THERMAL
, LAPIC_VECTOR(THERMAL
));
534 /* CMCI, if available */
535 if (mca_is_cmci_present())
536 LAPIC_WRITE(LVT_CMCI
, LAPIC_VECTOR(CMCI
));
539 if (((cpu_number() == master_cpu
) && lapic_errors_masked
== FALSE
) ||
540 (cpu_number() != master_cpu
)) {
542 LAPIC_WRITE(LVT_ERROR
, LAPIC_VECTOR(ERROR
));
548 boolean_t interrupt_unmasked
,
549 lapic_timer_mode_t mode
,
550 lapic_timer_divide_t divisor
,
551 lapic_timer_count_t initial_count
)
553 uint32_t timer_vector
;
555 mp_disable_preemption();
556 timer_vector
= LAPIC_READ(LVT_TIMER
);
557 timer_vector
&= ~(LAPIC_LVT_MASKED
|LAPIC_LVT_PERIODIC
);;
558 timer_vector
|= interrupt_unmasked
? 0 : LAPIC_LVT_MASKED
;
559 timer_vector
|= (mode
== periodic
) ? LAPIC_LVT_PERIODIC
: 0;
560 LAPIC_WRITE(LVT_TIMER
, timer_vector
);
561 LAPIC_WRITE(TIMER_DIVIDE_CONFIG
, divisor
);
562 LAPIC_WRITE(TIMER_INITIAL_COUNT
, initial_count
);
563 mp_enable_preemption();
568 boolean_t interrupt_unmasked
,
569 lapic_timer_mode_t mode
,
570 lapic_timer_divide_t divisor
)
572 uint32_t timer_vector
;
574 mp_disable_preemption();
575 timer_vector
= LAPIC_READ(LVT_TIMER
);
576 timer_vector
&= ~(LAPIC_LVT_MASKED
|
578 LAPIC_LVT_TSC_DEADLINE
);
579 timer_vector
|= interrupt_unmasked
? 0 : LAPIC_LVT_MASKED
;
580 timer_vector
|= (mode
== periodic
) ? LAPIC_LVT_PERIODIC
: 0;
581 LAPIC_WRITE(LVT_TIMER
, timer_vector
);
582 LAPIC_WRITE(TIMER_DIVIDE_CONFIG
, divisor
);
583 mp_enable_preemption();
587 * Configure TSC-deadline timer mode. The lapic interrupt is always unmasked.
590 lapic_config_tsc_deadline_timer(void)
592 uint32_t timer_vector
;
594 DBG("lapic_config_tsc_deadline_timer()\n");
595 mp_disable_preemption();
596 timer_vector
= LAPIC_READ(LVT_TIMER
);
597 timer_vector
&= ~(LAPIC_LVT_MASKED
|
599 timer_vector
|= LAPIC_LVT_TSC_DEADLINE
;
600 LAPIC_WRITE(LVT_TIMER
, timer_vector
);
602 /* Serialize writes per Intel OSWG */
604 lapic_set_tsc_deadline_timer(rdtsc64() + (1ULL<<32));
605 } while (lapic_get_tsc_deadline_timer() == 0);
606 lapic_set_tsc_deadline_timer(0);
608 mp_enable_preemption();
609 DBG("lapic_config_tsc_deadline_timer() done\n");
613 lapic_set_timer_fast(
614 lapic_timer_count_t initial_count
)
616 LAPIC_WRITE(LVT_TIMER
, LAPIC_READ(LVT_TIMER
) & ~LAPIC_LVT_MASKED
);
617 LAPIC_WRITE(TIMER_INITIAL_COUNT
, initial_count
);
621 lapic_set_tsc_deadline_timer(uint64_t deadline
)
623 /* Don't bother disarming: wrmsr64(MSR_IA32_TSC_DEADLINE, 0); */
624 wrmsr64(MSR_IA32_TSC_DEADLINE
, deadline
);
628 lapic_get_tsc_deadline_timer(void)
630 return rdmsr64(MSR_IA32_TSC_DEADLINE
);
635 lapic_timer_mode_t
*mode
,
636 lapic_timer_divide_t
*divisor
,
637 lapic_timer_count_t
*initial_count
,
638 lapic_timer_count_t
*current_count
)
640 mp_disable_preemption();
642 *mode
= (LAPIC_READ(LVT_TIMER
) & LAPIC_LVT_PERIODIC
) ?
645 *divisor
= LAPIC_READ(TIMER_DIVIDE_CONFIG
) & LAPIC_TIMER_DIVIDE_MASK
;
647 *initial_count
= LAPIC_READ(TIMER_INITIAL_COUNT
);
649 *current_count
= LAPIC_READ(TIMER_CURRENT_COUNT
);
650 mp_enable_preemption();
654 _lapic_end_of_interrupt(void)
660 lapic_end_of_interrupt(void)
662 _lapic_end_of_interrupt();
665 void lapic_unmask_perfcnt_interrupt(void) {
666 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_VECTOR(PERFCNT
));
669 void lapic_set_perfcnt_interrupt_mask(boolean_t mask
) {
670 uint32_t m
= (mask
? LAPIC_LVT_MASKED
: 0);
671 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_VECTOR(PERFCNT
) | m
);
675 lapic_set_intr_func(int vector
, i386_intr_func_t func
)
677 if (vector
> lapic_interrupt_base
)
678 vector
-= lapic_interrupt_base
;
681 case LAPIC_NMI_INTERRUPT
:
682 case LAPIC_INTERPROCESSOR_INTERRUPT
:
683 case LAPIC_TIMER_INTERRUPT
:
684 case LAPIC_THERMAL_INTERRUPT
:
685 case LAPIC_PERFCNT_INTERRUPT
:
686 case LAPIC_CMCI_INTERRUPT
:
687 case LAPIC_PM_INTERRUPT
:
688 lapic_intr_func
[vector
] = func
;
691 panic("lapic_set_intr_func(%d,%p) invalid vector\n",
696 void lapic_set_pmi_func(i386_intr_func_t func
) {
697 lapic_set_intr_func(LAPIC_VECTOR(PERFCNT
), func
);
701 lapic_interrupt(int interrupt_num
, x86_saved_state_t
*state
)
706 interrupt_num
-= lapic_interrupt_base
;
707 if (interrupt_num
< 0) {
708 if (interrupt_num
== (LAPIC_NMI_INTERRUPT
- lapic_interrupt_base
) &&
709 lapic_intr_func
[LAPIC_NMI_INTERRUPT
] != NULL
) {
710 retval
= (*lapic_intr_func
[LAPIC_NMI_INTERRUPT
])(state
);
717 switch(interrupt_num
) {
718 case LAPIC_TIMER_INTERRUPT
:
719 case LAPIC_THERMAL_INTERRUPT
:
720 case LAPIC_INTERPROCESSOR_INTERRUPT
:
721 case LAPIC_PM_INTERRUPT
:
722 if (lapic_intr_func
[interrupt_num
] != NULL
)
723 (void) (*lapic_intr_func
[interrupt_num
])(state
);
724 _lapic_end_of_interrupt();
727 case LAPIC_PERFCNT_INTERRUPT
:
728 /* If a function has been registered, invoke it. Otherwise,
731 if (lapic_intr_func
[interrupt_num
] != NULL
) {
732 (void) (*lapic_intr_func
[interrupt_num
])(state
);
733 /* Unmask the interrupt since we don't expect legacy users
734 * to be responsible for it.
736 lapic_unmask_perfcnt_interrupt();
737 _lapic_end_of_interrupt();
741 case LAPIC_CMCI_INTERRUPT
:
742 if (lapic_intr_func
[interrupt_num
] != NULL
)
743 (void) (*lapic_intr_func
[interrupt_num
])(state
);
744 /* return 0 for plaform expert to handle */
746 case LAPIC_ERROR_INTERRUPT
:
747 /* We treat error interrupts on APs as fatal.
748 * The current interrupt steering scheme directs most
749 * external interrupts to the BSP (HPET interrupts being
750 * a notable exception); hence, such an error
751 * on an AP may signify LVT corruption (with "may" being
752 * the operative word). On the BSP, we adopt a more
753 * lenient approach, in the interests of enhancing
754 * debuggability and reducing fragility.
755 * If "lapic_error_count_threshold" error interrupts
756 * occur within "lapic_error_time_threshold" absolute
757 * time units, we mask the error vector and log. The
758 * error interrupts themselves are likely
759 * side effects of issues which are beyond the purview of
760 * the local APIC interrupt handler, however. The Error
761 * Status Register value (the illegal destination
762 * vector code is one observed in practice) indicates
763 * the immediate cause of the error.
765 esr
= lapic_esr_read();
768 if ((debug_boot_arg
&& (lapic_dont_panic
== FALSE
)) ||
769 cpu_number() != master_cpu
) {
770 panic("Local APIC error, ESR: %d\n", esr
);
773 if (cpu_number() == master_cpu
) {
774 uint64_t abstime
= mach_absolute_time();
775 if ((abstime
- lapic_last_master_error
) < lapic_error_time_threshold
) {
776 if (lapic_master_error_count
++ > lapic_error_count_threshold
) {
777 lapic_errors_masked
= TRUE
;
778 LAPIC_WRITE(LVT_ERROR
, LAPIC_READ(LVT_ERROR
) | LAPIC_LVT_MASKED
);
779 printf("Local APIC: errors masked\n");
783 lapic_last_master_error
= abstime
;
784 lapic_master_error_count
= 0;
786 printf("Local APIC error on master CPU, ESR: %d, error count this run: %d\n", esr
, lapic_master_error_count
);
789 _lapic_end_of_interrupt();
792 case LAPIC_SPURIOUS_INTERRUPT
:
794 /* No EOI required here */
797 case LAPIC_PMC_SW_INTERRUPT
:
801 case LAPIC_KICK_INTERRUPT
:
802 _lapic_end_of_interrupt();
811 lapic_smm_restore(void)
815 if (lapic_os_enabled
== FALSE
)
818 state
= ml_set_interrupts_enabled(FALSE
);
820 if (LAPIC_ISR_IS_SET(LAPIC_REDUCED_INTERRUPT_BASE
, TIMER
)) {
822 * Bogus SMI handler enables interrupts but does not know about
823 * local APIC interrupt sources. When APIC timer counts down to
824 * zero while in SMM, local APIC will end up waiting for an EOI
825 * but no interrupt was delivered to the OS.
827 _lapic_end_of_interrupt();
830 * timer is one-shot, trigger another quick countdown to trigger
831 * another timer interrupt.
833 if (LAPIC_READ(TIMER_CURRENT_COUNT
) == 0) {
834 LAPIC_WRITE(TIMER_INITIAL_COUNT
, 1);
837 kprintf("lapic_smm_restore\n");
840 ml_set_interrupts_enabled(state
);
844 lapic_send_ipi(int cpu
, int vector
)
848 if (vector
< lapic_interrupt_base
)
849 vector
+= lapic_interrupt_base
;
851 state
= ml_set_interrupts_enabled(FALSE
);
853 /* Wait for pending outgoing send to complete */
854 while (LAPIC_READ_ICR() & LAPIC_ICR_DS_PENDING
) {
858 LAPIC_WRITE_ICR(cpu_to_lapic
[cpu
], vector
| LAPIC_ICR_DM_FIXED
);
860 (void) ml_set_interrupts_enabled(state
);
864 * The following interfaces are privately exported to AICPM.
868 lapic_is_interrupt_pending(void)
872 for (i
= 0; i
< 8; i
+= 1) {
873 if ((LAPIC_READ_OFFSET(IRR_BASE
, i
) != 0) ||
874 (LAPIC_READ_OFFSET(ISR_BASE
, i
) != 0))
882 lapic_is_interrupting(uint8_t vector
)
890 bit
= 1 << (vector
% 32);
892 irr
= LAPIC_READ_OFFSET(IRR_BASE
, i
);
893 isr
= LAPIC_READ_OFFSET(ISR_BASE
, i
);
895 if ((irr
| isr
) & bit
)
902 lapic_interrupt_counts(uint64_t intrs
[256])
913 for (i
= 0; i
< 8; i
+= 1) {
914 irr
= LAPIC_READ_OFFSET(IRR_BASE
, i
);
915 isr
= LAPIC_READ_OFFSET(ISR_BASE
, i
);
917 if ((isr
| irr
) == 0)
920 for (j
= (i
== 0) ? 16 : 0; j
< 32; j
+= 1) {
922 if ((isr
| irr
) & (1 << j
))
929 lapic_disable_timer(void)
934 * If we're in deadline timer mode,
935 * simply clear the deadline timer, otherwise
936 * mask the timer interrupt and clear the countdown.
938 lvt_timer
= LAPIC_READ(LVT_TIMER
);
939 if (lvt_timer
& LAPIC_LVT_TSC_DEADLINE
) {
940 wrmsr64(MSR_IA32_TSC_DEADLINE
, 0);
942 LAPIC_WRITE(LVT_TIMER
, lvt_timer
| LAPIC_LVT_MASKED
);
943 LAPIC_WRITE(TIMER_INITIAL_COUNT
, 0);
944 lvt_timer
= LAPIC_READ(LVT_TIMER
);
948 /* SPI returning the CMCI vector */
950 lapic_get_cmci_vector(void)
952 uint8_t cmci_vector
= 0;
954 /* CMCI, if available */
955 if (mca_is_cmci_present())
956 cmci_vector
= LAPIC_VECTOR(CMCI
);
961 #if DEVELOPMENT || DEBUG
962 extern void lapic_trigger_MC(void);
964 lapic_trigger_MC(void)
966 /* A 64-bit access to any register will do it. */
967 volatile uint64_t dummy
= *(volatile uint64_t *) (volatile void *) LAPIC_MMIO(ID
);