xref: /qemu/include/hw/vfio/vfio-common.h (revision 85aad98a)
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 #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 /*#define DEBUG_VFIO*/
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 
41 enum {
42     VFIO_DEVICE_TYPE_PCI = 0,
43     VFIO_DEVICE_TYPE_PLATFORM = 1,
44 };
45 
46 typedef struct VFIOMmap {
47     MemoryRegion mem;
48     void *mmap;
49     off_t offset;
50     size_t size;
51 } VFIOMmap;
52 
53 typedef struct VFIORegion {
54     struct VFIODevice *vbasedev;
55     off_t fd_offset; /* offset of region within device fd */
56     MemoryRegion *mem; /* slow, read/write access */
57     size_t size;
58     uint32_t flags; /* VFIO region flags (rd/wr/mmap) */
59     uint32_t nr_mmaps;
60     VFIOMmap *mmaps;
61     uint8_t nr; /* cache the region number for debug */
62 } VFIORegion;
63 
64 typedef struct VFIOAddressSpace {
65     AddressSpace *as;
66     QLIST_HEAD(, VFIOContainer) containers;
67     QLIST_ENTRY(VFIOAddressSpace) list;
68 } VFIOAddressSpace;
69 
70 struct VFIOGroup;
71 
72 typedef struct VFIOContainer {
73     VFIOAddressSpace *space;
74     int fd; /* /dev/vfio/vfio, empowered by the attached groups */
75     MemoryListener listener;
76     MemoryListener prereg_listener;
77     unsigned iommu_type;
78     int error;
79     bool initialized;
80     /*
81      * This assumes the host IOMMU can support only a single
82      * contiguous IOVA window.  We may need to generalize that in
83      * future
84      */
85     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
86     QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
87     QLIST_HEAD(, VFIOGroup) group_list;
88     QLIST_ENTRY(VFIOContainer) next;
89 } VFIOContainer;
90 
91 typedef struct VFIOGuestIOMMU {
92     VFIOContainer *container;
93     MemoryRegion *iommu;
94     hwaddr iommu_offset;
95     Notifier n;
96     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
97 } VFIOGuestIOMMU;
98 
99 typedef struct VFIOHostDMAWindow {
100     hwaddr min_iova;
101     hwaddr max_iova;
102     uint64_t iova_pgsizes;
103     QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next;
104 } VFIOHostDMAWindow;
105 
106 typedef struct VFIODeviceOps VFIODeviceOps;
107 
108 typedef struct VFIODevice {
109     QLIST_ENTRY(VFIODevice) next;
110     struct VFIOGroup *group;
111     char *sysfsdev;
112     char *name;
113     int fd;
114     int type;
115     bool reset_works;
116     bool needs_reset;
117     bool no_mmap;
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 } VFIOGroup;
138 
139 void vfio_put_base_device(VFIODevice *vbasedev);
140 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
141 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
142 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
143 void vfio_region_write(void *opaque, hwaddr addr,
144                            uint64_t data, unsigned size);
145 uint64_t vfio_region_read(void *opaque,
146                           hwaddr addr, unsigned size);
147 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
148                       int index, const char *name);
149 int vfio_region_mmap(VFIORegion *region);
150 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
151 void vfio_region_exit(VFIORegion *region);
152 void vfio_region_finalize(VFIORegion *region);
153 void vfio_reset_handler(void *opaque);
154 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as);
155 void vfio_put_group(VFIOGroup *group);
156 int vfio_get_device(VFIOGroup *group, const char *name,
157                     VFIODevice *vbasedev);
158 
159 extern const MemoryRegionOps vfio_region_ops;
160 extern QLIST_HEAD(vfio_group_head, VFIOGroup) vfio_group_list;
161 extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace) vfio_address_spaces;
162 
163 #ifdef CONFIG_LINUX
164 int vfio_get_region_info(VFIODevice *vbasedev, int index,
165                          struct vfio_region_info **info);
166 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
167                              uint32_t subtype, struct vfio_region_info **info);
168 #endif
169 extern const MemoryListener vfio_prereg_listener;
170 
171 int vfio_spapr_create_window(VFIOContainer *container,
172                              MemoryRegionSection *section,
173                              hwaddr *pgsize);
174 int vfio_spapr_remove_window(VFIOContainer *container,
175                              hwaddr offset_within_address_space);
176 
177 #endif /* !HW_VFIO_VFIO_COMMON_H */
178