xref: /openbmc/qemu/include/hw/vfio/vfio-common.h (revision 4a09d0bb)
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/address-spaces.h"
26 #include "exec/memory.h"
27 #include "qemu/queue.h"
28 #include "qemu/notify.h"
29 #ifdef CONFIG_LINUX
30 #include <linux/vfio.h>
31 #endif
32 
33 #define ERR_PREFIX "vfio error: %s: "
34 #define WARN_PREFIX "vfio warning: %s: "
35 
36 /*#define DEBUG_VFIO*/
37 #ifdef DEBUG_VFIO
38 #define DPRINTF(fmt, ...) \
39     do { fprintf(stderr, "vfio: " fmt, ## __VA_ARGS__); } while (0)
40 #else
41 #define DPRINTF(fmt, ...) \
42     do { } while (0)
43 #endif
44 
45 enum {
46     VFIO_DEVICE_TYPE_PCI = 0,
47     VFIO_DEVICE_TYPE_PLATFORM = 1,
48 };
49 
50 typedef struct VFIOMmap {
51     MemoryRegion mem;
52     void *mmap;
53     off_t offset;
54     size_t size;
55 } VFIOMmap;
56 
57 typedef struct VFIORegion {
58     struct VFIODevice *vbasedev;
59     off_t fd_offset; /* offset of region within device fd */
60     MemoryRegion *mem; /* slow, read/write access */
61     size_t size;
62     uint32_t flags; /* VFIO region flags (rd/wr/mmap) */
63     uint32_t nr_mmaps;
64     VFIOMmap *mmaps;
65     uint8_t nr; /* cache the region number for debug */
66 } VFIORegion;
67 
68 typedef struct VFIOAddressSpace {
69     AddressSpace *as;
70     QLIST_HEAD(, VFIOContainer) containers;
71     QLIST_ENTRY(VFIOAddressSpace) list;
72 } VFIOAddressSpace;
73 
74 struct VFIOGroup;
75 
76 typedef struct VFIOContainer {
77     VFIOAddressSpace *space;
78     int fd; /* /dev/vfio/vfio, empowered by the attached groups */
79     MemoryListener listener;
80     MemoryListener prereg_listener;
81     unsigned iommu_type;
82     int error;
83     bool initialized;
84     /*
85      * This assumes the host IOMMU can support only a single
86      * contiguous IOVA window.  We may need to generalize that in
87      * future
88      */
89     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
90     QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
91     QLIST_HEAD(, VFIOGroup) group_list;
92     QLIST_ENTRY(VFIOContainer) next;
93 } VFIOContainer;
94 
95 typedef struct VFIOGuestIOMMU {
96     VFIOContainer *container;
97     MemoryRegion *iommu;
98     hwaddr iommu_offset;
99     IOMMUNotifier n;
100     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
101 } VFIOGuestIOMMU;
102 
103 typedef struct VFIOHostDMAWindow {
104     hwaddr min_iova;
105     hwaddr max_iova;
106     uint64_t iova_pgsizes;
107     QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next;
108 } VFIOHostDMAWindow;
109 
110 typedef struct VFIODeviceOps VFIODeviceOps;
111 
112 typedef struct VFIODevice {
113     QLIST_ENTRY(VFIODevice) next;
114     struct VFIOGroup *group;
115     char *sysfsdev;
116     char *name;
117     int fd;
118     int type;
119     bool reset_works;
120     bool needs_reset;
121     bool no_mmap;
122     VFIODeviceOps *ops;
123     unsigned int num_irqs;
124     unsigned int num_regions;
125     unsigned int flags;
126 } VFIODevice;
127 
128 struct VFIODeviceOps {
129     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
130     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
131     void (*vfio_eoi)(VFIODevice *vdev);
132 };
133 
134 typedef struct VFIOGroup {
135     int fd;
136     int groupid;
137     VFIOContainer *container;
138     QLIST_HEAD(, VFIODevice) device_list;
139     QLIST_ENTRY(VFIOGroup) next;
140     QLIST_ENTRY(VFIOGroup) container_next;
141 } VFIOGroup;
142 
143 void vfio_put_base_device(VFIODevice *vbasedev);
144 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
145 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
146 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
147 void vfio_region_write(void *opaque, hwaddr addr,
148                            uint64_t data, unsigned size);
149 uint64_t vfio_region_read(void *opaque,
150                           hwaddr addr, unsigned size);
151 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
152                       int index, const char *name);
153 int vfio_region_mmap(VFIORegion *region);
154 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
155 void vfio_region_exit(VFIORegion *region);
156 void vfio_region_finalize(VFIORegion *region);
157 void vfio_reset_handler(void *opaque);
158 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);
159 void vfio_put_group(VFIOGroup *group);
160 int vfio_get_device(VFIOGroup *group, const char *name,
161                     VFIODevice *vbasedev, Error **errp);
162 
163 extern const MemoryRegionOps vfio_region_ops;
164 extern QLIST_HEAD(vfio_group_head, VFIOGroup) vfio_group_list;
165 extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace) vfio_address_spaces;
166 
167 #ifdef CONFIG_LINUX
168 int vfio_get_region_info(VFIODevice *vbasedev, int index,
169                          struct vfio_region_info **info);
170 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
171                              uint32_t subtype, struct vfio_region_info **info);
172 #endif
173 extern const MemoryListener vfio_prereg_listener;
174 
175 int vfio_spapr_create_window(VFIOContainer *container,
176                              MemoryRegionSection *section,
177                              hwaddr *pgsize);
178 int vfio_spapr_remove_window(VFIOContainer *container,
179                              hwaddr offset_within_address_space);
180 
181 #endif /* HW_VFIO_VFIO_COMMON_H */
182