History

Merge tag 'arm-swiotlb-5.3' of git://git.infradead.org/users/hch/dma-mapping

Pull arm swiotlb support from Christoph Hellwig: "This fixes a cascade of regressions that originally started with the addition of the ia64 port, but only got fatal once we removed most uses of block layer bounce buffering in Linux 4.18. The reason is that while the original i386/PAE code that was the first architecture that supported > 4GB of memory without an iommu decided to leave bounce buffering to the subsystems, which in those days just mean block and networking as no one else consumed arbitrary userspace memory. Later with ia64, x86_64 and other ports we assumed that either an iommu or something that fakes it up ("software IOTLB" in beautiful Intel speak) is present and that subsystems can rely on that for dealing with addressing limitations in devices. Except that the ARM LPAE scheme that added larger physical address to 32-bit ARM did not follow that scheme and thus only worked by chance and only for block and networking I/O directly to highmem. Long story, short fix - add swiotlb support to arm when build for LPAE platforms, which actuallys turns out to be pretty trivial with the modern dma-direct / swiotlb code to fix the Linux 4.18-ish regression" * tag 'arm-swiotlb-5.3' of git://git.infradead.org/users/hch/dma-mapping: arm: use swiotlb for bounce buffering on LPAE configs dma-mapping: check pfn validity in dma_common_{mmap,get_sgtable}

Linus Torvalds committed 7 years ago

Commit 234172f2 parents 35fca9f ad3c7b15 files · +85 −3Browse files

Unified · Split

5 files
  1. arch/arm/include/asm/dma-mapping.h +3 −1
  2. arch/arm/mm/Kconfig +5 −0
  3. arch/arm/mm/dma-mapping.c +61 −0
  4. arch/arm/mm/init.c +5 −0
  5. kernel/dma/mapping.c +11 −2
arch/arm/include/asm/dma-mapping.h+3−1
@@ -18,7 +18,9 @@ extern const struct dma_map_ops arm_coherent_dma_ops; · 17 unmodified lines
1818
1919 static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
2020 {
21− return IS_ENABLED(CONFIG_MMU) ? &arm_dma_ops : NULL;
21+ if (IS_ENABLED(CONFIG_MMU) && !IS_ENABLED(CONFIG_ARM_LPAE))
22+ return &arm_dma_ops;
23+ return NULL;
2224 }
2325
2426 #ifdef __arch_page_to_dma
arch/arm/mm/Kconfig+5−0
@@ -663,6 +663,11 @@ config ARM_LPAE · 662 unmodified lines
663663 depends on MMU && CPU_32v7 && !CPU_32v6 && !CPU_32v5 && \
664664 !CPU_32v4 && !CPU_32v3
665665 select PHYS_ADDR_T_64BIT
666+ select SWIOTLB
667+ select ARCH_HAS_DMA_COHERENT_TO_PFN
668+ select ARCH_HAS_DMA_MMAP_PGPROT
669+ select ARCH_HAS_SYNC_DMA_FOR_DEVICE
670+ select ARCH_HAS_SYNC_DMA_FOR_CPU
666671 help
667672 Say Y if you have an ARMv7 processor supporting the LPAE page
668673 table format and you would like to access memory beyond the
arch/arm/mm/dma-mapping.c+61−0
@@ -15,6 +15,7 @@ · 14 unmodified lines
1515 #include <linux/init.h>
1616 #include <linux/device.h>
1717 #include <linux/dma-mapping.h>
18+#include <linux/dma-noncoherent.h>
1819 #include <linux/dma-contiguous.h>
1920 #include <linux/highmem.h>
2021 #include <linux/memblock.h>
@@ -1125,6 +1126,19 @@ int arm_dma_supported(struct device *dev, u64 mask) · 1104 unmodified lines
11251126
11261127 static const struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
11271128 {
1129+ /*
1130+ * When CONFIG_ARM_LPAE is set, physical address can extend above
1131+ * 32-bits, which then can't be addressed by devices that only support
1132+ * 32-bit DMA.
1133+ * Use the generic dma-direct / swiotlb ops code in that case, as that
1134+ * handles bounce buffering for us.
1135+ *
1136+ * Note: this checks CONFIG_ARM_LPAE instead of CONFIG_SWIOTLB as the
1137+ * latter is also selected by the Xen code, but that code for now relies
1138+ * on non-NULL dev_dma_ops. To be cleaned up later.
1139+ */
1140+ if (IS_ENABLED(CONFIG_ARM_LPAE))
1141+ return NULL;
11281142 return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
11291143 }
11301144
@@ -2329,6 +2343,9 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · 1198 unmodified lines
23292343 const struct dma_map_ops *dma_ops;
23302344
23312345 dev->archdata.dma_coherent = coherent;
2346+#ifdef CONFIG_SWIOTLB
2347+ dev->dma_coherent = coherent;
2348+#endif
23322349
23332350 /*
23342351 * Don't override the dma_ops if they have already been set. Ideally
@@ -2363,3 +2380,47 @@ void arch_teardown_dma_ops(struct device *dev) · 28 unmodified lines
23632380 /* Let arch_setup_dma_ops() start again from scratch upon re-probe */
23642381 set_dma_ops(dev, NULL);
23652382 }
2383+
2384+#ifdef CONFIG_SWIOTLB
2385+void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
2386+ size_t size, enum dma_data_direction dir)
2387+{
2388+ __dma_page_cpu_to_dev(phys_to_page(paddr), paddr & (PAGE_SIZE - 1),
2389+ size, dir);
2390+}
2391+
2392+void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
2393+ size_t size, enum dma_data_direction dir)
2394+{
2395+ __dma_page_dev_to_cpu(phys_to_page(paddr), paddr & (PAGE_SIZE - 1),
2396+ size, dir);
2397+}
2398+
2399+long arch_dma_coherent_to_pfn(struct device *dev, void *cpu_addr,
2400+ dma_addr_t dma_addr)
2401+{
2402+ return dma_to_pfn(dev, dma_addr);
2403+}
2404+
2405+pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot,
2406+ unsigned long attrs)
2407+{
2408+ if (!dev_is_dma_coherent(dev))
2409+ return __get_dma_pgprot(attrs, prot);
2410+ return prot;
2411+}
2412+
2413+void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
2414+ gfp_t gfp, unsigned long attrs)
2415+{
2416+ return __dma_alloc(dev, size, dma_handle, gfp,
2417+ __get_dma_pgprot(attrs, PAGE_KERNEL), false,
2418+ attrs, __builtin_return_address(0));
2419+}
2420+
2421+void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
2422+ dma_addr_t dma_handle, unsigned long attrs)
2423+{
2424+ __arm_dma_free(dev, size, cpu_addr, dma_handle, attrs, false);
2425+}
2426+#endif /* CONFIG_SWIOTLB */
arch/arm/mm/init.c+5−0
@@ -21,6 +21,7 @@ · 20 unmodified lines
2121 #include <linux/dma-contiguous.h>
2222 #include <linux/sizes.h>
2323 #include <linux/stop_machine.h>
24+#include <linux/swiotlb.h>
2425
2526 #include <asm/cp15.h>
2627 #include <asm/mach-types.h>
@@ -463,6 +464,10 @@ static void __init free_highpages(void) · 436 unmodified lines
463464 */
464465 void __init mem_init(void)
465466 {
467+#ifdef CONFIG_ARM_LPAE
468+ swiotlb_init(1);
469+#endif
470+
466471 set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
467472
468473 /* this will put all unused low memory onto the freelists */
kernel/dma/mapping.c+11−2
@@ -116,11 +116,16 @@ int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt, · 115 unmodified lines
116116 int ret;
117117
118118 if (!dev_is_dma_coherent(dev)) {
119+ unsigned long pfn;
120+
119121 if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN))
120122 return -ENXIO;
121123
122− page = pfn_to_page(arch_dma_coherent_to_pfn(dev, cpu_addr,
123− dma_addr));
124+ /* If the PFN is not valid, we do not have a struct page */
125+ pfn = arch_dma_coherent_to_pfn(dev, cpu_addr, dma_addr);
126+ if (!pfn_valid(pfn))
127+ return -ENXIO;
128+ page = pfn_to_page(pfn);
124129 } else {
125130 page = virt_to_page(cpu_addr);
126131 }
@@ -170,7 +175,11 @@ int dma_common_mmap(struct device *dev, struct vm_area_struct *vma, · 43 unmodified lines
170175 if (!dev_is_dma_coherent(dev)) {
171176 if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN))
172177 return -ENXIO;
178+
179+ /* If the PFN is not valid, we do not have a struct page */
173180 pfn = arch_dma_coherent_to_pfn(dev, cpu_addr, dma_addr);
181+ if (!pfn_valid(pfn))
182+ return -ENXIO;
174183 } else {
175184 pfn = page_to_pfn(virt_to_page(cpu_addr));
176185 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t