2 * drivers/gpu/ion/ion_system_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.
18 #include <linux/dma-mapping.h>
19 #include <linux/err.h>
20 #include <linux/highmem.h>
21 #include <linux/ion.h>
23 #include <linux/scatterlist.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
31 struct list_head list;
34 static struct page_info *alloc_largest_available(unsigned long size)
36 static unsigned int orders[] = {8, 4, 0};
38 struct page_info *info;
41 for (i = 0; i < ARRAY_SIZE(orders); i++) {
42 if (size < (1 << orders[i]) * PAGE_SIZE)
44 page = alloc_pages(GFP_HIGHUSER | __GFP_ZERO |
45 __GFP_NOWARN | __GFP_NORETRY, orders[i]);
48 split_page(page, orders[i]);
51 info->order = orders[i];
57 static int ion_system_heap_allocate(struct ion_heap *heap,
58 struct ion_buffer *buffer,
59 unsigned long size, unsigned long align,
62 struct sg_table *table;
63 struct scatterlist *sg;
65 struct list_head pages;
66 struct page_info *info, *tmp_info;
68 long size_remaining = PAGE_ALIGN(size);
70 INIT_LIST_HEAD(&pages);
71 while (size_remaining > 0) {
72 info = alloc_largest_available(size_remaining);
75 list_add_tail(&info->list, &pages);
76 size_remaining -= (1 << info->order) * PAGE_SIZE;
79 table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
83 ret = sg_alloc_table(table, PAGE_ALIGN(size) / PAGE_SIZE, GFP_KERNEL);
88 list_for_each_entry_safe(info, tmp_info, &pages, list) {
89 struct page *page = info->page;
90 for (i = 0; i < (1 << info->order); i++) {
91 sg_set_page(sg, page + i, PAGE_SIZE, 0);
94 list_del(&info->list);
95 memset(info, 0, sizeof(struct page_info));
99 dma_sync_sg_for_device(NULL, table->sgl, table->nents,
102 buffer->priv_virt = table;
107 list_for_each_entry(info, &pages, list) {
108 for (i = 0; i < (1 << info->order); i++)
109 __free_page(info->page + i);
115 void ion_system_heap_free(struct ion_buffer *buffer)
118 struct scatterlist *sg;
119 struct sg_table *table = buffer->priv_virt;
121 for_each_sg(table->sgl, sg, table->nents, i)
122 __free_pages(sg_page(sg), get_order(sg_dma_len(sg)));
123 if (buffer->sg_table)
124 sg_free_table(buffer->sg_table);
125 kfree(buffer->sg_table);
128 struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap,
129 struct ion_buffer *buffer)
131 return buffer->priv_virt;
134 void ion_system_heap_unmap_dma(struct ion_heap *heap,
135 struct ion_buffer *buffer)
140 void *ion_system_heap_map_kernel(struct ion_heap *heap,
141 struct ion_buffer *buffer)
143 struct scatterlist *sg;
147 struct sg_table *table = buffer->priv_virt;
148 int npages = PAGE_ALIGN(buffer->size) / PAGE_SIZE;
149 struct page **pages = kzalloc(sizeof(struct page *) * npages,
151 struct page **tmp = pages;
153 if (buffer->flags & ION_FLAG_CACHED)
154 pgprot = PAGE_KERNEL;
156 pgprot = pgprot_writecombine(PAGE_KERNEL);
158 for_each_sg(table->sgl, sg, table->nents, i) {
159 int npages_this_entry = PAGE_ALIGN(sg_dma_len(sg)) / PAGE_SIZE;
160 struct page *page = sg_page(sg);
162 for (j = 0; j < npages_this_entry; j++) {
166 vaddr = vmap(pages, npages, VM_MAP, pgprot);
172 void ion_system_heap_unmap_kernel(struct ion_heap *heap,
173 struct ion_buffer *buffer)
175 vunmap(buffer->vaddr);
178 int ion_system_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer,
179 struct vm_area_struct *vma)
181 struct sg_table *table = buffer->priv_virt;
182 unsigned long addr = vma->vm_start;
183 unsigned long offset = vma->vm_pgoff;
184 struct scatterlist *sg;
187 for_each_sg(table->sgl, sg, table->nents, i) {
192 remap_pfn_range(vma, addr, page_to_pfn(sg_page(sg)),
193 sg_dma_len(sg), vma->vm_page_prot);
194 addr += sg_dma_len(sg);
195 if (addr >= vma->vm_end)
201 static struct ion_heap_ops vmalloc_ops = {
202 .allocate = ion_system_heap_allocate,
203 .free = ion_system_heap_free,
204 .map_dma = ion_system_heap_map_dma,
205 .unmap_dma = ion_system_heap_unmap_dma,
206 .map_kernel = ion_system_heap_map_kernel,
207 .unmap_kernel = ion_system_heap_unmap_kernel,
208 .map_user = ion_system_heap_map_user,
211 struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused)
213 struct ion_heap *heap;
215 heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
217 return ERR_PTR(-ENOMEM);
218 heap->ops = &vmalloc_ops;
219 heap->type = ION_HEAP_TYPE_SYSTEM;
223 void ion_system_heap_destroy(struct ion_heap *heap)
228 static int ion_system_contig_heap_allocate(struct ion_heap *heap,
229 struct ion_buffer *buffer,
234 buffer->priv_virt = kzalloc(len, GFP_KERNEL);
235 if (!buffer->priv_virt)
240 void ion_system_contig_heap_free(struct ion_buffer *buffer)
242 kfree(buffer->priv_virt);
245 static int ion_system_contig_heap_phys(struct ion_heap *heap,
246 struct ion_buffer *buffer,
247 ion_phys_addr_t *addr, size_t *len)
249 *addr = virt_to_phys(buffer->priv_virt);
254 struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap,
255 struct ion_buffer *buffer)
257 struct sg_table *table;
260 table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
262 return ERR_PTR(-ENOMEM);
263 ret = sg_alloc_table(table, 1, GFP_KERNEL);
268 sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size,
273 void ion_system_contig_heap_unmap_dma(struct ion_heap *heap,
274 struct ion_buffer *buffer)
276 sg_free_table(buffer->sg_table);
277 kfree(buffer->sg_table);
280 int ion_system_contig_heap_map_user(struct ion_heap *heap,
281 struct ion_buffer *buffer,
282 struct vm_area_struct *vma)
284 unsigned long pfn = __phys_to_pfn(virt_to_phys(buffer->priv_virt));
285 return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
286 vma->vm_end - vma->vm_start,
291 static struct ion_heap_ops kmalloc_ops = {
292 .allocate = ion_system_contig_heap_allocate,
293 .free = ion_system_contig_heap_free,
294 .phys = ion_system_contig_heap_phys,
295 .map_dma = ion_system_contig_heap_map_dma,
296 .unmap_dma = ion_system_contig_heap_unmap_dma,
297 .map_kernel = ion_system_heap_map_kernel,
298 .unmap_kernel = ion_system_heap_unmap_kernel,
299 .map_user = ion_system_contig_heap_map_user,
302 struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *unused)
304 struct ion_heap *heap;
306 heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
308 return ERR_PTR(-ENOMEM);
309 heap->ops = &kmalloc_ops;
310 heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG;
314 void ion_system_contig_heap_destroy(struct ion_heap *heap)