Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/staging/android/ion/ion_cma_heap.c |
| 3 | * |
| 4 | * Copyright (C) Linaro 2012 |
| 5 | * Author: <benjamin.gaignard@linaro.org> for ST-Ericsson. |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/dma-mapping.h> |
| 23 | |
| 24 | #include "ion.h" |
| 25 | #include "ion_priv.h" |
| 26 | |
| 27 | #define ION_CMA_ALLOCATE_FAILED -1 |
| 28 | |
| 29 | struct ion_cma_heap { |
| 30 | struct ion_heap heap; |
| 31 | struct device *dev; |
| 32 | }; |
| 33 | |
| 34 | #define to_cma_heap(x) container_of(x, struct ion_cma_heap, heap) |
| 35 | |
| 36 | struct ion_cma_buffer_info { |
| 37 | void *cpu_addr; |
| 38 | dma_addr_t handle; |
| 39 | struct sg_table *table; |
| 40 | }; |
| 41 | |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 42 | |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 43 | /* ION CMA heap operations functions */ |
| 44 | static int ion_cma_allocate(struct ion_heap *heap, struct ion_buffer *buffer, |
| 45 | unsigned long len, unsigned long align, |
| 46 | unsigned long flags) |
| 47 | { |
| 48 | struct ion_cma_heap *cma_heap = to_cma_heap(heap); |
| 49 | struct device *dev = cma_heap->dev; |
| 50 | struct ion_cma_buffer_info *info; |
| 51 | |
Colin Cross | 661f82f | 2013-12-13 19:26:32 -0800 | [diff] [blame] | 52 | if (buffer->flags & ION_FLAG_CACHED) |
| 53 | return -EINVAL; |
| 54 | |
| 55 | if (align > PAGE_SIZE) |
| 56 | return -EINVAL; |
| 57 | |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 58 | info = kzalloc(sizeof(struct ion_cma_buffer_info), GFP_KERNEL); |
Phong Tran | f0ca3e8 | 2014-07-20 11:10:55 +0700 | [diff] [blame] | 59 | if (!info) |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 60 | return ION_CMA_ALLOCATE_FAILED; |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 61 | |
Colin Cross | 661f82f | 2013-12-13 19:26:32 -0800 | [diff] [blame] | 62 | info->cpu_addr = dma_alloc_coherent(dev, len, &(info->handle), |
| 63 | GFP_HIGHUSER | __GFP_ZERO); |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 64 | |
| 65 | if (!info->cpu_addr) { |
| 66 | dev_err(dev, "Fail to allocate buffer\n"); |
| 67 | goto err; |
| 68 | } |
| 69 | |
| 70 | info->table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
Phong Tran | f0ca3e8 | 2014-07-20 11:10:55 +0700 | [diff] [blame] | 71 | if (!info->table) |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 72 | goto free_mem; |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 73 | |
Jignesh R Patel | 936d61e | 2015-07-28 16:19:36 +0530 | [diff] [blame] | 74 | if (dma_get_sgtable(dev, info->table, info->cpu_addr, info->handle, |
| 75 | len)) |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 76 | goto free_table; |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 77 | /* keep this for memory release */ |
| 78 | buffer->priv_virt = info; |
Laura Abbott | f82ad60 | 2016-08-08 09:52:56 -0700 | [diff] [blame] | 79 | buffer->sg_table = info->table; |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 80 | return 0; |
| 81 | |
| 82 | free_table: |
| 83 | kfree(info->table); |
| 84 | free_mem: |
| 85 | dma_free_coherent(dev, len, info->cpu_addr, info->handle); |
| 86 | err: |
| 87 | kfree(info); |
| 88 | return ION_CMA_ALLOCATE_FAILED; |
| 89 | } |
| 90 | |
| 91 | static void ion_cma_free(struct ion_buffer *buffer) |
| 92 | { |
| 93 | struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap); |
| 94 | struct device *dev = cma_heap->dev; |
| 95 | struct ion_cma_buffer_info *info = buffer->priv_virt; |
| 96 | |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 97 | /* release memory */ |
| 98 | dma_free_coherent(dev, buffer->size, info->cpu_addr, info->handle); |
| 99 | /* release sg table */ |
| 100 | sg_free_table(info->table); |
| 101 | kfree(info->table); |
| 102 | kfree(info); |
| 103 | } |
| 104 | |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 105 | static int ion_cma_mmap(struct ion_heap *mapper, struct ion_buffer *buffer, |
| 106 | struct vm_area_struct *vma) |
| 107 | { |
| 108 | struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap); |
| 109 | struct device *dev = cma_heap->dev; |
| 110 | struct ion_cma_buffer_info *info = buffer->priv_virt; |
| 111 | |
| 112 | return dma_mmap_coherent(dev, vma, info->cpu_addr, info->handle, |
| 113 | buffer->size); |
| 114 | } |
| 115 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 116 | static void *ion_cma_map_kernel(struct ion_heap *heap, |
| 117 | struct ion_buffer *buffer) |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 118 | { |
| 119 | struct ion_cma_buffer_info *info = buffer->priv_virt; |
| 120 | /* kernel memory mapping has been done at allocation time */ |
| 121 | return info->cpu_addr; |
| 122 | } |
| 123 | |
Colin Cross | 661f82f | 2013-12-13 19:26:32 -0800 | [diff] [blame] | 124 | static void ion_cma_unmap_kernel(struct ion_heap *heap, |
Ben LeMasurier | 679011b | 2016-08-22 07:45:53 -0600 | [diff] [blame] | 125 | struct ion_buffer *buffer) |
Colin Cross | 661f82f | 2013-12-13 19:26:32 -0800 | [diff] [blame] | 126 | { |
| 127 | } |
| 128 | |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 129 | static struct ion_heap_ops ion_cma_ops = { |
| 130 | .allocate = ion_cma_allocate, |
| 131 | .free = ion_cma_free, |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 132 | .map_user = ion_cma_mmap, |
| 133 | .map_kernel = ion_cma_map_kernel, |
Colin Cross | 661f82f | 2013-12-13 19:26:32 -0800 | [diff] [blame] | 134 | .unmap_kernel = ion_cma_unmap_kernel, |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *data) |
| 138 | { |
| 139 | struct ion_cma_heap *cma_heap; |
| 140 | |
| 141 | cma_heap = kzalloc(sizeof(struct ion_cma_heap), GFP_KERNEL); |
| 142 | |
| 143 | if (!cma_heap) |
| 144 | return ERR_PTR(-ENOMEM); |
| 145 | |
| 146 | cma_heap->heap.ops = &ion_cma_ops; |
Sriram Raghunathan | 7e41617 | 2015-09-22 22:35:51 +0530 | [diff] [blame] | 147 | /* |
| 148 | * get device from private heaps data, later it will be |
| 149 | * used to make the link with reserved CMA memory |
| 150 | */ |
Benjamin Gaignard | 349c9e1 | 2013-12-13 14:24:44 -0800 | [diff] [blame] | 151 | cma_heap->dev = data->priv; |
| 152 | cma_heap->heap.type = ION_HEAP_TYPE_DMA; |
| 153 | return &cma_heap->heap; |
| 154 | } |
| 155 | |
| 156 | void ion_cma_heap_destroy(struct ion_heap *heap) |
| 157 | { |
| 158 | struct ion_cma_heap *cma_heap = to_cma_heap(heap); |
| 159 | |
| 160 | kfree(cma_heap); |
| 161 | } |