Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

MIPS: consolidate the swiotlb implementations

Octeon and Loongson share exactly the same code, move it into a common
implementation, and use that implementation directly from get_arch_dma_ops.

Also provide the expected dma-direct.h helpers directly instead of
delegating to platform dma-coherence.h headers.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Patchwork: https://patchwork.linux-mips.org/patch/19534/
Signed-off-by: Paul Burton <paul.burton@mips.com>
Cc: Florian Fainelli <f.fainelli@gmail.com>
Cc: David Daney <david.daney@cavium.com>
Cc: Kevin Cernekee <cernekee@gmail.com>
Cc: Jiaxun Yang <jiaxun.yang@flygoat.com>
Cc: Tom Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Huacai Chen <chenhc@lemote.com>
Cc: iommu@lists.linux-foundation.org
Cc: linux-mips@linux-mips.org

authored by

Christoph Hellwig and committed by
Paul Burton
f6d302e3 e799de3e

+84 -153
-61
arch/mips/cavium-octeon/dma-octeon.c
··· 11 11 * Copyright (C) 2010 Cavium Networks, Inc. 12 12 */ 13 13 #include <linux/dma-direct.h> 14 - #include <linux/scatterlist.h> 15 14 #include <linux/bootmem.h> 16 15 #include <linux/swiotlb.h> 17 16 #include <linux/types.h> ··· 168 169 } 169 170 #endif /* CONFIG_PCI */ 170 171 171 - static dma_addr_t octeon_dma_map_page(struct device *dev, struct page *page, 172 - unsigned long offset, size_t size, enum dma_data_direction direction, 173 - unsigned long attrs) 174 - { 175 - dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size, 176 - direction, attrs); 177 - mb(); 178 - 179 - return daddr; 180 - } 181 - 182 - static int octeon_dma_map_sg(struct device *dev, struct scatterlist *sg, 183 - int nents, enum dma_data_direction direction, unsigned long attrs) 184 - { 185 - int r = swiotlb_map_sg_attrs(dev, sg, nents, direction, attrs); 186 - mb(); 187 - return r; 188 - } 189 - 190 - static void octeon_dma_sync_single_for_device(struct device *dev, 191 - dma_addr_t dma_handle, size_t size, enum dma_data_direction direction) 192 - { 193 - swiotlb_sync_single_for_device(dev, dma_handle, size, direction); 194 - mb(); 195 - } 196 - 197 - static void octeon_dma_sync_sg_for_device(struct device *dev, 198 - struct scatterlist *sg, int nelems, enum dma_data_direction direction) 199 - { 200 - swiotlb_sync_sg_for_device(dev, sg, nelems, direction); 201 - mb(); 202 - } 203 - 204 - static void *octeon_dma_alloc_coherent(struct device *dev, size_t size, 205 - dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) 206 - { 207 - void *ret = swiotlb_alloc(dev, size, dma_handle, gfp, attrs); 208 - 209 - mb(); 210 - 211 - return ret; 212 - } 213 - 214 172 dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr) 215 173 { 216 174 #ifdef CONFIG_PCI ··· 185 229 #endif 186 230 return daddr; 187 231 } 188 - 189 - static const struct dma_map_ops octeon_swiotlb_ops = { 190 - .alloc = octeon_dma_alloc_coherent, 191 - .free = swiotlb_free, 192 - .map_page = octeon_dma_map_page, 193 - .unmap_page = swiotlb_unmap_page, 194 - .map_sg = octeon_dma_map_sg, 195 - .unmap_sg = swiotlb_unmap_sg_attrs, 196 - .sync_single_for_cpu = swiotlb_sync_single_for_cpu, 197 - .sync_single_for_device = octeon_dma_sync_single_for_device, 198 - .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu, 199 - .sync_sg_for_device = octeon_dma_sync_sg_for_device, 200 - .mapping_error = swiotlb_dma_mapping_error, 201 - .dma_supported = swiotlb_dma_supported 202 - }; 203 232 204 233 char *octeon_swiotlb; 205 234 ··· 248 307 249 308 if (swiotlb_init_with_tbl(octeon_swiotlb, swiotlb_nslabs, 1) == -ENOMEM) 250 309 panic("Cannot allocate SWIOTLB buffer"); 251 - 252 - mips_dma_map_ops = &octeon_swiotlb_ops; 253 310 }
+16 -1
arch/mips/include/asm/dma-direct.h
··· 1 - #include <asm/dma-coherence.h> 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef _MIPS_DMA_DIRECT_H 3 + #define _MIPS_DMA_DIRECT_H 1 4 + 5 + static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) 6 + { 7 + if (!dev->dma_mask) 8 + return false; 9 + 10 + return addr + size - 1 <= *dev->dma_mask; 11 + } 12 + 13 + dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr); 14 + phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr); 15 + 16 + #endif /* _MIPS_DMA_DIRECT_H */
+5
arch/mips/include/asm/dma-mapping.h
··· 11 11 #endif 12 12 13 13 extern const struct dma_map_ops *mips_dma_map_ops; 14 + extern const struct dma_map_ops mips_swiotlb_ops; 14 15 15 16 static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus) 16 17 { 18 + #ifdef CONFIG_SWIOTLB 19 + return &mips_swiotlb_ops; 20 + #else 17 21 return mips_dma_map_ops; 22 + #endif 18 23 } 19 24 20 25 #define arch_setup_dma_ops arch_setup_dma_ops
-11
arch/mips/include/asm/mach-cavium-octeon/dma-coherence.h
··· 61 61 { 62 62 } 63 63 64 - static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) 65 - { 66 - if (!dev->dma_mask) 67 - return false; 68 - 69 - return addr + size - 1 <= *dev->dma_mask; 70 - } 71 - 72 - dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr); 73 - phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr); 74 - 75 64 extern char *octeon_swiotlb; 76 65 77 66 #endif /* __ASM_MACH_CAVIUM_OCTEON_DMA_COHERENCE_H */
-10
arch/mips/include/asm/mach-loongson64/dma-coherence.h
··· 17 17 18 18 struct device; 19 19 20 - static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) 21 - { 22 - if (!dev->dma_mask) 23 - return false; 24 - 25 - return addr + size - 1 <= *dev->dma_mask; 26 - } 27 - 28 - extern dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr); 29 - extern phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr); 30 20 static inline dma_addr_t plat_map_dma_mem(struct device *dev, void *addr, 31 21 size_t size) 32 22 {
+1 -70
arch/mips/loongson64/common/dma-swiotlb.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 - #include <linux/mm.h> 2 + #include <linux/dma-direct.h> 3 3 #include <linux/init.h> 4 - #include <linux/dma-mapping.h> 5 - #include <linux/scatterlist.h> 6 4 #include <linux/swiotlb.h> 7 - #include <linux/bootmem.h> 8 - 9 - #include <asm/bootinfo.h> 10 - #include <boot_param.h> 11 - #include <dma-coherence.h> 12 - 13 - static void *loongson_dma_alloc_coherent(struct device *dev, size_t size, 14 - dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) 15 - { 16 - void *ret = swiotlb_alloc(dev, size, dma_handle, gfp, attrs); 17 - 18 - mb(); 19 - return ret; 20 - } 21 - 22 - static dma_addr_t loongson_dma_map_page(struct device *dev, struct page *page, 23 - unsigned long offset, size_t size, 24 - enum dma_data_direction dir, 25 - unsigned long attrs) 26 - { 27 - dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size, 28 - dir, attrs); 29 - mb(); 30 - return daddr; 31 - } 32 - 33 - static int loongson_dma_map_sg(struct device *dev, struct scatterlist *sg, 34 - int nents, enum dma_data_direction dir, 35 - unsigned long attrs) 36 - { 37 - int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, attrs); 38 - mb(); 39 - 40 - return r; 41 - } 42 - 43 - static void loongson_dma_sync_single_for_device(struct device *dev, 44 - dma_addr_t dma_handle, size_t size, 45 - enum dma_data_direction dir) 46 - { 47 - swiotlb_sync_single_for_device(dev, dma_handle, size, dir); 48 - mb(); 49 - } 50 - 51 - static void loongson_dma_sync_sg_for_device(struct device *dev, 52 - struct scatterlist *sg, int nents, 53 - enum dma_data_direction dir) 54 - { 55 - swiotlb_sync_sg_for_device(dev, sg, nents, dir); 56 - mb(); 57 - } 58 5 59 6 dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr) 60 7 { ··· 27 80 return daddr; 28 81 } 29 82 30 - static const struct dma_map_ops loongson_dma_map_ops = { 31 - .alloc = loongson_dma_alloc_coherent, 32 - .free = swiotlb_free, 33 - .map_page = loongson_dma_map_page, 34 - .unmap_page = swiotlb_unmap_page, 35 - .map_sg = loongson_dma_map_sg, 36 - .unmap_sg = swiotlb_unmap_sg_attrs, 37 - .sync_single_for_cpu = swiotlb_sync_single_for_cpu, 38 - .sync_single_for_device = loongson_dma_sync_single_for_device, 39 - .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu, 40 - .sync_sg_for_device = loongson_dma_sync_sg_for_device, 41 - .mapping_error = swiotlb_dma_mapping_error, 42 - .dma_supported = swiotlb_dma_supported, 43 - }; 44 - 45 83 void __init plat_swiotlb_setup(void) 46 84 { 47 85 swiotlb_init(1); 48 - mips_dma_map_ops = &loongson_dma_map_ops; 49 86 }
+1
arch/mips/mm/Makefile
··· 17 17 obj-$(CONFIG_64BIT) += pgtable-64.o 18 18 obj-$(CONFIG_HIGHMEM) += highmem.o 19 19 obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o 20 + obj-$(CONFIG_SWIOTLB) += dma-swiotlb.o 20 21 21 22 obj-$(CONFIG_CPU_R4K_CACHE_TLB) += c-r4k.o cex-gen.o tlb-r4k.o 22 23 obj-$(CONFIG_CPU_R3000) += c-r3k.o tlb-r3k.o
+61
arch/mips/mm/dma-swiotlb.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #include <linux/dma-mapping.h> 3 + #include <linux/swiotlb.h> 4 + 5 + static void *mips_swiotlb_alloc(struct device *dev, size_t size, 6 + dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) 7 + { 8 + void *ret = swiotlb_alloc(dev, size, dma_handle, gfp, attrs); 9 + 10 + mb(); 11 + return ret; 12 + } 13 + 14 + static dma_addr_t mips_swiotlb_map_page(struct device *dev, 15 + struct page *page, unsigned long offset, size_t size, 16 + enum dma_data_direction dir, unsigned long attrs) 17 + { 18 + dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size, 19 + dir, attrs); 20 + mb(); 21 + return daddr; 22 + } 23 + 24 + static int mips_swiotlb_map_sg(struct device *dev, struct scatterlist *sg, 25 + int nents, enum dma_data_direction dir, unsigned long attrs) 26 + { 27 + int r = swiotlb_map_sg_attrs(dev, sg, nents, dir, attrs); 28 + mb(); 29 + 30 + return r; 31 + } 32 + 33 + static void mips_swiotlb_sync_single_for_device(struct device *dev, 34 + dma_addr_t dma_handle, size_t size, enum dma_data_direction dir) 35 + { 36 + swiotlb_sync_single_for_device(dev, dma_handle, size, dir); 37 + mb(); 38 + } 39 + 40 + static void mips_swiotlb_sync_sg_for_device(struct device *dev, 41 + struct scatterlist *sg, int nents, enum dma_data_direction dir) 42 + { 43 + swiotlb_sync_sg_for_device(dev, sg, nents, dir); 44 + mb(); 45 + } 46 + 47 + const struct dma_map_ops mips_swiotlb_ops = { 48 + .alloc = mips_swiotlb_alloc, 49 + .free = swiotlb_free, 50 + .map_page = mips_swiotlb_map_page, 51 + .unmap_page = swiotlb_unmap_page, 52 + .map_sg = mips_swiotlb_map_sg, 53 + .unmap_sg = swiotlb_unmap_sg_attrs, 54 + .sync_single_for_cpu = swiotlb_sync_single_for_cpu, 55 + .sync_single_for_device = mips_swiotlb_sync_single_for_device, 56 + .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu, 57 + .sync_sg_for_device = mips_swiotlb_sync_sg_for_device, 58 + .mapping_error = swiotlb_dma_mapping_error, 59 + .dma_supported = swiotlb_dma_supported, 60 + }; 61 + EXPORT_SYMBOL(mips_swiotlb_ops);