xref: /openbmc/qemu/include/hw/virtio/virtio.h (revision 701bff24)
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 "hw/virtio/vhost.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 #define TYPE_VIRTIO_DEVICE "virtio-device"
83 OBJECT_DECLARE_TYPE(VirtIODevice, VirtioDeviceClass, VIRTIO_DEVICE)
84 
85 typedef struct {
86     int virtio_bit;
87     const char *feature_desc;
88 } qmp_virtio_feature_map_t;
89 
90 enum virtio_device_endian {
91     VIRTIO_DEVICE_ENDIAN_UNKNOWN,
92     VIRTIO_DEVICE_ENDIAN_LITTLE,
93     VIRTIO_DEVICE_ENDIAN_BIG,
94 };
95 
96 struct VirtIODevice
97 {
98     DeviceState parent_obj;
99     const char *name;
100     uint8_t status;
101     uint8_t isr;
102     uint16_t queue_sel;
103     uint64_t guest_features;
104     uint64_t host_features;
105     uint64_t backend_features;
106     size_t config_len;
107     void *config;
108     uint16_t config_vector;
109     uint32_t generation;
110     int nvectors;
111     VirtQueue *vq;
112     MemoryListener listener;
113     uint16_t device_id;
114     /* @vm_running: current VM running state via virtio_vmstate_change() */
115     bool vm_running;
116     bool broken; /* device in invalid state, needs reset */
117     bool use_disabled_flag; /* allow use of 'disable' flag when needed */
118     bool disabled; /* device in temporarily disabled state */
119     bool use_started;
120     bool started;
121     bool start_on_kick; /* when virtio 1.0 feature has not been negotiated */
122     bool disable_legacy_check;
123     bool vhost_started;
124     VMChangeStateEntry *vmstate;
125     char *bus_name;
126     uint8_t device_endian;
127     bool use_guest_notifier_mask;
128     AddressSpace *dma_as;
129     QLIST_HEAD(, VirtQueue) *vector_queues;
130     QTAILQ_ENTRY(VirtIODevice) next;
131 };
132 
133 struct VirtioDeviceClass {
134     /*< private >*/
135     DeviceClass parent;
136     /*< public >*/
137 
138     /* This is what a VirtioDevice must implement */
139     DeviceRealize realize;
140     DeviceUnrealize unrealize;
141     uint64_t (*get_features)(VirtIODevice *vdev,
142                              uint64_t requested_features,
143                              Error **errp);
144     uint64_t (*bad_features)(VirtIODevice *vdev);
145     void (*set_features)(VirtIODevice *vdev, uint64_t val);
146     int (*validate_features)(VirtIODevice *vdev);
147     void (*get_config)(VirtIODevice *vdev, uint8_t *config);
148     void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
149     void (*reset)(VirtIODevice *vdev);
150     void (*set_status)(VirtIODevice *vdev, uint8_t val);
151     /* For transitional devices, this is a bitmap of features
152      * that are only exposed on the legacy interface but not
153      * the modern one.
154      */
155     uint64_t legacy_features;
156     /* Test and clear event pending status.
157      * Should be called after unmask to avoid losing events.
158      * If backend does not support masking,
159      * must check in frontend instead.
160      */
161     bool (*guest_notifier_pending)(VirtIODevice *vdev, int n);
162     /* Mask/unmask events from this vq. Any events reported
163      * while masked will become pending.
164      * If backend does not support masking,
165      * must mask in frontend instead.
166      */
167     void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask);
168     int (*start_ioeventfd)(VirtIODevice *vdev);
169     void (*stop_ioeventfd)(VirtIODevice *vdev);
170     /* Saving and loading of a device; trying to deprecate save/load
171      * use vmsd for new devices.
172      */
173     void (*save)(VirtIODevice *vdev, QEMUFile *f);
174     int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
175     /* Post load hook in vmsd is called early while device is processed, and
176      * when VirtIODevice isn't fully initialized.  Devices should use this instead,
177      * unless they specifically want to verify the migration stream as it's
178      * processed, e.g. for bounds checking.
179      */
180     int (*post_load)(VirtIODevice *vdev);
181     const VMStateDescription *vmsd;
182     bool (*primary_unplug_pending)(void *opaque);
183     struct vhost_dev *(*get_vhost)(VirtIODevice *vdev);
184 };
185 
186 void virtio_instance_init_common(Object *proxy_obj, void *data,
187                                  size_t vdev_size, const char *vdev_name);
188 
189 void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size);
190 
191 void virtio_cleanup(VirtIODevice *vdev);
192 
193 void virtio_error(VirtIODevice *vdev, const char *fmt, ...) G_GNUC_PRINTF(2, 3);
194 
195 /* Set the child bus name. */
196 void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name);
197 
198 typedef void (*VirtIOHandleOutput)(VirtIODevice *, VirtQueue *);
199 
200 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
201                             VirtIOHandleOutput handle_output);
202 
203 void virtio_del_queue(VirtIODevice *vdev, int n);
204 
205 void virtio_delete_queue(VirtQueue *vq);
206 
207 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
208                     unsigned int len);
209 void virtqueue_flush(VirtQueue *vq, unsigned int count);
210 void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
211                               unsigned int len);
212 void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem,
213                      unsigned int len);
214 bool virtqueue_rewind(VirtQueue *vq, unsigned int num);
215 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
216                     unsigned int len, unsigned int idx);
217 
218 void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem);
219 void *virtqueue_pop(VirtQueue *vq, size_t sz);
220 unsigned int virtqueue_drop_all(VirtQueue *vq);
221 void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz);
222 void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f,
223                                 VirtQueueElement *elem);
224 int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
225                           unsigned int out_bytes);
226 void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
227                                unsigned int *out_bytes,
228                                unsigned max_in_bytes, unsigned max_out_bytes);
229 
230 void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq);
231 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
232 
233 int virtio_save(VirtIODevice *vdev, QEMUFile *f);
234 
235 extern const VMStateInfo virtio_vmstate_info;
236 
237 #define VMSTATE_VIRTIO_DEVICE \
238     {                                         \
239         .name = "virtio",                     \
240         .info = &virtio_vmstate_info,         \
241         .flags = VMS_SINGLE,                  \
242     }
243 
244 int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id);
245 
246 void virtio_notify_config(VirtIODevice *vdev);
247 
248 bool virtio_queue_get_notification(VirtQueue *vq);
249 void virtio_queue_set_notification(VirtQueue *vq, int enable);
250 
251 int virtio_queue_ready(VirtQueue *vq);
252 
253 int virtio_queue_empty(VirtQueue *vq);
254 
255 /* Host binding interface.  */
256 
257 uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
258 uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
259 uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
260 void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
261 void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
262 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
263 uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr);
264 uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr);
265 uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr);
266 void virtio_config_modern_writeb(VirtIODevice *vdev,
267                                  uint32_t addr, uint32_t data);
268 void virtio_config_modern_writew(VirtIODevice *vdev,
269                                  uint32_t addr, uint32_t data);
270 void virtio_config_modern_writel(VirtIODevice *vdev,
271                                  uint32_t addr, uint32_t data);
272 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
273 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
274 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num);
275 int virtio_queue_get_num(VirtIODevice *vdev, int n);
276 int virtio_queue_get_max_num(VirtIODevice *vdev, int n);
277 int virtio_get_num_queues(VirtIODevice *vdev);
278 void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
279                             hwaddr avail, hwaddr used);
280 void virtio_queue_update_rings(VirtIODevice *vdev, int n);
281 void virtio_queue_set_align(VirtIODevice *vdev, int n, int align);
282 void virtio_queue_notify(VirtIODevice *vdev, int n);
283 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
284 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
285 int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n,
286                                       MemoryRegion *mr, bool assign);
287 int virtio_set_status(VirtIODevice *vdev, uint8_t val);
288 void virtio_reset(void *opaque);
289 void virtio_update_irq(VirtIODevice *vdev);
290 int virtio_set_features(VirtIODevice *vdev, uint64_t val);
291 
292 /* Base devices.  */
293 typedef struct VirtIOBlkConf VirtIOBlkConf;
294 struct virtio_net_conf;
295 typedef struct virtio_serial_conf virtio_serial_conf;
296 typedef struct virtio_input_conf virtio_input_conf;
297 typedef struct VirtIOSCSIConf VirtIOSCSIConf;
298 typedef struct VirtIORNGConf VirtIORNGConf;
299 
300 #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
301     DEFINE_PROP_BIT64("indirect_desc", _state, _field,    \
302                       VIRTIO_RING_F_INDIRECT_DESC, true), \
303     DEFINE_PROP_BIT64("event_idx", _state, _field,        \
304                       VIRTIO_RING_F_EVENT_IDX, true),     \
305     DEFINE_PROP_BIT64("notify_on_empty", _state, _field,  \
306                       VIRTIO_F_NOTIFY_ON_EMPTY, true), \
307     DEFINE_PROP_BIT64("any_layout", _state, _field, \
308                       VIRTIO_F_ANY_LAYOUT, true), \
309     DEFINE_PROP_BIT64("iommu_platform", _state, _field, \
310                       VIRTIO_F_IOMMU_PLATFORM, false), \
311     DEFINE_PROP_BIT64("packed", _state, _field, \
312                       VIRTIO_F_RING_PACKED, false)
313 
314 hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n);
315 bool virtio_queue_enabled_legacy(VirtIODevice *vdev, int n);
316 bool virtio_queue_enabled(VirtIODevice *vdev, int n);
317 hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n);
318 hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n);
319 hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n);
320 hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n);
321 hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n);
322 unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n);
323 void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n,
324                                      unsigned int idx);
325 void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n);
326 void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n);
327 void virtio_queue_update_used_idx(VirtIODevice *vdev, int n);
328 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
329 uint16_t virtio_get_queue_index(VirtQueue *vq);
330 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
331 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
332                                                 bool with_irqfd);
333 int virtio_device_start_ioeventfd(VirtIODevice *vdev);
334 int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
335 void virtio_device_release_ioeventfd(VirtIODevice *vdev);
336 bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev);
337 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
338 void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled);
339 void virtio_queue_host_notifier_read(EventNotifier *n);
340 void virtio_queue_aio_attach_host_notifier(VirtQueue *vq, AioContext *ctx);
341 void virtio_queue_aio_attach_host_notifier_no_poll(VirtQueue *vq, AioContext *ctx);
342 void virtio_queue_aio_detach_host_notifier(VirtQueue *vq, AioContext *ctx);
343 VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector);
344 VirtQueue *virtio_vector_next_queue(VirtQueue *vq);
345 
346 static inline void virtio_add_feature(uint64_t *features, unsigned int fbit)
347 {
348     assert(fbit < 64);
349     *features |= (1ULL << fbit);
350 }
351 
352 static inline void virtio_clear_feature(uint64_t *features, unsigned int fbit)
353 {
354     assert(fbit < 64);
355     *features &= ~(1ULL << fbit);
356 }
357 
358 static inline bool virtio_has_feature(uint64_t features, unsigned int fbit)
359 {
360     assert(fbit < 64);
361     return !!(features & (1ULL << fbit));
362 }
363 
364 static inline bool virtio_vdev_has_feature(VirtIODevice *vdev,
365                                            unsigned int fbit)
366 {
367     return virtio_has_feature(vdev->guest_features, fbit);
368 }
369 
370 static inline bool virtio_host_has_feature(VirtIODevice *vdev,
371                                            unsigned int fbit)
372 {
373     return virtio_has_feature(vdev->host_features, fbit);
374 }
375 
376 static inline bool virtio_is_big_endian(VirtIODevice *vdev)
377 {
378     if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
379         assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
380         return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
381     }
382     /* Devices conforming to VIRTIO 1.0 or later are always LE. */
383     return false;
384 }
385 
386 static inline bool virtio_device_started(VirtIODevice *vdev, uint8_t status)
387 {
388     if (vdev->use_started) {
389         return vdev->started;
390     }
391 
392     if (!vdev->vm_running) {
393         return false;
394     }
395 
396     return status & VIRTIO_CONFIG_S_DRIVER_OK;
397 }
398 
399 static inline void virtio_set_started(VirtIODevice *vdev, bool started)
400 {
401     if (started) {
402         vdev->start_on_kick = false;
403     }
404 
405     if (vdev->use_started) {
406         vdev->started = started;
407     }
408 }
409 
410 static inline void virtio_set_disabled(VirtIODevice *vdev, bool disable)
411 {
412     if (vdev->use_disabled_flag) {
413         vdev->disabled = disable;
414     }
415 }
416 
417 static inline bool virtio_device_disabled(VirtIODevice *vdev)
418 {
419     return unlikely(vdev->disabled || vdev->broken);
420 }
421 
422 bool virtio_legacy_allowed(VirtIODevice *vdev);
423 bool virtio_legacy_check_disabled(VirtIODevice *vdev);
424 
425 #endif
426