Add 64 bit display output support to sysctl plus convenient macros.
[dragonfly.git] / sys / kern / kern_intr.c
CommitLineData
984263bc 1/*
ef0fdad1
MD
2 * Copyright (c) 2003, Matthew Dillon <dillon@backplane.com> All rights reserved.
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 $
b68b7282 27 * $DragonFly: src/sys/kern/kern_intr.c,v 1.4 2003/06/29 05:29:31 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>
984263bc
MD
39
40#include <machine/ipl.h>
41
42#include <sys/interrupt.h>
43
ef0fdad1
MD
44typedef struct intrec {
45 struct intrec *next;
46 inthand2_t *handler;
47 void *argument;
48 const char *name;
49 int intr;
50} intrec_t;
51
52static intrec_t *intlists[NHWI+NSWI];
53static thread_t ithreads[NHWI+NSWI];
54static struct thread ithread_ary[NHWI+NSWI];
984263bc 55
ef0fdad1 56static void ithread_handler(void *arg);
984263bc
MD
57
58void
ef0fdad1 59register_swi(int intr, inthand2_t *handler, void *arg, const char *name)
984263bc 60{
ef0fdad1
MD
61 if (intr < NHWI || intr >= NHWI + NSWI)
62 panic("register_swi: bad intr %d", intr);
63 register_int(intr, handler, arg, name);
984263bc
MD
64}
65
66void
ef0fdad1 67register_int(int intr, inthand2_t *handler, void *arg, const char *name)
984263bc 68{
ef0fdad1
MD
69 intrec_t **list;
70 intrec_t *rec;
71 thread_t td;
72
73 if (intr < 0 || intr > NHWI + NSWI)
74 panic("register_int: bad intr %d", intr);
75
76 rec = malloc(sizeof(intrec_t), M_DEVBUF, M_NOWAIT);
77 if (rec == NULL)
78 panic("register_swi: malloc failed");
79 rec->handler = handler;
80 rec->argument = arg;
81 rec->name = name;
82 rec->intr = intr;
83 rec->next = NULL;
84
85 list = &intlists[intr];
86
87 /*
88 * Create an interrupt thread if necessary, leave it in an unscheduled
89 * state.
90 */
91 if ((td = ithreads[intr]) == NULL) {
92 lwkt_create((void *)ithread_handler, (void *)intr, &ithreads[intr],
93 &ithread_ary[intr], TDF_STOPREQ, "ithread %d", intr);
94 td = ithreads[intr];
95 }
96
97 /*
98 * Add the record to the interrupt list
99 */
100 crit_enter(); /* token */
101 while (*list != NULL)
102 list = &(*list)->next;
103 *list = rec;
104 crit_exit();
105}
984263bc 106
ef0fdad1
MD
107void
108unregister_swi(int intr, inthand2_t *handler)
109{
110 if (intr < NHWI || intr >= NHWI + NSWI)
111 panic("register_swi: bad intr %d", intr);
112 unregister_int(intr, handler);
984263bc
MD
113}
114
115void
ef0fdad1 116unregister_int(int intr, inthand2_t handler)
984263bc 117{
ef0fdad1
MD
118 intrec_t **list;
119 intrec_t *rec;
120
121 if (intr < 0 || intr > NHWI + NSWI)
122 panic("register_int: bad intr %d", intr);
123 list = &intlists[intr];
124 crit_enter();
125 while ((rec = *list) != NULL) {
126 if (rec->handler == (void *)handler) {
127 *list = rec->next;
128 break;
984263bc 129 }
ef0fdad1
MD
130 list = &rec->next;
131 }
132 crit_exit();
133 if (rec != NULL) {
134 free(rec, M_DEVBUF);
135 } else {
136 printf("warning: unregister_int: int %d handler %p not found\n",
137 intr, handler);
138 }
984263bc
MD
139}
140
ef0fdad1 141/*
b68b7282
MD
142 * Dispatch an interrupt. If there's nothing to do we have a stray
143 * interrupt and can just return, leaving the interrupt masked.
ef0fdad1
MD
144 */
145void
146sched_ithd(int intr)
147{
148 thread_t td;
149
150 if ((td = ithreads[intr]) != NULL) {
b68b7282 151 if (intlists[intr] == NULL) {
ef0fdad1 152 printf("sched_ithd: stray interrupt %d\n", intr);
b68b7282 153 } else {
ef0fdad1 154 lwkt_schedule(td);
b68b7282
MD
155 lwkt_preempt(td, intr);
156 }
ef0fdad1
MD
157 } else {
158 printf("sched_ithd: stray interrupt %d\n", intr);
159 }
160}
161
b68b7282
MD
162/*
163 * Interrupt threads run this as their main loop.
164 */
ef0fdad1
MD
165static void
166ithread_handler(void *arg)
167{
168 int intr = (int)arg;
169 intrec_t **list = &intlists[intr];
170 intrec_t *rec;
171 intrec_t *nrec;
172
173 crit_enter(); /* replaces SPLs */
174 for (;;) {
175 for (rec = *list; rec; rec = nrec) {
176 nrec = rec->next;
177 rec->handler(rec->argument);
178 }
179 ithread_done(intr);
b68b7282
MD
180
181 /*
182 * temporary sanity check. If we preempted another thread we
183 * are placed in another critical section by that thread which
184 * will be released when we block and resume the original thread.
185 */
186 KKASSERT(curthread->td_pri == TDPRI_CRIT ||
187 (curthread->td_preempted && curthread->td_pri == 2 * TDPRI_CRIT));
ef0fdad1
MD
188 }
189 crit_exit(); /* not reached */
190}
191
192
984263bc
MD
193/*
194 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
195 * The data for this machine dependent, and the declarations are in machine
196 * dependent code. The layout of intrnames and intrcnt however is machine
197 * independent.
198 *
199 * We do not know the length of intrcnt and intrnames at compile time, so
200 * calculate things at run time.
201 */
202static int
203sysctl_intrnames(SYSCTL_HANDLER_ARGS)
204{
205 return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
206 req));
207}
208
209SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
210 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
211
212static int
213sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
214{
215 return (sysctl_handle_opaque(oidp, intrcnt,
216 (char *)eintrcnt - (char *)intrcnt, req));
217}
218
219SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
220 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");