dma-nommu.c 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * DMA mapping support for platforms lacking IOMMUs.
 *
 * Copyright (C) 2009  Paul Mundt
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
#include <linux/dma-mapping.h>
#include <linux/io.h>
12
#include <asm/cacheflush.h>
13 14 15 16

static dma_addr_t nommu_map_page(struct device *dev, struct page *page,
				 unsigned long offset, size_t size,
				 enum dma_data_direction dir,
17
				 unsigned long attrs)
18
{
19 20
	dma_addr_t addr = page_to_phys(page) + offset
		- PFN_PHYS(dev->dma_pfn_offset);
21 22

	WARN_ON(size == 0);
23 24

	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
25
		sh_sync_dma_for_device(page_address(page) + offset, size, dir);
26 27 28 29 30 31

	return addr;
}

static int nommu_map_sg(struct device *dev, struct scatterlist *sg,
			int nents, enum dma_data_direction dir,
32
			unsigned long attrs)
33 34 35 36 37 38 39
{
	struct scatterlist *s;
	int i;

	WARN_ON(nents == 0 || sg[0].length == 0);

	for_each_sg(sg, s, nents, i) {
40 41
		dma_addr_t offset = PFN_PHYS(dev->dma_pfn_offset);

42 43
		BUG_ON(!sg_page(s));

44
		if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
45
			sh_sync_dma_for_device(sg_virt(s), s->length, dir);
46

47
		s->dma_address = sg_phys(s) - offset;
48 49 50 51 52 53
		s->dma_length = s->length;
	}

	return nents;
}

54
#ifdef CONFIG_DMA_NONCOHERENT
55
static void nommu_sync_single_for_device(struct device *dev, dma_addr_t addr,
56 57
			      size_t size, enum dma_data_direction dir)
{
58
	sh_sync_dma_for_device(phys_to_virt(addr), size, dir);
59 60
}

61
static void nommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
62 63 64 65 66 67
			  int nelems, enum dma_data_direction dir)
{
	struct scatterlist *s;
	int i;

	for_each_sg(sg, s, nelems, i)
68
		sh_sync_dma_for_device(sg_virt(s), s->length, dir);
69
}
70
#endif
71

72
const struct dma_map_ops nommu_dma_ops = {
73 74
	.alloc			= dma_generic_alloc_coherent,
	.free			= dma_generic_free_coherent,
75 76
	.map_page		= nommu_map_page,
	.map_sg			= nommu_map_sg,
77
#ifdef CONFIG_DMA_NONCOHERENT
78 79
	.sync_single_for_device	= nommu_sync_single_for_device,
	.sync_sg_for_device	= nommu_sync_sg_for_device,
80
#endif
81 82 83 84 85 86 87 88
};

void __init no_iommu_init(void)
{
	if (dma_ops)
		return;
	dma_ops = &nommu_dma_ops;
}