kvm tools, setup: Create private directory
[linux-2.6/next.git] / arch / microblaze / kernel / dma.c
blob393e6b2db688545aac0a1784f25ad51d73d89567
1 /*
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.
7 */
9 #include <linux/device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/gfp.h>
12 #include <linux/dma-debug.h>
13 #include <asm/bug.h>
14 #include <asm/cacheflush.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.
24 static inline void __dma_sync_page(unsigned long paddr, unsigned long offset,
25 size_t size, enum dma_data_direction direction)
27 switch (direction) {
28 case DMA_TO_DEVICE:
29 case DMA_BIDIRECTIONAL:
30 flush_dcache_range(paddr + offset, paddr + offset + size);
31 break;
32 case DMA_FROM_DEVICE:
33 invalidate_dcache_range(paddr + offset, paddr + offset + size);
34 break;
35 default:
36 BUG();
40 static unsigned long get_dma_direct_offset(struct device *dev)
42 if (likely(dev))
43 return (unsigned long)dev->archdata.dma_data;
45 return PCI_DRAM_OFFSET; /* FIXME Not sure if is correct */
48 #define NOT_COHERENT_CACHE
50 static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
51 dma_addr_t *dma_handle, gfp_t flag)
53 #ifdef NOT_COHERENT_CACHE
54 return consistent_alloc(flag, size, dma_handle);
55 #else
56 void *ret;
57 struct page *page;
58 int node = dev_to_node(dev);
60 /* ignore region specifiers */
61 flag &= ~(__GFP_HIGHMEM);
63 page = alloc_pages_node(node, flag, get_order(size));
64 if (page == NULL)
65 return NULL;
66 ret = page_address(page);
67 memset(ret, 0, size);
68 *dma_handle = virt_to_phys(ret) + get_dma_direct_offset(dev);
70 return ret;
71 #endif
74 static void dma_direct_free_coherent(struct device *dev, size_t size,
75 void *vaddr, dma_addr_t dma_handle)
77 #ifdef NOT_COHERENT_CACHE
78 consistent_free(size, vaddr);
79 #else
80 free_pages((unsigned long)vaddr, get_order(size));
81 #endif
84 static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
85 int nents, enum dma_data_direction direction,
86 struct dma_attrs *attrs)
88 struct scatterlist *sg;
89 int i;
91 /* FIXME this part of code is untested */
92 for_each_sg(sgl, sg, nents, i) {
93 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
94 __dma_sync_page(page_to_phys(sg_page(sg)), sg->offset,
95 sg->length, direction);
98 return nents;
101 static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
102 int nents, enum dma_data_direction direction,
103 struct dma_attrs *attrs)
107 static int dma_direct_dma_supported(struct device *dev, u64 mask)
109 return 1;
112 static inline dma_addr_t dma_direct_map_page(struct device *dev,
113 struct page *page,
114 unsigned long offset,
115 size_t size,
116 enum dma_data_direction direction,
117 struct dma_attrs *attrs)
119 __dma_sync_page(page_to_phys(page), offset, size, direction);
120 return page_to_phys(page) + offset + get_dma_direct_offset(dev);
123 static inline void dma_direct_unmap_page(struct device *dev,
124 dma_addr_t dma_address,
125 size_t size,
126 enum dma_data_direction direction,
127 struct dma_attrs *attrs)
129 /* There is not necessary to do cache cleanup
131 * phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
132 * dma_address is physical address
134 __dma_sync_page(dma_address, 0 , size, direction);
137 struct dma_map_ops dma_direct_ops = {
138 .alloc_coherent = dma_direct_alloc_coherent,
139 .free_coherent = dma_direct_free_coherent,
140 .map_sg = dma_direct_map_sg,
141 .unmap_sg = dma_direct_unmap_sg,
142 .dma_supported = dma_direct_dma_supported,
143 .map_page = dma_direct_map_page,
144 .unmap_page = dma_direct_unmap_page,
146 EXPORT_SYMBOL(dma_direct_ops);
148 /* Number of entries preallocated for DMA-API debugging */
149 #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
151 static int __init dma_init(void)
153 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
155 return 0;
157 fs_initcall(dma_init);