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>
62 #define INTRCNT_COUNT 256
63 #define INTRNAME_LEN (2*MAXCOMLEN + 1)
65 MALLOC_DECLARE(M_MIPSINTR);
66 MALLOC_DEFINE(M_MIPSINTR, "mipsintr", "MIPS interrupt handling");
74 void (*bus_ack)(int, void *); /* Additional ack */
75 void *bus_ack_arg; /* arg for additional ack */
76 struct intr_event *ie; /* event corresponding to intr */
81 static struct xlp_intrsrc xlp_interrupts[XLR_MAX_INTR];
82 static mips_intrcnt_t mips_intr_counters[XLR_MAX_INTR];
83 static int intrcnt_index;
86 xlp_irq_to_irt(int irq)
93 offset = XLP_IO_UART_OFFSET(0, irq - PIC_UART_0_IRQ);
94 return (xlp_socdev_irt(offset));
99 offset = XLP_IO_PCIE_OFFSET(0, irq - PIC_PCIE_0_IRQ);
100 return (xlp_socdev_irt(offset));
106 offset = XLP_IO_USB_OFFSET(0, irq - PIC_USB_0_IRQ);
107 return (xlp_socdev_irt(offset));
110 offset = XLP_IO_I2C0_OFFSET(0);
111 return (xlp_socdev_irt(offset) + irq - PIC_I2C_0_IRQ);
113 printf("ERROR: %s: unknown irq %d\n", __func__, irq);
119 xlp_enable_irq(int irq)
123 eimr = nlm_read_c0_eimr();
124 nlm_write_c0_eimr(eimr | (1ULL << irq));
128 cpu_establish_softintr(const char *name, driver_filter_t * filt,
129 void (*handler) (void *), void *arg, int irq, int flags,
133 panic("Soft interrupts unsupported!\n");
137 xlp_post_filter(void *source)
139 struct xlp_intrsrc *src = source;
142 src->bus_ack(src->irq, src->bus_ack_arg);
143 nlm_pic_ack(xlp_pic_base, src->irt);
147 xlp_pre_ithread(void *source)
149 struct xlp_intrsrc *src = source;
152 src->bus_ack(src->irq, src->bus_ack_arg);
156 xlp_post_ithread(void *source)
158 struct xlp_intrsrc *src = source;
160 nlm_pic_ack(xlp_pic_base, src->irt);
164 xlp_set_bus_ack(int irq, void (*ack)(int, void *), void *arg)
166 struct xlp_intrsrc *src;
168 KASSERT(irq > 0 && irq <= XLR_MAX_INTR,
169 ("%s called for bad hard intr %d", __func__, irq));
171 /* no locking needed - this will called early in boot */
172 src = &xlp_interrupts[irq];
173 KASSERT(src->ie != NULL,
174 ("%s called after IRQ enable for %d.", __func__, irq));
175 src->bus_ack_arg = arg;
180 cpu_establish_hardintr(const char *name, driver_filter_t * filt,
181 void (*handler) (void *), void *arg, int irq, int flags,
184 struct intr_event *ie; /* descriptor for the IRQ */
185 struct xlp_intrsrc *src = NULL;
188 KASSERT(irq > 0 && irq <= XLR_MAX_INTR ,
189 ("%s called for bad hard intr %d", __func__, irq));
192 * Locking - not needed now, because we do this only on
195 src = &xlp_interrupts[irq];
199 * PIC based interrupts need ack in PIC, and some SoC
200 * components need additional acks (e.g. PCI)
202 if (XLP_IRQ_IS_PICINTR(irq))
203 errcode = intr_event_create(&ie, src, 0, irq,
204 xlp_pre_ithread, xlp_post_ithread, xlp_post_filter,
205 NULL, "hard intr%d:", irq);
208 panic("Unsupported non filter percpu intr %d", irq);
209 errcode = intr_event_create(&ie, src, 0, irq,
210 NULL, NULL, NULL, NULL, "hard intr%d:", irq);
213 printf("Could not create event for intr %d\n", irq);
219 if (XLP_IRQ_IS_PICINTR(irq)) {
220 /* Set all irqs to CPU 0 for now */
221 src->irt = xlp_irq_to_irt(irq);
222 nlm_pic_write_irt_direct(xlp_pic_base, src->irt, 1, 0,
223 PIC_LOCAL_SCHEDULING, irq, 0);
226 intr_event_add_handler(ie, name, filt, handler, arg,
227 intr_priority(flags), flags, cookiep);
232 cpu_intr(struct trapframe *tf)
234 struct intr_event *ie;
240 /* find a list of enabled interrupts */
241 eirr = nlm_read_c0_eirr();
242 eimr = nlm_read_c0_eimr();
250 * No need to clear the EIRR here as the handler writes to
251 * compare which ACKs the interrupt.
253 if (eirr & (1 << IRQ_TIMER)) {
254 intr_event_handle(xlp_interrupts[IRQ_TIMER].ie, tf);
259 /* FIXME sched pin >? LOCK>? */
260 for (i = sizeof(eirr) * 8 - 1; i >= 0; i--) {
261 if ((eirr & (1ULL << i)) == 0)
264 ie = xlp_interrupts[i].ie;
265 /* Don't account special IRQs */
271 mips_intrcnt_inc(mips_intr_counters[i]);
274 /* Ack the IRQ on the CPU */
275 nlm_write_c0_eirr(1ULL << i);
276 if (intr_event_handle(ie, tf) != 0) {
277 printf("stray interrupt %d\n", i);
284 mips_intrcnt_setname(mips_intrcnt_t counter, const char *name)
286 int idx = counter - intrcnt;
288 KASSERT(counter != NULL, ("mips_intrcnt_setname: NULL counter"));
290 snprintf(intrnames + (MAXCOMLEN + 1) * idx,
291 MAXCOMLEN + 1, "%-*s", MAXCOMLEN, name);
295 mips_intrcnt_create(const char* name)
297 mips_intrcnt_t counter = &intrcnt[intrcnt_index++];
299 mips_intrcnt_setname(counter, name);
304 cpu_init_interrupts()
307 char name[MAXCOMLEN + 1];
309 intrcnt = mallocarray(INTRCNT_COUNT, sizeof(u_long), M_MIPSINTR,
311 intrnames = mallocarray(INTRCNT_COUNT, INTRNAME_LEN, M_MIPSINTR,
313 sintrcnt = INTRCNT_COUNT * sizeof(u_long);
314 sintrnames = INTRCNT_COUNT * INTRNAME_LEN;
317 * Initialize all available vectors so spare IRQ
318 * would show up in systat output
320 for (i = 0; i < XLR_MAX_INTR; i++) {
321 snprintf(name, MAXCOMLEN + 1, "int%d:", i);
322 mips_intr_counters[i] = mips_intrcnt_create(name);
326 static int xlp_pic_probe(device_t);
327 static int xlp_pic_attach(device_t);
330 xlp_pic_probe(device_t dev)
333 if (!ofw_bus_is_compatible(dev, "netlogic,xlp-pic"))
335 device_set_desc(dev, "XLP PIC");
340 xlp_pic_attach(device_t dev)
346 static device_method_t xlp_pic_methods[] = {
347 DEVMETHOD(device_probe, xlp_pic_probe),
348 DEVMETHOD(device_attach, xlp_pic_attach),
353 static driver_t xlp_pic_driver = {
359 static devclass_t xlp_pic_devclass;
360 DRIVER_MODULE(xlp_pic, simplebus, xlp_pic_driver, xlp_pic_devclass, 0, 0);