]> git.proxmox.com Git - mirror_qemu.git/blob - hw/virtio/virtio-bus.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[mirror_qemu.git] / hw / virtio / virtio-bus.c
1 /*
2 * VirtioBus
3 *
4 * Copyright (C) 2012 : GreenSocs Ltd
5 * http://www.greensocs.com/ , email: info@greensocs.com
6 *
7 * Developed by :
8 * Frederic Konrad <fred.konrad@greensocs.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <http://www.gnu.org/licenses/>.
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 /* #define DEBUG_VIRTIO_BUS */
33
34 #ifdef DEBUG_VIRTIO_BUS
35 #define DPRINTF(fmt, ...) \
36 do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
37 #else
38 #define DPRINTF(fmt, ...) do { } while (0)
39 #endif
40
41 /* A VirtIODevice is being plugged */
42 void 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 /* Get the features of the plugged device. */
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 /* Reset the virtio_bus */
67 void 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 /* A VirtIODevice is being unplugged */
78 void 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 /* Get the device id of the plugged device. */
94 uint16_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 /* Get the config_len field of the plugged device. */
102 size_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 /* Get bad features of the plugged device. */
110 uint32_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 /* Get config of the plugged device. */
125 void 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 /* Set config of the plugged device. */
138 void 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 int virtio_bus_start_ioeventfd(VirtioBusState *bus)
151 {
152 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
153 DeviceState *proxy = DEVICE(BUS(bus)->parent);
154 VirtIODevice *vdev = virtio_bus_get_device(bus);
155 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
156 int r;
157
158 if (!k->ioeventfd_assign || !k->ioeventfd_enabled(proxy)) {
159 return -ENOSYS;
160 }
161 if (bus->ioeventfd_started) {
162 return 0;
163 }
164 r = vdc->start_ioeventfd(vdev);
165 if (r < 0) {
166 error_report("%s: failed. Fallback to userspace (slower).", __func__);
167 return r;
168 }
169 bus->ioeventfd_started = true;
170 return 0;
171 }
172
173 void virtio_bus_stop_ioeventfd(VirtioBusState *bus)
174 {
175 VirtIODevice *vdev;
176 VirtioDeviceClass *vdc;
177
178 if (!bus->ioeventfd_started) {
179 return;
180 }
181
182 vdev = virtio_bus_get_device(bus);
183 vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
184 vdc->stop_ioeventfd(vdev);
185 bus->ioeventfd_started = false;
186 }
187
188 bool virtio_bus_ioeventfd_enabled(VirtioBusState *bus)
189 {
190 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
191 DeviceState *proxy = DEVICE(BUS(bus)->parent);
192
193 return k->ioeventfd_assign && k->ioeventfd_enabled(proxy);
194 }
195
196 /*
197 * This function switches ioeventfd on/off in the device.
198 * The caller must set or clear the handlers for the EventNotifier.
199 */
200 int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign)
201 {
202 VirtIODevice *vdev = virtio_bus_get_device(bus);
203 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
204 DeviceState *proxy = DEVICE(BUS(bus)->parent);
205 VirtQueue *vq = virtio_get_queue(vdev, n);
206 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
207 int r = 0;
208
209 if (!k->ioeventfd_assign) {
210 return -ENOSYS;
211 }
212
213 if (assign) {
214 assert(!bus->ioeventfd_started);
215 r = event_notifier_init(notifier, 1);
216 if (r < 0) {
217 error_report("%s: unable to init event notifier: %s (%d)",
218 __func__, strerror(-r), r);
219 return r;
220 }
221 r = k->ioeventfd_assign(proxy, notifier, n, true);
222 if (r < 0) {
223 error_report("%s: unable to assign ioeventfd: %d", __func__, r);
224 goto cleanup_event_notifier;
225 }
226 return 0;
227 } else {
228 if (!bus->ioeventfd_started) {
229 return 0;
230 }
231 k->ioeventfd_assign(proxy, notifier, n, false);
232 }
233
234 cleanup_event_notifier:
235 /* Test and clear notifier after disabling event,
236 * in case poll callback didn't have time to run.
237 */
238 virtio_queue_host_notifier_read(notifier);
239 event_notifier_cleanup(notifier);
240 return r;
241 }
242
243 static char *virtio_bus_get_dev_path(DeviceState *dev)
244 {
245 BusState *bus = qdev_get_parent_bus(dev);
246 DeviceState *proxy = DEVICE(bus->parent);
247 return qdev_get_dev_path(proxy);
248 }
249
250 static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
251 {
252 return NULL;
253 }
254
255 static void virtio_bus_class_init(ObjectClass *klass, void *data)
256 {
257 BusClass *bus_class = BUS_CLASS(klass);
258 bus_class->get_dev_path = virtio_bus_get_dev_path;
259 bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path;
260 }
261
262 static const TypeInfo virtio_bus_info = {
263 .name = TYPE_VIRTIO_BUS,
264 .parent = TYPE_BUS,
265 .instance_size = sizeof(VirtioBusState),
266 .abstract = true,
267 .class_size = sizeof(VirtioBusClass),
268 .class_init = virtio_bus_class_init
269 };
270
271 static void virtio_register_types(void)
272 {
273 type_register_static(&virtio_bus_info);
274 }
275
276 type_init(virtio_register_types)