All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexey Kardashevskiy <aik@ozlabs.ru>
To: qemu-devel@nongnu.org
Cc: Alexey Kardashevskiy <aik@ozlabs.ru>,
	qemu-ppc@nongnu.org, Alexander Graf <agraf@suse.de>,
	David Gibson <david@gibson.dropbear.id.au>,
	Alex Williamson <alex.williamson@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: [Qemu-devel] [PATCH qemu v16 17/19] spapr_iommu, vfio, memory: Notify IOMMU about starting/stopping being used by VFIO
Date: Wed,  4 May 2016 16:52:29 +1000	[thread overview]
Message-ID: <1462344751-28281-18-git-send-email-aik@ozlabs.ru> (raw)
In-Reply-To: <1462344751-28281-1-git-send-email-aik@ozlabs.ru>

The sPAPR TCE tables manage 2 copies when VFIO is using an IOMMU -
a guest view of the table and a hardware TCE table. If there is no VFIO
presense in the address space, then just the guest view is used, if
this is the case, it is allocated in the KVM. However since there is no
support yet for VFIO in KVM TCE hypercalls, when we start using VFIO,
we need to move the guest view from KVM to the userspace; and we need
to do this for every IOMMU on a bus with VFIO devices.

This adds vfio_start/vfio_stop callbacks in MemoryRegionIOMMUOps to
notify IOMMU about changing environment so it can reallocate the table
to/from KVM or (when available) hook the IOMMU groups with the logical
bus (LIOBN) in the KVM.

This removes explicit spapr_tce_set_need_vfio() call from PCI hotplug
path as the new callbacks do this better - they notify IOMMU at
the exact moment when the configuration is changed, and this also
includes the case of PCI hot unplug.

This postpones vfio_stop() till the end of region_del() as
vfio_dma_unmap() has to execute before VFIO support is disabled.

As there can be multiple containers attached to the same PHB/LIOBN,
this adds a wrapper with a use counter for every IOMMU MR and
stores them in a list in the VFIOAddressSpace.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
---
Changes:
v16:
* added a use counter in VFIOAddressSpace->VFIOIOMMUMR

v15:
* s/need_vfio/vfio-Users/g
---
 hw/ppc/spapr_iommu.c          | 12 ++++++++++++
 hw/ppc/spapr_pci.c            |  6 ------
 hw/vfio/common.c              | 45 ++++++++++++++++++++++++++++++++++++++++++-
 include/exec/memory.h         |  4 ++++
 include/hw/vfio/vfio-common.h |  7 +++++++
 5 files changed, 67 insertions(+), 7 deletions(-)

diff --git a/hw/ppc/spapr_iommu.c b/hw/ppc/spapr_iommu.c
index c945dba..7af2700 100644
--- a/hw/ppc/spapr_iommu.c
+++ b/hw/ppc/spapr_iommu.c
@@ -155,6 +155,16 @@ static uint64_t spapr_tce_get_page_sizes(MemoryRegion *iommu)
     return 1ULL << tcet->page_shift;
 }
 
+static void spapr_tce_vfio_start(MemoryRegion *iommu)
+{
+    spapr_tce_set_need_vfio(container_of(iommu, sPAPRTCETable, iommu), true);
+}
+
+static void spapr_tce_vfio_stop(MemoryRegion *iommu)
+{
+    spapr_tce_set_need_vfio(container_of(iommu, sPAPRTCETable, iommu), false);
+}
+
 static void spapr_tce_table_do_enable(sPAPRTCETable *tcet);
 static void spapr_tce_table_do_disable(sPAPRTCETable *tcet);
 
@@ -239,6 +249,8 @@ static const VMStateDescription vmstate_spapr_tce_table = {
 static MemoryRegionIOMMUOps spapr_iommu_ops = {
     .translate = spapr_tce_translate_iommu,
     .get_page_sizes = spapr_tce_get_page_sizes,
+    .vfio_start = spapr_tce_vfio_start,
+    .vfio_stop = spapr_tce_vfio_stop,
 };
 
 static int spapr_tce_table_realize(DeviceState *dev)
diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c
index 5b9ccff..51e7d56 100644
--- a/hw/ppc/spapr_pci.c
+++ b/hw/ppc/spapr_pci.c
@@ -1086,12 +1086,6 @@ static void spapr_phb_add_pci_device(sPAPRDRConnector *drc,
     void *fdt = NULL;
     int fdt_start_offset = 0, fdt_size;
 
-    if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
-        sPAPRTCETable *tcet = spapr_tce_find_by_liobn(phb->dma_liobn);
-
-        spapr_tce_set_need_vfio(tcet, true);
-    }
-
     if (dev->hotplugged) {
         fdt = create_device_tree(&fdt_size);
         fdt_start_offset = spapr_create_pci_child_dt(phb, pdev, fdt, 0);
diff --git a/hw/vfio/common.c b/hw/vfio/common.c
index 3f2fb23..03daf88 100644
--- a/hw/vfio/common.c
+++ b/hw/vfio/common.c
@@ -421,6 +421,26 @@ static void vfio_listener_region_add(MemoryListener *listener,
         QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next);
 
         memory_region_register_iommu_notifier(giommu->iommu, &giommu->n);
+
+        if (section->mr->iommu_ops && section->mr->iommu_ops->vfio_start) {
+            VFIOIOMMUMR *iommumr;
+            bool found = false;
+
+            QLIST_FOREACH(iommumr, &container->space->iommumrs, iommumr_next) {
+                if (iommumr->iommu == section->mr) {
+                    found = true;
+                    break;
+                }
+            }
+            if (!found) {
+                iommumr = g_malloc0(sizeof(*iommumr));
+                iommumr->iommu = section->mr;
+                section->mr->iommu_ops->vfio_start(section->mr);
+                QLIST_INSERT_HEAD(&container->space->iommumrs, iommumr,
+                                  iommumr_next);
+            }
+            ++iommumr->users;
+        }
         memory_region_iommu_replay(giommu->iommu, &giommu->n,
                                    false);
 
@@ -470,6 +490,7 @@ static void vfio_listener_region_del(MemoryListener *listener,
     hwaddr iova, end;
     Int128 llend, llsize;
     int ret;
+    MemoryRegion *iommu = NULL;
 
     if (vfio_listener_skipped_section(section)) {
         trace_vfio_listener_region_del_skip(
@@ -490,13 +511,30 @@ static void vfio_listener_region_del(MemoryListener *listener,
 
         QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) {
             if (giommu->iommu == section->mr) {
+                VFIOIOMMUMR *iommumr;
+
                 memory_region_unregister_iommu_notifier(&giommu->n);
+
+                QLIST_FOREACH(iommumr, &container->space->iommumrs,
+                              iommumr_next) {
+                    if (iommumr->iommu != section->mr) {
+                        continue;
+                    }
+                    --iommumr->users;
+                    if (iommumr->users) {
+                        break;
+                    }
+                    QLIST_REMOVE(iommumr, iommumr_next);
+                    g_free(iommumr);
+                    iommu = giommu->iommu;
+                    break;
+                }
+
                 QLIST_REMOVE(giommu, giommu_next);
                 g_free(giommu);
                 break;
             }
         }
-
         /*
          * FIXME: We assume the one big unmap below is adequate to
          * remove any individual page mappings in the IOMMU which
@@ -527,6 +565,10 @@ static void vfio_listener_region_del(MemoryListener *listener,
                      "0x%"HWADDR_PRIx") = %d (%m)",
                      container, iova, int128_get64(llsize), ret);
     }
+
+    if (iommu && iommu->iommu_ops && iommu->iommu_ops->vfio_stop) {
+        iommu->iommu_ops->vfio_stop(section->mr);
+    }
 }
 
 static const MemoryListener vfio_memory_listener = {
@@ -787,6 +829,7 @@ static VFIOAddressSpace *vfio_get_address_space(AddressSpace *as)
     space = g_malloc0(sizeof(*space));
     space->as = as;
     QLIST_INIT(&space->containers);
+    QLIST_INIT(&space->iommumrs);
 
     QLIST_INSERT_HEAD(&vfio_address_spaces, space, list);
 
diff --git a/include/exec/memory.h b/include/exec/memory.h
index a3a1703..52d2c70 100644
--- a/include/exec/memory.h
+++ b/include/exec/memory.h
@@ -151,6 +151,10 @@ struct MemoryRegionIOMMUOps {
     IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write);
     /* Returns supported page sizes */
     uint64_t (*get_page_sizes)(MemoryRegion *iommu);
+    /* Called when VFIO starts using this */
+    void (*vfio_start)(MemoryRegion *iommu);
+    /* Called when VFIO stops using this */
+    void (*vfio_stop)(MemoryRegion *iommu);
 };
 
 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
index 808263b..a9e6e33 100644
--- a/include/hw/vfio/vfio-common.h
+++ b/include/hw/vfio/vfio-common.h
@@ -64,9 +64,16 @@ typedef struct VFIORegion {
 typedef struct VFIOAddressSpace {
     AddressSpace *as;
     QLIST_HEAD(, VFIOContainer) containers;
+    QLIST_HEAD(, VFIOIOMMUMR) iommumrs;
     QLIST_ENTRY(VFIOAddressSpace) list;
 } VFIOAddressSpace;
 
+typedef struct VFIOIOMMUMR {
+    MemoryRegion *iommu;
+    int users;
+    QLIST_ENTRY(VFIOIOMMUMR) iommumr_next;
+} VFIOIOMMUMR;
+
 struct VFIOGroup;
 
 typedef struct VFIOContainer {
-- 
2.5.0.rc3

  parent reply	other threads:[~2016-05-04  6:56 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-04  6:52 [Qemu-devel] [PATCH qemu v16 00/19] spapr: vfio: Enable Dynamic DMA windows (DDW) Alexey Kardashevskiy
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 01/19] vfio: Delay DMA address space listener release Alexey Kardashevskiy
2016-05-05 22:39   ` Alex Williamson
2016-05-13  7:16     ` Alexey Kardashevskiy
2016-05-13 22:24       ` Alex Williamson
2016-05-25  6:34         ` David Gibson
2016-05-25 13:59           ` Alex Williamson
2016-05-26  1:00             ` David Gibson
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 02/19] memory: Call region_del() callbacks on memory listener unregistering Alexey Kardashevskiy
2016-05-05 22:45   ` Alex Williamson
2016-05-26  1:48     ` David Gibson
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 03/19] memory: Fix IOMMU replay base address Alexey Kardashevskiy
2016-05-26  1:50   ` David Gibson
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 04/19] vmstate: Define VARRAY with VMS_ALLOC Alexey Kardashevskiy
2016-05-27  7:54   ` Alexey Kardashevskiy
2016-06-01  2:29     ` Alexey Kardashevskiy
2016-06-01  8:11       ` Paolo Bonzini
2016-06-02  0:43         ` David Gibson
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 05/19] vfio: Check that IOMMU MR translates to system address space Alexey Kardashevskiy
2016-05-26  1:51   ` David Gibson
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 06/19] spapr_pci: Use correct DMA LIOBN when composing the device tree Alexey Kardashevskiy
2016-05-26  3:17   ` David Gibson
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 07/19] spapr_iommu: Move table allocation to helpers Alexey Kardashevskiy
2016-05-26  3:32   ` David Gibson
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 08/19] spapr_iommu: Introduce "enabled" state for TCE table Alexey Kardashevskiy
2016-05-26  3:39   ` David Gibson
2016-05-27  8:01     ` Alexey Kardashevskiy
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 09/19] spapr_iommu: Finish renaming vfio_accel to need_vfio Alexey Kardashevskiy
2016-05-26  3:18   ` David Gibson
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 10/19] spapr_iommu: Migrate full state Alexey Kardashevskiy
2016-05-26  4:01   ` David Gibson
2016-05-31  8:19     ` Alexey Kardashevskiy
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 11/19] spapr_iommu: Add root memory region Alexey Kardashevskiy
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 12/19] spapr_pci: Reset DMA config on PHB reset Alexey Kardashevskiy
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 13/19] memory: Add reporting of supported page sizes Alexey Kardashevskiy
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 14/19] vfio: spapr: Add DMA memory preregistering (SPAPR IOMMU v2) Alexey Kardashevskiy
2016-05-13 22:25   ` Alex Williamson
2016-05-16  1:10     ` Alexey Kardashevskiy
2016-05-16 20:20       ` Alex Williamson
2016-05-26  4:53         ` David Gibson
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 15/19] spapr_pci: Add and export DMA resetting helper Alexey Kardashevskiy
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 16/19] vfio: Add host side DMA window capabilities Alexey Kardashevskiy
2016-05-13 22:25   ` Alex Williamson
2016-05-27  0:36     ` David Gibson
2016-05-04  6:52 ` Alexey Kardashevskiy [this message]
2016-05-13 22:26   ` [Qemu-devel] [PATCH qemu v16 17/19] spapr_iommu, vfio, memory: Notify IOMMU about starting/stopping being used by VFIO Alex Williamson
2016-05-16  8:35     ` Alexey Kardashevskiy
2016-05-16 20:13       ` Alex Williamson
2016-05-20  8:04         ` [Qemu-devel] [RFC PATCH qemu] spapr_iommu, vfio, memory: Notify IOMMU about starting/stopping listening Alexey Kardashevskiy
2016-05-20 15:19           ` Alex Williamson
2016-05-27  0:43           ` David Gibson
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 18/19] vfio/spapr: Create DMA window dynamically (SPAPR IOMMU v2) Alexey Kardashevskiy
2016-05-13 22:26   ` Alex Williamson
2016-05-16  4:52     ` Alexey Kardashevskiy
2016-05-16 20:20       ` Alex Williamson
2016-05-27  0:50         ` David Gibson
2016-05-27  3:49         ` Alexey Kardashevskiy
2016-05-27  4:05           ` David Gibson
2016-05-04  6:52 ` [Qemu-devel] [PATCH qemu v16 19/19] spapr_pci/spapr_pci_vfio: Support Dynamic DMA Windows (DDW) Alexey Kardashevskiy
2016-05-13  8:41   ` Bharata B Rao
2016-05-13  8:49     ` Bharata B Rao
2016-05-16  6:25     ` Alexey Kardashevskiy
2016-05-17  5:32       ` Bharata B Rao
2016-05-27  4:44         ` David Gibson
2016-05-27  5:49           ` Bharata B Rao
2016-06-01  3:32             ` Bharata B Rao
2016-05-27  4:42     ` David Gibson
2016-05-13  4:54 ` [Qemu-devel] [PATCH qemu v16 00/19] spapr: vfio: Enable Dynamic DMA windows (DDW) Alexey Kardashevskiy
2016-05-13  5:36   ` Alex Williamson

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=1462344751-28281-18-git-send-email-aik@ozlabs.ru \
    --to=aik@ozlabs.ru \
    --cc=agraf@suse.de \
    --cc=alex.williamson@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.