shared_intr.c revision 1.14 1 /* $NetBSD: shared_intr.c,v 1.14 2000/06/04 05:27:45 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Authors: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 /*
31 * Common shared-interrupt-line functionality.
32 */
33
34 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
35
36 __KERNEL_RCSID(0, "$NetBSD: shared_intr.c,v 1.14 2000/06/04 05:27:45 thorpej Exp $");
37
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/syslog.h>
43 #include <sys/queue.h>
44
45 #include <machine/intr.h>
46
47 static const char *intr_typename __P((int));
48
49 static const char *
50 intr_typename(int type)
51 {
52
53 switch (type) {
54 case IST_UNUSABLE:
55 return ("disabled");
56 case IST_NONE:
57 return ("none");
58 case IST_PULSE:
59 return ("pulsed");
60 case IST_EDGE:
61 return ("edge-triggered");
62 case IST_LEVEL:
63 return ("level-triggered");
64 }
65 panic("intr_typename: unknown type %d", type);
66 }
67
68 struct alpha_shared_intr *
69 alpha_shared_intr_alloc(unsigned int n)
70 {
71 struct alpha_shared_intr *intr;
72 unsigned int i;
73
74 intr = malloc(n * sizeof (struct alpha_shared_intr), M_DEVBUF,
75 cold ? M_NOWAIT : M_WAITOK);
76 if (intr == NULL)
77 panic("alpha_shared_intr_alloc: couldn't malloc intr");
78
79 for (i = 0; i < n; i++) {
80 TAILQ_INIT(&intr[i].intr_q);
81 intr[i].intr_sharetype = IST_NONE;
82 intr[i].intr_dfltsharetype = IST_NONE;
83 intr[i].intr_nstrays = 0;
84 intr[i].intr_maxstrays = 5;
85 intr[i].intr_private = NULL;
86 }
87
88 return (intr);
89 }
90
91 int
92 alpha_shared_intr_dispatch(struct alpha_shared_intr *intr, unsigned int num)
93 {
94 struct alpha_shared_intrhand *ih;
95 int rv, handled;
96
97 ih = intr[num].intr_q.tqh_first;
98 handled = 0;
99 while (ih != NULL) {
100
101 /*
102 * The handler returns one of three values:
103 * 0: This interrupt wasn't for me.
104 * 1: This interrupt was for me.
105 * -1: This interrupt might have been for me, but I can't say
106 * for sure.
107 */
108 rv = (*ih->ih_fn)(ih->ih_arg);
109
110 handled = handled || (rv != 0);
111 ih = ih->ih_q.tqe_next;
112 }
113
114 return (handled);
115 }
116
117 void *
118 alpha_shared_intr_establish(struct alpha_shared_intr *intr, unsigned int num,
119 int type, int level, int (*fn)(void *), void *arg, const char *basename)
120 {
121 struct alpha_shared_intrhand *ih;
122
123 if (intr[num].intr_sharetype == IST_UNUSABLE) {
124 printf("alpha_shared_intr_establish: %s %d: unusable\n",
125 basename, num);
126 return NULL;
127 }
128
129 /* no point in sleeping unless someone can free memory. */
130 ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
131 if (ih == NULL)
132 panic("alpha_shared_intr_establish: can't malloc intrhand");
133
134 #ifdef DIAGNOSTIC
135 if (type == IST_NONE)
136 panic("alpha_shared_intr_establish: bogus type");
137 #endif
138
139 switch (intr[num].intr_sharetype) {
140 case IST_EDGE:
141 case IST_LEVEL:
142 if (type == intr[num].intr_sharetype)
143 break;
144 case IST_PULSE:
145 if (type != IST_NONE) {
146 if (intr[num].intr_q.tqh_first == NULL) {
147 printf("alpha_shared_intr_establish: %s %d: warning: using %s on %s\n",
148 basename, num, intr_typename(type),
149 intr_typename(intr[num].intr_sharetype));
150 type = intr[num].intr_sharetype;
151 } else {
152 panic("alpha_shared_intr_establish: %s %d: can't share %s with %s",
153 basename, num, intr_typename(type),
154 intr_typename(intr[num].intr_sharetype));
155 }
156 }
157 break;
158
159 case IST_NONE:
160 /* not currently used; safe */
161 break;
162 }
163
164 ih->ih_intrhead = intr;
165 ih->ih_fn = fn;
166 ih->ih_arg = arg;
167 ih->ih_level = level;
168 ih->ih_num = num;
169
170 intr[num].intr_sharetype = type;
171 TAILQ_INSERT_TAIL(&intr[num].intr_q, ih, ih_q);
172
173 return (ih);
174 }
175
176 void
177 alpha_shared_intr_disestablish(struct alpha_shared_intr *intr, void *cookie,
178 const char *basename)
179 {
180 struct alpha_shared_intrhand *ih = cookie;
181 unsigned int num = ih->ih_num;
182
183 /*
184 * Just remove it from the list and free the entry. We let
185 * the caller deal with resetting the share type, if appropriate.
186 */
187 TAILQ_REMOVE(&intr[num].intr_q, ih, ih_q);
188 }
189
190 int
191 alpha_shared_intr_get_sharetype(struct alpha_shared_intr *intr,
192 unsigned int num)
193 {
194
195 return (intr[num].intr_sharetype);
196 }
197
198 int
199 alpha_shared_intr_isactive(struct alpha_shared_intr *intr, unsigned int num)
200 {
201
202 return (intr[num].intr_q.tqh_first != NULL);
203 }
204
205 void
206 alpha_shared_intr_set_dfltsharetype(struct alpha_shared_intr *intr,
207 unsigned int num, int newdfltsharetype)
208 {
209
210 #ifdef DIAGNOSTIC
211 if (alpha_shared_intr_isactive(intr, num))
212 panic("alpha_shared_intr_set_dfltsharetype on active intr");
213 #endif
214
215 intr[num].intr_dfltsharetype = newdfltsharetype;
216 intr[num].intr_sharetype = intr[num].intr_dfltsharetype;
217 }
218
219 void
220 alpha_shared_intr_set_maxstrays(struct alpha_shared_intr *intr,
221 unsigned int num, int newmaxstrays)
222 {
223 int s = splhigh();
224 intr[num].intr_maxstrays = newmaxstrays;
225 intr[num].intr_nstrays = 0;
226 splx(s);
227 }
228
229 void
230 alpha_shared_intr_stray(struct alpha_shared_intr *intr, unsigned int num,
231 const char *basename)
232 {
233
234 intr[num].intr_nstrays++;
235
236 if (intr[num].intr_maxstrays == 0)
237 return;
238
239 if (intr[num].intr_nstrays <= intr[num].intr_maxstrays)
240 log(LOG_ERR, "stray %s %d%s\n", basename, num,
241 intr[num].intr_nstrays >= intr[num].intr_maxstrays ?
242 "; stopped logging" : "");
243 }
244
245 void
246 alpha_shared_intr_set_private(struct alpha_shared_intr *intr,
247 unsigned int num, void *v)
248 {
249
250 intr[num].intr_private = v;
251 }
252
253 void *
254 alpha_shared_intr_get_private(struct alpha_shared_intr *intr,
255 unsigned int num)
256 {
257
258 return (intr[num].intr_private);
259 }
260