xref: /qemu/include/hw/vfio/vfio-device.h (revision 005b8d10450d2d41e9c1bcf8da4085f23cb85b76)
1 /*
2  * common header for vfio based device assignment support
3  *
4  * Copyright Red Hat, Inc. 2012
5  *
6  * Authors:
7  *  Alex Williamson <alex.williamson@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  * Based on qemu-kvm device-assignment:
13  *  Adapted for KVM by Qumranet.
14  *  Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
15  *  Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
16  *  Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
17  *  Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
18  *  Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
19  */
20 
21 #ifndef HW_VFIO_VFIO_COMMON_H
22 #define HW_VFIO_VFIO_COMMON_H
23 
24 #include "system/memory.h"
25 #include "qemu/queue.h"
26 #ifdef CONFIG_LINUX
27 #include <linux/vfio.h>
28 #endif
29 #include "system/system.h"
30 #include "hw/vfio/vfio-container-base.h"
31 #include "system/host_iommu_device.h"
32 #include "system/iommufd.h"
33 
34 #define VFIO_MSG_PREFIX "vfio %s: "
35 
36 enum {
37     VFIO_DEVICE_TYPE_PCI = 0,
38     VFIO_DEVICE_TYPE_PLATFORM = 1,
39     VFIO_DEVICE_TYPE_CCW = 2,
40     VFIO_DEVICE_TYPE_AP = 3,
41 };
42 
43 typedef struct VFIODeviceOps VFIODeviceOps;
44 typedef struct VFIOMigration VFIOMigration;
45 
46 typedef struct IOMMUFDBackend IOMMUFDBackend;
47 typedef struct VFIOIOASHwpt VFIOIOASHwpt;
48 
49 typedef struct VFIODevice {
50     QLIST_ENTRY(VFIODevice) next;
51     QLIST_ENTRY(VFIODevice) container_next;
52     QLIST_ENTRY(VFIODevice) global_next;
53     struct VFIOGroup *group;
54     VFIOContainerBase *bcontainer;
55     char *sysfsdev;
56     char *name;
57     DeviceState *dev;
58     int fd;
59     int type;
60     bool mdev;
61     bool reset_works;
62     bool needs_reset;
63     bool no_mmap;
64     bool ram_block_discard_allowed;
65     OnOffAuto enable_migration;
66     OnOffAuto migration_multifd_transfer;
67     bool migration_events;
68     VFIODeviceOps *ops;
69     unsigned int num_irqs;
70     unsigned int num_regions;
71     unsigned int flags;
72     VFIOMigration *migration;
73     Error *migration_blocker;
74     OnOffAuto pre_copy_dirty_page_tracking;
75     OnOffAuto device_dirty_page_tracking;
76     bool dirty_pages_supported;
77     bool dirty_tracking; /* Protected by BQL */
78     bool iommu_dirty_tracking;
79     HostIOMMUDevice *hiod;
80     int devid;
81     IOMMUFDBackend *iommufd;
82     VFIOIOASHwpt *hwpt;
83     QLIST_ENTRY(VFIODevice) hwpt_next;
84 } VFIODevice;
85 
86 struct VFIODeviceOps {
87     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
88     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
89     void (*vfio_eoi)(VFIODevice *vdev);
90     Object *(*vfio_get_object)(VFIODevice *vdev);
91 
92     /**
93      * @vfio_save_config
94      *
95      * Save device config state
96      *
97      * @vdev: #VFIODevice for which to save the config
98      * @f: #QEMUFile where to send the data
99      * @errp: pointer to Error*, to store an error if it happens.
100      *
101      * Returns zero to indicate success and negative for error
102      */
103     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
104 
105     /**
106      * @vfio_load_config
107      *
108      * Load device config state
109      *
110      * @vdev: #VFIODevice for which to load the config
111      * @f: #QEMUFile where to get the data
112      *
113      * Returns zero to indicate success and negative for error
114      */
115     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
116 };
117 
118 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
119 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
120 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
121 bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
122                             int action, int fd, Error **errp);
123 
124 void vfio_reset_handler(void *opaque);
125 bool vfio_device_is_mdev(VFIODevice *vbasedev);
126 bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp);
127 bool vfio_attach_device(char *name, VFIODevice *vbasedev,
128                         AddressSpace *as, Error **errp);
129 void vfio_detach_device(VFIODevice *vbasedev);
130 VFIODevice *vfio_get_vfio_device(Object *obj);
131 
132 bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp);
133 void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer);
134 
135 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
136 extern VFIODeviceList vfio_device_list;
137 extern const MemoryListener vfio_memory_listener;
138 extern int vfio_kvm_device_fd;
139 
140 #ifdef CONFIG_LINUX
141 int vfio_get_region_info(VFIODevice *vbasedev, int index,
142                          struct vfio_region_info **info);
143 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
144                              uint32_t subtype, struct vfio_region_info **info);
145 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
146 #endif
147 
148 bool vfio_devices_all_dirty_tracking_started(
149     const VFIOContainerBase *bcontainer);
150 bool
151 vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer);
152 int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
153                 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
154 int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
155                           uint64_t size, ram_addr_t ram_addr, Error **errp);
156 
157 /* Returns 0 on success, or a negative errno. */
158 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
159 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
160 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
161                       DeviceState *dev, bool ram_discard);
162 int vfio_device_get_aw_bits(VFIODevice *vdev);
163 #endif /* HW_VFIO_VFIO_COMMON_H */
164