xref: /qemu/include/hw/vfio/vfio-device.h (revision d441e05e26033eb0e49c4185293424a480ef750f)
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;
1305ee3dc7aSYi Liu     int devid;
1315ee3dc7aSYi Liu     IOMMUFDBackend *iommufd;
132e2c7d025SEric Auger } VFIODevice;
133e2c7d025SEric Auger 
134e2c7d025SEric Auger struct VFIODeviceOps {
135e2c7d025SEric Auger     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
136e2c7d025SEric Auger     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
137e2c7d025SEric Auger     void (*vfio_eoi)(VFIODevice *vdev);
138e93b733bSKirti Wankhede     Object *(*vfio_get_object)(VFIODevice *vdev);
1393783f814SCédric Le Goater 
1403783f814SCédric Le Goater     /**
1413783f814SCédric Le Goater      * @vfio_save_config
1423783f814SCédric Le Goater      *
1433783f814SCédric Le Goater      * Save device config state
1443783f814SCédric Le Goater      *
1453783f814SCédric Le Goater      * @vdev: #VFIODevice for which to save the config
1463783f814SCédric Le Goater      * @f: #QEMUFile where to send the data
1473783f814SCédric Le Goater      * @errp: pointer to Error*, to store an error if it happens.
1483783f814SCédric Le Goater      *
1493783f814SCédric Le Goater      * Returns zero to indicate success and negative for error
1503783f814SCédric Le Goater      */
1513783f814SCédric Le Goater     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
1523783f814SCédric Le Goater 
1533783f814SCédric Le Goater     /**
1543783f814SCédric Le Goater      * @vfio_load_config
1553783f814SCédric Le Goater      *
1563783f814SCédric Le Goater      * Load device config state
1573783f814SCédric Le Goater      *
1583783f814SCédric Le Goater      * @vdev: #VFIODevice for which to load the config
1593783f814SCédric Le Goater      * @f: #QEMUFile where to get the data
1603783f814SCédric Le Goater      *
1613783f814SCédric Le Goater      * Returns zero to indicate success and negative for error
1623783f814SCédric Le Goater      */
163c5e2fb3cSKirti Wankhede     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
164e2c7d025SEric Auger };
165e2c7d025SEric Auger 
166e2c7d025SEric Auger typedef struct VFIOGroup {
167e2c7d025SEric Auger     int fd;
168e2c7d025SEric Auger     int groupid;
169e2c7d025SEric Auger     VFIOContainer *container;
170e2c7d025SEric Auger     QLIST_HEAD(, VFIODevice) device_list;
171e2c7d025SEric Auger     QLIST_ENTRY(VFIOGroup) next;
172e2c7d025SEric Auger     QLIST_ENTRY(VFIOGroup) container_next;
173aff92b82SDavid Hildenbrand     bool ram_block_discard_allowed;
174e2c7d025SEric Auger } VFIOGroup;
175e2c7d025SEric Auger 
1760533739eSZhenzhong Duan #define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio"
1779005f928SZhenzhong Duan #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \
1789005f928SZhenzhong Duan             TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
1790533739eSZhenzhong Duan 
1808b818e05SGerd Hoffmann typedef struct VFIODMABuf {
181c0fcd633SDongwon Kim     QemuDmaBuf *buf;
1828b818e05SGerd Hoffmann     uint32_t pos_x, pos_y, pos_updates;
1838b818e05SGerd Hoffmann     uint32_t hot_x, hot_y, hot_updates;
1848b818e05SGerd Hoffmann     int dmabuf_id;
1858b818e05SGerd Hoffmann     QTAILQ_ENTRY(VFIODMABuf) next;
1868b818e05SGerd Hoffmann } VFIODMABuf;
1878b818e05SGerd Hoffmann 
18800195ba7SGerd Hoffmann typedef struct VFIODisplay {
18900195ba7SGerd Hoffmann     QemuConsole *con;
190b290659fSGerd Hoffmann     RAMFBState *ramfb;
19108479114SGerd Hoffmann     struct vfio_region_info *edid_info;
19208479114SGerd Hoffmann     struct vfio_region_gfx_edid *edid_regs;
19308479114SGerd Hoffmann     uint8_t *edid_blob;
1948781c701SGerd Hoffmann     QEMUTimer *edid_link_timer;
19500195ba7SGerd Hoffmann     struct {
19600195ba7SGerd Hoffmann         VFIORegion buffer;
19700195ba7SGerd Hoffmann         DisplaySurface *surface;
19800195ba7SGerd Hoffmann     } region;
1998b818e05SGerd Hoffmann     struct {
2008b818e05SGerd Hoffmann         QTAILQ_HEAD(, VFIODMABuf) bufs;
2018b818e05SGerd Hoffmann         VFIODMABuf *primary;
2028b818e05SGerd Hoffmann         VFIODMABuf *cursor;
2038b818e05SGerd Hoffmann     } dmabuf;
20400195ba7SGerd Hoffmann } VFIODisplay;
20500195ba7SGerd Hoffmann 
2067e63b311SYi Liu VFIOAddressSpace *vfio_get_address_space(AddressSpace *as);
2077e63b311SYi Liu void vfio_put_address_space(VFIOAddressSpace *space);
2087e63b311SYi Liu 
209770c3b6eSZhenzhong Duan /* SPAPR specific */
210770c3b6eSZhenzhong Duan int vfio_spapr_container_init(VFIOContainer *container, Error **errp);
211770c3b6eSZhenzhong Duan void vfio_spapr_container_deinit(VFIOContainer *container);
2127e63b311SYi Liu 
213e2c7d025SEric Auger void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
214e2c7d025SEric Auger void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
215e2c7d025SEric Auger void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
21684e37d02SZhenzhong Duan bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
217201a7331SEric Auger                             int action, int fd, Error **errp);
218e2c7d025SEric Auger void vfio_region_write(void *opaque, hwaddr addr,
219e2c7d025SEric Auger                            uint64_t data, unsigned size);
220e2c7d025SEric Auger uint64_t vfio_region_read(void *opaque,
221e2c7d025SEric Auger                           hwaddr addr, unsigned size);
222db0da029SAlex Williamson int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
223db0da029SAlex Williamson                       int index, const char *name);
224db0da029SAlex Williamson int vfio_region_mmap(VFIORegion *region);
225db0da029SAlex Williamson void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
2260f7a903bSKirti Wankhede void vfio_region_unmap(VFIORegion *region);
227db0da029SAlex Williamson void vfio_region_exit(VFIORegion *region);
228db0da029SAlex Williamson void vfio_region_finalize(VFIORegion *region);
229e2c7d025SEric Auger void vfio_reset_handler(void *opaque);
230634f38f0SAlex Williamson struct vfio_device_info *vfio_get_device_info(int fd);
231b7754835SZhenzhong Duan bool vfio_attach_device(char *name, VFIODevice *vbasedev,
2325456b186SEric Auger                         AddressSpace *as, Error **errp);
2335456b186SEric Auger void vfio_detach_device(VFIODevice *vbasedev);
234e2c7d025SEric Auger 
2355621c02dSZhenzhong Duan int vfio_kvm_device_add_fd(int fd, Error **errp);
2365621c02dSZhenzhong Duan int vfio_kvm_device_del_fd(int fd, Error **errp);
2375621c02dSZhenzhong Duan 
238f38f5dd1SZhenzhong Duan bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp);
239d9fa4223SSteve Sistare void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer);
240d9fa4223SSteve Sistare 
241e2c7d025SEric Auger extern const MemoryRegionOps vfio_region_ops;
242f481ee2dSPaolo Bonzini typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
2433d779abaSZhenzhong Duan typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
244f481ee2dSPaolo Bonzini extern VFIOGroupList vfio_group_list;
2457e63b311SYi Liu extern VFIODeviceList vfio_device_list;
2467e63b311SYi Liu extern const MemoryListener vfio_memory_listener;
2477e63b311SYi Liu extern int vfio_kvm_device_fd;
248e2c7d025SEric Auger 
2493710586cSKirti Wankhede bool vfio_mig_active(void);
2508bbcb64aSAvihai Horon int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp);
25129d81b71SAvihai Horon void vfio_unblock_multiple_devices_migration(void);
2523c26c80aSZhenzhong Duan bool vfio_viommu_preset(VFIODevice *vbasedev);
2533710586cSKirti Wankhede int64_t vfio_mig_bytes_transferred(void);
254808642a2SAvihai Horon void vfio_reset_bytes_transferred(void);
2553d4d0f0eSJoao Martins bool vfio_device_state_is_running(VFIODevice *vbasedev);
2563d4d0f0eSJoao Martins bool vfio_device_state_is_precopy(VFIODevice *vbasedev);
2573710586cSKirti Wankhede 
25846900226SAlex Williamson #ifdef CONFIG_LINUX
25946900226SAlex Williamson int vfio_get_region_info(VFIODevice *vbasedev, int index,
26046900226SAlex Williamson                          struct vfio_region_info **info);
261e61a424fSAlex Williamson int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
262e61a424fSAlex Williamson                              uint32_t subtype, struct vfio_region_info **info);
263ae0215b2SAlexey Kardashevskiy bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
264013002f0SAlexey Kardashevskiy struct vfio_info_cap_header *
265013002f0SAlexey Kardashevskiy vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id);
2667486a628SMatthew Rosato bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,
2677486a628SMatthew Rosato                              unsigned int *avail);
26892fe289aSMatthew Rosato struct vfio_info_cap_header *
26992fe289aSMatthew Rosato vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id);
2701e09f52fSYi Liu struct vfio_info_cap_header *
2711e09f52fSYi Liu vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id);
27246900226SAlex Williamson #endif
273318f67ceSAlexey Kardashevskiy 
274d4a2af74SZhenzhong Duan bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
2758249cffcSAlex Williamson void vfio_migration_exit(VFIODevice *vbasedev);
276a9e271ecSKirti Wankhede 
2771e09f52fSYi Liu int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size);
2784517c33cSZhenzhong Duan bool
2794517c33cSZhenzhong Duan vfio_devices_all_running_and_mig_active(const VFIOContainerBase *bcontainer);
2804517c33cSZhenzhong Duan bool
2814517c33cSZhenzhong Duan vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer);
2824517c33cSZhenzhong Duan int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
2832da5f9e4SCédric Le Goater                 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
2844517c33cSZhenzhong Duan int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
2852da5f9e4SCédric Le Goater                           uint64_t size, ram_addr_t ram_addr, Error **errp);
286da3e04b2SZhenzhong Duan 
287da3e04b2SZhenzhong Duan /* Returns 0 on success, or a negative errno. */
288c6c6cf91SZhenzhong Duan bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
289da3e04b2SZhenzhong Duan void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
2906106a329SZhenzhong Duan void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
2916106a329SZhenzhong Duan                       DeviceState *dev, bool ram_discard);
292d441e05eSZhenzhong Duan int vfio_device_get_aw_bits(VFIODevice *vdev);
293175de524SMarkus Armbruster #endif /* HW_VFIO_VFIO_COMMON_H */
294