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 525ee3dc7aSYi Liu static int iommufd_cdev_kvm_device_add(VFIODevice *vbasedev, Error **errp) 535ee3dc7aSYi Liu { 545ee3dc7aSYi Liu 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 665ee3dc7aSYi Liu static int 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 int ret; 745ee3dc7aSYi Liu 755ee3dc7aSYi Liu ret = iommufd_backend_connect(iommufd, errp); 765ee3dc7aSYi Liu if (ret) { 775ee3dc7aSYi Liu return ret; 785ee3dc7aSYi Liu } 795ee3dc7aSYi Liu 805ee3dc7aSYi Liu /* 815ee3dc7aSYi Liu * Add device to kvm-vfio to be prepared for the tracking 825ee3dc7aSYi Liu * in KVM. Especially for some emulated devices, it requires 835ee3dc7aSYi Liu * to have kvm information in the device open. 845ee3dc7aSYi Liu */ 855ee3dc7aSYi Liu ret = iommufd_cdev_kvm_device_add(vbasedev, errp); 865ee3dc7aSYi Liu if (ret) { 875ee3dc7aSYi Liu goto err_kvm_device_add; 885ee3dc7aSYi Liu } 895ee3dc7aSYi Liu 905ee3dc7aSYi Liu /* Bind device to iommufd */ 915ee3dc7aSYi Liu bind.iommufd = iommufd->fd; 925ee3dc7aSYi Liu ret = ioctl(vbasedev->fd, VFIO_DEVICE_BIND_IOMMUFD, &bind); 935ee3dc7aSYi Liu if (ret) { 945ee3dc7aSYi Liu error_setg_errno(errp, errno, "error bind device fd=%d to iommufd=%d", 955ee3dc7aSYi Liu vbasedev->fd, bind.iommufd); 965ee3dc7aSYi Liu goto err_bind; 975ee3dc7aSYi Liu } 985ee3dc7aSYi Liu 995ee3dc7aSYi Liu vbasedev->devid = bind.out_devid; 1005ee3dc7aSYi Liu trace_iommufd_cdev_connect_and_bind(bind.iommufd, vbasedev->name, 1015ee3dc7aSYi Liu vbasedev->fd, vbasedev->devid); 1025ee3dc7aSYi Liu return ret; 1035ee3dc7aSYi Liu err_bind: 1045ee3dc7aSYi Liu iommufd_cdev_kvm_device_del(vbasedev); 1055ee3dc7aSYi Liu err_kvm_device_add: 1065ee3dc7aSYi Liu iommufd_backend_disconnect(iommufd); 1075ee3dc7aSYi Liu return ret; 1085ee3dc7aSYi Liu } 1095ee3dc7aSYi Liu 1105ee3dc7aSYi Liu static void iommufd_cdev_unbind_and_disconnect(VFIODevice *vbasedev) 1115ee3dc7aSYi Liu { 1125ee3dc7aSYi Liu /* Unbind is automatically conducted when device fd is closed */ 1135ee3dc7aSYi Liu iommufd_cdev_kvm_device_del(vbasedev); 1145ee3dc7aSYi Liu iommufd_backend_disconnect(vbasedev->iommufd); 1155ee3dc7aSYi Liu } 1165ee3dc7aSYi Liu 1175ee3dc7aSYi Liu static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp) 1185ee3dc7aSYi Liu { 119*5cf8f51cSZhao Liu ERRP_GUARD(); 1205ee3dc7aSYi Liu long int ret = -ENOTTY; 1215ee3dc7aSYi Liu char *path, *vfio_dev_path = NULL, *vfio_path = NULL; 1225ee3dc7aSYi Liu DIR *dir = NULL; 1235ee3dc7aSYi Liu struct dirent *dent; 1245ee3dc7aSYi Liu gchar *contents; 1255ee3dc7aSYi Liu gsize length; 1265ee3dc7aSYi Liu int major, minor; 1275ee3dc7aSYi Liu dev_t vfio_devt; 1285ee3dc7aSYi Liu 1295ee3dc7aSYi Liu path = g_strdup_printf("%s/vfio-dev", sysfs_path); 1305ee3dc7aSYi Liu dir = opendir(path); 1315ee3dc7aSYi Liu if (!dir) { 1325ee3dc7aSYi Liu error_setg_errno(errp, errno, "couldn't open directory %s", path); 1335ee3dc7aSYi Liu goto out_free_path; 1345ee3dc7aSYi Liu } 1355ee3dc7aSYi Liu 1365ee3dc7aSYi Liu while ((dent = readdir(dir))) { 1375ee3dc7aSYi Liu if (!strncmp(dent->d_name, "vfio", 4)) { 1385ee3dc7aSYi Liu vfio_dev_path = g_strdup_printf("%s/%s/dev", path, dent->d_name); 1395ee3dc7aSYi Liu break; 1405ee3dc7aSYi Liu } 1415ee3dc7aSYi Liu } 1425ee3dc7aSYi Liu 1435ee3dc7aSYi Liu if (!vfio_dev_path) { 1445ee3dc7aSYi Liu error_setg(errp, "failed to find vfio-dev/vfioX/dev"); 1455ee3dc7aSYi Liu goto out_close_dir; 1465ee3dc7aSYi Liu } 1475ee3dc7aSYi Liu 1485ee3dc7aSYi Liu if (!g_file_get_contents(vfio_dev_path, &contents, &length, NULL)) { 1495ee3dc7aSYi Liu error_setg(errp, "failed to load \"%s\"", vfio_dev_path); 1505ee3dc7aSYi Liu goto out_free_dev_path; 1515ee3dc7aSYi Liu } 1525ee3dc7aSYi Liu 1535ee3dc7aSYi Liu if (sscanf(contents, "%d:%d", &major, &minor) != 2) { 1545ee3dc7aSYi Liu error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path); 1555ee3dc7aSYi Liu goto out_free_dev_path; 1565ee3dc7aSYi Liu } 1575ee3dc7aSYi Liu g_free(contents); 1585ee3dc7aSYi Liu vfio_devt = makedev(major, minor); 1595ee3dc7aSYi Liu 1605ee3dc7aSYi Liu vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name); 1615ee3dc7aSYi Liu ret = open_cdev(vfio_path, vfio_devt); 1625ee3dc7aSYi Liu if (ret < 0) { 1635ee3dc7aSYi Liu error_setg(errp, "Failed to open %s", vfio_path); 1645ee3dc7aSYi Liu } 1655ee3dc7aSYi Liu 1665ee3dc7aSYi Liu trace_iommufd_cdev_getfd(vfio_path, ret); 1675ee3dc7aSYi Liu g_free(vfio_path); 1685ee3dc7aSYi Liu 1695ee3dc7aSYi Liu out_free_dev_path: 1705ee3dc7aSYi Liu g_free(vfio_dev_path); 1715ee3dc7aSYi Liu out_close_dir: 1725ee3dc7aSYi Liu closedir(dir); 1735ee3dc7aSYi Liu out_free_path: 1745ee3dc7aSYi Liu if (*errp) { 1755ee3dc7aSYi Liu error_prepend(errp, VFIO_MSG_PREFIX, path); 1765ee3dc7aSYi Liu } 1775ee3dc7aSYi Liu g_free(path); 1785ee3dc7aSYi Liu 1795ee3dc7aSYi Liu return ret; 1805ee3dc7aSYi Liu } 1815ee3dc7aSYi Liu 1825ee3dc7aSYi Liu static int iommufd_cdev_attach_ioas_hwpt(VFIODevice *vbasedev, uint32_t id, 1835ee3dc7aSYi Liu Error **errp) 1845ee3dc7aSYi Liu { 1855ee3dc7aSYi Liu int ret, iommufd = vbasedev->iommufd->fd; 1865ee3dc7aSYi Liu struct vfio_device_attach_iommufd_pt attach_data = { 1875ee3dc7aSYi Liu .argsz = sizeof(attach_data), 1885ee3dc7aSYi Liu .flags = 0, 1895ee3dc7aSYi Liu .pt_id = id, 1905ee3dc7aSYi Liu }; 1915ee3dc7aSYi Liu 1925ee3dc7aSYi Liu /* Attach device to an IOAS or hwpt within iommufd */ 1935ee3dc7aSYi Liu ret = ioctl(vbasedev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data); 1945ee3dc7aSYi Liu if (ret) { 1955ee3dc7aSYi Liu error_setg_errno(errp, errno, 1965ee3dc7aSYi Liu "[iommufd=%d] error attach %s (%d) to id=%d", 1975ee3dc7aSYi Liu iommufd, vbasedev->name, vbasedev->fd, id); 1985ee3dc7aSYi Liu } else { 1995ee3dc7aSYi Liu trace_iommufd_cdev_attach_ioas_hwpt(iommufd, vbasedev->name, 2005ee3dc7aSYi Liu vbasedev->fd, id); 2015ee3dc7aSYi Liu } 2025ee3dc7aSYi Liu return ret; 2035ee3dc7aSYi Liu } 2045ee3dc7aSYi Liu 2055ee3dc7aSYi Liu static int iommufd_cdev_detach_ioas_hwpt(VFIODevice *vbasedev, Error **errp) 2065ee3dc7aSYi Liu { 2075ee3dc7aSYi Liu int ret, iommufd = vbasedev->iommufd->fd; 2085ee3dc7aSYi Liu struct vfio_device_detach_iommufd_pt detach_data = { 2095ee3dc7aSYi Liu .argsz = sizeof(detach_data), 2105ee3dc7aSYi Liu .flags = 0, 2115ee3dc7aSYi Liu }; 2125ee3dc7aSYi Liu 2135ee3dc7aSYi Liu ret = ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_data); 2145ee3dc7aSYi Liu if (ret) { 2155ee3dc7aSYi Liu error_setg_errno(errp, errno, "detach %s failed", vbasedev->name); 2165ee3dc7aSYi Liu } else { 2175ee3dc7aSYi Liu trace_iommufd_cdev_detach_ioas_hwpt(iommufd, vbasedev->name); 2185ee3dc7aSYi Liu } 2195ee3dc7aSYi Liu return ret; 2205ee3dc7aSYi Liu } 2215ee3dc7aSYi Liu 2225ee3dc7aSYi Liu static int iommufd_cdev_attach_container(VFIODevice *vbasedev, 2235ee3dc7aSYi Liu VFIOIOMMUFDContainer *container, 2245ee3dc7aSYi Liu Error **errp) 2255ee3dc7aSYi Liu { 2265ee3dc7aSYi Liu return iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp); 2275ee3dc7aSYi Liu } 2285ee3dc7aSYi Liu 2295ee3dc7aSYi Liu static void iommufd_cdev_detach_container(VFIODevice *vbasedev, 2305ee3dc7aSYi Liu VFIOIOMMUFDContainer *container) 2315ee3dc7aSYi Liu { 2325ee3dc7aSYi Liu Error *err = NULL; 2335ee3dc7aSYi Liu 2345ee3dc7aSYi Liu if (iommufd_cdev_detach_ioas_hwpt(vbasedev, &err)) { 2355ee3dc7aSYi Liu error_report_err(err); 2365ee3dc7aSYi Liu } 2375ee3dc7aSYi Liu } 2385ee3dc7aSYi Liu 2395ee3dc7aSYi Liu static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container) 2405ee3dc7aSYi Liu { 2415ee3dc7aSYi Liu VFIOContainerBase *bcontainer = &container->bcontainer; 2425ee3dc7aSYi Liu 2435ee3dc7aSYi Liu if (!QLIST_EMPTY(&bcontainer->device_list)) { 2445ee3dc7aSYi Liu return; 2455ee3dc7aSYi Liu } 2465ee3dc7aSYi Liu memory_listener_unregister(&bcontainer->listener); 2475ee3dc7aSYi Liu vfio_container_destroy(bcontainer); 2485ee3dc7aSYi Liu iommufd_backend_free_id(container->be, container->ioas_id); 2495ee3dc7aSYi Liu g_free(container); 2505ee3dc7aSYi Liu } 2515ee3dc7aSYi Liu 2525ee3dc7aSYi Liu static int iommufd_cdev_ram_block_discard_disable(bool state) 2535ee3dc7aSYi Liu { 2545ee3dc7aSYi Liu /* 2555ee3dc7aSYi Liu * We support coordinated discarding of RAM via the RamDiscardManager. 2565ee3dc7aSYi Liu */ 2575ee3dc7aSYi Liu return ram_block_uncoordinated_discard_disable(state); 2585ee3dc7aSYi Liu } 2595ee3dc7aSYi Liu 260714e9affSZhenzhong Duan static int iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container, 261714e9affSZhenzhong Duan uint32_t ioas_id, Error **errp) 262714e9affSZhenzhong Duan { 263714e9affSZhenzhong Duan VFIOContainerBase *bcontainer = &container->bcontainer; 264714e9affSZhenzhong Duan struct iommu_ioas_iova_ranges *info; 265714e9affSZhenzhong Duan struct iommu_iova_range *iova_ranges; 266714e9affSZhenzhong Duan int ret, sz, fd = container->be->fd; 267714e9affSZhenzhong Duan 268714e9affSZhenzhong Duan info = g_malloc0(sizeof(*info)); 269714e9affSZhenzhong Duan info->size = sizeof(*info); 270714e9affSZhenzhong Duan info->ioas_id = ioas_id; 271714e9affSZhenzhong Duan 272714e9affSZhenzhong Duan ret = ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info); 273714e9affSZhenzhong Duan if (ret && errno != EMSGSIZE) { 274714e9affSZhenzhong Duan goto error; 275714e9affSZhenzhong Duan } 276714e9affSZhenzhong Duan 277714e9affSZhenzhong Duan sz = info->num_iovas * sizeof(struct iommu_iova_range); 278714e9affSZhenzhong Duan info = g_realloc(info, sizeof(*info) + sz); 279714e9affSZhenzhong Duan info->allowed_iovas = (uintptr_t)(info + 1); 280714e9affSZhenzhong Duan 281714e9affSZhenzhong Duan ret = ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info); 282714e9affSZhenzhong Duan if (ret) { 283714e9affSZhenzhong Duan goto error; 284714e9affSZhenzhong Duan } 285714e9affSZhenzhong Duan 286714e9affSZhenzhong Duan iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas; 287714e9affSZhenzhong Duan 288714e9affSZhenzhong Duan for (int i = 0; i < info->num_iovas; i++) { 289714e9affSZhenzhong Duan Range *range = g_new(Range, 1); 290714e9affSZhenzhong Duan 291714e9affSZhenzhong Duan range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last); 292714e9affSZhenzhong Duan bcontainer->iova_ranges = 293714e9affSZhenzhong Duan range_list_insert(bcontainer->iova_ranges, range); 294714e9affSZhenzhong Duan } 295714e9affSZhenzhong Duan bcontainer->pgsizes = info->out_iova_alignment; 296714e9affSZhenzhong Duan 297714e9affSZhenzhong Duan g_free(info); 298714e9affSZhenzhong Duan return 0; 299714e9affSZhenzhong Duan 300714e9affSZhenzhong Duan error: 301714e9affSZhenzhong Duan ret = -errno; 302714e9affSZhenzhong Duan g_free(info); 303714e9affSZhenzhong Duan error_setg_errno(errp, errno, "Cannot get IOVA ranges"); 304714e9affSZhenzhong Duan return ret; 305714e9affSZhenzhong Duan } 306714e9affSZhenzhong Duan 3075ee3dc7aSYi Liu static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev, 3085ee3dc7aSYi Liu AddressSpace *as, Error **errp) 3095ee3dc7aSYi Liu { 3105ee3dc7aSYi Liu VFIOContainerBase *bcontainer; 3115ee3dc7aSYi Liu VFIOIOMMUFDContainer *container; 3125ee3dc7aSYi Liu VFIOAddressSpace *space; 3135ee3dc7aSYi Liu struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; 3145ee3dc7aSYi Liu int ret, devfd; 3155ee3dc7aSYi Liu uint32_t ioas_id; 3165ee3dc7aSYi Liu Error *err = NULL; 317ce5f6d49SCédric Le Goater const VFIOIOMMUClass *iommufd_vioc = 318ce5f6d49SCédric Le Goater VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 3195ee3dc7aSYi Liu 320da3e04b2SZhenzhong Duan if (vbasedev->fd < 0) { 3215ee3dc7aSYi Liu devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp); 3225ee3dc7aSYi Liu if (devfd < 0) { 3235ee3dc7aSYi Liu return devfd; 3245ee3dc7aSYi Liu } 3255ee3dc7aSYi Liu vbasedev->fd = devfd; 326da3e04b2SZhenzhong Duan } else { 327da3e04b2SZhenzhong Duan devfd = vbasedev->fd; 328da3e04b2SZhenzhong Duan } 3295ee3dc7aSYi Liu 3305ee3dc7aSYi Liu ret = iommufd_cdev_connect_and_bind(vbasedev, errp); 3315ee3dc7aSYi Liu if (ret) { 3325ee3dc7aSYi Liu goto err_connect_bind; 3335ee3dc7aSYi Liu } 3345ee3dc7aSYi Liu 3355ee3dc7aSYi Liu space = vfio_get_address_space(as); 3365ee3dc7aSYi Liu 3375ee3dc7aSYi Liu /* try to attach to an existing container in this space */ 3385ee3dc7aSYi Liu QLIST_FOREACH(bcontainer, &space->containers, next) { 3395ee3dc7aSYi Liu container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); 340ce5f6d49SCédric Le Goater if (bcontainer->ops != iommufd_vioc || 3415ee3dc7aSYi Liu vbasedev->iommufd != container->be) { 3425ee3dc7aSYi Liu continue; 3435ee3dc7aSYi Liu } 3445ee3dc7aSYi Liu if (iommufd_cdev_attach_container(vbasedev, container, &err)) { 3455ee3dc7aSYi Liu const char *msg = error_get_pretty(err); 3465ee3dc7aSYi Liu 3475ee3dc7aSYi Liu trace_iommufd_cdev_fail_attach_existing_container(msg); 3485ee3dc7aSYi Liu error_free(err); 3495ee3dc7aSYi Liu err = NULL; 3505ee3dc7aSYi Liu } else { 3515ee3dc7aSYi Liu ret = iommufd_cdev_ram_block_discard_disable(true); 3525ee3dc7aSYi Liu if (ret) { 3535ee3dc7aSYi Liu error_setg(errp, 3545ee3dc7aSYi Liu "Cannot set discarding of RAM broken (%d)", ret); 3555ee3dc7aSYi Liu goto err_discard_disable; 3565ee3dc7aSYi Liu } 3575ee3dc7aSYi Liu goto found_container; 3585ee3dc7aSYi Liu } 3595ee3dc7aSYi Liu } 3605ee3dc7aSYi Liu 3615ee3dc7aSYi Liu /* Need to allocate a new dedicated container */ 3625ee3dc7aSYi Liu ret = iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp); 3635ee3dc7aSYi Liu if (ret < 0) { 3645ee3dc7aSYi Liu goto err_alloc_ioas; 3655ee3dc7aSYi Liu } 3665ee3dc7aSYi Liu 3675ee3dc7aSYi Liu trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id); 3685ee3dc7aSYi Liu 3695ee3dc7aSYi Liu container = g_malloc0(sizeof(*container)); 3705ee3dc7aSYi Liu container->be = vbasedev->iommufd; 3715ee3dc7aSYi Liu container->ioas_id = ioas_id; 3725ee3dc7aSYi Liu 3735ee3dc7aSYi Liu bcontainer = &container->bcontainer; 374ce5f6d49SCédric Le Goater vfio_container_init(bcontainer, space, iommufd_vioc); 3755ee3dc7aSYi Liu QLIST_INSERT_HEAD(&space->containers, bcontainer, next); 3765ee3dc7aSYi Liu 3775ee3dc7aSYi Liu ret = iommufd_cdev_attach_container(vbasedev, container, errp); 3785ee3dc7aSYi Liu if (ret) { 3795ee3dc7aSYi Liu goto err_attach_container; 3805ee3dc7aSYi Liu } 3815ee3dc7aSYi Liu 3825ee3dc7aSYi Liu ret = iommufd_cdev_ram_block_discard_disable(true); 3835ee3dc7aSYi Liu if (ret) { 3845ee3dc7aSYi Liu goto err_discard_disable; 3855ee3dc7aSYi Liu } 3865ee3dc7aSYi Liu 387714e9affSZhenzhong Duan ret = iommufd_cdev_get_info_iova_range(container, ioas_id, &err); 388714e9affSZhenzhong Duan if (ret) { 389714e9affSZhenzhong Duan error_append_hint(&err, 390714e9affSZhenzhong Duan "Fallback to default 64bit IOVA range and 4K page size\n"); 391714e9affSZhenzhong Duan warn_report_err(err); 392714e9affSZhenzhong Duan err = NULL; 3935ee3dc7aSYi Liu bcontainer->pgsizes = qemu_real_host_page_size(); 394714e9affSZhenzhong Duan } 3955ee3dc7aSYi Liu 3965ee3dc7aSYi Liu bcontainer->listener = vfio_memory_listener; 3975ee3dc7aSYi Liu memory_listener_register(&bcontainer->listener, bcontainer->space->as); 3985ee3dc7aSYi Liu 3995ee3dc7aSYi Liu if (bcontainer->error) { 4005ee3dc7aSYi Liu ret = -1; 4015ee3dc7aSYi Liu error_propagate_prepend(errp, bcontainer->error, 4025ee3dc7aSYi Liu "memory listener initialization failed: "); 4035ee3dc7aSYi Liu goto err_listener_register; 4045ee3dc7aSYi Liu } 4055ee3dc7aSYi Liu 4065ee3dc7aSYi Liu bcontainer->initialized = true; 4075ee3dc7aSYi Liu 4085ee3dc7aSYi Liu found_container: 4095ee3dc7aSYi Liu ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info); 4105ee3dc7aSYi Liu if (ret) { 4115ee3dc7aSYi Liu error_setg_errno(errp, errno, "error getting device info"); 4125ee3dc7aSYi Liu goto err_listener_register; 4135ee3dc7aSYi Liu } 4145ee3dc7aSYi Liu 4155ee3dc7aSYi Liu /* 4165ee3dc7aSYi Liu * TODO: examine RAM_BLOCK_DISCARD stuff, should we do group level 4175ee3dc7aSYi Liu * for discarding incompatibility check as well? 4185ee3dc7aSYi Liu */ 4195ee3dc7aSYi Liu if (vbasedev->ram_block_discard_allowed) { 4205ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false); 4215ee3dc7aSYi Liu } 4225ee3dc7aSYi Liu 4235ee3dc7aSYi Liu vbasedev->group = 0; 4245ee3dc7aSYi Liu vbasedev->num_irqs = dev_info.num_irqs; 4255ee3dc7aSYi Liu vbasedev->num_regions = dev_info.num_regions; 4265ee3dc7aSYi Liu vbasedev->flags = dev_info.flags; 4275ee3dc7aSYi Liu vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); 4285ee3dc7aSYi Liu vbasedev->bcontainer = bcontainer; 4295ee3dc7aSYi Liu QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next); 4305ee3dc7aSYi Liu QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next); 4315ee3dc7aSYi Liu 4325ee3dc7aSYi Liu trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs, 4335ee3dc7aSYi Liu vbasedev->num_regions, vbasedev->flags); 4345ee3dc7aSYi Liu return 0; 4355ee3dc7aSYi Liu 4365ee3dc7aSYi Liu err_listener_register: 4375ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false); 4385ee3dc7aSYi Liu err_discard_disable: 4395ee3dc7aSYi Liu iommufd_cdev_detach_container(vbasedev, container); 4405ee3dc7aSYi Liu err_attach_container: 4415ee3dc7aSYi Liu iommufd_cdev_container_destroy(container); 4425ee3dc7aSYi Liu err_alloc_ioas: 4435ee3dc7aSYi Liu vfio_put_address_space(space); 4445ee3dc7aSYi Liu iommufd_cdev_unbind_and_disconnect(vbasedev); 4455ee3dc7aSYi Liu err_connect_bind: 4465ee3dc7aSYi Liu close(vbasedev->fd); 4475ee3dc7aSYi Liu return ret; 4485ee3dc7aSYi Liu } 4495ee3dc7aSYi Liu 4505ee3dc7aSYi Liu static void iommufd_cdev_detach(VFIODevice *vbasedev) 4515ee3dc7aSYi Liu { 4525ee3dc7aSYi Liu VFIOContainerBase *bcontainer = vbasedev->bcontainer; 4535ee3dc7aSYi Liu VFIOAddressSpace *space = bcontainer->space; 4545ee3dc7aSYi Liu VFIOIOMMUFDContainer *container = container_of(bcontainer, 4555ee3dc7aSYi Liu VFIOIOMMUFDContainer, 4565ee3dc7aSYi Liu bcontainer); 4575ee3dc7aSYi Liu QLIST_REMOVE(vbasedev, global_next); 4585ee3dc7aSYi Liu QLIST_REMOVE(vbasedev, container_next); 4595ee3dc7aSYi Liu vbasedev->bcontainer = NULL; 4605ee3dc7aSYi Liu 4615ee3dc7aSYi Liu if (!vbasedev->ram_block_discard_allowed) { 4625ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false); 4635ee3dc7aSYi Liu } 4645ee3dc7aSYi Liu 4655ee3dc7aSYi Liu iommufd_cdev_detach_container(vbasedev, container); 4665ee3dc7aSYi Liu iommufd_cdev_container_destroy(container); 4675ee3dc7aSYi Liu vfio_put_address_space(space); 4685ee3dc7aSYi Liu 4695ee3dc7aSYi Liu iommufd_cdev_unbind_and_disconnect(vbasedev); 4705ee3dc7aSYi Liu close(vbasedev->fd); 4715ee3dc7aSYi Liu } 4725ee3dc7aSYi Liu 47396d6f85fSZhenzhong Duan static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid) 47496d6f85fSZhenzhong Duan { 47596d6f85fSZhenzhong Duan VFIODevice *vbasedev_iter; 476ce5f6d49SCédric Le Goater const VFIOIOMMUClass *iommufd_vioc = 477ce5f6d49SCédric Le Goater VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 47896d6f85fSZhenzhong Duan 47996d6f85fSZhenzhong Duan QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) { 480ce5f6d49SCédric Le Goater if (vbasedev_iter->bcontainer->ops != iommufd_vioc) { 48196d6f85fSZhenzhong Duan continue; 48296d6f85fSZhenzhong Duan } 48396d6f85fSZhenzhong Duan if (devid == vbasedev_iter->devid) { 48496d6f85fSZhenzhong Duan return vbasedev_iter; 48596d6f85fSZhenzhong Duan } 48696d6f85fSZhenzhong Duan } 48796d6f85fSZhenzhong Duan return NULL; 48896d6f85fSZhenzhong Duan } 48996d6f85fSZhenzhong Duan 49096d6f85fSZhenzhong Duan static VFIOPCIDevice * 49196d6f85fSZhenzhong Duan iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev, 49296d6f85fSZhenzhong Duan VFIODevice *reset_dev) 49396d6f85fSZhenzhong Duan { 49496d6f85fSZhenzhong Duan VFIODevice *vbasedev_tmp; 49596d6f85fSZhenzhong Duan 49696d6f85fSZhenzhong Duan if (dep_dev->devid == reset_dev->devid || 49796d6f85fSZhenzhong Duan dep_dev->devid == VFIO_PCI_DEVID_OWNED) { 49896d6f85fSZhenzhong Duan return NULL; 49996d6f85fSZhenzhong Duan } 50096d6f85fSZhenzhong Duan 50196d6f85fSZhenzhong Duan vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid); 50296d6f85fSZhenzhong Duan if (!vbasedev_tmp || !vbasedev_tmp->dev->realized || 50396d6f85fSZhenzhong Duan vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) { 50496d6f85fSZhenzhong Duan return NULL; 50596d6f85fSZhenzhong Duan } 50696d6f85fSZhenzhong Duan 50796d6f85fSZhenzhong Duan return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev); 50896d6f85fSZhenzhong Duan } 50996d6f85fSZhenzhong Duan 51096d6f85fSZhenzhong Duan static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single) 51196d6f85fSZhenzhong Duan { 51296d6f85fSZhenzhong Duan VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev); 51396d6f85fSZhenzhong Duan struct vfio_pci_hot_reset_info *info = NULL; 51496d6f85fSZhenzhong Duan struct vfio_pci_dependent_device *devices; 51596d6f85fSZhenzhong Duan struct vfio_pci_hot_reset *reset; 51696d6f85fSZhenzhong Duan int ret, i; 51796d6f85fSZhenzhong Duan bool multi = false; 51896d6f85fSZhenzhong Duan 51996d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi"); 52096d6f85fSZhenzhong Duan 52196d6f85fSZhenzhong Duan if (!single) { 52296d6f85fSZhenzhong Duan vfio_pci_pre_reset(vdev); 52396d6f85fSZhenzhong Duan } 52496d6f85fSZhenzhong Duan vdev->vbasedev.needs_reset = false; 52596d6f85fSZhenzhong Duan 52696d6f85fSZhenzhong Duan ret = vfio_pci_get_pci_hot_reset_info(vdev, &info); 52796d6f85fSZhenzhong Duan 52896d6f85fSZhenzhong Duan if (ret) { 52996d6f85fSZhenzhong Duan goto out_single; 53096d6f85fSZhenzhong Duan } 53196d6f85fSZhenzhong Duan 53296d6f85fSZhenzhong Duan assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID); 53396d6f85fSZhenzhong Duan 53496d6f85fSZhenzhong Duan devices = &info->devices[0]; 53596d6f85fSZhenzhong Duan 53696d6f85fSZhenzhong Duan if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) { 53796d6f85fSZhenzhong Duan if (!vdev->has_pm_reset) { 53896d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) { 53996d6f85fSZhenzhong Duan if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) { 54096d6f85fSZhenzhong Duan error_report("vfio: Cannot reset device %s, " 54196d6f85fSZhenzhong Duan "depends on device %04x:%02x:%02x.%x " 54296d6f85fSZhenzhong Duan "which is not owned.", 54396d6f85fSZhenzhong Duan vdev->vbasedev.name, devices[i].segment, 54496d6f85fSZhenzhong Duan devices[i].bus, PCI_SLOT(devices[i].devfn), 54596d6f85fSZhenzhong Duan PCI_FUNC(devices[i].devfn)); 54696d6f85fSZhenzhong Duan } 54796d6f85fSZhenzhong Duan } 54896d6f85fSZhenzhong Duan } 54996d6f85fSZhenzhong Duan ret = -EPERM; 55096d6f85fSZhenzhong Duan goto out_single; 55196d6f85fSZhenzhong Duan } 55296d6f85fSZhenzhong Duan 55396d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name); 55496d6f85fSZhenzhong Duan 55596d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) { 55696d6f85fSZhenzhong Duan VFIOPCIDevice *tmp; 55796d6f85fSZhenzhong Duan 55896d6f85fSZhenzhong Duan trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment, 55996d6f85fSZhenzhong Duan devices[i].bus, 56096d6f85fSZhenzhong Duan PCI_SLOT(devices[i].devfn), 56196d6f85fSZhenzhong Duan PCI_FUNC(devices[i].devfn), 56296d6f85fSZhenzhong Duan devices[i].devid); 56396d6f85fSZhenzhong Duan 56496d6f85fSZhenzhong Duan /* 56596d6f85fSZhenzhong Duan * If a VFIO cdev device is resettable, all the dependent devices 56696d6f85fSZhenzhong Duan * are either bound to same iommufd or within same iommu_groups as 56796d6f85fSZhenzhong Duan * one of the iommufd bound devices. 56896d6f85fSZhenzhong Duan */ 56996d6f85fSZhenzhong Duan assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED); 57096d6f85fSZhenzhong Duan 57196d6f85fSZhenzhong Duan tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev); 57296d6f85fSZhenzhong Duan if (!tmp) { 57396d6f85fSZhenzhong Duan continue; 57496d6f85fSZhenzhong Duan } 57596d6f85fSZhenzhong Duan 57696d6f85fSZhenzhong Duan if (single) { 57796d6f85fSZhenzhong Duan ret = -EINVAL; 57896d6f85fSZhenzhong Duan goto out_single; 57996d6f85fSZhenzhong Duan } 58096d6f85fSZhenzhong Duan vfio_pci_pre_reset(tmp); 58196d6f85fSZhenzhong Duan tmp->vbasedev.needs_reset = false; 58296d6f85fSZhenzhong Duan multi = true; 58396d6f85fSZhenzhong Duan } 58496d6f85fSZhenzhong Duan 58596d6f85fSZhenzhong Duan if (!single && !multi) { 58696d6f85fSZhenzhong Duan ret = -EINVAL; 58796d6f85fSZhenzhong Duan goto out_single; 58896d6f85fSZhenzhong Duan } 58996d6f85fSZhenzhong Duan 59096d6f85fSZhenzhong Duan /* Use zero length array for hot reset with iommufd backend */ 59196d6f85fSZhenzhong Duan reset = g_malloc0(sizeof(*reset)); 59296d6f85fSZhenzhong Duan reset->argsz = sizeof(*reset); 59396d6f85fSZhenzhong Duan 59496d6f85fSZhenzhong Duan /* Bus reset! */ 59596d6f85fSZhenzhong Duan ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset); 59696d6f85fSZhenzhong Duan g_free(reset); 59796d6f85fSZhenzhong Duan if (ret) { 59896d6f85fSZhenzhong Duan ret = -errno; 59996d6f85fSZhenzhong Duan } 60096d6f85fSZhenzhong Duan 60196d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset_result(vdev->vbasedev.name, 60296d6f85fSZhenzhong Duan ret ? strerror(errno) : "Success"); 60396d6f85fSZhenzhong Duan 60496d6f85fSZhenzhong Duan /* Re-enable INTx on affected devices */ 60596d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) { 60696d6f85fSZhenzhong Duan VFIOPCIDevice *tmp; 60796d6f85fSZhenzhong Duan 60896d6f85fSZhenzhong Duan tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev); 60996d6f85fSZhenzhong Duan if (!tmp) { 61096d6f85fSZhenzhong Duan continue; 61196d6f85fSZhenzhong Duan } 61296d6f85fSZhenzhong Duan vfio_pci_post_reset(tmp); 61396d6f85fSZhenzhong Duan } 61496d6f85fSZhenzhong Duan out_single: 61596d6f85fSZhenzhong Duan if (!single) { 61696d6f85fSZhenzhong Duan vfio_pci_post_reset(vdev); 61796d6f85fSZhenzhong Duan } 61896d6f85fSZhenzhong Duan g_free(info); 61996d6f85fSZhenzhong Duan 62096d6f85fSZhenzhong Duan return ret; 62196d6f85fSZhenzhong Duan } 62296d6f85fSZhenzhong Duan 623ce5f6d49SCédric Le Goater static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data) 624ce5f6d49SCédric Le Goater { 625ce5f6d49SCédric Le Goater VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass); 626ce5f6d49SCédric Le Goater 627ce5f6d49SCédric Le Goater vioc->dma_map = iommufd_cdev_map; 628ce5f6d49SCédric Le Goater vioc->dma_unmap = iommufd_cdev_unmap; 629ce5f6d49SCédric Le Goater vioc->attach_device = iommufd_cdev_attach; 630ce5f6d49SCédric Le Goater vioc->detach_device = iommufd_cdev_detach; 631ce5f6d49SCédric Le Goater vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset; 6325ee3dc7aSYi Liu }; 633ce5f6d49SCédric Le Goater 634ce5f6d49SCédric Le Goater static const TypeInfo types[] = { 635ce5f6d49SCédric Le Goater { 636ce5f6d49SCédric Le Goater .name = TYPE_VFIO_IOMMU_IOMMUFD, 637ce5f6d49SCédric Le Goater .parent = TYPE_VFIO_IOMMU, 638ce5f6d49SCédric Le Goater .class_init = vfio_iommu_iommufd_class_init, 639ce5f6d49SCédric Le Goater }, 640ce5f6d49SCédric Le Goater }; 641ce5f6d49SCédric Le Goater 642ce5f6d49SCédric Le Goater DEFINE_TYPES(types) 643