virtio-bus.c 5.3 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/>.
 *
 */

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

/* #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

40 41
/* A VirtIODevice is being plugged */
int virtio_bus_device_plugged(VirtIODevice *vdev)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
{
    DeviceState *qdev = DEVICE(vdev);
    BusState *qbus = BUS(qdev_get_parent_bus(qdev));
    VirtioBusState *bus = VIRTIO_BUS(qbus);
    VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
    DPRINTF("%s: plug device.\n", qbus->name);

    if (klass->device_plugged != NULL) {
        klass->device_plugged(qbus->parent);
    }

    return 0;
}

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

61
    DPRINTF("%s: reset device.\n", qbus->name);
P
Paolo Bonzini 已提交
62 63
    if (vdev != NULL) {
        virtio_reset(vdev);
64 65 66
    }
}

67 68
/* A VirtIODevice is being unplugged */
void virtio_bus_device_unplugged(VirtIODevice *vdev)
69
{
70 71 72
    DeviceState *qdev = DEVICE(vdev);
    BusState *qbus = BUS(qdev_get_parent_bus(qdev));
    VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus);
P
Paolo Bonzini 已提交
73

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

P
Paolo Bonzini 已提交
76
    if (vdev != NULL) {
77 78
        if (klass->device_unplugged != NULL) {
            klass->device_unplugged(qbus->parent);
79 80 81 82 83 84 85
        }
    }
}

/* Get the device id of the plugged device. */
uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
{
P
Paolo Bonzini 已提交
86 87 88
    VirtIODevice *vdev = virtio_bus_get_device(bus);
    assert(vdev != NULL);
    return vdev->device_id;
89 90 91 92 93
}

/* Get the config_len field of the plugged device. */
size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
{
P
Paolo Bonzini 已提交
94 95 96
    VirtIODevice *vdev = virtio_bus_get_device(bus);
    assert(vdev != NULL);
    return vdev->config_len;
97 98
}

99 100 101 102
/* Get the features of the plugged device. */
uint32_t virtio_bus_get_vdev_features(VirtioBusState *bus,
                                    uint32_t requested_features)
{
P
Paolo Bonzini 已提交
103
    VirtIODevice *vdev = virtio_bus_get_device(bus);
104
    VirtioDeviceClass *k;
P
Paolo Bonzini 已提交
105 106 107

    assert(vdev != NULL);
    k = VIRTIO_DEVICE_GET_CLASS(vdev);
108
    assert(k->get_features != NULL);
P
Paolo Bonzini 已提交
109
    return k->get_features(vdev, requested_features);
110 111
}

K
KONRAD Frederic 已提交
112 113 114 115
/* Set the features of the plugged device. */
void virtio_bus_set_vdev_features(VirtioBusState *bus,
                                      uint32_t requested_features)
{
P
Paolo Bonzini 已提交
116
    VirtIODevice *vdev = virtio_bus_get_device(bus);
K
KONRAD Frederic 已提交
117
    VirtioDeviceClass *k;
P
Paolo Bonzini 已提交
118 119 120

    assert(vdev != NULL);
    k = VIRTIO_DEVICE_GET_CLASS(vdev);
K
KONRAD Frederic 已提交
121
    if (k->set_features != NULL) {
P
Paolo Bonzini 已提交
122
        k->set_features(vdev, requested_features);
K
KONRAD Frederic 已提交
123 124 125
    }
}

126 127 128
/* Get bad features of the plugged device. */
uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
{
P
Paolo Bonzini 已提交
129
    VirtIODevice *vdev = virtio_bus_get_device(bus);
130
    VirtioDeviceClass *k;
P
Paolo Bonzini 已提交
131 132 133

    assert(vdev != NULL);
    k = VIRTIO_DEVICE_GET_CLASS(vdev);
134
    if (k->bad_features != NULL) {
P
Paolo Bonzini 已提交
135
        return k->bad_features(vdev);
136 137 138 139 140 141 142 143
    } else {
        return 0;
    }
}

/* Get config of the plugged device. */
void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
{
P
Paolo Bonzini 已提交
144
    VirtIODevice *vdev = virtio_bus_get_device(bus);
145
    VirtioDeviceClass *k;
P
Paolo Bonzini 已提交
146 147 148

    assert(vdev != NULL);
    k = VIRTIO_DEVICE_GET_CLASS(vdev);
149
    if (k->get_config != NULL) {
P
Paolo Bonzini 已提交
150
        k->get_config(vdev, config);
151 152 153
    }
}

K
KONRAD Frederic 已提交
154 155 156
/* Set config of the plugged device. */
void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
{
P
Paolo Bonzini 已提交
157
    VirtIODevice *vdev = virtio_bus_get_device(bus);
K
KONRAD Frederic 已提交
158
    VirtioDeviceClass *k;
P
Paolo Bonzini 已提交
159 160 161

    assert(vdev != NULL);
    k = VIRTIO_DEVICE_GET_CLASS(vdev);
K
KONRAD Frederic 已提交
162
    if (k->set_config != NULL) {
P
Paolo Bonzini 已提交
163
        k->set_config(vdev, config);
K
KONRAD Frederic 已提交
164 165 166
    }
}

167 168 169 170 171 172 173
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);
}

174 175 176 177 178
static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
{
    return NULL;
}

179 180 181 182
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;
183
    bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path;
184 185
}

186 187 188 189 190 191
static const TypeInfo virtio_bus_info = {
    .name = TYPE_VIRTIO_BUS,
    .parent = TYPE_BUS,
    .instance_size = sizeof(VirtioBusState),
    .abstract = true,
    .class_size = sizeof(VirtioBusClass),
192
    .class_init = virtio_bus_class_init
193 194 195 196 197 198 199 200
};

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

type_init(virtio_register_types)