dma.c 3.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
L
Linus Torvalds 已提交
3
 *
4
 * Provide default implementations of the DMA mapping callbacks for
5
 * directly mapped busses.
L
Linus Torvalds 已提交
6 7 8 9 10
 */

#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <asm/bug.h>
11
#include <asm/abs_addr.h>
L
Linus Torvalds 已提交
12

13 14
/*
 * Generic direct DMA implementation
15
 *
16 17 18
 * This implementation supports a per-device offset that can be applied if
 * the address at which memory is visible to devices is not 0. Platform code
 * can set archdata.dma_data to an unsigned long holding the offset. By
19
 * default the offset is PCI_DRAM_OFFSET.
20
 */
21

22 23
static unsigned long get_dma_direct_offset(struct device *dev)
{
24 25 26 27
	if (dev)
		return (unsigned long)dev->archdata.dma_data;

	return PCI_DRAM_OFFSET;
28 29
}

30 31
void *dma_direct_alloc_coherent(struct device *dev, size_t size,
				dma_addr_t *dma_handle, gfp_t flag)
32
{
33
	void *ret;
34
#ifdef CONFIG_NOT_COHERENT_CACHE
35 36 37 38 39
	ret = __dma_alloc_coherent(size, dma_handle, flag);
	if (ret == NULL)
		return NULL;
	*dma_handle += get_dma_direct_offset(dev);
	return ret;
40
#else
41
	struct page *page;
B
Becky Bruce 已提交
42
	int node = dev_to_node(dev);
43

44 45 46
	/* ignore region specifiers */
	flag  &= ~(__GFP_HIGHMEM);

47 48 49 50 51
	page = alloc_pages_node(node, flag, get_order(size));
	if (page == NULL)
		return NULL;
	ret = page_address(page);
	memset(ret, 0, size);
52
	*dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
53

54
	return ret;
55
#endif
L
Linus Torvalds 已提交
56 57
}

58 59
void dma_direct_free_coherent(struct device *dev, size_t size,
			      void *vaddr, dma_addr_t dma_handle)
L
Linus Torvalds 已提交
60
{
61 62 63
#ifdef CONFIG_NOT_COHERENT_CACHE
	__dma_free_coherent(size, vaddr);
#else
64
	free_pages((unsigned long)vaddr, get_order(size));
65
#endif
L
Linus Torvalds 已提交
66 67
}

J
Jens Axboe 已提交
68
static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
69 70
			     int nents, enum dma_data_direction direction,
			     struct dma_attrs *attrs)
L
Linus Torvalds 已提交
71
{
J
Jens Axboe 已提交
72
	struct scatterlist *sg;
73
	int i;
L
Linus Torvalds 已提交
74

J
Jens Axboe 已提交
75
	for_each_sg(sgl, sg, nents, i) {
76
		sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
77
		sg->dma_length = sg->length;
78
		__dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
79
	}
80

81
	return nents;
L
Linus Torvalds 已提交
82 83
}

84
static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
85 86
				int nents, enum dma_data_direction direction,
				struct dma_attrs *attrs)
L
Linus Torvalds 已提交
87
{
88
}
89

90 91
static int dma_direct_dma_supported(struct device *dev, u64 mask)
{
92
#ifdef CONFIG_PPC64
93 94 95 96 97
	/* Could be improved to check for memory though it better be
	 * done via some global so platforms can set the limit in case
	 * they have limited DMA windows
	 */
	return mask >= DMA_32BIT_MASK;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
#else
	return 1;
#endif
}

static inline dma_addr_t dma_direct_map_page(struct device *dev,
					     struct page *page,
					     unsigned long offset,
					     size_t size,
					     enum dma_data_direction dir,
					     struct dma_attrs *attrs)
{
	BUG_ON(dir == DMA_NONE);
	__dma_sync_page(page, offset, size, dir);
	return page_to_phys(page) + offset + get_dma_direct_offset(dev);
}

static inline void dma_direct_unmap_page(struct device *dev,
					 dma_addr_t dma_address,
					 size_t size,
					 enum dma_data_direction direction,
					 struct dma_attrs *attrs)
{
L
Linus Torvalds 已提交
121
}
122 123 124 125 126 127 128

struct dma_mapping_ops dma_direct_ops = {
	.alloc_coherent	= dma_direct_alloc_coherent,
	.free_coherent	= dma_direct_free_coherent,
	.map_sg		= dma_direct_map_sg,
	.unmap_sg	= dma_direct_unmap_sg,
	.dma_supported	= dma_direct_dma_supported,
129 130
	.map_page	= dma_direct_map_page,
	.unmap_page	= dma_direct_unmap_page,
131 132
};
EXPORT_SYMBOL(dma_direct_ops);