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