virtio-bus.c 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * VirtioBus
 *
 *  Copyright (C) 2012 : GreenSocs Ltd
 *      http://www.greensocs.com/ , email: info@greensocs.com
 *
 *  Developed by :
 *  Frederic Konrad   <fred.konrad@greensocs.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

P
Peter Maydell 已提交
25
#include "qemu/osdep.h"
26
#include "hw/hw.h"
27
#include "qemu/error-report.h"
28
#include "hw/qdev.h"
P
Paolo Bonzini 已提交
29 30
#include "hw/virtio/virtio-bus.h"
#include "hw/virtio/virtio.h"
31 32 33 34 35 36 37 38 39 40

/* #define DEBUG_VIRTIO_BUS */

#ifdef DEBUG_VIRTIO_BUS
#define DPRINTF(fmt, ...) \
do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
#else
#define DPRINTF(fmt, ...) do { } while (0)
#endif

41
/* A VirtIODevice is being plugged */
J
Jason Wang 已提交
42
void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)
43 44 45 46 47
{
    DeviceState *qdev = DEVICE(vdev);
    BusState *qbus = BUS(qdev_get_parent_bus(qdev));
    VirtioBusState *bus = VIRTIO_BUS(qbus);
    VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
C
Cornelia Huck 已提交
48 49
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);

50 51
    DPRINTF("%s: plug device.\n", qbus->name);

52 53
    if (klass->pre_plugged != NULL) {
        klass->pre_plugged(qbus->parent, errp);
54 55
    }

C
Cornelia Huck 已提交
56 57
    /* Get the features of the plugged device. */
    assert(vdc->get_features != NULL);
J
Jason Wang 已提交
58 59
    vdev->host_features = vdc->get_features(vdev, vdev->host_features,
                                            errp);
60 61 62

    if (klass->device_plugged != NULL) {
        klass->device_plugged(qbus->parent, errp);
63
    }
64 65 66 67 68
}

/* Reset the virtio_bus */
void virtio_bus_reset(VirtioBusState *bus)
{
P
Paolo Bonzini 已提交
69 70
    VirtIODevice *vdev = virtio_bus_get_device(bus);

71
    DPRINTF("%s: reset device.\n", BUS(bus)->name);
P
Paolo Bonzini 已提交
72 73
    if (vdev != NULL) {
        virtio_reset(vdev);
74 75 76
    }
}

77 78
/* A VirtIODevice is being unplugged */
void virtio_bus_device_unplugged(VirtIODevice *vdev)
79
{
80 81 82
    DeviceState *qdev = DEVICE(vdev);
    BusState *qbus = BUS(qdev_get_parent_bus(qdev));
    VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus);
P
Paolo Bonzini 已提交
83

84 85
    DPRINTF("%s: remove device.\n", qbus->name);

P
Paolo Bonzini 已提交
86
    if (vdev != NULL) {
87 88
        if (klass->device_unplugged != NULL) {
            klass->device_unplugged(qbus->parent);
89 90 91 92 93 94 95
        }
    }
}

/* Get the device id of the plugged device. */
uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
{
P
Paolo Bonzini 已提交
96 97 98
    VirtIODevice *vdev = virtio_bus_get_device(bus);
    assert(vdev != NULL);
    return vdev->device_id;
99 100 101 102 103
}

/* Get the config_len field of the plugged device. */
size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
{
P
Paolo Bonzini 已提交
104 105 106
    VirtIODevice *vdev = virtio_bus_get_device(bus);
    assert(vdev != NULL);
    return vdev->config_len;
107 108
}

109 110 111
/* Get bad features of the plugged device. */
uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
{
P
Paolo Bonzini 已提交
112
    VirtIODevice *vdev = virtio_bus_get_device(bus);
113
    VirtioDeviceClass *k;
P
Paolo Bonzini 已提交
114 115 116

    assert(vdev != NULL);
    k = VIRTIO_DEVICE_GET_CLASS(vdev);
117
    if (k->bad_features != NULL) {
P
Paolo Bonzini 已提交
118
        return k->bad_features(vdev);
119 120 121 122 123 124 125 126
    } else {
        return 0;
    }
}

/* Get config of the plugged device. */
void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
{
P
Paolo Bonzini 已提交
127
    VirtIODevice *vdev = virtio_bus_get_device(bus);
128
    VirtioDeviceClass *k;
P
Paolo Bonzini 已提交
129 130 131

    assert(vdev != NULL);
    k = VIRTIO_DEVICE_GET_CLASS(vdev);
132
    if (k->get_config != NULL) {
P
Paolo Bonzini 已提交
133
        k->get_config(vdev, config);
134 135 136
    }
}

K
KONRAD Frederic 已提交
137 138 139
/* Set config of the plugged device. */
void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
{
P
Paolo Bonzini 已提交
140
    VirtIODevice *vdev = virtio_bus_get_device(bus);
K
KONRAD Frederic 已提交
141
    VirtioDeviceClass *k;
P
Paolo Bonzini 已提交
142 143 144

    assert(vdev != NULL);
    k = VIRTIO_DEVICE_GET_CLASS(vdev);
K
KONRAD Frederic 已提交
145
    if (k->set_config != NULL) {
P
Paolo Bonzini 已提交
146
        k->set_config(vdev, config);
K
KONRAD Frederic 已提交
147 148 149
    }
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/*
 * This function handles both assigning the ioeventfd handler and
 * registering it with the kernel.
 * assign: register/deregister ioeventfd with the kernel
 * set_handler: use the generic ioeventfd handler
 */
static int set_host_notifier_internal(DeviceState *proxy, VirtioBusState *bus,
                                      int n, bool assign, bool set_handler)
{
    VirtIODevice *vdev = virtio_bus_get_device(bus);
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
    VirtQueue *vq = virtio_get_queue(vdev, n);
    EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
    int r = 0;

    if (assign) {
        r = event_notifier_init(notifier, 1);
        if (r < 0) {
168 169
            error_report("%s: unable to init event notifier: %s (%d)",
                         __func__, strerror(-r), r);
170 171 172 173 174 175 176 177 178 179 180 181
            return r;
        }
        virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
        r = k->ioeventfd_assign(proxy, notifier, n, assign);
        if (r < 0) {
            error_report("%s: unable to assign ioeventfd: %d", __func__, r);
            virtio_queue_set_host_notifier_fd_handler(vq, false, false);
            event_notifier_cleanup(notifier);
            return r;
        }
    } else {
        k->ioeventfd_assign(proxy, notifier, n, assign);
182
        virtio_queue_set_host_notifier_fd_handler(vq, false, false);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        event_notifier_cleanup(notifier);
    }
    return r;
}

void virtio_bus_start_ioeventfd(VirtioBusState *bus)
{
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
    DeviceState *proxy = DEVICE(BUS(bus)->parent);
    VirtIODevice *vdev;
    int n, r;

    if (!k->ioeventfd_started || k->ioeventfd_started(proxy)) {
        return;
    }
    if (k->ioeventfd_disabled(proxy)) {
        return;
    }
    vdev = virtio_bus_get_device(bus);
    for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
        if (!virtio_queue_get_num(vdev, n)) {
            continue;
        }
        r = set_host_notifier_internal(proxy, bus, n, true, true);
        if (r < 0) {
            goto assign_error;
        }
    }
    k->ioeventfd_set_started(proxy, true, false);
    return;

assign_error:
    while (--n >= 0) {
        if (!virtio_queue_get_num(vdev, n)) {
            continue;
        }

        r = set_host_notifier_internal(proxy, bus, n, false, false);
        assert(r >= 0);
    }
    k->ioeventfd_set_started(proxy, false, true);
    error_report("%s: failed. Fallback to userspace (slower).", __func__);
}

void virtio_bus_stop_ioeventfd(VirtioBusState *bus)
{
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
    DeviceState *proxy = DEVICE(BUS(bus)->parent);
    VirtIODevice *vdev;
    int n, r;

    if (!k->ioeventfd_started || !k->ioeventfd_started(proxy)) {
        return;
    }
    vdev = virtio_bus_get_device(bus);
    for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
        if (!virtio_queue_get_num(vdev, n)) {
            continue;
        }
        r = set_host_notifier_internal(proxy, bus, n, false, false);
        assert(r >= 0);
    }
    k->ioeventfd_set_started(proxy, false, false);
}

/*
 * This function switches from/to the generic ioeventfd handler.
 * assign==false means 'use generic ioeventfd handler'.
 */
int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign)
{
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus);
    DeviceState *proxy = DEVICE(BUS(bus)->parent);

    if (!k->ioeventfd_started) {
        return -ENOSYS;
    }
260
    k->ioeventfd_set_disabled(proxy, assign);
261 262 263 264
    if (assign) {
        /*
         * Stop using the generic ioeventfd, we are doing eventfd handling
         * ourselves below
265 266 267 268 269 270
         *
         * FIXME: We should just switch the handler and not deassign the
         * ioeventfd.
         * Otherwise, there's a window where we don't have an
         * ioeventfd and we may end up with a notification where
         * we don't expect one.
271
         */
272
        virtio_bus_stop_ioeventfd(bus);
273
    }
274
    return set_host_notifier_internal(proxy, bus, n, assign, false);
275 276
}

277 278 279 280 281 282 283
static char *virtio_bus_get_dev_path(DeviceState *dev)
{
    BusState *bus = qdev_get_parent_bus(dev);
    DeviceState *proxy = DEVICE(bus->parent);
    return qdev_get_dev_path(proxy);
}

284 285 286 287 288
static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
{
    return NULL;
}

289 290 291 292
static void virtio_bus_class_init(ObjectClass *klass, void *data)
{
    BusClass *bus_class = BUS_CLASS(klass);
    bus_class->get_dev_path = virtio_bus_get_dev_path;
293
    bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path;
294 295
}

296 297 298 299 300 301
static const TypeInfo virtio_bus_info = {
    .name = TYPE_VIRTIO_BUS,
    .parent = TYPE_BUS,
    .instance_size = sizeof(VirtioBusState),
    .abstract = true,
    .class_size = sizeof(VirtioBusClass),
302
    .class_init = virtio_bus_class_init
303 304 305 306 307 308 309 310
};

static void virtio_register_types(void)
{
    type_register_static(&virtio_bus_info);
}

type_init(virtio_register_types)