From: Eric Auger <eric.auger@redhat.com>
To: eric.auger.pro@gmail.com, eric.auger@redhat.com,
peter.maydell@linaro.org, alex.williamson@redhat.com,
mst@redhat.com, qemu-arm@nongnu.org, qemu-devel@nongnu.org,
jean-philippe.brucker@arm.com
Cc: will.deacon@arm.com, kevin.tian@intel.com, marc.zyngier@arm.com,
christoffer.dall@linaro.org, drjones@redhat.com, wei@redhat.com,
tn@semihalf.com, bharat.bhushan@nxp.com, peterx@redhat.com,
linuc.decode@gmail.com
Subject: [Qemu-devel] [RFC v4 06/16] virtio-iommu: Register attached devices
Date: Tue, 19 Sep 2017 09:46:38 +0200 [thread overview]
Message-ID: <1505807208-9063-7-git-send-email-eric.auger@redhat.com> (raw)
In-Reply-To: <1505807208-9063-1-git-send-email-eric.auger@redhat.com>
This patch introduce address space and device internal
datatypes. Both are stored in RB trees. The address space
owns a list of devices attached to it.
It is assumed the devid corresponds to the PCI BDF.
Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
v3 -> v4:
- new separate patch
---
hw/virtio/trace-events | 4 ++
hw/virtio/virtio-iommu.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 124 insertions(+)
diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events
index bc65356..74b92d3 100644
--- a/hw/virtio/trace-events
+++ b/hw/virtio/trace-events
@@ -38,3 +38,7 @@ virtio_iommu_map(uint32_t as, uint64_t phys_addr, uint64_t virt_addr, uint64_t s
virtio_iommu_unmap(uint32_t as, uint64_t virt_addr, uint64_t size) "as= %d virt_addr=0x%"PRIx64" size=0x%"PRIx64
virtio_iommu_translate(const char *name, uint32_t rid, uint64_t iova, int flag) "mr=%s rid=%d addr=0x%"PRIx64" flag=%d"
virtio_iommu_init_iommu_mr(char *iommu_mr) "init %s"
+virtio_iommu_get_dev(uint32_t devid) "Alloc devid=%d"
+virtio_iommu_put_dev(uint32_t devid) "Free devid=%d"
+virtio_iommu_get_as(uint32_t asid) "Alloc asid=%d"
+virtio_iommu_put_as(uint32_t asid) "Free asid=%d"
diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c
index f4cb76f..41a4bbc 100644
--- a/hw/virtio/virtio-iommu.c
+++ b/hw/virtio/virtio-iommu.c
@@ -32,15 +32,116 @@
#include "hw/virtio/virtio-bus.h"
#include "hw/virtio/virtio-access.h"
#include "hw/virtio/virtio-iommu.h"
+#include "hw/pci/pci_bus.h"
+#include "hw/pci/pci.h"
/* Max size */
#define VIOMMU_DEFAULT_QUEUE_SIZE 256
+typedef struct viommu_as {
+ uint32_t id;
+ GTree *mappings;
+ QLIST_HEAD(, viommu_dev) device_list;
+} viommu_as;
+
+typedef struct viommu_dev {
+ uint32_t id;
+ viommu_as *as;
+ QLIST_ENTRY(viommu_dev) next;
+ VirtIOIOMMU *viommu;
+} viommu_dev;
+
+typedef struct viommu_interval {
+ uint64_t low;
+ uint64_t high;
+} viommu_interval;
+
static inline uint16_t virtio_iommu_get_sid(IOMMUDevice *dev)
{
return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn);
}
+static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
+{
+ viommu_interval *inta = (viommu_interval *)a;
+ viommu_interval *intb = (viommu_interval *)b;
+
+ if (inta->high <= intb->low) {
+ return -1;
+ } else if (intb->high <= inta->low) {
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+static void virtio_iommu_detach_dev_from_as(viommu_dev *dev)
+{
+ QLIST_REMOVE(dev, next);
+ dev->as = NULL;
+}
+
+static viommu_dev *virtio_iommu_get_dev(VirtIOIOMMU *s, uint32_t devid)
+{
+ viommu_dev *dev;
+
+ dev = g_tree_lookup(s->devices, GUINT_TO_POINTER(devid));
+ if (dev) {
+ return dev;
+ }
+ dev = g_malloc0(sizeof(*dev));
+ dev->id = devid;
+ dev->viommu = s;
+ trace_virtio_iommu_get_dev(devid);
+ g_tree_insert(s->devices, GUINT_TO_POINTER(devid), dev);
+ return dev;
+}
+
+static void virtio_iommu_put_dev(gpointer data)
+{
+ viommu_dev *dev = (viommu_dev *)data;
+
+ if (dev->as) {
+ virtio_iommu_detach_dev_from_as(dev);
+ g_tree_unref(dev->as->mappings);
+ }
+
+ trace_virtio_iommu_put_dev(dev->id);
+ g_free(dev);
+}
+
+viommu_as *virtio_iommu_get_as(VirtIOIOMMU *s, uint32_t asid);
+viommu_as *virtio_iommu_get_as(VirtIOIOMMU *s, uint32_t asid)
+{
+ viommu_as *as;
+
+ as = g_tree_lookup(s->address_spaces, GUINT_TO_POINTER(asid));
+ if (as) {
+ return as;
+ }
+ as = g_malloc0(sizeof(*as));
+ as->id = asid;
+ as->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
+ NULL, (GDestroyNotify)g_free,
+ (GDestroyNotify)g_free);
+ g_tree_insert(s->address_spaces, GUINT_TO_POINTER(asid), as);
+ trace_virtio_iommu_get_as(asid);
+ return as;
+}
+
+static void virtio_iommu_put_as(gpointer data)
+{
+ viommu_as *as = (viommu_as *)data;
+ viommu_dev *iter, *tmp;
+
+ QLIST_FOREACH_SAFE(iter, &as->device_list, next, tmp) {
+ virtio_iommu_detach_dev_from_as(iter);
+ }
+ g_tree_destroy(as->mappings);
+ trace_virtio_iommu_put_as(as->id);
+ g_free(as);
+}
+
static AddressSpace *virtio_iommu_find_add_as(PCIBus *bus, void *opaque,
int devfn)
{
@@ -70,6 +171,8 @@ static AddressSpace *virtio_iommu_find_add_as(PCIBus *bus, void *opaque,
sdev->bus = bus;
sdev->devfn = devfn;
+ virtio_iommu_get_dev(s, PCI_BUILD_BDF(pci_bus_num(bus), devfn));
+
trace_virtio_iommu_init_iommu_mr(name);
memory_region_init_iommu(&sdev->iommu_mr, sizeof(sdev->iommu_mr),
@@ -360,6 +463,12 @@ static inline guint as_uint64_hash(gconstpointer v)
return (guint)*(const uint64_t *)v;
}
+static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
+{
+ uint ua = GPOINTER_TO_UINT(a);
+ uint ub = GPOINTER_TO_UINT(b);
+ return (ua > ub) - (ua < ub);
+}
static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
{
@@ -375,17 +484,28 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
s->config.page_size_mask = TARGET_PAGE_MASK;
s->config.input_range.end = -1UL;
+ qemu_mutex_init(&s->mutex);
+
memset(s->as_by_bus_num, 0, sizeof(s->as_by_bus_num));
s->as_by_busptr = g_hash_table_new_full(as_uint64_hash,
as_uint64_equal,
g_free, g_free);
+ s->address_spaces = g_tree_new_full((GCompareDataFunc)int_cmp,
+ NULL, NULL, virtio_iommu_put_as);
+ s->devices = g_tree_new_full((GCompareDataFunc)int_cmp,
+ NULL, NULL, virtio_iommu_put_dev);
+
virtio_iommu_init_as(s);
}
static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp)
{
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+ VirtIOIOMMU *s = VIRTIO_IOMMU(dev);
+
+ g_tree_destroy(s->address_spaces);
+ g_tree_destroy(s->devices);
virtio_cleanup(vdev);
}
--
2.5.5
next prev parent reply other threads:[~2017-09-19 7:48 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-19 7:46 [Qemu-devel] [RFC v4 00/16] VIRTIO-IOMMU device Eric Auger
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 01/16] update-linux-headers: import virtio_iommu.h Eric Auger
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 02/16] linux-headers: Update for virtio-iommu Eric Auger
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 03/16] virtio-iommu: add skeleton Eric Auger
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 04/16] virtio-iommu: Decode the command payload Eric Auger
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 05/16] virtio-iommu: Add the iommu regions Eric Auger
2017-09-19 7:46 ` Eric Auger [this message]
2017-09-22 7:29 ` [Qemu-devel] [RFC v4 06/16] virtio-iommu: Register attached devices Bharat Bhushan
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 07/16] virtio-iommu: Implement attach/detach command Eric Auger
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 08/16] virtio-iommu: Implement map/unmap Eric Auger
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 09/16] virtio-iommu: Implement translate Eric Auger
2017-09-22 6:52 ` Bharat Bhushan
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 10/16] virtio-iommu: Implement probe request Eric Auger
2017-09-27 10:53 ` Tomasz Nowicki
2017-09-27 11:00 ` Bharat Bhushan
2017-09-27 15:44 ` Auger Eric
2017-09-27 15:40 ` Auger Eric
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 11/16] hw/arm/virt: Add 2.11 machine type Eric Auger
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 12/16] hw/arm/virt: Add virtio-iommu to the virt board Eric Auger
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 13/16] memory.h: Add set_page_size_mask IOMMUMemoryRegion callback Eric Auger
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 14/16] hw/vfio/common: Set the IOMMUMemoryRegion supported page sizes Eric Auger
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 15/16] virtio-iommu: Implement set_page_size_mask Eric Auger
2017-09-19 7:46 ` [Qemu-devel] [RFC v4 16/16] hw/vfio/common: Do not print error when viommu translates into an mmio region Eric Auger
2017-09-27 11:07 ` [Qemu-devel] [RFC v4 00/16] VIRTIO-IOMMU device Tomasz Nowicki
2017-09-27 15:38 ` Auger Eric
2017-10-11 14:56 ` Peter Maydell
2017-10-11 16:08 ` Auger Eric
2017-10-12 9:54 ` Peter Maydell
2017-10-12 10:09 ` Auger Eric
2017-10-12 10:46 ` Jean-Philippe Brucker
2017-10-13 7:01 ` Tian, Kevin
2017-10-13 7:43 ` Auger Eric
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=1505807208-9063-7-git-send-email-eric.auger@redhat.com \
--to=eric.auger@redhat.com \
--cc=alex.williamson@redhat.com \
--cc=bharat.bhushan@nxp.com \
--cc=christoffer.dall@linaro.org \
--cc=drjones@redhat.com \
--cc=eric.auger.pro@gmail.com \
--cc=jean-philippe.brucker@arm.com \
--cc=kevin.tian@intel.com \
--cc=linuc.decode@gmail.com \
--cc=marc.zyngier@arm.com \
--cc=mst@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=peterx@redhat.com \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=tn@semihalf.com \
--cc=wei@redhat.com \
--cc=will.deacon@arm.com \
/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).