xref: /qemu/include/hw/vfio/vfio-device.h (revision b7b79588ebb365e157ec2425a4fa472a314c3ea5)
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 
87f4ec5e26SAlexey Kardashevskiy typedef struct VFIOHostDMAWindow {
88f4ec5e26SAlexey Kardashevskiy     hwaddr min_iova;
89f4ec5e26SAlexey Kardashevskiy     hwaddr max_iova;
90f4ec5e26SAlexey Kardashevskiy     uint64_t iova_pgsizes;
91f4ec5e26SAlexey Kardashevskiy     QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next;
92f4ec5e26SAlexey Kardashevskiy } VFIOHostDMAWindow;
93f4ec5e26SAlexey Kardashevskiy 
945ee3dc7aSYi Liu typedef struct IOMMUFDBackend IOMMUFDBackend;
955ee3dc7aSYi Liu 
965ee3dc7aSYi Liu typedef struct VFIOIOMMUFDContainer {
975ee3dc7aSYi Liu     VFIOContainerBase bcontainer;
985ee3dc7aSYi Liu     IOMMUFDBackend *be;
995ee3dc7aSYi Liu     uint32_t ioas_id;
1005ee3dc7aSYi Liu } VFIOIOMMUFDContainer;
1015ee3dc7aSYi Liu 
102e2c7d025SEric Auger typedef struct VFIODeviceOps VFIODeviceOps;
103e2c7d025SEric Auger 
104e2c7d025SEric Auger typedef struct VFIODevice {
105e2c7d025SEric Auger     QLIST_ENTRY(VFIODevice) next;
1067103ef7eSZhenzhong Duan     QLIST_ENTRY(VFIODevice) container_next;
1073d779abaSZhenzhong Duan     QLIST_ENTRY(VFIODevice) global_next;
108e2c7d025SEric Auger     struct VFIOGroup *group;
1093e6015d1SZhenzhong Duan     VFIOContainerBase *bcontainer;
1107df9381bSAlex Williamson     char *sysfsdev;
111e2c7d025SEric Auger     char *name;
1127da624e2SAlex Williamson     DeviceState *dev;
113e2c7d025SEric Auger     int fd;
114e2c7d025SEric Auger     int type;
115e2c7d025SEric Auger     bool reset_works;
116e2c7d025SEric Auger     bool needs_reset;
1175e15d79bSAlex Williamson     bool no_mmap;
118aff92b82SDavid Hildenbrand     bool ram_block_discard_allowed;
1198bbcb64aSAvihai Horon     OnOffAuto enable_migration;
1205e1f8905SAvihai Horon     bool migration_events;
121e2c7d025SEric Auger     VFIODeviceOps *ops;
122e2c7d025SEric Auger     unsigned int num_irqs;
123e2c7d025SEric Auger     unsigned int num_regions;
124e2c7d025SEric Auger     unsigned int flags;
125a9e271ecSKirti Wankhede     VFIOMigration *migration;
126a9e271ecSKirti Wankhede     Error *migration_blocker;
127bb0990d1SKirti Wankhede     OnOffAuto pre_copy_dirty_page_tracking;
1285255bbf4SJoao Martins     bool dirty_pages_supported;
1295255bbf4SJoao Martins     bool dirty_tracking;
130a7fd91b8SZhenzhong Duan     HostIOMMUDevice *hiod;
1315ee3dc7aSYi Liu     int devid;
1325ee3dc7aSYi Liu     IOMMUFDBackend *iommufd;
133e2c7d025SEric Auger } VFIODevice;
134e2c7d025SEric Auger 
135e2c7d025SEric Auger struct VFIODeviceOps {
136e2c7d025SEric Auger     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
137e2c7d025SEric Auger     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
138e2c7d025SEric Auger     void (*vfio_eoi)(VFIODevice *vdev);
139e93b733bSKirti Wankhede     Object *(*vfio_get_object)(VFIODevice *vdev);
1403783f814SCédric Le Goater 
1413783f814SCédric Le Goater     /**
1423783f814SCédric Le Goater      * @vfio_save_config
1433783f814SCédric Le Goater      *
1443783f814SCédric Le Goater      * Save device config state
1453783f814SCédric Le Goater      *
1463783f814SCédric Le Goater      * @vdev: #VFIODevice for which to save the config
1473783f814SCédric Le Goater      * @f: #QEMUFile where to send the data
1483783f814SCédric Le Goater      * @errp: pointer to Error*, to store an error if it happens.
1493783f814SCédric Le Goater      *
1503783f814SCédric Le Goater      * Returns zero to indicate success and negative for error
1513783f814SCédric Le Goater      */
1523783f814SCédric Le Goater     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
1533783f814SCédric Le Goater 
1543783f814SCédric Le Goater     /**
1553783f814SCédric Le Goater      * @vfio_load_config
1563783f814SCédric Le Goater      *
1573783f814SCédric Le Goater      * Load device config state
1583783f814SCédric Le Goater      *
1593783f814SCédric Le Goater      * @vdev: #VFIODevice for which to load the config
1603783f814SCédric Le Goater      * @f: #QEMUFile where to get the data
1613783f814SCédric Le Goater      *
1623783f814SCédric Le Goater      * Returns zero to indicate success and negative for error
1633783f814SCédric Le Goater      */
164c5e2fb3cSKirti Wankhede     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
165e2c7d025SEric Auger };
166e2c7d025SEric Auger 
167e2c7d025SEric Auger typedef struct VFIOGroup {
168e2c7d025SEric Auger     int fd;
169e2c7d025SEric Auger     int groupid;
170e2c7d025SEric Auger     VFIOContainer *container;
171e2c7d025SEric Auger     QLIST_HEAD(, VFIODevice) device_list;
172e2c7d025SEric Auger     QLIST_ENTRY(VFIOGroup) next;
173e2c7d025SEric Auger     QLIST_ENTRY(VFIOGroup) container_next;
174aff92b82SDavid Hildenbrand     bool ram_block_discard_allowed;
175e2c7d025SEric Auger } VFIOGroup;
176e2c7d025SEric Auger 
1770533739eSZhenzhong Duan #define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio"
1789005f928SZhenzhong Duan #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \
1799005f928SZhenzhong Duan             TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
1800533739eSZhenzhong Duan 
1818b818e05SGerd Hoffmann typedef struct VFIODMABuf {
182c0fcd633SDongwon Kim     QemuDmaBuf *buf;
1838b818e05SGerd Hoffmann     uint32_t pos_x, pos_y, pos_updates;
1848b818e05SGerd Hoffmann     uint32_t hot_x, hot_y, hot_updates;
1858b818e05SGerd Hoffmann     int dmabuf_id;
1868b818e05SGerd Hoffmann     QTAILQ_ENTRY(VFIODMABuf) next;
1878b818e05SGerd Hoffmann } VFIODMABuf;
1888b818e05SGerd Hoffmann 
18900195ba7SGerd Hoffmann typedef struct VFIODisplay {
19000195ba7SGerd Hoffmann     QemuConsole *con;
191b290659fSGerd Hoffmann     RAMFBState *ramfb;
19208479114SGerd Hoffmann     struct vfio_region_info *edid_info;
19308479114SGerd Hoffmann     struct vfio_region_gfx_edid *edid_regs;
19408479114SGerd Hoffmann     uint8_t *edid_blob;
1958781c701SGerd Hoffmann     QEMUTimer *edid_link_timer;
19600195ba7SGerd Hoffmann     struct {
19700195ba7SGerd Hoffmann         VFIORegion buffer;
19800195ba7SGerd Hoffmann         DisplaySurface *surface;
19900195ba7SGerd Hoffmann     } region;
2008b818e05SGerd Hoffmann     struct {
2018b818e05SGerd Hoffmann         QTAILQ_HEAD(, VFIODMABuf) bufs;
2028b818e05SGerd Hoffmann         VFIODMABuf *primary;
2038b818e05SGerd Hoffmann         VFIODMABuf *cursor;
2048b818e05SGerd Hoffmann     } dmabuf;
20500195ba7SGerd Hoffmann } VFIODisplay;
20600195ba7SGerd Hoffmann 
2077e63b311SYi Liu VFIOAddressSpace *vfio_get_address_space(AddressSpace *as);
2087e63b311SYi Liu void vfio_put_address_space(VFIOAddressSpace *space);
209b7b79588SCédric Le Goater void vfio_address_space_insert(VFIOAddressSpace *space,
210b7b79588SCédric Le Goater                                VFIOContainerBase *bcontainer);
2117e63b311SYi Liu 
212e2c7d025SEric Auger void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
213e2c7d025SEric Auger void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
214e2c7d025SEric Auger void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
21584e37d02SZhenzhong Duan bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
216201a7331SEric Auger                             int action, int fd, Error **errp);
217e2c7d025SEric Auger void vfio_region_write(void *opaque, hwaddr addr,
218e2c7d025SEric Auger                            uint64_t data, unsigned size);
219e2c7d025SEric Auger uint64_t vfio_region_read(void *opaque,
220e2c7d025SEric Auger                           hwaddr addr, unsigned size);
221db0da029SAlex Williamson int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
222db0da029SAlex Williamson                       int index, const char *name);
223db0da029SAlex Williamson int vfio_region_mmap(VFIORegion *region);
224db0da029SAlex Williamson void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
2250f7a903bSKirti Wankhede void vfio_region_unmap(VFIORegion *region);
226db0da029SAlex Williamson void vfio_region_exit(VFIORegion *region);
227db0da029SAlex Williamson void vfio_region_finalize(VFIORegion *region);
228e2c7d025SEric Auger void vfio_reset_handler(void *opaque);
229634f38f0SAlex Williamson struct vfio_device_info *vfio_get_device_info(int fd);
230b7754835SZhenzhong Duan bool vfio_attach_device(char *name, VFIODevice *vbasedev,
2315456b186SEric Auger                         AddressSpace *as, Error **errp);
2325456b186SEric Auger void vfio_detach_device(VFIODevice *vbasedev);
233e2c7d025SEric Auger 
2345621c02dSZhenzhong Duan int vfio_kvm_device_add_fd(int fd, Error **errp);
2355621c02dSZhenzhong Duan int vfio_kvm_device_del_fd(int fd, Error **errp);
2365621c02dSZhenzhong Duan 
237f38f5dd1SZhenzhong Duan bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp);
238d9fa4223SSteve Sistare void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer);
239d9fa4223SSteve Sistare 
240e2c7d025SEric Auger extern const MemoryRegionOps vfio_region_ops;
241f481ee2dSPaolo Bonzini typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
2423d779abaSZhenzhong Duan typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
243f481ee2dSPaolo Bonzini extern VFIOGroupList vfio_group_list;
2447e63b311SYi Liu extern VFIODeviceList vfio_device_list;
2457e63b311SYi Liu extern const MemoryListener vfio_memory_listener;
2467e63b311SYi Liu extern int vfio_kvm_device_fd;
247e2c7d025SEric Auger 
2483710586cSKirti Wankhede bool vfio_mig_active(void);
2498bbcb64aSAvihai Horon int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp);
25029d81b71SAvihai Horon void vfio_unblock_multiple_devices_migration(void);
2513c26c80aSZhenzhong Duan bool vfio_viommu_preset(VFIODevice *vbasedev);
2523710586cSKirti Wankhede int64_t vfio_mig_bytes_transferred(void);
253808642a2SAvihai Horon void vfio_reset_bytes_transferred(void);
2543d4d0f0eSJoao Martins bool vfio_device_state_is_running(VFIODevice *vbasedev);
2553d4d0f0eSJoao Martins bool vfio_device_state_is_precopy(VFIODevice *vbasedev);
2563710586cSKirti Wankhede 
25746900226SAlex Williamson #ifdef CONFIG_LINUX
25846900226SAlex Williamson int vfio_get_region_info(VFIODevice *vbasedev, int index,
25946900226SAlex Williamson                          struct vfio_region_info **info);
260e61a424fSAlex Williamson int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
261e61a424fSAlex Williamson                              uint32_t subtype, struct vfio_region_info **info);
262ae0215b2SAlexey Kardashevskiy bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
263013002f0SAlexey Kardashevskiy struct vfio_info_cap_header *
264013002f0SAlexey Kardashevskiy vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id);
2657486a628SMatthew Rosato bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,
2667486a628SMatthew Rosato                              unsigned int *avail);
26792fe289aSMatthew Rosato struct vfio_info_cap_header *
26892fe289aSMatthew Rosato vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id);
2691e09f52fSYi Liu struct vfio_info_cap_header *
2701e09f52fSYi Liu vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id);
27146900226SAlex Williamson #endif
272318f67ceSAlexey Kardashevskiy 
273d4a2af74SZhenzhong Duan bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
2748249cffcSAlex Williamson void vfio_migration_exit(VFIODevice *vbasedev);
275a9e271ecSKirti Wankhede 
2761e09f52fSYi Liu int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size);
2774517c33cSZhenzhong Duan bool
2784517c33cSZhenzhong Duan vfio_devices_all_running_and_mig_active(const VFIOContainerBase *bcontainer);
2794517c33cSZhenzhong Duan bool
2804517c33cSZhenzhong Duan vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer);
2814517c33cSZhenzhong Duan int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
2822da5f9e4SCédric Le Goater                 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
2834517c33cSZhenzhong Duan int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
2842da5f9e4SCédric Le Goater                           uint64_t size, ram_addr_t ram_addr, Error **errp);
285da3e04b2SZhenzhong Duan 
286da3e04b2SZhenzhong Duan /* Returns 0 on success, or a negative errno. */
287c6c6cf91SZhenzhong Duan bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
288da3e04b2SZhenzhong Duan void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
2896106a329SZhenzhong Duan void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
2906106a329SZhenzhong Duan                       DeviceState *dev, bool ram_discard);
291d441e05eSZhenzhong Duan int vfio_device_get_aw_bits(VFIODevice *vdev);
292175de524SMarkus Armbruster #endif /* HW_VFIO_VFIO_COMMON_H */
293