intr.h revision 1.65 1 1.65 bouyer /* $NetBSD: intr.h,v 1.65 2022/05/24 15:55:19 bouyer Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*-
4 1.61 thorpej * Copyright (c) 1998, 2001, 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc.
5 1.1 fvdl * All rights reserved.
6 1.1 fvdl *
7 1.1 fvdl * This code is derived from software contributed to The NetBSD Foundation
8 1.1 fvdl * by Charles M. Hannum, and by Jason R. Thorpe.
9 1.1 fvdl *
10 1.1 fvdl * Redistribution and use in source and binary forms, with or without
11 1.1 fvdl * modification, are permitted provided that the following conditions
12 1.1 fvdl * are met:
13 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
14 1.1 fvdl * notice, this list of conditions and the following disclaimer.
15 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
17 1.1 fvdl * documentation and/or other materials provided with the distribution.
18 1.1 fvdl *
19 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 fvdl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 fvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 fvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 fvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 fvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 fvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 fvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 fvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 fvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 fvdl * POSSIBILITY OF SUCH DAMAGE.
30 1.1 fvdl */
31 1.1 fvdl
32 1.1 fvdl #ifndef _X86_INTR_H_
33 1.1 fvdl #define _X86_INTR_H_
34 1.1 fvdl
35 1.29 ad #define __HAVE_FAST_SOFTINTS
36 1.57 cherry #if !defined(NO_PREEMPTION)
37 1.33 ad #define __HAVE_PREEMPTION
38 1.57 cherry #endif /* !defined(NO_PREEMPTION) */
39 1.29 ad
40 1.38 dyoung #ifdef _KERNEL
41 1.38 dyoung #include <sys/types.h>
42 1.62 bouyer #include <sys/kcpuset.h>
43 1.38 dyoung #else
44 1.38 dyoung #include <stdbool.h>
45 1.38 dyoung #endif
46 1.38 dyoung
47 1.37 dyoung #include <sys/evcnt.h>
48 1.46 knakahar #include <sys/queue.h>
49 1.1 fvdl #include <machine/intrdefs.h>
50 1.1 fvdl
51 1.62 bouyer #ifdef XEN
52 1.62 bouyer #include <xen/include/public/xen.h>
53 1.62 bouyer #include <xen/include/public/event_channel.h>
54 1.62 bouyer #endif /* XEN */
55 1.62 bouyer
56 1.1 fvdl #ifndef _LOCORE
57 1.1 fvdl #include <machine/pic.h>
58 1.1 fvdl
59 1.1 fvdl /*
60 1.1 fvdl * Struct describing an interrupt source for a CPU. struct cpu_info
61 1.1 fvdl * has an array of MAX_INTR_SOURCES of these. The index in the array
62 1.1 fvdl * is equal to the stub number of the stubcode as present in vector.s
63 1.1 fvdl *
64 1.1 fvdl * The primary CPU's array of interrupt sources has its first 16
65 1.1 fvdl * entries reserved for legacy ISA irq handlers. This means that
66 1.1 fvdl * they have a 1:1 mapping for arrayindex:irq_num. This is not
67 1.1 fvdl * true for interrupts that come in through IO APICs, to find
68 1.1 fvdl * their source, go through ci->ci_isources[index].is_pic
69 1.1 fvdl *
70 1.1 fvdl * It's possible to always maintain a 1:1 mapping, but that means
71 1.1 fvdl * limiting the total number of interrupt sources to MAX_INTR_SOURCES
72 1.1 fvdl * (32), instead of 32 per CPU. It also would mean that having multiple
73 1.1 fvdl * IO APICs which deliver interrupts from an equal pin number would
74 1.1 fvdl * overlap if they were to be sent to the same CPU.
75 1.1 fvdl */
76 1.1 fvdl
77 1.1 fvdl struct intrstub {
78 1.1 fvdl void *ist_entry;
79 1.49 msaitoh void *ist_recurse;
80 1.1 fvdl void *ist_resume;
81 1.1 fvdl };
82 1.1 fvdl
83 1.46 knakahar struct percpu_evcnt {
84 1.46 knakahar cpuid_t cpuid;
85 1.46 knakahar uint64_t count;
86 1.46 knakahar };
87 1.46 knakahar
88 1.1 fvdl struct intrsource {
89 1.1 fvdl int is_maxlevel; /* max. IPL for this source */
90 1.43 drochner int is_pin; /* IRQ for legacy; pin for IO APIC,
91 1.43 drochner -1 for MSI */
92 1.1 fvdl struct intrhand *is_handlers; /* handler chain */
93 1.1 fvdl struct pic *is_pic; /* originating PIC */
94 1.1 fvdl void *is_recurse; /* entry for spllower */
95 1.1 fvdl void *is_resume; /* entry for doreti */
96 1.29 ad lwp_t *is_lwp; /* for soft interrupts */
97 1.51 cherry #if defined(XEN)
98 1.51 cherry u_long ipl_evt_mask1; /* pending events for this IPL */
99 1.51 cherry u_long ipl_evt_mask2[NR_EVENT_CHANNELS];
100 1.51 cherry #endif
101 1.48 knakahar struct evcnt is_evcnt; /* interrupt counter per cpu */
102 1.61 thorpej /*
103 1.64 andvar * is_mask_count requires special handling; it can only be modified
104 1.61 thorpej * or examined on the CPU that owns the interrupt source, and such
105 1.61 thorpej * references need to be protected by disabling interrupts. This
106 1.61 thorpej * is because intr_mask() can be called from an interrupt handler.
107 1.61 thorpej * is_distribute_pending does not require such special handling
108 1.61 thorpej * because intr_unmask() cannot be called from an interrupt handler.
109 1.61 thorpej */
110 1.61 thorpej u_int is_mask_count; /* masked? (nested) [see above] */
111 1.61 thorpej int is_distribute_pending; /* ci<->ci move pending [cpu_lock] */
112 1.1 fvdl int is_flags; /* see below */
113 1.1 fvdl int is_type; /* level, edge */
114 1.1 fvdl int is_idtvec;
115 1.1 fvdl int is_minlevel;
116 1.29 ad char is_evname[32]; /* event counter name */
117 1.46 knakahar char is_intrid[INTRIDBUF]; /* intrid created by create_intrid() */
118 1.48 knakahar char is_xname[INTRDEVNAMEBUF]; /* device names */
119 1.46 knakahar cpuid_t is_active_cpu; /* active cpuid */
120 1.46 knakahar struct percpu_evcnt *is_saved_evcnt; /* interrupt count of deactivated cpus */
121 1.46 knakahar SIMPLEQ_ENTRY(intrsource) is_list; /* link of intrsources */
122 1.1 fvdl };
123 1.1 fvdl
124 1.1 fvdl #define IS_LEGACY 0x0001 /* legacy ISA irq source */
125 1.1 fvdl #define IS_IPI 0x0002
126 1.1 fvdl #define IS_LOG 0x0004
127 1.1 fvdl
128 1.1 fvdl /*
129 1.1 fvdl * Interrupt handler chains. *_intr_establish() insert a handler into
130 1.1 fvdl * the list. The handler is called with its (single) argument.
131 1.1 fvdl */
132 1.1 fvdl
133 1.1 fvdl struct intrhand {
134 1.62 bouyer struct pic *ih_pic;
135 1.1 fvdl int (*ih_fun)(void *);
136 1.1 fvdl void *ih_arg;
137 1.1 fvdl int ih_level;
138 1.14 yamt int (*ih_realfun)(void *);
139 1.14 yamt void *ih_realarg;
140 1.1 fvdl struct intrhand *ih_next;
141 1.39 ad struct intrhand **ih_prevp;
142 1.1 fvdl int ih_pin;
143 1.1 fvdl int ih_slot;
144 1.52 cherry #if defined(XEN)
145 1.65 bouyer int ih_pending;
146 1.51 cherry struct intrhand *ih_evt_next;
147 1.51 cherry #endif
148 1.1 fvdl struct cpu_info *ih_cpu;
149 1.63 riastrad char ih_xname[INTRDEVNAMEBUF];
150 1.1 fvdl };
151 1.1 fvdl
152 1.1 fvdl #define IMASK(ci,level) (ci)->ci_imask[(level)]
153 1.1 fvdl #define IUNMASK(ci,level) (ci)->ci_iunmask[(level)]
154 1.1 fvdl
155 1.41 plunky #ifdef _KERNEL
156 1.41 plunky
157 1.26 yamt void Xspllower(int);
158 1.26 yamt void spllower(int);
159 1.26 yamt int splraise(int);
160 1.26 yamt void softintr(int);
161 1.1 fvdl
162 1.1 fvdl /*
163 1.1 fvdl * Convert spl level to local APIC level
164 1.1 fvdl */
165 1.26 yamt
166 1.1 fvdl #define APIC_LEVEL(l) ((l) << 4)
167 1.1 fvdl
168 1.1 fvdl /*
169 1.26 yamt * Miscellaneous
170 1.1 fvdl */
171 1.1 fvdl
172 1.1 fvdl #define SPL_ASSERT_BELOW(x) KDASSERT(curcpu()->ci_ilevel < (x))
173 1.1 fvdl #define spl0() spllower(IPL_NONE)
174 1.1 fvdl #define splx(x) spllower(x)
175 1.1 fvdl
176 1.23 ad typedef uint8_t ipl_t;
177 1.22 yamt typedef struct {
178 1.22 yamt ipl_t _ipl;
179 1.22 yamt } ipl_cookie_t;
180 1.22 yamt
181 1.22 yamt static inline ipl_cookie_t
182 1.22 yamt makeiplcookie(ipl_t ipl)
183 1.22 yamt {
184 1.22 yamt
185 1.22 yamt return (ipl_cookie_t){._ipl = ipl};
186 1.22 yamt }
187 1.22 yamt
188 1.22 yamt static inline int
189 1.22 yamt splraiseipl(ipl_cookie_t icookie)
190 1.22 yamt {
191 1.22 yamt
192 1.22 yamt return splraise(icookie._ipl);
193 1.22 yamt }
194 1.22 yamt
195 1.18 yamt #include <sys/spl.h>
196 1.18 yamt
197 1.1 fvdl /*
198 1.1 fvdl * Stub declarations.
199 1.1 fvdl */
200 1.1 fvdl
201 1.29 ad void Xsoftintr(void);
202 1.55 christos void Xrecurse_preempt(void);
203 1.55 christos void Xresume_preempt(void);
204 1.1 fvdl
205 1.54 maxv extern struct intrstub legacy_stubs[];
206 1.2 fvdl extern struct intrstub ioapic_edge_stubs[];
207 1.2 fvdl extern struct intrstub ioapic_level_stubs[];
208 1.50 nonaka extern struct intrstub x2apic_edge_stubs[];
209 1.50 nonaka extern struct intrstub x2apic_level_stubs[];
210 1.1 fvdl
211 1.1 fvdl struct cpu_info;
212 1.1 fvdl
213 1.10 fvdl struct pcibus_attach_args;
214 1.10 fvdl
215 1.47 knakahar typedef uint64_t intr_handle_t;
216 1.47 knakahar
217 1.1 fvdl void intr_default_setup(void);
218 1.42 dyoung void x86_nmi(void);
219 1.48 knakahar void *intr_establish_xname(int, struct pic *, int, int, int, int (*)(void *),
220 1.48 knakahar void *, bool, const char *);
221 1.35 ad void *intr_establish(int, struct pic *, int, int, int, int (*)(void *), void *, bool);
222 1.61 thorpej void intr_mask(struct intrhand *);
223 1.61 thorpej void intr_unmask(struct intrhand *);
224 1.1 fvdl void intr_disestablish(struct intrhand *);
225 1.10 fvdl void intr_add_pcibus(struct pcibus_attach_args *);
226 1.47 knakahar const char *intr_string(intr_handle_t, char *, size_t);
227 1.1 fvdl void cpu_intr_init(struct cpu_info *);
228 1.47 knakahar int intr_find_mpmapping(int, int, intr_handle_t *);
229 1.21 christos struct pic *intr_findpic(int);
230 1.1 fvdl void intr_printconfig(void);
231 1.1 fvdl
232 1.53 knakahar const char *intr_create_intrid(int, struct pic *, int, char *, size_t);
233 1.46 knakahar struct intrsource *intr_allocate_io_intrsource(const char *);
234 1.46 knakahar void intr_free_io_intrsource(const char *);
235 1.46 knakahar
236 1.62 bouyer void x86_init_preempt(struct cpu_info *);
237 1.62 bouyer void x86_intr_calculatemasks(struct cpu_info *);
238 1.62 bouyer
239 1.1 fvdl int x86_send_ipi(struct cpu_info *, int);
240 1.1 fvdl void x86_broadcast_ipi(int);
241 1.1 fvdl void x86_ipi_handler(void);
242 1.1 fvdl
243 1.62 bouyer void x86_intr_get_devname(const char *, char *, size_t);
244 1.62 bouyer void x86_intr_get_assigned(const char *, kcpuset_t *);
245 1.62 bouyer uint64_t x86_intr_get_count(const char *, u_int);
246 1.62 bouyer
247 1.58 cherry #ifndef XENPV
248 1.45 uebayasi extern void (* const ipifunc[X86_NIPI])(struct cpu_info *);
249 1.51 cherry #endif
250 1.1 fvdl
251 1.41 plunky #endif /* _KERNEL */
252 1.41 plunky
253 1.1 fvdl #endif /* !_LOCORE */
254 1.1 fvdl
255 1.1 fvdl #endif /* !_X86_INTR_H_ */
256