xref: /openbsd-src/sys/arch/arm/arm/softintr.c (revision a4a50d9633ad5d32f291ac2e2a431b18d1120b73)
1 /*	$OpenBSD: softintr.c,v 1.11 2021/03/25 04:12:00 jsg Exp $	*/
2 /*	$NetBSD: softintr.c,v 1.2 2003/07/15 00:24:39 lukem Exp $	*/
3 
4 /*
5  * Copyright (c) 2001 Wasabi Systems, Inc.
6  * All rights reserved.
7  *
8  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed for the NetBSD Project by
21  *	Wasabi Systems, Inc.
22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 
43 #include <uvm/uvm_extern.h>
44 
45 #include <machine/intr.h>
46 
47 struct soft_intrq soft_intrq[SI_NQUEUES];
48 
49 /*
50  * softintr_init:
51  *
52  *	Initialize the software interrupt system.
53  */
54 void
softintr_init(void)55 softintr_init(void)
56 {
57 	struct soft_intrq *siq;
58 	int i;
59 
60 	for (i = 0; i < SI_NQUEUES; i++) {
61 		siq = &soft_intrq[i];
62 		TAILQ_INIT(&siq->siq_list);
63 		mtx_init(&siq->siq_mtx, IPL_HIGH);
64 		siq->siq_si = i;
65 	}
66 }
67 
68 /*
69  * softintr_dispatch:
70  *
71  *	Process pending software interrupts on the specified queue.
72  *
73  *	NOTE: We must already be at the correct interrupt priority level.
74  */
75 void
softintr_dispatch(int si)76 softintr_dispatch(int si)
77 {
78 	struct soft_intrq *siq = &soft_intrq[si];
79 	struct soft_intrhand *sih;
80 
81 	for (;;) {
82 		mtx_enter(&siq->siq_mtx);
83 		sih = TAILQ_FIRST(&siq->siq_list);
84 		if (sih == NULL) {
85 			mtx_leave(&siq->siq_mtx);
86 			break;
87 		}
88 
89 		TAILQ_REMOVE(&siq->siq_list, sih, sih_list);
90 		sih->sih_pending = 0;
91 
92 		uvmexp.softs++;
93 		mtx_leave(&siq->siq_mtx);
94 
95 		KERNEL_LOCK();
96 		(*sih->sih_func)(sih->sih_arg);
97 		KERNEL_UNLOCK();
98 	}
99 }
100 
101 /*
102  * softintr_establish:		[interface]
103  *
104  *	Register a software interrupt handler.
105  */
106 void *
softintr_establish(int ipl,void (* func)(void *),void * arg)107 softintr_establish(int ipl, void (*func)(void *), void *arg)
108 {
109 	struct soft_intrhand *sih;
110 	int si;
111 
112 	switch (ipl) {
113 	case IPL_SOFT:
114 		si = SI_SOFT;
115 		break;
116 
117 	case IPL_SOFTCLOCK:
118 		si = SI_SOFTCLOCK;
119 		break;
120 
121 	case IPL_SOFTNET:
122 		si = SI_SOFTNET;
123 		break;
124 
125 	case IPL_TTY:
126 	case IPL_SOFTTTY:
127 		si = SI_SOFTTTY;
128 		break;
129 
130 	default:
131 		panic("softintr_establish: unknown soft IPL %d", ipl);
132 	}
133 
134 	sih = malloc(sizeof(*sih), M_DEVBUF, M_NOWAIT);
135 	if (__predict_true(sih != NULL)) {
136 		sih->sih_func = func;
137 		sih->sih_arg = arg;
138 		sih->sih_siq = &soft_intrq[si];
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
softintr_disestablish(void * arg)150 softintr_disestablish(void *arg)
151 {
152 	struct soft_intrhand *sih = arg;
153 	struct soft_intrq *siq = sih->sih_siq;
154 
155 	mtx_enter(&siq->siq_mtx);
156 	if (sih->sih_pending) {
157 		TAILQ_REMOVE(&siq->siq_list, sih, sih_list);
158 		sih->sih_pending = 0;
159 	}
160 	mtx_leave(&siq->siq_mtx);
161 
162 	free(sih, M_DEVBUF, 0);
163 }
164