Skip to contentSkip to conversation

PCI: dwc: Support remove

PATCH Applied Jisheng Zhang 3 participants 8 messages Mar 29, 2019 – Apr 3, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover PCI: dwc: Support remove

Currently, the PCI dwc host users don't support the remove, but nothing prevent us from supporting it. To achieve this goal, we need to ensure we can do necessary clean up work.

Changes since v3:
- add Gustavo's ACK to patch2, patch5
- remove IS_ENABLED(CONFIG_PCI_MSI) check

Changes since v2:
- address all Gustavo's comments
- add Gustavo's ACK to patch1, patch3, patch4

Changes since v1:
- address Bjorn's comments, I.E Capitalize, s/irq/IRQ/, s/msi/MSI/

Jisheng Zhang (5):
PCI: dwc: Fix dw_pcie_free_msi() if msi_irq is invalid
PCI: dwc: Free the page for MSI IRQ in dw_pcie_free_msi()
PCI: dwc: Free MSI in the error code path of dw_pcie_host_init()
PCI: dwc: Use devm_pci_alloc_host_bridge() to simplify the code
PCI: dwc: Save root bus for driver remove

 .../pci/controller/dwc/pcie-designware-host.c | 60 ++++++++++---------
 drivers/pci/controller/dwc/pcie-designware.h  |  2 +
 2 files changed, 34 insertions(+), 28 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH v4 1/5 PCI: dwc: Fix dw_pcie_free_msi() if msi_irq is invalid

We should check msi_irq before calling irq_set_chained_handler() and irq_set_handler_data().

---
 drivers/pci/controller/dwc/pcie-designware-host.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +4 -2
index 25087d3c9a82..1040939f45b4 100644
@@ -298,8 +298,10 @@ int dw_pcie_allocate_domains(struct pcie_port *pp)
 
 void dw_pcie_free_msi(struct pcie_port *pp)
 {
-	irq_set_chained_handler(pp->msi_irq, NULL);
-	irq_set_handler_data(pp->msi_irq, NULL);
+	if (pp->msi_irq) {
+		irq_set_chained_handler(pp->msi_irq, NULL);
+		irq_set_handler_data(pp->msi_irq, NULL);
+	}
 
 	irq_domain_remove(pp->msi_domain);
 	irq_domain_remove(pp->irq_domain);
Signature · 1 line
2.20.1
#2 lore raw

PATCH v4 2/5 PCI: dwc: Free the page for MSI IRQ in dw_pcie_free_msi()

In reply to Jisheng Zhang

To avoid memory leak, we need to free the page for MSI in dw_pcie_free_msi().

---
 drivers/pci/controller/dwc/pcie-designware-host.c | 12 ++++++++----
 drivers/pci/controller/dwc/pcie-designware.h      |  1 +
 2 files changed, 9 insertions(+), 4 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +8 -4
index 1040939f45b4..a71b874ae3c0 100644
@@ -305,20 +305,24 @@ void dw_pcie_free_msi(struct pcie_port *pp)
 
 	irq_domain_remove(pp->msi_domain);
 	irq_domain_remove(pp->irq_domain);
+
+	if (pp->msi_page)
+		__free_page(pp->msi_page);
 }
 
 void dw_pcie_msi_init(struct pcie_port *pp)
 {
 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
 	struct device *dev = pci->dev;
-	struct page *page;
 	u64 msi_target;
 
-	page = alloc_page(GFP_KERNEL);
-	pp->msi_data = dma_map_page(dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
+	pp->msi_page = alloc_page(GFP_KERNEL);
+	pp->msi_data = dma_map_page(dev, pp->msi_page, 0, PAGE_SIZE,
+				    DMA_FROM_DEVICE);
 	if (dma_mapping_error(dev, pp->msi_data)) {
 		dev_err(dev, "Failed to map MSI data\n");
-		__free_page(page);
+		__free_page(pp->msi_page);
+		pp->msi_page = NULL;
 		return;
 	}
 	msi_target = (u64)pp->msi_data;
drivers/pci/controller/dwc/pcie-designware.h +1 -0
index 377f4c0b52da..6fb0a1879932 100644
@@ -179,6 +179,7 @@ struct pcie_port {
 	struct irq_domain	*irq_domain;
 	struct irq_domain	*msi_domain;
 	dma_addr_t		msi_data;
+	struct page		*msi_page;
 	u32			num_vectors;
 	u32			irq_mask[MAX_MSI_CTRLS];
 	raw_spinlock_t		lock;
Signature · 1 line
2.20.1
#3 lore raw

PATCH v4 3/5 PCI: dwc: Free MSI in the error code path of dw_pcie_host_init()

In reply to Jisheng Zhang

If we ever did some msi related initializations, we need to call dw_pcie_free_msi() in the error code path.

We also remove the IS_ENABLED(CONFIG_PCI_MSI) check for MSI init, because pci_msi_enabled() already has a stub for !CONFIG_PCI_MSI.

---
 drivers/pci/controller/dwc/pcie-designware-host.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +6 -3
index a71b874ae3c0..acc9be5cf34a 100644
@@ -445,7 +445,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
 	if (ret)
 		pci->num_viewport = 2;
 
-	if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_enabled()) {
+	if (pci_msi_enabled()) {
 		/*
 		 * If a specific SoC driver needs to change the
 		 * default number of vectors, it needs to implement
@@ -483,7 +483,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
 	if (pp->ops->host_init) {
 		ret = pp->ops->host_init(pp);
 		if (ret)
-			goto error;
+			goto err_free_msi;
 	}
 
 	pp->root_bus_nr = pp->busn->start;
@@ -497,7 +497,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
 
 	ret = pci_scan_root_bus_bridge(bridge);
 	if (ret)
-		goto error;
+		goto err_free_msi;
 
 	bus = bridge->bus;
 
@@ -513,6 +513,9 @@ int dw_pcie_host_init(struct pcie_port *pp)
 	pci_bus_add_devices(bus);
 	return 0;
 
+err_free_msi:
+	if (pci_msi_enabled() && !pp->ops->msi_host_init)
+		dw_pcie_free_msi(pp);
 error:
 	pci_free_host_bridge(bridge);
 	return ret;
Signature · 1 line
2.20.1
#4 lore raw

PATCH v4 4/5 PCI: dwc: Use devm_pci_alloc_host_bridge() to simplify the code

In reply to Jisheng Zhang

Use devm_pci_alloc_host_bridge() to simplify the error code path.

---
 .../pci/controller/dwc/pcie-designware-host.c | 21 +++++++------------
 1 file changed, 8 insertions(+), 13 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +8 -13
index acc9be5cf34a..dcc7405aff9a 100644
@@ -358,7 +358,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
 		dev_err(dev, "Missing *config* reg space\n");
 	}
 
-	bridge = pci_alloc_host_bridge(0);
+	bridge = devm_pci_alloc_host_bridge(dev, 0);
 	if (!bridge)
 		return -ENOMEM;
 
@@ -369,7 +369,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
 
 	ret = devm_request_pci_bus_resources(dev, &bridge->windows);
 	if (ret)
-		goto error;
+		return ret;
 
 	/* Get the I/O and memory ranges from DT */
 	resource_list_for_each_entry_safe(win, tmp, &bridge->windows) {
@@ -413,8 +413,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
 						resource_size(pp->cfg));
 		if (!pci->dbi_base) {
 			dev_err(dev, "Error with ioremap\n");
-			ret = -ENOMEM;
-			goto error;
+			return -ENOMEM;
 		}
 	}
 
@@ -425,8 +424,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
 					pp->cfg0_base, pp->cfg0_size);
 		if (!pp->va_cfg0_base) {
 			dev_err(dev, "Error with ioremap in function\n");
-			ret = -ENOMEM;
-			goto error;
+			return -ENOMEM;
 		}
 	}
 
@@ -436,8 +434,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
 						pp->cfg1_size);
 		if (!pp->va_cfg1_base) {
 			dev_err(dev, "Error with ioremap\n");
-			ret = -ENOMEM;
-			goto error;
+			return -ENOMEM;
 		}
 	}
 
@@ -460,14 +457,14 @@ int dw_pcie_host_init(struct pcie_port *pp)
 			    pp->num_vectors == 0) {
 				dev_err(dev,
 					"Invalid number of vectors\n");
-				goto error;
+				return -EINVAL;
 			}
 		}
 
 		if (!pp->ops->msi_host_init) {
 			ret = dw_pcie_allocate_domains(pp);
 			if (ret)
-				goto error;
+				return ret;
 
 			if (pp->msi_irq)
 				irq_set_chained_handler_and_data(pp->msi_irq,
@@ -476,7 +473,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
 		} else {
 			ret = pp->ops->msi_host_init(pp);
 			if (ret < 0)
-				goto error;
+				return ret;
 		}
 	}
 
@@ -516,8 +513,6 @@ int dw_pcie_host_init(struct pcie_port *pp)
 err_free_msi:
 	if (pci_msi_enabled() && !pp->ops->msi_host_init)
 		dw_pcie_free_msi(pp);
-error:
-	pci_free_host_bridge(bridge);
 	return ret;
 }
 
Signature · 1 line
2.20.1
#5 lore raw

PATCH v4 5/5 PCI: dwc: Save root bus for driver remove

In reply to Jisheng Zhang

Currently dwc host doesn't support the remove, but nothing prevent us from supporting it. Save the root bus for clean up work in driver remove code path.

After this patch, the dwc host users could implement its remove as:

static int foo_pcie_remove(struct platform_device *pdev)

{
	...
	pci_stop_root_bus(pp->root_bus);
	pci_remove_root_bus(pp->root_bus);
	dw_pcie_free_msi(pp);
	...
}
---
 drivers/pci/controller/dwc/pcie-designware-host.c | 12 ++++++------
 drivers/pci/controller/dwc/pcie-designware.h      |  1 +
 2 files changed, 7 insertions(+), 6 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +6 -6
index dcc7405aff9a..3e4169e738a5 100644
@@ -341,7 +341,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
 	struct device_node *np = dev->of_node;
 	struct platform_device *pdev = to_platform_device(dev);
 	struct resource_entry *win, *tmp;
-	struct pci_bus *bus, *child;
+	struct pci_bus *child;
 	struct pci_host_bridge *bridge;
 	struct resource *cfg_res;
 	int ret;
@@ -496,18 +496,18 @@ int dw_pcie_host_init(struct pcie_port *pp)
 	if (ret)
 		goto err_free_msi;
 
-	bus = bridge->bus;
+	pp->root_bus = bridge->bus;
 
 	if (pp->ops->scan_bus)
 		pp->ops->scan_bus(pp);
 
-	pci_bus_size_bridges(bus);
-	pci_bus_assign_resources(bus);
+	pci_bus_size_bridges(pp->root_bus);
+	pci_bus_assign_resources(pp->root_bus);
 
-	list_for_each_entry(child, &bus->children, node)
+	list_for_each_entry(child, &pp->root_bus->children, node)
 		pcie_bus_configure_settings(child);
 
-	pci_bus_add_devices(bus);
+	pci_bus_add_devices(pp->root_bus);
 	return 0;
 
 err_free_msi:
drivers/pci/controller/dwc/pcie-designware.h +1 -0
index 6fb0a1879932..adff0c713665 100644
@@ -182,6 +182,7 @@ struct pcie_port {
 	struct page		*msi_page;
 	u32			num_vectors;
 	u32			irq_mask[MAX_MSI_CTRLS];
+	struct pci_bus		*root_bus;
 	raw_spinlock_t		lock;
 	DECLARE_BITMAP(msi_irq_in_use, MAX_MSI_IRQS);
 };
Signature · 1 line
2.20.1
#6 lore raw
In reply to Jisheng Zhang
Quoted 23 lines · Jisheng Zhang

Currently, the PCI dwc host users don't support the remove, but nothing prevent us from supporting it. To achieve this goal, we need to ensure we can do necessary clean up work.

Changes since v3:
- add Gustavo's ACK to patch2, patch5
- remove IS_ENABLED(CONFIG_PCI_MSI) check

Changes since v2:
- address all Gustavo's comments
- add Gustavo's ACK to patch1, patch3, patch4

Changes since v1:
- address Bjorn's comments, I.E Capitalize, s/irq/IRQ/, s/msi/MSI/

Jisheng Zhang (5):
PCI: dwc: Fix dw_pcie_free_msi() if msi_irq is invalid
PCI: dwc: Free the page for MSI IRQ in dw_pcie_free_msi()
PCI: dwc: Free MSI in the error code path of dw_pcie_host_init()
PCI: dwc: Use devm_pci_alloc_host_bridge() to simplify the code
PCI: dwc: Save root bus for driver remove

Quoted 3 lines
 .../pci/controller/dwc/pcie-designware-host.c | 60 ++++++++++---------
 drivers/pci/controller/dwc/pcie-designware.h  |  2 +
 2 files changed, 34 insertions(+), 28 deletions(-)

Applied to pci/dwc for v5.2, thanks.

Lorenzo

#7 lore raw

Verified it with Tegra194 PCIe host controller driver, but had to do EXPORT_SYMBOL_GPL() for a bunch of APIs.
I'll push changes to export all those APIs as part of Tegra194 PCIe driver patch series.

Thanks
Vidya Sagar

Quoted 32 lines · Lorenzo Pieralisi
Quoted 23 lines · Jisheng Zhang

Currently, the PCI dwc host users don't support the remove, but nothing prevent us from supporting it. To achieve this goal, we need to ensure we can do necessary clean up work.

Changes since v3:
- add Gustavo's ACK to patch2, patch5
- remove IS_ENABLED(CONFIG_PCI_MSI) check

Changes since v2:
- address all Gustavo's comments
- add Gustavo's ACK to patch1, patch3, patch4

Changes since v1:
- address Bjorn's comments, I.E Capitalize, s/irq/IRQ/, s/msi/MSI/

Jisheng Zhang (5):
PCI: dwc: Fix dw_pcie_free_msi() if msi_irq is invalid
PCI: dwc: Free the page for MSI IRQ in dw_pcie_free_msi()
PCI: dwc: Free MSI in the error code path of dw_pcie_host_init()
PCI: dwc: Use devm_pci_alloc_host_bridge() to simplify the code
PCI: dwc: Save root bus for driver remove

Quoted 3 lines
  .../pci/controller/dwc/pcie-designware-host.c | 60 ++++++++++---------
  drivers/pci/controller/dwc/pcie-designware.h  |  2 +
  2 files changed, 34 insertions(+), 28 deletions(-)

Applied to pci/dwc for v5.2, thanks.

Lorenzo