16e6d8ac6SEric Auger /* 26e6d8ac6SEric Auger * iommufd container backend 36e6d8ac6SEric Auger * 46e6d8ac6SEric Auger * Copyright (C) 2023 Intel Corporation. 56e6d8ac6SEric Auger * Copyright Red Hat, Inc. 2023 66e6d8ac6SEric Auger * 76e6d8ac6SEric Auger * Authors: Yi Liu <yi.l.liu@intel.com> 86e6d8ac6SEric Auger * Eric Auger <eric.auger@redhat.com> 96e6d8ac6SEric Auger * 106e6d8ac6SEric Auger * SPDX-License-Identifier: GPL-2.0-or-later 116e6d8ac6SEric Auger */ 126e6d8ac6SEric Auger 136e6d8ac6SEric Auger #include "qemu/osdep.h" 146e6d8ac6SEric Auger #include "sysemu/iommufd.h" 156e6d8ac6SEric Auger #include "qapi/error.h" 166e6d8ac6SEric Auger #include "qemu/module.h" 176e6d8ac6SEric Auger #include "qom/object_interfaces.h" 186e6d8ac6SEric Auger #include "qemu/error-report.h" 196e6d8ac6SEric Auger #include "monitor/monitor.h" 206e6d8ac6SEric Auger #include "trace.h" 216e6d8ac6SEric Auger #include <sys/ioctl.h> 226e6d8ac6SEric Auger #include <linux/iommufd.h> 236e6d8ac6SEric Auger 246e6d8ac6SEric Auger static void iommufd_backend_init(Object *obj) 256e6d8ac6SEric Auger { 266e6d8ac6SEric Auger IOMMUFDBackend *be = IOMMUFD_BACKEND(obj); 276e6d8ac6SEric Auger 286e6d8ac6SEric Auger be->fd = -1; 296e6d8ac6SEric Auger be->users = 0; 306e6d8ac6SEric Auger be->owned = true; 316e6d8ac6SEric Auger } 326e6d8ac6SEric Auger 336e6d8ac6SEric Auger static void iommufd_backend_finalize(Object *obj) 346e6d8ac6SEric Auger { 356e6d8ac6SEric Auger IOMMUFDBackend *be = IOMMUFD_BACKEND(obj); 366e6d8ac6SEric Auger 376e6d8ac6SEric Auger if (be->owned) { 386e6d8ac6SEric Auger close(be->fd); 396e6d8ac6SEric Auger be->fd = -1; 406e6d8ac6SEric Auger } 416e6d8ac6SEric Auger } 426e6d8ac6SEric Auger 436e6d8ac6SEric Auger static void iommufd_backend_set_fd(Object *obj, const char *str, Error **errp) 446e6d8ac6SEric Auger { 45c1cccad8SZhao Liu ERRP_GUARD(); 466e6d8ac6SEric Auger IOMMUFDBackend *be = IOMMUFD_BACKEND(obj); 476e6d8ac6SEric Auger int fd = -1; 486e6d8ac6SEric Auger 496e6d8ac6SEric Auger fd = monitor_fd_param(monitor_cur(), str, errp); 506e6d8ac6SEric Auger if (fd == -1) { 516e6d8ac6SEric Auger error_prepend(errp, "Could not parse remote object fd %s:", str); 526e6d8ac6SEric Auger return; 536e6d8ac6SEric Auger } 546e6d8ac6SEric Auger be->fd = fd; 556e6d8ac6SEric Auger be->owned = false; 566e6d8ac6SEric Auger trace_iommu_backend_set_fd(be->fd); 576e6d8ac6SEric Auger } 586e6d8ac6SEric Auger 596e6d8ac6SEric Auger static bool iommufd_backend_can_be_deleted(UserCreatable *uc) 606e6d8ac6SEric Auger { 616e6d8ac6SEric Auger IOMMUFDBackend *be = IOMMUFD_BACKEND(uc); 626e6d8ac6SEric Auger 636e6d8ac6SEric Auger return !be->users; 646e6d8ac6SEric Auger } 656e6d8ac6SEric Auger 666e6d8ac6SEric Auger static void iommufd_backend_class_init(ObjectClass *oc, void *data) 676e6d8ac6SEric Auger { 686e6d8ac6SEric Auger UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); 696e6d8ac6SEric Auger 706e6d8ac6SEric Auger ucc->can_be_deleted = iommufd_backend_can_be_deleted; 716e6d8ac6SEric Auger 726e6d8ac6SEric Auger object_class_property_add_str(oc, "fd", NULL, iommufd_backend_set_fd); 736e6d8ac6SEric Auger } 746e6d8ac6SEric Auger 759067d50dSZhenzhong Duan bool iommufd_backend_connect(IOMMUFDBackend *be, Error **errp) 766e6d8ac6SEric Auger { 779067d50dSZhenzhong Duan int fd; 786e6d8ac6SEric Auger 796e6d8ac6SEric Auger if (be->owned && !be->users) { 8047cd2f1aSZhao Liu fd = qemu_open("/dev/iommu", O_RDWR, errp); 816e6d8ac6SEric Auger if (fd < 0) { 829067d50dSZhenzhong Duan return false; 836e6d8ac6SEric Auger } 846e6d8ac6SEric Auger be->fd = fd; 856e6d8ac6SEric Auger } 866e6d8ac6SEric Auger be->users++; 879067d50dSZhenzhong Duan 889067d50dSZhenzhong Duan trace_iommufd_backend_connect(be->fd, be->owned, be->users); 899067d50dSZhenzhong Duan return true; 906e6d8ac6SEric Auger } 916e6d8ac6SEric Auger 926e6d8ac6SEric Auger void iommufd_backend_disconnect(IOMMUFDBackend *be) 936e6d8ac6SEric Auger { 946e6d8ac6SEric Auger if (!be->users) { 956e6d8ac6SEric Auger goto out; 966e6d8ac6SEric Auger } 976e6d8ac6SEric Auger be->users--; 986e6d8ac6SEric Auger if (!be->users && be->owned) { 996e6d8ac6SEric Auger close(be->fd); 1006e6d8ac6SEric Auger be->fd = -1; 1016e6d8ac6SEric Auger } 1026e6d8ac6SEric Auger out: 1036e6d8ac6SEric Auger trace_iommufd_backend_disconnect(be->fd, be->users); 1046e6d8ac6SEric Auger } 1056e6d8ac6SEric Auger 1069067d50dSZhenzhong Duan bool iommufd_backend_alloc_ioas(IOMMUFDBackend *be, uint32_t *ioas_id, 1076e6d8ac6SEric Auger Error **errp) 1086e6d8ac6SEric Auger { 1099067d50dSZhenzhong Duan int fd = be->fd; 1106e6d8ac6SEric Auger struct iommu_ioas_alloc alloc_data = { 1116e6d8ac6SEric Auger .size = sizeof(alloc_data), 1126e6d8ac6SEric Auger .flags = 0, 1136e6d8ac6SEric Auger }; 1146e6d8ac6SEric Auger 1159067d50dSZhenzhong Duan if (ioctl(fd, IOMMU_IOAS_ALLOC, &alloc_data)) { 1166e6d8ac6SEric Auger error_setg_errno(errp, errno, "Failed to allocate ioas"); 1179067d50dSZhenzhong Duan return false; 1186e6d8ac6SEric Auger } 1196e6d8ac6SEric Auger 1206e6d8ac6SEric Auger *ioas_id = alloc_data.out_ioas_id; 1219067d50dSZhenzhong Duan trace_iommufd_backend_alloc_ioas(fd, *ioas_id); 1226e6d8ac6SEric Auger 1239067d50dSZhenzhong Duan return true; 1246e6d8ac6SEric Auger } 1256e6d8ac6SEric Auger 1266e6d8ac6SEric Auger void iommufd_backend_free_id(IOMMUFDBackend *be, uint32_t id) 1276e6d8ac6SEric Auger { 1286e6d8ac6SEric Auger int ret, fd = be->fd; 1296e6d8ac6SEric Auger struct iommu_destroy des = { 1306e6d8ac6SEric Auger .size = sizeof(des), 1316e6d8ac6SEric Auger .id = id, 1326e6d8ac6SEric Auger }; 1336e6d8ac6SEric Auger 1346e6d8ac6SEric Auger ret = ioctl(fd, IOMMU_DESTROY, &des); 1356e6d8ac6SEric Auger trace_iommufd_backend_free_id(fd, id, ret); 1366e6d8ac6SEric Auger if (ret) { 1376e6d8ac6SEric Auger error_report("Failed to free id: %u %m", id); 1386e6d8ac6SEric Auger } 1396e6d8ac6SEric Auger } 1406e6d8ac6SEric Auger 1416e6d8ac6SEric Auger int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova, 1426e6d8ac6SEric Auger ram_addr_t size, void *vaddr, bool readonly) 1436e6d8ac6SEric Auger { 1446e6d8ac6SEric Auger int ret, fd = be->fd; 1456e6d8ac6SEric Auger struct iommu_ioas_map map = { 1466e6d8ac6SEric Auger .size = sizeof(map), 1476e6d8ac6SEric Auger .flags = IOMMU_IOAS_MAP_READABLE | 1486e6d8ac6SEric Auger IOMMU_IOAS_MAP_FIXED_IOVA, 1496e6d8ac6SEric Auger .ioas_id = ioas_id, 1506e6d8ac6SEric Auger .__reserved = 0, 1516e6d8ac6SEric Auger .user_va = (uintptr_t)vaddr, 1526e6d8ac6SEric Auger .iova = iova, 1536e6d8ac6SEric Auger .length = size, 1546e6d8ac6SEric Auger }; 1556e6d8ac6SEric Auger 1566e6d8ac6SEric Auger if (!readonly) { 1576e6d8ac6SEric Auger map.flags |= IOMMU_IOAS_MAP_WRITEABLE; 1586e6d8ac6SEric Auger } 1596e6d8ac6SEric Auger 1606e6d8ac6SEric Auger ret = ioctl(fd, IOMMU_IOAS_MAP, &map); 1616e6d8ac6SEric Auger trace_iommufd_backend_map_dma(fd, ioas_id, iova, size, 1626e6d8ac6SEric Auger vaddr, readonly, ret); 1636e6d8ac6SEric Auger if (ret) { 1646e6d8ac6SEric Auger ret = -errno; 1656e6d8ac6SEric Auger 1666e6d8ac6SEric Auger /* TODO: Not support mapping hardware PCI BAR region for now. */ 1676e6d8ac6SEric Auger if (errno == EFAULT) { 1686e6d8ac6SEric Auger warn_report("IOMMU_IOAS_MAP failed: %m, PCI BAR?"); 1696e6d8ac6SEric Auger } else { 1706e6d8ac6SEric Auger error_report("IOMMU_IOAS_MAP failed: %m"); 1716e6d8ac6SEric Auger } 1726e6d8ac6SEric Auger } 1736e6d8ac6SEric Auger return ret; 1746e6d8ac6SEric Auger } 1756e6d8ac6SEric Auger 1766e6d8ac6SEric Auger int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id, 1776e6d8ac6SEric Auger hwaddr iova, ram_addr_t size) 1786e6d8ac6SEric Auger { 1796e6d8ac6SEric Auger int ret, fd = be->fd; 1806e6d8ac6SEric Auger struct iommu_ioas_unmap unmap = { 1816e6d8ac6SEric Auger .size = sizeof(unmap), 1826e6d8ac6SEric Auger .ioas_id = ioas_id, 1836e6d8ac6SEric Auger .iova = iova, 1846e6d8ac6SEric Auger .length = size, 1856e6d8ac6SEric Auger }; 1866e6d8ac6SEric Auger 1876e6d8ac6SEric Auger ret = ioctl(fd, IOMMU_IOAS_UNMAP, &unmap); 1886e6d8ac6SEric Auger /* 1896e6d8ac6SEric Auger * IOMMUFD takes mapping as some kind of object, unmapping 1906e6d8ac6SEric Auger * nonexistent mapping is treated as deleting a nonexistent 1916e6d8ac6SEric Auger * object and return ENOENT. This is different from legacy 1926e6d8ac6SEric Auger * backend which allows it. vIOMMU may trigger a lot of 1936e6d8ac6SEric Auger * redundant unmapping, to avoid flush the log, treat them 1946e6d8ac6SEric Auger * as succeess for IOMMUFD just like legacy backend. 1956e6d8ac6SEric Auger */ 1966e6d8ac6SEric Auger if (ret && errno == ENOENT) { 1976e6d8ac6SEric Auger trace_iommufd_backend_unmap_dma_non_exist(fd, ioas_id, iova, size, ret); 1986e6d8ac6SEric Auger ret = 0; 1996e6d8ac6SEric Auger } else { 2006e6d8ac6SEric Auger trace_iommufd_backend_unmap_dma(fd, ioas_id, iova, size, ret); 2016e6d8ac6SEric Auger } 2026e6d8ac6SEric Auger 2036e6d8ac6SEric Auger if (ret) { 2046e6d8ac6SEric Auger ret = -errno; 2056e6d8ac6SEric Auger error_report("IOMMU_IOAS_UNMAP failed: %m"); 2066e6d8ac6SEric Auger } 2076e6d8ac6SEric Auger return ret; 2086e6d8ac6SEric Auger } 2096e6d8ac6SEric Auger 21042965386SZhenzhong Duan bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid, 21142965386SZhenzhong Duan uint32_t *type, void *data, uint32_t len, 212*2d1bf258SJoao Martins uint64_t *caps, Error **errp) 21342965386SZhenzhong Duan { 21442965386SZhenzhong Duan struct iommu_hw_info info = { 21542965386SZhenzhong Duan .size = sizeof(info), 21642965386SZhenzhong Duan .dev_id = devid, 21742965386SZhenzhong Duan .data_len = len, 21842965386SZhenzhong Duan .data_uptr = (uintptr_t)data, 21942965386SZhenzhong Duan }; 22042965386SZhenzhong Duan 22142965386SZhenzhong Duan if (ioctl(be->fd, IOMMU_GET_HW_INFO, &info)) { 22242965386SZhenzhong Duan error_setg_errno(errp, errno, "Failed to get hardware info"); 22342965386SZhenzhong Duan return false; 22442965386SZhenzhong Duan } 22542965386SZhenzhong Duan 22642965386SZhenzhong Duan g_assert(type); 22742965386SZhenzhong Duan *type = info.out_data_type; 228*2d1bf258SJoao Martins g_assert(caps); 229*2d1bf258SJoao Martins *caps = info.out_capabilities; 23042965386SZhenzhong Duan 23142965386SZhenzhong Duan return true; 23242965386SZhenzhong Duan } 23342965386SZhenzhong Duan 23463c6e83eSZhenzhong Duan static int hiod_iommufd_get_cap(HostIOMMUDevice *hiod, int cap, Error **errp) 23563c6e83eSZhenzhong Duan { 23663c6e83eSZhenzhong Duan HostIOMMUDeviceCaps *caps = &hiod->caps; 23763c6e83eSZhenzhong Duan 23863c6e83eSZhenzhong Duan switch (cap) { 23963c6e83eSZhenzhong Duan case HOST_IOMMU_DEVICE_CAP_IOMMU_TYPE: 24063c6e83eSZhenzhong Duan return caps->type; 24163c6e83eSZhenzhong Duan case HOST_IOMMU_DEVICE_CAP_AW_BITS: 24263c6e83eSZhenzhong Duan return caps->aw_bits; 24363c6e83eSZhenzhong Duan default: 24463c6e83eSZhenzhong Duan error_setg(errp, "%s: unsupported capability %x", hiod->name, cap); 24563c6e83eSZhenzhong Duan return -EINVAL; 24663c6e83eSZhenzhong Duan } 24763c6e83eSZhenzhong Duan } 24863c6e83eSZhenzhong Duan 24963c6e83eSZhenzhong Duan static void hiod_iommufd_class_init(ObjectClass *oc, void *data) 25063c6e83eSZhenzhong Duan { 25163c6e83eSZhenzhong Duan HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc); 25263c6e83eSZhenzhong Duan 25363c6e83eSZhenzhong Duan hioc->get_cap = hiod_iommufd_get_cap; 25463c6e83eSZhenzhong Duan }; 25563c6e83eSZhenzhong Duan 2569005f928SZhenzhong Duan static const TypeInfo types[] = { 2579005f928SZhenzhong Duan { 2586e6d8ac6SEric Auger .name = TYPE_IOMMUFD_BACKEND, 2596e6d8ac6SEric Auger .parent = TYPE_OBJECT, 2606e6d8ac6SEric Auger .instance_size = sizeof(IOMMUFDBackend), 2616e6d8ac6SEric Auger .instance_init = iommufd_backend_init, 2626e6d8ac6SEric Auger .instance_finalize = iommufd_backend_finalize, 2636e6d8ac6SEric Auger .class_size = sizeof(IOMMUFDBackendClass), 2646e6d8ac6SEric Auger .class_init = iommufd_backend_class_init, 2656e6d8ac6SEric Auger .interfaces = (InterfaceInfo[]) { 2666e6d8ac6SEric Auger { TYPE_USER_CREATABLE }, 2676e6d8ac6SEric Auger { } 2686e6d8ac6SEric Auger } 2699005f928SZhenzhong Duan }, { 2709005f928SZhenzhong Duan .name = TYPE_HOST_IOMMU_DEVICE_IOMMUFD, 2719005f928SZhenzhong Duan .parent = TYPE_HOST_IOMMU_DEVICE, 27263c6e83eSZhenzhong Duan .class_init = hiod_iommufd_class_init, 2739005f928SZhenzhong Duan .abstract = true, 2749005f928SZhenzhong Duan } 2756e6d8ac6SEric Auger }; 2766e6d8ac6SEric Auger 2779005f928SZhenzhong Duan DEFINE_TYPES(types) 278