xref: /openbmc/qemu/include/hw/vfio/vfio-device.h (revision 184053f04f6ad6b2950d4712063ffed43bb2720f)
111b8b9d5SCédric Le Goater /*
211b8b9d5SCédric Le Goater  * VFIO Device interface
311b8b9d5SCédric Le Goater  *
411b8b9d5SCédric Le Goater  * Copyright Red Hat, Inc. 2012
511b8b9d5SCédric Le Goater  *
611b8b9d5SCédric Le Goater  * Authors:
711b8b9d5SCédric Le Goater  *  Alex Williamson <alex.williamson@redhat.com>
811b8b9d5SCédric Le Goater  *
911b8b9d5SCédric Le Goater  * This work is licensed under the terms of the GNU GPL, version 2.  See
1011b8b9d5SCédric Le Goater  * the COPYING file in the top-level directory.
1111b8b9d5SCédric Le Goater  *
1211b8b9d5SCédric Le Goater  * Based on qemu-kvm device-assignment:
1311b8b9d5SCédric Le Goater  *  Adapted for KVM by Qumranet.
1411b8b9d5SCédric Le Goater  *  Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
1511b8b9d5SCédric Le Goater  *  Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
1611b8b9d5SCédric Le Goater  *  Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
1711b8b9d5SCédric Le Goater  *  Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
1811b8b9d5SCédric Le Goater  *  Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
1911b8b9d5SCédric Le Goater  */
2011b8b9d5SCédric Le Goater 
2111b8b9d5SCédric Le Goater #ifndef HW_VFIO_VFIO_COMMON_H
2211b8b9d5SCédric Le Goater #define HW_VFIO_VFIO_COMMON_H
2311b8b9d5SCédric Le Goater 
2411b8b9d5SCédric Le Goater #include "system/memory.h"
2511b8b9d5SCédric Le Goater #include "qemu/queue.h"
2611b8b9d5SCédric Le Goater #ifdef CONFIG_LINUX
2711b8b9d5SCédric Le Goater #include <linux/vfio.h>
2811b8b9d5SCédric Le Goater #endif
2911b8b9d5SCédric Le Goater #include "system/system.h"
3011b8b9d5SCédric Le Goater #include "hw/vfio/vfio-container-base.h"
31dac0dd68SSteve Sistare #include "hw/vfio/vfio-cpr.h"
3211b8b9d5SCédric Le Goater #include "system/host_iommu_device.h"
3311b8b9d5SCédric Le Goater #include "system/iommufd.h"
3411b8b9d5SCédric Le Goater 
3511b8b9d5SCédric Le Goater #define VFIO_MSG_PREFIX "vfio %s: "
3611b8b9d5SCédric Le Goater 
3711b8b9d5SCédric Le Goater enum {
3811b8b9d5SCédric Le Goater     VFIO_DEVICE_TYPE_PCI = 0,
3911b8b9d5SCédric Le Goater     VFIO_DEVICE_TYPE_PLATFORM = 1,
4011b8b9d5SCédric Le Goater     VFIO_DEVICE_TYPE_CCW = 2,
4111b8b9d5SCédric Le Goater     VFIO_DEVICE_TYPE_AP = 3,
4211b8b9d5SCédric Le Goater };
4311b8b9d5SCédric Le Goater 
4411b8b9d5SCédric Le Goater typedef struct VFIODeviceOps VFIODeviceOps;
4538bf025dSJohn Levon typedef struct VFIODeviceIOOps VFIODeviceIOOps;
4611b8b9d5SCédric Le Goater typedef struct VFIOMigration VFIOMigration;
4711b8b9d5SCédric Le Goater 
4811b8b9d5SCédric Le Goater typedef struct IOMMUFDBackend IOMMUFDBackend;
4911b8b9d5SCédric Le Goater typedef struct VFIOIOASHwpt VFIOIOASHwpt;
50438d863fSJohn Levon typedef struct VFIOUserProxy VFIOUserProxy;
5111b8b9d5SCédric Le Goater 
5211b8b9d5SCédric Le Goater typedef struct VFIODevice {
5311b8b9d5SCédric Le Goater     QLIST_ENTRY(VFIODevice) next;
5411b8b9d5SCédric Le Goater     QLIST_ENTRY(VFIODevice) container_next;
5511b8b9d5SCédric Le Goater     QLIST_ENTRY(VFIODevice) global_next;
5611b8b9d5SCédric Le Goater     struct VFIOGroup *group;
5711b8b9d5SCédric Le Goater     VFIOContainerBase *bcontainer;
5811b8b9d5SCédric Le Goater     char *sysfsdev;
5911b8b9d5SCédric Le Goater     char *name;
6011b8b9d5SCédric Le Goater     DeviceState *dev;
6111b8b9d5SCédric Le Goater     int fd;
6211b8b9d5SCédric Le Goater     int type;
6311b8b9d5SCédric Le Goater     bool mdev;
6411b8b9d5SCédric Le Goater     bool reset_works;
6511b8b9d5SCédric Le Goater     bool needs_reset;
6611b8b9d5SCédric Le Goater     bool no_mmap;
6711b8b9d5SCédric Le Goater     bool ram_block_discard_allowed;
6811b8b9d5SCédric Le Goater     OnOffAuto enable_migration;
6911b8b9d5SCédric Le Goater     OnOffAuto migration_multifd_transfer;
7011b8b9d5SCédric Le Goater     bool migration_events;
7159adfc6fSJohn Levon     bool use_region_fds;
7211b8b9d5SCédric Le Goater     VFIODeviceOps *ops;
7338bf025dSJohn Levon     VFIODeviceIOOps *io_ops;
7411b8b9d5SCédric Le Goater     unsigned int num_irqs;
7511b8b9d5SCédric Le Goater     unsigned int num_regions;
7611b8b9d5SCédric Le Goater     unsigned int flags;
7711b8b9d5SCédric Le Goater     VFIOMigration *migration;
7811b8b9d5SCédric Le Goater     Error *migration_blocker;
7911b8b9d5SCédric Le Goater     OnOffAuto pre_copy_dirty_page_tracking;
8011b8b9d5SCédric Le Goater     OnOffAuto device_dirty_page_tracking;
8111b8b9d5SCédric Le Goater     bool dirty_pages_supported;
8211b8b9d5SCédric Le Goater     bool dirty_tracking; /* Protected by BQL */
8311b8b9d5SCédric Le Goater     bool iommu_dirty_tracking;
8411b8b9d5SCédric Le Goater     HostIOMMUDevice *hiod;
8511b8b9d5SCédric Le Goater     int devid;
8611b8b9d5SCédric Le Goater     IOMMUFDBackend *iommufd;
8711b8b9d5SCédric Le Goater     VFIOIOASHwpt *hwpt;
8811b8b9d5SCédric Le Goater     QLIST_ENTRY(VFIODevice) hwpt_next;
8995cdb024SJohn Levon     struct vfio_region_info **reginfo;
9059adfc6fSJohn Levon     int *region_fds;
91dac0dd68SSteve Sistare     VFIODeviceCPR cpr;
92438d863fSJohn Levon     VFIOUserProxy *proxy;
9311b8b9d5SCédric Le Goater } VFIODevice;
9411b8b9d5SCédric Le Goater 
9511b8b9d5SCédric Le Goater struct VFIODeviceOps {
9611b8b9d5SCédric Le Goater     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
9711b8b9d5SCédric Le Goater     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
9811b8b9d5SCédric Le Goater     void (*vfio_eoi)(VFIODevice *vdev);
9911b8b9d5SCédric Le Goater     Object *(*vfio_get_object)(VFIODevice *vdev);
10011b8b9d5SCédric Le Goater 
10111b8b9d5SCédric Le Goater     /**
10211b8b9d5SCédric Le Goater      * @vfio_save_config
10311b8b9d5SCédric Le Goater      *
10411b8b9d5SCédric Le Goater      * Save device config state
10511b8b9d5SCédric Le Goater      *
10611b8b9d5SCédric Le Goater      * @vdev: #VFIODevice for which to save the config
10711b8b9d5SCédric Le Goater      * @f: #QEMUFile where to send the data
10811b8b9d5SCédric Le Goater      * @errp: pointer to Error*, to store an error if it happens.
10911b8b9d5SCédric Le Goater      *
11011b8b9d5SCédric Le Goater      * Returns zero to indicate success and negative for error
11111b8b9d5SCédric Le Goater      */
11211b8b9d5SCédric Le Goater     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
11311b8b9d5SCédric Le Goater 
11411b8b9d5SCédric Le Goater     /**
11511b8b9d5SCédric Le Goater      * @vfio_load_config
11611b8b9d5SCédric Le Goater      *
11711b8b9d5SCédric Le Goater      * Load device config state
11811b8b9d5SCédric Le Goater      *
11911b8b9d5SCédric Le Goater      * @vdev: #VFIODevice for which to load the config
12011b8b9d5SCédric Le Goater      * @f: #QEMUFile where to get the data
12111b8b9d5SCédric Le Goater      *
12211b8b9d5SCédric Le Goater      * Returns zero to indicate success and negative for error
12311b8b9d5SCédric Le Goater      */
12411b8b9d5SCédric Le Goater     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
12511b8b9d5SCédric Le Goater };
12611b8b9d5SCédric Le Goater 
1275363a1a1SJohn Levon /*
1285363a1a1SJohn Levon  * Given a return value of either a short number of bytes read or -errno,
1295363a1a1SJohn Levon  * construct a meaningful error message.
1305363a1a1SJohn Levon  */
1315363a1a1SJohn Levon #define strreaderror(ret) \
1325363a1a1SJohn Levon     (ret < 0 ? strerror(-ret) : "short read")
1335363a1a1SJohn Levon 
1345363a1a1SJohn Levon /*
1355363a1a1SJohn Levon  * Given a return value of either a short number of bytes written or -errno,
1365363a1a1SJohn Levon  * construct a meaningful error message.
1375363a1a1SJohn Levon  */
1385363a1a1SJohn Levon #define strwriteerror(ret) \
1395363a1a1SJohn Levon     (ret < 0 ? strerror(-ret) : "short write")
1405363a1a1SJohn Levon 
141e218ccf0SCédric Le Goater void vfio_device_irq_disable(VFIODevice *vbasedev, int index);
142e218ccf0SCédric Le Goater void vfio_device_irq_unmask(VFIODevice *vbasedev, int index);
143e218ccf0SCédric Le Goater void vfio_device_irq_mask(VFIODevice *vbasedev, int index);
144e218ccf0SCédric Le Goater bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex,
14511b8b9d5SCédric Le Goater                                    int action, int fd, Error **errp);
14611b8b9d5SCédric Le Goater 
147e218ccf0SCédric Le Goater void vfio_device_reset_handler(void *opaque);
14811b8b9d5SCédric Le Goater bool vfio_device_is_mdev(VFIODevice *vbasedev);
1490805f829SZhenzhong Duan bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev,
1500805f829SZhenzhong Duan                                          const char *typename, Error **errp);
151e218ccf0SCédric Le Goater bool vfio_device_attach(char *name, VFIODevice *vbasedev,
15211b8b9d5SCédric Le Goater                         AddressSpace *as, Error **errp);
153ef73671fSJohn Levon bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name,
154ef73671fSJohn Levon                                       VFIODevice *vbasedev, AddressSpace *as,
155ef73671fSJohn Levon                                       Error **errp);
156e218ccf0SCédric Le Goater void vfio_device_detach(VFIODevice *vbasedev);
15711b8b9d5SCédric Le Goater VFIODevice *vfio_get_vfio_device(Object *obj);
15811b8b9d5SCédric Le Goater 
15911b8b9d5SCédric Le Goater typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
16011b8b9d5SCédric Le Goater extern VFIODeviceList vfio_device_list;
16111b8b9d5SCédric Le Goater 
16211b8b9d5SCédric Le Goater #ifdef CONFIG_LINUX
16338bf025dSJohn Levon /*
16438bf025dSJohn Levon  * How devices communicate with the server.  The default option is through
16538bf025dSJohn Levon  * ioctl() to the kernel VFIO driver, but vfio-user can use a socket to a remote
16638bf025dSJohn Levon  * process.
16738bf025dSJohn Levon  */
16838bf025dSJohn Levon struct VFIODeviceIOOps {
16938bf025dSJohn Levon     /**
17038bf025dSJohn Levon      * @device_feature
17138bf025dSJohn Levon      *
17238bf025dSJohn Levon      * Fill in feature info for the given device.
173079e7216SJohn Levon      *
174079e7216SJohn Levon      * @vdev: #VFIODevice to use
175079e7216SJohn Levon      * @feat: feature information to fill in
176079e7216SJohn Levon      *
177079e7216SJohn Levon      * Returns 0 on success or -errno.
17838bf025dSJohn Levon      */
179079e7216SJohn Levon     int (*device_feature)(VFIODevice *vdev, struct vfio_device_feature *feat);
18038bf025dSJohn Levon 
18138bf025dSJohn Levon     /**
18238bf025dSJohn Levon      * @get_region_info
18338bf025dSJohn Levon      *
184079e7216SJohn Levon      * Get the information for a given region on the device.
185079e7216SJohn Levon      *
186079e7216SJohn Levon      * @vdev: #VFIODevice to use
187079e7216SJohn Levon      * @info: set @info->index to the region index to look up; the rest of the
188079e7216SJohn Levon      *        struct will be filled in on success
189079e7216SJohn Levon      * @fd: pointer to the fd for the region; will be -1 if not found
190079e7216SJohn Levon      *
191079e7216SJohn Levon      * Returns 0 on success or -errno.
19238bf025dSJohn Levon      */
19338bf025dSJohn Levon     int (*get_region_info)(VFIODevice *vdev,
19459adfc6fSJohn Levon                            struct vfio_region_info *info, int *fd);
19538bf025dSJohn Levon 
19638bf025dSJohn Levon     /**
19738bf025dSJohn Levon      * @get_irq_info
19838bf025dSJohn Levon      *
199079e7216SJohn Levon      * @vdev: #VFIODevice to use
200079e7216SJohn Levon      * @irq: set @irq->index to the IRQ index to look up; the rest of the struct
201079e7216SJohn Levon      *       will be filled in on success
202079e7216SJohn Levon      *
203079e7216SJohn Levon      * Returns 0 on success or -errno.
20438bf025dSJohn Levon      */
20538bf025dSJohn Levon     int (*get_irq_info)(VFIODevice *vdev, struct vfio_irq_info *irq);
20638bf025dSJohn Levon 
20738bf025dSJohn Levon     /**
20838bf025dSJohn Levon      * @set_irqs
20938bf025dSJohn Levon      *
210079e7216SJohn Levon      * Configure IRQs.
211079e7216SJohn Levon      *
212079e7216SJohn Levon      * @vdev: #VFIODevice to use
213079e7216SJohn Levon      * @irqs: IRQ configuration as defined by VFIO docs.
214079e7216SJohn Levon      *
215079e7216SJohn Levon      * Returns 0 on success or -errno.
21638bf025dSJohn Levon      */
21738bf025dSJohn Levon     int (*set_irqs)(VFIODevice *vdev, struct vfio_irq_set *irqs);
218776066acSJohn Levon 
219776066acSJohn Levon     /**
220776066acSJohn Levon      * @region_read
221776066acSJohn Levon      *
222079e7216SJohn Levon      * Read part of a region.
223079e7216SJohn Levon      *
224079e7216SJohn Levon      * @vdev: #VFIODevice to use
225079e7216SJohn Levon      * @nr: region index
226079e7216SJohn Levon      * @off: offset within the region
227079e7216SJohn Levon      * @size: size in bytes to read
228079e7216SJohn Levon      * @data: buffer to read into
229079e7216SJohn Levon      *
230079e7216SJohn Levon      * Returns number of bytes read on success or -errno.
231776066acSJohn Levon      */
232776066acSJohn Levon     int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
233776066acSJohn Levon                        void *data);
234776066acSJohn Levon 
235776066acSJohn Levon     /**
236776066acSJohn Levon      * @region_write
237776066acSJohn Levon      *
238079e7216SJohn Levon      * Write part of a region.
239079e7216SJohn Levon      *
240079e7216SJohn Levon      * @vdev: #VFIODevice to use
241079e7216SJohn Levon      * @nr: region index
242079e7216SJohn Levon      * @off: offset within the region
243079e7216SJohn Levon      * @size: size in bytes to write
244079e7216SJohn Levon      * @data: buffer to write from
2458d60d069SJohn Levon      * @post: true if this is a posted write
246079e7216SJohn Levon      *
247079e7216SJohn Levon      * Returns number of bytes write on success or -errno.
248776066acSJohn Levon      */
249776066acSJohn Levon     int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
250a574b061SJohn Levon                         void *data, bool post);
25138bf025dSJohn Levon };
25238bf025dSJohn Levon 
253a901682fSJohn Levon void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
254a901682fSJohn Levon                          struct vfio_device_info *info);
255a901682fSJohn Levon 
256d60fb709SJohn Levon void vfio_device_unprepare(VFIODevice *vbasedev);
257d60fb709SJohn Levon 
258e218ccf0SCédric Le Goater int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
25911b8b9d5SCédric Le Goater                                 struct vfio_region_info **info);
260e218ccf0SCédric Le Goater int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type,
26111b8b9d5SCédric Le Goater                                      uint32_t subtype, struct vfio_region_info **info);
262b1f521deSJohn Levon 
263b1f521deSJohn Levon /**
264b1f521deSJohn Levon  * Return the fd for mapping this region. This is either the device's fd (for
265b1f521deSJohn Levon  * e.g. kernel vfio), or a per-region fd (for vfio-user).
266b1f521deSJohn Levon  *
267b1f521deSJohn Levon  * @vbasedev: #VFIODevice to use
268b1f521deSJohn Levon  * @index: region index
269b1f521deSJohn Levon  *
270b1f521deSJohn Levon  * Returns the fd.
271b1f521deSJohn Levon  */
272b1f521deSJohn Levon int vfio_device_get_region_fd(VFIODevice *vbasedev, int index);
273b1f521deSJohn Levon 
274e218ccf0SCédric Le Goater bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
2755321e623SJohn Levon 
2765321e623SJohn Levon int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
2775321e623SJohn Levon                                 struct vfio_irq_info *info);
27811b8b9d5SCédric Le Goater #endif
27911b8b9d5SCédric Le Goater 
28011b8b9d5SCédric Le Goater /* Returns 0 on success, or a negative errno. */
28111b8b9d5SCédric Le Goater bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
282*184053f0SSteve Sistare void vfio_device_free_name(VFIODevice *vbasedev);
28311b8b9d5SCédric Le Goater void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
28411b8b9d5SCédric Le Goater void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
28511b8b9d5SCédric Le Goater                       DeviceState *dev, bool ram_discard);
28611b8b9d5SCédric Le Goater int vfio_device_get_aw_bits(VFIODevice *vdev);
2877ed09191SSteve Sistare 
2887ed09191SSteve Sistare void vfio_kvm_device_close(void);
28911b8b9d5SCédric Le Goater #endif /* HW_VFIO_VFIO_COMMON_H */
290