xref: /openbmc/qemu/include/hw/vfio/vfio-device.h (revision a901682f53b51c07dc27aab7e30256855a2a1f2f)
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"
3111b8b9d5SCédric Le Goater #include "system/host_iommu_device.h"
3211b8b9d5SCédric Le Goater #include "system/iommufd.h"
3311b8b9d5SCédric Le Goater 
3411b8b9d5SCédric Le Goater #define VFIO_MSG_PREFIX "vfio %s: "
3511b8b9d5SCédric Le Goater 
3611b8b9d5SCédric Le Goater enum {
3711b8b9d5SCédric Le Goater     VFIO_DEVICE_TYPE_PCI = 0,
3811b8b9d5SCédric Le Goater     VFIO_DEVICE_TYPE_PLATFORM = 1,
3911b8b9d5SCédric Le Goater     VFIO_DEVICE_TYPE_CCW = 2,
4011b8b9d5SCédric Le Goater     VFIO_DEVICE_TYPE_AP = 3,
4111b8b9d5SCédric Le Goater };
4211b8b9d5SCédric Le Goater 
4311b8b9d5SCédric Le Goater typedef struct VFIODeviceOps VFIODeviceOps;
4411b8b9d5SCédric Le Goater typedef struct VFIOMigration VFIOMigration;
4511b8b9d5SCédric Le Goater 
4611b8b9d5SCédric Le Goater typedef struct IOMMUFDBackend IOMMUFDBackend;
4711b8b9d5SCédric Le Goater typedef struct VFIOIOASHwpt VFIOIOASHwpt;
4811b8b9d5SCédric Le Goater 
4911b8b9d5SCédric Le Goater typedef struct VFIODevice {
5011b8b9d5SCédric Le Goater     QLIST_ENTRY(VFIODevice) next;
5111b8b9d5SCédric Le Goater     QLIST_ENTRY(VFIODevice) container_next;
5211b8b9d5SCédric Le Goater     QLIST_ENTRY(VFIODevice) global_next;
5311b8b9d5SCédric Le Goater     struct VFIOGroup *group;
5411b8b9d5SCédric Le Goater     VFIOContainerBase *bcontainer;
5511b8b9d5SCédric Le Goater     char *sysfsdev;
5611b8b9d5SCédric Le Goater     char *name;
5711b8b9d5SCédric Le Goater     DeviceState *dev;
5811b8b9d5SCédric Le Goater     int fd;
5911b8b9d5SCédric Le Goater     int type;
6011b8b9d5SCédric Le Goater     bool mdev;
6111b8b9d5SCédric Le Goater     bool reset_works;
6211b8b9d5SCédric Le Goater     bool needs_reset;
6311b8b9d5SCédric Le Goater     bool no_mmap;
6411b8b9d5SCédric Le Goater     bool ram_block_discard_allowed;
6511b8b9d5SCédric Le Goater     OnOffAuto enable_migration;
6611b8b9d5SCédric Le Goater     OnOffAuto migration_multifd_transfer;
6711b8b9d5SCédric Le Goater     bool migration_events;
6811b8b9d5SCédric Le Goater     VFIODeviceOps *ops;
6911b8b9d5SCédric Le Goater     unsigned int num_irqs;
7011b8b9d5SCédric Le Goater     unsigned int num_regions;
7111b8b9d5SCédric Le Goater     unsigned int flags;
7211b8b9d5SCédric Le Goater     VFIOMigration *migration;
7311b8b9d5SCédric Le Goater     Error *migration_blocker;
7411b8b9d5SCédric Le Goater     OnOffAuto pre_copy_dirty_page_tracking;
7511b8b9d5SCédric Le Goater     OnOffAuto device_dirty_page_tracking;
7611b8b9d5SCédric Le Goater     bool dirty_pages_supported;
7711b8b9d5SCédric Le Goater     bool dirty_tracking; /* Protected by BQL */
7811b8b9d5SCédric Le Goater     bool iommu_dirty_tracking;
7911b8b9d5SCédric Le Goater     HostIOMMUDevice *hiod;
8011b8b9d5SCédric Le Goater     int devid;
8111b8b9d5SCédric Le Goater     IOMMUFDBackend *iommufd;
8211b8b9d5SCédric Le Goater     VFIOIOASHwpt *hwpt;
8311b8b9d5SCédric Le Goater     QLIST_ENTRY(VFIODevice) hwpt_next;
8411b8b9d5SCédric Le Goater } VFIODevice;
8511b8b9d5SCédric Le Goater 
8611b8b9d5SCédric Le Goater struct VFIODeviceOps {
8711b8b9d5SCédric Le Goater     void (*vfio_compute_needs_reset)(VFIODevice *vdev);
8811b8b9d5SCédric Le Goater     int (*vfio_hot_reset_multi)(VFIODevice *vdev);
8911b8b9d5SCédric Le Goater     void (*vfio_eoi)(VFIODevice *vdev);
9011b8b9d5SCédric Le Goater     Object *(*vfio_get_object)(VFIODevice *vdev);
9111b8b9d5SCédric Le Goater 
9211b8b9d5SCédric Le Goater     /**
9311b8b9d5SCédric Le Goater      * @vfio_save_config
9411b8b9d5SCédric Le Goater      *
9511b8b9d5SCédric Le Goater      * Save device config state
9611b8b9d5SCédric Le Goater      *
9711b8b9d5SCédric Le Goater      * @vdev: #VFIODevice for which to save the config
9811b8b9d5SCédric Le Goater      * @f: #QEMUFile where to send the data
9911b8b9d5SCédric Le Goater      * @errp: pointer to Error*, to store an error if it happens.
10011b8b9d5SCédric Le Goater      *
10111b8b9d5SCédric Le Goater      * Returns zero to indicate success and negative for error
10211b8b9d5SCédric Le Goater      */
10311b8b9d5SCédric Le Goater     int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
10411b8b9d5SCédric Le Goater 
10511b8b9d5SCédric Le Goater     /**
10611b8b9d5SCédric Le Goater      * @vfio_load_config
10711b8b9d5SCédric Le Goater      *
10811b8b9d5SCédric Le Goater      * Load device config state
10911b8b9d5SCédric Le Goater      *
11011b8b9d5SCédric Le Goater      * @vdev: #VFIODevice for which to load the config
11111b8b9d5SCédric Le Goater      * @f: #QEMUFile where to get the data
11211b8b9d5SCédric Le Goater      *
11311b8b9d5SCédric Le Goater      * Returns zero to indicate success and negative for error
11411b8b9d5SCédric Le Goater      */
11511b8b9d5SCédric Le Goater     int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
11611b8b9d5SCédric Le Goater };
11711b8b9d5SCédric Le Goater 
118e218ccf0SCédric Le Goater void vfio_device_irq_disable(VFIODevice *vbasedev, int index);
119e218ccf0SCédric Le Goater void vfio_device_irq_unmask(VFIODevice *vbasedev, int index);
120e218ccf0SCédric Le Goater void vfio_device_irq_mask(VFIODevice *vbasedev, int index);
121e218ccf0SCédric Le Goater bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex,
12211b8b9d5SCédric Le Goater                                    int action, int fd, Error **errp);
12311b8b9d5SCédric Le Goater 
124e218ccf0SCédric Le Goater void vfio_device_reset_handler(void *opaque);
12511b8b9d5SCédric Le Goater bool vfio_device_is_mdev(VFIODevice *vbasedev);
1260805f829SZhenzhong Duan bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev,
1270805f829SZhenzhong Duan                                          const char *typename, Error **errp);
128e218ccf0SCédric Le Goater bool vfio_device_attach(char *name, VFIODevice *vbasedev,
12911b8b9d5SCédric Le Goater                         AddressSpace *as, Error **errp);
130e218ccf0SCédric Le Goater void vfio_device_detach(VFIODevice *vbasedev);
13111b8b9d5SCédric Le Goater VFIODevice *vfio_get_vfio_device(Object *obj);
13211b8b9d5SCédric Le Goater 
13311b8b9d5SCédric Le Goater typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
13411b8b9d5SCédric Le Goater extern VFIODeviceList vfio_device_list;
13511b8b9d5SCédric Le Goater 
13611b8b9d5SCédric Le Goater #ifdef CONFIG_LINUX
137*a901682fSJohn Levon void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
138*a901682fSJohn Levon                          struct vfio_device_info *info);
139*a901682fSJohn Levon 
140e218ccf0SCédric Le Goater int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
14111b8b9d5SCédric Le Goater                                 struct vfio_region_info **info);
142e218ccf0SCédric Le Goater int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type,
14311b8b9d5SCédric Le Goater                                      uint32_t subtype, struct vfio_region_info **info);
144e218ccf0SCédric Le Goater bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
14511b8b9d5SCédric Le Goater #endif
14611b8b9d5SCédric Le Goater 
14711b8b9d5SCédric Le Goater /* Returns 0 on success, or a negative errno. */
14811b8b9d5SCédric Le Goater bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
14911b8b9d5SCédric Le Goater void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
15011b8b9d5SCédric Le Goater void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
15111b8b9d5SCédric Le Goater                       DeviceState *dev, bool ram_discard);
15211b8b9d5SCédric Le Goater int vfio_device_get_aw_bits(VFIODevice *vdev);
15311b8b9d5SCédric Le Goater #endif /* HW_VFIO_VFIO_COMMON_H */
154