]> git.proxmox.com Git - mirror_qemu.git/blame - include/hw/virtio/virtio.h
virtio: add ability to delete vq through a pointer
[mirror_qemu.git] / include / hw / virtio / virtio.h
CommitLineData
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"
967f97fa 24
8eca6b1b
AL
25/* A guest should never accept this. It implies negotiation is broken. */
26#define VIRTIO_F_BAD_FEATURE 30
967f97fa 27
5f456073
MT
28#define VIRTIO_LEGACY_FEATURES ((0x1ULL << VIRTIO_F_BAD_FEATURE) | \
29 (0x1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \
30 (0x1ULL << VIRTIO_F_ANY_LAYOUT))
31
967f97fa
AL
32struct VirtQueue;
33
a8170e5e 34static inline hwaddr vring_align(hwaddr addr,
f46f15bc
AL
35 unsigned long align)
36{
b8adbc65 37 return QEMU_ALIGN_UP(addr, align);
f46f15bc
AL
38}
39
ba550851
SG
40typedef struct VirtIOFeature {
41 uint64_t flags;
42 size_t end;
43} VirtIOFeature;
44
45size_t virtio_feature_get_config_size(VirtIOFeature *features,
46 uint64_t host_features);
47
967f97fa 48typedef struct VirtQueue VirtQueue;
967f97fa
AL
49
50#define VIRTQUEUE_MAX_SIZE 1024
51
52typedef struct VirtQueueElement
53{
54 unsigned int index;
86044b24
JW
55 unsigned int len;
56 unsigned int ndescs;
967f97fa
AL
57 unsigned int out_num;
58 unsigned int in_num;
3724650d
PB
59 hwaddr *in_addr;
60 hwaddr *out_addr;
61 struct iovec *in_sg;
62 struct iovec *out_sg;
967f97fa
AL
63} VirtQueueElement;
64
b829c2a9 65#define VIRTIO_QUEUE_MAX 1024
967f97fa 66
7055e687
MT
67#define VIRTIO_NO_VECTOR 0xffff
68
8e05db92
FK
69#define TYPE_VIRTIO_DEVICE "virtio-device"
70#define VIRTIO_DEVICE_GET_CLASS(obj) \
71 OBJECT_GET_CLASS(VirtioDeviceClass, obj, TYPE_VIRTIO_DEVICE)
72#define VIRTIO_DEVICE_CLASS(klass) \
73 OBJECT_CLASS_CHECK(VirtioDeviceClass, klass, TYPE_VIRTIO_DEVICE)
74#define VIRTIO_DEVICE(obj) \
75 OBJECT_CHECK(VirtIODevice, (obj), TYPE_VIRTIO_DEVICE)
76
616a6552
GK
77enum virtio_device_endian {
78 VIRTIO_DEVICE_ENDIAN_UNKNOWN,
79 VIRTIO_DEVICE_ENDIAN_LITTLE,
80 VIRTIO_DEVICE_ENDIAN_BIG,
81};
82
967f97fa
AL
83struct VirtIODevice
84{
8e05db92 85 DeviceState parent_obj;
967f97fa 86 const char *name;
967f97fa
AL
87 uint8_t status;
88 uint8_t isr;
89 uint16_t queue_sel;
019a3edb
GH
90 uint64_t guest_features;
91 uint64_t host_features;
75ebec11 92 uint64_t backend_features;
967f97fa
AL
93 size_t config_len;
94 void *config;
7055e687 95 uint16_t config_vector;
b8f05908 96 uint32_t generation;
7055e687 97 int nvectors;
967f97fa 98 VirtQueue *vq;
c611c764 99 MemoryListener listener;
53c25cea 100 uint16_t device_id;
85cf2a8d 101 bool vm_running;
f5ed3663 102 bool broken; /* device in invalid state, needs reset */
e57f2c31 103 bool use_started;
badaf79c 104 bool started;
7abccd08 105 bool start_on_kick; /* when virtio 1.0 feature has not been negotiated */
85cf2a8d 106 VMChangeStateEntry *vmstate;
1034e9cf 107 char *bus_name;
616a6552 108 uint8_t device_endian;
5669655a 109 bool use_guest_notifier_mask;
8607f5c3 110 AddressSpace *dma_as;
850d0070 111 QLIST_HEAD(, VirtQueue) *vector_queues;
967f97fa
AL
112};
113
8e05db92 114typedef struct VirtioDeviceClass {
0ba94b6f 115 /*< private >*/
8e05db92 116 DeviceClass parent;
0ba94b6f 117 /*< public >*/
1d244b42
AF
118
119 /* This is what a VirtioDevice must implement */
1d244b42 120 DeviceRealize realize;
306ec6c3 121 DeviceUnrealize unrealize;
9d5b731d
JW
122 uint64_t (*get_features)(VirtIODevice *vdev,
123 uint64_t requested_features,
124 Error **errp);
019a3edb 125 uint64_t (*bad_features)(VirtIODevice *vdev);
d5aaa1b0 126 void (*set_features)(VirtIODevice *vdev, uint64_t val);
0b352fd6 127 int (*validate_features)(VirtIODevice *vdev);
8e05db92
FK
128 void (*get_config)(VirtIODevice *vdev, uint8_t *config);
129 void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
130 void (*reset)(VirtIODevice *vdev);
131 void (*set_status)(VirtIODevice *vdev, uint8_t val);
9b706dbb
MT
132 /* For transitional devices, this is a bitmap of features
133 * that are only exposed on the legacy interface but not
134 * the modern one.
135 */
136 uint64_t legacy_features;
6a87acf7
FK
137 /* Test and clear event pending status.
138 * Should be called after unmask to avoid losing events.
139 * If backend does not support masking,
140 * must check in frontend instead.
141 */
142 bool (*guest_notifier_pending)(VirtIODevice *vdev, int n);
143 /* Mask/unmask events from this vq. Any events reported
144 * while masked will become pending.
145 * If backend does not support masking,
146 * must mask in frontend instead.
147 */
148 void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask);
ff4c07df
PB
149 int (*start_ioeventfd)(VirtIODevice *vdev);
150 void (*stop_ioeventfd)(VirtIODevice *vdev);
ea43e259
DDAG
151 /* Saving and loading of a device; trying to deprecate save/load
152 * use vmsd for new devices.
153 */
1b5fc0de
GK
154 void (*save)(VirtIODevice *vdev, QEMUFile *f);
155 int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
1dd71383
MT
156 /* Post load hook in vmsd is called early while device is processed, and
157 * when VirtIODevice isn't fully initialized. Devices should use this instead,
158 * unless they specifically want to verify the migration stream as it's
159 * processed, e.g. for bounds checking.
160 */
161 int (*post_load)(VirtIODevice *vdev);
ea43e259 162 const VMStateDescription *vmsd;
9711cd0d 163 bool (*primary_unplug_pending)(void *opaque);
8e05db92
FK
164} VirtioDeviceClass;
165
c8075caf
GA
166void virtio_instance_init_common(Object *proxy_obj, void *data,
167 size_t vdev_size, const char *vdev_name);
168
8e05db92
FK
169void virtio_init(VirtIODevice *vdev, const char *name,
170 uint16_t device_id, size_t config_size);
6a1a8cc7 171void virtio_cleanup(VirtIODevice *vdev);
8e05db92 172
f5ed3663
SH
173void virtio_error(VirtIODevice *vdev, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
174
1034e9cf
FK
175/* Set the child bus name. */
176void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name);
177
bf1780b0 178typedef void (*VirtIOHandleOutput)(VirtIODevice *, VirtQueue *);
07931698 179typedef bool (*VirtIOHandleAIOOutput)(VirtIODevice *, VirtQueue *);
bf1780b0 180
967f97fa 181VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
bf1780b0 182 VirtIOHandleOutput handle_output);
967f97fa 183
f23fd811
JW
184void virtio_del_queue(VirtIODevice *vdev, int n);
185
722f8c51
MT
186void virtio_delete_queue(VirtQueue *vq);
187
967f97fa
AL
188void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
189 unsigned int len);
190void virtqueue_flush(VirtQueue *vq, unsigned int count);
2640d2a5
SH
191void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
192 unsigned int len);
27e57efe
LP
193void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem,
194 unsigned int len);
297a75e6 195bool virtqueue_rewind(VirtQueue *vq, unsigned int num);
967f97fa
AL
196void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
197 unsigned int len, unsigned int idx);
198
8607f5c3 199void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem);
51b19ebe 200void *virtqueue_pop(VirtQueue *vq, size_t sz);
54e17709 201unsigned int virtqueue_drop_all(VirtQueue *vq);
8607f5c3 202void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz);
86044b24
JW
203void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f,
204 VirtQueueElement *elem);
0d8d7690
AS
205int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
206 unsigned int out_bytes);
207void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
e1f7b481
MT
208 unsigned int *out_bytes,
209 unsigned max_in_bytes, unsigned max_out_bytes);
967f97fa 210
83d768b5 211void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq);
967f97fa
AL
212void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
213
2f168d07 214int virtio_save(VirtIODevice *vdev, QEMUFile *f);
5943124c 215
1a665855
HP
216extern const VMStateInfo virtio_vmstate_info;
217
1a665855
HP
218#define VMSTATE_VIRTIO_DEVICE \
219 { \
220 .name = "virtio", \
221 .info = &virtio_vmstate_info, \
222 .flags = VMS_SINGLE, \
223 }
224
1b5fc0de 225int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id);
967f97fa
AL
226
227void virtio_notify_config(VirtIODevice *vdev);
228
229void virtio_queue_set_notification(VirtQueue *vq, int enable);
230
231int virtio_queue_ready(VirtQueue *vq);
232
233int virtio_queue_empty(VirtQueue *vq);
234
53c25cea
PB
235/* Host binding interface. */
236
53c25cea
PB
237uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
238uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
239uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
240void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
241void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
242void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
adfb743c
MT
243uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr);
244uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr);
245uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr);
246void virtio_config_modern_writeb(VirtIODevice *vdev,
247 uint32_t addr, uint32_t data);
248void virtio_config_modern_writew(VirtIODevice *vdev,
249 uint32_t addr, uint32_t data);
250void virtio_config_modern_writel(VirtIODevice *vdev,
251 uint32_t addr, uint32_t data);
a8170e5e
AK
252void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
253hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
e63c0ba1 254void virtio_queue_set_num(VirtIODevice *vdev, int n, int num);
53c25cea 255int virtio_queue_get_num(VirtIODevice *vdev, int n);
8c797e75 256int virtio_queue_get_max_num(VirtIODevice *vdev, int n);
8ad176aa 257int virtio_get_num_queues(VirtIODevice *vdev);
ab223c95
CH
258void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
259 hwaddr avail, hwaddr used);
260void virtio_queue_update_rings(VirtIODevice *vdev, int n);
6ce69d1c 261void virtio_queue_set_align(VirtIODevice *vdev, int n, int align);
53c25cea 262void virtio_queue_notify(VirtIODevice *vdev, int n);
7055e687
MT
263uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
264void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
6f80e617
TB
265int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n,
266 MemoryRegion *mr, bool assign);
0b352fd6 267int virtio_set_status(VirtIODevice *vdev, uint8_t val);
53c25cea
PB
268void virtio_reset(void *opaque);
269void virtio_update_irq(VirtIODevice *vdev);
d5aaa1b0 270int virtio_set_features(VirtIODevice *vdev, uint64_t val);
53c25cea 271
53c25cea 272/* Base devices. */
12c5674b 273typedef struct VirtIOBlkConf VirtIOBlkConf;
f0c07c7c 274struct virtio_net_conf;
6b331efb 275typedef struct virtio_serial_conf virtio_serial_conf;
f73ddbad 276typedef struct virtio_input_conf virtio_input_conf;
973abc7f 277typedef struct VirtIOSCSIConf VirtIOSCSIConf;
16c915ba 278typedef struct VirtIORNGConf VirtIORNGConf;
97b15621 279
8172539d 280#define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
019a3edb
GH
281 DEFINE_PROP_BIT64("indirect_desc", _state, _field, \
282 VIRTIO_RING_F_INDIRECT_DESC, true), \
283 DEFINE_PROP_BIT64("event_idx", _state, _field, \
284 VIRTIO_RING_F_EVENT_IDX, true), \
285 DEFINE_PROP_BIT64("notify_on_empty", _state, _field, \
09999a5f
MT
286 VIRTIO_F_NOTIFY_ON_EMPTY, true), \
287 DEFINE_PROP_BIT64("any_layout", _state, _field, \
8607f5c3
JW
288 VIRTIO_F_ANY_LAYOUT, true), \
289 DEFINE_PROP_BIT64("iommu_platform", _state, _field, \
74b3e466
JW
290 VIRTIO_F_IOMMU_PLATFORM, false), \
291 DEFINE_PROP_BIT64("packed", _state, _field, \
292 VIRTIO_F_RING_PACKED, false)
8172539d 293
a8170e5e 294hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n);
23bfaf77 295bool virtio_queue_enabled(VirtIODevice *vdev, int n);
a8170e5e
AK
296hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n);
297hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n);
a8170e5e
AK
298hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n);
299hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n);
300hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n);
86044b24
JW
301unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n);
302void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n,
303 unsigned int idx);
2d4ba6cc 304void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n);
6793dfd1 305void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n);
312d3b35 306void virtio_queue_update_used_idx(VirtIODevice *vdev, int n);
1cbdabe2 307VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
e78a2b42 308uint16_t virtio_get_queue_index(VirtQueue *vq);
1cbdabe2 309EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
15b2bd18
PB
310void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
311 bool with_irqfd);
ff4c07df 312int virtio_device_start_ioeventfd(VirtIODevice *vdev);
310837de
PB
313int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
314void virtio_device_release_ioeventfd(VirtIODevice *vdev);
8e93cef1 315bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev);
1cbdabe2 316EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
fcccb271 317void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled);
fa283a4a 318void virtio_queue_host_notifier_read(EventNotifier *n);
a1afb606 319void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
07931698 320 VirtIOHandleAIOOutput handle_output);
e0d686bf
JW
321VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector);
322VirtQueue *virtio_vector_next_queue(VirtQueue *vq);
98ed8ecf 323
019a3edb 324static inline void virtio_add_feature(uint64_t *features, unsigned int fbit)
0cd09c3a 325{
019a3edb 326 assert(fbit < 64);
d5aaa1b0 327 *features |= (1ULL << fbit);
0cd09c3a
CH
328}
329
019a3edb 330static inline void virtio_clear_feature(uint64_t *features, unsigned int fbit)
0cd09c3a 331{
019a3edb 332 assert(fbit < 64);
d5aaa1b0 333 *features &= ~(1ULL << fbit);
0cd09c3a
CH
334}
335
95129d6f 336static inline bool virtio_has_feature(uint64_t features, unsigned int fbit)
ef546f12 337{
019a3edb 338 assert(fbit < 64);
d5aaa1b0 339 return !!(features & (1ULL << fbit));
ef546f12
CH
340}
341
95129d6f
CH
342static inline bool virtio_vdev_has_feature(VirtIODevice *vdev,
343 unsigned int fbit)
ef546f12 344{
95129d6f 345 return virtio_has_feature(vdev->guest_features, fbit);
ef546f12
CH
346}
347
74aae7b2
JW
348static inline bool virtio_host_has_feature(VirtIODevice *vdev,
349 unsigned int fbit)
350{
95129d6f 351 return virtio_has_feature(vdev->host_features, fbit);
74aae7b2
JW
352}
353
616a6552 354static inline bool virtio_is_big_endian(VirtIODevice *vdev)
98ed8ecf 355{
95129d6f 356 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3c185597
CH
357 assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
358 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
359 }
360 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
361 return false;
98ed8ecf 362}
e57f2c31
XY
363
364static inline bool virtio_device_started(VirtIODevice *vdev, uint8_t status)
365{
366 if (vdev->use_started) {
367 return vdev->started;
368 }
369
370 return status & VIRTIO_CONFIG_S_DRIVER_OK;
371}
372
373static inline void virtio_set_started(VirtIODevice *vdev, bool started)
374{
375 if (started) {
376 vdev->start_on_kick = false;
377 }
378
379 if (vdev->use_started) {
380 vdev->started = started;
381 }
382}
967f97fa 383#endif