xref: /openbmc/qemu/hw/vfio/iommufd.c (revision dddfd8d6)
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 
113*dddfd8d6SJoao Martins static bool iommufd_hwpt_dirty_tracking(VFIOIOASHwpt *hwpt)
114*dddfd8d6SJoao Martins {
115*dddfd8d6SJoao Martins     return hwpt && hwpt->hwpt_flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
116*dddfd8d6SJoao Martins }
117*dddfd8d6SJoao Martins 
1185ee3dc7aSYi Liu static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
1195ee3dc7aSYi Liu {
1205cf8f51cSZhao Liu     ERRP_GUARD();
1215ee3dc7aSYi Liu     long int ret = -ENOTTY;
12257001144SCédric Le Goater     g_autofree char *path = NULL;
12357001144SCédric Le Goater     g_autofree char *vfio_dev_path = NULL;
12457001144SCédric Le Goater     g_autofree char *vfio_path = NULL;
1255ee3dc7aSYi Liu     DIR *dir = NULL;
1265ee3dc7aSYi Liu     struct dirent *dent;
12757001144SCédric Le Goater     g_autofree gchar *contents = NULL;
1285ee3dc7aSYi Liu     gsize length;
1295ee3dc7aSYi Liu     int major, minor;
1305ee3dc7aSYi Liu     dev_t vfio_devt;
1315ee3dc7aSYi Liu 
1325ee3dc7aSYi Liu     path = g_strdup_printf("%s/vfio-dev", sysfs_path);
1335ee3dc7aSYi Liu     dir = opendir(path);
1345ee3dc7aSYi Liu     if (!dir) {
1355ee3dc7aSYi Liu         error_setg_errno(errp, errno, "couldn't open directory %s", path);
13657001144SCédric Le Goater         goto out;
1375ee3dc7aSYi Liu     }
1385ee3dc7aSYi Liu 
1395ee3dc7aSYi Liu     while ((dent = readdir(dir))) {
1405ee3dc7aSYi Liu         if (!strncmp(dent->d_name, "vfio", 4)) {
1415ee3dc7aSYi Liu             vfio_dev_path = g_strdup_printf("%s/%s/dev", path, dent->d_name);
1425ee3dc7aSYi Liu             break;
1435ee3dc7aSYi Liu         }
1445ee3dc7aSYi Liu     }
1455ee3dc7aSYi Liu 
1465ee3dc7aSYi Liu     if (!vfio_dev_path) {
1475ee3dc7aSYi Liu         error_setg(errp, "failed to find vfio-dev/vfioX/dev");
1485ee3dc7aSYi Liu         goto out_close_dir;
1495ee3dc7aSYi Liu     }
1505ee3dc7aSYi Liu 
1515ee3dc7aSYi Liu     if (!g_file_get_contents(vfio_dev_path, &contents, &length, NULL)) {
1525ee3dc7aSYi Liu         error_setg(errp, "failed to load \"%s\"", vfio_dev_path);
15357001144SCédric Le Goater         goto out_close_dir;
1545ee3dc7aSYi Liu     }
1555ee3dc7aSYi Liu 
1565ee3dc7aSYi Liu     if (sscanf(contents, "%d:%d", &major, &minor) != 2) {
1575ee3dc7aSYi Liu         error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path);
15857001144SCédric Le Goater         goto out_close_dir;
1595ee3dc7aSYi Liu     }
1605ee3dc7aSYi Liu     vfio_devt = makedev(major, minor);
1615ee3dc7aSYi Liu 
1625ee3dc7aSYi Liu     vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name);
1635ee3dc7aSYi Liu     ret = open_cdev(vfio_path, vfio_devt);
1645ee3dc7aSYi Liu     if (ret < 0) {
1655ee3dc7aSYi Liu         error_setg(errp, "Failed to open %s", vfio_path);
1665ee3dc7aSYi Liu     }
1675ee3dc7aSYi Liu 
1685ee3dc7aSYi Liu     trace_iommufd_cdev_getfd(vfio_path, ret);
1695ee3dc7aSYi Liu 
1705ee3dc7aSYi Liu out_close_dir:
1715ee3dc7aSYi Liu     closedir(dir);
17257001144SCédric Le Goater out:
1735ee3dc7aSYi Liu     if (*errp) {
1745ee3dc7aSYi Liu         error_prepend(errp, VFIO_MSG_PREFIX, path);
1755ee3dc7aSYi Liu     }
1765ee3dc7aSYi Liu 
1775ee3dc7aSYi Liu     return ret;
1785ee3dc7aSYi Liu }
1795ee3dc7aSYi Liu 
180b07dcb7dSJoao Martins static int iommufd_cdev_attach_ioas_hwpt(VFIODevice *vbasedev, uint32_t id,
1815ee3dc7aSYi Liu                                          Error **errp)
1825ee3dc7aSYi Liu {
18345d0d8c4SZhenzhong Duan     int iommufd = vbasedev->iommufd->fd;
1845ee3dc7aSYi Liu     struct vfio_device_attach_iommufd_pt attach_data = {
1855ee3dc7aSYi Liu         .argsz = sizeof(attach_data),
1865ee3dc7aSYi Liu         .flags = 0,
1875ee3dc7aSYi Liu         .pt_id = id,
1885ee3dc7aSYi Liu     };
1895ee3dc7aSYi Liu 
1905ee3dc7aSYi Liu     /* Attach device to an IOAS or hwpt within iommufd */
19145d0d8c4SZhenzhong Duan     if (ioctl(vbasedev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data)) {
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);
195b07dcb7dSJoao Martins         return -errno;
1965ee3dc7aSYi Liu     }
1975ee3dc7aSYi Liu 
19845d0d8c4SZhenzhong Duan     trace_iommufd_cdev_attach_ioas_hwpt(iommufd, vbasedev->name,
19945d0d8c4SZhenzhong Duan                                         vbasedev->fd, id);
200b07dcb7dSJoao Martins     return 0;
20145d0d8c4SZhenzhong Duan }
20245d0d8c4SZhenzhong Duan 
20345d0d8c4SZhenzhong Duan static bool iommufd_cdev_detach_ioas_hwpt(VFIODevice *vbasedev, Error **errp)
2045ee3dc7aSYi Liu {
20545d0d8c4SZhenzhong Duan     int iommufd = vbasedev->iommufd->fd;
2065ee3dc7aSYi Liu     struct vfio_device_detach_iommufd_pt detach_data = {
2075ee3dc7aSYi Liu         .argsz = sizeof(detach_data),
2085ee3dc7aSYi Liu         .flags = 0,
2095ee3dc7aSYi Liu     };
2105ee3dc7aSYi Liu 
21145d0d8c4SZhenzhong Duan     if (ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_data)) {
2125ee3dc7aSYi Liu         error_setg_errno(errp, errno, "detach %s failed", vbasedev->name);
21345d0d8c4SZhenzhong Duan         return false;
2145ee3dc7aSYi Liu     }
2155ee3dc7aSYi Liu 
21645d0d8c4SZhenzhong Duan     trace_iommufd_cdev_detach_ioas_hwpt(iommufd, vbasedev->name);
21745d0d8c4SZhenzhong Duan     return true;
21845d0d8c4SZhenzhong Duan }
21945d0d8c4SZhenzhong Duan 
2205b1e96e6SJoao Martins static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
2215b1e96e6SJoao Martins                                          VFIOIOMMUFDContainer *container,
2225b1e96e6SJoao Martins                                          Error **errp)
2235b1e96e6SJoao Martins {
2245b1e96e6SJoao Martins     ERRP_GUARD();
2255b1e96e6SJoao Martins     IOMMUFDBackend *iommufd = vbasedev->iommufd;
2265b1e96e6SJoao Martins     uint32_t flags = 0;
2275b1e96e6SJoao Martins     VFIOIOASHwpt *hwpt;
2285b1e96e6SJoao Martins     uint32_t hwpt_id;
2295b1e96e6SJoao Martins     int ret;
2305b1e96e6SJoao Martins 
2315b1e96e6SJoao Martins     /* Try to find a domain */
2325b1e96e6SJoao Martins     QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
2335b1e96e6SJoao Martins         ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
2345b1e96e6SJoao Martins         if (ret) {
2355b1e96e6SJoao Martins             /* -EINVAL means the domain is incompatible with the device. */
2365b1e96e6SJoao Martins             if (ret == -EINVAL) {
2375b1e96e6SJoao Martins                 /*
2385b1e96e6SJoao Martins                  * It is an expected failure and it just means we will try
2395b1e96e6SJoao Martins                  * another domain, or create one if no existing compatible
2405b1e96e6SJoao Martins                  * domain is found. Hence why the error is discarded below.
2415b1e96e6SJoao Martins                  */
2425b1e96e6SJoao Martins                 error_free(*errp);
2435b1e96e6SJoao Martins                 *errp = NULL;
2445b1e96e6SJoao Martins                 continue;
2455b1e96e6SJoao Martins             }
2465b1e96e6SJoao Martins 
2475b1e96e6SJoao Martins             return false;
2485b1e96e6SJoao Martins         } else {
2495b1e96e6SJoao Martins             vbasedev->hwpt = hwpt;
2505b1e96e6SJoao Martins             QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next);
251*dddfd8d6SJoao Martins             vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt);
2525b1e96e6SJoao Martins             return true;
2535b1e96e6SJoao Martins         }
2545b1e96e6SJoao Martins     }
2555b1e96e6SJoao Martins 
256*dddfd8d6SJoao Martins     /*
257*dddfd8d6SJoao Martins      * This is quite early and VFIO Migration state isn't yet fully
258*dddfd8d6SJoao Martins      * initialized, thus rely only on IOMMU hardware capabilities as to
259*dddfd8d6SJoao Martins      * whether IOMMU dirty tracking is going to be requested. Later
260*dddfd8d6SJoao Martins      * vfio_migration_realize() may decide to use VF dirty tracking
261*dddfd8d6SJoao Martins      * instead.
262*dddfd8d6SJoao Martins      */
263*dddfd8d6SJoao Martins     if (vbasedev->hiod->caps.hw_caps & IOMMU_HW_CAP_DIRTY_TRACKING) {
264*dddfd8d6SJoao Martins         flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
265*dddfd8d6SJoao Martins     }
266*dddfd8d6SJoao Martins 
2675b1e96e6SJoao Martins     if (!iommufd_backend_alloc_hwpt(iommufd, vbasedev->devid,
2685b1e96e6SJoao Martins                                     container->ioas_id, flags,
2695b1e96e6SJoao Martins                                     IOMMU_HWPT_DATA_NONE, 0, NULL,
2705b1e96e6SJoao Martins                                     &hwpt_id, errp)) {
2715b1e96e6SJoao Martins         return false;
2725b1e96e6SJoao Martins     }
2735b1e96e6SJoao Martins 
2745b1e96e6SJoao Martins     hwpt = g_malloc0(sizeof(*hwpt));
2755b1e96e6SJoao Martins     hwpt->hwpt_id = hwpt_id;
276*dddfd8d6SJoao Martins     hwpt->hwpt_flags = flags;
2775b1e96e6SJoao Martins     QLIST_INIT(&hwpt->device_list);
2785b1e96e6SJoao Martins 
2795b1e96e6SJoao Martins     ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
2805b1e96e6SJoao Martins     if (ret) {
2815b1e96e6SJoao Martins         iommufd_backend_free_id(container->be, hwpt->hwpt_id);
2825b1e96e6SJoao Martins         g_free(hwpt);
2835b1e96e6SJoao Martins         return false;
2845b1e96e6SJoao Martins     }
2855b1e96e6SJoao Martins 
2865b1e96e6SJoao Martins     vbasedev->hwpt = hwpt;
287*dddfd8d6SJoao Martins     vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt);
2885b1e96e6SJoao Martins     QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next);
2895b1e96e6SJoao Martins     QLIST_INSERT_HEAD(&container->hwpt_list, hwpt, next);
290*dddfd8d6SJoao Martins     container->bcontainer.dirty_pages_supported |=
291*dddfd8d6SJoao Martins                                 vbasedev->iommu_dirty_tracking;
292*dddfd8d6SJoao Martins     if (container->bcontainer.dirty_pages_supported &&
293*dddfd8d6SJoao Martins         !vbasedev->iommu_dirty_tracking) {
294*dddfd8d6SJoao Martins         warn_report("IOMMU instance for device %s doesn't support dirty tracking",
295*dddfd8d6SJoao Martins                     vbasedev->name);
296*dddfd8d6SJoao Martins     }
2975b1e96e6SJoao Martins     return true;
2985b1e96e6SJoao Martins }
2995b1e96e6SJoao Martins 
3005b1e96e6SJoao Martins static void iommufd_cdev_autodomains_put(VFIODevice *vbasedev,
3015b1e96e6SJoao Martins                                          VFIOIOMMUFDContainer *container)
3025b1e96e6SJoao Martins {
3035b1e96e6SJoao Martins     VFIOIOASHwpt *hwpt = vbasedev->hwpt;
3045b1e96e6SJoao Martins 
3055b1e96e6SJoao Martins     QLIST_REMOVE(vbasedev, hwpt_next);
3065b1e96e6SJoao Martins     vbasedev->hwpt = NULL;
3075b1e96e6SJoao Martins 
3085b1e96e6SJoao Martins     if (QLIST_EMPTY(&hwpt->device_list)) {
3095b1e96e6SJoao Martins         QLIST_REMOVE(hwpt, next);
3105b1e96e6SJoao Martins         iommufd_backend_free_id(container->be, hwpt->hwpt_id);
3115b1e96e6SJoao Martins         g_free(hwpt);
3125b1e96e6SJoao Martins     }
3135b1e96e6SJoao Martins }
3145b1e96e6SJoao Martins 
31545d0d8c4SZhenzhong Duan static bool iommufd_cdev_attach_container(VFIODevice *vbasedev,
3165ee3dc7aSYi Liu                                           VFIOIOMMUFDContainer *container,
3175ee3dc7aSYi Liu                                           Error **errp)
3185ee3dc7aSYi Liu {
3195b1e96e6SJoao Martins     /* mdevs aren't physical devices and will fail with auto domains */
3205b1e96e6SJoao Martins     if (!vbasedev->mdev) {
3215b1e96e6SJoao Martins         return iommufd_cdev_autodomains_get(vbasedev, container, errp);
3225b1e96e6SJoao Martins     }
3235b1e96e6SJoao Martins 
324b07dcb7dSJoao Martins     return !iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp);
3255ee3dc7aSYi Liu }
3265ee3dc7aSYi Liu 
3275ee3dc7aSYi Liu static void iommufd_cdev_detach_container(VFIODevice *vbasedev,
3285ee3dc7aSYi Liu                                           VFIOIOMMUFDContainer *container)
3295ee3dc7aSYi Liu {
3305ee3dc7aSYi Liu     Error *err = NULL;
3315ee3dc7aSYi Liu 
33245d0d8c4SZhenzhong Duan     if (!iommufd_cdev_detach_ioas_hwpt(vbasedev, &err)) {
3335ee3dc7aSYi Liu         error_report_err(err);
3345ee3dc7aSYi Liu     }
3355b1e96e6SJoao Martins 
3365b1e96e6SJoao Martins     if (vbasedev->hwpt) {
3375b1e96e6SJoao Martins         iommufd_cdev_autodomains_put(vbasedev, container);
3385b1e96e6SJoao Martins     }
3395b1e96e6SJoao Martins 
3405ee3dc7aSYi Liu }
3415ee3dc7aSYi Liu 
3425ee3dc7aSYi Liu static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container)
3435ee3dc7aSYi Liu {
3445ee3dc7aSYi Liu     VFIOContainerBase *bcontainer = &container->bcontainer;
3455ee3dc7aSYi Liu 
3465ee3dc7aSYi Liu     if (!QLIST_EMPTY(&bcontainer->device_list)) {
3475ee3dc7aSYi Liu         return;
3485ee3dc7aSYi Liu     }
3495ee3dc7aSYi Liu     memory_listener_unregister(&bcontainer->listener);
3505ee3dc7aSYi Liu     iommufd_backend_free_id(container->be, container->ioas_id);
35193802605SCédric Le Goater     object_unref(container);
3525ee3dc7aSYi Liu }
3535ee3dc7aSYi Liu 
3545ee3dc7aSYi Liu static int iommufd_cdev_ram_block_discard_disable(bool state)
3555ee3dc7aSYi Liu {
3565ee3dc7aSYi Liu     /*
3575ee3dc7aSYi Liu      * We support coordinated discarding of RAM via the RamDiscardManager.
3585ee3dc7aSYi Liu      */
3595ee3dc7aSYi Liu     return ram_block_uncoordinated_discard_disable(state);
3605ee3dc7aSYi Liu }
3615ee3dc7aSYi Liu 
36245d0d8c4SZhenzhong Duan static bool iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container,
363714e9affSZhenzhong Duan                                              uint32_t ioas_id, Error **errp)
364714e9affSZhenzhong Duan {
365714e9affSZhenzhong Duan     VFIOContainerBase *bcontainer = &container->bcontainer;
366f3758413SZhenzhong Duan     g_autofree struct iommu_ioas_iova_ranges *info = NULL;
367714e9affSZhenzhong Duan     struct iommu_iova_range *iova_ranges;
36845d0d8c4SZhenzhong Duan     int sz, fd = container->be->fd;
369714e9affSZhenzhong Duan 
370714e9affSZhenzhong Duan     info = g_malloc0(sizeof(*info));
371714e9affSZhenzhong Duan     info->size = sizeof(*info);
372714e9affSZhenzhong Duan     info->ioas_id = ioas_id;
373714e9affSZhenzhong Duan 
37445d0d8c4SZhenzhong Duan     if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info) && errno != EMSGSIZE) {
375714e9affSZhenzhong Duan         goto error;
376714e9affSZhenzhong Duan     }
377714e9affSZhenzhong Duan 
378714e9affSZhenzhong Duan     sz = info->num_iovas * sizeof(struct iommu_iova_range);
379714e9affSZhenzhong Duan     info = g_realloc(info, sizeof(*info) + sz);
380714e9affSZhenzhong Duan     info->allowed_iovas = (uintptr_t)(info + 1);
381714e9affSZhenzhong Duan 
38245d0d8c4SZhenzhong Duan     if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info)) {
383714e9affSZhenzhong Duan         goto error;
384714e9affSZhenzhong Duan     }
385714e9affSZhenzhong Duan 
386714e9affSZhenzhong Duan     iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas;
387714e9affSZhenzhong Duan 
388714e9affSZhenzhong Duan     for (int i = 0; i < info->num_iovas; i++) {
389714e9affSZhenzhong Duan         Range *range = g_new(Range, 1);
390714e9affSZhenzhong Duan 
391714e9affSZhenzhong Duan         range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last);
392714e9affSZhenzhong Duan         bcontainer->iova_ranges =
393714e9affSZhenzhong Duan             range_list_insert(bcontainer->iova_ranges, range);
394714e9affSZhenzhong Duan     }
395714e9affSZhenzhong Duan     bcontainer->pgsizes = info->out_iova_alignment;
396714e9affSZhenzhong Duan 
39745d0d8c4SZhenzhong Duan     return true;
398714e9affSZhenzhong Duan 
399714e9affSZhenzhong Duan error:
400714e9affSZhenzhong Duan     error_setg_errno(errp, errno, "Cannot get IOVA ranges");
40145d0d8c4SZhenzhong Duan     return false;
402714e9affSZhenzhong Duan }
403714e9affSZhenzhong Duan 
404b7754835SZhenzhong Duan static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
4055ee3dc7aSYi Liu                                 AddressSpace *as, Error **errp)
4065ee3dc7aSYi Liu {
4075ee3dc7aSYi Liu     VFIOContainerBase *bcontainer;
4085ee3dc7aSYi Liu     VFIOIOMMUFDContainer *container;
4095ee3dc7aSYi Liu     VFIOAddressSpace *space;
4105ee3dc7aSYi Liu     struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
4115ee3dc7aSYi Liu     int ret, devfd;
4125ee3dc7aSYi Liu     uint32_t ioas_id;
4135ee3dc7aSYi Liu     Error *err = NULL;
414ce5f6d49SCédric Le Goater     const VFIOIOMMUClass *iommufd_vioc =
415ce5f6d49SCédric Le Goater         VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
4165ee3dc7aSYi Liu 
417da3e04b2SZhenzhong Duan     if (vbasedev->fd < 0) {
4185ee3dc7aSYi Liu         devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp);
4195ee3dc7aSYi Liu         if (devfd < 0) {
420b7754835SZhenzhong Duan             return false;
4215ee3dc7aSYi Liu         }
4225ee3dc7aSYi Liu         vbasedev->fd = devfd;
423da3e04b2SZhenzhong Duan     } else {
424da3e04b2SZhenzhong Duan         devfd = vbasedev->fd;
425da3e04b2SZhenzhong Duan     }
4265ee3dc7aSYi Liu 
42745d0d8c4SZhenzhong Duan     if (!iommufd_cdev_connect_and_bind(vbasedev, errp)) {
4285ee3dc7aSYi Liu         goto err_connect_bind;
4295ee3dc7aSYi Liu     }
4305ee3dc7aSYi Liu 
4315ee3dc7aSYi Liu     space = vfio_get_address_space(as);
4325ee3dc7aSYi Liu 
43383a4d596SJoao Martins     /*
43483a4d596SJoao Martins      * The HostIOMMUDevice data from legacy backend is static and doesn't need
43583a4d596SJoao Martins      * any information from the (type1-iommu) backend to be initialized. In
43683a4d596SJoao Martins      * contrast however, the IOMMUFD HostIOMMUDevice data requires the iommufd
43783a4d596SJoao Martins      * FD to be connected and having a devid to be able to successfully call
43883a4d596SJoao Martins      * iommufd_backend_get_device_info().
43983a4d596SJoao Martins      */
44083a4d596SJoao Martins     if (!vfio_device_hiod_realize(vbasedev, errp)) {
44183a4d596SJoao Martins         goto err_alloc_ioas;
44283a4d596SJoao Martins     }
44383a4d596SJoao Martins 
4445ee3dc7aSYi Liu     /* try to attach to an existing container in this space */
4455ee3dc7aSYi Liu     QLIST_FOREACH(bcontainer, &space->containers, next) {
4465ee3dc7aSYi Liu         container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
44741d698b8SCédric Le Goater         if (VFIO_IOMMU_GET_CLASS(bcontainer) != iommufd_vioc ||
4485ee3dc7aSYi Liu             vbasedev->iommufd != container->be) {
4495ee3dc7aSYi Liu             continue;
4505ee3dc7aSYi Liu         }
45145d0d8c4SZhenzhong Duan         if (!iommufd_cdev_attach_container(vbasedev, container, &err)) {
4525ee3dc7aSYi Liu             const char *msg = error_get_pretty(err);
4535ee3dc7aSYi Liu 
4545ee3dc7aSYi Liu             trace_iommufd_cdev_fail_attach_existing_container(msg);
4555ee3dc7aSYi Liu             error_free(err);
4565ee3dc7aSYi Liu             err = NULL;
4575ee3dc7aSYi Liu         } else {
4585ee3dc7aSYi Liu             ret = iommufd_cdev_ram_block_discard_disable(true);
4595ee3dc7aSYi Liu             if (ret) {
4605ee3dc7aSYi Liu                 error_setg(errp,
4615ee3dc7aSYi Liu                               "Cannot set discarding of RAM broken (%d)", ret);
4625ee3dc7aSYi Liu                 goto err_discard_disable;
4635ee3dc7aSYi Liu             }
4645ee3dc7aSYi Liu             goto found_container;
4655ee3dc7aSYi Liu         }
4665ee3dc7aSYi Liu     }
4675ee3dc7aSYi Liu 
4685ee3dc7aSYi Liu     /* Need to allocate a new dedicated container */
4699067d50dSZhenzhong Duan     if (!iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp)) {
4705ee3dc7aSYi Liu         goto err_alloc_ioas;
4715ee3dc7aSYi Liu     }
4725ee3dc7aSYi Liu 
4735ee3dc7aSYi Liu     trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id);
4745ee3dc7aSYi Liu 
47593802605SCédric Le Goater     container = VFIO_IOMMU_IOMMUFD(object_new(TYPE_VFIO_IOMMU_IOMMUFD));
4765ee3dc7aSYi Liu     container->be = vbasedev->iommufd;
4775ee3dc7aSYi Liu     container->ioas_id = ioas_id;
4785b1e96e6SJoao Martins     QLIST_INIT(&container->hwpt_list);
4795ee3dc7aSYi Liu 
4805ee3dc7aSYi Liu     bcontainer = &container->bcontainer;
481b7b79588SCédric Le Goater     vfio_address_space_insert(space, bcontainer);
4825ee3dc7aSYi Liu 
48345d0d8c4SZhenzhong Duan     if (!iommufd_cdev_attach_container(vbasedev, container, errp)) {
4845ee3dc7aSYi Liu         goto err_attach_container;
4855ee3dc7aSYi Liu     }
4865ee3dc7aSYi Liu 
4875ee3dc7aSYi Liu     ret = iommufd_cdev_ram_block_discard_disable(true);
4885ee3dc7aSYi Liu     if (ret) {
4895ee3dc7aSYi Liu         goto err_discard_disable;
4905ee3dc7aSYi Liu     }
4915ee3dc7aSYi Liu 
49245d0d8c4SZhenzhong Duan     if (!iommufd_cdev_get_info_iova_range(container, ioas_id, &err)) {
493714e9affSZhenzhong Duan         error_append_hint(&err,
494714e9affSZhenzhong Duan                    "Fallback to default 64bit IOVA range and 4K page size\n");
495714e9affSZhenzhong Duan         warn_report_err(err);
496714e9affSZhenzhong Duan         err = NULL;
4975ee3dc7aSYi Liu         bcontainer->pgsizes = qemu_real_host_page_size();
498714e9affSZhenzhong Duan     }
4995ee3dc7aSYi Liu 
5005ee3dc7aSYi Liu     bcontainer->listener = vfio_memory_listener;
5015ee3dc7aSYi Liu     memory_listener_register(&bcontainer->listener, bcontainer->space->as);
5025ee3dc7aSYi Liu 
5035ee3dc7aSYi Liu     if (bcontainer->error) {
5045ee3dc7aSYi Liu         error_propagate_prepend(errp, bcontainer->error,
5055ee3dc7aSYi Liu                                 "memory listener initialization failed: ");
5065ee3dc7aSYi Liu         goto err_listener_register;
5075ee3dc7aSYi Liu     }
5085ee3dc7aSYi Liu 
5095ee3dc7aSYi Liu     bcontainer->initialized = true;
5105ee3dc7aSYi Liu 
5115ee3dc7aSYi Liu found_container:
5125ee3dc7aSYi Liu     ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info);
5135ee3dc7aSYi Liu     if (ret) {
5145ee3dc7aSYi Liu         error_setg_errno(errp, errno, "error getting device info");
5155ee3dc7aSYi Liu         goto err_listener_register;
5165ee3dc7aSYi Liu     }
5175ee3dc7aSYi Liu 
518f38f5dd1SZhenzhong Duan     if (!vfio_cpr_register_container(bcontainer, errp)) {
519d9fa4223SSteve Sistare         goto err_listener_register;
520d9fa4223SSteve Sistare     }
521d9fa4223SSteve Sistare 
5225ee3dc7aSYi Liu     /*
5235ee3dc7aSYi Liu      * TODO: examine RAM_BLOCK_DISCARD stuff, should we do group level
5245ee3dc7aSYi Liu      * for discarding incompatibility check as well?
5255ee3dc7aSYi Liu      */
5265ee3dc7aSYi Liu     if (vbasedev->ram_block_discard_allowed) {
5275ee3dc7aSYi Liu         iommufd_cdev_ram_block_discard_disable(false);
5285ee3dc7aSYi Liu     }
5295ee3dc7aSYi Liu 
5305ee3dc7aSYi Liu     vbasedev->group = 0;
5315ee3dc7aSYi Liu     vbasedev->num_irqs = dev_info.num_irqs;
5325ee3dc7aSYi Liu     vbasedev->num_regions = dev_info.num_regions;
5335ee3dc7aSYi Liu     vbasedev->flags = dev_info.flags;
5345ee3dc7aSYi Liu     vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
5355ee3dc7aSYi Liu     vbasedev->bcontainer = bcontainer;
5365ee3dc7aSYi Liu     QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
5375ee3dc7aSYi Liu     QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
5385ee3dc7aSYi Liu 
5395ee3dc7aSYi Liu     trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
5405ee3dc7aSYi Liu                                    vbasedev->num_regions, vbasedev->flags);
541b7754835SZhenzhong Duan     return true;
5425ee3dc7aSYi Liu 
5435ee3dc7aSYi Liu err_listener_register:
5445ee3dc7aSYi Liu     iommufd_cdev_ram_block_discard_disable(false);
5455ee3dc7aSYi Liu err_discard_disable:
5465ee3dc7aSYi Liu     iommufd_cdev_detach_container(vbasedev, container);
5475ee3dc7aSYi Liu err_attach_container:
5485ee3dc7aSYi Liu     iommufd_cdev_container_destroy(container);
5495ee3dc7aSYi Liu err_alloc_ioas:
5505ee3dc7aSYi Liu     vfio_put_address_space(space);
5515ee3dc7aSYi Liu     iommufd_cdev_unbind_and_disconnect(vbasedev);
5525ee3dc7aSYi Liu err_connect_bind:
5535ee3dc7aSYi Liu     close(vbasedev->fd);
554b7754835SZhenzhong Duan     return false;
5555ee3dc7aSYi Liu }
5565ee3dc7aSYi Liu 
5575ee3dc7aSYi Liu static void iommufd_cdev_detach(VFIODevice *vbasedev)
5585ee3dc7aSYi Liu {
5595ee3dc7aSYi Liu     VFIOContainerBase *bcontainer = vbasedev->bcontainer;
5605ee3dc7aSYi Liu     VFIOAddressSpace *space = bcontainer->space;
5615ee3dc7aSYi Liu     VFIOIOMMUFDContainer *container = container_of(bcontainer,
5625ee3dc7aSYi Liu                                                    VFIOIOMMUFDContainer,
5635ee3dc7aSYi Liu                                                    bcontainer);
5645ee3dc7aSYi Liu     QLIST_REMOVE(vbasedev, global_next);
5655ee3dc7aSYi Liu     QLIST_REMOVE(vbasedev, container_next);
5665ee3dc7aSYi Liu     vbasedev->bcontainer = NULL;
5675ee3dc7aSYi Liu 
5685ee3dc7aSYi Liu     if (!vbasedev->ram_block_discard_allowed) {
5695ee3dc7aSYi Liu         iommufd_cdev_ram_block_discard_disable(false);
5705ee3dc7aSYi Liu     }
5715ee3dc7aSYi Liu 
572d9fa4223SSteve Sistare     vfio_cpr_unregister_container(bcontainer);
5735ee3dc7aSYi Liu     iommufd_cdev_detach_container(vbasedev, container);
5745ee3dc7aSYi Liu     iommufd_cdev_container_destroy(container);
5755ee3dc7aSYi Liu     vfio_put_address_space(space);
5765ee3dc7aSYi Liu 
5775ee3dc7aSYi Liu     iommufd_cdev_unbind_and_disconnect(vbasedev);
5785ee3dc7aSYi Liu     close(vbasedev->fd);
5795ee3dc7aSYi Liu }
5805ee3dc7aSYi Liu 
58196d6f85fSZhenzhong Duan static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid)
58296d6f85fSZhenzhong Duan {
58396d6f85fSZhenzhong Duan     VFIODevice *vbasedev_iter;
584ce5f6d49SCédric Le Goater     const VFIOIOMMUClass *iommufd_vioc =
585ce5f6d49SCédric Le Goater         VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
58696d6f85fSZhenzhong Duan 
58796d6f85fSZhenzhong Duan     QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) {
58841d698b8SCédric Le Goater         if (VFIO_IOMMU_GET_CLASS(vbasedev_iter->bcontainer) != iommufd_vioc) {
58996d6f85fSZhenzhong Duan             continue;
59096d6f85fSZhenzhong Duan         }
59196d6f85fSZhenzhong Duan         if (devid == vbasedev_iter->devid) {
59296d6f85fSZhenzhong Duan             return vbasedev_iter;
59396d6f85fSZhenzhong Duan         }
59496d6f85fSZhenzhong Duan     }
59596d6f85fSZhenzhong Duan     return NULL;
59696d6f85fSZhenzhong Duan }
59796d6f85fSZhenzhong Duan 
59896d6f85fSZhenzhong Duan static VFIOPCIDevice *
59996d6f85fSZhenzhong Duan iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev,
60096d6f85fSZhenzhong Duan                                     VFIODevice *reset_dev)
60196d6f85fSZhenzhong Duan {
60296d6f85fSZhenzhong Duan     VFIODevice *vbasedev_tmp;
60396d6f85fSZhenzhong Duan 
60496d6f85fSZhenzhong Duan     if (dep_dev->devid == reset_dev->devid ||
60596d6f85fSZhenzhong Duan         dep_dev->devid == VFIO_PCI_DEVID_OWNED) {
60696d6f85fSZhenzhong Duan         return NULL;
60796d6f85fSZhenzhong Duan     }
60896d6f85fSZhenzhong Duan 
60996d6f85fSZhenzhong Duan     vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid);
61096d6f85fSZhenzhong Duan     if (!vbasedev_tmp || !vbasedev_tmp->dev->realized ||
61196d6f85fSZhenzhong Duan         vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) {
61296d6f85fSZhenzhong Duan         return NULL;
61396d6f85fSZhenzhong Duan     }
61496d6f85fSZhenzhong Duan 
61596d6f85fSZhenzhong Duan     return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev);
61696d6f85fSZhenzhong Duan }
61796d6f85fSZhenzhong Duan 
61896d6f85fSZhenzhong Duan static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single)
61996d6f85fSZhenzhong Duan {
62096d6f85fSZhenzhong Duan     VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
62196d6f85fSZhenzhong Duan     struct vfio_pci_hot_reset_info *info = NULL;
62296d6f85fSZhenzhong Duan     struct vfio_pci_dependent_device *devices;
62396d6f85fSZhenzhong Duan     struct vfio_pci_hot_reset *reset;
62496d6f85fSZhenzhong Duan     int ret, i;
62596d6f85fSZhenzhong Duan     bool multi = false;
62696d6f85fSZhenzhong Duan 
62796d6f85fSZhenzhong Duan     trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi");
62896d6f85fSZhenzhong Duan 
62996d6f85fSZhenzhong Duan     if (!single) {
63096d6f85fSZhenzhong Duan         vfio_pci_pre_reset(vdev);
63196d6f85fSZhenzhong Duan     }
63296d6f85fSZhenzhong Duan     vdev->vbasedev.needs_reset = false;
63396d6f85fSZhenzhong Duan 
63496d6f85fSZhenzhong Duan     ret = vfio_pci_get_pci_hot_reset_info(vdev, &info);
63596d6f85fSZhenzhong Duan 
63696d6f85fSZhenzhong Duan     if (ret) {
63796d6f85fSZhenzhong Duan         goto out_single;
63896d6f85fSZhenzhong Duan     }
63996d6f85fSZhenzhong Duan 
64096d6f85fSZhenzhong Duan     assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID);
64196d6f85fSZhenzhong Duan 
64296d6f85fSZhenzhong Duan     devices = &info->devices[0];
64396d6f85fSZhenzhong Duan 
64496d6f85fSZhenzhong Duan     if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) {
64596d6f85fSZhenzhong Duan         if (!vdev->has_pm_reset) {
64696d6f85fSZhenzhong Duan             for (i = 0; i < info->count; i++) {
64796d6f85fSZhenzhong Duan                 if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) {
64896d6f85fSZhenzhong Duan                     error_report("vfio: Cannot reset device %s, "
64996d6f85fSZhenzhong Duan                                  "depends on device %04x:%02x:%02x.%x "
65096d6f85fSZhenzhong Duan                                  "which is not owned.",
65196d6f85fSZhenzhong Duan                                  vdev->vbasedev.name, devices[i].segment,
65296d6f85fSZhenzhong Duan                                  devices[i].bus, PCI_SLOT(devices[i].devfn),
65396d6f85fSZhenzhong Duan                                  PCI_FUNC(devices[i].devfn));
65496d6f85fSZhenzhong Duan                 }
65596d6f85fSZhenzhong Duan             }
65696d6f85fSZhenzhong Duan         }
65796d6f85fSZhenzhong Duan         ret = -EPERM;
65896d6f85fSZhenzhong Duan         goto out_single;
65996d6f85fSZhenzhong Duan     }
66096d6f85fSZhenzhong Duan 
66196d6f85fSZhenzhong Duan     trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name);
66296d6f85fSZhenzhong Duan 
66396d6f85fSZhenzhong Duan     for (i = 0; i < info->count; i++) {
66496d6f85fSZhenzhong Duan         VFIOPCIDevice *tmp;
66596d6f85fSZhenzhong Duan 
66696d6f85fSZhenzhong Duan         trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment,
66796d6f85fSZhenzhong Duan                                                      devices[i].bus,
66896d6f85fSZhenzhong Duan                                                      PCI_SLOT(devices[i].devfn),
66996d6f85fSZhenzhong Duan                                                      PCI_FUNC(devices[i].devfn),
67096d6f85fSZhenzhong Duan                                                      devices[i].devid);
67196d6f85fSZhenzhong Duan 
67296d6f85fSZhenzhong Duan         /*
67396d6f85fSZhenzhong Duan          * If a VFIO cdev device is resettable, all the dependent devices
67496d6f85fSZhenzhong Duan          * are either bound to same iommufd or within same iommu_groups as
67596d6f85fSZhenzhong Duan          * one of the iommufd bound devices.
67696d6f85fSZhenzhong Duan          */
67796d6f85fSZhenzhong Duan         assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED);
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 
68496d6f85fSZhenzhong Duan         if (single) {
68596d6f85fSZhenzhong Duan             ret = -EINVAL;
68696d6f85fSZhenzhong Duan             goto out_single;
68796d6f85fSZhenzhong Duan         }
68896d6f85fSZhenzhong Duan         vfio_pci_pre_reset(tmp);
68996d6f85fSZhenzhong Duan         tmp->vbasedev.needs_reset = false;
69096d6f85fSZhenzhong Duan         multi = true;
69196d6f85fSZhenzhong Duan     }
69296d6f85fSZhenzhong Duan 
69396d6f85fSZhenzhong Duan     if (!single && !multi) {
69496d6f85fSZhenzhong Duan         ret = -EINVAL;
69596d6f85fSZhenzhong Duan         goto out_single;
69696d6f85fSZhenzhong Duan     }
69796d6f85fSZhenzhong Duan 
69896d6f85fSZhenzhong Duan     /* Use zero length array for hot reset with iommufd backend */
69996d6f85fSZhenzhong Duan     reset = g_malloc0(sizeof(*reset));
70096d6f85fSZhenzhong Duan     reset->argsz = sizeof(*reset);
70196d6f85fSZhenzhong Duan 
70296d6f85fSZhenzhong Duan      /* Bus reset! */
70396d6f85fSZhenzhong Duan     ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset);
70496d6f85fSZhenzhong Duan     g_free(reset);
70596d6f85fSZhenzhong Duan     if (ret) {
70696d6f85fSZhenzhong Duan         ret = -errno;
70796d6f85fSZhenzhong Duan     }
70896d6f85fSZhenzhong Duan 
70996d6f85fSZhenzhong Duan     trace_vfio_pci_hot_reset_result(vdev->vbasedev.name,
71096d6f85fSZhenzhong Duan                                     ret ? strerror(errno) : "Success");
71196d6f85fSZhenzhong Duan 
71296d6f85fSZhenzhong Duan     /* Re-enable INTx on affected devices */
71396d6f85fSZhenzhong Duan     for (i = 0; i < info->count; i++) {
71496d6f85fSZhenzhong Duan         VFIOPCIDevice *tmp;
71596d6f85fSZhenzhong Duan 
71696d6f85fSZhenzhong Duan         tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
71796d6f85fSZhenzhong Duan         if (!tmp) {
71896d6f85fSZhenzhong Duan             continue;
71996d6f85fSZhenzhong Duan         }
72096d6f85fSZhenzhong Duan         vfio_pci_post_reset(tmp);
72196d6f85fSZhenzhong Duan     }
72296d6f85fSZhenzhong Duan out_single:
72396d6f85fSZhenzhong Duan     if (!single) {
72496d6f85fSZhenzhong Duan         vfio_pci_post_reset(vdev);
72596d6f85fSZhenzhong Duan     }
72696d6f85fSZhenzhong Duan     g_free(info);
72796d6f85fSZhenzhong Duan 
72896d6f85fSZhenzhong Duan     return ret;
72996d6f85fSZhenzhong Duan }
73096d6f85fSZhenzhong Duan 
731ce5f6d49SCédric Le Goater static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
732ce5f6d49SCédric Le Goater {
733ce5f6d49SCédric Le Goater     VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
734ce5f6d49SCédric Le Goater 
735a7fd91b8SZhenzhong Duan     vioc->hiod_typename = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO;
736a7fd91b8SZhenzhong Duan 
737ce5f6d49SCédric Le Goater     vioc->dma_map = iommufd_cdev_map;
738ce5f6d49SCédric Le Goater     vioc->dma_unmap = iommufd_cdev_unmap;
739ce5f6d49SCédric Le Goater     vioc->attach_device = iommufd_cdev_attach;
740ce5f6d49SCédric Le Goater     vioc->detach_device = iommufd_cdev_detach;
741ce5f6d49SCédric Le Goater     vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
7425ee3dc7aSYi Liu };
743ce5f6d49SCédric Le Goater 
74493058952SZhenzhong Duan static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
74593058952SZhenzhong Duan                                       Error **errp)
74693058952SZhenzhong Duan {
74793058952SZhenzhong Duan     VFIODevice *vdev = opaque;
74893058952SZhenzhong Duan     HostIOMMUDeviceCaps *caps = &hiod->caps;
74993058952SZhenzhong Duan     enum iommu_hw_info_type type;
75093058952SZhenzhong Duan     union {
75193058952SZhenzhong Duan         struct iommu_hw_info_vtd vtd;
75293058952SZhenzhong Duan     } data;
7532d1bf258SJoao Martins     uint64_t hw_caps;
75493058952SZhenzhong Duan 
755dc169694SEric Auger     hiod->agent = opaque;
756dc169694SEric Auger 
75793058952SZhenzhong Duan     if (!iommufd_backend_get_device_info(vdev->iommufd, vdev->devid,
7582d1bf258SJoao Martins                                          &type, &data, sizeof(data),
7592d1bf258SJoao Martins                                          &hw_caps, errp)) {
76093058952SZhenzhong Duan         return false;
76193058952SZhenzhong Duan     }
76293058952SZhenzhong Duan 
76393058952SZhenzhong Duan     hiod->name = g_strdup(vdev->name);
76493058952SZhenzhong Duan     caps->type = type;
76521e8d3a3SJoao Martins     caps->hw_caps = hw_caps;
76693058952SZhenzhong Duan 
76793058952SZhenzhong Duan     return true;
76893058952SZhenzhong Duan }
76993058952SZhenzhong Duan 
7703ad35d91SEric Auger static GList *
771d59ca1caSEric Auger hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod)
7723ad35d91SEric Auger {
7733ad35d91SEric Auger     VFIODevice *vdev = hiod->agent;
7743ad35d91SEric Auger 
7753ad35d91SEric Auger     g_assert(vdev);
7763966bca5SEric Auger     return vfio_container_get_iova_ranges(vdev->bcontainer);
7773ad35d91SEric Auger }
7783ad35d91SEric Auger 
7798fe0ebe1SEric Auger static uint64_t
7808fe0ebe1SEric Auger hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod)
7818fe0ebe1SEric Auger {
7828fe0ebe1SEric Auger     VFIODevice *vdev = hiod->agent;
7838fe0ebe1SEric Auger 
7848fe0ebe1SEric Auger     g_assert(vdev);
7858fe0ebe1SEric Auger     return vfio_container_get_page_size_mask(vdev->bcontainer);
7868fe0ebe1SEric Auger }
7878fe0ebe1SEric Auger 
7888fe0ebe1SEric Auger 
78993058952SZhenzhong Duan static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
79093058952SZhenzhong Duan {
79193058952SZhenzhong Duan     HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
79293058952SZhenzhong Duan 
79393058952SZhenzhong Duan     hiodc->realize = hiod_iommufd_vfio_realize;
7943ad35d91SEric Auger     hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
7958fe0ebe1SEric Auger     hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
79693058952SZhenzhong Duan };
79793058952SZhenzhong Duan 
798ce5f6d49SCédric Le Goater static const TypeInfo types[] = {
799ce5f6d49SCédric Le Goater     {
800ce5f6d49SCédric Le Goater         .name = TYPE_VFIO_IOMMU_IOMMUFD,
801ce5f6d49SCédric Le Goater         .parent = TYPE_VFIO_IOMMU,
802504d297eSCédric Le Goater         .instance_size = sizeof(VFIOIOMMUFDContainer),
803ce5f6d49SCédric Le Goater         .class_init = vfio_iommu_iommufd_class_init,
8049005f928SZhenzhong Duan     }, {
8059005f928SZhenzhong Duan         .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO,
8069005f928SZhenzhong Duan         .parent = TYPE_HOST_IOMMU_DEVICE_IOMMUFD,
80793058952SZhenzhong Duan         .class_init = hiod_iommufd_vfio_class_init,
8089005f928SZhenzhong Duan     }
809ce5f6d49SCédric Le Goater };
810ce5f6d49SCédric Le Goater 
811ce5f6d49SCédric Le Goater DEFINE_TYPES(types)
812