Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 17607CF64B1 for ; Sat, 22 Nov 2025 07:50:26 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vMiKE-0008Di-Rd; Sat, 22 Nov 2025 02:46:31 -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 1vMhTk-0000gO-RA for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:26 -0500 Received: from mgamail.intel.com ([192.198.163.14]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vMhTN-0001CG-NN for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:12 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1763794314; x=1795330314; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fwkIF75YKfczbZku0xCM5nGDSNWKts3J6DNrrIfsjiY=; b=XH2CzjNQSVktr894n5Qgj5O6OV1ft56ErId7X6eaNQIw3BHaXQUT4WOF 1Mb5qmwY+8CkdnBHxAjY6ILnpPADLz62FMr/L/YnJpBWj2f3xMYQvXSLK IfBEIZMA7W+uEN0wKgMiij4uSYHwOE38f+MT3FLzXDORXCtxZQUK6fSKp AL0gOzC7SXroceyoFegJKG7rBl1lpCm2GP3RPjQA2vOWPzj3gQv5PJ2St ZGmOAt08z2n7xBEKQI8eZj0QM1vXtYYwkmTTq/6wrrLXwszprzMK5VCjt OI+mx5oy1wC5W7VX07operx9+tkCfPj26QzZBoibNirua1xRvvSVMaF0b A==; X-CSE-ConnectionGUID: uIdmJ5/PQxuM1ckAOQWveA== X-CSE-MsgGUID: 6HMquwMgQ16pXOv8z2TTaw== X-IronPort-AV: E=McAfee;i="6800,10657,11620"; a="65924073" X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="65924073" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa108.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2025 22:50:22 -0800 X-CSE-ConnectionGUID: +SIWi5PVTa6cawuWtXe10w== X-CSE-MsgGUID: Vdd8VcNAQkGrTZYcQ9EsYw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="191064612" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2025 22:50:22 -0800 From: Vivek Kasireddy To: qemu-devel@nongnu.org Cc: Vivek Kasireddy , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH v3 4/9] vfio/region: Add a helper to get region index from memory region Date: Fri, 21 Nov 2025 22:46:25 -0800 Message-ID: <20251122064936.2948632-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251122064936.2948632-1-vivek.kasireddy@intel.com> References: <20251122064936.2948632-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=192.198.163.14; envelope-from=vivek.kasireddy@intel.com; helo=mgamail.intel.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, T_SPF_HELO_TEMPERROR=0.01, T_SPF_TEMPERROR=0.01 autolearn=no 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Having a way to figure out the region index (or bar) associated with a memory region is helpful in various scenarios. For example, this capability can be useful in retrieving the region info needed for mapping a part of a VFIO region or creating a dmabuf. Cc: Alex Williamson Cc: Cédric Le Goater Signed-off-by: Vivek Kasireddy --- hw/vfio/region.c | 14 ++++++++++++++ include/hw/vfio/vfio-device.h | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/hw/vfio/region.c b/hw/vfio/region.c index b165ab0b93..046adfaa2c 100644 --- a/hw/vfio/region.c +++ b/hw/vfio/region.c @@ -398,3 +398,17 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled) trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem), enabled); } + +int vfio_get_region_index_from_mr(MemoryRegion *mr) +{ + VFIORegion *region; + + while (mr->container) { + if (mr->ops == &vfio_region_ops) { + region = mr->opaque; + return region->nr; + } + mr = mr->container; + } + return -1; +} diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index bb28123faf..44cacd3728 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -290,6 +290,16 @@ bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_t int vfio_device_get_irq_info(VFIODevice *vbasedev, int index, struct vfio_irq_info *info); + +/** + * Return the region index associated with a given MemoryRegion. The index + * can be useful in retrieving region info. + * + * @mr: MemoryRegion to use + * + * Returns the region index or -1 on error. + */ +int vfio_get_region_index_from_mr(MemoryRegion *mr); #endif /* Returns 0 on success, or a negative errno. */ -- 2.50.1