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>
63 #include <sys/kdebug.h>
66 #define PAUSE delay(1000000)
67 #define DBG(x...) kprintf(x)
73 lapic_ops_table_t
*lapic_ops
; /* Lapic operations switch */
75 static vm_map_offset_t lapic_pbase
; /* Physical base memory-mapped regs */
76 static vm_offset_t lapic_vbase
; /* Virtual base memory-mapped regs */
78 static i386_intr_func_t lapic_intr_func
[LAPIC_FUNC_TABLE_SIZE
];
80 /* TRUE if local APIC was enabled by the OS not by the BIOS */
81 static boolean_t lapic_os_enabled
= FALSE
;
83 static boolean_t lapic_errors_masked
= FALSE
;
84 static uint64_t lapic_last_master_error
= 0;
85 static uint64_t lapic_error_time_threshold
= 0;
86 static unsigned lapic_master_error_count
= 0;
87 static unsigned lapic_error_count_threshold
= 5;
88 static boolean_t lapic_dont_panic
= FALSE
;
92 lapic_cpu_map_dump(void)
96 for (i
= 0; i
< MAX_CPUS
; i
++) {
97 if (cpu_to_lapic
[i
] == -1)
99 kprintf("cpu_to_lapic[%d]: %d\n",
102 for (i
= 0; i
< MAX_LAPICIDS
; i
++) {
103 if (lapic_to_cpu
[i
] == -1)
105 kprintf("lapic_to_cpu[%d]: %d\n",
109 #endif /* MP_DEBUG */
115 vm_map_entry_t entry
;
116 vm_map_offset_t lapic_vbase64
;
117 /* Establish a map to the local apic */
119 lapic_vbase64
= (vm_offset_t
)vm_map_min(kernel_map
);
120 result
= vm_map_find_space(kernel_map
,
122 round_page(LAPIC_SIZE
), 0,
123 VM_MAKE_TAG(VM_MEMORY_IOKIT
), &entry
);
124 /* Convert 64-bit vm_map_offset_t to "pointer sized" vm_offset_t
126 lapic_vbase
= (vm_offset_t
) lapic_vbase64
;
127 if (result
!= KERN_SUCCESS
) {
128 panic("legacy_init: vm_map_find_entry FAILED (err=%d)", result
);
130 vm_map_unlock(kernel_map
);
133 * Map in the local APIC non-cacheable, as recommended by Intel
134 * in section 8.4.1 of the "System Programming Guide".
135 * In fact, this is redundant because EFI will have assigned an
136 * MTRR physical range containing the local APIC's MMIO space as
137 * UC and this will override the default PAT setting.
139 pmap_enter(pmap_kernel(),
141 (ppnum_t
) i386_btop(lapic_pbase
),
142 VM_PROT_READ
|VM_PROT_WRITE
,
150 legacy_read(lapic_register_t reg
)
152 return *LAPIC_MMIO(reg
);
156 legacy_write(lapic_register_t reg
, uint32_t value
)
158 *LAPIC_MMIO(reg
) = value
;
161 static lapic_ops_table_t legacy_ops
= {
173 x2apic_read(lapic_register_t reg
)
178 rdmsr(LAPIC_MSR(reg
), lo
, hi
);
183 x2apic_write(lapic_register_t reg
, uint32_t value
)
185 wrmsr(LAPIC_MSR(reg
), value
, 0);
188 static lapic_ops_table_t x2apic_ops
= {
200 boolean_t is_boot_processor
;
201 boolean_t is_lapic_enabled
;
204 /* Examine the local APIC state */
205 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
206 is_boot_processor
= (lo
& MSR_IA32_APIC_BASE_BSP
) != 0;
207 is_lapic_enabled
= (lo
& MSR_IA32_APIC_BASE_ENABLE
) != 0;
208 is_x2apic
= (lo
& MSR_IA32_APIC_BASE_EXTENDED
) != 0;
209 lapic_pbase
= (lo
& MSR_IA32_APIC_BASE_BASE
);
210 kprintf("MSR_IA32_APIC_BASE 0x%llx %s %s mode %s\n", lapic_pbase
,
211 is_lapic_enabled
? "enabled" : "disabled",
212 is_x2apic
? "extended" : "legacy",
213 is_boot_processor
? "BSP" : "AP");
214 if (!is_boot_processor
|| !is_lapic_enabled
)
215 panic("Unexpected local APIC state\n");
217 lapic_ops
= is_x2apic
? &x2apic_ops
: &legacy_ops
;
221 if ((LAPIC_READ(VERSION
)&LAPIC_VERSION_MASK
) < 0x14) {
222 panic("Local APIC version 0x%x, 0x14 or more expected\n",
223 (LAPIC_READ(VERSION
)&LAPIC_VERSION_MASK
));
226 /* Set up the lapic_id <-> cpu_number map and add this boot processor */
227 lapic_cpu_map_init();
228 lapic_cpu_map((LAPIC_READ(ID
)>>LAPIC_ID_SHIFT
)&LAPIC_ID_MASK
, 0);
229 kprintf("Boot cpu local APIC id 0x%x\n", cpu_to_lapic
[0]);
236 /* write-read register */
237 LAPIC_WRITE(ERROR_STATUS
, 0);
238 return LAPIC_READ(ERROR_STATUS
);
242 lapic_esr_clear(void)
244 LAPIC_WRITE(ERROR_STATUS
, 0);
245 LAPIC_WRITE(ERROR_STATUS
, 0);
248 static const char *DM_str
[8] = {
258 static const char *TMR_str
[] = {
270 #define BOOL(a) ((a)?' ':'!')
272 LAPIC_READ(lvt)&LAPIC_LVT_VECTOR_MASK
274 (LAPIC_READ(lvt)&LAPIC_LVT_DS_PENDING)?" SendPending" : "Idle"
276 DM_str[(LAPIC_READ(lvt)>>LAPIC_LVT_DM_SHIFT)&LAPIC_LVT_DM_MASK]
278 BOOL(LAPIC_READ(lvt)&LAPIC_LVT_MASKED)
280 (LAPIC_READ(lvt)&LAPIC_LVT_TM_LEVEL)? "Level" : "Edge"
282 (LAPIC_READ(lvt)&LAPIC_LVT_IP_PLRITY_LOW)? "Low " : "High"
284 kprintf("LAPIC %d at %p version 0x%x\n",
285 (LAPIC_READ(ID
)>>LAPIC_ID_SHIFT
)&LAPIC_ID_MASK
,
286 (void *) lapic_vbase
,
287 LAPIC_READ(VERSION
)&LAPIC_VERSION_MASK
);
288 kprintf("Priorities: Task 0x%x Arbitration 0x%x Processor 0x%x\n",
289 LAPIC_READ(TPR
)&LAPIC_TPR_MASK
,
290 LAPIC_READ(APR
)&LAPIC_APR_MASK
,
291 LAPIC_READ(PPR
)&LAPIC_PPR_MASK
);
292 kprintf("Destination Format 0x%x Logical Destination 0x%x\n",
293 LAPIC_READ(DFR
)>>LAPIC_DFR_SHIFT
,
294 LAPIC_READ(LDR
)>>LAPIC_LDR_SHIFT
);
295 kprintf("%cEnabled %cFocusChecking SV 0x%x\n",
296 BOOL(LAPIC_READ(SVR
)&LAPIC_SVR_ENABLE
),
297 BOOL(!(LAPIC_READ(SVR
)&LAPIC_SVR_FOCUS_OFF
)),
298 LAPIC_READ(SVR
) & LAPIC_SVR_MASK
);
300 if (mca_is_cmci_present())
301 kprintf("LVT_CMCI: Vector 0x%02x [%s] %s %cmasked\n",
307 kprintf("LVT_TIMER: Vector 0x%02x %s %cmasked %s\n",
311 TMR_str
[(LAPIC_READ(LVT_TIMER
) >> LAPIC_LVT_TMR_SHIFT
)
312 & LAPIC_LVT_TMR_MASK
]);
313 kprintf(" Initial Count: 0x%08x \n", LAPIC_READ(TIMER_INITIAL_COUNT
));
314 kprintf(" Current Count: 0x%08x \n", LAPIC_READ(TIMER_CURRENT_COUNT
));
315 kprintf(" Divide Config: 0x%08x \n", LAPIC_READ(TIMER_DIVIDE_CONFIG
));
316 kprintf("LVT_PERFCNT: Vector 0x%02x [%s] %s %cmasked\n",
321 kprintf("LVT_THERMAL: Vector 0x%02x [%s] %s %cmasked\n",
326 kprintf("LVT_LINT0: Vector 0x%02x [%s][%s][%s] %s %cmasked\n",
333 kprintf("LVT_LINT1: Vector 0x%02x [%s][%s][%s] %s %cmasked\n",
340 kprintf("LVT_ERROR: Vector 0x%02x %s %cmasked\n",
344 kprintf("ESR: %08x \n", lapic_esr_read());
346 for(i
=0xf; i
>=0; i
--)
347 kprintf("%x%x%x%x",i
,i
,i
,i
);
351 kprintf("%08x",LAPIC_READ_OFFSET(TMR_BASE
, i
));
355 kprintf("%08x",LAPIC_READ_OFFSET(IRR_BASE
, i
));
358 for(i
=7; i
>= 0; i
--)
359 kprintf("%08x",LAPIC_READ_OFFSET(ISR_BASE
, i
));
369 if (cpuid_features() & CPUID_FEATURE_APIC
)
372 if (cpuid_family() == 6 || cpuid_family() == 15) {
375 * There may be a local APIC which wasn't enabled by BIOS.
376 * So we try to enable it explicitly.
378 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
379 lo
&= ~MSR_IA32_APIC_BASE_BASE
;
380 lo
|= MSR_IA32_APIC_BASE_ENABLE
| LAPIC_START
;
381 lo
|= MSR_IA32_APIC_BASE_ENABLE
;
382 wrmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
385 * Re-initialize cpu features info and re-check.
388 if (cpuid_features() & CPUID_FEATURE_APIC
) {
389 printf("Local APIC discovered and enabled\n");
390 lapic_os_enabled
= TRUE
;
391 lapic_interrupt_base
= LAPIC_REDUCED_INTERRUPT_BASE
;
406 /* Shutdown if local APIC was enabled by OS */
407 if (lapic_os_enabled
== FALSE
)
410 mp_disable_preemption();
413 if (get_cpu_number() == master_cpu
) {
414 value
= LAPIC_READ(LVT_LINT0
);
415 value
|= LAPIC_LVT_MASKED
;
416 LAPIC_WRITE(LVT_LINT0
, value
);
420 LAPIC_WRITE(LVT_ERROR
, LAPIC_READ(LVT_ERROR
) | LAPIC_LVT_MASKED
);
423 LAPIC_WRITE(LVT_TIMER
, LAPIC_READ(LVT_TIMER
) | LAPIC_LVT_MASKED
);
425 /* Perfmon: masked */
426 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_READ(LVT_PERFCNT
) | LAPIC_LVT_MASKED
);
428 /* APIC software disabled */
429 LAPIC_WRITE(SVR
, LAPIC_READ(SVR
) & ~LAPIC_SVR_ENABLE
);
431 /* Bypass the APIC completely and update cpu features */
432 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
433 lo
&= ~MSR_IA32_APIC_BASE_ENABLE
;
434 wrmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
437 mp_enable_preemption();
441 lapic_configure(void)
445 if (lapic_error_time_threshold
== 0 && cpu_number() == 0) {
446 nanoseconds_to_absolutetime(NSEC_PER_SEC
>> 2, &lapic_error_time_threshold
);
447 if (!PE_parse_boot_argn("lapic_dont_panic", &lapic_dont_panic
, sizeof(lapic_dont_panic
))) {
448 lapic_dont_panic
= FALSE
;
452 /* Set flat delivery model, logical processor id */
453 LAPIC_WRITE(DFR
, LAPIC_DFR_FLAT
);
454 LAPIC_WRITE(LDR
, (get_cpu_number()) << LAPIC_LDR_SHIFT
);
459 LAPIC_WRITE(SVR
, LAPIC_VECTOR(SPURIOUS
) | LAPIC_SVR_ENABLE
);
462 if (get_cpu_number() == master_cpu
) {
463 value
= LAPIC_READ(LVT_LINT0
);
464 value
&= ~LAPIC_LVT_MASKED
;
465 value
|= LAPIC_LVT_DM_EXTINT
;
466 LAPIC_WRITE(LVT_LINT0
, value
);
469 /* Timer: unmasked, one-shot */
470 LAPIC_WRITE(LVT_TIMER
, LAPIC_VECTOR(TIMER
));
472 /* Perfmon: unmasked */
473 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_VECTOR(PERFCNT
));
475 /* Thermal: unmasked */
476 LAPIC_WRITE(LVT_THERMAL
, LAPIC_VECTOR(THERMAL
));
479 /* CMCI, if available */
480 if (mca_is_cmci_present())
481 LAPIC_WRITE(LVT_CMCI
, LAPIC_VECTOR(CMCI
));
484 if (((cpu_number() == master_cpu
) && lapic_errors_masked
== FALSE
) ||
485 (cpu_number() != master_cpu
)) {
487 LAPIC_WRITE(LVT_ERROR
, LAPIC_VECTOR(ERROR
));
493 boolean_t interrupt_unmasked
,
494 lapic_timer_mode_t mode
,
495 lapic_timer_divide_t divisor
,
496 lapic_timer_count_t initial_count
)
498 uint32_t timer_vector
;
500 mp_disable_preemption();
501 timer_vector
= LAPIC_READ(LVT_TIMER
);
502 timer_vector
&= ~(LAPIC_LVT_MASKED
|LAPIC_LVT_PERIODIC
);;
503 timer_vector
|= interrupt_unmasked
? 0 : LAPIC_LVT_MASKED
;
504 timer_vector
|= (mode
== periodic
) ? LAPIC_LVT_PERIODIC
: 0;
505 LAPIC_WRITE(LVT_TIMER
, timer_vector
);
506 LAPIC_WRITE(TIMER_DIVIDE_CONFIG
, divisor
);
507 LAPIC_WRITE(TIMER_INITIAL_COUNT
, initial_count
);
508 mp_enable_preemption();
513 boolean_t interrupt_unmasked
,
514 lapic_timer_mode_t mode
,
515 lapic_timer_divide_t divisor
)
517 uint32_t timer_vector
;
519 mp_disable_preemption();
520 timer_vector
= LAPIC_READ(LVT_TIMER
);
521 timer_vector
&= ~(LAPIC_LVT_MASKED
|
523 LAPIC_LVT_TSC_DEADLINE
);
524 timer_vector
|= interrupt_unmasked
? 0 : LAPIC_LVT_MASKED
;
525 timer_vector
|= (mode
== periodic
) ? LAPIC_LVT_PERIODIC
: 0;
526 LAPIC_WRITE(LVT_TIMER
, timer_vector
);
527 LAPIC_WRITE(TIMER_DIVIDE_CONFIG
, divisor
);
528 mp_enable_preemption();
532 * Configure TSC-deadline timer mode. The lapic interrupt is always unmasked.
535 lapic_config_tsc_deadline_timer(void)
537 uint32_t timer_vector
;
539 DBG("lapic_config_tsc_deadline_timer()\n");
540 mp_disable_preemption();
541 timer_vector
= LAPIC_READ(LVT_TIMER
);
542 timer_vector
&= ~(LAPIC_LVT_MASKED
|
544 timer_vector
|= LAPIC_LVT_TSC_DEADLINE
;
545 LAPIC_WRITE(LVT_TIMER
, timer_vector
);
547 /* Serialize writes per Intel OSWG */
549 lapic_set_tsc_deadline_timer(rdtsc64() + (1ULL<<32));
550 } while (lapic_get_tsc_deadline_timer() == 0);
551 lapic_set_tsc_deadline_timer(0);
553 mp_enable_preemption();
554 DBG("lapic_config_tsc_deadline_timer() done\n");
558 lapic_set_timer_fast(
559 lapic_timer_count_t initial_count
)
561 LAPIC_WRITE(LVT_TIMER
, LAPIC_READ(LVT_TIMER
) & ~LAPIC_LVT_MASKED
);
562 LAPIC_WRITE(TIMER_INITIAL_COUNT
, initial_count
);
566 lapic_set_tsc_deadline_timer(uint64_t deadline
)
568 /* Don't bother disarming: wrmsr64(MSR_IA32_TSC_DEADLINE, 0); */
569 wrmsr64(MSR_IA32_TSC_DEADLINE
, deadline
);
573 lapic_get_tsc_deadline_timer(void)
575 return rdmsr64(MSR_IA32_TSC_DEADLINE
);
580 lapic_timer_mode_t
*mode
,
581 lapic_timer_divide_t
*divisor
,
582 lapic_timer_count_t
*initial_count
,
583 lapic_timer_count_t
*current_count
)
585 mp_disable_preemption();
587 *mode
= (LAPIC_READ(LVT_TIMER
) & LAPIC_LVT_PERIODIC
) ?
590 *divisor
= LAPIC_READ(TIMER_DIVIDE_CONFIG
) & LAPIC_TIMER_DIVIDE_MASK
;
592 *initial_count
= LAPIC_READ(TIMER_INITIAL_COUNT
);
594 *current_count
= LAPIC_READ(TIMER_CURRENT_COUNT
);
595 mp_enable_preemption();
599 _lapic_end_of_interrupt(void)
605 lapic_end_of_interrupt(void)
607 _lapic_end_of_interrupt();
610 void lapic_unmask_perfcnt_interrupt(void) {
611 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_VECTOR(PERFCNT
));
614 void lapic_set_perfcnt_interrupt_mask(boolean_t mask
) {
615 uint32_t m
= (mask
? LAPIC_LVT_MASKED
: 0);
616 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_VECTOR(PERFCNT
) | m
);
620 lapic_set_intr_func(int vector
, i386_intr_func_t func
)
622 if (vector
> lapic_interrupt_base
)
623 vector
-= lapic_interrupt_base
;
626 case LAPIC_NMI_INTERRUPT
:
627 case LAPIC_INTERPROCESSOR_INTERRUPT
:
628 case LAPIC_TIMER_INTERRUPT
:
629 case LAPIC_THERMAL_INTERRUPT
:
630 case LAPIC_PERFCNT_INTERRUPT
:
631 case LAPIC_CMCI_INTERRUPT
:
632 case LAPIC_PM_INTERRUPT
:
633 lapic_intr_func
[vector
] = func
;
636 panic("lapic_set_intr_func(%d,%p) invalid vector\n",
641 void lapic_set_pmi_func(i386_intr_func_t func
) {
642 lapic_set_intr_func(LAPIC_VECTOR(PERFCNT
), func
);
646 lapic_interrupt(int interrupt_num
, x86_saved_state_t
*state
)
651 interrupt_num
-= lapic_interrupt_base
;
652 if (interrupt_num
< 0) {
653 if (interrupt_num
== (LAPIC_NMI_INTERRUPT
- lapic_interrupt_base
) &&
654 lapic_intr_func
[LAPIC_NMI_INTERRUPT
] != NULL
) {
655 retval
= (*lapic_intr_func
[LAPIC_NMI_INTERRUPT
])(state
);
662 switch(interrupt_num
) {
663 case LAPIC_TIMER_INTERRUPT
:
664 case LAPIC_THERMAL_INTERRUPT
:
665 case LAPIC_INTERPROCESSOR_INTERRUPT
:
666 case LAPIC_PM_INTERRUPT
:
667 if (lapic_intr_func
[interrupt_num
] != NULL
)
668 (void) (*lapic_intr_func
[interrupt_num
])(state
);
669 _lapic_end_of_interrupt();
672 case LAPIC_PERFCNT_INTERRUPT
:
673 /* If a function has been registered, invoke it. Otherwise,
676 if (lapic_intr_func
[interrupt_num
] != NULL
) {
677 (void) (*lapic_intr_func
[interrupt_num
])(state
);
678 /* Unmask the interrupt since we don't expect legacy users
679 * to be responsible for it.
681 lapic_unmask_perfcnt_interrupt();
682 _lapic_end_of_interrupt();
686 case LAPIC_CMCI_INTERRUPT
:
687 if (lapic_intr_func
[interrupt_num
] != NULL
)
688 (void) (*lapic_intr_func
[interrupt_num
])(state
);
689 /* return 0 for plaform expert to handle */
691 case LAPIC_ERROR_INTERRUPT
:
692 /* We treat error interrupts on APs as fatal.
693 * The current interrupt steering scheme directs most
694 * external interrupts to the BSP (HPET interrupts being
695 * a notable exception); hence, such an error
696 * on an AP may signify LVT corruption (with "may" being
697 * the operative word). On the BSP, we adopt a more
698 * lenient approach, in the interests of enhancing
699 * debuggability and reducing fragility.
700 * If "lapic_error_count_threshold" error interrupts
701 * occur within "lapic_error_time_threshold" absolute
702 * time units, we mask the error vector and log. The
703 * error interrupts themselves are likely
704 * side effects of issues which are beyond the purview of
705 * the local APIC interrupt handler, however. The Error
706 * Status Register value (the illegal destination
707 * vector code is one observed in practice) indicates
708 * the immediate cause of the error.
710 esr
= lapic_esr_read();
713 if ((debug_boot_arg
&& (lapic_dont_panic
== FALSE
)) ||
714 cpu_number() != master_cpu
) {
715 panic("Local APIC error, ESR: %d\n", esr
);
718 if (cpu_number() == master_cpu
) {
719 uint64_t abstime
= mach_absolute_time();
720 if ((abstime
- lapic_last_master_error
) < lapic_error_time_threshold
) {
721 if (lapic_master_error_count
++ > lapic_error_count_threshold
) {
722 lapic_errors_masked
= TRUE
;
723 LAPIC_WRITE(LVT_ERROR
, LAPIC_READ(LVT_ERROR
) | LAPIC_LVT_MASKED
);
724 printf("Local APIC: errors masked\n");
728 lapic_last_master_error
= abstime
;
729 lapic_master_error_count
= 0;
731 printf("Local APIC error on master CPU, ESR: %d, error count this run: %d\n", esr
, lapic_master_error_count
);
734 _lapic_end_of_interrupt();
737 case LAPIC_SPURIOUS_INTERRUPT
:
739 /* No EOI required here */
742 case LAPIC_PMC_SW_INTERRUPT
:
746 ml_get_csw_threads(&old
, &new);
748 if (pmc_context_switch(old
, new) == TRUE
) {
750 /* No EOI required for SWI */
752 #endif /* CONFIG_COUNTERS */
761 lapic_smm_restore(void)
765 if (lapic_os_enabled
== FALSE
)
768 state
= ml_set_interrupts_enabled(FALSE
);
770 if (LAPIC_ISR_IS_SET(LAPIC_REDUCED_INTERRUPT_BASE
, TIMER
)) {
772 * Bogus SMI handler enables interrupts but does not know about
773 * local APIC interrupt sources. When APIC timer counts down to
774 * zero while in SMM, local APIC will end up waiting for an EOI
775 * but no interrupt was delivered to the OS.
777 _lapic_end_of_interrupt();
780 * timer is one-shot, trigger another quick countdown to trigger
781 * another timer interrupt.
783 if (LAPIC_READ(TIMER_CURRENT_COUNT
) == 0) {
784 LAPIC_WRITE(TIMER_INITIAL_COUNT
, 1);
787 kprintf("lapic_smm_restore\n");
790 ml_set_interrupts_enabled(state
);
794 lapic_send_ipi(int cpu
, int vector
)
798 if (vector
< lapic_interrupt_base
)
799 vector
+= lapic_interrupt_base
;
801 state
= ml_set_interrupts_enabled(FALSE
);
803 /* Wait for pending outgoing send to complete */
804 while (LAPIC_READ(ICR
) & LAPIC_ICR_DS_PENDING
) {
808 LAPIC_WRITE(ICRD
, cpu_to_lapic
[cpu
] << LAPIC_ICRD_DEST_SHIFT
);
809 LAPIC_WRITE(ICR
, vector
| LAPIC_ICR_DM_FIXED
);
811 (void) ml_set_interrupts_enabled(state
);
815 * The following interfaces are privately exported to AICPM.
819 lapic_is_interrupt_pending(void)
823 for (i
= 0; i
< 8; i
+= 1) {
824 if ((LAPIC_READ_OFFSET(IRR_BASE
, i
) != 0) ||
825 (LAPIC_READ_OFFSET(ISR_BASE
, i
) != 0))
833 lapic_is_interrupting(uint8_t vector
)
841 bit
= 1 << (vector
% 32);
843 irr
= LAPIC_READ_OFFSET(IRR_BASE
, i
);
844 isr
= LAPIC_READ_OFFSET(ISR_BASE
, i
);
846 if ((irr
| isr
) & bit
)
853 lapic_interrupt_counts(uint64_t intrs
[256])
864 for (i
= 0; i
< 8; i
+= 1) {
865 irr
= LAPIC_READ_OFFSET(IRR_BASE
, i
);
866 isr
= LAPIC_READ_OFFSET(ISR_BASE
, i
);
868 if ((isr
| irr
) == 0)
871 for (j
= (i
== 0) ? 16 : 0; j
< 32; j
+= 1) {
873 if ((isr
| irr
) & (1 << j
))
880 lapic_disable_timer(void)
885 * If we're in deadline timer mode,
886 * simply clear the deadline timer, otherwise
887 * mask the timer interrupt and clear the countdown.
889 lvt_timer
= LAPIC_READ(LVT_TIMER
);
890 if (lvt_timer
& LAPIC_LVT_TSC_DEADLINE
) {
891 wrmsr64(MSR_IA32_TSC_DEADLINE
, 0);
893 LAPIC_WRITE(LVT_TIMER
, lvt_timer
| LAPIC_LVT_MASKED
);
894 LAPIC_WRITE(TIMER_INITIAL_COUNT
, 0);
895 lvt_timer
= LAPIC_READ(LVT_TIMER
);