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 |
Arnd Bergmann | 88ced03 | 2005-12-16 22:43:46 +0100 | [diff] [blame] | 9 | #ifdef __KERNEL__ |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 10 | |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/cache.h> |
| 13 | /* need struct page definitions */ |
| 14 | #include <linux/mm.h> |
| 15 | #include <asm/scatterlist.h> |
| 16 | #include <asm/io.h> |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 17 | |
| 18 | #define DMA_ERROR_CODE (~(dma_addr_t)0x0) |
| 19 | |
| 20 | #ifdef CONFIG_NOT_COHERENT_CACHE |
| 21 | /* |
| 22 | * DMA-consistent mapping functions for PowerPCs that don't support |
| 23 | * cache snooping. These allocate/free a region of uncached mapped |
| 24 | * memory space for use with DMA devices. Alternatively, you could |
| 25 | * allocate the space "normally" and use the cache management functions |
| 26 | * to ensure it is consistent. |
| 27 | */ |
| 28 | extern void *__dma_alloc_coherent(size_t size, dma_addr_t *handle, gfp_t gfp); |
| 29 | extern void __dma_free_coherent(size_t size, void *vaddr); |
| 30 | extern void __dma_sync(void *vaddr, size_t size, int direction); |
| 31 | extern void __dma_sync_page(struct page *page, unsigned long offset, |
| 32 | size_t size, int direction); |
| 33 | |
| 34 | #else /* ! CONFIG_NOT_COHERENT_CACHE */ |
| 35 | /* |
| 36 | * Cache coherent cores. |
| 37 | */ |
| 38 | |
| 39 | #define __dma_alloc_coherent(gfp, size, handle) NULL |
Al Viro | 6a13f66 | 2007-01-30 13:24:00 +0000 | [diff] [blame] | 40 | #define __dma_free_coherent(size, addr) ((void)0) |
| 41 | #define __dma_sync(addr, size, rw) ((void)0) |
| 42 | #define __dma_sync_page(pg, off, sz, rw) ((void)0) |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 43 | |
| 44 | #endif /* ! CONFIG_NOT_COHERENT_CACHE */ |
| 45 | |
| 46 | #ifdef CONFIG_PPC64 |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 47 | /* |
| 48 | * DMA operations are abstracted for G5 vs. i/pSeries, PCI vs. VIO |
| 49 | */ |
| 50 | struct dma_mapping_ops { |
| 51 | void * (*alloc_coherent)(struct device *dev, size_t size, |
| 52 | dma_addr_t *dma_handle, gfp_t flag); |
| 53 | void (*free_coherent)(struct device *dev, size_t size, |
| 54 | void *vaddr, dma_addr_t dma_handle); |
| 55 | dma_addr_t (*map_single)(struct device *dev, void *ptr, |
| 56 | size_t size, enum dma_data_direction direction); |
| 57 | void (*unmap_single)(struct device *dev, dma_addr_t dma_addr, |
| 58 | size_t size, enum dma_data_direction direction); |
| 59 | int (*map_sg)(struct device *dev, struct scatterlist *sg, |
| 60 | int nents, enum dma_data_direction direction); |
| 61 | void (*unmap_sg)(struct device *dev, struct scatterlist *sg, |
| 62 | int nents, enum dma_data_direction direction); |
| 63 | int (*dma_supported)(struct device *dev, u64 mask); |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 64 | int (*set_dma_mask)(struct device *dev, u64 dma_mask); |
| 65 | }; |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 66 | |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 67 | static inline struct dma_mapping_ops *get_dma_ops(struct device *dev) |
| 68 | { |
| 69 | /* We don't handle the NULL dev case for ISA for now. We could |
| 70 | * do it via an out of line call but it is not needed for now. The |
| 71 | * only ISA DMA device we support is the floppy and we have a hack |
| 72 | * in the floppy driver directly to get a device for us. |
| 73 | */ |
| 74 | if (unlikely(dev == NULL || dev->archdata.dma_ops == NULL)) |
| 75 | return NULL; |
| 76 | return dev->archdata.dma_ops; |
| 77 | } |
| 78 | |
| 79 | static inline int dma_supported(struct device *dev, u64 mask) |
| 80 | { |
| 81 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 82 | |
| 83 | if (unlikely(dma_ops == NULL)) |
| 84 | return 0; |
| 85 | if (dma_ops->dma_supported == NULL) |
| 86 | return 1; |
| 87 | return dma_ops->dma_supported(dev, mask); |
| 88 | } |
| 89 | |
| 90 | static inline int dma_set_mask(struct device *dev, u64 dma_mask) |
| 91 | { |
| 92 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 93 | |
| 94 | if (unlikely(dma_ops == NULL)) |
| 95 | return -EIO; |
| 96 | if (dma_ops->set_dma_mask != NULL) |
| 97 | return dma_ops->set_dma_mask(dev, dma_mask); |
Benjamin Herrenschmidt | 8f2ea1f | 2007-08-07 08:05:10 +1000 | [diff] [blame] | 98 | if (!dev->dma_mask || !dma_supported(dev, dma_mask)) |
Benjamin Herrenschmidt | 12d04ee | 2006-11-11 17:25:02 +1100 | [diff] [blame] | 99 | return -EIO; |
| 100 | *dev->dma_mask = dma_mask; |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static inline void *dma_alloc_coherent(struct device *dev, size_t size, |
| 105 | dma_addr_t *dma_handle, gfp_t flag) |
| 106 | { |
| 107 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 108 | |
| 109 | BUG_ON(!dma_ops); |
| 110 | return dma_ops->alloc_coherent(dev, size, dma_handle, flag); |
| 111 | } |
| 112 | |
| 113 | static inline void dma_free_coherent(struct device *dev, size_t size, |
| 114 | void *cpu_addr, dma_addr_t dma_handle) |
| 115 | { |
| 116 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 117 | |
| 118 | BUG_ON(!dma_ops); |
| 119 | dma_ops->free_coherent(dev, size, cpu_addr, dma_handle); |
| 120 | } |
| 121 | |
| 122 | static inline dma_addr_t dma_map_single(struct device *dev, void *cpu_addr, |
| 123 | size_t size, |
| 124 | enum dma_data_direction direction) |
| 125 | { |
| 126 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 127 | |
| 128 | BUG_ON(!dma_ops); |
| 129 | return dma_ops->map_single(dev, cpu_addr, size, direction); |
| 130 | } |
| 131 | |
| 132 | static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, |
| 133 | size_t size, |
| 134 | enum dma_data_direction direction) |
| 135 | { |
| 136 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 137 | |
| 138 | BUG_ON(!dma_ops); |
| 139 | dma_ops->unmap_single(dev, dma_addr, size, direction); |
| 140 | } |
| 141 | |
| 142 | static inline dma_addr_t dma_map_page(struct device *dev, struct page *page, |
| 143 | unsigned long offset, size_t size, |
| 144 | enum dma_data_direction direction) |
| 145 | { |
| 146 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 147 | |
| 148 | BUG_ON(!dma_ops); |
| 149 | return dma_ops->map_single(dev, page_address(page) + offset, size, |
| 150 | direction); |
| 151 | } |
| 152 | |
| 153 | static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address, |
| 154 | size_t size, |
| 155 | enum dma_data_direction direction) |
| 156 | { |
| 157 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 158 | |
| 159 | BUG_ON(!dma_ops); |
| 160 | dma_ops->unmap_single(dev, dma_address, size, direction); |
| 161 | } |
| 162 | |
| 163 | static inline int dma_map_sg(struct device *dev, struct scatterlist *sg, |
| 164 | int nents, enum dma_data_direction direction) |
| 165 | { |
| 166 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 167 | |
| 168 | BUG_ON(!dma_ops); |
| 169 | return dma_ops->map_sg(dev, sg, nents, direction); |
| 170 | } |
| 171 | |
| 172 | static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg, |
| 173 | int nhwentries, |
| 174 | enum dma_data_direction direction) |
| 175 | { |
| 176 | struct dma_mapping_ops *dma_ops = get_dma_ops(dev); |
| 177 | |
| 178 | BUG_ON(!dma_ops); |
| 179 | dma_ops->unmap_sg(dev, sg, nhwentries, direction); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /* |
| 184 | * Available generic sets of operations |
| 185 | */ |
| 186 | extern struct dma_mapping_ops dma_iommu_ops; |
| 187 | extern struct dma_mapping_ops dma_direct_ops; |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 188 | |
Benjamin Herrenschmidt | 92b20c4 | 2006-11-11 17:25:14 +1100 | [diff] [blame] | 189 | extern unsigned long dma_direct_offset; |
| 190 | |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 191 | #else /* CONFIG_PPC64 */ |
| 192 | |
| 193 | #define dma_supported(dev, mask) (1) |
| 194 | |
| 195 | static inline int dma_set_mask(struct device *dev, u64 dma_mask) |
| 196 | { |
| 197 | if (!dev->dma_mask || !dma_supported(dev, mask)) |
| 198 | return -EIO; |
| 199 | |
| 200 | *dev->dma_mask = dma_mask; |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static inline void *dma_alloc_coherent(struct device *dev, size_t size, |
| 206 | dma_addr_t * dma_handle, |
| 207 | gfp_t gfp) |
| 208 | { |
| 209 | #ifdef CONFIG_NOT_COHERENT_CACHE |
| 210 | return __dma_alloc_coherent(size, dma_handle, gfp); |
| 211 | #else |
| 212 | void *ret; |
| 213 | /* ignore region specifiers */ |
| 214 | gfp &= ~(__GFP_DMA | __GFP_HIGHMEM); |
| 215 | |
| 216 | if (dev == NULL || dev->coherent_dma_mask < 0xffffffff) |
| 217 | gfp |= GFP_DMA; |
| 218 | |
| 219 | ret = (void *)__get_free_pages(gfp, get_order(size)); |
| 220 | |
| 221 | if (ret != NULL) { |
| 222 | memset(ret, 0, size); |
| 223 | *dma_handle = virt_to_bus(ret); |
| 224 | } |
| 225 | |
| 226 | return ret; |
| 227 | #endif |
| 228 | } |
| 229 | |
| 230 | static inline void |
| 231 | dma_free_coherent(struct device *dev, size_t size, void *vaddr, |
| 232 | dma_addr_t dma_handle) |
| 233 | { |
| 234 | #ifdef CONFIG_NOT_COHERENT_CACHE |
| 235 | __dma_free_coherent(size, vaddr); |
| 236 | #else |
| 237 | free_pages((unsigned long)vaddr, get_order(size)); |
| 238 | #endif |
| 239 | } |
| 240 | |
| 241 | static inline dma_addr_t |
| 242 | dma_map_single(struct device *dev, void *ptr, size_t size, |
| 243 | enum dma_data_direction direction) |
| 244 | { |
| 245 | BUG_ON(direction == DMA_NONE); |
| 246 | |
| 247 | __dma_sync(ptr, size, direction); |
| 248 | |
| 249 | return virt_to_bus(ptr); |
| 250 | } |
| 251 | |
Segher Boessenkool | f774216d | 2007-08-02 01:41:15 +1000 | [diff] [blame] | 252 | static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, |
| 253 | size_t size, |
| 254 | enum dma_data_direction direction) |
| 255 | { |
| 256 | /* We do nothing. */ |
| 257 | } |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 258 | |
| 259 | static inline dma_addr_t |
| 260 | dma_map_page(struct device *dev, struct page *page, |
| 261 | unsigned long offset, size_t size, |
| 262 | enum dma_data_direction direction) |
| 263 | { |
| 264 | BUG_ON(direction == DMA_NONE); |
| 265 | |
| 266 | __dma_sync_page(page, offset, size, direction); |
| 267 | |
| 268 | return page_to_bus(page) + offset; |
| 269 | } |
| 270 | |
Segher Boessenkool | f774216d | 2007-08-02 01:41:15 +1000 | [diff] [blame] | 271 | static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address, |
| 272 | size_t size, |
| 273 | enum dma_data_direction direction) |
| 274 | { |
| 275 | /* We do nothing. */ |
| 276 | } |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 277 | |
| 278 | static inline int |
| 279 | dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 280 | enum dma_data_direction direction) |
| 281 | { |
| 282 | int i; |
| 283 | |
| 284 | BUG_ON(direction == DMA_NONE); |
| 285 | |
| 286 | for (i = 0; i < nents; i++, sg++) { |
| 287 | BUG_ON(!sg->page); |
| 288 | __dma_sync_page(sg->page, sg->offset, sg->length, direction); |
| 289 | sg->dma_address = page_to_bus(sg->page) + sg->offset; |
| 290 | } |
| 291 | |
| 292 | return nents; |
| 293 | } |
| 294 | |
Segher Boessenkool | f774216d | 2007-08-02 01:41:15 +1000 | [diff] [blame] | 295 | static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg, |
| 296 | int nhwentries, |
| 297 | enum dma_data_direction direction) |
| 298 | { |
| 299 | /* We don't do anything here. */ |
| 300 | } |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 301 | |
| 302 | #endif /* CONFIG_PPC64 */ |
| 303 | |
| 304 | static inline void dma_sync_single_for_cpu(struct device *dev, |
| 305 | dma_addr_t dma_handle, size_t size, |
| 306 | enum dma_data_direction direction) |
| 307 | { |
| 308 | BUG_ON(direction == DMA_NONE); |
| 309 | __dma_sync(bus_to_virt(dma_handle), size, direction); |
| 310 | } |
| 311 | |
| 312 | static inline void dma_sync_single_for_device(struct device *dev, |
| 313 | dma_addr_t dma_handle, size_t size, |
| 314 | enum dma_data_direction direction) |
| 315 | { |
| 316 | BUG_ON(direction == DMA_NONE); |
| 317 | __dma_sync(bus_to_virt(dma_handle), size, direction); |
| 318 | } |
| 319 | |
| 320 | static inline void dma_sync_sg_for_cpu(struct device *dev, |
| 321 | struct scatterlist *sg, int nents, |
| 322 | enum dma_data_direction direction) |
| 323 | { |
| 324 | int i; |
| 325 | |
| 326 | BUG_ON(direction == DMA_NONE); |
| 327 | |
| 328 | for (i = 0; i < nents; i++, sg++) |
| 329 | __dma_sync_page(sg->page, sg->offset, sg->length, direction); |
| 330 | } |
| 331 | |
| 332 | static inline void dma_sync_sg_for_device(struct device *dev, |
| 333 | struct scatterlist *sg, int nents, |
| 334 | enum dma_data_direction direction) |
| 335 | { |
| 336 | int i; |
| 337 | |
| 338 | BUG_ON(direction == DMA_NONE); |
| 339 | |
| 340 | for (i = 0; i < nents; i++, sg++) |
| 341 | __dma_sync_page(sg->page, sg->offset, sg->length, direction); |
| 342 | } |
| 343 | |
| 344 | static inline int dma_mapping_error(dma_addr_t dma_addr) |
| 345 | { |
| 346 | #ifdef CONFIG_PPC64 |
| 347 | return (dma_addr == DMA_ERROR_CODE); |
| 348 | #else |
| 349 | return 0; |
| 350 | #endif |
| 351 | } |
| 352 | |
| 353 | #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f) |
| 354 | #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h) |
| 355 | #ifdef CONFIG_NOT_COHERENT_CACHE |
Ralf Baechle | f67637e | 2006-12-06 20:38:54 -0800 | [diff] [blame] | 356 | #define dma_is_consistent(d, h) (0) |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 357 | #else |
Ralf Baechle | f67637e | 2006-12-06 20:38:54 -0800 | [diff] [blame] | 358 | #define dma_is_consistent(d, h) (1) |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 359 | #endif |
| 360 | |
| 361 | static inline int dma_get_cache_alignment(void) |
| 362 | { |
| 363 | #ifdef CONFIG_PPC64 |
| 364 | /* no easy way to get cache size on all processors, so return |
| 365 | * the maximum possible, to be safe */ |
Ravikiran G Thirumalai | 1fd73c6 | 2006-01-08 01:01:28 -0800 | [diff] [blame] | 366 | return (1 << INTERNODE_CACHE_SHIFT); |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 367 | #else |
| 368 | /* |
| 369 | * Each processor family will define its own L1_CACHE_SHIFT, |
| 370 | * L1_CACHE_BYTES wraps to this, so this is always safe. |
| 371 | */ |
| 372 | return L1_CACHE_BYTES; |
| 373 | #endif |
| 374 | } |
| 375 | |
| 376 | static inline void dma_sync_single_range_for_cpu(struct device *dev, |
| 377 | dma_addr_t dma_handle, unsigned long offset, size_t size, |
| 378 | enum dma_data_direction direction) |
| 379 | { |
| 380 | /* just sync everything for now */ |
| 381 | dma_sync_single_for_cpu(dev, dma_handle, offset + size, direction); |
| 382 | } |
| 383 | |
| 384 | static inline void dma_sync_single_range_for_device(struct device *dev, |
| 385 | dma_addr_t dma_handle, unsigned long offset, size_t size, |
| 386 | enum dma_data_direction direction) |
| 387 | { |
| 388 | /* just sync everything for now */ |
| 389 | dma_sync_single_for_device(dev, dma_handle, offset + size, direction); |
| 390 | } |
| 391 | |
Ralf Baechle | d3fa72e | 2006-12-06 20:38:56 -0800 | [diff] [blame] | 392 | 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] | 393 | enum dma_data_direction direction) |
| 394 | { |
| 395 | BUG_ON(direction == DMA_NONE); |
| 396 | __dma_sync(vaddr, size, (int)direction); |
| 397 | } |
| 398 | |
Arnd Bergmann | 88ced03 | 2005-12-16 22:43:46 +0100 | [diff] [blame] | 399 | #endif /* __KERNEL__ */ |
Stephen Rothwell | 78b0973 | 2005-11-19 01:40:46 +1100 | [diff] [blame] | 400 | #endif /* _ASM_DMA_MAPPING_H */ |