4 * Copyright IBM, Corp. 2007
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #ifndef _QEMU_VIRTIO_H
15 #define _QEMU_VIRTIO_H
29 /* from Linux's linux/virtio_config.h */
31 /* Status byte for guest to report progress, and synchronize features. */
32 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
33 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
34 /* We have found a driver for the device. */
35 #define VIRTIO_CONFIG_S_DRIVER 2
36 /* Driver has used its parts of the config, and is happy */
37 #define VIRTIO_CONFIG_S_DRIVER_OK 4
38 /* We've given up on this device. */
39 #define VIRTIO_CONFIG_S_FAILED 0x80
41 /* We notify when the ring is completely used, even if the guest is supressing
43 #define VIRTIO_F_NOTIFY_ON_EMPTY 24
45 /* from Linux's linux/virtio_ring.h */
47 /* This marks a buffer as continuing via the next field. */
48 #define VRING_DESC_F_NEXT 1
49 /* This marks a buffer as write-only (otherwise read-only). */
50 #define VRING_DESC_F_WRITE 2
52 /* This means don't notify other side when buffer added. */
53 #define VRING_USED_F_NO_NOTIFY 1
54 /* This means don't interrupt guest when buffer consumed. */
55 #define VRING_AVAIL_F_NO_INTERRUPT 1
59 static inline target_phys_addr_t vring_align(target_phys_addr_t addr,
62 return (addr + align - 1) & ~(align - 1);
65 typedef struct VirtQueue VirtQueue;
66 typedef struct VirtIODevice VirtIODevice;
68 #define VIRTQUEUE_MAX_SIZE 1024
70 typedef struct VirtQueueElement
75 target_phys_addr_t in_addr[VIRTQUEUE_MAX_SIZE];
76 struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
77 struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
80 #define VIRTIO_PCI_QUEUE_MAX 16
93 uint32_t (*get_features)(VirtIODevice *vdev);
94 void (*set_features)(VirtIODevice *vdev, uint32_t val);
95 void (*get_config)(VirtIODevice *vdev, uint8_t *config);
96 void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
97 void (*reset)(VirtIODevice *vdev);
101 VirtIODevice *virtio_init_pci(PCIBus *bus, const char *name,
102 uint16_t vendor, uint16_t device,
103 uint16_t subvendor, uint16_t subdevice,
104 uint8_t class_code, uint8_t subclass_code,
105 uint8_t pif, size_t config_size,
108 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
109 void (*handle_output)(VirtIODevice *,
112 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
114 void virtqueue_flush(VirtQueue *vq, unsigned int count);
115 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
116 unsigned int len, unsigned int idx);
118 int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem);
119 int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes);
121 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
123 void virtio_save(VirtIODevice *vdev, QEMUFile *f);
125 void virtio_load(VirtIODevice *vdev, QEMUFile *f);
127 void virtio_notify_config(VirtIODevice *vdev);
129 void virtio_queue_set_notification(VirtQueue *vq, int enable);
131 int virtio_queue_ready(VirtQueue *vq);
133 int virtio_queue_empty(VirtQueue *vq);