1 /*
2  * Copyright Rusty Russell IBM Corporation 2007.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * $FreeBSD$
7  */
8 
9 #ifndef VIRTIO_RING_H
10 #define	VIRTIO_RING_H
11 
12 #include <metal/compiler.h>
13 
14 #if defined __cplusplus
15 extern "C" {
16 #endif
17 
18 /* This marks a buffer as continuing via the next field. */
19 #define VRING_DESC_F_NEXT       1
20 /* This marks a buffer as write-only (otherwise read-only). */
21 #define VRING_DESC_F_WRITE      2
22 /* This means the buffer contains a list of buffer descriptors. */
23 #define VRING_DESC_F_INDIRECT   4
24 
25 /* The Host uses this in used->flags to advise the Guest: don't kick me
26  * when you add a buffer.  It's unreliable, so it's simply an
27  * optimization.  Guest will still kick if it's out of buffers.
28  */
29 #define VRING_USED_F_NO_NOTIFY  1
30 /* The Guest uses this in avail->flags to advise the Host: don't
31  * interrupt me when you consume a buffer.  It's unreliable, so it's
32  * simply an optimization.
33  */
34 #define VRING_AVAIL_F_NO_INTERRUPT      1
35 
36 /**
37  * @brief VirtIO ring descriptors.
38  *
39  * The descriptor table refers to the buffers the driver is using for the
40  * device. addr is a physical address, and the buffers can be chained via \ref next.
41  * Each descriptor describes a buffer which is read-only for the device
42  * (“device-readable”) or write-only for the device (“device-writable”), but a
43  * chain of descriptors can contain both device-readable and device-writable
44  * buffers.
45  */
46 METAL_PACKED_BEGIN
47 struct vring_desc {
48 	/** Address (guest-physical) */
49 	uint64_t addr;
50 
51 	/** Length */
52 	uint32_t len;
53 
54 	/** Flags relevant to the descriptors */
55 	uint16_t flags;
56 
57 	/** We chain unused descriptors via this, too */
58 	uint16_t next;
59 } METAL_PACKED_END;
60 
61 /**
62  * @brief Used to offer buffers to the device.
63  *
64  * Each ring entry refers to the head of a descriptor chain. It is only
65  * written by the driver and read by the device.
66  */
67 METAL_PACKED_BEGIN
68 struct vring_avail {
69 	/** Flag which determines whether device notifications are required */
70 	uint16_t flags;
71 
72 	/**
73 	 * Indicates where the driver puts the next descriptor entry in the
74 	 * ring (modulo the queue size)
75 	 */
76 	uint16_t idx;
77 
78 	/** The ring of descriptors */
79 	uint16_t ring[0];
80 } METAL_PACKED_END;
81 
82 /* uint32_t is used here for ids for padding reasons. */
83 METAL_PACKED_BEGIN
84 struct vring_used_elem {
85 	union {
86 		uint16_t event;
87 		/* Index of start of used descriptor chain. */
88 		uint32_t id;
89 	};
90 	/* Total length of the descriptor chain which was written to. */
91 	uint32_t len;
92 } METAL_PACKED_END;
93 
94 /**
95  * @brief The device returns buffers to this structure when done with them
96  *
97  * The structure is only written to by the device, and read by the driver.
98  */
99 METAL_PACKED_BEGIN
100 struct vring_used {
101 	/** Flag which determines whether device notifications are required */
102 	uint16_t flags;
103 
104 	/**
105 	 * Indicates where the driver puts the next descriptor entry in the
106 	 * ring (modulo the queue size)
107 	 */
108 	uint16_t idx;
109 
110 	/** The ring of descriptors */
111 	struct vring_used_elem ring[0];
112 } METAL_PACKED_END;
113 
114 /**
115  * @brief The virtqueue layout structure
116  *
117  * Each virtqueue consists of; descriptor table, available ring, used ring,
118  * where each part is physically contiguous in guest memory.
119  *
120  * When the driver wants to send a buffer to the device, it fills in a slot in
121  * the descriptor table (or chains several together), and writes the descriptor
122  * index into the available ring. It then notifies the device. When the device
123  * has finished a buffer, it writes the descriptor index into the used ring,
124  * and sends an interrupt.
125  *
126  * The standard layout for the ring is a continuous chunk of memory which
127  * looks like this.  We assume num is a power of 2.
128  *
129  * struct vring {
130  *      // The actual descriptors (16 bytes each)
131  *      struct vring_desc desc[num];
132  *
133  *      // A ring of available descriptor heads with free-running index.
134  *      __u16 avail_flags;
135  *      __u16 avail_idx;
136  *      __u16 available[num];
137  *      __u16 used_event_idx;
138  *
139  *      // Padding to the next align boundary.
140  *      char pad[];
141  *
142  *      // A ring of used descriptor heads with free-running index.
143  *      __u16 used_flags;
144  *      __u16 used_idx;
145  *      struct vring_used_elem used[num];
146  *      __u16 avail_event_idx;
147  * };
148  *
149  * NOTE: for VirtIO PCI, align is 4096.
150  */
151 struct vring {
152 	/**
153 	 * The maximum number of buffer descriptors in the virtqueue.
154 	 * The value is always a power of 2.
155 	 */
156 	unsigned int num;
157 
158 	/** The actual buffer descriptors, 16 bytes each */
159 	struct vring_desc *desc;
160 
161 	/** A ring of available descriptor heads with free-running index */
162 	struct vring_avail *avail;
163 
164 	/** A ring of used descriptor heads with free-running index */
165 	struct vring_used *used;
166 };
167 
168 /*
169  * We publish the used event index at the end of the available ring, and vice
170  * versa. They are at the end for backwards compatibility.
171  */
172 #define vring_used_event(vr)	((vr)->avail->ring[(vr)->num])
173 #define vring_avail_event(vr)	((vr)->used->ring[(vr)->num].event)
174 
vring_size(unsigned int num,unsigned long align)175 static inline int vring_size(unsigned int num, unsigned long align)
176 {
177 	int size;
178 
179 	size = num * sizeof(struct vring_desc);
180 	size += sizeof(struct vring_avail) + (num * sizeof(uint16_t)) +
181 	    sizeof(uint16_t);
182 	size = (size + align - 1) & ~(align - 1);
183 	size += sizeof(struct vring_used) +
184 	    (num * sizeof(struct vring_used_elem)) + sizeof(uint16_t);
185 
186 	return size;
187 }
188 
189 static inline void
vring_init(struct vring * vr,unsigned int num,uint8_t * p,unsigned long align)190 vring_init(struct vring *vr, unsigned int num, uint8_t *p, unsigned long align)
191 {
192 	vr->num = num;
193 	vr->desc = (struct vring_desc *)p;
194 	vr->avail = (struct vring_avail *)(p + num * sizeof(struct vring_desc));
195 	vr->used = (struct vring_used *)
196 	    (((unsigned long)&vr->avail->ring[num] + sizeof(uint16_t) +
197 	      align - 1) & ~(align - 1));
198 }
199 
200 /*
201  * The following is used with VIRTIO_RING_F_EVENT_IDX.
202  *
203  * Assuming a given event_idx value from the other size, if we have
204  * just incremented index from old to new_idx, should we trigger an
205  * event?
206  */
207 static inline int
vring_need_event(uint16_t event_idx,uint16_t new_idx,uint16_t old)208 vring_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
209 {
210 	return (uint16_t)(new_idx - event_idx - 1) <
211 	    (uint16_t)(new_idx - old);
212 }
213 
214 #if defined __cplusplus
215 }
216 #endif
217 
218 #endif				/* VIRTIO_RING_H */
219