2 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
4 * Provide default implementations of the DMA mapping callbacks for
5 * directly mapped busses.
8 #include <linux/device.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/dma-debug.h>
11 #include <linux/gfp.h>
12 #include <linux/memblock.h>
13 #include <linux/export.h>
14 #include <linux/pci.h>
17 #include <asm/abs_addr.h>
18 #include <asm/machdep.h>
21 * Generic direct DMA implementation
23 * This implementation supports a per-device offset that can be applied if
24 * the address at which memory is visible to devices is not 0. Platform code
25 * can set archdata.dma_data to an unsigned long holding the offset. By
26 * default the offset is PCI_DRAM_OFFSET.
30 void *dma_direct_alloc_coherent(struct device
*dev
, size_t size
,
31 dma_addr_t
*dma_handle
, gfp_t flag
,
32 struct dma_attrs
*attrs
)
35 #ifdef CONFIG_NOT_COHERENT_CACHE
36 ret
= __dma_alloc_coherent(dev
, size
, dma_handle
, flag
);
39 *dma_handle
+= get_dma_offset(dev
);
43 int node
= dev_to_node(dev
);
45 /* ignore region specifiers */
46 flag
&= ~(__GFP_HIGHMEM
);
48 page
= alloc_pages_node(node
, flag
, get_order(size
));
51 ret
= page_address(page
);
53 *dma_handle
= virt_to_abs(ret
) + get_dma_offset(dev
);
59 void dma_direct_free_coherent(struct device
*dev
, size_t size
,
60 void *vaddr
, dma_addr_t dma_handle
,
61 struct dma_attrs
*attrs
)
63 #ifdef CONFIG_NOT_COHERENT_CACHE
64 __dma_free_coherent(size
, vaddr
);
66 free_pages((unsigned long)vaddr
, get_order(size
));
70 int dma_direct_mmap_coherent(struct device
*dev
, struct vm_area_struct
*vma
,
71 void *cpu_addr
, dma_addr_t handle
, size_t size
,
72 struct dma_attrs
*attrs
)
76 #ifdef CONFIG_NOT_COHERENT_CACHE
77 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
78 pfn
= __dma_get_coherent_pfn((unsigned long)cpu_addr
);
80 pfn
= page_to_pfn(virt_to_page(cpu_addr
));
82 return remap_pfn_range(vma
, vma
->vm_start
,
84 vma
->vm_end
- vma
->vm_start
,
88 static int dma_direct_map_sg(struct device
*dev
, struct scatterlist
*sgl
,
89 int nents
, enum dma_data_direction direction
,
90 struct dma_attrs
*attrs
)
92 struct scatterlist
*sg
;
95 for_each_sg(sgl
, sg
, nents
, i
) {
96 sg
->dma_address
= sg_phys(sg
) + get_dma_offset(dev
);
97 sg
->dma_length
= sg
->length
;
98 __dma_sync_page(sg_page(sg
), sg
->offset
, sg
->length
, direction
);
104 static void dma_direct_unmap_sg(struct device
*dev
, struct scatterlist
*sg
,
105 int nents
, enum dma_data_direction direction
,
106 struct dma_attrs
*attrs
)
110 static int dma_direct_dma_supported(struct device
*dev
, u64 mask
)
113 /* Could be improved so platforms can set the limit in case
114 * they have limited DMA windows
116 return mask
>= get_dma_offset(dev
) + (memblock_end_of_DRAM() - 1);
122 static u64
dma_direct_get_required_mask(struct device
*dev
)
126 end
= memblock_end_of_DRAM() + get_dma_offset(dev
);
128 mask
= 1ULL << (fls64(end
) - 1);
134 static inline dma_addr_t
dma_direct_map_page(struct device
*dev
,
136 unsigned long offset
,
138 enum dma_data_direction dir
,
139 struct dma_attrs
*attrs
)
141 BUG_ON(dir
== DMA_NONE
);
142 __dma_sync_page(page
, offset
, size
, dir
);
143 return page_to_phys(page
) + offset
+ get_dma_offset(dev
);
146 static inline void dma_direct_unmap_page(struct device
*dev
,
147 dma_addr_t dma_address
,
149 enum dma_data_direction direction
,
150 struct dma_attrs
*attrs
)
154 #ifdef CONFIG_NOT_COHERENT_CACHE
155 static inline void dma_direct_sync_sg(struct device
*dev
,
156 struct scatterlist
*sgl
, int nents
,
157 enum dma_data_direction direction
)
159 struct scatterlist
*sg
;
162 for_each_sg(sgl
, sg
, nents
, i
)
163 __dma_sync_page(sg_page(sg
), sg
->offset
, sg
->length
, direction
);
166 static inline void dma_direct_sync_single(struct device
*dev
,
167 dma_addr_t dma_handle
, size_t size
,
168 enum dma_data_direction direction
)
170 __dma_sync(bus_to_virt(dma_handle
), size
, direction
);
174 struct dma_map_ops dma_direct_ops
= {
175 .alloc
= dma_direct_alloc_coherent
,
176 .free
= dma_direct_free_coherent
,
177 .mmap
= dma_direct_mmap_coherent
,
178 .map_sg
= dma_direct_map_sg
,
179 .unmap_sg
= dma_direct_unmap_sg
,
180 .dma_supported
= dma_direct_dma_supported
,
181 .map_page
= dma_direct_map_page
,
182 .unmap_page
= dma_direct_unmap_page
,
183 .get_required_mask
= dma_direct_get_required_mask
,
184 #ifdef CONFIG_NOT_COHERENT_CACHE
185 .sync_single_for_cpu
= dma_direct_sync_single
,
186 .sync_single_for_device
= dma_direct_sync_single
,
187 .sync_sg_for_cpu
= dma_direct_sync_sg
,
188 .sync_sg_for_device
= dma_direct_sync_sg
,
191 EXPORT_SYMBOL(dma_direct_ops
);
193 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
195 int dma_set_mask(struct device
*dev
, u64 dma_mask
)
197 struct dma_map_ops
*dma_ops
= get_dma_ops(dev
);
199 if (ppc_md
.dma_set_mask
)
200 return ppc_md
.dma_set_mask(dev
, dma_mask
);
201 if ((dma_ops
!= NULL
) && (dma_ops
->set_dma_mask
!= NULL
))
202 return dma_ops
->set_dma_mask(dev
, dma_mask
);
203 if (!dev
->dma_mask
|| !dma_supported(dev
, dma_mask
))
205 *dev
->dma_mask
= dma_mask
;
208 EXPORT_SYMBOL(dma_set_mask
);
210 u64
dma_get_required_mask(struct device
*dev
)
212 struct dma_map_ops
*dma_ops
= get_dma_ops(dev
);
214 if (ppc_md
.dma_get_required_mask
)
215 return ppc_md
.dma_get_required_mask(dev
);
217 if (unlikely(dma_ops
== NULL
))
220 if (dma_ops
->get_required_mask
)
221 return dma_ops
->get_required_mask(dev
);
223 return DMA_BIT_MASK(8 * sizeof(dma_addr_t
));
225 EXPORT_SYMBOL_GPL(dma_get_required_mask
);
227 static int __init
dma_init(void)
229 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES
);
231 dma_debug_add_bus(&pci_bus_type
);
234 dma_debug_add_bus(&vio_bus_type
);
239 fs_initcall(dma_init
);