blob: 9849f3963e752f01b99450c7636132fba3aefda5 [file] [log] [blame]
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -08001/*
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
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080017#include <asm/page.h>
18#include <linux/dma-mapping.h>
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080019#include <linux/err.h>
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080020#include <linux/highmem.h>
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080021#include <linux/mm.h>
22#include <linux/scatterlist.h>
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080023#include <linux/seq_file.h>
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080024#include <linux/slab.h>
25#include <linux/vmalloc.h>
26#include "ion.h"
27#include "ion_priv.h"
28
Colin Crossf63958d2013-12-13 19:26:28 -080029static gfp_t high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN |
30 __GFP_NORETRY) & ~__GFP_WAIT;
31static gfp_t low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080032static const unsigned int orders[] = {8, 4, 0};
33static const int num_orders = ARRAY_SIZE(orders);
34static int order_to_index(unsigned int order)
35{
36 int i;
37 for (i = 0; i < num_orders; i++)
38 if (order == orders[i])
39 return i;
40 BUG();
41 return -1;
42}
43
44static unsigned int order_to_size(int order)
45{
46 return PAGE_SIZE << order;
47}
48
49struct ion_system_heap {
50 struct ion_heap heap;
51 struct ion_page_pool **pools;
52};
53
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080054struct page_info {
55 struct page *page;
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080056 unsigned int order;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080057 struct list_head list;
58};
59
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080060static struct page *alloc_buffer_page(struct ion_system_heap *heap,
61 struct ion_buffer *buffer,
62 unsigned long order)
63{
64 bool cached = ion_buffer_cached(buffer);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080065 struct ion_page_pool *pool = heap->pools[order_to_index(order)];
66 struct page *page;
Rebecca Schultz Zavinb0599c02013-12-13 14:24:08 -080067
Rebecca Schultz Zavinee4a4982013-12-13 14:24:12 -080068 if (!cached) {
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080069 page = ion_page_pool_alloc(pool);
Rebecca Schultz Zavinee4a4982013-12-13 14:24:12 -080070 } else {
71 gfp_t gfp_flags = low_order_gfp_flags;
72
73 if (order > 4)
74 gfp_flags = high_order_gfp_flags;
Colin Crossa3056902013-12-13 19:26:25 -080075 page = alloc_pages(gfp_flags, order);
Rebecca Schultz Zavin8fae8312013-12-13 14:24:18 -080076 if (!page)
Colin Crossf63958d2013-12-13 19:26:28 -080077 return NULL;
Colin Crosse946b202013-12-13 14:25:01 -080078 ion_pages_sync_for_device(NULL, page, PAGE_SIZE << order,
79 DMA_BIDIRECTIONAL);
Rebecca Schultz Zavinee4a4982013-12-13 14:24:12 -080080 }
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080081 if (!page)
Colin Crossf63958d2013-12-13 19:26:28 -080082 return NULL;
Rebecca Schultz Zavin8fae8312013-12-13 14:24:18 -080083
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080084 return page;
85}
86
87static void free_buffer_page(struct ion_system_heap *heap,
88 struct ion_buffer *buffer, struct page *page,
Rebecca Schultz Zavin0b6b2cd2013-12-13 14:24:32 -080089 unsigned int order)
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080090{
91 bool cached = ion_buffer_cached(buffer);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080092
93 if (!cached) {
94 struct ion_page_pool *pool = heap->pools[order_to_index(order)];
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080095 ion_page_pool_free(pool, page);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080096 } else {
97 __free_pages(page, order);
98 }
99}
100
101
102static struct page_info *alloc_largest_available(struct ion_system_heap *heap,
103 struct ion_buffer *buffer,
104 unsigned long size,
Rebecca Schultz Zavinba96a2e2013-12-13 14:24:07 -0800105 unsigned int max_order)
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800106{
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800107 struct page *page;
108 struct page_info *info;
109 int i;
110
John Stultzf4ea8232013-12-16 21:07:52 -0800111 info = kmalloc(sizeof(struct page_info), GFP_KERNEL);
112 if (!info)
113 return NULL;
114
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800115 for (i = 0; i < num_orders; i++) {
116 if (size < order_to_size(orders[i]))
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800117 continue;
Rebecca Schultz Zavinba96a2e2013-12-13 14:24:07 -0800118 if (max_order < orders[i])
119 continue;
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800120
121 page = alloc_buffer_page(heap, buffer, orders[i]);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800122 if (!page)
123 continue;
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800124
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800125 info->page = page;
126 info->order = orders[i];
Colin Crossc9e84402014-02-04 16:08:38 -0800127 INIT_LIST_HEAD(&info->list);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800128 return info;
129 }
John Stultzf4ea8232013-12-16 21:07:52 -0800130 kfree(info);
131
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800132 return NULL;
133}
134
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800135static int ion_system_heap_allocate(struct ion_heap *heap,
136 struct ion_buffer *buffer,
137 unsigned long size, unsigned long align,
138 unsigned long flags)
139{
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800140 struct ion_system_heap *sys_heap = container_of(heap,
141 struct ion_system_heap,
142 heap);
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800143 struct sg_table *table;
144 struct scatterlist *sg;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800145 int ret;
146 struct list_head pages;
147 struct page_info *info, *tmp_info;
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -0800148 int i = 0;
Colin Crossc9e84402014-02-04 16:08:38 -0800149 unsigned long size_remaining = PAGE_ALIGN(size);
Rebecca Schultz Zavinba96a2e2013-12-13 14:24:07 -0800150 unsigned int max_order = orders[0];
151
Colin Crossc13d1df2013-12-13 14:25:03 -0800152 if (align > PAGE_SIZE)
153 return -EINVAL;
154
Colin Crossc9e84402014-02-04 16:08:38 -0800155 if (size / PAGE_SIZE > totalram_pages / 2)
156 return -ENOMEM;
157
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800158 INIT_LIST_HEAD(&pages);
159 while (size_remaining > 0) {
John Stultze1d855b2013-12-13 19:26:33 -0800160 info = alloc_largest_available(sys_heap, buffer, size_remaining,
161 max_order);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800162 if (!info)
163 goto err;
164 list_add_tail(&info->list, &pages);
165 size_remaining -= (1 << info->order) * PAGE_SIZE;
Rebecca Schultz Zavinba96a2e2013-12-13 14:24:07 -0800166 max_order = info->order;
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -0800167 i++;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800168 }
John Stultzea725ec2013-12-13 19:26:18 -0800169 table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800170 if (!table)
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800171 goto err;
172
Rebecca Schultz Zavinc13bd1c2013-12-13 14:24:45 -0800173 ret = sg_alloc_table(table, i, GFP_KERNEL);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800174 if (ret)
175 goto err1;
176
177 sg = table->sgl;
178 list_for_each_entry_safe(info, tmp_info, &pages, list) {
179 struct page *page = info->page;
Rebecca Schultz Zavinc13bd1c2013-12-13 14:24:45 -0800180 sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE, 0);
181 sg = sg_next(sg);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800182 list_del(&info->list);
Rebecca Schultz Zavin708f0ca2013-12-13 14:23:59 -0800183 kfree(info);
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800184 }
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800185
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800186 buffer->priv_virt = table;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800187 return 0;
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800188err1:
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800189 kfree(table);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800190err:
John Stultzea725ec2013-12-13 19:26:18 -0800191 list_for_each_entry_safe(info, tmp_info, &pages, list) {
Rebecca Schultz Zavin0b6b2cd2013-12-13 14:24:32 -0800192 free_buffer_page(sys_heap, buffer, info->page, info->order);
Rebecca Schultz Zavin708f0ca2013-12-13 14:23:59 -0800193 kfree(info);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800194 }
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800195 return -ENOMEM;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800196}
197
Colin Crossf63958d2013-12-13 19:26:28 -0800198static void ion_system_heap_free(struct ion_buffer *buffer)
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800199{
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800200 struct ion_heap *heap = buffer->heap;
201 struct ion_system_heap *sys_heap = container_of(heap,
202 struct ion_system_heap,
203 heap);
Rebecca Schultz Zavin88982272013-12-13 14:24:26 -0800204 struct sg_table *table = buffer->sg_table;
Rebecca Schultz Zavin0b6b2cd2013-12-13 14:24:32 -0800205 bool cached = ion_buffer_cached(buffer);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800206 struct scatterlist *sg;
207 LIST_HEAD(pages);
208 int i;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800209
Rebecca Schultz Zavin0b6b2cd2013-12-13 14:24:32 -0800210 /* uncached pages come from the page pools, zero them before returning
211 for security purposes (other allocations are zerod at alloc time */
212 if (!cached)
213 ion_heap_buffer_zero(buffer);
Rebecca Schultz Zavin77cbe822013-12-13 14:24:31 -0800214
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800215 for_each_sg(table->sgl, sg, table->nents, i)
Rebecca Schultz Zavin77cbe822013-12-13 14:24:31 -0800216 free_buffer_page(sys_heap, buffer, sg_page(sg),
Colin Cross06e0dca2013-12-13 14:25:02 -0800217 get_order(sg->length));
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800218 sg_free_table(table);
219 kfree(table);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800220}
221
Colin Crossf63958d2013-12-13 19:26:28 -0800222static struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap,
223 struct ion_buffer *buffer)
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800224{
225 return buffer->priv_virt;
226}
227
Colin Crossf63958d2013-12-13 19:26:28 -0800228static void ion_system_heap_unmap_dma(struct ion_heap *heap,
229 struct ion_buffer *buffer)
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800230{
231 return;
232}
233
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800234static struct ion_heap_ops system_heap_ops = {
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800235 .allocate = ion_system_heap_allocate,
236 .free = ion_system_heap_free,
237 .map_dma = ion_system_heap_map_dma,
238 .unmap_dma = ion_system_heap_unmap_dma,
Rebecca Schultz Zavin88982272013-12-13 14:24:26 -0800239 .map_kernel = ion_heap_map_kernel,
240 .unmap_kernel = ion_heap_unmap_kernel,
241 .map_user = ion_heap_map_user,
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800242};
243
John Stultzb1aced62013-12-13 19:26:35 -0800244static unsigned long ion_system_heap_shrink_count(struct shrinker *shrinker,
245 struct shrink_control *sc)
246{
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800247 struct ion_heap *heap = container_of(shrinker, struct ion_heap,
248 shrinker);
249 struct ion_system_heap *sys_heap = container_of(heap,
250 struct ion_system_heap,
251 heap);
252 int nr_total = 0;
John Stultzb1aced62013-12-13 19:26:35 -0800253 int i;
254
255 /* total number of items is whatever the page pools are holding
256 plus whatever's in the freelist */
257 for (i = 0; i < num_orders; i++) {
258 struct ion_page_pool *pool = sys_heap->pools[i];
259 nr_total += ion_page_pool_shrink(pool, sc->gfp_mask, 0);
260 }
261 nr_total += ion_heap_freelist_size(heap) / PAGE_SIZE;
262 return nr_total;
263
264}
265
266static unsigned long ion_system_heap_shrink_scan(struct shrinker *shrinker,
267 struct shrink_control *sc)
268{
269
270 struct ion_heap *heap = container_of(shrinker, struct ion_heap,
271 shrinker);
272 struct ion_system_heap *sys_heap = container_of(heap,
273 struct ion_system_heap,
274 heap);
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800275 int nr_freed = 0;
276 int i;
277
278 if (sc->nr_to_scan == 0)
279 goto end;
280
281 /* shrink the free list first, no point in zeroing the memory if
282 we're just going to reclaim it */
283 nr_freed += ion_heap_freelist_drain(heap, sc->nr_to_scan * PAGE_SIZE) /
284 PAGE_SIZE;
285
286 if (nr_freed >= sc->nr_to_scan)
287 goto end;
288
289 for (i = 0; i < num_orders; i++) {
290 struct ion_page_pool *pool = sys_heap->pools[i];
291
292 nr_freed += ion_page_pool_shrink(pool, sc->gfp_mask,
293 sc->nr_to_scan);
294 if (nr_freed >= sc->nr_to_scan)
295 break;
296 }
297
298end:
John Stultzb1aced62013-12-13 19:26:35 -0800299 return nr_freed;
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800300
301}
302
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800303static int ion_system_heap_debug_show(struct ion_heap *heap, struct seq_file *s,
304 void *unused)
305{
306
307 struct ion_system_heap *sys_heap = container_of(heap,
308 struct ion_system_heap,
309 heap);
310 int i;
311 for (i = 0; i < num_orders; i++) {
312 struct ion_page_pool *pool = sys_heap->pools[i];
Rebecca Schultz Zavin0fb9b812013-12-13 14:24:13 -0800313 seq_printf(s, "%d order %u highmem pages in pool = %lu total\n",
314 pool->high_count, pool->order,
315 (1 << pool->order) * PAGE_SIZE * pool->high_count);
316 seq_printf(s, "%d order %u lowmem pages in pool = %lu total\n",
317 pool->low_count, pool->order,
318 (1 << pool->order) * PAGE_SIZE * pool->low_count);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800319 }
320 return 0;
321}
322
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800323struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused)
324{
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800325 struct ion_system_heap *heap;
326 int i;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800327
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800328 heap = kzalloc(sizeof(struct ion_system_heap), GFP_KERNEL);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800329 if (!heap)
330 return ERR_PTR(-ENOMEM);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800331 heap->heap.ops = &system_heap_ops;
332 heap->heap.type = ION_HEAP_TYPE_SYSTEM;
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800333 heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE;
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800334 heap->pools = kzalloc(sizeof(struct ion_page_pool *) * num_orders,
335 GFP_KERNEL);
336 if (!heap->pools)
337 goto err_alloc_pools;
338 for (i = 0; i < num_orders; i++) {
339 struct ion_page_pool *pool;
Rebecca Schultz Zavinee4a4982013-12-13 14:24:12 -0800340 gfp_t gfp_flags = low_order_gfp_flags;
341
342 if (orders[i] > 4)
343 gfp_flags = high_order_gfp_flags;
344 pool = ion_page_pool_create(gfp_flags, orders[i]);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800345 if (!pool)
346 goto err_create_pool;
347 heap->pools[i] = pool;
348 }
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800349
John Stultzb1aced62013-12-13 19:26:35 -0800350 heap->heap.shrinker.scan_objects = ion_system_heap_shrink_scan;
351 heap->heap.shrinker.count_objects = ion_system_heap_shrink_count;
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800352 heap->heap.shrinker.seeks = DEFAULT_SEEKS;
353 heap->heap.shrinker.batch = 0;
354 register_shrinker(&heap->heap.shrinker);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800355 heap->heap.debug_show = ion_system_heap_debug_show;
356 return &heap->heap;
357err_create_pool:
358 for (i = 0; i < num_orders; i++)
359 if (heap->pools[i])
360 ion_page_pool_destroy(heap->pools[i]);
361 kfree(heap->pools);
362err_alloc_pools:
363 kfree(heap);
364 return ERR_PTR(-ENOMEM);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800365}
366
367void ion_system_heap_destroy(struct ion_heap *heap)
368{
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800369 struct ion_system_heap *sys_heap = container_of(heap,
370 struct ion_system_heap,
371 heap);
372 int i;
373
374 for (i = 0; i < num_orders; i++)
375 ion_page_pool_destroy(sys_heap->pools[i]);
376 kfree(sys_heap->pools);
377 kfree(sys_heap);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800378}
379
380static int ion_system_contig_heap_allocate(struct ion_heap *heap,
381 struct ion_buffer *buffer,
382 unsigned long len,
383 unsigned long align,
384 unsigned long flags)
385{
Colin Crossc13d1df2013-12-13 14:25:03 -0800386 int order = get_order(len);
Colin Cross5c6a4702013-12-13 19:26:27 -0800387 struct page *page;
388 struct sg_table *table;
389 unsigned long i;
390 int ret;
Colin Crossc13d1df2013-12-13 14:25:03 -0800391
392 if (align > (PAGE_SIZE << order))
393 return -EINVAL;
394
Colin Cross5c6a4702013-12-13 19:26:27 -0800395 page = alloc_pages(low_order_gfp_flags, order);
396 if (!page)
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800397 return -ENOMEM;
Colin Cross5c6a4702013-12-13 19:26:27 -0800398
399 split_page(page, order);
400
401 len = PAGE_ALIGN(len);
402 for (i = len >> PAGE_SHIFT; i < (1 << order); i++)
403 __free_page(page + i);
404
405 table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
406 if (!table) {
407 ret = -ENOMEM;
408 goto out;
409 }
410
411 ret = sg_alloc_table(table, 1, GFP_KERNEL);
412 if (ret)
413 goto out;
414
415 sg_set_page(table->sgl, page, len, 0);
416
417 buffer->priv_virt = table;
418
419 ion_pages_sync_for_device(NULL, page, len, DMA_BIDIRECTIONAL);
420
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800421 return 0;
Colin Cross5c6a4702013-12-13 19:26:27 -0800422
423out:
424 for (i = 0; i < len >> PAGE_SHIFT; i++)
425 __free_page(page + i);
426 kfree(table);
427 return ret;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800428}
429
Colin Crossf63958d2013-12-13 19:26:28 -0800430static void ion_system_contig_heap_free(struct ion_buffer *buffer)
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800431{
Colin Cross5c6a4702013-12-13 19:26:27 -0800432 struct sg_table *table = buffer->priv_virt;
433 struct page *page = sg_page(table->sgl);
434 unsigned long pages = PAGE_ALIGN(buffer->size) >> PAGE_SHIFT;
435 unsigned long i;
436
437 for (i = 0; i < pages; i++)
438 __free_page(page + i);
439 sg_free_table(table);
440 kfree(table);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800441}
442
443static int ion_system_contig_heap_phys(struct ion_heap *heap,
444 struct ion_buffer *buffer,
445 ion_phys_addr_t *addr, size_t *len)
446{
Colin Cross5c6a4702013-12-13 19:26:27 -0800447 struct sg_table *table = buffer->priv_virt;
448 struct page *page = sg_page(table->sgl);
449 *addr = page_to_phys(page);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800450 *len = buffer->size;
451 return 0;
452}
453
Colin Crossf63958d2013-12-13 19:26:28 -0800454static struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap,
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800455 struct ion_buffer *buffer)
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800456{
Colin Cross5c6a4702013-12-13 19:26:27 -0800457 return buffer->priv_virt;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800458}
459
Colin Crossf63958d2013-12-13 19:26:28 -0800460static void ion_system_contig_heap_unmap_dma(struct ion_heap *heap,
461 struct ion_buffer *buffer)
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800462{
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800463}
464
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800465static struct ion_heap_ops kmalloc_ops = {
466 .allocate = ion_system_contig_heap_allocate,
467 .free = ion_system_contig_heap_free,
468 .phys = ion_system_contig_heap_phys,
469 .map_dma = ion_system_contig_heap_map_dma,
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800470 .unmap_dma = ion_system_contig_heap_unmap_dma,
Rebecca Schultz Zavin88982272013-12-13 14:24:26 -0800471 .map_kernel = ion_heap_map_kernel,
472 .unmap_kernel = ion_heap_unmap_kernel,
Colin Crossa82130f2013-12-13 19:26:15 -0800473 .map_user = ion_heap_map_user,
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800474};
475
476struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *unused)
477{
478 struct ion_heap *heap;
479
480 heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
481 if (!heap)
482 return ERR_PTR(-ENOMEM);
483 heap->ops = &kmalloc_ops;
484 heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG;
485 return heap;
486}
487
488void ion_system_contig_heap_destroy(struct ion_heap *heap)
489{
490 kfree(heap);
491}
492