2 * Copyright 2011 Nathan Whitehorn
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
41 #include <machine/bus.h>
42 #include <machine/intr_machdep.h>
43 #include <machine/md_var.h>
44 #include <machine/rtas.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include "phyp-hvcall.h"
52 #define XICP_PRIORITY 5 /* Random non-zero number */
54 #define MAX_XICP_IRQS (1<<24) /* 24-bit XIRR field */
56 static int xicp_probe(device_t);
57 static int xicp_attach(device_t);
58 static int xics_probe(device_t);
59 static int xics_attach(device_t);
61 static void xicp_bind(device_t dev, u_int irq, cpuset_t cpumask);
62 static void xicp_dispatch(device_t, struct trapframe *);
63 static void xicp_enable(device_t, u_int, u_int);
64 static void xicp_eoi(device_t, u_int);
65 static void xicp_ipi(device_t, u_int);
66 static void xicp_mask(device_t, u_int);
67 static void xicp_unmask(device_t, u_int);
69 static device_method_t xicp_methods[] = {
70 /* Device interface */
71 DEVMETHOD(device_probe, xicp_probe),
72 DEVMETHOD(device_attach, xicp_attach),
75 DEVMETHOD(pic_bind, xicp_bind),
76 DEVMETHOD(pic_dispatch, xicp_dispatch),
77 DEVMETHOD(pic_enable, xicp_enable),
78 DEVMETHOD(pic_eoi, xicp_eoi),
79 DEVMETHOD(pic_ipi, xicp_ipi),
80 DEVMETHOD(pic_mask, xicp_mask),
81 DEVMETHOD(pic_unmask, xicp_unmask),
86 static device_method_t xics_methods[] = {
87 /* Device interface */
88 DEVMETHOD(device_probe, xics_probe),
89 DEVMETHOD(device_attach, xics_attach),
102 /* XXX: inefficient -- hash table? tree? */
110 static driver_t xicp_driver = {
113 sizeof(struct xicp_softc)
116 static driver_t xics_driver = {
122 static devclass_t xicp_devclass;
123 static devclass_t xics_devclass;
125 EARLY_DRIVER_MODULE(xicp, nexus, xicp_driver, xicp_devclass, 0, 0,
126 BUS_PASS_INTERRUPT-1);
127 EARLY_DRIVER_MODULE(xics, nexus, xics_driver, xics_devclass, 0, 0,
131 xicp_probe(device_t dev)
133 if (ofw_bus_get_name(dev) == NULL || strcmp(ofw_bus_get_name(dev),
134 "interrupt-controller") != 0)
137 if (!ofw_bus_is_compatible(dev, "ibm,ppc-xicp"))
140 device_set_desc(dev, "PAPR virtual interrupt controller");
141 return (BUS_PROBE_GENERIC);
145 xics_probe(device_t dev)
147 if (ofw_bus_get_name(dev) == NULL || strcmp(ofw_bus_get_name(dev),
148 "interrupt-controller") != 0)
151 if (!ofw_bus_is_compatible(dev, "ibm,ppc-xics"))
154 device_set_desc(dev, "PAPR virtual interrupt source");
155 return (BUS_PROBE_GENERIC);
159 xicp_attach(device_t dev)
161 struct xicp_softc *sc = device_get_softc(dev);
162 phandle_t phandle = ofw_bus_get_node(dev);
164 mtx_init(&sc->sc_mtx, "XICP", NULL, MTX_DEF);
167 sc->ibm_int_on = rtas_token_lookup("ibm,int-on");
168 sc->ibm_int_off = rtas_token_lookup("ibm,int-off");
169 sc->ibm_set_xive = rtas_token_lookup("ibm,set-xive");
170 sc->ibm_get_xive = rtas_token_lookup("ibm,get-xive");
172 if (OF_getproplen(phandle, "ibm,phandle") > 0)
173 OF_getprop(phandle, "ibm,phandle", &phandle, sizeof(phandle));
175 powerpc_register_pic(dev, phandle, MAX_XICP_IRQS,
176 1 /* Number of IPIs */, FALSE);
183 xics_attach(device_t dev)
185 phandle_t phandle = ofw_bus_get_node(dev);
187 if (OF_getproplen(phandle, "ibm,phandle") > 0)
188 OF_getprop(phandle, "ibm,phandle", &phandle, sizeof(phandle));
190 /* The XICP (root PIC) will handle all our interrupts */
191 powerpc_register_pic(root_pic, phandle, MAX_XICP_IRQS,
192 1 /* Number of IPIs */, FALSE);
202 xicp_bind(device_t dev, u_int irq, cpuset_t cpumask)
204 struct xicp_softc *sc = device_get_softc(dev);
208 * This doesn't appear to actually support affinity groups, so just
212 if (CPU_ISSET(cpu, &cpumask)) break;
214 rtas_call_method(sc->ibm_set_xive, 3, 1, irq, cpu, XICP_PRIORITY,
219 xicp_dispatch(device_t dev, struct trapframe *tf)
221 struct xicp_softc *sc;
225 sc = device_get_softc(dev);
227 /* Return value in R4, use the PFT call */
228 phyp_pft_hcall(H_XIRR, 0, 0, 0, 0, &xirr, &junk, &junk);
231 if (xirr == 0) { /* No more pending interrupts? */
232 phyp_hcall(H_CPPR, (uint64_t)0xff);
235 if (xirr == XICP_IPI) { /* Magic number for IPIs */
236 xirr = MAX_XICP_IRQS; /* Map to FreeBSD magic */
237 phyp_hcall(H_IPI, (uint64_t)(PCPU_GET(cpuid)),
238 0xff); /* Clear IPI */
241 /* XXX: super inefficient */
242 for (i = 0; i < sc->nintvecs; i++) {
243 if (sc->intvecs[i].irq == xirr)
247 KASSERT(i < sc->nintvecs, ("Unmapped XIRR"));
248 powerpc_dispatch_intr(sc->intvecs[i].vector, tf);
253 xicp_enable(device_t dev, u_int irq, u_int vector)
255 struct xicp_softc *sc;
258 sc = device_get_softc(dev);
260 KASSERT(sc->nintvecs + 1 < sizeof(sc->intvecs)/sizeof(sc->intvecs[0]),
261 ("Too many XICP interrupts"));
263 mtx_lock(&sc->sc_mtx);
264 sc->intvecs[sc->nintvecs].irq = irq;
265 sc->intvecs[sc->nintvecs].vector = vector;
268 mtx_unlock(&sc->sc_mtx);
270 /* IPIs are also enabled */
271 if (irq == MAX_XICP_IRQS)
274 /* Bind to this CPU to start: distrib. ID is last entry in gserver# */
275 cpu = PCPU_GET(cpuid);
276 rtas_call_method(sc->ibm_set_xive, 3, 1, irq, cpu, XICP_PRIORITY,
278 xicp_unmask(dev, irq);
282 xicp_eoi(device_t dev, u_int irq)
286 if (irq == MAX_XICP_IRQS) /* Remap IPI interrupt to internal value */
288 xirr = irq | (XICP_PRIORITY << 24);
290 phyp_hcall(H_EOI, xirr);
294 xicp_ipi(device_t dev, u_int cpu)
297 phyp_hcall(H_IPI, (uint64_t)cpu, XICP_PRIORITY);
301 xicp_mask(device_t dev, u_int irq)
303 struct xicp_softc *sc = device_get_softc(dev);
306 if (irq == MAX_XICP_IRQS)
309 rtas_call_method(sc->ibm_int_off, 1, 1, irq, &status);
313 xicp_unmask(device_t dev, u_int irq)
315 struct xicp_softc *sc = device_get_softc(dev);
318 if (irq == MAX_XICP_IRQS)
321 rtas_call_method(sc->ibm_int_on, 1, 1, irq, &status);