ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-sh / dma-mapping.h
1 #ifndef __ASM_SH_DMA_MAPPING_H
2 #define __ASM_SH_DMA_MAPPING_H
3
4 #include <linux/config.h>
5 #include <linux/mm.h>
6 #include <linux/device.h>
7 #include <asm/scatterlist.h>
8 #include <asm/io.h>
9
10 /* arch/sh/mm/consistent.c */
11 extern void *consistent_alloc(int gfp, size_t size, dma_addr_t *handle);
12 extern void consistent_free(void *vaddr, size_t size);
13 extern void consistent_sync(void *vaddr, size_t size, int direction);
14
15 #ifdef CONFIG_SH_DREAMCAST
16 struct pci_dev;
17 extern struct bus_type pci_bus_type;
18 extern void *__pci_alloc_consistent(struct pci_dev *hwdev, size_t size,
19                                     dma_addr_t *dma_handle);
20 extern void __pci_free_consistent(struct pci_dev *hwdev, size_t size,
21                                   void *vaddr, dma_addr_t dma_handle);
22 #endif
23
24 #define dma_supported(dev, mask)        (1)
25
26 static inline int dma_set_mask(struct device *dev, u64 mask)
27 {
28         if (!dev->dma_mask || !dma_supported(dev, mask))
29                 return -EIO;
30
31         *dev->dma_mask = mask;
32
33         return 0;
34 }
35
36 static inline void *dma_alloc_coherent(struct device *dev, size_t size,
37                          dma_addr_t *dma_handle, int flag)
38 {
39         /*
40          * Some platforms have special pci_alloc_consistent() implementations,
41          * in these instances we can't use the generic consistent_alloc().
42          */
43 #ifdef CONFIG_SH_DREAMCAST
44         if (dev && dev->bus == &pci_bus_type)
45                 return __pci_alloc_consistent(NULL, size, dma_handle);
46 #endif
47
48         return consistent_alloc(flag, size, dma_handle);
49 }
50
51 static inline void dma_free_coherent(struct device *dev, size_t size,
52                        void *vaddr, dma_addr_t dma_handle)
53 {
54         /*
55          * Same note as above applies to pci_free_consistent()..
56          */
57 #ifdef CONFIG_SH_DREAMCAST
58         if (dev && dev->bus == &pci_bus_type) {
59                 __pci_free_consistent(NULL, size, vaddr, dma_handle);
60                 return;
61         }
62 #endif
63
64         consistent_free(vaddr, size);
65 }
66
67 static inline void dma_cache_sync(void *vaddr, size_t size,
68                                   enum dma_data_direction dir)
69 {
70         consistent_sync(vaddr, size, (int)dir);
71 }
72
73 static inline dma_addr_t dma_map_single(struct device *dev,
74                                         void *ptr, size_t size,
75                                         enum dma_data_direction dir)
76 {
77 #if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
78         if (dev->bus == &pci_bus_type)
79                 return virt_to_bus(ptr);
80 #endif
81         dma_cache_sync(ptr, size, dir);
82
83         return virt_to_bus(ptr);
84 }
85
86 #define dma_unmap_single(dev, addr, size, dir)  do { } while (0)
87
88 static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
89                              int nents, enum dma_data_direction dir)
90 {
91         int i;
92
93         for (i = 0; i < nents; i++) {
94 #if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
95                 dma_cache_sync(page_address(sg[i].page) + sg[i].offset,
96                                sg[i].length, dir);
97 #endif
98                 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
99         }
100
101         return nents;
102 }
103
104 #define dma_unmap_sg(dev, sg, nents, dir)       do { } while (0)
105
106 static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
107                                       unsigned long offset, size_t size,
108                                       enum dma_data_direction dir)
109 {
110         return dma_map_single(dev, page_address(page) + offset, size, dir);
111 }
112
113 static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
114                                   size_t size, enum dma_data_direction dir)
115 {
116         dma_unmap_single(dev, dma_address, size, dir);
117 }
118
119 static inline void dma_sync_single(struct device *dev, dma_addr_t dma_handle,
120                                    size_t size, enum dma_data_direction dir)
121 {
122 #if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
123         if (dev->bus == &pci_bus_type)
124                 return;
125 #endif
126         dma_cache_sync(bus_to_virt(dma_handle), size, dir);
127 }
128
129 static inline void dma_sync_single_range(struct device *dev,
130                                          dma_addr_t dma_handle,
131                                          unsigned long offset, size_t size,
132                                          enum dma_data_direction dir)
133 {
134 #if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
135         if (dev->bus == &pci_bus_type)
136                 return;
137 #endif
138         dma_cache_sync(bus_to_virt(dma_handle) + offset, size, dir);
139 }
140
141 static inline void dma_sync_sg(struct device *dev, struct scatterlist *sg,
142                                int nelems, enum dma_data_direction dir)
143 {
144         int i;
145
146         for (i = 0; i < nelems; i++) {
147 #if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
148                 dma_cache_sync(page_address(sg[i].page) + sg[i].offset,
149                                sg[i].length, dir);
150 #endif
151                 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
152         }
153 }
154
155 static inline int dma_get_cache_alignment(void)
156 {
157         /*
158          * Each processor family will define its own L1_CACHE_SHIFT,
159          * L1_CACHE_BYTES wraps to this, so this is always safe.
160          */
161         return L1_CACHE_BYTES;
162 }
163
164 #endif /* __ASM_SH_DMA_MAPPING_H */
165