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/dma-debug.h>
13 #include <asm/cacheflush.h>
16 * Generic direct DMA implementation
18 * This implementation supports a per-device offset that can be applied if
19 * the address at which memory is visible to devices is not 0. Platform code
20 * can set archdata.dma_data to an unsigned long holding the offset. By
21 * default the offset is PCI_DRAM_OFFSET.
23 static inline void __dma_sync_page(unsigned long paddr, unsigned long offset,
24 size_t size, enum dma_data_direction direction)
28 flush_dcache_range(paddr + offset, paddr + offset + size);
31 invalidate_dcache_range(paddr + offset, paddr + offset + size);
38 static unsigned long get_dma_direct_offset(struct device *dev)
41 return (unsigned long)dev->archdata.dma_data;
43 return PCI_DRAM_OFFSET; /* FIXME Not sure if is correct */
46 void *dma_direct_alloc_coherent(struct device *dev, size_t size,
47 dma_addr_t *dma_handle, gfp_t flag)
51 int node = dev_to_node(dev);
53 /* ignore region specifiers */
54 flag &= ~(__GFP_HIGHMEM);
56 page = alloc_pages_node(node, flag, get_order(size));
59 ret = page_address(page);
61 *dma_handle = virt_to_phys(ret) + get_dma_direct_offset(dev);
66 void dma_direct_free_coherent(struct device *dev, size_t size,
67 void *vaddr, dma_addr_t dma_handle)
69 free_pages((unsigned long)vaddr, get_order(size));
72 static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
73 int nents, enum dma_data_direction direction,
74 struct dma_attrs *attrs)
76 struct scatterlist *sg;
79 /* FIXME this part of code is untested */
80 for_each_sg(sgl, sg, nents, i) {
81 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
82 sg->dma_length = sg->length;
83 __dma_sync_page(page_to_phys(sg_page(sg)), sg->offset,
84 sg->length, direction);
90 static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
91 int nents, enum dma_data_direction direction,
92 struct dma_attrs *attrs)
96 static int dma_direct_dma_supported(struct device *dev, u64 mask)
101 static inline dma_addr_t dma_direct_map_page(struct device *dev,
103 unsigned long offset,
105 enum dma_data_direction direction,
106 struct dma_attrs *attrs)
108 BUG_ON(direction == DMA_NONE);
109 __dma_sync_page(page_to_phys(page), offset, size, direction);
110 return page_to_phys(page) + offset + get_dma_direct_offset(dev);
113 static inline void dma_direct_unmap_page(struct device *dev,
114 dma_addr_t dma_address,
116 enum dma_data_direction direction,
117 struct dma_attrs *attrs)
119 /* There is not necessary to do cache cleanup
121 * phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
122 * dma_address is physical address
124 __dma_sync_page((void *)dma_address, 0 , size, direction);
127 struct dma_map_ops dma_direct_ops = {
128 .alloc_coherent = dma_direct_alloc_coherent,
129 .free_coherent = dma_direct_free_coherent,
130 .map_sg = dma_direct_map_sg,
131 .unmap_sg = dma_direct_unmap_sg,
132 .dma_supported = dma_direct_dma_supported,
133 .map_page = dma_direct_map_page,
134 .unmap_page = dma_direct_unmap_page,
136 EXPORT_SYMBOL(dma_direct_ops);
138 /* Number of entries preallocated for DMA-API debugging */
139 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
141 static int __init dma_init(void)
143 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
147 fs_initcall(dma_init);