GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / staging / android / ion / ion_chunk_heap.c
1 /*
2  * drivers/staging/android/ion/ion_chunk_heap.c
3  *
4  * Copyright (C) 2012 Google, Inc.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16 #include <linux/dma-mapping.h>
17 #include <linux/err.h>
18 #include <linux/genalloc.h>
19 #include <linux/io.h>
20 #include <linux/mm.h>
21 #include <linux/scatterlist.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include "ion.h"
25
26 struct ion_chunk_heap {
27         struct ion_heap heap;
28         struct gen_pool *pool;
29         phys_addr_t base;
30         unsigned long chunk_size;
31         unsigned long size;
32         unsigned long allocated;
33 };
34
35 static int ion_chunk_heap_allocate(struct ion_heap *heap,
36                                    struct ion_buffer *buffer,
37                                    unsigned long size,
38                                    unsigned long flags)
39 {
40         struct ion_chunk_heap *chunk_heap =
41                 container_of(heap, struct ion_chunk_heap, heap);
42         struct sg_table *table;
43         struct scatterlist *sg;
44         int ret, i;
45         unsigned long num_chunks;
46         unsigned long allocated_size;
47
48         allocated_size = ALIGN(size, chunk_heap->chunk_size);
49         num_chunks = allocated_size / chunk_heap->chunk_size;
50
51         if (allocated_size > chunk_heap->size - chunk_heap->allocated)
52                 return -ENOMEM;
53
54         table = kmalloc(sizeof(*table), GFP_KERNEL);
55         if (!table)
56                 return -ENOMEM;
57         ret = sg_alloc_table(table, num_chunks, GFP_KERNEL);
58         if (ret) {
59                 kfree(table);
60                 return ret;
61         }
62
63         sg = table->sgl;
64         for (i = 0; i < num_chunks; i++) {
65                 unsigned long paddr = gen_pool_alloc(chunk_heap->pool,
66                                                      chunk_heap->chunk_size);
67                 if (!paddr)
68                         goto err;
69                 sg_set_page(sg, pfn_to_page(PFN_DOWN(paddr)),
70                             chunk_heap->chunk_size, 0);
71                 sg = sg_next(sg);
72         }
73
74         buffer->sg_table = table;
75         chunk_heap->allocated += allocated_size;
76         return 0;
77 err:
78         sg = table->sgl;
79         for (i -= 1; i >= 0; i--) {
80                 gen_pool_free(chunk_heap->pool, page_to_phys(sg_page(sg)),
81                               sg->length);
82                 sg = sg_next(sg);
83         }
84         sg_free_table(table);
85         kfree(table);
86         return -ENOMEM;
87 }
88
89 static void ion_chunk_heap_free(struct ion_buffer *buffer)
90 {
91         struct ion_heap *heap = buffer->heap;
92         struct ion_chunk_heap *chunk_heap =
93                 container_of(heap, struct ion_chunk_heap, heap);
94         struct sg_table *table = buffer->sg_table;
95         struct scatterlist *sg;
96         int i;
97         unsigned long allocated_size;
98
99         allocated_size = ALIGN(buffer->size, chunk_heap->chunk_size);
100
101         ion_heap_buffer_zero(buffer);
102
103         for_each_sg(table->sgl, sg, table->nents, i) {
104                 gen_pool_free(chunk_heap->pool, page_to_phys(sg_page(sg)),
105                               sg->length);
106         }
107         chunk_heap->allocated -= allocated_size;
108         sg_free_table(table);
109         kfree(table);
110 }
111
112 static struct ion_heap_ops chunk_heap_ops = {
113         .allocate = ion_chunk_heap_allocate,
114         .free = ion_chunk_heap_free,
115         .map_user = ion_heap_map_user,
116         .map_kernel = ion_heap_map_kernel,
117         .unmap_kernel = ion_heap_unmap_kernel,
118 };
119
120 struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *heap_data)
121 {
122         struct ion_chunk_heap *chunk_heap;
123         int ret;
124         struct page *page;
125         size_t size;
126
127         page = pfn_to_page(PFN_DOWN(heap_data->base));
128         size = heap_data->size;
129
130         ret = ion_heap_pages_zero(page, size, pgprot_writecombine(PAGE_KERNEL));
131         if (ret)
132                 return ERR_PTR(ret);
133
134         chunk_heap = kzalloc(sizeof(*chunk_heap), GFP_KERNEL);
135         if (!chunk_heap)
136                 return ERR_PTR(-ENOMEM);
137
138         chunk_heap->chunk_size = (unsigned long)heap_data->priv;
139         chunk_heap->pool = gen_pool_create(get_order(chunk_heap->chunk_size) +
140                                            PAGE_SHIFT, -1);
141         if (!chunk_heap->pool) {
142                 ret = -ENOMEM;
143                 goto error_gen_pool_create;
144         }
145         chunk_heap->base = heap_data->base;
146         chunk_heap->size = heap_data->size;
147         chunk_heap->allocated = 0;
148
149         gen_pool_add(chunk_heap->pool, chunk_heap->base, heap_data->size, -1);
150         chunk_heap->heap.ops = &chunk_heap_ops;
151         chunk_heap->heap.type = ION_HEAP_TYPE_CHUNK;
152         chunk_heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE;
153         pr_debug("%s: base %pa size %zu\n", __func__,
154                  &chunk_heap->base, heap_data->size);
155
156         return &chunk_heap->heap;
157
158 error_gen_pool_create:
159         kfree(chunk_heap);
160         return ERR_PTR(ret);
161 }
162