2 * Copyright (c) 2006 Benno Rice.
3 * Copyright (C) 2007-2011 MARVELL INTERNATIONAL LTD.
4 * Copyright (c) 2012 Semihalf.
7 * Developed by Semihalf.
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 ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * from: FreeBSD: //depot/projects/arm/src/sys/arm/xscale/pxa2x0/pxa2x0_icu.c, rev 1
30 * from: FreeBSD: src/sys/arm/mv/ic.c,v 1.5 2011/02/08 01:49:30
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_platform.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/cpuset.h>
45 #include <sys/module.h>
47 #include <sys/mutex.h>
51 #include <machine/bus.h>
52 #include <machine/intr.h>
53 #include <machine/smp.h>
55 #include <arm/mv/mvvar.h>
56 #include <arm/mv/mvreg.h>
58 #include <dev/ofw/ofw_bus.h>
59 #include <dev/ofw/ofw_bus_subr.h>
60 #include <dev/fdt/fdt_common.h>
67 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
68 printf(fmt,##args); } while (0)
70 #define debugf(fmt, args...)
73 #define MPIC_INT_ERR 4
74 #define MPIC_INT_MSI 96
76 #define MPIC_IRQ_MASK 0x3ff
79 #define MPIC_SOFT_INT 0x4
80 #define MPIC_SOFT_INT_DRBL1 (1 << 5)
81 #define MPIC_ERR_CAUSE 0x20
84 #define MPIC_INT_CTL(irq) (0x100 + (irq)*4)
86 #define MPIC_INT_IRQ_FIQ_MASK(cpuid) (0x101 << (cpuid))
87 #define MPIC_CTRL_NIRQS(ctrl) (((ctrl) >> 2) & 0x3ff)
89 #define MPIC_IN_DRBL 0x08
90 #define MPIC_IN_DRBL_MASK 0x0c
91 #define MPIC_PPI_CAUSE 0x10
93 #define MPIC_IIACK 0x44
96 #define MPIC_ERR_MASK 0xe50
101 struct mv_mpic_irqsrc {
102 struct intr_irqsrc mmi_isrc;
107 struct mv_mpic_softc {
109 struct resource * mpic_res[4];
110 bus_space_tag_t mpic_bst;
111 bus_space_handle_t mpic_bsh;
112 bus_space_tag_t cpu_bst;
113 bus_space_handle_t cpu_bsh;
114 bus_space_tag_t drbl_bst;
115 bus_space_handle_t drbl_bsh;
118 struct mv_mpic_irqsrc * mpic_isrcs;
124 static struct resource_spec mv_mpic_spec[] = {
125 { SYS_RES_MEMORY, 0, RF_ACTIVE },
126 { SYS_RES_MEMORY, 1, RF_ACTIVE },
127 { SYS_RES_MEMORY, 2, RF_ACTIVE | RF_OPTIONAL },
128 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_OPTIONAL },
132 static struct ofw_compat_data compat_data[] = {
134 {"marvell,mpic", true},
138 static struct mv_mpic_softc *mv_mpic_sc = NULL;
140 void mpic_send_ipi(int cpus, u_int ipi);
142 static int mv_mpic_probe(device_t);
143 static int mv_mpic_attach(device_t);
144 uint32_t mv_mpic_get_cause(void);
145 uint32_t mv_mpic_get_cause_err(void);
146 uint32_t mv_mpic_get_msi(void);
147 static void mpic_unmask_irq(uintptr_t nb);
148 static void mpic_mask_irq(uintptr_t nb);
149 static void mpic_mask_irq_err(uintptr_t nb);
150 static void mpic_unmask_irq_err(uintptr_t nb);
151 static boolean_t mpic_irq_is_percpu(uintptr_t);
153 static int mpic_intr(void *arg);
155 static void mpic_unmask_msi(void);
157 #define MPIC_WRITE(softc, reg, val) \
158 bus_space_write_4((softc)->mpic_bst, (softc)->mpic_bsh, (reg), (val))
159 #define MPIC_READ(softc, reg) \
160 bus_space_read_4((softc)->mpic_bst, (softc)->mpic_bsh, (reg))
162 #define MPIC_CPU_WRITE(softc, reg, val) \
163 bus_space_write_4((softc)->cpu_bst, (softc)->cpu_bsh, (reg), (val))
164 #define MPIC_CPU_READ(softc, reg) \
165 bus_space_read_4((softc)->cpu_bst, (softc)->cpu_bsh, (reg))
167 #define MPIC_DRBL_WRITE(softc, reg, val) \
168 bus_space_write_4((softc)->drbl_bst, (softc)->drbl_bsh, (reg), (val))
169 #define MPIC_DRBL_READ(softc, reg) \
170 bus_space_read_4((softc)->drbl_bst, (softc)->drbl_bsh, (reg))
173 mv_mpic_probe(device_t dev)
176 if (!ofw_bus_status_okay(dev))
179 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
182 device_set_desc(dev, "Marvell Integrated Interrupt Controller");
188 mv_mpic_register_isrcs(struct mv_mpic_softc *sc)
192 struct intr_irqsrc *isrc;
195 sc->mpic_isrcs = malloc(sc->nirqs * sizeof (*sc->mpic_isrcs), M_DEVBUF,
198 name = device_get_nameunit(sc->sc_dev);
199 for (irq = 0; irq < sc->nirqs; irq++) {
200 sc->mpic_isrcs[irq].mmi_irq = irq;
202 isrc = &sc->mpic_isrcs[irq].mmi_isrc;
203 if (irq < MPIC_PPI) {
204 error = intr_isrc_register(isrc, sc->sc_dev,
205 INTR_ISRCF_PPI, "%s", name);
207 error = intr_isrc_register(isrc, sc->sc_dev, 0, "%s",
211 /* XXX call intr_isrc_deregister() */
212 device_printf(sc->sc_dev, "%s failed", __func__);
221 mv_mpic_attach(device_t dev)
223 struct mv_mpic_softc *sc;
227 sc = (struct mv_mpic_softc *)device_get_softc(dev);
229 if (mv_mpic_sc != NULL)
235 mtx_init(&sc->mtx, "MPIC lock", NULL, MTX_SPIN);
237 error = bus_alloc_resources(dev, mv_mpic_spec, sc->mpic_res);
239 device_printf(dev, "could not allocate resources\n");
243 if (sc->mpic_res[3] == NULL)
244 device_printf(dev, "No interrupt to use.\n");
246 bus_setup_intr(dev, sc->mpic_res[3], INTR_TYPE_CLK,
247 mpic_intr, NULL, sc, &sc->intr_hand);
250 sc->mpic_bst = rman_get_bustag(sc->mpic_res[0]);
251 sc->mpic_bsh = rman_get_bushandle(sc->mpic_res[0]);
253 sc->cpu_bst = rman_get_bustag(sc->mpic_res[1]);
254 sc->cpu_bsh = rman_get_bushandle(sc->mpic_res[1]);
256 if (sc->mpic_res[2] != NULL) {
257 /* This is required only if MSIs are used. */
258 sc->drbl_bst = rman_get_bustag(sc->mpic_res[2]);
259 sc->drbl_bsh = rman_get_bushandle(sc->mpic_res[2]);
262 MPIC_WRITE(mv_mpic_sc, MPIC_CTRL, 1);
263 MPIC_CPU_WRITE(mv_mpic_sc, MPIC_CTP, 0);
265 val = MPIC_READ(mv_mpic_sc, MPIC_CTRL);
266 sc->nirqs = MPIC_CTRL_NIRQS(val);
269 if (mv_mpic_register_isrcs(sc) != 0) {
270 device_printf(dev, "could not register PIC ISRCs\n");
271 bus_release_resources(dev, mv_mpic_spec, sc->mpic_res);
275 OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
277 if (intr_pic_register(dev, OF_xref_from_device(dev)) == NULL) {
278 device_printf(dev, "could not register PIC\n");
279 bus_release_resources(dev, mv_mpic_spec, sc->mpic_res);
293 struct mv_mpic_softc *sc;
294 uint32_t cause, irqsrc;
299 cpuid = PCPU_GET(cpuid);
302 for (cause = MPIC_CPU_READ(sc, MPIC_PPI_CAUSE); cause > 0;
303 cause >>= 1, irq++) {
305 irqsrc = MPIC_READ(sc, MPIC_INT_CTL(irq));
306 if ((irqsrc & MPIC_INT_IRQ_FIQ_MASK(cpuid)) == 0)
308 if (intr_isrc_dispatch(&sc->mpic_isrcs[irq].mmi_isrc,
309 curthread->td_intr_frame) != 0) {
311 device_printf(sc->sc_dev, "Stray irq %u "
317 return (FILTER_HANDLED);
321 mpic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
325 irq = ((struct mv_mpic_irqsrc *)isrc)->mmi_irq;
330 mpic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
334 irq = ((struct mv_mpic_irqsrc *)isrc)->mmi_irq;
335 mpic_unmask_irq(irq);
339 mpic_map_intr(device_t dev, struct intr_map_data *data,
340 struct intr_irqsrc **isrcp)
342 struct intr_map_data_fdt *daf;
343 struct mv_mpic_softc *sc;
345 if (data->type != INTR_MAP_DATA_FDT)
348 sc = device_get_softc(dev);
349 daf = (struct intr_map_data_fdt *)data;
351 if (daf->ncells !=1 || daf->cells[0] >= sc->nirqs)
354 *isrcp = &sc->mpic_isrcs[daf->cells[0]].mmi_isrc;
359 mpic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
362 mpic_disable_intr(dev, isrc);
366 mpic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
369 mpic_enable_intr(dev, isrc);
373 mpic_post_filter(device_t dev, struct intr_irqsrc *isrc)
378 static device_method_t mv_mpic_methods[] = {
379 DEVMETHOD(device_probe, mv_mpic_probe),
380 DEVMETHOD(device_attach, mv_mpic_attach),
383 DEVMETHOD(pic_disable_intr, mpic_disable_intr),
384 DEVMETHOD(pic_enable_intr, mpic_enable_intr),
385 DEVMETHOD(pic_map_intr, mpic_map_intr),
386 DEVMETHOD(pic_post_filter, mpic_post_filter),
387 DEVMETHOD(pic_post_ithread, mpic_post_ithread),
388 DEVMETHOD(pic_pre_ithread, mpic_pre_ithread),
393 static driver_t mv_mpic_driver = {
396 sizeof(struct mv_mpic_softc),
399 static devclass_t mv_mpic_devclass;
401 EARLY_DRIVER_MODULE(mpic, simplebus, mv_mpic_driver, mv_mpic_devclass, 0, 0,
402 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
406 arm_get_next_irq(int last)
408 u_int irq, next = -1;
410 irq = mv_mpic_get_cause() & MPIC_IRQ_MASK;
411 CTR2(KTR_INTR, "%s: irq:%#x", __func__, irq);
413 if (irq != MPIC_IRQ_MASK) {
414 if (irq == MPIC_INT_ERR)
415 irq = mv_mpic_get_cause_err();
416 if (irq == MPIC_INT_MSI)
417 irq = mv_mpic_get_msi();
421 CTR3(KTR_INTR, "%s: last=%d, next=%d", __func__, last, next);
426 * XXX We can make arm_enable_irq to operate on ICE and then mask/unmask only
427 * by ISM/ICM and remove access to ICE in masking operation
430 arm_mask_irq(uintptr_t nb)
437 arm_unmask_irq(uintptr_t nb)
445 mpic_unmask_msi(void)
448 mpic_unmask_irq(MPIC_INT_MSI);
452 mpic_unmask_irq_err(uintptr_t nb)
457 MPIC_WRITE(mv_mpic_sc, MPIC_ISE, MPIC_INT_ERR);
458 MPIC_CPU_WRITE(mv_mpic_sc, MPIC_ICM, MPIC_INT_ERR);
460 bit_off = nb - ERR_IRQ;
461 mask = MPIC_CPU_READ(mv_mpic_sc, MPIC_ERR_MASK);
462 mask |= (1 << bit_off);
463 MPIC_CPU_WRITE(mv_mpic_sc, MPIC_ERR_MASK, mask);
467 mpic_mask_irq_err(uintptr_t nb)
472 bit_off = nb - ERR_IRQ;
473 mask = MPIC_CPU_READ(mv_mpic_sc, MPIC_ERR_MASK);
474 mask &= ~(1 << bit_off);
475 MPIC_CPU_WRITE(mv_mpic_sc, MPIC_ERR_MASK, mask);
479 mpic_irq_is_percpu(uintptr_t nb)
488 mpic_unmask_irq(uintptr_t nb)
491 if (mpic_irq_is_percpu(nb))
492 MPIC_CPU_WRITE(mv_mpic_sc, MPIC_ICM, nb);
493 else if (nb < ERR_IRQ)
494 MPIC_WRITE(mv_mpic_sc, MPIC_ISE, nb);
495 else if (nb < MSI_IRQ)
496 mpic_unmask_irq_err(nb);
499 MPIC_CPU_WRITE(mv_mpic_sc, MPIC_IN_DRBL_MASK, 0xffffffff);
503 mpic_mask_irq(uintptr_t nb)
506 if (mpic_irq_is_percpu(nb))
507 MPIC_CPU_WRITE(mv_mpic_sc, MPIC_ISM, nb);
508 else if (nb < ERR_IRQ)
509 MPIC_WRITE(mv_mpic_sc, MPIC_ICE, nb);
510 else if (nb < MSI_IRQ)
511 mpic_mask_irq_err(nb);
515 mv_mpic_get_cause(void)
518 return (MPIC_CPU_READ(mv_mpic_sc, MPIC_IIACK));
522 mv_mpic_get_cause_err(void)
527 err_cause = MPIC_READ(mv_mpic_sc, MPIC_ERR_CAUSE);
530 bit_off = ffs(err_cause) - 1;
534 debugf("%s: irq:%x cause:%x\n", __func__, bit_off, err_cause);
535 return (ERR_IRQ + bit_off);
539 mv_mpic_get_msi(void)
544 KASSERT(mv_mpic_sc->drbl_bst != NULL, ("No doorbell in mv_mpic_get_msi"));
545 cause = MPIC_DRBL_READ(mv_mpic_sc, 0);
548 bit_off = ffs(cause) - 1;
552 debugf("%s: irq:%x cause:%x\n", __func__, bit_off, cause);
554 cause &= ~(1 << bit_off);
555 MPIC_DRBL_WRITE(mv_mpic_sc, 0, cause);
557 return (MSI_IRQ + bit_off);
561 mv_msi_data(int irq, uint64_t *addr, uint32_t *data)
563 u_long phys, base, size;
567 node = ofw_bus_get_node(mv_mpic_sc->sc_dev);
569 /* Get physical address of register space */
570 error = fdt_get_range(OF_parent(node), 0, &phys, &size);
572 printf("%s: Cannot get register physical address, err:%d",
577 /* Get offset of MPIC register space */
578 error = fdt_regsize(node, &base, &size);
580 printf("%s: Cannot get MPIC register offset, err:%d",
585 *addr = phys + base + MPIC_SOFT_INT;
586 *data = MPIC_SOFT_INT_DRBL1 | irq;
592 #if defined(SMP) && defined(SOC_MV_ARMADAXP)
594 intr_pic_init_secondary(void)
599 pic_ipi_send(cpuset_t cpus, u_int ipi)
604 for (i = 0; i < MAXCPU; i++)
605 if (CPU_ISSET(i, &cpus))
606 val |= (1 << (8 + i));
608 MPIC_WRITE(mv_mpic_sc, MPIC_SOFT_INT, val);
612 pic_ipi_read(int i __unused)
617 val = MPIC_CPU_READ(mv_mpic_sc, MPIC_IN_DRBL);
620 MPIC_CPU_WRITE(mv_mpic_sc, MPIC_IN_DRBL, ~(1 << ipi));
628 pic_ipi_clear(int ipi)