Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
12d04eef | 2 | * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation |
1da177e4 | 3 | * |
12d04eef | 4 | * Provide default implementations of the DMA mapping callbacks for |
8dd0e952 | 5 | * directly mapped busses. |
1da177e4 LT |
6 | */ |
7 | ||
8 | #include <linux/device.h> | |
9 | #include <linux/dma-mapping.h> | |
80d3e8ab | 10 | #include <linux/dma-debug.h> |
5a0e3ad6 | 11 | #include <linux/gfp.h> |
b2f2e8fe | 12 | #include <linux/lmb.h> |
1da177e4 | 13 | #include <asm/bug.h> |
12d04eef | 14 | #include <asm/abs_addr.h> |
1da177e4 | 15 | |
12d04eef BH |
16 | /* |
17 | * Generic direct DMA implementation | |
92b20c40 | 18 | * |
31d1b493 ME |
19 | * This implementation supports a per-device offset that can be applied if |
20 | * the address at which memory is visible to devices is not 0. Platform code | |
21 | * can set archdata.dma_data to an unsigned long holding the offset. By | |
4fc665b8 | 22 | * default the offset is PCI_DRAM_OFFSET. |
12d04eef | 23 | */ |
5d33eebe | 24 | |
35e4a6e2 | 25 | |
4fc665b8 BB |
26 | void *dma_direct_alloc_coherent(struct device *dev, size_t size, |
27 | dma_addr_t *dma_handle, gfp_t flag) | |
12d04eef | 28 | { |
8aa26590 | 29 | void *ret; |
4fc665b8 | 30 | #ifdef CONFIG_NOT_COHERENT_CACHE |
8b31e49d | 31 | ret = __dma_alloc_coherent(dev, size, dma_handle, flag); |
8aa26590 BH |
32 | if (ret == NULL) |
33 | return NULL; | |
1cebd7a0 | 34 | *dma_handle += get_dma_offset(dev); |
8aa26590 | 35 | return ret; |
4fc665b8 | 36 | #else |
c80d9133 | 37 | struct page *page; |
8fae0353 | 38 | int node = dev_to_node(dev); |
12d04eef | 39 | |
4fc665b8 BB |
40 | /* ignore region specifiers */ |
41 | flag &= ~(__GFP_HIGHMEM); | |
42 | ||
c80d9133 BH |
43 | page = alloc_pages_node(node, flag, get_order(size)); |
44 | if (page == NULL) | |
45 | return NULL; | |
46 | ret = page_address(page); | |
47 | memset(ret, 0, size); | |
1cebd7a0 | 48 | *dma_handle = virt_to_abs(ret) + get_dma_offset(dev); |
c80d9133 | 49 | |
12d04eef | 50 | return ret; |
4fc665b8 | 51 | #endif |
1da177e4 | 52 | } |
1da177e4 | 53 | |
4fc665b8 BB |
54 | void dma_direct_free_coherent(struct device *dev, size_t size, |
55 | void *vaddr, dma_addr_t dma_handle) | |
1da177e4 | 56 | { |
4fc665b8 BB |
57 | #ifdef CONFIG_NOT_COHERENT_CACHE |
58 | __dma_free_coherent(size, vaddr); | |
59 | #else | |
12d04eef | 60 | free_pages((unsigned long)vaddr, get_order(size)); |
4fc665b8 | 61 | #endif |
1da177e4 | 62 | } |
1da177e4 | 63 | |
78bdc310 | 64 | static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, |
3affedc4 MN |
65 | int nents, enum dma_data_direction direction, |
66 | struct dma_attrs *attrs) | |
1da177e4 | 67 | { |
78bdc310 | 68 | struct scatterlist *sg; |
12d04eef | 69 | int i; |
1da177e4 | 70 | |
78bdc310 | 71 | for_each_sg(sgl, sg, nents, i) { |
1cebd7a0 | 72 | sg->dma_address = sg_phys(sg) + get_dma_offset(dev); |
12d04eef | 73 | sg->dma_length = sg->length; |
2434bbb3 | 74 | __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction); |
12d04eef | 75 | } |
5d33eebe | 76 | |
12d04eef | 77 | return nents; |
1da177e4 | 78 | } |
1da177e4 | 79 | |
12d04eef | 80 | static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg, |
3affedc4 MN |
81 | int nents, enum dma_data_direction direction, |
82 | struct dma_attrs *attrs) | |
1da177e4 | 83 | { |
12d04eef | 84 | } |
5d33eebe | 85 | |
12d04eef BH |
86 | static int dma_direct_dma_supported(struct device *dev, u64 mask) |
87 | { | |
4fc665b8 | 88 | #ifdef CONFIG_PPC64 |
b2f2e8fe | 89 | /* Could be improved so platforms can set the limit in case |
12d04eef BH |
90 | * they have limited DMA windows |
91 | */ | |
b2f2e8fe | 92 | return mask >= (lmb_end_of_DRAM() - 1); |
4fc665b8 BB |
93 | #else |
94 | return 1; | |
95 | #endif | |
96 | } | |
97 | ||
98 | static inline dma_addr_t dma_direct_map_page(struct device *dev, | |
99 | struct page *page, | |
100 | unsigned long offset, | |
101 | size_t size, | |
102 | enum dma_data_direction dir, | |
103 | struct dma_attrs *attrs) | |
104 | { | |
105 | BUG_ON(dir == DMA_NONE); | |
106 | __dma_sync_page(page, offset, size, dir); | |
1cebd7a0 | 107 | return page_to_phys(page) + offset + get_dma_offset(dev); |
4fc665b8 BB |
108 | } |
109 | ||
110 | static inline void dma_direct_unmap_page(struct device *dev, | |
111 | dma_addr_t dma_address, | |
112 | size_t size, | |
113 | enum dma_data_direction direction, | |
114 | struct dma_attrs *attrs) | |
115 | { | |
1da177e4 | 116 | } |
12d04eef | 117 | |
15e09c0e BB |
118 | #ifdef CONFIG_NOT_COHERENT_CACHE |
119 | static inline void dma_direct_sync_sg(struct device *dev, | |
120 | struct scatterlist *sgl, int nents, | |
121 | enum dma_data_direction direction) | |
122 | { | |
123 | struct scatterlist *sg; | |
124 | int i; | |
125 | ||
126 | for_each_sg(sgl, sg, nents, i) | |
127 | __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction); | |
128 | } | |
129 | ||
712d3e22 FT |
130 | static inline void dma_direct_sync_single(struct device *dev, |
131 | dma_addr_t dma_handle, size_t size, | |
132 | enum dma_data_direction direction) | |
15e09c0e | 133 | { |
712d3e22 | 134 | __dma_sync(bus_to_virt(dma_handle), size, direction); |
15e09c0e BB |
135 | } |
136 | #endif | |
137 | ||
45223c54 | 138 | struct dma_map_ops dma_direct_ops = { |
12d04eef BH |
139 | .alloc_coherent = dma_direct_alloc_coherent, |
140 | .free_coherent = dma_direct_free_coherent, | |
12d04eef BH |
141 | .map_sg = dma_direct_map_sg, |
142 | .unmap_sg = dma_direct_unmap_sg, | |
143 | .dma_supported = dma_direct_dma_supported, | |
4fc665b8 BB |
144 | .map_page = dma_direct_map_page, |
145 | .unmap_page = dma_direct_unmap_page, | |
15e09c0e | 146 | #ifdef CONFIG_NOT_COHERENT_CACHE |
712d3e22 FT |
147 | .sync_single_for_cpu = dma_direct_sync_single, |
148 | .sync_single_for_device = dma_direct_sync_single, | |
15e09c0e BB |
149 | .sync_sg_for_cpu = dma_direct_sync_sg, |
150 | .sync_sg_for_device = dma_direct_sync_sg, | |
151 | #endif | |
12d04eef BH |
152 | }; |
153 | EXPORT_SYMBOL(dma_direct_ops); | |
80d3e8ab FT |
154 | |
155 | #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16) | |
156 | ||
157 | static int __init dma_init(void) | |
158 | { | |
159 | dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES); | |
160 | ||
161 | return 0; | |
162 | } | |
163 | fs_initcall(dma_init); |