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