2 * Copyright 2003-2011 Netlogic Microsystems (Netlogic). All rights
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY Netlogic Microsystems ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/interrupt.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
43 #include <machine/cpu.h>
44 #include <machine/cpufunc.h>
45 #include <machine/cpuinfo.h>
46 #include <machine/cpuregs.h>
47 #include <machine/frame.h>
48 #include <machine/intr_machdep.h>
49 #include <machine/md_var.h>
50 #include <machine/trap.h>
51 #include <machine/hwfunc.h>
53 #include <mips/nlm/hal/haldefs.h>
54 #include <mips/nlm/hal/iomap.h>
55 #include <mips/nlm/hal/mips-extns.h>
56 #include <mips/nlm/interrupt.h>
57 #include <mips/nlm/hal/pic.h>
58 #include <mips/nlm/xlp.h>
61 void (*bus_ack)(int, void *); /* Additional ack */
62 void *bus_ack_arg; /* arg for additional ack */
63 struct intr_event *ie; /* event corresponding to intr */
68 static struct xlp_intrsrc xlp_interrupts[XLR_MAX_INTR];
69 static mips_intrcnt_t mips_intr_counters[XLR_MAX_INTR];
70 static int intrcnt_index;
73 xlp_irq_to_irt(int irq)
80 offset = XLP_IO_UART_OFFSET(0, irq - PIC_UART_0_IRQ);
81 return (xlp_socdev_irt(offset));
86 offset = XLP_IO_PCIE_OFFSET(0, irq - PIC_PCIE_0_IRQ);
87 return (xlp_socdev_irt(offset));
93 offset = XLP_IO_USB_OFFSET(0, irq - PIC_USB_0_IRQ);
94 return (xlp_socdev_irt(offset));
97 offset = XLP_IO_I2C0_OFFSET(0);
98 return (xlp_socdev_irt(offset) + irq - PIC_I2C_0_IRQ);
100 printf("ERROR: %s: unknown irq %d\n", __func__, irq);
106 xlp_enable_irq(int irq)
110 eimr = nlm_read_c0_eimr();
111 nlm_write_c0_eimr(eimr | (1ULL << irq));
115 cpu_establish_softintr(const char *name, driver_filter_t * filt,
116 void (*handler) (void *), void *arg, int irq, int flags,
120 panic("Soft interrupts unsupported!\n");
124 xlp_post_filter(void *source)
126 struct xlp_intrsrc *src = source;
129 src->bus_ack(src->irq, src->bus_ack_arg);
130 nlm_pic_ack(xlp_pic_base, src->irt);
134 xlp_pre_ithread(void *source)
136 struct xlp_intrsrc *src = source;
139 src->bus_ack(src->irq, src->bus_ack_arg);
143 xlp_post_ithread(void *source)
145 struct xlp_intrsrc *src = source;
147 nlm_pic_ack(xlp_pic_base, src->irt);
151 xlp_set_bus_ack(int irq, void (*ack)(int, void *), void *arg)
153 struct xlp_intrsrc *src;
155 KASSERT(irq > 0 && irq <= XLR_MAX_INTR,
156 ("%s called for bad hard intr %d", __func__, irq));
158 /* no locking needed - this will called early in boot */
159 src = &xlp_interrupts[irq];
160 KASSERT(src->ie != NULL,
161 ("%s called after IRQ enable for %d.", __func__, irq));
162 src->bus_ack_arg = arg;
167 cpu_establish_hardintr(const char *name, driver_filter_t * filt,
168 void (*handler) (void *), void *arg, int irq, int flags,
171 struct intr_event *ie; /* descriptor for the IRQ */
172 struct xlp_intrsrc *src = NULL;
175 KASSERT(irq > 0 && irq <= XLR_MAX_INTR ,
176 ("%s called for bad hard intr %d", __func__, irq));
179 * Locking - not needed now, because we do this only on
182 src = &xlp_interrupts[irq];
186 * PIC based interrupts need ack in PIC, and some SoC
187 * components need additional acks (e.g. PCI)
189 if (XLP_IRQ_IS_PICINTR(irq))
190 errcode = intr_event_create(&ie, src, 0, irq,
191 xlp_pre_ithread, xlp_post_ithread, xlp_post_filter,
192 NULL, "hard intr%d:", irq);
195 panic("Unsupported non filter percpu intr %d", irq);
196 errcode = intr_event_create(&ie, src, 0, irq,
197 NULL, NULL, NULL, NULL, "hard intr%d:", irq);
200 printf("Could not create event for intr %d\n", irq);
206 if (XLP_IRQ_IS_PICINTR(irq)) {
207 /* Set all irqs to CPU 0 for now */
208 src->irt = xlp_irq_to_irt(irq);
209 nlm_pic_write_irt_direct(xlp_pic_base, src->irt, 1, 0,
210 PIC_LOCAL_SCHEDULING, irq, 0);
213 intr_event_add_handler(ie, name, filt, handler, arg,
214 intr_priority(flags), flags, cookiep);
219 cpu_intr(struct trapframe *tf)
221 struct intr_event *ie;
227 /* find a list of enabled interrupts */
228 eirr = nlm_read_c0_eirr();
229 eimr = nlm_read_c0_eimr();
237 * No need to clear the EIRR here as the handler writes to
238 * compare which ACKs the interrupt.
240 if (eirr & (1 << IRQ_TIMER)) {
241 intr_event_handle(xlp_interrupts[IRQ_TIMER].ie, tf);
246 /* FIXME sched pin >? LOCK>? */
247 for (i = sizeof(eirr) * 8 - 1; i >= 0; i--) {
248 if ((eirr & (1ULL << i)) == 0)
251 ie = xlp_interrupts[i].ie;
252 /* Don't account special IRQs */
258 mips_intrcnt_inc(mips_intr_counters[i]);
261 /* Ack the IRQ on the CPU */
262 nlm_write_c0_eirr(1ULL << i);
263 if (intr_event_handle(ie, tf) != 0) {
264 printf("stray interrupt %d\n", i);
271 mips_intrcnt_setname(mips_intrcnt_t counter, const char *name)
273 int idx = counter - intrcnt;
275 KASSERT(counter != NULL, ("mips_intrcnt_setname: NULL counter"));
277 snprintf(intrnames + (MAXCOMLEN + 1) * idx,
278 MAXCOMLEN + 1, "%-*s", MAXCOMLEN, name);
282 mips_intrcnt_create(const char* name)
284 mips_intrcnt_t counter = &intrcnt[intrcnt_index++];
286 mips_intrcnt_setname(counter, name);
291 cpu_init_interrupts()
294 char name[MAXCOMLEN + 1];
297 * Initialize all available vectors so spare IRQ
298 * would show up in systat output
300 for (i = 0; i < XLR_MAX_INTR; i++) {
301 snprintf(name, MAXCOMLEN + 1, "int%d:", i);
302 mips_intr_counters[i] = mips_intrcnt_create(name);
306 static int xlp_pic_probe(device_t);
307 static int xlp_pic_attach(device_t);
310 xlp_pic_probe(device_t dev)
313 if (!ofw_bus_is_compatible(dev, "netlogic,xlp-pic"))
315 device_set_desc(dev, "XLP PIC");
320 xlp_pic_attach(device_t dev)
326 static device_method_t xlp_pic_methods[] = {
327 DEVMETHOD(device_probe, xlp_pic_probe),
328 DEVMETHOD(device_attach, xlp_pic_attach),
333 static driver_t xlp_pic_driver = {
339 static devclass_t xlp_pic_devclass;
340 DRIVER_MODULE(xlp_pic, simplebus, xlp_pic_driver, xlp_pic_devclass, 0, 0);