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