intr.h revision 1.4 1 1.4 thorpej /* $NetBSD: intr.h,v 1.4 2024/01/15 18:47:03 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2023, 2024 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #ifndef _M68k_INTR_H_
33 1.1 thorpej #define _M68k_INTR_H_
34 1.1 thorpej
35 1.1 thorpej #include <sys/types.h>
36 1.1 thorpej #include <machine/psl.h>
37 1.1 thorpej
38 1.1 thorpej /*
39 1.1 thorpej * Logical interrupt priority levels -- these are distinct from
40 1.1 thorpej * the hardware interrupt priority levels of the m68k.
41 1.1 thorpej */
42 1.1 thorpej #define IPL_NONE 0
43 1.1 thorpej #define IPL_SOFTCLOCK 1 /* clock software interrupts */
44 1.1 thorpej #define IPL_SOFTBIO 2 /* block device software interrupts */
45 1.1 thorpej #define IPL_SOFTNET 3 /* network software interrupts */
46 1.1 thorpej #define IPL_SOFTSERIAL 4 /* serial device software interrupts */
47 1.1 thorpej #define IPL_VM 5 /* all interrupts that can allocate memory */
48 1.1 thorpej #define IPL_SCHED 6 /* scheduler / hard clock interrupts */
49 1.1 thorpej #define IPL_HIGH 7 /* blocks all interrupts */
50 1.1 thorpej #define NIPL 8
51 1.1 thorpej
52 1.2 thorpej /*
53 1.2 thorpej * Abstract ISR priorities. These allow sorting of latency-sensitive
54 1.2 thorpej * devices earlier on the shared auto-vectored interrupt lists.
55 1.2 thorpej */
56 1.2 thorpej #define ISRPRI_BIO 0 /* a block I/O device */
57 1.2 thorpej #define ISRPRI_MISC 0 /* misc. devices */
58 1.2 thorpej #define ISRPRI_NET 1 /* a network interface */
59 1.2 thorpej #define ISRPRI_TTY 2 /* a serial port */
60 1.2 thorpej #define ISRPRI_DISPLAY 2 /* display devices / framebuffers */
61 1.2 thorpej #define ISRPRI_TTYNOBUF 3 /* a particularly bad serial port */
62 1.2 thorpej #define ISRPRI_AUDIO 4 /* audio devices */
63 1.2 thorpej
64 1.1 thorpej #if defined(_KERNEL) || defined(_KMEMUSER)
65 1.1 thorpej typedef struct {
66 1.1 thorpej uint16_t _psl; /* physical manifestation of logical IPL_* */
67 1.1 thorpej } ipl_cookie_t;
68 1.1 thorpej #endif
69 1.1 thorpej
70 1.1 thorpej #ifdef _KERNEL
71 1.1 thorpej extern int idepth; /* interrupt depth */
72 1.1 thorpej extern const uint16_t ipl2psl_table[NIPL];
73 1.1 thorpej
74 1.1 thorpej typedef int ipl_t; /* logical IPL_* value */
75 1.1 thorpej
76 1.1 thorpej static inline bool
77 1.1 thorpej cpu_intr_p(void)
78 1.1 thorpej {
79 1.1 thorpej return idepth != 0;
80 1.1 thorpej }
81 1.1 thorpej
82 1.1 thorpej static inline ipl_cookie_t
83 1.1 thorpej makeiplcookie(ipl_t ipl)
84 1.1 thorpej {
85 1.1 thorpej return (ipl_cookie_t){._psl = ipl2psl_table[ipl]};
86 1.1 thorpej }
87 1.1 thorpej
88 1.1 thorpej static inline int
89 1.1 thorpej splraiseipl(ipl_cookie_t icookie)
90 1.1 thorpej {
91 1.1 thorpej return _splraise(icookie._psl);
92 1.1 thorpej }
93 1.1 thorpej
94 1.1 thorpej /*
95 1.1 thorpej * These are essentially constant equivalents of what's in
96 1.1 thorpej * ipl2psl_table[] to avoid the memory reference.
97 1.1 thorpej */
98 1.1 thorpej #define splsoftclock() _splraise(PSL_S | MACHINE_PSL_IPL_SOFTCLOCK)
99 1.1 thorpej #define splsoftbio() _splraise(PSL_S | MACHINE_PSL_IPL_SOFTBIO)
100 1.1 thorpej #define splsoftnet() _splraise(PSL_S | MACHINE_PSL_IPL_SOFTNET)
101 1.1 thorpej #define splsoftserial() _splraise(PSL_S | MACHINE_PSL_IPL_SOFTSERIAL)
102 1.1 thorpej #define splvm() _splraise(PSL_S | MACHINE_PSL_IPL_VM)
103 1.1 thorpej #define splsched() _splraise(PSL_S | MACHINE_PSL_IPL_SCHED)
104 1.1 thorpej #define splhigh() spl7()
105 1.1 thorpej
106 1.1 thorpej /*
107 1.1 thorpej * XXX TODO: Support for hardware-assisted soft interrupts (sun68k)
108 1.1 thorpej * XXX and fast-soft-interrupts (others).
109 1.1 thorpej */
110 1.1 thorpej #define spl0() _spl0()
111 1.1 thorpej #define splx(s) _splx(s)
112 1.1 thorpej
113 1.1 thorpej #ifdef _M68K_INTR_PRIVATE
114 1.1 thorpej #include <sys/queue.h>
115 1.1 thorpej
116 1.1 thorpej struct m68k_intrhand {
117 1.1 thorpej LIST_ENTRY(m68k_intrhand) ih_link;
118 1.1 thorpej int (*ih_func)(void *);
119 1.1 thorpej void *ih_arg;
120 1.1 thorpej struct evcnt *ih_evcnt;
121 1.1 thorpej int ih_ipl; /* m68k IPL, not IPL_* */
122 1.1 thorpej int ih_vec;
123 1.2 thorpej int ih_isrpri;
124 1.1 thorpej };
125 1.1 thorpej LIST_HEAD(m68k_intrhand_list, m68k_intrhand);
126 1.1 thorpej
127 1.1 thorpej struct m68k_ih_allocfuncs {
128 1.1 thorpej struct m68k_intrhand * (*alloc)(int km_flag);
129 1.1 thorpej void (*free)(struct m68k_intrhand *);
130 1.1 thorpej };
131 1.1 thorpej #else
132 1.1 thorpej struct m68k_ih_allocfuncs;
133 1.1 thorpej #endif /* _M68K_INTR_PRIVATE */
134 1.1 thorpej
135 1.3 thorpej #include <sys/evcnt.h>
136 1.3 thorpej
137 1.4 thorpej #ifdef __HAVE_LEGACY_INTRCNT
138 1.4 thorpej #define m68k_count_intr(x) \
139 1.4 thorpej do { \
140 1.4 thorpej extern u_int intrcnt[]; \
141 1.4 thorpej intrcnt[(x)]++; \
142 1.4 thorpej curcpu()->ci_data.cpu_nintr++; \
143 1.4 thorpej } while (/*CONSTCOND*/0)
144 1.4 thorpej #else
145 1.3 thorpej /*
146 1.3 thorpej * This is exposed here so that platform-specific interrupt handlers
147 1.3 thorpej * can access it.
148 1.3 thorpej */
149 1.3 thorpej extern struct evcnt m68k_intr_evcnt[];
150 1.4 thorpej
151 1.4 thorpej #define m68k_count_intr(x) \
152 1.4 thorpej do { \
153 1.4 thorpej /* 32-bit counter should be sufficient for m68k. */ \
154 1.4 thorpej m68k_intr_evcnt[(x)].ev_count32++; \
155 1.4 thorpej curcpu()->ci_data.cpu_nintr++; \
156 1.4 thorpej } while (/*CONSTCOND*/0)
157 1.4 thorpej #endif /* __HAVE_LEGACY_INTRCNT */
158 1.1 thorpej
159 1.1 thorpej /*
160 1.1 thorpej * Common m68k interrupt dispatch:
161 1.1 thorpej *
162 1.1 thorpej * ==> m68k_intr_init(const struct m68k_ih_allocfuncs *allocfuncs)
163 1.1 thorpej *
164 1.1 thorpej * Initialize the interrupt system. If the platform needs to store
165 1.1 thorpej * additional information in the interrupt handle, then it can provide
166 1.1 thorpej * its own alloc/free routines. Otherwise, pass NULL to get the default.
167 1.1 thorpej * If a platform doesn't want the special allocator behavior, calling
168 1.1 thorpej * this function is optional; it will be done for you on the first call
169 1.1 thorpej * to m68k_intr_establish().
170 1.1 thorpej *
171 1.1 thorpej * ==> m68k_intr_establish(int (*func)(void *), void *arg,
172 1.1 thorpej * struct evcnt *ev, int vec, int ipl, int flags)
173 1.1 thorpej *
174 1.1 thorpej * Establish an interrupt handler. If vec is 0, then the handler is
175 1.1 thorpej * registered in the auto-vector list corresponding to the specified
176 1.1 thorpej * m68k interrupt priroity level (this is NOT an IPL_* value). Otherwise.
177 1.1 thorpej * the handler is registered at the specified vector.
178 1.1 thorpej *
179 1.1 thorpej * Vectored interrupts are not sharable. The interrupt vector must be
180 1.1 thorpej * within the platform's "user vector" region, which is generally defined
181 1.1 thorpej * as vectors 64-255, although some platforms may use vectors that start
182 1.1 thorpej * below 64 (in which case, that platform must define MACHINE_USERVEC_START
183 1.1 thorpej * to override the default).
184 1.1 thorpej *
185 1.1 thorpej * Vectored interrupt support is not included by default in order to reduce
186 1.1 thorpej * the memory footprint. If a platform wishes to enable vectored interrupts,
187 1.1 thorpej * then it should define __HAVE_M68K_INTR_VECTORED in its <machine/types.h>
188 1.1 thorpej * and genassym.cf.
189 1.1 thorpej *
190 1.1 thorpej * ==> m68k_intr_disestablish(void *ih)
191 1.1 thorpej *
192 1.1 thorpej * Removes a previously-established interrupt handler. Returns true
193 1.1 thorpej * if there are no more handlers on the list that handler was on. This
194 1.1 thorpej * information can be used to e.g. disable interrupts on a PIC.
195 1.1 thorpej */
196 1.1 thorpej void m68k_intr_init(const struct m68k_ih_allocfuncs *);
197 1.1 thorpej void *m68k_intr_establish(int (*)(void *), void *, struct evcnt *,
198 1.1 thorpej int/*vec*/, int/*m68k ipl*/, int/*isrpri*/, int/*flags*/);
199 1.1 thorpej bool m68k_intr_disestablish(void *);
200 1.1 thorpej
201 1.1 thorpej #endif /* _KERNEL */
202 1.1 thorpej
203 1.1 thorpej #endif /* _M68k_INTR_H_ */
204