]>
Commit | Line | Data |
---|---|---|
c13c8260 CL |
1 | /* |
2 | * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License as published by the Free | |
6 | * Software Foundation; either version 2 of the License, or (at your option) | |
7 | * any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along with | |
15 | * this program; if not, write to the Free Software Foundation, Inc., 59 | |
16 | * Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | * | |
18 | * The full GNU General Public License is included in this distribution in the | |
19 | * file called COPYING. | |
20 | */ | |
21 | ||
22 | /* | |
23 | * This code implements the DMA subsystem. It provides a HW-neutral interface | |
24 | * for other kernel code to use asynchronous memory copy capabilities, | |
25 | * if present, and allows different HW DMA drivers to register as providing | |
26 | * this capability. | |
27 | * | |
28 | * Due to the fact we are accelerating what is already a relatively fast | |
29 | * operation, the code goes to great lengths to avoid additional overhead, | |
30 | * such as locking. | |
31 | * | |
32 | * LOCKING: | |
33 | * | |
34 | * The subsystem keeps two global lists, dma_device_list and dma_client_list. | |
35 | * Both of these are protected by a mutex, dma_list_mutex. | |
36 | * | |
37 | * Each device has a channels list, which runs unlocked but is never modified | |
38 | * once the device is registered, it's just setup by the driver. | |
39 | * | |
d379b01e DW |
40 | * Each client is responsible for keeping track of the channels it uses. See |
41 | * the definition of dma_event_callback in dmaengine.h. | |
c13c8260 CL |
42 | * |
43 | * Each device has a kref, which is initialized to 1 when the device is | |
891f78ea TJ |
44 | * registered. A kref_get is done for each device registered. When the |
45 | * device is released, the coresponding kref_put is done in the release | |
c13c8260 CL |
46 | * method. Every time one of the device's channels is allocated to a client, |
47 | * a kref_get occurs. When the channel is freed, the coresponding kref_put | |
48 | * happens. The device's release function does a completion, so | |
891f78ea | 49 | * unregister_device does a remove event, device_unregister, a kref_put |
c13c8260 CL |
50 | * for the first reference, then waits on the completion for all other |
51 | * references to finish. | |
52 | * | |
53 | * Each channel has an open-coded implementation of Rusty Russell's "bigref," | |
d379b01e DW |
54 | * with a kref and a per_cpu local_t. A dma_chan_get is called when a client |
55 | * signals that it wants to use a channel, and dma_chan_put is called when | |
56 | * a channel is removed or a client using it is unregesitered. A client can | |
57 | * take extra references per outstanding transaction, as is the case with | |
58 | * the NET DMA client. The release function does a kref_put on the device. | |
59 | * -ChrisL, DanW | |
c13c8260 CL |
60 | */ |
61 | ||
62 | #include <linux/init.h> | |
63 | #include <linux/module.h> | |
7405f74b | 64 | #include <linux/mm.h> |
c13c8260 CL |
65 | #include <linux/device.h> |
66 | #include <linux/dmaengine.h> | |
67 | #include <linux/hardirq.h> | |
68 | #include <linux/spinlock.h> | |
69 | #include <linux/percpu.h> | |
70 | #include <linux/rcupdate.h> | |
71 | #include <linux/mutex.h> | |
7405f74b | 72 | #include <linux/jiffies.h> |
c13c8260 CL |
73 | |
74 | static DEFINE_MUTEX(dma_list_mutex); | |
75 | static LIST_HEAD(dma_device_list); | |
76 | static LIST_HEAD(dma_client_list); | |
77 | ||
78 | /* --- sysfs implementation --- */ | |
79 | ||
891f78ea | 80 | static ssize_t show_memcpy_count(struct device *dev, struct device_attribute *attr, char *buf) |
c13c8260 | 81 | { |
891f78ea | 82 | struct dma_chan *chan = to_dma_chan(dev); |
c13c8260 CL |
83 | unsigned long count = 0; |
84 | int i; | |
85 | ||
17f3ae08 | 86 | for_each_possible_cpu(i) |
c13c8260 CL |
87 | count += per_cpu_ptr(chan->local, i)->memcpy_count; |
88 | ||
89 | return sprintf(buf, "%lu\n", count); | |
90 | } | |
91 | ||
891f78ea TJ |
92 | static ssize_t show_bytes_transferred(struct device *dev, struct device_attribute *attr, |
93 | char *buf) | |
c13c8260 | 94 | { |
891f78ea | 95 | struct dma_chan *chan = to_dma_chan(dev); |
c13c8260 CL |
96 | unsigned long count = 0; |
97 | int i; | |
98 | ||
17f3ae08 | 99 | for_each_possible_cpu(i) |
c13c8260 CL |
100 | count += per_cpu_ptr(chan->local, i)->bytes_transferred; |
101 | ||
102 | return sprintf(buf, "%lu\n", count); | |
103 | } | |
104 | ||
891f78ea | 105 | static ssize_t show_in_use(struct device *dev, struct device_attribute *attr, char *buf) |
c13c8260 | 106 | { |
891f78ea | 107 | struct dma_chan *chan = to_dma_chan(dev); |
d379b01e DW |
108 | int in_use = 0; |
109 | ||
110 | if (unlikely(chan->slow_ref) && | |
111 | atomic_read(&chan->refcount.refcount) > 1) | |
112 | in_use = 1; | |
113 | else { | |
114 | if (local_read(&(per_cpu_ptr(chan->local, | |
115 | get_cpu())->refcount)) > 0) | |
116 | in_use = 1; | |
117 | put_cpu(); | |
118 | } | |
c13c8260 | 119 | |
d379b01e | 120 | return sprintf(buf, "%d\n", in_use); |
c13c8260 CL |
121 | } |
122 | ||
891f78ea | 123 | static struct device_attribute dma_attrs[] = { |
c13c8260 CL |
124 | __ATTR(memcpy_count, S_IRUGO, show_memcpy_count, NULL), |
125 | __ATTR(bytes_transferred, S_IRUGO, show_bytes_transferred, NULL), | |
126 | __ATTR(in_use, S_IRUGO, show_in_use, NULL), | |
127 | __ATTR_NULL | |
128 | }; | |
129 | ||
130 | static void dma_async_device_cleanup(struct kref *kref); | |
131 | ||
891f78ea | 132 | static void dma_dev_release(struct device *dev) |
c13c8260 | 133 | { |
891f78ea | 134 | struct dma_chan *chan = to_dma_chan(dev); |
c13c8260 CL |
135 | kref_put(&chan->device->refcount, dma_async_device_cleanup); |
136 | } | |
137 | ||
138 | static struct class dma_devclass = { | |
891f78ea TJ |
139 | .name = "dma", |
140 | .dev_attrs = dma_attrs, | |
141 | .dev_release = dma_dev_release, | |
c13c8260 CL |
142 | }; |
143 | ||
144 | /* --- client and device registration --- */ | |
145 | ||
d379b01e DW |
146 | #define dma_chan_satisfies_mask(chan, mask) \ |
147 | __dma_chan_satisfies_mask((chan), &(mask)) | |
148 | static int | |
149 | __dma_chan_satisfies_mask(struct dma_chan *chan, dma_cap_mask_t *want) | |
150 | { | |
151 | dma_cap_mask_t has; | |
152 | ||
153 | bitmap_and(has.bits, want->bits, chan->device->cap_mask.bits, | |
154 | DMA_TX_TYPE_END); | |
155 | return bitmap_equal(want->bits, has.bits, DMA_TX_TYPE_END); | |
156 | } | |
157 | ||
c13c8260 | 158 | /** |
d379b01e | 159 | * dma_client_chan_alloc - try to allocate channels to a client |
c13c8260 CL |
160 | * @client: &dma_client |
161 | * | |
162 | * Called with dma_list_mutex held. | |
163 | */ | |
d379b01e | 164 | static void dma_client_chan_alloc(struct dma_client *client) |
c13c8260 CL |
165 | { |
166 | struct dma_device *device; | |
167 | struct dma_chan *chan; | |
c13c8260 | 168 | int desc; /* allocated descriptor count */ |
d379b01e | 169 | enum dma_state_client ack; |
c13c8260 | 170 | |
d379b01e DW |
171 | /* Find a channel */ |
172 | list_for_each_entry(device, &dma_device_list, global_node) | |
c13c8260 | 173 | list_for_each_entry(chan, &device->channels, device_node) { |
d379b01e | 174 | if (!dma_chan_satisfies_mask(chan, client->cap_mask)) |
c13c8260 CL |
175 | continue; |
176 | ||
177 | desc = chan->device->device_alloc_chan_resources(chan); | |
178 | if (desc >= 0) { | |
d379b01e DW |
179 | ack = client->event_callback(client, |
180 | chan, | |
181 | DMA_RESOURCE_AVAILABLE); | |
182 | ||
183 | /* we are done once this client rejects | |
184 | * an available resource | |
185 | */ | |
348badf1 | 186 | if (ack == DMA_ACK) |
d379b01e | 187 | dma_chan_get(chan); |
348badf1 | 188 | else if (ack == DMA_NAK) |
d379b01e | 189 | return; |
c13c8260 CL |
190 | } |
191 | } | |
c13c8260 CL |
192 | } |
193 | ||
7405f74b DW |
194 | enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie) |
195 | { | |
196 | enum dma_status status; | |
197 | unsigned long dma_sync_wait_timeout = jiffies + msecs_to_jiffies(5000); | |
198 | ||
199 | dma_async_issue_pending(chan); | |
200 | do { | |
201 | status = dma_async_is_tx_complete(chan, cookie, NULL, NULL); | |
202 | if (time_after_eq(jiffies, dma_sync_wait_timeout)) { | |
203 | printk(KERN_ERR "dma_sync_wait_timeout!\n"); | |
204 | return DMA_ERROR; | |
205 | } | |
206 | } while (status == DMA_IN_PROGRESS); | |
207 | ||
208 | return status; | |
209 | } | |
210 | EXPORT_SYMBOL(dma_sync_wait); | |
211 | ||
c13c8260 | 212 | /** |
6508871e RD |
213 | * dma_chan_cleanup - release a DMA channel's resources |
214 | * @kref: kernel reference structure that contains the DMA channel device | |
c13c8260 CL |
215 | */ |
216 | void dma_chan_cleanup(struct kref *kref) | |
217 | { | |
218 | struct dma_chan *chan = container_of(kref, struct dma_chan, refcount); | |
219 | chan->device->device_free_chan_resources(chan); | |
c13c8260 CL |
220 | kref_put(&chan->device->refcount, dma_async_device_cleanup); |
221 | } | |
765e3d8a | 222 | EXPORT_SYMBOL(dma_chan_cleanup); |
c13c8260 CL |
223 | |
224 | static void dma_chan_free_rcu(struct rcu_head *rcu) | |
225 | { | |
226 | struct dma_chan *chan = container_of(rcu, struct dma_chan, rcu); | |
227 | int bias = 0x7FFFFFFF; | |
228 | int i; | |
17f3ae08 | 229 | for_each_possible_cpu(i) |
c13c8260 CL |
230 | bias -= local_read(&per_cpu_ptr(chan->local, i)->refcount); |
231 | atomic_sub(bias, &chan->refcount.refcount); | |
232 | kref_put(&chan->refcount, dma_chan_cleanup); | |
233 | } | |
234 | ||
d379b01e | 235 | static void dma_chan_release(struct dma_chan *chan) |
c13c8260 CL |
236 | { |
237 | atomic_add(0x7FFFFFFF, &chan->refcount.refcount); | |
238 | chan->slow_ref = 1; | |
239 | call_rcu(&chan->rcu, dma_chan_free_rcu); | |
240 | } | |
241 | ||
242 | /** | |
d379b01e | 243 | * dma_chans_notify_available - broadcast available channels to the clients |
c13c8260 | 244 | */ |
d379b01e | 245 | static void dma_clients_notify_available(void) |
c13c8260 CL |
246 | { |
247 | struct dma_client *client; | |
c13c8260 CL |
248 | |
249 | mutex_lock(&dma_list_mutex); | |
250 | ||
d379b01e DW |
251 | list_for_each_entry(client, &dma_client_list, global_node) |
252 | dma_client_chan_alloc(client); | |
c13c8260 CL |
253 | |
254 | mutex_unlock(&dma_list_mutex); | |
255 | } | |
256 | ||
257 | /** | |
d379b01e DW |
258 | * dma_chans_notify_available - tell the clients that a channel is going away |
259 | * @chan: channel on its way out | |
c13c8260 | 260 | */ |
d379b01e | 261 | static void dma_clients_notify_removed(struct dma_chan *chan) |
c13c8260 CL |
262 | { |
263 | struct dma_client *client; | |
d379b01e | 264 | enum dma_state_client ack; |
c13c8260 | 265 | |
d379b01e DW |
266 | mutex_lock(&dma_list_mutex); |
267 | ||
268 | list_for_each_entry(client, &dma_client_list, global_node) { | |
269 | ack = client->event_callback(client, chan, | |
270 | DMA_RESOURCE_REMOVED); | |
271 | ||
272 | /* client was holding resources for this channel so | |
273 | * free it | |
274 | */ | |
348badf1 | 275 | if (ack == DMA_ACK) |
d379b01e | 276 | dma_chan_put(chan); |
d379b01e | 277 | } |
c13c8260 | 278 | |
d379b01e DW |
279 | mutex_unlock(&dma_list_mutex); |
280 | } | |
c13c8260 | 281 | |
d379b01e DW |
282 | /** |
283 | * dma_async_client_register - register a &dma_client | |
284 | * @client: ptr to a client structure with valid 'event_callback' and 'cap_mask' | |
285 | */ | |
286 | void dma_async_client_register(struct dma_client *client) | |
287 | { | |
c13c8260 CL |
288 | mutex_lock(&dma_list_mutex); |
289 | list_add_tail(&client->global_node, &dma_client_list); | |
290 | mutex_unlock(&dma_list_mutex); | |
c13c8260 | 291 | } |
765e3d8a | 292 | EXPORT_SYMBOL(dma_async_client_register); |
c13c8260 CL |
293 | |
294 | /** | |
295 | * dma_async_client_unregister - unregister a client and free the &dma_client | |
6508871e | 296 | * @client: &dma_client to free |
c13c8260 CL |
297 | * |
298 | * Force frees any allocated DMA channels, frees the &dma_client memory | |
299 | */ | |
300 | void dma_async_client_unregister(struct dma_client *client) | |
301 | { | |
d379b01e | 302 | struct dma_device *device; |
c13c8260 | 303 | struct dma_chan *chan; |
d379b01e | 304 | enum dma_state_client ack; |
c13c8260 CL |
305 | |
306 | if (!client) | |
307 | return; | |
308 | ||
c13c8260 | 309 | mutex_lock(&dma_list_mutex); |
d379b01e DW |
310 | /* free all channels the client is holding */ |
311 | list_for_each_entry(device, &dma_device_list, global_node) | |
312 | list_for_each_entry(chan, &device->channels, device_node) { | |
313 | ack = client->event_callback(client, chan, | |
314 | DMA_RESOURCE_REMOVED); | |
315 | ||
348badf1 | 316 | if (ack == DMA_ACK) |
d379b01e | 317 | dma_chan_put(chan); |
d379b01e DW |
318 | } |
319 | ||
c13c8260 CL |
320 | list_del(&client->global_node); |
321 | mutex_unlock(&dma_list_mutex); | |
c13c8260 | 322 | } |
765e3d8a | 323 | EXPORT_SYMBOL(dma_async_client_unregister); |
c13c8260 CL |
324 | |
325 | /** | |
d379b01e DW |
326 | * dma_async_client_chan_request - send all available channels to the |
327 | * client that satisfy the capability mask | |
328 | * @client - requester | |
c13c8260 | 329 | */ |
d379b01e | 330 | void dma_async_client_chan_request(struct dma_client *client) |
c13c8260 | 331 | { |
d379b01e DW |
332 | mutex_lock(&dma_list_mutex); |
333 | dma_client_chan_alloc(client); | |
334 | mutex_unlock(&dma_list_mutex); | |
c13c8260 | 335 | } |
765e3d8a | 336 | EXPORT_SYMBOL(dma_async_client_chan_request); |
c13c8260 CL |
337 | |
338 | /** | |
6508871e | 339 | * dma_async_device_register - registers DMA devices found |
c13c8260 CL |
340 | * @device: &dma_device |
341 | */ | |
342 | int dma_async_device_register(struct dma_device *device) | |
343 | { | |
344 | static int id; | |
ff487fb7 | 345 | int chancnt = 0, rc; |
c13c8260 CL |
346 | struct dma_chan* chan; |
347 | ||
348 | if (!device) | |
349 | return -ENODEV; | |
350 | ||
7405f74b DW |
351 | /* validate device routines */ |
352 | BUG_ON(dma_has_cap(DMA_MEMCPY, device->cap_mask) && | |
353 | !device->device_prep_dma_memcpy); | |
354 | BUG_ON(dma_has_cap(DMA_XOR, device->cap_mask) && | |
355 | !device->device_prep_dma_xor); | |
356 | BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) && | |
357 | !device->device_prep_dma_zero_sum); | |
358 | BUG_ON(dma_has_cap(DMA_MEMSET, device->cap_mask) && | |
359 | !device->device_prep_dma_memset); | |
360 | BUG_ON(dma_has_cap(DMA_ZERO_SUM, device->cap_mask) && | |
361 | !device->device_prep_dma_interrupt); | |
362 | ||
363 | BUG_ON(!device->device_alloc_chan_resources); | |
364 | BUG_ON(!device->device_free_chan_resources); | |
365 | BUG_ON(!device->device_dependency_added); | |
366 | BUG_ON(!device->device_is_tx_complete); | |
367 | BUG_ON(!device->device_issue_pending); | |
368 | BUG_ON(!device->dev); | |
369 | ||
c13c8260 CL |
370 | init_completion(&device->done); |
371 | kref_init(&device->refcount); | |
372 | device->dev_id = id++; | |
373 | ||
374 | /* represent channels in sysfs. Probably want devs too */ | |
375 | list_for_each_entry(chan, &device->channels, device_node) { | |
376 | chan->local = alloc_percpu(typeof(*chan->local)); | |
377 | if (chan->local == NULL) | |
378 | continue; | |
379 | ||
380 | chan->chan_id = chancnt++; | |
891f78ea TJ |
381 | chan->dev.class = &dma_devclass; |
382 | chan->dev.parent = NULL; | |
383 | snprintf(chan->dev.bus_id, BUS_ID_SIZE, "dma%dchan%d", | |
c13c8260 CL |
384 | device->dev_id, chan->chan_id); |
385 | ||
891f78ea | 386 | rc = device_register(&chan->dev); |
ff487fb7 JG |
387 | if (rc) { |
388 | chancnt--; | |
389 | free_percpu(chan->local); | |
390 | chan->local = NULL; | |
391 | goto err_out; | |
392 | } | |
393 | ||
348badf1 HS |
394 | /* One for the channel, one of the class device */ |
395 | kref_get(&device->refcount); | |
c13c8260 | 396 | kref_get(&device->refcount); |
d379b01e DW |
397 | kref_init(&chan->refcount); |
398 | chan->slow_ref = 0; | |
399 | INIT_RCU_HEAD(&chan->rcu); | |
c13c8260 CL |
400 | } |
401 | ||
402 | mutex_lock(&dma_list_mutex); | |
403 | list_add_tail(&device->global_node, &dma_device_list); | |
404 | mutex_unlock(&dma_list_mutex); | |
405 | ||
d379b01e | 406 | dma_clients_notify_available(); |
c13c8260 CL |
407 | |
408 | return 0; | |
ff487fb7 JG |
409 | |
410 | err_out: | |
411 | list_for_each_entry(chan, &device->channels, device_node) { | |
412 | if (chan->local == NULL) | |
413 | continue; | |
414 | kref_put(&device->refcount, dma_async_device_cleanup); | |
891f78ea | 415 | device_unregister(&chan->dev); |
ff487fb7 JG |
416 | chancnt--; |
417 | free_percpu(chan->local); | |
418 | } | |
419 | return rc; | |
c13c8260 | 420 | } |
765e3d8a | 421 | EXPORT_SYMBOL(dma_async_device_register); |
c13c8260 CL |
422 | |
423 | /** | |
6508871e RD |
424 | * dma_async_device_cleanup - function called when all references are released |
425 | * @kref: kernel reference object | |
c13c8260 CL |
426 | */ |
427 | static void dma_async_device_cleanup(struct kref *kref) | |
428 | { | |
429 | struct dma_device *device; | |
430 | ||
431 | device = container_of(kref, struct dma_device, refcount); | |
432 | complete(&device->done); | |
433 | } | |
434 | ||
6508871e RD |
435 | /** |
436 | * dma_async_device_unregister - unregisters DMA devices | |
437 | * @device: &dma_device | |
438 | */ | |
439 | void dma_async_device_unregister(struct dma_device *device) | |
c13c8260 CL |
440 | { |
441 | struct dma_chan *chan; | |
c13c8260 CL |
442 | |
443 | mutex_lock(&dma_list_mutex); | |
444 | list_del(&device->global_node); | |
445 | mutex_unlock(&dma_list_mutex); | |
446 | ||
447 | list_for_each_entry(chan, &device->channels, device_node) { | |
d379b01e | 448 | dma_clients_notify_removed(chan); |
891f78ea | 449 | device_unregister(&chan->dev); |
d379b01e | 450 | dma_chan_release(chan); |
c13c8260 | 451 | } |
c13c8260 CL |
452 | |
453 | kref_put(&device->refcount, dma_async_device_cleanup); | |
454 | wait_for_completion(&device->done); | |
455 | } | |
765e3d8a | 456 | EXPORT_SYMBOL(dma_async_device_unregister); |
c13c8260 | 457 | |
7405f74b DW |
458 | /** |
459 | * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses | |
460 | * @chan: DMA channel to offload copy to | |
461 | * @dest: destination address (virtual) | |
462 | * @src: source address (virtual) | |
463 | * @len: length | |
464 | * | |
465 | * Both @dest and @src must be mappable to a bus address according to the | |
466 | * DMA mapping API rules for streaming mappings. | |
467 | * Both @dest and @src must stay memory resident (kernel memory or locked | |
468 | * user space pages). | |
469 | */ | |
470 | dma_cookie_t | |
471 | dma_async_memcpy_buf_to_buf(struct dma_chan *chan, void *dest, | |
472 | void *src, size_t len) | |
473 | { | |
474 | struct dma_device *dev = chan->device; | |
475 | struct dma_async_tx_descriptor *tx; | |
476 | dma_addr_t addr; | |
477 | dma_cookie_t cookie; | |
478 | int cpu; | |
479 | ||
480 | tx = dev->device_prep_dma_memcpy(chan, len, 0); | |
481 | if (!tx) | |
482 | return -ENOMEM; | |
483 | ||
484 | tx->ack = 1; | |
485 | tx->callback = NULL; | |
486 | addr = dma_map_single(dev->dev, src, len, DMA_TO_DEVICE); | |
487 | tx->tx_set_src(addr, tx, 0); | |
488 | addr = dma_map_single(dev->dev, dest, len, DMA_FROM_DEVICE); | |
489 | tx->tx_set_dest(addr, tx, 0); | |
490 | cookie = tx->tx_submit(tx); | |
491 | ||
492 | cpu = get_cpu(); | |
493 | per_cpu_ptr(chan->local, cpu)->bytes_transferred += len; | |
494 | per_cpu_ptr(chan->local, cpu)->memcpy_count++; | |
495 | put_cpu(); | |
496 | ||
497 | return cookie; | |
498 | } | |
499 | EXPORT_SYMBOL(dma_async_memcpy_buf_to_buf); | |
500 | ||
501 | /** | |
502 | * dma_async_memcpy_buf_to_pg - offloaded copy from address to page | |
503 | * @chan: DMA channel to offload copy to | |
504 | * @page: destination page | |
505 | * @offset: offset in page to copy to | |
506 | * @kdata: source address (virtual) | |
507 | * @len: length | |
508 | * | |
509 | * Both @page/@offset and @kdata must be mappable to a bus address according | |
510 | * to the DMA mapping API rules for streaming mappings. | |
511 | * Both @page/@offset and @kdata must stay memory resident (kernel memory or | |
512 | * locked user space pages) | |
513 | */ | |
514 | dma_cookie_t | |
515 | dma_async_memcpy_buf_to_pg(struct dma_chan *chan, struct page *page, | |
516 | unsigned int offset, void *kdata, size_t len) | |
517 | { | |
518 | struct dma_device *dev = chan->device; | |
519 | struct dma_async_tx_descriptor *tx; | |
520 | dma_addr_t addr; | |
521 | dma_cookie_t cookie; | |
522 | int cpu; | |
523 | ||
524 | tx = dev->device_prep_dma_memcpy(chan, len, 0); | |
525 | if (!tx) | |
526 | return -ENOMEM; | |
527 | ||
528 | tx->ack = 1; | |
529 | tx->callback = NULL; | |
530 | addr = dma_map_single(dev->dev, kdata, len, DMA_TO_DEVICE); | |
531 | tx->tx_set_src(addr, tx, 0); | |
532 | addr = dma_map_page(dev->dev, page, offset, len, DMA_FROM_DEVICE); | |
533 | tx->tx_set_dest(addr, tx, 0); | |
534 | cookie = tx->tx_submit(tx); | |
535 | ||
536 | cpu = get_cpu(); | |
537 | per_cpu_ptr(chan->local, cpu)->bytes_transferred += len; | |
538 | per_cpu_ptr(chan->local, cpu)->memcpy_count++; | |
539 | put_cpu(); | |
540 | ||
541 | return cookie; | |
542 | } | |
543 | EXPORT_SYMBOL(dma_async_memcpy_buf_to_pg); | |
544 | ||
545 | /** | |
546 | * dma_async_memcpy_pg_to_pg - offloaded copy from page to page | |
547 | * @chan: DMA channel to offload copy to | |
548 | * @dest_pg: destination page | |
549 | * @dest_off: offset in page to copy to | |
550 | * @src_pg: source page | |
551 | * @src_off: offset in page to copy from | |
552 | * @len: length | |
553 | * | |
554 | * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus | |
555 | * address according to the DMA mapping API rules for streaming mappings. | |
556 | * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident | |
557 | * (kernel memory or locked user space pages). | |
558 | */ | |
559 | dma_cookie_t | |
560 | dma_async_memcpy_pg_to_pg(struct dma_chan *chan, struct page *dest_pg, | |
561 | unsigned int dest_off, struct page *src_pg, unsigned int src_off, | |
562 | size_t len) | |
563 | { | |
564 | struct dma_device *dev = chan->device; | |
565 | struct dma_async_tx_descriptor *tx; | |
566 | dma_addr_t addr; | |
567 | dma_cookie_t cookie; | |
568 | int cpu; | |
569 | ||
570 | tx = dev->device_prep_dma_memcpy(chan, len, 0); | |
571 | if (!tx) | |
572 | return -ENOMEM; | |
573 | ||
574 | tx->ack = 1; | |
575 | tx->callback = NULL; | |
576 | addr = dma_map_page(dev->dev, src_pg, src_off, len, DMA_TO_DEVICE); | |
577 | tx->tx_set_src(addr, tx, 0); | |
578 | addr = dma_map_page(dev->dev, dest_pg, dest_off, len, DMA_FROM_DEVICE); | |
579 | tx->tx_set_dest(addr, tx, 0); | |
580 | cookie = tx->tx_submit(tx); | |
581 | ||
582 | cpu = get_cpu(); | |
583 | per_cpu_ptr(chan->local, cpu)->bytes_transferred += len; | |
584 | per_cpu_ptr(chan->local, cpu)->memcpy_count++; | |
585 | put_cpu(); | |
586 | ||
587 | return cookie; | |
588 | } | |
589 | EXPORT_SYMBOL(dma_async_memcpy_pg_to_pg); | |
590 | ||
591 | void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx, | |
592 | struct dma_chan *chan) | |
593 | { | |
594 | tx->chan = chan; | |
595 | spin_lock_init(&tx->lock); | |
596 | INIT_LIST_HEAD(&tx->depend_node); | |
597 | INIT_LIST_HEAD(&tx->depend_list); | |
598 | } | |
599 | EXPORT_SYMBOL(dma_async_tx_descriptor_init); | |
600 | ||
c13c8260 CL |
601 | static int __init dma_bus_init(void) |
602 | { | |
603 | mutex_init(&dma_list_mutex); | |
604 | return class_register(&dma_devclass); | |
605 | } | |
c13c8260 CL |
606 | subsys_initcall(dma_bus_init); |
607 |