Skip to contentSkip to conversation

x86/PCI: sta2x11: use default DMA address translation ops

PATCH In review Nicolas Saenz Julienne 2 participants 3 messages Oct 16, 2019 – Oct 18, 2019

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

PATCH 1/1 x86/PCI: sta2x11: use default DMA address translation ops

The devices found behind this PCIe chip have unusual DMA mapping constraints as there is an AMBA interconnect placed in between them and the different PCI endpoints. The offset between physical memory addresses and AMBA's view is provided by reading a PCI config register, which is saved and used whenever DMA mapping is needed.

It turns out that this DMA setup can be represented by properly setting 'dma_pfn_offset', 'dma_bus_mask' and 'dma_mask' during the PCI device enable fixup. And ultimately allows us to get rid of this device's custom DMA functions.

Aside from the code deletion and DMA setup, sta2x11_pdev_to_mapping() is moved to avoid warnings whenever CONFIG_PM is not enabled.

---

Note: This was only compile tested.

 arch/x86/Kconfig              |   1 -
 arch/x86/include/asm/device.h |   3 -
 arch/x86/pci/sta2x11-fixup.c  | 135 +++++++---------------------------
 3 files changed, 25 insertions(+), 114 deletions(-)
arch/x86/Kconfig +0 -1
index abe822d52167..9c2a51e1cb59 100644
@@ -708,7 +708,6 @@ config X86_SUPPORTS_MEMORY_FAILURE
 config STA2X11
 	bool "STA2X11 Companion Chip Support"
 	depends on X86_32_NON_STANDARD && PCI
-	select ARCH_HAS_PHYS_TO_DMA
 	select SWIOTLB
 	select MFD_STA2X11
 	select GPIOLIB
arch/x86/include/asm/device.h +0 -3
index a8f6c809d9b1..5e12c63b47aa 100644
@@ -6,9 +6,6 @@ struct dev_archdata {
 #if defined(CONFIG_INTEL_IOMMU) || defined(CONFIG_AMD_IOMMU)
 	void *iommu; /* hook for IOMMU specific extension */
 #endif
-#ifdef CONFIG_STA2X11
-	bool is_sta2x11;
-#endif
 };
 
 #if defined(CONFIG_X86_DEV_DMA_OPS) && defined(CONFIG_PCI_DOMAINS)
arch/x86/pci/sta2x11-fixup.c +25 -110

185 lines not on this page · raw message

Signature · 1 line
2.23.0
#1 lore raw

Hi Nicolas,

I'm hoping Christoph will chime in and one of the x86 guys will merge this, since I'm not a DMA expert. Trivial comments/questions below.

Quoted 16 lines · Nicolas Saenz Julienne

The devices found behind this PCIe chip have unusual DMA mapping constraints as there is an AMBA interconnect placed in between them and the different PCI endpoints. The offset between physical memory addresses and AMBA's view is provided by reading a PCI config register, which is saved and used whenever DMA mapping is needed.

It turns out that this DMA setup can be represented by properly setting 'dma_pfn_offset', 'dma_bus_mask' and 'dma_mask' during the PCI device enable fixup. And ultimately allows us to get rid of this device's custom DMA functions.

Aside from the code deletion and DMA setup, sta2x11_pdev_to_mapping() is moved to avoid warnings whenever CONFIG_PM is not enabled.

arch/x86/pci/sta2x11-fixup.c phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr) · line 143

Quoted 5 lines
arch/x86/pci/sta2x11-fixup.c +4 -1
-	pci_read_config_dword(pdev, AHB_BASE(0), &map->amba_base);
+
+	pci_read_config_dword(pdev, AHB_BASE(0), &amba_base);
+	dev->dma_pfn_offset = PFN_DOWN(-amba_base);
+	dev->bus_dma_mask = amba_base + STA2X11_AMBA_SIZE - 1;

I think of a mask as typically being one less than a power of two, but that's not the case here, e.g., STA2X11_AMBA_SIZE - 1 == 0x1fffffff (512MB-1), so if amba_size is 1G, the mask will be 0x5fffffff.

Just double-checking to be sure that's what you intend.

arch/x86/pci/sta2x11-fixup.c phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr) · line 146

Quoted 2 lines
arch/x86/pci/sta2x11-fixup.c +2 -0
+	pci_set_consistent_dma_mask(pdev, amba_base + STA2X11_AMBA_SIZE - 1);
+	pci_set_dma_mask(pdev, amba_base + STA2X11_AMBA_SIZE - 1);

Maybe add a local variable instead of repeating the "amba_base + ..." expression three times?

arch/x86/pci/sta2x11-fixup.c static void sta2x11_map_ep(struct pci_dev *pdev) · line 160

Quoted 11 lines
arch/x86/pci/sta2x11-fixup.c +2 -3
 	/* Configure AHB mapping */
 	pci_write_config_dword(pdev, AHB_PEXLBASE(0), 0);
@@ -252,14 +156,25 @@ static void sta2x11_map_ep(struct pci_dev *pdev)
 		pci_write_config_dword(pdev, AHB_CRW(i), 0);
 
 	dev_info(&pdev->dev,
-		 "sta2x11: Map EP %i: AMBA address %#8x-%#8x\n",
-		 sta2x11_pdev_to_ep(pdev),  map->amba_base,
-		 map->amba_base + STA2X11_AMBA_SIZE - 1);
+		 "sta2x11: Map EP %i: AMBA address %#8x-%#8llx\n",
+		 sta2x11_pdev_to_ep(pdev), amba_base, dev->bus_dma_mask);

This would read better as

amba_base, amba_base + STA2X11_AMBA_SIZE - 1

I know that's the same dev->bus_dma_mask, but a "mask" is not the obvious name for the end of a range.

#2 lore raw

Hi Bjorn,
thanks for having a look at it.

Quoted 31 lines · Bjorn Helgaas

Hi Nicolas,

I'm hoping Christoph will chime in and one of the x86 guys will merge this, since I'm not a DMA expert. Trivial comments/questions below.

Quoted 15 lines · Nicolas Saenz Julienne

The devices found behind this PCIe chip have unusual DMA mapping constraints as there is an AMBA interconnect placed in between them and the different PCI endpoints. The offset between physical memory addresses and AMBA's view is provided by reading a PCI config register, which is saved and used whenever DMA mapping is needed.

It turns out that this DMA setup can be represented by properly setting 'dma_pfn_offset', 'dma_bus_mask' and 'dma_mask' during the PCI device enable fixup. And ultimately allows us to get rid of this device's custom DMA functions.

Aside from the code deletion and DMA setup, sta2x11_pdev_to_mapping() is moved to avoid warnings whenever CONFIG_PM is not enabled.

arch/x86/pci/sta2x11-fixup.c phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr) · line 143

Quoted 5 lines
arch/x86/pci/sta2x11-fixup.c +4 -1
-	pci_read_config_dword(pdev, AHB_BASE(0), &map->amba_base);
+
+	pci_read_config_dword(pdev, AHB_BASE(0), &amba_base);
+	dev->dma_pfn_offset = PFN_DOWN(-amba_base);
+	dev->bus_dma_mask = amba_base + STA2X11_AMBA_SIZE - 1;

I think of a mask as typically being one less than a power of two, but that's not the case here, e.g., STA2X11_AMBA_SIZE - 1 == 0x1fffffff (512MB-1), so if amba_size is 1G, the mask will be 0x5fffffff.

Quoted 1 line

Just double-checking to be sure that's what you intend.

Yes I'm aware of it.

I know it's counter-intuitive name wise but dma-direct uses it more like a top address than a mask (see for example 'dma_capable()' in dma-direct.h). There is at least another driver using 'dev->bus_dma_mask' as such (see arch/powerpc/sysdev/fsl_pci.c).

That said, I agree it something Christoph should ratify for this to get merged.

Quoted 5 lines

arch/x86/pci/sta2x11-fixup.c phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr) · line 146

Quoted 2 lines
arch/x86/pci/sta2x11-fixup.c +2 -0
+	pci_set_consistent_dma_mask(pdev, amba_base + STA2X11_AMBA_SIZE - 1);
+	pci_set_dma_mask(pdev, amba_base + STA2X11_AMBA_SIZE - 1);

Maybe add a local variable instead of repeating the "amba_base + ..." expression three times?

Noted.

Quoted 14 lines

arch/x86/pci/sta2x11-fixup.c static void sta2x11_map_ep(struct pci_dev *pdev) · line 160

Quoted 11 lines
arch/x86/pci/sta2x11-fixup.c +2 -3
 	/* Configure AHB mapping */
 	pci_write_config_dword(pdev, AHB_PEXLBASE(0), 0);
@@ -252,14 +156,25 @@ static void sta2x11_map_ep(struct pci_dev *pdev)
 		pci_write_config_dword(pdev, AHB_CRW(i), 0);
 
 	dev_info(&pdev->dev,
-		 "sta2x11: Map EP %i: AMBA address %#8x-%#8x\n",
-		 sta2x11_pdev_to_ep(pdev),  map->amba_base,
-		 map->amba_base + STA2X11_AMBA_SIZE - 1);
+		 "sta2x11: Map EP %i: AMBA address %#8x-%#8llx\n",
+		 sta2x11_pdev_to_ep(pdev), amba_base, dev->bus_dma_mask);

This would read better as

Quoted 4 lines

amba_base, amba_base + STA2X11_AMBA_SIZE - 1

I know that's the same dev->bus_dma_mask, but a "mask" is not the obvious name for the end of a range.

Ok.

I just got a notification from my mail server that it failed to deliver the mail to Christoph, it seems I misspelled it.

I'll fix all this and send a v2.

Regards,
Nicolas