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