• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-direct.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/dma-heap.h>
17 #include <linux/err.h>
18 #include <linux/highmem.h>
19 #include <linux/iommu.h>
20 #include <linux/mm.h>
21 #include <linux/module.h>
22 #include <linux/printk.h>
23 #include <linux/scatterlist.h>
24 #include <linux/swiotlb.h>
25 #include <linux/vmalloc.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 
39 	bool uncached;
40 };
41 
42 struct dma_heap_attachment {
43 	struct device *dev;
44 	struct sg_table *table;
45 	struct list_head list;
46 	bool mapped;
47 
48 	bool uncached;
49 };
50 
51 #define LOW_ORDER_GFP (GFP_HIGHUSER | __GFP_ZERO)
52 #define HIGH_ORDER_GFP  (((GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN \
53 				| __GFP_NORETRY) & ~__GFP_RECLAIM) \
54 				| __GFP_COMP)
55 static gfp_t order_flags[] = {HIGH_ORDER_GFP, HIGH_ORDER_GFP, LOW_ORDER_GFP};
56 /*
57  * The selection of the orders used for allocation (1MB, 64K, 4K) is designed
58  * to match with the sizes often found in IOMMUs. Using order 4 pages instead
59  * of order 0 pages can significantly improve the performance of many IOMMUs
60  * by reducing TLB pressure and time spent updating page tables.
61  */
62 static const unsigned int orders[] = {8, 4, 0};
63 #define NUM_ORDERS ARRAY_SIZE(orders)
64 
needs_swiotlb_bounce(struct device * dev,struct sg_table * table)65 static bool needs_swiotlb_bounce(struct device *dev, struct sg_table *table)
66 {
67 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
68 	struct scatterlist *sg;
69 	int i;
70 
71 	for_each_sgtable_dma_sg(table, sg, i) {
72 		// SG_DMA_SWIOTLB is set only for dma-iommu, not for dma-direct
73 		if (domain && IS_ENABLED(CONFIG_NEED_SG_DMA_FLAGS)) {
74 			if (sg_dma_is_swiotlb(table->sgl))
75 				return true;
76 		} else {
77 			phys_addr_t paddr = domain ?
78 					    iommu_iova_to_phys(domain, sg_dma_address(sg)) :
79 					    dma_to_phys(dev, sg_dma_address(sg));
80 			if (swiotlb_find_pool(dev, paddr))
81 				return true;
82 		}
83 	}
84 	return false;
85 }
86 
dup_sg_table(struct sg_table * table)87 static struct sg_table *dup_sg_table(struct sg_table *table)
88 {
89 	struct sg_table *new_table;
90 	int ret, i;
91 	struct scatterlist *sg, *new_sg;
92 
93 	new_table = kzalloc(sizeof(*new_table), GFP_KERNEL);
94 	if (!new_table)
95 		return ERR_PTR(-ENOMEM);
96 
97 	ret = sg_alloc_table(new_table, table->orig_nents, GFP_KERNEL);
98 	if (ret) {
99 		kfree(new_table);
100 		return ERR_PTR(-ENOMEM);
101 	}
102 
103 	new_sg = new_table->sgl;
104 	for_each_sgtable_sg(table, sg, i) {
105 		sg_set_page(new_sg, sg_page(sg), sg->length, sg->offset);
106 		new_sg = sg_next(new_sg);
107 	}
108 
109 	return new_table;
110 }
111 
system_heap_attach(struct dma_buf * dmabuf,struct dma_buf_attachment * attachment)112 static int system_heap_attach(struct dma_buf *dmabuf,
113 			      struct dma_buf_attachment *attachment)
114 {
115 	struct system_heap_buffer *buffer = dmabuf->priv;
116 	struct dma_heap_attachment *a;
117 	struct sg_table *table;
118 
119 	a = kzalloc(sizeof(*a), GFP_KERNEL);
120 	if (!a)
121 		return -ENOMEM;
122 
123 	table = dup_sg_table(&buffer->sg_table);
124 	if (IS_ERR(table)) {
125 		kfree(a);
126 		return -ENOMEM;
127 	}
128 
129 	a->table = table;
130 	a->dev = attachment->dev;
131 	INIT_LIST_HEAD(&a->list);
132 	a->mapped = false;
133 	a->uncached = buffer->uncached;
134 	attachment->priv = a;
135 
136 	mutex_lock(&buffer->lock);
137 	list_add(&a->list, &buffer->attachments);
138 	mutex_unlock(&buffer->lock);
139 
140 	return 0;
141 }
142 
system_heap_detach(struct dma_buf * dmabuf,struct dma_buf_attachment * attachment)143 static void system_heap_detach(struct dma_buf *dmabuf,
144 			       struct dma_buf_attachment *attachment)
145 {
146 	struct system_heap_buffer *buffer = dmabuf->priv;
147 	struct dma_heap_attachment *a = attachment->priv;
148 
149 	mutex_lock(&buffer->lock);
150 	list_del(&a->list);
151 	mutex_unlock(&buffer->lock);
152 
153 	sg_free_table(a->table);
154 	kfree(a->table);
155 	kfree(a);
156 }
157 
system_heap_map_dma_buf(struct dma_buf_attachment * attachment,enum dma_data_direction direction)158 static struct sg_table *system_heap_map_dma_buf(struct dma_buf_attachment *attachment,
159 						enum dma_data_direction direction)
160 {
161 	struct dma_heap_attachment *a = attachment->priv;
162 	struct sg_table *table = a->table;
163 	int attr = attachment->dma_map_attrs;
164 	int ret;
165 
166 	if (a->uncached)
167 		attr |= DMA_ATTR_SKIP_CPU_SYNC;
168 
169 	ret = dma_map_sgtable(attachment->dev, table, direction, attr);
170 	if (ret)
171 		return ERR_PTR(ret);
172 
173 	if (a->uncached && needs_swiotlb_bounce(attachment->dev, table)) {
174 		pr_err("Cannot map uncached system heap buffer for %s, as it requires SWIOTLB",
175 			dev_name(attachment->dev));
176 		dma_unmap_sgtable(attachment->dev, table, direction, attr);
177 		return ERR_PTR(-EINVAL);
178 	}
179 
180 	a->mapped = true;
181 	return table;
182 }
183 
system_heap_unmap_dma_buf(struct dma_buf_attachment * attachment,struct sg_table * table,enum dma_data_direction direction)184 static void system_heap_unmap_dma_buf(struct dma_buf_attachment *attachment,
185 				      struct sg_table *table,
186 				      enum dma_data_direction direction)
187 {
188 	struct dma_heap_attachment *a = attachment->priv;
189 	int attr = attachment->dma_map_attrs;
190 
191 	if (a->uncached)
192 		attr |= DMA_ATTR_SKIP_CPU_SYNC;
193 	a->mapped = false;
194 	dma_unmap_sgtable(attachment->dev, table, direction, attr);
195 }
196 
system_heap_dma_buf_begin_cpu_access(struct dma_buf * dmabuf,enum dma_data_direction direction)197 static int system_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
198 						enum dma_data_direction direction)
199 {
200 	struct system_heap_buffer *buffer = dmabuf->priv;
201 	struct dma_heap_attachment *a;
202 
203 	mutex_lock(&buffer->lock);
204 
205 	if (buffer->vmap_cnt)
206 		invalidate_kernel_vmap_range(buffer->vaddr, buffer->len);
207 
208 	if (!buffer->uncached) {
209 		list_for_each_entry(a, &buffer->attachments, list) {
210 			if (!a->mapped)
211 				continue;
212 			dma_sync_sgtable_for_cpu(a->dev, a->table, direction);
213 		}
214 	}
215 	mutex_unlock(&buffer->lock);
216 
217 	return 0;
218 }
219 
system_heap_dma_buf_end_cpu_access(struct dma_buf * dmabuf,enum dma_data_direction direction)220 static int system_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
221 					      enum dma_data_direction direction)
222 {
223 	struct system_heap_buffer *buffer = dmabuf->priv;
224 	struct dma_heap_attachment *a;
225 
226 	mutex_lock(&buffer->lock);
227 
228 	if (buffer->vmap_cnt)
229 		flush_kernel_vmap_range(buffer->vaddr, buffer->len);
230 
231 	if (!buffer->uncached) {
232 		list_for_each_entry(a, &buffer->attachments, list) {
233 			if (!a->mapped)
234 				continue;
235 			dma_sync_sgtable_for_device(a->dev, a->table, direction);
236 		}
237 	}
238 	mutex_unlock(&buffer->lock);
239 
240 	return 0;
241 }
242 
system_heap_mmap(struct dma_buf * dmabuf,struct vm_area_struct * vma)243 static int system_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
244 {
245 	struct system_heap_buffer *buffer = dmabuf->priv;
246 	struct sg_table *table = &buffer->sg_table;
247 	unsigned long addr = vma->vm_start;
248 	struct sg_page_iter piter;
249 	int ret;
250 
251 	if (buffer->uncached)
252 		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
253 
254 	for_each_sgtable_page(table, &piter, vma->vm_pgoff) {
255 		struct page *page = sg_page_iter_page(&piter);
256 
257 		ret = remap_pfn_range(vma, addr, page_to_pfn(page), PAGE_SIZE,
258 				      vma->vm_page_prot);
259 		if (ret)
260 			return ret;
261 		addr += PAGE_SIZE;
262 		if (addr >= vma->vm_end)
263 			return 0;
264 	}
265 	return 0;
266 }
267 
system_heap_do_vmap(struct system_heap_buffer * buffer)268 static void *system_heap_do_vmap(struct system_heap_buffer *buffer)
269 {
270 	struct sg_table *table = &buffer->sg_table;
271 	int npages = PAGE_ALIGN(buffer->len) / PAGE_SIZE;
272 	struct page **pages = vmalloc(sizeof(struct page *) * npages);
273 	struct page **tmp = pages;
274 	struct sg_page_iter piter;
275 	pgprot_t pgprot = PAGE_KERNEL;
276 	void *vaddr;
277 
278 	if (!pages)
279 		return ERR_PTR(-ENOMEM);
280 
281 	if (buffer->uncached)
282 		pgprot = pgprot_writecombine(PAGE_KERNEL);
283 
284 	for_each_sgtable_page(table, &piter, 0) {
285 		WARN_ON(tmp - pages >= npages);
286 		*tmp++ = sg_page_iter_page(&piter);
287 	}
288 
289 	vaddr = vmap(pages, npages, VM_MAP, pgprot);
290 	vfree(pages);
291 
292 	if (!vaddr)
293 		return ERR_PTR(-ENOMEM);
294 
295 	return vaddr;
296 }
297 
system_heap_vmap(struct dma_buf * dmabuf,struct iosys_map * map)298 static int system_heap_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
299 {
300 	struct system_heap_buffer *buffer = dmabuf->priv;
301 	void *vaddr;
302 	int ret = 0;
303 
304 	mutex_lock(&buffer->lock);
305 	if (buffer->vmap_cnt) {
306 		buffer->vmap_cnt++;
307 		iosys_map_set_vaddr(map, buffer->vaddr);
308 		goto out;
309 	}
310 
311 	vaddr = system_heap_do_vmap(buffer);
312 	if (IS_ERR(vaddr)) {
313 		ret = PTR_ERR(vaddr);
314 		goto out;
315 	}
316 
317 	buffer->vaddr = vaddr;
318 	buffer->vmap_cnt++;
319 	iosys_map_set_vaddr(map, buffer->vaddr);
320 out:
321 	mutex_unlock(&buffer->lock);
322 
323 	return ret;
324 }
325 
system_heap_vunmap(struct dma_buf * dmabuf,struct iosys_map * map)326 static void system_heap_vunmap(struct dma_buf *dmabuf, struct iosys_map *map)
327 {
328 	struct system_heap_buffer *buffer = dmabuf->priv;
329 
330 	mutex_lock(&buffer->lock);
331 	if (!--buffer->vmap_cnt) {
332 		vunmap(buffer->vaddr);
333 		buffer->vaddr = NULL;
334 	}
335 	mutex_unlock(&buffer->lock);
336 	iosys_map_clear(map);
337 }
338 
system_heap_dma_buf_release(struct dma_buf * dmabuf)339 static void system_heap_dma_buf_release(struct dma_buf *dmabuf)
340 {
341 	struct system_heap_buffer *buffer = dmabuf->priv;
342 	struct sg_table *table;
343 	struct scatterlist *sg;
344 	int i;
345 
346 	table = &buffer->sg_table;
347 	for_each_sgtable_sg(table, sg, i) {
348 		struct page *page = sg_page(sg);
349 
350 		__free_pages(page, compound_order(page));
351 	}
352 	sg_free_table(table);
353 	kfree(buffer);
354 }
355 
356 static const struct dma_buf_ops system_heap_buf_ops = {
357 	.attach = system_heap_attach,
358 	.detach = system_heap_detach,
359 	.map_dma_buf = system_heap_map_dma_buf,
360 	.unmap_dma_buf = system_heap_unmap_dma_buf,
361 	.begin_cpu_access = system_heap_dma_buf_begin_cpu_access,
362 	.end_cpu_access = system_heap_dma_buf_end_cpu_access,
363 	.mmap = system_heap_mmap,
364 	.vmap = system_heap_vmap,
365 	.vunmap = system_heap_vunmap,
366 	.release = system_heap_dma_buf_release,
367 };
368 
alloc_largest_available(unsigned long size,unsigned int max_order)369 static struct page *alloc_largest_available(unsigned long size,
370 					    unsigned int max_order)
371 {
372 	struct page *page;
373 	int i;
374 
375 	for (i = 0; i < NUM_ORDERS; i++) {
376 		if (size <  (PAGE_SIZE << orders[i]))
377 			continue;
378 		if (max_order < orders[i])
379 			continue;
380 
381 		page = alloc_pages(order_flags[i], orders[i]);
382 		if (!page)
383 			continue;
384 		return page;
385 	}
386 	return NULL;
387 }
388 
system_heap_do_allocate(struct dma_heap * heap,unsigned long len,u32 fd_flags,u64 heap_flags,bool uncached)389 static struct dma_buf *system_heap_do_allocate(struct dma_heap *heap,
390 					       unsigned long len,
391 					       u32 fd_flags,
392 					       u64 heap_flags,
393 					       bool uncached)
394 {
395 	struct system_heap_buffer *buffer;
396 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
397 	unsigned long size_remaining = len;
398 	unsigned int max_order = orders[0];
399 	struct dma_buf *dmabuf;
400 	struct sg_table *table;
401 	struct scatterlist *sg;
402 	struct list_head pages;
403 	struct page *page, *tmp_page;
404 	int i, ret = -ENOMEM;
405 
406 	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
407 	if (!buffer)
408 		return ERR_PTR(-ENOMEM);
409 
410 	INIT_LIST_HEAD(&buffer->attachments);
411 	mutex_init(&buffer->lock);
412 	buffer->heap = heap;
413 	buffer->len = len;
414 	buffer->uncached = uncached;
415 
416 	INIT_LIST_HEAD(&pages);
417 	i = 0;
418 	while (size_remaining > 0) {
419 		/*
420 		 * Avoid trying to allocate memory if the process
421 		 * has been killed by SIGKILL
422 		 */
423 		if (fatal_signal_pending(current)) {
424 			ret = -EINTR;
425 			goto free_buffer;
426 		}
427 
428 		page = alloc_largest_available(size_remaining, max_order);
429 		if (!page)
430 			goto free_buffer;
431 
432 		list_add_tail(&page->lru, &pages);
433 		size_remaining -= page_size(page);
434 		max_order = compound_order(page);
435 		i++;
436 	}
437 
438 	table = &buffer->sg_table;
439 	if (sg_alloc_table(table, i, GFP_KERNEL))
440 		goto free_buffer;
441 
442 	sg = table->sgl;
443 	list_for_each_entry_safe(page, tmp_page, &pages, lru) {
444 		sg_set_page(sg, page, page_size(page), 0);
445 		sg = sg_next(sg);
446 		list_del(&page->lru);
447 	}
448 
449 	/* create the dmabuf */
450 	exp_info.exp_name = dma_heap_get_name(heap);
451 	exp_info.ops = &system_heap_buf_ops;
452 	exp_info.size = buffer->len;
453 	exp_info.flags = fd_flags;
454 	exp_info.priv = buffer;
455 	dmabuf = dma_buf_export(&exp_info);
456 	if (IS_ERR(dmabuf)) {
457 		ret = PTR_ERR(dmabuf);
458 		goto free_pages;
459 	}
460 
461 	/*
462 	 * For uncached buffers, we need to initially flush cpu cache, since
463 	 * the __GFP_ZERO on the allocation means the zeroing was done by the
464 	 * cpu and thus it is likely cached. Map (and implicitly flush) and
465 	 * unmap it now so we don't get corruption later on.
466 	 */
467 	if (buffer->uncached) {
468 		dma_map_sgtable(dma_heap_get_dev(heap), table, DMA_BIDIRECTIONAL, 0);
469 		dma_unmap_sgtable(dma_heap_get_dev(heap), table, DMA_BIDIRECTIONAL, 0);
470 	}
471 
472 	return dmabuf;
473 
474 free_pages:
475 	for_each_sgtable_sg(table, sg, i) {
476 		struct page *p = sg_page(sg);
477 
478 		__free_pages(p, compound_order(p));
479 	}
480 	sg_free_table(table);
481 free_buffer:
482 	list_for_each_entry_safe(page, tmp_page, &pages, lru)
483 		__free_pages(page, compound_order(page));
484 	kfree(buffer);
485 
486 	return ERR_PTR(ret);
487 }
488 
system_heap_allocate(struct dma_heap * heap,unsigned long len,u32 fd_flags,u64 heap_flags)489 static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
490 					    unsigned long len,
491 					    u32 fd_flags,
492 					    u64 heap_flags)
493 {
494 	return system_heap_do_allocate(heap, len, fd_flags, heap_flags, false);
495 }
496 
497 static const struct dma_heap_ops system_heap_ops = {
498 	.allocate = system_heap_allocate,
499 };
500 
system_uncached_heap_allocate(struct dma_heap * heap,unsigned long len,u32 fd_flags,u64 heap_flags)501 static struct dma_buf *system_uncached_heap_allocate(struct dma_heap *heap,
502 						     unsigned long len,
503 						     u32 fd_flags,
504 						     u64 heap_flags)
505 {
506 	return system_heap_do_allocate(heap, len, fd_flags, heap_flags, true);
507 }
508 
509 /* 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,u32 fd_flags,u64 heap_flags)510 static struct dma_buf *system_uncached_heap_not_initialized(struct dma_heap *heap,
511 							    unsigned long len,
512 							    u32 fd_flags,
513 							    u64 heap_flags)
514 {
515 	return ERR_PTR(-EBUSY);
516 }
517 
518 static struct dma_heap_ops system_uncached_heap_ops = {
519 	/* After system_heap_create is complete, we will swap this */
520 	.allocate = system_uncached_heap_not_initialized,
521 };
522 
system_heap_create(void)523 static int system_heap_create(void)
524 {
525 	struct dma_heap_export_info exp_info;
526 
527 	exp_info.name = "system";
528 	exp_info.ops = &system_heap_ops;
529 	exp_info.priv = NULL;
530 
531 	sys_heap = dma_heap_add(&exp_info);
532 	if (IS_ERR(sys_heap))
533 		return PTR_ERR(sys_heap);
534 
535 	exp_info.name = "system-uncached";
536 	exp_info.ops = &system_uncached_heap_ops;
537 	exp_info.priv = NULL;
538 
539 	sys_uncached_heap = dma_heap_add(&exp_info);
540 	if (IS_ERR(sys_uncached_heap))
541 		return PTR_ERR(sys_uncached_heap);
542 
543 	dma_coerce_mask_and_coherent(dma_heap_get_dev(sys_uncached_heap), DMA_BIT_MASK(64));
544 	mb(); /* make sure we only set allocate after dma_mask is set */
545 	system_uncached_heap_ops.allocate = system_uncached_heap_allocate;
546 
547 	return 0;
548 }
549 module_init(system_heap_create);
550 MODULE_LICENSE("GPL v2");
551 MODULE_IMPORT_NS(DMA_BUF);
552