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