Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/sh/mm/consistent.c |
| 3 | * |
Paul Mundt | 8a7bcf0 | 2007-11-11 17:07:06 +0900 | [diff] [blame] | 4 | * Copyright (C) 2004 - 2007 Paul Mundt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Magnus Damm | f93e97e | 2008-01-24 18:35:10 +0900 | [diff] [blame] | 6 | * Declared coherent memory functions based on arch/x86/kernel/pci-dma_32.c |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * This file is subject to the terms and conditions of the GNU General Public |
| 9 | * License. See the file "COPYING" in the main directory of this archive |
| 10 | * for more details. |
| 11 | */ |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/dma-mapping.h> |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 14 | #include <asm/cacheflush.h> |
| 15 | #include <asm/addrspace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/io.h> |
| 17 | |
Magnus Damm | f93e97e | 2008-01-24 18:35:10 +0900 | [diff] [blame] | 18 | struct dma_coherent_mem { |
| 19 | void *virt_base; |
| 20 | u32 device_base; |
| 21 | int size; |
| 22 | int flags; |
| 23 | unsigned long *bitmap; |
| 24 | }; |
| 25 | |
| 26 | void *dma_alloc_coherent(struct device *dev, size_t size, |
| 27 | dma_addr_t *dma_handle, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Magnus Damm | 2a3eeba | 2008-01-25 12:42:48 +0900 | [diff] [blame] | 29 | void *ret, *ret_nocache; |
Magnus Damm | f93e97e | 2008-01-24 18:35:10 +0900 | [diff] [blame] | 30 | int order = get_order(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Dmitry Baryshkov | 9de90ac | 2008-07-18 13:30:31 +0400 | [diff] [blame^] | 32 | if (dma_alloc_from_coherent(dev, size, dma_handle, &ret)) |
| 33 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Magnus Damm | f93e97e | 2008-01-24 18:35:10 +0900 | [diff] [blame] | 35 | ret = (void *)__get_free_pages(gfp, order); |
Magnus Damm | 2a3eeba | 2008-01-25 12:42:48 +0900 | [diff] [blame] | 36 | if (!ret) |
| 37 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Magnus Damm | 2a3eeba | 2008-01-25 12:42:48 +0900 | [diff] [blame] | 39 | memset(ret, 0, size); |
| 40 | /* |
| 41 | * Pages from the page allocator may have data present in |
| 42 | * cache. So flush the cache before using uncached memory. |
| 43 | */ |
| 44 | dma_cache_sync(dev, ret, size, DMA_BIDIRECTIONAL); |
| 45 | |
| 46 | ret_nocache = ioremap_nocache(virt_to_phys(ret), size); |
| 47 | if (!ret_nocache) { |
| 48 | free_pages((unsigned long)ret, order); |
| 49 | return NULL; |
Magnus Damm | f93e97e | 2008-01-24 18:35:10 +0900 | [diff] [blame] | 50 | } |
Magnus Damm | 2a3eeba | 2008-01-25 12:42:48 +0900 | [diff] [blame] | 51 | |
| 52 | *dma_handle = virt_to_phys(ret); |
| 53 | return ret_nocache; |
Magnus Damm | f93e97e | 2008-01-24 18:35:10 +0900 | [diff] [blame] | 54 | } |
| 55 | EXPORT_SYMBOL(dma_alloc_coherent); |
| 56 | |
| 57 | void dma_free_coherent(struct device *dev, size_t size, |
| 58 | void *vaddr, dma_addr_t dma_handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
Magnus Damm | f93e97e | 2008-01-24 18:35:10 +0900 | [diff] [blame] | 60 | struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL; |
| 61 | int order = get_order(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Dmitry Baryshkov | 9de90ac | 2008-07-18 13:30:31 +0400 | [diff] [blame^] | 63 | if (!dma_release_from_coherent(dev, order, vaddr)) { |
Magnus Damm | f93e97e | 2008-01-24 18:35:10 +0900 | [diff] [blame] | 64 | WARN_ON(irqs_disabled()); /* for portability */ |
| 65 | BUG_ON(mem && mem->flags & DMA_MEMORY_EXCLUSIVE); |
Magnus Damm | 2a3eeba | 2008-01-25 12:42:48 +0900 | [diff] [blame] | 66 | free_pages((unsigned long)phys_to_virt(dma_handle), order); |
| 67 | iounmap(vaddr); |
Magnus Damm | f93e97e | 2008-01-24 18:35:10 +0900 | [diff] [blame] | 68 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
Magnus Damm | f93e97e | 2008-01-24 18:35:10 +0900 | [diff] [blame] | 70 | EXPORT_SYMBOL(dma_free_coherent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Magnus Damm | f93e97e | 2008-01-24 18:35:10 +0900 | [diff] [blame] | 72 | void dma_cache_sync(struct device *dev, void *vaddr, size_t size, |
| 73 | enum dma_data_direction direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Paul Mundt | 8a7bcf0 | 2007-11-11 17:07:06 +0900 | [diff] [blame] | 75 | #ifdef CONFIG_CPU_SH5 |
| 76 | void *p1addr = vaddr; |
| 77 | #else |
| 78 | void *p1addr = (void*) P1SEGADDR((unsigned long)vaddr); |
| 79 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | switch (direction) { |
| 82 | case DMA_FROM_DEVICE: /* invalidate only */ |
Ralf Baechle | 622a9ed | 2007-10-16 23:29:42 -0700 | [diff] [blame] | 83 | __flush_invalidate_region(p1addr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | break; |
| 85 | case DMA_TO_DEVICE: /* writeback only */ |
Ralf Baechle | 622a9ed | 2007-10-16 23:29:42 -0700 | [diff] [blame] | 86 | __flush_wback_region(p1addr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | break; |
| 88 | case DMA_BIDIRECTIONAL: /* writeback and invalidate */ |
Ralf Baechle | 622a9ed | 2007-10-16 23:29:42 -0700 | [diff] [blame] | 89 | __flush_purge_region(p1addr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | break; |
| 91 | default: |
| 92 | BUG(); |
| 93 | } |
| 94 | } |
Magnus Damm | f93e97e | 2008-01-24 18:35:10 +0900 | [diff] [blame] | 95 | EXPORT_SYMBOL(dma_cache_sync); |