1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/* $OpenBSD: mvmpic.c,v 1.3 2018/12/07 21:33:28 patrick Exp $ */
/*
* Copyright (c) 2007,2009,2011 Dale Rahn <drahn@openbsd.org>
* Copyright (c) 2015 Patrick Wildt <patrick@blueri.se>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <sys/evcount.h>
#include <arm/cpufunc.h>
#include <machine/bus.h>
#include <machine/fdt.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/fdt.h>
#define MPIC_CTRL 0x000 /* control register */
#define MPIC_CTRL_PRIO_EN 0x1
#define MPIC_SOFTINT 0x004 /* software triggered interrupt register */
#define MPIC_INTERR 0x020 /* SOC main interrupt error cause register */
#define MPIC_ISE 0x030 /* interrupt set enable */
#define MPIC_ICE 0x034 /* interrupt clear enable */
#define MPIC_ISCR(x) (0x100 + (4 * x)) /* interrupt x source control register */
#define MPIC_ISCR_PRIO_SHIFT 24
#define MPIC_ISCR_INTEN (1 << 28)
#define MPIC_DOORBELL_CAUSE 0x008
#define MPIC_CTP 0x040 /* current task priority */
#define MPIC_CTP_SHIFT 28
#define MPIC_IACK 0x044 /* interrupt acknowledge */
#define MPIC_ISM 0x048 /* set mask */
#define MPIC_ICM 0x04c /* clear mask */
struct mpic_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_m_ioh, sc_c_ioh;
int sc_node;
struct intrhand **sc_handlers;
int sc_ipl;
int sc_nintr;
struct evcount sc_spur;
struct interrupt_controller sc_intc;
void *sc_ih;
};
struct intrhand {
int (*ih_func)(void *); /* handler */
void *ih_arg; /* arg for handler */
int ih_ipl; /* IPL_* */
int ih_irq; /* IRQ number */
struct evcount ih_count;
char *ih_name;
void *ih_sc;
};
int mpic_match(struct device *, void *, void *);
void mpic_attach(struct device *, struct device *, void *);
void mpic_calc_mask(struct mpic_softc *);
void *mpic_intr_establish(void *, int *, int,
int (*)(void *), void *, char *);
void mpic_intr_disestablish(void *);
int mpic_intr(void *);
void mpic_set_priority(struct mpic_softc *, int, int);
void mpic_intr_enable(struct mpic_softc *, int);
void mpic_intr_disable(struct mpic_softc *, int);
struct cfattach mvmpic_ca = {
sizeof (struct mpic_softc), mpic_match, mpic_attach
};
struct cfdriver mvmpic_cd = {
NULL, "mvmpic", DV_DULL
};
int
mpic_match(struct device *parent, void *cfdata, void *aux)
{
struct fdt_attach_args *faa = aux;
return OF_is_compatible(faa->fa_node, "marvell,mpic");
}
void
mpic_attach(struct device *parent, struct device *self, void *args)
{
struct mpic_softc *sc = (struct mpic_softc *)self;
struct fdt_attach_args *faa = args;
int i;
sc->sc_node = faa->fa_node;
sc->sc_iot = faa->fa_iot;
if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
faa->fa_reg[0].size, 0, &sc->sc_m_ioh))
panic("%s: main bus_space_map failed!", __func__);
if (bus_space_map(sc->sc_iot, faa->fa_reg[1].addr,
faa->fa_reg[1].size, 0, &sc->sc_c_ioh))
panic("%s: cpu bus_space_map failed!", __func__);
evcount_attach(&sc->sc_spur, "irq1023/spur", NULL);
sc->sc_nintr = (bus_space_read_4(sc->sc_iot, sc->sc_m_ioh,
MPIC_CTRL) >> 2) & 0x3ff;
printf(" nirq %d\n", sc->sc_nintr);
/* Disable all interrupts */
for (i = 0; i < sc->sc_nintr; i++) {
bus_space_write_4(sc->sc_iot, sc->sc_m_ioh, MPIC_ICE, i);
bus_space_write_4(sc->sc_iot, sc->sc_c_ioh, MPIC_ISM, i);
}
/* Clear pending IPIs */
bus_space_write_4(sc->sc_iot, sc->sc_c_ioh, MPIC_DOORBELL_CAUSE, 0);
/* Enable hardware priorization selection */
bus_space_write_4(sc->sc_iot, sc->sc_m_ioh, MPIC_CTRL,
MPIC_CTRL_PRIO_EN);
/* Always allow everything. */
bus_space_write_4(sc->sc_iot, sc->sc_c_ioh, MPIC_CTP,
(bus_space_read_4(sc->sc_iot, sc->sc_c_ioh, MPIC_CTP) &
~(0xf << MPIC_CTP_SHIFT)) | (IPL_NONE << MPIC_CTP_SHIFT));
sc->sc_handlers = mallocarray(sc->sc_nintr,
sizeof(*sc->sc_handlers), M_DEVBUF, M_ZERO | M_NOWAIT);
sc->sc_ipl = IPL_NONE;
mpic_calc_mask(sc);
sc->sc_intc.ic_node = faa->fa_node;
sc->sc_intc.ic_cookie = sc;
sc->sc_intc.ic_establish = mpic_intr_establish;
arm_intr_register_fdt(&sc->sc_intc);
}
void
mpic_set_priority(struct mpic_softc *sc, int irq, int pri)
{
bus_space_write_4(sc->sc_iot, sc->sc_m_ioh, MPIC_ISCR(irq),
(bus_space_read_4(sc->sc_iot, sc->sc_m_ioh, MPIC_ISCR(irq)) &
~(0xf << MPIC_ISCR_PRIO_SHIFT)) | (pri << MPIC_ISCR_PRIO_SHIFT));
}
void
mpic_intr_enable(struct mpic_softc *sc, int irq)
{
bus_space_write_4(sc->sc_iot, sc->sc_m_ioh, MPIC_ISE, irq);
bus_space_write_4(sc->sc_iot, sc->sc_c_ioh, MPIC_ICM, irq);
}
void
mpic_intr_disable(struct mpic_softc *sc, int irq)
{
bus_space_write_4(sc->sc_iot, sc->sc_m_ioh, MPIC_ICE, irq);
bus_space_write_4(sc->sc_iot, sc->sc_c_ioh, MPIC_ISM, irq);
}
void
mpic_calc_mask(struct mpic_softc *sc)
{
struct intrhand *ih;
int irq;
int max = IPL_NONE;
int min = IPL_HIGH;
for (irq = 0; irq < sc->sc_nintr; irq++) {
ih = sc->sc_handlers[irq];
if (ih == NULL)
continue;
if (ih->ih_ipl > max)
max = ih->ih_ipl;
if (ih->ih_ipl < min)
min = ih->ih_ipl;
}
if (max == IPL_NONE)
min = IPL_NONE;
if (sc->sc_ipl != min) {
sc->sc_ipl = min;
if (sc->sc_ih != NULL)
arm_intr_disestablish_fdt(sc->sc_ih);
if (sc->sc_ipl != IPL_NONE)
sc->sc_ih = arm_intr_establish_fdt(sc->sc_node,
sc->sc_ipl, mpic_intr, sc, sc->sc_dev.dv_xname);
}
}
int
mpic_intr(void *cookie)
{
struct mpic_softc *sc = cookie;
struct intrhand *ih;
int irq, s;
irq = bus_space_read_4(sc->sc_iot, sc->sc_c_ioh, MPIC_IACK) & 0x3ff;
if (irq == 1023) {
sc->sc_spur.ec_count++;
return 1;
}
if (irq >= sc->sc_nintr)
return 1;
if ((ih = sc->sc_handlers[irq]) != NULL) {
s = splraise(ih->ih_ipl);
if (ih->ih_func(ih->ih_arg))
ih->ih_count.ec_count++;
splx(s);
}
return 1;
}
void *
mpic_intr_establish(void *cookie, int *cells, int level,
int (*func)(void *), void *arg, char *name)
{
struct mpic_softc *sc = cookie;
struct intrhand *ih;
int psw;
int irqno = cells[0];
if (irqno < 0 || irqno >= sc->sc_nintr)
panic("%s: bogus irqnumber %d: %s", __func__,
irqno, name);
if (sc->sc_handlers[irqno] != NULL)
panic("%s: irq %d already registered" , __func__,
irqno);
ih = malloc(sizeof(*ih), M_DEVBUF, M_WAITOK);
ih->ih_func = func;
ih->ih_arg = arg;
ih->ih_ipl = level;
ih->ih_irq = irqno;
ih->ih_name = name;
ih->ih_sc = sc;
psw = disable_interrupts(PSR_I);
sc->sc_handlers[irqno] = ih;
if (name != NULL)
evcount_attach(&ih->ih_count, name, &ih->ih_irq);
#ifdef DEBUG_INTC
printf("%s: irq %d level %d [%s]\n", __func__, irqno, level, name);
#endif
mpic_calc_mask(sc);
mpic_set_priority(sc, irqno, level);
mpic_intr_enable(sc, irqno);
restore_interrupts(psw);
return (ih);
}
void
mpic_intr_disestablish(void *cookie)
{
struct intrhand *ih = cookie;
struct mpic_softc *sc = ih->ih_sc;
int psw;
psw = disable_interrupts(PSR_I);
#ifdef DEBUG_INTC
printf("%s: irq %d ipl %d [%s]\n", __func__, ih->ih_irq, ih->ih_ipl,
ih->ih_name);
#endif
mpic_intr_disable(sc, ih->ih_irq);
sc->sc_handlers[ih->ih_irq] = NULL;
if (ih->ih_name != NULL)
evcount_detach(&ih->ih_count);
free(ih, M_DEVBUF, sizeof(*ih));
mpic_calc_mask(sc);
restore_interrupts(psw);
}
|