Skip to contentSkip to conversation

PCI: tegra: Use the DMA-API to get the MSI address

PATCH Superseded Vidya Sagar 2 participants 3 messages Mar 12, 2019 – Mar 18, 2019

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: tegra: Use the DMA-API to get the MSI address

Since the upstream MSI memory writes are generated by downstream devices, it is logically correct to have MSI target memory coming from the DMA pool reserved for PCIe than from the general memory pool reserved for CPU access. This avoids PCIe DMA addresses coinciding with MSI target address thereby raising unwanted MSI interrupts. This patch also enforces to limit the MSI target address to 32-bits to make it work for PCIe endponits that support only 32-bit MSI target address and those that support 64-bit MSI target address anyway work with 32-bit MSI target address

---
Earlier, a different patch was sent with the subject
"PCI: tegra: Do not allocate MSI target memory"
( http://patchwork.ozlabs.org/patch/1049550/ )
to address the same issue, but since I'm going to use a different subject for this patch, I'm sending it as a different patch instead of a different version to the previous patch

 drivers/pci/controller/pci-tegra.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
drivers/pci/controller/pci-tegra.c +20 -8
index f4f53d092e00..55a1626ddb69 100644
@@ -231,8 +231,8 @@ struct tegra_msi {
 	struct msi_controller chip;
 	DECLARE_BITMAP(used, INT_PCI_MSI_NR);
 	struct irq_domain *domain;
-	unsigned long pages;
 	struct mutex lock;
+	void *virt;
 	u64 phys;
 	int irq;
 };
@@ -1536,7 +1536,7 @@ static int tegra_pcie_msi_setup(struct tegra_pcie *pcie)
 	err = platform_get_irq_byname(pdev, "msi");
 	if (err < 0) {
 		dev_err(dev, "failed to get IRQ: %d\n", err);
-		goto err;
+		goto free_irq_domain;
 	}
 
 	msi->irq = err;
@@ -1545,17 +1545,29 @@ static int tegra_pcie_msi_setup(struct tegra_pcie *pcie)
 			  tegra_msi_irq_chip.name, pcie);
 	if (err < 0) {
 		dev_err(dev, "failed to request IRQ: %d\n", err);
-		goto err;
+		goto free_irq_domain;
+	}
+
+	err = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
+	if (err < 0) {
+		dev_err(dev, "failed to set dma coherent mask: %d\n", err);
+		goto free_irq;
+	}
+
+	msi->virt = dma_alloc_coherent(dev, PAGE_SIZE, &msi->phys, GFP_KERNEL);
+	if (!msi->virt) {
+		dev_err(dev, "failed to alloc dma mem for MSI\n");
+		err = -ENOMEM;
+		goto free_irq;
 	}
 
-	/* setup AFI/FPCI range */
-	msi->pages = __get_free_pages(GFP_KERNEL, 0);
-	msi->phys = virt_to_phys((void *)msi->pages);
 	host->msi = &msi->chip;
 
 	return 0;
 
-err:
+free_irq:
+	free_irq(msi->irq, pcie);
+free_irq_domain:
 	irq_domain_remove(msi->domain);
 	return err;
 }
@@ -1592,7 +1604,7 @@ static void tegra_pcie_msi_teardown(struct tegra_pcie *pcie)
 	struct tegra_msi *msi = &pcie->msi;
 	unsigned int i, irq;
 
-	free_pages(msi->pages, 0);
+	dma_free_coherent(pcie->dev, PAGE_SIZE, msi->virt, msi->phys);
 
 	if (msi->irq > 0)
 		free_irq(msi->irq, pcie);
Signature · 1 line
2.7.4
#1 lore raw

Any one for review?
Adding Lucas Stach <[email protected]> who reviewed my previous patch where I tried to address the same issue

Quoted 88 lines · Vidya Sagar

Since the upstream MSI memory writes are generated by downstream devices, it is logically correct to have MSI target memory coming from the DMA pool reserved for PCIe than from the general memory pool reserved for CPU access. This avoids PCIe DMA addresses coinciding with MSI target address thereby raising unwanted MSI interrupts. This patch also enforces to limit the MSI target address to 32-bits to make it work for PCIe endponits that support only 32-bit MSI target address and those that support 64-bit MSI target address anyway work with 32-bit MSI target address

---
Earlier, a different patch was sent with the subject
"PCI: tegra: Do not allocate MSI target memory"
( http://patchwork.ozlabs.org/patch/1049550/ )
to address the same issue, but since I'm going to use a different subject for this patch, I'm sending it as a different patch instead of a different version to the previous patch

  drivers/pci/controller/pci-tegra.c | 28 ++++++++++++++++++++--------
  1 file changed, 20 insertions(+), 8 deletions(-)
drivers/pci/controller/pci-tegra.c +20 -8
index f4f53d092e00..55a1626ddb69 100644
@@ -231,8 +231,8 @@ struct tegra_msi {
  	struct msi_controller chip;
  	DECLARE_BITMAP(used, INT_PCI_MSI_NR);
  	struct irq_domain *domain;
-	unsigned long pages;
  	struct mutex lock;
+	void *virt;
  	u64 phys;
  	int irq;
  };
@@ -1536,7 +1536,7 @@ static int tegra_pcie_msi_setup(struct tegra_pcie *pcie)
  	err = platform_get_irq_byname(pdev, "msi");
  	if (err < 0) {
  		dev_err(dev, "failed to get IRQ: %d\n", err);
-		goto err;
+		goto free_irq_domain;
  	}
 
  	msi->irq = err;
@@ -1545,17 +1545,29 @@ static int tegra_pcie_msi_setup(struct tegra_pcie *pcie)
  			  tegra_msi_irq_chip.name, pcie);
  	if (err < 0) {
  		dev_err(dev, "failed to request IRQ: %d\n", err);
-		goto err;
+		goto free_irq_domain;
+	}
+
+	err = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
+	if (err < 0) {
+		dev_err(dev, "failed to set dma coherent mask: %d\n", err);
+		goto free_irq;
+	}
+
+	msi->virt = dma_alloc_coherent(dev, PAGE_SIZE, &msi->phys, GFP_KERNEL);
+	if (!msi->virt) {
+		dev_err(dev, "failed to alloc dma mem for MSI\n");
+		err = -ENOMEM;
+		goto free_irq;
  	}
 
-	/* setup AFI/FPCI range */
-	msi->pages = __get_free_pages(GFP_KERNEL, 0);
-	msi->phys = virt_to_phys((void *)msi->pages);
  	host->msi = &msi->chip;
 
  	return 0;
 
-err:
+free_irq:
+	free_irq(msi->irq, pcie);
+free_irq_domain:
  	irq_domain_remove(msi->domain);
  	return err;
  }
@@ -1592,7 +1604,7 @@ static void tegra_pcie_msi_teardown(struct tegra_pcie *pcie)
  	struct tegra_msi *msi = &pcie->msi;
  	unsigned int i, irq;
 
-	free_pages(msi->pages, 0);
+	dma_free_coherent(pcie->dev, PAGE_SIZE, msi->virt, msi->phys);
 
  	if (msi->irq > 0)
  		free_irq(msi->irq, pcie);
Reviewed-by Acked-by on v1 1/1
#2 lore raw
In reply to Vidya Sagar
Quoted 3 lines · Vidya Sagar

Since the upstream MSI memory writes are generated by downstream devices, it is logically correct to have MSI target memory coming from the DMA pool reserved for PCIe than from the general memory pool reserved for CPU access.

This is slightly confusing because there's no dedicated DMA pool that is reserved for PCIe. At least not upstream. That said, if there were such a pool, then, yes, we'd want the allocation to be from that pool, so I think this is okay as-is.

Quoted 3 lines

This avoids PCIe DMA addresses coinciding with MSI target address thereby raising unwanted MSI interrupts. This patch also enforces to limit the MSI target address to 32-bits to make it work for PCIe endponits that support

endpoints

Quoted 2 lines

only 32-bit MSI target address and those that support 64-bit MSI target address anyway work with 32-bit MSI target address

Fullstop at the end here.

Quoted 18 lines

---
Earlier, a different patch was sent with the subject
"PCI: tegra: Do not allocate MSI target memory"
( http://patchwork.ozlabs.org/patch/1049550/ )
to address the same issue, but since I'm going to use a different subject for this patch, I'm sending it as a different patch instead of a different version to the previous patch

 drivers/pci/controller/pci-tegra.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
drivers/pci/controller/pci-tegra.c +0 -0
index f4f53d092e00..55a1626ddb69 100644

drivers/pci/controller/pci-tegra.c struct tegra_msi { · line 236

Quoted 7 lines
drivers/pci/controller/pci-tegra.c +1 -1
@@ -231,8 +231,8 @@ struct tegra_msi {
 	struct msi_controller chip;
 	DECLARE_BITMAP(used, INT_PCI_MSI_NR);
 	struct irq_domain *domain;
-	unsigned long pages;
 	struct mutex lock;
+	void *virt;
 	u64 phys;

I think this now needs to be dma_addr_t.

Quoted 12 lines
drivers/pci/controller/pci-tegra.c +1 -1
 	int irq;
 };
@@ -1536,7 +1536,7 @@ static int tegra_pcie_msi_setup(struct tegra_pcie *pcie)
 	err = platform_get_irq_byname(pdev, "msi");
 	if (err < 0) {
 		dev_err(dev, "failed to get IRQ: %d\n", err);
-		goto err;
+		goto free_irq_domain;
 	}
 
 	msi->irq = err;

drivers/pci/controller/pci-tegra.c static int tegra_pcie_msi_setup(struct tegra_pcie *pcie) · line 1551

Quoted 8 lines
drivers/pci/controller/pci-tegra.c +4 -1
@@ -1545,17 +1545,29 @@ static int tegra_pcie_msi_setup(struct tegra_pcie *pcie)
 			  tegra_msi_irq_chip.name, pcie);
 	if (err < 0) {
 		dev_err(dev, "failed to request IRQ: %d\n", err);
-		goto err;
+		goto free_irq_domain;
+	}
+
+	err = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));

This is not technically correct because the controller supports a much wider address space (48 bits I think), so perhaps add a comment above the line to explain why you're restricting it to 32 bits here.

drivers/pci/controller/pci-tegra.c static int tegra_pcie_msi_setup(struct tegra_pcie *pcie) · line 1553

Quoted 2 lines
drivers/pci/controller/pci-tegra.c +2 -0
+	if (err < 0) {
+		dev_err(dev, "failed to set dma coherent mask: %d\n", err);

"dma" -> "DMA", please.

drivers/pci/controller/pci-tegra.c static int tegra_pcie_msi_setup(struct tegra_pcie *pcie) · line 1559

Quoted 6 lines
drivers/pci/controller/pci-tegra.c +6 -0
+		goto free_irq;
+	}
+
+	msi->virt = dma_alloc_coherent(dev, PAGE_SIZE, &msi->phys, GFP_KERNEL);
+	if (!msi->virt) {
+		dev_err(dev, "failed to alloc dma mem for MSI\n");

I'm not sure this needs an error message. The system should already warn if this happens. It does so at least for regular memory allocations. But if you want to keep it, please spell out "allocate" and "memory". Also:
"dma" -> "DMA".

Quoted 19 lines
drivers/pci/controller/pci-tegra.c +5 -4
+		err = -ENOMEM;
+		goto free_irq;
 	}
 
-	/* setup AFI/FPCI range */
-	msi->pages = __get_free_pages(GFP_KERNEL, 0);
-	msi->phys = virt_to_phys((void *)msi->pages);
 	host->msi = &msi->chip;
 
 	return 0;
 
-err:
+free_irq:
+	free_irq(msi->irq, pcie);
+free_irq_domain:
 	irq_domain_remove(msi->domain);
 	return err;
 }

drivers/pci/controller/pci-tegra.c static void tegra_pcie_msi_teardown(struct tegra_pcie *pcie) · line 1610

Quoted 8 lines
drivers/pci/controller/pci-tegra.c +1 -1
@@ -1592,7 +1604,7 @@ static void tegra_pcie_msi_teardown(struct tegra_pcie *pcie)
 	struct tegra_msi *msi = &pcie->msi;
 	unsigned int i, irq;
 
-	free_pages(msi->pages, 0);
+	dma_free_coherent(pcie->dev, PAGE_SIZE, msi->virt, msi->phys);
 
 	if (msi->irq > 0)
 		free_irq(msi->irq, pcie);

With the above issues fixed, this is: