shared_intr.c revision 1.2 1 /* $NetBSD: shared_intr.c,v 1.2 1997/04/06 22:55:59 cgd 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 <machine/options.h> /* Pull in config options headers */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/syslog.h>
40 #include <sys/queue.h>
41
42 #include <machine/intr.h>
43
44 extern int cold;
45
46 static const char *intr_typename __P((int));
47
48 static const char *
49 intr_typename(type)
50 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(n)
70 unsigned int n;
71 {
72 struct alpha_shared_intr *intr;
73 unsigned int i;
74
75 intr = malloc(n * sizeof (struct alpha_shared_intr), M_DEVBUF,
76 cold ? M_NOWAIT : M_WAITOK);
77 if (intr == NULL)
78 panic("alpha_shared_intr_alloc: couldn't malloc intr");
79
80 for (i = 0; i < n; i++) {
81 TAILQ_INIT(&intr[i].intr_q);
82 intr[i].intr_sharetype = IST_NONE;
83 intr[i].intr_dfltsharetype = IST_NONE;
84 intr[i].intr_nstrays = 0;
85 intr[i].intr_maxstrays = 5;
86 }
87
88 return (intr);
89 }
90
91 int
92 alpha_shared_intr_dispatch(intr, num)
93 struct alpha_shared_intr *intr;
94 unsigned int num;
95 {
96 struct alpha_shared_intrhand *ih;
97 int rv, handled;
98
99 ih = intr[num].intr_q.tqh_first;
100 handled = 0;
101 while (ih != NULL) {
102
103 /*
104 * The handler returns one of three values:
105 * 0: This interrupt wasn't for me.
106 * 1: This interrupt was for me.
107 * -1: This interrupt might have been for me, but I can't say
108 * for sure.
109 */
110 rv = (*ih->ih_fn)(ih->ih_arg);
111
112 handled = handled || (rv != 0);
113 ih = ih->ih_q.tqe_next;
114 }
115
116 return (handled);
117 }
118
119 void *
120 alpha_shared_intr_establish(intr, num, type, level, fn, arg, basename)
121 struct alpha_shared_intr *intr;
122 unsigned int num;
123 int type, level;
124 int (*fn) __P((void *));
125 void *arg;
126 const char *basename;
127 {
128 struct alpha_shared_intrhand *ih;
129
130 if (intr[num].intr_sharetype == IST_UNUSABLE) {
131 printf("alpha_shared_intr_establish: %s %d: unusable\n",
132 basename, num);
133 return NULL;
134 }
135
136 /* no point in sleeping unless someone can free memory. */
137 ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
138 if (ih == NULL)
139 panic("alpha_shared_intr_establish: can't malloc intrhand");
140
141 #ifdef DIAGNOSTIC
142 if (type == IST_NONE)
143 panic("alpha_shared_intr_establish: bogus type");
144 #endif
145
146 switch (intr[num].intr_sharetype) {
147 case IST_EDGE:
148 case IST_LEVEL:
149 if (type == intr[num].intr_sharetype)
150 break;
151 case IST_PULSE:
152 if (type != IST_NONE) {
153 if (intr[num].intr_q.tqh_first == NULL) {
154 printf("alpha_shared_intr_establish: %s %d: warning: using %s on %s\n",
155 basename, num, intr_typename(type),
156 intr_typename(intr[num].intr_sharetype));
157 type = intr[num].intr_sharetype;
158 } else {
159 panic("alpha_shared_intr_establish: %s %d: can't share %s with %s",
160 basename, num, intr_typename(type),
161 intr_typename(intr[num].intr_sharetype));
162 }
163 }
164 break;
165
166 case IST_NONE:
167 /* not currently used; safe */
168 break;
169 }
170
171 ih->ih_fn = fn;
172 ih->ih_arg = arg;
173 ih->ih_level = level;
174
175 intr[num].intr_sharetype = type;
176 TAILQ_INSERT_TAIL(&intr[num].intr_q, ih, ih_q);
177
178 return (ih);
179 }
180
181 int
182 alpha_shared_intr_get_sharetype(intr, num)
183 struct alpha_shared_intr *intr;
184 unsigned int num;
185 {
186
187 return (intr[num].intr_sharetype);
188 }
189
190 int
191 alpha_shared_intr_isactive(intr, num)
192 struct alpha_shared_intr *intr;
193 unsigned int num;
194 {
195
196 return (intr[num].intr_q.tqh_first != NULL);
197 }
198
199 void
200 alpha_shared_intr_set_dfltsharetype(intr, num, newdfltsharetype)
201 struct alpha_shared_intr *intr;
202 unsigned int num;
203 int newdfltsharetype;
204 {
205
206 #ifdef DIAGNOSTIC
207 if (alpha_shared_intr_isactive(intr, num))
208 panic("alpha_shared_intr_set_dfltsharetype on active intr");
209 #endif
210
211 intr[num].intr_dfltsharetype = newdfltsharetype;
212 intr[num].intr_sharetype = intr[num].intr_dfltsharetype;
213 }
214
215 void
216 alpha_shared_intr_set_maxstrays(intr, num, newmaxstrays)
217 struct alpha_shared_intr *intr;
218 unsigned int num;
219 int newmaxstrays;
220 {
221
222 #ifdef DIAGNOSTIC
223 if (alpha_shared_intr_isactive(intr, num))
224 panic("alpha_shared_intr_set_maxstrays on active intr");
225 #endif
226
227 intr[num].intr_maxstrays = newmaxstrays;
228 intr[num].intr_nstrays = 0;
229 }
230
231 void
232 alpha_shared_intr_stray(intr, num, basename)
233 struct alpha_shared_intr *intr;
234 unsigned int num;
235 const char *basename;
236 {
237
238 intr[num].intr_nstrays++;
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