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