2 * Copyright (C) 2009-2010 PetaLogix
3 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
5 * Provide default implementations of the DMA mapping callbacks for
6 * directly mapped busses.
9 #include <linux/device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/gfp.h>
12 #include <linux/dma-debug.h>
13 #include <linux/export.h>
17 * Generic direct DMA implementation
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
22 * default the offset is PCI_DRAM_OFFSET.
25 static unsigned long get_dma_direct_offset(struct device
*dev
)
28 return (unsigned long)dev
->archdata
.dma_data
;
30 return PCI_DRAM_OFFSET
; /* FIXME Not sure if is correct */
33 #define NOT_COHERENT_CACHE
35 static void *dma_direct_alloc_coherent(struct device
*dev
, size_t size
,
36 dma_addr_t
*dma_handle
, gfp_t flag
,
37 struct dma_attrs
*attrs
)
39 #ifdef NOT_COHERENT_CACHE
40 return consistent_alloc(flag
, size
, dma_handle
);
44 int node
= dev_to_node(dev
);
46 /* ignore region specifiers */
47 flag
&= ~(__GFP_HIGHMEM
);
49 page
= alloc_pages_node(node
, flag
, get_order(size
));
52 ret
= page_address(page
);
54 *dma_handle
= virt_to_phys(ret
) + get_dma_direct_offset(dev
);
60 static void dma_direct_free_coherent(struct device
*dev
, size_t size
,
61 void *vaddr
, dma_addr_t dma_handle
,
62 struct dma_attrs
*attrs
)
64 #ifdef NOT_COHERENT_CACHE
65 consistent_free(size
, vaddr
);
67 free_pages((unsigned long)vaddr
, get_order(size
));
71 static int dma_direct_map_sg(struct device
*dev
, struct scatterlist
*sgl
,
72 int nents
, enum dma_data_direction direction
,
73 struct dma_attrs
*attrs
)
75 struct scatterlist
*sg
;
78 /* FIXME this part of code is untested */
79 for_each_sg(sgl
, sg
, nents
, i
) {
80 sg
->dma_address
= sg_phys(sg
) + get_dma_direct_offset(dev
);
81 __dma_sync(page_to_phys(sg_page(sg
)) + sg
->offset
,
82 sg
->length
, direction
);
88 static void dma_direct_unmap_sg(struct device
*dev
, struct scatterlist
*sg
,
89 int nents
, enum dma_data_direction direction
,
90 struct dma_attrs
*attrs
)
94 static int dma_direct_dma_supported(struct device
*dev
, u64 mask
)
99 static inline dma_addr_t
dma_direct_map_page(struct device
*dev
,
101 unsigned long offset
,
103 enum dma_data_direction direction
,
104 struct dma_attrs
*attrs
)
106 __dma_sync(page_to_phys(page
) + offset
, size
, direction
);
107 return page_to_phys(page
) + offset
+ get_dma_direct_offset(dev
);
110 static inline void dma_direct_unmap_page(struct device
*dev
,
111 dma_addr_t dma_address
,
113 enum dma_data_direction direction
,
114 struct dma_attrs
*attrs
)
116 /* There is not necessary to do cache cleanup
118 * phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
119 * dma_address is physical address
121 __dma_sync(dma_address
, size
, direction
);
125 dma_direct_sync_single_for_cpu(struct device
*dev
,
126 dma_addr_t dma_handle
, size_t size
,
127 enum dma_data_direction direction
)
130 * It's pointless to flush the cache as the memory segment
131 * is given to the CPU
134 if (direction
== DMA_FROM_DEVICE
)
135 __dma_sync(dma_handle
, size
, direction
);
139 dma_direct_sync_single_for_device(struct device
*dev
,
140 dma_addr_t dma_handle
, size_t size
,
141 enum dma_data_direction direction
)
144 * It's pointless to invalidate the cache if the device isn't
145 * supposed to write to the relevant region
148 if (direction
== DMA_TO_DEVICE
)
149 __dma_sync(dma_handle
, size
, direction
);
153 dma_direct_sync_sg_for_cpu(struct device
*dev
,
154 struct scatterlist
*sgl
, int nents
,
155 enum dma_data_direction direction
)
157 struct scatterlist
*sg
;
160 /* FIXME this part of code is untested */
161 if (direction
== DMA_FROM_DEVICE
)
162 for_each_sg(sgl
, sg
, nents
, i
)
163 __dma_sync(sg
->dma_address
, sg
->length
, direction
);
167 dma_direct_sync_sg_for_device(struct device
*dev
,
168 struct scatterlist
*sgl
, int nents
,
169 enum dma_data_direction direction
)
171 struct scatterlist
*sg
;
174 /* FIXME this part of code is untested */
175 if (direction
== DMA_TO_DEVICE
)
176 for_each_sg(sgl
, sg
, nents
, i
)
177 __dma_sync(sg
->dma_address
, sg
->length
, direction
);
180 struct dma_map_ops dma_direct_ops
= {
181 .alloc
= dma_direct_alloc_coherent
,
182 .free
= dma_direct_free_coherent
,
183 .map_sg
= dma_direct_map_sg
,
184 .unmap_sg
= dma_direct_unmap_sg
,
185 .dma_supported
= dma_direct_dma_supported
,
186 .map_page
= dma_direct_map_page
,
187 .unmap_page
= dma_direct_unmap_page
,
188 .sync_single_for_cpu
= dma_direct_sync_single_for_cpu
,
189 .sync_single_for_device
= dma_direct_sync_single_for_device
,
190 .sync_sg_for_cpu
= dma_direct_sync_sg_for_cpu
,
191 .sync_sg_for_device
= dma_direct_sync_sg_for_device
,
193 EXPORT_SYMBOL(dma_direct_ops
);
195 /* Number of entries preallocated for DMA-API debugging */
196 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
198 static int __init
dma_init(void)
200 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES
);
204 fs_initcall(dma_init
);