KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 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 | |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 25 | #include "hw/hw.h" |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 26 | #include "qemu/error-report.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 27 | #include "hw/qdev.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 28 | #include "hw/virtio/virtio-bus.h" |
| 29 | #include "hw/virtio/virtio.h" |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 30 | |
| 31 | /* #define DEBUG_VIRTIO_BUS */ |
| 32 | |
| 33 | #ifdef DEBUG_VIRTIO_BUS |
| 34 | #define DPRINTF(fmt, ...) \ |
| 35 | do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0) |
| 36 | #else |
| 37 | #define DPRINTF(fmt, ...) do { } while (0) |
| 38 | #endif |
| 39 | |
Paolo Bonzini | 5e96f5d | 2013-09-20 13:59:08 +0200 | [diff] [blame] | 40 | /* A VirtIODevice is being plugged */ |
| 41 | int virtio_bus_device_plugged(VirtIODevice *vdev) |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 42 | { |
| 43 | DeviceState *qdev = DEVICE(vdev); |
| 44 | BusState *qbus = BUS(qdev_get_parent_bus(qdev)); |
| 45 | VirtioBusState *bus = VIRTIO_BUS(qbus); |
| 46 | VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus); |
| 47 | DPRINTF("%s: plug device.\n", qbus->name); |
| 48 | |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 49 | if (klass->device_plugged != NULL) { |
| 50 | klass->device_plugged(qbus->parent); |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | /* Reset the virtio_bus */ |
| 57 | void virtio_bus_reset(VirtioBusState *bus) |
| 58 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 59 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
| 60 | |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 61 | DPRINTF("%s: reset device.\n", qbus->name); |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 62 | if (vdev != NULL) { |
| 63 | virtio_reset(vdev); |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Paolo Bonzini | 5e96f5d | 2013-09-20 13:59:08 +0200 | [diff] [blame] | 67 | /* A VirtIODevice is being unplugged */ |
| 68 | void virtio_bus_device_unplugged(VirtIODevice *vdev) |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 69 | { |
Paolo Bonzini | 5e96f5d | 2013-09-20 13:59:08 +0200 | [diff] [blame] | 70 | DeviceState *qdev = DEVICE(vdev); |
| 71 | BusState *qbus = BUS(qdev_get_parent_bus(qdev)); |
| 72 | VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus); |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 73 | |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 74 | DPRINTF("%s: remove device.\n", qbus->name); |
| 75 | |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 76 | if (vdev != NULL) { |
Paolo Bonzini | 5e96f5d | 2013-09-20 13:59:08 +0200 | [diff] [blame] | 77 | if (klass->device_unplugged != NULL) { |
| 78 | klass->device_unplugged(qbus->parent); |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 79 | } |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | /* Get the device id of the plugged device. */ |
| 84 | uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus) |
| 85 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 86 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
| 87 | assert(vdev != NULL); |
| 88 | return vdev->device_id; |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* Get the config_len field of the plugged device. */ |
| 92 | size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus) |
| 93 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 94 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
| 95 | assert(vdev != NULL); |
| 96 | return vdev->config_len; |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 97 | } |
| 98 | |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 99 | /* Get the features of the plugged device. */ |
| 100 | uint32_t virtio_bus_get_vdev_features(VirtioBusState *bus, |
| 101 | uint32_t requested_features) |
| 102 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 103 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 104 | VirtioDeviceClass *k; |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 105 | |
| 106 | assert(vdev != NULL); |
| 107 | k = VIRTIO_DEVICE_GET_CLASS(vdev); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 108 | assert(k->get_features != NULL); |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 109 | return k->get_features(vdev, requested_features); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 110 | } |
| 111 | |
KONRAD Frederic | 5d448f9 | 2013-04-24 10:21:17 +0200 | [diff] [blame] | 112 | /* Set the features of the plugged device. */ |
| 113 | void virtio_bus_set_vdev_features(VirtioBusState *bus, |
| 114 | uint32_t requested_features) |
| 115 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 116 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
KONRAD Frederic | 5d448f9 | 2013-04-24 10:21:17 +0200 | [diff] [blame] | 117 | VirtioDeviceClass *k; |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 118 | |
| 119 | assert(vdev != NULL); |
| 120 | k = VIRTIO_DEVICE_GET_CLASS(vdev); |
KONRAD Frederic | 5d448f9 | 2013-04-24 10:21:17 +0200 | [diff] [blame] | 121 | if (k->set_features != NULL) { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 122 | k->set_features(vdev, requested_features); |
KONRAD Frederic | 5d448f9 | 2013-04-24 10:21:17 +0200 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 126 | /* Get bad features of the plugged device. */ |
| 127 | uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus) |
| 128 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 129 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 130 | VirtioDeviceClass *k; |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 131 | |
| 132 | assert(vdev != NULL); |
| 133 | k = VIRTIO_DEVICE_GET_CLASS(vdev); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 134 | if (k->bad_features != NULL) { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 135 | return k->bad_features(vdev); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 136 | } else { |
| 137 | return 0; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* Get config of the plugged device. */ |
| 142 | void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config) |
| 143 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 144 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 145 | VirtioDeviceClass *k; |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 146 | |
| 147 | assert(vdev != NULL); |
| 148 | k = VIRTIO_DEVICE_GET_CLASS(vdev); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 149 | if (k->get_config != NULL) { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 150 | k->get_config(vdev, config); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
KONRAD Frederic | 5d448f9 | 2013-04-24 10:21:17 +0200 | [diff] [blame] | 154 | /* Set config of the plugged device. */ |
| 155 | void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config) |
| 156 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 157 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
KONRAD Frederic | 5d448f9 | 2013-04-24 10:21:17 +0200 | [diff] [blame] | 158 | VirtioDeviceClass *k; |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 159 | |
| 160 | assert(vdev != NULL); |
| 161 | k = VIRTIO_DEVICE_GET_CLASS(vdev); |
KONRAD Frederic | 5d448f9 | 2013-04-24 10:21:17 +0200 | [diff] [blame] | 162 | if (k->set_config != NULL) { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 163 | k->set_config(vdev, config); |
KONRAD Frederic | 5d448f9 | 2013-04-24 10:21:17 +0200 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
KONRAD Frederic | 6d46895 | 2013-05-16 19:06:07 +0200 | [diff] [blame] | 167 | static char *virtio_bus_get_dev_path(DeviceState *dev) |
| 168 | { |
| 169 | BusState *bus = qdev_get_parent_bus(dev); |
| 170 | DeviceState *proxy = DEVICE(bus->parent); |
| 171 | return qdev_get_dev_path(proxy); |
| 172 | } |
| 173 | |
Amos Kong | bbfa18f | 2013-05-29 15:56:42 +0800 | [diff] [blame] | 174 | static char *virtio_bus_get_fw_dev_path(DeviceState *dev) |
| 175 | { |
| 176 | return NULL; |
| 177 | } |
| 178 | |
KONRAD Frederic | 6d46895 | 2013-05-16 19:06:07 +0200 | [diff] [blame] | 179 | static void virtio_bus_class_init(ObjectClass *klass, void *data) |
| 180 | { |
| 181 | BusClass *bus_class = BUS_CLASS(klass); |
| 182 | bus_class->get_dev_path = virtio_bus_get_dev_path; |
Amos Kong | bbfa18f | 2013-05-29 15:56:42 +0800 | [diff] [blame] | 183 | bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path; |
KONRAD Frederic | 6d46895 | 2013-05-16 19:06:07 +0200 | [diff] [blame] | 184 | } |
| 185 | |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 186 | static const TypeInfo virtio_bus_info = { |
| 187 | .name = TYPE_VIRTIO_BUS, |
| 188 | .parent = TYPE_BUS, |
| 189 | .instance_size = sizeof(VirtioBusState), |
| 190 | .abstract = true, |
| 191 | .class_size = sizeof(VirtioBusClass), |
KONRAD Frederic | 6d46895 | 2013-05-16 19:06:07 +0200 | [diff] [blame] | 192 | .class_init = virtio_bus_class_init |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | static void virtio_register_types(void) |
| 196 | { |
| 197 | type_register_static(&virtio_bus_info); |
| 198 | } |
| 199 | |
| 200 | type_init(virtio_register_types) |