Skip to content

Commit 7b903e6

Browse files
author
Jonas Bonn
committed
openrisc: provide dma_map_ops
This switches OpenRISC over to fully using the generic dma-mapping framework. This was almost already the case as the architecture's implementation was essentially a copy of the generic header. This also brings this architecture in line with the recent changes to dma_map_ops (adding attributes to ops->alloc). Signed-off-by: Jonas Bonn <jonas@southpole.se>
1 parent b0e026f commit 7b903e6

2 files changed

Lines changed: 107 additions & 149 deletions

File tree

‎arch/openrisc/include/asm/dma-mapping.h‎

Lines changed: 34 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -20,150 +20,71 @@
2020
/*
2121
* See Documentation/DMA-API-HOWTO.txt and
2222
* Documentation/DMA-API.txt for documentation.
23-
*
24-
* This file is written with the intention of eventually moving over
25-
* to largely using asm-generic/dma-mapping-common.h in its place.
2623
*/
2724

2825
#include <linux/dma-debug.h>
2926
#include <asm-generic/dma-coherent.h>
3027
#include <linux/kmemcheck.h>
28+
#include <linux/dma-mapping.h>
3129

3230
#define DMA_ERROR_CODE (~(dma_addr_t)0x0)
3331

32+
extern struct dma_map_ops or1k_dma_map_ops;
3433

35-
#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
36-
#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
37-
38-
void *or1k_dma_alloc_coherent(struct device *dev, size_t size,
39-
dma_addr_t *dma_handle, gfp_t flag);
40-
void or1k_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
41-
dma_addr_t dma_handle);
42-
dma_addr_t or1k_map_page(struct device *dev, struct page *page,
43-
unsigned long offset, size_t size,
44-
enum dma_data_direction dir,
45-
struct dma_attrs *attrs);
46-
void or1k_unmap_page(struct device *dev, dma_addr_t dma_handle,
47-
size_t size, enum dma_data_direction dir,
48-
struct dma_attrs *attrs);
49-
int or1k_map_sg(struct device *dev, struct scatterlist *sg,
50-
int nents, enum dma_data_direction dir,
51-
struct dma_attrs *attrs);
52-
void or1k_unmap_sg(struct device *dev, struct scatterlist *sg,
53-
int nents, enum dma_data_direction dir,
54-
struct dma_attrs *attrs);
55-
void or1k_sync_single_for_cpu(struct device *dev,
56-
dma_addr_t dma_handle, size_t size,
57-
enum dma_data_direction dir);
58-
void or1k_sync_single_for_device(struct device *dev,
59-
dma_addr_t dma_handle, size_t size,
60-
enum dma_data_direction dir);
61-
62-
static inline void *dma_alloc_coherent(struct device *dev, size_t size,
63-
dma_addr_t *dma_handle, gfp_t flag)
34+
static inline struct dma_map_ops *get_dma_ops(struct device *dev)
6435
{
65-
void *memory;
66-
67-
memory = or1k_dma_alloc_coherent(dev, size, dma_handle, flag);
68-
69-
debug_dma_alloc_coherent(dev, size, *dma_handle, memory);
70-
return memory;
36+
return &or1k_dma_map_ops;
7137
}
7238

73-
static inline void dma_free_coherent(struct device *dev, size_t size,
74-
void *cpu_addr, dma_addr_t dma_handle)
75-
{
76-
debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
77-
or1k_dma_free_coherent(dev, size, cpu_addr, dma_handle);
78-
}
39+
#include <asm-generic/dma-mapping-common.h>
7940

80-
static inline dma_addr_t dma_map_single(struct device *dev, void *ptr,
81-
size_t size,
82-
enum dma_data_direction dir)
83-
{
84-
dma_addr_t addr;
85-
86-
kmemcheck_mark_initialized(ptr, size);
87-
BUG_ON(!valid_dma_direction(dir));
88-
addr = or1k_map_page(dev, virt_to_page(ptr),
89-
(unsigned long)ptr & ~PAGE_MASK, size,
90-
dir, NULL);
91-
debug_dma_map_page(dev, virt_to_page(ptr),
92-
(unsigned long)ptr & ~PAGE_MASK, size,
93-
dir, addr, true);
94-
return addr;
95-
}
41+
#define dma_alloc_coherent(d,s,h,f) dma_alloc_attrs(d,s,h,f,NULL)
9642

97-
static inline void dma_unmap_single(struct device *dev, dma_addr_t addr,
98-
size_t size,
99-
enum dma_data_direction dir)
43+
static inline void *dma_alloc_attrs(struct device *dev, size_t size,
44+
dma_addr_t *dma_handle, gfp_t gfp,
45+
struct dma_attrs *attrs)
10046
{
101-
BUG_ON(!valid_dma_direction(dir));
102-
or1k_unmap_page(dev, addr, size, dir, NULL);
103-
debug_dma_unmap_page(dev, addr, size, dir, true);
104-
}
47+
struct dma_map_ops *ops = get_dma_ops(dev);
48+
void *memory;
10549

106-
static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
107-
int nents, enum dma_data_direction dir)
108-
{
109-
int i, ents;
110-
struct scatterlist *s;
50+
memory = ops->alloc(dev, size, dma_handle, gfp, attrs);
11151

112-
for_each_sg(sg, s, nents, i)
113-
kmemcheck_mark_initialized(sg_virt(s), s->length);
114-
BUG_ON(!valid_dma_direction(dir));
115-
ents = or1k_map_sg(dev, sg, nents, dir, NULL);
116-
debug_dma_map_sg(dev, sg, nents, ents, dir);
52+
debug_dma_alloc_coherent(dev, size, *dma_handle, memory);
11753

118-
return ents;
54+
return memory;
11955
}
12056

121-
static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
122-
int nents, enum dma_data_direction dir)
123-
{
124-
BUG_ON(!valid_dma_direction(dir));
125-
debug_dma_unmap_sg(dev, sg, nents, dir);
126-
or1k_unmap_sg(dev, sg, nents, dir, NULL);
127-
}
57+
#define dma_free_coherent(d,s,c,h) dma_free_attrs(d,s,c,h,NULL)
12858

129-
static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
130-
size_t offset, size_t size,
131-
enum dma_data_direction dir)
59+
static inline void dma_free_attrs(struct device *dev, size_t size,
60+
void *cpu_addr, dma_addr_t dma_handle,
61+
struct dma_attrs *attrs)
13262
{
133-
dma_addr_t addr;
63+
struct dma_map_ops *ops = get_dma_ops(dev);
13464

135-
kmemcheck_mark_initialized(page_address(page) + offset, size);
136-
BUG_ON(!valid_dma_direction(dir));
137-
addr = or1k_map_page(dev, page, offset, size, dir, NULL);
138-
debug_dma_map_page(dev, page, offset, size, dir, addr, false);
65+
debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
13966

140-
return addr;
67+
ops->free(dev, size, cpu_addr, dma_handle, attrs);
14168
}
14269

143-
static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
144-
size_t size, enum dma_data_direction dir)
70+
static inline void *dma_alloc_noncoherent(struct device *dev, size_t size,
71+
dma_addr_t *dma_handle, gfp_t gfp)
14572
{
146-
BUG_ON(!valid_dma_direction(dir));
147-
or1k_unmap_page(dev, addr, size, dir, NULL);
148-
debug_dma_unmap_page(dev, addr, size, dir, true);
149-
}
73+
struct dma_attrs attrs;
15074

151-
static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
152-
size_t size,
153-
enum dma_data_direction dir)
154-
{
155-
BUG_ON(!valid_dma_direction(dir));
156-
or1k_sync_single_for_cpu(dev, addr, size, dir);
157-
debug_dma_sync_single_for_cpu(dev, addr, size, dir);
75+
dma_set_attr(DMA_ATTR_NON_CONSISTENT, &attrs);
76+
77+
return dma_alloc_attrs(dev, size, dma_handle, gfp, &attrs);
15878
}
15979

160-
static inline void dma_sync_single_for_device(struct device *dev,
161-
dma_addr_t addr, size_t size,
162-
enum dma_data_direction dir)
80+
static inline void dma_free_noncoherent(struct device *dev, size_t size,
81+
void *cpu_addr, dma_addr_t dma_handle)
16382
{
164-
BUG_ON(!valid_dma_direction(dir));
165-
or1k_sync_single_for_device(dev, addr, size, dir);
166-
debug_dma_sync_single_for_device(dev, addr, size, dir);
83+
struct dma_attrs attrs;
84+
85+
dma_set_attr(DMA_ATTR_NON_CONSISTENT, &attrs);
86+
87+
dma_free_attrs(dev, size, cpu_addr, dma_handle, &attrs);
16788
}
16889

16990
static inline int dma_supported(struct device *dev, u64 dma_mask)

‎arch/openrisc/kernel/dma.c‎

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121

2222
#include <linux/dma-mapping.h>
2323
#include <linux/dma-debug.h>
24+
#include <linux/export.h>
25+
#include <linux/dma-attrs.h>
2426

2527
#include <asm/cpuinfo.h>
2628
#include <asm/spr_defs.h>
2729
#include <asm/tlbflush.h>
2830

29-
static int page_set_nocache(pte_t *pte, unsigned long addr,
30-
unsigned long next, struct mm_walk *walk)
31+
static int
32+
page_set_nocache(pte_t *pte, unsigned long addr,
33+
unsigned long next, struct mm_walk *walk)
3134
{
3235
unsigned long cl;
3336

@@ -46,8 +49,9 @@ static int page_set_nocache(pte_t *pte, unsigned long addr,
4649
return 0;
4750
}
4851

49-
static int page_clear_nocache(pte_t *pte, unsigned long addr,
50-
unsigned long next, struct mm_walk *walk)
52+
static int
53+
page_clear_nocache(pte_t *pte, unsigned long addr,
54+
unsigned long next, struct mm_walk *walk)
5155
{
5256
pte_val(*pte) &= ~_PAGE_CI;
5357

@@ -67,9 +71,19 @@ static int page_clear_nocache(pte_t *pte, unsigned long addr,
6771
* cache-inhibit bit on those pages, and makes sure that the pages are
6872
* flushed out of the cache before they are used.
6973
*
74+
* If the NON_CONSISTENT attribute is set, then this function just
75+
* returns "normal", cachable memory.
76+
*
77+
* There are additional flags WEAK_ORDERING and WRITE_COMBINE to take
78+
* into consideration here, too. All current known implementations of
79+
* the OR1K support only strongly ordered memory accesses, so that flag
80+
* is being ignored for now; uncached but write-combined memory is a
81+
* missing feature of the OR1K.
7082
*/
71-
void *or1k_dma_alloc_coherent(struct device *dev, size_t size,
72-
dma_addr_t *dma_handle, gfp_t gfp)
83+
static void *
84+
or1k_dma_alloc(struct device *dev, size_t size,
85+
dma_addr_t *dma_handle, gfp_t gfp,
86+
struct dma_attrs *attrs)
7387
{
7488
unsigned long va;
7589
void *page;
@@ -87,37 +101,43 @@ void *or1k_dma_alloc_coherent(struct device *dev, size_t size,
87101

88102
va = (unsigned long)page;
89103

90-
/*
91-
* We need to iterate through the pages, clearing the dcache for
92-
* them and setting the cache-inhibit bit.
93-
*/
94-
if (walk_page_range(va, va + size, &walk)) {
95-
free_pages_exact(page, size);
96-
return NULL;
104+
if (!dma_get_attr(DMA_ATTR_NON_CONSISTENT, attrs)) {
105+
/*
106+
* We need to iterate through the pages, clearing the dcache for
107+
* them and setting the cache-inhibit bit.
108+
*/
109+
if (walk_page_range(va, va + size, &walk)) {
110+
free_pages_exact(page, size);
111+
return NULL;
112+
}
97113
}
98114

99115
return (void *)va;
100116
}
101117

102-
void or1k_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
103-
dma_addr_t dma_handle)
118+
static void
119+
or1k_dma_free(struct device *dev, size_t size, void *vaddr,
120+
dma_addr_t dma_handle, struct dma_attrs *attrs)
104121
{
105122
unsigned long va = (unsigned long)vaddr;
106123
struct mm_walk walk = {
107124
.pte_entry = page_clear_nocache,
108125
.mm = &init_mm
109126
};
110127

111-
/* walk_page_range shouldn't be able to fail here */
112-
WARN_ON(walk_page_range(va, va + size, &walk));
128+
if (!dma_get_attr(DMA_ATTR_NON_CONSISTENT, attrs)) {
129+
/* walk_page_range shouldn't be able to fail here */
130+
WARN_ON(walk_page_range(va, va + size, &walk));
131+
}
113132

114133
free_pages_exact(vaddr, size);
115134
}
116135

117-
dma_addr_t or1k_map_page(struct device *dev, struct page *page,
118-
unsigned long offset, size_t size,
119-
enum dma_data_direction dir,
120-
struct dma_attrs *attrs)
136+
static dma_addr_t
137+
or1k_map_page(struct device *dev, struct page *page,
138+
unsigned long offset, size_t size,
139+
enum dma_data_direction dir,
140+
struct dma_attrs *attrs)
121141
{
122142
unsigned long cl;
123143
dma_addr_t addr = page_to_phys(page) + offset;
@@ -147,16 +167,18 @@ dma_addr_t or1k_map_page(struct device *dev, struct page *page,
147167
return addr;
148168
}
149169

150-
void or1k_unmap_page(struct device *dev, dma_addr_t dma_handle,
151-
size_t size, enum dma_data_direction dir,
152-
struct dma_attrs *attrs)
170+
static void
171+
or1k_unmap_page(struct device *dev, dma_addr_t dma_handle,
172+
size_t size, enum dma_data_direction dir,
173+
struct dma_attrs *attrs)
153174
{
154175
/* Nothing special to do here... */
155176
}
156177

157-
int or1k_map_sg(struct device *dev, struct scatterlist *sg,
158-
int nents, enum dma_data_direction dir,
159-
struct dma_attrs *attrs)
178+
static int
179+
or1k_map_sg(struct device *dev, struct scatterlist *sg,
180+
int nents, enum dma_data_direction dir,
181+
struct dma_attrs *attrs)
160182
{
161183
struct scatterlist *s;
162184
int i;
@@ -169,9 +191,10 @@ int or1k_map_sg(struct device *dev, struct scatterlist *sg,
169191
return nents;
170192
}
171193

172-
void or1k_unmap_sg(struct device *dev, struct scatterlist *sg,
173-
int nents, enum dma_data_direction dir,
174-
struct dma_attrs *attrs)
194+
static void
195+
or1k_unmap_sg(struct device *dev, struct scatterlist *sg,
196+
int nents, enum dma_data_direction dir,
197+
struct dma_attrs *attrs)
175198
{
176199
struct scatterlist *s;
177200
int i;
@@ -181,9 +204,10 @@ void or1k_unmap_sg(struct device *dev, struct scatterlist *sg,
181204
}
182205
}
183206

184-
void or1k_sync_single_for_cpu(struct device *dev,
185-
dma_addr_t dma_handle, size_t size,
186-
enum dma_data_direction dir)
207+
static void
208+
or1k_sync_single_for_cpu(struct device *dev,
209+
dma_addr_t dma_handle, size_t size,
210+
enum dma_data_direction dir)
187211
{
188212
unsigned long cl;
189213
dma_addr_t addr = dma_handle;
@@ -193,9 +217,10 @@ void or1k_sync_single_for_cpu(struct device *dev,
193217
mtspr(SPR_DCBIR, cl);
194218
}
195219

196-
void or1k_sync_single_for_device(struct device *dev,
197-
dma_addr_t dma_handle, size_t size,
198-
enum dma_data_direction dir)
220+
static void
221+
or1k_sync_single_for_device(struct device *dev,
222+
dma_addr_t dma_handle, size_t size,
223+
enum dma_data_direction dir)
199224
{
200225
unsigned long cl;
201226
dma_addr_t addr = dma_handle;
@@ -205,6 +230,18 @@ void or1k_sync_single_for_device(struct device *dev,
205230
mtspr(SPR_DCBFR, cl);
206231
}
207232

233+
struct dma_map_ops or1k_dma_map_ops = {
234+
.alloc = or1k_dma_alloc,
235+
.free = or1k_dma_free,
236+
.map_page = or1k_map_page,
237+
.unmap_page = or1k_unmap_page,
238+
.map_sg = or1k_map_sg,
239+
.unmap_sg = or1k_unmap_sg,
240+
.sync_single_for_cpu = or1k_sync_single_for_cpu,
241+
.sync_single_for_device = or1k_sync_single_for_device,
242+
};
243+
EXPORT_SYMBOL(or1k_dma_map_ops);
244+
208245
/* Number of entries preallocated for DMA-API debugging */
209246
#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
210247

0 commit comments

Comments
 (0)