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/address-spaces.h"
26 #include "exec/memory.h"
27 #include "qemu/queue.h"
28 #include "qemu/notify.h"
30 #include <linux/vfio.h>
33 #define ERR_PREFIX "vfio error: %s: "
34 #define WARN_PREFIX "vfio warning: %s: "
36 /*#define DEBUG_VFIO*/
38 #define DPRINTF(fmt, ...) \
39 do { fprintf(stderr, "vfio: " fmt, ## __VA_ARGS__); } while (0)
41 #define DPRINTF(fmt, ...) \
46 VFIO_DEVICE_TYPE_PCI = 0,
47 VFIO_DEVICE_TYPE_PLATFORM = 1,
48 VFIO_DEVICE_TYPE_CCW = 2,
51 typedef struct VFIOMmap {
58 typedef struct VFIORegion {
59 struct VFIODevice *vbasedev;
60 off_t fd_offset; /* offset of region within device fd */
61 MemoryRegion *mem; /* slow, read/write access */
63 uint32_t flags; /* VFIO region flags (rd/wr/mmap) */
66 uint8_t nr; /* cache the region number for debug */
69 typedef struct VFIOAddressSpace {
71 QLIST_HEAD(, VFIOContainer) containers;
72 QLIST_ENTRY(VFIOAddressSpace) list;
77 typedef struct VFIOContainer {
78 VFIOAddressSpace *space;
79 int fd; /* /dev/vfio/vfio, empowered by the attached groups */
80 MemoryListener listener;
81 MemoryListener prereg_listener;
86 * This assumes the host IOMMU can support only a single
87 * contiguous IOVA window. We may need to generalize that in
90 QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
91 QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
92 QLIST_HEAD(, VFIOGroup) group_list;
93 QLIST_ENTRY(VFIOContainer) next;
96 typedef struct VFIOGuestIOMMU {
97 VFIOContainer *container;
101 QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
104 typedef struct VFIOHostDMAWindow {
107 uint64_t iova_pgsizes;
108 QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next;
111 typedef struct VFIODeviceOps VFIODeviceOps;
113 typedef struct VFIODevice {
114 QLIST_ENTRY(VFIODevice) next;
115 struct VFIOGroup *group;
124 unsigned int num_irqs;
125 unsigned int num_regions;
129 struct VFIODeviceOps {
130 void (*vfio_compute_needs_reset)(VFIODevice *vdev);
131 int (*vfio_hot_reset_multi)(VFIODevice *vdev);
132 void (*vfio_eoi)(VFIODevice *vdev);
135 typedef struct VFIOGroup {
138 VFIOContainer *container;
139 QLIST_HEAD(, VFIODevice) device_list;
140 QLIST_ENTRY(VFIOGroup) next;
141 QLIST_ENTRY(VFIOGroup) container_next;
144 void vfio_put_base_device(VFIODevice *vbasedev);
145 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
146 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
147 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
148 void vfio_region_write(void *opaque, hwaddr addr,
149 uint64_t data, unsigned size);
150 uint64_t vfio_region_read(void *opaque,
151 hwaddr addr, unsigned size);
152 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
153 int index, const char *name);
154 int vfio_region_mmap(VFIORegion *region);
155 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
156 void vfio_region_exit(VFIORegion *region);
157 void vfio_region_finalize(VFIORegion *region);
158 void vfio_reset_handler(void *opaque);
159 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);
160 void vfio_put_group(VFIOGroup *group);
161 int vfio_get_device(VFIOGroup *group, const char *name,
162 VFIODevice *vbasedev, Error **errp);
164 extern const MemoryRegionOps vfio_region_ops;
165 extern QLIST_HEAD(vfio_group_head, VFIOGroup) vfio_group_list;
166 extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace) vfio_address_spaces;
169 int vfio_get_region_info(VFIODevice *vbasedev, int index,
170 struct vfio_region_info **info);
171 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
172 uint32_t subtype, struct vfio_region_info **info);
174 extern const MemoryListener vfio_prereg_listener;
176 int vfio_spapr_create_window(VFIOContainer *container,
177 MemoryRegionSection *section,
179 int vfio_spapr_remove_window(VFIOContainer *container,
180 hwaddr offset_within_address_space);
182 #endif /* HW_VFIO_VFIO_COMMON_H */