hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// SPDX-License-Identifier: GPL-2.0
/*
 * ION Memory Allocator CMA heap exporter
 *
 * Copyright (C) Linaro 2012
 * Author: <benjamin.gaignard@linaro.org> for ST-Ericsson.
 */
 
#include <linux/device.h>
#include <linux/ion.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/cma.h>
#include <linux/scatterlist.h>
#include <linux/highmem.h>
 
struct ion_cma_heap {
   struct ion_heap heap;
   struct cma *cma;
} cma_heaps[MAX_CMA_AREAS];
 
#define to_cma_heap(x) container_of(x, struct ion_cma_heap, heap)
 
/* ION CMA heap operations functions */
static int ion_cma_allocate(struct ion_heap *heap, struct ion_buffer *buffer,
               unsigned long len,
               unsigned long flags)
{
   struct ion_cma_heap *cma_heap = to_cma_heap(heap);
   struct sg_table *table;
   struct page *pages;
   unsigned long size = PAGE_ALIGN(len);
   unsigned long nr_pages = size >> PAGE_SHIFT;
   unsigned long align = get_order(size);
   int ret;
 
   if (align > CONFIG_CMA_ALIGNMENT)
       align = CONFIG_CMA_ALIGNMENT;
 
   pages = cma_alloc(cma_heap->cma, nr_pages, align, false);
   if (!pages)
       return -ENOMEM;
 
   if (PageHighMem(pages)) {
       unsigned long nr_clear_pages = nr_pages;
       struct page *page = pages;
 
       while (nr_clear_pages > 0) {
           void *vaddr = kmap_atomic(page);
 
           memset(vaddr, 0, PAGE_SIZE);
           kunmap_atomic(vaddr);
           page++;
           nr_clear_pages--;
       }
   } else {
       memset(page_address(pages), 0, size);
   }
 
   table = kmalloc(sizeof(*table), GFP_KERNEL);
   if (!table)
       goto err;
 
   ret = sg_alloc_table(table, 1, GFP_KERNEL);
   if (ret)
       goto free_mem;
 
   sg_set_page(table->sgl, pages, size, 0);
 
   buffer->priv_virt = pages;
   buffer->sg_table = table;
 
   ion_buffer_prep_noncached(buffer);
 
   return 0;
 
free_mem:
   kfree(table);
err:
   cma_release(cma_heap->cma, pages, nr_pages);
   return -ENOMEM;
}
 
static void ion_cma_free(struct ion_buffer *buffer)
{
   struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
   struct page *pages = buffer->priv_virt;
   unsigned long nr_pages = PAGE_ALIGN(buffer->size) >> PAGE_SHIFT;
 
   /* release memory */
   cma_release(cma_heap->cma, pages, nr_pages);
   /* release sg table */
   sg_free_table(buffer->sg_table);
   kfree(buffer->sg_table);
}
 
static struct ion_heap_ops ion_cma_ops = {
   .allocate = ion_cma_allocate,
   .free = ion_cma_free,
};
 
static int __ion_add_cma_heap(struct cma *cma, void *data)
{
   int *cma_nr = data;
   struct ion_cma_heap *cma_heap;
   int ret;
 
   if (*cma_nr >= MAX_CMA_AREAS)
       return -EINVAL;
 
   cma_heap = &cma_heaps[*cma_nr];
   cma_heap->heap.ops = &ion_cma_ops;
   cma_heap->heap.type = ION_HEAP_TYPE_DMA;
   cma_heap->heap.name = cma_get_name(cma);
 
   ret = ion_device_add_heap(&cma_heap->heap);
   if (ret)
       goto out;
 
   cma_heap->cma = cma;
   *cma_nr += 1;
out:
   return 0;
}
 
static int __init ion_cma_heap_init(void)
{
   int ret;
   int nr = 0;
 
   ret = cma_for_each_area(__ion_add_cma_heap, &nr);
   if (ret) {
       for (nr = 0; nr < MAX_CMA_AREAS && cma_heaps[nr].cma; nr++)
           ion_device_remove_heap(&cma_heaps[nr].heap);
   }
 
   return ret;
}
 
static void __exit ion_cma_heap_exit(void)
{
   int nr;
 
   for (nr = 0; nr < MAX_CMA_AREAS && cma_heaps[nr].cma; nr++)
       ion_device_remove_heap(&cma_heaps[nr].heap);
}
 
module_init(ion_cma_heap_init);
module_exit(ion_cma_heap_exit);
MODULE_LICENSE("GPL v2");