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