xref: /qemu/include/hw/vfio/vfio-common.h (revision 52ea63de)
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     int error;
77     bool initialized;
78     /*
79      * This assumes the host IOMMU can support only a single
80      * contiguous IOVA window.  We may need to generalize that in
81      * future
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 
90 typedef struct VFIOGuestIOMMU {
91     VFIOContainer *container;
92     MemoryRegion *iommu;
93     hwaddr iommu_offset;
94     Notifier n;
95     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
96 } VFIOGuestIOMMU;
97 
98 typedef struct VFIODeviceOps VFIODeviceOps;
99 
100 typedef struct VFIODevice {
101     QLIST_ENTRY(VFIODevice) next;
102     struct VFIOGroup *group;
103     char *sysfsdev;
104     char *name;
105     int fd;
106     int type;
107     bool reset_works;
108     bool needs_reset;
109     bool no_mmap;
110     VFIODeviceOps *ops;
111     unsigned int num_irqs;
112     unsigned int num_regions;
113     unsigned int flags;
114 } VFIODevice;
115 
116 struct VFIODeviceOps {
117     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
118     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
119     void (*vfio_eoi)(VFIODevice *vdev);
120 };
121 
122 typedef struct VFIOGroup {
123     int fd;
124     int groupid;
125     VFIOContainer *container;
126     QLIST_HEAD(, VFIODevice) device_list;
127     QLIST_ENTRY(VFIOGroup) next;
128     QLIST_ENTRY(VFIOGroup) container_next;
129 } VFIOGroup;
130 
131 void vfio_put_base_device(VFIODevice *vbasedev);
132 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
133 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
134 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
135 void vfio_region_write(void *opaque, hwaddr addr,
136                            uint64_t data, unsigned size);
137 uint64_t vfio_region_read(void *opaque,
138                           hwaddr addr, unsigned size);
139 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
140                       int index, const char *name);
141 int vfio_region_mmap(VFIORegion *region);
142 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
143 void vfio_region_exit(VFIORegion *region);
144 void vfio_region_finalize(VFIORegion *region);
145 void vfio_reset_handler(void *opaque);
146 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as);
147 void vfio_put_group(VFIOGroup *group);
148 int vfio_get_device(VFIOGroup *group, const char *name,
149                     VFIODevice *vbasedev);
150 
151 extern const MemoryRegionOps vfio_region_ops;
152 extern QLIST_HEAD(vfio_group_head, VFIOGroup) vfio_group_list;
153 extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace) vfio_address_spaces;
154 
155 #ifdef CONFIG_LINUX
156 int vfio_get_region_info(VFIODevice *vbasedev, int index,
157                          struct vfio_region_info **info);
158 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
159                              uint32_t subtype, struct vfio_region_info **info);
160 #endif
161 #endif /* !HW_VFIO_VFIO_COMMON_H */
162