Skip to content

Commit f6d302e

Browse files
Christoph Hellwigpaulburton
authored andcommitted
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
1 parent e799de3 commit f6d302e

File tree

8 files changed

+84
-153
lines changed

8 files changed

+84
-153
lines changed

arch/mips/cavium-octeon/dma-octeon.c

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
* Copyright (C) 2010 Cavium Networks, Inc.
1212
*/
1313
#include <linux/dma-direct.h>
14-
#include <linux/scatterlist.h>
1514
#include <linux/bootmem.h>
1615
#include <linux/swiotlb.h>
1716
#include <linux/types.h>
@@ -169,49 +168,6 @@ void __init octeon_pci_dma_init(void)
169168
}
170169
#endif /* CONFIG_PCI */
171170

172-
static dma_addr_t octeon_dma_map_page(struct device *dev, struct page *page,
173-
unsigned long offset, size_t size, enum dma_data_direction direction,
174-
unsigned long attrs)
175-
{
176-
dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,
177-
direction, attrs);
178-
mb();
179-
180-
return daddr;
181-
}
182-
183-
static int octeon_dma_map_sg(struct device *dev, struct scatterlist *sg,
184-
int nents, enum dma_data_direction direction, unsigned long attrs)
185-
{
186-
int r = swiotlb_map_sg_attrs(dev, sg, nents, direction, attrs);
187-
mb();
188-
return r;
189-
}
190-
191-
static void octeon_dma_sync_single_for_device(struct device *dev,
192-
dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
193-
{
194-
swiotlb_sync_single_for_device(dev, dma_handle, size, direction);
195-
mb();
196-
}
197-
198-
static void octeon_dma_sync_sg_for_device(struct device *dev,
199-
struct scatterlist *sg, int nelems, enum dma_data_direction direction)
200-
{
201-
swiotlb_sync_sg_for_device(dev, sg, nelems, direction);
202-
mb();
203-
}
204-
205-
static void *octeon_dma_alloc_coherent(struct device *dev, size_t size,
206-
dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
207-
{
208-
void *ret = swiotlb_alloc(dev, size, dma_handle, gfp, attrs);
209-
210-
mb();
211-
212-
return ret;
213-
}
214-
215171
dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr)
216172
{
217173
#ifdef CONFIG_PCI
@@ -230,21 +186,6 @@ phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr)
230186
return daddr;
231187
}
232188

233-
static const struct dma_map_ops octeon_swiotlb_ops = {
234-
.alloc = octeon_dma_alloc_coherent,
235-
.free = swiotlb_free,
236-
.map_page = octeon_dma_map_page,
237-
.unmap_page = swiotlb_unmap_page,
238-
.map_sg = octeon_dma_map_sg,
239-
.unmap_sg = swiotlb_unmap_sg_attrs,
240-
.sync_single_for_cpu = swiotlb_sync_single_for_cpu,
241-
.sync_single_for_device = octeon_dma_sync_single_for_device,
242-
.sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
243-
.sync_sg_for_device = octeon_dma_sync_sg_for_device,
244-
.mapping_error = swiotlb_dma_mapping_error,
245-
.dma_supported = swiotlb_dma_supported
246-
};
247-
248189
char *octeon_swiotlb;
249190

250191
void __init plat_swiotlb_setup(void)
@@ -307,6 +248,4 @@ void __init plat_swiotlb_setup(void)
307248

308249
if (swiotlb_init_with_tbl(octeon_swiotlb, swiotlb_nslabs, 1) == -ENOMEM)
309250
panic("Cannot allocate SWIOTLB buffer");
310-
311-
mips_dma_map_ops = &octeon_swiotlb_ops;
312251
}

arch/mips/include/asm/dma-direct.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
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 */

arch/mips/include/asm/dma-mapping.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111
#endif
1212

1313
extern const struct dma_map_ops *mips_dma_map_ops;
14+
extern const struct dma_map_ops mips_swiotlb_ops;
1415

1516
static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
1617
{
18+
#ifdef CONFIG_SWIOTLB
19+
return &mips_swiotlb_ops;
20+
#else
1721
return mips_dma_map_ops;
22+
#endif
1823
}
1924

2025
#define arch_setup_dma_ops arch_setup_dma_ops

arch/mips/include/asm/mach-cavium-octeon/dma-coherence.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,6 @@ static inline void plat_post_dma_flush(struct device *dev)
6161
{
6262
}
6363

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-
7564
extern char *octeon_swiotlb;
7665

7766
#endif /* __ASM_MACH_CAVIUM_OCTEON_DMA_COHERENCE_H */

arch/mips/include/asm/mach-loongson64/dma-coherence.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@
1717

1818
struct device;
1919

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);
3020
static inline dma_addr_t plat_map_dma_mem(struct device *dev, void *addr,
3121
size_t size)
3222
{
Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
2-
#include <linux/mm.h>
2+
#include <linux/dma-direct.h>
33
#include <linux/init.h>
4-
#include <linux/dma-mapping.h>
5-
#include <linux/scatterlist.h>
64
#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-
}
585

596
dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr)
607
{
@@ -80,23 +27,7 @@ phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr)
8027
return daddr;
8128
}
8229

83-
static const struct dma_map_ops loongson_dma_map_ops = {
84-
.alloc = loongson_dma_alloc_coherent,
85-
.free = swiotlb_free,
86-
.map_page = loongson_dma_map_page,
87-
.unmap_page = swiotlb_unmap_page,
88-
.map_sg = loongson_dma_map_sg,
89-
.unmap_sg = swiotlb_unmap_sg_attrs,
90-
.sync_single_for_cpu = swiotlb_sync_single_for_cpu,
91-
.sync_single_for_device = loongson_dma_sync_single_for_device,
92-
.sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
93-
.sync_sg_for_device = loongson_dma_sync_sg_for_device,
94-
.mapping_error = swiotlb_dma_mapping_error,
95-
.dma_supported = swiotlb_dma_supported,
96-
};
97-
9830
void __init plat_swiotlb_setup(void)
9931
{
10032
swiotlb_init(1);
101-
mips_dma_map_ops = &loongson_dma_map_ops;
10233
}

arch/mips/mm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ obj-$(CONFIG_32BIT) += ioremap.o pgtable-32.o
1717
obj-$(CONFIG_64BIT) += pgtable-64.o
1818
obj-$(CONFIG_HIGHMEM) += highmem.o
1919
obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o
20+
obj-$(CONFIG_SWIOTLB) += dma-swiotlb.o
2021

2122
obj-$(CONFIG_CPU_R4K_CACHE_TLB) += c-r4k.o cex-gen.o tlb-r4k.o
2223
obj-$(CONFIG_CPU_R3000) += c-r3k.o tlb-r3k.o

arch/mips/mm/dma-swiotlb.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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);

0 commit comments

Comments
 (0)