Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Virtio PCI Bindings |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2007 |
| 5 | * Copyright (c) 2009 CodeSourcery |
| 6 | * |
| 7 | * Authors: |
| 8 | * Anthony Liguori <aliguori@us.ibm.com> |
| 9 | * Paul Brook <paul@codesourcery.com> |
| 10 | * |
| 11 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 12 | * the COPYING file in the top-level directory. |
| 13 | * |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 14 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 15 | * GNU GPL, version 2 or (at your option) any later version. |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <inttypes.h> |
| 19 | |
| 20 | #include "virtio.h" |
Michael S. Tsirkin | 8172539 | 2010-01-10 13:52:53 +0200 | [diff] [blame] | 21 | #include "virtio-blk.h" |
| 22 | #include "virtio-net.h" |
Amit Shah | 6b331ef | 2011-02-03 11:22:32 +0530 | [diff] [blame] | 23 | #include "virtio-serial.h" |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 24 | #include "pci.h" |
Markus Armbruster | 2f79201 | 2010-02-18 16:24:31 +0100 | [diff] [blame] | 25 | #include "qemu-error.h" |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 26 | #include "msix.h" |
Gerd Hoffmann | a1e0fea | 2009-07-15 13:48:25 +0200 | [diff] [blame] | 27 | #include "net.h" |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 28 | #include "loader.h" |
Michael S. Tsirkin | ade80dc | 2010-03-17 13:08:13 +0200 | [diff] [blame] | 29 | #include "kvm.h" |
Blue Swirl | 2446333 | 2010-08-24 15:22:24 +0000 | [diff] [blame] | 30 | #include "blockdev.h" |
Aneesh Kumar K.V | 9fe1ebe | 2011-06-01 12:35:13 +0530 | [diff] [blame] | 31 | #include "virtio-pci.h" |
Michael S. Tsirkin | 1129714 | 2011-07-27 11:08:20 +0300 | [diff] [blame] | 32 | #include "range.h" |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 33 | |
| 34 | /* from Linux's linux/virtio_pci.h */ |
| 35 | |
| 36 | /* A 32-bit r/o bitmask of the features supported by the host */ |
| 37 | #define VIRTIO_PCI_HOST_FEATURES 0 |
| 38 | |
| 39 | /* A 32-bit r/w bitmask of features activated by the guest */ |
| 40 | #define VIRTIO_PCI_GUEST_FEATURES 4 |
| 41 | |
| 42 | /* A 32-bit r/w PFN for the currently selected queue */ |
| 43 | #define VIRTIO_PCI_QUEUE_PFN 8 |
| 44 | |
| 45 | /* A 16-bit r/o queue size for the currently selected queue */ |
| 46 | #define VIRTIO_PCI_QUEUE_NUM 12 |
| 47 | |
| 48 | /* A 16-bit r/w queue selector */ |
| 49 | #define VIRTIO_PCI_QUEUE_SEL 14 |
| 50 | |
| 51 | /* A 16-bit r/w queue notifier */ |
| 52 | #define VIRTIO_PCI_QUEUE_NOTIFY 16 |
| 53 | |
| 54 | /* An 8-bit device status register. */ |
| 55 | #define VIRTIO_PCI_STATUS 18 |
| 56 | |
| 57 | /* An 8-bit r/o interrupt status register. Reading the value will return the |
| 58 | * current contents of the ISR and will also clear it. This is effectively |
| 59 | * a read-and-acknowledge. */ |
| 60 | #define VIRTIO_PCI_ISR 19 |
| 61 | |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 62 | /* MSI-X registers: only enabled if MSI-X is enabled. */ |
| 63 | /* A 16-bit vector for configuration changes. */ |
| 64 | #define VIRTIO_MSI_CONFIG_VECTOR 20 |
| 65 | /* A 16-bit vector for selected queue notifications. */ |
| 66 | #define VIRTIO_MSI_QUEUE_VECTOR 22 |
| 67 | |
| 68 | /* Config space size */ |
| 69 | #define VIRTIO_PCI_CONFIG_NOMSI 20 |
| 70 | #define VIRTIO_PCI_CONFIG_MSI 24 |
| 71 | #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \ |
| 72 | VIRTIO_PCI_CONFIG_MSI : \ |
| 73 | VIRTIO_PCI_CONFIG_NOMSI) |
| 74 | |
| 75 | /* The remaining space is defined by each driver as the per-driver |
| 76 | * configuration space */ |
| 77 | #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \ |
| 78 | VIRTIO_PCI_CONFIG_MSI : \ |
| 79 | VIRTIO_PCI_CONFIG_NOMSI) |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 80 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 81 | /* How many bits to shift physical queue address written to QUEUE_PFN. |
| 82 | * 12 is historical, and due to x86 page size. */ |
| 83 | #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12 |
| 84 | |
Stefan Hajnoczi | 3dbca8e | 2010-12-17 12:01:49 +0000 | [diff] [blame] | 85 | /* Flags track per-device state like workarounds for quirks in older guests. */ |
| 86 | #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0) |
Alexander Graf | c81131d | 2010-03-16 19:18:07 +0100 | [diff] [blame] | 87 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 88 | /* QEMU doesn't strictly need write barriers since everything runs in |
| 89 | * lock-step. We'll leave the calls to wmb() in though to make it obvious for |
| 90 | * KVM or if kqemu gets SMP support. |
| 91 | */ |
| 92 | #define wmb() do { } while (0) |
| 93 | |
Benjamin Herrenschmidt | 82afa58 | 2012-01-10 01:35:11 +0000 | [diff] [blame] | 94 | /* HACK for virtio to determine if it's running a big endian guest */ |
| 95 | bool virtio_is_big_endian(void); |
| 96 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 97 | /* virtio device */ |
| 98 | |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 99 | static void virtio_pci_notify(void *opaque, uint16_t vector) |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 100 | { |
| 101 | VirtIOPCIProxy *proxy = opaque; |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 102 | if (msix_enabled(&proxy->pci_dev)) |
| 103 | msix_notify(&proxy->pci_dev, vector); |
| 104 | else |
| 105 | qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1); |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 108 | static void virtio_pci_save_config(void * opaque, QEMUFile *f) |
| 109 | { |
| 110 | VirtIOPCIProxy *proxy = opaque; |
| 111 | pci_device_save(&proxy->pci_dev, f); |
| 112 | msix_save(&proxy->pci_dev, f); |
| 113 | if (msix_present(&proxy->pci_dev)) |
| 114 | qemu_put_be16(f, proxy->vdev->config_vector); |
| 115 | } |
| 116 | |
| 117 | static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f) |
| 118 | { |
| 119 | VirtIOPCIProxy *proxy = opaque; |
| 120 | if (msix_present(&proxy->pci_dev)) |
| 121 | qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n)); |
| 122 | } |
| 123 | |
| 124 | static int virtio_pci_load_config(void * opaque, QEMUFile *f) |
| 125 | { |
| 126 | VirtIOPCIProxy *proxy = opaque; |
| 127 | int ret; |
| 128 | ret = pci_device_load(&proxy->pci_dev, f); |
Michael S. Tsirkin | e6da768 | 2009-07-05 16:02:34 +0300 | [diff] [blame] | 129 | if (ret) { |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 130 | return ret; |
Michael S. Tsirkin | e6da768 | 2009-07-05 16:02:34 +0300 | [diff] [blame] | 131 | } |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 132 | msix_load(&proxy->pci_dev, f); |
Michael S. Tsirkin | e6da768 | 2009-07-05 16:02:34 +0300 | [diff] [blame] | 133 | if (msix_present(&proxy->pci_dev)) { |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 134 | qemu_get_be16s(f, &proxy->vdev->config_vector); |
Michael S. Tsirkin | e6da768 | 2009-07-05 16:02:34 +0300 | [diff] [blame] | 135 | } else { |
| 136 | proxy->vdev->config_vector = VIRTIO_NO_VECTOR; |
| 137 | } |
| 138 | if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) { |
| 139 | return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector); |
| 140 | } |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f) |
| 145 | { |
| 146 | VirtIOPCIProxy *proxy = opaque; |
| 147 | uint16_t vector; |
Michael S. Tsirkin | e6da768 | 2009-07-05 16:02:34 +0300 | [diff] [blame] | 148 | if (msix_present(&proxy->pci_dev)) { |
| 149 | qemu_get_be16s(f, &vector); |
| 150 | } else { |
| 151 | vector = VIRTIO_NO_VECTOR; |
| 152 | } |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 153 | virtio_queue_set_vector(proxy->vdev, n, vector); |
Michael S. Tsirkin | e6da768 | 2009-07-05 16:02:34 +0300 | [diff] [blame] | 154 | if (vector != VIRTIO_NO_VECTOR) { |
| 155 | return msix_vector_use(&proxy->pci_dev, vector); |
| 156 | } |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 160 | static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy, |
| 161 | int n, bool assign) |
| 162 | { |
| 163 | VirtQueue *vq = virtio_get_queue(proxy->vdev, n); |
| 164 | EventNotifier *notifier = virtio_queue_get_host_notifier(vq); |
Avi Kivity | da146d0 | 2011-08-08 16:09:13 +0300 | [diff] [blame] | 165 | int r = 0; |
| 166 | |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 167 | if (assign) { |
| 168 | r = event_notifier_init(notifier, 1); |
| 169 | if (r < 0) { |
Michael S. Tsirkin | b36e391 | 2011-01-11 14:10:15 +0200 | [diff] [blame] | 170 | error_report("%s: unable to init event notifier: %d", |
| 171 | __func__, r); |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 172 | return r; |
| 173 | } |
Avi Kivity | da146d0 | 2011-08-08 16:09:13 +0300 | [diff] [blame] | 174 | memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, |
| 175 | true, n, event_notifier_get_fd(notifier)); |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 176 | } else { |
Avi Kivity | da146d0 | 2011-08-08 16:09:13 +0300 | [diff] [blame] | 177 | memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, |
| 178 | true, n, event_notifier_get_fd(notifier)); |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 179 | /* Handle the race condition where the guest kicked and we deassigned |
| 180 | * before we got around to handling the kick. |
| 181 | */ |
| 182 | if (event_notifier_test_and_clear(notifier)) { |
| 183 | virtio_queue_notify_vq(vq); |
| 184 | } |
| 185 | |
| 186 | event_notifier_cleanup(notifier); |
| 187 | } |
| 188 | return r; |
| 189 | } |
| 190 | |
| 191 | static void virtio_pci_host_notifier_read(void *opaque) |
| 192 | { |
| 193 | VirtQueue *vq = opaque; |
| 194 | EventNotifier *n = virtio_queue_get_host_notifier(vq); |
| 195 | if (event_notifier_test_and_clear(n)) { |
| 196 | virtio_queue_notify_vq(vq); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy, |
| 201 | int n, bool assign) |
| 202 | { |
| 203 | VirtQueue *vq = virtio_get_queue(proxy->vdev, n); |
| 204 | EventNotifier *notifier = virtio_queue_get_host_notifier(vq); |
| 205 | if (assign) { |
| 206 | qemu_set_fd_handler(event_notifier_get_fd(notifier), |
| 207 | virtio_pci_host_notifier_read, NULL, vq); |
| 208 | } else { |
| 209 | qemu_set_fd_handler(event_notifier_get_fd(notifier), |
| 210 | NULL, NULL, NULL); |
| 211 | } |
| 212 | } |
| 213 | |
Michael S. Tsirkin | b36e391 | 2011-01-11 14:10:15 +0200 | [diff] [blame] | 214 | static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy) |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 215 | { |
| 216 | int n, r; |
| 217 | |
| 218 | if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) || |
| 219 | proxy->ioeventfd_disabled || |
| 220 | proxy->ioeventfd_started) { |
Michael S. Tsirkin | b36e391 | 2011-01-11 14:10:15 +0200 | [diff] [blame] | 221 | return; |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { |
| 225 | if (!virtio_queue_get_num(proxy->vdev, n)) { |
| 226 | continue; |
| 227 | } |
| 228 | |
| 229 | r = virtio_pci_set_host_notifier_internal(proxy, n, true); |
| 230 | if (r < 0) { |
| 231 | goto assign_error; |
| 232 | } |
| 233 | |
| 234 | virtio_pci_set_host_notifier_fd_handler(proxy, n, true); |
| 235 | } |
| 236 | proxy->ioeventfd_started = true; |
Michael S. Tsirkin | b36e391 | 2011-01-11 14:10:15 +0200 | [diff] [blame] | 237 | return; |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 238 | |
| 239 | assign_error: |
| 240 | while (--n >= 0) { |
| 241 | if (!virtio_queue_get_num(proxy->vdev, n)) { |
| 242 | continue; |
| 243 | } |
| 244 | |
| 245 | virtio_pci_set_host_notifier_fd_handler(proxy, n, false); |
Michael S. Tsirkin | b36e391 | 2011-01-11 14:10:15 +0200 | [diff] [blame] | 246 | r = virtio_pci_set_host_notifier_internal(proxy, n, false); |
| 247 | assert(r >= 0); |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 248 | } |
| 249 | proxy->ioeventfd_started = false; |
Michael S. Tsirkin | b36e391 | 2011-01-11 14:10:15 +0200 | [diff] [blame] | 250 | error_report("%s: failed. Fallback to a userspace (slower).", __func__); |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Michael S. Tsirkin | b36e391 | 2011-01-11 14:10:15 +0200 | [diff] [blame] | 253 | static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy) |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 254 | { |
Michael S. Tsirkin | b36e391 | 2011-01-11 14:10:15 +0200 | [diff] [blame] | 255 | int r; |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 256 | int n; |
| 257 | |
| 258 | if (!proxy->ioeventfd_started) { |
Michael S. Tsirkin | b36e391 | 2011-01-11 14:10:15 +0200 | [diff] [blame] | 259 | return; |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { |
| 263 | if (!virtio_queue_get_num(proxy->vdev, n)) { |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | virtio_pci_set_host_notifier_fd_handler(proxy, n, false); |
Michael S. Tsirkin | b36e391 | 2011-01-11 14:10:15 +0200 | [diff] [blame] | 268 | r = virtio_pci_set_host_notifier_internal(proxy, n, false); |
| 269 | assert(r >= 0); |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 270 | } |
| 271 | proxy->ioeventfd_started = false; |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Aneesh Kumar K.V | 8798d6c | 2011-12-04 22:35:28 +0530 | [diff] [blame] | 274 | void virtio_pci_reset(DeviceState *d) |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 275 | { |
Michael S. Tsirkin | e489030 | 2009-09-16 13:40:37 +0300 | [diff] [blame] | 276 | VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev); |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 277 | virtio_pci_stop_ioeventfd(proxy); |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 278 | virtio_reset(proxy->vdev); |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 279 | msix_reset(&proxy->pci_dev); |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 280 | proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 281 | } |
| 282 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 283 | static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
| 284 | { |
| 285 | VirtIOPCIProxy *proxy = opaque; |
| 286 | VirtIODevice *vdev = proxy->vdev; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 287 | target_phys_addr_t pa; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 288 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 289 | switch (addr) { |
| 290 | case VIRTIO_PCI_GUEST_FEATURES: |
| 291 | /* Guest does not negotiate properly? We have to assume nothing. */ |
| 292 | if (val & (1 << VIRTIO_F_BAD_FEATURE)) { |
Paolo Bonzini | ad0c933 | 2011-11-24 13:28:52 +0100 | [diff] [blame] | 293 | val = vdev->bad_features ? vdev->bad_features(vdev) : 0; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 294 | } |
Paolo Bonzini | ad0c933 | 2011-11-24 13:28:52 +0100 | [diff] [blame] | 295 | virtio_set_features(vdev, val); |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 296 | break; |
| 297 | case VIRTIO_PCI_QUEUE_PFN: |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 298 | pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
Michael S. Tsirkin | 1b8e9b2 | 2009-11-24 16:45:35 +0200 | [diff] [blame] | 299 | if (pa == 0) { |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 300 | virtio_pci_stop_ioeventfd(proxy); |
Michael S. Tsirkin | 1b8e9b2 | 2009-11-24 16:45:35 +0200 | [diff] [blame] | 301 | virtio_reset(proxy->vdev); |
| 302 | msix_unuse_all_vectors(&proxy->pci_dev); |
| 303 | } |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 304 | else |
| 305 | virtio_queue_set_addr(vdev, vdev->queue_sel, pa); |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 306 | break; |
| 307 | case VIRTIO_PCI_QUEUE_SEL: |
| 308 | if (val < VIRTIO_PCI_QUEUE_MAX) |
| 309 | vdev->queue_sel = val; |
| 310 | break; |
| 311 | case VIRTIO_PCI_QUEUE_NOTIFY: |
Stefan Hajnoczi | 7157e2e | 2011-05-08 22:29:07 +0100 | [diff] [blame] | 312 | if (val < VIRTIO_PCI_QUEUE_MAX) { |
| 313 | virtio_queue_notify(vdev, val); |
| 314 | } |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 315 | break; |
| 316 | case VIRTIO_PCI_STATUS: |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 317 | if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { |
| 318 | virtio_pci_stop_ioeventfd(proxy); |
| 319 | } |
| 320 | |
Michael S. Tsirkin | 3e607cb | 2010-03-17 13:08:05 +0200 | [diff] [blame] | 321 | virtio_set_status(vdev, val & 0xFF); |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 322 | |
| 323 | if (val & VIRTIO_CONFIG_S_DRIVER_OK) { |
| 324 | virtio_pci_start_ioeventfd(proxy); |
| 325 | } |
| 326 | |
Michael S. Tsirkin | 1b8e9b2 | 2009-11-24 16:45:35 +0200 | [diff] [blame] | 327 | if (vdev->status == 0) { |
| 328 | virtio_reset(proxy->vdev); |
| 329 | msix_unuse_all_vectors(&proxy->pci_dev); |
| 330 | } |
Alexander Graf | c81131d | 2010-03-16 19:18:07 +0100 | [diff] [blame] | 331 | |
| 332 | /* Linux before 2.6.34 sets the device as OK without enabling |
| 333 | the PCI device bus master bit. In this case we need to disable |
| 334 | some safety checks. */ |
| 335 | if ((val & VIRTIO_CONFIG_S_DRIVER_OK) && |
| 336 | !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { |
Stefan Hajnoczi | 3dbca8e | 2010-12-17 12:01:49 +0000 | [diff] [blame] | 337 | proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
Alexander Graf | c81131d | 2010-03-16 19:18:07 +0100 | [diff] [blame] | 338 | } |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 339 | break; |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 340 | case VIRTIO_MSI_CONFIG_VECTOR: |
| 341 | msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); |
| 342 | /* Make it possible for guest to discover an error took place. */ |
| 343 | if (msix_vector_use(&proxy->pci_dev, val) < 0) |
| 344 | val = VIRTIO_NO_VECTOR; |
| 345 | vdev->config_vector = val; |
| 346 | break; |
| 347 | case VIRTIO_MSI_QUEUE_VECTOR: |
| 348 | msix_vector_unuse(&proxy->pci_dev, |
| 349 | virtio_queue_vector(vdev, vdev->queue_sel)); |
| 350 | /* Make it possible for guest to discover an error took place. */ |
| 351 | if (msix_vector_use(&proxy->pci_dev, val) < 0) |
| 352 | val = VIRTIO_NO_VECTOR; |
| 353 | virtio_queue_set_vector(vdev, vdev->queue_sel, val); |
| 354 | break; |
| 355 | default: |
Stefan Hajnoczi | 4e02d46 | 2010-11-15 20:44:38 +0000 | [diff] [blame] | 356 | error_report("%s: unexpected address 0x%x value 0x%x", |
| 357 | __func__, addr, val); |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 358 | break; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 362 | static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr) |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 363 | { |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 364 | VirtIODevice *vdev = proxy->vdev; |
| 365 | uint32_t ret = 0xFFFFFFFF; |
| 366 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 367 | switch (addr) { |
| 368 | case VIRTIO_PCI_HOST_FEATURES: |
Michael S. Tsirkin | 8172539 | 2010-01-10 13:52:53 +0200 | [diff] [blame] | 369 | ret = proxy->host_features; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 370 | break; |
| 371 | case VIRTIO_PCI_GUEST_FEATURES: |
Michael S. Tsirkin | 704a76f | 2010-01-10 13:52:47 +0200 | [diff] [blame] | 372 | ret = vdev->guest_features; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 373 | break; |
| 374 | case VIRTIO_PCI_QUEUE_PFN: |
| 375 | ret = virtio_queue_get_addr(vdev, vdev->queue_sel) |
| 376 | >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
| 377 | break; |
| 378 | case VIRTIO_PCI_QUEUE_NUM: |
| 379 | ret = virtio_queue_get_num(vdev, vdev->queue_sel); |
| 380 | break; |
| 381 | case VIRTIO_PCI_QUEUE_SEL: |
| 382 | ret = vdev->queue_sel; |
| 383 | break; |
| 384 | case VIRTIO_PCI_STATUS: |
| 385 | ret = vdev->status; |
| 386 | break; |
| 387 | case VIRTIO_PCI_ISR: |
| 388 | /* reading from the ISR also clears it. */ |
| 389 | ret = vdev->isr; |
| 390 | vdev->isr = 0; |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 391 | qemu_set_irq(proxy->pci_dev.irq[0], 0); |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 392 | break; |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 393 | case VIRTIO_MSI_CONFIG_VECTOR: |
| 394 | ret = vdev->config_vector; |
| 395 | break; |
| 396 | case VIRTIO_MSI_QUEUE_VECTOR: |
| 397 | ret = virtio_queue_vector(vdev, vdev->queue_sel); |
| 398 | break; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 399 | default: |
| 400 | break; |
| 401 | } |
| 402 | |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr) |
| 407 | { |
| 408 | VirtIOPCIProxy *proxy = opaque; |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 409 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 410 | if (addr < config) |
| 411 | return virtio_ioport_read(proxy, addr); |
| 412 | addr -= config; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 413 | return virtio_config_readb(proxy->vdev, addr); |
| 414 | } |
| 415 | |
| 416 | static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr) |
| 417 | { |
| 418 | VirtIOPCIProxy *proxy = opaque; |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 419 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
Benjamin Herrenschmidt | 82afa58 | 2012-01-10 01:35:11 +0000 | [diff] [blame] | 420 | uint16_t val; |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 421 | if (addr < config) |
| 422 | return virtio_ioport_read(proxy, addr); |
| 423 | addr -= config; |
Benjamin Herrenschmidt | 82afa58 | 2012-01-10 01:35:11 +0000 | [diff] [blame] | 424 | val = virtio_config_readw(proxy->vdev, addr); |
| 425 | if (virtio_is_big_endian()) { |
| 426 | /* |
| 427 | * virtio is odd, ioports are LE but config space is target native |
| 428 | * endian. However, in qemu, all PIO is LE, so we need to re-swap |
| 429 | * on BE targets |
| 430 | */ |
| 431 | val = bswap16(val); |
| 432 | } |
| 433 | return val; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr) |
| 437 | { |
| 438 | VirtIOPCIProxy *proxy = opaque; |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 439 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
Benjamin Herrenschmidt | 82afa58 | 2012-01-10 01:35:11 +0000 | [diff] [blame] | 440 | uint32_t val; |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 441 | if (addr < config) |
| 442 | return virtio_ioport_read(proxy, addr); |
| 443 | addr -= config; |
Benjamin Herrenschmidt | 82afa58 | 2012-01-10 01:35:11 +0000 | [diff] [blame] | 444 | val = virtio_config_readl(proxy->vdev, addr); |
| 445 | if (virtio_is_big_endian()) { |
| 446 | val = bswap32(val); |
| 447 | } |
| 448 | return val; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val) |
| 452 | { |
| 453 | VirtIOPCIProxy *proxy = opaque; |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 454 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 455 | if (addr < config) { |
| 456 | virtio_ioport_write(proxy, addr, val); |
| 457 | return; |
| 458 | } |
| 459 | addr -= config; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 460 | virtio_config_writeb(proxy->vdev, addr, val); |
| 461 | } |
| 462 | |
| 463 | static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val) |
| 464 | { |
| 465 | VirtIOPCIProxy *proxy = opaque; |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 466 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 467 | if (addr < config) { |
| 468 | virtio_ioport_write(proxy, addr, val); |
| 469 | return; |
| 470 | } |
| 471 | addr -= config; |
Benjamin Herrenschmidt | 82afa58 | 2012-01-10 01:35:11 +0000 | [diff] [blame] | 472 | if (virtio_is_big_endian()) { |
| 473 | val = bswap16(val); |
| 474 | } |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 475 | virtio_config_writew(proxy->vdev, addr, val); |
| 476 | } |
| 477 | |
| 478 | static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val) |
| 479 | { |
| 480 | VirtIOPCIProxy *proxy = opaque; |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 481 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 482 | if (addr < config) { |
| 483 | virtio_ioport_write(proxy, addr, val); |
| 484 | return; |
| 485 | } |
| 486 | addr -= config; |
Benjamin Herrenschmidt | 82afa58 | 2012-01-10 01:35:11 +0000 | [diff] [blame] | 487 | if (virtio_is_big_endian()) { |
| 488 | val = bswap32(val); |
| 489 | } |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 490 | virtio_config_writel(proxy->vdev, addr, val); |
| 491 | } |
| 492 | |
Avi Kivity | da146d0 | 2011-08-08 16:09:13 +0300 | [diff] [blame] | 493 | const MemoryRegionPortio virtio_portio[] = { |
| 494 | { 0, 0x10000, 1, .write = virtio_pci_config_writeb, }, |
| 495 | { 0, 0x10000, 2, .write = virtio_pci_config_writew, }, |
| 496 | { 0, 0x10000, 4, .write = virtio_pci_config_writel, }, |
| 497 | { 0, 0x10000, 1, .read = virtio_pci_config_readb, }, |
| 498 | { 0, 0x10000, 2, .read = virtio_pci_config_readw, }, |
| 499 | { 0, 0x10000, 4, .read = virtio_pci_config_readl, }, |
| 500 | PORTIO_END_OF_LIST() |
| 501 | }; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 502 | |
Avi Kivity | da146d0 | 2011-08-08 16:09:13 +0300 | [diff] [blame] | 503 | static const MemoryRegionOps virtio_pci_config_ops = { |
| 504 | .old_portio = virtio_portio, |
| 505 | .endianness = DEVICE_LITTLE_ENDIAN, |
| 506 | }; |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 507 | |
| 508 | static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, |
| 509 | uint32_t val, int len) |
| 510 | { |
Yan Vugenfirer | ed757e1 | 2009-09-08 10:49:41 -0400 | [diff] [blame] | 511 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
| 512 | |
Michael S. Tsirkin | 1129714 | 2011-07-27 11:08:20 +0300 | [diff] [blame] | 513 | pci_default_write_config(pci_dev, address, val, len); |
| 514 | |
| 515 | if (range_covers_byte(address, len, PCI_COMMAND) && |
| 516 | !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) && |
| 517 | !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) { |
| 518 | virtio_pci_stop_ioeventfd(proxy); |
| 519 | virtio_set_status(proxy->vdev, |
| 520 | proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); |
Yan Vugenfirer | ed757e1 | 2009-09-08 10:49:41 -0400 | [diff] [blame] | 521 | } |
| 522 | |
Michael S. Tsirkin | 8535247 | 2009-09-22 13:35:28 +0300 | [diff] [blame] | 523 | msix_write_config(pci_dev, address, val, len); |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 524 | } |
| 525 | |
Michael S. Tsirkin | 6d74ca5 | 2009-12-08 20:07:48 +0200 | [diff] [blame] | 526 | static unsigned virtio_pci_get_features(void *opaque) |
| 527 | { |
Michael S. Tsirkin | 8172539 | 2010-01-10 13:52:53 +0200 | [diff] [blame] | 528 | VirtIOPCIProxy *proxy = opaque; |
| 529 | return proxy->host_features; |
Michael S. Tsirkin | 6d74ca5 | 2009-12-08 20:07:48 +0200 | [diff] [blame] | 530 | } |
| 531 | |
Michael S. Tsirkin | ade80dc | 2010-03-17 13:08:13 +0200 | [diff] [blame] | 532 | static void virtio_pci_guest_notifier_read(void *opaque) |
| 533 | { |
| 534 | VirtQueue *vq = opaque; |
| 535 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); |
| 536 | if (event_notifier_test_and_clear(n)) { |
| 537 | virtio_irq(vq); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign) |
| 542 | { |
| 543 | VirtIOPCIProxy *proxy = opaque; |
| 544 | VirtQueue *vq = virtio_get_queue(proxy->vdev, n); |
| 545 | EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); |
| 546 | |
| 547 | if (assign) { |
| 548 | int r = event_notifier_init(notifier, 0); |
| 549 | if (r < 0) { |
| 550 | return r; |
| 551 | } |
| 552 | qemu_set_fd_handler(event_notifier_get_fd(notifier), |
| 553 | virtio_pci_guest_notifier_read, NULL, vq); |
| 554 | } else { |
| 555 | qemu_set_fd_handler(event_notifier_get_fd(notifier), |
| 556 | NULL, NULL, NULL); |
| 557 | event_notifier_cleanup(notifier); |
| 558 | } |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
mst@redhat.com | 5430a28 | 2011-02-01 22:13:42 +0200 | [diff] [blame] | 563 | static bool virtio_pci_query_guest_notifiers(void *opaque) |
| 564 | { |
| 565 | VirtIOPCIProxy *proxy = opaque; |
| 566 | return msix_enabled(&proxy->pci_dev); |
| 567 | } |
| 568 | |
Michael S. Tsirkin | 54dd932 | 2010-10-06 15:20:17 +0200 | [diff] [blame] | 569 | static int virtio_pci_set_guest_notifiers(void *opaque, bool assign) |
| 570 | { |
| 571 | VirtIOPCIProxy *proxy = opaque; |
| 572 | VirtIODevice *vdev = proxy->vdev; |
| 573 | int r, n; |
| 574 | |
| 575 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { |
| 576 | if (!virtio_queue_get_num(vdev, n)) { |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | r = virtio_pci_set_guest_notifier(opaque, n, assign); |
| 581 | if (r < 0) { |
| 582 | goto assign_error; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | return 0; |
| 587 | |
| 588 | assign_error: |
| 589 | /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */ |
| 590 | while (--n >= 0) { |
| 591 | virtio_pci_set_guest_notifier(opaque, n, !assign); |
| 592 | } |
| 593 | return r; |
| 594 | } |
| 595 | |
Michael S. Tsirkin | ade80dc | 2010-03-17 13:08:13 +0200 | [diff] [blame] | 596 | static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign) |
| 597 | { |
| 598 | VirtIOPCIProxy *proxy = opaque; |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 599 | |
| 600 | /* Stop using ioeventfd for virtqueue kick if the device starts using host |
| 601 | * notifiers. This makes it easy to avoid stepping on each others' toes. |
| 602 | */ |
| 603 | proxy->ioeventfd_disabled = assign; |
Michael S. Tsirkin | ade80dc | 2010-03-17 13:08:13 +0200 | [diff] [blame] | 604 | if (assign) { |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 605 | virtio_pci_stop_ioeventfd(proxy); |
Michael S. Tsirkin | ade80dc | 2010-03-17 13:08:13 +0200 | [diff] [blame] | 606 | } |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 607 | /* We don't need to start here: it's not needed because backend |
| 608 | * currently only stops on status change away from ok, |
| 609 | * reset, vmstop and such. If we do add code to start here, |
| 610 | * need to check vmstate, device state etc. */ |
| 611 | return virtio_pci_set_host_notifier_internal(proxy, n, assign); |
| 612 | } |
| 613 | |
| 614 | static void virtio_pci_vmstate_change(void *opaque, bool running) |
| 615 | { |
| 616 | VirtIOPCIProxy *proxy = opaque; |
| 617 | |
| 618 | if (running) { |
Michael S. Tsirkin | 89c473f | 2011-03-19 19:28:19 +0200 | [diff] [blame] | 619 | /* Try to find out if the guest has bus master disabled, but is |
| 620 | in ready state. Then we have a buggy guest OS. */ |
| 621 | if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) && |
| 622 | !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { |
| 623 | proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
| 624 | } |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 625 | virtio_pci_start_ioeventfd(proxy); |
| 626 | } else { |
| 627 | virtio_pci_stop_ioeventfd(proxy); |
| 628 | } |
Michael S. Tsirkin | ade80dc | 2010-03-17 13:08:13 +0200 | [diff] [blame] | 629 | } |
| 630 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 631 | static const VirtIOBindings virtio_pci_bindings = { |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 632 | .notify = virtio_pci_notify, |
| 633 | .save_config = virtio_pci_save_config, |
| 634 | .load_config = virtio_pci_load_config, |
| 635 | .save_queue = virtio_pci_save_queue, |
| 636 | .load_queue = virtio_pci_load_queue, |
Michael S. Tsirkin | 6d74ca5 | 2009-12-08 20:07:48 +0200 | [diff] [blame] | 637 | .get_features = virtio_pci_get_features, |
mst@redhat.com | 5430a28 | 2011-02-01 22:13:42 +0200 | [diff] [blame] | 638 | .query_guest_notifiers = virtio_pci_query_guest_notifiers, |
Michael S. Tsirkin | ade80dc | 2010-03-17 13:08:13 +0200 | [diff] [blame] | 639 | .set_host_notifier = virtio_pci_set_host_notifier, |
Michael S. Tsirkin | 54dd932 | 2010-10-06 15:20:17 +0200 | [diff] [blame] | 640 | .set_guest_notifiers = virtio_pci_set_guest_notifiers, |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 641 | .vmstate_change = virtio_pci_vmstate_change, |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 642 | }; |
| 643 | |
Michael S. Tsirkin | befeac4 | 2011-06-14 17:51:11 +0300 | [diff] [blame] | 644 | void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev) |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 645 | { |
| 646 | uint8_t *config; |
| 647 | uint32_t size; |
| 648 | |
| 649 | proxy->vdev = vdev; |
| 650 | |
| 651 | config = proxy->pci_dev.config; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 652 | |
Isaku Yamahata | e75ccf2 | 2011-05-25 10:58:36 +0900 | [diff] [blame] | 653 | if (proxy->class_code) { |
| 654 | pci_config_set_class(config, proxy->class_code); |
| 655 | } |
Hui Kai Ran | ad3d11e | 2011-12-08 13:49:13 +0800 | [diff] [blame] | 656 | pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID, |
| 657 | pci_get_word(config + PCI_VENDOR_ID)); |
| 658 | pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id); |
| 659 | config[PCI_INTERRUPT_PIN] = 1; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 660 | |
Avi Kivity | 95524ae | 2011-08-08 16:09:26 +0300 | [diff] [blame] | 661 | memory_region_init(&proxy->msix_bar, "virtio-msix", 4096); |
| 662 | if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, |
| 663 | &proxy->msix_bar, 1, 0)) { |
Avi Kivity | e824b2c | 2011-08-08 16:09:31 +0300 | [diff] [blame] | 664 | pci_register_bar(&proxy->pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, |
| 665 | &proxy->msix_bar); |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 666 | } else |
| 667 | vdev->nvectors = 0; |
| 668 | |
Yan Vugenfirer | ed757e1 | 2009-09-08 10:49:41 -0400 | [diff] [blame] | 669 | proxy->pci_dev.config_write = virtio_write_config; |
| 670 | |
Michael S. Tsirkin | aba800a | 2009-06-21 19:50:26 +0300 | [diff] [blame] | 671 | size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 672 | if (size & (size-1)) |
| 673 | size = 1 << qemu_fls(size); |
| 674 | |
Avi Kivity | da146d0 | 2011-08-08 16:09:13 +0300 | [diff] [blame] | 675 | memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy, |
| 676 | "virtio-pci", size); |
Avi Kivity | e824b2c | 2011-08-08 16:09:31 +0300 | [diff] [blame] | 677 | pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, |
| 678 | &proxy->bar); |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 679 | |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 680 | if (!kvm_has_many_ioeventfds()) { |
| 681 | proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; |
| 682 | } |
| 683 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 684 | virtio_bind_device(vdev, &virtio_pci_bindings, proxy); |
Michael S. Tsirkin | 8172539 | 2010-01-10 13:52:53 +0200 | [diff] [blame] | 685 | proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY; |
| 686 | proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE; |
| 687 | proxy->host_features = vdev->get_features(vdev, proxy->host_features); |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 690 | static int virtio_blk_init_pci(PCIDevice *pci_dev) |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 691 | { |
| 692 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
| 693 | VirtIODevice *vdev; |
| 694 | |
Mark McLoughlin | 85c2c73 | 2009-07-30 11:29:13 +0100 | [diff] [blame] | 695 | if (proxy->class_code != PCI_CLASS_STORAGE_SCSI && |
| 696 | proxy->class_code != PCI_CLASS_STORAGE_OTHER) |
| 697 | proxy->class_code = PCI_CLASS_STORAGE_SCSI; |
| 698 | |
Markus Armbruster | a8686a9 | 2011-06-20 11:35:18 +0200 | [diff] [blame] | 699 | vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block, |
| 700 | &proxy->block_serial); |
Markus Armbruster | ac0c14d | 2010-07-06 14:37:42 +0200 | [diff] [blame] | 701 | if (!vdev) { |
| 702 | return -1; |
| 703 | } |
Gerd Hoffmann | 177539e | 2009-08-12 12:47:24 +0200 | [diff] [blame] | 704 | vdev->nvectors = proxy->nvectors; |
Isaku Yamahata | e75ccf2 | 2011-05-25 10:58:36 +0900 | [diff] [blame] | 705 | virtio_init_pci(proxy, vdev); |
Gerd Hoffmann | 177539e | 2009-08-12 12:47:24 +0200 | [diff] [blame] | 706 | /* make the actual value visible */ |
| 707 | proxy->nvectors = vdev->nvectors; |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 708 | return 0; |
Mark McLoughlin | 21d58b5 | 2009-07-07 12:09:58 +0100 | [diff] [blame] | 709 | } |
| 710 | |
Michael S. Tsirkin | 0f457d9 | 2009-10-05 23:02:20 +0200 | [diff] [blame] | 711 | static int virtio_exit_pci(PCIDevice *pci_dev) |
| 712 | { |
Avi Kivity | da146d0 | 2011-08-08 16:09:13 +0300 | [diff] [blame] | 713 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
Avi Kivity | 95524ae | 2011-08-08 16:09:26 +0300 | [diff] [blame] | 714 | int r; |
Avi Kivity | da146d0 | 2011-08-08 16:09:13 +0300 | [diff] [blame] | 715 | |
| 716 | memory_region_destroy(&proxy->bar); |
Avi Kivity | 95524ae | 2011-08-08 16:09:26 +0300 | [diff] [blame] | 717 | r = msix_uninit(pci_dev, &proxy->msix_bar); |
| 718 | memory_region_destroy(&proxy->msix_bar); |
| 719 | return r; |
Michael S. Tsirkin | 0f457d9 | 2009-10-05 23:02:20 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Gerd Hoffmann | 56a1493 | 2009-09-25 21:42:46 +0200 | [diff] [blame] | 722 | static int virtio_blk_exit_pci(PCIDevice *pci_dev) |
| 723 | { |
| 724 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
| 725 | |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 726 | virtio_pci_stop_ioeventfd(proxy); |
Alex Williamson | 9d0d313 | 2010-07-20 11:14:22 -0600 | [diff] [blame] | 727 | virtio_blk_exit(proxy->vdev); |
Markus Armbruster | f8b6cc0 | 2010-05-05 16:36:52 +0200 | [diff] [blame] | 728 | blockdev_mark_auto_del(proxy->block.bs); |
Michael S. Tsirkin | 0f457d9 | 2009-10-05 23:02:20 +0200 | [diff] [blame] | 729 | return virtio_exit_pci(pci_dev); |
Gerd Hoffmann | 56a1493 | 2009-09-25 21:42:46 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 732 | static int virtio_serial_init_pci(PCIDevice *pci_dev) |
Mark McLoughlin | 21d58b5 | 2009-07-07 12:09:58 +0100 | [diff] [blame] | 733 | { |
Gerd Hoffmann | d6beee9 | 2009-07-15 13:48:24 +0200 | [diff] [blame] | 734 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
Mark McLoughlin | 85c2c73 | 2009-07-30 11:29:13 +0100 | [diff] [blame] | 735 | VirtIODevice *vdev; |
| 736 | |
Gerd Hoffmann | d6beee9 | 2009-07-15 13:48:24 +0200 | [diff] [blame] | 737 | if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER && |
| 738 | proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */ |
| 739 | proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */ |
| 740 | proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER; |
| 741 | |
Amit Shah | 6b331ef | 2011-02-03 11:22:32 +0530 | [diff] [blame] | 742 | vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial); |
Amit Shah | 25fe365 | 2009-09-29 15:51:04 +0530 | [diff] [blame] | 743 | if (!vdev) { |
| 744 | return -1; |
| 745 | } |
Amit Shah | 573fb60 | 2010-02-25 17:24:44 +0530 | [diff] [blame] | 746 | vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED |
Amit Shah | 6b331ef | 2011-02-03 11:22:32 +0530 | [diff] [blame] | 747 | ? proxy->serial.max_virtserial_ports + 1 |
Amit Shah | 573fb60 | 2010-02-25 17:24:44 +0530 | [diff] [blame] | 748 | : proxy->nvectors; |
Isaku Yamahata | e75ccf2 | 2011-05-25 10:58:36 +0900 | [diff] [blame] | 749 | virtio_init_pci(proxy, vdev); |
Amit Shah | a182920 | 2010-01-20 00:36:58 +0530 | [diff] [blame] | 750 | proxy->nvectors = vdev->nvectors; |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 751 | return 0; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 752 | } |
| 753 | |
Amit Shah | 8b53a86 | 2010-08-19 06:51:04 +0530 | [diff] [blame] | 754 | static int virtio_serial_exit_pci(PCIDevice *pci_dev) |
| 755 | { |
| 756 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
| 757 | |
Amit Shah | 3205922 | 2011-02-17 12:31:10 +0530 | [diff] [blame] | 758 | virtio_pci_stop_ioeventfd(proxy); |
Amit Shah | 8b53a86 | 2010-08-19 06:51:04 +0530 | [diff] [blame] | 759 | virtio_serial_exit(proxy->vdev); |
| 760 | return virtio_exit_pci(pci_dev); |
| 761 | } |
| 762 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 763 | static int virtio_net_init_pci(PCIDevice *pci_dev) |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 764 | { |
| 765 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
| 766 | VirtIODevice *vdev; |
| 767 | |
Alex Williamson | f0c07c7 | 2010-09-02 09:00:50 -0600 | [diff] [blame] | 768 | vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net); |
Gerd Hoffmann | a1e0fea | 2009-07-15 13:48:25 +0200 | [diff] [blame] | 769 | |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 770 | vdev->nvectors = proxy->nvectors; |
Isaku Yamahata | e75ccf2 | 2011-05-25 10:58:36 +0900 | [diff] [blame] | 771 | virtio_init_pci(proxy, vdev); |
Gerd Hoffmann | a1e0fea | 2009-07-15 13:48:25 +0200 | [diff] [blame] | 772 | |
| 773 | /* make the actual value visible */ |
| 774 | proxy->nvectors = vdev->nvectors; |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 775 | return 0; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 776 | } |
| 777 | |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 778 | static int virtio_net_exit_pci(PCIDevice *pci_dev) |
| 779 | { |
| 780 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
| 781 | |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 782 | virtio_pci_stop_ioeventfd(proxy); |
Gerd Hoffmann | 97b1562 | 2009-10-21 15:25:35 +0200 | [diff] [blame] | 783 | virtio_net_exit(proxy->vdev); |
| 784 | return virtio_exit_pci(pci_dev); |
| 785 | } |
| 786 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 787 | static int virtio_balloon_init_pci(PCIDevice *pci_dev) |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 788 | { |
| 789 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
| 790 | VirtIODevice *vdev; |
| 791 | |
| 792 | vdev = virtio_balloon_init(&pci_dev->qdev); |
Amit Shah | f76f665 | 2011-07-27 12:29:33 +0530 | [diff] [blame] | 793 | if (!vdev) { |
| 794 | return -1; |
| 795 | } |
Isaku Yamahata | e75ccf2 | 2011-05-25 10:58:36 +0900 | [diff] [blame] | 796 | virtio_init_pci(proxy, vdev); |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 797 | return 0; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 798 | } |
| 799 | |
Amit Shah | 855d7e2 | 2011-07-27 13:50:41 +0530 | [diff] [blame] | 800 | static int virtio_balloon_exit_pci(PCIDevice *pci_dev) |
| 801 | { |
| 802 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
| 803 | |
| 804 | virtio_pci_stop_ioeventfd(proxy); |
| 805 | virtio_balloon_exit(proxy->vdev); |
| 806 | return virtio_exit_pci(pci_dev); |
| 807 | } |
| 808 | |
Anthony Liguori | e855761 | 2011-12-06 19:32:44 -0600 | [diff] [blame] | 809 | static PCIDeviceInfo virtio_blk_info = { |
| 810 | .qdev.name = "virtio-blk-pci", |
| 811 | .qdev.alias = "virtio-blk", |
| 812 | .qdev.size = sizeof(VirtIOPCIProxy), |
| 813 | .init = virtio_blk_init_pci, |
| 814 | .exit = virtio_blk_exit_pci, |
| 815 | .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
| 816 | .device_id = PCI_DEVICE_ID_VIRTIO_BLOCK, |
| 817 | .revision = VIRTIO_PCI_ABI_VERSION, |
| 818 | .class_id = PCI_CLASS_STORAGE_SCSI, |
| 819 | .qdev.props = (Property[]) { |
| 820 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), |
| 821 | DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block), |
| 822 | DEFINE_PROP_STRING("serial", VirtIOPCIProxy, block_serial), |
| 823 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), |
| 824 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), |
| 825 | DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features), |
| 826 | DEFINE_PROP_END_OF_LIST(), |
| 827 | }, |
| 828 | .qdev.reset = virtio_pci_reset, |
| 829 | }; |
| 830 | |
| 831 | static PCIDeviceInfo virtio_net_info = { |
| 832 | .qdev.name = "virtio-net-pci", |
| 833 | .qdev.alias = "virtio-net", |
| 834 | .qdev.size = sizeof(VirtIOPCIProxy), |
| 835 | .init = virtio_net_init_pci, |
| 836 | .exit = virtio_net_exit_pci, |
| 837 | .romfile = "pxe-virtio.rom", |
| 838 | .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
| 839 | .device_id = PCI_DEVICE_ID_VIRTIO_NET, |
| 840 | .revision = VIRTIO_PCI_ABI_VERSION, |
| 841 | .class_id = PCI_CLASS_NETWORK_ETHERNET, |
| 842 | .qdev.props = (Property[]) { |
| 843 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false), |
| 844 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), |
| 845 | DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features), |
| 846 | DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic), |
| 847 | DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy, net.txtimer, TX_TIMER_INTERVAL), |
| 848 | DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy, net.txburst, TX_BURST), |
| 849 | DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx), |
| 850 | DEFINE_PROP_END_OF_LIST(), |
| 851 | }, |
| 852 | .qdev.reset = virtio_pci_reset, |
| 853 | }; |
| 854 | |
| 855 | static PCIDeviceInfo virtio_serial_info = { |
| 856 | .qdev.name = "virtio-serial-pci", |
| 857 | .qdev.alias = "virtio-serial", |
| 858 | .qdev.size = sizeof(VirtIOPCIProxy), |
| 859 | .init = virtio_serial_init_pci, |
| 860 | .exit = virtio_serial_exit_pci, |
| 861 | .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
| 862 | .device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE, |
| 863 | .revision = VIRTIO_PCI_ABI_VERSION, |
| 864 | .class_id = PCI_CLASS_COMMUNICATION_OTHER, |
| 865 | .qdev.props = (Property[]) { |
| 866 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), |
| 867 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED), |
| 868 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), |
| 869 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), |
| 870 | DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, serial.max_virtserial_ports, 31), |
| 871 | DEFINE_PROP_END_OF_LIST(), |
| 872 | }, |
| 873 | .qdev.reset = virtio_pci_reset, |
| 874 | }; |
| 875 | |
| 876 | static PCIDeviceInfo virtio_balloon_info = { |
| 877 | .qdev.name = "virtio-balloon-pci", |
| 878 | .qdev.alias = "virtio-balloon", |
| 879 | .qdev.size = sizeof(VirtIOPCIProxy), |
| 880 | .init = virtio_balloon_init_pci, |
| 881 | .exit = virtio_balloon_exit_pci, |
| 882 | .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
| 883 | .device_id = PCI_DEVICE_ID_VIRTIO_BALLOON, |
| 884 | .revision = VIRTIO_PCI_ABI_VERSION, |
| 885 | .class_id = PCI_CLASS_MEMORY_RAM, |
| 886 | .qdev.props = (Property[]) { |
| 887 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), |
| 888 | DEFINE_PROP_END_OF_LIST(), |
| 889 | }, |
| 890 | .qdev.reset = virtio_pci_reset, |
Gerd Hoffmann | 0aab0d3 | 2009-06-30 14:12:07 +0200 | [diff] [blame] | 891 | }; |
| 892 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 893 | static void virtio_pci_register_devices(void) |
| 894 | { |
Anthony Liguori | e855761 | 2011-12-06 19:32:44 -0600 | [diff] [blame] | 895 | pci_qdev_register(&virtio_blk_info); |
| 896 | pci_qdev_register(&virtio_net_info); |
| 897 | pci_qdev_register(&virtio_serial_info); |
| 898 | pci_qdev_register(&virtio_balloon_info); |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | device_init(virtio_pci_register_devices) |