Skip to contentSkip to conversation

PCI: dwc: Support remove

PATCH Superseded Jisheng Zhang 3 participants 12 messages Mar 18, 2019 – Mar 29, 2019

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 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 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 | 59 ++++++++++---------
 drivers/pci/controller/dwc/pcie-designware.h  |  2 +
 2 files changed, 34 insertions(+), 27 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH v3 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 v3 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
Acked-by on v3 2/5
#3 lore raw

Re: [PATCH v3 2/5] PCI: dwc: Free the page for MSI IRQ in dw_pcie_free_msi()

Hi,

Quoted 48 lines · 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 +0 -0
index 377f4c0b52da..6fb0a1879932 100644

drivers/pci/controller/dwc/pcie-designware.h struct pcie_port { · line 185

Quoted 7 lines
drivers/pci/controller/dwc/pcie-designware.h +1 -0
@@ -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;
#4 lore raw

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.

---
 drivers/pci/controller/dwc/pcie-designware-host.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +6 -2
index a71b874ae3c0..585080699675 100644
@@ -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,10 @@ int dw_pcie_host_init(struct pcie_port *pp)
 	pci_bus_add_devices(bus);
 	return 0;
 
+err_free_msi:
+	if (IS_ENABLED(CONFIG_PCI_MSI) && 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

Re: PCI: dwc: Free MSI in the error code path of dw_pcie_host_init()

Quoted 2 lines · On Mon, 18 Mar 2019 17:47:49 +0800 Jisheng Zhang wrote:

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

Sorry, "PATCH" is missing in the email title, please ignore this one

#6 lore raw

PATCH v3 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.

---
 drivers/pci/controller/dwc/pcie-designware-host.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +6 -2
index a71b874ae3c0..585080699675 100644
@@ -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,10 @@ int dw_pcie_host_init(struct pcie_port *pp)
 	pci_bus_add_devices(bus);
 	return 0;
 
+err_free_msi:
+	if (IS_ENABLED(CONFIG_PCI_MSI) && 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
#7 lore raw

Re: [PATCH v3 3/5] PCI: dwc: Free MSI in the error code path of dw_pcie_host_init()

Quoted 32 lines · Jisheng Zhang

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

---
  drivers/pci/controller/dwc/pcie-designware-host.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +2 -2
index a71b874ae3c0..585080699675 100644
@@ -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;
 

drivers/pci/controller/dwc/pcie-designware-host.c int dw_pcie_host_init(struct pcie_port *pp) · line 517

Quoted 5 lines
drivers/pci/controller/dwc/pcie-designware-host.c +2 -0
@@ -513,6 +513,10 @@ int dw_pcie_host_init(struct pcie_port *pp)
  	pci_bus_add_devices(bus);
  	return 0;
 
+err_free_msi:
+	if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_enabled() &&

pci_msi_enabled() already has a stub for !CONFIG_PCI_MSI, so you shouldn't need an explicit IS_ENABLED() here.

Robin.

Quoted 6 lines
drivers/pci/controller/dwc/pcie-designware-host.c +2 -0
+			!pp->ops->msi_host_init)
+		dw_pcie_free_msi(pp);
  error:
  	pci_free_host_bridge(bridge);
  	return ret;
#8 lore raw

Re: [PATCH v3 3/5] PCI: dwc: Free MSI in the error code path of dw_pcie_host_init()

Quoted 38 lines · On Thu, 28 Mar 2019 17:08:43 +0000 Robin Murphy wrote:
Quoted 35 lines · Jisheng Zhang

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

---
  drivers/pci/controller/dwc/pcie-designware-host.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/pci/controller/dwc/pcie-designware-host.c +2 -2
index a71b874ae3c0..585080699675 100644
@@ -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,10 @@ int dw_pcie_host_init(struct pcie_port *pp)
      pci_bus_add_devices(bus);
      return 0;
 
Quoted 5 lines

drivers/pci/controller/dwc/pcie-designware-host.c int dw_pcie_host_init(struct pcie_port *pp) · line 517

Quoted 2 lines
drivers/pci/controller/dwc/pcie-designware-host.c +2 -0
+err_free_msi:
+     if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_enabled() &&  

pci_msi_enabled() already has a stub for !CONFIG_PCI_MSI, so you shouldn't need an explicit IS_ENABLED() here.

Indeed, thanks very much. updated in V4

Thanks

#9 lore raw

PATCH v3 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 585080699675..68a9dac23b1b 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;
 		}
 	}
 
@@ -517,8 +514,6 @@ int dw_pcie_host_init(struct pcie_port *pp)
 	if (IS_ENABLED(CONFIG_PCI_MSI) && 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
#10 lore raw

PATCH v3 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 68a9dac23b1b..7e9dad7ba056 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
Acked-by on v3 5/5
#11 lore raw

Re: [PATCH v3 5/5] PCI: dwc: Save root bus for driver remove

Hi,

Quoted 64 lines · 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 68a9dac23b1b..7e9dad7ba056 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 +0 -0
index 6fb0a1879932..adff0c713665 100644

drivers/pci/controller/dwc/pcie-designware.h struct pcie_port { · line 188

Quoted 7 lines
drivers/pci/controller/dwc/pcie-designware.h +1 -0
@@ -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);
 };