blob: b51fa6a1c1c66afa8644f254ff49fc2faa370fdd [file] [log] [blame]
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001/*
2 * drivers/gpu/ion/ion_carveout_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 Zavin0c38bfd2011-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#include <linux/spinlock.h>
18
19#include <linux/err.h>
20#include <linux/genalloc.h>
21#include <linux/io.h>
22#include <linux/ion.h>
23#include <linux/mm.h>
24#include <linux/scatterlist.h>
25#include <linux/slab.h>
26#include <linux/vmalloc.h>
Laura Abbott8c017362011-09-22 20:59:12 -070027#include <linux/iommu.h>
Olav Haugan3d4fe1a2012-01-13 11:42:15 -080028#include <linux/seq_file.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070029#include "ion_priv.h"
30
Laura Abbott8c017362011-09-22 20:59:12 -070031#include <mach/iommu_domains.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070032#include <asm/mach/map.h>
Olav Haugan85c95402012-05-30 17:32:37 -070033#include <asm/cacheflush.h>
Mitchel Humpherysaf2e5c52012-09-06 12:16:36 -070034#include <linux/msm_ion.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070035
36struct ion_carveout_heap {
37 struct ion_heap heap;
38 struct gen_pool *pool;
39 ion_phys_addr_t base;
Laura Abbott68c80642011-10-21 17:32:27 -070040 unsigned long allocated_bytes;
41 unsigned long total_size;
Olav Hauganee0f7802011-12-19 13:28:57 -080042 int (*request_region)(void *);
43 int (*release_region)(void *);
Alex Bird8a3ede32011-11-07 12:33:42 -080044 atomic_t map_count;
45 void *bus_id;
Olav Haugan85c95402012-05-30 17:32:37 -070046 unsigned int has_outer_cache;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070047};
48
49ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap,
50 unsigned long size,
51 unsigned long align)
52{
53 struct ion_carveout_heap *carveout_heap =
54 container_of(heap, struct ion_carveout_heap, heap);
Laura Abbott5ee39212011-10-11 10:12:40 -070055 unsigned long offset = gen_pool_alloc_aligned(carveout_heap->pool,
56 size, ilog2(align));
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070057
Laura Abbottd16dd602011-10-31 13:51:10 -070058 if (!offset) {
59 if ((carveout_heap->total_size -
Olav Haugan739e5182012-04-19 14:14:43 -070060 carveout_heap->allocated_bytes) >= size)
Laura Abbottd16dd602011-10-31 13:51:10 -070061 pr_debug("%s: heap %s has enough memory (%lx) but"
62 " the allocation of size %lx still failed."
63 " Memory is probably fragmented.",
64 __func__, heap->name,
65 carveout_heap->total_size -
66 carveout_heap->allocated_bytes, size);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070067 return ION_CARVEOUT_ALLOCATE_FAIL;
Laura Abbottd16dd602011-10-31 13:51:10 -070068 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070069
Laura Abbott68c80642011-10-21 17:32:27 -070070 carveout_heap->allocated_bytes += size;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070071 return offset;
72}
73
74void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
75 unsigned long size)
76{
77 struct ion_carveout_heap *carveout_heap =
78 container_of(heap, struct ion_carveout_heap, heap);
79
80 if (addr == ION_CARVEOUT_ALLOCATE_FAIL)
81 return;
82 gen_pool_free(carveout_heap->pool, addr, size);
Laura Abbott68c80642011-10-21 17:32:27 -070083 carveout_heap->allocated_bytes -= size;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070084}
85
86static int ion_carveout_heap_phys(struct ion_heap *heap,
87 struct ion_buffer *buffer,
88 ion_phys_addr_t *addr, size_t *len)
89{
90 *addr = buffer->priv_phys;
91 *len = buffer->size;
92 return 0;
93}
94
95static int ion_carveout_heap_allocate(struct ion_heap *heap,
96 struct ion_buffer *buffer,
97 unsigned long size, unsigned long align,
98 unsigned long flags)
99{
100 buffer->priv_phys = ion_carveout_allocate(heap, size, align);
101 return buffer->priv_phys == ION_CARVEOUT_ALLOCATE_FAIL ? -ENOMEM : 0;
102}
103
104static void ion_carveout_heap_free(struct ion_buffer *buffer)
105{
106 struct ion_heap *heap = buffer->heap;
107
108 ion_carveout_free(heap, buffer->priv_phys, buffer->size);
109 buffer->priv_phys = ION_CARVEOUT_ALLOCATE_FAIL;
110}
111
Laura Abbottb14ed962012-01-30 14:18:08 -0800112struct sg_table *ion_carveout_heap_map_dma(struct ion_heap *heap,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700113 struct ion_buffer *buffer)
114{
Laura Abbottb14ed962012-01-30 14:18:08 -0800115 struct sg_table *table;
116 int ret;
Jordan Crouse0a1195e2011-10-20 14:03:31 -0600117
Laura Abbottb14ed962012-01-30 14:18:08 -0800118 table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
119 if (!table)
Jordan Crouse0a1195e2011-10-20 14:03:31 -0600120 return ERR_PTR(-ENOMEM);
121
Laura Abbottb14ed962012-01-30 14:18:08 -0800122 ret = sg_alloc_table(table, 1, GFP_KERNEL);
123 if (ret)
124 goto err0;
Jordan Crouse0a1195e2011-10-20 14:03:31 -0600125
Laura Abbottb14ed962012-01-30 14:18:08 -0800126 table->sgl->length = buffer->size;
127 table->sgl->offset = 0;
128 table->sgl->dma_address = buffer->priv_phys;
129
130 return table;
131
132err0:
133 kfree(table);
134 return ERR_PTR(ret);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700135}
136
137void ion_carveout_heap_unmap_dma(struct ion_heap *heap,
138 struct ion_buffer *buffer)
139{
Laura Abbottb14ed962012-01-30 14:18:08 -0800140 if (buffer->sg_table)
141 sg_free_table(buffer->sg_table);
142 kfree(buffer->sg_table);
143 buffer->sg_table = 0;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700144}
145
Olav Haugancb9ad9e2012-01-09 11:15:14 -0800146static int ion_carveout_request_region(struct ion_carveout_heap *carveout_heap)
147{
148 int ret_value = 0;
149 if (atomic_inc_return(&carveout_heap->map_count) == 1) {
150 if (carveout_heap->request_region) {
151 ret_value = carveout_heap->request_region(
152 carveout_heap->bus_id);
153 if (ret_value) {
154 pr_err("Unable to request SMI region");
155 atomic_dec(&carveout_heap->map_count);
156 }
157 }
158 }
159 return ret_value;
160}
161
162static int ion_carveout_release_region(struct ion_carveout_heap *carveout_heap)
163{
164 int ret_value = 0;
165 if (atomic_dec_and_test(&carveout_heap->map_count)) {
166 if (carveout_heap->release_region) {
167 ret_value = carveout_heap->release_region(
168 carveout_heap->bus_id);
169 if (ret_value)
170 pr_err("Unable to release SMI region");
171 }
172 }
173 return ret_value;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700174}
175
176void *ion_carveout_heap_map_kernel(struct ion_heap *heap,
Laura Abbottb14ed962012-01-30 14:18:08 -0800177 struct ion_buffer *buffer)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700178{
Alex Bird8a3ede32011-11-07 12:33:42 -0800179 struct ion_carveout_heap *carveout_heap =
180 container_of(heap, struct ion_carveout_heap, heap);
Olav Haugancb9ad9e2012-01-09 11:15:14 -0800181 void *ret_value;
Alex Bird8a3ede32011-11-07 12:33:42 -0800182
Olav Haugancb9ad9e2012-01-09 11:15:14 -0800183 if (ion_carveout_request_region(carveout_heap))
184 return NULL;
185
Laura Abbottb14ed962012-01-30 14:18:08 -0800186 if (ION_IS_CACHED(buffer->flags))
Olav Haugancb9ad9e2012-01-09 11:15:14 -0800187 ret_value = ioremap_cached(buffer->priv_phys, buffer->size);
Laura Abbotte8f28022011-09-28 14:40:40 -0700188 else
Olav Haugancb9ad9e2012-01-09 11:15:14 -0800189 ret_value = ioremap(buffer->priv_phys, buffer->size);
190
191 if (!ret_value)
192 ion_carveout_release_region(carveout_heap);
193 return ret_value;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700194}
195
196void ion_carveout_heap_unmap_kernel(struct ion_heap *heap,
197 struct ion_buffer *buffer)
198{
Alex Bird8a3ede32011-11-07 12:33:42 -0800199 struct ion_carveout_heap *carveout_heap =
200 container_of(heap, struct ion_carveout_heap, heap);
201
Colin Cross9a826402012-04-04 13:50:43 -0700202 __arm_iounmap(buffer->vaddr);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700203 buffer->vaddr = NULL;
Alex Bird8a3ede32011-11-07 12:33:42 -0800204
Olav Haugancb9ad9e2012-01-09 11:15:14 -0800205 ion_carveout_release_region(carveout_heap);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700206 return;
207}
208
209int ion_carveout_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer,
Laura Abbottb14ed962012-01-30 14:18:08 -0800210 struct vm_area_struct *vma)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700211{
Alex Bird8a3ede32011-11-07 12:33:42 -0800212 struct ion_carveout_heap *carveout_heap =
213 container_of(heap, struct ion_carveout_heap, heap);
Olav Haugancb9ad9e2012-01-09 11:15:14 -0800214 int ret_value = 0;
Alex Bird8a3ede32011-11-07 12:33:42 -0800215
Olav Haugancb9ad9e2012-01-09 11:15:14 -0800216 if (ion_carveout_request_region(carveout_heap))
217 return -EINVAL;
Alex Bird8a3ede32011-11-07 12:33:42 -0800218
Laura Abbottb14ed962012-01-30 14:18:08 -0800219 if (!ION_IS_CACHED(buffer->flags))
Olav Haugande074a72012-02-22 15:39:54 -0800220 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
221
222 ret_value = remap_pfn_range(vma, vma->vm_start,
223 __phys_to_pfn(buffer->priv_phys) + vma->vm_pgoff,
224 vma->vm_end - vma->vm_start,
225 vma->vm_page_prot);
Olav Haugancb9ad9e2012-01-09 11:15:14 -0800226
227 if (ret_value)
228 ion_carveout_release_region(carveout_heap);
229 return ret_value;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700230}
231
Alex Bird8a3ede32011-11-07 12:33:42 -0800232void ion_carveout_heap_unmap_user(struct ion_heap *heap,
233 struct ion_buffer *buffer)
234{
235 struct ion_carveout_heap *carveout_heap =
236 container_of(heap, struct ion_carveout_heap, heap);
Olav Haugancb9ad9e2012-01-09 11:15:14 -0800237 ion_carveout_release_region(carveout_heap);
Alex Bird8a3ede32011-11-07 12:33:42 -0800238}
239
Laura Abbottabcb6f72011-10-04 16:26:49 -0700240int ion_carveout_cache_ops(struct ion_heap *heap, struct ion_buffer *buffer,
241 void *vaddr, unsigned int offset, unsigned int length,
242 unsigned int cmd)
243{
Olav Haugan85c95402012-05-30 17:32:37 -0700244 void (*outer_cache_op)(phys_addr_t, phys_addr_t);
245 struct ion_carveout_heap *carveout_heap =
246 container_of(heap, struct ion_carveout_heap, heap);
Laura Abbottabcb6f72011-10-04 16:26:49 -0700247
248 switch (cmd) {
249 case ION_IOC_CLEAN_CACHES:
Olav Haugan85c95402012-05-30 17:32:37 -0700250 dmac_clean_range(vaddr, vaddr + length);
251 outer_cache_op = outer_clean_range;
Laura Abbottabcb6f72011-10-04 16:26:49 -0700252 break;
253 case ION_IOC_INV_CACHES:
Olav Haugan85c95402012-05-30 17:32:37 -0700254 dmac_inv_range(vaddr, vaddr + length);
255 outer_cache_op = outer_inv_range;
Laura Abbottabcb6f72011-10-04 16:26:49 -0700256 break;
257 case ION_IOC_CLEAN_INV_CACHES:
Olav Haugan85c95402012-05-30 17:32:37 -0700258 dmac_flush_range(vaddr, vaddr + length);
259 outer_cache_op = outer_flush_range;
Laura Abbottabcb6f72011-10-04 16:26:49 -0700260 break;
261 default:
262 return -EINVAL;
263 }
264
Olav Haugan85c95402012-05-30 17:32:37 -0700265 if (carveout_heap->has_outer_cache) {
266 unsigned long pstart = buffer->priv_phys + offset;
267 outer_cache_op(pstart, pstart + length);
268 }
Laura Abbottabcb6f72011-10-04 16:26:49 -0700269 return 0;
270}
271
Olav Haugan0671b9a2012-05-25 11:58:56 -0700272static int ion_carveout_print_debug(struct ion_heap *heap, struct seq_file *s,
273 const struct rb_root *mem_map)
Laura Abbott68c80642011-10-21 17:32:27 -0700274{
275 struct ion_carveout_heap *carveout_heap =
276 container_of(heap, struct ion_carveout_heap, heap);
277
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800278 seq_printf(s, "total bytes currently allocated: %lx\n",
279 carveout_heap->allocated_bytes);
280 seq_printf(s, "total heap size: %lx\n", carveout_heap->total_size);
Laura Abbott68c80642011-10-21 17:32:27 -0700281
Olav Haugan0671b9a2012-05-25 11:58:56 -0700282 if (mem_map) {
283 unsigned long base = carveout_heap->base;
284 unsigned long size = carveout_heap->total_size;
285 unsigned long end = base+size;
286 unsigned long last_end = base;
287 struct rb_node *n;
288
289 seq_printf(s, "\nMemory Map\n");
290 seq_printf(s, "%16.s %14.s %14.s %14.s\n",
291 "client", "start address", "end address",
292 "size (hex)");
293
294 for (n = rb_first(mem_map); n; n = rb_next(n)) {
295 struct mem_map_data *data =
296 rb_entry(n, struct mem_map_data, node);
297 const char *client_name = "(null)";
298
299 if (last_end < data->addr) {
Laura Abbott1135c9e2013-03-13 15:33:40 -0700300 phys_addr_t da;
301
302 da = data->addr-1;
303 seq_printf(s, "%16.s %14pa %14pa %14lu (%lx)\n",
304 "FREE", &last_end, &da,
Olav Haugan0671b9a2012-05-25 11:58:56 -0700305 data->addr-last_end,
306 data->addr-last_end);
307 }
308
309 if (data->client_name)
310 client_name = data->client_name;
311
Laura Abbott1135c9e2013-03-13 15:33:40 -0700312 seq_printf(s, "%16.s %14pa %14pa %14lu (%lx)\n",
313 client_name, &data->addr,
314 &data->addr_end,
Olav Haugan0671b9a2012-05-25 11:58:56 -0700315 data->size, data->size);
316 last_end = data->addr_end+1;
317 }
318 if (last_end < end) {
319 seq_printf(s, "%16.s %14lx %14lx %14lu (%lx)\n", "FREE",
320 last_end, end-1, end-last_end, end-last_end);
321 }
322 }
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800323 return 0;
Laura Abbott68c80642011-10-21 17:32:27 -0700324}
325
Laura Abbott8c017362011-09-22 20:59:12 -0700326int ion_carveout_heap_map_iommu(struct ion_buffer *buffer,
327 struct ion_iommu_map *data,
328 unsigned int domain_num,
329 unsigned int partition_num,
330 unsigned long align,
331 unsigned long iova_length,
332 unsigned long flags)
333{
Laura Abbott8c017362011-09-22 20:59:12 -0700334 struct iommu_domain *domain;
Olav Haugan16cdb412012-03-27 13:02:17 -0700335 int ret = 0;
Laura Abbott8c017362011-09-22 20:59:12 -0700336 unsigned long extra;
Olav Haugan16cdb412012-03-27 13:02:17 -0700337 struct scatterlist *sglist = 0;
Olav Hauganf310cf22012-05-08 08:42:49 -0700338 int prot = IOMMU_WRITE | IOMMU_READ;
339 prot |= ION_IS_CACHED(flags) ? IOMMU_CACHE : 0;
Laura Abbott8c017362011-09-22 20:59:12 -0700340
341 data->mapped_size = iova_length;
342
343 if (!msm_use_iommu()) {
344 data->iova_addr = buffer->priv_phys;
345 return 0;
346 }
347
348 extra = iova_length - buffer->size;
349
Laura Abbottd01221b2012-05-16 17:52:49 -0700350 ret = msm_allocate_iova_address(domain_num, partition_num,
351 data->mapped_size, align,
352 &data->iova_addr);
Laura Abbott8c017362011-09-22 20:59:12 -0700353
Laura Abbottd01221b2012-05-16 17:52:49 -0700354 if (ret)
Laura Abbott8c017362011-09-22 20:59:12 -0700355 goto out;
Laura Abbott8c017362011-09-22 20:59:12 -0700356
357 domain = msm_get_iommu_domain(domain_num);
358
359 if (!domain) {
360 ret = -ENOMEM;
361 goto out1;
362 }
363
Olav Haugan16cdb412012-03-27 13:02:17 -0700364 sglist = vmalloc(sizeof(*sglist));
365 if (!sglist)
366 goto out1;
Laura Abbott8c017362011-09-22 20:59:12 -0700367
Olav Haugan16cdb412012-03-27 13:02:17 -0700368 sg_init_table(sglist, 1);
369 sglist->length = buffer->size;
370 sglist->offset = 0;
371 sglist->dma_address = buffer->priv_phys;
372
373 ret = iommu_map_range(domain, data->iova_addr, sglist,
374 buffer->size, prot);
375 if (ret) {
376 pr_err("%s: could not map %lx in domain %p\n",
377 __func__, data->iova_addr, domain);
378 goto out1;
Laura Abbott8c017362011-09-22 20:59:12 -0700379 }
380
Olav Haugan16cdb412012-03-27 13:02:17 -0700381 if (extra) {
382 unsigned long extra_iova_addr = data->iova_addr + buffer->size;
Mitchel Humpherysaf3b5222013-01-15 15:38:52 -0800383 unsigned long phys_addr = sg_phys(sglist);
384 ret = msm_iommu_map_extra(domain, extra_iova_addr, phys_addr,
385 extra, SZ_4K, prot);
Olav Haugan16cdb412012-03-27 13:02:17 -0700386 if (ret)
387 goto out2;
388 }
389 vfree(sglist);
390 return ret;
Laura Abbott8c017362011-09-22 20:59:12 -0700391
392out2:
Olav Haugan16cdb412012-03-27 13:02:17 -0700393 iommu_unmap_range(domain, data->iova_addr, buffer->size);
Laura Abbott8c017362011-09-22 20:59:12 -0700394out1:
Olav Haugan16cdb412012-03-27 13:02:17 -0700395 vfree(sglist);
Laura Abbott8c017362011-09-22 20:59:12 -0700396 msm_free_iova_address(data->iova_addr, domain_num, partition_num,
397 data->mapped_size);
398
399out:
400
401 return ret;
402}
403
404void ion_carveout_heap_unmap_iommu(struct ion_iommu_map *data)
405{
Laura Abbott8c017362011-09-22 20:59:12 -0700406 unsigned int domain_num;
407 unsigned int partition_num;
408 struct iommu_domain *domain;
409
410 if (!msm_use_iommu())
411 return;
412
413 domain_num = iommu_map_domain(data);
414 partition_num = iommu_map_partition(data);
415
416 domain = msm_get_iommu_domain(domain_num);
417
418 if (!domain) {
419 WARN(1, "Could not get domain %d. Corruption?\n", domain_num);
420 return;
421 }
422
Olav Haugan16cdb412012-03-27 13:02:17 -0700423 iommu_unmap_range(domain, data->iova_addr, data->mapped_size);
Laura Abbott8c017362011-09-22 20:59:12 -0700424 msm_free_iova_address(data->iova_addr, domain_num, partition_num,
425 data->mapped_size);
426
427 return;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700428}
429
430static struct ion_heap_ops carveout_heap_ops = {
431 .allocate = ion_carveout_heap_allocate,
432 .free = ion_carveout_heap_free,
433 .phys = ion_carveout_heap_phys,
434 .map_user = ion_carveout_heap_map_user,
435 .map_kernel = ion_carveout_heap_map_kernel,
Alex Bird8a3ede32011-11-07 12:33:42 -0800436 .unmap_user = ion_carveout_heap_unmap_user,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700437 .unmap_kernel = ion_carveout_heap_unmap_kernel,
Jordan Crouse0a1195e2011-10-20 14:03:31 -0600438 .map_dma = ion_carveout_heap_map_dma,
439 .unmap_dma = ion_carveout_heap_unmap_dma,
Laura Abbottabcb6f72011-10-04 16:26:49 -0700440 .cache_op = ion_carveout_cache_ops,
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800441 .print_debug = ion_carveout_print_debug,
Laura Abbott8c017362011-09-22 20:59:12 -0700442 .map_iommu = ion_carveout_heap_map_iommu,
443 .unmap_iommu = ion_carveout_heap_unmap_iommu,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700444};
445
446struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *heap_data)
447{
448 struct ion_carveout_heap *carveout_heap;
Laura Abbottb1b7b432011-08-03 13:25:08 -0700449 int ret;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700450
451 carveout_heap = kzalloc(sizeof(struct ion_carveout_heap), GFP_KERNEL);
452 if (!carveout_heap)
453 return ERR_PTR(-ENOMEM);
454
455 carveout_heap->pool = gen_pool_create(12, -1);
456 if (!carveout_heap->pool) {
457 kfree(carveout_heap);
458 return ERR_PTR(-ENOMEM);
459 }
460 carveout_heap->base = heap_data->base;
Laura Abbottb1b7b432011-08-03 13:25:08 -0700461 ret = gen_pool_add(carveout_heap->pool, carveout_heap->base,
462 heap_data->size, -1);
463 if (ret < 0) {
Olav Haugan7fba5cf2012-01-06 10:05:31 -0800464 gen_pool_destroy(carveout_heap->pool);
Laura Abbottb1b7b432011-08-03 13:25:08 -0700465 kfree(carveout_heap);
466 return ERR_PTR(-EINVAL);
467 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700468 carveout_heap->heap.ops = &carveout_heap_ops;
469 carveout_heap->heap.type = ION_HEAP_TYPE_CARVEOUT;
Laura Abbott68c80642011-10-21 17:32:27 -0700470 carveout_heap->allocated_bytes = 0;
471 carveout_heap->total_size = heap_data->size;
Olav Haugan85c95402012-05-30 17:32:37 -0700472 carveout_heap->has_outer_cache = heap_data->has_outer_cache;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700473
Olav Haugan0703dbf2011-12-19 17:53:38 -0800474 if (heap_data->extra_data) {
475 struct ion_co_heap_pdata *extra_data =
476 heap_data->extra_data;
477
478 if (extra_data->setup_region)
479 carveout_heap->bus_id = extra_data->setup_region();
480 if (extra_data->request_region)
481 carveout_heap->request_region =
482 extra_data->request_region;
483 if (extra_data->release_region)
484 carveout_heap->release_region =
485 extra_data->release_region;
486 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700487 return &carveout_heap->heap;
488}
489
490void ion_carveout_heap_destroy(struct ion_heap *heap)
491{
492 struct ion_carveout_heap *carveout_heap =
493 container_of(heap, struct ion_carveout_heap, heap);
494
495 gen_pool_destroy(carveout_heap->pool);
496 kfree(carveout_heap);
497 carveout_heap = NULL;
498}