]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/intr_machdep.c
This commit was generated by cvs2svn to compensate for changes in r156066,
[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)(void *);
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 = intr_event_create(&isrc->is_event, isrc, 0,
85             (mask_fn)isrc->is_pic->pic_enable_source, "irq%d:", vector);
86         if (error)
87                 return (error);
88         mtx_lock_spin(&intr_table_lock);
89         if (interrupt_sources[vector] != NULL) {
90                 mtx_unlock_spin(&intr_table_lock);
91                 intr_event_destroy(isrc->is_event);
92                 return (EEXIST);
93         }
94         intrcnt_register(isrc);
95         interrupt_sources[vector] = isrc;
96         mtx_unlock_spin(&intr_table_lock);
97         return (0);
98 }
99
100 struct intsrc *
101 intr_lookup_source(int vector)
102 {
103
104         return (interrupt_sources[vector]);
105 }
106
107 int
108 intr_add_handler(const char *name, int vector, driver_intr_t handler,
109     void *arg, enum intr_type flags, void **cookiep)
110 {
111         struct intsrc *isrc;
112         int error;
113
114         isrc = intr_lookup_source(vector);
115         if (isrc == NULL)
116                 return (EINVAL);
117         error = intr_event_add_handler(isrc->is_event, name, handler, arg,
118             intr_priority(flags), flags, cookiep);
119         if (error == 0) {
120                 intrcnt_updatename(isrc);
121                 isrc->is_pic->pic_enable_intr(isrc);
122                 isrc->is_pic->pic_enable_source(isrc);
123         }
124         return (error);
125 }
126
127 int
128 intr_remove_handler(void *cookie)
129 {
130         int error;
131
132         error = intr_event_remove_handler(cookie);
133 #ifdef XXX
134         if (error == 0)
135                 intrcnt_updatename(/* XXX */);
136 #endif
137         return (error);
138 }
139
140 int
141 intr_config_intr(int vector, enum intr_trigger trig, enum intr_polarity pol)
142 {
143         struct intsrc *isrc;
144
145         isrc = intr_lookup_source(vector);
146         if (isrc == NULL)
147                 return (EINVAL);
148         return (isrc->is_pic->pic_config_intr(isrc, trig, pol));
149 }
150
151 void
152 intr_execute_handlers(struct intsrc *isrc, struct trapframe *frame)
153 {
154         struct thread *td;
155         struct intr_event *ie;
156         struct intr_handler *ih;
157         int error, vector, thread;
158
159         td = curthread;
160
161         /*
162          * We count software interrupts when we process them.  The
163          * code here follows previous practice, but there's an
164          * argument for counting hardware interrupts when they're
165          * processed too.
166          */
167         (*isrc->is_count)++;
168         PCPU_LAZY_INC(cnt.v_intr);
169
170         ie = isrc->is_event;
171
172         /*
173          * XXX: We assume that IRQ 0 is only used for the ISA timer
174          * device (clk).
175          */
176         vector = isrc->is_pic->pic_vector(isrc);
177         if (vector == 0)
178                 clkintr_pending = 1;
179
180         /*
181          * For stray interrupts, mask and EOI the source, bump the
182          * stray count, and log the condition.
183          */
184         if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) {
185                 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
186                 (*isrc->is_straycount)++;
187                 if (*isrc->is_straycount < MAX_STRAY_LOG)
188                         log(LOG_ERR, "stray irq%d\n", vector);
189                 else if (*isrc->is_straycount == MAX_STRAY_LOG)
190                         log(LOG_CRIT,
191                             "too many stray irq %d's: not logging anymore\n",
192                             vector);
193                 return;
194         }
195
196         /*
197          * Execute fast interrupt handlers directly.
198          * To support clock handlers, if a handler registers
199          * with a NULL argument, then we pass it a pointer to
200          * a trapframe as its argument.
201          */
202         td->td_intr_nesting_level++;
203         thread = 0;
204         critical_enter();
205         TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
206                 if (!(ih->ih_flags & IH_FAST)) {
207                         thread = 1;
208                         continue;
209                 }
210                 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
211                     ih->ih_handler, ih->ih_argument == NULL ? frame :
212                     ih->ih_argument, ih->ih_name);
213                 if (ih->ih_argument == NULL)
214                         ih->ih_handler(frame);
215                 else
216                         ih->ih_handler(ih->ih_argument);
217         }
218
219         /*
220          * If there are any threaded handlers that need to run,
221          * mask the source as well as sending it an EOI.  Otherwise,
222          * just send it an EOI but leave it unmasked.
223          */
224         if (thread)
225                 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
226         else
227                 isrc->is_pic->pic_eoi_source(isrc);
228         critical_exit();
229
230         /* Schedule the ithread if needed. */
231         if (thread) {
232                 error = intr_event_schedule_thread(ie);
233                 KASSERT(error == 0, ("bad stray interrupt"));
234         }
235         td->td_intr_nesting_level--;
236 }
237
238 void
239 intr_resume(void)
240 {
241         struct intsrc **isrc;
242         int i;
243
244         mtx_lock_spin(&intr_table_lock);
245         for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
246                 if (*isrc != NULL && (*isrc)->is_pic->pic_resume != NULL)
247                         (*isrc)->is_pic->pic_resume(*isrc);
248         mtx_unlock_spin(&intr_table_lock);
249 }
250
251 void
252 intr_suspend(void)
253 {
254         struct intsrc **isrc;
255         int i;
256
257         mtx_lock_spin(&intr_table_lock);
258         for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
259                 if (*isrc != NULL && (*isrc)->is_pic->pic_suspend != NULL)
260                         (*isrc)->is_pic->pic_suspend(*isrc);
261         mtx_unlock_spin(&intr_table_lock);
262 }
263
264 static void
265 intrcnt_setname(const char *name, int index)
266 {
267
268         snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
269             MAXCOMLEN, name);
270 }
271
272 static void
273 intrcnt_updatename(struct intsrc *is)
274 {
275
276         intrcnt_setname(is->is_event->ie_fullname, is->is_index);
277 }
278
279 static void
280 intrcnt_register(struct intsrc *is)
281 {
282         char straystr[MAXCOMLEN + 1];
283
284         /* mtx_assert(&intr_table_lock, MA_OWNED); */
285         KASSERT(is->is_event != NULL, ("%s: isrc with no event", __func__));
286         is->is_index = intrcnt_index;
287         intrcnt_index += 2;
288         snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
289             is->is_pic->pic_vector(is));
290         intrcnt_updatename(is);
291         is->is_count = &intrcnt[is->is_index];
292         intrcnt_setname(straystr, is->is_index + 1);
293         is->is_straycount = &intrcnt[is->is_index + 1];
294 }
295
296 void
297 intrcnt_add(const char *name, u_long **countp)
298 {
299
300         mtx_lock_spin(&intr_table_lock);
301         *countp = &intrcnt[intrcnt_index];
302         intrcnt_setname(name, intrcnt_index);
303         intrcnt_index++;
304         mtx_unlock_spin(&intr_table_lock);
305 }
306
307 static void
308 intr_init(void *dummy __unused)
309 {
310
311         intrcnt_setname("???", 0);
312         intrcnt_index = 1;
313         mtx_init(&intr_table_lock, "intr table", NULL, MTX_SPIN);
314 }
315 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL)
316
317 #ifdef DDB
318 /*
319  * Dump data about interrupt handlers
320  */
321 DB_SHOW_COMMAND(irqs, db_show_irqs)
322 {
323         struct intsrc **isrc;
324         int i, quit, verbose;
325
326         quit = 0;
327         if (strcmp(modif, "v") == 0)
328                 verbose = 1;
329         else
330                 verbose = 0;
331         isrc = interrupt_sources;
332         db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
333         for (i = 0; i < NUM_IO_INTS && !quit; i++, isrc++)
334                 if (*isrc != NULL)
335                         db_dump_intr_event((*isrc)->is_event, verbose);
336 }
337 #endif