DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Cheryl Bansal <cheryl.bansal@altera.com>
To: Yipeng Wang <yipeng1.wang@intel.com>,
	Sameh Gobriel <sameh.gobriel@intel.com>,
	Bruce Richardson <bruce.richardson@intel.com>,
	Vladimir Medvedkin <vladimir.medvedkin@intel.com>,
	Chenbo Xia <chenbox@nvidia.com>,
	Nipun Gupta <nipun.gupta@amd.com>
Cc: dev@dpdk.org, stable@dpdk.org, Cheryl Bansal <cheryl.bansal@altera.com>
Subject: [PATCH 2/2] bus/pci: add selective BAR mapping support
Date: Mon, 23 Nov 2026 11:43:19 +0530	[thread overview]
Message-ID: <20261123061319.163645-2-cheryl.bansal@altera.com> (raw)
In-Reply-To: <20261123061319.163645-1-cheryl.bansal@altera.com>

Drivers that only need a subset of PCI BARs still pay the cost of
mapping every valid BAR. There is no bus-level way to select which
BARs are mapped without changing each PMD.

Add the bar_mask PCI bus argument to select which BARs to map. If it
is omitted, all valid BARs are mapped. A zero mask, a value outside
BAR0-BAR5, or a malformed value is rejected. The argument is parsed
from bus_str so it is not passed to PMDs. UIO and VFIO map slots are
indexed by BAR number so primary and secondary processes stay
consistent when some BARs are skipped.

Example: -a bus=pci,addr=0000:01:00.0,bar_mask=0x05

Signed-off-by: Cheryl Bansal <cheryl.bansal@altera.com>
---
 doc/guides/prog_guide/devargs.rst      | 13 +++++++
 doc/guides/rel_notes/release_26_11.rst |  6 +++
 drivers/bus/pci/linux/pci_vfio.c       |  7 ++++
 drivers/bus/pci/pci_common.c           | 46 +++++++++++++++++++++++
 drivers/bus/pci/pci_common_uio.c       | 52 ++++++++++++++------------
 drivers/bus/pci/pci_params.c           |  2 +
 drivers/bus/pci/private.h              | 16 ++++++++
 7 files changed, 118 insertions(+), 24 deletions(-)

diff --git a/doc/guides/prog_guide/devargs.rst b/doc/guides/prog_guide/devargs.rst
index c8a7224aa0..dc30f3a487 100644
--- a/doc/guides/prog_guide/devargs.rst
+++ b/doc/guides/prog_guide/devargs.rst
@@ -82,6 +82,19 @@ The above example will pass ``representor=[0-3]`` devarg
 only to device corresponding to PCI address ``0000:02:00.0``,
 and only if it is managed by ``ice`` driver.
 
+PCI bus arguments include:
+
+* ``addr``: PCI address of the device (BDF).
+* ``bar_mask``: bitmask of BARs to map (bits 0-5 for BAR0-BAR5).
+  If omitted, all valid BARs are mapped.
+  A zero mask, a value outside BAR0-BAR5, or a malformed value is rejected.
+
+.. code-block:: none
+
+   bus=pci,addr=0000:01:00.0,bar_mask=0x05
+
+The above example maps BAR0 and BAR2 only for the given PCI device.
+
 
 Simplified syntax
 ~~~~~~~~~~~~~~~~~
diff --git a/doc/guides/rel_notes/release_26_11.rst b/doc/guides/rel_notes/release_26_11.rst
index 10ab2355ae..90dc79ee16 100644
--- a/doc/guides/rel_notes/release_26_11.rst
+++ b/doc/guides/rel_notes/release_26_11.rst
@@ -58,6 +58,12 @@ New Features
   the kernel driver is the generic ``vfio-platform``,
   which carries no device identity in its name.
 
+* **Added selective PCI BAR mapping.**
+
+  Added the PCI bus argument ``bar_mask`` to select which BARs to map.
+  If omitted, all valid BARs are mapped.
+  Example: ``-a bus=pci,addr=0000:01:00.0,bar_mask=0x05``.
+
 * **Added 1.6T link speed to ethdev.**
 
   Added the 1.6 Tb/s Ethernet link speed,
diff --git a/drivers/bus/pci/linux/pci_vfio.c b/drivers/bus/pci/linux/pci_vfio.c
index 4363d7ff2c..1046d84c36 100644
--- a/drivers/bus/pci/linux/pci_vfio.c
+++ b/drivers/bus/pci/linux/pci_vfio.c
@@ -738,6 +738,7 @@ pci_vfio_map_resource_primary(struct rte_pci_device *dev)
 	struct mapped_pci_resource *vfio_res = NULL;
 	struct mapped_pci_res_list *vfio_res_list =
 		RTE_TAILQ_CAST(dev_vfio_tailq.head, mapped_pci_res_list);
+	uint8_t bar_mask = pci_get_bar_mask(dev);
 
 	struct pci_map *maps;
 
@@ -814,6 +815,9 @@ pci_vfio_map_resource_primary(struct rte_pci_device *dev)
 		struct vfio_info_cap_header *hdr;
 		struct vfio_region_info_cap_sparse_mmap *sparse;
 
+		if ((bar_mask & (1 << i)) == 0)
+			continue;
+
 		ret = pci_vfio_get_region_info(vfio_dev_fd, &reg, i);
 		if (ret < 0) {
 			PCI_LOG(ERR, "%s cannot get device region info error %i (%s)",
@@ -1018,6 +1022,9 @@ pci_vfio_map_resource_secondary(struct rte_pci_device *dev)
 int
 pci_vfio_map_resource(struct rte_pci_device *dev)
 {
+	if (RTE_PCI_DEVICE_INTERNAL(dev)->bar_mask_err != 0)
+		return -EINVAL;
+
 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
 		return pci_vfio_map_resource_primary(dev);
 	else
diff --git a/drivers/bus/pci/pci_common.c b/drivers/bus/pci/pci_common.c
index 3770b4ad75..f36e66a48d 100644
--- a/drivers/bus/pci/pci_common.c
+++ b/drivers/bus/pci/pci_common.c
@@ -3,6 +3,7 @@
  * Copyright 2013-2014 6WIND S.A.
  */
 
+#include <errno.h>
 #include <string.h>
 #include <inttypes.h>
 #include <stdint.h>
@@ -25,6 +26,7 @@
 #include <rte_string_fns.h>
 #include <rte_common.h>
 #include <rte_devargs.h>
+#include <rte_kvargs.h>
 #include <rte_tailq.h>
 
 #include "private.h"
@@ -78,15 +80,59 @@ pci_asprintf(char **buffer, const char *format, ...)
 }
 #endif /* RTE_EXEC_ENV_WINDOWS */
 
+/* Parse bar_mask from bus_str. Absent => 0 (map all). Invalid => -EINVAL. */
+static int
+pci_parse_bar_mask(const struct rte_devargs *devargs, uint8_t *mask)
+{
+	struct rte_kvargs *kvargs;
+	const char *bar_mask_str;
+	unsigned long val;
+	char *endptr;
+
+	*mask = 0;
+	if (devargs == NULL || devargs->bus_str == NULL)
+		return 0;
+
+	kvargs = rte_kvargs_parse(devargs->bus_str, NULL);
+	if (kvargs == NULL) {
+		PCI_LOG(ERR, "cannot parse bus arguments: %s",
+			devargs->bus_str);
+		return -EINVAL;
+	}
+
+	bar_mask_str = rte_kvargs_get(kvargs, PCI_BAR_MASK_ARG);
+	if (bar_mask_str == NULL) {
+		rte_kvargs_free(kvargs);
+		return 0;
+	}
+
+	errno = 0;
+	val = strtoul(bar_mask_str, &endptr, 0);
+	if (errno != 0 || endptr == bar_mask_str || *endptr != '\0' ||
+			val == 0 || val > PCI_BAR_MASK_ALL) {
+		PCI_LOG(ERR, "Invalid bar_mask value '%s'", bar_mask_str);
+		rte_kvargs_free(kvargs);
+		return -EINVAL;
+	}
+
+	*mask = (uint8_t)val;
+	rte_kvargs_free(kvargs);
+	return 0;
+}
+
 void
 pci_common_set(struct rte_pci_device *dev)
 {
+	struct rte_pci_device_internal *pdev = RTE_PCI_DEVICE_INTERNAL(dev);
+
 	/* Each device has its internal, canonical name set. */
 	rte_pci_device_name(&dev->addr,
 			dev->name, sizeof(dev->name));
 	dev->device.name = dev->name;
 
 	dev->device.devargs = rte_bus_find_devargs(&rte_pci_bus, dev->name);
+	pdev->bar_mask_err = pci_parse_bar_mask(dev->device.devargs,
+			&pdev->bar_mask);
 
 	if (dev->bus_info != NULL ||
 			asprintf(&dev->bus_info, "vendor_id=%"PRIx16", device_id=%"PRIx16,
diff --git a/drivers/bus/pci/pci_common_uio.c b/drivers/bus/pci/pci_common_uio.c
index 52a10f806a..19d8df5d1d 100644
--- a/drivers/bus/pci/pci_common_uio.c
+++ b/drivers/bus/pci/pci_common_uio.c
@@ -27,7 +27,7 @@ EAL_REGISTER_TAILQ(rte_uio_tailq)
 static int
 pci_uio_map_secondary(struct rte_pci_device *dev)
 {
-	int fd, i = 0, j, res_idx;
+	int fd, i, j;
 	struct mapped_pci_resource *uio_res;
 	struct mapped_pci_res_list *uio_res_list =
 			RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
@@ -38,15 +38,11 @@ pci_uio_map_secondary(struct rte_pci_device *dev)
 		if (rte_pci_addr_cmp(&uio_res->pci_addr, &dev->addr))
 			continue;
 
-		/* Map all BARs */
-		for (res_idx = 0; res_idx != PCI_MAX_RESOURCE; res_idx++) {
-			/* skip empty BAR */
-			if (dev->mem_resource[res_idx].phys_addr == 0)
+		/* Replay primary mappings (indexed by BAR) */
+		for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+			if (uio_res->maps[i].size == 0)
 				continue;
 
-			if (i >= uio_res->nb_maps)
-				return -1;
-
 			/*
 			 * open devname, to mmap it
 			 */
@@ -80,9 +76,7 @@ pci_uio_map_secondary(struct rte_pci_device *dev)
 				}
 				return -1;
 			}
-			dev->mem_resource[res_idx].addr = mapaddr;
-
-			i++;
+			dev->mem_resource[i].addr = mapaddr;
 		}
 		return 0;
 	}
@@ -95,11 +89,15 @@ pci_uio_map_secondary(struct rte_pci_device *dev)
 int
 pci_uio_map_resource(struct rte_pci_device *dev)
 {
-	int i, map_idx = 0, ret;
+	int i, ret;
 	uint64_t phaddr;
 	struct mapped_pci_resource *uio_res = NULL;
 	struct mapped_pci_res_list *uio_res_list =
 		RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
+	uint8_t bar_mask = pci_get_bar_mask(dev);
+
+	if (RTE_PCI_DEVICE_INTERNAL(dev)->bar_mask_err != 0)
+		return -EINVAL;
 
 	/* allocate uio resource */
 	ret = pci_uio_alloc_resource(dev, &uio_res);
@@ -110,31 +108,34 @@ pci_uio_map_resource(struct rte_pci_device *dev)
 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
 		return pci_uio_map_secondary(dev);
 
-	/* Map all BARs */
+	/* Index maps[] by BAR number */
 	for (i = 0; i != PCI_MAX_RESOURCE; i++) {
+		if ((bar_mask & (1 << i)) == 0)
+			continue;
+
 		/* skip empty BAR */
 		phaddr = dev->mem_resource[i].phys_addr;
 		if (phaddr == 0)
 			continue;
 
 		ret = pci_uio_map_resource_by_index(dev, i,
-				uio_res, map_idx);
+				uio_res, i);
 		if (ret)
 			goto error;
-
-		map_idx++;
 	}
 
-	uio_res->nb_maps = map_idx;
+	uio_res->nb_maps = PCI_MAX_RESOURCE;
 
 	TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
 
 	return 0;
 error:
-	for (i = 0; i < map_idx; i++) {
-		pci_unmap_resource(uio_res->maps[i].addr,
-				(size_t)uio_res->maps[i].size);
-		rte_free(uio_res->maps[i].path);
+	for (i = 0; i < PCI_MAX_RESOURCE; i++) {
+		if (uio_res->maps[i].addr != NULL) {
+			pci_unmap_resource(uio_res->maps[i].addr,
+					(size_t)uio_res->maps[i].size);
+			rte_free(uio_res->maps[i].path);
+		}
 	}
 	pci_uio_free_resource(dev, uio_res);
 	return -1;
@@ -149,6 +150,8 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res)
 		return;
 
 	for (i = 0; i != uio_res->nb_maps; i++) {
+		if (uio_res->maps[i].addr == NULL)
+			continue;
 		pci_unmap_resource(uio_res->maps[i].addr,
 				(size_t)uio_res->maps[i].size);
 		if (rte_eal_process_type() == RTE_PROC_PRIMARY)
@@ -166,10 +169,11 @@ pci_uio_remap_resource(struct rte_pci_device *dev)
 	if (dev == NULL)
 		return -1;
 
-	/* Remap all BARs */
+	/* Remap mapped BARs only */
 	for (i = 0; i != PCI_MAX_RESOURCE; i++) {
-		/* skip empty BAR */
-		if (dev->mem_resource[i].phys_addr == 0)
+		/* skip empty or unmapped BAR */
+		if (dev->mem_resource[i].phys_addr == 0 ||
+				dev->mem_resource[i].addr == NULL)
 			continue;
 		map_address = mmap(dev->mem_resource[i].addr,
 				(size_t)dev->mem_resource[i].len,
diff --git a/drivers/bus/pci/pci_params.c b/drivers/bus/pci/pci_params.c
index 6cbd98b4c8..4fe148b0a1 100644
--- a/drivers/bus/pci/pci_params.c
+++ b/drivers/bus/pci/pci_params.c
@@ -17,11 +17,13 @@
 
 enum pci_params {
 	RTE_PCI_PARAM_ADDR,
+	RTE_PCI_PARAM_BAR_MASK,
 	RTE_PCI_PARAM_MAX,
 };
 
 static const char * const pci_params_keys[] = {
 	[RTE_PCI_PARAM_ADDR] = "addr",
+	[RTE_PCI_PARAM_BAR_MASK] = PCI_BAR_MASK_ARG,
 	[RTE_PCI_PARAM_MAX] = NULL,
 };
 
diff --git a/drivers/bus/pci/private.h b/drivers/bus/pci/private.h
index f266a1b04d..0806822602 100644
--- a/drivers/bus/pci/private.h
+++ b/drivers/bus/pci/private.h
@@ -43,8 +43,24 @@ struct rte_pci_device_internal {
 	struct rte_pci_device device;
 	/* PCI regions provided by e.g. VFIO. */
 	struct rte_pci_region region[RTE_MAX_PCI_REGIONS];
+	int bar_mask_err;	/* nonzero if bar_mask was present but invalid */
+	uint8_t bar_mask;	/* 0 = map all BARs */
 };
 
+#define PCI_BAR_MASK_ARG "bar_mask"
+#define PCI_BAR_MASK_ALL ((1U << PCI_MAX_RESOURCE) - 1)
+
+static inline uint8_t
+pci_get_bar_mask(struct rte_pci_device *dev)
+{
+	struct rte_pci_device_internal *pdev = RTE_PCI_DEVICE_INTERNAL(dev);
+	uint8_t mask = pdev->bar_mask;
+
+	if (mask == 0)
+		mask = PCI_BAR_MASK_ALL;
+	return mask;
+}
+
 /**
  * Scan the content of the PCI bus, and the devices in the devices
  * list
-- 
2.43.0


  reply	other threads:[~2026-11-23  6:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  6:13 [PATCH 1/2] hash: fix maybe-uninitialized warning on minsize build Cheryl Bansal
2026-11-23  6:13 ` Cheryl Bansal [this message]
2026-11-23  7:57   ` [PATCH 2/2] bus/pci: add selective BAR mapping support David Marchand
2026-11-23 17:03     ` Stephen Hemminger

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=20261123061319.163645-2-cheryl.bansal@altera.com \
    --to=cheryl.bansal@altera.com \
    --cc=bruce.richardson@intel.com \
    --cc=chenbox@nvidia.com \
    --cc=dev@dpdk.org \
    --cc=nipun.gupta@amd.com \
    --cc=sameh.gobriel@intel.com \
    --cc=stable@dpdk.org \
    --cc=vladimir.medvedkin@intel.com \
    --cc=yipeng1.wang@intel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox