Add 'group' properties to 'vfio-pci' device. This allows
to add vfio-pci device using precreated vfio container and group, e.g.
-object vfio-container,id=ct,fd=5 \
-object vfio-group,id=grp,fd=6,container=ct \
-device vfio-pci,host=05:00.0,group=grp
Signed-off-by: Andrey Ryabinin <arbn@yandex-team.com>
---
hw/vfio/ap.c | 2 +-
hw/vfio/ccw.c | 2 +-
hw/vfio/common.c | 45 ++++++++++++++++++++---------------
hw/vfio/pci.c | 10 +++++++-
hw/vfio/platform.c | 2 +-
include/hw/vfio/vfio-common.h | 2 +-
6 files changed, 39 insertions(+), 24 deletions(-)
diff --git a/hw/vfio/ap.c b/hw/vfio/ap.c
index e0dd561e85a..2a5c322883b 100644
--- a/hw/vfio/ap.c
+++ b/hw/vfio/ap.c
@@ -81,7 +81,7 @@ static VFIOGroup *vfio_ap_get_group(VFIOAPDevice *vapdev, Error **errp)
g_free(group_path);
- return vfio_get_group(groupid, &address_space_memory, errp);
+ return vfio_get_group(&vapdev->vdev, groupid, &address_space_memory, errp);
}
static void vfio_ap_realize(DeviceState *dev, Error **errp)
diff --git a/hw/vfio/ccw.c b/hw/vfio/ccw.c
index 0354737666a..500f0f62163 100644
--- a/hw/vfio/ccw.c
+++ b/hw/vfio/ccw.c
@@ -650,7 +650,7 @@ static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp)
return NULL;
}
- return vfio_get_group(groupid, &address_space_memory, errp);
+ return vfio_get_group(NULL, groupid, &address_space_memory, errp);
}
static void vfio_ccw_realize(DeviceState *dev, Error **errp)
diff --git a/hw/vfio/common.c b/hw/vfio/common.c
index 95722ecf96a..64ace47822d 100644
--- a/hw/vfio/common.c
+++ b/hw/vfio/common.c
@@ -2271,30 +2271,35 @@ static void vfio_disconnect_container(VFIOGroup *group)
object_unref(OBJECT(container));
}
-VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp)
+VFIOGroup *vfio_get_group(VFIODevice *vdev, int groupid, AddressSpace *as, Error **errp)
{
VFIOGroup *group;
struct vfio_group_status status = { .argsz = sizeof(status) };
- QLIST_FOREACH(group, &vfio_group_list, next) {
- if (group->groupid == groupid) {
- /* Found it. Now is it already in the right context? */
- if (group->container->space->as == as) {
- return group;
- } else {
- error_setg(errp, "group %d used in multiple address spaces",
- group->groupid);
- return NULL;
+ if (!vdev->group) {
+ QLIST_FOREACH(group, &vfio_group_list, next) {
+ if (group->groupid == groupid) {
+ /* Found it. Now is it already in the right context? */
+ if (group->container->space->as == as) {
+ return group;
+ } else {
+ error_setg(errp, "group %d used in multiple address spaces",
+ group->groupid);
+ return NULL;
+ }
}
}
- }
-
- group = VFIO_GROUP(object_new(TYPE_VFIO_GROUP));
- object_property_set_int(OBJECT(group), "groupid", groupid, errp);
- user_creatable_complete(USER_CREATABLE(group), errp);
- if (*errp) {
- object_unref(OBJECT(group));
- return NULL;
+ group = VFIO_GROUP(object_new(TYPE_VFIO_GROUP));
+ object_property_set_int(OBJECT(group), "groupid", groupid, errp);
+ user_creatable_complete(USER_CREATABLE(group), errp);
+ if (*errp) {
+ object_unref(OBJECT(group));
+ return NULL;
+ }
+ } else {
+ group = vdev->group;
+ group->groupid = groupid;
+ object_ref(OBJECT(group));
}
if (vfio_connect_container(group, as, errp)) {
@@ -2388,7 +2393,9 @@ void vfio_put_base_device(VFIODevice *vbasedev)
if (!vbasedev->group) {
return;
}
- QLIST_REMOVE(vbasedev, next);
+ if (!QLIST_EMPTY(&vbasedev->group->device_list)) {
+ QLIST_REMOVE(vbasedev, next);
+ }
vbasedev->group = NULL;
trace_vfio_put_base_device(vbasedev->fd);
close(vbasedev->fd);
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 939dcc3d4a9..d671eb74881 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -29,6 +29,7 @@
#include "hw/qdev-properties.h"
#include "hw/qdev-properties-system.h"
#include "migration/vmstate.h"
+#include "monitor/monitor.h"
#include "qapi/qmp/qdict.h"
#include "qemu/error-report.h"
#include "qemu/main-loop.h"
@@ -2902,7 +2903,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
trace_vfio_realize(vbasedev->name, groupid);
- group = vfio_get_group(groupid, pci_device_iommu_address_space(pdev), errp);
+ group = vfio_get_group(&vdev->vbasedev, groupid,
+ pci_device_iommu_address_space(pdev), errp);
if (!group) {
goto error;
}
@@ -3190,6 +3192,7 @@ static void vfio_instance_finalize(Object *obj)
static void vfio_exitfn(PCIDevice *pdev)
{
VFIOPCIDevice *vdev = VFIO_PCI(pdev);
+ VFIOGroup *group = vdev->vbasedev.group;
vfio_unregister_req_notifier(vdev);
vfio_unregister_err_notifier(vdev);
@@ -3204,6 +3207,8 @@ static void vfio_exitfn(PCIDevice *pdev)
vfio_teardown_msi(vdev);
vfio_bars_exit(vdev);
vfio_migration_finalize(&vdev->vbasedev);
+
+ vfio_put_group(group);
}
static void vfio_pci_reset(DeviceState *dev)
@@ -3330,6 +3335,9 @@ static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
pdc->exit = vfio_exitfn;
pdc->config_read = vfio_pci_read_config;
pdc->config_write = vfio_pci_write_config;
+ object_class_property_add_link(klass, "group", TYPE_VFIO_GROUP,
+ offsetof(VFIOPCIDevice, vbasedev.group),
+ object_property_allow_set_link, 0);
}
static const TypeInfo vfio_pci_dev_info = {
diff --git a/hw/vfio/platform.c b/hw/vfio/platform.c
index 5af73f92876..3a72e085026 100644
--- a/hw/vfio/platform.c
+++ b/hw/vfio/platform.c
@@ -577,7 +577,7 @@ static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp)
trace_vfio_platform_base_device_init(vbasedev->name, groupid);
- group = vfio_get_group(groupid, &address_space_memory, errp);
+ group = vfio_get_group(vbasedev, groupid, &address_space_memory, errp);
if (!group) {
return -ENOENT;
}
diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
index f2d67093f44..79b43eb76b3 100644
--- a/include/hw/vfio/vfio-common.h
+++ b/include/hw/vfio/vfio-common.h
@@ -216,7 +216,7 @@ void vfio_region_unmap(VFIORegion *region);
void vfio_region_exit(VFIORegion *region);
void vfio_region_finalize(VFIORegion *region);
void vfio_reset_handler(void *opaque);
-VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);
+VFIOGroup *vfio_get_group(VFIODevice *vdev, int groupid, AddressSpace *as, Error **errp);
void vfio_put_group(VFIOGroup *group);
int vfio_get_device(VFIOGroup *group, const char *name,
VFIODevice *vbasedev, Error **errp);
--
2.37.3