xref: /openbmc/qemu/hw/vfio/iommufd.c (revision 35e83a9f)
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 
29 static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
30                             ram_addr_t size, void *vaddr, bool readonly)
31 {
32     const VFIOIOMMUFDContainer *container =
33         container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
34 
35     return iommufd_backend_map_dma(container->be,
36                                    container->ioas_id,
37                                    iova, size, vaddr, readonly);
38 }
39 
40 static int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer,
41                               hwaddr iova, ram_addr_t size,
42                               IOMMUTLBEntry *iotlb)
43 {
44     const VFIOIOMMUFDContainer *container =
45         container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
46 
47     /* TODO: Handle dma_unmap_bitmap with iotlb args (migration) */
48     return iommufd_backend_unmap_dma(container->be,
49                                      container->ioas_id, iova, size);
50 }
51 
52 static int iommufd_cdev_kvm_device_add(VFIODevice *vbasedev, Error **errp)
53 {
54     return vfio_kvm_device_add_fd(vbasedev->fd, errp);
55 }
56 
57 static void iommufd_cdev_kvm_device_del(VFIODevice *vbasedev)
58 {
59     Error *err = NULL;
60 
61     if (vfio_kvm_device_del_fd(vbasedev->fd, &err)) {
62         error_report_err(err);
63     }
64 }
65 
66 static int iommufd_cdev_connect_and_bind(VFIODevice *vbasedev, Error **errp)
67 {
68     IOMMUFDBackend *iommufd = vbasedev->iommufd;
69     struct vfio_device_bind_iommufd bind = {
70         .argsz = sizeof(bind),
71         .flags = 0,
72     };
73     int ret;
74 
75     ret = iommufd_backend_connect(iommufd, errp);
76     if (ret) {
77         return ret;
78     }
79 
80     /*
81      * Add device to kvm-vfio to be prepared for the tracking
82      * in KVM. Especially for some emulated devices, it requires
83      * to have kvm information in the device open.
84      */
85     ret = iommufd_cdev_kvm_device_add(vbasedev, errp);
86     if (ret) {
87         goto err_kvm_device_add;
88     }
89 
90     /* Bind device to iommufd */
91     bind.iommufd = iommufd->fd;
92     ret = ioctl(vbasedev->fd, VFIO_DEVICE_BIND_IOMMUFD, &bind);
93     if (ret) {
94         error_setg_errno(errp, errno, "error bind device fd=%d to iommufd=%d",
95                          vbasedev->fd, bind.iommufd);
96         goto err_bind;
97     }
98 
99     vbasedev->devid = bind.out_devid;
100     trace_iommufd_cdev_connect_and_bind(bind.iommufd, vbasedev->name,
101                                         vbasedev->fd, vbasedev->devid);
102     return ret;
103 err_bind:
104     iommufd_cdev_kvm_device_del(vbasedev);
105 err_kvm_device_add:
106     iommufd_backend_disconnect(iommufd);
107     return ret;
108 }
109 
110 static void iommufd_cdev_unbind_and_disconnect(VFIODevice *vbasedev)
111 {
112     /* Unbind is automatically conducted when device fd is closed */
113     iommufd_cdev_kvm_device_del(vbasedev);
114     iommufd_backend_disconnect(vbasedev->iommufd);
115 }
116 
117 static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
118 {
119     ERRP_GUARD();
120     long int ret = -ENOTTY;
121     char *path, *vfio_dev_path = NULL, *vfio_path = NULL;
122     DIR *dir = NULL;
123     struct dirent *dent;
124     gchar *contents;
125     gsize length;
126     int major, minor;
127     dev_t vfio_devt;
128 
129     path = g_strdup_printf("%s/vfio-dev", sysfs_path);
130     dir = opendir(path);
131     if (!dir) {
132         error_setg_errno(errp, errno, "couldn't open directory %s", path);
133         goto out_free_path;
134     }
135 
136     while ((dent = readdir(dir))) {
137         if (!strncmp(dent->d_name, "vfio", 4)) {
138             vfio_dev_path = g_strdup_printf("%s/%s/dev", path, dent->d_name);
139             break;
140         }
141     }
142 
143     if (!vfio_dev_path) {
144         error_setg(errp, "failed to find vfio-dev/vfioX/dev");
145         goto out_close_dir;
146     }
147 
148     if (!g_file_get_contents(vfio_dev_path, &contents, &length, NULL)) {
149         error_setg(errp, "failed to load \"%s\"", vfio_dev_path);
150         goto out_free_dev_path;
151     }
152 
153     if (sscanf(contents, "%d:%d", &major, &minor) != 2) {
154         error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path);
155         goto out_free_dev_path;
156     }
157     g_free(contents);
158     vfio_devt = makedev(major, minor);
159 
160     vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name);
161     ret = open_cdev(vfio_path, vfio_devt);
162     if (ret < 0) {
163         error_setg(errp, "Failed to open %s", vfio_path);
164     }
165 
166     trace_iommufd_cdev_getfd(vfio_path, ret);
167     g_free(vfio_path);
168 
169 out_free_dev_path:
170     g_free(vfio_dev_path);
171 out_close_dir:
172     closedir(dir);
173 out_free_path:
174     if (*errp) {
175         error_prepend(errp, VFIO_MSG_PREFIX, path);
176     }
177     g_free(path);
178 
179     return ret;
180 }
181 
182 static int iommufd_cdev_attach_ioas_hwpt(VFIODevice *vbasedev, uint32_t id,
183                                          Error **errp)
184 {
185     int ret, iommufd = vbasedev->iommufd->fd;
186     struct vfio_device_attach_iommufd_pt attach_data = {
187         .argsz = sizeof(attach_data),
188         .flags = 0,
189         .pt_id = id,
190     };
191 
192     /* Attach device to an IOAS or hwpt within iommufd */
193     ret = ioctl(vbasedev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data);
194     if (ret) {
195         error_setg_errno(errp, errno,
196                          "[iommufd=%d] error attach %s (%d) to id=%d",
197                          iommufd, vbasedev->name, vbasedev->fd, id);
198     } else {
199         trace_iommufd_cdev_attach_ioas_hwpt(iommufd, vbasedev->name,
200                                             vbasedev->fd, id);
201     }
202     return ret;
203 }
204 
205 static int iommufd_cdev_detach_ioas_hwpt(VFIODevice *vbasedev, Error **errp)
206 {
207     int ret, iommufd = vbasedev->iommufd->fd;
208     struct vfio_device_detach_iommufd_pt detach_data = {
209         .argsz = sizeof(detach_data),
210         .flags = 0,
211     };
212 
213     ret = ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_data);
214     if (ret) {
215         error_setg_errno(errp, errno, "detach %s failed", vbasedev->name);
216     } else {
217         trace_iommufd_cdev_detach_ioas_hwpt(iommufd, vbasedev->name);
218     }
219     return ret;
220 }
221 
222 static int iommufd_cdev_attach_container(VFIODevice *vbasedev,
223                                          VFIOIOMMUFDContainer *container,
224                                          Error **errp)
225 {
226     return iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp);
227 }
228 
229 static void iommufd_cdev_detach_container(VFIODevice *vbasedev,
230                                           VFIOIOMMUFDContainer *container)
231 {
232     Error *err = NULL;
233 
234     if (iommufd_cdev_detach_ioas_hwpt(vbasedev, &err)) {
235         error_report_err(err);
236     }
237 }
238 
239 static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container)
240 {
241     VFIOContainerBase *bcontainer = &container->bcontainer;
242 
243     if (!QLIST_EMPTY(&bcontainer->device_list)) {
244         return;
245     }
246     memory_listener_unregister(&bcontainer->listener);
247     vfio_container_destroy(bcontainer);
248     iommufd_backend_free_id(container->be, container->ioas_id);
249     g_free(container);
250 }
251 
252 static int iommufd_cdev_ram_block_discard_disable(bool state)
253 {
254     /*
255      * We support coordinated discarding of RAM via the RamDiscardManager.
256      */
257     return ram_block_uncoordinated_discard_disable(state);
258 }
259 
260 static int iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container,
261                                             uint32_t ioas_id, Error **errp)
262 {
263     VFIOContainerBase *bcontainer = &container->bcontainer;
264     struct iommu_ioas_iova_ranges *info;
265     struct iommu_iova_range *iova_ranges;
266     int ret, sz, fd = container->be->fd;
267 
268     info = g_malloc0(sizeof(*info));
269     info->size = sizeof(*info);
270     info->ioas_id = ioas_id;
271 
272     ret = ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info);
273     if (ret && errno != EMSGSIZE) {
274         goto error;
275     }
276 
277     sz = info->num_iovas * sizeof(struct iommu_iova_range);
278     info = g_realloc(info, sizeof(*info) + sz);
279     info->allowed_iovas = (uintptr_t)(info + 1);
280 
281     ret = ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info);
282     if (ret) {
283         goto error;
284     }
285 
286     iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas;
287 
288     for (int i = 0; i < info->num_iovas; i++) {
289         Range *range = g_new(Range, 1);
290 
291         range_set_bounds(range, iova_ranges[i].start, iova_ranges[i].last);
292         bcontainer->iova_ranges =
293             range_list_insert(bcontainer->iova_ranges, range);
294     }
295     bcontainer->pgsizes = info->out_iova_alignment;
296 
297     g_free(info);
298     return 0;
299 
300 error:
301     ret = -errno;
302     g_free(info);
303     error_setg_errno(errp, errno, "Cannot get IOVA ranges");
304     return ret;
305 }
306 
307 static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
308                                AddressSpace *as, Error **errp)
309 {
310     VFIOContainerBase *bcontainer;
311     VFIOIOMMUFDContainer *container;
312     VFIOAddressSpace *space;
313     struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
314     int ret, devfd;
315     uint32_t ioas_id;
316     Error *err = NULL;
317     const VFIOIOMMUClass *iommufd_vioc =
318         VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
319 
320     if (vbasedev->fd < 0) {
321         devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp);
322         if (devfd < 0) {
323             return devfd;
324         }
325         vbasedev->fd = devfd;
326     } else {
327         devfd = vbasedev->fd;
328     }
329 
330     ret = iommufd_cdev_connect_and_bind(vbasedev, errp);
331     if (ret) {
332         goto err_connect_bind;
333     }
334 
335     space = vfio_get_address_space(as);
336 
337     /* try to attach to an existing container in this space */
338     QLIST_FOREACH(bcontainer, &space->containers, next) {
339         container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
340         if (bcontainer->ops != iommufd_vioc ||
341             vbasedev->iommufd != container->be) {
342             continue;
343         }
344         if (iommufd_cdev_attach_container(vbasedev, container, &err)) {
345             const char *msg = error_get_pretty(err);
346 
347             trace_iommufd_cdev_fail_attach_existing_container(msg);
348             error_free(err);
349             err = NULL;
350         } else {
351             ret = iommufd_cdev_ram_block_discard_disable(true);
352             if (ret) {
353                 error_setg(errp,
354                               "Cannot set discarding of RAM broken (%d)", ret);
355                 goto err_discard_disable;
356             }
357             goto found_container;
358         }
359     }
360 
361     /* Need to allocate a new dedicated container */
362     ret = iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp);
363     if (ret < 0) {
364         goto err_alloc_ioas;
365     }
366 
367     trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id);
368 
369     container = g_malloc0(sizeof(*container));
370     container->be = vbasedev->iommufd;
371     container->ioas_id = ioas_id;
372 
373     bcontainer = &container->bcontainer;
374     vfio_container_init(bcontainer, space, iommufd_vioc);
375     QLIST_INSERT_HEAD(&space->containers, bcontainer, next);
376 
377     ret = iommufd_cdev_attach_container(vbasedev, container, errp);
378     if (ret) {
379         goto err_attach_container;
380     }
381 
382     ret = iommufd_cdev_ram_block_discard_disable(true);
383     if (ret) {
384         goto err_discard_disable;
385     }
386 
387     ret = iommufd_cdev_get_info_iova_range(container, ioas_id, &err);
388     if (ret) {
389         error_append_hint(&err,
390                    "Fallback to default 64bit IOVA range and 4K page size\n");
391         warn_report_err(err);
392         err = NULL;
393         bcontainer->pgsizes = qemu_real_host_page_size();
394     }
395 
396     bcontainer->listener = vfio_memory_listener;
397     memory_listener_register(&bcontainer->listener, bcontainer->space->as);
398 
399     if (bcontainer->error) {
400         ret = -1;
401         error_propagate_prepend(errp, bcontainer->error,
402                                 "memory listener initialization failed: ");
403         goto err_listener_register;
404     }
405 
406     bcontainer->initialized = true;
407 
408 found_container:
409     ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info);
410     if (ret) {
411         error_setg_errno(errp, errno, "error getting device info");
412         goto err_listener_register;
413     }
414 
415     /*
416      * TODO: examine RAM_BLOCK_DISCARD stuff, should we do group level
417      * for discarding incompatibility check as well?
418      */
419     if (vbasedev->ram_block_discard_allowed) {
420         iommufd_cdev_ram_block_discard_disable(false);
421     }
422 
423     vbasedev->group = 0;
424     vbasedev->num_irqs = dev_info.num_irqs;
425     vbasedev->num_regions = dev_info.num_regions;
426     vbasedev->flags = dev_info.flags;
427     vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
428     vbasedev->bcontainer = bcontainer;
429     QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
430     QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
431 
432     trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
433                                    vbasedev->num_regions, vbasedev->flags);
434     return 0;
435 
436 err_listener_register:
437     iommufd_cdev_ram_block_discard_disable(false);
438 err_discard_disable:
439     iommufd_cdev_detach_container(vbasedev, container);
440 err_attach_container:
441     iommufd_cdev_container_destroy(container);
442 err_alloc_ioas:
443     vfio_put_address_space(space);
444     iommufd_cdev_unbind_and_disconnect(vbasedev);
445 err_connect_bind:
446     close(vbasedev->fd);
447     return ret;
448 }
449 
450 static void iommufd_cdev_detach(VFIODevice *vbasedev)
451 {
452     VFIOContainerBase *bcontainer = vbasedev->bcontainer;
453     VFIOAddressSpace *space = bcontainer->space;
454     VFIOIOMMUFDContainer *container = container_of(bcontainer,
455                                                    VFIOIOMMUFDContainer,
456                                                    bcontainer);
457     QLIST_REMOVE(vbasedev, global_next);
458     QLIST_REMOVE(vbasedev, container_next);
459     vbasedev->bcontainer = NULL;
460 
461     if (!vbasedev->ram_block_discard_allowed) {
462         iommufd_cdev_ram_block_discard_disable(false);
463     }
464 
465     iommufd_cdev_detach_container(vbasedev, container);
466     iommufd_cdev_container_destroy(container);
467     vfio_put_address_space(space);
468 
469     iommufd_cdev_unbind_and_disconnect(vbasedev);
470     close(vbasedev->fd);
471 }
472 
473 static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid)
474 {
475     VFIODevice *vbasedev_iter;
476     const VFIOIOMMUClass *iommufd_vioc =
477         VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
478 
479     QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) {
480         if (vbasedev_iter->bcontainer->ops != iommufd_vioc) {
481             continue;
482         }
483         if (devid == vbasedev_iter->devid) {
484             return vbasedev_iter;
485         }
486     }
487     return NULL;
488 }
489 
490 static VFIOPCIDevice *
491 iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev,
492                                     VFIODevice *reset_dev)
493 {
494     VFIODevice *vbasedev_tmp;
495 
496     if (dep_dev->devid == reset_dev->devid ||
497         dep_dev->devid == VFIO_PCI_DEVID_OWNED) {
498         return NULL;
499     }
500 
501     vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid);
502     if (!vbasedev_tmp || !vbasedev_tmp->dev->realized ||
503         vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) {
504         return NULL;
505     }
506 
507     return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev);
508 }
509 
510 static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single)
511 {
512     VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
513     struct vfio_pci_hot_reset_info *info = NULL;
514     struct vfio_pci_dependent_device *devices;
515     struct vfio_pci_hot_reset *reset;
516     int ret, i;
517     bool multi = false;
518 
519     trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi");
520 
521     if (!single) {
522         vfio_pci_pre_reset(vdev);
523     }
524     vdev->vbasedev.needs_reset = false;
525 
526     ret = vfio_pci_get_pci_hot_reset_info(vdev, &info);
527 
528     if (ret) {
529         goto out_single;
530     }
531 
532     assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID);
533 
534     devices = &info->devices[0];
535 
536     if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) {
537         if (!vdev->has_pm_reset) {
538             for (i = 0; i < info->count; i++) {
539                 if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) {
540                     error_report("vfio: Cannot reset device %s, "
541                                  "depends on device %04x:%02x:%02x.%x "
542                                  "which is not owned.",
543                                  vdev->vbasedev.name, devices[i].segment,
544                                  devices[i].bus, PCI_SLOT(devices[i].devfn),
545                                  PCI_FUNC(devices[i].devfn));
546                 }
547             }
548         }
549         ret = -EPERM;
550         goto out_single;
551     }
552 
553     trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name);
554 
555     for (i = 0; i < info->count; i++) {
556         VFIOPCIDevice *tmp;
557 
558         trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment,
559                                                      devices[i].bus,
560                                                      PCI_SLOT(devices[i].devfn),
561                                                      PCI_FUNC(devices[i].devfn),
562                                                      devices[i].devid);
563 
564         /*
565          * If a VFIO cdev device is resettable, all the dependent devices
566          * are either bound to same iommufd or within same iommu_groups as
567          * one of the iommufd bound devices.
568          */
569         assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED);
570 
571         tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
572         if (!tmp) {
573             continue;
574         }
575 
576         if (single) {
577             ret = -EINVAL;
578             goto out_single;
579         }
580         vfio_pci_pre_reset(tmp);
581         tmp->vbasedev.needs_reset = false;
582         multi = true;
583     }
584 
585     if (!single && !multi) {
586         ret = -EINVAL;
587         goto out_single;
588     }
589 
590     /* Use zero length array for hot reset with iommufd backend */
591     reset = g_malloc0(sizeof(*reset));
592     reset->argsz = sizeof(*reset);
593 
594      /* Bus reset! */
595     ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset);
596     g_free(reset);
597     if (ret) {
598         ret = -errno;
599     }
600 
601     trace_vfio_pci_hot_reset_result(vdev->vbasedev.name,
602                                     ret ? strerror(errno) : "Success");
603 
604     /* Re-enable INTx on affected devices */
605     for (i = 0; i < info->count; i++) {
606         VFIOPCIDevice *tmp;
607 
608         tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
609         if (!tmp) {
610             continue;
611         }
612         vfio_pci_post_reset(tmp);
613     }
614 out_single:
615     if (!single) {
616         vfio_pci_post_reset(vdev);
617     }
618     g_free(info);
619 
620     return ret;
621 }
622 
623 static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
624 {
625     VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
626 
627     vioc->dma_map = iommufd_cdev_map;
628     vioc->dma_unmap = iommufd_cdev_unmap;
629     vioc->attach_device = iommufd_cdev_attach;
630     vioc->detach_device = iommufd_cdev_detach;
631     vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
632 };
633 
634 static const TypeInfo types[] = {
635     {
636         .name = TYPE_VFIO_IOMMU_IOMMUFD,
637         .parent = TYPE_VFIO_IOMMU,
638         .class_init = vfio_iommu_iommufd_class_init,
639     },
640 };
641 
642 DEFINE_TYPES(types)
643