xref: /qemu/include/hw/vfio/vfio-common.h (revision 2ce68e4c)
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 enum {
39     VFIO_DEVICE_TYPE_PCI = 0,
40     VFIO_DEVICE_TYPE_PLATFORM = 1,
41 };
42 
43 typedef struct VFIORegion {
44     struct VFIODevice *vbasedev;
45     off_t fd_offset; /* offset of region within device fd */
46     MemoryRegion mem; /* slow, read/write access */
47     MemoryRegion mmap_mem; /* direct mapped access */
48     void *mmap;
49     size_t size;
50     uint32_t flags; /* VFIO region flags (rd/wr/mmap) */
51     uint8_t nr; /* cache the region number for debug */
52 } VFIORegion;
53 
54 typedef struct VFIOAddressSpace {
55     AddressSpace *as;
56     QLIST_HEAD(, VFIOContainer) containers;
57     QLIST_ENTRY(VFIOAddressSpace) list;
58 } VFIOAddressSpace;
59 
60 struct VFIOGroup;
61 
62 typedef struct VFIOContainer {
63     VFIOAddressSpace *space;
64     int fd; /* /dev/vfio/vfio, empowered by the attached groups */
65     MemoryListener listener;
66     int error;
67     bool initialized;
68     /*
69      * This assumes the host IOMMU can support only a single
70      * contiguous IOVA window.  We may need to generalize that in
71      * future
72      */
73     hwaddr min_iova, max_iova;
74     uint64_t iova_pgsizes;
75     QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
76     QLIST_HEAD(, VFIOGroup) group_list;
77     QLIST_ENTRY(VFIOContainer) next;
78 } VFIOContainer;
79 
80 typedef struct VFIOGuestIOMMU {
81     VFIOContainer *container;
82     MemoryRegion *iommu;
83     Notifier n;
84     QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
85 } VFIOGuestIOMMU;
86 
87 typedef struct VFIODeviceOps VFIODeviceOps;
88 
89 typedef struct VFIODevice {
90     QLIST_ENTRY(VFIODevice) next;
91     struct VFIOGroup *group;
92     char *name;
93     int fd;
94     int type;
95     bool reset_works;
96     bool needs_reset;
97     bool no_mmap;
98     VFIODeviceOps *ops;
99     unsigned int num_irqs;
100     unsigned int num_regions;
101     unsigned int flags;
102 } VFIODevice;
103 
104 struct VFIODeviceOps {
105     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
106     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
107     void (*vfio_eoi)(VFIODevice *vdev);
108 };
109 
110 typedef struct VFIOGroup {
111     int fd;
112     int groupid;
113     VFIOContainer *container;
114     QLIST_HEAD(, VFIODevice) device_list;
115     QLIST_ENTRY(VFIOGroup) next;
116     QLIST_ENTRY(VFIOGroup) container_next;
117 } VFIOGroup;
118 
119 void vfio_put_base_device(VFIODevice *vbasedev);
120 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
121 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
122 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
123 void vfio_region_write(void *opaque, hwaddr addr,
124                            uint64_t data, unsigned size);
125 uint64_t vfio_region_read(void *opaque,
126                           hwaddr addr, unsigned size);
127 int vfio_mmap_region(Object *vdev, VFIORegion *region,
128                      MemoryRegion *mem, MemoryRegion *submem,
129                      void **map, size_t size, off_t offset,
130                      const char *name);
131 void vfio_reset_handler(void *opaque);
132 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as);
133 void vfio_put_group(VFIOGroup *group);
134 int vfio_get_device(VFIOGroup *group, const char *name,
135                     VFIODevice *vbasedev);
136 
137 extern const MemoryRegionOps vfio_region_ops;
138 extern QLIST_HEAD(vfio_group_head, VFIOGroup) vfio_group_list;
139 extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace) vfio_address_spaces;
140 
141 #endif /* !HW_VFIO_VFIO_COMMON_H */
142