15ee3dc7aSYi Liu /* 25ee3dc7aSYi Liu * iommufd container backend 35ee3dc7aSYi Liu * 45ee3dc7aSYi Liu * Copyright (C) 2023 Intel Corporation. 55ee3dc7aSYi Liu * Copyright Red Hat, Inc. 2023 65ee3dc7aSYi Liu * 75ee3dc7aSYi Liu * Authors: Yi Liu <yi.l.liu@intel.com> 85ee3dc7aSYi Liu * Eric Auger <eric.auger@redhat.com> 95ee3dc7aSYi Liu * 105ee3dc7aSYi Liu * SPDX-License-Identifier: GPL-2.0-or-later 115ee3dc7aSYi Liu */ 125ee3dc7aSYi Liu 135ee3dc7aSYi Liu #include "qemu/osdep.h" 145ee3dc7aSYi Liu #include <sys/ioctl.h> 155ee3dc7aSYi Liu #include <linux/vfio.h> 165ee3dc7aSYi Liu #include <linux/iommufd.h> 175ee3dc7aSYi Liu 185ee3dc7aSYi Liu #include "hw/vfio/vfio-common.h" 195ee3dc7aSYi Liu #include "qemu/error-report.h" 205ee3dc7aSYi Liu #include "trace.h" 215ee3dc7aSYi Liu #include "qapi/error.h" 225ee3dc7aSYi Liu #include "sysemu/iommufd.h" 235ee3dc7aSYi Liu #include "hw/qdev-core.h" 245ee3dc7aSYi Liu #include "sysemu/reset.h" 255ee3dc7aSYi Liu #include "qemu/cutils.h" 265ee3dc7aSYi Liu #include "qemu/chardev_open.h" 2796d6f85fSZhenzhong Duan #include "pci.h" 285ee3dc7aSYi Liu 294517c33cSZhenzhong Duan static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova, 305ee3dc7aSYi Liu ram_addr_t size, void *vaddr, bool readonly) 315ee3dc7aSYi Liu { 324517c33cSZhenzhong Duan const VFIOIOMMUFDContainer *container = 335ee3dc7aSYi Liu container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); 345ee3dc7aSYi Liu 355ee3dc7aSYi Liu return iommufd_backend_map_dma(container->be, 365ee3dc7aSYi Liu container->ioas_id, 375ee3dc7aSYi Liu iova, size, vaddr, readonly); 385ee3dc7aSYi Liu } 395ee3dc7aSYi Liu 404517c33cSZhenzhong Duan static int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer, 415ee3dc7aSYi Liu hwaddr iova, ram_addr_t size, 425ee3dc7aSYi Liu IOMMUTLBEntry *iotlb) 435ee3dc7aSYi Liu { 444517c33cSZhenzhong Duan const VFIOIOMMUFDContainer *container = 455ee3dc7aSYi Liu container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); 465ee3dc7aSYi Liu 475ee3dc7aSYi Liu /* TODO: Handle dma_unmap_bitmap with iotlb args (migration) */ 485ee3dc7aSYi Liu return iommufd_backend_unmap_dma(container->be, 495ee3dc7aSYi Liu container->ioas_id, iova, size); 505ee3dc7aSYi Liu } 515ee3dc7aSYi Liu 5245d0d8c4SZhenzhong Duan static bool iommufd_cdev_kvm_device_add(VFIODevice *vbasedev, Error **errp) 535ee3dc7aSYi Liu { 5445d0d8c4SZhenzhong Duan return !vfio_kvm_device_add_fd(vbasedev->fd, errp); 555ee3dc7aSYi Liu } 565ee3dc7aSYi Liu 575ee3dc7aSYi Liu static void iommufd_cdev_kvm_device_del(VFIODevice *vbasedev) 585ee3dc7aSYi Liu { 595ee3dc7aSYi Liu Error *err = NULL; 605ee3dc7aSYi Liu 615ee3dc7aSYi Liu if (vfio_kvm_device_del_fd(vbasedev->fd, &err)) { 625ee3dc7aSYi Liu error_report_err(err); 635ee3dc7aSYi Liu } 645ee3dc7aSYi Liu } 655ee3dc7aSYi Liu 6645d0d8c4SZhenzhong Duan static bool iommufd_cdev_connect_and_bind(VFIODevice *vbasedev, Error **errp) 675ee3dc7aSYi Liu { 685ee3dc7aSYi Liu IOMMUFDBackend *iommufd = vbasedev->iommufd; 695ee3dc7aSYi Liu struct vfio_device_bind_iommufd bind = { 705ee3dc7aSYi Liu .argsz = sizeof(bind), 715ee3dc7aSYi Liu .flags = 0, 725ee3dc7aSYi Liu }; 735ee3dc7aSYi Liu 749067d50dSZhenzhong Duan if (!iommufd_backend_connect(iommufd, errp)) { 7545d0d8c4SZhenzhong Duan return false; 765ee3dc7aSYi Liu } 775ee3dc7aSYi Liu 785ee3dc7aSYi Liu /* 795ee3dc7aSYi Liu * Add device to kvm-vfio to be prepared for the tracking 805ee3dc7aSYi Liu * in KVM. Especially for some emulated devices, it requires 815ee3dc7aSYi Liu * to have kvm information in the device open. 825ee3dc7aSYi Liu */ 8345d0d8c4SZhenzhong Duan if (!iommufd_cdev_kvm_device_add(vbasedev, errp)) { 845ee3dc7aSYi Liu goto err_kvm_device_add; 855ee3dc7aSYi Liu } 865ee3dc7aSYi Liu 875ee3dc7aSYi Liu /* Bind device to iommufd */ 885ee3dc7aSYi Liu bind.iommufd = iommufd->fd; 8945d0d8c4SZhenzhong Duan if (ioctl(vbasedev->fd, VFIO_DEVICE_BIND_IOMMUFD, &bind)) { 905ee3dc7aSYi Liu error_setg_errno(errp, errno, "error bind device fd=%d to iommufd=%d", 915ee3dc7aSYi Liu vbasedev->fd, bind.iommufd); 925ee3dc7aSYi Liu goto err_bind; 935ee3dc7aSYi Liu } 945ee3dc7aSYi Liu 955ee3dc7aSYi Liu vbasedev->devid = bind.out_devid; 965ee3dc7aSYi Liu trace_iommufd_cdev_connect_and_bind(bind.iommufd, vbasedev->name, 975ee3dc7aSYi Liu vbasedev->fd, vbasedev->devid); 9845d0d8c4SZhenzhong Duan return true; 995ee3dc7aSYi Liu err_bind: 1005ee3dc7aSYi Liu iommufd_cdev_kvm_device_del(vbasedev); 1015ee3dc7aSYi Liu err_kvm_device_add: 1025ee3dc7aSYi Liu iommufd_backend_disconnect(iommufd); 10345d0d8c4SZhenzhong Duan return false; 1045ee3dc7aSYi Liu } 1055ee3dc7aSYi Liu 1065ee3dc7aSYi Liu static void iommufd_cdev_unbind_and_disconnect(VFIODevice *vbasedev) 1075ee3dc7aSYi Liu { 1085ee3dc7aSYi Liu /* Unbind is automatically conducted when device fd is closed */ 1095ee3dc7aSYi Liu iommufd_cdev_kvm_device_del(vbasedev); 1105ee3dc7aSYi Liu iommufd_backend_disconnect(vbasedev->iommufd); 1115ee3dc7aSYi Liu } 1125ee3dc7aSYi Liu 1135ee3dc7aSYi Liu static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp) 1145ee3dc7aSYi Liu { 1155cf8f51cSZhao Liu ERRP_GUARD(); 1165ee3dc7aSYi Liu long int ret = -ENOTTY; 11757001144SCédric Le Goater g_autofree char *path = NULL; 11857001144SCédric Le Goater g_autofree char *vfio_dev_path = NULL; 11957001144SCédric Le Goater g_autofree char *vfio_path = NULL; 1205ee3dc7aSYi Liu DIR *dir = NULL; 1215ee3dc7aSYi Liu struct dirent *dent; 12257001144SCédric Le Goater g_autofree gchar *contents = NULL; 1235ee3dc7aSYi Liu gsize length; 1245ee3dc7aSYi Liu int major, minor; 1255ee3dc7aSYi Liu dev_t vfio_devt; 1265ee3dc7aSYi Liu 1275ee3dc7aSYi Liu path = g_strdup_printf("%s/vfio-dev", sysfs_path); 1285ee3dc7aSYi Liu dir = opendir(path); 1295ee3dc7aSYi Liu if (!dir) { 1305ee3dc7aSYi Liu error_setg_errno(errp, errno, "couldn't open directory %s", path); 13157001144SCédric Le Goater goto out; 1325ee3dc7aSYi Liu } 1335ee3dc7aSYi Liu 1345ee3dc7aSYi Liu while ((dent = readdir(dir))) { 1355ee3dc7aSYi Liu if (!strncmp(dent->d_name, "vfio", 4)) { 1365ee3dc7aSYi Liu vfio_dev_path = g_strdup_printf("%s/%s/dev", path, dent->d_name); 1375ee3dc7aSYi Liu break; 1385ee3dc7aSYi Liu } 1395ee3dc7aSYi Liu } 1405ee3dc7aSYi Liu 1415ee3dc7aSYi Liu if (!vfio_dev_path) { 1425ee3dc7aSYi Liu error_setg(errp, "failed to find vfio-dev/vfioX/dev"); 1435ee3dc7aSYi Liu goto out_close_dir; 1445ee3dc7aSYi Liu } 1455ee3dc7aSYi Liu 1465ee3dc7aSYi Liu if (!g_file_get_contents(vfio_dev_path, &contents, &length, NULL)) { 1475ee3dc7aSYi Liu error_setg(errp, "failed to load \"%s\"", vfio_dev_path); 14857001144SCédric Le Goater goto out_close_dir; 1495ee3dc7aSYi Liu } 1505ee3dc7aSYi Liu 1515ee3dc7aSYi Liu if (sscanf(contents, "%d:%d", &major, &minor) != 2) { 1525ee3dc7aSYi Liu error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path); 15357001144SCédric Le Goater goto out_close_dir; 1545ee3dc7aSYi Liu } 1555ee3dc7aSYi Liu vfio_devt = makedev(major, minor); 1565ee3dc7aSYi Liu 1575ee3dc7aSYi Liu vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name); 1585ee3dc7aSYi Liu ret = open_cdev(vfio_path, vfio_devt); 1595ee3dc7aSYi Liu if (ret < 0) { 1605ee3dc7aSYi Liu error_setg(errp, "Failed to open %s", vfio_path); 1615ee3dc7aSYi Liu } 1625ee3dc7aSYi Liu 1635ee3dc7aSYi Liu trace_iommufd_cdev_getfd(vfio_path, ret); 1645ee3dc7aSYi Liu 1655ee3dc7aSYi Liu out_close_dir: 1665ee3dc7aSYi Liu closedir(dir); 16757001144SCédric Le Goater out: 1685ee3dc7aSYi Liu if (*errp) { 1695ee3dc7aSYi Liu error_prepend(errp, VFIO_MSG_PREFIX, path); 1705ee3dc7aSYi Liu } 1715ee3dc7aSYi Liu 1725ee3dc7aSYi Liu return ret; 1735ee3dc7aSYi Liu } 1745ee3dc7aSYi Liu 17545d0d8c4SZhenzhong Duan static bool iommufd_cdev_attach_ioas_hwpt(VFIODevice *vbasedev, uint32_t id, 1765ee3dc7aSYi Liu Error **errp) 1775ee3dc7aSYi Liu { 17845d0d8c4SZhenzhong Duan int iommufd = vbasedev->iommufd->fd; 1795ee3dc7aSYi Liu struct vfio_device_attach_iommufd_pt attach_data = { 1805ee3dc7aSYi Liu .argsz = sizeof(attach_data), 1815ee3dc7aSYi Liu .flags = 0, 1825ee3dc7aSYi Liu .pt_id = id, 1835ee3dc7aSYi Liu }; 1845ee3dc7aSYi Liu 1855ee3dc7aSYi Liu /* Attach device to an IOAS or hwpt within iommufd */ 18645d0d8c4SZhenzhong Duan if (ioctl(vbasedev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data)) { 1875ee3dc7aSYi Liu error_setg_errno(errp, errno, 1885ee3dc7aSYi Liu "[iommufd=%d] error attach %s (%d) to id=%d", 1895ee3dc7aSYi Liu iommufd, vbasedev->name, vbasedev->fd, id); 19045d0d8c4SZhenzhong Duan return false; 1915ee3dc7aSYi Liu } 1925ee3dc7aSYi Liu 19345d0d8c4SZhenzhong Duan trace_iommufd_cdev_attach_ioas_hwpt(iommufd, vbasedev->name, 19445d0d8c4SZhenzhong Duan vbasedev->fd, id); 19545d0d8c4SZhenzhong Duan return true; 19645d0d8c4SZhenzhong Duan } 19745d0d8c4SZhenzhong Duan 19845d0d8c4SZhenzhong Duan static bool iommufd_cdev_detach_ioas_hwpt(VFIODevice *vbasedev, Error **errp) 1995ee3dc7aSYi Liu { 20045d0d8c4SZhenzhong Duan int iommufd = vbasedev->iommufd->fd; 2015ee3dc7aSYi Liu struct vfio_device_detach_iommufd_pt detach_data = { 2025ee3dc7aSYi Liu .argsz = sizeof(detach_data), 2035ee3dc7aSYi Liu .flags = 0, 2045ee3dc7aSYi Liu }; 2055ee3dc7aSYi Liu 20645d0d8c4SZhenzhong Duan if (ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_data)) { 2075ee3dc7aSYi Liu error_setg_errno(errp, errno, "detach %s failed", vbasedev->name); 20845d0d8c4SZhenzhong Duan return false; 2095ee3dc7aSYi Liu } 2105ee3dc7aSYi Liu 21145d0d8c4SZhenzhong Duan trace_iommufd_cdev_detach_ioas_hwpt(iommufd, vbasedev->name); 21245d0d8c4SZhenzhong Duan return true; 21345d0d8c4SZhenzhong Duan } 21445d0d8c4SZhenzhong Duan 21545d0d8c4SZhenzhong Duan static bool iommufd_cdev_attach_container(VFIODevice *vbasedev, 2165ee3dc7aSYi Liu VFIOIOMMUFDContainer *container, 2175ee3dc7aSYi Liu Error **errp) 2185ee3dc7aSYi Liu { 2195ee3dc7aSYi Liu return iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp); 2205ee3dc7aSYi Liu } 2215ee3dc7aSYi Liu 2225ee3dc7aSYi Liu static void iommufd_cdev_detach_container(VFIODevice *vbasedev, 2235ee3dc7aSYi Liu VFIOIOMMUFDContainer *container) 2245ee3dc7aSYi Liu { 2255ee3dc7aSYi Liu Error *err = NULL; 2265ee3dc7aSYi Liu 22745d0d8c4SZhenzhong Duan if (!iommufd_cdev_detach_ioas_hwpt(vbasedev, &err)) { 2285ee3dc7aSYi Liu error_report_err(err); 2295ee3dc7aSYi Liu } 2305ee3dc7aSYi Liu } 2315ee3dc7aSYi Liu 2325ee3dc7aSYi Liu static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container) 2335ee3dc7aSYi Liu { 2345ee3dc7aSYi Liu VFIOContainerBase *bcontainer = &container->bcontainer; 2355ee3dc7aSYi Liu 2365ee3dc7aSYi Liu if (!QLIST_EMPTY(&bcontainer->device_list)) { 2375ee3dc7aSYi Liu return; 2385ee3dc7aSYi Liu } 2395ee3dc7aSYi Liu memory_listener_unregister(&bcontainer->listener); 2405ee3dc7aSYi Liu vfio_container_destroy(bcontainer); 2415ee3dc7aSYi Liu iommufd_backend_free_id(container->be, container->ioas_id); 2425ee3dc7aSYi Liu g_free(container); 2435ee3dc7aSYi Liu } 2445ee3dc7aSYi Liu 2455ee3dc7aSYi Liu static int iommufd_cdev_ram_block_discard_disable(bool state) 2465ee3dc7aSYi Liu { 2475ee3dc7aSYi Liu /* 2485ee3dc7aSYi Liu * We support coordinated discarding of RAM via the RamDiscardManager. 2495ee3dc7aSYi Liu */ 2505ee3dc7aSYi Liu return ram_block_uncoordinated_discard_disable(state); 2515ee3dc7aSYi Liu } 2525ee3dc7aSYi Liu 25345d0d8c4SZhenzhong Duan static bool iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container, 254714e9affSZhenzhong Duan uint32_t ioas_id, Error **errp) 255714e9affSZhenzhong Duan { 256714e9affSZhenzhong Duan VFIOContainerBase *bcontainer = &container->bcontainer; 257f3758413SZhenzhong Duan g_autofree struct iommu_ioas_iova_ranges *info = NULL; 258714e9affSZhenzhong Duan struct iommu_iova_range *iova_ranges; 25945d0d8c4SZhenzhong Duan int sz, fd = container->be->fd; 260714e9affSZhenzhong Duan 261714e9affSZhenzhong Duan info = g_malloc0(sizeof(*info)); 262714e9affSZhenzhong Duan info->size = sizeof(*info); 263714e9affSZhenzhong Duan info->ioas_id = ioas_id; 264714e9affSZhenzhong Duan 26545d0d8c4SZhenzhong Duan if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info) && errno != EMSGSIZE) { 266714e9affSZhenzhong Duan goto error; 267714e9affSZhenzhong Duan } 268714e9affSZhenzhong Duan 269714e9affSZhenzhong Duan sz = info->num_iovas * sizeof(struct iommu_iova_range); 270714e9affSZhenzhong Duan info = g_realloc(info, sizeof(*info) + sz); 271714e9affSZhenzhong Duan info->allowed_iovas = (uintptr_t)(info + 1); 272714e9affSZhenzhong Duan 27345d0d8c4SZhenzhong Duan if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info)) { 274714e9affSZhenzhong Duan goto error; 275714e9affSZhenzhong Duan } 276714e9affSZhenzhong Duan 277714e9affSZhenzhong Duan iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas; 278714e9affSZhenzhong Duan 279714e9affSZhenzhong Duan for (int i = 0; i < info->num_iovas; i++) { 280714e9affSZhenzhong Duan Range *range = g_new(Range, 1); 281714e9affSZhenzhong Duan 282714e9affSZhenzhong Duan range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last); 283714e9affSZhenzhong Duan bcontainer->iova_ranges = 284714e9affSZhenzhong Duan range_list_insert(bcontainer->iova_ranges, range); 285714e9affSZhenzhong Duan } 286714e9affSZhenzhong Duan bcontainer->pgsizes = info->out_iova_alignment; 287714e9affSZhenzhong Duan 28845d0d8c4SZhenzhong Duan return true; 289714e9affSZhenzhong Duan 290714e9affSZhenzhong Duan error: 291714e9affSZhenzhong Duan error_setg_errno(errp, errno, "Cannot get IOVA ranges"); 29245d0d8c4SZhenzhong Duan return false; 293714e9affSZhenzhong Duan } 294714e9affSZhenzhong Duan 295b7754835SZhenzhong Duan static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev, 2965ee3dc7aSYi Liu AddressSpace *as, Error **errp) 2975ee3dc7aSYi Liu { 2985ee3dc7aSYi Liu VFIOContainerBase *bcontainer; 2995ee3dc7aSYi Liu VFIOIOMMUFDContainer *container; 3005ee3dc7aSYi Liu VFIOAddressSpace *space; 3015ee3dc7aSYi Liu struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; 3025ee3dc7aSYi Liu int ret, devfd; 3035ee3dc7aSYi Liu uint32_t ioas_id; 3045ee3dc7aSYi Liu Error *err = NULL; 305ce5f6d49SCédric Le Goater const VFIOIOMMUClass *iommufd_vioc = 306ce5f6d49SCédric Le Goater VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 3075ee3dc7aSYi Liu 308da3e04b2SZhenzhong Duan if (vbasedev->fd < 0) { 3095ee3dc7aSYi Liu devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp); 3105ee3dc7aSYi Liu if (devfd < 0) { 311b7754835SZhenzhong Duan return false; 3125ee3dc7aSYi Liu } 3135ee3dc7aSYi Liu vbasedev->fd = devfd; 314da3e04b2SZhenzhong Duan } else { 315da3e04b2SZhenzhong Duan devfd = vbasedev->fd; 316da3e04b2SZhenzhong Duan } 3175ee3dc7aSYi Liu 31845d0d8c4SZhenzhong Duan if (!iommufd_cdev_connect_and_bind(vbasedev, errp)) { 3195ee3dc7aSYi Liu goto err_connect_bind; 3205ee3dc7aSYi Liu } 3215ee3dc7aSYi Liu 3225ee3dc7aSYi Liu space = vfio_get_address_space(as); 3235ee3dc7aSYi Liu 3245ee3dc7aSYi Liu /* try to attach to an existing container in this space */ 3255ee3dc7aSYi Liu QLIST_FOREACH(bcontainer, &space->containers, next) { 3265ee3dc7aSYi Liu container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); 327ce5f6d49SCédric Le Goater if (bcontainer->ops != iommufd_vioc || 3285ee3dc7aSYi Liu vbasedev->iommufd != container->be) { 3295ee3dc7aSYi Liu continue; 3305ee3dc7aSYi Liu } 33145d0d8c4SZhenzhong Duan if (!iommufd_cdev_attach_container(vbasedev, container, &err)) { 3325ee3dc7aSYi Liu const char *msg = error_get_pretty(err); 3335ee3dc7aSYi Liu 3345ee3dc7aSYi Liu trace_iommufd_cdev_fail_attach_existing_container(msg); 3355ee3dc7aSYi Liu error_free(err); 3365ee3dc7aSYi Liu err = NULL; 3375ee3dc7aSYi Liu } else { 3385ee3dc7aSYi Liu ret = iommufd_cdev_ram_block_discard_disable(true); 3395ee3dc7aSYi Liu if (ret) { 3405ee3dc7aSYi Liu error_setg(errp, 3415ee3dc7aSYi Liu "Cannot set discarding of RAM broken (%d)", ret); 3425ee3dc7aSYi Liu goto err_discard_disable; 3435ee3dc7aSYi Liu } 3445ee3dc7aSYi Liu goto found_container; 3455ee3dc7aSYi Liu } 3465ee3dc7aSYi Liu } 3475ee3dc7aSYi Liu 3485ee3dc7aSYi Liu /* Need to allocate a new dedicated container */ 3499067d50dSZhenzhong Duan if (!iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp)) { 3505ee3dc7aSYi Liu goto err_alloc_ioas; 3515ee3dc7aSYi Liu } 3525ee3dc7aSYi Liu 3535ee3dc7aSYi Liu trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id); 3545ee3dc7aSYi Liu 3555ee3dc7aSYi Liu container = g_malloc0(sizeof(*container)); 3565ee3dc7aSYi Liu container->be = vbasedev->iommufd; 3575ee3dc7aSYi Liu container->ioas_id = ioas_id; 3585ee3dc7aSYi Liu 3595ee3dc7aSYi Liu bcontainer = &container->bcontainer; 36009181a8eSCédric Le Goater vfio_container_init(bcontainer, iommufd_vioc); 361b7b79588SCédric Le Goater vfio_address_space_insert(space, bcontainer); 3625ee3dc7aSYi Liu 36345d0d8c4SZhenzhong Duan if (!iommufd_cdev_attach_container(vbasedev, container, errp)) { 3645ee3dc7aSYi Liu goto err_attach_container; 3655ee3dc7aSYi Liu } 3665ee3dc7aSYi Liu 3675ee3dc7aSYi Liu ret = iommufd_cdev_ram_block_discard_disable(true); 3685ee3dc7aSYi Liu if (ret) { 3695ee3dc7aSYi Liu goto err_discard_disable; 3705ee3dc7aSYi Liu } 3715ee3dc7aSYi Liu 37245d0d8c4SZhenzhong Duan if (!iommufd_cdev_get_info_iova_range(container, ioas_id, &err)) { 373714e9affSZhenzhong Duan error_append_hint(&err, 374714e9affSZhenzhong Duan "Fallback to default 64bit IOVA range and 4K page size\n"); 375714e9affSZhenzhong Duan warn_report_err(err); 376714e9affSZhenzhong Duan err = NULL; 3775ee3dc7aSYi Liu bcontainer->pgsizes = qemu_real_host_page_size(); 378714e9affSZhenzhong Duan } 3795ee3dc7aSYi Liu 3805ee3dc7aSYi Liu bcontainer->listener = vfio_memory_listener; 3815ee3dc7aSYi Liu memory_listener_register(&bcontainer->listener, bcontainer->space->as); 3825ee3dc7aSYi Liu 3835ee3dc7aSYi Liu if (bcontainer->error) { 3845ee3dc7aSYi Liu error_propagate_prepend(errp, bcontainer->error, 3855ee3dc7aSYi Liu "memory listener initialization failed: "); 3865ee3dc7aSYi Liu goto err_listener_register; 3875ee3dc7aSYi Liu } 3885ee3dc7aSYi Liu 3895ee3dc7aSYi Liu bcontainer->initialized = true; 3905ee3dc7aSYi Liu 3915ee3dc7aSYi Liu found_container: 3925ee3dc7aSYi Liu ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info); 3935ee3dc7aSYi Liu if (ret) { 3945ee3dc7aSYi Liu error_setg_errno(errp, errno, "error getting device info"); 3955ee3dc7aSYi Liu goto err_listener_register; 3965ee3dc7aSYi Liu } 3975ee3dc7aSYi Liu 398f38f5dd1SZhenzhong Duan if (!vfio_cpr_register_container(bcontainer, errp)) { 399d9fa4223SSteve Sistare goto err_listener_register; 400d9fa4223SSteve Sistare } 401d9fa4223SSteve Sistare 4025ee3dc7aSYi Liu /* 4035ee3dc7aSYi Liu * TODO: examine RAM_BLOCK_DISCARD stuff, should we do group level 4045ee3dc7aSYi Liu * for discarding incompatibility check as well? 4055ee3dc7aSYi Liu */ 4065ee3dc7aSYi Liu if (vbasedev->ram_block_discard_allowed) { 4075ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false); 4085ee3dc7aSYi Liu } 4095ee3dc7aSYi Liu 4105ee3dc7aSYi Liu vbasedev->group = 0; 4115ee3dc7aSYi Liu vbasedev->num_irqs = dev_info.num_irqs; 4125ee3dc7aSYi Liu vbasedev->num_regions = dev_info.num_regions; 4135ee3dc7aSYi Liu vbasedev->flags = dev_info.flags; 4145ee3dc7aSYi Liu vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); 4155ee3dc7aSYi Liu vbasedev->bcontainer = bcontainer; 4165ee3dc7aSYi Liu QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next); 4175ee3dc7aSYi Liu QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next); 4185ee3dc7aSYi Liu 4195ee3dc7aSYi Liu trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs, 4205ee3dc7aSYi Liu vbasedev->num_regions, vbasedev->flags); 421b7754835SZhenzhong Duan return true; 4225ee3dc7aSYi Liu 4235ee3dc7aSYi Liu err_listener_register: 4245ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false); 4255ee3dc7aSYi Liu err_discard_disable: 4265ee3dc7aSYi Liu iommufd_cdev_detach_container(vbasedev, container); 4275ee3dc7aSYi Liu err_attach_container: 4285ee3dc7aSYi Liu iommufd_cdev_container_destroy(container); 4295ee3dc7aSYi Liu err_alloc_ioas: 4305ee3dc7aSYi Liu vfio_put_address_space(space); 4315ee3dc7aSYi Liu iommufd_cdev_unbind_and_disconnect(vbasedev); 4325ee3dc7aSYi Liu err_connect_bind: 4335ee3dc7aSYi Liu close(vbasedev->fd); 434b7754835SZhenzhong Duan return false; 4355ee3dc7aSYi Liu } 4365ee3dc7aSYi Liu 4375ee3dc7aSYi Liu static void iommufd_cdev_detach(VFIODevice *vbasedev) 4385ee3dc7aSYi Liu { 4395ee3dc7aSYi Liu VFIOContainerBase *bcontainer = vbasedev->bcontainer; 4405ee3dc7aSYi Liu VFIOAddressSpace *space = bcontainer->space; 4415ee3dc7aSYi Liu VFIOIOMMUFDContainer *container = container_of(bcontainer, 4425ee3dc7aSYi Liu VFIOIOMMUFDContainer, 4435ee3dc7aSYi Liu bcontainer); 4445ee3dc7aSYi Liu QLIST_REMOVE(vbasedev, global_next); 4455ee3dc7aSYi Liu QLIST_REMOVE(vbasedev, container_next); 4465ee3dc7aSYi Liu vbasedev->bcontainer = NULL; 4475ee3dc7aSYi Liu 4485ee3dc7aSYi Liu if (!vbasedev->ram_block_discard_allowed) { 4495ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false); 4505ee3dc7aSYi Liu } 4515ee3dc7aSYi Liu 452d9fa4223SSteve Sistare vfio_cpr_unregister_container(bcontainer); 4535ee3dc7aSYi Liu iommufd_cdev_detach_container(vbasedev, container); 4545ee3dc7aSYi Liu iommufd_cdev_container_destroy(container); 4555ee3dc7aSYi Liu vfio_put_address_space(space); 4565ee3dc7aSYi Liu 4575ee3dc7aSYi Liu iommufd_cdev_unbind_and_disconnect(vbasedev); 4585ee3dc7aSYi Liu close(vbasedev->fd); 4595ee3dc7aSYi Liu } 4605ee3dc7aSYi Liu 46196d6f85fSZhenzhong Duan static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid) 46296d6f85fSZhenzhong Duan { 46396d6f85fSZhenzhong Duan VFIODevice *vbasedev_iter; 464ce5f6d49SCédric Le Goater const VFIOIOMMUClass *iommufd_vioc = 465ce5f6d49SCédric Le Goater VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 46696d6f85fSZhenzhong Duan 46796d6f85fSZhenzhong Duan QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) { 468ce5f6d49SCédric Le Goater if (vbasedev_iter->bcontainer->ops != iommufd_vioc) { 46996d6f85fSZhenzhong Duan continue; 47096d6f85fSZhenzhong Duan } 47196d6f85fSZhenzhong Duan if (devid == vbasedev_iter->devid) { 47296d6f85fSZhenzhong Duan return vbasedev_iter; 47396d6f85fSZhenzhong Duan } 47496d6f85fSZhenzhong Duan } 47596d6f85fSZhenzhong Duan return NULL; 47696d6f85fSZhenzhong Duan } 47796d6f85fSZhenzhong Duan 47896d6f85fSZhenzhong Duan static VFIOPCIDevice * 47996d6f85fSZhenzhong Duan iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev, 48096d6f85fSZhenzhong Duan VFIODevice *reset_dev) 48196d6f85fSZhenzhong Duan { 48296d6f85fSZhenzhong Duan VFIODevice *vbasedev_tmp; 48396d6f85fSZhenzhong Duan 48496d6f85fSZhenzhong Duan if (dep_dev->devid == reset_dev->devid || 48596d6f85fSZhenzhong Duan dep_dev->devid == VFIO_PCI_DEVID_OWNED) { 48696d6f85fSZhenzhong Duan return NULL; 48796d6f85fSZhenzhong Duan } 48896d6f85fSZhenzhong Duan 48996d6f85fSZhenzhong Duan vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid); 49096d6f85fSZhenzhong Duan if (!vbasedev_tmp || !vbasedev_tmp->dev->realized || 49196d6f85fSZhenzhong Duan vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) { 49296d6f85fSZhenzhong Duan return NULL; 49396d6f85fSZhenzhong Duan } 49496d6f85fSZhenzhong Duan 49596d6f85fSZhenzhong Duan return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev); 49696d6f85fSZhenzhong Duan } 49796d6f85fSZhenzhong Duan 49896d6f85fSZhenzhong Duan static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single) 49996d6f85fSZhenzhong Duan { 50096d6f85fSZhenzhong Duan VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev); 50196d6f85fSZhenzhong Duan struct vfio_pci_hot_reset_info *info = NULL; 50296d6f85fSZhenzhong Duan struct vfio_pci_dependent_device *devices; 50396d6f85fSZhenzhong Duan struct vfio_pci_hot_reset *reset; 50496d6f85fSZhenzhong Duan int ret, i; 50596d6f85fSZhenzhong Duan bool multi = false; 50696d6f85fSZhenzhong Duan 50796d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi"); 50896d6f85fSZhenzhong Duan 50996d6f85fSZhenzhong Duan if (!single) { 51096d6f85fSZhenzhong Duan vfio_pci_pre_reset(vdev); 51196d6f85fSZhenzhong Duan } 51296d6f85fSZhenzhong Duan vdev->vbasedev.needs_reset = false; 51396d6f85fSZhenzhong Duan 51496d6f85fSZhenzhong Duan ret = vfio_pci_get_pci_hot_reset_info(vdev, &info); 51596d6f85fSZhenzhong Duan 51696d6f85fSZhenzhong Duan if (ret) { 51796d6f85fSZhenzhong Duan goto out_single; 51896d6f85fSZhenzhong Duan } 51996d6f85fSZhenzhong Duan 52096d6f85fSZhenzhong Duan assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID); 52196d6f85fSZhenzhong Duan 52296d6f85fSZhenzhong Duan devices = &info->devices[0]; 52396d6f85fSZhenzhong Duan 52496d6f85fSZhenzhong Duan if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) { 52596d6f85fSZhenzhong Duan if (!vdev->has_pm_reset) { 52696d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) { 52796d6f85fSZhenzhong Duan if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) { 52896d6f85fSZhenzhong Duan error_report("vfio: Cannot reset device %s, " 52996d6f85fSZhenzhong Duan "depends on device %04x:%02x:%02x.%x " 53096d6f85fSZhenzhong Duan "which is not owned.", 53196d6f85fSZhenzhong Duan vdev->vbasedev.name, devices[i].segment, 53296d6f85fSZhenzhong Duan devices[i].bus, PCI_SLOT(devices[i].devfn), 53396d6f85fSZhenzhong Duan PCI_FUNC(devices[i].devfn)); 53496d6f85fSZhenzhong Duan } 53596d6f85fSZhenzhong Duan } 53696d6f85fSZhenzhong Duan } 53796d6f85fSZhenzhong Duan ret = -EPERM; 53896d6f85fSZhenzhong Duan goto out_single; 53996d6f85fSZhenzhong Duan } 54096d6f85fSZhenzhong Duan 54196d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name); 54296d6f85fSZhenzhong Duan 54396d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) { 54496d6f85fSZhenzhong Duan VFIOPCIDevice *tmp; 54596d6f85fSZhenzhong Duan 54696d6f85fSZhenzhong Duan trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment, 54796d6f85fSZhenzhong Duan devices[i].bus, 54896d6f85fSZhenzhong Duan PCI_SLOT(devices[i].devfn), 54996d6f85fSZhenzhong Duan PCI_FUNC(devices[i].devfn), 55096d6f85fSZhenzhong Duan devices[i].devid); 55196d6f85fSZhenzhong Duan 55296d6f85fSZhenzhong Duan /* 55396d6f85fSZhenzhong Duan * If a VFIO cdev device is resettable, all the dependent devices 55496d6f85fSZhenzhong Duan * are either bound to same iommufd or within same iommu_groups as 55596d6f85fSZhenzhong Duan * one of the iommufd bound devices. 55696d6f85fSZhenzhong Duan */ 55796d6f85fSZhenzhong Duan assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED); 55896d6f85fSZhenzhong Duan 55996d6f85fSZhenzhong Duan tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev); 56096d6f85fSZhenzhong Duan if (!tmp) { 56196d6f85fSZhenzhong Duan continue; 56296d6f85fSZhenzhong Duan } 56396d6f85fSZhenzhong Duan 56496d6f85fSZhenzhong Duan if (single) { 56596d6f85fSZhenzhong Duan ret = -EINVAL; 56696d6f85fSZhenzhong Duan goto out_single; 56796d6f85fSZhenzhong Duan } 56896d6f85fSZhenzhong Duan vfio_pci_pre_reset(tmp); 56996d6f85fSZhenzhong Duan tmp->vbasedev.needs_reset = false; 57096d6f85fSZhenzhong Duan multi = true; 57196d6f85fSZhenzhong Duan } 57296d6f85fSZhenzhong Duan 57396d6f85fSZhenzhong Duan if (!single && !multi) { 57496d6f85fSZhenzhong Duan ret = -EINVAL; 57596d6f85fSZhenzhong Duan goto out_single; 57696d6f85fSZhenzhong Duan } 57796d6f85fSZhenzhong Duan 57896d6f85fSZhenzhong Duan /* Use zero length array for hot reset with iommufd backend */ 57996d6f85fSZhenzhong Duan reset = g_malloc0(sizeof(*reset)); 58096d6f85fSZhenzhong Duan reset->argsz = sizeof(*reset); 58196d6f85fSZhenzhong Duan 58296d6f85fSZhenzhong Duan /* Bus reset! */ 58396d6f85fSZhenzhong Duan ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset); 58496d6f85fSZhenzhong Duan g_free(reset); 58596d6f85fSZhenzhong Duan if (ret) { 58696d6f85fSZhenzhong Duan ret = -errno; 58796d6f85fSZhenzhong Duan } 58896d6f85fSZhenzhong Duan 58996d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset_result(vdev->vbasedev.name, 59096d6f85fSZhenzhong Duan ret ? strerror(errno) : "Success"); 59196d6f85fSZhenzhong Duan 59296d6f85fSZhenzhong Duan /* Re-enable INTx on affected devices */ 59396d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) { 59496d6f85fSZhenzhong Duan VFIOPCIDevice *tmp; 59596d6f85fSZhenzhong Duan 59696d6f85fSZhenzhong Duan tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev); 59796d6f85fSZhenzhong Duan if (!tmp) { 59896d6f85fSZhenzhong Duan continue; 59996d6f85fSZhenzhong Duan } 60096d6f85fSZhenzhong Duan vfio_pci_post_reset(tmp); 60196d6f85fSZhenzhong Duan } 60296d6f85fSZhenzhong Duan out_single: 60396d6f85fSZhenzhong Duan if (!single) { 60496d6f85fSZhenzhong Duan vfio_pci_post_reset(vdev); 60596d6f85fSZhenzhong Duan } 60696d6f85fSZhenzhong Duan g_free(info); 60796d6f85fSZhenzhong Duan 60896d6f85fSZhenzhong Duan return ret; 60996d6f85fSZhenzhong Duan } 61096d6f85fSZhenzhong Duan 611ce5f6d49SCédric Le Goater static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data) 612ce5f6d49SCédric Le Goater { 613ce5f6d49SCédric Le Goater VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass); 614ce5f6d49SCédric Le Goater 615a7fd91b8SZhenzhong Duan vioc->hiod_typename = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO; 616a7fd91b8SZhenzhong Duan 617ce5f6d49SCédric Le Goater vioc->dma_map = iommufd_cdev_map; 618ce5f6d49SCédric Le Goater vioc->dma_unmap = iommufd_cdev_unmap; 619ce5f6d49SCédric Le Goater vioc->attach_device = iommufd_cdev_attach; 620ce5f6d49SCédric Le Goater vioc->detach_device = iommufd_cdev_detach; 621ce5f6d49SCédric Le Goater vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset; 6225ee3dc7aSYi Liu }; 623ce5f6d49SCédric Le Goater 62493058952SZhenzhong Duan static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque, 62593058952SZhenzhong Duan Error **errp) 62693058952SZhenzhong Duan { 62793058952SZhenzhong Duan VFIODevice *vdev = opaque; 62893058952SZhenzhong Duan HostIOMMUDeviceCaps *caps = &hiod->caps; 62993058952SZhenzhong Duan enum iommu_hw_info_type type; 63093058952SZhenzhong Duan union { 63193058952SZhenzhong Duan struct iommu_hw_info_vtd vtd; 63293058952SZhenzhong Duan } data; 63393058952SZhenzhong Duan 634dc169694SEric Auger hiod->agent = opaque; 635dc169694SEric Auger 63693058952SZhenzhong Duan if (!iommufd_backend_get_device_info(vdev->iommufd, vdev->devid, 63793058952SZhenzhong Duan &type, &data, sizeof(data), errp)) { 63893058952SZhenzhong Duan return false; 63993058952SZhenzhong Duan } 64093058952SZhenzhong Duan 64193058952SZhenzhong Duan hiod->name = g_strdup(vdev->name); 64293058952SZhenzhong Duan caps->type = type; 64393058952SZhenzhong Duan caps->aw_bits = vfio_device_get_aw_bits(vdev); 64493058952SZhenzhong Duan 64593058952SZhenzhong Duan return true; 64693058952SZhenzhong Duan } 64793058952SZhenzhong Duan 6483ad35d91SEric Auger static GList * 6493ad35d91SEric Auger hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod, Error **errp) 6503ad35d91SEric Auger { 6513ad35d91SEric Auger VFIODevice *vdev = hiod->agent; 6523ad35d91SEric Auger GList *l = NULL; 6533ad35d91SEric Auger 6543ad35d91SEric Auger g_assert(vdev); 6553ad35d91SEric Auger 6563ad35d91SEric Auger if (vdev->bcontainer) { 6573ad35d91SEric Auger l = g_list_copy(vdev->bcontainer->iova_ranges); 6583ad35d91SEric Auger } 6593ad35d91SEric Auger 6603ad35d91SEric Auger return l; 6613ad35d91SEric Auger } 6623ad35d91SEric Auger 66393058952SZhenzhong Duan static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data) 66493058952SZhenzhong Duan { 66593058952SZhenzhong Duan HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc); 66693058952SZhenzhong Duan 66793058952SZhenzhong Duan hiodc->realize = hiod_iommufd_vfio_realize; 6683ad35d91SEric Auger hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges; 66993058952SZhenzhong Duan }; 67093058952SZhenzhong Duan 671ce5f6d49SCédric Le Goater static const TypeInfo types[] = { 672ce5f6d49SCédric Le Goater { 673ce5f6d49SCédric Le Goater .name = TYPE_VFIO_IOMMU_IOMMUFD, 674ce5f6d49SCédric Le Goater .parent = TYPE_VFIO_IOMMU, 675*504d297eSCédric Le Goater .instance_size = sizeof(VFIOIOMMUFDContainer), 676ce5f6d49SCédric Le Goater .class_init = vfio_iommu_iommufd_class_init, 6779005f928SZhenzhong Duan }, { 6789005f928SZhenzhong Duan .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO, 6799005f928SZhenzhong Duan .parent = TYPE_HOST_IOMMU_DEVICE_IOMMUFD, 68093058952SZhenzhong Duan .class_init = hiod_iommufd_vfio_class_init, 6819005f928SZhenzhong Duan } 682ce5f6d49SCédric Le Goater }; 683ce5f6d49SCédric Le Goater 684ce5f6d49SCédric Le Goater DEFINE_TYPES(types) 685