xref: /openbmc/qemu/subprojects/libvduse/standard-headers/linux/virtio_ring.h (revision d4083f50e021f52addd02c9f83465937f8486556)
19fbe302bSMichael S. Tsirkin #ifndef _LINUX_VIRTIO_RING_H
29fbe302bSMichael S. Tsirkin #define _LINUX_VIRTIO_RING_H
3*d4083f50SAlexey Perevalov /* An interface for efficient virtio implementation, currently for use by KVM,
4*d4083f50SAlexey Perevalov  * but hopefully others soon.  Do NOT change this since it will
59fbe302bSMichael S. Tsirkin  * break existing servers and clients.
69fbe302bSMichael S. Tsirkin  *
79fbe302bSMichael S. Tsirkin  * This header is BSD licensed so anyone can use the definitions to implement
89fbe302bSMichael S. Tsirkin  * compatible drivers/servers.
99fbe302bSMichael S. Tsirkin  *
109fbe302bSMichael S. Tsirkin  * Redistribution and use in source and binary forms, with or without
119fbe302bSMichael S. Tsirkin  * modification, are permitted provided that the following conditions
129fbe302bSMichael S. Tsirkin  * are met:
139fbe302bSMichael S. Tsirkin  * 1. Redistributions of source code must retain the above copyright
149fbe302bSMichael S. Tsirkin  *    notice, this list of conditions and the following disclaimer.
159fbe302bSMichael S. Tsirkin  * 2. Redistributions in binary form must reproduce the above copyright
169fbe302bSMichael S. Tsirkin  *    notice, this list of conditions and the following disclaimer in the
179fbe302bSMichael S. Tsirkin  *    documentation and/or other materials provided with the distribution.
189fbe302bSMichael S. Tsirkin  * 3. Neither the name of IBM nor the names of its contributors
199fbe302bSMichael S. Tsirkin  *    may be used to endorse or promote products derived from this software
209fbe302bSMichael S. Tsirkin  *    without specific prior written permission.
219fbe302bSMichael S. Tsirkin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
229fbe302bSMichael S. Tsirkin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239fbe302bSMichael S. Tsirkin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249fbe302bSMichael S. Tsirkin  * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
259fbe302bSMichael S. Tsirkin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269fbe302bSMichael S. Tsirkin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279fbe302bSMichael S. Tsirkin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289fbe302bSMichael S. Tsirkin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299fbe302bSMichael S. Tsirkin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309fbe302bSMichael S. Tsirkin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319fbe302bSMichael S. Tsirkin  * SUCH DAMAGE.
329fbe302bSMichael S. Tsirkin  *
339fbe302bSMichael S. Tsirkin  * Copyright Rusty Russell IBM Corporation 2007. */
34120758fbSPaolo Bonzini #include <stdint.h>
359fbe302bSMichael S. Tsirkin #include "standard-headers/linux/types.h"
369fbe302bSMichael S. Tsirkin #include "standard-headers/linux/virtio_types.h"
379fbe302bSMichael S. Tsirkin 
389fbe302bSMichael S. Tsirkin /* This marks a buffer as continuing via the next field. */
399fbe302bSMichael S. Tsirkin #define VRING_DESC_F_NEXT	1
409fbe302bSMichael S. Tsirkin /* This marks a buffer as write-only (otherwise read-only). */
419fbe302bSMichael S. Tsirkin #define VRING_DESC_F_WRITE	2
429fbe302bSMichael S. Tsirkin /* This means the buffer contains a list of buffer descriptors. */
439fbe302bSMichael S. Tsirkin #define VRING_DESC_F_INDIRECT	4
449fbe302bSMichael S. Tsirkin 
459fbe302bSMichael S. Tsirkin /* The Host uses this in used->flags to advise the Guest: don't kick me when
469fbe302bSMichael S. Tsirkin  * you add a buffer.  It's unreliable, so it's simply an optimization.  Guest
479fbe302bSMichael S. Tsirkin  * will still kick if it's out of buffers. */
489fbe302bSMichael S. Tsirkin #define VRING_USED_F_NO_NOTIFY	1
499fbe302bSMichael S. Tsirkin /* The Guest uses this in avail->flags to advise the Host: don't interrupt me
509fbe302bSMichael S. Tsirkin  * when you consume a buffer.  It's unreliable, so it's simply an
519fbe302bSMichael S. Tsirkin  * optimization.  */
529fbe302bSMichael S. Tsirkin #define VRING_AVAIL_F_NO_INTERRUPT	1
539fbe302bSMichael S. Tsirkin 
549fbe302bSMichael S. Tsirkin /* We support indirect buffer descriptors */
559fbe302bSMichael S. Tsirkin #define VIRTIO_RING_F_INDIRECT_DESC	28
569fbe302bSMichael S. Tsirkin 
579fbe302bSMichael S. Tsirkin /* The Guest publishes the used index for which it expects an interrupt
589fbe302bSMichael S. Tsirkin  * at the end of the avail ring. Host should ignore the avail->flags field. */
599fbe302bSMichael S. Tsirkin /* The Host publishes the avail index for which it expects a kick
609fbe302bSMichael S. Tsirkin  * at the end of the used ring. Guest should ignore the used->flags field. */
619fbe302bSMichael S. Tsirkin #define VIRTIO_RING_F_EVENT_IDX		29
629fbe302bSMichael S. Tsirkin 
639fbe302bSMichael S. Tsirkin /* Virtio ring descriptors: 16 bytes.  These can chain together via "next". */
649fbe302bSMichael S. Tsirkin struct vring_desc {
659fbe302bSMichael S. Tsirkin 	/* Address (guest-physical). */
669fbe302bSMichael S. Tsirkin 	__virtio64 addr;
679fbe302bSMichael S. Tsirkin 	/* Length. */
689fbe302bSMichael S. Tsirkin 	__virtio32 len;
699fbe302bSMichael S. Tsirkin 	/* The flags as indicated above. */
709fbe302bSMichael S. Tsirkin 	__virtio16 flags;
719fbe302bSMichael S. Tsirkin 	/* We chain unused descriptors via this, too */
729fbe302bSMichael S. Tsirkin 	__virtio16 next;
739fbe302bSMichael S. Tsirkin };
749fbe302bSMichael S. Tsirkin 
759fbe302bSMichael S. Tsirkin struct vring_avail {
769fbe302bSMichael S. Tsirkin 	__virtio16 flags;
779fbe302bSMichael S. Tsirkin 	__virtio16 idx;
789fbe302bSMichael S. Tsirkin 	__virtio16 ring[];
799fbe302bSMichael S. Tsirkin };
809fbe302bSMichael S. Tsirkin 
819fbe302bSMichael S. Tsirkin /* u32 is used here for ids for padding reasons. */
829fbe302bSMichael S. Tsirkin struct vring_used_elem {
839fbe302bSMichael S. Tsirkin 	/* Index of start of used descriptor chain. */
849fbe302bSMichael S. Tsirkin 	__virtio32 id;
859fbe302bSMichael S. Tsirkin 	/* Total length of the descriptor chain which was used (written to) */
869fbe302bSMichael S. Tsirkin 	__virtio32 len;
879fbe302bSMichael S. Tsirkin };
889fbe302bSMichael S. Tsirkin 
899fbe302bSMichael S. Tsirkin struct vring_used {
909fbe302bSMichael S. Tsirkin 	__virtio16 flags;
919fbe302bSMichael S. Tsirkin 	__virtio16 idx;
929fbe302bSMichael S. Tsirkin 	struct vring_used_elem ring[];
939fbe302bSMichael S. Tsirkin };
949fbe302bSMichael S. Tsirkin 
959fbe302bSMichael S. Tsirkin struct vring {
969fbe302bSMichael S. Tsirkin 	unsigned int num;
979fbe302bSMichael S. Tsirkin 
989fbe302bSMichael S. Tsirkin 	struct vring_desc *desc;
999fbe302bSMichael S. Tsirkin 
1009fbe302bSMichael S. Tsirkin 	struct vring_avail *avail;
1019fbe302bSMichael S. Tsirkin 
1029fbe302bSMichael S. Tsirkin 	struct vring_used *used;
1039fbe302bSMichael S. Tsirkin };
1049fbe302bSMichael S. Tsirkin 
1059fbe302bSMichael S. Tsirkin /* Alignment requirements for vring elements.
1069fbe302bSMichael S. Tsirkin  * When using pre-virtio 1.0 layout, these fall out naturally.
1079fbe302bSMichael S. Tsirkin  */
1089fbe302bSMichael S. Tsirkin #define VRING_AVAIL_ALIGN_SIZE 2
1099fbe302bSMichael S. Tsirkin #define VRING_USED_ALIGN_SIZE 4
1109fbe302bSMichael S. Tsirkin #define VRING_DESC_ALIGN_SIZE 16
1119fbe302bSMichael S. Tsirkin 
1129fbe302bSMichael S. Tsirkin /* The standard layout for the ring is a continuous chunk of memory which looks
1139fbe302bSMichael S. Tsirkin  * like this.  We assume num is a power of 2.
1149fbe302bSMichael S. Tsirkin  *
1159fbe302bSMichael S. Tsirkin  * struct vring
1169fbe302bSMichael S. Tsirkin  * {
1179fbe302bSMichael S. Tsirkin  *	// The actual descriptors (16 bytes each)
1189fbe302bSMichael S. Tsirkin  *	struct vring_desc desc[num];
1199fbe302bSMichael S. Tsirkin  *
1209fbe302bSMichael S. Tsirkin  *	// A ring of available descriptor heads with free-running index.
1219fbe302bSMichael S. Tsirkin  *	__virtio16 avail_flags;
1229fbe302bSMichael S. Tsirkin  *	__virtio16 avail_idx;
1239fbe302bSMichael S. Tsirkin  *	__virtio16 available[num];
1249fbe302bSMichael S. Tsirkin  *	__virtio16 used_event_idx;
1259fbe302bSMichael S. Tsirkin  *
1269fbe302bSMichael S. Tsirkin  *	// Padding to the next align boundary.
1279fbe302bSMichael S. Tsirkin  *	char pad[];
1289fbe302bSMichael S. Tsirkin  *
1299fbe302bSMichael S. Tsirkin  *	// A ring of used descriptor heads with free-running index.
1309fbe302bSMichael S. Tsirkin  *	__virtio16 used_flags;
1319fbe302bSMichael S. Tsirkin  *	__virtio16 used_idx;
1329fbe302bSMichael S. Tsirkin  *	struct vring_used_elem used[num];
1339fbe302bSMichael S. Tsirkin  *	__virtio16 avail_event_idx;
1349fbe302bSMichael S. Tsirkin  * };
1359fbe302bSMichael S. Tsirkin  */
1369fbe302bSMichael S. Tsirkin /* We publish the used event index at the end of the available ring, and vice
1379fbe302bSMichael S. Tsirkin  * versa. They are at the end for backwards compatibility. */
1389fbe302bSMichael S. Tsirkin #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num])
1399fbe302bSMichael S. Tsirkin #define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num])
1409fbe302bSMichael S. Tsirkin 
141e0d2be2aSMichael S. Tsirkin static inline void vring_init(struct vring *vr, unsigned int num, void *p,
1429fbe302bSMichael S. Tsirkin 			      unsigned long align)
1439fbe302bSMichael S. Tsirkin {
1449fbe302bSMichael S. Tsirkin 	vr->num = num;
1459fbe302bSMichael S. Tsirkin 	vr->desc = p;
1469fbe302bSMichael S. Tsirkin 	vr->avail = p + num*sizeof(struct vring_desc);
147120758fbSPaolo Bonzini 	vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16)
1489fbe302bSMichael S. Tsirkin 		+ align-1) & ~(align - 1));
1499fbe302bSMichael S. Tsirkin }
1509fbe302bSMichael S. Tsirkin 
151e0d2be2aSMichael S. Tsirkin static inline unsigned vring_size(unsigned int num, unsigned long align)
1529fbe302bSMichael S. Tsirkin {
1539fbe302bSMichael S. Tsirkin 	return ((sizeof(struct vring_desc) * num + sizeof(__virtio16) * (3 + num)
1549fbe302bSMichael S. Tsirkin 		 + align - 1) & ~(align - 1))
1559fbe302bSMichael S. Tsirkin 		+ sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num;
1569fbe302bSMichael S. Tsirkin }
1579fbe302bSMichael S. Tsirkin 
1589fbe302bSMichael S. Tsirkin /* The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX */
15924a31426SPaolo Bonzini /* Assuming a given event_idx value from the other side, if
1609fbe302bSMichael S. Tsirkin  * we have just incremented index from old to new_idx,
1619fbe302bSMichael S. Tsirkin  * should we trigger an event? */
162e0d2be2aSMichael S. Tsirkin static inline int vring_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
1639fbe302bSMichael S. Tsirkin {
1649fbe302bSMichael S. Tsirkin 	/* Note: Xen has similar logic for notification hold-off
1659fbe302bSMichael S. Tsirkin 	 * in include/xen/interface/io/ring.h with req_event and req_prod
1669fbe302bSMichael S. Tsirkin 	 * corresponding to event_idx + 1 and new_idx respectively.
1679fbe302bSMichael S. Tsirkin 	 * Note also that req_event and req_prod in Xen start at 1,
1689fbe302bSMichael S. Tsirkin 	 * event indexes in virtio start at 0. */
1699fbe302bSMichael S. Tsirkin 	return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
1709fbe302bSMichael S. Tsirkin }
1719fbe302bSMichael S. Tsirkin 
1729fbe302bSMichael S. Tsirkin #endif /* _LINUX_VIRTIO_RING_H */
173