2019-02-26 11:57:16

by Jisheng Zhang

[permalink] [raw]
Subject: [PATCH 0/5] 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.

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 | 38 ++++++++++---------
drivers/pci/controller/dwc/pcie-designware.h | 2 +
2 files changed, 23 insertions(+), 17 deletions(-)

--
2.20.1



2019-02-26 11:57:04

by Jisheng Zhang

[permalink] [raw]
Subject: [PATCH 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().

Signed-off-by: Jisheng Zhang <[email protected]>
---
drivers/pci/controller/dwc/pcie-designware-host.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/controller/dwc/pcie-designware-host.c b/drivers/pci/controller/dwc/pcie-designware-host.c
index 0c18ab63811f..a94d3530b694 100644
--- a/drivers/pci/controller/dwc/pcie-designware-host.c
+++ b/drivers/pci/controller/dwc/pcie-designware-host.c
@@ -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);
--
2.20.1


2019-02-26 11:58:26

by Jisheng Zhang

[permalink] [raw]
Subject: [PATCH 3/5] PCI: dwc: free msi in the error code path of dw_pcie_host_init()

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

Signed-off-by: Jisheng Zhang <[email protected]>
---
drivers/pci/controller/dwc/pcie-designware-host.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/controller/dwc/pcie-designware-host.c b/drivers/pci/controller/dwc/pcie-designware-host.c
index abe3ff5f0867..66569d0f3ab9 100644
--- a/drivers/pci/controller/dwc/pcie-designware-host.c
+++ b/drivers/pci/controller/dwc/pcie-designware-host.c
@@ -482,7 +482,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;
@@ -496,7 +496,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;

@@ -512,6 +512,9 @@ 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) && !pp->ops->msi_host_init)
+ dw_pcie_free_msi(pp);
error:
pci_free_host_bridge(bridge);
return ret;
--
2.20.1


2019-02-26 11:59:11

by Jisheng Zhang

[permalink] [raw]
Subject: [PATCH 4/5] PCI: dwc: use devm_pci_alloc_host_bridge() to simplify the code

Use devm_pci_alloc_host_bridge() to simplify the error code path.

Signed-off-by: Jisheng Zhang <[email protected]>
---
.../pci/controller/dwc/pcie-designware-host.c | 21 +++++++------------
1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/drivers/pci/controller/dwc/pcie-designware-host.c b/drivers/pci/controller/dwc/pcie-designware-host.c
index 66569d0f3ab9..4831c12fee93 100644
--- a/drivers/pci/controller/dwc/pcie-designware-host.c
+++ b/drivers/pci/controller/dwc/pcie-designware-host.c
@@ -357,7 +357,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;

@@ -368,7 +368,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) {
@@ -412,8 +412,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;
}
}

@@ -424,8 +423,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;
}
}

@@ -435,8 +433,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;
}
}

@@ -459,14 +456,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,
@@ -475,7 +472,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;
}
}

@@ -515,8 +512,6 @@ int dw_pcie_host_init(struct pcie_port *pp)
err_free_msi:
if (IS_ENABLED(CONFIG_PCI_MSI) && !pp->ops->msi_host_init)
dw_pcie_free_msi(pp);
-error:
- pci_free_host_bridge(bridge);
return ret;
}

--
2.20.1


2019-02-26 11:59:26

by Jisheng Zhang

[permalink] [raw]
Subject: [PATCH 2/5] PCI: dwc: free the page for MSI irq in dw_pcie_free_msi()

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

Signed-off-by: Jisheng Zhang <[email protected]>
---
drivers/pci/controller/dwc/pcie-designware-host.c | 3 +++
drivers/pci/controller/dwc/pcie-designware.h | 1 +
2 files changed, 4 insertions(+)

diff --git a/drivers/pci/controller/dwc/pcie-designware-host.c b/drivers/pci/controller/dwc/pcie-designware-host.c
index a94d3530b694..abe3ff5f0867 100644
--- a/drivers/pci/controller/dwc/pcie-designware-host.c
+++ b/drivers/pci/controller/dwc/pcie-designware-host.c
@@ -305,6 +305,8 @@ void dw_pcie_free_msi(struct pcie_port *pp)

irq_domain_remove(pp->msi_domain);
irq_domain_remove(pp->irq_domain);
+
+ __free_page(pp->msi_page);
}

void dw_pcie_msi_init(struct pcie_port *pp)
@@ -322,6 +324,7 @@ void dw_pcie_msi_init(struct pcie_port *pp)
return;
}
msi_target = (u64)pp->msi_data;
+ pp->msi_page = page;

/* Program the msi_data */
dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4,
diff --git a/drivers/pci/controller/dwc/pcie-designware.h b/drivers/pci/controller/dwc/pcie-designware.h
index f6fb65a40f10..3be7ca9f1fc3 100644
--- a/drivers/pci/controller/dwc/pcie-designware.h
+++ b/drivers/pci/controller/dwc/pcie-designware.h
@@ -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;
--
2.20.1


2019-02-26 11:59:57

by Jisheng Zhang

[permalink] [raw]
Subject: [PATCH 5/5] PCI: dwc: save root bus for driver remove

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);
...
}

Signed-off-by: Jisheng Zhang <[email protected]>
---
drivers/pci/controller/dwc/pcie-designware-host.c | 1 +
drivers/pci/controller/dwc/pcie-designware.h | 1 +
2 files changed, 2 insertions(+)

diff --git a/drivers/pci/controller/dwc/pcie-designware-host.c b/drivers/pci/controller/dwc/pcie-designware-host.c
index 4831c12fee93..ca45a4471ca0 100644
--- a/drivers/pci/controller/dwc/pcie-designware-host.c
+++ b/drivers/pci/controller/dwc/pcie-designware-host.c
@@ -496,6 +496,7 @@ int dw_pcie_host_init(struct pcie_port *pp)
goto err_free_msi;

bus = bridge->bus;
+ pp->root_bus = bus;

if (pp->ops->scan_bus)
pp->ops->scan_bus(pp);
diff --git a/drivers/pci/controller/dwc/pcie-designware.h b/drivers/pci/controller/dwc/pcie-designware.h
index 3be7ca9f1fc3..cd92ded606c6 100644
--- a/drivers/pci/controller/dwc/pcie-designware.h
+++ b/drivers/pci/controller/dwc/pcie-designware.h
@@ -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);
};
--
2.20.1


2019-02-26 21:12:12

by Bjorn Helgaas

[permalink] [raw]
Subject: Re: [PATCH 0/5] PCI: dwc: support remove

On Tue, Feb 26, 2019 at 5:55 AM Jisheng Zhang
<[email protected]> wrote:
>
> 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.
>
> 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

If you post a v2 of this series, please:

Capitalize the first word of each subject, e.g., "PCI: dwc: Fix
dw_pcie_free_msi() if msi_irq is invalid"
s/irq/IRQ/
s/msi/MSI/

2019-02-27 06:49:01

by Jisheng Zhang

[permalink] [raw]
Subject: Re: [PATCH 0/5] PCI: dwc: support remove

On Tue, 26 Feb 2019 15:11:03 -0600 Bjorn Helgaas wrote:

>
>
> On Tue, Feb 26, 2019 at 5:55 AM Jisheng Zhang
> <[email protected]> wrote:
> >
> > 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.
> >
> > 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
>
> If you post a v2 of this series, please:
>
> Capitalize the first word of each subject, e.g., "PCI: dwc: Fix
> dw_pcie_free_msi() if msi_irq is invalid"
> s/irq/IRQ/
> s/msi/MSI/

Thanks for the review. Will do when post a v2. I'm still trying to
collect more code review comments.

Thanks