2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
5 * Copyright (c) 1996, by Steve Passe
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. The name of the developer may NOT be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 * 3. Neither the name of the author nor the names of any co-contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Local APIC support on Pentium and later processors.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include "opt_atpic.h"
40 #include "opt_hwpmc_hooks.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
53 #include <sys/sched.h>
55 #include <sys/sysctl.h>
56 #include <sys/timeet.h>
61 #include <x86/apicreg.h>
62 #include <machine/clock.h>
63 #include <machine/cpufunc.h>
64 #include <machine/cputypes.h>
65 #include <machine/frame.h>
66 #include <machine/intr_machdep.h>
67 #include <x86/apicvar.h>
69 #include <machine/md_var.h>
70 #include <machine/smp.h>
71 #include <machine/specialreg.h>
75 #include <sys/interrupt.h>
80 #define SDT_APIC SDT_SYSIGT
83 #define SDT_APIC SDT_SYS386IGT
84 #define GSEL_APIC GSEL(GCODE_SEL, SEL_KPL)
87 static MALLOC_DEFINE(M_LAPIC, "local_apic", "Local APIC items");
89 /* Sanity checks on IDT vectors. */
90 CTASSERT(APIC_IO_INTS + APIC_NUM_IOINTS == APIC_TIMER_INT);
91 CTASSERT(APIC_TIMER_INT < APIC_LOCAL_INTS);
92 CTASSERT(APIC_LOCAL_INTS == 240);
93 CTASSERT(IPI_STOP < APIC_SPURIOUS_INT);
95 /* Magic IRQ values for the timer and syscalls. */
96 #define IRQ_TIMER (NUM_IO_INTS + 1)
97 #define IRQ_SYSCALL (NUM_IO_INTS + 2)
98 #define IRQ_DTRACE_RET (NUM_IO_INTS + 3)
99 #define IRQ_EVTCHN (NUM_IO_INTS + 4)
101 enum lat_timer_mode {
103 LAT_MODE_PERIODIC = 1,
104 LAT_MODE_ONESHOT = 2,
105 LAT_MODE_DEADLINE = 3,
109 * Support for local APICs. Local APICs manage interrupts on each
110 * individual processor as opposed to I/O APICs which receive interrupts
111 * from I/O devices and then forward them on to the local APICs.
113 * Local APICs can also send interrupts to each other thus providing the
114 * mechanism for IPIs.
118 u_int lvt_edgetrigger:1;
119 u_int lvt_activehi:1;
127 struct lvt la_lvts[APIC_LVT_MAX + 1];
128 struct lvt la_elvts[APIC_ELVT_MAX + 1];;
131 u_int la_cluster_id:2;
133 u_long *la_timer_count;
134 uint64_t la_timer_period;
135 enum lat_timer_mode la_timer_mode;
136 uint32_t lvt_timer_base;
137 uint32_t lvt_timer_last;
138 /* Include IDT_SYSCALL to make indexing easier. */
139 int la_ioint_irqs[APIC_NUM_IOINTS + 1];
142 /* Global defaults for local APIC LVT entries. */
143 static struct lvt lvts[APIC_LVT_MAX + 1] = {
144 { 1, 1, 1, 1, APIC_LVT_DM_EXTINT, 0 }, /* LINT0: masked ExtINT */
145 { 1, 1, 0, 1, APIC_LVT_DM_NMI, 0 }, /* LINT1: NMI */
146 { 1, 1, 1, 1, APIC_LVT_DM_FIXED, APIC_TIMER_INT }, /* Timer */
147 { 1, 1, 0, 1, APIC_LVT_DM_FIXED, APIC_ERROR_INT }, /* Error */
148 { 1, 1, 1, 1, APIC_LVT_DM_NMI, 0 }, /* PMC */
149 { 1, 1, 1, 1, APIC_LVT_DM_FIXED, APIC_THERMAL_INT }, /* Thermal */
150 { 1, 1, 1, 1, APIC_LVT_DM_FIXED, APIC_CMC_INT }, /* CMCI */
153 /* Global defaults for AMD local APIC ELVT entries. */
154 static struct lvt elvts[APIC_ELVT_MAX + 1] = {
155 { 1, 1, 1, 0, APIC_LVT_DM_FIXED, 0 },
156 { 1, 1, 1, 0, APIC_LVT_DM_FIXED, APIC_CMC_INT },
157 { 1, 1, 1, 0, APIC_LVT_DM_FIXED, 0 },
158 { 1, 1, 1, 0, APIC_LVT_DM_FIXED, 0 },
161 static inthand_t *ioint_handlers[] = {
163 IDTVEC(apic_isr1), /* 32 - 63 */
164 IDTVEC(apic_isr2), /* 64 - 95 */
165 IDTVEC(apic_isr3), /* 96 - 127 */
166 IDTVEC(apic_isr4), /* 128 - 159 */
167 IDTVEC(apic_isr5), /* 160 - 191 */
168 IDTVEC(apic_isr6), /* 192 - 223 */
169 IDTVEC(apic_isr7), /* 224 - 255 */
172 static inthand_t *ioint_pti_handlers[] = {
174 IDTVEC(apic_isr1_pti), /* 32 - 63 */
175 IDTVEC(apic_isr2_pti), /* 64 - 95 */
176 IDTVEC(apic_isr3_pti), /* 96 - 127 */
177 IDTVEC(apic_isr4_pti), /* 128 - 159 */
178 IDTVEC(apic_isr5_pti), /* 160 - 191 */
179 IDTVEC(apic_isr6_pti), /* 192 - 223 */
180 IDTVEC(apic_isr7_pti), /* 224 - 255 */
183 static u_int32_t lapic_timer_divisors[] = {
184 APIC_TDCR_1, APIC_TDCR_2, APIC_TDCR_4, APIC_TDCR_8, APIC_TDCR_16,
185 APIC_TDCR_32, APIC_TDCR_64, APIC_TDCR_128
188 extern inthand_t IDTVEC(rsvd_pti), IDTVEC(rsvd);
190 volatile char *lapic_map;
191 vm_paddr_t lapic_paddr;
193 int lapic_eoi_suppression;
194 static int lapic_timer_tsc_deadline;
195 static u_long lapic_timer_divisor, count_freq;
196 static struct eventtimer lapic_et;
198 static uint64_t lapic_ipi_wait_mult;
200 unsigned int max_apic_id;
202 SYSCTL_NODE(_hw, OID_AUTO, apic, CTLFLAG_RD, 0, "APIC options");
203 SYSCTL_INT(_hw_apic, OID_AUTO, x2apic_mode, CTLFLAG_RD, &x2apic_mode, 0, "");
204 SYSCTL_INT(_hw_apic, OID_AUTO, eoi_suppression, CTLFLAG_RD,
205 &lapic_eoi_suppression, 0, "");
206 SYSCTL_INT(_hw_apic, OID_AUTO, timer_tsc_deadline, CTLFLAG_RD,
207 &lapic_timer_tsc_deadline, 0, "");
209 static void lapic_calibrate_initcount(struct lapic *la);
210 static void lapic_calibrate_deadline(struct lapic *la);
213 lapic_read32(enum LAPIC_REGISTERS reg)
218 res = rdmsr32(MSR_APIC_000 + reg);
220 res = *(volatile uint32_t *)(lapic_map + reg * LAPIC_MEM_MUL);
226 lapic_write32(enum LAPIC_REGISTERS reg, uint32_t val)
232 wrmsr(MSR_APIC_000 + reg, val);
234 *(volatile uint32_t *)(lapic_map + reg * LAPIC_MEM_MUL) = val;
239 lapic_write32_nofence(enum LAPIC_REGISTERS reg, uint32_t val)
243 wrmsr(MSR_APIC_000 + reg, val);
245 *(volatile uint32_t *)(lapic_map + reg * LAPIC_MEM_MUL) = val;
257 v = rdmsr(MSR_APIC_000 + LAPIC_ICR_LO);
259 vhi = lapic_read32(LAPIC_ICR_HI);
260 vlo = lapic_read32(LAPIC_ICR_LO);
261 v = ((uint64_t)vhi << 32) | vlo;
267 lapic_read_icr_lo(void)
270 return (lapic_read32(LAPIC_ICR_LO));
274 lapic_write_icr(uint32_t vhi, uint32_t vlo)
279 v = ((uint64_t)vhi << 32) | vlo;
281 wrmsr(MSR_APIC_000 + LAPIC_ICR_LO, v);
283 lapic_write32(LAPIC_ICR_HI, vhi);
284 lapic_write32(LAPIC_ICR_LO, vlo);
290 native_lapic_enable_x2apic(void)
294 apic_base = rdmsr(MSR_APICBASE);
295 apic_base |= APICBASE_X2APIC | APICBASE_ENABLED;
296 wrmsr(MSR_APICBASE, apic_base);
300 native_lapic_is_x2apic(void)
304 apic_base = rdmsr(MSR_APICBASE);
305 return ((apic_base & (APICBASE_X2APIC | APICBASE_ENABLED)) ==
306 (APICBASE_X2APIC | APICBASE_ENABLED));
309 static void lapic_enable(void);
310 static void lapic_resume(struct pic *pic, bool suspend_cancelled);
311 static void lapic_timer_oneshot(struct lapic *);
312 static void lapic_timer_oneshot_nointr(struct lapic *, uint32_t);
313 static void lapic_timer_periodic(struct lapic *);
314 static void lapic_timer_deadline(struct lapic *);
315 static void lapic_timer_stop(struct lapic *);
316 static void lapic_timer_set_divisor(u_int divisor);
317 static uint32_t lvt_mode(struct lapic *la, u_int pin, uint32_t value);
318 static int lapic_et_start(struct eventtimer *et,
319 sbintime_t first, sbintime_t period);
320 static int lapic_et_stop(struct eventtimer *et);
321 static u_int apic_idt_to_irq(u_int apic_id, u_int vector);
322 static void lapic_set_tpr(u_int vector);
324 struct pic lapic_pic = { .pic_resume = lapic_resume };
326 /* Forward declarations for apic_ops */
327 static void native_lapic_create(u_int apic_id, int boot_cpu);
328 static void native_lapic_init(vm_paddr_t addr);
329 static void native_lapic_xapic_mode(void);
330 static void native_lapic_setup(int boot);
331 static void native_lapic_dump(const char *str);
332 static void native_lapic_disable(void);
333 static void native_lapic_eoi(void);
334 static int native_lapic_id(void);
335 static int native_lapic_intr_pending(u_int vector);
336 static u_int native_apic_cpuid(u_int apic_id);
337 static u_int native_apic_alloc_vector(u_int apic_id, u_int irq);
338 static u_int native_apic_alloc_vectors(u_int apic_id, u_int *irqs,
339 u_int count, u_int align);
340 static void native_apic_disable_vector(u_int apic_id, u_int vector);
341 static void native_apic_enable_vector(u_int apic_id, u_int vector);
342 static void native_apic_free_vector(u_int apic_id, u_int vector, u_int irq);
343 static void native_lapic_set_logical_id(u_int apic_id, u_int cluster,
345 static int native_lapic_enable_pmc(void);
346 static void native_lapic_disable_pmc(void);
347 static void native_lapic_reenable_pmc(void);
348 static void native_lapic_enable_cmc(void);
349 static int native_lapic_enable_mca_elvt(void);
350 static int native_lapic_set_lvt_mask(u_int apic_id, u_int lvt,
352 static int native_lapic_set_lvt_mode(u_int apic_id, u_int lvt,
354 static int native_lapic_set_lvt_polarity(u_int apic_id, u_int lvt,
355 enum intr_polarity pol);
356 static int native_lapic_set_lvt_triggermode(u_int apic_id, u_int lvt,
357 enum intr_trigger trigger);
359 static void native_lapic_ipi_raw(register_t icrlo, u_int dest);
360 static void native_lapic_ipi_vectored(u_int vector, int dest);
361 static int native_lapic_ipi_wait(int delay);
363 static int native_lapic_ipi_alloc(inthand_t *ipifunc);
364 static void native_lapic_ipi_free(int vector);
366 struct apic_ops apic_ops = {
367 .create = native_lapic_create,
368 .init = native_lapic_init,
369 .xapic_mode = native_lapic_xapic_mode,
370 .is_x2apic = native_lapic_is_x2apic,
371 .setup = native_lapic_setup,
372 .dump = native_lapic_dump,
373 .disable = native_lapic_disable,
374 .eoi = native_lapic_eoi,
375 .id = native_lapic_id,
376 .intr_pending = native_lapic_intr_pending,
377 .set_logical_id = native_lapic_set_logical_id,
378 .cpuid = native_apic_cpuid,
379 .alloc_vector = native_apic_alloc_vector,
380 .alloc_vectors = native_apic_alloc_vectors,
381 .enable_vector = native_apic_enable_vector,
382 .disable_vector = native_apic_disable_vector,
383 .free_vector = native_apic_free_vector,
384 .enable_pmc = native_lapic_enable_pmc,
385 .disable_pmc = native_lapic_disable_pmc,
386 .reenable_pmc = native_lapic_reenable_pmc,
387 .enable_cmc = native_lapic_enable_cmc,
388 .enable_mca_elvt = native_lapic_enable_mca_elvt,
390 .ipi_raw = native_lapic_ipi_raw,
391 .ipi_vectored = native_lapic_ipi_vectored,
392 .ipi_wait = native_lapic_ipi_wait,
394 .ipi_alloc = native_lapic_ipi_alloc,
395 .ipi_free = native_lapic_ipi_free,
396 .set_lvt_mask = native_lapic_set_lvt_mask,
397 .set_lvt_mode = native_lapic_set_lvt_mode,
398 .set_lvt_polarity = native_lapic_set_lvt_polarity,
399 .set_lvt_triggermode = native_lapic_set_lvt_triggermode,
403 lvt_mode_impl(struct lapic *la, struct lvt *lvt, u_int pin, uint32_t value)
406 value &= ~(APIC_LVT_M | APIC_LVT_TM | APIC_LVT_IIPP | APIC_LVT_DM |
408 if (lvt->lvt_edgetrigger == 0)
409 value |= APIC_LVT_TM;
410 if (lvt->lvt_activehi == 0)
411 value |= APIC_LVT_IIPP_INTALO;
414 value |= lvt->lvt_mode;
415 switch (lvt->lvt_mode) {
416 case APIC_LVT_DM_NMI:
417 case APIC_LVT_DM_SMI:
418 case APIC_LVT_DM_INIT:
419 case APIC_LVT_DM_EXTINT:
420 if (!lvt->lvt_edgetrigger && bootverbose) {
421 printf("lapic%u: Forcing LINT%u to edge trigger\n",
423 value &= ~APIC_LVT_TM;
425 /* Use a vector of 0. */
427 case APIC_LVT_DM_FIXED:
428 value |= lvt->lvt_vector;
431 panic("bad APIC LVT delivery mode: %#x\n", value);
437 lvt_mode(struct lapic *la, u_int pin, uint32_t value)
441 KASSERT(pin <= APIC_LVT_MAX,
442 ("%s: pin %u out of range", __func__, pin));
443 if (la->la_lvts[pin].lvt_active)
444 lvt = &la->la_lvts[pin];
448 return (lvt_mode_impl(la, lvt, pin, value));
452 elvt_mode(struct lapic *la, u_int idx, uint32_t value)
456 KASSERT(idx <= APIC_ELVT_MAX,
457 ("%s: idx %u out of range", __func__, idx));
459 elvt = &la->la_elvts[idx];
460 KASSERT(elvt->lvt_active, ("%s: ELVT%u is not active", __func__, idx));
461 KASSERT(elvt->lvt_edgetrigger,
462 ("%s: ELVT%u is not edge triggered", __func__, idx));
463 KASSERT(elvt->lvt_activehi,
464 ("%s: ELVT%u is not active high", __func__, idx));
465 return (lvt_mode_impl(la, elvt, idx, value));
469 * Map the local APIC and setup necessary interrupt vectors.
472 native_lapic_init(vm_paddr_t addr)
475 uint64_t r, r1, r2, rx;
482 * Enable x2APIC mode if possible. Map the local APIC
485 * Keep the LAPIC registers page mapped uncached for x2APIC
486 * mode too, to have direct map page attribute set to
487 * uncached. This is needed to work around CPU errata present
488 * on all Intel processors.
490 KASSERT(trunc_page(addr) == addr,
491 ("local APIC not aligned on a page boundary"));
493 lapic_map = pmap_mapdev(addr, PAGE_SIZE);
495 native_lapic_enable_x2apic();
499 /* Setup the spurious interrupt handler. */
500 setidt(APIC_SPURIOUS_INT, IDTVEC(spuriousint), SDT_APIC, SEL_KPL,
503 /* Perform basic initialization of the BSP's local APIC. */
506 /* Set BSP's per-CPU local APIC ID. */
507 PCPU_SET(apic_id, lapic_id());
509 /* Local APIC timer interrupt. */
510 setidt(APIC_TIMER_INT, pti ? IDTVEC(timerint_pti) : IDTVEC(timerint),
511 SDT_APIC, SEL_KPL, GSEL_APIC);
513 /* Local APIC error interrupt. */
514 setidt(APIC_ERROR_INT, pti ? IDTVEC(errorint_pti) : IDTVEC(errorint),
515 SDT_APIC, SEL_KPL, GSEL_APIC);
517 /* XXX: Thermal interrupt */
519 /* Local APIC CMCI. */
520 setidt(APIC_CMC_INT, pti ? IDTVEC(cmcint_pti) : IDTVEC(cmcint),
521 SDT_APIC, SEL_KPL, GSEL_APIC);
523 if ((resource_int_value("apic", 0, "clock", &i) != 0 || i != 0)) {
525 /* Intel CPUID 0x06 EAX[2] set if APIC timer runs in C3. */
526 if (cpu_vendor_id == CPU_VENDOR_INTEL && cpu_high >= 6) {
527 do_cpuid(0x06, regs);
528 if ((regs[0] & CPUTPM1_ARAT) != 0)
530 } else if (cpu_vendor_id == CPU_VENDOR_AMD &&
531 CPUID_TO_FAMILY(cpu_id) >= 0x12) {
534 bzero(&lapic_et, sizeof(lapic_et));
535 lapic_et.et_name = "LAPIC";
536 lapic_et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT |
538 lapic_et.et_quality = 600;
540 lapic_et.et_flags |= ET_FLAGS_C3STOP;
541 lapic_et.et_quality = 100;
543 if ((cpu_feature & CPUID_TSC) != 0 &&
544 (cpu_feature2 & CPUID2_TSCDLT) != 0 &&
545 tsc_is_invariant && tsc_freq != 0) {
546 lapic_timer_tsc_deadline = 1;
547 TUNABLE_INT_FETCH("hw.lapic_tsc_deadline",
548 &lapic_timer_tsc_deadline);
551 lapic_et.et_frequency = 0;
552 /* We don't know frequency yet, so trying to guess. */
553 lapic_et.et_min_period = 0x00001000LL;
554 lapic_et.et_max_period = SBT_1S;
555 lapic_et.et_start = lapic_et_start;
556 lapic_et.et_stop = lapic_et_stop;
557 lapic_et.et_priv = NULL;
558 et_register(&lapic_et);
562 * Set lapic_eoi_suppression after lapic_enable(), to not
563 * enable suppression in the hardware prematurely. Note that
564 * we by default enable suppression even when system only has
565 * one IO-APIC, since EOI is broadcasted to all APIC agents,
566 * including CPUs, otherwise.
568 * It seems that at least some KVM versions report
569 * EOI_SUPPRESSION bit, but auto-EOI does not work.
571 ver = lapic_read32(LAPIC_VERSION);
572 if ((ver & APIC_VER_EOI_SUPPRESSION) != 0) {
573 lapic_eoi_suppression = 1;
574 if (vm_guest == VM_GUEST_KVM) {
577 "KVM -- disabling lapic eoi suppression\n");
578 lapic_eoi_suppression = 0;
580 TUNABLE_INT_FETCH("hw.lapic_eoi_suppression",
581 &lapic_eoi_suppression);
587 * Calibrate the busy loop waiting for IPI ack in xAPIC mode.
588 * lapic_ipi_wait_mult contains the number of iterations which
589 * approximately delay execution for 1 microsecond (the
590 * argument to native_lapic_ipi_wait() is in microseconds).
592 * We assume that TSC is present and already measured.
593 * Possible TSC frequency jumps are irrelevant to the
594 * calibration loop below, the CPU clock management code is
595 * not yet started, and we do not enter sleep states.
597 KASSERT((cpu_feature & CPUID_TSC) != 0 && tsc_freq != 0,
598 ("TSC not initialized"));
601 for (rx = 0; rx < LOOPS; rx++) {
602 (void)lapic_read_icr_lo();
606 r1 = tsc_freq * LOOPS;
608 lapic_ipi_wait_mult = r1 >= r2 ? r1 / r2 : 1;
610 printf("LAPIC: ipi_wait() us multiplier %ju (r %ju "
611 "tsc %ju)\n", (uintmax_t)lapic_ipi_wait_mult,
612 (uintmax_t)r, (uintmax_t)tsc_freq);
620 * Create a local APIC instance.
623 native_lapic_create(u_int apic_id, int boot_cpu)
627 if (apic_id > max_apic_id) {
628 printf("APIC: Ignoring local APIC with ID %d\n", apic_id);
630 panic("Can't ignore BSP");
633 KASSERT(!lapics[apic_id].la_present, ("duplicate local APIC %u",
637 * Assume no local LVT overrides and a cluster of 0 and
638 * intra-cluster ID of 0.
640 lapics[apic_id].la_present = 1;
641 lapics[apic_id].la_id = apic_id;
642 for (i = 0; i <= APIC_LVT_MAX; i++) {
643 lapics[apic_id].la_lvts[i] = lvts[i];
644 lapics[apic_id].la_lvts[i].lvt_active = 0;
646 for (i = 0; i <= APIC_ELVT_MAX; i++) {
647 lapics[apic_id].la_elvts[i] = elvts[i];
648 lapics[apic_id].la_elvts[i].lvt_active = 0;
650 for (i = 0; i <= APIC_NUM_IOINTS; i++)
651 lapics[apic_id].la_ioint_irqs[i] = -1;
652 lapics[apic_id].la_ioint_irqs[IDT_SYSCALL - APIC_IO_INTS] = IRQ_SYSCALL;
653 lapics[apic_id].la_ioint_irqs[APIC_TIMER_INT - APIC_IO_INTS] =
656 lapics[apic_id].la_ioint_irqs[IDT_DTRACE_RET - APIC_IO_INTS] =
660 lapics[apic_id].la_ioint_irqs[IDT_EVTCHN - APIC_IO_INTS] = IRQ_EVTCHN;
665 cpu_add(apic_id, boot_cpu);
669 static inline uint32_t
670 amd_read_ext_features(void)
674 if (cpu_vendor_id != CPU_VENDOR_AMD)
676 version = lapic_read32(LAPIC_VERSION);
677 if ((version & APIC_VER_AMD_EXT_SPACE) != 0)
678 return (lapic_read32(LAPIC_EXT_FEATURES));
683 static inline uint32_t
684 amd_read_elvt_count(void)
689 extf = amd_read_ext_features();
690 count = (extf & APIC_EXTF_ELVT_MASK) >> APIC_EXTF_ELVT_SHIFT;
691 count = min(count, APIC_ELVT_MAX + 1);
696 * Dump contents of local APIC registers
699 native_lapic_dump(const char* str)
707 version = lapic_read32(LAPIC_VERSION);
708 maxlvt = (version & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
709 printf("cpu%d %s:\n", PCPU_GET(cpuid), str);
710 printf(" ID: 0x%08x VER: 0x%08x LDR: 0x%08x DFR: 0x%08x",
711 lapic_read32(LAPIC_ID), version,
712 lapic_read32(LAPIC_LDR), x2apic_mode ? 0 : lapic_read32(LAPIC_DFR));
713 if ((cpu_feature2 & CPUID2_X2APIC) != 0)
714 printf(" x2APIC: %d", x2apic_mode);
715 printf("\n lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
716 lapic_read32(LAPIC_LVT_LINT0), lapic_read32(LAPIC_LVT_LINT1),
717 lapic_read32(LAPIC_TPR), lapic_read32(LAPIC_SVR));
718 printf(" timer: 0x%08x therm: 0x%08x err: 0x%08x",
719 lapic_read32(LAPIC_LVT_TIMER), lapic_read32(LAPIC_LVT_THERMAL),
720 lapic_read32(LAPIC_LVT_ERROR));
721 if (maxlvt >= APIC_LVT_PMC)
722 printf(" pmc: 0x%08x", lapic_read32(LAPIC_LVT_PCINT));
724 if (maxlvt >= APIC_LVT_CMCI)
725 printf(" cmci: 0x%08x\n", lapic_read32(LAPIC_LVT_CMCI));
726 extf = amd_read_ext_features();
728 printf(" AMD ext features: 0x%08x\n", extf);
729 elvt_count = amd_read_elvt_count();
730 for (i = 0; i < elvt_count; i++)
731 printf(" AMD elvt%d: 0x%08x\n", i,
732 lapic_read32(LAPIC_EXT_LVT0 + i));
737 native_lapic_xapic_mode(void)
741 saveintr = intr_disable();
743 native_lapic_enable_x2apic();
744 intr_restore(saveintr);
748 native_lapic_setup(int boot)
754 char buf[MAXCOMLEN + 1];
758 saveintr = intr_disable();
760 la = &lapics[lapic_id()];
761 KASSERT(la->la_present, ("missing APIC structure"));
762 version = lapic_read32(LAPIC_VERSION);
763 maxlvt = (version & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
765 /* Initialize the TPR to allow all interrupts. */
768 /* Setup spurious vector and enable the local APIC. */
771 /* Program LINT[01] LVT entries. */
772 lapic_write32(LAPIC_LVT_LINT0, lvt_mode(la, APIC_LVT_LINT0,
773 lapic_read32(LAPIC_LVT_LINT0)));
774 lapic_write32(LAPIC_LVT_LINT1, lvt_mode(la, APIC_LVT_LINT1,
775 lapic_read32(LAPIC_LVT_LINT1)));
777 /* Program the PMC LVT entry if present. */
778 if (maxlvt >= APIC_LVT_PMC) {
779 lapic_write32(LAPIC_LVT_PCINT, lvt_mode(la, APIC_LVT_PMC,
783 /* Program timer LVT and setup handler. */
784 la->lvt_timer_base = lvt_mode(la, APIC_LVT_TIMER,
785 lapic_read32(LAPIC_LVT_TIMER));
786 la->lvt_timer_last = la->lvt_timer_base;
787 lapic_write32(LAPIC_LVT_TIMER, la->lvt_timer_base);
789 snprintf(buf, sizeof(buf), "cpu%d:timer", PCPU_GET(cpuid));
790 intrcnt_add(buf, &la->la_timer_count);
793 /* Calibrate the timer parameters using BSP. */
794 if (boot && IS_BSP()) {
795 lapic_calibrate_initcount(la);
796 if (lapic_timer_tsc_deadline)
797 lapic_calibrate_deadline(la);
800 /* Setup the timer if configured. */
801 if (la->la_timer_mode != LAT_MODE_UNDEF) {
802 KASSERT(la->la_timer_period != 0, ("lapic%u: zero divisor",
804 switch (la->la_timer_mode) {
805 case LAT_MODE_PERIODIC:
806 lapic_timer_set_divisor(lapic_timer_divisor);
807 lapic_timer_periodic(la);
809 case LAT_MODE_ONESHOT:
810 lapic_timer_set_divisor(lapic_timer_divisor);
811 lapic_timer_oneshot(la);
813 case LAT_MODE_DEADLINE:
814 lapic_timer_deadline(la);
817 panic("corrupted la_timer_mode %p %d", la,
822 /* Program error LVT and clear any existing errors. */
823 lapic_write32(LAPIC_LVT_ERROR, lvt_mode(la, APIC_LVT_ERROR,
824 lapic_read32(LAPIC_LVT_ERROR)));
825 lapic_write32(LAPIC_ESR, 0);
827 /* XXX: Thermal LVT */
829 /* Program the CMCI LVT entry if present. */
830 if (maxlvt >= APIC_LVT_CMCI) {
831 lapic_write32(LAPIC_LVT_CMCI, lvt_mode(la, APIC_LVT_CMCI,
832 lapic_read32(LAPIC_LVT_CMCI)));
835 elvt_count = amd_read_elvt_count();
836 for (i = 0; i < elvt_count; i++) {
837 if (la->la_elvts[i].lvt_active)
838 lapic_write32(LAPIC_EXT_LVT0 + i,
839 elvt_mode(la, i, lapic_read32(LAPIC_EXT_LVT0 + i)));
842 intr_restore(saveintr);
846 native_lapic_reenable_pmc(void)
851 value = lapic_read32(LAPIC_LVT_PCINT);
852 value &= ~APIC_LVT_M;
853 lapic_write32(LAPIC_LVT_PCINT, value);
859 lapic_update_pmc(void *dummy)
863 la = &lapics[lapic_id()];
864 lapic_write32(LAPIC_LVT_PCINT, lvt_mode(la, APIC_LVT_PMC,
865 lapic_read32(LAPIC_LVT_PCINT)));
870 native_lapic_enable_pmc(void)
875 /* Fail if the local APIC is not present. */
876 if (!x2apic_mode && lapic_map == NULL)
879 /* Fail if the PMC LVT is not present. */
880 maxlvt = (lapic_read32(LAPIC_VERSION) & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
881 if (maxlvt < APIC_LVT_PMC)
884 lvts[APIC_LVT_PMC].lvt_masked = 0;
886 #ifdef EARLY_AP_STARTUP
887 MPASS(mp_ncpus == 1 || smp_started);
888 smp_rendezvous(NULL, lapic_update_pmc, NULL, NULL);
892 * If hwpmc was loaded at boot time then the APs may not be
893 * started yet. In that case, don't forward the request to
894 * them as they will program the lvt when they start.
897 smp_rendezvous(NULL, lapic_update_pmc, NULL, NULL);
900 lapic_update_pmc(NULL);
909 native_lapic_disable_pmc(void)
914 /* Fail if the local APIC is not present. */
915 if (!x2apic_mode && lapic_map == NULL)
918 /* Fail if the PMC LVT is not present. */
919 maxlvt = (lapic_read32(LAPIC_VERSION) & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
920 if (maxlvt < APIC_LVT_PMC)
923 lvts[APIC_LVT_PMC].lvt_masked = 1;
926 /* The APs should always be started when hwpmc is unloaded. */
927 KASSERT(mp_ncpus == 1 || smp_started, ("hwpmc unloaded too early"));
929 smp_rendezvous(NULL, lapic_update_pmc, NULL, NULL);
934 lapic_calibrate_initcount(struct lapic *la)
938 /* Start off with a divisor of 2 (power on reset default). */
939 lapic_timer_divisor = 2;
940 /* Try to calibrate the local APIC timer. */
942 lapic_timer_set_divisor(lapic_timer_divisor);
943 lapic_timer_oneshot_nointr(la, APIC_TIMER_MAX_COUNT);
945 value = APIC_TIMER_MAX_COUNT - lapic_read32(LAPIC_CCR_TIMER);
946 if (value != APIC_TIMER_MAX_COUNT)
948 lapic_timer_divisor <<= 1;
949 } while (lapic_timer_divisor <= 128);
950 if (lapic_timer_divisor > 128)
951 panic("lapic: Divisor too big");
953 printf("lapic: Divisor %lu, Frequency %lu Hz\n",
954 lapic_timer_divisor, value);
960 lapic_calibrate_deadline(struct lapic *la __unused)
964 printf("lapic: deadline tsc mode, Frequency %ju Hz\n",
965 (uintmax_t)tsc_freq);
970 lapic_change_mode(struct eventtimer *et, struct lapic *la,
971 enum lat_timer_mode newmode)
974 if (la->la_timer_mode == newmode)
977 case LAT_MODE_PERIODIC:
978 lapic_timer_set_divisor(lapic_timer_divisor);
979 et->et_frequency = count_freq;
981 case LAT_MODE_DEADLINE:
982 et->et_frequency = tsc_freq;
984 case LAT_MODE_ONESHOT:
985 lapic_timer_set_divisor(lapic_timer_divisor);
986 et->et_frequency = count_freq;
989 panic("lapic_change_mode %d", newmode);
991 la->la_timer_mode = newmode;
992 et->et_min_period = (0x00000002LLU << 32) / et->et_frequency;
993 et->et_max_period = (0xfffffffeLLU << 32) / et->et_frequency;
997 lapic_et_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
1001 la = &lapics[PCPU_GET(apic_id)];
1003 lapic_change_mode(et, la, LAT_MODE_PERIODIC);
1004 la->la_timer_period = ((uint32_t)et->et_frequency * period) >>
1006 lapic_timer_periodic(la);
1007 } else if (lapic_timer_tsc_deadline) {
1008 lapic_change_mode(et, la, LAT_MODE_DEADLINE);
1009 la->la_timer_period = (et->et_frequency * first) >> 32;
1010 lapic_timer_deadline(la);
1012 lapic_change_mode(et, la, LAT_MODE_ONESHOT);
1013 la->la_timer_period = ((uint32_t)et->et_frequency * first) >>
1015 lapic_timer_oneshot(la);
1021 lapic_et_stop(struct eventtimer *et)
1025 la = &lapics[PCPU_GET(apic_id)];
1026 lapic_timer_stop(la);
1027 la->la_timer_mode = LAT_MODE_UNDEF;
1032 native_lapic_disable(void)
1036 /* Software disable the local APIC. */
1037 value = lapic_read32(LAPIC_SVR);
1038 value &= ~APIC_SVR_SWEN;
1039 lapic_write32(LAPIC_SVR, value);
1047 /* Program the spurious vector to enable the local APIC. */
1048 value = lapic_read32(LAPIC_SVR);
1049 value &= ~(APIC_SVR_VECTOR | APIC_SVR_FOCUS);
1050 value |= APIC_SVR_FEN | APIC_SVR_SWEN | APIC_SPURIOUS_INT;
1051 if (lapic_eoi_suppression)
1052 value |= APIC_SVR_EOI_SUPPRESSION;
1053 lapic_write32(LAPIC_SVR, value);
1056 /* Reset the local APIC on the BSP during resume. */
1058 lapic_resume(struct pic *pic, bool suspend_cancelled)
1065 native_lapic_id(void)
1069 KASSERT(x2apic_mode || lapic_map != NULL, ("local APIC is not mapped"));
1070 v = lapic_read32(LAPIC_ID);
1072 v >>= APIC_ID_SHIFT;
1077 native_lapic_intr_pending(u_int vector)
1082 * The IRR registers are an array of registers each of which
1083 * only describes 32 interrupts in the low 32 bits. Thus, we
1084 * divide the vector by 32 to get the register index.
1085 * Finally, we modulus the vector by 32 to determine the
1086 * individual bit to test.
1088 irr = lapic_read32(LAPIC_IRR0 + vector / 32);
1089 return (irr & 1 << (vector % 32));
1093 native_lapic_set_logical_id(u_int apic_id, u_int cluster, u_int cluster_id)
1097 KASSERT(lapics[apic_id].la_present, ("%s: APIC %u doesn't exist",
1098 __func__, apic_id));
1099 KASSERT(cluster <= APIC_MAX_CLUSTER, ("%s: cluster %u too big",
1100 __func__, cluster));
1101 KASSERT(cluster_id <= APIC_MAX_INTRACLUSTER_ID,
1102 ("%s: intra cluster id %u too big", __func__, cluster_id));
1103 la = &lapics[apic_id];
1104 la->la_cluster = cluster;
1105 la->la_cluster_id = cluster_id;
1109 native_lapic_set_lvt_mask(u_int apic_id, u_int pin, u_char masked)
1112 if (pin > APIC_LVT_MAX)
1114 if (apic_id == APIC_ID_ALL) {
1115 lvts[pin].lvt_masked = masked;
1119 KASSERT(lapics[apic_id].la_present,
1120 ("%s: missing APIC %u", __func__, apic_id));
1121 lapics[apic_id].la_lvts[pin].lvt_masked = masked;
1122 lapics[apic_id].la_lvts[pin].lvt_active = 1;
1124 printf("lapic%u:", apic_id);
1127 printf(" LINT%u %s\n", pin, masked ? "masked" : "unmasked");
1132 native_lapic_set_lvt_mode(u_int apic_id, u_int pin, u_int32_t mode)
1136 if (pin > APIC_LVT_MAX)
1138 if (apic_id == APIC_ID_ALL) {
1143 KASSERT(lapics[apic_id].la_present,
1144 ("%s: missing APIC %u", __func__, apic_id));
1145 lvt = &lapics[apic_id].la_lvts[pin];
1146 lvt->lvt_active = 1;
1148 printf("lapic%u:", apic_id);
1150 lvt->lvt_mode = mode;
1152 case APIC_LVT_DM_NMI:
1153 case APIC_LVT_DM_SMI:
1154 case APIC_LVT_DM_INIT:
1155 case APIC_LVT_DM_EXTINT:
1156 lvt->lvt_edgetrigger = 1;
1157 lvt->lvt_activehi = 1;
1158 if (mode == APIC_LVT_DM_EXTINT)
1159 lvt->lvt_masked = 1;
1161 lvt->lvt_masked = 0;
1164 panic("Unsupported delivery mode: 0x%x\n", mode);
1167 printf(" Routing ");
1169 case APIC_LVT_DM_NMI:
1172 case APIC_LVT_DM_SMI:
1175 case APIC_LVT_DM_INIT:
1178 case APIC_LVT_DM_EXTINT:
1182 printf(" -> LINT%u\n", pin);
1188 native_lapic_set_lvt_polarity(u_int apic_id, u_int pin, enum intr_polarity pol)
1191 if (pin > APIC_LVT_MAX || pol == INTR_POLARITY_CONFORM)
1193 if (apic_id == APIC_ID_ALL) {
1194 lvts[pin].lvt_activehi = (pol == INTR_POLARITY_HIGH);
1198 KASSERT(lapics[apic_id].la_present,
1199 ("%s: missing APIC %u", __func__, apic_id));
1200 lapics[apic_id].la_lvts[pin].lvt_active = 1;
1201 lapics[apic_id].la_lvts[pin].lvt_activehi =
1202 (pol == INTR_POLARITY_HIGH);
1204 printf("lapic%u:", apic_id);
1207 printf(" LINT%u polarity: %s\n", pin,
1208 pol == INTR_POLARITY_HIGH ? "high" : "low");
1213 native_lapic_set_lvt_triggermode(u_int apic_id, u_int pin,
1214 enum intr_trigger trigger)
1217 if (pin > APIC_LVT_MAX || trigger == INTR_TRIGGER_CONFORM)
1219 if (apic_id == APIC_ID_ALL) {
1220 lvts[pin].lvt_edgetrigger = (trigger == INTR_TRIGGER_EDGE);
1224 KASSERT(lapics[apic_id].la_present,
1225 ("%s: missing APIC %u", __func__, apic_id));
1226 lapics[apic_id].la_lvts[pin].lvt_edgetrigger =
1227 (trigger == INTR_TRIGGER_EDGE);
1228 lapics[apic_id].la_lvts[pin].lvt_active = 1;
1230 printf("lapic%u:", apic_id);
1233 printf(" LINT%u trigger: %s\n", pin,
1234 trigger == INTR_TRIGGER_EDGE ? "edge" : "level");
1239 * Adjust the TPR of the current CPU so that it blocks all interrupts below
1240 * the passed in vector.
1243 lapic_set_tpr(u_int vector)
1246 lapic_write32(LAPIC_TPR, vector);
1250 tpr = lapic_read32(LAPIC_TPR) & ~APIC_TPR_PRIO;
1252 lapic_write32(LAPIC_TPR, tpr);
1257 native_lapic_eoi(void)
1260 lapic_write32_nofence(LAPIC_EOI, 0);
1264 lapic_handle_intr(int vector, struct trapframe *frame)
1266 struct intsrc *isrc;
1268 isrc = intr_lookup_source(apic_idt_to_irq(PCPU_GET(apic_id),
1270 intr_execute_handlers(isrc, frame);
1274 lapic_handle_timer(struct trapframe *frame)
1277 struct trapframe *oldframe;
1280 /* Send EOI first thing. */
1283 #if defined(SMP) && !defined(SCHED_ULE)
1285 * Don't do any accounting for the disabled HTT cores, since it
1286 * will provide misleading numbers for the userland.
1288 * No locking is necessary here, since even if we lose the race
1289 * when hlt_cpus_mask changes it is not a big deal, really.
1291 * Don't do that for ULE, since ULE doesn't consider hlt_cpus_mask
1292 * and unlike other schedulers it actually schedules threads to
1295 if (CPU_ISSET(PCPU_GET(cpuid), &hlt_cpus_mask))
1299 /* Look up our local APIC structure for the tick counters. */
1300 la = &lapics[PCPU_GET(apic_id)];
1301 (*la->la_timer_count)++;
1303 if (lapic_et.et_active) {
1305 td->td_intr_nesting_level++;
1306 oldframe = td->td_intr_frame;
1307 td->td_intr_frame = frame;
1308 lapic_et.et_event_cb(&lapic_et, lapic_et.et_arg);
1309 td->td_intr_frame = oldframe;
1310 td->td_intr_nesting_level--;
1316 lapic_timer_set_divisor(u_int divisor)
1319 KASSERT(powerof2(divisor), ("lapic: invalid divisor %u", divisor));
1320 KASSERT(ffs(divisor) <= nitems(lapic_timer_divisors),
1321 ("lapic: invalid divisor %u", divisor));
1322 lapic_write32(LAPIC_DCR_TIMER, lapic_timer_divisors[ffs(divisor) - 1]);
1326 lapic_timer_oneshot(struct lapic *la)
1330 value = la->lvt_timer_base;
1331 value &= ~(APIC_LVTT_TM | APIC_LVT_M);
1332 value |= APIC_LVTT_TM_ONE_SHOT;
1333 la->lvt_timer_last = value;
1334 lapic_write32(LAPIC_LVT_TIMER, value);
1335 lapic_write32(LAPIC_ICR_TIMER, la->la_timer_period);
1339 lapic_timer_oneshot_nointr(struct lapic *la, uint32_t count)
1343 value = la->lvt_timer_base;
1344 value &= ~APIC_LVTT_TM;
1345 value |= APIC_LVTT_TM_ONE_SHOT | APIC_LVT_M;
1346 la->lvt_timer_last = value;
1347 lapic_write32(LAPIC_LVT_TIMER, value);
1348 lapic_write32(LAPIC_ICR_TIMER, count);
1352 lapic_timer_periodic(struct lapic *la)
1356 value = la->lvt_timer_base;
1357 value &= ~(APIC_LVTT_TM | APIC_LVT_M);
1358 value |= APIC_LVTT_TM_PERIODIC;
1359 la->lvt_timer_last = value;
1360 lapic_write32(LAPIC_LVT_TIMER, value);
1361 lapic_write32(LAPIC_ICR_TIMER, la->la_timer_period);
1365 lapic_timer_deadline(struct lapic *la)
1369 value = la->lvt_timer_base;
1370 value &= ~(APIC_LVTT_TM | APIC_LVT_M);
1371 value |= APIC_LVTT_TM_TSCDLT;
1372 if (value != la->lvt_timer_last) {
1373 la->lvt_timer_last = value;
1374 lapic_write32_nofence(LAPIC_LVT_TIMER, value);
1378 wrmsr(MSR_TSC_DEADLINE, la->la_timer_period + rdtsc());
1382 lapic_timer_stop(struct lapic *la)
1386 if (la->la_timer_mode == LAT_MODE_DEADLINE) {
1387 wrmsr(MSR_TSC_DEADLINE, 0);
1390 value = la->lvt_timer_base;
1391 value &= ~APIC_LVTT_TM;
1392 value |= APIC_LVT_M;
1393 la->lvt_timer_last = value;
1394 lapic_write32(LAPIC_LVT_TIMER, value);
1399 lapic_handle_cmc(void)
1407 * Called from the mca_init() to activate the CMC interrupt if this CPU is
1408 * responsible for monitoring any MC banks for CMC events. Since mca_init()
1409 * is called prior to lapic_setup() during boot, this just needs to unmask
1410 * this CPU's LVT_CMCI entry.
1413 native_lapic_enable_cmc(void)
1418 if (!x2apic_mode && lapic_map == NULL)
1421 apic_id = PCPU_GET(apic_id);
1422 KASSERT(lapics[apic_id].la_present,
1423 ("%s: missing APIC %u", __func__, apic_id));
1424 lapics[apic_id].la_lvts[APIC_LVT_CMCI].lvt_masked = 0;
1425 lapics[apic_id].la_lvts[APIC_LVT_CMCI].lvt_active = 1;
1427 printf("lapic%u: CMCI unmasked\n", apic_id);
1431 native_lapic_enable_mca_elvt(void)
1438 if (lapic_map == NULL)
1442 apic_id = PCPU_GET(apic_id);
1443 KASSERT(lapics[apic_id].la_present,
1444 ("%s: missing APIC %u", __func__, apic_id));
1445 elvt_count = amd_read_elvt_count();
1446 if (elvt_count <= APIC_ELVT_MCA)
1449 value = lapic_read32(LAPIC_EXT_LVT0 + APIC_ELVT_MCA);
1450 if ((value & APIC_LVT_M) == 0) {
1452 printf("AMD MCE Thresholding Extended LVT is already active\n");
1453 return (APIC_ELVT_MCA);
1455 lapics[apic_id].la_elvts[APIC_ELVT_MCA].lvt_masked = 0;
1456 lapics[apic_id].la_elvts[APIC_ELVT_MCA].lvt_active = 1;
1458 printf("lapic%u: MCE Thresholding ELVT unmasked\n", apic_id);
1459 return (APIC_ELVT_MCA);
1463 lapic_handle_error(void)
1468 * Read the contents of the error status register. Write to
1469 * the register first before reading from it to force the APIC
1470 * to update its value to indicate any errors that have
1471 * occurred since the previous write to the register.
1473 lapic_write32(LAPIC_ESR, 0);
1474 esr = lapic_read32(LAPIC_ESR);
1476 printf("CPU%d: local APIC error 0x%x\n", PCPU_GET(cpuid), esr);
1481 native_apic_cpuid(u_int apic_id)
1484 return apic_cpuids[apic_id];
1490 /* Request a free IDT vector to be used by the specified IRQ. */
1492 native_apic_alloc_vector(u_int apic_id, u_int irq)
1496 KASSERT(irq < NUM_IO_INTS, ("Invalid IRQ %u", irq));
1499 * Search for a free vector. Currently we just use a very simple
1500 * algorithm to find the first free vector.
1502 mtx_lock_spin(&icu_lock);
1503 for (vector = 0; vector < APIC_NUM_IOINTS; vector++) {
1504 if (lapics[apic_id].la_ioint_irqs[vector] != -1)
1506 lapics[apic_id].la_ioint_irqs[vector] = irq;
1507 mtx_unlock_spin(&icu_lock);
1508 return (vector + APIC_IO_INTS);
1510 mtx_unlock_spin(&icu_lock);
1515 * Request 'count' free contiguous IDT vectors to be used by 'count'
1516 * IRQs. 'count' must be a power of two and the vectors will be
1517 * aligned on a boundary of 'align'. If the request cannot be
1518 * satisfied, 0 is returned.
1521 native_apic_alloc_vectors(u_int apic_id, u_int *irqs, u_int count, u_int align)
1523 u_int first, run, vector;
1525 KASSERT(powerof2(count), ("bad count"));
1526 KASSERT(powerof2(align), ("bad align"));
1527 KASSERT(align >= count, ("align < count"));
1529 for (run = 0; run < count; run++)
1530 KASSERT(irqs[run] < NUM_IO_INTS, ("Invalid IRQ %u at index %u",
1535 * Search for 'count' free vectors. As with apic_alloc_vector(),
1536 * this just uses a simple first fit algorithm.
1540 mtx_lock_spin(&icu_lock);
1541 for (vector = 0; vector < APIC_NUM_IOINTS; vector++) {
1543 /* Vector is in use, end run. */
1544 if (lapics[apic_id].la_ioint_irqs[vector] != -1) {
1550 /* Start a new run if run == 0 and vector is aligned. */
1552 if ((vector & (align - 1)) != 0)
1558 /* Keep looping if the run isn't long enough yet. */
1562 /* Found a run, assign IRQs and return the first vector. */
1563 for (vector = 0; vector < count; vector++)
1564 lapics[apic_id].la_ioint_irqs[first + vector] =
1566 mtx_unlock_spin(&icu_lock);
1567 return (first + APIC_IO_INTS);
1569 mtx_unlock_spin(&icu_lock);
1570 printf("APIC: Couldn't find APIC vectors for %u IRQs\n", count);
1575 * Enable a vector for a particular apic_id. Since all lapics share idt
1576 * entries and ioint_handlers this enables the vector on all lapics. lapics
1577 * which do not have the vector configured would report spurious interrupts
1581 native_apic_enable_vector(u_int apic_id, u_int vector)
1584 KASSERT(vector != IDT_SYSCALL, ("Attempt to overwrite syscall entry"));
1585 KASSERT(ioint_handlers[vector / 32] != NULL,
1586 ("No ISR handler for vector %u", vector));
1587 #ifdef KDTRACE_HOOKS
1588 KASSERT(vector != IDT_DTRACE_RET,
1589 ("Attempt to overwrite DTrace entry"));
1591 setidt(vector, (pti ? ioint_pti_handlers : ioint_handlers)[vector / 32],
1592 SDT_APIC, SEL_KPL, GSEL_APIC);
1596 native_apic_disable_vector(u_int apic_id, u_int vector)
1599 KASSERT(vector != IDT_SYSCALL, ("Attempt to overwrite syscall entry"));
1600 #ifdef KDTRACE_HOOKS
1601 KASSERT(vector != IDT_DTRACE_RET,
1602 ("Attempt to overwrite DTrace entry"));
1604 KASSERT(ioint_handlers[vector / 32] != NULL,
1605 ("No ISR handler for vector %u", vector));
1608 * We can not currently clear the idt entry because other cpus
1609 * may have a valid vector at this offset.
1611 setidt(vector, pti ? &IDTVEC(rsvd_pti) : &IDTVEC(rsvd), SDT_APIC,
1612 SEL_KPL, GSEL_APIC);
1616 /* Release an APIC vector when it's no longer in use. */
1618 native_apic_free_vector(u_int apic_id, u_int vector, u_int irq)
1622 KASSERT(vector >= APIC_IO_INTS && vector != IDT_SYSCALL &&
1623 vector <= APIC_IO_INTS + APIC_NUM_IOINTS,
1624 ("Vector %u does not map to an IRQ line", vector));
1625 KASSERT(irq < NUM_IO_INTS, ("Invalid IRQ %u", irq));
1626 KASSERT(lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS] ==
1627 irq, ("IRQ mismatch"));
1628 #ifdef KDTRACE_HOOKS
1629 KASSERT(vector != IDT_DTRACE_RET,
1630 ("Attempt to overwrite DTrace entry"));
1634 * Bind us to the cpu that owned the vector before freeing it so
1635 * we don't lose an interrupt delivery race.
1640 if (sched_is_bound(td))
1641 panic("apic_free_vector: Thread already bound.\n");
1642 sched_bind(td, apic_cpuid(apic_id));
1645 mtx_lock_spin(&icu_lock);
1646 lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS] = -1;
1647 mtx_unlock_spin(&icu_lock);
1655 /* Map an IDT vector (APIC) to an IRQ (interrupt source). */
1657 apic_idt_to_irq(u_int apic_id, u_int vector)
1661 KASSERT(vector >= APIC_IO_INTS && vector != IDT_SYSCALL &&
1662 vector <= APIC_IO_INTS + APIC_NUM_IOINTS,
1663 ("Vector %u does not map to an IRQ line", vector));
1664 #ifdef KDTRACE_HOOKS
1665 KASSERT(vector != IDT_DTRACE_RET,
1666 ("Attempt to overwrite DTrace entry"));
1668 irq = lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS];
1676 * Dump data about APIC IDT vector mappings.
1678 DB_SHOW_COMMAND(apic, db_show_apic)
1680 struct intsrc *isrc;
1685 if (strcmp(modif, "vv") == 0)
1687 else if (strcmp(modif, "v") == 0)
1691 for (apic_id = 0; apic_id <= max_apic_id; apic_id++) {
1692 if (lapics[apic_id].la_present == 0)
1694 db_printf("Interrupts bound to lapic %u\n", apic_id);
1695 for (i = 0; i < APIC_NUM_IOINTS + 1 && !db_pager_quit; i++) {
1696 irq = lapics[apic_id].la_ioint_irqs[i];
1697 if (irq == -1 || irq == IRQ_SYSCALL)
1699 #ifdef KDTRACE_HOOKS
1700 if (irq == IRQ_DTRACE_RET)
1704 if (irq == IRQ_EVTCHN)
1707 db_printf("vec 0x%2x -> ", i + APIC_IO_INTS);
1708 if (irq == IRQ_TIMER)
1709 db_printf("lapic timer\n");
1710 else if (irq < NUM_IO_INTS) {
1711 isrc = intr_lookup_source(irq);
1712 if (isrc == NULL || verbose == 0)
1713 db_printf("IRQ %u\n", irq);
1715 db_dump_intr_event(isrc->is_event,
1718 db_printf("IRQ %u ???\n", irq);
1724 dump_mask(const char *prefix, uint32_t v, int base)
1729 for (i = 0; i < 32; i++)
1732 db_printf("%s:", prefix);
1735 db_printf(" %02x", base + i);
1741 /* Show info from the lapic regs for this CPU. */
1742 DB_SHOW_COMMAND(lapic, db_show_lapic)
1746 db_printf("lapic ID = %d\n", lapic_id());
1747 v = lapic_read32(LAPIC_VERSION);
1748 db_printf("version = %d.%d\n", (v & APIC_VER_VERSION) >> 4,
1750 db_printf("max LVT = %d\n", (v & APIC_VER_MAXLVT) >> MAXLVTSHIFT);
1751 v = lapic_read32(LAPIC_SVR);
1752 db_printf("SVR = %02x (%s)\n", v & APIC_SVR_VECTOR,
1753 v & APIC_SVR_ENABLE ? "enabled" : "disabled");
1754 db_printf("TPR = %02x\n", lapic_read32(LAPIC_TPR));
1756 #define dump_field(prefix, regn, index) \
1757 dump_mask(__XSTRING(prefix ## index), \
1758 lapic_read32(LAPIC_ ## regn ## index), \
1761 db_printf("In-service Interrupts:\n");
1762 dump_field(isr, ISR, 0);
1763 dump_field(isr, ISR, 1);
1764 dump_field(isr, ISR, 2);
1765 dump_field(isr, ISR, 3);
1766 dump_field(isr, ISR, 4);
1767 dump_field(isr, ISR, 5);
1768 dump_field(isr, ISR, 6);
1769 dump_field(isr, ISR, 7);
1771 db_printf("TMR Interrupts:\n");
1772 dump_field(tmr, TMR, 0);
1773 dump_field(tmr, TMR, 1);
1774 dump_field(tmr, TMR, 2);
1775 dump_field(tmr, TMR, 3);
1776 dump_field(tmr, TMR, 4);
1777 dump_field(tmr, TMR, 5);
1778 dump_field(tmr, TMR, 6);
1779 dump_field(tmr, TMR, 7);
1781 db_printf("IRR Interrupts:\n");
1782 dump_field(irr, IRR, 0);
1783 dump_field(irr, IRR, 1);
1784 dump_field(irr, IRR, 2);
1785 dump_field(irr, IRR, 3);
1786 dump_field(irr, IRR, 4);
1787 dump_field(irr, IRR, 5);
1788 dump_field(irr, IRR, 6);
1789 dump_field(irr, IRR, 7);
1796 * APIC probing support code. This includes code to manage enumerators.
1799 static SLIST_HEAD(, apic_enumerator) enumerators =
1800 SLIST_HEAD_INITIALIZER(enumerators);
1801 static struct apic_enumerator *best_enum;
1804 apic_register_enumerator(struct apic_enumerator *enumerator)
1807 struct apic_enumerator *apic_enum;
1809 SLIST_FOREACH(apic_enum, &enumerators, apic_next) {
1810 if (apic_enum == enumerator)
1811 panic("%s: Duplicate register of %s", __func__,
1812 enumerator->apic_name);
1815 SLIST_INSERT_HEAD(&enumerators, enumerator, apic_next);
1819 * We have to look for CPU's very, very early because certain subsystems
1820 * want to know how many CPU's we have extremely early on in the boot
1824 apic_init(void *dummy __unused)
1826 struct apic_enumerator *enumerator;
1829 /* We only support built in local APICs. */
1830 if (!(cpu_feature & CPUID_APIC))
1833 /* Don't probe if APIC mode is disabled. */
1834 if (resource_disabled("apic", 0))
1837 /* Probe all the enumerators to find the best match. */
1840 SLIST_FOREACH(enumerator, &enumerators, apic_next) {
1841 retval = enumerator->apic_probe();
1844 if (best_enum == NULL || best < retval) {
1845 best_enum = enumerator;
1849 if (best_enum == NULL) {
1851 printf("APIC: Could not find any APICs.\n");
1853 panic("running without device atpic requires a local APIC");
1859 printf("APIC: Using the %s enumerator.\n",
1860 best_enum->apic_name);
1864 * To work around an errata, we disable the local APIC on some
1865 * CPUs during early startup. We need to turn the local APIC back
1866 * on on such CPUs now.
1868 ppro_reenable_apic();
1871 /* Probe the CPU's in the system. */
1872 retval = best_enum->apic_probe_cpus();
1874 printf("%s: Failed to probe CPUs: returned %d\n",
1875 best_enum->apic_name, retval);
1878 SYSINIT(apic_init, SI_SUB_TUNABLES - 1, SI_ORDER_SECOND, apic_init, NULL);
1881 * Setup the local APIC. We have to do this prior to starting up the APs
1885 apic_setup_local(void *dummy __unused)
1889 if (best_enum == NULL)
1892 lapics = malloc(sizeof(*lapics) * (max_apic_id + 1), M_LAPIC,
1895 /* Initialize the local APIC. */
1896 retval = best_enum->apic_setup_local();
1898 printf("%s: Failed to setup the local APIC: returned %d\n",
1899 best_enum->apic_name, retval);
1901 SYSINIT(apic_setup_local, SI_SUB_CPU, SI_ORDER_SECOND, apic_setup_local, NULL);
1904 * Setup the I/O APICs.
1907 apic_setup_io(void *dummy __unused)
1911 if (best_enum == NULL)
1915 * Local APIC must be registered before other PICs and pseudo PICs
1916 * for proper suspend/resume order.
1918 intr_register_pic(&lapic_pic);
1920 retval = best_enum->apic_setup_io();
1922 printf("%s: Failed to setup I/O APICs: returned %d\n",
1923 best_enum->apic_name, retval);
1926 * Finish setting up the local APIC on the BSP once we know
1927 * how to properly program the LINT pins. In particular, this
1928 * enables the EOI suppression mode, if LAPIC support it and
1929 * user did not disabled the mode.
1935 /* Enable the MSI "pic". */
1936 init_ops.msi_init();
1938 SYSINIT(apic_setup_io, SI_SUB_INTR, SI_ORDER_THIRD, apic_setup_io, NULL);
1942 * Inter Processor Interrupt functions. The lapic_ipi_*() functions are
1943 * private to the MD code. The public interface for the rest of the
1944 * kernel is defined in mp_machdep.c.
1948 * Wait delay microseconds for IPI to be sent. If delay is -1, we
1952 native_lapic_ipi_wait(int delay)
1956 /* LAPIC_ICR.APIC_DELSTAT_MASK is undefined in x2APIC mode */
1960 for (rx = 0; delay == -1 || rx < lapic_ipi_wait_mult * delay; rx++) {
1961 if ((lapic_read_icr_lo() & APIC_DELSTAT_MASK) ==
1970 native_lapic_ipi_raw(register_t icrlo, u_int dest)
1974 register_t saveintr;
1976 /* XXX: Need more sanity checking of icrlo? */
1977 KASSERT(x2apic_mode || lapic_map != NULL,
1978 ("%s called too early", __func__));
1979 KASSERT(x2apic_mode ||
1980 (dest & ~(APIC_ID_MASK >> APIC_ID_SHIFT)) == 0,
1981 ("%s: invalid dest field", __func__));
1982 KASSERT((icrlo & APIC_ICRLO_RESV_MASK) == 0,
1983 ("%s: reserved bits set in ICR LO register", __func__));
1985 /* Set destination in ICR HI register if it is being used. */
1987 saveintr = intr_disable();
1988 icr = lapic_read_icr();
1991 if ((icrlo & APIC_DEST_MASK) == APIC_DEST_DESTFLD) {
1996 vhi &= ~APIC_ID_MASK;
1997 vhi |= dest << APIC_ID_SHIFT;
2003 /* Program the contents of the IPI and dispatch it. */
2008 vlo &= APIC_ICRLO_RESV_MASK;
2011 lapic_write_icr(vhi, vlo);
2013 intr_restore(saveintr);
2016 #define BEFORE_SPIN 50000
2017 #ifdef DETECT_DEADLOCK
2018 #define AFTER_SPIN 50
2022 native_lapic_ipi_vectored(u_int vector, int dest)
2024 register_t icrlo, destfield;
2026 KASSERT((vector & ~APIC_VECTOR_MASK) == 0,
2027 ("%s: invalid vector %d", __func__, vector));
2029 icrlo = APIC_DESTMODE_PHY | APIC_TRIGMOD_EDGE | APIC_LEVEL_ASSERT;
2032 * NMI IPIs are just fake vectors used to send a NMI. Use special rules
2033 * regarding NMIs if passed, otherwise specify the vector.
2035 if (vector >= IPI_NMI_FIRST)
2036 icrlo |= APIC_DELMODE_NMI;
2038 icrlo |= vector | APIC_DELMODE_FIXED;
2041 case APIC_IPI_DEST_SELF:
2042 icrlo |= APIC_DEST_SELF;
2044 case APIC_IPI_DEST_ALL:
2045 icrlo |= APIC_DEST_ALLISELF;
2047 case APIC_IPI_DEST_OTHERS:
2048 icrlo |= APIC_DEST_ALLESELF;
2051 KASSERT(x2apic_mode ||
2052 (dest & ~(APIC_ID_MASK >> APIC_ID_SHIFT)) == 0,
2053 ("%s: invalid destination 0x%x", __func__, dest));
2057 /* Wait for an earlier IPI to finish. */
2058 if (!lapic_ipi_wait(BEFORE_SPIN)) {
2059 if (panicstr != NULL)
2062 panic("APIC: Previous IPI is stuck");
2065 lapic_ipi_raw(icrlo, destfield);
2067 #ifdef DETECT_DEADLOCK
2068 /* Wait for IPI to be delivered. */
2069 if (!lapic_ipi_wait(AFTER_SPIN)) {
2070 #ifdef needsattention
2074 * The above function waits for the message to actually be
2075 * delivered. It breaks out after an arbitrary timeout
2076 * since the message should eventually be delivered (at
2077 * least in theory) and that if it wasn't we would catch
2078 * the failure with the check above when the next IPI is
2081 * We could skip this wait entirely, EXCEPT it probably
2082 * protects us from other routines that assume that the
2083 * message was delivered and acted upon when this function
2086 printf("APIC: IPI might be stuck\n");
2087 #else /* !needsattention */
2088 /* Wait until mesage is sent without a timeout. */
2089 while (lapic_read_icr_lo() & APIC_DELSTAT_PEND)
2091 #endif /* needsattention */
2093 #endif /* DETECT_DEADLOCK */
2099 * Since the IDT is shared by all CPUs the IPI slot update needs to be globally
2102 * Consider the case where an IPI is generated immediately after allocation:
2103 * vector = lapic_ipi_alloc(ipifunc);
2104 * ipi_selected(other_cpus, vector);
2106 * In xAPIC mode a write to ICR_LO has serializing semantics because the
2107 * APIC page is mapped as an uncached region. In x2APIC mode there is an
2108 * explicit 'mfence' before the ICR MSR is written. Therefore in both cases
2109 * the IDT slot update is globally visible before the IPI is delivered.
2112 native_lapic_ipi_alloc(inthand_t *ipifunc)
2114 struct gate_descriptor *ip;
2118 KASSERT(ipifunc != &IDTVEC(rsvd) && ipifunc != &IDTVEC(rsvd_pti),
2119 ("invalid ipifunc %p", ipifunc));
2122 mtx_lock_spin(&icu_lock);
2123 for (idx = IPI_DYN_FIRST; idx <= IPI_DYN_LAST; idx++) {
2125 func = (ip->gd_hioffset << 16) | ip->gd_looffset;
2126 if ((!pti && func == (uintptr_t)&IDTVEC(rsvd)) ||
2127 (pti && func == (uintptr_t)&IDTVEC(rsvd_pti))) {
2129 setidt(vector, ipifunc, SDT_APIC, SEL_KPL, GSEL_APIC);
2133 mtx_unlock_spin(&icu_lock);
2138 native_lapic_ipi_free(int vector)
2140 struct gate_descriptor *ip;
2143 KASSERT(vector >= IPI_DYN_FIRST && vector <= IPI_DYN_LAST,
2144 ("%s: invalid vector %d", __func__, vector));
2146 mtx_lock_spin(&icu_lock);
2148 func = (ip->gd_hioffset << 16) | ip->gd_looffset;
2149 KASSERT(func != (uintptr_t)&IDTVEC(rsvd) &&
2150 func != (uintptr_t)&IDTVEC(rsvd_pti),
2151 ("invalid idtfunc %#lx", func));
2152 setidt(vector, pti ? &IDTVEC(rsvd_pti) : &IDTVEC(rsvd), SDT_APIC,
2153 SEL_KPL, GSEL_APIC);
2154 mtx_unlock_spin(&icu_lock);