xref: /freebsd/sys/dev/virtio/virtio_ring.h (revision 7aa1dba6b00ccfb7d66627badc8a7aaa06b02946)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright Rusty Russell IBM Corporation 2007.
5  *
6  * This header is BSD licensed so anyone can use the definitions to implement
7  * compatible drivers/servers.
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  * 3. Neither the name of IBM nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef VIRTIO_RING_H
34 #define	VIRTIO_RING_H
35 
36 /* This marks a buffer as continuing via the next field. */
37 #define VRING_DESC_F_NEXT       1
38 /* This marks a buffer as write-only (otherwise read-only). */
39 #define VRING_DESC_F_WRITE      2
40 /* This means the buffer contains a list of buffer descriptors. */
41 #define VRING_DESC_F_INDIRECT	4
42 
43 /* The Host uses this in used->flags to advise the Guest: don't kick me
44  * when you add a buffer.  It's unreliable, so it's simply an
45  * optimization.  Guest will still kick if it's out of buffers. */
46 #define VRING_USED_F_NO_NOTIFY  1
47 /* The Guest uses this in avail->flags to advise the Host: don't
48  * interrupt me when you consume a buffer.  It's unreliable, so it's
49  * simply an optimization.  */
50 #define VRING_AVAIL_F_NO_INTERRUPT      1
51 
52 /* VirtIO ring descriptors: 16 bytes.
53  * These can chain together via "next". */
54 struct vring_desc {
55         /* Address (guest-physical). */
56         uint64_t addr;
57         /* Length. */
58         uint32_t len;
59         /* The flags as indicated above. */
60         uint16_t flags;
61         /* We chain unused descriptors via this, too. */
62         uint16_t next;
63 };
64 
65 struct vring_avail {
66         uint16_t flags;
67         uint16_t idx;
68         uint16_t ring[0];
69 };
70 
71 /* uint32_t is used here for ids for padding reasons. */
72 struct vring_used_elem {
73         /* Index of start of used descriptor chain. */
74         uint32_t id;
75         /* Total length of the descriptor chain which was written to. */
76         uint32_t len;
77 };
78 
79 struct vring_used {
80         uint16_t flags;
81         uint16_t idx;
82         struct vring_used_elem ring[0];
83 };
84 
85 struct vring {
86 	unsigned int num;
87 
88 	struct vring_desc *desc;
89 	struct vring_avail *avail;
90 	struct vring_used *used;
91 
92 	vm_paddr_t desc_paddr;
93 	vm_paddr_t avail_paddr;
94 	vm_paddr_t used_paddr;
95 };
96 
97 /* Alignment requirements for vring elements.
98  * When using pre-virtio 1.0 layout, these fall out naturally.
99  */
100 #define VRING_AVAIL_ALIGN_SIZE 2
101 #define VRING_USED_ALIGN_SIZE 4
102 #define VRING_DESC_ALIGN_SIZE 16
103 
104 /* The standard layout for the ring is a continuous chunk of memory which
105  * looks like this.  We assume num is a power of 2.
106  *
107  * struct vring {
108  *      // The actual descriptors (16 bytes each)
109  *      struct vring_desc desc[num];
110  *
111  *      // A ring of available descriptor heads with free-running index.
112  *      __u16 avail_flags;
113  *      __u16 avail_idx;
114  *      __u16 available[num];
115  *      __u16 used_event_idx;
116  *
117  *      // Padding to the next align boundary.
118  *      char pad[];
119  *
120  *      // A ring of used descriptor heads with free-running index.
121  *      __u16 used_flags;
122  *      __u16 used_idx;
123  *      struct vring_used_elem used[num];
124  *      __u16 avail_event_idx;
125  * };
126  *
127  * NOTE: for VirtIO PCI, align is 4096.
128  */
129 
130 /*
131  * We publish the used event index at the end of the available ring, and vice
132  * versa. They are at the end for backwards compatibility.
133  */
134 #define vring_used_event(vr)	((vr)->avail->ring[(vr)->num])
135 #define vring_avail_event(vr)	(*(uint16_t *)&(vr)->used->ring[(vr)->num])
136 
137 static inline int
138 vring_size(unsigned int num, unsigned long align)
139 {
140 	int size;
141 
142 	size = num * sizeof(struct vring_desc);
143 	size += sizeof(struct vring_avail) + (num * sizeof(uint16_t)) +
144 	    sizeof(uint16_t);
145 	size = (size + align - 1) & ~(align - 1);
146 	size += sizeof(struct vring_used) +
147 	    (num * sizeof(struct vring_used_elem)) + sizeof(uint16_t);
148 	return (size);
149 }
150 
151 static inline void
152 vring_init(struct vring *vr, unsigned int num, uint8_t *p, vm_paddr_t paddr,
153     unsigned long align)
154 {
155 	unsigned long avail_offset;
156 	unsigned long used_offset;
157 
158 	avail_offset = num * sizeof(struct vring_desc);
159 	used_offset = (avail_offset + sizeof(struct vring_avail) +
160 	    sizeof(uint16_t) * num + align - 1) & ~(align - 1);
161 
162 	vr->num = num;
163 	vr->desc = (struct vring_desc *) p;
164 	vr->avail = (struct vring_avail *) (p + avail_offset);
165 	vr->used = (struct vring_used *) (p + used_offset);
166 
167 	vr->desc_paddr = paddr;
168 	vr->avail_paddr = paddr + avail_offset;
169 	vr->used_paddr = paddr + used_offset;
170 }
171 
172 /*
173  * The following is used with VIRTIO_RING_F_EVENT_IDX.
174  *
175  * Assuming a given event_idx value from the other size, if we have
176  * just incremented index from old to new_idx, should we trigger an
177  * event?
178  */
179 static inline int
180 vring_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
181 {
182 
183 	return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
184 }
185 #endif /* VIRTIO_RING_H */
186