intr_machdep.c revision 127977
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32/*-
33 * Copyright (c) 2001 Jake Burkholder.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
58 *	form: src/sys/i386/isa/intr_machdep.c,v 1.57 2001/07/20
59 *
60 * $FreeBSD: head/sys/sparc64/sparc64/intr_machdep.c 127977 2004-04-07 05:00:01Z imp $
61 */
62
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/queue.h>
66#include <sys/bus.h>
67#include <sys/interrupt.h>
68#include <sys/lock.h>
69#include <sys/mutex.h>
70#include <sys/pcpu.h>
71#include <sys/vmmeter.h>
72
73#include <machine/frame.h>
74#include <machine/intr_machdep.h>
75
76#define	MAX_STRAY_LOG	5
77
78CTASSERT((1 << IV_SHIFT) == sizeof(struct intr_vector));
79
80ih_func_t *intr_handlers[PIL_MAX];
81u_int16_t	pil_countp[PIL_MAX];
82
83struct	intr_vector intr_vectors[IV_MAX];
84u_long	intr_stray_count[IV_MAX];
85u_int16_t	intr_countp[IV_MAX];
86
87char *pil_names[] = {
88	"stray",
89	"low",		/* PIL_LOW */
90	"ithrd",	/* PIL_ITHREAD */
91	"rndzvs",	/* PIL_RENDEZVOUS */
92	"ast",		/* PIL_AST */
93	"stop",		/* PIL_STOP */
94	"stray", "stray", "stray", "stray", "stray", "stray", "stray",
95	"fast",		/* PIL_FAST */
96	"tick",		/* PIL_TICK */
97};
98
99/* protect the intr_vectors table */
100static struct	mtx intr_table_lock;
101
102static void intr_stray_level(struct trapframe *tf);
103static void intr_stray_vector(void *cookie);
104
105/*
106 * not MPSAFE
107 */
108static void
109update_intrname(int vec, const char *name, int ispil)
110{
111	char buf[32];
112	char *cp;
113	int off, name_index;
114
115	if (intrnames[0] == '\0') {
116		/* for bitbucket */
117		if (bootverbose)
118			printf("initalizing intr_countp\n");
119		off = sprintf(intrnames, "???") + 1;
120
121		off += sprintf(intrnames + off, "stray") + 1;
122		for (name_index = 0; name_index < IV_MAX; name_index++)
123			intr_countp[name_index] = 1;
124
125		off += sprintf(intrnames + off, "pil") + 1;
126		for (name_index = 0; name_index < PIL_MAX; name_index++)
127			pil_countp[name_index] = 2;
128	}
129
130	if (name == NULL)
131		name = "???";
132
133	if (snprintf(buf, sizeof(buf), "%s %s%d", name, ispil ? "pil" : "vec",
134	    vec) >= sizeof(buf))
135		goto use_bitbucket;
136
137	/*
138	 * Search for `buf' in `intrnames'.  In the usual case when it is
139	 * not found, append it to the end if there is enough space (the \0
140	 * terminator for the previous string, if any, becomes a separator).
141	 */
142	for (cp = intrnames, name_index = 0; cp != eintrnames &&
143	    name_index < IV_MAX; cp += strlen(cp) + 1, name_index++) {
144		if (*cp == '\0') {
145			if (strlen(buf) >= eintrnames - cp)
146				break;
147			strcpy(cp, buf);
148			goto found;
149		}
150		if (strcmp(cp, buf) == 0)
151			goto found;
152	}
153
154use_bitbucket:
155	name_index = 0;
156found:
157	if (!ispil)
158		intr_countp[vec] = name_index;
159	else
160		pil_countp[vec] = name_index;
161}
162
163void
164intr_setup(int pri, ih_func_t *ihf, int vec, iv_func_t *ivf, void *iva)
165{
166	u_long ps;
167
168	ps = intr_disable();
169	if (vec != -1) {
170		intr_vectors[vec].iv_func = ivf;
171		intr_vectors[vec].iv_arg = iva;
172		intr_vectors[vec].iv_pri = pri;
173		intr_vectors[vec].iv_vec = vec;
174	}
175	update_intrname(pri, pil_names[pri], 1);
176	intr_handlers[pri] = ihf;
177	intr_restore(ps);
178}
179
180static void
181intr_stray_level(struct trapframe *tf)
182{
183	printf("stray level interrupt %ld\n", tf->tf_level);
184}
185
186static void
187intr_stray_vector(void *cookie)
188{
189	struct intr_vector *iv;
190
191	iv = cookie;
192	if (intr_stray_count[iv->iv_vec] < MAX_STRAY_LOG) {
193		printf("stray vector interrupt %d\n", iv->iv_vec);
194		atomic_add_long(&intr_stray_count[iv->iv_vec], 1);
195		if (intr_stray_count[iv->iv_vec] >= MAX_STRAY_LOG)
196			printf("got %d stray interrupt %d's: not logging "
197			    "anymore\n", MAX_STRAY_LOG, iv->iv_vec);
198	}
199}
200
201void
202intr_init1()
203{
204	int i;
205
206	/* Mark all interrupts as being stray. */
207	for (i = 0; i < PIL_MAX; i++)
208		intr_handlers[i] = intr_stray_level;
209	for (i = 0; i < IV_MAX; i++) {
210		intr_vectors[i].iv_func = intr_stray_vector;
211		intr_vectors[i].iv_arg = &intr_vectors[i];
212		intr_vectors[i].iv_pri = PIL_LOW;
213		intr_vectors[i].iv_vec = i;
214	}
215	intr_handlers[PIL_LOW] = intr_fast;
216}
217
218void
219intr_init2()
220{
221
222	mtx_init(&intr_table_lock, "ithread table lock", NULL, MTX_SPIN);
223}
224
225/* Schedule a heavyweight interrupt process. */
226static void
227sched_ithd(void *cookie)
228{
229	struct intr_vector *iv;
230	int error;
231
232	iv = cookie;
233#ifdef notyet
234	error = ithread_schedule(iv->iv_ithd);
235#else
236	error = ithread_schedule(iv->iv_ithd, 0);
237#endif
238	if (error == EINVAL)
239		intr_stray_vector(iv);
240}
241
242int
243inthand_add(const char *name, int vec, void (*handler)(void *), void *arg,
244    int flags, void **cookiep)
245{
246	struct intr_vector *iv;
247	struct ithd *ithd;		/* descriptor for the IRQ */
248	int errcode = 0;
249	int created_ithd = 0;
250
251	/*
252	 * Work around a race where more than one CPU may be registering
253	 * handlers on the same IRQ at the same time.
254	 */
255	iv = &intr_vectors[vec];
256	mtx_lock_spin(&intr_table_lock);
257	ithd = iv->iv_ithd;
258	mtx_unlock_spin(&intr_table_lock);
259	if (ithd == NULL) {
260		errcode = ithread_create(&ithd, vec, 0, NULL, NULL, "intr%d:",
261		    vec);
262		if (errcode)
263			return (errcode);
264		mtx_lock_spin(&intr_table_lock);
265		if (iv->iv_ithd == NULL) {
266			iv->iv_ithd = ithd;
267			created_ithd++;
268			mtx_unlock_spin(&intr_table_lock);
269		} else {
270			struct ithd *orphan;
271
272			orphan = ithd;
273			ithd = iv->iv_ithd;
274			mtx_unlock_spin(&intr_table_lock);
275			ithread_destroy(orphan);
276		}
277	}
278
279	errcode = ithread_add_handler(ithd, name, handler, arg,
280	    ithread_priority(flags), flags, cookiep);
281
282	if ((flags & INTR_FAST) == 0 || errcode) {
283		intr_setup(PIL_ITHREAD, intr_fast, vec, sched_ithd, iv);
284		errcode = 0;
285	}
286
287	if (errcode)
288		return (errcode);
289
290	if (flags & INTR_FAST)
291		intr_setup(PIL_FAST, intr_fast, vec, handler, arg);
292
293	intr_stray_count[vec] = 0;
294
295	update_intrname(vec, name, 0);
296
297	return (0);
298}
299
300int
301inthand_remove(int vec, void *cookie)
302{
303	struct intr_vector *iv;
304	int error;
305
306	error = ithread_remove_handler(cookie);
307	if (error == 0) {
308		/*
309		 * XXX: maybe this should be done regardless of whether
310		 * ithread_remove_handler() succeeded?
311		 */
312		iv = &intr_vectors[vec];
313		mtx_lock_spin(&intr_table_lock);
314		if (iv->iv_ithd == NULL) {
315			intr_setup(PIL_ITHREAD, intr_fast, vec,
316			    intr_stray_vector, iv);
317		} else {
318			intr_setup(PIL_LOW, intr_fast, vec, sched_ithd, iv);
319		}
320		mtx_unlock_spin(&intr_table_lock);
321	}
322	return (error);
323}
324