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