2 * Copyright (c) 2008-2009 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/mtrr.h>
52 #include <i386/postcode.h>
53 #include <i386/cpu_threads.h>
54 #include <i386/machine_routines.h>
56 #include <i386/machine_check.h>
64 #include <machine/db_machdep.h>
67 #include <sys/kdebug.h>
70 #define PAUSE delay(1000000)
71 #define DBG(x...) kprintf(x)
77 /* Initialize lapic_id so cpu_number() works on non SMP systems */
78 unsigned long lapic_id_initdata
= 0;
79 unsigned long lapic_id
= (unsigned long)&lapic_id_initdata
;
80 vm_offset_t lapic_start
;
82 static i386_intr_func_t lapic_intr_func
[LAPIC_FUNC_TABLE_SIZE
];
84 /* TRUE if local APIC was enabled by the OS not by the BIOS */
85 static boolean_t lapic_os_enabled
= FALSE
;
87 static boolean_t lapic_errors_masked
= FALSE
;
88 static uint64_t lapic_last_master_error
= 0;
89 static uint64_t lapic_error_time_threshold
= 0;
90 static unsigned lapic_master_error_count
= 0;
91 static unsigned lapic_error_count_threshold
= 5;
92 static boolean_t lapic_dont_panic
= FALSE
;
94 /* Base vector for local APIC interrupt sources */
95 int lapic_interrupt_base
= LAPIC_DEFAULT_INTERRUPT_BASE
;
97 #define MAX_LAPICIDS (LAPIC_ID_MAX+1)
98 int lapic_to_cpu
[MAX_LAPICIDS
];
99 int cpu_to_lapic
[MAX_CPUS
];
102 lapic_cpu_map_init(void)
106 for (i
= 0; i
< MAX_CPUS
; i
++)
107 cpu_to_lapic
[i
] = -1;
108 for (i
= 0; i
< MAX_LAPICIDS
; i
++)
109 lapic_to_cpu
[i
] = -1;
113 lapic_cpu_map(int apic_id
, int cpu
)
115 assert(apic_id
< MAX_LAPICIDS
);
116 assert(cpu
< MAX_CPUS
);
117 cpu_to_lapic
[cpu
] = apic_id
;
118 lapic_to_cpu
[apic_id
] = cpu
;
122 * Retrieve the local apic ID a cpu.
124 * Returns the local apic ID for the given processor.
125 * If the processor does not exist or apic not configured, returns -1.
129 ml_get_apicid(uint32_t cpu
)
131 if(cpu
>= (uint32_t)MAX_CPUS
)
132 return 0xFFFFFFFF; /* Return -1 if cpu too big */
134 /* Return the apic ID (or -1 if not configured) */
135 return (uint32_t)cpu_to_lapic
[cpu
];
140 ml_get_cpuid(uint32_t lapic_index
)
142 if(lapic_index
>= (uint32_t)MAX_LAPICIDS
)
143 return 0xFFFFFFFF; /* Return -1 if cpu too big */
145 /* Return the cpu ID (or -1 if not configured) */
146 return (uint32_t)lapic_to_cpu
[lapic_index
];
153 lapic_cpu_map_dump(void)
157 for (i
= 0; i
< MAX_CPUS
; i
++) {
158 if (cpu_to_lapic
[i
] == -1)
160 kprintf("cpu_to_lapic[%d]: %d\n",
163 for (i
= 0; i
< MAX_LAPICIDS
; i
++) {
164 if (lapic_to_cpu
[i
] == -1)
166 kprintf("lapic_to_cpu[%d]: %d\n",
170 #endif /* MP_DEBUG */
176 vm_map_entry_t entry
;
179 boolean_t is_boot_processor
;
180 boolean_t is_lapic_enabled
;
181 vm_offset_t lapic_base
;
183 /* Examine the local APIC state */
184 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
185 is_boot_processor
= (lo
& MSR_IA32_APIC_BASE_BSP
) != 0;
186 is_lapic_enabled
= (lo
& MSR_IA32_APIC_BASE_ENABLE
) != 0;
187 lapic_base
= (lo
& MSR_IA32_APIC_BASE_BASE
);
188 kprintf("MSR_IA32_APIC_BASE %p %s %s\n", (void *) lapic_base
,
189 is_lapic_enabled
? "enabled" : "disabled",
190 is_boot_processor
? "BSP" : "AP");
191 if (!is_boot_processor
|| !is_lapic_enabled
)
192 panic("Unexpected local APIC state\n");
194 /* Establish a map to the local apic */
195 lapic_start
= (vm_offset_t
)vm_map_min(kernel_map
);
196 result
= vm_map_find_space(kernel_map
,
197 (vm_map_address_t
*) &lapic_start
,
198 round_page(LAPIC_SIZE
), 0,
199 VM_MAKE_TAG(VM_MEMORY_IOKIT
), &entry
);
200 if (result
!= KERN_SUCCESS
) {
201 panic("smp_init: vm_map_find_entry FAILED (err=%d)", result
);
203 vm_map_unlock(kernel_map
);
204 /* Map in the local APIC non-cacheable, as recommended by Intel
205 * in section 8.4.1 of the "System Programming Guide".
207 pmap_enter(pmap_kernel(),
209 (ppnum_t
) i386_btop(lapic_base
),
210 VM_PROT_READ
|VM_PROT_WRITE
,
213 lapic_id
= (unsigned long)(lapic_start
+ LAPIC_ID
);
215 if ((LAPIC_READ(VERSION
)&LAPIC_VERSION_MASK
) < 0x14) {
216 panic("Local APIC version 0x%x, 0x14 or more expected\n",
217 (LAPIC_READ(VERSION
)&LAPIC_VERSION_MASK
));
220 /* Set up the lapic_id <-> cpu_number map and add this boot processor */
221 lapic_cpu_map_init();
222 lapic_cpu_map((LAPIC_READ(ID
)>>LAPIC_ID_SHIFT
)&LAPIC_ID_MASK
, 0);
223 kprintf("Boot cpu local APIC id 0x%x\n", cpu_to_lapic
[0]);
230 /* write-read register */
231 LAPIC_WRITE(ERROR_STATUS
, 0);
232 return LAPIC_READ(ERROR_STATUS
);
236 lapic_esr_clear(void)
238 LAPIC_WRITE(ERROR_STATUS
, 0);
239 LAPIC_WRITE(ERROR_STATUS
, 0);
242 static const char *DM_str
[8] = {
257 #define BOOL(a) ((a)?' ':'!')
259 LAPIC_READ(lvt)&LAPIC_LVT_VECTOR_MASK
261 (LAPIC_READ(lvt)&LAPIC_LVT_DS_PENDING)?" SendPending" : "Idle"
263 DM_str[(LAPIC_READ(lvt)>>LAPIC_LVT_DM_SHIFT)&LAPIC_LVT_DM_MASK]
265 BOOL(LAPIC_READ(lvt)&LAPIC_LVT_MASKED)
267 (LAPIC_READ(lvt)&LAPIC_LVT_TM_LEVEL)? "Level" : "Edge"
269 (LAPIC_READ(lvt)&LAPIC_LVT_IP_PLRITY_LOW)? "Low " : "High"
271 kprintf("LAPIC %d at %p version 0x%x\n",
272 (LAPIC_READ(ID
)>>LAPIC_ID_SHIFT
)&LAPIC_ID_MASK
,
273 (void *) lapic_start
,
274 LAPIC_READ(VERSION
)&LAPIC_VERSION_MASK
);
275 kprintf("Priorities: Task 0x%x Arbitration 0x%x Processor 0x%x\n",
276 LAPIC_READ(TPR
)&LAPIC_TPR_MASK
,
277 LAPIC_READ(APR
)&LAPIC_APR_MASK
,
278 LAPIC_READ(PPR
)&LAPIC_PPR_MASK
);
279 kprintf("Destination Format 0x%x Logical Destination 0x%x\n",
280 LAPIC_READ(DFR
)>>LAPIC_DFR_SHIFT
,
281 LAPIC_READ(LDR
)>>LAPIC_LDR_SHIFT
);
282 kprintf("%cEnabled %cFocusChecking SV 0x%x\n",
283 BOOL(LAPIC_READ(SVR
)&LAPIC_SVR_ENABLE
),
284 BOOL(!(LAPIC_READ(SVR
)&LAPIC_SVR_FOCUS_OFF
)),
285 LAPIC_READ(SVR
) & LAPIC_SVR_MASK
);
287 if (mca_is_cmci_present())
288 kprintf("LVT_CMCI: Vector 0x%02x [%s] %s %cmasked\n",
294 kprintf("LVT_TIMER: Vector 0x%02x %s %cmasked %s\n",
298 (LAPIC_READ(LVT_TIMER
)&LAPIC_LVT_PERIODIC
)?"Periodic":"OneShot");
299 kprintf(" Initial Count: 0x%08x \n", LAPIC_READ(TIMER_INITIAL_COUNT
));
300 kprintf(" Current Count: 0x%08x \n", LAPIC_READ(TIMER_CURRENT_COUNT
));
301 kprintf(" Divide Config: 0x%08x \n", LAPIC_READ(TIMER_DIVIDE_CONFIG
));
302 kprintf("LVT_PERFCNT: Vector 0x%02x [%s] %s %cmasked\n",
307 kprintf("LVT_THERMAL: Vector 0x%02x [%s] %s %cmasked\n",
312 kprintf("LVT_LINT0: Vector 0x%02x [%s][%s][%s] %s %cmasked\n",
319 kprintf("LVT_LINT1: Vector 0x%02x [%s][%s][%s] %s %cmasked\n",
326 kprintf("LVT_ERROR: Vector 0x%02x %s %cmasked\n",
330 kprintf("ESR: %08x \n", lapic_esr_read());
332 for(i
=0xf; i
>=0; i
--)
333 kprintf("%x%x%x%x",i
,i
,i
,i
);
337 kprintf("%08x",LAPIC_READ_OFFSET(TMR_BASE
, i
*0x10));
341 kprintf("%08x",LAPIC_READ_OFFSET(IRR_BASE
, i
*0x10));
344 for(i
=7; i
>= 0; i
--)
345 kprintf("%08x",LAPIC_READ_OFFSET(ISR_BASE
, i
*0x10));
356 db_apic(__unused db_expr_t addr
,
357 __unused
int have_addr
,
358 __unused db_expr_t count
,
359 __unused
char *modif
)
375 if (cpuid_features() & CPUID_FEATURE_APIC
)
378 if (cpuid_family() == 6 || cpuid_family() == 15) {
381 * There may be a local APIC which wasn't enabled by BIOS.
382 * So we try to enable it explicitly.
384 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
385 lo
&= ~MSR_IA32_APIC_BASE_BASE
;
386 lo
|= MSR_IA32_APIC_BASE_ENABLE
| LAPIC_START
;
387 lo
|= MSR_IA32_APIC_BASE_ENABLE
;
388 wrmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
391 * Re-initialize cpu features info and re-check.
394 if (cpuid_features() & CPUID_FEATURE_APIC
) {
395 printf("Local APIC discovered and enabled\n");
396 lapic_os_enabled
= TRUE
;
397 lapic_interrupt_base
= LAPIC_REDUCED_INTERRUPT_BASE
;
412 /* Shutdown if local APIC was enabled by OS */
413 if (lapic_os_enabled
== FALSE
)
416 mp_disable_preemption();
419 if (get_cpu_number() == master_cpu
) {
420 value
= LAPIC_READ(LVT_LINT0
);
421 value
|= LAPIC_LVT_MASKED
;
422 LAPIC_WRITE(LVT_LINT0
, value
);
426 LAPIC_WRITE(LVT_ERROR
, LAPIC_READ(LVT_ERROR
) | LAPIC_LVT_MASKED
);
429 LAPIC_WRITE(LVT_TIMER
, LAPIC_READ(LVT_TIMER
) | LAPIC_LVT_MASKED
);
431 /* Perfmon: masked */
432 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_READ(LVT_PERFCNT
) | LAPIC_LVT_MASKED
);
434 /* APIC software disabled */
435 LAPIC_WRITE(SVR
, LAPIC_READ(SVR
) & ~LAPIC_SVR_ENABLE
);
437 /* Bypass the APIC completely and update cpu features */
438 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
439 lo
&= ~MSR_IA32_APIC_BASE_ENABLE
;
440 wrmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
443 mp_enable_preemption();
447 lapic_configure(void)
451 if (lapic_error_time_threshold
== 0 && cpu_number() == 0) {
452 nanoseconds_to_absolutetime(NSEC_PER_SEC
>> 2, &lapic_error_time_threshold
);
453 if (!PE_parse_boot_argn("lapic_dont_panic", &lapic_dont_panic
, sizeof(lapic_dont_panic
))) {
454 lapic_dont_panic
= FALSE
;
458 /* Set flat delivery model, logical processor id */
459 LAPIC_WRITE(DFR
, LAPIC_DFR_FLAT
);
460 LAPIC_WRITE(LDR
, (get_cpu_number()) << LAPIC_LDR_SHIFT
);
465 LAPIC_WRITE(SVR
, LAPIC_VECTOR(SPURIOUS
) | LAPIC_SVR_ENABLE
);
468 if (get_cpu_number() == master_cpu
) {
469 value
= LAPIC_READ(LVT_LINT0
);
470 value
&= ~LAPIC_LVT_MASKED
;
471 value
|= LAPIC_LVT_DM_EXTINT
;
472 LAPIC_WRITE(LVT_LINT0
, value
);
475 /* Timer: unmasked, one-shot */
476 LAPIC_WRITE(LVT_TIMER
, LAPIC_VECTOR(TIMER
));
478 /* Perfmon: unmasked */
479 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_VECTOR(PERFCNT
));
481 /* Thermal: unmasked */
482 LAPIC_WRITE(LVT_THERMAL
, LAPIC_VECTOR(THERMAL
));
485 /* CMCI, if available */
486 if (mca_is_cmci_present())
487 LAPIC_WRITE(LVT_CMCI
, LAPIC_VECTOR(CMCI
));
490 if (((cpu_number() == master_cpu
) && lapic_errors_masked
== FALSE
) ||
491 (cpu_number() != master_cpu
)) {
493 LAPIC_WRITE(LVT_ERROR
, LAPIC_VECTOR(ERROR
));
499 boolean_t interrupt_unmasked
,
500 lapic_timer_mode_t mode
,
501 lapic_timer_divide_t divisor
,
502 lapic_timer_count_t initial_count
)
505 uint32_t timer_vector
;
507 state
= ml_set_interrupts_enabled(FALSE
);
508 timer_vector
= LAPIC_READ(LVT_TIMER
);
509 timer_vector
&= ~(LAPIC_LVT_MASKED
|LAPIC_LVT_PERIODIC
);;
510 timer_vector
|= interrupt_unmasked
? 0 : LAPIC_LVT_MASKED
;
511 timer_vector
|= (mode
== periodic
) ? LAPIC_LVT_PERIODIC
: 0;
512 LAPIC_WRITE(LVT_TIMER
, timer_vector
);
513 LAPIC_WRITE(TIMER_DIVIDE_CONFIG
, divisor
);
514 LAPIC_WRITE(TIMER_INITIAL_COUNT
, initial_count
);
515 ml_set_interrupts_enabled(state
);
520 lapic_timer_mode_t
*mode
,
521 lapic_timer_divide_t
*divisor
,
522 lapic_timer_count_t
*initial_count
,
523 lapic_timer_count_t
*current_count
)
527 state
= ml_set_interrupts_enabled(FALSE
);
529 *mode
= (LAPIC_READ(LVT_TIMER
) & LAPIC_LVT_PERIODIC
) ?
532 *divisor
= LAPIC_READ(TIMER_DIVIDE_CONFIG
) & LAPIC_TIMER_DIVIDE_MASK
;
534 *initial_count
= LAPIC_READ(TIMER_INITIAL_COUNT
);
536 *current_count
= LAPIC_READ(TIMER_CURRENT_COUNT
);
537 ml_set_interrupts_enabled(state
);
541 _lapic_end_of_interrupt(void)
547 lapic_end_of_interrupt(void)
549 _lapic_end_of_interrupt();
552 void lapic_unmask_perfcnt_interrupt(void) {
553 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_VECTOR(PERFCNT
));
557 lapic_set_intr_func(int vector
, i386_intr_func_t func
)
559 if (vector
> lapic_interrupt_base
)
560 vector
-= lapic_interrupt_base
;
563 case LAPIC_NMI_INTERRUPT
:
564 case LAPIC_INTERPROCESSOR_INTERRUPT
:
565 case LAPIC_TIMER_INTERRUPT
:
566 case LAPIC_THERMAL_INTERRUPT
:
567 case LAPIC_PERFCNT_INTERRUPT
:
568 case LAPIC_CMCI_INTERRUPT
:
569 case LAPIC_PM_INTERRUPT
:
570 lapic_intr_func
[vector
] = func
;
573 panic("lapic_set_intr_func(%d,%p) invalid vector\n",
579 lapic_interrupt(int interrupt_num
, x86_saved_state_t
*state
)
584 interrupt_num
-= lapic_interrupt_base
;
585 if (interrupt_num
< 0) {
586 if (interrupt_num
== (LAPIC_NMI_INTERRUPT
- lapic_interrupt_base
) &&
587 lapic_intr_func
[LAPIC_NMI_INTERRUPT
] != NULL
) {
588 retval
= (*lapic_intr_func
[LAPIC_NMI_INTERRUPT
])(state
);
589 _lapic_end_of_interrupt();
596 switch(interrupt_num
) {
597 case LAPIC_TIMER_INTERRUPT
:
598 case LAPIC_THERMAL_INTERRUPT
:
599 case LAPIC_INTERPROCESSOR_INTERRUPT
:
600 case LAPIC_PM_INTERRUPT
:
601 if (lapic_intr_func
[interrupt_num
] != NULL
)
602 (void) (*lapic_intr_func
[interrupt_num
])(state
);
603 _lapic_end_of_interrupt();
606 case LAPIC_PERFCNT_INTERRUPT
:
607 /* If a function has been registered, invoke it. Otherwise,
610 if (lapic_intr_func
[interrupt_num
] != NULL
) {
611 (void) (*lapic_intr_func
[interrupt_num
])(state
);
612 /* Unmask the interrupt since we don't expect legacy users
613 * to be responsible for it.
615 lapic_unmask_perfcnt_interrupt();
616 _lapic_end_of_interrupt();
620 case LAPIC_CMCI_INTERRUPT
:
621 if (lapic_intr_func
[interrupt_num
] != NULL
)
622 (void) (*lapic_intr_func
[interrupt_num
])(state
);
623 /* return 0 for plaform expert to handle */
625 case LAPIC_ERROR_INTERRUPT
:
626 /* We treat error interrupts on APs as fatal.
627 * The current interrupt steering scheme directs most
628 * external interrupts to the BSP (HPET interrupts being
629 * a notable exception); hence, such an error
630 * on an AP may signify LVT corruption (with "may" being
631 * the operative word). On the BSP, we adopt a more
632 * lenient approach, in the interests of enhancing
633 * debuggability and reducing fragility.
634 * If "lapic_error_count_threshold" error interrupts
635 * occur within "lapic_error_time_threshold" absolute
636 * time units, we mask the error vector and log. The
637 * error interrupts themselves are likely
638 * side effects of issues which are beyond the purview of
639 * the local APIC interrupt handler, however. The Error
640 * Status Register value (the illegal destination
641 * vector code is one observed in practice) indicates
642 * the immediate cause of the error.
644 esr
= lapic_esr_read();
647 if ((debug_boot_arg
&& (lapic_dont_panic
== FALSE
)) ||
648 cpu_number() != master_cpu
) {
649 panic("Local APIC error, ESR: %d\n", esr
);
652 if (cpu_number() == master_cpu
) {
653 uint64_t abstime
= mach_absolute_time();
654 if ((abstime
- lapic_last_master_error
) < lapic_error_time_threshold
) {
655 if (lapic_master_error_count
++ > lapic_error_count_threshold
) {
656 lapic_errors_masked
= TRUE
;
657 LAPIC_WRITE(LVT_ERROR
, LAPIC_READ(LVT_ERROR
) | LAPIC_LVT_MASKED
);
658 printf("Local APIC: errors masked\n");
662 lapic_last_master_error
= abstime
;
663 lapic_master_error_count
= 0;
665 printf("Local APIC error on master CPU, ESR: %d, error count this run: %d\n", esr
, lapic_master_error_count
);
668 _lapic_end_of_interrupt();
671 case LAPIC_SPURIOUS_INTERRUPT
:
673 /* No EOI required here */
676 case LAPIC_PMC_SW_INTERRUPT
:
680 ml_get_csw_threads(&old
, &new);
682 if (pmc_context_switch(old
, new) == TRUE
) {
684 /* No EOI required for SWI */
686 #endif /* CONFIG_COUNTERS */
695 lapic_smm_restore(void)
699 if (lapic_os_enabled
== FALSE
)
702 state
= ml_set_interrupts_enabled(FALSE
);
704 if (LAPIC_ISR_IS_SET(LAPIC_REDUCED_INTERRUPT_BASE
, TIMER
)) {
706 * Bogus SMI handler enables interrupts but does not know about
707 * local APIC interrupt sources. When APIC timer counts down to
708 * zero while in SMM, local APIC will end up waiting for an EOI
709 * but no interrupt was delivered to the OS.
711 _lapic_end_of_interrupt();
714 * timer is one-shot, trigger another quick countdown to trigger
715 * another timer interrupt.
717 if (LAPIC_READ(TIMER_CURRENT_COUNT
) == 0) {
718 LAPIC_WRITE(TIMER_INITIAL_COUNT
, 1);
721 kprintf("lapic_smm_restore\n");
724 ml_set_interrupts_enabled(state
);
728 lapic_send_ipi(int cpu
, int vector
)
732 if (vector
< lapic_interrupt_base
)
733 vector
+= lapic_interrupt_base
;
735 state
= ml_set_interrupts_enabled(FALSE
);
737 /* Wait for pending outgoing send to complete */
738 while (LAPIC_READ(ICR
) & LAPIC_ICR_DS_PENDING
) {
742 LAPIC_WRITE(ICRD
, cpu_to_lapic
[cpu
] << LAPIC_ICRD_DEST_SHIFT
);
743 LAPIC_WRITE(ICR
, vector
| LAPIC_ICR_DM_FIXED
);
745 (void) ml_set_interrupts_enabled(state
);