4 * Copyright IBM, Corp. 2007
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
17 #include "exec/memory.h"
18 #include "hw/qdev-core.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"
25 /* A guest should never accept this. It implies negotiation is broken. */
26 #define VIRTIO_F_BAD_FEATURE 30
28 #define VIRTIO_LEGACY_FEATURES ((0x1ULL << VIRTIO_F_BAD_FEATURE) | \
29 (0x1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \
30 (0x1ULL << VIRTIO_F_ANY_LAYOUT))
34 static inline hwaddr
vring_align(hwaddr addr
,
37 return QEMU_ALIGN_UP(addr
, align
);
40 typedef struct VirtIOFeature
{
45 size_t virtio_feature_get_config_size(VirtIOFeature
*features
,
46 uint64_t host_features
);
48 typedef struct VirtQueue VirtQueue
;
50 #define VIRTQUEUE_MAX_SIZE 1024
52 typedef struct VirtQueueElement
65 #define VIRTIO_QUEUE_MAX 1024
67 #define VIRTIO_NO_VECTOR 0xffff
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)
77 enum virtio_device_endian
{
78 VIRTIO_DEVICE_ENDIAN_UNKNOWN
,
79 VIRTIO_DEVICE_ENDIAN_LITTLE
,
80 VIRTIO_DEVICE_ENDIAN_BIG
,
85 DeviceState parent_obj
;
90 uint64_t guest_features
;
91 uint64_t host_features
;
92 uint64_t backend_features
;
95 uint16_t config_vector
;
99 MemoryListener listener
;
102 bool broken
; /* device in invalid state, needs reset */
105 bool start_on_kick
; /* when virtio 1.0 feature has not been negotiated */
106 VMChangeStateEntry
*vmstate
;
108 uint8_t device_endian
;
109 bool use_guest_notifier_mask
;
110 AddressSpace
*dma_as
;
111 QLIST_HEAD(, VirtQueue
) *vector_queues
;
114 typedef struct VirtioDeviceClass
{
119 /* This is what a VirtioDevice must implement */
120 DeviceRealize realize
;
121 DeviceUnrealize unrealize
;
122 uint64_t (*get_features
)(VirtIODevice
*vdev
,
123 uint64_t requested_features
,
125 uint64_t (*bad_features
)(VirtIODevice
*vdev
);
126 void (*set_features
)(VirtIODevice
*vdev
, uint64_t val
);
127 int (*validate_features
)(VirtIODevice
*vdev
);
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
);
132 /* For transitional devices, this is a bitmap of features
133 * that are only exposed on the legacy interface but not
136 uint64_t legacy_features
;
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.
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.
148 void (*guest_notifier_mask
)(VirtIODevice
*vdev
, int n
, bool mask
);
149 int (*start_ioeventfd
)(VirtIODevice
*vdev
);
150 void (*stop_ioeventfd
)(VirtIODevice
*vdev
);
151 /* Saving and loading of a device; trying to deprecate save/load
152 * use vmsd for new devices.
154 void (*save
)(VirtIODevice
*vdev
, QEMUFile
*f
);
155 int (*load
)(VirtIODevice
*vdev
, QEMUFile
*f
, int version_id
);
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.
161 int (*post_load
)(VirtIODevice
*vdev
);
162 const VMStateDescription
*vmsd
;
163 bool (*primary_unplug_pending
)(void *opaque
);
166 void virtio_instance_init_common(Object
*proxy_obj
, void *data
,
167 size_t vdev_size
, const char *vdev_name
);
169 void virtio_init(VirtIODevice
*vdev
, const char *name
,
170 uint16_t device_id
, size_t config_size
);
171 void virtio_cleanup(VirtIODevice
*vdev
);
173 void virtio_error(VirtIODevice
*vdev
, const char *fmt
, ...) GCC_FMT_ATTR(2, 3);
175 /* Set the child bus name. */
176 void virtio_device_set_child_bus_name(VirtIODevice
*vdev
, char *bus_name
);
178 typedef void (*VirtIOHandleOutput
)(VirtIODevice
*, VirtQueue
*);
179 typedef bool (*VirtIOHandleAIOOutput
)(VirtIODevice
*, VirtQueue
*);
181 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
182 VirtIOHandleOutput handle_output
);
184 void virtio_del_queue(VirtIODevice
*vdev
, int n
);
186 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
188 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
);
189 void virtqueue_detach_element(VirtQueue
*vq
, const VirtQueueElement
*elem
,
191 void virtqueue_unpop(VirtQueue
*vq
, const VirtQueueElement
*elem
,
193 bool virtqueue_rewind(VirtQueue
*vq
, unsigned int num
);
194 void virtqueue_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
195 unsigned int len
, unsigned int idx
);
197 void virtqueue_map(VirtIODevice
*vdev
, VirtQueueElement
*elem
);
198 void *virtqueue_pop(VirtQueue
*vq
, size_t sz
);
199 unsigned int virtqueue_drop_all(VirtQueue
*vq
);
200 void *qemu_get_virtqueue_element(VirtIODevice
*vdev
, QEMUFile
*f
, size_t sz
);
201 void qemu_put_virtqueue_element(VirtIODevice
*vdev
, QEMUFile
*f
,
202 VirtQueueElement
*elem
);
203 int virtqueue_avail_bytes(VirtQueue
*vq
, unsigned int in_bytes
,
204 unsigned int out_bytes
);
205 void virtqueue_get_avail_bytes(VirtQueue
*vq
, unsigned int *in_bytes
,
206 unsigned int *out_bytes
,
207 unsigned max_in_bytes
, unsigned max_out_bytes
);
209 void virtio_notify_irqfd(VirtIODevice
*vdev
, VirtQueue
*vq
);
210 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
);
212 int virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
);
214 extern const VMStateInfo virtio_vmstate_info
;
216 #define VMSTATE_VIRTIO_DEVICE \
219 .info = &virtio_vmstate_info, \
220 .flags = VMS_SINGLE, \
223 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
, int version_id
);
225 void virtio_notify_config(VirtIODevice
*vdev
);
227 void virtio_queue_set_notification(VirtQueue
*vq
, int enable
);
229 int virtio_queue_ready(VirtQueue
*vq
);
231 int virtio_queue_empty(VirtQueue
*vq
);
233 /* Host binding interface. */
235 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
);
236 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
);
237 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
);
238 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
);
239 void virtio_config_writew(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
);
240 void virtio_config_writel(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
);
241 uint32_t virtio_config_modern_readb(VirtIODevice
*vdev
, uint32_t addr
);
242 uint32_t virtio_config_modern_readw(VirtIODevice
*vdev
, uint32_t addr
);
243 uint32_t virtio_config_modern_readl(VirtIODevice
*vdev
, uint32_t addr
);
244 void virtio_config_modern_writeb(VirtIODevice
*vdev
,
245 uint32_t addr
, uint32_t data
);
246 void virtio_config_modern_writew(VirtIODevice
*vdev
,
247 uint32_t addr
, uint32_t data
);
248 void virtio_config_modern_writel(VirtIODevice
*vdev
,
249 uint32_t addr
, uint32_t data
);
250 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, hwaddr addr
);
251 hwaddr
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
);
252 void virtio_queue_set_num(VirtIODevice
*vdev
, int n
, int num
);
253 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
);
254 int virtio_queue_get_max_num(VirtIODevice
*vdev
, int n
);
255 int virtio_get_num_queues(VirtIODevice
*vdev
);
256 void virtio_queue_set_rings(VirtIODevice
*vdev
, int n
, hwaddr desc
,
257 hwaddr avail
, hwaddr used
);
258 void virtio_queue_update_rings(VirtIODevice
*vdev
, int n
);
259 void virtio_queue_set_align(VirtIODevice
*vdev
, int n
, int align
);
260 void virtio_queue_notify(VirtIODevice
*vdev
, int n
);
261 uint16_t virtio_queue_vector(VirtIODevice
*vdev
, int n
);
262 void virtio_queue_set_vector(VirtIODevice
*vdev
, int n
, uint16_t vector
);
263 int virtio_queue_set_host_notifier_mr(VirtIODevice
*vdev
, int n
,
264 MemoryRegion
*mr
, bool assign
);
265 int virtio_set_status(VirtIODevice
*vdev
, uint8_t val
);
266 void virtio_reset(void *opaque
);
267 void virtio_update_irq(VirtIODevice
*vdev
);
268 int virtio_set_features(VirtIODevice
*vdev
, uint64_t val
);
271 typedef struct VirtIOBlkConf VirtIOBlkConf
;
272 struct virtio_net_conf
;
273 typedef struct virtio_serial_conf virtio_serial_conf
;
274 typedef struct virtio_input_conf virtio_input_conf
;
275 typedef struct VirtIOSCSIConf VirtIOSCSIConf
;
276 typedef struct VirtIORNGConf VirtIORNGConf
;
278 #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
279 DEFINE_PROP_BIT64("indirect_desc", _state, _field, \
280 VIRTIO_RING_F_INDIRECT_DESC, true), \
281 DEFINE_PROP_BIT64("event_idx", _state, _field, \
282 VIRTIO_RING_F_EVENT_IDX, true), \
283 DEFINE_PROP_BIT64("notify_on_empty", _state, _field, \
284 VIRTIO_F_NOTIFY_ON_EMPTY, true), \
285 DEFINE_PROP_BIT64("any_layout", _state, _field, \
286 VIRTIO_F_ANY_LAYOUT, true), \
287 DEFINE_PROP_BIT64("iommu_platform", _state, _field, \
288 VIRTIO_F_IOMMU_PLATFORM, false), \
289 DEFINE_PROP_BIT64("packed", _state, _field, \
290 VIRTIO_F_RING_PACKED, false)
292 hwaddr
virtio_queue_get_desc_addr(VirtIODevice
*vdev
, int n
);
293 bool virtio_queue_enabled(VirtIODevice
*vdev
, int n
);
294 hwaddr
virtio_queue_get_avail_addr(VirtIODevice
*vdev
, int n
);
295 hwaddr
virtio_queue_get_used_addr(VirtIODevice
*vdev
, int n
);
296 hwaddr
virtio_queue_get_desc_size(VirtIODevice
*vdev
, int n
);
297 hwaddr
virtio_queue_get_avail_size(VirtIODevice
*vdev
, int n
);
298 hwaddr
virtio_queue_get_used_size(VirtIODevice
*vdev
, int n
);
299 unsigned int virtio_queue_get_last_avail_idx(VirtIODevice
*vdev
, int n
);
300 void virtio_queue_set_last_avail_idx(VirtIODevice
*vdev
, int n
,
302 void virtio_queue_restore_last_avail_idx(VirtIODevice
*vdev
, int n
);
303 void virtio_queue_invalidate_signalled_used(VirtIODevice
*vdev
, int n
);
304 void virtio_queue_update_used_idx(VirtIODevice
*vdev
, int n
);
305 VirtQueue
*virtio_get_queue(VirtIODevice
*vdev
, int n
);
306 uint16_t virtio_get_queue_index(VirtQueue
*vq
);
307 EventNotifier
*virtio_queue_get_guest_notifier(VirtQueue
*vq
);
308 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
310 int virtio_device_start_ioeventfd(VirtIODevice
*vdev
);
311 int virtio_device_grab_ioeventfd(VirtIODevice
*vdev
);
312 void virtio_device_release_ioeventfd(VirtIODevice
*vdev
);
313 bool virtio_device_ioeventfd_enabled(VirtIODevice
*vdev
);
314 EventNotifier
*virtio_queue_get_host_notifier(VirtQueue
*vq
);
315 void virtio_queue_host_notifier_read(EventNotifier
*n
);
316 void virtio_queue_aio_set_host_notifier_handler(VirtQueue
*vq
, AioContext
*ctx
,
317 VirtIOHandleAIOOutput handle_output
);
318 VirtQueue
*virtio_vector_first_queue(VirtIODevice
*vdev
, uint16_t vector
);
319 VirtQueue
*virtio_vector_next_queue(VirtQueue
*vq
);
321 static inline void virtio_add_feature(uint64_t *features
, unsigned int fbit
)
324 *features
|= (1ULL << fbit
);
327 static inline void virtio_clear_feature(uint64_t *features
, unsigned int fbit
)
330 *features
&= ~(1ULL << fbit
);
333 static inline bool virtio_has_feature(uint64_t features
, unsigned int fbit
)
336 return !!(features
& (1ULL << fbit
));
339 static inline bool virtio_vdev_has_feature(VirtIODevice
*vdev
,
342 return virtio_has_feature(vdev
->guest_features
, fbit
);
345 static inline bool virtio_host_has_feature(VirtIODevice
*vdev
,
348 return virtio_has_feature(vdev
->host_features
, fbit
);
351 static inline bool virtio_is_big_endian(VirtIODevice
*vdev
)
353 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
354 assert(vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_UNKNOWN
);
355 return vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_BIG
;
357 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
361 static inline bool virtio_device_started(VirtIODevice
*vdev
, uint8_t status
)
363 if (vdev
->use_started
) {
364 return vdev
->started
;
367 return status
& VIRTIO_CONFIG_S_DRIVER_OK
;
370 static inline void virtio_set_started(VirtIODevice
*vdev
, bool started
)
373 vdev
->start_on_kick
= false;
376 if (vdev
->use_started
) {
377 vdev
->started
= started
;