dma-mapping: implement dmam_alloc_coherent using dmam_alloc_attrs
[linux-2.6-microblaze.git] / kernel / dma / mapping.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * arch-independent dma-mapping routines
4  *
5  * Copyright (c) 2006  SUSE Linux Products GmbH
6  * Copyright (c) 2006  Tejun Heo <teheo@suse.de>
7  */
8 #include <linux/memblock.h> /* for max_pfn */
9 #include <linux/acpi.h>
10 #include <linux/dma-direct.h>
11 #include <linux/dma-noncoherent.h>
12 #include <linux/export.h>
13 #include <linux/gfp.h>
14 #include <linux/of_device.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
17
18 /*
19  * Managed DMA API
20  */
21 struct dma_devres {
22         size_t          size;
23         void            *vaddr;
24         dma_addr_t      dma_handle;
25         unsigned long   attrs;
26 };
27
28 static void dmam_release(struct device *dev, void *res)
29 {
30         struct dma_devres *this = res;
31
32         dma_free_attrs(dev, this->size, this->vaddr, this->dma_handle,
33                         this->attrs);
34 }
35
36 static int dmam_match(struct device *dev, void *res, void *match_data)
37 {
38         struct dma_devres *this = res, *match = match_data;
39
40         if (this->vaddr == match->vaddr) {
41                 WARN_ON(this->size != match->size ||
42                         this->dma_handle != match->dma_handle);
43                 return 1;
44         }
45         return 0;
46 }
47
48 /**
49  * dmam_free_coherent - Managed dma_free_coherent()
50  * @dev: Device to free coherent memory for
51  * @size: Size of allocation
52  * @vaddr: Virtual address of the memory to free
53  * @dma_handle: DMA handle of the memory to free
54  *
55  * Managed dma_free_coherent().
56  */
57 void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
58                         dma_addr_t dma_handle)
59 {
60         struct dma_devres match_data = { size, vaddr, dma_handle };
61
62         dma_free_coherent(dev, size, vaddr, dma_handle);
63         WARN_ON(devres_destroy(dev, dmam_release, dmam_match, &match_data));
64 }
65 EXPORT_SYMBOL(dmam_free_coherent);
66
67 /**
68  * dmam_alloc_attrs - Managed dma_alloc_attrs()
69  * @dev: Device to allocate non_coherent memory for
70  * @size: Size of allocation
71  * @dma_handle: Out argument for allocated DMA handle
72  * @gfp: Allocation flags
73  * @attrs: Flags in the DMA_ATTR_* namespace.
74  *
75  * Managed dma_alloc_attrs().  Memory allocated using this function will be
76  * automatically released on driver detach.
77  *
78  * RETURNS:
79  * Pointer to allocated memory on success, NULL on failure.
80  */
81 void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
82                 gfp_t gfp, unsigned long attrs)
83 {
84         struct dma_devres *dr;
85         void *vaddr;
86
87         dr = devres_alloc(dmam_release, sizeof(*dr), gfp);
88         if (!dr)
89                 return NULL;
90
91         vaddr = dma_alloc_attrs(dev, size, dma_handle, gfp, attrs);
92         if (!vaddr) {
93                 devres_free(dr);
94                 return NULL;
95         }
96
97         dr->vaddr = vaddr;
98         dr->dma_handle = *dma_handle;
99         dr->size = size;
100         dr->attrs = attrs;
101
102         devres_add(dev, dr);
103
104         return vaddr;
105 }
106 EXPORT_SYMBOL(dmam_alloc_attrs);
107
108 #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
109
110 static void dmam_coherent_decl_release(struct device *dev, void *res)
111 {
112         dma_release_declared_memory(dev);
113 }
114
115 /**
116  * dmam_declare_coherent_memory - Managed dma_declare_coherent_memory()
117  * @dev: Device to declare coherent memory for
118  * @phys_addr: Physical address of coherent memory to be declared
119  * @device_addr: Device address of coherent memory to be declared
120  * @size: Size of coherent memory to be declared
121  * @flags: Flags
122  *
123  * Managed dma_declare_coherent_memory().
124  *
125  * RETURNS:
126  * 0 on success, -errno on failure.
127  */
128 int dmam_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
129                                  dma_addr_t device_addr, size_t size, int flags)
130 {
131         void *res;
132         int rc;
133
134         res = devres_alloc(dmam_coherent_decl_release, 0, GFP_KERNEL);
135         if (!res)
136                 return -ENOMEM;
137
138         rc = dma_declare_coherent_memory(dev, phys_addr, device_addr, size,
139                                          flags);
140         if (!rc)
141                 devres_add(dev, res);
142         else
143                 devres_free(res);
144
145         return rc;
146 }
147 EXPORT_SYMBOL(dmam_declare_coherent_memory);
148
149 /**
150  * dmam_release_declared_memory - Managed dma_release_declared_memory().
151  * @dev: Device to release declared coherent memory for
152  *
153  * Managed dmam_release_declared_memory().
154  */
155 void dmam_release_declared_memory(struct device *dev)
156 {
157         WARN_ON(devres_destroy(dev, dmam_coherent_decl_release, NULL, NULL));
158 }
159 EXPORT_SYMBOL(dmam_release_declared_memory);
160
161 #endif
162
163 /*
164  * Create scatter-list for the already allocated DMA buffer.
165  */
166 int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
167                  void *cpu_addr, dma_addr_t dma_addr, size_t size,
168                  unsigned long attrs)
169 {
170         struct page *page;
171         int ret;
172
173         if (!dev_is_dma_coherent(dev)) {
174                 if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN))
175                         return -ENXIO;
176
177                 page = pfn_to_page(arch_dma_coherent_to_pfn(dev, cpu_addr,
178                                 dma_addr));
179         } else {
180                 page = virt_to_page(cpu_addr);
181         }
182
183         ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
184         if (!ret)
185                 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
186         return ret;
187 }
188
189 int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
190                 void *cpu_addr, dma_addr_t dma_addr, size_t size,
191                 unsigned long attrs)
192 {
193         const struct dma_map_ops *ops = get_dma_ops(dev);
194
195         if (!dma_is_direct(ops) && ops->get_sgtable)
196                 return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
197                                         attrs);
198         return dma_common_get_sgtable(dev, sgt, cpu_addr, dma_addr, size,
199                         attrs);
200 }
201 EXPORT_SYMBOL(dma_get_sgtable_attrs);
202
203 /*
204  * Create userspace mapping for the DMA-coherent memory.
205  */
206 int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
207                 void *cpu_addr, dma_addr_t dma_addr, size_t size,
208                 unsigned long attrs)
209 {
210 #ifndef CONFIG_ARCH_NO_COHERENT_DMA_MMAP
211         unsigned long user_count = vma_pages(vma);
212         unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
213         unsigned long off = vma->vm_pgoff;
214         unsigned long pfn;
215         int ret = -ENXIO;
216
217         vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, attrs);
218
219         if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
220                 return ret;
221
222         if (off >= count || user_count > count - off)
223                 return -ENXIO;
224
225         if (!dev_is_dma_coherent(dev)) {
226                 if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN))
227                         return -ENXIO;
228                 pfn = arch_dma_coherent_to_pfn(dev, cpu_addr, dma_addr);
229         } else {
230                 pfn = page_to_pfn(virt_to_page(cpu_addr));
231         }
232
233         return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
234                         user_count << PAGE_SHIFT, vma->vm_page_prot);
235 #else
236         return -ENXIO;
237 #endif /* !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
238 }
239
240 /**
241  * dma_mmap_attrs - map a coherent DMA allocation into user space
242  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
243  * @vma: vm_area_struct describing requested user mapping
244  * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs
245  * @dma_addr: device-view address returned from dma_alloc_attrs
246  * @size: size of memory originally requested in dma_alloc_attrs
247  * @attrs: attributes of mapping properties requested in dma_alloc_attrs
248  *
249  * Map a coherent DMA buffer previously allocated by dma_alloc_attrs into user
250  * space.  The coherent DMA buffer must not be freed by the driver until the
251  * user space mapping has been released.
252  */
253 int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
254                 void *cpu_addr, dma_addr_t dma_addr, size_t size,
255                 unsigned long attrs)
256 {
257         const struct dma_map_ops *ops = get_dma_ops(dev);
258
259         if (!dma_is_direct(ops) && ops->mmap)
260                 return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
261         return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
262 }
263 EXPORT_SYMBOL(dma_mmap_attrs);
264
265 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
266 static u64 dma_default_get_required_mask(struct device *dev)
267 {
268         u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
269         u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
270         u64 mask;
271
272         if (!high_totalram) {
273                 /* convert to mask just covering totalram */
274                 low_totalram = (1 << (fls(low_totalram) - 1));
275                 low_totalram += low_totalram - 1;
276                 mask = low_totalram;
277         } else {
278                 high_totalram = (1 << (fls(high_totalram) - 1));
279                 high_totalram += high_totalram - 1;
280                 mask = (((u64)high_totalram) << 32) + 0xffffffff;
281         }
282         return mask;
283 }
284
285 u64 dma_get_required_mask(struct device *dev)
286 {
287         const struct dma_map_ops *ops = get_dma_ops(dev);
288
289         if (dma_is_direct(ops))
290                 return dma_direct_get_required_mask(dev);
291         if (ops->get_required_mask)
292                 return ops->get_required_mask(dev);
293         return dma_default_get_required_mask(dev);
294 }
295 EXPORT_SYMBOL_GPL(dma_get_required_mask);
296 #endif
297
298 #ifndef arch_dma_alloc_attrs
299 #define arch_dma_alloc_attrs(dev)       (true)
300 #endif
301
302 void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
303                 gfp_t flag, unsigned long attrs)
304 {
305         const struct dma_map_ops *ops = get_dma_ops(dev);
306         void *cpu_addr;
307
308         WARN_ON_ONCE(dev && !dev->coherent_dma_mask);
309
310         if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr))
311                 return cpu_addr;
312
313         /* let the implementation decide on the zone to allocate from: */
314         flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
315
316         if (!arch_dma_alloc_attrs(&dev))
317                 return NULL;
318
319         if (dma_is_direct(ops))
320                 cpu_addr = dma_direct_alloc(dev, size, dma_handle, flag, attrs);
321         else if (ops->alloc)
322                 cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
323         else
324                 return NULL;
325
326         debug_dma_alloc_coherent(dev, size, *dma_handle, cpu_addr);
327         return cpu_addr;
328 }
329 EXPORT_SYMBOL(dma_alloc_attrs);
330
331 void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
332                 dma_addr_t dma_handle, unsigned long attrs)
333 {
334         const struct dma_map_ops *ops = get_dma_ops(dev);
335
336         if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr))
337                 return;
338         /*
339          * On non-coherent platforms which implement DMA-coherent buffers via
340          * non-cacheable remaps, ops->free() may call vunmap(). Thus getting
341          * this far in IRQ context is a) at risk of a BUG_ON() or trying to
342          * sleep on some machines, and b) an indication that the driver is
343          * probably misusing the coherent API anyway.
344          */
345         WARN_ON(irqs_disabled());
346
347         if (!cpu_addr)
348                 return;
349
350         debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
351         if (dma_is_direct(ops))
352                 dma_direct_free(dev, size, cpu_addr, dma_handle, attrs);
353         else if (ops->free)
354                 ops->free(dev, size, cpu_addr, dma_handle, attrs);
355 }
356 EXPORT_SYMBOL(dma_free_attrs);
357
358 static inline void dma_check_mask(struct device *dev, u64 mask)
359 {
360         if (sme_active() && (mask < (((u64)sme_get_me_mask() << 1) - 1)))
361                 dev_warn(dev, "SME is active, device will require DMA bounce buffers\n");
362 }
363
364 int dma_supported(struct device *dev, u64 mask)
365 {
366         const struct dma_map_ops *ops = get_dma_ops(dev);
367
368         if (dma_is_direct(ops))
369                 return dma_direct_supported(dev, mask);
370         if (!ops->dma_supported)
371                 return 1;
372         return ops->dma_supported(dev, mask);
373 }
374 EXPORT_SYMBOL(dma_supported);
375
376 #ifndef HAVE_ARCH_DMA_SET_MASK
377 int dma_set_mask(struct device *dev, u64 mask)
378 {
379         if (!dev->dma_mask || !dma_supported(dev, mask))
380                 return -EIO;
381
382         dma_check_mask(dev, mask);
383         *dev->dma_mask = mask;
384         return 0;
385 }
386 EXPORT_SYMBOL(dma_set_mask);
387 #endif
388
389 #ifndef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
390 int dma_set_coherent_mask(struct device *dev, u64 mask)
391 {
392         if (!dma_supported(dev, mask))
393                 return -EIO;
394
395         dma_check_mask(dev, mask);
396         dev->coherent_dma_mask = mask;
397         return 0;
398 }
399 EXPORT_SYMBOL(dma_set_coherent_mask);
400 #endif
401
402 void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
403                 enum dma_data_direction dir)
404 {
405         const struct dma_map_ops *ops = get_dma_ops(dev);
406
407         BUG_ON(!valid_dma_direction(dir));
408
409         if (dma_is_direct(ops))
410                 arch_dma_cache_sync(dev, vaddr, size, dir);
411         else if (ops->cache_sync)
412                 ops->cache_sync(dev, vaddr, size, dir);
413 }
414 EXPORT_SYMBOL(dma_cache_sync);