Vineet Gupta | 1162b07 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * DMA Coherent API Notes |
| 11 | * |
| 12 | * I/O is inherently non-coherent on ARC. So a coherent DMA buffer is |
| 13 | * implemented by accessintg it using a kernel virtual address, with |
| 14 | * Cache bit off in the TLB entry. |
| 15 | * |
| 16 | * The default DMA address == Phy address which is 0x8000_0000 based. |
Vineet Gupta | 1162b07 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/dma-mapping.h> |
| 20 | #include <linux/dma-debug.h> |
| 21 | #include <linux/export.h> |
| 22 | #include <asm/cacheflush.h> |
| 23 | |
| 24 | /* |
| 25 | * Helpers for Coherent DMA API. |
| 26 | */ |
| 27 | void *dma_alloc_noncoherent(struct device *dev, size_t size, |
| 28 | dma_addr_t *dma_handle, gfp_t gfp) |
| 29 | { |
| 30 | void *paddr; |
| 31 | |
| 32 | /* This is linear addr (0x8000_0000 based) */ |
| 33 | paddr = alloc_pages_exact(size, gfp); |
| 34 | if (!paddr) |
| 35 | return NULL; |
| 36 | |
| 37 | /* This is bus address, platform dependent */ |
Vineet Gupta | 454bfda | 2015-04-16 21:04:49 +0530 | [diff] [blame^] | 38 | *dma_handle = (dma_addr_t)paddr; |
Vineet Gupta | 1162b07 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 39 | |
| 40 | return paddr; |
| 41 | } |
| 42 | EXPORT_SYMBOL(dma_alloc_noncoherent); |
| 43 | |
| 44 | void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr, |
| 45 | dma_addr_t dma_handle) |
| 46 | { |
Vineet Gupta | 454bfda | 2015-04-16 21:04:49 +0530 | [diff] [blame^] | 47 | free_pages_exact((void *)dma_handle, size); |
Vineet Gupta | 1162b07 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 48 | } |
| 49 | EXPORT_SYMBOL(dma_free_noncoherent); |
| 50 | |
| 51 | void *dma_alloc_coherent(struct device *dev, size_t size, |
| 52 | dma_addr_t *dma_handle, gfp_t gfp) |
| 53 | { |
| 54 | void *paddr, *kvaddr; |
| 55 | |
| 56 | /* This is linear addr (0x8000_0000 based) */ |
| 57 | paddr = alloc_pages_exact(size, gfp); |
| 58 | if (!paddr) |
| 59 | return NULL; |
| 60 | |
| 61 | /* This is kernel Virtual address (0x7000_0000 based) */ |
| 62 | kvaddr = ioremap_nocache((unsigned long)paddr, size); |
| 63 | if (kvaddr != NULL) |
| 64 | memset(kvaddr, 0, size); |
| 65 | |
| 66 | /* This is bus address, platform dependent */ |
Vineet Gupta | 454bfda | 2015-04-16 21:04:49 +0530 | [diff] [blame^] | 67 | *dma_handle = (dma_addr_t)paddr; |
Vineet Gupta | 1162b07 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 68 | |
| 69 | return kvaddr; |
| 70 | } |
| 71 | EXPORT_SYMBOL(dma_alloc_coherent); |
| 72 | |
| 73 | void dma_free_coherent(struct device *dev, size_t size, void *kvaddr, |
| 74 | dma_addr_t dma_handle) |
| 75 | { |
| 76 | iounmap((void __force __iomem *)kvaddr); |
| 77 | |
Vineet Gupta | 454bfda | 2015-04-16 21:04:49 +0530 | [diff] [blame^] | 78 | free_pages_exact((void *)dma_handle, size); |
Vineet Gupta | 1162b07 | 2013-01-18 15:12:20 +0530 | [diff] [blame] | 79 | } |
| 80 | EXPORT_SYMBOL(dma_free_coherent); |
| 81 | |
| 82 | /* |
| 83 | * Helper for streaming DMA... |
| 84 | */ |
| 85 | void __arc_dma_cache_sync(unsigned long paddr, size_t size, |
| 86 | enum dma_data_direction dir) |
| 87 | { |
| 88 | __inline_dma_cache_sync(paddr, size, dir); |
| 89 | } |
| 90 | EXPORT_SYMBOL(__arc_dma_cache_sync); |