Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / kern / kern_intr.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice 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
30
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>
36
37#include <machine/ipl.h>
38
39#include <sys/interrupt.h>
40
41struct swilist {
42 swihand_t *sl_handler;
43 struct swilist *sl_next;
44};
45
46static struct swilist swilists[NSWI];
47
48void
49register_swi(intr, handler)
50 int intr;
51 swihand_t *handler;
52{
53 struct swilist *slp, *slq;
54 int s;
55
56 if (intr < NHWI || intr >= NHWI + NSWI)
57 panic("register_swi: bad intr %d", intr);
58 if (handler == swi_generic || handler == swi_null)
59 panic("register_swi: bad handler %p", (void *)handler);
60 slp = &swilists[intr - NHWI];
61 s = splhigh();
62 if (ihandlers[intr] == swi_null)
63 ihandlers[intr] = handler;
64 else {
65 if (slp->sl_next == NULL) {
66 slp->sl_handler = ihandlers[intr];
67 ihandlers[intr] = swi_generic;
68 }
69 slq = malloc(sizeof(*slq), M_DEVBUF, M_NOWAIT);
70 if (slq == NULL)
71 panic("register_swi: malloc failed");
72 slq->sl_handler = handler;
73 slq->sl_next = NULL;
74 while (slp->sl_next != NULL)
75 slp = slp->sl_next;
76 slp->sl_next = slq;
77 }
78 splx(s);
79}
80
81void
82swi_dispatcher(intr)
83 int intr;
84{
85 struct swilist *slp;
86
87 slp = &swilists[intr - NHWI];
88 do {
89 (*slp->sl_handler)();
90 slp = slp->sl_next;
91 } while (slp != NULL);
92}
93
94void
95unregister_swi(intr, handler)
96 int intr;
97 swihand_t *handler;
98{
99 struct swilist *slfoundpred, *slp, *slq;
100 int s;
101
102 if (intr < NHWI || intr >= NHWI + NSWI)
103 panic("unregister_swi: bad intr %d", intr);
104 if (handler == swi_generic || handler == swi_null)
105 panic("unregister_swi: bad handler %p", (void *)handler);
106 slp = &swilists[intr - NHWI];
107 s = splhigh();
108 if (ihandlers[intr] == handler)
109 ihandlers[intr] = swi_null;
110 else if (slp->sl_next != NULL) {
111 slfoundpred = NULL;
112 for (slq = slp->sl_next; slq != NULL;
113 slp = slq, slq = slp->sl_next)
114 if (slq->sl_handler == handler)
115 slfoundpred = slp;
116 slp = &swilists[intr - NHWI];
117 if (slfoundpred != NULL) {
118 slq = slfoundpred->sl_next;
119 slfoundpred->sl_next = slq->sl_next;
120 free(slq, M_DEVBUF);
121 } else if (slp->sl_handler == handler) {
122 slq = slp->sl_next;
123 slp->sl_next = slq->sl_next;
124 slp->sl_handler = slq->sl_handler;
125 free(slq, M_DEVBUF);
126 }
127 if (slp->sl_next == NULL)
128 ihandlers[intr] = slp->sl_handler;
129 }
130 splx(s);
131}
132
133/*
134 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
135 * The data for this machine dependent, and the declarations are in machine
136 * dependent code. The layout of intrnames and intrcnt however is machine
137 * independent.
138 *
139 * We do not know the length of intrcnt and intrnames at compile time, so
140 * calculate things at run time.
141 */
142static int
143sysctl_intrnames(SYSCTL_HANDLER_ARGS)
144{
145 return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
146 req));
147}
148
149SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
150 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
151
152static int
153sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
154{
155 return (sysctl_handle_opaque(oidp, intrcnt,
156 (char *)eintrcnt - (char *)intrcnt, req));
157}
158
159SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
160 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");