From nobody Fri Nov 14 23:29:45 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=1762666855; cv=none; d=zohomail.com; s=zohoarc; b=UxrwnWGToGGyIphGBv/wJjPF3Q9o1i9vhAxy07vv1fKtk7aPfmUAlyDizPWgbO9apxyvNIpYPWz5l4q5VGUB5T7fvfJBh84K5xh9juHXp1k3j7pI3NV0YPajmtE+KCYMl2pQKsMQRqY/g6oTL7m/ZGZNO2wmXvdhi4WK93sd32M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1762666855; 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=KDGO/Z8yk+Eo0bS7HvarOTWfa7HYCeNA9yMYh2Lcgw8=; b=KXpBYcrsWjNH2P419Sz+WkZKkSgNZnqkYjUbetnDgZvKttTvQLh7rHq6V1ADfb+HJJrjBT9UsTULsJJrjdWwbIky5qik9ke6gzZkLF8HsRkDcMgnT74HJ/mHa17d3srjHO2kpX2o1T+v+Mi1leyaUtDZCyxNmSHl4t4UeVS1ZFw= 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 1762666855679465.0250668655118; Sat, 8 Nov 2025 21:40:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vHy8s-0005br-Jq; Sun, 09 Nov 2025 00:39:10 -0500 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 1vHy8a-0005Wp-TL for qemu-devel@nongnu.org; Sun, 09 Nov 2025 00:38:53 -0500 Received: from mgamail.intel.com ([192.198.163.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vHy8Z-0007aH-4n for qemu-devel@nongnu.org; Sun, 09 Nov 2025 00:38:52 -0500 Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2025 21:38:41 -0800 Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2025 21:38:41 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762666731; x=1794202731; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Z6qFGL5WE+eZg5Z8dvi+fn6ZmgEH4hUNJqCkhe0bpoo=; b=cVuYxiSjAFu7ejntj7M0Gd/HlAhXIiA22bMM7AVG3xRON4cd9wR7nB4d kU4A/XDAwSlgbyvt24jVIiM/Z5M5wGAsnElLKL6iuQJJcHV5LVNZDvUBi hpq28QYUor0gbSdANPMrxjuBCLBPwq8TAMyoTMWb1gVovsa0KVh34plcj cFoiw5DR49K9XYGTtk27tQVGspFzqcI24JN/ym46YeSlfTfwKCVq/lI6y CwKfFyacaWPf0e8F2Hr/DQsf0UvqTHFB+AzvxxZFsAM7PiXxAcj1/5Sbh 55AjljfKb6ZQuIKhjrT2xbaPtTpZ1bCZ/WLO7cB7E2kQ/Z+mEDQkfFkj5 g==; X-CSE-ConnectionGUID: gcUvZYHzQUaGAdUWCJt1lw== X-CSE-MsgGUID: 8gv3EkRtTfycmlZWbDpruw== X-IronPort-AV: E=McAfee;i="6800,10657,11607"; a="63965791" X-IronPort-AV: E=Sophos;i="6.19,291,1754982000"; d="scan'208";a="63965791" X-CSE-ConnectionGUID: YYReIa0VReybm7NJnLUTTQ== X-CSE-MsgGUID: 9MqzAaCiSGqearlBRAb/xg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,291,1754982000"; d="scan'208";a="188129063" From: Vivek Kasireddy To: qemu-devel@nongnu.org Cc: Vivek Kasireddy , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH v2 07/10] vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF Date: Sat, 8 Nov 2025 21:33:50 -0800 Message-ID: <20251109053801.2267149-8-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251109053801.2267149-1-vivek.kasireddy@intel.com> References: <20251109053801.2267149-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=192.198.163.18; 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: 1762666856615158500 In order to implement VFIO_DEVICE_FEATURE_DMA_BUF, we first need to identify the VFIO region and index 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/device.c | 40 +++++++++++++++++++++++++++++++++++ include/hw/vfio/vfio-device.h | 14 ++++++++++++ 2 files changed, 54 insertions(+) diff --git a/hw/vfio/device.c b/hw/vfio/device.c index 9ff73f9941..5417142482 100644 --- a/hw/vfio/device.c +++ b/hw/vfio/device.c @@ -21,6 +21,7 @@ #include "qemu/osdep.h" #include =20 +#include "system/ramblock.h" #include "hw/vfio/vfio-device.h" #include "hw/vfio/pci.h" #include "hw/hw.h" @@ -615,3 +616,42 @@ VFIODevice *vfio_device_lookup(MemoryRegion *mr) } return NULL; } + +int vfio_device_create_dmabuf_fd(VFIODevice *vbasedev, + struct iovec *iov, unsigned int iov_cnt) +{ + g_autofree struct vfio_device_feature *feature =3D NULL; + struct vfio_device_feature_dma_buf *dma_buf; + ram_addr_t offset; + RAMBlock *rb; + size_t argsz; + int i, index; + + 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++) { + rb =3D qemu_ram_block_from_host(iov[i].iov_base, false, &offset); + if (!rb) { + return -1; + } + + index =3D vfio_get_region_index_from_mr(rb->mr); + if (index < 0) { + return -1; + } + + dma_buf->region_index =3D index; + 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 vbasedev->io_ops->device_feature(vbasedev, feature); +} diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 2f8087f133..7fc2912f15 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -309,6 +309,20 @@ int vfio_get_region_index_from_mr(MemoryRegion *mr); * Returns the VFIO device if found or NULL. */ VFIODevice *vfio_device_lookup(MemoryRegion *mr); + +/** + * Create and return a dmabuf fd by first translating the addresses in the + * iovec array into VFIO region offsets and then invoking the + * VFIO_DEVICE_FEATURE_DMA_BUF feature. + * + * @vbasedev: #VFIODevice to use + * @iov: array of iovec entries associated with a buffer + * @iov_cnt: number of entries in the iovec array + * + * Returns the newly created dmabuf fd or -1 on error. + */ +int vfio_device_create_dmabuf_fd(VFIODevice *vbasedev, + struct iovec *iov, unsigned int iov_cnt); #endif =20 /* Returns 0 on success, or a negative errno. */ --=20 2.50.1