dma.c 3.4 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 28 29 30 31 32 33 34 35 36 37 38
/*
 *  Copyright (C) 2011 Texas Instruments Incorporated
 *  Author: Mark Salter <msalter@redhat.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 */
#include <linux/module.h>
#include <linux/dma-mapping.h>
#include <linux/mm.h>
#include <linux/mm_types.h>
#include <linux/scatterlist.h>

#include <asm/cacheflush.h>

static void c6x_dma_sync(dma_addr_t handle, size_t size,
			 enum dma_data_direction dir)
{
	unsigned long paddr = handle;

	BUG_ON(!valid_dma_direction(dir));

	switch (dir) {
	case DMA_FROM_DEVICE:
		L2_cache_block_invalidate(paddr, paddr + size);
		break;
	case DMA_TO_DEVICE:
		L2_cache_block_writeback(paddr, paddr + size);
		break;
	case DMA_BIDIRECTIONAL:
		L2_cache_block_writeback_invalidate(paddr, paddr + size);
		break;
	default:
		break;
	}
}

C
Christoph Hellwig 已提交
39 40
static dma_addr_t c6x_dma_map_page(struct device *dev, struct page *page,
		unsigned long offset, size_t size, enum dma_data_direction dir,
41
		unsigned long attrs)
42
{
C
Christoph Hellwig 已提交
43
	dma_addr_t handle = virt_to_phys(page_address(page) + offset);
44

45 46 47
	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
		c6x_dma_sync(handle, size, dir);

C
Christoph Hellwig 已提交
48
	return handle;
49 50
}

C
Christoph Hellwig 已提交
51
static void c6x_dma_unmap_page(struct device *dev, dma_addr_t handle,
52
		size_t size, enum dma_data_direction dir, unsigned long attrs)
53
{
54 55
	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
		c6x_dma_sync(handle, size, dir);
56 57
}

C
Christoph Hellwig 已提交
58
static int c6x_dma_map_sg(struct device *dev, struct scatterlist *sglist,
59
		int nents, enum dma_data_direction dir, unsigned long attrs)
60 61 62 63
{
	struct scatterlist *sg;
	int i;

C
Christoph Hellwig 已提交
64 65
	for_each_sg(sglist, sg, nents, i) {
		sg->dma_address = sg_phys(sg);
66 67
		if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
			c6x_dma_sync(sg->dma_address, sg->length, dir);
C
Christoph Hellwig 已提交
68
	}
69 70 71 72

	return nents;
}

C
Christoph Hellwig 已提交
73
static void c6x_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
74
		  int nents, enum dma_data_direction dir, unsigned long attrs)
75 76 77 78
{
	struct scatterlist *sg;
	int i;

79 80 81
	if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
		return;

82
	for_each_sg(sglist, sg, nents, i)
C
Christoph Hellwig 已提交
83
		c6x_dma_sync(sg_dma_address(sg), sg->length, dir);
84 85
}

C
Christoph Hellwig 已提交
86 87
static void c6x_dma_sync_single_for_cpu(struct device *dev, dma_addr_t handle,
		size_t size, enum dma_data_direction dir)
88 89 90 91 92
{
	c6x_dma_sync(handle, size, dir);

}

C
Christoph Hellwig 已提交
93 94
static void c6x_dma_sync_single_for_device(struct device *dev,
		dma_addr_t handle, size_t size, enum dma_data_direction dir)
95 96 97 98 99
{
	c6x_dma_sync(handle, size, dir);

}

C
Christoph Hellwig 已提交
100 101 102
static void c6x_dma_sync_sg_for_cpu(struct device *dev,
		struct scatterlist *sglist, int nents,
		enum dma_data_direction dir)
103 104 105 106 107
{
	struct scatterlist *sg;
	int i;

	for_each_sg(sglist, sg, nents, i)
C
Christoph Hellwig 已提交
108
		c6x_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
109 110 111 112
					sg->length, dir);

}

C
Christoph Hellwig 已提交
113 114 115
static void c6x_dma_sync_sg_for_device(struct device *dev,
		struct scatterlist *sglist, int nents,
		enum dma_data_direction dir)
116 117 118 119 120
{
	struct scatterlist *sg;
	int i;

	for_each_sg(sglist, sg, nents, i)
C
Christoph Hellwig 已提交
121
		c6x_dma_sync_single_for_device(dev, sg_dma_address(sg),
122 123 124 125
					   sg->length, dir);

}

126
const struct dma_map_ops c6x_dma_ops = {
C
Christoph Hellwig 已提交
127 128 129 130 131 132 133 134 135 136 137 138
	.alloc			= c6x_dma_alloc,
	.free			= c6x_dma_free,
	.map_page		= c6x_dma_map_page,
	.unmap_page		= c6x_dma_unmap_page,
	.map_sg			= c6x_dma_map_sg,
	.unmap_sg		= c6x_dma_unmap_sg,
	.sync_single_for_device	= c6x_dma_sync_single_for_device,
	.sync_single_for_cpu	= c6x_dma_sync_single_for_cpu,
	.sync_sg_for_device	= c6x_dma_sync_sg_for_device,
	.sync_sg_for_cpu	= c6x_dma_sync_sg_for_cpu,
};
EXPORT_SYMBOL(c6x_dma_ops);