Merge branch 'x86-asm-generic-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / arch / powerpc / kernel / dma.c
1 /*
2  * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
3  *
4  * Provide default implementations of the DMA mapping callbacks for
5  * directly mapped busses.
6  */
7
8 #include <linux/device.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/lmb.h>
11 #include <asm/bug.h>
12 #include <asm/abs_addr.h>
13
14 /*
15  * Generic direct DMA implementation
16  *
17  * This implementation supports a per-device offset that can be applied if
18  * the address at which memory is visible to devices is not 0. Platform code
19  * can set archdata.dma_data to an unsigned long holding the offset. By
20  * default the offset is PCI_DRAM_OFFSET.
21  */
22
23 unsigned long get_dma_direct_offset(struct device *dev)
24 {
25         if (dev)
26                 return (unsigned long)dev->archdata.dma_data;
27
28         return PCI_DRAM_OFFSET;
29 }
30
31 void *dma_direct_alloc_coherent(struct device *dev, size_t size,
32                                 dma_addr_t *dma_handle, gfp_t flag)
33 {
34         void *ret;
35 #ifdef CONFIG_NOT_COHERENT_CACHE
36         ret = __dma_alloc_coherent(dev, size, dma_handle, flag);
37         if (ret == NULL)
38                 return NULL;
39         *dma_handle += get_dma_direct_offset(dev);
40         return ret;
41 #else
42         struct page *page;
43         int node = dev_to_node(dev);
44
45         /* ignore region specifiers */
46         flag  &= ~(__GFP_HIGHMEM);
47
48         page = alloc_pages_node(node, flag, get_order(size));
49         if (page == NULL)
50                 return NULL;
51         ret = page_address(page);
52         memset(ret, 0, size);
53         *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
54
55         return ret;
56 #endif
57 }
58
59 void dma_direct_free_coherent(struct device *dev, size_t size,
60                               void *vaddr, dma_addr_t dma_handle)
61 {
62 #ifdef CONFIG_NOT_COHERENT_CACHE
63         __dma_free_coherent(size, vaddr);
64 #else
65         free_pages((unsigned long)vaddr, get_order(size));
66 #endif
67 }
68
69 static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
70                              int nents, enum dma_data_direction direction,
71                              struct dma_attrs *attrs)
72 {
73         struct scatterlist *sg;
74         int i;
75
76         for_each_sg(sgl, sg, nents, i) {
77                 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
78                 sg->dma_length = sg->length;
79                 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
80         }
81
82         return nents;
83 }
84
85 static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
86                                 int nents, enum dma_data_direction direction,
87                                 struct dma_attrs *attrs)
88 {
89 }
90
91 static int dma_direct_dma_supported(struct device *dev, u64 mask)
92 {
93 #ifdef CONFIG_PPC64
94         /* Could be improved so platforms can set the limit in case
95          * they have limited DMA windows
96          */
97         return mask >= (lmb_end_of_DRAM() - 1);
98 #else
99         return 1;
100 #endif
101 }
102
103 static inline dma_addr_t dma_direct_map_page(struct device *dev,
104                                              struct page *page,
105                                              unsigned long offset,
106                                              size_t size,
107                                              enum dma_data_direction dir,
108                                              struct dma_attrs *attrs)
109 {
110         BUG_ON(dir == DMA_NONE);
111         __dma_sync_page(page, offset, size, dir);
112         return page_to_phys(page) + offset + get_dma_direct_offset(dev);
113 }
114
115 static inline void dma_direct_unmap_page(struct device *dev,
116                                          dma_addr_t dma_address,
117                                          size_t size,
118                                          enum dma_data_direction direction,
119                                          struct dma_attrs *attrs)
120 {
121 }
122
123 #ifdef CONFIG_NOT_COHERENT_CACHE
124 static inline void dma_direct_sync_sg(struct device *dev,
125                 struct scatterlist *sgl, int nents,
126                 enum dma_data_direction direction)
127 {
128         struct scatterlist *sg;
129         int i;
130
131         for_each_sg(sgl, sg, nents, i)
132                 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
133 }
134
135 static inline void dma_direct_sync_single_range(struct device *dev,
136                 dma_addr_t dma_handle, unsigned long offset, size_t size,
137                 enum dma_data_direction direction)
138 {
139         __dma_sync(bus_to_virt(dma_handle+offset), size, direction);
140 }
141 #endif
142
143 struct dma_mapping_ops dma_direct_ops = {
144         .alloc_coherent = dma_direct_alloc_coherent,
145         .free_coherent  = dma_direct_free_coherent,
146         .map_sg         = dma_direct_map_sg,
147         .unmap_sg       = dma_direct_unmap_sg,
148         .dma_supported  = dma_direct_dma_supported,
149         .map_page       = dma_direct_map_page,
150         .unmap_page     = dma_direct_unmap_page,
151 #ifdef CONFIG_NOT_COHERENT_CACHE
152         .sync_single_range_for_cpu      = dma_direct_sync_single_range,
153         .sync_single_range_for_device   = dma_direct_sync_single_range,
154         .sync_sg_for_cpu                = dma_direct_sync_sg,
155         .sync_sg_for_device             = dma_direct_sync_sg,
156 #endif
157 };
158 EXPORT_SYMBOL(dma_direct_ops);