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: releng/5.2/sys/amd64/amd64/intr_machdep.c 122841 2003-11-17 06:10:15Z peter $
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 void
142 intr_execute_handlers(struct intsrc *isrc, struct intrframe *iframe)
143 {
144 struct thread *td;
145 struct ithd *it;
146 struct intrhand *ih;
147 int error, vector;
148
149 td = curthread;
150 td->td_intr_nesting_level++;
151
152 /*
153 * We count software interrupts when we process them. The
154 * code here follows previous practice, but there's an
155 * argument for counting hardware interrupts when they're
156 * processed too.
157 */
158 atomic_add_long(isrc->is_count, 1);
159 atomic_add_int(&cnt.v_intr, 1);
160
161 it = isrc->is_ithread;
162 if (it == NULL)
163 ih = NULL;
164 else
165 ih = TAILQ_FIRST(&it->it_handlers);
166
167 /*
168 * XXX: We assume that IRQ 0 is only used for the ISA timer
169 * device (clk).
170 */
171 vector = isrc->is_pic->pic_vector(isrc);
172 if (vector == 0)
173 clkintr_pending = 1;
174
175 critical_enter();
176 if (ih != NULL && ih->ih_flags & IH_FAST) {
177 /*
178 * Execute fast interrupt handlers directly.
179 * To support clock handlers, if a handler registers
180 * with a NULL argument, then we pass it a pointer to
181 * a trapframe as its argument.
182 */
183 TAILQ_FOREACH(ih, &it->it_handlers, ih_next) {
184 MPASS(ih->ih_flags & IH_FAST);
185 CTR3(KTR_INTR, "%s: executing handler %p(%p)",
186 __func__, ih->ih_handler,
187 ih->ih_argument == NULL ? iframe :
188 ih->ih_argument);
189 if (ih->ih_argument == NULL)
190 ih->ih_handler(iframe);
191 else
192 ih->ih_handler(ih->ih_argument);
193 }
194 isrc->is_pic->pic_eoi_source(isrc);
195 error = 0;
196 } else {
197 /*
198 * For stray and threaded interrupts, we mask and EOI the
199 * source.
200 */
201 isrc->is_pic->pic_disable_source(isrc);
202 isrc->is_pic->pic_eoi_source(isrc);
203 if (ih == NULL)
204 error = EINVAL;
205 else
206 error = ithread_schedule(it, !cold);
207 }
208 critical_exit();
209 if (error == EINVAL) {
210 atomic_add_long(isrc->is_straycount, 1);
211 if (*isrc->is_straycount < MAX_STRAY_LOG)
212 log(LOG_ERR, "stray irq%d\n", vector);
213 else if (*isrc->is_straycount == MAX_STRAY_LOG)
214 log(LOG_CRIT,
215 "too many stray irq %d's: not logging anymore\n",
216 vector);
217 }
218 td->td_intr_nesting_level--;
219 }
220
221 void
222 intr_resume(void)
223 {
224 struct intsrc **isrc;
225 int i;
226
227 mtx_lock_spin(&intr_table_lock);
228 for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
229 if (*isrc != NULL && (*isrc)->is_pic->pic_resume != NULL)
230 (*isrc)->is_pic->pic_resume(*isrc);
231 mtx_unlock_spin(&intr_table_lock);
232 }
233
234 void
235 intr_suspend(void)
236 {
237 struct intsrc **isrc;
238 int i;
239
240 mtx_lock_spin(&intr_table_lock);
241 for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
242 if (*isrc != NULL && (*isrc)->is_pic->pic_suspend != NULL)
243 (*isrc)->is_pic->pic_suspend(*isrc);
244 mtx_unlock_spin(&intr_table_lock);
245 }
246
247 static void
248 intrcnt_setname(const char *name, int index)
249 {
250
251 snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
252 MAXCOMLEN, name);
253 }
254
255 static void
256 intrcnt_updatename(struct intsrc *is)
257 {
258
259 intrcnt_setname(is->is_ithread->it_td->td_proc->p_comm, is->is_index);
260 }
261
262 static void
263 intrcnt_register(struct intsrc *is)
264 {
265 char straystr[MAXCOMLEN + 1];
266
267 /* mtx_assert(&intr_table_lock, MA_OWNED); */
268 KASSERT(is->is_ithread != NULL, ("%s: isrc with no ithread", __func__));
269 is->is_index = intrcnt_index;
270 intrcnt_index += 2;
271 snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
272 is->is_pic->pic_vector(is));
273 intrcnt_updatename(is);
274 is->is_count = &intrcnt[is->is_index];
275 intrcnt_setname(straystr, is->is_index + 1);
276 is->is_straycount = &intrcnt[is->is_index + 1];
277 }
278
279 static void
280 intr_init(void *dummy __unused)
281 {
282
283 intrcnt_setname("???", 0);
284 intrcnt_index = 1;
285 mtx_init(&intr_table_lock, "intr table", NULL, MTX_SPIN);
286 }
287 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL)
288
289 #ifdef DDB
290 /*
291 * Dump data about interrupt handlers
292 */
293 DB_SHOW_COMMAND(irqs, db_show_irqs)
294 {
295 struct intsrc **isrc;
296 int i, quit, verbose;
297
298 quit = 0;
299 if (strcmp(modif, "v") == 0)
300 verbose = 1;
301 else
302 verbose = 0;
303 isrc = interrupt_sources;
304 db_setup_paging(db_simple_pager, &quit, DB_LINES_PER_PAGE);
305 for (i = 0; i < NUM_IO_INTS && !quit; i++, isrc++)
306 if (*isrc != NULL)
307 db_dump_ithread((*isrc)->is_ithread, verbose);
308 }
309 #endif
Cache object: 5badcc627671a9176c6a378d1af2e745
|