1
2
3
4
5
6
7
8
9
10
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
29
30
31
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
39struct VirtQueue;
40
41static inline hwaddr vring_align(hwaddr addr,
42 unsigned long align)
43{
44 return QEMU_ALIGN_UP(addr, align);
45}
46
47typedef struct VirtIOFeature {
48 uint64_t flags;
49 size_t end;
50} VirtIOFeature;
51
52typedef struct VirtIOConfigSizeParams {
53 size_t min_size;
54 size_t max_size;
55 const VirtIOFeature *feature_sizes;
56} VirtIOConfigSizeParams;
57
58size_t virtio_get_config_size(const VirtIOConfigSizeParams *params,
59 uint64_t host_features);
60
61typedef struct VirtQueue VirtQueue;
62
63#define VIRTQUEUE_MAX_SIZE 1024
64
65typedef 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"
83OBJECT_DECLARE_TYPE(VirtIODevice, VirtioDeviceClass, VIRTIO_DEVICE)
84
85typedef struct {
86 int virtio_bit;
87 const char *feature_desc;
88} qmp_virtio_feature_map_t;
89
90enum virtio_device_endian {
91 VIRTIO_DEVICE_ENDIAN_UNKNOWN,
92 VIRTIO_DEVICE_ENDIAN_LITTLE,
93 VIRTIO_DEVICE_ENDIAN_BIG,
94};
95
96struct 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
115 bool vm_running;
116 bool broken;
117 bool use_disabled_flag;
118 bool disabled;
119
120
121
122
123
124
125
126 bool use_started;
127 bool started;
128 bool start_on_kick;
129 bool disable_legacy_check;
130 bool vhost_started;
131 VMChangeStateEntry *vmstate;
132 char *bus_name;
133 uint8_t device_endian;
134 bool use_guest_notifier_mask;
135 AddressSpace *dma_as;
136 QLIST_HEAD(, VirtQueue) *vector_queues;
137 QTAILQ_ENTRY(VirtIODevice) next;
138 bool device_iotlb_enabled;
139};
140
141struct VirtioDeviceClass {
142
143 DeviceClass parent;
144
145
146
147 DeviceRealize realize;
148 DeviceUnrealize unrealize;
149 uint64_t (*get_features)(VirtIODevice *vdev,
150 uint64_t requested_features,
151 Error **errp);
152 uint64_t (*bad_features)(VirtIODevice *vdev);
153 void (*set_features)(VirtIODevice *vdev, uint64_t val);
154 int (*validate_features)(VirtIODevice *vdev);
155 void (*get_config)(VirtIODevice *vdev, uint8_t *config);
156 void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
157 void (*reset)(VirtIODevice *vdev);
158 void (*set_status)(VirtIODevice *vdev, uint8_t val);
159
160 void (*queue_reset)(VirtIODevice *vdev, uint32_t queue_index);
161
162 void (*queue_enable)(VirtIODevice *vdev, uint32_t queue_index);
163
164
165
166
167 uint64_t legacy_features;
168
169
170
171
172
173 bool (*guest_notifier_pending)(VirtIODevice *vdev, int n);
174
175
176
177
178
179 void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask);
180 int (*start_ioeventfd)(VirtIODevice *vdev);
181 void (*stop_ioeventfd)(VirtIODevice *vdev);
182
183
184
185 void (*save)(VirtIODevice *vdev, QEMUFile *f);
186 int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
187
188
189
190
191
192 int (*post_load)(VirtIODevice *vdev);
193 const VMStateDescription *vmsd;
194 bool (*primary_unplug_pending)(void *opaque);
195 struct vhost_dev *(*get_vhost)(VirtIODevice *vdev);
196 void (*toggle_device_iotlb)(VirtIODevice *vdev);
197};
198
199void virtio_instance_init_common(Object *proxy_obj, void *data,
200 size_t vdev_size, const char *vdev_name);
201
202void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size);
203
204void virtio_cleanup(VirtIODevice *vdev);
205
206void virtio_error(VirtIODevice *vdev, const char *fmt, ...) G_GNUC_PRINTF(2, 3);
207
208
209void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name);
210
211typedef void (*VirtIOHandleOutput)(VirtIODevice *, VirtQueue *);
212
213VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
214 VirtIOHandleOutput handle_output);
215
216void virtio_del_queue(VirtIODevice *vdev, int n);
217
218void virtio_delete_queue(VirtQueue *vq);
219
220void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
221 unsigned int len);
222void virtqueue_flush(VirtQueue *vq, unsigned int count);
223void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
224 unsigned int len);
225void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem,
226 unsigned int len);
227bool virtqueue_rewind(VirtQueue *vq, unsigned int num);
228void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
229 unsigned int len, unsigned int idx);
230
231void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem);
232void *virtqueue_pop(VirtQueue *vq, size_t sz);
233unsigned int virtqueue_drop_all(VirtQueue *vq);
234void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz);
235void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f,
236 VirtQueueElement *elem);
237int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
238 unsigned int out_bytes);
239void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
240 unsigned int *out_bytes,
241 unsigned max_in_bytes, unsigned max_out_bytes);
242
243void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq);
244void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
245
246int virtio_save(VirtIODevice *vdev, QEMUFile *f);
247
248extern const VMStateInfo virtio_vmstate_info;
249
250#define VMSTATE_VIRTIO_DEVICE \
251 { \
252 .name = "virtio", \
253 .info = &virtio_vmstate_info, \
254 .flags = VMS_SINGLE, \
255 }
256
257int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id);
258
259void virtio_notify_config(VirtIODevice *vdev);
260
261bool virtio_queue_get_notification(VirtQueue *vq);
262void virtio_queue_set_notification(VirtQueue *vq, int enable);
263
264int virtio_queue_ready(VirtQueue *vq);
265
266int virtio_queue_empty(VirtQueue *vq);
267
268
269
270uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
271uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
272uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
273void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
274void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
275void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
276uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr);
277uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr);
278uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr);
279void virtio_config_modern_writeb(VirtIODevice *vdev,
280 uint32_t addr, uint32_t data);
281void virtio_config_modern_writew(VirtIODevice *vdev,
282 uint32_t addr, uint32_t data);
283void virtio_config_modern_writel(VirtIODevice *vdev,
284 uint32_t addr, uint32_t data);
285void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
286hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
287void virtio_queue_set_num(VirtIODevice *vdev, int n, int num);
288int virtio_queue_get_num(VirtIODevice *vdev, int n);
289int virtio_queue_get_max_num(VirtIODevice *vdev, int n);
290int virtio_get_num_queues(VirtIODevice *vdev);
291void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
292 hwaddr avail, hwaddr used);
293void virtio_queue_update_rings(VirtIODevice *vdev, int n);
294void virtio_queue_set_align(VirtIODevice *vdev, int n, int align);
295void virtio_queue_notify(VirtIODevice *vdev, int n);
296uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
297void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
298int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n,
299 MemoryRegion *mr, bool assign);
300int virtio_set_status(VirtIODevice *vdev, uint8_t val);
301void virtio_reset(void *opaque);
302void virtio_queue_reset(VirtIODevice *vdev, uint32_t queue_index);
303void virtio_queue_enable(VirtIODevice *vdev, uint32_t queue_index);
304void virtio_update_irq(VirtIODevice *vdev);
305int virtio_set_features(VirtIODevice *vdev, uint64_t val);
306
307
308typedef struct VirtIOBlkConf VirtIOBlkConf;
309struct virtio_net_conf;
310typedef struct virtio_serial_conf virtio_serial_conf;
311typedef struct virtio_input_conf virtio_input_conf;
312typedef struct VirtIOSCSIConf VirtIOSCSIConf;
313typedef struct VirtIORNGConf VirtIORNGConf;
314
315#define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
316 DEFINE_PROP_BIT64("indirect_desc", _state, _field, \
317 VIRTIO_RING_F_INDIRECT_DESC, true), \
318 DEFINE_PROP_BIT64("event_idx", _state, _field, \
319 VIRTIO_RING_F_EVENT_IDX, true), \
320 DEFINE_PROP_BIT64("notify_on_empty", _state, _field, \
321 VIRTIO_F_NOTIFY_ON_EMPTY, true), \
322 DEFINE_PROP_BIT64("any_layout", _state, _field, \
323 VIRTIO_F_ANY_LAYOUT, true), \
324 DEFINE_PROP_BIT64("iommu_platform", _state, _field, \
325 VIRTIO_F_IOMMU_PLATFORM, false), \
326 DEFINE_PROP_BIT64("packed", _state, _field, \
327 VIRTIO_F_RING_PACKED, false), \
328 DEFINE_PROP_BIT64("queue_reset", _state, _field, \
329 VIRTIO_F_RING_RESET, true)
330
331hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n);
332bool virtio_queue_enabled_legacy(VirtIODevice *vdev, int n);
333bool virtio_queue_enabled(VirtIODevice *vdev, int n);
334hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n);
335hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n);
336hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n);
337hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n);
338hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n);
339unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n);
340void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n,
341 unsigned int idx);
342void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n);
343void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n);
344void virtio_queue_update_used_idx(VirtIODevice *vdev, int n);
345VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
346uint16_t virtio_get_queue_index(VirtQueue *vq);
347EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
348void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
349 bool with_irqfd);
350int virtio_device_start_ioeventfd(VirtIODevice *vdev);
351int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
352void virtio_device_release_ioeventfd(VirtIODevice *vdev);
353bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev);
354EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
355void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled);
356void virtio_queue_host_notifier_read(EventNotifier *n);
357void virtio_queue_aio_attach_host_notifier(VirtQueue *vq, AioContext *ctx);
358void virtio_queue_aio_attach_host_notifier_no_poll(VirtQueue *vq, AioContext *ctx);
359void virtio_queue_aio_detach_host_notifier(VirtQueue *vq, AioContext *ctx);
360VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector);
361VirtQueue *virtio_vector_next_queue(VirtQueue *vq);
362
363static inline void virtio_add_feature(uint64_t *features, unsigned int fbit)
364{
365 assert(fbit < 64);
366 *features |= (1ULL << fbit);
367}
368
369static inline void virtio_clear_feature(uint64_t *features, unsigned int fbit)
370{
371 assert(fbit < 64);
372 *features &= ~(1ULL << fbit);
373}
374
375static inline bool virtio_has_feature(uint64_t features, unsigned int fbit)
376{
377 assert(fbit < 64);
378 return !!(features & (1ULL << fbit));
379}
380
381static inline bool virtio_vdev_has_feature(VirtIODevice *vdev,
382 unsigned int fbit)
383{
384 return virtio_has_feature(vdev->guest_features, fbit);
385}
386
387static inline bool virtio_host_has_feature(VirtIODevice *vdev,
388 unsigned int fbit)
389{
390 return virtio_has_feature(vdev->host_features, fbit);
391}
392
393static inline bool virtio_is_big_endian(VirtIODevice *vdev)
394{
395 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
396 assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
397 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
398 }
399
400 return false;
401}
402
403
404
405
406
407
408
409
410
411
412
413static inline bool virtio_device_started(VirtIODevice *vdev, uint8_t status)
414{
415 if (vdev->use_started) {
416 return vdev->started;
417 }
418
419 return status & VIRTIO_CONFIG_S_DRIVER_OK;
420}
421
422
423
424
425
426
427
428
429
430
431static inline bool virtio_device_should_start(VirtIODevice *vdev, uint8_t status)
432{
433 if (!vdev->vm_running) {
434 return false;
435 }
436
437 return virtio_device_started(vdev, status);
438}
439
440static inline void virtio_set_started(VirtIODevice *vdev, bool started)
441{
442 if (started) {
443 vdev->start_on_kick = false;
444 }
445
446 if (vdev->use_started) {
447 vdev->started = started;
448 }
449}
450
451static inline void virtio_set_disabled(VirtIODevice *vdev, bool disable)
452{
453 if (vdev->use_disabled_flag) {
454 vdev->disabled = disable;
455 }
456}
457
458static inline bool virtio_device_disabled(VirtIODevice *vdev)
459{
460 return unlikely(vdev->disabled || vdev->broken);
461}
462
463bool virtio_legacy_allowed(VirtIODevice *vdev);
464bool virtio_legacy_check_disabled(VirtIODevice *vdev);
465
466#endif
467