Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 IBM |
| 3 | * |
| 4 | * Implements the generic device dma API for powerpc. |
| 5 | * the pci and vio busses |
| 6 | */ |
| 7 | #ifndef _ASM_DMA_MAPPING_H |
| 8 | #define _ASM_DMA_MAPPING_H |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 9 | #ifdef __KERNEL__ |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/cache.h> |
| 13 | /* need struct page definitions */ |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/scatterlist.h> |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 16 | #include <linux/dma-attrs.h> |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 17 | #include <asm/io.h> |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 18 | #include <asm/swiotlb.h> |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 19 | |
| 20 | #define DMA_ERROR_CODE (~(dma_addr_t)0x0) |
| 21 | |
Becky Bruce | ec3cf2e | 2009-05-14 12:42:28 +0000 | [diff] [blame] | 22 | /* Some dma direct funcs must be visible for use in other dma_ops */ |
| 23 | extern void *dma_direct_alloc_coherent(struct device *dev, size_t size, |
| 24 | dma_addr_t *dma_handle, gfp_t flag); |
| 25 | extern void dma_direct_free_coherent(struct device *dev, size_t size, |
| 26 | void *vaddr, dma_addr_t dma_handle); |
| 27 | |
| 28 | extern unsigned long get_dma_direct_offset(struct device *dev); |
| 29 | |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 30 | #ifdef CONFIG_NOT_COHERENT_CACHE |
| 31 | /* |
| 32 | * DMA-consistent mapping functions for PowerPCs that don't support |
| 33 | * cache snooping. These allocate/free a region of uncached mapped |
| 34 | * memory space for use with DMA devices. Alternatively, you could |
| 35 | * allocate the space "normally" and use the cache management functions |
| 36 | * to ensure it is consistent. |
| 37 | */ |
Benjamin Herrenschmidt | 8b31e49 | 2009-05-27 13:50:33 +1000 | [diff] [blame] | 38 | struct device; |
| 39 | extern void *__dma_alloc_coherent(struct device *dev, size_t size, |
| 40 | dma_addr_t *handle, gfp_t gfp); |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 41 | extern void __dma_free_coherent(size_t size, void *vaddr); |
| 42 | extern void __dma_sync(void *vaddr, size_t size, int direction); |
| 43 | extern void __dma_sync_page(struct page *page, unsigned long offset, |
| 44 | size_t size, int direction); |
| 45 | |
| 46 | #else /* ! CONFIG_NOT_COHERENT_CACHE */ |
| 47 | /* |
| 48 | * Cache coherent cores. |
| 49 | */ |
| 50 | |
Benjamin Herrenschmidt | 8b31e49 | 2009-05-27 13:50:33 +1000 | [diff] [blame] | 51 | #define __dma_alloc_coherent(dev, gfp, size, handle) NULL |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 52 | #define __dma_free_coherent(size, addr) ((void)0) |
| 53 | #define __dma_sync(addr, size, rw) ((void)0) |
| 54 | #define __dma_sync_page(pg, off, sz, rw) ((void)0) |
| 55 | |
| 56 | #endif /* ! CONFIG_NOT_COHERENT_CACHE */ |
| 57 | |
Mark Nelson | 3a4c6f0 | 2008-07-05 05:05:45 +1000 | [diff] [blame] | 58 | static inline unsigned long device_to_mask(struct device *dev) |
| 59 | { |
| 60 | if (dev->dma_mask && *dev->dma_mask) |
| 61 | return *dev->dma_mask; |
| 62 | /* Assume devices without mask can take 32 bit addresses */ |
| 63 | return 0xfffffffful; |
| 64 | } |
| 65 | |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 66 | /* |
| 67 | * DMA operations are abstracted for G5 vs. i/pSeries, PCI vs. VIO |
| 68 | */ |
| 69 | struct dma_mapping_ops { |
| 70 | void * (*alloc_coherent)(struct device *dev, size_t size, |
| 71 | dma_addr_t *dma_handle, gfp_t flag); |
| 72 | void (*free_coherent)(struct device *dev, size_t size, |
| 73 | void *vaddr, dma_addr_t dma_handle); |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 74 | int (*map_sg)(struct device *dev, struct scatterlist *sg, |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 75 | int nents, enum dma_data_direction direction, |
| 76 | struct dma_attrs *attrs); |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 77 | void (*unmap_sg)(struct device *dev, struct scatterlist *sg, |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 78 | int nents, enum dma_data_direction direction, |
| 79 | struct dma_attrs *attrs); |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 80 | int (*dma_supported)(struct device *dev, u64 mask); |
| 81 | int (*set_dma_mask)(struct device *dev, u64 dma_mask); |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 82 | dma_addr_t (*map_page)(struct device *dev, struct page *page, |
| 83 | unsigned long offset, size_t size, |
| 84 | enum dma_data_direction direction, |
| 85 | struct dma_attrs *attrs); |
| 86 | void (*unmap_page)(struct device *dev, |
| 87 | dma_addr_t dma_address, size_t size, |
| 88 | enum dma_data_direction direction, |
| 89 | struct dma_attrs *attrs); |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 90 | #ifdef CONFIG_PPC_NEED_DMA_SYNC_OPS |
| 91 | void (*sync_single_range_for_cpu)(struct device *hwdev, |
| 92 | dma_addr_t dma_handle, unsigned long offset, |
| 93 | size_t size, |
| 94 | enum dma_data_direction direction); |
| 95 | void (*sync_single_range_for_device)(struct device *hwdev, |
| 96 | dma_addr_t dma_handle, unsigned long offset, |
| 97 | size_t size, |
| 98 | enum dma_data_direction direction); |
| 99 | void (*sync_sg_for_cpu)(struct device *hwdev, |
| 100 | struct scatterlist *sg, int nelems, |
| 101 | enum dma_data_direction direction); |
| 102 | void (*sync_sg_for_device)(struct device *hwdev, |
| 103 | struct scatterlist *sg, int nelems, |
| 104 | enum dma_data_direction direction); |
| 105 | #endif |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 106 | }; |
| 107 | |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 108 | /* |
| 109 | * Available generic sets of operations |
| 110 | */ |
| 111 | #ifdef CONFIG_PPC64 |
| 112 | extern struct dma_mapping_ops dma_iommu_ops; |
| 113 | #endif |
| 114 | extern struct dma_mapping_ops dma_direct_ops; |
| 115 | |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 116 | static inline struct dma_mapping_ops *get_dma_ops(struct device *dev) |
| 117 | { |
| 118 | /* We don't handle the NULL dev case for ISA for now. We could |
| 119 | * do it via an out of line call but it is not needed for now. The |
| 120 | * only ISA DMA device we support is the floppy and we have a hack |
| 121 | * in the floppy driver directly to get a device for us. |
| 122 | */ |
Kumar Gala | 4ae0ff6 | 2009-03-19 03:40:52 +0000 | [diff] [blame] | 123 | if (unlikely(dev == NULL)) |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 124 | return NULL; |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 125 | |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 126 | return dev->archdata.dma_ops; |
| 127 | } |
| 128 | |
Michael Ellerman | 1f62a16 | 2008-01-30 01:13:58 +1100 | [diff] [blame] | 129 | static inline void set_dma_ops(struct device *dev, struct dma_mapping_ops *ops) |
| 130 | { |
| 131 | dev->archdata.dma_ops = ops; |
| 132 | } |
| 133 | |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 134 | static inline int dma_supported(struct device *dev, u64 mask) |
| 135 | { |
| 136 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 137 | |
| 138 | if (unlikely(dma_ops == NULL)) |
| 139 | return 0; |
| 140 | if (dma_ops->dma_supported == NULL) |
| 141 | return 1; |
| 142 | return dma_ops->dma_supported(dev, mask); |
| 143 | } |
| 144 | |
Michael Ellerman | 84631f3 | 2007-12-17 17:35:53 +1100 | [diff] [blame] | 145 | /* We have our own implementation of pci_set_dma_mask() */ |
| 146 | #define HAVE_ARCH_PCI_SET_DMA_MASK |
| 147 | |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 148 | static inline int dma_set_mask(struct device *dev, u64 dma_mask) |
| 149 | { |
| 150 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 151 | |
| 152 | if (unlikely(dma_ops == NULL)) |
| 153 | return -EIO; |
| 154 | if (dma_ops->set_dma_mask != NULL) |
| 155 | return dma_ops->set_dma_mask(dev, dma_mask); |
| 156 | if (!dev->dma_mask || !dma_supported(dev, dma_mask)) |
| 157 | return -EIO; |
| 158 | *dev->dma_mask = dma_mask; |
| 159 | return 0; |
| 160 | } |
| 161 | |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 162 | /* |
Mark Nelson | c73049f | 2008-10-27 20:38:14 +0000 | [diff] [blame] | 163 | * map_/unmap_single actually call through to map/unmap_page now that all the |
| 164 | * dma_mapping_ops have been converted over. We just have to get the page and |
| 165 | * offset to pass through to map_page |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 166 | */ |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 167 | static inline dma_addr_t dma_map_single_attrs(struct device *dev, |
| 168 | void *cpu_addr, |
| 169 | size_t size, |
| 170 | enum dma_data_direction direction, |
| 171 | struct dma_attrs *attrs) |
| 172 | { |
| 173 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 174 | |
| 175 | BUG_ON(!dma_ops); |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 176 | |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 177 | return dma_ops->map_page(dev, virt_to_page(cpu_addr), |
| 178 | (unsigned long)cpu_addr % PAGE_SIZE, size, |
| 179 | direction, attrs); |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static inline void dma_unmap_single_attrs(struct device *dev, |
| 183 | dma_addr_t dma_addr, |
| 184 | size_t size, |
| 185 | enum dma_data_direction direction, |
| 186 | struct dma_attrs *attrs) |
| 187 | { |
| 188 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 189 | |
| 190 | BUG_ON(!dma_ops); |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 191 | |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 192 | dma_ops->unmap_page(dev, dma_addr, size, direction, attrs); |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static inline dma_addr_t dma_map_page_attrs(struct device *dev, |
| 196 | struct page *page, |
| 197 | unsigned long offset, size_t size, |
| 198 | enum dma_data_direction direction, |
| 199 | struct dma_attrs *attrs) |
| 200 | { |
| 201 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 202 | |
| 203 | BUG_ON(!dma_ops); |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 204 | |
Mark Nelson | c73049f | 2008-10-27 20:38:14 +0000 | [diff] [blame] | 205 | return dma_ops->map_page(dev, page, offset, size, direction, attrs); |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static inline void dma_unmap_page_attrs(struct device *dev, |
| 209 | dma_addr_t dma_address, |
| 210 | size_t size, |
| 211 | enum dma_data_direction direction, |
| 212 | struct dma_attrs *attrs) |
| 213 | { |
| 214 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 215 | |
| 216 | BUG_ON(!dma_ops); |
Becky Bruce | 4fc665b | 2008-09-12 10:34:46 +0000 | [diff] [blame] | 217 | |
Mark Nelson | c73049f | 2008-10-27 20:38:14 +0000 | [diff] [blame] | 218 | dma_ops->unmap_page(dev, dma_address, size, direction, attrs); |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, |
| 222 | int nents, enum dma_data_direction direction, |
| 223 | struct dma_attrs *attrs) |
| 224 | { |
| 225 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 226 | |
| 227 | BUG_ON(!dma_ops); |
| 228 | return dma_ops->map_sg(dev, sg, nents, direction, attrs); |
| 229 | } |
| 230 | |
| 231 | static inline void dma_unmap_sg_attrs(struct device *dev, |
| 232 | struct scatterlist *sg, |
| 233 | int nhwentries, |
| 234 | enum dma_data_direction direction, |
| 235 | struct dma_attrs *attrs) |
| 236 | { |
| 237 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 238 | |
| 239 | BUG_ON(!dma_ops); |
| 240 | dma_ops->unmap_sg(dev, sg, nhwentries, direction, attrs); |
| 241 | } |
| 242 | |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 243 | static inline void *dma_alloc_coherent(struct device *dev, size_t size, |
| 244 | dma_addr_t *dma_handle, gfp_t flag) |
| 245 | { |
| 246 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 247 | |
| 248 | BUG_ON(!dma_ops); |
| 249 | return dma_ops->alloc_coherent(dev, size, dma_handle, flag); |
| 250 | } |
| 251 | |
| 252 | static inline void dma_free_coherent(struct device *dev, size_t size, |
| 253 | void *cpu_addr, dma_addr_t dma_handle) |
| 254 | { |
| 255 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 256 | |
| 257 | BUG_ON(!dma_ops); |
| 258 | dma_ops->free_coherent(dev, size, cpu_addr, dma_handle); |
| 259 | } |
| 260 | |
| 261 | static inline dma_addr_t dma_map_single(struct device *dev, void *cpu_addr, |
| 262 | size_t size, |
| 263 | enum dma_data_direction direction) |
| 264 | { |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 265 | return dma_map_single_attrs(dev, cpu_addr, size, direction, NULL); |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, |
| 269 | size_t size, |
| 270 | enum dma_data_direction direction) |
| 271 | { |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 272 | dma_unmap_single_attrs(dev, dma_addr, size, direction, NULL); |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static inline dma_addr_t dma_map_page(struct device *dev, struct page *page, |
| 276 | unsigned long offset, size_t size, |
| 277 | enum dma_data_direction direction) |
| 278 | { |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 279 | return dma_map_page_attrs(dev, page, offset, size, direction, NULL); |
Anton Blanchard | 33ff910 | 2007-10-16 14:54:33 -0500 | [diff] [blame] | 280 | } |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 281 | |
| 282 | static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address, |
| 283 | size_t size, |
| 284 | enum dma_data_direction direction) |
| 285 | { |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 286 | dma_unmap_page_attrs(dev, dma_address, size, direction, NULL); |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static inline int dma_map_sg(struct device *dev, struct scatterlist *sg, |
| 290 | int nents, enum dma_data_direction direction) |
| 291 | { |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 292 | return dma_map_sg_attrs(dev, sg, nents, direction, NULL); |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg, |
| 296 | int nhwentries, |
| 297 | enum dma_data_direction direction) |
| 298 | { |
Mark Nelson | 3affedc | 2008-07-05 05:05:42 +1000 | [diff] [blame] | 299 | dma_unmap_sg_attrs(dev, sg, nhwentries, direction, NULL); |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 300 | } |
| 301 | |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 302 | #ifdef CONFIG_PPC_NEED_DMA_SYNC_OPS |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 303 | static inline void dma_sync_single_for_cpu(struct device *dev, |
| 304 | dma_addr_t dma_handle, size_t size, |
| 305 | enum dma_data_direction direction) |
| 306 | { |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 307 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 308 | |
| 309 | BUG_ON(!dma_ops); |
Michael Ellerman | 6f0b1c6 | 2009-06-22 23:13:48 +0000 | [diff] [blame] | 310 | |
| 311 | if (dma_ops->sync_single_range_for_cpu) |
| 312 | dma_ops->sync_single_range_for_cpu(dev, dma_handle, 0, |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 313 | size, direction); |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static inline void dma_sync_single_for_device(struct device *dev, |
| 317 | dma_addr_t dma_handle, size_t size, |
| 318 | enum dma_data_direction direction) |
| 319 | { |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 320 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 321 | |
| 322 | BUG_ON(!dma_ops); |
Michael Ellerman | 6f0b1c6 | 2009-06-22 23:13:48 +0000 | [diff] [blame] | 323 | |
| 324 | if (dma_ops->sync_single_range_for_device) |
| 325 | dma_ops->sync_single_range_for_device(dev, dma_handle, |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 326 | 0, size, direction); |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static inline void dma_sync_sg_for_cpu(struct device *dev, |
Jens Axboe | 78bdc31 | 2007-10-12 13:44:12 +0200 | [diff] [blame] | 330 | struct scatterlist *sgl, int nents, |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 331 | enum dma_data_direction direction) |
| 332 | { |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 333 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 334 | |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 335 | BUG_ON(!dma_ops); |
Michael Ellerman | 6f0b1c6 | 2009-06-22 23:13:48 +0000 | [diff] [blame] | 336 | |
| 337 | if (dma_ops->sync_sg_for_cpu) |
| 338 | dma_ops->sync_sg_for_cpu(dev, sgl, nents, direction); |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static inline void dma_sync_sg_for_device(struct device *dev, |
Jens Axboe | 78bdc31 | 2007-10-12 13:44:12 +0200 | [diff] [blame] | 342 | struct scatterlist *sgl, int nents, |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 343 | enum dma_data_direction direction) |
| 344 | { |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 345 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 346 | |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 347 | BUG_ON(!dma_ops); |
Michael Ellerman | 6f0b1c6 | 2009-06-22 23:13:48 +0000 | [diff] [blame] | 348 | |
| 349 | if (dma_ops->sync_sg_for_device) |
| 350 | dma_ops->sync_sg_for_device(dev, sgl, nents, direction); |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 351 | } |
| 352 | |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 353 | static inline void dma_sync_single_range_for_cpu(struct device *dev, |
| 354 | dma_addr_t dma_handle, unsigned long offset, size_t size, |
| 355 | enum dma_data_direction direction) |
| 356 | { |
| 357 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 358 | |
| 359 | BUG_ON(!dma_ops); |
Michael Ellerman | 6f0b1c6 | 2009-06-22 23:13:48 +0000 | [diff] [blame] | 360 | |
| 361 | if (dma_ops->sync_single_range_for_cpu) |
| 362 | dma_ops->sync_single_range_for_cpu(dev, dma_handle, |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 363 | offset, size, direction); |
| 364 | } |
| 365 | |
| 366 | static inline void dma_sync_single_range_for_device(struct device *dev, |
| 367 | dma_addr_t dma_handle, unsigned long offset, size_t size, |
| 368 | enum dma_data_direction direction) |
| 369 | { |
| 370 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 371 | |
| 372 | BUG_ON(!dma_ops); |
Michael Ellerman | 6f0b1c6 | 2009-06-22 23:13:48 +0000 | [diff] [blame] | 373 | |
| 374 | if (dma_ops->sync_single_range_for_device) |
| 375 | dma_ops->sync_single_range_for_device(dev, dma_handle, offset, |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 376 | size, direction); |
| 377 | } |
| 378 | #else /* CONFIG_PPC_NEED_DMA_SYNC_OPS */ |
Becky Bruce | 0efbb57 | 2008-12-04 08:12:40 +0000 | [diff] [blame] | 379 | static inline void dma_sync_single_for_cpu(struct device *dev, |
| 380 | dma_addr_t dma_handle, size_t size, |
| 381 | enum dma_data_direction direction) |
| 382 | { |
| 383 | } |
| 384 | |
| 385 | static inline void dma_sync_single_for_device(struct device *dev, |
| 386 | dma_addr_t dma_handle, size_t size, |
| 387 | enum dma_data_direction direction) |
| 388 | { |
| 389 | } |
| 390 | |
| 391 | static inline void dma_sync_sg_for_cpu(struct device *dev, |
| 392 | struct scatterlist *sgl, int nents, |
| 393 | enum dma_data_direction direction) |
| 394 | { |
| 395 | } |
| 396 | |
| 397 | static inline void dma_sync_sg_for_device(struct device *dev, |
| 398 | struct scatterlist *sgl, int nents, |
| 399 | enum dma_data_direction direction) |
| 400 | { |
| 401 | } |
| 402 | |
| 403 | static inline void dma_sync_single_range_for_cpu(struct device *dev, |
| 404 | dma_addr_t dma_handle, unsigned long offset, size_t size, |
| 405 | enum dma_data_direction direction) |
| 406 | { |
| 407 | } |
| 408 | |
| 409 | static inline void dma_sync_single_range_for_device(struct device *dev, |
| 410 | dma_addr_t dma_handle, unsigned long offset, size_t size, |
| 411 | enum dma_data_direction direction) |
| 412 | { |
| 413 | } |
Becky Bruce | 15e09c0 | 2008-11-20 06:49:16 +0000 | [diff] [blame] | 414 | #endif |
| 415 | |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 416 | static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr) |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 417 | { |
| 418 | #ifdef CONFIG_PPC64 |
| 419 | return (dma_addr == DMA_ERROR_CODE); |
| 420 | #else |
| 421 | return 0; |
| 422 | #endif |
| 423 | } |
| 424 | |
FUJITA Tomonori | 9a937c9 | 2009-07-10 10:04:57 +0900 | [diff] [blame] | 425 | static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) |
| 426 | { |
FUJITA Tomonori | 762afb7 | 2009-08-04 19:08:22 +0000 | [diff] [blame^] | 427 | #ifdef CONFIG_SWIOTLB |
| 428 | struct dev_archdata *sd = &dev->archdata; |
FUJITA Tomonori | 9a937c9 | 2009-07-10 10:04:57 +0900 | [diff] [blame] | 429 | |
FUJITA Tomonori | 762afb7 | 2009-08-04 19:08:22 +0000 | [diff] [blame^] | 430 | if (sd->max_direct_dma_addr && addr + size > sd->max_direct_dma_addr) |
FUJITA Tomonori | 9a937c9 | 2009-07-10 10:04:57 +0900 | [diff] [blame] | 431 | return 0; |
FUJITA Tomonori | 762afb7 | 2009-08-04 19:08:22 +0000 | [diff] [blame^] | 432 | #endif |
FUJITA Tomonori | 9a937c9 | 2009-07-10 10:04:57 +0900 | [diff] [blame] | 433 | |
| 434 | if (!dev->dma_mask) |
| 435 | return 0; |
| 436 | |
| 437 | return addr + size <= *dev->dma_mask; |
| 438 | } |
| 439 | |
FUJITA Tomonori | 8d4f533 | 2009-07-10 10:05:01 +0900 | [diff] [blame] | 440 | static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) |
| 441 | { |
| 442 | return paddr + get_dma_direct_offset(dev); |
| 443 | } |
| 444 | |
| 445 | static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) |
| 446 | { |
| 447 | return daddr - get_dma_direct_offset(dev); |
| 448 | } |
| 449 | |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 450 | #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f) |
| 451 | #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h) |
| 452 | #ifdef CONFIG_NOT_COHERENT_CACHE |
Ralf Baechle | f67637e | 2006-12-06 20:38:54 -0800 | [diff] [blame] | 453 | #define dma_is_consistent(d, h) (0) |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 454 | #else |
Ralf Baechle | f67637e | 2006-12-06 20:38:54 -0800 | [diff] [blame] | 455 | #define dma_is_consistent(d, h) (1) |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 456 | #endif |
| 457 | |
| 458 | static inline int dma_get_cache_alignment(void) |
| 459 | { |
| 460 | #ifdef CONFIG_PPC64 |
| 461 | /* no easy way to get cache size on all processors, so return |
| 462 | * the maximum possible, to be safe */ |
Ravikiran G Thirumalai | 1fd73c6 | 2006-01-08 01:01:28 -0800 | [diff] [blame] | 463 | return (1 << INTERNODE_CACHE_SHIFT); |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 464 | #else |
| 465 | /* |
| 466 | * Each processor family will define its own L1_CACHE_SHIFT, |
| 467 | * L1_CACHE_BYTES wraps to this, so this is always safe. |
| 468 | */ |
| 469 | return L1_CACHE_BYTES; |
| 470 | #endif |
| 471 | } |
| 472 | |
Ralf Baechle | d3fa72e | 2006-12-06 20:38:56 -0800 | [diff] [blame] | 473 | static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size, |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 474 | enum dma_data_direction direction) |
| 475 | { |
| 476 | BUG_ON(direction == DMA_NONE); |
| 477 | __dma_sync(vaddr, size, (int)direction); |
| 478 | } |
| 479 | |
Arnd Bergmann | 88ced03 | 2005-12-16 22:43:46 +0100 | [diff] [blame] | 480 | #endif /* __KERNEL__ */ |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 481 | #endif /* _ASM_DMA_MAPPING_H */ |