xref: /openbmc/qemu/include/hw/virtio/virtio.h (revision fa59483d)
1 /*
2  * Virtio Support
3  *
4  * Copyright IBM, Corp. 2007
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 #ifndef QEMU_VIRTIO_H
15 #define QEMU_VIRTIO_H
16 
17 #include "hw/hw.h"
18 #include "net/net.h"
19 #include "hw/qdev.h"
20 #include "sysemu/sysemu.h"
21 #include "qemu/event_notifier.h"
22 #include "standard-headers/linux/virtio_config.h"
23 #include "standard-headers/linux/virtio_ring.h"
24 
25 /* A guest should never accept this.  It implies negotiation is broken. */
26 #define VIRTIO_F_BAD_FEATURE		30
27 
28 #define VIRTIO_LEGACY_FEATURES ((0x1ULL << VIRTIO_F_BAD_FEATURE) | \
29                                 (0x1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \
30                                 (0x1ULL << VIRTIO_F_ANY_LAYOUT))
31 
32 struct VirtQueue;
33 
34 static inline hwaddr vring_align(hwaddr addr,
35                                              unsigned long align)
36 {
37     return QEMU_ALIGN_UP(addr, align);
38 }
39 
40 /*
41  * Calculate the number of bytes up to and including the given 'field' of
42  * 'container'.
43  */
44 #define virtio_endof(container, field) \
45     (offsetof(container, field) + sizeof_field(container, field))
46 
47 typedef struct VirtIOFeature {
48     uint64_t flags;
49     size_t end;
50 } VirtIOFeature;
51 
52 size_t virtio_feature_get_config_size(VirtIOFeature *features,
53                                       uint64_t host_features);
54 
55 typedef struct VirtQueue VirtQueue;
56 
57 #define VIRTQUEUE_MAX_SIZE 1024
58 
59 typedef struct VirtQueueElement
60 {
61     unsigned int index;
62     unsigned int out_num;
63     unsigned int in_num;
64     hwaddr *in_addr;
65     hwaddr *out_addr;
66     struct iovec *in_sg;
67     struct iovec *out_sg;
68 } VirtQueueElement;
69 
70 #define VIRTIO_QUEUE_MAX 1024
71 
72 #define VIRTIO_NO_VECTOR 0xffff
73 
74 #define TYPE_VIRTIO_DEVICE "virtio-device"
75 #define VIRTIO_DEVICE_GET_CLASS(obj) \
76         OBJECT_GET_CLASS(VirtioDeviceClass, obj, TYPE_VIRTIO_DEVICE)
77 #define VIRTIO_DEVICE_CLASS(klass) \
78         OBJECT_CLASS_CHECK(VirtioDeviceClass, klass, TYPE_VIRTIO_DEVICE)
79 #define VIRTIO_DEVICE(obj) \
80         OBJECT_CHECK(VirtIODevice, (obj), TYPE_VIRTIO_DEVICE)
81 
82 enum virtio_device_endian {
83     VIRTIO_DEVICE_ENDIAN_UNKNOWN,
84     VIRTIO_DEVICE_ENDIAN_LITTLE,
85     VIRTIO_DEVICE_ENDIAN_BIG,
86 };
87 
88 struct VirtIODevice
89 {
90     DeviceState parent_obj;
91     const char *name;
92     uint8_t status;
93     uint8_t isr;
94     uint16_t queue_sel;
95     uint64_t guest_features;
96     uint64_t host_features;
97     uint64_t backend_features;
98     size_t config_len;
99     void *config;
100     uint16_t config_vector;
101     uint32_t generation;
102     int nvectors;
103     VirtQueue *vq;
104     MemoryListener listener;
105     uint16_t device_id;
106     bool vm_running;
107     bool broken; /* device in invalid state, needs reset */
108     VMChangeStateEntry *vmstate;
109     char *bus_name;
110     uint8_t device_endian;
111     bool use_guest_notifier_mask;
112     AddressSpace *dma_as;
113     QLIST_HEAD(, VirtQueue) *vector_queues;
114 };
115 
116 typedef struct VirtioDeviceClass {
117     /*< private >*/
118     DeviceClass parent;
119     /*< public >*/
120 
121     /* This is what a VirtioDevice must implement */
122     DeviceRealize realize;
123     DeviceUnrealize unrealize;
124     uint64_t (*get_features)(VirtIODevice *vdev,
125                              uint64_t requested_features,
126                              Error **errp);
127     uint64_t (*bad_features)(VirtIODevice *vdev);
128     void (*set_features)(VirtIODevice *vdev, uint64_t val);
129     int (*validate_features)(VirtIODevice *vdev);
130     void (*get_config)(VirtIODevice *vdev, uint8_t *config);
131     void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
132     void (*reset)(VirtIODevice *vdev);
133     void (*set_status)(VirtIODevice *vdev, uint8_t val);
134     /* For transitional devices, this is a bitmap of features
135      * that are only exposed on the legacy interface but not
136      * the modern one.
137      */
138     uint64_t legacy_features;
139     /* Test and clear event pending status.
140      * Should be called after unmask to avoid losing events.
141      * If backend does not support masking,
142      * must check in frontend instead.
143      */
144     bool (*guest_notifier_pending)(VirtIODevice *vdev, int n);
145     /* Mask/unmask events from this vq. Any events reported
146      * while masked will become pending.
147      * If backend does not support masking,
148      * must mask in frontend instead.
149      */
150     void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask);
151     int (*start_ioeventfd)(VirtIODevice *vdev);
152     void (*stop_ioeventfd)(VirtIODevice *vdev);
153     /* Saving and loading of a device; trying to deprecate save/load
154      * use vmsd for new devices.
155      */
156     void (*save)(VirtIODevice *vdev, QEMUFile *f);
157     int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
158     const VMStateDescription *vmsd;
159 } VirtioDeviceClass;
160 
161 void virtio_instance_init_common(Object *proxy_obj, void *data,
162                                  size_t vdev_size, const char *vdev_name);
163 
164 void virtio_init(VirtIODevice *vdev, const char *name,
165                          uint16_t device_id, size_t config_size);
166 void virtio_cleanup(VirtIODevice *vdev);
167 
168 void virtio_error(VirtIODevice *vdev, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
169 
170 /* Set the child bus name. */
171 void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name);
172 
173 typedef void (*VirtIOHandleOutput)(VirtIODevice *, VirtQueue *);
174 typedef bool (*VirtIOHandleAIOOutput)(VirtIODevice *, VirtQueue *);
175 
176 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
177                             VirtIOHandleOutput handle_output);
178 
179 void virtio_del_queue(VirtIODevice *vdev, int n);
180 
181 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
182                     unsigned int len);
183 void virtqueue_flush(VirtQueue *vq, unsigned int count);
184 void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
185                               unsigned int len);
186 void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem,
187                      unsigned int len);
188 bool virtqueue_rewind(VirtQueue *vq, unsigned int num);
189 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
190                     unsigned int len, unsigned int idx);
191 
192 void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem);
193 void *virtqueue_pop(VirtQueue *vq, size_t sz);
194 unsigned int virtqueue_drop_all(VirtQueue *vq);
195 void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz);
196 void qemu_put_virtqueue_element(QEMUFile *f, VirtQueueElement *elem);
197 int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
198                           unsigned int out_bytes);
199 void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
200                                unsigned int *out_bytes,
201                                unsigned max_in_bytes, unsigned max_out_bytes);
202 
203 void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq);
204 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
205 
206 int virtio_save(VirtIODevice *vdev, QEMUFile *f);
207 
208 extern const VMStateInfo virtio_vmstate_info;
209 
210 #define VMSTATE_VIRTIO_DEVICE \
211     {                                         \
212         .name = "virtio",                     \
213         .info = &virtio_vmstate_info,         \
214         .flags = VMS_SINGLE,                  \
215     }
216 
217 int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id);
218 
219 void virtio_notify_config(VirtIODevice *vdev);
220 
221 void virtio_queue_set_notification(VirtQueue *vq, int enable);
222 
223 int virtio_queue_ready(VirtQueue *vq);
224 
225 int virtio_queue_empty(VirtQueue *vq);
226 
227 /* Host binding interface.  */
228 
229 uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
230 uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
231 uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
232 void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
233 void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
234 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
235 uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr);
236 uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr);
237 uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr);
238 void virtio_config_modern_writeb(VirtIODevice *vdev,
239                                  uint32_t addr, uint32_t data);
240 void virtio_config_modern_writew(VirtIODevice *vdev,
241                                  uint32_t addr, uint32_t data);
242 void virtio_config_modern_writel(VirtIODevice *vdev,
243                                  uint32_t addr, uint32_t data);
244 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
245 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
246 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num);
247 int virtio_queue_get_num(VirtIODevice *vdev, int n);
248 int virtio_queue_get_max_num(VirtIODevice *vdev, int n);
249 int virtio_get_num_queues(VirtIODevice *vdev);
250 void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
251                             hwaddr avail, hwaddr used);
252 void virtio_queue_update_rings(VirtIODevice *vdev, int n);
253 void virtio_queue_set_align(VirtIODevice *vdev, int n, int align);
254 void virtio_queue_notify(VirtIODevice *vdev, int n);
255 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
256 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
257 int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n,
258                                       MemoryRegion *mr, bool assign);
259 int virtio_set_status(VirtIODevice *vdev, uint8_t val);
260 void virtio_reset(void *opaque);
261 void virtio_update_irq(VirtIODevice *vdev);
262 int virtio_set_features(VirtIODevice *vdev, uint64_t val);
263 
264 /* Base devices.  */
265 typedef struct VirtIOBlkConf VirtIOBlkConf;
266 struct virtio_net_conf;
267 typedef struct virtio_serial_conf virtio_serial_conf;
268 typedef struct virtio_input_conf virtio_input_conf;
269 typedef struct VirtIOSCSIConf VirtIOSCSIConf;
270 typedef struct VirtIORNGConf VirtIORNGConf;
271 
272 #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
273     DEFINE_PROP_BIT64("indirect_desc", _state, _field,    \
274                       VIRTIO_RING_F_INDIRECT_DESC, true), \
275     DEFINE_PROP_BIT64("event_idx", _state, _field,        \
276                       VIRTIO_RING_F_EVENT_IDX, true),     \
277     DEFINE_PROP_BIT64("notify_on_empty", _state, _field,  \
278                       VIRTIO_F_NOTIFY_ON_EMPTY, true), \
279     DEFINE_PROP_BIT64("any_layout", _state, _field, \
280                       VIRTIO_F_ANY_LAYOUT, true), \
281     DEFINE_PROP_BIT64("iommu_platform", _state, _field, \
282                       VIRTIO_F_IOMMU_PLATFORM, false)
283 
284 hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n);
285 hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n);
286 hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n);
287 hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n);
288 hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n);
289 hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n);
290 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n);
291 void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx);
292 void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n);
293 void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n);
294 void virtio_queue_update_used_idx(VirtIODevice *vdev, int n);
295 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
296 uint16_t virtio_get_queue_index(VirtQueue *vq);
297 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
298 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
299                                                 bool with_irqfd);
300 int virtio_device_start_ioeventfd(VirtIODevice *vdev);
301 void virtio_device_stop_ioeventfd(VirtIODevice *vdev);
302 int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
303 void virtio_device_release_ioeventfd(VirtIODevice *vdev);
304 bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev);
305 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
306 void virtio_queue_host_notifier_read(EventNotifier *n);
307 void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
308                                                 VirtIOHandleAIOOutput handle_output);
309 VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector);
310 VirtQueue *virtio_vector_next_queue(VirtQueue *vq);
311 
312 static inline void virtio_add_feature(uint64_t *features, unsigned int fbit)
313 {
314     assert(fbit < 64);
315     *features |= (1ULL << fbit);
316 }
317 
318 static inline void virtio_clear_feature(uint64_t *features, unsigned int fbit)
319 {
320     assert(fbit < 64);
321     *features &= ~(1ULL << fbit);
322 }
323 
324 static inline bool virtio_has_feature(uint64_t features, unsigned int fbit)
325 {
326     assert(fbit < 64);
327     return !!(features & (1ULL << fbit));
328 }
329 
330 static inline bool virtio_vdev_has_feature(VirtIODevice *vdev,
331                                            unsigned int fbit)
332 {
333     return virtio_has_feature(vdev->guest_features, fbit);
334 }
335 
336 static inline bool virtio_host_has_feature(VirtIODevice *vdev,
337                                            unsigned int fbit)
338 {
339     return virtio_has_feature(vdev->host_features, fbit);
340 }
341 
342 static inline bool virtio_is_big_endian(VirtIODevice *vdev)
343 {
344     if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
345         assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
346         return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
347     }
348     /* Devices conforming to VIRTIO 1.0 or later are always LE. */
349     return false;
350 }
351 #endif
352