xref: /qemu/include/hw/vfio/vfio-device.h (revision 563ac3d18129a2770a285cc16c20ad50c8adc7c0)
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 "hw/vfio/vfio-cpr.h"
32 #include "system/host_iommu_device.h"
33 #include "system/iommufd.h"
34 
35 #define VFIO_MSG_PREFIX "vfio %s: "
36 
37 enum {
38     VFIO_DEVICE_TYPE_PCI = 0,
39     VFIO_DEVICE_TYPE_PLATFORM = 1,
40     VFIO_DEVICE_TYPE_CCW = 2,
41     VFIO_DEVICE_TYPE_AP = 3,
42 };
43 
44 typedef struct VFIODeviceOps VFIODeviceOps;
45 typedef struct VFIODeviceIOOps VFIODeviceIOOps;
46 typedef struct VFIOMigration VFIOMigration;
47 
48 typedef struct IOMMUFDBackend IOMMUFDBackend;
49 typedef struct VFIOIOASHwpt VFIOIOASHwpt;
50 typedef struct VFIOUserProxy VFIOUserProxy;
51 
52 typedef struct VFIODevice {
53     QLIST_ENTRY(VFIODevice) next;
54     QLIST_ENTRY(VFIODevice) container_next;
55     QLIST_ENTRY(VFIODevice) global_next;
56     struct VFIOGroup *group;
57     VFIOContainerBase *bcontainer;
58     char *sysfsdev;
59     char *name;
60     DeviceState *dev;
61     int fd;
62     int type;
63     bool mdev;
64     bool reset_works;
65     bool needs_reset;
66     bool no_mmap;
67     bool ram_block_discard_allowed;
68     OnOffAuto enable_migration;
69     OnOffAuto migration_multifd_transfer;
70     bool migration_events;
71     bool use_region_fds;
72     VFIODeviceOps *ops;
73     VFIODeviceIOOps *io_ops;
74     unsigned int num_irqs;
75     unsigned int num_regions;
76     unsigned int flags;
77     VFIOMigration *migration;
78     Error *migration_blocker;
79     OnOffAuto pre_copy_dirty_page_tracking;
80     OnOffAuto device_dirty_page_tracking;
81     bool dirty_pages_supported;
82     bool dirty_tracking; /* Protected by BQL */
83     bool iommu_dirty_tracking;
84     HostIOMMUDevice *hiod;
85     int devid;
86     IOMMUFDBackend *iommufd;
87     VFIOIOASHwpt *hwpt;
88     QLIST_ENTRY(VFIODevice) hwpt_next;
89     struct vfio_region_info **reginfo;
90     int *region_fds;
91     VFIODeviceCPR cpr;
92     VFIOUserProxy *proxy;
93 } VFIODevice;
94 
95 struct VFIODeviceOps {
96     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
97     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
98     void (*vfio_eoi)(VFIODevice *vdev);
99     Object *(*vfio_get_object)(VFIODevice *vdev);
100 
101     /**
102      * @vfio_save_config
103      *
104      * Save device config state
105      *
106      * @vdev: #VFIODevice for which to save the config
107      * @f: #QEMUFile where to send the data
108      * @errp: pointer to Error*, to store an error if it happens.
109      *
110      * Returns zero to indicate success and negative for error
111      */
112     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
113 
114     /**
115      * @vfio_load_config
116      *
117      * Load device config state
118      *
119      * @vdev: #VFIODevice for which to load the config
120      * @f: #QEMUFile where to get the data
121      *
122      * Returns zero to indicate success and negative for error
123      */
124     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
125 };
126 
127 /*
128  * Given a return value of either a short number of bytes read or -errno,
129  * construct a meaningful error message.
130  */
131 #define strreaderror(ret) \
132     (ret < 0 ? strerror(-ret) : "short read")
133 
134 /*
135  * Given a return value of either a short number of bytes written or -errno,
136  * construct a meaningful error message.
137  */
138 #define strwriteerror(ret) \
139     (ret < 0 ? strerror(-ret) : "short write")
140 
141 void vfio_device_irq_disable(VFIODevice *vbasedev, int index);
142 void vfio_device_irq_unmask(VFIODevice *vbasedev, int index);
143 void vfio_device_irq_mask(VFIODevice *vbasedev, int index);
144 bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex,
145                                    int action, int fd, Error **errp);
146 
147 void vfio_device_reset_handler(void *opaque);
148 bool vfio_device_is_mdev(VFIODevice *vbasedev);
149 bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev,
150                                          const char *typename, Error **errp);
151 bool vfio_device_attach(char *name, VFIODevice *vbasedev,
152                         AddressSpace *as, Error **errp);
153 bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
154                                       VFIODevice *vbasedev, AddressSpace *as,
155                                       Error **errp);
156 void vfio_device_detach(VFIODevice *vbasedev);
157 VFIODevice *vfio_get_vfio_device(Object *obj);
158 
159 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
160 extern VFIODeviceList vfio_device_list;
161 
162 #ifdef CONFIG_LINUX
163 /*
164  * How devices communicate with the server.  The default option is through
165  * ioctl() to the kernel VFIO driver, but vfio-user can use a socket to a remote
166  * process.
167  */
168 struct VFIODeviceIOOps {
169     /**
170      * @device_feature
171      *
172      * Fill in feature info for the given device.
173      *
174      * @vdev: #VFIODevice to use
175      * @feat: feature information to fill in
176      *
177      * Returns 0 on success or -errno.
178      */
179     int (*device_feature)(VFIODevice *vdev, struct vfio_device_feature *feat);
180 
181     /**
182      * @get_region_info
183      *
184      * Get the information for a given region on the device.
185      *
186      * @vdev: #VFIODevice to use
187      * @info: set @info->index to the region index to look up; the rest of the
188      *        struct will be filled in on success
189      * @fd: pointer to the fd for the region; will be -1 if not found
190      *
191      * Returns 0 on success or -errno.
192      */
193     int (*get_region_info)(VFIODevice *vdev,
194                            struct vfio_region_info *info, int *fd);
195 
196     /**
197      * @get_irq_info
198      *
199      * @vdev: #VFIODevice to use
200      * @irq: set @irq->index to the IRQ index to look up; the rest of the struct
201      *       will be filled in on success
202      *
203      * Returns 0 on success or -errno.
204      */
205     int (*get_irq_info)(VFIODevice *vdev, struct vfio_irq_info *irq);
206 
207     /**
208      * @set_irqs
209      *
210      * Configure IRQs.
211      *
212      * @vdev: #VFIODevice to use
213      * @irqs: IRQ configuration as defined by VFIO docs.
214      *
215      * Returns 0 on success or -errno.
216      */
217     int (*set_irqs)(VFIODevice *vdev, struct vfio_irq_set *irqs);
218 
219     /**
220      * @region_read
221      *
222      * Read part of a region.
223      *
224      * @vdev: #VFIODevice to use
225      * @nr: region index
226      * @off: offset within the region
227      * @size: size in bytes to read
228      * @data: buffer to read into
229      *
230      * Returns number of bytes read on success or -errno.
231      */
232     int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
233                        void *data);
234 
235     /**
236      * @region_write
237      *
238      * Write part of a region.
239      *
240      * @vdev: #VFIODevice to use
241      * @nr: region index
242      * @off: offset within the region
243      * @size: size in bytes to write
244      * @data: buffer to write from
245      * @post: true if this is a posted write
246      *
247      * Returns number of bytes write on success or -errno.
248      */
249     int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
250                         void *data, bool post);
251 };
252 
253 void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
254                          struct vfio_device_info *info);
255 
256 void vfio_device_unprepare(VFIODevice *vbasedev);
257 
258 int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
259                                 struct vfio_region_info **info);
260 int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type,
261                                      uint32_t subtype, struct vfio_region_info **info);
262 
263 /**
264  * Return the fd for mapping this region. This is either the device's fd (for
265  * e.g. kernel vfio), or a per-region fd (for vfio-user).
266  *
267  * @vbasedev: #VFIODevice to use
268  * @index: region index
269  *
270  * Returns the fd.
271  */
272 int vfio_device_get_region_fd(VFIODevice *vbasedev, int index);
273 
274 bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
275 
276 int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
277                                 struct vfio_irq_info *info);
278 #endif
279 
280 /* Returns 0 on success, or a negative errno. */
281 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
282 void vfio_device_free_name(VFIODevice *vbasedev);
283 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
284 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
285                       DeviceState *dev, bool ram_discard);
286 int vfio_device_get_aw_bits(VFIODevice *vdev);
287 
288 void vfio_kvm_device_close(void);
289 #endif /* HW_VFIO_VFIO_COMMON_H */
290