1 /*
2  * drivers/staging/android/ion/ion_system_heap.c
3  *
4  * Copyright (C) 2011 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 
17 #include <asm/page.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/err.h>
20 #include <linux/highmem.h>
21 #include <linux/mm.h>
22 #include <linux/scatterlist.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include "ion.h"
27 #include "ion_priv.h"
28 
29 static gfp_t high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN |
30 				     __GFP_NORETRY) & ~__GFP_DIRECT_RECLAIM;
31 static gfp_t low_order_gfp_flags  = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN);
32 static const unsigned int orders[] = {8, 4, 0};
33 static const int num_orders = ARRAY_SIZE(orders);
order_to_index(unsigned int order)34 static int order_to_index(unsigned int order)
35 {
36 	int i;
37 
38 	for (i = 0; i < num_orders; i++)
39 		if (order == orders[i])
40 			return i;
41 	BUG();
42 	return -1;
43 }
44 
order_to_size(int order)45 static inline unsigned int order_to_size(int order)
46 {
47 	return PAGE_SIZE << order;
48 }
49 
50 struct ion_system_heap {
51 	struct ion_heap heap;
52 	struct ion_page_pool *pools[0];
53 };
54 
alloc_buffer_page(struct ion_system_heap * heap,struct ion_buffer * buffer,unsigned long order)55 static struct page *alloc_buffer_page(struct ion_system_heap *heap,
56 				      struct ion_buffer *buffer,
57 				      unsigned long order)
58 {
59 	bool cached = ion_buffer_cached(buffer);
60 	struct ion_page_pool *pool = heap->pools[order_to_index(order)];
61 	struct page *page;
62 
63 	if (!cached) {
64 		page = ion_page_pool_alloc(pool);
65 	} else {
66 		gfp_t gfp_flags = low_order_gfp_flags;
67 
68 		if (order > 4)
69 			gfp_flags = high_order_gfp_flags;
70 		page = alloc_pages(gfp_flags | __GFP_COMP, order);
71 		if (!page)
72 			return NULL;
73 		ion_pages_sync_for_device(NULL, page, PAGE_SIZE << order,
74 						DMA_BIDIRECTIONAL);
75 	}
76 
77 	return page;
78 }
79 
free_buffer_page(struct ion_system_heap * heap,struct ion_buffer * buffer,struct page * page)80 static void free_buffer_page(struct ion_system_heap *heap,
81 			     struct ion_buffer *buffer, struct page *page)
82 {
83 	unsigned int order = compound_order(page);
84 	bool cached = ion_buffer_cached(buffer);
85 
86 	if (!cached && !(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE)) {
87 		struct ion_page_pool *pool = heap->pools[order_to_index(order)];
88 
89 		ion_page_pool_free(pool, page);
90 	} else {
91 		__free_pages(page, order);
92 	}
93 }
94 
95 
alloc_largest_available(struct ion_system_heap * heap,struct ion_buffer * buffer,unsigned long size,unsigned int max_order)96 static struct page *alloc_largest_available(struct ion_system_heap *heap,
97 					    struct ion_buffer *buffer,
98 					    unsigned long size,
99 					    unsigned int max_order)
100 {
101 	struct page *page;
102 	int i;
103 
104 	for (i = 0; i < num_orders; i++) {
105 		if (size < order_to_size(orders[i]))
106 			continue;
107 		if (max_order < orders[i])
108 			continue;
109 
110 		page = alloc_buffer_page(heap, buffer, orders[i]);
111 		if (!page)
112 			continue;
113 
114 		return page;
115 	}
116 
117 	return NULL;
118 }
119 
ion_system_heap_allocate(struct ion_heap * heap,struct ion_buffer * buffer,unsigned long size,unsigned long align,unsigned long flags)120 static int ion_system_heap_allocate(struct ion_heap *heap,
121 				     struct ion_buffer *buffer,
122 				     unsigned long size, unsigned long align,
123 				     unsigned long flags)
124 {
125 	struct ion_system_heap *sys_heap = container_of(heap,
126 							struct ion_system_heap,
127 							heap);
128 	struct sg_table *table;
129 	struct scatterlist *sg;
130 	struct list_head pages;
131 	struct page *page, *tmp_page;
132 	int i = 0;
133 	unsigned long size_remaining = PAGE_ALIGN(size);
134 	unsigned int max_order = orders[0];
135 
136 	if (align > PAGE_SIZE)
137 		return -EINVAL;
138 
139 	if (size / PAGE_SIZE > totalram_pages / 2)
140 		return -ENOMEM;
141 
142 	INIT_LIST_HEAD(&pages);
143 	while (size_remaining > 0) {
144 		page = alloc_largest_available(sys_heap, buffer, size_remaining,
145 						max_order);
146 		if (!page)
147 			goto free_pages;
148 		list_add_tail(&page->lru, &pages);
149 		size_remaining -= PAGE_SIZE << compound_order(page);
150 		max_order = compound_order(page);
151 		i++;
152 	}
153 	table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
154 	if (!table)
155 		goto free_pages;
156 
157 	if (sg_alloc_table(table, i, GFP_KERNEL))
158 		goto free_table;
159 
160 	sg = table->sgl;
161 	list_for_each_entry_safe(page, tmp_page, &pages, lru) {
162 		sg_set_page(sg, page, PAGE_SIZE << compound_order(page), 0);
163 		sg = sg_next(sg);
164 		list_del(&page->lru);
165 	}
166 
167 	buffer->priv_virt = table;
168 	return 0;
169 
170 free_table:
171 	kfree(table);
172 free_pages:
173 	list_for_each_entry_safe(page, tmp_page, &pages, lru)
174 		free_buffer_page(sys_heap, buffer, page);
175 	return -ENOMEM;
176 }
177 
ion_system_heap_free(struct ion_buffer * buffer)178 static void ion_system_heap_free(struct ion_buffer *buffer)
179 {
180 	struct ion_system_heap *sys_heap = container_of(buffer->heap,
181 							struct ion_system_heap,
182 							heap);
183 	struct sg_table *table = buffer->sg_table;
184 	bool cached = ion_buffer_cached(buffer);
185 	struct scatterlist *sg;
186 	int i;
187 
188 	/*
189 	 *  uncached pages come from the page pools, zero them before returning
190 	 *  for security purposes (other allocations are zerod at
191 	 *  alloc time
192 	 */
193 	if (!cached && !(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE))
194 		ion_heap_buffer_zero(buffer);
195 
196 	for_each_sg(table->sgl, sg, table->nents, i)
197 		free_buffer_page(sys_heap, buffer, sg_page(sg));
198 	sg_free_table(table);
199 	kfree(table);
200 }
201 
ion_system_heap_map_dma(struct ion_heap * heap,struct ion_buffer * buffer)202 static struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap,
203 						struct ion_buffer *buffer)
204 {
205 	return buffer->priv_virt;
206 }
207 
ion_system_heap_unmap_dma(struct ion_heap * heap,struct ion_buffer * buffer)208 static void ion_system_heap_unmap_dma(struct ion_heap *heap,
209 				      struct ion_buffer *buffer)
210 {
211 }
212 
ion_system_heap_shrink(struct ion_heap * heap,gfp_t gfp_mask,int nr_to_scan)213 static int ion_system_heap_shrink(struct ion_heap *heap, gfp_t gfp_mask,
214 					int nr_to_scan)
215 {
216 	struct ion_system_heap *sys_heap;
217 	int nr_total = 0;
218 	int i, nr_freed;
219 	int only_scan = 0;
220 
221 	sys_heap = container_of(heap, struct ion_system_heap, heap);
222 
223 	if (!nr_to_scan)
224 		only_scan = 1;
225 
226 	for (i = 0; i < num_orders; i++) {
227 		struct ion_page_pool *pool = sys_heap->pools[i];
228 
229 		nr_freed = ion_page_pool_shrink(pool, gfp_mask, nr_to_scan);
230 		nr_total += nr_freed;
231 
232 		if (!only_scan) {
233 			nr_to_scan -= nr_freed;
234 			/* shrink completed */
235 			if (nr_to_scan <= 0)
236 				break;
237 		}
238 	}
239 
240 	return nr_total;
241 }
242 
243 static struct ion_heap_ops system_heap_ops = {
244 	.allocate = ion_system_heap_allocate,
245 	.free = ion_system_heap_free,
246 	.map_dma = ion_system_heap_map_dma,
247 	.unmap_dma = ion_system_heap_unmap_dma,
248 	.map_kernel = ion_heap_map_kernel,
249 	.unmap_kernel = ion_heap_unmap_kernel,
250 	.map_user = ion_heap_map_user,
251 	.shrink = ion_system_heap_shrink,
252 };
253 
ion_system_heap_debug_show(struct ion_heap * heap,struct seq_file * s,void * unused)254 static int ion_system_heap_debug_show(struct ion_heap *heap, struct seq_file *s,
255 				      void *unused)
256 {
257 
258 	struct ion_system_heap *sys_heap = container_of(heap,
259 							struct ion_system_heap,
260 							heap);
261 	int i;
262 
263 	for (i = 0; i < num_orders; i++) {
264 		struct ion_page_pool *pool = sys_heap->pools[i];
265 
266 		seq_printf(s, "%d order %u highmem pages in pool = %lu total\n",
267 			   pool->high_count, pool->order,
268 			   (PAGE_SIZE << pool->order) * pool->high_count);
269 		seq_printf(s, "%d order %u lowmem pages in pool = %lu total\n",
270 			   pool->low_count, pool->order,
271 			   (PAGE_SIZE << pool->order) * pool->low_count);
272 	}
273 	return 0;
274 }
275 
ion_system_heap_create(struct ion_platform_heap * unused)276 struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused)
277 {
278 	struct ion_system_heap *heap;
279 	int i;
280 
281 	heap = kzalloc(sizeof(struct ion_system_heap) +
282 			sizeof(struct ion_page_pool *) * num_orders,
283 			GFP_KERNEL);
284 	if (!heap)
285 		return ERR_PTR(-ENOMEM);
286 	heap->heap.ops = &system_heap_ops;
287 	heap->heap.type = ION_HEAP_TYPE_SYSTEM;
288 	heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE;
289 
290 	for (i = 0; i < num_orders; i++) {
291 		struct ion_page_pool *pool;
292 		gfp_t gfp_flags = low_order_gfp_flags;
293 
294 		if (orders[i] > 4)
295 			gfp_flags = high_order_gfp_flags;
296 		pool = ion_page_pool_create(gfp_flags, orders[i]);
297 		if (!pool)
298 			goto destroy_pools;
299 		heap->pools[i] = pool;
300 	}
301 
302 	heap->heap.debug_show = ion_system_heap_debug_show;
303 	return &heap->heap;
304 
305 destroy_pools:
306 	while (i--)
307 		ion_page_pool_destroy(heap->pools[i]);
308 	kfree(heap);
309 	return ERR_PTR(-ENOMEM);
310 }
311 
ion_system_heap_destroy(struct ion_heap * heap)312 void ion_system_heap_destroy(struct ion_heap *heap)
313 {
314 	struct ion_system_heap *sys_heap = container_of(heap,
315 							struct ion_system_heap,
316 							heap);
317 	int i;
318 
319 	for (i = 0; i < num_orders; i++)
320 		ion_page_pool_destroy(sys_heap->pools[i]);
321 	kfree(sys_heap);
322 }
323 
ion_system_contig_heap_allocate(struct ion_heap * heap,struct ion_buffer * buffer,unsigned long len,unsigned long align,unsigned long flags)324 static int ion_system_contig_heap_allocate(struct ion_heap *heap,
325 					   struct ion_buffer *buffer,
326 					   unsigned long len,
327 					   unsigned long align,
328 					   unsigned long flags)
329 {
330 	int order = get_order(len);
331 	struct page *page;
332 	struct sg_table *table;
333 	unsigned long i;
334 	int ret;
335 
336 	if (align > (PAGE_SIZE << order))
337 		return -EINVAL;
338 
339 	page = alloc_pages(low_order_gfp_flags, order);
340 	if (!page)
341 		return -ENOMEM;
342 
343 	split_page(page, order);
344 
345 	len = PAGE_ALIGN(len);
346 	for (i = len >> PAGE_SHIFT; i < (1 << order); i++)
347 		__free_page(page + i);
348 
349 	table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
350 	if (!table) {
351 		ret = -ENOMEM;
352 		goto free_pages;
353 	}
354 
355 	ret = sg_alloc_table(table, 1, GFP_KERNEL);
356 	if (ret)
357 		goto free_table;
358 
359 	sg_set_page(table->sgl, page, len, 0);
360 
361 	buffer->priv_virt = table;
362 
363 	ion_pages_sync_for_device(NULL, page, len, DMA_BIDIRECTIONAL);
364 
365 	return 0;
366 
367 free_table:
368 	kfree(table);
369 free_pages:
370 	for (i = 0; i < len >> PAGE_SHIFT; i++)
371 		__free_page(page + i);
372 
373 	return ret;
374 }
375 
ion_system_contig_heap_free(struct ion_buffer * buffer)376 static void ion_system_contig_heap_free(struct ion_buffer *buffer)
377 {
378 	struct sg_table *table = buffer->priv_virt;
379 	struct page *page = sg_page(table->sgl);
380 	unsigned long pages = PAGE_ALIGN(buffer->size) >> PAGE_SHIFT;
381 	unsigned long i;
382 
383 	for (i = 0; i < pages; i++)
384 		__free_page(page + i);
385 	sg_free_table(table);
386 	kfree(table);
387 }
388 
ion_system_contig_heap_phys(struct ion_heap * heap,struct ion_buffer * buffer,ion_phys_addr_t * addr,size_t * len)389 static int ion_system_contig_heap_phys(struct ion_heap *heap,
390 				       struct ion_buffer *buffer,
391 				       ion_phys_addr_t *addr, size_t *len)
392 {
393 	struct sg_table *table = buffer->priv_virt;
394 	struct page *page = sg_page(table->sgl);
395 	*addr = page_to_phys(page);
396 	*len = buffer->size;
397 	return 0;
398 }
399 
ion_system_contig_heap_map_dma(struct ion_heap * heap,struct ion_buffer * buffer)400 static struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap,
401 						struct ion_buffer *buffer)
402 {
403 	return buffer->priv_virt;
404 }
405 
ion_system_contig_heap_unmap_dma(struct ion_heap * heap,struct ion_buffer * buffer)406 static void ion_system_contig_heap_unmap_dma(struct ion_heap *heap,
407 					     struct ion_buffer *buffer)
408 {
409 }
410 
411 static struct ion_heap_ops kmalloc_ops = {
412 	.allocate = ion_system_contig_heap_allocate,
413 	.free = ion_system_contig_heap_free,
414 	.phys = ion_system_contig_heap_phys,
415 	.map_dma = ion_system_contig_heap_map_dma,
416 	.unmap_dma = ion_system_contig_heap_unmap_dma,
417 	.map_kernel = ion_heap_map_kernel,
418 	.unmap_kernel = ion_heap_unmap_kernel,
419 	.map_user = ion_heap_map_user,
420 };
421 
ion_system_contig_heap_create(struct ion_platform_heap * unused)422 struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *unused)
423 {
424 	struct ion_heap *heap;
425 
426 	heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
427 	if (!heap)
428 		return ERR_PTR(-ENOMEM);
429 	heap->ops = &kmalloc_ops;
430 	heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG;
431 	return heap;
432 }
433 
ion_system_contig_heap_destroy(struct ion_heap * heap)434 void ion_system_contig_heap_destroy(struct ion_heap *heap)
435 {
436 	kfree(heap);
437 }
438