xref: /openbmc/qemu/hw/vfio/iommufd.c (revision d47f418ea15cd405f41984af80c3cd336c693be3)
1 /*
2  * iommufd container backend
3  *
4  * Copyright (C) 2023 Intel Corporation.
5  * Copyright Red Hat, Inc. 2023
6  *
7  * Authors: Yi Liu <yi.l.liu@intel.com>
8  *          Eric Auger <eric.auger@redhat.com>
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 #include "qemu/osdep.h"
14 #include <sys/ioctl.h>
15 #include <linux/vfio.h>
16 #include <linux/iommufd.h>
17 
18 #include "hw/vfio/vfio-common.h"
19 #include "qemu/error-report.h"
20 #include "trace.h"
21 #include "qapi/error.h"
22 #include "sysemu/iommufd.h"
23 #include "hw/qdev-core.h"
24 #include "sysemu/reset.h"
25 #include "qemu/cutils.h"
26 #include "qemu/chardev_open.h"
27 #include "pci.h"
28 #include "exec/ram_addr.h"
29 
iommufd_cdev_map(const VFIOContainerBase * bcontainer,hwaddr iova,ram_addr_t size,void * vaddr,bool readonly)30 static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
31                             ram_addr_t size, void *vaddr, bool readonly)
32 {
33     const VFIOIOMMUFDContainer *container =
34         container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
35 
36     return iommufd_backend_map_dma(container->be,
37                                    container->ioas_id,
38                                    iova, size, vaddr, readonly);
39 }
40 
iommufd_cdev_unmap(const VFIOContainerBase * bcontainer,hwaddr iova,ram_addr_t size,IOMMUTLBEntry * iotlb)41 static int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer,
42                               hwaddr iova, ram_addr_t size,
43                               IOMMUTLBEntry *iotlb)
44 {
45     const VFIOIOMMUFDContainer *container =
46         container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
47 
48     /* TODO: Handle dma_unmap_bitmap with iotlb args (migration) */
49     return iommufd_backend_unmap_dma(container->be,
50                                      container->ioas_id, iova, size);
51 }
52 
iommufd_cdev_kvm_device_add(VFIODevice * vbasedev,Error ** errp)53 static bool iommufd_cdev_kvm_device_add(VFIODevice *vbasedev, Error **errp)
54 {
55     return !vfio_kvm_device_add_fd(vbasedev->fd, errp);
56 }
57 
iommufd_cdev_kvm_device_del(VFIODevice * vbasedev)58 static void iommufd_cdev_kvm_device_del(VFIODevice *vbasedev)
59 {
60     Error *err = NULL;
61 
62     if (vfio_kvm_device_del_fd(vbasedev->fd, &err)) {
63         error_report_err(err);
64     }
65 }
66 
iommufd_cdev_connect_and_bind(VFIODevice * vbasedev,Error ** errp)67 static bool iommufd_cdev_connect_and_bind(VFIODevice *vbasedev, Error **errp)
68 {
69     IOMMUFDBackend *iommufd = vbasedev->iommufd;
70     struct vfio_device_bind_iommufd bind = {
71         .argsz = sizeof(bind),
72         .flags = 0,
73     };
74 
75     if (!iommufd_backend_connect(iommufd, errp)) {
76         return false;
77     }
78 
79     /*
80      * Add device to kvm-vfio to be prepared for the tracking
81      * in KVM. Especially for some emulated devices, it requires
82      * to have kvm information in the device open.
83      */
84     if (!iommufd_cdev_kvm_device_add(vbasedev, errp)) {
85         goto err_kvm_device_add;
86     }
87 
88     /* Bind device to iommufd */
89     bind.iommufd = iommufd->fd;
90     if (ioctl(vbasedev->fd, VFIO_DEVICE_BIND_IOMMUFD, &bind)) {
91         error_setg_errno(errp, errno, "error bind device fd=%d to iommufd=%d",
92                          vbasedev->fd, bind.iommufd);
93         goto err_bind;
94     }
95 
96     vbasedev->devid = bind.out_devid;
97     trace_iommufd_cdev_connect_and_bind(bind.iommufd, vbasedev->name,
98                                         vbasedev->fd, vbasedev->devid);
99     return true;
100 err_bind:
101     iommufd_cdev_kvm_device_del(vbasedev);
102 err_kvm_device_add:
103     iommufd_backend_disconnect(iommufd);
104     return false;
105 }
106 
iommufd_cdev_unbind_and_disconnect(VFIODevice * vbasedev)107 static void iommufd_cdev_unbind_and_disconnect(VFIODevice *vbasedev)
108 {
109     /* Unbind is automatically conducted when device fd is closed */
110     iommufd_cdev_kvm_device_del(vbasedev);
111     iommufd_backend_disconnect(vbasedev->iommufd);
112 }
113 
iommufd_hwpt_dirty_tracking(VFIOIOASHwpt * hwpt)114 static bool iommufd_hwpt_dirty_tracking(VFIOIOASHwpt *hwpt)
115 {
116     return hwpt && hwpt->hwpt_flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
117 }
118 
iommufd_set_dirty_page_tracking(const VFIOContainerBase * bcontainer,bool start,Error ** errp)119 static int iommufd_set_dirty_page_tracking(const VFIOContainerBase *bcontainer,
120                                            bool start, Error **errp)
121 {
122     const VFIOIOMMUFDContainer *container =
123         container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
124     VFIOIOASHwpt *hwpt;
125 
126     QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
127         if (!iommufd_hwpt_dirty_tracking(hwpt)) {
128             continue;
129         }
130 
131         if (!iommufd_backend_set_dirty_tracking(container->be,
132                                                 hwpt->hwpt_id, start, errp)) {
133             goto err;
134         }
135     }
136 
137     return 0;
138 
139 err:
140     QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
141         if (!iommufd_hwpt_dirty_tracking(hwpt)) {
142             continue;
143         }
144         iommufd_backend_set_dirty_tracking(container->be,
145                                            hwpt->hwpt_id, !start, NULL);
146     }
147     return -EINVAL;
148 }
149 
iommufd_query_dirty_bitmap(const VFIOContainerBase * bcontainer,VFIOBitmap * vbmap,hwaddr iova,hwaddr size,Error ** errp)150 static int iommufd_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
151                                       VFIOBitmap *vbmap, hwaddr iova,
152                                       hwaddr size, Error **errp)
153 {
154     VFIOIOMMUFDContainer *container = container_of(bcontainer,
155                                                    VFIOIOMMUFDContainer,
156                                                    bcontainer);
157     unsigned long page_size = qemu_real_host_page_size();
158     VFIOIOASHwpt *hwpt;
159 
160     QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
161         if (!iommufd_hwpt_dirty_tracking(hwpt)) {
162             continue;
163         }
164 
165         if (!iommufd_backend_get_dirty_bitmap(container->be, hwpt->hwpt_id,
166                                               iova, size, page_size,
167                                               (uint64_t *)vbmap->bitmap,
168                                               errp)) {
169             return -EINVAL;
170         }
171     }
172 
173     return 0;
174 }
175 
iommufd_cdev_getfd(const char * sysfs_path,Error ** errp)176 static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
177 {
178     ERRP_GUARD();
179     long int ret = -ENOTTY;
180     g_autofree char *path = NULL;
181     g_autofree char *vfio_dev_path = NULL;
182     g_autofree char *vfio_path = NULL;
183     DIR *dir = NULL;
184     struct dirent *dent;
185     g_autofree gchar *contents = NULL;
186     gsize length;
187     int major, minor;
188     dev_t vfio_devt;
189 
190     path = g_strdup_printf("%s/vfio-dev", sysfs_path);
191     dir = opendir(path);
192     if (!dir) {
193         error_setg_errno(errp, errno, "couldn't open directory %s", path);
194         goto out;
195     }
196 
197     while ((dent = readdir(dir))) {
198         if (!strncmp(dent->d_name, "vfio", 4)) {
199             vfio_dev_path = g_strdup_printf("%s/%s/dev", path, dent->d_name);
200             break;
201         }
202     }
203 
204     if (!vfio_dev_path) {
205         error_setg(errp, "failed to find vfio-dev/vfioX/dev");
206         goto out_close_dir;
207     }
208 
209     if (!g_file_get_contents(vfio_dev_path, &contents, &length, NULL)) {
210         error_setg(errp, "failed to load \"%s\"", vfio_dev_path);
211         goto out_close_dir;
212     }
213 
214     if (sscanf(contents, "%d:%d", &major, &minor) != 2) {
215         error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path);
216         goto out_close_dir;
217     }
218     vfio_devt = makedev(major, minor);
219 
220     vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name);
221     ret = open_cdev(vfio_path, vfio_devt);
222     if (ret < 0) {
223         error_setg(errp, "Failed to open %s", vfio_path);
224     }
225 
226     trace_iommufd_cdev_getfd(vfio_path, ret);
227 
228 out_close_dir:
229     closedir(dir);
230 out:
231     if (*errp) {
232         error_prepend(errp, VFIO_MSG_PREFIX, path);
233     }
234 
235     return ret;
236 }
237 
iommufd_cdev_attach_ioas_hwpt(VFIODevice * vbasedev,uint32_t id,Error ** errp)238 static int iommufd_cdev_attach_ioas_hwpt(VFIODevice *vbasedev, uint32_t id,
239                                          Error **errp)
240 {
241     int iommufd = vbasedev->iommufd->fd;
242     struct vfio_device_attach_iommufd_pt attach_data = {
243         .argsz = sizeof(attach_data),
244         .flags = 0,
245         .pt_id = id,
246     };
247 
248     /* Attach device to an IOAS or hwpt within iommufd */
249     if (ioctl(vbasedev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data)) {
250         error_setg_errno(errp, errno,
251                          "[iommufd=%d] error attach %s (%d) to id=%d",
252                          iommufd, vbasedev->name, vbasedev->fd, id);
253         return -errno;
254     }
255 
256     trace_iommufd_cdev_attach_ioas_hwpt(iommufd, vbasedev->name,
257                                         vbasedev->fd, id);
258     return 0;
259 }
260 
iommufd_cdev_detach_ioas_hwpt(VFIODevice * vbasedev,Error ** errp)261 static bool iommufd_cdev_detach_ioas_hwpt(VFIODevice *vbasedev, Error **errp)
262 {
263     int iommufd = vbasedev->iommufd->fd;
264     struct vfio_device_detach_iommufd_pt detach_data = {
265         .argsz = sizeof(detach_data),
266         .flags = 0,
267     };
268 
269     if (ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_data)) {
270         error_setg_errno(errp, errno, "detach %s failed", vbasedev->name);
271         return false;
272     }
273 
274     trace_iommufd_cdev_detach_ioas_hwpt(iommufd, vbasedev->name);
275     return true;
276 }
277 
iommufd_cdev_autodomains_get(VFIODevice * vbasedev,VFIOIOMMUFDContainer * container,Error ** errp)278 static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
279                                          VFIOIOMMUFDContainer *container,
280                                          Error **errp)
281 {
282     ERRP_GUARD();
283     IOMMUFDBackend *iommufd = vbasedev->iommufd;
284     uint32_t flags = 0;
285     VFIOIOASHwpt *hwpt;
286     uint32_t hwpt_id;
287     int ret;
288 
289     /* Try to find a domain */
290     QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
291         ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
292         if (ret) {
293             /* -EINVAL means the domain is incompatible with the device. */
294             if (ret == -EINVAL) {
295                 /*
296                  * It is an expected failure and it just means we will try
297                  * another domain, or create one if no existing compatible
298                  * domain is found. Hence why the error is discarded below.
299                  */
300                 error_free(*errp);
301                 *errp = NULL;
302                 continue;
303             }
304 
305             return false;
306         } else {
307             vbasedev->hwpt = hwpt;
308             QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next);
309             vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt);
310             return true;
311         }
312     }
313 
314     /*
315      * This is quite early and VFIO Migration state isn't yet fully
316      * initialized, thus rely only on IOMMU hardware capabilities as to
317      * whether IOMMU dirty tracking is going to be requested. Later
318      * vfio_migration_realize() may decide to use VF dirty tracking
319      * instead.
320      */
321     if (vbasedev->hiod->caps.hw_caps & IOMMU_HW_CAP_DIRTY_TRACKING) {
322         flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
323     }
324 
325     if (!iommufd_backend_alloc_hwpt(iommufd, vbasedev->devid,
326                                     container->ioas_id, flags,
327                                     IOMMU_HWPT_DATA_NONE, 0, NULL,
328                                     &hwpt_id, errp)) {
329         return false;
330     }
331 
332     hwpt = g_malloc0(sizeof(*hwpt));
333     hwpt->hwpt_id = hwpt_id;
334     hwpt->hwpt_flags = flags;
335     QLIST_INIT(&hwpt->device_list);
336 
337     ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
338     if (ret) {
339         iommufd_backend_free_id(container->be, hwpt->hwpt_id);
340         g_free(hwpt);
341         return false;
342     }
343 
344     vbasedev->hwpt = hwpt;
345     vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt);
346     QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next);
347     QLIST_INSERT_HEAD(&container->hwpt_list, hwpt, next);
348     container->bcontainer.dirty_pages_supported |=
349                                 vbasedev->iommu_dirty_tracking;
350     if (container->bcontainer.dirty_pages_supported &&
351         !vbasedev->iommu_dirty_tracking) {
352         warn_report("IOMMU instance for device %s doesn't support dirty tracking",
353                     vbasedev->name);
354     }
355     return true;
356 }
357 
iommufd_cdev_autodomains_put(VFIODevice * vbasedev,VFIOIOMMUFDContainer * container)358 static void iommufd_cdev_autodomains_put(VFIODevice *vbasedev,
359                                          VFIOIOMMUFDContainer *container)
360 {
361     VFIOIOASHwpt *hwpt = vbasedev->hwpt;
362 
363     QLIST_REMOVE(vbasedev, hwpt_next);
364     vbasedev->hwpt = NULL;
365 
366     if (QLIST_EMPTY(&hwpt->device_list)) {
367         QLIST_REMOVE(hwpt, next);
368         iommufd_backend_free_id(container->be, hwpt->hwpt_id);
369         g_free(hwpt);
370     }
371 }
372 
iommufd_cdev_attach_container(VFIODevice * vbasedev,VFIOIOMMUFDContainer * container,Error ** errp)373 static bool iommufd_cdev_attach_container(VFIODevice *vbasedev,
374                                           VFIOIOMMUFDContainer *container,
375                                           Error **errp)
376 {
377     /* mdevs aren't physical devices and will fail with auto domains */
378     if (!vbasedev->mdev) {
379         return iommufd_cdev_autodomains_get(vbasedev, container, errp);
380     }
381 
382     return !iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp);
383 }
384 
iommufd_cdev_detach_container(VFIODevice * vbasedev,VFIOIOMMUFDContainer * container)385 static void iommufd_cdev_detach_container(VFIODevice *vbasedev,
386                                           VFIOIOMMUFDContainer *container)
387 {
388     Error *err = NULL;
389 
390     if (!iommufd_cdev_detach_ioas_hwpt(vbasedev, &err)) {
391         error_report_err(err);
392     }
393 
394     if (vbasedev->hwpt) {
395         iommufd_cdev_autodomains_put(vbasedev, container);
396     }
397 
398 }
399 
iommufd_cdev_container_destroy(VFIOIOMMUFDContainer * container)400 static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container)
401 {
402     VFIOContainerBase *bcontainer = &container->bcontainer;
403 
404     if (!QLIST_EMPTY(&bcontainer->device_list)) {
405         return;
406     }
407     memory_listener_unregister(&bcontainer->listener);
408     iommufd_backend_free_id(container->be, container->ioas_id);
409     object_unref(container);
410 }
411 
iommufd_cdev_ram_block_discard_disable(bool state)412 static int iommufd_cdev_ram_block_discard_disable(bool state)
413 {
414     /*
415      * We support coordinated discarding of RAM via the RamDiscardManager.
416      */
417     return ram_block_uncoordinated_discard_disable(state);
418 }
419 
iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer * container,uint32_t ioas_id,Error ** errp)420 static bool iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container,
421                                              uint32_t ioas_id, Error **errp)
422 {
423     VFIOContainerBase *bcontainer = &container->bcontainer;
424     g_autofree struct iommu_ioas_iova_ranges *info = NULL;
425     struct iommu_iova_range *iova_ranges;
426     int sz, fd = container->be->fd;
427 
428     info = g_malloc0(sizeof(*info));
429     info->size = sizeof(*info);
430     info->ioas_id = ioas_id;
431 
432     if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info) && errno != EMSGSIZE) {
433         goto error;
434     }
435 
436     sz = info->num_iovas * sizeof(struct iommu_iova_range);
437     info = g_realloc(info, sizeof(*info) + sz);
438     info->allowed_iovas = (uintptr_t)(info + 1);
439 
440     if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info)) {
441         goto error;
442     }
443 
444     iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas;
445 
446     for (int i = 0; i < info->num_iovas; i++) {
447         Range *range = g_new(Range, 1);
448 
449         range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last);
450         bcontainer->iova_ranges =
451             range_list_insert(bcontainer->iova_ranges, range);
452     }
453     bcontainer->pgsizes = info->out_iova_alignment;
454 
455     return true;
456 
457 error:
458     error_setg_errno(errp, errno, "Cannot get IOVA ranges");
459     return false;
460 }
461 
iommufd_cdev_attach(const char * name,VFIODevice * vbasedev,AddressSpace * as,Error ** errp)462 static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
463                                 AddressSpace *as, Error **errp)
464 {
465     VFIOContainerBase *bcontainer;
466     VFIOIOMMUFDContainer *container;
467     VFIOAddressSpace *space;
468     struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
469     int ret, devfd;
470     uint32_t ioas_id;
471     Error *err = NULL;
472     const VFIOIOMMUClass *iommufd_vioc =
473         VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
474 
475     if (vbasedev->fd < 0) {
476         devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp);
477         if (devfd < 0) {
478             return false;
479         }
480         vbasedev->fd = devfd;
481     } else {
482         devfd = vbasedev->fd;
483     }
484 
485     if (!iommufd_cdev_connect_and_bind(vbasedev, errp)) {
486         goto err_connect_bind;
487     }
488 
489     space = vfio_get_address_space(as);
490 
491     /*
492      * The HostIOMMUDevice data from legacy backend is static and doesn't need
493      * any information from the (type1-iommu) backend to be initialized. In
494      * contrast however, the IOMMUFD HostIOMMUDevice data requires the iommufd
495      * FD to be connected and having a devid to be able to successfully call
496      * iommufd_backend_get_device_info().
497      */
498     if (!vfio_device_hiod_realize(vbasedev, errp)) {
499         goto err_alloc_ioas;
500     }
501 
502     /* try to attach to an existing container in this space */
503     QLIST_FOREACH(bcontainer, &space->containers, next) {
504         container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
505         if (VFIO_IOMMU_GET_CLASS(bcontainer) != iommufd_vioc ||
506             vbasedev->iommufd != container->be) {
507             continue;
508         }
509         if (!iommufd_cdev_attach_container(vbasedev, container, &err)) {
510             const char *msg = error_get_pretty(err);
511 
512             trace_iommufd_cdev_fail_attach_existing_container(msg);
513             error_free(err);
514             err = NULL;
515         } else {
516             ret = iommufd_cdev_ram_block_discard_disable(true);
517             if (ret) {
518                 error_setg_errno(errp, -ret,
519                                  "Cannot set discarding of RAM broken");
520                 goto err_discard_disable;
521             }
522             goto found_container;
523         }
524     }
525 
526     /* Need to allocate a new dedicated container */
527     if (!iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp)) {
528         goto err_alloc_ioas;
529     }
530 
531     trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id);
532 
533     container = VFIO_IOMMU_IOMMUFD(object_new(TYPE_VFIO_IOMMU_IOMMUFD));
534     container->be = vbasedev->iommufd;
535     container->ioas_id = ioas_id;
536     QLIST_INIT(&container->hwpt_list);
537 
538     bcontainer = &container->bcontainer;
539     vfio_address_space_insert(space, bcontainer);
540 
541     if (!iommufd_cdev_attach_container(vbasedev, container, errp)) {
542         goto err_attach_container;
543     }
544 
545     ret = iommufd_cdev_ram_block_discard_disable(true);
546     if (ret) {
547         error_setg_errno(errp, -ret, "Cannot set discarding of RAM broken");
548         goto err_discard_disable;
549     }
550 
551     if (!iommufd_cdev_get_info_iova_range(container, ioas_id, &err)) {
552         error_append_hint(&err,
553                    "Fallback to default 64bit IOVA range and 4K page size\n");
554         warn_report_err(err);
555         err = NULL;
556         bcontainer->pgsizes = qemu_real_host_page_size();
557     }
558 
559     bcontainer->listener = vfio_memory_listener;
560     memory_listener_register(&bcontainer->listener, bcontainer->space->as);
561 
562     if (bcontainer->error) {
563         error_propagate_prepend(errp, bcontainer->error,
564                                 "memory listener initialization failed: ");
565         goto err_listener_register;
566     }
567 
568     bcontainer->initialized = true;
569 
570 found_container:
571     ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info);
572     if (ret) {
573         error_setg_errno(errp, errno, "error getting device info");
574         goto err_listener_register;
575     }
576 
577     if (!vfio_cpr_register_container(bcontainer, errp)) {
578         goto err_listener_register;
579     }
580 
581     /*
582      * TODO: examine RAM_BLOCK_DISCARD stuff, should we do group level
583      * for discarding incompatibility check as well?
584      */
585     if (vbasedev->ram_block_discard_allowed) {
586         iommufd_cdev_ram_block_discard_disable(false);
587     }
588 
589     vbasedev->group = 0;
590     vbasedev->num_irqs = dev_info.num_irqs;
591     vbasedev->num_regions = dev_info.num_regions;
592     vbasedev->flags = dev_info.flags;
593     vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
594     vbasedev->bcontainer = bcontainer;
595     QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
596     QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
597 
598     trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
599                                    vbasedev->num_regions, vbasedev->flags);
600     return true;
601 
602 err_listener_register:
603     iommufd_cdev_ram_block_discard_disable(false);
604 err_discard_disable:
605     iommufd_cdev_detach_container(vbasedev, container);
606 err_attach_container:
607     iommufd_cdev_container_destroy(container);
608 err_alloc_ioas:
609     vfio_put_address_space(space);
610     iommufd_cdev_unbind_and_disconnect(vbasedev);
611 err_connect_bind:
612     close(vbasedev->fd);
613     return false;
614 }
615 
iommufd_cdev_detach(VFIODevice * vbasedev)616 static void iommufd_cdev_detach(VFIODevice *vbasedev)
617 {
618     VFIOContainerBase *bcontainer = vbasedev->bcontainer;
619     VFIOAddressSpace *space = bcontainer->space;
620     VFIOIOMMUFDContainer *container = container_of(bcontainer,
621                                                    VFIOIOMMUFDContainer,
622                                                    bcontainer);
623     QLIST_REMOVE(vbasedev, global_next);
624     QLIST_REMOVE(vbasedev, container_next);
625     vbasedev->bcontainer = NULL;
626 
627     if (!vbasedev->ram_block_discard_allowed) {
628         iommufd_cdev_ram_block_discard_disable(false);
629     }
630 
631     vfio_cpr_unregister_container(bcontainer);
632     iommufd_cdev_detach_container(vbasedev, container);
633     iommufd_cdev_container_destroy(container);
634     vfio_put_address_space(space);
635 
636     iommufd_cdev_unbind_and_disconnect(vbasedev);
637     close(vbasedev->fd);
638 }
639 
iommufd_cdev_pci_find_by_devid(__u32 devid)640 static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid)
641 {
642     VFIODevice *vbasedev_iter;
643     const VFIOIOMMUClass *iommufd_vioc =
644         VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
645 
646     QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) {
647         if (VFIO_IOMMU_GET_CLASS(vbasedev_iter->bcontainer) != iommufd_vioc) {
648             continue;
649         }
650         if (devid == vbasedev_iter->devid) {
651             return vbasedev_iter;
652         }
653     }
654     return NULL;
655 }
656 
657 static VFIOPCIDevice *
iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device * dep_dev,VFIODevice * reset_dev)658 iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev,
659                                     VFIODevice *reset_dev)
660 {
661     VFIODevice *vbasedev_tmp;
662 
663     if (dep_dev->devid == reset_dev->devid ||
664         dep_dev->devid == VFIO_PCI_DEVID_OWNED) {
665         return NULL;
666     }
667 
668     vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid);
669     if (!vbasedev_tmp || !vbasedev_tmp->dev->realized ||
670         vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) {
671         return NULL;
672     }
673 
674     return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev);
675 }
676 
iommufd_cdev_pci_hot_reset(VFIODevice * vbasedev,bool single)677 static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single)
678 {
679     VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
680     struct vfio_pci_hot_reset_info *info = NULL;
681     struct vfio_pci_dependent_device *devices;
682     struct vfio_pci_hot_reset *reset;
683     int ret, i;
684     bool multi = false;
685 
686     trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi");
687 
688     if (!single) {
689         vfio_pci_pre_reset(vdev);
690     }
691     vdev->vbasedev.needs_reset = false;
692 
693     ret = vfio_pci_get_pci_hot_reset_info(vdev, &info);
694 
695     if (ret) {
696         goto out_single;
697     }
698 
699     assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID);
700 
701     devices = &info->devices[0];
702 
703     if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) {
704         if (!vdev->has_pm_reset) {
705             for (i = 0; i < info->count; i++) {
706                 if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) {
707                     error_report("vfio: Cannot reset device %s, "
708                                  "depends on device %04x:%02x:%02x.%x "
709                                  "which is not owned.",
710                                  vdev->vbasedev.name, devices[i].segment,
711                                  devices[i].bus, PCI_SLOT(devices[i].devfn),
712                                  PCI_FUNC(devices[i].devfn));
713                 }
714             }
715         }
716         ret = -EPERM;
717         goto out_single;
718     }
719 
720     trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name);
721 
722     for (i = 0; i < info->count; i++) {
723         VFIOPCIDevice *tmp;
724 
725         trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment,
726                                                      devices[i].bus,
727                                                      PCI_SLOT(devices[i].devfn),
728                                                      PCI_FUNC(devices[i].devfn),
729                                                      devices[i].devid);
730 
731         /*
732          * If a VFIO cdev device is resettable, all the dependent devices
733          * are either bound to same iommufd or within same iommu_groups as
734          * one of the iommufd bound devices.
735          */
736         assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED);
737 
738         tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
739         if (!tmp) {
740             continue;
741         }
742 
743         if (single) {
744             ret = -EINVAL;
745             goto out_single;
746         }
747         vfio_pci_pre_reset(tmp);
748         tmp->vbasedev.needs_reset = false;
749         multi = true;
750     }
751 
752     if (!single && !multi) {
753         ret = -EINVAL;
754         goto out_single;
755     }
756 
757     /* Use zero length array for hot reset with iommufd backend */
758     reset = g_malloc0(sizeof(*reset));
759     reset->argsz = sizeof(*reset);
760 
761      /* Bus reset! */
762     ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset);
763     g_free(reset);
764     if (ret) {
765         ret = -errno;
766     }
767 
768     trace_vfio_pci_hot_reset_result(vdev->vbasedev.name,
769                                     ret ? strerror(errno) : "Success");
770 
771     /* Re-enable INTx on affected devices */
772     for (i = 0; i < info->count; i++) {
773         VFIOPCIDevice *tmp;
774 
775         tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
776         if (!tmp) {
777             continue;
778         }
779         vfio_pci_post_reset(tmp);
780     }
781 out_single:
782     if (!single) {
783         vfio_pci_post_reset(vdev);
784     }
785     g_free(info);
786 
787     return ret;
788 }
789 
vfio_iommu_iommufd_class_init(ObjectClass * klass,void * data)790 static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
791 {
792     VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
793 
794     vioc->hiod_typename = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO;
795 
796     vioc->dma_map = iommufd_cdev_map;
797     vioc->dma_unmap = iommufd_cdev_unmap;
798     vioc->attach_device = iommufd_cdev_attach;
799     vioc->detach_device = iommufd_cdev_detach;
800     vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
801     vioc->set_dirty_page_tracking = iommufd_set_dirty_page_tracking;
802     vioc->query_dirty_bitmap = iommufd_query_dirty_bitmap;
803 };
804 
hiod_iommufd_vfio_realize(HostIOMMUDevice * hiod,void * opaque,Error ** errp)805 static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
806                                       Error **errp)
807 {
808     VFIODevice *vdev = opaque;
809     HostIOMMUDeviceCaps *caps = &hiod->caps;
810     enum iommu_hw_info_type type;
811     union {
812         struct iommu_hw_info_vtd vtd;
813     } data;
814     uint64_t hw_caps;
815 
816     hiod->agent = opaque;
817 
818     if (!iommufd_backend_get_device_info(vdev->iommufd, vdev->devid,
819                                          &type, &data, sizeof(data),
820                                          &hw_caps, errp)) {
821         return false;
822     }
823 
824     hiod->name = g_strdup(vdev->name);
825     caps->type = type;
826     caps->hw_caps = hw_caps;
827 
828     return true;
829 }
830 
831 static GList *
hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice * hiod)832 hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod)
833 {
834     VFIODevice *vdev = hiod->agent;
835 
836     g_assert(vdev);
837     return vfio_container_get_iova_ranges(vdev->bcontainer);
838 }
839 
840 static uint64_t
hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice * hiod)841 hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod)
842 {
843     VFIODevice *vdev = hiod->agent;
844 
845     g_assert(vdev);
846     return vfio_container_get_page_size_mask(vdev->bcontainer);
847 }
848 
849 
hiod_iommufd_vfio_class_init(ObjectClass * oc,void * data)850 static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
851 {
852     HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
853 
854     hiodc->realize = hiod_iommufd_vfio_realize;
855     hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
856     hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
857 };
858 
859 static const TypeInfo types[] = {
860     {
861         .name = TYPE_VFIO_IOMMU_IOMMUFD,
862         .parent = TYPE_VFIO_IOMMU,
863         .instance_size = sizeof(VFIOIOMMUFDContainer),
864         .class_init = vfio_iommu_iommufd_class_init,
865     }, {
866         .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO,
867         .parent = TYPE_HOST_IOMMU_DEVICE_IOMMUFD,
868         .class_init = hiod_iommufd_vfio_class_init,
869     }
870 };
871 
872 DEFINE_TYPES(types)
873