Statistics
| Branch: | Revision:

root / hw / virtio.h @ a1e47211

History | View | Annotate | Download (9.3 kB)

1 967f97fa aliguori
/*
2 967f97fa aliguori
 * Virtio Support
3 967f97fa aliguori
 *
4 967f97fa aliguori
 * Copyright IBM, Corp. 2007
5 967f97fa aliguori
 *
6 967f97fa aliguori
 * Authors:
7 967f97fa aliguori
 *  Anthony Liguori   <aliguori@us.ibm.com>
8 967f97fa aliguori
 *
9 967f97fa aliguori
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10 967f97fa aliguori
 * the COPYING file in the top-level directory.
11 967f97fa aliguori
 *
12 967f97fa aliguori
 */
13 967f97fa aliguori
14 967f97fa aliguori
#ifndef _QEMU_VIRTIO_H
15 967f97fa aliguori
#define _QEMU_VIRTIO_H
16 967f97fa aliguori
17 967f97fa aliguori
#include "hw.h"
18 97b15621 Gerd Hoffmann
#include "net.h"
19 53c25cea Paul Brook
#include "qdev.h"
20 d176c495 Gerd Hoffmann
#include "sysemu.h"
21 1cbdabe2 Michael S. Tsirkin
#include "event_notifier.h"
22 9f107513 Anthony Liguori
#ifdef CONFIG_LINUX
23 9f107513 Anthony Liguori
#include "9p.h"
24 9f107513 Anthony Liguori
#endif
25 967f97fa aliguori
26 967f97fa aliguori
/* from Linux's linux/virtio_config.h */
27 967f97fa aliguori
28 967f97fa aliguori
/* Status byte for guest to report progress, and synchronize features. */
29 967f97fa aliguori
/* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
30 967f97fa aliguori
#define VIRTIO_CONFIG_S_ACKNOWLEDGE     1
31 967f97fa aliguori
/* We have found a driver for the device. */
32 967f97fa aliguori
#define VIRTIO_CONFIG_S_DRIVER          2
33 967f97fa aliguori
/* Driver has used its parts of the config, and is happy */
34 967f97fa aliguori
#define VIRTIO_CONFIG_S_DRIVER_OK       4
35 967f97fa aliguori
/* We've given up on this device. */
36 967f97fa aliguori
#define VIRTIO_CONFIG_S_FAILED          0x80
37 967f97fa aliguori
38 6d74ca5a Michael S. Tsirkin
/* Some virtio feature bits (currently bits 28 through 31) are reserved for the
39 6d74ca5a Michael S. Tsirkin
 * transport being used (eg. virtio_ring), the rest are per-device feature bits. */
40 6d74ca5a Michael S. Tsirkin
#define VIRTIO_TRANSPORT_F_START        28
41 6d74ca5a Michael S. Tsirkin
#define VIRTIO_TRANSPORT_F_END          32
42 6d74ca5a Michael S. Tsirkin
43 d89c682f Stefan Weil
/* We notify when the ring is completely used, even if the guest is suppressing
44 967f97fa aliguori
 * callbacks */
45 967f97fa aliguori
#define VIRTIO_F_NOTIFY_ON_EMPTY        24
46 efeea6d0 Mark McLoughlin
/* We support indirect buffer descriptors */
47 efeea6d0 Mark McLoughlin
#define VIRTIO_RING_F_INDIRECT_DESC     28
48 bcbabae8 Michael S. Tsirkin
/* The Guest publishes the used index for which it expects an interrupt
49 bcbabae8 Michael S. Tsirkin
 * at the end of the avail ring. Host should ignore the avail->flags field. */
50 bcbabae8 Michael S. Tsirkin
/* The Host publishes the avail index for which it expects a kick
51 bcbabae8 Michael S. Tsirkin
 * at the end of the used ring. Guest should ignore the used->flags field. */
52 bcbabae8 Michael S. Tsirkin
#define VIRTIO_RING_F_EVENT_IDX         29
53 8eca6b1b aliguori
/* A guest should never accept this.  It implies negotiation is broken. */
54 8eca6b1b aliguori
#define VIRTIO_F_BAD_FEATURE                30
55 967f97fa aliguori
56 967f97fa aliguori
/* from Linux's linux/virtio_ring.h */
57 967f97fa aliguori
58 967f97fa aliguori
/* This marks a buffer as continuing via the next field. */
59 967f97fa aliguori
#define VRING_DESC_F_NEXT       1
60 967f97fa aliguori
/* This marks a buffer as write-only (otherwise read-only). */
61 967f97fa aliguori
#define VRING_DESC_F_WRITE      2
62 efeea6d0 Mark McLoughlin
/* This means the buffer contains a list of buffer descriptors. */
63 efeea6d0 Mark McLoughlin
#define VRING_DESC_F_INDIRECT  4
64 967f97fa aliguori
65 967f97fa aliguori
/* This means don't notify other side when buffer added. */
66 967f97fa aliguori
#define VRING_USED_F_NO_NOTIFY  1
67 967f97fa aliguori
/* This means don't interrupt guest when buffer consumed. */
68 967f97fa aliguori
#define VRING_AVAIL_F_NO_INTERRUPT      1
69 967f97fa aliguori
70 967f97fa aliguori
struct VirtQueue;
71 967f97fa aliguori
72 c227f099 Anthony Liguori
static inline target_phys_addr_t vring_align(target_phys_addr_t addr,
73 f46f15bc aliguori
                                             unsigned long align)
74 f46f15bc aliguori
{
75 f46f15bc aliguori
    return (addr + align - 1) & ~(align - 1);
76 f46f15bc aliguori
}
77 f46f15bc aliguori
78 967f97fa aliguori
typedef struct VirtQueue VirtQueue;
79 967f97fa aliguori
80 967f97fa aliguori
#define VIRTQUEUE_MAX_SIZE 1024
81 967f97fa aliguori
82 967f97fa aliguori
typedef struct VirtQueueElement
83 967f97fa aliguori
{
84 967f97fa aliguori
    unsigned int index;
85 967f97fa aliguori
    unsigned int out_num;
86 967f97fa aliguori
    unsigned int in_num;
87 c227f099 Anthony Liguori
    target_phys_addr_t in_addr[VIRTQUEUE_MAX_SIZE];
88 42fb2e07 Kevin Wolf
    target_phys_addr_t out_addr[VIRTQUEUE_MAX_SIZE];
89 967f97fa aliguori
    struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
90 967f97fa aliguori
    struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
91 967f97fa aliguori
} VirtQueueElement;
92 967f97fa aliguori
93 53c25cea Paul Brook
typedef struct {
94 7055e687 Michael S. Tsirkin
    void (*notify)(void * opaque, uint16_t vector);
95 ff24bd58 Michael S. Tsirkin
    void (*save_config)(void * opaque, QEMUFile *f);
96 ff24bd58 Michael S. Tsirkin
    void (*save_queue)(void * opaque, int n, QEMUFile *f);
97 ff24bd58 Michael S. Tsirkin
    int (*load_config)(void * opaque, QEMUFile *f);
98 ff24bd58 Michael S. Tsirkin
    int (*load_queue)(void * opaque, int n, QEMUFile *f);
99 89c473fd Michael S. Tsirkin
    int (*load_done)(void * opaque, QEMUFile *f);
100 6d74ca5a Michael S. Tsirkin
    unsigned (*get_features)(void * opaque);
101 5430a28f mst@redhat.com
    bool (*query_guest_notifiers)(void * opaque);
102 54dd9321 Michael S. Tsirkin
    int (*set_guest_notifiers)(void * opaque, bool assigned);
103 1cbdabe2 Michael S. Tsirkin
    int (*set_host_notifier)(void * opaque, int n, bool assigned);
104 85cf2a8d Michael S. Tsirkin
    void (*vmstate_change)(void * opaque, bool running);
105 53c25cea Paul Brook
} VirtIOBindings;
106 53c25cea Paul Brook
107 bb61564c Amit Shah
#define VIRTIO_PCI_QUEUE_MAX 64
108 967f97fa aliguori
109 7055e687 Michael S. Tsirkin
#define VIRTIO_NO_VECTOR 0xffff
110 7055e687 Michael S. Tsirkin
111 967f97fa aliguori
struct VirtIODevice
112 967f97fa aliguori
{
113 967f97fa aliguori
    const char *name;
114 967f97fa aliguori
    uint8_t status;
115 967f97fa aliguori
    uint8_t isr;
116 967f97fa aliguori
    uint16_t queue_sel;
117 704a76fc Michael S. Tsirkin
    uint32_t guest_features;
118 967f97fa aliguori
    size_t config_len;
119 967f97fa aliguori
    void *config;
120 7055e687 Michael S. Tsirkin
    uint16_t config_vector;
121 7055e687 Michael S. Tsirkin
    int nvectors;
122 8172539d Michael S. Tsirkin
    uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features);
123 8eca6b1b aliguori
    uint32_t (*bad_features)(VirtIODevice *vdev);
124 967f97fa aliguori
    void (*set_features)(VirtIODevice *vdev, uint32_t val);
125 967f97fa aliguori
    void (*get_config)(VirtIODevice *vdev, uint8_t *config);
126 967f97fa aliguori
    void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
127 967f97fa aliguori
    void (*reset)(VirtIODevice *vdev);
128 3e607cb5 Michael S. Tsirkin
    void (*set_status)(VirtIODevice *vdev, uint8_t val);
129 967f97fa aliguori
    VirtQueue *vq;
130 53c25cea Paul Brook
    const VirtIOBindings *binding;
131 53c25cea Paul Brook
    void *binding_opaque;
132 53c25cea Paul Brook
    uint16_t device_id;
133 85cf2a8d Michael S. Tsirkin
    bool vm_running;
134 85cf2a8d Michael S. Tsirkin
    VMChangeStateEntry *vmstate;
135 967f97fa aliguori
};
136 967f97fa aliguori
137 967f97fa aliguori
VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
138 967f97fa aliguori
                            void (*handle_output)(VirtIODevice *,
139 967f97fa aliguori
                                                  VirtQueue *));
140 967f97fa aliguori
141 967f97fa aliguori
void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
142 967f97fa aliguori
                    unsigned int len);
143 967f97fa aliguori
void virtqueue_flush(VirtQueue *vq, unsigned int count);
144 967f97fa aliguori
void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
145 967f97fa aliguori
                    unsigned int len, unsigned int idx);
146 967f97fa aliguori
147 42fb2e07 Kevin Wolf
void virtqueue_map_sg(struct iovec *sg, target_phys_addr_t *addr,
148 42fb2e07 Kevin Wolf
    size_t num_sg, int is_write);
149 967f97fa aliguori
int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem);
150 0d8d7690 Amit Shah
int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
151 0d8d7690 Amit Shah
                          unsigned int out_bytes);
152 0d8d7690 Amit Shah
void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
153 0d8d7690 Amit Shah
                               unsigned int *out_bytes);
154 967f97fa aliguori
155 967f97fa aliguori
void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
156 967f97fa aliguori
157 967f97fa aliguori
void virtio_save(VirtIODevice *vdev, QEMUFile *f);
158 967f97fa aliguori
159 7055e687 Michael S. Tsirkin
int virtio_load(VirtIODevice *vdev, QEMUFile *f);
160 967f97fa aliguori
161 b946a153 aliguori
void virtio_cleanup(VirtIODevice *vdev);
162 b946a153 aliguori
163 967f97fa aliguori
void virtio_notify_config(VirtIODevice *vdev);
164 967f97fa aliguori
165 967f97fa aliguori
void virtio_queue_set_notification(VirtQueue *vq, int enable);
166 967f97fa aliguori
167 967f97fa aliguori
int virtio_queue_ready(VirtQueue *vq);
168 967f97fa aliguori
169 967f97fa aliguori
int virtio_queue_empty(VirtQueue *vq);
170 967f97fa aliguori
171 53c25cea Paul Brook
/* Host binding interface.  */
172 53c25cea Paul Brook
173 53c25cea Paul Brook
VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
174 53c25cea Paul Brook
                                 size_t config_size, size_t struct_size);
175 53c25cea Paul Brook
uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
176 53c25cea Paul Brook
uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
177 53c25cea Paul Brook
uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
178 53c25cea Paul Brook
void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
179 53c25cea Paul Brook
void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
180 53c25cea Paul Brook
void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
181 c227f099 Anthony Liguori
void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr);
182 c227f099 Anthony Liguori
target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n);
183 53c25cea Paul Brook
int virtio_queue_get_num(VirtIODevice *vdev, int n);
184 53c25cea Paul Brook
void virtio_queue_notify(VirtIODevice *vdev, int n);
185 7055e687 Michael S. Tsirkin
uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
186 7055e687 Michael S. Tsirkin
void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector);
187 4e1837f8 Stefan Hajnoczi
void virtio_set_status(VirtIODevice *vdev, uint8_t val);
188 53c25cea Paul Brook
void virtio_reset(void *opaque);
189 53c25cea Paul Brook
void virtio_update_irq(VirtIODevice *vdev);
190 ad0c9332 Paolo Bonzini
int virtio_set_features(VirtIODevice *vdev, uint32_t val);
191 53c25cea Paul Brook
192 53c25cea Paul Brook
void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
193 53c25cea Paul Brook
                        void *opaque);
194 53c25cea Paul Brook
195 53c25cea Paul Brook
/* Base devices.  */
196 12c5674b Paolo Bonzini
typedef struct VirtIOBlkConf VirtIOBlkConf;
197 12c5674b Paolo Bonzini
VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk);
198 f0c07c7c Alex Williamson
struct virtio_net_conf;
199 f0c07c7c Alex Williamson
VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
200 f0c07c7c Alex Williamson
                              struct virtio_net_conf *net);
201 6b331efb Amit Shah
typedef struct virtio_serial_conf virtio_serial_conf;
202 6b331efb Amit Shah
VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *serial);
203 53c25cea Paul Brook
VirtIODevice *virtio_balloon_init(DeviceState *dev);
204 973abc7f Stefan Hajnoczi
typedef struct VirtIOSCSIConf VirtIOSCSIConf;
205 973abc7f Stefan Hajnoczi
VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *conf);
206 9f107513 Anthony Liguori
#ifdef CONFIG_LINUX
207 9f107513 Anthony Liguori
VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf);
208 9f107513 Anthony Liguori
#endif
209 9f107513 Anthony Liguori
210 53c25cea Paul Brook
211 97b15621 Gerd Hoffmann
void virtio_net_exit(VirtIODevice *vdev);
212 9d0d3138 Alex Williamson
void virtio_blk_exit(VirtIODevice *vdev);
213 8b53a865 Amit Shah
void virtio_serial_exit(VirtIODevice *vdev);
214 855d7e25 Amit Shah
void virtio_balloon_exit(VirtIODevice *vdev);
215 973abc7f Stefan Hajnoczi
void virtio_scsi_exit(VirtIODevice *vdev);
216 97b15621 Gerd Hoffmann
217 8172539d Michael S. Tsirkin
#define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \
218 8172539d Michael S. Tsirkin
        DEFINE_PROP_BIT("indirect_desc", _state, _field, \
219 bcbabae8 Michael S. Tsirkin
                        VIRTIO_RING_F_INDIRECT_DESC, true), \
220 bcbabae8 Michael S. Tsirkin
        DEFINE_PROP_BIT("event_idx", _state, _field, \
221 bcbabae8 Michael S. Tsirkin
                        VIRTIO_RING_F_EVENT_IDX, true)
222 8172539d Michael S. Tsirkin
223 1cbdabe2 Michael S. Tsirkin
target_phys_addr_t virtio_queue_get_desc_addr(VirtIODevice *vdev, int n);
224 1cbdabe2 Michael S. Tsirkin
target_phys_addr_t virtio_queue_get_avail_addr(VirtIODevice *vdev, int n);
225 1cbdabe2 Michael S. Tsirkin
target_phys_addr_t virtio_queue_get_used_addr(VirtIODevice *vdev, int n);
226 1cbdabe2 Michael S. Tsirkin
target_phys_addr_t virtio_queue_get_ring_addr(VirtIODevice *vdev, int n);
227 1cbdabe2 Michael S. Tsirkin
target_phys_addr_t virtio_queue_get_desc_size(VirtIODevice *vdev, int n);
228 1cbdabe2 Michael S. Tsirkin
target_phys_addr_t virtio_queue_get_avail_size(VirtIODevice *vdev, int n);
229 1cbdabe2 Michael S. Tsirkin
target_phys_addr_t virtio_queue_get_used_size(VirtIODevice *vdev, int n);
230 1cbdabe2 Michael S. Tsirkin
target_phys_addr_t virtio_queue_get_ring_size(VirtIODevice *vdev, int n);
231 1cbdabe2 Michael S. Tsirkin
uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n);
232 1cbdabe2 Michael S. Tsirkin
void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx);
233 1cbdabe2 Michael S. Tsirkin
VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
234 c80decdb Paolo Bonzini
int virtio_queue_get_id(VirtQueue *vq);
235 1cbdabe2 Michael S. Tsirkin
EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
236 15b2bd18 Paolo Bonzini
void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
237 15b2bd18 Paolo Bonzini
                                                bool with_irqfd);
238 1cbdabe2 Michael S. Tsirkin
EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
239 26b9b5fe Paolo Bonzini
void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,
240 26b9b5fe Paolo Bonzini
                                               bool set_handler);
241 25db9ebe Stefan Hajnoczi
void virtio_queue_notify_vq(VirtQueue *vq);
242 1cbdabe2 Michael S. Tsirkin
void virtio_irq(VirtQueue *vq);
243 967f97fa aliguori
#endif