xref: /openbmc/qemu/include/hw/virtio/virtio.h (revision 4921d0a7)
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 "exec/memory.h"
18 #include "hw/qdev-core.h"
19 #include "net/net.h"
20 #include "migration/vmstate.h"
21 #include "qemu/event_notifier.h"
22 #include "standard-headers/linux/virtio_config.h"
23 #include "standard-headers/linux/virtio_ring.h"
24 #include "qom/object.h"
25 #include "block/aio.h"
26 
27 /*
28  * A guest should never accept this. It implies negotiation is broken
29  * between the driver frontend and the device. This bit is re-used for
30  * vhost-user to advertise VHOST_USER_F_PROTOCOL_FEATURES between QEMU
31  * and a vhost-user backend.
32  */
33 #define VIRTIO_F_BAD_FEATURE 30
34 
35 #define VIRTIO_LEGACY_FEATURES ((0x1ULL << VIRTIO_F_BAD_FEATURE) | \
36                                 (0x1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \
37                                 (0x1ULL << VIRTIO_F_ANY_LAYOUT))
38 
39 struct VirtQueue;
40 
41 static inline hwaddr vring_align(hwaddr addr,
42                                              unsigned long align)
43 {
44     return QEMU_ALIGN_UP(addr, align);
45 }
46 
47 typedef struct VirtIOFeature {
48     uint64_t flags;
49     size_t end;
50 } VirtIOFeature;
51 
52 typedef struct VirtIOConfigSizeParams {
53     size_t min_size;
54     size_t max_size;
55     const VirtIOFeature *feature_sizes;
56 } VirtIOConfigSizeParams;
57 
58 size_t virtio_get_config_size(const VirtIOConfigSizeParams *params,
59                               uint64_t host_features);
60 
61 typedef struct VirtQueue VirtQueue;
62 
63 #define VIRTQUEUE_MAX_SIZE 1024
64 
65 typedef struct VirtQueueElement
66 {
67     unsigned int index;
68     unsigned int len;
69     unsigned int ndescs;
70     unsigned int out_num;
71     unsigned int in_num;
72     hwaddr *in_addr;
73     hwaddr *out_addr;
74     struct iovec *in_sg;
75     struct iovec *out_sg;
76 } VirtQueueElement;
77 
78 #define VIRTIO_QUEUE_MAX 1024
79 
80 #define VIRTIO_NO_VECTOR 0xffff
81 
82 /* special index value used internally for config irqs */
83 #define VIRTIO_CONFIG_IRQ_IDX -1
84 
85 #define TYPE_VIRTIO_DEVICE "virtio-device"
86 OBJECT_DECLARE_TYPE(VirtIODevice, VirtioDeviceClass, VIRTIO_DEVICE)
87 
88 typedef struct {
89     int virtio_bit;
90     const char *feature_desc;
91 } qmp_virtio_feature_map_t;
92 
93 enum virtio_device_endian {
94     VIRTIO_DEVICE_ENDIAN_UNKNOWN,
95     VIRTIO_DEVICE_ENDIAN_LITTLE,
96     VIRTIO_DEVICE_ENDIAN_BIG,
97 };
98 
99 /**
100  * struct VirtIODevice - common VirtIO structure
101  * @name: name of the device
102  * @status: VirtIO Device Status field
103  *
104  */
105 struct VirtIODevice
106 {
107     DeviceState parent_obj;
108     const char *name;
109     uint8_t status;
110     uint8_t isr;
111     uint16_t queue_sel;
112     /**
113      * These fields represent a set of VirtIO features at various
114      * levels of the stack. @host_features indicates the complete
115      * feature set the VirtIO device can offer to the driver.
116      * @guest_features indicates which features the VirtIO driver has
117      * selected by writing to the feature register. Finally
118      * @backend_features represents everything supported by the
119      * backend (e.g. vhost) and could potentially be a subset of the
120      * total feature set offered by QEMU.
121      */
122     uint64_t host_features;
123     uint64_t guest_features;
124     uint64_t backend_features;
125 
126     size_t config_len;
127     void *config;
128     uint16_t config_vector;
129     uint32_t generation;
130     int nvectors;
131     VirtQueue *vq;
132     MemoryListener listener;
133     uint16_t device_id;
134     /* @vm_running: current VM running state via virtio_vmstate_change() */
135     bool vm_running;
136     bool broken; /* device in invalid state, needs reset */
137     bool use_disabled_flag; /* allow use of 'disable' flag when needed */
138     bool disabled; /* device in temporarily disabled state */
139     /**
140      * @use_started: true if the @started flag should be used to check the
141      * current state of the VirtIO device. Otherwise status bits
142      * should be checked for a current status of the device.
143      * @use_started is only set via QMP and defaults to true for all
144      * modern machines (since 4.1).
145      */
146     bool use_started;
147     bool started;
148     bool start_on_kick; /* when virtio 1.0 feature has not been negotiated */
149     bool disable_legacy_check;
150     bool vhost_started;
151     VMChangeStateEntry *vmstate;
152     char *bus_name;
153     uint8_t device_endian;
154     /**
155      * @user_guest_notifier_mask: gate usage of ->guest_notifier_mask() callback.
156      * This is used to suppress the masking of guest updates for
157      * vhost-user devices which are asynchronous by design.
158      */
159     bool use_guest_notifier_mask;
160     AddressSpace *dma_as;
161     QLIST_HEAD(, VirtQueue) *vector_queues;
162     QTAILQ_ENTRY(VirtIODevice) next;
163     /**
164      * @config_notifier: the event notifier that handles config events
165      */
166     EventNotifier config_notifier;
167     bool device_iotlb_enabled;
168 };
169 
170 struct VirtioDeviceClass {
171     /*< private >*/
172     DeviceClass parent;
173     /*< public >*/
174 
175     /* This is what a VirtioDevice must implement */
176     DeviceRealize realize;
177     DeviceUnrealize unrealize;
178     uint64_t (*get_features)(VirtIODevice *vdev,
179                              uint64_t requested_features,
180                              Error **errp);
181     uint64_t (*bad_features)(VirtIODevice *vdev);
182     void (*set_features)(VirtIODevice *vdev, uint64_t val);
183     int (*validate_features)(VirtIODevice *vdev);
184     void (*get_config)(VirtIODevice *vdev, uint8_t *config);
185     void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
186     void (*reset)(VirtIODevice *vdev);
187     void (*set_status)(VirtIODevice *vdev, uint8_t val);
188     /* Device must validate queue_index.  */
189     void (*queue_reset)(VirtIODevice *vdev, uint32_t queue_index);
190     /* Device must validate queue_index.  */
191     void (*queue_enable)(VirtIODevice *vdev, uint32_t queue_index);
192     /* For transitional devices, this is a bitmap of features
193      * that are only exposed on the legacy interface but not
194      * the modern one.
195      */
196     uint64_t legacy_features;
197     /* Test and clear event pending status.
198      * Should be called after unmask to avoid losing events.
199      * If backend does not support masking,
200      * must check in frontend instead.
201      */
202     bool (*guest_notifier_pending)(VirtIODevice *vdev, int n);
203     /* Mask/unmask events from this vq. Any events reported
204      * while masked will become pending.
205      * If backend does not support masking,
206      * must mask in frontend instead.
207      */
208     void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask);
209     int (*start_ioeventfd)(VirtIODevice *vdev);
210     void (*stop_ioeventfd)(VirtIODevice *vdev);
211     /* Saving and loading of a device; trying to deprecate save/load
212      * use vmsd for new devices.
213      */
214     void (*save)(VirtIODevice *vdev, QEMUFile *f);
215     int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
216     /* Post load hook in vmsd is called early while device is processed, and
217      * when VirtIODevice isn't fully initialized.  Devices should use this instead,
218      * unless they specifically want to verify the migration stream as it's
219      * processed, e.g. for bounds checking.
220      */
221     int (*post_load)(VirtIODevice *vdev);
222     const VMStateDescription *vmsd;
223     bool (*primary_unplug_pending)(void *opaque);
224     struct vhost_dev *(*get_vhost)(VirtIODevice *vdev);
225     void (*toggle_device_iotlb)(VirtIODevice *vdev);
226 };
227 
228 void virtio_instance_init_common(Object *proxy_obj, void *data,
229                                  size_t vdev_size, const char *vdev_name);
230 
231 /**
232  * virtio_init() - initialise the common VirtIODevice structure
233  * @vdev: pointer to VirtIODevice
234  * @device_id: the VirtIO device ID (see virtio_ids.h)
235  * @config_size: size of the config space
236  */
237 void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size);
238 
239 void virtio_cleanup(VirtIODevice *vdev);
240 
241 void virtio_error(VirtIODevice *vdev, const char *fmt, ...) G_GNUC_PRINTF(2, 3);
242 
243 /* Set the child bus name. */
244 void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name);
245 
246 typedef void (*VirtIOHandleOutput)(VirtIODevice *, VirtQueue *);
247 
248 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
249                             VirtIOHandleOutput handle_output);
250 
251 void virtio_del_queue(VirtIODevice *vdev, int n);
252 
253 void virtio_delete_queue(VirtQueue *vq);
254 
255 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
256                     unsigned int len);
257 void virtqueue_flush(VirtQueue *vq, unsigned int count);
258 void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
259                               unsigned int len);
260 void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem,
261                      unsigned int len);
262 bool virtqueue_rewind(VirtQueue *vq, unsigned int num);
263 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
264                     unsigned int len, unsigned int idx);
265 
266 void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem);
267 void *virtqueue_pop(VirtQueue *vq, size_t sz);
268 unsigned int virtqueue_drop_all(VirtQueue *vq);
269 void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz);
270 void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f,
271                                 VirtQueueElement *elem);
272 int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
273                           unsigned int out_bytes);
274 void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
275                                unsigned int *out_bytes,
276                                unsigned max_in_bytes, unsigned max_out_bytes);
277 
278 void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq);
279 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
280 
281 int virtio_save(VirtIODevice *vdev, QEMUFile *f);
282 
283 extern const VMStateInfo virtio_vmstate_info;
284 
285 #define VMSTATE_VIRTIO_DEVICE \
286     {                                         \
287         .name = "virtio",                     \
288         .info = &virtio_vmstate_info,         \
289         .flags = VMS_SINGLE,                  \
290     }
291 
292 int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id);
293 
294 /**
295  * virtio_notify_config() - signal a change to device config
296  * @vdev: the virtio device
297  *
298  * Assuming the virtio device is up (VIRTIO_CONFIG_S_DRIVER_OK) this
299  * will trigger a guest interrupt and update the config version.
300  */
301 void virtio_notify_config(VirtIODevice *vdev);
302 
303 bool virtio_queue_get_notification(VirtQueue *vq);
304 void virtio_queue_set_notification(VirtQueue *vq, int enable);
305 
306 int virtio_queue_ready(VirtQueue *vq);
307 
308 int virtio_queue_empty(VirtQueue *vq);
309 
310 /* Host binding interface.  */
311 
312 uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
313 uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
314 uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
315 void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
316 void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
317 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
318 uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr);
319 uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr);
320 uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr);
321 void virtio_config_modern_writeb(VirtIODevice *vdev,
322                                  uint32_t addr, uint32_t data);
323 void virtio_config_modern_writew(VirtIODevice *vdev,
324                                  uint32_t addr, uint32_t data);
325 void virtio_config_modern_writel(VirtIODevice *vdev,
326                                  uint32_t addr, uint32_t data);
327 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
328 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
329 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num);
330 int virtio_queue_get_num(VirtIODevice *vdev, int n);
331 int virtio_queue_get_max_num(VirtIODevice *vdev, int n);
332 int virtio_get_num_queues(VirtIODevice *vdev);
333 void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
334                             hwaddr avail, hwaddr used);
335 void virtio_queue_update_rings(VirtIODevice *vdev, int n);
336 void virtio_init_region_cache(VirtIODevice *vdev, int n);
337 void virtio_queue_set_align(VirtIODevice *vdev, int n, int align);
338 void virtio_queue_notify(VirtIODevice *vdev, int n);
339 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
340 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
341 int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n,
342                                       MemoryRegion *mr, bool assign);
343 int virtio_set_status(VirtIODevice *vdev, uint8_t val);
344 void virtio_reset(void *opaque);
345 void virtio_queue_reset(VirtIODevice *vdev, uint32_t queue_index);
346 void virtio_queue_enable(VirtIODevice *vdev, uint32_t queue_index);
347 void virtio_update_irq(VirtIODevice *vdev);
348 int virtio_set_features(VirtIODevice *vdev, uint64_t val);
349 
350 /* Base devices.  */
351 typedef struct VirtIOBlkConf VirtIOBlkConf;
352 struct virtio_net_conf;
353 typedef struct virtio_serial_conf virtio_serial_conf;
354 typedef struct virtio_input_conf virtio_input_conf;
355 typedef struct VirtIOSCSIConf VirtIOSCSIConf;
356 typedef struct VirtIORNGConf VirtIORNGConf;
357 
358 #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
359     DEFINE_PROP_BIT64("indirect_desc", _state, _field,    \
360                       VIRTIO_RING_F_INDIRECT_DESC, true), \
361     DEFINE_PROP_BIT64("event_idx", _state, _field,        \
362                       VIRTIO_RING_F_EVENT_IDX, true),     \
363     DEFINE_PROP_BIT64("notify_on_empty", _state, _field,  \
364                       VIRTIO_F_NOTIFY_ON_EMPTY, true), \
365     DEFINE_PROP_BIT64("any_layout", _state, _field, \
366                       VIRTIO_F_ANY_LAYOUT, true), \
367     DEFINE_PROP_BIT64("iommu_platform", _state, _field, \
368                       VIRTIO_F_IOMMU_PLATFORM, false), \
369     DEFINE_PROP_BIT64("packed", _state, _field, \
370                       VIRTIO_F_RING_PACKED, false), \
371     DEFINE_PROP_BIT64("queue_reset", _state, _field, \
372                       VIRTIO_F_RING_RESET, true)
373 
374 hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n);
375 bool virtio_queue_enabled_legacy(VirtIODevice *vdev, int n);
376 bool virtio_queue_enabled(VirtIODevice *vdev, int n);
377 hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n);
378 hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n);
379 hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n);
380 hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n);
381 hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n);
382 unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n);
383 void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n,
384                                      unsigned int idx);
385 void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n);
386 void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n);
387 void virtio_queue_update_used_idx(VirtIODevice *vdev, int n);
388 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
389 uint16_t virtio_get_queue_index(VirtQueue *vq);
390 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
391 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
392                                                 bool with_irqfd);
393 int virtio_device_start_ioeventfd(VirtIODevice *vdev);
394 int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
395 void virtio_device_release_ioeventfd(VirtIODevice *vdev);
396 bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev);
397 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
398 void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled);
399 void virtio_queue_host_notifier_read(EventNotifier *n);
400 void virtio_queue_aio_attach_host_notifier(VirtQueue *vq, AioContext *ctx);
401 void virtio_queue_aio_attach_host_notifier_no_poll(VirtQueue *vq, AioContext *ctx);
402 void virtio_queue_aio_detach_host_notifier(VirtQueue *vq, AioContext *ctx);
403 VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector);
404 VirtQueue *virtio_vector_next_queue(VirtQueue *vq);
405 EventNotifier *virtio_config_get_guest_notifier(VirtIODevice *vdev);
406 void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev,
407                                                  bool assign, bool with_irqfd);
408 
409 static inline void virtio_add_feature(uint64_t *features, unsigned int fbit)
410 {
411     assert(fbit < 64);
412     *features |= (1ULL << fbit);
413 }
414 
415 static inline void virtio_clear_feature(uint64_t *features, unsigned int fbit)
416 {
417     assert(fbit < 64);
418     *features &= ~(1ULL << fbit);
419 }
420 
421 static inline bool virtio_has_feature(uint64_t features, unsigned int fbit)
422 {
423     assert(fbit < 64);
424     return !!(features & (1ULL << fbit));
425 }
426 
427 static inline bool virtio_vdev_has_feature(const VirtIODevice *vdev,
428                                            unsigned int fbit)
429 {
430     return virtio_has_feature(vdev->guest_features, fbit);
431 }
432 
433 static inline bool virtio_host_has_feature(VirtIODevice *vdev,
434                                            unsigned int fbit)
435 {
436     return virtio_has_feature(vdev->host_features, fbit);
437 }
438 
439 static inline bool virtio_is_big_endian(VirtIODevice *vdev)
440 {
441     if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
442         assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
443         return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
444     }
445     /* Devices conforming to VIRTIO 1.0 or later are always LE. */
446     return false;
447 }
448 
449 /**
450  * virtio_device_started() - check if device started
451  * @vdev - the VirtIO device
452  * @status - the devices status bits
453  *
454  * Check if the device is started. For most modern machines this is
455  * tracked via the @vdev->started field (to support migration),
456  * otherwise we check for the final negotiated status bit that
457  * indicates everything is ready.
458  */
459 static inline bool virtio_device_started(VirtIODevice *vdev, uint8_t status)
460 {
461     if (vdev->use_started) {
462         return vdev->started;
463     }
464 
465     return status & VIRTIO_CONFIG_S_DRIVER_OK;
466 }
467 
468 /**
469  * virtio_device_should_start() - check if device startable
470  * @vdev - the VirtIO device
471  * @status - the devices status bits
472  *
473  * This is similar to virtio_device_started() but also encapsulates a
474  * check on the VM status which would prevent a device starting
475  * anyway.
476  */
477 static inline bool virtio_device_should_start(VirtIODevice *vdev, uint8_t status)
478 {
479     if (!vdev->vm_running) {
480         return false;
481     }
482 
483     return virtio_device_started(vdev, status);
484 }
485 
486 static inline void virtio_set_started(VirtIODevice *vdev, bool started)
487 {
488     if (started) {
489         vdev->start_on_kick = false;
490     }
491 
492     if (vdev->use_started) {
493         vdev->started = started;
494     }
495 }
496 
497 static inline void virtio_set_disabled(VirtIODevice *vdev, bool disable)
498 {
499     if (vdev->use_disabled_flag) {
500         vdev->disabled = disable;
501     }
502 }
503 
504 static inline bool virtio_device_disabled(VirtIODevice *vdev)
505 {
506     return unlikely(vdev->disabled || vdev->broken);
507 }
508 
509 bool virtio_legacy_allowed(VirtIODevice *vdev);
510 bool virtio_legacy_check_disabled(VirtIODevice *vdev);
511 
512 QEMUBH *virtio_bh_new_guarded_full(DeviceState *dev,
513                                    QEMUBHFunc *cb, void *opaque,
514                                    const char *name);
515 #define virtio_bh_new_guarded(dev, cb, opaque) \
516     virtio_bh_new_guarded_full((dev), (cb), (opaque), (stringify(cb)))
517 
518 #endif
519