1/*	$OpenBSD: softintr.h,v 1.2 2020/08/14 16:51:09 kettenis Exp $	*/
2/*	$NetBSD: softintr.h,v 1.1 2002/01/29 22:54:14 thorpej Exp $	*/
3
4/*-
5 * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Charles M. Hannum, and 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#ifndef	_MACHINE_SOFTINTR_H_
34#define	_MACHINE_SOFTINTR_H_
35
36#ifdef _KERNEL
37
38#include <sys/mutex.h>
39#include <sys/queue.h>
40
41/*
42 * Generic software interrupt support.
43 *
44 * To use this code, include <machine/softintr.h> from your platform's
45 * <machine/intr.h>.
46 */
47
48#define	SIR_SOFT	0	/* for IPL_SOFT */
49#define	SIR_CLOCK	1	/* for IPL_SOFTCLOCK */
50#define	SIR_NET		2	/* for IPL_SOFTNET */
51#define	SIR_TTY		3	/* for IPL_SOFTTTY */
52
53#define	SI_NSOFTINTR		4
54
55struct soft_intrhand {
56	TAILQ_ENTRY(soft_intrhand)
57		sih_q;
58	struct soft_intr *sih_intrhead;
59	void    (*sih_fn)(void *);
60	void    (*sih_fnwrap)(void *);
61	void    *sih_arg;
62	void    *sih_argwrap;
63	int     sih_pending;
64};
65
66struct soft_intr {
67	TAILQ_HEAD(, soft_intrhand)
68			softintr_q;
69	int             softintr_ssir;
70	struct mutex    softintr_lock;
71};
72
73#define SOFTINTR_ESTABLISH_MPSAFE       0x01
74void    *softintr_establish_flags(int, void (*)(void *), void *, int);
75#define softintr_establish(i, f, a)                                     \
76        softintr_establish_flags(i, f, a, 0)
77#define softintr_establish_mpsafe(i, f, a)                              \
78        softintr_establish_flags(i, f, a, SOFTINTR_ESTABLISH_MPSAFE)
79void    softintr_disestablish(void *);
80void    softintr_init(void);
81void    softintr_dispatch(int);
82void    softintr(int);
83
84#define softintr_schedule(arg)						\
85do {									\
86	struct soft_intrhand *__sih = (arg);				\
87	struct soft_intr *__si = __sih->sih_intrhead;			\
88									\
89	mtx_enter(&__si->softintr_lock);				\
90	if (__sih->sih_pending == 0) {					\
91		TAILQ_INSERT_TAIL(&__si->softintr_q, __sih, sih_q);	\
92		__sih->sih_pending = 1;					\
93		softintr(__si->softintr_ssir);				\
94	}								\
95	mtx_leave(&__si->softintr_lock);				\
96} while (/*CONSTCOND*/ 0)
97
98#endif /* _KERNEL */
99
100#endif	/* _MACHINE_SOFTINTR_H_ */
101