xref: /minix3/minix/lib/libvirtio/virtio_ring.h (revision 65f76edb8f568553a5b4386b0c4917803d47e97d)
1433d6423SLionel Sambuc #ifndef _LINUX_VIRTIO_RING_H
2433d6423SLionel Sambuc #define _LINUX_VIRTIO_RING_H
3433d6423SLionel Sambuc /* An interface for efficient virtio implementation, currently for use by KVM
4433d6423SLionel Sambuc  * and lguest, but hopefully others soon.  Do NOT change this since it will
5433d6423SLionel Sambuc  * break existing servers and clients.
6433d6423SLionel Sambuc  *
7433d6423SLionel Sambuc  * This header is BSD licensed so anyone can use the definitions to implement
8433d6423SLionel Sambuc  * compatible drivers/servers.
9433d6423SLionel Sambuc  *
10433d6423SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11433d6423SLionel Sambuc  * modification, are permitted provided that the following conditions
12433d6423SLionel Sambuc  * are met:
13433d6423SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14433d6423SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15433d6423SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16433d6423SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17433d6423SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18433d6423SLionel Sambuc  * 3. Neither the name of IBM nor the names of its contributors
19433d6423SLionel Sambuc  *    may be used to endorse or promote products derived from this software
20433d6423SLionel Sambuc  *    without specific prior written permission.
21433d6423SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
22433d6423SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23433d6423SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24433d6423SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
25433d6423SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26433d6423SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27433d6423SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28433d6423SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29433d6423SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30433d6423SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31433d6423SLionel Sambuc  * SUCH DAMAGE.
32433d6423SLionel Sambuc  *
33433d6423SLionel Sambuc  * Copyright Rusty Russell IBM Corporation 2007. */
34433d6423SLionel Sambuc 
35433d6423SLionel Sambuc /* This marks a buffer as continuing via the next field. */
36433d6423SLionel Sambuc #define VRING_DESC_F_NEXT	1
37433d6423SLionel Sambuc /* This marks a buffer as write-only (otherwise read-only). */
38433d6423SLionel Sambuc #define VRING_DESC_F_WRITE	2
39433d6423SLionel Sambuc /* This means the buffer contains a list of buffer descriptors. */
40433d6423SLionel Sambuc #define VRING_DESC_F_INDIRECT	4
41433d6423SLionel Sambuc 
42433d6423SLionel Sambuc /* The Host uses this in used->flags to advise the Guest: don't kick me when
43433d6423SLionel Sambuc  * you add a buffer.  It's unreliable, so it's simply an optimization.  Guest
44433d6423SLionel Sambuc  * will still kick if it's out of buffers. */
45433d6423SLionel Sambuc #define VRING_USED_F_NO_NOTIFY	1
46433d6423SLionel Sambuc /* The Guest uses this in avail->flags to advise the Host: don't interrupt me
47433d6423SLionel Sambuc  * when you consume a buffer.  It's unreliable, so it's simply an
48433d6423SLionel Sambuc  * optimization.  */
49433d6423SLionel Sambuc #define VRING_AVAIL_F_NO_INTERRUPT	1
50433d6423SLionel Sambuc 
51433d6423SLionel Sambuc /* We support indirect buffer descriptors */
52433d6423SLionel Sambuc #define VIRTIO_RING_F_INDIRECT_DESC	28
53433d6423SLionel Sambuc 
54433d6423SLionel Sambuc /* The Guest publishes the used index for which it expects an interrupt
55433d6423SLionel Sambuc  * at the end of the avail ring. Host should ignore the avail->flags field. */
56433d6423SLionel Sambuc /* The Host publishes the avail index for which it expects a kick
57433d6423SLionel Sambuc  * at the end of the used ring. Guest should ignore the used->flags field. */
58433d6423SLionel Sambuc #define VIRTIO_RING_F_EVENT_IDX		29
59433d6423SLionel Sambuc 
60433d6423SLionel Sambuc /* Virtio ring descriptors: 16 bytes.  These can chain together via "next". */
61433d6423SLionel Sambuc struct vring_desc {
62433d6423SLionel Sambuc 	/* Address (guest-physical). */
63433d6423SLionel Sambuc 	u64_t addr;
64433d6423SLionel Sambuc 	/* Length. */
65433d6423SLionel Sambuc 	u32_t len;
66433d6423SLionel Sambuc 	/* The flags as indicated above. */
67433d6423SLionel Sambuc 	u16_t flags;
68433d6423SLionel Sambuc 	/* We chain unused descriptors via this, too */
69433d6423SLionel Sambuc 	u16_t next;
70433d6423SLionel Sambuc };
71433d6423SLionel Sambuc 
72433d6423SLionel Sambuc struct vring_avail {
73433d6423SLionel Sambuc 	u16_t flags;
74433d6423SLionel Sambuc 	u16_t idx;
75433d6423SLionel Sambuc 	u16_t ring[];
76433d6423SLionel Sambuc };
77433d6423SLionel Sambuc 
78433d6423SLionel Sambuc /* u32 is used here for ids for padding reasons. */
79433d6423SLionel Sambuc struct vring_used_elem {
80433d6423SLionel Sambuc 	/* Index of start of used descriptor chain. */
81433d6423SLionel Sambuc 	u32_t id;
82433d6423SLionel Sambuc 	/* Total length of the descriptor chain which was used (written to) */
83433d6423SLionel Sambuc 	u32_t len;
84433d6423SLionel Sambuc };
85433d6423SLionel Sambuc 
86433d6423SLionel Sambuc struct vring_used {
87433d6423SLionel Sambuc 	u16_t flags;
88433d6423SLionel Sambuc 	u16_t idx;
89433d6423SLionel Sambuc 	struct vring_used_elem ring[];
90433d6423SLionel Sambuc };
91433d6423SLionel Sambuc 
92433d6423SLionel Sambuc struct vring {
93433d6423SLionel Sambuc 	unsigned int num;
94433d6423SLionel Sambuc 
95433d6423SLionel Sambuc 	struct vring_desc *desc;
96433d6423SLionel Sambuc 
97433d6423SLionel Sambuc 	struct vring_avail *avail;
98433d6423SLionel Sambuc 
99433d6423SLionel Sambuc 	struct vring_used *used;
100433d6423SLionel Sambuc };
101433d6423SLionel Sambuc 
102433d6423SLionel Sambuc /* The standard layout for the ring is a continuous chunk of memory which looks
103433d6423SLionel Sambuc  * like this.  We assume num is a power of 2.
104433d6423SLionel Sambuc  *
105433d6423SLionel Sambuc  * struct vring
106433d6423SLionel Sambuc  * {
107433d6423SLionel Sambuc  *	// The actual descriptors (16 bytes each)
108433d6423SLionel Sambuc  *	struct vring_desc desc[num];
109433d6423SLionel Sambuc  *
110433d6423SLionel Sambuc  *	// A ring of available descriptor heads with free-running index.
111433d6423SLionel Sambuc  *	u16_t avail_flags;
112433d6423SLionel Sambuc  *	u16_t avail_idx;
113433d6423SLionel Sambuc  *	u16_t available[num];
114433d6423SLionel Sambuc  *	u16_t used_event_idx;
115433d6423SLionel Sambuc  *
116433d6423SLionel Sambuc  *	// Padding to the next align boundary.
117433d6423SLionel Sambuc  *	char pad[];
118433d6423SLionel Sambuc  *
119433d6423SLionel Sambuc  *	// A ring of used descriptor heads with free-running index.
120433d6423SLionel Sambuc  *	u16_t used_flags;
121433d6423SLionel Sambuc  *	u16_t used_idx;
122433d6423SLionel Sambuc  *	struct vring_used_elem used[num];
123433d6423SLionel Sambuc  *	u16_t avail_event_idx;
124433d6423SLionel Sambuc  * };
125433d6423SLionel Sambuc  */
126433d6423SLionel Sambuc /* We publish the used event index at the end of the available ring, and vice
127433d6423SLionel Sambuc  * versa. They are at the end for backwards compatibility. */
128433d6423SLionel Sambuc #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num])
129433d6423SLionel Sambuc #define vring_avail_event(vr) (*(u16_t *)&(vr)->used->ring[(vr)->num])
130433d6423SLionel Sambuc 
vring_init(struct vring * vr,unsigned int num,void * p,unsigned long align)131433d6423SLionel Sambuc static inline void vring_init(struct vring *vr, unsigned int num, void *p,
132433d6423SLionel Sambuc 			      unsigned long align)
133433d6423SLionel Sambuc {
134433d6423SLionel Sambuc 	vr->num = num;
135433d6423SLionel Sambuc 	vr->desc = p;
136*65f76edbSDavid van Moolenbroek 	vr->avail = (void *)((char *)p + num*sizeof(struct vring_desc));
137433d6423SLionel Sambuc 	vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + sizeof(u16_t)
138433d6423SLionel Sambuc 		+ align-1) & ~(align - 1));
139433d6423SLionel Sambuc }
140433d6423SLionel Sambuc 
vring_size(unsigned int num,unsigned long align)141433d6423SLionel Sambuc static inline unsigned vring_size(unsigned int num, unsigned long align)
142433d6423SLionel Sambuc {
143433d6423SLionel Sambuc 	return ((sizeof(struct vring_desc) * num + sizeof(u16_t) * (3 + num)
144433d6423SLionel Sambuc 		 + align - 1) & ~(align - 1))
145433d6423SLionel Sambuc 		+ sizeof(u16_t) * 3 + sizeof(struct vring_used_elem) * num;
146433d6423SLionel Sambuc }
147433d6423SLionel Sambuc 
148433d6423SLionel Sambuc #if 0
149433d6423SLionel Sambuc /* The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX */
150433d6423SLionel Sambuc /* Assuming a given event_idx value from the other size, if
151433d6423SLionel Sambuc  * we have just incremented index from old to new_idx,
152433d6423SLionel Sambuc  * should we trigger an event? */
153433d6423SLionel Sambuc static inline int vring_need_event(u16_t event_idx, u16_t new_idx, u16_t old)
154433d6423SLionel Sambuc {
155433d6423SLionel Sambuc 	/* Note: Xen has similar logic for notification hold-off
156433d6423SLionel Sambuc 	 * in include/xen/interface/io/ring.h with req_event and req_prod
157433d6423SLionel Sambuc 	 * corresponding to event_idx + 1 and new_idx respectively.
158433d6423SLionel Sambuc 	 * Note also that req_event and req_prod in Xen start at 1,
159433d6423SLionel Sambuc 	 * event indexes in virtio start at 0. */
160433d6423SLionel Sambuc 	return (u16_t)(new_idx - event_idx - 1) < (u16_t)(new_idx - old);
161433d6423SLionel Sambuc }
162433d6423SLionel Sambuc 
163433d6423SLionel Sambuc #ifdef __KERNEL__
164433d6423SLionel Sambuc #include <linux/irqreturn.h>
165433d6423SLionel Sambuc struct virtio_device;
166433d6423SLionel Sambuc struct virtqueue;
167433d6423SLionel Sambuc 
168433d6423SLionel Sambuc struct virtqueue *vring_new_virtqueue(unsigned int num,
169433d6423SLionel Sambuc 				      unsigned int vring_align,
170433d6423SLionel Sambuc 				      struct virtio_device *vdev,
171433d6423SLionel Sambuc 				      bool weak_barriers,
172433d6423SLionel Sambuc 				      void *pages,
173433d6423SLionel Sambuc 				      void (*notify)(struct virtqueue *vq),
174433d6423SLionel Sambuc 				      void (*callback)(struct virtqueue *vq),
175433d6423SLionel Sambuc 				      const char *name);
176433d6423SLionel Sambuc void vring_del_virtqueue(struct virtqueue *vq);
177433d6423SLionel Sambuc /* Filter out transport-specific feature bits. */
178433d6423SLionel Sambuc void vring_transport_features(struct virtio_device *vdev);
179433d6423SLionel Sambuc 
180433d6423SLionel Sambuc irqreturn_t vring_interrupt(int irq, void *_vq);
181433d6423SLionel Sambuc #endif /* __KERNEL__ */
182433d6423SLionel Sambuc #endif /* 0 */
183433d6423SLionel Sambuc #endif /* _LINUX_VIRTIO_RING_H */
184