All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jagannathan Raman <jag.raman@oracle.com>
To: qemu-devel@nongnu.org
Cc: stefanha@redhat.com, mst@redhat.com, f4bug@amsat.org,
	pbonzini@redhat.com,  marcandre.lureau@redhat.com,
	thuth@redhat.com, bleal@redhat.com, berrange@redhat.com,
	peter.maydell@linaro.org, eduardo@habkost.net,
	marcel.apfelbaum@gmail.com, eblake@redhat.com, armbru@redhat.com,
	quintela@redhat.com, dgilbert@redhat.com, imammedo@redhat.com,
	peterx@redhat.com, john.levon@nutanix.com,
	thanos.makatos@nutanix.com, elena.ufimtseva@oracle.com,
	john.g.johnson@oracle.com, kanth.ghatraju@oracle.com,
	jag.raman@oracle.com
Subject: [PATCH v10 11/14] vfio-user: handle DMA mappings
Date: Tue, 24 May 2022 11:30:30 -0400	[thread overview]
Message-ID: <52415ba6025ed886828cd49744ed833c121e58ff.1653404595.git.jag.raman@oracle.com> (raw)
In-Reply-To: <cover.1653404595.git.jag.raman@oracle.com>

Define and register callbacks to manage the RAM regions used for
device DMA

Signed-off-by: Elena Ufimtseva <elena.ufimtseva@oracle.com>
Signed-off-by: John G Johnson <john.g.johnson@oracle.com>
Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 hw/remote/machine.c       |  5 ++++
 hw/remote/vfio-user-obj.c | 55 +++++++++++++++++++++++++++++++++++++++
 hw/remote/trace-events    |  2 ++
 3 files changed, 62 insertions(+)

diff --git a/hw/remote/machine.c b/hw/remote/machine.c
index cbb2add291..645b54343d 100644
--- a/hw/remote/machine.c
+++ b/hw/remote/machine.c
@@ -22,6 +22,7 @@
 #include "hw/remote/iohub.h"
 #include "hw/remote/iommu.h"
 #include "hw/qdev-core.h"
+#include "hw/remote/iommu.h"
 
 static void remote_machine_init(MachineState *machine)
 {
@@ -51,6 +52,10 @@ static void remote_machine_init(MachineState *machine)
 
     pci_host = PCI_HOST_BRIDGE(rem_host);
 
+    if (s->vfio_user) {
+        remote_iommu_setup(pci_host->bus);
+    }
+
     remote_iohub_init(&s->iohub);
 
     pci_bus_irqs(pci_host->bus, remote_iohub_set_irq, remote_iohub_map_irq,
diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c
index 575bd47397..8d208f1294 100644
--- a/hw/remote/vfio-user-obj.c
+++ b/hw/remote/vfio-user-obj.c
@@ -284,6 +284,54 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx, char * const buf,
     return count;
 }
 
+static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info)
+{
+    VfuObject *o = vfu_get_private(vfu_ctx);
+    AddressSpace *dma_as = NULL;
+    MemoryRegion *subregion = NULL;
+    g_autofree char *name = NULL;
+    struct iovec *iov = &info->iova;
+
+    if (!info->vaddr) {
+        return;
+    }
+
+    name = g_strdup_printf("mem-%s-%"PRIx64"", o->device,
+                           (uint64_t)info->vaddr);
+
+    subregion = g_new0(MemoryRegion, 1);
+
+    memory_region_init_ram_ptr(subregion, NULL, name,
+                               iov->iov_len, info->vaddr);
+
+    dma_as = pci_device_iommu_address_space(o->pci_dev);
+
+    memory_region_add_subregion(dma_as->root, (hwaddr)iov->iov_base, subregion);
+
+    trace_vfu_dma_register((uint64_t)iov->iov_base, iov->iov_len);
+}
+
+static void dma_unregister(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info)
+{
+    VfuObject *o = vfu_get_private(vfu_ctx);
+    AddressSpace *dma_as = NULL;
+    MemoryRegion *mr = NULL;
+    ram_addr_t offset;
+
+    mr = memory_region_from_host(info->vaddr, &offset);
+    if (!mr) {
+        return;
+    }
+
+    dma_as = pci_device_iommu_address_space(o->pci_dev);
+
+    memory_region_del_subregion(dma_as->root, mr);
+
+    object_unparent((OBJECT(mr)));
+
+    trace_vfu_dma_unregister((uint64_t)info->iova.iov_base);
+}
+
 /*
  * TYPE_VFU_OBJECT depends on the availability of the 'socket' and 'device'
  * properties. It also depends on devices instantiated in QEMU. These
@@ -387,6 +435,13 @@ static void vfu_object_init_ctx(VfuObject *o, Error **errp)
         goto fail;
     }
 
+    ret = vfu_setup_device_dma(o->vfu_ctx, &dma_register, &dma_unregister);
+    if (ret < 0) {
+        error_setg(errp, "vfu: Failed to setup DMA handlers for %s",
+                   o->device);
+        goto fail;
+    }
+
     ret = vfu_realize_ctx(o->vfu_ctx);
     if (ret < 0) {
         error_setg(errp, "vfu: Failed to realize device %s- %s",
diff --git a/hw/remote/trace-events b/hw/remote/trace-events
index 2ef7884346..f945c7e33b 100644
--- a/hw/remote/trace-events
+++ b/hw/remote/trace-events
@@ -7,3 +7,5 @@ mpqemu_recv_io_error(int cmd, int size, int nfds) "failed to receive %d size %d,
 vfu_prop(const char *prop, const char *val) "vfu: setting %s as %s"
 vfu_cfg_read(uint32_t offset, uint32_t val) "vfu: cfg: 0x%u -> 0x%x"
 vfu_cfg_write(uint32_t offset, uint32_t val) "vfu: cfg: 0x%u <- 0x%x"
+vfu_dma_register(uint64_t gpa, size_t len) "vfu: registering GPA 0x%"PRIx64", %zu bytes"
+vfu_dma_unregister(uint64_t gpa) "vfu: unregistering GPA 0x%"PRIx64""
-- 
2.20.1



  parent reply	other threads:[~2022-05-24 15:37 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-24 15:30 [PATCH v10 00/14] vfio-user server in QEMU Jagannathan Raman
2022-05-24 15:30 ` [PATCH v10 01/14] qdev: unplug blocker for devices Jagannathan Raman
2022-05-24 15:30 ` [PATCH v10 02/14] remote/machine: add HotplugHandler for remote machine Jagannathan Raman
2022-05-24 15:30 ` [PATCH v10 03/14] remote/machine: add vfio-user property Jagannathan Raman
2022-05-24 15:30 ` [PATCH v10 04/14] vfio-user: build library Jagannathan Raman
2022-05-25 14:46   ` Stefan Hajnoczi
2022-05-24 15:30 ` [PATCH v10 05/14] vfio-user: define vfio-user-server object Jagannathan Raman
2022-05-24 15:30 ` [PATCH v10 06/14] vfio-user: instantiate vfio-user context Jagannathan Raman
2022-05-24 15:30 ` [PATCH v10 07/14] vfio-user: find and init PCI device Jagannathan Raman
2022-05-24 15:30 ` [PATCH v10 08/14] vfio-user: run vfio-user context Jagannathan Raman
2022-05-24 15:30 ` [PATCH v10 09/14] vfio-user: handle PCI config space accesses Jagannathan Raman
2022-05-24 15:30 ` [PATCH v10 10/14] vfio-user: IOMMU support for remote device Jagannathan Raman
2022-05-24 15:30 ` Jagannathan Raman [this message]
2022-05-24 15:30 ` [PATCH v10 12/14] vfio-user: handle PCI BAR accesses Jagannathan Raman
2022-05-24 15:30 ` [PATCH v10 13/14] vfio-user: handle device interrupts Jagannathan Raman
2022-05-25 14:53   ` Stefan Hajnoczi
2022-05-31 15:01     ` Jag Raman
2022-05-31 20:10       ` Alex Williamson
2022-05-31 21:03         ` Stefan Hajnoczi
2022-05-31 21:45           ` Alex Williamson
2022-06-01  6:37             ` John Johnson
2022-06-01  9:38               ` Stefan Hajnoczi
2022-06-01 17:00             ` Jag Raman
2022-06-01 17:26               ` Alex Williamson
2022-06-01 18:01                 ` Jag Raman
2022-06-01 18:30                   ` Alex Williamson
2022-06-01 19:38                     ` Jag Raman
2022-06-03 12:20                 ` John Johnson
2022-06-06 18:32   ` Alexander Duyck
2022-06-06 19:29     ` Jag Raman
2022-06-06 20:38       ` Alexander Duyck
2022-05-24 15:30 ` [PATCH v10 14/14] vfio-user: handle reset of remote device Jagannathan Raman
2022-05-25 14:55 ` [PATCH v10 00/14] vfio-user server in QEMU Stefan Hajnoczi

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=52415ba6025ed886828cd49744ed833c121e58ff.1653404595.git.jag.raman@oracle.com \
    --to=jag.raman@oracle.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=bleal@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=elena.ufimtseva@oracle.com \
    --cc=f4bug@amsat.org \
    --cc=imammedo@redhat.com \
    --cc=john.g.johnson@oracle.com \
    --cc=john.levon@nutanix.com \
    --cc=kanth.ghatraju@oracle.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=stefanha@redhat.com \
    --cc=thanos.makatos@nutanix.com \
    --cc=thuth@redhat.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 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.