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 24e2c7d025SEric Auger #include "exec/memory.h" 25e2c7d025SEric Auger #include "qemu/queue.h" 26e2c7d025SEric Auger #include "qemu/notify.h" 278b818e05SGerd Hoffmann #include "ui/console.h" 28b290659fSGerd Hoffmann #include "hw/display/ramfb.h" 2946900226SAlex Williamson #ifdef CONFIG_LINUX 3046900226SAlex Williamson #include <linux/vfio.h> 3146900226SAlex Williamson #endif 3202a7e71bSKirti Wankhede #include "sysemu/sysemu.h" 33f61dddd7SZhenzhong Duan #include "hw/vfio/vfio-container-base.h" 340533739eSZhenzhong Duan #include "sysemu/host_iommu_device.h" 35e2c7d025SEric Auger 36e1eb292aSMarkus Armbruster #define VFIO_MSG_PREFIX "vfio %s: " 37426ec904SEric Auger 38e2c7d025SEric Auger enum { 39e2c7d025SEric Auger VFIO_DEVICE_TYPE_PCI = 0, 400ea2730bSEric Auger VFIO_DEVICE_TYPE_PLATFORM = 1, 411dcac3e1SXiao Feng Ren VFIO_DEVICE_TYPE_CCW = 2, 422fe2942cSTony Krowiak VFIO_DEVICE_TYPE_AP = 3, 43e2c7d025SEric Auger }; 44e2c7d025SEric Auger 45db0da029SAlex Williamson typedef struct VFIOMmap { 46db0da029SAlex Williamson MemoryRegion mem; 47db0da029SAlex Williamson void *mmap; 48db0da029SAlex Williamson off_t offset; 49db0da029SAlex Williamson size_t size; 50db0da029SAlex Williamson } VFIOMmap; 51db0da029SAlex Williamson 52e2c7d025SEric Auger typedef struct VFIORegion { 53e2c7d025SEric Auger struct VFIODevice *vbasedev; 54e2c7d025SEric Auger off_t fd_offset; /* offset of region within device fd */ 55db0da029SAlex Williamson MemoryRegion *mem; /* slow, read/write access */ 56e2c7d025SEric Auger size_t size; 57e2c7d025SEric Auger uint32_t flags; /* VFIO region flags (rd/wr/mmap) */ 58db0da029SAlex Williamson uint32_t nr_mmaps; 59db0da029SAlex Williamson VFIOMmap *mmaps; 60e2c7d025SEric Auger uint8_t nr; /* cache the region number for debug */ 61e2c7d025SEric Auger } VFIORegion; 62e2c7d025SEric Auger 63a9e271ecSKirti Wankhede typedef struct VFIOMigration { 64050c588cSKirti Wankhede struct VFIODevice *vbasedev; 6502a7e71bSKirti Wankhede VMChangeStateEntry *vm_state; 663e775730SSteve Sistare NotifierWithReturn migration_state; 6731bcbbb5SAvihai Horon uint32_t device_state; 6831bcbbb5SAvihai Horon int data_fd; 6931bcbbb5SAvihai Horon void *data_buffer; 7031bcbbb5SAvihai Horon size_t data_buffer_size; 716cd1fe11SAvihai Horon uint64_t mig_flags; 72eda7362aSAvihai Horon uint64_t precopy_init_size; 73eda7362aSAvihai Horon uint64_t precopy_dirty_size; 74745c4291SAvihai Horon bool initial_data_sent; 75a9e271ecSKirti Wankhede } VFIOMigration; 76a9e271ecSKirti Wankhede 77e2c7d025SEric Auger struct VFIOGroup; 78e2c7d025SEric Auger 79e2c7d025SEric Auger typedef struct VFIOContainer { 80f61dddd7SZhenzhong Duan VFIOContainerBase bcontainer; 81e2c7d025SEric Auger int fd; /* /dev/vfio/vfio, empowered by the attached groups */ 82318f67ceSAlexey Kardashevskiy unsigned iommu_type; 83e2c7d025SEric Auger QLIST_HEAD(, VFIOGroup) group_list; 84e2c7d025SEric Auger } VFIOContainer; 85e2c7d025SEric Auger 86f4ec5e26SAlexey Kardashevskiy typedef struct VFIOHostDMAWindow { 87f4ec5e26SAlexey Kardashevskiy hwaddr min_iova; 88f4ec5e26SAlexey Kardashevskiy hwaddr max_iova; 89f4ec5e26SAlexey Kardashevskiy uint64_t iova_pgsizes; 90f4ec5e26SAlexey Kardashevskiy QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next; 91f4ec5e26SAlexey Kardashevskiy } VFIOHostDMAWindow; 92f4ec5e26SAlexey Kardashevskiy 935ee3dc7aSYi Liu typedef struct IOMMUFDBackend IOMMUFDBackend; 945ee3dc7aSYi Liu 955ee3dc7aSYi Liu typedef struct VFIOIOMMUFDContainer { 965ee3dc7aSYi Liu VFIOContainerBase bcontainer; 975ee3dc7aSYi Liu IOMMUFDBackend *be; 985ee3dc7aSYi Liu uint32_t ioas_id; 995ee3dc7aSYi Liu } VFIOIOMMUFDContainer; 1005ee3dc7aSYi Liu 101e2c7d025SEric Auger typedef struct VFIODeviceOps VFIODeviceOps; 102e2c7d025SEric Auger 103e2c7d025SEric Auger typedef struct VFIODevice { 104e2c7d025SEric Auger QLIST_ENTRY(VFIODevice) next; 1057103ef7eSZhenzhong Duan QLIST_ENTRY(VFIODevice) container_next; 1063d779abaSZhenzhong Duan QLIST_ENTRY(VFIODevice) global_next; 107e2c7d025SEric Auger struct VFIOGroup *group; 1083e6015d1SZhenzhong Duan VFIOContainerBase *bcontainer; 1097df9381bSAlex Williamson char *sysfsdev; 110e2c7d025SEric Auger char *name; 1117da624e2SAlex Williamson DeviceState *dev; 112e2c7d025SEric Auger int fd; 113e2c7d025SEric Auger int type; 114e2c7d025SEric Auger bool reset_works; 115e2c7d025SEric Auger bool needs_reset; 1165e15d79bSAlex Williamson bool no_mmap; 117aff92b82SDavid Hildenbrand bool ram_block_discard_allowed; 1188bbcb64aSAvihai Horon OnOffAuto enable_migration; 1195e1f8905SAvihai Horon bool migration_events; 120e2c7d025SEric Auger VFIODeviceOps *ops; 121e2c7d025SEric Auger unsigned int num_irqs; 122e2c7d025SEric Auger unsigned int num_regions; 123e2c7d025SEric Auger unsigned int flags; 124a9e271ecSKirti Wankhede VFIOMigration *migration; 125a9e271ecSKirti Wankhede Error *migration_blocker; 126bb0990d1SKirti Wankhede OnOffAuto pre_copy_dirty_page_tracking; 1275255bbf4SJoao Martins bool dirty_pages_supported; 1285255bbf4SJoao Martins bool dirty_tracking; 1295ee3dc7aSYi Liu int devid; 1305ee3dc7aSYi Liu IOMMUFDBackend *iommufd; 131e2c7d025SEric Auger } VFIODevice; 132e2c7d025SEric Auger 133e2c7d025SEric Auger struct VFIODeviceOps { 134e2c7d025SEric Auger void (*vfio_compute_needs_reset)(VFIODevice *vdev); 135e2c7d025SEric Auger int (*vfio_hot_reset_multi)(VFIODevice *vdev); 136e2c7d025SEric Auger void (*vfio_eoi)(VFIODevice *vdev); 137e93b733bSKirti Wankhede Object *(*vfio_get_object)(VFIODevice *vdev); 1383783f814SCédric Le Goater 1393783f814SCédric Le Goater /** 1403783f814SCédric Le Goater * @vfio_save_config 1413783f814SCédric Le Goater * 1423783f814SCédric Le Goater * Save device config state 1433783f814SCédric Le Goater * 1443783f814SCédric Le Goater * @vdev: #VFIODevice for which to save the config 1453783f814SCédric Le Goater * @f: #QEMUFile where to send the data 1463783f814SCédric Le Goater * @errp: pointer to Error*, to store an error if it happens. 1473783f814SCédric Le Goater * 1483783f814SCédric Le Goater * Returns zero to indicate success and negative for error 1493783f814SCédric Le Goater */ 1503783f814SCédric Le Goater int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp); 1513783f814SCédric Le Goater 1523783f814SCédric Le Goater /** 1533783f814SCédric Le Goater * @vfio_load_config 1543783f814SCédric Le Goater * 1553783f814SCédric Le Goater * Load device config state 1563783f814SCédric Le Goater * 1573783f814SCédric Le Goater * @vdev: #VFIODevice for which to load the config 1583783f814SCédric Le Goater * @f: #QEMUFile where to get the data 1593783f814SCédric Le Goater * 1603783f814SCédric Le Goater * Returns zero to indicate success and negative for error 1613783f814SCédric Le Goater */ 162c5e2fb3cSKirti Wankhede int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f); 163e2c7d025SEric Auger }; 164e2c7d025SEric Auger 165e2c7d025SEric Auger typedef struct VFIOGroup { 166e2c7d025SEric Auger int fd; 167e2c7d025SEric Auger int groupid; 168e2c7d025SEric Auger VFIOContainer *container; 169e2c7d025SEric Auger QLIST_HEAD(, VFIODevice) device_list; 170e2c7d025SEric Auger QLIST_ENTRY(VFIOGroup) next; 171e2c7d025SEric Auger QLIST_ENTRY(VFIOGroup) container_next; 172aff92b82SDavid Hildenbrand bool ram_block_discard_allowed; 173e2c7d025SEric Auger } VFIOGroup; 174e2c7d025SEric Auger 1750533739eSZhenzhong Duan #define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio" 1760533739eSZhenzhong Duan 1778b818e05SGerd Hoffmann typedef struct VFIODMABuf { 178c0fcd633SDongwon Kim QemuDmaBuf *buf; 1798b818e05SGerd Hoffmann uint32_t pos_x, pos_y, pos_updates; 1808b818e05SGerd Hoffmann uint32_t hot_x, hot_y, hot_updates; 1818b818e05SGerd Hoffmann int dmabuf_id; 1828b818e05SGerd Hoffmann QTAILQ_ENTRY(VFIODMABuf) next; 1838b818e05SGerd Hoffmann } VFIODMABuf; 1848b818e05SGerd Hoffmann 18500195ba7SGerd Hoffmann typedef struct VFIODisplay { 18600195ba7SGerd Hoffmann QemuConsole *con; 187b290659fSGerd Hoffmann RAMFBState *ramfb; 18808479114SGerd Hoffmann struct vfio_region_info *edid_info; 18908479114SGerd Hoffmann struct vfio_region_gfx_edid *edid_regs; 19008479114SGerd Hoffmann uint8_t *edid_blob; 1918781c701SGerd Hoffmann QEMUTimer *edid_link_timer; 19200195ba7SGerd Hoffmann struct { 19300195ba7SGerd Hoffmann VFIORegion buffer; 19400195ba7SGerd Hoffmann DisplaySurface *surface; 19500195ba7SGerd Hoffmann } region; 1968b818e05SGerd Hoffmann struct { 1978b818e05SGerd Hoffmann QTAILQ_HEAD(, VFIODMABuf) bufs; 1988b818e05SGerd Hoffmann VFIODMABuf *primary; 1998b818e05SGerd Hoffmann VFIODMABuf *cursor; 2008b818e05SGerd Hoffmann } dmabuf; 20100195ba7SGerd Hoffmann } VFIODisplay; 20200195ba7SGerd Hoffmann 2037e63b311SYi Liu VFIOAddressSpace *vfio_get_address_space(AddressSpace *as); 2047e63b311SYi Liu void vfio_put_address_space(VFIOAddressSpace *space); 2057e63b311SYi Liu 206770c3b6eSZhenzhong Duan /* SPAPR specific */ 207770c3b6eSZhenzhong Duan int vfio_spapr_container_init(VFIOContainer *container, Error **errp); 208770c3b6eSZhenzhong Duan void vfio_spapr_container_deinit(VFIOContainer *container); 2097e63b311SYi Liu 210e2c7d025SEric Auger void vfio_disable_irqindex(VFIODevice *vbasedev, int index); 211e2c7d025SEric Auger void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index); 212e2c7d025SEric Auger void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index); 21384e37d02SZhenzhong Duan bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex, 214201a7331SEric Auger int action, int fd, Error **errp); 215e2c7d025SEric Auger void vfio_region_write(void *opaque, hwaddr addr, 216e2c7d025SEric Auger uint64_t data, unsigned size); 217e2c7d025SEric Auger uint64_t vfio_region_read(void *opaque, 218e2c7d025SEric Auger hwaddr addr, unsigned size); 219db0da029SAlex Williamson int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, 220db0da029SAlex Williamson int index, const char *name); 221db0da029SAlex Williamson int vfio_region_mmap(VFIORegion *region); 222db0da029SAlex Williamson void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled); 2230f7a903bSKirti Wankhede void vfio_region_unmap(VFIORegion *region); 224db0da029SAlex Williamson void vfio_region_exit(VFIORegion *region); 225db0da029SAlex Williamson void vfio_region_finalize(VFIORegion *region); 226e2c7d025SEric Auger void vfio_reset_handler(void *opaque); 227634f38f0SAlex Williamson struct vfio_device_info *vfio_get_device_info(int fd); 228b7754835SZhenzhong Duan bool vfio_attach_device(char *name, VFIODevice *vbasedev, 2295456b186SEric Auger AddressSpace *as, Error **errp); 2305456b186SEric Auger void vfio_detach_device(VFIODevice *vbasedev); 231e2c7d025SEric Auger 2325621c02dSZhenzhong Duan int vfio_kvm_device_add_fd(int fd, Error **errp); 2335621c02dSZhenzhong Duan int vfio_kvm_device_del_fd(int fd, Error **errp); 2345621c02dSZhenzhong Duan 235f38f5dd1SZhenzhong Duan bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp); 236d9fa4223SSteve Sistare void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer); 237d9fa4223SSteve Sistare 238e2c7d025SEric Auger extern const MemoryRegionOps vfio_region_ops; 239f481ee2dSPaolo Bonzini typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList; 2403d779abaSZhenzhong Duan typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList; 241f481ee2dSPaolo Bonzini extern VFIOGroupList vfio_group_list; 2427e63b311SYi Liu extern VFIODeviceList vfio_device_list; 2437e63b311SYi Liu extern const MemoryListener vfio_memory_listener; 2447e63b311SYi Liu extern int vfio_kvm_device_fd; 245e2c7d025SEric Auger 2463710586cSKirti Wankhede bool vfio_mig_active(void); 2478bbcb64aSAvihai Horon int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp); 24829d81b71SAvihai Horon void vfio_unblock_multiple_devices_migration(void); 2493c26c80aSZhenzhong Duan bool vfio_viommu_preset(VFIODevice *vbasedev); 2503710586cSKirti Wankhede int64_t vfio_mig_bytes_transferred(void); 251808642a2SAvihai Horon void vfio_reset_bytes_transferred(void); 2523d4d0f0eSJoao Martins bool vfio_device_state_is_running(VFIODevice *vbasedev); 2533d4d0f0eSJoao Martins bool vfio_device_state_is_precopy(VFIODevice *vbasedev); 2543710586cSKirti Wankhede 25546900226SAlex Williamson #ifdef CONFIG_LINUX 25646900226SAlex Williamson int vfio_get_region_info(VFIODevice *vbasedev, int index, 25746900226SAlex Williamson struct vfio_region_info **info); 258e61a424fSAlex Williamson int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, 259e61a424fSAlex Williamson uint32_t subtype, struct vfio_region_info **info); 260ae0215b2SAlexey Kardashevskiy bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); 261013002f0SAlexey Kardashevskiy struct vfio_info_cap_header * 262013002f0SAlexey Kardashevskiy vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id); 2637486a628SMatthew Rosato bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, 2647486a628SMatthew Rosato unsigned int *avail); 26592fe289aSMatthew Rosato struct vfio_info_cap_header * 26692fe289aSMatthew Rosato vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id); 2671e09f52fSYi Liu struct vfio_info_cap_header * 2681e09f52fSYi Liu vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id); 26946900226SAlex Williamson #endif 270318f67ceSAlexey Kardashevskiy 271d4a2af74SZhenzhong Duan bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp); 2728249cffcSAlex Williamson void vfio_migration_exit(VFIODevice *vbasedev); 273a9e271ecSKirti Wankhede 2741e09f52fSYi Liu int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size); 2754517c33cSZhenzhong Duan bool 2764517c33cSZhenzhong Duan vfio_devices_all_running_and_mig_active(const VFIOContainerBase *bcontainer); 2774517c33cSZhenzhong Duan bool 2784517c33cSZhenzhong Duan vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer); 2794517c33cSZhenzhong Duan int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer, 2802da5f9e4SCédric Le Goater VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp); 2814517c33cSZhenzhong Duan int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova, 2822da5f9e4SCédric Le Goater uint64_t size, ram_addr_t ram_addr, Error **errp); 283da3e04b2SZhenzhong Duan 284da3e04b2SZhenzhong Duan /* Returns 0 on success, or a negative errno. */ 285c6c6cf91SZhenzhong Duan bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp); 286da3e04b2SZhenzhong Duan void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp); 2876106a329SZhenzhong Duan void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, 2886106a329SZhenzhong Duan DeviceState *dev, bool ram_discard); 289175de524SMarkus Armbruster #endif /* HW_VFIO_VFIO_COMMON_H */ 290