]> Git Repo - linux.git/blob - arch/powerpc/kernel/dma.c
dma-mapping: implement dma_map_single_attrs using dma_map_page_attrs
[linux.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/dma-debug.h>
11 #include <linux/gfp.h>
12 #include <linux/memblock.h>
13 #include <linux/export.h>
14 #include <linux/pci.h>
15 #include <asm/vio.h>
16 #include <asm/bug.h>
17 #include <asm/machdep.h>
18 #include <asm/swiotlb.h>
19 #include <asm/iommu.h>
20
21 /*
22  * Generic direct DMA implementation
23  *
24  * This implementation supports a per-device offset that can be applied if
25  * the address at which memory is visible to devices is not 0. Platform code
26  * can set archdata.dma_data to an unsigned long holding the offset. By
27  * default the offset is PCI_DRAM_OFFSET.
28  */
29
30 static u64 __maybe_unused get_pfn_limit(struct device *dev)
31 {
32         u64 pfn = (dev->coherent_dma_mask >> PAGE_SHIFT) + 1;
33         struct dev_archdata __maybe_unused *sd = &dev->archdata;
34
35 #ifdef CONFIG_SWIOTLB
36         if (sd->max_direct_dma_addr && dev->dma_ops == &powerpc_swiotlb_dma_ops)
37                 pfn = min_t(u64, pfn, sd->max_direct_dma_addr >> PAGE_SHIFT);
38 #endif
39
40         return pfn;
41 }
42
43 static int dma_nommu_dma_supported(struct device *dev, u64 mask)
44 {
45 #ifdef CONFIG_PPC64
46         u64 limit = get_dma_offset(dev) + (memblock_end_of_DRAM() - 1);
47
48         /* Limit fits in the mask, we are good */
49         if (mask >= limit)
50                 return 1;
51
52 #ifdef CONFIG_FSL_SOC
53         /*
54          * Freescale gets another chance via ZONE_DMA, however
55          * that will have to be refined if/when they support iommus
56          */
57         return 1;
58 #endif
59         /* Sorry ... */
60         return 0;
61 #else
62         return 1;
63 #endif
64 }
65
66 #ifndef CONFIG_NOT_COHERENT_CACHE
67 void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
68                                   dma_addr_t *dma_handle, gfp_t flag,
69                                   unsigned long attrs)
70 {
71         void *ret;
72         struct page *page;
73         int node = dev_to_node(dev);
74 #ifdef CONFIG_FSL_SOC
75         u64 pfn = get_pfn_limit(dev);
76         int zone;
77
78         /*
79          * This code should be OK on other platforms, but we have drivers that
80          * don't set coherent_dma_mask. As a workaround we just ifdef it. This
81          * whole routine needs some serious cleanup.
82          */
83
84         zone = dma_pfn_limit_to_zone(pfn);
85         if (zone < 0) {
86                 dev_err(dev, "%s: No suitable zone for pfn %#llx\n",
87                         __func__, pfn);
88                 return NULL;
89         }
90
91         switch (zone) {
92 #ifdef CONFIG_ZONE_DMA
93         case ZONE_DMA:
94                 flag |= GFP_DMA;
95                 break;
96 #endif
97         };
98 #endif /* CONFIG_FSL_SOC */
99
100         page = alloc_pages_node(node, flag, get_order(size));
101         if (page == NULL)
102                 return NULL;
103         ret = page_address(page);
104         memset(ret, 0, size);
105         *dma_handle = __pa(ret) + get_dma_offset(dev);
106
107         return ret;
108 }
109
110 void __dma_nommu_free_coherent(struct device *dev, size_t size,
111                                 void *vaddr, dma_addr_t dma_handle,
112                                 unsigned long attrs)
113 {
114         free_pages((unsigned long)vaddr, get_order(size));
115 }
116 #endif /* !CONFIG_NOT_COHERENT_CACHE */
117
118 static void *dma_nommu_alloc_coherent(struct device *dev, size_t size,
119                                        dma_addr_t *dma_handle, gfp_t flag,
120                                        unsigned long attrs)
121 {
122         struct iommu_table *iommu;
123
124         /* The coherent mask may be smaller than the real mask, check if
125          * we can really use the direct ops
126          */
127         if (dma_nommu_dma_supported(dev, dev->coherent_dma_mask))
128                 return __dma_nommu_alloc_coherent(dev, size, dma_handle,
129                                                    flag, attrs);
130
131         /* Ok we can't ... do we have an iommu ? If not, fail */
132         iommu = get_iommu_table_base(dev);
133         if (!iommu)
134                 return NULL;
135
136         /* Try to use the iommu */
137         return iommu_alloc_coherent(dev, iommu, size, dma_handle,
138                                     dev->coherent_dma_mask, flag,
139                                     dev_to_node(dev));
140 }
141
142 static void dma_nommu_free_coherent(struct device *dev, size_t size,
143                                      void *vaddr, dma_addr_t dma_handle,
144                                      unsigned long attrs)
145 {
146         struct iommu_table *iommu;
147
148         /* See comments in dma_nommu_alloc_coherent() */
149         if (dma_nommu_dma_supported(dev, dev->coherent_dma_mask))
150                 return __dma_nommu_free_coherent(dev, size, vaddr, dma_handle,
151                                                   attrs);
152         /* Maybe we used an iommu ... */
153         iommu = get_iommu_table_base(dev);
154
155         /* If we hit that we should have never allocated in the first
156          * place so how come we are freeing ?
157          */
158         if (WARN_ON(!iommu))
159                 return;
160         iommu_free_coherent(iommu, size, vaddr, dma_handle);
161 }
162
163 int dma_nommu_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
164                              void *cpu_addr, dma_addr_t handle, size_t size,
165                              unsigned long attrs)
166 {
167         unsigned long pfn;
168
169 #ifdef CONFIG_NOT_COHERENT_CACHE
170         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
171         pfn = __dma_get_coherent_pfn((unsigned long)cpu_addr);
172 #else
173         pfn = page_to_pfn(virt_to_page(cpu_addr));
174 #endif
175         return remap_pfn_range(vma, vma->vm_start,
176                                pfn + vma->vm_pgoff,
177                                vma->vm_end - vma->vm_start,
178                                vma->vm_page_prot);
179 }
180
181 static int dma_nommu_map_sg(struct device *dev, struct scatterlist *sgl,
182                              int nents, enum dma_data_direction direction,
183                              unsigned long attrs)
184 {
185         struct scatterlist *sg;
186         int i;
187
188         for_each_sg(sgl, sg, nents, i) {
189                 sg->dma_address = sg_phys(sg) + get_dma_offset(dev);
190                 sg->dma_length = sg->length;
191
192                 if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
193                         continue;
194
195                 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
196         }
197
198         return nents;
199 }
200
201 static void dma_nommu_unmap_sg(struct device *dev, struct scatterlist *sgl,
202                                 int nents, enum dma_data_direction direction,
203                                 unsigned long attrs)
204 {
205         struct scatterlist *sg;
206         int i;
207
208         for_each_sg(sgl, sg, nents, i)
209                 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
210 }
211
212 static u64 dma_nommu_get_required_mask(struct device *dev)
213 {
214         u64 end, mask;
215
216         end = memblock_end_of_DRAM() + get_dma_offset(dev);
217
218         mask = 1ULL << (fls64(end) - 1);
219         mask += mask - 1;
220
221         return mask;
222 }
223
224 static inline dma_addr_t dma_nommu_map_page(struct device *dev,
225                                              struct page *page,
226                                              unsigned long offset,
227                                              size_t size,
228                                              enum dma_data_direction dir,
229                                              unsigned long attrs)
230 {
231         if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
232                 __dma_sync_page(page, offset, size, dir);
233
234         return page_to_phys(page) + offset + get_dma_offset(dev);
235 }
236
237 static inline void dma_nommu_unmap_page(struct device *dev,
238                                          dma_addr_t dma_address,
239                                          size_t size,
240                                          enum dma_data_direction direction,
241                                          unsigned long attrs)
242 {
243         if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
244                 __dma_sync(bus_to_virt(dma_address), size, direction);
245 }
246
247 #ifdef CONFIG_NOT_COHERENT_CACHE
248 static inline void dma_nommu_sync_sg(struct device *dev,
249                 struct scatterlist *sgl, int nents,
250                 enum dma_data_direction direction)
251 {
252         struct scatterlist *sg;
253         int i;
254
255         for_each_sg(sgl, sg, nents, i)
256                 __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
257 }
258
259 static inline void dma_nommu_sync_single(struct device *dev,
260                                           dma_addr_t dma_handle, size_t size,
261                                           enum dma_data_direction direction)
262 {
263         __dma_sync(bus_to_virt(dma_handle), size, direction);
264 }
265 #endif
266
267 const struct dma_map_ops dma_nommu_ops = {
268         .alloc                          = dma_nommu_alloc_coherent,
269         .free                           = dma_nommu_free_coherent,
270         .mmap                           = dma_nommu_mmap_coherent,
271         .map_sg                         = dma_nommu_map_sg,
272         .unmap_sg                       = dma_nommu_unmap_sg,
273         .dma_supported                  = dma_nommu_dma_supported,
274         .map_page                       = dma_nommu_map_page,
275         .unmap_page                     = dma_nommu_unmap_page,
276         .get_required_mask              = dma_nommu_get_required_mask,
277 #ifdef CONFIG_NOT_COHERENT_CACHE
278         .sync_single_for_cpu            = dma_nommu_sync_single,
279         .sync_single_for_device         = dma_nommu_sync_single,
280         .sync_sg_for_cpu                = dma_nommu_sync_sg,
281         .sync_sg_for_device             = dma_nommu_sync_sg,
282 #endif
283 };
284 EXPORT_SYMBOL(dma_nommu_ops);
285
286 int dma_set_coherent_mask(struct device *dev, u64 mask)
287 {
288         if (!dma_supported(dev, mask)) {
289                 /*
290                  * We need to special case the direct DMA ops which can
291                  * support a fallback for coherent allocations. There
292                  * is no dma_op->set_coherent_mask() so we have to do
293                  * things the hard way:
294                  */
295                 if (get_dma_ops(dev) != &dma_nommu_ops ||
296                     get_iommu_table_base(dev) == NULL ||
297                     !dma_iommu_dma_supported(dev, mask))
298                         return -EIO;
299         }
300         dev->coherent_dma_mask = mask;
301         return 0;
302 }
303 EXPORT_SYMBOL(dma_set_coherent_mask);
304
305 int dma_set_mask(struct device *dev, u64 dma_mask)
306 {
307         if (ppc_md.dma_set_mask)
308                 return ppc_md.dma_set_mask(dev, dma_mask);
309
310         if (dev_is_pci(dev)) {
311                 struct pci_dev *pdev = to_pci_dev(dev);
312                 struct pci_controller *phb = pci_bus_to_host(pdev->bus);
313                 if (phb->controller_ops.dma_set_mask)
314                         return phb->controller_ops.dma_set_mask(pdev, dma_mask);
315         }
316
317         if (!dev->dma_mask || !dma_supported(dev, dma_mask))
318                 return -EIO;
319         *dev->dma_mask = dma_mask;
320         return 0;
321 }
322 EXPORT_SYMBOL(dma_set_mask);
323
324 u64 __dma_get_required_mask(struct device *dev)
325 {
326         const struct dma_map_ops *dma_ops = get_dma_ops(dev);
327
328         if (unlikely(dma_ops == NULL))
329                 return 0;
330
331         if (dma_ops->get_required_mask)
332                 return dma_ops->get_required_mask(dev);
333
334         return DMA_BIT_MASK(8 * sizeof(dma_addr_t));
335 }
336
337 u64 dma_get_required_mask(struct device *dev)
338 {
339         if (ppc_md.dma_get_required_mask)
340                 return ppc_md.dma_get_required_mask(dev);
341
342         if (dev_is_pci(dev)) {
343                 struct pci_dev *pdev = to_pci_dev(dev);
344                 struct pci_controller *phb = pci_bus_to_host(pdev->bus);
345                 if (phb->controller_ops.dma_get_required_mask)
346                         return phb->controller_ops.dma_get_required_mask(pdev);
347         }
348
349         return __dma_get_required_mask(dev);
350 }
351 EXPORT_SYMBOL_GPL(dma_get_required_mask);
352
353 static int __init dma_init(void)
354 {
355 #ifdef CONFIG_IBMVIO
356         dma_debug_add_bus(&vio_bus_type);
357 #endif
358
359        return 0;
360 }
361 fs_initcall(dma_init);
362
This page took 0.050418 seconds and 4 git commands to generate.