blob: ffb3aca689624ecafb6f9399d25505aa01de9cfb [file] [log] [blame]
/*
* drivers/staging/android/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 <linux/spinlock.h>
#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/genalloc.h>
#include <linux/io.h>
#include <linux/mm.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/platform_device.h>
#include "ion.h"
#define ION_CARVEOUT_ALLOCATE_FAIL -1
struct ion_carveout_heap {
struct ion_heap heap;
struct gen_pool *pool;
phys_addr_t base;
};
struct ion_carveout_info {
int heap_num;
struct ion_platform_heap *heaps_data;
struct ion_heap **heaps;
};
static phys_addr_t ion_carveout_allocate(struct ion_heap *heap,
unsigned long size)
{
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;
}
static void ion_carveout_free(struct ion_heap *heap, 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_allocate(struct ion_heap *heap,
struct ion_buffer *buffer,
unsigned long size,
unsigned long flags)
{
struct sg_table *table;
phys_addr_t paddr;
int ret;
table = kmalloc(sizeof(*table), GFP_KERNEL);
if (!table)
return -ENOMEM;
ret = sg_alloc_table(table, 1, GFP_KERNEL);
if (ret)
goto err_free;
paddr = ion_carveout_allocate(heap, size);
if (paddr == ION_CARVEOUT_ALLOCATE_FAIL) {
ret = -ENOMEM;
goto err_free_table;
}
sg_set_page(table->sgl, pfn_to_page(PFN_DOWN(paddr)), size, 0);
buffer->sg_table = table;
return 0;
err_free_table:
sg_free_table(table);
err_free:
kfree(table);
return ret;
}
static void ion_carveout_heap_free(struct ion_buffer *buffer)
{
struct ion_heap *heap = buffer->heap;
struct sg_table *table = buffer->sg_table;
struct page *page = sg_page(table->sgl);
phys_addr_t paddr = PFN_PHYS(page_to_pfn(page));
ion_heap_buffer_zero(buffer);
ion_carveout_free(heap, paddr, buffer->size);
sg_free_table(table);
kfree(table);
}
static struct ion_heap_ops carveout_heap_ops = {
.allocate = ion_carveout_heap_allocate,
.free = ion_carveout_heap_free,
.map_user = ion_heap_map_user,
.map_kernel = ion_heap_map_kernel,
.unmap_kernel = ion_heap_unmap_kernel,
};
struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *heap_data)
{
struct ion_carveout_heap *carveout_heap;
int ret;
struct page *page;
size_t size;
page = pfn_to_page(PFN_DOWN(heap_data->base));
size = heap_data->size;
ret = ion_heap_pages_zero(page, size, pgprot_writecombine(PAGE_KERNEL));
if (ret)
return ERR_PTR(ret);
carveout_heap = kzalloc(sizeof(*carveout_heap), GFP_KERNEL);
if (!carveout_heap)
return ERR_PTR(-ENOMEM);
carveout_heap->pool = gen_pool_create(PAGE_SHIFT, -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;
carveout_heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE;
carveout_heap->heap.name = "ion_carveout_heap";
return &carveout_heap->heap;
}
static 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);
}
static void *ion_carveout_malloc_info(struct device *dev, int heap_num)
{
int size = 0;
unsigned char *p = NULL;
struct ion_carveout_info *info = NULL;
int info_len = sizeof(*info);
int heaps_data_len = sizeof(struct ion_platform_heap);
int heaps_len = sizeof(struct ion_heap *);
size = info_len + heaps_data_len * heap_num
+ heaps_len * heap_num;
p = devm_kzalloc(dev, size, GFP_KERNEL);
if (!p) {
pr_err("ion_carveout_malloc_info fail\n");
return p;
}
info = (struct ion_carveout_info *)(p);
info->heap_num = heap_num;
info->heaps_data = (struct ion_platform_heap *)(p + info_len);
info->heaps = (struct ion_heap **)(p + info_len +
heaps_data_len * heap_num);
return (void *)(p);
}
static int ion_carveout_get_info(struct device *dev,
struct ion_carveout_info **info)
{
int i, res = -ENODEV;
int heap_num = 0;
struct device_node *np;
struct resource r;
struct ion_carveout_info *tmp_info;
np = dev->of_node;
if (!np)
goto err_node;
res = of_property_read_u32(np, "pool-num", &heap_num);
if (res)
goto err_node;
tmp_info = (struct ion_carveout_info *)ion_carveout_malloc_info(dev, heap_num);
if (!tmp_info) {
res = -ENOMEM;
goto err_node;
}
for (i = 0; i < heap_num; i++) {
res = of_address_to_resource(np, i, &r);
if (res)
goto err_node;
(tmp_info->heaps_data + i)->id = i;
(tmp_info->heaps_data + i)->base = r.start;
(tmp_info->heaps_data + i)->size = resource_size(&r);
(tmp_info->heaps_data + i)->name = r.name;
}
*info = tmp_info;
return 0;
err_node:
pr_err("ion_carveout_get_info failed (%d)\n", res);
return res;
}
static int ion_carveout_probe(struct platform_device *pdev)
{
int res = 0;
int i = 0;
struct ion_carveout_info *info;
res = ion_carveout_get_info(&pdev->dev, &info);
if (res != 0)
return res;
for (i = 0; i < info->heap_num; i++) {
struct ion_platform_heap *heap_data = (info->heaps_data + i);
info->heaps[i] = ion_carveout_heap_create(heap_data);
if (IS_ERR_OR_NULL(info->heaps[i])) {
res = PTR_ERR(info->heaps[i]);
info->heaps[i] = NULL;
goto err_create_heap;
}
ion_device_add_heap(info->heaps[i]);
}
platform_set_drvdata(pdev, info);
dev_info(&pdev->dev, "ion_carveout_probe %d heaps done\n", info->heap_num);
return 0;
err_create_heap:
dev_info(&pdev->dev, "probe failed with %d\n", res);
for (i = 0; i < info->heap_num; i++) {
if (info->heaps[i])
ion_carveout_heap_destroy(info->heaps[i]);
}
return res;
}
static int ion_carveout_remove(struct platform_device *pdev)
{
int i = 0;
struct ion_carveout_info *info;
info = (struct ion_carveout_info *)dev_get_drvdata(&pdev->dev);
for (i = 0; i < info->heap_num; i++) {
if (info->heaps[i])
ion_carveout_heap_destroy(info->heaps[i]);
}
return 0;
}
static const struct of_device_id ion_carveout_heaps_of_match[] = {
{ .compatible = "syna,ion-carveout-heaps", },
{},
};
static struct platform_driver ion_carveout_driver = {
.probe = ion_carveout_probe,
.remove = ion_carveout_remove,
.driver = {
.name = "ion-carveout",
.of_match_table = ion_carveout_heaps_of_match,
},
};
static int __init ion_carveout_init(void)
{
return platform_driver_register(&ion_carveout_driver);
}
device_initcall(ion_carveout_init);