1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "hw/hw.h"
27#include "qemu/error-report.h"
28#include "hw/qdev.h"
29#include "hw/virtio/virtio-bus.h"
30#include "hw/virtio/virtio.h"
31
32
33
34#ifdef DEBUG_VIRTIO_BUS
35#define DPRINTF(fmt, ...) \
36do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
37#else
38#define DPRINTF(fmt, ...) do { } while (0)
39#endif
40
41
42void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)
43{
44 DeviceState *qdev = DEVICE(vdev);
45 BusState *qbus = BUS(qdev_get_parent_bus(qdev));
46 VirtioBusState *bus = VIRTIO_BUS(qbus);
47 VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
48 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
49
50 DPRINTF("%s: plug device.\n", qbus->name);
51
52 if (klass->pre_plugged != NULL) {
53 klass->pre_plugged(qbus->parent, errp);
54 }
55
56
57 assert(vdc->get_features != NULL);
58 vdev->host_features = vdc->get_features(vdev, vdev->host_features,
59 errp);
60
61 if (klass->device_plugged != NULL) {
62 klass->device_plugged(qbus->parent, errp);
63 }
64}
65
66
67void virtio_bus_reset(VirtioBusState *bus)
68{
69 VirtIODevice *vdev = virtio_bus_get_device(bus);
70
71 DPRINTF("%s: reset device.\n", BUS(bus)->name);
72 if (vdev != NULL) {
73 virtio_reset(vdev);
74 }
75}
76
77
78void virtio_bus_device_unplugged(VirtIODevice *vdev)
79{
80 DeviceState *qdev = DEVICE(vdev);
81 BusState *qbus = BUS(qdev_get_parent_bus(qdev));
82 VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus);
83
84 DPRINTF("%s: remove device.\n", qbus->name);
85
86 if (vdev != NULL) {
87 if (klass->device_unplugged != NULL) {
88 klass->device_unplugged(qbus->parent);
89 }
90 }
91}
92
93
94uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
95{
96 VirtIODevice *vdev = virtio_bus_get_device(bus);
97 assert(vdev != NULL);
98 return vdev->device_id;
99}
100
101
102size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
103{
104 VirtIODevice *vdev = virtio_bus_get_device(bus);
105 assert(vdev != NULL);
106 return vdev->config_len;
107}
108
109
110uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
111{
112 VirtIODevice *vdev = virtio_bus_get_device(bus);
113 VirtioDeviceClass *k;
114
115 assert(vdev != NULL);
116 k = VIRTIO_DEVICE_GET_CLASS(vdev);
117 if (k->bad_features != NULL) {
118 return k->bad_features(vdev);
119 } else {
120 return 0;
121 }
122}
123
124
125void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
126{
127 VirtIODevice *vdev = virtio_bus_get_device(bus);
128 VirtioDeviceClass *k;
129
130 assert(vdev != NULL);
131 k = VIRTIO_DEVICE_GET_CLASS(vdev);
132 if (k->get_config != NULL) {
133 k->get_config(vdev, config);
134 }
135}
136
137
138void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
139{
140 VirtIODevice *vdev = virtio_bus_get_device(bus);
141 VirtioDeviceClass *k;
142
143 assert(vdev != NULL);
144 k = VIRTIO_DEVICE_GET_CLASS(vdev);
145 if (k->set_config != NULL) {
146 k->set_config(vdev, config);
147 }
148}
149
150
151int virtio_bus_grab_ioeventfd(VirtioBusState *bus)
152{
153 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
154
155
156
157
158 if (!k->ioeventfd_assign) {
159 return -ENOSYS;
160 }
161
162 if (bus->ioeventfd_grabbed == 0 && bus->ioeventfd_started) {
163 virtio_bus_stop_ioeventfd(bus);
164
165
166
167 bus->ioeventfd_started = true;
168 }
169 bus->ioeventfd_grabbed++;
170 return 0;
171}
172
173void virtio_bus_release_ioeventfd(VirtioBusState *bus)
174{
175 assert(bus->ioeventfd_grabbed != 0);
176 if (--bus->ioeventfd_grabbed == 0 && bus->ioeventfd_started) {
177
178 bus->ioeventfd_started = false;
179 virtio_bus_start_ioeventfd(bus);
180 }
181}
182
183int virtio_bus_start_ioeventfd(VirtioBusState *bus)
184{
185 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
186 DeviceState *proxy = DEVICE(BUS(bus)->parent);
187 VirtIODevice *vdev = virtio_bus_get_device(bus);
188 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
189 int r;
190
191 if (!k->ioeventfd_assign || !k->ioeventfd_enabled(proxy)) {
192 return -ENOSYS;
193 }
194 if (bus->ioeventfd_started) {
195 return 0;
196 }
197
198
199 if (!bus->ioeventfd_grabbed) {
200 r = vdc->start_ioeventfd(vdev);
201 if (r < 0) {
202 error_report("%s: failed. Fallback to userspace (slower).", __func__);
203 return r;
204 }
205 }
206 bus->ioeventfd_started = true;
207 return 0;
208}
209
210void virtio_bus_stop_ioeventfd(VirtioBusState *bus)
211{
212 VirtIODevice *vdev;
213 VirtioDeviceClass *vdc;
214
215 if (!bus->ioeventfd_started) {
216 return;
217 }
218
219
220 if (!bus->ioeventfd_grabbed) {
221 vdev = virtio_bus_get_device(bus);
222 vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
223 vdc->stop_ioeventfd(vdev);
224 }
225 bus->ioeventfd_started = false;
226}
227
228bool virtio_bus_ioeventfd_enabled(VirtioBusState *bus)
229{
230 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
231 DeviceState *proxy = DEVICE(BUS(bus)->parent);
232
233 return k->ioeventfd_assign && k->ioeventfd_enabled(proxy);
234}
235
236
237
238
239
240int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign)
241{
242 VirtIODevice *vdev = virtio_bus_get_device(bus);
243 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
244 DeviceState *proxy = DEVICE(BUS(bus)->parent);
245 VirtQueue *vq = virtio_get_queue(vdev, n);
246 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
247 int r = 0;
248
249 if (!k->ioeventfd_assign) {
250 return -ENOSYS;
251 }
252
253 if (assign) {
254 r = event_notifier_init(notifier, 1);
255 if (r < 0) {
256 error_report("%s: unable to init event notifier: %s (%d)",
257 __func__, strerror(-r), r);
258 return r;
259 }
260 r = k->ioeventfd_assign(proxy, notifier, n, true);
261 if (r < 0) {
262 error_report("%s: unable to assign ioeventfd: %d", __func__, r);
263 goto cleanup_event_notifier;
264 }
265 return 0;
266 } else {
267 k->ioeventfd_assign(proxy, notifier, n, false);
268 }
269
270cleanup_event_notifier:
271
272
273
274 virtio_queue_host_notifier_read(notifier);
275 event_notifier_cleanup(notifier);
276 return r;
277}
278
279static char *virtio_bus_get_dev_path(DeviceState *dev)
280{
281 BusState *bus = qdev_get_parent_bus(dev);
282 DeviceState *proxy = DEVICE(bus->parent);
283 return qdev_get_dev_path(proxy);
284}
285
286static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
287{
288 return NULL;
289}
290
291static void virtio_bus_class_init(ObjectClass *klass, void *data)
292{
293 BusClass *bus_class = BUS_CLASS(klass);
294 bus_class->get_dev_path = virtio_bus_get_dev_path;
295 bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path;
296}
297
298static const TypeInfo virtio_bus_info = {
299 .name = TYPE_VIRTIO_BUS,
300 .parent = TYPE_BUS,
301 .instance_size = sizeof(VirtioBusState),
302 .abstract = true,
303 .class_size = sizeof(VirtioBusClass),
304 .class_init = virtio_bus_class_init
305};
306
307static void virtio_register_types(void)
308{
309 type_register_static(&virtio_bus_info);
310}
311
312type_init(virtio_register_types)
313