Ia64: stop passing in global variable as argument to cmos_init()
[qemu-kvm/amd-iommu.git] / hw / virtio.h
blob0dcedbf439dee8e7fd82478907d22a6c7052a6dc
1 /*
2 * Virtio Support
4 * Copyright IBM, Corp. 2007
6 * Authors:
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
17 #include <sys/uio.h>
18 #include "hw.h"
19 #include "pci.h"
21 /* from Linux's linux/virtio_config.h */
23 /* Status byte for guest to report progress, and synchronize features. */
24 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
25 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
26 /* We have found a driver for the device. */
27 #define VIRTIO_CONFIG_S_DRIVER 2
28 /* Driver has used its parts of the config, and is happy */
29 #define VIRTIO_CONFIG_S_DRIVER_OK 4
30 /* We've given up on this device. */
31 #define VIRTIO_CONFIG_S_FAILED 0x80
33 /* We notify when the ring is completely used, even if the guest is supressing
34 * callbacks */
35 #define VIRTIO_F_NOTIFY_ON_EMPTY 24
37 /* from Linux's linux/virtio_ring.h */
39 /* This marks a buffer as continuing via the next field. */
40 #define VRING_DESC_F_NEXT 1
41 /* This marks a buffer as write-only (otherwise read-only). */
42 #define VRING_DESC_F_WRITE 2
44 /* This means don't notify other side when buffer added. */
45 #define VRING_USED_F_NO_NOTIFY 1
46 /* This means don't interrupt guest when buffer consumed. */
47 #define VRING_AVAIL_F_NO_INTERRUPT 1
49 typedef struct VirtQueue VirtQueue;
50 typedef struct VirtIODevice VirtIODevice;
52 typedef struct VRingDesc
54 uint64_t addr;
55 uint32_t len;
56 uint16_t flags;
57 uint16_t next;
58 } VRingDesc;
60 typedef struct VRingAvail
62 uint16_t flags;
63 uint16_t idx;
64 uint16_t ring[0];
65 } VRingAvail;
67 typedef struct VRingUsedElem
69 uint32_t id;
70 uint32_t len;
71 } VRingUsedElem;
73 typedef struct VRingUsed
75 uint16_t flags;
76 uint16_t idx;
77 VRingUsedElem ring[0];
78 } VRingUsed;
80 typedef struct VRing
82 unsigned int num;
83 VRingDesc *desc;
84 VRingAvail *avail;
85 VRingUsed *used;
86 } VRing;
88 struct VirtQueue
90 VRing vring;
91 uint32_t pfn;
92 uint16_t last_avail_idx;
93 int inuse;
94 void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
97 #define VIRTQUEUE_MAX_SIZE 1024
99 typedef struct VirtQueueElement
101 unsigned int index;
102 unsigned int out_num;
103 unsigned int in_num;
104 struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
105 struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
106 } VirtQueueElement;
108 #define VIRTIO_PCI_QUEUE_MAX 16
110 struct VirtIODevice
112 PCIDevice pci_dev;
113 const char *name;
114 uint32_t addr;
115 uint8_t status;
116 uint8_t isr;
117 uint16_t queue_sel;
118 uint32_t features;
119 size_t config_len;
120 void *config;
121 uint32_t (*get_features)(VirtIODevice *vdev);
122 void (*set_features)(VirtIODevice *vdev, uint32_t val);
123 void (*get_config)(VirtIODevice *vdev, uint8_t *config);
124 void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
125 void (*reset)(VirtIODevice *vdev);
126 VirtQueue vq[VIRTIO_PCI_QUEUE_MAX];
129 VirtIODevice *virtio_init_pci(PCIBus *bus, const char *name,
130 uint16_t vendor, uint16_t device,
131 uint16_t subvendor, uint16_t subdevice,
132 uint8_t class_code, uint8_t subclass_code,
133 uint8_t pif, size_t config_size,
134 size_t struct_size);
136 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
137 void (*handle_output)(VirtIODevice *,
138 VirtQueue *));
140 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
141 unsigned int len);
143 int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem);
145 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
147 void virtio_save(VirtIODevice *vdev, QEMUFile *f);
149 void virtio_load(VirtIODevice *vdev, QEMUFile *f);
151 void virtio_notify_config(VirtIODevice *vdev);
153 #endif