13
#include "qemu/osdep.h"
15
#include <linux/vfio.h>
16
#include <linux/iommufd.h>
18
#include "hw/vfio/vfio-common.h"
19
#include "qemu/error-report.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"
28
#include "exec/ram_addr.h"
30
static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
31
ram_addr_t size, void *vaddr, bool readonly)
33
const VFIOIOMMUFDContainer *container =
34
container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
36
return iommufd_backend_map_dma(container->be,
38
iova, size, vaddr, readonly);
41
static int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer,
42
hwaddr iova, ram_addr_t size,
45
const VFIOIOMMUFDContainer *container =
46
container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
49
return iommufd_backend_unmap_dma(container->be,
50
container->ioas_id, iova, size);
53
static bool iommufd_cdev_kvm_device_add(VFIODevice *vbasedev, Error **errp)
55
return !vfio_kvm_device_add_fd(vbasedev->fd, errp);
58
static void iommufd_cdev_kvm_device_del(VFIODevice *vbasedev)
62
if (vfio_kvm_device_del_fd(vbasedev->fd, &err)) {
63
error_report_err(err);
67
static bool iommufd_cdev_connect_and_bind(VFIODevice *vbasedev, Error **errp)
69
IOMMUFDBackend *iommufd = vbasedev->iommufd;
70
struct vfio_device_bind_iommufd bind = {
71
.argsz = sizeof(bind),
75
if (!iommufd_backend_connect(iommufd, errp)) {
84
if (!iommufd_cdev_kvm_device_add(vbasedev, errp)) {
85
goto err_kvm_device_add;
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);
96
vbasedev->devid = bind.out_devid;
97
trace_iommufd_cdev_connect_and_bind(bind.iommufd, vbasedev->name,
98
vbasedev->fd, vbasedev->devid);
101
iommufd_cdev_kvm_device_del(vbasedev);
103
iommufd_backend_disconnect(iommufd);
107
static void iommufd_cdev_unbind_and_disconnect(VFIODevice *vbasedev)
110
iommufd_cdev_kvm_device_del(vbasedev);
111
iommufd_backend_disconnect(vbasedev->iommufd);
114
static bool iommufd_hwpt_dirty_tracking(VFIOIOASHwpt *hwpt)
116
return hwpt && hwpt->hwpt_flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
119
static int iommufd_set_dirty_page_tracking(const VFIOContainerBase *bcontainer,
120
bool start, Error **errp)
122
const VFIOIOMMUFDContainer *container =
123
container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
126
QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
127
if (!iommufd_hwpt_dirty_tracking(hwpt)) {
131
if (!iommufd_backend_set_dirty_tracking(container->be,
132
hwpt->hwpt_id, start, errp)) {
140
QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
141
if (!iommufd_hwpt_dirty_tracking(hwpt)) {
144
iommufd_backend_set_dirty_tracking(container->be,
145
hwpt->hwpt_id, !start, NULL);
150
static int iommufd_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
151
VFIOBitmap *vbmap, hwaddr iova,
152
hwaddr size, Error **errp)
154
VFIOIOMMUFDContainer *container = container_of(bcontainer,
155
VFIOIOMMUFDContainer,
157
unsigned long page_size = qemu_real_host_page_size();
160
QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
161
if (!iommufd_hwpt_dirty_tracking(hwpt)) {
165
if (!iommufd_backend_get_dirty_bitmap(container->be, hwpt->hwpt_id,
166
iova, size, page_size,
167
(uint64_t *)vbmap->bitmap,
176
static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
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;
185
g_autofree gchar *contents = NULL;
190
path = g_strdup_printf("%s/vfio-dev", sysfs_path);
193
error_setg_errno(errp, errno, "couldn't open directory %s", path);
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);
204
if (!vfio_dev_path) {
205
error_setg(errp, "failed to find vfio-dev/vfioX/dev");
209
if (!g_file_get_contents(vfio_dev_path, &contents, &length, NULL)) {
210
error_setg(errp, "failed to load \"%s\"", vfio_dev_path);
214
if (sscanf(contents, "%d:%d", &major, &minor) != 2) {
215
error_setg(errp, "failed to get major:minor for \"%s\"", vfio_dev_path);
218
vfio_devt = makedev(major, minor);
220
vfio_path = g_strdup_printf("/dev/vfio/devices/%s", dent->d_name);
221
ret = open_cdev(vfio_path, vfio_devt);
223
error_setg(errp, "Failed to open %s", vfio_path);
226
trace_iommufd_cdev_getfd(vfio_path, ret);
232
error_prepend(errp, VFIO_MSG_PREFIX, path);
238
static int iommufd_cdev_attach_ioas_hwpt(VFIODevice *vbasedev, uint32_t id,
241
int iommufd = vbasedev->iommufd->fd;
242
struct vfio_device_attach_iommufd_pt attach_data = {
243
.argsz = sizeof(attach_data),
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);
256
trace_iommufd_cdev_attach_ioas_hwpt(iommufd, vbasedev->name,
261
static bool iommufd_cdev_detach_ioas_hwpt(VFIODevice *vbasedev, Error **errp)
263
int iommufd = vbasedev->iommufd->fd;
264
struct vfio_device_detach_iommufd_pt detach_data = {
265
.argsz = sizeof(detach_data),
269
if (ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_data)) {
270
error_setg_errno(errp, errno, "detach %s failed", vbasedev->name);
274
trace_iommufd_cdev_detach_ioas_hwpt(iommufd, vbasedev->name);
278
static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
279
VFIOIOMMUFDContainer *container,
283
IOMMUFDBackend *iommufd = vbasedev->iommufd;
290
QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
291
ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
294
if (ret == -EINVAL) {
307
vbasedev->hwpt = hwpt;
308
QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next);
309
vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt);
321
if (vbasedev->hiod->caps.hw_caps & IOMMU_HW_CAP_DIRTY_TRACKING) {
322
flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
325
if (!iommufd_backend_alloc_hwpt(iommufd, vbasedev->devid,
326
container->ioas_id, flags,
327
IOMMU_HWPT_DATA_NONE, 0, NULL,
332
hwpt = g_malloc0(sizeof(*hwpt));
333
hwpt->hwpt_id = hwpt_id;
334
hwpt->hwpt_flags = flags;
335
QLIST_INIT(&hwpt->device_list);
337
ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
339
iommufd_backend_free_id(container->be, hwpt->hwpt_id);
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",
358
static void iommufd_cdev_autodomains_put(VFIODevice *vbasedev,
359
VFIOIOMMUFDContainer *container)
361
VFIOIOASHwpt *hwpt = vbasedev->hwpt;
363
QLIST_REMOVE(vbasedev, hwpt_next);
364
vbasedev->hwpt = NULL;
366
if (QLIST_EMPTY(&hwpt->device_list)) {
367
QLIST_REMOVE(hwpt, next);
368
iommufd_backend_free_id(container->be, hwpt->hwpt_id);
373
static bool iommufd_cdev_attach_container(VFIODevice *vbasedev,
374
VFIOIOMMUFDContainer *container,
378
if (!vbasedev->mdev) {
379
return iommufd_cdev_autodomains_get(vbasedev, container, errp);
382
return !iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp);
385
static void iommufd_cdev_detach_container(VFIODevice *vbasedev,
386
VFIOIOMMUFDContainer *container)
390
if (!iommufd_cdev_detach_ioas_hwpt(vbasedev, &err)) {
391
error_report_err(err);
394
if (vbasedev->hwpt) {
395
iommufd_cdev_autodomains_put(vbasedev, container);
400
static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container)
402
VFIOContainerBase *bcontainer = &container->bcontainer;
404
if (!QLIST_EMPTY(&bcontainer->device_list)) {
407
memory_listener_unregister(&bcontainer->listener);
408
iommufd_backend_free_id(container->be, container->ioas_id);
409
object_unref(container);
412
static int iommufd_cdev_ram_block_discard_disable(bool state)
417
return ram_block_uncoordinated_discard_disable(state);
420
static bool iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container,
421
uint32_t ioas_id, Error **errp)
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;
428
info = g_malloc0(sizeof(*info));
429
info->size = sizeof(*info);
430
info->ioas_id = ioas_id;
432
if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info) && errno != EMSGSIZE) {
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);
440
if (ioctl(fd, IOMMU_IOAS_IOVA_RANGES, info)) {
444
iova_ranges = (struct iommu_iova_range *)(uintptr_t)info->allowed_iovas;
446
for (int i = 0; i < info->num_iovas; i++) {
447
Range *range = g_new(Range, 1);
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);
453
bcontainer->pgsizes = info->out_iova_alignment;
458
error_setg_errno(errp, errno, "Cannot get IOVA ranges");
462
static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
463
AddressSpace *as, Error **errp)
465
VFIOContainerBase *bcontainer;
466
VFIOIOMMUFDContainer *container;
467
VFIOAddressSpace *space;
468
struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
472
const VFIOIOMMUClass *iommufd_vioc =
473
VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
475
if (vbasedev->fd < 0) {
476
devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp);
480
vbasedev->fd = devfd;
482
devfd = vbasedev->fd;
485
if (!iommufd_cdev_connect_and_bind(vbasedev, errp)) {
486
goto err_connect_bind;
489
space = vfio_get_address_space(as);
498
if (!vfio_device_hiod_realize(vbasedev, errp)) {
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) {
509
if (!iommufd_cdev_attach_container(vbasedev, container, &err)) {
510
const char *msg = error_get_pretty(err);
512
trace_iommufd_cdev_fail_attach_existing_container(msg);
516
ret = iommufd_cdev_ram_block_discard_disable(true);
519
"Cannot set discarding of RAM broken (%d)", ret);
520
goto err_discard_disable;
522
goto found_container;
527
if (!iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp)) {
531
trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id);
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);
538
bcontainer = &container->bcontainer;
539
vfio_address_space_insert(space, bcontainer);
541
if (!iommufd_cdev_attach_container(vbasedev, container, errp)) {
542
goto err_attach_container;
545
ret = iommufd_cdev_ram_block_discard_disable(true);
547
goto err_discard_disable;
550
if (!iommufd_cdev_get_info_iova_range(container, ioas_id, &err)) {
551
error_append_hint(&err,
552
"Fallback to default 64bit IOVA range and 4K page size\n");
553
warn_report_err(err);
555
bcontainer->pgsizes = qemu_real_host_page_size();
558
bcontainer->listener = vfio_memory_listener;
559
memory_listener_register(&bcontainer->listener, bcontainer->space->as);
561
if (bcontainer->error) {
562
error_propagate_prepend(errp, bcontainer->error,
563
"memory listener initialization failed: ");
564
goto err_listener_register;
567
bcontainer->initialized = true;
570
ret = ioctl(devfd, VFIO_DEVICE_GET_INFO, &dev_info);
572
error_setg_errno(errp, errno, "error getting device info");
573
goto err_listener_register;
576
if (!vfio_cpr_register_container(bcontainer, errp)) {
577
goto err_listener_register;
584
if (vbasedev->ram_block_discard_allowed) {
585
iommufd_cdev_ram_block_discard_disable(false);
589
vbasedev->num_irqs = dev_info.num_irqs;
590
vbasedev->num_regions = dev_info.num_regions;
591
vbasedev->flags = dev_info.flags;
592
vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
593
vbasedev->bcontainer = bcontainer;
594
QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
595
QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
597
trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
598
vbasedev->num_regions, vbasedev->flags);
601
err_listener_register:
602
iommufd_cdev_ram_block_discard_disable(false);
604
iommufd_cdev_detach_container(vbasedev, container);
606
iommufd_cdev_container_destroy(container);
608
vfio_put_address_space(space);
609
iommufd_cdev_unbind_and_disconnect(vbasedev);
615
static void iommufd_cdev_detach(VFIODevice *vbasedev)
617
VFIOContainerBase *bcontainer = vbasedev->bcontainer;
618
VFIOAddressSpace *space = bcontainer->space;
619
VFIOIOMMUFDContainer *container = container_of(bcontainer,
620
VFIOIOMMUFDContainer,
622
QLIST_REMOVE(vbasedev, global_next);
623
QLIST_REMOVE(vbasedev, container_next);
624
vbasedev->bcontainer = NULL;
626
if (!vbasedev->ram_block_discard_allowed) {
627
iommufd_cdev_ram_block_discard_disable(false);
630
vfio_cpr_unregister_container(bcontainer);
631
iommufd_cdev_detach_container(vbasedev, container);
632
iommufd_cdev_container_destroy(container);
633
vfio_put_address_space(space);
635
iommufd_cdev_unbind_and_disconnect(vbasedev);
639
static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid)
641
VFIODevice *vbasedev_iter;
642
const VFIOIOMMUClass *iommufd_vioc =
643
VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
645
QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) {
646
if (VFIO_IOMMU_GET_CLASS(vbasedev_iter->bcontainer) != iommufd_vioc) {
649
if (devid == vbasedev_iter->devid) {
650
return vbasedev_iter;
656
static VFIOPCIDevice *
657
iommufd_cdev_dep_get_realized_vpdev(struct vfio_pci_dependent_device *dep_dev,
658
VFIODevice *reset_dev)
660
VFIODevice *vbasedev_tmp;
662
if (dep_dev->devid == reset_dev->devid ||
663
dep_dev->devid == VFIO_PCI_DEVID_OWNED) {
667
vbasedev_tmp = iommufd_cdev_pci_find_by_devid(dep_dev->devid);
668
if (!vbasedev_tmp || !vbasedev_tmp->dev->realized ||
669
vbasedev_tmp->type != VFIO_DEVICE_TYPE_PCI) {
673
return container_of(vbasedev_tmp, VFIOPCIDevice, vbasedev);
676
static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single)
678
VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
679
struct vfio_pci_hot_reset_info *info = NULL;
680
struct vfio_pci_dependent_device *devices;
681
struct vfio_pci_hot_reset *reset;
685
trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi");
688
vfio_pci_pre_reset(vdev);
690
vdev->vbasedev.needs_reset = false;
692
ret = vfio_pci_get_pci_hot_reset_info(vdev, &info);
698
assert(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID);
700
devices = &info->devices[0];
702
if (!(info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) {
703
if (!vdev->has_pm_reset) {
704
for (i = 0; i < info->count; i++) {
705
if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) {
706
error_report("vfio: Cannot reset device %s, "
707
"depends on device %04x:%02x:%02x.%x "
708
"which is not owned.",
709
vdev->vbasedev.name, devices[i].segment,
710
devices[i].bus, PCI_SLOT(devices[i].devfn),
711
PCI_FUNC(devices[i].devfn));
719
trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name);
721
for (i = 0; i < info->count; i++) {
724
trace_iommufd_cdev_pci_hot_reset_dep_devices(devices[i].segment,
726
PCI_SLOT(devices[i].devfn),
727
PCI_FUNC(devices[i].devfn),
735
assert(devices[i].devid != VFIO_PCI_DEVID_NOT_OWNED);
737
tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
746
vfio_pci_pre_reset(tmp);
747
tmp->vbasedev.needs_reset = false;
751
if (!single && !multi) {
757
reset = g_malloc0(sizeof(*reset));
758
reset->argsz = sizeof(*reset);
761
ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset);
767
trace_vfio_pci_hot_reset_result(vdev->vbasedev.name,
768
ret ? strerror(errno) : "Success");
771
for (i = 0; i < info->count; i++) {
774
tmp = iommufd_cdev_dep_get_realized_vpdev(&devices[i], &vdev->vbasedev);
778
vfio_pci_post_reset(tmp);
782
vfio_pci_post_reset(vdev);
789
static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
791
VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
793
vioc->hiod_typename = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO;
795
vioc->dma_map = iommufd_cdev_map;
796
vioc->dma_unmap = iommufd_cdev_unmap;
797
vioc->attach_device = iommufd_cdev_attach;
798
vioc->detach_device = iommufd_cdev_detach;
799
vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
800
vioc->set_dirty_page_tracking = iommufd_set_dirty_page_tracking;
801
vioc->query_dirty_bitmap = iommufd_query_dirty_bitmap;
804
static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
807
VFIODevice *vdev = opaque;
808
HostIOMMUDeviceCaps *caps = &hiod->caps;
809
enum iommu_hw_info_type type;
811
struct iommu_hw_info_vtd vtd;
815
hiod->agent = opaque;
817
if (!iommufd_backend_get_device_info(vdev->iommufd, vdev->devid,
818
&type, &data, sizeof(data),
823
hiod->name = g_strdup(vdev->name);
825
caps->hw_caps = hw_caps;
831
hiod_iommufd_vfio_get_iova_ranges(HostIOMMUDevice *hiod)
833
VFIODevice *vdev = hiod->agent;
836
return vfio_container_get_iova_ranges(vdev->bcontainer);
840
hiod_iommufd_vfio_get_page_size_mask(HostIOMMUDevice *hiod)
842
VFIODevice *vdev = hiod->agent;
845
return vfio_container_get_page_size_mask(vdev->bcontainer);
849
static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
851
HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
853
hiodc->realize = hiod_iommufd_vfio_realize;
854
hiodc->get_iova_ranges = hiod_iommufd_vfio_get_iova_ranges;
855
hiodc->get_page_size_mask = hiod_iommufd_vfio_get_page_size_mask;
858
static const TypeInfo types[] = {
860
.name = TYPE_VFIO_IOMMU_IOMMUFD,
861
.parent = TYPE_VFIO_IOMMU,
862
.instance_size = sizeof(VFIOIOMMUFDContainer),
863
.class_init = vfio_iommu_iommufd_class_init,
865
.name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO,
866
.parent = TYPE_HOST_IOMMU_DEVICE_IOMMUFD,
867
.class_init = hiod_iommufd_vfio_class_init,