xref: /freebsd/sys/kern/kern_intr.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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  * $Id: kern_intr.c,v 1.21 1998/11/10 09:16:29 peter Exp $
27  *
28  */
29 
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/errno.h>
35 
36 #include <machine/ipl.h>
37 
38 #include <sys/interrupt.h>
39 
40 struct swilist {
41 	swihand_t	*sl_handler;
42 	struct swilist	*sl_next;
43 };
44 
45 static struct swilist swilists[NSWI];
46 
47 void
48 register_swi(intr, handler)
49 	int intr;
50 	swihand_t *handler;
51 {
52 	struct swilist *slp, *slq;
53 	int s;
54 
55 	if (intr < NHWI || intr >= NHWI + NSWI)
56 		panic("register_swi: bad intr %d", intr);
57 	if (handler == swi_generic || handler == swi_null)
58 		panic("register_swi: bad handler %p", (void *)handler);
59 	slp = &swilists[intr - NHWI];
60 	s = splhigh();
61 	if (ihandlers[intr] == swi_null)
62 		ihandlers[intr] = handler;
63 	else {
64 		if (slp->sl_next == NULL) {
65 			slp->sl_handler = ihandlers[intr];
66 			ihandlers[intr] = swi_generic;
67 		}
68 		slq = malloc(sizeof(*slq), M_DEVBUF, M_NOWAIT);
69 		if (slq == NULL)
70 			panic("register_swi: malloc failed");
71 		slq->sl_handler = handler;
72 		slq->sl_next = NULL;
73 		while (slp->sl_next != NULL)
74 			slp = slp->sl_next;
75 		slp->sl_next = slq;
76 	}
77 	splx(s);
78 }
79 
80 void
81 swi_dispatcher(intr)
82 	int intr;
83 {
84 	struct swilist *slp;
85 
86 	slp = &swilists[intr - NHWI];
87 	do {
88 		(*slp->sl_handler)();
89 		slp = slp->sl_next;
90 	} while (slp != NULL);
91 }
92 
93 void
94 unregister_swi(intr, handler)
95 	int intr;
96 	swihand_t *handler;
97 {
98 	struct swilist *slfoundpred, *slp, *slq;
99 	int s;
100 
101 	if (intr < NHWI || intr >= NHWI + NSWI)
102 		panic("unregister_swi: bad intr %d", intr);
103 	if (handler == swi_generic || handler == swi_null)
104 		panic("unregister_swi: bad handler %p", (void *)handler);
105 	slp = &swilists[intr - NHWI];
106 	s = splhigh();
107 	if (ihandlers[intr] == handler)
108 		ihandlers[intr] = swi_null;
109 	else if (slp->sl_next != NULL) {
110 		slfoundpred = NULL;
111 		for (slq = slp->sl_next; slq != NULL;
112 		    slp = slq, slq = slp->sl_next)
113 			if (slq->sl_handler == handler)
114 				slfoundpred = slp;
115 		slp = &swilists[intr - NHWI];
116 		if (slfoundpred != NULL) {
117 			slq = slfoundpred->sl_next;
118 			slfoundpred->sl_next = slq->sl_next;
119 			free(slq, M_DEVBUF);
120 		} else if (slp->sl_handler == handler) {
121 			slq = slp->sl_next;
122 			slp->sl_next = slq->sl_next;
123 			slp->sl_handler = slq->sl_handler;
124 			free(slq, M_DEVBUF);
125 		}
126 		if (slp->sl_next == NULL)
127 			ihandlers[intr] = slp->sl_handler;
128 	}
129 	splx(s);
130 }
131 
132