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