io_apic.c revision 307258
1/*-
2 * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/10/sys/x86/x86/io_apic.c 307258 2016-10-14 04:13:59Z sephe $");
29
30#include "opt_isa.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/bus.h>
35#include <sys/kernel.h>
36#include <sys/lock.h>
37#include <sys/malloc.h>
38#include <sys/module.h>
39#include <sys/mutex.h>
40#include <sys/sysctl.h>
41
42#include <dev/pci/pcireg.h>
43#include <dev/pci/pcivar.h>
44
45#include <vm/vm.h>
46#include <vm/pmap.h>
47
48#include <x86/apicreg.h>
49#include <machine/frame.h>
50#include <machine/intr_machdep.h>
51#include <machine/apicvar.h>
52#include <machine/resource.h>
53#include <machine/segments.h>
54
55#define IOAPIC_ISA_INTS		16
56#define	IOAPIC_MEM_REGION	32
57#define	IOAPIC_REDTBL_LO(i)	(IOAPIC_REDTBL + (i) * 2)
58#define	IOAPIC_REDTBL_HI(i)	(IOAPIC_REDTBL_LO(i) + 1)
59
60#define	IRQ_EXTINT		(NUM_IO_INTS + 1)
61#define	IRQ_NMI			(NUM_IO_INTS + 2)
62#define	IRQ_SMI			(NUM_IO_INTS + 3)
63#define	IRQ_DISABLED		(NUM_IO_INTS + 4)
64
65static MALLOC_DEFINE(M_IOAPIC, "io_apic", "I/O APIC structures");
66
67/*
68 * I/O APIC interrupt source driver.  Each pin is assigned an IRQ cookie
69 * as laid out in the ACPI System Interrupt number model where each I/O
70 * APIC has a contiguous chunk of the System Interrupt address space.
71 * We assume that IRQs 1 - 15 behave like ISA IRQs and that all other
72 * IRQs behave as PCI IRQs by default.  We also assume that the pin for
73 * IRQ 0 is actually an ExtINT pin.  The apic enumerators override the
74 * configuration of individual pins as indicated by their tables.
75 *
76 * Documentation for the I/O APIC: "82093AA I/O Advanced Programmable
77 * Interrupt Controller (IOAPIC)", May 1996, Intel Corp.
78 * ftp://download.intel.com/design/chipsets/datashts/29056601.pdf
79 */
80
81struct ioapic_intsrc {
82	struct intsrc io_intsrc;
83	u_int io_irq;
84	u_int io_intpin:8;
85	u_int io_vector:8;
86	u_int io_cpu:8;
87	u_int io_activehi:1;
88	u_int io_edgetrigger:1;
89	u_int io_masked:1;
90	int io_bus:4;
91	uint32_t io_lowreg;
92};
93
94struct ioapic {
95	struct pic io_pic;
96	u_int io_id:8;			/* logical ID */
97	u_int io_apic_id:4;
98	u_int io_intbase:8;		/* System Interrupt base */
99	u_int io_numintr:8;
100	volatile ioapic_t *io_addr;	/* XXX: should use bus_space */
101	vm_paddr_t io_paddr;
102	STAILQ_ENTRY(ioapic) io_next;
103	struct ioapic_intsrc io_pins[0];
104};
105
106static u_int	ioapic_read(volatile ioapic_t *apic, int reg);
107static void	ioapic_write(volatile ioapic_t *apic, int reg, u_int val);
108static const char *ioapic_bus_string(int bus_type);
109static void	ioapic_print_irq(struct ioapic_intsrc *intpin);
110static void	ioapic_enable_source(struct intsrc *isrc);
111static void	ioapic_disable_source(struct intsrc *isrc, int eoi);
112static void	ioapic_eoi_source(struct intsrc *isrc);
113static void	ioapic_enable_intr(struct intsrc *isrc);
114static void	ioapic_disable_intr(struct intsrc *isrc);
115static int	ioapic_vector(struct intsrc *isrc);
116static int	ioapic_source_pending(struct intsrc *isrc);
117static int	ioapic_config_intr(struct intsrc *isrc, enum intr_trigger trig,
118		    enum intr_polarity pol);
119static void	ioapic_resume(struct pic *pic, bool suspend_cancelled);
120static int	ioapic_assign_cpu(struct intsrc *isrc, u_int apic_id);
121static void	ioapic_program_intpin(struct ioapic_intsrc *intpin);
122
123static STAILQ_HEAD(,ioapic) ioapic_list = STAILQ_HEAD_INITIALIZER(ioapic_list);
124struct pic ioapic_template = { ioapic_enable_source, ioapic_disable_source,
125			       ioapic_eoi_source, ioapic_enable_intr,
126			       ioapic_disable_intr, ioapic_vector,
127			       ioapic_source_pending, NULL, ioapic_resume,
128			       ioapic_config_intr, ioapic_assign_cpu };
129
130static int next_ioapic_base;
131static u_int next_id;
132
133static SYSCTL_NODE(_hw, OID_AUTO, apic, CTLFLAG_RD, 0, "APIC options");
134static int enable_extint;
135SYSCTL_INT(_hw_apic, OID_AUTO, enable_extint, CTLFLAG_RDTUN, &enable_extint, 0,
136    "Enable the ExtINT pin in the first I/O APIC");
137TUNABLE_INT("hw.apic.enable_extint", &enable_extint);
138
139static __inline void
140_ioapic_eoi_source(struct intsrc *isrc)
141{
142	lapic_eoi();
143}
144
145static u_int
146ioapic_read(volatile ioapic_t *apic, int reg)
147{
148
149	mtx_assert(&icu_lock, MA_OWNED);
150	apic->ioregsel = reg;
151	return (apic->iowin);
152}
153
154static void
155ioapic_write(volatile ioapic_t *apic, int reg, u_int val)
156{
157
158	mtx_assert(&icu_lock, MA_OWNED);
159	apic->ioregsel = reg;
160	apic->iowin = val;
161}
162
163static const char *
164ioapic_bus_string(int bus_type)
165{
166
167	switch (bus_type) {
168	case APIC_BUS_ISA:
169		return ("ISA");
170	case APIC_BUS_EISA:
171		return ("EISA");
172	case APIC_BUS_PCI:
173		return ("PCI");
174	default:
175		return ("unknown");
176	}
177}
178
179static void
180ioapic_print_irq(struct ioapic_intsrc *intpin)
181{
182
183	switch (intpin->io_irq) {
184	case IRQ_DISABLED:
185		printf("disabled");
186		break;
187	case IRQ_EXTINT:
188		printf("ExtINT");
189		break;
190	case IRQ_NMI:
191		printf("NMI");
192		break;
193	case IRQ_SMI:
194		printf("SMI");
195		break;
196	default:
197		printf("%s IRQ %u", ioapic_bus_string(intpin->io_bus),
198		    intpin->io_irq);
199	}
200}
201
202static void
203ioapic_enable_source(struct intsrc *isrc)
204{
205	struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
206	struct ioapic *io = (struct ioapic *)isrc->is_pic;
207	uint32_t flags;
208
209	mtx_lock_spin(&icu_lock);
210	if (intpin->io_masked) {
211		flags = intpin->io_lowreg & ~IOART_INTMASK;
212		ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
213		    flags);
214		intpin->io_masked = 0;
215	}
216	mtx_unlock_spin(&icu_lock);
217}
218
219static void
220ioapic_disable_source(struct intsrc *isrc, int eoi)
221{
222	struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
223	struct ioapic *io = (struct ioapic *)isrc->is_pic;
224	uint32_t flags;
225
226	mtx_lock_spin(&icu_lock);
227	if (!intpin->io_masked && !intpin->io_edgetrigger) {
228		flags = intpin->io_lowreg | IOART_INTMSET;
229		ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
230		    flags);
231		intpin->io_masked = 1;
232	}
233
234	if (eoi == PIC_EOI)
235		_ioapic_eoi_source(isrc);
236
237	mtx_unlock_spin(&icu_lock);
238}
239
240static void
241ioapic_eoi_source(struct intsrc *isrc)
242{
243
244	_ioapic_eoi_source(isrc);
245}
246
247/*
248 * Completely program an intpin based on the data in its interrupt source
249 * structure.
250 */
251static void
252ioapic_program_intpin(struct ioapic_intsrc *intpin)
253{
254	struct ioapic *io = (struct ioapic *)intpin->io_intsrc.is_pic;
255	uint32_t low, high, value;
256
257	/*
258	 * If a pin is completely invalid or if it is valid but hasn't
259	 * been enabled yet, just ensure that the pin is masked.
260	 */
261	mtx_assert(&icu_lock, MA_OWNED);
262	if (intpin->io_irq == IRQ_DISABLED || (intpin->io_irq < NUM_IO_INTS &&
263	    intpin->io_vector == 0)) {
264		low = ioapic_read(io->io_addr,
265		    IOAPIC_REDTBL_LO(intpin->io_intpin));
266		if ((low & IOART_INTMASK) == IOART_INTMCLR)
267			ioapic_write(io->io_addr,
268			    IOAPIC_REDTBL_LO(intpin->io_intpin),
269			    low | IOART_INTMSET);
270		return;
271	}
272
273	/* Set the destination. */
274	low = IOART_DESTPHY;
275	high = intpin->io_cpu << APIC_ID_SHIFT;
276
277	/* Program the rest of the low word. */
278	if (intpin->io_edgetrigger)
279		low |= IOART_TRGREDG;
280	else
281		low |= IOART_TRGRLVL;
282	if (intpin->io_activehi)
283		low |= IOART_INTAHI;
284	else
285		low |= IOART_INTALO;
286	if (intpin->io_masked)
287		low |= IOART_INTMSET;
288	switch (intpin->io_irq) {
289	case IRQ_EXTINT:
290		KASSERT(intpin->io_edgetrigger,
291		    ("ExtINT not edge triggered"));
292		low |= IOART_DELEXINT;
293		break;
294	case IRQ_NMI:
295		KASSERT(intpin->io_edgetrigger,
296		    ("NMI not edge triggered"));
297		low |= IOART_DELNMI;
298		break;
299	case IRQ_SMI:
300		KASSERT(intpin->io_edgetrigger,
301		    ("SMI not edge triggered"));
302		low |= IOART_DELSMI;
303		break;
304	default:
305		KASSERT(intpin->io_vector != 0, ("No vector for IRQ %u",
306		    intpin->io_irq));
307		low |= IOART_DELFIXED | intpin->io_vector;
308	}
309
310	/* Write the values to the APIC. */
311	value = ioapic_read(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin));
312	value &= ~IOART_DEST;
313	value |= high;
314	ioapic_write(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin), value);
315	intpin->io_lowreg = low;
316	ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin), low);
317}
318
319static int
320ioapic_assign_cpu(struct intsrc *isrc, u_int apic_id)
321{
322	struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
323	struct ioapic *io = (struct ioapic *)isrc->is_pic;
324	u_int old_vector, new_vector;
325	u_int old_id;
326
327	/*
328	 * On Hyper-V:
329	 * - Stick to the first cpu for all I/O APIC pins.
330	 * - And don't allow destination cpu changes.
331	 */
332	if (vm_guest == VM_GUEST_HV) {
333		if (intpin->io_vector)
334			return (EINVAL);
335		else
336			apic_id = 0;
337	}
338
339	/*
340	 * keep 1st core as the destination for NMI
341	 */
342	if (intpin->io_irq == IRQ_NMI)
343		apic_id = 0;
344
345	/*
346	 * Set us up to free the old irq.
347	 */
348	old_vector = intpin->io_vector;
349	old_id = intpin->io_cpu;
350	if (old_vector && apic_id == old_id)
351		return (0);
352
353	/*
354	 * Allocate an APIC vector for this interrupt pin.  Once
355	 * we have a vector we program the interrupt pin.
356	 */
357	new_vector = apic_alloc_vector(apic_id, intpin->io_irq);
358	if (new_vector == 0)
359		return (ENOSPC);
360
361	/*
362	 * Mask the old intpin if it is enabled while it is migrated.
363	 *
364	 * At least some level-triggered interrupts seem to need the
365	 * extra DELAY() to avoid being stuck in a non-EOI'd state.
366	 */
367	mtx_lock_spin(&icu_lock);
368	if (!intpin->io_masked && !intpin->io_edgetrigger) {
369		ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
370		    intpin->io_lowreg | IOART_INTMSET);
371		mtx_unlock_spin(&icu_lock);
372		DELAY(100);
373		mtx_lock_spin(&icu_lock);
374	}
375
376	intpin->io_cpu = apic_id;
377	intpin->io_vector = new_vector;
378	if (isrc->is_handlers > 0)
379		apic_enable_vector(intpin->io_cpu, intpin->io_vector);
380	if (bootverbose) {
381		printf("ioapic%u: routing intpin %u (", io->io_id,
382		    intpin->io_intpin);
383		ioapic_print_irq(intpin);
384		printf(") to lapic %u vector %u\n", intpin->io_cpu,
385		    intpin->io_vector);
386	}
387	ioapic_program_intpin(intpin);
388	mtx_unlock_spin(&icu_lock);
389
390	/*
391	 * Free the old vector after the new one is established.  This is done
392	 * to prevent races where we could miss an interrupt.
393	 */
394	if (old_vector) {
395		if (isrc->is_handlers > 0)
396			apic_disable_vector(old_id, old_vector);
397		apic_free_vector(old_id, old_vector, intpin->io_irq);
398	}
399	return (0);
400}
401
402static void
403ioapic_enable_intr(struct intsrc *isrc)
404{
405	struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
406
407	if (intpin->io_vector == 0)
408		if (ioapic_assign_cpu(isrc, intr_next_cpu()) != 0)
409			panic("Couldn't find an APIC vector for IRQ %d",
410			    intpin->io_irq);
411	apic_enable_vector(intpin->io_cpu, intpin->io_vector);
412}
413
414
415static void
416ioapic_disable_intr(struct intsrc *isrc)
417{
418	struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
419	u_int vector;
420
421	if (intpin->io_vector != 0) {
422		/* Mask this interrupt pin and free its APIC vector. */
423		vector = intpin->io_vector;
424		apic_disable_vector(intpin->io_cpu, vector);
425		mtx_lock_spin(&icu_lock);
426		intpin->io_masked = 1;
427		intpin->io_vector = 0;
428		ioapic_program_intpin(intpin);
429		mtx_unlock_spin(&icu_lock);
430		apic_free_vector(intpin->io_cpu, vector, intpin->io_irq);
431	}
432}
433
434static int
435ioapic_vector(struct intsrc *isrc)
436{
437	struct ioapic_intsrc *pin;
438
439	pin = (struct ioapic_intsrc *)isrc;
440	return (pin->io_irq);
441}
442
443static int
444ioapic_source_pending(struct intsrc *isrc)
445{
446	struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
447
448	if (intpin->io_vector == 0)
449		return 0;
450	return (lapic_intr_pending(intpin->io_vector));
451}
452
453static int
454ioapic_config_intr(struct intsrc *isrc, enum intr_trigger trig,
455    enum intr_polarity pol)
456{
457	struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
458	struct ioapic *io = (struct ioapic *)isrc->is_pic;
459	int changed;
460
461	KASSERT(!(trig == INTR_TRIGGER_CONFORM || pol == INTR_POLARITY_CONFORM),
462	    ("%s: Conforming trigger or polarity\n", __func__));
463
464	/*
465	 * EISA interrupts always use active high polarity, so don't allow
466	 * them to be set to active low.
467	 *
468	 * XXX: Should we write to the ELCR if the trigger mode changes for
469	 * an EISA IRQ or an ISA IRQ with the ELCR present?
470	 */
471	mtx_lock_spin(&icu_lock);
472	if (intpin->io_bus == APIC_BUS_EISA)
473		pol = INTR_POLARITY_HIGH;
474	changed = 0;
475	if (intpin->io_edgetrigger != (trig == INTR_TRIGGER_EDGE)) {
476		if (bootverbose)
477			printf("ioapic%u: Changing trigger for pin %u to %s\n",
478			    io->io_id, intpin->io_intpin,
479			    trig == INTR_TRIGGER_EDGE ? "edge" : "level");
480		intpin->io_edgetrigger = (trig == INTR_TRIGGER_EDGE);
481		changed++;
482	}
483	if (intpin->io_activehi != (pol == INTR_POLARITY_HIGH)) {
484		if (bootverbose)
485			printf("ioapic%u: Changing polarity for pin %u to %s\n",
486			    io->io_id, intpin->io_intpin,
487			    pol == INTR_POLARITY_HIGH ? "high" : "low");
488		intpin->io_activehi = (pol == INTR_POLARITY_HIGH);
489		changed++;
490	}
491	if (changed)
492		ioapic_program_intpin(intpin);
493	mtx_unlock_spin(&icu_lock);
494	return (0);
495}
496
497static void
498ioapic_resume(struct pic *pic, bool suspend_cancelled)
499{
500	struct ioapic *io = (struct ioapic *)pic;
501	int i;
502
503	mtx_lock_spin(&icu_lock);
504	for (i = 0; i < io->io_numintr; i++)
505		ioapic_program_intpin(&io->io_pins[i]);
506	mtx_unlock_spin(&icu_lock);
507}
508
509/*
510 * Create a plain I/O APIC object.
511 */
512void *
513ioapic_create(vm_paddr_t addr, int32_t apic_id, int intbase)
514{
515	struct ioapic *io;
516	struct ioapic_intsrc *intpin;
517	volatile ioapic_t *apic;
518	u_int numintr, i;
519	uint32_t value;
520
521	/* Map the register window so we can access the device. */
522	apic = pmap_mapdev(addr, IOAPIC_MEM_REGION);
523	mtx_lock_spin(&icu_lock);
524	value = ioapic_read(apic, IOAPIC_VER);
525	mtx_unlock_spin(&icu_lock);
526
527	/* If it's version register doesn't seem to work, punt. */
528	if (value == 0xffffffff) {
529		pmap_unmapdev((vm_offset_t)apic, IOAPIC_MEM_REGION);
530		return (NULL);
531	}
532
533	/* Determine the number of vectors and set the APIC ID. */
534	numintr = ((value & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
535	io = malloc(sizeof(struct ioapic) +
536	    numintr * sizeof(struct ioapic_intsrc), M_IOAPIC, M_WAITOK);
537	io->io_pic = ioapic_template;
538	mtx_lock_spin(&icu_lock);
539	io->io_id = next_id++;
540	io->io_apic_id = ioapic_read(apic, IOAPIC_ID) >> APIC_ID_SHIFT;
541	if (apic_id != -1 && io->io_apic_id != apic_id) {
542		ioapic_write(apic, IOAPIC_ID, apic_id << APIC_ID_SHIFT);
543		mtx_unlock_spin(&icu_lock);
544		io->io_apic_id = apic_id;
545		printf("ioapic%u: Changing APIC ID to %d\n", io->io_id,
546		    apic_id);
547	} else
548		mtx_unlock_spin(&icu_lock);
549	if (intbase == -1) {
550		intbase = next_ioapic_base;
551		printf("ioapic%u: Assuming intbase of %d\n", io->io_id,
552		    intbase);
553	} else if (intbase != next_ioapic_base && bootverbose)
554		printf("ioapic%u: WARNING: intbase %d != expected base %d\n",
555		    io->io_id, intbase, next_ioapic_base);
556	io->io_intbase = intbase;
557	next_ioapic_base = intbase + numintr;
558	io->io_numintr = numintr;
559	io->io_addr = apic;
560	io->io_paddr = addr;
561
562	/*
563	 * Initialize pins.  Start off with interrupts disabled.  Default
564	 * to active-hi and edge-triggered for ISA interrupts and active-lo
565	 * and level-triggered for all others.
566	 */
567	bzero(io->io_pins, sizeof(struct ioapic_intsrc) * numintr);
568	mtx_lock_spin(&icu_lock);
569	for (i = 0, intpin = io->io_pins; i < numintr; i++, intpin++) {
570		intpin->io_intsrc.is_pic = (struct pic *)io;
571		intpin->io_intpin = i;
572		intpin->io_irq = intbase + i;
573
574		/*
575		 * Assume that pin 0 on the first I/O APIC is an ExtINT pin.
576		 * Assume that pins 1-15 are ISA interrupts and that all
577		 * other pins are PCI interrupts.
578		 */
579		if (intpin->io_irq == 0)
580			ioapic_set_extint(io, i);
581		else if (intpin->io_irq < IOAPIC_ISA_INTS) {
582			intpin->io_bus = APIC_BUS_ISA;
583			intpin->io_activehi = 1;
584			intpin->io_edgetrigger = 1;
585			intpin->io_masked = 1;
586		} else {
587			intpin->io_bus = APIC_BUS_PCI;
588			intpin->io_activehi = 0;
589			intpin->io_edgetrigger = 0;
590			intpin->io_masked = 1;
591		}
592
593		/*
594		 * Route interrupts to the BSP by default.  Interrupts may
595		 * be routed to other CPUs later after they are enabled.
596		 */
597		intpin->io_cpu = PCPU_GET(apic_id);
598		value = ioapic_read(apic, IOAPIC_REDTBL_LO(i));
599		ioapic_write(apic, IOAPIC_REDTBL_LO(i), value | IOART_INTMSET);
600	}
601	mtx_unlock_spin(&icu_lock);
602
603	return (io);
604}
605
606int
607ioapic_get_vector(void *cookie, u_int pin)
608{
609	struct ioapic *io;
610
611	io = (struct ioapic *)cookie;
612	if (pin >= io->io_numintr)
613		return (-1);
614	return (io->io_pins[pin].io_irq);
615}
616
617int
618ioapic_disable_pin(void *cookie, u_int pin)
619{
620	struct ioapic *io;
621
622	io = (struct ioapic *)cookie;
623	if (pin >= io->io_numintr)
624		return (EINVAL);
625	if (io->io_pins[pin].io_irq == IRQ_DISABLED)
626		return (EINVAL);
627	io->io_pins[pin].io_irq = IRQ_DISABLED;
628	if (bootverbose)
629		printf("ioapic%u: intpin %d disabled\n", io->io_id, pin);
630	return (0);
631}
632
633int
634ioapic_remap_vector(void *cookie, u_int pin, int vector)
635{
636	struct ioapic *io;
637
638	io = (struct ioapic *)cookie;
639	if (pin >= io->io_numintr || vector < 0)
640		return (EINVAL);
641	if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
642		return (EINVAL);
643	io->io_pins[pin].io_irq = vector;
644	if (bootverbose)
645		printf("ioapic%u: Routing IRQ %d -> intpin %d\n", io->io_id,
646		    vector, pin);
647	return (0);
648}
649
650int
651ioapic_set_bus(void *cookie, u_int pin, int bus_type)
652{
653	struct ioapic *io;
654
655	if (bus_type < 0 || bus_type > APIC_BUS_MAX)
656		return (EINVAL);
657	io = (struct ioapic *)cookie;
658	if (pin >= io->io_numintr)
659		return (EINVAL);
660	if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
661		return (EINVAL);
662	if (io->io_pins[pin].io_bus == bus_type)
663		return (0);
664	io->io_pins[pin].io_bus = bus_type;
665	if (bootverbose)
666		printf("ioapic%u: intpin %d bus %s\n", io->io_id, pin,
667		    ioapic_bus_string(bus_type));
668	return (0);
669}
670
671int
672ioapic_set_nmi(void *cookie, u_int pin)
673{
674	struct ioapic *io;
675
676	io = (struct ioapic *)cookie;
677	if (pin >= io->io_numintr)
678		return (EINVAL);
679	if (io->io_pins[pin].io_irq == IRQ_NMI)
680		return (0);
681	if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
682		return (EINVAL);
683	io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
684	io->io_pins[pin].io_irq = IRQ_NMI;
685	io->io_pins[pin].io_masked = 0;
686	io->io_pins[pin].io_edgetrigger = 1;
687	io->io_pins[pin].io_activehi = 1;
688	if (bootverbose)
689		printf("ioapic%u: Routing NMI -> intpin %d\n",
690		    io->io_id, pin);
691	return (0);
692}
693
694int
695ioapic_set_smi(void *cookie, u_int pin)
696{
697	struct ioapic *io;
698
699	io = (struct ioapic *)cookie;
700	if (pin >= io->io_numintr)
701		return (EINVAL);
702	if (io->io_pins[pin].io_irq == IRQ_SMI)
703		return (0);
704	if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
705		return (EINVAL);
706	io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
707	io->io_pins[pin].io_irq = IRQ_SMI;
708	io->io_pins[pin].io_masked = 0;
709	io->io_pins[pin].io_edgetrigger = 1;
710	io->io_pins[pin].io_activehi = 1;
711	if (bootverbose)
712		printf("ioapic%u: Routing SMI -> intpin %d\n",
713		    io->io_id, pin);
714	return (0);
715}
716
717int
718ioapic_set_extint(void *cookie, u_int pin)
719{
720	struct ioapic *io;
721
722	io = (struct ioapic *)cookie;
723	if (pin >= io->io_numintr)
724		return (EINVAL);
725	if (io->io_pins[pin].io_irq == IRQ_EXTINT)
726		return (0);
727	if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
728		return (EINVAL);
729	io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
730	io->io_pins[pin].io_irq = IRQ_EXTINT;
731	if (enable_extint)
732		io->io_pins[pin].io_masked = 0;
733	else
734		io->io_pins[pin].io_masked = 1;
735	io->io_pins[pin].io_edgetrigger = 1;
736	io->io_pins[pin].io_activehi = 1;
737	if (bootverbose)
738		printf("ioapic%u: Routing external 8259A's -> intpin %d\n",
739		    io->io_id, pin);
740	return (0);
741}
742
743int
744ioapic_set_polarity(void *cookie, u_int pin, enum intr_polarity pol)
745{
746	struct ioapic *io;
747	int activehi;
748
749	io = (struct ioapic *)cookie;
750	if (pin >= io->io_numintr || pol == INTR_POLARITY_CONFORM)
751		return (EINVAL);
752	if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
753		return (EINVAL);
754	activehi = (pol == INTR_POLARITY_HIGH);
755	if (io->io_pins[pin].io_activehi == activehi)
756		return (0);
757	io->io_pins[pin].io_activehi = activehi;
758	if (bootverbose)
759		printf("ioapic%u: intpin %d polarity: %s\n", io->io_id, pin,
760		    pol == INTR_POLARITY_HIGH ? "high" : "low");
761	return (0);
762}
763
764int
765ioapic_set_triggermode(void *cookie, u_int pin, enum intr_trigger trigger)
766{
767	struct ioapic *io;
768	int edgetrigger;
769
770	io = (struct ioapic *)cookie;
771	if (pin >= io->io_numintr || trigger == INTR_TRIGGER_CONFORM)
772		return (EINVAL);
773	if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
774		return (EINVAL);
775	edgetrigger = (trigger == INTR_TRIGGER_EDGE);
776	if (io->io_pins[pin].io_edgetrigger == edgetrigger)
777		return (0);
778	io->io_pins[pin].io_edgetrigger = edgetrigger;
779	if (bootverbose)
780		printf("ioapic%u: intpin %d trigger: %s\n", io->io_id, pin,
781		    trigger == INTR_TRIGGER_EDGE ? "edge" : "level");
782	return (0);
783}
784
785/*
786 * Register a complete I/O APIC object with the interrupt subsystem.
787 */
788void
789ioapic_register(void *cookie)
790{
791	struct ioapic_intsrc *pin;
792	struct ioapic *io;
793	volatile ioapic_t *apic;
794	uint32_t flags;
795	int i;
796
797	io = (struct ioapic *)cookie;
798	apic = io->io_addr;
799	mtx_lock_spin(&icu_lock);
800	flags = ioapic_read(apic, IOAPIC_VER) & IOART_VER_VERSION;
801	STAILQ_INSERT_TAIL(&ioapic_list, io, io_next);
802	mtx_unlock_spin(&icu_lock);
803	printf("ioapic%u <Version %u.%u> irqs %u-%u on motherboard\n",
804	    io->io_id, flags >> 4, flags & 0xf, io->io_intbase,
805	    io->io_intbase + io->io_numintr - 1);
806
807	/* Register valid pins as interrupt sources. */
808	intr_register_pic(&io->io_pic);
809	for (i = 0, pin = io->io_pins; i < io->io_numintr; i++, pin++)
810		if (pin->io_irq < NUM_IO_INTS)
811			intr_register_source(&pin->io_intsrc);
812}
813
814/* A simple new-bus driver to consume PCI I/O APIC devices. */
815static int
816ioapic_pci_probe(device_t dev)
817{
818
819	if (pci_get_class(dev) == PCIC_BASEPERIPH &&
820	    pci_get_subclass(dev) == PCIS_BASEPERIPH_PIC) {
821		switch (pci_get_progif(dev)) {
822		case PCIP_BASEPERIPH_PIC_IO_APIC:
823			device_set_desc(dev, "IO APIC");
824			break;
825		case PCIP_BASEPERIPH_PIC_IOX_APIC:
826			device_set_desc(dev, "IO(x) APIC");
827			break;
828		default:
829			return (ENXIO);
830		}
831		device_quiet(dev);
832		return (-10000);
833	}
834	return (ENXIO);
835}
836
837static int
838ioapic_pci_attach(device_t dev)
839{
840
841	return (0);
842}
843
844static device_method_t ioapic_pci_methods[] = {
845	/* Device interface */
846	DEVMETHOD(device_probe,		ioapic_pci_probe),
847	DEVMETHOD(device_attach,	ioapic_pci_attach),
848
849	{ 0, 0 }
850};
851
852DEFINE_CLASS_0(ioapic, ioapic_pci_driver, ioapic_pci_methods, 0);
853
854static devclass_t ioapic_devclass;
855DRIVER_MODULE(ioapic, pci, ioapic_pci_driver, ioapic_devclass, 0, 0);
856
857/*
858 * A new-bus driver to consume the memory resources associated with
859 * the APICs in the system.  On some systems ACPI or PnPBIOS system
860 * resource devices may already claim these resources.  To keep from
861 * breaking those devices, we attach ourself to the nexus device after
862 * legacy0 and acpi0 and ignore any allocation failures.
863 */
864static void
865apic_identify(driver_t *driver, device_t parent)
866{
867
868	/*
869	 * Add at order 12.  acpi0 is probed at order 10 and legacy0
870	 * is probed at order 11.
871	 */
872	if (lapic_paddr != 0)
873		BUS_ADD_CHILD(parent, 12, "apic", 0);
874}
875
876static int
877apic_probe(device_t dev)
878{
879
880	device_set_desc(dev, "APIC resources");
881	device_quiet(dev);
882	return (0);
883}
884
885static void
886apic_add_resource(device_t dev, int rid, vm_paddr_t base, size_t length)
887{
888	int error;
889
890#ifdef PAE
891	/*
892	 * Resources use long's to track resources, so we can't
893	 * include memory regions above 4GB.
894	 */
895	if (base >= ~0ul)
896		return;
897#endif
898	error = bus_set_resource(dev, SYS_RES_MEMORY, rid, base, length);
899	if (error)
900		panic("apic_add_resource: resource %d failed set with %d", rid,
901		    error);
902	bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 0);
903}
904
905static int
906apic_attach(device_t dev)
907{
908	struct ioapic *io;
909	int i;
910
911	/* Reserve the local APIC. */
912	apic_add_resource(dev, 0, lapic_paddr, sizeof(lapic_t));
913	i = 1;
914	STAILQ_FOREACH(io, &ioapic_list, io_next) {
915		apic_add_resource(dev, i, io->io_paddr, IOAPIC_MEM_REGION);
916		i++;
917	}
918	return (0);
919}
920
921static device_method_t apic_methods[] = {
922	/* Device interface */
923	DEVMETHOD(device_identify,	apic_identify),
924	DEVMETHOD(device_probe,		apic_probe),
925	DEVMETHOD(device_attach,	apic_attach),
926
927	{ 0, 0 }
928};
929
930DEFINE_CLASS_0(apic, apic_driver, apic_methods, 0);
931
932static devclass_t apic_devclass;
933DRIVER_MODULE(apic, nexus, apic_driver, apic_devclass, 0, 0);
934
935#include "opt_ddb.h"
936
937#ifdef DDB
938#include <ddb/ddb.h>
939
940static const char *
941ioapic_delivery_mode(uint32_t mode)
942{
943
944	switch (mode) {
945	case IOART_DELFIXED:
946		return ("fixed");
947	case IOART_DELLOPRI:
948		return ("lowestpri");
949	case IOART_DELSMI:
950		return ("SMI");
951	case IOART_DELRSV1:
952		return ("rsrvd1");
953	case IOART_DELNMI:
954		return ("NMI");
955	case IOART_DELINIT:
956		return ("INIT");
957	case IOART_DELRSV2:
958		return ("rsrvd2");
959	case IOART_DELEXINT:
960		return ("ExtINT");
961	default:
962		return ("");
963	}
964}
965
966static u_int
967db_ioapic_read(volatile ioapic_t *apic, int reg)
968{
969
970	apic->ioregsel = reg;
971	return (apic->iowin);
972}
973
974static void
975db_show_ioapic_one(volatile ioapic_t *io_addr)
976{
977	uint32_t r, lo, hi;
978	int mre, i;
979
980	r = db_ioapic_read(io_addr, IOAPIC_VER);
981	mre = (r & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT;
982	db_printf("Id 0x%08x Ver 0x%02x MRE %d\n",
983	    db_ioapic_read(io_addr, IOAPIC_ID), r & IOART_VER_VERSION, mre);
984	for (i = 0; i < mre; i++) {
985		lo = db_ioapic_read(io_addr, IOAPIC_REDTBL_LO(i));
986		hi = db_ioapic_read(io_addr, IOAPIC_REDTBL_HI(i));
987		db_printf("  pin %d Dest %s/%x %smasked Trig %s RemoteIRR %d "
988		    "Polarity %s Status %s DeliveryMode %s Vec %d\n", i,
989		    (lo & IOART_DESTMOD) == IOART_DESTLOG ? "log" : "phy",
990		    (hi & IOART_DEST) >> 24,
991		    (lo & IOART_INTMASK) == IOART_INTMSET ? "" : "not",
992		    (lo & IOART_TRGRMOD) == IOART_TRGRLVL ? "lvl" : "edge",
993		    (lo & IOART_REM_IRR) == IOART_REM_IRR ? 1 : 0,
994		    (lo & IOART_INTPOL) == IOART_INTALO ? "low" : "high",
995		    (lo & IOART_DELIVS) == IOART_DELIVS ? "pend" : "idle",
996		    ioapic_delivery_mode(lo & IOART_DELMOD),
997		    (lo & IOART_INTVEC));
998	  }
999}
1000
1001DB_SHOW_COMMAND(ioapic, db_show_ioapic)
1002{
1003	struct ioapic *ioapic;
1004	int idx, i;
1005
1006	if (!have_addr) {
1007		db_printf("usage: show ioapic index\n");
1008		return;
1009	}
1010
1011	idx = (int)addr;
1012	i = 0;
1013	STAILQ_FOREACH(ioapic, &ioapic_list, io_next) {
1014		if (idx == i) {
1015			db_show_ioapic_one(ioapic->io_addr);
1016			break;
1017		}
1018		i++;
1019	}
1020}
1021
1022DB_SHOW_ALL_COMMAND(ioapics, db_show_all_ioapics)
1023{
1024	struct ioapic *ioapic;
1025
1026	STAILQ_FOREACH(ioapic, &ioapic_list, io_next)
1027		db_show_ioapic_one(ioapic->io_addr);
1028}
1029#endif
1030