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