Skip to content

Commit

Permalink
vfio/iommufd: Introduce a VFIOIOMMU iommufd QOM interface
Browse files Browse the repository at this point in the history
As previously done for the sPAPR and legacy IOMMU backends, convert
the VFIOIOMMUOps struct to a QOM interface. The set of of operations
for this backend can be referenced with a literal typename instead of
a C struct.

Reviewed-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Tested-by: Eric Farman <farman@linux.ibm.com>
Signed-off-by: Cédric Le Goater <clg@redhat.com>
  • Loading branch information
legoater committed Jan 5, 2024
1 parent f221f64 commit ce5f6d4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion hw/vfio/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ int vfio_attach_device(char *name, VFIODevice *vbasedev,

#ifdef CONFIG_IOMMUFD
if (vbasedev->iommufd) {
ops = &vfio_iommufd_ops;
ops = VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
}
#endif

Expand Down
35 changes: 26 additions & 9 deletions hw/vfio/iommufd.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
int ret, devfd;
uint32_t ioas_id;
Error *err = NULL;
const VFIOIOMMUClass *iommufd_vioc =
VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));

if (vbasedev->fd < 0) {
devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp);
Expand All @@ -340,7 +342,7 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
/* try to attach to an existing container in this space */
QLIST_FOREACH(bcontainer, &space->containers, next) {
container = container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
if (bcontainer->ops != &vfio_iommufd_ops ||
if (bcontainer->ops != iommufd_vioc ||
vbasedev->iommufd != container->be) {
continue;
}
Expand Down Expand Up @@ -374,7 +376,7 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
container->ioas_id = ioas_id;

bcontainer = &container->bcontainer;
vfio_container_init(bcontainer, space, &vfio_iommufd_ops);
vfio_container_init(bcontainer, space, iommufd_vioc);
QLIST_INSERT_HEAD(&space->containers, bcontainer, next);

ret = iommufd_cdev_attach_container(vbasedev, container, errp);
Expand Down Expand Up @@ -476,9 +478,11 @@ static void iommufd_cdev_detach(VFIODevice *vbasedev)
static VFIODevice *iommufd_cdev_pci_find_by_devid(__u32 devid)
{
VFIODevice *vbasedev_iter;
const VFIOIOMMUClass *iommufd_vioc =
VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));

QLIST_FOREACH(vbasedev_iter, &vfio_device_list, global_next) {
if (vbasedev_iter->bcontainer->ops != &vfio_iommufd_ops) {
if (vbasedev_iter->bcontainer->ops != iommufd_vioc) {
continue;
}
if (devid == vbasedev_iter->devid) {
Expand Down Expand Up @@ -621,10 +625,23 @@ static int iommufd_cdev_pci_hot_reset(VFIODevice *vbasedev, bool single)
return ret;
}

const VFIOIOMMUOps vfio_iommufd_ops = {
.dma_map = iommufd_cdev_map,
.dma_unmap = iommufd_cdev_unmap,
.attach_device = iommufd_cdev_attach,
.detach_device = iommufd_cdev_detach,
.pci_hot_reset = iommufd_cdev_pci_hot_reset,
static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
{
VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);

vioc->dma_map = iommufd_cdev_map;
vioc->dma_unmap = iommufd_cdev_unmap;
vioc->attach_device = iommufd_cdev_attach;
vioc->detach_device = iommufd_cdev_detach;
vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
};

static const TypeInfo types[] = {
{
.name = TYPE_VFIO_IOMMU_IOMMUFD,
.parent = TYPE_VFIO_IOMMU,
.class_init = vfio_iommu_iommufd_class_init,
},
};

DEFINE_TYPES(types)
1 change: 0 additions & 1 deletion include/hw/vfio/vfio-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList;
extern VFIOGroupList vfio_group_list;
extern VFIODeviceList vfio_device_list;
extern const VFIOIOMMUOps vfio_iommufd_ops;
extern const MemoryListener vfio_memory_listener;
extern int vfio_kvm_device_fd;

Expand Down
2 changes: 1 addition & 1 deletion include/hw/vfio/vfio-container-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

typedef struct VFIODevice VFIODevice;
typedef struct VFIOIOMMUClass VFIOIOMMUClass;
#define VFIOIOMMUOps VFIOIOMMUClass /* To remove */

typedef struct {
unsigned long *bitmap;
Expand Down Expand Up @@ -96,6 +95,7 @@ void vfio_container_destroy(VFIOContainerBase *bcontainer);
#define TYPE_VFIO_IOMMU "vfio-iommu"
#define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"
#define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"
#define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd"

/*
* VFIOContainerBase is not an abstract QOM object because it felt
Expand Down

0 comments on commit ce5f6d4

Please sign in to comment.