xref: /openbmc/qemu/include/hw/vfio/vfio-common.h (revision 77a8257e)
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 
29 /*#define DEBUG_VFIO*/
30 #ifdef DEBUG_VFIO
31 #define DPRINTF(fmt, ...) \
32     do { fprintf(stderr, "vfio: " fmt, ## __VA_ARGS__); } while (0)
33 #else
34 #define DPRINTF(fmt, ...) \
35     do { } while (0)
36 #endif
37 
38 /* Extra debugging, trap acceleration paths for more logging */
39 #define VFIO_ALLOW_KVM_INTX 1
40 #define VFIO_ALLOW_KVM_MSI 1
41 #define VFIO_ALLOW_KVM_MSIX 1
42 
43 enum {
44     VFIO_DEVICE_TYPE_PCI = 0,
45 };
46 
47 typedef struct VFIORegion {
48     struct VFIODevice *vbasedev;
49     off_t fd_offset; /* offset of region within device fd */
50     MemoryRegion mem; /* slow, read/write access */
51     MemoryRegion mmap_mem; /* direct mapped access */
52     void *mmap;
53     size_t size;
54     uint32_t flags; /* VFIO region flags (rd/wr/mmap) */
55     uint8_t nr; /* cache the region number for debug */
56 } VFIORegion;
57 
58 typedef struct VFIOAddressSpace {
59     AddressSpace *as;
60     QLIST_HEAD(, VFIOContainer) containers;
61     QLIST_ENTRY(VFIOAddressSpace) list;
62 } VFIOAddressSpace;
63 
64 struct VFIOGroup;
65 
66 typedef struct VFIOType1 {
67     MemoryListener listener;
68     int error;
69     bool initialized;
70 } VFIOType1;
71 
72 typedef struct VFIOContainer {
73     VFIOAddressSpace *space;
74     int fd; /* /dev/vfio/vfio, empowered by the attached groups */
75     struct {
76         /* enable abstraction to support various iommu backends */
77         union {
78             VFIOType1 type1;
79         };
80         void (*release)(struct VFIOContainer *);
81     } iommu_data;
82     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
83     QLIST_HEAD(, VFIOGroup) group_list;
84     QLIST_ENTRY(VFIOContainer) next;
85 } VFIOContainer;
86 
87 typedef struct VFIOGuestIOMMU {
88     VFIOContainer *container;
89     MemoryRegion *iommu;
90     Notifier n;
91     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
92 } VFIOGuestIOMMU;
93 
94 typedef struct VFIODeviceOps VFIODeviceOps;
95 
96 typedef struct VFIODevice {
97     QLIST_ENTRY(VFIODevice) next;
98     struct VFIOGroup *group;
99     char *name;
100     int fd;
101     int type;
102     bool reset_works;
103     bool needs_reset;
104     bool allow_mmap;
105     VFIODeviceOps *ops;
106     unsigned int num_irqs;
107     unsigned int num_regions;
108     unsigned int flags;
109 } VFIODevice;
110 
111 struct VFIODeviceOps {
112     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
113     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
114     void (*vfio_eoi)(VFIODevice *vdev);
115 };
116 
117 typedef struct VFIOGroup {
118     int fd;
119     int groupid;
120     VFIOContainer *container;
121     QLIST_HEAD(, VFIODevice) device_list;
122     QLIST_ENTRY(VFIOGroup) next;
123     QLIST_ENTRY(VFIOGroup) container_next;
124 } VFIOGroup;
125 
126 void vfio_put_base_device(VFIODevice *vbasedev);
127 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
128 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
129 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
130 void vfio_region_write(void *opaque, hwaddr addr,
131                            uint64_t data, unsigned size);
132 uint64_t vfio_region_read(void *opaque,
133                           hwaddr addr, unsigned size);
134 int vfio_mmap_region(Object *vdev, VFIORegion *region,
135                      MemoryRegion *mem, MemoryRegion *submem,
136                      void **map, size_t size, off_t offset,
137                      const char *name);
138 void vfio_reset_handler(void *opaque);
139 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as);
140 void vfio_put_group(VFIOGroup *group);
141 int vfio_get_device(VFIOGroup *group, const char *name,
142                     VFIODevice *vbasedev);
143 
144 extern const MemoryRegionOps vfio_region_ops;
145 extern QLIST_HEAD(vfio_group_head, VFIOGroup) vfio_group_list;
146 extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace) vfio_address_spaces;
147 
148 #endif /* !HW_VFIO_VFIO_COMMON_H */
149