xref: /openbsd/sys/arch/i386/i386/softintr.c (revision 09467b48)
1 /*	$OpenBSD: softintr.c,v 1.7 2015/09/01 06:01:26 deraadt Exp $	*/
2 /*	$NetBSD: softintr.c,v 1.1 2003/02/26 21:26:12 fvdl Exp $	*/
3 
4 /*-
5  * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Generic soft interrupt implementation for NetBSD/x86.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/malloc.h>
39 
40 #include <machine/intr.h>
41 
42 #include <uvm/uvm_extern.h>
43 
44 struct i386_soft_intr i386_soft_intrs[I386_NSOFTINTR];
45 
46 const int i386_soft_intr_to_ssir[I386_NSOFTINTR] = {
47 	SIR_CLOCK,
48 	SIR_NET,
49 	SIR_TTY,
50 };
51 
52 /*
53  * softintr_init:
54  *
55  *	Initialize the software interrupt system.
56  */
57 void
58 softintr_init(void)
59 {
60 	struct i386_soft_intr *si;
61 	int i;
62 
63 	for (i = 0; i < I386_NSOFTINTR; i++) {
64 		si = &i386_soft_intrs[i];
65 		TAILQ_INIT(&si->softintr_q);
66 		mtx_init(&si->softintr_lock, IPL_HIGH);
67 		si->softintr_ssir = i386_soft_intr_to_ssir[i];
68 	}
69 }
70 
71 /*
72  * softintr_dispatch:
73  *
74  *	Process pending software interrupts.
75  */
76 void
77 softintr_dispatch(int which)
78 {
79 	struct i386_soft_intr *si = &i386_soft_intrs[which];
80 	struct i386_soft_intrhand *sih;
81 
82 	KERNEL_LOCK();
83 	for (;;) {
84 		mtx_enter(&si->softintr_lock);
85 		sih = TAILQ_FIRST(&si->softintr_q);
86 		if (sih == NULL) {
87 			mtx_leave(&si->softintr_lock);
88 			break;
89 		}
90 		TAILQ_REMOVE(&si->softintr_q, sih, sih_q);
91 		sih->sih_pending = 0;
92 
93 		uvmexp.softs++;
94 
95 		mtx_leave(&si->softintr_lock);
96 
97 		(*sih->sih_fn)(sih->sih_arg);
98 	}
99 	KERNEL_UNLOCK();
100 }
101 
102 /*
103  * softintr_establish:		[interface]
104  *
105  *	Register a software interrupt handler.
106  */
107 void *
108 softintr_establish(int ipl, void (*func)(void *), void *arg)
109 {
110 	struct i386_soft_intr *si;
111 	struct i386_soft_intrhand *sih;
112 	int which;
113 
114 	switch (ipl) {
115 	case IPL_SOFTCLOCK:
116 		which = I386_SOFTINTR_SOFTCLOCK;
117 		break;
118 
119 	case IPL_SOFTNET:
120 		which = I386_SOFTINTR_SOFTNET;
121 		break;
122 
123 	case IPL_TTY:
124 	case IPL_SOFTTTY:
125 		which = I386_SOFTINTR_SOFTTTY;
126 		break;
127 
128 	default:
129 		panic("softintr_establish");
130 	}
131 
132 	si = &i386_soft_intrs[which];
133 
134 	sih = malloc(sizeof(*sih), M_DEVBUF, M_NOWAIT);
135 	if (__predict_true(sih != NULL)) {
136 		sih->sih_intrhead = si;
137 		sih->sih_fn = func;
138 		sih->sih_arg = arg;
139 		sih->sih_pending = 0;
140 	}
141 	return (sih);
142 }
143 
144 /*
145  * softintr_disestablish:	[interface]
146  *
147  *	Unregister a software interrupt handler.
148  */
149 void
150 softintr_disestablish(void *arg)
151 {
152 	struct i386_soft_intrhand *sih = arg;
153 	struct i386_soft_intr *si = sih->sih_intrhead;
154 
155 	mtx_enter(&si->softintr_lock);
156 	if (sih->sih_pending) {
157 		TAILQ_REMOVE(&si->softintr_q, sih, sih_q);
158 		sih->sih_pending = 0;
159 	}
160 	mtx_leave(&si->softintr_lock);
161 
162 	free(sih, M_DEVBUF, sizeof(*sih));
163 }
164