xref: /openbmc/qemu/include/hw/virtio/virtio.h (revision e1fe50dc)
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 #ifdef CONFIG_VIRTFS
23 #include "hw/virtio/virtio-9p.h"
24 #endif
25 
26 /* from Linux's linux/virtio_config.h */
27 
28 /* Status byte for guest to report progress, and synchronize features. */
29 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
30 #define VIRTIO_CONFIG_S_ACKNOWLEDGE     1
31 /* We have found a driver for the device. */
32 #define VIRTIO_CONFIG_S_DRIVER          2
33 /* Driver has used its parts of the config, and is happy */
34 #define VIRTIO_CONFIG_S_DRIVER_OK       4
35 /* We've given up on this device. */
36 #define VIRTIO_CONFIG_S_FAILED          0x80
37 
38 /* Some virtio feature bits (currently bits 28 through 31) are reserved for the
39  * transport being used (eg. virtio_ring), the rest are per-device feature bits. */
40 #define VIRTIO_TRANSPORT_F_START        28
41 #define VIRTIO_TRANSPORT_F_END          32
42 
43 /* We notify when the ring is completely used, even if the guest is suppressing
44  * callbacks */
45 #define VIRTIO_F_NOTIFY_ON_EMPTY        24
46 /* We support indirect buffer descriptors */
47 #define VIRTIO_RING_F_INDIRECT_DESC     28
48 /* The Guest publishes the used index for which it expects an interrupt
49  * at the end of the avail ring. Host should ignore the avail->flags field. */
50 /* The Host publishes the avail index for which it expects a kick
51  * at the end of the used ring. Guest should ignore the used->flags field. */
52 #define VIRTIO_RING_F_EVENT_IDX         29
53 /* A guest should never accept this.  It implies negotiation is broken. */
54 #define VIRTIO_F_BAD_FEATURE		30
55 
56 /* from Linux's linux/virtio_ring.h */
57 
58 /* This marks a buffer as continuing via the next field. */
59 #define VRING_DESC_F_NEXT       1
60 /* This marks a buffer as write-only (otherwise read-only). */
61 #define VRING_DESC_F_WRITE      2
62 /* This means the buffer contains a list of buffer descriptors. */
63 #define VRING_DESC_F_INDIRECT  4
64 
65 /* This means don't notify other side when buffer added. */
66 #define VRING_USED_F_NO_NOTIFY  1
67 /* This means don't interrupt guest when buffer consumed. */
68 #define VRING_AVAIL_F_NO_INTERRUPT      1
69 
70 struct VirtQueue;
71 
72 static inline hwaddr vring_align(hwaddr addr,
73                                              unsigned long align)
74 {
75     return (addr + align - 1) & ~(align - 1);
76 }
77 
78 typedef struct VirtQueue VirtQueue;
79 
80 #define VIRTQUEUE_MAX_SIZE 1024
81 
82 typedef struct VirtQueueElement
83 {
84     unsigned int index;
85     unsigned int out_num;
86     unsigned int in_num;
87     hwaddr in_addr[VIRTQUEUE_MAX_SIZE];
88     hwaddr out_addr[VIRTQUEUE_MAX_SIZE];
89     struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
90     struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
91 } VirtQueueElement;
92 
93 typedef struct {
94     void (*notify)(DeviceState *d, uint16_t vector);
95     void (*save_config)(DeviceState *d, QEMUFile *f);
96     void (*save_queue)(DeviceState *d, int n, QEMUFile *f);
97     int (*load_config)(DeviceState *d, QEMUFile *f);
98     int (*load_queue)(DeviceState *d, int n, QEMUFile *f);
99     int (*load_done)(DeviceState *d, QEMUFile *f);
100     unsigned (*get_features)(DeviceState *d);
101     bool (*query_guest_notifiers)(DeviceState *d);
102     int (*set_guest_notifiers)(DeviceState *d, int nvqs, bool assigned);
103     int (*set_host_notifier)(DeviceState *d, int n, bool assigned);
104     void (*vmstate_change)(DeviceState *d, bool running);
105 } VirtIOBindings;
106 
107 #define VIRTIO_PCI_QUEUE_MAX 64
108 
109 #define VIRTIO_NO_VECTOR 0xffff
110 
111 #define TYPE_VIRTIO_DEVICE "virtio-device"
112 #define VIRTIO_DEVICE_GET_CLASS(obj) \
113         OBJECT_GET_CLASS(VirtioDeviceClass, obj, TYPE_VIRTIO_DEVICE)
114 #define VIRTIO_DEVICE_CLASS(klass) \
115         OBJECT_CLASS_CHECK(VirtioDeviceClass, klass, TYPE_VIRTIO_DEVICE)
116 #define VIRTIO_DEVICE(obj) \
117         OBJECT_CHECK(VirtIODevice, (obj), TYPE_VIRTIO_DEVICE)
118 
119 struct VirtIODevice
120 {
121     DeviceState parent_obj;
122     const char *name;
123     uint8_t status;
124     uint8_t isr;
125     uint16_t queue_sel;
126     uint32_t guest_features;
127     size_t config_len;
128     void *config;
129     uint16_t config_vector;
130     int nvectors;
131     /*
132      * Function pointers will be removed at the end of the series as they are in
133      * VirtioDeviceClass.
134      */
135     uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features);
136     uint32_t (*bad_features)(VirtIODevice *vdev);
137     void (*set_features)(VirtIODevice *vdev, uint32_t val);
138     void (*get_config)(VirtIODevice *vdev, uint8_t *config);
139     void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
140     void (*reset)(VirtIODevice *vdev);
141     void (*set_status)(VirtIODevice *vdev, uint8_t val);
142     /* Test and clear event pending status.
143      * Should be called after unmask to avoid losing events.
144      * If backend does not support masking,
145      * must check in frontend instead.
146      */
147     bool (*guest_notifier_pending)(VirtIODevice *vdev, int n);
148     /* Mask/unmask events from this vq. Any events reported
149      * while masked will become pending.
150      * If backend does not support masking,
151      * must mask in frontend instead.
152      */
153     void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask);
154 
155     VirtQueue *vq;
156     const VirtIOBindings *binding;
157     DeviceState *binding_opaque;
158     uint16_t device_id;
159     bool vm_running;
160     VMChangeStateEntry *vmstate;
161 };
162 
163 typedef struct VirtioDeviceClass {
164     /* This is what a VirtioDevice must implement */
165     DeviceClass parent;
166     int (*init)(VirtIODevice *vdev);
167     uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features);
168     uint32_t (*bad_features)(VirtIODevice *vdev);
169     void (*set_features)(VirtIODevice *vdev, uint32_t val);
170     void (*get_config)(VirtIODevice *vdev, uint8_t *config);
171     void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
172     void (*reset)(VirtIODevice *vdev);
173     void (*set_status)(VirtIODevice *vdev, uint8_t val);
174     /* Test and clear event pending status.
175      * Should be called after unmask to avoid losing events.
176      * If backend does not support masking,
177      * must check in frontend instead.
178      */
179     bool (*guest_notifier_pending)(VirtIODevice *vdev, int n);
180     /* Mask/unmask events from this vq. Any events reported
181      * while masked will become pending.
182      * If backend does not support masking,
183      * must mask in frontend instead.
184      */
185     void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask);
186 } VirtioDeviceClass;
187 
188 void virtio_init(VirtIODevice *vdev, const char *name,
189                          uint16_t device_id, size_t config_size);
190 void virtio_common_cleanup(VirtIODevice *vdev);
191 
192 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
193                             void (*handle_output)(VirtIODevice *,
194                                                   VirtQueue *));
195 
196 void virtio_del_queue(VirtIODevice *vdev, int n);
197 
198 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
199                     unsigned int len);
200 void virtqueue_flush(VirtQueue *vq, unsigned int count);
201 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
202                     unsigned int len, unsigned int idx);
203 
204 void virtqueue_map_sg(struct iovec *sg, hwaddr *addr,
205     size_t num_sg, int is_write);
206 int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem);
207 int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
208                           unsigned int out_bytes);
209 void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
210                                unsigned int *out_bytes,
211                                unsigned max_in_bytes, unsigned max_out_bytes);
212 
213 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
214 
215 void virtio_save(VirtIODevice *vdev, QEMUFile *f);
216 
217 int virtio_load(VirtIODevice *vdev, QEMUFile *f);
218 
219 void virtio_cleanup(VirtIODevice *vdev);
220 
221 void virtio_notify_config(VirtIODevice *vdev);
222 
223 void virtio_queue_set_notification(VirtQueue *vq, int enable);
224 
225 int virtio_queue_ready(VirtQueue *vq);
226 
227 int virtio_queue_empty(VirtQueue *vq);
228 
229 /* Host binding interface.  */
230 
231 VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
232                                  size_t config_size, size_t struct_size);
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 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr);
240 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n);
241 int virtio_queue_get_num(VirtIODevice *vdev, int n);
242 void virtio_queue_notify(VirtIODevice *vdev, int n);
243 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
244 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
245 void virtio_set_status(VirtIODevice *vdev, uint8_t val);
246 void virtio_reset(void *opaque);
247 void virtio_update_irq(VirtIODevice *vdev);
248 int virtio_set_features(VirtIODevice *vdev, uint32_t val);
249 
250 void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
251                         DeviceState *opaque);
252 
253 /* Base devices.  */
254 typedef struct VirtIOBlkConf VirtIOBlkConf;
255 struct virtio_net_conf;
256 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
257                               struct virtio_net_conf *net,
258                               uint32_t host_features);
259 typedef struct virtio_serial_conf virtio_serial_conf;
260 VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *serial);
261 typedef struct VirtIOSCSIConf VirtIOSCSIConf;
262 VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *conf);
263 typedef struct VirtIORNGConf VirtIORNGConf;
264 VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf);
265 #ifdef CONFIG_VIRTFS
266 VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf);
267 #endif
268 
269 
270 void virtio_net_exit(VirtIODevice *vdev);
271 void virtio_serial_exit(VirtIODevice *vdev);
272 void virtio_scsi_exit(VirtIODevice *vdev);
273 void virtio_rng_exit(VirtIODevice *vdev);
274 
275 #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
276 	DEFINE_PROP_BIT("indirect_desc", _state, _field, \
277 			VIRTIO_RING_F_INDIRECT_DESC, true), \
278 	DEFINE_PROP_BIT("event_idx", _state, _field, \
279 			VIRTIO_RING_F_EVENT_IDX, true)
280 
281 hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n);
282 hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n);
283 hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n);
284 hwaddr virtio_queue_get_ring_addr(VirtIODevice *vdev, int n);
285 hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n);
286 hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n);
287 hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n);
288 hwaddr virtio_queue_get_ring_size(VirtIODevice *vdev, int n);
289 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n);
290 void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx);
291 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
292 uint16_t virtio_get_queue_index(VirtQueue *vq);
293 int virtio_queue_get_id(VirtQueue *vq);
294 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
295 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
296                                                 bool with_irqfd);
297 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
298 void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,
299                                                bool set_handler);
300 void virtio_queue_notify_vq(VirtQueue *vq);
301 void virtio_irq(VirtQueue *vq);
302 #endif
303