Skip to content

Commit 07940c3

Browse files
rvoskLorenzo Pieralisi
authored andcommitted
PCI: dwc: Fix MSI page leakage in suspend/resume
Currently, dw_pcie_msi_init() allocates and maps page for msi, then program the PCIE_MSI_ADDR_LO and PCIE_MSI_ADDR_HI. The Root Complex may lose power during suspend-to-RAM, so when we resume, we want to redo the latter but not the former. If designware based driver (for example, pcie-tegra194.c) calls dw_pcie_msi_init() in resume path, the msi page will be leaked. As pointed out by Rob and Ard, there's no need to allocate a page for the MSI address, we could use an address in the driver data. To avoid map the MSI msg again during resume, we move the map MSI msg from dw_pcie_msi_init() to dw_pcie_host_init(). Suggested-by: Rob Herring <robh@kernel.org> Link: https://lore.kernel.org/r/20201009155505.5a580ef5@xhacker.debian Signed-off-by: Jisheng Zhang <Jisheng.Zhang@synaptics.com> Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> Reviewed-by: Rob Herring <robh@kernel.org>
1 parent 7428332 commit 07940c3

3 files changed

Lines changed: 36 additions & 17 deletions

File tree

‎drivers/pci/controller/dwc/pci-dra7xx.c‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,9 @@ static struct irq_chip dra7xx_pci_msi_bottom_irq_chip = {
466466
static int dra7xx_pcie_msi_host_init(struct pcie_port *pp)
467467
{
468468
struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
469+
struct device *dev = pci->dev;
469470
u32 ctrl, num_ctrls;
471+
int ret;
470472

471473
pp->msi_irq_chip = &dra7xx_pci_msi_bottom_irq_chip;
472474

@@ -482,7 +484,21 @@ static int dra7xx_pcie_msi_host_init(struct pcie_port *pp)
482484
~0);
483485
}
484486

485-
return dw_pcie_allocate_domains(pp);
487+
ret = dw_pcie_allocate_domains(pp);
488+
if (ret)
489+
return ret;
490+
491+
pp->msi_data = dma_map_single_attrs(dev, &pp->msi_msg,
492+
sizeof(pp->msi_msg),
493+
DMA_FROM_DEVICE,
494+
DMA_ATTR_SKIP_CPU_SYNC);
495+
ret = dma_mapping_error(dev, pp->msi_data);
496+
if (ret) {
497+
dev_err(dev, "Failed to map MSI data\n");
498+
pp->msi_data = 0;
499+
dw_pcie_free_msi(pp);
500+
}
501+
return ret;
486502
}
487503

488504
static const struct dw_pcie_host_ops dra7xx_pcie_host_ops = {

‎drivers/pci/controller/dwc/pcie-designware-host.c‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -266,30 +266,23 @@ void dw_pcie_free_msi(struct pcie_port *pp)
266266
irq_domain_remove(pp->msi_domain);
267267
irq_domain_remove(pp->irq_domain);
268268

269-
if (pp->msi_page)
270-
__free_page(pp->msi_page);
269+
if (pp->msi_data) {
270+
struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
271+
struct device *dev = pci->dev;
272+
273+
dma_unmap_single_attrs(dev, pp->msi_data, sizeof(pp->msi_msg),
274+
DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
275+
}
271276
}
272277

273278
void dw_pcie_msi_init(struct pcie_port *pp)
274279
{
275280
struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
276-
struct device *dev = pci->dev;
277-
u64 msi_target;
281+
u64 msi_target = (u64)pp->msi_data;
278282

279283
if (!IS_ENABLED(CONFIG_PCI_MSI))
280284
return;
281285

282-
pp->msi_page = alloc_page(GFP_KERNEL);
283-
pp->msi_data = dma_map_page(dev, pp->msi_page, 0, PAGE_SIZE,
284-
DMA_FROM_DEVICE);
285-
if (dma_mapping_error(dev, pp->msi_data)) {
286-
dev_err(dev, "Failed to map MSI data\n");
287-
__free_page(pp->msi_page);
288-
pp->msi_page = NULL;
289-
return;
290-
}
291-
msi_target = (u64)pp->msi_data;
292-
293286
/* Program the msi_data */
294287
dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_LO, lower_32_bits(msi_target));
295288
dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_HI, upper_32_bits(msi_target));
@@ -394,6 +387,16 @@ int dw_pcie_host_init(struct pcie_port *pp)
394387
irq_set_chained_handler_and_data(pp->msi_irq,
395388
dw_chained_msi_isr,
396389
pp);
390+
391+
pp->msi_data = dma_map_single_attrs(pci->dev, &pp->msi_msg,
392+
sizeof(pp->msi_msg),
393+
DMA_FROM_DEVICE,
394+
DMA_ATTR_SKIP_CPU_SYNC);
395+
if (dma_mapping_error(pci->dev, pp->msi_data)) {
396+
dev_err(pci->dev, "Failed to map MSI data\n");
397+
pp->msi_data = 0;
398+
goto err_free_msi;
399+
}
397400
} else {
398401
ret = pp->ops->msi_host_init(pp);
399402
if (ret < 0)

‎drivers/pci/controller/dwc/pcie-designware.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ struct pcie_port {
190190
int msi_irq;
191191
struct irq_domain *irq_domain;
192192
struct irq_domain *msi_domain;
193+
u16 msi_msg;
193194
dma_addr_t msi_data;
194-
struct page *msi_page;
195195
struct irq_chip *msi_irq_chip;
196196
u32 num_vectors;
197197
u32 irq_mask[MAX_MSI_CTRLS];

0 commit comments

Comments
 (0)