]>
Commit | Line | Data |
---|---|---|
967f97fa AL |
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 | ||
2a6a4076 MA |
14 | #ifndef QEMU_VIRTIO_H |
15 | #define QEMU_VIRTIO_H | |
967f97fa | 16 | |
d4842052 | 17 | #include "exec/memory.h" |
a27bd6c7 | 18 | #include "hw/qdev-core.h" |
1422e32d | 19 | #include "net/net.h" |
d6454270 | 20 | #include "migration/vmstate.h" |
1de7afc9 | 21 | #include "qemu/event_notifier.h" |
e9600c6c MT |
22 | #include "standard-headers/linux/virtio_config.h" |
23 | #include "standard-headers/linux/virtio_ring.h" | |
db1015e9 | 24 | #include "qom/object.h" |
967f97fa | 25 | |
ea5d6ea7 AB |
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 | */ | |
48805df9 | 32 | #define VIRTIO_F_BAD_FEATURE 30 |
967f97fa | 33 | |
5f456073 MT |
34 | #define VIRTIO_LEGACY_FEATURES ((0x1ULL << VIRTIO_F_BAD_FEATURE) | \ |
35 | (0x1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \ | |
36 | (0x1ULL << VIRTIO_F_ANY_LAYOUT)) | |
37 | ||
967f97fa AL |
38 | struct VirtQueue; |
39 | ||
a8170e5e | 40 | static inline hwaddr vring_align(hwaddr addr, |
f46f15bc AL |
41 | unsigned long align) |
42 | { | |
b8adbc65 | 43 | return QEMU_ALIGN_UP(addr, align); |
f46f15bc AL |
44 | } |
45 | ||
ba550851 SG |
46 | typedef struct VirtIOFeature { |
47 | uint64_t flags; | |
48 | size_t end; | |
49 | } VirtIOFeature; | |
50 | ||
d74c30c8 DT |
51 | typedef struct VirtIOConfigSizeParams { |
52 | size_t min_size; | |
53 | size_t max_size; | |
54 | const VirtIOFeature *feature_sizes; | |
55 | } VirtIOConfigSizeParams; | |
56 | ||
57 | size_t virtio_get_config_size(const VirtIOConfigSizeParams *params, | |
58 | uint64_t host_features); | |
ba550851 | 59 | |
967f97fa | 60 | typedef struct VirtQueue VirtQueue; |
967f97fa AL |
61 | |
62 | #define VIRTQUEUE_MAX_SIZE 1024 | |
63 | ||
64 | typedef struct VirtQueueElement | |
65 | { | |
66 | unsigned int index; | |
86044b24 JW |
67 | unsigned int len; |
68 | unsigned int ndescs; | |
967f97fa AL |
69 | unsigned int out_num; |
70 | unsigned int in_num; | |
3724650d PB |
71 | hwaddr *in_addr; |
72 | hwaddr *out_addr; | |
73 | struct iovec *in_sg; | |
74 | struct iovec *out_sg; | |
967f97fa AL |
75 | } VirtQueueElement; |
76 | ||
b829c2a9 | 77 | #define VIRTIO_QUEUE_MAX 1024 |
967f97fa | 78 | |
7055e687 MT |
79 | #define VIRTIO_NO_VECTOR 0xffff |
80 | ||
544f0278 CL |
81 | /* special index value used internally for config irqs */ |
82 | #define VIRTIO_CONFIG_IRQ_IDX -1 | |
83 | ||
8e05db92 | 84 | #define TYPE_VIRTIO_DEVICE "virtio-device" |
a489d195 | 85 | OBJECT_DECLARE_TYPE(VirtIODevice, VirtioDeviceClass, VIRTIO_DEVICE) |
8e05db92 | 86 | |
f3034ad7 LV |
87 | typedef struct { |
88 | int virtio_bit; | |
89 | const char *feature_desc; | |
90 | } qmp_virtio_feature_map_t; | |
91 | ||
616a6552 GK |
92 | enum virtio_device_endian { |
93 | VIRTIO_DEVICE_ENDIAN_UNKNOWN, | |
94 | VIRTIO_DEVICE_ENDIAN_LITTLE, | |
95 | VIRTIO_DEVICE_ENDIAN_BIG, | |
96 | }; | |
97 | ||
9600c98e AB |
98 | /** |
99 | * struct VirtIODevice - common VirtIO structure | |
100 | * @name: name of the device | |
101 | * @status: VirtIO Device Status field | |
102 | * | |
103 | */ | |
967f97fa AL |
104 | struct VirtIODevice |
105 | { | |
8e05db92 | 106 | DeviceState parent_obj; |
967f97fa | 107 | const char *name; |
967f97fa AL |
108 | uint8_t status; |
109 | uint8_t isr; | |
110 | uint16_t queue_sel; | |
9600c98e AB |
111 | /** |
112 | * These fields represent a set of VirtIO features at various | |
113 | * levels of the stack. @host_features indicates the complete | |
114 | * feature set the VirtIO device can offer to the driver. | |
115 | * @guest_features indicates which features the VirtIO driver has | |
116 | * selected by writing to the feature register. Finally | |
117 | * @backend_features represents everything supported by the | |
118 | * backend (e.g. vhost) and could potentially be a subset of the | |
119 | * total feature set offered by QEMU. | |
120 | */ | |
019a3edb | 121 | uint64_t host_features; |
9600c98e | 122 | uint64_t guest_features; |
75ebec11 | 123 | uint64_t backend_features; |
9600c98e | 124 | |
967f97fa AL |
125 | size_t config_len; |
126 | void *config; | |
7055e687 | 127 | uint16_t config_vector; |
b8f05908 | 128 | uint32_t generation; |
7055e687 | 129 | int nvectors; |
967f97fa | 130 | VirtQueue *vq; |
c611c764 | 131 | MemoryListener listener; |
53c25cea | 132 | uint16_t device_id; |
9f6bcfd9 | 133 | /* @vm_running: current VM running state via virtio_vmstate_change() */ |
85cf2a8d | 134 | bool vm_running; |
f5ed3663 | 135 | bool broken; /* device in invalid state, needs reset */ |
9d7bd082 MR |
136 | bool use_disabled_flag; /* allow use of 'disable' flag when needed */ |
137 | bool disabled; /* device in temporarily disabled state */ | |
4987e5bf AB |
138 | /** |
139 | * @use_started: true if the @started flag should be used to check the | |
140 | * current state of the VirtIO device. Otherwise status bits | |
141 | * should be checked for a current status of the device. | |
142 | * @use_started is only set via QMP and defaults to true for all | |
143 | * modern machines (since 4.1). | |
144 | */ | |
e57f2c31 | 145 | bool use_started; |
badaf79c | 146 | bool started; |
7abccd08 | 147 | bool start_on_kick; /* when virtio 1.0 feature has not been negotiated */ |
d55f5182 | 148 | bool disable_legacy_check; |
c255488d | 149 | bool vhost_started; |
85cf2a8d | 150 | VMChangeStateEntry *vmstate; |
1034e9cf | 151 | char *bus_name; |
616a6552 | 152 | uint8_t device_endian; |
5669655a | 153 | bool use_guest_notifier_mask; |
8607f5c3 | 154 | AddressSpace *dma_as; |
850d0070 | 155 | QLIST_HEAD(, VirtQueue) *vector_queues; |
a5ebce38 | 156 | QTAILQ_ENTRY(VirtIODevice) next; |
7d847d0c | 157 | EventNotifier config_notifier; |
206e91d1 | 158 | bool device_iotlb_enabled; |
967f97fa AL |
159 | }; |
160 | ||
db1015e9 | 161 | struct VirtioDeviceClass { |
0ba94b6f | 162 | /*< private >*/ |
8e05db92 | 163 | DeviceClass parent; |
0ba94b6f | 164 | /*< public >*/ |
1d244b42 AF |
165 | |
166 | /* This is what a VirtioDevice must implement */ | |
1d244b42 | 167 | DeviceRealize realize; |
306ec6c3 | 168 | DeviceUnrealize unrealize; |
9d5b731d JW |
169 | uint64_t (*get_features)(VirtIODevice *vdev, |
170 | uint64_t requested_features, | |
171 | Error **errp); | |
019a3edb | 172 | uint64_t (*bad_features)(VirtIODevice *vdev); |
d5aaa1b0 | 173 | void (*set_features)(VirtIODevice *vdev, uint64_t val); |
0b352fd6 | 174 | int (*validate_features)(VirtIODevice *vdev); |
8e05db92 FK |
175 | void (*get_config)(VirtIODevice *vdev, uint8_t *config); |
176 | void (*set_config)(VirtIODevice *vdev, const uint8_t *config); | |
177 | void (*reset)(VirtIODevice *vdev); | |
178 | void (*set_status)(VirtIODevice *vdev, uint8_t val); | |
f47af0af | 179 | /* Device must validate queue_index. */ |
b3a8d6f4 | 180 | void (*queue_reset)(VirtIODevice *vdev, uint32_t queue_index); |
f47af0af | 181 | /* Device must validate queue_index. */ |
3c37f8b8 | 182 | void (*queue_enable)(VirtIODevice *vdev, uint32_t queue_index); |
9b706dbb MT |
183 | /* For transitional devices, this is a bitmap of features |
184 | * that are only exposed on the legacy interface but not | |
185 | * the modern one. | |
186 | */ | |
187 | uint64_t legacy_features; | |
6a87acf7 FK |
188 | /* Test and clear event pending status. |
189 | * Should be called after unmask to avoid losing events. | |
190 | * If backend does not support masking, | |
191 | * must check in frontend instead. | |
192 | */ | |
193 | bool (*guest_notifier_pending)(VirtIODevice *vdev, int n); | |
194 | /* Mask/unmask events from this vq. Any events reported | |
195 | * while masked will become pending. | |
196 | * If backend does not support masking, | |
197 | * must mask in frontend instead. | |
198 | */ | |
199 | void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask); | |
ff4c07df PB |
200 | int (*start_ioeventfd)(VirtIODevice *vdev); |
201 | void (*stop_ioeventfd)(VirtIODevice *vdev); | |
ea43e259 DDAG |
202 | /* Saving and loading of a device; trying to deprecate save/load |
203 | * use vmsd for new devices. | |
204 | */ | |
1b5fc0de GK |
205 | void (*save)(VirtIODevice *vdev, QEMUFile *f); |
206 | int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id); | |
1dd71383 MT |
207 | /* Post load hook in vmsd is called early while device is processed, and |
208 | * when VirtIODevice isn't fully initialized. Devices should use this instead, | |
209 | * unless they specifically want to verify the migration stream as it's | |
210 | * processed, e.g. for bounds checking. | |
211 | */ | |
212 | int (*post_load)(VirtIODevice *vdev); | |
ea43e259 | 213 | const VMStateDescription *vmsd; |
9711cd0d | 214 | bool (*primary_unplug_pending)(void *opaque); |
c255488d | 215 | struct vhost_dev *(*get_vhost)(VirtIODevice *vdev); |
206e91d1 | 216 | void (*toggle_device_iotlb)(VirtIODevice *vdev); |
db1015e9 | 217 | }; |
8e05db92 | 218 | |
c8075caf GA |
219 | void virtio_instance_init_common(Object *proxy_obj, void *data, |
220 | size_t vdev_size, const char *vdev_name); | |
221 | ||
3857cd5c JP |
222 | void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size); |
223 | ||
6a1a8cc7 | 224 | void virtio_cleanup(VirtIODevice *vdev); |
8e05db92 | 225 | |
9edc6313 | 226 | void virtio_error(VirtIODevice *vdev, const char *fmt, ...) G_GNUC_PRINTF(2, 3); |
f5ed3663 | 227 | |
1034e9cf FK |
228 | /* Set the child bus name. */ |
229 | void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name); | |
230 | ||
bf1780b0 FZ |
231 | typedef void (*VirtIOHandleOutput)(VirtIODevice *, VirtQueue *); |
232 | ||
967f97fa | 233 | VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size, |
bf1780b0 | 234 | VirtIOHandleOutput handle_output); |
967f97fa | 235 | |
f23fd811 JW |
236 | void virtio_del_queue(VirtIODevice *vdev, int n); |
237 | ||
722f8c51 MT |
238 | void virtio_delete_queue(VirtQueue *vq); |
239 | ||
967f97fa AL |
240 | void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, |
241 | unsigned int len); | |
242 | void virtqueue_flush(VirtQueue *vq, unsigned int count); | |
2640d2a5 SH |
243 | void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem, |
244 | unsigned int len); | |
27e57efe LP |
245 | void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem, |
246 | unsigned int len); | |
297a75e6 | 247 | bool virtqueue_rewind(VirtQueue *vq, unsigned int num); |
967f97fa AL |
248 | void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, |
249 | unsigned int len, unsigned int idx); | |
250 | ||
8607f5c3 | 251 | void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem); |
51b19ebe | 252 | void *virtqueue_pop(VirtQueue *vq, size_t sz); |
54e17709 | 253 | unsigned int virtqueue_drop_all(VirtQueue *vq); |
8607f5c3 | 254 | void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz); |
86044b24 JW |
255 | void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, |
256 | VirtQueueElement *elem); | |
0d8d7690 AS |
257 | int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, |
258 | unsigned int out_bytes); | |
259 | void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, | |
e1f7b481 MT |
260 | unsigned int *out_bytes, |
261 | unsigned max_in_bytes, unsigned max_out_bytes); | |
967f97fa | 262 | |
83d768b5 | 263 | void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq); |
967f97fa AL |
264 | void virtio_notify(VirtIODevice *vdev, VirtQueue *vq); |
265 | ||
2f168d07 | 266 | int virtio_save(VirtIODevice *vdev, QEMUFile *f); |
5943124c | 267 | |
1a665855 HP |
268 | extern const VMStateInfo virtio_vmstate_info; |
269 | ||
1a665855 HP |
270 | #define VMSTATE_VIRTIO_DEVICE \ |
271 | { \ | |
272 | .name = "virtio", \ | |
273 | .info = &virtio_vmstate_info, \ | |
274 | .flags = VMS_SINGLE, \ | |
275 | } | |
276 | ||
1b5fc0de | 277 | int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id); |
967f97fa AL |
278 | |
279 | void virtio_notify_config(VirtIODevice *vdev); | |
280 | ||
d0435bc5 | 281 | bool virtio_queue_get_notification(VirtQueue *vq); |
967f97fa AL |
282 | void virtio_queue_set_notification(VirtQueue *vq, int enable); |
283 | ||
284 | int virtio_queue_ready(VirtQueue *vq); | |
285 | ||
286 | int virtio_queue_empty(VirtQueue *vq); | |
287 | ||
53c25cea PB |
288 | /* Host binding interface. */ |
289 | ||
53c25cea PB |
290 | uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr); |
291 | uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr); | |
292 | uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr); | |
293 | void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data); | |
294 | void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data); | |
295 | void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data); | |
adfb743c MT |
296 | uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr); |
297 | uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr); | |
298 | uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr); | |
299 | void virtio_config_modern_writeb(VirtIODevice *vdev, | |
300 | uint32_t addr, uint32_t data); | |
301 | void virtio_config_modern_writew(VirtIODevice *vdev, | |
302 | uint32_t addr, uint32_t data); | |
303 | void virtio_config_modern_writel(VirtIODevice *vdev, | |
304 | uint32_t addr, uint32_t data); | |
a8170e5e AK |
305 | void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr); |
306 | hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n); | |
e63c0ba1 | 307 | void virtio_queue_set_num(VirtIODevice *vdev, int n, int num); |
53c25cea | 308 | int virtio_queue_get_num(VirtIODevice *vdev, int n); |
8c797e75 | 309 | int virtio_queue_get_max_num(VirtIODevice *vdev, int n); |
8ad176aa | 310 | int virtio_get_num_queues(VirtIODevice *vdev); |
ab223c95 CH |
311 | void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc, |
312 | hwaddr avail, hwaddr used); | |
313 | void virtio_queue_update_rings(VirtIODevice *vdev, int n); | |
f0d634ea | 314 | void virtio_init_region_cache(VirtIODevice *vdev, int n); |
6ce69d1c | 315 | void virtio_queue_set_align(VirtIODevice *vdev, int n, int align); |
53c25cea | 316 | void virtio_queue_notify(VirtIODevice *vdev, int n); |
7055e687 MT |
317 | uint16_t virtio_queue_vector(VirtIODevice *vdev, int n); |
318 | void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector); | |
6f80e617 TB |
319 | int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n, |
320 | MemoryRegion *mr, bool assign); | |
0b352fd6 | 321 | int virtio_set_status(VirtIODevice *vdev, uint8_t val); |
53c25cea | 322 | void virtio_reset(void *opaque); |
b3a8d6f4 | 323 | void virtio_queue_reset(VirtIODevice *vdev, uint32_t queue_index); |
3c37f8b8 | 324 | void virtio_queue_enable(VirtIODevice *vdev, uint32_t queue_index); |
53c25cea | 325 | void virtio_update_irq(VirtIODevice *vdev); |
d5aaa1b0 | 326 | int virtio_set_features(VirtIODevice *vdev, uint64_t val); |
53c25cea | 327 | |
53c25cea | 328 | /* Base devices. */ |
12c5674b | 329 | typedef struct VirtIOBlkConf VirtIOBlkConf; |
f0c07c7c | 330 | struct virtio_net_conf; |
6b331efb | 331 | typedef struct virtio_serial_conf virtio_serial_conf; |
f73ddbad | 332 | typedef struct virtio_input_conf virtio_input_conf; |
973abc7f | 333 | typedef struct VirtIOSCSIConf VirtIOSCSIConf; |
16c915ba | 334 | typedef struct VirtIORNGConf VirtIORNGConf; |
97b15621 | 335 | |
8172539d | 336 | #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \ |
019a3edb GH |
337 | DEFINE_PROP_BIT64("indirect_desc", _state, _field, \ |
338 | VIRTIO_RING_F_INDIRECT_DESC, true), \ | |
339 | DEFINE_PROP_BIT64("event_idx", _state, _field, \ | |
340 | VIRTIO_RING_F_EVENT_IDX, true), \ | |
341 | DEFINE_PROP_BIT64("notify_on_empty", _state, _field, \ | |
09999a5f MT |
342 | VIRTIO_F_NOTIFY_ON_EMPTY, true), \ |
343 | DEFINE_PROP_BIT64("any_layout", _state, _field, \ | |
8607f5c3 JW |
344 | VIRTIO_F_ANY_LAYOUT, true), \ |
345 | DEFINE_PROP_BIT64("iommu_platform", _state, _field, \ | |
74b3e466 JW |
346 | VIRTIO_F_IOMMU_PLATFORM, false), \ |
347 | DEFINE_PROP_BIT64("packed", _state, _field, \ | |
69e1c14a KX |
348 | VIRTIO_F_RING_PACKED, false), \ |
349 | DEFINE_PROP_BIT64("queue_reset", _state, _field, \ | |
350 | VIRTIO_F_RING_RESET, true) | |
8172539d | 351 | |
a8170e5e | 352 | hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n); |
0c9753eb | 353 | bool virtio_queue_enabled_legacy(VirtIODevice *vdev, int n); |
23bfaf77 | 354 | bool virtio_queue_enabled(VirtIODevice *vdev, int n); |
a8170e5e AK |
355 | hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n); |
356 | hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n); | |
a8170e5e AK |
357 | hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n); |
358 | hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n); | |
359 | hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n); | |
86044b24 JW |
360 | unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n); |
361 | void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, | |
362 | unsigned int idx); | |
2d4ba6cc | 363 | void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n); |
6793dfd1 | 364 | void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n); |
312d3b35 | 365 | void virtio_queue_update_used_idx(VirtIODevice *vdev, int n); |
1cbdabe2 | 366 | VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n); |
e78a2b42 | 367 | uint16_t virtio_get_queue_index(VirtQueue *vq); |
1cbdabe2 | 368 | EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq); |
15b2bd18 PB |
369 | void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign, |
370 | bool with_irqfd); | |
ff4c07df | 371 | int virtio_device_start_ioeventfd(VirtIODevice *vdev); |
310837de PB |
372 | int virtio_device_grab_ioeventfd(VirtIODevice *vdev); |
373 | void virtio_device_release_ioeventfd(VirtIODevice *vdev); | |
8e93cef1 | 374 | bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev); |
1cbdabe2 | 375 | EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq); |
fcccb271 | 376 | void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled); |
fa283a4a | 377 | void virtio_queue_host_notifier_read(EventNotifier *n); |
db608fb7 | 378 | void virtio_queue_aio_attach_host_notifier(VirtQueue *vq, AioContext *ctx); |
38738f7d | 379 | void virtio_queue_aio_attach_host_notifier_no_poll(VirtQueue *vq, AioContext *ctx); |
db608fb7 | 380 | void virtio_queue_aio_detach_host_notifier(VirtQueue *vq, AioContext *ctx); |
e0d686bf JW |
381 | VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector); |
382 | VirtQueue *virtio_vector_next_queue(VirtQueue *vq); | |
7d847d0c CL |
383 | EventNotifier *virtio_config_get_guest_notifier(VirtIODevice *vdev); |
384 | void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev, | |
385 | bool assign, bool with_irqfd); | |
98ed8ecf | 386 | |
019a3edb | 387 | static inline void virtio_add_feature(uint64_t *features, unsigned int fbit) |
0cd09c3a | 388 | { |
019a3edb | 389 | assert(fbit < 64); |
d5aaa1b0 | 390 | *features |= (1ULL << fbit); |
0cd09c3a CH |
391 | } |
392 | ||
019a3edb | 393 | static inline void virtio_clear_feature(uint64_t *features, unsigned int fbit) |
0cd09c3a | 394 | { |
019a3edb | 395 | assert(fbit < 64); |
d5aaa1b0 | 396 | *features &= ~(1ULL << fbit); |
0cd09c3a CH |
397 | } |
398 | ||
95129d6f | 399 | static inline bool virtio_has_feature(uint64_t features, unsigned int fbit) |
ef546f12 | 400 | { |
019a3edb | 401 | assert(fbit < 64); |
d5aaa1b0 | 402 | return !!(features & (1ULL << fbit)); |
ef546f12 CH |
403 | } |
404 | ||
0a47810b | 405 | static inline bool virtio_vdev_has_feature(const VirtIODevice *vdev, |
95129d6f | 406 | unsigned int fbit) |
ef546f12 | 407 | { |
95129d6f | 408 | return virtio_has_feature(vdev->guest_features, fbit); |
ef546f12 CH |
409 | } |
410 | ||
74aae7b2 JW |
411 | static inline bool virtio_host_has_feature(VirtIODevice *vdev, |
412 | unsigned int fbit) | |
413 | { | |
95129d6f | 414 | return virtio_has_feature(vdev->host_features, fbit); |
74aae7b2 JW |
415 | } |
416 | ||
616a6552 | 417 | static inline bool virtio_is_big_endian(VirtIODevice *vdev) |
98ed8ecf | 418 | { |
95129d6f | 419 | if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { |
3c185597 CH |
420 | assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN); |
421 | return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG; | |
422 | } | |
423 | /* Devices conforming to VIRTIO 1.0 or later are always LE. */ | |
424 | return false; | |
98ed8ecf | 425 | } |
e57f2c31 | 426 | |
4987e5bf AB |
427 | /** |
428 | * virtio_device_started() - check if device started | |
429 | * @vdev - the VirtIO device | |
430 | * @status - the devices status bits | |
431 | * | |
432 | * Check if the device is started. For most modern machines this is | |
433 | * tracked via the @vdev->started field (to support migration), | |
434 | * otherwise we check for the final negotiated status bit that | |
435 | * indicates everything is ready. | |
436 | */ | |
e57f2c31 XY |
437 | static inline bool virtio_device_started(VirtIODevice *vdev, uint8_t status) |
438 | { | |
439 | if (vdev->use_started) { | |
440 | return vdev->started; | |
441 | } | |
442 | ||
259d69c0 AB |
443 | return status & VIRTIO_CONFIG_S_DRIVER_OK; |
444 | } | |
445 | ||
446 | /** | |
447 | * virtio_device_should_start() - check if device startable | |
448 | * @vdev - the VirtIO device | |
449 | * @status - the devices status bits | |
450 | * | |
451 | * This is similar to virtio_device_started() but also encapsulates a | |
452 | * check on the VM status which would prevent a device starting | |
453 | * anyway. | |
454 | */ | |
455 | static inline bool virtio_device_should_start(VirtIODevice *vdev, uint8_t status) | |
456 | { | |
9f6bcfd9 AB |
457 | if (!vdev->vm_running) { |
458 | return false; | |
459 | } | |
460 | ||
4987e5bf | 461 | return virtio_device_started(vdev, status); |
e57f2c31 XY |
462 | } |
463 | ||
464 | static inline void virtio_set_started(VirtIODevice *vdev, bool started) | |
465 | { | |
466 | if (started) { | |
467 | vdev->start_on_kick = false; | |
468 | } | |
469 | ||
470 | if (vdev->use_started) { | |
471 | vdev->started = started; | |
472 | } | |
473 | } | |
9d7bd082 MR |
474 | |
475 | static inline void virtio_set_disabled(VirtIODevice *vdev, bool disable) | |
476 | { | |
477 | if (vdev->use_disabled_flag) { | |
478 | vdev->disabled = disable; | |
479 | } | |
480 | } | |
481 | ||
482 | static inline bool virtio_device_disabled(VirtIODevice *vdev) | |
483 | { | |
484 | return unlikely(vdev->disabled || vdev->broken); | |
485 | } | |
486 | ||
7c78bdd7 | 487 | bool virtio_legacy_allowed(VirtIODevice *vdev); |
d55f5182 | 488 | bool virtio_legacy_check_disabled(VirtIODevice *vdev); |
7c78bdd7 | 489 | |
967f97fa | 490 | #endif |