11a992bbaSAndre Przywara #ifndef _UAPI_LINUX_VIRTIO_RING_H
21a992bbaSAndre Przywara #define _UAPI_LINUX_VIRTIO_RING_H
31a992bbaSAndre Przywara /* An interface for efficient virtio implementation, currently for use by KVM,
41a992bbaSAndre Przywara * but hopefully others soon. Do NOT change this since it will
51a992bbaSAndre Przywara * break existing servers and clients.
61a992bbaSAndre Przywara *
71a992bbaSAndre Przywara * This header is BSD licensed so anyone can use the definitions to implement
81a992bbaSAndre Przywara * compatible drivers/servers.
91a992bbaSAndre Przywara *
101a992bbaSAndre Przywara * Redistribution and use in source and binary forms, with or without
111a992bbaSAndre Przywara * modification, are permitted provided that the following conditions
121a992bbaSAndre Przywara * are met:
131a992bbaSAndre Przywara * 1. Redistributions of source code must retain the above copyright
141a992bbaSAndre Przywara * notice, this list of conditions and the following disclaimer.
151a992bbaSAndre Przywara * 2. Redistributions in binary form must reproduce the above copyright
161a992bbaSAndre Przywara * notice, this list of conditions and the following disclaimer in the
171a992bbaSAndre Przywara * documentation and/or other materials provided with the distribution.
181a992bbaSAndre Przywara * 3. Neither the name of IBM nor the names of its contributors
191a992bbaSAndre Przywara * may be used to endorse or promote products derived from this software
201a992bbaSAndre Przywara * without specific prior written permission.
211a992bbaSAndre Przywara * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
221a992bbaSAndre Przywara * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231a992bbaSAndre Przywara * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241a992bbaSAndre Przywara * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
251a992bbaSAndre Przywara * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261a992bbaSAndre Przywara * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271a992bbaSAndre Przywara * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281a992bbaSAndre Przywara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291a992bbaSAndre Przywara * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301a992bbaSAndre Przywara * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311a992bbaSAndre Przywara * SUCH DAMAGE.
321a992bbaSAndre Przywara *
331a992bbaSAndre Przywara * Copyright Rusty Russell IBM Corporation 2007. */
341a992bbaSAndre Przywara #ifndef __KERNEL__
351a992bbaSAndre Przywara #include <stdint.h>
361a992bbaSAndre Przywara #endif
371a992bbaSAndre Przywara #include <linux/types.h>
381a992bbaSAndre Przywara #include <linux/virtio_types.h>
391a992bbaSAndre Przywara
401a992bbaSAndre Przywara /* This marks a buffer as continuing via the next field. */
411a992bbaSAndre Przywara #define VRING_DESC_F_NEXT 1
421a992bbaSAndre Przywara /* This marks a buffer as write-only (otherwise read-only). */
431a992bbaSAndre Przywara #define VRING_DESC_F_WRITE 2
441a992bbaSAndre Przywara /* This means the buffer contains a list of buffer descriptors. */
451a992bbaSAndre Przywara #define VRING_DESC_F_INDIRECT 4
461a992bbaSAndre Przywara
471a992bbaSAndre Przywara /*
481a992bbaSAndre Przywara * Mark a descriptor as available or used in packed ring.
491a992bbaSAndre Przywara * Notice: they are defined as shifts instead of shifted values.
501a992bbaSAndre Przywara */
511a992bbaSAndre Przywara #define VRING_PACKED_DESC_F_AVAIL 7
521a992bbaSAndre Przywara #define VRING_PACKED_DESC_F_USED 15
531a992bbaSAndre Przywara
541a992bbaSAndre Przywara /* The Host uses this in used->flags to advise the Guest: don't kick me when
551a992bbaSAndre Przywara * you add a buffer. It's unreliable, so it's simply an optimization. Guest
561a992bbaSAndre Przywara * will still kick if it's out of buffers. */
571a992bbaSAndre Przywara #define VRING_USED_F_NO_NOTIFY 1
581a992bbaSAndre Przywara /* The Guest uses this in avail->flags to advise the Host: don't interrupt me
591a992bbaSAndre Przywara * when you consume a buffer. It's unreliable, so it's simply an
601a992bbaSAndre Przywara * optimization. */
611a992bbaSAndre Przywara #define VRING_AVAIL_F_NO_INTERRUPT 1
621a992bbaSAndre Przywara
631a992bbaSAndre Przywara /* Enable events in packed ring. */
641a992bbaSAndre Przywara #define VRING_PACKED_EVENT_FLAG_ENABLE 0x0
651a992bbaSAndre Przywara /* Disable events in packed ring. */
661a992bbaSAndre Przywara #define VRING_PACKED_EVENT_FLAG_DISABLE 0x1
671a992bbaSAndre Przywara /*
681a992bbaSAndre Przywara * Enable events for a specific descriptor in packed ring.
691a992bbaSAndre Przywara * (as specified by Descriptor Ring Change Event Offset/Wrap Counter).
701a992bbaSAndre Przywara * Only valid if VIRTIO_RING_F_EVENT_IDX has been negotiated.
711a992bbaSAndre Przywara */
721a992bbaSAndre Przywara #define VRING_PACKED_EVENT_FLAG_DESC 0x2
731a992bbaSAndre Przywara
741a992bbaSAndre Przywara /*
751a992bbaSAndre Przywara * Wrap counter bit shift in event suppression structure
761a992bbaSAndre Przywara * of packed ring.
771a992bbaSAndre Przywara */
781a992bbaSAndre Przywara #define VRING_PACKED_EVENT_F_WRAP_CTR 15
791a992bbaSAndre Przywara
801a992bbaSAndre Przywara /* We support indirect buffer descriptors */
811a992bbaSAndre Przywara #define VIRTIO_RING_F_INDIRECT_DESC 28
821a992bbaSAndre Przywara
831a992bbaSAndre Przywara /* The Guest publishes the used index for which it expects an interrupt
841a992bbaSAndre Przywara * at the end of the avail ring. Host should ignore the avail->flags field. */
851a992bbaSAndre Przywara /* The Host publishes the avail index for which it expects a kick
861a992bbaSAndre Przywara * at the end of the used ring. Guest should ignore the used->flags field. */
871a992bbaSAndre Przywara #define VIRTIO_RING_F_EVENT_IDX 29
881a992bbaSAndre Przywara
891a992bbaSAndre Przywara /* Alignment requirements for vring elements.
901a992bbaSAndre Przywara * When using pre-virtio 1.0 layout, these fall out naturally.
911a992bbaSAndre Przywara */
921a992bbaSAndre Przywara #define VRING_AVAIL_ALIGN_SIZE 2
931a992bbaSAndre Przywara #define VRING_USED_ALIGN_SIZE 4
941a992bbaSAndre Przywara #define VRING_DESC_ALIGN_SIZE 16
951a992bbaSAndre Przywara
96*76dfc0cfSAnup Patel /**
97*76dfc0cfSAnup Patel * struct vring_desc - Virtio ring descriptors,
98*76dfc0cfSAnup Patel * 16 bytes long. These can chain together via @next.
99*76dfc0cfSAnup Patel *
100*76dfc0cfSAnup Patel * @addr: buffer address (guest-physical)
101*76dfc0cfSAnup Patel * @len: buffer length
102*76dfc0cfSAnup Patel * @flags: descriptor flags
103*76dfc0cfSAnup Patel * @next: index of the next descriptor in the chain,
104*76dfc0cfSAnup Patel * if the VRING_DESC_F_NEXT flag is set. We chain unused
105*76dfc0cfSAnup Patel * descriptors via this, too.
106*76dfc0cfSAnup Patel */
1071a992bbaSAndre Przywara struct vring_desc {
1081a992bbaSAndre Przywara __virtio64 addr;
1091a992bbaSAndre Przywara __virtio32 len;
1101a992bbaSAndre Przywara __virtio16 flags;
1111a992bbaSAndre Przywara __virtio16 next;
1121a992bbaSAndre Przywara };
1131a992bbaSAndre Przywara
1141a992bbaSAndre Przywara struct vring_avail {
1151a992bbaSAndre Przywara __virtio16 flags;
1161a992bbaSAndre Przywara __virtio16 idx;
1171a992bbaSAndre Przywara __virtio16 ring[];
1181a992bbaSAndre Przywara };
1191a992bbaSAndre Przywara
1201a992bbaSAndre Przywara /* u32 is used here for ids for padding reasons. */
1211a992bbaSAndre Przywara struct vring_used_elem {
1221a992bbaSAndre Przywara /* Index of start of used descriptor chain. */
1231a992bbaSAndre Przywara __virtio32 id;
1241a992bbaSAndre Przywara /* Total length of the descriptor chain which was used (written to) */
1251a992bbaSAndre Przywara __virtio32 len;
1261a992bbaSAndre Przywara };
1271a992bbaSAndre Przywara
1281a992bbaSAndre Przywara typedef struct vring_used_elem __attribute__((aligned(VRING_USED_ALIGN_SIZE)))
1291a992bbaSAndre Przywara vring_used_elem_t;
1301a992bbaSAndre Przywara
1311a992bbaSAndre Przywara struct vring_used {
1321a992bbaSAndre Przywara __virtio16 flags;
1331a992bbaSAndre Przywara __virtio16 idx;
1341a992bbaSAndre Przywara vring_used_elem_t ring[];
1351a992bbaSAndre Przywara };
1361a992bbaSAndre Przywara
1371a992bbaSAndre Przywara /*
1381a992bbaSAndre Przywara * The ring element addresses are passed between components with different
1391a992bbaSAndre Przywara * alignments assumptions. Thus, we might need to decrease the compiler-selected
1401a992bbaSAndre Przywara * alignment, and so must use a typedef to make sure the aligned attribute
1411a992bbaSAndre Przywara * actually takes hold:
1421a992bbaSAndre Przywara *
1431a992bbaSAndre Przywara * https://gcc.gnu.org/onlinedocs//gcc/Common-Type-Attributes.html#Common-Type-Attributes
1441a992bbaSAndre Przywara *
1451a992bbaSAndre Przywara * When used on a struct, or struct member, the aligned attribute can only
1461a992bbaSAndre Przywara * increase the alignment; in order to decrease it, the packed attribute must
1471a992bbaSAndre Przywara * be specified as well. When used as part of a typedef, the aligned attribute
1481a992bbaSAndre Przywara * can both increase and decrease alignment, and specifying the packed
1491a992bbaSAndre Przywara * attribute generates a warning.
1501a992bbaSAndre Przywara */
1511a992bbaSAndre Przywara typedef struct vring_desc __attribute__((aligned(VRING_DESC_ALIGN_SIZE)))
1521a992bbaSAndre Przywara vring_desc_t;
1531a992bbaSAndre Przywara typedef struct vring_avail __attribute__((aligned(VRING_AVAIL_ALIGN_SIZE)))
1541a992bbaSAndre Przywara vring_avail_t;
1551a992bbaSAndre Przywara typedef struct vring_used __attribute__((aligned(VRING_USED_ALIGN_SIZE)))
1561a992bbaSAndre Przywara vring_used_t;
1571a992bbaSAndre Przywara
1581a992bbaSAndre Przywara struct vring {
1591a992bbaSAndre Przywara unsigned int num;
1601a992bbaSAndre Przywara
1611a992bbaSAndre Przywara vring_desc_t *desc;
1621a992bbaSAndre Przywara
1631a992bbaSAndre Przywara vring_avail_t *avail;
1641a992bbaSAndre Przywara
1651a992bbaSAndre Przywara vring_used_t *used;
1661a992bbaSAndre Przywara };
1671a992bbaSAndre Przywara
1681a992bbaSAndre Przywara #ifndef VIRTIO_RING_NO_LEGACY
1691a992bbaSAndre Przywara
1701a992bbaSAndre Przywara /* The standard layout for the ring is a continuous chunk of memory which looks
1711a992bbaSAndre Przywara * like this. We assume num is a power of 2.
1721a992bbaSAndre Przywara *
1731a992bbaSAndre Przywara * struct vring
1741a992bbaSAndre Przywara * {
1751a992bbaSAndre Przywara * // The actual descriptors (16 bytes each)
1761a992bbaSAndre Przywara * struct vring_desc desc[num];
1771a992bbaSAndre Przywara *
1781a992bbaSAndre Przywara * // A ring of available descriptor heads with free-running index.
1791a992bbaSAndre Przywara * __virtio16 avail_flags;
1801a992bbaSAndre Przywara * __virtio16 avail_idx;
1811a992bbaSAndre Przywara * __virtio16 available[num];
1821a992bbaSAndre Przywara * __virtio16 used_event_idx;
1831a992bbaSAndre Przywara *
1841a992bbaSAndre Przywara * // Padding to the next align boundary.
1851a992bbaSAndre Przywara * char pad[];
1861a992bbaSAndre Przywara *
1871a992bbaSAndre Przywara * // A ring of used descriptor heads with free-running index.
1881a992bbaSAndre Przywara * __virtio16 used_flags;
1891a992bbaSAndre Przywara * __virtio16 used_idx;
1901a992bbaSAndre Przywara * struct vring_used_elem used[num];
1911a992bbaSAndre Przywara * __virtio16 avail_event_idx;
1921a992bbaSAndre Przywara * };
1931a992bbaSAndre Przywara */
1941a992bbaSAndre Przywara /* We publish the used event index at the end of the available ring, and vice
1951a992bbaSAndre Przywara * versa. They are at the end for backwards compatibility. */
1961a992bbaSAndre Przywara #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num])
1971a992bbaSAndre Przywara #define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num])
1981a992bbaSAndre Przywara
vring_init(struct vring * vr,unsigned int num,void * p,unsigned long align)1991a992bbaSAndre Przywara static inline void vring_init(struct vring *vr, unsigned int num, void *p,
2001a992bbaSAndre Przywara unsigned long align)
2011a992bbaSAndre Przywara {
2021a992bbaSAndre Przywara vr->num = num;
2031a992bbaSAndre Przywara vr->desc = p;
2041a992bbaSAndre Przywara vr->avail = (struct vring_avail *)((char *)p + num * sizeof(struct vring_desc));
2051a992bbaSAndre Przywara vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16)
2061a992bbaSAndre Przywara + align-1) & ~(align - 1));
2071a992bbaSAndre Przywara }
2081a992bbaSAndre Przywara
vring_size(unsigned int num,unsigned long align)2091a992bbaSAndre Przywara static inline unsigned vring_size(unsigned int num, unsigned long align)
2101a992bbaSAndre Przywara {
2111a992bbaSAndre Przywara return ((sizeof(struct vring_desc) * num + sizeof(__virtio16) * (3 + num)
2121a992bbaSAndre Przywara + align - 1) & ~(align - 1))
2131a992bbaSAndre Przywara + sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num;
2141a992bbaSAndre Przywara }
2151a992bbaSAndre Przywara
2161a992bbaSAndre Przywara #endif /* VIRTIO_RING_NO_LEGACY */
2171a992bbaSAndre Przywara
2181a992bbaSAndre Przywara /* The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX */
2191a992bbaSAndre Przywara /* Assuming a given event_idx value from the other side, if
2201a992bbaSAndre Przywara * we have just incremented index from old to new_idx,
2211a992bbaSAndre Przywara * should we trigger an event? */
vring_need_event(__u16 event_idx,__u16 new_idx,__u16 old)2221a992bbaSAndre Przywara static inline int vring_need_event(__u16 event_idx, __u16 new_idx, __u16 old)
2231a992bbaSAndre Przywara {
2241a992bbaSAndre Przywara /* Note: Xen has similar logic for notification hold-off
2251a992bbaSAndre Przywara * in include/xen/interface/io/ring.h with req_event and req_prod
2261a992bbaSAndre Przywara * corresponding to event_idx + 1 and new_idx respectively.
2271a992bbaSAndre Przywara * Note also that req_event and req_prod in Xen start at 1,
2281a992bbaSAndre Przywara * event indexes in virtio start at 0. */
2291a992bbaSAndre Przywara return (__u16)(new_idx - event_idx - 1) < (__u16)(new_idx - old);
2301a992bbaSAndre Przywara }
2311a992bbaSAndre Przywara
2321a992bbaSAndre Przywara struct vring_packed_desc_event {
2331a992bbaSAndre Przywara /* Descriptor Ring Change Event Offset/Wrap Counter. */
2341a992bbaSAndre Przywara __le16 off_wrap;
2351a992bbaSAndre Przywara /* Descriptor Ring Change Event Flags. */
2361a992bbaSAndre Przywara __le16 flags;
2371a992bbaSAndre Przywara };
2381a992bbaSAndre Przywara
2391a992bbaSAndre Przywara struct vring_packed_desc {
2401a992bbaSAndre Przywara /* Buffer Address. */
2411a992bbaSAndre Przywara __le64 addr;
2421a992bbaSAndre Przywara /* Buffer Length. */
2431a992bbaSAndre Przywara __le32 len;
2441a992bbaSAndre Przywara /* Buffer ID. */
2451a992bbaSAndre Przywara __le16 id;
2461a992bbaSAndre Przywara /* The flags depending on descriptor type. */
2471a992bbaSAndre Przywara __le16 flags;
2481a992bbaSAndre Przywara };
2491a992bbaSAndre Przywara
2501a992bbaSAndre Przywara #endif /* _UAPI_LINUX_VIRTIO_RING_H */
251