intr.h revision 1.1 1 1.1 thorpej /* $NetBSD: intr.h,v 1.1 2024/01/14 22:32:32 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.1 thorpej #if defined(_KERNEL) || defined(_KMEMUSER)
53 1.1 thorpej typedef struct {
54 1.1 thorpej uint16_t _psl; /* physical manifestation of logical IPL_* */
55 1.1 thorpej } ipl_cookie_t;
56 1.1 thorpej #endif
57 1.1 thorpej
58 1.1 thorpej #ifdef _KERNEL
59 1.1 thorpej extern int idepth; /* interrupt depth */
60 1.1 thorpej extern const uint16_t ipl2psl_table[NIPL];
61 1.1 thorpej
62 1.1 thorpej typedef int ipl_t; /* logical IPL_* value */
63 1.1 thorpej
64 1.1 thorpej static inline bool
65 1.1 thorpej cpu_intr_p(void)
66 1.1 thorpej {
67 1.1 thorpej return idepth != 0;
68 1.1 thorpej }
69 1.1 thorpej
70 1.1 thorpej static inline ipl_cookie_t
71 1.1 thorpej makeiplcookie(ipl_t ipl)
72 1.1 thorpej {
73 1.1 thorpej return (ipl_cookie_t){._psl = ipl2psl_table[ipl]};
74 1.1 thorpej }
75 1.1 thorpej
76 1.1 thorpej static inline int
77 1.1 thorpej splraiseipl(ipl_cookie_t icookie)
78 1.1 thorpej {
79 1.1 thorpej return _splraise(icookie._psl);
80 1.1 thorpej }
81 1.1 thorpej
82 1.1 thorpej /*
83 1.1 thorpej * These are essentially constant equivalents of what's in
84 1.1 thorpej * ipl2psl_table[] to avoid the memory reference.
85 1.1 thorpej */
86 1.1 thorpej #define splsoftclock() _splraise(PSL_S | MACHINE_PSL_IPL_SOFTCLOCK)
87 1.1 thorpej #define splsoftbio() _splraise(PSL_S | MACHINE_PSL_IPL_SOFTBIO)
88 1.1 thorpej #define splsoftnet() _splraise(PSL_S | MACHINE_PSL_IPL_SOFTNET)
89 1.1 thorpej #define splsoftserial() _splraise(PSL_S | MACHINE_PSL_IPL_SOFTSERIAL)
90 1.1 thorpej #define splvm() _splraise(PSL_S | MACHINE_PSL_IPL_VM)
91 1.1 thorpej #define splsched() _splraise(PSL_S | MACHINE_PSL_IPL_SCHED)
92 1.1 thorpej #define splhigh() spl7()
93 1.1 thorpej
94 1.1 thorpej /*
95 1.1 thorpej * XXX TODO: Support for hardware-assisted soft interrupts (sun68k)
96 1.1 thorpej * XXX and fast-soft-interrupts (others).
97 1.1 thorpej */
98 1.1 thorpej #define spl0() _spl0()
99 1.1 thorpej #define splx(s) _splx(s)
100 1.1 thorpej
101 1.1 thorpej #ifdef _M68K_INTR_PRIVATE
102 1.1 thorpej #include <sys/queue.h>
103 1.1 thorpej
104 1.1 thorpej struct m68k_intrhand {
105 1.1 thorpej LIST_ENTRY(m68k_intrhand) ih_link;
106 1.1 thorpej int (*ih_func)(void *);
107 1.1 thorpej void *ih_arg;
108 1.1 thorpej struct evcnt *ih_evcnt;
109 1.1 thorpej int ih_ipl; /* m68k IPL, not IPL_* */
110 1.1 thorpej int ih_vec;
111 1.1 thorpej };
112 1.1 thorpej LIST_HEAD(m68k_intrhand_list, m68k_intrhand);
113 1.1 thorpej
114 1.1 thorpej struct m68k_ih_allocfuncs {
115 1.1 thorpej struct m68k_intrhand * (*alloc)(int km_flag);
116 1.1 thorpej void (*free)(struct m68k_intrhand *);
117 1.1 thorpej };
118 1.1 thorpej #else
119 1.1 thorpej struct m68k_ih_allocfuncs;
120 1.1 thorpej #endif /* _M68K_INTR_PRIVATE */
121 1.1 thorpej
122 1.1 thorpej struct evcnt;
123 1.1 thorpej
124 1.1 thorpej /*
125 1.1 thorpej * Common m68k interrupt dispatch:
126 1.1 thorpej *
127 1.1 thorpej * ==> m68k_intr_init(const struct m68k_ih_allocfuncs *allocfuncs)
128 1.1 thorpej *
129 1.1 thorpej * Initialize the interrupt system. If the platform needs to store
130 1.1 thorpej * additional information in the interrupt handle, then it can provide
131 1.1 thorpej * its own alloc/free routines. Otherwise, pass NULL to get the default.
132 1.1 thorpej * If a platform doesn't want the special allocator behavior, calling
133 1.1 thorpej * this function is optional; it will be done for you on the first call
134 1.1 thorpej * to m68k_intr_establish().
135 1.1 thorpej *
136 1.1 thorpej * ==> m68k_intr_establish(int (*func)(void *), void *arg,
137 1.1 thorpej * struct evcnt *ev, int vec, int ipl, int flags)
138 1.1 thorpej *
139 1.1 thorpej * Establish an interrupt handler. If vec is 0, then the handler is
140 1.1 thorpej * registered in the auto-vector list corresponding to the specified
141 1.1 thorpej * m68k interrupt priroity level (this is NOT an IPL_* value). Otherwise.
142 1.1 thorpej * the handler is registered at the specified vector.
143 1.1 thorpej *
144 1.1 thorpej * Vectored interrupts are not sharable. The interrupt vector must be
145 1.1 thorpej * within the platform's "user vector" region, which is generally defined
146 1.1 thorpej * as vectors 64-255, although some platforms may use vectors that start
147 1.1 thorpej * below 64 (in which case, that platform must define MACHINE_USERVEC_START
148 1.1 thorpej * to override the default).
149 1.1 thorpej *
150 1.1 thorpej * Vectored interrupt support is not included by default in order to reduce
151 1.1 thorpej * the memory footprint. If a platform wishes to enable vectored interrupts,
152 1.1 thorpej * then it should define __HAVE_M68K_INTR_VECTORED in its <machine/types.h>
153 1.1 thorpej * and genassym.cf.
154 1.1 thorpej *
155 1.1 thorpej * ==> m68k_intr_disestablish(void *ih)
156 1.1 thorpej *
157 1.1 thorpej * Removes a previously-established interrupt handler. Returns true
158 1.1 thorpej * if there are no more handlers on the list that handler was on. This
159 1.1 thorpej * information can be used to e.g. disable interrupts on a PIC.
160 1.1 thorpej */
161 1.1 thorpej void m68k_intr_init(const struct m68k_ih_allocfuncs *);
162 1.1 thorpej void *m68k_intr_establish(int (*)(void *), void *, struct evcnt *,
163 1.1 thorpej int/*vec*/, int/*m68k ipl*/, int/*isrpri*/, int/*flags*/);
164 1.1 thorpej bool m68k_intr_disestablish(void *);
165 1.1 thorpej
166 1.1 thorpej #endif /* _KERNEL */
167 1.1 thorpej
168 1.1 thorpej #endif /* _M68k_INTR_H_ */
169