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