xref: /OK3568_Linux_fs/kernel/drivers/dma-buf/heaps/system_heap.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DMABUF System heap exporter
4  *
5  * Copyright (C) 2011 Google, Inc.
6  * Copyright (C) 2019, 2020 Linaro Ltd.
7  *
8  * Portions based off 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 
13 #include <linux/dma-buf.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/dma-heap.h>
16 #include <linux/err.h>
17 #include <linux/highmem.h>
18 #include <linux/mm.h>
19 #include <linux/module.h>
20 #include <linux/scatterlist.h>
21 #include <linux/slab.h>
22 #include <linux/vmalloc.h>
23 
24 #include "page_pool.h"
25 #include "deferred-free-helper.h"
26 
27 static struct dma_heap *sys_heap;
28 static struct dma_heap *sys_uncached_heap;
29 
30 struct system_heap_buffer {
31 	struct dma_heap *heap;
32 	struct list_head attachments;
33 	struct mutex lock;
34 	unsigned long len;
35 	struct sg_table sg_table;
36 	int vmap_cnt;
37 	void *vaddr;
38 	struct deferred_freelist_item deferred_free;
39 
40 	bool uncached;
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 	bool uncached;
50 };
51 
52 #define LOW_ORDER_GFP (GFP_HIGHUSER | __GFP_ZERO)
53 #define HIGH_ORDER_GFP  (((GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN \
54 				| __GFP_NORETRY) & ~__GFP_RECLAIM) \
55 				| __GFP_COMP)
56 static gfp_t order_flags[] = {HIGH_ORDER_GFP, HIGH_ORDER_GFP, LOW_ORDER_GFP};
57 /*
58  * The selection of the orders used for allocation (1MB, 64K, 4K) is designed
59  * to match with the sizes often found in IOMMUs. Using order 4 pages instead
60  * of order 0 pages can significantly improve the performance of many IOMMUs
61  * by reducing TLB pressure and time spent updating page tables.
62  */
63 static const unsigned int orders[] = {8, 4, 0};
64 #define NUM_ORDERS ARRAY_SIZE(orders)
65 struct dmabuf_page_pool *pools[NUM_ORDERS];
66 
dup_sg_table(struct sg_table * table)67 static struct sg_table *dup_sg_table(struct sg_table *table)
68 {
69 	struct sg_table *new_table;
70 	int ret, i;
71 	struct scatterlist *sg, *new_sg;
72 
73 	new_table = kzalloc(sizeof(*new_table), GFP_KERNEL);
74 	if (!new_table)
75 		return ERR_PTR(-ENOMEM);
76 
77 	ret = sg_alloc_table(new_table, table->orig_nents, GFP_KERNEL);
78 	if (ret) {
79 		kfree(new_table);
80 		return ERR_PTR(-ENOMEM);
81 	}
82 
83 	new_sg = new_table->sgl;
84 	for_each_sgtable_sg(table, sg, i) {
85 		sg_set_page(new_sg, sg_page(sg), sg->length, sg->offset);
86 		new_sg = sg_next(new_sg);
87 	}
88 
89 	return new_table;
90 }
91 
system_heap_attach(struct dma_buf * dmabuf,struct dma_buf_attachment * attachment)92 static int system_heap_attach(struct dma_buf *dmabuf,
93 			      struct dma_buf_attachment *attachment)
94 {
95 	struct system_heap_buffer *buffer = dmabuf->priv;
96 	struct dma_heap_attachment *a;
97 	struct sg_table *table;
98 
99 	a = kzalloc(sizeof(*a), GFP_KERNEL);
100 	if (!a)
101 		return -ENOMEM;
102 
103 	table = dup_sg_table(&buffer->sg_table);
104 	if (IS_ERR(table)) {
105 		kfree(a);
106 		return -ENOMEM;
107 	}
108 
109 	a->table = table;
110 	a->dev = attachment->dev;
111 	INIT_LIST_HEAD(&a->list);
112 	a->mapped = false;
113 	a->uncached = buffer->uncached;
114 	attachment->priv = a;
115 
116 	mutex_lock(&buffer->lock);
117 	list_add(&a->list, &buffer->attachments);
118 	mutex_unlock(&buffer->lock);
119 
120 	return 0;
121 }
122 
system_heap_detach(struct dma_buf * dmabuf,struct dma_buf_attachment * attachment)123 static void system_heap_detach(struct dma_buf *dmabuf,
124 			       struct dma_buf_attachment *attachment)
125 {
126 	struct system_heap_buffer *buffer = dmabuf->priv;
127 	struct dma_heap_attachment *a = attachment->priv;
128 
129 	mutex_lock(&buffer->lock);
130 	list_del(&a->list);
131 	mutex_unlock(&buffer->lock);
132 
133 	sg_free_table(a->table);
134 	kfree(a->table);
135 	kfree(a);
136 }
137 
system_heap_map_dma_buf(struct dma_buf_attachment * attachment,enum dma_data_direction direction)138 static struct sg_table *system_heap_map_dma_buf(struct dma_buf_attachment *attachment,
139 						enum dma_data_direction direction)
140 {
141 	struct dma_heap_attachment *a = attachment->priv;
142 	struct sg_table *table = a->table;
143 	int attr = attachment->dma_map_attrs;
144 	int ret;
145 
146 	if (a->uncached)
147 		attr |= DMA_ATTR_SKIP_CPU_SYNC;
148 
149 	ret = dma_map_sgtable(attachment->dev, table, direction, attr);
150 	if (ret)
151 		return ERR_PTR(ret);
152 
153 	a->mapped = true;
154 	return table;
155 }
156 
system_heap_unmap_dma_buf(struct dma_buf_attachment * attachment,struct sg_table * table,enum dma_data_direction direction)157 static void system_heap_unmap_dma_buf(struct dma_buf_attachment *attachment,
158 				      struct sg_table *table,
159 				      enum dma_data_direction direction)
160 {
161 	struct dma_heap_attachment *a = attachment->priv;
162 	int attr = attachment->dma_map_attrs;
163 
164 	if (a->uncached)
165 		attr |= DMA_ATTR_SKIP_CPU_SYNC;
166 	a->mapped = false;
167 	dma_unmap_sgtable(attachment->dev, table, direction, attr);
168 }
169 
system_heap_dma_buf_begin_cpu_access(struct dma_buf * dmabuf,enum dma_data_direction direction)170 static int system_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
171 						enum dma_data_direction direction)
172 {
173 	struct system_heap_buffer *buffer = dmabuf->priv;
174 	struct dma_heap_attachment *a;
175 
176 	mutex_lock(&buffer->lock);
177 
178 	if (buffer->vmap_cnt)
179 		invalidate_kernel_vmap_range(buffer->vaddr, buffer->len);
180 
181 	if (!buffer->uncached) {
182 		list_for_each_entry(a, &buffer->attachments, list) {
183 			if (!a->mapped)
184 				continue;
185 			dma_sync_sgtable_for_cpu(a->dev, a->table, direction);
186 		}
187 	}
188 	mutex_unlock(&buffer->lock);
189 
190 	return 0;
191 }
192 
system_heap_dma_buf_end_cpu_access(struct dma_buf * dmabuf,enum dma_data_direction direction)193 static int system_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
194 					      enum dma_data_direction direction)
195 {
196 	struct system_heap_buffer *buffer = dmabuf->priv;
197 	struct dma_heap_attachment *a;
198 
199 	mutex_lock(&buffer->lock);
200 
201 	if (buffer->vmap_cnt)
202 		flush_kernel_vmap_range(buffer->vaddr, buffer->len);
203 
204 	if (!buffer->uncached) {
205 		list_for_each_entry(a, &buffer->attachments, list) {
206 			if (!a->mapped)
207 				continue;
208 			dma_sync_sgtable_for_device(a->dev, a->table, direction);
209 		}
210 	}
211 	mutex_unlock(&buffer->lock);
212 
213 	return 0;
214 }
215 
system_heap_mmap(struct dma_buf * dmabuf,struct vm_area_struct * vma)216 static int system_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
217 {
218 	struct system_heap_buffer *buffer = dmabuf->priv;
219 	struct sg_table *table = &buffer->sg_table;
220 	unsigned long addr = vma->vm_start;
221 	struct sg_page_iter piter;
222 	int ret;
223 
224 	if (buffer->uncached)
225 		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
226 
227 	for_each_sgtable_page(table, &piter, vma->vm_pgoff) {
228 		struct page *page = sg_page_iter_page(&piter);
229 
230 		ret = remap_pfn_range(vma, addr, page_to_pfn(page), PAGE_SIZE,
231 				      vma->vm_page_prot);
232 		if (ret)
233 			return ret;
234 		addr += PAGE_SIZE;
235 		if (addr >= vma->vm_end)
236 			return 0;
237 	}
238 	return 0;
239 }
240 
system_heap_do_vmap(struct system_heap_buffer * buffer)241 static void *system_heap_do_vmap(struct system_heap_buffer *buffer)
242 {
243 	struct sg_table *table = &buffer->sg_table;
244 	int npages = PAGE_ALIGN(buffer->len) / PAGE_SIZE;
245 	struct page **pages = vmalloc(sizeof(struct page *) * npages);
246 	struct page **tmp = pages;
247 	struct sg_page_iter piter;
248 	pgprot_t pgprot = PAGE_KERNEL;
249 	void *vaddr;
250 
251 	if (!pages)
252 		return ERR_PTR(-ENOMEM);
253 
254 	if (buffer->uncached)
255 		pgprot = pgprot_writecombine(PAGE_KERNEL);
256 
257 	for_each_sgtable_page(table, &piter, 0) {
258 		WARN_ON(tmp - pages >= npages);
259 		*tmp++ = sg_page_iter_page(&piter);
260 	}
261 
262 	vaddr = vmap(pages, npages, VM_MAP, pgprot);
263 	vfree(pages);
264 
265 	if (!vaddr)
266 		return ERR_PTR(-ENOMEM);
267 
268 	return vaddr;
269 }
270 
system_heap_vmap(struct dma_buf * dmabuf)271 static void *system_heap_vmap(struct dma_buf *dmabuf)
272 {
273 	struct system_heap_buffer *buffer = dmabuf->priv;
274 	void *vaddr;
275 
276 	mutex_lock(&buffer->lock);
277 	if (buffer->vmap_cnt) {
278 		buffer->vmap_cnt++;
279 		vaddr = buffer->vaddr;
280 		goto out;
281 	}
282 
283 	vaddr = system_heap_do_vmap(buffer);
284 	if (IS_ERR(vaddr))
285 		goto out;
286 
287 	buffer->vaddr = vaddr;
288 	buffer->vmap_cnt++;
289 out:
290 	mutex_unlock(&buffer->lock);
291 
292 	return vaddr;
293 }
294 
system_heap_vunmap(struct dma_buf * dmabuf,void * vaddr)295 static void system_heap_vunmap(struct dma_buf *dmabuf, void *vaddr)
296 {
297 	struct system_heap_buffer *buffer = dmabuf->priv;
298 
299 	mutex_lock(&buffer->lock);
300 	if (!--buffer->vmap_cnt) {
301 		vunmap(buffer->vaddr);
302 		buffer->vaddr = NULL;
303 	}
304 	mutex_unlock(&buffer->lock);
305 }
306 
system_heap_zero_buffer(struct system_heap_buffer * buffer)307 static int system_heap_zero_buffer(struct system_heap_buffer *buffer)
308 {
309 	struct sg_table *sgt = &buffer->sg_table;
310 	struct sg_page_iter piter;
311 	struct page *p;
312 	void *vaddr;
313 	int ret = 0;
314 
315 	for_each_sgtable_page(sgt, &piter, 0) {
316 		p = sg_page_iter_page(&piter);
317 		vaddr = kmap_atomic(p);
318 		memset(vaddr, 0, PAGE_SIZE);
319 		kunmap_atomic(vaddr);
320 	}
321 
322 	return ret;
323 }
324 
system_heap_buf_free(struct deferred_freelist_item * item,enum df_reason reason)325 static void system_heap_buf_free(struct deferred_freelist_item *item,
326 				 enum df_reason reason)
327 {
328 	struct system_heap_buffer *buffer;
329 	struct sg_table *table;
330 	struct scatterlist *sg;
331 	int i, j;
332 
333 	buffer = container_of(item, struct system_heap_buffer, deferred_free);
334 	/* Zero the buffer pages before adding back to the pool */
335 	if (reason == DF_NORMAL)
336 		if (system_heap_zero_buffer(buffer))
337 			reason = DF_UNDER_PRESSURE; // On failure, just free
338 
339 	table = &buffer->sg_table;
340 	for_each_sgtable_sg(table, sg, i) {
341 		struct page *page = sg_page(sg);
342 
343 		if (reason == DF_UNDER_PRESSURE) {
344 			__free_pages(page, compound_order(page));
345 		} else {
346 			for (j = 0; j < NUM_ORDERS; j++) {
347 				if (compound_order(page) == orders[j])
348 					break;
349 			}
350 			dmabuf_page_pool_free(pools[j], page);
351 		}
352 	}
353 	sg_free_table(table);
354 	kfree(buffer);
355 }
356 
system_heap_dma_buf_release(struct dma_buf * dmabuf)357 static void system_heap_dma_buf_release(struct dma_buf *dmabuf)
358 {
359 	struct system_heap_buffer *buffer = dmabuf->priv;
360 	int npages = PAGE_ALIGN(buffer->len) / PAGE_SIZE;
361 
362 	deferred_free(&buffer->deferred_free, system_heap_buf_free, npages);
363 }
364 
365 static const struct dma_buf_ops system_heap_buf_ops = {
366 	.attach = system_heap_attach,
367 	.detach = system_heap_detach,
368 	.map_dma_buf = system_heap_map_dma_buf,
369 	.unmap_dma_buf = system_heap_unmap_dma_buf,
370 	.begin_cpu_access = system_heap_dma_buf_begin_cpu_access,
371 	.end_cpu_access = system_heap_dma_buf_end_cpu_access,
372 	.mmap = system_heap_mmap,
373 	.vmap = system_heap_vmap,
374 	.vunmap = system_heap_vunmap,
375 	.release = system_heap_dma_buf_release,
376 };
377 
alloc_largest_available(unsigned long size,unsigned int max_order)378 static struct page *alloc_largest_available(unsigned long size,
379 					    unsigned int max_order)
380 {
381 	struct page *page;
382 	int i;
383 
384 	for (i = 0; i < NUM_ORDERS; i++) {
385 		if (size <  (PAGE_SIZE << orders[i]))
386 			continue;
387 		if (max_order < orders[i])
388 			continue;
389 		page = dmabuf_page_pool_alloc(pools[i]);
390 		if (!page)
391 			continue;
392 		return page;
393 	}
394 	return NULL;
395 }
396 
system_heap_do_allocate(struct dma_heap * heap,unsigned long len,unsigned long fd_flags,unsigned long heap_flags,bool uncached)397 static struct dma_buf *system_heap_do_allocate(struct dma_heap *heap,
398 					       unsigned long len,
399 					       unsigned long fd_flags,
400 					       unsigned long heap_flags,
401 					       bool uncached)
402 {
403 	struct system_heap_buffer *buffer;
404 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
405 	unsigned long size_remaining = len;
406 	unsigned int max_order = orders[0];
407 	struct dma_buf *dmabuf;
408 	struct sg_table *table;
409 	struct scatterlist *sg;
410 	struct list_head pages;
411 	struct page *page, *tmp_page;
412 	int i, ret = -ENOMEM;
413 
414 	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
415 	if (!buffer)
416 		return ERR_PTR(-ENOMEM);
417 
418 	INIT_LIST_HEAD(&buffer->attachments);
419 	mutex_init(&buffer->lock);
420 	buffer->heap = heap;
421 	buffer->len = len;
422 	buffer->uncached = uncached;
423 
424 	INIT_LIST_HEAD(&pages);
425 	i = 0;
426 	while (size_remaining > 0) {
427 		/*
428 		 * Avoid trying to allocate memory if the process
429 		 * has been killed by SIGKILL
430 		 */
431 		if (fatal_signal_pending(current))
432 			goto free_buffer;
433 
434 		page = alloc_largest_available(size_remaining, max_order);
435 		if (!page)
436 			goto free_buffer;
437 
438 		list_add_tail(&page->lru, &pages);
439 		size_remaining -= page_size(page);
440 		max_order = compound_order(page);
441 		i++;
442 	}
443 
444 	table = &buffer->sg_table;
445 	if (sg_alloc_table(table, i, GFP_KERNEL))
446 		goto free_buffer;
447 
448 	sg = table->sgl;
449 	list_for_each_entry_safe(page, tmp_page, &pages, lru) {
450 		sg_set_page(sg, page, page_size(page), 0);
451 		sg = sg_next(sg);
452 		list_del(&page->lru);
453 	}
454 
455 	/* create the dmabuf */
456 	exp_info.exp_name = dma_heap_get_name(heap);
457 	exp_info.ops = &system_heap_buf_ops;
458 	exp_info.size = buffer->len;
459 	exp_info.flags = fd_flags;
460 	exp_info.priv = buffer;
461 	dmabuf = dma_buf_export(&exp_info);
462 	if (IS_ERR(dmabuf)) {
463 		ret = PTR_ERR(dmabuf);
464 		goto free_pages;
465 	}
466 
467 	/*
468 	 * For uncached buffers, we need to initially flush cpu cache, since
469 	 * the __GFP_ZERO on the allocation means the zeroing was done by the
470 	 * cpu and thus it is likely cached. Map (and implicitly flush) and
471 	 * unmap it now so we don't get corruption later on.
472 	 */
473 	if (buffer->uncached) {
474 		dma_map_sgtable(dma_heap_get_dev(heap), table, DMA_BIDIRECTIONAL, 0);
475 		dma_unmap_sgtable(dma_heap_get_dev(heap), table, DMA_BIDIRECTIONAL, 0);
476 	}
477 
478 	return dmabuf;
479 
480 free_pages:
481 	for_each_sgtable_sg(table, sg, i) {
482 		struct page *p = sg_page(sg);
483 
484 		__free_pages(p, compound_order(p));
485 	}
486 	sg_free_table(table);
487 free_buffer:
488 	list_for_each_entry_safe(page, tmp_page, &pages, lru)
489 		__free_pages(page, compound_order(page));
490 	kfree(buffer);
491 
492 	return ERR_PTR(ret);
493 }
494 
system_heap_allocate(struct dma_heap * heap,unsigned long len,unsigned long fd_flags,unsigned long heap_flags)495 static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
496 					    unsigned long len,
497 					    unsigned long fd_flags,
498 					    unsigned long heap_flags)
499 {
500 	return system_heap_do_allocate(heap, len, fd_flags, heap_flags, false);
501 }
502 
system_get_pool_size(struct dma_heap * heap)503 static long system_get_pool_size(struct dma_heap *heap)
504 {
505 	int i;
506 	long num_pages = 0;
507 	struct dmabuf_page_pool **pool;
508 
509 	pool = pools;
510 	for (i = 0; i < NUM_ORDERS; i++, pool++) {
511 		num_pages += ((*pool)->count[POOL_LOWPAGE] +
512 			      (*pool)->count[POOL_HIGHPAGE]) << (*pool)->order;
513 	}
514 
515 	return num_pages << PAGE_SHIFT;
516 }
517 
518 static const struct dma_heap_ops system_heap_ops = {
519 	.allocate = system_heap_allocate,
520 	.get_pool_size = system_get_pool_size,
521 };
522 
system_uncached_heap_allocate(struct dma_heap * heap,unsigned long len,unsigned long fd_flags,unsigned long heap_flags)523 static struct dma_buf *system_uncached_heap_allocate(struct dma_heap *heap,
524 						     unsigned long len,
525 						     unsigned long fd_flags,
526 						     unsigned long heap_flags)
527 {
528 	return system_heap_do_allocate(heap, len, fd_flags, heap_flags, true);
529 }
530 
531 /* Dummy function to be used until we can call coerce_mask_and_coherent */
system_uncached_heap_not_initialized(struct dma_heap * heap,unsigned long len,unsigned long fd_flags,unsigned long heap_flags)532 static struct dma_buf *system_uncached_heap_not_initialized(struct dma_heap *heap,
533 							    unsigned long len,
534 							    unsigned long fd_flags,
535 							    unsigned long heap_flags)
536 {
537 	return ERR_PTR(-EBUSY);
538 }
539 
540 static struct dma_heap_ops system_uncached_heap_ops = {
541 	/* After system_heap_create is complete, we will swap this */
542 	.allocate = system_uncached_heap_not_initialized,
543 };
544 
set_heap_dev_dma(struct device * heap_dev)545 static int set_heap_dev_dma(struct device *heap_dev)
546 {
547 	int err = 0;
548 
549 	if (!heap_dev)
550 		return -EINVAL;
551 
552 	dma_coerce_mask_and_coherent(heap_dev, DMA_BIT_MASK(64));
553 
554 	if (!heap_dev->dma_parms) {
555 		heap_dev->dma_parms = devm_kzalloc(heap_dev,
556 						   sizeof(*heap_dev->dma_parms),
557 						   GFP_KERNEL);
558 		if (!heap_dev->dma_parms)
559 			return -ENOMEM;
560 
561 		err = dma_set_max_seg_size(heap_dev, (unsigned int)DMA_BIT_MASK(64));
562 		if (err) {
563 			devm_kfree(heap_dev, heap_dev->dma_parms);
564 			dev_err(heap_dev, "Failed to set DMA segment size, err:%d\n", err);
565 			return err;
566 		}
567 	}
568 
569 	return 0;
570 }
571 
system_heap_create(void)572 static int system_heap_create(void)
573 {
574 	struct dma_heap_export_info exp_info;
575 	int i, err = 0;
576 
577 	for (i = 0; i < NUM_ORDERS; i++) {
578 		pools[i] = dmabuf_page_pool_create(order_flags[i], orders[i]);
579 
580 		if (!pools[i]) {
581 			int j;
582 
583 			pr_err("%s: page pool creation failed!\n", __func__);
584 			for (j = 0; j < i; j++)
585 				dmabuf_page_pool_destroy(pools[j]);
586 			return -ENOMEM;
587 		}
588 	}
589 
590 	exp_info.name = "system";
591 	exp_info.ops = &system_heap_ops;
592 	exp_info.priv = NULL;
593 
594 	sys_heap = dma_heap_add(&exp_info);
595 	if (IS_ERR(sys_heap))
596 		return PTR_ERR(sys_heap);
597 
598 	exp_info.name = "system-uncached";
599 	exp_info.ops = &system_uncached_heap_ops;
600 	exp_info.priv = NULL;
601 
602 	sys_uncached_heap = dma_heap_add(&exp_info);
603 	if (IS_ERR(sys_uncached_heap))
604 		return PTR_ERR(sys_uncached_heap);
605 
606 	err = set_heap_dev_dma(dma_heap_get_dev(sys_uncached_heap));
607 	if (err)
608 		return err;
609 
610 	mb(); /* make sure we only set allocate after dma_mask is set */
611 	system_uncached_heap_ops.allocate = system_uncached_heap_allocate;
612 
613 	return 0;
614 }
615 module_init(system_heap_create);
616 MODULE_LICENSE("GPL v2");
617