Glauber Costa | 6f53663 | 2008-03-25 18:36:20 -0300 | [diff] [blame] | 1 | #ifndef _ASM_DMA_MAPPING_H_ |
| 2 | #define _ASM_DMA_MAPPING_H_ |
| 3 | |
| 4 | /* |
| 5 | * IOMMU interface. See Documentation/DMA-mapping.txt and DMA-API.txt for |
| 6 | * documentation. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/scatterlist.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <asm/swiotlb.h> |
Joerg Roedel | 6c505ce | 2008-08-19 16:32:45 +0200 | [diff] [blame^] | 12 | #include <asm-generic/dma-coherent.h> |
Glauber Costa | 6f53663 | 2008-03-25 18:36:20 -0300 | [diff] [blame] | 13 | |
Glauber Costa | 7c18341 | 2008-03-25 18:36:36 -0300 | [diff] [blame] | 14 | extern dma_addr_t bad_dma_address; |
Glauber Costa | b7107a3 | 2008-03-25 18:36:39 -0300 | [diff] [blame] | 15 | extern int iommu_merge; |
Joerg Roedel | 6c505ce | 2008-08-19 16:32:45 +0200 | [diff] [blame^] | 16 | extern struct device x86_dma_fallback_dev; |
Glauber Costa | b7107a3 | 2008-03-25 18:36:39 -0300 | [diff] [blame] | 17 | extern int panic_on_overflow; |
Glauber Costa | fae9a0d | 2008-04-08 13:20:56 -0300 | [diff] [blame] | 18 | extern int force_iommu; |
Glauber Costa | 7c18341 | 2008-03-25 18:36:36 -0300 | [diff] [blame] | 19 | |
Glauber Costa | 6f53663 | 2008-03-25 18:36:20 -0300 | [diff] [blame] | 20 | struct dma_mapping_ops { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 21 | int (*mapping_error)(struct device *dev, |
| 22 | dma_addr_t dma_addr); |
Glauber Costa | 6f53663 | 2008-03-25 18:36:20 -0300 | [diff] [blame] | 23 | void* (*alloc_coherent)(struct device *dev, size_t size, |
| 24 | dma_addr_t *dma_handle, gfp_t gfp); |
| 25 | void (*free_coherent)(struct device *dev, size_t size, |
| 26 | void *vaddr, dma_addr_t dma_handle); |
Ingo Molnar | 2be6214 | 2008-04-19 19:19:56 +0200 | [diff] [blame] | 27 | dma_addr_t (*map_single)(struct device *hwdev, phys_addr_t ptr, |
Glauber Costa | 6f53663 | 2008-03-25 18:36:20 -0300 | [diff] [blame] | 28 | size_t size, int direction); |
| 29 | /* like map_single, but doesn't check the device mask */ |
Ingo Molnar | 2be6214 | 2008-04-19 19:19:56 +0200 | [diff] [blame] | 30 | dma_addr_t (*map_simple)(struct device *hwdev, phys_addr_t ptr, |
Glauber Costa | 6f53663 | 2008-03-25 18:36:20 -0300 | [diff] [blame] | 31 | size_t size, int direction); |
| 32 | void (*unmap_single)(struct device *dev, dma_addr_t addr, |
| 33 | size_t size, int direction); |
| 34 | void (*sync_single_for_cpu)(struct device *hwdev, |
| 35 | dma_addr_t dma_handle, size_t size, |
| 36 | int direction); |
| 37 | void (*sync_single_for_device)(struct device *hwdev, |
| 38 | dma_addr_t dma_handle, size_t size, |
| 39 | int direction); |
| 40 | void (*sync_single_range_for_cpu)(struct device *hwdev, |
| 41 | dma_addr_t dma_handle, unsigned long offset, |
| 42 | size_t size, int direction); |
| 43 | void (*sync_single_range_for_device)(struct device *hwdev, |
| 44 | dma_addr_t dma_handle, unsigned long offset, |
| 45 | size_t size, int direction); |
| 46 | void (*sync_sg_for_cpu)(struct device *hwdev, |
| 47 | struct scatterlist *sg, int nelems, |
| 48 | int direction); |
| 49 | void (*sync_sg_for_device)(struct device *hwdev, |
| 50 | struct scatterlist *sg, int nelems, |
| 51 | int direction); |
| 52 | int (*map_sg)(struct device *hwdev, struct scatterlist *sg, |
| 53 | int nents, int direction); |
| 54 | void (*unmap_sg)(struct device *hwdev, |
| 55 | struct scatterlist *sg, int nents, |
| 56 | int direction); |
| 57 | int (*dma_supported)(struct device *hwdev, u64 mask); |
| 58 | int is_phys; |
| 59 | }; |
| 60 | |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 61 | extern struct dma_mapping_ops *dma_ops; |
Glauber Costa | 22456b9 | 2008-03-25 18:36:21 -0300 | [diff] [blame] | 62 | |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 63 | static inline struct dma_mapping_ops *get_dma_ops(struct device *dev) |
Glauber Costa | c786df0 | 2008-03-25 18:36:37 -0300 | [diff] [blame] | 64 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 65 | #ifdef CONFIG_X86_32 |
| 66 | return dma_ops; |
| 67 | #else |
| 68 | if (unlikely(!dev) || !dev->archdata.dma_ops) |
| 69 | return dma_ops; |
| 70 | else |
| 71 | return dev->archdata.dma_ops; |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | /* Make sure we keep the same behaviour */ |
| 76 | static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr) |
| 77 | { |
| 78 | #ifdef CONFIG_X86_32 |
| 79 | return 0; |
| 80 | #else |
| 81 | struct dma_mapping_ops *ops = get_dma_ops(dev); |
| 82 | if (ops->mapping_error) |
| 83 | return ops->mapping_error(dev, dma_addr); |
Glauber Costa | c786df0 | 2008-03-25 18:36:37 -0300 | [diff] [blame] | 84 | |
| 85 | return (dma_addr == bad_dma_address); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 86 | #endif |
Glauber Costa | c786df0 | 2008-03-25 18:36:37 -0300 | [diff] [blame] | 87 | } |
| 88 | |
Glauber Costa | 8d396de | 2008-03-25 18:36:31 -0300 | [diff] [blame] | 89 | #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f) |
| 90 | #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h) |
Joerg Roedel | 6c505ce | 2008-08-19 16:32:45 +0200 | [diff] [blame^] | 91 | #define dma_is_consistent(d, h) (1) |
Glauber Costa | 8d396de | 2008-03-25 18:36:31 -0300 | [diff] [blame] | 92 | |
Glauber Costa | 802c1f6 | 2008-03-25 18:36:34 -0300 | [diff] [blame] | 93 | extern int dma_supported(struct device *hwdev, u64 mask); |
| 94 | extern int dma_set_mask(struct device *dev, u64 mask); |
| 95 | |
Glauber Costa | 22456b9 | 2008-03-25 18:36:21 -0300 | [diff] [blame] | 96 | static inline dma_addr_t |
| 97 | dma_map_single(struct device *hwdev, void *ptr, size_t size, |
| 98 | int direction) |
| 99 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 100 | struct dma_mapping_ops *ops = get_dma_ops(hwdev); |
| 101 | |
Glauber Costa | 22456b9 | 2008-03-25 18:36:21 -0300 | [diff] [blame] | 102 | BUG_ON(!valid_dma_direction(direction)); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 103 | return ops->map_single(hwdev, virt_to_phys(ptr), size, direction); |
Glauber Costa | 22456b9 | 2008-03-25 18:36:21 -0300 | [diff] [blame] | 104 | } |
| 105 | |
Glauber Costa | 0cb0ae6 | 2008-03-25 18:36:22 -0300 | [diff] [blame] | 106 | static inline void |
| 107 | dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size, |
| 108 | int direction) |
| 109 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 110 | struct dma_mapping_ops *ops = get_dma_ops(dev); |
| 111 | |
Glauber Costa | 0cb0ae6 | 2008-03-25 18:36:22 -0300 | [diff] [blame] | 112 | BUG_ON(!valid_dma_direction(direction)); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 113 | if (ops->unmap_single) |
| 114 | ops->unmap_single(dev, addr, size, direction); |
Glauber Costa | 0cb0ae6 | 2008-03-25 18:36:22 -0300 | [diff] [blame] | 115 | } |
| 116 | |
Glauber Costa | 16a3ce9 | 2008-03-25 18:36:23 -0300 | [diff] [blame] | 117 | static inline int |
| 118 | dma_map_sg(struct device *hwdev, struct scatterlist *sg, |
| 119 | int nents, int direction) |
| 120 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 121 | struct dma_mapping_ops *ops = get_dma_ops(hwdev); |
| 122 | |
Glauber Costa | 16a3ce9 | 2008-03-25 18:36:23 -0300 | [diff] [blame] | 123 | BUG_ON(!valid_dma_direction(direction)); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 124 | return ops->map_sg(hwdev, sg, nents, direction); |
Glauber Costa | 16a3ce9 | 2008-03-25 18:36:23 -0300 | [diff] [blame] | 125 | } |
Glauber Costa | 72c784f | 2008-03-25 18:36:24 -0300 | [diff] [blame] | 126 | |
| 127 | static inline void |
| 128 | dma_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nents, |
| 129 | int direction) |
| 130 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 131 | struct dma_mapping_ops *ops = get_dma_ops(hwdev); |
| 132 | |
Glauber Costa | 72c784f | 2008-03-25 18:36:24 -0300 | [diff] [blame] | 133 | BUG_ON(!valid_dma_direction(direction)); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 134 | if (ops->unmap_sg) |
| 135 | ops->unmap_sg(hwdev, sg, nents, direction); |
Glauber Costa | 72c784f | 2008-03-25 18:36:24 -0300 | [diff] [blame] | 136 | } |
Glauber Costa | c01dd8c | 2008-03-25 18:36:25 -0300 | [diff] [blame] | 137 | |
| 138 | static inline void |
| 139 | dma_sync_single_for_cpu(struct device *hwdev, dma_addr_t dma_handle, |
| 140 | size_t size, int direction) |
| 141 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 142 | struct dma_mapping_ops *ops = get_dma_ops(hwdev); |
| 143 | |
Glauber Costa | c01dd8c | 2008-03-25 18:36:25 -0300 | [diff] [blame] | 144 | BUG_ON(!valid_dma_direction(direction)); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 145 | if (ops->sync_single_for_cpu) |
| 146 | ops->sync_single_for_cpu(hwdev, dma_handle, size, direction); |
Glauber Costa | c01dd8c | 2008-03-25 18:36:25 -0300 | [diff] [blame] | 147 | flush_write_buffers(); |
| 148 | } |
| 149 | |
Glauber Costa | 9231b26 | 2008-03-25 18:36:26 -0300 | [diff] [blame] | 150 | static inline void |
| 151 | dma_sync_single_for_device(struct device *hwdev, dma_addr_t dma_handle, |
| 152 | size_t size, int direction) |
| 153 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 154 | struct dma_mapping_ops *ops = get_dma_ops(hwdev); |
| 155 | |
Glauber Costa | 9231b26 | 2008-03-25 18:36:26 -0300 | [diff] [blame] | 156 | BUG_ON(!valid_dma_direction(direction)); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 157 | if (ops->sync_single_for_device) |
| 158 | ops->sync_single_for_device(hwdev, dma_handle, size, direction); |
Glauber Costa | 9231b26 | 2008-03-25 18:36:26 -0300 | [diff] [blame] | 159 | flush_write_buffers(); |
| 160 | } |
| 161 | |
Glauber Costa | 627610f | 2008-03-25 18:36:27 -0300 | [diff] [blame] | 162 | static inline void |
| 163 | dma_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dma_handle, |
| 164 | unsigned long offset, size_t size, int direction) |
| 165 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 166 | struct dma_mapping_ops *ops = get_dma_ops(hwdev); |
Glauber Costa | 627610f | 2008-03-25 18:36:27 -0300 | [diff] [blame] | 167 | |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 168 | BUG_ON(!valid_dma_direction(direction)); |
| 169 | if (ops->sync_single_range_for_cpu) |
| 170 | ops->sync_single_range_for_cpu(hwdev, dma_handle, offset, |
| 171 | size, direction); |
Glauber Costa | 627610f | 2008-03-25 18:36:27 -0300 | [diff] [blame] | 172 | flush_write_buffers(); |
| 173 | } |
Glauber Costa | 7136233 | 2008-03-25 18:36:28 -0300 | [diff] [blame] | 174 | |
| 175 | static inline void |
| 176 | dma_sync_single_range_for_device(struct device *hwdev, dma_addr_t dma_handle, |
| 177 | unsigned long offset, size_t size, |
| 178 | int direction) |
| 179 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 180 | struct dma_mapping_ops *ops = get_dma_ops(hwdev); |
Glauber Costa | 7136233 | 2008-03-25 18:36:28 -0300 | [diff] [blame] | 181 | |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 182 | BUG_ON(!valid_dma_direction(direction)); |
| 183 | if (ops->sync_single_range_for_device) |
| 184 | ops->sync_single_range_for_device(hwdev, dma_handle, |
| 185 | offset, size, direction); |
Glauber Costa | 7136233 | 2008-03-25 18:36:28 -0300 | [diff] [blame] | 186 | flush_write_buffers(); |
| 187 | } |
| 188 | |
Glauber Costa | ed435de | 2008-03-25 18:36:29 -0300 | [diff] [blame] | 189 | static inline void |
| 190 | dma_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg, |
| 191 | int nelems, int direction) |
| 192 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 193 | struct dma_mapping_ops *ops = get_dma_ops(hwdev); |
| 194 | |
Glauber Costa | ed435de | 2008-03-25 18:36:29 -0300 | [diff] [blame] | 195 | BUG_ON(!valid_dma_direction(direction)); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 196 | if (ops->sync_sg_for_cpu) |
| 197 | ops->sync_sg_for_cpu(hwdev, sg, nelems, direction); |
Glauber Costa | ed435de | 2008-03-25 18:36:29 -0300 | [diff] [blame] | 198 | flush_write_buffers(); |
| 199 | } |
Glauber Costa | e7f3a91 | 2008-03-25 18:36:30 -0300 | [diff] [blame] | 200 | |
| 201 | static inline void |
| 202 | dma_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg, |
| 203 | int nelems, int direction) |
| 204 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 205 | struct dma_mapping_ops *ops = get_dma_ops(hwdev); |
| 206 | |
Glauber Costa | e7f3a91 | 2008-03-25 18:36:30 -0300 | [diff] [blame] | 207 | BUG_ON(!valid_dma_direction(direction)); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 208 | if (ops->sync_sg_for_device) |
| 209 | ops->sync_sg_for_device(hwdev, sg, nelems, direction); |
Glauber Costa | e7f3a91 | 2008-03-25 18:36:30 -0300 | [diff] [blame] | 210 | |
| 211 | flush_write_buffers(); |
| 212 | } |
Glauber Costa | 4d92fbf | 2008-03-25 18:36:32 -0300 | [diff] [blame] | 213 | |
| 214 | static inline dma_addr_t dma_map_page(struct device *dev, struct page *page, |
| 215 | size_t offset, size_t size, |
| 216 | int direction) |
| 217 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 218 | struct dma_mapping_ops *ops = get_dma_ops(dev); |
| 219 | |
Ingo Molnar | 2be6214 | 2008-04-19 19:19:56 +0200 | [diff] [blame] | 220 | BUG_ON(!valid_dma_direction(direction)); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 221 | return ops->map_single(dev, page_to_phys(page) + offset, |
| 222 | size, direction); |
Glauber Costa | 4d92fbf | 2008-03-25 18:36:32 -0300 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static inline void dma_unmap_page(struct device *dev, dma_addr_t addr, |
| 226 | size_t size, int direction) |
| 227 | { |
| 228 | dma_unmap_single(dev, addr, size, direction); |
| 229 | } |
| 230 | |
Glauber Costa | 3cb6a91 | 2008-03-25 18:36:33 -0300 | [diff] [blame] | 231 | static inline void |
| 232 | dma_cache_sync(struct device *dev, void *vaddr, size_t size, |
| 233 | enum dma_data_direction dir) |
| 234 | { |
| 235 | flush_write_buffers(); |
| 236 | } |
Glauber Costa | ae17a63b | 2008-03-25 18:36:38 -0300 | [diff] [blame] | 237 | |
Glauber Costa | b7107a3 | 2008-03-25 18:36:39 -0300 | [diff] [blame] | 238 | static inline int dma_get_cache_alignment(void) |
| 239 | { |
| 240 | /* no easy way to get cache size on all x86, so return the |
| 241 | * maximum possible, to be safe */ |
| 242 | return boot_cpu_data.x86_clflush_size; |
| 243 | } |
| 244 | |
Joerg Roedel | 6c505ce | 2008-08-19 16:32:45 +0200 | [diff] [blame^] | 245 | static inline void * |
| 246 | dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, |
| 247 | gfp_t gfp) |
| 248 | { |
| 249 | struct dma_mapping_ops *ops = get_dma_ops(dev); |
| 250 | void *memory; |
Glauber Costa | b7107a3 | 2008-03-25 18:36:39 -0300 | [diff] [blame] | 251 | |
Joerg Roedel | 6c505ce | 2008-08-19 16:32:45 +0200 | [diff] [blame^] | 252 | if (dma_alloc_from_coherent(dev, size, dma_handle, &memory)) |
| 253 | return memory; |
| 254 | |
| 255 | if (!dev) { |
| 256 | dev = &x86_dma_fallback_dev; |
| 257 | gfp |= GFP_DMA; |
| 258 | } |
| 259 | |
| 260 | if (ops->alloc_coherent) |
| 261 | return ops->alloc_coherent(dev, size, |
| 262 | dma_handle, gfp); |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | static inline void dma_free_coherent(struct device *dev, size_t size, |
| 267 | void *vaddr, dma_addr_t bus) |
| 268 | { |
| 269 | struct dma_mapping_ops *ops = get_dma_ops(dev); |
| 270 | |
| 271 | WARN_ON(irqs_disabled()); /* for portability */ |
| 272 | |
| 273 | if (dma_release_from_coherent(dev, get_order(size), vaddr)) |
| 274 | return; |
| 275 | |
| 276 | if (ops->free_coherent) |
| 277 | ops->free_coherent(dev, size, vaddr, bus); |
| 278 | } |
| 279 | |
Glauber Costa | 6f53663 | 2008-03-25 18:36:20 -0300 | [diff] [blame] | 280 | #endif |