root / hw / virtio.h @ 1d108d97
History | View | Annotate | Download (5.6 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 | 53c25cea | Paul Brook | #include "qdev.h" |
19 | 967f97fa | aliguori | |
20 | 967f97fa | aliguori | /* from Linux's linux/virtio_config.h */
|
21 | 967f97fa | aliguori | |
22 | 967f97fa | aliguori | /* Status byte for guest to report progress, and synchronize features. */
|
23 | 967f97fa | aliguori | /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
|
24 | 967f97fa | aliguori | #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1 |
25 | 967f97fa | aliguori | /* We have found a driver for the device. */
|
26 | 967f97fa | aliguori | #define VIRTIO_CONFIG_S_DRIVER 2 |
27 | 967f97fa | aliguori | /* Driver has used its parts of the config, and is happy */
|
28 | 967f97fa | aliguori | #define VIRTIO_CONFIG_S_DRIVER_OK 4 |
29 | 967f97fa | aliguori | /* We've given up on this device. */
|
30 | 967f97fa | aliguori | #define VIRTIO_CONFIG_S_FAILED 0x80 |
31 | 967f97fa | aliguori | |
32 | 967f97fa | aliguori | /* We notify when the ring is completely used, even if the guest is supressing
|
33 | 967f97fa | aliguori | * callbacks */
|
34 | 967f97fa | aliguori | #define VIRTIO_F_NOTIFY_ON_EMPTY 24 |
35 | efeea6d0 | Mark McLoughlin | /* We support indirect buffer descriptors */
|
36 | efeea6d0 | Mark McLoughlin | #define VIRTIO_RING_F_INDIRECT_DESC 28 |
37 | 8eca6b1b | aliguori | /* A guest should never accept this. It implies negotiation is broken. */
|
38 | 8eca6b1b | aliguori | #define VIRTIO_F_BAD_FEATURE 30 |
39 | 967f97fa | aliguori | |
40 | 967f97fa | aliguori | /* from Linux's linux/virtio_ring.h */
|
41 | 967f97fa | aliguori | |
42 | 967f97fa | aliguori | /* This marks a buffer as continuing via the next field. */
|
43 | 967f97fa | aliguori | #define VRING_DESC_F_NEXT 1 |
44 | 967f97fa | aliguori | /* This marks a buffer as write-only (otherwise read-only). */
|
45 | 967f97fa | aliguori | #define VRING_DESC_F_WRITE 2 |
46 | efeea6d0 | Mark McLoughlin | /* This means the buffer contains a list of buffer descriptors. */
|
47 | efeea6d0 | Mark McLoughlin | #define VRING_DESC_F_INDIRECT 4 |
48 | 967f97fa | aliguori | |
49 | 967f97fa | aliguori | /* This means don't notify other side when buffer added. */
|
50 | 967f97fa | aliguori | #define VRING_USED_F_NO_NOTIFY 1 |
51 | 967f97fa | aliguori | /* This means don't interrupt guest when buffer consumed. */
|
52 | 967f97fa | aliguori | #define VRING_AVAIL_F_NO_INTERRUPT 1 |
53 | 967f97fa | aliguori | |
54 | 967f97fa | aliguori | struct VirtQueue;
|
55 | 967f97fa | aliguori | |
56 | f46f15bc | aliguori | static inline target_phys_addr_t vring_align(target_phys_addr_t addr, |
57 | f46f15bc | aliguori | unsigned long align) |
58 | f46f15bc | aliguori | { |
59 | f46f15bc | aliguori | return (addr + align - 1) & ~(align - 1); |
60 | f46f15bc | aliguori | } |
61 | f46f15bc | aliguori | |
62 | 967f97fa | aliguori | typedef struct VirtQueue VirtQueue; |
63 | 967f97fa | aliguori | typedef struct VirtIODevice VirtIODevice; |
64 | 967f97fa | aliguori | |
65 | 967f97fa | aliguori | #define VIRTQUEUE_MAX_SIZE 1024 |
66 | 967f97fa | aliguori | |
67 | 967f97fa | aliguori | typedef struct VirtQueueElement |
68 | 967f97fa | aliguori | { |
69 | 967f97fa | aliguori | unsigned int index; |
70 | 967f97fa | aliguori | unsigned int out_num; |
71 | 967f97fa | aliguori | unsigned int in_num; |
72 | 967f97fa | aliguori | target_phys_addr_t in_addr[VIRTQUEUE_MAX_SIZE]; |
73 | 967f97fa | aliguori | struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
|
74 | 967f97fa | aliguori | struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
|
75 | 967f97fa | aliguori | } VirtQueueElement; |
76 | 967f97fa | aliguori | |
77 | 53c25cea | Paul Brook | typedef struct { |
78 | 7055e687 | Michael S. Tsirkin | void (*notify)(void * opaque, uint16_t vector); |
79 | ff24bd58 | Michael S. Tsirkin | void (*save_config)(void * opaque, QEMUFile *f); |
80 | ff24bd58 | Michael S. Tsirkin | void (*save_queue)(void * opaque, int n, QEMUFile *f); |
81 | ff24bd58 | Michael S. Tsirkin | int (*load_config)(void * opaque, QEMUFile *f); |
82 | ff24bd58 | Michael S. Tsirkin | int (*load_queue)(void * opaque, int n, QEMUFile *f); |
83 | 53c25cea | Paul Brook | } VirtIOBindings; |
84 | 53c25cea | Paul Brook | |
85 | 967f97fa | aliguori | #define VIRTIO_PCI_QUEUE_MAX 16 |
86 | 967f97fa | aliguori | |
87 | 7055e687 | Michael S. Tsirkin | #define VIRTIO_NO_VECTOR 0xffff |
88 | 7055e687 | Michael S. Tsirkin | |
89 | 967f97fa | aliguori | struct VirtIODevice
|
90 | 967f97fa | aliguori | { |
91 | 967f97fa | aliguori | const char *name; |
92 | 967f97fa | aliguori | uint8_t status; |
93 | 967f97fa | aliguori | uint8_t isr; |
94 | 967f97fa | aliguori | uint16_t queue_sel; |
95 | 967f97fa | aliguori | uint32_t features; |
96 | 967f97fa | aliguori | size_t config_len; |
97 | 967f97fa | aliguori | void *config;
|
98 | 7055e687 | Michael S. Tsirkin | uint16_t config_vector; |
99 | 7055e687 | Michael S. Tsirkin | int nvectors;
|
100 | 967f97fa | aliguori | uint32_t (*get_features)(VirtIODevice *vdev); |
101 | 8eca6b1b | aliguori | uint32_t (*bad_features)(VirtIODevice *vdev); |
102 | 967f97fa | aliguori | void (*set_features)(VirtIODevice *vdev, uint32_t val);
|
103 | 967f97fa | aliguori | void (*get_config)(VirtIODevice *vdev, uint8_t *config);
|
104 | 967f97fa | aliguori | void (*set_config)(VirtIODevice *vdev, const uint8_t *config); |
105 | 967f97fa | aliguori | void (*reset)(VirtIODevice *vdev);
|
106 | 967f97fa | aliguori | VirtQueue *vq; |
107 | 53c25cea | Paul Brook | const VirtIOBindings *binding;
|
108 | 53c25cea | Paul Brook | void *binding_opaque;
|
109 | 53c25cea | Paul Brook | uint16_t device_id; |
110 | 967f97fa | aliguori | }; |
111 | 967f97fa | aliguori | |
112 | 967f97fa | aliguori | VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
|
113 | 967f97fa | aliguori | void (*handle_output)(VirtIODevice *,
|
114 | 967f97fa | aliguori | VirtQueue *)); |
115 | 967f97fa | aliguori | |
116 | 967f97fa | aliguori | void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, |
117 | 967f97fa | aliguori | unsigned int len); |
118 | 967f97fa | aliguori | void virtqueue_flush(VirtQueue *vq, unsigned int count); |
119 | 967f97fa | aliguori | void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, |
120 | 967f97fa | aliguori | unsigned int len, unsigned int idx); |
121 | 967f97fa | aliguori | |
122 | 967f97fa | aliguori | int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem);
|
123 | 967f97fa | aliguori | int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes); |
124 | 967f97fa | aliguori | |
125 | 967f97fa | aliguori | void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);
|
126 | 967f97fa | aliguori | |
127 | 967f97fa | aliguori | void virtio_save(VirtIODevice *vdev, QEMUFile *f);
|
128 | 967f97fa | aliguori | |
129 | 7055e687 | Michael S. Tsirkin | int virtio_load(VirtIODevice *vdev, QEMUFile *f);
|
130 | 967f97fa | aliguori | |
131 | b946a153 | aliguori | void virtio_cleanup(VirtIODevice *vdev);
|
132 | b946a153 | aliguori | |
133 | 967f97fa | aliguori | void virtio_notify_config(VirtIODevice *vdev);
|
134 | 967f97fa | aliguori | |
135 | 967f97fa | aliguori | void virtio_queue_set_notification(VirtQueue *vq, int enable); |
136 | 967f97fa | aliguori | |
137 | 967f97fa | aliguori | int virtio_queue_ready(VirtQueue *vq);
|
138 | 967f97fa | aliguori | |
139 | 967f97fa | aliguori | int virtio_queue_empty(VirtQueue *vq);
|
140 | 967f97fa | aliguori | |
141 | 53c25cea | Paul Brook | /* Host binding interface. */
|
142 | 53c25cea | Paul Brook | |
143 | 53c25cea | Paul Brook | VirtIODevice *virtio_common_init(const char *name, uint16_t device_id, |
144 | 53c25cea | Paul Brook | size_t config_size, size_t struct_size); |
145 | 53c25cea | Paul Brook | uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr); |
146 | 53c25cea | Paul Brook | uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr); |
147 | 53c25cea | Paul Brook | uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr); |
148 | 53c25cea | Paul Brook | void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
|
149 | 53c25cea | Paul Brook | void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
|
150 | 53c25cea | Paul Brook | void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
|
151 | 53c25cea | Paul Brook | void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr); |
152 | 53c25cea | Paul Brook | target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n);
|
153 | 53c25cea | Paul Brook | int virtio_queue_get_num(VirtIODevice *vdev, int n); |
154 | 53c25cea | Paul Brook | void virtio_queue_notify(VirtIODevice *vdev, int n); |
155 | 7055e687 | Michael S. Tsirkin | uint16_t virtio_queue_vector(VirtIODevice *vdev, int n);
|
156 | 7055e687 | Michael S. Tsirkin | void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector); |
157 | 53c25cea | Paul Brook | void virtio_reset(void *opaque); |
158 | 53c25cea | Paul Brook | void virtio_update_irq(VirtIODevice *vdev);
|
159 | 53c25cea | Paul Brook | |
160 | 53c25cea | Paul Brook | void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding, |
161 | 53c25cea | Paul Brook | void *opaque);
|
162 | 53c25cea | Paul Brook | |
163 | 53c25cea | Paul Brook | /* Base devices. */
|
164 | 53c25cea | Paul Brook | VirtIODevice *virtio_blk_init(DeviceState *dev); |
165 | 53c25cea | Paul Brook | VirtIODevice *virtio_net_init(DeviceState *dev); |
166 | 53c25cea | Paul Brook | VirtIODevice *virtio_console_init(DeviceState *dev); |
167 | 53c25cea | Paul Brook | VirtIODevice *virtio_balloon_init(DeviceState *dev); |
168 | 53c25cea | Paul Brook | |
169 | 967f97fa | aliguori | #endif |