2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2003-2011 Netlogic Microsystems (Netlogic). All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
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
15 * the documentation and/or other materials provided with the
18 * THIS SOFTWARE IS PROVIDED BY Netlogic Microsystems ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/interrupt.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
45 #include <machine/cpu.h>
46 #include <machine/cpufunc.h>
47 #include <machine/cpuinfo.h>
48 #include <machine/cpuregs.h>
49 #include <machine/frame.h>
50 #include <machine/intr_machdep.h>
51 #include <machine/md_var.h>
52 #include <machine/trap.h>
53 #include <machine/hwfunc.h>
55 #include <mips/nlm/hal/haldefs.h>
56 #include <mips/nlm/hal/iomap.h>
57 #include <mips/nlm/hal/mips-extns.h>
58 #include <mips/nlm/interrupt.h>
59 #include <mips/nlm/hal/pic.h>
60 #include <mips/nlm/xlp.h>
63 void (*bus_ack)(int, void *); /* Additional ack */
64 void *bus_ack_arg; /* arg for additional ack */
65 struct intr_event *ie; /* event corresponding to intr */
70 static struct xlp_intrsrc xlp_interrupts[XLR_MAX_INTR];
71 static mips_intrcnt_t mips_intr_counters[XLR_MAX_INTR];
72 static int intrcnt_index;
75 xlp_irq_to_irt(int irq)
82 offset = XLP_IO_UART_OFFSET(0, irq - PIC_UART_0_IRQ);
83 return (xlp_socdev_irt(offset));
88 offset = XLP_IO_PCIE_OFFSET(0, irq - PIC_PCIE_0_IRQ);
89 return (xlp_socdev_irt(offset));
95 offset = XLP_IO_USB_OFFSET(0, irq - PIC_USB_0_IRQ);
96 return (xlp_socdev_irt(offset));
99 offset = XLP_IO_I2C0_OFFSET(0);
100 return (xlp_socdev_irt(offset) + irq - PIC_I2C_0_IRQ);
102 printf("ERROR: %s: unknown irq %d\n", __func__, irq);
108 xlp_enable_irq(int irq)
112 eimr = nlm_read_c0_eimr();
113 nlm_write_c0_eimr(eimr | (1ULL << irq));
117 cpu_establish_softintr(const char *name, driver_filter_t * filt,
118 void (*handler) (void *), void *arg, int irq, int flags,
122 panic("Soft interrupts unsupported!\n");
126 xlp_post_filter(void *source)
128 struct xlp_intrsrc *src = source;
131 src->bus_ack(src->irq, src->bus_ack_arg);
132 nlm_pic_ack(xlp_pic_base, src->irt);
136 xlp_pre_ithread(void *source)
138 struct xlp_intrsrc *src = source;
141 src->bus_ack(src->irq, src->bus_ack_arg);
145 xlp_post_ithread(void *source)
147 struct xlp_intrsrc *src = source;
149 nlm_pic_ack(xlp_pic_base, src->irt);
153 xlp_set_bus_ack(int irq, void (*ack)(int, void *), void *arg)
155 struct xlp_intrsrc *src;
157 KASSERT(irq > 0 && irq <= XLR_MAX_INTR,
158 ("%s called for bad hard intr %d", __func__, irq));
160 /* no locking needed - this will called early in boot */
161 src = &xlp_interrupts[irq];
162 KASSERT(src->ie != NULL,
163 ("%s called after IRQ enable for %d.", __func__, irq));
164 src->bus_ack_arg = arg;
169 cpu_establish_hardintr(const char *name, driver_filter_t * filt,
170 void (*handler) (void *), void *arg, int irq, int flags,
173 struct intr_event *ie; /* descriptor for the IRQ */
174 struct xlp_intrsrc *src = NULL;
177 KASSERT(irq > 0 && irq <= XLR_MAX_INTR ,
178 ("%s called for bad hard intr %d", __func__, irq));
181 * Locking - not needed now, because we do this only on
184 src = &xlp_interrupts[irq];
188 * PIC based interrupts need ack in PIC, and some SoC
189 * components need additional acks (e.g. PCI)
191 if (XLP_IRQ_IS_PICINTR(irq))
192 errcode = intr_event_create(&ie, src, 0, irq,
193 xlp_pre_ithread, xlp_post_ithread, xlp_post_filter,
194 NULL, "hard intr%d:", irq);
197 panic("Unsupported non filter percpu intr %d", irq);
198 errcode = intr_event_create(&ie, src, 0, irq,
199 NULL, NULL, NULL, NULL, "hard intr%d:", irq);
202 printf("Could not create event for intr %d\n", irq);
208 if (XLP_IRQ_IS_PICINTR(irq)) {
209 /* Set all irqs to CPU 0 for now */
210 src->irt = xlp_irq_to_irt(irq);
211 nlm_pic_write_irt_direct(xlp_pic_base, src->irt, 1, 0,
212 PIC_LOCAL_SCHEDULING, irq, 0);
215 intr_event_add_handler(ie, name, filt, handler, arg,
216 intr_priority(flags), flags, cookiep);
221 cpu_intr(struct trapframe *tf)
223 struct intr_event *ie;
229 /* find a list of enabled interrupts */
230 eirr = nlm_read_c0_eirr();
231 eimr = nlm_read_c0_eimr();
239 * No need to clear the EIRR here as the handler writes to
240 * compare which ACKs the interrupt.
242 if (eirr & (1 << IRQ_TIMER)) {
243 intr_event_handle(xlp_interrupts[IRQ_TIMER].ie, tf);
248 /* FIXME sched pin >? LOCK>? */
249 for (i = sizeof(eirr) * 8 - 1; i >= 0; i--) {
250 if ((eirr & (1ULL << i)) == 0)
253 ie = xlp_interrupts[i].ie;
254 /* Don't account special IRQs */
260 mips_intrcnt_inc(mips_intr_counters[i]);
263 /* Ack the IRQ on the CPU */
264 nlm_write_c0_eirr(1ULL << i);
265 if (intr_event_handle(ie, tf) != 0) {
266 printf("stray interrupt %d\n", i);
273 mips_intrcnt_setname(mips_intrcnt_t counter, const char *name)
275 int idx = counter - intrcnt;
277 KASSERT(counter != NULL, ("mips_intrcnt_setname: NULL counter"));
279 snprintf(intrnames + (MAXCOMLEN + 1) * idx,
280 MAXCOMLEN + 1, "%-*s", MAXCOMLEN, name);
284 mips_intrcnt_create(const char* name)
286 mips_intrcnt_t counter = &intrcnt[intrcnt_index++];
288 mips_intrcnt_setname(counter, name);
293 cpu_init_interrupts()
296 char name[MAXCOMLEN + 1];
299 * Initialize all available vectors so spare IRQ
300 * would show up in systat output
302 for (i = 0; i < XLR_MAX_INTR; i++) {
303 snprintf(name, MAXCOMLEN + 1, "int%d:", i);
304 mips_intr_counters[i] = mips_intrcnt_create(name);
308 static int xlp_pic_probe(device_t);
309 static int xlp_pic_attach(device_t);
312 xlp_pic_probe(device_t dev)
315 if (!ofw_bus_is_compatible(dev, "netlogic,xlp-pic"))
317 device_set_desc(dev, "XLP PIC");
322 xlp_pic_attach(device_t dev)
328 static device_method_t xlp_pic_methods[] = {
329 DEVMETHOD(device_probe, xlp_pic_probe),
330 DEVMETHOD(device_attach, xlp_pic_attach),
335 static driver_t xlp_pic_driver = {
341 static devclass_t xlp_pic_devclass;
342 DRIVER_MODULE(xlp_pic, simplebus, xlp_pic_driver, xlp_pic_devclass, 0, 0);