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 bool iommufd_hwpt_dirty_tracking(VFIOIOASHwpt *hwpt) 114 { 115 return hwpt && hwpt->hwpt_flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING; 116 } 117 118 static int iommufd_set_dirty_page_tracking(const VFIOContainerBase *bcontainer, 119 bool start, Error **errp) 120 { 121 const VFIOIOMMUFDContainer *container = 122 container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); 123 VFIOIOASHwpt *hwpt; 124 125 QLIST_FOREACH(hwpt, &container->hwpt_list, next) { 126 if (!iommufd_hwpt_dirty_tracking(hwpt)) { 127 continue; 128 } 129 130 if (!iommufd_backend_set_dirty_tracking(container->be, 131 hwpt->hwpt_id, start, errp)) { 132 goto err; 133 } 134 } 135 136 return 0; 137 138 err: 139 QLIST_FOREACH(hwpt, &container->hwpt_list, next) { 140 if (!iommufd_hwpt_dirty_tracking(hwpt)) { 141 continue; 142 } 143 iommufd_backend_set_dirty_tracking(container->be, 144 hwpt->hwpt_id, !start, NULL); 145 } 146 return -EINVAL; 147 } 148 149 static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp) 150 { 151 ERRP_GUARD(); 152 long int ret = -ENOTTY; 153 g_autofree char *path = NULL; 154 g_autofree char *vfio_dev_path = NULL; 155 g_autofree char *vfio_path = NULL; 156 DIR *dir = NULL; 157 struct dirent *dent; 158 g_autofree gchar *contents = NULL; 159 gsize length; 160 int major, minor; 161 dev_t vfio_devt; 162 163 path = g_strdup_printf("%s/vfio-dev", sysfs_path); 164 dir = opendir(path); 165 if (!dir) { 166 error_setg_errno(errp, errno, "couldn't open directory %s", path); 167 goto out; 168 } 169 170 while ((dent = readdir(dir))) { 171 if (!strncmp(dent->d_name, "vfio", 4)) { 172 vfio_dev_path = g_strdup_printf("%s/%s/dev", path, dent->d_name); 173 break; 174 } 175 } 176 177 if (!vfio_dev_path) { 178 error_setg(errp, "failed to find vfio-dev/vfioX/dev"); 179 goto out_close_dir; 180 } 181 182 if (!g_file_get_contents(vfio_dev_path, &contents, &length, NULL)) { 183 error_setg(errp, "failed to load \"%s\"", vfio_dev_path); 184 goto out_close_dir; 185 } 186 187 if (sscanf(contents, "%d:%d", &major, &minor) != 2) { 188 error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path); 189 goto out_close_dir; 190 } 191 vfio_devt = makedev(major, minor); 192 193 vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name); 194 ret = open_cdev(vfio_path, vfio_devt); 195 if (ret < 0) { 196 error_setg(errp, "Failed to open %s", vfio_path); 197 } 198 199 trace_iommufd_cdev_getfd(vfio_path, ret); 200 201 out_close_dir: 202 closedir(dir); 203 out: 204 if (*errp) { 205 error_prepend(errp, VFIO_MSG_PREFIX, path); 206 } 207 208 return ret; 209 } 210 211 static int iommufd_cdev_attach_ioas_hwpt(VFIODevice *vbasedev, uint32_t id, 212 Error **errp) 213 { 214 int iommufd = vbasedev->iommufd->fd; 215 struct vfio_device_attach_iommufd_pt attach_data = { 216 .argsz = sizeof(attach_data), 217 .flags = 0, 218 .pt_id = id, 219 }; 220 221 /* Attach device to an IOAS or hwpt within iommufd */ 222 if (ioctl(vbasedev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data)) { 223 error_setg_errno(errp, errno, 224 "[iommufd=%d] error attach %s (%d) to id=%d", 225 iommufd, vbasedev->name, vbasedev->fd, id); 226 return -errno; 227 } 228 229 trace_iommufd_cdev_attach_ioas_hwpt(iommufd, vbasedev->name, 230 vbasedev->fd, id); 231 return 0; 232 } 233 234 static bool iommufd_cdev_detach_ioas_hwpt(VFIODevice *vbasedev, Error **errp) 235 { 236 int iommufd = vbasedev->iommufd->fd; 237 struct vfio_device_detach_iommufd_pt detach_data = { 238 .argsz = sizeof(detach_data), 239 .flags = 0, 240 }; 241 242 if (ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_data)) { 243 error_setg_errno(errp, errno, "detach %s failed", vbasedev->name); 244 return false; 245 } 246 247 trace_iommufd_cdev_detach_ioas_hwpt(iommufd, vbasedev->name); 248 return true; 249 } 250 251 static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, 252 VFIOIOMMUFDContainer *container, 253 Error **errp) 254 { 255 ERRP_GUARD(); 256 IOMMUFDBackend *iommufd = vbasedev->iommufd; 257 uint32_t flags = 0; 258 VFIOIOASHwpt *hwpt; 259 uint32_t hwpt_id; 260 int ret; 261 262 /* Try to find a domain */ 263 QLIST_FOREACH(hwpt, &container->hwpt_list, next) { 264 ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp); 265 if (ret) { 266 /* -EINVAL means the domain is incompatible with the device. */ 267 if (ret == -EINVAL) { 268 /* 269 * It is an expected failure and it just means we will try 270 * another domain, or create one if no existing compatible 271 * domain is found. Hence why the error is discarded below. 272 */ 273 error_free(*errp); 274 *errp = NULL; 275 continue; 276 } 277 278 return false; 279 } else { 280 vbasedev->hwpt = hwpt; 281 QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next); 282 vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt); 283 return true; 284 } 285 } 286 287 /* 288 * This is quite early and VFIO Migration state isn't yet fully 289 * initialized, thus rely only on IOMMU hardware capabilities as to 290 * whether IOMMU dirty tracking is going to be requested. Later 291 * vfio_migration_realize() may decide to use VF dirty tracking 292 * instead. 293 */ 294 if (vbasedev->hiod->caps.hw_caps & IOMMU_HW_CAP_DIRTY_TRACKING) { 295 flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING; 296 } 297 298 if (!iommufd_backend_alloc_hwpt(iommufd, vbasedev->devid, 299 container->ioas_id, flags, 300 IOMMU_HWPT_DATA_NONE, 0, NULL, 301 &hwpt_id, errp)) { 302 return false; 303 } 304 305 hwpt = g_malloc0(sizeof(*hwpt)); 306 hwpt->hwpt_id = hwpt_id; 307 hwpt->hwpt_flags = flags; 308 QLIST_INIT(&hwpt->device_list); 309 310 ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp); 311 if (ret) { 312 iommufd_backend_free_id(container->be, hwpt->hwpt_id); 313 g_free(hwpt); 314 return false; 315 } 316 317 vbasedev->hwpt = hwpt; 318 vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt); 319 QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next); 320 QLIST_INSERT_HEAD(&container->hwpt_list, hwpt, next); 321 container->bcontainer.dirty_pages_supported |= 322 vbasedev->iommu_dirty_tracking; 323 if (container->bcontainer.dirty_pages_supported && 324 !vbasedev->iommu_dirty_tracking) { 325 warn_report("IOMMU instance for device %s doesn't support dirty tracking", 326 vbasedev->name); 327 } 328 return true; 329 } 330 331 static void iommufd_cdev_autodomains_put(VFIODevice *vbasedev, 332 VFIOIOMMUFDContainer *container) 333 { 334 VFIOIOASHwpt *hwpt = vbasedev->hwpt; 335 336 QLIST_REMOVE(vbasedev, hwpt_next); 337 vbasedev->hwpt = NULL; 338 339 if (QLIST_EMPTY(&hwpt->device_list)) { 340 QLIST_REMOVE(hwpt, next); 341 iommufd_backend_free_id(container->be, hwpt->hwpt_id); 342 g_free(hwpt); 343 } 344 } 345 346 static bool iommufd_cdev_attach_container(VFIODevice *vbasedev, 347 VFIOIOMMUFDContainer *container, 348 Error **errp) 349 { 350 /* mdevs aren't physical devices and will fail with auto domains */ 351 if (!vbasedev->mdev) { 352 return iommufd_cdev_autodomains_get(vbasedev, container, errp); 353 } 354 355 return !iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp); 356 } 357 358 static void iommufd_cdev_detach_container(VFIODevice *vbasedev, 359 VFIOIOMMUFDContainer *container) 360 { 361 Error *err = NULL; 362 363 if (!iommufd_cdev_detach_ioas_hwpt(vbasedev, &err)) { 364 error_report_err(err); 365 } 366 367 if (vbasedev->hwpt) { 368 iommufd_cdev_autodomains_put(vbasedev, container); 369 } 370 371 } 372 373 static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container) 374 { 375 VFIOContainerBase *bcontainer = &container->bcontainer; 376 377 if (!QLIST_EMPTY(&bcontainer->device_list)) { 378 return; 379 } 380 memory_listener_unregister(&bcontainer->listener); 381 iommufd_backend_free_id(container->be, container->ioas_id); 382 object_unref(container); 383 } 384 385 static int iommufd_cdev_ram_block_discard_disable(bool state) 386 { 387 /* 388 * We support coordinated discarding of RAM via the RamDiscardManager. 389 */ 390 return ram_block_uncoordinated_discard_disable(state); 391 } 392 393 static bool iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container, 394 uint32_t ioas_id, Error **errp) 395 { 396 VFIOContainerBase *bcontainer = &container->bcontainer; 397 g_autofree struct iommu_ioas_iova_ranges *info = NULL; 398 struct iommu_iova_range *iova_ranges; 399 int sz, fd = container->be->fd; 400 401 info = g_malloc0(sizeof(*info)); 402 info->size = sizeof(*info); 403 info->ioas_id = ioas_id; 404 405 if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info) && errno != EMSGSIZE) { 406 goto error; 407 } 408 409 sz = info->num_iovas * sizeof(struct iommu_iova_range); 410 info = g_realloc(info, sizeof(*info) + sz); 411 info->allowed_iovas = (uintptr_t)(info + 1); 412 413 if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info)) { 414 goto error; 415 } 416 417 iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas; 418 419 for (int i = 0; i < info->num_iovas; i++) { 420 Range *range = g_new(Range, 1); 421 422 range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last); 423 bcontainer->iova_ranges = 424 range_list_insert(bcontainer->iova_ranges, range); 425 } 426 bcontainer->pgsizes = info->out_iova_alignment; 427 428 return true; 429 430 error: 431 error_setg_errno(errp, errno, "Cannot get IOVA ranges"); 432 return false; 433 } 434 435 static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev, 436 AddressSpace *as, Error **errp) 437 { 438 VFIOContainerBase *bcontainer; 439 VFIOIOMMUFDContainer *container; 440 VFIOAddressSpace *space; 441 struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; 442 int ret, devfd; 443 uint32_t ioas_id; 444 Error *err = NULL; 445 const VFIOIOMMUClass *iommufd_vioc = 446 VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 447 448 if (vbasedev->fd < 0) { 449 devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp); 450 if (devfd < 0) { 451 return false; 452 } 453 vbasedev->fd = devfd; 454 } else { 455 devfd = vbasedev->fd; 456 } 457 458 if (!iommufd_cdev_connect_and_bind(vbasedev, errp)) { 459 goto err_connect_bind; 460 } 461 462 space = vfio_get_address_space(as); 463 464 /* 465 * The HostIOMMUDevice data from legacy backend is static and doesn't need 466 * any information from the (type1-iommu) backend to be initialized. In 467 * contrast however, the IOMMUFD HostIOMMUDevice data requires the iommufd 468 * FD to be connected and having a devid to be able to successfully call 469 * iommufd_backend_get_device_info(). 470 */ 471 if (!vfio_device_hiod_realize(vbasedev, errp)) { 472 goto err_alloc_ioas; 473 } 474 475 /* try to attach to an existing container in this space */ 476 QLIST_FOREACH(bcontainer, &space->containers, next) { 477 container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); 478 if (VFIO_IOMMU_GET_CLASS(bcontainer) != iommufd_vioc || 479 vbasedev->iommufd != container->be) { 480 continue; 481 } 482 if (!iommufd_cdev_attach_container(vbasedev, container, &err)) { 483 const char *msg = error_get_pretty(err); 484 485 trace_iommufd_cdev_fail_attach_existing_container(msg); 486 error_free(err); 487 err = NULL; 488 } else { 489 ret = iommufd_cdev_ram_block_discard_disable(true); 490 if (ret) { 491 error_setg(errp, 492 "Cannot set discarding of RAM broken (%d)", ret); 493 goto err_discard_disable; 494 } 495 goto found_container; 496 } 497 } 498 499 /* Need to allocate a new dedicated container */ 500 if (!iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp)) { 501 goto err_alloc_ioas; 502 } 503 504 trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id); 505 506 container = VFIO_IOMMU_IOMMUFD(object_new(TYPE_VFIO_IOMMU_IOMMUFD)); 507 container->be = vbasedev->iommufd; 508 container->ioas_id = ioas_id; 509 QLIST_INIT(&container->hwpt_list); 510 511 bcontainer = &container->bcontainer; 512 vfio_address_space_insert(space, bcontainer); 513 514 if (!iommufd_cdev_attach_container(vbasedev, container, errp)) { 515 goto err_attach_container; 516 } 517 518 ret = iommufd_cdev_ram_block_discard_disable(true); 519 if (ret) { 520 goto err_discard_disable; 521 } 522 523 if (!iommufd_cdev_get_info_iova_range(container, ioas_id, &err)) { 524 error_append_hint(&err, 525 "Fallback to default 64bit IOVA range and 4K page size\n"); 526 warn_report_err(err); 527 err = NULL; 528 bcontainer->pgsizes = qemu_real_host_page_size(); 529 } 530 531 bcontainer->listener = vfio_memory_listener; 532 memory_listener_register(&bcontainer->listener, bcontainer->space->as); 533 534 if (bcontainer->error) { 535 error_propagate_prepend(errp, bcontainer->error, 536 "memory listener initialization failed: "); 537 goto err_listener_register; 538 } 539 540 bcontainer->initialized = true; 541 542 found_container: 543 ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info); 544 if (ret) { 545 error_setg_errno(errp, errno, "error getting device info"); 546 goto err_listener_register; 547 } 548 549 if (!vfio_cpr_register_container(bcontainer, errp)) { 550 goto err_listener_register; 551 } 552 553 /* 554 * TODO: examine RAM_BLOCK_DISCARD stuff, should we do group level 555 * for discarding incompatibility check as well? 556 */ 557 if (vbasedev->ram_block_discard_allowed) { 558 iommufd_cdev_ram_block_discard_disable(false); 559 } 560 561 vbasedev->group = 0; 562 vbasedev->num_irqs = dev_info.num_irqs; 563 vbasedev->num_regions = dev_info.num_regions; 564 vbasedev->flags = dev_info.flags; 565 vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); 566 vbasedev->bcontainer = bcontainer; 567 QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next); 568 QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next); 569 570 trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs, 571 vbasedev->num_regions, vbasedev->flags); 572 return true; 573 574 err_listener_register: 575 iommufd_cdev_ram_block_discard_disable(false); 576 err_discard_disable: 577 iommufd_cdev_detach_container(vbasedev, container); 578 err_attach_container: 579 iommufd_cdev_container_destroy(container); 580 err_alloc_ioas: 581 vfio_put_address_space(space); 582 iommufd_cdev_unbind_and_disconnect(vbasedev); 583 err_connect_bind: 584 close(vbasedev->fd); 585 return false; 586 } 587 588 static void iommufd_cdev_detach(VFIODevice *vbasedev) 589 { 590 VFIOContainerBase *bcontainer = vbasedev->bcontainer; 591 VFIOAddressSpace *space = bcontainer->space; 592 VFIOIOMMUFDContainer *container = container_of(bcontainer, 593 VFIOIOMMUFDContainer, 594 bcontainer); 595 QLIST_REMOVE(vbasedev, global_next); 596 QLIST_REMOVE(vbasedev, container_next); 597 vbasedev->bcontainer = NULL; 598 599 if (!vbasedev->ram_block_discard_allowed) { 600 iommufd_cdev_ram_block_discard_disable(false); 601 } 602 603 vfio_cpr_unregister_container(bcontainer); 604 iommufd_cdev_detach_container(vbasedev, container); 605 iommufd_cdev_container_destroy(container); 606 vfio_put_address_space(space); 607 608 iommufd_cdev_unbind_and_disconnect(vbasedev); 609 close(vbasedev->fd); 610 } 611 612 static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid) 613 { 614 VFIODevice *vbasedev_iter; 615 const VFIOIOMMUClass *iommufd_vioc = 616 VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 617 618 QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) { 619 if (VFIO_IOMMU_GET_CLASS(vbasedev_iter->bcontainer) != iommufd_vioc) { 620 continue; 621 } 622 if (devid == vbasedev_iter->devid) { 623 return vbasedev_iter; 624 } 625 } 626 return NULL; 627 } 628 629 static VFIOPCIDevice * 630 iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev, 631 VFIODevice *reset_dev) 632 { 633 VFIODevice *vbasedev_tmp; 634 635 if (dep_dev->devid == reset_dev->devid || 636 dep_dev->devid == VFIO_PCI_DEVID_OWNED) { 637 return NULL; 638 } 639 640 vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid); 641 if (!vbasedev_tmp || !vbasedev_tmp->dev->realized || 642 vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) { 643 return NULL; 644 } 645 646 return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev); 647 } 648 649 static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single) 650 { 651 VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev); 652 struct vfio_pci_hot_reset_info *info = NULL; 653 struct vfio_pci_dependent_device *devices; 654 struct vfio_pci_hot_reset *reset; 655 int ret, i; 656 bool multi = false; 657 658 trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi"); 659 660 if (!single) { 661 vfio_pci_pre_reset(vdev); 662 } 663 vdev->vbasedev.needs_reset = false; 664 665 ret = vfio_pci_get_pci_hot_reset_info(vdev, &info); 666 667 if (ret) { 668 goto out_single; 669 } 670 671 assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID); 672 673 devices = &info->devices[0]; 674 675 if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) { 676 if (!vdev->has_pm_reset) { 677 for (i = 0; i < info->count; i++) { 678 if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) { 679 error_report("vfio: Cannot reset device %s, " 680 "depends on device %04x:%02x:%02x.%x " 681 "which is not owned.", 682 vdev->vbasedev.name, devices[i].segment, 683 devices[i].bus, PCI_SLOT(devices[i].devfn), 684 PCI_FUNC(devices[i].devfn)); 685 } 686 } 687 } 688 ret = -EPERM; 689 goto out_single; 690 } 691 692 trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name); 693 694 for (i = 0; i < info->count; i++) { 695 VFIOPCIDevice *tmp; 696 697 trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment, 698 devices[i].bus, 699 PCI_SLOT(devices[i].devfn), 700 PCI_FUNC(devices[i].devfn), 701 devices[i].devid); 702 703 /* 704 * If a VFIO cdev device is resettable, all the dependent devices 705 * are either bound to same iommufd or within same iommu_groups as 706 * one of the iommufd bound devices. 707 */ 708 assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED); 709 710 tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev); 711 if (!tmp) { 712 continue; 713 } 714 715 if (single) { 716 ret = -EINVAL; 717 goto out_single; 718 } 719 vfio_pci_pre_reset(tmp); 720 tmp->vbasedev.needs_reset = false; 721 multi = true; 722 } 723 724 if (!single && !multi) { 725 ret = -EINVAL; 726 goto out_single; 727 } 728 729 /* Use zero length array for hot reset with iommufd backend */ 730 reset = g_malloc0(sizeof(*reset)); 731 reset->argsz = sizeof(*reset); 732 733 /* Bus reset! */ 734 ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset); 735 g_free(reset); 736 if (ret) { 737 ret = -errno; 738 } 739 740 trace_vfio_pci_hot_reset_result(vdev->vbasedev.name, 741 ret ? strerror(errno) : "Success"); 742 743 /* Re-enable INTx on affected devices */ 744 for (i = 0; i < info->count; i++) { 745 VFIOPCIDevice *tmp; 746 747 tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev); 748 if (!tmp) { 749 continue; 750 } 751 vfio_pci_post_reset(tmp); 752 } 753 out_single: 754 if (!single) { 755 vfio_pci_post_reset(vdev); 756 } 757 g_free(info); 758 759 return ret; 760 } 761 762 static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data) 763 { 764 VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass); 765 766 vioc->hiod_typename = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO; 767 768 vioc->dma_map = iommufd_cdev_map; 769 vioc->dma_unmap = iommufd_cdev_unmap; 770 vioc->attach_device = iommufd_cdev_attach; 771 vioc->detach_device = iommufd_cdev_detach; 772 vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset; 773 vioc->set_dirty_page_tracking = iommufd_set_dirty_page_tracking; 774 }; 775 776 static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque, 777 Error **errp) 778 { 779 VFIODevice *vdev = opaque; 780 HostIOMMUDeviceCaps *caps = &hiod->caps; 781 enum iommu_hw_info_type type; 782 union { 783 struct iommu_hw_info_vtd vtd; 784 } data; 785 uint64_t hw_caps; 786 787 hiod->agent = opaque; 788 789 if (!iommufd_backend_get_device_info(vdev->iommufd, vdev->devid, 790 &type, &data, sizeof(data), 791 &hw_caps, errp)) { 792 return false; 793 } 794 795 hiod->name = g_strdup(vdev->name); 796 caps->type = type; 797 caps->hw_caps = hw_caps; 798 799 return true; 800 } 801 802 static GList * 803 hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod) 804 { 805 VFIODevice *vdev = hiod->agent; 806 807 g_assert(vdev); 808 return vfio_container_get_iova_ranges(vdev->bcontainer); 809 } 810 811 static uint64_t 812 hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod) 813 { 814 VFIODevice *vdev = hiod->agent; 815 816 g_assert(vdev); 817 return vfio_container_get_page_size_mask(vdev->bcontainer); 818 } 819 820 821 static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data) 822 { 823 HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc); 824 825 hiodc->realize = hiod_iommufd_vfio_realize; 826 hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges; 827 hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask; 828 }; 829 830 static const TypeInfo types[] = { 831 { 832 .name = TYPE_VFIO_IOMMU_IOMMUFD, 833 .parent = TYPE_VFIO_IOMMU, 834 .instance_size = sizeof(VFIOIOMMUFDContainer), 835 .class_init = vfio_iommu_iommufd_class_init, 836 }, { 837 .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO, 838 .parent = TYPE_HOST_IOMMU_DEVICE_IOMMUFD, 839 .class_init = hiod_iommufd_vfio_class_init, 840 } 841 }; 842 843 DEFINE_TYPES(types) 844