Skip to contentSkip to conversation

x86: Get rid of custom DMA functions

PATCH In review Nicolas Saenz Julienne 3 participants 5 messages Nov 7, 2019 – Nov 14, 2019

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

v3 cover x86: Get rid of custom DMA functions

sta2x11 is the only x86 device that depends custom DMA direct functions. It turns out it can be made standard by carefully setting the device's DMA masks and offset.

Originally only patch #2 was sent but I realised patch #1 is also needed, which is a good addition as it's also a prerequisite to get proper DMA support on the Raspberry Pi 4[1].

[1] https://lkml.org/lkml/2019/10/15/523

---

Changes since v2:
- cleanup dma-direct.h

Changes since v1:
- Small cleanups in sta2x11-fixup.x
- add patch checking DMA addresses lower bounds

Nicolas Saenz Julienne (2):
dma-direct: check for overflows on 32 bit DMA addresses x86/PCI: sta2x11: use default DMA address translation

 arch/x86/Kconfig                  |   1 -
 arch/x86/include/asm/device.h     |   3 -
 arch/x86/include/asm/dma-direct.h |   9 --
 arch/x86/pci/sta2x11-fixup.c      | 135 ++++++------------------------
 include/linux/dma-direct.h        |  12 ++-
 5 files changed, 36 insertions(+), 124 deletions(-)
 delete mode 100644 arch/x86/include/asm/dma-direct.h
Signature · 1 line
2.23.0
#1 lore raw

PATCH v3 1/2 dma-direct: check for overflows on 32 bit DMA addresses

As seen on the new Raspberry Pi 4 and sta2x11's DMA implementation it is possible for a device configured with 32 bit DMA addresses and a partial DMA mapping located at the end of the address space to overflow. It happens when a higher physical address, not DMAable, is translated to it's DMA counterpart.

For example the Raspberry Pi 4, configurable up to 4 GB of memory, has an interconnect capable of addressing the lower 1 GB of physical memory with a DMA offset of 0xc0000000. It transpires that, any attempt to translate physical addresses higher than the first GB will result in an overflow which dma_capable() can't detect as it only checks for addresses bigger then the maximum allowed DMA address.

Fix this by verifying in dma_capable() if the DMA address range provided is at any point lower than the minimum possible DMA address on the bus.

---

Changes since v2:
- Cleanup code: use IS_ENABLED, a tmp variable for end, and

    use phys_to_dma().
 include/linux/dma-direct.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
include/linux/dma-direct.h +10 -2
index adf993a3bd58..6a18a97b76a8 100644
@@ -3,8 +3,11 @@
 #define _LINUX_DMA_DIRECT_H 1
 
 #include <linux/dma-mapping.h>
+#include <linux/memblock.h> /* for min_low_pfn */
 #include <linux/mem_encrypt.h>
 
+static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr);
+
 #ifdef CONFIG_ARCH_HAS_PHYS_TO_DMA
 #include <asm/dma-direct.h>
 #else
@@ -24,11 +27,16 @@ static inline phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t dev_addr)
 
 static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
 {
+	dma_addr_t end = addr + size - 1;
+
 	if (!dev->dma_mask)
 		return false;
 
-	return addr + size - 1 <=
-		min_not_zero(*dev->dma_mask, dev->bus_dma_mask);
+	if (!IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT) &&
+	    min(addr, end) < phys_to_dma(dev, PFN_PHYS(min_low_pfn)))
+		return false;
+
+	return end <= min_not_zero(*dev->dma_mask, dev->bus_dma_mask);
 }
 #endif /* !CONFIG_ARCH_HAS_PHYS_TO_DMA */
 
Signature · 1 line
2.23.0
#2 lore raw

PATCH v3 2/2 x86/PCI: sta2x11: use default DMA address translation

In reply to 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.

---

Changed since v1:
- use variable to avoid recalculating AMBA's max address
- remove x86's dma-direct.h as it's not longer needed

 arch/x86/Kconfig                  |   1 -
 arch/x86/include/asm/device.h     |   3 -
 arch/x86/include/asm/dma-direct.h |   9 --
 arch/x86/pci/sta2x11-fixup.c      | 135 ++++++------------------------
 4 files changed, 26 insertions(+), 122 deletions(-)
 delete mode 100644 arch/x86/include/asm/dma-direct.h
arch/x86/Kconfig +0 -1
index 45699e458057..d67bdf00e2ab 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/include/asm/dma-direct.h deleted +0 -9
deleted file mode 100644
index 1a19251eaac9..000000000000
@@ -1,9 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-#ifndef ASM_X86_DMA_DIRECT_H
-#define ASM_X86_DMA_DIRECT_H 1
-
-bool dma_capable(struct device *dev, dma_addr_t addr, size_t size);
-dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr);
-phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr);
-
-#endif /* ASM_X86_DMA_DIRECT_H */
arch/x86/pci/sta2x11-fixup.c +26 -109

185 lines not on this page · raw message

Signature · 1 line
2.23.0
maintainer
#3 lore raw

Re: [PATCH v3 2/2] x86/PCI: sta2x11: use default DMA address translation

Quoted 23 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.

---

Changed since v1:
- use variable to avoid recalculating AMBA's max address
- remove x86's dma-direct.h as it's not longer needed

Quoted 6 lines
 arch/x86/Kconfig                  |   1 -
 arch/x86/include/asm/device.h     |   3 -
 arch/x86/include/asm/dma-direct.h |   9 --
 arch/x86/pci/sta2x11-fixup.c      | 135 ++++++------------------------
 4 files changed, 26 insertions(+), 122 deletions(-)
 delete mode 100644 arch/x86/include/asm/dma-direct.h

Ok, I have only 2/2 in my mbox so in the future, when sending a whole set, make sure you Cc everybody on all the patches so that people can see the whole thing.

Then, I went and read back all the discussion about this cleanup and how it is hard to test it because it is not in PCs but in automotive installations...

Long story, short, I like patches with negative diffstats :) so I could take it through tip unless Christoph has different plans for this.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#4 lore raw

Re: [PATCH v3 2/2] x86/PCI: sta2x11: use default DMA address translation

Quoted 10 lines · Borislav Petkov

Ok, I have only 2/2 in my mbox so in the future, when sending a whole set, make sure you Cc everybody on all the patches so that people can see the whole thing.

Then, I went and read back all the discussion about this cleanup and how it is hard to test it because it is not in PCs but in automotive installations...

Long story, short, I like patches with negative diffstats :) so I could take it through tip unless Christoph has different plans for this.

I've already queued this up in the dma-mapping tree.