xref: /openbmc/qemu/include/hw/vfio/vfio-common.h (revision cc37d98b)
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 
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 typedef struct VFIOMigration {
62     struct VFIODevice *vbasedev;
63     VMChangeStateEntry *vm_state;
64     Notifier migration_state;
65     uint32_t device_state;
66     int data_fd;
67     void *data_buffer;
68     size_t data_buffer_size;
69 } VFIOMigration;
70 
71 typedef struct VFIOAddressSpace {
72     AddressSpace *as;
73     QLIST_HEAD(, VFIOContainer) containers;
74     QLIST_ENTRY(VFIOAddressSpace) list;
75 } VFIOAddressSpace;
76 
77 struct VFIOGroup;
78 
79 typedef struct VFIOContainer {
80     VFIOAddressSpace *space;
81     int fd; /* /dev/vfio/vfio, empowered by the attached groups */
82     MemoryListener listener;
83     MemoryListener prereg_listener;
84     unsigned iommu_type;
85     Error *error;
86     bool initialized;
87     bool dirty_pages_supported;
88     uint64_t dirty_pgsizes;
89     uint64_t max_dirty_bitmap_size;
90     unsigned long pgsizes;
91     unsigned int dma_max_mappings;
92     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
93     QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
94     QLIST_HEAD(, VFIOGroup) group_list;
95     QLIST_HEAD(, VFIORamDiscardListener) vrdl_list;
96     QLIST_ENTRY(VFIOContainer) next;
97 } VFIOContainer;
98 
99 typedef struct VFIOGuestIOMMU {
100     VFIOContainer *container;
101     IOMMUMemoryRegion *iommu_mr;
102     hwaddr iommu_offset;
103     IOMMUNotifier n;
104     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
105 } VFIOGuestIOMMU;
106 
107 typedef struct VFIORamDiscardListener {
108     VFIOContainer *container;
109     MemoryRegion *mr;
110     hwaddr offset_within_address_space;
111     hwaddr size;
112     uint64_t granularity;
113     RamDiscardListener listener;
114     QLIST_ENTRY(VFIORamDiscardListener) next;
115 } VFIORamDiscardListener;
116 
117 typedef struct VFIOHostDMAWindow {
118     hwaddr min_iova;
119     hwaddr max_iova;
120     uint64_t iova_pgsizes;
121     QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next;
122 } VFIOHostDMAWindow;
123 
124 typedef struct VFIODeviceOps VFIODeviceOps;
125 
126 typedef struct VFIODevice {
127     QLIST_ENTRY(VFIODevice) next;
128     struct VFIOGroup *group;
129     char *sysfsdev;
130     char *name;
131     DeviceState *dev;
132     int fd;
133     int type;
134     bool reset_works;
135     bool needs_reset;
136     bool no_mmap;
137     bool ram_block_discard_allowed;
138     bool enable_migration;
139     VFIODeviceOps *ops;
140     unsigned int num_irqs;
141     unsigned int num_regions;
142     unsigned int flags;
143     VFIOMigration *migration;
144     Error *migration_blocker;
145     OnOffAuto pre_copy_dirty_page_tracking;
146     bool dirty_pages_supported;
147     bool dirty_tracking;
148 } VFIODevice;
149 
150 struct VFIODeviceOps {
151     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
152     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
153     void (*vfio_eoi)(VFIODevice *vdev);
154     Object *(*vfio_get_object)(VFIODevice *vdev);
155     void (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f);
156     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
157 };
158 
159 typedef struct VFIOGroup {
160     int fd;
161     int groupid;
162     VFIOContainer *container;
163     QLIST_HEAD(, VFIODevice) device_list;
164     QLIST_ENTRY(VFIOGroup) next;
165     QLIST_ENTRY(VFIOGroup) container_next;
166     bool ram_block_discard_allowed;
167 } VFIOGroup;
168 
169 typedef struct VFIODMABuf {
170     QemuDmaBuf buf;
171     uint32_t pos_x, pos_y, pos_updates;
172     uint32_t hot_x, hot_y, hot_updates;
173     int dmabuf_id;
174     QTAILQ_ENTRY(VFIODMABuf) next;
175 } VFIODMABuf;
176 
177 typedef struct VFIODisplay {
178     QemuConsole *con;
179     RAMFBState *ramfb;
180     struct vfio_region_info *edid_info;
181     struct vfio_region_gfx_edid *edid_regs;
182     uint8_t *edid_blob;
183     QEMUTimer *edid_link_timer;
184     struct {
185         VFIORegion buffer;
186         DisplaySurface *surface;
187     } region;
188     struct {
189         QTAILQ_HEAD(, VFIODMABuf) bufs;
190         VFIODMABuf *primary;
191         VFIODMABuf *cursor;
192     } dmabuf;
193 } VFIODisplay;
194 
195 void vfio_put_base_device(VFIODevice *vbasedev);
196 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
197 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
198 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
199 int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
200                            int action, int fd, Error **errp);
201 void vfio_region_write(void *opaque, hwaddr addr,
202                            uint64_t data, unsigned size);
203 uint64_t vfio_region_read(void *opaque,
204                           hwaddr addr, unsigned size);
205 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
206                       int index, const char *name);
207 int vfio_region_mmap(VFIORegion *region);
208 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
209 void vfio_region_unmap(VFIORegion *region);
210 void vfio_region_exit(VFIORegion *region);
211 void vfio_region_finalize(VFIORegion *region);
212 void vfio_reset_handler(void *opaque);
213 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);
214 void vfio_put_group(VFIOGroup *group);
215 int vfio_get_device(VFIOGroup *group, const char *name,
216                     VFIODevice *vbasedev, Error **errp);
217 
218 extern const MemoryRegionOps vfio_region_ops;
219 typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
220 extern VFIOGroupList vfio_group_list;
221 
222 bool vfio_mig_active(void);
223 int vfio_block_multiple_devices_migration(Error **errp);
224 void vfio_unblock_multiple_devices_migration(void);
225 int vfio_block_giommu_migration(Error **errp);
226 int64_t vfio_mig_bytes_transferred(void);
227 
228 #ifdef CONFIG_LINUX
229 int vfio_get_region_info(VFIODevice *vbasedev, int index,
230                          struct vfio_region_info **info);
231 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
232                              uint32_t subtype, struct vfio_region_info **info);
233 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
234 struct vfio_info_cap_header *
235 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id);
236 bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,
237                              unsigned int *avail);
238 struct vfio_info_cap_header *
239 vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id);
240 #endif
241 extern const MemoryListener vfio_prereg_listener;
242 
243 int vfio_spapr_create_window(VFIOContainer *container,
244                              MemoryRegionSection *section,
245                              hwaddr *pgsize);
246 int vfio_spapr_remove_window(VFIOContainer *container,
247                              hwaddr offset_within_address_space);
248 
249 int vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
250 void vfio_migration_exit(VFIODevice *vbasedev);
251 void vfio_migration_finalize(void);
252 
253 #endif /* HW_VFIO_VFIO_COMMON_H */
254