xref: /qemu/include/hw/vfio/vfio-device.h (revision 5a339dd8c1e34e13ae7f64477e20decdf381aae7)
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 VFIOMmap {
44     MemoryRegion mem;
45     void *mmap;
46     off_t offset;
47     size_t size;
48 } VFIOMmap;
49 
50 typedef struct VFIORegion {
51     struct VFIODevice *vbasedev;
52     off_t fd_offset; /* offset of region within device fd */
53     MemoryRegion *mem; /* slow, read/write access */
54     size_t size;
55     uint32_t flags; /* VFIO region flags (rd/wr/mmap) */
56     uint32_t nr_mmaps;
57     VFIOMmap *mmaps;
58     uint8_t nr; /* cache the region number for debug */
59 } VFIORegion;
60 
61 struct VFIOGroup;
62 
63 typedef struct VFIOContainer {
64     VFIOContainerBase bcontainer;
65     int fd; /* /dev/vfio/vfio, empowered by the attached groups */
66     unsigned iommu_type;
67     QLIST_HEAD(, VFIOGroup) group_list;
68 } VFIOContainer;
69 
70 OBJECT_DECLARE_SIMPLE_TYPE(VFIOContainer, VFIO_IOMMU_LEGACY);
71 
72 typedef struct IOMMUFDBackend IOMMUFDBackend;
73 
74 typedef struct VFIOIOASHwpt {
75     uint32_t hwpt_id;
76     uint32_t hwpt_flags;
77     QLIST_HEAD(, VFIODevice) device_list;
78     QLIST_ENTRY(VFIOIOASHwpt) next;
79 } VFIOIOASHwpt;
80 
81 typedef struct VFIOIOMMUFDContainer {
82     VFIOContainerBase bcontainer;
83     IOMMUFDBackend *be;
84     uint32_t ioas_id;
85     QLIST_HEAD(, VFIOIOASHwpt) hwpt_list;
86 } VFIOIOMMUFDContainer;
87 
88 OBJECT_DECLARE_SIMPLE_TYPE(VFIOIOMMUFDContainer, VFIO_IOMMU_IOMMUFD);
89 
90 typedef struct VFIODeviceOps VFIODeviceOps;
91 typedef struct VFIOMigration VFIOMigration;
92 
93 typedef struct VFIODevice {
94     QLIST_ENTRY(VFIODevice) next;
95     QLIST_ENTRY(VFIODevice) container_next;
96     QLIST_ENTRY(VFIODevice) global_next;
97     struct VFIOGroup *group;
98     VFIOContainerBase *bcontainer;
99     char *sysfsdev;
100     char *name;
101     DeviceState *dev;
102     int fd;
103     int type;
104     bool mdev;
105     bool reset_works;
106     bool needs_reset;
107     bool no_mmap;
108     bool ram_block_discard_allowed;
109     OnOffAuto enable_migration;
110     OnOffAuto migration_multifd_transfer;
111     bool migration_events;
112     VFIODeviceOps *ops;
113     unsigned int num_irqs;
114     unsigned int num_regions;
115     unsigned int flags;
116     VFIOMigration *migration;
117     Error *migration_blocker;
118     OnOffAuto pre_copy_dirty_page_tracking;
119     OnOffAuto device_dirty_page_tracking;
120     bool dirty_pages_supported;
121     bool dirty_tracking; /* Protected by BQL */
122     bool iommu_dirty_tracking;
123     HostIOMMUDevice *hiod;
124     int devid;
125     IOMMUFDBackend *iommufd;
126     VFIOIOASHwpt *hwpt;
127     QLIST_ENTRY(VFIODevice) hwpt_next;
128 } VFIODevice;
129 
130 struct VFIODeviceOps {
131     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
132     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
133     void (*vfio_eoi)(VFIODevice *vdev);
134     Object *(*vfio_get_object)(VFIODevice *vdev);
135 
136     /**
137      * @vfio_save_config
138      *
139      * Save device config state
140      *
141      * @vdev: #VFIODevice for which to save the config
142      * @f: #QEMUFile where to send the data
143      * @errp: pointer to Error*, to store an error if it happens.
144      *
145      * Returns zero to indicate success and negative for error
146      */
147     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
148 
149     /**
150      * @vfio_load_config
151      *
152      * Load device config state
153      *
154      * @vdev: #VFIODevice for which to load the config
155      * @f: #QEMUFile where to get the data
156      *
157      * Returns zero to indicate success and negative for error
158      */
159     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
160 };
161 
162 typedef struct VFIOGroup {
163     int fd;
164     int groupid;
165     VFIOContainer *container;
166     QLIST_HEAD(, VFIODevice) device_list;
167     QLIST_ENTRY(VFIOGroup) next;
168     QLIST_ENTRY(VFIOGroup) container_next;
169     bool ram_block_discard_allowed;
170 } VFIOGroup;
171 
172 #define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio"
173 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \
174             TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio"
175 
176 VFIOAddressSpace *vfio_get_address_space(AddressSpace *as);
177 void vfio_put_address_space(VFIOAddressSpace *space);
178 void vfio_address_space_insert(VFIOAddressSpace *space,
179                                VFIOContainerBase *bcontainer);
180 
181 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
182 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
183 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
184 bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
185                             int action, int fd, Error **errp);
186 void vfio_region_write(void *opaque, hwaddr addr,
187                            uint64_t data, unsigned size);
188 uint64_t vfio_region_read(void *opaque,
189                           hwaddr addr, unsigned size);
190 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
191                       int index, const char *name);
192 int vfio_region_mmap(VFIORegion *region);
193 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
194 void vfio_region_unmap(VFIORegion *region);
195 void vfio_region_exit(VFIORegion *region);
196 void vfio_region_finalize(VFIORegion *region);
197 void vfio_reset_handler(void *opaque);
198 struct vfio_device_info *vfio_get_device_info(int fd);
199 bool vfio_device_is_mdev(VFIODevice *vbasedev);
200 bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp);
201 bool vfio_attach_device(char *name, VFIODevice *vbasedev,
202                         AddressSpace *as, Error **errp);
203 void vfio_detach_device(VFIODevice *vbasedev);
204 VFIODevice *vfio_get_vfio_device(Object *obj);
205 
206 int vfio_kvm_device_add_fd(int fd, Error **errp);
207 int vfio_kvm_device_del_fd(int fd, Error **errp);
208 
209 bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp);
210 void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer);
211 
212 extern const MemoryRegionOps vfio_region_ops;
213 typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
214 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
215 extern VFIOGroupList vfio_group_list;
216 extern VFIODeviceList vfio_device_list;
217 extern const MemoryListener vfio_memory_listener;
218 extern int vfio_kvm_device_fd;
219 
220 #ifdef CONFIG_LINUX
221 int vfio_get_region_info(VFIODevice *vbasedev, int index,
222                          struct vfio_region_info **info);
223 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
224                              uint32_t subtype, struct vfio_region_info **info);
225 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
226 struct vfio_info_cap_header *
227 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id);
228 bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,
229                              unsigned int *avail);
230 struct vfio_info_cap_header *
231 vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id);
232 struct vfio_info_cap_header *
233 vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id);
234 #endif
235 
236 int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size);
237 bool vfio_devices_all_dirty_tracking_started(
238     const VFIOContainerBase *bcontainer);
239 bool
240 vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer);
241 int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
242                 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
243 int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
244                           uint64_t size, ram_addr_t ram_addr, Error **errp);
245 
246 /* Returns 0 on success, or a negative errno. */
247 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
248 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
249 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
250                       DeviceState *dev, bool ram_discard);
251 int vfio_device_get_aw_bits(VFIODevice *vdev);
252 #endif /* HW_VFIO_VFIO_COMMON_H */
253