xref: /openbmc/qemu/include/hw/vfio/vfio-common.h (revision ae3c12a0)
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/memory.h"
26 #include "qemu/queue.h"
27 #include "qemu/notify.h"
28 #include "ui/console.h"
29 #include "hw/display/ramfb.h"
30 #ifdef CONFIG_LINUX
31 #include <linux/vfio.h>
32 #endif
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 VFIOAddressSpace {
62     AddressSpace *as;
63     QLIST_HEAD(, VFIOContainer) containers;
64     QLIST_ENTRY(VFIOAddressSpace) list;
65 } VFIOAddressSpace;
66 
67 struct VFIOGroup;
68 
69 typedef struct VFIOContainer {
70     VFIOAddressSpace *space;
71     int fd; /* /dev/vfio/vfio, empowered by the attached groups */
72     MemoryListener listener;
73     MemoryListener prereg_listener;
74     unsigned iommu_type;
75     int error;
76     bool initialized;
77     unsigned long pgsizes;
78     /*
79      * This assumes the host IOMMU can support only a single
80      * contiguous IOVA window.  We may need to generalize that in
81      * future
82      */
83     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
84     QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
85     QLIST_HEAD(, VFIOGroup) group_list;
86     QLIST_ENTRY(VFIOContainer) next;
87 } VFIOContainer;
88 
89 typedef struct VFIOGuestIOMMU {
90     VFIOContainer *container;
91     IOMMUMemoryRegion *iommu;
92     hwaddr iommu_offset;
93     IOMMUNotifier n;
94     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
95 } VFIOGuestIOMMU;
96 
97 typedef struct VFIOHostDMAWindow {
98     hwaddr min_iova;
99     hwaddr max_iova;
100     uint64_t iova_pgsizes;
101     QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next;
102 } VFIOHostDMAWindow;
103 
104 typedef struct VFIODeviceOps VFIODeviceOps;
105 
106 typedef struct VFIODevice {
107     QLIST_ENTRY(VFIODevice) next;
108     struct VFIOGroup *group;
109     char *sysfsdev;
110     char *name;
111     DeviceState *dev;
112     int fd;
113     int type;
114     bool reset_works;
115     bool needs_reset;
116     bool no_mmap;
117     bool balloon_allowed;
118     VFIODeviceOps *ops;
119     unsigned int num_irqs;
120     unsigned int num_regions;
121     unsigned int flags;
122 } VFIODevice;
123 
124 struct VFIODeviceOps {
125     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
126     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
127     void (*vfio_eoi)(VFIODevice *vdev);
128 };
129 
130 typedef struct VFIOGroup {
131     int fd;
132     int groupid;
133     VFIOContainer *container;
134     QLIST_HEAD(, VFIODevice) device_list;
135     QLIST_ENTRY(VFIOGroup) next;
136     QLIST_ENTRY(VFIOGroup) container_next;
137     bool balloon_allowed;
138 } VFIOGroup;
139 
140 typedef struct VFIODMABuf {
141     QemuDmaBuf buf;
142     uint32_t pos_x, pos_y, pos_updates;
143     uint32_t hot_x, hot_y, hot_updates;
144     int dmabuf_id;
145     QTAILQ_ENTRY(VFIODMABuf) next;
146 } VFIODMABuf;
147 
148 typedef struct VFIODisplay {
149     QemuConsole *con;
150     RAMFBState *ramfb;
151     struct vfio_region_info *edid_info;
152     struct vfio_region_gfx_edid *edid_regs;
153     uint8_t *edid_blob;
154     QEMUTimer *edid_link_timer;
155     struct {
156         VFIORegion buffer;
157         DisplaySurface *surface;
158     } region;
159     struct {
160         QTAILQ_HEAD(, VFIODMABuf) bufs;
161         VFIODMABuf *primary;
162         VFIODMABuf *cursor;
163     } dmabuf;
164 } VFIODisplay;
165 
166 void vfio_put_base_device(VFIODevice *vbasedev);
167 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
168 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
169 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
170 void vfio_region_write(void *opaque, hwaddr addr,
171                            uint64_t data, unsigned size);
172 uint64_t vfio_region_read(void *opaque,
173                           hwaddr addr, unsigned size);
174 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
175                       int index, const char *name);
176 int vfio_region_mmap(VFIORegion *region);
177 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
178 void vfio_region_exit(VFIORegion *region);
179 void vfio_region_finalize(VFIORegion *region);
180 void vfio_reset_handler(void *opaque);
181 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);
182 void vfio_put_group(VFIOGroup *group);
183 int vfio_get_device(VFIOGroup *group, const char *name,
184                     VFIODevice *vbasedev, Error **errp);
185 
186 extern const MemoryRegionOps vfio_region_ops;
187 typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
188 extern VFIOGroupList vfio_group_list;
189 
190 #ifdef CONFIG_LINUX
191 int vfio_get_region_info(VFIODevice *vbasedev, int index,
192                          struct vfio_region_info **info);
193 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
194                              uint32_t subtype, struct vfio_region_info **info);
195 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
196 struct vfio_info_cap_header *
197 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id);
198 #endif
199 extern const MemoryListener vfio_prereg_listener;
200 
201 int vfio_spapr_create_window(VFIOContainer *container,
202                              MemoryRegionSection *section,
203                              hwaddr *pgsize);
204 int vfio_spapr_remove_window(VFIOContainer *container,
205                              hwaddr offset_within_address_space);
206 
207 #endif /* HW_VFIO_VFIO_COMMON_H */
208