Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/gpu/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/ion.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/dma-mapping.h> |
Laura Abbott | 4979d97 | 2012-08-15 11:09:10 -0700 | [diff] [blame] | 24 | #include <linux/msm_ion.h> |
| 25 | #include <mach/iommu_domains.h> |
| 26 | |
| 27 | #include <asm/cacheflush.h> |
Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 28 | |
| 29 | /* for ion_heap_ops structure */ |
| 30 | #include "ion_priv.h" |
| 31 | |
| 32 | #define ION_CMA_ALLOCATE_FAILED -1 |
| 33 | |
| 34 | struct ion_cma_buffer_info { |
| 35 | void *cpu_addr; |
| 36 | dma_addr_t handle; |
| 37 | struct sg_table *table; |
Laura Abbott | 4979d97 | 2012-08-15 11:09:10 -0700 | [diff] [blame] | 38 | bool is_cached; |
Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Laura Abbott | 4979d97 | 2012-08-15 11:09:10 -0700 | [diff] [blame] | 41 | static int cma_heap_has_outer_cache; |
Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 42 | /* |
| 43 | * Create scatter-list for the already allocated DMA buffer. |
| 44 | * This function could be replace by dma_common_get_sgtable |
| 45 | * as soon as it will avalaible. |
| 46 | */ |
| 47 | int ion_cma_get_sgtable(struct device *dev, struct sg_table *sgt, |
| 48 | void *cpu_addr, dma_addr_t handle, size_t size) |
| 49 | { |
Laura Abbott | f2f4285 | 2013-04-15 11:15:54 -0700 | [diff] [blame] | 50 | struct page *page = phys_to_page(handle); |
Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 51 | int ret; |
| 52 | |
| 53 | ret = sg_alloc_table(sgt, 1, GFP_KERNEL); |
| 54 | if (unlikely(ret)) |
| 55 | return ret; |
| 56 | |
| 57 | sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | /* ION CMA heap operations functions */ |
| 62 | static int ion_cma_allocate(struct ion_heap *heap, struct ion_buffer *buffer, |
| 63 | unsigned long len, unsigned long align, |
| 64 | unsigned long flags) |
| 65 | { |
| 66 | struct device *dev = heap->priv; |
| 67 | struct ion_cma_buffer_info *info; |
| 68 | |
| 69 | dev_dbg(dev, "Request buffer allocation len %ld\n", len); |
| 70 | |
| 71 | info = kzalloc(sizeof(struct ion_cma_buffer_info), GFP_KERNEL); |
| 72 | if (!info) { |
| 73 | dev_err(dev, "Can't allocate buffer info\n"); |
| 74 | return ION_CMA_ALLOCATE_FAILED; |
| 75 | } |
| 76 | |
Laura Abbott | 4979d97 | 2012-08-15 11:09:10 -0700 | [diff] [blame] | 77 | if (!ION_IS_CACHED(flags)) |
| 78 | info->cpu_addr = dma_alloc_writecombine(dev, len, |
Laura Abbott | 83062cf | 2013-06-19 10:07:21 -0700 | [diff] [blame] | 79 | &(info->handle), GFP_KERNEL); |
Laura Abbott | 4979d97 | 2012-08-15 11:09:10 -0700 | [diff] [blame] | 80 | else |
| 81 | info->cpu_addr = dma_alloc_nonconsistent(dev, len, |
Laura Abbott | 83062cf | 2013-06-19 10:07:21 -0700 | [diff] [blame] | 82 | &(info->handle), GFP_KERNEL); |
Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 83 | |
| 84 | if (!info->cpu_addr) { |
| 85 | dev_err(dev, "Fail to allocate buffer\n"); |
| 86 | goto err; |
| 87 | } |
| 88 | |
| 89 | info->table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 90 | if (!info->table) { |
| 91 | dev_err(dev, "Fail to allocate sg table\n"); |
| 92 | goto err; |
| 93 | } |
| 94 | |
Laura Abbott | 4979d97 | 2012-08-15 11:09:10 -0700 | [diff] [blame] | 95 | info->is_cached = ION_IS_CACHED(flags); |
| 96 | |
Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 97 | ion_cma_get_sgtable(dev, |
| 98 | info->table, info->cpu_addr, info->handle, len); |
| 99 | |
| 100 | /* keep this for memory release */ |
| 101 | buffer->priv_virt = info; |
| 102 | dev_dbg(dev, "Allocate buffer %p\n", buffer); |
| 103 | return 0; |
| 104 | |
| 105 | err: |
| 106 | kfree(info); |
| 107 | return ION_CMA_ALLOCATE_FAILED; |
| 108 | } |
| 109 | |
| 110 | static void ion_cma_free(struct ion_buffer *buffer) |
| 111 | { |
| 112 | struct device *dev = buffer->heap->priv; |
| 113 | struct ion_cma_buffer_info *info = buffer->priv_virt; |
| 114 | |
| 115 | dev_dbg(dev, "Release buffer %p\n", buffer); |
| 116 | /* release memory */ |
| 117 | dma_free_coherent(dev, buffer->size, info->cpu_addr, info->handle); |
Laura Abbott | ab439ca | 2013-05-16 13:00:08 -0700 | [diff] [blame] | 118 | sg_free_table(info->table); |
Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 119 | /* release sg table */ |
| 120 | kfree(info->table); |
| 121 | kfree(info); |
| 122 | } |
| 123 | |
| 124 | /* return physical address in addr */ |
| 125 | static int ion_cma_phys(struct ion_heap *heap, struct ion_buffer *buffer, |
| 126 | ion_phys_addr_t *addr, size_t *len) |
| 127 | { |
| 128 | struct device *dev = heap->priv; |
| 129 | struct ion_cma_buffer_info *info = buffer->priv_virt; |
| 130 | |
Laura Abbott | bd4af16 | 2013-03-18 11:14:47 -0700 | [diff] [blame] | 131 | dev_dbg(dev, "Return buffer %p physical address 0x%pa\n", buffer, |
| 132 | &info->handle); |
Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 133 | |
Laura Abbott | 4979d97 | 2012-08-15 11:09:10 -0700 | [diff] [blame] | 134 | *addr = info->handle; |
Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 135 | *len = buffer->size; |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | struct sg_table *ion_cma_heap_map_dma(struct ion_heap *heap, |
| 141 | struct ion_buffer *buffer) |
| 142 | { |
| 143 | struct ion_cma_buffer_info *info = buffer->priv_virt; |
| 144 | |
| 145 | return info->table; |
| 146 | } |
| 147 | |
| 148 | void ion_cma_heap_unmap_dma(struct ion_heap *heap, |
| 149 | struct ion_buffer *buffer) |
| 150 | { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | static int ion_cma_mmap(struct ion_heap *mapper, struct ion_buffer *buffer, |
| 155 | struct vm_area_struct *vma) |
| 156 | { |
| 157 | struct device *dev = buffer->heap->priv; |
| 158 | struct ion_cma_buffer_info *info = buffer->priv_virt; |
| 159 | |
Laura Abbott | 4979d97 | 2012-08-15 11:09:10 -0700 | [diff] [blame] | 160 | if (info->is_cached) |
| 161 | return dma_mmap_nonconsistent(dev, vma, info->cpu_addr, |
| 162 | info->handle, buffer->size); |
| 163 | else |
| 164 | return dma_mmap_writecombine(dev, vma, info->cpu_addr, |
| 165 | info->handle, buffer->size); |
| 166 | } |
| 167 | |
| 168 | static void *ion_cma_map_kernel(struct ion_heap *heap, |
| 169 | struct ion_buffer *buffer) |
| 170 | { |
| 171 | struct ion_cma_buffer_info *info = buffer->priv_virt; |
| 172 | |
| 173 | return info->cpu_addr; |
| 174 | } |
| 175 | |
| 176 | static void ion_cma_unmap_kernel(struct ion_heap *heap, |
| 177 | struct ion_buffer *buffer) |
| 178 | { |
| 179 | return; |
| 180 | } |
| 181 | |
Chintan Pandya | daf7562 | 2013-01-29 19:40:01 +0530 | [diff] [blame] | 182 | static int ion_cma_print_debug(struct ion_heap *heap, struct seq_file *s, |
Mitchel Humpherys | ee0aa9c | 2013-11-15 22:56:04 -0800 | [diff] [blame] | 183 | const struct list_head *mem_map) |
Chintan Pandya | daf7562 | 2013-01-29 19:40:01 +0530 | [diff] [blame] | 184 | { |
| 185 | if (mem_map) { |
Mitchel Humpherys | ee0aa9c | 2013-11-15 22:56:04 -0800 | [diff] [blame] | 186 | struct mem_map_data *data; |
Chintan Pandya | daf7562 | 2013-01-29 19:40:01 +0530 | [diff] [blame] | 187 | |
| 188 | seq_printf(s, "\nMemory Map\n"); |
| 189 | seq_printf(s, "%16.s %14.s %14.s %14.s\n", |
| 190 | "client", "start address", "end address", |
| 191 | "size (hex)"); |
| 192 | |
Mitchel Humpherys | ee0aa9c | 2013-11-15 22:56:04 -0800 | [diff] [blame] | 193 | list_for_each_entry(data, mem_map, node) { |
Chintan Pandya | daf7562 | 2013-01-29 19:40:01 +0530 | [diff] [blame] | 194 | const char *client_name = "(null)"; |
| 195 | |
| 196 | |
| 197 | if (data->client_name) |
| 198 | client_name = data->client_name; |
| 199 | |
Laura Abbott | 1135c9e | 2013-03-13 15:33:40 -0700 | [diff] [blame] | 200 | seq_printf(s, "%16.s %14pa %14pa %14lu (%lx)\n", |
| 201 | client_name, &data->addr, |
| 202 | &data->addr_end, |
Chintan Pandya | daf7562 | 2013-01-29 19:40:01 +0530 | [diff] [blame] | 203 | data->size, data->size); |
| 204 | } |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 209 | static struct ion_heap_ops ion_cma_ops = { |
| 210 | .allocate = ion_cma_allocate, |
| 211 | .free = ion_cma_free, |
| 212 | .map_dma = ion_cma_heap_map_dma, |
| 213 | .unmap_dma = ion_cma_heap_unmap_dma, |
| 214 | .phys = ion_cma_phys, |
| 215 | .map_user = ion_cma_mmap, |
Laura Abbott | 4979d97 | 2012-08-15 11:09:10 -0700 | [diff] [blame] | 216 | .map_kernel = ion_cma_map_kernel, |
| 217 | .unmap_kernel = ion_cma_unmap_kernel, |
Chintan Pandya | daf7562 | 2013-01-29 19:40:01 +0530 | [diff] [blame] | 218 | .print_debug = ion_cma_print_debug, |
Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *data) |
| 222 | { |
| 223 | struct ion_heap *heap; |
| 224 | |
| 225 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 226 | |
| 227 | if (!heap) |
| 228 | return ERR_PTR(-ENOMEM); |
| 229 | |
| 230 | heap->ops = &ion_cma_ops; |
| 231 | /* set device as private heaps data, later it will be |
| 232 | * used to make the link with reserved CMA memory */ |
| 233 | heap->priv = data->priv; |
| 234 | heap->type = ION_HEAP_TYPE_DMA; |
Laura Abbott | 4979d97 | 2012-08-15 11:09:10 -0700 | [diff] [blame] | 235 | cma_heap_has_outer_cache = data->has_outer_cache; |
Benjamin Gaignard | 07b590e | 2012-08-15 10:55:10 -0700 | [diff] [blame] | 236 | return heap; |
| 237 | } |
| 238 | |
| 239 | void ion_cma_heap_destroy(struct ion_heap *heap) |
| 240 | { |
| 241 | kfree(heap); |
| 242 | } |