]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/kernel/dma_64.c
powerpc/dma: implement new dma_*map*_attrs() interfaces
[linux-2.6-omap-h63xx.git] / arch / powerpc / kernel / dma_64.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 and busses using the iommu infrastructure
6  */
7
8 #include <linux/device.h>
9 #include <linux/dma-mapping.h>
10 #include <asm/bug.h>
11 #include <asm/iommu.h>
12 #include <asm/abs_addr.h>
13
14 /*
15  * Generic iommu implementation
16  */
17
18 static inline unsigned long device_to_mask(struct device *dev)
19 {
20         if (dev->dma_mask && *dev->dma_mask)
21                 return *dev->dma_mask;
22         /* Assume devices without mask can take 32 bit addresses */
23         return 0xfffffffful;
24 }
25
26
27 /* Allocates a contiguous real buffer and creates mappings over it.
28  * Returns the virtual address of the buffer and sets dma_handle
29  * to the dma address (mapping) of the first page.
30  */
31 static void *dma_iommu_alloc_coherent(struct device *dev, size_t size,
32                                       dma_addr_t *dma_handle, gfp_t flag)
33 {
34         return iommu_alloc_coherent(dev, dev->archdata.dma_data, size,
35                                     dma_handle, device_to_mask(dev), flag,
36                                     dev->archdata.numa_node);
37 }
38
39 static void dma_iommu_free_coherent(struct device *dev, size_t size,
40                                     void *vaddr, dma_addr_t dma_handle)
41 {
42         iommu_free_coherent(dev->archdata.dma_data, size, vaddr, dma_handle);
43 }
44
45 /* Creates TCEs for a user provided buffer.  The user buffer must be
46  * contiguous real kernel storage (not vmalloc).  The address of the buffer
47  * passed here is the kernel (virtual) address of the buffer.  The buffer
48  * need not be page aligned, the dma_addr_t returned will point to the same
49  * byte within the page as vaddr.
50  */
51 static dma_addr_t dma_iommu_map_single(struct device *dev, void *vaddr,
52                                        size_t size,
53                                        enum dma_data_direction direction,
54                                        struct dma_attrs *attrs)
55 {
56         return iommu_map_single(dev, dev->archdata.dma_data, vaddr, size,
57                                 device_to_mask(dev), direction, attrs);
58 }
59
60
61 static void dma_iommu_unmap_single(struct device *dev, dma_addr_t dma_handle,
62                                    size_t size,
63                                    enum dma_data_direction direction,
64                                    struct dma_attrs *attrs)
65 {
66         iommu_unmap_single(dev->archdata.dma_data, dma_handle, size, direction,
67                            attrs);
68 }
69
70
71 static int dma_iommu_map_sg(struct device *dev, struct scatterlist *sglist,
72                             int nelems, enum dma_data_direction direction,
73                             struct dma_attrs *attrs)
74 {
75         return iommu_map_sg(dev, dev->archdata.dma_data, sglist, nelems,
76                             device_to_mask(dev), direction, attrs);
77 }
78
79 static void dma_iommu_unmap_sg(struct device *dev, struct scatterlist *sglist,
80                 int nelems, enum dma_data_direction direction,
81                 struct dma_attrs *attrs)
82 {
83         iommu_unmap_sg(dev->archdata.dma_data, sglist, nelems, direction,
84                        attrs);
85 }
86
87 /* We support DMA to/from any memory page via the iommu */
88 static int dma_iommu_dma_supported(struct device *dev, u64 mask)
89 {
90         struct iommu_table *tbl = dev->archdata.dma_data;
91
92         if (!tbl || tbl->it_offset > mask) {
93                 printk(KERN_INFO
94                        "Warning: IOMMU offset too big for device mask\n");
95                 if (tbl)
96                         printk(KERN_INFO
97                                "mask: 0x%08lx, table offset: 0x%08lx\n",
98                                 mask, tbl->it_offset);
99                 else
100                         printk(KERN_INFO "mask: 0x%08lx, table unavailable\n",
101                                 mask);
102                 return 0;
103         } else
104                 return 1;
105 }
106
107 struct dma_mapping_ops dma_iommu_ops = {
108         .alloc_coherent = dma_iommu_alloc_coherent,
109         .free_coherent  = dma_iommu_free_coherent,
110         .map_single     = dma_iommu_map_single,
111         .unmap_single   = dma_iommu_unmap_single,
112         .map_sg         = dma_iommu_map_sg,
113         .unmap_sg       = dma_iommu_unmap_sg,
114         .dma_supported  = dma_iommu_dma_supported,
115 };
116 EXPORT_SYMBOL(dma_iommu_ops);
117
118 /*
119  * Generic direct DMA implementation
120  *
121  * This implementation supports a per-device offset that can be applied if
122  * the address at which memory is visible to devices is not 0. Platform code
123  * can set archdata.dma_data to an unsigned long holding the offset. By
124  * default the offset is zero.
125  */
126
127 static unsigned long get_dma_direct_offset(struct device *dev)
128 {
129         return (unsigned long)dev->archdata.dma_data;
130 }
131
132 static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
133                                        dma_addr_t *dma_handle, gfp_t flag)
134 {
135         struct page *page;
136         void *ret;
137         int node = dev->archdata.numa_node;
138
139         page = alloc_pages_node(node, flag, get_order(size));
140         if (page == NULL)
141                 return NULL;
142         ret = page_address(page);
143         memset(ret, 0, size);
144         *dma_handle = virt_to_abs(ret) + get_dma_direct_offset(dev);
145
146         return ret;
147 }
148
149 static void dma_direct_free_coherent(struct device *dev, size_t size,
150                                      void *vaddr, dma_addr_t dma_handle)
151 {
152         free_pages((unsigned long)vaddr, get_order(size));
153 }
154
155 static dma_addr_t dma_direct_map_single(struct device *dev, void *ptr,
156                                         size_t size,
157                                         enum dma_data_direction direction,
158                                         struct dma_attrs *attrs)
159 {
160         return virt_to_abs(ptr) + get_dma_direct_offset(dev);
161 }
162
163 static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
164                                     size_t size,
165                                     enum dma_data_direction direction,
166                                     struct dma_attrs *attrs)
167 {
168 }
169
170 static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
171                              int nents, enum dma_data_direction direction,
172                              struct dma_attrs *attrs)
173 {
174         struct scatterlist *sg;
175         int i;
176
177         for_each_sg(sgl, sg, nents, i) {
178                 sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
179                 sg->dma_length = sg->length;
180         }
181
182         return nents;
183 }
184
185 static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
186                                 int nents, enum dma_data_direction direction,
187                                 struct dma_attrs *attrs)
188 {
189 }
190
191 static int dma_direct_dma_supported(struct device *dev, u64 mask)
192 {
193         /* Could be improved to check for memory though it better be
194          * done via some global so platforms can set the limit in case
195          * they have limited DMA windows
196          */
197         return mask >= DMA_32BIT_MASK;
198 }
199
200 struct dma_mapping_ops dma_direct_ops = {
201         .alloc_coherent = dma_direct_alloc_coherent,
202         .free_coherent  = dma_direct_free_coherent,
203         .map_single     = dma_direct_map_single,
204         .unmap_single   = dma_direct_unmap_single,
205         .map_sg         = dma_direct_map_sg,
206         .unmap_sg       = dma_direct_unmap_sg,
207         .dma_supported  = dma_direct_dma_supported,
208 };
209 EXPORT_SYMBOL(dma_direct_ops);