find_ram_offset: Align ram_addr_t allocation on long boundaries
[qemu/kevin.git] / hw / virtio / virtio-bus.c
blob3042232daf8db6e547053e2fe32528af11679ca6
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 "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"
34 /* #define DEBUG_VIRTIO_BUS */
36 #ifdef DEBUG_VIRTIO_BUS
37 #define DPRINTF(fmt, ...) \
38 do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
39 #else
40 #define DPRINTF(fmt, ...) do { } while (0)
41 #endif
43 /* A VirtIODevice is being plugged */
44 void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)
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;
54 DPRINTF("%s: plug device.\n", qbus->name);
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;
64 /* Get the features of the plugged device. */
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;
73 if (klass->device_plugged != NULL) {
74 klass->device_plugged(qbus->parent, &local_err);
76 if (local_err) {
77 error_propagate(errp, local_err);
78 return;
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;
89 /* Reset the virtio_bus */
90 void virtio_bus_reset(VirtioBusState *bus)
92 VirtIODevice *vdev = virtio_bus_get_device(bus);
94 DPRINTF("%s: reset device.\n", BUS(bus)->name);
95 if (vdev != NULL) {
96 virtio_reset(vdev);
100 /* A VirtIODevice is being unplugged */
101 void virtio_bus_device_unplugged(VirtIODevice *vdev)
103 DeviceState *qdev = DEVICE(vdev);
104 BusState *qbus = BUS(qdev_get_parent_bus(qdev));
105 VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus);
107 DPRINTF("%s: remove device.\n", qbus->name);
109 if (vdev != NULL) {
110 if (klass->device_unplugged != NULL) {
111 klass->device_unplugged(qbus->parent);
116 /* Get the device id of the plugged device. */
117 uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
119 VirtIODevice *vdev = virtio_bus_get_device(bus);
120 assert(vdev != NULL);
121 return vdev->device_id;
124 /* Get the config_len field of the plugged device. */
125 size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
127 VirtIODevice *vdev = virtio_bus_get_device(bus);
128 assert(vdev != NULL);
129 return vdev->config_len;
132 /* Get bad features of the plugged device. */
133 uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
135 VirtIODevice *vdev = virtio_bus_get_device(bus);
136 VirtioDeviceClass *k;
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;
147 /* Get config of the plugged device. */
148 void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
150 VirtIODevice *vdev = virtio_bus_get_device(bus);
151 VirtioDeviceClass *k;
153 assert(vdev != NULL);
154 k = VIRTIO_DEVICE_GET_CLASS(vdev);
155 if (k->get_config != NULL) {
156 k->get_config(vdev, config);
160 /* Set config of the plugged device. */
161 void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
163 VirtIODevice *vdev = virtio_bus_get_device(bus);
164 VirtioDeviceClass *k;
166 assert(vdev != NULL);
167 k = VIRTIO_DEVICE_GET_CLASS(vdev);
168 if (k->set_config != NULL) {
169 k->set_config(vdev, config);
173 /* On success, ioeventfd ownership belongs to the caller. */
174 int virtio_bus_grab_ioeventfd(VirtioBusState *bus)
176 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
178 /* vhost can be used even if ioeventfd=off in the proxy device,
179 * so do not check k->ioeventfd_enabled.
181 if (!k->ioeventfd_assign) {
182 return -ENOSYS;
185 if (bus->ioeventfd_grabbed == 0 && bus->ioeventfd_started) {
186 virtio_bus_stop_ioeventfd(bus);
187 /* Remember that we need to restart ioeventfd
188 * when ioeventfd_grabbed becomes zero.
190 bus->ioeventfd_started = true;
192 bus->ioeventfd_grabbed++;
193 return 0;
196 void virtio_bus_release_ioeventfd(VirtioBusState *bus)
198 assert(bus->ioeventfd_grabbed != 0);
199 if (--bus->ioeventfd_grabbed == 0 && bus->ioeventfd_started) {
200 /* Force virtio_bus_start_ioeventfd to act. */
201 bus->ioeventfd_started = false;
202 virtio_bus_start_ioeventfd(bus);
206 int virtio_bus_start_ioeventfd(VirtioBusState *bus)
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;
214 if (!k->ioeventfd_assign || !k->ioeventfd_enabled(proxy)) {
215 return -ENOSYS;
217 if (bus->ioeventfd_started) {
218 return 0;
221 /* Only set our notifier if we have ownership. */
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;
229 bus->ioeventfd_started = true;
230 return 0;
233 void virtio_bus_stop_ioeventfd(VirtioBusState *bus)
235 VirtIODevice *vdev;
236 VirtioDeviceClass *vdc;
238 if (!bus->ioeventfd_started) {
239 return;
242 /* Only remove our notifier if we have ownership. */
243 if (!bus->ioeventfd_grabbed) {
244 vdev = virtio_bus_get_device(bus);
245 vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
246 vdc->stop_ioeventfd(vdev);
248 bus->ioeventfd_started = false;
251 bool virtio_bus_ioeventfd_enabled(VirtioBusState *bus)
253 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
254 DeviceState *proxy = DEVICE(BUS(bus)->parent);
256 return k->ioeventfd_assign && k->ioeventfd_enabled(proxy);
260 * This function switches ioeventfd on/off in the device.
261 * The caller must set or clear the handlers for the EventNotifier.
263 int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign)
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;
272 if (!k->ioeventfd_assign) {
273 return -ENOSYS;
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;
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 goto cleanup_event_notifier;
288 return 0;
289 } else {
290 k->ioeventfd_assign(proxy, notifier, n, false);
293 cleanup_event_notifier:
294 /* Test and clear notifier after disabling event,
295 * in case poll callback didn't have time to run.
297 virtio_queue_host_notifier_read(notifier);
298 event_notifier_cleanup(notifier);
299 return r;
302 static char *virtio_bus_get_dev_path(DeviceState *dev)
304 BusState *bus = qdev_get_parent_bus(dev);
305 DeviceState *proxy = DEVICE(bus->parent);
306 return qdev_get_dev_path(proxy);
309 static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
311 return NULL;
314 static void virtio_bus_class_init(ObjectClass *klass, void *data)
316 BusClass *bus_class = BUS_CLASS(klass);
317 bus_class->get_dev_path = virtio_bus_get_dev_path;
318 bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path;
321 static const TypeInfo virtio_bus_info = {
322 .name = TYPE_VIRTIO_BUS,
323 .parent = TYPE_BUS,
324 .instance_size = sizeof(VirtioBusState),
325 .abstract = true,
326 .class_size = sizeof(VirtioBusClass),
327 .class_init = virtio_bus_class_init
330 static void virtio_register_types(void)
332 type_register_static(&virtio_bus_info);
335 type_init(virtio_register_types)