xref: /qemu/include/hw/vfio/vfio-device.h (revision 504d297e10fdfe1b1243274e334abb0074ee69f4)
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;
119e2c7d025SEric Auger     bool reset_works;
120e2c7d025SEric Auger     bool needs_reset;
1215e15d79bSAlex Williamson     bool no_mmap;
122aff92b82SDavid Hildenbrand     bool ram_block_discard_allowed;
1238bbcb64aSAvihai Horon     OnOffAuto enable_migration;
1245e1f8905SAvihai Horon     bool migration_events;
125e2c7d025SEric Auger     VFIODeviceOps *ops;
126e2c7d025SEric Auger     unsigned int num_irqs;
127e2c7d025SEric Auger     unsigned int num_regions;
128e2c7d025SEric Auger     unsigned int flags;
129a9e271ecSKirti Wankhede     VFIOMigration *migration;
130a9e271ecSKirti Wankhede     Error *migration_blocker;
131bb0990d1SKirti Wankhede     OnOffAuto pre_copy_dirty_page_tracking;
1325255bbf4SJoao Martins     bool dirty_pages_supported;
1335255bbf4SJoao Martins     bool dirty_tracking;
134a7fd91b8SZhenzhong Duan     HostIOMMUDevice *hiod;
1355ee3dc7aSYi Liu     int devid;
1365ee3dc7aSYi Liu     IOMMUFDBackend *iommufd;
137e2c7d025SEric Auger } VFIODevice;
138e2c7d025SEric Auger 
139e2c7d025SEric Auger struct VFIODeviceOps {
140e2c7d025SEric Auger     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
141e2c7d025SEric Auger     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
142e2c7d025SEric Auger     void (*vfio_eoi)(VFIODevice *vdev);
143e93b733bSKirti Wankhede     Object *(*vfio_get_object)(VFIODevice *vdev);
1443783f814SCédric Le Goater 
1453783f814SCédric Le Goater     /**
1463783f814SCédric Le Goater      * @vfio_save_config
1473783f814SCédric Le Goater      *
1483783f814SCédric Le Goater      * Save device config state
1493783f814SCédric Le Goater      *
1503783f814SCédric Le Goater      * @vdev: #VFIODevice for which to save the config
1513783f814SCédric Le Goater      * @f: #QEMUFile where to send the data
1523783f814SCédric Le Goater      * @errp: pointer to Error*, to store an error if it happens.
1533783f814SCédric Le Goater      *
1543783f814SCédric Le Goater      * Returns zero to indicate success and negative for error
1553783f814SCédric Le Goater      */
1563783f814SCédric Le Goater     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
1573783f814SCédric Le Goater 
1583783f814SCédric Le Goater     /**
1593783f814SCédric Le Goater      * @vfio_load_config
1603783f814SCédric Le Goater      *
1613783f814SCédric Le Goater      * Load device config state
1623783f814SCédric Le Goater      *
1633783f814SCédric Le Goater      * @vdev: #VFIODevice for which to load the config
1643783f814SCédric Le Goater      * @f: #QEMUFile where to get the data
1653783f814SCédric Le Goater      *
1663783f814SCédric Le Goater      * Returns zero to indicate success and negative for error
1673783f814SCédric Le Goater      */
168c5e2fb3cSKirti Wankhede     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
169e2c7d025SEric Auger };
170e2c7d025SEric Auger 
171e2c7d025SEric Auger typedef struct VFIOGroup {
172e2c7d025SEric Auger     int fd;
173e2c7d025SEric Auger     int groupid;
174e2c7d025SEric Auger     VFIOContainer *container;
175e2c7d025SEric Auger     QLIST_HEAD(, VFIODevice) device_list;
176e2c7d025SEric Auger     QLIST_ENTRY(VFIOGroup) next;
177e2c7d025SEric Auger     QLIST_ENTRY(VFIOGroup) container_next;
178aff92b82SDavid Hildenbrand     bool ram_block_discard_allowed;
179e2c7d025SEric Auger } VFIOGroup;
180e2c7d025SEric Auger 
1810533739eSZhenzhong Duan #define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio"
1829005f928SZhenzhong Duan #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \
1839005f928SZhenzhong Duan             TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
1840533739eSZhenzhong Duan 
1858b818e05SGerd Hoffmann typedef struct VFIODMABuf {
186c0fcd633SDongwon Kim     QemuDmaBuf *buf;
1878b818e05SGerd Hoffmann     uint32_t pos_x, pos_y, pos_updates;
1888b818e05SGerd Hoffmann     uint32_t hot_x, hot_y, hot_updates;
1898b818e05SGerd Hoffmann     int dmabuf_id;
1908b818e05SGerd Hoffmann     QTAILQ_ENTRY(VFIODMABuf) next;
1918b818e05SGerd Hoffmann } VFIODMABuf;
1928b818e05SGerd Hoffmann 
19300195ba7SGerd Hoffmann typedef struct VFIODisplay {
19400195ba7SGerd Hoffmann     QemuConsole *con;
195b290659fSGerd Hoffmann     RAMFBState *ramfb;
19608479114SGerd Hoffmann     struct vfio_region_info *edid_info;
19708479114SGerd Hoffmann     struct vfio_region_gfx_edid *edid_regs;
19808479114SGerd Hoffmann     uint8_t *edid_blob;
1998781c701SGerd Hoffmann     QEMUTimer *edid_link_timer;
20000195ba7SGerd Hoffmann     struct {
20100195ba7SGerd Hoffmann         VFIORegion buffer;
20200195ba7SGerd Hoffmann         DisplaySurface *surface;
20300195ba7SGerd Hoffmann     } region;
2048b818e05SGerd Hoffmann     struct {
2058b818e05SGerd Hoffmann         QTAILQ_HEAD(, VFIODMABuf) bufs;
2068b818e05SGerd Hoffmann         VFIODMABuf *primary;
2078b818e05SGerd Hoffmann         VFIODMABuf *cursor;
2088b818e05SGerd Hoffmann     } dmabuf;
20900195ba7SGerd Hoffmann } VFIODisplay;
21000195ba7SGerd Hoffmann 
2117e63b311SYi Liu VFIOAddressSpace *vfio_get_address_space(AddressSpace *as);
2127e63b311SYi Liu void vfio_put_address_space(VFIOAddressSpace *space);
213b7b79588SCédric Le Goater void vfio_address_space_insert(VFIOAddressSpace *space,
214b7b79588SCédric Le Goater                                VFIOContainerBase *bcontainer);
2157e63b311SYi Liu 
216e2c7d025SEric Auger void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
217e2c7d025SEric Auger void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
218e2c7d025SEric Auger void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
21984e37d02SZhenzhong Duan bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
220201a7331SEric Auger                             int action, int fd, Error **errp);
221e2c7d025SEric Auger void vfio_region_write(void *opaque, hwaddr addr,
222e2c7d025SEric Auger                            uint64_t data, unsigned size);
223e2c7d025SEric Auger uint64_t vfio_region_read(void *opaque,
224e2c7d025SEric Auger                           hwaddr addr, unsigned size);
225db0da029SAlex Williamson int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
226db0da029SAlex Williamson                       int index, const char *name);
227db0da029SAlex Williamson int vfio_region_mmap(VFIORegion *region);
228db0da029SAlex Williamson void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
2290f7a903bSKirti Wankhede void vfio_region_unmap(VFIORegion *region);
230db0da029SAlex Williamson void vfio_region_exit(VFIORegion *region);
231db0da029SAlex Williamson void vfio_region_finalize(VFIORegion *region);
232e2c7d025SEric Auger void vfio_reset_handler(void *opaque);
233634f38f0SAlex Williamson struct vfio_device_info *vfio_get_device_info(int fd);
234b7754835SZhenzhong Duan bool vfio_attach_device(char *name, VFIODevice *vbasedev,
2355456b186SEric Auger                         AddressSpace *as, Error **errp);
2365456b186SEric Auger void vfio_detach_device(VFIODevice *vbasedev);
237e2c7d025SEric Auger 
2385621c02dSZhenzhong Duan int vfio_kvm_device_add_fd(int fd, Error **errp);
2395621c02dSZhenzhong Duan int vfio_kvm_device_del_fd(int fd, Error **errp);
2405621c02dSZhenzhong Duan 
241f38f5dd1SZhenzhong Duan bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp);
242d9fa4223SSteve Sistare void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer);
243d9fa4223SSteve Sistare 
244e2c7d025SEric Auger extern const MemoryRegionOps vfio_region_ops;
245f481ee2dSPaolo Bonzini typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
2463d779abaSZhenzhong Duan typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
247f481ee2dSPaolo Bonzini extern VFIOGroupList vfio_group_list;
2487e63b311SYi Liu extern VFIODeviceList vfio_device_list;
2497e63b311SYi Liu extern const MemoryListener vfio_memory_listener;
2507e63b311SYi Liu extern int vfio_kvm_device_fd;
251e2c7d025SEric Auger 
2523710586cSKirti Wankhede bool vfio_mig_active(void);
2538bbcb64aSAvihai Horon int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp);
25429d81b71SAvihai Horon void vfio_unblock_multiple_devices_migration(void);
2553c26c80aSZhenzhong Duan bool vfio_viommu_preset(VFIODevice *vbasedev);
2563710586cSKirti Wankhede int64_t vfio_mig_bytes_transferred(void);
257808642a2SAvihai Horon void vfio_reset_bytes_transferred(void);
2583d4d0f0eSJoao Martins bool vfio_device_state_is_running(VFIODevice *vbasedev);
2593d4d0f0eSJoao Martins bool vfio_device_state_is_precopy(VFIODevice *vbasedev);
2603710586cSKirti Wankhede 
26146900226SAlex Williamson #ifdef CONFIG_LINUX
26246900226SAlex Williamson int vfio_get_region_info(VFIODevice *vbasedev, int index,
26346900226SAlex Williamson                          struct vfio_region_info **info);
264e61a424fSAlex Williamson int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
265e61a424fSAlex Williamson                              uint32_t subtype, struct vfio_region_info **info);
266ae0215b2SAlexey Kardashevskiy bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
267013002f0SAlexey Kardashevskiy struct vfio_info_cap_header *
268013002f0SAlexey Kardashevskiy vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id);
2697486a628SMatthew Rosato bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,
2707486a628SMatthew Rosato                              unsigned int *avail);
27192fe289aSMatthew Rosato struct vfio_info_cap_header *
27292fe289aSMatthew Rosato vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id);
2731e09f52fSYi Liu struct vfio_info_cap_header *
2741e09f52fSYi Liu vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id);
27546900226SAlex Williamson #endif
276318f67ceSAlexey Kardashevskiy 
277d4a2af74SZhenzhong Duan bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
2788249cffcSAlex Williamson void vfio_migration_exit(VFIODevice *vbasedev);
279a9e271ecSKirti Wankhede 
2801e09f52fSYi Liu int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size);
2814517c33cSZhenzhong Duan bool
2824517c33cSZhenzhong Duan vfio_devices_all_running_and_mig_active(const VFIOContainerBase *bcontainer);
2834517c33cSZhenzhong Duan bool
2844517c33cSZhenzhong Duan vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer);
2854517c33cSZhenzhong Duan int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
2862da5f9e4SCédric Le Goater                 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
2874517c33cSZhenzhong Duan int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
2882da5f9e4SCédric Le Goater                           uint64_t size, ram_addr_t ram_addr, Error **errp);
289da3e04b2SZhenzhong Duan 
290da3e04b2SZhenzhong Duan /* Returns 0 on success, or a negative errno. */
291c6c6cf91SZhenzhong Duan bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
292da3e04b2SZhenzhong Duan void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
2936106a329SZhenzhong Duan void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
2946106a329SZhenzhong Duan                       DeviceState *dev, bool ram_discard);
295d441e05eSZhenzhong Duan int vfio_device_get_aw_bits(VFIODevice *vdev);
296175de524SMarkus Armbruster #endif /* HW_VFIO_VFIO_COMMON_H */
297