2 * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3 * Copyright (c) 1996, by Steve Passe
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. The name of the developer may NOT be used to endorse or promote products
12 * derived from this software without specific prior written permission.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Local APIC support on Pentium and later processors.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_atpic.h"
38 #include "opt_hwpmc_hooks.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/mutex.h>
50 #include <sys/sched.h>
52 #include <sys/sysctl.h>
53 #include <sys/timeet.h>
58 #include <x86/apicreg.h>
59 #include <machine/clock.h>
60 #include <machine/cpufunc.h>
61 #include <machine/cputypes.h>
62 #include <machine/frame.h>
63 #include <machine/intr_machdep.h>
64 #include <x86/apicvar.h>
66 #include <machine/md_var.h>
67 #include <machine/smp.h>
68 #include <machine/specialreg.h>
72 #include <sys/interrupt.h>
77 #define SDT_APIC SDT_SYSIGT
78 #define SDT_APICT SDT_SYSIGT
81 #define SDT_APIC SDT_SYS386IGT
82 #define SDT_APICT SDT_SYS386TGT
83 #define GSEL_APIC GSEL(GCODE_SEL, SEL_KPL)
86 /* Sanity checks on IDT vectors. */
87 CTASSERT(APIC_IO_INTS + APIC_NUM_IOINTS == APIC_TIMER_INT);
88 CTASSERT(APIC_TIMER_INT < APIC_LOCAL_INTS);
89 CTASSERT(APIC_LOCAL_INTS == 240);
90 CTASSERT(IPI_STOP < APIC_SPURIOUS_INT);
92 /* Magic IRQ values for the timer and syscalls. */
93 #define IRQ_TIMER (NUM_IO_INTS + 1)
94 #define IRQ_SYSCALL (NUM_IO_INTS + 2)
95 #define IRQ_DTRACE_RET (NUM_IO_INTS + 3)
96 #define IRQ_EVTCHN (NUM_IO_INTS + 4)
100 LAT_MODE_PERIODIC = 1,
101 LAT_MODE_ONESHOT = 2,
102 LAT_MODE_DEADLINE = 3,
106 * Support for local APICs. Local APICs manage interrupts on each
107 * individual processor as opposed to I/O APICs which receive interrupts
108 * from I/O devices and then forward them on to the local APICs.
110 * Local APICs can also send interrupts to each other thus providing the
111 * mechanism for IPIs.
115 u_int lvt_edgetrigger:1;
116 u_int lvt_activehi:1;
124 struct lvt la_lvts[APIC_LVT_MAX + 1];
127 u_int la_cluster_id:2;
129 u_long *la_timer_count;
130 uint64_t la_timer_period;
131 enum lat_timer_mode la_timer_mode;
132 uint32_t lvt_timer_base;
133 uint32_t lvt_timer_last;
134 /* Include IDT_SYSCALL to make indexing easier. */
135 int la_ioint_irqs[APIC_NUM_IOINTS + 1];
136 } static lapics[MAX_APIC_ID + 1];
138 /* Global defaults for local APIC LVT entries. */
139 static struct lvt lvts[APIC_LVT_MAX + 1] = {
140 { 1, 1, 1, 1, APIC_LVT_DM_EXTINT, 0 }, /* LINT0: masked ExtINT */
141 { 1, 1, 0, 1, APIC_LVT_DM_NMI, 0 }, /* LINT1: NMI */
142 { 1, 1, 1, 1, APIC_LVT_DM_FIXED, APIC_TIMER_INT }, /* Timer */
143 { 1, 1, 0, 1, APIC_LVT_DM_FIXED, APIC_ERROR_INT }, /* Error */
144 { 1, 1, 1, 1, APIC_LVT_DM_NMI, 0 }, /* PMC */
145 { 1, 1, 1, 1, APIC_LVT_DM_FIXED, APIC_THERMAL_INT }, /* Thermal */
146 { 1, 1, 1, 1, APIC_LVT_DM_FIXED, APIC_CMC_INT }, /* CMCI */
149 static inthand_t *ioint_handlers[] = {
151 IDTVEC(apic_isr1), /* 32 - 63 */
152 IDTVEC(apic_isr2), /* 64 - 95 */
153 IDTVEC(apic_isr3), /* 96 - 127 */
154 IDTVEC(apic_isr4), /* 128 - 159 */
155 IDTVEC(apic_isr5), /* 160 - 191 */
156 IDTVEC(apic_isr6), /* 192 - 223 */
157 IDTVEC(apic_isr7), /* 224 - 255 */
161 static u_int32_t lapic_timer_divisors[] = {
162 APIC_TDCR_1, APIC_TDCR_2, APIC_TDCR_4, APIC_TDCR_8, APIC_TDCR_16,
163 APIC_TDCR_32, APIC_TDCR_64, APIC_TDCR_128
166 extern inthand_t IDTVEC(rsvd);
168 volatile char *lapic_map;
169 vm_paddr_t lapic_paddr;
171 int lapic_eoi_suppression;
172 static int lapic_timer_tsc_deadline;
173 static u_long lapic_timer_divisor;
174 static struct eventtimer lapic_et;
176 static uint64_t lapic_ipi_wait_mult;
179 SYSCTL_NODE(_hw, OID_AUTO, apic, CTLFLAG_RD, 0, "APIC options");
180 SYSCTL_INT(_hw_apic, OID_AUTO, x2apic_mode, CTLFLAG_RD, &x2apic_mode, 0, "");
181 SYSCTL_INT(_hw_apic, OID_AUTO, eoi_suppression, CTLFLAG_RD,
182 &lapic_eoi_suppression, 0, "");
183 SYSCTL_INT(_hw_apic, OID_AUTO, timer_tsc_deadline, CTLFLAG_RD,
184 &lapic_timer_tsc_deadline, 0, "");
187 lapic_read32(enum LAPIC_REGISTERS reg)
192 res = rdmsr32(MSR_APIC_000 + reg);
194 res = *(volatile uint32_t *)(lapic_map + reg * LAPIC_MEM_MUL);
200 lapic_write32(enum LAPIC_REGISTERS reg, uint32_t val)
205 wrmsr(MSR_APIC_000 + reg, val);
207 *(volatile uint32_t *)(lapic_map + reg * LAPIC_MEM_MUL) = val;
212 lapic_write32_nofence(enum LAPIC_REGISTERS reg, uint32_t val)
216 wrmsr(MSR_APIC_000 + reg, val);
218 *(volatile uint32_t *)(lapic_map + reg * LAPIC_MEM_MUL) = val;
230 v = rdmsr(MSR_APIC_000 + LAPIC_ICR_LO);
232 vhi = lapic_read32(LAPIC_ICR_HI);
233 vlo = lapic_read32(LAPIC_ICR_LO);
234 v = ((uint64_t)vhi << 32) | vlo;
240 lapic_read_icr_lo(void)
243 return (lapic_read32(LAPIC_ICR_LO));
247 lapic_write_icr(uint32_t vhi, uint32_t vlo)
252 v = ((uint64_t)vhi << 32) | vlo;
254 wrmsr(MSR_APIC_000 + LAPIC_ICR_LO, v);
256 lapic_write32(LAPIC_ICR_HI, vhi);
257 lapic_write32(LAPIC_ICR_LO, vlo);
263 native_lapic_enable_x2apic(void)
267 apic_base = rdmsr(MSR_APICBASE);
268 apic_base |= APICBASE_X2APIC | APICBASE_ENABLED;
269 wrmsr(MSR_APICBASE, apic_base);
272 static void lapic_enable(void);
273 static void lapic_resume(struct pic *pic, bool suspend_cancelled);
274 static void lapic_timer_oneshot(struct lapic *);
275 static void lapic_timer_oneshot_nointr(struct lapic *, uint32_t);
276 static void lapic_timer_periodic(struct lapic *);
277 static void lapic_timer_deadline(struct lapic *);
278 static void lapic_timer_stop(struct lapic *);
279 static void lapic_timer_set_divisor(u_int divisor);
280 static uint32_t lvt_mode(struct lapic *la, u_int pin, uint32_t value);
281 static int lapic_et_start(struct eventtimer *et,
282 sbintime_t first, sbintime_t period);
283 static int lapic_et_stop(struct eventtimer *et);
284 static u_int apic_idt_to_irq(u_int apic_id, u_int vector);
285 static void lapic_set_tpr(u_int vector);
287 struct pic lapic_pic = { .pic_resume = lapic_resume };
289 /* Forward declarations for apic_ops */
290 static void native_lapic_create(u_int apic_id, int boot_cpu);
291 static void native_lapic_init(vm_paddr_t addr);
292 static void native_lapic_xapic_mode(void);
293 static void native_lapic_setup(int boot);
294 static void native_lapic_dump(const char *str);
295 static void native_lapic_disable(void);
296 static void native_lapic_eoi(void);
297 static int native_lapic_id(void);
298 static int native_lapic_intr_pending(u_int vector);
299 static u_int native_apic_cpuid(u_int apic_id);
300 static u_int native_apic_alloc_vector(u_int apic_id, u_int irq);
301 static u_int native_apic_alloc_vectors(u_int apic_id, u_int *irqs,
302 u_int count, u_int align);
303 static void native_apic_disable_vector(u_int apic_id, u_int vector);
304 static void native_apic_enable_vector(u_int apic_id, u_int vector);
305 static void native_apic_free_vector(u_int apic_id, u_int vector, u_int irq);
306 static void native_lapic_set_logical_id(u_int apic_id, u_int cluster,
308 static int native_lapic_enable_pmc(void);
309 static void native_lapic_disable_pmc(void);
310 static void native_lapic_reenable_pmc(void);
311 static void native_lapic_enable_cmc(void);
312 static int native_lapic_set_lvt_mask(u_int apic_id, u_int lvt,
314 static int native_lapic_set_lvt_mode(u_int apic_id, u_int lvt,
316 static int native_lapic_set_lvt_polarity(u_int apic_id, u_int lvt,
317 enum intr_polarity pol);
318 static int native_lapic_set_lvt_triggermode(u_int apic_id, u_int lvt,
319 enum intr_trigger trigger);
321 static void native_lapic_ipi_raw(register_t icrlo, u_int dest);
322 static void native_lapic_ipi_vectored(u_int vector, int dest);
323 static int native_lapic_ipi_wait(int delay);
325 static int native_lapic_ipi_alloc(inthand_t *ipifunc);
326 static void native_lapic_ipi_free(int vector);
328 struct apic_ops apic_ops = {
329 .create = native_lapic_create,
330 .init = native_lapic_init,
331 .xapic_mode = native_lapic_xapic_mode,
332 .setup = native_lapic_setup,
333 .dump = native_lapic_dump,
334 .disable = native_lapic_disable,
335 .eoi = native_lapic_eoi,
336 .id = native_lapic_id,
337 .intr_pending = native_lapic_intr_pending,
338 .set_logical_id = native_lapic_set_logical_id,
339 .cpuid = native_apic_cpuid,
340 .alloc_vector = native_apic_alloc_vector,
341 .alloc_vectors = native_apic_alloc_vectors,
342 .enable_vector = native_apic_enable_vector,
343 .disable_vector = native_apic_disable_vector,
344 .free_vector = native_apic_free_vector,
345 .enable_pmc = native_lapic_enable_pmc,
346 .disable_pmc = native_lapic_disable_pmc,
347 .reenable_pmc = native_lapic_reenable_pmc,
348 .enable_cmc = native_lapic_enable_cmc,
350 .ipi_raw = native_lapic_ipi_raw,
351 .ipi_vectored = native_lapic_ipi_vectored,
352 .ipi_wait = native_lapic_ipi_wait,
354 .ipi_alloc = native_lapic_ipi_alloc,
355 .ipi_free = native_lapic_ipi_free,
356 .set_lvt_mask = native_lapic_set_lvt_mask,
357 .set_lvt_mode = native_lapic_set_lvt_mode,
358 .set_lvt_polarity = native_lapic_set_lvt_polarity,
359 .set_lvt_triggermode = native_lapic_set_lvt_triggermode,
363 lvt_mode(struct lapic *la, u_int pin, uint32_t value)
367 KASSERT(pin <= APIC_LVT_MAX, ("%s: pin %u out of range", __func__, pin));
368 if (la->la_lvts[pin].lvt_active)
369 lvt = &la->la_lvts[pin];
373 value &= ~(APIC_LVT_M | APIC_LVT_TM | APIC_LVT_IIPP | APIC_LVT_DM |
375 if (lvt->lvt_edgetrigger == 0)
376 value |= APIC_LVT_TM;
377 if (lvt->lvt_activehi == 0)
378 value |= APIC_LVT_IIPP_INTALO;
381 value |= lvt->lvt_mode;
382 switch (lvt->lvt_mode) {
383 case APIC_LVT_DM_NMI:
384 case APIC_LVT_DM_SMI:
385 case APIC_LVT_DM_INIT:
386 case APIC_LVT_DM_EXTINT:
387 if (!lvt->lvt_edgetrigger && bootverbose) {
388 printf("lapic%u: Forcing LINT%u to edge trigger\n",
390 value |= APIC_LVT_TM;
392 /* Use a vector of 0. */
394 case APIC_LVT_DM_FIXED:
395 value |= lvt->lvt_vector;
398 panic("bad APIC LVT delivery mode: %#x\n", value);
404 * Map the local APIC and setup necessary interrupt vectors.
407 native_lapic_init(vm_paddr_t addr)
410 uint64_t r, r1, r2, rx;
417 * Enable x2APIC mode if possible. Map the local APIC
420 * Keep the LAPIC registers page mapped uncached for x2APIC
421 * mode too, to have direct map page attribute set to
422 * uncached. This is needed to work around CPU errata present
423 * on all Intel processors.
425 KASSERT(trunc_page(addr) == addr,
426 ("local APIC not aligned on a page boundary"));
428 lapic_map = pmap_mapdev(addr, PAGE_SIZE);
430 native_lapic_enable_x2apic();
434 /* Setup the spurious interrupt handler. */
435 setidt(APIC_SPURIOUS_INT, IDTVEC(spuriousint), SDT_APIC, SEL_KPL,
438 /* Perform basic initialization of the BSP's local APIC. */
441 /* Set BSP's per-CPU local APIC ID. */
442 PCPU_SET(apic_id, lapic_id());
444 /* Local APIC timer interrupt. */
445 setidt(APIC_TIMER_INT, IDTVEC(timerint), SDT_APIC, SEL_KPL, GSEL_APIC);
447 /* Local APIC error interrupt. */
448 setidt(APIC_ERROR_INT, IDTVEC(errorint), SDT_APIC, SEL_KPL, GSEL_APIC);
450 /* XXX: Thermal interrupt */
452 /* Local APIC CMCI. */
453 setidt(APIC_CMC_INT, IDTVEC(cmcint), SDT_APICT, SEL_KPL, GSEL_APIC);
455 if ((resource_int_value("apic", 0, "clock", &i) != 0 || i != 0)) {
457 /* Intel CPUID 0x06 EAX[2] set if APIC timer runs in C3. */
458 if (cpu_vendor_id == CPU_VENDOR_INTEL && cpu_high >= 6) {
459 do_cpuid(0x06, regs);
460 if ((regs[0] & CPUTPM1_ARAT) != 0)
463 bzero(&lapic_et, sizeof(lapic_et));
464 lapic_et.et_name = "LAPIC";
465 lapic_et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT |
467 lapic_et.et_quality = 600;
469 lapic_et.et_flags |= ET_FLAGS_C3STOP;
470 lapic_et.et_quality -= 200;
471 } else if ((cpu_feature & CPUID_TSC) != 0 &&
472 (cpu_feature2 & CPUID2_TSCDLT) != 0 &&
473 tsc_is_invariant && tsc_freq != 0) {
474 lapic_timer_tsc_deadline = 1;
475 TUNABLE_INT_FETCH("hw.lapic_tsc_deadline",
476 &lapic_timer_tsc_deadline);
479 lapic_et.et_frequency = 0;
480 /* We don't know frequency yet, so trying to guess. */
481 lapic_et.et_min_period = 0x00001000LL;
482 lapic_et.et_max_period = SBT_1S;
483 lapic_et.et_start = lapic_et_start;
484 lapic_et.et_stop = lapic_et_stop;
485 lapic_et.et_priv = NULL;
486 et_register(&lapic_et);
490 * Set lapic_eoi_suppression after lapic_enable(), to not
491 * enable suppression in the hardware prematurely. Note that
492 * we by default enable suppression even when system only has
493 * one IO-APIC, since EOI is broadcasted to all APIC agents,
494 * including CPUs, otherwise.
496 * It seems that at least some KVM versions report
497 * EOI_SUPPRESSION bit, but auto-EOI does not work.
499 ver = lapic_read32(LAPIC_VERSION);
500 if ((ver & APIC_VER_EOI_SUPPRESSION) != 0) {
501 lapic_eoi_suppression = 1;
502 if (vm_guest == VM_GUEST_VM &&
503 !strcmp(hv_vendor, "KVMKVMKVM")) {
506 "KVM -- disabling lapic eoi suppression\n");
507 lapic_eoi_suppression = 0;
509 TUNABLE_INT_FETCH("hw.lapic_eoi_suppression",
510 &lapic_eoi_suppression);
514 #define LOOPS 1000000
516 * Calibrate the busy loop waiting for IPI ack in xAPIC mode.
517 * lapic_ipi_wait_mult contains the number of iterations which
518 * approximately delay execution for 1 microsecond (the
519 * argument to native_lapic_ipi_wait() is in microseconds).
521 * We assume that TSC is present and already measured.
522 * Possible TSC frequency jumps are irrelevant to the
523 * calibration loop below, the CPU clock management code is
524 * not yet started, and we do not enter sleep states.
526 KASSERT((cpu_feature & CPUID_TSC) != 0 && tsc_freq != 0,
527 ("TSC not initialized"));
529 for (rx = 0; rx < LOOPS; rx++) {
530 (void)lapic_read_icr_lo();
534 r1 = tsc_freq * LOOPS;
536 lapic_ipi_wait_mult = r1 >= r2 ? r1 / r2 : 1;
538 printf("LAPIC: ipi_wait() us multiplier %ju (r %ju tsc %ju)\n",
539 (uintmax_t)lapic_ipi_wait_mult, (uintmax_t)r,
540 (uintmax_t)tsc_freq);
547 * Create a local APIC instance.
550 native_lapic_create(u_int apic_id, int boot_cpu)
554 if (apic_id > MAX_APIC_ID) {
555 printf("APIC: Ignoring local APIC with ID %d\n", apic_id);
557 panic("Can't ignore BSP");
560 KASSERT(!lapics[apic_id].la_present, ("duplicate local APIC %u",
564 * Assume no local LVT overrides and a cluster of 0 and
565 * intra-cluster ID of 0.
567 lapics[apic_id].la_present = 1;
568 lapics[apic_id].la_id = apic_id;
569 for (i = 0; i <= APIC_LVT_MAX; i++) {
570 lapics[apic_id].la_lvts[i] = lvts[i];
571 lapics[apic_id].la_lvts[i].lvt_active = 0;
573 for (i = 0; i <= APIC_NUM_IOINTS; i++)
574 lapics[apic_id].la_ioint_irqs[i] = -1;
575 lapics[apic_id].la_ioint_irqs[IDT_SYSCALL - APIC_IO_INTS] = IRQ_SYSCALL;
576 lapics[apic_id].la_ioint_irqs[APIC_TIMER_INT - APIC_IO_INTS] =
579 lapics[apic_id].la_ioint_irqs[IDT_DTRACE_RET - APIC_IO_INTS] =
583 lapics[apic_id].la_ioint_irqs[IDT_EVTCHN - APIC_IO_INTS] = IRQ_EVTCHN;
588 cpu_add(apic_id, boot_cpu);
593 * Dump contents of local APIC registers
596 native_lapic_dump(const char* str)
600 maxlvt = (lapic_read32(LAPIC_VERSION) & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
601 printf("cpu%d %s:\n", PCPU_GET(cpuid), str);
602 printf(" ID: 0x%08x VER: 0x%08x LDR: 0x%08x DFR: 0x%08x",
603 lapic_read32(LAPIC_ID), lapic_read32(LAPIC_VERSION),
604 lapic_read32(LAPIC_LDR), x2apic_mode ? 0 : lapic_read32(LAPIC_DFR));
605 if ((cpu_feature2 & CPUID2_X2APIC) != 0)
606 printf(" x2APIC: %d", x2apic_mode);
607 printf("\n lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
608 lapic_read32(LAPIC_LVT_LINT0), lapic_read32(LAPIC_LVT_LINT1),
609 lapic_read32(LAPIC_TPR), lapic_read32(LAPIC_SVR));
610 printf(" timer: 0x%08x therm: 0x%08x err: 0x%08x",
611 lapic_read32(LAPIC_LVT_TIMER), lapic_read32(LAPIC_LVT_THERMAL),
612 lapic_read32(LAPIC_LVT_ERROR));
613 if (maxlvt >= APIC_LVT_PMC)
614 printf(" pmc: 0x%08x", lapic_read32(LAPIC_LVT_PCINT));
616 if (maxlvt >= APIC_LVT_CMCI)
617 printf(" cmci: 0x%08x\n", lapic_read32(LAPIC_LVT_CMCI));
621 native_lapic_xapic_mode(void)
625 saveintr = intr_disable();
627 native_lapic_enable_x2apic();
628 intr_restore(saveintr);
632 native_lapic_setup(int boot)
637 char buf[MAXCOMLEN + 1];
639 saveintr = intr_disable();
641 la = &lapics[lapic_id()];
642 KASSERT(la->la_present, ("missing APIC structure"));
643 maxlvt = (lapic_read32(LAPIC_VERSION) & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
645 /* Initialize the TPR to allow all interrupts. */
648 /* Setup spurious vector and enable the local APIC. */
651 /* Program LINT[01] LVT entries. */
652 lapic_write32(LAPIC_LVT_LINT0, lvt_mode(la, APIC_LVT_LINT0,
653 lapic_read32(LAPIC_LVT_LINT0)));
654 lapic_write32(LAPIC_LVT_LINT1, lvt_mode(la, APIC_LVT_LINT1,
655 lapic_read32(LAPIC_LVT_LINT1)));
657 /* Program the PMC LVT entry if present. */
658 if (maxlvt >= APIC_LVT_PMC) {
659 lapic_write32(LAPIC_LVT_PCINT, lvt_mode(la, APIC_LVT_PMC,
663 /* Program timer LVT and setup handler. */
664 la->lvt_timer_base = lvt_mode(la, APIC_LVT_TIMER,
665 lapic_read32(LAPIC_LVT_TIMER));
666 la->lvt_timer_last = la->lvt_timer_base;
667 lapic_write32(LAPIC_LVT_TIMER, la->lvt_timer_base);
669 snprintf(buf, sizeof(buf), "cpu%d:timer", PCPU_GET(cpuid));
670 intrcnt_add(buf, &la->la_timer_count);
673 /* Setup the timer if configured. */
674 if (la->la_timer_mode != LAT_MODE_UNDEF) {
675 KASSERT(la->la_timer_period != 0, ("lapic%u: zero divisor",
677 switch (la->la_timer_mode) {
678 case LAT_MODE_PERIODIC:
679 lapic_timer_set_divisor(lapic_timer_divisor);
680 lapic_timer_periodic(la);
682 case LAT_MODE_ONESHOT:
683 lapic_timer_set_divisor(lapic_timer_divisor);
684 lapic_timer_oneshot(la);
686 case LAT_MODE_DEADLINE:
687 lapic_timer_deadline(la);
690 panic("corrupted la_timer_mode %p %d", la,
695 /* Program error LVT and clear any existing errors. */
696 lapic_write32(LAPIC_LVT_ERROR, lvt_mode(la, APIC_LVT_ERROR,
697 lapic_read32(LAPIC_LVT_ERROR)));
698 lapic_write32(LAPIC_ESR, 0);
700 /* XXX: Thermal LVT */
702 /* Program the CMCI LVT entry if present. */
703 if (maxlvt >= APIC_LVT_CMCI) {
704 lapic_write32(LAPIC_LVT_CMCI, lvt_mode(la, APIC_LVT_CMCI,
705 lapic_read32(LAPIC_LVT_CMCI)));
708 intr_restore(saveintr);
712 native_lapic_reenable_pmc(void)
717 value = lapic_read32(LAPIC_LVT_PCINT);
718 value &= ~APIC_LVT_M;
719 lapic_write32(LAPIC_LVT_PCINT, value);
725 lapic_update_pmc(void *dummy)
729 la = &lapics[lapic_id()];
730 lapic_write32(LAPIC_LVT_PCINT, lvt_mode(la, APIC_LVT_PMC,
731 lapic_read32(LAPIC_LVT_PCINT)));
736 native_lapic_enable_pmc(void)
741 /* Fail if the local APIC is not present. */
742 if (!x2apic_mode && lapic_map == NULL)
745 /* Fail if the PMC LVT is not present. */
746 maxlvt = (lapic_read32(LAPIC_VERSION) & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
747 if (maxlvt < APIC_LVT_PMC)
750 lvts[APIC_LVT_PMC].lvt_masked = 0;
754 * If hwpmc was loaded at boot time then the APs may not be
755 * started yet. In that case, don't forward the request to
756 * them as they will program the lvt when they start.
759 smp_rendezvous(NULL, lapic_update_pmc, NULL, NULL);
762 lapic_update_pmc(NULL);
770 native_lapic_disable_pmc(void)
775 /* Fail if the local APIC is not present. */
776 if (!x2apic_mode && lapic_map == NULL)
779 /* Fail if the PMC LVT is not present. */
780 maxlvt = (lapic_read32(LAPIC_VERSION) & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
781 if (maxlvt < APIC_LVT_PMC)
784 lvts[APIC_LVT_PMC].lvt_masked = 1;
787 /* The APs should always be started when hwpmc is unloaded. */
788 KASSERT(mp_ncpus == 1 || smp_started, ("hwpmc unloaded too early"));
790 smp_rendezvous(NULL, lapic_update_pmc, NULL, NULL);
795 lapic_calibrate_initcount(struct eventtimer *et, struct lapic *la)
799 /* Start off with a divisor of 2 (power on reset default). */
800 lapic_timer_divisor = 2;
801 /* Try to calibrate the local APIC timer. */
803 lapic_timer_set_divisor(lapic_timer_divisor);
804 lapic_timer_oneshot_nointr(la, APIC_TIMER_MAX_COUNT);
806 value = APIC_TIMER_MAX_COUNT - lapic_read32(LAPIC_CCR_TIMER);
807 if (value != APIC_TIMER_MAX_COUNT)
809 lapic_timer_divisor <<= 1;
810 } while (lapic_timer_divisor <= 128);
811 if (lapic_timer_divisor > 128)
812 panic("lapic: Divisor too big");
814 printf("lapic: Divisor %lu, Frequency %lu Hz\n",
815 lapic_timer_divisor, value);
817 et->et_frequency = value;
821 lapic_calibrate_deadline(struct eventtimer *et, struct lapic *la __unused)
824 et->et_frequency = tsc_freq;
826 printf("lapic: deadline tsc mode, Frequency %ju Hz\n",
827 (uintmax_t)et->et_frequency);
832 lapic_et_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
836 la = &lapics[PCPU_GET(apic_id)];
837 if (et->et_frequency == 0) {
838 if (lapic_timer_tsc_deadline)
839 lapic_calibrate_deadline(et, la);
841 lapic_calibrate_initcount(et, la);
842 et->et_min_period = (0x00000002LLU << 32) / et->et_frequency;
843 et->et_max_period = (0xfffffffeLLU << 32) / et->et_frequency;
846 if (la->la_timer_mode == LAT_MODE_UNDEF)
847 lapic_timer_set_divisor(lapic_timer_divisor);
848 la->la_timer_mode = LAT_MODE_PERIODIC;
849 la->la_timer_period = ((uint32_t)et->et_frequency * period) >>
851 lapic_timer_periodic(la);
852 } else if (lapic_timer_tsc_deadline) {
853 la->la_timer_mode = LAT_MODE_DEADLINE;
854 la->la_timer_period = (et->et_frequency * first) >> 32;
855 lapic_timer_deadline(la);
857 if (la->la_timer_mode == LAT_MODE_UNDEF)
858 lapic_timer_set_divisor(lapic_timer_divisor);
859 la->la_timer_mode = LAT_MODE_ONESHOT;
860 la->la_timer_period = ((uint32_t)et->et_frequency * first) >>
862 lapic_timer_oneshot(la);
868 lapic_et_stop(struct eventtimer *et)
872 la = &lapics[PCPU_GET(apic_id)];
873 lapic_timer_stop(la);
874 la->la_timer_mode = LAT_MODE_UNDEF;
879 native_lapic_disable(void)
883 /* Software disable the local APIC. */
884 value = lapic_read32(LAPIC_SVR);
885 value &= ~APIC_SVR_SWEN;
886 lapic_write32(LAPIC_SVR, value);
894 /* Program the spurious vector to enable the local APIC. */
895 value = lapic_read32(LAPIC_SVR);
896 value &= ~(APIC_SVR_VECTOR | APIC_SVR_FOCUS);
897 value |= APIC_SVR_FEN | APIC_SVR_SWEN | APIC_SPURIOUS_INT;
898 if (lapic_eoi_suppression)
899 value |= APIC_SVR_EOI_SUPPRESSION;
900 lapic_write32(LAPIC_SVR, value);
903 /* Reset the local APIC on the BSP during resume. */
905 lapic_resume(struct pic *pic, bool suspend_cancelled)
912 native_lapic_id(void)
916 KASSERT(x2apic_mode || lapic_map != NULL, ("local APIC is not mapped"));
917 v = lapic_read32(LAPIC_ID);
924 native_lapic_intr_pending(u_int vector)
929 * The IRR registers are an array of registers each of which
930 * only describes 32 interrupts in the low 32 bits. Thus, we
931 * divide the vector by 32 to get the register index.
932 * Finally, we modulus the vector by 32 to determine the
933 * individual bit to test.
935 irr = lapic_read32(LAPIC_IRR0 + vector / 32);
936 return (irr & 1 << (vector % 32));
940 native_lapic_set_logical_id(u_int apic_id, u_int cluster, u_int cluster_id)
944 KASSERT(lapics[apic_id].la_present, ("%s: APIC %u doesn't exist",
946 KASSERT(cluster <= APIC_MAX_CLUSTER, ("%s: cluster %u too big",
948 KASSERT(cluster_id <= APIC_MAX_INTRACLUSTER_ID,
949 ("%s: intra cluster id %u too big", __func__, cluster_id));
950 la = &lapics[apic_id];
951 la->la_cluster = cluster;
952 la->la_cluster_id = cluster_id;
956 native_lapic_set_lvt_mask(u_int apic_id, u_int pin, u_char masked)
959 if (pin > APIC_LVT_MAX)
961 if (apic_id == APIC_ID_ALL) {
962 lvts[pin].lvt_masked = masked;
966 KASSERT(lapics[apic_id].la_present,
967 ("%s: missing APIC %u", __func__, apic_id));
968 lapics[apic_id].la_lvts[pin].lvt_masked = masked;
969 lapics[apic_id].la_lvts[pin].lvt_active = 1;
971 printf("lapic%u:", apic_id);
974 printf(" LINT%u %s\n", pin, masked ? "masked" : "unmasked");
979 native_lapic_set_lvt_mode(u_int apic_id, u_int pin, u_int32_t mode)
983 if (pin > APIC_LVT_MAX)
985 if (apic_id == APIC_ID_ALL) {
990 KASSERT(lapics[apic_id].la_present,
991 ("%s: missing APIC %u", __func__, apic_id));
992 lvt = &lapics[apic_id].la_lvts[pin];
995 printf("lapic%u:", apic_id);
997 lvt->lvt_mode = mode;
999 case APIC_LVT_DM_NMI:
1000 case APIC_LVT_DM_SMI:
1001 case APIC_LVT_DM_INIT:
1002 case APIC_LVT_DM_EXTINT:
1003 lvt->lvt_edgetrigger = 1;
1004 lvt->lvt_activehi = 1;
1005 if (mode == APIC_LVT_DM_EXTINT)
1006 lvt->lvt_masked = 1;
1008 lvt->lvt_masked = 0;
1011 panic("Unsupported delivery mode: 0x%x\n", mode);
1014 printf(" Routing ");
1016 case APIC_LVT_DM_NMI:
1019 case APIC_LVT_DM_SMI:
1022 case APIC_LVT_DM_INIT:
1025 case APIC_LVT_DM_EXTINT:
1029 printf(" -> LINT%u\n", pin);
1035 native_lapic_set_lvt_polarity(u_int apic_id, u_int pin, enum intr_polarity pol)
1038 if (pin > APIC_LVT_MAX || pol == INTR_POLARITY_CONFORM)
1040 if (apic_id == APIC_ID_ALL) {
1041 lvts[pin].lvt_activehi = (pol == INTR_POLARITY_HIGH);
1045 KASSERT(lapics[apic_id].la_present,
1046 ("%s: missing APIC %u", __func__, apic_id));
1047 lapics[apic_id].la_lvts[pin].lvt_active = 1;
1048 lapics[apic_id].la_lvts[pin].lvt_activehi =
1049 (pol == INTR_POLARITY_HIGH);
1051 printf("lapic%u:", apic_id);
1054 printf(" LINT%u polarity: %s\n", pin,
1055 pol == INTR_POLARITY_HIGH ? "high" : "low");
1060 native_lapic_set_lvt_triggermode(u_int apic_id, u_int pin,
1061 enum intr_trigger trigger)
1064 if (pin > APIC_LVT_MAX || trigger == INTR_TRIGGER_CONFORM)
1066 if (apic_id == APIC_ID_ALL) {
1067 lvts[pin].lvt_edgetrigger = (trigger == INTR_TRIGGER_EDGE);
1071 KASSERT(lapics[apic_id].la_present,
1072 ("%s: missing APIC %u", __func__, apic_id));
1073 lapics[apic_id].la_lvts[pin].lvt_edgetrigger =
1074 (trigger == INTR_TRIGGER_EDGE);
1075 lapics[apic_id].la_lvts[pin].lvt_active = 1;
1077 printf("lapic%u:", apic_id);
1080 printf(" LINT%u trigger: %s\n", pin,
1081 trigger == INTR_TRIGGER_EDGE ? "edge" : "level");
1086 * Adjust the TPR of the current CPU so that it blocks all interrupts below
1087 * the passed in vector.
1090 lapic_set_tpr(u_int vector)
1093 lapic_write32(LAPIC_TPR, vector);
1097 tpr = lapic_read32(LAPIC_TPR) & ~APIC_TPR_PRIO;
1099 lapic_write32(LAPIC_TPR, tpr);
1104 native_lapic_eoi(void)
1107 lapic_write32_nofence(LAPIC_EOI, 0);
1111 lapic_handle_intr(int vector, struct trapframe *frame)
1113 struct intsrc *isrc;
1115 isrc = intr_lookup_source(apic_idt_to_irq(PCPU_GET(apic_id),
1117 intr_execute_handlers(isrc, frame);
1121 lapic_handle_timer(struct trapframe *frame)
1124 struct trapframe *oldframe;
1127 /* Send EOI first thing. */
1130 #if defined(SMP) && !defined(SCHED_ULE)
1132 * Don't do any accounting for the disabled HTT cores, since it
1133 * will provide misleading numbers for the userland.
1135 * No locking is necessary here, since even if we lose the race
1136 * when hlt_cpus_mask changes it is not a big deal, really.
1138 * Don't do that for ULE, since ULE doesn't consider hlt_cpus_mask
1139 * and unlike other schedulers it actually schedules threads to
1142 if (CPU_ISSET(PCPU_GET(cpuid), &hlt_cpus_mask))
1146 /* Look up our local APIC structure for the tick counters. */
1147 la = &lapics[PCPU_GET(apic_id)];
1148 (*la->la_timer_count)++;
1150 if (lapic_et.et_active) {
1152 td->td_intr_nesting_level++;
1153 oldframe = td->td_intr_frame;
1154 td->td_intr_frame = frame;
1155 lapic_et.et_event_cb(&lapic_et, lapic_et.et_arg);
1156 td->td_intr_frame = oldframe;
1157 td->td_intr_nesting_level--;
1163 lapic_timer_set_divisor(u_int divisor)
1166 KASSERT(powerof2(divisor), ("lapic: invalid divisor %u", divisor));
1167 KASSERT(ffs(divisor) <= sizeof(lapic_timer_divisors) /
1168 sizeof(u_int32_t), ("lapic: invalid divisor %u", divisor));
1169 lapic_write32(LAPIC_DCR_TIMER, lapic_timer_divisors[ffs(divisor) - 1]);
1173 lapic_timer_oneshot(struct lapic *la)
1177 value = la->lvt_timer_base;
1178 value &= ~(APIC_LVTT_TM | APIC_LVT_M);
1179 value |= APIC_LVTT_TM_ONE_SHOT;
1180 la->lvt_timer_last = value;
1181 lapic_write32(LAPIC_LVT_TIMER, value);
1182 lapic_write32(LAPIC_ICR_TIMER, la->la_timer_period);
1186 lapic_timer_oneshot_nointr(struct lapic *la, uint32_t count)
1190 value = la->lvt_timer_base;
1191 value &= ~APIC_LVTT_TM;
1192 value |= APIC_LVTT_TM_ONE_SHOT | APIC_LVT_M;
1193 la->lvt_timer_last = value;
1194 lapic_write32(LAPIC_LVT_TIMER, value);
1195 lapic_write32(LAPIC_ICR_TIMER, count);
1199 lapic_timer_periodic(struct lapic *la)
1203 value = la->lvt_timer_base;
1204 value &= ~(APIC_LVTT_TM | APIC_LVT_M);
1205 value |= APIC_LVTT_TM_PERIODIC;
1206 la->lvt_timer_last = value;
1207 lapic_write32(LAPIC_LVT_TIMER, value);
1208 lapic_write32(LAPIC_ICR_TIMER, la->la_timer_period);
1212 lapic_timer_deadline(struct lapic *la)
1216 value = la->lvt_timer_base;
1217 value &= ~(APIC_LVTT_TM | APIC_LVT_M);
1218 value |= APIC_LVTT_TM_TSCDLT;
1219 if (value != la->lvt_timer_last) {
1220 la->lvt_timer_last = value;
1221 lapic_write32_nofence(LAPIC_LVT_TIMER, value);
1225 wrmsr(MSR_TSC_DEADLINE, la->la_timer_period + rdtsc());
1229 lapic_timer_stop(struct lapic *la)
1233 if (la->la_timer_mode == LAT_MODE_DEADLINE) {
1234 wrmsr(MSR_TSC_DEADLINE, 0);
1237 value = la->lvt_timer_base;
1238 value &= ~APIC_LVTT_TM;
1239 value |= APIC_LVT_M;
1240 la->lvt_timer_last = value;
1241 lapic_write32(LAPIC_LVT_TIMER, value);
1246 lapic_handle_cmc(void)
1254 * Called from the mca_init() to activate the CMC interrupt if this CPU is
1255 * responsible for monitoring any MC banks for CMC events. Since mca_init()
1256 * is called prior to lapic_setup() during boot, this just needs to unmask
1257 * this CPU's LVT_CMCI entry.
1260 native_lapic_enable_cmc(void)
1265 if (!x2apic_mode && lapic_map == NULL)
1268 apic_id = PCPU_GET(apic_id);
1269 KASSERT(lapics[apic_id].la_present,
1270 ("%s: missing APIC %u", __func__, apic_id));
1271 lapics[apic_id].la_lvts[APIC_LVT_CMCI].lvt_masked = 0;
1272 lapics[apic_id].la_lvts[APIC_LVT_CMCI].lvt_active = 1;
1274 printf("lapic%u: CMCI unmasked\n", apic_id);
1278 lapic_handle_error(void)
1283 * Read the contents of the error status register. Write to
1284 * the register first before reading from it to force the APIC
1285 * to update its value to indicate any errors that have
1286 * occurred since the previous write to the register.
1288 lapic_write32(LAPIC_ESR, 0);
1289 esr = lapic_read32(LAPIC_ESR);
1291 printf("CPU%d: local APIC error 0x%x\n", PCPU_GET(cpuid), esr);
1296 native_apic_cpuid(u_int apic_id)
1299 return apic_cpuids[apic_id];
1305 /* Request a free IDT vector to be used by the specified IRQ. */
1307 native_apic_alloc_vector(u_int apic_id, u_int irq)
1311 KASSERT(irq < NUM_IO_INTS, ("Invalid IRQ %u", irq));
1314 * Search for a free vector. Currently we just use a very simple
1315 * algorithm to find the first free vector.
1317 mtx_lock_spin(&icu_lock);
1318 for (vector = 0; vector < APIC_NUM_IOINTS; vector++) {
1319 if (lapics[apic_id].la_ioint_irqs[vector] != -1)
1321 lapics[apic_id].la_ioint_irqs[vector] = irq;
1322 mtx_unlock_spin(&icu_lock);
1323 return (vector + APIC_IO_INTS);
1325 mtx_unlock_spin(&icu_lock);
1330 * Request 'count' free contiguous IDT vectors to be used by 'count'
1331 * IRQs. 'count' must be a power of two and the vectors will be
1332 * aligned on a boundary of 'align'. If the request cannot be
1333 * satisfied, 0 is returned.
1336 native_apic_alloc_vectors(u_int apic_id, u_int *irqs, u_int count, u_int align)
1338 u_int first, run, vector;
1340 KASSERT(powerof2(count), ("bad count"));
1341 KASSERT(powerof2(align), ("bad align"));
1342 KASSERT(align >= count, ("align < count"));
1344 for (run = 0; run < count; run++)
1345 KASSERT(irqs[run] < NUM_IO_INTS, ("Invalid IRQ %u at index %u",
1350 * Search for 'count' free vectors. As with apic_alloc_vector(),
1351 * this just uses a simple first fit algorithm.
1355 mtx_lock_spin(&icu_lock);
1356 for (vector = 0; vector < APIC_NUM_IOINTS; vector++) {
1358 /* Vector is in use, end run. */
1359 if (lapics[apic_id].la_ioint_irqs[vector] != -1) {
1365 /* Start a new run if run == 0 and vector is aligned. */
1367 if ((vector & (align - 1)) != 0)
1373 /* Keep looping if the run isn't long enough yet. */
1377 /* Found a run, assign IRQs and return the first vector. */
1378 for (vector = 0; vector < count; vector++)
1379 lapics[apic_id].la_ioint_irqs[first + vector] =
1381 mtx_unlock_spin(&icu_lock);
1382 return (first + APIC_IO_INTS);
1384 mtx_unlock_spin(&icu_lock);
1385 printf("APIC: Couldn't find APIC vectors for %u IRQs\n", count);
1390 * Enable a vector for a particular apic_id. Since all lapics share idt
1391 * entries and ioint_handlers this enables the vector on all lapics. lapics
1392 * which do not have the vector configured would report spurious interrupts
1396 native_apic_enable_vector(u_int apic_id, u_int vector)
1399 KASSERT(vector != IDT_SYSCALL, ("Attempt to overwrite syscall entry"));
1400 KASSERT(ioint_handlers[vector / 32] != NULL,
1401 ("No ISR handler for vector %u", vector));
1402 #ifdef KDTRACE_HOOKS
1403 KASSERT(vector != IDT_DTRACE_RET,
1404 ("Attempt to overwrite DTrace entry"));
1406 setidt(vector, ioint_handlers[vector / 32], SDT_APIC, SEL_KPL,
1411 native_apic_disable_vector(u_int apic_id, u_int vector)
1414 KASSERT(vector != IDT_SYSCALL, ("Attempt to overwrite syscall entry"));
1415 #ifdef KDTRACE_HOOKS
1416 KASSERT(vector != IDT_DTRACE_RET,
1417 ("Attempt to overwrite DTrace entry"));
1419 KASSERT(ioint_handlers[vector / 32] != NULL,
1420 ("No ISR handler for vector %u", vector));
1423 * We can not currently clear the idt entry because other cpus
1424 * may have a valid vector at this offset.
1426 setidt(vector, &IDTVEC(rsvd), SDT_APICT, SEL_KPL, GSEL_APIC);
1430 /* Release an APIC vector when it's no longer in use. */
1432 native_apic_free_vector(u_int apic_id, u_int vector, u_int irq)
1436 KASSERT(vector >= APIC_IO_INTS && vector != IDT_SYSCALL &&
1437 vector <= APIC_IO_INTS + APIC_NUM_IOINTS,
1438 ("Vector %u does not map to an IRQ line", vector));
1439 KASSERT(irq < NUM_IO_INTS, ("Invalid IRQ %u", irq));
1440 KASSERT(lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS] ==
1441 irq, ("IRQ mismatch"));
1442 #ifdef KDTRACE_HOOKS
1443 KASSERT(vector != IDT_DTRACE_RET,
1444 ("Attempt to overwrite DTrace entry"));
1448 * Bind us to the cpu that owned the vector before freeing it so
1449 * we don't lose an interrupt delivery race.
1454 if (sched_is_bound(td))
1455 panic("apic_free_vector: Thread already bound.\n");
1456 sched_bind(td, apic_cpuid(apic_id));
1459 mtx_lock_spin(&icu_lock);
1460 lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS] = -1;
1461 mtx_unlock_spin(&icu_lock);
1469 /* Map an IDT vector (APIC) to an IRQ (interrupt source). */
1471 apic_idt_to_irq(u_int apic_id, u_int vector)
1475 KASSERT(vector >= APIC_IO_INTS && vector != IDT_SYSCALL &&
1476 vector <= APIC_IO_INTS + APIC_NUM_IOINTS,
1477 ("Vector %u does not map to an IRQ line", vector));
1478 #ifdef KDTRACE_HOOKS
1479 KASSERT(vector != IDT_DTRACE_RET,
1480 ("Attempt to overwrite DTrace entry"));
1482 irq = lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS];
1490 * Dump data about APIC IDT vector mappings.
1492 DB_SHOW_COMMAND(apic, db_show_apic)
1494 struct intsrc *isrc;
1499 if (strcmp(modif, "vv") == 0)
1501 else if (strcmp(modif, "v") == 0)
1505 for (apic_id = 0; apic_id <= MAX_APIC_ID; apic_id++) {
1506 if (lapics[apic_id].la_present == 0)
1508 db_printf("Interrupts bound to lapic %u\n", apic_id);
1509 for (i = 0; i < APIC_NUM_IOINTS + 1 && !db_pager_quit; i++) {
1510 irq = lapics[apic_id].la_ioint_irqs[i];
1511 if (irq == -1 || irq == IRQ_SYSCALL)
1513 #ifdef KDTRACE_HOOKS
1514 if (irq == IRQ_DTRACE_RET)
1518 if (irq == IRQ_EVTCHN)
1521 db_printf("vec 0x%2x -> ", i + APIC_IO_INTS);
1522 if (irq == IRQ_TIMER)
1523 db_printf("lapic timer\n");
1524 else if (irq < NUM_IO_INTS) {
1525 isrc = intr_lookup_source(irq);
1526 if (isrc == NULL || verbose == 0)
1527 db_printf("IRQ %u\n", irq);
1529 db_dump_intr_event(isrc->is_event,
1532 db_printf("IRQ %u ???\n", irq);
1538 dump_mask(const char *prefix, uint32_t v, int base)
1543 for (i = 0; i < 32; i++)
1546 db_printf("%s:", prefix);
1549 db_printf(" %02x", base + i);
1555 /* Show info from the lapic regs for this CPU. */
1556 DB_SHOW_COMMAND(lapic, db_show_lapic)
1560 db_printf("lapic ID = %d\n", lapic_id());
1561 v = lapic_read32(LAPIC_VERSION);
1562 db_printf("version = %d.%d\n", (v & APIC_VER_VERSION) >> 4,
1564 db_printf("max LVT = %d\n", (v & APIC_VER_MAXLVT) >> MAXLVTSHIFT);
1565 v = lapic_read32(LAPIC_SVR);
1566 db_printf("SVR = %02x (%s)\n", v & APIC_SVR_VECTOR,
1567 v & APIC_SVR_ENABLE ? "enabled" : "disabled");
1568 db_printf("TPR = %02x\n", lapic_read32(LAPIC_TPR));
1570 #define dump_field(prefix, regn, index) \
1571 dump_mask(__XSTRING(prefix ## index), \
1572 lapic_read32(LAPIC_ ## regn ## index), \
1575 db_printf("In-service Interrupts:\n");
1576 dump_field(isr, ISR, 0);
1577 dump_field(isr, ISR, 1);
1578 dump_field(isr, ISR, 2);
1579 dump_field(isr, ISR, 3);
1580 dump_field(isr, ISR, 4);
1581 dump_field(isr, ISR, 5);
1582 dump_field(isr, ISR, 6);
1583 dump_field(isr, ISR, 7);
1585 db_printf("TMR Interrupts:\n");
1586 dump_field(tmr, TMR, 0);
1587 dump_field(tmr, TMR, 1);
1588 dump_field(tmr, TMR, 2);
1589 dump_field(tmr, TMR, 3);
1590 dump_field(tmr, TMR, 4);
1591 dump_field(tmr, TMR, 5);
1592 dump_field(tmr, TMR, 6);
1593 dump_field(tmr, TMR, 7);
1595 db_printf("IRR Interrupts:\n");
1596 dump_field(irr, IRR, 0);
1597 dump_field(irr, IRR, 1);
1598 dump_field(irr, IRR, 2);
1599 dump_field(irr, IRR, 3);
1600 dump_field(irr, IRR, 4);
1601 dump_field(irr, IRR, 5);
1602 dump_field(irr, IRR, 6);
1603 dump_field(irr, IRR, 7);
1610 * APIC probing support code. This includes code to manage enumerators.
1613 static SLIST_HEAD(, apic_enumerator) enumerators =
1614 SLIST_HEAD_INITIALIZER(enumerators);
1615 static struct apic_enumerator *best_enum;
1618 apic_register_enumerator(struct apic_enumerator *enumerator)
1621 struct apic_enumerator *apic_enum;
1623 SLIST_FOREACH(apic_enum, &enumerators, apic_next) {
1624 if (apic_enum == enumerator)
1625 panic("%s: Duplicate register of %s", __func__,
1626 enumerator->apic_name);
1629 SLIST_INSERT_HEAD(&enumerators, enumerator, apic_next);
1633 * We have to look for CPU's very, very early because certain subsystems
1634 * want to know how many CPU's we have extremely early on in the boot
1638 apic_init(void *dummy __unused)
1640 struct apic_enumerator *enumerator;
1643 /* We only support built in local APICs. */
1644 if (!(cpu_feature & CPUID_APIC))
1647 /* Don't probe if APIC mode is disabled. */
1648 if (resource_disabled("apic", 0))
1651 /* Probe all the enumerators to find the best match. */
1654 SLIST_FOREACH(enumerator, &enumerators, apic_next) {
1655 retval = enumerator->apic_probe();
1658 if (best_enum == NULL || best < retval) {
1659 best_enum = enumerator;
1663 if (best_enum == NULL) {
1665 printf("APIC: Could not find any APICs.\n");
1667 panic("running without device atpic requires a local APIC");
1673 printf("APIC: Using the %s enumerator.\n",
1674 best_enum->apic_name);
1678 * To work around an errata, we disable the local APIC on some
1679 * CPUs during early startup. We need to turn the local APIC back
1680 * on on such CPUs now.
1682 ppro_reenable_apic();
1685 /* Probe the CPU's in the system. */
1686 retval = best_enum->apic_probe_cpus();
1688 printf("%s: Failed to probe CPUs: returned %d\n",
1689 best_enum->apic_name, retval);
1692 SYSINIT(apic_init, SI_SUB_TUNABLES - 1, SI_ORDER_SECOND, apic_init, NULL);
1695 * Setup the local APIC. We have to do this prior to starting up the APs
1699 apic_setup_local(void *dummy __unused)
1703 if (best_enum == NULL)
1706 /* Initialize the local APIC. */
1707 retval = best_enum->apic_setup_local();
1709 printf("%s: Failed to setup the local APIC: returned %d\n",
1710 best_enum->apic_name, retval);
1712 SYSINIT(apic_setup_local, SI_SUB_CPU, SI_ORDER_SECOND, apic_setup_local, NULL);
1715 * Setup the I/O APICs.
1718 apic_setup_io(void *dummy __unused)
1722 if (best_enum == NULL)
1726 * Local APIC must be registered before other PICs and pseudo PICs
1727 * for proper suspend/resume order.
1729 intr_register_pic(&lapic_pic);
1731 retval = best_enum->apic_setup_io();
1733 printf("%s: Failed to setup I/O APICs: returned %d\n",
1734 best_enum->apic_name, retval);
1737 * Finish setting up the local APIC on the BSP once we know
1738 * how to properly program the LINT pins. In particular, this
1739 * enables the EOI suppression mode, if LAPIC support it and
1740 * user did not disabled the mode.
1746 /* Enable the MSI "pic". */
1747 init_ops.msi_init();
1749 SYSINIT(apic_setup_io, SI_SUB_INTR, SI_ORDER_THIRD, apic_setup_io, NULL);
1753 * Inter Processor Interrupt functions. The lapic_ipi_*() functions are
1754 * private to the MD code. The public interface for the rest of the
1755 * kernel is defined in mp_machdep.c.
1759 * Wait delay microseconds for IPI to be sent. If delay is -1, we
1763 native_lapic_ipi_wait(int delay)
1767 /* LAPIC_ICR.APIC_DELSTAT_MASK is undefined in x2APIC mode */
1771 for (rx = 0; delay == -1 || rx < lapic_ipi_wait_mult * delay; rx++) {
1772 if ((lapic_read_icr_lo() & APIC_DELSTAT_MASK) ==
1781 native_lapic_ipi_raw(register_t icrlo, u_int dest)
1785 register_t saveintr;
1787 /* XXX: Need more sanity checking of icrlo? */
1788 KASSERT(x2apic_mode || lapic_map != NULL,
1789 ("%s called too early", __func__));
1790 KASSERT(x2apic_mode ||
1791 (dest & ~(APIC_ID_MASK >> APIC_ID_SHIFT)) == 0,
1792 ("%s: invalid dest field", __func__));
1793 KASSERT((icrlo & APIC_ICRLO_RESV_MASK) == 0,
1794 ("%s: reserved bits set in ICR LO register", __func__));
1796 /* Set destination in ICR HI register if it is being used. */
1798 saveintr = intr_disable();
1799 icr = lapic_read_icr();
1802 if ((icrlo & APIC_DEST_MASK) == APIC_DEST_DESTFLD) {
1807 vhi &= ~APIC_ID_MASK;
1808 vhi |= dest << APIC_ID_SHIFT;
1814 /* Program the contents of the IPI and dispatch it. */
1819 vlo &= APIC_ICRLO_RESV_MASK;
1822 lapic_write_icr(vhi, vlo);
1824 intr_restore(saveintr);
1827 #define BEFORE_SPIN 50000
1828 #ifdef DETECT_DEADLOCK
1829 #define AFTER_SPIN 50
1833 native_lapic_ipi_vectored(u_int vector, int dest)
1835 register_t icrlo, destfield;
1837 KASSERT((vector & ~APIC_VECTOR_MASK) == 0,
1838 ("%s: invalid vector %d", __func__, vector));
1840 icrlo = APIC_DESTMODE_PHY | APIC_TRIGMOD_EDGE | APIC_LEVEL_ASSERT;
1843 * NMI IPIs are just fake vectors used to send a NMI. Use special rules
1844 * regarding NMIs if passed, otherwise specify the vector.
1846 if (vector >= IPI_NMI_FIRST)
1847 icrlo |= APIC_DELMODE_NMI;
1849 icrlo |= vector | APIC_DELMODE_FIXED;
1852 case APIC_IPI_DEST_SELF:
1853 icrlo |= APIC_DEST_SELF;
1855 case APIC_IPI_DEST_ALL:
1856 icrlo |= APIC_DEST_ALLISELF;
1858 case APIC_IPI_DEST_OTHERS:
1859 icrlo |= APIC_DEST_ALLESELF;
1862 KASSERT(x2apic_mode ||
1863 (dest & ~(APIC_ID_MASK >> APIC_ID_SHIFT)) == 0,
1864 ("%s: invalid destination 0x%x", __func__, dest));
1868 /* Wait for an earlier IPI to finish. */
1869 if (!lapic_ipi_wait(BEFORE_SPIN)) {
1870 if (panicstr != NULL)
1873 panic("APIC: Previous IPI is stuck");
1876 lapic_ipi_raw(icrlo, destfield);
1878 #ifdef DETECT_DEADLOCK
1879 /* Wait for IPI to be delivered. */
1880 if (!lapic_ipi_wait(AFTER_SPIN)) {
1881 #ifdef needsattention
1885 * The above function waits for the message to actually be
1886 * delivered. It breaks out after an arbitrary timeout
1887 * since the message should eventually be delivered (at
1888 * least in theory) and that if it wasn't we would catch
1889 * the failure with the check above when the next IPI is
1892 * We could skip this wait entirely, EXCEPT it probably
1893 * protects us from other routines that assume that the
1894 * message was delivered and acted upon when this function
1897 printf("APIC: IPI might be stuck\n");
1898 #else /* !needsattention */
1899 /* Wait until mesage is sent without a timeout. */
1900 while (lapic_read_icr_lo() & APIC_DELSTAT_PEND)
1902 #endif /* needsattention */
1904 #endif /* DETECT_DEADLOCK */
1910 * Since the IDT is shared by all CPUs the IPI slot update needs to be globally
1913 * Consider the case where an IPI is generated immediately after allocation:
1914 * vector = lapic_ipi_alloc(ipifunc);
1915 * ipi_selected(other_cpus, vector);
1917 * In xAPIC mode a write to ICR_LO has serializing semantics because the
1918 * APIC page is mapped as an uncached region. In x2APIC mode there is an
1919 * explicit 'mfence' before the ICR MSR is written. Therefore in both cases
1920 * the IDT slot update is globally visible before the IPI is delivered.
1923 native_lapic_ipi_alloc(inthand_t *ipifunc)
1925 struct gate_descriptor *ip;
1929 KASSERT(ipifunc != &IDTVEC(rsvd), ("invalid ipifunc %p", ipifunc));
1932 mtx_lock_spin(&icu_lock);
1933 for (idx = IPI_DYN_FIRST; idx <= IPI_DYN_LAST; idx++) {
1935 func = (ip->gd_hioffset << 16) | ip->gd_looffset;
1936 if (func == (uintptr_t)&IDTVEC(rsvd)) {
1938 setidt(vector, ipifunc, SDT_APIC, SEL_KPL, GSEL_APIC);
1942 mtx_unlock_spin(&icu_lock);
1947 native_lapic_ipi_free(int vector)
1949 struct gate_descriptor *ip;
1952 KASSERT(vector >= IPI_DYN_FIRST && vector <= IPI_DYN_LAST,
1953 ("%s: invalid vector %d", __func__, vector));
1955 mtx_lock_spin(&icu_lock);
1957 func = (ip->gd_hioffset << 16) | ip->gd_looffset;
1958 KASSERT(func != (uintptr_t)&IDTVEC(rsvd),
1959 ("invalid idtfunc %#lx", func));
1960 setidt(vector, &IDTVEC(rsvd), SDT_APICT, SEL_KPL, GSEL_APIC);
1961 mtx_unlock_spin(&icu_lock);