xref: /openbmc/linux/kernel/dma/remap.c (revision 176f011b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2012 ARM Ltd.
4  * Copyright (c) 2014 The Linux Foundation
5  */
6 #include <linux/dma-direct.h>
7 #include <linux/dma-noncoherent.h>
8 #include <linux/dma-contiguous.h>
9 #include <linux/init.h>
10 #include <linux/genalloc.h>
11 #include <linux/slab.h>
12 #include <linux/vmalloc.h>
13 
14 static struct vm_struct *__dma_common_pages_remap(struct page **pages,
15 			size_t size, unsigned long vm_flags, pgprot_t prot,
16 			const void *caller)
17 {
18 	struct vm_struct *area;
19 
20 	area = get_vm_area_caller(size, vm_flags, caller);
21 	if (!area)
22 		return NULL;
23 
24 	if (map_vm_area(area, prot, pages)) {
25 		vunmap(area->addr);
26 		return NULL;
27 	}
28 
29 	return area;
30 }
31 
32 /*
33  * Remaps an array of PAGE_SIZE pages into another vm_area.
34  * Cannot be used in non-sleeping contexts
35  */
36 void *dma_common_pages_remap(struct page **pages, size_t size,
37 			unsigned long vm_flags, pgprot_t prot,
38 			const void *caller)
39 {
40 	struct vm_struct *area;
41 
42 	area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller);
43 	if (!area)
44 		return NULL;
45 
46 	area->pages = pages;
47 
48 	return area->addr;
49 }
50 
51 /*
52  * Remaps an allocated contiguous region into another vm_area.
53  * Cannot be used in non-sleeping contexts
54  */
55 void *dma_common_contiguous_remap(struct page *page, size_t size,
56 			unsigned long vm_flags,
57 			pgprot_t prot, const void *caller)
58 {
59 	int i;
60 	struct page **pages;
61 	struct vm_struct *area;
62 
63 	pages = kmalloc(sizeof(struct page *) << get_order(size), GFP_KERNEL);
64 	if (!pages)
65 		return NULL;
66 
67 	for (i = 0; i < (size >> PAGE_SHIFT); i++)
68 		pages[i] = nth_page(page, i);
69 
70 	area = __dma_common_pages_remap(pages, size, vm_flags, prot, caller);
71 
72 	kfree(pages);
73 
74 	if (!area)
75 		return NULL;
76 	return area->addr;
77 }
78 
79 /*
80  * Unmaps a range previously mapped by dma_common_*_remap
81  */
82 void dma_common_free_remap(void *cpu_addr, size_t size, unsigned long vm_flags)
83 {
84 	struct vm_struct *area = find_vm_area(cpu_addr);
85 
86 	if (!area || (area->flags & vm_flags) != vm_flags) {
87 		WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
88 		return;
89 	}
90 
91 	unmap_kernel_range((unsigned long)cpu_addr, PAGE_ALIGN(size));
92 	vunmap(cpu_addr);
93 }
94 
95 #ifdef CONFIG_DMA_DIRECT_REMAP
96 static struct gen_pool *atomic_pool __ro_after_init;
97 
98 #define DEFAULT_DMA_COHERENT_POOL_SIZE  SZ_256K
99 static size_t atomic_pool_size __initdata = DEFAULT_DMA_COHERENT_POOL_SIZE;
100 
101 static int __init early_coherent_pool(char *p)
102 {
103 	atomic_pool_size = memparse(p, &p);
104 	return 0;
105 }
106 early_param("coherent_pool", early_coherent_pool);
107 
108 int __init dma_atomic_pool_init(gfp_t gfp, pgprot_t prot)
109 {
110 	unsigned int pool_size_order = get_order(atomic_pool_size);
111 	unsigned long nr_pages = atomic_pool_size >> PAGE_SHIFT;
112 	struct page *page;
113 	void *addr;
114 	int ret;
115 
116 	if (dev_get_cma_area(NULL))
117 		page = dma_alloc_from_contiguous(NULL, nr_pages,
118 						 pool_size_order, false);
119 	else
120 		page = alloc_pages(gfp, pool_size_order);
121 	if (!page)
122 		goto out;
123 
124 	arch_dma_prep_coherent(page, atomic_pool_size);
125 
126 	atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
127 	if (!atomic_pool)
128 		goto free_page;
129 
130 	addr = dma_common_contiguous_remap(page, atomic_pool_size, VM_USERMAP,
131 					   prot, __builtin_return_address(0));
132 	if (!addr)
133 		goto destroy_genpool;
134 
135 	ret = gen_pool_add_virt(atomic_pool, (unsigned long)addr,
136 				page_to_phys(page), atomic_pool_size, -1);
137 	if (ret)
138 		goto remove_mapping;
139 	gen_pool_set_algo(atomic_pool, gen_pool_first_fit_order_align, NULL);
140 
141 	pr_info("DMA: preallocated %zu KiB pool for atomic allocations\n",
142 		atomic_pool_size / 1024);
143 	return 0;
144 
145 remove_mapping:
146 	dma_common_free_remap(addr, atomic_pool_size, VM_USERMAP);
147 destroy_genpool:
148 	gen_pool_destroy(atomic_pool);
149 	atomic_pool = NULL;
150 free_page:
151 	if (!dma_release_from_contiguous(NULL, page, nr_pages))
152 		__free_pages(page, pool_size_order);
153 out:
154 	pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
155 		atomic_pool_size / 1024);
156 	return -ENOMEM;
157 }
158 
159 bool dma_in_atomic_pool(void *start, size_t size)
160 {
161 	return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
162 }
163 
164 void *dma_alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags)
165 {
166 	unsigned long val;
167 	void *ptr = NULL;
168 
169 	if (!atomic_pool) {
170 		WARN(1, "coherent pool not initialised!\n");
171 		return NULL;
172 	}
173 
174 	val = gen_pool_alloc(atomic_pool, size);
175 	if (val) {
176 		phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
177 
178 		*ret_page = pfn_to_page(__phys_to_pfn(phys));
179 		ptr = (void *)val;
180 		memset(ptr, 0, size);
181 	}
182 
183 	return ptr;
184 }
185 
186 bool dma_free_from_pool(void *start, size_t size)
187 {
188 	if (!dma_in_atomic_pool(start, size))
189 		return false;
190 	gen_pool_free(atomic_pool, (unsigned long)start, size);
191 	return true;
192 }
193 
194 void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
195 		gfp_t flags, unsigned long attrs)
196 {
197 	struct page *page = NULL;
198 	void *ret;
199 
200 	size = PAGE_ALIGN(size);
201 
202 	if (!gfpflags_allow_blocking(flags) &&
203 	    !(attrs & DMA_ATTR_NO_KERNEL_MAPPING)) {
204 		ret = dma_alloc_from_pool(size, &page, flags);
205 		if (!ret)
206 			return NULL;
207 		goto done;
208 	}
209 
210 	page = __dma_direct_alloc_pages(dev, size, dma_handle, flags, attrs);
211 	if (!page)
212 		return NULL;
213 
214 	/* remove any dirty cache lines on the kernel alias */
215 	arch_dma_prep_coherent(page, size);
216 
217 	if (attrs & DMA_ATTR_NO_KERNEL_MAPPING) {
218 		ret = page; /* opaque cookie */
219 		goto done;
220 	}
221 
222 	/* create a coherent mapping */
223 	ret = dma_common_contiguous_remap(page, size, VM_USERMAP,
224 			arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs),
225 			__builtin_return_address(0));
226 	if (!ret) {
227 		__dma_direct_free_pages(dev, size, page);
228 		return ret;
229 	}
230 
231 	memset(ret, 0, size);
232 done:
233 	*dma_handle = phys_to_dma(dev, page_to_phys(page));
234 	return ret;
235 }
236 
237 void arch_dma_free(struct device *dev, size_t size, void *vaddr,
238 		dma_addr_t dma_handle, unsigned long attrs)
239 {
240 	if (attrs & DMA_ATTR_NO_KERNEL_MAPPING) {
241 		/* vaddr is a struct page cookie, not a kernel address */
242 		__dma_direct_free_pages(dev, size, vaddr);
243 	} else if (!dma_free_from_pool(vaddr, PAGE_ALIGN(size))) {
244 		phys_addr_t phys = dma_to_phys(dev, dma_handle);
245 		struct page *page = pfn_to_page(__phys_to_pfn(phys));
246 
247 		vunmap(vaddr);
248 		__dma_direct_free_pages(dev, size, page);
249 	}
250 }
251 
252 long arch_dma_coherent_to_pfn(struct device *dev, void *cpu_addr,
253 		dma_addr_t dma_addr)
254 {
255 	return __phys_to_pfn(dma_to_phys(dev, dma_addr));
256 }
257 #endif /* CONFIG_DMA_DIRECT_REMAP */
258