2 * Copyright (c) 2015 Alexander Kabaev
3 * Copyright (c) 2006 Oleksandr Tymoshenko
4 * Copyright (c) 2002-2004 Juli Mallett <jmallett@FreeBSD.org>
5 * Copyright (c) 2017 The FreeBSD Foundation
8 * Portions of this software were developed by Landon Fuller
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification, immediately at the beginning of the file.
17 * 2. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_platform.h"
38 #include "opt_hwpmc_hooks.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/module.h>
46 #include <sys/malloc.h>
50 #include <sys/cpuset.h>
51 #include <sys/limits.h>
53 #include <sys/mutex.h>
55 #include <sys/sched.h>
57 #include <sys/pmckern.h>
59 #include <machine/bus.h>
60 #include <machine/hwfunc.h>
61 #include <machine/intr.h>
62 #include <machine/smp.h>
65 #include <dev/fdt/fdt_common.h>
66 #include <dev/ofw/openfirm.h>
67 #include <dev/ofw/ofw_bus.h>
68 #include <dev/ofw/ofw_bus_subr.h>
73 struct mips_pic_softc;
75 static int mips_pic_intr(void *);
76 static struct mips_pic_intr *mips_pic_find_intr(struct resource *r);
77 static int mips_pic_map_fixed_intr(u_int irq,
78 struct mips_pic_intr **mapping);
79 static void cpu_establish_intr(struct mips_pic_softc *sc,
80 const char *name, driver_filter_t *filt,
81 void (*handler)(void*), void *arg, int irq,
82 int flags, void **cookiep);
84 #define INTR_MAP_DATA_MIPS INTR_MAP_DATA_PLAT_1
86 struct intr_map_data_mips_pic {
87 struct intr_map_data hdr;
92 * MIPS interrupt state; available prior to MIPS PIC device attachment.
94 static struct mips_pic_intr {
95 u_int mips_irq; /**< MIPS IRQ# 0-7 */
96 u_int intr_irq; /**< INTRNG IRQ#, or INTR_IRQ_INVALID if unmapped */
97 u_int consumers; /**< INTRNG activation refcount */
98 struct resource *res; /**< resource shared by all interrupt handlers registered via
99 cpu_establish_hardintr() or cpu_establish_softintr(); NULL
100 if no interrupt handlers are yet registered. */
101 } mips_pic_intrs[] = {
102 { 0, INTR_IRQ_INVALID, 0, NULL },
103 { 1, INTR_IRQ_INVALID, 0, NULL },
104 { 2, INTR_IRQ_INVALID, 0, NULL },
105 { 3, INTR_IRQ_INVALID, 0, NULL },
106 { 4, INTR_IRQ_INVALID, 0, NULL },
107 { 5, INTR_IRQ_INVALID, 0, NULL },
108 { 6, INTR_IRQ_INVALID, 0, NULL },
109 { 7, INTR_IRQ_INVALID, 0, NULL },
112 struct mtx mips_pic_mtx;
113 MTX_SYSINIT(mips_pic_mtx, &mips_pic_mtx, "mips intr controller mutex", MTX_DEF);
115 struct mips_pic_irqsrc {
116 struct intr_irqsrc isrc;
120 struct mips_pic_softc {
122 struct mips_pic_irqsrc pic_irqs[NREAL_IRQS];
126 static struct mips_pic_softc *pic_sc;
128 #define PIC_INTR_ISRC(sc, irq) (&(sc)->pic_irqs[(irq)].isrc)
131 static struct ofw_compat_data compat_data[] = {
132 {"mti,cpu-interrupt-controller", true},
139 mips_pic_identify(driver_t *drv, device_t parent)
142 BUS_ADD_CHILD(parent, 0, "cpupic", 0);
147 mips_pic_probe(device_t dev)
151 if (!ofw_bus_status_okay(dev))
154 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
157 device_set_desc(dev, "MIPS32 Interrupt Controller");
158 return (BUS_PROBE_DEFAULT);
162 pic_irq_unmask(struct mips_pic_softc *sc, u_int irq)
165 mips_wr_status(mips_rd_status() | ((1 << irq) << 8));
169 pic_irq_mask(struct mips_pic_softc *sc, u_int irq)
172 mips_wr_status(mips_rd_status() & ~((1 << irq) << 8));
175 static inline intptr_t
176 pic_xref(device_t dev)
179 return (OF_xref_from_node(ofw_bus_get_node(dev)));
181 return (MIPS_PIC_XREF);
186 mips_pic_register_isrcs(struct mips_pic_softc *sc)
189 uint32_t irq, i, tmpirq;
190 struct intr_irqsrc *isrc;
193 for (irq = 0; irq < sc->nirqs; irq++) {
194 sc->pic_irqs[irq].irq = irq;
196 isrc = PIC_INTR_ISRC(sc, irq);
197 if (irq < NSOFT_IRQS) {
202 tmpirq = irq - NSOFT_IRQS;
204 error = intr_isrc_register(isrc, sc->pic_dev, 0, "%s%u",
207 for (i = 0; i < irq; i++) {
208 intr_isrc_deregister(PIC_INTR_ISRC(sc, i));
210 device_printf(sc->pic_dev, "%s failed", __func__);
219 mips_pic_attach(device_t dev)
221 struct mips_pic_softc *sc;
222 intptr_t xref = pic_xref(dev);
227 sc = device_get_softc(dev);
232 /* Set the number of interrupts */
233 sc->nirqs = nitems(sc->pic_irqs);
235 /* Register the interrupts */
236 if (mips_pic_register_isrcs(sc) != 0) {
237 device_printf(dev, "could not register PIC ISRCs\n");
242 * Now, when everything is initialized, it's right time to
243 * register interrupt controller to interrupt framefork.
245 if (intr_pic_register(dev, xref) == NULL) {
246 device_printf(dev, "could not register PIC\n");
250 /* Claim our root controller role */
251 if (intr_pic_claim_root(dev, xref, mips_pic_intr, sc, 0) != 0) {
252 device_printf(dev, "could not set PIC as a root\n");
253 intr_pic_deregister(dev, xref);
264 mips_pic_intr(void *arg)
266 struct mips_pic_softc *sc = arg;
267 register_t cause, status;
270 cause = mips_rd_cause();
271 status = mips_rd_status();
272 intr = (cause & MIPS_INT_MASK) >> 8;
274 * Do not handle masked interrupts. They were masked by
275 * pre_ithread function (mips_mask_XXX_intr) and will be
276 * unmasked once ithread is through with handler
278 intr &= (status & MIPS_INT_MASK) >> 8;
279 while ((i = fls(intr)) != 0) {
280 i--; /* Get a 0-offset interrupt. */
283 if (intr_isrc_dispatch(PIC_INTR_ISRC(sc, i),
284 curthread->td_intr_frame) != 0) {
285 device_printf(sc->pic_dev,
286 "Stray interrupt %u detected\n", i);
292 KASSERT(i == 0, ("all interrupts handled"));
295 if (pmc_hook && (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN)) {
296 struct trapframe *tf = PCPU_GET(curthread)->td_intr_frame;
298 pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, tf);
301 return (FILTER_HANDLED);
305 mips_pic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
309 irq = ((struct mips_pic_irqsrc *)isrc)->irq;
310 pic_irq_mask(device_get_softc(dev), irq);
314 mips_pic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
318 irq = ((struct mips_pic_irqsrc *)isrc)->irq;
319 pic_irq_unmask(device_get_softc(dev), irq);
323 mips_pic_map_intr(device_t dev, struct intr_map_data *data,
324 struct intr_irqsrc **isrcp)
326 struct mips_pic_softc *sc;
329 sc = device_get_softc(dev);
332 if (data->type == INTR_MAP_DATA_FDT) {
333 struct intr_map_data_fdt *daf;
335 daf = (struct intr_map_data_fdt *)data;
337 if (daf->ncells != 1 || daf->cells[0] >= sc->nirqs)
340 *isrcp = PIC_INTR_ISRC(sc, daf->cells[0]);
343 if (data->type == INTR_MAP_DATA_MIPS) {
344 struct intr_map_data_mips_pic *mpd;
346 mpd = (struct intr_map_data_mips_pic *)data;
348 if (mpd->irq < 0 || mpd->irq >= sc->nirqs)
351 *isrcp = PIC_INTR_ISRC(sc, mpd->irq);
360 mips_pic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
363 mips_pic_disable_intr(dev, isrc);
367 mips_pic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
370 mips_pic_enable_intr(dev, isrc);
374 mips_pic_post_filter(device_t dev, struct intr_irqsrc *isrc)
378 static device_method_t mips_pic_methods[] = {
379 /* Device interface */
381 DEVMETHOD(device_identify, mips_pic_identify),
383 DEVMETHOD(device_probe, mips_pic_probe),
384 DEVMETHOD(device_attach, mips_pic_attach),
386 /* Interrupt controller interface */
387 DEVMETHOD(pic_disable_intr, mips_pic_disable_intr),
388 DEVMETHOD(pic_enable_intr, mips_pic_enable_intr),
389 DEVMETHOD(pic_map_intr, mips_pic_map_intr),
390 DEVMETHOD(pic_pre_ithread, mips_pic_pre_ithread),
391 DEVMETHOD(pic_post_ithread, mips_pic_post_ithread),
392 DEVMETHOD(pic_post_filter, mips_pic_post_filter),
397 static driver_t mips_pic_driver = {
400 sizeof(struct mips_pic_softc),
403 static devclass_t mips_pic_devclass;
406 EARLY_DRIVER_MODULE(cpupic, ofwbus, mips_pic_driver, mips_pic_devclass, 0, 0,
409 EARLY_DRIVER_MODULE(cpupic, nexus, mips_pic_driver, mips_pic_devclass, 0, 0,
414 * Return the MIPS interrupt map entry for @p r, or NULL if no such entry has
417 static struct mips_pic_intr *
418 mips_pic_find_intr(struct resource *r)
420 struct mips_pic_intr *intr;
423 irq = rman_get_start(r);
424 if (irq != rman_get_end(r) || rman_get_size(r) != 1)
427 mtx_lock(&mips_pic_mtx);
428 for (size_t i = 0; i < nitems(mips_pic_intrs); i++) {
429 intr = &mips_pic_intrs[i];
431 if (intr->intr_irq != irq)
434 mtx_unlock(&mips_pic_mtx);
437 mtx_unlock(&mips_pic_mtx);
444 * Allocate a fixed IRQ mapping for the given MIPS @p irq, or return the
445 * existing mapping if @p irq was previously mapped.
447 * @param irq The MIPS IRQ to be mapped.
448 * @param[out] mapping On success, will be populated with the interrupt
452 * @retval EINVAL if @p irq is not a valid MIPS IRQ#.
453 * @retval non-zero If allocating the MIPS IRQ mapping otherwise fails, a
454 * regular unix error code will be returned.
457 mips_pic_map_fixed_intr(u_int irq, struct mips_pic_intr **mapping)
459 struct mips_pic_intr *intr;
460 struct intr_map_data_mips_pic *data;
464 if (irq < 0 || irq >= nitems(mips_pic_intrs))
467 mtx_lock(&mips_pic_mtx);
469 /* Fetch corresponding interrupt entry */
470 intr = &mips_pic_intrs[irq];
471 KASSERT(intr->mips_irq == irq,
472 ("intr %u found at index %u", intr->mips_irq, irq));
474 /* Already mapped? */
475 if (intr->intr_irq != INTR_IRQ_INVALID) {
476 mtx_unlock(&mips_pic_mtx);
481 /* Map the interrupt */
482 data = (struct intr_map_data_mips_pic *)intr_alloc_map_data(
483 INTR_MAP_DATA_MIPS, sizeof(*data), M_WAITOK | M_ZERO);
484 data->irq = intr->mips_irq;
487 /* PIC must be attached on FDT devices */
488 KASSERT(pic_sc != NULL, ("%s: no pic", __func__));
490 pic_dev = pic_sc->pic_dev;
491 xref = pic_xref(pic_dev);
493 /* PIC has a fixed xref, and may not have been attached yet */
496 pic_dev = pic_sc->pic_dev;
498 xref = MIPS_PIC_XREF;
501 KASSERT(intr->intr_irq == INTR_IRQ_INVALID, ("duplicate map"));
502 intr->intr_irq = intr_map_irq(pic_dev, xref, &data->hdr);
505 mtx_unlock(&mips_pic_mtx);
511 * Produce fixed IRQ mappings for all MIPS IRQs.
513 * Non-FDT/OFW MIPS targets do not provide an equivalent to OFW_BUS_MAP_INTR();
514 * it is instead necessary to reserve INTRNG IRQ# 0-7 for use by MIPS device
515 * drivers that assume INTRNG IRQs 0-7 are directly mapped to MIPS IRQs 0-7.
517 * XXX: There is no support in INTRNG for reserving a fixed IRQ range. However,
518 * we should be called prior to any other interrupt mapping requests, and work
519 * around this by iteratively allocating the required 0-7 MIP IRQ# range.
522 * @retval non-zero If allocating the MIPS IRQ mappings otherwise fails, a
523 * regular unix error code will be returned.
526 mips_pic_map_fixed_intrs(void)
530 for (u_int i = 0; i < nitems(mips_pic_intrs); i++) {
531 struct mips_pic_intr *intr;
533 if ((error = mips_pic_map_fixed_intr(i, &intr)))
536 /* INTRNG IRQs 0-7 must be directly mapped to MIPS IRQs 0-7 */
537 if (intr->intr_irq != intr->mips_irq) {
538 panic("invalid IRQ mapping: %u->%u", intr->intr_irq,
547 * If @p r references a MIPS interrupt mapped by the MIPS32 interrupt
548 * controller, handle interrupt activation internally.
550 * Otherwise, delegate directly to intr_activate_irq().
553 mips_pic_activate_intr(device_t child, struct resource *r)
555 struct mips_pic_intr *intr;
558 /* Is this one of our shared MIPS interrupts? */
559 if ((intr = mips_pic_find_intr(r)) == NULL) {
560 /* Delegate to standard INTRNG activation */
561 return (intr_activate_irq(child, r));
564 /* Bump consumer count and request activation if required */
565 mtx_lock(&mips_pic_mtx);
566 if (intr->consumers == UINT_MAX) {
567 mtx_unlock(&mips_pic_mtx);
571 if (intr->consumers == 0) {
572 if ((error = intr_activate_irq(child, r))) {
573 mtx_unlock(&mips_pic_mtx);
579 mtx_unlock(&mips_pic_mtx);
585 * If @p r references a MIPS interrupt mapped by the MIPS32 interrupt
586 * controller, handle interrupt deactivation internally.
588 * Otherwise, delegate directly to intr_deactivate_irq().
591 mips_pic_deactivate_intr(device_t child, struct resource *r)
593 struct mips_pic_intr *intr;
596 /* Is this one of our shared MIPS interrupts? */
597 if ((intr = mips_pic_find_intr(r)) == NULL) {
598 /* Delegate to standard INTRNG deactivation */
599 return (intr_deactivate_irq(child, r));
602 /* Decrement consumer count and request deactivation if required */
603 mtx_lock(&mips_pic_mtx);
604 KASSERT(intr->consumers > 0, ("refcount overrelease"));
606 if (intr->consumers == 1) {
607 if ((error = intr_deactivate_irq(child, r))) {
608 mtx_unlock(&mips_pic_mtx);
614 mtx_unlock(&mips_pic_mtx);
619 cpu_init_interrupts(void)
624 * Provide backwards-compatible support for registering a MIPS interrupt handler
625 * directly, without allocating a bus resource.
628 cpu_establish_intr(struct mips_pic_softc *sc, const char *name,
629 driver_filter_t *filt, void (*handler)(void*), void *arg, int irq,
630 int flags, void **cookiep)
632 struct mips_pic_intr *intr;
633 struct resource *res;
639 /* Fetch (or create) a fixed mapping */
640 if ((error = mips_pic_map_fixed_intr(irq, &intr)))
641 panic("Unable to map IRQ %d: %d", irq, error);
643 /* Fetch the backing resource, if any */
644 mtx_lock(&mips_pic_mtx);
646 mtx_unlock(&mips_pic_mtx);
648 /* Allocate our IRQ resource */
650 /* Optimistically perform resource allocation */
651 rid = intr->intr_irq;
652 res = bus_alloc_resource(sc->pic_dev, SYS_RES_IRQ, &rid,
653 intr->intr_irq, intr->intr_irq, 1, RF_SHAREABLE|RF_ACTIVE);
656 /* Try to update intr->res */
657 mtx_lock(&mips_pic_mtx);
658 if (intr->res == NULL) {
661 mtx_unlock(&mips_pic_mtx);
663 /* If intr->res was updated concurrently, free our local
664 * resource allocation */
665 if (intr->res != res) {
666 bus_release_resource(sc->pic_dev, SYS_RES_IRQ,
670 /* Maybe someone else allocated it? */
671 mtx_lock(&mips_pic_mtx);
673 mtx_unlock(&mips_pic_mtx);
677 panic("Unable to allocate IRQ %d->%u resource", irq,
682 error = bus_setup_intr(sc->pic_dev, res, flags, filt, handler, arg,
685 panic("Unable to add IRQ %d handler: %d", irq, error);
689 cpu_establish_hardintr(const char *name, driver_filter_t *filt,
690 void (*handler)(void*), void *arg, int irq, int flags, void **cookiep)
692 KASSERT(pic_sc != NULL, ("%s: no pic", __func__));
694 if (irq < 0 || irq >= NHARD_IRQS)
695 panic("%s called for unknown hard intr %d", __func__, irq);
697 cpu_establish_intr(pic_sc, name, filt, handler, arg, irq+NSOFT_IRQS,
702 cpu_establish_softintr(const char *name, driver_filter_t *filt,
703 void (*handler)(void*), void *arg, int irq, int flags,
706 KASSERT(pic_sc != NULL, ("%s: no pic", __func__));
708 if (irq < 0 || irq >= NSOFT_IRQS)
709 panic("%s called for unknown soft intr %d", __func__, irq);
711 cpu_establish_intr(pic_sc, name, filt, handler, arg, irq, flags,