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"
28*7c30710bSJoao Martins #include "exec/ram_addr.h"
295ee3dc7aSYi Liu
iommufd_cdev_map(const VFIOContainerBase * bcontainer,hwaddr iova,ram_addr_t size,void * vaddr,bool readonly)304517c33cSZhenzhong Duan static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
315ee3dc7aSYi Liu ram_addr_t size, void *vaddr, bool readonly)
325ee3dc7aSYi Liu {
334517c33cSZhenzhong Duan const VFIOIOMMUFDContainer *container =
345ee3dc7aSYi Liu container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
355ee3dc7aSYi Liu
365ee3dc7aSYi Liu return iommufd_backend_map_dma(container->be,
375ee3dc7aSYi Liu container->ioas_id,
385ee3dc7aSYi Liu iova, size, vaddr, readonly);
395ee3dc7aSYi Liu }
405ee3dc7aSYi Liu
iommufd_cdev_unmap(const VFIOContainerBase * bcontainer,hwaddr iova,ram_addr_t size,IOMMUTLBEntry * iotlb)414517c33cSZhenzhong Duan static int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer,
425ee3dc7aSYi Liu hwaddr iova, ram_addr_t size,
435ee3dc7aSYi Liu IOMMUTLBEntry *iotlb)
445ee3dc7aSYi Liu {
454517c33cSZhenzhong Duan const VFIOIOMMUFDContainer *container =
465ee3dc7aSYi Liu container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
475ee3dc7aSYi Liu
485ee3dc7aSYi Liu /* TODO: Handle dma_unmap_bitmap with iotlb args (migration) */
495ee3dc7aSYi Liu return iommufd_backend_unmap_dma(container->be,
505ee3dc7aSYi Liu container->ioas_id, iova, size);
515ee3dc7aSYi Liu }
525ee3dc7aSYi Liu
iommufd_cdev_kvm_device_add(VFIODevice * vbasedev,Error ** errp)5345d0d8c4SZhenzhong Duan static bool iommufd_cdev_kvm_device_add(VFIODevice *vbasedev, Error **errp)
545ee3dc7aSYi Liu {
5545d0d8c4SZhenzhong Duan return !vfio_kvm_device_add_fd(vbasedev->fd, errp);
565ee3dc7aSYi Liu }
575ee3dc7aSYi Liu
iommufd_cdev_kvm_device_del(VFIODevice * vbasedev)585ee3dc7aSYi Liu static void iommufd_cdev_kvm_device_del(VFIODevice *vbasedev)
595ee3dc7aSYi Liu {
605ee3dc7aSYi Liu Error *err = NULL;
615ee3dc7aSYi Liu
625ee3dc7aSYi Liu if (vfio_kvm_device_del_fd(vbasedev->fd, &err)) {
635ee3dc7aSYi Liu error_report_err(err);
645ee3dc7aSYi Liu }
655ee3dc7aSYi Liu }
665ee3dc7aSYi Liu
iommufd_cdev_connect_and_bind(VFIODevice * vbasedev,Error ** errp)6745d0d8c4SZhenzhong Duan static bool iommufd_cdev_connect_and_bind(VFIODevice *vbasedev, Error **errp)
685ee3dc7aSYi Liu {
695ee3dc7aSYi Liu IOMMUFDBackend *iommufd = vbasedev->iommufd;
705ee3dc7aSYi Liu struct vfio_device_bind_iommufd bind = {
715ee3dc7aSYi Liu .argsz = sizeof(bind),
725ee3dc7aSYi Liu .flags = 0,
735ee3dc7aSYi Liu };
745ee3dc7aSYi Liu
759067d50dSZhenzhong Duan if (!iommufd_backend_connect(iommufd, errp)) {
7645d0d8c4SZhenzhong Duan return false;
775ee3dc7aSYi Liu }
785ee3dc7aSYi Liu
795ee3dc7aSYi Liu /*
805ee3dc7aSYi Liu * Add device to kvm-vfio to be prepared for the tracking
815ee3dc7aSYi Liu * in KVM. Especially for some emulated devices, it requires
825ee3dc7aSYi Liu * to have kvm information in the device open.
835ee3dc7aSYi Liu */
8445d0d8c4SZhenzhong Duan if (!iommufd_cdev_kvm_device_add(vbasedev, errp)) {
855ee3dc7aSYi Liu goto err_kvm_device_add;
865ee3dc7aSYi Liu }
875ee3dc7aSYi Liu
885ee3dc7aSYi Liu /* Bind device to iommufd */
895ee3dc7aSYi Liu bind.iommufd = iommufd->fd;
9045d0d8c4SZhenzhong Duan if (ioctl(vbasedev->fd, VFIO_DEVICE_BIND_IOMMUFD, &bind)) {
915ee3dc7aSYi Liu error_setg_errno(errp, errno, "error bind device fd=%d to iommufd=%d",
925ee3dc7aSYi Liu vbasedev->fd, bind.iommufd);
935ee3dc7aSYi Liu goto err_bind;
945ee3dc7aSYi Liu }
955ee3dc7aSYi Liu
965ee3dc7aSYi Liu vbasedev->devid = bind.out_devid;
975ee3dc7aSYi Liu trace_iommufd_cdev_connect_and_bind(bind.iommufd, vbasedev->name,
985ee3dc7aSYi Liu vbasedev->fd, vbasedev->devid);
9945d0d8c4SZhenzhong Duan return true;
1005ee3dc7aSYi Liu err_bind:
1015ee3dc7aSYi Liu iommufd_cdev_kvm_device_del(vbasedev);
1025ee3dc7aSYi Liu err_kvm_device_add:
1035ee3dc7aSYi Liu iommufd_backend_disconnect(iommufd);
10445d0d8c4SZhenzhong Duan return false;
1055ee3dc7aSYi Liu }
1065ee3dc7aSYi Liu
iommufd_cdev_unbind_and_disconnect(VFIODevice * vbasedev)1075ee3dc7aSYi Liu static void iommufd_cdev_unbind_and_disconnect(VFIODevice *vbasedev)
1085ee3dc7aSYi Liu {
1095ee3dc7aSYi Liu /* Unbind is automatically conducted when device fd is closed */
1105ee3dc7aSYi Liu iommufd_cdev_kvm_device_del(vbasedev);
1115ee3dc7aSYi Liu iommufd_backend_disconnect(vbasedev->iommufd);
1125ee3dc7aSYi Liu }
1135ee3dc7aSYi Liu
iommufd_hwpt_dirty_tracking(VFIOIOASHwpt * hwpt)114dddfd8d6SJoao Martins static bool iommufd_hwpt_dirty_tracking(VFIOIOASHwpt *hwpt)
115dddfd8d6SJoao Martins {
116dddfd8d6SJoao Martins return hwpt && hwpt->hwpt_flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
117dddfd8d6SJoao Martins }
118dddfd8d6SJoao Martins
iommufd_set_dirty_page_tracking(const VFIOContainerBase * bcontainer,bool start,Error ** errp)11952ce8822SJoao Martins static int iommufd_set_dirty_page_tracking(const VFIOContainerBase *bcontainer,
12052ce8822SJoao Martins bool start, Error **errp)
12152ce8822SJoao Martins {
12252ce8822SJoao Martins const VFIOIOMMUFDContainer *container =
12352ce8822SJoao Martins container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
12452ce8822SJoao Martins VFIOIOASHwpt *hwpt;
12552ce8822SJoao Martins
12652ce8822SJoao Martins QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
12752ce8822SJoao Martins if (!iommufd_hwpt_dirty_tracking(hwpt)) {
12852ce8822SJoao Martins continue;
12952ce8822SJoao Martins }
13052ce8822SJoao Martins
13152ce8822SJoao Martins if (!iommufd_backend_set_dirty_tracking(container->be,
13252ce8822SJoao Martins hwpt->hwpt_id, start, errp)) {
13352ce8822SJoao Martins goto err;
13452ce8822SJoao Martins }
13552ce8822SJoao Martins }
13652ce8822SJoao Martins
13752ce8822SJoao Martins return 0;
13852ce8822SJoao Martins
13952ce8822SJoao Martins err:
14052ce8822SJoao Martins QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
14152ce8822SJoao Martins if (!iommufd_hwpt_dirty_tracking(hwpt)) {
14252ce8822SJoao Martins continue;
14352ce8822SJoao Martins }
14452ce8822SJoao Martins iommufd_backend_set_dirty_tracking(container->be,
14552ce8822SJoao Martins hwpt->hwpt_id, !start, NULL);
14652ce8822SJoao Martins }
14752ce8822SJoao Martins return -EINVAL;
14852ce8822SJoao Martins }
14952ce8822SJoao Martins
iommufd_query_dirty_bitmap(const VFIOContainerBase * bcontainer,VFIOBitmap * vbmap,hwaddr iova,hwaddr size,Error ** errp)150*7c30710bSJoao Martins static int iommufd_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
151*7c30710bSJoao Martins VFIOBitmap *vbmap, hwaddr iova,
152*7c30710bSJoao Martins hwaddr size, Error **errp)
153*7c30710bSJoao Martins {
154*7c30710bSJoao Martins VFIOIOMMUFDContainer *container = container_of(bcontainer,
155*7c30710bSJoao Martins VFIOIOMMUFDContainer,
156*7c30710bSJoao Martins bcontainer);
157*7c30710bSJoao Martins unsigned long page_size = qemu_real_host_page_size();
158*7c30710bSJoao Martins VFIOIOASHwpt *hwpt;
159*7c30710bSJoao Martins
160*7c30710bSJoao Martins QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
161*7c30710bSJoao Martins if (!iommufd_hwpt_dirty_tracking(hwpt)) {
162*7c30710bSJoao Martins continue;
163*7c30710bSJoao Martins }
164*7c30710bSJoao Martins
165*7c30710bSJoao Martins if (!iommufd_backend_get_dirty_bitmap(container->be, hwpt->hwpt_id,
166*7c30710bSJoao Martins iova, size, page_size,
167*7c30710bSJoao Martins (uint64_t *)vbmap->bitmap,
168*7c30710bSJoao Martins errp)) {
169*7c30710bSJoao Martins return -EINVAL;
170*7c30710bSJoao Martins }
171*7c30710bSJoao Martins }
172*7c30710bSJoao Martins
173*7c30710bSJoao Martins return 0;
174*7c30710bSJoao Martins }
175*7c30710bSJoao Martins
iommufd_cdev_getfd(const char * sysfs_path,Error ** errp)1765ee3dc7aSYi Liu static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
1775ee3dc7aSYi Liu {
1785cf8f51cSZhao Liu ERRP_GUARD();
1795ee3dc7aSYi Liu long int ret = -ENOTTY;
18057001144SCédric Le Goater g_autofree char *path = NULL;
18157001144SCédric Le Goater g_autofree char *vfio_dev_path = NULL;
18257001144SCédric Le Goater g_autofree char *vfio_path = NULL;
1835ee3dc7aSYi Liu DIR *dir = NULL;
1845ee3dc7aSYi Liu struct dirent *dent;
18557001144SCédric Le Goater g_autofree gchar *contents = NULL;
1865ee3dc7aSYi Liu gsize length;
1875ee3dc7aSYi Liu int major, minor;
1885ee3dc7aSYi Liu dev_t vfio_devt;
1895ee3dc7aSYi Liu
1905ee3dc7aSYi Liu path = g_strdup_printf("%s/vfio-dev", sysfs_path);
1915ee3dc7aSYi Liu dir = opendir(path);
1925ee3dc7aSYi Liu if (!dir) {
1935ee3dc7aSYi Liu error_setg_errno(errp, errno, "couldn't open directory %s", path);
19457001144SCédric Le Goater goto out;
1955ee3dc7aSYi Liu }
1965ee3dc7aSYi Liu
1975ee3dc7aSYi Liu while ((dent = readdir(dir))) {
1985ee3dc7aSYi Liu if (!strncmp(dent->d_name, "vfio", 4)) {
1995ee3dc7aSYi Liu vfio_dev_path = g_strdup_printf("%s/%s/dev", path, dent->d_name);
2005ee3dc7aSYi Liu break;
2015ee3dc7aSYi Liu }
2025ee3dc7aSYi Liu }
2035ee3dc7aSYi Liu
2045ee3dc7aSYi Liu if (!vfio_dev_path) {
2055ee3dc7aSYi Liu error_setg(errp, "failed to find vfio-dev/vfioX/dev");
2065ee3dc7aSYi Liu goto out_close_dir;
2075ee3dc7aSYi Liu }
2085ee3dc7aSYi Liu
2095ee3dc7aSYi Liu if (!g_file_get_contents(vfio_dev_path, &contents, &length, NULL)) {
2105ee3dc7aSYi Liu error_setg(errp, "failed to load \"%s\"", vfio_dev_path);
21157001144SCédric Le Goater goto out_close_dir;
2125ee3dc7aSYi Liu }
2135ee3dc7aSYi Liu
2145ee3dc7aSYi Liu if (sscanf(contents, "%d:%d", &major, &minor) != 2) {
2155ee3dc7aSYi Liu error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path);
21657001144SCédric Le Goater goto out_close_dir;
2175ee3dc7aSYi Liu }
2185ee3dc7aSYi Liu vfio_devt = makedev(major, minor);
2195ee3dc7aSYi Liu
2205ee3dc7aSYi Liu vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name);
2215ee3dc7aSYi Liu ret = open_cdev(vfio_path, vfio_devt);
2225ee3dc7aSYi Liu if (ret < 0) {
2235ee3dc7aSYi Liu error_setg(errp, "Failed to open %s", vfio_path);
2245ee3dc7aSYi Liu }
2255ee3dc7aSYi Liu
2265ee3dc7aSYi Liu trace_iommufd_cdev_getfd(vfio_path, ret);
2275ee3dc7aSYi Liu
2285ee3dc7aSYi Liu out_close_dir:
2295ee3dc7aSYi Liu closedir(dir);
23057001144SCédric Le Goater out:
2315ee3dc7aSYi Liu if (*errp) {
2325ee3dc7aSYi Liu error_prepend(errp, VFIO_MSG_PREFIX, path);
2335ee3dc7aSYi Liu }
2345ee3dc7aSYi Liu
2355ee3dc7aSYi Liu return ret;
2365ee3dc7aSYi Liu }
2375ee3dc7aSYi Liu
iommufd_cdev_attach_ioas_hwpt(VFIODevice * vbasedev,uint32_t id,Error ** errp)238b07dcb7dSJoao Martins static int iommufd_cdev_attach_ioas_hwpt(VFIODevice *vbasedev, uint32_t id,
2395ee3dc7aSYi Liu Error **errp)
2405ee3dc7aSYi Liu {
24145d0d8c4SZhenzhong Duan int iommufd = vbasedev->iommufd->fd;
2425ee3dc7aSYi Liu struct vfio_device_attach_iommufd_pt attach_data = {
2435ee3dc7aSYi Liu .argsz = sizeof(attach_data),
2445ee3dc7aSYi Liu .flags = 0,
2455ee3dc7aSYi Liu .pt_id = id,
2465ee3dc7aSYi Liu };
2475ee3dc7aSYi Liu
2485ee3dc7aSYi Liu /* Attach device to an IOAS or hwpt within iommufd */
24945d0d8c4SZhenzhong Duan if (ioctl(vbasedev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data)) {
2505ee3dc7aSYi Liu error_setg_errno(errp, errno,
2515ee3dc7aSYi Liu "[iommufd=%d] error attach %s (%d) to id=%d",
2525ee3dc7aSYi Liu iommufd, vbasedev->name, vbasedev->fd, id);
253b07dcb7dSJoao Martins return -errno;
2545ee3dc7aSYi Liu }
2555ee3dc7aSYi Liu
25645d0d8c4SZhenzhong Duan trace_iommufd_cdev_attach_ioas_hwpt(iommufd, vbasedev->name,
25745d0d8c4SZhenzhong Duan vbasedev->fd, id);
258b07dcb7dSJoao Martins return 0;
25945d0d8c4SZhenzhong Duan }
26045d0d8c4SZhenzhong Duan
iommufd_cdev_detach_ioas_hwpt(VFIODevice * vbasedev,Error ** errp)26145d0d8c4SZhenzhong Duan static bool iommufd_cdev_detach_ioas_hwpt(VFIODevice *vbasedev, Error **errp)
2625ee3dc7aSYi Liu {
26345d0d8c4SZhenzhong Duan int iommufd = vbasedev->iommufd->fd;
2645ee3dc7aSYi Liu struct vfio_device_detach_iommufd_pt detach_data = {
2655ee3dc7aSYi Liu .argsz = sizeof(detach_data),
2665ee3dc7aSYi Liu .flags = 0,
2675ee3dc7aSYi Liu };
2685ee3dc7aSYi Liu
26945d0d8c4SZhenzhong Duan if (ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_data)) {
2705ee3dc7aSYi Liu error_setg_errno(errp, errno, "detach %s failed", vbasedev->name);
27145d0d8c4SZhenzhong Duan return false;
2725ee3dc7aSYi Liu }
2735ee3dc7aSYi Liu
27445d0d8c4SZhenzhong Duan trace_iommufd_cdev_detach_ioas_hwpt(iommufd, vbasedev->name);
27545d0d8c4SZhenzhong Duan return true;
27645d0d8c4SZhenzhong Duan }
27745d0d8c4SZhenzhong Duan
iommufd_cdev_autodomains_get(VFIODevice * vbasedev,VFIOIOMMUFDContainer * container,Error ** errp)2785b1e96e6SJoao Martins static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
2795b1e96e6SJoao Martins VFIOIOMMUFDContainer *container,
2805b1e96e6SJoao Martins Error **errp)
2815b1e96e6SJoao Martins {
2825b1e96e6SJoao Martins ERRP_GUARD();
2835b1e96e6SJoao Martins IOMMUFDBackend *iommufd = vbasedev->iommufd;
2845b1e96e6SJoao Martins uint32_t flags = 0;
2855b1e96e6SJoao Martins VFIOIOASHwpt *hwpt;
2865b1e96e6SJoao Martins uint32_t hwpt_id;
2875b1e96e6SJoao Martins int ret;
2885b1e96e6SJoao Martins
2895b1e96e6SJoao Martins /* Try to find a domain */
2905b1e96e6SJoao Martins QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
2915b1e96e6SJoao Martins ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
2925b1e96e6SJoao Martins if (ret) {
2935b1e96e6SJoao Martins /* -EINVAL means the domain is incompatible with the device. */
2945b1e96e6SJoao Martins if (ret == -EINVAL) {
2955b1e96e6SJoao Martins /*
2965b1e96e6SJoao Martins * It is an expected failure and it just means we will try
2975b1e96e6SJoao Martins * another domain, or create one if no existing compatible
2985b1e96e6SJoao Martins * domain is found. Hence why the error is discarded below.
2995b1e96e6SJoao Martins */
3005b1e96e6SJoao Martins error_free(*errp);
3015b1e96e6SJoao Martins *errp = NULL;
3025b1e96e6SJoao Martins continue;
3035b1e96e6SJoao Martins }
3045b1e96e6SJoao Martins
3055b1e96e6SJoao Martins return false;
3065b1e96e6SJoao Martins } else {
3075b1e96e6SJoao Martins vbasedev->hwpt = hwpt;
3085b1e96e6SJoao Martins QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next);
309dddfd8d6SJoao Martins vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt);
3105b1e96e6SJoao Martins return true;
3115b1e96e6SJoao Martins }
3125b1e96e6SJoao Martins }
3135b1e96e6SJoao Martins
314dddfd8d6SJoao Martins /*
315dddfd8d6SJoao Martins * This is quite early and VFIO Migration state isn't yet fully
316dddfd8d6SJoao Martins * initialized, thus rely only on IOMMU hardware capabilities as to
317dddfd8d6SJoao Martins * whether IOMMU dirty tracking is going to be requested. Later
318dddfd8d6SJoao Martins * vfio_migration_realize() may decide to use VF dirty tracking
319dddfd8d6SJoao Martins * instead.
320dddfd8d6SJoao Martins */
321dddfd8d6SJoao Martins if (vbasedev->hiod->caps.hw_caps & IOMMU_HW_CAP_DIRTY_TRACKING) {
322dddfd8d6SJoao Martins flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
323dddfd8d6SJoao Martins }
324dddfd8d6SJoao Martins
3255b1e96e6SJoao Martins if (!iommufd_backend_alloc_hwpt(iommufd, vbasedev->devid,
3265b1e96e6SJoao Martins container->ioas_id, flags,
3275b1e96e6SJoao Martins IOMMU_HWPT_DATA_NONE, 0, NULL,
3285b1e96e6SJoao Martins &hwpt_id, errp)) {
3295b1e96e6SJoao Martins return false;
3305b1e96e6SJoao Martins }
3315b1e96e6SJoao Martins
3325b1e96e6SJoao Martins hwpt = g_malloc0(sizeof(*hwpt));
3335b1e96e6SJoao Martins hwpt->hwpt_id = hwpt_id;
334dddfd8d6SJoao Martins hwpt->hwpt_flags = flags;
3355b1e96e6SJoao Martins QLIST_INIT(&hwpt->device_list);
3365b1e96e6SJoao Martins
3375b1e96e6SJoao Martins ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
3385b1e96e6SJoao Martins if (ret) {
3395b1e96e6SJoao Martins iommufd_backend_free_id(container->be, hwpt->hwpt_id);
3405b1e96e6SJoao Martins g_free(hwpt);
3415b1e96e6SJoao Martins return false;
3425b1e96e6SJoao Martins }
3435b1e96e6SJoao Martins
3445b1e96e6SJoao Martins vbasedev->hwpt = hwpt;
345dddfd8d6SJoao Martins vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt);
3465b1e96e6SJoao Martins QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next);
3475b1e96e6SJoao Martins QLIST_INSERT_HEAD(&container->hwpt_list, hwpt, next);
348dddfd8d6SJoao Martins container->bcontainer.dirty_pages_supported |=
349dddfd8d6SJoao Martins vbasedev->iommu_dirty_tracking;
350dddfd8d6SJoao Martins if (container->bcontainer.dirty_pages_supported &&
351dddfd8d6SJoao Martins !vbasedev->iommu_dirty_tracking) {
352dddfd8d6SJoao Martins warn_report("IOMMU instance for device %s doesn't support dirty tracking",
353dddfd8d6SJoao Martins vbasedev->name);
354dddfd8d6SJoao Martins }
3555b1e96e6SJoao Martins return true;
3565b1e96e6SJoao Martins }
3575b1e96e6SJoao Martins
iommufd_cdev_autodomains_put(VFIODevice * vbasedev,VFIOIOMMUFDContainer * container)3585b1e96e6SJoao Martins static void iommufd_cdev_autodomains_put(VFIODevice *vbasedev,
3595b1e96e6SJoao Martins VFIOIOMMUFDContainer *container)
3605b1e96e6SJoao Martins {
3615b1e96e6SJoao Martins VFIOIOASHwpt *hwpt = vbasedev->hwpt;
3625b1e96e6SJoao Martins
3635b1e96e6SJoao Martins QLIST_REMOVE(vbasedev, hwpt_next);
3645b1e96e6SJoao Martins vbasedev->hwpt = NULL;
3655b1e96e6SJoao Martins
3665b1e96e6SJoao Martins if (QLIST_EMPTY(&hwpt->device_list)) {
3675b1e96e6SJoao Martins QLIST_REMOVE(hwpt, next);
3685b1e96e6SJoao Martins iommufd_backend_free_id(container->be, hwpt->hwpt_id);
3695b1e96e6SJoao Martins g_free(hwpt);
3705b1e96e6SJoao Martins }
3715b1e96e6SJoao Martins }
3725b1e96e6SJoao Martins
iommufd_cdev_attach_container(VFIODevice * vbasedev,VFIOIOMMUFDContainer * container,Error ** errp)37345d0d8c4SZhenzhong Duan static bool iommufd_cdev_attach_container(VFIODevice *vbasedev,
3745ee3dc7aSYi Liu VFIOIOMMUFDContainer *container,
3755ee3dc7aSYi Liu Error **errp)
3765ee3dc7aSYi Liu {
3775b1e96e6SJoao Martins /* mdevs aren't physical devices and will fail with auto domains */
3785b1e96e6SJoao Martins if (!vbasedev->mdev) {
3795b1e96e6SJoao Martins return iommufd_cdev_autodomains_get(vbasedev, container, errp);
3805b1e96e6SJoao Martins }
3815b1e96e6SJoao Martins
382b07dcb7dSJoao Martins return !iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp);
3835ee3dc7aSYi Liu }
3845ee3dc7aSYi Liu
iommufd_cdev_detach_container(VFIODevice * vbasedev,VFIOIOMMUFDContainer * container)3855ee3dc7aSYi Liu static void iommufd_cdev_detach_container(VFIODevice *vbasedev,
3865ee3dc7aSYi Liu VFIOIOMMUFDContainer *container)
3875ee3dc7aSYi Liu {
3885ee3dc7aSYi Liu Error *err = NULL;
3895ee3dc7aSYi Liu
39045d0d8c4SZhenzhong Duan if (!iommufd_cdev_detach_ioas_hwpt(vbasedev, &err)) {
3915ee3dc7aSYi Liu error_report_err(err);
3925ee3dc7aSYi Liu }
3935b1e96e6SJoao Martins
3945b1e96e6SJoao Martins if (vbasedev->hwpt) {
3955b1e96e6SJoao Martins iommufd_cdev_autodomains_put(vbasedev, container);
3965b1e96e6SJoao Martins }
3975b1e96e6SJoao Martins
3985ee3dc7aSYi Liu }
3995ee3dc7aSYi Liu
iommufd_cdev_container_destroy(VFIOIOMMUFDContainer * container)4005ee3dc7aSYi Liu static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container)
4015ee3dc7aSYi Liu {
4025ee3dc7aSYi Liu VFIOContainerBase *bcontainer = &container->bcontainer;
4035ee3dc7aSYi Liu
4045ee3dc7aSYi Liu if (!QLIST_EMPTY(&bcontainer->device_list)) {
4055ee3dc7aSYi Liu return;
4065ee3dc7aSYi Liu }
4075ee3dc7aSYi Liu memory_listener_unregister(&bcontainer->listener);
4085ee3dc7aSYi Liu iommufd_backend_free_id(container->be, container->ioas_id);
40993802605SCédric Le Goater object_unref(container);
4105ee3dc7aSYi Liu }
4115ee3dc7aSYi Liu
iommufd_cdev_ram_block_discard_disable(bool state)4125ee3dc7aSYi Liu static int iommufd_cdev_ram_block_discard_disable(bool state)
4135ee3dc7aSYi Liu {
4145ee3dc7aSYi Liu /*
4155ee3dc7aSYi Liu * We support coordinated discarding of RAM via the RamDiscardManager.
4165ee3dc7aSYi Liu */
4175ee3dc7aSYi Liu return ram_block_uncoordinated_discard_disable(state);
4185ee3dc7aSYi Liu }
4195ee3dc7aSYi Liu
iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer * container,uint32_t ioas_id,Error ** errp)42045d0d8c4SZhenzhong Duan static bool iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container,
421714e9affSZhenzhong Duan uint32_t ioas_id, Error **errp)
422714e9affSZhenzhong Duan {
423714e9affSZhenzhong Duan VFIOContainerBase *bcontainer = &container->bcontainer;
424f3758413SZhenzhong Duan g_autofree struct iommu_ioas_iova_ranges *info = NULL;
425714e9affSZhenzhong Duan struct iommu_iova_range *iova_ranges;
42645d0d8c4SZhenzhong Duan int sz, fd = container->be->fd;
427714e9affSZhenzhong Duan
428714e9affSZhenzhong Duan info = g_malloc0(sizeof(*info));
429714e9affSZhenzhong Duan info->size = sizeof(*info);
430714e9affSZhenzhong Duan info->ioas_id = ioas_id;
431714e9affSZhenzhong Duan
43245d0d8c4SZhenzhong Duan if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info) && errno != EMSGSIZE) {
433714e9affSZhenzhong Duan goto error;
434714e9affSZhenzhong Duan }
435714e9affSZhenzhong Duan
436714e9affSZhenzhong Duan sz = info->num_iovas * sizeof(struct iommu_iova_range);
437714e9affSZhenzhong Duan info = g_realloc(info, sizeof(*info) + sz);
438714e9affSZhenzhong Duan info->allowed_iovas = (uintptr_t)(info + 1);
439714e9affSZhenzhong Duan
44045d0d8c4SZhenzhong Duan if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info)) {
441714e9affSZhenzhong Duan goto error;
442714e9affSZhenzhong Duan }
443714e9affSZhenzhong Duan
444714e9affSZhenzhong Duan iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas;
445714e9affSZhenzhong Duan
446714e9affSZhenzhong Duan for (int i = 0; i < info->num_iovas; i++) {
447714e9affSZhenzhong Duan Range *range = g_new(Range, 1);
448714e9affSZhenzhong Duan
449714e9affSZhenzhong Duan range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last);
450714e9affSZhenzhong Duan bcontainer->iova_ranges =
451714e9affSZhenzhong Duan range_list_insert(bcontainer->iova_ranges, range);
452714e9affSZhenzhong Duan }
453714e9affSZhenzhong Duan bcontainer->pgsizes = info->out_iova_alignment;
454714e9affSZhenzhong Duan
45545d0d8c4SZhenzhong Duan return true;
456714e9affSZhenzhong Duan
457714e9affSZhenzhong Duan error:
458714e9affSZhenzhong Duan error_setg_errno(errp, errno, "Cannot get IOVA ranges");
45945d0d8c4SZhenzhong Duan return false;
460714e9affSZhenzhong Duan }
461714e9affSZhenzhong Duan
iommufd_cdev_attach(const char * name,VFIODevice * vbasedev,AddressSpace * as,Error ** errp)462b7754835SZhenzhong Duan static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
4635ee3dc7aSYi Liu AddressSpace *as, Error **errp)
4645ee3dc7aSYi Liu {
4655ee3dc7aSYi Liu VFIOContainerBase *bcontainer;
4665ee3dc7aSYi Liu VFIOIOMMUFDContainer *container;
4675ee3dc7aSYi Liu VFIOAddressSpace *space;
4685ee3dc7aSYi Liu struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
4695ee3dc7aSYi Liu int ret, devfd;
4705ee3dc7aSYi Liu uint32_t ioas_id;
4715ee3dc7aSYi Liu Error *err = NULL;
472ce5f6d49SCédric Le Goater const VFIOIOMMUClass *iommufd_vioc =
473ce5f6d49SCédric Le Goater VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
4745ee3dc7aSYi Liu
475da3e04b2SZhenzhong Duan if (vbasedev->fd < 0) {
4765ee3dc7aSYi Liu devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp);
4775ee3dc7aSYi Liu if (devfd < 0) {
478b7754835SZhenzhong Duan return false;
4795ee3dc7aSYi Liu }
4805ee3dc7aSYi Liu vbasedev->fd = devfd;
481da3e04b2SZhenzhong Duan } else {
482da3e04b2SZhenzhong Duan devfd = vbasedev->fd;
483da3e04b2SZhenzhong Duan }
4845ee3dc7aSYi Liu
48545d0d8c4SZhenzhong Duan if (!iommufd_cdev_connect_and_bind(vbasedev, errp)) {
4865ee3dc7aSYi Liu goto err_connect_bind;
4875ee3dc7aSYi Liu }
4885ee3dc7aSYi Liu
4895ee3dc7aSYi Liu space = vfio_get_address_space(as);
4905ee3dc7aSYi Liu
49183a4d596SJoao Martins /*
49283a4d596SJoao Martins * The HostIOMMUDevice data from legacy backend is static and doesn't need
49383a4d596SJoao Martins * any information from the (type1-iommu) backend to be initialized. In
49483a4d596SJoao Martins * contrast however, the IOMMUFD HostIOMMUDevice data requires the iommufd
49583a4d596SJoao Martins * FD to be connected and having a devid to be able to successfully call
49683a4d596SJoao Martins * iommufd_backend_get_device_info().
49783a4d596SJoao Martins */
49883a4d596SJoao Martins if (!vfio_device_hiod_realize(vbasedev, errp)) {
49983a4d596SJoao Martins goto err_alloc_ioas;
50083a4d596SJoao Martins }
50183a4d596SJoao Martins
5025ee3dc7aSYi Liu /* try to attach to an existing container in this space */
5035ee3dc7aSYi Liu QLIST_FOREACH(bcontainer, &space->containers, next) {
5045ee3dc7aSYi Liu container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
50541d698b8SCédric Le Goater if (VFIO_IOMMU_GET_CLASS(bcontainer) != iommufd_vioc ||
5065ee3dc7aSYi Liu vbasedev->iommufd != container->be) {
5075ee3dc7aSYi Liu continue;
5085ee3dc7aSYi Liu }
50945d0d8c4SZhenzhong Duan if (!iommufd_cdev_attach_container(vbasedev, container, &err)) {
5105ee3dc7aSYi Liu const char *msg = error_get_pretty(err);
5115ee3dc7aSYi Liu
5125ee3dc7aSYi Liu trace_iommufd_cdev_fail_attach_existing_container(msg);
5135ee3dc7aSYi Liu error_free(err);
5145ee3dc7aSYi Liu err = NULL;
5155ee3dc7aSYi Liu } else {
5165ee3dc7aSYi Liu ret = iommufd_cdev_ram_block_discard_disable(true);
5175ee3dc7aSYi Liu if (ret) {
5185ee3dc7aSYi Liu error_setg(errp,
5195ee3dc7aSYi Liu "Cannot set discarding of RAM broken (%d)", ret);
5205ee3dc7aSYi Liu goto err_discard_disable;
5215ee3dc7aSYi Liu }
5225ee3dc7aSYi Liu goto found_container;
5235ee3dc7aSYi Liu }
5245ee3dc7aSYi Liu }
5255ee3dc7aSYi Liu
5265ee3dc7aSYi Liu /* Need to allocate a new dedicated container */
5279067d50dSZhenzhong Duan if (!iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp)) {
5285ee3dc7aSYi Liu goto err_alloc_ioas;
5295ee3dc7aSYi Liu }
5305ee3dc7aSYi Liu
5315ee3dc7aSYi Liu trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id);
5325ee3dc7aSYi Liu
53393802605SCédric Le Goater container = VFIO_IOMMU_IOMMUFD(object_new(TYPE_VFIO_IOMMU_IOMMUFD));
5345ee3dc7aSYi Liu container->be = vbasedev->iommufd;
5355ee3dc7aSYi Liu container->ioas_id = ioas_id;
5365b1e96e6SJoao Martins QLIST_INIT(&container->hwpt_list);
5375ee3dc7aSYi Liu
5385ee3dc7aSYi Liu bcontainer = &container->bcontainer;
539b7b79588SCédric Le Goater vfio_address_space_insert(space, bcontainer);
5405ee3dc7aSYi Liu
54145d0d8c4SZhenzhong Duan if (!iommufd_cdev_attach_container(vbasedev, container, errp)) {
5425ee3dc7aSYi Liu goto err_attach_container;
5435ee3dc7aSYi Liu }
5445ee3dc7aSYi Liu
5455ee3dc7aSYi Liu ret = iommufd_cdev_ram_block_discard_disable(true);
5465ee3dc7aSYi Liu if (ret) {
5475ee3dc7aSYi Liu goto err_discard_disable;
5485ee3dc7aSYi Liu }
5495ee3dc7aSYi Liu
55045d0d8c4SZhenzhong Duan if (!iommufd_cdev_get_info_iova_range(container, ioas_id, &err)) {
551714e9affSZhenzhong Duan error_append_hint(&err,
552714e9affSZhenzhong Duan "Fallback to default 64bit IOVA range and 4K page size\n");
553714e9affSZhenzhong Duan warn_report_err(err);
554714e9affSZhenzhong Duan err = NULL;
5555ee3dc7aSYi Liu bcontainer->pgsizes = qemu_real_host_page_size();
556714e9affSZhenzhong Duan }
5575ee3dc7aSYi Liu
5585ee3dc7aSYi Liu bcontainer->listener = vfio_memory_listener;
5595ee3dc7aSYi Liu memory_listener_register(&bcontainer->listener, bcontainer->space->as);
5605ee3dc7aSYi Liu
5615ee3dc7aSYi Liu if (bcontainer->error) {
5625ee3dc7aSYi Liu error_propagate_prepend(errp, bcontainer->error,
5635ee3dc7aSYi Liu "memory listener initialization failed: ");
5645ee3dc7aSYi Liu goto err_listener_register;
5655ee3dc7aSYi Liu }
5665ee3dc7aSYi Liu
5675ee3dc7aSYi Liu bcontainer->initialized = true;
5685ee3dc7aSYi Liu
5695ee3dc7aSYi Liu found_container:
5705ee3dc7aSYi Liu ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info);
5715ee3dc7aSYi Liu if (ret) {
5725ee3dc7aSYi Liu error_setg_errno(errp, errno, "error getting device info");
5735ee3dc7aSYi Liu goto err_listener_register;
5745ee3dc7aSYi Liu }
5755ee3dc7aSYi Liu
576f38f5dd1SZhenzhong Duan if (!vfio_cpr_register_container(bcontainer, errp)) {
577d9fa4223SSteve Sistare goto err_listener_register;
578d9fa4223SSteve Sistare }
579d9fa4223SSteve Sistare
5805ee3dc7aSYi Liu /*
5815ee3dc7aSYi Liu * TODO: examine RAM_BLOCK_DISCARD stuff, should we do group level
5825ee3dc7aSYi Liu * for discarding incompatibility check as well?
5835ee3dc7aSYi Liu */
5845ee3dc7aSYi Liu if (vbasedev->ram_block_discard_allowed) {
5855ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false);
5865ee3dc7aSYi Liu }
5875ee3dc7aSYi Liu
5885ee3dc7aSYi Liu vbasedev->group = 0;
5895ee3dc7aSYi Liu vbasedev->num_irqs = dev_info.num_irqs;
5905ee3dc7aSYi Liu vbasedev->num_regions = dev_info.num_regions;
5915ee3dc7aSYi Liu vbasedev->flags = dev_info.flags;
5925ee3dc7aSYi Liu vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
5935ee3dc7aSYi Liu vbasedev->bcontainer = bcontainer;
5945ee3dc7aSYi Liu QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
5955ee3dc7aSYi Liu QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
5965ee3dc7aSYi Liu
5975ee3dc7aSYi Liu trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
5985ee3dc7aSYi Liu vbasedev->num_regions, vbasedev->flags);
599b7754835SZhenzhong Duan return true;
6005ee3dc7aSYi Liu
6015ee3dc7aSYi Liu err_listener_register:
6025ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false);
6035ee3dc7aSYi Liu err_discard_disable:
6045ee3dc7aSYi Liu iommufd_cdev_detach_container(vbasedev, container);
6055ee3dc7aSYi Liu err_attach_container:
6065ee3dc7aSYi Liu iommufd_cdev_container_destroy(container);
6075ee3dc7aSYi Liu err_alloc_ioas:
6085ee3dc7aSYi Liu vfio_put_address_space(space);
6095ee3dc7aSYi Liu iommufd_cdev_unbind_and_disconnect(vbasedev);
6105ee3dc7aSYi Liu err_connect_bind:
6115ee3dc7aSYi Liu close(vbasedev->fd);
612b7754835SZhenzhong Duan return false;
6135ee3dc7aSYi Liu }
6145ee3dc7aSYi Liu
iommufd_cdev_detach(VFIODevice * vbasedev)6155ee3dc7aSYi Liu static void iommufd_cdev_detach(VFIODevice *vbasedev)
6165ee3dc7aSYi Liu {
6175ee3dc7aSYi Liu VFIOContainerBase *bcontainer = vbasedev->bcontainer;
6185ee3dc7aSYi Liu VFIOAddressSpace *space = bcontainer->space;
6195ee3dc7aSYi Liu VFIOIOMMUFDContainer *container = container_of(bcontainer,
6205ee3dc7aSYi Liu VFIOIOMMUFDContainer,
6215ee3dc7aSYi Liu bcontainer);
6225ee3dc7aSYi Liu QLIST_REMOVE(vbasedev, global_next);
6235ee3dc7aSYi Liu QLIST_REMOVE(vbasedev, container_next);
6245ee3dc7aSYi Liu vbasedev->bcontainer = NULL;
6255ee3dc7aSYi Liu
6265ee3dc7aSYi Liu if (!vbasedev->ram_block_discard_allowed) {
6275ee3dc7aSYi Liu iommufd_cdev_ram_block_discard_disable(false);
6285ee3dc7aSYi Liu }
6295ee3dc7aSYi Liu
630d9fa4223SSteve Sistare vfio_cpr_unregister_container(bcontainer);
6315ee3dc7aSYi Liu iommufd_cdev_detach_container(vbasedev, container);
6325ee3dc7aSYi Liu iommufd_cdev_container_destroy(container);
6335ee3dc7aSYi Liu vfio_put_address_space(space);
6345ee3dc7aSYi Liu
6355ee3dc7aSYi Liu iommufd_cdev_unbind_and_disconnect(vbasedev);
6365ee3dc7aSYi Liu close(vbasedev->fd);
6375ee3dc7aSYi Liu }
6385ee3dc7aSYi Liu
iommufd_cdev_pci_find_by_devid(__u32 devid)63996d6f85fSZhenzhong Duan static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid)
64096d6f85fSZhenzhong Duan {
64196d6f85fSZhenzhong Duan VFIODevice *vbasedev_iter;
642ce5f6d49SCédric Le Goater const VFIOIOMMUClass *iommufd_vioc =
643ce5f6d49SCédric Le Goater VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
64496d6f85fSZhenzhong Duan
64596d6f85fSZhenzhong Duan QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) {
64641d698b8SCédric Le Goater if (VFIO_IOMMU_GET_CLASS(vbasedev_iter->bcontainer) != iommufd_vioc) {
64796d6f85fSZhenzhong Duan continue;
64896d6f85fSZhenzhong Duan }
64996d6f85fSZhenzhong Duan if (devid == vbasedev_iter->devid) {
65096d6f85fSZhenzhong Duan return vbasedev_iter;
65196d6f85fSZhenzhong Duan }
65296d6f85fSZhenzhong Duan }
65396d6f85fSZhenzhong Duan return NULL;
65496d6f85fSZhenzhong Duan }
65596d6f85fSZhenzhong Duan
65696d6f85fSZhenzhong Duan static VFIOPCIDevice *
iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device * dep_dev,VFIODevice * reset_dev)65796d6f85fSZhenzhong Duan iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev,
65896d6f85fSZhenzhong Duan VFIODevice *reset_dev)
65996d6f85fSZhenzhong Duan {
66096d6f85fSZhenzhong Duan VFIODevice *vbasedev_tmp;
66196d6f85fSZhenzhong Duan
66296d6f85fSZhenzhong Duan if (dep_dev->devid == reset_dev->devid ||
66396d6f85fSZhenzhong Duan dep_dev->devid == VFIO_PCI_DEVID_OWNED) {
66496d6f85fSZhenzhong Duan return NULL;
66596d6f85fSZhenzhong Duan }
66696d6f85fSZhenzhong Duan
66796d6f85fSZhenzhong Duan vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid);
66896d6f85fSZhenzhong Duan if (!vbasedev_tmp || !vbasedev_tmp->dev->realized ||
66996d6f85fSZhenzhong Duan vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) {
67096d6f85fSZhenzhong Duan return NULL;
67196d6f85fSZhenzhong Duan }
67296d6f85fSZhenzhong Duan
67396d6f85fSZhenzhong Duan return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev);
67496d6f85fSZhenzhong Duan }
67596d6f85fSZhenzhong Duan
iommufd_cdev_pci_hot_reset(VFIODevice * vbasedev,bool single)67696d6f85fSZhenzhong Duan static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single)
67796d6f85fSZhenzhong Duan {
67896d6f85fSZhenzhong Duan VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
67996d6f85fSZhenzhong Duan struct vfio_pci_hot_reset_info *info = NULL;
68096d6f85fSZhenzhong Duan struct vfio_pci_dependent_device *devices;
68196d6f85fSZhenzhong Duan struct vfio_pci_hot_reset *reset;
68296d6f85fSZhenzhong Duan int ret, i;
68396d6f85fSZhenzhong Duan bool multi = false;
68496d6f85fSZhenzhong Duan
68596d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi");
68696d6f85fSZhenzhong Duan
68796d6f85fSZhenzhong Duan if (!single) {
68896d6f85fSZhenzhong Duan vfio_pci_pre_reset(vdev);
68996d6f85fSZhenzhong Duan }
69096d6f85fSZhenzhong Duan vdev->vbasedev.needs_reset = false;
69196d6f85fSZhenzhong Duan
69296d6f85fSZhenzhong Duan ret = vfio_pci_get_pci_hot_reset_info(vdev, &info);
69396d6f85fSZhenzhong Duan
69496d6f85fSZhenzhong Duan if (ret) {
69596d6f85fSZhenzhong Duan goto out_single;
69696d6f85fSZhenzhong Duan }
69796d6f85fSZhenzhong Duan
69896d6f85fSZhenzhong Duan assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID);
69996d6f85fSZhenzhong Duan
70096d6f85fSZhenzhong Duan devices = &info->devices[0];
70196d6f85fSZhenzhong Duan
70296d6f85fSZhenzhong Duan if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) {
70396d6f85fSZhenzhong Duan if (!vdev->has_pm_reset) {
70496d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) {
70596d6f85fSZhenzhong Duan if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) {
70696d6f85fSZhenzhong Duan error_report("vfio: Cannot reset device %s, "
70796d6f85fSZhenzhong Duan "depends on device %04x:%02x:%02x.%x "
70896d6f85fSZhenzhong Duan "which is not owned.",
70996d6f85fSZhenzhong Duan vdev->vbasedev.name, devices[i].segment,
71096d6f85fSZhenzhong Duan devices[i].bus, PCI_SLOT(devices[i].devfn),
71196d6f85fSZhenzhong Duan PCI_FUNC(devices[i].devfn));
71296d6f85fSZhenzhong Duan }
71396d6f85fSZhenzhong Duan }
71496d6f85fSZhenzhong Duan }
71596d6f85fSZhenzhong Duan ret = -EPERM;
71696d6f85fSZhenzhong Duan goto out_single;
71796d6f85fSZhenzhong Duan }
71896d6f85fSZhenzhong Duan
71996d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name);
72096d6f85fSZhenzhong Duan
72196d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) {
72296d6f85fSZhenzhong Duan VFIOPCIDevice *tmp;
72396d6f85fSZhenzhong Duan
72496d6f85fSZhenzhong Duan trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment,
72596d6f85fSZhenzhong Duan devices[i].bus,
72696d6f85fSZhenzhong Duan PCI_SLOT(devices[i].devfn),
72796d6f85fSZhenzhong Duan PCI_FUNC(devices[i].devfn),
72896d6f85fSZhenzhong Duan devices[i].devid);
72996d6f85fSZhenzhong Duan
73096d6f85fSZhenzhong Duan /*
73196d6f85fSZhenzhong Duan * If a VFIO cdev device is resettable, all the dependent devices
73296d6f85fSZhenzhong Duan * are either bound to same iommufd or within same iommu_groups as
73396d6f85fSZhenzhong Duan * one of the iommufd bound devices.
73496d6f85fSZhenzhong Duan */
73596d6f85fSZhenzhong Duan assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED);
73696d6f85fSZhenzhong Duan
73796d6f85fSZhenzhong Duan tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
73896d6f85fSZhenzhong Duan if (!tmp) {
73996d6f85fSZhenzhong Duan continue;
74096d6f85fSZhenzhong Duan }
74196d6f85fSZhenzhong Duan
74296d6f85fSZhenzhong Duan if (single) {
74396d6f85fSZhenzhong Duan ret = -EINVAL;
74496d6f85fSZhenzhong Duan goto out_single;
74596d6f85fSZhenzhong Duan }
74696d6f85fSZhenzhong Duan vfio_pci_pre_reset(tmp);
74796d6f85fSZhenzhong Duan tmp->vbasedev.needs_reset = false;
74896d6f85fSZhenzhong Duan multi = true;
74996d6f85fSZhenzhong Duan }
75096d6f85fSZhenzhong Duan
75196d6f85fSZhenzhong Duan if (!single && !multi) {
75296d6f85fSZhenzhong Duan ret = -EINVAL;
75396d6f85fSZhenzhong Duan goto out_single;
75496d6f85fSZhenzhong Duan }
75596d6f85fSZhenzhong Duan
75696d6f85fSZhenzhong Duan /* Use zero length array for hot reset with iommufd backend */
75796d6f85fSZhenzhong Duan reset = g_malloc0(sizeof(*reset));
75896d6f85fSZhenzhong Duan reset->argsz = sizeof(*reset);
75996d6f85fSZhenzhong Duan
76096d6f85fSZhenzhong Duan /* Bus reset! */
76196d6f85fSZhenzhong Duan ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset);
76296d6f85fSZhenzhong Duan g_free(reset);
76396d6f85fSZhenzhong Duan if (ret) {
76496d6f85fSZhenzhong Duan ret = -errno;
76596d6f85fSZhenzhong Duan }
76696d6f85fSZhenzhong Duan
76796d6f85fSZhenzhong Duan trace_vfio_pci_hot_reset_result(vdev->vbasedev.name,
76896d6f85fSZhenzhong Duan ret ? strerror(errno) : "Success");
76996d6f85fSZhenzhong Duan
77096d6f85fSZhenzhong Duan /* Re-enable INTx on affected devices */
77196d6f85fSZhenzhong Duan for (i = 0; i < info->count; i++) {
77296d6f85fSZhenzhong Duan VFIOPCIDevice *tmp;
77396d6f85fSZhenzhong Duan
77496d6f85fSZhenzhong Duan tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
77596d6f85fSZhenzhong Duan if (!tmp) {
77696d6f85fSZhenzhong Duan continue;
77796d6f85fSZhenzhong Duan }
77896d6f85fSZhenzhong Duan vfio_pci_post_reset(tmp);
77996d6f85fSZhenzhong Duan }
78096d6f85fSZhenzhong Duan out_single:
78196d6f85fSZhenzhong Duan if (!single) {
78296d6f85fSZhenzhong Duan vfio_pci_post_reset(vdev);
78396d6f85fSZhenzhong Duan }
78496d6f85fSZhenzhong Duan g_free(info);
78596d6f85fSZhenzhong Duan
78696d6f85fSZhenzhong Duan return ret;
78796d6f85fSZhenzhong Duan }
78896d6f85fSZhenzhong Duan
vfio_iommu_iommufd_class_init(ObjectClass * klass,void * data)789ce5f6d49SCédric Le Goater static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
790ce5f6d49SCédric Le Goater {
791ce5f6d49SCédric Le Goater VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
792ce5f6d49SCédric Le Goater
793a7fd91b8SZhenzhong Duan vioc->hiod_typename = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO;
794a7fd91b8SZhenzhong Duan
795ce5f6d49SCédric Le Goater vioc->dma_map = iommufd_cdev_map;
796ce5f6d49SCédric Le Goater vioc->dma_unmap = iommufd_cdev_unmap;
797ce5f6d49SCédric Le Goater vioc->attach_device = iommufd_cdev_attach;
798ce5f6d49SCédric Le Goater vioc->detach_device = iommufd_cdev_detach;
799ce5f6d49SCédric Le Goater vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
80052ce8822SJoao Martins vioc->set_dirty_page_tracking = iommufd_set_dirty_page_tracking;
801*7c30710bSJoao Martins vioc->query_dirty_bitmap = iommufd_query_dirty_bitmap;
8025ee3dc7aSYi Liu };
803ce5f6d49SCédric Le Goater
hiod_iommufd_vfio_realize(HostIOMMUDevice * hiod,void * opaque,Error ** errp)80493058952SZhenzhong Duan static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
80593058952SZhenzhong Duan Error **errp)
80693058952SZhenzhong Duan {
80793058952SZhenzhong Duan VFIODevice *vdev = opaque;
80893058952SZhenzhong Duan HostIOMMUDeviceCaps *caps = &hiod->caps;
80993058952SZhenzhong Duan enum iommu_hw_info_type type;
81093058952SZhenzhong Duan union {
81193058952SZhenzhong Duan struct iommu_hw_info_vtd vtd;
81293058952SZhenzhong Duan } data;
8132d1bf258SJoao Martins uint64_t hw_caps;
81493058952SZhenzhong Duan
815dc169694SEric Auger hiod->agent = opaque;
816dc169694SEric Auger
81793058952SZhenzhong Duan if (!iommufd_backend_get_device_info(vdev->iommufd, vdev->devid,
8182d1bf258SJoao Martins &type, &data, sizeof(data),
8192d1bf258SJoao Martins &hw_caps, errp)) {
82093058952SZhenzhong Duan return false;
82193058952SZhenzhong Duan }
82293058952SZhenzhong Duan
82393058952SZhenzhong Duan hiod->name = g_strdup(vdev->name);
82493058952SZhenzhong Duan caps->type = type;
82521e8d3a3SJoao Martins caps->hw_caps = hw_caps;
82693058952SZhenzhong Duan
82793058952SZhenzhong Duan return true;
82893058952SZhenzhong Duan }
82993058952SZhenzhong Duan
8303ad35d91SEric Auger static GList *
hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice * hiod)831d59ca1caSEric Auger hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod)
8323ad35d91SEric Auger {
8333ad35d91SEric Auger VFIODevice *vdev = hiod->agent;
8343ad35d91SEric Auger
8353ad35d91SEric Auger g_assert(vdev);
8363966bca5SEric Auger return vfio_container_get_iova_ranges(vdev->bcontainer);
8373ad35d91SEric Auger }
8383ad35d91SEric Auger
8398fe0ebe1SEric Auger static uint64_t
hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice * hiod)8408fe0ebe1SEric Auger hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod)
8418fe0ebe1SEric Auger {
8428fe0ebe1SEric Auger VFIODevice *vdev = hiod->agent;
8438fe0ebe1SEric Auger
8448fe0ebe1SEric Auger g_assert(vdev);
8458fe0ebe1SEric Auger return vfio_container_get_page_size_mask(vdev->bcontainer);
8468fe0ebe1SEric Auger }
8478fe0ebe1SEric Auger
8488fe0ebe1SEric Auger
hiod_iommufd_vfio_class_init(ObjectClass * oc,void * data)84993058952SZhenzhong Duan static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
85093058952SZhenzhong Duan {
85193058952SZhenzhong Duan HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
85293058952SZhenzhong Duan
85393058952SZhenzhong Duan hiodc->realize = hiod_iommufd_vfio_realize;
8543ad35d91SEric Auger hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
8558fe0ebe1SEric Auger hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
85693058952SZhenzhong Duan };
85793058952SZhenzhong Duan
858ce5f6d49SCédric Le Goater static const TypeInfo types[] = {
859ce5f6d49SCédric Le Goater {
860ce5f6d49SCédric Le Goater .name = TYPE_VFIO_IOMMU_IOMMUFD,
861ce5f6d49SCédric Le Goater .parent = TYPE_VFIO_IOMMU,
862504d297eSCédric Le Goater .instance_size = sizeof(VFIOIOMMUFDContainer),
863ce5f6d49SCédric Le Goater .class_init = vfio_iommu_iommufd_class_init,
8649005f928SZhenzhong Duan }, {
8659005f928SZhenzhong Duan .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO,
8669005f928SZhenzhong Duan .parent = TYPE_HOST_IOMMU_DEVICE_IOMMUFD,
86793058952SZhenzhong Duan .class_init = hiod_iommufd_vfio_class_init,
8689005f928SZhenzhong Duan }
869ce5f6d49SCédric Le Goater };
870ce5f6d49SCédric Le Goater
871ce5f6d49SCédric Le Goater DEFINE_TYPES(types)
872