2 * Copyright (c) 2014-2015 Ruslan Bukin <br@bsdpad.com>
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
32 * BERI interface for Virtio MMIO bus.
34 * This driver provides interrupt-engine for software-implemented
35 * Virtio MMIO backend.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
46 #include <sys/malloc.h>
48 #include <sys/timeet.h>
49 #include <sys/timetc.h>
50 #include <sys/watchdog.h>
52 #include <machine/bus.h>
53 #include <machine/fdt.h>
54 #include <machine/cpu.h>
55 #include <machine/cache.h>
57 #include <dev/fdt/fdt_common.h>
58 #include <dev/ofw/openfirm.h>
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
62 #include <dev/beri/virtio/virtio_mmio_platform.h>
63 #include <dev/virtio/mmio/virtio_mmio.h>
64 #include <dev/altera/pio/pio.h>
66 #include "virtio_mmio_if.h"
69 static void platform_intr(void *arg);
71 struct virtio_mmio_platform_softc {
72 struct resource *res[1];
75 bus_space_handle_t bsh;
77 void (*intr_handler)(void *);
85 setup_pio(struct virtio_mmio_platform_softc *sc, char *name, device_t *dev)
92 if ((node = ofw_bus_get_node(sc->dev)) == -1)
95 if (OF_searchencprop(node, name, &xref,
96 sizeof(xref)) == -1) {
100 pio_node = OF_node_from_xref(xref);
101 SLIST_FOREACH(ic, &fdt_ic_list_head, fdt_ics) {
102 if (ic->iph == pio_node) {
104 PIO_CONFIGURE(*dev, PIO_OUT_ALL,
114 virtio_mmio_platform_probe(device_t dev)
117 if (!ofw_bus_status_okay(dev))
120 if (!ofw_bus_is_compatible(dev, "beri,virtio_mmio_platform"))
123 device_set_desc(dev, "Virtio MMIO platform");
124 return (BUS_PROBE_DEFAULT);
128 virtio_mmio_platform_attach(device_t dev)
130 struct virtio_mmio_platform_softc *sc;
134 sc = device_get_softc(dev);
138 if ((setup_pio(sc, "pio-send", &sc->pio_send) != 0) ||
139 (setup_pio(sc, "pio-recv", &sc->pio_recv) != 0))
142 if ((node = ofw_bus_get_node(sc->dev)) == -1)
145 fic = malloc(sizeof(*fic), M_DEVBUF, M_WAITOK|M_ZERO);
148 SLIST_INSERT_HEAD(&fdt_ic_list_head, fic, fdt_ics);
154 platform_prewrite(device_t dev, size_t offset, int val)
156 struct virtio_mmio_platform_softc *sc;
158 sc = device_get_softc(dev);
161 case (VIRTIO_MMIO_QUEUE_NOTIFY):
162 mips_dcache_wbinv_all();
172 platform_note(device_t dev, size_t offset, int val)
174 struct virtio_mmio_platform_softc *sc;
178 sc = device_get_softc(dev);
181 case (VIRTIO_MMIO_QUEUE_NOTIFY):
189 case (VIRTIO_MMIO_QUEUE_PFN):
192 case (VIRTIO_MMIO_QUEUE_SEL):
200 mips_dcache_wbinv_all();
205 PIO_SET(sc->pio_send, note, 1);
208 * Wait until host ack the request.
209 * Usually done within few cycles.
213 for (i = 100; i > 0; i--) {
214 if (PIO_READ(sc->pio_send) == 0)
219 device_printf(sc->dev, "Warning: host busy\n");
226 platform_intr(void *arg)
228 struct virtio_mmio_platform_softc *sc;
235 reg = PIO_READ(sc->pio_recv);
238 PIO_SET(sc->pio_recv, reg, 0);
241 /* Writeback, invalidate cache */
242 mips_dcache_wbinv_all();
244 if (sc->intr_handler != NULL)
245 sc->intr_handler(sc->ih_user);
249 platform_setup_intr(device_t dev, device_t mmio_dev,
250 void *intr_handler, void *ih_user)
252 struct virtio_mmio_platform_softc *sc;
255 sc = device_get_softc(dev);
257 sc->intr_handler = intr_handler;
258 sc->ih_user = ih_user;
261 PIO_SETUP_IRQ(sc->pio_recv, platform_intr, sc);
266 sc->res[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
269 device_printf(dev, "Can't allocate interrupt\n");
273 if (bus_setup_intr(dev, sc->res[0], INTR_TYPE_MISC | INTR_MPSAFE,
274 NULL, platform_intr, sc, &sc->ih)) {
275 device_printf(dev, "Can't setup the interrupt\n");
283 platform_poll(device_t dev)
286 mips_dcache_wbinv_all();
291 static device_method_t virtio_mmio_platform_methods[] = {
292 DEVMETHOD(device_probe, virtio_mmio_platform_probe),
293 DEVMETHOD(device_attach, virtio_mmio_platform_attach),
295 /* virtio_mmio_if.h */
296 DEVMETHOD(virtio_mmio_prewrite, platform_prewrite),
297 DEVMETHOD(virtio_mmio_note, platform_note),
298 DEVMETHOD(virtio_mmio_poll, platform_poll),
299 DEVMETHOD(virtio_mmio_setup_intr, platform_setup_intr),
303 static driver_t virtio_mmio_platform_driver = {
304 "virtio_mmio_platform",
305 virtio_mmio_platform_methods,
306 sizeof(struct virtio_mmio_platform_softc),
309 static devclass_t virtio_mmio_platform_devclass;
311 DRIVER_MODULE(virtio_mmio_platform, simplebus, virtio_mmio_platform_driver,
312 virtio_mmio_platform_devclass, 0, 0);