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