From nobody Wed Dec 17 21:46:25 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1655310005; cv=none; d=zohomail.com; s=zohoarc; b=F3Gect8MWx3iqalX27zr5fAJCOxSKpSmPe9DAyGyV7/6Z6UVfE75B/0I6R9gQ+SpAuVklB3ACexCf74FpQ3sX+ogLjS4M2IjX6E5cKzF7FDi7qZo6wnFD0Dc3LAAV7OC+Bh6c7vbyrc0Vnqesdc977ILRhlKRoSaueQBYPu7gnI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655310005; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=oI1hNwZfXybxeQev35wgePDxNDqDRiY/UsjPHZD66z0=; b=cx7w6GeB7/XQz3YrAbgaQwh8eKgEph50SOTbDWFvIjWhvkxoSJRgGg0Udx3sLvrpx0+fbF5zNFZM1lePZoV9jPWgUFZvOEtgyqf4vphyxRMSlR5OYK/cn9X44npK1QeVIrQftyRDE0VYoij0Cq1eQSZDkvMGgf7HQlmsrRZoYbw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 165531000575447.195249817734634; Wed, 15 Jun 2022 09:20:05 -0700 (PDT) Received: from localhost ([::1]:53110 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o1VkS-0001AV-N4 for importer@patchew.org; Wed, 15 Jun 2022 12:20:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43360) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o1VLs-0004FP-WE for qemu-devel@nongnu.org; Wed, 15 Jun 2022 11:54:41 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:39664) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o1VLq-0007MZ-MA for qemu-devel@nongnu.org; Wed, 15 Jun 2022 11:54:40 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-461-7--9q-oXMz2UfFLE1ReotA-1; Wed, 15 Jun 2022 11:54:34 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 496421DC2425; Wed, 15 Jun 2022 15:54:33 +0000 (UTC) Received: from localhost (unknown [10.39.192.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id D7AF5111F5; Wed, 15 Jun 2022 15:54:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655308477; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oI1hNwZfXybxeQev35wgePDxNDqDRiY/UsjPHZD66z0=; b=RW7mzWDK6WYnCRukwem46l2sSNREFkfPLwdz8Od+BNP1kik9997aHW3FLe/56m1yw45c8V M++G2bJ1Y49OrIjNjByKiiA7iagNnWyVqvFzYDFQiujcOUirOUDwuPpK5XMf/ck3FadAEA lfGFOGKOrFIzykDel7TRH49IYV/4gI4= X-MC-Unique: 7--9q-oXMz2UfFLE1ReotA-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Thomas Huth , Jagannathan Raman , Aarushi Mehta , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Elena Ufimtseva , Marcel Apfelbaum , Stefan Hajnoczi , Laurent Vivier , , "Dr. David Alan Gilbert" , Richard Henderson , virtio-fs@redhat.com, Hanna Reitz , David Hildenbrand , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Eric Blake , Kevin Wolf , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Beraldo Leal , Peter Xu , Eduardo Habkost , Qiuhao Li , Paolo Bonzini , Markus Armbruster , Bandan Das , "Michael S. Tsirkin" , Stefano Garzarella , Alexander Bulekov , Julia Suvorova , Darren Kenny , Wainer dos Santos Moschetta , John G Johnson Subject: [PULL 13/18] vfio-user: handle DMA mappings Date: Wed, 15 Jun 2022 16:51:24 +0100 Message-Id: <20220615155129.1025811-14-stefanha@redhat.com> In-Reply-To: <20220615155129.1025811-1-stefanha@redhat.com> References: <20220615155129.1025811-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1655310007790100001 Content-Type: text/plain; charset="utf-8" From: Jagannathan Raman Define and register callbacks to manage the RAM regions used for device DMA Signed-off-by: Elena Ufimtseva Signed-off-by: John G Johnson Signed-off-by: Jagannathan Raman Reviewed-by: Stefan Hajnoczi Message-id: faacbcd45c4d02c591f0dbfdc19041fbb3eae7eb.1655151679.git.jag.ram= an@oracle.com Signed-off-by: Stefan Hajnoczi --- 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" =20 static void remote_machine_init(MachineState *machine) { @@ -51,6 +52,10 @@ static void remote_machine_init(MachineState *machine) =20 pci_host =3D PCI_HOST_BRIDGE(rem_host); =20 + if (s->vfio_user) { + remote_iommu_setup(pci_host->bus); + } + remote_iohub_init(&s->iohub); =20 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 cef473cb98..7b21f77052 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_ct= x, char * const buf, return count; } =20 +static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info) +{ + VfuObject *o =3D vfu_get_private(vfu_ctx); + AddressSpace *dma_as =3D NULL; + MemoryRegion *subregion =3D NULL; + g_autofree char *name =3D NULL; + struct iovec *iov =3D &info->iova; + + if (!info->vaddr) { + return; + } + + name =3D g_strdup_printf("mem-%s-%"PRIx64"", o->device, + (uint64_t)info->vaddr); + + subregion =3D g_new0(MemoryRegion, 1); + + memory_region_init_ram_ptr(subregion, NULL, name, + iov->iov_len, info->vaddr); + + dma_as =3D pci_device_iommu_address_space(o->pci_dev); + + memory_region_add_subregion(dma_as->root, (hwaddr)iov->iov_base, subre= gion); + + 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 =3D vfu_get_private(vfu_ctx); + AddressSpace *dma_as =3D NULL; + MemoryRegion *mr =3D NULL; + ram_addr_t offset; + + mr =3D memory_region_from_host(info->vaddr, &offset); + if (!mr) { + return; + } + + dma_as =3D 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; } =20 + ret =3D vfu_setup_device_dma(o->vfu_ctx, &dma_register, &dma_unregiste= r); + if (ret < 0) { + error_setg(errp, "vfu: Failed to setup DMA handlers for %s", + o->device); + goto fail; + } + ret =3D 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 t= o 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%"PRIx6= 4", %zu bytes" +vfu_dma_unregister(uint64_t gpa) "vfu: unregistering GPA 0x%"PRIx64"" --=20 2.36.1