1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#ifndef DMAENGINE_H
22#define DMAENGINE_H
23
24#ifdef CONFIG_DMA_ENGINE
25
26#include <linux/device.h>
27#include <linux/uio.h>
28#include <linux/kref.h>
29#include <linux/completion.h>
30#include <linux/rcupdate.h>
31
32
33
34
35
36
37
38
39enum dma_event {
40 DMA_RESOURCE_SUSPEND,
41 DMA_RESOURCE_RESUME,
42 DMA_RESOURCE_ADDED,
43 DMA_RESOURCE_REMOVED,
44};
45
46
47
48
49
50
51typedef s32 dma_cookie_t;
52
53#define dma_submit_error(cookie) ((cookie) < 0 ? 1 : 0)
54
55
56
57
58
59
60
61enum dma_status {
62 DMA_SUCCESS,
63 DMA_IN_PROGRESS,
64 DMA_ERROR,
65};
66
67
68
69
70
71
72
73
74struct dma_chan_percpu {
75 local_t refcount;
76
77 unsigned long memcpy_count;
78 unsigned long bytes_transferred;
79};
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95struct dma_chan {
96 struct dma_client *client;
97 struct dma_device *device;
98 dma_cookie_t cookie;
99
100
101 int chan_id;
102 struct class_device class_dev;
103
104 struct kref refcount;
105 int slow_ref;
106 struct rcu_head rcu;
107
108 struct list_head client_node;
109 struct list_head device_node;
110 struct dma_chan_percpu *local;
111};
112
113void dma_chan_cleanup(struct kref *kref);
114
115static inline void dma_chan_get(struct dma_chan *chan)
116{
117 if (unlikely(chan->slow_ref))
118 kref_get(&chan->refcount);
119 else {
120 local_inc(&(per_cpu_ptr(chan->local, get_cpu())->refcount));
121 put_cpu();
122 }
123}
124
125static inline void dma_chan_put(struct dma_chan *chan)
126{
127 if (unlikely(chan->slow_ref))
128 kref_put(&chan->refcount, dma_chan_cleanup);
129 else {
130 local_dec(&(per_cpu_ptr(chan->local, get_cpu())->refcount));
131 put_cpu();
132 }
133}
134
135
136
137
138typedef void (*dma_event_callback) (struct dma_client *client,
139 struct dma_chan *chan, enum dma_event event);
140
141
142
143
144
145
146
147
148
149
150struct dma_client {
151 dma_event_callback event_callback;
152 unsigned int chan_count;
153 unsigned int chans_desired;
154
155 spinlock_t lock;
156 struct list_head channels;
157 struct list_head global_node;
158};
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177struct dma_device {
178
179 unsigned int chancnt;
180 struct list_head channels;
181 struct list_head global_node;
182
183 struct kref refcount;
184 struct completion done;
185
186 int dev_id;
187
188 int (*device_alloc_chan_resources)(struct dma_chan *chan);
189 void (*device_free_chan_resources)(struct dma_chan *chan);
190 dma_cookie_t (*device_memcpy_buf_to_buf)(struct dma_chan *chan,
191 void *dest, void *src, size_t len);
192 dma_cookie_t (*device_memcpy_buf_to_pg)(struct dma_chan *chan,
193 struct page *page, unsigned int offset, void *kdata,
194 size_t len);
195 dma_cookie_t (*device_memcpy_pg_to_pg)(struct dma_chan *chan,
196 struct page *dest_pg, unsigned int dest_off,
197 struct page *src_pg, unsigned int src_off, size_t len);
198 enum dma_status (*device_memcpy_complete)(struct dma_chan *chan,
199 dma_cookie_t cookie, dma_cookie_t *last,
200 dma_cookie_t *used);
201 void (*device_memcpy_issue_pending)(struct dma_chan *chan);
202};
203
204
205
206struct dma_client *dma_async_client_register(dma_event_callback event_callback);
207void dma_async_client_unregister(struct dma_client *client);
208void dma_async_client_chan_request(struct dma_client *client,
209 unsigned int number);
210
211
212
213
214
215
216
217
218
219
220
221
222
223static inline dma_cookie_t dma_async_memcpy_buf_to_buf(struct dma_chan *chan,
224 void *dest, void *src, size_t len)
225{
226 int cpu = get_cpu();
227 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
228 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
229 put_cpu();
230
231 return chan->device->device_memcpy_buf_to_buf(chan, dest, src, len);
232}
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247static inline dma_cookie_t dma_async_memcpy_buf_to_pg(struct dma_chan *chan,
248 struct page *page, unsigned int offset, void *kdata, size_t len)
249{
250 int cpu = get_cpu();
251 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
252 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
253 put_cpu();
254
255 return chan->device->device_memcpy_buf_to_pg(chan, page, offset,
256 kdata, len);
257}
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273static inline dma_cookie_t dma_async_memcpy_pg_to_pg(struct dma_chan *chan,
274 struct page *dest_pg, unsigned int dest_off, struct page *src_pg,
275 unsigned int src_off, size_t len)
276{
277 int cpu = get_cpu();
278 per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
279 per_cpu_ptr(chan->local, cpu)->memcpy_count++;
280 put_cpu();
281
282 return chan->device->device_memcpy_pg_to_pg(chan, dest_pg, dest_off,
283 src_pg, src_off, len);
284}
285
286
287
288
289
290
291
292
293static inline void dma_async_memcpy_issue_pending(struct dma_chan *chan)
294{
295 return chan->device->device_memcpy_issue_pending(chan);
296}
297
298
299
300
301
302
303
304
305
306
307
308
309static inline enum dma_status dma_async_memcpy_complete(struct dma_chan *chan,
310 dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
311{
312 return chan->device->device_memcpy_complete(chan, cookie, last, used);
313}
314
315
316
317
318
319
320
321
322
323
324static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
325 dma_cookie_t last_complete, dma_cookie_t last_used)
326{
327 if (last_complete <= last_used) {
328 if ((cookie <= last_complete) || (cookie > last_used))
329 return DMA_SUCCESS;
330 } else {
331 if ((cookie <= last_complete) && (cookie > last_used))
332 return DMA_SUCCESS;
333 }
334 return DMA_IN_PROGRESS;
335}
336
337
338
339
340int dma_async_device_register(struct dma_device *device);
341void dma_async_device_unregister(struct dma_device *device);
342
343
344
345struct dma_page_list {
346 char *base_address;
347 int nr_pages;
348 struct page **pages;
349};
350
351struct dma_pinned_list {
352 int nr_iovecs;
353 struct dma_page_list page_list[0];
354};
355
356struct dma_pinned_list *dma_pin_iovec_pages(struct iovec *iov, size_t len);
357void dma_unpin_iovec_pages(struct dma_pinned_list* pinned_list);
358
359dma_cookie_t dma_memcpy_to_iovec(struct dma_chan *chan, struct iovec *iov,
360 struct dma_pinned_list *pinned_list, unsigned char *kdata, size_t len);
361dma_cookie_t dma_memcpy_pg_to_iovec(struct dma_chan *chan, struct iovec *iov,
362 struct dma_pinned_list *pinned_list, struct page *page,
363 unsigned int offset, size_t len);
364
365#endif
366#endif
367