2 * Copyright (c) 2008 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>
41 #include <vm/vm_map.h>
42 #include <vm/vm_kern.h>
44 #include <i386/lapic.h>
45 #include <i386/cpuid.h>
46 #include <i386/proc_reg.h>
47 #include <i386/machine_cpu.h>
48 #include <i386/misc_protos.h>
50 #include <i386/mtrr.h>
51 #include <i386/postcode.h>
52 #include <i386/cpu_threads.h>
53 #include <i386/trap.h>
54 #include <i386/machine_routines.h>
55 #include <i386/machine_check.h>
58 #include <machine/db_machdep.h>
61 #include <sys/kdebug.h>
64 #define PAUSE delay(1000000)
65 #define DBG(x...) kprintf(x)
71 /* Initialize lapic_id so cpu_number() works on non SMP systems */
72 unsigned long lapic_id_initdata
= 0;
73 unsigned long lapic_id
= (unsigned long)&lapic_id_initdata
;
74 vm_offset_t lapic_start
;
76 static i386_intr_func_t lapic_intr_func
[LAPIC_FUNC_TABLE_SIZE
];
78 /* TRUE if local APIC was enabled by the OS not by the BIOS */
79 static boolean_t lapic_os_enabled
= FALSE
;
81 static boolean_t lapic_errors_masked
= FALSE
;
82 static uint64_t lapic_last_master_error
= 0;
83 static uint64_t lapic_error_time_threshold
= 0;
84 static unsigned lapic_master_error_count
= 0;
85 static unsigned lapic_error_count_threshold
= 5;
86 static boolean_t lapic_dont_panic
= FALSE
;
88 extern int debug_boot_arg
;
90 /* Base vector for local APIC interrupt sources */
91 int lapic_interrupt_base
= LAPIC_DEFAULT_INTERRUPT_BASE
;
93 int lapic_to_cpu
[MAX_CPUS
];
94 int cpu_to_lapic
[MAX_CPUS
];
97 lapic_cpu_map_init(void)
101 for (i
= 0; i
< MAX_CPUS
; i
++) {
102 lapic_to_cpu
[i
] = -1;
103 cpu_to_lapic
[i
] = -1;
108 lapic_cpu_map(int apic_id
, int cpu
)
110 cpu_to_lapic
[cpu
] = apic_id
;
111 lapic_to_cpu
[apic_id
] = cpu
;
115 * Retrieve the local apic ID a cpu.
117 * Returns the local apic ID for the given processor.
118 * If the processor does not exist or apic not configured, returns -1.
122 ml_get_apicid(uint32_t cpu
)
124 if(cpu
>= (uint32_t)MAX_CPUS
)
125 return 0xFFFFFFFF; /* Return -1 if cpu too big */
127 /* Return the apic ID (or -1 if not configured) */
128 return (uint32_t)cpu_to_lapic
[cpu
];
134 lapic_cpu_map_dump(void)
138 for (i
= 0; i
< MAX_CPUS
; i
++) {
139 if (cpu_to_lapic
[i
] == -1)
141 kprintf("cpu_to_lapic[%d]: %d\n",
144 for (i
= 0; i
< MAX_CPUS
; i
++) {
145 if (lapic_to_cpu
[i
] == -1)
147 kprintf("lapic_to_cpu[%d]: %d\n",
151 #endif /* MP_DEBUG */
157 vm_map_entry_t entry
;
160 boolean_t is_boot_processor
;
161 boolean_t is_lapic_enabled
;
162 vm_offset_t lapic_base
;
164 /* Examine the local APIC state */
165 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
166 is_boot_processor
= (lo
& MSR_IA32_APIC_BASE_BSP
) != 0;
167 is_lapic_enabled
= (lo
& MSR_IA32_APIC_BASE_ENABLE
) != 0;
168 lapic_base
= (lo
& MSR_IA32_APIC_BASE_BASE
);
169 kprintf("MSR_IA32_APIC_BASE 0x%x %s %s\n", lapic_base
,
170 is_lapic_enabled
? "enabled" : "disabled",
171 is_boot_processor
? "BSP" : "AP");
172 if (!is_boot_processor
|| !is_lapic_enabled
)
173 panic("Unexpected local APIC state\n");
175 /* Establish a map to the local apic */
176 lapic_start
= vm_map_min(kernel_map
);
177 result
= vm_map_find_space(kernel_map
,
178 (vm_map_address_t
*) &lapic_start
,
179 round_page(LAPIC_SIZE
), 0,
180 VM_MAKE_TAG(VM_MEMORY_IOKIT
), &entry
);
181 if (result
!= KERN_SUCCESS
) {
182 panic("smp_init: vm_map_find_entry FAILED (err=%d)", result
);
184 vm_map_unlock(kernel_map
);
185 /* Map in the local APIC non-cacheable, as recommended by Intel
186 * in section 8.4.1 of the "System Programming Guide".
188 pmap_enter(pmap_kernel(),
190 (ppnum_t
) i386_btop(lapic_base
),
191 VM_PROT_READ
|VM_PROT_WRITE
,
194 lapic_id
= (unsigned long)(lapic_start
+ LAPIC_ID
);
196 if ((LAPIC_READ(VERSION
)&LAPIC_VERSION_MASK
) < 0x14) {
197 printf("Local APIC version 0x%x, 0x14 or greater expected\n",
198 (LAPIC_READ(VERSION
)&LAPIC_VERSION_MASK
));
201 /* Set up the lapic_id <-> cpu_number map and add this boot processor */
202 lapic_cpu_map_init();
203 lapic_cpu_map((LAPIC_READ(ID
)>>LAPIC_ID_SHIFT
)&LAPIC_ID_MASK
, 0);
204 kprintf("Boot cpu local APIC id 0x%x\n", cpu_to_lapic
[0]);
211 /* write-read register */
212 LAPIC_WRITE(ERROR_STATUS
, 0);
213 return LAPIC_READ(ERROR_STATUS
);
217 lapic_esr_clear(void)
219 LAPIC_WRITE(ERROR_STATUS
, 0);
220 LAPIC_WRITE(ERROR_STATUS
, 0);
223 static const char *DM_str
[8] = {
238 #define BOOL(a) ((a)?' ':'!')
240 LAPIC_READ(lvt)&LAPIC_LVT_VECTOR_MASK
242 (LAPIC_READ(lvt)&LAPIC_LVT_DS_PENDING)?" SendPending" : "Idle"
244 DM_str[(LAPIC_READ(lvt)>>LAPIC_LVT_DM_SHIFT)&LAPIC_LVT_DM_MASK]
246 BOOL(LAPIC_READ(lvt)&LAPIC_LVT_MASKED)
248 (LAPIC_READ(lvt)&LAPIC_LVT_TM_LEVEL)? "Level" : "Edge"
250 (LAPIC_READ(lvt)&LAPIC_LVT_IP_PLRITY_LOW)? "Low " : "High"
252 kprintf("LAPIC %d at 0x%x version 0x%x\n",
253 (LAPIC_READ(ID
)>>LAPIC_ID_SHIFT
)&LAPIC_ID_MASK
,
255 LAPIC_READ(VERSION
)&LAPIC_VERSION_MASK
);
256 kprintf("Priorities: Task 0x%x Arbitration 0x%x Processor 0x%x\n",
257 LAPIC_READ(TPR
)&LAPIC_TPR_MASK
,
258 LAPIC_READ(APR
)&LAPIC_APR_MASK
,
259 LAPIC_READ(PPR
)&LAPIC_PPR_MASK
);
260 kprintf("Destination Format 0x%x Logical Destination 0x%x\n",
261 LAPIC_READ(DFR
)>>LAPIC_DFR_SHIFT
,
262 LAPIC_READ(LDR
)>>LAPIC_LDR_SHIFT
);
263 kprintf("%cEnabled %cFocusChecking SV 0x%x\n",
264 BOOL(LAPIC_READ(SVR
)&LAPIC_SVR_ENABLE
),
265 BOOL(!(LAPIC_READ(SVR
)&LAPIC_SVR_FOCUS_OFF
)),
266 LAPIC_READ(SVR
) & LAPIC_SVR_MASK
);
267 if (mca_is_cmci_present())
268 kprintf("LVT_CMCI: Vector 0x%02x [%s] %s %cmasked\n",
273 kprintf("LVT_TIMER: Vector 0x%02x %s %cmasked %s\n",
277 (LAPIC_READ(LVT_TIMER
)&LAPIC_LVT_PERIODIC
)?"Periodic":"OneShot");
278 kprintf(" Initial Count: 0x%08x \n", LAPIC_READ(TIMER_INITIAL_COUNT
));
279 kprintf(" Current Count: 0x%08x \n", LAPIC_READ(TIMER_CURRENT_COUNT
));
280 kprintf(" Divide Config: 0x%08x \n", LAPIC_READ(TIMER_DIVIDE_CONFIG
));
281 kprintf("LVT_PERFCNT: Vector 0x%02x [%s] %s %cmasked\n",
286 kprintf("LVT_THERMAL: Vector 0x%02x [%s] %s %cmasked\n",
291 kprintf("LVT_LINT0: Vector 0x%02x [%s][%s][%s] %s %cmasked\n",
298 kprintf("LVT_LINT1: Vector 0x%02x [%s][%s][%s] %s %cmasked\n",
305 kprintf("LVT_ERROR: Vector 0x%02x %s %cmasked\n",
309 kprintf("ESR: %08x \n", lapic_esr_read());
311 for(i
=0xf; i
>=0; i
--)
312 kprintf("%x%x%x%x",i
,i
,i
,i
);
316 kprintf("%08x",LAPIC_READ_OFFSET(TMR_BASE
, i
*0x10));
320 kprintf("%08x",LAPIC_READ_OFFSET(IRR_BASE
, i
*0x10));
323 for(i
=7; i
>= 0; i
--)
324 kprintf("%08x",LAPIC_READ_OFFSET(ISR_BASE
, i
*0x10));
335 db_apic(__unused db_expr_t addr
,
336 __unused
int have_addr
,
337 __unused db_expr_t count
,
338 __unused
char *modif
)
354 if (cpuid_features() & CPUID_FEATURE_APIC
)
357 if (cpuid_family() == 6 || cpuid_family() == 15) {
360 * There may be a local APIC which wasn't enabled by BIOS.
361 * So we try to enable it explicitly.
363 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
364 lo
&= ~MSR_IA32_APIC_BASE_BASE
;
365 lo
|= MSR_IA32_APIC_BASE_ENABLE
| LAPIC_START
;
366 lo
|= MSR_IA32_APIC_BASE_ENABLE
;
367 wrmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
370 * Re-initialize cpu features info and re-check.
373 if (cpuid_features() & CPUID_FEATURE_APIC
) {
374 printf("Local APIC discovered and enabled\n");
375 lapic_os_enabled
= TRUE
;
376 lapic_interrupt_base
= LAPIC_REDUCED_INTERRUPT_BASE
;
391 /* Shutdown if local APIC was enabled by OS */
392 if (lapic_os_enabled
== FALSE
)
395 mp_disable_preemption();
398 if (get_cpu_number() == master_cpu
) {
399 value
= LAPIC_READ(LVT_LINT0
);
400 value
|= LAPIC_LVT_MASKED
;
401 LAPIC_WRITE(LVT_LINT0
, value
);
405 LAPIC_WRITE(LVT_ERROR
, LAPIC_READ(LVT_ERROR
) | LAPIC_LVT_MASKED
);
408 LAPIC_WRITE(LVT_TIMER
, LAPIC_READ(LVT_TIMER
) | LAPIC_LVT_MASKED
);
410 /* Perfmon: masked */
411 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_READ(LVT_PERFCNT
) | LAPIC_LVT_MASKED
);
413 /* APIC software disabled */
414 LAPIC_WRITE(SVR
, LAPIC_READ(SVR
) & ~LAPIC_SVR_ENABLE
);
416 /* Bypass the APIC completely and update cpu features */
417 rdmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
418 lo
&= ~MSR_IA32_APIC_BASE_ENABLE
;
419 wrmsr(MSR_IA32_APIC_BASE
, lo
, hi
);
422 mp_enable_preemption();
426 lapic_configure(void)
430 if (lapic_error_time_threshold
== 0 && cpu_number() == 0) {
431 nanoseconds_to_absolutetime(NSEC_PER_SEC
>> 2, &lapic_error_time_threshold
);
432 if (!PE_parse_boot_argn("lapic_dont_panic", &lapic_dont_panic
, sizeof(lapic_dont_panic
))) {
433 lapic_dont_panic
= FALSE
;
437 /* Set flat delivery model, logical processor id */
438 LAPIC_WRITE(DFR
, LAPIC_DFR_FLAT
);
439 LAPIC_WRITE(LDR
, (get_cpu_number()) << LAPIC_LDR_SHIFT
);
444 LAPIC_WRITE(SVR
, LAPIC_VECTOR(SPURIOUS
) | LAPIC_SVR_ENABLE
);
447 if (get_cpu_number() == master_cpu
) {
448 value
= LAPIC_READ(LVT_LINT0
);
449 value
&= ~LAPIC_LVT_MASKED
;
450 value
|= LAPIC_LVT_DM_EXTINT
;
451 LAPIC_WRITE(LVT_LINT0
, value
);
454 /* Timer: unmasked, one-shot */
455 LAPIC_WRITE(LVT_TIMER
, LAPIC_VECTOR(TIMER
));
457 /* Perfmon: unmasked */
458 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_VECTOR(PERFCNT
));
460 /* Thermal: unmasked */
461 LAPIC_WRITE(LVT_THERMAL
, LAPIC_VECTOR(THERMAL
));
463 /* CMCI, if available */
464 if (mca_is_cmci_present())
465 LAPIC_WRITE(LVT_CMCI
, LAPIC_VECTOR(CMCI
));
467 if (((cpu_number() == master_cpu
) && lapic_errors_masked
== FALSE
) ||
468 (cpu_number() != master_cpu
)) {
470 LAPIC_WRITE(LVT_ERROR
, LAPIC_VECTOR(ERROR
));
477 lapic_timer_mode_t mode
,
478 lapic_timer_divide_t divisor
,
479 lapic_timer_count_t initial_count
)
482 uint32_t timer_vector
;
484 state
= ml_set_interrupts_enabled(FALSE
);
485 timer_vector
= LAPIC_READ(LVT_TIMER
);
486 timer_vector
&= ~(LAPIC_LVT_MASKED
|LAPIC_LVT_PERIODIC
);;
487 timer_vector
|= interrupt
? 0 : LAPIC_LVT_MASKED
;
488 timer_vector
|= (mode
== periodic
) ? LAPIC_LVT_PERIODIC
: 0;
489 LAPIC_WRITE(LVT_TIMER
, timer_vector
);
490 LAPIC_WRITE(TIMER_DIVIDE_CONFIG
, divisor
);
491 LAPIC_WRITE(TIMER_INITIAL_COUNT
, initial_count
);
492 ml_set_interrupts_enabled(state
);
497 lapic_timer_mode_t
*mode
,
498 lapic_timer_divide_t
*divisor
,
499 lapic_timer_count_t
*initial_count
,
500 lapic_timer_count_t
*current_count
)
504 state
= ml_set_interrupts_enabled(FALSE
);
506 *mode
= (LAPIC_READ(LVT_TIMER
) & LAPIC_LVT_PERIODIC
) ?
509 *divisor
= LAPIC_READ(TIMER_DIVIDE_CONFIG
) & LAPIC_TIMER_DIVIDE_MASK
;
511 *initial_count
= LAPIC_READ(TIMER_INITIAL_COUNT
);
513 *current_count
= LAPIC_READ(TIMER_CURRENT_COUNT
);
514 ml_set_interrupts_enabled(state
);
518 _lapic_end_of_interrupt(void)
524 lapic_end_of_interrupt(void)
526 _lapic_end_of_interrupt();
530 lapic_set_intr_func(int vector
, i386_intr_func_t func
)
532 if (vector
> lapic_interrupt_base
)
533 vector
-= lapic_interrupt_base
;
536 case LAPIC_NMI_INTERRUPT
:
537 case LAPIC_INTERPROCESSOR_INTERRUPT
:
538 case LAPIC_TIMER_INTERRUPT
:
539 case LAPIC_THERMAL_INTERRUPT
:
540 case LAPIC_PERFCNT_INTERRUPT
:
541 case LAPIC_CMCI_INTERRUPT
:
542 lapic_intr_func
[vector
] = func
;
545 panic("lapic_set_intr_func(%d,%p) invalid vector\n",
551 lapic_interrupt(int interrupt
, x86_saved_state_t
*state
)
556 interrupt
-= lapic_interrupt_base
;
558 if (interrupt
== (LAPIC_NMI_INTERRUPT
- lapic_interrupt_base
) &&
559 lapic_intr_func
[LAPIC_NMI_INTERRUPT
] != NULL
) {
560 retval
= (*lapic_intr_func
[LAPIC_NMI_INTERRUPT
])(state
);
561 _lapic_end_of_interrupt();
569 case LAPIC_TIMER_INTERRUPT
:
570 case LAPIC_THERMAL_INTERRUPT
:
571 case LAPIC_PERFCNT_INTERRUPT
:
572 case LAPIC_INTERPROCESSOR_INTERRUPT
:
573 if (lapic_intr_func
[interrupt
] != NULL
)
574 (void) (*lapic_intr_func
[interrupt
])(state
);
575 if (interrupt
== LAPIC_PERFCNT_INTERRUPT
)
576 /* Clear interrupt masked */
577 LAPIC_WRITE(LVT_PERFCNT
, LAPIC_VECTOR(PERFCNT
));
578 _lapic_end_of_interrupt();
581 case LAPIC_CMCI_INTERRUPT
:
582 if (lapic_intr_func
[interrupt
] != NULL
)
583 (void) (*lapic_intr_func
[interrupt
])(state
);
584 /* return 0 for plaform expert to handle */
586 case LAPIC_ERROR_INTERRUPT
:
587 /* We treat error interrupts on APs as fatal.
588 * The current interrupt steering scheme directs most
589 * external interrupts to the BSP (HPET interrupts being
590 * a notable exception); hence, such an error
591 * on an AP may signify LVT corruption (with "may" being
592 * the operative word). On the BSP, we adopt a more
593 * lenient approach, in the interests of enhancing
594 * debuggability and reducing fragility.
595 * If "lapic_error_count_threshold" error interrupts
596 * occur within "lapic_error_time_threshold" absolute
597 * time units, we mask the error vector and log. The
598 * error interrupts themselves are likely
599 * side effects of issues which are beyond the purview of
600 * the local APIC interrupt handler, however. The Error
601 * Status Register value (the illegal destination
602 * vector code is one observed in practice) indicates
603 * the immediate cause of the error.
605 esr
= lapic_esr_read();
608 if ((debug_boot_arg
&& (lapic_dont_panic
== FALSE
)) ||
609 cpu_number() != master_cpu
) {
610 panic("Local APIC error, ESR: %d\n", esr
);
613 if (cpu_number() == master_cpu
) {
614 uint64_t abstime
= mach_absolute_time();
615 if ((abstime
- lapic_last_master_error
) < lapic_error_time_threshold
) {
616 if (lapic_master_error_count
++ > lapic_error_count_threshold
) {
617 lapic_errors_masked
= TRUE
;
618 LAPIC_WRITE(LVT_ERROR
, LAPIC_READ(LVT_ERROR
) | LAPIC_LVT_MASKED
);
619 printf("Local APIC: errors masked\n");
623 lapic_last_master_error
= abstime
;
624 lapic_master_error_count
= 0;
626 printf("Local APIC error on master CPU, ESR: %d, error count this run: %d\n", esr
, lapic_master_error_count
);
629 _lapic_end_of_interrupt();
632 case LAPIC_SPURIOUS_INTERRUPT
:
634 /* No EOI required here */
643 lapic_smm_restore(void)
647 if (lapic_os_enabled
== FALSE
)
650 state
= ml_set_interrupts_enabled(FALSE
);
652 if (LAPIC_ISR_IS_SET(LAPIC_REDUCED_INTERRUPT_BASE
, TIMER
)) {
654 * Bogus SMI handler enables interrupts but does not know about
655 * local APIC interrupt sources. When APIC timer counts down to
656 * zero while in SMM, local APIC will end up waiting for an EOI
657 * but no interrupt was delivered to the OS.
659 _lapic_end_of_interrupt();
662 * timer is one-shot, trigger another quick countdown to trigger
663 * another timer interrupt.
665 if (LAPIC_READ(TIMER_CURRENT_COUNT
) == 0) {
666 LAPIC_WRITE(TIMER_INITIAL_COUNT
, 1);
669 kprintf("lapic_smm_restore\n");
672 ml_set_interrupts_enabled(state
);