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