dma-mapping.h 8.2 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
 */

V
Vegard Nossum 已提交
9
#include <linux/kmemcheck.h>
10
#include <linux/scatterlist.h>
11
#include <linux/dma-debug.h>
12
#include <linux/dma-attrs.h>
13 14
#include <asm/io.h>
#include <asm/swiotlb.h>
15
#include <asm-generic/dma-coherent.h>
16

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

22 23 24
extern struct dma_map_ops *dma_ops;

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

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

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

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

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

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

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

63
	BUG_ON(!valid_dma_direction(dir));
V
Vegard Nossum 已提交
64
	kmemcheck_mark_initialized(ptr, size);
65
	addr = ops->map_page(hwdev, virt_to_page(ptr),
66
			     (unsigned long)ptr & ~PAGE_MASK, size,
67
			     dir, NULL);
68 69 70 71
	debug_dma_map_page(hwdev, virt_to_page(ptr),
			   (unsigned long)ptr & ~PAGE_MASK, size,
			   dir, addr, true);
	return addr;
72 73
}

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

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

86 87
static inline int
dma_map_sg(struct device *hwdev, struct scatterlist *sg,
88
	   int nents, enum dma_data_direction dir)
89
{
90
	struct dma_map_ops *ops = get_dma_ops(hwdev);
91
	int ents;
92 93
	struct scatterlist *s;
	int i;
94

95
	BUG_ON(!valid_dma_direction(dir));
96 97
	for_each_sg(sg, s, nents, i)
		kmemcheck_mark_initialized(sg_virt(s), s->length);
98 99 100 101
	ents = ops->map_sg(hwdev, sg, nents, dir, NULL);
	debug_dma_map_sg(hwdev, sg, nents, ents, dir);

	return ents;
102
}
103 104 105

static inline void
dma_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nents,
106
	     enum dma_data_direction dir)
107
{
108
	struct dma_map_ops *ops = get_dma_ops(hwdev);
109

110
	BUG_ON(!valid_dma_direction(dir));
111
	debug_dma_unmap_sg(hwdev, sg, nents, dir);
112
	if (ops->unmap_sg)
113
		ops->unmap_sg(hwdev, sg, nents, dir, NULL);
114
}
115 116 117

static inline void
dma_sync_single_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
118
			size_t size, enum dma_data_direction dir)
119
{
120
	struct dma_map_ops *ops = get_dma_ops(hwdev);
121

122
	BUG_ON(!valid_dma_direction(dir));
123
	if (ops->sync_single_for_cpu)
124
		ops->sync_single_for_cpu(hwdev, dma_handle, size, dir);
125
	debug_dma_sync_single_for_cpu(hwdev, dma_handle, size, dir);
126 127 128
	flush_write_buffers();
}

129 130
static inline void
dma_sync_single_for_device(struct device *hwdev, dma_addr_t dma_handle,
131
			   size_t size, enum dma_data_direction dir)
132
{
133
	struct dma_map_ops *ops = get_dma_ops(hwdev);
134

135
	BUG_ON(!valid_dma_direction(dir));
136
	if (ops->sync_single_for_device)
137
		ops->sync_single_for_device(hwdev, dma_handle, size, dir);
138
	debug_dma_sync_single_for_device(hwdev, dma_handle, size, dir);
139 140 141
	flush_write_buffers();
}

142 143
static inline void
dma_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
144 145
			      unsigned long offset, size_t size,
			      enum dma_data_direction dir)
146
{
147
	struct dma_map_ops *ops = get_dma_ops(hwdev);
148

149
	BUG_ON(!valid_dma_direction(dir));
150 151
	if (ops->sync_single_range_for_cpu)
		ops->sync_single_range_for_cpu(hwdev, dma_handle, offset,
152
					       size, dir);
153 154
	debug_dma_sync_single_range_for_cpu(hwdev, dma_handle,
					    offset, size, dir);
155 156
	flush_write_buffers();
}
157 158 159 160

static inline void
dma_sync_single_range_for_device(struct device *hwdev, dma_addr_t dma_handle,
				 unsigned long offset, size_t size,
161
				 enum dma_data_direction dir)
162
{
163
	struct dma_map_ops *ops = get_dma_ops(hwdev);
164

165
	BUG_ON(!valid_dma_direction(dir));
166 167
	if (ops->sync_single_range_for_device)
		ops->sync_single_range_for_device(hwdev, dma_handle,
168
						  offset, size, dir);
169 170
	debug_dma_sync_single_range_for_device(hwdev, dma_handle,
					       offset, size, dir);
171 172 173
	flush_write_buffers();
}

174 175
static inline void
dma_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
176
		    int nelems, enum dma_data_direction dir)
177
{
178
	struct dma_map_ops *ops = get_dma_ops(hwdev);
179

180
	BUG_ON(!valid_dma_direction(dir));
181
	if (ops->sync_sg_for_cpu)
182
		ops->sync_sg_for_cpu(hwdev, sg, nelems, dir);
183
	debug_dma_sync_sg_for_cpu(hwdev, sg, nelems, dir);
184 185
	flush_write_buffers();
}
186 187 188

static inline void
dma_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
189
		       int nelems, enum dma_data_direction dir)
190
{
191
	struct dma_map_ops *ops = get_dma_ops(hwdev);
192

193
	BUG_ON(!valid_dma_direction(dir));
194
	if (ops->sync_sg_for_device)
195
		ops->sync_sg_for_device(hwdev, sg, nelems, dir);
196
	debug_dma_sync_sg_for_device(hwdev, sg, nelems, dir);
197 198 199

	flush_write_buffers();
}
200 201 202

static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
				      size_t offset, size_t size,
203
				      enum dma_data_direction dir)
204
{
205
	struct dma_map_ops *ops = get_dma_ops(dev);
206
	dma_addr_t addr;
207

208
	BUG_ON(!valid_dma_direction(dir));
209
	kmemcheck_mark_initialized(page_address(page) + offset, size);
210 211 212 213
	addr = ops->map_page(dev, page, offset, size, dir, NULL);
	debug_dma_map_page(dev, page, offset, size, dir, addr, false);

	return addr;
214 215 216
}

static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
217
				  size_t size, enum dma_data_direction dir)
218
{
219 220 221 222 223 224
	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);
225 226
}

227 228 229 230 231 232
static inline void
dma_cache_sync(struct device *dev, void *vaddr, size_t size,
	enum dma_data_direction dir)
{
	flush_write_buffers();
}
233

234 235 236 237 238 239 240
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;
}

241 242 243 244
static inline unsigned long dma_alloc_coherent_mask(struct device *dev,
						    gfp_t gfp)
{
	unsigned long dma_mask = 0;
245

246 247
	dma_mask = dev->coherent_dma_mask;
	if (!dma_mask)
248
		dma_mask = (gfp & GFP_DMA) ? DMA_BIT_MASK(24) : DMA_BIT_MASK(32);
249 250 251 252 253 254 255 256

	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);

257
	if (dma_mask <= DMA_BIT_MASK(24))
258 259
		gfp |= GFP_DMA;
#ifdef CONFIG_X86_64
260
	if (dma_mask <= DMA_BIT_MASK(32) && !(gfp & GFP_DMA))
261 262 263 264 265
		gfp |= GFP_DMA32;
#endif
       return gfp;
}

266 267 268 269
static inline void *
dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
		gfp_t gfp)
{
270
	struct dma_map_ops *ops = get_dma_ops(dev);
271 272
	void *memory;

273 274
	gfp &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);

275 276 277 278 279 280 281 282
	if (dma_alloc_from_coherent(dev, size, dma_handle, &memory))
		return memory;

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

283
	if (!is_device_dma_capable(dev))
284 285
		return NULL;

286 287 288
	if (!ops->alloc_coherent)
		return NULL;

289 290 291 292 293
	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;
294 295 296 297 298
}

static inline void dma_free_coherent(struct device *dev, size_t size,
				     void *vaddr, dma_addr_t bus)
{
299
	struct dma_map_ops *ops = get_dma_ops(dev);
300 301 302 303 304 305

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

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

306
	debug_dma_free_coherent(dev, size, vaddr, bus);
307 308 309
	if (ops->free_coherent)
		ops->free_coherent(dev, size, vaddr, bus);
}
310

311
#endif