dma-mapping.h 8.0 KB
Newer Older
H
H. Peter Anvin 已提交
1 2
#ifndef _ASM_X86_DMA_MAPPING_H
#define _ASM_X86_DMA_MAPPING_H
3 4

/*
5 6
 * IOMMU interface. See Documentation/PCI/PCI-DMA-mapping.txt and
 * Documentation/DMA-API.txt for documentation.
7 8 9
 */

#include <linux/scatterlist.h>
10
#include <linux/dma-debug.h>
11
#include <linux/dma-attrs.h>
12 13
#include <asm/io.h>
#include <asm/swiotlb.h>
14
#include <asm-generic/dma-coherent.h>
15

16
extern dma_addr_t bad_dma_address;
17
extern int iommu_merge;
18
extern struct device x86_dma_fallback_dev;
19
extern int panic_on_overflow;
20

21 22 23
extern struct dma_map_ops *dma_ops;

static inline struct dma_map_ops *get_dma_ops(struct device *dev)
G
Glauber Costa 已提交
24
{
25 26 27 28 29 30 31
#ifdef CONFIG_X86_32
	return dma_ops;
#else
	if (unlikely(!dev) || !dev->archdata.dma_ops)
		return dma_ops;
	else
		return dev->archdata.dma_ops;
32
#endif
33 34 35 36 37
}

/* Make sure we keep the same behaviour */
static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
{
38
	struct dma_map_ops *ops = get_dma_ops(dev);
39 40
	if (ops->mapping_error)
		return ops->mapping_error(dev, dma_addr);
G
Glauber Costa 已提交
41

42
	return (dma_addr == bad_dma_address);
G
Glauber Costa 已提交
43 44
}

45 46
#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)
47
#define dma_is_consistent(d, h)	(1)
48

49 50 51
extern int dma_supported(struct device *hwdev, u64 mask);
extern int dma_set_mask(struct device *dev, u64 mask);

52 53 54
extern void *dma_generic_alloc_coherent(struct device *dev, size_t size,
					dma_addr_t *dma_addr, gfp_t flag);

55 56
static inline dma_addr_t
dma_map_single(struct device *hwdev, void *ptr, size_t size,
57
	       enum dma_data_direction dir)
58
{
59
	struct dma_map_ops *ops = get_dma_ops(hwdev);
60
	dma_addr_t addr;
61

62
	BUG_ON(!valid_dma_direction(dir));
63
	addr = ops->map_page(hwdev, virt_to_page(ptr),
64
			     (unsigned long)ptr & ~PAGE_MASK, size,
65
			     dir, NULL);
66 67 68 69
	debug_dma_map_page(hwdev, virt_to_page(ptr),
			   (unsigned long)ptr & ~PAGE_MASK, size,
			   dir, addr, true);
	return addr;
70 71
}

72 73
static inline void
dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
74
		 enum dma_data_direction dir)
75
{
76
	struct dma_map_ops *ops = get_dma_ops(dev);
77

78
	BUG_ON(!valid_dma_direction(dir));
79
	if (ops->unmap_page)
80
		ops->unmap_page(dev, addr, size, dir, NULL);
81
	debug_dma_unmap_page(dev, addr, size, dir, true);
82 83
}

84 85
static inline int
dma_map_sg(struct device *hwdev, struct scatterlist *sg,
86
	   int nents, enum dma_data_direction dir)
87
{
88
	struct dma_map_ops *ops = get_dma_ops(hwdev);
89
	int ents;
90

91
	BUG_ON(!valid_dma_direction(dir));
92 93 94 95
	ents = ops->map_sg(hwdev, sg, nents, dir, NULL);
	debug_dma_map_sg(hwdev, sg, nents, ents, dir);

	return ents;
96
}
97 98 99

static inline void
dma_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nents,
100
	     enum dma_data_direction dir)
101
{
102
	struct dma_map_ops *ops = get_dma_ops(hwdev);
103

104
	BUG_ON(!valid_dma_direction(dir));
105
	debug_dma_unmap_sg(hwdev, sg, nents, dir);
106
	if (ops->unmap_sg)
107
		ops->unmap_sg(hwdev, sg, nents, dir, NULL);
108
}
109 110 111

static inline void
dma_sync_single_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
112
			size_t size, enum dma_data_direction dir)
113
{
114
	struct dma_map_ops *ops = get_dma_ops(hwdev);
115

116
	BUG_ON(!valid_dma_direction(dir));
117
	if (ops->sync_single_for_cpu)
118
		ops->sync_single_for_cpu(hwdev, dma_handle, size, dir);
119
	debug_dma_sync_single_for_cpu(hwdev, dma_handle, size, dir);
120 121 122
	flush_write_buffers();
}

123 124
static inline void
dma_sync_single_for_device(struct device *hwdev, dma_addr_t dma_handle,
125
			   size_t size, enum dma_data_direction dir)
126
{
127
	struct dma_map_ops *ops = get_dma_ops(hwdev);
128

129
	BUG_ON(!valid_dma_direction(dir));
130
	if (ops->sync_single_for_device)
131
		ops->sync_single_for_device(hwdev, dma_handle, size, dir);
132
	debug_dma_sync_single_for_device(hwdev, dma_handle, size, dir);
133 134 135
	flush_write_buffers();
}

136 137
static inline void
dma_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
138 139
			      unsigned long offset, size_t size,
			      enum dma_data_direction dir)
140
{
141
	struct dma_map_ops *ops = get_dma_ops(hwdev);
142

143
	BUG_ON(!valid_dma_direction(dir));
144 145
	if (ops->sync_single_range_for_cpu)
		ops->sync_single_range_for_cpu(hwdev, dma_handle, offset,
146
					       size, dir);
147 148
	debug_dma_sync_single_range_for_cpu(hwdev, dma_handle,
					    offset, size, dir);
149 150
	flush_write_buffers();
}
151 152 153 154

static inline void
dma_sync_single_range_for_device(struct device *hwdev, dma_addr_t dma_handle,
				 unsigned long offset, size_t size,
155
				 enum dma_data_direction dir)
156
{
157
	struct dma_map_ops *ops = get_dma_ops(hwdev);
158

159
	BUG_ON(!valid_dma_direction(dir));
160 161
	if (ops->sync_single_range_for_device)
		ops->sync_single_range_for_device(hwdev, dma_handle,
162
						  offset, size, dir);
163 164
	debug_dma_sync_single_range_for_device(hwdev, dma_handle,
					       offset, size, dir);
165 166 167
	flush_write_buffers();
}

168 169
static inline void
dma_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
170
		    int nelems, enum dma_data_direction dir)
171
{
172
	struct dma_map_ops *ops = get_dma_ops(hwdev);
173

174
	BUG_ON(!valid_dma_direction(dir));
175
	if (ops->sync_sg_for_cpu)
176
		ops->sync_sg_for_cpu(hwdev, sg, nelems, dir);
177
	debug_dma_sync_sg_for_cpu(hwdev, sg, nelems, dir);
178 179
	flush_write_buffers();
}
180 181 182

static inline void
dma_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
183
		       int nelems, enum dma_data_direction dir)
184
{
185
	struct dma_map_ops *ops = get_dma_ops(hwdev);
186

187
	BUG_ON(!valid_dma_direction(dir));
188
	if (ops->sync_sg_for_device)
189
		ops->sync_sg_for_device(hwdev, sg, nelems, dir);
190
	debug_dma_sync_sg_for_device(hwdev, sg, nelems, dir);
191 192 193

	flush_write_buffers();
}
194 195 196

static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
				      size_t offset, size_t size,
197
				      enum dma_data_direction dir)
198
{
199
	struct dma_map_ops *ops = get_dma_ops(dev);
200
	dma_addr_t addr;
201

202
	BUG_ON(!valid_dma_direction(dir));
203 204 205 206
	addr = ops->map_page(dev, page, offset, size, dir, NULL);
	debug_dma_map_page(dev, page, offset, size, dir, addr, false);

	return addr;
207 208 209
}

static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
210
				  size_t size, enum dma_data_direction dir)
211
{
212 213 214 215 216 217
	struct dma_map_ops *ops = get_dma_ops(dev);

	BUG_ON(!valid_dma_direction(dir));
	if (ops->unmap_page)
		ops->unmap_page(dev, addr, size, dir, NULL);
	debug_dma_unmap_page(dev, addr, size, dir, false);
218 219
}

220 221 222 223 224 225
static inline void
dma_cache_sync(struct device *dev, void *vaddr, size_t size,
	enum dma_data_direction dir)
{
	flush_write_buffers();
}
226

227 228 229 230 231 232 233
static inline int dma_get_cache_alignment(void)
{
	/* no easy way to get cache size on all x86, so return the
	 * maximum possible, to be safe */
	return boot_cpu_data.x86_clflush_size;
}

234 235 236 237
static inline unsigned long dma_alloc_coherent_mask(struct device *dev,
						    gfp_t gfp)
{
	unsigned long dma_mask = 0;
238

239 240 241 242 243 244 245 246 247 248 249
	dma_mask = dev->coherent_dma_mask;
	if (!dma_mask)
		dma_mask = (gfp & GFP_DMA) ? DMA_24BIT_MASK : DMA_32BIT_MASK;

	return dma_mask;
}

static inline gfp_t dma_alloc_coherent_gfp_flags(struct device *dev, gfp_t gfp)
{
	unsigned long dma_mask = dma_alloc_coherent_mask(dev, gfp);

250 251 252
	if (dma_mask <= DMA_24BIT_MASK)
		gfp |= GFP_DMA;
#ifdef CONFIG_X86_64
253 254 255 256 257 258
	if (dma_mask <= DMA_32BIT_MASK && !(gfp & GFP_DMA))
		gfp |= GFP_DMA32;
#endif
       return gfp;
}

259 260 261 262
static inline void *
dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
		gfp_t gfp)
{
263
	struct dma_map_ops *ops = get_dma_ops(dev);
264 265
	void *memory;

266 267
	gfp &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);

268 269 270 271 272 273 274 275
	if (dma_alloc_from_coherent(dev, size, dma_handle, &memory))
		return memory;

	if (!dev) {
		dev = &x86_dma_fallback_dev;
		gfp |= GFP_DMA;
	}

276
	if (!is_device_dma_capable(dev))
277 278
		return NULL;

279 280 281
	if (!ops->alloc_coherent)
		return NULL;

282 283 284 285 286
	memory = ops->alloc_coherent(dev, size, dma_handle,
				     dma_alloc_coherent_gfp_flags(dev, gfp));
	debug_dma_alloc_coherent(dev, size, *dma_handle, memory);

	return memory;
287 288 289 290 291
}

static inline void dma_free_coherent(struct device *dev, size_t size,
				     void *vaddr, dma_addr_t bus)
{
292
	struct dma_map_ops *ops = get_dma_ops(dev);
293 294 295 296 297 298

	WARN_ON(irqs_disabled());       /* for portability */

	if (dma_release_from_coherent(dev, get_order(size), vaddr))
		return;

299
	debug_dma_free_coherent(dev, size, vaddr, bus);
300 301 302
	if (ops->free_coherent)
		ops->free_coherent(dev, size, vaddr, bus);
}
303

304
#endif