From nobody Mon Sep 8 11:56:40 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1756878555; cv=none; d=zohomail.com; s=zohoarc; b=NOIZViEed+66W9/2HYaqu7ULLDOYyIqswILT7bv7eOZoTLalIlJvrf8RVuisn0Z/aYS7gh8PDk9pfK/EwB9Esc9uSOLmqv1C642tZpp0RdLnNQB6KLYfpuL1wfxzwOwbI9VMDVDmmYp1EglwAgAIYQNfnqkLQ/dpi4dcJDJy/VI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756878555; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=A7OaSXRoS5SqaFf3VFHjqMYcEYyBn0glsJIRdETEUxk=; b=dSNoF634GTkz+UhjZGH8dwUG92qAhJQbR7iib88IxhHiXfjK49kPd/FyhzJPgwjidW6qmOnhayOYbpnxomdt3TBl5oCblEXIx2FvrUY1RDFWXuwzPjd5IP/foIXXnFAabgr/z9nBv6yCTB57qotF2X2y8Op/C0v1MUc1jk3AKFM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; 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 1756878555247216.04944755275437; Tue, 2 Sep 2025 22:49:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1utgL7-0005Tg-E7; Wed, 03 Sep 2025 01:47:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1utgL0-0005Rm-GG for qemu-devel@nongnu.org; Wed, 03 Sep 2025 01:47:18 -0400 Received: from mgamail.intel.com ([198.175.65.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1utgKy-00020Q-P0 for qemu-devel@nongnu.org; Wed, 03 Sep 2025 01:47:18 -0400 Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Sep 2025 22:47:09 -0700 Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Sep 2025 22:47:08 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1756878437; x=1788414437; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=L8wsa5EiKVsZTTAjXcSAQwAl5L2jNrbI/NYMZNyKHtE=; b=F467Gd/JI0PyGyk7OsGoGxsAtrx9E+9hJlnoSSJDX1IZS8YK6Msd6Ozn SVHS6FRQY2TCUEZup+hGCM0Mi07Fj08WnDEJzNqBIbIIBsOMK9aGHVwpK TvUlYTWvhg8HRmKURV6ZNvBE2bZEvbR/0rznZAJpOVhB8Q5ICyU3h3P7S sPX2fAa3KRte6hc98PNBSU1DMLq3u2sj/7x82Jy+sSOAMtUj+GHjj0X30 uAGX1D0V8EkAzIu21DI4MAZd5fxSZLq9HGZZ1DnxoGLzUzLXrvV2zwItu tK8smSdhP1Tn4DdRV9Ur1Hy7zSmepTBdGKapSdE7epJ/Sv52YyDoJcWYX A==; X-CSE-ConnectionGUID: 7M9uiSF5QLOxpLqZFLBO2g== X-CSE-MsgGUID: ZcrgeHNQQ4CehCl04/GTrA== X-IronPort-AV: E=McAfee;i="6800,10657,11541"; a="58878187" X-IronPort-AV: E=Sophos;i="6.18,233,1751266800"; d="scan'208";a="58878187" X-CSE-ConnectionGUID: m6/wSRLwQ4KmS0uOH7X2dg== X-CSE-MsgGUID: y9dLOmXjTb2ChRg4VaroMg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.18,233,1751266800"; d="scan'208";a="175634765" From: Vivek Kasireddy To: qemu-devel@nongnu.org Cc: Vivek Kasireddy , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [RFC 2/6] vfio: Add support for VFIO_DEVICE_FEATURE_DMA_BUF Date: Tue, 2 Sep 2025 22:42:15 -0700 Message-ID: <20250903054438.1179384-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250903054438.1179384-1-vivek.kasireddy@intel.com> References: <20250903054438.1179384-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=198.175.65.20; envelope-from=vivek.kasireddy@intel.com; helo=mgamail.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1756878558289124100 In order to implement VFIO_DEVICE_FEATURE_DMA_BUF, we first need to identify the PCI region the buffer (represented by iovec) belongs to and then translate its addresses to offsets within that region. The qemu_ram_block_from_host() API gives us both the region and the offset info we need to populate the dma ranges in order to invoke this feature. Cc: Alex Williamson Cc: C=C3=A9dric Le Goater Signed-off-by: Vivek Kasireddy --- hw/vfio/region.c | 49 +++++++++++++++++++++++++++++++++++ include/hw/vfio/vfio-device.h | 3 +++ 2 files changed, 52 insertions(+) diff --git a/hw/vfio/region.c b/hw/vfio/region.c index d04c57db63..b58188147c 100644 --- a/hw/vfio/region.c +++ b/hw/vfio/region.c @@ -28,6 +28,7 @@ #include "qapi/error.h" #include "qemu/error-report.h" #include "qemu/units.h" +#include "system/ramblock.h" #include "monitor/monitor.h" #include "vfio-helpers.h" =20 @@ -401,3 +402,51 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region,= bool enabled) trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem), enabled); } + +int vfio_device_create_dmabuf(VFIODevice *vdev, + struct iovec *iov, unsigned int iov_cnt) +{ + g_autofree struct vfio_device_feature *feature; + struct vfio_device_feature_dma_buf *dma_buf; + VFIORegion *region; + ram_addr_t offset; + MemoryRegion *mr; + RAMBlock *rb; + size_t argsz; + int i; + + argsz =3D sizeof(*feature) + sizeof (*dma_buf) + + sizeof(struct vfio_region_dma_range) * iov_cnt; + feature =3D g_malloc0(argsz); + dma_buf =3D (struct vfio_device_feature_dma_buf *)feature->data; + + for (i =3D 0; i < iov_cnt; i++) { + rcu_read_lock(); + rb =3D qemu_ram_block_from_host(iov[i].iov_base, false, &offset); + rcu_read_unlock(); + + if (!rb) { + return -1; + } + + mr =3D rb->mr; + if (mr->ops !=3D &vfio_region_ops) { + mr =3D mr->container; + if (mr->ops !=3D &vfio_region_ops) { + return -1; + } + } + + region =3D mr->opaque; + dma_buf->region_index =3D region->nr; + dma_buf->dma_ranges[i].offset =3D offset; + dma_buf->dma_ranges[i].length =3D iov[i].iov_len; + } + + dma_buf->nr_ranges =3D iov_cnt; + dma_buf->open_flags =3D O_RDONLY | O_CLOEXEC; + feature->argsz =3D argsz; + feature->flags =3D VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_B= UF; + + return ioctl(vdev->fd, VFIO_DEVICE_FEATURE, feature); +} diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 6e4d5ccdac..e127abd5f0 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -277,6 +277,9 @@ bool vfio_device_has_region_cap(VFIODevice *vbasedev, i= nt region, uint16_t cap_t =20 int vfio_device_get_irq_info(VFIODevice *vbasedev, int index, struct vfio_irq_info *info); + +int vfio_device_create_dmabuf(VFIODevice *vbasedev, + struct iovec *iov, unsigned int iov_cnt); #endif =20 /* Returns 0 on success, or a negative errno. */ --=20 2.50.1