Michal Simek | ccfe27d | 2010-01-14 11:21:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009-2010 PetaLogix |
| 3 | * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation |
| 4 | * |
| 5 | * Provide default implementations of the DMA mapping callbacks for |
| 6 | * directly mapped busses. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/dma-mapping.h> |
| 11 | #include <linux/dma-debug.h> |
| 12 | #include <asm/bug.h> |
Michal Simek | 2549edd | 2010-01-20 14:36:24 +0100 | [diff] [blame^] | 13 | #include <asm/cacheflush.h> |
Michal Simek | ccfe27d | 2010-01-14 11:21:02 +0100 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * Generic direct DMA implementation |
| 17 | * |
| 18 | * This implementation supports a per-device offset that can be applied if |
| 19 | * the address at which memory is visible to devices is not 0. Platform code |
| 20 | * can set archdata.dma_data to an unsigned long holding the offset. By |
| 21 | * default the offset is PCI_DRAM_OFFSET. |
| 22 | */ |
| 23 | |
Michal Simek | 2549edd | 2010-01-20 14:36:24 +0100 | [diff] [blame^] | 24 | static inline void __dma_sync_page(void *vaddr, unsigned long offset, |
| 25 | size_t size, enum dma_data_direction direction) |
| 26 | { |
| 27 | unsigned long start = virt_to_phys(vaddr); |
| 28 | |
| 29 | switch (direction) { |
| 30 | case DMA_TO_DEVICE: |
| 31 | flush_dcache_range(start + offset, start + offset + size); |
| 32 | break; |
| 33 | case DMA_FROM_DEVICE: |
| 34 | invalidate_dcache_range(start + offset, start + offset + size); |
| 35 | break; |
| 36 | default: |
| 37 | BUG(); |
| 38 | } |
| 39 | } |
| 40 | |
Michal Simek | ccfe27d | 2010-01-14 11:21:02 +0100 | [diff] [blame] | 41 | static unsigned long get_dma_direct_offset(struct device *dev) |
| 42 | { |
| 43 | if (dev) |
| 44 | return (unsigned long)dev->archdata.dma_data; |
| 45 | |
| 46 | return PCI_DRAM_OFFSET; /* FIXME Not sure if is correct */ |
| 47 | } |
| 48 | |
| 49 | void *dma_direct_alloc_coherent(struct device *dev, size_t size, |
| 50 | dma_addr_t *dma_handle, gfp_t flag) |
| 51 | { |
| 52 | void *ret; |
| 53 | struct page *page; |
| 54 | int node = dev_to_node(dev); |
| 55 | |
| 56 | /* ignore region specifiers */ |
| 57 | flag &= ~(__GFP_HIGHMEM); |
| 58 | |
| 59 | page = alloc_pages_node(node, flag, get_order(size)); |
| 60 | if (page == NULL) |
| 61 | return NULL; |
| 62 | ret = page_address(page); |
| 63 | memset(ret, 0, size); |
| 64 | *dma_handle = virt_to_phys(ret) + get_dma_direct_offset(dev); |
| 65 | |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | void dma_direct_free_coherent(struct device *dev, size_t size, |
| 70 | void *vaddr, dma_addr_t dma_handle) |
| 71 | { |
| 72 | free_pages((unsigned long)vaddr, get_order(size)); |
| 73 | } |
| 74 | |
| 75 | static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, |
| 76 | int nents, enum dma_data_direction direction, |
| 77 | struct dma_attrs *attrs) |
| 78 | { |
| 79 | struct scatterlist *sg; |
| 80 | int i; |
| 81 | |
| 82 | for_each_sg(sgl, sg, nents, i) { |
| 83 | sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev); |
| 84 | sg->dma_length = sg->length; |
| 85 | __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction); |
| 86 | } |
| 87 | |
| 88 | return nents; |
| 89 | } |
| 90 | |
| 91 | static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg, |
| 92 | int nents, enum dma_data_direction direction, |
| 93 | struct dma_attrs *attrs) |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | static int dma_direct_dma_supported(struct device *dev, u64 mask) |
| 98 | { |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | static inline dma_addr_t dma_direct_map_page(struct device *dev, |
| 103 | struct page *page, |
| 104 | unsigned long offset, |
| 105 | size_t size, |
Michal Simek | 2549edd | 2010-01-20 14:36:24 +0100 | [diff] [blame^] | 106 | enum dma_data_direction direction, |
Michal Simek | ccfe27d | 2010-01-14 11:21:02 +0100 | [diff] [blame] | 107 | struct dma_attrs *attrs) |
| 108 | { |
Michal Simek | 2549edd | 2010-01-20 14:36:24 +0100 | [diff] [blame^] | 109 | BUG_ON(direction == DMA_NONE); |
| 110 | __dma_sync_page(page, offset, size, direction); |
Michal Simek | ccfe27d | 2010-01-14 11:21:02 +0100 | [diff] [blame] | 111 | return page_to_phys(page) + offset + get_dma_direct_offset(dev); |
| 112 | } |
| 113 | |
| 114 | static inline void dma_direct_unmap_page(struct device *dev, |
| 115 | dma_addr_t dma_address, |
| 116 | size_t size, |
| 117 | enum dma_data_direction direction, |
| 118 | struct dma_attrs *attrs) |
| 119 | { |
Michal Simek | 2549edd | 2010-01-20 14:36:24 +0100 | [diff] [blame^] | 120 | /* There is not necessary to do cache cleanup */ |
| 121 | /* __dma_sync_page(dma_address, 0 , size, direction); */ |
Michal Simek | ccfe27d | 2010-01-14 11:21:02 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | struct dma_map_ops dma_direct_ops = { |
| 125 | .alloc_coherent = dma_direct_alloc_coherent, |
| 126 | .free_coherent = dma_direct_free_coherent, |
| 127 | .map_sg = dma_direct_map_sg, |
| 128 | .unmap_sg = dma_direct_unmap_sg, |
| 129 | .dma_supported = dma_direct_dma_supported, |
| 130 | .map_page = dma_direct_map_page, |
| 131 | .unmap_page = dma_direct_unmap_page, |
| 132 | }; |
| 133 | EXPORT_SYMBOL(dma_direct_ops); |
| 134 | |
| 135 | /* Number of entries preallocated for DMA-API debugging */ |
| 136 | #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16) |
| 137 | |
| 138 | static int __init dma_init(void) |
| 139 | { |
| 140 | dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | fs_initcall(dma_init); |