1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Helpers for DMA ops implementations. These generally rely on the fact that
4 * the allocated memory contains normal pages in the direct kernel mapping.
5 */
6 #include <linux/dma-map-ops.h>
7
dma_common_vaddr_to_page(void * cpu_addr)8 static struct page *dma_common_vaddr_to_page(void *cpu_addr)
9 {
10 if (is_vmalloc_addr(cpu_addr))
11 return vmalloc_to_page(cpu_addr);
12 return virt_to_page(cpu_addr);
13 }
14
15 /*
16 * Create scatter-list for the already allocated DMA buffer.
17 */
dma_common_get_sgtable(struct device * dev,struct sg_table * sgt,void * cpu_addr,dma_addr_t dma_addr,size_t size,unsigned long attrs)18 int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
19 void *cpu_addr, dma_addr_t dma_addr, size_t size,
20 unsigned long attrs)
21 {
22 struct page *page = dma_common_vaddr_to_page(cpu_addr);
23 int ret;
24
25 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
26 if (!ret)
27 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
28 return ret;
29 }
30 EXPORT_SYMBOL_GPL(dma_common_get_sgtable);
31
32 /*
33 * Create userspace mapping for the DMA-coherent memory.
34 */
dma_common_mmap(struct device * dev,struct vm_area_struct * vma,void * cpu_addr,dma_addr_t dma_addr,size_t size,unsigned long attrs)35 int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
36 void *cpu_addr, dma_addr_t dma_addr, size_t size,
37 unsigned long attrs)
38 {
39 #ifdef CONFIG_MMU
40 unsigned long user_count = vma_pages(vma);
41 unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
42 unsigned long off = vma->vm_pgoff;
43 struct page *page = dma_common_vaddr_to_page(cpu_addr);
44 int ret = -ENXIO;
45
46 vma->vm_page_prot = dma_pgprot(dev, vma->vm_page_prot, attrs);
47
48 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
49 return ret;
50
51 if (off >= count || user_count > count - off)
52 return -ENXIO;
53
54 return remap_pfn_range(vma, vma->vm_start,
55 page_to_pfn(page) + vma->vm_pgoff,
56 user_count << PAGE_SHIFT, vma->vm_page_prot);
57 #else
58 return -ENXIO;
59 #endif /* CONFIG_MMU */
60 }
61 EXPORT_SYMBOL_GPL(dma_common_mmap);
62
dma_common_alloc_pages(struct device * dev,size_t size,dma_addr_t * dma_handle,enum dma_data_direction dir,gfp_t gfp)63 struct page *dma_common_alloc_pages(struct device *dev, size_t size,
64 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
65 {
66 const struct dma_map_ops *ops = get_dma_ops(dev);
67 struct page *page;
68
69 page = dma_alloc_contiguous(dev, size, gfp);
70 if (!page)
71 page = alloc_pages_node(dev_to_node(dev), gfp, get_order(size));
72 if (!page)
73 return NULL;
74
75 *dma_handle = ops->map_page(dev, page, 0, size, dir,
76 DMA_ATTR_SKIP_CPU_SYNC);
77 if (*dma_handle == DMA_MAPPING_ERROR) {
78 dma_free_contiguous(dev, page, size);
79 return NULL;
80 }
81
82 memset(page_address(page), 0, size);
83 return page;
84 }
85
dma_common_free_pages(struct device * dev,size_t size,struct page * page,dma_addr_t dma_handle,enum dma_data_direction dir)86 void dma_common_free_pages(struct device *dev, size_t size, struct page *page,
87 dma_addr_t dma_handle, enum dma_data_direction dir)
88 {
89 const struct dma_map_ops *ops = get_dma_ops(dev);
90
91 if (ops->unmap_page)
92 ops->unmap_page(dev, dma_handle, size, dir,
93 DMA_ATTR_SKIP_CPU_SYNC);
94 dma_free_contiguous(dev, page, size);
95 }
96