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 VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); 1024 1025 msix_uninit_exclusive_bar(pci_dev); 1026 memory_region_destroy(&proxy->bar); 1027 } 1028 1029 static void virtio_pci_reset(DeviceState *qdev) 1030 { 1031 VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); 1032 VirtioBusState *bus = VIRTIO_BUS(&proxy->bus); 1033 virtio_pci_stop_ioeventfd(proxy); 1034 virtio_bus_reset(bus); 1035 msix_unuse_all_vectors(&proxy->pci_dev); 1036 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG; 1037 } 1038 1039 static Property virtio_pci_properties[] = { 1040 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), 1041 DEFINE_PROP_END_OF_LIST(), 1042 }; 1043 1044 static void virtio_pci_class_init(ObjectClass *klass, void *data) 1045 { 1046 DeviceClass *dc = DEVICE_CLASS(klass); 1047 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1048 1049 dc->props = virtio_pci_properties; 1050 k->init = virtio_pci_init; 1051 k->exit = virtio_pci_exit; 1052 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1053 k->revision = VIRTIO_PCI_ABI_VERSION; 1054 k->class_id = PCI_CLASS_OTHERS; 1055 dc->reset = virtio_pci_reset; 1056 } 1057 1058 static const TypeInfo virtio_pci_info = { 1059 .name = TYPE_VIRTIO_PCI, 1060 .parent = TYPE_PCI_DEVICE, 1061 .instance_size = sizeof(VirtIOPCIProxy), 1062 .class_init = virtio_pci_class_init, 1063 .class_size = sizeof(VirtioPCIClass), 1064 .abstract = true, 1065 }; 1066 1067 /* virtio-blk-pci */ 1068 1069 static Property virtio_blk_pci_properties[] = { 1070 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1071 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1072 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1073 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 1074 DEFINE_PROP_END_OF_LIST(), 1075 }; 1076 1077 static int virtio_blk_pci_init(VirtIOPCIProxy *vpci_dev) 1078 { 1079 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev); 1080 DeviceState *vdev = DEVICE(&dev->vdev); 1081 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1082 if (qdev_init(vdev) < 0) { 1083 return -1; 1084 } 1085 return 0; 1086 } 1087 1088 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data) 1089 { 1090 DeviceClass *dc = DEVICE_CLASS(klass); 1091 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1092 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1093 1094 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1095 dc->props = virtio_blk_pci_properties; 1096 k->init = virtio_blk_pci_init; 1097 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1098 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK; 1099 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1100 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1101 } 1102 1103 static void virtio_blk_pci_instance_init(Object *obj) 1104 { 1105 VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj); 1106 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BLK); 1107 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1108 object_unref(OBJECT(&dev->vdev)); 1109 qdev_alias_all_properties(DEVICE(&dev->vdev), obj); 1110 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread", 1111 &error_abort); 1112 } 1113 1114 static const TypeInfo virtio_blk_pci_info = { 1115 .name = TYPE_VIRTIO_BLK_PCI, 1116 .parent = TYPE_VIRTIO_PCI, 1117 .instance_size = sizeof(VirtIOBlkPCI), 1118 .instance_init = virtio_blk_pci_instance_init, 1119 .class_init = virtio_blk_pci_class_init, 1120 }; 1121 1122 /* virtio-scsi-pci */ 1123 1124 static Property virtio_scsi_pci_properties[] = { 1125 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1126 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1127 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 1128 DEV_NVECTORS_UNSPECIFIED), 1129 DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy, host_features), 1130 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI, vdev.parent_obj.conf), 1131 DEFINE_PROP_END_OF_LIST(), 1132 }; 1133 1134 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev) 1135 { 1136 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev); 1137 DeviceState *vdev = DEVICE(&dev->vdev); 1138 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 1139 DeviceState *proxy = DEVICE(vpci_dev); 1140 char *bus_name; 1141 1142 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1143 vpci_dev->nvectors = vs->conf.num_queues + 3; 1144 } 1145 1146 /* 1147 * For command line compatibility, this sets the virtio-scsi-device bus 1148 * name as before. 1149 */ 1150 if (proxy->id) { 1151 bus_name = g_strdup_printf("%s.0", proxy->id); 1152 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 1153 g_free(bus_name); 1154 } 1155 1156 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1157 if (qdev_init(vdev) < 0) { 1158 return -1; 1159 } 1160 return 0; 1161 } 1162 1163 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data) 1164 { 1165 DeviceClass *dc = DEVICE_CLASS(klass); 1166 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1167 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1168 k->init = virtio_scsi_pci_init_pci; 1169 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1170 dc->props = virtio_scsi_pci_properties; 1171 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1172 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; 1173 pcidev_k->revision = 0x00; 1174 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1175 } 1176 1177 static void virtio_scsi_pci_instance_init(Object *obj) 1178 { 1179 VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj); 1180 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SCSI); 1181 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1182 } 1183 1184 static const TypeInfo virtio_scsi_pci_info = { 1185 .name = TYPE_VIRTIO_SCSI_PCI, 1186 .parent = TYPE_VIRTIO_PCI, 1187 .instance_size = sizeof(VirtIOSCSIPCI), 1188 .instance_init = virtio_scsi_pci_instance_init, 1189 .class_init = virtio_scsi_pci_class_init, 1190 }; 1191 1192 /* vhost-scsi-pci */ 1193 1194 #ifdef CONFIG_VHOST_SCSI 1195 static Property vhost_scsi_pci_properties[] = { 1196 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 1197 DEV_NVECTORS_UNSPECIFIED), 1198 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI, vdev.parent_obj.conf), 1199 DEFINE_PROP_END_OF_LIST(), 1200 }; 1201 1202 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev) 1203 { 1204 VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev); 1205 DeviceState *vdev = DEVICE(&dev->vdev); 1206 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 1207 1208 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1209 vpci_dev->nvectors = vs->conf.num_queues + 3; 1210 } 1211 1212 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1213 if (qdev_init(vdev) < 0) { 1214 return -1; 1215 } 1216 return 0; 1217 } 1218 1219 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data) 1220 { 1221 DeviceClass *dc = DEVICE_CLASS(klass); 1222 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1223 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1224 k->init = vhost_scsi_pci_init_pci; 1225 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1226 dc->props = vhost_scsi_pci_properties; 1227 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1228 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; 1229 pcidev_k->revision = 0x00; 1230 pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; 1231 } 1232 1233 static void vhost_scsi_pci_instance_init(Object *obj) 1234 { 1235 VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj); 1236 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VHOST_SCSI); 1237 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1238 } 1239 1240 static const TypeInfo vhost_scsi_pci_info = { 1241 .name = TYPE_VHOST_SCSI_PCI, 1242 .parent = TYPE_VIRTIO_PCI, 1243 .instance_size = sizeof(VHostSCSIPCI), 1244 .instance_init = vhost_scsi_pci_instance_init, 1245 .class_init = vhost_scsi_pci_class_init, 1246 }; 1247 #endif 1248 1249 /* virtio-balloon-pci */ 1250 1251 static void balloon_pci_stats_get_all(Object *obj, struct Visitor *v, 1252 void *opaque, const char *name, 1253 Error **errp) 1254 { 1255 VirtIOBalloonPCI *dev = opaque; 1256 object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp); 1257 } 1258 1259 static void balloon_pci_stats_get_poll_interval(Object *obj, struct Visitor *v, 1260 void *opaque, const char *name, 1261 Error **errp) 1262 { 1263 VirtIOBalloonPCI *dev = opaque; 1264 object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval", 1265 errp); 1266 } 1267 1268 static void balloon_pci_stats_set_poll_interval(Object *obj, struct Visitor *v, 1269 void *opaque, const char *name, 1270 Error **errp) 1271 { 1272 VirtIOBalloonPCI *dev = opaque; 1273 object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval", 1274 errp); 1275 } 1276 1277 static Property virtio_balloon_pci_properties[] = { 1278 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1279 DEFINE_PROP_END_OF_LIST(), 1280 }; 1281 1282 static int virtio_balloon_pci_init(VirtIOPCIProxy *vpci_dev) 1283 { 1284 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev); 1285 DeviceState *vdev = DEVICE(&dev->vdev); 1286 1287 if (vpci_dev->class_code != PCI_CLASS_OTHERS && 1288 vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */ 1289 vpci_dev->class_code = PCI_CLASS_OTHERS; 1290 } 1291 1292 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1293 if (qdev_init(vdev) < 0) { 1294 return -1; 1295 } 1296 return 0; 1297 } 1298 1299 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data) 1300 { 1301 DeviceClass *dc = DEVICE_CLASS(klass); 1302 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1303 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1304 k->init = virtio_balloon_pci_init; 1305 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1306 dc->props = virtio_balloon_pci_properties; 1307 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1308 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON; 1309 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1310 pcidev_k->class_id = PCI_CLASS_OTHERS; 1311 } 1312 1313 static void virtio_balloon_pci_instance_init(Object *obj) 1314 { 1315 VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj); 1316 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BALLOON); 1317 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1318 1319 object_property_add(obj, "guest-stats", "guest statistics", 1320 balloon_pci_stats_get_all, NULL, NULL, dev, 1321 NULL); 1322 1323 object_property_add(obj, "guest-stats-polling-interval", "int", 1324 balloon_pci_stats_get_poll_interval, 1325 balloon_pci_stats_set_poll_interval, 1326 NULL, dev, NULL); 1327 } 1328 1329 static const TypeInfo virtio_balloon_pci_info = { 1330 .name = TYPE_VIRTIO_BALLOON_PCI, 1331 .parent = TYPE_VIRTIO_PCI, 1332 .instance_size = sizeof(VirtIOBalloonPCI), 1333 .instance_init = virtio_balloon_pci_instance_init, 1334 .class_init = virtio_balloon_pci_class_init, 1335 }; 1336 1337 /* virtio-serial-pci */ 1338 1339 static int virtio_serial_pci_init(VirtIOPCIProxy *vpci_dev) 1340 { 1341 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev); 1342 DeviceState *vdev = DEVICE(&dev->vdev); 1343 DeviceState *proxy = DEVICE(vpci_dev); 1344 char *bus_name; 1345 1346 if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER && 1347 vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */ 1348 vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */ 1349 vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER; 1350 } 1351 1352 /* backwards-compatibility with machines that were created with 1353 DEV_NVECTORS_UNSPECIFIED */ 1354 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { 1355 vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1; 1356 } 1357 1358 /* 1359 * For command line compatibility, this sets the virtio-serial-device bus 1360 * name as before. 1361 */ 1362 if (proxy->id) { 1363 bus_name = g_strdup_printf("%s.0", proxy->id); 1364 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 1365 g_free(bus_name); 1366 } 1367 1368 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1369 if (qdev_init(vdev) < 0) { 1370 return -1; 1371 } 1372 return 0; 1373 } 1374 1375 static Property virtio_serial_pci_properties[] = { 1376 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1377 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 1378 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 1379 DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), 1380 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI, vdev.serial), 1381 DEFINE_PROP_END_OF_LIST(), 1382 }; 1383 1384 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data) 1385 { 1386 DeviceClass *dc = DEVICE_CLASS(klass); 1387 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1388 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1389 k->init = virtio_serial_pci_init; 1390 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 1391 dc->props = virtio_serial_pci_properties; 1392 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1393 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE; 1394 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1395 pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER; 1396 } 1397 1398 static void virtio_serial_pci_instance_init(Object *obj) 1399 { 1400 VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj); 1401 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SERIAL); 1402 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1403 } 1404 1405 static const TypeInfo virtio_serial_pci_info = { 1406 .name = TYPE_VIRTIO_SERIAL_PCI, 1407 .parent = TYPE_VIRTIO_PCI, 1408 .instance_size = sizeof(VirtIOSerialPCI), 1409 .instance_init = virtio_serial_pci_instance_init, 1410 .class_init = virtio_serial_pci_class_init, 1411 }; 1412 1413 /* virtio-net-pci */ 1414 1415 static Property virtio_net_properties[] = { 1416 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 1417 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false), 1418 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), 1419 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features), 1420 DEFINE_NIC_PROPERTIES(VirtIONetPCI, vdev.nic_conf), 1421 DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI, vdev.net_conf), 1422 DEFINE_PROP_END_OF_LIST(), 1423 }; 1424 1425 static int virtio_net_pci_init(VirtIOPCIProxy *vpci_dev) 1426 { 1427 DeviceState *qdev = DEVICE(vpci_dev); 1428 VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev); 1429 DeviceState *vdev = DEVICE(&dev->vdev); 1430 1431 virtio_net_set_config_size(&dev->vdev, vpci_dev->host_features); 1432 virtio_net_set_netclient_name(&dev->vdev, qdev->id, 1433 object_get_typename(OBJECT(qdev))); 1434 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1435 if (qdev_init(vdev) < 0) { 1436 return -1; 1437 } 1438 return 0; 1439 } 1440 1441 static void virtio_net_pci_class_init(ObjectClass *klass, void *data) 1442 { 1443 DeviceClass *dc = DEVICE_CLASS(klass); 1444 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1445 VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); 1446 1447 k->romfile = "efi-virtio.rom"; 1448 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1449 k->device_id = PCI_DEVICE_ID_VIRTIO_NET; 1450 k->revision = VIRTIO_PCI_ABI_VERSION; 1451 k->class_id = PCI_CLASS_NETWORK_ETHERNET; 1452 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 1453 dc->props = virtio_net_properties; 1454 vpciklass->init = virtio_net_pci_init; 1455 } 1456 1457 static void virtio_net_pci_instance_init(Object *obj) 1458 { 1459 VirtIONetPCI *dev = VIRTIO_NET_PCI(obj); 1460 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_NET); 1461 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1462 } 1463 1464 static const TypeInfo virtio_net_pci_info = { 1465 .name = TYPE_VIRTIO_NET_PCI, 1466 .parent = TYPE_VIRTIO_PCI, 1467 .instance_size = sizeof(VirtIONetPCI), 1468 .instance_init = virtio_net_pci_instance_init, 1469 .class_init = virtio_net_pci_class_init, 1470 }; 1471 1472 /* virtio-rng-pci */ 1473 1474 static Property virtio_rng_pci_properties[] = { 1475 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI, vdev.conf), 1476 DEFINE_PROP_END_OF_LIST(), 1477 }; 1478 1479 static int virtio_rng_pci_init(VirtIOPCIProxy *vpci_dev) 1480 { 1481 VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev); 1482 DeviceState *vdev = DEVICE(&vrng->vdev); 1483 1484 qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); 1485 if (qdev_init(vdev) < 0) { 1486 return -1; 1487 } 1488 1489 object_property_set_link(OBJECT(vrng), 1490 OBJECT(vrng->vdev.conf.rng), "rng", 1491 NULL); 1492 1493 return 0; 1494 } 1495 1496 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data) 1497 { 1498 DeviceClass *dc = DEVICE_CLASS(klass); 1499 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); 1500 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); 1501 1502 k->init = virtio_rng_pci_init; 1503 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1504 dc->props = virtio_rng_pci_properties; 1505 1506 pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; 1507 pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG; 1508 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; 1509 pcidev_k->class_id = PCI_CLASS_OTHERS; 1510 } 1511 1512 static void virtio_rng_initfn(Object *obj) 1513 { 1514 VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj); 1515 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_RNG); 1516 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 1517 object_property_add_link(obj, "rng", TYPE_RNG_BACKEND, 1518 (Object **)&dev->vdev.conf.rng, 1519 qdev_prop_allow_set_link_before_realize, 1520 OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL); 1521 1522 } 1523 1524 static const TypeInfo virtio_rng_pci_info = { 1525 .name = TYPE_VIRTIO_RNG_PCI, 1526 .parent = TYPE_VIRTIO_PCI, 1527 .instance_size = sizeof(VirtIORngPCI), 1528 .instance_init = virtio_rng_initfn, 1529 .class_init = virtio_rng_pci_class_init, 1530 }; 1531 1532 /* virtio-pci-bus */ 1533 1534 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, 1535 VirtIOPCIProxy *dev) 1536 { 1537 DeviceState *qdev = DEVICE(dev); 1538 BusState *qbus; 1539 char virtio_bus_name[] = "virtio-bus"; 1540 1541 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev, 1542 virtio_bus_name); 1543 qbus = BUS(bus); 1544 qbus->allow_hotplug = 1; 1545 } 1546 1547 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data) 1548 { 1549 BusClass *bus_class = BUS_CLASS(klass); 1550 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); 1551 bus_class->max_dev = 1; 1552 k->notify = virtio_pci_notify; 1553 k->save_config = virtio_pci_save_config; 1554 k->load_config = virtio_pci_load_config; 1555 k->save_queue = virtio_pci_save_queue; 1556 k->load_queue = virtio_pci_load_queue; 1557 k->get_features = virtio_pci_get_features; 1558 k->query_guest_notifiers = virtio_pci_query_guest_notifiers; 1559 k->set_host_notifier = virtio_pci_set_host_notifier; 1560 k->set_guest_notifiers = virtio_pci_set_guest_notifiers; 1561 k->vmstate_change = virtio_pci_vmstate_change; 1562 k->device_plugged = virtio_pci_device_plugged; 1563 k->device_unplugged = virtio_pci_device_unplugged; 1564 } 1565 1566 static const TypeInfo virtio_pci_bus_info = { 1567 .name = TYPE_VIRTIO_PCI_BUS, 1568 .parent = TYPE_VIRTIO_BUS, 1569 .instance_size = sizeof(VirtioPCIBusState), 1570 .class_init = virtio_pci_bus_class_init, 1571 }; 1572 1573 static void virtio_pci_register_types(void) 1574 { 1575 type_register_static(&virtio_rng_pci_info); 1576 type_register_static(&virtio_pci_bus_info); 1577 type_register_static(&virtio_pci_info); 1578 #ifdef CONFIG_VIRTFS 1579 type_register_static(&virtio_9p_pci_info); 1580 #endif 1581 type_register_static(&virtio_blk_pci_info); 1582 type_register_static(&virtio_scsi_pci_info); 1583 type_register_static(&virtio_balloon_pci_info); 1584 type_register_static(&virtio_serial_pci_info); 1585 type_register_static(&virtio_net_pci_info); 1586 #ifdef CONFIG_VHOST_SCSI 1587 type_register_static(&vhost_scsi_pci_info); 1588 #endif 1589 } 1590 1591 type_init(virtio_pci_register_types) 1592