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