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