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 * 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. 16 */ 17 18 #include <inttypes.h> 19 20 #include "hw/virtio/virtio.h" 21 #include "hw/virtio/virtio-blk.h" 22 #include "hw/virtio/virtio-net.h" 23 #include "hw/virtio/virtio-serial.h" 24 #include "hw/virtio/virtio-scsi.h" 25 #include "hw/virtio/virtio-balloon.h" 26 #include "hw/pci/pci.h" 27 #include "qemu/error-report.h" 28 #include "hw/pci/msi.h" 29 #include "hw/pci/msix.h" 30 #include "hw/loader.h" 31 #include "sysemu/kvm.h" 32 #include "sysemu/blockdev.h" 33 #include "virtio-pci.h" 34 #include "qemu/range.h" 35 #include "hw/virtio/virtio-bus.h" 36 #include "qapi/visitor.h" 37 38 /* from Linux's linux/virtio_pci.h */ 39 40 /* A 32-bit r/o bitmask of the features supported by the host */ 41 #define VIRTIO_PCI_HOST_FEATURES 0 42 43 /* A 32-bit r/w bitmask of features activated by the guest */ 44 #define VIRTIO_PCI_GUEST_FEATURES 4 45 46 /* A 32-bit r/w PFN for the currently selected queue */ 47 #define VIRTIO_PCI_QUEUE_PFN 8 48 49 /* A 16-bit r/o queue size for the currently selected queue */ 50 #define VIRTIO_PCI_QUEUE_NUM 12 51 52 /* A 16-bit r/w queue selector */ 53 #define VIRTIO_PCI_QUEUE_SEL 14 54 55 /* A 16-bit r/w queue notifier */ 56 #define VIRTIO_PCI_QUEUE_NOTIFY 16 57 58 /* An 8-bit device status register. */ 59 #define VIRTIO_PCI_STATUS 18 60 61 /* An 8-bit r/o interrupt status register. Reading the value will return the 62 * current contents of the ISR and will also clear it. This is effectively 63 * a read-and-acknowledge. */ 64 #define VIRTIO_PCI_ISR 19 65 66 /* MSI-X registers: only enabled if MSI-X is enabled. */ 67 /* A 16-bit vector for configuration changes. */ 68 #define VIRTIO_MSI_CONFIG_VECTOR 20 69 /* A 16-bit vector for selected queue notifications. */ 70 #define VIRTIO_MSI_QUEUE_VECTOR 22 71 72 /* Config space size */ 73 #define VIRTIO_PCI_CONFIG_NOMSI 20 74 #define VIRTIO_PCI_CONFIG_MSI 24 75 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \ 76 VIRTIO_PCI_CONFIG_MSI : \ 77 VIRTIO_PCI_CONFIG_NOMSI) 78 79 /* The remaining space is defined by each driver as the per-driver 80 * configuration space */ 81 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \ 82 VIRTIO_PCI_CONFIG_MSI : \ 83 VIRTIO_PCI_CONFIG_NOMSI) 84 85 /* How many bits to shift physical queue address written to QUEUE_PFN. 86 * 12 is historical, and due to x86 page size. */ 87 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12 88 89 /* Flags track per-device state like workarounds for quirks in older guests. */ 90 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0) 91 92 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, 93 VirtIOPCIProxy *dev); 94 95 /* virtio device */ 96 /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */ 97 static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d) 98 { 99 return container_of(d, VirtIOPCIProxy, pci_dev.qdev); 100 } 101 102 /* DeviceState to VirtIOPCIProxy. Note: used on datapath, 103 * be careful and test performance if you change this. 104 */ 105 static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d) 106 { 107 return container_of(d, VirtIOPCIProxy, pci_dev.qdev); 108 } 109 110 static void virtio_pci_notify(DeviceState *d, uint16_t vector) 111 { 112 VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d); 113 114 if (msix_enabled(&proxy->pci_dev)) 115 msix_notify(&proxy->pci_dev, vector); 116 else { 117 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 118 pci_set_irq(&proxy->pci_dev, vdev->isr & 1); 119 } 120 } 121 122 static void virtio_pci_save_config(DeviceState *d, QEMUFile *f) 123 { 124 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 125 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 126 127 pci_device_save(&proxy->pci_dev, f); 128 msix_save(&proxy->pci_dev, f); 129 if (msix_present(&proxy->pci_dev)) 130 qemu_put_be16(f, vdev->config_vector); 131 } 132 133 static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f) 134 { 135 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 136 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 137 138 if (msix_present(&proxy->pci_dev)) 139 qemu_put_be16(f, virtio_queue_vector(vdev, n)); 140 } 141 142 static int virtio_pci_load_config(DeviceState *d, QEMUFile *f) 143 { 144 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 145 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 146 147 int ret; 148 ret = pci_device_load(&proxy->pci_dev, f); 149 if (ret) { 150 return ret; 151 } 152 msix_unuse_all_vectors(&proxy->pci_dev); 153 msix_load(&proxy->pci_dev, f); 154 if (msix_present(&proxy->pci_dev)) { 155 qemu_get_be16s(f, &vdev->config_vector); 156 } else { 157 vdev->config_vector = VIRTIO_NO_VECTOR; 158 } 159 if (vdev->config_vector != VIRTIO_NO_VECTOR) { 160 return msix_vector_use(&proxy->pci_dev, vdev->config_vector); 161 } 162 return 0; 163 } 164 165 static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f) 166 { 167 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 168 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 169 170 uint16_t vector; 171 if (msix_present(&proxy->pci_dev)) { 172 qemu_get_be16s(f, &vector); 173 } else { 174 vector = VIRTIO_NO_VECTOR; 175 } 176 virtio_queue_set_vector(vdev, n, vector); 177 if (vector != VIRTIO_NO_VECTOR) { 178 return msix_vector_use(&proxy->pci_dev, vector); 179 } 180 return 0; 181 } 182 183 static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy, 184 int n, bool assign, bool set_handler) 185 { 186 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 187 VirtQueue *vq = virtio_get_queue(vdev, n); 188 EventNotifier *notifier = virtio_queue_get_host_notifier(vq); 189 int r = 0; 190 191 if (assign) { 192 r = event_notifier_init(notifier, 1); 193 if (r < 0) { 194 error_report("%s: unable to init event notifier: %d", 195 __func__, r); 196 return r; 197 } 198 virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler); 199 memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, 200 true, n, notifier); 201 } else { 202 memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, 203 true, n, notifier); 204 virtio_queue_set_host_notifier_fd_handler(vq, false, false); 205 event_notifier_cleanup(notifier); 206 } 207 return r; 208 } 209 210 static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy) 211 { 212 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 213 int n, r; 214 215 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) || 216 proxy->ioeventfd_disabled || 217 proxy->ioeventfd_started) { 218 return; 219 } 220 221 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { 222 if (!virtio_queue_get_num(vdev, n)) { 223 continue; 224 } 225 226 r = virtio_pci_set_host_notifier_internal(proxy, n, true, true); 227 if (r < 0) { 228 goto assign_error; 229 } 230 } 231 proxy->ioeventfd_started = true; 232 return; 233 234 assign_error: 235 while (--n >= 0) { 236 if (!virtio_queue_get_num(vdev, n)) { 237 continue; 238 } 239 240 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false); 241 assert(r >= 0); 242 } 243 proxy->ioeventfd_started = false; 244 error_report("%s: failed. Fallback to a userspace (slower).", __func__); 245 } 246 247 static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy) 248 { 249 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 250 int r; 251 int n; 252 253 if (!proxy->ioeventfd_started) { 254 return; 255 } 256 257 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { 258 if (!virtio_queue_get_num(vdev, n)) { 259 continue; 260 } 261 262 r = virtio_pci_set_host_notifier_internal(proxy, n, false, false); 263 assert(r >= 0); 264 } 265 proxy->ioeventfd_started = false; 266 } 267 268 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) 269 { 270 VirtIOPCIProxy *proxy = opaque; 271 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 272 hwaddr pa; 273 274 switch (addr) { 275 case VIRTIO_PCI_GUEST_FEATURES: 276 /* Guest does not negotiate properly? We have to assume nothing. */ 277 if (val & (1 << VIRTIO_F_BAD_FEATURE)) { 278 val = virtio_bus_get_vdev_bad_features(&proxy->bus); 279 } 280 virtio_set_features(vdev, val); 281 break; 282 case VIRTIO_PCI_QUEUE_PFN: 283 pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; 284 if (pa == 0) { 285 virtio_pci_stop_ioeventfd(proxy); 286 virtio_reset(vdev); 287 msix_unuse_all_vectors(&proxy->pci_dev); 288 } 289 else 290 virtio_queue_set_addr(vdev, vdev->queue_sel, pa); 291 break; 292 case VIRTIO_PCI_QUEUE_SEL: 293 if (val < VIRTIO_PCI_QUEUE_MAX) 294 vdev->queue_sel = val; 295 break; 296 case VIRTIO_PCI_QUEUE_NOTIFY: 297 if (val < VIRTIO_PCI_QUEUE_MAX) { 298 virtio_queue_notify(vdev, val); 299 } 300 break; 301 case VIRTIO_PCI_STATUS: 302 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { 303 virtio_pci_stop_ioeventfd(proxy); 304 } 305 306 virtio_set_status(vdev, val & 0xFF); 307 308 if (val & VIRTIO_CONFIG_S_DRIVER_OK) { 309 virtio_pci_start_ioeventfd(proxy); 310 } 311 312 if (vdev->status == 0) { 313 virtio_reset(vdev); 314 msix_unuse_all_vectors(&proxy->pci_dev); 315 } 316 317 /* Linux before 2.6.34 sets the device as OK without enabling 318 the PCI device bus master bit. In this case we need to disable 319 some safety checks. */ 320 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) && 321 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { 322 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; 323 } 324 break; 325 case VIRTIO_MSI_CONFIG_VECTOR: 326 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); 327 /* Make it possible for guest to discover an error took place. */ 328 if (msix_vector_use(&proxy->pci_dev, val) < 0) 329 val = VIRTIO_NO_VECTOR; 330 vdev->config_vector = val; 331 break; 332 case VIRTIO_MSI_QUEUE_VECTOR: 333 msix_vector_unuse(&proxy->pci_dev, 334 virtio_queue_vector(vdev, vdev->queue_sel)); 335 /* Make it possible for guest to discover an error took place. */ 336 if (msix_vector_use(&proxy->pci_dev, val) < 0) 337 val = VIRTIO_NO_VECTOR; 338 virtio_queue_set_vector(vdev, vdev->queue_sel, val); 339 break; 340 default: 341 error_report("%s: unexpected address 0x%x value 0x%x", 342 __func__, addr, val); 343 break; 344 } 345 } 346 347 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr) 348 { 349 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 350 uint32_t ret = 0xFFFFFFFF; 351 352 switch (addr) { 353 case VIRTIO_PCI_HOST_FEATURES: 354 ret = proxy->host_features; 355 break; 356 case VIRTIO_PCI_GUEST_FEATURES: 357 ret = vdev->guest_features; 358 break; 359 case VIRTIO_PCI_QUEUE_PFN: 360 ret = virtio_queue_get_addr(vdev, vdev->queue_sel) 361 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; 362 break; 363 case VIRTIO_PCI_QUEUE_NUM: 364 ret = virtio_queue_get_num(vdev, vdev->queue_sel); 365 break; 366 case VIRTIO_PCI_QUEUE_SEL: 367 ret = vdev->queue_sel; 368 break; 369 case VIRTIO_PCI_STATUS: 370 ret = vdev->status; 371 break; 372 case VIRTIO_PCI_ISR: 373 /* reading from the ISR also clears it. */ 374 ret = vdev->isr; 375 vdev->isr = 0; 376 pci_irq_deassert(&proxy->pci_dev); 377 break; 378 case VIRTIO_MSI_CONFIG_VECTOR: 379 ret = vdev->config_vector; 380 break; 381 case VIRTIO_MSI_QUEUE_VECTOR: 382 ret = virtio_queue_vector(vdev, vdev->queue_sel); 383 break; 384 default: 385 break; 386 } 387 388 return ret; 389 } 390 391 static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr, 392 unsigned size) 393 { 394 VirtIOPCIProxy *proxy = opaque; 395 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 396 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); 397 uint64_t val = 0; 398 if (addr < config) { 399 return virtio_ioport_read(proxy, addr); 400 } 401 addr -= config; 402 403 switch (size) { 404 case 1: 405 val = virtio_config_readb(vdev, addr); 406 break; 407 case 2: 408 val = virtio_config_readw(vdev, addr); 409 if (virtio_is_big_endian(vdev)) { 410 val = bswap16(val); 411 } 412 break; 413 case 4: 414 val = virtio_config_readl(vdev, addr); 415 if (virtio_is_big_endian(vdev)) { 416 val = bswap32(val); 417 } 418 break; 419 } 420 return val; 421 } 422 423 static void virtio_pci_config_write(void *opaque, hwaddr addr, 424 uint64_t val, unsigned size) 425 { 426 VirtIOPCIProxy *proxy = opaque; 427 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); 428 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 429 if (addr < config) { 430 virtio_ioport_write(proxy, addr, val); 431 return; 432 } 433 addr -= config; 434 /* 435 * Virtio-PCI is odd. Ioports are LE but config space is target native 436 * endian. 437 */ 438 switch (size) { 439 case 1: 440 virtio_config_writeb(vdev, addr, val); 441 break; 442 case 2: 443 if (virtio_is_big_endian(vdev)) { 444 val = bswap16(val); 445 } 446 virtio_config_writew(vdev, addr, val); 447 break; 448 case 4: 449 if (virtio_is_big_endian(vdev)) { 450 val = bswap32(val); 451 } 452 virtio_config_writel(vdev, addr, val); 453 break; 454 } 455 } 456 457 static const MemoryRegionOps virtio_pci_config_ops = { 458 .read = virtio_pci_config_read, 459 .write = virtio_pci_config_write, 460 .impl = { 461 .min_access_size = 1, 462 .max_access_size = 4, 463 }, 464 .endianness = DEVICE_LITTLE_ENDIAN, 465 }; 466 467 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, 468 uint32_t val, int len) 469 { 470 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); 471 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 472 473 pci_default_write_config(pci_dev, address, val, len); 474 475 if (range_covers_byte(address, len, PCI_COMMAND) && 476 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) && 477 !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) { 478 virtio_pci_stop_ioeventfd(proxy); 479 virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); 480 } 481 } 482 483 static unsigned virtio_pci_get_features(DeviceState *d) 484 { 485 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 486 return proxy->host_features; 487 } 488 489 static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy, 490 unsigned int queue_no, 491 unsigned int vector, 492 MSIMessage msg) 493 { 494 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 495 int ret; 496 497 if (irqfd->users == 0) { 498 ret = kvm_irqchip_add_msi_route(kvm_state, msg); 499 if (ret < 0) { 500 return ret; 501 } 502 irqfd->virq = ret; 503 } 504 irqfd->users++; 505 return 0; 506 } 507 508 static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy, 509 unsigned int vector) 510 { 511 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 512 if (--irqfd->users == 0) { 513 kvm_irqchip_release_virq(kvm_state, irqfd->virq); 514 } 515 } 516 517 static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy, 518 unsigned int queue_no, 519 unsigned int vector) 520 { 521 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 522 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 523 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 524 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 525 int ret; 526 ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, NULL, irqfd->virq); 527 return ret; 528 } 529 530 static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy, 531 unsigned int queue_no, 532 unsigned int vector) 533 { 534 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 535 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 536 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 537 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; 538 int ret; 539 540 ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq); 541 assert(ret == 0); 542 } 543 544 static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs) 545 { 546 PCIDevice *dev = &proxy->pci_dev; 547 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 548 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 549 unsigned int vector; 550 int ret, queue_no; 551 MSIMessage msg; 552 553 for (queue_no = 0; queue_no < nvqs; queue_no++) { 554 if (!virtio_queue_get_num(vdev, queue_no)) { 555 break; 556 } 557 vector = virtio_queue_vector(vdev, queue_no); 558 if (vector >= msix_nr_vectors_allocated(dev)) { 559 continue; 560 } 561 msg = msix_get_message(dev, vector); 562 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg); 563 if (ret < 0) { 564 goto undo; 565 } 566 /* If guest supports masking, set up irqfd now. 567 * Otherwise, delay until unmasked in the frontend. 568 */ 569 if (k->guest_notifier_mask) { 570 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); 571 if (ret < 0) { 572 kvm_virtio_pci_vq_vector_release(proxy, vector); 573 goto undo; 574 } 575 } 576 } 577 return 0; 578 579 undo: 580 while (--queue_no >= 0) { 581 vector = virtio_queue_vector(vdev, queue_no); 582 if (vector >= msix_nr_vectors_allocated(dev)) { 583 continue; 584 } 585 if (k->guest_notifier_mask) { 586 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 587 } 588 kvm_virtio_pci_vq_vector_release(proxy, vector); 589 } 590 return ret; 591 } 592 593 static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs) 594 { 595 PCIDevice *dev = &proxy->pci_dev; 596 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 597 unsigned int vector; 598 int queue_no; 599 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 600 601 for (queue_no = 0; queue_no < nvqs; queue_no++) { 602 if (!virtio_queue_get_num(vdev, queue_no)) { 603 break; 604 } 605 vector = virtio_queue_vector(vdev, queue_no); 606 if (vector >= msix_nr_vectors_allocated(dev)) { 607 continue; 608 } 609 /* If guest supports masking, clean up irqfd now. 610 * Otherwise, it was cleaned when masked in the frontend. 611 */ 612 if (k->guest_notifier_mask) { 613 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 614 } 615 kvm_virtio_pci_vq_vector_release(proxy, vector); 616 } 617 } 618 619 static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy, 620 unsigned int queue_no, 621 unsigned int vector, 622 MSIMessage msg) 623 { 624 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 625 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 626 VirtQueue *vq = virtio_get_queue(vdev, queue_no); 627 EventNotifier *n = virtio_queue_get_guest_notifier(vq); 628 VirtIOIRQFD *irqfd; 629 int ret = 0; 630 631 if (proxy->vector_irqfd) { 632 irqfd = &proxy->vector_irqfd[vector]; 633 if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) { 634 ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg); 635 if (ret < 0) { 636 return ret; 637 } 638 } 639 } 640 641 /* If guest supports masking, irqfd is already setup, unmask it. 642 * Otherwise, set it up now. 643 */ 644 if (k->guest_notifier_mask) { 645 k->guest_notifier_mask(vdev, queue_no, false); 646 /* Test after unmasking to avoid losing events. */ 647 if (k->guest_notifier_pending && 648 k->guest_notifier_pending(vdev, queue_no)) { 649 event_notifier_set(n); 650 } 651 } else { 652 ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); 653 } 654 return ret; 655 } 656 657 static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy, 658 unsigned int queue_no, 659 unsigned int vector) 660 { 661 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 662 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 663 664 /* If guest supports masking, keep irqfd but mask it. 665 * Otherwise, clean it up now. 666 */ 667 if (k->guest_notifier_mask) { 668 k->guest_notifier_mask(vdev, queue_no, true); 669 } else { 670 kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); 671 } 672 } 673 674 static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector, 675 MSIMessage msg) 676 { 677 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 678 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 679 int ret, queue_no; 680 681 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { 682 if (!virtio_queue_get_num(vdev, queue_no)) { 683 break; 684 } 685 if (virtio_queue_vector(vdev, queue_no) != vector) { 686 continue; 687 } 688 ret = virtio_pci_vq_vector_unmask(proxy, queue_no, vector, msg); 689 if (ret < 0) { 690 goto undo; 691 } 692 } 693 return 0; 694 695 undo: 696 while (--queue_no >= 0) { 697 if (virtio_queue_vector(vdev, queue_no) != vector) { 698 continue; 699 } 700 virtio_pci_vq_vector_mask(proxy, queue_no, vector); 701 } 702 return ret; 703 } 704 705 static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector) 706 { 707 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 708 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 709 int queue_no; 710 711 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { 712 if (!virtio_queue_get_num(vdev, queue_no)) { 713 break; 714 } 715 if (virtio_queue_vector(vdev, queue_no) != vector) { 716 continue; 717 } 718 virtio_pci_vq_vector_mask(proxy, queue_no, vector); 719 } 720 } 721 722 static void virtio_pci_vector_poll(PCIDevice *dev, 723 unsigned int vector_start, 724 unsigned int vector_end) 725 { 726 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); 727 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 728 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 729 int queue_no; 730 unsigned int vector; 731 EventNotifier *notifier; 732 VirtQueue *vq; 733 734 for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { 735 if (!virtio_queue_get_num(vdev, queue_no)) { 736 break; 737 } 738 vector = virtio_queue_vector(vdev, queue_no); 739 if (vector < vector_start || vector >= vector_end || 740 !msix_is_masked(dev, vector)) { 741 continue; 742 } 743 vq = virtio_get_queue(vdev, queue_no); 744 notifier = virtio_queue_get_guest_notifier(vq); 745 if (k->guest_notifier_pending) { 746 if (k->guest_notifier_pending(vdev, queue_no)) { 747 msix_set_pending(dev, vector); 748 } 749 } else if (event_notifier_test_and_clear(notifier)) { 750 msix_set_pending(dev, vector); 751 } 752 } 753 } 754 755 static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign, 756 bool with_irqfd) 757 { 758 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 759 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 760 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); 761 VirtQueue *vq = virtio_get_queue(vdev, n); 762 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); 763 764 if (assign) { 765 int r = event_notifier_init(notifier, 0); 766 if (r < 0) { 767 return r; 768 } 769 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd); 770 } else { 771 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd); 772 event_notifier_cleanup(notifier); 773 } 774 775 if (!msix_enabled(&proxy->pci_dev) && vdc->guest_notifier_mask) { 776 vdc->guest_notifier_mask(vdev, n, !assign); 777 } 778 779 return 0; 780 } 781 782 static bool virtio_pci_query_guest_notifiers(DeviceState *d) 783 { 784 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 785 return msix_enabled(&proxy->pci_dev); 786 } 787 788 static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign) 789 { 790 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 791 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 792 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 793 int r, n; 794 bool with_irqfd = msix_enabled(&proxy->pci_dev) && 795 kvm_msi_via_irqfd_enabled(); 796 797 nvqs = MIN(nvqs, VIRTIO_PCI_QUEUE_MAX); 798 799 /* When deassigning, pass a consistent nvqs value 800 * to avoid leaking notifiers. 801 */ 802 assert(assign || nvqs == proxy->nvqs_with_notifiers); 803 804 proxy->nvqs_with_notifiers = nvqs; 805 806 /* Must unset vector notifier while guest notifier is still assigned */ 807 if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) { 808 msix_unset_vector_notifiers(&proxy->pci_dev); 809 if (proxy->vector_irqfd) { 810 kvm_virtio_pci_vector_release(proxy, nvqs); 811 g_free(proxy->vector_irqfd); 812 proxy->vector_irqfd = NULL; 813 } 814 } 815 816 for (n = 0; n < nvqs; n++) { 817 if (!virtio_queue_get_num(vdev, n)) { 818 break; 819 } 820 821 r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd); 822 if (r < 0) { 823 goto assign_error; 824 } 825 } 826 827 /* Must set vector notifier after guest notifier has been assigned */ 828 if ((with_irqfd || k->guest_notifier_mask) && assign) { 829 if (with_irqfd) { 830 proxy->vector_irqfd = 831 g_malloc0(sizeof(*proxy->vector_irqfd) * 832 msix_nr_vectors_allocated(&proxy->pci_dev)); 833 r = kvm_virtio_pci_vector_use(proxy, nvqs); 834 if (r < 0) { 835 goto assign_error; 836 } 837 } 838 r = msix_set_vector_notifiers(&proxy->pci_dev, 839 virtio_pci_vector_unmask, 840 virtio_pci_vector_mask, 841 virtio_pci_vector_poll); 842 if (r < 0) { 843 goto notifiers_error; 844 } 845 } 846 847 return 0; 848 849 notifiers_error: 850 if (with_irqfd) { 851 assert(assign); 852 kvm_virtio_pci_vector_release(proxy, nvqs); 853 } 854 855 assign_error: 856 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */ 857 assert(assign); 858 while (--n >= 0) { 859 virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd); 860 } 861 return r; 862 } 863 864 static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign) 865 { 866 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 867 868 /* Stop using ioeventfd for virtqueue kick if the device starts using host 869 * notifiers. This makes it easy to avoid stepping on each others' toes. 870 */ 871 proxy->ioeventfd_disabled = assign; 872 if (assign) { 873 virtio_pci_stop_ioeventfd(proxy); 874 } 875 /* We don't need to start here: it's not needed because backend 876 * currently only stops on status change away from ok, 877 * reset, vmstop and such. If we do add code to start here, 878 * need to check vmstate, device state etc. */ 879 return virtio_pci_set_host_notifier_internal(proxy, n, assign, false); 880 } 881 882 static void virtio_pci_vmstate_change(DeviceState *d, bool running) 883 { 884 VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); 885 VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); 886 887 if (running) { 888 /* Try to find out if the guest has bus master disabled, but is 889 in ready state. Then we have a buggy guest OS. */ 890 if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) && 891 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { 892 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; 893 } 894 virtio_pci_start_ioeventfd(proxy); 895 } else { 896 virtio_pci_stop_ioeventfd(proxy); 897 } 898 } 899 900 #ifdef CONFIG_VIRTFS 901 static int virtio_9p_init_pci(VirtIOPCIProxy *vpci_dev) 902 { 903 V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev); 904 DeviceState *vdev = DEVICE(&dev->vdev); 905 906 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 907 if (qdev_init(vdev) < 0) { 908 return -1; 909 } 910 return 0; 911 } 912 913 static Property virtio_9p_pci_properties[] = { 914 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 915 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 916 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 917 DEFINE_VIRTIO_9P_PROPERTIES(V9fsPCIState, vdev.fsconf), 918 DEFINE_PROP_END_OF_LIST(), 919 }; 920 921 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data) 922 { 923 DeviceClass *dc = DEVICE_CLASS(klass); 924 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 925 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 926 927 k->init = virtio_9p_init_pci; 928 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 929 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P; 930 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 931 pcidev_k->class_id = 0x2; 932 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 933 dc->props = virtio_9p_pci_properties; 934 } 935 936 static void virtio_9p_pci_instance_init(Object *obj) 937 { 938 V9fsPCIState *dev = VIRTIO_9P_PCI(obj); 939 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_9P); 940 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 941 } 942 943 static const TypeInfo virtio_9p_pci_info = { 944 .name = TYPE_VIRTIO_9P_PCI, 945 .parent = TYPE_VIRTIO_PCI, 946 .instance_size = sizeof(V9fsPCIState), 947 .instance_init = virtio_9p_pci_instance_init, 948 .class_init = virtio_9p_pci_class_init, 949 }; 950 #endif /* CONFIG_VIRTFS */ 951 952 /* 953 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus. 954 */ 955 956 /* This is called by virtio-bus just after the device is plugged. */ 957 static void virtio_pci_device_plugged(DeviceState *d) 958 { 959 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 960 VirtioBusState *bus = &proxy->bus; 961 uint8_t *config; 962 uint32_t size; 963 964 config = proxy->pci_dev.config; 965 if (proxy->class_code) { 966 pci_config_set_class(config, proxy->class_code); 967 } 968 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID, 969 pci_get_word(config + PCI_VENDOR_ID)); 970 pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus)); 971 config[PCI_INTERRUPT_PIN] = 1; 972 973 if (proxy->nvectors && 974 msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) { 975 error_report("unable to init msix vectors to %" PRIu32, 976 proxy->nvectors); 977 proxy->nvectors = 0; 978 } 979 980 proxy->pci_dev.config_write = virtio_write_config; 981 982 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) 983 + virtio_bus_get_vdev_config_len(bus); 984 if (size & (size - 1)) { 985 size = 1 << qemu_fls(size); 986 } 987 988 memory_region_init_io(&proxy->bar, OBJECT(proxy), &virtio_pci_config_ops, 989 proxy, "virtio-pci", size); 990 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, 991 &proxy->bar); 992 993 if (!kvm_has_many_ioeventfds()) { 994 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; 995 } 996 997 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY; 998 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE; 999 proxy->host_features = virtio_bus_get_vdev_features(bus, 1000 proxy->host_features); 1001 } 1002 1003 static void virtio_pci_device_unplugged(DeviceState *d) 1004 { 1005 VirtIOPCIProxy *proxy = VIRTIO_PCI(d); 1006 1007 virtio_pci_stop_ioeventfd(proxy); 1008 } 1009 1010 static int virtio_pci_init(PCIDevice *pci_dev) 1011 { 1012 VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev); 1013 VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev); 1014 virtio_pci_bus_new(&dev->bus, sizeof(dev->bus), dev); 1015 if (k->init != NULL) { 1016 return k->init(dev); 1017 } 1018 return 0; 1019 } 1020 1021 static void virtio_pci_exit(PCIDevice *pci_dev) 1022 { 1023 msix_uninit_exclusive_bar(pci_dev); 1024 } 1025 1026 static void virtio_pci_reset(DeviceState *qdev) 1027 { 1028 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); 1029 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus); 1030 virtio_pci_stop_ioeventfd(proxy); 1031 virtio_bus_reset(bus); 1032 msix_unuse_all_vectors(&proxy->pci_dev); 1033 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG; 1034 } 1035 1036 static Property virtio_pci_properties[] = { 1037 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), 1038 DEFINE_PROP_END_OF_LIST(), 1039 }; 1040 1041 static void virtio_pci_class_init(ObjectClass *klass, void *data) 1042 { 1043 DeviceClass *dc = DEVICE_CLASS(klass); 1044 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1045 1046 dc->props = virtio_pci_properties; 1047 k->init = virtio_pci_init; 1048 k->exit = virtio_pci_exit; 1049 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1050 k->revision = VIRTIO_PCI_ABI_VERSION; 1051 k->class_id = PCI_CLASS_OTHERS; 1052 dc->reset = virtio_pci_reset; 1053 } 1054 1055 static const TypeInfo virtio_pci_info = { 1056 .name = TYPE_VIRTIO_PCI, 1057 .parent = TYPE_PCI_DEVICE, 1058 .instance_size = sizeof(VirtIOPCIProxy), 1059 .class_init = virtio_pci_class_init, 1060 .class_size = sizeof(VirtioPCIClass), 1061 .abstract = true, 1062 }; 1063 1064 /* virtio-blk-pci */ 1065 1066 static Property virtio_blk_pci_properties[] = { 1067 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1068 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1069 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1070 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 1071 DEFINE_PROP_END_OF_LIST(), 1072 }; 1073 1074 static int virtio_blk_pci_init(VirtIOPCIProxy *vpci_dev) 1075 { 1076 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev); 1077 DeviceState *vdev = DEVICE(&dev->vdev); 1078 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1079 if (qdev_init(vdev) < 0) { 1080 return -1; 1081 } 1082 return 0; 1083 } 1084 1085 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data) 1086 { 1087 DeviceClass *dc = DEVICE_CLASS(klass); 1088 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1089 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1090 1091 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1092 dc->props = virtio_blk_pci_properties; 1093 k->init = virtio_blk_pci_init; 1094 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1095 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK; 1096 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1097 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1098 } 1099 1100 static void virtio_blk_pci_instance_init(Object *obj) 1101 { 1102 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj); 1103 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BLK); 1104 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1105 object_unref(OBJECT(&dev->vdev)); 1106 qdev_alias_all_properties(DEVICE(&dev->vdev), obj); 1107 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread", 1108 &error_abort); 1109 } 1110 1111 static const TypeInfo virtio_blk_pci_info = { 1112 .name = TYPE_VIRTIO_BLK_PCI, 1113 .parent = TYPE_VIRTIO_PCI, 1114 .instance_size = sizeof(VirtIOBlkPCI), 1115 .instance_init = virtio_blk_pci_instance_init, 1116 .class_init = virtio_blk_pci_class_init, 1117 }; 1118 1119 /* virtio-scsi-pci */ 1120 1121 static Property virtio_scsi_pci_properties[] = { 1122 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1123 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1124 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 1125 DEV_NVECTORS_UNSPECIFIED), 1126 DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy, host_features), 1127 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI, vdev.parent_obj.conf), 1128 DEFINE_PROP_END_OF_LIST(), 1129 }; 1130 1131 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev) 1132 { 1133 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev); 1134 DeviceState *vdev = DEVICE(&dev->vdev); 1135 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 1136 DeviceState *proxy = DEVICE(vpci_dev); 1137 char *bus_name; 1138 1139 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1140 vpci_dev->nvectors = vs->conf.num_queues + 3; 1141 } 1142 1143 /* 1144 * For command line compatibility, this sets the virtio-scsi-device bus 1145 * name as before. 1146 */ 1147 if (proxy->id) { 1148 bus_name = g_strdup_printf("%s.0", proxy->id); 1149 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 1150 g_free(bus_name); 1151 } 1152 1153 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1154 if (qdev_init(vdev) < 0) { 1155 return -1; 1156 } 1157 return 0; 1158 } 1159 1160 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data) 1161 { 1162 DeviceClass *dc = DEVICE_CLASS(klass); 1163 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1164 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1165 k->init = virtio_scsi_pci_init_pci; 1166 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1167 dc->props = virtio_scsi_pci_properties; 1168 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1169 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; 1170 pcidev_k->revision = 0x00; 1171 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1172 } 1173 1174 static void virtio_scsi_pci_instance_init(Object *obj) 1175 { 1176 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj); 1177 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SCSI); 1178 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1179 } 1180 1181 static const TypeInfo virtio_scsi_pci_info = { 1182 .name = TYPE_VIRTIO_SCSI_PCI, 1183 .parent = TYPE_VIRTIO_PCI, 1184 .instance_size = sizeof(VirtIOSCSIPCI), 1185 .instance_init = virtio_scsi_pci_instance_init, 1186 .class_init = virtio_scsi_pci_class_init, 1187 }; 1188 1189 /* vhost-scsi-pci */ 1190 1191 #ifdef CONFIG_VHOST_SCSI 1192 static Property vhost_scsi_pci_properties[] = { 1193 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 1194 DEV_NVECTORS_UNSPECIFIED), 1195 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI, vdev.parent_obj.conf), 1196 DEFINE_PROP_END_OF_LIST(), 1197 }; 1198 1199 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev) 1200 { 1201 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev); 1202 DeviceState *vdev = DEVICE(&dev->vdev); 1203 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 1204 1205 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1206 vpci_dev->nvectors = vs->conf.num_queues + 3; 1207 } 1208 1209 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1210 if (qdev_init(vdev) < 0) { 1211 return -1; 1212 } 1213 return 0; 1214 } 1215 1216 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data) 1217 { 1218 DeviceClass *dc = DEVICE_CLASS(klass); 1219 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1220 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1221 k->init = vhost_scsi_pci_init_pci; 1222 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1223 dc->props = vhost_scsi_pci_properties; 1224 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1225 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; 1226 pcidev_k->revision = 0x00; 1227 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1228 } 1229 1230 static void vhost_scsi_pci_instance_init(Object *obj) 1231 { 1232 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj); 1233 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VHOST_SCSI); 1234 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1235 } 1236 1237 static const TypeInfo vhost_scsi_pci_info = { 1238 .name = TYPE_VHOST_SCSI_PCI, 1239 .parent = TYPE_VIRTIO_PCI, 1240 .instance_size = sizeof(VHostSCSIPCI), 1241 .instance_init = vhost_scsi_pci_instance_init, 1242 .class_init = vhost_scsi_pci_class_init, 1243 }; 1244 #endif 1245 1246 /* virtio-balloon-pci */ 1247 1248 static void balloon_pci_stats_get_all(Object *obj, struct Visitor *v, 1249 void *opaque, const char *name, 1250 Error **errp) 1251 { 1252 VirtIOBalloonPCI *dev = opaque; 1253 object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp); 1254 } 1255 1256 static void balloon_pci_stats_get_poll_interval(Object *obj, struct Visitor *v, 1257 void *opaque, const char *name, 1258 Error **errp) 1259 { 1260 VirtIOBalloonPCI *dev = opaque; 1261 object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval", 1262 errp); 1263 } 1264 1265 static void balloon_pci_stats_set_poll_interval(Object *obj, struct Visitor *v, 1266 void *opaque, const char *name, 1267 Error **errp) 1268 { 1269 VirtIOBalloonPCI *dev = opaque; 1270 object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval", 1271 errp); 1272 } 1273 1274 static Property virtio_balloon_pci_properties[] = { 1275 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1276 DEFINE_PROP_END_OF_LIST(), 1277 }; 1278 1279 static int virtio_balloon_pci_init(VirtIOPCIProxy *vpci_dev) 1280 { 1281 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev); 1282 DeviceState *vdev = DEVICE(&dev->vdev); 1283 1284 if (vpci_dev->class_code != PCI_CLASS_OTHERS && 1285 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */ 1286 vpci_dev->class_code = PCI_CLASS_OTHERS; 1287 } 1288 1289 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1290 if (qdev_init(vdev) < 0) { 1291 return -1; 1292 } 1293 return 0; 1294 } 1295 1296 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data) 1297 { 1298 DeviceClass *dc = DEVICE_CLASS(klass); 1299 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1300 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1301 k->init = virtio_balloon_pci_init; 1302 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1303 dc->props = virtio_balloon_pci_properties; 1304 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1305 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON; 1306 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1307 pcidev_k->class_id = PCI_CLASS_OTHERS; 1308 } 1309 1310 static void virtio_balloon_pci_instance_init(Object *obj) 1311 { 1312 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj); 1313 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BALLOON); 1314 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1315 1316 object_property_add(obj, "guest-stats", "guest statistics", 1317 balloon_pci_stats_get_all, NULL, NULL, dev, 1318 NULL); 1319 1320 object_property_add(obj, "guest-stats-polling-interval", "int", 1321 balloon_pci_stats_get_poll_interval, 1322 balloon_pci_stats_set_poll_interval, 1323 NULL, dev, NULL); 1324 } 1325 1326 static const TypeInfo virtio_balloon_pci_info = { 1327 .name = TYPE_VIRTIO_BALLOON_PCI, 1328 .parent = TYPE_VIRTIO_PCI, 1329 .instance_size = sizeof(VirtIOBalloonPCI), 1330 .instance_init = virtio_balloon_pci_instance_init, 1331 .class_init = virtio_balloon_pci_class_init, 1332 }; 1333 1334 /* virtio-serial-pci */ 1335 1336 static int virtio_serial_pci_init(VirtIOPCIProxy *vpci_dev) 1337 { 1338 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev); 1339 DeviceState *vdev = DEVICE(&dev->vdev); 1340 DeviceState *proxy = DEVICE(vpci_dev); 1341 char *bus_name; 1342 1343 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER && 1344 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */ 1345 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */ 1346 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER; 1347 } 1348 1349 /* backwards-compatibility with machines that were created with 1350 DEV_NVECTORS_UNSPECIFIED */ 1351 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1352 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1; 1353 } 1354 1355 /* 1356 * For command line compatibility, this sets the virtio-serial-device bus 1357 * name as before. 1358 */ 1359 if (proxy->id) { 1360 bus_name = g_strdup_printf("%s.0", proxy->id); 1361 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 1362 g_free(bus_name); 1363 } 1364 1365 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1366 if (qdev_init(vdev) < 0) { 1367 return -1; 1368 } 1369 return 0; 1370 } 1371 1372 static Property virtio_serial_pci_properties[] = { 1373 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1374 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1375 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 1376 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1377 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI, vdev.serial), 1378 DEFINE_PROP_END_OF_LIST(), 1379 }; 1380 1381 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data) 1382 { 1383 DeviceClass *dc = DEVICE_CLASS(klass); 1384 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1385 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1386 k->init = virtio_serial_pci_init; 1387 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 1388 dc->props = virtio_serial_pci_properties; 1389 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1390 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE; 1391 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1392 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER; 1393 } 1394 1395 static void virtio_serial_pci_instance_init(Object *obj) 1396 { 1397 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj); 1398 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SERIAL); 1399 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1400 } 1401 1402 static const TypeInfo virtio_serial_pci_info = { 1403 .name = TYPE_VIRTIO_SERIAL_PCI, 1404 .parent = TYPE_VIRTIO_PCI, 1405 .instance_size = sizeof(VirtIOSerialPCI), 1406 .instance_init = virtio_serial_pci_instance_init, 1407 .class_init = virtio_serial_pci_class_init, 1408 }; 1409 1410 /* virtio-net-pci */ 1411 1412 static Property virtio_net_properties[] = { 1413 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1414 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false), 1415 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), 1416 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features), 1417 DEFINE_NIC_PROPERTIES(VirtIONetPCI, vdev.nic_conf), 1418 DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI, vdev.net_conf), 1419 DEFINE_PROP_END_OF_LIST(), 1420 }; 1421 1422 static int virtio_net_pci_init(VirtIOPCIProxy *vpci_dev) 1423 { 1424 DeviceState *qdev = DEVICE(vpci_dev); 1425 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev); 1426 DeviceState *vdev = DEVICE(&dev->vdev); 1427 1428 virtio_net_set_config_size(&dev->vdev, vpci_dev->host_features); 1429 virtio_net_set_netclient_name(&dev->vdev, qdev->id, 1430 object_get_typename(OBJECT(qdev))); 1431 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1432 if (qdev_init(vdev) < 0) { 1433 return -1; 1434 } 1435 return 0; 1436 } 1437 1438 static void virtio_net_pci_class_init(ObjectClass *klass, void *data) 1439 { 1440 DeviceClass *dc = DEVICE_CLASS(klass); 1441 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1442 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); 1443 1444 k->romfile = "efi-virtio.rom"; 1445 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1446 k->device_id = PCI_DEVICE_ID_VIRTIO_NET; 1447 k->revision = VIRTIO_PCI_ABI_VERSION; 1448 k->class_id = PCI_CLASS_NETWORK_ETHERNET; 1449 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 1450 dc->props = virtio_net_properties; 1451 vpciklass->init = virtio_net_pci_init; 1452 } 1453 1454 static void virtio_net_pci_instance_init(Object *obj) 1455 { 1456 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj); 1457 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_NET); 1458 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1459 } 1460 1461 static const TypeInfo virtio_net_pci_info = { 1462 .name = TYPE_VIRTIO_NET_PCI, 1463 .parent = TYPE_VIRTIO_PCI, 1464 .instance_size = sizeof(VirtIONetPCI), 1465 .instance_init = virtio_net_pci_instance_init, 1466 .class_init = virtio_net_pci_class_init, 1467 }; 1468 1469 /* virtio-rng-pci */ 1470 1471 static Property virtio_rng_pci_properties[] = { 1472 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI, vdev.conf), 1473 DEFINE_PROP_END_OF_LIST(), 1474 }; 1475 1476 static int virtio_rng_pci_init(VirtIOPCIProxy *vpci_dev) 1477 { 1478 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev); 1479 DeviceState *vdev = DEVICE(&vrng->vdev); 1480 1481 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1482 if (qdev_init(vdev) < 0) { 1483 return -1; 1484 } 1485 1486 object_property_set_link(OBJECT(vrng), 1487 OBJECT(vrng->vdev.conf.rng), "rng", 1488 NULL); 1489 1490 return 0; 1491 } 1492 1493 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data) 1494 { 1495 DeviceClass *dc = DEVICE_CLASS(klass); 1496 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1497 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1498 1499 k->init = virtio_rng_pci_init; 1500 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1501 dc->props = virtio_rng_pci_properties; 1502 1503 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1504 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG; 1505 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1506 pcidev_k->class_id = PCI_CLASS_OTHERS; 1507 } 1508 1509 static void virtio_rng_initfn(Object *obj) 1510 { 1511 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj); 1512 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_RNG); 1513 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1514 object_property_add_link(obj, "rng", TYPE_RNG_BACKEND, 1515 (Object **)&dev->vdev.conf.rng, 1516 qdev_prop_allow_set_link_before_realize, 1517 OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL); 1518 1519 } 1520 1521 static const TypeInfo virtio_rng_pci_info = { 1522 .name = TYPE_VIRTIO_RNG_PCI, 1523 .parent = TYPE_VIRTIO_PCI, 1524 .instance_size = sizeof(VirtIORngPCI), 1525 .instance_init = virtio_rng_initfn, 1526 .class_init = virtio_rng_pci_class_init, 1527 }; 1528 1529 /* virtio-pci-bus */ 1530 1531 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, 1532 VirtIOPCIProxy *dev) 1533 { 1534 DeviceState *qdev = DEVICE(dev); 1535 BusState *qbus; 1536 char virtio_bus_name[] = "virtio-bus"; 1537 1538 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev, 1539 virtio_bus_name); 1540 qbus = BUS(bus); 1541 qbus->allow_hotplug = 1; 1542 } 1543 1544 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data) 1545 { 1546 BusClass *bus_class = BUS_CLASS(klass); 1547 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); 1548 bus_class->max_dev = 1; 1549 k->notify = virtio_pci_notify; 1550 k->save_config = virtio_pci_save_config; 1551 k->load_config = virtio_pci_load_config; 1552 k->save_queue = virtio_pci_save_queue; 1553 k->load_queue = virtio_pci_load_queue; 1554 k->get_features = virtio_pci_get_features; 1555 k->query_guest_notifiers = virtio_pci_query_guest_notifiers; 1556 k->set_host_notifier = virtio_pci_set_host_notifier; 1557 k->set_guest_notifiers = virtio_pci_set_guest_notifiers; 1558 k->vmstate_change = virtio_pci_vmstate_change; 1559 k->device_plugged = virtio_pci_device_plugged; 1560 k->device_unplugged = virtio_pci_device_unplugged; 1561 } 1562 1563 static const TypeInfo virtio_pci_bus_info = { 1564 .name = TYPE_VIRTIO_PCI_BUS, 1565 .parent = TYPE_VIRTIO_BUS, 1566 .instance_size = sizeof(VirtioPCIBusState), 1567 .class_init = virtio_pci_bus_class_init, 1568 }; 1569 1570 static void virtio_pci_register_types(void) 1571 { 1572 type_register_static(&virtio_rng_pci_info); 1573 type_register_static(&virtio_pci_bus_info); 1574 type_register_static(&virtio_pci_info); 1575 #ifdef CONFIG_VIRTFS 1576 type_register_static(&virtio_9p_pci_info); 1577 #endif 1578 type_register_static(&virtio_blk_pci_info); 1579 type_register_static(&virtio_scsi_pci_info); 1580 type_register_static(&virtio_balloon_pci_info); 1581 type_register_static(&virtio_serial_pci_info); 1582 type_register_static(&virtio_net_pci_info); 1583 #ifdef CONFIG_VHOST_SCSI 1584 type_register_static(&vhost_scsi_pci_info); 1585 #endif 1586 } 1587 1588 type_init(virtio_pci_register_types) 1589