blob: c538e30e990941f464e4576a8e9e4256416813fb [file] [log] [blame]
Thomas Gleixnera61127c2019-05-29 16:57:49 -07001// SPDX-License-Identifier: GPL-2.0-only
Dan Williams9bc89cd2007-01-02 11:10:44 -07002/*
3 * copy offload engine support
4 *
5 * Copyright © 2006, Intel Corporation.
6 *
7 * Dan Williams <dan.j.williams@intel.com>
8 *
9 * with architecture considerations by:
10 * Neil Brown <neilb@suse.de>
11 * Jeff Garzik <jeff@garzik.org>
Dan Williams9bc89cd2007-01-02 11:10:44 -070012 */
13#include <linux/kernel.h>
14#include <linux/highmem.h>
Paul Gortmaker4bb33cc2011-05-27 14:41:48 -040015#include <linux/module.h>
Dan Williams9bc89cd2007-01-02 11:10:44 -070016#include <linux/mm.h>
17#include <linux/dma-mapping.h>
18#include <linux/async_tx.h>
19
20/**
21 * async_memcpy - attempt to copy memory with a dma engine.
22 * @dest: destination page
23 * @src: src page
Dan Williamsa08abd82009-06-03 11:43:59 -070024 * @dest_offset: offset into 'dest' to start transaction
25 * @src_offset: offset into 'src' to start transaction
Dan Williams9bc89cd2007-01-02 11:10:44 -070026 * @len: length in bytes
Dan Williamsa08abd82009-06-03 11:43:59 -070027 * @submit: submission / completion modifiers
28 *
29 * honored flags: ASYNC_TX_ACK
Dan Williams9bc89cd2007-01-02 11:10:44 -070030 */
31struct dma_async_tx_descriptor *
32async_memcpy(struct page *dest, struct page *src, unsigned int dest_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -070033 unsigned int src_offset, size_t len,
34 struct async_submit_ctl *submit)
Dan Williams9bc89cd2007-01-02 11:10:44 -070035{
Dan Williamsa08abd82009-06-03 11:43:59 -070036 struct dma_chan *chan = async_tx_find_channel(submit, DMA_MEMCPY,
Dan Williams47437b22008-02-02 19:49:59 -070037 &dest, 1, &src, 1, len);
Dan Williams9bc89cd2007-01-02 11:10:44 -070038 struct dma_device *device = chan ? chan->device : NULL;
Dan Williams00367312008-02-02 19:49:57 -070039 struct dma_async_tx_descriptor *tx = NULL;
Dan Williams89716462013-10-18 19:35:25 +020040 struct dmaengine_unmap_data *unmap = NULL;
Dan Williams9bc89cd2007-01-02 11:10:44 -070041
Dan Williams89716462013-10-18 19:35:25 +020042 if (device)
NeilBrownb02bab62016-01-07 11:02:34 +110043 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
Dan Williams89716462013-10-18 19:35:25 +020044
45 if (unmap && is_dma_copy_aligned(device, src_offset, dest_offset, len)) {
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +020046 unsigned long dma_prep_flags = 0;
Dan Williams9bc89cd2007-01-02 11:10:44 -070047
Dan Williams0403e382009-09-08 17:42:50 -070048 if (submit->cb_fn)
49 dma_prep_flags |= DMA_PREP_INTERRUPT;
50 if (submit->flags & ASYNC_TX_FENCE)
51 dma_prep_flags |= DMA_PREP_FENCE;
Dan Williams00367312008-02-02 19:49:57 -070052
Dan Williams89716462013-10-18 19:35:25 +020053 unmap->to_cnt = 1;
54 unmap->addr[0] = dma_map_page(device->dev, src, src_offset, len,
55 DMA_TO_DEVICE);
56 unmap->from_cnt = 1;
57 unmap->addr[1] = dma_map_page(device->dev, dest, dest_offset, len,
58 DMA_FROM_DEVICE);
59 unmap->len = len;
Dan Williams00367312008-02-02 19:49:57 -070060
Dan Williams89716462013-10-18 19:35:25 +020061 tx = device->device_prep_dma_memcpy(chan, unmap->addr[1],
62 unmap->addr[0], len,
63 dma_prep_flags);
Dan Williams00367312008-02-02 19:49:57 -070064 }
65
66 if (tx) {
Dan Williams3280ab3e2008-03-13 17:45:28 -070067 pr_debug("%s: (async) len: %zu\n", __func__, len);
Dan Williams89716462013-10-18 19:35:25 +020068
69 dma_set_unmap(tx, unmap);
Dan Williamsa08abd82009-06-03 11:43:59 -070070 async_tx_submit(chan, tx, submit);
Dan Williams00367312008-02-02 19:49:57 -070071 } else {
Dan Williams9bc89cd2007-01-02 11:10:44 -070072 void *dest_buf, *src_buf;
Dan Williams3280ab3e2008-03-13 17:45:28 -070073 pr_debug("%s: (sync) len: %zu\n", __func__, len);
Dan Williams9bc89cd2007-01-02 11:10:44 -070074
75 /* wait for any prerequisite operations */
Dan Williamsa08abd82009-06-03 11:43:59 -070076 async_tx_quiesce(&submit->depend_tx);
Dan Williams9bc89cd2007-01-02 11:10:44 -070077
Cong Wangf0dfc0b2011-11-25 23:14:17 +080078 dest_buf = kmap_atomic(dest) + dest_offset;
79 src_buf = kmap_atomic(src) + src_offset;
Dan Williams9bc89cd2007-01-02 11:10:44 -070080
81 memcpy(dest_buf, src_buf, len);
82
Cong Wangf0dfc0b2011-11-25 23:14:17 +080083 kunmap_atomic(src_buf);
84 kunmap_atomic(dest_buf);
Dan Williams9bc89cd2007-01-02 11:10:44 -070085
Dan Williamsa08abd82009-06-03 11:43:59 -070086 async_tx_sync_epilog(submit);
Dan Williams9bc89cd2007-01-02 11:10:44 -070087 }
88
Dan Williams89716462013-10-18 19:35:25 +020089 dmaengine_unmap_put(unmap);
90
Dan Williams9bc89cd2007-01-02 11:10:44 -070091 return tx;
92}
93EXPORT_SYMBOL_GPL(async_memcpy);
94
Dan Williams9bc89cd2007-01-02 11:10:44 -070095MODULE_AUTHOR("Intel Corporation");
96MODULE_DESCRIPTION("asynchronous memcpy api");
97MODULE_LICENSE("GPL");