2 * drivers/gpu/ion/ion_carveout_heap.c
4 * Copyright (C) 2011 Google, Inc.
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.
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.
16 #include <linux/spinlock.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/err.h>
19 #include <linux/genalloc.h>
22 #include <linux/scatterlist.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
28 struct ion_carveout_heap {
30 struct gen_pool *pool;
34 ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap,
38 struct ion_carveout_heap *carveout_heap =
39 container_of(heap, struct ion_carveout_heap, heap);
40 unsigned long offset = gen_pool_alloc(carveout_heap->pool, size);
43 return ION_CARVEOUT_ALLOCATE_FAIL;
48 void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
51 struct ion_carveout_heap *carveout_heap =
52 container_of(heap, struct ion_carveout_heap, heap);
54 if (addr == ION_CARVEOUT_ALLOCATE_FAIL)
56 gen_pool_free(carveout_heap->pool, addr, size);
59 static int ion_carveout_heap_phys(struct ion_heap *heap,
60 struct ion_buffer *buffer,
61 ion_phys_addr_t *addr, size_t *len)
63 struct sg_table *table = buffer->priv_virt;
64 struct page *page = sg_page(table->sgl);
65 ion_phys_addr_t paddr = PFN_PHYS(page_to_pfn(page));
72 static int ion_carveout_heap_allocate(struct ion_heap *heap,
73 struct ion_buffer *buffer,
74 unsigned long size, unsigned long align,
77 struct sg_table *table;
78 ion_phys_addr_t paddr;
81 if (align > PAGE_SIZE)
84 table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
87 ret = sg_alloc_table(table, 1, GFP_KERNEL);
91 paddr = ion_carveout_allocate(heap, size, align);
92 if (paddr == ION_CARVEOUT_ALLOCATE_FAIL) {
97 sg_set_page(table->sgl, pfn_to_page(PFN_DOWN(paddr)), size, 0);
98 buffer->priv_virt = table;
103 sg_free_table(table);
109 static void ion_carveout_heap_free(struct ion_buffer *buffer)
111 struct ion_heap *heap = buffer->heap;
112 struct sg_table *table = buffer->priv_virt;
113 struct page *page = sg_page(table->sgl);
114 ion_phys_addr_t paddr = PFN_PHYS(page_to_pfn(page));
116 ion_heap_buffer_zero(buffer);
118 if (ion_buffer_cached(buffer))
119 dma_sync_sg_for_device(NULL, table->sgl, table->nents,
122 ion_carveout_free(heap, paddr, buffer->size);
123 sg_free_table(table);
127 static struct sg_table *ion_carveout_heap_map_dma(struct ion_heap *heap,
128 struct ion_buffer *buffer)
130 return buffer->priv_virt;
133 static void ion_carveout_heap_unmap_dma(struct ion_heap *heap,
134 struct ion_buffer *buffer)
139 static struct ion_heap_ops carveout_heap_ops = {
140 .allocate = ion_carveout_heap_allocate,
141 .free = ion_carveout_heap_free,
142 .phys = ion_carveout_heap_phys,
143 .map_dma = ion_carveout_heap_map_dma,
144 .unmap_dma = ion_carveout_heap_unmap_dma,
145 .map_user = ion_heap_map_user,
146 .map_kernel = ion_heap_map_kernel,
147 .unmap_kernel = ion_heap_unmap_kernel,
150 struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *heap_data)
152 struct ion_carveout_heap *carveout_heap;
158 page = pfn_to_page(PFN_DOWN(heap_data->base));
159 size = heap_data->size;
161 printk("%s: %x@%lx\n", __func__, size, heap_data->base);
163 ion_pages_sync_for_device(NULL, page, size, DMA_BIDIRECTIONAL);
165 ret = ion_heap_pages_zero(page, size, pgprot_writecombine(PAGE_KERNEL));
169 carveout_heap = kzalloc(sizeof(struct ion_carveout_heap), GFP_KERNEL);
171 return ERR_PTR(-ENOMEM);
173 carveout_heap->pool = gen_pool_create(12, -1);
174 if (!carveout_heap->pool) {
175 kfree(carveout_heap);
176 return ERR_PTR(-ENOMEM);
178 carveout_heap->base = heap_data->base;
179 gen_pool_add(carveout_heap->pool, carveout_heap->base, heap_data->size,
181 carveout_heap->heap.ops = &carveout_heap_ops;
182 carveout_heap->heap.type = ION_HEAP_TYPE_CARVEOUT;
183 carveout_heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE;
185 return &carveout_heap->heap;
188 void ion_carveout_heap_destroy(struct ion_heap *heap)
190 struct ion_carveout_heap *carveout_heap =
191 container_of(heap, struct ion_carveout_heap, heap);
193 gen_pool_destroy(carveout_heap->pool);
194 kfree(carveout_heap);
195 carveout_heap = NULL;