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 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD$
30 */
31
32 /*
33 * Machine dependent interrupt code for i386. For the i386, we have to
34 * deal with different PICs. Thus, we use the passed in vector to lookup
35 * an interrupt source associated with that vector. The interrupt source
36 * describes which PIC the source belongs to and includes methods to handle
37 * that source.
38 */
39
40 #include "opt_ddb.h"
41
42 #include <sys/param.h>
43 #include <sys/bus.h>
44 #include <sys/interrupt.h>
45 #include <sys/lock.h>
46 #include <sys/ktr.h>
47 #include <sys/kernel.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.h>
50 #include <sys/syslog.h>
51 #include <sys/systm.h>
52 #include <machine/clock.h>
53 #include <machine/intr_machdep.h>
54 #ifdef DDB
55 #include <ddb/ddb.h>
56 #endif
57
58 #define MAX_STRAY_LOG 5
59
60 typedef void (*mask_fn)(uintptr_t vector);
61
62 static int intrcnt_index;
63 static struct intsrc *interrupt_sources[NUM_IO_INTS];
64 static struct mtx intr_table_lock;
65
66 static void intr_init(void *__dummy);
67 static void intrcnt_setname(const char *name, int index);
68 static void intrcnt_updatename(struct intsrc *is);
69 static void intrcnt_register(struct intsrc *is);
70
71 /*
72 * Register a new interrupt source with the global interrupt system.
73 * The global interrupts need to be disabled when this function is
74 * called.
75 */
76 int
77 intr_register_source(struct intsrc *isrc)
78 {
79 int error, vector;
80
81 vector = isrc->is_pic->pic_vector(isrc);
82 if (interrupt_sources[vector] != NULL)
83 return (EEXIST);
84 error = ithread_create(&isrc->is_ithread, (uintptr_t)isrc, 0,
85 (mask_fn)isrc->is_pic->pic_disable_source,
86 (mask_fn)isrc->is_pic->pic_enable_source, "irq%d:", vector);
87 if (error)
88 return (error);
89 mtx_lock_spin(&intr_table_lock);
90 if (interrupt_sources[vector] != NULL) {
91 mtx_unlock_spin(&intr_table_lock);
92 ithread_destroy(isrc->is_ithread);
93 return (EEXIST);
94 }
95 intrcnt_register(isrc);
96 interrupt_sources[vector] = isrc;
97 mtx_unlock_spin(&intr_table_lock);
98 return (0);
99 }
100
101 struct intsrc *
102 intr_lookup_source(int vector)
103 {
104
105 return (interrupt_sources[vector]);
106 }
107
108 int
109 intr_add_handler(const char *name, int vector, driver_intr_t handler,
110 void *arg, enum intr_type flags, void **cookiep)
111 {
112 struct intsrc *isrc;
113 int error;
114
115 isrc = intr_lookup_source(vector);
116 if (isrc == NULL)
117 return (EINVAL);
118 error = ithread_add_handler(isrc->is_ithread, name, handler, arg,
119 ithread_priority(flags), flags, cookiep);
120 if (error == 0) {
121 intrcnt_updatename(isrc);
122 isrc->is_pic->pic_enable_intr(isrc);
123 isrc->is_pic->pic_enable_source(isrc);
124 }
125 return (error);
126 }
127
128 int
129 intr_remove_handler(void *cookie)
130 {
131 int error;
132
133 error = ithread_remove_handler(cookie);
134 #ifdef XXX
135 if (error == 0)
136 intrcnt_updatename(/* XXX */);
137 #endif
138 return (error);
139 }
140
141 int
142 intr_config_intr(int vector, enum intr_trigger trig, enum intr_polarity pol)
143 {
144 struct intsrc *isrc;
145
146 isrc = intr_lookup_source(vector);
147 if (isrc == NULL)
148 return (EINVAL);
149 return (isrc->is_pic->pic_config_intr(isrc, trig, pol));
150 }
151
152 void
153 intr_execute_handlers(struct intsrc *isrc, struct intrframe *iframe)
154 {
155 struct thread *td;
156 struct ithd *it;
157 struct intrhand *ih;
158 int error, vector;
159
160 td = curthread;
161 td->td_intr_nesting_level++;
162
163 /*
164 * We count software interrupts when we process them. The
165 * code here follows previous practice, but there's an
166 * argument for counting hardware interrupts when they're
167 * processed too.
168 */
169 (*isrc->is_count)++;
170 cnt.v_intr++;
171
172 it = isrc->is_ithread;
173 if (it == NULL)
174 ih = NULL;
175 else
176 ih = TAILQ_FIRST(&it->it_handlers);
177
178 /*
179 * XXX: We assume that IRQ 0 is only used for the ISA timer
180 * device (clk).
181 */
182 vector = isrc->is_pic->pic_vector(isrc);
183 if (vector == 0)
184 clkintr_pending = 1;
185
186 if (ih != NULL && ih->ih_flags & IH_FAST) {
187 /*
188 * Execute fast interrupt handlers directly.
189 * To support clock handlers, if a handler registers
190 * with a NULL argument, then we pass it a pointer to
191 * a trapframe as its argument.
192 */
193 critical_enter();
194 TAILQ_FOREACH(ih, &it->it_handlers, ih_next) {
195 MPASS(ih->ih_flags & IH_FAST);
196 CTR3(KTR_INTR, "%s: executing handler %p(%p)",
197 __func__, ih->ih_handler,
198 ih->ih_argument == NULL ? iframe :
199 ih->ih_argument);
200 if (ih->ih_argument == NULL)
201 ih->ih_handler(iframe);
202 else
203 ih->ih_handler(ih->ih_argument);
204 }
205 isrc->is_pic->pic_eoi_source(isrc);
206 error = 0;
207 /* XXX */
208 td->td_pflags &= ~TDP_OWEPREEMPT;
209 critical_exit();
210 } else {
211 /*
212 * For stray and threaded interrupts, we mask and EOI the
213 * source.
214 */
215 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
216 if (ih == NULL)
217 error = EINVAL;
218 else
219 error = ithread_schedule(it);
220 }
221 if (error == EINVAL) {
222 (*isrc->is_straycount)++;
223 if (*isrc->is_straycount < MAX_STRAY_LOG)
224 log(LOG_ERR, "stray irq%d\n", vector);
225 else if (*isrc->is_straycount == MAX_STRAY_LOG)
226 log(LOG_CRIT,
227 "too many stray irq %d's: not logging anymore\n",
228 vector);
229 }
230 td->td_intr_nesting_level--;
231 }
232
233 void
234 intr_resume(void)
235 {
236 struct intsrc **isrc;
237 int i;
238
239 mtx_lock_spin(&intr_table_lock);
240 for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
241 if (*isrc != NULL && (*isrc)->is_pic->pic_resume != NULL)
242 (*isrc)->is_pic->pic_resume(*isrc);
243 mtx_unlock_spin(&intr_table_lock);
244 }
245
246 void
247 intr_suspend(void)
248 {
249 struct intsrc **isrc;
250 int i;
251
252 mtx_lock_spin(&intr_table_lock);
253 for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
254 if (*isrc != NULL && (*isrc)->is_pic->pic_suspend != NULL)
255 (*isrc)->is_pic->pic_suspend(*isrc);
256 mtx_unlock_spin(&intr_table_lock);
257 }
258
259 static void
260 intrcnt_setname(const char *name, int index)
261 {
262
263 snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
264 MAXCOMLEN, name);
265 }
266
267 static void
268 intrcnt_updatename(struct intsrc *is)
269 {
270
271 intrcnt_setname(is->is_ithread->it_td->td_proc->p_comm, is->is_index);
272 }
273
274 static void
275 intrcnt_register(struct intsrc *is)
276 {
277 char straystr[MAXCOMLEN + 1];
278
279 /* mtx_assert(&intr_table_lock, MA_OWNED); */
280 KASSERT(is->is_ithread != NULL, ("%s: isrc with no ithread", __func__));
281 is->is_index = intrcnt_index;
282 intrcnt_index += 2;
283 snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
284 is->is_pic->pic_vector(is));
285 intrcnt_updatename(is);
286 is->is_count = &intrcnt[is->is_index];
287 intrcnt_setname(straystr, is->is_index + 1);
288 is->is_straycount = &intrcnt[is->is_index + 1];
289 }
290
291 static void
292 intr_init(void *dummy __unused)
293 {
294
295 intrcnt_setname("???", 0);
296 intrcnt_index = 1;
297 mtx_init(&intr_table_lock, "intr table", NULL, MTX_SPIN);
298 }
299 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL)
300
301 #ifdef DDB
302 /*
303 * Dump data about interrupt handlers
304 */
305 DB_SHOW_COMMAND(irqs, db_show_irqs)
306 {
307 struct intsrc **isrc;
308 int i, quit, verbose;
309
310 quit = 0;
311 if (strcmp(modif, "v") == 0)
312 verbose = 1;
313 else
314 verbose = 0;
315 isrc = interrupt_sources;
316 db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
317 for (i = 0; i < NUM_IO_INTS && !quit; i++, isrc++)
318 if (*isrc != NULL)
319 db_dump_ithread((*isrc)->is_ithread, verbose);
320 }
321 #endif
Cache object: 0446402b25befca99c6888ef64bac52e
|