xref: /qemu/include/hw/vfio/vfio-device.h (revision 38bf025d0dc80c1ae6e8f36093c5145c08b332d9)
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 VFIODeviceIOOps VFIODeviceIOOps;
45 typedef struct VFIOMigration VFIOMigration;
46 
47 typedef struct IOMMUFDBackend IOMMUFDBackend;
48 typedef struct VFIOIOASHwpt VFIOIOASHwpt;
49 
50 typedef struct VFIODevice {
51     QLIST_ENTRY(VFIODevice) next;
52     QLIST_ENTRY(VFIODevice) container_next;
53     QLIST_ENTRY(VFIODevice) global_next;
54     struct VFIOGroup *group;
55     VFIOContainerBase *bcontainer;
56     char *sysfsdev;
57     char *name;
58     DeviceState *dev;
59     int fd;
60     int type;
61     bool mdev;
62     bool reset_works;
63     bool needs_reset;
64     bool no_mmap;
65     bool ram_block_discard_allowed;
66     OnOffAuto enable_migration;
67     OnOffAuto migration_multifd_transfer;
68     bool migration_events;
69     VFIODeviceOps *ops;
70     VFIODeviceIOOps *io_ops;
71     unsigned int num_irqs;
72     unsigned int num_regions;
73     unsigned int flags;
74     VFIOMigration *migration;
75     Error *migration_blocker;
76     OnOffAuto pre_copy_dirty_page_tracking;
77     OnOffAuto device_dirty_page_tracking;
78     bool dirty_pages_supported;
79     bool dirty_tracking; /* Protected by BQL */
80     bool iommu_dirty_tracking;
81     HostIOMMUDevice *hiod;
82     int devid;
83     IOMMUFDBackend *iommufd;
84     VFIOIOASHwpt *hwpt;
85     QLIST_ENTRY(VFIODevice) hwpt_next;
86 } VFIODevice;
87 
88 struct VFIODeviceOps {
89     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
90     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
91     void (*vfio_eoi)(VFIODevice *vdev);
92     Object *(*vfio_get_object)(VFIODevice *vdev);
93 
94     /**
95      * @vfio_save_config
96      *
97      * Save device config state
98      *
99      * @vdev: #VFIODevice for which to save the config
100      * @f: #QEMUFile where to send the data
101      * @errp: pointer to Error*, to store an error if it happens.
102      *
103      * Returns zero to indicate success and negative for error
104      */
105     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
106 
107     /**
108      * @vfio_load_config
109      *
110      * Load device config state
111      *
112      * @vdev: #VFIODevice for which to load the config
113      * @f: #QEMUFile where to get the data
114      *
115      * Returns zero to indicate success and negative for error
116      */
117     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
118 };
119 
120 /*
121  * Given a return value of either a short number of bytes read or -errno,
122  * construct a meaningful error message.
123  */
124 #define strreaderror(ret) \
125     (ret < 0 ? strerror(-ret) : "short read")
126 
127 /*
128  * Given a return value of either a short number of bytes written or -errno,
129  * construct a meaningful error message.
130  */
131 #define strwriteerror(ret) \
132     (ret < 0 ? strerror(-ret) : "short write")
133 
134 void vfio_device_irq_disable(VFIODevice *vbasedev, int index);
135 void vfio_device_irq_unmask(VFIODevice *vbasedev, int index);
136 void vfio_device_irq_mask(VFIODevice *vbasedev, int index);
137 bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex,
138                                    int action, int fd, Error **errp);
139 
140 void vfio_device_reset_handler(void *opaque);
141 bool vfio_device_is_mdev(VFIODevice *vbasedev);
142 bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev,
143                                          const char *typename, Error **errp);
144 bool vfio_device_attach(char *name, VFIODevice *vbasedev,
145                         AddressSpace *as, Error **errp);
146 bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
147                                       VFIODevice *vbasedev, AddressSpace *as,
148                                       Error **errp);
149 void vfio_device_detach(VFIODevice *vbasedev);
150 VFIODevice *vfio_get_vfio_device(Object *obj);
151 
152 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
153 extern VFIODeviceList vfio_device_list;
154 
155 #ifdef CONFIG_LINUX
156 /*
157  * How devices communicate with the server.  The default option is through
158  * ioctl() to the kernel VFIO driver, but vfio-user can use a socket to a remote
159  * process.
160  */
161 struct VFIODeviceIOOps {
162     /**
163      * @device_feature
164      *
165      * Fill in feature info for the given device.
166      */
167     int (*device_feature)(VFIODevice *vdev, struct vfio_device_feature *);
168 
169     /**
170      * @get_region_info
171      *
172      * Fill in @info with information on the region given by @info->index.
173      */
174     int (*get_region_info)(VFIODevice *vdev,
175                            struct vfio_region_info *info);
176 
177     /**
178      * @get_irq_info
179      *
180      * Fill in @irq with information on the IRQ given by @info->index.
181      */
182     int (*get_irq_info)(VFIODevice *vdev, struct vfio_irq_info *irq);
183 
184     /**
185      * @set_irqs
186      *
187      * Configure IRQs as defined by @irqs.
188      */
189     int (*set_irqs)(VFIODevice *vdev, struct vfio_irq_set *irqs);
190 };
191 
192 void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
193                          struct vfio_device_info *info);
194 
195 void vfio_device_unprepare(VFIODevice *vbasedev);
196 
197 int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
198                                 struct vfio_region_info **info);
199 int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type,
200                                      uint32_t subtype, struct vfio_region_info **info);
201 bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
202 
203 int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
204                                 struct vfio_irq_info *info);
205 #endif
206 
207 /* Returns 0 on success, or a negative errno. */
208 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
209 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
210 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
211                       DeviceState *dev, bool ram_discard);
212 int vfio_device_get_aw_bits(VFIODevice *vdev);
213 #endif /* HW_VFIO_VFIO_COMMON_H */
214