Skip to contentSkip to conversation

PCI: host-generic: Move bridge allocation outside of pci_host_common_init()

PATCH Superseded Marc Zyngier 3 participants 4 messages Nov 20, 2025 – Nov 24, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

PATCH 1/1 PCI: host-generic: Move bridge allocation outside of pci_host_common_init()

Having the host bridge allocation inside pci_host_common_init() results in a lot of complexity in the pcie-apple driver (the only direct user of this function outside of code PCI code).

It forces the allocation of driver-specific tracking structures outside of the bridge allocation, which in turns requires it to use inneficient data structures to match the bridge and the private structre as needed.

Instead, let the bridge structure be passed to pci_host_common_init(), allowing the driver to allocate it together with the private data, as it is usually intended. The driver can then retrieve the bridge via the owning device attached to the PCI config window structure. This allows the pcie-apple driver to be significantly simplified.

Both core and driver code are changed in one go to avoid going via a transitional interface.

Cc Radu Rendec <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Manivannan Sadhasivam <[email protected]>
Cc Rob Herring <[email protected]>
Cc Krzysztof Wilczyński <[email protected]>
Cc Lorenzo Pieralisi <[email protected]>
---
 drivers/pci/controller/pci-host-common.c | 13 ++++----
 drivers/pci/controller/pci-host-common.h |  1 +
 drivers/pci/controller/pcie-apple.c      | 42 ++++--------------------
 3 files changed, 14 insertions(+), 42 deletions(-)
drivers/pci/controller/pci-host-common.c +7 -6
index 810d1c8de24e9..c473e7c03baca 100644
@@ -53,16 +53,12 @@ struct pci_config_window *pci_host_common_ecam_create(struct device *dev,
 EXPORT_SYMBOL_GPL(pci_host_common_ecam_create);
 
 int pci_host_common_init(struct platform_device *pdev,
+			 struct pci_host_bridge *bridge,
 			 const struct pci_ecam_ops *ops)
 {
 	struct device *dev = &pdev->dev;
-	struct pci_host_bridge *bridge;
 	struct pci_config_window *cfg;
 
-	bridge = devm_pci_alloc_host_bridge(dev, 0);
-	if (!bridge)
-		return -ENOMEM;
-
 	of_pci_check_probe_only();
 
 	platform_set_drvdata(pdev, bridge);
@@ -85,12 +81,17 @@ EXPORT_SYMBOL_GPL(pci_host_common_init);
 int pci_host_common_probe(struct platform_device *pdev)
 {
 	const struct pci_ecam_ops *ops;
+	struct pci_host_bridge *bridge;
 
 	ops = of_device_get_match_data(&pdev->dev);
 	if (!ops)
 		return -ENODEV;
 
-	return pci_host_common_init(pdev, ops);
+	bridge = devm_pci_alloc_host_bridge(&pdev->dev, 0);
+	if (!bridge)
+		return -ENOMEM;
+
+	return pci_host_common_init(pdev, bridge, ops);
 }
 EXPORT_SYMBOL_GPL(pci_host_common_probe);
 
drivers/pci/controller/pci-host-common.h +1 -0
index 51c35ec0cf37d..b5075d4bd7eb3 100644
@@ -14,6 +14,7 @@ struct pci_ecam_ops;
 
 int pci_host_common_probe(struct platform_device *pdev);
 int pci_host_common_init(struct platform_device *pdev,
+			 struct pci_host_bridge *bridge,
 			 const struct pci_ecam_ops *ops);
 void pci_host_common_remove(struct platform_device *pdev);
 
drivers/pci/controller/pcie-apple.c +6 -36

73 lines not on this page · raw message

Signature · 1 line
2.47.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Marc Zyngier

Having the host bridge allocation inside pci_host_common_init() results in a lot of complexity in the pcie-apple driver (the only direct user of this function outside of code PCI code).

                              ^^^^

nit: s/code/core :)

Quoted 125 lines

It forces the allocation of driver-specific tracking structures outside of the bridge allocation, which in turns requires it to use inneficient data structures to match the bridge and the private structre as needed.

Instead, let the bridge structure be passed to pci_host_common_init(), allowing the driver to allocate it together with the private data, as it is usually intended. The driver can then retrieve the bridge via the owning device attached to the PCI config window structure. This allows the pcie-apple driver to be significantly simplified.

Both core and driver code are changed in one go to avoid going via a transitional interface.

Cc Radu Rendec <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Manivannan Sadhasivam <[email protected]>
Cc Rob Herring <[email protected]>
Cc Krzysztof Wilczyński <[email protected]>
Cc Lorenzo Pieralisi <[email protected]>

---
 drivers/pci/controller/pci-host-common.c | 13 ++++---- drivers/pci/controller/pci-host-common.h |  1 + drivers/pci/controller/pcie-apple.c      | 42 ++++--------------------

 3 files changed, 14 insertions(+), 42 deletions(-)
drivers/pci/controller/pci-host-common.c +0 -0
index 810d1c8de24e9..c473e7c03baca 100644
@@ -53,16 +53,12 @@ struct pci_config_window *pci_host_common_ecam_create(struct device *dev,
  EXPORT_SYMBOL_GPL(pci_host_common_ecam_create);


 int pci_host_common_init(struct platform_device *pdev,

drivers/pci/controller/pci-host-common.c +1 -0
+			 struct pci_host_bridge *bridge,

  const struct pci_ecam_ops *ops) {
  struct device *dev = &pdev->dev;

drivers/pci/controller/pci-host-common.c +0 -1
-	struct pci_host_bridge *bridge;

  struct pci_config_window *cfg;

drivers/pci/controller/pci-host-common.c +0 -4
-	bridge = devm_pci_alloc_host_bridge(dev, 0);
-	if (!bridge)
-		return -ENOMEM;
-

  of_pci_check_probe_only();

  platform_set_drvdata(pdev, bridge);

drivers/pci/controller/pci-host-common.c +1 -0
@@ -85,12 +81,17 @@ EXPORT_SYMBOL_GPL(pci_host_common_init);
  int pci_host_common_probe(struct platform_device *pdev)
  {
  	const struct pci_ecam_ops *ops;
+	struct pci_host_bridge *bridge;


  ops = of_device_get_match_data(&pdev->dev); if (!ops)
  return -ENODEV;

drivers/pci/controller/pci-host-common.c +5 -1
-	return pci_host_common_init(pdev, ops);
+	bridge = devm_pci_alloc_host_bridge(&pdev->dev, 0);
+	if (!bridge)
+		return -ENOMEM;
+
+	return pci_host_common_init(pdev, bridge, ops);

 }
 EXPORT_SYMBOL_GPL(pci_host_common_probe);

drivers/pci/controller/pci-host-common.h +0 -0
index 51c35ec0cf37d..b5075d4bd7eb3 100644
@@ -14,6 +14,7 @@ struct pci_ecam_ops;


 int pci_host_common_probe(struct platform_device *pdev); int pci_host_common_init(struct platform_device *pdev,

drivers/pci/controller/pci-host-common.h +1 -0
+			 struct pci_host_bridge *bridge,

  const struct pci_ecam_ops *ops); void pci_host_common_remove(struct platform_device *pdev);

drivers/pci/controller/pcie-apple.c +0 -0
index 0380d300adca6..a902aa81a497e 100644
@@ -206,9 +206,6 @@ struct apple_pcie_port {
  	int			idx;
  };

drivers/pci/controller/pcie-apple.c +0 -3
-static LIST_HEAD(pcie_list);
-static DEFINE_MUTEX(pcie_list_lock);
-

 static void rmw_set(u32 set, void __iomem *addr) {
  writel_relaxed(readl_relaxed(addr) | set, addr);

drivers/pci/controller/pcie-apple.c +0 -0
@@ -724,32 +721,9 @@ static int apple_msi_init(struct apple_pcie *pcie)
  	return 0;
  }

drivers/pci/controller/pcie-apple.c +0 -14
-static void apple_pcie_register(struct apple_pcie *pcie)
-{
-	guard(mutex)(&pcie_list_lock);
-
-	list_add_tail(&pcie->entry, &pcie_list);
-}
-
-static void apple_pcie_unregister(struct apple_pcie *pcie)
-{
-	guard(mutex)(&pcie_list_lock);
-
-	list_del(&pcie->entry);
-}
-

 static struct apple_pcie *apple_pcie_lookup(struct device *dev) {

drivers/pci/controller/pcie-apple.c +1 -10
-	struct apple_pcie *pcie;
-
-	guard(mutex)(&pcie_list_lock);
-
-	list_for_each_entry(pcie, &pcie_list, entry) {
-		if (pcie->dev == dev)
-			return pcie;
-	}
-
-	return NULL;
+	return pci_host_bridge_priv(dev_get_drvdata(dev));
Quoted 3 lines

 }

You forgot to remove the entry field from struct apple_pcie. It's no longer needed now that pcie_list is gone.

Quoted 32 lines


 static struct apple_pcie_port *apple_pcie_get_port(struct pci_dev *pdev)

drivers/pci/controller/pcie-apple.c +1 -0
@@ -875,13 +849,15 @@ static const struct pci_ecam_ops apple_pcie_cfg_ecam_ops = {
  static int apple_pcie_probe(struct platform_device *pdev)
  {
  	struct device *dev = &pdev->dev;
+	struct pci_host_bridge *bridge;
  	struct apple_pcie *pcie;
  	int ret;

drivers/pci/controller/pcie-apple.c +2 -2
-	pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
-	if (!pcie)
+	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
+	if (!bridge)

  return -ENOMEM;

drivers/pci/controller/pcie-apple.c +1 -0
+	pcie = pci_host_bridge_priv(bridge);

  pcie->dev = dev;
  pcie->hw = of_device_get_match_data(dev);
  if (!pcie->hw)

drivers/pci/controller/pcie-apple.c +0 -0
@@ -897,13 +873,7 @@ static int apple_pcie_probe(struct platform_device *pdev)
  	if (ret)
  		return ret;

drivers/pci/controller/pcie-apple.c +1 -7
-	apple_pcie_register(pcie);
-
-	ret = pci_host_common_init(pdev, &apple_pcie_cfg_ecam_ops);
-	if (ret)
-		apple_pcie_unregister(pcie);
-
-	return ret;
+	return pci_host_common_init(pdev, bridge, &apple_pcie_cfg_ecam_ops);
Quoted 3 lines

 }

 static const struct of_device_id apple_pcie_of_match[] = {

With those two nitpicks addressed,

And thanks again for spending time on this and creating the patch.

Signature · 1 line
Radu
author maintainer
#2 lore raw
Quoted 131 lines · Radu Rendec
Quoted 3 lines · Marc Zyngier

Having the host bridge allocation inside pci_host_common_init() results in a lot of complexity in the pcie-apple driver (the only direct user of this function outside of code PCI code).

                              ^^^^

nit: s/code/core :)

Quoted 123 lines

It forces the allocation of driver-specific tracking structures outside of the bridge allocation, which in turns requires it to use inneficient data structures to match the bridge and the private structre as needed.

Instead, let the bridge structure be passed to pci_host_common_init(), allowing the driver to allocate it together with the private data, as it is usually intended. The driver can then retrieve the bridge via the owning device attached to the PCI config window structure. This allows the pcie-apple driver to be significantly simplified.

Both core and driver code are changed in one go to avoid going via a transitional interface.

Cc Radu Rendec <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Manivannan Sadhasivam <[email protected]>
Cc Rob Herring <[email protected]>
Cc Krzysztof Wilczyński <[email protected]>
Cc Lorenzo Pieralisi <[email protected]>

---
 drivers/pci/controller/pci-host-common.c | 13 ++++---- drivers/pci/controller/pci-host-common.h |  1 + drivers/pci/controller/pcie-apple.c      | 42 ++++--------------------

 3 files changed, 14 insertions(+), 42 deletions(-)
drivers/pci/controller/pci-host-common.c +0 -0
index 810d1c8de24e9..c473e7c03baca 100644
@@ -53,16 +53,12 @@ struct pci_config_window *pci_host_common_ecam_create(struct device *dev,
  EXPORT_SYMBOL_GPL(pci_host_common_ecam_create);


 int pci_host_common_init(struct platform_device *pdev,

drivers/pci/controller/pci-host-common.c +1 -0
+			 struct pci_host_bridge *bridge,

  const struct pci_ecam_ops *ops) {
  struct device *dev = &pdev->dev;

drivers/pci/controller/pci-host-common.c +0 -1
-	struct pci_host_bridge *bridge;

  struct pci_config_window *cfg;

drivers/pci/controller/pci-host-common.c +0 -4
-	bridge = devm_pci_alloc_host_bridge(dev, 0);
-	if (!bridge)
-		return -ENOMEM;
-

  of_pci_check_probe_only();

  platform_set_drvdata(pdev, bridge);

drivers/pci/controller/pci-host-common.c +1 -0
@@ -85,12 +81,17 @@ EXPORT_SYMBOL_GPL(pci_host_common_init);
  int pci_host_common_probe(struct platform_device *pdev)
  {
  	const struct pci_ecam_ops *ops;
+	struct pci_host_bridge *bridge;


  ops = of_device_get_match_data(&pdev->dev); if (!ops)
  return -ENODEV;

drivers/pci/controller/pci-host-common.c +5 -1
-	return pci_host_common_init(pdev, ops);
+	bridge = devm_pci_alloc_host_bridge(&pdev->dev, 0);
+	if (!bridge)
+		return -ENOMEM;
+
+	return pci_host_common_init(pdev, bridge, ops);

 }
 EXPORT_SYMBOL_GPL(pci_host_common_probe);

drivers/pci/controller/pci-host-common.h +0 -0
index 51c35ec0cf37d..b5075d4bd7eb3 100644
@@ -14,6 +14,7 @@ struct pci_ecam_ops;


 int pci_host_common_probe(struct platform_device *pdev); int pci_host_common_init(struct platform_device *pdev,

drivers/pci/controller/pci-host-common.h +1 -0
+			 struct pci_host_bridge *bridge,

  const struct pci_ecam_ops *ops); void pci_host_common_remove(struct platform_device *pdev);

drivers/pci/controller/pcie-apple.c +0 -0
index 0380d300adca6..a902aa81a497e 100644
@@ -206,9 +206,6 @@ struct apple_pcie_port {
  	int			idx;
  };

drivers/pci/controller/pcie-apple.c +0 -3
-static LIST_HEAD(pcie_list);
-static DEFINE_MUTEX(pcie_list_lock);
-

 static void rmw_set(u32 set, void __iomem *addr) {
  writel_relaxed(readl_relaxed(addr) | set, addr);

drivers/pci/controller/pcie-apple.c +0 -0
@@ -724,32 +721,9 @@ static int apple_msi_init(struct apple_pcie *pcie)
  	return 0;
  }

drivers/pci/controller/pcie-apple.c +0 -14
-static void apple_pcie_register(struct apple_pcie *pcie)
-{
-	guard(mutex)(&pcie_list_lock);
-
-	list_add_tail(&pcie->entry, &pcie_list);
-}
-
-static void apple_pcie_unregister(struct apple_pcie *pcie)
-{
-	guard(mutex)(&pcie_list_lock);
-
-	list_del(&pcie->entry);
-}
-

 static struct apple_pcie *apple_pcie_lookup(struct device *dev) {

drivers/pci/controller/pcie-apple.c +0 -9
-	struct apple_pcie *pcie;
-
-	guard(mutex)(&pcie_list_lock);
-
-	list_for_each_entry(pcie, &pcie_list, entry) {
-		if (pcie->dev == dev)
-			return pcie;
-	}
-
Quoted 8 lines
Quoted 5 lines
drivers/pci/controller/pcie-apple.c +1 -1
-	return NULL;
+	return pci_host_bridge_priv(dev_get_drvdata(dev));

 }

You forgot to remove the entry field from struct apple_pcie. It's no longer needed now that pcie_list is gone.

Ah, good point. Fixed.

Quoted 39 lines
Quoted 33 lines


 static struct apple_pcie_port *apple_pcie_get_port(struct pci_dev *pdev)

drivers/pci/controller/pcie-apple.c +1 -0
@@ -875,13 +849,15 @@ static const struct pci_ecam_ops apple_pcie_cfg_ecam_ops = {
  static int apple_pcie_probe(struct platform_device *pdev)
  {
  	struct device *dev = &pdev->dev;
+	struct pci_host_bridge *bridge;
  	struct apple_pcie *pcie;
  	int ret;

drivers/pci/controller/pcie-apple.c +2 -2
-	pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
-	if (!pcie)
+	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
+	if (!bridge)

  return -ENOMEM;

drivers/pci/controller/pcie-apple.c +1 -0
+	pcie = pci_host_bridge_priv(bridge);

  pcie->dev = dev;
  pcie->hw = of_device_get_match_data(dev);
  if (!pcie->hw)

drivers/pci/controller/pcie-apple.c +0 -0
@@ -897,13 +873,7 @@ static int apple_pcie_probe(struct platform_device *pdev)
  	if (ret)
  		return ret;

drivers/pci/controller/pcie-apple.c +1 -7
-	apple_pcie_register(pcie);
-
-	ret = pci_host_common_init(pdev, &apple_pcie_cfg_ecam_ops);
-	if (ret)
-		apple_pcie_unregister(pcie);
-
-	return ret;
+	return pci_host_common_init(pdev, bridge, &apple_pcie_cfg_ecam_ops);
Quoted 3 lines

 }

 static const struct of_device_id apple_pcie_of_match[] = {

With those two nitpicks addressed,

Quoted 1 line

Thanks. I'll repost this patch some time next week, as this is only an optimisation, not really a fix.

Quoted 2 lines

And thanks again for spending time on this and creating the patch.

Nerd-sniping is a sport, it seems... ;-)

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#3 lore raw
In reply to Marc Zyngier
Quoted 7 lines · Marc Zyngier

Having the host bridge allocation inside pci_host_common_init() results in a lot of complexity in the pcie-apple driver (the only direct user of this function outside of code PCI code).

It forces the allocation of driver-specific tracking structures outside of the bridge allocation, which in turns requires it to use inneficient data structures to match the bridge and the private structre as needed.

Nits since you plan to repost:

s/in turns/in turn/ (maybe a British/American idiom difference?) s/inneficient/inefficient/
s/structre/structure/

Quoted 5 lines

Instead, let the bridge structure be passed to pci_host_common_init(), allowing the driver to allocate it together with the private data, as it is usually intended. The driver can then retrieve the bridge via the owning device attached to the PCI config window structure. This allows the pcie-apple driver to be significantly simplified.

Nice simplification, thanks for doing this!

Bjorn