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>
11 #include <asm/abs_addr.h>
14 * Generic direct DMA implementation
16 * This implementation supports a per-device offset that can be applied if
17 * the address at which memory is visible to devices is not 0. Platform code
18 * can set archdata.dma_data to an unsigned long holding the offset. By
19 * default the offset is zero.
22 static unsigned long get_dma_direct_offset(struct device *dev)
24 return (unsigned long)dev->archdata.dma_data;
27 static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
28 dma_addr_t *dma_handle, gfp_t flag)
32 int node = dev_to_node(dev);
34 page = alloc_pages_node(node, flag, get_order(size));
37 ret = page_address(page);
39 *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
44 static void dma_direct_free_coherent(struct device *dev, size_t size,
45 void *vaddr, dma_addr_t dma_handle)
47 free_pages((unsigned long)vaddr, get_order(size));
50 static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
52 enum dma_data_direction direction,
53 struct dma_attrs *attrs)
55 return virt_to_abs(ptr) + get_dma_direct_offset(dev);
58 static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
60 enum dma_data_direction direction,
61 struct dma_attrs *attrs)
65 static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
66 int nents, enum dma_data_direction direction,
67 struct dma_attrs *attrs)
69 struct scatterlist *sg;
72 for_each_sg(sgl, sg, nents, i) {
73 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
74 sg->dma_length = sg->length;
80 static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
81 int nents, enum dma_data_direction direction,
82 struct dma_attrs *attrs)
86 static int dma_direct_dma_supported(struct device *dev, u64 mask)
88 /* Could be improved to check for memory though it better be
89 * done via some global so platforms can set the limit in case
90 * they have limited DMA windows
92 return mask >= DMA_32BIT_MASK;
95 struct dma_mapping_ops dma_direct_ops = {
96 .alloc_coherent = dma_direct_alloc_coherent,
97 .free_coherent = dma_direct_free_coherent,
98 .map_single = dma_direct_map_single,
99 .unmap_single = dma_direct_unmap_single,
100 .map_sg = dma_direct_map_sg,
101 .unmap_sg = dma_direct_unmap_sg,
102 .dma_supported = dma_direct_dma_supported,
104 EXPORT_SYMBOL(dma_direct_ops);