/* * drivers/gpu/ion/ion_carveout_heap.c * * Copyright (C) 2011 Google, Inc. * * 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 #include #include #include #include #include #include #include #include #include "ion_priv.h" #ifdef CONFIG_ARCH_SCX35L64 #include #else #include #endif struct ion_carveout_heap { struct ion_heap heap; struct gen_pool *pool; ion_phys_addr_t base; }; ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size, unsigned long align) { struct ion_carveout_heap *carveout_heap = container_of(heap, struct ion_carveout_heap, heap); unsigned long offset = gen_pool_alloc(carveout_heap->pool, size); if (!offset) return ION_CARVEOUT_ALLOCATE_FAIL; return offset; } void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr, unsigned long size) { struct ion_carveout_heap *carveout_heap = container_of(heap, struct ion_carveout_heap, heap); if (addr == ION_CARVEOUT_ALLOCATE_FAIL) return; gen_pool_free(carveout_heap->pool, addr, size); } static int ion_carveout_heap_phys(struct ion_heap *heap, struct ion_buffer *buffer, ion_phys_addr_t *addr, size_t *len) { *addr = buffer->priv_phys; *len = buffer->size; return 0; } static int ion_carveout_heap_allocate(struct ion_heap *heap, struct ion_buffer *buffer, unsigned long size, unsigned long align, unsigned long flags) { buffer->priv_phys = ion_carveout_allocate(heap, size, align); return buffer->priv_phys == ION_CARVEOUT_ALLOCATE_FAIL ? -ENOMEM : 0; } static void ion_carveout_heap_free(struct ion_buffer *buffer) { struct ion_heap *heap = buffer->heap; ion_carveout_free(heap, buffer->priv_phys, buffer->size); buffer->priv_phys = ION_CARVEOUT_ALLOCATE_FAIL; } struct sg_table *ion_carveout_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, phys_to_page(buffer->priv_phys), buffer->size, 0); return table; } void ion_carveout_heap_unmap_dma(struct ion_heap *heap, struct ion_buffer *buffer) { sg_free_table(buffer->sg_table); kfree(buffer->sg_table); buffer->sg_table=NULL; } void *ion_carveout_heap_map_kernel(struct ion_heap *heap, struct ion_buffer *buffer) { void *ret; #ifndef CONFIG_ARCH_SCX35L64 pgprot_t mtype = MT_MEMORY_NONCACHED; #else pgprot_t mtype = 11; #endif if (buffer->flags & ION_FLAG_CACHED) #ifndef CONFIG_ARCH_SCX35L64 mtype = MT_MEMORY; #else mtype = 9; #endif #ifndef CONFIG_ARCH_SCX35L64 ret = __arm_ioremap(buffer->priv_phys, buffer->size, mtype); #else ret = __ioremap(buffer->priv_phys, buffer->size, mtype); #endif if (ret == NULL) return ERR_PTR(-ENOMEM); return ret; } void ion_carveout_heap_unmap_kernel(struct ion_heap *heap, struct ion_buffer *buffer) { #ifndef CONFIG_ARCH_SCX35L64 __arm_iounmap(buffer->vaddr); #else __iounmap(buffer->vaddr); #endif buffer->vaddr = NULL; return; } int ion_carveout_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer, struct vm_area_struct *vma) { return remap_pfn_range(vma, vma->vm_start, __phys_to_pfn(buffer->priv_phys) + vma->vm_pgoff, vma->vm_end - vma->vm_start, pgprot_noncached(vma->vm_page_prot)); } #if defined(CONFIG_SPRD_IOMMU) int ion_carveout_heap_map_iommu(struct ion_buffer *buffer, int domain_num, unsigned long *ptr_iova) { int ret=0; if(0==buffer->iomap_cnt[domain_num]) { buffer->iova[domain_num]=sprd_iova_alloc(domain_num,buffer->size); ret = sprd_iova_map(domain_num,buffer->iova[domain_num],buffer); } *ptr_iova=buffer->iova[domain_num]; buffer->iomap_cnt[domain_num]++; return ret; } int ion_carveout_heap_unmap_iommu(struct ion_buffer *buffer, int domain_num) { int ret=0; buffer->iomap_cnt[domain_num]--; if(0==buffer->iomap_cnt[domain_num]) { ret=sprd_iova_unmap(domain_num,buffer->iova[domain_num],buffer); sprd_iova_free(domain_num,buffer->iova[domain_num],buffer->size); buffer->iova[domain_num]=0; } return ret; } #endif static struct ion_heap_ops carveout_heap_ops = { .allocate = ion_carveout_heap_allocate, .free = ion_carveout_heap_free, .phys = ion_carveout_heap_phys, .map_dma = ion_carveout_heap_map_dma, .unmap_dma = ion_carveout_heap_unmap_dma, .map_user = ion_carveout_heap_map_user, .map_kernel = ion_carveout_heap_map_kernel, .unmap_kernel = ion_carveout_heap_unmap_kernel, #if defined(CONFIG_SPRD_IOMMU) .map_iommu = ion_carveout_heap_map_iommu, .unmap_iommu = ion_carveout_heap_unmap_iommu, #endif }; struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *heap_data) { struct ion_carveout_heap *carveout_heap; carveout_heap = kzalloc(sizeof(struct ion_carveout_heap), GFP_KERNEL); if (!carveout_heap) return ERR_PTR(-ENOMEM); carveout_heap->pool = gen_pool_create(12, -1); if (!carveout_heap->pool) { kfree(carveout_heap); return ERR_PTR(-ENOMEM); } carveout_heap->base = heap_data->base; gen_pool_add(carveout_heap->pool, carveout_heap->base, heap_data->size, -1); carveout_heap->heap.ops = &carveout_heap_ops; carveout_heap->heap.type = ION_HEAP_TYPE_CARVEOUT; return &carveout_heap->heap; } void ion_carveout_heap_destroy(struct ion_heap *heap) { struct ion_carveout_heap *carveout_heap = container_of(heap, struct ion_carveout_heap, heap); gen_pool_destroy(carveout_heap->pool); kfree(carveout_heap); carveout_heap = NULL; }