xref: /qemu/include/hw/vfio/vfio-device.h (revision 5363a1a117ea6e1af520d1faed303f944975f760)
1 /*
2  * VFIO Device interface
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 "system/memory.h"
25 #include "qemu/queue.h"
26 #ifdef CONFIG_LINUX
27 #include <linux/vfio.h>
28 #endif
29 #include "system/system.h"
30 #include "hw/vfio/vfio-container-base.h"
31 #include "system/host_iommu_device.h"
32 #include "system/iommufd.h"
33 
34 #define VFIO_MSG_PREFIX "vfio %s: "
35 
36 enum {
37     VFIO_DEVICE_TYPE_PCI = 0,
38     VFIO_DEVICE_TYPE_PLATFORM = 1,
39     VFIO_DEVICE_TYPE_CCW = 2,
40     VFIO_DEVICE_TYPE_AP = 3,
41 };
42 
43 typedef struct VFIODeviceOps VFIODeviceOps;
44 typedef struct VFIOMigration VFIOMigration;
45 
46 typedef struct IOMMUFDBackend IOMMUFDBackend;
47 typedef struct VFIOIOASHwpt VFIOIOASHwpt;
48 
49 typedef struct VFIODevice {
50     QLIST_ENTRY(VFIODevice) next;
51     QLIST_ENTRY(VFIODevice) container_next;
52     QLIST_ENTRY(VFIODevice) global_next;
53     struct VFIOGroup *group;
54     VFIOContainerBase *bcontainer;
55     char *sysfsdev;
56     char *name;
57     DeviceState *dev;
58     int fd;
59     int type;
60     bool mdev;
61     bool reset_works;
62     bool needs_reset;
63     bool no_mmap;
64     bool ram_block_discard_allowed;
65     OnOffAuto enable_migration;
66     OnOffAuto migration_multifd_transfer;
67     bool migration_events;
68     VFIODeviceOps *ops;
69     unsigned int num_irqs;
70     unsigned int num_regions;
71     unsigned int flags;
72     VFIOMigration *migration;
73     Error *migration_blocker;
74     OnOffAuto pre_copy_dirty_page_tracking;
75     OnOffAuto device_dirty_page_tracking;
76     bool dirty_pages_supported;
77     bool dirty_tracking; /* Protected by BQL */
78     bool iommu_dirty_tracking;
79     HostIOMMUDevice *hiod;
80     int devid;
81     IOMMUFDBackend *iommufd;
82     VFIOIOASHwpt *hwpt;
83     QLIST_ENTRY(VFIODevice) hwpt_next;
84 } VFIODevice;
85 
86 struct VFIODeviceOps {
87     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
88     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
89     void (*vfio_eoi)(VFIODevice *vdev);
90     Object *(*vfio_get_object)(VFIODevice *vdev);
91 
92     /**
93      * @vfio_save_config
94      *
95      * Save device config state
96      *
97      * @vdev: #VFIODevice for which to save the config
98      * @f: #QEMUFile where to send the data
99      * @errp: pointer to Error*, to store an error if it happens.
100      *
101      * Returns zero to indicate success and negative for error
102      */
103     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
104 
105     /**
106      * @vfio_load_config
107      *
108      * Load device config state
109      *
110      * @vdev: #VFIODevice for which to load the config
111      * @f: #QEMUFile where to get the data
112      *
113      * Returns zero to indicate success and negative for error
114      */
115     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
116 };
117 
118 /*
119  * Given a return value of either a short number of bytes read or -errno,
120  * construct a meaningful error message.
121  */
122 #define strreaderror(ret) \
123     (ret < 0 ? strerror(-ret) : "short read")
124 
125 /*
126  * Given a return value of either a short number of bytes written or -errno,
127  * construct a meaningful error message.
128  */
129 #define strwriteerror(ret) \
130     (ret < 0 ? strerror(-ret) : "short write")
131 
132 void vfio_device_irq_disable(VFIODevice *vbasedev, int index);
133 void vfio_device_irq_unmask(VFIODevice *vbasedev, int index);
134 void vfio_device_irq_mask(VFIODevice *vbasedev, int index);
135 bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex,
136                                    int action, int fd, Error **errp);
137 
138 void vfio_device_reset_handler(void *opaque);
139 bool vfio_device_is_mdev(VFIODevice *vbasedev);
140 bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev,
141                                          const char *typename, Error **errp);
142 bool vfio_device_attach(char *name, VFIODevice *vbasedev,
143                         AddressSpace *as, Error **errp);
144 bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
145                                       VFIODevice *vbasedev, AddressSpace *as,
146                                       Error **errp);
147 void vfio_device_detach(VFIODevice *vbasedev);
148 VFIODevice *vfio_get_vfio_device(Object *obj);
149 
150 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
151 extern VFIODeviceList vfio_device_list;
152 
153 #ifdef CONFIG_LINUX
154 void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
155                          struct vfio_device_info *info);
156 
157 void vfio_device_unprepare(VFIODevice *vbasedev);
158 
159 int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
160                                 struct vfio_region_info **info);
161 int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type,
162                                      uint32_t subtype, struct vfio_region_info **info);
163 bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
164 
165 int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
166                                 struct vfio_irq_info *info);
167 #endif
168 
169 /* Returns 0 on success, or a negative errno. */
170 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
171 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
172 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
173                       DeviceState *dev, bool ram_discard);
174 int vfio_device_get_aw_bits(VFIODevice *vdev);
175 #endif /* HW_VFIO_VFIO_COMMON_H */
176