From: Eric Auger <eric.auger@redhat.com>
To: eric.auger@redhat.com, eric.auger.pro@gmail.com,
peter.maydell@linaro.org, qemu-arm@nongnu.org,
qemu-devel@nongnu.org, alex.williamson@redhat.com,
pranav.sawargaonkar@gmail.com
Cc: diana.craciun@freescale.com, christoffer.dall@linaro.org,
drjones@redhat.com, Bharat.Bhushan@freescale.com
Subject: [Qemu-devel] [RFC v4 7/8] hw: vfio: common: vfio_prepare_msi_mapping
Date: Thu, 6 Oct 2016 11:41:29 +0000 [thread overview]
Message-ID: <1475754090-22681-8-git-send-email-eric.auger@redhat.com> (raw)
In-Reply-To: <1475754090-22681-1-git-send-email-eric.auger@redhat.com>
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 -> v4:
- initialize err to NULL
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..c773332 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 = NULL;
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 instantiates 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 */
--
1.9.1
next prev parent reply other threads:[~2016-10-06 11:42 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-10-06 11:41 [Qemu-devel] [RFC v4 0/8] KVM PCI/MSI passthrough with mach-virt Eric Auger
2016-10-06 11:41 ` [Qemu-devel] [RFC v4 1/8] linux-headers: Partial update for MSI IOVA handling Eric Auger
2016-10-06 11:41 ` [Qemu-devel] [RFC v4 2/8] hw: vfio: common: vfio_get_iommu_type1_info Eric Auger
2016-10-06 11:41 ` [Qemu-devel] [RFC v4 3/8] hw: vfio: common: Introduce vfio_register_msi_iova Eric Auger
2016-10-06 11:41 ` [Qemu-devel] [RFC v4 4/8] memory: Add reserved_iova region type Eric Auger
2016-10-06 11:41 ` [Qemu-devel] [RFC v4 5/8] memory: memory_region_find_by_name Eric Auger
2016-10-06 11:41 ` [Qemu-devel] [RFC v4 6/8] hw: platform-bus: Enable to map any memory region onto the platform-bus Eric Auger
2016-10-06 11:41 ` Eric Auger [this message]
2016-10-06 11:41 ` [Qemu-devel] [RFC v4 8/8] hw: vfio: common: Adapt vfio_listeners for reserved_iova region Eric Auger
2016-10-10 16:16 ` [Qemu-devel] [RFC v4 0/8] KVM PCI/MSI passthrough with mach-virt no-reply
2016-10-10 21:11 ` no-reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1475754090-22681-8-git-send-email-eric.auger@redhat.com \
--to=eric.auger@redhat.com \
--cc=Bharat.Bhushan@freescale.com \
--cc=alex.williamson@redhat.com \
--cc=christoffer.dall@linaro.org \
--cc=diana.craciun@freescale.com \
--cc=drjones@redhat.com \
--cc=eric.auger.pro@gmail.com \
--cc=peter.maydell@linaro.org \
--cc=pranav.sawargaonkar@gmail.com \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).