xref: /OK3568_Linux_fs/kernel/drivers/dma-buf/heaps/cma_heap.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DMABUF CMA heap exporter
4  *
5  * Copyright (C) 2012, 2019, 2020 Linaro Ltd.
6  * Author: <benjamin.gaignard@linaro.org> for ST-Ericsson.
7  *
8  * Also utilizing parts of Andrew Davis' SRAM heap:
9  * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
10  *	Andrew F. Davis <afd@ti.com>
11  */
12 #include <linux/cma.h>
13 #include <linux/dma-buf.h>
14 #include <linux/dma-heap.h>
15 #include <linux/dma-map-ops.h>
16 #include <linux/err.h>
17 #include <linux/highmem.h>
18 #include <linux/io.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/scatterlist.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 
25 
26 struct cma_heap {
27 	struct dma_heap *heap;
28 	struct cma *cma;
29 };
30 
31 struct cma_heap_buffer {
32 	struct cma_heap *heap;
33 	struct list_head attachments;
34 	struct mutex lock;
35 	unsigned long len;
36 	struct page *cma_pages;
37 	struct page **pages;
38 	pgoff_t pagecount;
39 	int vmap_cnt;
40 	void *vaddr;
41 };
42 
43 struct dma_heap_attachment {
44 	struct device *dev;
45 	struct sg_table table;
46 	struct list_head list;
47 	bool mapped;
48 };
49 
cma_heap_attach(struct dma_buf * dmabuf,struct dma_buf_attachment * attachment)50 static int cma_heap_attach(struct dma_buf *dmabuf,
51 			   struct dma_buf_attachment *attachment)
52 {
53 	struct cma_heap_buffer *buffer = dmabuf->priv;
54 	struct dma_heap_attachment *a;
55 	int ret;
56 
57 	a = kzalloc(sizeof(*a), GFP_KERNEL);
58 	if (!a)
59 		return -ENOMEM;
60 
61 	ret = sg_alloc_table_from_pages(&a->table, buffer->pages,
62 					buffer->pagecount, 0,
63 					buffer->pagecount << PAGE_SHIFT,
64 					GFP_KERNEL);
65 	if (ret) {
66 		kfree(a);
67 		return ret;
68 	}
69 
70 	a->dev = attachment->dev;
71 	INIT_LIST_HEAD(&a->list);
72 	a->mapped = false;
73 
74 	attachment->priv = a;
75 
76 	mutex_lock(&buffer->lock);
77 	list_add(&a->list, &buffer->attachments);
78 	mutex_unlock(&buffer->lock);
79 
80 	return 0;
81 }
82 
cma_heap_detach(struct dma_buf * dmabuf,struct dma_buf_attachment * attachment)83 static void cma_heap_detach(struct dma_buf *dmabuf,
84 			    struct dma_buf_attachment *attachment)
85 {
86 	struct cma_heap_buffer *buffer = dmabuf->priv;
87 	struct dma_heap_attachment *a = attachment->priv;
88 
89 	mutex_lock(&buffer->lock);
90 	list_del(&a->list);
91 	mutex_unlock(&buffer->lock);
92 
93 	sg_free_table(&a->table);
94 	kfree(a);
95 }
96 
cma_heap_map_dma_buf(struct dma_buf_attachment * attachment,enum dma_data_direction direction)97 static struct sg_table *cma_heap_map_dma_buf(struct dma_buf_attachment *attachment,
98 					     enum dma_data_direction direction)
99 {
100 	struct dma_heap_attachment *a = attachment->priv;
101 	struct sg_table *table = &a->table;
102 	int attrs = attachment->dma_map_attrs;
103 	int ret;
104 
105 	ret = dma_map_sgtable(attachment->dev, table, direction, attrs);
106 	if (ret)
107 		return ERR_PTR(-ENOMEM);
108 	a->mapped = true;
109 	return table;
110 }
111 
cma_heap_unmap_dma_buf(struct dma_buf_attachment * attachment,struct sg_table * table,enum dma_data_direction direction)112 static void cma_heap_unmap_dma_buf(struct dma_buf_attachment *attachment,
113 				   struct sg_table *table,
114 				   enum dma_data_direction direction)
115 {
116 	struct dma_heap_attachment *a = attachment->priv;
117 	int attrs = attachment->dma_map_attrs;
118 
119 	a->mapped = false;
120 	dma_unmap_sgtable(attachment->dev, table, direction, attrs);
121 }
122 
cma_heap_dma_buf_begin_cpu_access(struct dma_buf * dmabuf,enum dma_data_direction direction)123 static int cma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
124 					     enum dma_data_direction direction)
125 {
126 	struct cma_heap_buffer *buffer = dmabuf->priv;
127 	struct dma_heap_attachment *a;
128 
129 	mutex_lock(&buffer->lock);
130 
131 	if (buffer->vmap_cnt)
132 		invalidate_kernel_vmap_range(buffer->vaddr, buffer->len);
133 
134 	list_for_each_entry(a, &buffer->attachments, list) {
135 		if (!a->mapped)
136 			continue;
137 		dma_sync_sgtable_for_cpu(a->dev, &a->table, direction);
138 	}
139 	mutex_unlock(&buffer->lock);
140 
141 	return 0;
142 }
143 
cma_heap_dma_buf_end_cpu_access(struct dma_buf * dmabuf,enum dma_data_direction direction)144 static int cma_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
145 					   enum dma_data_direction direction)
146 {
147 	struct cma_heap_buffer *buffer = dmabuf->priv;
148 	struct dma_heap_attachment *a;
149 
150 	mutex_lock(&buffer->lock);
151 
152 	if (buffer->vmap_cnt)
153 		flush_kernel_vmap_range(buffer->vaddr, buffer->len);
154 
155 	list_for_each_entry(a, &buffer->attachments, list) {
156 		if (!a->mapped)
157 			continue;
158 		dma_sync_sgtable_for_device(a->dev, &a->table, direction);
159 	}
160 	mutex_unlock(&buffer->lock);
161 
162 	return 0;
163 }
164 
cma_heap_vm_fault(struct vm_fault * vmf)165 static vm_fault_t cma_heap_vm_fault(struct vm_fault *vmf)
166 {
167 	struct vm_area_struct *vma = vmf->vma;
168 	struct cma_heap_buffer *buffer = vma->vm_private_data;
169 
170 	if (vmf->pgoff > buffer->pagecount)
171 		return VM_FAULT_SIGBUS;
172 
173 	vmf->page = buffer->pages[vmf->pgoff];
174 	get_page(vmf->page);
175 
176 	return 0;
177 }
178 
179 static const struct vm_operations_struct dma_heap_vm_ops = {
180 	.fault = cma_heap_vm_fault,
181 };
182 
cma_heap_mmap(struct dma_buf * dmabuf,struct vm_area_struct * vma)183 static int cma_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
184 {
185 	struct cma_heap_buffer *buffer = dmabuf->priv;
186 
187 	if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
188 		return -EINVAL;
189 
190 	vma->vm_ops = &dma_heap_vm_ops;
191 	vma->vm_private_data = buffer;
192 
193 	return 0;
194 }
195 
cma_heap_do_vmap(struct cma_heap_buffer * buffer)196 static void *cma_heap_do_vmap(struct cma_heap_buffer *buffer)
197 {
198 	void *vaddr;
199 
200 	vaddr = vmap(buffer->pages, buffer->pagecount, VM_MAP, PAGE_KERNEL);
201 	if (!vaddr)
202 		return ERR_PTR(-ENOMEM);
203 
204 	return vaddr;
205 }
206 
cma_heap_vmap(struct dma_buf * dmabuf)207 static void *cma_heap_vmap(struct dma_buf *dmabuf)
208 {
209 	struct cma_heap_buffer *buffer = dmabuf->priv;
210 	void *vaddr;
211 
212 	mutex_lock(&buffer->lock);
213 	if (buffer->vmap_cnt) {
214 		buffer->vmap_cnt++;
215 		vaddr = buffer->vaddr;
216 		goto out;
217 	}
218 
219 	vaddr = cma_heap_do_vmap(buffer);
220 	if (IS_ERR(vaddr))
221 		goto out;
222 
223 	buffer->vaddr = vaddr;
224 	buffer->vmap_cnt++;
225 out:
226 	mutex_unlock(&buffer->lock);
227 
228 	return vaddr;
229 }
230 
cma_heap_vunmap(struct dma_buf * dmabuf,void * vaddr)231 static void cma_heap_vunmap(struct dma_buf *dmabuf, void *vaddr)
232 {
233 	struct cma_heap_buffer *buffer = dmabuf->priv;
234 
235 	mutex_lock(&buffer->lock);
236 	if (!--buffer->vmap_cnt) {
237 		vunmap(buffer->vaddr);
238 		buffer->vaddr = NULL;
239 	}
240 	mutex_unlock(&buffer->lock);
241 }
242 
cma_heap_dma_buf_release(struct dma_buf * dmabuf)243 static void cma_heap_dma_buf_release(struct dma_buf *dmabuf)
244 {
245 	struct cma_heap_buffer *buffer = dmabuf->priv;
246 	struct cma_heap *cma_heap = buffer->heap;
247 
248 	if (buffer->vmap_cnt > 0) {
249 		WARN(1, "%s: buffer still mapped in the kernel\n", __func__);
250 		vunmap(buffer->vaddr);
251 	}
252 
253 	/* free page list */
254 	kfree(buffer->pages);
255 	/* release memory */
256 	cma_release(cma_heap->cma, buffer->cma_pages, buffer->pagecount);
257 	kfree(buffer);
258 }
259 
260 static const struct dma_buf_ops cma_heap_buf_ops = {
261 	.attach = cma_heap_attach,
262 	.detach = cma_heap_detach,
263 	.map_dma_buf = cma_heap_map_dma_buf,
264 	.unmap_dma_buf = cma_heap_unmap_dma_buf,
265 	.begin_cpu_access = cma_heap_dma_buf_begin_cpu_access,
266 	.end_cpu_access = cma_heap_dma_buf_end_cpu_access,
267 	.mmap = cma_heap_mmap,
268 	.vmap = cma_heap_vmap,
269 	.vunmap = cma_heap_vunmap,
270 	.release = cma_heap_dma_buf_release,
271 };
272 
cma_heap_allocate(struct dma_heap * heap,unsigned long len,unsigned long fd_flags,unsigned long heap_flags)273 static struct dma_buf *cma_heap_allocate(struct dma_heap *heap,
274 					 unsigned long len,
275 					 unsigned long fd_flags,
276 					 unsigned long heap_flags)
277 {
278 	struct cma_heap *cma_heap = dma_heap_get_drvdata(heap);
279 	struct cma_heap_buffer *buffer;
280 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
281 	size_t size = PAGE_ALIGN(len);
282 	pgoff_t pagecount = size >> PAGE_SHIFT;
283 	unsigned long align = get_order(size);
284 	struct page *cma_pages;
285 	struct dma_buf *dmabuf;
286 	int ret = -ENOMEM;
287 	pgoff_t pg;
288 
289 	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
290 	if (!buffer)
291 		return ERR_PTR(-ENOMEM);
292 
293 	INIT_LIST_HEAD(&buffer->attachments);
294 	mutex_init(&buffer->lock);
295 	buffer->len = size;
296 
297 	if (align > CONFIG_CMA_ALIGNMENT)
298 		align = CONFIG_CMA_ALIGNMENT;
299 
300 	cma_pages = cma_alloc(cma_heap->cma, pagecount, align, GFP_KERNEL);
301 	if (!cma_pages)
302 		goto free_buffer;
303 
304 	/* Clear the cma pages */
305 	if (PageHighMem(cma_pages)) {
306 		unsigned long nr_clear_pages = pagecount;
307 		struct page *page = cma_pages;
308 
309 		while (nr_clear_pages > 0) {
310 			void *vaddr = kmap_atomic(page);
311 
312 			memset(vaddr, 0, PAGE_SIZE);
313 			kunmap_atomic(vaddr);
314 			/*
315 			 * Avoid wasting time zeroing memory if the process
316 			 * has been killed by by SIGKILL
317 			 */
318 			if (fatal_signal_pending(current))
319 				goto free_cma;
320 			page++;
321 			nr_clear_pages--;
322 		}
323 	} else {
324 		memset(page_address(cma_pages), 0, size);
325 	}
326 
327 	buffer->pages = kmalloc_array(pagecount, sizeof(*buffer->pages), GFP_KERNEL);
328 	if (!buffer->pages) {
329 		ret = -ENOMEM;
330 		goto free_cma;
331 	}
332 
333 	for (pg = 0; pg < pagecount; pg++)
334 		buffer->pages[pg] = &cma_pages[pg];
335 
336 	buffer->cma_pages = cma_pages;
337 	buffer->heap = cma_heap;
338 	buffer->pagecount = pagecount;
339 
340 	/* create the dmabuf */
341 	exp_info.exp_name = dma_heap_get_name(heap);
342 	exp_info.ops = &cma_heap_buf_ops;
343 	exp_info.size = buffer->len;
344 	exp_info.flags = fd_flags;
345 	exp_info.priv = buffer;
346 	dmabuf = dma_buf_export(&exp_info);
347 	if (IS_ERR(dmabuf)) {
348 		ret = PTR_ERR(dmabuf);
349 		goto free_pages;
350 	}
351 
352 	return dmabuf;
353 
354 free_pages:
355 	kfree(buffer->pages);
356 free_cma:
357 	cma_release(cma_heap->cma, cma_pages, pagecount);
358 free_buffer:
359 	kfree(buffer);
360 
361 	return ERR_PTR(ret);
362 }
363 
364 static const struct dma_heap_ops cma_heap_ops = {
365 	.allocate = cma_heap_allocate,
366 };
367 
__add_cma_heap(struct cma * cma,void * data)368 static int __add_cma_heap(struct cma *cma, void *data)
369 {
370 	struct cma_heap *cma_heap;
371 	struct dma_heap_export_info exp_info;
372 
373 	cma_heap = kzalloc(sizeof(*cma_heap), GFP_KERNEL);
374 	if (!cma_heap)
375 		return -ENOMEM;
376 	cma_heap->cma = cma;
377 
378 	exp_info.name = cma_get_name(cma);
379 	exp_info.ops = &cma_heap_ops;
380 	exp_info.priv = cma_heap;
381 
382 	cma_heap->heap = dma_heap_add(&exp_info);
383 	if (IS_ERR(cma_heap->heap)) {
384 		int ret = PTR_ERR(cma_heap->heap);
385 
386 		kfree(cma_heap);
387 		return ret;
388 	}
389 
390 	return 0;
391 }
392 
add_default_cma_heap(void)393 static int add_default_cma_heap(void)
394 {
395 	struct cma *default_cma = dev_get_cma_area(NULL);
396 	int ret = 0;
397 
398 	if (default_cma)
399 		ret = __add_cma_heap(default_cma, NULL);
400 
401 	return ret;
402 }
403 module_init(add_default_cma_heap);
404 MODULE_DESCRIPTION("DMA-BUF CMA Heap");
405 MODULE_LICENSE("GPL v2");
406