loongson_intr.c revision 1.4 1 /* $NetBSD: loongson_intr.c,v 1.4 2014/03/29 19:28:28 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: loongson_intr.c,v 1.4 2014/03/29 19:28:28 christos Exp $");
34
35 #define __INTR_PRIVATE
36
37 #include <sys/param.h>
38 #include <sys/device.h>
39 #include <sys/cpu.h>
40 #include <sys/intr.h>
41 #include <sys/bus.h>
42 #include <sys/malloc.h>
43
44 #include <mips/mips3_clock.h>
45 #include <machine/locore.h>
46
47 #include <evbmips/loongson/autoconf.h>
48 #include <evbmips/loongson/loongson_intr.h>
49
50 #include <mips/locore.h>
51
52 #include <mips/bonito/bonitoreg.h>
53 #include <mips/bonito/bonitovar.h>
54
55 #include <dev/pci/pciidereg.h>
56 #include <dev/isa/isavar.h>
57
58 #include "isa.h"
59
60 #ifdef INTR_DEBUG
61 #define DPRINTF(x) printf x
62 #else
63 #define DPRINTF(x)
64 #endif
65
66 struct bonito_intrhead bonito_intrhead[BONITO_NINTS];
67
68 /*
69 * This is a mask of bits to clear in the SR when we go to a
70 * given hardware interrupt priority level.
71 */
72 static const struct ipl_sr_map loongson_ipl_sr_map = {
73 .sr_bits = {
74 [IPL_NONE] = 0,
75 [IPL_SOFTCLOCK] = MIPS_SOFT_INT_MASK_0,
76 [IPL_SOFTNET] = MIPS_SOFT_INT_MASK_0 | MIPS_SOFT_INT_MASK_1,
77 [IPL_VM] =
78 MIPS_SOFT_INT_MASK_0 | MIPS_SOFT_INT_MASK_1 |
79 MIPS_INT_MASK_0 |
80 MIPS_INT_MASK_1 |
81 MIPS_INT_MASK_2 |
82 MIPS_INT_MASK_3 |
83 MIPS_INT_MASK_4,
84 [IPL_SCHED] =
85 MIPS_SOFT_INT_MASK_0 | MIPS_SOFT_INT_MASK_1 |
86 MIPS_INT_MASK_0 |
87 MIPS_INT_MASK_1 |
88 MIPS_INT_MASK_2 |
89 MIPS_INT_MASK_3 |
90 MIPS_INT_MASK_4 |
91 MIPS_INT_MASK_5,
92 [IPL_DDB] = MIPS_INT_MASK,
93 [IPL_HIGH] = MIPS_INT_MASK,
94 },
95 };
96
97
98 void
99 evbmips_intr_init(void)
100 {
101 const struct bonito_config * const bc = sys_platform->bonito_config;
102 const struct bonito_irqmap *irqmap;
103 int i;
104
105 ipl_sr_map = loongson_ipl_sr_map;
106
107 for (i = 0; i < BONITO_NDIRECT; i++) {
108 irqmap = &sys_platform->irq_map[i];
109 if (irqmap->name == NULL)
110 continue;
111 DPRINTF(("attach %d %s\n", i, irqmap->name));
112 evcnt_attach_dynamic(&bonito_intrhead[i].intr_count,
113 EVCNT_TYPE_INTR, NULL, "bonito", irqmap->name);
114 LIST_INIT(&bonito_intrhead[i].intrhand_head);
115 }
116
117 REGVAL(BONITO_GPIOIE) = bc->bc_gpioIE;
118 REGVAL(BONITO_INTEDGE) = bc->bc_intEdge;
119 /* REGVAL(BONITO_INTSTEER) = bc->bc_intSteer; XXX */
120 REGVAL(BONITO_INTPOL) = bc->bc_intPol;
121 REGVAL(BONITO_INTENCLR) = 0xffffffff;
122 (void)REGVAL(BONITO_INTENCLR);
123
124 if (sys_platform->isa_chipset != NULL) {
125 int irq;
126 static char irqstr[8];
127 for (irq = 0; irq < BONITO_NISA; irq++) {
128 i = BONITO_ISA_IRQ(irq);
129 snprintf(irqstr, sizeof(irqstr), "irq %d", irq);
130 DPRINTF(("attach %d %d %s\n", i, irq, irqstr));
131 evcnt_attach_dynamic(&bonito_intrhead[i].intr_count,
132 EVCNT_TYPE_INTR, NULL, "isa", irqstr);
133 LIST_INIT(&bonito_intrhead[i].intrhand_head);
134 }
135 }
136 }
137
138 void
139 evbmips_iointr(int ppl, vaddr_t pc, uint32_t ipending)
140 {
141 struct evbmips_intrhand *ih;
142 int irq;
143 uint32_t isr0, isr, imr;
144
145 /*
146 * Read the interrupt pending registers, mask them with the
147 * ones we have enabled, and service them in order of decreasing
148 * priority.
149 */
150 isr0 = REGVAL(BONITO_INTISR);
151 imr = REGVAL(BONITO_INTEN);
152
153 if (ipending & sys_platform->bonito_mips_intr) {
154 isr = isr0 & imr & LOONGSON_INTRMASK_LVL4;
155
156 REGVAL(BONITO_INTENCLR) = isr;
157 (void)REGVAL(BONITO_INTENCLR);
158
159 for (irq = 0; irq < BONITO_NINTS; irq++) {
160 if ((isr & (1 << irq)) == 0)
161 continue;
162 bonito_intrhead[irq].intr_count.ev_count++;
163 LIST_FOREACH (ih,
164 &bonito_intrhead[irq].intrhand_head, ih_q) {
165 (*ih->ih_func)(ih->ih_arg);
166 }
167 }
168 REGVAL(BONITO_INTENSET) = isr;
169 (void)REGVAL(BONITO_INTENSET);
170 }
171 if (isr0 & LOONGSON_INTRMASK_INT0)
172 sys_platform->isa_intr(ppl, pc, ipending);
173 }
174
175 void *
176 loongson_pciide_compat_intr_establish(void *v, device_t dev,
177 const struct pci_attach_args *pa, int chan, int (*func)(void *), void *arg)
178 {
179 pci_chipset_tag_t pc = pa->pa_pc;
180 void *cookie;
181 int bus, irq;
182 char buf[PCI_INTRSTR_LEN];
183
184 pci_decompose_tag(pc, pa->pa_tag, &bus, NULL, NULL);
185
186 /*
187 * If this isn't PCI bus #0, all bets are off.
188 */
189 if (bus != 0)
190 return (NULL);
191
192 irq = PCIIDE_COMPAT_IRQ(chan);
193 #if NISA > 0
194 if (sys_platform->isa_chipset != NULL)
195 cookie = isa_intr_establish(sys_platform->isa_chipset, irq,
196 IST_EDGE, IPL_BIO, func, arg);
197 else
198 #endif
199 cookie = NULL;
200 if (cookie == NULL)
201 return (NULL);
202 printf("%s: %s channel interrupting at %s\n", device_xname(dev),
203 PCIIDE_CHANNEL_NAME(chan),
204 isa_intr_string(sys_platform->isa_chipset, irq, buf, sizeof(buf)));
205 return (cookie);
206 }
207
208 int
209 loongson_pci_intr_map(const struct pci_attach_args *pa,
210 pci_intr_handle_t *ihp)
211 {
212 pcitag_t bustag = pa->pa_intrtag;
213 int buspin = pa->pa_intrpin;
214 pci_chipset_tag_t pc = pa->pa_pc;
215 int device, function;
216
217 if (buspin == 0) {
218 /* No IRQ used. */
219 return (1);
220 }
221
222 if (buspin > 4) {
223 printf("loongson_pci_intr_map: bad interrupt pin %d\n",
224 buspin);
225 return (1);
226 }
227
228 pci_decompose_tag(pc, bustag, NULL, &device, &function);
229 return (sys_platform->p_pci_intr_map(device, function, buspin, ihp));
230 }
231
232 const char *
233 loongson_pci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
234 {
235
236 const struct bonito_config *bc = v;
237 return loongson_intr_string(bc, ih, buf, len);
238 }
239
240 const struct evcnt *
241 loongson_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
242 {
243
244 return &bonito_intrhead[ih].intr_count;
245 }
246
247 void *
248 loongson_pci_intr_establish(void *v, pci_intr_handle_t ih, int level,
249 int (*func)(void *), void *arg)
250 {
251 if (BONITO_IRQ_IS_ISA(ih)) {
252 if (sys_platform->isa_chipset == NULL)
253 panic("ISA interrupt on non-ISA platform");
254 return sys_platform->isa_chipset->ic_intr_establish(v,
255 BONITO_IRQ_TO_ISA(ih), IST_EDGE, level, func, arg);
256 }
257 return evbmips_intr_establish(ih, func, arg);
258 }
259
260 void
261 loongson_pci_intr_disestablish(void *v, void *cookie)
262 {
263 struct evbmips_intrhand *ih = cookie;
264 if (BONITO_IRQ_IS_ISA(ih->ih_irq)) {
265 if (sys_platform->isa_chipset == NULL)
266 panic("ISA interrupt on non-ISA platform");
267 sys_platform->isa_chipset->ic_intr_disestablish(v, ih);
268 return;
269 }
270 return (evbmips_intr_disestablish(cookie));
271 }
272
273 void
274 loongson_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz,
275 int *iline)
276 {
277
278 /*
279 * We actually don't need to do anything; everything is handled
280 * in pci_intr_map().
281 */
282 *iline = 0;
283 }
284
285
286 void *
287 evbmips_intr_establish(int irq, int (*func)(void *), void *arg)
288 {
289 struct evbmips_intrhand *ih;
290 int s;
291
292 KASSERT(irq < BONITO_NINTS);
293 DPRINTF(("loongson_intr_establish %d %p", irq, func));
294
295 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT|M_ZERO);
296 if (ih == NULL)
297 return NULL;
298
299 ih->ih_func = func;
300 ih->ih_arg = arg;
301 ih->ih_irq = irq;
302 DPRINTF((" ih %p", ih));
303
304 /* link it into tables */
305 s = splhigh();
306 LIST_INSERT_HEAD(&bonito_intrhead[irq].intrhand_head, ih, ih_q);
307 /* and enable it */
308 DPRINTF((" inten 0x%x", REGVAL(BONITO_INTEN)));
309 if (bonito_intrhead[irq].refcnt++ == 0 && !BONITO_IRQ_IS_ISA(irq))
310 REGVAL(BONITO_INTENSET) = (1 << ih->ih_irq);
311 DPRINTF((" now 0x%x\n", REGVAL(BONITO_INTEN)));
312 splx(s);
313
314 return (ih);
315 }
316
317 void
318 evbmips_intr_disestablish(void *cookie)
319 {
320 struct evbmips_intrhand *ih = cookie;
321 int s;
322
323 s = splhigh();
324 LIST_REMOVE(ih, ih_q);
325 bonito_intrhead[ih->ih_irq].refcnt--;
326 if (bonito_intrhead[ih->ih_irq].refcnt == 0 &&
327 !BONITO_IRQ_IS_ISA(ih->ih_irq))
328 REGVAL(BONITO_INTENCLR) = (1 << ih->ih_irq);
329 splx(s);
330 free(ih, M_DEVBUF);
331 }
332
333 const char *
334 loongson_intr_string(const struct bonito_config *bc, int irq, char *buf, size_t len)
335 {
336 if (BONITO_IRQ_IS_ISA(irq))
337 snprintf(buf, len, "isa irq %d", BONITO_IRQ_TO_ISA(irq));
338 else
339 strlcpy(buf, sys_platform->irq_map[irq].name, len);
340 return buf;
341 }
342