1e2c7d025SEric Auger /* 2e2c7d025SEric Auger * common header for vfio based device assignment support 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 }; 42e2c7d025SEric Auger struct VFIOGroup; 43e2c7d025SEric Auger 44e2c7d025SEric Auger typedef struct VFIOContainer { 45f61dddd7SZhenzhong Duan VFIOContainerBase bcontainer; 46e2c7d025SEric Auger int fd; /* /dev/vfio/vfio, empowered by the attached groups */ 47318f67ceSAlexey Kardashevskiy unsigned iommu_type; 48e2c7d025SEric Auger QLIST_HEAD(, VFIOGroup) group_list; 49e2c7d025SEric Auger } VFIOContainer; 50e2c7d025SEric Auger 51504d297eSCédric Le Goater OBJECT_DECLARE_SIMPLE_TYPE(VFIOContainer, VFIO_IOMMU_LEGACY); 52504d297eSCédric Le Goater 53e2c7d025SEric Auger typedef struct VFIODeviceOps VFIODeviceOps; 54b553d2c4SCédric Le Goater typedef struct VFIOMigration VFIOMigration; 55e2c7d025SEric Auger 56d4a8f286SCédric Le Goater typedef struct IOMMUFDBackend IOMMUFDBackend; 57d4a8f286SCédric Le Goater typedef struct VFIOIOASHwpt VFIOIOASHwpt; 58d4a8f286SCédric Le Goater 59e2c7d025SEric Auger typedef struct VFIODevice { 60e2c7d025SEric Auger QLIST_ENTRY(VFIODevice) next; 617103ef7eSZhenzhong Duan QLIST_ENTRY(VFIODevice) container_next; 623d779abaSZhenzhong Duan QLIST_ENTRY(VFIODevice) global_next; 63e2c7d025SEric Auger struct VFIOGroup *group; 643e6015d1SZhenzhong Duan VFIOContainerBase *bcontainer; 657df9381bSAlex Williamson char *sysfsdev; 66e2c7d025SEric Auger char *name; 677da624e2SAlex Williamson DeviceState *dev; 68e2c7d025SEric Auger int fd; 69e2c7d025SEric Auger int type; 7013e522f6SJoao Martins bool mdev; 71e2c7d025SEric Auger bool reset_works; 72e2c7d025SEric Auger bool needs_reset; 735e15d79bSAlex Williamson bool no_mmap; 74aff92b82SDavid Hildenbrand bool ram_block_discard_allowed; 758bbcb64aSAvihai Horon OnOffAuto enable_migration; 76623af41dSMaciej S. Szmigiero OnOffAuto migration_multifd_transfer; 775e1f8905SAvihai Horon bool migration_events; 78e2c7d025SEric Auger VFIODeviceOps *ops; 79e2c7d025SEric Auger unsigned int num_irqs; 80e2c7d025SEric Auger unsigned int num_regions; 81e2c7d025SEric Auger unsigned int flags; 82a9e271ecSKirti Wankhede VFIOMigration *migration; 83a9e271ecSKirti Wankhede Error *migration_blocker; 84bb0990d1SKirti Wankhede OnOffAuto pre_copy_dirty_page_tracking; 8530b91677SJoao Martins OnOffAuto device_dirty_page_tracking; 865255bbf4SJoao Martins bool dirty_pages_supported; 870ae05e08SAvihai Horon bool dirty_tracking; /* Protected by BQL */ 88dddfd8d6SJoao Martins bool iommu_dirty_tracking; 89a7fd91b8SZhenzhong Duan HostIOMMUDevice *hiod; 905ee3dc7aSYi Liu int devid; 915ee3dc7aSYi Liu IOMMUFDBackend *iommufd; 925b1e96e6SJoao Martins VFIOIOASHwpt *hwpt; 935b1e96e6SJoao Martins QLIST_ENTRY(VFIODevice) hwpt_next; 94e2c7d025SEric Auger } VFIODevice; 95e2c7d025SEric Auger 96e2c7d025SEric Auger struct VFIODeviceOps { 97e2c7d025SEric Auger void (*vfio_compute_needs_reset)(VFIODevice *vdev); 98e2c7d025SEric Auger int (*vfio_hot_reset_multi)(VFIODevice *vdev); 99e2c7d025SEric Auger void (*vfio_eoi)(VFIODevice *vdev); 100e93b733bSKirti Wankhede Object *(*vfio_get_object)(VFIODevice *vdev); 1013783f814SCédric Le Goater 1023783f814SCédric Le Goater /** 1033783f814SCédric Le Goater * @vfio_save_config 1043783f814SCédric Le Goater * 1053783f814SCédric Le Goater * Save device config state 1063783f814SCédric Le Goater * 1073783f814SCédric Le Goater * @vdev: #VFIODevice for which to save the config 1083783f814SCédric Le Goater * @f: #QEMUFile where to send the data 1093783f814SCédric Le Goater * @errp: pointer to Error*, to store an error if it happens. 1103783f814SCédric Le Goater * 1113783f814SCédric Le Goater * Returns zero to indicate success and negative for error 1123783f814SCédric Le Goater */ 1133783f814SCédric Le Goater int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp); 1143783f814SCédric Le Goater 1153783f814SCédric Le Goater /** 1163783f814SCédric Le Goater * @vfio_load_config 1173783f814SCédric Le Goater * 1183783f814SCédric Le Goater * Load device config state 1193783f814SCédric Le Goater * 1203783f814SCédric Le Goater * @vdev: #VFIODevice for which to load the config 1213783f814SCédric Le Goater * @f: #QEMUFile where to get the data 1223783f814SCédric Le Goater * 1233783f814SCédric Le Goater * Returns zero to indicate success and negative for error 1243783f814SCédric Le Goater */ 125c5e2fb3cSKirti Wankhede int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f); 126e2c7d025SEric Auger }; 127e2c7d025SEric Auger 128e2c7d025SEric Auger typedef struct VFIOGroup { 129e2c7d025SEric Auger int fd; 130e2c7d025SEric Auger int groupid; 131e2c7d025SEric Auger VFIOContainer *container; 132e2c7d025SEric Auger QLIST_HEAD(, VFIODevice) device_list; 133e2c7d025SEric Auger QLIST_ENTRY(VFIOGroup) next; 134e2c7d025SEric Auger QLIST_ENTRY(VFIOGroup) container_next; 135aff92b82SDavid Hildenbrand bool ram_block_discard_allowed; 136e2c7d025SEric Auger } VFIOGroup; 137e2c7d025SEric Auger 1380533739eSZhenzhong Duan #define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio" 1399005f928SZhenzhong Duan #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \ 1409005f928SZhenzhong Duan TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio" 1410533739eSZhenzhong Duan 1427e63b311SYi Liu VFIOAddressSpace *vfio_get_address_space(AddressSpace *as); 1437e63b311SYi Liu void vfio_put_address_space(VFIOAddressSpace *space); 144b7b79588SCédric Le Goater void vfio_address_space_insert(VFIOAddressSpace *space, 145b7b79588SCédric Le Goater VFIOContainerBase *bcontainer); 1467e63b311SYi Liu 147e2c7d025SEric Auger void vfio_disable_irqindex(VFIODevice *vbasedev, int index); 148e2c7d025SEric Auger void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index); 149e2c7d025SEric Auger void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index); 15084e37d02SZhenzhong Duan bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex, 151201a7331SEric Auger int action, int fd, Error **errp); 152*499e53ccSCédric Le Goater 153e2c7d025SEric Auger void vfio_reset_handler(void *opaque); 154634f38f0SAlex Williamson struct vfio_device_info *vfio_get_device_info(int fd); 15513e522f6SJoao Martins bool vfio_device_is_mdev(VFIODevice *vbasedev); 15683a4d596SJoao Martins bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp); 157b7754835SZhenzhong Duan bool vfio_attach_device(char *name, VFIODevice *vbasedev, 1585456b186SEric Auger AddressSpace *as, Error **errp); 1595456b186SEric Auger void vfio_detach_device(VFIODevice *vbasedev); 16080936cf7SCédric Le Goater VFIODevice *vfio_get_vfio_device(Object *obj); 161e2c7d025SEric Auger 1625621c02dSZhenzhong Duan int vfio_kvm_device_add_fd(int fd, Error **errp); 1635621c02dSZhenzhong Duan int vfio_kvm_device_del_fd(int fd, Error **errp); 1645621c02dSZhenzhong Duan 165f38f5dd1SZhenzhong Duan bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp); 166d9fa4223SSteve Sistare void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer); 167d9fa4223SSteve Sistare 168f481ee2dSPaolo Bonzini typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList; 1693d779abaSZhenzhong Duan typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList; 170f481ee2dSPaolo Bonzini extern VFIOGroupList vfio_group_list; 1717e63b311SYi Liu extern VFIODeviceList vfio_device_list; 1727e63b311SYi Liu extern const MemoryListener vfio_memory_listener; 1737e63b311SYi Liu extern int vfio_kvm_device_fd; 174e2c7d025SEric Auger 17546900226SAlex Williamson #ifdef CONFIG_LINUX 17646900226SAlex Williamson int vfio_get_region_info(VFIODevice *vbasedev, int index, 17746900226SAlex Williamson struct vfio_region_info **info); 178e61a424fSAlex Williamson int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, 179e61a424fSAlex Williamson uint32_t subtype, struct vfio_region_info **info); 180ae0215b2SAlexey Kardashevskiy bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); 181013002f0SAlexey Kardashevskiy struct vfio_info_cap_header * 182013002f0SAlexey Kardashevskiy vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id); 1837486a628SMatthew Rosato bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, 1847486a628SMatthew Rosato unsigned int *avail); 18592fe289aSMatthew Rosato struct vfio_info_cap_header * 18692fe289aSMatthew Rosato vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id); 1871e09f52fSYi Liu struct vfio_info_cap_header * 1881e09f52fSYi Liu vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id); 18946900226SAlex Williamson #endif 190318f67ceSAlexey Kardashevskiy 1911e09f52fSYi Liu int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size); 1926e9df66eSAvihai Horon bool vfio_devices_all_dirty_tracking_started( 1936e9df66eSAvihai Horon const VFIOContainerBase *bcontainer); 1944517c33cSZhenzhong Duan bool 1954517c33cSZhenzhong Duan vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer); 1964517c33cSZhenzhong Duan int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer, 1972da5f9e4SCédric Le Goater VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp); 1984517c33cSZhenzhong Duan int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova, 1992da5f9e4SCédric Le Goater uint64_t size, ram_addr_t ram_addr, Error **errp); 200da3e04b2SZhenzhong Duan 201da3e04b2SZhenzhong Duan /* Returns 0 on success, or a negative errno. */ 202c6c6cf91SZhenzhong Duan bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp); 203da3e04b2SZhenzhong Duan void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp); 2046106a329SZhenzhong Duan void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, 2056106a329SZhenzhong Duan DeviceState *dev, bool ram_discard); 206d441e05eSZhenzhong Duan int vfio_device_get_aw_bits(VFIODevice *vdev); 207175de524SMarkus Armbruster #endif /* HW_VFIO_VFIO_COMMON_H */ 208