blob: 8d0867b98e05141ce77d5bb4f3837e3877e0c124 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ASM_SH_DMA_MAPPING_H
2#define __ASM_SH_DMA_MAPPING_H
3
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/mm.h>
5#include <asm/scatterlist.h>
Paul Mundt0d831772006-01-16 22:14:09 -08006#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <asm/io.h>
8
9extern struct bus_type pci_bus_type;
10
11/* arch/sh/mm/consistent.c */
Al Viro6dae2c22005-10-21 03:21:38 -040012extern void *consistent_alloc(gfp_t gfp, size_t size, dma_addr_t *handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013extern void consistent_free(void *vaddr, size_t size);
14extern void consistent_sync(void *vaddr, size_t size, int direction);
15
16#define dma_supported(dev, mask) (1)
17
18static inline int dma_set_mask(struct device *dev, u64 mask)
19{
20 if (!dev->dma_mask || !dma_supported(dev, mask))
21 return -EIO;
22
23 *dev->dma_mask = mask;
24
25 return 0;
26}
27
28static inline void *dma_alloc_coherent(struct device *dev, size_t size,
Al Viro6dae2c22005-10-21 03:21:38 -040029 dma_addr_t *dma_handle, gfp_t flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 if (sh_mv.mv_consistent_alloc) {
32 void *ret;
33
34 ret = sh_mv.mv_consistent_alloc(dev, size, dma_handle, flag);
35 if (ret != NULL)
36 return ret;
37 }
38
39 return consistent_alloc(flag, size, dma_handle);
40}
41
42static inline void dma_free_coherent(struct device *dev, size_t size,
43 void *vaddr, dma_addr_t dma_handle)
44{
45 if (sh_mv.mv_consistent_free) {
46 int ret;
47
48 ret = sh_mv.mv_consistent_free(dev, size, vaddr, dma_handle);
49 if (ret == 0)
50 return;
51 }
52
53 consistent_free(vaddr, size);
54}
55
Ralf Baechled3fa72e2006-12-06 20:38:56 -080056static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 enum dma_data_direction dir)
58{
59 consistent_sync(vaddr, size, (int)dir);
60}
61
62static inline dma_addr_t dma_map_single(struct device *dev,
63 void *ptr, size_t size,
64 enum dma_data_direction dir)
65{
66#if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
67 if (dev->bus == &pci_bus_type)
68 return virt_to_bus(ptr);
69#endif
Paul Mundt54321432006-12-09 09:17:01 +090070 dma_cache_sync(dev, ptr, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 return virt_to_bus(ptr);
73}
74
75#define dma_unmap_single(dev, addr, size, dir) do { } while (0)
76
77static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
78 int nents, enum dma_data_direction dir)
79{
80 int i;
81
82 for (i = 0; i < nents; i++) {
83#if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
Paul Mundt54321432006-12-09 09:17:01 +090084 dma_cache_sync(dev, page_address(sg[i].page) + sg[i].offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 sg[i].length, dir);
86#endif
87 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
88 }
89
90 return nents;
91}
92
93#define dma_unmap_sg(dev, sg, nents, dir) do { } while (0)
94
95static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
96 unsigned long offset, size_t size,
97 enum dma_data_direction dir)
98{
99 return dma_map_single(dev, page_address(page) + offset, size, dir);
100}
101
102static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
103 size_t size, enum dma_data_direction dir)
104{
105 dma_unmap_single(dev, dma_address, size, dir);
106}
107
108static inline void dma_sync_single(struct device *dev, dma_addr_t dma_handle,
109 size_t size, enum dma_data_direction dir)
110{
111#if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
112 if (dev->bus == &pci_bus_type)
113 return;
114#endif
Paul Mundt54321432006-12-09 09:17:01 +0900115 dma_cache_sync(dev, bus_to_virt(dma_handle), size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static inline void dma_sync_single_range(struct device *dev,
119 dma_addr_t dma_handle,
120 unsigned long offset, size_t size,
121 enum dma_data_direction dir)
122{
123#if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
124 if (dev->bus == &pci_bus_type)
125 return;
126#endif
Paul Mundt54321432006-12-09 09:17:01 +0900127 dma_cache_sync(dev, bus_to_virt(dma_handle) + offset, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130static inline void dma_sync_sg(struct device *dev, struct scatterlist *sg,
131 int nelems, enum dma_data_direction dir)
132{
133 int i;
134
135 for (i = 0; i < nelems; i++) {
136#if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
Paul Mundt54321432006-12-09 09:17:01 +0900137 dma_cache_sync(dev, page_address(sg[i].page) + sg[i].offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 sg[i].length, dir);
139#endif
140 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
141 }
142}
143
Paul Mundt87b0ef92006-09-27 18:34:41 +0900144static inline void dma_sync_single_for_cpu(struct device *dev,
145 dma_addr_t dma_handle, size_t size,
146 enum dma_data_direction dir)
147{
148 dma_sync_single(dev, dma_handle, size, dir);
149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Paul Mundt87b0ef92006-09-27 18:34:41 +0900151static inline void dma_sync_single_for_device(struct device *dev,
152 dma_addr_t dma_handle,
153 size_t size,
154 enum dma_data_direction dir)
155{
156 dma_sync_single(dev, dma_handle, size, dir);
157}
158
159static inline void dma_sync_sg_for_cpu(struct device *dev,
160 struct scatterlist *sg, int nelems,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 enum dma_data_direction dir)
Paul Mundt87b0ef92006-09-27 18:34:41 +0900162{
163 dma_sync_sg(dev, sg, nelems, dir);
164}
Paul Mundt0d831772006-01-16 22:14:09 -0800165
Paul Mundt87b0ef92006-09-27 18:34:41 +0900166static inline void dma_sync_sg_for_device(struct device *dev,
167 struct scatterlist *sg, int nelems,
168 enum dma_data_direction dir)
169{
170 dma_sync_sg(dev, sg, nelems, dir);
171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174static inline int dma_get_cache_alignment(void)
175{
176 /*
177 * Each processor family will define its own L1_CACHE_SHIFT,
178 * L1_CACHE_BYTES wraps to this, so this is always safe.
179 */
180 return L1_CACHE_BYTES;
181}
182
183static inline int dma_mapping_error(dma_addr_t dma_addr)
184{
185 return dma_addr == 0;
186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#endif /* __ASM_SH_DMA_MAPPING_H */