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