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> |
| 12 | |
Glauber Costa | 7c18341 | 2008-03-25 18:36:36 -0300 | [diff] [blame^] | 13 | extern dma_addr_t bad_dma_address; |
| 14 | |
Glauber Costa | 6f53663 | 2008-03-25 18:36:20 -0300 | [diff] [blame] | 15 | struct dma_mapping_ops { |
| 16 | int (*mapping_error)(dma_addr_t dma_addr); |
| 17 | void* (*alloc_coherent)(struct device *dev, size_t size, |
| 18 | dma_addr_t *dma_handle, gfp_t gfp); |
| 19 | void (*free_coherent)(struct device *dev, size_t size, |
| 20 | void *vaddr, dma_addr_t dma_handle); |
Ingo Molnar | 2be6214 | 2008-04-19 19:19:56 +0200 | [diff] [blame] | 21 | dma_addr_t (*map_single)(struct device *hwdev, phys_addr_t ptr, |
Glauber Costa | 6f53663 | 2008-03-25 18:36:20 -0300 | [diff] [blame] | 22 | size_t size, int direction); |
| 23 | /* like map_single, but doesn't check the device mask */ |
Ingo Molnar | 2be6214 | 2008-04-19 19:19:56 +0200 | [diff] [blame] | 24 | dma_addr_t (*map_simple)(struct device *hwdev, phys_addr_t ptr, |
Glauber Costa | 6f53663 | 2008-03-25 18:36:20 -0300 | [diff] [blame] | 25 | size_t size, int direction); |
| 26 | void (*unmap_single)(struct device *dev, dma_addr_t addr, |
| 27 | size_t size, int direction); |
| 28 | void (*sync_single_for_cpu)(struct device *hwdev, |
| 29 | dma_addr_t dma_handle, size_t size, |
| 30 | int direction); |
| 31 | void (*sync_single_for_device)(struct device *hwdev, |
| 32 | dma_addr_t dma_handle, size_t size, |
| 33 | int direction); |
| 34 | void (*sync_single_range_for_cpu)(struct device *hwdev, |
| 35 | dma_addr_t dma_handle, unsigned long offset, |
| 36 | size_t size, int direction); |
| 37 | void (*sync_single_range_for_device)(struct device *hwdev, |
| 38 | dma_addr_t dma_handle, unsigned long offset, |
| 39 | size_t size, int direction); |
| 40 | void (*sync_sg_for_cpu)(struct device *hwdev, |
| 41 | struct scatterlist *sg, int nelems, |
| 42 | int direction); |
| 43 | void (*sync_sg_for_device)(struct device *hwdev, |
| 44 | struct scatterlist *sg, int nelems, |
| 45 | int direction); |
| 46 | int (*map_sg)(struct device *hwdev, struct scatterlist *sg, |
| 47 | int nents, int direction); |
| 48 | void (*unmap_sg)(struct device *hwdev, |
| 49 | struct scatterlist *sg, int nents, |
| 50 | int direction); |
| 51 | int (*dma_supported)(struct device *hwdev, u64 mask); |
| 52 | int is_phys; |
| 53 | }; |
| 54 | |
Glauber Costa | 22456b9 | 2008-03-25 18:36:21 -0300 | [diff] [blame] | 55 | extern const struct dma_mapping_ops *dma_ops; |
| 56 | |
Glauber Costa | 8d396de | 2008-03-25 18:36:31 -0300 | [diff] [blame] | 57 | #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f) |
| 58 | #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h) |
| 59 | |
| 60 | void *dma_alloc_coherent(struct device *dev, size_t size, |
| 61 | dma_addr_t *dma_handle, gfp_t flag); |
| 62 | |
| 63 | void dma_free_coherent(struct device *dev, size_t size, |
| 64 | void *vaddr, dma_addr_t dma_handle); |
| 65 | |
| 66 | |
Glauber Costa | 802c1f6 | 2008-03-25 18:36:34 -0300 | [diff] [blame] | 67 | extern int dma_supported(struct device *hwdev, u64 mask); |
| 68 | extern int dma_set_mask(struct device *dev, u64 mask); |
| 69 | |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 70 | #ifdef CONFIG_X86_32 |
| 71 | # include "dma-mapping_32.h" |
| 72 | #else |
| 73 | # include "dma-mapping_64.h" |
| 74 | #endif |
Glauber Costa | 6f53663 | 2008-03-25 18:36:20 -0300 | [diff] [blame] | 75 | |
Glauber Costa | 22456b9 | 2008-03-25 18:36:21 -0300 | [diff] [blame] | 76 | static inline dma_addr_t |
| 77 | dma_map_single(struct device *hwdev, void *ptr, size_t size, |
| 78 | int direction) |
| 79 | { |
| 80 | BUG_ON(!valid_dma_direction(direction)); |
Ingo Molnar | 2be6214 | 2008-04-19 19:19:56 +0200 | [diff] [blame] | 81 | return dma_ops->map_single(hwdev, virt_to_phys(ptr), size, direction); |
Glauber Costa | 22456b9 | 2008-03-25 18:36:21 -0300 | [diff] [blame] | 82 | } |
| 83 | |
Glauber Costa | 0cb0ae6 | 2008-03-25 18:36:22 -0300 | [diff] [blame] | 84 | static inline void |
| 85 | dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size, |
| 86 | int direction) |
| 87 | { |
| 88 | BUG_ON(!valid_dma_direction(direction)); |
| 89 | if (dma_ops->unmap_single) |
| 90 | dma_ops->unmap_single(dev, addr, size, direction); |
| 91 | } |
| 92 | |
Glauber Costa | 16a3ce9 | 2008-03-25 18:36:23 -0300 | [diff] [blame] | 93 | static inline int |
| 94 | dma_map_sg(struct device *hwdev, struct scatterlist *sg, |
| 95 | int nents, int direction) |
| 96 | { |
| 97 | BUG_ON(!valid_dma_direction(direction)); |
| 98 | return dma_ops->map_sg(hwdev, sg, nents, direction); |
| 99 | } |
Glauber Costa | 72c784f | 2008-03-25 18:36:24 -0300 | [diff] [blame] | 100 | |
| 101 | static inline void |
| 102 | dma_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nents, |
| 103 | int direction) |
| 104 | { |
| 105 | BUG_ON(!valid_dma_direction(direction)); |
| 106 | if (dma_ops->unmap_sg) |
| 107 | dma_ops->unmap_sg(hwdev, sg, nents, direction); |
| 108 | } |
Glauber Costa | c01dd8c | 2008-03-25 18:36:25 -0300 | [diff] [blame] | 109 | |
| 110 | static inline void |
| 111 | dma_sync_single_for_cpu(struct device *hwdev, dma_addr_t dma_handle, |
| 112 | size_t size, int direction) |
| 113 | { |
| 114 | BUG_ON(!valid_dma_direction(direction)); |
| 115 | if (dma_ops->sync_single_for_cpu) |
| 116 | dma_ops->sync_single_for_cpu(hwdev, dma_handle, size, |
| 117 | direction); |
| 118 | flush_write_buffers(); |
| 119 | } |
| 120 | |
Glauber Costa | 9231b26 | 2008-03-25 18:36:26 -0300 | [diff] [blame] | 121 | static inline void |
| 122 | dma_sync_single_for_device(struct device *hwdev, dma_addr_t dma_handle, |
| 123 | size_t size, int direction) |
| 124 | { |
| 125 | BUG_ON(!valid_dma_direction(direction)); |
| 126 | if (dma_ops->sync_single_for_device) |
| 127 | dma_ops->sync_single_for_device(hwdev, dma_handle, size, |
| 128 | direction); |
| 129 | flush_write_buffers(); |
| 130 | } |
| 131 | |
Glauber Costa | 627610f | 2008-03-25 18:36:27 -0300 | [diff] [blame] | 132 | static inline void |
| 133 | dma_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dma_handle, |
| 134 | unsigned long offset, size_t size, int direction) |
| 135 | { |
| 136 | BUG_ON(!valid_dma_direction(direction)); |
| 137 | if (dma_ops->sync_single_range_for_cpu) |
| 138 | dma_ops->sync_single_range_for_cpu(hwdev, dma_handle, offset, |
| 139 | size, direction); |
| 140 | |
| 141 | flush_write_buffers(); |
| 142 | } |
Glauber Costa | 7136233 | 2008-03-25 18:36:28 -0300 | [diff] [blame] | 143 | |
| 144 | static inline void |
| 145 | dma_sync_single_range_for_device(struct device *hwdev, dma_addr_t dma_handle, |
| 146 | unsigned long offset, size_t size, |
| 147 | int direction) |
| 148 | { |
| 149 | BUG_ON(!valid_dma_direction(direction)); |
| 150 | if (dma_ops->sync_single_range_for_device) |
| 151 | dma_ops->sync_single_range_for_device(hwdev, dma_handle, |
| 152 | offset, size, direction); |
| 153 | |
| 154 | flush_write_buffers(); |
| 155 | } |
| 156 | |
Glauber Costa | ed435de | 2008-03-25 18:36:29 -0300 | [diff] [blame] | 157 | static inline void |
| 158 | dma_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg, |
| 159 | int nelems, int direction) |
| 160 | { |
| 161 | BUG_ON(!valid_dma_direction(direction)); |
| 162 | if (dma_ops->sync_sg_for_cpu) |
| 163 | dma_ops->sync_sg_for_cpu(hwdev, sg, nelems, direction); |
| 164 | flush_write_buffers(); |
| 165 | } |
Glauber Costa | e7f3a91 | 2008-03-25 18:36:30 -0300 | [diff] [blame] | 166 | |
| 167 | static inline void |
| 168 | dma_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg, |
| 169 | int nelems, int direction) |
| 170 | { |
| 171 | BUG_ON(!valid_dma_direction(direction)); |
| 172 | if (dma_ops->sync_sg_for_device) |
| 173 | dma_ops->sync_sg_for_device(hwdev, sg, nelems, direction); |
| 174 | |
| 175 | flush_write_buffers(); |
| 176 | } |
Glauber Costa | 4d92fbf | 2008-03-25 18:36:32 -0300 | [diff] [blame] | 177 | |
| 178 | static inline dma_addr_t dma_map_page(struct device *dev, struct page *page, |
| 179 | size_t offset, size_t size, |
| 180 | int direction) |
| 181 | { |
Ingo Molnar | 2be6214 | 2008-04-19 19:19:56 +0200 | [diff] [blame] | 182 | BUG_ON(!valid_dma_direction(direction)); |
| 183 | return dma_ops->map_single(dev, page_to_phys(page)+offset, |
| 184 | size, direction); |
Glauber Costa | 4d92fbf | 2008-03-25 18:36:32 -0300 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static inline void dma_unmap_page(struct device *dev, dma_addr_t addr, |
| 188 | size_t size, int direction) |
| 189 | { |
| 190 | dma_unmap_single(dev, addr, size, direction); |
| 191 | } |
| 192 | |
Glauber Costa | 3cb6a91 | 2008-03-25 18:36:33 -0300 | [diff] [blame] | 193 | static inline void |
| 194 | dma_cache_sync(struct device *dev, void *vaddr, size_t size, |
| 195 | enum dma_data_direction dir) |
| 196 | { |
| 197 | flush_write_buffers(); |
| 198 | } |
Glauber Costa | 6f53663 | 2008-03-25 18:36:20 -0300 | [diff] [blame] | 199 | #endif |