blob: fcea067f7a9ce08830c17971207b28c14d27dac8 [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>
Jens Axboe71df50a2007-10-23 12:52:48 +02005#include <linux/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
Paul Mundtc7666e72007-02-13 11:11:22 +090056#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
57#define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
58#define dma_is_consistent(d, h) (1)
59
Ralf Baechled3fa72e2006-12-06 20:38:56 -080060static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 enum dma_data_direction dir)
62{
63 consistent_sync(vaddr, size, (int)dir);
64}
65
66static inline dma_addr_t dma_map_single(struct device *dev,
67 void *ptr, size_t size,
68 enum dma_data_direction dir)
69{
70#if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
71 if (dev->bus == &pci_bus_type)
Paul Mundte257ad02007-07-25 11:18:00 +090072 return virt_to_phys(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#endif
Paul Mundt54321432006-12-09 09:17:01 +090074 dma_cache_sync(dev, ptr, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Paul Mundte257ad02007-07-25 11:18:00 +090076 return virt_to_phys(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79#define dma_unmap_single(dev, addr, size, dir) do { } while (0)
80
81static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
82 int nents, enum dma_data_direction dir)
83{
84 int i;
85
86 for (i = 0; i < nents; i++) {
87#if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
Jens Axboe71df50a2007-10-23 12:52:48 +020088 dma_cache_sync(dev, sg_virt(&sg[i]), sg[i].length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#endif
Jens Axboe71df50a2007-10-23 12:52:48 +020090 sg[i].dma_address = sg_phys(&sg[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
93 return nents;
94}
95
96#define dma_unmap_sg(dev, sg, nents, dir) do { } while (0)
97
98static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
99 unsigned long offset, size_t size,
100 enum dma_data_direction dir)
101{
102 return dma_map_single(dev, page_address(page) + offset, size, dir);
103}
104
105static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
106 size_t size, enum dma_data_direction dir)
107{
108 dma_unmap_single(dev, dma_address, size, dir);
109}
110
111static inline void dma_sync_single(struct device *dev, dma_addr_t dma_handle,
112 size_t size, enum dma_data_direction dir)
113{
114#if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
115 if (dev->bus == &pci_bus_type)
116 return;
117#endif
Paul Mundte257ad02007-07-25 11:18:00 +0900118 dma_cache_sync(dev, phys_to_virt(dma_handle), size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static inline void dma_sync_single_range(struct device *dev,
122 dma_addr_t dma_handle,
123 unsigned long offset, size_t size,
124 enum dma_data_direction dir)
125{
126#if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
127 if (dev->bus == &pci_bus_type)
128 return;
129#endif
Paul Mundte257ad02007-07-25 11:18:00 +0900130 dma_cache_sync(dev, phys_to_virt(dma_handle) + offset, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static inline void dma_sync_sg(struct device *dev, struct scatterlist *sg,
134 int nelems, enum dma_data_direction dir)
135{
136 int i;
137
138 for (i = 0; i < nelems; i++) {
139#if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
Jens Axboe71df50a2007-10-23 12:52:48 +0200140 dma_cache_sync(dev, sg_virt(&sg[i]), sg[i].length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#endif
Jens Axboe71df50a2007-10-23 12:52:48 +0200142 sg[i].dma_address = sg_phys(&sg[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144}
145
Paul Mundt87b0ef92006-09-27 18:34:41 +0900146static inline void dma_sync_single_for_cpu(struct device *dev,
147 dma_addr_t dma_handle, size_t size,
148 enum dma_data_direction dir)
149{
150 dma_sync_single(dev, dma_handle, size, dir);
151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Paul Mundt87b0ef92006-09-27 18:34:41 +0900153static inline void dma_sync_single_for_device(struct device *dev,
154 dma_addr_t dma_handle,
155 size_t size,
156 enum dma_data_direction dir)
157{
158 dma_sync_single(dev, dma_handle, size, dir);
159}
160
Paul Mundt32239262007-08-10 02:37:01 +0900161static inline void dma_sync_single_range_for_cpu(struct device *dev,
162 dma_addr_t dma_handle,
163 unsigned long offset,
164 size_t size,
165 enum dma_data_direction direction)
166{
167 dma_sync_single_for_cpu(dev, dma_handle+offset, size, direction);
168}
169
170static inline void dma_sync_single_range_for_device(struct device *dev,
171 dma_addr_t dma_handle,
172 unsigned long offset,
173 size_t size,
174 enum dma_data_direction direction)
175{
176 dma_sync_single_for_device(dev, dma_handle+offset, size, direction);
177}
178
179
Paul Mundt87b0ef92006-09-27 18:34:41 +0900180static inline void dma_sync_sg_for_cpu(struct device *dev,
181 struct scatterlist *sg, int nelems,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 enum dma_data_direction dir)
Paul Mundt87b0ef92006-09-27 18:34:41 +0900183{
184 dma_sync_sg(dev, sg, nelems, dir);
185}
Paul Mundt0d831772006-01-16 22:14:09 -0800186
Paul Mundt87b0ef92006-09-27 18:34:41 +0900187static inline void dma_sync_sg_for_device(struct device *dev,
188 struct scatterlist *sg, int nelems,
189 enum dma_data_direction dir)
190{
191 dma_sync_sg(dev, sg, nelems, dir);
192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195static inline int dma_get_cache_alignment(void)
196{
197 /*
198 * Each processor family will define its own L1_CACHE_SHIFT,
199 * L1_CACHE_BYTES wraps to this, so this is always safe.
200 */
201 return L1_CACHE_BYTES;
202}
203
204static inline int dma_mapping_error(dma_addr_t dma_addr)
205{
206 return dma_addr == 0;
207}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#endif /* __ASM_SH_DMA_MAPPING_H */