xref: /qemu/include/hw/vfio/vfio-device.h (revision 13e522f644e2b15fa857028a33e6a3b75e45158d)
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"
359005f928SZhenzhong Duan #include "sysemu/iommufd.h"
36e2c7d025SEric Auger 
37e1eb292aSMarkus Armbruster #define VFIO_MSG_PREFIX "vfio %s: "
38426ec904SEric Auger 
39e2c7d025SEric Auger enum {
40e2c7d025SEric Auger     VFIO_DEVICE_TYPE_PCI = 0,
410ea2730bSEric Auger     VFIO_DEVICE_TYPE_PLATFORM = 1,
421dcac3e1SXiao Feng Ren     VFIO_DEVICE_TYPE_CCW = 2,
432fe2942cSTony Krowiak     VFIO_DEVICE_TYPE_AP = 3,
44e2c7d025SEric Auger };
45e2c7d025SEric Auger 
46db0da029SAlex Williamson typedef struct VFIOMmap {
47db0da029SAlex Williamson     MemoryRegion mem;
48db0da029SAlex Williamson     void *mmap;
49db0da029SAlex Williamson     off_t offset;
50db0da029SAlex Williamson     size_t size;
51db0da029SAlex Williamson } VFIOMmap;
52db0da029SAlex Williamson 
53e2c7d025SEric Auger typedef struct VFIORegion {
54e2c7d025SEric Auger     struct VFIODevice *vbasedev;
55e2c7d025SEric Auger     off_t fd_offset; /* offset of region within device fd */
56db0da029SAlex Williamson     MemoryRegion *mem; /* slow, read/write access */
57e2c7d025SEric Auger     size_t size;
58e2c7d025SEric Auger     uint32_t flags; /* VFIO region flags (rd/wr/mmap) */
59db0da029SAlex Williamson     uint32_t nr_mmaps;
60db0da029SAlex Williamson     VFIOMmap *mmaps;
61e2c7d025SEric Auger     uint8_t nr; /* cache the region number for debug */
62e2c7d025SEric Auger } VFIORegion;
63e2c7d025SEric Auger 
64a9e271ecSKirti Wankhede typedef struct VFIOMigration {
65050c588cSKirti Wankhede     struct VFIODevice *vbasedev;
6602a7e71bSKirti Wankhede     VMChangeStateEntry *vm_state;
673e775730SSteve Sistare     NotifierWithReturn migration_state;
6831bcbbb5SAvihai Horon     uint32_t device_state;
6931bcbbb5SAvihai Horon     int data_fd;
7031bcbbb5SAvihai Horon     void *data_buffer;
7131bcbbb5SAvihai Horon     size_t data_buffer_size;
726cd1fe11SAvihai Horon     uint64_t mig_flags;
73eda7362aSAvihai Horon     uint64_t precopy_init_size;
74eda7362aSAvihai Horon     uint64_t precopy_dirty_size;
75745c4291SAvihai Horon     bool initial_data_sent;
76a9e271ecSKirti Wankhede } VFIOMigration;
77a9e271ecSKirti Wankhede 
78e2c7d025SEric Auger struct VFIOGroup;
79e2c7d025SEric Auger 
80e2c7d025SEric Auger typedef struct VFIOContainer {
81f61dddd7SZhenzhong Duan     VFIOContainerBase bcontainer;
82e2c7d025SEric Auger     int fd; /* /dev/vfio/vfio, empowered by the attached groups */
83318f67ceSAlexey Kardashevskiy     unsigned iommu_type;
84e2c7d025SEric Auger     QLIST_HEAD(, VFIOGroup) group_list;
85e2c7d025SEric Auger } VFIOContainer;
86e2c7d025SEric Auger 
87504d297eSCédric Le Goater OBJECT_DECLARE_SIMPLE_TYPE(VFIOContainer, VFIO_IOMMU_LEGACY);
88504d297eSCédric Le Goater 
89f4ec5e26SAlexey Kardashevskiy typedef struct VFIOHostDMAWindow {
90f4ec5e26SAlexey Kardashevskiy     hwaddr min_iova;
91f4ec5e26SAlexey Kardashevskiy     hwaddr max_iova;
92f4ec5e26SAlexey Kardashevskiy     uint64_t iova_pgsizes;
93f4ec5e26SAlexey Kardashevskiy     QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next;
94f4ec5e26SAlexey Kardashevskiy } VFIOHostDMAWindow;
95f4ec5e26SAlexey Kardashevskiy 
965ee3dc7aSYi Liu typedef struct IOMMUFDBackend IOMMUFDBackend;
975ee3dc7aSYi Liu 
985ee3dc7aSYi Liu typedef struct VFIOIOMMUFDContainer {
995ee3dc7aSYi Liu     VFIOContainerBase bcontainer;
1005ee3dc7aSYi Liu     IOMMUFDBackend *be;
1015ee3dc7aSYi Liu     uint32_t ioas_id;
1025ee3dc7aSYi Liu } VFIOIOMMUFDContainer;
1035ee3dc7aSYi Liu 
104504d297eSCédric Le Goater OBJECT_DECLARE_SIMPLE_TYPE(VFIOIOMMUFDContainer, VFIO_IOMMU_IOMMUFD);
105504d297eSCédric Le Goater 
106e2c7d025SEric Auger typedef struct VFIODeviceOps VFIODeviceOps;
107e2c7d025SEric Auger 
108e2c7d025SEric Auger typedef struct VFIODevice {
109e2c7d025SEric Auger     QLIST_ENTRY(VFIODevice) next;
1107103ef7eSZhenzhong Duan     QLIST_ENTRY(VFIODevice) container_next;
1113d779abaSZhenzhong Duan     QLIST_ENTRY(VFIODevice) global_next;
112e2c7d025SEric Auger     struct VFIOGroup *group;
1133e6015d1SZhenzhong Duan     VFIOContainerBase *bcontainer;
1147df9381bSAlex Williamson     char *sysfsdev;
115e2c7d025SEric Auger     char *name;
1167da624e2SAlex Williamson     DeviceState *dev;
117e2c7d025SEric Auger     int fd;
118e2c7d025SEric Auger     int type;
11913e522f6SJoao Martins     bool mdev;
120e2c7d025SEric Auger     bool reset_works;
121e2c7d025SEric Auger     bool needs_reset;
1225e15d79bSAlex Williamson     bool no_mmap;
123aff92b82SDavid Hildenbrand     bool ram_block_discard_allowed;
1248bbcb64aSAvihai Horon     OnOffAuto enable_migration;
1255e1f8905SAvihai Horon     bool migration_events;
126e2c7d025SEric Auger     VFIODeviceOps *ops;
127e2c7d025SEric Auger     unsigned int num_irqs;
128e2c7d025SEric Auger     unsigned int num_regions;
129e2c7d025SEric Auger     unsigned int flags;
130a9e271ecSKirti Wankhede     VFIOMigration *migration;
131a9e271ecSKirti Wankhede     Error *migration_blocker;
132bb0990d1SKirti Wankhede     OnOffAuto pre_copy_dirty_page_tracking;
1335255bbf4SJoao Martins     bool dirty_pages_supported;
1345255bbf4SJoao Martins     bool dirty_tracking;
135a7fd91b8SZhenzhong Duan     HostIOMMUDevice *hiod;
1365ee3dc7aSYi Liu     int devid;
1375ee3dc7aSYi Liu     IOMMUFDBackend *iommufd;
138e2c7d025SEric Auger } VFIODevice;
139e2c7d025SEric Auger 
140e2c7d025SEric Auger struct VFIODeviceOps {
141e2c7d025SEric Auger     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
142e2c7d025SEric Auger     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
143e2c7d025SEric Auger     void (*vfio_eoi)(VFIODevice *vdev);
144e93b733bSKirti Wankhede     Object *(*vfio_get_object)(VFIODevice *vdev);
1453783f814SCédric Le Goater 
1463783f814SCédric Le Goater     /**
1473783f814SCédric Le Goater      * @vfio_save_config
1483783f814SCédric Le Goater      *
1493783f814SCédric Le Goater      * Save device config state
1503783f814SCédric Le Goater      *
1513783f814SCédric Le Goater      * @vdev: #VFIODevice for which to save the config
1523783f814SCédric Le Goater      * @f: #QEMUFile where to send the data
1533783f814SCédric Le Goater      * @errp: pointer to Error*, to store an error if it happens.
1543783f814SCédric Le Goater      *
1553783f814SCédric Le Goater      * Returns zero to indicate success and negative for error
1563783f814SCédric Le Goater      */
1573783f814SCédric Le Goater     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
1583783f814SCédric Le Goater 
1593783f814SCédric Le Goater     /**
1603783f814SCédric Le Goater      * @vfio_load_config
1613783f814SCédric Le Goater      *
1623783f814SCédric Le Goater      * Load device config state
1633783f814SCédric Le Goater      *
1643783f814SCédric Le Goater      * @vdev: #VFIODevice for which to load the config
1653783f814SCédric Le Goater      * @f: #QEMUFile where to get the data
1663783f814SCédric Le Goater      *
1673783f814SCédric Le Goater      * Returns zero to indicate success and negative for error
1683783f814SCédric Le Goater      */
169c5e2fb3cSKirti Wankhede     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
170e2c7d025SEric Auger };
171e2c7d025SEric Auger 
172e2c7d025SEric Auger typedef struct VFIOGroup {
173e2c7d025SEric Auger     int fd;
174e2c7d025SEric Auger     int groupid;
175e2c7d025SEric Auger     VFIOContainer *container;
176e2c7d025SEric Auger     QLIST_HEAD(, VFIODevice) device_list;
177e2c7d025SEric Auger     QLIST_ENTRY(VFIOGroup) next;
178e2c7d025SEric Auger     QLIST_ENTRY(VFIOGroup) container_next;
179aff92b82SDavid Hildenbrand     bool ram_block_discard_allowed;
180e2c7d025SEric Auger } VFIOGroup;
181e2c7d025SEric Auger 
1820533739eSZhenzhong Duan #define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio"
1839005f928SZhenzhong Duan #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \
1849005f928SZhenzhong Duan             TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
1850533739eSZhenzhong Duan 
1868b818e05SGerd Hoffmann typedef struct VFIODMABuf {
187c0fcd633SDongwon Kim     QemuDmaBuf *buf;
1888b818e05SGerd Hoffmann     uint32_t pos_x, pos_y, pos_updates;
1898b818e05SGerd Hoffmann     uint32_t hot_x, hot_y, hot_updates;
1908b818e05SGerd Hoffmann     int dmabuf_id;
1918b818e05SGerd Hoffmann     QTAILQ_ENTRY(VFIODMABuf) next;
1928b818e05SGerd Hoffmann } VFIODMABuf;
1938b818e05SGerd Hoffmann 
19400195ba7SGerd Hoffmann typedef struct VFIODisplay {
19500195ba7SGerd Hoffmann     QemuConsole *con;
196b290659fSGerd Hoffmann     RAMFBState *ramfb;
19708479114SGerd Hoffmann     struct vfio_region_info *edid_info;
19808479114SGerd Hoffmann     struct vfio_region_gfx_edid *edid_regs;
19908479114SGerd Hoffmann     uint8_t *edid_blob;
2008781c701SGerd Hoffmann     QEMUTimer *edid_link_timer;
20100195ba7SGerd Hoffmann     struct {
20200195ba7SGerd Hoffmann         VFIORegion buffer;
20300195ba7SGerd Hoffmann         DisplaySurface *surface;
20400195ba7SGerd Hoffmann     } region;
2058b818e05SGerd Hoffmann     struct {
2068b818e05SGerd Hoffmann         QTAILQ_HEAD(, VFIODMABuf) bufs;
2078b818e05SGerd Hoffmann         VFIODMABuf *primary;
2088b818e05SGerd Hoffmann         VFIODMABuf *cursor;
2098b818e05SGerd Hoffmann     } dmabuf;
21000195ba7SGerd Hoffmann } VFIODisplay;
21100195ba7SGerd Hoffmann 
2127e63b311SYi Liu VFIOAddressSpace *vfio_get_address_space(AddressSpace *as);
2137e63b311SYi Liu void vfio_put_address_space(VFIOAddressSpace *space);
214b7b79588SCédric Le Goater void vfio_address_space_insert(VFIOAddressSpace *space,
215b7b79588SCédric Le Goater                                VFIOContainerBase *bcontainer);
2167e63b311SYi Liu 
217e2c7d025SEric Auger void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
218e2c7d025SEric Auger void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
219e2c7d025SEric Auger void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
22084e37d02SZhenzhong Duan bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
221201a7331SEric Auger                             int action, int fd, Error **errp);
222e2c7d025SEric Auger void vfio_region_write(void *opaque, hwaddr addr,
223e2c7d025SEric Auger                            uint64_t data, unsigned size);
224e2c7d025SEric Auger uint64_t vfio_region_read(void *opaque,
225e2c7d025SEric Auger                           hwaddr addr, unsigned size);
226db0da029SAlex Williamson int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
227db0da029SAlex Williamson                       int index, const char *name);
228db0da029SAlex Williamson int vfio_region_mmap(VFIORegion *region);
229db0da029SAlex Williamson void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
2300f7a903bSKirti Wankhede void vfio_region_unmap(VFIORegion *region);
231db0da029SAlex Williamson void vfio_region_exit(VFIORegion *region);
232db0da029SAlex Williamson void vfio_region_finalize(VFIORegion *region);
233e2c7d025SEric Auger void vfio_reset_handler(void *opaque);
234634f38f0SAlex Williamson struct vfio_device_info *vfio_get_device_info(int fd);
23513e522f6SJoao Martins bool vfio_device_is_mdev(VFIODevice *vbasedev);
236b7754835SZhenzhong Duan bool vfio_attach_device(char *name, VFIODevice *vbasedev,
2375456b186SEric Auger                         AddressSpace *as, Error **errp);
2385456b186SEric Auger void vfio_detach_device(VFIODevice *vbasedev);
239e2c7d025SEric Auger 
2405621c02dSZhenzhong Duan int vfio_kvm_device_add_fd(int fd, Error **errp);
2415621c02dSZhenzhong Duan int vfio_kvm_device_del_fd(int fd, Error **errp);
2425621c02dSZhenzhong Duan 
243f38f5dd1SZhenzhong Duan bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp);
244d9fa4223SSteve Sistare void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer);
245d9fa4223SSteve Sistare 
246e2c7d025SEric Auger extern const MemoryRegionOps vfio_region_ops;
247f481ee2dSPaolo Bonzini typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
2483d779abaSZhenzhong Duan typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
249f481ee2dSPaolo Bonzini extern VFIOGroupList vfio_group_list;
2507e63b311SYi Liu extern VFIODeviceList vfio_device_list;
2517e63b311SYi Liu extern const MemoryListener vfio_memory_listener;
2527e63b311SYi Liu extern int vfio_kvm_device_fd;
253e2c7d025SEric Auger 
2543710586cSKirti Wankhede bool vfio_mig_active(void);
2558bbcb64aSAvihai Horon int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp);
25629d81b71SAvihai Horon void vfio_unblock_multiple_devices_migration(void);
2573c26c80aSZhenzhong Duan bool vfio_viommu_preset(VFIODevice *vbasedev);
2583710586cSKirti Wankhede int64_t vfio_mig_bytes_transferred(void);
259808642a2SAvihai Horon void vfio_reset_bytes_transferred(void);
2603d4d0f0eSJoao Martins bool vfio_device_state_is_running(VFIODevice *vbasedev);
2613d4d0f0eSJoao Martins bool vfio_device_state_is_precopy(VFIODevice *vbasedev);
2623710586cSKirti Wankhede 
26346900226SAlex Williamson #ifdef CONFIG_LINUX
26446900226SAlex Williamson int vfio_get_region_info(VFIODevice *vbasedev, int index,
26546900226SAlex Williamson                          struct vfio_region_info **info);
266e61a424fSAlex Williamson int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
267e61a424fSAlex Williamson                              uint32_t subtype, struct vfio_region_info **info);
268ae0215b2SAlexey Kardashevskiy bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
269013002f0SAlexey Kardashevskiy struct vfio_info_cap_header *
270013002f0SAlexey Kardashevskiy vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id);
2717486a628SMatthew Rosato bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,
2727486a628SMatthew Rosato                              unsigned int *avail);
27392fe289aSMatthew Rosato struct vfio_info_cap_header *
27492fe289aSMatthew Rosato vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id);
2751e09f52fSYi Liu struct vfio_info_cap_header *
2761e09f52fSYi Liu vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id);
27746900226SAlex Williamson #endif
278318f67ceSAlexey Kardashevskiy 
279d4a2af74SZhenzhong Duan bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
2808249cffcSAlex Williamson void vfio_migration_exit(VFIODevice *vbasedev);
281a9e271ecSKirti Wankhede 
2821e09f52fSYi Liu int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size);
2834517c33cSZhenzhong Duan bool
2844517c33cSZhenzhong Duan vfio_devices_all_running_and_mig_active(const VFIOContainerBase *bcontainer);
2854517c33cSZhenzhong Duan bool
2864517c33cSZhenzhong Duan vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer);
2874517c33cSZhenzhong Duan int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
2882da5f9e4SCédric Le Goater                 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
2894517c33cSZhenzhong Duan int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
2902da5f9e4SCédric Le Goater                           uint64_t size, ram_addr_t ram_addr, Error **errp);
291da3e04b2SZhenzhong Duan 
292da3e04b2SZhenzhong Duan /* Returns 0 on success, or a negative errno. */
293c6c6cf91SZhenzhong Duan bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
294da3e04b2SZhenzhong Duan void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
2956106a329SZhenzhong Duan void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
2966106a329SZhenzhong Duan                       DeviceState *dev, bool ram_discard);
297d441e05eSZhenzhong Duan int vfio_device_get_aw_bits(VFIODevice *vdev);
298175de524SMarkus Armbruster #endif /* HW_VFIO_VFIO_COMMON_H */
299