xref: /qemu/include/hw/vfio/vfio-device.h (revision 11b8b9d53d8b30bf1823e9d2e303c28fe13acf34)
1e2c7d025SEric Auger /*
2*11b8b9d5SCédric Le Goater  * VFIO Device interface
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 
248be545baSRichard Henderson #include "system/memory.h"
25e2c7d025SEric Auger #include "qemu/queue.h"
2646900226SAlex Williamson #ifdef CONFIG_LINUX
2746900226SAlex Williamson #include <linux/vfio.h>
2846900226SAlex Williamson #endif
2932cad1ffSPhilippe Mathieu-Daudé #include "system/system.h"
30f61dddd7SZhenzhong Duan #include "hw/vfio/vfio-container-base.h"
3132cad1ffSPhilippe Mathieu-Daudé #include "system/host_iommu_device.h"
3232cad1ffSPhilippe Mathieu-Daudé #include "system/iommufd.h"
33e2c7d025SEric Auger 
34e1eb292aSMarkus Armbruster #define VFIO_MSG_PREFIX "vfio %s: "
35426ec904SEric Auger 
36e2c7d025SEric Auger enum {
37e2c7d025SEric Auger     VFIO_DEVICE_TYPE_PCI = 0,
380ea2730bSEric Auger     VFIO_DEVICE_TYPE_PLATFORM = 1,
391dcac3e1SXiao Feng Ren     VFIO_DEVICE_TYPE_CCW = 2,
402fe2942cSTony Krowiak     VFIO_DEVICE_TYPE_AP = 3,
41e2c7d025SEric Auger };
42504d297eSCédric Le Goater 
43e2c7d025SEric Auger typedef struct VFIODeviceOps VFIODeviceOps;
44b553d2c4SCédric Le Goater typedef struct VFIOMigration VFIOMigration;
45e2c7d025SEric Auger 
46d4a8f286SCédric Le Goater typedef struct IOMMUFDBackend IOMMUFDBackend;
47d4a8f286SCédric Le Goater typedef struct VFIOIOASHwpt VFIOIOASHwpt;
48d4a8f286SCédric Le Goater 
49e2c7d025SEric Auger typedef struct VFIODevice {
50e2c7d025SEric Auger     QLIST_ENTRY(VFIODevice) next;
517103ef7eSZhenzhong Duan     QLIST_ENTRY(VFIODevice) container_next;
523d779abaSZhenzhong Duan     QLIST_ENTRY(VFIODevice) global_next;
53e2c7d025SEric Auger     struct VFIOGroup *group;
543e6015d1SZhenzhong Duan     VFIOContainerBase *bcontainer;
557df9381bSAlex Williamson     char *sysfsdev;
56e2c7d025SEric Auger     char *name;
577da624e2SAlex Williamson     DeviceState *dev;
58e2c7d025SEric Auger     int fd;
59e2c7d025SEric Auger     int type;
6013e522f6SJoao Martins     bool mdev;
61e2c7d025SEric Auger     bool reset_works;
62e2c7d025SEric Auger     bool needs_reset;
635e15d79bSAlex Williamson     bool no_mmap;
64aff92b82SDavid Hildenbrand     bool ram_block_discard_allowed;
658bbcb64aSAvihai Horon     OnOffAuto enable_migration;
66623af41dSMaciej S. Szmigiero     OnOffAuto migration_multifd_transfer;
675e1f8905SAvihai Horon     bool migration_events;
68e2c7d025SEric Auger     VFIODeviceOps *ops;
69e2c7d025SEric Auger     unsigned int num_irqs;
70e2c7d025SEric Auger     unsigned int num_regions;
71e2c7d025SEric Auger     unsigned int flags;
72a9e271ecSKirti Wankhede     VFIOMigration *migration;
73a9e271ecSKirti Wankhede     Error *migration_blocker;
74bb0990d1SKirti Wankhede     OnOffAuto pre_copy_dirty_page_tracking;
7530b91677SJoao Martins     OnOffAuto device_dirty_page_tracking;
765255bbf4SJoao Martins     bool dirty_pages_supported;
770ae05e08SAvihai Horon     bool dirty_tracking; /* Protected by BQL */
78dddfd8d6SJoao Martins     bool iommu_dirty_tracking;
79a7fd91b8SZhenzhong Duan     HostIOMMUDevice *hiod;
805ee3dc7aSYi Liu     int devid;
815ee3dc7aSYi Liu     IOMMUFDBackend *iommufd;
825b1e96e6SJoao Martins     VFIOIOASHwpt *hwpt;
835b1e96e6SJoao Martins     QLIST_ENTRY(VFIODevice) hwpt_next;
84e2c7d025SEric Auger } VFIODevice;
85e2c7d025SEric Auger 
86e2c7d025SEric Auger struct VFIODeviceOps {
87e2c7d025SEric Auger     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
88e2c7d025SEric Auger     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
89e2c7d025SEric Auger     void (*vfio_eoi)(VFIODevice *vdev);
90e93b733bSKirti Wankhede     Object *(*vfio_get_object)(VFIODevice *vdev);
913783f814SCédric Le Goater 
923783f814SCédric Le Goater     /**
933783f814SCédric Le Goater      * @vfio_save_config
943783f814SCédric Le Goater      *
953783f814SCédric Le Goater      * Save device config state
963783f814SCédric Le Goater      *
973783f814SCédric Le Goater      * @vdev: #VFIODevice for which to save the config
983783f814SCédric Le Goater      * @f: #QEMUFile where to send the data
993783f814SCédric Le Goater      * @errp: pointer to Error*, to store an error if it happens.
1003783f814SCédric Le Goater      *
1013783f814SCédric Le Goater      * Returns zero to indicate success and negative for error
1023783f814SCédric Le Goater      */
1033783f814SCédric Le Goater     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
1043783f814SCédric Le Goater 
1053783f814SCédric Le Goater     /**
1063783f814SCédric Le Goater      * @vfio_load_config
1073783f814SCédric Le Goater      *
1083783f814SCédric Le Goater      * Load device config state
1093783f814SCédric Le Goater      *
1103783f814SCédric Le Goater      * @vdev: #VFIODevice for which to load the config
1113783f814SCédric Le Goater      * @f: #QEMUFile where to get the data
1123783f814SCédric Le Goater      *
1133783f814SCédric Le Goater      * Returns zero to indicate success and negative for error
1143783f814SCédric Le Goater      */
115c5e2fb3cSKirti Wankhede     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
116e2c7d025SEric Auger };
117e2c7d025SEric Auger 
118e2c7d025SEric Auger void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
119e2c7d025SEric Auger void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
120e2c7d025SEric Auger void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
12184e37d02SZhenzhong Duan bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
122201a7331SEric Auger                             int action, int fd, Error **errp);
123499e53ccSCédric Le Goater 
124e2c7d025SEric Auger void vfio_reset_handler(void *opaque);
12513e522f6SJoao Martins bool vfio_device_is_mdev(VFIODevice *vbasedev);
12683a4d596SJoao Martins bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp);
127b7754835SZhenzhong Duan bool vfio_attach_device(char *name, VFIODevice *vbasedev,
1285456b186SEric Auger                         AddressSpace *as, Error **errp);
1295456b186SEric Auger void vfio_detach_device(VFIODevice *vbasedev);
13080936cf7SCédric Le Goater VFIODevice *vfio_get_vfio_device(Object *obj);
131e2c7d025SEric Auger 
1323d779abaSZhenzhong Duan typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
1337e63b311SYi Liu extern VFIODeviceList vfio_device_list;
134e2c7d025SEric Auger 
13546900226SAlex Williamson #ifdef CONFIG_LINUX
13646900226SAlex Williamson int vfio_get_region_info(VFIODevice *vbasedev, int index,
13746900226SAlex Williamson                          struct vfio_region_info **info);
138e61a424fSAlex Williamson int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
139e61a424fSAlex Williamson                              uint32_t subtype, struct vfio_region_info **info);
140ae0215b2SAlexey Kardashevskiy bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
14146900226SAlex Williamson #endif
142318f67ceSAlexey Kardashevskiy 
143da3e04b2SZhenzhong Duan /* Returns 0 on success, or a negative errno. */
144c6c6cf91SZhenzhong Duan bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
145da3e04b2SZhenzhong Duan void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
1466106a329SZhenzhong Duan void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
1476106a329SZhenzhong Duan                       DeviceState *dev, bool ram_discard);
148d441e05eSZhenzhong Duan int vfio_device_get_aw_bits(VFIODevice *vdev);
149175de524SMarkus Armbruster #endif /* HW_VFIO_VFIO_COMMON_H */
150