]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/intr.c
Upgrade to OpenSSH 6.8p1.
[FreeBSD/FreeBSD.git] / sys / arm / arm / intr.c
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 "opt_platform.h"
40 #include "opt_hwpmc_hooks.h"
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/syslog.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51 #include <sys/bus.h>
52 #include <sys/interrupt.h>
53 #include <sys/conf.h>
54 #include <sys/pmc.h>
55 #include <sys/pmckern.h>
56
57 #include <machine/atomic.h>
58 #include <machine/bus.h>
59 #include <machine/intr.h>
60 #include <machine/cpu.h>
61
62 #ifdef FDT
63 #include <dev/fdt/fdt_common.h>
64 #include <machine/fdt.h>
65 #endif
66
67 #define INTRNAME_LEN    (MAXCOMLEN + 1)
68
69 typedef void (*mask_fn)(void *);
70
71 static struct intr_event *intr_events[NIRQ];
72
73 void    intr_irq_handler(struct trapframe *);
74
75 void (*arm_post_filter)(void *) = NULL;
76 int (*arm_config_irq)(int irq, enum intr_trigger trig,
77     enum intr_polarity pol) = NULL;
78
79 /* Data for statistics reporting. */
80 u_long intrcnt[NIRQ];
81 char intrnames[(NIRQ * INTRNAME_LEN) + 1];
82 size_t sintrcnt = sizeof(intrcnt);
83 size_t sintrnames = sizeof(intrnames);
84
85 /*
86  * Pre-format intrnames into an array of fixed-size strings containing spaces.
87  * This allows us to avoid the need for an intermediate table of indices into
88  * the names and counts arrays, while still meeting the requirements and
89  * assumptions of vmstat(8) and the kdb "show intrcnt" command, the two
90  * consumers of this data.
91  */
92 static void
93 intr_init(void *unused)
94 {
95         int i;
96
97         for (i = 0; i < NIRQ; ++i) {
98                 snprintf(&intrnames[i * INTRNAME_LEN], INTRNAME_LEN, "%-*s",
99                     INTRNAME_LEN - 1, "");
100         }
101 }
102
103 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL);
104
105 #ifdef FDT
106 int
107 intr_fdt_map_irq(phandle_t iparent, pcell_t *intr, int icells)
108 {
109         fdt_pic_decode_t intr_decode;
110         phandle_t intr_parent;
111         int i, rv, interrupt, trig, pol;
112
113         intr_parent = OF_node_from_xref(iparent);
114         for (i = 0; i < icells; i++)
115                 intr[i] = cpu_to_fdt32(intr[i]);
116
117         for (i = 0; fdt_pic_table[i] != NULL; i++) {
118                 intr_decode = fdt_pic_table[i];
119                 rv = intr_decode(intr_parent, intr, &interrupt, &trig, &pol);
120
121                 if (rv == 0) {
122                         /* This was recognized as our PIC and decoded. */
123                         interrupt = FDT_MAP_IRQ(intr_parent, interrupt);
124                         return (interrupt);
125                 }
126         }
127
128         /* Not in table, so guess */
129         interrupt = FDT_MAP_IRQ(intr_parent, fdt32_to_cpu(intr[0]));
130
131         return (interrupt);
132 }
133 #endif
134
135 void
136 arm_setup_irqhandler(const char *name, driver_filter_t *filt,
137     void (*hand)(void*), void *arg, int irq, int flags, void **cookiep)
138 {
139         struct intr_event *event;
140         int error;
141
142         if (irq < 0 || irq >= NIRQ)
143                 return;
144         event = intr_events[irq];
145         if (event == NULL) {
146                 error = intr_event_create(&event, (void *)irq, 0, irq,
147                     (mask_fn)arm_mask_irq, (mask_fn)arm_unmask_irq,
148                     arm_post_filter, NULL, "intr%d:", irq);
149                 if (error)
150                         return;
151                 intr_events[irq] = event;
152                 snprintf(&intrnames[irq * INTRNAME_LEN], INTRNAME_LEN,
153                     "irq%d: %-*s", irq, INTRNAME_LEN - 1, name);
154         }
155         intr_event_add_handler(event, name, filt, hand, arg,
156             intr_priority(flags), flags, cookiep);
157 }
158
159 int
160 arm_remove_irqhandler(int irq, void *cookie)
161 {
162         struct intr_event *event;
163         int error;
164
165         event = intr_events[irq];
166         arm_mask_irq(irq);
167
168         error = intr_event_remove_handler(cookie);
169
170         if (!TAILQ_EMPTY(&event->ie_handlers))
171                 arm_unmask_irq(irq);
172         return (error);
173 }
174
175 void dosoftints(void);
176 void
177 dosoftints(void)
178 {
179 }
180
181 void
182 intr_irq_handler(struct trapframe *frame)
183 {
184         struct intr_event *event;
185         int i;
186
187         PCPU_INC(cnt.v_intr);
188         i = -1;
189         while ((i = arm_get_next_irq(i)) != -1) {
190                 intrcnt[i]++;
191                 event = intr_events[i];
192                 if (intr_event_handle(event, frame) != 0) {
193                         /* XXX: Log stray IRQs */
194                         arm_mask_irq(i);
195                 }
196         }
197 #ifdef HWPMC_HOOKS
198         if (pmc_hook && (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
199                 pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, frame);
200 #endif
201 }