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