Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _X8664_DMA_MAPPING_H |
| 2 | #define _X8664_DMA_MAPPING_H 1 |
| 3 | |
| 4 | /* |
| 5 | * IOMMU interface. See Documentation/DMA-mapping.txt and DMA-API.txt for |
| 6 | * documentation. |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
| 10 | #include <asm/scatterlist.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <asm/swiotlb.h> |
| 13 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 14 | struct dma_mapping_ops { |
| 15 | int (*mapping_error)(dma_addr_t dma_addr); |
| 16 | void* (*alloc_coherent)(struct device *dev, size_t size, |
| 17 | dma_addr_t *dma_handle, gfp_t gfp); |
| 18 | void (*free_coherent)(struct device *dev, size_t size, |
| 19 | void *vaddr, dma_addr_t dma_handle); |
| 20 | dma_addr_t (*map_single)(struct device *hwdev, void *ptr, |
| 21 | size_t size, int direction); |
| 22 | /* like map_single, but doesn't check the device mask */ |
| 23 | dma_addr_t (*map_simple)(struct device *hwdev, char *ptr, |
| 24 | size_t size, int direction); |
| 25 | void (*unmap_single)(struct device *dev, dma_addr_t addr, |
| 26 | size_t size, int direction); |
| 27 | void (*sync_single_for_cpu)(struct device *hwdev, |
| 28 | dma_addr_t dma_handle, size_t size, |
| 29 | int direction); |
| 30 | void (*sync_single_for_device)(struct device *hwdev, |
| 31 | dma_addr_t dma_handle, size_t size, |
| 32 | int direction); |
| 33 | void (*sync_single_range_for_cpu)(struct device *hwdev, |
| 34 | dma_addr_t dma_handle, unsigned long offset, |
| 35 | size_t size, int direction); |
| 36 | void (*sync_single_range_for_device)(struct device *hwdev, |
| 37 | dma_addr_t dma_handle, unsigned long offset, |
| 38 | size_t size, int direction); |
| 39 | void (*sync_sg_for_cpu)(struct device *hwdev, |
| 40 | struct scatterlist *sg, int nelems, |
| 41 | int direction); |
| 42 | void (*sync_sg_for_device)(struct device *hwdev, |
| 43 | struct scatterlist *sg, int nelems, |
| 44 | int direction); |
| 45 | int (*map_sg)(struct device *hwdev, struct scatterlist *sg, |
| 46 | int nents, int direction); |
| 47 | void (*unmap_sg)(struct device *hwdev, |
| 48 | struct scatterlist *sg, int nents, |
| 49 | int direction); |
| 50 | int (*dma_supported)(struct device *hwdev, u64 mask); |
| 51 | int is_phys; |
| 52 | }; |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | extern dma_addr_t bad_dma_address; |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 55 | extern struct dma_mapping_ops* dma_ops; |
| 56 | extern int iommu_merge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Jon Mason | a3c042a | 2006-06-26 13:58:08 +0200 | [diff] [blame] | 58 | static inline int valid_dma_direction(int dma_direction) |
| 59 | { |
| 60 | return ((dma_direction == DMA_BIDIRECTIONAL) || |
| 61 | (dma_direction == DMA_TO_DEVICE) || |
| 62 | (dma_direction == DMA_FROM_DEVICE)); |
| 63 | } |
| 64 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 65 | static inline int dma_mapping_error(dma_addr_t dma_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 67 | if (dma_ops->mapping_error) |
| 68 | return dma_ops->mapping_error(dma_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 70 | return (dma_addr == bad_dma_address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 73 | extern void *dma_alloc_coherent(struct device *dev, size_t size, |
| 74 | dma_addr_t *dma_handle, gfp_t gfp); |
| 75 | extern void dma_free_coherent(struct device *dev, size_t size, void *vaddr, |
| 76 | dma_addr_t dma_handle); |
| 77 | |
| 78 | static inline dma_addr_t |
| 79 | dma_map_single(struct device *hwdev, void *ptr, size_t size, |
| 80 | int direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Jon Mason | a3c042a | 2006-06-26 13:58:08 +0200 | [diff] [blame] | 82 | BUG_ON(!valid_dma_direction(direction)); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 83 | return dma_ops->map_single(hwdev, ptr, size, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 86 | static inline void |
| 87 | dma_unmap_single(struct device *dev, dma_addr_t addr,size_t size, |
| 88 | int direction) |
| 89 | { |
Jon Mason | a3c042a | 2006-06-26 13:58:08 +0200 | [diff] [blame] | 90 | BUG_ON(!valid_dma_direction(direction)); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 91 | dma_ops->unmap_single(dev, addr, size, direction); |
| 92 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | #define dma_map_page(dev,page,offset,size,dir) \ |
| 95 | dma_map_single((dev), page_address(page)+(offset), (size), (dir)) |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | #define dma_unmap_page dma_unmap_single |
| 98 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 99 | static inline void |
| 100 | dma_sync_single_for_cpu(struct device *hwdev, dma_addr_t dma_handle, |
| 101 | size_t size, int direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
Jon Mason | a3c042a | 2006-06-26 13:58:08 +0200 | [diff] [blame] | 103 | BUG_ON(!valid_dma_direction(direction)); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 104 | if (dma_ops->sync_single_for_cpu) |
| 105 | dma_ops->sync_single_for_cpu(hwdev, dma_handle, size, |
| 106 | direction); |
| 107 | flush_write_buffers(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 110 | static inline void |
| 111 | dma_sync_single_for_device(struct device *hwdev, dma_addr_t dma_handle, |
| 112 | size_t size, int direction) |
| 113 | { |
Jon Mason | a3c042a | 2006-06-26 13:58:08 +0200 | [diff] [blame] | 114 | BUG_ON(!valid_dma_direction(direction)); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 115 | if (dma_ops->sync_single_for_device) |
| 116 | dma_ops->sync_single_for_device(hwdev, dma_handle, size, |
| 117 | direction); |
| 118 | flush_write_buffers(); |
| 119 | } |
| 120 | |
| 121 | static inline void |
| 122 | dma_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dma_handle, |
| 123 | unsigned long offset, size_t size, int direction) |
| 124 | { |
Jon Mason | a3c042a | 2006-06-26 13:58:08 +0200 | [diff] [blame] | 125 | BUG_ON(!valid_dma_direction(direction)); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 126 | if (dma_ops->sync_single_range_for_cpu) { |
| 127 | dma_ops->sync_single_range_for_cpu(hwdev, dma_handle, offset, size, direction); |
| 128 | } |
| 129 | |
| 130 | flush_write_buffers(); |
| 131 | } |
| 132 | |
| 133 | static inline void |
| 134 | dma_sync_single_range_for_device(struct device *hwdev, dma_addr_t dma_handle, |
| 135 | unsigned long offset, size_t size, int direction) |
| 136 | { |
Jon Mason | a3c042a | 2006-06-26 13:58:08 +0200 | [diff] [blame] | 137 | BUG_ON(!valid_dma_direction(direction)); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 138 | if (dma_ops->sync_single_range_for_device) |
| 139 | dma_ops->sync_single_range_for_device(hwdev, dma_handle, |
| 140 | offset, size, direction); |
| 141 | |
| 142 | flush_write_buffers(); |
| 143 | } |
| 144 | |
| 145 | static inline void |
| 146 | dma_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg, |
| 147 | int nelems, int direction) |
| 148 | { |
Jon Mason | a3c042a | 2006-06-26 13:58:08 +0200 | [diff] [blame] | 149 | BUG_ON(!valid_dma_direction(direction)); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 150 | if (dma_ops->sync_sg_for_cpu) |
| 151 | dma_ops->sync_sg_for_cpu(hwdev, sg, nelems, direction); |
| 152 | flush_write_buffers(); |
| 153 | } |
| 154 | |
| 155 | static inline void |
| 156 | dma_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg, |
| 157 | int nelems, int direction) |
| 158 | { |
Jon Mason | a3c042a | 2006-06-26 13:58:08 +0200 | [diff] [blame] | 159 | BUG_ON(!valid_dma_direction(direction)); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 160 | if (dma_ops->sync_sg_for_device) { |
| 161 | dma_ops->sync_sg_for_device(hwdev, sg, nelems, direction); |
| 162 | } |
| 163 | |
| 164 | flush_write_buffers(); |
| 165 | } |
| 166 | |
| 167 | static inline int |
| 168 | dma_map_sg(struct device *hwdev, struct scatterlist *sg, int nents, int direction) |
| 169 | { |
Jon Mason | a3c042a | 2006-06-26 13:58:08 +0200 | [diff] [blame] | 170 | BUG_ON(!valid_dma_direction(direction)); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 171 | return dma_ops->map_sg(hwdev, sg, nents, direction); |
| 172 | } |
| 173 | |
| 174 | static inline void |
| 175 | dma_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nents, |
| 176 | int direction) |
| 177 | { |
Jon Mason | a3c042a | 2006-06-26 13:58:08 +0200 | [diff] [blame] | 178 | BUG_ON(!valid_dma_direction(direction)); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 179 | dma_ops->unmap_sg(hwdev, sg, nents, direction); |
| 180 | } |
| 181 | |
| 182 | extern int dma_supported(struct device *hwdev, u64 mask); |
| 183 | |
| 184 | /* same for gart, swiotlb, and nommu */ |
| 185 | static inline int dma_get_cache_alignment(void) |
| 186 | { |
| 187 | return boot_cpu_data.x86_clflush_size; |
| 188 | } |
| 189 | |
| 190 | #define dma_is_consistent(h) 1 |
| 191 | |
| 192 | extern int dma_set_mask(struct device *dev, u64 mask); |
| 193 | |
| 194 | static inline void |
| 195 | dma_cache_sync(void *vaddr, size_t size, enum dma_data_direction dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
| 197 | flush_write_buffers(); |
| 198 | } |
| 199 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 200 | extern struct device fallback_dev; |
| 201 | extern int panic_on_overflow; |
| 202 | |
| 203 | #endif /* _X8664_DMA_MAPPING_H */ |