Robert P. J. Day | 96532ba | 2008-02-03 15:06:26 +0200 | [diff] [blame] | 1 | #ifndef _LINUX_DMA_MAPPING_H |
| 2 | #define _LINUX_DMA_MAPPING_H |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | |
| 4 | #include <linux/device.h> |
| 5 | #include <linux/err.h> |
| 6 | |
| 7 | /* These definitions mirror those in pci.h, so they can be used |
| 8 | * interchangeably with their PCI_ counterparts */ |
| 9 | enum dma_data_direction { |
| 10 | DMA_BIDIRECTIONAL = 0, |
| 11 | DMA_TO_DEVICE = 1, |
| 12 | DMA_FROM_DEVICE = 2, |
| 13 | DMA_NONE = 3, |
| 14 | }; |
| 15 | |
Andrew Morton | 8f286c3 | 2007-10-18 03:05:07 -0700 | [diff] [blame] | 16 | #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1)) |
Borislav Petkov | 34c6538 | 2007-10-18 03:05:06 -0700 | [diff] [blame] | 17 | |
Andrew Morton | 8f286c3 | 2007-10-18 03:05:07 -0700 | [diff] [blame] | 18 | /* |
| 19 | * NOTE: do not use the below macros in new code and do not add new definitions |
| 20 | * here. |
| 21 | * |
| 22 | * Instead, just open-code DMA_BIT_MASK(n) within your driver |
| 23 | */ |
| 24 | #define DMA_64BIT_MASK DMA_BIT_MASK(64) |
Borislav Petkov | 34c6538 | 2007-10-18 03:05:06 -0700 | [diff] [blame] | 25 | #define DMA_48BIT_MASK DMA_BIT_MASK(48) |
| 26 | #define DMA_47BIT_MASK DMA_BIT_MASK(47) |
| 27 | #define DMA_40BIT_MASK DMA_BIT_MASK(40) |
| 28 | #define DMA_39BIT_MASK DMA_BIT_MASK(39) |
| 29 | #define DMA_35BIT_MASK DMA_BIT_MASK(35) |
| 30 | #define DMA_32BIT_MASK DMA_BIT_MASK(32) |
| 31 | #define DMA_31BIT_MASK DMA_BIT_MASK(31) |
| 32 | #define DMA_30BIT_MASK DMA_BIT_MASK(30) |
| 33 | #define DMA_29BIT_MASK DMA_BIT_MASK(29) |
| 34 | #define DMA_28BIT_MASK DMA_BIT_MASK(28) |
| 35 | #define DMA_24BIT_MASK DMA_BIT_MASK(24) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
James Bottomley | 32e8f70 | 2007-10-16 01:23:55 -0700 | [diff] [blame] | 37 | #define DMA_MASK_NONE 0x0ULL |
| 38 | |
Rolf Eike Beer | d6bd3a3 | 2006-09-29 01:59:48 -0700 | [diff] [blame] | 39 | static inline int valid_dma_direction(int dma_direction) |
| 40 | { |
| 41 | return ((dma_direction == DMA_BIDIRECTIONAL) || |
| 42 | (dma_direction == DMA_TO_DEVICE) || |
| 43 | (dma_direction == DMA_FROM_DEVICE)); |
| 44 | } |
| 45 | |
James Bottomley | 32e8f70 | 2007-10-16 01:23:55 -0700 | [diff] [blame] | 46 | static inline int is_device_dma_capable(struct device *dev) |
| 47 | { |
| 48 | return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE; |
| 49 | } |
| 50 | |
FUJITA Tomonori | 636dc67 | 2008-09-10 01:06:46 +0900 | [diff] [blame] | 51 | static inline int is_buffer_dma_capable(u64 mask, dma_addr_t addr, size_t size) |
| 52 | { |
| 53 | return addr + size <= mask; |
| 54 | } |
| 55 | |
Dan Williams | 1b0fac4 | 2007-07-15 23:40:26 -0700 | [diff] [blame] | 56 | #ifdef CONFIG_HAS_DMA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include <asm/dma-mapping.h> |
Dan Williams | 1b0fac4 | 2007-07-15 23:40:26 -0700 | [diff] [blame] | 58 | #else |
| 59 | #include <asm-generic/dma-mapping-broken.h> |
| 60 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* Backwards compat, remove in 2.7.x */ |
| 63 | #define dma_sync_single dma_sync_single_for_cpu |
| 64 | #define dma_sync_sg dma_sync_sg_for_cpu |
| 65 | |
FUJITA Tomonori | 589fc9a | 2008-09-12 19:42:34 +0900 | [diff] [blame] | 66 | static inline u64 dma_get_mask(struct device *dev) |
| 67 | { |
FUJITA Tomonori | 07a2c01 | 2008-09-19 02:02:05 +0900 | [diff] [blame] | 68 | if (dev && dev->dma_mask && *dev->dma_mask) |
FUJITA Tomonori | 589fc9a | 2008-09-12 19:42:34 +0900 | [diff] [blame] | 69 | return *dev->dma_mask; |
| 70 | return DMA_32BIT_MASK; |
| 71 | } |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | extern u64 dma_get_required_mask(struct device *dev); |
| 74 | |
FUJITA Tomonori | 6b7b651 | 2008-02-04 22:27:55 -0800 | [diff] [blame] | 75 | static inline unsigned int dma_get_max_seg_size(struct device *dev) |
| 76 | { |
| 77 | return dev->dma_parms ? dev->dma_parms->max_segment_size : 65536; |
| 78 | } |
| 79 | |
| 80 | static inline unsigned int dma_set_max_seg_size(struct device *dev, |
| 81 | unsigned int size) |
| 82 | { |
| 83 | if (dev->dma_parms) { |
| 84 | dev->dma_parms->max_segment_size = size; |
| 85 | return 0; |
| 86 | } else |
| 87 | return -EIO; |
| 88 | } |
| 89 | |
FUJITA Tomonori | d22a696 | 2008-02-04 22:28:13 -0800 | [diff] [blame] | 90 | static inline unsigned long dma_get_seg_boundary(struct device *dev) |
| 91 | { |
| 92 | return dev->dma_parms ? |
| 93 | dev->dma_parms->segment_boundary_mask : 0xffffffff; |
| 94 | } |
| 95 | |
| 96 | static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask) |
| 97 | { |
| 98 | if (dev->dma_parms) { |
| 99 | dev->dma_parms->segment_boundary_mask = mask; |
| 100 | return 0; |
| 101 | } else |
| 102 | return -EIO; |
| 103 | } |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | /* flags for the coherent memory api */ |
| 106 | #define DMA_MEMORY_MAP 0x01 |
| 107 | #define DMA_MEMORY_IO 0x02 |
| 108 | #define DMA_MEMORY_INCLUDES_CHILDREN 0x04 |
| 109 | #define DMA_MEMORY_EXCLUSIVE 0x08 |
| 110 | |
| 111 | #ifndef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY |
| 112 | static inline int |
| 113 | dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr, |
| 114 | dma_addr_t device_addr, size_t size, int flags) |
| 115 | { |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static inline void |
| 120 | dma_release_declared_memory(struct device *dev) |
| 121 | { |
| 122 | } |
| 123 | |
| 124 | static inline void * |
| 125 | dma_mark_declared_memory_occupied(struct device *dev, |
| 126 | dma_addr_t device_addr, size_t size) |
| 127 | { |
| 128 | return ERR_PTR(-EBUSY); |
| 129 | } |
| 130 | #endif |
| 131 | |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 132 | /* |
| 133 | * Managed DMA API |
| 134 | */ |
| 135 | extern void *dmam_alloc_coherent(struct device *dev, size_t size, |
| 136 | dma_addr_t *dma_handle, gfp_t gfp); |
| 137 | extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr, |
| 138 | dma_addr_t dma_handle); |
| 139 | extern void *dmam_alloc_noncoherent(struct device *dev, size_t size, |
| 140 | dma_addr_t *dma_handle, gfp_t gfp); |
| 141 | extern void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr, |
| 142 | dma_addr_t dma_handle); |
| 143 | #ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY |
| 144 | extern int dmam_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr, |
| 145 | dma_addr_t device_addr, size_t size, |
| 146 | int flags); |
| 147 | extern void dmam_release_declared_memory(struct device *dev); |
| 148 | #else /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */ |
| 149 | static inline int dmam_declare_coherent_memory(struct device *dev, |
| 150 | dma_addr_t bus_addr, dma_addr_t device_addr, |
| 151 | size_t size, gfp_t gfp) |
| 152 | { |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static inline void dmam_release_declared_memory(struct device *dev) |
| 157 | { |
| 158 | } |
| 159 | #endif /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */ |
| 160 | |
Arthur Kepner | 74bc7ce | 2008-04-29 01:00:30 -0700 | [diff] [blame] | 161 | #ifndef CONFIG_HAVE_DMA_ATTRS |
| 162 | struct dma_attrs; |
| 163 | |
| 164 | #define dma_map_single_attrs(dev, cpu_addr, size, dir, attrs) \ |
| 165 | dma_map_single(dev, cpu_addr, size, dir) |
| 166 | |
| 167 | #define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \ |
| 168 | dma_unmap_single(dev, dma_addr, size, dir) |
| 169 | |
| 170 | #define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \ |
| 171 | dma_map_sg(dev, sgl, nents, dir) |
| 172 | |
| 173 | #define dma_unmap_sg_attrs(dev, sgl, nents, dir, attrs) \ |
| 174 | dma_unmap_sg(dev, sgl, nents, dir) |
| 175 | |
| 176 | #endif /* CONFIG_HAVE_DMA_ATTRS */ |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | #endif |