Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 1 | /* |
Uwe Kleine-König | 5886269 | 2007-05-09 07:51:49 +0200 | [diff] [blame] | 2 | * arch/xtensa/kernel/pci-dma.c |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 3 | * |
| 4 | * DMA coherent memory allocation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2 of the License, or (at your |
| 9 | * option) any later version. |
| 10 | * |
| 11 | * Copyright (C) 2002 - 2005 Tensilica Inc. |
| 12 | * |
| 13 | * Based on version for i386. |
| 14 | * |
| 15 | * Chris Zankel <chris@zankel.net> |
| 16 | * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com> |
| 17 | */ |
| 18 | |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/pci.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/gfp.h> |
Max Filippov | d3738f4 | 2012-09-17 05:44:56 +0400 | [diff] [blame^] | 24 | #include <linux/module.h> |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 25 | #include <asm/io.h> |
| 26 | #include <asm/cacheflush.h> |
| 27 | |
| 28 | /* |
| 29 | * Note: We assume that the full memory space is always mapped to 'kseg' |
| 30 | * Otherwise we have to use page attributes (not implemented). |
| 31 | */ |
| 32 | |
| 33 | void * |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 34 | dma_alloc_coherent(struct device *dev,size_t size,dma_addr_t *handle,gfp_t flag) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 35 | { |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 36 | unsigned long ret; |
| 37 | unsigned long uncached = 0; |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 38 | |
| 39 | /* ignore region speicifiers */ |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 40 | |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 41 | flag &= ~(__GFP_DMA | __GFP_HIGHMEM); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 42 | |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 43 | if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff)) |
| 44 | flag |= GFP_DMA; |
| 45 | ret = (unsigned long)__get_free_pages(flag, get_order(size)); |
| 46 | |
| 47 | if (ret == 0) |
| 48 | return NULL; |
| 49 | |
| 50 | /* We currently don't support coherent memory outside KSEG */ |
| 51 | |
| 52 | if (ret < XCHAL_KSEG_CACHED_VADDR |
| 53 | || ret >= XCHAL_KSEG_CACHED_VADDR + XCHAL_KSEG_SIZE) |
| 54 | BUG(); |
| 55 | |
| 56 | |
| 57 | if (ret != 0) { |
| 58 | memset((void*) ret, 0, size); |
| 59 | uncached = ret+XCHAL_KSEG_BYPASS_VADDR-XCHAL_KSEG_CACHED_VADDR; |
| 60 | *handle = virt_to_bus((void*)ret); |
| 61 | __flush_invalidate_dcache_range(ret, size); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 62 | } |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 63 | |
| 64 | return (void*)uncached; |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 65 | } |
Max Filippov | d3738f4 | 2012-09-17 05:44:56 +0400 | [diff] [blame^] | 66 | EXPORT_SYMBOL(dma_alloc_coherent); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 67 | |
| 68 | void dma_free_coherent(struct device *hwdev, size_t size, |
| 69 | void *vaddr, dma_addr_t dma_handle) |
| 70 | { |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 71 | long addr=(long)vaddr+XCHAL_KSEG_CACHED_VADDR-XCHAL_KSEG_BYPASS_VADDR; |
| 72 | |
| 73 | if (addr < 0 || addr >= XCHAL_KSEG_SIZE) |
| 74 | BUG(); |
| 75 | |
| 76 | free_pages(addr, get_order(size)); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 77 | } |
Max Filippov | d3738f4 | 2012-09-17 05:44:56 +0400 | [diff] [blame^] | 78 | EXPORT_SYMBOL(dma_free_coherent); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 79 | |
| 80 | |
| 81 | void consistent_sync(void *vaddr, size_t size, int direction) |
| 82 | { |
| 83 | switch (direction) { |
| 84 | case PCI_DMA_NONE: |
| 85 | BUG(); |
| 86 | case PCI_DMA_FROMDEVICE: /* invalidate only */ |
| 87 | __invalidate_dcache_range((unsigned long)vaddr, |
| 88 | (unsigned long)size); |
| 89 | break; |
| 90 | |
| 91 | case PCI_DMA_TODEVICE: /* writeback only */ |
| 92 | case PCI_DMA_BIDIRECTIONAL: /* writeback and invalidate */ |
| 93 | __flush_invalidate_dcache_range((unsigned long)vaddr, |
| 94 | (unsigned long)size); |
| 95 | break; |
| 96 | } |
| 97 | } |
Max Filippov | d3738f4 | 2012-09-17 05:44:56 +0400 | [diff] [blame^] | 98 | EXPORT_SYMBOL(consistent_sync); |