]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/intr_machdep.c
This commit was generated by cvs2svn to compensate for changes in r147894,
[FreeBSD/FreeBSD.git] / sys / i386 / i386 / intr_machdep.c
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         PCPU_LAZY_INC(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                 critical_exit();
208         } else {
209                 /*
210                  * For stray and threaded interrupts, we mask and EOI the
211                  * source.
212                  */
213                 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
214                 if (ih == NULL)
215                         error = EINVAL;
216                 else
217                         error = ithread_schedule(it);
218         }
219         if (error == EINVAL) {
220                 (*isrc->is_straycount)++;
221                 if (*isrc->is_straycount < MAX_STRAY_LOG)
222                         log(LOG_ERR, "stray irq%d\n", vector);
223                 else if (*isrc->is_straycount == MAX_STRAY_LOG)
224                         log(LOG_CRIT,
225                             "too many stray irq %d's: not logging anymore\n",
226                             vector);
227         }
228         td->td_intr_nesting_level--;
229 }
230
231 void
232 intr_resume(void)
233 {
234         struct intsrc **isrc;
235         int i;
236
237         mtx_lock_spin(&intr_table_lock);
238         for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
239                 if (*isrc != NULL && (*isrc)->is_pic->pic_resume != NULL)
240                         (*isrc)->is_pic->pic_resume(*isrc);
241         mtx_unlock_spin(&intr_table_lock);
242 }
243
244 void
245 intr_suspend(void)
246 {
247         struct intsrc **isrc;
248         int i;
249
250         mtx_lock_spin(&intr_table_lock);
251         for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
252                 if (*isrc != NULL && (*isrc)->is_pic->pic_suspend != NULL)
253                         (*isrc)->is_pic->pic_suspend(*isrc);
254         mtx_unlock_spin(&intr_table_lock);
255 }
256
257 static void
258 intrcnt_setname(const char *name, int index)
259 {
260
261         snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
262             MAXCOMLEN, name);
263 }
264
265 static void
266 intrcnt_updatename(struct intsrc *is)
267 {
268
269         intrcnt_setname(is->is_ithread->it_td->td_proc->p_comm, is->is_index);
270 }
271
272 static void
273 intrcnt_register(struct intsrc *is)
274 {
275         char straystr[MAXCOMLEN + 1];
276
277         /* mtx_assert(&intr_table_lock, MA_OWNED); */
278         KASSERT(is->is_ithread != NULL, ("%s: isrc with no ithread", __func__));
279         is->is_index = intrcnt_index;
280         intrcnt_index += 2;
281         snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
282             is->is_pic->pic_vector(is));
283         intrcnt_updatename(is);
284         is->is_count = &intrcnt[is->is_index];
285         intrcnt_setname(straystr, is->is_index + 1);
286         is->is_straycount = &intrcnt[is->is_index + 1];
287 }
288
289 void
290 intrcnt_add(const char *name, u_long **countp)
291 {
292
293         mtx_lock_spin(&intr_table_lock);
294         *countp = &intrcnt[intrcnt_index];
295         intrcnt_setname(name, intrcnt_index);
296         intrcnt_index++;
297         mtx_unlock_spin(&intr_table_lock);
298 }
299
300 static void
301 intr_init(void *dummy __unused)
302 {
303
304         intrcnt_setname("???", 0);
305         intrcnt_index = 1;
306         mtx_init(&intr_table_lock, "intr table", NULL, MTX_SPIN);
307 }
308 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL)
309
310 #ifdef DDB
311 /*
312  * Dump data about interrupt handlers
313  */
314 DB_SHOW_COMMAND(irqs, db_show_irqs)
315 {
316         struct intsrc **isrc;
317         int i, quit, verbose;
318
319         quit = 0;
320         if (strcmp(modif, "v") == 0)
321                 verbose = 1;
322         else
323                 verbose = 0;
324         isrc = interrupt_sources;
325         db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
326         for (i = 0; i < NUM_IO_INTS && !quit; i++, isrc++)
327                 if (*isrc != NULL)
328                         db_dump_ithread((*isrc)->is_ithread, verbose);
329 }
330 #endif