xref: /openbmc/qemu/include/hw/vfio/vfio-common.h (revision 0dacec87)
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 "qemu-common.h"
25 #include "exec/address-spaces.h"
26 #include "exec/memory.h"
27 #include "qemu/queue.h"
28 #include "qemu/notify.h"
29 #include "ui/console.h"
30 #ifdef CONFIG_LINUX
31 #include <linux/vfio.h>
32 #endif
33 
34 #define ERR_PREFIX "vfio error: %s: "
35 #define WARN_PREFIX "vfio warning: %s: "
36 
37 /*#define DEBUG_VFIO*/
38 #ifdef DEBUG_VFIO
39 #define DPRINTF(fmt, ...) \
40     do { fprintf(stderr, "vfio: " fmt, ## __VA_ARGS__); } while (0)
41 #else
42 #define DPRINTF(fmt, ...) \
43     do { } while (0)
44 #endif
45 
46 enum {
47     VFIO_DEVICE_TYPE_PCI = 0,
48     VFIO_DEVICE_TYPE_PLATFORM = 1,
49     VFIO_DEVICE_TYPE_CCW = 2,
50 };
51 
52 typedef struct VFIOMmap {
53     MemoryRegion mem;
54     void *mmap;
55     off_t offset;
56     size_t size;
57 } VFIOMmap;
58 
59 typedef struct VFIORegion {
60     struct VFIODevice *vbasedev;
61     off_t fd_offset; /* offset of region within device fd */
62     MemoryRegion *mem; /* slow, read/write access */
63     size_t size;
64     uint32_t flags; /* VFIO region flags (rd/wr/mmap) */
65     uint32_t nr_mmaps;
66     VFIOMmap *mmaps;
67     uint8_t nr; /* cache the region number for debug */
68 } VFIORegion;
69 
70 typedef struct VFIOAddressSpace {
71     AddressSpace *as;
72     QLIST_HEAD(, VFIOContainer) containers;
73     QLIST_ENTRY(VFIOAddressSpace) list;
74 } VFIOAddressSpace;
75 
76 struct VFIOGroup;
77 
78 typedef struct VFIOContainer {
79     VFIOAddressSpace *space;
80     int fd; /* /dev/vfio/vfio, empowered by the attached groups */
81     MemoryListener listener;
82     MemoryListener prereg_listener;
83     unsigned iommu_type;
84     int error;
85     bool initialized;
86     /*
87      * This assumes the host IOMMU can support only a single
88      * contiguous IOVA window.  We may need to generalize that in
89      * future
90      */
91     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
92     QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
93     QLIST_HEAD(, VFIOGroup) group_list;
94     QLIST_ENTRY(VFIOContainer) next;
95 } VFIOContainer;
96 
97 typedef struct VFIOGuestIOMMU {
98     VFIOContainer *container;
99     IOMMUMemoryRegion *iommu;
100     hwaddr iommu_offset;
101     IOMMUNotifier n;
102     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
103 } VFIOGuestIOMMU;
104 
105 typedef struct VFIOHostDMAWindow {
106     hwaddr min_iova;
107     hwaddr max_iova;
108     uint64_t iova_pgsizes;
109     QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next;
110 } VFIOHostDMAWindow;
111 
112 typedef struct VFIODeviceOps VFIODeviceOps;
113 
114 typedef struct VFIODevice {
115     QLIST_ENTRY(VFIODevice) next;
116     struct VFIOGroup *group;
117     char *sysfsdev;
118     char *name;
119     DeviceState *dev;
120     int fd;
121     int type;
122     bool reset_works;
123     bool needs_reset;
124     bool no_mmap;
125     VFIODeviceOps *ops;
126     unsigned int num_irqs;
127     unsigned int num_regions;
128     unsigned int flags;
129 } VFIODevice;
130 
131 struct VFIODeviceOps {
132     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
133     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
134     void (*vfio_eoi)(VFIODevice *vdev);
135 };
136 
137 typedef struct VFIOGroup {
138     int fd;
139     int groupid;
140     VFIOContainer *container;
141     QLIST_HEAD(, VFIODevice) device_list;
142     QLIST_ENTRY(VFIOGroup) next;
143     QLIST_ENTRY(VFIOGroup) container_next;
144 } VFIOGroup;
145 
146 typedef struct VFIODMABuf {
147     QemuDmaBuf buf;
148     uint32_t pos_x, pos_y, pos_updates;
149     uint32_t hot_x, hot_y, hot_updates;
150     int dmabuf_id;
151     QTAILQ_ENTRY(VFIODMABuf) next;
152 } VFIODMABuf;
153 
154 typedef struct VFIODisplay {
155     QemuConsole *con;
156     struct {
157         VFIORegion buffer;
158         DisplaySurface *surface;
159     } region;
160     struct {
161         QTAILQ_HEAD(, VFIODMABuf) bufs;
162         VFIODMABuf *primary;
163         VFIODMABuf *cursor;
164     } dmabuf;
165 } VFIODisplay;
166 
167 void vfio_put_base_device(VFIODevice *vbasedev);
168 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
169 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
170 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
171 void vfio_region_write(void *opaque, hwaddr addr,
172                            uint64_t data, unsigned size);
173 uint64_t vfio_region_read(void *opaque,
174                           hwaddr addr, unsigned size);
175 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
176                       int index, const char *name);
177 int vfio_region_mmap(VFIORegion *region);
178 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
179 void vfio_region_exit(VFIORegion *region);
180 void vfio_region_finalize(VFIORegion *region);
181 void vfio_reset_handler(void *opaque);
182 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);
183 void vfio_put_group(VFIOGroup *group);
184 int vfio_get_device(VFIOGroup *group, const char *name,
185                     VFIODevice *vbasedev, Error **errp);
186 
187 extern const MemoryRegionOps vfio_region_ops;
188 extern QLIST_HEAD(vfio_group_head, VFIOGroup) vfio_group_list;
189 extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace) vfio_address_spaces;
190 
191 #ifdef CONFIG_LINUX
192 int vfio_get_region_info(VFIODevice *vbasedev, int index,
193                          struct vfio_region_info **info);
194 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
195                              uint32_t subtype, struct vfio_region_info **info);
196 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
197 #endif
198 extern const MemoryListener vfio_prereg_listener;
199 
200 int vfio_spapr_create_window(VFIOContainer *container,
201                              MemoryRegionSection *section,
202                              hwaddr *pgsize);
203 int vfio_spapr_remove_window(VFIOContainer *container,
204                              hwaddr offset_within_address_space);
205 
206 #endif /* HW_VFIO_VFIO_COMMON_H */
207