116694521SOleksandr Tymoshenko /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3af3dc4a7SPedro F. Giffuni *
416694521SOleksandr Tymoshenko * Copyright (c) 2006 Benno Rice.
516694521SOleksandr Tymoshenko * Copyright (C) 2007-2011 MARVELL INTERNATIONAL LTD.
6aa0ea9d0SGrzegorz Bernacki * Copyright (c) 2012 Semihalf.
716694521SOleksandr Tymoshenko * All rights reserved.
816694521SOleksandr Tymoshenko *
916694521SOleksandr Tymoshenko * Developed by Semihalf.
1016694521SOleksandr Tymoshenko *
1116694521SOleksandr Tymoshenko * Redistribution and use in source and binary forms, with or without
1216694521SOleksandr Tymoshenko * modification, are permitted provided that the following conditions
1316694521SOleksandr Tymoshenko * are met:
1416694521SOleksandr Tymoshenko * 1. Redistributions of source code must retain the above copyright
1516694521SOleksandr Tymoshenko * notice, this list of conditions and the following disclaimer.
1616694521SOleksandr Tymoshenko * 2. Redistributions in binary form must reproduce the above copyright
1716694521SOleksandr Tymoshenko * notice, this list of conditions and the following disclaimer in the
1816694521SOleksandr Tymoshenko * documentation and/or other materials provided with the distribution.
1916694521SOleksandr Tymoshenko *
2016694521SOleksandr Tymoshenko * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2116694521SOleksandr Tymoshenko * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2216694521SOleksandr Tymoshenko * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2316694521SOleksandr Tymoshenko * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2416694521SOleksandr Tymoshenko * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2516694521SOleksandr Tymoshenko * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2616694521SOleksandr Tymoshenko * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2716694521SOleksandr Tymoshenko * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2816694521SOleksandr Tymoshenko * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2916694521SOleksandr Tymoshenko * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3016694521SOleksandr Tymoshenko *
3116694521SOleksandr Tymoshenko * from: FreeBSD: //depot/projects/arm/src/sys/arm/xscale/pxa2x0/pxa2x0_icu.c, rev 1
3216694521SOleksandr Tymoshenko * from: FreeBSD: src/sys/arm/mv/ic.c,v 1.5 2011/02/08 01:49:30
3316694521SOleksandr Tymoshenko */
3416694521SOleksandr Tymoshenko
3516694521SOleksandr Tymoshenko #include <sys/cdefs.h>
36ca8e2078SWojciech Macek #include "opt_platform.h"
37ca8e2078SWojciech Macek
3816694521SOleksandr Tymoshenko #include <sys/param.h>
3916694521SOleksandr Tymoshenko #include <sys/systm.h>
4016694521SOleksandr Tymoshenko #include <sys/bus.h>
4116694521SOleksandr Tymoshenko #include <sys/kernel.h>
4216694521SOleksandr Tymoshenko #include <sys/cpuset.h>
4316694521SOleksandr Tymoshenko #include <sys/ktr.h>
44ca8e2078SWojciech Macek #include <sys/kdb.h>
4516694521SOleksandr Tymoshenko #include <sys/module.h>
46ca8e2078SWojciech Macek #include <sys/lock.h>
47ca8e2078SWojciech Macek #include <sys/mutex.h>
4816694521SOleksandr Tymoshenko #include <sys/rman.h>
49ca8e2078SWojciech Macek #include <sys/proc.h>
5008d94c6eSZbigniew Bodek #include <sys/smp.h>
5116694521SOleksandr Tymoshenko
5216694521SOleksandr Tymoshenko #include <machine/bus.h>
5316694521SOleksandr Tymoshenko #include <machine/intr.h>
5416694521SOleksandr Tymoshenko #include <machine/smp.h>
5516694521SOleksandr Tymoshenko
56aa0ea9d0SGrzegorz Bernacki #include <arm/mv/mvvar.h>
57ca8e2078SWojciech Macek #include <arm/mv/mvreg.h>
58aa0ea9d0SGrzegorz Bernacki
5916694521SOleksandr Tymoshenko #include <dev/ofw/ofw_bus.h>
6016694521SOleksandr Tymoshenko #include <dev/ofw/ofw_bus_subr.h>
61aa0ea9d0SGrzegorz Bernacki #include <dev/fdt/fdt_common.h>
6216694521SOleksandr Tymoshenko
63ca8e2078SWojciech Macek #include "pic_if.h"
64ca8e2078SWojciech Macek
65aa0ea9d0SGrzegorz Bernacki #ifdef DEBUG
66aa0ea9d0SGrzegorz Bernacki #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
67aa0ea9d0SGrzegorz Bernacki printf(fmt,##args); } while (0)
68aa0ea9d0SGrzegorz Bernacki #else
69aa0ea9d0SGrzegorz Bernacki #define debugf(fmt, args...)
70aa0ea9d0SGrzegorz Bernacki #endif
71aa0ea9d0SGrzegorz Bernacki
7208d94c6eSZbigniew Bodek #define MPIC_INT_LOCAL 3
73aa0ea9d0SGrzegorz Bernacki #define MPIC_INT_ERR 4
74aa0ea9d0SGrzegorz Bernacki #define MPIC_INT_MSI 96
7516694521SOleksandr Tymoshenko
767e53dd74SWojciech Macek #define MPIC_IRQ_MASK 0x3ff
7716694521SOleksandr Tymoshenko
7816694521SOleksandr Tymoshenko #define MPIC_CTRL 0x0
7916694521SOleksandr Tymoshenko #define MPIC_SOFT_INT 0x4
80aa0ea9d0SGrzegorz Bernacki #define MPIC_SOFT_INT_DRBL1 (1 << 5)
8116694521SOleksandr Tymoshenko #define MPIC_ERR_CAUSE 0x20
8216694521SOleksandr Tymoshenko #define MPIC_ISE 0x30
8316694521SOleksandr Tymoshenko #define MPIC_ICE 0x34
84ca8e2078SWojciech Macek #define MPIC_INT_CTL(irq) (0x100 + (irq)*4)
8516694521SOleksandr Tymoshenko
86ca8e2078SWojciech Macek #define MPIC_INT_IRQ_FIQ_MASK(cpuid) (0x101 << (cpuid))
87ca8e2078SWojciech Macek #define MPIC_CTRL_NIRQS(ctrl) (((ctrl) >> 2) & 0x3ff)
8816694521SOleksandr Tymoshenko
89ca8e2078SWojciech Macek #define MPIC_IN_DRBL 0x08
90ca8e2078SWojciech Macek #define MPIC_IN_DRBL_MASK 0x0c
91ca8e2078SWojciech Macek #define MPIC_PPI_CAUSE 0x10
92ca8e2078SWojciech Macek #define MPIC_CTP 0x40
93ca8e2078SWojciech Macek #define MPIC_IIACK 0x44
94ca8e2078SWojciech Macek #define MPIC_ISM 0x48
95ca8e2078SWojciech Macek #define MPIC_ICM 0x4c
9608d94c6eSZbigniew Bodek #define MPIC_ERR_MASK 0x50
9708d94c6eSZbigniew Bodek #define MPIC_LOCAL_MASK 0x54
9808d94c6eSZbigniew Bodek #define MPIC_CPU(n) (n) * 0x100
99ca8e2078SWojciech Macek
100ca8e2078SWojciech Macek #define MPIC_PPI 32
10116694521SOleksandr Tymoshenko
102bff6be3eSSvatopluk Kraus struct mv_mpic_irqsrc {
103bff6be3eSSvatopluk Kraus struct intr_irqsrc mmi_isrc;
104bff6be3eSSvatopluk Kraus u_int mmi_irq;
105bff6be3eSSvatopluk Kraus };
106bff6be3eSSvatopluk Kraus
10716694521SOleksandr Tymoshenko struct mv_mpic_softc {
108aa0ea9d0SGrzegorz Bernacki device_t sc_dev;
109ca8e2078SWojciech Macek struct resource * mpic_res[4];
11016694521SOleksandr Tymoshenko bus_space_tag_t mpic_bst;
11116694521SOleksandr Tymoshenko bus_space_handle_t mpic_bsh;
11216694521SOleksandr Tymoshenko bus_space_tag_t cpu_bst;
11316694521SOleksandr Tymoshenko bus_space_handle_t cpu_bsh;
114aa0ea9d0SGrzegorz Bernacki bus_space_tag_t drbl_bst;
115aa0ea9d0SGrzegorz Bernacki bus_space_handle_t drbl_bsh;
116ca8e2078SWojciech Macek struct mtx mtx;
117bff6be3eSSvatopluk Kraus struct mv_mpic_irqsrc * mpic_isrcs;
118ca8e2078SWojciech Macek int nirqs;
119ca8e2078SWojciech Macek void * intr_hand;
12016694521SOleksandr Tymoshenko };
12116694521SOleksandr Tymoshenko
12216694521SOleksandr Tymoshenko static struct resource_spec mv_mpic_spec[] = {
12316694521SOleksandr Tymoshenko { SYS_RES_MEMORY, 0, RF_ACTIVE },
12416694521SOleksandr Tymoshenko { SYS_RES_MEMORY, 1, RF_ACTIVE },
125ca8e2078SWojciech Macek { SYS_RES_MEMORY, 2, RF_ACTIVE | RF_OPTIONAL },
126ca8e2078SWojciech Macek { SYS_RES_IRQ, 0, RF_ACTIVE | RF_OPTIONAL },
12716694521SOleksandr Tymoshenko { -1, 0 }
12816694521SOleksandr Tymoshenko };
12916694521SOleksandr Tymoshenko
130ca8e2078SWojciech Macek static struct ofw_compat_data compat_data[] = {
131ca8e2078SWojciech Macek {"mrvl,mpic", true},
132ca8e2078SWojciech Macek {"marvell,mpic", true},
133ca8e2078SWojciech Macek {NULL, false}
134ca8e2078SWojciech Macek };
135ca8e2078SWojciech Macek
13616694521SOleksandr Tymoshenko static struct mv_mpic_softc *mv_mpic_sc = NULL;
13716694521SOleksandr Tymoshenko
13816694521SOleksandr Tymoshenko void mpic_send_ipi(int cpus, u_int ipi);
13916694521SOleksandr Tymoshenko
14016694521SOleksandr Tymoshenko static int mv_mpic_probe(device_t);
14116694521SOleksandr Tymoshenko static int mv_mpic_attach(device_t);
14216694521SOleksandr Tymoshenko uint32_t mv_mpic_get_cause(void);
14316694521SOleksandr Tymoshenko uint32_t mv_mpic_get_cause_err(void);
144aa0ea9d0SGrzegorz Bernacki uint32_t mv_mpic_get_msi(void);
145ca8e2078SWojciech Macek static void mpic_unmask_irq(uintptr_t nb);
146ca8e2078SWojciech Macek static void mpic_mask_irq(uintptr_t nb);
147ca8e2078SWojciech Macek static void mpic_mask_irq_err(uintptr_t nb);
148ca8e2078SWojciech Macek static void mpic_unmask_irq_err(uintptr_t nb);
149c7a65ae3SWojciech Macek static boolean_t mpic_irq_is_percpu(uintptr_t);
150ca8e2078SWojciech Macek static int mpic_intr(void *arg);
1510044ecdeSLuiz Otavio O Souza static void mpic_unmask_msi(void);
152244af1d4SMarcin Wojtas void mpic_ipi_send(device_t, struct intr_irqsrc*, cpuset_t, u_int);
153244af1d4SMarcin Wojtas int mpic_ipi_read(int);
154244af1d4SMarcin Wojtas void mpic_ipi_clear(int);
155ca8e2078SWojciech Macek
156ca8e2078SWojciech Macek #define MPIC_WRITE(softc, reg, val) \
157ca8e2078SWojciech Macek bus_space_write_4((softc)->mpic_bst, (softc)->mpic_bsh, (reg), (val))
158ca8e2078SWojciech Macek #define MPIC_READ(softc, reg) \
159ca8e2078SWojciech Macek bus_space_read_4((softc)->mpic_bst, (softc)->mpic_bsh, (reg))
16016694521SOleksandr Tymoshenko
16116694521SOleksandr Tymoshenko #define MPIC_CPU_WRITE(softc, reg, val) \
16216694521SOleksandr Tymoshenko bus_space_write_4((softc)->cpu_bst, (softc)->cpu_bsh, (reg), (val))
16316694521SOleksandr Tymoshenko #define MPIC_CPU_READ(softc, reg) \
16416694521SOleksandr Tymoshenko bus_space_read_4((softc)->cpu_bst, (softc)->cpu_bsh, (reg))
16516694521SOleksandr Tymoshenko
166aa0ea9d0SGrzegorz Bernacki #define MPIC_DRBL_WRITE(softc, reg, val) \
167aa0ea9d0SGrzegorz Bernacki bus_space_write_4((softc)->drbl_bst, (softc)->drbl_bsh, (reg), (val))
168aa0ea9d0SGrzegorz Bernacki #define MPIC_DRBL_READ(softc, reg) \
169aa0ea9d0SGrzegorz Bernacki bus_space_read_4((softc)->drbl_bst, (softc)->drbl_bsh, (reg))
170aa0ea9d0SGrzegorz Bernacki
17116694521SOleksandr Tymoshenko static int
mv_mpic_probe(device_t dev)17216694521SOleksandr Tymoshenko mv_mpic_probe(device_t dev)
17316694521SOleksandr Tymoshenko {
17416694521SOleksandr Tymoshenko
175add35ed5SIan Lepore if (!ofw_bus_status_okay(dev))
176add35ed5SIan Lepore return (ENXIO);
177add35ed5SIan Lepore
178ca8e2078SWojciech Macek if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
17916694521SOleksandr Tymoshenko return (ENXIO);
18016694521SOleksandr Tymoshenko
18116694521SOleksandr Tymoshenko device_set_desc(dev, "Marvell Integrated Interrupt Controller");
18216694521SOleksandr Tymoshenko return (0);
18316694521SOleksandr Tymoshenko }
18416694521SOleksandr Tymoshenko
185bff6be3eSSvatopluk Kraus static int
mv_mpic_register_isrcs(struct mv_mpic_softc * sc)186bff6be3eSSvatopluk Kraus mv_mpic_register_isrcs(struct mv_mpic_softc *sc)
187bff6be3eSSvatopluk Kraus {
188bff6be3eSSvatopluk Kraus int error;
189bff6be3eSSvatopluk Kraus uint32_t irq;
190bff6be3eSSvatopluk Kraus struct intr_irqsrc *isrc;
191bff6be3eSSvatopluk Kraus const char *name;
192bff6be3eSSvatopluk Kraus
193bff6be3eSSvatopluk Kraus sc->mpic_isrcs = malloc(sc->nirqs * sizeof (*sc->mpic_isrcs), M_DEVBUF,
194bff6be3eSSvatopluk Kraus M_WAITOK | M_ZERO);
195bff6be3eSSvatopluk Kraus
196bff6be3eSSvatopluk Kraus name = device_get_nameunit(sc->sc_dev);
197bff6be3eSSvatopluk Kraus for (irq = 0; irq < sc->nirqs; irq++) {
198bff6be3eSSvatopluk Kraus sc->mpic_isrcs[irq].mmi_irq = irq;
199bff6be3eSSvatopluk Kraus
200bff6be3eSSvatopluk Kraus isrc = &sc->mpic_isrcs[irq].mmi_isrc;
201bff6be3eSSvatopluk Kraus if (irq < MPIC_PPI) {
202bff6be3eSSvatopluk Kraus error = intr_isrc_register(isrc, sc->sc_dev,
203bff6be3eSSvatopluk Kraus INTR_ISRCF_PPI, "%s", name);
204bff6be3eSSvatopluk Kraus } else {
205bff6be3eSSvatopluk Kraus error = intr_isrc_register(isrc, sc->sc_dev, 0, "%s",
206bff6be3eSSvatopluk Kraus name);
207bff6be3eSSvatopluk Kraus }
208bff6be3eSSvatopluk Kraus if (error != 0) {
209bff6be3eSSvatopluk Kraus /* XXX call intr_isrc_deregister() */
210bff6be3eSSvatopluk Kraus device_printf(sc->sc_dev, "%s failed", __func__);
211bff6be3eSSvatopluk Kraus return (error);
212bff6be3eSSvatopluk Kraus }
213bff6be3eSSvatopluk Kraus }
214bff6be3eSSvatopluk Kraus return (0);
215bff6be3eSSvatopluk Kraus }
216bff6be3eSSvatopluk Kraus
21716694521SOleksandr Tymoshenko static int
mv_mpic_attach(device_t dev)21816694521SOleksandr Tymoshenko mv_mpic_attach(device_t dev)
21916694521SOleksandr Tymoshenko {
22016694521SOleksandr Tymoshenko struct mv_mpic_softc *sc;
22116694521SOleksandr Tymoshenko int error;
222ca8e2078SWojciech Macek uint32_t val;
22308d94c6eSZbigniew Bodek int cpu;
22416694521SOleksandr Tymoshenko
22516694521SOleksandr Tymoshenko sc = (struct mv_mpic_softc *)device_get_softc(dev);
22616694521SOleksandr Tymoshenko
22716694521SOleksandr Tymoshenko if (mv_mpic_sc != NULL)
22816694521SOleksandr Tymoshenko return (ENXIO);
22916694521SOleksandr Tymoshenko mv_mpic_sc = sc;
23016694521SOleksandr Tymoshenko
231aa0ea9d0SGrzegorz Bernacki sc->sc_dev = dev;
232aa0ea9d0SGrzegorz Bernacki
233ca8e2078SWojciech Macek mtx_init(&sc->mtx, "MPIC lock", NULL, MTX_SPIN);
234ca8e2078SWojciech Macek
23516694521SOleksandr Tymoshenko error = bus_alloc_resources(dev, mv_mpic_spec, sc->mpic_res);
23616694521SOleksandr Tymoshenko if (error) {
23716694521SOleksandr Tymoshenko device_printf(dev, "could not allocate resources\n");
23816694521SOleksandr Tymoshenko return (ENXIO);
23916694521SOleksandr Tymoshenko }
240ca8e2078SWojciech Macek if (sc->mpic_res[3] == NULL)
241ca8e2078SWojciech Macek device_printf(dev, "No interrupt to use.\n");
242ca8e2078SWojciech Macek else
243ca8e2078SWojciech Macek bus_setup_intr(dev, sc->mpic_res[3], INTR_TYPE_CLK,
244ca8e2078SWojciech Macek mpic_intr, NULL, sc, &sc->intr_hand);
24516694521SOleksandr Tymoshenko
24616694521SOleksandr Tymoshenko sc->mpic_bst = rman_get_bustag(sc->mpic_res[0]);
24716694521SOleksandr Tymoshenko sc->mpic_bsh = rman_get_bushandle(sc->mpic_res[0]);
24816694521SOleksandr Tymoshenko
24916694521SOleksandr Tymoshenko sc->cpu_bst = rman_get_bustag(sc->mpic_res[1]);
25016694521SOleksandr Tymoshenko sc->cpu_bsh = rman_get_bushandle(sc->mpic_res[1]);
25116694521SOleksandr Tymoshenko
252ca8e2078SWojciech Macek if (sc->mpic_res[2] != NULL) {
253ca8e2078SWojciech Macek /* This is required only if MSIs are used. */
254aa0ea9d0SGrzegorz Bernacki sc->drbl_bst = rman_get_bustag(sc->mpic_res[2]);
255aa0ea9d0SGrzegorz Bernacki sc->drbl_bsh = rman_get_bushandle(sc->mpic_res[2]);
256ca8e2078SWojciech Macek }
257aa0ea9d0SGrzegorz Bernacki
2580044ecdeSLuiz Otavio O Souza MPIC_WRITE(mv_mpic_sc, MPIC_CTRL, 1);
25916694521SOleksandr Tymoshenko MPIC_CPU_WRITE(mv_mpic_sc, MPIC_CTP, 0);
26016694521SOleksandr Tymoshenko
261ca8e2078SWojciech Macek val = MPIC_READ(mv_mpic_sc, MPIC_CTRL);
262ca8e2078SWojciech Macek sc->nirqs = MPIC_CTRL_NIRQS(val);
263ca8e2078SWojciech Macek
264bff6be3eSSvatopluk Kraus if (mv_mpic_register_isrcs(sc) != 0) {
265bff6be3eSSvatopluk Kraus device_printf(dev, "could not register PIC ISRCs\n");
266bff6be3eSSvatopluk Kraus bus_release_resources(dev, mv_mpic_spec, sc->mpic_res);
267bff6be3eSSvatopluk Kraus return (ENXIO);
268bff6be3eSSvatopluk Kraus }
269b488f7aaSZbigniew Bodek
270b488f7aaSZbigniew Bodek OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
271b488f7aaSZbigniew Bodek
2729346e913SAndrew Turner if (intr_pic_register(dev, OF_xref_from_device(dev)) == NULL) {
273ca8e2078SWojciech Macek device_printf(dev, "could not register PIC\n");
274ca8e2078SWojciech Macek bus_release_resources(dev, mv_mpic_spec, sc->mpic_res);
275ca8e2078SWojciech Macek return (ENXIO);
276ca8e2078SWojciech Macek }
277ca8e2078SWojciech Macek
278ca8e2078SWojciech Macek mpic_unmask_msi();
279aa0ea9d0SGrzegorz Bernacki
28008d94c6eSZbigniew Bodek /* Unmask CPU performance counters overflow irq */
28108d94c6eSZbigniew Bodek for (cpu = 0; cpu < mp_ncpus; cpu++)
28208d94c6eSZbigniew Bodek MPIC_CPU_WRITE(mv_mpic_sc, MPIC_CPU(cpu) + MPIC_LOCAL_MASK,
28308d94c6eSZbigniew Bodek (1 << cpu) | MPIC_CPU_READ(mv_mpic_sc,
28408d94c6eSZbigniew Bodek MPIC_CPU(cpu) + MPIC_LOCAL_MASK));
28508d94c6eSZbigniew Bodek
28616694521SOleksandr Tymoshenko return (0);
28716694521SOleksandr Tymoshenko }
28816694521SOleksandr Tymoshenko
289ca8e2078SWojciech Macek static int
mpic_intr(void * arg)290ca8e2078SWojciech Macek mpic_intr(void *arg)
291ca8e2078SWojciech Macek {
292ca8e2078SWojciech Macek struct mv_mpic_softc *sc;
293ca8e2078SWojciech Macek uint32_t cause, irqsrc;
294ca8e2078SWojciech Macek unsigned int irq;
295ca8e2078SWojciech Macek u_int cpuid;
296ca8e2078SWojciech Macek
297ca8e2078SWojciech Macek sc = arg;
298ca8e2078SWojciech Macek cpuid = PCPU_GET(cpuid);
299ca8e2078SWojciech Macek irq = 0;
300ca8e2078SWojciech Macek
301ca8e2078SWojciech Macek for (cause = MPIC_CPU_READ(sc, MPIC_PPI_CAUSE); cause > 0;
302ca8e2078SWojciech Macek cause >>= 1, irq++) {
303ca8e2078SWojciech Macek if (cause & 1) {
304ca8e2078SWojciech Macek irqsrc = MPIC_READ(sc, MPIC_INT_CTL(irq));
305ca8e2078SWojciech Macek if ((irqsrc & MPIC_INT_IRQ_FIQ_MASK(cpuid)) == 0)
306ca8e2078SWojciech Macek continue;
307bff6be3eSSvatopluk Kraus if (intr_isrc_dispatch(&sc->mpic_isrcs[irq].mmi_isrc,
308bff6be3eSSvatopluk Kraus curthread->td_intr_frame) != 0) {
309ca8e2078SWojciech Macek mpic_mask_irq(irq);
310bff6be3eSSvatopluk Kraus device_printf(sc->sc_dev, "Stray irq %u "
311bff6be3eSSvatopluk Kraus "disabled\n", irq);
312ca8e2078SWojciech Macek }
313ca8e2078SWojciech Macek }
314ca8e2078SWojciech Macek }
315ca8e2078SWojciech Macek
316ca8e2078SWojciech Macek return (FILTER_HANDLED);
317ca8e2078SWojciech Macek }
318ca8e2078SWojciech Macek
319ca8e2078SWojciech Macek static void
mpic_disable_intr(device_t dev,struct intr_irqsrc * isrc)320bff6be3eSSvatopluk Kraus mpic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
321ca8e2078SWojciech Macek {
322ca8e2078SWojciech Macek u_int irq;
323ca8e2078SWojciech Macek
324bff6be3eSSvatopluk Kraus irq = ((struct mv_mpic_irqsrc *)isrc)->mmi_irq;
325ca8e2078SWojciech Macek mpic_mask_irq(irq);
326ca8e2078SWojciech Macek }
327ca8e2078SWojciech Macek
328ca8e2078SWojciech Macek static void
mpic_enable_intr(device_t dev,struct intr_irqsrc * isrc)329bff6be3eSSvatopluk Kraus mpic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
330ca8e2078SWojciech Macek {
331ca8e2078SWojciech Macek u_int irq;
332ca8e2078SWojciech Macek
333bff6be3eSSvatopluk Kraus irq = ((struct mv_mpic_irqsrc *)isrc)->mmi_irq;
334ca8e2078SWojciech Macek mpic_unmask_irq(irq);
335ca8e2078SWojciech Macek }
336bff6be3eSSvatopluk Kraus
337bff6be3eSSvatopluk Kraus static int
mpic_map_intr(device_t dev,struct intr_map_data * data,struct intr_irqsrc ** isrcp)338bff6be3eSSvatopluk Kraus mpic_map_intr(device_t dev, struct intr_map_data *data,
339bff6be3eSSvatopluk Kraus struct intr_irqsrc **isrcp)
340bff6be3eSSvatopluk Kraus {
341cd642c88SSvatopluk Kraus struct intr_map_data_fdt *daf;
342bff6be3eSSvatopluk Kraus struct mv_mpic_softc *sc;
343bff6be3eSSvatopluk Kraus
344cd642c88SSvatopluk Kraus if (data->type != INTR_MAP_DATA_FDT)
345cd642c88SSvatopluk Kraus return (ENOTSUP);
346bff6be3eSSvatopluk Kraus
347cd642c88SSvatopluk Kraus sc = device_get_softc(dev);
348cd642c88SSvatopluk Kraus daf = (struct intr_map_data_fdt *)data;
349cd642c88SSvatopluk Kraus
350cd642c88SSvatopluk Kraus if (daf->ncells !=1 || daf->cells[0] >= sc->nirqs)
351bff6be3eSSvatopluk Kraus return (EINVAL);
352bff6be3eSSvatopluk Kraus
353cd642c88SSvatopluk Kraus *isrcp = &sc->mpic_isrcs[daf->cells[0]].mmi_isrc;
354bff6be3eSSvatopluk Kraus return (0);
355bff6be3eSSvatopluk Kraus }
356bff6be3eSSvatopluk Kraus
357ca8e2078SWojciech Macek static void
mpic_pre_ithread(device_t dev,struct intr_irqsrc * isrc)358ca8e2078SWojciech Macek mpic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
359ca8e2078SWojciech Macek {
360ca8e2078SWojciech Macek
361bff6be3eSSvatopluk Kraus mpic_disable_intr(dev, isrc);
362ca8e2078SWojciech Macek }
363ca8e2078SWojciech Macek
364ca8e2078SWojciech Macek static void
mpic_post_ithread(device_t dev,struct intr_irqsrc * isrc)365ca8e2078SWojciech Macek mpic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
366ca8e2078SWojciech Macek {
367ca8e2078SWojciech Macek
368bff6be3eSSvatopluk Kraus mpic_enable_intr(dev, isrc);
369ca8e2078SWojciech Macek }
370babd7717SSvatopluk Kraus
371babd7717SSvatopluk Kraus static void
mpic_post_filter(device_t dev,struct intr_irqsrc * isrc)372babd7717SSvatopluk Kraus mpic_post_filter(device_t dev, struct intr_irqsrc *isrc)
373babd7717SSvatopluk Kraus {
374babd7717SSvatopluk Kraus }
375ca8e2078SWojciech Macek
37616694521SOleksandr Tymoshenko static device_method_t mv_mpic_methods[] = {
37716694521SOleksandr Tymoshenko DEVMETHOD(device_probe, mv_mpic_probe),
37816694521SOleksandr Tymoshenko DEVMETHOD(device_attach, mv_mpic_attach),
379ca8e2078SWojciech Macek
380bff6be3eSSvatopluk Kraus DEVMETHOD(pic_disable_intr, mpic_disable_intr),
381bff6be3eSSvatopluk Kraus DEVMETHOD(pic_enable_intr, mpic_enable_intr),
382bff6be3eSSvatopluk Kraus DEVMETHOD(pic_map_intr, mpic_map_intr),
383babd7717SSvatopluk Kraus DEVMETHOD(pic_post_filter, mpic_post_filter),
384ca8e2078SWojciech Macek DEVMETHOD(pic_post_ithread, mpic_post_ithread),
385ca8e2078SWojciech Macek DEVMETHOD(pic_pre_ithread, mpic_pre_ithread),
386244af1d4SMarcin Wojtas DEVMETHOD(pic_ipi_send, mpic_ipi_send),
38716694521SOleksandr Tymoshenko { 0, 0 }
38816694521SOleksandr Tymoshenko };
38916694521SOleksandr Tymoshenko
39016694521SOleksandr Tymoshenko static driver_t mv_mpic_driver = {
39116694521SOleksandr Tymoshenko "mpic",
39216694521SOleksandr Tymoshenko mv_mpic_methods,
39316694521SOleksandr Tymoshenko sizeof(struct mv_mpic_softc),
39416694521SOleksandr Tymoshenko };
39516694521SOleksandr Tymoshenko
396a3b866cbSJohn Baldwin EARLY_DRIVER_MODULE(mpic, simplebus, mv_mpic_driver, 0, 0,
397da081cb5SZbigniew Bodek BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
39816694521SOleksandr Tymoshenko
399ca8e2078SWojciech Macek static void
mpic_unmask_msi(void)400ca8e2078SWojciech Macek mpic_unmask_msi(void)
401ca8e2078SWojciech Macek {
402ca8e2078SWojciech Macek
403ca8e2078SWojciech Macek mpic_unmask_irq(MPIC_INT_MSI);
404ca8e2078SWojciech Macek }
405ca8e2078SWojciech Macek
406ca8e2078SWojciech Macek static void
mpic_unmask_irq_err(uintptr_t nb)407ca8e2078SWojciech Macek mpic_unmask_irq_err(uintptr_t nb)
408ca8e2078SWojciech Macek {
40916694521SOleksandr Tymoshenko uint32_t mask;
41016694521SOleksandr Tymoshenko uint8_t bit_off;
41116694521SOleksandr Tymoshenko
4120044ecdeSLuiz Otavio O Souza MPIC_WRITE(mv_mpic_sc, MPIC_ISE, MPIC_INT_ERR);
413aa0ea9d0SGrzegorz Bernacki MPIC_CPU_WRITE(mv_mpic_sc, MPIC_ICM, MPIC_INT_ERR);
41416694521SOleksandr Tymoshenko
415aa0ea9d0SGrzegorz Bernacki bit_off = nb - ERR_IRQ;
41616694521SOleksandr Tymoshenko mask = MPIC_CPU_READ(mv_mpic_sc, MPIC_ERR_MASK);
41716694521SOleksandr Tymoshenko mask |= (1 << bit_off);
41816694521SOleksandr Tymoshenko MPIC_CPU_WRITE(mv_mpic_sc, MPIC_ERR_MASK, mask);
41916694521SOleksandr Tymoshenko }
42016694521SOleksandr Tymoshenko
421aa0ea9d0SGrzegorz Bernacki static void
mpic_mask_irq_err(uintptr_t nb)422ca8e2078SWojciech Macek mpic_mask_irq_err(uintptr_t nb)
423ca8e2078SWojciech Macek {
424ca8e2078SWojciech Macek uint32_t mask;
425ca8e2078SWojciech Macek uint8_t bit_off;
426ca8e2078SWojciech Macek
427ca8e2078SWojciech Macek bit_off = nb - ERR_IRQ;
428ca8e2078SWojciech Macek mask = MPIC_CPU_READ(mv_mpic_sc, MPIC_ERR_MASK);
429ca8e2078SWojciech Macek mask &= ~(1 << bit_off);
430ca8e2078SWojciech Macek MPIC_CPU_WRITE(mv_mpic_sc, MPIC_ERR_MASK, mask);
431ca8e2078SWojciech Macek }
432ca8e2078SWojciech Macek
433c7a65ae3SWojciech Macek static boolean_t
mpic_irq_is_percpu(uintptr_t nb)434c7a65ae3SWojciech Macek mpic_irq_is_percpu(uintptr_t nb)
435c7a65ae3SWojciech Macek {
436c7a65ae3SWojciech Macek if (nb < MPIC_PPI)
437c7a65ae3SWojciech Macek return TRUE;
438c7a65ae3SWojciech Macek
439c7a65ae3SWojciech Macek return FALSE;
440c7a65ae3SWojciech Macek }
441c7a65ae3SWojciech Macek
442ca8e2078SWojciech Macek static void
mpic_unmask_irq(uintptr_t nb)443ca8e2078SWojciech Macek mpic_unmask_irq(uintptr_t nb)
444aa0ea9d0SGrzegorz Bernacki {
445aa0ea9d0SGrzegorz Bernacki
44608d94c6eSZbigniew Bodek #ifdef SMP
44708d94c6eSZbigniew Bodek int cpu;
44808d94c6eSZbigniew Bodek
44908d94c6eSZbigniew Bodek if (nb == MPIC_INT_LOCAL) {
45008d94c6eSZbigniew Bodek for (cpu = 0; cpu < mp_ncpus; cpu++)
45108d94c6eSZbigniew Bodek MPIC_CPU_WRITE(mv_mpic_sc,
45208d94c6eSZbigniew Bodek MPIC_CPU(cpu) + MPIC_ICM, nb);
45308d94c6eSZbigniew Bodek return;
45408d94c6eSZbigniew Bodek }
45508d94c6eSZbigniew Bodek #endif
456c7a65ae3SWojciech Macek if (mpic_irq_is_percpu(nb))
457ca8e2078SWojciech Macek MPIC_CPU_WRITE(mv_mpic_sc, MPIC_ICM, nb);
458c7a65ae3SWojciech Macek else if (nb < ERR_IRQ)
459c7a65ae3SWojciech Macek MPIC_WRITE(mv_mpic_sc, MPIC_ISE, nb);
460c7a65ae3SWojciech Macek else if (nb < MSI_IRQ)
461ca8e2078SWojciech Macek mpic_unmask_irq_err(nb);
462ca8e2078SWojciech Macek
463ca8e2078SWojciech Macek if (nb == 0)
464ca8e2078SWojciech Macek MPIC_CPU_WRITE(mv_mpic_sc, MPIC_IN_DRBL_MASK, 0xffffffff);
465ca8e2078SWojciech Macek }
466ca8e2078SWojciech Macek
467ca8e2078SWojciech Macek static void
mpic_mask_irq(uintptr_t nb)468ca8e2078SWojciech Macek mpic_mask_irq(uintptr_t nb)
469ca8e2078SWojciech Macek {
470ca8e2078SWojciech Macek
47108d94c6eSZbigniew Bodek #ifdef SMP
47208d94c6eSZbigniew Bodek int cpu;
47308d94c6eSZbigniew Bodek
47408d94c6eSZbigniew Bodek if (nb == MPIC_INT_LOCAL) {
47508d94c6eSZbigniew Bodek for (cpu = 0; cpu < mp_ncpus; cpu++)
47608d94c6eSZbigniew Bodek MPIC_CPU_WRITE(mv_mpic_sc,
47708d94c6eSZbigniew Bodek MPIC_CPU(cpu) + MPIC_ISM, nb);
47808d94c6eSZbigniew Bodek return;
47908d94c6eSZbigniew Bodek }
48008d94c6eSZbigniew Bodek #endif
481c7a65ae3SWojciech Macek if (mpic_irq_is_percpu(nb))
482ca8e2078SWojciech Macek MPIC_CPU_WRITE(mv_mpic_sc, MPIC_ISM, nb);
483c7a65ae3SWojciech Macek else if (nb < ERR_IRQ)
484c7a65ae3SWojciech Macek MPIC_WRITE(mv_mpic_sc, MPIC_ICE, nb);
485c7a65ae3SWojciech Macek else if (nb < MSI_IRQ)
486ca8e2078SWojciech Macek mpic_mask_irq_err(nb);
487aa0ea9d0SGrzegorz Bernacki }
488aa0ea9d0SGrzegorz Bernacki
48916694521SOleksandr Tymoshenko uint32_t
mv_mpic_get_cause(void)49016694521SOleksandr Tymoshenko mv_mpic_get_cause(void)
49116694521SOleksandr Tymoshenko {
49216694521SOleksandr Tymoshenko
49316694521SOleksandr Tymoshenko return (MPIC_CPU_READ(mv_mpic_sc, MPIC_IIACK));
49416694521SOleksandr Tymoshenko }
49516694521SOleksandr Tymoshenko
49616694521SOleksandr Tymoshenko uint32_t
mv_mpic_get_cause_err(void)49716694521SOleksandr Tymoshenko mv_mpic_get_cause_err(void)
49816694521SOleksandr Tymoshenko {
49916694521SOleksandr Tymoshenko uint32_t err_cause;
50016694521SOleksandr Tymoshenko uint8_t bit_off;
50116694521SOleksandr Tymoshenko
5020044ecdeSLuiz Otavio O Souza err_cause = MPIC_READ(mv_mpic_sc, MPIC_ERR_CAUSE);
50316694521SOleksandr Tymoshenko
50416694521SOleksandr Tymoshenko if (err_cause)
50516694521SOleksandr Tymoshenko bit_off = ffs(err_cause) - 1;
50616694521SOleksandr Tymoshenko else
50716694521SOleksandr Tymoshenko return (-1);
508aa0ea9d0SGrzegorz Bernacki
509aa0ea9d0SGrzegorz Bernacki debugf("%s: irq:%x cause:%x\n", __func__, bit_off, err_cause);
510aa0ea9d0SGrzegorz Bernacki return (ERR_IRQ + bit_off);
511aa0ea9d0SGrzegorz Bernacki }
512aa0ea9d0SGrzegorz Bernacki
513aa0ea9d0SGrzegorz Bernacki uint32_t
mv_mpic_get_msi(void)514aa0ea9d0SGrzegorz Bernacki mv_mpic_get_msi(void)
515aa0ea9d0SGrzegorz Bernacki {
516aa0ea9d0SGrzegorz Bernacki uint32_t cause;
517aa0ea9d0SGrzegorz Bernacki uint8_t bit_off;
518aa0ea9d0SGrzegorz Bernacki
519ca8e2078SWojciech Macek KASSERT(mv_mpic_sc->drbl_bst != NULL, ("No doorbell in mv_mpic_get_msi"));
520aa0ea9d0SGrzegorz Bernacki cause = MPIC_DRBL_READ(mv_mpic_sc, 0);
521aa0ea9d0SGrzegorz Bernacki
522aa0ea9d0SGrzegorz Bernacki if (cause)
523aa0ea9d0SGrzegorz Bernacki bit_off = ffs(cause) - 1;
524aa0ea9d0SGrzegorz Bernacki else
525aa0ea9d0SGrzegorz Bernacki return (-1);
526aa0ea9d0SGrzegorz Bernacki
527aa0ea9d0SGrzegorz Bernacki debugf("%s: irq:%x cause:%x\n", __func__, bit_off, cause);
528aa0ea9d0SGrzegorz Bernacki
529aa0ea9d0SGrzegorz Bernacki cause &= ~(1 << bit_off);
530aa0ea9d0SGrzegorz Bernacki MPIC_DRBL_WRITE(mv_mpic_sc, 0, cause);
531aa0ea9d0SGrzegorz Bernacki
532aa0ea9d0SGrzegorz Bernacki return (MSI_IRQ + bit_off);
533aa0ea9d0SGrzegorz Bernacki }
534aa0ea9d0SGrzegorz Bernacki
535aa0ea9d0SGrzegorz Bernacki int
mv_msi_data(int irq,uint64_t * addr,uint32_t * data)536aa0ea9d0SGrzegorz Bernacki mv_msi_data(int irq, uint64_t *addr, uint32_t *data)
537aa0ea9d0SGrzegorz Bernacki {
538aa0ea9d0SGrzegorz Bernacki u_long phys, base, size;
539aa0ea9d0SGrzegorz Bernacki phandle_t node;
540aa0ea9d0SGrzegorz Bernacki int error;
541aa0ea9d0SGrzegorz Bernacki
542aa0ea9d0SGrzegorz Bernacki node = ofw_bus_get_node(mv_mpic_sc->sc_dev);
543aa0ea9d0SGrzegorz Bernacki
544255eff3bSPedro F. Giffuni /* Get physical address of register space */
545aa0ea9d0SGrzegorz Bernacki error = fdt_get_range(OF_parent(node), 0, &phys, &size);
546aa0ea9d0SGrzegorz Bernacki if (error) {
547aa0ea9d0SGrzegorz Bernacki printf("%s: Cannot get register physical address, err:%d",
548aa0ea9d0SGrzegorz Bernacki __func__, error);
549aa0ea9d0SGrzegorz Bernacki return (error);
550aa0ea9d0SGrzegorz Bernacki }
551aa0ea9d0SGrzegorz Bernacki
552aa0ea9d0SGrzegorz Bernacki /* Get offset of MPIC register space */
553aa0ea9d0SGrzegorz Bernacki error = fdt_regsize(node, &base, &size);
554aa0ea9d0SGrzegorz Bernacki if (error) {
555aa0ea9d0SGrzegorz Bernacki printf("%s: Cannot get MPIC register offset, err:%d",
556aa0ea9d0SGrzegorz Bernacki __func__, error);
557aa0ea9d0SGrzegorz Bernacki return (error);
558aa0ea9d0SGrzegorz Bernacki }
559aa0ea9d0SGrzegorz Bernacki
560aa0ea9d0SGrzegorz Bernacki *addr = phys + base + MPIC_SOFT_INT;
561aa0ea9d0SGrzegorz Bernacki *data = MPIC_SOFT_INT_DRBL1 | irq;
562aa0ea9d0SGrzegorz Bernacki
563aa0ea9d0SGrzegorz Bernacki return (0);
56416694521SOleksandr Tymoshenko }
56516694521SOleksandr Tymoshenko
56616694521SOleksandr Tymoshenko void
mpic_ipi_send(device_t dev,struct intr_irqsrc * isrc,cpuset_t cpus,u_int ipi)567244af1d4SMarcin Wojtas mpic_ipi_send(device_t dev, struct intr_irqsrc *isrc, cpuset_t cpus, u_int ipi)
56816694521SOleksandr Tymoshenko {
56916694521SOleksandr Tymoshenko uint32_t val, i;
57016694521SOleksandr Tymoshenko
57116694521SOleksandr Tymoshenko val = 0x00000000;
57216694521SOleksandr Tymoshenko for (i = 0; i < MAXCPU; i++)
57316694521SOleksandr Tymoshenko if (CPU_ISSET(i, &cpus))
57416694521SOleksandr Tymoshenko val |= (1 << (8 + i));
57516694521SOleksandr Tymoshenko val |= ipi;
5760044ecdeSLuiz Otavio O Souza MPIC_WRITE(mv_mpic_sc, MPIC_SOFT_INT, val);
57716694521SOleksandr Tymoshenko }
57816694521SOleksandr Tymoshenko
57916694521SOleksandr Tymoshenko int
mpic_ipi_read(int i __unused)580244af1d4SMarcin Wojtas mpic_ipi_read(int i __unused)
58116694521SOleksandr Tymoshenko {
58216694521SOleksandr Tymoshenko uint32_t val;
58317fb49c1SAndrew Turner int ipi;
58416694521SOleksandr Tymoshenko
585aa0ea9d0SGrzegorz Bernacki val = MPIC_CPU_READ(mv_mpic_sc, MPIC_IN_DRBL);
58617fb49c1SAndrew Turner if (val) {
58717fb49c1SAndrew Turner ipi = ffs(val) - 1;
58817fb49c1SAndrew Turner MPIC_CPU_WRITE(mv_mpic_sc, MPIC_IN_DRBL, ~(1 << ipi));
58917fb49c1SAndrew Turner return (ipi);
59017fb49c1SAndrew Turner }
59116694521SOleksandr Tymoshenko
59216694521SOleksandr Tymoshenko return (0x3ff);
59316694521SOleksandr Tymoshenko }
59416694521SOleksandr Tymoshenko
59516694521SOleksandr Tymoshenko void
mpic_ipi_clear(int ipi)596244af1d4SMarcin Wojtas mpic_ipi_clear(int ipi)
59716694521SOleksandr Tymoshenko {
59816694521SOleksandr Tymoshenko }
599