blob: 7e064c68c5ec8a0ab538a15947d5c44b2db0a322 [file] [log] [blame]
Sam Ravnborga439fe52008-07-27 23:00:59 +02001#ifndef ___ASM_SPARC_DMA_MAPPING_H
2#define ___ASM_SPARC_DMA_MAPPING_H
FUJITA Tomonorid6986412009-05-14 16:23:11 +00003
4#include <linux/scatterlist.h>
5#include <linux/mm.h>
FUJITA Tomonori02f7a182009-08-10 11:53:13 +09006#include <linux/dma-debug.h>
FUJITA Tomonorib9f69f42009-05-14 16:23:08 +00007
8#define DMA_ERROR_CODE (~(dma_addr_t)0x0)
9
Sam Ravnborgf05a6862014-05-16 23:25:50 +020010int dma_supported(struct device *dev, u64 mask);
FUJITA Tomonorib9f69f42009-05-14 16:23:08 +000011
FUJITA Tomonorib9f69f42009-05-14 16:23:08 +000012#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
13#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
FUJITA Tomonorib9f69f42009-05-14 16:23:08 +000014
David S. Miller0d1d9092014-11-26 13:36:41 -080015static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
16 enum dma_data_direction dir)
17{
18 /* Since dma_{alloc,free}_noncoherent() allocated coherent memory, this
19 * routine can be a nop.
20 */
21}
22
Sam Ravnborg87e677c2012-05-26 15:56:03 +020023extern struct dma_map_ops *dma_ops;
24extern struct dma_map_ops *leon_dma_ops;
25extern struct dma_map_ops pci32_dma_ops;
26
FUJITA Tomonoriee664a92009-08-10 11:53:16 +090027extern struct bus_type pci_bus_type;
FUJITA Tomonori02f7a182009-08-10 11:53:13 +090028
29static inline struct dma_map_ops *get_dma_ops(struct device *dev)
30{
Andreas Larssonb84ca922014-09-08 09:48:52 +020031#ifdef CONFIG_SPARC_LEON
Sam Ravnborg87e677c2012-05-26 15:56:03 +020032 if (sparc_cpu_model == sparc_leon)
33 return leon_dma_ops;
Andreas Larssonb84ca922014-09-08 09:48:52 +020034#endif
35#if defined(CONFIG_SPARC32) && defined(CONFIG_PCI)
36 if (dev->bus == &pci_bus_type)
FUJITA Tomonoriee664a92009-08-10 11:53:16 +090037 return &pci32_dma_ops;
38#endif
FUJITA Tomonori02f7a182009-08-10 11:53:13 +090039 return dma_ops;
40}
41
42#include <asm-generic/dma-mapping-common.h>
FUJITA Tomonorid6986412009-05-14 16:23:11 +000043
Andrzej Pietrasiewiczc4162582012-03-27 14:56:55 +020044#define dma_alloc_coherent(d,s,h,f) dma_alloc_attrs(d,s,h,f,NULL)
45
46static inline void *dma_alloc_attrs(struct device *dev, size_t size,
47 dma_addr_t *dma_handle, gfp_t flag,
48 struct dma_attrs *attrs)
FUJITA Tomonorid6986412009-05-14 16:23:11 +000049{
FUJITA Tomonori02f7a182009-08-10 11:53:13 +090050 struct dma_map_ops *ops = get_dma_ops(dev);
FUJITA Tomonori451d7402009-08-10 11:53:17 +090051 void *cpu_addr;
FUJITA Tomonori02f7a182009-08-10 11:53:13 +090052
Andrzej Pietrasiewiczc4162582012-03-27 14:56:55 +020053 cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
FUJITA Tomonori451d7402009-08-10 11:53:17 +090054 debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
55 return cpu_addr;
FUJITA Tomonorid6986412009-05-14 16:23:11 +000056}
57
Andrzej Pietrasiewiczc4162582012-03-27 14:56:55 +020058#define dma_free_coherent(d,s,c,h) dma_free_attrs(d,s,c,h,NULL)
59
60static inline void dma_free_attrs(struct device *dev, size_t size,
61 void *cpu_addr, dma_addr_t dma_handle,
62 struct dma_attrs *attrs)
FUJITA Tomonorid6986412009-05-14 16:23:11 +000063{
FUJITA Tomonori02f7a182009-08-10 11:53:13 +090064 struct dma_map_ops *ops = get_dma_ops(dev);
FUJITA Tomonorid6986412009-05-14 16:23:11 +000065
FUJITA Tomonori451d7402009-08-10 11:53:17 +090066 debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
Andrzej Pietrasiewiczc4162582012-03-27 14:56:55 +020067 ops->free(dev, size, cpu_addr, dma_handle, attrs);
FUJITA Tomonorid6986412009-05-14 16:23:11 +000068}
69
FUJITA Tomonorid6986412009-05-14 16:23:11 +000070static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
71{
Shuah Khan5d346d12012-10-25 18:31:42 -060072 debug_dma_mapping_error(dev, dma_addr);
FUJITA Tomonorid6986412009-05-14 16:23:11 +000073 return (dma_addr == DMA_ERROR_CODE);
74}
75
FUJITA Tomonori34900422010-03-10 15:23:35 -080076static inline int dma_set_mask(struct device *dev, u64 mask)
77{
78#ifdef CONFIG_PCI
79 if (dev->bus == &pci_bus_type) {
80 if (!dev->dma_mask || !dma_supported(dev, mask))
81 return -EINVAL;
82 *dev->dma_mask = mask;
83 return 0;
84 }
85#endif
86 return -EINVAL;
87}
88
Sam Ravnborga439fe52008-07-27 23:00:59 +020089#endif