intr: Pass cpuid to register_int and unregister_int
[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 $
27 *
28 */
29
984263bc
MD
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/malloc.h>
33#include <sys/kernel.h>
34#include <sys/sysctl.h>
ef0fdad1
MD
35#include <sys/thread.h>
36#include <sys/proc.h>
7e071e7a 37#include <sys/random.h>
477d3c1c 38#include <sys/serialize.h>
a7231bde 39#include <sys/interrupt.h>
477d3c1c 40#include <sys/bus.h>
37e7efec 41#include <sys/machintr.h>
984263bc 42
477d3c1c 43#include <machine/frame.h>
984263bc 44
684a93c4
MD
45#include <sys/thread2.h>
46#include <sys/mplock2.h>
47
9d522d14
MD
48struct info_info;
49
ef0fdad1
MD
50typedef struct intrec {
51 struct intrec *next;
9d522d14 52 struct intr_info *info;
ef0fdad1
MD
53 inthand2_t *handler;
54 void *argument;
477d3c1c 55 char *name;
ef0fdad1 56 int intr;
477d3c1c
MD
57 int intr_flags;
58 struct lwkt_serialize *serializer;
59} *intrec_t;
60
61struct intr_info {
62 intrec_t i_reclist;
63 struct thread i_thread;
64 struct random_softc i_random;
65 int i_running;
862f2618
MD
66 long i_count; /* interrupts dispatched */
67 int i_mplock_required;
477d3c1c
MD
68 int i_fast;
69 int i_slow;
f33e9c1c 70 int i_state;
b560de96
MD
71 int i_errorticks;
72 unsigned long i_straycount;
5f456c40
MD
73} intr_info_ary[MAX_INTS];
74
75int max_installed_hard_intr;
76int max_installed_soft_intr;
477d3c1c 77
a9d00ec1
MD
78#define EMERGENCY_INTR_POLLING_FREQ_MAX 20000
79
250ce837
MD
80/*
81 * Assert that callers into interrupt handlers don't return with
82 * dangling tokens, spinlocks, or mp locks.
83 */
3933a3ab 84#ifdef INVARIANTS
149a3d9e
VS
85
86#define TD_INVARIANTS_DECLARE \
149a3d9e
VS
87 int spincount; \
88 lwkt_tokref_t curstop
89
90#define TD_INVARIANTS_GET(td) \
91 do { \
149a3d9e
VS
92 spincount = (td)->td_gd->gd_spinlocks_wr; \
93 curstop = (td)->td_toks_stop; \
94 } while(0)
95
96#define TD_INVARIANTS_TEST(td, name) \
97 do { \
98 KASSERT(spincount == (td)->td_gd->gd_spinlocks_wr, \
99 ("spincount mismatch after interrupt handler %s", \
100 name)); \
101 KASSERT(curstop == (td)->td_toks_stop, \
102 ("token count mismatch after interrupt handler %s", \
103 name)); \
149a3d9e 104 } while(0)
250ce837
MD
105
106#else
107
b5d16701 108/* !INVARIANTS */
3933a3ab
MD
109
110#define TD_INVARIANTS_DECLARE
111#define TD_INVARIANTS_GET(td)
149a3d9e 112#define TD_INVARIANTS_TEST(td, name)
3933a3ab 113
149a3d9e 114#endif /* ndef INVARIANTS */
3933a3ab 115
a9d00ec1
MD
116static int sysctl_emergency_freq(SYSCTL_HANDLER_ARGS);
117static int sysctl_emergency_enable(SYSCTL_HANDLER_ARGS);
96d52ac8 118static void emergency_intr_timer_callback(systimer_t, int, struct intrframe *);
a9d00ec1
MD
119static void ithread_handler(void *arg);
120static void ithread_emergency(void *arg);
b560de96 121static void report_stray_interrupt(int intr, struct intr_info *info);
6355d931 122static void int_moveto_destcpu(int *, int);
4c846371 123static void int_moveto_origcpu(int, int);
a9d00ec1 124
c157ff7a 125int intr_info_size = NELEM(intr_info_ary);
37d44089 126
a9d00ec1
MD
127static struct systimer emergency_intr_timer;
128static struct thread emergency_intr_thread;
129
f33e9c1c
MD
130#define ISTATE_NOTHREAD 0
131#define ISTATE_NORMAL 1
132#define ISTATE_LIVELOCKED 2
37d44089 133
b560de96 134static int livelock_limit = 40000;
0e6beaa3 135static int livelock_lowater = 20000;
b560de96 136static int livelock_debug = -1;
37d44089
MD
137SYSCTL_INT(_kern, OID_AUTO, livelock_limit,
138 CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit");
f33e9c1c
MD
139SYSCTL_INT(_kern, OID_AUTO, livelock_lowater,
140 CTLFLAG_RW, &livelock_lowater, 0, "Livelock low-water mark restore");
b560de96
MD
141SYSCTL_INT(_kern, OID_AUTO, livelock_debug,
142 CTLFLAG_RW, &livelock_debug, 0, "Livelock debug intr#");
984263bc 143
a9d00ec1
MD
144static int emergency_intr_enable = 0; /* emergency interrupt polling */
145TUNABLE_INT("kern.emergency_intr_enable", &emergency_intr_enable);
146SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_enable, CTLTYPE_INT | CTLFLAG_RW,
147 0, 0, sysctl_emergency_enable, "I", "Emergency Interrupt Poll Enable");
148
149static int emergency_intr_freq = 10; /* emergency polling frequency */
150TUNABLE_INT("kern.emergency_intr_freq", &emergency_intr_freq);
151SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_freq, CTLTYPE_INT | CTLFLAG_RW,
152 0, 0, sysctl_emergency_freq, "I", "Emergency Interrupt Poll Frequency");
153
154/*
155 * Sysctl support routines
156 */
157static int
158sysctl_emergency_enable(SYSCTL_HANDLER_ARGS)
159{
160 int error, enabled;
161
162 enabled = emergency_intr_enable;
163 error = sysctl_handle_int(oidp, &enabled, 0, req);
164 if (error || req->newptr == NULL)
165 return error;
166 emergency_intr_enable = enabled;
167 if (emergency_intr_enable) {
ba39e2e0
MD
168 systimer_adjust_periodic(&emergency_intr_timer,
169 emergency_intr_freq);
a9d00ec1 170 } else {
ba39e2e0 171 systimer_adjust_periodic(&emergency_intr_timer, 1);
a9d00ec1
MD
172 }
173 return 0;
174}
175
176static int
177sysctl_emergency_freq(SYSCTL_HANDLER_ARGS)
178{
179 int error, phz;
180
181 phz = emergency_intr_freq;
182 error = sysctl_handle_int(oidp, &phz, 0, req);
183 if (error || req->newptr == NULL)
184 return error;
185 if (phz <= 0)
186 return EINVAL;
187 else if (phz > EMERGENCY_INTR_POLLING_FREQ_MAX)
188 phz = EMERGENCY_INTR_POLLING_FREQ_MAX;
189
190 emergency_intr_freq = phz;
191 if (emergency_intr_enable) {
ba39e2e0
MD
192 systimer_adjust_periodic(&emergency_intr_timer,
193 emergency_intr_freq);
a9d00ec1 194 } else {
ba39e2e0 195 systimer_adjust_periodic(&emergency_intr_timer, 1);
a9d00ec1
MD
196 }
197 return 0;
198}
984263bc 199
45d76888
MD
200/*
201 * Register an SWI or INTerrupt handler.
45d76888 202 */
477d3c1c
MD
203void *
204register_swi(int intr, inthand2_t *handler, void *arg, const char *name,
205 struct lwkt_serialize *serializer)
984263bc 206{
5f456c40 207 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
ef0fdad1 208 panic("register_swi: bad intr %d", intr);
6355d931 209 return(register_int(intr, handler, arg, name, serializer, 0, 0));
984263bc
MD
210}
211
8619d09d
AH
212void *
213register_swi_mp(int intr, inthand2_t *handler, void *arg, const char *name,
214 struct lwkt_serialize *serializer)
215{
216 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
217 panic("register_swi: bad intr %d", intr);
6355d931 218 return(register_int(intr, handler, arg, name, serializer, INTR_MPSAFE, 0));
8619d09d
AH
219}
220
477d3c1c
MD
221void *
222register_int(int intr, inthand2_t *handler, void *arg, const char *name,
6355d931 223 struct lwkt_serialize *serializer, int intr_flags, int cpuid)
984263bc 224{
477d3c1c
MD
225 struct intr_info *info;
226 struct intrec **list;
227 intrec_t rec;
6355d931
SZ
228 int orig_cpuid;
229
230 KKASSERT(cpuid >= 0 && cpuid < ncpus);
ef0fdad1 231
5f456c40 232 if (intr < 0 || intr >= MAX_INTS)
ef0fdad1 233 panic("register_int: bad intr %d", intr);
477d3c1c
MD
234 if (name == NULL)
235 name = "???";
236 info = &intr_info_ary[intr];
237
9d522d14
MD
238 /*
239 * Construct an interrupt handler record
240 */
efda3bd0
MD
241 rec = kmalloc(sizeof(struct intrec), M_DEVBUF, M_INTWAIT);
242 rec->name = kmalloc(strlen(name) + 1, M_DEVBUF, M_INTWAIT);
477d3c1c 243 strcpy(rec->name, name);
ef0fdad1 244
9d522d14 245 rec->info = info;
ef0fdad1
MD
246 rec->handler = handler;
247 rec->argument = arg;
ef0fdad1 248 rec->intr = intr;
477d3c1c 249 rec->intr_flags = intr_flags;
ef0fdad1 250 rec->next = NULL;
477d3c1c 251 rec->serializer = serializer;
ef0fdad1 252
a9d00ec1
MD
253 /*
254 * Create an emergency polling thread and set up a systimer to wake
255 * it up.
256 */
257 if (emergency_intr_thread.td_kstack == NULL) {
fdce8919 258 lwkt_create(ithread_emergency, NULL, NULL, &emergency_intr_thread,
d2d8515b 259 TDF_STOPREQ | TDF_INTTHREAD, ncpus - 1, "ithread emerg");
a9d00ec1
MD
260 systimer_init_periodic_nq(&emergency_intr_timer,
261 emergency_intr_timer_callback, &emergency_intr_thread,
262 (emergency_intr_enable ? emergency_intr_freq : 1));
263 }
264
6355d931 265 int_moveto_destcpu(&orig_cpuid, cpuid);
db958607 266
ef0fdad1
MD
267 /*
268 * Create an interrupt thread if necessary, leave it in an unscheduled
45d76888 269 * state.
ef0fdad1 270 */
f33e9c1c
MD
271 if (info->i_state == ISTATE_NOTHREAD) {
272 info->i_state = ISTATE_NORMAL;
fdce8919 273 lwkt_create(ithread_handler, (void *)(intptr_t)intr, NULL,
6355d931 274 &info->i_thread, TDF_STOPREQ | TDF_INTTHREAD, cpuid,
fdce8919 275 "ithread %d", intr);
5f456c40 276 if (intr >= FIRST_SOFTINT)
477d3c1c 277 lwkt_setpri(&info->i_thread, TDPRI_SOFT_NORM);
4b5f931b 278 else
477d3c1c
MD
279 lwkt_setpri(&info->i_thread, TDPRI_INT_MED);
280 info->i_thread.td_preemptable = lwkt_preempt;
ef0fdad1
MD
281 }
282
9d522d14
MD
283 list = &info->i_reclist;
284
ef0fdad1 285 /*
9d522d14 286 * Keep track of how many fast and slow interrupts we have.
862f2618
MD
287 * Set i_mplock_required if any handler in the chain requires
288 * the MP lock to operate.
ef0fdad1 289 */
862f2618
MD
290 if ((intr_flags & INTR_MPSAFE) == 0)
291 info->i_mplock_required = 1;
f8a09be1 292 if (intr_flags & INTR_CLOCK)
9d522d14
MD
293 ++info->i_fast;
294 else
295 ++info->i_slow;
296
8b3ec75a
MD
297 /*
298 * Enable random number generation keying off of this interrupt.
299 */
300 if ((intr_flags & INTR_NOENTROPY) == 0 && info->i_random.sc_enabled == 0) {
301 info->i_random.sc_enabled = 1;
302 info->i_random.sc_intr = intr;
303 }
304
9d522d14
MD
305 /*
306 * Add the record to the interrupt list.
307 */
308 crit_enter();
ef0fdad1
MD
309 while (*list != NULL)
310 list = &(*list)->next;
311 *list = rec;
312 crit_exit();
5f456c40
MD
313
314 /*
315 * Update max_installed_hard_intr to make the emergency intr poll
316 * a bit more efficient.
317 */
318 if (intr < FIRST_SOFTINT) {
319 if (max_installed_hard_intr <= intr)
320 max_installed_hard_intr = intr + 1;
321 } else {
322 if (max_installed_soft_intr <= intr)
323 max_installed_soft_intr = intr + 1;
324 }
9d522d14
MD
325
326 /*
327 * Setup the machine level interrupt vector
328 */
f416026e
SZ
329 if (intr < FIRST_SOFTINT && info->i_slow + info->i_fast == 1)
330 machintr_intr_setup(intr, intr_flags);
9d522d14 331
4c846371 332 int_moveto_origcpu(orig_cpuid, cpuid);
db958607 333
477d3c1c 334 return(rec);
ef0fdad1 335}
984263bc 336
9d522d14 337void
477d3c1c 338unregister_swi(void *id)
ef0fdad1 339{
6355d931 340 unregister_int(id, 0);
984263bc
MD
341}
342
9d522d14 343void
6355d931 344unregister_int(void *id, int cpuid)
984263bc 345{
477d3c1c
MD
346 struct intr_info *info;
347 struct intrec **list;
348 intrec_t rec;
6355d931
SZ
349 int intr, orig_cpuid;
350
351 KKASSERT(cpuid >= 0 && cpuid < ncpus);
477d3c1c
MD
352
353 intr = ((intrec_t)id)->intr;
ef0fdad1 354
5f456c40 355 if (intr < 0 || intr >= MAX_INTS)
ef0fdad1 356 panic("register_int: bad intr %d", intr);
477d3c1c
MD
357
358 info = &intr_info_ary[intr];
359
6355d931 360 int_moveto_destcpu(&orig_cpuid, cpuid);
4c846371 361
477d3c1c 362 /*
9d522d14
MD
363 * Remove the interrupt descriptor, adjust the descriptor count,
364 * and teardown the machine level vector if this was the last interrupt.
477d3c1c 365 */
ef0fdad1 366 crit_enter();
477d3c1c 367 list = &info->i_reclist;
ef0fdad1 368 while ((rec = *list) != NULL) {
9d522d14 369 if (rec == id)
ef0fdad1 370 break;
ef0fdad1
MD
371 list = &rec->next;
372 }
9d522d14 373 if (rec) {
acf7409e
SZ
374 intrec_t rec0;
375
9d522d14 376 *list = rec->next;
f8a09be1 377 if (rec->intr_flags & INTR_CLOCK)
9d522d14
MD
378 --info->i_fast;
379 else
380 --info->i_slow;
e8727dce 381 if (intr < FIRST_SOFTINT && info->i_fast + info->i_slow == 0)
f416026e 382 machintr_intr_teardown(intr);
862f2618 383
acf7409e
SZ
384 /*
385 * Clear i_mplock_required if no handlers in the chain require the
386 * MP lock.
387 */
388 for (rec0 = info->i_reclist; rec0; rec0 = rec0->next) {
389 if ((rec0->intr_flags & INTR_MPSAFE) == 0)
390 break;
391 }
392 if (rec0 == NULL)
862f2618 393 info->i_mplock_required = 0;
acf7409e 394 }
862f2618 395
ef0fdad1 396 crit_exit();
477d3c1c 397
4c846371
SZ
398 int_moveto_origcpu(orig_cpuid, cpuid);
399
477d3c1c 400 /*
9d522d14 401 * Free the record.
477d3c1c 402 */
ef0fdad1 403 if (rec != NULL) {
efda3bd0
MD
404 kfree(rec->name, M_DEVBUF);
405 kfree(rec, M_DEVBUF);
ef0fdad1 406 } else {
6ea70f76 407 kprintf("warning: unregister_int: int %d handler for %s not found\n",
477d3c1c 408 intr, ((intrec_t)id)->name);
ef0fdad1 409 }
477d3c1c
MD
410}
411
412const char *
413get_registered_name(int intr)
414{
415 intrec_t rec;
416
5f456c40 417 if (intr < 0 || intr >= MAX_INTS)
477d3c1c
MD
418 panic("register_int: bad intr %d", intr);
419
420 if ((rec = intr_info_ary[intr].i_reclist) == NULL)
421 return(NULL);
422 else if (rec->next)
423 return("mux");
424 else
425 return(rec->name);
984263bc
MD
426}
427
477d3c1c
MD
428int
429count_registered_ints(int intr)
430{
431 struct intr_info *info;
432
5f456c40 433 if (intr < 0 || intr >= MAX_INTS)
477d3c1c
MD
434 panic("register_int: bad intr %d", intr);
435 info = &intr_info_ary[intr];
436 return(info->i_fast + info->i_slow);
437}
438
439long
440get_interrupt_counter(int intr)
441{
442 struct intr_info *info;
443
5f456c40 444 if (intr < 0 || intr >= MAX_INTS)
477d3c1c
MD
445 panic("register_int: bad intr %d", intr);
446 info = &intr_info_ary[intr];
447 return(info->i_count);
448}
449
450
4b5f931b
MD
451void
452swi_setpriority(int intr, int pri)
453{
477d3c1c 454 struct intr_info *info;
4b5f931b 455
5f456c40 456 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
4b5f931b 457 panic("register_swi: bad intr %d", intr);
477d3c1c 458 info = &intr_info_ary[intr];
f33e9c1c 459 if (info->i_state != ISTATE_NOTHREAD)
477d3c1c 460 lwkt_setpri(&info->i_thread, pri);
4b5f931b
MD
461}
462
7e071e7a
MD
463void
464register_randintr(int intr)
465{
477d3c1c
MD
466 struct intr_info *info;
467
5f456c40 468 if (intr < 0 || intr >= MAX_INTS)
417c990a 469 panic("register_randintr: bad intr %d", intr);
477d3c1c
MD
470 info = &intr_info_ary[intr];
471 info->i_random.sc_intr = intr;
472 info->i_random.sc_enabled = 1;
7e071e7a
MD
473}
474
475void
476unregister_randintr(int intr)
477{
477d3c1c
MD
478 struct intr_info *info;
479
5f456c40 480 if (intr < 0 || intr >= MAX_INTS)
477d3c1c
MD
481 panic("register_swi: bad intr %d", intr);
482 info = &intr_info_ary[intr];
8b3ec75a 483 info->i_random.sc_enabled = -1;
7e071e7a
MD
484}
485
5f456c40
MD
486int
487next_registered_randintr(int intr)
488{
489 struct intr_info *info;
490
491 if (intr < 0 || intr >= MAX_INTS)
492 panic("register_swi: bad intr %d", intr);
493 while (intr < MAX_INTS) {
494 info = &intr_info_ary[intr];
8b3ec75a 495 if (info->i_random.sc_enabled > 0)
5f456c40
MD
496 break;
497 ++intr;
498 }
499 return(intr);
500}
501
ef0fdad1 502/*
b68b7282
MD
503 * Dispatch an interrupt. If there's nothing to do we have a stray
504 * interrupt and can just return, leaving the interrupt masked.
96728c05 505 *
477d3c1c 506 * We need to schedule the interrupt and set its i_running bit. If
96728c05
MD
507 * we are not on the interrupt thread's cpu we have to send a message
508 * to the correct cpu that will issue the desired action (interlocking
f33e9c1c
MD
509 * with the interrupt thread's critical section). We do NOT attempt to
510 * reschedule interrupts whos i_running bit is already set because
511 * this would prematurely wakeup a livelock-limited interrupt thread.
512 *
513 * i_running is only tested/set on the same cpu as the interrupt thread.
96728c05
MD
514 *
515 * We are NOT in a critical section, which will allow the scheduled
71ef2f5c 516 * interrupt to preempt us. The MP lock might *NOT* be held here.
ef0fdad1 517 */
b8a98473
MD
518#ifdef SMP
519
96728c05
MD
520static void
521sched_ithd_remote(void *arg)
522{
bfc09ba0 523 sched_ithd((int)(intptr_t)arg);
96728c05
MD
524}
525
b8a98473
MD
526#endif
527
ef0fdad1
MD
528void
529sched_ithd(int intr)
530{
477d3c1c 531 struct intr_info *info;
ef0fdad1 532
477d3c1c
MD
533 info = &intr_info_ary[intr];
534
535 ++info->i_count;
f33e9c1c 536 if (info->i_state != ISTATE_NOTHREAD) {
477d3c1c 537 if (info->i_reclist == NULL) {
b560de96 538 report_stray_interrupt(intr, info);
b68b7282 539 } else {
b8a98473 540#ifdef SMP
477d3c1c 541 if (info->i_thread.td_gd == mycpu) {
f33e9c1c
MD
542 if (info->i_running == 0) {
543 info->i_running = 1;
544 if (info->i_state != ISTATE_LIVELOCKED)
545 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */
546 }
96728c05 547 } else {
477d3c1c 548 lwkt_send_ipiq(info->i_thread.td_gd,
bfc09ba0 549 sched_ithd_remote, (void *)(intptr_t)intr);
96728c05 550 }
b8a98473 551#else
f33e9c1c
MD
552 if (info->i_running == 0) {
553 info->i_running = 1;
554 if (info->i_state != ISTATE_LIVELOCKED)
555 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */
556 }
b8a98473 557#endif
b68b7282 558 }
ef0fdad1 559 } else {
b560de96 560 report_stray_interrupt(intr, info);
ef0fdad1
MD
561 }
562}
563
b560de96
MD
564static void
565report_stray_interrupt(int intr, struct intr_info *info)
566{
567 ++info->i_straycount;
568 if (info->i_straycount < 10) {
569 if (info->i_errorticks == ticks)
570 return;
571 info->i_errorticks = ticks;
572 kprintf("sched_ithd: stray interrupt %d on cpu %d\n",
573 intr, mycpuid);
7e88c0e6 574 } else if (info->i_straycount == 10) {
b560de96
MD
575 kprintf("sched_ithd: %ld stray interrupts %d on cpu %d - "
576 "there will be no further reports\n",
577 info->i_straycount, intr, mycpuid);
578 }
579}
580
37d44089
MD
581/*
582 * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL
583 * might not be held).
584 */
585static void
96d52ac8
SZ
586ithread_livelock_wakeup(systimer_t st, int in_ipi __unused,
587 struct intrframe *frame __unused)
37d44089 588{
477d3c1c 589 struct intr_info *info;
37d44089 590
973c11b9 591 info = &intr_info_ary[(int)(intptr_t)st->data];
f33e9c1c 592 if (info->i_state != ISTATE_NOTHREAD)
477d3c1c 593 lwkt_schedule(&info->i_thread);
37d44089
MD
594}
595
729e15a8
SZ
596/*
597 * Schedule ithread within fast intr handler
598 *
599 * XXX Protect sched_ithd() call with gd_intr_nesting_level?
600 * Interrupts aren't enabled, but still...
601 */
602static __inline void
603ithread_fast_sched(int intr, thread_t td)
604{
605 ++td->td_nest_count;
606
607 /*
608 * We are already in critical section, exit it now to
609 * allow preemption.
610 */
611 crit_exit_quick(td);
612 sched_ithd(intr);
613 crit_enter_quick(td);
614
615 --td->td_nest_count;
616}
617
67b9bb39 618/*
7bd34050 619 * This function is called directly from the ICU or APIC vector code assembly
477d3c1c
MD
620 * to process an interrupt. The critical section and interrupt deferral
621 * checks have already been done but the function is entered WITHOUT
622 * a critical section held. The BGL may or may not be held.
623 *
624 * Must return non-zero if we do not want the vector code to re-enable
625 * the interrupt (which we don't if we have to schedule the interrupt)
67b9bb39 626 */
c7eb0589 627int ithread_fast_handler(struct intrframe *frame);
477d3c1c
MD
628
629int
c7eb0589 630ithread_fast_handler(struct intrframe *frame)
477d3c1c
MD
631{
632 int intr;
633 struct intr_info *info;
634 struct intrec **list;
635 int must_schedule;
636#ifdef SMP
637 int got_mplock;
638#endif
3933a3ab 639 TD_INVARIANTS_DECLARE;
c24c20c0 640 intrec_t rec, nrec;
477d3c1c 641 globaldata_t gd;
729e15a8 642 thread_t td;
477d3c1c 643
c7eb0589 644 intr = frame->if_vec;
477d3c1c 645 gd = mycpu;
729e15a8
SZ
646 td = curthread;
647
648 /* We must be in critical section. */
f9235b6d 649 KKASSERT(td->td_critcount);
477d3c1c
MD
650
651 info = &intr_info_ary[intr];
652
653 /*
654 * If we are not processing any FAST interrupts, just schedule the thing.
477d3c1c
MD
655 */
656 if (info->i_fast == 0) {
3848f1c7 657 ++gd->gd_cnt.v_intr;
729e15a8 658 ithread_fast_sched(intr, td);
477d3c1c
MD
659 return(1);
660 }
661
662 /*
663 * This should not normally occur since interrupts ought to be
664 * masked if the ithread has been scheduled or is running.
665 */
666 if (info->i_running)
667 return(1);
668
669 /*
670 * Bump the interrupt nesting level to process any FAST interrupts.
671 * Obtain the MP lock as necessary. If the MP lock cannot be obtained,
672 * schedule the interrupt thread to deal with the issue instead.
673 *
674 * To reduce overhead, just leave the MP lock held once it has been
675 * obtained.
676 */
477d3c1c
MD
677 ++gd->gd_intr_nesting_level;
678 ++gd->gd_cnt.v_intr;
679 must_schedule = info->i_slow;
680#ifdef SMP
681 got_mplock = 0;
682#endif
683
3933a3ab 684 TD_INVARIANTS_GET(td);
477d3c1c 685 list = &info->i_reclist;
3933a3ab 686
c24c20c0
MD
687 for (rec = *list; rec; rec = nrec) {
688 /* rec may be invalid after call */
689 nrec = rec->next;
477d3c1c 690
f8a09be1 691 if (rec->intr_flags & INTR_CLOCK) {
477d3c1c
MD
692#ifdef SMP
693 if ((rec->intr_flags & INTR_MPSAFE) == 0 && got_mplock == 0) {
694 if (try_mplock() == 0) {
f5c2d910
SZ
695 /* Couldn't get the MP lock; just schedule it. */
696 must_schedule = 1;
477d3c1c
MD
697 break;
698 }
699 got_mplock = 1;
700 }
701#endif
702 if (rec->serializer) {
703 must_schedule += lwkt_serialize_handler_try(
704 rec->serializer, rec->handler,
c7eb0589 705 rec->argument, frame);
477d3c1c 706 } else {
c7eb0589 707 rec->handler(rec->argument, frame);
477d3c1c 708 }
3933a3ab 709 TD_INVARIANTS_TEST(td, rec->name);
477d3c1c
MD
710 }
711 }
712
713 /*
714 * Cleanup
715 */
716 --gd->gd_intr_nesting_level;
717#ifdef SMP
718 if (got_mplock)
719 rel_mplock();
720#endif
477d3c1c
MD
721
722 /*
729e15a8
SZ
723 * If we had a problem, or mixed fast and slow interrupt handlers are
724 * registered, schedule the ithread to catch the missed records (it
725 * will just re-run all of them). A return value of 0 indicates that
726 * all handlers have been run and the interrupt can be re-enabled, and
727 * a non-zero return indicates that the interrupt thread controls
728 * re-enablement.
477d3c1c 729 */
afd7b1c0 730 if (must_schedule > 0)
729e15a8 731 ithread_fast_sched(intr, td);
afd7b1c0 732 else if (must_schedule == 0)
477d3c1c
MD
733 ++info->i_count;
734 return(must_schedule);
735}
736
b68b7282 737/*
45d76888
MD
738 * Interrupt threads run this as their main loop.
739 *
68b3ccd4 740 * The handler begins execution outside a critical section and no MP lock.
37d44089 741 *
477d3c1c 742 * The i_running state starts at 0. When an interrupt occurs, the hardware
37d44089
MD
743 * interrupt is disabled and sched_ithd() The HW interrupt remains disabled
744 * until all routines have run. We then call ithread_done() to reenable
45d76888
MD
745 * the HW interrupt and deschedule us until the next interrupt.
746 *
477d3c1c 747 * We are responsible for atomically checking i_running and ithread_done()
45d76888 748 * is responsible for atomically checking for platform-specific delayed
477d3c1c 749 * interrupts. i_running for our irq is only set in the context of our cpu,
45d76888 750 * so a critical section is a sufficient interlock.
b68b7282 751 */
93781523
MD
752#define LIVELOCK_TIMEFRAME(freq) ((freq) >> 2) /* 1/4 second */
753
ef0fdad1
MD
754static void
755ithread_handler(void *arg)
756{
477d3c1c 757 struct intr_info *info;
f33e9c1c 758 int use_limit;
b560de96 759 __uint32_t lseconds;
477d3c1c 760 int intr;
9d522d14 761 int mpheld;
477d3c1c
MD
762 struct intrec **list;
763 intrec_t rec, nrec;
f33e9c1c 764 globaldata_t gd;
67b9bb39 765 struct systimer ill_timer; /* enforced freq. timer */
f33e9c1c 766 u_int ill_count; /* interrupt livelock counter */
3933a3ab 767 TD_INVARIANTS_DECLARE;
45d76888 768
f33e9c1c 769 ill_count = 0;
973c11b9 770 intr = (int)(intptr_t)arg;
477d3c1c
MD
771 info = &intr_info_ary[intr];
772 list = &info->i_reclist;
477d3c1c 773
45d76888 774 /*
862f2618 775 * The loop must be entered with one critical section held. The thread
fdce8919 776 * does not hold the mplock on startup.
45d76888 777 */
e381e77c
MD
778 gd = mycpu;
779 lseconds = gd->gd_time_seconds;
45d76888 780 crit_enter_gd(gd);
862f2618 781 mpheld = 0;
ef0fdad1 782
ef0fdad1 783 for (;;) {
862f2618
MD
784 /*
785 * The chain is only considered MPSAFE if all its interrupt handlers
786 * are MPSAFE. However, if intr_mpsafe has been turned off we
787 * always operate with the BGL.
788 */
0e6beaa3 789#ifdef SMP
c9e9fb21 790 if (info->i_mplock_required != mpheld) {
862f2618
MD
791 if (info->i_mplock_required) {
792 KKASSERT(mpheld == 0);
c9e9fb21 793 get_mplock();
862f2618
MD
794 mpheld = 1;
795 } else {
796 KKASSERT(mpheld != 0);
797 rel_mplock();
798 mpheld = 0;
799 }
800 }
0e6beaa3 801#endif
862f2618 802
3933a3ab
MD
803 TD_INVARIANTS_GET(gd->gd_curthread);
804
93781523 805 /*
f33e9c1c
MD
806 * If an interrupt is pending, clear i_running and execute the
807 * handlers. Note that certain types of interrupts can re-trigger
808 * and set i_running again.
45d76888 809 *
f33e9c1c 810 * Each handler is run in a critical section. Note that we run both
862f2618 811 * FAST and SLOW designated service routines.
93781523 812 */
f33e9c1c
MD
813 if (info->i_running) {
814 ++ill_count;
815 info->i_running = 0;
9d522d14 816
b560de96
MD
817 if (*list == NULL)
818 report_stray_interrupt(intr, info);
819
f33e9c1c 820 for (rec = *list; rec; rec = nrec) {
c24c20c0 821 /* rec may be invalid after call */
f33e9c1c
MD
822 nrec = rec->next;
823 if (rec->serializer) {
824 lwkt_serialize_handler_call(rec->serializer, rec->handler,
825 rec->argument, NULL);
826 } else {
827 rec->handler(rec->argument, NULL);
828 }
3933a3ab 829 TD_INVARIANTS_TEST(gd->gd_curthread, rec->name);
477d3c1c 830 }
ef0fdad1 831 }
37d44089
MD
832
833 /*
834 * This is our interrupt hook to add rate randomness to the random
835 * number generator.
836 */
8b3ec75a 837 if (info->i_random.sc_enabled > 0)
96728c05 838 add_interrupt_randomness(intr);
37d44089
MD
839
840 /*
f33e9c1c
MD
841 * Unmask the interrupt to allow it to trigger again. This only
842 * applies to certain types of interrupts (typ level interrupts).
843 * This can result in the interrupt retriggering, but the retrigger
844 * will not be processed until we cycle our critical section.
363d922a
MD
845 *
846 * Only unmask interrupts while handlers are installed. It is
847 * possible to hit a situation where no handlers are installed
848 * due to a device driver livelocking and then tearing down its
849 * interrupt on close (the parallel bus being a good example).
37d44089 850 */
6d164b20 851 if (intr < FIRST_SOFTINT && *list)
35b2edcb 852 machintr_intr_enable(intr);
f33e9c1c
MD
853
854 /*
855 * Do a quick exit/enter to catch any higher-priority interrupt
856 * sources, such as the statclock, so thread time accounting
857 * will still work. This may also cause an interrupt to re-trigger.
858 */
859 crit_exit_gd(gd);
860 crit_enter_gd(gd);
861
862 /*
863 * LIVELOCK STATE MACHINE
864 */
865 switch(info->i_state) {
866 case ISTATE_NORMAL:
867 /*
b560de96 868 * Reset the count each second.
f33e9c1c 869 */
b560de96
MD
870 if (lseconds != gd->gd_time_seconds) {
871 lseconds = gd->gd_time_seconds;
872 ill_count = 0;
f33e9c1c
MD
873 }
874
875 /*
876 * If we did not exceed the frequency limit, we are done.
877 * If the interrupt has not retriggered we deschedule ourselves.
878 */
879 if (ill_count <= livelock_limit) {
880 if (info->i_running == 0) {
881 lwkt_deschedule_self(gd->gd_curthread);
882 lwkt_switch();
883 }
37d44089 884 break;
f33e9c1c
MD
885 }
886
887 /*
888 * Otherwise we are livelocked. Set up a periodic systimer
889 * to wake the thread up at the limit frequency.
890 */
b560de96 891 kprintf("intr %d at %d/%d hz, livelocked limit engaged!\n",
59d9413f 892 intr, ill_count, livelock_limit);
f33e9c1c
MD
893 info->i_state = ISTATE_LIVELOCKED;
894 if ((use_limit = livelock_limit) < 100)
895 use_limit = 100;
896 else if (use_limit > 500000)
897 use_limit = 500000;
79b38af2 898 systimer_init_periodic_nq(&ill_timer, ithread_livelock_wakeup,
973c11b9 899 (void *)(intptr_t)intr, use_limit);
37d44089 900 /* fall through */
f33e9c1c 901 case ISTATE_LIVELOCKED:
37d44089 902 /*
f33e9c1c
MD
903 * Wait for our periodic timer to go off. Since the interrupt
904 * has re-armed it can still set i_running, but it will not
905 * reschedule us while we are in a livelocked state.
37d44089 906 */
f33e9c1c 907 lwkt_deschedule_self(gd->gd_curthread);
37d44089 908 lwkt_switch();
93781523 909
37d44089 910 /*
b560de96
MD
911 * Check once a second to see if the livelock condition no
912 * longer applies.
37d44089 913 */
b560de96
MD
914 if (lseconds != gd->gd_time_seconds) {
915 lseconds = gd->gd_time_seconds;
f33e9c1c 916 if (ill_count < livelock_lowater) {
b560de96
MD
917 info->i_state = ISTATE_NORMAL;
918 systimer_del(&ill_timer);
919 kprintf("intr %d at %d/%d hz, livelock removed\n",
920 intr, ill_count, livelock_lowater);
921 } else if (livelock_debug == intr ||
922 (bootverbose && cold)) {
923 kprintf("intr %d at %d/%d hz, in livelock\n",
924 intr, ill_count, livelock_lowater);
f33e9c1c 925 }
b560de96 926 ill_count = 0;
37d44089
MD
927 }
928 break;
929 }
ef0fdad1 930 }
eccb255f 931 /* NOT REACHED */
ef0fdad1
MD
932}
933
a9d00ec1
MD
934/*
935 * Emergency interrupt polling thread. The thread begins execution
936 * outside a critical section with the BGL held.
937 *
938 * If emergency interrupt polling is enabled, this thread will
939 * execute all system interrupts not marked INTR_NOPOLL at the
940 * specified polling frequency.
941 *
942 * WARNING! This thread runs *ALL* interrupt service routines that
943 * are not marked INTR_NOPOLL, which basically means everything except
944 * the 8254 clock interrupt and the ATA interrupt. It has very high
945 * overhead and should only be used in situations where the machine
946 * cannot otherwise be made to work. Due to the severe performance
947 * degredation, it should not be enabled on production machines.
948 */
949static void
950ithread_emergency(void *arg __unused)
951{
eccb255f 952 globaldata_t gd = mycpu;
a9d00ec1
MD
953 struct intr_info *info;
954 intrec_t rec, nrec;
955 int intr;
3933a3ab 956 TD_INVARIANTS_DECLARE;
a9d00ec1 957
c9e9fb21 958 get_mplock();
eccb255f
MD
959 crit_enter_gd(gd);
960 TD_INVARIANTS_GET(gd->gd_curthread);
c9e9fb21 961
a9d00ec1 962 for (;;) {
5f456c40 963 for (intr = 0; intr < max_installed_hard_intr; ++intr) {
a9d00ec1
MD
964 info = &intr_info_ary[intr];
965 for (rec = info->i_reclist; rec; rec = nrec) {
c24c20c0
MD
966 /* rec may be invalid after call */
967 nrec = rec->next;
a9d00ec1
MD
968 if ((rec->intr_flags & INTR_NOPOLL) == 0) {
969 if (rec->serializer) {
c24c20c0 970 lwkt_serialize_handler_try(rec->serializer,
a9d00ec1
MD
971 rec->handler, rec->argument, NULL);
972 } else {
973 rec->handler(rec->argument, NULL);
974 }
eccb255f 975 TD_INVARIANTS_TEST(gd->gd_curthread, rec->name);
a9d00ec1 976 }
a9d00ec1
MD
977 }
978 }
eccb255f 979 lwkt_deschedule_self(gd->gd_curthread);
a9d00ec1
MD
980 lwkt_switch();
981 }
eccb255f 982 /* NOT REACHED */
a9d00ec1
MD
983}
984
985/*
986 * Systimer callback - schedule the emergency interrupt poll thread
987 * if emergency polling is enabled.
988 */
989static
990void
96d52ac8
SZ
991emergency_intr_timer_callback(systimer_t info, int in_ipi __unused,
992 struct intrframe *frame __unused)
a9d00ec1
MD
993{
994 if (emergency_intr_enable)
995 lwkt_schedule(info->data);
996}
997
9db4b353
SZ
998int
999ithread_cpuid(int intr)
1000{
1001 const struct intr_info *info;
1002
1003 KKASSERT(intr >= 0 && intr < MAX_INTS);
1004 info = &intr_info_ary[intr];
1005
1006 if (info->i_state == ISTATE_NOTHREAD)
1007 return -1;
1008 return info->i_thread.td_gd->gd_cpuid;
1009}
1010
984263bc
MD
1011/*
1012 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1013 * The data for this machine dependent, and the declarations are in machine
1014 * dependent code. The layout of intrnames and intrcnt however is machine
1015 * independent.
1016 *
1017 * We do not know the length of intrcnt and intrnames at compile time, so
1018 * calculate things at run time.
1019 */
477d3c1c 1020
984263bc
MD
1021static int
1022sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1023{
477d3c1c
MD
1024 struct intr_info *info;
1025 intrec_t rec;
1026 int error = 0;
1027 int len;
1028 int intr;
1029 char buf[64];
1030
5f456c40 1031 for (intr = 0; error == 0 && intr < MAX_INTS; ++intr) {
477d3c1c
MD
1032 info = &intr_info_ary[intr];
1033
1034 len = 0;
1035 buf[0] = 0;
1036 for (rec = info->i_reclist; rec; rec = rec->next) {
f8c7a42d 1037 ksnprintf(buf + len, sizeof(buf) - len, "%s%s",
477d3c1c
MD
1038 (len ? "/" : ""), rec->name);
1039 len += strlen(buf + len);
1040 }
1041 if (len == 0) {
f8c7a42d 1042 ksnprintf(buf, sizeof(buf), "irq%d", intr);
477d3c1c
MD
1043 len = strlen(buf);
1044 }
1045 error = SYSCTL_OUT(req, buf, len + 1);
1046 }
1047 return (error);
984263bc
MD
1048}
1049
477d3c1c 1050
984263bc
MD
1051SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1052 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1053
1054static int
1055sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1056{
477d3c1c
MD
1057 struct intr_info *info;
1058 int error = 0;
1059 int intr;
1060
5f456c40 1061 for (intr = 0; intr < max_installed_hard_intr; ++intr) {
477d3c1c
MD
1062 info = &intr_info_ary[intr];
1063
1064 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
1065 if (error)
5f456c40
MD
1066 goto failed;
1067 }
1068 for (intr = FIRST_SOFTINT; intr < max_installed_soft_intr; ++intr) {
1069 info = &intr_info_ary[intr];
1070
1071 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
1072 if (error)
1073 goto failed;
477d3c1c 1074 }
5f456c40 1075failed:
477d3c1c 1076 return(error);
984263bc
MD
1077}
1078
1079SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1080 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
477d3c1c 1081
3242c748
SZ
1082static int
1083sysctl_intrcnt_all(SYSCTL_HANDLER_ARGS)
1084{
1085 struct intr_info *info;
1086 int error = 0;
1087 int intr;
1088
1089 for (intr = 0; intr < MAX_INTS; ++intr) {
1090 info = &intr_info_ary[intr];
1091
1092 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
1093 if (error)
1094 goto failed;
1095 }
1096failed:
1097 return(error);
1098}
1099
1100SYSCTL_PROC(_hw, OID_AUTO, intrcnt_all, CTLTYPE_OPAQUE | CTLFLAG_RD,
1101 NULL, 0, sysctl_intrcnt_all, "", "Interrupt Counts");
1102
4c846371 1103static void
6355d931 1104int_moveto_destcpu(int *orig_cpuid0, int cpuid)
4c846371 1105{
6355d931 1106 int orig_cpuid = mycpuid;
4c846371
SZ
1107
1108 if (cpuid != orig_cpuid)
1109 lwkt_migratecpu(cpuid);
1110
1111 *orig_cpuid0 = orig_cpuid;
4c846371
SZ
1112}
1113
1114static void
1115int_moveto_origcpu(int orig_cpuid, int cpuid)
1116{
1117 if (cpuid != orig_cpuid)
1118 lwkt_migratecpu(orig_cpuid);
1119}