168c07d76SCédric Le Goater /* 268c07d76SCédric Le Goater * VFIO device 368c07d76SCédric Le Goater * 468c07d76SCédric Le Goater * Copyright Red Hat, Inc. 2012 568c07d76SCédric Le Goater * 668c07d76SCédric Le Goater * Authors: 768c07d76SCédric Le Goater * Alex Williamson <alex.williamson@redhat.com> 868c07d76SCédric Le Goater * 968c07d76SCédric Le Goater * This work is licensed under the terms of the GNU GPL, version 2. See 1068c07d76SCédric Le Goater * the COPYING file in the top-level directory. 1168c07d76SCédric Le Goater * 1268c07d76SCédric Le Goater * Based on qemu-kvm device-assignment: 1368c07d76SCédric Le Goater * Adapted for KVM by Qumranet. 1468c07d76SCédric Le Goater * Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com) 1568c07d76SCédric Le Goater * Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com) 1668c07d76SCédric Le Goater * Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com) 1768c07d76SCédric Le Goater * Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com) 1868c07d76SCédric Le Goater * Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com) 1968c07d76SCédric Le Goater */ 2068c07d76SCédric Le Goater 2168c07d76SCédric Le Goater #include "qemu/osdep.h" 2268c07d76SCédric Le Goater #include <sys/ioctl.h> 2368c07d76SCédric Le Goater 2411b8b9d5SCédric Le Goater #include "hw/vfio/vfio-device.h" 2568c07d76SCédric Le Goater #include "hw/vfio/pci.h" 2668c07d76SCédric Le Goater #include "hw/hw.h" 2768c07d76SCédric Le Goater #include "trace.h" 2868c07d76SCédric Le Goater #include "qapi/error.h" 2968c07d76SCédric Le Goater #include "qemu/error-report.h" 3068c07d76SCédric Le Goater #include "qemu/units.h" 3168c07d76SCédric Le Goater #include "monitor/monitor.h" 3268c07d76SCédric Le Goater #include "vfio-helpers.h" 3368c07d76SCédric Le Goater 34a997b506SCédric Le Goater VFIODeviceList vfio_device_list = 35a997b506SCédric Le Goater QLIST_HEAD_INITIALIZER(vfio_device_list); 36a997b506SCédric Le Goater 3768c07d76SCédric Le Goater /* 38819a5865SCédric Le Goater * We want to differentiate hot reset of multiple in-use devices vs 39819a5865SCédric Le Goater * hot reset of a single in-use device. VFIO_DEVICE_RESET will already 40819a5865SCédric Le Goater * handle the case of doing hot resets when there is only a single 41819a5865SCédric Le Goater * device per bus. The in-use here refers to how many VFIODevices are 42819a5865SCédric Le Goater * affected. A hot reset that affects multiple devices, but only a 43819a5865SCédric Le Goater * single in-use device, means that we can call it from our bus 44819a5865SCédric Le Goater * ->reset() callback since the extent is effectively a single 45819a5865SCédric Le Goater * device. This allows us to make use of it in the hotplug path. When 46819a5865SCédric Le Goater * there are multiple in-use devices, we can only trigger the hot 47819a5865SCédric Le Goater * reset during a system reset and thus from our reset handler. We 48819a5865SCédric Le Goater * separate _one vs _multi here so that we don't overlap and do a 49819a5865SCédric Le Goater * double reset on the system reset path where both our reset handler 50819a5865SCédric Le Goater * and ->reset() callback are used. Calling _one() will only do a hot 51819a5865SCédric Le Goater * reset for the one in-use devices case, calling _multi() will do 52819a5865SCédric Le Goater * nothing if a _one() would have been sufficient. 53819a5865SCédric Le Goater */ 54e218ccf0SCédric Le Goater void vfio_device_reset_handler(void *opaque) 55819a5865SCédric Le Goater { 56819a5865SCédric Le Goater VFIODevice *vbasedev; 57819a5865SCédric Le Goater 58e218ccf0SCédric Le Goater trace_vfio_device_reset_handler(); 59819a5865SCédric Le Goater QLIST_FOREACH(vbasedev, &vfio_device_list, global_next) { 60819a5865SCédric Le Goater if (vbasedev->dev->realized) { 61819a5865SCédric Le Goater vbasedev->ops->vfio_compute_needs_reset(vbasedev); 62819a5865SCédric Le Goater } 63819a5865SCédric Le Goater } 64819a5865SCédric Le Goater 65819a5865SCédric Le Goater QLIST_FOREACH(vbasedev, &vfio_device_list, global_next) { 66819a5865SCédric Le Goater if (vbasedev->dev->realized && vbasedev->needs_reset) { 67819a5865SCédric Le Goater vbasedev->ops->vfio_hot_reset_multi(vbasedev); 68819a5865SCédric Le Goater } 69819a5865SCédric Le Goater } 70819a5865SCédric Le Goater } 71819a5865SCédric Le Goater 72819a5865SCédric Le Goater /* 7368c07d76SCédric Le Goater * Common VFIO interrupt disable 7468c07d76SCédric Le Goater */ 75e218ccf0SCédric Le Goater void vfio_device_irq_disable(VFIODevice *vbasedev, int index) 7668c07d76SCédric Le Goater { 7768c07d76SCédric Le Goater struct vfio_irq_set irq_set = { 7868c07d76SCédric Le Goater .argsz = sizeof(irq_set), 7968c07d76SCédric Le Goater .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER, 8068c07d76SCédric Le Goater .index = index, 8168c07d76SCédric Le Goater .start = 0, 8268c07d76SCédric Le Goater .count = 0, 8368c07d76SCédric Le Goater }; 8468c07d76SCédric Le Goater 8538bf025dSJohn Levon vbasedev->io_ops->set_irqs(vbasedev, &irq_set); 8668c07d76SCédric Le Goater } 8768c07d76SCédric Le Goater 88e218ccf0SCédric Le Goater void vfio_device_irq_unmask(VFIODevice *vbasedev, int index) 8968c07d76SCédric Le Goater { 9068c07d76SCédric Le Goater struct vfio_irq_set irq_set = { 9168c07d76SCédric Le Goater .argsz = sizeof(irq_set), 9268c07d76SCédric Le Goater .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK, 9368c07d76SCédric Le Goater .index = index, 9468c07d76SCédric Le Goater .start = 0, 9568c07d76SCédric Le Goater .count = 1, 9668c07d76SCédric Le Goater }; 9768c07d76SCédric Le Goater 9838bf025dSJohn Levon vbasedev->io_ops->set_irqs(vbasedev, &irq_set); 9968c07d76SCédric Le Goater } 10068c07d76SCédric Le Goater 101e218ccf0SCédric Le Goater void vfio_device_irq_mask(VFIODevice *vbasedev, int index) 10268c07d76SCédric Le Goater { 10368c07d76SCédric Le Goater struct vfio_irq_set irq_set = { 10468c07d76SCédric Le Goater .argsz = sizeof(irq_set), 10568c07d76SCédric Le Goater .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK, 10668c07d76SCédric Le Goater .index = index, 10768c07d76SCédric Le Goater .start = 0, 10868c07d76SCédric Le Goater .count = 1, 10968c07d76SCédric Le Goater }; 11068c07d76SCédric Le Goater 11138bf025dSJohn Levon vbasedev->io_ops->set_irqs(vbasedev, &irq_set); 11268c07d76SCédric Le Goater } 11368c07d76SCédric Le Goater 11468c07d76SCédric Le Goater static inline const char *action_to_str(int action) 11568c07d76SCédric Le Goater { 11668c07d76SCédric Le Goater switch (action) { 11768c07d76SCédric Le Goater case VFIO_IRQ_SET_ACTION_MASK: 11868c07d76SCédric Le Goater return "MASK"; 11968c07d76SCédric Le Goater case VFIO_IRQ_SET_ACTION_UNMASK: 12068c07d76SCédric Le Goater return "UNMASK"; 12168c07d76SCédric Le Goater case VFIO_IRQ_SET_ACTION_TRIGGER: 12268c07d76SCédric Le Goater return "TRIGGER"; 12368c07d76SCédric Le Goater default: 12468c07d76SCédric Le Goater return "UNKNOWN ACTION"; 12568c07d76SCédric Le Goater } 12668c07d76SCédric Le Goater } 12768c07d76SCédric Le Goater 12868c07d76SCédric Le Goater static const char *index_to_str(VFIODevice *vbasedev, int index) 12968c07d76SCédric Le Goater { 13068c07d76SCédric Le Goater if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) { 13168c07d76SCédric Le Goater return NULL; 13268c07d76SCédric Le Goater } 13368c07d76SCédric Le Goater 13468c07d76SCédric Le Goater switch (index) { 13568c07d76SCédric Le Goater case VFIO_PCI_INTX_IRQ_INDEX: 13668c07d76SCédric Le Goater return "INTX"; 13768c07d76SCédric Le Goater case VFIO_PCI_MSI_IRQ_INDEX: 13868c07d76SCédric Le Goater return "MSI"; 13968c07d76SCédric Le Goater case VFIO_PCI_MSIX_IRQ_INDEX: 14068c07d76SCédric Le Goater return "MSIX"; 14168c07d76SCédric Le Goater case VFIO_PCI_ERR_IRQ_INDEX: 14268c07d76SCédric Le Goater return "ERR"; 14368c07d76SCédric Le Goater case VFIO_PCI_REQ_IRQ_INDEX: 14468c07d76SCédric Le Goater return "REQ"; 14568c07d76SCédric Le Goater default: 14668c07d76SCédric Le Goater return NULL; 14768c07d76SCédric Le Goater } 14868c07d76SCédric Le Goater } 14968c07d76SCédric Le Goater 150e218ccf0SCédric Le Goater bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex, 15168c07d76SCédric Le Goater int action, int fd, Error **errp) 15268c07d76SCédric Le Goater { 15368c07d76SCédric Le Goater ERRP_GUARD(); 15468c07d76SCédric Le Goater g_autofree struct vfio_irq_set *irq_set = NULL; 15568c07d76SCédric Le Goater int argsz; 15668c07d76SCédric Le Goater const char *name; 15768c07d76SCédric Le Goater int32_t *pfd; 15868c07d76SCédric Le Goater 15968c07d76SCédric Le Goater argsz = sizeof(*irq_set) + sizeof(*pfd); 16068c07d76SCédric Le Goater 16168c07d76SCédric Le Goater irq_set = g_malloc0(argsz); 16268c07d76SCédric Le Goater irq_set->argsz = argsz; 16368c07d76SCédric Le Goater irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | action; 16468c07d76SCédric Le Goater irq_set->index = index; 16568c07d76SCédric Le Goater irq_set->start = subindex; 16668c07d76SCédric Le Goater irq_set->count = 1; 16768c07d76SCédric Le Goater pfd = (int32_t *)&irq_set->data; 16868c07d76SCédric Le Goater *pfd = fd; 16968c07d76SCédric Le Goater 17038bf025dSJohn Levon if (!vbasedev->io_ops->set_irqs(vbasedev, irq_set)) { 17168c07d76SCédric Le Goater return true; 17268c07d76SCédric Le Goater } 17368c07d76SCédric Le Goater 17468c07d76SCédric Le Goater error_setg_errno(errp, errno, "VFIO_DEVICE_SET_IRQS failure"); 17568c07d76SCédric Le Goater 17668c07d76SCédric Le Goater name = index_to_str(vbasedev, index); 17768c07d76SCédric Le Goater if (name) { 17868c07d76SCédric Le Goater error_prepend(errp, "%s-%d: ", name, subindex); 17968c07d76SCédric Le Goater } else { 18068c07d76SCédric Le Goater error_prepend(errp, "index %d-%d: ", index, subindex); 18168c07d76SCédric Le Goater } 18268c07d76SCédric Le Goater error_prepend(errp, 18368c07d76SCédric Le Goater "Failed to %s %s eventfd signaling for interrupt ", 18468c07d76SCédric Le Goater fd < 0 ? "tear down" : "set up", action_to_str(action)); 18568c07d76SCédric Le Goater return false; 18668c07d76SCédric Le Goater } 18768c07d76SCédric Le Goater 1885321e623SJohn Levon int vfio_device_get_irq_info(VFIODevice *vbasedev, int index, 1895321e623SJohn Levon struct vfio_irq_info *info) 1905321e623SJohn Levon { 1915321e623SJohn Levon memset(info, 0, sizeof(*info)); 1925321e623SJohn Levon 1935321e623SJohn Levon info->argsz = sizeof(*info); 1945321e623SJohn Levon info->index = index; 1955321e623SJohn Levon 19638bf025dSJohn Levon return vbasedev->io_ops->get_irq_info(vbasedev, info); 1975321e623SJohn Levon } 1985321e623SJohn Levon 199e218ccf0SCédric Le Goater int vfio_device_get_region_info(VFIODevice *vbasedev, int index, 20068c07d76SCédric Le Goater struct vfio_region_info **info) 20168c07d76SCédric Le Goater { 20268c07d76SCédric Le Goater size_t argsz = sizeof(struct vfio_region_info); 20338bf025dSJohn Levon int ret; 20468c07d76SCédric Le Goater 20595cdb024SJohn Levon /* check cache */ 20695cdb024SJohn Levon if (vbasedev->reginfo[index] != NULL) { 20795cdb024SJohn Levon *info = vbasedev->reginfo[index]; 20895cdb024SJohn Levon return 0; 20995cdb024SJohn Levon } 21095cdb024SJohn Levon 21168c07d76SCédric Le Goater *info = g_malloc0(argsz); 21268c07d76SCédric Le Goater 21368c07d76SCédric Le Goater (*info)->index = index; 21468c07d76SCédric Le Goater retry: 21568c07d76SCédric Le Goater (*info)->argsz = argsz; 21668c07d76SCédric Le Goater 21738bf025dSJohn Levon ret = vbasedev->io_ops->get_region_info(vbasedev, *info); 21838bf025dSJohn Levon if (ret != 0) { 21968c07d76SCédric Le Goater g_free(*info); 22068c07d76SCédric Le Goater *info = NULL; 22138bf025dSJohn Levon return ret; 22268c07d76SCédric Le Goater } 22368c07d76SCédric Le Goater 22468c07d76SCédric Le Goater if ((*info)->argsz > argsz) { 22568c07d76SCédric Le Goater argsz = (*info)->argsz; 22668c07d76SCédric Le Goater *info = g_realloc(*info, argsz); 22768c07d76SCédric Le Goater 22868c07d76SCédric Le Goater goto retry; 22968c07d76SCédric Le Goater } 23068c07d76SCédric Le Goater 23195cdb024SJohn Levon /* fill cache */ 23295cdb024SJohn Levon vbasedev->reginfo[index] = *info; 23395cdb024SJohn Levon 23468c07d76SCédric Le Goater return 0; 23568c07d76SCédric Le Goater } 23668c07d76SCédric Le Goater 237e218ccf0SCédric Le Goater int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type, 23868c07d76SCédric Le Goater uint32_t subtype, struct vfio_region_info **info) 23968c07d76SCédric Le Goater { 24068c07d76SCédric Le Goater int i; 24168c07d76SCédric Le Goater 24268c07d76SCédric Le Goater for (i = 0; i < vbasedev->num_regions; i++) { 24368c07d76SCédric Le Goater struct vfio_info_cap_header *hdr; 24468c07d76SCédric Le Goater struct vfio_region_info_cap_type *cap_type; 24568c07d76SCédric Le Goater 246e218ccf0SCédric Le Goater if (vfio_device_get_region_info(vbasedev, i, info)) { 24768c07d76SCédric Le Goater continue; 24868c07d76SCédric Le Goater } 24968c07d76SCédric Le Goater 25068c07d76SCédric Le Goater hdr = vfio_get_region_info_cap(*info, VFIO_REGION_INFO_CAP_TYPE); 25168c07d76SCédric Le Goater if (!hdr) { 25268c07d76SCédric Le Goater continue; 25368c07d76SCédric Le Goater } 25468c07d76SCédric Le Goater 25568c07d76SCédric Le Goater cap_type = container_of(hdr, struct vfio_region_info_cap_type, header); 25668c07d76SCédric Le Goater 257e218ccf0SCédric Le Goater trace_vfio_device_get_region_info_type(vbasedev->name, i, 25868c07d76SCédric Le Goater cap_type->type, cap_type->subtype); 25968c07d76SCédric Le Goater 26068c07d76SCédric Le Goater if (cap_type->type == type && cap_type->subtype == subtype) { 26168c07d76SCédric Le Goater return 0; 26268c07d76SCédric Le Goater } 26368c07d76SCédric Le Goater } 26468c07d76SCédric Le Goater 26568c07d76SCédric Le Goater *info = NULL; 26668c07d76SCédric Le Goater return -ENODEV; 26768c07d76SCédric Le Goater } 26868c07d76SCédric Le Goater 269e218ccf0SCédric Le Goater bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type) 27068c07d76SCédric Le Goater { 27195cdb024SJohn Levon struct vfio_region_info *info = NULL; 27268c07d76SCédric Le Goater bool ret = false; 27368c07d76SCédric Le Goater 274e218ccf0SCédric Le Goater if (!vfio_device_get_region_info(vbasedev, region, &info)) { 27568c07d76SCédric Le Goater if (vfio_get_region_info_cap(info, cap_type)) { 27668c07d76SCédric Le Goater ret = true; 27768c07d76SCédric Le Goater } 27868c07d76SCédric Le Goater } 27968c07d76SCédric Le Goater 28068c07d76SCédric Le Goater return ret; 28168c07d76SCédric Le Goater } 28268c07d76SCédric Le Goater 28368c07d76SCédric Le Goater bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp) 28468c07d76SCédric Le Goater { 28568c07d76SCédric Le Goater ERRP_GUARD(); 28668c07d76SCédric Le Goater struct stat st; 28768c07d76SCédric Le Goater 28868c07d76SCédric Le Goater if (vbasedev->fd < 0) { 28968c07d76SCédric Le Goater if (stat(vbasedev->sysfsdev, &st) < 0) { 29068c07d76SCédric Le Goater error_setg_errno(errp, errno, "no such host device"); 29168c07d76SCédric Le Goater error_prepend(errp, VFIO_MSG_PREFIX, vbasedev->sysfsdev); 29268c07d76SCédric Le Goater return false; 29368c07d76SCédric Le Goater } 29468c07d76SCédric Le Goater /* User may specify a name, e.g: VFIO platform device */ 29568c07d76SCédric Le Goater if (!vbasedev->name) { 29668c07d76SCédric Le Goater vbasedev->name = g_path_get_basename(vbasedev->sysfsdev); 29768c07d76SCédric Le Goater } 29868c07d76SCédric Le Goater } else { 29968c07d76SCédric Le Goater if (!vbasedev->iommufd) { 30068c07d76SCédric Le Goater error_setg(errp, "Use FD passing only with iommufd backend"); 30168c07d76SCédric Le Goater return false; 30268c07d76SCédric Le Goater } 30368c07d76SCédric Le Goater /* 30468c07d76SCédric Le Goater * Give a name with fd so any function printing out vbasedev->name 30568c07d76SCédric Le Goater * will not break. 30668c07d76SCédric Le Goater */ 30768c07d76SCédric Le Goater if (!vbasedev->name) { 30868c07d76SCédric Le Goater vbasedev->name = g_strdup_printf("VFIO_FD%d", vbasedev->fd); 30968c07d76SCédric Le Goater } 31068c07d76SCédric Le Goater } 31168c07d76SCédric Le Goater 31268c07d76SCédric Le Goater return true; 31368c07d76SCédric Le Goater } 31468c07d76SCédric Le Goater 31568c07d76SCédric Le Goater void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp) 31668c07d76SCédric Le Goater { 31768c07d76SCédric Le Goater ERRP_GUARD(); 31868c07d76SCédric Le Goater int fd = monitor_fd_param(monitor_cur(), str, errp); 31968c07d76SCédric Le Goater 32068c07d76SCédric Le Goater if (fd < 0) { 32168c07d76SCédric Le Goater error_prepend(errp, "Could not parse remote object fd %s:", str); 32268c07d76SCédric Le Goater return; 32368c07d76SCédric Le Goater } 32468c07d76SCédric Le Goater vbasedev->fd = fd; 32568c07d76SCédric Le Goater } 32668c07d76SCédric Le Goater 32738bf025dSJohn Levon static VFIODeviceIOOps vfio_device_io_ops_ioctl; 32838bf025dSJohn Levon 32968c07d76SCédric Le Goater void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, 33068c07d76SCédric Le Goater DeviceState *dev, bool ram_discard) 33168c07d76SCédric Le Goater { 33268c07d76SCédric Le Goater vbasedev->type = type; 33368c07d76SCédric Le Goater vbasedev->ops = ops; 33438bf025dSJohn Levon vbasedev->io_ops = &vfio_device_io_ops_ioctl; 33568c07d76SCédric Le Goater vbasedev->dev = dev; 33668c07d76SCédric Le Goater vbasedev->fd = -1; 33768c07d76SCédric Le Goater 33868c07d76SCédric Le Goater vbasedev->ram_block_discard_allowed = ram_discard; 33968c07d76SCédric Le Goater } 34068c07d76SCédric Le Goater 34168c07d76SCédric Le Goater int vfio_device_get_aw_bits(VFIODevice *vdev) 34268c07d76SCédric Le Goater { 34368c07d76SCédric Le Goater /* 34468c07d76SCédric Le Goater * iova_ranges is a sorted list. For old kernels that support 34568c07d76SCédric Le Goater * VFIO but not support query of iova ranges, iova_ranges is NULL, 34668c07d76SCédric Le Goater * in this case HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX(64) is returned. 34768c07d76SCédric Le Goater */ 34868c07d76SCédric Le Goater GList *l = g_list_last(vdev->bcontainer->iova_ranges); 34968c07d76SCédric Le Goater 35068c07d76SCédric Le Goater if (l) { 35168c07d76SCédric Le Goater Range *range = l->data; 35268c07d76SCédric Le Goater return range_get_last_bit(range) + 1; 35368c07d76SCédric Le Goater } 35468c07d76SCédric Le Goater 35568c07d76SCédric Le Goater return HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX; 35668c07d76SCédric Le Goater } 35768c07d76SCédric Le Goater 35868c07d76SCédric Le Goater bool vfio_device_is_mdev(VFIODevice *vbasedev) 35968c07d76SCédric Le Goater { 36068c07d76SCédric Le Goater g_autofree char *subsys = NULL; 36168c07d76SCédric Le Goater g_autofree char *tmp = NULL; 36268c07d76SCédric Le Goater 36368c07d76SCédric Le Goater if (!vbasedev->sysfsdev) { 36468c07d76SCédric Le Goater return false; 36568c07d76SCédric Le Goater } 36668c07d76SCédric Le Goater 36768c07d76SCédric Le Goater tmp = g_strdup_printf("%s/subsystem", vbasedev->sysfsdev); 36868c07d76SCédric Le Goater subsys = realpath(tmp, NULL); 36968c07d76SCédric Le Goater return subsys && (strcmp(subsys, "/sys/bus/mdev") == 0); 37068c07d76SCédric Le Goater } 37168c07d76SCédric Le Goater 3720805f829SZhenzhong Duan bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev, 3730805f829SZhenzhong Duan const char *typename, Error **errp) 37468c07d76SCédric Le Goater { 3750805f829SZhenzhong Duan HostIOMMUDevice *hiod; 37668c07d76SCédric Le Goater 3770805f829SZhenzhong Duan if (vbasedev->mdev) { 37868c07d76SCédric Le Goater return true; 37968c07d76SCédric Le Goater } 38068c07d76SCédric Le Goater 3810805f829SZhenzhong Duan hiod = HOST_IOMMU_DEVICE(object_new(typename)); 3820805f829SZhenzhong Duan 3830805f829SZhenzhong Duan if (!HOST_IOMMU_DEVICE_GET_CLASS(hiod)->realize(hiod, vbasedev, errp)) { 3840805f829SZhenzhong Duan object_unref(hiod); 3850805f829SZhenzhong Duan return false; 3860805f829SZhenzhong Duan } 3870805f829SZhenzhong Duan 3880805f829SZhenzhong Duan vbasedev->hiod = hiod; 3890805f829SZhenzhong Duan return true; 39068c07d76SCédric Le Goater } 39168c07d76SCédric Le Goater 39268c07d76SCédric Le Goater VFIODevice *vfio_get_vfio_device(Object *obj) 39368c07d76SCédric Le Goater { 39468c07d76SCédric Le Goater if (object_dynamic_cast(obj, TYPE_VFIO_PCI)) { 395*d4e392d0SJohn Levon return &VFIO_PCI_BASE(obj)->vbasedev; 39668c07d76SCédric Le Goater } else { 39768c07d76SCédric Le Goater return NULL; 39868c07d76SCédric Le Goater } 39968c07d76SCédric Le Goater } 400923b1141SCédric Le Goater 401ef73671fSJohn Levon bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name, 402ef73671fSJohn Levon VFIODevice *vbasedev, AddressSpace *as, 403ef73671fSJohn Levon Error **errp) 404923b1141SCédric Le Goater { 405923b1141SCédric Le Goater const VFIOIOMMUClass *ops = 406ef73671fSJohn Levon VFIO_IOMMU_CLASS(object_class_by_name(iommu_type)); 407923b1141SCédric Le Goater 408923b1141SCédric Le Goater assert(ops); 409923b1141SCédric Le Goater 4100805f829SZhenzhong Duan return ops->attach_device(name, vbasedev, as, errp); 411923b1141SCédric Le Goater } 412923b1141SCédric Le Goater 413ef73671fSJohn Levon bool vfio_device_attach(char *name, VFIODevice *vbasedev, 414ef73671fSJohn Levon AddressSpace *as, Error **errp) 415ef73671fSJohn Levon { 416ef73671fSJohn Levon const char *iommu_type = vbasedev->iommufd ? 417ef73671fSJohn Levon TYPE_VFIO_IOMMU_IOMMUFD : 418ef73671fSJohn Levon TYPE_VFIO_IOMMU_LEGACY; 419ef73671fSJohn Levon 420ef73671fSJohn Levon return vfio_device_attach_by_iommu_type(iommu_type, name, vbasedev, 421ef73671fSJohn Levon as, errp); 422ef73671fSJohn Levon } 423ef73671fSJohn Levon 424e218ccf0SCédric Le Goater void vfio_device_detach(VFIODevice *vbasedev) 425923b1141SCédric Le Goater { 426923b1141SCédric Le Goater if (!vbasedev->bcontainer) { 427923b1141SCédric Le Goater return; 428923b1141SCédric Le Goater } 429923b1141SCédric Le Goater VFIO_IOMMU_GET_CLASS(vbasedev->bcontainer)->detach_device(vbasedev); 430923b1141SCédric Le Goater } 431a901682fSJohn Levon 432a901682fSJohn Levon void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer, 433a901682fSJohn Levon struct vfio_device_info *info) 434a901682fSJohn Levon { 435a901682fSJohn Levon vbasedev->num_irqs = info->num_irqs; 436a901682fSJohn Levon vbasedev->num_regions = info->num_regions; 437a901682fSJohn Levon vbasedev->flags = info->flags; 438a901682fSJohn Levon vbasedev->reset_works = !!(info->flags & VFIO_DEVICE_FLAGS_RESET); 439a901682fSJohn Levon 440a901682fSJohn Levon vbasedev->bcontainer = bcontainer; 441a901682fSJohn Levon QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next); 442a901682fSJohn Levon 443a901682fSJohn Levon QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next); 44495cdb024SJohn Levon 44595cdb024SJohn Levon vbasedev->reginfo = g_new0(struct vfio_region_info *, 44695cdb024SJohn Levon vbasedev->num_regions); 447a901682fSJohn Levon } 448d60fb709SJohn Levon 449d60fb709SJohn Levon void vfio_device_unprepare(VFIODevice *vbasedev) 450d60fb709SJohn Levon { 45195cdb024SJohn Levon int i; 45295cdb024SJohn Levon 45395cdb024SJohn Levon for (i = 0; i < vbasedev->num_regions; i++) { 45495cdb024SJohn Levon g_free(vbasedev->reginfo[i]); 45595cdb024SJohn Levon } 45695cdb024SJohn Levon g_free(vbasedev->reginfo); 45795cdb024SJohn Levon vbasedev->reginfo = NULL; 45895cdb024SJohn Levon 459d60fb709SJohn Levon QLIST_REMOVE(vbasedev, container_next); 460d60fb709SJohn Levon QLIST_REMOVE(vbasedev, global_next); 461d60fb709SJohn Levon vbasedev->bcontainer = NULL; 462d60fb709SJohn Levon } 46338bf025dSJohn Levon 46438bf025dSJohn Levon /* 46538bf025dSJohn Levon * Traditional ioctl() based io 46638bf025dSJohn Levon */ 46738bf025dSJohn Levon 46838bf025dSJohn Levon static int vfio_device_io_device_feature(VFIODevice *vbasedev, 46938bf025dSJohn Levon struct vfio_device_feature *feature) 47038bf025dSJohn Levon { 47138bf025dSJohn Levon int ret; 47238bf025dSJohn Levon 47338bf025dSJohn Levon ret = ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature); 47438bf025dSJohn Levon 47538bf025dSJohn Levon return ret < 0 ? -errno : ret; 47638bf025dSJohn Levon } 47738bf025dSJohn Levon 47838bf025dSJohn Levon static int vfio_device_io_get_region_info(VFIODevice *vbasedev, 47938bf025dSJohn Levon struct vfio_region_info *info) 48038bf025dSJohn Levon { 48138bf025dSJohn Levon int ret; 48238bf025dSJohn Levon 48338bf025dSJohn Levon ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_REGION_INFO, info); 48438bf025dSJohn Levon 48538bf025dSJohn Levon return ret < 0 ? -errno : ret; 48638bf025dSJohn Levon } 48738bf025dSJohn Levon 48838bf025dSJohn Levon static int vfio_device_io_get_irq_info(VFIODevice *vbasedev, 48938bf025dSJohn Levon struct vfio_irq_info *info) 49038bf025dSJohn Levon { 49138bf025dSJohn Levon int ret; 49238bf025dSJohn Levon 49338bf025dSJohn Levon ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_IRQ_INFO, info); 49438bf025dSJohn Levon 49538bf025dSJohn Levon return ret < 0 ? -errno : ret; 49638bf025dSJohn Levon } 49738bf025dSJohn Levon 49838bf025dSJohn Levon static int vfio_device_io_set_irqs(VFIODevice *vbasedev, 49938bf025dSJohn Levon struct vfio_irq_set *irqs) 50038bf025dSJohn Levon { 50138bf025dSJohn Levon int ret; 50238bf025dSJohn Levon 50338bf025dSJohn Levon ret = ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irqs); 50438bf025dSJohn Levon 50538bf025dSJohn Levon return ret < 0 ? -errno : ret; 50638bf025dSJohn Levon } 50738bf025dSJohn Levon 508776066acSJohn Levon static int vfio_device_io_region_read(VFIODevice *vbasedev, uint8_t index, 509776066acSJohn Levon off_t off, uint32_t size, void *data) 510776066acSJohn Levon { 511776066acSJohn Levon struct vfio_region_info *info; 512776066acSJohn Levon int ret; 513776066acSJohn Levon 514776066acSJohn Levon ret = vfio_device_get_region_info(vbasedev, index, &info); 515776066acSJohn Levon if (ret != 0) { 516776066acSJohn Levon return ret; 517776066acSJohn Levon } 518776066acSJohn Levon 519776066acSJohn Levon ret = pread(vbasedev->fd, data, size, info->offset + off); 520776066acSJohn Levon 521776066acSJohn Levon return ret < 0 ? -errno : ret; 522776066acSJohn Levon } 523776066acSJohn Levon 524776066acSJohn Levon static int vfio_device_io_region_write(VFIODevice *vbasedev, uint8_t index, 525776066acSJohn Levon off_t off, uint32_t size, void *data) 526776066acSJohn Levon { 527776066acSJohn Levon struct vfio_region_info *info; 528776066acSJohn Levon int ret; 529776066acSJohn Levon 530776066acSJohn Levon ret = vfio_device_get_region_info(vbasedev, index, &info); 531776066acSJohn Levon if (ret != 0) { 532776066acSJohn Levon return ret; 533776066acSJohn Levon } 534776066acSJohn Levon 535776066acSJohn Levon ret = pwrite(vbasedev->fd, data, size, info->offset + off); 536776066acSJohn Levon 537776066acSJohn Levon return ret < 0 ? -errno : ret; 538776066acSJohn Levon } 539776066acSJohn Levon 54038bf025dSJohn Levon static VFIODeviceIOOps vfio_device_io_ops_ioctl = { 54138bf025dSJohn Levon .device_feature = vfio_device_io_device_feature, 54238bf025dSJohn Levon .get_region_info = vfio_device_io_get_region_info, 54338bf025dSJohn Levon .get_irq_info = vfio_device_io_get_irq_info, 54438bf025dSJohn Levon .set_irqs = vfio_device_io_set_irqs, 545776066acSJohn Levon .region_read = vfio_device_io_region_read, 546776066acSJohn Levon .region_write = vfio_device_io_region_write, 54738bf025dSJohn Levon }; 548