2 * common header for vfio based device assignment support
4 * Copyright Red Hat, Inc. 2012
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Based on qemu-kvm device-assignment:
13 * Adapted for KVM by Qumranet.
21 #ifndef HW_VFIO_VFIO_COMMON_H
22 #define HW_VFIO_VFIO_COMMON_H
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"
30 #include <linux/vfio.h>
33 #define ERR_PREFIX "vfio error: %s: "
34 #define WARN_PREFIX "vfio warning: %s: "
37 VFIO_DEVICE_TYPE_PCI = 0,
38 VFIO_DEVICE_TYPE_PLATFORM = 1,
39 VFIO_DEVICE_TYPE_CCW = 2,
42 typedef struct VFIOMmap {
49 typedef struct VFIORegion {
50 struct VFIODevice *vbasedev;
51 off_t fd_offset; /* offset of region within device fd */
52 MemoryRegion *mem; /* slow, read/write access */
54 uint32_t flags; /* VFIO region flags (rd/wr/mmap) */
57 uint8_t nr; /* cache the region number for debug */
60 typedef struct VFIOAddressSpace {
62 QLIST_HEAD(, VFIOContainer) containers;
63 QLIST_ENTRY(VFIOAddressSpace) list;
68 typedef struct VFIOContainer {
69 VFIOAddressSpace *space;
70 int fd; /* /dev/vfio/vfio, empowered by the attached groups */
71 MemoryListener listener;
72 MemoryListener prereg_listener;
77 * This assumes the host IOMMU can support only a single
78 * contiguous IOVA window. We may need to generalize that in
81 QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
82 QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
83 QLIST_HEAD(, VFIOGroup) group_list;
84 QLIST_ENTRY(VFIOContainer) next;
87 typedef struct VFIOGuestIOMMU {
88 VFIOContainer *container;
89 IOMMUMemoryRegion *iommu;
92 QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
95 typedef struct VFIOHostDMAWindow {
98 uint64_t iova_pgsizes;
99 QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next;
102 typedef struct VFIODeviceOps VFIODeviceOps;
104 typedef struct VFIODevice {
105 QLIST_ENTRY(VFIODevice) next;
106 struct VFIOGroup *group;
115 bool balloon_allowed;
117 unsigned int num_irqs;
118 unsigned int num_regions;
122 struct VFIODeviceOps {
123 void (*vfio_compute_needs_reset)(VFIODevice *vdev);
124 int (*vfio_hot_reset_multi)(VFIODevice *vdev);
125 void (*vfio_eoi)(VFIODevice *vdev);
128 typedef struct VFIOGroup {
131 VFIOContainer *container;
132 QLIST_HEAD(, VFIODevice) device_list;
133 QLIST_ENTRY(VFIOGroup) next;
134 QLIST_ENTRY(VFIOGroup) container_next;
135 bool balloon_allowed;
138 typedef struct VFIODMABuf {
140 uint32_t pos_x, pos_y, pos_updates;
141 uint32_t hot_x, hot_y, hot_updates;
143 QTAILQ_ENTRY(VFIODMABuf) next;
146 typedef struct VFIODisplay {
150 DisplaySurface *surface;
153 QTAILQ_HEAD(, VFIODMABuf) bufs;
159 void vfio_put_base_device(VFIODevice *vbasedev);
160 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
161 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
162 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
163 void vfio_region_write(void *opaque, hwaddr addr,
164 uint64_t data, unsigned size);
165 uint64_t vfio_region_read(void *opaque,
166 hwaddr addr, unsigned size);
167 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
168 int index, const char *name);
169 int vfio_region_mmap(VFIORegion *region);
170 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
171 void vfio_region_exit(VFIORegion *region);
172 void vfio_region_finalize(VFIORegion *region);
173 void vfio_reset_handler(void *opaque);
174 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);
175 void vfio_put_group(VFIOGroup *group);
176 int vfio_get_device(VFIOGroup *group, const char *name,
177 VFIODevice *vbasedev, Error **errp);
179 extern const MemoryRegionOps vfio_region_ops;
180 extern QLIST_HEAD(vfio_group_head, VFIOGroup) vfio_group_list;
181 extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace) vfio_address_spaces;
184 int vfio_get_region_info(VFIODevice *vbasedev, int index,
185 struct vfio_region_info **info);
186 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
187 uint32_t subtype, struct vfio_region_info **info);
188 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
190 extern const MemoryListener vfio_prereg_listener;
192 int vfio_spapr_create_window(VFIOContainer *container,
193 MemoryRegionSection *section,
195 int vfio_spapr_remove_window(VFIOContainer *container,
196 hwaddr offset_within_address_space);
198 #endif /* HW_VFIO_VFIO_COMMON_H */