xref: /openbmc/qemu/hw/vfio/iommufd.c (revision f3758413)
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 {
1195cf8f51cSZhao Liu     ERRP_GUARD();
1205ee3dc7aSYi Liu     long int ret = -ENOTTY;
12157001144SCédric Le Goater     g_autofree char *path = NULL;
12257001144SCédric Le Goater     g_autofree char *vfio_dev_path = NULL;
12357001144SCédric Le Goater     g_autofree char *vfio_path = NULL;
1245ee3dc7aSYi Liu     DIR *dir = NULL;
1255ee3dc7aSYi Liu     struct dirent *dent;
12657001144SCédric Le Goater     g_autofree gchar *contents = NULL;
1275ee3dc7aSYi Liu     gsize length;
1285ee3dc7aSYi Liu     int major, minor;
1295ee3dc7aSYi Liu     dev_t vfio_devt;
1305ee3dc7aSYi Liu 
1315ee3dc7aSYi Liu     path = g_strdup_printf("%s/vfio-dev", sysfs_path);
1325ee3dc7aSYi Liu     dir = opendir(path);
1335ee3dc7aSYi Liu     if (!dir) {
1345ee3dc7aSYi Liu         error_setg_errno(errp, errno, "couldn't open directory %s", path);
13557001144SCédric Le Goater         goto out;
1365ee3dc7aSYi Liu     }
1375ee3dc7aSYi Liu 
1385ee3dc7aSYi Liu     while ((dent = readdir(dir))) {
1395ee3dc7aSYi Liu         if (!strncmp(dent->d_name, "vfio", 4)) {
1405ee3dc7aSYi Liu             vfio_dev_path = g_strdup_printf("%s/%s/dev", path, dent->d_name);
1415ee3dc7aSYi Liu             break;
1425ee3dc7aSYi Liu         }
1435ee3dc7aSYi Liu     }
1445ee3dc7aSYi Liu 
1455ee3dc7aSYi Liu     if (!vfio_dev_path) {
1465ee3dc7aSYi Liu         error_setg(errp, "failed to find vfio-dev/vfioX/dev");
1475ee3dc7aSYi Liu         goto out_close_dir;
1485ee3dc7aSYi Liu     }
1495ee3dc7aSYi Liu 
1505ee3dc7aSYi Liu     if (!g_file_get_contents(vfio_dev_path, &contents, &length, NULL)) {
1515ee3dc7aSYi Liu         error_setg(errp, "failed to load \"%s\"", vfio_dev_path);
15257001144SCédric Le Goater         goto out_close_dir;
1535ee3dc7aSYi Liu     }
1545ee3dc7aSYi Liu 
1555ee3dc7aSYi Liu     if (sscanf(contents, "%d:%d", &major, &minor) != 2) {
1565ee3dc7aSYi Liu         error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path);
15757001144SCédric Le Goater         goto out_close_dir;
1585ee3dc7aSYi Liu     }
1595ee3dc7aSYi Liu     vfio_devt = makedev(major, minor);
1605ee3dc7aSYi Liu 
1615ee3dc7aSYi Liu     vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name);
1625ee3dc7aSYi Liu     ret = open_cdev(vfio_path, vfio_devt);
1635ee3dc7aSYi Liu     if (ret < 0) {
1645ee3dc7aSYi Liu         error_setg(errp, "Failed to open %s", vfio_path);
1655ee3dc7aSYi Liu     }
1665ee3dc7aSYi Liu 
1675ee3dc7aSYi Liu     trace_iommufd_cdev_getfd(vfio_path, ret);
1685ee3dc7aSYi Liu 
1695ee3dc7aSYi Liu out_close_dir:
1705ee3dc7aSYi Liu     closedir(dir);
17157001144SCédric Le Goater out:
1725ee3dc7aSYi Liu     if (*errp) {
1735ee3dc7aSYi Liu         error_prepend(errp, VFIO_MSG_PREFIX, path);
1745ee3dc7aSYi Liu     }
1755ee3dc7aSYi Liu 
1765ee3dc7aSYi Liu     return ret;
1775ee3dc7aSYi Liu }
1785ee3dc7aSYi Liu 
1795ee3dc7aSYi Liu static int iommufd_cdev_attach_ioas_hwpt(VFIODevice *vbasedev, uint32_t id,
1805ee3dc7aSYi Liu                                          Error **errp)
1815ee3dc7aSYi Liu {
1825ee3dc7aSYi Liu     int ret, iommufd = vbasedev->iommufd->fd;
1835ee3dc7aSYi Liu     struct vfio_device_attach_iommufd_pt attach_data = {
1845ee3dc7aSYi Liu         .argsz = sizeof(attach_data),
1855ee3dc7aSYi Liu         .flags = 0,
1865ee3dc7aSYi Liu         .pt_id = id,
1875ee3dc7aSYi Liu     };
1885ee3dc7aSYi Liu 
1895ee3dc7aSYi Liu     /* Attach device to an IOAS or hwpt within iommufd */
1905ee3dc7aSYi Liu     ret = ioctl(vbasedev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data);
1915ee3dc7aSYi Liu     if (ret) {
1925ee3dc7aSYi Liu         error_setg_errno(errp, errno,
1935ee3dc7aSYi Liu                          "[iommufd=%d] error attach %s (%d) to id=%d",
1945ee3dc7aSYi Liu                          iommufd, vbasedev->name, vbasedev->fd, id);
1955ee3dc7aSYi Liu     } else {
1965ee3dc7aSYi Liu         trace_iommufd_cdev_attach_ioas_hwpt(iommufd, vbasedev->name,
1975ee3dc7aSYi Liu                                             vbasedev->fd, id);
1985ee3dc7aSYi Liu     }
1995ee3dc7aSYi Liu     return ret;
2005ee3dc7aSYi Liu }
2015ee3dc7aSYi Liu 
2025ee3dc7aSYi Liu static int iommufd_cdev_detach_ioas_hwpt(VFIODevice *vbasedev, Error **errp)
2035ee3dc7aSYi Liu {
2045ee3dc7aSYi Liu     int ret, iommufd = vbasedev->iommufd->fd;
2055ee3dc7aSYi Liu     struct vfio_device_detach_iommufd_pt detach_data = {
2065ee3dc7aSYi Liu         .argsz = sizeof(detach_data),
2075ee3dc7aSYi Liu         .flags = 0,
2085ee3dc7aSYi Liu     };
2095ee3dc7aSYi Liu 
2105ee3dc7aSYi Liu     ret = ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_data);
2115ee3dc7aSYi Liu     if (ret) {
2125ee3dc7aSYi Liu         error_setg_errno(errp, errno, "detach %s failed", vbasedev->name);
2135ee3dc7aSYi Liu     } else {
2145ee3dc7aSYi Liu         trace_iommufd_cdev_detach_ioas_hwpt(iommufd, vbasedev->name);
2155ee3dc7aSYi Liu     }
2165ee3dc7aSYi Liu     return ret;
2175ee3dc7aSYi Liu }
2185ee3dc7aSYi Liu 
2195ee3dc7aSYi Liu static int iommufd_cdev_attach_container(VFIODevice *vbasedev,
2205ee3dc7aSYi Liu                                          VFIOIOMMUFDContainer *container,
2215ee3dc7aSYi Liu                                          Error **errp)
2225ee3dc7aSYi Liu {
2235ee3dc7aSYi Liu     return iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp);
2245ee3dc7aSYi Liu }
2255ee3dc7aSYi Liu 
2265ee3dc7aSYi Liu static void iommufd_cdev_detach_container(VFIODevice *vbasedev,
2275ee3dc7aSYi Liu                                           VFIOIOMMUFDContainer *container)
2285ee3dc7aSYi Liu {
2295ee3dc7aSYi Liu     Error *err = NULL;
2305ee3dc7aSYi Liu 
2315ee3dc7aSYi Liu     if (iommufd_cdev_detach_ioas_hwpt(vbasedev, &err)) {
2325ee3dc7aSYi Liu         error_report_err(err);
2335ee3dc7aSYi Liu     }
2345ee3dc7aSYi Liu }
2355ee3dc7aSYi Liu 
2365ee3dc7aSYi Liu static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container)
2375ee3dc7aSYi Liu {
2385ee3dc7aSYi Liu     VFIOContainerBase *bcontainer = &container->bcontainer;
2395ee3dc7aSYi Liu 
2405ee3dc7aSYi Liu     if (!QLIST_EMPTY(&bcontainer->device_list)) {
2415ee3dc7aSYi Liu         return;
2425ee3dc7aSYi Liu     }
2435ee3dc7aSYi Liu     memory_listener_unregister(&bcontainer->listener);
2445ee3dc7aSYi Liu     vfio_container_destroy(bcontainer);
2455ee3dc7aSYi Liu     iommufd_backend_free_id(container->be, container->ioas_id);
2465ee3dc7aSYi Liu     g_free(container);
2475ee3dc7aSYi Liu }
2485ee3dc7aSYi Liu 
2495ee3dc7aSYi Liu static int iommufd_cdev_ram_block_discard_disable(bool state)
2505ee3dc7aSYi Liu {
2515ee3dc7aSYi Liu     /*
2525ee3dc7aSYi Liu      * We support coordinated discarding of RAM via the RamDiscardManager.
2535ee3dc7aSYi Liu      */
2545ee3dc7aSYi Liu     return ram_block_uncoordinated_discard_disable(state);
2555ee3dc7aSYi Liu }
2565ee3dc7aSYi Liu 
257714e9affSZhenzhong Duan static int iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container,
258714e9affSZhenzhong Duan                                             uint32_t ioas_id, Error **errp)
259714e9affSZhenzhong Duan {
260714e9affSZhenzhong Duan     VFIOContainerBase *bcontainer = &container->bcontainer;
261*f3758413SZhenzhong Duan     g_autofree struct iommu_ioas_iova_ranges *info = NULL;
262714e9affSZhenzhong Duan     struct iommu_iova_range *iova_ranges;
263714e9affSZhenzhong Duan     int ret, sz, fd = container->be->fd;
264714e9affSZhenzhong Duan 
265714e9affSZhenzhong Duan     info = g_malloc0(sizeof(*info));
266714e9affSZhenzhong Duan     info->size = sizeof(*info);
267714e9affSZhenzhong Duan     info->ioas_id = ioas_id;
268714e9affSZhenzhong Duan 
269714e9affSZhenzhong Duan     ret = ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info);
270714e9affSZhenzhong Duan     if (ret && errno != EMSGSIZE) {
271714e9affSZhenzhong Duan         goto error;
272714e9affSZhenzhong Duan     }
273714e9affSZhenzhong Duan 
274714e9affSZhenzhong Duan     sz = info->num_iovas * sizeof(struct iommu_iova_range);
275714e9affSZhenzhong Duan     info = g_realloc(info, sizeof(*info) + sz);
276714e9affSZhenzhong Duan     info->allowed_iovas = (uintptr_t)(info + 1);
277714e9affSZhenzhong Duan 
278714e9affSZhenzhong Duan     ret = ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info);
279714e9affSZhenzhong Duan     if (ret) {
280714e9affSZhenzhong Duan         goto error;
281714e9affSZhenzhong Duan     }
282714e9affSZhenzhong Duan 
283714e9affSZhenzhong Duan     iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas;
284714e9affSZhenzhong Duan 
285714e9affSZhenzhong Duan     for (int i = 0; i < info->num_iovas; i++) {
286714e9affSZhenzhong Duan         Range *range = g_new(Range, 1);
287714e9affSZhenzhong Duan 
288714e9affSZhenzhong Duan         range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last);
289714e9affSZhenzhong Duan         bcontainer->iova_ranges =
290714e9affSZhenzhong Duan             range_list_insert(bcontainer->iova_ranges, range);
291714e9affSZhenzhong Duan     }
292714e9affSZhenzhong Duan     bcontainer->pgsizes = info->out_iova_alignment;
293714e9affSZhenzhong Duan 
294714e9affSZhenzhong Duan     return 0;
295714e9affSZhenzhong Duan 
296714e9affSZhenzhong Duan error:
297714e9affSZhenzhong Duan     ret = -errno;
298714e9affSZhenzhong Duan     error_setg_errno(errp, errno, "Cannot get IOVA ranges");
299714e9affSZhenzhong Duan     return ret;
300714e9affSZhenzhong Duan }
301714e9affSZhenzhong Duan 
3025ee3dc7aSYi Liu static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
3035ee3dc7aSYi Liu                                AddressSpace *as, Error **errp)
3045ee3dc7aSYi Liu {
3055ee3dc7aSYi Liu     VFIOContainerBase *bcontainer;
3065ee3dc7aSYi Liu     VFIOIOMMUFDContainer *container;
3075ee3dc7aSYi Liu     VFIOAddressSpace *space;
3085ee3dc7aSYi Liu     struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
3095ee3dc7aSYi Liu     int ret, devfd;
3105ee3dc7aSYi Liu     uint32_t ioas_id;
3115ee3dc7aSYi Liu     Error *err = NULL;
312ce5f6d49SCédric Le Goater     const VFIOIOMMUClass *iommufd_vioc =
313ce5f6d49SCédric Le Goater         VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
3145ee3dc7aSYi Liu 
315da3e04b2SZhenzhong Duan     if (vbasedev->fd < 0) {
3165ee3dc7aSYi Liu         devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp);
3175ee3dc7aSYi Liu         if (devfd < 0) {
3185ee3dc7aSYi Liu             return devfd;
3195ee3dc7aSYi Liu         }
3205ee3dc7aSYi Liu         vbasedev->fd = devfd;
321da3e04b2SZhenzhong Duan     } else {
322da3e04b2SZhenzhong Duan         devfd = vbasedev->fd;
323da3e04b2SZhenzhong Duan     }
3245ee3dc7aSYi Liu 
3255ee3dc7aSYi Liu     ret = iommufd_cdev_connect_and_bind(vbasedev, errp);
3265ee3dc7aSYi Liu     if (ret) {
3275ee3dc7aSYi Liu         goto err_connect_bind;
3285ee3dc7aSYi Liu     }
3295ee3dc7aSYi Liu 
3305ee3dc7aSYi Liu     space = vfio_get_address_space(as);
3315ee3dc7aSYi Liu 
3325ee3dc7aSYi Liu     /* try to attach to an existing container in this space */
3335ee3dc7aSYi Liu     QLIST_FOREACH(bcontainer, &space->containers, next) {
3345ee3dc7aSYi Liu         container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
335ce5f6d49SCédric Le Goater         if (bcontainer->ops != iommufd_vioc ||
3365ee3dc7aSYi Liu             vbasedev->iommufd != container->be) {
3375ee3dc7aSYi Liu             continue;
3385ee3dc7aSYi Liu         }
3395ee3dc7aSYi Liu         if (iommufd_cdev_attach_container(vbasedev, container, &err)) {
3405ee3dc7aSYi Liu             const char *msg = error_get_pretty(err);
3415ee3dc7aSYi Liu 
3425ee3dc7aSYi Liu             trace_iommufd_cdev_fail_attach_existing_container(msg);
3435ee3dc7aSYi Liu             error_free(err);
3445ee3dc7aSYi Liu             err = NULL;
3455ee3dc7aSYi Liu         } else {
3465ee3dc7aSYi Liu             ret = iommufd_cdev_ram_block_discard_disable(true);
3475ee3dc7aSYi Liu             if (ret) {
3485ee3dc7aSYi Liu                 error_setg(errp,
3495ee3dc7aSYi Liu                               "Cannot set discarding of RAM broken (%d)", ret);
3505ee3dc7aSYi Liu                 goto err_discard_disable;
3515ee3dc7aSYi Liu             }
3525ee3dc7aSYi Liu             goto found_container;
3535ee3dc7aSYi Liu         }
3545ee3dc7aSYi Liu     }
3555ee3dc7aSYi Liu 
3565ee3dc7aSYi Liu     /* Need to allocate a new dedicated container */
3575ee3dc7aSYi Liu     ret = iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp);
3585ee3dc7aSYi Liu     if (ret < 0) {
3595ee3dc7aSYi Liu         goto err_alloc_ioas;
3605ee3dc7aSYi Liu     }
3615ee3dc7aSYi Liu 
3625ee3dc7aSYi Liu     trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id);
3635ee3dc7aSYi Liu 
3645ee3dc7aSYi Liu     container = g_malloc0(sizeof(*container));
3655ee3dc7aSYi Liu     container->be = vbasedev->iommufd;
3665ee3dc7aSYi Liu     container->ioas_id = ioas_id;
3675ee3dc7aSYi Liu 
3685ee3dc7aSYi Liu     bcontainer = &container->bcontainer;
369ce5f6d49SCédric Le Goater     vfio_container_init(bcontainer, space, iommufd_vioc);
3705ee3dc7aSYi Liu     QLIST_INSERT_HEAD(&space->containers, bcontainer, next);
3715ee3dc7aSYi Liu 
3725ee3dc7aSYi Liu     ret = iommufd_cdev_attach_container(vbasedev, container, errp);
3735ee3dc7aSYi Liu     if (ret) {
3745ee3dc7aSYi Liu         goto err_attach_container;
3755ee3dc7aSYi Liu     }
3765ee3dc7aSYi Liu 
3775ee3dc7aSYi Liu     ret = iommufd_cdev_ram_block_discard_disable(true);
3785ee3dc7aSYi Liu     if (ret) {
3795ee3dc7aSYi Liu         goto err_discard_disable;
3805ee3dc7aSYi Liu     }
3815ee3dc7aSYi Liu 
382714e9affSZhenzhong Duan     ret = iommufd_cdev_get_info_iova_range(container, ioas_id, &err);
383714e9affSZhenzhong Duan     if (ret) {
384714e9affSZhenzhong Duan         error_append_hint(&err,
385714e9affSZhenzhong Duan                    "Fallback to default 64bit IOVA range and 4K page size\n");
386714e9affSZhenzhong Duan         warn_report_err(err);
387714e9affSZhenzhong Duan         err = NULL;
3885ee3dc7aSYi Liu         bcontainer->pgsizes = qemu_real_host_page_size();
389714e9affSZhenzhong Duan     }
3905ee3dc7aSYi Liu 
3915ee3dc7aSYi Liu     bcontainer->listener = vfio_memory_listener;
3925ee3dc7aSYi Liu     memory_listener_register(&bcontainer->listener, bcontainer->space->as);
3935ee3dc7aSYi Liu 
3945ee3dc7aSYi Liu     if (bcontainer->error) {
3955ee3dc7aSYi Liu         ret = -1;
3965ee3dc7aSYi Liu         error_propagate_prepend(errp, bcontainer->error,
3975ee3dc7aSYi Liu                                 "memory listener initialization failed: ");
3985ee3dc7aSYi Liu         goto err_listener_register;
3995ee3dc7aSYi Liu     }
4005ee3dc7aSYi Liu 
4015ee3dc7aSYi Liu     bcontainer->initialized = true;
4025ee3dc7aSYi Liu 
4035ee3dc7aSYi Liu found_container:
4045ee3dc7aSYi Liu     ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info);
4055ee3dc7aSYi Liu     if (ret) {
4065ee3dc7aSYi Liu         error_setg_errno(errp, errno, "error getting device info");
4075ee3dc7aSYi Liu         goto err_listener_register;
4085ee3dc7aSYi Liu     }
4095ee3dc7aSYi Liu 
410d9fa4223SSteve Sistare     ret = vfio_cpr_register_container(bcontainer, errp);
411d9fa4223SSteve Sistare     if (ret) {
412d9fa4223SSteve Sistare         goto err_listener_register;
413d9fa4223SSteve Sistare     }
414d9fa4223SSteve Sistare 
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 
465d9fa4223SSteve Sistare     vfio_cpr_unregister_container(bcontainer);
4665ee3dc7aSYi Liu     iommufd_cdev_detach_container(vbasedev, container);
4675ee3dc7aSYi Liu     iommufd_cdev_container_destroy(container);
4685ee3dc7aSYi Liu     vfio_put_address_space(space);
4695ee3dc7aSYi Liu 
4705ee3dc7aSYi Liu     iommufd_cdev_unbind_and_disconnect(vbasedev);
4715ee3dc7aSYi Liu     close(vbasedev->fd);
4725ee3dc7aSYi Liu }
4735ee3dc7aSYi Liu 
47496d6f85fSZhenzhong Duan static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid)
47596d6f85fSZhenzhong Duan {
47696d6f85fSZhenzhong Duan     VFIODevice *vbasedev_iter;
477ce5f6d49SCédric Le Goater     const VFIOIOMMUClass *iommufd_vioc =
478ce5f6d49SCédric Le Goater         VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
47996d6f85fSZhenzhong Duan 
48096d6f85fSZhenzhong Duan     QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) {
481ce5f6d49SCédric Le Goater         if (vbasedev_iter->bcontainer->ops != iommufd_vioc) {
48296d6f85fSZhenzhong Duan             continue;
48396d6f85fSZhenzhong Duan         }
48496d6f85fSZhenzhong Duan         if (devid == vbasedev_iter->devid) {
48596d6f85fSZhenzhong Duan             return vbasedev_iter;
48696d6f85fSZhenzhong Duan         }
48796d6f85fSZhenzhong Duan     }
48896d6f85fSZhenzhong Duan     return NULL;
48996d6f85fSZhenzhong Duan }
49096d6f85fSZhenzhong Duan 
49196d6f85fSZhenzhong Duan static VFIOPCIDevice *
49296d6f85fSZhenzhong Duan iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev,
49396d6f85fSZhenzhong Duan                                     VFIODevice *reset_dev)
49496d6f85fSZhenzhong Duan {
49596d6f85fSZhenzhong Duan     VFIODevice *vbasedev_tmp;
49696d6f85fSZhenzhong Duan 
49796d6f85fSZhenzhong Duan     if (dep_dev->devid == reset_dev->devid ||
49896d6f85fSZhenzhong Duan         dep_dev->devid == VFIO_PCI_DEVID_OWNED) {
49996d6f85fSZhenzhong Duan         return NULL;
50096d6f85fSZhenzhong Duan     }
50196d6f85fSZhenzhong Duan 
50296d6f85fSZhenzhong Duan     vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid);
50396d6f85fSZhenzhong Duan     if (!vbasedev_tmp || !vbasedev_tmp->dev->realized ||
50496d6f85fSZhenzhong Duan         vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) {
50596d6f85fSZhenzhong Duan         return NULL;
50696d6f85fSZhenzhong Duan     }
50796d6f85fSZhenzhong Duan 
50896d6f85fSZhenzhong Duan     return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev);
50996d6f85fSZhenzhong Duan }
51096d6f85fSZhenzhong Duan 
51196d6f85fSZhenzhong Duan static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single)
51296d6f85fSZhenzhong Duan {
51396d6f85fSZhenzhong Duan     VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
51496d6f85fSZhenzhong Duan     struct vfio_pci_hot_reset_info *info = NULL;
51596d6f85fSZhenzhong Duan     struct vfio_pci_dependent_device *devices;
51696d6f85fSZhenzhong Duan     struct vfio_pci_hot_reset *reset;
51796d6f85fSZhenzhong Duan     int ret, i;
51896d6f85fSZhenzhong Duan     bool multi = false;
51996d6f85fSZhenzhong Duan 
52096d6f85fSZhenzhong Duan     trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi");
52196d6f85fSZhenzhong Duan 
52296d6f85fSZhenzhong Duan     if (!single) {
52396d6f85fSZhenzhong Duan         vfio_pci_pre_reset(vdev);
52496d6f85fSZhenzhong Duan     }
52596d6f85fSZhenzhong Duan     vdev->vbasedev.needs_reset = false;
52696d6f85fSZhenzhong Duan 
52796d6f85fSZhenzhong Duan     ret = vfio_pci_get_pci_hot_reset_info(vdev, &info);
52896d6f85fSZhenzhong Duan 
52996d6f85fSZhenzhong Duan     if (ret) {
53096d6f85fSZhenzhong Duan         goto out_single;
53196d6f85fSZhenzhong Duan     }
53296d6f85fSZhenzhong Duan 
53396d6f85fSZhenzhong Duan     assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID);
53496d6f85fSZhenzhong Duan 
53596d6f85fSZhenzhong Duan     devices = &info->devices[0];
53696d6f85fSZhenzhong Duan 
53796d6f85fSZhenzhong Duan     if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) {
53896d6f85fSZhenzhong Duan         if (!vdev->has_pm_reset) {
53996d6f85fSZhenzhong Duan             for (i = 0; i < info->count; i++) {
54096d6f85fSZhenzhong Duan                 if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) {
54196d6f85fSZhenzhong Duan                     error_report("vfio: Cannot reset device %s, "
54296d6f85fSZhenzhong Duan                                  "depends on device %04x:%02x:%02x.%x "
54396d6f85fSZhenzhong Duan                                  "which is not owned.",
54496d6f85fSZhenzhong Duan                                  vdev->vbasedev.name, devices[i].segment,
54596d6f85fSZhenzhong Duan                                  devices[i].bus, PCI_SLOT(devices[i].devfn),
54696d6f85fSZhenzhong Duan                                  PCI_FUNC(devices[i].devfn));
54796d6f85fSZhenzhong Duan                 }
54896d6f85fSZhenzhong Duan             }
54996d6f85fSZhenzhong Duan         }
55096d6f85fSZhenzhong Duan         ret = -EPERM;
55196d6f85fSZhenzhong Duan         goto out_single;
55296d6f85fSZhenzhong Duan     }
55396d6f85fSZhenzhong Duan 
55496d6f85fSZhenzhong Duan     trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name);
55596d6f85fSZhenzhong Duan 
55696d6f85fSZhenzhong Duan     for (i = 0; i < info->count; i++) {
55796d6f85fSZhenzhong Duan         VFIOPCIDevice *tmp;
55896d6f85fSZhenzhong Duan 
55996d6f85fSZhenzhong Duan         trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment,
56096d6f85fSZhenzhong Duan                                                      devices[i].bus,
56196d6f85fSZhenzhong Duan                                                      PCI_SLOT(devices[i].devfn),
56296d6f85fSZhenzhong Duan                                                      PCI_FUNC(devices[i].devfn),
56396d6f85fSZhenzhong Duan                                                      devices[i].devid);
56496d6f85fSZhenzhong Duan 
56596d6f85fSZhenzhong Duan         /*
56696d6f85fSZhenzhong Duan          * If a VFIO cdev device is resettable, all the dependent devices
56796d6f85fSZhenzhong Duan          * are either bound to same iommufd or within same iommu_groups as
56896d6f85fSZhenzhong Duan          * one of the iommufd bound devices.
56996d6f85fSZhenzhong Duan          */
57096d6f85fSZhenzhong Duan         assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED);
57196d6f85fSZhenzhong Duan 
57296d6f85fSZhenzhong Duan         tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
57396d6f85fSZhenzhong Duan         if (!tmp) {
57496d6f85fSZhenzhong Duan             continue;
57596d6f85fSZhenzhong Duan         }
57696d6f85fSZhenzhong Duan 
57796d6f85fSZhenzhong Duan         if (single) {
57896d6f85fSZhenzhong Duan             ret = -EINVAL;
57996d6f85fSZhenzhong Duan             goto out_single;
58096d6f85fSZhenzhong Duan         }
58196d6f85fSZhenzhong Duan         vfio_pci_pre_reset(tmp);
58296d6f85fSZhenzhong Duan         tmp->vbasedev.needs_reset = false;
58396d6f85fSZhenzhong Duan         multi = true;
58496d6f85fSZhenzhong Duan     }
58596d6f85fSZhenzhong Duan 
58696d6f85fSZhenzhong Duan     if (!single && !multi) {
58796d6f85fSZhenzhong Duan         ret = -EINVAL;
58896d6f85fSZhenzhong Duan         goto out_single;
58996d6f85fSZhenzhong Duan     }
59096d6f85fSZhenzhong Duan 
59196d6f85fSZhenzhong Duan     /* Use zero length array for hot reset with iommufd backend */
59296d6f85fSZhenzhong Duan     reset = g_malloc0(sizeof(*reset));
59396d6f85fSZhenzhong Duan     reset->argsz = sizeof(*reset);
59496d6f85fSZhenzhong Duan 
59596d6f85fSZhenzhong Duan      /* Bus reset! */
59696d6f85fSZhenzhong Duan     ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset);
59796d6f85fSZhenzhong Duan     g_free(reset);
59896d6f85fSZhenzhong Duan     if (ret) {
59996d6f85fSZhenzhong Duan         ret = -errno;
60096d6f85fSZhenzhong Duan     }
60196d6f85fSZhenzhong Duan 
60296d6f85fSZhenzhong Duan     trace_vfio_pci_hot_reset_result(vdev->vbasedev.name,
60396d6f85fSZhenzhong Duan                                     ret ? strerror(errno) : "Success");
60496d6f85fSZhenzhong Duan 
60596d6f85fSZhenzhong Duan     /* Re-enable INTx on affected devices */
60696d6f85fSZhenzhong Duan     for (i = 0; i < info->count; i++) {
60796d6f85fSZhenzhong Duan         VFIOPCIDevice *tmp;
60896d6f85fSZhenzhong Duan 
60996d6f85fSZhenzhong Duan         tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
61096d6f85fSZhenzhong Duan         if (!tmp) {
61196d6f85fSZhenzhong Duan             continue;
61296d6f85fSZhenzhong Duan         }
61396d6f85fSZhenzhong Duan         vfio_pci_post_reset(tmp);
61496d6f85fSZhenzhong Duan     }
61596d6f85fSZhenzhong Duan out_single:
61696d6f85fSZhenzhong Duan     if (!single) {
61796d6f85fSZhenzhong Duan         vfio_pci_post_reset(vdev);
61896d6f85fSZhenzhong Duan     }
61996d6f85fSZhenzhong Duan     g_free(info);
62096d6f85fSZhenzhong Duan 
62196d6f85fSZhenzhong Duan     return ret;
62296d6f85fSZhenzhong Duan }
62396d6f85fSZhenzhong Duan 
624ce5f6d49SCédric Le Goater static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
625ce5f6d49SCédric Le Goater {
626ce5f6d49SCédric Le Goater     VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
627ce5f6d49SCédric Le Goater 
628ce5f6d49SCédric Le Goater     vioc->dma_map = iommufd_cdev_map;
629ce5f6d49SCédric Le Goater     vioc->dma_unmap = iommufd_cdev_unmap;
630ce5f6d49SCédric Le Goater     vioc->attach_device = iommufd_cdev_attach;
631ce5f6d49SCédric Le Goater     vioc->detach_device = iommufd_cdev_detach;
632ce5f6d49SCédric Le Goater     vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
6335ee3dc7aSYi Liu };
634ce5f6d49SCédric Le Goater 
635ce5f6d49SCédric Le Goater static const TypeInfo types[] = {
636ce5f6d49SCédric Le Goater     {
637ce5f6d49SCédric Le Goater         .name = TYPE_VFIO_IOMMU_IOMMUFD,
638ce5f6d49SCédric Le Goater         .parent = TYPE_VFIO_IOMMU,
639ce5f6d49SCédric Le Goater         .class_init = vfio_iommu_iommufd_class_init,
640ce5f6d49SCédric Le Goater     },
641ce5f6d49SCédric Le Goater };
642ce5f6d49SCédric Le Goater 
643ce5f6d49SCédric Le Goater DEFINE_TYPES(types)
644