1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#ifndef HW_VFIO_VFIO_COMMON_H
21#define HW_VFIO_VFIO_COMMON_H
22
23#include "qemu-common.h"
24#include "exec/address-spaces.h"
25#include "exec/memory.h"
26#include "qemu/queue.h"
27#include "qemu/notify.h"
28#ifdef CONFIG_LINUX
29#include <linux/vfio.h>
30#endif
31
32
33#ifdef DEBUG_VFIO
34#define DPRINTF(fmt, ...) \
35 do { fprintf(stderr, "vfio: " fmt, ## __VA_ARGS__); } while (0)
36#else
37#define DPRINTF(fmt, ...) \
38 do { } while (0)
39#endif
40
41enum {
42 VFIO_DEVICE_TYPE_PCI = 0,
43 VFIO_DEVICE_TYPE_PLATFORM = 1,
44};
45
46typedef struct VFIOMmap {
47 MemoryRegion mem;
48 void *mmap;
49 off_t offset;
50 size_t size;
51} VFIOMmap;
52
53typedef struct VFIORegion {
54 struct VFIODevice *vbasedev;
55 off_t fd_offset;
56 MemoryRegion *mem;
57 size_t size;
58 uint32_t flags;
59 uint32_t nr_mmaps;
60 VFIOMmap *mmaps;
61 uint8_t nr;
62} VFIORegion;
63
64typedef struct VFIOAddressSpace {
65 AddressSpace *as;
66 QLIST_HEAD(, VFIOContainer) containers;
67 QLIST_ENTRY(VFIOAddressSpace) list;
68} VFIOAddressSpace;
69
70struct VFIOGroup;
71
72typedef struct VFIOContainer {
73 VFIOAddressSpace *space;
74 int fd;
75 MemoryListener listener;
76 int error;
77 bool initialized;
78
79
80
81
82
83 hwaddr min_iova, max_iova;
84 uint64_t iova_pgsizes;
85 QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
86 QLIST_HEAD(, VFIOGroup) group_list;
87 QLIST_ENTRY(VFIOContainer) next;
88} VFIOContainer;
89
90typedef struct VFIOGuestIOMMU {
91 VFIOContainer *container;
92 MemoryRegion *iommu;
93 Notifier n;
94 QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
95} VFIOGuestIOMMU;
96
97typedef struct VFIODeviceOps VFIODeviceOps;
98
99typedef struct VFIODevice {
100 QLIST_ENTRY(VFIODevice) next;
101 struct VFIOGroup *group;
102 char *sysfsdev;
103 char *name;
104 int fd;
105 int type;
106 bool reset_works;
107 bool needs_reset;
108 bool no_mmap;
109 VFIODeviceOps *ops;
110 unsigned int num_irqs;
111 unsigned int num_regions;
112 unsigned int flags;
113} VFIODevice;
114
115struct VFIODeviceOps {
116 void (*vfio_compute_needs_reset)(VFIODevice *vdev);
117 int (*vfio_hot_reset_multi)(VFIODevice *vdev);
118 void (*vfio_eoi)(VFIODevice *vdev);
119};
120
121typedef struct VFIOGroup {
122 int fd;
123 int groupid;
124 VFIOContainer *container;
125 QLIST_HEAD(, VFIODevice) device_list;
126 QLIST_ENTRY(VFIOGroup) next;
127 QLIST_ENTRY(VFIOGroup) container_next;
128} VFIOGroup;
129
130void vfio_put_base_device(VFIODevice *vbasedev);
131void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
132void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
133void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
134void vfio_region_write(void *opaque, hwaddr addr,
135 uint64_t data, unsigned size);
136uint64_t vfio_region_read(void *opaque,
137 hwaddr addr, unsigned size);
138int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
139 int index, const char *name);
140int vfio_region_mmap(VFIORegion *region);
141void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
142void vfio_region_exit(VFIORegion *region);
143void vfio_region_finalize(VFIORegion *region);
144void vfio_reset_handler(void *opaque);
145VFIOGroup *vfio_get_group(int groupid, AddressSpace *as);
146void vfio_put_group(VFIOGroup *group);
147int vfio_get_device(VFIOGroup *group, const char *name,
148 VFIODevice *vbasedev);
149
150extern const MemoryRegionOps vfio_region_ops;
151extern QLIST_HEAD(vfio_group_head, VFIOGroup) vfio_group_list;
152extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace) vfio_address_spaces;
153
154#ifdef CONFIG_LINUX
155int vfio_get_region_info(VFIODevice *vbasedev, int index,
156 struct vfio_region_info **info);
157#endif
158#endif
159