diff options
| author | Marek Szyprowski <m.szyprowski@samsung.com> | 2012-05-16 12:31:23 -0400 |
|---|---|---|
| committer | Marek Szyprowski <m.szyprowski@samsung.com> | 2012-05-21 09:06:22 -0400 |
| commit | f99d60341238fe73fc514129cd9ae4e44e1b2c47 (patch) | |
| tree | 4e3214b9cdfbcddd9243f14161c77956eb3f5791 | |
| parent | 51fde3499b531d4cf278f4d2eaa6c45b2865b16b (diff) | |
ARM: dma-mapping: use alloc, mmap, free from dma_ops
This patch converts dma_alloc/free/mmap_{coherent,writecombine}
functions to use generic alloc/free/mmap methods from dma_map_ops
structure. A new DMA_ATTR_WRITE_COMBINE DMA attribute have been
introduced to implement writecombine methods.
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Acked-by: Kyungmin Park <kyungmin.park@samsung.com>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Tested-By: Subash Patel <subash.ramaswamy@linaro.org>
| -rw-r--r-- | arch/arm/common/dmabounce.c | 3 | ||||
| -rw-r--r-- | arch/arm/include/asm/dma-mapping.h | 107 | ||||
| -rw-r--r-- | arch/arm/mm/dma-mapping.c | 60 |
3 files changed, 104 insertions, 66 deletions
diff --git a/arch/arm/common/dmabounce.c b/arch/arm/common/dmabounce.c index 813c29dc6613..9d7eb530f95f 100644 --- a/arch/arm/common/dmabounce.c +++ b/arch/arm/common/dmabounce.c | |||
| @@ -449,6 +449,9 @@ static int dmabounce_set_mask(struct device *dev, u64 dma_mask) | |||
| 449 | } | 449 | } |
| 450 | 450 | ||
| 451 | static struct dma_map_ops dmabounce_ops = { | 451 | static struct dma_map_ops dmabounce_ops = { |
| 452 | .alloc = arm_dma_alloc, | ||
| 453 | .free = arm_dma_free, | ||
| 454 | .mmap = arm_dma_mmap, | ||
| 452 | .map_page = dmabounce_map_page, | 455 | .map_page = dmabounce_map_page, |
| 453 | .unmap_page = dmabounce_unmap_page, | 456 | .unmap_page = dmabounce_unmap_page, |
| 454 | .sync_single_for_cpu = dmabounce_sync_for_cpu, | 457 | .sync_single_for_cpu = dmabounce_sync_for_cpu, |
diff --git a/arch/arm/include/asm/dma-mapping.h b/arch/arm/include/asm/dma-mapping.h index 7a7c3c762f5f..bbef15d04890 100644 --- a/arch/arm/include/asm/dma-mapping.h +++ b/arch/arm/include/asm/dma-mapping.h | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | #include <linux/mm_types.h> | 6 | #include <linux/mm_types.h> |
| 7 | #include <linux/scatterlist.h> | 7 | #include <linux/scatterlist.h> |
| 8 | #include <linux/dma-attrs.h> | ||
| 8 | #include <linux/dma-debug.h> | 9 | #include <linux/dma-debug.h> |
| 9 | 10 | ||
| 10 | #include <asm-generic/dma-coherent.h> | 11 | #include <asm-generic/dma-coherent.h> |
| @@ -110,68 +111,115 @@ static inline void dma_free_noncoherent(struct device *dev, size_t size, | |||
| 110 | extern int dma_supported(struct device *dev, u64 mask); | 111 | extern int dma_supported(struct device *dev, u64 mask); |
| 111 | 112 | ||
| 112 | /** | 113 | /** |
| 113 | * dma_alloc_coherent - allocate consistent memory for DMA | 114 | * arm_dma_alloc - allocate consistent memory for DMA |
| 114 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices | 115 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 115 | * @size: required memory size | 116 | * @size: required memory size |
| 116 | * @handle: bus-specific DMA address | 117 | * @handle: bus-specific DMA address |
| 118 | * @attrs: optinal attributes that specific mapping properties | ||
| 117 | * | 119 | * |
| 118 | * Allocate some uncached, unbuffered memory for a device for | 120 | * Allocate some memory for a device for performing DMA. This function |
| 119 | * performing DMA. This function allocates pages, and will | 121 | * allocates pages, and will return the CPU-viewed address, and sets @handle |
| 120 | * return the CPU-viewed address, and sets @handle to be the | 122 | * to be the device-viewed address. |
| 121 | * device-viewed address. | ||
| 122 | */ | 123 | */ |
| 123 | extern void *dma_alloc_coherent(struct device *, size_t, dma_addr_t *, gfp_t); | 124 | extern void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, |
| 125 | gfp_t gfp, struct dma_attrs *attrs); | ||
| 126 | |||
| 127 | #define dma_alloc_coherent(d, s, h, f) dma_alloc_attrs(d, s, h, f, NULL) | ||
| 128 | |||
| 129 | static inline void *dma_alloc_attrs(struct device *dev, size_t size, | ||
| 130 | dma_addr_t *dma_handle, gfp_t flag, | ||
| 131 | struct dma_attrs *attrs) | ||
| 132 | { | ||
| 133 | struct dma_map_ops *ops = get_dma_ops(dev); | ||
| 134 | void *cpu_addr; | ||
| 135 | BUG_ON(!ops); | ||
| 136 | |||
| 137 | cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs); | ||
| 138 | debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr); | ||
| 139 | return cpu_addr; | ||
| 140 | } | ||
| 124 | 141 | ||
| 125 | /** | 142 | /** |
| 126 | * dma_free_coherent - free memory allocated by dma_alloc_coherent | 143 | * arm_dma_free - free memory allocated by arm_dma_alloc |
| 127 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices | 144 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 128 | * @size: size of memory originally requested in dma_alloc_coherent | 145 | * @size: size of memory originally requested in dma_alloc_coherent |
| 129 | * @cpu_addr: CPU-view address returned from dma_alloc_coherent | 146 | * @cpu_addr: CPU-view address returned from dma_alloc_coherent |
| 130 | * @handle: device-view address returned from dma_alloc_coherent | 147 | * @handle: device-view address returned from dma_alloc_coherent |
| 148 | * @attrs: optinal attributes that specific mapping properties | ||
| 131 | * | 149 | * |
| 132 | * Free (and unmap) a DMA buffer previously allocated by | 150 | * Free (and unmap) a DMA buffer previously allocated by |
| 133 | * dma_alloc_coherent(). | 151 | * arm_dma_alloc(). |
| 134 | * | 152 | * |
| 135 | * References to memory and mappings associated with cpu_addr/handle | 153 | * References to memory and mappings associated with cpu_addr/handle |
| 136 | * during and after this call executing are illegal. | 154 | * during and after this call executing are illegal. |
| 137 | */ | 155 | */ |
| 138 | extern void dma_free_coherent(struct device *, size_t, void *, dma_addr_t); | 156 | extern void arm_dma_free(struct device *dev, size_t size, void *cpu_addr, |
| 157 | dma_addr_t handle, struct dma_attrs *attrs); | ||
| 158 | |||
| 159 | #define dma_free_coherent(d, s, c, h) dma_free_attrs(d, s, c, h, NULL) | ||
| 160 | |||
| 161 | static inline void dma_free_attrs(struct device *dev, size_t size, | ||
| 162 | void *cpu_addr, dma_addr_t dma_handle, | ||
| 163 | struct dma_attrs *attrs) | ||
| 164 | { | ||
| 165 | struct dma_map_ops *ops = get_dma_ops(dev); | ||
| 166 | BUG_ON(!ops); | ||
| 167 | |||
| 168 | debug_dma_free_coherent(dev, size, cpu_addr, dma_handle); | ||
| 169 | ops->free(dev, size, cpu_addr, dma_handle, attrs); | ||
| 170 | } | ||
| 139 | 171 | ||
| 140 | /** | 172 | /** |
| 141 | * dma_mmap_coherent - map a coherent DMA allocation into user space | 173 | * arm_dma_mmap - map a coherent DMA allocation into user space |
| 142 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices | 174 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices |
| 143 | * @vma: vm_area_struct describing requested user mapping | 175 | * @vma: vm_area_struct describing requested user mapping |
| 144 | * @cpu_addr: kernel CPU-view address returned from dma_alloc_coherent | 176 | * @cpu_addr: kernel CPU-view address returned from dma_alloc_coherent |
| 145 | * @handle: device-view address returned from dma_alloc_coherent | 177 | * @handle: device-view address returned from dma_alloc_coherent |
| 146 | * @size: size of memory originally requested in dma_alloc_coherent | 178 | * @size: size of memory originally requested in dma_alloc_coherent |
| 179 | * @attrs: optinal attributes that specific mapping properties | ||
| 147 | * | 180 | * |
| 148 | * Map a coherent DMA buffer previously allocated by dma_alloc_coherent | 181 | * Map a coherent DMA buffer previously allocated by dma_alloc_coherent |
| 149 | * into user space. The coherent DMA buffer must not be freed by the | 182 | * into user space. The coherent DMA buffer must not be freed by the |
| 150 | * driver until the user space mapping has been released. | 183 | * driver until the user space mapping has been released. |
| 151 | */ | 184 | */ |
| 152 | int dma_mmap_coherent(struct device *, struct vm_area_struct *, | 185 | extern int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma, |
| 153 | void *, dma_addr_t, size_t); | 186 | void *cpu_addr, dma_addr_t dma_addr, size_t size, |
| 187 | struct dma_attrs *attrs); | ||
| 154 | 188 | ||
| 189 | #define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, NULL) | ||
| 155 | 190 | ||
| 156 | /** | 191 | static inline int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, |
| 157 | * dma_alloc_writecombine - allocate writecombining memory for DMA | 192 | void *cpu_addr, dma_addr_t dma_addr, |
| 158 | * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices | 193 | size_t size, struct dma_attrs *attrs) |
| 159 | * @size: required memory size | 194 | { |
| 160 | * @handle: bus-specific DMA address | 195 | struct dma_map_ops *ops = get_dma_ops(dev); |
| 161 | * | 196 | BUG_ON(!ops); |
| 162 | * Allocate some uncached, buffered memory for a device for | 197 | return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs); |
| 163 | * performing DMA. This function allocates pages, and will | 198 | } |
| 164 | * return the CPU-viewed address, and sets @handle to be the | ||
| 165 | * device-viewed address. | ||
| 166 | */ | ||
| 167 | extern void *dma_alloc_writecombine(struct device *, size_t, dma_addr_t *, | ||
| 168 | gfp_t); | ||
| 169 | 199 | ||
| 170 | #define dma_free_writecombine(dev,size,cpu_addr,handle) \ | 200 | static inline void *dma_alloc_writecombine(struct device *dev, size_t size, |
| 171 | dma_free_coherent(dev,size,cpu_addr,handle) | 201 | dma_addr_t *dma_handle, gfp_t flag) |
| 202 | { | ||
| 203 | DEFINE_DMA_ATTRS(attrs); | ||
| 204 | dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs); | ||
| 205 | return dma_alloc_attrs(dev, size, dma_handle, flag, &attrs); | ||
| 206 | } | ||
| 172 | 207 | ||
| 173 | int dma_mmap_writecombine(struct device *, struct vm_area_struct *, | 208 | static inline void dma_free_writecombine(struct device *dev, size_t size, |
| 174 | void *, dma_addr_t, size_t); | 209 | void *cpu_addr, dma_addr_t dma_handle) |
| 210 | { | ||
| 211 | DEFINE_DMA_ATTRS(attrs); | ||
| 212 | dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs); | ||
| 213 | return dma_free_attrs(dev, size, cpu_addr, dma_handle, &attrs); | ||
| 214 | } | ||
| 215 | |||
| 216 | static inline int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma, | ||
| 217 | void *cpu_addr, dma_addr_t dma_addr, size_t size) | ||
| 218 | { | ||
| 219 | DEFINE_DMA_ATTRS(attrs); | ||
| 220 | dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs); | ||
| 221 | return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, &attrs); | ||
| 222 | } | ||
| 175 | 223 | ||
| 176 | /* | 224 | /* |
| 177 | * This can be called during boot to increase the size of the consistent | 225 | * This can be called during boot to increase the size of the consistent |
| @@ -180,7 +228,6 @@ int dma_mmap_writecombine(struct device *, struct vm_area_struct *, | |||
| 180 | */ | 228 | */ |
| 181 | extern void __init init_consistent_dma_size(unsigned long size); | 229 | extern void __init init_consistent_dma_size(unsigned long size); |
| 182 | 230 | ||
| 183 | |||
| 184 | /* | 231 | /* |
| 185 | * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic" | 232 | * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic" |
| 186 | * and utilize bounce buffers as needed to work around limited DMA windows. | 233 | * and utilize bounce buffers as needed to work around limited DMA windows. |
diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c index dddb406d0763..2501866a904c 100644 --- a/arch/arm/mm/dma-mapping.c +++ b/arch/arm/mm/dma-mapping.c | |||
| @@ -113,6 +113,9 @@ static void arm_dma_sync_single_for_device(struct device *dev, | |||
| 113 | static int arm_dma_set_mask(struct device *dev, u64 dma_mask); | 113 | static int arm_dma_set_mask(struct device *dev, u64 dma_mask); |
| 114 | 114 | ||
| 115 | struct dma_map_ops arm_dma_ops = { | 115 | struct dma_map_ops arm_dma_ops = { |
| 116 | .alloc = arm_dma_alloc, | ||
| 117 | .free = arm_dma_free, | ||
| 118 | .mmap = arm_dma_mmap, | ||
| 116 | .map_page = arm_dma_map_page, | 119 | .map_page = arm_dma_map_page, |
| 117 | .unmap_page = arm_dma_unmap_page, | 120 | .unmap_page = arm_dma_unmap_page, |
| 118 | .map_sg = arm_dma_map_sg, | 121 | .map_sg = arm_dma_map_sg, |
| @@ -415,10 +418,19 @@ static void __dma_free_remap(void *cpu_addr, size_t size) | |||
| 415 | arm_vmregion_free(&consistent_head, c); | 418 | arm_vmregion_free(&consistent_head, c); |
| 416 | } | 419 | } |
| 417 | 420 | ||
| 421 | static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot) | ||
| 422 | { | ||
| 423 | prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ? | ||
| 424 | pgprot_writecombine(prot) : | ||
| 425 | pgprot_dmacoherent(prot); | ||
| 426 | return prot; | ||
| 427 | } | ||
| 428 | |||
| 418 | #else /* !CONFIG_MMU */ | 429 | #else /* !CONFIG_MMU */ |
| 419 | 430 | ||
| 420 | #define __dma_alloc_remap(page, size, gfp, prot, c) page_address(page) | 431 | #define __dma_alloc_remap(page, size, gfp, prot, c) page_address(page) |
| 421 | #define __dma_free_remap(addr, size) do { } while (0) | 432 | #define __dma_free_remap(addr, size) do { } while (0) |
| 433 | #define __get_dma_pgprot(attrs, prot) __pgprot(0) | ||
| 422 | 434 | ||
| 423 | #endif /* CONFIG_MMU */ | 435 | #endif /* CONFIG_MMU */ |
| 424 | 436 | ||
| @@ -462,41 +474,33 @@ __dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp, | |||
| 462 | * Allocate DMA-coherent memory space and return both the kernel remapped | 474 | * Allocate DMA-coherent memory space and return both the kernel remapped |
| 463 | * virtual and bus address for that space. | 475 | * virtual and bus address for that space. |
| 464 | */ | 476 | */ |
| 465 | void * | 477 | void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, |
| 466 | dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp) | 478 | gfp_t gfp, struct dma_attrs *attrs) |
| 467 | { | 479 | { |
| 480 | pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel); | ||
| 468 | void *memory; | 481 | void *memory; |
| 469 | 482 | ||
| 470 | if (dma_alloc_from_coherent(dev, size, handle, &memory)) | 483 | if (dma_alloc_from_coherent(dev, size, handle, &memory)) |
| 471 | return memory; | 484 | return memory; |
| 472 | 485 | ||
| 473 | return __dma_alloc(dev, size, handle, gfp, | 486 | return __dma_alloc(dev, size, handle, gfp, prot, |
| 474 | pgprot_dmacoherent(pgprot_kernel), | ||
| 475 | __builtin_return_address(0)); | 487 | __builtin_return_address(0)); |
| 476 | } | 488 | } |
| 477 | EXPORT_SYMBOL(dma_alloc_coherent); | ||
| 478 | 489 | ||
| 479 | /* | 490 | /* |
| 480 | * Allocate a writecombining region, in much the same way as | 491 | * Create userspace mapping for the DMA-coherent memory. |
| 481 | * dma_alloc_coherent above. | ||
| 482 | */ | 492 | */ |
| 483 | void * | 493 | int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma, |
| 484 | dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp) | 494 | void *cpu_addr, dma_addr_t dma_addr, size_t size, |
| 485 | { | 495 | struct dma_attrs *attrs) |
| 486 | return __dma_alloc(dev, size, handle, gfp, | ||
| 487 | pgprot_writecombine(pgprot_kernel), | ||
| 488 | __builtin_return_address(0)); | ||
| 489 | } | ||
| 490 | EXPORT_SYMBOL(dma_alloc_writecombine); | ||
| 491 | |||
| 492 | static int dma_mmap(struct device *dev, struct vm_area_struct *vma, | ||
| 493 | void *cpu_addr, dma_addr_t dma_addr, size_t size) | ||
| 494 | { | 496 | { |
| 495 | int ret = -ENXIO; | 497 | int ret = -ENXIO; |
| 496 | #ifdef CONFIG_MMU | 498 | #ifdef CONFIG_MMU |
| 497 | unsigned long user_size, kern_size; | 499 | unsigned long user_size, kern_size; |
| 498 | struct arm_vmregion *c; | 500 | struct arm_vmregion *c; |
| 499 | 501 | ||
| 502 | vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot); | ||
| 503 | |||
| 500 | if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret)) | 504 | if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret)) |
| 501 | return ret; | 505 | return ret; |
| 502 | 506 | ||
| @@ -521,27 +525,12 @@ static int dma_mmap(struct device *dev, struct vm_area_struct *vma, | |||
| 521 | return ret; | 525 | return ret; |
| 522 | } | 526 | } |
| 523 | 527 | ||
| 524 | int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma, | ||
| 525 | void *cpu_addr, dma_addr_t dma_addr, size_t size) | ||
| 526 | { | ||
| 527 | vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot); | ||
| 528 | return dma_mmap(dev, vma, cpu_addr, dma_addr, size); | ||
| 529 | } | ||
| 530 | EXPORT_SYMBOL(dma_mmap_coherent); | ||
| 531 | |||
| 532 | int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma, | ||
| 533 | void *cpu_addr, dma_addr_t dma_addr, size_t size) | ||
| 534 | { | ||
| 535 | vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); | ||
| 536 | return dma_mmap(dev, vma, cpu_addr, dma_addr, size); | ||
| 537 | } | ||
| 538 | EXPORT_SYMBOL(dma_mmap_writecombine); | ||
| 539 | |||
| 540 | /* | 528 | /* |
| 541 | * free a page as defined by the above mapping. | 529 | * free a page as defined by the above mapping. |
| 542 | * Must not be called with IRQs disabled. | 530 | * Must not be called with IRQs disabled. |
| 543 | */ | 531 | */ |
| 544 | void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle) | 532 | void arm_dma_free(struct device *dev, size_t size, void *cpu_addr, |
| 533 | dma_addr_t handle, struct dma_attrs *attrs) | ||
| 545 | { | 534 | { |
| 546 | WARN_ON(irqs_disabled()); | 535 | WARN_ON(irqs_disabled()); |
| 547 | 536 | ||
| @@ -555,7 +544,6 @@ void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr | |||
| 555 | 544 | ||
| 556 | __dma_free_buffer(pfn_to_page(dma_to_pfn(dev, handle)), size); | 545 | __dma_free_buffer(pfn_to_page(dma_to_pfn(dev, handle)), size); |
| 557 | } | 546 | } |
| 558 | EXPORT_SYMBOL(dma_free_coherent); | ||
| 559 | 547 | ||
| 560 | static void dma_cache_maint_page(struct page *page, unsigned long offset, | 548 | static void dma_cache_maint_page(struct page *page, unsigned long offset, |
| 561 | size_t size, enum dma_data_direction dir, | 549 | size_t size, enum dma_data_direction dir, |
