| /* |
| * drivers/gpu/ion/ion_system_heap.c |
| * |
| * Copyright (C) 2011 Google, Inc. |
| * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
| * |
| * This software is licensed under the terms of the GNU General Public |
| * License version 2, as published by the Free Software Foundation, and |
| * may be copied, distributed, and modified under those terms. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * |
| */ |
| |
| #include <asm/page.h> |
| #include <linux/dma-mapping.h> |
| #include <linux/err.h> |
| #include <linux/highmem.h> |
| #include <linux/ion.h> |
| #include <linux/mm.h> |
| #include <linux/scatterlist.h> |
| #include <linux/slab.h> |
| #include <linux/vmalloc.h> |
| #include <linux/seq_file.h> |
| #include "ion_priv.h" |
| #include <mach/memory.h> |
| #include <asm/cacheflush.h> |
| #include <linux/msm_ion.h> |
| #include <linux/dma-mapping.h> |
| |
| static atomic_t system_heap_allocated; |
| static atomic_t system_contig_heap_allocated; |
| |
| struct page_info { |
| struct page *page; |
| unsigned long order; |
| struct list_head list; |
| }; |
| |
| static struct page_info *alloc_largest_available(unsigned long size, |
| bool split_pages) |
| { |
| static unsigned int orders[] = {8, 4, 0}; |
| struct page *page; |
| struct page_info *info; |
| int i; |
| |
| for (i = 0; i < ARRAY_SIZE(orders); i++) { |
| if (size < (1 << orders[i]) * PAGE_SIZE) |
| continue; |
| page = alloc_pages(GFP_HIGHUSER | __GFP_ZERO | |
| __GFP_NOWARN | __GFP_NORETRY, orders[i]); |
| if (!page) |
| continue; |
| if (split_pages) |
| split_page(page, orders[i]); |
| info = kmap(page); |
| info->page = page; |
| info->order = orders[i]; |
| return info; |
| } |
| return NULL; |
| } |
| |
| static int ion_system_heap_allocate(struct ion_heap *heap, |
| struct ion_buffer *buffer, |
| unsigned long size, unsigned long align, |
| unsigned long flags) |
| { |
| struct sg_table *table; |
| struct scatterlist *sg; |
| int ret; |
| struct list_head pages; |
| struct page_info *info, *tmp_info; |
| int i = 0; |
| long size_remaining = PAGE_ALIGN(size); |
| bool split_pages = ion_buffer_fault_user_mappings(buffer); |
| |
| |
| INIT_LIST_HEAD(&pages); |
| while (size_remaining > 0) { |
| info = alloc_largest_available(size_remaining, split_pages); |
| if (!info) |
| goto err; |
| list_add_tail(&info->list, &pages); |
| size_remaining -= (1 << info->order) * PAGE_SIZE; |
| i++; |
| } |
| |
| table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
| if (!table) |
| goto err; |
| |
| if (split_pages) |
| ret = sg_alloc_table(table, PAGE_ALIGN(size) / PAGE_SIZE, |
| GFP_KERNEL); |
| else |
| ret = sg_alloc_table(table, i, GFP_KERNEL); |
| |
| if (ret) |
| goto err1; |
| |
| sg = table->sgl; |
| list_for_each_entry_safe(info, tmp_info, &pages, list) { |
| struct page *page = info->page; |
| |
| if (split_pages) { |
| for (i = 0; i < (1 << info->order); i++) { |
| sg_set_page(sg, page + i, PAGE_SIZE, 0); |
| sg = sg_next(sg); |
| } |
| } else { |
| sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE, |
| 0); |
| sg = sg_next(sg); |
| } |
| list_del(&info->list); |
| kunmap(page); |
| } |
| |
| dma_sync_sg_for_device(NULL, table->sgl, table->nents, |
| DMA_BIDIRECTIONAL); |
| |
| buffer->priv_virt = table; |
| atomic_add(size, &system_heap_allocated); |
| return 0; |
| err1: |
| kfree(table); |
| err: |
| list_for_each_entry(info, &pages, list) { |
| if (split_pages) |
| for (i = 0; i < (1 << info->order); i++) |
| __free_page(info->page + i); |
| else |
| __free_pages(info->page, info->order); |
| |
| kunmap(info->page); |
| } |
| return -ENOMEM; |
| } |
| |
| void ion_system_heap_free(struct ion_buffer *buffer) |
| { |
| int i; |
| struct scatterlist *sg; |
| struct sg_table *table = buffer->priv_virt; |
| |
| for_each_sg(table->sgl, sg, table->nents, i) |
| __free_pages(sg_page(sg), get_order(sg_dma_len(sg))); |
| if (buffer->sg_table) |
| sg_free_table(buffer->sg_table); |
| kfree(buffer->sg_table); |
| atomic_sub(buffer->size, &system_heap_allocated); |
| } |
| |
| struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap, |
| struct ion_buffer *buffer) |
| { |
| return buffer->priv_virt; |
| } |
| |
| void ion_system_heap_unmap_dma(struct ion_heap *heap, |
| struct ion_buffer *buffer) |
| { |
| return; |
| } |
| |
| void *ion_system_heap_map_kernel(struct ion_heap *heap, |
| struct ion_buffer *buffer) |
| { |
| struct scatterlist *sg; |
| int i, j; |
| void *vaddr; |
| pgprot_t pgprot; |
| struct sg_table *table = buffer->priv_virt; |
| int npages = PAGE_ALIGN(buffer->size) / PAGE_SIZE; |
| struct page **pages = kzalloc(sizeof(struct page *) * npages, |
| GFP_KERNEL); |
| struct page **tmp = pages; |
| |
| if (buffer->flags & ION_FLAG_CACHED) |
| pgprot = PAGE_KERNEL; |
| else |
| pgprot = pgprot_writecombine(PAGE_KERNEL); |
| |
| for_each_sg(table->sgl, sg, table->nents, i) { |
| int npages_this_entry = PAGE_ALIGN(sg_dma_len(sg)) / PAGE_SIZE; |
| struct page *page = sg_page(sg); |
| BUG_ON(i >= npages); |
| for (j = 0; j < npages_this_entry; j++) { |
| *(tmp++) = page++; |
| } |
| } |
| vaddr = vmap(pages, npages, VM_MAP, pgprot); |
| kfree(pages); |
| |
| return vaddr; |
| } |
| |
| void ion_system_heap_unmap_kernel(struct ion_heap *heap, |
| struct ion_buffer *buffer) |
| { |
| vunmap(buffer->vaddr); |
| } |
| |
| int ion_system_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer, |
| struct vm_area_struct *vma) |
| { |
| struct sg_table *table = buffer->priv_virt; |
| unsigned long addr = vma->vm_start; |
| unsigned long offset = vma->vm_pgoff; |
| struct scatterlist *sg; |
| int i; |
| |
| if (!ION_IS_CACHED(buffer->flags)) { |
| pr_err("%s: cannot map system heap uncached\n", __func__); |
| return -EINVAL; |
| } |
| |
| for_each_sg(table->sgl, sg, table->nents, i) { |
| if (offset) { |
| offset--; |
| continue; |
| } |
| remap_pfn_range(vma, addr, page_to_pfn(sg_page(sg)), |
| sg_dma_len(sg), vma->vm_page_prot); |
| addr += sg_dma_len(sg); |
| } |
| return 0; |
| } |
| |
| static int ion_system_print_debug(struct ion_heap *heap, struct seq_file *s, |
| const struct rb_root *unused) |
| { |
| seq_printf(s, "total bytes currently allocated: %lx\n", |
| (unsigned long) atomic_read(&system_heap_allocated)); |
| |
| return 0; |
| } |
| |
| static struct ion_heap_ops vmalloc_ops = { |
| .allocate = ion_system_heap_allocate, |
| .free = ion_system_heap_free, |
| .map_dma = ion_system_heap_map_dma, |
| .unmap_dma = ion_system_heap_unmap_dma, |
| .map_kernel = ion_system_heap_map_kernel, |
| .unmap_kernel = ion_system_heap_unmap_kernel, |
| .map_user = ion_system_heap_map_user, |
| .print_debug = ion_system_print_debug, |
| }; |
| |
| struct ion_heap *ion_system_heap_create(struct ion_platform_heap *pheap) |
| { |
| struct ion_heap *heap; |
| |
| heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| if (!heap) |
| return ERR_PTR(-ENOMEM); |
| heap->ops = &vmalloc_ops; |
| heap->type = ION_HEAP_TYPE_SYSTEM; |
| return heap; |
| } |
| |
| void ion_system_heap_destroy(struct ion_heap *heap) |
| { |
| kfree(heap); |
| } |
| |
| static int ion_system_contig_heap_allocate(struct ion_heap *heap, |
| struct ion_buffer *buffer, |
| unsigned long len, |
| unsigned long align, |
| unsigned long flags) |
| { |
| buffer->priv_virt = kzalloc(len, GFP_KERNEL); |
| if (!buffer->priv_virt) |
| return -ENOMEM; |
| atomic_add(len, &system_contig_heap_allocated); |
| return 0; |
| } |
| |
| void ion_system_contig_heap_free(struct ion_buffer *buffer) |
| { |
| kfree(buffer->priv_virt); |
| atomic_sub(buffer->size, &system_contig_heap_allocated); |
| } |
| |
| static int ion_system_contig_heap_phys(struct ion_heap *heap, |
| struct ion_buffer *buffer, |
| ion_phys_addr_t *addr, size_t *len) |
| { |
| *addr = virt_to_phys(buffer->priv_virt); |
| *len = buffer->size; |
| return 0; |
| } |
| |
| struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap, |
| struct ion_buffer *buffer) |
| { |
| struct sg_table *table; |
| int ret; |
| |
| table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| if (!table) |
| return ERR_PTR(-ENOMEM); |
| ret = sg_alloc_table(table, 1, GFP_KERNEL); |
| if (ret) { |
| kfree(table); |
| return ERR_PTR(ret); |
| } |
| sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size, |
| 0); |
| return table; |
| } |
| |
| void ion_system_contig_heap_unmap_dma(struct ion_heap *heap, |
| struct ion_buffer *buffer) |
| { |
| sg_free_table(buffer->sg_table); |
| kfree(buffer->sg_table); |
| } |
| |
| int ion_system_contig_heap_map_user(struct ion_heap *heap, |
| struct ion_buffer *buffer, |
| struct vm_area_struct *vma) |
| { |
| unsigned long pfn = __phys_to_pfn(virt_to_phys(buffer->priv_virt)); |
| |
| if (ION_IS_CACHED(buffer->flags)) |
| return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff, |
| vma->vm_end - vma->vm_start, |
| vma->vm_page_prot); |
| else { |
| pr_err("%s: cannot map system heap uncached\n", __func__); |
| return -EINVAL; |
| } |
| } |
| |
| static int ion_system_contig_print_debug(struct ion_heap *heap, |
| struct seq_file *s, |
| const struct rb_root *unused) |
| { |
| seq_printf(s, "total bytes currently allocated: %lx\n", |
| (unsigned long) atomic_read(&system_contig_heap_allocated)); |
| |
| return 0; |
| } |
| |
| void *ion_system_contig_heap_map_kernel(struct ion_heap *heap, |
| struct ion_buffer *buffer) |
| { |
| return buffer->priv_virt; |
| } |
| |
| void ion_system_contig_heap_unmap_kernel(struct ion_heap *heap, |
| struct ion_buffer *buffer) |
| { |
| return; |
| } |
| |
| static struct ion_heap_ops kmalloc_ops = { |
| .allocate = ion_system_contig_heap_allocate, |
| .free = ion_system_contig_heap_free, |
| .phys = ion_system_contig_heap_phys, |
| .map_dma = ion_system_contig_heap_map_dma, |
| .unmap_dma = ion_system_contig_heap_unmap_dma, |
| .map_kernel = ion_system_contig_heap_map_kernel, |
| .unmap_kernel = ion_system_contig_heap_unmap_kernel, |
| .map_user = ion_system_contig_heap_map_user, |
| .print_debug = ion_system_contig_print_debug, |
| }; |
| |
| struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *pheap) |
| { |
| struct ion_heap *heap; |
| |
| heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| if (!heap) |
| return ERR_PTR(-ENOMEM); |
| heap->ops = &kmalloc_ops; |
| heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG; |
| return heap; |
| } |
| |
| void ion_system_contig_heap_destroy(struct ion_heap *heap) |
| { |
| kfree(heap); |
| } |
| |