[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Minios-devel] [UNIKRAFT PATCH 2/7] plat/common: Introduce virtio standard header



Reviewed-by: Simon Kuenzer <simon.kuenzer@xxxxxxxxx>

On 19.10.18 15:06, Sharan Santhanam wrote:
We introduce the standard virtio header files.
* virtio_ids: List the ids of the virtio devices
* virtio_ring.h: Describes the virtio ring descriptor, used and
                                 available descriptors.
* virtio_pci.h: Describes the virtio PCI device register map.
* virtio_types.h:  Describes the type of the data structure.
* virtio_config.h: Provides helper function to perform IO
                                operation on the device.

Signed-off-by: Sharan Santhanam <sharan.santhanam@xxxxxxxxx>
---
  plat/common/include/pci/virtio/virtio_pci.h |  66 -------
  plat/drivers/include/virtio/virtio_config.h | 259 ++++++++++++++++++++++++++++
  plat/drivers/include/virtio/virtio_ids.h    |  64 +++++++
  plat/drivers/include/virtio/virtio_pci.h    |  70 ++++++++
  plat/drivers/include/virtio/virtio_ring.h   | 196 +++++++++++++++++++++
  plat/drivers/include/virtio/virtio_types.h  |  62 +++++++
  6 files changed, 651 insertions(+), 66 deletions(-)
  delete mode 100644 plat/common/include/pci/virtio/virtio_pci.h
  create mode 100644 plat/drivers/include/virtio/virtio_config.h
  create mode 100644 plat/drivers/include/virtio/virtio_ids.h
  create mode 100644 plat/drivers/include/virtio/virtio_pci.h
  create mode 100644 plat/drivers/include/virtio/virtio_ring.h
  create mode 100644 plat/drivers/include/virtio/virtio_types.h

diff --git a/plat/common/include/pci/virtio/virtio_pci.h 
b/plat/common/include/pci/virtio/virtio_pci.h
deleted file mode 100644
index 6f9a6e1..0000000
--- a/plat/common/include/pci/virtio/virtio_pci.h
+++ /dev/null
@@ -1,66 +0,0 @@
-/* SPDX-License-Identifier: ISC */
-/*
- * Authors: Dan Williams
- *          Costin Lupu <costin.lupu@xxxxxxxxx>
- *
- * Copyright (c) 2015, IBM
- * Copyright (c) 2018, NEC Europe Ltd., NEC Corporation
- *
- * Permission to use, copy, modify, and/or distribute this software
- * for any purpose with or without fee is hereby granted, provided
- * that the above copyright notice and this permission notice appear
- * in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
- * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
- * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
- * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
- * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
- * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-/* Taken and adapted from solo5 virtio_pci.h */
-
-#ifndef __PLAT_CMN_PCI_VIRTIO_PCI_H__
-#define __PLAT_CMN_PCI_VIRTIO_PCI_H__
-
-/* virtio config space layout */
-/* TODO we currently support only the legacy interface */
-#define VIRTIO_PCI_HOST_FEATURES        0    /* 32-bit r/o */
-#define VIRTIO_PCI_GUEST_FEATURES       4    /* 32-bit r/w */
-#define VIRTIO_PCI_QUEUE_PFN            8    /* 32-bit r/w */
-#define VIRTIO_PCI_QUEUE_SIZE           12   /* 16-bit r/o */
-#define VIRTIO_PCI_QUEUE_SEL            14   /* 16-bit r/w */
-#define VIRTIO_PCI_QUEUE_NOTIFY         16   /* 16-bit r/w */
-
-/*
- * Shift size used for writing physical queue address to QUEUE_PFN
- */
-#define VIRTIO_PCI_QUEUE_ADDR_SHIFT     12
-
-
-/*
- * The status register lets us tell the device where we are in
- * initialization
- */
-#define VIRTIO_PCI_STATUS               18   /* 8-bit r/w */
-#define VIRTIO_PCI_STATUS_ACK           0x1  /* we recognize device as virtio 
*/
-#define VIRTIO_PCI_STATUS_DRIVER        0x2  /* we want to drive it */
-#define VIRTIO_PCI_STATUS_DRIVER_OK     0x4  /* initialization is complete */
-#define VIRTIO_PCI_STATUS_DEVICE_RESET  0x40 /* device needs reset */
-#define VIRTIO_PCI_STATUS_FAIL          0x80 /* tell device something's wrong 
*/
-
-/*
- * Reading the value will return the current contents of the interrupt
- * status register and will also clear it.  This is effectively a
- * read-and-acknowledge.
- */
-#define VIRTIO_PCI_ISR                  19   /* 8-bit r/o */
-#define VIRTIO_PCI_ISR_HAS_INTR         0x1  /* interrupt is for this device */
-#define VIRTIO_PCI_ISR_CONFIG           0x2  /* config change bit */
-
-/* TODO Revisit when adding MSI support. */
-#define VIRTIO_PCI_CONFIG_OFF           20
-
-#endif /* __PLAT_CMN_PCI_VIRTIO_PCI_H__ */
diff --git a/plat/drivers/include/virtio/virtio_config.h 
b/plat/drivers/include/virtio/virtio_config.h
new file mode 100644
index 0000000..1c4e7ef
--- /dev/null
+++ b/plat/drivers/include/virtio/virtio_config.h
@@ -0,0 +1,259 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Authors: Sharan Santhanam <sharan.santhanam@xxxxxxxxx>
+ *
+ * Copyright (c) 2018, NEC Europe Ltd., NEC Corporation. All rights reserved.
+ *
+ * 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.
+ * 3. Neither the name of the copyright holder nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
+ *
+ * THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
+ */
+
+#ifndef __PLAT_DRV_VIRTIO_CONFIG_H
+#define __PLAT_DRV_VIRTIO_CONFIG_H
+
+#include <uk/arch/types.h>
+#include <cpu.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus __ */
+
+#define MAX_TRY_COUNT    (10)
+
+#define VIRTIO_CONFIG_STATUS_RESET         0x0  /* Reset the device */
+#define VIRTIO_CONFIG_STATUS_ACK           0x1  /* recognize device as virtio 
*/
+#define VIRTIO_CONFIG_STATUS_DRIVER        0x2  /* driver for the device 
found*/
+#define VIRTIO_CONFIG_STATUS_DRIVER_OK     0x4  /* initialization is complete 
*/
+#define VIRTIO_CONFIG_STATUS_NEEDS_RESET   0x40 /* device needs reset */
+#define VIRTIO_CONFIG_STATUS_FAIL          0x80 /* device something's wrong*/
+
+#define VIRTIO_TRANSPORT_F_START    28
+#define VIRTIO_TRANSPORT_F_END      32
+
+#ifdef __X86_64__
+static inline void _virtio_cwrite_bytes(const void *addr, const __u8 offset,
+                                       const void *buf, int len, int type_len)
+{
+       int i = 0;
+       __u16 io_addr;
+       int count;
+
+       count  = len / type_len;
+       for (i = 0; i < count; i++) {
+               io_addr = ((unsigned long)addr) + offset + (i * type_len);
+               switch (type_len) {
+               case 1:
+                       outb(io_addr, ((__u8 *)buf)[i * type_len]);
+                       break;
+               case 2:
+                       outw(io_addr, ((__u16 *)buf)[i * type_len]);
+                       break;
+               case 4:
+                       outl(io_addr, ((__u32 *)buf)[i * type_len]);
+                       break;
+               default:
+                       UK_CRASH("Unsupported virtio write operation\n");
+               }
+       }
+}
+
+static inline void _virtio_cread_bytes(const void *addr, const __u8 offset,
+                                      void *buf, int len, int type_len)
+{
+       int i = 0;
+       __u16 io_addr;
+       int count;
+
+       count = len / type_len;
+       for (i = 0; i < count; i++) {
+               io_addr = ((unsigned long)addr) + offset + (i * type_len);
+               switch (type_len) {
+               case 1:
+                       ((__u8 *)buf)[i * type_len] = inb(io_addr);
+                       break;
+               case 2:
+                       ((__u16 *)buf)[i * type_len] = inw(io_addr);
+                       break;
+               case 4:
+                       ((__u32 *)buf)[i * type_len] = inl(io_addr);
+                       break;
+               case 8:
+                       ((__u64  *)buf)[i * type_len] = inq(io_addr);
+                       break;
+               default:
+                       UK_CRASH("Unsupported virtio read operation\n");
+               }
+       }
+}
+#else  /* __X86_64__ */
+#error "Virtio driver not supported for this architecture"
+#endif /* __X86_64__ */
+
+/**
+ * Read the virtio device configuration of specified length.
+ *
+ * @param addr
+ *   The base address of the device.
+ * @param offset
+ *   The offset with the device address space.
+ * @param buf
+ *    The destination buffer to which the value has to be copied.
+ * @param len
+ *    The length of the destination buffer.
+ */
+static inline int virtio_cread_bytes_many(const void *addr, const __u8 offset,
+                                         __u8 *buf, __u32 len)
+{
+       __u8 old_buf[len];
+       int check = -1;
+       int cnt = 0;
+       __u32 i = 0;
+
+       do {
+               check = len;
+               _virtio_cread_bytes(addr, offset, &old_buf[0], len, 1);
+               _virtio_cread_bytes(addr, offset, buf, len, 1);
+
+               for (i = 0; i < len; i++) {
+                       if (unlikely(buf[i] != old_buf[i])) {
+                               check = -1; /* Need to retry configuration */
+                               break;
+                       }
+               }
+               cnt++;
+       } while (check == -1 && cnt < MAX_TRY_COUNT);
+
+       return check;
+}
+
+/**
+ * Read the single byte configuration.
+ *
+ * @param addr
+ *   The base address of the device.
+ * @param offset
+ *   The offset with the device address space.
+ * @return __u8
+ *   Returns the value configuration register.
+ */
+static inline __u8 virtio_cread8(const void *addr, const __u8 offset)
+{
+       __u8 buf = 0;
+
+       _virtio_cread_bytes(addr, offset, &buf, sizeof(buf), sizeof(buf));
+       return buf;
+}
+
+/**
+ * Read the single word configuration.
+ *
+ * @param addr
+ *   The base address of the device.
+ * @param offset
+ *   The offset with the device address space.
+ * @return __u16
+ *   Returns the value configuration register.
+ */
+static inline __u16 virtio_cread16(const void *addr, const __u8 offset)
+{
+       __u16 buf = 0;
+
+       _virtio_cread_bytes(addr, offset, &buf, sizeof(buf), sizeof(buf));
+       return buf;
+}
+
+/**
+ * Read the single long word configuration.
+ *
+ * @param addr
+ *   The base address of the device.
+ * @param offset
+ *   The offset with the device address space.
+ * @return __u32
+ *   Returns the value configuration register.
+ */
+static inline __u32 virtio_cread32(const void *addr, const __u8 offset)
+{
+       __u32 buf = 0;
+
+       _virtio_cread_bytes(addr, offset, &buf, sizeof(buf), sizeof(buf));
+       return buf;
+}
+
+/**
+ * Write the configuration.
+ *
+ * @param addr
+ *   The base address of the device.
+ * @param offset
+ *   The offset with the device address space.
+ * @param data
+ *   The value to write to the configuration.
+ */
+static inline void virtio_cwrite8(const void *addr, const __u8 offset,
+                                 const __u8 data)
+{
+       _virtio_cwrite_bytes(addr, offset, &data, sizeof(data), sizeof(data));
+}
+
+/**
+ * Write the configuration.
+ *
+ * @param addr
+ *   The base address of the device.
+ * @param offset
+ *   The offset with the device address space.
+ * @param data
+ *   The value to write to the configuration.
+ */
+static inline void virtio_cwrite16(const void *addr, const __u8 offset,
+                                  const __u16 data)
+{
+       _virtio_cwrite_bytes(addr, offset, &data, sizeof(data), sizeof(data));
+}
+
+/**
+ * Write the configuration.
+ *
+ * @param addr
+ *   The base address of the device.
+ * @param offset
+ *   The offset with the device address space.
+ * @param data
+ *   The value to write to the configuration.
+ */
+static inline void virtio_cwrite32(const void *addr, const __u8 offset,
+                                  const __u32 data)
+{
+       _virtio_cwrite_bytes(addr, offset, &data, sizeof(data), sizeof(data));
+}
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus __ */
+
+#endif /* __PLAT_DRV_VIRTIO_CONFIG_H */
diff --git a/plat/drivers/include/virtio/virtio_ids.h 
b/plat/drivers/include/virtio/virtio_ids.h
new file mode 100644
index 0000000..c70de23
--- /dev/null
+++ b/plat/drivers/include/virtio/virtio_ids.h
@@ -0,0 +1,64 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/**
+ * Virtio IDs
+ *
+ * This header is BSD licensed so anyone can use the definitions to implement
+ * compatible drivers/servers.
+ *
+ * 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.
+ * 3. Neither the name of IBM nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 IBM 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.
+ */
+/**
+ * Taken and modified from Linux Kernel.
+ * include/uapi/linux/virtio_ids.h
+ *
+ * Commit-id:dbaf0624ffa5
+ */
+#ifndef __PLAT_DRV_VIRTIO_IDS_H
+#define __PLAT_DRV_VIRTIO_IDS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus __ */
+
+#define VIRTIO_ID_INVALID       0 /* Invalid device (Reserved) */
+#define VIRTIO_ID_NET          1 /* virtio net */
+#define VIRTIO_ID_BLOCK                2 /* virtio block */
+#define VIRTIO_ID_CONSOLE      3 /* virtio console */
+#define VIRTIO_ID_RNG          4 /* virtio rng */
+#define VIRTIO_ID_BALLOON      5 /* virtio balloon */
+#define VIRTIO_ID_RPMSG                7 /* virtio remote processor messaging 
*/
+#define VIRTIO_ID_SCSI         8 /* virtio scsi */
+#define VIRTIO_ID_9P           9 /* 9p virtio console */
+#define VIRTIO_ID_RPROC_SERIAL 11 /* virtio remoteproc serial link */
+#define VIRTIO_ID_CAIF        12 /* Virtio caif */
+#define VIRTIO_ID_GPU          16 /* virtio GPU */
+#define VIRTIO_ID_INPUT        18 /* virtio input */
+#define VIRTIO_ID_VSOCK        19 /* virtio vsock transport */
+#define VIRTIO_ID_CRYPTO       20 /* virtio crypto */
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus __ */
+
+#endif /* __PLAT_DRV_VIRTIO_IDS_H */
diff --git a/plat/drivers/include/virtio/virtio_pci.h 
b/plat/drivers/include/virtio/virtio_pci.h
new file mode 100644
index 0000000..e542781
--- /dev/null
+++ b/plat/drivers/include/virtio/virtio_pci.h
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: ISC */
+/*
+ * Authors: Dan Williams
+ *          Costin Lupu <costin.lupu@xxxxxxxxx>
+ *          Sharan Santhanam <sharan.santhanam@xxxxxxxxx>
+ *
+ * Copyright (c) 2015, IBM
+ * Copyright (c) 2018, NEC Europe Ltd., NEC Corporation
+ *
+ * Permission to use, copy, modify, and/or distribute this software
+ * for any purpose with or without fee is hereby granted, provided
+ * that the above copyright notice and this permission notice appear
+ * in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
+ * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+/* Taken and adapted from solo5 virtio_pci.h */
+
+#ifndef __PLAT_DRV_VIRTIO_PCI_H__
+#define __PLAT_DRV_VIRTIO_PCI_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus __ */
+
+/* virtio config space layout */
+/* TODO we currently support only the legacy interface */
+#define VIRTIO_PCI_HOST_FEATURES        0    /* 32-bit r/o */
+#define VIRTIO_PCI_GUEST_FEATURES       4    /* 32-bit r/w */
+#define VIRTIO_PCI_QUEUE_PFN            8    /* 32-bit r/w */
+#define VIRTIO_PCI_QUEUE_SIZE           12   /* 16-bit r/o */
+#define VIRTIO_PCI_QUEUE_SEL            14   /* 16-bit r/w */
+#define VIRTIO_PCI_QUEUE_NOTIFY         16   /* 16-bit r/w */
+
+/*
+ * Shift size used for writing physical queue address to QUEUE_PFN
+ */
+#define VIRTIO_PCI_QUEUE_ADDR_SHIFT     12
+
+/*
+ * The status register lets us tell the device where we are in
+ * initialization
+ */
+#define VIRTIO_PCI_STATUS               18   /* 8-bit r/w */
+
+/*
+ * Reading the value will return the current contents of the interrupt
+ * status register and will also clear it.  This is effectively a
+ * read-and-acknowledge.
+ */
+#define VIRTIO_PCI_ISR                  19   /* 8-bit r/o */
+#define VIRTIO_PCI_ISR_HAS_INTR         0x1  /* interrupt is for this device */
+#define VIRTIO_PCI_ISR_CONFIG           0x2  /* config change bit */
+
+/* TODO Revisit when adding MSI support. */
+#define VIRTIO_PCI_CONFIG_OFF           20
+#define VIRTIO_PCI_VRING_ALIGN          4096
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus __ */
+
+#endif /* __PLAT_DRV_VIRTIO_PCI_H__ */
diff --git a/plat/drivers/include/virtio/virtio_ring.h 
b/plat/drivers/include/virtio/virtio_ring.h
new file mode 100644
index 0000000..9f65594
--- /dev/null
+++ b/plat/drivers/include/virtio/virtio_ring.h
@@ -0,0 +1,196 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/* An interface for efficient virtio implementation, currently for use by KVM,
+ * but hopefully others soon.  Do NOT change this since it will
+ * break existing servers and clients.
+ *
+ * This header is BSD licensed so anyone can use the definitions to implement
+ * compatible drivers/servers.
+ *
+ * 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.
+ * 3. Neither the name of IBM nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 IBM 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.
+ *
+ * Copyright Rusty Russell IBM Corporation 2007. */
+/**
+ * Taken and modified from Linux.
+ * include/uapi/linux/virtio_ring.h
+ *
+ * Commit id: ecda85e70277
+ */
+
+#ifndef __PLAT_DRV_VIRTIO_RING_H__
+#define __PLAT_DRV_VIRTIO_RING_H__
+
+#include <uk/arch/limits.h>
+#include <virtio/virtio_types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus __ */
+
+#if __BYTE_ORDER__ !=  __ORDER_LITTLE_ENDIAN__
+#error "Please define conversion functions from host to little endian!"
+#endif
+
+/* This marks a buffer as continuing via the next field. */
+#define VRING_DESC_F_NEXT       1
+/* This marks a buffer as write-only (otherwise read-only). */
+#define VRING_DESC_F_WRITE      2
+/* This means the buffer contains a list of buffer descriptors. */
+#define VRING_DESC_F_INDIRECT   4
+
+/*
+ * The device uses this in used->flags to advise the driver:
+ * don't kick me when you add a buffer. It's unreliable, so
+ * it's simply an optimization.
+ */
+#define VRING_USED_F_NO_NOTIFY  1
+
+/*
+ * The driver uses this in avail->flags to advise the device:
+ * don't interrupt me when you consume a buffer. It's unreliable, so
+ * it's simply an optimization.
+ */
+#define VRING_AVAIL_F_NO_INTERRUPT      1
+
+/* Support for indirect descriptors */
+#define VIRTIO_F_INDIRECT_DESC    28
+
+/* Support for avail_event and used_event fields */
+#define VIRTIO_F_EVENT_IDX        29
+
+/* Arbitrary descriptor layouts. */
+#define VIRTIO_F_ANY_LAYOUT       27
+
+/**
+ * Virtqueue descriptors: 16 bytes.
+ * These can chain together via "next".
+ */
+struct vring_desc {
+       /* Address (guest-physical). */
+       __virtio_le64 addr;
+       /* Length. */
+       __virtio_le32 len;
+       /* The flags as indicated above. */
+       __virtio_le16 flags;
+       /* We chain unused descriptors via this, too */
+       __virtio_le16 next;
+};
+
+struct vring_avail {
+       __virtio_le16 flags;
+       __virtio_le16 idx;
+       __virtio_le16 ring[];
+       /* Only if VIRTIO_F_EVENT_IDX: __virtio_le16 used_event; */
+};
+
+/* __virtio_le32 is used here for ids for padding reasons. */
+struct vring_used_elem {
+       /* Index of start of used descriptor chain. */
+       __virtio_le32 id;
+       /* Total length of the descriptor chain which was written to. */
+       __virtio_le32 len;
+};
+
+struct vring_used {
+       __virtio_le16 flags;
+       __virtio_le16 idx;
+       struct vring_used_elem ring[];
+       /* Only if VIRTIO_F_EVENT_IDX: __virtio_le16 avail_event; */
+};
+
+struct vring {
+       unsigned int num;
+
+       struct vring_desc *desc;
+       struct vring_avail *avail;
+       struct vring_used *used;
+};
+
+/* The standard layout for the ring is a continuous chunk of memory which
+ * looks like this.  We assume num is a power of 2.
+ *
+ * struct vring {
+ *      // The actual descriptors (16 bytes each)
+ *      struct vring_desc desc[num];
+ *
+ *      // A ring of available descriptor heads with free-running index.
+ *      __virtio_le16 avail_flags;
+ *      __virtio_le16 avail_idx;
+ *      __virtio_le16 available[num];
+ *      __virtio_le16 used_event_idx;
+ *
+ *      // Padding to the next align boundary.
+ *      char pad[];
+ *
+ *      // A ring of used descriptor heads with free-running index.
+ *      __virtio_le16 used_flags;
+ *      __virtio_le16 used_idx;
+ *      struct vring_used_elem used[num];
+ *      __virtio_le16 avail_event_idx;
+ * };
+ *
+ * NOTE: for VirtIO PCI, align is 4096.
+ */
+
+/**
+ * We publish the used event index at the end of the available ring, and vice
+ * versa. They are at the end for backwards compatibility.
+ */
+#define vring_used_event(vr) ((vr)->avail->ring[(vr)->num])
+#define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num])
+
+static inline void vring_init(struct vring *vr, unsigned int num, uint8_t *p,
+                             unsigned long align)
+{
+       vr->num = num;
+       vr->desc = (struct vring_desc *) p;
+       vr->avail = (struct vring_avail *) (p +
+                       num * sizeof(struct vring_desc));
+       vr->used = (void *)
+       (((unsigned long) &vr->avail->ring[num] + align - 1) & ~(align - 1));
+}
+
+static inline unsigned int vring_size(unsigned int num, unsigned long align)
+{
+       int size;
+
+       size = num * sizeof(struct vring_desc);
+       size += sizeof(struct vring_avail) + (num * sizeof(uint16_t)) +
+               sizeof(uint16_t);
+       size = (size + align - 1) & ~(align - 1);
+       size += sizeof(struct vring_used) +
+           (num * sizeof(struct vring_used_elem)) + sizeof(uint16_t);
+       return size;
+}
+
+static inline int vring_need_event(__u16 event_idx, __u16 new_idx,
+                                  __u16 old_idx)
+{
+       return (new_idx - event_idx - 1) < (new_idx - old_idx);
+}
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus __ */
+
+#endif /* __PLAT_DRV_VIRTIO_RING_H__ */
diff --git a/plat/drivers/include/virtio/virtio_types.h 
b/plat/drivers/include/virtio/virtio_types.h
new file mode 100644
index 0000000..a804c2c
--- /dev/null
+++ b/plat/drivers/include/virtio/virtio_types.h
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/* Type definitions for virtio implementations.
+ *
+ * This header is BSD licensed so anyone can use the definitions to implement
+ * compatible drivers/servers.
+ *
+ * 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.
+ * 3. Neither the name of IBM nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 IBM 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.
+ *
+ * Copyright (C) 2014 Red Hat, Inc.
+ * Author: Michael S. Tsirkin <mst@xxxxxxxxxx>
+ *        Sharan Santhanam <sharan.santhanam@xxxxxxxxx>
+ */
+/**
+ * Taken and modified from Linux Kernel.
+ * include/uapi/linux/virtio_types.h
+ *
+ * Commit-id: 9efeccacd3a4
+ */
+#ifndef __PLAT_DRV_VIRTIO_TYPES_H
+#define __PLAT_DRV_VIRTIO_TYPES_H
+
+#include <uk/arch/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus  */
+
+/**
+ * __virtio{16,32,64} have the following meaning:
+ * - __u{16,32,64} for virtio devices in legacy mode, accessed in native endian
+ * - __le{16,32,64} for standard-compliant virtio devices
+ */
+typedef __u16 __virtio_le16;
+typedef __u32 __virtio_le32;
+typedef __u64 __virtio_le64;
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus  */
+
+#endif /* __PLAT_DRV_VIRTIO_TYPES_H */


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.