Message ID | 1475747444-12552-8-git-send-email-eric.auger@redhat.com |
---|---|
State | New |
Headers | show |
Hi, On 06/10/2016 11:50, Eric Auger wrote: > Introduce an helper function to retrieve the iommu type1 capability > chain info. > > The first capability ready to be exploited is the msi geometry > capability. vfio_prepare_msi_mapping allocates a MemoryRegion > dedicated to host MSI IOVA mapping. Its size matches the host needs. > This region is mapped on guest side on the platform bus memory container. > > Signed-off-by: Eric Auger <eric.auger@redhat.com> > > --- > > v3: creation > --- > hw/vfio/common.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 74 insertions(+) > > diff --git a/hw/vfio/common.c b/hw/vfio/common.c > index fe8a855..7d20c33 100644 > --- a/hw/vfio/common.c > +++ b/hw/vfio/common.c > @@ -34,6 +34,8 @@ > #include "qemu/range.h" > #include "sysemu/kvm.h" > #include "trace.h" > +#include "hw/platform-bus.h" > +#include "qapi/error.h" > > struct vfio_group_head vfio_group_list = > QLIST_HEAD_INITIALIZER(vfio_group_list); > @@ -948,12 +950,76 @@ retry: > return 0; > } > > +static struct vfio_info_cap_header * > +vfio_get_iommu_type1_info_cap(struct vfio_iommu_type1_info *info, uint16_t id) > +{ > + struct vfio_info_cap_header *hdr; > + void *ptr = info; > + > + if (!(info->flags & VFIO_IOMMU_INFO_CAPS)) { > + return NULL; > + } > + > + for (hdr = ptr + info->cap_offset; hdr != ptr; hdr = ptr + hdr->next) { > + if (hdr->id == id) { > + return hdr; > + } > + } > + return NULL; > +} > + > +static void vfio_prepare_msi_mapping(struct vfio_iommu_type1_info *info, > + AddressSpace *as, Error **errp) > +{ > + struct vfio_iommu_type1_info_cap_msi_geometry *msi_geometry; > + MemoryRegion *pbus_region, *reserved_reg; > + struct vfio_info_cap_header *hdr; > + PlatformBusDevice *pbus; > + > + hdr = vfio_get_iommu_type1_info_cap(info, > + VFIO_IOMMU_TYPE1_INFO_CAP_MSI_GEOMETRY); > + if (!hdr) { > + return; > + } > + > + msi_geometry = container_of(hdr, > + struct vfio_iommu_type1_info_cap_msi_geometry, > + header); > + > + if (msi_geometry->flags & VFIO_IOMMU_MSI_GEOMETRY_RESERVED) { > + return; > + } > + > + /* > + * MSI must be iommu mapped: allocate a GPA region located on the > + * platform bus that the host will be able to use for MSI IOVA allocation > + */ > + reserved_reg = memory_region_find_by_name(as->root, "reserved-iova"); > + if (reserved_reg) { > + memory_region_unref(reserved_reg); > + return; > + } > + > + pbus_region = memory_region_find_by_name(as->root, "platform bus"); > + if (!pbus_region) { > + error_setg(errp, "no platform bus memory container found"); > + return; > + } > + pbus = container_of(pbus_region, PlatformBusDevice, mmio); > + reserved_reg = g_new0(MemoryRegion, 1); > + memory_region_init_reserved_iova(reserved_reg, OBJECT(pbus), > + "reserved-iova", > + msi_geometry->size, &error_fatal); > + platform_bus_map_region(pbus, reserved_reg); > + memory_region_unref(pbus_region); > +} > > static int vfio_connect_container(VFIOGroup *group, AddressSpace *as) > { > VFIOContainer *container; > int ret, fd; > VFIOAddressSpace *space; > + Error *err; A last minute change related to future vfio realize migration that I forgot to test. err needs to be initialized to NULL here. Will re-post. Sorry for the inconvience. Thanks Eric > > space = vfio_get_address_space(as); > > @@ -1011,6 +1077,14 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as) > * going to actually try in practice. > */ > vfio_get_iommu_type1_info(fd, &pinfo); > + vfio_prepare_msi_mapping(pinfo, as, &err); > + if (err) { > + error_append_hint(&err, > + "Make sure your machine instantiate a platform bus\n"); > + error_report_err(err); > + goto free_container_exit; > + } > + > /* Ignore errors */ > if (ret || !(pinfo->flags & VFIO_IOMMU_INFO_PGSIZES)) { > /* Assume 4k IOVA page size */ >
diff --git a/hw/vfio/common.c b/hw/vfio/common.c index fe8a855..7d20c33 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -34,6 +34,8 @@ #include "qemu/range.h" #include "sysemu/kvm.h" #include "trace.h" +#include "hw/platform-bus.h" +#include "qapi/error.h" struct vfio_group_head vfio_group_list = QLIST_HEAD_INITIALIZER(vfio_group_list); @@ -948,12 +950,76 @@ retry: return 0; } +static struct vfio_info_cap_header * +vfio_get_iommu_type1_info_cap(struct vfio_iommu_type1_info *info, uint16_t id) +{ + struct vfio_info_cap_header *hdr; + void *ptr = info; + + if (!(info->flags & VFIO_IOMMU_INFO_CAPS)) { + return NULL; + } + + for (hdr = ptr + info->cap_offset; hdr != ptr; hdr = ptr + hdr->next) { + if (hdr->id == id) { + return hdr; + } + } + return NULL; +} + +static void vfio_prepare_msi_mapping(struct vfio_iommu_type1_info *info, + AddressSpace *as, Error **errp) +{ + struct vfio_iommu_type1_info_cap_msi_geometry *msi_geometry; + MemoryRegion *pbus_region, *reserved_reg; + struct vfio_info_cap_header *hdr; + PlatformBusDevice *pbus; + + hdr = vfio_get_iommu_type1_info_cap(info, + VFIO_IOMMU_TYPE1_INFO_CAP_MSI_GEOMETRY); + if (!hdr) { + return; + } + + msi_geometry = container_of(hdr, + struct vfio_iommu_type1_info_cap_msi_geometry, + header); + + if (msi_geometry->flags & VFIO_IOMMU_MSI_GEOMETRY_RESERVED) { + return; + } + + /* + * MSI must be iommu mapped: allocate a GPA region located on the + * platform bus that the host will be able to use for MSI IOVA allocation + */ + reserved_reg = memory_region_find_by_name(as->root, "reserved-iova"); + if (reserved_reg) { + memory_region_unref(reserved_reg); + return; + } + + pbus_region = memory_region_find_by_name(as->root, "platform bus"); + if (!pbus_region) { + error_setg(errp, "no platform bus memory container found"); + return; + } + pbus = container_of(pbus_region, PlatformBusDevice, mmio); + reserved_reg = g_new0(MemoryRegion, 1); + memory_region_init_reserved_iova(reserved_reg, OBJECT(pbus), + "reserved-iova", + msi_geometry->size, &error_fatal); + platform_bus_map_region(pbus, reserved_reg); + memory_region_unref(pbus_region); +} static int vfio_connect_container(VFIOGroup *group, AddressSpace *as) { VFIOContainer *container; int ret, fd; VFIOAddressSpace *space; + Error *err; space = vfio_get_address_space(as); @@ -1011,6 +1077,14 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as) * going to actually try in practice. */ vfio_get_iommu_type1_info(fd, &pinfo); + vfio_prepare_msi_mapping(pinfo, as, &err); + if (err) { + error_append_hint(&err, + "Make sure your machine instantiate a platform bus\n"); + error_report_err(err); + goto free_container_exit; + } + /* Ignore errors */ if (ret || !(pinfo->flags & VFIO_IOMMU_INFO_PGSIZES)) { /* Assume 4k IOVA page size */
Introduce an helper function to retrieve the iommu type1 capability chain info. The first capability ready to be exploited is the msi geometry capability. vfio_prepare_msi_mapping allocates a MemoryRegion dedicated to host MSI IOVA mapping. Its size matches the host needs. This region is mapped on guest side on the platform bus memory container. Signed-off-by: Eric Auger <eric.auger@redhat.com> --- v3: creation --- hw/vfio/common.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+)