2 * Copyright (c) 2010 Juli Mallett <jmallett@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/interrupt.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/malloc.h>
42 #include <machine/bus.h>
43 #include <machine/intr_machdep.h>
45 #include <contrib/octeon-sdk/cvmx.h>
46 #include <contrib/octeon-sdk/cvmx-interrupt.h>
49 * This bus sits between devices/buses and nexus and handles CIU interrupts
50 * and passes everything else through. It should really be a nexus subclass
51 * or something, but for now this will be sufficient.
54 #define CIU_IRQ_HARD (0)
56 #define CIU_IRQ_EN0_BEGIN CVMX_IRQ_WORKQ0
57 #define CIU_IRQ_EN0_END CVMX_IRQ_BOOTDMA
58 #define CIU_IRQ_EN0_COUNT ((CIU_IRQ_EN0_END - CIU_IRQ_EN0_BEGIN) + 1)
60 #define CIU_IRQ_EN1_BEGIN CVMX_IRQ_WDOG0
61 #define CIU_IRQ_EN1_END CVMX_IRQ_DFM
62 #define CIU_IRQ_EN1_COUNT ((CIU_IRQ_EN1_END - CIU_IRQ_EN1_BEGIN) + 1)
66 struct resource *ciu_irq;
69 static mips_intrcnt_t ciu_en0_intrcnt[CIU_IRQ_EN0_COUNT];
70 static mips_intrcnt_t ciu_en1_intrcnt[CIU_IRQ_EN1_COUNT];
72 static struct intr_event *ciu_en0_intr_events[CIU_IRQ_EN0_COUNT];
73 static struct intr_event *ciu_en1_intr_events[CIU_IRQ_EN1_COUNT];
75 static int ciu_probe(device_t);
76 static int ciu_attach(device_t);
77 static struct resource *ciu_alloc_resource(device_t, device_t, int, int *,
78 u_long, u_long, u_long, u_int);
79 static int ciu_setup_intr(device_t, device_t, struct resource *,
80 int, driver_filter_t *, driver_intr_t *,
82 static int ciu_teardown_intr(device_t, device_t,
83 struct resource *, void *);
84 static int ciu_bind_intr(device_t, device_t, struct resource *,
86 static int ciu_describe_intr(device_t, device_t,
87 struct resource *, void *,
89 static void ciu_hinted_child(device_t, const char *, int);
91 static void ciu_en0_intr_mask(void *);
92 static void ciu_en0_intr_unmask(void *);
94 static int ciu_en0_intr_bind(void *, u_char);
97 static void ciu_en1_intr_mask(void *);
98 static void ciu_en1_intr_unmask(void *);
100 static int ciu_en1_intr_bind(void *, u_char);
103 static int ciu_intr(void *);
106 ciu_probe(device_t dev)
108 if (device_get_unit(dev) != 0)
111 device_set_desc(dev, "Cavium Octeon Central Interrupt Unit");
116 ciu_attach(device_t dev)
118 char name[MAXCOMLEN + 1];
119 struct ciu_softc *sc;
124 sc = device_get_softc(dev);
127 sc->ciu_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, CIU_IRQ_HARD,
128 CIU_IRQ_HARD, 1, RF_ACTIVE);
129 if (sc->ciu_irq == NULL) {
130 device_printf(dev, "could not allocate irq%d\n", CIU_IRQ_HARD);
134 error = bus_setup_intr(dev, sc->ciu_irq, INTR_TYPE_MISC, ciu_intr,
137 device_printf(dev, "bus_setup_intr failed: %d\n", error);
141 sc->irq_rman.rm_type = RMAN_ARRAY;
142 sc->irq_rman.rm_descr = "CIU IRQ";
144 error = rman_init(&sc->irq_rman);
149 * We have two contiguous IRQ regions, use a single rman.
151 error = rman_manage_region(&sc->irq_rman, CIU_IRQ_EN0_BEGIN,
156 for (i = 0; i < CIU_IRQ_EN0_COUNT; i++) {
157 snprintf(name, sizeof name, "int%d:", i + CIU_IRQ_EN0_BEGIN);
158 ciu_en0_intrcnt[i] = mips_intrcnt_create(name);
161 for (i = 0; i < CIU_IRQ_EN1_COUNT; i++) {
162 snprintf(name, sizeof name, "int%d:", i + CIU_IRQ_EN1_BEGIN);
163 ciu_en1_intrcnt[i] = mips_intrcnt_create(name);
166 bus_generic_probe(dev);
167 bus_generic_attach(dev);
172 static struct resource *
173 ciu_alloc_resource(device_t bus, device_t child, int type, int *rid,
174 u_long start, u_long end, u_long count, u_int flags)
176 struct resource *res;
177 struct ciu_softc *sc;
179 sc = device_get_softc(bus);
185 return (bus_alloc_resource(device_get_parent(bus), type, rid,
186 start, end, count, flags));
190 * One interrupt at a time for now.
195 res = rman_reserve_resource(&sc->irq_rman, start, end, count, flags,
204 ciu_setup_intr(device_t bus, device_t child, struct resource *res, int flags,
205 driver_filter_t *filter, driver_intr_t *intr, void *arg,
208 struct intr_event *event, **eventp;
209 void (*mask_func)(void *);
210 void (*unmask_func)(void *);
211 int (*bind_func)(void *, u_char);
212 mips_intrcnt_t intrcnt;
216 irq = rman_get_start(res);
217 if (irq <= CIU_IRQ_EN0_END) {
218 eventp = &ciu_en0_intr_events[irq - CIU_IRQ_EN0_BEGIN];
219 intrcnt = ciu_en0_intrcnt[irq - CIU_IRQ_EN0_BEGIN];
220 mask_func = ciu_en0_intr_mask;
221 unmask_func = ciu_en0_intr_unmask;
223 bind_func = ciu_en0_intr_bind;
226 eventp = &ciu_en1_intr_events[irq - CIU_IRQ_EN1_BEGIN];
227 intrcnt = ciu_en1_intrcnt[irq - CIU_IRQ_EN1_BEGIN];
228 mask_func = ciu_en1_intr_mask;
229 unmask_func = ciu_en1_intr_unmask;
231 bind_func = ciu_en1_intr_bind;
238 if ((event = *eventp) == NULL) {
239 error = intr_event_create(eventp, (void *)(uintptr_t)irq, 0,
240 irq, mask_func, unmask_func, NULL, bind_func, "int%d", irq);
246 unmask_func((void *)(uintptr_t)irq);
249 intr_event_add_handler(event, device_get_nameunit(child),
250 filter, intr, arg, intr_priority(flags), flags, cookiep);
252 mips_intrcnt_setname(intrcnt, event->ie_fullname);
258 ciu_teardown_intr(device_t bus, device_t child, struct resource *res,
263 error = intr_event_remove_handler(cookie);
272 ciu_bind_intr(device_t bus, device_t child, struct resource *res, int cpu)
274 struct intr_event *event;
277 irq = rman_get_start(res);
278 if (irq <= CIU_IRQ_EN0_END)
279 event = ciu_en0_intr_events[irq - CIU_IRQ_EN0_BEGIN];
281 event = ciu_en1_intr_events[irq - CIU_IRQ_EN1_BEGIN];
283 return (intr_event_bind(event, cpu));
288 ciu_describe_intr(device_t bus, device_t child, struct resource *res,
289 void *cookie, const char *descr)
291 struct intr_event *event;
292 mips_intrcnt_t intrcnt;
296 irq = rman_get_start(res);
297 if (irq <= CIU_IRQ_EN0_END) {
298 event = ciu_en0_intr_events[irq - CIU_IRQ_EN0_BEGIN];
299 intrcnt = ciu_en0_intrcnt[irq - CIU_IRQ_EN0_BEGIN];
301 event = ciu_en1_intr_events[irq - CIU_IRQ_EN1_BEGIN];
302 intrcnt = ciu_en1_intrcnt[irq - CIU_IRQ_EN1_BEGIN];
305 error = intr_event_describe_handler(event, cookie, descr);
309 mips_intrcnt_setname(intrcnt, event->ie_fullname);
315 ciu_hinted_child(device_t bus, const char *dname, int dunit)
317 BUS_ADD_CHILD(bus, 0, dname, dunit);
321 ciu_en0_intr_mask(void *arg)
326 irq = (uintptr_t)arg;
327 mask = cvmx_read_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num()*2));
328 mask &= ~(1ull << (irq - CIU_IRQ_EN0_BEGIN));
329 cvmx_write_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num()*2), mask);
333 ciu_en0_intr_unmask(void *arg)
338 irq = (uintptr_t)arg;
339 mask = cvmx_read_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num()*2));
340 mask |= 1ull << (irq - CIU_IRQ_EN0_BEGIN);
341 cvmx_write_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num()*2), mask);
346 ciu_en0_intr_bind(void *arg, u_char target)
352 irq = (uintptr_t)arg;
354 mask = cvmx_read_csr(CVMX_CIU_INTX_EN0(core*2));
356 mask |= 1ull << (irq - CIU_IRQ_EN0_BEGIN);
358 mask &= ~(1ull << (irq - CIU_IRQ_EN0_BEGIN));
359 cvmx_write_csr(CVMX_CIU_INTX_EN0(core*2), mask);
367 ciu_en1_intr_mask(void *arg)
372 irq = (uintptr_t)arg;
373 mask = cvmx_read_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num()*2));
374 mask &= ~(1ull << (irq - CIU_IRQ_EN1_BEGIN));
375 cvmx_write_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num()*2), mask);
379 ciu_en1_intr_unmask(void *arg)
384 irq = (uintptr_t)arg;
385 mask = cvmx_read_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num()*2));
386 mask |= 1ull << (irq - CIU_IRQ_EN1_BEGIN);
387 cvmx_write_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num()*2), mask);
392 ciu_en1_intr_bind(void *arg, u_char target)
398 irq = (uintptr_t)arg;
400 mask = cvmx_read_csr(CVMX_CIU_INTX_EN1(core*2));
402 mask |= 1ull << (irq - CIU_IRQ_EN1_BEGIN);
404 mask &= ~(1ull << (irq - CIU_IRQ_EN1_BEGIN));
405 cvmx_write_csr(CVMX_CIU_INTX_EN1(core*2), mask);
415 struct ciu_softc *sc;
416 uint64_t en0_sum, en1_sum;
417 uint64_t en0_mask, en1_mask;
424 en0_sum = cvmx_read_csr(CVMX_CIU_INTX_SUM0(cvmx_get_core_num()*2));
425 en1_sum = cvmx_read_csr(CVMX_CIU_INT_SUM1);
427 en0_mask = cvmx_read_csr(CVMX_CIU_INTX_EN0(cvmx_get_core_num()*2));
428 en1_mask = cvmx_read_csr(CVMX_CIU_INTX_EN1(cvmx_get_core_num()*2));
433 if (en0_sum == 0 && en1_sum == 0)
434 return (FILTER_STRAY);
437 for (irq_index = 0; en0_sum != 0; irq_index++, en0_sum >>= 1) {
438 if ((en0_sum & 1) == 0)
441 mips_intrcnt_inc(ciu_en0_intrcnt[irq_index]);
443 error = intr_event_handle(ciu_en0_intr_events[irq_index], NULL);
445 printf("%s: stray en0 irq%d\n", __func__, irq_index);
449 for (irq_index = 0; en1_sum != 0; irq_index++, en1_sum >>= 1) {
450 if ((en1_sum & 1) == 0)
453 mips_intrcnt_inc(ciu_en1_intrcnt[irq_index]);
455 error = intr_event_handle(ciu_en1_intr_events[irq_index], NULL);
457 printf("%s: stray en1 irq%d\n", __func__, irq_index);
460 return (FILTER_HANDLED);
463 static device_method_t ciu_methods[] = {
464 DEVMETHOD(device_probe, ciu_probe),
465 DEVMETHOD(device_attach, ciu_attach),
467 DEVMETHOD(bus_alloc_resource, ciu_alloc_resource),
468 DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
469 DEVMETHOD(bus_setup_intr, ciu_setup_intr),
470 DEVMETHOD(bus_teardown_intr, ciu_teardown_intr),
472 DEVMETHOD(bus_bind_intr, ciu_bind_intr),
474 DEVMETHOD(bus_describe_intr, ciu_describe_intr),
476 DEVMETHOD(bus_add_child, bus_generic_add_child),
477 DEVMETHOD(bus_hinted_child, ciu_hinted_child),
482 static driver_t ciu_driver = {
485 sizeof(struct ciu_softc),
487 static devclass_t ciu_devclass;
488 DRIVER_MODULE(ciu, nexus, ciu_driver, ciu_devclass, 0, 0);