dma-mapping.h 5.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
#ifndef __ASM_SH_DMA_MAPPING_H
#define __ASM_SH_DMA_MAPPING_H

#include <linux/mm.h>
5
#include <linux/scatterlist.h>
6
#include <linux/dma-debug.h>
P
Paul Mundt 已提交
7
#include <asm/cacheflush.h>
L
Linus Torvalds 已提交
8
#include <asm/io.h>
9
#include <asm-generic/dma-coherent.h>
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

extern struct bus_type pci_bus_type;

#define dma_supported(dev, mask)	(1)

static inline int dma_set_mask(struct device *dev, u64 mask)
{
	if (!dev->dma_mask || !dma_supported(dev, mask))
		return -EIO;

	*dev->dma_mask = mask;

	return 0;
}

25 26
void *dma_alloc_coherent(struct device *dev, size_t size,
			 dma_addr_t *dma_handle, gfp_t flag);
L
Linus Torvalds 已提交
27

28 29
void dma_free_coherent(struct device *dev, size_t size,
		       void *vaddr, dma_addr_t dma_handle);
L
Linus Torvalds 已提交
30

31 32
void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
		    enum dma_data_direction dir);
L
Linus Torvalds 已提交
33

34 35 36 37
#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
#define dma_is_consistent(d, h) (1)

L
Linus Torvalds 已提交
38 39 40 41
static inline dma_addr_t dma_map_single(struct device *dev,
					void *ptr, size_t size,
					enum dma_data_direction dir)
{
42 43
	dma_addr_t addr = virt_to_phys(ptr);

L
Linus Torvalds 已提交
44 45
#if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
	if (dev->bus == &pci_bus_type)
46
		return addr;
L
Linus Torvalds 已提交
47
#endif
P
Paul Mundt 已提交
48
	dma_cache_sync(dev, ptr, size, dir);
L
Linus Torvalds 已提交
49

50 51 52 53 54
	debug_dma_map_page(dev, virt_to_page(ptr),
			   (unsigned long)ptr & ~PAGE_MASK, size,
			   dir, addr, true);

	return addr;
L
Linus Torvalds 已提交
55 56
}

57 58 59 60 61
static inline void dma_unmap_single(struct device *dev, dma_addr_t addr,
				    size_t size, enum dma_data_direction dir)
{
	debug_dma_unmap_page(dev, addr, size, dir, true);
}
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69

static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
			     int nents, enum dma_data_direction dir)
{
	int i;

	for (i = 0; i < nents; i++) {
#if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
70
		dma_cache_sync(dev, sg_virt(&sg[i]), sg[i].length, dir);
L
Linus Torvalds 已提交
71
#endif
72
		sg[i].dma_address = sg_phys(&sg[i]);
73
		sg[i].dma_length = sg[i].length;
L
Linus Torvalds 已提交
74 75
	}

76 77
	debug_dma_map_sg(dev, sg, nents, i, dir);

L
Linus Torvalds 已提交
78 79 80
	return nents;
}

81 82 83 84 85
static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
				int nents, enum dma_data_direction dir)
{
	debug_dma_unmap_sg(dev, sg, nents, dir);
}
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
				      unsigned long offset, size_t size,
				      enum dma_data_direction dir)
{
	return dma_map_single(dev, page_address(page) + offset, size, dir);
}

static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
				  size_t size, enum dma_data_direction dir)
{
	dma_unmap_single(dev, dma_address, size, dir);
}

static inline void dma_sync_single(struct device *dev, dma_addr_t dma_handle,
				   size_t size, enum dma_data_direction dir)
{
#if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
	if (dev->bus == &pci_bus_type)
		return;
#endif
107
	dma_cache_sync(dev, phys_to_virt(dma_handle), size, dir);
L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115 116 117 118
}

static inline void dma_sync_single_range(struct device *dev,
					 dma_addr_t dma_handle,
					 unsigned long offset, size_t size,
					 enum dma_data_direction dir)
{
#if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
	if (dev->bus == &pci_bus_type)
		return;
#endif
119
	dma_cache_sync(dev, phys_to_virt(dma_handle) + offset, size, dir);
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128
}

static inline void dma_sync_sg(struct device *dev, struct scatterlist *sg,
			       int nelems, enum dma_data_direction dir)
{
	int i;

	for (i = 0; i < nelems; i++) {
#if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
129
		dma_cache_sync(dev, sg_virt(&sg[i]), sg[i].length, dir);
L
Linus Torvalds 已提交
130
#endif
131
		sg[i].dma_address = sg_phys(&sg[i]);
132
		sg[i].dma_length = sg[i].length;
L
Linus Torvalds 已提交
133 134 135
	}
}

P
Paul Mundt 已提交
136 137 138 139 140
static inline void dma_sync_single_for_cpu(struct device *dev,
					   dma_addr_t dma_handle, size_t size,
					   enum dma_data_direction dir)
{
	dma_sync_single(dev, dma_handle, size, dir);
141
	debug_dma_sync_single_for_cpu(dev, dma_handle, size, dir);
P
Paul Mundt 已提交
142 143 144 145 146 147 148 149
}

static inline void dma_sync_single_for_device(struct device *dev,
					      dma_addr_t dma_handle,
					      size_t size,
					      enum dma_data_direction dir)
{
	dma_sync_single(dev, dma_handle, size, dir);
150
	debug_dma_sync_single_for_device(dev, dma_handle, size, dir);
P
Paul Mundt 已提交
151
}
L
Linus Torvalds 已提交
152

153 154 155 156 157 158 159
static inline void dma_sync_single_range_for_cpu(struct device *dev,
						 dma_addr_t dma_handle,
						 unsigned long offset,
						 size_t size,
						 enum dma_data_direction direction)
{
	dma_sync_single_for_cpu(dev, dma_handle+offset, size, direction);
160 161
	debug_dma_sync_single_range_for_cpu(dev, dma_handle,
					    offset, size, direction);
162 163 164 165 166 167 168 169 170
}

static inline void dma_sync_single_range_for_device(struct device *dev,
						    dma_addr_t dma_handle,
						    unsigned long offset,
						    size_t size,
						    enum dma_data_direction direction)
{
	dma_sync_single_for_device(dev, dma_handle+offset, size, direction);
171 172
	debug_dma_sync_single_range_for_device(dev, dma_handle,
					       offset, size, direction);
173 174 175
}


P
Paul Mundt 已提交
176 177
static inline void dma_sync_sg_for_cpu(struct device *dev,
				       struct scatterlist *sg, int nelems,
P
Paul Mundt 已提交
178
				       enum dma_data_direction dir)
P
Paul Mundt 已提交
179 180
{
	dma_sync_sg(dev, sg, nelems, dir);
181
	debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
P
Paul Mundt 已提交
182
}
L
Linus Torvalds 已提交
183

P
Paul Mundt 已提交
184 185 186 187 188
static inline void dma_sync_sg_for_device(struct device *dev,
					  struct scatterlist *sg, int nelems,
					  enum dma_data_direction dir)
{
	dma_sync_sg(dev, sg, nelems, dir);
189
	debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
P
Paul Mundt 已提交
190
}
L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198 199 200

static inline int dma_get_cache_alignment(void)
{
	/*
	 * Each processor family will define its own L1_CACHE_SHIFT,
	 * L1_CACHE_BYTES wraps to this, so this is always safe.
	 */
	return L1_CACHE_BYTES;
}

201
static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
L
Linus Torvalds 已提交
202 203 204
{
	return dma_addr == 0;
}
205 206 207 208 209 210 211 212 213 214 215 216 217 218

#define ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY

extern int
dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
			    dma_addr_t device_addr, size_t size, int flags);

extern void
dma_release_declared_memory(struct device *dev);

extern void *
dma_mark_declared_memory_occupied(struct device *dev,
				  dma_addr_t device_addr, size_t size);

L
Linus Torvalds 已提交
219
#endif /* __ASM_SH_DMA_MAPPING_H */