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 iommufd_backend_free_id(container->be, container->ioas_id); 24193802605SCédric Le Goater object_unref(container); 2425ee3dc7aSYi Liu } 2435ee3dc7aSYi Liu 2445ee3dc7aSYi Liu static int iommufd_cdev_ram_block_discard_disable(bool state) 2455ee3dc7aSYi Liu { 2465ee3dc7aSYi Liu /* 2475ee3dc7aSYi Liu * We support coordinated discarding of RAM via the RamDiscardManager. 2485ee3dc7aSYi Liu */ 2495ee3dc7aSYi Liu return ram_block_uncoordinated_discard_disable(state); 2505ee3dc7aSYi Liu } 2515ee3dc7aSYi Liu 25245d0d8c4SZhenzhong Duan static bool iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container, 253714e9affSZhenzhong Duan uint32_t ioas_id, Error **errp) 254714e9affSZhenzhong Duan { 255714e9affSZhenzhong Duan VFIOContainerBase *bcontainer = &container->bcontainer; 256f3758413SZhenzhong Duan g_autofree struct iommu_ioas_iova_ranges *info = NULL; 257714e9affSZhenzhong Duan struct iommu_iova_range *iova_ranges; 25845d0d8c4SZhenzhong Duan int sz, fd = container->be->fd; 259714e9affSZhenzhong Duan 260714e9affSZhenzhong Duan info = g_malloc0(sizeof(*info)); 261714e9affSZhenzhong Duan info->size = sizeof(*info); 262714e9affSZhenzhong Duan info->ioas_id = ioas_id; 263714e9affSZhenzhong Duan 26445d0d8c4SZhenzhong Duan if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info) && errno != EMSGSIZE) { 265714e9affSZhenzhong Duan goto error; 266714e9affSZhenzhong Duan } 267714e9affSZhenzhong Duan 268714e9affSZhenzhong Duan sz = info->num_iovas * sizeof(struct iommu_iova_range); 269714e9affSZhenzhong Duan info = g_realloc(info, sizeof(*info) + sz); 270714e9affSZhenzhong Duan info->allowed_iovas = (uintptr_t)(info + 1); 271714e9affSZhenzhong Duan 27245d0d8c4SZhenzhong Duan if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info)) { 273714e9affSZhenzhong Duan goto error; 274714e9affSZhenzhong Duan } 275714e9affSZhenzhong Duan 276714e9affSZhenzhong Duan iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas; 277714e9affSZhenzhong Duan 278714e9affSZhenzhong Duan for (int i = 0; i < info->num_iovas; i++) { 279714e9affSZhenzhong Duan Range *range = g_new(Range, 1); 280714e9affSZhenzhong Duan 281714e9affSZhenzhong Duan range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last); 282714e9affSZhenzhong Duan bcontainer->iova_ranges = 283714e9affSZhenzhong Duan range_list_insert(bcontainer->iova_ranges, range); 284714e9affSZhenzhong Duan } 285714e9affSZhenzhong Duan bcontainer->pgsizes = info->out_iova_alignment; 286714e9affSZhenzhong Duan 28745d0d8c4SZhenzhong Duan return true; 288714e9affSZhenzhong Duan 289714e9affSZhenzhong Duan error: 290714e9affSZhenzhong Duan error_setg_errno(errp, errno, "Cannot get IOVA ranges"); 29145d0d8c4SZhenzhong Duan return false; 292714e9affSZhenzhong Duan } 293714e9affSZhenzhong Duan 294b7754835SZhenzhong Duan static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev, 2955ee3dc7aSYi Liu AddressSpace *as, Error **errp) 2965ee3dc7aSYi Liu { 2975ee3dc7aSYi Liu VFIOContainerBase *bcontainer; 2985ee3dc7aSYi Liu VFIOIOMMUFDContainer *container; 2995ee3dc7aSYi Liu VFIOAddressSpace *space; 3005ee3dc7aSYi Liu struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; 3015ee3dc7aSYi Liu int ret, devfd; 3025ee3dc7aSYi Liu uint32_t ioas_id; 3035ee3dc7aSYi Liu Error *err = NULL; 304ce5f6d49SCédric Le Goater const VFIOIOMMUClass *iommufd_vioc = 305ce5f6d49SCédric Le Goater VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 3065ee3dc7aSYi Liu 307da3e04b2SZhenzhong Duan if (vbasedev->fd < 0) { 3085ee3dc7aSYi Liu devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp); 3095ee3dc7aSYi Liu if (devfd < 0) { 310b7754835SZhenzhong Duan return false; 3115ee3dc7aSYi Liu } 3125ee3dc7aSYi Liu vbasedev->fd = devfd; 313da3e04b2SZhenzhong Duan } else { 314da3e04b2SZhenzhong Duan devfd = vbasedev->fd; 315da3e04b2SZhenzhong Duan } 3165ee3dc7aSYi Liu 31745d0d8c4SZhenzhong Duan if (!iommufd_cdev_connect_and_bind(vbasedev, errp)) { 3185ee3dc7aSYi Liu goto err_connect_bind; 3195ee3dc7aSYi Liu } 3205ee3dc7aSYi Liu 3215ee3dc7aSYi Liu space = vfio_get_address_space(as); 3225ee3dc7aSYi Liu 3235ee3dc7aSYi Liu /* try to attach to an existing container in this space */ 3245ee3dc7aSYi Liu QLIST_FOREACH(bcontainer, &space->containers, next) { 3255ee3dc7aSYi Liu container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); 32641d698b8SCédric Le Goater if (VFIO_IOMMU_GET_CLASS(bcontainer) != iommufd_vioc || 3275ee3dc7aSYi Liu vbasedev->iommufd != container->be) { 3285ee3dc7aSYi Liu continue; 3295ee3dc7aSYi Liu } 33045d0d8c4SZhenzhong Duan if (!iommufd_cdev_attach_container(vbasedev, container, &err)) { 3315ee3dc7aSYi Liu const char *msg = error_get_pretty(err); 3325ee3dc7aSYi Liu 3335ee3dc7aSYi Liu trace_iommufd_cdev_fail_attach_existing_container(msg); 3345ee3dc7aSYi Liu error_free(err); 3355ee3dc7aSYi Liu err = NULL; 3365ee3dc7aSYi Liu } else { 3375ee3dc7aSYi Liu ret = iommufd_cdev_ram_block_discard_disable(true); 3385ee3dc7aSYi Liu if (ret) { 3395ee3dc7aSYi Liu error_setg(errp, 3405ee3dc7aSYi Liu "Cannot set discarding of RAM broken (%d)", ret); 3415ee3dc7aSYi Liu goto err_discard_disable; 3425ee3dc7aSYi Liu } 3435ee3dc7aSYi Liu goto found_container; 3445ee3dc7aSYi Liu } 3455ee3dc7aSYi Liu } 3465ee3dc7aSYi Liu 3475ee3dc7aSYi Liu /* Need to allocate a new dedicated container */ 3489067d50dSZhenzhong Duan if (!iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp)) { 3495ee3dc7aSYi Liu goto err_alloc_ioas; 3505ee3dc7aSYi Liu } 3515ee3dc7aSYi Liu 3525ee3dc7aSYi Liu trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id); 3535ee3dc7aSYi Liu 35493802605SCédric Le Goater container = VFIO_IOMMU_IOMMUFD(object_new(TYPE_VFIO_IOMMU_IOMMUFD)); 3555ee3dc7aSYi Liu container->be = vbasedev->iommufd; 3565ee3dc7aSYi Liu container->ioas_id = ioas_id; 3575ee3dc7aSYi Liu 3585ee3dc7aSYi Liu bcontainer = &container->bcontainer; 359b7b79588SCédric Le Goater vfio_address_space_insert(space, bcontainer); 3605ee3dc7aSYi Liu 36145d0d8c4SZhenzhong Duan if (!iommufd_cdev_attach_container(vbasedev, container, errp)) { 3625ee3dc7aSYi Liu goto err_attach_container; 3635ee3dc7aSYi Liu } 3645ee3dc7aSYi Liu 3655ee3dc7aSYi Liu ret = iommufd_cdev_ram_block_discard_disable(true); 3665ee3dc7aSYi Liu if (ret) { 3675ee3dc7aSYi Liu goto err_discard_disable; 3685ee3dc7aSYi Liu } 3695ee3dc7aSYi Liu 37045d0d8c4SZhenzhong Duan if (!iommufd_cdev_get_info_iova_range(container, ioas_id, &err)) { 371714e9affSZhenzhong Duan error_append_hint(&err, 372714e9affSZhenzhong Duan "Fallback to default 64bit IOVA range and 4K page size\n"); 373714e9affSZhenzhong Duan warn_report_err(err); 374714e9affSZhenzhong Duan err = NULL; 3755ee3dc7aSYi Liu bcontainer->pgsizes = qemu_real_host_page_size(); 376714e9affSZhenzhong Duan } 3775ee3dc7aSYi Liu 3785ee3dc7aSYi Liu bcontainer->listener = vfio_memory_listener; 3795ee3dc7aSYi Liu memory_listener_register(&bcontainer->listener, bcontainer->space->as); 3805ee3dc7aSYi Liu 3815ee3dc7aSYi Liu if (bcontainer->error) { 3825ee3dc7aSYi Liu error_propagate_prepend(errp, bcontainer->error, 3835ee3dc7aSYi Liu "memory listener initialization failed: "); 3845ee3dc7aSYi Liu goto err_listener_register; 3855ee3dc7aSYi Liu } 3865ee3dc7aSYi Liu 3875ee3dc7aSYi Liu bcontainer->initialized = true; 3885ee3dc7aSYi Liu 3895ee3dc7aSYi Liu found_container: 3905ee3dc7aSYi Liu ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info); 3915ee3dc7aSYi Liu if (ret) { 3925ee3dc7aSYi Liu error_setg_errno(errp, errno, "error getting device info"); 3935ee3dc7aSYi Liu goto err_listener_register; 3945ee3dc7aSYi Liu } 3955ee3dc7aSYi Liu 396f38f5dd1SZhenzhong Duan if (!vfio_cpr_register_container(bcontainer, errp)) { 397d9fa4223SSteve Sistare goto err_listener_register; 398d9fa4223SSteve Sistare } 399d9fa4223SSteve Sistare 4005ee3dc7aSYi Liu /* 4015ee3dc7aSYi Liu * TODO: examine RAM_BLOCK_DISCARD stuff, should we do group level 4025ee3dc7aSYi Liu * for discarding incompatibility check as well? 4035ee3dc7aSYi Liu */ 4045ee3dc7aSYi Liu if (vbasedev->ram_block_discard_allowed) { 4055ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false); 4065ee3dc7aSYi Liu } 4075ee3dc7aSYi Liu 4085ee3dc7aSYi Liu vbasedev->group = 0; 4095ee3dc7aSYi Liu vbasedev->num_irqs = dev_info.num_irqs; 4105ee3dc7aSYi Liu vbasedev->num_regions = dev_info.num_regions; 4115ee3dc7aSYi Liu vbasedev->flags = dev_info.flags; 4125ee3dc7aSYi Liu vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); 4135ee3dc7aSYi Liu vbasedev->bcontainer = bcontainer; 4145ee3dc7aSYi Liu QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next); 4155ee3dc7aSYi Liu QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next); 4165ee3dc7aSYi Liu 4175ee3dc7aSYi Liu trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs, 4185ee3dc7aSYi Liu vbasedev->num_regions, vbasedev->flags); 419b7754835SZhenzhong Duan return true; 4205ee3dc7aSYi Liu 4215ee3dc7aSYi Liu err_listener_register: 4225ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false); 4235ee3dc7aSYi Liu err_discard_disable: 4245ee3dc7aSYi Liu iommufd_cdev_detach_container(vbasedev, container); 4255ee3dc7aSYi Liu err_attach_container: 4265ee3dc7aSYi Liu iommufd_cdev_container_destroy(container); 4275ee3dc7aSYi Liu err_alloc_ioas: 4285ee3dc7aSYi Liu vfio_put_address_space(space); 4295ee3dc7aSYi Liu iommufd_cdev_unbind_and_disconnect(vbasedev); 4305ee3dc7aSYi Liu err_connect_bind: 4315ee3dc7aSYi Liu close(vbasedev->fd); 432b7754835SZhenzhong Duan return false; 4335ee3dc7aSYi Liu } 4345ee3dc7aSYi Liu 4355ee3dc7aSYi Liu static void iommufd_cdev_detach(VFIODevice *vbasedev) 4365ee3dc7aSYi Liu { 4375ee3dc7aSYi Liu VFIOContainerBase *bcontainer = vbasedev->bcontainer; 4385ee3dc7aSYi Liu VFIOAddressSpace *space = bcontainer->space; 4395ee3dc7aSYi Liu VFIOIOMMUFDContainer *container = container_of(bcontainer, 4405ee3dc7aSYi Liu VFIOIOMMUFDContainer, 4415ee3dc7aSYi Liu bcontainer); 4425ee3dc7aSYi Liu QLIST_REMOVE(vbasedev, global_next); 4435ee3dc7aSYi Liu QLIST_REMOVE(vbasedev, container_next); 4445ee3dc7aSYi Liu vbasedev->bcontainer = NULL; 4455ee3dc7aSYi Liu 4465ee3dc7aSYi Liu if (!vbasedev->ram_block_discard_allowed) { 4475ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false); 4485ee3dc7aSYi Liu } 4495ee3dc7aSYi Liu 450d9fa4223SSteve Sistare vfio_cpr_unregister_container(bcontainer); 4515ee3dc7aSYi Liu iommufd_cdev_detach_container(vbasedev, container); 4525ee3dc7aSYi Liu iommufd_cdev_container_destroy(container); 4535ee3dc7aSYi Liu vfio_put_address_space(space); 4545ee3dc7aSYi Liu 4555ee3dc7aSYi Liu iommufd_cdev_unbind_and_disconnect(vbasedev); 4565ee3dc7aSYi Liu close(vbasedev->fd); 4575ee3dc7aSYi Liu } 4585ee3dc7aSYi Liu 45996d6f85fSZhenzhong Duan static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid) 46096d6f85fSZhenzhong Duan { 46196d6f85fSZhenzhong Duan VFIODevice *vbasedev_iter; 462ce5f6d49SCédric Le Goater const VFIOIOMMUClass *iommufd_vioc = 463ce5f6d49SCédric Le Goater VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 46496d6f85fSZhenzhong Duan 46596d6f85fSZhenzhong Duan QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) { 46641d698b8SCédric Le Goater if (VFIO_IOMMU_GET_CLASS(vbasedev_iter->bcontainer) != iommufd_vioc) { 46796d6f85fSZhenzhong Duan continue; 46896d6f85fSZhenzhong Duan } 46996d6f85fSZhenzhong Duan if (devid == vbasedev_iter->devid) { 47096d6f85fSZhenzhong Duan return vbasedev_iter; 47196d6f85fSZhenzhong Duan } 47296d6f85fSZhenzhong Duan } 47396d6f85fSZhenzhong Duan return NULL; 47496d6f85fSZhenzhong Duan } 47596d6f85fSZhenzhong Duan 47696d6f85fSZhenzhong Duan static VFIOPCIDevice * 47796d6f85fSZhenzhong Duan iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev, 47896d6f85fSZhenzhong Duan VFIODevice *reset_dev) 47996d6f85fSZhenzhong Duan { 48096d6f85fSZhenzhong Duan VFIODevice *vbasedev_tmp; 48196d6f85fSZhenzhong Duan 48296d6f85fSZhenzhong Duan if (dep_dev->devid == reset_dev->devid || 48396d6f85fSZhenzhong Duan dep_dev->devid == VFIO_PCI_DEVID_OWNED) { 48496d6f85fSZhenzhong Duan return NULL; 48596d6f85fSZhenzhong Duan } 48696d6f85fSZhenzhong Duan 48796d6f85fSZhenzhong Duan vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid); 48896d6f85fSZhenzhong Duan if (!vbasedev_tmp || !vbasedev_tmp->dev->realized || 48996d6f85fSZhenzhong Duan vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) { 49096d6f85fSZhenzhong Duan return NULL; 49196d6f85fSZhenzhong Duan } 49296d6f85fSZhenzhong Duan 49396d6f85fSZhenzhong Duan return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev); 49496d6f85fSZhenzhong Duan } 49596d6f85fSZhenzhong Duan 49696d6f85fSZhenzhong Duan static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single) 49796d6f85fSZhenzhong Duan { 49896d6f85fSZhenzhong Duan VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev); 49996d6f85fSZhenzhong Duan struct vfio_pci_hot_reset_info *info = NULL; 50096d6f85fSZhenzhong Duan struct vfio_pci_dependent_device *devices; 50196d6f85fSZhenzhong Duan struct vfio_pci_hot_reset *reset; 50296d6f85fSZhenzhong Duan int ret, i; 50396d6f85fSZhenzhong Duan bool multi = false; 50496d6f85fSZhenzhong Duan 50596d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi"); 50696d6f85fSZhenzhong Duan 50796d6f85fSZhenzhong Duan if (!single) { 50896d6f85fSZhenzhong Duan vfio_pci_pre_reset(vdev); 50996d6f85fSZhenzhong Duan } 51096d6f85fSZhenzhong Duan vdev->vbasedev.needs_reset = false; 51196d6f85fSZhenzhong Duan 51296d6f85fSZhenzhong Duan ret = vfio_pci_get_pci_hot_reset_info(vdev, &info); 51396d6f85fSZhenzhong Duan 51496d6f85fSZhenzhong Duan if (ret) { 51596d6f85fSZhenzhong Duan goto out_single; 51696d6f85fSZhenzhong Duan } 51796d6f85fSZhenzhong Duan 51896d6f85fSZhenzhong Duan assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID); 51996d6f85fSZhenzhong Duan 52096d6f85fSZhenzhong Duan devices = &info->devices[0]; 52196d6f85fSZhenzhong Duan 52296d6f85fSZhenzhong Duan if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) { 52396d6f85fSZhenzhong Duan if (!vdev->has_pm_reset) { 52496d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) { 52596d6f85fSZhenzhong Duan if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) { 52696d6f85fSZhenzhong Duan error_report("vfio: Cannot reset device %s, " 52796d6f85fSZhenzhong Duan "depends on device %04x:%02x:%02x.%x " 52896d6f85fSZhenzhong Duan "which is not owned.", 52996d6f85fSZhenzhong Duan vdev->vbasedev.name, devices[i].segment, 53096d6f85fSZhenzhong Duan devices[i].bus, PCI_SLOT(devices[i].devfn), 53196d6f85fSZhenzhong Duan PCI_FUNC(devices[i].devfn)); 53296d6f85fSZhenzhong Duan } 53396d6f85fSZhenzhong Duan } 53496d6f85fSZhenzhong Duan } 53596d6f85fSZhenzhong Duan ret = -EPERM; 53696d6f85fSZhenzhong Duan goto out_single; 53796d6f85fSZhenzhong Duan } 53896d6f85fSZhenzhong Duan 53996d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name); 54096d6f85fSZhenzhong Duan 54196d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) { 54296d6f85fSZhenzhong Duan VFIOPCIDevice *tmp; 54396d6f85fSZhenzhong Duan 54496d6f85fSZhenzhong Duan trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment, 54596d6f85fSZhenzhong Duan devices[i].bus, 54696d6f85fSZhenzhong Duan PCI_SLOT(devices[i].devfn), 54796d6f85fSZhenzhong Duan PCI_FUNC(devices[i].devfn), 54896d6f85fSZhenzhong Duan devices[i].devid); 54996d6f85fSZhenzhong Duan 55096d6f85fSZhenzhong Duan /* 55196d6f85fSZhenzhong Duan * If a VFIO cdev device is resettable, all the dependent devices 55296d6f85fSZhenzhong Duan * are either bound to same iommufd or within same iommu_groups as 55396d6f85fSZhenzhong Duan * one of the iommufd bound devices. 55496d6f85fSZhenzhong Duan */ 55596d6f85fSZhenzhong Duan assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED); 55696d6f85fSZhenzhong Duan 55796d6f85fSZhenzhong Duan tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev); 55896d6f85fSZhenzhong Duan if (!tmp) { 55996d6f85fSZhenzhong Duan continue; 56096d6f85fSZhenzhong Duan } 56196d6f85fSZhenzhong Duan 56296d6f85fSZhenzhong Duan if (single) { 56396d6f85fSZhenzhong Duan ret = -EINVAL; 56496d6f85fSZhenzhong Duan goto out_single; 56596d6f85fSZhenzhong Duan } 56696d6f85fSZhenzhong Duan vfio_pci_pre_reset(tmp); 56796d6f85fSZhenzhong Duan tmp->vbasedev.needs_reset = false; 56896d6f85fSZhenzhong Duan multi = true; 56996d6f85fSZhenzhong Duan } 57096d6f85fSZhenzhong Duan 57196d6f85fSZhenzhong Duan if (!single && !multi) { 57296d6f85fSZhenzhong Duan ret = -EINVAL; 57396d6f85fSZhenzhong Duan goto out_single; 57496d6f85fSZhenzhong Duan } 57596d6f85fSZhenzhong Duan 57696d6f85fSZhenzhong Duan /* Use zero length array for hot reset with iommufd backend */ 57796d6f85fSZhenzhong Duan reset = g_malloc0(sizeof(*reset)); 57896d6f85fSZhenzhong Duan reset->argsz = sizeof(*reset); 57996d6f85fSZhenzhong Duan 58096d6f85fSZhenzhong Duan /* Bus reset! */ 58196d6f85fSZhenzhong Duan ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset); 58296d6f85fSZhenzhong Duan g_free(reset); 58396d6f85fSZhenzhong Duan if (ret) { 58496d6f85fSZhenzhong Duan ret = -errno; 58596d6f85fSZhenzhong Duan } 58696d6f85fSZhenzhong Duan 58796d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset_result(vdev->vbasedev.name, 58896d6f85fSZhenzhong Duan ret ? strerror(errno) : "Success"); 58996d6f85fSZhenzhong Duan 59096d6f85fSZhenzhong Duan /* Re-enable INTx on affected devices */ 59196d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) { 59296d6f85fSZhenzhong Duan VFIOPCIDevice *tmp; 59396d6f85fSZhenzhong Duan 59496d6f85fSZhenzhong Duan tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev); 59596d6f85fSZhenzhong Duan if (!tmp) { 59696d6f85fSZhenzhong Duan continue; 59796d6f85fSZhenzhong Duan } 59896d6f85fSZhenzhong Duan vfio_pci_post_reset(tmp); 59996d6f85fSZhenzhong Duan } 60096d6f85fSZhenzhong Duan out_single: 60196d6f85fSZhenzhong Duan if (!single) { 60296d6f85fSZhenzhong Duan vfio_pci_post_reset(vdev); 60396d6f85fSZhenzhong Duan } 60496d6f85fSZhenzhong Duan g_free(info); 60596d6f85fSZhenzhong Duan 60696d6f85fSZhenzhong Duan return ret; 60796d6f85fSZhenzhong Duan } 60896d6f85fSZhenzhong Duan 609ce5f6d49SCédric Le Goater static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data) 610ce5f6d49SCédric Le Goater { 611ce5f6d49SCédric Le Goater VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass); 612ce5f6d49SCédric Le Goater 613a7fd91b8SZhenzhong Duan vioc->hiod_typename = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO; 614a7fd91b8SZhenzhong Duan 615ce5f6d49SCédric Le Goater vioc->dma_map = iommufd_cdev_map; 616ce5f6d49SCédric Le Goater vioc->dma_unmap = iommufd_cdev_unmap; 617ce5f6d49SCédric Le Goater vioc->attach_device = iommufd_cdev_attach; 618ce5f6d49SCédric Le Goater vioc->detach_device = iommufd_cdev_detach; 619ce5f6d49SCédric Le Goater vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset; 6205ee3dc7aSYi Liu }; 621ce5f6d49SCédric Le Goater 62293058952SZhenzhong Duan static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque, 62393058952SZhenzhong Duan Error **errp) 62493058952SZhenzhong Duan { 62593058952SZhenzhong Duan VFIODevice *vdev = opaque; 62693058952SZhenzhong Duan HostIOMMUDeviceCaps *caps = &hiod->caps; 62793058952SZhenzhong Duan enum iommu_hw_info_type type; 62893058952SZhenzhong Duan union { 62993058952SZhenzhong Duan struct iommu_hw_info_vtd vtd; 63093058952SZhenzhong Duan } data; 631*2d1bf258SJoao Martins uint64_t hw_caps; 63293058952SZhenzhong Duan 633dc169694SEric Auger hiod->agent = opaque; 634dc169694SEric Auger 63593058952SZhenzhong Duan if (!iommufd_backend_get_device_info(vdev->iommufd, vdev->devid, 636*2d1bf258SJoao Martins &type, &data, sizeof(data), 637*2d1bf258SJoao Martins &hw_caps, 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 * 649d59ca1caSEric Auger hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod) 6503ad35d91SEric Auger { 6513ad35d91SEric Auger VFIODevice *vdev = hiod->agent; 6523ad35d91SEric Auger 6533ad35d91SEric Auger g_assert(vdev); 6543966bca5SEric Auger return vfio_container_get_iova_ranges(vdev->bcontainer); 6553ad35d91SEric Auger } 6563ad35d91SEric Auger 6578fe0ebe1SEric Auger static uint64_t 6588fe0ebe1SEric Auger hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod) 6598fe0ebe1SEric Auger { 6608fe0ebe1SEric Auger VFIODevice *vdev = hiod->agent; 6618fe0ebe1SEric Auger 6628fe0ebe1SEric Auger g_assert(vdev); 6638fe0ebe1SEric Auger return vfio_container_get_page_size_mask(vdev->bcontainer); 6648fe0ebe1SEric Auger } 6658fe0ebe1SEric Auger 6668fe0ebe1SEric Auger 66793058952SZhenzhong Duan static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data) 66893058952SZhenzhong Duan { 66993058952SZhenzhong Duan HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc); 67093058952SZhenzhong Duan 67193058952SZhenzhong Duan hiodc->realize = hiod_iommufd_vfio_realize; 6723ad35d91SEric Auger hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges; 6738fe0ebe1SEric Auger hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask; 67493058952SZhenzhong Duan }; 67593058952SZhenzhong Duan 676ce5f6d49SCédric Le Goater static const TypeInfo types[] = { 677ce5f6d49SCédric Le Goater { 678ce5f6d49SCédric Le Goater .name = TYPE_VFIO_IOMMU_IOMMUFD, 679ce5f6d49SCédric Le Goater .parent = TYPE_VFIO_IOMMU, 680504d297eSCédric Le Goater .instance_size = sizeof(VFIOIOMMUFDContainer), 681ce5f6d49SCédric Le Goater .class_init = vfio_iommu_iommufd_class_init, 6829005f928SZhenzhong Duan }, { 6839005f928SZhenzhong Duan .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO, 6849005f928SZhenzhong Duan .parent = TYPE_HOST_IOMMU_DEVICE_IOMMUFD, 68593058952SZhenzhong Duan .class_init = hiod_iommufd_vfio_class_init, 6869005f928SZhenzhong Duan } 687ce5f6d49SCédric Le Goater }; 688ce5f6d49SCédric Le Goater 689ce5f6d49SCédric Le Goater DEFINE_TYPES(types) 690