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;
175 static uint64_t lapic_ipi_wait_mult;
177 SYSCTL_NODE(_hw, OID_AUTO, apic, CTLFLAG_RD, 0, "APIC options");
178 SYSCTL_INT(_hw_apic, OID_AUTO, x2apic_mode, CTLFLAG_RD, &x2apic_mode, 0, "");
179 SYSCTL_INT(_hw_apic, OID_AUTO, eoi_suppression, CTLFLAG_RD,
180 &lapic_eoi_suppression, 0, "");
181 SYSCTL_INT(_hw_apic, OID_AUTO, timer_tsc_deadline, CTLFLAG_RD,
182 &lapic_timer_tsc_deadline, 0, "");
185 lapic_read32(enum LAPIC_REGISTERS reg)
190 res = rdmsr32(MSR_APIC_000 + reg);
192 res = *(volatile uint32_t *)(lapic_map + reg * LAPIC_MEM_MUL);
198 lapic_write32(enum LAPIC_REGISTERS reg, uint32_t val)
203 wrmsr(MSR_APIC_000 + reg, val);
205 *(volatile uint32_t *)(lapic_map + reg * LAPIC_MEM_MUL) = val;
210 lapic_write32_nofence(enum LAPIC_REGISTERS reg, uint32_t val)
214 wrmsr(MSR_APIC_000 + reg, val);
216 *(volatile uint32_t *)(lapic_map + reg * LAPIC_MEM_MUL) = val;
228 v = rdmsr(MSR_APIC_000 + LAPIC_ICR_LO);
230 vhi = lapic_read32(LAPIC_ICR_HI);
231 vlo = lapic_read32(LAPIC_ICR_LO);
232 v = ((uint64_t)vhi << 32) | vlo;
238 lapic_read_icr_lo(void)
241 return (lapic_read32(LAPIC_ICR_LO));
245 lapic_write_icr(uint32_t vhi, uint32_t vlo)
250 v = ((uint64_t)vhi << 32) | vlo;
252 wrmsr(MSR_APIC_000 + LAPIC_ICR_LO, v);
254 lapic_write32(LAPIC_ICR_HI, vhi);
255 lapic_write32(LAPIC_ICR_LO, vlo);
261 native_lapic_enable_x2apic(void)
265 apic_base = rdmsr(MSR_APICBASE);
266 apic_base |= APICBASE_X2APIC | APICBASE_ENABLED;
267 wrmsr(MSR_APICBASE, apic_base);
270 static void lapic_enable(void);
271 static void lapic_resume(struct pic *pic, bool suspend_cancelled);
272 static void lapic_timer_oneshot(struct lapic *);
273 static void lapic_timer_oneshot_nointr(struct lapic *, uint32_t);
274 static void lapic_timer_periodic(struct lapic *);
275 static void lapic_timer_deadline(struct lapic *);
276 static void lapic_timer_stop(struct lapic *);
277 static void lapic_timer_set_divisor(u_int divisor);
278 static uint32_t lvt_mode(struct lapic *la, u_int pin, uint32_t value);
279 static int lapic_et_start(struct eventtimer *et,
280 sbintime_t first, sbintime_t period);
281 static int lapic_et_stop(struct eventtimer *et);
282 static u_int apic_idt_to_irq(u_int apic_id, u_int vector);
283 static void lapic_set_tpr(u_int vector);
285 struct pic lapic_pic = { .pic_resume = lapic_resume };
287 /* Forward declarations for apic_ops */
288 static void native_lapic_create(u_int apic_id, int boot_cpu);
289 static void native_lapic_init(vm_paddr_t addr);
290 static void native_lapic_xapic_mode(void);
291 static void native_lapic_setup(int boot);
292 static void native_lapic_dump(const char *str);
293 static void native_lapic_disable(void);
294 static void native_lapic_eoi(void);
295 static int native_lapic_id(void);
296 static int native_lapic_intr_pending(u_int vector);
297 static u_int native_apic_cpuid(u_int apic_id);
298 static u_int native_apic_alloc_vector(u_int apic_id, u_int irq);
299 static u_int native_apic_alloc_vectors(u_int apic_id, u_int *irqs,
300 u_int count, u_int align);
301 static void native_apic_disable_vector(u_int apic_id, u_int vector);
302 static void native_apic_enable_vector(u_int apic_id, u_int vector);
303 static void native_apic_free_vector(u_int apic_id, u_int vector, u_int irq);
304 static void native_lapic_set_logical_id(u_int apic_id, u_int cluster,
306 static int native_lapic_enable_pmc(void);
307 static void native_lapic_disable_pmc(void);
308 static void native_lapic_reenable_pmc(void);
309 static void native_lapic_enable_cmc(void);
310 static int native_lapic_set_lvt_mask(u_int apic_id, u_int lvt,
312 static int native_lapic_set_lvt_mode(u_int apic_id, u_int lvt,
314 static int native_lapic_set_lvt_polarity(u_int apic_id, u_int lvt,
315 enum intr_polarity pol);
316 static int native_lapic_set_lvt_triggermode(u_int apic_id, u_int lvt,
317 enum intr_trigger trigger);
319 static void native_lapic_ipi_raw(register_t icrlo, u_int dest);
320 static void native_lapic_ipi_vectored(u_int vector, int dest);
321 static int native_lapic_ipi_wait(int delay);
322 static int native_lapic_ipi_alloc(inthand_t *ipifunc);
323 static void native_lapic_ipi_free(int vector);
326 struct apic_ops apic_ops = {
327 .create = native_lapic_create,
328 .init = native_lapic_init,
329 .xapic_mode = native_lapic_xapic_mode,
330 .setup = native_lapic_setup,
331 .dump = native_lapic_dump,
332 .disable = native_lapic_disable,
333 .eoi = native_lapic_eoi,
334 .id = native_lapic_id,
335 .intr_pending = native_lapic_intr_pending,
336 .set_logical_id = native_lapic_set_logical_id,
337 .cpuid = native_apic_cpuid,
338 .alloc_vector = native_apic_alloc_vector,
339 .alloc_vectors = native_apic_alloc_vectors,
340 .enable_vector = native_apic_enable_vector,
341 .disable_vector = native_apic_disable_vector,
342 .free_vector = native_apic_free_vector,
343 .enable_pmc = native_lapic_enable_pmc,
344 .disable_pmc = native_lapic_disable_pmc,
345 .reenable_pmc = native_lapic_reenable_pmc,
346 .enable_cmc = native_lapic_enable_cmc,
348 .ipi_raw = native_lapic_ipi_raw,
349 .ipi_vectored = native_lapic_ipi_vectored,
350 .ipi_wait = native_lapic_ipi_wait,
351 .ipi_alloc = native_lapic_ipi_alloc,
352 .ipi_free = native_lapic_ipi_free,
354 .set_lvt_mask = native_lapic_set_lvt_mask,
355 .set_lvt_mode = native_lapic_set_lvt_mode,
356 .set_lvt_polarity = native_lapic_set_lvt_polarity,
357 .set_lvt_triggermode = native_lapic_set_lvt_triggermode,
361 lvt_mode(struct lapic *la, u_int pin, uint32_t value)
365 KASSERT(pin <= APIC_LVT_MAX, ("%s: pin %u out of range", __func__, pin));
366 if (la->la_lvts[pin].lvt_active)
367 lvt = &la->la_lvts[pin];
371 value &= ~(APIC_LVT_M | APIC_LVT_TM | APIC_LVT_IIPP | APIC_LVT_DM |
373 if (lvt->lvt_edgetrigger == 0)
374 value |= APIC_LVT_TM;
375 if (lvt->lvt_activehi == 0)
376 value |= APIC_LVT_IIPP_INTALO;
379 value |= lvt->lvt_mode;
380 switch (lvt->lvt_mode) {
381 case APIC_LVT_DM_NMI:
382 case APIC_LVT_DM_SMI:
383 case APIC_LVT_DM_INIT:
384 case APIC_LVT_DM_EXTINT:
385 if (!lvt->lvt_edgetrigger && bootverbose) {
386 printf("lapic%u: Forcing LINT%u to edge trigger\n",
388 value |= APIC_LVT_TM;
390 /* Use a vector of 0. */
392 case APIC_LVT_DM_FIXED:
393 value |= lvt->lvt_vector;
396 panic("bad APIC LVT delivery mode: %#x\n", value);
402 * Map the local APIC and setup necessary interrupt vectors.
405 native_lapic_init(vm_paddr_t addr)
413 * Enable x2APIC mode if possible. Map the local APIC
416 * Keep the LAPIC registers page mapped uncached for x2APIC
417 * mode too, to have direct map page attribute set to
418 * uncached. This is needed to work around CPU errata present
419 * on all Intel processors.
421 KASSERT(trunc_page(addr) == addr,
422 ("local APIC not aligned on a page boundary"));
424 lapic_map = pmap_mapdev(addr, PAGE_SIZE);
426 native_lapic_enable_x2apic();
430 /* Setup the spurious interrupt handler. */
431 setidt(APIC_SPURIOUS_INT, IDTVEC(spuriousint), SDT_APIC, SEL_KPL,
434 /* Perform basic initialization of the BSP's local APIC. */
437 /* Set BSP's per-CPU local APIC ID. */
438 PCPU_SET(apic_id, lapic_id());
440 /* Local APIC timer interrupt. */
441 setidt(APIC_TIMER_INT, IDTVEC(timerint), SDT_APIC, SEL_KPL, GSEL_APIC);
443 /* Local APIC error interrupt. */
444 setidt(APIC_ERROR_INT, IDTVEC(errorint), SDT_APIC, SEL_KPL, GSEL_APIC);
446 /* XXX: Thermal interrupt */
448 /* Local APIC CMCI. */
449 setidt(APIC_CMC_INT, IDTVEC(cmcint), SDT_APICT, SEL_KPL, GSEL_APIC);
451 if ((resource_int_value("apic", 0, "clock", &i) != 0 || i != 0)) {
453 /* Intel CPUID 0x06 EAX[2] set if APIC timer runs in C3. */
454 if (cpu_vendor_id == CPU_VENDOR_INTEL && cpu_high >= 6) {
455 do_cpuid(0x06, regs);
456 if ((regs[0] & CPUTPM1_ARAT) != 0)
459 bzero(&lapic_et, sizeof(lapic_et));
460 lapic_et.et_name = "LAPIC";
461 lapic_et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT |
463 lapic_et.et_quality = 600;
465 lapic_et.et_flags |= ET_FLAGS_C3STOP;
466 lapic_et.et_quality -= 200;
467 } else if ((cpu_feature & CPUID_TSC) != 0 &&
468 (cpu_feature2 & CPUID2_TSCDLT) != 0 &&
469 tsc_is_invariant && tsc_freq != 0) {
470 lapic_timer_tsc_deadline = 1;
471 TUNABLE_INT_FETCH("hw.lapic_tsc_deadline",
472 &lapic_timer_tsc_deadline);
475 lapic_et.et_frequency = 0;
476 /* We don't know frequency yet, so trying to guess. */
477 lapic_et.et_min_period = 0x00001000LL;
478 lapic_et.et_max_period = SBT_1S;
479 lapic_et.et_start = lapic_et_start;
480 lapic_et.et_stop = lapic_et_stop;
481 lapic_et.et_priv = NULL;
482 et_register(&lapic_et);
486 * Set lapic_eoi_suppression after lapic_enable(), to not
487 * enable suppression in the hardware prematurely. Note that
488 * we by default enable suppression even when system only has
489 * one IO-APIC, since EOI is broadcasted to all APIC agents,
490 * including CPUs, otherwise.
492 * It seems that at least some KVM versions report
493 * EOI_SUPPRESSION bit, but auto-EOI does not work.
495 ver = lapic_read32(LAPIC_VERSION);
496 if ((ver & APIC_VER_EOI_SUPPRESSION) != 0) {
497 lapic_eoi_suppression = 1;
498 if (vm_guest == VM_GUEST_VM &&
499 !strcmp(hv_vendor, "KVMKVMKVM")) {
502 "KVM -- disabling lapic eoi suppression\n");
503 lapic_eoi_suppression = 0;
505 TUNABLE_INT_FETCH("hw.lapic_eoi_suppression",
506 &lapic_eoi_suppression);
509 #define LOOPS 1000000
511 * Calibrate the busy loop waiting for IPI ack in xAPIC mode.
512 * lapic_ipi_wait_mult contains the number of iterations which
513 * approximately delay execution for 1 microsecond (the
514 * argument to native_lapic_ipi_wait() is in microseconds).
516 * We assume that TSC is present and already measured.
517 * Possible TSC frequency jumps are irrelevant to the
518 * calibration loop below, the CPU clock management code is
519 * not yet started, and we do not enter sleep states.
521 KASSERT((cpu_feature & CPUID_TSC) != 0 && tsc_freq != 0,
522 ("TSC not initialized"));
524 for (r = 0; r < LOOPS; r++) {
525 (void)lapic_read_icr_lo();
529 lapic_ipi_wait_mult = (r * 1000000) / tsc_freq / LOOPS;
531 printf("LAPIC: ipi_wait() us multiplier %jd (r %jd tsc %jd)\n",
532 (uintmax_t)lapic_ipi_wait_mult, (uintmax_t)r,
533 (uintmax_t)tsc_freq);
539 * Create a local APIC instance.
542 native_lapic_create(u_int apic_id, int boot_cpu)
546 if (apic_id > MAX_APIC_ID) {
547 printf("APIC: Ignoring local APIC with ID %d\n", apic_id);
549 panic("Can't ignore BSP");
552 KASSERT(!lapics[apic_id].la_present, ("duplicate local APIC %u",
556 * Assume no local LVT overrides and a cluster of 0 and
557 * intra-cluster ID of 0.
559 lapics[apic_id].la_present = 1;
560 lapics[apic_id].la_id = apic_id;
561 for (i = 0; i <= APIC_LVT_MAX; i++) {
562 lapics[apic_id].la_lvts[i] = lvts[i];
563 lapics[apic_id].la_lvts[i].lvt_active = 0;
565 for (i = 0; i <= APIC_NUM_IOINTS; i++)
566 lapics[apic_id].la_ioint_irqs[i] = -1;
567 lapics[apic_id].la_ioint_irqs[IDT_SYSCALL - APIC_IO_INTS] = IRQ_SYSCALL;
568 lapics[apic_id].la_ioint_irqs[APIC_TIMER_INT - APIC_IO_INTS] =
571 lapics[apic_id].la_ioint_irqs[IDT_DTRACE_RET - APIC_IO_INTS] =
575 lapics[apic_id].la_ioint_irqs[IDT_EVTCHN - APIC_IO_INTS] = IRQ_EVTCHN;
580 cpu_add(apic_id, boot_cpu);
585 * Dump contents of local APIC registers
588 native_lapic_dump(const char* str)
592 maxlvt = (lapic_read32(LAPIC_VERSION) & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
593 printf("cpu%d %s:\n", PCPU_GET(cpuid), str);
594 printf(" ID: 0x%08x VER: 0x%08x LDR: 0x%08x DFR: 0x%08x",
595 lapic_read32(LAPIC_ID), lapic_read32(LAPIC_VERSION),
596 lapic_read32(LAPIC_LDR), x2apic_mode ? 0 : lapic_read32(LAPIC_DFR));
597 if ((cpu_feature2 & CPUID2_X2APIC) != 0)
598 printf(" x2APIC: %d", x2apic_mode);
599 printf("\n lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
600 lapic_read32(LAPIC_LVT_LINT0), lapic_read32(LAPIC_LVT_LINT1),
601 lapic_read32(LAPIC_TPR), lapic_read32(LAPIC_SVR));
602 printf(" timer: 0x%08x therm: 0x%08x err: 0x%08x",
603 lapic_read32(LAPIC_LVT_TIMER), lapic_read32(LAPIC_LVT_THERMAL),
604 lapic_read32(LAPIC_LVT_ERROR));
605 if (maxlvt >= APIC_LVT_PMC)
606 printf(" pmc: 0x%08x", lapic_read32(LAPIC_LVT_PCINT));
608 if (maxlvt >= APIC_LVT_CMCI)
609 printf(" cmci: 0x%08x\n", lapic_read32(LAPIC_LVT_CMCI));
613 native_lapic_xapic_mode(void)
617 saveintr = intr_disable();
619 native_lapic_enable_x2apic();
620 intr_restore(saveintr);
624 native_lapic_setup(int boot)
629 char buf[MAXCOMLEN + 1];
631 saveintr = intr_disable();
633 la = &lapics[lapic_id()];
634 KASSERT(la->la_present, ("missing APIC structure"));
635 maxlvt = (lapic_read32(LAPIC_VERSION) & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
637 /* Initialize the TPR to allow all interrupts. */
640 /* Setup spurious vector and enable the local APIC. */
643 /* Program LINT[01] LVT entries. */
644 lapic_write32(LAPIC_LVT_LINT0, lvt_mode(la, APIC_LVT_LINT0,
645 lapic_read32(LAPIC_LVT_LINT0)));
646 lapic_write32(LAPIC_LVT_LINT1, lvt_mode(la, APIC_LVT_LINT1,
647 lapic_read32(LAPIC_LVT_LINT1)));
649 /* Program the PMC LVT entry if present. */
650 if (maxlvt >= APIC_LVT_PMC) {
651 lapic_write32(LAPIC_LVT_PCINT, lvt_mode(la, APIC_LVT_PMC,
655 /* Program timer LVT and setup handler. */
656 la->lvt_timer_base = lvt_mode(la, APIC_LVT_TIMER,
657 lapic_read32(LAPIC_LVT_TIMER));
658 la->lvt_timer_last = la->lvt_timer_base;
659 lapic_write32(LAPIC_LVT_TIMER, la->lvt_timer_base);
661 snprintf(buf, sizeof(buf), "cpu%d:timer", PCPU_GET(cpuid));
662 intrcnt_add(buf, &la->la_timer_count);
665 /* Setup the timer if configured. */
666 if (la->la_timer_mode != LAT_MODE_UNDEF) {
667 KASSERT(la->la_timer_period != 0, ("lapic%u: zero divisor",
669 switch (la->la_timer_mode) {
670 case LAT_MODE_PERIODIC:
671 lapic_timer_set_divisor(lapic_timer_divisor);
672 lapic_timer_periodic(la);
674 case LAT_MODE_ONESHOT:
675 lapic_timer_set_divisor(lapic_timer_divisor);
676 lapic_timer_oneshot(la);
678 case LAT_MODE_DEADLINE:
679 lapic_timer_deadline(la);
682 panic("corrupted la_timer_mode %p %d", la,
687 /* Program error LVT and clear any existing errors. */
688 lapic_write32(LAPIC_LVT_ERROR, lvt_mode(la, APIC_LVT_ERROR,
689 lapic_read32(LAPIC_LVT_ERROR)));
690 lapic_write32(LAPIC_ESR, 0);
692 /* XXX: Thermal LVT */
694 /* Program the CMCI LVT entry if present. */
695 if (maxlvt >= APIC_LVT_CMCI) {
696 lapic_write32(LAPIC_LVT_CMCI, lvt_mode(la, APIC_LVT_CMCI,
697 lapic_read32(LAPIC_LVT_CMCI)));
700 intr_restore(saveintr);
704 native_lapic_reenable_pmc(void)
709 value = lapic_read32(LAPIC_LVT_PCINT);
710 value &= ~APIC_LVT_M;
711 lapic_write32(LAPIC_LVT_PCINT, value);
717 lapic_update_pmc(void *dummy)
721 la = &lapics[lapic_id()];
722 lapic_write32(LAPIC_LVT_PCINT, lvt_mode(la, APIC_LVT_PMC,
723 lapic_read32(LAPIC_LVT_PCINT)));
728 native_lapic_enable_pmc(void)
733 /* Fail if the local APIC is not present. */
734 if (!x2apic_mode && lapic_map == NULL)
737 /* Fail if the PMC LVT is not present. */
738 maxlvt = (lapic_read32(LAPIC_VERSION) & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
739 if (maxlvt < APIC_LVT_PMC)
742 lvts[APIC_LVT_PMC].lvt_masked = 0;
746 * If hwpmc was loaded at boot time then the APs may not be
747 * started yet. In that case, don't forward the request to
748 * them as they will program the lvt when they start.
751 smp_rendezvous(NULL, lapic_update_pmc, NULL, NULL);
754 lapic_update_pmc(NULL);
762 native_lapic_disable_pmc(void)
767 /* Fail if the local APIC is not present. */
768 if (!x2apic_mode && lapic_map == NULL)
771 /* Fail if the PMC LVT is not present. */
772 maxlvt = (lapic_read32(LAPIC_VERSION) & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
773 if (maxlvt < APIC_LVT_PMC)
776 lvts[APIC_LVT_PMC].lvt_masked = 1;
779 /* The APs should always be started when hwpmc is unloaded. */
780 KASSERT(mp_ncpus == 1 || smp_started, ("hwpmc unloaded too early"));
782 smp_rendezvous(NULL, lapic_update_pmc, NULL, NULL);
787 lapic_calibrate_initcount(struct eventtimer *et, struct lapic *la)
791 /* Start off with a divisor of 2 (power on reset default). */
792 lapic_timer_divisor = 2;
793 /* Try to calibrate the local APIC timer. */
795 lapic_timer_set_divisor(lapic_timer_divisor);
796 lapic_timer_oneshot_nointr(la, APIC_TIMER_MAX_COUNT);
798 value = APIC_TIMER_MAX_COUNT - lapic_read32(LAPIC_CCR_TIMER);
799 if (value != APIC_TIMER_MAX_COUNT)
801 lapic_timer_divisor <<= 1;
802 } while (lapic_timer_divisor <= 128);
803 if (lapic_timer_divisor > 128)
804 panic("lapic: Divisor too big");
806 printf("lapic: Divisor %lu, Frequency %lu Hz\n",
807 lapic_timer_divisor, value);
809 et->et_frequency = value;
813 lapic_calibrate_deadline(struct eventtimer *et, struct lapic *la __unused)
816 et->et_frequency = tsc_freq;
818 printf("lapic: deadline tsc mode, Frequency %ju Hz\n",
819 (uintmax_t)et->et_frequency);
824 lapic_et_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
828 la = &lapics[PCPU_GET(apic_id)];
829 if (et->et_frequency == 0) {
830 if (lapic_timer_tsc_deadline)
831 lapic_calibrate_deadline(et, la);
833 lapic_calibrate_initcount(et, la);
834 et->et_min_period = (0x00000002LLU << 32) / et->et_frequency;
835 et->et_max_period = (0xfffffffeLLU << 32) / et->et_frequency;
838 if (la->la_timer_mode == LAT_MODE_UNDEF)
839 lapic_timer_set_divisor(lapic_timer_divisor);
840 la->la_timer_mode = LAT_MODE_PERIODIC;
841 la->la_timer_period = ((uint32_t)et->et_frequency * period) >>
843 lapic_timer_periodic(la);
844 } else if (lapic_timer_tsc_deadline) {
845 la->la_timer_mode = LAT_MODE_DEADLINE;
846 la->la_timer_period = (et->et_frequency * first) >> 32;
847 lapic_timer_deadline(la);
849 if (la->la_timer_mode == LAT_MODE_UNDEF)
850 lapic_timer_set_divisor(lapic_timer_divisor);
851 la->la_timer_mode = LAT_MODE_ONESHOT;
852 la->la_timer_period = ((uint32_t)et->et_frequency * first) >>
854 lapic_timer_oneshot(la);
860 lapic_et_stop(struct eventtimer *et)
864 la = &lapics[PCPU_GET(apic_id)];
865 lapic_timer_stop(la);
866 la->la_timer_mode = LAT_MODE_UNDEF;
871 native_lapic_disable(void)
875 /* Software disable the local APIC. */
876 value = lapic_read32(LAPIC_SVR);
877 value &= ~APIC_SVR_SWEN;
878 lapic_write32(LAPIC_SVR, value);
886 /* Program the spurious vector to enable the local APIC. */
887 value = lapic_read32(LAPIC_SVR);
888 value &= ~(APIC_SVR_VECTOR | APIC_SVR_FOCUS);
889 value |= APIC_SVR_FEN | APIC_SVR_SWEN | APIC_SPURIOUS_INT;
890 if (lapic_eoi_suppression)
891 value |= APIC_SVR_EOI_SUPPRESSION;
892 lapic_write32(LAPIC_SVR, value);
895 /* Reset the local APIC on the BSP during resume. */
897 lapic_resume(struct pic *pic, bool suspend_cancelled)
904 native_lapic_id(void)
908 KASSERT(x2apic_mode || lapic_map != NULL, ("local APIC is not mapped"));
909 v = lapic_read32(LAPIC_ID);
916 native_lapic_intr_pending(u_int vector)
921 * The IRR registers are an array of registers each of which
922 * only describes 32 interrupts in the low 32 bits. Thus, we
923 * divide the vector by 32 to get the register index.
924 * Finally, we modulus the vector by 32 to determine the
925 * individual bit to test.
927 irr = lapic_read32(LAPIC_IRR0 + vector / 32);
928 return (irr & 1 << (vector % 32));
932 native_lapic_set_logical_id(u_int apic_id, u_int cluster, u_int cluster_id)
936 KASSERT(lapics[apic_id].la_present, ("%s: APIC %u doesn't exist",
938 KASSERT(cluster <= APIC_MAX_CLUSTER, ("%s: cluster %u too big",
940 KASSERT(cluster_id <= APIC_MAX_INTRACLUSTER_ID,
941 ("%s: intra cluster id %u too big", __func__, cluster_id));
942 la = &lapics[apic_id];
943 la->la_cluster = cluster;
944 la->la_cluster_id = cluster_id;
948 native_lapic_set_lvt_mask(u_int apic_id, u_int pin, u_char masked)
951 if (pin > APIC_LVT_MAX)
953 if (apic_id == APIC_ID_ALL) {
954 lvts[pin].lvt_masked = masked;
958 KASSERT(lapics[apic_id].la_present,
959 ("%s: missing APIC %u", __func__, apic_id));
960 lapics[apic_id].la_lvts[pin].lvt_masked = masked;
961 lapics[apic_id].la_lvts[pin].lvt_active = 1;
963 printf("lapic%u:", apic_id);
966 printf(" LINT%u %s\n", pin, masked ? "masked" : "unmasked");
971 native_lapic_set_lvt_mode(u_int apic_id, u_int pin, u_int32_t mode)
975 if (pin > APIC_LVT_MAX)
977 if (apic_id == APIC_ID_ALL) {
982 KASSERT(lapics[apic_id].la_present,
983 ("%s: missing APIC %u", __func__, apic_id));
984 lvt = &lapics[apic_id].la_lvts[pin];
987 printf("lapic%u:", apic_id);
989 lvt->lvt_mode = mode;
991 case APIC_LVT_DM_NMI:
992 case APIC_LVT_DM_SMI:
993 case APIC_LVT_DM_INIT:
994 case APIC_LVT_DM_EXTINT:
995 lvt->lvt_edgetrigger = 1;
996 lvt->lvt_activehi = 1;
997 if (mode == APIC_LVT_DM_EXTINT)
1000 lvt->lvt_masked = 0;
1003 panic("Unsupported delivery mode: 0x%x\n", mode);
1006 printf(" Routing ");
1008 case APIC_LVT_DM_NMI:
1011 case APIC_LVT_DM_SMI:
1014 case APIC_LVT_DM_INIT:
1017 case APIC_LVT_DM_EXTINT:
1021 printf(" -> LINT%u\n", pin);
1027 native_lapic_set_lvt_polarity(u_int apic_id, u_int pin, enum intr_polarity pol)
1030 if (pin > APIC_LVT_MAX || pol == INTR_POLARITY_CONFORM)
1032 if (apic_id == APIC_ID_ALL) {
1033 lvts[pin].lvt_activehi = (pol == INTR_POLARITY_HIGH);
1037 KASSERT(lapics[apic_id].la_present,
1038 ("%s: missing APIC %u", __func__, apic_id));
1039 lapics[apic_id].la_lvts[pin].lvt_active = 1;
1040 lapics[apic_id].la_lvts[pin].lvt_activehi =
1041 (pol == INTR_POLARITY_HIGH);
1043 printf("lapic%u:", apic_id);
1046 printf(" LINT%u polarity: %s\n", pin,
1047 pol == INTR_POLARITY_HIGH ? "high" : "low");
1052 native_lapic_set_lvt_triggermode(u_int apic_id, u_int pin,
1053 enum intr_trigger trigger)
1056 if (pin > APIC_LVT_MAX || trigger == INTR_TRIGGER_CONFORM)
1058 if (apic_id == APIC_ID_ALL) {
1059 lvts[pin].lvt_edgetrigger = (trigger == INTR_TRIGGER_EDGE);
1063 KASSERT(lapics[apic_id].la_present,
1064 ("%s: missing APIC %u", __func__, apic_id));
1065 lapics[apic_id].la_lvts[pin].lvt_edgetrigger =
1066 (trigger == INTR_TRIGGER_EDGE);
1067 lapics[apic_id].la_lvts[pin].lvt_active = 1;
1069 printf("lapic%u:", apic_id);
1072 printf(" LINT%u trigger: %s\n", pin,
1073 trigger == INTR_TRIGGER_EDGE ? "edge" : "level");
1078 * Adjust the TPR of the current CPU so that it blocks all interrupts below
1079 * the passed in vector.
1082 lapic_set_tpr(u_int vector)
1085 lapic_write32(LAPIC_TPR, vector);
1089 tpr = lapic_read32(LAPIC_TPR) & ~APIC_TPR_PRIO;
1091 lapic_write32(LAPIC_TPR, tpr);
1096 native_lapic_eoi(void)
1099 lapic_write32_nofence(LAPIC_EOI, 0);
1103 lapic_handle_intr(int vector, struct trapframe *frame)
1105 struct intsrc *isrc;
1107 isrc = intr_lookup_source(apic_idt_to_irq(PCPU_GET(apic_id),
1109 intr_execute_handlers(isrc, frame);
1113 lapic_handle_timer(struct trapframe *frame)
1116 struct trapframe *oldframe;
1119 /* Send EOI first thing. */
1122 #if defined(SMP) && !defined(SCHED_ULE)
1124 * Don't do any accounting for the disabled HTT cores, since it
1125 * will provide misleading numbers for the userland.
1127 * No locking is necessary here, since even if we lose the race
1128 * when hlt_cpus_mask changes it is not a big deal, really.
1130 * Don't do that for ULE, since ULE doesn't consider hlt_cpus_mask
1131 * and unlike other schedulers it actually schedules threads to
1134 if (CPU_ISSET(PCPU_GET(cpuid), &hlt_cpus_mask))
1138 /* Look up our local APIC structure for the tick counters. */
1139 la = &lapics[PCPU_GET(apic_id)];
1140 (*la->la_timer_count)++;
1142 if (lapic_et.et_active) {
1144 td->td_intr_nesting_level++;
1145 oldframe = td->td_intr_frame;
1146 td->td_intr_frame = frame;
1147 lapic_et.et_event_cb(&lapic_et, lapic_et.et_arg);
1148 td->td_intr_frame = oldframe;
1149 td->td_intr_nesting_level--;
1155 lapic_timer_set_divisor(u_int divisor)
1158 KASSERT(powerof2(divisor), ("lapic: invalid divisor %u", divisor));
1159 KASSERT(ffs(divisor) <= sizeof(lapic_timer_divisors) /
1160 sizeof(u_int32_t), ("lapic: invalid divisor %u", divisor));
1161 lapic_write32(LAPIC_DCR_TIMER, lapic_timer_divisors[ffs(divisor) - 1]);
1165 lapic_timer_oneshot(struct lapic *la)
1169 value = la->lvt_timer_base;
1170 value &= ~(APIC_LVTT_TM | APIC_LVT_M);
1171 value |= APIC_LVTT_TM_ONE_SHOT;
1172 la->lvt_timer_last = value;
1173 lapic_write32(LAPIC_LVT_TIMER, value);
1174 lapic_write32(LAPIC_ICR_TIMER, la->la_timer_period);
1178 lapic_timer_oneshot_nointr(struct lapic *la, uint32_t count)
1182 value = la->lvt_timer_base;
1183 value &= ~APIC_LVTT_TM;
1184 value |= APIC_LVTT_TM_ONE_SHOT | APIC_LVT_M;
1185 la->lvt_timer_last = value;
1186 lapic_write32(LAPIC_LVT_TIMER, value);
1187 lapic_write32(LAPIC_ICR_TIMER, count);
1191 lapic_timer_periodic(struct lapic *la)
1195 value = la->lvt_timer_base;
1196 value &= ~(APIC_LVTT_TM | APIC_LVT_M);
1197 value |= APIC_LVTT_TM_PERIODIC;
1198 la->lvt_timer_last = value;
1199 lapic_write32(LAPIC_LVT_TIMER, value);
1200 lapic_write32(LAPIC_ICR_TIMER, la->la_timer_period);
1204 lapic_timer_deadline(struct lapic *la)
1208 value = la->lvt_timer_base;
1209 value &= ~(APIC_LVTT_TM | APIC_LVT_M);
1210 value |= APIC_LVTT_TM_TSCDLT;
1211 if (value != la->lvt_timer_last) {
1212 la->lvt_timer_last = value;
1213 lapic_write32_nofence(LAPIC_LVT_TIMER, value);
1217 wrmsr(MSR_TSC_DEADLINE, la->la_timer_period + rdtsc());
1221 lapic_timer_stop(struct lapic *la)
1225 if (la->la_timer_mode == LAT_MODE_DEADLINE) {
1226 wrmsr(MSR_TSC_DEADLINE, 0);
1229 value = la->lvt_timer_base;
1230 value &= ~APIC_LVTT_TM;
1231 value |= APIC_LVT_M;
1232 la->lvt_timer_last = value;
1233 lapic_write32(LAPIC_LVT_TIMER, value);
1238 lapic_handle_cmc(void)
1246 * Called from the mca_init() to activate the CMC interrupt if this CPU is
1247 * responsible for monitoring any MC banks for CMC events. Since mca_init()
1248 * is called prior to lapic_setup() during boot, this just needs to unmask
1249 * this CPU's LVT_CMCI entry.
1252 native_lapic_enable_cmc(void)
1257 if (!x2apic_mode && lapic_map == NULL)
1260 apic_id = PCPU_GET(apic_id);
1261 KASSERT(lapics[apic_id].la_present,
1262 ("%s: missing APIC %u", __func__, apic_id));
1263 lapics[apic_id].la_lvts[APIC_LVT_CMCI].lvt_masked = 0;
1264 lapics[apic_id].la_lvts[APIC_LVT_CMCI].lvt_active = 1;
1266 printf("lapic%u: CMCI unmasked\n", apic_id);
1270 lapic_handle_error(void)
1275 * Read the contents of the error status register. Write to
1276 * the register first before reading from it to force the APIC
1277 * to update its value to indicate any errors that have
1278 * occurred since the previous write to the register.
1280 lapic_write32(LAPIC_ESR, 0);
1281 esr = lapic_read32(LAPIC_ESR);
1283 printf("CPU%d: local APIC error 0x%x\n", PCPU_GET(cpuid), esr);
1288 native_apic_cpuid(u_int apic_id)
1291 return apic_cpuids[apic_id];
1297 /* Request a free IDT vector to be used by the specified IRQ. */
1299 native_apic_alloc_vector(u_int apic_id, u_int irq)
1303 KASSERT(irq < NUM_IO_INTS, ("Invalid IRQ %u", irq));
1306 * Search for a free vector. Currently we just use a very simple
1307 * algorithm to find the first free vector.
1309 mtx_lock_spin(&icu_lock);
1310 for (vector = 0; vector < APIC_NUM_IOINTS; vector++) {
1311 if (lapics[apic_id].la_ioint_irqs[vector] != -1)
1313 lapics[apic_id].la_ioint_irqs[vector] = irq;
1314 mtx_unlock_spin(&icu_lock);
1315 return (vector + APIC_IO_INTS);
1317 mtx_unlock_spin(&icu_lock);
1322 * Request 'count' free contiguous IDT vectors to be used by 'count'
1323 * IRQs. 'count' must be a power of two and the vectors will be
1324 * aligned on a boundary of 'align'. If the request cannot be
1325 * satisfied, 0 is returned.
1328 native_apic_alloc_vectors(u_int apic_id, u_int *irqs, u_int count, u_int align)
1330 u_int first, run, vector;
1332 KASSERT(powerof2(count), ("bad count"));
1333 KASSERT(powerof2(align), ("bad align"));
1334 KASSERT(align >= count, ("align < count"));
1336 for (run = 0; run < count; run++)
1337 KASSERT(irqs[run] < NUM_IO_INTS, ("Invalid IRQ %u at index %u",
1342 * Search for 'count' free vectors. As with apic_alloc_vector(),
1343 * this just uses a simple first fit algorithm.
1347 mtx_lock_spin(&icu_lock);
1348 for (vector = 0; vector < APIC_NUM_IOINTS; vector++) {
1350 /* Vector is in use, end run. */
1351 if (lapics[apic_id].la_ioint_irqs[vector] != -1) {
1357 /* Start a new run if run == 0 and vector is aligned. */
1359 if ((vector & (align - 1)) != 0)
1365 /* Keep looping if the run isn't long enough yet. */
1369 /* Found a run, assign IRQs and return the first vector. */
1370 for (vector = 0; vector < count; vector++)
1371 lapics[apic_id].la_ioint_irqs[first + vector] =
1373 mtx_unlock_spin(&icu_lock);
1374 return (first + APIC_IO_INTS);
1376 mtx_unlock_spin(&icu_lock);
1377 printf("APIC: Couldn't find APIC vectors for %u IRQs\n", count);
1382 * Enable a vector for a particular apic_id. Since all lapics share idt
1383 * entries and ioint_handlers this enables the vector on all lapics. lapics
1384 * which do not have the vector configured would report spurious interrupts
1388 native_apic_enable_vector(u_int apic_id, u_int vector)
1391 KASSERT(vector != IDT_SYSCALL, ("Attempt to overwrite syscall entry"));
1392 KASSERT(ioint_handlers[vector / 32] != NULL,
1393 ("No ISR handler for vector %u", vector));
1394 #ifdef KDTRACE_HOOKS
1395 KASSERT(vector != IDT_DTRACE_RET,
1396 ("Attempt to overwrite DTrace entry"));
1398 setidt(vector, ioint_handlers[vector / 32], SDT_APIC, SEL_KPL,
1403 native_apic_disable_vector(u_int apic_id, u_int vector)
1406 KASSERT(vector != IDT_SYSCALL, ("Attempt to overwrite syscall entry"));
1407 #ifdef KDTRACE_HOOKS
1408 KASSERT(vector != IDT_DTRACE_RET,
1409 ("Attempt to overwrite DTrace entry"));
1411 KASSERT(ioint_handlers[vector / 32] != NULL,
1412 ("No ISR handler for vector %u", vector));
1415 * We can not currently clear the idt entry because other cpus
1416 * may have a valid vector at this offset.
1418 setidt(vector, &IDTVEC(rsvd), SDT_APICT, SEL_KPL, GSEL_APIC);
1422 /* Release an APIC vector when it's no longer in use. */
1424 native_apic_free_vector(u_int apic_id, u_int vector, u_int irq)
1428 KASSERT(vector >= APIC_IO_INTS && vector != IDT_SYSCALL &&
1429 vector <= APIC_IO_INTS + APIC_NUM_IOINTS,
1430 ("Vector %u does not map to an IRQ line", vector));
1431 KASSERT(irq < NUM_IO_INTS, ("Invalid IRQ %u", irq));
1432 KASSERT(lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS] ==
1433 irq, ("IRQ mismatch"));
1434 #ifdef KDTRACE_HOOKS
1435 KASSERT(vector != IDT_DTRACE_RET,
1436 ("Attempt to overwrite DTrace entry"));
1440 * Bind us to the cpu that owned the vector before freeing it so
1441 * we don't lose an interrupt delivery race.
1446 if (sched_is_bound(td))
1447 panic("apic_free_vector: Thread already bound.\n");
1448 sched_bind(td, apic_cpuid(apic_id));
1451 mtx_lock_spin(&icu_lock);
1452 lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS] = -1;
1453 mtx_unlock_spin(&icu_lock);
1461 /* Map an IDT vector (APIC) to an IRQ (interrupt source). */
1463 apic_idt_to_irq(u_int apic_id, u_int vector)
1467 KASSERT(vector >= APIC_IO_INTS && vector != IDT_SYSCALL &&
1468 vector <= APIC_IO_INTS + APIC_NUM_IOINTS,
1469 ("Vector %u does not map to an IRQ line", vector));
1470 #ifdef KDTRACE_HOOKS
1471 KASSERT(vector != IDT_DTRACE_RET,
1472 ("Attempt to overwrite DTrace entry"));
1474 irq = lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS];
1482 * Dump data about APIC IDT vector mappings.
1484 DB_SHOW_COMMAND(apic, db_show_apic)
1486 struct intsrc *isrc;
1491 if (strcmp(modif, "vv") == 0)
1493 else if (strcmp(modif, "v") == 0)
1497 for (apic_id = 0; apic_id <= MAX_APIC_ID; apic_id++) {
1498 if (lapics[apic_id].la_present == 0)
1500 db_printf("Interrupts bound to lapic %u\n", apic_id);
1501 for (i = 0; i < APIC_NUM_IOINTS + 1 && !db_pager_quit; i++) {
1502 irq = lapics[apic_id].la_ioint_irqs[i];
1503 if (irq == -1 || irq == IRQ_SYSCALL)
1505 #ifdef KDTRACE_HOOKS
1506 if (irq == IRQ_DTRACE_RET)
1510 if (irq == IRQ_EVTCHN)
1513 db_printf("vec 0x%2x -> ", i + APIC_IO_INTS);
1514 if (irq == IRQ_TIMER)
1515 db_printf("lapic timer\n");
1516 else if (irq < NUM_IO_INTS) {
1517 isrc = intr_lookup_source(irq);
1518 if (isrc == NULL || verbose == 0)
1519 db_printf("IRQ %u\n", irq);
1521 db_dump_intr_event(isrc->is_event,
1524 db_printf("IRQ %u ???\n", irq);
1530 dump_mask(const char *prefix, uint32_t v, int base)
1535 for (i = 0; i < 32; i++)
1538 db_printf("%s:", prefix);
1541 db_printf(" %02x", base + i);
1547 /* Show info from the lapic regs for this CPU. */
1548 DB_SHOW_COMMAND(lapic, db_show_lapic)
1552 db_printf("lapic ID = %d\n", lapic_id());
1553 v = lapic_read32(LAPIC_VERSION);
1554 db_printf("version = %d.%d\n", (v & APIC_VER_VERSION) >> 4,
1556 db_printf("max LVT = %d\n", (v & APIC_VER_MAXLVT) >> MAXLVTSHIFT);
1557 v = lapic_read32(LAPIC_SVR);
1558 db_printf("SVR = %02x (%s)\n", v & APIC_SVR_VECTOR,
1559 v & APIC_SVR_ENABLE ? "enabled" : "disabled");
1560 db_printf("TPR = %02x\n", lapic_read32(LAPIC_TPR));
1562 #define dump_field(prefix, regn, index) \
1563 dump_mask(__XSTRING(prefix ## index), \
1564 lapic_read32(LAPIC_ ## regn ## index), \
1567 db_printf("In-service Interrupts:\n");
1568 dump_field(isr, ISR, 0);
1569 dump_field(isr, ISR, 1);
1570 dump_field(isr, ISR, 2);
1571 dump_field(isr, ISR, 3);
1572 dump_field(isr, ISR, 4);
1573 dump_field(isr, ISR, 5);
1574 dump_field(isr, ISR, 6);
1575 dump_field(isr, ISR, 7);
1577 db_printf("TMR Interrupts:\n");
1578 dump_field(tmr, TMR, 0);
1579 dump_field(tmr, TMR, 1);
1580 dump_field(tmr, TMR, 2);
1581 dump_field(tmr, TMR, 3);
1582 dump_field(tmr, TMR, 4);
1583 dump_field(tmr, TMR, 5);
1584 dump_field(tmr, TMR, 6);
1585 dump_field(tmr, TMR, 7);
1587 db_printf("IRR Interrupts:\n");
1588 dump_field(irr, IRR, 0);
1589 dump_field(irr, IRR, 1);
1590 dump_field(irr, IRR, 2);
1591 dump_field(irr, IRR, 3);
1592 dump_field(irr, IRR, 4);
1593 dump_field(irr, IRR, 5);
1594 dump_field(irr, IRR, 6);
1595 dump_field(irr, IRR, 7);
1602 * APIC probing support code. This includes code to manage enumerators.
1605 static SLIST_HEAD(, apic_enumerator) enumerators =
1606 SLIST_HEAD_INITIALIZER(enumerators);
1607 static struct apic_enumerator *best_enum;
1610 apic_register_enumerator(struct apic_enumerator *enumerator)
1613 struct apic_enumerator *apic_enum;
1615 SLIST_FOREACH(apic_enum, &enumerators, apic_next) {
1616 if (apic_enum == enumerator)
1617 panic("%s: Duplicate register of %s", __func__,
1618 enumerator->apic_name);
1621 SLIST_INSERT_HEAD(&enumerators, enumerator, apic_next);
1625 * We have to look for CPU's very, very early because certain subsystems
1626 * want to know how many CPU's we have extremely early on in the boot
1630 apic_init(void *dummy __unused)
1632 struct apic_enumerator *enumerator;
1635 /* We only support built in local APICs. */
1636 if (!(cpu_feature & CPUID_APIC))
1639 /* Don't probe if APIC mode is disabled. */
1640 if (resource_disabled("apic", 0))
1643 /* Probe all the enumerators to find the best match. */
1646 SLIST_FOREACH(enumerator, &enumerators, apic_next) {
1647 retval = enumerator->apic_probe();
1650 if (best_enum == NULL || best < retval) {
1651 best_enum = enumerator;
1655 if (best_enum == NULL) {
1657 printf("APIC: Could not find any APICs.\n");
1659 panic("running without device atpic requires a local APIC");
1665 printf("APIC: Using the %s enumerator.\n",
1666 best_enum->apic_name);
1670 * To work around an errata, we disable the local APIC on some
1671 * CPUs during early startup. We need to turn the local APIC back
1672 * on on such CPUs now.
1674 ppro_reenable_apic();
1677 /* Probe the CPU's in the system. */
1678 retval = best_enum->apic_probe_cpus();
1680 printf("%s: Failed to probe CPUs: returned %d\n",
1681 best_enum->apic_name, retval);
1684 SYSINIT(apic_init, SI_SUB_TUNABLES - 1, SI_ORDER_SECOND, apic_init, NULL);
1687 * Setup the local APIC. We have to do this prior to starting up the APs
1691 apic_setup_local(void *dummy __unused)
1695 if (best_enum == NULL)
1698 /* Initialize the local APIC. */
1699 retval = best_enum->apic_setup_local();
1701 printf("%s: Failed to setup the local APIC: returned %d\n",
1702 best_enum->apic_name, retval);
1704 SYSINIT(apic_setup_local, SI_SUB_CPU, SI_ORDER_SECOND, apic_setup_local, NULL);
1707 * Setup the I/O APICs.
1710 apic_setup_io(void *dummy __unused)
1714 if (best_enum == NULL)
1718 * Local APIC must be registered before other PICs and pseudo PICs
1719 * for proper suspend/resume order.
1721 intr_register_pic(&lapic_pic);
1723 retval = best_enum->apic_setup_io();
1725 printf("%s: Failed to setup I/O APICs: returned %d\n",
1726 best_enum->apic_name, retval);
1729 * Finish setting up the local APIC on the BSP once we know
1730 * how to properly program the LINT pins. In particular, this
1731 * enables the EOI suppression mode, if LAPIC support it and
1732 * user did not disabled the mode.
1738 /* Enable the MSI "pic". */
1739 init_ops.msi_init();
1741 SYSINIT(apic_setup_io, SI_SUB_INTR, SI_ORDER_THIRD, apic_setup_io, NULL);
1745 * Inter Processor Interrupt functions. The lapic_ipi_*() functions are
1746 * private to the MD code. The public interface for the rest of the
1747 * kernel is defined in mp_machdep.c.
1751 * Wait delay microseconds for IPI to be sent. If delay is -1, we
1755 native_lapic_ipi_wait(int delay)
1757 uint64_t i, counter;
1759 /* LAPIC_ICR.APIC_DELSTAT_MASK is undefined in x2APIC mode */
1760 if (x2apic_mode || delay == -1)
1763 counter = lapic_ipi_wait_mult * delay;
1764 for (i = 0; i < counter; i++) {
1765 if ((lapic_read_icr_lo() & APIC_DELSTAT_MASK) ==
1774 native_lapic_ipi_raw(register_t icrlo, u_int dest)
1778 register_t saveintr;
1780 /* XXX: Need more sanity checking of icrlo? */
1781 KASSERT(x2apic_mode || lapic_map != NULL,
1782 ("%s called too early", __func__));
1783 KASSERT(x2apic_mode ||
1784 (dest & ~(APIC_ID_MASK >> APIC_ID_SHIFT)) == 0,
1785 ("%s: invalid dest field", __func__));
1786 KASSERT((icrlo & APIC_ICRLO_RESV_MASK) == 0,
1787 ("%s: reserved bits set in ICR LO register", __func__));
1789 /* Set destination in ICR HI register if it is being used. */
1791 saveintr = intr_disable();
1792 icr = lapic_read_icr();
1795 if ((icrlo & APIC_DEST_MASK) == APIC_DEST_DESTFLD) {
1800 vhi &= ~APIC_ID_MASK;
1801 vhi |= dest << APIC_ID_SHIFT;
1807 /* Program the contents of the IPI and dispatch it. */
1812 vlo &= APIC_ICRLO_RESV_MASK;
1815 lapic_write_icr(vhi, vlo);
1817 intr_restore(saveintr);
1820 #define BEFORE_SPIN 50000
1821 #ifdef DETECT_DEADLOCK
1822 #define AFTER_SPIN 50
1826 native_lapic_ipi_vectored(u_int vector, int dest)
1828 register_t icrlo, destfield;
1830 KASSERT((vector & ~APIC_VECTOR_MASK) == 0,
1831 ("%s: invalid vector %d", __func__, vector));
1833 icrlo = APIC_DESTMODE_PHY | APIC_TRIGMOD_EDGE | APIC_LEVEL_ASSERT;
1836 * NMI IPIs are just fake vectors used to send a NMI. Use special rules
1837 * regarding NMIs if passed, otherwise specify the vector.
1839 if (vector >= IPI_NMI_FIRST)
1840 icrlo |= APIC_DELMODE_NMI;
1842 icrlo |= vector | APIC_DELMODE_FIXED;
1845 case APIC_IPI_DEST_SELF:
1846 icrlo |= APIC_DEST_SELF;
1848 case APIC_IPI_DEST_ALL:
1849 icrlo |= APIC_DEST_ALLISELF;
1851 case APIC_IPI_DEST_OTHERS:
1852 icrlo |= APIC_DEST_ALLESELF;
1855 KASSERT(x2apic_mode ||
1856 (dest & ~(APIC_ID_MASK >> APIC_ID_SHIFT)) == 0,
1857 ("%s: invalid destination 0x%x", __func__, dest));
1861 /* Wait for an earlier IPI to finish. */
1862 if (!lapic_ipi_wait(BEFORE_SPIN)) {
1863 if (panicstr != NULL)
1866 panic("APIC: Previous IPI is stuck");
1869 lapic_ipi_raw(icrlo, destfield);
1871 #ifdef DETECT_DEADLOCK
1872 /* Wait for IPI to be delivered. */
1873 if (!lapic_ipi_wait(AFTER_SPIN)) {
1874 #ifdef needsattention
1878 * The above function waits for the message to actually be
1879 * delivered. It breaks out after an arbitrary timeout
1880 * since the message should eventually be delivered (at
1881 * least in theory) and that if it wasn't we would catch
1882 * the failure with the check above when the next IPI is
1885 * We could skip this wait entirely, EXCEPT it probably
1886 * protects us from other routines that assume that the
1887 * message was delivered and acted upon when this function
1890 printf("APIC: IPI might be stuck\n");
1891 #else /* !needsattention */
1892 /* Wait until mesage is sent without a timeout. */
1893 while (lapic_read_icr_lo() & APIC_DELSTAT_PEND)
1895 #endif /* needsattention */
1897 #endif /* DETECT_DEADLOCK */
1901 * Since the IDT is shared by all CPUs the IPI slot update needs to be globally
1904 * Consider the case where an IPI is generated immediately after allocation:
1905 * vector = lapic_ipi_alloc(ipifunc);
1906 * ipi_selected(other_cpus, vector);
1908 * In xAPIC mode a write to ICR_LO has serializing semantics because the
1909 * APIC page is mapped as an uncached region. In x2APIC mode there is an
1910 * explicit 'mfence' before the ICR MSR is written. Therefore in both cases
1911 * the IDT slot update is globally visible before the IPI is delivered.
1914 native_lapic_ipi_alloc(inthand_t *ipifunc)
1916 struct gate_descriptor *ip;
1920 KASSERT(ipifunc != &IDTVEC(rsvd), ("invalid ipifunc %p", ipifunc));
1923 mtx_lock_spin(&icu_lock);
1924 for (idx = IPI_DYN_FIRST; idx <= IPI_DYN_LAST; idx++) {
1926 func = (ip->gd_hioffset << 16) | ip->gd_looffset;
1927 if (func == (uintptr_t)&IDTVEC(rsvd)) {
1929 setidt(vector, ipifunc, SDT_APIC, SEL_KPL, GSEL_APIC);
1933 mtx_unlock_spin(&icu_lock);
1938 native_lapic_ipi_free(int vector)
1940 struct gate_descriptor *ip;
1943 KASSERT(vector >= IPI_DYN_FIRST && vector <= IPI_DYN_LAST,
1944 ("%s: invalid vector %d", __func__, vector));
1946 mtx_lock_spin(&icu_lock);
1948 func = (ip->gd_hioffset << 16) | ip->gd_looffset;
1949 KASSERT(func != (uintptr_t)&IDTVEC(rsvd),
1950 ("invalid idtfunc %#lx", func));
1951 setidt(vector, &IDTVEC(rsvd), SDT_APICT, SEL_KPL, GSEL_APIC);
1952 mtx_unlock_spin(&icu_lock);