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