This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 88ff3f135a1 drivers/pci: multi domains
88ff3f135a1 is described below
commit 88ff3f135a17af8b7c0c8615d554bf84521c28ff
Author: Yanfeng Liu <[email protected]>
AuthorDate: Mon Sep 15 14:49:17 2025 +0800
drivers/pci: multi domains
This adds multi PCI domains support, using one `/dev/pci` for all PCI
domains. By default `fdt_pci_ecam_register()` API scans all PCI domains
in DTB. Use API `fdt_pci_ecam_register_domain()` if only one PCI domain
is needed.
Signed-off-by: Yanfeng Liu <[email protected]>
---
drivers/devicetree/fdt_pci.c | 108 +++++++++++++++++++++++++++++++++++++------
drivers/pci/pci.c | 7 +--
drivers/pci/pci_ecam.c | 9 ++--
include/nuttx/fdt.h | 8 ++--
include/nuttx/pci/pci.h | 10 ++++
include/nuttx/pci/pci_ecam.h | 4 +-
6 files changed, 121 insertions(+), 25 deletions(-)
diff --git a/drivers/devicetree/fdt_pci.c b/drivers/devicetree/fdt_pci.c
index f1fade1e99b..c858fe07b14 100644
--- a/drivers/devicetree/fdt_pci.c
+++ b/drivers/devicetree/fdt_pci.c
@@ -24,6 +24,8 @@
* Included Files
****************************************************************************/
+#include <assert.h>
+#include <debug.h>
#include <errno.h>
#include <nuttx/pci/pci_ecam.h>
@@ -42,31 +44,32 @@
#define FDT_PCI_PREFETCH 0x40000000
/****************************************************************************
- * Public Functions
+ * Private Functions
****************************************************************************/
/****************************************************************************
- * Name: fdt_pci_ecam_register
+ * Name: fdt_pci_ecam_register_offset
*
* Description:
- * This function is used to register an ecam driver from the device tree
+ * Register ECAM for a PCI controller per info from a device tree.
*
* Input Parameters:
* fdt - Device tree handle
+ * offset - offset of PCI controller node in DTB
*
* Returned Value:
* Return 0 if success, nageative if failed
*
****************************************************************************/
-int fdt_pci_ecam_register(FAR const void *fdt)
+static int fdt_pci_ecam_register_offset(FAR const void *fdt, int offset)
{
struct pci_resource_s prefetch;
struct pci_resource_s cfg;
struct pci_resource_s mem;
struct pci_resource_s io;
FAR const fdt32_t *ranges;
- int offset;
+ uint32_t domain;
/* #address-size must be 3
* defined in the PCI Bus Binding to IEEE Std 1275-1994 :
@@ -85,18 +88,18 @@ int fdt_pci_ecam_register(FAR const void *fdt)
int rlen;
int pna;
+ rlen = fdt_load_prop_u32(fdt, offset, "linux,pci-domain", 0, &domain);
+ if (rlen < 0)
+ {
+ pcierr("Failed loading domain: %d\n", rlen);
+ return rlen;
+ }
+
memset(&prefetch, 0, sizeof(prefetch));
memset(&cfg, 0, sizeof(cfg));
memset(&mem, 0, sizeof(mem));
memset(&io, 0, sizeof(io));
- offset = fdt_node_offset_by_compatible(fdt, -1,
- "pci-host-ecam-generic");
- if (offset < 0)
- {
- return offset;
- }
-
/* Get the reg address, 64 or 32 */
cfg.start = fdt_get_reg_base(fdt, offset, 0);
@@ -154,5 +157,84 @@ int fdt_pci_ecam_register(FAR const void *fdt)
}
}
- return pci_ecam_register(&cfg, &io, &mem, &prefetch);
+ return pci_ecam_register(domain, &cfg, &io, &mem, &prefetch);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fdt_pci_ecam_register/_domain
+ *
+ * Description:
+ * Register one or all ECAM instances
+ *
+ * Input Parameters:
+ * fdt - Device tree handle
+ * domain - The specific domain id
+ *
+ * Returned Value:
+ * Return 0 if success, nageative if failed
+ *
+ ****************************************************************************/
+
+int fdt_pci_ecam_register(FAR const void *fdt)
+{
+ int offset = -1;
+ int rc;
+
+ DEBUGASSERT(fdt);
+
+ for (; ; )
+ {
+ offset = fdt_node_offset_by_compatible(fdt, offset,
+ "pci-host-ecam-generic");
+ if (offset < 0)
+ {
+ break;
+ }
+
+ rc = fdt_pci_ecam_register_offset(fdt, offset);
+ if (rc < 0)
+ {
+ pcierr("Failed scanning: %d\n", rc);
+ return rc;
+ }
+ }
+
+ return 0;
+}
+
+int fdt_pci_ecam_register_domain(FAR const void *fdt, uint16_t domain)
+{
+ uint32_t d;
+ int offset = -1;
+ int rc;
+
+ DEBUGASSERT(fdt);
+
+ for (; ; )
+ {
+ offset = fdt_node_offset_by_compatible(fdt, offset,
+ "pci-host-ecam-generic");
+ if (offset < 0)
+ {
+ break;
+ }
+
+ rc = fdt_load_prop_u32(fdt, offset, "linux,pci-domain", 0, &d);
+ if (rc < 0)
+ {
+ pcierr("Failed loading domain: %d\n", rc);
+ return rc;
+ }
+
+ if (d == domain)
+ {
+ return fdt_pci_ecam_register_offset(fdt, offset);
+ }
+ }
+
+ return -EINVAL;
}
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 85098b40b99..d1d8c9a74e9 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -252,7 +252,6 @@ static int pci_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
FAR struct pci_controller_s *ctrl;
FAR struct pcisel *sel;
uint32_t devfn;
- uint8_t i = 0;
int ret;
sel = (FAR struct pcisel *)arg;
@@ -266,17 +265,15 @@ static int pci_ioctl(FAR struct file *filep, int cmd,
unsigned long arg)
list_for_every_entry(&g_pci_ctrl_list, ctrl, struct pci_controller_s, node)
{
- if (i == sel->pc_domain)
+ if (ctrl->domain == sel->pc_domain)
{
break;
}
-
- i++;
}
nxmutex_unlock(&g_pci_lock);
- if (i != sel->pc_domain)
+ if (&ctrl->node == &g_pci_ctrl_list)
{
return -ENODEV;
}
diff --git a/drivers/pci/pci_ecam.c b/drivers/pci/pci_ecam.c
index 2a8a1284094..53e61667ecc 100644
--- a/drivers/pci/pci_ecam.c
+++ b/drivers/pci/pci_ecam.c
@@ -434,9 +434,9 @@ static int pci_ecam_connect_irq(FAR struct pci_bus_s *bus,
FAR int *irq,
static int pci_ecam_get_irq(FAR struct pci_bus_s *bus, uint32_t devfn,
uint8_t line, uint8_t pin)
{
- UNUSED(bus);
+ uint32_t sbdf = PCI_SBDF(bus->ctrl->domain, bus->ctrl->busno, devfn);
- return up_get_legacy_irq(devfn, line, pin);
+ return up_get_legacy_irq(sbdf, line, pin);
}
/****************************************************************************
@@ -489,6 +489,7 @@ static uintptr_t pci_ecam_map(FAR struct pci_bus_s *bus,
uintptr_t start,
* This function is used to register an ecam driver for pci.
*
* Input Parameters:
+ * domain - PCI domain/segment id
* cfg - Configuration space data
* io - I/O space data
* mem - No-prefetchable space data
@@ -499,7 +500,8 @@ static uintptr_t pci_ecam_map(FAR struct pci_bus_s *bus,
uintptr_t start,
*
****************************************************************************/
-int pci_ecam_register(FAR const struct pci_resource_s *cfg,
+int pci_ecam_register(uint16_t domain,
+ FAR const struct pci_resource_s *cfg,
FAR const struct pci_resource_s *io,
FAR const struct pci_resource_s *mem,
FAR const struct pci_resource_s *mem_pref)
@@ -534,6 +536,7 @@ int pci_ecam_register(FAR const struct pci_resource_s *cfg,
memcpy(&ecam->ctrl.mem_pref, mem_pref, sizeof(*mem_pref));
}
+ ecam->ctrl.domain = domain;
ecam->ctrl.ops = &g_pci_ecam_ops;
return pci_register_controller(&ecam->ctrl);
}
diff --git a/include/nuttx/fdt.h b/include/nuttx/fdt.h
index fe004c4796a..5e00b531e73 100644
--- a/include/nuttx/fdt.h
+++ b/include/nuttx/fdt.h
@@ -454,21 +454,23 @@ int fdt_load_prop_u32(FAR const void *fdt, int offset,
FAR uint32_t *value);
/****************************************************************************
- * Name: pci_ecam_register_from_fdt
+ * Name: fdt_pci_ecam_register/_domain
*
* Description:
- * This function is used to register an ecam driver from the device tree
+ * This functions register one or all ECAMs in device tree.
*
* Input Parameters:
* fdt - Device tree handle
+ * domain - The PCI domain to register
*
* Returned Value:
- * Return 0 if success, nageative if failed
+ * Return 0 if success, negative if failed
*
****************************************************************************/
#ifdef CONFIG_PCI
int fdt_pci_ecam_register(FAR const void *fdt);
+int fdt_pci_ecam_register_domain(FAR const void *fdt, uint16_t domain);
#endif
/****************************************************************************
diff --git a/include/nuttx/pci/pci.h b/include/nuttx/pci/pci.h
index 808a0ae41e5..04d66faebde 100644
--- a/include/nuttx/pci/pci.h
+++ b/include/nuttx/pci/pci.h
@@ -55,6 +55,15 @@
#define PCI_SLOT(devfn) (((devfn) >> 3) & 0x1f)
#define PCI_FUNC(devfn) ((devfn) & 0x07)
+/* Upper 16bit for domain/segment, 8bit for bus and last 8bit for devfn */
+
+#define PCI_SBDF(seg, bus, devfn) ((((seg) & 0xffff) << 16) | \
+ (((bus) & 0xff) << 8) | \
+ ((devfn) & 0xff))
+
+#define PCI_DOMAIN(sbdf) (((sbdf) >> 16) & 0xffff)
+#define PCI_BUS(sbdf) (((sbdf) >> 8) & 0xff)
+
#define PCI_ANY_ID (uint16_t)(~0)
/* PCI_DEFINE_DEVICE_TABLE - macro used to describe a pci device table
@@ -361,6 +370,7 @@ struct pci_controller_s
FAR struct pci_bus_s *bus;
struct list_node node;
+ uint16_t domain; /* PCI domain/segment id */
uint8_t busno;
};
diff --git a/include/nuttx/pci/pci_ecam.h b/include/nuttx/pci/pci_ecam.h
index c40e4e3b0b5..dec9a25cc6a 100644
--- a/include/nuttx/pci/pci_ecam.h
+++ b/include/nuttx/pci/pci_ecam.h
@@ -40,6 +40,7 @@
* This function is used to register an ecam driver for pci.
*
* Input Parameters:
+ * domain - PCI domain/segment number
* cfg - Configuration space data
* io - I/O space data
* mem - No-prefetchable space data
@@ -50,7 +51,8 @@
*
****************************************************************************/
-int pci_ecam_register(FAR const struct pci_resource_s *cfg,
+int pci_ecam_register(uint16_t domain,
+ FAR const struct pci_resource_s *cfg,
FAR const struct pci_resource_s *io,
FAR const struct pci_resource_s *mem,
FAR const struct pci_resource_s *mem_pref);