Skip to content

Commit 6894258

Browse files
Christoph Hellwigtorvalds
authored andcommitted
dma-mapping: consolidate dma_{alloc,free}_{attrs,coherent}
Since 2009 we have a nice asm-generic header implementing lots of DMA API functions for architectures using struct dma_map_ops, but unfortunately it's still missing a lot of APIs that all architectures still have to duplicate. This series consolidates the remaining functions, although we still need arch opt outs for two of them as a few architectures have very non-standard implementations. This patch (of 5): The coherent DMA allocator works the same over all architectures supporting dma_map operations. This patch consolidates them and converges the minor differences: - the debug_dma helpers are now called from all architectures, including those that were previously missing them - dma_alloc_from_coherent and dma_release_from_coherent are now always called from the generic alloc/free routines instead of the ops dma-mapping-common.h always includes dma-coherent.h to get the defintions for them, or the stubs if the architecture doesn't support this feature - checks for ->alloc / ->free presence are removed. There is only one magic instead of dma_map_ops without them (mic_dma_ops) and that one is x86 only anyway. Besides that only x86 needs special treatment to replace a default devices if none is passed and tweak the gfp_flags. An optional arch hook is provided for that. [linux@roeck-us.net: fix build] [jcmvbkbc@gmail.com: fix xtensa] Signed-off-by: Christoph Hellwig <hch@lst.de> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Russell King <linux@arm.linux.org.uk> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will.deacon@arm.com> Cc: Yoshinori Sato <ysato@users.sourceforge.jp> Cc: Michal Simek <monstr@monstr.eu> Cc: Jonas Bonn <jonas@southpole.se> Cc: Chris Metcalf <cmetcalf@ezchip.com> Cc: Guan Xuetao <gxt@mprc.pku.edu.cn> Cc: Ralf Baechle <ralf@linux-mips.org> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Ingo Molnar <mingo@elte.hu> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Andy Shevchenko <andy.shevchenko@gmail.com> Signed-off-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Max Filippov <jcmvbkbc@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent fb6dd5f commit 6894258

File tree

25 files changed

+70
-569
lines changed

25 files changed

+70
-569
lines changed

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,6 @@ static inline struct dma_map_ops *get_dma_ops(struct device *dev)
1212

1313
#include <asm-generic/dma-mapping-common.h>
1414

15-
#define dma_alloc_coherent(d,s,h,f) dma_alloc_attrs(d,s,h,f,NULL)
16-
17-
static inline void *dma_alloc_attrs(struct device *dev, size_t size,
18-
dma_addr_t *dma_handle, gfp_t gfp,
19-
struct dma_attrs *attrs)
20-
{
21-
return get_dma_ops(dev)->alloc(dev, size, dma_handle, gfp, attrs);
22-
}
23-
24-
#define dma_free_coherent(d,s,c,h) dma_free_attrs(d,s,c,h,NULL)
25-
26-
static inline void dma_free_attrs(struct device *dev, size_t size,
27-
void *vaddr, dma_addr_t dma_handle,
28-
struct dma_attrs *attrs)
29-
{
30-
get_dma_ops(dev)->free(dev, size, vaddr, dma_handle, attrs);
31-
}
32-
3315
static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
3416
{
3517
return get_dma_ops(dev)->mapping_error(dev, dma_addr);

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

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <linux/dma-attrs.h>
99
#include <linux/dma-debug.h>
1010

11-
#include <asm-generic/dma-coherent.h>
1211
#include <asm/memory.h>
1312

1413
#include <xen/xen.h>
@@ -209,21 +208,6 @@ extern int arm_dma_set_mask(struct device *dev, u64 dma_mask);
209208
extern void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
210209
gfp_t gfp, struct dma_attrs *attrs);
211210

212-
#define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL)
213-
214-
static inline void *dma_alloc_attrs(struct device *dev, size_t size,
215-
dma_addr_t *dma_handle, gfp_t flag,
216-
struct dma_attrs *attrs)
217-
{
218-
struct dma_map_ops *ops = get_dma_ops(dev);
219-
void *cpu_addr;
220-
BUG_ON(!ops);
221-
222-
cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
223-
debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
224-
return cpu_addr;
225-
}
226-
227211
/**
228212
* arm_dma_free - free memory allocated by arm_dma_alloc
229213
* @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
@@ -241,19 +225,6 @@ static inline void *dma_alloc_attrs(struct device *dev, size_t size,
241225
extern void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
242226
dma_addr_t handle, struct dma_attrs *attrs);
243227

244-
#define dma_free_coherent(d, s, c, h) dma_free_attrs(d, s, c, h, NULL)
245-
246-
static inline void dma_free_attrs(struct device *dev, size_t size,
247-
void *cpu_addr, dma_addr_t dma_handle,
248-
struct dma_attrs *attrs)
249-
{
250-
struct dma_map_ops *ops = get_dma_ops(dev);
251-
BUG_ON(!ops);
252-
253-
debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
254-
ops->free(dev, size, cpu_addr, dma_handle, attrs);
255-
}
256-
257228
/**
258229
* arm_dma_mmap - map a coherent DMA allocation into user space
259230
* @dev: valid struct device pointer, or NULL for ISA and EISA-like devices

arch/arm/mm/dma-mapping.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,6 @@ void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
676676
gfp_t gfp, struct dma_attrs *attrs)
677677
{
678678
pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
679-
void *memory;
680-
681-
if (dma_alloc_from_coherent(dev, size, handle, &memory))
682-
return memory;
683679

684680
return __dma_alloc(dev, size, handle, gfp, prot, false,
685681
attrs, __builtin_return_address(0));
@@ -688,11 +684,6 @@ void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
688684
static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
689685
dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
690686
{
691-
void *memory;
692-
693-
if (dma_alloc_from_coherent(dev, size, handle, &memory))
694-
return memory;
695-
696687
return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
697688
attrs, __builtin_return_address(0));
698689
}
@@ -752,9 +743,6 @@ static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
752743
struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
753744
bool want_vaddr = !dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs);
754745

755-
if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
756-
return;
757-
758746
size = PAGE_ALIGN(size);
759747

760748
if (nommu()) {

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
#include <linux/types.h>
2323
#include <linux/vmalloc.h>
2424

25-
#include <asm-generic/dma-coherent.h>
26-
2725
#include <xen/xen.h>
2826
#include <asm/xen/hypervisor.h>
2927

@@ -120,37 +118,6 @@ static inline void dma_mark_clean(void *addr, size_t size)
120118
{
121119
}
122120

123-
#define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL)
124-
#define dma_free_coherent(d, s, h, f) dma_free_attrs(d, s, h, f, NULL)
125-
126-
static inline void *dma_alloc_attrs(struct device *dev, size_t size,
127-
dma_addr_t *dma_handle, gfp_t flags,
128-
struct dma_attrs *attrs)
129-
{
130-
struct dma_map_ops *ops = get_dma_ops(dev);
131-
void *vaddr;
132-
133-
if (dma_alloc_from_coherent(dev, size, dma_handle, &vaddr))
134-
return vaddr;
135-
136-
vaddr = ops->alloc(dev, size, dma_handle, flags, attrs);
137-
debug_dma_alloc_coherent(dev, size, *dma_handle, vaddr);
138-
return vaddr;
139-
}
140-
141-
static inline void dma_free_attrs(struct device *dev, size_t size,
142-
void *vaddr, dma_addr_t dev_addr,
143-
struct dma_attrs *attrs)
144-
{
145-
struct dma_map_ops *ops = get_dma_ops(dev);
146-
147-
if (dma_release_from_coherent(dev, get_order(size), vaddr))
148-
return;
149-
150-
debug_dma_free_coherent(dev, size, vaddr, dev_addr);
151-
ops->free(dev, size, vaddr, dev_addr, attrs);
152-
}
153-
154121
/*
155122
* There is no dma_cache_sync() implementation, so just return NULL here.
156123
*/

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef _H8300_DMA_MAPPING_H
22
#define _H8300_DMA_MAPPING_H
33

4-
#include <asm-generic/dma-coherent.h>
5-
64
extern struct dma_map_ops h8300_dma_map_ops;
75

86
static inline struct dma_map_ops *get_dma_ops(struct device *dev)
@@ -25,30 +23,6 @@ static inline int dma_set_mask(struct device *dev, u64 mask)
2523
#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
2624
#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
2725

28-
#define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL)
29-
30-
static inline void *dma_alloc_attrs(struct device *dev, size_t size,
31-
dma_addr_t *dma_handle, gfp_t flag,
32-
struct dma_attrs *attrs)
33-
{
34-
struct dma_map_ops *ops = get_dma_ops(dev);
35-
void *memory;
36-
37-
memory = ops->alloc(dev, size, dma_handle, flag, attrs);
38-
return memory;
39-
}
40-
41-
#define dma_free_coherent(d, s, c, h) dma_free_attrs(d, s, c, h, NULL)
42-
43-
static inline void dma_free_attrs(struct device *dev, size_t size,
44-
void *cpu_addr, dma_addr_t dma_handle,
45-
struct dma_attrs *attrs)
46-
{
47-
struct dma_map_ops *ops = get_dma_ops(dev);
48-
49-
ops->free(dev, size, cpu_addr, dma_handle, attrs);
50-
}
51-
5226
static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
5327
{
5428
return 0;

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -70,37 +70,4 @@ static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
7070
return (dma_addr == bad_dma_address);
7171
}
7272

73-
#define dma_alloc_coherent(d,s,h,f) dma_alloc_attrs(d,s,h,f,NULL)
74-
75-
static inline void *dma_alloc_attrs(struct device *dev, size_t size,
76-
dma_addr_t *dma_handle, gfp_t flag,
77-
struct dma_attrs *attrs)
78-
{
79-
void *ret;
80-
struct dma_map_ops *ops = get_dma_ops(dev);
81-
82-
BUG_ON(!dma_ops);
83-
84-
ret = ops->alloc(dev, size, dma_handle, flag, attrs);
85-
86-
debug_dma_alloc_coherent(dev, size, *dma_handle, ret);
87-
88-
return ret;
89-
}
90-
91-
#define dma_free_coherent(d,s,c,h) dma_free_attrs(d,s,c,h,NULL)
92-
93-
static inline void dma_free_attrs(struct device *dev, size_t size,
94-
void *cpu_addr, dma_addr_t dma_handle,
95-
struct dma_attrs *attrs)
96-
{
97-
struct dma_map_ops *dma_ops = get_dma_ops(dev);
98-
99-
BUG_ON(!dma_ops);
100-
101-
dma_ops->free(dev, size, cpu_addr, dma_handle, attrs);
102-
103-
debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
104-
}
105-
10673
#endif

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

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,6 @@ extern void machvec_dma_sync_single(struct device *, dma_addr_t, size_t,
2323
extern void machvec_dma_sync_sg(struct device *, struct scatterlist *, int,
2424
enum dma_data_direction);
2525

26-
#define dma_alloc_coherent(d,s,h,f) dma_alloc_attrs(d,s,h,f,NULL)
27-
28-
static inline void *dma_alloc_attrs(struct device *dev, size_t size,
29-
dma_addr_t *daddr, gfp_t gfp,
30-
struct dma_attrs *attrs)
31-
{
32-
struct dma_map_ops *ops = platform_dma_get_ops(dev);
33-
void *caddr;
34-
35-
caddr = ops->alloc(dev, size, daddr, gfp, attrs);
36-
debug_dma_alloc_coherent(dev, size, *daddr, caddr);
37-
return caddr;
38-
}
39-
40-
#define dma_free_coherent(d,s,c,h) dma_free_attrs(d,s,c,h,NULL)
41-
42-
static inline void dma_free_attrs(struct device *dev, size_t size,
43-
void *caddr, dma_addr_t daddr,
44-
struct dma_attrs *attrs)
45-
{
46-
struct dma_map_ops *ops = platform_dma_get_ops(dev);
47-
debug_dma_free_coherent(dev, size, caddr, daddr);
48-
ops->free(dev, size, caddr, daddr, attrs);
49-
}
50-
5126
#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
5227
#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
5328

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <linux/dma-debug.h>
2828
#include <linux/dma-attrs.h>
2929
#include <asm/io.h>
30-
#include <asm-generic/dma-coherent.h>
3130
#include <asm/cacheflush.h>
3231

3332
#define DMA_ERROR_CODE (~(dma_addr_t)0x0)
@@ -102,36 +101,6 @@ static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
102101
#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
103102
#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
104103

105-
#define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL)
106-
107-
static inline void *dma_alloc_attrs(struct device *dev, size_t size,
108-
dma_addr_t *dma_handle, gfp_t flag,
109-
struct dma_attrs *attrs)
110-
{
111-
struct dma_map_ops *ops = get_dma_ops(dev);
112-
void *memory;
113-
114-
BUG_ON(!ops);
115-
116-
memory = ops->alloc(dev, size, dma_handle, flag, attrs);
117-
118-
debug_dma_alloc_coherent(dev, size, *dma_handle, memory);
119-
return memory;
120-
}
121-
122-
#define dma_free_coherent(d,s,c,h) dma_free_attrs(d, s, c, h, NULL)
123-
124-
static inline void dma_free_attrs(struct device *dev, size_t size,
125-
void *cpu_addr, dma_addr_t dma_handle,
126-
struct dma_attrs *attrs)
127-
{
128-
struct dma_map_ops *ops = get_dma_ops(dev);
129-
130-
BUG_ON(!ops);
131-
debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
132-
ops->free(dev, size, cpu_addr, dma_handle, attrs);
133-
}
134-
135104
static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
136105
enum dma_data_direction direction)
137106
{

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ static void *octeon_dma_alloc_coherent(struct device *dev, size_t size,
161161
{
162162
void *ret;
163163

164-
if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
165-
return ret;
166-
167164
/* ignore region specifiers */
168165
gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
169166

@@ -194,11 +191,6 @@ static void *octeon_dma_alloc_coherent(struct device *dev, size_t size,
194191
static void octeon_dma_free_coherent(struct device *dev, size_t size,
195192
void *vaddr, dma_addr_t dma_handle, struct dma_attrs *attrs)
196193
{
197-
int order = get_order(size);
198-
199-
if (dma_release_from_coherent(dev, order, vaddr))
200-
return;
201-
202194
swiotlb_free_coherent(dev, size, vaddr, dma_handle);
203195
}
204196

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <linux/scatterlist.h>
55
#include <asm/dma-coherence.h>
66
#include <asm/cache.h>
7-
#include <asm-generic/dma-coherent.h>
87

98
#ifndef CONFIG_SGI_IP27 /* Kludge to fix 2.6.39 build for IP27 */
109
#include <dma-coherence.h>
@@ -65,36 +64,6 @@ dma_set_mask(struct device *dev, u64 mask)
6564
extern void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
6665
enum dma_data_direction direction);
6766

68-
#define dma_alloc_coherent(d,s,h,f) dma_alloc_attrs(d,s,h,f,NULL)
69-
70-
static inline void *dma_alloc_attrs(struct device *dev, size_t size,
71-
dma_addr_t *dma_handle, gfp_t gfp,
72-
struct dma_attrs *attrs)
73-
{
74-
void *ret;
75-
struct dma_map_ops *ops = get_dma_ops(dev);
76-
77-
ret = ops->alloc(dev, size, dma_handle, gfp, attrs);
78-
79-
debug_dma_alloc_coherent(dev, size, *dma_handle, ret);
80-
81-
return ret;
82-
}
83-
84-
#define dma_free_coherent(d,s,c,h) dma_free_attrs(d,s,c,h,NULL)
85-
86-
static inline void dma_free_attrs(struct device *dev, size_t size,
87-
void *vaddr, dma_addr_t dma_handle,
88-
struct dma_attrs *attrs)
89-
{
90-
struct dma_map_ops *ops = get_dma_ops(dev);
91-
92-
ops->free(dev, size, vaddr, dma_handle, attrs);
93-
94-
debug_dma_free_coherent(dev, size, vaddr, dma_handle);
95-
}
96-
97-
9867
void *dma_alloc_noncoherent(struct device *dev, size_t size,
9968
dma_addr_t *dma_handle, gfp_t flag);
10069

arch/mips/loongson64/common/dma-swiotlb.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
1414
{
1515
void *ret;
1616

17-
if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
18-
return ret;
19-
2017
/* ignore region specifiers */
2118
gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
2219

@@ -46,11 +43,6 @@ static void *loongson_dma_alloc_coherent(struct device *dev, size_t size,
4643
static void loongson_dma_free_coherent(struct device *dev, size_t size,
4744
void *vaddr, dma_addr_t dma_handle, struct dma_attrs *attrs)
4845
{
49-
int order = get_order(size);
50-
51-
if (dma_release_from_coherent(dev, order, vaddr))
52-
return;
53-
5446
swiotlb_free_coherent(dev, size, vaddr, dma_handle);
5547
}
5648

0 commit comments

Comments
 (0)