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 175b07dcb7dSJoao Martins static int 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); 190b07dcb7dSJoao Martins return -errno; 1915ee3dc7aSYi Liu } 1925ee3dc7aSYi Liu 19345d0d8c4SZhenzhong Duan trace_iommufd_cdev_attach_ioas_hwpt(iommufd, vbasedev->name, 19445d0d8c4SZhenzhong Duan vbasedev->fd, id); 195b07dcb7dSJoao Martins return 0; 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 2155b1e96e6SJoao Martins static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev, 2165b1e96e6SJoao Martins VFIOIOMMUFDContainer *container, 2175b1e96e6SJoao Martins Error **errp) 2185b1e96e6SJoao Martins { 2195b1e96e6SJoao Martins ERRP_GUARD(); 2205b1e96e6SJoao Martins IOMMUFDBackend *iommufd = vbasedev->iommufd; 2215b1e96e6SJoao Martins uint32_t flags = 0; 2225b1e96e6SJoao Martins VFIOIOASHwpt *hwpt; 2235b1e96e6SJoao Martins uint32_t hwpt_id; 2245b1e96e6SJoao Martins int ret; 2255b1e96e6SJoao Martins 2265b1e96e6SJoao Martins /* Try to find a domain */ 2275b1e96e6SJoao Martins QLIST_FOREACH(hwpt, &container->hwpt_list, next) { 2285b1e96e6SJoao Martins ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp); 2295b1e96e6SJoao Martins if (ret) { 2305b1e96e6SJoao Martins /* -EINVAL means the domain is incompatible with the device. */ 2315b1e96e6SJoao Martins if (ret == -EINVAL) { 2325b1e96e6SJoao Martins /* 2335b1e96e6SJoao Martins * It is an expected failure and it just means we will try 2345b1e96e6SJoao Martins * another domain, or create one if no existing compatible 2355b1e96e6SJoao Martins * domain is found. Hence why the error is discarded below. 2365b1e96e6SJoao Martins */ 2375b1e96e6SJoao Martins error_free(*errp); 2385b1e96e6SJoao Martins *errp = NULL; 2395b1e96e6SJoao Martins continue; 2405b1e96e6SJoao Martins } 2415b1e96e6SJoao Martins 2425b1e96e6SJoao Martins return false; 2435b1e96e6SJoao Martins } else { 2445b1e96e6SJoao Martins vbasedev->hwpt = hwpt; 2455b1e96e6SJoao Martins QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next); 2465b1e96e6SJoao Martins return true; 2475b1e96e6SJoao Martins } 2485b1e96e6SJoao Martins } 2495b1e96e6SJoao Martins 2505b1e96e6SJoao Martins if (!iommufd_backend_alloc_hwpt(iommufd, vbasedev->devid, 2515b1e96e6SJoao Martins container->ioas_id, flags, 2525b1e96e6SJoao Martins IOMMU_HWPT_DATA_NONE, 0, NULL, 2535b1e96e6SJoao Martins &hwpt_id, errp)) { 2545b1e96e6SJoao Martins return false; 2555b1e96e6SJoao Martins } 2565b1e96e6SJoao Martins 2575b1e96e6SJoao Martins hwpt = g_malloc0(sizeof(*hwpt)); 2585b1e96e6SJoao Martins hwpt->hwpt_id = hwpt_id; 2595b1e96e6SJoao Martins QLIST_INIT(&hwpt->device_list); 2605b1e96e6SJoao Martins 2615b1e96e6SJoao Martins ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp); 2625b1e96e6SJoao Martins if (ret) { 2635b1e96e6SJoao Martins iommufd_backend_free_id(container->be, hwpt->hwpt_id); 2645b1e96e6SJoao Martins g_free(hwpt); 2655b1e96e6SJoao Martins return false; 2665b1e96e6SJoao Martins } 2675b1e96e6SJoao Martins 2685b1e96e6SJoao Martins vbasedev->hwpt = hwpt; 2695b1e96e6SJoao Martins QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next); 2705b1e96e6SJoao Martins QLIST_INSERT_HEAD(&container->hwpt_list, hwpt, next); 2715b1e96e6SJoao Martins return true; 2725b1e96e6SJoao Martins } 2735b1e96e6SJoao Martins 2745b1e96e6SJoao Martins static void iommufd_cdev_autodomains_put(VFIODevice *vbasedev, 2755b1e96e6SJoao Martins VFIOIOMMUFDContainer *container) 2765b1e96e6SJoao Martins { 2775b1e96e6SJoao Martins VFIOIOASHwpt *hwpt = vbasedev->hwpt; 2785b1e96e6SJoao Martins 2795b1e96e6SJoao Martins QLIST_REMOVE(vbasedev, hwpt_next); 2805b1e96e6SJoao Martins vbasedev->hwpt = NULL; 2815b1e96e6SJoao Martins 2825b1e96e6SJoao Martins if (QLIST_EMPTY(&hwpt->device_list)) { 2835b1e96e6SJoao Martins QLIST_REMOVE(hwpt, next); 2845b1e96e6SJoao Martins iommufd_backend_free_id(container->be, hwpt->hwpt_id); 2855b1e96e6SJoao Martins g_free(hwpt); 2865b1e96e6SJoao Martins } 2875b1e96e6SJoao Martins } 2885b1e96e6SJoao Martins 28945d0d8c4SZhenzhong Duan static bool iommufd_cdev_attach_container(VFIODevice *vbasedev, 2905ee3dc7aSYi Liu VFIOIOMMUFDContainer *container, 2915ee3dc7aSYi Liu Error **errp) 2925ee3dc7aSYi Liu { 2935b1e96e6SJoao Martins /* mdevs aren't physical devices and will fail with auto domains */ 2945b1e96e6SJoao Martins if (!vbasedev->mdev) { 2955b1e96e6SJoao Martins return iommufd_cdev_autodomains_get(vbasedev, container, errp); 2965b1e96e6SJoao Martins } 2975b1e96e6SJoao Martins 298b07dcb7dSJoao Martins return !iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp); 2995ee3dc7aSYi Liu } 3005ee3dc7aSYi Liu 3015ee3dc7aSYi Liu static void iommufd_cdev_detach_container(VFIODevice *vbasedev, 3025ee3dc7aSYi Liu VFIOIOMMUFDContainer *container) 3035ee3dc7aSYi Liu { 3045ee3dc7aSYi Liu Error *err = NULL; 3055ee3dc7aSYi Liu 30645d0d8c4SZhenzhong Duan if (!iommufd_cdev_detach_ioas_hwpt(vbasedev, &err)) { 3075ee3dc7aSYi Liu error_report_err(err); 3085ee3dc7aSYi Liu } 3095b1e96e6SJoao Martins 3105b1e96e6SJoao Martins if (vbasedev->hwpt) { 3115b1e96e6SJoao Martins iommufd_cdev_autodomains_put(vbasedev, container); 3125b1e96e6SJoao Martins } 3135b1e96e6SJoao Martins 3145ee3dc7aSYi Liu } 3155ee3dc7aSYi Liu 3165ee3dc7aSYi Liu static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container) 3175ee3dc7aSYi Liu { 3185ee3dc7aSYi Liu VFIOContainerBase *bcontainer = &container->bcontainer; 3195ee3dc7aSYi Liu 3205ee3dc7aSYi Liu if (!QLIST_EMPTY(&bcontainer->device_list)) { 3215ee3dc7aSYi Liu return; 3225ee3dc7aSYi Liu } 3235ee3dc7aSYi Liu memory_listener_unregister(&bcontainer->listener); 3245ee3dc7aSYi Liu iommufd_backend_free_id(container->be, container->ioas_id); 32593802605SCédric Le Goater object_unref(container); 3265ee3dc7aSYi Liu } 3275ee3dc7aSYi Liu 3285ee3dc7aSYi Liu static int iommufd_cdev_ram_block_discard_disable(bool state) 3295ee3dc7aSYi Liu { 3305ee3dc7aSYi Liu /* 3315ee3dc7aSYi Liu * We support coordinated discarding of RAM via the RamDiscardManager. 3325ee3dc7aSYi Liu */ 3335ee3dc7aSYi Liu return ram_block_uncoordinated_discard_disable(state); 3345ee3dc7aSYi Liu } 3355ee3dc7aSYi Liu 33645d0d8c4SZhenzhong Duan static bool iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container, 337714e9affSZhenzhong Duan uint32_t ioas_id, Error **errp) 338714e9affSZhenzhong Duan { 339714e9affSZhenzhong Duan VFIOContainerBase *bcontainer = &container->bcontainer; 340f3758413SZhenzhong Duan g_autofree struct iommu_ioas_iova_ranges *info = NULL; 341714e9affSZhenzhong Duan struct iommu_iova_range *iova_ranges; 34245d0d8c4SZhenzhong Duan int sz, fd = container->be->fd; 343714e9affSZhenzhong Duan 344714e9affSZhenzhong Duan info = g_malloc0(sizeof(*info)); 345714e9affSZhenzhong Duan info->size = sizeof(*info); 346714e9affSZhenzhong Duan info->ioas_id = ioas_id; 347714e9affSZhenzhong Duan 34845d0d8c4SZhenzhong Duan if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info) && errno != EMSGSIZE) { 349714e9affSZhenzhong Duan goto error; 350714e9affSZhenzhong Duan } 351714e9affSZhenzhong Duan 352714e9affSZhenzhong Duan sz = info->num_iovas * sizeof(struct iommu_iova_range); 353714e9affSZhenzhong Duan info = g_realloc(info, sizeof(*info) + sz); 354714e9affSZhenzhong Duan info->allowed_iovas = (uintptr_t)(info + 1); 355714e9affSZhenzhong Duan 35645d0d8c4SZhenzhong Duan if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info)) { 357714e9affSZhenzhong Duan goto error; 358714e9affSZhenzhong Duan } 359714e9affSZhenzhong Duan 360714e9affSZhenzhong Duan iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas; 361714e9affSZhenzhong Duan 362714e9affSZhenzhong Duan for (int i = 0; i < info->num_iovas; i++) { 363714e9affSZhenzhong Duan Range *range = g_new(Range, 1); 364714e9affSZhenzhong Duan 365714e9affSZhenzhong Duan range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last); 366714e9affSZhenzhong Duan bcontainer->iova_ranges = 367714e9affSZhenzhong Duan range_list_insert(bcontainer->iova_ranges, range); 368714e9affSZhenzhong Duan } 369714e9affSZhenzhong Duan bcontainer->pgsizes = info->out_iova_alignment; 370714e9affSZhenzhong Duan 37145d0d8c4SZhenzhong Duan return true; 372714e9affSZhenzhong Duan 373714e9affSZhenzhong Duan error: 374714e9affSZhenzhong Duan error_setg_errno(errp, errno, "Cannot get IOVA ranges"); 37545d0d8c4SZhenzhong Duan return false; 376714e9affSZhenzhong Duan } 377714e9affSZhenzhong Duan 378b7754835SZhenzhong Duan static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev, 3795ee3dc7aSYi Liu AddressSpace *as, Error **errp) 3805ee3dc7aSYi Liu { 3815ee3dc7aSYi Liu VFIOContainerBase *bcontainer; 3825ee3dc7aSYi Liu VFIOIOMMUFDContainer *container; 3835ee3dc7aSYi Liu VFIOAddressSpace *space; 3845ee3dc7aSYi Liu struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; 3855ee3dc7aSYi Liu int ret, devfd; 3865ee3dc7aSYi Liu uint32_t ioas_id; 3875ee3dc7aSYi Liu Error *err = NULL; 388ce5f6d49SCédric Le Goater const VFIOIOMMUClass *iommufd_vioc = 389ce5f6d49SCédric Le Goater VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 3905ee3dc7aSYi Liu 391da3e04b2SZhenzhong Duan if (vbasedev->fd < 0) { 3925ee3dc7aSYi Liu devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp); 3935ee3dc7aSYi Liu if (devfd < 0) { 394b7754835SZhenzhong Duan return false; 3955ee3dc7aSYi Liu } 3965ee3dc7aSYi Liu vbasedev->fd = devfd; 397da3e04b2SZhenzhong Duan } else { 398da3e04b2SZhenzhong Duan devfd = vbasedev->fd; 399da3e04b2SZhenzhong Duan } 4005ee3dc7aSYi Liu 40145d0d8c4SZhenzhong Duan if (!iommufd_cdev_connect_and_bind(vbasedev, errp)) { 4025ee3dc7aSYi Liu goto err_connect_bind; 4035ee3dc7aSYi Liu } 4045ee3dc7aSYi Liu 4055ee3dc7aSYi Liu space = vfio_get_address_space(as); 4065ee3dc7aSYi Liu 4075ee3dc7aSYi Liu /* try to attach to an existing container in this space */ 4085ee3dc7aSYi Liu QLIST_FOREACH(bcontainer, &space->containers, next) { 4095ee3dc7aSYi Liu container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); 41041d698b8SCédric Le Goater if (VFIO_IOMMU_GET_CLASS(bcontainer) != iommufd_vioc || 4115ee3dc7aSYi Liu vbasedev->iommufd != container->be) { 4125ee3dc7aSYi Liu continue; 4135ee3dc7aSYi Liu } 41445d0d8c4SZhenzhong Duan if (!iommufd_cdev_attach_container(vbasedev, container, &err)) { 4155ee3dc7aSYi Liu const char *msg = error_get_pretty(err); 4165ee3dc7aSYi Liu 4175ee3dc7aSYi Liu trace_iommufd_cdev_fail_attach_existing_container(msg); 4185ee3dc7aSYi Liu error_free(err); 4195ee3dc7aSYi Liu err = NULL; 4205ee3dc7aSYi Liu } else { 4215ee3dc7aSYi Liu ret = iommufd_cdev_ram_block_discard_disable(true); 4225ee3dc7aSYi Liu if (ret) { 4235ee3dc7aSYi Liu error_setg(errp, 4245ee3dc7aSYi Liu "Cannot set discarding of RAM broken (%d)", ret); 4255ee3dc7aSYi Liu goto err_discard_disable; 4265ee3dc7aSYi Liu } 4275ee3dc7aSYi Liu goto found_container; 4285ee3dc7aSYi Liu } 4295ee3dc7aSYi Liu } 4305ee3dc7aSYi Liu 4315ee3dc7aSYi Liu /* Need to allocate a new dedicated container */ 4329067d50dSZhenzhong Duan if (!iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp)) { 4335ee3dc7aSYi Liu goto err_alloc_ioas; 4345ee3dc7aSYi Liu } 4355ee3dc7aSYi Liu 4365ee3dc7aSYi Liu trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id); 4375ee3dc7aSYi Liu 43893802605SCédric Le Goater container = VFIO_IOMMU_IOMMUFD(object_new(TYPE_VFIO_IOMMU_IOMMUFD)); 4395ee3dc7aSYi Liu container->be = vbasedev->iommufd; 4405ee3dc7aSYi Liu container->ioas_id = ioas_id; 4415b1e96e6SJoao Martins QLIST_INIT(&container->hwpt_list); 4425ee3dc7aSYi Liu 4435ee3dc7aSYi Liu bcontainer = &container->bcontainer; 444b7b79588SCédric Le Goater vfio_address_space_insert(space, bcontainer); 4455ee3dc7aSYi Liu 44645d0d8c4SZhenzhong Duan if (!iommufd_cdev_attach_container(vbasedev, container, errp)) { 4475ee3dc7aSYi Liu goto err_attach_container; 4485ee3dc7aSYi Liu } 4495ee3dc7aSYi Liu 4505ee3dc7aSYi Liu ret = iommufd_cdev_ram_block_discard_disable(true); 4515ee3dc7aSYi Liu if (ret) { 4525ee3dc7aSYi Liu goto err_discard_disable; 4535ee3dc7aSYi Liu } 4545ee3dc7aSYi Liu 45545d0d8c4SZhenzhong Duan if (!iommufd_cdev_get_info_iova_range(container, ioas_id, &err)) { 456714e9affSZhenzhong Duan error_append_hint(&err, 457714e9affSZhenzhong Duan "Fallback to default 64bit IOVA range and 4K page size\n"); 458714e9affSZhenzhong Duan warn_report_err(err); 459714e9affSZhenzhong Duan err = NULL; 4605ee3dc7aSYi Liu bcontainer->pgsizes = qemu_real_host_page_size(); 461714e9affSZhenzhong Duan } 4625ee3dc7aSYi Liu 4635ee3dc7aSYi Liu bcontainer->listener = vfio_memory_listener; 4645ee3dc7aSYi Liu memory_listener_register(&bcontainer->listener, bcontainer->space->as); 4655ee3dc7aSYi Liu 4665ee3dc7aSYi Liu if (bcontainer->error) { 4675ee3dc7aSYi Liu error_propagate_prepend(errp, bcontainer->error, 4685ee3dc7aSYi Liu "memory listener initialization failed: "); 4695ee3dc7aSYi Liu goto err_listener_register; 4705ee3dc7aSYi Liu } 4715ee3dc7aSYi Liu 4725ee3dc7aSYi Liu bcontainer->initialized = true; 4735ee3dc7aSYi Liu 4745ee3dc7aSYi Liu found_container: 4755ee3dc7aSYi Liu ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info); 4765ee3dc7aSYi Liu if (ret) { 4775ee3dc7aSYi Liu error_setg_errno(errp, errno, "error getting device info"); 4785ee3dc7aSYi Liu goto err_listener_register; 4795ee3dc7aSYi Liu } 4805ee3dc7aSYi Liu 481f38f5dd1SZhenzhong Duan if (!vfio_cpr_register_container(bcontainer, errp)) { 482d9fa4223SSteve Sistare goto err_listener_register; 483d9fa4223SSteve Sistare } 484d9fa4223SSteve Sistare 4855ee3dc7aSYi Liu /* 4865ee3dc7aSYi Liu * TODO: examine RAM_BLOCK_DISCARD stuff, should we do group level 4875ee3dc7aSYi Liu * for discarding incompatibility check as well? 4885ee3dc7aSYi Liu */ 4895ee3dc7aSYi Liu if (vbasedev->ram_block_discard_allowed) { 4905ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false); 4915ee3dc7aSYi Liu } 4925ee3dc7aSYi Liu 4935ee3dc7aSYi Liu vbasedev->group = 0; 4945ee3dc7aSYi Liu vbasedev->num_irqs = dev_info.num_irqs; 4955ee3dc7aSYi Liu vbasedev->num_regions = dev_info.num_regions; 4965ee3dc7aSYi Liu vbasedev->flags = dev_info.flags; 4975ee3dc7aSYi Liu vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); 4985ee3dc7aSYi Liu vbasedev->bcontainer = bcontainer; 4995ee3dc7aSYi Liu QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next); 5005ee3dc7aSYi Liu QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next); 5015ee3dc7aSYi Liu 5025ee3dc7aSYi Liu trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs, 5035ee3dc7aSYi Liu vbasedev->num_regions, vbasedev->flags); 504b7754835SZhenzhong Duan return true; 5055ee3dc7aSYi Liu 5065ee3dc7aSYi Liu err_listener_register: 5075ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false); 5085ee3dc7aSYi Liu err_discard_disable: 5095ee3dc7aSYi Liu iommufd_cdev_detach_container(vbasedev, container); 5105ee3dc7aSYi Liu err_attach_container: 5115ee3dc7aSYi Liu iommufd_cdev_container_destroy(container); 5125ee3dc7aSYi Liu err_alloc_ioas: 5135ee3dc7aSYi Liu vfio_put_address_space(space); 5145ee3dc7aSYi Liu iommufd_cdev_unbind_and_disconnect(vbasedev); 5155ee3dc7aSYi Liu err_connect_bind: 5165ee3dc7aSYi Liu close(vbasedev->fd); 517b7754835SZhenzhong Duan return false; 5185ee3dc7aSYi Liu } 5195ee3dc7aSYi Liu 5205ee3dc7aSYi Liu static void iommufd_cdev_detach(VFIODevice *vbasedev) 5215ee3dc7aSYi Liu { 5225ee3dc7aSYi Liu VFIOContainerBase *bcontainer = vbasedev->bcontainer; 5235ee3dc7aSYi Liu VFIOAddressSpace *space = bcontainer->space; 5245ee3dc7aSYi Liu VFIOIOMMUFDContainer *container = container_of(bcontainer, 5255ee3dc7aSYi Liu VFIOIOMMUFDContainer, 5265ee3dc7aSYi Liu bcontainer); 5275ee3dc7aSYi Liu QLIST_REMOVE(vbasedev, global_next); 5285ee3dc7aSYi Liu QLIST_REMOVE(vbasedev, container_next); 5295ee3dc7aSYi Liu vbasedev->bcontainer = NULL; 5305ee3dc7aSYi Liu 5315ee3dc7aSYi Liu if (!vbasedev->ram_block_discard_allowed) { 5325ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false); 5335ee3dc7aSYi Liu } 5345ee3dc7aSYi Liu 535d9fa4223SSteve Sistare vfio_cpr_unregister_container(bcontainer); 5365ee3dc7aSYi Liu iommufd_cdev_detach_container(vbasedev, container); 5375ee3dc7aSYi Liu iommufd_cdev_container_destroy(container); 5385ee3dc7aSYi Liu vfio_put_address_space(space); 5395ee3dc7aSYi Liu 5405ee3dc7aSYi Liu iommufd_cdev_unbind_and_disconnect(vbasedev); 5415ee3dc7aSYi Liu close(vbasedev->fd); 5425ee3dc7aSYi Liu } 5435ee3dc7aSYi Liu 54496d6f85fSZhenzhong Duan static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid) 54596d6f85fSZhenzhong Duan { 54696d6f85fSZhenzhong Duan VFIODevice *vbasedev_iter; 547ce5f6d49SCédric Le Goater const VFIOIOMMUClass *iommufd_vioc = 548ce5f6d49SCédric Le Goater VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 54996d6f85fSZhenzhong Duan 55096d6f85fSZhenzhong Duan QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) { 55141d698b8SCédric Le Goater if (VFIO_IOMMU_GET_CLASS(vbasedev_iter->bcontainer) != iommufd_vioc) { 55296d6f85fSZhenzhong Duan continue; 55396d6f85fSZhenzhong Duan } 55496d6f85fSZhenzhong Duan if (devid == vbasedev_iter->devid) { 55596d6f85fSZhenzhong Duan return vbasedev_iter; 55696d6f85fSZhenzhong Duan } 55796d6f85fSZhenzhong Duan } 55896d6f85fSZhenzhong Duan return NULL; 55996d6f85fSZhenzhong Duan } 56096d6f85fSZhenzhong Duan 56196d6f85fSZhenzhong Duan static VFIOPCIDevice * 56296d6f85fSZhenzhong Duan iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev, 56396d6f85fSZhenzhong Duan VFIODevice *reset_dev) 56496d6f85fSZhenzhong Duan { 56596d6f85fSZhenzhong Duan VFIODevice *vbasedev_tmp; 56696d6f85fSZhenzhong Duan 56796d6f85fSZhenzhong Duan if (dep_dev->devid == reset_dev->devid || 56896d6f85fSZhenzhong Duan dep_dev->devid == VFIO_PCI_DEVID_OWNED) { 56996d6f85fSZhenzhong Duan return NULL; 57096d6f85fSZhenzhong Duan } 57196d6f85fSZhenzhong Duan 57296d6f85fSZhenzhong Duan vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid); 57396d6f85fSZhenzhong Duan if (!vbasedev_tmp || !vbasedev_tmp->dev->realized || 57496d6f85fSZhenzhong Duan vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) { 57596d6f85fSZhenzhong Duan return NULL; 57696d6f85fSZhenzhong Duan } 57796d6f85fSZhenzhong Duan 57896d6f85fSZhenzhong Duan return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev); 57996d6f85fSZhenzhong Duan } 58096d6f85fSZhenzhong Duan 58196d6f85fSZhenzhong Duan static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single) 58296d6f85fSZhenzhong Duan { 58396d6f85fSZhenzhong Duan VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev); 58496d6f85fSZhenzhong Duan struct vfio_pci_hot_reset_info *info = NULL; 58596d6f85fSZhenzhong Duan struct vfio_pci_dependent_device *devices; 58696d6f85fSZhenzhong Duan struct vfio_pci_hot_reset *reset; 58796d6f85fSZhenzhong Duan int ret, i; 58896d6f85fSZhenzhong Duan bool multi = false; 58996d6f85fSZhenzhong Duan 59096d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi"); 59196d6f85fSZhenzhong Duan 59296d6f85fSZhenzhong Duan if (!single) { 59396d6f85fSZhenzhong Duan vfio_pci_pre_reset(vdev); 59496d6f85fSZhenzhong Duan } 59596d6f85fSZhenzhong Duan vdev->vbasedev.needs_reset = false; 59696d6f85fSZhenzhong Duan 59796d6f85fSZhenzhong Duan ret = vfio_pci_get_pci_hot_reset_info(vdev, &info); 59896d6f85fSZhenzhong Duan 59996d6f85fSZhenzhong Duan if (ret) { 60096d6f85fSZhenzhong Duan goto out_single; 60196d6f85fSZhenzhong Duan } 60296d6f85fSZhenzhong Duan 60396d6f85fSZhenzhong Duan assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID); 60496d6f85fSZhenzhong Duan 60596d6f85fSZhenzhong Duan devices = &info->devices[0]; 60696d6f85fSZhenzhong Duan 60796d6f85fSZhenzhong Duan if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) { 60896d6f85fSZhenzhong Duan if (!vdev->has_pm_reset) { 60996d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) { 61096d6f85fSZhenzhong Duan if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) { 61196d6f85fSZhenzhong Duan error_report("vfio: Cannot reset device %s, " 61296d6f85fSZhenzhong Duan "depends on device %04x:%02x:%02x.%x " 61396d6f85fSZhenzhong Duan "which is not owned.", 61496d6f85fSZhenzhong Duan vdev->vbasedev.name, devices[i].segment, 61596d6f85fSZhenzhong Duan devices[i].bus, PCI_SLOT(devices[i].devfn), 61696d6f85fSZhenzhong Duan PCI_FUNC(devices[i].devfn)); 61796d6f85fSZhenzhong Duan } 61896d6f85fSZhenzhong Duan } 61996d6f85fSZhenzhong Duan } 62096d6f85fSZhenzhong Duan ret = -EPERM; 62196d6f85fSZhenzhong Duan goto out_single; 62296d6f85fSZhenzhong Duan } 62396d6f85fSZhenzhong Duan 62496d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name); 62596d6f85fSZhenzhong Duan 62696d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) { 62796d6f85fSZhenzhong Duan VFIOPCIDevice *tmp; 62896d6f85fSZhenzhong Duan 62996d6f85fSZhenzhong Duan trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment, 63096d6f85fSZhenzhong Duan devices[i].bus, 63196d6f85fSZhenzhong Duan PCI_SLOT(devices[i].devfn), 63296d6f85fSZhenzhong Duan PCI_FUNC(devices[i].devfn), 63396d6f85fSZhenzhong Duan devices[i].devid); 63496d6f85fSZhenzhong Duan 63596d6f85fSZhenzhong Duan /* 63696d6f85fSZhenzhong Duan * If a VFIO cdev device is resettable, all the dependent devices 63796d6f85fSZhenzhong Duan * are either bound to same iommufd or within same iommu_groups as 63896d6f85fSZhenzhong Duan * one of the iommufd bound devices. 63996d6f85fSZhenzhong Duan */ 64096d6f85fSZhenzhong Duan assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED); 64196d6f85fSZhenzhong Duan 64296d6f85fSZhenzhong Duan tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev); 64396d6f85fSZhenzhong Duan if (!tmp) { 64496d6f85fSZhenzhong Duan continue; 64596d6f85fSZhenzhong Duan } 64696d6f85fSZhenzhong Duan 64796d6f85fSZhenzhong Duan if (single) { 64896d6f85fSZhenzhong Duan ret = -EINVAL; 64996d6f85fSZhenzhong Duan goto out_single; 65096d6f85fSZhenzhong Duan } 65196d6f85fSZhenzhong Duan vfio_pci_pre_reset(tmp); 65296d6f85fSZhenzhong Duan tmp->vbasedev.needs_reset = false; 65396d6f85fSZhenzhong Duan multi = true; 65496d6f85fSZhenzhong Duan } 65596d6f85fSZhenzhong Duan 65696d6f85fSZhenzhong Duan if (!single && !multi) { 65796d6f85fSZhenzhong Duan ret = -EINVAL; 65896d6f85fSZhenzhong Duan goto out_single; 65996d6f85fSZhenzhong Duan } 66096d6f85fSZhenzhong Duan 66196d6f85fSZhenzhong Duan /* Use zero length array for hot reset with iommufd backend */ 66296d6f85fSZhenzhong Duan reset = g_malloc0(sizeof(*reset)); 66396d6f85fSZhenzhong Duan reset->argsz = sizeof(*reset); 66496d6f85fSZhenzhong Duan 66596d6f85fSZhenzhong Duan /* Bus reset! */ 66696d6f85fSZhenzhong Duan ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset); 66796d6f85fSZhenzhong Duan g_free(reset); 66896d6f85fSZhenzhong Duan if (ret) { 66996d6f85fSZhenzhong Duan ret = -errno; 67096d6f85fSZhenzhong Duan } 67196d6f85fSZhenzhong Duan 67296d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset_result(vdev->vbasedev.name, 67396d6f85fSZhenzhong Duan ret ? strerror(errno) : "Success"); 67496d6f85fSZhenzhong Duan 67596d6f85fSZhenzhong Duan /* Re-enable INTx on affected devices */ 67696d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) { 67796d6f85fSZhenzhong Duan VFIOPCIDevice *tmp; 67896d6f85fSZhenzhong Duan 67996d6f85fSZhenzhong Duan tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev); 68096d6f85fSZhenzhong Duan if (!tmp) { 68196d6f85fSZhenzhong Duan continue; 68296d6f85fSZhenzhong Duan } 68396d6f85fSZhenzhong Duan vfio_pci_post_reset(tmp); 68496d6f85fSZhenzhong Duan } 68596d6f85fSZhenzhong Duan out_single: 68696d6f85fSZhenzhong Duan if (!single) { 68796d6f85fSZhenzhong Duan vfio_pci_post_reset(vdev); 68896d6f85fSZhenzhong Duan } 68996d6f85fSZhenzhong Duan g_free(info); 69096d6f85fSZhenzhong Duan 69196d6f85fSZhenzhong Duan return ret; 69296d6f85fSZhenzhong Duan } 69396d6f85fSZhenzhong Duan 694ce5f6d49SCédric Le Goater static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data) 695ce5f6d49SCédric Le Goater { 696ce5f6d49SCédric Le Goater VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass); 697ce5f6d49SCédric Le Goater 698a7fd91b8SZhenzhong Duan vioc->hiod_typename = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO; 699a7fd91b8SZhenzhong Duan 700ce5f6d49SCédric Le Goater vioc->dma_map = iommufd_cdev_map; 701ce5f6d49SCédric Le Goater vioc->dma_unmap = iommufd_cdev_unmap; 702ce5f6d49SCédric Le Goater vioc->attach_device = iommufd_cdev_attach; 703ce5f6d49SCédric Le Goater vioc->detach_device = iommufd_cdev_detach; 704ce5f6d49SCédric Le Goater vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset; 7055ee3dc7aSYi Liu }; 706ce5f6d49SCédric Le Goater 70793058952SZhenzhong Duan static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque, 70893058952SZhenzhong Duan Error **errp) 70993058952SZhenzhong Duan { 71093058952SZhenzhong Duan VFIODevice *vdev = opaque; 71193058952SZhenzhong Duan HostIOMMUDeviceCaps *caps = &hiod->caps; 71293058952SZhenzhong Duan enum iommu_hw_info_type type; 71393058952SZhenzhong Duan union { 71493058952SZhenzhong Duan struct iommu_hw_info_vtd vtd; 71593058952SZhenzhong Duan } data; 7162d1bf258SJoao Martins uint64_t hw_caps; 71793058952SZhenzhong Duan 718dc169694SEric Auger hiod->agent = opaque; 719dc169694SEric Auger 72093058952SZhenzhong Duan if (!iommufd_backend_get_device_info(vdev->iommufd, vdev->devid, 7212d1bf258SJoao Martins &type, &data, sizeof(data), 7222d1bf258SJoao Martins &hw_caps, errp)) { 72393058952SZhenzhong Duan return false; 72493058952SZhenzhong Duan } 72593058952SZhenzhong Duan 72693058952SZhenzhong Duan hiod->name = g_strdup(vdev->name); 72793058952SZhenzhong Duan caps->type = type; 728*21e8d3a3SJoao Martins caps->hw_caps = hw_caps; 72993058952SZhenzhong Duan 73093058952SZhenzhong Duan return true; 73193058952SZhenzhong Duan } 73293058952SZhenzhong Duan 7333ad35d91SEric Auger static GList * 734d59ca1caSEric Auger hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod) 7353ad35d91SEric Auger { 7363ad35d91SEric Auger VFIODevice *vdev = hiod->agent; 7373ad35d91SEric Auger 7383ad35d91SEric Auger g_assert(vdev); 7393966bca5SEric Auger return vfio_container_get_iova_ranges(vdev->bcontainer); 7403ad35d91SEric Auger } 7413ad35d91SEric Auger 7428fe0ebe1SEric Auger static uint64_t 7438fe0ebe1SEric Auger hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod) 7448fe0ebe1SEric Auger { 7458fe0ebe1SEric Auger VFIODevice *vdev = hiod->agent; 7468fe0ebe1SEric Auger 7478fe0ebe1SEric Auger g_assert(vdev); 7488fe0ebe1SEric Auger return vfio_container_get_page_size_mask(vdev->bcontainer); 7498fe0ebe1SEric Auger } 7508fe0ebe1SEric Auger 7518fe0ebe1SEric Auger 75293058952SZhenzhong Duan static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data) 75393058952SZhenzhong Duan { 75493058952SZhenzhong Duan HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc); 75593058952SZhenzhong Duan 75693058952SZhenzhong Duan hiodc->realize = hiod_iommufd_vfio_realize; 7573ad35d91SEric Auger hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges; 7588fe0ebe1SEric Auger hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask; 75993058952SZhenzhong Duan }; 76093058952SZhenzhong Duan 761ce5f6d49SCédric Le Goater static const TypeInfo types[] = { 762ce5f6d49SCédric Le Goater { 763ce5f6d49SCédric Le Goater .name = TYPE_VFIO_IOMMU_IOMMUFD, 764ce5f6d49SCédric Le Goater .parent = TYPE_VFIO_IOMMU, 765504d297eSCédric Le Goater .instance_size = sizeof(VFIOIOMMUFDContainer), 766ce5f6d49SCédric Le Goater .class_init = vfio_iommu_iommufd_class_init, 7679005f928SZhenzhong Duan }, { 7689005f928SZhenzhong Duan .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO, 7699005f928SZhenzhong Duan .parent = TYPE_HOST_IOMMU_DEVICE_IOMMUFD, 77093058952SZhenzhong Duan .class_init = hiod_iommufd_vfio_class_init, 7719005f928SZhenzhong Duan } 772ce5f6d49SCédric Le Goater }; 773ce5f6d49SCédric Le Goater 774ce5f6d49SCédric Le Goater DEFINE_TYPES(types) 775