intr.c revision 266755
1/*	$NetBSD: intr.c,v 1.12 2003/07/15 00:24:41 lukem Exp $	*/
2
3/*-
4 * Copyright (c) 2004 Olivier Houchard.
5 * Copyright (c) 1994-1998 Mark Brinicombe.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by Mark Brinicombe
19 *	for the NetBSD Project.
20 * 4. The name of the company nor the name of the author may be used to
21 *    endorse or promote products derived from this software without specific
22 *    prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * Soft interrupt and other generic interrupt functions.
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: stable/10/sys/arm/arm/intr.c 266755 2014-05-27 16:17:25Z ian $");
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/syslog.h>
44#include <sys/malloc.h>
45#include <sys/proc.h>
46#include <sys/bus.h>
47#include <sys/interrupt.h>
48#include <sys/conf.h>
49#include <machine/atomic.h>
50#include <machine/intr.h>
51#include <machine/cpu.h>
52
53#define	INTRNAME_LEN	(MAXCOMLEN + 1)
54
55typedef void (*mask_fn)(void *);
56
57static struct intr_event *intr_events[NIRQ];
58
59void	arm_irq_handler(struct trapframe *);
60
61void (*arm_post_filter)(void *) = NULL;
62int (*arm_config_irq)(int irq, enum intr_trigger trig,
63    enum intr_polarity pol) = NULL;
64
65/* Data for statistics reporting. */
66u_long intrcnt[NIRQ];
67char intrnames[NIRQ * INTRNAME_LEN];
68size_t sintrcnt = sizeof(intrcnt);
69size_t sintrnames = sizeof(intrnames);
70
71/*
72 * Pre-format intrnames into an array of fixed-size strings containing spaces.
73 * This allows us to avoid the need for an intermediate table of indices into
74 * the names and counts arrays, while still meeting the requirements and
75 * assumptions of vmstat(8) and the kdb "show intrcnt" command, the two
76 * consumers of this data.
77 */
78void
79arm_intrnames_init(void)
80{
81	int i;
82
83	for (i = 0; i < NIRQ; ++i) {
84		snprintf(&intrnames[i * INTRNAME_LEN], INTRNAME_LEN, "%-*s",
85		    INTRNAME_LEN - 1, "");
86	}
87}
88
89void
90arm_setup_irqhandler(const char *name, driver_filter_t *filt,
91    void (*hand)(void*), void *arg, int irq, int flags, void **cookiep)
92{
93	struct intr_event *event;
94	int error;
95
96	if (irq < 0 || irq >= NIRQ)
97		return;
98	event = intr_events[irq];
99	if (event == NULL) {
100		error = intr_event_create(&event, (void *)irq, 0, irq,
101		    (mask_fn)arm_mask_irq, (mask_fn)arm_unmask_irq,
102		    arm_post_filter, NULL, "intr%d:", irq);
103		if (error)
104			return;
105		intr_events[irq] = event;
106		snprintf(&intrnames[irq * INTRNAME_LEN], INTRNAME_LEN,
107		    "irq%d: %-*s", irq, INTRNAME_LEN - 1, name);
108	}
109	intr_event_add_handler(event, name, filt, hand, arg,
110	    intr_priority(flags), flags, cookiep);
111}
112
113int
114arm_remove_irqhandler(int irq, void *cookie)
115{
116	struct intr_event *event;
117	int error;
118
119	event = intr_events[irq];
120	arm_mask_irq(irq);
121
122	error = intr_event_remove_handler(cookie);
123
124	if (!TAILQ_EMPTY(&event->ie_handlers))
125		arm_unmask_irq(irq);
126	return (error);
127}
128
129void dosoftints(void);
130void
131dosoftints(void)
132{
133}
134
135void
136arm_irq_handler(struct trapframe *frame)
137{
138	struct intr_event *event;
139	int i;
140
141	PCPU_INC(cnt.v_intr);
142	i = -1;
143	while ((i = arm_get_next_irq(i)) != -1) {
144		intrcnt[i]++;
145		event = intr_events[i];
146		if (intr_event_handle(event, frame) != 0) {
147			/* XXX: Log stray IRQs */
148			arm_mask_irq(i);
149		}
150	}
151}
152
153/*
154 * arm_irq_memory_barrier()
155 *
156 * Ensure all writes to device memory have reached devices before proceeding.
157 *
158 * This is intended to be called from the post-filter and post-thread routines
159 * of an interrupt controller implementation.  A peripheral device driver should
160 * use bus_space_barrier() if it needs to ensure a write has reached the
161 * hardware for some reason other than clearing interrupt conditions.
162 *
163 * The need for this function arises from the ARM weak memory ordering model.
164 * Writes to locations mapped with the Device attribute bypass any caches, but
165 * are buffered.  Multiple writes to the same device will be observed by that
166 * device in the order issued by the cpu.  Writes to different devices may
167 * appear at those devices in a different order than issued by the cpu.  That
168 * is, if the cpu writes to device A then device B, the write to device B could
169 * complete before the write to device A.
170 *
171 * Consider a typical device interrupt handler which services the interrupt and
172 * writes to a device status-acknowledge register to clear the interrupt before
173 * returning.  That write is posted to the L2 controller which "immediately"
174 * places it in a store buffer and automatically drains that buffer.  This can
175 * be less immediate than you'd think... There may be no free slots in the store
176 * buffers, so an existing buffer has to be drained first to make room.  The
177 * target bus may be busy with other traffic (such as DMA for various devices),
178 * delaying the drain of the store buffer for some indeterminate time.  While
179 * all this delay is happening, execution proceeds on the CPU, unwinding its way
180 * out of the interrupt call stack to the point where the interrupt driver code
181 * is ready to EOI and unmask the interrupt.  The interrupt controller may be
182 * accessed via a faster bus than the hardware whose handler just ran; the write
183 * to unmask and EOI the interrupt may complete quickly while the device write
184 * to ack and clear the interrupt source is still lingering in a store buffer
185 * waiting for access to a slower bus.  With the interrupt unmasked at the
186 * interrupt controller but still active at the device, as soon as interrupts
187 * are enabled on the core the device re-interrupts immediately: now you've got
188 * a spurious interrupt on your hands.
189 *
190 * The right way to fix this problem is for every device driver to use the
191 * proper bus_space_barrier() calls in its interrupt handler.  For ARM a single
192 * barrier call at the end of the handler would work.  This would have to be
193 * done to every driver in the system, not just arm-specific drivers.
194 *
195 * Another potential fix is to map all device memory as Strongly-Ordered rather
196 * than Device memory, which takes the store buffers out of the picture.  This
197 * has a pretty big impact on overall system performance, because each strongly
198 * ordered memory access causes all L2 store buffers to be drained.
199 *
200 * A compromise solution is to have the interrupt controller implementation call
201 * this function to establish a barrier between writes to the interrupt-source
202 * device and writes to the interrupt controller device.
203 *
204 * This takes the interrupt number as an argument, and currently doesn't use it.
205 * The plan is that maybe some day there is a way to flag certain interrupts as
206 * "memory barrier safe" and we can avoid this overhead with them.
207 */
208void
209arm_irq_memory_barrier(uintptr_t irq)
210{
211
212	dsb();
213	cpu_l2cache_drain_writebuf();
214}
215
216