xref: /qemu/include/hw/virtio/virtio.h (revision b8f059081d93f1802480059d1d49fe5c1d32f60c)
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 "hw/hw.h"
18 #include "net/net.h"
19 #include "hw/qdev.h"
20 #include "sysemu/sysemu.h"
21 #include "qemu/event_notifier.h"
22 #include "standard-headers/linux/virtio_config.h"
23 #include "standard-headers/linux/virtio_ring.h"
24 
25 /* A guest should never accept this.  It implies negotiation is broken. */
26 #define VIRTIO_F_BAD_FEATURE		30
27 
28 struct VirtQueue;
29 
30 static inline hwaddr vring_align(hwaddr addr,
31                                              unsigned long align)
32 {
33     return (addr + align - 1) & ~(align - 1);
34 }
35 
36 typedef struct VirtQueue VirtQueue;
37 
38 #define VIRTQUEUE_MAX_SIZE 1024
39 
40 typedef struct VirtQueueElement
41 {
42     unsigned int index;
43     unsigned int out_num;
44     unsigned int in_num;
45     hwaddr in_addr[VIRTQUEUE_MAX_SIZE];
46     hwaddr out_addr[VIRTQUEUE_MAX_SIZE];
47     struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
48     struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
49 } VirtQueueElement;
50 
51 #define VIRTIO_QUEUE_MAX 1024
52 
53 #define VIRTIO_NO_VECTOR 0xffff
54 
55 #define TYPE_VIRTIO_DEVICE "virtio-device"
56 #define VIRTIO_DEVICE_GET_CLASS(obj) \
57         OBJECT_GET_CLASS(VirtioDeviceClass, obj, TYPE_VIRTIO_DEVICE)
58 #define VIRTIO_DEVICE_CLASS(klass) \
59         OBJECT_CLASS_CHECK(VirtioDeviceClass, klass, TYPE_VIRTIO_DEVICE)
60 #define VIRTIO_DEVICE(obj) \
61         OBJECT_CHECK(VirtIODevice, (obj), TYPE_VIRTIO_DEVICE)
62 
63 enum virtio_device_endian {
64     VIRTIO_DEVICE_ENDIAN_UNKNOWN,
65     VIRTIO_DEVICE_ENDIAN_LITTLE,
66     VIRTIO_DEVICE_ENDIAN_BIG,
67 };
68 
69 struct VirtIODevice
70 {
71     DeviceState parent_obj;
72     const char *name;
73     uint8_t status;
74     uint8_t isr;
75     uint16_t queue_sel;
76     uint64_t guest_features;
77     uint64_t host_features;
78     size_t config_len;
79     void *config;
80     uint16_t config_vector;
81     uint32_t generation;
82     int nvectors;
83     VirtQueue *vq;
84     uint16_t device_id;
85     bool vm_running;
86     VMChangeStateEntry *vmstate;
87     char *bus_name;
88     uint8_t device_endian;
89     QLIST_HEAD(, VirtQueue) *vector_queues;
90 };
91 
92 typedef struct VirtioDeviceClass {
93     /*< private >*/
94     DeviceClass parent;
95     /*< public >*/
96 
97     /* This is what a VirtioDevice must implement */
98     DeviceRealize realize;
99     DeviceUnrealize unrealize;
100     uint64_t (*get_features)(VirtIODevice *vdev, uint64_t requested_features);
101     uint64_t (*bad_features)(VirtIODevice *vdev);
102     void (*set_features)(VirtIODevice *vdev, uint64_t val);
103     int (*validate_features)(VirtIODevice *vdev);
104     void (*get_config)(VirtIODevice *vdev, uint8_t *config);
105     void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
106     void (*reset)(VirtIODevice *vdev);
107     void (*set_status)(VirtIODevice *vdev, uint8_t val);
108     /* Test and clear event pending status.
109      * Should be called after unmask to avoid losing events.
110      * If backend does not support masking,
111      * must check in frontend instead.
112      */
113     bool (*guest_notifier_pending)(VirtIODevice *vdev, int n);
114     /* Mask/unmask events from this vq. Any events reported
115      * while masked will become pending.
116      * If backend does not support masking,
117      * must mask in frontend instead.
118      */
119     void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask);
120     void (*save)(VirtIODevice *vdev, QEMUFile *f);
121     int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
122 } VirtioDeviceClass;
123 
124 void virtio_instance_init_common(Object *proxy_obj, void *data,
125                                  size_t vdev_size, const char *vdev_name);
126 
127 void virtio_init(VirtIODevice *vdev, const char *name,
128                          uint16_t device_id, size_t config_size);
129 void virtio_cleanup(VirtIODevice *vdev);
130 
131 /* Set the child bus name. */
132 void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name);
133 
134 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
135                             void (*handle_output)(VirtIODevice *,
136                                                   VirtQueue *));
137 
138 void virtio_del_queue(VirtIODevice *vdev, int n);
139 
140 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
141                     unsigned int len);
142 void virtqueue_flush(VirtQueue *vq, unsigned int count);
143 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
144                     unsigned int len, unsigned int idx);
145 
146 void virtqueue_map_sg(struct iovec *sg, hwaddr *addr,
147     size_t num_sg, int is_write);
148 int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem);
149 int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
150                           unsigned int out_bytes);
151 void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
152                                unsigned int *out_bytes,
153                                unsigned max_in_bytes, unsigned max_out_bytes);
154 
155 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
156 
157 void virtio_save(VirtIODevice *vdev, QEMUFile *f);
158 
159 int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id);
160 
161 void virtio_notify_config(VirtIODevice *vdev);
162 
163 void virtio_queue_set_notification(VirtQueue *vq, int enable);
164 
165 int virtio_queue_ready(VirtQueue *vq);
166 
167 int virtio_queue_empty(VirtQueue *vq);
168 
169 /* Host binding interface.  */
170 
171 uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
172 uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
173 uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
174 void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
175 void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
176 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
177 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
178 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
179 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num);
180 int virtio_queue_get_num(VirtIODevice *vdev, int n);
181 int virtio_get_num_queues(VirtIODevice *vdev);
182 void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
183                             hwaddr avail, hwaddr used);
184 void virtio_queue_update_rings(VirtIODevice *vdev, int n);
185 void virtio_queue_set_align(VirtIODevice *vdev, int n, int align);
186 void virtio_queue_notify(VirtIODevice *vdev, int n);
187 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
188 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
189 int virtio_set_status(VirtIODevice *vdev, uint8_t val);
190 void virtio_reset(void *opaque);
191 void virtio_update_irq(VirtIODevice *vdev);
192 int virtio_set_features(VirtIODevice *vdev, uint64_t val);
193 
194 /* Base devices.  */
195 typedef struct VirtIOBlkConf VirtIOBlkConf;
196 struct virtio_net_conf;
197 typedef struct virtio_serial_conf virtio_serial_conf;
198 typedef struct virtio_input_conf virtio_input_conf;
199 typedef struct VirtIOSCSIConf VirtIOSCSIConf;
200 typedef struct VirtIORNGConf VirtIORNGConf;
201 
202 #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
203     DEFINE_PROP_BIT64("indirect_desc", _state, _field,    \
204                       VIRTIO_RING_F_INDIRECT_DESC, true), \
205     DEFINE_PROP_BIT64("event_idx", _state, _field,        \
206                       VIRTIO_RING_F_EVENT_IDX, true),     \
207     DEFINE_PROP_BIT64("notify_on_empty", _state, _field,  \
208                       VIRTIO_F_NOTIFY_ON_EMPTY, true)
209 
210 hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n);
211 hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n);
212 hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n);
213 hwaddr virtio_queue_get_ring_addr(VirtIODevice *vdev, int n);
214 hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n);
215 hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n);
216 hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n);
217 hwaddr virtio_queue_get_ring_size(VirtIODevice *vdev, int n);
218 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n);
219 void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx);
220 void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n);
221 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
222 uint16_t virtio_get_queue_index(VirtQueue *vq);
223 int virtio_queue_get_id(VirtQueue *vq);
224 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
225 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
226                                                 bool with_irqfd);
227 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
228 void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,
229                                                bool set_handler);
230 void virtio_queue_notify_vq(VirtQueue *vq);
231 void virtio_irq(VirtQueue *vq);
232 VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector);
233 VirtQueue *virtio_vector_next_queue(VirtQueue *vq);
234 
235 static inline void virtio_add_feature(uint64_t *features, unsigned int fbit)
236 {
237     assert(fbit < 64);
238     *features |= (1ULL << fbit);
239 }
240 
241 static inline void virtio_clear_feature(uint64_t *features, unsigned int fbit)
242 {
243     assert(fbit < 64);
244     *features &= ~(1ULL << fbit);
245 }
246 
247 static inline bool __virtio_has_feature(uint64_t features, unsigned int fbit)
248 {
249     assert(fbit < 64);
250     return !!(features & (1ULL << fbit));
251 }
252 
253 static inline bool virtio_has_feature(VirtIODevice *vdev, unsigned int fbit)
254 {
255     return __virtio_has_feature(vdev->guest_features, fbit);
256 }
257 
258 static inline bool virtio_is_big_endian(VirtIODevice *vdev)
259 {
260     if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
261         assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
262         return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
263     }
264     /* Devices conforming to VIRTIO 1.0 or later are always LE. */
265     return false;
266 }
267 #endif
268