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