blob: 4e9f55c791cdbd095b291b3da78806855285c78b [file] [log] [blame]
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001/*
2 * drivers/gpu/ion/ion_system_heap.c
3 *
4 * Copyright (C) 2011 Google, Inc.
Mitchel Humpherysaf3b5222013-01-15 15:38:52 -08005 * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07006 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070018#include <asm/page.h>
19#include <linux/dma-mapping.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070020#include <linux/err.h>
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070021#include <linux/highmem.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070022#include <linux/ion.h>
23#include <linux/mm.h>
24#include <linux/scatterlist.h>
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -070025#include <linux/seq_file.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070026#include <linux/slab.h>
27#include <linux/vmalloc.h>
Olav Haugan3d4fe1a2012-01-13 11:42:15 -080028#include <linux/seq_file.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070029#include "ion_priv.h"
Laura Abbottabcb6f72011-10-04 16:26:49 -070030#include <mach/memory.h>
Olav Haugan85c95402012-05-30 17:32:37 -070031#include <asm/cacheflush.h>
Mitchel Humpherysaf2e5c52012-09-06 12:16:36 -070032#include <linux/msm_ion.h>
Neeti Desai3f3c2822013-03-08 17:29:53 -080033#include <linux/dma-mapping.h>
Adrian Alexei21f62bd2013-04-22 12:57:41 -070034#include <trace/events/kmem.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070035
Laura Abbott68c80642011-10-21 17:32:27 -070036static atomic_t system_heap_allocated;
37static atomic_t system_contig_heap_allocated;
38
Rebecca Schultz Zavinbff299e2012-10-02 22:43:41 -070039static unsigned int high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO |
40 __GFP_NOWARN | __GFP_NORETRY |
Rebecca Schultz Zavin1797e59a2012-10-18 21:51:53 -070041 __GFP_NO_KSWAPD) & ~__GFP_WAIT;
Rebecca Schultz Zavinbff299e2012-10-02 22:43:41 -070042static unsigned int low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO |
43 __GFP_NOWARN);
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -070044static const unsigned int orders[] = {8, 4, 0};
45static const int num_orders = ARRAY_SIZE(orders);
46static int order_to_index(unsigned int order)
47{
48 int i;
49 for (i = 0; i < num_orders; i++)
50 if (order == orders[i])
51 return i;
52 BUG();
53 return -1;
54}
55
56static unsigned int order_to_size(int order)
57{
58 return PAGE_SIZE << order;
59}
60
61struct ion_system_heap {
62 struct ion_heap heap;
63 struct ion_page_pool **pools;
64};
65
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070066struct page_info {
67 struct page *page;
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -070068 unsigned int order;
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070069 struct list_head list;
70};
71
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -070072static struct page *alloc_buffer_page(struct ion_system_heap *heap,
73 struct ion_buffer *buffer,
74 unsigned long order)
75{
76 bool cached = ion_buffer_cached(buffer);
77 bool split_pages = ion_buffer_fault_user_mappings(buffer);
78 struct ion_page_pool *pool = heap->pools[order_to_index(order)];
79 struct page *page;
Rebecca Schultz Zavin96dd58d2012-09-26 10:58:30 -070080
Rebecca Schultz Zavinbff299e2012-10-02 22:43:41 -070081 if (!cached) {
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -070082 page = ion_page_pool_alloc(pool);
Rebecca Schultz Zavinbff299e2012-10-02 22:43:41 -070083 } else {
Rebecca Schultz Zavin220a6522012-10-18 21:54:01 -070084 struct scatterlist sg;
Rebecca Schultz Zavinbff299e2012-10-02 22:43:41 -070085 gfp_t gfp_flags = low_order_gfp_flags;
86
87 if (order > 4)
88 gfp_flags = high_order_gfp_flags;
Adrian Alexei21f62bd2013-04-22 12:57:41 -070089 trace_alloc_pages_sys_start(gfp_flags, order);
Rebecca Schultz Zavinbff299e2012-10-02 22:43:41 -070090 page = alloc_pages(gfp_flags, order);
Adrian Alexei21f62bd2013-04-22 12:57:41 -070091 trace_alloc_pages_sys_end(gfp_flags, order);
92 if (!page) {
93 trace_alloc_pages_sys_fail(gfp_flags, order);
Rebecca Schultz Zavin220a6522012-10-18 21:54:01 -070094 return 0;
Adrian Alexei21f62bd2013-04-22 12:57:41 -070095 }
Rebecca Schultz Zavin220a6522012-10-18 21:54:01 -070096 sg_init_table(&sg, 1);
97 sg_set_page(&sg, page, PAGE_SIZE << order, 0);
98 dma_sync_sg_for_device(NULL, &sg, 1, DMA_BIDIRECTIONAL);
Rebecca Schultz Zavinbff299e2012-10-02 22:43:41 -070099 }
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700100 if (!page)
101 return 0;
Rebecca Schultz Zavin220a6522012-10-18 21:54:01 -0700102
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700103 if (split_pages)
104 split_page(page, order);
105 return page;
106}
107
108static void free_buffer_page(struct ion_system_heap *heap,
109 struct ion_buffer *buffer, struct page *page,
Rebecca Schultz Zavinca12f5d2013-01-09 11:26:37 -0800110 unsigned int order)
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700111{
112 bool cached = ion_buffer_cached(buffer);
113 bool split_pages = ion_buffer_fault_user_mappings(buffer);
114 int i;
115
116 if (!cached) {
117 struct ion_page_pool *pool = heap->pools[order_to_index(order)];
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700118 ion_page_pool_free(pool, page);
119 } else if (split_pages) {
120 for (i = 0; i < (1 << order); i++)
121 __free_page(page + i);
122 } else {
123 __free_pages(page, order);
124 }
125}
126
127
128static struct page_info *alloc_largest_available(struct ion_system_heap *heap,
129 struct ion_buffer *buffer,
130 unsigned long size,
Rebecca Schultz Zavin158316f2012-09-25 20:55:27 -0700131 unsigned int max_order)
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700132{
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700133 struct page *page;
134 struct page_info *info;
135 int i;
136
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700137 for (i = 0; i < num_orders; i++) {
138 if (size < order_to_size(orders[i]))
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700139 continue;
Rebecca Schultz Zavin158316f2012-09-25 20:55:27 -0700140 if (max_order < orders[i])
141 continue;
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700142
143 page = alloc_buffer_page(heap, buffer, orders[i]);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700144 if (!page)
145 continue;
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700146
147 info = kmalloc(sizeof(struct page_info), GFP_KERNEL);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700148 info->page = page;
149 info->order = orders[i];
150 return info;
151 }
152 return NULL;
153}
154
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700155static int ion_system_heap_allocate(struct ion_heap *heap,
156 struct ion_buffer *buffer,
157 unsigned long size, unsigned long align,
158 unsigned long flags)
159{
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700160 struct ion_system_heap *sys_heap = container_of(heap,
161 struct ion_system_heap,
162 heap);
Laura Abbottb14ed962012-01-30 14:18:08 -0800163 struct sg_table *table;
164 struct scatterlist *sg;
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700165 int ret;
166 struct list_head pages;
167 struct page_info *info, *tmp_info;
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -0700168 int i = 0;
Olav Haugand8770692013-04-17 16:11:31 -0700169 unsigned long size_remaining = PAGE_ALIGN(size);
Rebecca Schultz Zavin158316f2012-09-25 20:55:27 -0700170 unsigned int max_order = orders[0];
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700171 bool split_pages = ion_buffer_fault_user_mappings(buffer);
Rebecca Schultz Zavin158316f2012-09-25 20:55:27 -0700172
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700173 INIT_LIST_HEAD(&pages);
174 while (size_remaining > 0) {
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700175 info = alloc_largest_available(sys_heap, buffer, size_remaining, max_order);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700176 if (!info)
177 goto err;
178 list_add_tail(&info->list, &pages);
179 size_remaining -= (1 << info->order) * PAGE_SIZE;
Rebecca Schultz Zavin158316f2012-09-25 20:55:27 -0700180 max_order = info->order;
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -0700181 i++;
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700182 }
Laura Abbott68c80642011-10-21 17:32:27 -0700183
Laura Abbottb14ed962012-01-30 14:18:08 -0800184 table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
185 if (!table)
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700186 goto err;
187
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -0700188 if (split_pages)
189 ret = sg_alloc_table(table, PAGE_ALIGN(size) / PAGE_SIZE,
190 GFP_KERNEL);
191 else
192 ret = sg_alloc_table(table, i, GFP_KERNEL);
193
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700194 if (ret)
195 goto err1;
196
197 sg = table->sgl;
198 list_for_each_entry_safe(info, tmp_info, &pages, list) {
199 struct page *page = info->page;
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -0700200 if (split_pages) {
201 for (i = 0; i < (1 << info->order); i++) {
202 sg_set_page(sg, page + i, PAGE_SIZE, 0);
203 sg = sg_next(sg);
204 }
205 } else {
206 sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE,
207 0);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700208 sg = sg_next(sg);
209 }
210 list_del(&info->list);
Rebecca Schultz Zavin6a93a292012-08-21 21:35:20 -0700211 kfree(info);
Laura Abbottb14ed962012-01-30 14:18:08 -0800212 }
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700213
Laura Abbottb14ed962012-01-30 14:18:08 -0800214 buffer->priv_virt = table;
Laura Abbott68c80642011-10-21 17:32:27 -0700215 atomic_add(size, &system_heap_allocated);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700216 return 0;
Laura Abbottb14ed962012-01-30 14:18:08 -0800217err1:
Laura Abbottb14ed962012-01-30 14:18:08 -0800218 kfree(table);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700219err:
220 list_for_each_entry(info, &pages, list) {
Rebecca Schultz Zavinca12f5d2013-01-09 11:26:37 -0800221 free_buffer_page(sys_heap, buffer, info->page, info->order);
Rebecca Schultz Zavin6a93a292012-08-21 21:35:20 -0700222 kfree(info);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700223 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800224 return -ENOMEM;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700225}
226
227void ion_system_heap_free(struct ion_buffer *buffer)
228{
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700229 struct ion_heap *heap = buffer->heap;
230 struct ion_system_heap *sys_heap = container_of(heap,
231 struct ion_system_heap,
232 heap);
Rebecca Schultz Zavin3df181c2012-11-15 10:43:46 -0800233 struct sg_table *table = buffer->sg_table;
Rebecca Schultz Zavinca12f5d2013-01-09 11:26:37 -0800234 bool cached = ion_buffer_cached(buffer);
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700235 struct scatterlist *sg;
236 LIST_HEAD(pages);
237 int i;
Laura Abbottb14ed962012-01-30 14:18:08 -0800238
Rebecca Schultz Zavinca12f5d2013-01-09 11:26:37 -0800239 /* uncached pages come from the page pools, zero them before returning
240 for security purposes (other allocations are zerod at alloc time */
241 if (!cached)
242 ion_heap_buffer_zero(buffer);
Rebecca Schultz Zavinf9101222012-12-18 22:46:57 -0800243
Laura Abbottb14ed962012-01-30 14:18:08 -0800244 for_each_sg(table->sgl, sg, table->nents, i)
Rebecca Schultz Zavinf9101222012-12-18 22:46:57 -0800245 free_buffer_page(sys_heap, buffer, sg_page(sg),
Rebecca Schultz Zavinca12f5d2013-01-09 11:26:37 -0800246 get_order(sg_dma_len(sg)));
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700247 sg_free_table(table);
248 kfree(table);
Laura Abbott68c80642011-10-21 17:32:27 -0700249 atomic_sub(buffer->size, &system_heap_allocated);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700250}
251
Laura Abbottb14ed962012-01-30 14:18:08 -0800252struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap,
253 struct ion_buffer *buffer)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700254{
Laura Abbottb14ed962012-01-30 14:18:08 -0800255 return buffer->priv_virt;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700256}
257
258void ion_system_heap_unmap_dma(struct ion_heap *heap,
259 struct ion_buffer *buffer)
260{
Laura Abbottb14ed962012-01-30 14:18:08 -0800261 return;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700262}
263
Olav Haugan0671b9a2012-05-25 11:58:56 -0700264static int ion_system_print_debug(struct ion_heap *heap, struct seq_file *s,
265 const struct rb_root *unused)
Laura Abbott68c80642011-10-21 17:32:27 -0700266{
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800267 seq_printf(s, "total bytes currently allocated: %lx\n",
268 (unsigned long) atomic_read(&system_heap_allocated));
269
270 return 0;
Laura Abbott68c80642011-10-21 17:32:27 -0700271}
272
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700273static struct ion_heap_ops system_heap_ops = {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700274 .allocate = ion_system_heap_allocate,
275 .free = ion_system_heap_free,
276 .map_dma = ion_system_heap_map_dma,
277 .unmap_dma = ion_system_heap_unmap_dma,
Rebecca Schultz Zavin3df181c2012-11-15 10:43:46 -0800278 .map_kernel = ion_heap_map_kernel,
279 .unmap_kernel = ion_heap_unmap_kernel,
280 .map_user = ion_heap_map_user,
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800281 .print_debug = ion_system_print_debug,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700282};
283
Olav Haugan85c95402012-05-30 17:32:37 -0700284struct ion_heap *ion_system_heap_create(struct ion_platform_heap *pheap)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700285{
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700286 struct ion_system_heap *heap;
287 int i;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700288
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700289 heap = kzalloc(sizeof(struct ion_system_heap), GFP_KERNEL);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700290 if (!heap)
291 return ERR_PTR(-ENOMEM);
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700292 heap->heap.ops = &system_heap_ops;
293 heap->heap.type = ION_HEAP_TYPE_SYSTEM;
Rebecca Schultz Zavin618d6be2013-02-13 14:48:11 -0800294 heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE;
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700295 heap->pools = kzalloc(sizeof(struct ion_page_pool *) * num_orders,
296 GFP_KERNEL);
297 if (!heap->pools)
298 goto err_alloc_pools;
299 for (i = 0; i < num_orders; i++) {
300 struct ion_page_pool *pool;
Rebecca Schultz Zavinbff299e2012-10-02 22:43:41 -0700301 gfp_t gfp_flags = low_order_gfp_flags;
302
303 if (orders[i] > 4)
304 gfp_flags = high_order_gfp_flags;
305 pool = ion_page_pool_create(gfp_flags, orders[i]);
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700306 if (!pool)
307 goto err_create_pool;
308 heap->pools[i] = pool;
309 }
310 return &heap->heap;
311err_create_pool:
312 for (i = 0; i < num_orders; i++)
313 if (heap->pools[i])
314 ion_page_pool_destroy(heap->pools[i]);
315 kfree(heap->pools);
316err_alloc_pools:
317 kfree(heap);
318 return ERR_PTR(-ENOMEM);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700319}
320
321void ion_system_heap_destroy(struct ion_heap *heap)
322{
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700323 struct ion_system_heap *sys_heap = container_of(heap,
324 struct ion_system_heap,
325 heap);
326 int i;
327
328 for (i = 0; i < num_orders; i++)
329 ion_page_pool_destroy(sys_heap->pools[i]);
330 kfree(sys_heap->pools);
331 kfree(sys_heap);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700332}
333
334static int ion_system_contig_heap_allocate(struct ion_heap *heap,
335 struct ion_buffer *buffer,
336 unsigned long len,
337 unsigned long align,
338 unsigned long flags)
339{
340 buffer->priv_virt = kzalloc(len, GFP_KERNEL);
341 if (!buffer->priv_virt)
342 return -ENOMEM;
Laura Abbott68c80642011-10-21 17:32:27 -0700343 atomic_add(len, &system_contig_heap_allocated);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700344 return 0;
345}
346
347void ion_system_contig_heap_free(struct ion_buffer *buffer)
348{
349 kfree(buffer->priv_virt);
Laura Abbott68c80642011-10-21 17:32:27 -0700350 atomic_sub(buffer->size, &system_contig_heap_allocated);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700351}
352
353static int ion_system_contig_heap_phys(struct ion_heap *heap,
354 struct ion_buffer *buffer,
355 ion_phys_addr_t *addr, size_t *len)
356{
357 *addr = virt_to_phys(buffer->priv_virt);
358 *len = buffer->size;
359 return 0;
360}
361
Laura Abbottb14ed962012-01-30 14:18:08 -0800362struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap,
Rebecca Schultz Zavinb1790672012-06-14 15:08:53 -0700363 struct ion_buffer *buffer)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700364{
Laura Abbottb14ed962012-01-30 14:18:08 -0800365 struct sg_table *table;
366 int ret;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700367
Laura Abbottb14ed962012-01-30 14:18:08 -0800368 table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
369 if (!table)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700370 return ERR_PTR(-ENOMEM);
Laura Abbottb14ed962012-01-30 14:18:08 -0800371 ret = sg_alloc_table(table, 1, GFP_KERNEL);
372 if (ret) {
373 kfree(table);
374 return ERR_PTR(ret);
375 }
376 sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size,
377 0);
378 return table;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700379}
380
Rebecca Schultz Zavinb1790672012-06-14 15:08:53 -0700381void ion_system_contig_heap_unmap_dma(struct ion_heap *heap,
382 struct ion_buffer *buffer)
383{
384 sg_free_table(buffer->sg_table);
385 kfree(buffer->sg_table);
386}
387
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700388int ion_system_contig_heap_map_user(struct ion_heap *heap,
389 struct ion_buffer *buffer,
Laura Abbottb14ed962012-01-30 14:18:08 -0800390 struct vm_area_struct *vma)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700391{
392 unsigned long pfn = __phys_to_pfn(virt_to_phys(buffer->priv_virt));
Laura Abbott894fd582011-08-19 13:33:56 -0700393
Laura Abbottb14ed962012-01-30 14:18:08 -0800394 if (ION_IS_CACHED(buffer->flags))
Laura Abbott894fd582011-08-19 13:33:56 -0700395 return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700396 vma->vm_end - vma->vm_start,
397 vma->vm_page_prot);
Laura Abbott894fd582011-08-19 13:33:56 -0700398 else {
399 pr_err("%s: cannot map system heap uncached\n", __func__);
400 return -EINVAL;
401 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700402}
403
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800404static int ion_system_contig_print_debug(struct ion_heap *heap,
Olav Haugan0671b9a2012-05-25 11:58:56 -0700405 struct seq_file *s,
406 const struct rb_root *unused)
Laura Abbott68c80642011-10-21 17:32:27 -0700407{
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800408 seq_printf(s, "total bytes currently allocated: %lx\n",
409 (unsigned long) atomic_read(&system_contig_heap_allocated));
410
411 return 0;
Laura Abbott68c80642011-10-21 17:32:27 -0700412}
413
Rohit Vaswani35edc882012-11-20 10:20:47 -0800414void *ion_system_contig_heap_map_kernel(struct ion_heap *heap,
415 struct ion_buffer *buffer)
416{
417 return buffer->priv_virt;
418}
419
420void ion_system_contig_heap_unmap_kernel(struct ion_heap *heap,
421 struct ion_buffer *buffer)
422{
423 return;
424}
425
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700426static struct ion_heap_ops kmalloc_ops = {
427 .allocate = ion_system_contig_heap_allocate,
428 .free = ion_system_contig_heap_free,
429 .phys = ion_system_contig_heap_phys,
430 .map_dma = ion_system_contig_heap_map_dma,
Rebecca Schultz Zavinb1790672012-06-14 15:08:53 -0700431 .unmap_dma = ion_system_contig_heap_unmap_dma,
Rebecca Schultz Zavin3df181c2012-11-15 10:43:46 -0800432 .map_kernel = ion_heap_map_kernel,
433 .unmap_kernel = ion_heap_unmap_kernel,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700434 .map_user = ion_system_contig_heap_map_user,
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800435 .print_debug = ion_system_contig_print_debug,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700436};
437
Olav Haugan85c95402012-05-30 17:32:37 -0700438struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *pheap)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700439{
440 struct ion_heap *heap;
441
442 heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
443 if (!heap)
444 return ERR_PTR(-ENOMEM);
445 heap->ops = &kmalloc_ops;
446 heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700447 return heap;
448}
449
450void ion_system_contig_heap_destroy(struct ion_heap *heap)
451{
452 kfree(heap);
453}
454