1/*	$NetBSD: isa_irqhandler.c,v 1.29 2020/11/22 03:57:19 thorpej Exp $	*/
2
3/*
4 * Copyright 1997
5 * Digital Equipment Corporation. All rights reserved.
6 *
7 * This software is furnished under license and may be used and
8 * copied only in accordance with the following terms and conditions.
9 * Subject to these conditions, you may download, copy, install,
10 * use, modify and distribute this software in source and/or binary
11 * form. No title or ownership is transferred hereby.
12 *
13 * 1) Any source code used, modified or distributed must reproduce
14 *    and retain this copyright notice and list of conditions as
15 *    they appear in the source file.
16 *
17 * 2) No right is granted to use any trade name, trademark, or logo of
18 *    Digital Equipment Corporation. Neither the "Digital Equipment
19 *    Corporation" name nor any trademark or logo of Digital Equipment
20 *    Corporation may be used to endorse or promote products derived
21 *    from this software without the prior written permission of
22 *    Digital Equipment Corporation.
23 *
24 * 3) This software is provided "AS-IS" and any express or implied
25 *    warranties, including but not limited to, any implied warranties
26 *    of merchantability, fitness for a particular purpose, or
27 *    non-infringement are disclaimed. In no event shall DIGITAL be
28 *    liable for any damages whatsoever, and in particular, DIGITAL
29 *    shall not be liable for special, indirect, consequential, or
30 *    incidental damages or damages for lost profits, loss of
31 *    revenue or loss of use, whether such damages arise in contract,
32 *    negligence, tort, under statute, in equity, at law or otherwise,
33 *    even if advised of the possibility of such damage.
34 */
35
36/*
37 * Copyright (c) 1994-1998 Mark Brinicombe.
38 * Copyright (c) 1994 Brini.
39 * All rights reserved.
40 *
41 * This code is derived from software written for Brini by Mark Brinicombe
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 *    notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 *    notice, this list of conditions and the following disclaimer in the
50 *    documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 *    must display the following acknowledgement:
53 *	This product includes software developed by Mark Brinicombe
54 *	for the NetBSD Project.
55 * 4. The name of the company nor the name of the author may be used to
56 *    endorse or promote products derived from this software without specific
57 *    prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 *
70 * IRQ/FIQ initialisation, claim, release and handler routines
71 *
72 *	from: irqhandler.c
73 *
74 * Created      : 30/09/94
75 */
76
77#include <sys/cdefs.h>
78__KERNEL_RCSID(0, "$NetBSD: isa_irqhandler.c,v 1.29 2020/11/22 03:57:19 thorpej Exp $");
79
80#include <sys/param.h>
81#include <sys/systm.h>
82#include <sys/syslog.h>
83#include <sys/kmem.h>
84#include <sys/intr.h>
85
86#include <arm/locore.h>
87
88#include <machine/irqhandler.h>
89
90irqhandler_t *irqhandlers[NIRQS];
91
92u_int current_mask;
93u_int actual_mask;
94u_int disabled_mask;
95u_int irqmasks[NIPL];
96
97/* Prototypes */
98
99extern void set_spl_masks(void);
100void irq_calculatemasks(void);
101void stray_irqhandler(u_int);
102
103#define WriteWord(a, b) *((volatile unsigned int *)(a)) = (b)
104
105/*
106 * void irq_init(void)
107 *
108 * Initialise the IRQ/FIQ sub system
109 */
110
111void
112irq_init(void)
113{
114	int loop;
115
116	/* Clear all the IRQ handlers and the irq block masks */
117	for (loop = 0; loop < NIRQS; ++loop) {
118		irqhandlers[loop] = NULL;
119	}
120
121	/*
122	 * Setup the irqmasks for the different Interrupt Priority Levels
123	 * We will start with no bits set and these will be updated as handlers
124	 * are installed at different IPL's.
125	 */
126	for (loop = 0; loop < NIPL; ++loop)
127		irqmasks[loop] = 0;
128
129	current_mask = 0x00000000;
130	disabled_mask = 0x00000000;
131	actual_mask = 0x00000000;
132
133	set_spl_masks();
134
135	/* Enable IRQ's and FIQ's */
136	enable_interrupts(I32_bit | F32_bit);
137}
138
139
140/*
141 * int irq_claim(int irq, irqhandler_t *handler)
142 *
143 * Enable an IRQ and install a handler for it.
144 */
145
146int
147irq_claim(int irq, irqhandler_t *handler, const char *group, const char *name)
148{
149
150#ifdef DIAGNOSTIC
151	/* Sanity check */
152	if (handler == NULL)
153		panic("NULL interrupt handler");
154	if (handler->ih_func == NULL)
155		panic("Interrupt handler does not have a function");
156#endif	/* DIAGNOSTIC */
157
158	/*
159	 * IRQ_INSTRUCT indicates that we should get the irq number
160	 * from the irq structure
161	 */
162	if (irq == IRQ_INSTRUCT)
163		irq = handler->ih_num;
164
165	/* Make sure the irq number is valid */
166	if (irq < 0 || irq >= NIRQS)
167		return(-1);
168
169	/* Make sure the level is valid */
170	if (handler->ih_level < 0 || handler->ih_level >= NIPL)
171    	        return(-1);
172
173	/* Attach evcnt */
174	evcnt_attach_dynamic(&handler->ih_ev, EVCNT_TYPE_INTR, NULL,
175	    group, name);
176
177	/* Attach handler at top of chain */
178	handler->ih_next = irqhandlers[irq];
179	irqhandlers[irq] = handler;
180
181	/*
182	 * Reset the flags for this handler.
183	 * As the handler is now in the chain mark it as active.
184	 */
185	handler->ih_flags = 0 | IRQ_FLAG_ACTIVE;
186
187	/*
188	 * Record the interrupt number for accounting.
189	 * Done here as the accounting number may not be the same as the
190	 * IRQ number though for the moment they are
191	 */
192	handler->ih_num = irq;
193
194	irq_calculatemasks();
195
196	enable_irq(irq);
197	set_spl_masks();
198	return(0);
199}
200
201
202/*
203 * int irq_release(int irq, irqhandler_t *handler)
204 *
205 * Disable an IRQ and remove a handler for it.
206 */
207
208int
209irq_release(int irq, irqhandler_t *handler)
210{
211	irqhandler_t *irqhand;
212	irqhandler_t **prehand;
213
214	/*
215	 * IRQ_INSTRUCT indicates that we should get the irq number
216	 * from the irq structure
217	 */
218	if (irq == IRQ_INSTRUCT)
219		irq = handler->ih_num;
220
221	/* Make sure the irq number is valid */
222	if (irq < 0 || irq >= NIRQS)
223		return(-1);
224
225	/* Locate the handler */
226	prehand = &irqhandlers[irq];
227	irqhand = *prehand;
228
229	while (irqhand && handler != irqhand) {
230		prehand = &irqhand->ih_next;
231		irqhand = *prehand;
232	}
233
234	/* Remove the handler if located */
235	if (irqhand)
236		*prehand = irqhand->ih_next;
237	else
238		return(-1);
239
240	/* The handler has been removed from the chain so mark it as inactive */
241	irqhand->ih_flags &= ~IRQ_FLAG_ACTIVE;
242
243	/* Make sure the head of the handler list is active */
244	if (irqhandlers[irq])
245		irqhandlers[irq]->ih_flags |= IRQ_FLAG_ACTIVE;
246
247	evcnt_detach(&irqhand->ih_ev);
248
249	irq_calculatemasks();
250
251	/*
252	 * Disable the appropriate mask bit if there are no handlers left for
253	 * this IRQ.
254	 */
255	if (irqhandlers[irq] == NULL)
256		disable_irq(irq);
257
258	set_spl_masks();
259
260	return(0);
261}
262
263/* adapted from .../i386/isa/isa_machdep.c */
264/*
265 * Recalculate the interrupt masks from scratch.
266 * We could code special registry and deregistry versions of this function that
267 * would be faster, but the code would be nastier, and we don't expect this to
268 * happen very much anyway.
269 */
270void
271irq_calculatemasks(void)
272{
273	int          irq, level;
274	irqhandler_t *ptr;
275	int          irqlevel[NIRQS];
276
277	/* First, figure out which levels each IRQ uses. */
278	for (irq = 0; irq < NIRQS; irq++) {
279		int levels = 0;
280		for (ptr = irqhandlers[irq]; ptr; ptr = ptr->ih_next)
281			levels |= 1 << ptr->ih_level;
282		irqlevel[irq] = levels;
283	}
284
285	/* Then figure out which IRQs use each level. */
286	for (level = 0; level < NIPL; level++) {
287		int irqs = 0;
288		for (irq = 0; irq < NIRQS; irq++)
289			if (irqlevel[irq] & (1 << level))
290				irqs |= 1 << irq;
291		irqmasks[level] = ~irqs;
292	}
293
294	/*
295	 * Enforce a hierarchy that gives slow devices a better chance at not
296	 * dropping data.
297	 */
298	KASSERT(irqmasks[IPL_NONE] == ~0);
299	irqmasks[IPL_SOFTCLOCK] &= irqmasks[IPL_NONE];
300	irqmasks[IPL_SOFTBIO] &= irqmasks[IPL_SOFTCLOCK];
301	irqmasks[IPL_SOFTNET] &= irqmasks[IPL_SOFTBIO];
302	irqmasks[IPL_SOFTSERIAL] &= irqmasks[IPL_SOFTNET];
303	irqmasks[IPL_VM] &= irqmasks[IPL_SOFTSERIAL];
304	irqmasks[IPL_CLOCK] &= irqmasks[IPL_VM];
305	irqmasks[IPL_HIGH] &= irqmasks[IPL_CLOCK];
306}
307
308
309void *
310intr_claim(int irq, int level, int (*ih_func)(void *), void *ih_arg, const char *group, const char *name)
311{
312	irqhandler_t *ih;
313
314	ih = kmem_zalloc(sizeof(*ih), KM_SLEEP);
315	ih->ih_level = level;
316	ih->ih_func = ih_func;
317	ih->ih_arg = ih_arg;
318	ih->ih_flags = 0;
319
320	if (irq_claim(irq, ih, group, name) != 0) {
321		kmem_free(ih, sizeof(*ih));
322		return(NULL);
323	}
324
325	return(ih);
326}
327
328int
329intr_release(void *arg)
330{
331	irqhandler_t *ih = (irqhandler_t *)arg;
332
333	if (irq_release(ih->ih_num, ih) == 0) {
334		kmem_free(ih, sizeof(*ih));
335		return(0);
336	}
337	return(1);
338}
339
340
341/*
342 * void disable_irq(int irq)
343 *
344 * Disables a specific irq. The irq is removed from the master irq mask
345 */
346
347void
348disable_irq(int irq)
349{
350	u_int oldirqstate;
351
352	oldirqstate = disable_interrupts(I32_bit);
353	current_mask &= ~(1 << irq);
354	irq_setmasks();
355	restore_interrupts(oldirqstate);
356}
357
358
359/*
360 * void enable_irq(int irq)
361 *
362 * Enables a specific irq. The irq is added to the master irq mask
363 * This routine should be used with caution. A handler should already
364 * be installed.
365 */
366
367void
368enable_irq(int irq)
369{
370	u_int oldirqstate;
371
372	oldirqstate = disable_interrupts(I32_bit);
373	current_mask |= (1 << irq);
374	irq_setmasks();
375	restore_interrupts(oldirqstate);
376}
377
378
379/*
380 * void stray_irqhandler(u_int mask)
381 *
382 * Handler for stray interrupts. This gets called if a handler cannot be
383 * found for an interrupt.
384 */
385
386void
387stray_irqhandler(u_int mask)
388{
389	static u_int stray_irqs = 0;
390
391	if (++stray_irqs <= 8)
392		log(LOG_ERR, "Stray interrupt %08x%s\n", mask,
393		    stray_irqs >= 8 ? ": stopped logging" : "");
394}
395