xref: /freebsd/sys/x86/include/apicvar.h (revision 04e832672159cae412e8984e0b0cabfa6e7428b7)
1e07ef9b0SJohn Baldwin /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3ebf5747bSPedro F. Giffuni  *
4e07ef9b0SJohn Baldwin  * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
5e07ef9b0SJohn Baldwin  *
6e07ef9b0SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
7e07ef9b0SJohn Baldwin  * modification, are permitted provided that the following conditions
8e07ef9b0SJohn Baldwin  * are met:
9e07ef9b0SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
10e07ef9b0SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
11e07ef9b0SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
12e07ef9b0SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
13e07ef9b0SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
14e07ef9b0SJohn Baldwin  *
15e07ef9b0SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16e07ef9b0SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17e07ef9b0SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18e07ef9b0SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19e07ef9b0SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20e07ef9b0SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21e07ef9b0SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22e07ef9b0SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23e07ef9b0SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24e07ef9b0SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25e07ef9b0SJohn Baldwin  * SUCH DAMAGE.
26e07ef9b0SJohn Baldwin  */
27e07ef9b0SJohn Baldwin 
28e07ef9b0SJohn Baldwin #ifndef _X86_APICVAR_H_
29e07ef9b0SJohn Baldwin #define _X86_APICVAR_H_
30e07ef9b0SJohn Baldwin 
31e07ef9b0SJohn Baldwin /*
32e07ef9b0SJohn Baldwin  * Local && I/O APIC variable definitions.
33e07ef9b0SJohn Baldwin  */
34e07ef9b0SJohn Baldwin 
35e07ef9b0SJohn Baldwin /*
36e07ef9b0SJohn Baldwin  * Layout of local APIC interrupt vectors:
37e07ef9b0SJohn Baldwin  *
38e07ef9b0SJohn Baldwin  *	0xff (255)  +-------------+
39e07ef9b0SJohn Baldwin  *                  |             | 15 (Spurious / IPIs / Local Interrupts)
40e07ef9b0SJohn Baldwin  *	0xf0 (240)  +-------------+
41e07ef9b0SJohn Baldwin  *                  |             | 14 (I/O Interrupts / Timer)
42e07ef9b0SJohn Baldwin  *	0xe0 (224)  +-------------+
43e07ef9b0SJohn Baldwin  *                  |             | 13 (I/O Interrupts)
44e07ef9b0SJohn Baldwin  *	0xd0 (208)  +-------------+
45e07ef9b0SJohn Baldwin  *                  |             | 12 (I/O Interrupts)
46e07ef9b0SJohn Baldwin  *	0xc0 (192)  +-------------+
47e07ef9b0SJohn Baldwin  *                  |             | 11 (I/O Interrupts)
48e07ef9b0SJohn Baldwin  *	0xb0 (176)  +-------------+
49e07ef9b0SJohn Baldwin  *                  |             | 10 (I/O Interrupts)
50e07ef9b0SJohn Baldwin  *	0xa0 (160)  +-------------+
51e07ef9b0SJohn Baldwin  *                  |             | 9 (I/O Interrupts)
52e07ef9b0SJohn Baldwin  *	0x90 (144)  +-------------+
53e07ef9b0SJohn Baldwin  *                  |             | 8 (I/O Interrupts / System Calls)
54e07ef9b0SJohn Baldwin  *	0x80 (128)  +-------------+
55e07ef9b0SJohn Baldwin  *                  |             | 7 (I/O Interrupts)
56e07ef9b0SJohn Baldwin  *	0x70 (112)  +-------------+
57e07ef9b0SJohn Baldwin  *                  |             | 6 (I/O Interrupts)
58e07ef9b0SJohn Baldwin  *	0x60 (96)   +-------------+
59e07ef9b0SJohn Baldwin  *                  |             | 5 (I/O Interrupts)
60e07ef9b0SJohn Baldwin  *	0x50 (80)   +-------------+
61e07ef9b0SJohn Baldwin  *                  |             | 4 (I/O Interrupts)
62e07ef9b0SJohn Baldwin  *	0x40 (64)   +-------------+
63e07ef9b0SJohn Baldwin  *                  |             | 3 (I/O Interrupts)
64e07ef9b0SJohn Baldwin  *	0x30 (48)   +-------------+
65e07ef9b0SJohn Baldwin  *                  |             | 2 (ATPIC Interrupts)
66e07ef9b0SJohn Baldwin  *	0x20 (32)   +-------------+
67e07ef9b0SJohn Baldwin  *                  |             | 1 (Exceptions, traps, faults, etc.)
68e07ef9b0SJohn Baldwin  *	0x10 (16)   +-------------+
69e07ef9b0SJohn Baldwin  *                  |             | 0 (Exceptions, traps, faults, etc.)
70e07ef9b0SJohn Baldwin  *	0x00 (0)    +-------------+
71e07ef9b0SJohn Baldwin  *
72e07ef9b0SJohn Baldwin  * Note: 0x80 needs to be handled specially and not allocated to an
73e07ef9b0SJohn Baldwin  * I/O device!
74e07ef9b0SJohn Baldwin  */
75e07ef9b0SJohn Baldwin 
76a74bb29aSRoger Pau Monné #define	xAPIC_MAX_APIC_ID	0xfe
77a74bb29aSRoger Pau Monné #define	xAPIC_ID_ALL		0xff
78c8113dadSEd Maste #define	MAX_APIC_ID		0x800
79a74bb29aSRoger Pau Monné #define	APIC_ID_ALL		0xffffffff
80a74bb29aSRoger Pau Monné 
814e328681SEd Maste /*
824e328681SEd Maste  * The 0xff ID is used for broadcast IPIs for local APICs when not using
834e328681SEd Maste  * x2APIC.  IPIs are not sent to I/O APICs so it's acceptable for an I/O APIC
844e328681SEd Maste  * to use that ID.
854e328681SEd Maste  */
864e328681SEd Maste #define	IOAPIC_MAX_ID		0xff
87e07ef9b0SJohn Baldwin 
88e07ef9b0SJohn Baldwin /* I/O Interrupts are used for external devices such as ISA, PCI, etc. */
89e07ef9b0SJohn Baldwin #define	APIC_IO_INTS	(IDT_IO_INTS + 16)
90e07ef9b0SJohn Baldwin #define	APIC_NUM_IOINTS	191
91e07ef9b0SJohn Baldwin 
92e07ef9b0SJohn Baldwin /* The timer interrupt is used for clock handling and drives hardclock, etc. */
93e07ef9b0SJohn Baldwin #define	APIC_TIMER_INT	(APIC_IO_INTS + APIC_NUM_IOINTS)
94e07ef9b0SJohn Baldwin 
95e07ef9b0SJohn Baldwin /*
96e07ef9b0SJohn Baldwin  ********************* !!! WARNING !!! ******************************
97e07ef9b0SJohn Baldwin  * Each local apic has an interrupt receive fifo that is two entries deep
98e07ef9b0SJohn Baldwin  * for each interrupt priority class (higher 4 bits of interrupt vector).
99e07ef9b0SJohn Baldwin  * Once the fifo is full the APIC can no longer receive interrupts for this
100e07ef9b0SJohn Baldwin  * class and sending IPIs from other CPUs will be blocked.
101e07ef9b0SJohn Baldwin  * To avoid deadlocks there should be no more than two IPI interrupts
102e07ef9b0SJohn Baldwin  * pending at the same time.
103e07ef9b0SJohn Baldwin  * Currently this is guaranteed by dividing the IPIs in two groups that have
104e07ef9b0SJohn Baldwin  * each at most one IPI interrupt pending. The first group is protected by the
105e07ef9b0SJohn Baldwin  * smp_ipi_mtx and waits for the completion of the IPI (Only one IPI user
106e07ef9b0SJohn Baldwin  * at a time) The second group uses a single interrupt and a bitmap to avoid
107e07ef9b0SJohn Baldwin  * redundant IPI interrupts.
108e07ef9b0SJohn Baldwin  */
109e07ef9b0SJohn Baldwin 
110e07ef9b0SJohn Baldwin /* Interrupts for local APIC LVT entries other than the timer. */
111e07ef9b0SJohn Baldwin #define	APIC_LOCAL_INTS	240
112e07ef9b0SJohn Baldwin #define	APIC_ERROR_INT	APIC_LOCAL_INTS
113e07ef9b0SJohn Baldwin #define	APIC_THERMAL_INT (APIC_LOCAL_INTS + 1)
114e07ef9b0SJohn Baldwin #define	APIC_CMC_INT	(APIC_LOCAL_INTS + 2)
115e07ef9b0SJohn Baldwin #define	APIC_IPI_INTS	(APIC_LOCAL_INTS + 3)
116e07ef9b0SJohn Baldwin 
117e07ef9b0SJohn Baldwin #define	IPI_RENDEZVOUS	(APIC_IPI_INTS)		/* Inter-CPU rendezvous. */
118dc43978aSKonstantin Belousov #define	IPI_INVLOP	(APIC_IPI_INTS + 1)	/* TLB Shootdown IPIs, amd64 */
119dc43978aSKonstantin Belousov #define	IPI_INVLTLB	(APIC_IPI_INTS + 1)	/* TLB Shootdown IPIs, i386 */
120e07ef9b0SJohn Baldwin #define	IPI_INVLPG	(APIC_IPI_INTS + 2)
121e07ef9b0SJohn Baldwin #define	IPI_INVLRNG	(APIC_IPI_INTS + 3)
122e07ef9b0SJohn Baldwin #define	IPI_INVLCACHE	(APIC_IPI_INTS + 4)
123e07ef9b0SJohn Baldwin /* Vector to handle bitmap based IPIs */
1248958d18cSNeel Natu #define	IPI_BITMAP_VECTOR	(APIC_IPI_INTS + 5)
125e07ef9b0SJohn Baldwin 
126e07ef9b0SJohn Baldwin /* IPIs handled by IPI_BITMAP_VECTOR */
127e07ef9b0SJohn Baldwin #define	IPI_AST		0 	/* Generate software trap. */
128e07ef9b0SJohn Baldwin #define IPI_PREEMPT     1
129e07ef9b0SJohn Baldwin #define IPI_HARDCLOCK   2
1301c29da02SMark Johnston #define	IPI_TRACE	3	/* Collect stack trace. */
1311c29da02SMark Johnston #define	IPI_BITMAP_LAST IPI_TRACE
132e07ef9b0SJohn Baldwin #define IPI_IS_BITMAPED(x) ((x) <= IPI_BITMAP_LAST)
133e07ef9b0SJohn Baldwin 
1348958d18cSNeel Natu #define	IPI_STOP	(APIC_IPI_INTS + 6)	/* Stop CPU until restarted. */
1358958d18cSNeel Natu #define	IPI_SUSPEND	(APIC_IPI_INTS + 7)	/* Suspend CPU until restarted. */
136aba10e13SAlexander Motin #define	IPI_SWI		(APIC_IPI_INTS + 8)	/* Run clk_intr_event. */
137aba10e13SAlexander Motin #define	IPI_DYN_FIRST	(APIC_IPI_INTS + 9)
1381c29da02SMark Johnston #define	IPI_DYN_LAST	(254)			/* IPIs allocated at runtime */
139847383d0SNeel Natu 
140847383d0SNeel Natu /*
141847383d0SNeel Natu  * IPI_STOP_HARD does not need to occupy a slot in the IPI vector space since
142847383d0SNeel Natu  * it is delivered using an NMI anyways.
143847383d0SNeel Natu  */
1441c29da02SMark Johnston #define	IPI_NMI_FIRST	255
145847383d0SNeel Natu #define	IPI_STOP_HARD	255			/* Stop CPU with a NMI. */
146e07ef9b0SJohn Baldwin 
147e07ef9b0SJohn Baldwin /*
148e07ef9b0SJohn Baldwin  * The spurious interrupt can share the priority class with the IPIs since
149e07ef9b0SJohn Baldwin  * it is not a normal interrupt. (Does not use the APIC's interrupt fifo)
150e07ef9b0SJohn Baldwin  */
151e07ef9b0SJohn Baldwin #define	APIC_SPURIOUS_INT 255
152e07ef9b0SJohn Baldwin 
153e07ef9b0SJohn Baldwin #ifndef LOCORE
154e07ef9b0SJohn Baldwin 
155e07ef9b0SJohn Baldwin #define	APIC_IPI_DEST_SELF	-1
156e07ef9b0SJohn Baldwin #define	APIC_IPI_DEST_ALL	-2
157e07ef9b0SJohn Baldwin #define	APIC_IPI_DEST_OTHERS	-3
158e07ef9b0SJohn Baldwin 
159e07ef9b0SJohn Baldwin #define	APIC_BUS_UNKNOWN	-1
160e07ef9b0SJohn Baldwin #define	APIC_BUS_ISA		0
161e07ef9b0SJohn Baldwin #define	APIC_BUS_EISA		1
162e07ef9b0SJohn Baldwin #define	APIC_BUS_PCI		2
163e07ef9b0SJohn Baldwin #define	APIC_BUS_MAX		APIC_BUS_PCI
164e07ef9b0SJohn Baldwin 
165fd036deaSJohn Baldwin #define	IRQ_EXTINT		-1
166fd036deaSJohn Baldwin #define	IRQ_NMI			-2
167fd036deaSJohn Baldwin #define	IRQ_SMI			-3
168fd036deaSJohn Baldwin #define	IRQ_DISABLED		-4
1690a110d5bSKonstantin Belousov 
170e07ef9b0SJohn Baldwin /*
171315fbaecSEd Maste  * An APIC enumerator is a pseudo bus driver that enumerates APIC's including
172e07ef9b0SJohn Baldwin  * CPU's and I/O APIC's.
173e07ef9b0SJohn Baldwin  */
174e07ef9b0SJohn Baldwin struct apic_enumerator {
175e07ef9b0SJohn Baldwin 	const char *apic_name;
176e07ef9b0SJohn Baldwin 	int (*apic_probe)(void);
177e07ef9b0SJohn Baldwin 	int (*apic_probe_cpus)(void);
178e07ef9b0SJohn Baldwin 	int (*apic_setup_local)(void);
179e07ef9b0SJohn Baldwin 	int (*apic_setup_io)(void);
180e07ef9b0SJohn Baldwin 	SLIST_ENTRY(apic_enumerator) apic_next;
181e07ef9b0SJohn Baldwin };
182e07ef9b0SJohn Baldwin 
183e07ef9b0SJohn Baldwin inthand_t
184e07ef9b0SJohn Baldwin 	IDTVEC(apic_isr1), IDTVEC(apic_isr2), IDTVEC(apic_isr3),
185e07ef9b0SJohn Baldwin 	IDTVEC(apic_isr4), IDTVEC(apic_isr5), IDTVEC(apic_isr6),
186e07ef9b0SJohn Baldwin 	IDTVEC(apic_isr7), IDTVEC(cmcint), IDTVEC(errorint),
187bd50262fSKonstantin Belousov 	IDTVEC(spuriousint), IDTVEC(timerint),
188bd50262fSKonstantin Belousov 	IDTVEC(apic_isr1_pti), IDTVEC(apic_isr2_pti), IDTVEC(apic_isr3_pti),
189bd50262fSKonstantin Belousov 	IDTVEC(apic_isr4_pti), IDTVEC(apic_isr5_pti), IDTVEC(apic_isr6_pti),
190bd50262fSKonstantin Belousov 	IDTVEC(apic_isr7_pti), IDTVEC(cmcint_pti), IDTVEC(errorint_pti),
191bd50262fSKonstantin Belousov 	IDTVEC(spuriousint_pti), IDTVEC(timerint_pti);
192e07ef9b0SJohn Baldwin 
193e07ef9b0SJohn Baldwin extern vm_paddr_t lapic_paddr;
19484525e55SRoger Pau Monné extern int *apic_cpuids;
195e07ef9b0SJohn Baldwin 
196e0516c75SRoger Pau Monné /* Allow to replace the lapic_ipi_vectored implementation. */
197e0516c75SRoger Pau Monné extern void (*ipi_vectored)(u_int, int);
198e0516c75SRoger Pau Monné 
19990fb07edSElliott Mitchell typedef struct ioapic *ioapic_drv_t;
20090fb07edSElliott Mitchell 
201e07ef9b0SJohn Baldwin void	apic_register_enumerator(struct apic_enumerator *enumerator);
20290fb07edSElliott Mitchell ioapic_drv_t	ioapic_create(vm_paddr_t addr, int32_t apic_id, int intbase);
20390fb07edSElliott Mitchell int	ioapic_disable_pin(ioapic_drv_t cookie, u_int pin);
20490fb07edSElliott Mitchell int	ioapic_get_vector(ioapic_drv_t cookie, u_int pin);
20590fb07edSElliott Mitchell void	ioapic_register(ioapic_drv_t cookie);
20690fb07edSElliott Mitchell int	ioapic_remap_vector(ioapic_drv_t cookie, u_int pin, int vector);
20790fb07edSElliott Mitchell int	ioapic_set_bus(ioapic_drv_t cookie, u_int pin, int bus_type);
20890fb07edSElliott Mitchell int	ioapic_set_extint(ioapic_drv_t cookie, u_int pin);
20990fb07edSElliott Mitchell int	ioapic_set_nmi(ioapic_drv_t cookie, u_int pin);
21090fb07edSElliott Mitchell int	ioapic_set_polarity(ioapic_drv_t cookie, u_int pin, enum intr_polarity pol);
21190fb07edSElliott Mitchell int	ioapic_set_triggermode(ioapic_drv_t cookie, u_int pin,
212e07ef9b0SJohn Baldwin 	    enum intr_trigger trigger);
21390fb07edSElliott Mitchell int	ioapic_set_smi(ioapic_drv_t cookie, u_int pin);
214ef409edeSRoger Pau Monné 
215e0516c75SRoger Pau Monné void	lapic_create(u_int apic_id, int boot_cpu);
216e0516c75SRoger Pau Monné void	lapic_init(vm_paddr_t addr);
217e0516c75SRoger Pau Monné void	lapic_xapic_mode(void);
218e0516c75SRoger Pau Monné bool	lapic_is_x2apic(void);
219e0516c75SRoger Pau Monné void	lapic_setup(int boot);
220e0516c75SRoger Pau Monné void	lapic_dump(const char *str);
221e0516c75SRoger Pau Monné void	lapic_disable(void);
222e0516c75SRoger Pau Monné void	lapic_eoi(void);
223e0516c75SRoger Pau Monné int	lapic_id(void);
224e0516c75SRoger Pau Monné int	lapic_intr_pending(u_int vector);
225ef409edeSRoger Pau Monné /* XXX: UNUSED */
226e0516c75SRoger Pau Monné void	lapic_set_logical_id(u_int apic_id, u_int cluster, u_int cluster_id);
227e0516c75SRoger Pau Monné u_int	apic_cpuid(u_int apic_id);
228e0516c75SRoger Pau Monné u_int	apic_alloc_vector(u_int apic_id, u_int irq);
229e0516c75SRoger Pau Monné u_int	apic_alloc_vectors(u_int apic_id, u_int *irqs, u_int count, u_int align);
230e0516c75SRoger Pau Monné void	apic_enable_vector(u_int apic_id, u_int vector);
231e0516c75SRoger Pau Monné void	apic_disable_vector(u_int apic_id, u_int vector);
232e0516c75SRoger Pau Monné void	apic_free_vector(u_int apic_id, u_int vector, u_int irq);
233e0516c75SRoger Pau Monné void	lapic_calibrate_timer(void);
234*04e83267SBojan Novković int	lapic_enable_pcint(void);
235*04e83267SBojan Novković void	lapic_disable_pcint(void);
236*04e83267SBojan Novković void	lapic_reenable_pcint(void);
237e0516c75SRoger Pau Monné void	lapic_enable_cmc(void);
238e0516c75SRoger Pau Monné int	lapic_enable_mca_elvt(void);
239e0516c75SRoger Pau Monné void	lapic_ipi_raw(register_t icrlo, u_int dest);
240ef409edeSRoger Pau Monné 
241ef409edeSRoger Pau Monné static inline void
lapic_ipi_vectored(u_int vector,int dest)242ef409edeSRoger Pau Monné lapic_ipi_vectored(u_int vector, int dest)
243ef409edeSRoger Pau Monné {
244ef409edeSRoger Pau Monné 
245e0516c75SRoger Pau Monné 	ipi_vectored(vector, dest);
246ef409edeSRoger Pau Monné }
247ef409edeSRoger Pau Monné 
248e0516c75SRoger Pau Monné int	lapic_ipi_wait(int delay);
249e0516c75SRoger Pau Monné int	lapic_ipi_alloc(inthand_t *ipifunc);
250e0516c75SRoger Pau Monné void	lapic_ipi_free(int vector);
251e0516c75SRoger Pau Monné int	lapic_set_lvt_mask(u_int apic_id, u_int lvt, u_char masked);
252e0516c75SRoger Pau Monné int	lapic_set_lvt_mode(u_int apic_id, u_int lvt, u_int32_t mode);
253e0516c75SRoger Pau Monné int	lapic_set_lvt_polarity(u_int apic_id, u_int lvt,
254e0516c75SRoger Pau Monné 	    enum intr_polarity pol);
255e0516c75SRoger Pau Monné int	lapic_set_lvt_triggermode(u_int apic_id, u_int lvt,
256e0516c75SRoger Pau Monné 	    enum intr_trigger trigger);
257e07ef9b0SJohn Baldwin void	lapic_handle_cmc(void);
258e07ef9b0SJohn Baldwin void	lapic_handle_error(void);
259e07ef9b0SJohn Baldwin void	lapic_handle_intr(int vector, struct trapframe *frame);
260e07ef9b0SJohn Baldwin void	lapic_handle_timer(struct trapframe *frame);
261e07ef9b0SJohn Baldwin 
2623fd0053aSKonstantin Belousov int	ioapic_get_rid(u_int apic_id, uint16_t *ridp);
263ea4e4449SKonstantin Belousov device_t ioapic_get_dev(u_int apic_id);
2643fd0053aSKonstantin Belousov 
2654c918926SKonstantin Belousov extern int x2apic_mode;
2662d4c4c8dSKonstantin Belousov extern int lapic_eoi_suppression;
2674c918926SKonstantin Belousov 
2684c918926SKonstantin Belousov #ifdef _SYS_SYSCTL_H_
2694c918926SKonstantin Belousov SYSCTL_DECL(_hw_apic);
2704c918926SKonstantin Belousov #endif
2714c918926SKonstantin Belousov 
272e07ef9b0SJohn Baldwin #endif /* !LOCORE */
273e07ef9b0SJohn Baldwin #endif /* _X86_APICVAR_H_ */
274