1 /* 2 * iommufd container backend 3 * 4 * Copyright (C) 2023 Intel Corporation. 5 * Copyright Red Hat, Inc. 2023 6 * 7 * Authors: Yi Liu <yi.l.liu@intel.com> 8 * Eric Auger <eric.auger@redhat.com> 9 * 10 * SPDX-License-Identifier: GPL-2.0-or-later 11 */ 12 13 #include "qemu/osdep.h" 14 #include <sys/ioctl.h> 15 #include <linux/vfio.h> 16 #include <linux/iommufd.h> 17 18 #include "hw/vfio/vfio-common.h" 19 #include "qemu/error-report.h" 20 #include "trace.h" 21 #include "qapi/error.h" 22 #include "sysemu/iommufd.h" 23 #include "hw/qdev-core.h" 24 #include "sysemu/reset.h" 25 #include "qemu/cutils.h" 26 #include "qemu/chardev_open.h" 27 #include "pci.h" 28 29 static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova, 30 ram_addr_t size, void *vaddr, bool readonly) 31 { 32 const VFIOIOMMUFDContainer *container = 33 container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); 34 35 return iommufd_backend_map_dma(container->be, 36 container->ioas_id, 37 iova, size, vaddr, readonly); 38 } 39 40 static int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer, 41 hwaddr iova, ram_addr_t size, 42 IOMMUTLBEntry *iotlb) 43 { 44 const VFIOIOMMUFDContainer *container = 45 container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); 46 47 /* TODO: Handle dma_unmap_bitmap with iotlb args (migration) */ 48 return iommufd_backend_unmap_dma(container->be, 49 container->ioas_id, iova, size); 50 } 51 52 static bool iommufd_cdev_kvm_device_add(VFIODevice *vbasedev, Error **errp) 53 { 54 return !vfio_kvm_device_add_fd(vbasedev->fd, errp); 55 } 56 57 static void iommufd_cdev_kvm_device_del(VFIODevice *vbasedev) 58 { 59 Error *err = NULL; 60 61 if (vfio_kvm_device_del_fd(vbasedev->fd, &err)) { 62 error_report_err(err); 63 } 64 } 65 66 static bool iommufd_cdev_connect_and_bind(VFIODevice *vbasedev, Error **errp) 67 { 68 IOMMUFDBackend *iommufd = vbasedev->iommufd; 69 struct vfio_device_bind_iommufd bind = { 70 .argsz = sizeof(bind), 71 .flags = 0, 72 }; 73 74 if (!iommufd_backend_connect(iommufd, errp)) { 75 return false; 76 } 77 78 /* 79 * Add device to kvm-vfio to be prepared for the tracking 80 * in KVM. Especially for some emulated devices, it requires 81 * to have kvm information in the device open. 82 */ 83 if (!iommufd_cdev_kvm_device_add(vbasedev, errp)) { 84 goto err_kvm_device_add; 85 } 86 87 /* Bind device to iommufd */ 88 bind.iommufd = iommufd->fd; 89 if (ioctl(vbasedev->fd, VFIO_DEVICE_BIND_IOMMUFD, &bind)) { 90 error_setg_errno(errp, errno, "error bind device fd=%d to iommufd=%d", 91 vbasedev->fd, bind.iommufd); 92 goto err_bind; 93 } 94 95 vbasedev->devid = bind.out_devid; 96 trace_iommufd_cdev_connect_and_bind(bind.iommufd, vbasedev->name, 97 vbasedev->fd, vbasedev->devid); 98 return true; 99 err_bind: 100 iommufd_cdev_kvm_device_del(vbasedev); 101 err_kvm_device_add: 102 iommufd_backend_disconnect(iommufd); 103 return false; 104 } 105 106 static void iommufd_cdev_unbind_and_disconnect(VFIODevice *vbasedev) 107 { 108 /* Unbind is automatically conducted when device fd is closed */ 109 iommufd_cdev_kvm_device_del(vbasedev); 110 iommufd_backend_disconnect(vbasedev->iommufd); 111 } 112 113 static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp) 114 { 115 ERRP_GUARD(); 116 long int ret = -ENOTTY; 117 g_autofree char *path = NULL; 118 g_autofree char *vfio_dev_path = NULL; 119 g_autofree char *vfio_path = NULL; 120 DIR *dir = NULL; 121 struct dirent *dent; 122 g_autofree gchar *contents = NULL; 123 gsize length; 124 int major, minor; 125 dev_t vfio_devt; 126 127 path = g_strdup_printf("%s/vfio-dev", sysfs_path); 128 dir = opendir(path); 129 if (!dir) { 130 error_setg_errno(errp, errno, "couldn't open directory %s", path); 131 goto out; 132 } 133 134 while ((dent = readdir(dir))) { 135 if (!strncmp(dent->d_name, "vfio", 4)) { 136 vfio_dev_path = g_strdup_printf("%s/%s/dev", path, dent->d_name); 137 break; 138 } 139 } 140 141 if (!vfio_dev_path) { 142 error_setg(errp, "failed to find vfio-dev/vfioX/dev"); 143 goto out_close_dir; 144 } 145 146 if (!g_file_get_contents(vfio_dev_path, &contents, &length, NULL)) { 147 error_setg(errp, "failed to load \"%s\"", vfio_dev_path); 148 goto out_close_dir; 149 } 150 151 if (sscanf(contents, "%d:%d", &major, &minor) != 2) { 152 error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path); 153 goto out_close_dir; 154 } 155 vfio_devt = makedev(major, minor); 156 157 vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name); 158 ret = open_cdev(vfio_path, vfio_devt); 159 if (ret < 0) { 160 error_setg(errp, "Failed to open %s", vfio_path); 161 } 162 163 trace_iommufd_cdev_getfd(vfio_path, ret); 164 165 out_close_dir: 166 closedir(dir); 167 out: 168 if (*errp) { 169 error_prepend(errp, VFIO_MSG_PREFIX, path); 170 } 171 172 return ret; 173 } 174 175 static int iommufd_cdev_attach_ioas_hwpt(VFIODevice *vbasedev, uint32_t id, 176 Error **errp) 177 { 178 int iommufd = vbasedev->iommufd->fd; 179 struct vfio_device_attach_iommufd_pt attach_data = { 180 .argsz = sizeof(attach_data), 181 .flags = 0, 182 .pt_id = id, 183 }; 184 185 /* Attach device to an IOAS or hwpt within iommufd */ 186 if (ioctl(vbasedev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data)) { 187 error_setg_errno(errp, errno, 188 "[iommufd=%d] error attach %s (%d) to id=%d", 189 iommufd, vbasedev->name, vbasedev->fd, id); 190 return -errno; 191 } 192 193 trace_iommufd_cdev_attach_ioas_hwpt(iommufd, vbasedev->name, 194 vbasedev->fd, id); 195 return 0; 196 } 197 198 static bool iommufd_cdev_detach_ioas_hwpt(VFIODevice *vbasedev, Error **errp) 199 { 200 int iommufd = vbasedev->iommufd->fd; 201 struct vfio_device_detach_iommufd_pt detach_data = { 202 .argsz = sizeof(detach_data), 203 .flags = 0, 204 }; 205 206 if (ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_data)) { 207 error_setg_errno(errp, errno, "detach %s failed", vbasedev->name); 208 return false; 209 } 210 211 trace_iommufd_cdev_detach_ioas_hwpt(iommufd, vbasedev->name); 212 return true; 213 } 214 215 static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, 216 VFIOIOMMUFDContainer *container, 217 Error **errp) 218 { 219 ERRP_GUARD(); 220 IOMMUFDBackend *iommufd = vbasedev->iommufd; 221 uint32_t flags = 0; 222 VFIOIOASHwpt *hwpt; 223 uint32_t hwpt_id; 224 int ret; 225 226 /* Try to find a domain */ 227 QLIST_FOREACH(hwpt, &container->hwpt_list, next) { 228 ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp); 229 if (ret) { 230 /* -EINVAL means the domain is incompatible with the device. */ 231 if (ret == -EINVAL) { 232 /* 233 * It is an expected failure and it just means we will try 234 * another domain, or create one if no existing compatible 235 * domain is found. Hence why the error is discarded below. 236 */ 237 error_free(*errp); 238 *errp = NULL; 239 continue; 240 } 241 242 return false; 243 } else { 244 vbasedev->hwpt = hwpt; 245 QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next); 246 return true; 247 } 248 } 249 250 if (!iommufd_backend_alloc_hwpt(iommufd, vbasedev->devid, 251 container->ioas_id, flags, 252 IOMMU_HWPT_DATA_NONE, 0, NULL, 253 &hwpt_id, errp)) { 254 return false; 255 } 256 257 hwpt = g_malloc0(sizeof(*hwpt)); 258 hwpt->hwpt_id = hwpt_id; 259 QLIST_INIT(&hwpt->device_list); 260 261 ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp); 262 if (ret) { 263 iommufd_backend_free_id(container->be, hwpt->hwpt_id); 264 g_free(hwpt); 265 return false; 266 } 267 268 vbasedev->hwpt = hwpt; 269 QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next); 270 QLIST_INSERT_HEAD(&container->hwpt_list, hwpt, next); 271 return true; 272 } 273 274 static void iommufd_cdev_autodomains_put(VFIODevice *vbasedev, 275 VFIOIOMMUFDContainer *container) 276 { 277 VFIOIOASHwpt *hwpt = vbasedev->hwpt; 278 279 QLIST_REMOVE(vbasedev, hwpt_next); 280 vbasedev->hwpt = NULL; 281 282 if (QLIST_EMPTY(&hwpt->device_list)) { 283 QLIST_REMOVE(hwpt, next); 284 iommufd_backend_free_id(container->be, hwpt->hwpt_id); 285 g_free(hwpt); 286 } 287 } 288 289 static bool iommufd_cdev_attach_container(VFIODevice *vbasedev, 290 VFIOIOMMUFDContainer *container, 291 Error **errp) 292 { 293 /* mdevs aren't physical devices and will fail with auto domains */ 294 if (!vbasedev->mdev) { 295 return iommufd_cdev_autodomains_get(vbasedev, container, errp); 296 } 297 298 return !iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp); 299 } 300 301 static void iommufd_cdev_detach_container(VFIODevice *vbasedev, 302 VFIOIOMMUFDContainer *container) 303 { 304 Error *err = NULL; 305 306 if (!iommufd_cdev_detach_ioas_hwpt(vbasedev, &err)) { 307 error_report_err(err); 308 } 309 310 if (vbasedev->hwpt) { 311 iommufd_cdev_autodomains_put(vbasedev, container); 312 } 313 314 } 315 316 static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container) 317 { 318 VFIOContainerBase *bcontainer = &container->bcontainer; 319 320 if (!QLIST_EMPTY(&bcontainer->device_list)) { 321 return; 322 } 323 memory_listener_unregister(&bcontainer->listener); 324 iommufd_backend_free_id(container->be, container->ioas_id); 325 object_unref(container); 326 } 327 328 static int iommufd_cdev_ram_block_discard_disable(bool state) 329 { 330 /* 331 * We support coordinated discarding of RAM via the RamDiscardManager. 332 */ 333 return ram_block_uncoordinated_discard_disable(state); 334 } 335 336 static bool iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container, 337 uint32_t ioas_id, Error **errp) 338 { 339 VFIOContainerBase *bcontainer = &container->bcontainer; 340 g_autofree struct iommu_ioas_iova_ranges *info = NULL; 341 struct iommu_iova_range *iova_ranges; 342 int sz, fd = container->be->fd; 343 344 info = g_malloc0(sizeof(*info)); 345 info->size = sizeof(*info); 346 info->ioas_id = ioas_id; 347 348 if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info) && errno != EMSGSIZE) { 349 goto error; 350 } 351 352 sz = info->num_iovas * sizeof(struct iommu_iova_range); 353 info = g_realloc(info, sizeof(*info) + sz); 354 info->allowed_iovas = (uintptr_t)(info + 1); 355 356 if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info)) { 357 goto error; 358 } 359 360 iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas; 361 362 for (int i = 0; i < info->num_iovas; i++) { 363 Range *range = g_new(Range, 1); 364 365 range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last); 366 bcontainer->iova_ranges = 367 range_list_insert(bcontainer->iova_ranges, range); 368 } 369 bcontainer->pgsizes = info->out_iova_alignment; 370 371 return true; 372 373 error: 374 error_setg_errno(errp, errno, "Cannot get IOVA ranges"); 375 return false; 376 } 377 378 static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev, 379 AddressSpace *as, Error **errp) 380 { 381 VFIOContainerBase *bcontainer; 382 VFIOIOMMUFDContainer *container; 383 VFIOAddressSpace *space; 384 struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; 385 int ret, devfd; 386 uint32_t ioas_id; 387 Error *err = NULL; 388 const VFIOIOMMUClass *iommufd_vioc = 389 VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 390 391 if (vbasedev->fd < 0) { 392 devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp); 393 if (devfd < 0) { 394 return false; 395 } 396 vbasedev->fd = devfd; 397 } else { 398 devfd = vbasedev->fd; 399 } 400 401 if (!iommufd_cdev_connect_and_bind(vbasedev, errp)) { 402 goto err_connect_bind; 403 } 404 405 space = vfio_get_address_space(as); 406 407 /* try to attach to an existing container in this space */ 408 QLIST_FOREACH(bcontainer, &space->containers, next) { 409 container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); 410 if (VFIO_IOMMU_GET_CLASS(bcontainer) != iommufd_vioc || 411 vbasedev->iommufd != container->be) { 412 continue; 413 } 414 if (!iommufd_cdev_attach_container(vbasedev, container, &err)) { 415 const char *msg = error_get_pretty(err); 416 417 trace_iommufd_cdev_fail_attach_existing_container(msg); 418 error_free(err); 419 err = NULL; 420 } else { 421 ret = iommufd_cdev_ram_block_discard_disable(true); 422 if (ret) { 423 error_setg(errp, 424 "Cannot set discarding of RAM broken (%d)", ret); 425 goto err_discard_disable; 426 } 427 goto found_container; 428 } 429 } 430 431 /* Need to allocate a new dedicated container */ 432 if (!iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp)) { 433 goto err_alloc_ioas; 434 } 435 436 trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id); 437 438 container = VFIO_IOMMU_IOMMUFD(object_new(TYPE_VFIO_IOMMU_IOMMUFD)); 439 container->be = vbasedev->iommufd; 440 container->ioas_id = ioas_id; 441 QLIST_INIT(&container->hwpt_list); 442 443 bcontainer = &container->bcontainer; 444 vfio_address_space_insert(space, bcontainer); 445 446 if (!iommufd_cdev_attach_container(vbasedev, container, errp)) { 447 goto err_attach_container; 448 } 449 450 ret = iommufd_cdev_ram_block_discard_disable(true); 451 if (ret) { 452 goto err_discard_disable; 453 } 454 455 if (!iommufd_cdev_get_info_iova_range(container, ioas_id, &err)) { 456 error_append_hint(&err, 457 "Fallback to default 64bit IOVA range and 4K page size\n"); 458 warn_report_err(err); 459 err = NULL; 460 bcontainer->pgsizes = qemu_real_host_page_size(); 461 } 462 463 bcontainer->listener = vfio_memory_listener; 464 memory_listener_register(&bcontainer->listener, bcontainer->space->as); 465 466 if (bcontainer->error) { 467 error_propagate_prepend(errp, bcontainer->error, 468 "memory listener initialization failed: "); 469 goto err_listener_register; 470 } 471 472 bcontainer->initialized = true; 473 474 found_container: 475 ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info); 476 if (ret) { 477 error_setg_errno(errp, errno, "error getting device info"); 478 goto err_listener_register; 479 } 480 481 if (!vfio_cpr_register_container(bcontainer, errp)) { 482 goto err_listener_register; 483 } 484 485 /* 486 * TODO: examine RAM_BLOCK_DISCARD stuff, should we do group level 487 * for discarding incompatibility check as well? 488 */ 489 if (vbasedev->ram_block_discard_allowed) { 490 iommufd_cdev_ram_block_discard_disable(false); 491 } 492 493 vbasedev->group = 0; 494 vbasedev->num_irqs = dev_info.num_irqs; 495 vbasedev->num_regions = dev_info.num_regions; 496 vbasedev->flags = dev_info.flags; 497 vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); 498 vbasedev->bcontainer = bcontainer; 499 QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next); 500 QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next); 501 502 trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs, 503 vbasedev->num_regions, vbasedev->flags); 504 return true; 505 506 err_listener_register: 507 iommufd_cdev_ram_block_discard_disable(false); 508 err_discard_disable: 509 iommufd_cdev_detach_container(vbasedev, container); 510 err_attach_container: 511 iommufd_cdev_container_destroy(container); 512 err_alloc_ioas: 513 vfio_put_address_space(space); 514 iommufd_cdev_unbind_and_disconnect(vbasedev); 515 err_connect_bind: 516 close(vbasedev->fd); 517 return false; 518 } 519 520 static void iommufd_cdev_detach(VFIODevice *vbasedev) 521 { 522 VFIOContainerBase *bcontainer = vbasedev->bcontainer; 523 VFIOAddressSpace *space = bcontainer->space; 524 VFIOIOMMUFDContainer *container = container_of(bcontainer, 525 VFIOIOMMUFDContainer, 526 bcontainer); 527 QLIST_REMOVE(vbasedev, global_next); 528 QLIST_REMOVE(vbasedev, container_next); 529 vbasedev->bcontainer = NULL; 530 531 if (!vbasedev->ram_block_discard_allowed) { 532 iommufd_cdev_ram_block_discard_disable(false); 533 } 534 535 vfio_cpr_unregister_container(bcontainer); 536 iommufd_cdev_detach_container(vbasedev, container); 537 iommufd_cdev_container_destroy(container); 538 vfio_put_address_space(space); 539 540 iommufd_cdev_unbind_and_disconnect(vbasedev); 541 close(vbasedev->fd); 542 } 543 544 static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid) 545 { 546 VFIODevice *vbasedev_iter; 547 const VFIOIOMMUClass *iommufd_vioc = 548 VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 549 550 QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) { 551 if (VFIO_IOMMU_GET_CLASS(vbasedev_iter->bcontainer) != iommufd_vioc) { 552 continue; 553 } 554 if (devid == vbasedev_iter->devid) { 555 return vbasedev_iter; 556 } 557 } 558 return NULL; 559 } 560 561 static VFIOPCIDevice * 562 iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev, 563 VFIODevice *reset_dev) 564 { 565 VFIODevice *vbasedev_tmp; 566 567 if (dep_dev->devid == reset_dev->devid || 568 dep_dev->devid == VFIO_PCI_DEVID_OWNED) { 569 return NULL; 570 } 571 572 vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid); 573 if (!vbasedev_tmp || !vbasedev_tmp->dev->realized || 574 vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) { 575 return NULL; 576 } 577 578 return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev); 579 } 580 581 static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single) 582 { 583 VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev); 584 struct vfio_pci_hot_reset_info *info = NULL; 585 struct vfio_pci_dependent_device *devices; 586 struct vfio_pci_hot_reset *reset; 587 int ret, i; 588 bool multi = false; 589 590 trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi"); 591 592 if (!single) { 593 vfio_pci_pre_reset(vdev); 594 } 595 vdev->vbasedev.needs_reset = false; 596 597 ret = vfio_pci_get_pci_hot_reset_info(vdev, &info); 598 599 if (ret) { 600 goto out_single; 601 } 602 603 assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID); 604 605 devices = &info->devices[0]; 606 607 if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) { 608 if (!vdev->has_pm_reset) { 609 for (i = 0; i < info->count; i++) { 610 if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) { 611 error_report("vfio: Cannot reset device %s, " 612 "depends on device %04x:%02x:%02x.%x " 613 "which is not owned.", 614 vdev->vbasedev.name, devices[i].segment, 615 devices[i].bus, PCI_SLOT(devices[i].devfn), 616 PCI_FUNC(devices[i].devfn)); 617 } 618 } 619 } 620 ret = -EPERM; 621 goto out_single; 622 } 623 624 trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name); 625 626 for (i = 0; i < info->count; i++) { 627 VFIOPCIDevice *tmp; 628 629 trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment, 630 devices[i].bus, 631 PCI_SLOT(devices[i].devfn), 632 PCI_FUNC(devices[i].devfn), 633 devices[i].devid); 634 635 /* 636 * If a VFIO cdev device is resettable, all the dependent devices 637 * are either bound to same iommufd or within same iommu_groups as 638 * one of the iommufd bound devices. 639 */ 640 assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED); 641 642 tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev); 643 if (!tmp) { 644 continue; 645 } 646 647 if (single) { 648 ret = -EINVAL; 649 goto out_single; 650 } 651 vfio_pci_pre_reset(tmp); 652 tmp->vbasedev.needs_reset = false; 653 multi = true; 654 } 655 656 if (!single && !multi) { 657 ret = -EINVAL; 658 goto out_single; 659 } 660 661 /* Use zero length array for hot reset with iommufd backend */ 662 reset = g_malloc0(sizeof(*reset)); 663 reset->argsz = sizeof(*reset); 664 665 /* Bus reset! */ 666 ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset); 667 g_free(reset); 668 if (ret) { 669 ret = -errno; 670 } 671 672 trace_vfio_pci_hot_reset_result(vdev->vbasedev.name, 673 ret ? strerror(errno) : "Success"); 674 675 /* Re-enable INTx on affected devices */ 676 for (i = 0; i < info->count; i++) { 677 VFIOPCIDevice *tmp; 678 679 tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev); 680 if (!tmp) { 681 continue; 682 } 683 vfio_pci_post_reset(tmp); 684 } 685 out_single: 686 if (!single) { 687 vfio_pci_post_reset(vdev); 688 } 689 g_free(info); 690 691 return ret; 692 } 693 694 static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data) 695 { 696 VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass); 697 698 vioc->hiod_typename = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO; 699 700 vioc->dma_map = iommufd_cdev_map; 701 vioc->dma_unmap = iommufd_cdev_unmap; 702 vioc->attach_device = iommufd_cdev_attach; 703 vioc->detach_device = iommufd_cdev_detach; 704 vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset; 705 }; 706 707 static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque, 708 Error **errp) 709 { 710 VFIODevice *vdev = opaque; 711 HostIOMMUDeviceCaps *caps = &hiod->caps; 712 enum iommu_hw_info_type type; 713 union { 714 struct iommu_hw_info_vtd vtd; 715 } data; 716 uint64_t hw_caps; 717 718 hiod->agent = opaque; 719 720 if (!iommufd_backend_get_device_info(vdev->iommufd, vdev->devid, 721 &type, &data, sizeof(data), 722 &hw_caps, errp)) { 723 return false; 724 } 725 726 hiod->name = g_strdup(vdev->name); 727 caps->type = type; 728 729 return true; 730 } 731 732 static GList * 733 hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod) 734 { 735 VFIODevice *vdev = hiod->agent; 736 737 g_assert(vdev); 738 return vfio_container_get_iova_ranges(vdev->bcontainer); 739 } 740 741 static uint64_t 742 hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod) 743 { 744 VFIODevice *vdev = hiod->agent; 745 746 g_assert(vdev); 747 return vfio_container_get_page_size_mask(vdev->bcontainer); 748 } 749 750 751 static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data) 752 { 753 HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc); 754 755 hiodc->realize = hiod_iommufd_vfio_realize; 756 hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges; 757 hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask; 758 }; 759 760 static const TypeInfo types[] = { 761 { 762 .name = TYPE_VFIO_IOMMU_IOMMUFD, 763 .parent = TYPE_VFIO_IOMMU, 764 .instance_size = sizeof(VFIOIOMMUFDContainer), 765 .class_init = vfio_iommu_iommufd_class_init, 766 }, { 767 .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO, 768 .parent = TYPE_HOST_IOMMU_DEVICE_IOMMUFD, 769 .class_init = hiod_iommufd_vfio_class_init, 770 } 771 }; 772 773 DEFINE_TYPES(types) 774