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 | |
Peter Maydell | 9b8bfe2 | 2016-01-26 18:17:07 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 26 | #include "qemu/error-report.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 27 | #include "qemu/module.h" |
Fam Zheng | a77690c | 2017-03-17 20:32:42 +0800 | [diff] [blame] | 28 | #include "qapi/error.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 29 | #include "hw/virtio/virtio-bus.h" |
| 30 | #include "hw/virtio/virtio.h" |
Jason Wang | 8607f5c | 2016-12-30 18:09:10 +0800 | [diff] [blame] | 31 | #include "exec/address-spaces.h" |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 32 | |
| 33 | /* #define DEBUG_VIRTIO_BUS */ |
| 34 | |
| 35 | #ifdef DEBUG_VIRTIO_BUS |
| 36 | #define DPRINTF(fmt, ...) \ |
| 37 | do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0) |
| 38 | #else |
| 39 | #define DPRINTF(fmt, ...) do { } while (0) |
| 40 | #endif |
| 41 | |
Paolo Bonzini | 5e96f5d | 2013-09-20 13:59:08 +0200 | [diff] [blame] | 42 | /* A VirtIODevice is being plugged */ |
Jason Wang | e839804 | 2015-05-29 14:15:25 +0800 | [diff] [blame] | 43 | void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp) |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 44 | { |
| 45 | DeviceState *qdev = DEVICE(vdev); |
| 46 | BusState *qbus = BUS(qdev_get_parent_bus(qdev)); |
| 47 | VirtioBusState *bus = VIRTIO_BUS(qbus); |
| 48 | VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus); |
Cornelia Huck | 6b8f102 | 2015-05-26 16:34:47 +0200 | [diff] [blame] | 49 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
Jason Wang | 2943b53 | 2017-01-17 12:01:00 +0800 | [diff] [blame] | 50 | bool has_iommu = virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM); |
Fam Zheng | a77690c | 2017-03-17 20:32:42 +0800 | [diff] [blame] | 51 | Error *local_err = NULL; |
Cornelia Huck | 6b8f102 | 2015-05-26 16:34:47 +0200 | [diff] [blame] | 52 | |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 53 | DPRINTF("%s: plug device.\n", qbus->name); |
| 54 | |
Maxime Coquelin | d1b4259 | 2016-09-13 15:30:30 +0200 | [diff] [blame] | 55 | if (klass->pre_plugged != NULL) { |
Fam Zheng | a77690c | 2017-03-17 20:32:42 +0800 | [diff] [blame] | 56 | klass->pre_plugged(qbus->parent, &local_err); |
| 57 | if (local_err) { |
| 58 | error_propagate(errp, local_err); |
| 59 | return; |
| 60 | } |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Cornelia Huck | 6b8f102 | 2015-05-26 16:34:47 +0200 | [diff] [blame] | 63 | /* Get the features of the plugged device. */ |
| 64 | assert(vdc->get_features != NULL); |
Jason Wang | 9d5b731 | 2015-07-27 17:49:19 +0800 | [diff] [blame] | 65 | vdev->host_features = vdc->get_features(vdev, vdev->host_features, |
Fam Zheng | a77690c | 2017-03-17 20:32:42 +0800 | [diff] [blame] | 66 | &local_err); |
| 67 | if (local_err) { |
| 68 | error_propagate(errp, local_err); |
| 69 | return; |
| 70 | } |
Maxime Coquelin | d1b4259 | 2016-09-13 15:30:30 +0200 | [diff] [blame] | 71 | |
| 72 | if (klass->device_plugged != NULL) { |
Fam Zheng | a77690c | 2017-03-17 20:32:42 +0800 | [diff] [blame] | 73 | klass->device_plugged(qbus->parent, &local_err); |
| 74 | } |
| 75 | if (local_err) { |
| 76 | error_propagate(errp, local_err); |
| 77 | return; |
Cornelia Huck | 11380b3 | 2015-12-02 18:31:57 +0100 | [diff] [blame] | 78 | } |
Jason Wang | 8607f5c | 2016-12-30 18:09:10 +0800 | [diff] [blame] | 79 | |
Jason Wang | 2943b53 | 2017-01-17 12:01:00 +0800 | [diff] [blame] | 80 | if (klass->get_dma_as != NULL && has_iommu) { |
| 81 | virtio_add_feature(&vdev->host_features, VIRTIO_F_IOMMU_PLATFORM); |
Jason Wang | 8607f5c | 2016-12-30 18:09:10 +0800 | [diff] [blame] | 82 | vdev->dma_as = klass->get_dma_as(qbus->parent); |
| 83 | } else { |
| 84 | vdev->dma_as = &address_space_memory; |
| 85 | } |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* Reset the virtio_bus */ |
| 89 | void virtio_bus_reset(VirtioBusState *bus) |
| 90 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 91 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
| 92 | |
Gonglei | 2c80ab1 | 2014-11-13 10:39:32 +0800 | [diff] [blame] | 93 | DPRINTF("%s: reset device.\n", BUS(bus)->name); |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 94 | if (vdev != NULL) { |
| 95 | virtio_reset(vdev); |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Paolo Bonzini | 5e96f5d | 2013-09-20 13:59:08 +0200 | [diff] [blame] | 99 | /* A VirtIODevice is being unplugged */ |
| 100 | void virtio_bus_device_unplugged(VirtIODevice *vdev) |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 101 | { |
Paolo Bonzini | 5e96f5d | 2013-09-20 13:59:08 +0200 | [diff] [blame] | 102 | DeviceState *qdev = DEVICE(vdev); |
| 103 | BusState *qbus = BUS(qdev_get_parent_bus(qdev)); |
| 104 | VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(qbus); |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 105 | |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 106 | DPRINTF("%s: remove device.\n", qbus->name); |
| 107 | |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 108 | if (vdev != NULL) { |
Paolo Bonzini | 5e96f5d | 2013-09-20 13:59:08 +0200 | [diff] [blame] | 109 | if (klass->device_unplugged != NULL) { |
| 110 | klass->device_unplugged(qbus->parent); |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 111 | } |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | /* Get the device id of the plugged device. */ |
| 116 | uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus) |
| 117 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 118 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
| 119 | assert(vdev != NULL); |
| 120 | return vdev->device_id; |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* Get the config_len field of the plugged device. */ |
| 124 | size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus) |
| 125 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 126 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
| 127 | assert(vdev != NULL); |
| 128 | return vdev->config_len; |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 129 | } |
| 130 | |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 131 | /* Get bad features of the plugged device. */ |
| 132 | uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus) |
| 133 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 134 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 135 | VirtioDeviceClass *k; |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 136 | |
| 137 | assert(vdev != NULL); |
| 138 | k = VIRTIO_DEVICE_GET_CLASS(vdev); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 139 | if (k->bad_features != NULL) { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 140 | return k->bad_features(vdev); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 141 | } else { |
| 142 | return 0; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* Get config of the plugged device. */ |
| 147 | void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config) |
| 148 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 149 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 150 | VirtioDeviceClass *k; |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 151 | |
| 152 | assert(vdev != NULL); |
| 153 | k = VIRTIO_DEVICE_GET_CLASS(vdev); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 154 | if (k->get_config != NULL) { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 155 | k->get_config(vdev, config); |
KONRAD Frederic | 8e05db9 | 2013-01-15 00:08:02 +0100 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
KONRAD Frederic | 5d448f9 | 2013-04-24 10:21:17 +0200 | [diff] [blame] | 159 | /* Set config of the plugged device. */ |
| 160 | void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config) |
| 161 | { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 162 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
KONRAD Frederic | 5d448f9 | 2013-04-24 10:21:17 +0200 | [diff] [blame] | 163 | VirtioDeviceClass *k; |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 164 | |
| 165 | assert(vdev != NULL); |
| 166 | k = VIRTIO_DEVICE_GET_CLASS(vdev); |
KONRAD Frederic | 5d448f9 | 2013-04-24 10:21:17 +0200 | [diff] [blame] | 167 | if (k->set_config != NULL) { |
Paolo Bonzini | 06d3dff | 2013-09-20 13:31:39 +0200 | [diff] [blame] | 168 | k->set_config(vdev, config); |
KONRAD Frederic | 5d448f9 | 2013-04-24 10:21:17 +0200 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Paolo Bonzini | 310837d | 2016-11-18 16:07:00 +0100 | [diff] [blame] | 172 | /* On success, ioeventfd ownership belongs to the caller. */ |
| 173 | int virtio_bus_grab_ioeventfd(VirtioBusState *bus) |
| 174 | { |
| 175 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus); |
| 176 | |
| 177 | /* vhost can be used even if ioeventfd=off in the proxy device, |
| 178 | * so do not check k->ioeventfd_enabled. |
| 179 | */ |
| 180 | if (!k->ioeventfd_assign) { |
| 181 | return -ENOSYS; |
| 182 | } |
| 183 | |
| 184 | if (bus->ioeventfd_grabbed == 0 && bus->ioeventfd_started) { |
| 185 | virtio_bus_stop_ioeventfd(bus); |
| 186 | /* Remember that we need to restart ioeventfd |
| 187 | * when ioeventfd_grabbed becomes zero. |
| 188 | */ |
| 189 | bus->ioeventfd_started = true; |
| 190 | } |
| 191 | bus->ioeventfd_grabbed++; |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | void virtio_bus_release_ioeventfd(VirtioBusState *bus) |
| 196 | { |
| 197 | assert(bus->ioeventfd_grabbed != 0); |
| 198 | if (--bus->ioeventfd_grabbed == 0 && bus->ioeventfd_started) { |
| 199 | /* Force virtio_bus_start_ioeventfd to act. */ |
| 200 | bus->ioeventfd_started = false; |
| 201 | virtio_bus_start_ioeventfd(bus); |
| 202 | } |
| 203 | } |
| 204 | |
Paolo Bonzini | ff4c07d | 2016-10-21 22:48:07 +0200 | [diff] [blame] | 205 | int virtio_bus_start_ioeventfd(VirtioBusState *bus) |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 206 | { |
| 207 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus); |
| 208 | DeviceState *proxy = DEVICE(BUS(bus)->parent); |
Paolo Bonzini | ff4c07d | 2016-10-21 22:48:07 +0200 | [diff] [blame] | 209 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
| 210 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
| 211 | int r; |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 212 | |
Paolo Bonzini | 8e93cef | 2016-10-21 22:48:08 +0200 | [diff] [blame] | 213 | if (!k->ioeventfd_assign || !k->ioeventfd_enabled(proxy)) { |
Paolo Bonzini | ff4c07d | 2016-10-21 22:48:07 +0200 | [diff] [blame] | 214 | return -ENOSYS; |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 215 | } |
Paolo Bonzini | e616c2f | 2016-10-21 22:48:13 +0200 | [diff] [blame] | 216 | if (bus->ioeventfd_started) { |
Paolo Bonzini | ff4c07d | 2016-10-21 22:48:07 +0200 | [diff] [blame] | 217 | return 0; |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 218 | } |
Paolo Bonzini | 310837d | 2016-11-18 16:07:00 +0100 | [diff] [blame] | 219 | |
| 220 | /* Only set our notifier if we have ownership. */ |
| 221 | if (!bus->ioeventfd_grabbed) { |
| 222 | r = vdc->start_ioeventfd(vdev); |
| 223 | if (r < 0) { |
| 224 | error_report("%s: failed. Fallback to userspace (slower).", __func__); |
| 225 | return r; |
| 226 | } |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 227 | } |
Paolo Bonzini | b13d396 | 2016-10-21 22:48:06 +0200 | [diff] [blame] | 228 | bus->ioeventfd_started = true; |
Paolo Bonzini | ff4c07d | 2016-10-21 22:48:07 +0200 | [diff] [blame] | 229 | return 0; |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void virtio_bus_stop_ioeventfd(VirtioBusState *bus) |
| 233 | { |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 234 | VirtIODevice *vdev; |
Paolo Bonzini | ff4c07d | 2016-10-21 22:48:07 +0200 | [diff] [blame] | 235 | VirtioDeviceClass *vdc; |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 236 | |
Paolo Bonzini | b13d396 | 2016-10-21 22:48:06 +0200 | [diff] [blame] | 237 | if (!bus->ioeventfd_started) { |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 238 | return; |
| 239 | } |
Paolo Bonzini | ff4c07d | 2016-10-21 22:48:07 +0200 | [diff] [blame] | 240 | |
Paolo Bonzini | 310837d | 2016-11-18 16:07:00 +0100 | [diff] [blame] | 241 | /* Only remove our notifier if we have ownership. */ |
| 242 | if (!bus->ioeventfd_grabbed) { |
| 243 | vdev = virtio_bus_get_device(bus); |
| 244 | vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
| 245 | vdc->stop_ioeventfd(vdev); |
| 246 | } |
Paolo Bonzini | b13d396 | 2016-10-21 22:48:06 +0200 | [diff] [blame] | 247 | bus->ioeventfd_started = false; |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Paolo Bonzini | 8e93cef | 2016-10-21 22:48:08 +0200 | [diff] [blame] | 250 | bool virtio_bus_ioeventfd_enabled(VirtioBusState *bus) |
| 251 | { |
| 252 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus); |
| 253 | DeviceState *proxy = DEVICE(BUS(bus)->parent); |
| 254 | |
| 255 | return k->ioeventfd_assign && k->ioeventfd_enabled(proxy); |
| 256 | } |
| 257 | |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 258 | /* |
Paolo Bonzini | e616c2f | 2016-10-21 22:48:13 +0200 | [diff] [blame] | 259 | * This function switches ioeventfd on/off in the device. |
| 260 | * The caller must set or clear the handlers for the EventNotifier. |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 261 | */ |
| 262 | int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign) |
| 263 | { |
Paolo Bonzini | 2bd3c31 | 2016-10-21 22:48:16 +0200 | [diff] [blame] | 264 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 265 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(bus); |
| 266 | DeviceState *proxy = DEVICE(BUS(bus)->parent); |
Paolo Bonzini | 2bd3c31 | 2016-10-21 22:48:16 +0200 | [diff] [blame] | 267 | VirtQueue *vq = virtio_get_queue(vdev, n); |
| 268 | EventNotifier *notifier = virtio_queue_get_host_notifier(vq); |
| 269 | int r = 0; |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 270 | |
Paolo Bonzini | b13d396 | 2016-10-21 22:48:06 +0200 | [diff] [blame] | 271 | if (!k->ioeventfd_assign) { |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 272 | return -ENOSYS; |
| 273 | } |
Paolo Bonzini | 2bd3c31 | 2016-10-21 22:48:16 +0200 | [diff] [blame] | 274 | |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 275 | if (assign) { |
Paolo Bonzini | 2bd3c31 | 2016-10-21 22:48:16 +0200 | [diff] [blame] | 276 | r = event_notifier_init(notifier, 1); |
| 277 | if (r < 0) { |
| 278 | error_report("%s: unable to init event notifier: %s (%d)", |
| 279 | __func__, strerror(-r), r); |
| 280 | return r; |
| 281 | } |
| 282 | r = k->ioeventfd_assign(proxy, notifier, n, true); |
| 283 | if (r < 0) { |
| 284 | error_report("%s: unable to assign ioeventfd: %d", __func__, r); |
Gal Hammer | 7614361 | 2018-01-29 16:20:56 +0200 | [diff] [blame] | 285 | virtio_bus_cleanup_host_notifier(bus, n); |
Paolo Bonzini | 2bd3c31 | 2016-10-21 22:48:16 +0200 | [diff] [blame] | 286 | } |
Paolo Bonzini | e616c2f | 2016-10-21 22:48:13 +0200 | [diff] [blame] | 287 | } else { |
Paolo Bonzini | 2bd3c31 | 2016-10-21 22:48:16 +0200 | [diff] [blame] | 288 | k->ioeventfd_assign(proxy, notifier, n, false); |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 289 | } |
Paolo Bonzini | 2bd3c31 | 2016-10-21 22:48:16 +0200 | [diff] [blame] | 290 | |
Stefan Hajnoczi | fcccb27 | 2019-11-05 15:09:46 +0100 | [diff] [blame] | 291 | if (r == 0) { |
| 292 | virtio_queue_set_host_notifier_enabled(vq, assign); |
| 293 | } |
| 294 | |
Gal Hammer | 7614361 | 2018-01-29 16:20:56 +0200 | [diff] [blame] | 295 | return r; |
| 296 | } |
| 297 | |
| 298 | void virtio_bus_cleanup_host_notifier(VirtioBusState *bus, int n) |
| 299 | { |
| 300 | VirtIODevice *vdev = virtio_bus_get_device(bus); |
| 301 | VirtQueue *vq = virtio_get_queue(vdev, n); |
| 302 | EventNotifier *notifier = virtio_queue_get_host_notifier(vq); |
| 303 | |
Michael S. Tsirkin | 1ef8185 | 2018-01-24 19:09:13 +0200 | [diff] [blame] | 304 | /* Test and clear notifier after disabling event, |
| 305 | * in case poll callback didn't have time to run. |
| 306 | */ |
| 307 | virtio_queue_host_notifier_read(notifier); |
| 308 | event_notifier_cleanup(notifier); |
Cornelia Huck | 6798e24 | 2016-06-10 11:04:09 +0200 | [diff] [blame] | 309 | } |
| 310 | |
KONRAD Frederic | 6d46895 | 2013-05-16 19:06:07 +0200 | [diff] [blame] | 311 | static char *virtio_bus_get_dev_path(DeviceState *dev) |
| 312 | { |
| 313 | BusState *bus = qdev_get_parent_bus(dev); |
| 314 | DeviceState *proxy = DEVICE(bus->parent); |
| 315 | return qdev_get_dev_path(proxy); |
| 316 | } |
| 317 | |
Amos Kong | bbfa18f | 2013-05-29 15:56:42 +0800 | [diff] [blame] | 318 | static char *virtio_bus_get_fw_dev_path(DeviceState *dev) |
| 319 | { |
| 320 | return NULL; |
| 321 | } |
| 322 | |
KONRAD Frederic | 6d46895 | 2013-05-16 19:06:07 +0200 | [diff] [blame] | 323 | static void virtio_bus_class_init(ObjectClass *klass, void *data) |
| 324 | { |
| 325 | BusClass *bus_class = BUS_CLASS(klass); |
| 326 | bus_class->get_dev_path = virtio_bus_get_dev_path; |
Amos Kong | bbfa18f | 2013-05-29 15:56:42 +0800 | [diff] [blame] | 327 | bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path; |
KONRAD Frederic | 6d46895 | 2013-05-16 19:06:07 +0200 | [diff] [blame] | 328 | } |
| 329 | |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 330 | static const TypeInfo virtio_bus_info = { |
| 331 | .name = TYPE_VIRTIO_BUS, |
| 332 | .parent = TYPE_BUS, |
| 333 | .instance_size = sizeof(VirtioBusState), |
| 334 | .abstract = true, |
| 335 | .class_size = sizeof(VirtioBusClass), |
KONRAD Frederic | 6d46895 | 2013-05-16 19:06:07 +0200 | [diff] [blame] | 336 | .class_init = virtio_bus_class_init |
KONRAD Frederic | ff8eca5 | 2013-01-15 00:08:01 +0100 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | static void virtio_register_types(void) |
| 340 | { |
| 341 | type_register_static(&virtio_bus_info); |
| 342 | } |
| 343 | |
| 344 | type_init(virtio_register_types) |