The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/beri/virtio/virtio.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
    3  * All rights reserved.
    4  *
    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.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   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.
   17  *
   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
   28  * SUCH DAMAGE.
   29  */
   30 
   31 /*
   32  * BERI virtio mmio backend common methods
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD$");
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/bus.h>
   41 #include <sys/cdefs.h>
   42 #include <sys/kernel.h>
   43 #include <sys/module.h>
   44 #include <sys/malloc.h>
   45 #include <sys/rman.h>
   46 #include <sys/timeet.h>
   47 #include <sys/timetc.h>
   48 #include <sys/conf.h>
   49 #include <sys/uio.h>
   50 #include <sys/stat.h>
   51 #include <sys/time.h>
   52 #include <sys/event.h>
   53 #include <sys/selinfo.h>
   54 #include <sys/endian.h>
   55 #include <sys/rwlock.h>
   56 
   57 #include <machine/bus.h>
   58 #include <machine/fdt.h>
   59 #include <machine/cpu.h>
   60 #include <machine/intr.h>
   61 
   62 #include <dev/fdt/fdt_common.h>
   63 #include <dev/ofw/openfirm.h>
   64 #include <dev/ofw/ofw_bus.h>
   65 #include <dev/ofw/ofw_bus_subr.h>
   66 
   67 #include <dev/beri/virtio/virtio.h>
   68 #include <dev/virtio/virtqueue.h>
   69 #include <dev/virtio/virtio_ring.h>
   70 #include <dev/altera/pio/pio.h>
   71 
   72 #include "pio_if.h"
   73 
   74 int
   75 vq_ring_ready(struct vqueue_info *vq)
   76 {
   77 
   78         return (vq->vq_flags & VQ_ALLOC);
   79 }
   80 
   81 int
   82 vq_has_descs(struct vqueue_info *vq)
   83 {
   84 
   85         return (vq_ring_ready(vq) && vq->vq_last_avail !=
   86                 be16toh(vq->vq_avail->idx));
   87 }
   88 
   89 void *
   90 paddr_map(uint32_t offset, uint32_t phys, uint32_t size)
   91 {
   92         bus_space_handle_t bsh;
   93 
   94         if (bus_space_map(fdtbus_bs_tag, (phys + offset),
   95                         size, 0, &bsh) != 0) {
   96                 panic("Couldn't map 0x%08x\n", (phys + offset));
   97         }
   98 
   99         return (void *)(bsh);
  100 }
  101 
  102 void
  103 paddr_unmap(void *phys, uint32_t size)
  104 {
  105 
  106         bus_space_unmap(fdtbus_bs_tag, (bus_space_handle_t)phys, size);
  107 }
  108 
  109 static inline void
  110 _vq_record(uint32_t offs, int i, volatile struct vring_desc *vd,
  111         struct iovec *iov, int n_iov, uint16_t *flags) {
  112         if (i >= n_iov)
  113                 return;
  114 
  115         iov[i].iov_base = paddr_map(offs, be64toh(vd->addr),
  116                                 be32toh(vd->len));
  117         iov[i].iov_len = be32toh(vd->len);
  118         if (flags != NULL)
  119                 flags[i] = be16toh(vd->flags);
  120 }
  121 
  122 int
  123 vq_getchain(uint32_t offs, struct vqueue_info *vq,
  124         struct iovec *iov, int n_iov, uint16_t *flags)
  125 {
  126         volatile struct vring_desc *vdir, *vindir, *vp;
  127         int idx, ndesc, n_indir;
  128         int head, next;
  129         int i;
  130 
  131         idx = vq->vq_last_avail;
  132         ndesc = (be16toh(vq->vq_avail->idx) - idx);
  133         if (ndesc == 0)
  134                 return (0);
  135 
  136         head = be16toh(vq->vq_avail->ring[idx & (vq->vq_qsize - 1)]);
  137         next = head;
  138 
  139         for (i = 0; i < VQ_MAX_DESCRIPTORS; next = be16toh(vdir->next)) {
  140                 vdir = &vq->vq_desc[next];
  141                 if ((be16toh(vdir->flags) & VRING_DESC_F_INDIRECT) == 0) {
  142                         _vq_record(offs, i, vdir, iov, n_iov, flags);
  143                         i++;
  144                 } else {
  145                         n_indir = be32toh(vdir->len) / 16;
  146                         vindir = paddr_map(offs, be64toh(vdir->addr),
  147                                         be32toh(vdir->len));
  148                         next = 0;
  149                         for (;;) {
  150                                 vp = &vindir[next];
  151                                 _vq_record(offs, i, vp, iov, n_iov, flags);
  152                                 i+=1;
  153                                 if ((be16toh(vp->flags) & \
  154                                         VRING_DESC_F_NEXT) == 0)
  155                                         break;
  156                                 next = be16toh(vp->next);
  157                         }
  158                         paddr_unmap(__DEVOLATILE(void *, vindir), be32toh(vdir->len));
  159                 }
  160 
  161                 if ((be16toh(vdir->flags) & VRING_DESC_F_NEXT) == 0)
  162                         return (i);
  163         }
  164 
  165         return (i);
  166 }
  167 
  168 void
  169 vq_relchain(struct vqueue_info *vq, struct iovec *iov, int n, uint32_t iolen)
  170 {
  171         volatile struct vring_used_elem *vue;
  172         volatile struct vring_used *vu;
  173         uint16_t head, uidx, mask;
  174         int i;
  175 
  176         mask = vq->vq_qsize - 1;
  177         vu = vq->vq_used;
  178         head = be16toh(vq->vq_avail->ring[vq->vq_last_avail++ & mask]);
  179 
  180         uidx = be16toh(vu->idx);
  181         vue = &vu->ring[uidx++ & mask];
  182         vue->id = htobe32(head);
  183 
  184         vue->len = htobe32(iolen);
  185         vu->idx = htobe16(uidx);
  186 
  187         /* Clean up */
  188         for (i = 0; i < n; i++) {
  189                 paddr_unmap((void *)iov[i].iov_base, iov[i].iov_len);
  190         }
  191 }
  192 
  193 int
  194 setup_pio(device_t dev, char *name, device_t *pio_dev)
  195 {
  196         phandle_t pio_node;
  197         struct fdt_ic *ic;
  198         phandle_t xref;
  199         phandle_t node;
  200 
  201         if ((node = ofw_bus_get_node(dev)) == -1)
  202                 return (ENXIO);
  203 
  204         if (OF_searchencprop(node, name, &xref,
  205                 sizeof(xref)) == -1) {
  206                 return (ENXIO);
  207         }
  208 
  209         pio_node = OF_node_from_xref(xref);
  210         SLIST_FOREACH(ic, &fdt_ic_list_head, fdt_ics) {
  211                 if (ic->iph == pio_node) {
  212                         *pio_dev = ic->dev;
  213                         return (0);
  214                 }
  215         }
  216 
  217         return (ENXIO);
  218 }
  219 
  220 int
  221 setup_offset(device_t dev, uint32_t *offset)
  222 {
  223         pcell_t dts_value[2];
  224         phandle_t mem_node;
  225         phandle_t xref;
  226         phandle_t node;
  227         int len;
  228 
  229         if ((node = ofw_bus_get_node(dev)) == -1)
  230                 return (ENXIO);
  231 
  232         if (OF_searchencprop(node, "beri-mem", &xref,
  233                 sizeof(xref)) == -1) {
  234                 return (ENXIO);
  235         }
  236 
  237         mem_node = OF_node_from_xref(xref);
  238         if ((len = OF_getproplen(mem_node, "reg")) <= 0)
  239                 return (ENXIO);
  240         OF_getencprop(mem_node, "reg", dts_value, len);
  241         *offset = dts_value[0];
  242 
  243         return (0);
  244 }
  245 
  246 struct iovec *
  247 getcopy(struct iovec *iov, int n)
  248 {
  249         struct iovec *tiov;
  250         int i;
  251 
  252         tiov = malloc(n * sizeof(struct iovec), M_DEVBUF, M_NOWAIT);
  253         for (i = 0; i < n; i++) {
  254                 tiov[i].iov_base = iov[i].iov_base;
  255                 tiov[i].iov_len = iov[i].iov_len;
  256         }
  257 
  258         return (tiov);
  259 }

Cache object: edac41370a79ccec867cb54903e811a0


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.