2014-12-22 19:54:51 +03:00
|
|
|
/*
|
|
|
|
* common header for vfio based device assignment support
|
|
|
|
*
|
|
|
|
* Copyright Red Hat, Inc. 2012
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Alex Williamson <alex.williamson@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
* Based on qemu-kvm device-assignment:
|
|
|
|
* Adapted for KVM by Qumranet.
|
|
|
|
* Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
|
|
|
|
* Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
|
|
|
|
* Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
|
|
|
|
* Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
|
|
|
|
* Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
|
|
|
|
*/
|
2016-06-29 16:29:06 +03:00
|
|
|
|
2014-12-22 19:54:51 +03:00
|
|
|
#ifndef HW_VFIO_VFIO_COMMON_H
|
|
|
|
#define HW_VFIO_VFIO_COMMON_H
|
|
|
|
|
|
|
|
#include "exec/memory.h"
|
|
|
|
#include "qemu/queue.h"
|
|
|
|
#include "qemu/notify.h"
|
2018-03-13 20:17:30 +03:00
|
|
|
#include "ui/console.h"
|
2018-10-15 19:52:09 +03:00
|
|
|
#include "hw/display/ramfb.h"
|
2016-03-10 19:39:07 +03:00
|
|
|
#ifdef CONFIG_LINUX
|
|
|
|
#include <linux/vfio.h>
|
|
|
|
#endif
|
2020-10-26 12:36:15 +03:00
|
|
|
#include "sysemu/sysemu.h"
|
2023-11-02 10:12:27 +03:00
|
|
|
#include "hw/vfio/vfio-container-base.h"
|
2014-12-22 19:54:51 +03:00
|
|
|
|
2018-10-17 11:26:29 +03:00
|
|
|
#define VFIO_MSG_PREFIX "vfio %s: "
|
2016-10-17 19:57:56 +03:00
|
|
|
|
2014-12-22 19:54:51 +03:00
|
|
|
enum {
|
|
|
|
VFIO_DEVICE_TYPE_PCI = 0,
|
2015-06-08 18:25:25 +03:00
|
|
|
VFIO_DEVICE_TYPE_PLATFORM = 1,
|
2017-05-17 03:48:07 +03:00
|
|
|
VFIO_DEVICE_TYPE_CCW = 2,
|
2018-10-10 20:03:07 +03:00
|
|
|
VFIO_DEVICE_TYPE_AP = 3,
|
2014-12-22 19:54:51 +03:00
|
|
|
};
|
|
|
|
|
2016-03-10 19:39:07 +03:00
|
|
|
typedef struct VFIOMmap {
|
|
|
|
MemoryRegion mem;
|
|
|
|
void *mmap;
|
|
|
|
off_t offset;
|
|
|
|
size_t size;
|
|
|
|
} VFIOMmap;
|
|
|
|
|
2014-12-22 19:54:51 +03:00
|
|
|
typedef struct VFIORegion {
|
|
|
|
struct VFIODevice *vbasedev;
|
|
|
|
off_t fd_offset; /* offset of region within device fd */
|
2016-03-10 19:39:07 +03:00
|
|
|
MemoryRegion *mem; /* slow, read/write access */
|
2014-12-22 19:54:51 +03:00
|
|
|
size_t size;
|
|
|
|
uint32_t flags; /* VFIO region flags (rd/wr/mmap) */
|
2016-03-10 19:39:07 +03:00
|
|
|
uint32_t nr_mmaps;
|
|
|
|
VFIOMmap *mmaps;
|
2014-12-22 19:54:51 +03:00
|
|
|
uint8_t nr; /* cache the region number for debug */
|
|
|
|
} VFIORegion;
|
|
|
|
|
2020-10-26 12:36:14 +03:00
|
|
|
typedef struct VFIOMigration {
|
2020-10-26 12:36:16 +03:00
|
|
|
struct VFIODevice *vbasedev;
|
2020-10-26 12:36:15 +03:00
|
|
|
VMChangeStateEntry *vm_state;
|
2024-02-22 20:28:29 +03:00
|
|
|
NotifierWithReturn migration_state;
|
2023-02-16 17:36:27 +03:00
|
|
|
uint32_t device_state;
|
|
|
|
int data_fd;
|
|
|
|
void *data_buffer;
|
|
|
|
size_t data_buffer_size;
|
2023-06-21 14:11:59 +03:00
|
|
|
uint64_t mig_flags;
|
2023-06-21 14:12:00 +03:00
|
|
|
uint64_t precopy_init_size;
|
|
|
|
uint64_t precopy_dirty_size;
|
2023-06-21 14:12:01 +03:00
|
|
|
bool initial_data_sent;
|
2020-10-26 12:36:14 +03:00
|
|
|
} VFIOMigration;
|
|
|
|
|
2014-12-22 19:54:51 +03:00
|
|
|
struct VFIOGroup;
|
|
|
|
|
|
|
|
typedef struct VFIOContainer {
|
2023-11-02 10:12:27 +03:00
|
|
|
VFIOContainerBase bcontainer;
|
2014-12-22 19:54:51 +03:00
|
|
|
int fd; /* /dev/vfio/vfio, empowered by the attached groups */
|
2016-07-04 06:33:04 +03:00
|
|
|
unsigned iommu_type;
|
2014-12-22 19:54:51 +03:00
|
|
|
QLIST_HEAD(, VFIOGroup) group_list;
|
|
|
|
} VFIOContainer;
|
|
|
|
|
2016-07-04 06:33:05 +03:00
|
|
|
typedef struct VFIOHostDMAWindow {
|
|
|
|
hwaddr min_iova;
|
|
|
|
hwaddr max_iova;
|
|
|
|
uint64_t iova_pgsizes;
|
|
|
|
QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next;
|
|
|
|
} VFIOHostDMAWindow;
|
|
|
|
|
2023-11-21 11:44:03 +03:00
|
|
|
typedef struct IOMMUFDBackend IOMMUFDBackend;
|
|
|
|
|
|
|
|
typedef struct VFIOIOMMUFDContainer {
|
|
|
|
VFIOContainerBase bcontainer;
|
|
|
|
IOMMUFDBackend *be;
|
|
|
|
uint32_t ioas_id;
|
|
|
|
} VFIOIOMMUFDContainer;
|
|
|
|
|
2014-12-22 19:54:51 +03:00
|
|
|
typedef struct VFIODeviceOps VFIODeviceOps;
|
|
|
|
|
|
|
|
typedef struct VFIODevice {
|
|
|
|
QLIST_ENTRY(VFIODevice) next;
|
2023-10-09 12:09:14 +03:00
|
|
|
QLIST_ENTRY(VFIODevice) container_next;
|
2023-10-09 12:09:16 +03:00
|
|
|
QLIST_ENTRY(VFIODevice) global_next;
|
2014-12-22 19:54:51 +03:00
|
|
|
struct VFIOGroup *group;
|
2023-11-02 10:12:34 +03:00
|
|
|
VFIOContainerBase *bcontainer;
|
vfio: Add sysfsdev property for pci & platform
vfio-pci currently requires a host= parameter, which comes in the
form of a PCI address in [domain:]<bus:slot.function> notation. We
expect to find a matching entry in sysfs for that under
/sys/bus/pci/devices/. vfio-platform takes a similar approach, but
defines the host= parameter to be a string, which can be matched
directly under /sys/bus/platform/devices/. On the PCI side, we have
some interest in using vfio to expose vGPU devices. These are not
actual discrete PCI devices, so they don't have a compatible host PCI
bus address or a device link where QEMU wants to look for it. There's
also really no requirement that vfio can only be used to expose
physical devices, a new vfio bus and iommu driver could expose a
completely emulated device. To fit within the vfio framework, it
would need a kernel struct device and associated IOMMU group, but
those are easy constraints to manage.
To support such devices, which would include vGPUs, that honor the
VFIO PCI programming API, but are not necessarily backed by a unique
PCI address, add support for specifying any device in sysfs. The
vfio API already has support for probing the device type to ensure
compatibility with either vfio-pci or vfio-platform.
With this, a vfio-pci device could either be specified as:
-device vfio-pci,host=02:00.0
or
-device vfio-pci,sysfsdev=/sys/devices/pci0000:00/0000:00:1c.0/0000:02:00.0
or even
-device vfio-pci,sysfsdev=/sys/bus/pci/devices/0000:02:00.0
When vGPU support comes along, this might look something more like:
-device vfio-pci,sysfsdev=/sys/devices/virtual/intel-vgpu/vgpu0@0000:00:02.0
NB - This is only a made up example path
The same change is made for vfio-platform, specifying sysfsdev has
precedence over the old host option.
Tested-by: Eric Auger <eric.auger@linaro.org>
Reviewed-by: Eric Auger <eric.auger@linaro.org>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
2016-03-10 19:39:07 +03:00
|
|
|
char *sysfsdev;
|
2014-12-22 19:54:51 +03:00
|
|
|
char *name;
|
2017-07-10 19:39:43 +03:00
|
|
|
DeviceState *dev;
|
2014-12-22 19:54:51 +03:00
|
|
|
int fd;
|
|
|
|
int type;
|
|
|
|
bool reset_works;
|
|
|
|
bool needs_reset;
|
2015-09-23 22:04:44 +03:00
|
|
|
bool no_mmap;
|
2020-06-26 10:22:30 +03:00
|
|
|
bool ram_block_discard_allowed;
|
2023-06-28 10:31:12 +03:00
|
|
|
OnOffAuto enable_migration;
|
2014-12-22 19:54:51 +03:00
|
|
|
VFIODeviceOps *ops;
|
|
|
|
unsigned int num_irqs;
|
|
|
|
unsigned int num_regions;
|
|
|
|
unsigned int flags;
|
2020-10-26 12:36:14 +03:00
|
|
|
VFIOMigration *migration;
|
|
|
|
Error *migration_blocker;
|
2020-11-23 17:23:19 +03:00
|
|
|
OnOffAuto pre_copy_dirty_page_tracking;
|
2023-03-07 15:54:45 +03:00
|
|
|
bool dirty_pages_supported;
|
|
|
|
bool dirty_tracking;
|
2023-11-21 11:44:03 +03:00
|
|
|
int devid;
|
|
|
|
IOMMUFDBackend *iommufd;
|
2014-12-22 19:54:51 +03:00
|
|
|
} VFIODevice;
|
|
|
|
|
|
|
|
struct VFIODeviceOps {
|
|
|
|
void (*vfio_compute_needs_reset)(VFIODevice *vdev);
|
|
|
|
int (*vfio_hot_reset_multi)(VFIODevice *vdev);
|
|
|
|
void (*vfio_eoi)(VFIODevice *vdev);
|
2020-10-26 12:36:12 +03:00
|
|
|
Object *(*vfio_get_object)(VFIODevice *vdev);
|
2020-10-26 12:36:13 +03:00
|
|
|
void (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f);
|
|
|
|
int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f);
|
2014-12-22 19:54:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct VFIOGroup {
|
|
|
|
int fd;
|
|
|
|
int groupid;
|
|
|
|
VFIOContainer *container;
|
|
|
|
QLIST_HEAD(, VFIODevice) device_list;
|
|
|
|
QLIST_ENTRY(VFIOGroup) next;
|
|
|
|
QLIST_ENTRY(VFIOGroup) container_next;
|
2020-06-26 10:22:30 +03:00
|
|
|
bool ram_block_discard_allowed;
|
2014-12-22 19:54:51 +03:00
|
|
|
} VFIOGroup;
|
|
|
|
|
2018-03-13 20:17:30 +03:00
|
|
|
typedef struct VFIODMABuf {
|
|
|
|
QemuDmaBuf buf;
|
|
|
|
uint32_t pos_x, pos_y, pos_updates;
|
|
|
|
uint32_t hot_x, hot_y, hot_updates;
|
|
|
|
int dmabuf_id;
|
|
|
|
QTAILQ_ENTRY(VFIODMABuf) next;
|
|
|
|
} VFIODMABuf;
|
|
|
|
|
2018-03-13 20:17:30 +03:00
|
|
|
typedef struct VFIODisplay {
|
|
|
|
QemuConsole *con;
|
2018-10-15 19:52:09 +03:00
|
|
|
RAMFBState *ramfb;
|
2019-03-11 20:14:39 +03:00
|
|
|
struct vfio_region_info *edid_info;
|
|
|
|
struct vfio_region_gfx_edid *edid_regs;
|
|
|
|
uint8_t *edid_blob;
|
2019-03-11 20:14:40 +03:00
|
|
|
QEMUTimer *edid_link_timer;
|
2018-03-13 20:17:30 +03:00
|
|
|
struct {
|
|
|
|
VFIORegion buffer;
|
|
|
|
DisplaySurface *surface;
|
|
|
|
} region;
|
2018-03-13 20:17:30 +03:00
|
|
|
struct {
|
|
|
|
QTAILQ_HEAD(, VFIODMABuf) bufs;
|
|
|
|
VFIODMABuf *primary;
|
|
|
|
VFIODMABuf *cursor;
|
|
|
|
} dmabuf;
|
2018-03-13 20:17:30 +03:00
|
|
|
} VFIODisplay;
|
|
|
|
|
2023-10-09 12:09:17 +03:00
|
|
|
VFIOAddressSpace *vfio_get_address_space(AddressSpace *as);
|
|
|
|
void vfio_put_address_space(VFIOAddressSpace *space);
|
|
|
|
|
2023-11-02 10:12:24 +03:00
|
|
|
/* SPAPR specific */
|
|
|
|
int vfio_spapr_container_init(VFIOContainer *container, Error **errp);
|
|
|
|
void vfio_spapr_container_deinit(VFIOContainer *container);
|
2023-10-09 12:09:17 +03:00
|
|
|
|
2014-12-22 19:54:51 +03:00
|
|
|
void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
|
|
|
|
void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
|
|
|
|
void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
|
2019-06-13 18:57:37 +03:00
|
|
|
int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
|
|
|
|
int action, int fd, Error **errp);
|
2014-12-22 19:54:51 +03:00
|
|
|
void vfio_region_write(void *opaque, hwaddr addr,
|
|
|
|
uint64_t data, unsigned size);
|
|
|
|
uint64_t vfio_region_read(void *opaque,
|
|
|
|
hwaddr addr, unsigned size);
|
2016-03-10 19:39:07 +03:00
|
|
|
int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region,
|
|
|
|
int index, const char *name);
|
|
|
|
int vfio_region_mmap(VFIORegion *region);
|
|
|
|
void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled);
|
2020-10-26 12:36:11 +03:00
|
|
|
void vfio_region_unmap(VFIORegion *region);
|
2016-03-10 19:39:07 +03:00
|
|
|
void vfio_region_exit(VFIORegion *region);
|
|
|
|
void vfio_region_finalize(VFIORegion *region);
|
2014-12-22 19:54:51 +03:00
|
|
|
void vfio_reset_handler(void *opaque);
|
2023-06-01 17:45:06 +03:00
|
|
|
struct vfio_device_info *vfio_get_device_info(int fd);
|
2023-10-09 12:09:09 +03:00
|
|
|
int vfio_attach_device(char *name, VFIODevice *vbasedev,
|
|
|
|
AddressSpace *as, Error **errp);
|
|
|
|
void vfio_detach_device(VFIODevice *vbasedev);
|
2014-12-22 19:54:51 +03:00
|
|
|
|
2023-10-09 12:09:08 +03:00
|
|
|
int vfio_kvm_device_add_fd(int fd, Error **errp);
|
|
|
|
int vfio_kvm_device_del_fd(int fd, Error **errp);
|
|
|
|
|
2014-12-22 19:54:51 +03:00
|
|
|
extern const MemoryRegionOps vfio_region_ops;
|
2018-12-06 13:56:15 +03:00
|
|
|
typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
|
2023-10-09 12:09:16 +03:00
|
|
|
typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
|
2018-12-06 13:56:15 +03:00
|
|
|
extern VFIOGroupList vfio_group_list;
|
2023-10-09 12:09:17 +03:00
|
|
|
extern VFIODeviceList vfio_device_list;
|
|
|
|
extern const MemoryListener vfio_memory_listener;
|
|
|
|
extern int vfio_kvm_device_fd;
|
2014-12-22 19:54:51 +03:00
|
|
|
|
2020-10-26 12:36:27 +03:00
|
|
|
bool vfio_mig_active(void);
|
2023-06-28 10:31:12 +03:00
|
|
|
int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp);
|
2023-02-16 17:36:24 +03:00
|
|
|
void vfio_unblock_multiple_devices_migration(void);
|
2023-07-03 10:15:07 +03:00
|
|
|
bool vfio_viommu_preset(VFIODevice *vbasedev);
|
2020-10-26 12:36:27 +03:00
|
|
|
int64_t vfio_mig_bytes_transferred(void);
|
2023-06-28 10:31:11 +03:00
|
|
|
void vfio_reset_bytes_transferred(void);
|
2023-08-02 11:14:47 +03:00
|
|
|
bool vfio_device_state_is_running(VFIODevice *vbasedev);
|
|
|
|
bool vfio_device_state_is_precopy(VFIODevice *vbasedev);
|
2020-10-26 12:36:27 +03:00
|
|
|
|
2016-03-10 19:39:07 +03:00
|
|
|
#ifdef CONFIG_LINUX
|
|
|
|
int vfio_get_region_info(VFIODevice *vbasedev, int index,
|
|
|
|
struct vfio_region_info **info);
|
2016-05-26 18:43:20 +03:00
|
|
|
int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
|
|
|
|
uint32_t subtype, struct vfio_region_info **info);
|
2018-03-13 20:17:31 +03:00
|
|
|
bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type);
|
2019-03-07 08:05:17 +03:00
|
|
|
struct vfio_info_cap_header *
|
|
|
|
vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id);
|
2020-10-26 18:34:33 +03:00
|
|
|
bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,
|
|
|
|
unsigned int *avail);
|
2020-10-26 18:34:40 +03:00
|
|
|
struct vfio_info_cap_header *
|
|
|
|
vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id);
|
2023-10-09 12:09:05 +03:00
|
|
|
struct vfio_info_cap_header *
|
|
|
|
vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id);
|
2016-03-10 19:39:07 +03:00
|
|
|
#endif
|
2016-07-04 06:33:04 +03:00
|
|
|
|
2023-07-03 10:15:10 +03:00
|
|
|
bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
|
2023-03-07 19:53:46 +03:00
|
|
|
void vfio_migration_exit(VFIODevice *vbasedev);
|
2020-10-26 12:36:14 +03:00
|
|
|
|
2023-10-09 12:09:05 +03:00
|
|
|
int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size);
|
2023-11-21 11:44:17 +03:00
|
|
|
bool
|
|
|
|
vfio_devices_all_running_and_mig_active(const VFIOContainerBase *bcontainer);
|
|
|
|
bool
|
|
|
|
vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer);
|
|
|
|
int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
|
2023-10-09 12:09:17 +03:00
|
|
|
VFIOBitmap *vbmap, hwaddr iova,
|
|
|
|
hwaddr size);
|
2023-11-21 11:44:17 +03:00
|
|
|
int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
|
|
|
|
uint64_t size, ram_addr_t ram_addr);
|
2023-11-21 11:44:10 +03:00
|
|
|
|
|
|
|
/* Returns 0 on success, or a negative errno. */
|
|
|
|
int vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
|
|
|
|
void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
|
2023-11-21 11:44:25 +03:00
|
|
|
void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
|
|
|
|
DeviceState *dev, bool ram_discard);
|
2016-06-29 16:29:06 +03:00
|
|
|
#endif /* HW_VFIO_VFIO_COMMON_H */
|