kern_softint.c revision 1.1.2.2 1 1.1.2.2 ad /* $NetBSD: kern_softint.c,v 1.1.2.2 2007/07/01 21:31:32 ad Exp $ */
2 1.1.2.1 ad
3 1.1.2.1 ad /*-
4 1.1.2.1 ad * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.1.2.1 ad * All rights reserved.
6 1.1.2.1 ad *
7 1.1.2.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 ad * by Andrew Doran.
9 1.1.2.1 ad *
10 1.1.2.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 ad * modification, are permitted provided that the following conditions
12 1.1.2.1 ad * are met:
13 1.1.2.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 ad * documentation and/or other materials provided with the distribution.
18 1.1.2.1 ad * 3. All advertising materials mentioning features or use of this software
19 1.1.2.1 ad * must display the following acknowledgement:
20 1.1.2.1 ad * This product includes software developed by the NetBSD
21 1.1.2.1 ad * Foundation, Inc. and its contributors.
22 1.1.2.1 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.1 ad * contributors may be used to endorse or promote products derived
24 1.1.2.1 ad * from this software without specific prior written permission.
25 1.1.2.1 ad *
26 1.1.2.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.1 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.1 ad */
38 1.1.2.1 ad
39 1.1.2.1 ad /*
40 1.1.2.2 ad * Generic software interrupt framework.
41 1.1.2.2 ad *
42 1.1.2.2 ad * Overview
43 1.1.2.2 ad *
44 1.1.2.2 ad * The soft interrupt framework provides a mechanism to schedule a
45 1.1.2.2 ad * low priority callback that runs with thread context. It allows
46 1.1.2.2 ad * for dynamic registration of software interrupts, and for fair
47 1.1.2.2 ad * queueing and prioritization of those interrupts. The callbacks
48 1.1.2.2 ad * can be scheduled to run from nearly any point in the kernel: by
49 1.1.2.2 ad * code running with thread context, by code running from a
50 1.1.2.2 ad * hardware interrupt handler, and at any interrupt priority
51 1.1.2.2 ad * level.
52 1.1.2.2 ad *
53 1.1.2.2 ad * Priority levels
54 1.1.2.2 ad *
55 1.1.2.2 ad * Since soft interrupt dispatch can be tied to the underlying
56 1.1.2.2 ad * architecture's interrupt dispatch code, it may be limited by
57 1.1.2.2 ad * both by the capabilities of the hardware and the capabilities
58 1.1.2.2 ad * of the interrupt dispatch code itself. Therefore the number of
59 1.1.2.2 ad * levels is restricted to four. In order of priority (lowest to
60 1.1.2.2 ad * highest) the levels are: clock, bio, net, serial.
61 1.1.2.2 ad *
62 1.1.2.2 ad * The symbolic names are provided only as guide and in isolation
63 1.1.2.2 ad * do not have any direct connection with a particular kind of
64 1.1.2.2 ad * device activity.
65 1.1.2.2 ad *
66 1.1.2.2 ad * The four priority levels map directly to scheduler priority
67 1.1.2.2 ad * levels, and where the architecture implements 'fast' software
68 1.1.2.2 ad * interrupts, they also map onto interrupt priorities. The
69 1.1.2.2 ad * interrupt priorities are intended to be hidden from machine
70 1.1.2.2 ad * independent code, which should use multiprocessor and
71 1.1.2.2 ad * preemption aware mechanisms to synchronize with software
72 1.1.2.2 ad * interrupts (for example: mutexes).
73 1.1.2.2 ad *
74 1.1.2.2 ad * Capabilities
75 1.1.2.2 ad *
76 1.1.2.2 ad * As with hardware interrupt handlers, software interrupts run
77 1.1.2.2 ad * with limited machine context. In particular, they do not
78 1.1.2.2 ad * posess any VM (virtual memory) context, and should therefore
79 1.1.2.2 ad * not try to operate on user space addresses, or to use virtual
80 1.1.2.2 ad * memory facilities other than those noted as interrupt safe.
81 1.1.2.2 ad *
82 1.1.2.2 ad * Unlike hardware interrupts, software interrupts do have thread
83 1.1.2.2 ad * context. They may block on synchronization objects, sleep, and
84 1.1.2.2 ad * resume execution at a later time. Since software interrupts
85 1.1.2.2 ad * are a limited resource and (typically) run with higher priority
86 1.1.2.2 ad * than all other threads in the system, all block-and-resume
87 1.1.2.2 ad * activity by a software interrupt must be kept short in order to
88 1.1.2.2 ad * allow futher processing at that level to continue. The kernel
89 1.1.2.2 ad * does not allow software interrupts to use facilities or perform
90 1.1.2.2 ad * actions that may block for a significant amount of time. This
91 1.1.2.2 ad * means that it's not valid for a software interrupt to: sleep on
92 1.1.2.2 ad * condition variables, use the lockmgr() facility, or wait for
93 1.1.2.2 ad * resources to become available (for example, memory).
94 1.1.2.2 ad *
95 1.1.2.2 ad * Software interrupts may block to await ownership of locks,
96 1.1.2.2 ad * which are typically owned only for a short perioid of time:
97 1.1.2.2 ad * mutexes and reader/writer locks. By extension, code running in
98 1.1.2.2 ad * the bottom half of the kernel must take care to ensure that any
99 1.1.2.2 ad * lock that may be taken from a software interrupt can not be
100 1.1.2.2 ad * held for more than a short period of time.
101 1.1.2.2 ad *
102 1.1.2.2 ad * Per-CPU operation
103 1.1.2.2 ad *
104 1.1.2.2 ad * Soft interrupts are strictly per-CPU. If a soft interrupt is
105 1.1.2.2 ad * triggered on a CPU, it will only be dispatched on that CPU.
106 1.1.2.2 ad * Each LWP dedicated to handling a soft interrupt is bound to
107 1.1.2.2 ad * it's home CPU, so if the LWP blocks and needs to run again, it
108 1.1.2.2 ad * can only run there. Nearly all data structures used to manage
109 1.1.2.2 ad * software interrupts are per-CPU.
110 1.1.2.2 ad *
111 1.1.2.2 ad * Soft interrupts can occur many thousands of times per second.
112 1.1.2.2 ad * In light of this, the per-CPU requirement is intended to solve
113 1.1.2.2 ad * three problems:
114 1.1.2.2 ad *
115 1.1.2.2 ad * 1) For passing work down from a hardware interrupt handler to a
116 1.1.2.2 ad * software interrupt (for example, using a queue) spinlocks need
117 1.1.2.2 ad * not be used to guarantee data integrity. Adjusting the CPU
118 1.1.2.2 ad * local interrupt priority level is sufficient. Acquiring
119 1.1.2.2 ad * spinlocks is computationally expensive, as it increases traffic
120 1.1.2.2 ad * on the system bus and can stall processors with long execution
121 1.1.2.2 ad * pipelines.
122 1.1.2.2 ad *
123 1.1.2.2 ad * 2) Often hardware interrupt handlers manipulate data structures
124 1.1.2.2 ad * and then pass those to a software interrupt for further
125 1.1.2.2 ad * processing. If those data structures are immediately passed to
126 1.1.2.2 ad * another CPU, the associated cache lines may be forced across
127 1.1.2.2 ad * the system bus, generating more bus traffic.
128 1.1.2.2 ad *
129 1.1.2.2 ad * 3) The data structures used to manage soft interrupts are also
130 1.1.2.2 ad * CPU local, again to reduce unnecessary bus traffic.
131 1.1.2.2 ad *
132 1.1.2.2 ad * Generic implementation
133 1.1.2.2 ad *
134 1.1.2.2 ad * A generic, low performance implementation is provided that
135 1.1.2.2 ad * works across all architectures, with no machine-dependent
136 1.1.2.2 ad * modifications needed. This implementation uses the scheduler,
137 1.1.2.2 ad * and so has a number of restrictions:
138 1.1.2.2 ad *
139 1.1.2.2 ad * 1) Since software interrupts can be triggered from any priority
140 1.1.2.2 ad * level, on architectures where the generic implementation is
141 1.1.2.2 ad * used IPL_SCHED must be equal to IPL_HIGH.
142 1.1.2.2 ad *
143 1.1.2.2 ad * 2) The software interrupts are not preemptive, and so must wait
144 1.1.2.2 ad * for the currently executing thread to yield the CPU. This
145 1.1.2.2 ad * can introduce latency.
146 1.1.2.2 ad *
147 1.1.2.2 ad * 3) A context switch is required for each soft interrupt to be
148 1.1.2.2 ad * handled, which can be quite expensive.
149 1.1.2.2 ad *
150 1.1.2.2 ad * 'Fast' software interrupts
151 1.1.2.2 ad *
152 1.1.2.2 ad * XXX
153 1.1.2.1 ad *
154 1.1.2.1 ad * The !__HAVE_FAST_SOFTINTS case assumes splhigh == splsched.
155 1.1.2.1 ad */
156 1.1.2.1 ad
157 1.1.2.1 ad #include <sys/cdefs.h>
158 1.1.2.2 ad __KERNEL_RCSID(0, "$NetBSD: kern_softint.c,v 1.1.2.2 2007/07/01 21:31:32 ad Exp $");
159 1.1.2.1 ad
160 1.1.2.1 ad #include <sys/param.h>
161 1.1.2.1 ad #include <sys/malloc.h>
162 1.1.2.1 ad #include <sys/proc.h>
163 1.1.2.1 ad #include <sys/intr.h>
164 1.1.2.1 ad #include <sys/mutex.h>
165 1.1.2.1 ad #include <sys/kthread.h>
166 1.1.2.1 ad #include <sys/evcnt.h>
167 1.1.2.1 ad #include <sys/cpu.h>
168 1.1.2.1 ad
169 1.1.2.1 ad #include <net/netisr.h>
170 1.1.2.1 ad
171 1.1.2.1 ad #include <uvm/uvm_extern.h>
172 1.1.2.1 ad
173 1.1.2.1 ad #define PRI_SOFTCLOCK PRI_INTERRUPT
174 1.1.2.1 ad #define PRI_SOFTBIO (PRI_INTERRUPT + 4)
175 1.1.2.1 ad #define PRI_SOFTNET (PRI_INTERRUPT + 8)
176 1.1.2.1 ad #define PRI_SOFTSERIAL (PRI_INTERRUPT + 12)
177 1.1.2.1 ad
178 1.1.2.1 ad /* This could overlap with signal info in struct lwp. */
179 1.1.2.1 ad typedef struct softint {
180 1.1.2.1 ad TAILQ_HEAD(, softhand) si_q;
181 1.1.2.1 ad struct lwp *si_lwp;
182 1.1.2.1 ad struct cpu_info *si_cpu;
183 1.1.2.1 ad uintptr_t si_machdep;
184 1.1.2.1 ad struct evcnt si_evcnt;
185 1.1.2.1 ad int si_active;
186 1.1.2.2 ad char si_name[8];
187 1.1.2.1 ad } softint_t;
188 1.1.2.1 ad
189 1.1.2.1 ad typedef struct softhand {
190 1.1.2.1 ad TAILQ_ENTRY(softhand) sh_q;
191 1.1.2.1 ad void (*sh_func)(void *);
192 1.1.2.1 ad void *sh_arg;
193 1.1.2.1 ad softint_t *sh_isr;
194 1.1.2.1 ad u_int sh_pending;
195 1.1.2.1 ad u_int sh_flags;
196 1.1.2.1 ad } softhand_t;
197 1.1.2.1 ad
198 1.1.2.1 ad typedef struct softcpu {
199 1.1.2.1 ad struct cpu_info *sc_cpu;
200 1.1.2.1 ad softint_t sc_int[SOFTINT_COUNT];
201 1.1.2.1 ad softhand_t sc_hand[1];
202 1.1.2.1 ad } softcpu_t;
203 1.1.2.1 ad
204 1.1.2.1 ad static void softint_thread(void *);
205 1.1.2.1 ad static void softint_netisr(void *);
206 1.1.2.1 ad
207 1.1.2.1 ad u_int softint_bytes = 8192;
208 1.1.2.1 ad static u_int softint_max;
209 1.1.2.1 ad static kmutex_t softint_lock;
210 1.1.2.1 ad static void *softint_netisr_sih;
211 1.1.2.2 ad struct evcnt softint_block;
212 1.1.2.1 ad
213 1.1.2.1 ad /*
214 1.1.2.1 ad * softint_init_isr:
215 1.1.2.1 ad *
216 1.1.2.1 ad * Initialize a single interrupt level for a single CPU.
217 1.1.2.1 ad */
218 1.1.2.1 ad static void
219 1.1.2.1 ad softint_init_isr(softcpu_t *sc, const char *desc, pri_t pri, u_int level)
220 1.1.2.1 ad {
221 1.1.2.1 ad struct cpu_info *ci;
222 1.1.2.1 ad softint_t *si;
223 1.1.2.1 ad int error;
224 1.1.2.1 ad
225 1.1.2.1 ad si = &sc->sc_int[level];
226 1.1.2.1 ad ci = sc->sc_cpu;
227 1.1.2.1 ad si->si_cpu = ci;
228 1.1.2.1 ad
229 1.1.2.1 ad TAILQ_INIT(&si->si_q);
230 1.1.2.1 ad
231 1.1.2.1 ad error = kthread_create(pri, KTHREAD_MPSAFE | KTHREAD_INTR |
232 1.1.2.1 ad KTHREAD_IDLE, ci, softint_thread, si, &si->si_lwp,
233 1.1.2.1 ad "soft%s/%d", desc, (int)ci->ci_cpuid);
234 1.1.2.1 ad if (error != 0)
235 1.1.2.1 ad panic("softint_init_isr: error %d", error);
236 1.1.2.1 ad
237 1.1.2.2 ad snprintf(si->si_name, sizeof(si->si_name), "%s/%d", desc,
238 1.1.2.2 ad (int)ci->ci_cpuid);
239 1.1.2.1 ad evcnt_attach_dynamic(&si->si_evcnt, EVCNT_TYPE_INTR, NULL,
240 1.1.2.2 ad "softint", si->si_name);
241 1.1.2.1 ad
242 1.1.2.1 ad softint_init_md(si->si_lwp, level, &si->si_machdep, si);
243 1.1.2.2 ad
244 1.1.2.2 ad #ifdef __HAVE_FAST_SOFTINTS
245 1.1.2.2 ad si->si_lwp->l_mutex = &ci->ci_schedstate.spc_lwplock;
246 1.1.2.2 ad #endif
247 1.1.2.1 ad }
248 1.1.2.1 ad
249 1.1.2.1 ad /*
250 1.1.2.1 ad * softint_init_md:
251 1.1.2.1 ad *
252 1.1.2.1 ad * Perform machine-dependent initialization. Arguments:
253 1.1.2.1 ad *
254 1.1.2.1 ad * l
255 1.1.2.1 ad *
256 1.1.2.1 ad * LWP to handle the interrupt
257 1.1.2.1 ad *
258 1.1.2.1 ad * level
259 1.1.2.1 ad *
260 1.1.2.1 ad * Symbolic level: SOFTINT_*
261 1.1.2.1 ad *
262 1.1.2.1 ad * machdep
263 1.1.2.1 ad *
264 1.1.2.1 ad * Private value for machine dependent code,
265 1.1.2.1 ad * passed by MI code to softint_trigger().
266 1.1.2.1 ad *
267 1.1.2.1 ad * cookie
268 1.1.2.1 ad *
269 1.1.2.1 ad * Value to be passed to softint_execute() by
270 1.1.2.1 ad * MD code when an interrupt is being handled.
271 1.1.2.1 ad */
272 1.1.2.1 ad #ifndef __HAVE_FAST_SOFTINTS
273 1.1.2.1 ad void
274 1.1.2.1 ad softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep, void *cookie)
275 1.1.2.1 ad {
276 1.1.2.1 ad
277 1.1.2.1 ad *machdep = (lwp_t)l;
278 1.1.2.1 ad
279 1.1.2.1 ad lwp_lock(l);
280 1.1.2.1 ad /* Cheat and make the KASSERT in softint_thread() happy. */
281 1.1.2.1 ad ((softint_t *)cookie)->si_active = 1;
282 1.1.2.1 ad l->l_stat = LSRUN;
283 1.1.2.1 ad sched_enqueue(l, false);
284 1.1.2.1 ad lwp_unlock(l);
285 1.1.2.1 ad }
286 1.1.2.1 ad #endif /* !__HAVE_FAST_SOFTINTS */
287 1.1.2.1 ad
288 1.1.2.1 ad /*
289 1.1.2.1 ad * softint_init:
290 1.1.2.1 ad *
291 1.1.2.1 ad * Initialize per-CPU data structures. Called from mi_cpu_attach().
292 1.1.2.1 ad */
293 1.1.2.1 ad void
294 1.1.2.1 ad softint_init(struct cpu_info *ci)
295 1.1.2.1 ad {
296 1.1.2.1 ad static struct cpu_info *first;
297 1.1.2.1 ad softcpu_t *sc, *scfirst;
298 1.1.2.1 ad softhand_t *sh, *shmax;
299 1.1.2.1 ad
300 1.1.2.1 ad if (first == NULL) {
301 1.1.2.2 ad /* Boot CPU. */
302 1.1.2.1 ad first = ci;
303 1.1.2.1 ad mutex_init(&softint_lock, MUTEX_DEFAULT, IPL_NONE);
304 1.1.2.1 ad softint_bytes = round_page(softint_bytes);
305 1.1.2.1 ad softint_max = (softint_bytes - sizeof(softcpu_t)) /
306 1.1.2.1 ad sizeof(softhand_t);
307 1.1.2.2 ad evcnt_attach_dynamic(&softint_block, EVCNT_TYPE_INTR,
308 1.1.2.2 ad NULL, "softint", "block");
309 1.1.2.1 ad }
310 1.1.2.1 ad
311 1.1.2.1 ad sc = (softcpu_t *)uvm_km_alloc(kernel_map, softint_bytes, 0,
312 1.1.2.1 ad UVM_KMF_WIRED | UVM_KMF_ZERO);
313 1.1.2.1 ad if (sc == NULL)
314 1.1.2.1 ad panic("softint_init_cpu: cannot allocate memory");
315 1.1.2.1 ad
316 1.1.2.1 ad ci->ci_data.cpu_softcpu = sc;
317 1.1.2.1 ad sc->sc_cpu = ci;
318 1.1.2.1 ad
319 1.1.2.1 ad softint_init_isr(sc, "net", PRI_SOFTNET, SOFTINT_NET);
320 1.1.2.1 ad softint_init_isr(sc, "bio", PRI_SOFTBIO, SOFTINT_BIO);
321 1.1.2.1 ad softint_init_isr(sc, "clk", PRI_SOFTCLOCK, SOFTINT_CLOCK);
322 1.1.2.1 ad softint_init_isr(sc, "ser", PRI_SOFTSERIAL, SOFTINT_SERIAL);
323 1.1.2.1 ad
324 1.1.2.1 ad if (first != ci) {
325 1.1.2.2 ad /* Don't lock -- autoconfiguration will prevent reentry. */
326 1.1.2.1 ad scfirst = first->ci_data.cpu_softcpu;
327 1.1.2.1 ad sh = sc->sc_hand;
328 1.1.2.1 ad memcpy(sh, scfirst->sc_hand, sizeof(*sh) * softint_max);
329 1.1.2.1 ad
330 1.1.2.1 ad /* Update pointers for this CPU. */
331 1.1.2.1 ad for (shmax = sh + softint_max; sh < shmax; sh++) {
332 1.1.2.1 ad if (sh->sh_func == NULL)
333 1.1.2.1 ad continue;
334 1.1.2.1 ad sh->sh_isr =
335 1.1.2.1 ad &sc->sc_int[sh->sh_flags & SOFTINT_LVLMASK];
336 1.1.2.1 ad }
337 1.1.2.1 ad } else {
338 1.1.2.1 ad /* Establish a handler for legacy net interrupts. */
339 1.1.2.1 ad softint_netisr_sih = softint_establish(SOFTINT_NET,
340 1.1.2.1 ad softint_netisr, NULL);
341 1.1.2.1 ad KASSERT(softint_netisr_sih != NULL);
342 1.1.2.1 ad }
343 1.1.2.1 ad }
344 1.1.2.1 ad
345 1.1.2.1 ad /*
346 1.1.2.1 ad * softint_establish:
347 1.1.2.1 ad *
348 1.1.2.1 ad * Register a software interrupt handler.
349 1.1.2.1 ad */
350 1.1.2.1 ad void *
351 1.1.2.1 ad softint_establish(u_int flags, void (*func)(void *), void *arg)
352 1.1.2.1 ad {
353 1.1.2.1 ad CPU_INFO_ITERATOR cii;
354 1.1.2.1 ad struct cpu_info *ci;
355 1.1.2.1 ad softcpu_t *sc;
356 1.1.2.1 ad softhand_t *sh;
357 1.1.2.1 ad u_int level;
358 1.1.2.1 ad u_int index;
359 1.1.2.1 ad
360 1.1.2.1 ad level = (flags & SOFTINT_LVLMASK);
361 1.1.2.1 ad KASSERT(level < SOFTINT_COUNT);
362 1.1.2.1 ad
363 1.1.2.1 ad mutex_enter(&softint_lock);
364 1.1.2.1 ad
365 1.1.2.1 ad /* Find a free slot. */
366 1.1.2.1 ad sc = curcpu()->ci_data.cpu_softcpu;
367 1.1.2.1 ad for (index = 1; index < softint_max; index++)
368 1.1.2.1 ad if (sc->sc_hand[index].sh_func == NULL)
369 1.1.2.1 ad break;
370 1.1.2.1 ad if (index == softint_max) {
371 1.1.2.1 ad mutex_exit(&softint_lock);
372 1.1.2.1 ad printf("WARNING: softint_establish: table full, "
373 1.1.2.1 ad "increase softint_bytes\n");
374 1.1.2.1 ad return NULL;
375 1.1.2.1 ad }
376 1.1.2.1 ad
377 1.1.2.1 ad /* Set up the handler on each CPU. */
378 1.1.2.1 ad for (CPU_INFO_FOREACH(cii, ci)) {
379 1.1.2.1 ad sc = ci->ci_data.cpu_softcpu;
380 1.1.2.1 ad sh = &sc->sc_hand[index];
381 1.1.2.1 ad
382 1.1.2.1 ad sh->sh_isr = &sc->sc_int[level];
383 1.1.2.1 ad sh->sh_func = func;
384 1.1.2.1 ad sh->sh_arg = arg;
385 1.1.2.1 ad sh->sh_flags = flags;
386 1.1.2.1 ad sh->sh_pending = 0;
387 1.1.2.1 ad }
388 1.1.2.1 ad
389 1.1.2.1 ad mutex_exit(&softint_lock);
390 1.1.2.1 ad
391 1.1.2.1 ad return (void *)index;
392 1.1.2.1 ad }
393 1.1.2.1 ad
394 1.1.2.1 ad /*
395 1.1.2.1 ad * softint_disestablish:
396 1.1.2.1 ad *
397 1.1.2.1 ad * Unregister a software interrupt handler.
398 1.1.2.1 ad */
399 1.1.2.1 ad void
400 1.1.2.1 ad softint_disestablish(void *arg)
401 1.1.2.1 ad {
402 1.1.2.1 ad CPU_INFO_ITERATOR cii;
403 1.1.2.1 ad struct cpu_info *ci;
404 1.1.2.1 ad softcpu_t *sc;
405 1.1.2.1 ad softhand_t *sh;
406 1.1.2.1 ad u_int index;
407 1.1.2.1 ad
408 1.1.2.1 ad index = (u_int)arg;
409 1.1.2.1 ad KASSERT(index != 0 && index < softint_max);
410 1.1.2.1 ad
411 1.1.2.1 ad mutex_enter(&softint_lock);
412 1.1.2.1 ad
413 1.1.2.1 ad /* Set up the handler on each CPU. */
414 1.1.2.1 ad for (CPU_INFO_FOREACH(cii, ci)) {
415 1.1.2.1 ad sc = ci->ci_data.cpu_softcpu;
416 1.1.2.1 ad sh = &sc->sc_hand[index];
417 1.1.2.1 ad KASSERT(sh->sh_func != NULL);
418 1.1.2.1 ad KASSERT(sh->sh_pending == 0);
419 1.1.2.1 ad sh->sh_func = NULL;
420 1.1.2.1 ad }
421 1.1.2.1 ad
422 1.1.2.1 ad mutex_exit(&softint_lock);
423 1.1.2.1 ad }
424 1.1.2.1 ad
425 1.1.2.1 ad /*
426 1.1.2.1 ad * softint_trigger:
427 1.1.2.1 ad *
428 1.1.2.1 ad * Cause a soft interrupt handler to begin executing.
429 1.1.2.1 ad */
430 1.1.2.1 ad #ifndef __HAVE_FAST_SOFTINTS
431 1.1.2.1 ad inline void
432 1.1.2.1 ad softint_trigger(uintptr_t machdep)
433 1.1.2.1 ad {
434 1.1.2.1 ad struct cpu_info *ci;
435 1.1.2.1 ad lwp_t *l;
436 1.1.2.1 ad
437 1.1.2.1 ad l = (lwp_t *)machdep;
438 1.1.2.1 ad ci = l->l_cpu;
439 1.1.2.1 ad
440 1.1.2.1 ad spc_lock(ci);
441 1.1.2.1 ad l->l_mutex = ci->ci_schedstate.spc_mutex;
442 1.1.2.1 ad l->l_stat = LSRUN;
443 1.1.2.1 ad sched_enqueue(l, false);
444 1.1.2.1 ad cpu_need_resched(ci, 1);
445 1.1.2.1 ad spc_unlock(ci);
446 1.1.2.1 ad }
447 1.1.2.1 ad #endif /* __HAVE_FAST_SOFTINTS */
448 1.1.2.1 ad
449 1.1.2.1 ad /*
450 1.1.2.1 ad * softint_schedule:
451 1.1.2.1 ad *
452 1.1.2.1 ad * Trigger a software interrupt. Must be called from a hardware
453 1.1.2.1 ad * interrupt handler, or with preemption disabled (since we are
454 1.1.2.1 ad * using the value of curcpu()).
455 1.1.2.1 ad */
456 1.1.2.1 ad void
457 1.1.2.1 ad softint_schedule(void *arg)
458 1.1.2.1 ad {
459 1.1.2.1 ad softhand_t *sh;
460 1.1.2.1 ad softint_t *si;
461 1.1.2.1 ad u_int index;
462 1.1.2.1 ad int s;
463 1.1.2.1 ad
464 1.1.2.1 ad /* Find the handler record for this CPU. */
465 1.1.2.1 ad index = (u_int)arg;
466 1.1.2.1 ad KASSERT(index != 0 && index < softint_max);
467 1.1.2.1 ad sh = &((softcpu_t *)curcpu()->ci_data.cpu_softcpu)->sc_hand[index];
468 1.1.2.1 ad
469 1.1.2.1 ad /* If it's already pending there's nothing to do. */
470 1.1.2.1 ad if (sh->sh_pending)
471 1.1.2.1 ad return;
472 1.1.2.1 ad
473 1.1.2.1 ad /*
474 1.1.2.1 ad * Enqueue the handler into the LWP's pending list.
475 1.1.2.1 ad * If the LWP is completely idle, then make it run.
476 1.1.2.1 ad */
477 1.1.2.1 ad s = splhigh();
478 1.1.2.1 ad if (!sh->sh_pending) {
479 1.1.2.1 ad si = sh->sh_isr;
480 1.1.2.1 ad sh->sh_pending = 1;
481 1.1.2.1 ad TAILQ_INSERT_TAIL(&si->si_q, sh, sh_q);
482 1.1.2.1 ad if (si->si_active == 0) {
483 1.1.2.1 ad si->si_active = 1;
484 1.1.2.1 ad softint_trigger(si->si_machdep);
485 1.1.2.1 ad }
486 1.1.2.1 ad }
487 1.1.2.1 ad splx(s);
488 1.1.2.1 ad }
489 1.1.2.1 ad
490 1.1.2.1 ad /*
491 1.1.2.1 ad * softint_thread:
492 1.1.2.1 ad *
493 1.1.2.1 ad * MI software interrupt dispatch. In the __HAVE_FAST_SOFTINTS
494 1.1.2.1 ad * case, the LWP is switched to without restoring any state, so
495 1.1.2.1 ad * we should not arrive here - there is a direct handoff between
496 1.1.2.1 ad * the interrupt stub and softint_execute().
497 1.1.2.1 ad */
498 1.1.2.1 ad void
499 1.1.2.1 ad softint_thread(void *cookie)
500 1.1.2.1 ad {
501 1.1.2.1 ad #ifdef __HAVE_FAST_SOFTINTS
502 1.1.2.1 ad panic("softint_thread");
503 1.1.2.1 ad #else /* __HAVE_FAST_SOFTINTS */
504 1.1.2.1 ad lwp_t *l;
505 1.1.2.1 ad int s;
506 1.1.2.1 ad
507 1.1.2.1 ad l = curlwp;
508 1.1.2.1 ad s = splhigh();
509 1.1.2.1 ad
510 1.1.2.1 ad for (;;) {
511 1.1.2.1 ad softint_execute(cookie, s);
512 1.1.2.1 ad
513 1.1.2.1 ad lwp_lock(l);
514 1.1.2.1 ad l->l_stat = LSIDL;
515 1.1.2.1 ad mi_switch(l);
516 1.1.2.1 ad }
517 1.1.2.1 ad #endif /* !__HAVE_FAST_SOFTINTS */
518 1.1.2.1 ad }
519 1.1.2.1 ad
520 1.1.2.1 ad /*
521 1.1.2.1 ad * softint_execute:
522 1.1.2.1 ad *
523 1.1.2.1 ad * Invoke handlers for the specified soft interrupt.
524 1.1.2.1 ad * Must be entered at splhigh. Will drop the priority
525 1.1.2.1 ad * to the level specified, but returns back at splhigh.
526 1.1.2.1 ad */
527 1.1.2.1 ad void
528 1.1.2.1 ad softint_execute(void *cookie, int s)
529 1.1.2.1 ad {
530 1.1.2.1 ad softint_t *si;
531 1.1.2.1 ad softhand_t *sh;
532 1.1.2.2 ad lwp_t *l, *l2;
533 1.1.2.1 ad
534 1.1.2.1 ad si = cookie;
535 1.1.2.2 ad l = si->si_lwp;
536 1.1.2.1 ad
537 1.1.2.1 ad KASSERT(si->si_lwp == curlwp);
538 1.1.2.1 ad KASSERT(si->si_cpu == curcpu());
539 1.1.2.1 ad KASSERT(si->si_lwp->l_wchan == NULL);
540 1.1.2.1 ad KASSERT(!TAILQ_EMPTY(&si->si_q));
541 1.1.2.1 ad KASSERT(si->si_active);
542 1.1.2.1 ad
543 1.1.2.1 ad while (!TAILQ_EMPTY(&si->si_q)) {
544 1.1.2.1 ad /*
545 1.1.2.2 ad * If any interrupted LWP has higher priority then we
546 1.1.2.2 ad * must yield immediatley. Note that IPL_HIGH may be
547 1.1.2.2 ad * above IPL_SCHED, so we have to drop the interrupt
548 1.1.2.2 ad * priority level before yielding.
549 1.1.2.2 ad *
550 1.1.2.2 ad * XXXAD Optimise this away.
551 1.1.2.2 ad */
552 1.1.2.2 ad for (l2 = l->l_pinned; l2 != NULL; l2 = l2->l_pinned) {
553 1.1.2.2 ad if (lwp_eprio(l2) > l->l_priority)
554 1.1.2.2 ad break;
555 1.1.2.2 ad }
556 1.1.2.2 ad if (l2 != NULL) {
557 1.1.2.2 ad splx(s);
558 1.1.2.2 ad yield();
559 1.1.2.2 ad (void)splhigh();
560 1.1.2.2 ad continue;
561 1.1.2.2 ad }
562 1.1.2.2 ad
563 1.1.2.2 ad /*
564 1.1.2.1 ad * Pick the longest waiting handler to run. We block
565 1.1.2.1 ad * interrupts but do not lock in order to do this, as
566 1.1.2.1 ad * we are protecting against the local CPU only.
567 1.1.2.1 ad */
568 1.1.2.1 ad sh = TAILQ_FIRST(&si->si_q);
569 1.1.2.1 ad TAILQ_REMOVE(&si->si_q, sh, sh_q);
570 1.1.2.1 ad sh->sh_pending = 0;
571 1.1.2.1 ad splx(s);
572 1.1.2.1 ad
573 1.1.2.1 ad /* Run the handler. */
574 1.1.2.1 ad if ((sh->sh_flags & SOFTINT_MPSAFE) == 0) {
575 1.1.2.2 ad KERNEL_LOCK(1, l);
576 1.1.2.1 ad }
577 1.1.2.1 ad (*sh->sh_func)(sh->sh_arg);
578 1.1.2.1 ad if ((sh->sh_flags & SOFTINT_MPSAFE) == 0) {
579 1.1.2.2 ad KERNEL_UNLOCK_ONE(l);
580 1.1.2.1 ad }
581 1.1.2.1 ad
582 1.1.2.1 ad (void)splhigh();
583 1.1.2.1 ad }
584 1.1.2.1 ad
585 1.1.2.1 ad /*
586 1.1.2.1 ad * Unlocked, but only for statistics.
587 1.1.2.1 ad * Should be per-CPU to prevent cache ping-pong.
588 1.1.2.1 ad */
589 1.1.2.1 ad uvmexp.softs++;
590 1.1.2.1 ad
591 1.1.2.1 ad si->si_evcnt.ev_count++;
592 1.1.2.1 ad si->si_active = 0;
593 1.1.2.1 ad }
594 1.1.2.1 ad
595 1.1.2.1 ad /*
596 1.1.2.1 ad * schednetisr:
597 1.1.2.1 ad *
598 1.1.2.1 ad * Trigger a legacy network interrupt. XXX Needs to go away.
599 1.1.2.1 ad */
600 1.1.2.1 ad void
601 1.1.2.1 ad schednetisr(int isr)
602 1.1.2.1 ad {
603 1.1.2.1 ad int s;
604 1.1.2.1 ad
605 1.1.2.1 ad s = splhigh();
606 1.1.2.1 ad curcpu()->ci_data.cpu_netisrs |= (1 << isr);
607 1.1.2.1 ad softint_schedule(softint_netisr_sih);
608 1.1.2.1 ad splx(s);
609 1.1.2.1 ad }
610 1.1.2.1 ad
611 1.1.2.1 ad /*
612 1.1.2.1 ad * softintr_netisr:
613 1.1.2.1 ad *
614 1.1.2.2 ad * Dispatch legacy network interrupts. XXX Needs to go away.
615 1.1.2.1 ad */
616 1.1.2.1 ad static void
617 1.1.2.1 ad softint_netisr(void *cookie)
618 1.1.2.1 ad {
619 1.1.2.1 ad struct cpu_info *ci;
620 1.1.2.1 ad int s, bits;
621 1.1.2.1 ad
622 1.1.2.1 ad ci = curcpu();
623 1.1.2.1 ad
624 1.1.2.1 ad s = splhigh();
625 1.1.2.1 ad bits = ci->ci_data.cpu_netisrs;
626 1.1.2.1 ad ci->ci_data.cpu_netisrs = 0;
627 1.1.2.1 ad splx(s);
628 1.1.2.1 ad
629 1.1.2.1 ad #define DONETISR(which, func) \
630 1.1.2.1 ad do { \
631 1.1.2.1 ad void func(void); \
632 1.1.2.1 ad if ((bits & (1 << which)) != 0) \
633 1.1.2.1 ad func(); \
634 1.1.2.1 ad } while(0);
635 1.1.2.1 ad #include <net/netisr_dispatch.h>
636 1.1.2.1 ad #undef DONETISR
637 1.1.2.1 ad }
638