Fix a type-o that was causing the wrong mbuf's csum_data to be adjusted
[dragonfly.git] / sys / kern / kern_intr.c
CommitLineData
984263bc 1/*
033a4603 2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com> All rights reserved.
ef0fdad1 3 * Copyright (c) 1997, Stefan Esser <se@freebsd.org> All rights reserved.
984263bc
MD
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 unmodified, this list of conditions, and the following
10 * disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sys/kern/kern_intr.c,v 1.24.2.1 2001/10/14 20:05:50 luigi Exp $
59d9413f 27 * $DragonFly: src/sys/kern/kern_intr.c,v 1.39 2005/12/23 22:17:39 swildner Exp $
984263bc
MD
28 *
29 */
30
984263bc
MD
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/malloc.h>
34#include <sys/kernel.h>
35#include <sys/sysctl.h>
ef0fdad1
MD
36#include <sys/thread.h>
37#include <sys/proc.h>
38#include <sys/thread2.h>
7e071e7a 39#include <sys/random.h>
477d3c1c
MD
40#include <sys/serialize.h>
41#include <sys/bus.h>
37e7efec 42#include <sys/machintr.h>
984263bc
MD
43
44#include <machine/ipl.h>
477d3c1c 45#include <machine/frame.h>
984263bc
MD
46
47#include <sys/interrupt.h>
48
9d522d14
MD
49struct info_info;
50
ef0fdad1
MD
51typedef struct intrec {
52 struct intrec *next;
9d522d14 53 struct intr_info *info;
ef0fdad1
MD
54 inthand2_t *handler;
55 void *argument;
477d3c1c 56 char *name;
ef0fdad1 57 int intr;
477d3c1c
MD
58 int intr_flags;
59 struct lwkt_serialize *serializer;
60} *intrec_t;
61
62struct intr_info {
63 intrec_t i_reclist;
64 struct thread i_thread;
65 struct random_softc i_random;
66 int i_running;
862f2618
MD
67 long i_count; /* interrupts dispatched */
68 int i_mplock_required;
477d3c1c
MD
69 int i_fast;
70 int i_slow;
f33e9c1c 71 int i_state;
5f456c40
MD
72} intr_info_ary[MAX_INTS];
73
74int max_installed_hard_intr;
75int max_installed_soft_intr;
477d3c1c 76
a9d00ec1
MD
77#define EMERGENCY_INTR_POLLING_FREQ_MAX 20000
78
79static int sysctl_emergency_freq(SYSCTL_HANDLER_ARGS);
80static int sysctl_emergency_enable(SYSCTL_HANDLER_ARGS);
81static void emergency_intr_timer_callback(systimer_t, struct intrframe *);
82static void ithread_handler(void *arg);
83static void ithread_emergency(void *arg);
84
477d3c1c 85int intr_info_size = sizeof(intr_info_ary) / sizeof(intr_info_ary[0]);
37d44089 86
a9d00ec1
MD
87static struct systimer emergency_intr_timer;
88static struct thread emergency_intr_thread;
89
f33e9c1c
MD
90#define ISTATE_NOTHREAD 0
91#define ISTATE_NORMAL 1
92#define ISTATE_LIVELOCKED 2
37d44089 93
0e6beaa3 94#ifdef SMP
862f2618 95static int intr_mpsafe = 0;
862f2618
MD
96TUNABLE_INT("kern.intr_mpsafe", &intr_mpsafe);
97SYSCTL_INT(_kern, OID_AUTO, intr_mpsafe,
98 CTLFLAG_RW, &intr_mpsafe, 0, "Run INTR_MPSAFE handlers without the BGL");
0e6beaa3
MD
99#endif
100static int livelock_limit = 50000;
101static int livelock_lowater = 20000;
37d44089
MD
102SYSCTL_INT(_kern, OID_AUTO, livelock_limit,
103 CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit");
f33e9c1c
MD
104SYSCTL_INT(_kern, OID_AUTO, livelock_lowater,
105 CTLFLAG_RW, &livelock_lowater, 0, "Livelock low-water mark restore");
984263bc 106
a9d00ec1
MD
107static int emergency_intr_enable = 0; /* emergency interrupt polling */
108TUNABLE_INT("kern.emergency_intr_enable", &emergency_intr_enable);
109SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_enable, CTLTYPE_INT | CTLFLAG_RW,
110 0, 0, sysctl_emergency_enable, "I", "Emergency Interrupt Poll Enable");
111
112static int emergency_intr_freq = 10; /* emergency polling frequency */
113TUNABLE_INT("kern.emergency_intr_freq", &emergency_intr_freq);
114SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_freq, CTLTYPE_INT | CTLFLAG_RW,
115 0, 0, sysctl_emergency_freq, "I", "Emergency Interrupt Poll Frequency");
116
117/*
118 * Sysctl support routines
119 */
120static int
121sysctl_emergency_enable(SYSCTL_HANDLER_ARGS)
122{
123 int error, enabled;
124
125 enabled = emergency_intr_enable;
126 error = sysctl_handle_int(oidp, &enabled, 0, req);
127 if (error || req->newptr == NULL)
128 return error;
129 emergency_intr_enable = enabled;
130 if (emergency_intr_enable) {
131 emergency_intr_timer.periodic =
132 sys_cputimer->fromhz(emergency_intr_freq);
133 } else {
134 emergency_intr_timer.periodic = sys_cputimer->fromhz(1);
135 }
136 return 0;
137}
138
139static int
140sysctl_emergency_freq(SYSCTL_HANDLER_ARGS)
141{
142 int error, phz;
143
144 phz = emergency_intr_freq;
145 error = sysctl_handle_int(oidp, &phz, 0, req);
146 if (error || req->newptr == NULL)
147 return error;
148 if (phz <= 0)
149 return EINVAL;
150 else if (phz > EMERGENCY_INTR_POLLING_FREQ_MAX)
151 phz = EMERGENCY_INTR_POLLING_FREQ_MAX;
152
153 emergency_intr_freq = phz;
154 if (emergency_intr_enable) {
155 emergency_intr_timer.periodic =
156 sys_cputimer->fromhz(emergency_intr_freq);
157 } else {
158 emergency_intr_timer.periodic = sys_cputimer->fromhz(1);
159 }
160 return 0;
161}
984263bc 162
45d76888
MD
163/*
164 * Register an SWI or INTerrupt handler.
45d76888 165 */
477d3c1c
MD
166void *
167register_swi(int intr, inthand2_t *handler, void *arg, const char *name,
168 struct lwkt_serialize *serializer)
984263bc 169{
5f456c40 170 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
ef0fdad1 171 panic("register_swi: bad intr %d", intr);
477d3c1c 172 return(register_int(intr, handler, arg, name, serializer, 0));
984263bc
MD
173}
174
477d3c1c
MD
175void *
176register_int(int intr, inthand2_t *handler, void *arg, const char *name,
177 struct lwkt_serialize *serializer, int intr_flags)
984263bc 178{
477d3c1c
MD
179 struct intr_info *info;
180 struct intrec **list;
181 intrec_t rec;
ef0fdad1 182
5f456c40 183 if (intr < 0 || intr >= MAX_INTS)
ef0fdad1 184 panic("register_int: bad intr %d", intr);
477d3c1c
MD
185 if (name == NULL)
186 name = "???";
187 info = &intr_info_ary[intr];
188
9d522d14
MD
189 /*
190 * Construct an interrupt handler record
191 */
477d3c1c
MD
192 rec = malloc(sizeof(struct intrec), M_DEVBUF, M_INTWAIT);
193 rec->name = malloc(strlen(name) + 1, M_DEVBUF, M_INTWAIT);
194 strcpy(rec->name, name);
ef0fdad1 195
9d522d14 196 rec->info = info;
ef0fdad1
MD
197 rec->handler = handler;
198 rec->argument = arg;
ef0fdad1 199 rec->intr = intr;
477d3c1c 200 rec->intr_flags = intr_flags;
ef0fdad1 201 rec->next = NULL;
477d3c1c 202 rec->serializer = serializer;
ef0fdad1 203
a9d00ec1
MD
204 /*
205 * Create an emergency polling thread and set up a systimer to wake
206 * it up.
207 */
208 if (emergency_intr_thread.td_kstack == NULL) {
209 lwkt_create(ithread_emergency, NULL, NULL,
210 &emergency_intr_thread, TDF_STOPREQ|TDF_INTTHREAD, -1,
211 "ithread emerg");
212 systimer_init_periodic_nq(&emergency_intr_timer,
213 emergency_intr_timer_callback, &emergency_intr_thread,
214 (emergency_intr_enable ? emergency_intr_freq : 1));
215 }
216
ef0fdad1
MD
217 /*
218 * Create an interrupt thread if necessary, leave it in an unscheduled
45d76888 219 * state.
ef0fdad1 220 */
f33e9c1c
MD
221 if (info->i_state == ISTATE_NOTHREAD) {
222 info->i_state = ISTATE_NORMAL;
477d3c1c 223 lwkt_create((void *)ithread_handler, (void *)intr, NULL,
862f2618 224 &info->i_thread, TDF_STOPREQ|TDF_INTTHREAD|TDF_MPSAFE, -1,
75cdbe6c 225 "ithread %d", intr);
5f456c40 226 if (intr >= FIRST_SOFTINT)
477d3c1c 227 lwkt_setpri(&info->i_thread, TDPRI_SOFT_NORM);
4b5f931b 228 else
477d3c1c
MD
229 lwkt_setpri(&info->i_thread, TDPRI_INT_MED);
230 info->i_thread.td_preemptable = lwkt_preempt;
ef0fdad1
MD
231 }
232
9d522d14
MD
233 list = &info->i_reclist;
234
ef0fdad1 235 /*
9d522d14 236 * Keep track of how many fast and slow interrupts we have.
862f2618
MD
237 * Set i_mplock_required if any handler in the chain requires
238 * the MP lock to operate.
ef0fdad1 239 */
862f2618
MD
240 if ((intr_flags & INTR_MPSAFE) == 0)
241 info->i_mplock_required = 1;
9d522d14
MD
242 if (intr_flags & INTR_FAST)
243 ++info->i_fast;
244 else
245 ++info->i_slow;
246
247 /*
248 * Add the record to the interrupt list.
249 */
250 crit_enter();
ef0fdad1
MD
251 while (*list != NULL)
252 list = &(*list)->next;
253 *list = rec;
254 crit_exit();
5f456c40
MD
255
256 /*
257 * Update max_installed_hard_intr to make the emergency intr poll
258 * a bit more efficient.
259 */
260 if (intr < FIRST_SOFTINT) {
261 if (max_installed_hard_intr <= intr)
262 max_installed_hard_intr = intr + 1;
263 } else {
264 if (max_installed_soft_intr <= intr)
265 max_installed_soft_intr = intr + 1;
266 }
9d522d14
MD
267
268 /*
269 * Setup the machine level interrupt vector
270 */
e8727dce 271 if (intr < FIRST_SOFTINT && info->i_slow + info->i_fast == 1) {
9d522d14
MD
272 if (machintr_vector_setup(intr, intr_flags))
273 printf("machintr_vector_setup: failed on irq %d\n", intr);
274 }
275
477d3c1c 276 return(rec);
ef0fdad1 277}
984263bc 278
9d522d14 279void
477d3c1c 280unregister_swi(void *id)
ef0fdad1 281{
9d522d14 282 unregister_int(id);
984263bc
MD
283}
284
9d522d14 285void
477d3c1c 286unregister_int(void *id)
984263bc 287{
477d3c1c
MD
288 struct intr_info *info;
289 struct intrec **list;
290 intrec_t rec;
291 int intr;
292
293 intr = ((intrec_t)id)->intr;
ef0fdad1 294
5f456c40 295 if (intr < 0 || intr >= MAX_INTS)
ef0fdad1 296 panic("register_int: bad intr %d", intr);
477d3c1c
MD
297
298 info = &intr_info_ary[intr];
299
300 /*
9d522d14
MD
301 * Remove the interrupt descriptor, adjust the descriptor count,
302 * and teardown the machine level vector if this was the last interrupt.
477d3c1c 303 */
ef0fdad1 304 crit_enter();
477d3c1c 305 list = &info->i_reclist;
ef0fdad1 306 while ((rec = *list) != NULL) {
9d522d14 307 if (rec == id)
ef0fdad1 308 break;
ef0fdad1
MD
309 list = &rec->next;
310 }
9d522d14 311 if (rec) {
acf7409e
SZ
312 intrec_t rec0;
313
9d522d14
MD
314 *list = rec->next;
315 if (rec->intr_flags & INTR_FAST)
316 --info->i_fast;
317 else
318 --info->i_slow;
e8727dce 319 if (intr < FIRST_SOFTINT && info->i_fast + info->i_slow == 0)
9d522d14 320 machintr_vector_teardown(intr);
862f2618 321
acf7409e
SZ
322 /*
323 * Clear i_mplock_required if no handlers in the chain require the
324 * MP lock.
325 */
326 for (rec0 = info->i_reclist; rec0; rec0 = rec0->next) {
327 if ((rec0->intr_flags & INTR_MPSAFE) == 0)
328 break;
329 }
330 if (rec0 == NULL)
862f2618 331 info->i_mplock_required = 0;
acf7409e 332 }
862f2618 333
ef0fdad1 334 crit_exit();
477d3c1c
MD
335
336 /*
9d522d14 337 * Free the record.
477d3c1c 338 */
ef0fdad1 339 if (rec != NULL) {
477d3c1c 340 free(rec->name, M_DEVBUF);
ef0fdad1
MD
341 free(rec, M_DEVBUF);
342 } else {
477d3c1c
MD
343 printf("warning: unregister_int: int %d handler for %s not found\n",
344 intr, ((intrec_t)id)->name);
ef0fdad1 345 }
477d3c1c
MD
346}
347
348const char *
349get_registered_name(int intr)
350{
351 intrec_t rec;
352
5f456c40 353 if (intr < 0 || intr >= MAX_INTS)
477d3c1c
MD
354 panic("register_int: bad intr %d", intr);
355
356 if ((rec = intr_info_ary[intr].i_reclist) == NULL)
357 return(NULL);
358 else if (rec->next)
359 return("mux");
360 else
361 return(rec->name);
984263bc
MD
362}
363
477d3c1c
MD
364int
365count_registered_ints(int intr)
366{
367 struct intr_info *info;
368
5f456c40 369 if (intr < 0 || intr >= MAX_INTS)
477d3c1c
MD
370 panic("register_int: bad intr %d", intr);
371 info = &intr_info_ary[intr];
372 return(info->i_fast + info->i_slow);
373}
374
375long
376get_interrupt_counter(int intr)
377{
378 struct intr_info *info;
379
5f456c40 380 if (intr < 0 || intr >= MAX_INTS)
477d3c1c
MD
381 panic("register_int: bad intr %d", intr);
382 info = &intr_info_ary[intr];
383 return(info->i_count);
384}
385
386
4b5f931b
MD
387void
388swi_setpriority(int intr, int pri)
389{
477d3c1c 390 struct intr_info *info;
4b5f931b 391
5f456c40 392 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
4b5f931b 393 panic("register_swi: bad intr %d", intr);
477d3c1c 394 info = &intr_info_ary[intr];
f33e9c1c 395 if (info->i_state != ISTATE_NOTHREAD)
477d3c1c 396 lwkt_setpri(&info->i_thread, pri);
4b5f931b
MD
397}
398
7e071e7a
MD
399void
400register_randintr(int intr)
401{
477d3c1c
MD
402 struct intr_info *info;
403
5f456c40 404 if (intr < 0 || intr >= MAX_INTS)
417c990a 405 panic("register_randintr: bad intr %d", intr);
477d3c1c
MD
406 info = &intr_info_ary[intr];
407 info->i_random.sc_intr = intr;
408 info->i_random.sc_enabled = 1;
7e071e7a
MD
409}
410
411void
412unregister_randintr(int intr)
413{
477d3c1c
MD
414 struct intr_info *info;
415
5f456c40 416 if (intr < 0 || intr >= MAX_INTS)
477d3c1c
MD
417 panic("register_swi: bad intr %d", intr);
418 info = &intr_info_ary[intr];
419 info->i_random.sc_enabled = 0;
7e071e7a
MD
420}
421
5f456c40
MD
422int
423next_registered_randintr(int intr)
424{
425 struct intr_info *info;
426
427 if (intr < 0 || intr >= MAX_INTS)
428 panic("register_swi: bad intr %d", intr);
429 while (intr < MAX_INTS) {
430 info = &intr_info_ary[intr];
431 if (info->i_random.sc_enabled)
432 break;
433 ++intr;
434 }
435 return(intr);
436}
437
ef0fdad1 438/*
b68b7282
MD
439 * Dispatch an interrupt. If there's nothing to do we have a stray
440 * interrupt and can just return, leaving the interrupt masked.
96728c05 441 *
477d3c1c 442 * We need to schedule the interrupt and set its i_running bit. If
96728c05
MD
443 * we are not on the interrupt thread's cpu we have to send a message
444 * to the correct cpu that will issue the desired action (interlocking
f33e9c1c
MD
445 * with the interrupt thread's critical section). We do NOT attempt to
446 * reschedule interrupts whos i_running bit is already set because
447 * this would prematurely wakeup a livelock-limited interrupt thread.
448 *
449 * i_running is only tested/set on the same cpu as the interrupt thread.
96728c05
MD
450 *
451 * We are NOT in a critical section, which will allow the scheduled
71ef2f5c 452 * interrupt to preempt us. The MP lock might *NOT* be held here.
ef0fdad1 453 */
b8a98473
MD
454#ifdef SMP
455
96728c05
MD
456static void
457sched_ithd_remote(void *arg)
458{
459 sched_ithd((int)arg);
460}
461
b8a98473
MD
462#endif
463
ef0fdad1
MD
464void
465sched_ithd(int intr)
466{
477d3c1c 467 struct intr_info *info;
ef0fdad1 468
477d3c1c
MD
469 info = &intr_info_ary[intr];
470
471 ++info->i_count;
f33e9c1c 472 if (info->i_state != ISTATE_NOTHREAD) {
477d3c1c 473 if (info->i_reclist == NULL) {
ef0fdad1 474 printf("sched_ithd: stray interrupt %d\n", intr);
b68b7282 475 } else {
b8a98473 476#ifdef SMP
477d3c1c 477 if (info->i_thread.td_gd == mycpu) {
f33e9c1c
MD
478 if (info->i_running == 0) {
479 info->i_running = 1;
480 if (info->i_state != ISTATE_LIVELOCKED)
481 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */
482 }
96728c05 483 } else {
477d3c1c
MD
484 lwkt_send_ipiq(info->i_thread.td_gd,
485 sched_ithd_remote, (void *)intr);
96728c05 486 }
b8a98473 487#else
f33e9c1c
MD
488 if (info->i_running == 0) {
489 info->i_running = 1;
490 if (info->i_state != ISTATE_LIVELOCKED)
491 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */
492 }
b8a98473 493#endif
b68b7282 494 }
ef0fdad1
MD
495 } else {
496 printf("sched_ithd: stray interrupt %d\n", intr);
497 }
498}
499
37d44089
MD
500/*
501 * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL
502 * might not be held).
503 */
504static void
477d3c1c 505ithread_livelock_wakeup(systimer_t st)
37d44089 506{
477d3c1c 507 struct intr_info *info;
37d44089 508
477d3c1c 509 info = &intr_info_ary[(int)st->data];
f33e9c1c 510 if (info->i_state != ISTATE_NOTHREAD)
477d3c1c 511 lwkt_schedule(&info->i_thread);
37d44089
MD
512}
513
67b9bb39 514/*
477d3c1c
MD
515 * This function is called drectly from the ICU or APIC vector code assembly
516 * to process an interrupt. The critical section and interrupt deferral
517 * checks have already been done but the function is entered WITHOUT
518 * a critical section held. The BGL may or may not be held.
519 *
520 * Must return non-zero if we do not want the vector code to re-enable
521 * the interrupt (which we don't if we have to schedule the interrupt)
67b9bb39 522 */
477d3c1c
MD
523int ithread_fast_handler(struct intrframe frame);
524
525int
526ithread_fast_handler(struct intrframe frame)
527{
528 int intr;
529 struct intr_info *info;
530 struct intrec **list;
531 int must_schedule;
532#ifdef SMP
533 int got_mplock;
534#endif
535 intrec_t rec, next_rec;
536 globaldata_t gd;
537
538 intr = frame.if_vec;
539 gd = mycpu;
540
541 info = &intr_info_ary[intr];
542
543 /*
544 * If we are not processing any FAST interrupts, just schedule the thing.
545 * (since we aren't in a critical section, this can result in a
546 * preemption)
547 */
548 if (info->i_fast == 0) {
549 sched_ithd(intr);
550 return(1);
551 }
552
553 /*
554 * This should not normally occur since interrupts ought to be
555 * masked if the ithread has been scheduled or is running.
556 */
557 if (info->i_running)
558 return(1);
559
560 /*
561 * Bump the interrupt nesting level to process any FAST interrupts.
562 * Obtain the MP lock as necessary. If the MP lock cannot be obtained,
563 * schedule the interrupt thread to deal with the issue instead.
564 *
565 * To reduce overhead, just leave the MP lock held once it has been
566 * obtained.
567 */
568 crit_enter_gd(gd);
569 ++gd->gd_intr_nesting_level;
570 ++gd->gd_cnt.v_intr;
571 must_schedule = info->i_slow;
572#ifdef SMP
573 got_mplock = 0;
574#endif
575
576 list = &info->i_reclist;
577 for (rec = *list; rec; rec = next_rec) {
578 next_rec = rec->next; /* rec may be invalid after call */
579
580 if (rec->intr_flags & INTR_FAST) {
581#ifdef SMP
582 if ((rec->intr_flags & INTR_MPSAFE) == 0 && got_mplock == 0) {
583 if (try_mplock() == 0) {
afd7b1c0
MD
584 int owner;
585
477d3c1c 586 /*
afd7b1c0
MD
587 * If we couldn't get the MP lock try to forward it
588 * to the cpu holding the MP lock, setting must_schedule
589 * to -1 so we do not schedule and also do not unmask
590 * the interrupt. Otherwise just schedule it.
477d3c1c 591 */
afd7b1c0
MD
592 owner = owner_mplock();
593 if (owner >= 0 && owner != gd->gd_cpuid) {
594 lwkt_send_ipiq_bycpu(owner, forward_fastint_remote,
7e2d9bde 595 (void *)intr);
afd7b1c0
MD
596 must_schedule = -1;
597 ++gd->gd_cnt.v_forwarded_ints;
598 } else {
599 must_schedule = 1;
600 }
477d3c1c
MD
601 break;
602 }
603 got_mplock = 1;
604 }
605#endif
606 if (rec->serializer) {
607 must_schedule += lwkt_serialize_handler_try(
608 rec->serializer, rec->handler,
609 rec->argument, &frame);
610 } else {
611 rec->handler(rec->argument, &frame);
612 }
613 }
614 }
615
616 /*
617 * Cleanup
618 */
619 --gd->gd_intr_nesting_level;
620#ifdef SMP
621 if (got_mplock)
622 rel_mplock();
623#endif
624 crit_exit_gd(gd);
625
626 /*
627 * If we had a problem, schedule the thread to catch the missed
628 * records (it will just re-run all of them). A return value of 0
629 * indicates that all handlers have been run and the interrupt can
630 * be re-enabled, and a non-zero return indicates that the interrupt
631 * thread controls re-enablement.
632 */
afd7b1c0 633 if (must_schedule > 0)
477d3c1c 634 sched_ithd(intr);
afd7b1c0 635 else if (must_schedule == 0)
477d3c1c
MD
636 ++info->i_count;
637 return(must_schedule);
638}
639
640#if 0
641
6426: ; \
643 /* could not get the MP lock, forward the interrupt */ \
644 movl mp_lock, %eax ; /* check race */ \
645 cmpl $MP_FREE_LOCK,%eax ; \
646 je 2b ; \
647 incl PCPU(cnt)+V_FORWARDED_INTS ; \
648 subl $12,%esp ; \
649 movl $irq_num,8(%esp) ; \
650 movl $forward_fastint_remote,4(%esp) ; \
651 movl %eax,(%esp) ; \
652 call lwkt_send_ipiq_bycpu ; \
653 addl $12,%esp ; \
654 jmp 5f ;
655
656#endif
67b9bb39 657
37d44089 658
b68b7282 659/*
45d76888
MD
660 * Interrupt threads run this as their main loop.
661 *
662 * The handler begins execution outside a critical section and with the BGL
663 * held.
37d44089 664 *
477d3c1c 665 * The i_running state starts at 0. When an interrupt occurs, the hardware
37d44089
MD
666 * interrupt is disabled and sched_ithd() The HW interrupt remains disabled
667 * until all routines have run. We then call ithread_done() to reenable
45d76888
MD
668 * the HW interrupt and deschedule us until the next interrupt.
669 *
477d3c1c 670 * We are responsible for atomically checking i_running and ithread_done()
45d76888 671 * is responsible for atomically checking for platform-specific delayed
477d3c1c 672 * interrupts. i_running for our irq is only set in the context of our cpu,
45d76888 673 * so a critical section is a sufficient interlock.
b68b7282 674 */
93781523
MD
675#define LIVELOCK_TIMEFRAME(freq) ((freq) >> 2) /* 1/4 second */
676
ef0fdad1
MD
677static void
678ithread_handler(void *arg)
679{
477d3c1c 680 struct intr_info *info;
f33e9c1c
MD
681 int use_limit;
682 int lticks;
683 int lcount;
477d3c1c 684 int intr;
9d522d14 685 int mpheld;
477d3c1c
MD
686 struct intrec **list;
687 intrec_t rec, nrec;
f33e9c1c 688 globaldata_t gd;
67b9bb39 689 struct systimer ill_timer; /* enforced freq. timer */
f33e9c1c 690 u_int ill_count; /* interrupt livelock counter */
45d76888 691
f33e9c1c
MD
692 ill_count = 0;
693 lticks = ticks;
694 lcount = 0;
477d3c1c
MD
695 intr = (int)arg;
696 info = &intr_info_ary[intr];
697 list = &info->i_reclist;
698 gd = mycpu;
699
45d76888 700 /*
862f2618
MD
701 * The loop must be entered with one critical section held. The thread
702 * is created with TDF_MPSAFE so the MP lock is not held on start.
45d76888
MD
703 */
704 crit_enter_gd(gd);
862f2618 705 mpheld = 0;
ef0fdad1 706
ef0fdad1 707 for (;;) {
862f2618
MD
708 /*
709 * The chain is only considered MPSAFE if all its interrupt handlers
710 * are MPSAFE. However, if intr_mpsafe has been turned off we
711 * always operate with the BGL.
712 */
0e6beaa3 713#ifdef SMP
862f2618
MD
714 if (intr_mpsafe == 0) {
715 if (mpheld == 0) {
716 get_mplock();
717 mpheld = 1;
718 }
719 } else if (info->i_mplock_required != mpheld) {
720 if (info->i_mplock_required) {
721 KKASSERT(mpheld == 0);
722 get_mplock();
723 mpheld = 1;
724 } else {
725 KKASSERT(mpheld != 0);
726 rel_mplock();
727 mpheld = 0;
728 }
729 }
0e6beaa3 730#endif
862f2618 731
93781523 732 /*
f33e9c1c
MD
733 * If an interrupt is pending, clear i_running and execute the
734 * handlers. Note that certain types of interrupts can re-trigger
735 * and set i_running again.
45d76888 736 *
f33e9c1c 737 * Each handler is run in a critical section. Note that we run both
862f2618 738 * FAST and SLOW designated service routines.
93781523 739 */
f33e9c1c
MD
740 if (info->i_running) {
741 ++ill_count;
742 info->i_running = 0;
9d522d14 743
f33e9c1c
MD
744 for (rec = *list; rec; rec = nrec) {
745 nrec = rec->next;
746 if (rec->serializer) {
747 lwkt_serialize_handler_call(rec->serializer, rec->handler,
748 rec->argument, NULL);
749 } else {
750 rec->handler(rec->argument, NULL);
751 }
477d3c1c 752 }
ef0fdad1 753 }
37d44089
MD
754
755 /*
756 * This is our interrupt hook to add rate randomness to the random
757 * number generator.
758 */
477d3c1c 759 if (info->i_random.sc_enabled)
96728c05 760 add_interrupt_randomness(intr);
37d44089
MD
761
762 /*
f33e9c1c
MD
763 * Unmask the interrupt to allow it to trigger again. This only
764 * applies to certain types of interrupts (typ level interrupts).
765 * This can result in the interrupt retriggering, but the retrigger
766 * will not be processed until we cycle our critical section.
363d922a
MD
767 *
768 * Only unmask interrupts while handlers are installed. It is
769 * possible to hit a situation where no handlers are installed
770 * due to a device driver livelocking and then tearing down its
771 * interrupt on close (the parallel bus being a good example).
37d44089 772 */
363d922a 773 if (*list)
37e7efec 774 machintr_intren(intr);
f33e9c1c
MD
775
776 /*
777 * Do a quick exit/enter to catch any higher-priority interrupt
778 * sources, such as the statclock, so thread time accounting
779 * will still work. This may also cause an interrupt to re-trigger.
780 */
781 crit_exit_gd(gd);
782 crit_enter_gd(gd);
783
784 /*
785 * LIVELOCK STATE MACHINE
786 */
787 switch(info->i_state) {
788 case ISTATE_NORMAL:
789 /*
790 * Calculate a running average every tick.
791 */
792 if (lticks != ticks) {
793 lticks = ticks;
794 ill_count -= ill_count / hz;
795 }
796
797 /*
798 * If we did not exceed the frequency limit, we are done.
799 * If the interrupt has not retriggered we deschedule ourselves.
800 */
801 if (ill_count <= livelock_limit) {
802 if (info->i_running == 0) {
803 lwkt_deschedule_self(gd->gd_curthread);
804 lwkt_switch();
805 }
37d44089 806 break;
f33e9c1c
MD
807 }
808
809 /*
810 * Otherwise we are livelocked. Set up a periodic systimer
811 * to wake the thread up at the limit frequency.
812 */
813 printf("intr %d at %d > %d hz, livelocked limit engaged!\n",
59d9413f 814 intr, ill_count, livelock_limit);
f33e9c1c
MD
815 info->i_state = ISTATE_LIVELOCKED;
816 if ((use_limit = livelock_limit) < 100)
817 use_limit = 100;
818 else if (use_limit > 500000)
819 use_limit = 500000;
820 systimer_init_periodic(&ill_timer, ithread_livelock_wakeup,
821 (void *)intr, use_limit);
822 lcount = 0;
37d44089 823 /* fall through */
f33e9c1c 824 case ISTATE_LIVELOCKED:
37d44089 825 /*
f33e9c1c
MD
826 * Wait for our periodic timer to go off. Since the interrupt
827 * has re-armed it can still set i_running, but it will not
828 * reschedule us while we are in a livelocked state.
37d44089 829 */
f33e9c1c 830 lwkt_deschedule_self(gd->gd_curthread);
37d44089 831 lwkt_switch();
93781523 832
37d44089 833 /*
f33e9c1c
MD
834 * Check to see if the livelock condition no longer applies.
835 * The interrupt must be able to operate normally for one
836 * full second before we restore normal operation.
37d44089 837 */
f33e9c1c
MD
838 if (lticks != ticks) {
839 lticks = ticks;
840 if (ill_count < livelock_lowater) {
841 if (++lcount >= hz) {
842 info->i_state = ISTATE_NORMAL;
843 systimer_del(&ill_timer);
844 printf("intr %d at %d < %d hz, livelock removed\n",
845 intr, ill_count, livelock_lowater);
846 }
847 } else {
848 lcount = 0;
849 }
850 ill_count -= ill_count / hz;
37d44089
MD
851 }
852 break;
853 }
ef0fdad1 854 }
e43a034f 855 /* not reached */
ef0fdad1
MD
856}
857
a9d00ec1
MD
858/*
859 * Emergency interrupt polling thread. The thread begins execution
860 * outside a critical section with the BGL held.
861 *
862 * If emergency interrupt polling is enabled, this thread will
863 * execute all system interrupts not marked INTR_NOPOLL at the
864 * specified polling frequency.
865 *
866 * WARNING! This thread runs *ALL* interrupt service routines that
867 * are not marked INTR_NOPOLL, which basically means everything except
868 * the 8254 clock interrupt and the ATA interrupt. It has very high
869 * overhead and should only be used in situations where the machine
870 * cannot otherwise be made to work. Due to the severe performance
871 * degredation, it should not be enabled on production machines.
872 */
873static void
874ithread_emergency(void *arg __unused)
875{
876 struct intr_info *info;
877 intrec_t rec, nrec;
878 int intr;
879
880 for (;;) {
5f456c40 881 for (intr = 0; intr < max_installed_hard_intr; ++intr) {
a9d00ec1
MD
882 info = &intr_info_ary[intr];
883 for (rec = info->i_reclist; rec; rec = nrec) {
884 if ((rec->intr_flags & INTR_NOPOLL) == 0) {
885 if (rec->serializer) {
886 lwkt_serialize_handler_call(rec->serializer,
887 rec->handler, rec->argument, NULL);
888 } else {
889 rec->handler(rec->argument, NULL);
890 }
891 }
892 nrec = rec->next;
893 }
894 }
895 lwkt_deschedule_self(curthread);
896 lwkt_switch();
897 }
898}
899
900/*
901 * Systimer callback - schedule the emergency interrupt poll thread
902 * if emergency polling is enabled.
903 */
904static
905void
906emergency_intr_timer_callback(systimer_t info, struct intrframe *frame __unused)
907{
908 if (emergency_intr_enable)
909 lwkt_schedule(info->data);
910}
911
984263bc
MD
912/*
913 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
914 * The data for this machine dependent, and the declarations are in machine
915 * dependent code. The layout of intrnames and intrcnt however is machine
916 * independent.
917 *
918 * We do not know the length of intrcnt and intrnames at compile time, so
919 * calculate things at run time.
920 */
477d3c1c 921
984263bc
MD
922static int
923sysctl_intrnames(SYSCTL_HANDLER_ARGS)
924{
477d3c1c
MD
925 struct intr_info *info;
926 intrec_t rec;
927 int error = 0;
928 int len;
929 int intr;
930 char buf[64];
931
5f456c40 932 for (intr = 0; error == 0 && intr < MAX_INTS; ++intr) {
477d3c1c
MD
933 info = &intr_info_ary[intr];
934
935 len = 0;
936 buf[0] = 0;
937 for (rec = info->i_reclist; rec; rec = rec->next) {
938 snprintf(buf + len, sizeof(buf) - len, "%s%s",
939 (len ? "/" : ""), rec->name);
940 len += strlen(buf + len);
941 }
942 if (len == 0) {
943 snprintf(buf, sizeof(buf), "irq%d", intr);
944 len = strlen(buf);
945 }
946 error = SYSCTL_OUT(req, buf, len + 1);
947 }
948 return (error);
984263bc
MD
949}
950
477d3c1c 951
984263bc
MD
952SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
953 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
954
955static int
956sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
957{
477d3c1c
MD
958 struct intr_info *info;
959 int error = 0;
960 int intr;
961
5f456c40 962 for (intr = 0; intr < max_installed_hard_intr; ++intr) {
477d3c1c
MD
963 info = &intr_info_ary[intr];
964
965 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
966 if (error)
5f456c40
MD
967 goto failed;
968 }
969 for (intr = FIRST_SOFTINT; intr < max_installed_soft_intr; ++intr) {
970 info = &intr_info_ary[intr];
971
972 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
973 if (error)
974 goto failed;
477d3c1c 975 }
5f456c40 976failed:
477d3c1c 977 return(error);
984263bc
MD
978}
979
980SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
981 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
477d3c1c 982