util/hbitmap: update orig_size on truncate
[qemu/ar7.git] / hw / virtio / virtio-bus.c
blob1d29a631f3a61482538e84c3f67609225ca908ed
1 /*
2 * VirtioBus
4 * Copyright (C) 2012 : GreenSocs Ltd
5 * http://www.greensocs.com/ , email: info@greensocs.com
7 * Developed by :
8 * Frederic Konrad <fred.konrad@greensocs.com>
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.
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.
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/>.
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"
35 /* #define DEBUG_VIRTIO_BUS */
37 #ifdef DEBUG_VIRTIO_BUS
38 #define DPRINTF(fmt, ...) \
39 do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
40 #else
41 #define DPRINTF(fmt, ...) do { } while (0)
42 #endif
44 /* A VirtIODevice is being plugged */
45 void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)
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;
55 DPRINTF("%s: plug device.\n", qbus->name);
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;
65 /* Get the features of the plugged device. */
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;
74 if (klass->device_plugged != NULL) {
75 klass->device_plugged(qbus->parent, &local_err);
77 if (local_err) {
78 error_propagate(errp, local_err);
79 return;
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;
90 /* Reset the virtio_bus */
91 void virtio_bus_reset(VirtioBusState *bus)
93 VirtIODevice *vdev = virtio_bus_get_device(bus);
95 DPRINTF("%s: reset device.\n", BUS(bus)->name);
96 if (vdev != NULL) {
97 virtio_reset(vdev);
101 /* A VirtIODevice is being unplugged */
102 void virtio_bus_device_unplugged(VirtIODevice *vdev)
104 DeviceState *qdev = DEVICE(vdev);
105 BusState *qbus = BUS(qdev_get_parent_bus(qdev));
106 VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus);
108 DPRINTF("%s: remove device.\n", qbus->name);
110 if (vdev != NULL) {
111 if (klass->device_unplugged != NULL) {
112 klass->device_unplugged(qbus->parent);
117 /* Get the device id of the plugged device. */
118 uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
120 VirtIODevice *vdev = virtio_bus_get_device(bus);
121 assert(vdev != NULL);
122 return vdev->device_id;
125 /* Get the config_len field of the plugged device. */
126 size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
128 VirtIODevice *vdev = virtio_bus_get_device(bus);
129 assert(vdev != NULL);
130 return vdev->config_len;
133 /* Get bad features of the plugged device. */
134 uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
136 VirtIODevice *vdev = virtio_bus_get_device(bus);
137 VirtioDeviceClass *k;
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;
148 /* Get config of the plugged device. */
149 void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
151 VirtIODevice *vdev = virtio_bus_get_device(bus);
152 VirtioDeviceClass *k;
154 assert(vdev != NULL);
155 k = VIRTIO_DEVICE_GET_CLASS(vdev);
156 if (k->get_config != NULL) {
157 k->get_config(vdev, config);
161 /* Set config of the plugged device. */
162 void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
164 VirtIODevice *vdev = virtio_bus_get_device(bus);
165 VirtioDeviceClass *k;
167 assert(vdev != NULL);
168 k = VIRTIO_DEVICE_GET_CLASS(vdev);
169 if (k->set_config != NULL) {
170 k->set_config(vdev, config);
174 /* On success, ioeventfd ownership belongs to the caller. */
175 int virtio_bus_grab_ioeventfd(VirtioBusState *bus)
177 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
179 /* vhost can be used even if ioeventfd=off in the proxy device,
180 * so do not check k->ioeventfd_enabled.
182 if (!k->ioeventfd_assign) {
183 return -ENOSYS;
186 if (bus->ioeventfd_grabbed == 0 && bus->ioeventfd_started) {
187 virtio_bus_stop_ioeventfd(bus);
188 /* Remember that we need to restart ioeventfd
189 * when ioeventfd_grabbed becomes zero.
191 bus->ioeventfd_started = true;
193 bus->ioeventfd_grabbed++;
194 return 0;
197 void virtio_bus_release_ioeventfd(VirtioBusState *bus)
199 assert(bus->ioeventfd_grabbed != 0);
200 if (--bus->ioeventfd_grabbed == 0 && bus->ioeventfd_started) {
201 /* Force virtio_bus_start_ioeventfd to act. */
202 bus->ioeventfd_started = false;
203 virtio_bus_start_ioeventfd(bus);
207 int virtio_bus_start_ioeventfd(VirtioBusState *bus)
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;
215 if (!k->ioeventfd_assign || !k->ioeventfd_enabled(proxy)) {
216 return -ENOSYS;
218 if (bus->ioeventfd_started) {
219 return 0;
222 /* Only set our notifier if we have ownership. */
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;
230 bus->ioeventfd_started = true;
231 return 0;
234 void virtio_bus_stop_ioeventfd(VirtioBusState *bus)
236 VirtIODevice *vdev;
237 VirtioDeviceClass *vdc;
239 if (!bus->ioeventfd_started) {
240 return;
243 /* Only remove our notifier if we have ownership. */
244 if (!bus->ioeventfd_grabbed) {
245 vdev = virtio_bus_get_device(bus);
246 vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
247 vdc->stop_ioeventfd(vdev);
249 bus->ioeventfd_started = false;
252 bool virtio_bus_ioeventfd_enabled(VirtioBusState *bus)
254 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
255 DeviceState *proxy = DEVICE(BUS(bus)->parent);
257 return k->ioeventfd_assign && k->ioeventfd_enabled(proxy);
261 * This function switches ioeventfd on/off in the device.
262 * The caller must set or clear the handlers for the EventNotifier.
264 int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign)
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;
273 if (!k->ioeventfd_assign) {
274 return -ENOSYS;
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;
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);
289 } else {
290 k->ioeventfd_assign(proxy, notifier, n, false);
293 return r;
296 void virtio_bus_cleanup_host_notifier(VirtioBusState *bus, int n)
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);
302 /* Test and clear notifier after disabling event,
303 * in case poll callback didn't have time to run.
305 virtio_queue_host_notifier_read(notifier);
306 event_notifier_cleanup(notifier);
309 static char *virtio_bus_get_dev_path(DeviceState *dev)
311 BusState *bus = qdev_get_parent_bus(dev);
312 DeviceState *proxy = DEVICE(bus->parent);
313 return qdev_get_dev_path(proxy);
316 static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
318 return NULL;
321 static void virtio_bus_class_init(ObjectClass *klass, void *data)
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;
328 static 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
337 static void virtio_register_types(void)
339 type_register_static(&virtio_bus_info);
342 type_init(virtio_register_types)