]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/intr_machdep.c
This commit was generated by cvs2svn to compensate for changes in r152058,
[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 intrframe *iframe)
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         }
194
195         /*
196          * Execute fast interrupt handlers directly.
197          * To support clock handlers, if a handler registers
198          * with a NULL argument, then we pass it a pointer to
199          * an intrframe as its argument.
200          */
201         td->td_intr_nesting_level++;
202         thread = 0;
203         critical_enter();
204         TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
205                 if (!(ih->ih_flags & IH_FAST)) {
206                         thread = 1;
207                         continue;
208                 }
209                 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
210                     ih->ih_handler, ih->ih_argument == NULL ? iframe :
211                     ih->ih_argument, ih->ih_name);
212                 if (ih->ih_argument == NULL)
213                         ih->ih_handler(iframe);
214                 else
215                         ih->ih_handler(ih->ih_argument);
216         }
217
218         /*
219          * If there are any threaded handlers that need to run,
220          * mask the source as well as sending it an EOI.  Otherwise,
221          * just send it an EOI but leave it unmasked.
222          */
223         if (thread)
224                 isrc->is_pic->pic_disable_source(isrc, PIC_EOI);
225         else
226                 isrc->is_pic->pic_eoi_source(isrc);
227         critical_exit();
228
229         /* Schedule the ithread if needed. */
230         if (thread) {
231                 error = intr_event_schedule_thread(ie);
232                 KASSERT(error == 0, ("bad stray interrupt"));
233         }
234         td->td_intr_nesting_level--;
235 }
236
237 void
238 intr_resume(void)
239 {
240         struct intsrc **isrc;
241         int i;
242
243         mtx_lock_spin(&intr_table_lock);
244         for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
245                 if (*isrc != NULL && (*isrc)->is_pic->pic_resume != NULL)
246                         (*isrc)->is_pic->pic_resume(*isrc);
247         mtx_unlock_spin(&intr_table_lock);
248 }
249
250 void
251 intr_suspend(void)
252 {
253         struct intsrc **isrc;
254         int i;
255
256         mtx_lock_spin(&intr_table_lock);
257         for (i = 0, isrc = interrupt_sources; i < NUM_IO_INTS; i++, isrc++)
258                 if (*isrc != NULL && (*isrc)->is_pic->pic_suspend != NULL)
259                         (*isrc)->is_pic->pic_suspend(*isrc);
260         mtx_unlock_spin(&intr_table_lock);
261 }
262
263 static void
264 intrcnt_setname(const char *name, int index)
265 {
266
267         snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
268             MAXCOMLEN, name);
269 }
270
271 static void
272 intrcnt_updatename(struct intsrc *is)
273 {
274
275         intrcnt_setname(is->is_event->ie_fullname, is->is_index);
276 }
277
278 static void
279 intrcnt_register(struct intsrc *is)
280 {
281         char straystr[MAXCOMLEN + 1];
282
283         /* mtx_assert(&intr_table_lock, MA_OWNED); */
284         KASSERT(is->is_event != NULL, ("%s: isrc with no event", __func__));
285         is->is_index = intrcnt_index;
286         intrcnt_index += 2;
287         snprintf(straystr, MAXCOMLEN + 1, "stray irq%d",
288             is->is_pic->pic_vector(is));
289         intrcnt_updatename(is);
290         is->is_count = &intrcnt[is->is_index];
291         intrcnt_setname(straystr, is->is_index + 1);
292         is->is_straycount = &intrcnt[is->is_index + 1];
293 }
294
295 void
296 intrcnt_add(const char *name, u_long **countp)
297 {
298
299         mtx_lock_spin(&intr_table_lock);
300         *countp = &intrcnt[intrcnt_index];
301         intrcnt_setname(name, intrcnt_index);
302         intrcnt_index++;
303         mtx_unlock_spin(&intr_table_lock);
304 }
305
306 static void
307 intr_init(void *dummy __unused)
308 {
309
310         intrcnt_setname("???", 0);
311         intrcnt_index = 1;
312         mtx_init(&intr_table_lock, "intr table", NULL, MTX_SPIN);
313 }
314 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL)
315
316 #ifdef DDB
317 /*
318  * Dump data about interrupt handlers
319  */
320 DB_SHOW_COMMAND(irqs, db_show_irqs)
321 {
322         struct intsrc **isrc;
323         int i, quit, verbose;
324
325         quit = 0;
326         if (strcmp(modif, "v") == 0)
327                 verbose = 1;
328         else
329                 verbose = 0;
330         isrc = interrupt_sources;
331         db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
332         for (i = 0; i < NUM_IO_INTS && !quit; i++, isrc++)
333                 if (*isrc != NULL)
334                         db_dump_intr_event((*isrc)->is_event, verbose);
335 }
336 #endif