async_memcpy.c 3.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * copy offload engine support
 *
 * Copyright © 2006, Intel Corporation.
 *
 *      Dan Williams <dan.j.williams@intel.com>
 *
 *      with architecture considerations by:
 *      Neil Brown <neilb@suse.de>
 *      Jeff Garzik <jeff@garzik.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
#include <linux/kernel.h>
#include <linux/highmem.h>
28
#include <linux/module.h>
29 30 31 32 33 34 35 36
#include <linux/mm.h>
#include <linux/dma-mapping.h>
#include <linux/async_tx.h>

/**
 * async_memcpy - attempt to copy memory with a dma engine.
 * @dest: destination page
 * @src: src page
37 38
 * @dest_offset: offset into 'dest' to start transaction
 * @src_offset: offset into 'src' to start transaction
39
 * @len: length in bytes
40 41 42
 * @submit: submission / completion modifiers
 *
 * honored flags: ASYNC_TX_ACK
43 44 45
 */
struct dma_async_tx_descriptor *
async_memcpy(struct page *dest, struct page *src, unsigned int dest_offset,
46 47
	     unsigned int src_offset, size_t len,
	     struct async_submit_ctl *submit)
48
{
49
	struct dma_chan *chan = async_tx_find_channel(submit, DMA_MEMCPY,
50
						      &dest, 1, &src, 1, len);
51
	struct dma_device *device = chan ? chan->device : NULL;
52
	struct dma_async_tx_descriptor *tx = NULL;
53
	struct dmaengine_unmap_data *unmap = NULL;
54

55
	if (device)
56
		unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
57 58

	if (unmap && is_dma_copy_aligned(device, src_offset, dest_offset, len)) {
59
		unsigned long dma_prep_flags = 0;
60

61 62 63 64
		if (submit->cb_fn)
			dma_prep_flags |= DMA_PREP_INTERRUPT;
		if (submit->flags & ASYNC_TX_FENCE)
			dma_prep_flags |= DMA_PREP_FENCE;
65 66 67 68 69 70 71 72 73 74 75 76

		unmap->to_cnt = 1;
		unmap->addr[0] = dma_map_page(device->dev, src, src_offset, len,
					      DMA_TO_DEVICE);
		unmap->from_cnt = 1;
		unmap->addr[1] = dma_map_page(device->dev, dest, dest_offset, len,
					      DMA_FROM_DEVICE);
		unmap->len = len;

		tx = device->device_prep_dma_memcpy(chan, unmap->addr[1],
						    unmap->addr[0], len,
						    dma_prep_flags);
77
	}
78

79
	if (tx) {
80
		pr_debug("%s: (async) len: %zu\n", __func__, len);
81 82

		dma_set_unmap(tx, unmap);
83
		async_tx_submit(chan, tx, submit);
84
	} else {
85
		void *dest_buf, *src_buf;
86
		pr_debug("%s: (sync) len: %zu\n", __func__, len);
87 88

		/* wait for any prerequisite operations */
89
		async_tx_quiesce(&submit->depend_tx);
90

91 92
		dest_buf = kmap_atomic(dest) + dest_offset;
		src_buf = kmap_atomic(src) + src_offset;
93 94 95

		memcpy(dest_buf, src_buf, len);

96 97
		kunmap_atomic(src_buf);
		kunmap_atomic(dest_buf);
98

99
		async_tx_sync_epilog(submit);
100 101
	}

102 103
	dmaengine_unmap_put(unmap);

104 105 106 107 108 109 110
	return tx;
}
EXPORT_SYMBOL_GPL(async_memcpy);

MODULE_AUTHOR("Intel Corporation");
MODULE_DESCRIPTION("asynchronous memcpy api");
MODULE_LICENSE("GPL");