2 * Copyright (c) 2013 SRI International
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/systm.h>
43 #include <machine/bus.h>
44 #include <machine/intr_machdep.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include <dev/fdt/fdt_common.h>
51 #include "fdt_ic_if.h"
55 static uint64_t bp_read_cfg(struct beripic_softc *, int);
56 static void bp_write_cfg(struct beripic_softc *, int, uint64_t);
57 static void bp_detach_resources(device_t);
58 static char *bp_strconfig(uint64_t, char *, size_t);
59 static void bp_config_source(device_t, int, int, u_long, u_long);
61 static void bp_set_counter_name(device_t, device_t, int);
64 static int beripic_fdt_probe(device_t);
65 static int beripic_fdt_attach(device_t);
67 static int beripic_activate_intr(device_t, struct resource *);
68 static struct resource *
69 beripic_alloc_intr(device_t, device_t, int *, u_long, u_int);
70 static int beripic_config_intr(device_t, int, enum intr_trigger,
72 static int beripic_release_intr(device_t, struct resource *);
73 static int beripic_setup_intr(device_t, device_t, struct resource *,
74 int, driver_filter_t *, driver_intr_t *, void *, void **);
75 static int beripic_teardown_intr(device_t, device_t, struct resource *,
78 static int beripic_filter(void *);
79 static void beripic_intr(void *);
81 #define BP_MAX_HARD_IRQS 6
82 #define BP_FIRST_SOFT 64
84 struct beripic_softc {
86 struct resource *bp_cfg_res;
87 struct resource *bp_read_res;
88 struct resource *bp_set_res;
89 struct resource *bp_clear_res;
94 bus_space_tag_t bp_cfg_bst;
95 bus_space_tag_t bp_read_bst;
96 bus_space_tag_t bp_set_bst;
97 bus_space_tag_t bp_clear_bst;
98 bus_space_handle_t bp_cfg_bsh;
99 bus_space_handle_t bp_read_bsh;
100 bus_space_handle_t bp_set_bsh;
101 bus_space_handle_t bp_clear_bsh;
103 struct resource *bp_irqs[BP_MAX_HARD_IRQS];
104 int bp_irq_rids[BP_MAX_HARD_IRQS];
114 struct rman bp_src_rman;
117 mips_intrcnt_t *bp_counters;
120 struct mtx bp_cfgmtx;
123 struct beripic_intr_arg {
124 driver_filter_t *filter;
127 struct resource *irq;
129 mips_intrcnt_t counter;
133 struct beripic_cookie {
134 struct beripic_intr_arg *bpia;
135 struct resource *hirq;
139 #define BP_CFG_MASK_E 0x80000000ull
140 #define BP_CFG_SHIFT_E 31
141 #define BP_CFG_MASK_TID 0x7FFFFF00ull /* Depends on CPU */
142 #define BP_CFG_SHIFT_TID 8
143 #define BP_CFG_MASK_IRQ 0x0000000Full
144 #define BP_CFG_SHIFT_IRQ 0
145 #define BP_CFG_VALID (BP_CFG_MASK_E|BP_CFG_MASK_TID|BP_CFG_MASK_IRQ)
146 #define BP_CFG_RESERVED ~BP_CFG_VALID
148 #define BP_CFG_ENABLED(cfg) (((cfg) & BP_CFG_MASK_E) >> BP_CFG_SHIFT_E)
149 #define BP_CFG_TID(cfg) (((cfg) & BP_CFG_MASK_TID) >> BP_CFG_SHIFT_TID)
150 #define BP_CFG_IRQ(cfg) (((cfg) & BP_CFG_MASK_IRQ) >> BP_CFG_SHIFT_IRQ)
152 MALLOC_DEFINE(M_BERIPIC, "beripic", "beripic memory");
155 bp_read_cfg(struct beripic_softc *sc, int irq)
158 KASSERT((irq >= 0 && irq < sc->bp_nsrcs),
159 ("IRQ of of range %d (0-%d)", irq, sc->bp_nsrcs - 1));
160 return (bus_space_read_8(sc->bp_cfg_bst, sc->bp_cfg_bsh, irq * 8));
164 bp_write_cfg(struct beripic_softc *sc, int irq, uint64_t config)
167 KASSERT((irq >= 0 && irq < sc->bp_nsrcs),
168 ("IRQ of of range %d (0-%d)", irq, sc->bp_nsrcs - 1));
169 bus_space_write_8(sc->bp_cfg_bst, sc->bp_cfg_bsh, irq * 8, config);
173 bp_detach_resources(device_t dev)
175 struct beripic_softc *sc;
178 sc = device_get_softc(dev);
180 if (sc->bp_cfg_res != NULL) {
181 bus_release_resource(dev, SYS_RES_MEMORY, sc->bp_cfg_rid,
183 sc->bp_cfg_res = NULL;
185 if (sc->bp_read_res != NULL) {
186 bus_release_resource(dev, SYS_RES_MEMORY, sc->bp_read_rid,
188 sc->bp_read_res = NULL;
190 if (sc->bp_set_res != NULL) {
191 bus_release_resource(dev, SYS_RES_MEMORY, sc->bp_set_rid,
193 sc->bp_set_res = NULL;
195 if (sc->bp_clear_res != NULL) {
196 bus_release_resource(dev, SYS_RES_MEMORY, sc->bp_clear_rid,
198 sc->bp_clear_res = NULL;
200 for (i = sc->bp_nirqs - 1; i >= 0; i--) {
201 bus_release_resource(dev, SYS_RES_IRQ, sc->bp_irq_rids[i],
208 bp_strconfig(uint64_t config, char *configstr, size_t len)
211 if (snprintf(configstr, len, "%s tid: %llu hardintr %llu",
212 BP_CFG_ENABLED(config) ? "enabled" : "disabled",
213 BP_CFG_TID(config), BP_CFG_IRQ(config)) > len - 1)
219 bp_config_source(device_t ic, int src, int enable, u_long tid, u_long irq)
221 struct beripic_softc *sc;
224 sc = device_get_softc(ic);
227 config |= enable << BP_CFG_SHIFT_E;
228 config |= tid << BP_CFG_SHIFT_TID;
229 config |= irq << BP_CFG_SHIFT_IRQ;
231 bp_write_cfg(sc, src, config);
236 bp_set_counter_name(device_t ic, device_t child, int src)
238 struct beripic_softc *sc;
239 char name[MAXCOMLEN + 1];
241 sc = device_get_softc(ic);
243 if (snprintf(name, sizeof(name), "bp%dsrc%d%s%s%s",
244 device_get_unit(ic), src, src < sc->bp_nhard ? "" : "s",
245 child == NULL ? "" : " ",
246 child == NULL ? " " : device_get_nameunit(child)) >= sizeof(name))
247 name[sizeof(name) - 2] = '+';
249 mips_intrcnt_setname(sc->bp_counters[src], name);
254 beripic_fdt_probe(device_t dev)
257 if (!ofw_bus_status_okay(dev))
260 if (!ofw_bus_is_compatible(dev, "sri-cambridge,beri-pic"))
263 device_set_desc(dev, "BERI Programmable Interrupt Controller");
264 return (BUS_PROBE_DEFAULT);
268 beripic_fdt_attach(device_t dev)
271 struct beripic_softc *sc;
273 pcell_t nhard, nsoft;
278 sc = device_get_softc(dev);
281 mtx_init(&sc->bp_cfgmtx, "beripic config lock", NULL, MTX_DEF);
284 * FDT lists CONFIG, IP_READ, IP_SET, and IP_CLEAR registers as
285 * seperate memory regions in that order.
288 sc->bp_cfg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
289 &sc->bp_cfg_rid, RF_ACTIVE);
290 if (sc->bp_cfg_res == NULL) {
291 device_printf(dev, "failed to map config memory");
296 device_printf(sc->bp_dev, "config region at mem %p-%p\n",
297 (void *)rman_get_start(sc->bp_cfg_res),
298 (void *)(rman_get_start(sc->bp_cfg_res) +
299 rman_get_size(sc->bp_cfg_res)));
302 sc->bp_read_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
303 &sc->bp_read_rid, RF_ACTIVE);
304 if (sc->bp_read_res == NULL) {
305 device_printf(dev, "failed to map IP read memory");
310 device_printf(sc->bp_dev, "IP read region at mem %p-%p\n",
311 (void *)rman_get_start(sc->bp_read_res),
312 (void *)(rman_get_start(sc->bp_read_res) +
313 rman_get_size(sc->bp_read_res)));
316 sc->bp_set_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
317 &sc->bp_set_rid, RF_ACTIVE);
318 if (sc->bp_set_res == NULL) {
319 device_printf(dev, "failed to map IP read memory");
324 device_printf(sc->bp_dev, "IP set region at mem %p-%p\n",
325 (void *)rman_get_start(sc->bp_set_res),
326 (void *)(rman_get_start(sc->bp_set_res) +
327 rman_get_size(sc->bp_set_res)));
329 sc->bp_clear_rid = 3;
330 sc->bp_clear_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
331 &sc->bp_clear_rid, RF_ACTIVE);
332 if (sc->bp_clear_res == NULL) {
333 device_printf(dev, "failed to map IP read memory");
338 device_printf(sc->bp_dev, "IP clear region at mem %p-%p\n",
339 (void *)rman_get_start(sc->bp_clear_res),
340 (void *)(rman_get_start(sc->bp_clear_res) +
341 rman_get_size(sc->bp_clear_res)));
344 for (i = 0; i < BP_MAX_HARD_IRQS; i++) {
345 sc->bp_irq_rids[i] = i;
346 sc->bp_irqs[i] = bus_alloc_resource_any(dev, SYS_RES_IRQ,
347 &sc->bp_irq_rids[i], RF_ACTIVE | RF_SHAREABLE);
348 if (sc->bp_irqs[i] == NULL)
352 device_printf(dev, "failed to allocate any parent IRQs!");
358 ph = ofw_bus_gen_get_node(device_get_parent(dev), dev);
364 /* XXX: get nthreads from cpu(s) somehow */
367 if (OF_getprop(ph, "hard-interrupt-sources", &nhard, sizeof(nhard))
369 device_printf(dev, "failed to get number of hard sources");
373 if (OF_getprop(ph, "soft-interrupt-sources", &nsoft, sizeof(nsoft))
375 device_printf(dev, "failed to get number of soft sources");
380 sc->bp_nhard = nhard;
381 sc->bp_nsoft = nsoft;
382 sc->bp_nsrcs = sc->bp_nhard + sc->bp_nsoft;
383 /* XXX: should deal with gap between hard and soft */
384 KASSERT(sc->bp_nhard <= BP_FIRST_SOFT,
385 ("too many hard sources"));
386 KASSERT(rman_get_size(sc->bp_cfg_res) / 8 == sc->bp_nsrcs,
387 ("config space size does not match sources"));
388 KASSERT(sc->bp_nhard % 64 == 0,
389 ("Non-multiple of 64 intr counts not supported"));
390 KASSERT(sc->bp_nsoft % 64 == 0,
391 ("Non-multiple of 64 intr counts not supported"));
393 device_printf(dev, "%d hard and %d soft sources\n",
394 sc->bp_nhard, sc->bp_nsoft);
397 sc->bp_counters = malloc(sizeof(*sc->bp_counters) * sc->bp_nsrcs,
398 M_BERIPIC, M_WAITOK|M_ZERO);
399 for (i = 0; i < sc->bp_nsrcs; i++) {
400 sc->bp_counters[i] = mips_intrcnt_create("");
401 bp_set_counter_name(dev, NULL, i);
405 sc->bp_src_rman.rm_start = 0;
406 sc->bp_src_rman.rm_end = sc->bp_nsrcs - 1;
407 sc->bp_src_rman.rm_type = RMAN_ARRAY;
408 sc->bp_src_rman.rm_descr = "Interrupt source";
409 if (rman_init(&(sc->bp_src_rman)) != 0 ||
410 rman_manage_region(&(sc->bp_src_rman), 0, sc->bp_nsrcs - 1) != 0) {
411 device_printf(dev, "Failed to set up sources rman");
416 sc->bp_cfg_bst = rman_get_bustag(sc->bp_cfg_res);
417 sc->bp_cfg_bsh = rman_get_bushandle(sc->bp_cfg_res);
418 sc->bp_read_bst = rman_get_bustag(sc->bp_read_res);
419 sc->bp_read_bsh = rman_get_bushandle(sc->bp_read_res);
420 sc->bp_set_bst = rman_get_bustag(sc->bp_set_res);
421 sc->bp_set_bsh = rman_get_bushandle(sc->bp_set_res);
422 sc->bp_clear_bst = rman_get_bustag(sc->bp_clear_res);
423 sc->bp_clear_bsh = rman_get_bushandle(sc->bp_clear_res);
425 for (src = 0; src < sc->bp_nsrcs; src++) {
426 config = bp_read_cfg(sc, src);
431 device_printf(dev, "initial config: src %d: %s\n", src,
432 bp_strconfig(config, configstr, sizeof(configstr)));
433 if (config & BP_CFG_RESERVED)
435 "reserved bits not 0: 0x%016jx\n",
439 bp_config_source(dev, src, 0, 0, 0);
442 fic = malloc(sizeof(*fic), M_BERIPIC, M_WAITOK|M_ZERO);
445 SLIST_INSERT_HEAD(&fdt_ic_list_head, fic, fdt_ics);
449 bp_detach_resources(dev);
454 static struct resource *
455 beripic_alloc_intr(device_t ic, device_t child, int *rid, u_long irq,
458 struct beripic_softc *sc;
461 sc = device_get_softc(ic);
463 rv = rman_reserve_resource(&(sc->bp_src_rman), irq, irq, 1, flags,
466 printf("%s: could not reserve source interrupt for %s\n",
467 __func__, device_get_nameunit(child));
468 rman_set_rid(rv, *rid);
470 if ((flags & RF_ACTIVE) &&
471 beripic_activate_intr(ic, rv) != 0) {
472 printf("%s: could not activate interrupt\n", __func__);
473 rman_release_resource(rv);
481 beripic_release_intr(device_t ic, struct resource *r)
484 return (rman_release_resource(r));
488 beripic_activate_intr(device_t ic, struct resource *r)
491 return (rman_activate_resource(r));
495 beripic_deactivate_intr(device_t ic, struct resource *r)
498 return (rman_deactivate_resource(r));
502 beripic_config_intr(device_t dev, int irq, enum intr_trigger trig,
503 enum intr_polarity pol)
506 if (trig != INTR_TRIGGER_CONFORM || pol != INTR_POLARITY_CONFORM)
513 beripic_setup_intr(device_t ic, device_t child, struct resource *irq,
514 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
517 struct beripic_softc *sc;
518 struct beripic_intr_arg *bpia;
519 struct beripic_cookie *bpc;
521 u_long hirq, src, tid;
523 sc = device_get_softc(ic);
525 src = rman_get_start(irq);
527 KASSERT(src < sc->bp_nsrcs, ("source (%lu) out of range 0-%d",
528 src, sc->bp_nsrcs - 1));
530 bpia = malloc(sizeof(*bpia), M_BERIPIC, M_WAITOK|M_ZERO);
531 bpia->filter = filter;
536 bpia->counter = sc->bp_counters[src];
537 bp_set_counter_name(ic, child, src);
540 bpc = malloc(sizeof(*bpc), M_BERIPIC, M_WAITOK|M_ZERO);
543 mtx_lock(&(sc->bp_cfgmtx));
544 bpc->hirq = sc->bp_irqs[sc->bp_next_irq];
545 hirq = rman_get_start(bpc->hirq);
546 tid = sc->bp_next_tid;
548 error = BUS_SETUP_INTR(device_get_parent(ic), ic, bpc->hirq, flags,
549 beripic_filter, intr == NULL ? NULL : beripic_intr, bpia,
556 /* XXX: bind ithread to cpu */
558 if (sc->bp_next_tid >= sc->bp_nthreads)
562 if (sc->bp_next_tid == 0) {
564 if (sc->bp_next_irq >= sc->bp_nirqs)
567 mtx_unlock(&(sc->bp_cfgmtx));
571 bp_config_source(ic, rman_get_start(irq), 1, tid, hirq);
575 free(bpc, M_BERIPIC);
576 free(bpia, M_BERIPIC);
582 beripic_teardown_intr(device_t dev, device_t child, struct resource *irq,
585 struct beripic_cookie *bpc;
590 bp_config_source(dev, rman_get_start(irq), 0, 0, 0);
592 free(bpc->bpia, M_BERIPIC);
594 error = BUS_TEARDOWN_INTR(device_get_parent(dev), dev, bpc->hirq,
597 free(bpc, M_BERIPIC);
603 beripic_filter(void *arg)
605 struct beripic_intr_arg *bpic;
610 mips_intrcnt_inc(bpic->counter);
613 /* XXX: Add a check that our source is high */
615 if (bpic->filter == NULL)
616 return (FILTER_SCHEDULE_THREAD);
618 return (bpic->filter(bpic->arg));
622 beripic_intr(void *arg)
624 struct beripic_intr_arg *bpic;
628 KASSERT(bpic->intr != NULL,
629 ("%s installed, but no child intr", __func__));
631 bpic->intr(bpic->arg);
636 beripic_setup_ipi(device_t ic, u_int tid, u_int ipi_irq)
639 bp_config_source(ic, BP_FIRST_SOFT + tid, 1, tid, ipi_irq);
643 beripic_send_ipi(device_t ic, u_int tid)
645 struct beripic_softc *sc;
648 sc = device_get_softc(ic);
650 KASSERT(tid < sc->bp_nsoft, ("tid (%d) too large\n", tid));
652 bit = 1ULL << (tid % 64);
653 bus_space_write_8(sc->bp_set_bst, sc->bp_set_bsh,
654 (BP_FIRST_SOFT / 8) + (tid / 64), bit);
658 beripic_clear_ipi(device_t ic, u_int tid)
660 struct beripic_softc *sc;
663 sc = device_get_softc(ic);
665 KASSERT(tid < sc->bp_nsoft, ("tid (%d) to large\n", tid));
667 bit = 1ULL << (tid % 64);
668 bus_space_write_8(sc->bp_clear_bst, sc->bp_clear_bsh,
669 (BP_FIRST_SOFT / 8) + (tid / 64), bit);
673 devclass_t beripic_devclass;
675 static device_method_t beripic_fdt_methods[] = {
676 /* Device interface */
677 DEVMETHOD(device_probe, beripic_fdt_probe),
678 DEVMETHOD(device_attach, beripic_fdt_attach),
680 DEVMETHOD(fdt_ic_activate_intr, beripic_activate_intr),
681 DEVMETHOD(fdt_ic_alloc_intr, beripic_alloc_intr),
682 DEVMETHOD(fdt_ic_config_intr, beripic_config_intr),
683 DEVMETHOD(fdt_ic_deactivate_intr, beripic_deactivate_intr),
684 DEVMETHOD(fdt_ic_release_intr, beripic_release_intr),
685 DEVMETHOD(fdt_ic_setup_intr, beripic_setup_intr),
686 DEVMETHOD(fdt_ic_teardown_intr, beripic_teardown_intr),
689 DEVMETHOD(fdt_ic_setup_ipi, beripic_setup_ipi),
690 DEVMETHOD(fdt_ic_clear_ipi, beripic_clear_ipi),
691 DEVMETHOD(fdt_ic_send_ipi, beripic_send_ipi),
697 static driver_t beripic_fdt_driver = {
700 sizeof(struct beripic_softc)
703 DRIVER_MODULE(beripic, simplebus, beripic_fdt_driver, beripic_devclass, 0, 0);