blob: d4c3e5512dd54dbcf0b3f6d9a927a1e8bbb3fc5a [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 |
Mel Gormand0164ad2015-11-06 16:28:21 -080030 __GFP_NORETRY) & ~__GFP_DIRECT_RECLAIM;
Colin Crossf63958d2013-12-13 19:26:28 -080031static 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;
Seunghun Lee10f62862014-05-01 01:30:23 +090037
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080038 for (i = 0; i < num_orders; i++)
39 if (order == orders[i])
40 return i;
41 BUG();
42 return -1;
43}
44
Heesub Shin79240742014-05-28 15:52:52 +090045static inline unsigned int order_to_size(int order)
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080046{
47 return PAGE_SIZE << order;
48}
49
50struct ion_system_heap {
51 struct ion_heap heap;
Heesub Shin69445612014-05-30 10:26:30 +090052 struct ion_page_pool *pools[0];
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080053};
54
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080055static 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);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080060 struct ion_page_pool *pool = heap->pools[order_to_index(order)];
61 struct page *page;
Rebecca Schultz Zavinb0599c02013-12-13 14:24:08 -080062
Rebecca Schultz Zavinee4a4982013-12-13 14:24:12 -080063 if (!cached) {
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080064 page = ion_page_pool_alloc(pool);
Rebecca Schultz Zavinee4a4982013-12-13 14:24:12 -080065 } else {
66 gfp_t gfp_flags = low_order_gfp_flags;
67
68 if (order > 4)
69 gfp_flags = high_order_gfp_flags;
Heesub Shinbdeb9f12014-05-28 15:52:55 +090070 page = alloc_pages(gfp_flags | __GFP_COMP, order);
Rebecca Schultz Zavin8fae8312013-12-13 14:24:18 -080071 if (!page)
Colin Crossf63958d2013-12-13 19:26:28 -080072 return NULL;
Colin Crosse946b202013-12-13 14:25:01 -080073 ion_pages_sync_for_device(NULL, page, PAGE_SIZE << order,
74 DMA_BIDIRECTIONAL);
Rebecca Schultz Zavinee4a4982013-12-13 14:24:12 -080075 }
Rebecca Schultz Zavin8fae8312013-12-13 14:24:18 -080076
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080077 return page;
78}
79
80static void free_buffer_page(struct ion_system_heap *heap,
Heesub Shin06566f52014-05-30 10:26:29 +090081 struct ion_buffer *buffer, struct page *page)
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080082{
Heesub Shin06566f52014-05-30 10:26:29 +090083 unsigned int order = compound_order(page);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080084 bool cached = ion_buffer_cached(buffer);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080085
Mitchel Humpherys53a91c62014-02-17 13:58:39 -080086 if (!cached && !(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE)) {
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080087 struct ion_page_pool *pool = heap->pools[order_to_index(order)];
Seunghun Lee10f62862014-05-01 01:30:23 +090088
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080089 ion_page_pool_free(pool, page);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -080090 } else {
91 __free_pages(page, order);
92 }
93}
94
95
Heesub Shin7eb88bf2014-05-30 10:26:28 +090096static struct page *alloc_largest_available(struct ion_system_heap *heap,
97 struct ion_buffer *buffer,
98 unsigned long size,
99 unsigned int max_order)
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800100{
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800101 struct page *page;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800102 int i;
103
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800104 for (i = 0; i < num_orders; i++) {
105 if (size < order_to_size(orders[i]))
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800106 continue;
Rebecca Schultz Zavinba96a2e2013-12-13 14:24:07 -0800107 if (max_order < orders[i])
108 continue;
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800109
110 page = alloc_buffer_page(heap, buffer, orders[i]);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800111 if (!page)
112 continue;
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800113
Heesub Shin7eb88bf2014-05-30 10:26:28 +0900114 return page;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800115 }
John Stultzf4ea8232013-12-16 21:07:52 -0800116
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800117 return NULL;
118}
119
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800120static 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{
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800125 struct ion_system_heap *sys_heap = container_of(heap,
126 struct ion_system_heap,
127 heap);
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800128 struct sg_table *table;
129 struct scatterlist *sg;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800130 struct list_head pages;
Heesub Shin7eb88bf2014-05-30 10:26:28 +0900131 struct page *page, *tmp_page;
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -0800132 int i = 0;
Colin Crossc9e84402014-02-04 16:08:38 -0800133 unsigned long size_remaining = PAGE_ALIGN(size);
Rebecca Schultz Zavinba96a2e2013-12-13 14:24:07 -0800134 unsigned int max_order = orders[0];
135
Colin Crossc13d1df2013-12-13 14:25:03 -0800136 if (align > PAGE_SIZE)
137 return -EINVAL;
138
Colin Crossc9e84402014-02-04 16:08:38 -0800139 if (size / PAGE_SIZE > totalram_pages / 2)
140 return -ENOMEM;
141
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800142 INIT_LIST_HEAD(&pages);
143 while (size_remaining > 0) {
Heesub Shin7eb88bf2014-05-30 10:26:28 +0900144 page = alloc_largest_available(sys_heap, buffer, size_remaining,
John Stultze1d855b2013-12-13 19:26:33 -0800145 max_order);
Heesub Shin7eb88bf2014-05-30 10:26:28 +0900146 if (!page)
Heesub Shin79240742014-05-28 15:52:52 +0900147 goto free_pages;
Heesub Shin7eb88bf2014-05-30 10:26:28 +0900148 list_add_tail(&page->lru, &pages);
149 size_remaining -= PAGE_SIZE << compound_order(page);
150 max_order = compound_order(page);
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -0800151 i++;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800152 }
Gioh Kimb6152012014-04-25 08:24:24 +0900153 table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800154 if (!table)
Heesub Shin79240742014-05-28 15:52:52 +0900155 goto free_pages;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800156
Heesub Shin79240742014-05-28 15:52:52 +0900157 if (sg_alloc_table(table, i, GFP_KERNEL))
158 goto free_table;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800159
160 sg = table->sgl;
Heesub Shin7eb88bf2014-05-30 10:26:28 +0900161 list_for_each_entry_safe(page, tmp_page, &pages, lru) {
Heesub Shind10e4ff2014-05-30 10:26:27 +0900162 sg_set_page(sg, page, PAGE_SIZE << compound_order(page), 0);
Rebecca Schultz Zavinc13bd1c2013-12-13 14:24:45 -0800163 sg = sg_next(sg);
Heesub Shin7eb88bf2014-05-30 10:26:28 +0900164 list_del(&page->lru);
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800165 }
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800166
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800167 buffer->priv_virt = table;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800168 return 0;
Heesub Shin79240742014-05-28 15:52:52 +0900169
170free_table:
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800171 kfree(table);
Heesub Shin79240742014-05-28 15:52:52 +0900172free_pages:
Heesub Shin7eb88bf2014-05-30 10:26:28 +0900173 list_for_each_entry_safe(page, tmp_page, &pages, lru)
Heesub Shin06566f52014-05-30 10:26:29 +0900174 free_buffer_page(sys_heap, buffer, page);
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800175 return -ENOMEM;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800176}
177
Colin Crossf63958d2013-12-13 19:26:28 -0800178static void ion_system_heap_free(struct ion_buffer *buffer)
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800179{
Heesub Shin79240742014-05-28 15:52:52 +0900180 struct ion_system_heap *sys_heap = container_of(buffer->heap,
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800181 struct ion_system_heap,
182 heap);
Rebecca Schultz Zavin88982272013-12-13 14:24:26 -0800183 struct sg_table *table = buffer->sg_table;
Rebecca Schultz Zavin0b6b2cd2013-12-13 14:24:32 -0800184 bool cached = ion_buffer_cached(buffer);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800185 struct scatterlist *sg;
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800186 int i;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800187
Sriram Raghunathan7e416172015-09-22 22:35:51 +0530188 /*
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 */
Mitchel Humpherys53a91c62014-02-17 13:58:39 -0800193 if (!cached && !(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE))
Rebecca Schultz Zavin0b6b2cd2013-12-13 14:24:32 -0800194 ion_heap_buffer_zero(buffer);
Rebecca Schultz Zavin77cbe822013-12-13 14:24:31 -0800195
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800196 for_each_sg(table->sgl, sg, table->nents, i)
Heesub Shin06566f52014-05-30 10:26:29 +0900197 free_buffer_page(sys_heap, buffer, sg_page(sg));
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800198 sg_free_table(table);
199 kfree(table);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800200}
201
Colin Crossf63958d2013-12-13 19:26:28 -0800202static struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap,
203 struct ion_buffer *buffer)
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800204{
205 return buffer->priv_virt;
206}
207
Colin Crossf63958d2013-12-13 19:26:28 -0800208static void ion_system_heap_unmap_dma(struct ion_heap *heap,
209 struct ion_buffer *buffer)
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800210{
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800211}
212
Colin Crossb9daf0b2014-02-17 13:58:38 -0800213static 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;
Gioh Kimb44d9ce2015-07-06 15:14:40 +0900218 int i, nr_freed;
219 int only_scan = 0;
Colin Crossb9daf0b2014-02-17 13:58:38 -0800220
221 sys_heap = container_of(heap, struct ion_system_heap, heap);
222
Gioh Kimb44d9ce2015-07-06 15:14:40 +0900223 if (!nr_to_scan)
224 only_scan = 1;
225
Colin Crossb9daf0b2014-02-17 13:58:38 -0800226 for (i = 0; i < num_orders; i++) {
227 struct ion_page_pool *pool = sys_heap->pools[i];
Seunghun Lee10f62862014-05-01 01:30:23 +0900228
Gioh Kimb44d9ce2015-07-06 15:14:40 +0900229 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 }
Colin Crossb9daf0b2014-02-17 13:58:38 -0800238 }
239
240 return nr_total;
241}
242
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800243static struct ion_heap_ops system_heap_ops = {
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800244 .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,
Rebecca Schultz Zavin88982272013-12-13 14:24:26 -0800248 .map_kernel = ion_heap_map_kernel,
249 .unmap_kernel = ion_heap_unmap_kernel,
250 .map_user = ion_heap_map_user,
Colin Crossb9daf0b2014-02-17 13:58:38 -0800251 .shrink = ion_system_heap_shrink,
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800252};
253
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800254static 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;
Seunghun Lee10f62862014-05-01 01:30:23 +0900262
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800263 for (i = 0; i < num_orders; i++) {
264 struct ion_page_pool *pool = sys_heap->pools[i];
Seunghun Lee10f62862014-05-01 01:30:23 +0900265
Rebecca Schultz Zavin0fb9b812013-12-13 14:24:13 -0800266 seq_printf(s, "%d order %u highmem pages in pool = %lu total\n",
267 pool->high_count, pool->order,
Heesub Shin79240742014-05-28 15:52:52 +0900268 (PAGE_SIZE << pool->order) * pool->high_count);
Rebecca Schultz Zavin0fb9b812013-12-13 14:24:13 -0800269 seq_printf(s, "%d order %u lowmem pages in pool = %lu total\n",
270 pool->low_count, pool->order,
Heesub Shin79240742014-05-28 15:52:52 +0900271 (PAGE_SIZE << pool->order) * pool->low_count);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800272 }
273 return 0;
274}
275
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800276struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused)
277{
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800278 struct ion_system_heap *heap;
279 int i;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800280
Heesub Shin69445612014-05-30 10:26:30 +0900281 heap = kzalloc(sizeof(struct ion_system_heap) +
282 sizeof(struct ion_page_pool *) * num_orders,
283 GFP_KERNEL);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800284 if (!heap)
285 return ERR_PTR(-ENOMEM);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800286 heap->heap.ops = &system_heap_ops;
287 heap->heap.type = ION_HEAP_TYPE_SYSTEM;
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800288 heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE;
Heesub Shin69445612014-05-30 10:26:30 +0900289
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800290 for (i = 0; i < num_orders; i++) {
291 struct ion_page_pool *pool;
Rebecca Schultz Zavinee4a4982013-12-13 14:24:12 -0800292 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]);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800297 if (!pool)
Heesub Shin79240742014-05-28 15:52:52 +0900298 goto destroy_pools;
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800299 heap->pools[i] = pool;
300 }
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800301
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800302 heap->heap.debug_show = ion_system_heap_debug_show;
303 return &heap->heap;
Heesub Shin79240742014-05-28 15:52:52 +0900304
305destroy_pools:
306 while (i--)
307 ion_page_pool_destroy(heap->pools[i]);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800308 kfree(heap);
309 return ERR_PTR(-ENOMEM);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800310}
311
312void ion_system_heap_destroy(struct ion_heap *heap)
313{
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800314 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]);
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800321 kfree(sys_heap);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800322}
323
324static 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{
Colin Crossc13d1df2013-12-13 14:25:03 -0800330 int order = get_order(len);
Colin Cross5c6a4702013-12-13 19:26:27 -0800331 struct page *page;
332 struct sg_table *table;
333 unsigned long i;
334 int ret;
Colin Crossc13d1df2013-12-13 14:25:03 -0800335
336 if (align > (PAGE_SIZE << order))
337 return -EINVAL;
338
Colin Cross5c6a4702013-12-13 19:26:27 -0800339 page = alloc_pages(low_order_gfp_flags, order);
340 if (!page)
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800341 return -ENOMEM;
Colin Cross5c6a4702013-12-13 19:26:27 -0800342
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
Gioh Kimb6152012014-04-25 08:24:24 +0900349 table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
Colin Cross5c6a4702013-12-13 19:26:27 -0800350 if (!table) {
351 ret = -ENOMEM;
Heesub Shin79240742014-05-28 15:52:52 +0900352 goto free_pages;
Colin Cross5c6a4702013-12-13 19:26:27 -0800353 }
354
355 ret = sg_alloc_table(table, 1, GFP_KERNEL);
356 if (ret)
Heesub Shin79240742014-05-28 15:52:52 +0900357 goto free_table;
Colin Cross5c6a4702013-12-13 19:26:27 -0800358
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
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800365 return 0;
Colin Cross5c6a4702013-12-13 19:26:27 -0800366
Heesub Shin79240742014-05-28 15:52:52 +0900367free_table:
368 kfree(table);
369free_pages:
Colin Cross5c6a4702013-12-13 19:26:27 -0800370 for (i = 0; i < len >> PAGE_SHIFT; i++)
371 __free_page(page + i);
Heesub Shin79240742014-05-28 15:52:52 +0900372
Colin Cross5c6a4702013-12-13 19:26:27 -0800373 return ret;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800374}
375
Colin Crossf63958d2013-12-13 19:26:28 -0800376static void ion_system_contig_heap_free(struct ion_buffer *buffer)
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800377{
Colin Cross5c6a4702013-12-13 19:26:27 -0800378 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);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800387}
388
389static 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{
Colin Cross5c6a4702013-12-13 19:26:27 -0800393 struct sg_table *table = buffer->priv_virt;
394 struct page *page = sg_page(table->sgl);
395 *addr = page_to_phys(page);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800396 *len = buffer->size;
397 return 0;
398}
399
Colin Crossf63958d2013-12-13 19:26:28 -0800400static struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap,
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800401 struct ion_buffer *buffer)
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800402{
Colin Cross5c6a4702013-12-13 19:26:27 -0800403 return buffer->priv_virt;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800404}
405
Colin Crossf63958d2013-12-13 19:26:28 -0800406static void ion_system_contig_heap_unmap_dma(struct ion_heap *heap,
407 struct ion_buffer *buffer)
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800408{
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800409}
410
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800411static 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,
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800416 .unmap_dma = ion_system_contig_heap_unmap_dma,
Rebecca Schultz Zavin88982272013-12-13 14:24:26 -0800417 .map_kernel = ion_heap_map_kernel,
418 .unmap_kernel = ion_heap_unmap_kernel,
Colin Crossa82130f2013-12-13 19:26:15 -0800419 .map_user = ion_heap_map_user,
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800420};
421
422struct 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
434void ion_system_contig_heap_destroy(struct ion_heap *heap)
435{
436 kfree(heap);
437}