Index: head/sys/dev/altera/pio/pio.c =================================================================== --- head/sys/dev/altera/pio/pio.c (revision 276669) +++ head/sys/dev/altera/pio/pio.c (revision 276670) @@ -1,215 +1,213 @@ /*- * Copyright (c) 2014 Ruslan Bukin * All rights reserved. * * This software was developed by SRI International and the University of * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) * ("CTSRD"), as part of the DARPA CRASH research programme. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Altera PIO (Parallel IO) device driver */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "pio_if.h" #define READ4(_sc, _reg) bus_read_4((_sc)->res[0], _reg) #define READ2(_sc, _reg) bus_read_2((_sc)->res[0], _reg) #define READ1(_sc, _reg) bus_read_1((_sc)->res[0], _reg) #define WRITE4(_sc, _reg, _val) bus_write_4((_sc)->res[0], _reg, _val) #define WRITE2(_sc, _reg, _val) bus_write_2((_sc)->res[0], _reg, _val) #define WRITE1(_sc, _reg, _val) bus_write_1((_sc)->res[0], _reg, _val) struct pio_softc { struct resource *res[2]; bus_space_tag_t bst; bus_space_handle_t bsh; device_t dev; void *ih; }; static struct resource_spec pio_spec[] = { { SYS_RES_MEMORY, 0, RF_ACTIVE }, { SYS_RES_IRQ, 0, RF_ACTIVE }, { -1, 0 } }; static int pio_setup_irq(device_t dev, void *intr_handler, void *ih_user) { struct pio_softc *sc; sc = device_get_softc(dev); /* Setup interrupt handlers */ if (bus_setup_intr(sc->dev, sc->res[1], INTR_TYPE_BIO | INTR_MPSAFE, NULL, intr_handler, ih_user, &sc->ih)) { device_printf(sc->dev, "Unable to setup intr\n"); return (1); } return (0); } static int pio_teardown_irq(device_t dev) { struct pio_softc *sc; sc = device_get_softc(dev); bus_teardown_intr(sc->dev, sc->res[1], sc->ih); return (0); } static int pio_read(device_t dev) { struct pio_softc *sc; sc = device_get_softc(dev); return (READ4(sc, PIO_DATA)); } static int pio_set(device_t dev, int bit, int enable) { struct pio_softc *sc; sc = device_get_softc(dev); if (enable) WRITE4(sc, PIO_OUTSET, bit); else WRITE4(sc, PIO_OUTCLR, bit); return (0); } static int pio_configure(device_t dev, int dir, int mask) { struct pio_softc *sc; sc = device_get_softc(dev); WRITE4(sc, PIO_INT_MASK, mask); WRITE4(sc, PIO_DIR, dir); return (0); } static int pio_probe(device_t dev) { if (!ofw_bus_status_okay(dev)) return (ENXIO); if (!ofw_bus_is_compatible(dev, "altr,pio")) return (ENXIO); device_set_desc(dev, "Altera PIO"); return (BUS_PROBE_DEFAULT); } static int pio_attach(device_t dev) { struct pio_softc *sc; struct fdt_ic *fic; phandle_t node; sc = device_get_softc(dev); sc->dev = dev; if (bus_alloc_resources(dev, pio_spec, sc->res)) { device_printf(dev, "could not allocate resources\n"); return (ENXIO); } /* Memory interface */ sc->bst = rman_get_bustag(sc->res[0]); sc->bsh = rman_get_bushandle(sc->res[0]); - WRITE4(sc, PIO_DATA, 0); - if ((node = ofw_bus_get_node(sc->dev)) == -1) return (ENXIO); fic = malloc(sizeof(*fic), M_DEVBUF, M_WAITOK|M_ZERO); fic->iph = node; fic->dev = dev; SLIST_INSERT_HEAD(&fdt_ic_list_head, fic, fdt_ics); return (0); } static device_method_t pio_methods[] = { DEVMETHOD(device_probe, pio_probe), DEVMETHOD(device_attach, pio_attach), /* pio_if.m */ DEVMETHOD(pio_read, pio_read), DEVMETHOD(pio_configure, pio_configure), DEVMETHOD(pio_set, pio_set), DEVMETHOD(pio_setup_irq, pio_setup_irq), DEVMETHOD(pio_teardown_irq, pio_teardown_irq), DEVMETHOD_END }; static driver_t pio_driver = { "altera_pio", pio_methods, sizeof(struct pio_softc), }; static devclass_t pio_devclass; DRIVER_MODULE(altera_pio, simplebus, pio_driver, pio_devclass, 0, 0); Index: head/sys/dev/beri/virtio/virtio.c =================================================================== --- head/sys/dev/beri/virtio/virtio.c (revision 276669) +++ head/sys/dev/beri/virtio/virtio.c (revision 276670) @@ -1,248 +1,246 @@ /*- * Copyright (c) 2014 Ruslan Bukin * All rights reserved. * * This software was developed by SRI International and the University of * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) * ("CTSRD"), as part of the DARPA CRASH research programme. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * BERI virtio mmio backend common methods */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "pio_if.h" int vq_ring_ready(struct vqueue_info *vq) { return (vq->vq_flags & VQ_ALLOC); } int vq_has_descs(struct vqueue_info *vq) { return (vq_ring_ready(vq) && vq->vq_last_avail != be16toh(vq->vq_avail->idx)); } void * paddr_map(uint32_t offset, uint32_t phys, uint32_t size) { bus_space_handle_t bsh; if (bus_space_map(fdtbus_bs_tag, (phys + offset), size, 0, &bsh) != 0) { panic("Couldn't map 0x%08x\n", (phys + offset)); } return (void *)(bsh); } void paddr_unmap(void *phys, uint32_t size) { bus_space_unmap(fdtbus_bs_tag, (bus_space_handle_t)phys, size); } static inline void _vq_record(uint32_t offs, int i, volatile struct vring_desc *vd, struct iovec *iov, int n_iov, uint16_t *flags) { if (i >= n_iov) return; iov[i].iov_base = paddr_map(offs, be64toh(vd->addr), be32toh(vd->len)); iov[i].iov_len = be32toh(vd->len); if (flags != NULL) flags[i] = be16toh(vd->flags); } int vq_getchain(uint32_t offs, struct vqueue_info *vq, struct iovec *iov, int n_iov, uint16_t *flags) { volatile struct vring_desc *vdir, *vindir, *vp; int idx, ndesc, n_indir; int head, next; int i; idx = vq->vq_last_avail; ndesc = (be16toh(vq->vq_avail->idx) - idx); if (ndesc == 0) return (0); head = be16toh(vq->vq_avail->ring[idx & (vq->vq_qsize - 1)]); next = head; for (i = 0; i < VQ_MAX_DESCRIPTORS; next = be16toh(vdir->next)) { vdir = &vq->vq_desc[next]; if ((be16toh(vdir->flags) & VRING_DESC_F_INDIRECT) == 0) { _vq_record(offs, i, vdir, iov, n_iov, flags); i++; } else { n_indir = be32toh(vdir->len) / 16; vindir = paddr_map(offs, be64toh(vdir->addr), be32toh(vdir->len)); next = 0; for (;;) { vp = &vindir[next]; _vq_record(offs, i, vp, iov, n_iov, flags); i+=1; if ((be16toh(vp->flags) & \ VRING_DESC_F_NEXT) == 0) break; next = be16toh(vp->next); } paddr_unmap(__DEVOLATILE(void *, vindir), be32toh(vdir->len)); } if ((be16toh(vdir->flags) & VRING_DESC_F_NEXT) == 0) return (i); } return (i); } void vq_relchain(struct vqueue_info *vq, struct iovec *iov, int n, uint32_t iolen) { volatile struct vring_used_elem *vue; volatile struct vring_used *vu; uint16_t head, uidx, mask; int i; mask = vq->vq_qsize - 1; vu = vq->vq_used; head = be16toh(vq->vq_avail->ring[vq->vq_last_avail++ & mask]); uidx = be16toh(vu->idx); vue = &vu->ring[uidx++ & mask]; vue->id = htobe32(head); vue->len = htobe32(iolen); vu->idx = htobe16(uidx); /* Clean up */ for (i = 1; i < (n-1); i++) { paddr_unmap((void *)iov[i].iov_base, iov[i].iov_len); } } int setup_pio(device_t dev, char *name, device_t *pio_dev) { phandle_t pio_node; struct fdt_ic *ic; phandle_t xref; phandle_t node; if ((node = ofw_bus_get_node(dev)) == -1) return (ENXIO); if (OF_searchencprop(node, name, &xref, sizeof(xref)) == -1) { return (ENXIO); } pio_node = OF_node_from_xref(xref); SLIST_FOREACH(ic, &fdt_ic_list_head, fdt_ics) { if (ic->iph == pio_node) { *pio_dev = ic->dev; - PIO_CONFIGURE(*pio_dev, PIO_OUT_ALL, - PIO_UNMASK_ALL); return (0); } } return (ENXIO); } int setup_offset(device_t dev, uint32_t *offset) { pcell_t dts_value[2]; phandle_t mem_node; phandle_t xref; phandle_t node; int len; if ((node = ofw_bus_get_node(dev)) == -1) return (ENXIO); if (OF_searchencprop(node, "beri-mem", &xref, sizeof(xref)) == -1) { return (ENXIO); } mem_node = OF_node_from_xref(xref); if ((len = OF_getproplen(mem_node, "reg")) <= 0) return (ENXIO); OF_getencprop(mem_node, "reg", dts_value, len); *offset = dts_value[0]; return (0); }