1e2c7d025SEric Auger /* 211b8b9d5SCédric Le Goater * VFIO Device interface 3e2c7d025SEric Auger * 4e2c7d025SEric Auger * Copyright Red Hat, Inc. 2012 5e2c7d025SEric Auger * 6e2c7d025SEric Auger * Authors: 7e2c7d025SEric Auger * Alex Williamson <alex.williamson@redhat.com> 8e2c7d025SEric Auger * 9e2c7d025SEric Auger * This work is licensed under the terms of the GNU GPL, version 2. See 10e2c7d025SEric Auger * the COPYING file in the top-level directory. 11e2c7d025SEric Auger * 12e2c7d025SEric Auger * Based on qemu-kvm device-assignment: 13e2c7d025SEric Auger * Adapted for KVM by Qumranet. 14e2c7d025SEric Auger * Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com) 15e2c7d025SEric Auger * Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com) 16e2c7d025SEric Auger * Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com) 17e2c7d025SEric Auger * Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com) 18e2c7d025SEric Auger * Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com) 19e2c7d025SEric Auger */ 20175de524SMarkus Armbruster 21e2c7d025SEric Auger #ifndef HW_VFIO_VFIO_COMMON_H 22e2c7d025SEric Auger #define HW_VFIO_VFIO_COMMON_H 23e2c7d025SEric Auger 248be545baSRichard Henderson #include "system/memory.h" 25e2c7d025SEric Auger #include "qemu/queue.h" 2646900226SAlex Williamson #ifdef CONFIG_LINUX 2746900226SAlex Williamson #include <linux/vfio.h> 2846900226SAlex Williamson #endif 2932cad1ffSPhilippe Mathieu-Daudé #include "system/system.h" 30f61dddd7SZhenzhong Duan #include "hw/vfio/vfio-container-base.h" 3132cad1ffSPhilippe Mathieu-Daudé #include "system/host_iommu_device.h" 3232cad1ffSPhilippe Mathieu-Daudé #include "system/iommufd.h" 33e2c7d025SEric Auger 34e1eb292aSMarkus Armbruster #define VFIO_MSG_PREFIX "vfio %s: " 35426ec904SEric Auger 36e2c7d025SEric Auger enum { 37e2c7d025SEric Auger VFIO_DEVICE_TYPE_PCI = 0, 380ea2730bSEric Auger VFIO_DEVICE_TYPE_PLATFORM = 1, 391dcac3e1SXiao Feng Ren VFIO_DEVICE_TYPE_CCW = 2, 402fe2942cSTony Krowiak VFIO_DEVICE_TYPE_AP = 3, 41e2c7d025SEric Auger }; 42504d297eSCédric Le Goater 43e2c7d025SEric Auger typedef struct VFIODeviceOps VFIODeviceOps; 44b553d2c4SCédric Le Goater typedef struct VFIOMigration VFIOMigration; 45e2c7d025SEric Auger 46d4a8f286SCédric Le Goater typedef struct IOMMUFDBackend IOMMUFDBackend; 47d4a8f286SCédric Le Goater typedef struct VFIOIOASHwpt VFIOIOASHwpt; 48d4a8f286SCédric Le Goater 49e2c7d025SEric Auger typedef struct VFIODevice { 50e2c7d025SEric Auger QLIST_ENTRY(VFIODevice) next; 517103ef7eSZhenzhong Duan QLIST_ENTRY(VFIODevice) container_next; 523d779abaSZhenzhong Duan QLIST_ENTRY(VFIODevice) global_next; 53e2c7d025SEric Auger struct VFIOGroup *group; 543e6015d1SZhenzhong Duan VFIOContainerBase *bcontainer; 557df9381bSAlex Williamson char *sysfsdev; 56e2c7d025SEric Auger char *name; 577da624e2SAlex Williamson DeviceState *dev; 58e2c7d025SEric Auger int fd; 59e2c7d025SEric Auger int type; 6013e522f6SJoao Martins bool mdev; 61e2c7d025SEric Auger bool reset_works; 62e2c7d025SEric Auger bool needs_reset; 635e15d79bSAlex Williamson bool no_mmap; 64aff92b82SDavid Hildenbrand bool ram_block_discard_allowed; 658bbcb64aSAvihai Horon OnOffAuto enable_migration; 66623af41dSMaciej S. Szmigiero OnOffAuto migration_multifd_transfer; 675e1f8905SAvihai Horon bool migration_events; 68e2c7d025SEric Auger VFIODeviceOps *ops; 69e2c7d025SEric Auger unsigned int num_irqs; 70e2c7d025SEric Auger unsigned int num_regions; 71e2c7d025SEric Auger unsigned int flags; 72a9e271ecSKirti Wankhede VFIOMigration *migration; 73a9e271ecSKirti Wankhede Error *migration_blocker; 74bb0990d1SKirti Wankhede OnOffAuto pre_copy_dirty_page_tracking; 7530b91677SJoao Martins OnOffAuto device_dirty_page_tracking; 765255bbf4SJoao Martins bool dirty_pages_supported; 770ae05e08SAvihai Horon bool dirty_tracking; /* Protected by BQL */ 78dddfd8d6SJoao Martins bool iommu_dirty_tracking; 79a7fd91b8SZhenzhong Duan HostIOMMUDevice *hiod; 805ee3dc7aSYi Liu int devid; 815ee3dc7aSYi Liu IOMMUFDBackend *iommufd; 825b1e96e6SJoao Martins VFIOIOASHwpt *hwpt; 835b1e96e6SJoao Martins QLIST_ENTRY(VFIODevice) hwpt_next; 84e2c7d025SEric Auger } VFIODevice; 85e2c7d025SEric Auger 86e2c7d025SEric Auger struct VFIODeviceOps { 87e2c7d025SEric Auger void (*vfio_compute_needs_reset)(VFIODevice *vdev); 88e2c7d025SEric Auger int (*vfio_hot_reset_multi)(VFIODevice *vdev); 89e2c7d025SEric Auger void (*vfio_eoi)(VFIODevice *vdev); 90e93b733bSKirti Wankhede Object *(*vfio_get_object)(VFIODevice *vdev); 913783f814SCédric Le Goater 923783f814SCédric Le Goater /** 933783f814SCédric Le Goater * @vfio_save_config 943783f814SCédric Le Goater * 953783f814SCédric Le Goater * Save device config state 963783f814SCédric Le Goater * 973783f814SCédric Le Goater * @vdev: #VFIODevice for which to save the config 983783f814SCédric Le Goater * @f: #QEMUFile where to send the data 993783f814SCédric Le Goater * @errp: pointer to Error*, to store an error if it happens. 1003783f814SCédric Le Goater * 1013783f814SCédric Le Goater * Returns zero to indicate success and negative for error 1023783f814SCédric Le Goater */ 1033783f814SCédric Le Goater int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp); 1043783f814SCédric Le Goater 1053783f814SCédric Le Goater /** 1063783f814SCédric Le Goater * @vfio_load_config 1073783f814SCédric Le Goater * 1083783f814SCédric Le Goater * Load device config state 1093783f814SCédric Le Goater * 1103783f814SCédric Le Goater * @vdev: #VFIODevice for which to load the config 1113783f814SCédric Le Goater * @f: #QEMUFile where to get the data 1123783f814SCédric Le Goater * 1133783f814SCédric Le Goater * Returns zero to indicate success and negative for error 1143783f814SCédric Le Goater */ 115c5e2fb3cSKirti Wankhede int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f); 116e2c7d025SEric Auger }; 117e2c7d025SEric Auger 118e218ccf0SCédric Le Goater void vfio_device_irq_disable(VFIODevice *vbasedev, int index); 119e218ccf0SCédric Le Goater void vfio_device_irq_unmask(VFIODevice *vbasedev, int index); 120e218ccf0SCédric Le Goater void vfio_device_irq_mask(VFIODevice *vbasedev, int index); 121e218ccf0SCédric Le Goater bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex, 122201a7331SEric Auger int action, int fd, Error **errp); 123499e53ccSCédric Le Goater 124e218ccf0SCédric Le Goater void vfio_device_reset_handler(void *opaque); 12513e522f6SJoao Martins bool vfio_device_is_mdev(VFIODevice *vbasedev); 1260805f829SZhenzhong Duan bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev, 1270805f829SZhenzhong Duan const char *typename, Error **errp); 128e218ccf0SCédric Le Goater bool vfio_device_attach(char *name, VFIODevice *vbasedev, 1295456b186SEric Auger AddressSpace *as, Error **errp); 130ef73671fSJohn Levon bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name, 131ef73671fSJohn Levon VFIODevice *vbasedev, AddressSpace *as, 132ef73671fSJohn Levon Error **errp); 133e218ccf0SCédric Le Goater void vfio_device_detach(VFIODevice *vbasedev); 13480936cf7SCédric Le Goater VFIODevice *vfio_get_vfio_device(Object *obj); 135e2c7d025SEric Auger 1363d779abaSZhenzhong Duan typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList; 1377e63b311SYi Liu extern VFIODeviceList vfio_device_list; 138e2c7d025SEric Auger 13946900226SAlex Williamson #ifdef CONFIG_LINUX 140a901682fSJohn Levon void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer, 141a901682fSJohn Levon struct vfio_device_info *info); 142a901682fSJohn Levon 143d60fb709SJohn Levon void vfio_device_unprepare(VFIODevice *vbasedev); 144d60fb709SJohn Levon 145e218ccf0SCédric Le Goater int vfio_device_get_region_info(VFIODevice *vbasedev, int index, 14646900226SAlex Williamson struct vfio_region_info **info); 147e218ccf0SCédric Le Goater int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type, 148e61a424fSAlex Williamson uint32_t subtype, struct vfio_region_info **info); 149e218ccf0SCédric Le Goater bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); 150*5321e623SJohn Levon 151*5321e623SJohn Levon int vfio_device_get_irq_info(VFIODevice *vbasedev, int index, 152*5321e623SJohn Levon struct vfio_irq_info *info); 15346900226SAlex Williamson #endif 154318f67ceSAlexey Kardashevskiy 155da3e04b2SZhenzhong Duan /* Returns 0 on success, or a negative errno. */ 156c6c6cf91SZhenzhong Duan bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp); 157da3e04b2SZhenzhong Duan void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp); 1586106a329SZhenzhong Duan void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, 1596106a329SZhenzhong Duan DeviceState *dev, bool ram_discard); 160d441e05eSZhenzhong Duan int vfio_device_get_aw_bits(VFIODevice *vdev); 161175de524SMarkus Armbruster #endif /* HW_VFIO_VFIO_COMMON_H */ 162