aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux')
-rw-r--r--include/linux/dma-debug.h34
-rw-r--r--include/linux/dma-direct.h19
-rw-r--r--include/linux/dma-iommu.h1
-rw-r--r--include/linux/dma-mapping.h350
-rw-r--r--include/linux/dma-noncoherent.h7
-rw-r--r--include/linux/scatterlist.h6
-rw-r--r--include/linux/swiotlb.h77
7 files changed, 185 insertions, 309 deletions
diff --git a/include/linux/dma-debug.h b/include/linux/dma-debug.h
index 30213adbb6b9..2ad5c363d7d5 100644
--- a/include/linux/dma-debug.h
+++ b/include/linux/dma-debug.h
@@ -30,8 +30,6 @@ struct bus_type;
30 30
31extern void dma_debug_add_bus(struct bus_type *bus); 31extern void dma_debug_add_bus(struct bus_type *bus);
32 32
33extern int dma_debug_resize_entries(u32 num_entries);
34
35extern void debug_dma_map_single(struct device *dev, const void *addr, 33extern void debug_dma_map_single(struct device *dev, const void *addr,
36 unsigned long len); 34 unsigned long len);
37 35
@@ -72,17 +70,6 @@ extern void debug_dma_sync_single_for_device(struct device *dev,
72 dma_addr_t dma_handle, 70 dma_addr_t dma_handle,
73 size_t size, int direction); 71 size_t size, int direction);
74 72
75extern void debug_dma_sync_single_range_for_cpu(struct device *dev,
76 dma_addr_t dma_handle,
77 unsigned long offset,
78 size_t size,
79 int direction);
80
81extern void debug_dma_sync_single_range_for_device(struct device *dev,
82 dma_addr_t dma_handle,
83 unsigned long offset,
84 size_t size, int direction);
85
86extern void debug_dma_sync_sg_for_cpu(struct device *dev, 73extern void debug_dma_sync_sg_for_cpu(struct device *dev,
87 struct scatterlist *sg, 74 struct scatterlist *sg,
88 int nelems, int direction); 75 int nelems, int direction);
@@ -101,11 +88,6 @@ static inline void dma_debug_add_bus(struct bus_type *bus)
101{ 88{
102} 89}
103 90
104static inline int dma_debug_resize_entries(u32 num_entries)
105{
106 return 0;
107}
108
109static inline void debug_dma_map_single(struct device *dev, const void *addr, 91static inline void debug_dma_map_single(struct device *dev, const void *addr,
110 unsigned long len) 92 unsigned long len)
111{ 93{
@@ -174,22 +156,6 @@ static inline void debug_dma_sync_single_for_device(struct device *dev,
174{ 156{
175} 157}
176 158
177static inline void debug_dma_sync_single_range_for_cpu(struct device *dev,
178 dma_addr_t dma_handle,
179 unsigned long offset,
180 size_t size,
181 int direction)
182{
183}
184
185static inline void debug_dma_sync_single_range_for_device(struct device *dev,
186 dma_addr_t dma_handle,
187 unsigned long offset,
188 size_t size,
189 int direction)
190{
191}
192
193static inline void debug_dma_sync_sg_for_cpu(struct device *dev, 159static inline void debug_dma_sync_sg_for_cpu(struct device *dev,
194 struct scatterlist *sg, 160 struct scatterlist *sg,
195 int nelems, int direction) 161 int nelems, int direction)
diff --git a/include/linux/dma-direct.h b/include/linux/dma-direct.h
index 9e66bfe369aa..b7338702592a 100644
--- a/include/linux/dma-direct.h
+++ b/include/linux/dma-direct.h
@@ -5,8 +5,6 @@
5#include <linux/dma-mapping.h> 5#include <linux/dma-mapping.h>
6#include <linux/mem_encrypt.h> 6#include <linux/mem_encrypt.h>
7 7
8#define DIRECT_MAPPING_ERROR (~(dma_addr_t)0)
9
10#ifdef CONFIG_ARCH_HAS_PHYS_TO_DMA 8#ifdef CONFIG_ARCH_HAS_PHYS_TO_DMA
11#include <asm/dma-direct.h> 9#include <asm/dma-direct.h>
12#else 10#else
@@ -50,14 +48,6 @@ static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
50 return __sme_clr(__dma_to_phys(dev, daddr)); 48 return __sme_clr(__dma_to_phys(dev, daddr));
51} 49}
52 50
53#ifdef CONFIG_ARCH_HAS_DMA_MARK_CLEAN
54void dma_mark_clean(void *addr, size_t size);
55#else
56static inline void dma_mark_clean(void *addr, size_t size)
57{
58}
59#endif /* CONFIG_ARCH_HAS_DMA_MARK_CLEAN */
60
61u64 dma_direct_get_required_mask(struct device *dev); 51u64 dma_direct_get_required_mask(struct device *dev);
62void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, 52void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
63 gfp_t gfp, unsigned long attrs); 53 gfp_t gfp, unsigned long attrs);
@@ -67,11 +57,8 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
67 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs); 57 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs);
68void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr, 58void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr,
69 dma_addr_t dma_addr, unsigned long attrs); 59 dma_addr_t dma_addr, unsigned long attrs);
70dma_addr_t dma_direct_map_page(struct device *dev, struct page *page, 60struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
71 unsigned long offset, size_t size, enum dma_data_direction dir, 61 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs);
72 unsigned long attrs); 62void __dma_direct_free_pages(struct device *dev, size_t size, struct page *page);
73int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
74 enum dma_data_direction dir, unsigned long attrs);
75int dma_direct_supported(struct device *dev, u64 mask); 63int dma_direct_supported(struct device *dev, u64 mask);
76int dma_direct_mapping_error(struct device *dev, dma_addr_t dma_addr);
77#endif /* _LINUX_DMA_DIRECT_H */ 64#endif /* _LINUX_DMA_DIRECT_H */
diff --git a/include/linux/dma-iommu.h b/include/linux/dma-iommu.h
index e8ca5e654277..e760dc5d1fa8 100644
--- a/include/linux/dma-iommu.h
+++ b/include/linux/dma-iommu.h
@@ -69,7 +69,6 @@ dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
69 size_t size, enum dma_data_direction dir, unsigned long attrs); 69 size_t size, enum dma_data_direction dir, unsigned long attrs);
70void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle, 70void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
71 size_t size, enum dma_data_direction dir, unsigned long attrs); 71 size_t size, enum dma_data_direction dir, unsigned long attrs);
72int iommu_dma_mapping_error(struct device *dev, dma_addr_t dma_addr);
73 72
74/* The DMA API isn't _quite_ the whole story, though... */ 73/* The DMA API isn't _quite_ the whole story, though... */
75void iommu_dma_map_msi_msg(int irq, struct msi_msg *msg); 74void iommu_dma_map_msi_msg(int irq, struct msi_msg *msg);
diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h
index d327bdd53716..ba521d5506c9 100644
--- a/include/linux/dma-mapping.h
+++ b/include/linux/dma-mapping.h
@@ -128,13 +128,14 @@ struct dma_map_ops {
128 enum dma_data_direction dir); 128 enum dma_data_direction dir);
129 void (*cache_sync)(struct device *dev, void *vaddr, size_t size, 129 void (*cache_sync)(struct device *dev, void *vaddr, size_t size,
130 enum dma_data_direction direction); 130 enum dma_data_direction direction);
131 int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
132 int (*dma_supported)(struct device *dev, u64 mask); 131 int (*dma_supported)(struct device *dev, u64 mask);
133 u64 (*get_required_mask)(struct device *dev); 132 u64 (*get_required_mask)(struct device *dev);
134}; 133};
135 134
136extern const struct dma_map_ops dma_direct_ops; 135#define DMA_MAPPING_ERROR (~(dma_addr_t)0)
136
137extern const struct dma_map_ops dma_virt_ops; 137extern const struct dma_map_ops dma_virt_ops;
138extern const struct dma_map_ops dma_dummy_ops;
138 139
139#define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1)) 140#define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
140 141
@@ -220,6 +221,69 @@ static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
220} 221}
221#endif 222#endif
222 223
224static inline bool dma_is_direct(const struct dma_map_ops *ops)
225{
226 return likely(!ops);
227}
228
229/*
230 * All the dma_direct_* declarations are here just for the indirect call bypass,
231 * and must not be used directly drivers!
232 */
233dma_addr_t dma_direct_map_page(struct device *dev, struct page *page,
234 unsigned long offset, size_t size, enum dma_data_direction dir,
235 unsigned long attrs);
236int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
237 enum dma_data_direction dir, unsigned long attrs);
238
239#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
240 defined(CONFIG_SWIOTLB)
241void dma_direct_sync_single_for_device(struct device *dev,
242 dma_addr_t addr, size_t size, enum dma_data_direction dir);
243void dma_direct_sync_sg_for_device(struct device *dev,
244 struct scatterlist *sgl, int nents, enum dma_data_direction dir);
245#else
246static inline void dma_direct_sync_single_for_device(struct device *dev,
247 dma_addr_t addr, size_t size, enum dma_data_direction dir)
248{
249}
250static inline void dma_direct_sync_sg_for_device(struct device *dev,
251 struct scatterlist *sgl, int nents, enum dma_data_direction dir)
252{
253}
254#endif
255
256#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
257 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) || \
258 defined(CONFIG_SWIOTLB)
259void dma_direct_unmap_page(struct device *dev, dma_addr_t addr,
260 size_t size, enum dma_data_direction dir, unsigned long attrs);
261void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl,
262 int nents, enum dma_data_direction dir, unsigned long attrs);
263void dma_direct_sync_single_for_cpu(struct device *dev,
264 dma_addr_t addr, size_t size, enum dma_data_direction dir);
265void dma_direct_sync_sg_for_cpu(struct device *dev,
266 struct scatterlist *sgl, int nents, enum dma_data_direction dir);
267#else
268static inline void dma_direct_unmap_page(struct device *dev, dma_addr_t addr,
269 size_t size, enum dma_data_direction dir, unsigned long attrs)
270{
271}
272static inline void dma_direct_unmap_sg(struct device *dev,
273 struct scatterlist *sgl, int nents, enum dma_data_direction dir,
274 unsigned long attrs)
275{
276}
277static inline void dma_direct_sync_single_for_cpu(struct device *dev,
278 dma_addr_t addr, size_t size, enum dma_data_direction dir)
279{
280}
281static inline void dma_direct_sync_sg_for_cpu(struct device *dev,
282 struct scatterlist *sgl, int nents, enum dma_data_direction dir)
283{
284}
285#endif
286
223static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr, 287static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
224 size_t size, 288 size_t size,
225 enum dma_data_direction dir, 289 enum dma_data_direction dir,
@@ -230,9 +294,12 @@ static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
230 294
231 BUG_ON(!valid_dma_direction(dir)); 295 BUG_ON(!valid_dma_direction(dir));
232 debug_dma_map_single(dev, ptr, size); 296 debug_dma_map_single(dev, ptr, size);
233 addr = ops->map_page(dev, virt_to_page(ptr), 297 if (dma_is_direct(ops))
234 offset_in_page(ptr), size, 298 addr = dma_direct_map_page(dev, virt_to_page(ptr),
235 dir, attrs); 299 offset_in_page(ptr), size, dir, attrs);
300 else
301 addr = ops->map_page(dev, virt_to_page(ptr),
302 offset_in_page(ptr), size, dir, attrs);
236 debug_dma_map_page(dev, virt_to_page(ptr), 303 debug_dma_map_page(dev, virt_to_page(ptr),
237 offset_in_page(ptr), size, 304 offset_in_page(ptr), size,
238 dir, addr, true); 305 dir, addr, true);
@@ -247,11 +314,19 @@ static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
247 const struct dma_map_ops *ops = get_dma_ops(dev); 314 const struct dma_map_ops *ops = get_dma_ops(dev);
248 315
249 BUG_ON(!valid_dma_direction(dir)); 316 BUG_ON(!valid_dma_direction(dir));
250 if (ops->unmap_page) 317 if (dma_is_direct(ops))
318 dma_direct_unmap_page(dev, addr, size, dir, attrs);
319 else if (ops->unmap_page)
251 ops->unmap_page(dev, addr, size, dir, attrs); 320 ops->unmap_page(dev, addr, size, dir, attrs);
252 debug_dma_unmap_page(dev, addr, size, dir, true); 321 debug_dma_unmap_page(dev, addr, size, dir, true);
253} 322}
254 323
324static inline void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr,
325 size_t size, enum dma_data_direction dir, unsigned long attrs)
326{
327 return dma_unmap_single_attrs(dev, addr, size, dir, attrs);
328}
329
255/* 330/*
256 * dma_maps_sg_attrs returns 0 on error and > 0 on success. 331 * dma_maps_sg_attrs returns 0 on error and > 0 on success.
257 * It should never return a value < 0. 332 * It should never return a value < 0.
@@ -264,7 +339,10 @@ static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
264 int ents; 339 int ents;
265 340
266 BUG_ON(!valid_dma_direction(dir)); 341 BUG_ON(!valid_dma_direction(dir));
267 ents = ops->map_sg(dev, sg, nents, dir, attrs); 342 if (dma_is_direct(ops))
343 ents = dma_direct_map_sg(dev, sg, nents, dir, attrs);
344 else
345 ents = ops->map_sg(dev, sg, nents, dir, attrs);
268 BUG_ON(ents < 0); 346 BUG_ON(ents < 0);
269 debug_dma_map_sg(dev, sg, nents, ents, dir); 347 debug_dma_map_sg(dev, sg, nents, ents, dir);
270 348
@@ -279,7 +357,9 @@ static inline void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg
279 357
280 BUG_ON(!valid_dma_direction(dir)); 358 BUG_ON(!valid_dma_direction(dir));
281 debug_dma_unmap_sg(dev, sg, nents, dir); 359 debug_dma_unmap_sg(dev, sg, nents, dir);
282 if (ops->unmap_sg) 360 if (dma_is_direct(ops))
361 dma_direct_unmap_sg(dev, sg, nents, dir, attrs);
362 else if (ops->unmap_sg)
283 ops->unmap_sg(dev, sg, nents, dir, attrs); 363 ops->unmap_sg(dev, sg, nents, dir, attrs);
284} 364}
285 365
@@ -293,25 +373,15 @@ static inline dma_addr_t dma_map_page_attrs(struct device *dev,
293 dma_addr_t addr; 373 dma_addr_t addr;
294 374
295 BUG_ON(!valid_dma_direction(dir)); 375 BUG_ON(!valid_dma_direction(dir));
296 addr = ops->map_page(dev, page, offset, size, dir, attrs); 376 if (dma_is_direct(ops))
377 addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);
378 else
379 addr = ops->map_page(dev, page, offset, size, dir, attrs);
297 debug_dma_map_page(dev, page, offset, size, dir, addr, false); 380 debug_dma_map_page(dev, page, offset, size, dir, addr, false);
298 381
299 return addr; 382 return addr;
300} 383}
301 384
302static inline void dma_unmap_page_attrs(struct device *dev,
303 dma_addr_t addr, size_t size,
304 enum dma_data_direction dir,
305 unsigned long attrs)
306{
307 const struct dma_map_ops *ops = get_dma_ops(dev);
308
309 BUG_ON(!valid_dma_direction(dir));
310 if (ops->unmap_page)
311 ops->unmap_page(dev, addr, size, dir, attrs);
312 debug_dma_unmap_page(dev, addr, size, dir, false);
313}
314
315static inline dma_addr_t dma_map_resource(struct device *dev, 385static inline dma_addr_t dma_map_resource(struct device *dev,
316 phys_addr_t phys_addr, 386 phys_addr_t phys_addr,
317 size_t size, 387 size_t size,
@@ -327,7 +397,7 @@ static inline dma_addr_t dma_map_resource(struct device *dev,
327 BUG_ON(pfn_valid(PHYS_PFN(phys_addr))); 397 BUG_ON(pfn_valid(PHYS_PFN(phys_addr)));
328 398
329 addr = phys_addr; 399 addr = phys_addr;
330 if (ops->map_resource) 400 if (ops && ops->map_resource)
331 addr = ops->map_resource(dev, phys_addr, size, dir, attrs); 401 addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
332 402
333 debug_dma_map_resource(dev, phys_addr, size, dir, addr); 403 debug_dma_map_resource(dev, phys_addr, size, dir, addr);
@@ -342,7 +412,7 @@ static inline void dma_unmap_resource(struct device *dev, dma_addr_t addr,
342 const struct dma_map_ops *ops = get_dma_ops(dev); 412 const struct dma_map_ops *ops = get_dma_ops(dev);
343 413
344 BUG_ON(!valid_dma_direction(dir)); 414 BUG_ON(!valid_dma_direction(dir));
345 if (ops->unmap_resource) 415 if (ops && ops->unmap_resource)
346 ops->unmap_resource(dev, addr, size, dir, attrs); 416 ops->unmap_resource(dev, addr, size, dir, attrs);
347 debug_dma_unmap_resource(dev, addr, size, dir); 417 debug_dma_unmap_resource(dev, addr, size, dir);
348} 418}
@@ -354,11 +424,20 @@ static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
354 const struct dma_map_ops *ops = get_dma_ops(dev); 424 const struct dma_map_ops *ops = get_dma_ops(dev);
355 425
356 BUG_ON(!valid_dma_direction(dir)); 426 BUG_ON(!valid_dma_direction(dir));
357 if (ops->sync_single_for_cpu) 427 if (dma_is_direct(ops))
428 dma_direct_sync_single_for_cpu(dev, addr, size, dir);
429 else if (ops->sync_single_for_cpu)
358 ops->sync_single_for_cpu(dev, addr, size, dir); 430 ops->sync_single_for_cpu(dev, addr, size, dir);
359 debug_dma_sync_single_for_cpu(dev, addr, size, dir); 431 debug_dma_sync_single_for_cpu(dev, addr, size, dir);
360} 432}
361 433
434static inline void dma_sync_single_range_for_cpu(struct device *dev,
435 dma_addr_t addr, unsigned long offset, size_t size,
436 enum dma_data_direction dir)
437{
438 return dma_sync_single_for_cpu(dev, addr + offset, size, dir);
439}
440
362static inline void dma_sync_single_for_device(struct device *dev, 441static inline void dma_sync_single_for_device(struct device *dev,
363 dma_addr_t addr, size_t size, 442 dma_addr_t addr, size_t size,
364 enum dma_data_direction dir) 443 enum dma_data_direction dir)
@@ -366,37 +445,18 @@ static inline void dma_sync_single_for_device(struct device *dev,
366 const struct dma_map_ops *ops = get_dma_ops(dev); 445 const struct dma_map_ops *ops = get_dma_ops(dev);
367 446
368 BUG_ON(!valid_dma_direction(dir)); 447 BUG_ON(!valid_dma_direction(dir));
369 if (ops->sync_single_for_device) 448 if (dma_is_direct(ops))
449 dma_direct_sync_single_for_device(dev, addr, size, dir);
450 else if (ops->sync_single_for_device)
370 ops->sync_single_for_device(dev, addr, size, dir); 451 ops->sync_single_for_device(dev, addr, size, dir);
371 debug_dma_sync_single_for_device(dev, addr, size, dir); 452 debug_dma_sync_single_for_device(dev, addr, size, dir);
372} 453}
373 454
374static inline void dma_sync_single_range_for_cpu(struct device *dev,
375 dma_addr_t addr,
376 unsigned long offset,
377 size_t size,
378 enum dma_data_direction dir)
379{
380 const struct dma_map_ops *ops = get_dma_ops(dev);
381
382 BUG_ON(!valid_dma_direction(dir));
383 if (ops->sync_single_for_cpu)
384 ops->sync_single_for_cpu(dev, addr + offset, size, dir);
385 debug_dma_sync_single_range_for_cpu(dev, addr, offset, size, dir);
386}
387
388static inline void dma_sync_single_range_for_device(struct device *dev, 455static inline void dma_sync_single_range_for_device(struct device *dev,
389 dma_addr_t addr, 456 dma_addr_t addr, unsigned long offset, size_t size,
390 unsigned long offset, 457 enum dma_data_direction dir)
391 size_t size,
392 enum dma_data_direction dir)
393{ 458{
394 const struct dma_map_ops *ops = get_dma_ops(dev); 459 return dma_sync_single_for_device(dev, addr + offset, size, dir);
395
396 BUG_ON(!valid_dma_direction(dir));
397 if (ops->sync_single_for_device)
398 ops->sync_single_for_device(dev, addr + offset, size, dir);
399 debug_dma_sync_single_range_for_device(dev, addr, offset, size, dir);
400} 460}
401 461
402static inline void 462static inline void
@@ -406,7 +466,9 @@ dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
406 const struct dma_map_ops *ops = get_dma_ops(dev); 466 const struct dma_map_ops *ops = get_dma_ops(dev);
407 467
408 BUG_ON(!valid_dma_direction(dir)); 468 BUG_ON(!valid_dma_direction(dir));
409 if (ops->sync_sg_for_cpu) 469 if (dma_is_direct(ops))
470 dma_direct_sync_sg_for_cpu(dev, sg, nelems, dir);
471 else if (ops->sync_sg_for_cpu)
410 ops->sync_sg_for_cpu(dev, sg, nelems, dir); 472 ops->sync_sg_for_cpu(dev, sg, nelems, dir);
411 debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir); 473 debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
412} 474}
@@ -418,7 +480,9 @@ dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
418 const struct dma_map_ops *ops = get_dma_ops(dev); 480 const struct dma_map_ops *ops = get_dma_ops(dev);
419 481
420 BUG_ON(!valid_dma_direction(dir)); 482 BUG_ON(!valid_dma_direction(dir));
421 if (ops->sync_sg_for_device) 483 if (dma_is_direct(ops))
484 dma_direct_sync_sg_for_device(dev, sg, nelems, dir);
485 else if (ops->sync_sg_for_device)
422 ops->sync_sg_for_device(dev, sg, nelems, dir); 486 ops->sync_sg_for_device(dev, sg, nelems, dir);
423 debug_dma_sync_sg_for_device(dev, sg, nelems, dir); 487 debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
424 488
@@ -431,16 +495,8 @@ dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
431#define dma_map_page(d, p, o, s, r) dma_map_page_attrs(d, p, o, s, r, 0) 495#define dma_map_page(d, p, o, s, r) dma_map_page_attrs(d, p, o, s, r, 0)
432#define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0) 496#define dma_unmap_page(d, a, s, r) dma_unmap_page_attrs(d, a, s, r, 0)
433 497
434static inline void 498void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
435dma_cache_sync(struct device *dev, void *vaddr, size_t size, 499 enum dma_data_direction dir);
436 enum dma_data_direction dir)
437{
438 const struct dma_map_ops *ops = get_dma_ops(dev);
439
440 BUG_ON(!valid_dma_direction(dir));
441 if (ops->cache_sync)
442 ops->cache_sync(dev, vaddr, size, dir);
443}
444 500
445extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma, 501extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
446 void *cpu_addr, dma_addr_t dma_addr, size_t size, 502 void *cpu_addr, dma_addr_t dma_addr, size_t size,
@@ -455,107 +511,29 @@ void *dma_common_pages_remap(struct page **pages, size_t size,
455 const void *caller); 511 const void *caller);
456void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags); 512void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags);
457 513
458/** 514int __init dma_atomic_pool_init(gfp_t gfp, pgprot_t prot);
459 * dma_mmap_attrs - map a coherent DMA allocation into user space 515bool dma_in_atomic_pool(void *start, size_t size);
460 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices 516void *dma_alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags);
461 * @vma: vm_area_struct describing requested user mapping 517bool dma_free_from_pool(void *start, size_t size);
462 * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
463 * @handle: device-view address returned from dma_alloc_attrs
464 * @size: size of memory originally requested in dma_alloc_attrs
465 * @attrs: attributes of mapping properties requested in dma_alloc_attrs
466 *
467 * Map a coherent DMA buffer previously allocated by dma_alloc_attrs
468 * into user space. The coherent DMA buffer must not be freed by the
469 * driver until the user space mapping has been released.
470 */
471static inline int
472dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr,
473 dma_addr_t dma_addr, size_t size, unsigned long attrs)
474{
475 const struct dma_map_ops *ops = get_dma_ops(dev);
476 BUG_ON(!ops);
477 if (ops->mmap)
478 return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
479 return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
480}
481 518
519int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
520 void *cpu_addr, dma_addr_t dma_addr, size_t size,
521 unsigned long attrs);
482#define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0) 522#define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, 0)
483 523
484int 524int
485dma_common_get_sgtable(struct device *dev, struct sg_table *sgt, void *cpu_addr, 525dma_common_get_sgtable(struct device *dev, struct sg_table *sgt, void *cpu_addr,
486 dma_addr_t dma_addr, size_t size, unsigned long attrs); 526 dma_addr_t dma_addr, size_t size, unsigned long attrs);
487 527
488static inline int 528int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
489dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr, 529 void *cpu_addr, dma_addr_t dma_addr, size_t size,
490 dma_addr_t dma_addr, size_t size, 530 unsigned long attrs);
491 unsigned long attrs)
492{
493 const struct dma_map_ops *ops = get_dma_ops(dev);
494 BUG_ON(!ops);
495 if (ops->get_sgtable)
496 return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
497 attrs);
498 return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
499 attrs);
500}
501
502#define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0) 531#define dma_get_sgtable(d, t, v, h, s) dma_get_sgtable_attrs(d, t, v, h, s, 0)
503 532
504#ifndef arch_dma_alloc_attrs 533void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
505#define arch_dma_alloc_attrs(dev) (true) 534 gfp_t flag, unsigned long attrs);
506#endif 535void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
507 536 dma_addr_t dma_handle, unsigned long attrs);
508static inline void *dma_alloc_attrs(struct device *dev, size_t size,
509 dma_addr_t *dma_handle, gfp_t flag,
510 unsigned long attrs)
511{
512 const struct dma_map_ops *ops = get_dma_ops(dev);
513 void *cpu_addr;
514
515 BUG_ON(!ops);
516 WARN_ON_ONCE(dev && !dev->coherent_dma_mask);
517
518 if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr))
519 return cpu_addr;
520
521 /* let the implementation decide on the zone to allocate from: */
522 flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
523
524 if (!arch_dma_alloc_attrs(&dev))
525 return NULL;
526 if (!ops->alloc)
527 return NULL;
528
529 cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
530 debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
531 return cpu_addr;
532}
533
534static inline void dma_free_attrs(struct device *dev, size_t size,
535 void *cpu_addr, dma_addr_t dma_handle,
536 unsigned long attrs)
537{
538 const struct dma_map_ops *ops = get_dma_ops(dev);
539
540 BUG_ON(!ops);
541
542 if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr))
543 return;
544 /*
545 * On non-coherent platforms which implement DMA-coherent buffers via
546 * non-cacheable remaps, ops->free() may call vunmap(). Thus getting
547 * this far in IRQ context is a) at risk of a BUG_ON() or trying to
548 * sleep on some machines, and b) an indication that the driver is
549 * probably misusing the coherent API anyway.
550 */
551 WARN_ON(irqs_disabled());
552
553 if (!ops->free || !cpu_addr)
554 return;
555
556 debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
557 ops->free(dev, size, cpu_addr, dma_handle, attrs);
558}
559 537
560static inline void *dma_alloc_coherent(struct device *dev, size_t size, 538static inline void *dma_alloc_coherent(struct device *dev, size_t size,
561 dma_addr_t *dma_handle, gfp_t gfp) 539 dma_addr_t *dma_handle, gfp_t gfp)
@@ -573,43 +551,16 @@ static inline void dma_free_coherent(struct device *dev, size_t size,
573 551
574static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr) 552static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
575{ 553{
576 const struct dma_map_ops *ops = get_dma_ops(dev);
577
578 debug_dma_mapping_error(dev, dma_addr); 554 debug_dma_mapping_error(dev, dma_addr);
579 if (ops->mapping_error)
580 return ops->mapping_error(dev, dma_addr);
581 return 0;
582}
583
584static inline void dma_check_mask(struct device *dev, u64 mask)
585{
586 if (sme_active() && (mask < (((u64)sme_get_me_mask() << 1) - 1)))
587 dev_warn(dev, "SME is active, device will require DMA bounce buffers\n");
588}
589
590static inline int dma_supported(struct device *dev, u64 mask)
591{
592 const struct dma_map_ops *ops = get_dma_ops(dev);
593
594 if (!ops)
595 return 0;
596 if (!ops->dma_supported)
597 return 1;
598 return ops->dma_supported(dev, mask);
599}
600
601#ifndef HAVE_ARCH_DMA_SET_MASK
602static inline int dma_set_mask(struct device *dev, u64 mask)
603{
604 if (!dev->dma_mask || !dma_supported(dev, mask))
605 return -EIO;
606 555
607 dma_check_mask(dev, mask); 556 if (dma_addr == DMA_MAPPING_ERROR)
608 557 return -ENOMEM;
609 *dev->dma_mask = mask;
610 return 0; 558 return 0;
611} 559}
612#endif 560
561int dma_supported(struct device *dev, u64 mask);
562int dma_set_mask(struct device *dev, u64 mask);
563int dma_set_coherent_mask(struct device *dev, u64 mask);
613 564
614static inline u64 dma_get_mask(struct device *dev) 565static inline u64 dma_get_mask(struct device *dev)
615{ 566{
@@ -618,21 +569,6 @@ static inline u64 dma_get_mask(struct device *dev)
618 return DMA_BIT_MASK(32); 569 return DMA_BIT_MASK(32);
619} 570}
620 571
621#ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
622int dma_set_coherent_mask(struct device *dev, u64 mask);
623#else
624static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
625{
626 if (!dma_supported(dev, mask))
627 return -EIO;
628
629 dma_check_mask(dev, mask);
630
631 dev->coherent_dma_mask = mask;
632 return 0;
633}
634#endif
635
636/* 572/*
637 * Set both the DMA mask and the coherent DMA mask to the same thing. 573 * Set both the DMA mask and the coherent DMA mask to the same thing.
638 * Note that we don't check the return value from dma_set_coherent_mask() 574 * Note that we don't check the return value from dma_set_coherent_mask()
@@ -676,8 +612,7 @@ static inline unsigned int dma_get_max_seg_size(struct device *dev)
676 return SZ_64K; 612 return SZ_64K;
677} 613}
678 614
679static inline unsigned int dma_set_max_seg_size(struct device *dev, 615static inline int dma_set_max_seg_size(struct device *dev, unsigned int size)
680 unsigned int size)
681{ 616{
682 if (dev->dma_parms) { 617 if (dev->dma_parms) {
683 dev->dma_parms->max_segment_size = size; 618 dev->dma_parms->max_segment_size = size;
@@ -709,12 +644,13 @@ static inline unsigned long dma_max_pfn(struct device *dev)
709} 644}
710#endif 645#endif
711 646
647/*
648 * Please always use dma_alloc_coherent instead as it already zeroes the memory!
649 */
712static inline void *dma_zalloc_coherent(struct device *dev, size_t size, 650static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
713 dma_addr_t *dma_handle, gfp_t flag) 651 dma_addr_t *dma_handle, gfp_t flag)
714{ 652{
715 void *ret = dma_alloc_coherent(dev, size, dma_handle, 653 return dma_alloc_coherent(dev, size, dma_handle, flag);
716 flag | __GFP_ZERO);
717 return ret;
718} 654}
719 655
720static inline int dma_get_cache_alignment(void) 656static inline int dma_get_cache_alignment(void)
diff --git a/include/linux/dma-noncoherent.h b/include/linux/dma-noncoherent.h
index 9051b055beec..69b36ed31a99 100644
--- a/include/linux/dma-noncoherent.h
+++ b/include/linux/dma-noncoherent.h
@@ -38,7 +38,10 @@ pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot,
38void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size, 38void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
39 enum dma_data_direction direction); 39 enum dma_data_direction direction);
40#else 40#else
41#define arch_dma_cache_sync NULL 41static inline void arch_dma_cache_sync(struct device *dev, void *vaddr,
42 size_t size, enum dma_data_direction direction)
43{
44}
42#endif /* CONFIG_DMA_NONCOHERENT_CACHE_SYNC */ 45#endif /* CONFIG_DMA_NONCOHERENT_CACHE_SYNC */
43 46
44#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE 47#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE
@@ -69,4 +72,6 @@ static inline void arch_sync_dma_for_cpu_all(struct device *dev)
69} 72}
70#endif /* CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL */ 73#endif /* CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL */
71 74
75void arch_dma_prep_coherent(struct page *page, size_t size);
76
72#endif /* _LINUX_DMA_NONCOHERENT_H */ 77#endif /* _LINUX_DMA_NONCOHERENT_H */
diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h
index 093aa57120b0..b96f0d0b5b8f 100644
--- a/include/linux/scatterlist.h
+++ b/include/linux/scatterlist.h
@@ -324,10 +324,10 @@ size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
324 * Like SG_CHUNK_SIZE, but for archs that have sg chaining. This limit 324 * Like SG_CHUNK_SIZE, but for archs that have sg chaining. This limit
325 * is totally arbitrary, a setting of 2048 will get you at least 8mb ios. 325 * is totally arbitrary, a setting of 2048 will get you at least 8mb ios.
326 */ 326 */
327#ifdef CONFIG_ARCH_HAS_SG_CHAIN 327#ifdef CONFIG_ARCH_NO_SG_CHAIN
328#define SG_MAX_SEGMENTS 2048
329#else
330#define SG_MAX_SEGMENTS SG_CHUNK_SIZE 328#define SG_MAX_SEGMENTS SG_CHUNK_SIZE
329#else
330#define SG_MAX_SEGMENTS 2048
331#endif 331#endif
332 332
333#ifdef CONFIG_SG_POOL 333#ifdef CONFIG_SG_POOL
diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h
index a387b59640a4..7c007ed7505f 100644
--- a/include/linux/swiotlb.h
+++ b/include/linux/swiotlb.h
@@ -16,8 +16,6 @@ enum swiotlb_force {
16 SWIOTLB_NO_FORCE, /* swiotlb=noforce */ 16 SWIOTLB_NO_FORCE, /* swiotlb=noforce */
17}; 17};
18 18
19extern enum swiotlb_force swiotlb_force;
20
21/* 19/*
22 * Maximum allowable number of contiguous slabs to map, 20 * Maximum allowable number of contiguous slabs to map,
23 * must be a power of 2. What is the appropriate value ? 21 * must be a power of 2. What is the appropriate value ?
@@ -46,9 +44,6 @@ enum dma_sync_target {
46 SYNC_FOR_DEVICE = 1, 44 SYNC_FOR_DEVICE = 1,
47}; 45};
48 46
49/* define the last possible byte of physical address space as a mapping error */
50#define SWIOTLB_MAP_ERROR (~(phys_addr_t)0x0)
51
52extern phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, 47extern phys_addr_t swiotlb_tbl_map_single(struct device *hwdev,
53 dma_addr_t tbl_dma_addr, 48 dma_addr_t tbl_dma_addr,
54 phys_addr_t phys, size_t size, 49 phys_addr_t phys, size_t size,
@@ -65,56 +60,44 @@ extern void swiotlb_tbl_sync_single(struct device *hwdev,
65 size_t size, enum dma_data_direction dir, 60 size_t size, enum dma_data_direction dir,
66 enum dma_sync_target target); 61 enum dma_sync_target target);
67 62
68/* Accessory functions. */
69
70extern dma_addr_t swiotlb_map_page(struct device *dev, struct page *page,
71 unsigned long offset, size_t size,
72 enum dma_data_direction dir,
73 unsigned long attrs);
74extern void swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr,
75 size_t size, enum dma_data_direction dir,
76 unsigned long attrs);
77
78extern int
79swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
80 enum dma_data_direction dir,
81 unsigned long attrs);
82
83extern void
84swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
85 int nelems, enum dma_data_direction dir,
86 unsigned long attrs);
87
88extern void
89swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
90 size_t size, enum dma_data_direction dir);
91
92extern void
93swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
94 int nelems, enum dma_data_direction dir);
95
96extern void
97swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
98 size_t size, enum dma_data_direction dir);
99
100extern void
101swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
102 int nelems, enum dma_data_direction dir);
103
104extern int 63extern int
105swiotlb_dma_supported(struct device *hwdev, u64 mask); 64swiotlb_dma_supported(struct device *hwdev, u64 mask);
106 65
107#ifdef CONFIG_SWIOTLB 66#ifdef CONFIG_SWIOTLB
108extern void __init swiotlb_exit(void); 67extern enum swiotlb_force swiotlb_force;
68extern phys_addr_t io_tlb_start, io_tlb_end;
69
70static inline bool is_swiotlb_buffer(phys_addr_t paddr)
71{
72 return paddr >= io_tlb_start && paddr < io_tlb_end;
73}
74
75bool swiotlb_map(struct device *dev, phys_addr_t *phys, dma_addr_t *dma_addr,
76 size_t size, enum dma_data_direction dir, unsigned long attrs);
77void __init swiotlb_exit(void);
109unsigned int swiotlb_max_segment(void); 78unsigned int swiotlb_max_segment(void);
110#else 79#else
111static inline void swiotlb_exit(void) { } 80#define swiotlb_force SWIOTLB_NO_FORCE
112static inline unsigned int swiotlb_max_segment(void) { return 0; } 81static inline bool is_swiotlb_buffer(phys_addr_t paddr)
113#endif 82{
83 return false;
84}
85static inline bool swiotlb_map(struct device *dev, phys_addr_t *phys,
86 dma_addr_t *dma_addr, size_t size, enum dma_data_direction dir,
87 unsigned long attrs)
88{
89 return false;
90}
91static inline void swiotlb_exit(void)
92{
93}
94static inline unsigned int swiotlb_max_segment(void)
95{
96 return 0;
97}
98#endif /* CONFIG_SWIOTLB */
114 99
115extern void swiotlb_print_info(void); 100extern void swiotlb_print_info(void);
116extern void swiotlb_set_max_segment(unsigned int); 101extern void swiotlb_set_max_segment(unsigned int);
117 102
118extern const struct dma_map_ops swiotlb_dma_ops;
119
120#endif /* __LINUX_SWIOTLB_H */ 103#endif /* __LINUX_SWIOTLB_H */