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> |
| 13 | |
| 14 | /* |
| 15 | * Generic direct DMA implementation |
| 16 | * |
| 17 | * This implementation supports a per-device offset that can be applied if |
| 18 | * the address at which memory is visible to devices is not 0. Platform code |
| 19 | * can set archdata.dma_data to an unsigned long holding the offset. By |
| 20 | * default the offset is PCI_DRAM_OFFSET. |
| 21 | */ |
| 22 | |
| 23 | static unsigned long get_dma_direct_offset(struct device *dev) |
| 24 | { |
| 25 | if (dev) |
| 26 | return (unsigned long)dev->archdata.dma_data; |
| 27 | |
| 28 | return PCI_DRAM_OFFSET; /* FIXME Not sure if is correct */ |
| 29 | } |
| 30 | |
| 31 | void *dma_direct_alloc_coherent(struct device *dev, size_t size, |
| 32 | dma_addr_t *dma_handle, gfp_t flag) |
| 33 | { |
| 34 | void *ret; |
| 35 | struct page *page; |
| 36 | int node = dev_to_node(dev); |
| 37 | |
| 38 | /* ignore region specifiers */ |
| 39 | flag &= ~(__GFP_HIGHMEM); |
| 40 | |
| 41 | page = alloc_pages_node(node, flag, get_order(size)); |
| 42 | if (page == NULL) |
| 43 | return NULL; |
| 44 | ret = page_address(page); |
| 45 | memset(ret, 0, size); |
| 46 | *dma_handle = virt_to_phys(ret) + get_dma_direct_offset(dev); |
| 47 | |
| 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | void dma_direct_free_coherent(struct device *dev, size_t size, |
| 52 | void *vaddr, dma_addr_t dma_handle) |
| 53 | { |
| 54 | free_pages((unsigned long)vaddr, get_order(size)); |
| 55 | } |
| 56 | |
| 57 | static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, |
| 58 | int nents, enum dma_data_direction direction, |
| 59 | struct dma_attrs *attrs) |
| 60 | { |
| 61 | struct scatterlist *sg; |
| 62 | int i; |
| 63 | |
| 64 | for_each_sg(sgl, sg, nents, i) { |
| 65 | sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev); |
| 66 | sg->dma_length = sg->length; |
| 67 | __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction); |
| 68 | } |
| 69 | |
| 70 | return nents; |
| 71 | } |
| 72 | |
| 73 | static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg, |
| 74 | int nents, enum dma_data_direction direction, |
| 75 | struct dma_attrs *attrs) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | static int dma_direct_dma_supported(struct device *dev, u64 mask) |
| 80 | { |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | static inline dma_addr_t dma_direct_map_page(struct device *dev, |
| 85 | struct page *page, |
| 86 | unsigned long offset, |
| 87 | size_t size, |
| 88 | enum dma_data_direction dir, |
| 89 | struct dma_attrs *attrs) |
| 90 | { |
| 91 | BUG_ON(dir == DMA_NONE); |
| 92 | __dma_sync_page(page, offset, size, dir); |
| 93 | return page_to_phys(page) + offset + get_dma_direct_offset(dev); |
| 94 | } |
| 95 | |
| 96 | static inline void dma_direct_unmap_page(struct device *dev, |
| 97 | dma_addr_t dma_address, |
| 98 | size_t size, |
| 99 | enum dma_data_direction direction, |
| 100 | struct dma_attrs *attrs) |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | struct dma_map_ops dma_direct_ops = { |
| 105 | .alloc_coherent = dma_direct_alloc_coherent, |
| 106 | .free_coherent = dma_direct_free_coherent, |
| 107 | .map_sg = dma_direct_map_sg, |
| 108 | .unmap_sg = dma_direct_unmap_sg, |
| 109 | .dma_supported = dma_direct_dma_supported, |
| 110 | .map_page = dma_direct_map_page, |
| 111 | .unmap_page = dma_direct_unmap_page, |
| 112 | }; |
| 113 | EXPORT_SYMBOL(dma_direct_ops); |
| 114 | |
| 115 | /* Number of entries preallocated for DMA-API debugging */ |
| 116 | #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16) |
| 117 | |
| 118 | static int __init dma_init(void) |
| 119 | { |
| 120 | dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | fs_initcall(dma_init); |